semf
uarthardware.cpp
Go to the documentation of this file.
1
12#include <cstdint>
13
14namespace semf
15{
16void UartHardware::write(const uint8_t data[], size_t dataSize)
17{
18 SEMF_INFO("data %p, dataSize is %u", data, dataSize);
19 if (m_isBusyWriting)
20 {
21 SEMF_ERROR("is busy");
22 error(Error(kSemfClassId, static_cast<uint8_t>(ErrorCode::Write_IsBusy)));
23 return;
24 }
25 if (data == nullptr)
26 {
27 SEMF_ERROR("data is nullptr");
28 error(Error(kSemfClassId, static_cast<uint8_t>(ErrorCode::Write_DataIsNullptr)));
29 return;
30 }
31 if (dataSize == 0)
32 {
33 SEMF_ERROR("dataSize is 0");
34 error(Error(kSemfClassId, static_cast<uint8_t>(ErrorCode::Write_DataSizeIsZero)));
35 return;
36 }
37
38 m_isBusyWriting = true;
39 writeHardware(data, dataSize);
40}
41
42void UartHardware::read(uint8_t buffer[], size_t bufferSize)
43{
44 SEMF_INFO("buffer %p, bufferSize is %u", buffer, bufferSize);
45 if (m_isBusyReading)
46 {
47 SEMF_ERROR("is busy");
48 error(Error(kSemfClassId, static_cast<uint8_t>(ErrorCode::Read_IsBusy)));
49 return;
50 }
51 if (buffer == nullptr)
52 {
53 SEMF_ERROR("data is nullptr");
54 error(Error(kSemfClassId, static_cast<uint8_t>(ErrorCode::Read_BufferIsNullptr)));
55 return;
56 }
57 if (bufferSize == 0)
58 {
59 SEMF_ERROR("dataSize is 0");
60 error(Error(kSemfClassId, static_cast<uint8_t>(ErrorCode::Read_BufferSizeIsZero)));
61 return;
62 }
63
64 m_isBusyReading = true;
65 readHardware(buffer, bufferSize);
66}
67
69{
70 return m_isBusyReading;
71}
72
74{
75 return m_isBusyWriting;
76}
77
79{
80 m_isBusyReading = isBusy;
81}
82
84{
85 m_isBusyWriting = isBusy;
86}
87
89{
90 m_isBusyWriting = false;
91 SEMF_INFO("data written");
93}
94
96{
97 m_isBusyReading = false;
98 SEMF_INFO("data available");
100}
101
103{
104 m_isBusyReading = false;
105 m_isBusyWriting = false;
106 SEMF_ERROR("error");
107 error(thrown);
108}
109} /* namespace semf */
Class for representing errors. Every error should have a unique source code. As a user feel encourage...
Definition: error.h:22
void setBusyReading(bool isBusy)
Sets the busy flag for reading.
void onError(Error thrown)
Is called if an error occurred by hardware read or write access. Will throw error signal.
virtual void readHardware(uint8_t buffer[], size_t bufferSize)=0
Hardware will read data in interrupt mode.
void write(const uint8_t data[], size_t dataSize) override
For writing data, dataWritten signal will be emitted after successful write.
void setBusyWriting(bool isBusy)
Sets the busy flag for writing.
bool isBusyReading() const override
Communication hardware is busy reading at the moment.
void read(uint8_t buffer[], size_t bufferSize) override
For reading data, dataAvailable signal will be emitted after successful read.
virtual void writeHardware(const uint8_t data[], size_t dataSize)=0
Hardware will write data in interrupt mode.
bool isBusyWriting() const override
Communication hardware is busy writing at the moment.
Signal< Error > error
Definition: communication.h:64
#define SEMF_ERROR(...)
Definition: debug.h:39
#define SEMF_INFO(...)
Definition: debug.h:41