Skip to content

Commit

Permalink
more const, safety checks, formatting, docs
Browse files Browse the repository at this point in the history
  • Loading branch information
john30 committed Dec 1, 2023
1 parent 92675ba commit f00db14
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 14 deletions.
19 changes: 10 additions & 9 deletions src/lib/ebus/device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ using std::fixed;
#define ENH_RES_ERROR_EBUS ((uint8_t)0xb)
#define ENH_RES_ERROR_HOST ((uint8_t)0xc)

// ebusd enhanced error codes for the ERROR_* responses
// ebusd enhanced error codes for the ENH_RES_ERROR_* responses
#define ENH_ERR_FRAMING ((uint8_t)0x00)
#define ENH_ERR_OVERRUN ((uint8_t)0x01)

Expand All @@ -70,7 +70,7 @@ result_t PlainCharDevice::send(symbol_t value) {
}

result_t PlainCharDevice::recv(unsigned int timeout, symbol_t* value, ArbitrationState* arbitrationState) {
if (m_arbitrationMaster != SYN) {
if (m_arbitrationMaster != SYN && arbitrationState) {
*arbitrationState = as_running;
}
uint64_t until = timeout == 0 ? 0 : clockGetMillis() + timeout + m_transport->getLatency();
Expand Down Expand Up @@ -105,7 +105,7 @@ result_t PlainCharDevice::recv(unsigned int timeout, symbol_t* value, Arbitratio
result = RESULT_CONTINUE;
}
if (*value != SYN || m_arbitrationMaster == SYN || m_arbitrationCheck) {
if (m_arbitrationMaster != SYN) {
if (m_arbitrationMaster != SYN && arbitrationState) {
if (m_arbitrationCheck) {
*arbitrationState = *value == m_arbitrationMaster ? as_won : as_lost;
m_arbitrationMaster = SYN;
Expand All @@ -116,7 +116,7 @@ result_t PlainCharDevice::recv(unsigned int timeout, symbol_t* value, Arbitratio
}
return result;
}
if (len == 1) {
if (len == 1 && arbitrationState) {
// arbitration executed by ebusd itself
bool wrote = m_transport->write(&m_arbitrationMaster, 1) == RESULT_OK; // send as fast as possible
if (!wrote) {
Expand Down Expand Up @@ -285,7 +285,7 @@ result_t EnhancedCharDevice::send(symbol_t value) {
}

result_t EnhancedCharDevice::recv(unsigned int timeout, symbol_t* value, ArbitrationState* arbitrationState) {
if (m_arbitrationMaster != SYN) {
if (arbitrationState && m_arbitrationMaster != SYN) {
*arbitrationState = as_running;
}
uint64_t until = timeout == 0 ? 0 : clockGetMillis() + timeout + m_transport->getLatency();
Expand Down Expand Up @@ -366,7 +366,6 @@ result_t EnhancedCharDevice::notifyTransportStatus(bool opened) {
return result;
}


result_t EnhancedCharDevice::handleEnhancedBufferedData(const uint8_t* data, size_t len,
symbol_t* value, ArbitrationState* arbitrationState) {
bool valueSet = false;
Expand Down Expand Up @@ -415,7 +414,9 @@ symbol_t* value, ArbitrationState* arbitrationState) {
break;
}
sent = cmd == ENH_RES_STARTED;
*arbitrationState = sent ? as_won : as_lost;
if (arbitrationState) {
*arbitrationState = sent ? as_won : as_lost;
}
m_arbitrationMaster = SYN;
m_arbitrationCheck = 0;
*value = data;
Expand All @@ -429,7 +430,7 @@ symbol_t* value, ArbitrationState* arbitrationState) {
break;
}
*value = data;
if (data == SYN && *arbitrationState == as_running && m_arbitrationCheck) {
if (data == SYN && arbitrationState && *arbitrationState == as_running && m_arbitrationCheck) {
if (m_arbitrationCheck < 3) { // wait for three SYN symbols before switching to timeout
m_arbitrationCheck++;
} else {
Expand All @@ -441,7 +442,7 @@ symbol_t* value, ArbitrationState* arbitrationState) {
valueSet = true;
break;
case ENH_RES_RESETTED:
if (*arbitrationState != as_none) {
if (arbitrationState && *arbitrationState != as_none) {
*arbitrationState = as_error;
m_arbitrationMaster = SYN;
m_arbitrationCheck = 0;
Expand Down
4 changes: 2 additions & 2 deletions src/lib/ebus/device.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ namespace ebusd {
* The data transport itself is handled by a @a Transport instance.
*/

/** the arbitration state handled by @a Device. */
/** the arbitration state handled by @a CharDevice. */
enum ArbitrationState {
as_none, //!< no arbitration in process
as_start, //!< arbitration start requested
Expand All @@ -62,7 +62,7 @@ class DeviceListener {
* @param len the length of received/sent data.
* @param received @a true on reception, @a false on sending.
*/
virtual void notifyDeviceData(symbol_t* data, size_t len, bool received) = 0; // abstract
virtual void notifyDeviceData(const symbol_t* data, size_t len, bool received) = 0; // abstract

/**
* Called to notify a status message from the device.
Expand Down
2 changes: 1 addition & 1 deletion src/lib/ebus/protocol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ void ProtocolHandler::formatInfoJson(ostringstream* ostream) const {
m_device->formatInfoJson(ostream);
}

void ProtocolHandler::notifyDeviceData(symbol_t* data, size_t len, bool received) {
void ProtocolHandler::notifyDeviceData(const symbol_t* data, size_t len, bool received) {
if (received && m_dumpFile) {
m_dumpFile->write(data, len);
}
Expand Down
4 changes: 2 additions & 2 deletions src/lib/ebus/protocol.h
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ class ProtocolListener {
/**
* Handles input from and output to eBUS with respect to the eBUS protocol.
*/
class ProtocolHandler : public WaitThread, DeviceListener {
class ProtocolHandler : public WaitThread, public DeviceListener {
public:
/**
* Construct a new instance.
Expand Down Expand Up @@ -381,7 +381,7 @@ class ProtocolHandler : public WaitThread, DeviceListener {
virtual bool supportsUpdateCheck() const { return m_device->supportsUpdateCheck(); }

// @copydoc
virtual void notifyDeviceData(symbol_t* symbols, size_t len, bool received);
virtual void notifyDeviceData(const symbol_t* data, size_t len, bool received);

// @copydoc
void notifyDeviceStatus(bool error, const char* message) override;
Expand Down

0 comments on commit f00db14

Please sign in to comment.