Skip to content

Commit

Permalink
add result code to protocol listener
Browse files Browse the repository at this point in the history
  • Loading branch information
john30 committed Nov 11, 2023
1 parent dbbf235 commit bfe7b45
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/ebusd/bushandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ result_t BusHandler::readFromBus(Message* message, const string& inputStr, symbo
return ret;
}

void BusHandler::notifyProtocolStatus(ProtocolState state) {
void BusHandler::notifyProtocolStatus(ProtocolState state, result_t result) {
if (state == ps_empty && m_pollInterval > 0) { // check for poll/scan
time_t now;
time(&now);
Expand Down
2 changes: 1 addition & 1 deletion src/ebusd/bushandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@ class BusHandler : public ProtocolListener {
void setScanConfigLoaded(symbol_t address, const string& file);

// @copydoc
void notifyProtocolStatus(ProtocolState state) override;
void notifyProtocolStatus(ProtocolState state, result_t result) override;

// @copydoc
result_t notifyProtocolAnswer(const MasterSymbolString& master, SlaveSymbolString* slave) override;
Expand Down
12 changes: 12 additions & 0 deletions src/lib/ebus/protocol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,18 @@ using std::hex;
using std::setfill;
using std::setw;

const char* getProtocolStateCode(ProtocolState state) {
switch (state) {
case ps_noSignal: return "no signal";
case ps_idle: return "idle";
case ps_idleSYN: return "idle, SYN generator";
case ps_recv: return "receive";
case ps_send: return "send";
case ps_empty: return "idle, empty";
default: return "unknown";
}
}

bool ActiveBusRequest::notify(result_t result, const SlaveSymbolString& slave) {
if (result == RESULT_OK) {
string str = slave.getStr();
Expand Down
17 changes: 15 additions & 2 deletions src/lib/ebus/protocol.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,18 @@ typedef struct ebus_protocol_config {
enum ProtocolState {
ps_noSignal, //!< no signal on the bus
ps_idle, //!< idle (after @a SYN symbol)
ps_idleSYN, //!< idle (after sent SYN symbol in acting as SYN generator)
ps_recv, //!< receiving
ps_send, //!< sending
ps_empty, //!< idle, no more lock remaining, and no other request queued
};

/**
* Return the string corresponding to the @a ProtocolState.
* @param state the @a ProtocolState.
* @return the string corresponding to the @a ProtocolState.
*/
const char* getProtocolStateCode(ProtocolState state);

class ProtocolHandler;

Expand Down Expand Up @@ -208,8 +217,9 @@ class ProtocolListener {
/**
* Called to notify a status update from the protocol.
* @param state the current protocol state.
* @param result the error code reason for the state change, or @a RESULT_OK.
*/
virtual void notifyProtocolStatus(ProtocolState state) = 0; // abstract
virtual void notifyProtocolStatus(ProtocolState state, result_t result) = 0; // abstract

/**
* Called to notify a new valid seen address on the bus.
Expand Down Expand Up @@ -253,7 +263,7 @@ class ProtocolHandler : public WaitThread, DeviceListener {
ProtocolHandler(const ebus_protocol_config_t config,
Device* device, ProtocolListener* listener)
: WaitThread(), m_config(config), m_device(device), m_listener(listener),
m_reconnect(false),
m_listenerState(ps_noSignal), m_reconnect(false),
m_ownMasterAddress(config.ownAddress), m_ownSlaveAddress(getSlaveAddress(config.ownAddress)),
m_addressConflict(false),
m_masterCount(config.readOnly ? 0 : 1),
Expand Down Expand Up @@ -519,6 +529,9 @@ class ProtocolHandler : public WaitThread, DeviceListener {
/** the @a ProtocolListener. */
ProtocolListener *m_listener;

/** the last state the listener was informed with. */
ProtocolState m_listenerState;

/** set to @p true when the device shall be reconnected. */
bool m_reconnect;

Expand Down
20 changes: 14 additions & 6 deletions src/lib/ebus/protocol_direct.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ result_t DirectProtocolHandler::handleSymbol() {
if (!m_device->isArbitrating() && m_currentRequest == nullptr && m_remainLockCount == 0) {
BusRequest* startRequest = m_nextRequests.peek();
if (startRequest == nullptr) {
m_listener->notifyProtocolStatus(ps_empty);
m_listener->notifyProtocolStatus(ps_empty, RESULT_OK);
startRequest = m_nextRequests.peek();
}
if (startRequest != nullptr) { // initiate arbitration
Expand Down Expand Up @@ -677,9 +677,6 @@ result_t DirectProtocolHandler::setState(BusState state, result_t result, bool f
}

if (state == bs_noSignal) { // notify all requests
if (m_state != bs_noSignal) {
m_listener->notifyProtocolStatus(ps_idle);
}
m_response.clear(); // notify with empty response
while ((m_currentRequest = m_nextRequests.pop()) != nullptr) {
bool restart = m_currentRequest->notify(RESULT_ERR_NO_SIGNAL, m_response);
Expand All @@ -692,12 +689,13 @@ result_t DirectProtocolHandler::setState(BusState state, result_t result, bool f
m_finishedRequests.push(m_currentRequest);
}
}
} else if (m_state == bs_noSignal) {
m_listener->notifyProtocolStatus(ps_noSignal);
}

m_escape = 0;
if (state == m_state) {
if (m_listener && result != RESULT_OK) {
m_listener->notifyProtocolStatus(m_listenerState, result);
}
return result;
}
if ((result < RESULT_OK && !(result == RESULT_ERR_TIMEOUT && state == bs_skip && m_state == bs_ready))
Expand All @@ -718,6 +716,16 @@ result_t DirectProtocolHandler::setState(BusState state, result_t result, bool f
logNotice(lf_bus, "signal acquired");
}
}
if (m_listener) {
ProtocolState pstate = protocolStateByBusState[state];
if (pstate == ps_idle && m_generateSynInterval == SYN_INTERVAL) {
pstate = ps_idleSYN;
}
if (result != RESULT_OK || pstate != m_listenerState) {
m_listener->notifyProtocolStatus(pstate, result);
m_listenerState = pstate;
}
}
m_state = state;

if (state == bs_ready || state == bs_skip) {
Expand Down

0 comments on commit bfe7b45

Please sign in to comment.