From f00db143c7f165ed94b7c436c13cfcb981ca03e9 Mon Sep 17 00:00:00 2001 From: John Date: Fri, 1 Dec 2023 20:46:35 +0100 Subject: [PATCH] more const, safety checks, formatting, docs --- src/lib/ebus/device.cpp | 19 ++++++++++--------- src/lib/ebus/device.h | 4 ++-- src/lib/ebus/protocol.cpp | 2 +- src/lib/ebus/protocol.h | 4 ++-- 4 files changed, 15 insertions(+), 14 deletions(-) diff --git a/src/lib/ebus/device.cpp b/src/lib/ebus/device.cpp index 5ae58e275..2e6ac88a8 100755 --- a/src/lib/ebus/device.cpp +++ b/src/lib/ebus/device.cpp @@ -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) @@ -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(); @@ -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; @@ -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) { @@ -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(); @@ -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; @@ -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; @@ -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 { @@ -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; diff --git a/src/lib/ebus/device.h b/src/lib/ebus/device.h index 61db9450a..9e67ca908 100755 --- a/src/lib/ebus/device.h +++ b/src/lib/ebus/device.h @@ -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 @@ -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. diff --git a/src/lib/ebus/protocol.cpp b/src/lib/ebus/protocol.cpp index b6fe0b673..3d605f672 100644 --- a/src/lib/ebus/protocol.cpp +++ b/src/lib/ebus/protocol.cpp @@ -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); } diff --git a/src/lib/ebus/protocol.h b/src/lib/ebus/protocol.h index 283952cb3..540945b8d 100755 --- a/src/lib/ebus/protocol.h +++ b/src/lib/ebus/protocol.h @@ -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. @@ -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;