diff --git a/src/ebusd/bushandler.cpp b/src/ebusd/bushandler.cpp index 8bb95ea3..95fbd81f 100644 --- a/src/ebusd/bushandler.cpp +++ b/src/ebusd/bushandler.cpp @@ -603,8 +603,16 @@ result_t BusHandler::setState(BusState state, result_t result, bool firstRepetit else if (state == bs_sendSyn || (result != RESULT_OK && !firstRepetition)) { logDebug(lf_bus, "notify request: %s", getResultCode(result)); unsigned char dstAddress = m_currentRequest->m_master[1]; - if (result == RESULT_OK && isValidAddress(dstAddress, false)) + if (result == RESULT_OK && isValidAddress(dstAddress, false) && !m_seenAddresses[dstAddress]) { + if (m_autoLockCount) { + unsigned char master = getMasterAddress(dstAddress); + if (master != SYN && !m_seenAddresses[master]) { + m_seenAddresses[master] = true; + m_lockCount++; + } + } m_seenAddresses[dstAddress] = true; + } bool restart = m_currentRequest->notify(result, m_response); if (restart) { m_currentRequest->m_busLostRetries = 0; @@ -664,15 +672,28 @@ void BusHandler::receiveCompleted() { unsigned char srcAddress = m_command[0], dstAddress = m_command[1]; bool master = isMaster(dstAddress); + if (m_autoLockCount && isMaster(srcAddress) && !m_seenAddresses[srcAddress]) { + m_lockCount++; + } m_seenAddresses[srcAddress] = true; if (dstAddress == BROADCAST) logInfo(lf_update, "update BC cmd: %s", m_command.getDataStr().c_str()); else if (master) { logInfo(lf_update, "update MM cmd: %s", m_command.getDataStr().c_str()); + if (m_autoLockCount && !m_seenAddresses[dstAddress]) { + m_lockCount++; + } m_seenAddresses[dstAddress] = true; } else { logInfo(lf_update, "update MS cmd: %s / %s", m_command.getDataStr().c_str(), m_response.getDataStr().c_str()); + if (m_autoLockCount) { + unsigned char master = getMasterAddress(dstAddress); + if (master != SYN && !m_seenAddresses[master]) { + m_seenAddresses[master] = true; + m_lockCount++; + } + } m_seenAddresses[dstAddress] = true; } @@ -736,8 +757,8 @@ result_t BusHandler::startScan(bool full) if (!isValidAddress(slave, false) || isMaster(slave)) continue; if (!full && !m_seenAddresses[slave]) { - unsigned char master = (unsigned char)(slave+256-5); // check if we saw the corresponding master already - if (!isMaster(master) || !m_seenAddresses[slave]) + unsigned char master = getMasterAddress(slave); // check if we saw the corresponding master already + if (master == SYN || !m_seenAddresses[master]) continue; } diff --git a/src/ebusd/bushandler.h b/src/ebusd/bushandler.h index 3e3aba37..de1d60d4 100644 --- a/src/ebusd/bushandler.h +++ b/src/ebusd/bushandler.h @@ -258,7 +258,7 @@ public: * @param failedSendRetries the number of times a failed send is repeated (other than lost arbitration). * @param slaveRecvTimeout the maximum time in microseconds an addressed slave is expected to acknowledge. * @param busAcquireTimeout the maximum time in microseconds for bus acquisition. - * @param lockCount the number of AUTO-SYN symbols before sending is allowed after lost arbitration. + * @param lockCount the number of AUTO-SYN symbols before sending is allowed after lost arbitration, or 0 for auto detection. * @param pollInterval the interval in seconds in which poll messages are cycled, or 0 if disabled. */ BusHandler(Device* device, MessageMap* messages, @@ -270,7 +270,7 @@ public: m_ownMasterAddress(ownAddress), m_ownSlaveAddress((unsigned char)(ownAddress+5)), m_answer(answer), m_busLostRetries(busLostRetries), m_failedSendRetries(failedSendRetries), m_busAcquireTimeout(busAcquireTimeout), m_slaveRecvTimeout(slaveRecvTimeout), - m_lockCount(lockCount), m_remainLockCount(lockCount), + m_autoLockCount(lockCount==0), m_lockCount(lockCount==0 ? 1 : lockCount), m_remainLockCount(lockCount), m_pollInterval(pollInterval), m_lastReceive(0), m_lastPoll(0), m_currentRequest(NULL), m_nextSendPos(0), m_symPerSec(0), m_maxSymPerSec(0), @@ -389,8 +389,11 @@ private: /** the maximum time in microseconds an addressed slave is expected to acknowledge. */ const unsigned int m_slaveRecvTimeout; + /** whether m_lockCount shall be detected automatically. */ + const bool m_autoLockCount; + /** the number of AUTO-SYN symbols before sending is allowed after lost arbitration. */ - const unsigned int m_lockCount; + unsigned int m_lockCount; /** the remaining number of AUTO-SYN symbols before sending is allowed again. */ unsigned int m_remainLockCount;