diff --git a/lib/App/common/deviceNode2D.cpp b/lib/App/common/deviceNode2D.cpp index 3298081f..c855d193 100644 --- a/lib/App/common/deviceNode2D.cpp +++ b/lib/App/common/deviceNode2D.cpp @@ -36,6 +36,7 @@ device2DSensor::device2DSensor(uint32_t id) : protocolLGS_2D() { m_pPort = NULL; + m_nPortEpoch = 0; m_bPortType = 0; m_bOpRetryCnt = LGS_CMD_RETRY_COUNT; m_nProcessingCmdId = 0; @@ -302,6 +303,7 @@ void device2DSensor::connectMode() updateDataStatus(ARGUS_DS_CONNECTING); m_bActualDeviceWM = MODE_NONE; m_bRequestedDeviceWM = MODE_NONE; + m_nPortEpoch = m_pPort->getConnectEpoch(); transactionSetResult(TRANSACTION_RESULT_OK); break; }; @@ -3302,6 +3304,20 @@ void device2DSensor::proceedDBCommands() bool device2DSensor::validateCommunicationRessource() { + /* + The transport (e.g. an Ethernet2UART gateway) reconnected since the sensor was initialised: run the + connection sequence again (MODE_DETECTOR + handshake). The sensor may have left detector mode while + the link was down; a fast reconnect was not seen here as CS_DISCONNECTED, so the sensor was only + polled in idle mode (getState2), believed to be in detector mode, and stayed "offline" until a restart. + */ + if (m_pPort != NULL && m_pPort->getConnectEpoch() != m_nPortEpoch) + { + LOG("Transport reconnected - re-initialising the sensor"); + m_nPortEpoch = m_pPort->getConnectEpoch(); + updateDataStatus(ARGUS_DS_DISCONNECTED); + setWM(LRF_WM_CONNECTION); + return false; + }; if (getConnectionStatus() != CS_CONNECTED) { #ifdef DEBUG_EXTRA diff --git a/lib/App/common/deviceNode2D.h b/lib/App/common/deviceNode2D.h index 190f7d25..8528a223 100644 --- a/lib/App/common/deviceNode2D.h +++ b/lib/App/common/deviceNode2D.h @@ -135,6 +135,7 @@ class device2DSensor : public deviceNodeSensor, public protocolLGS_2D uint8_t m_bPortType; basicTransport * m_pPort; + uint32_t m_nPortEpoch; // m_pPort->getConnectEpoch() when the sensor was (re)initialised uint32_t m_nSleepTime; uint8_t m_bInitialFlips; uint16_t m_nCurrentAngle; diff --git a/lib/App/common/deviceNodeEthernet2Serial.cpp b/lib/App/common/deviceNodeEthernet2Serial.cpp index 4ae0efed..38c333d0 100644 --- a/lib/App/common/deviceNodeEthernet2Serial.cpp +++ b/lib/App/common/deviceNodeEthernet2Serial.cpp @@ -1311,7 +1311,26 @@ int deviceETH2UART::recptBlock(unsigned char * Buffer, int * BufferLength) { if (RetVal == 0 || (errno != EAGAIN && errno != EWOULDBLOCK && errno != EINTR)) { - LOG_ERROR("Peer Closes Connection!"); + // Closed by the gateway (FIN) or an error - e.g. ETIMEDOUT: sent data not acknowledged + // within E2UART_TCP_USER_TIMEOUT_MS, ECONNRESET: reset by the gateway / a NAT on the way + char tWhy[96]; + if (RetVal == 0) + { + snprintf(tWhy, sizeof(tWhy), "closed by the gateway"); + } + else + { + snprintf(tWhy, sizeof(tWhy), "error: %s", strerror(errno)); + }; + char tTcp[160]; +#ifdef SERIAL_PING_PONG + tcpInfoText(tTcp, sizeof(tTcp)); +#else + tTcp[0] = 0; +#endif + char tB[320]; + snprintf(tB, sizeof(tB), "Peer Closes Connection! (%s; %s)", tWhy, tTcp); + LOG_ERROR(tB); shutdown(m_iDeviceFD, 2); close(m_iDeviceFD); *BufferLength = 0; diff --git a/lib/components/base/basicTransport.cpp b/lib/components/base/basicTransport.cpp index af59161f..501bb724 100644 --- a/lib/components/base/basicTransport.cpp +++ b/lib/components/base/basicTransport.cpp @@ -11,6 +11,7 @@ basicTransport::basicTransport() : { m_pLog = basicLogCollection::getInstance(); m_eConnectionStatus = CS_UNKNOWN; + m_nConnectEpoch = 0; } basicTransport::~basicTransport() @@ -48,6 +49,10 @@ bool basicTransport::removeRecipient(protocolParser * ProtoParser) void basicTransport::setConnectionStatus(CONNECTION_STATUS CS) { m_mQueueProtectorMutex.lock(); + if (CS == CS_CONNECTED && m_eConnectionStatus != CS_CONNECTED) + { + m_nConnectEpoch = m_nConnectEpoch + 1; + }; m_eConnectionStatus = CS; for (parseQueue::iterator index = m_vParsers.begin(); index != m_vParsers.end(); index++) { diff --git a/lib/components/base/basicTransport.h b/lib/components/base/basicTransport.h index bf327293..e56861e3 100644 --- a/lib/components/base/basicTransport.h +++ b/lib/components/base/basicTransport.h @@ -57,6 +57,7 @@ class basicTransport basicMutex m_mBufferProtectorMutex; CONNECTION_STATUS m_eConnectionStatus; + volatile uint32_t m_nConnectEpoch; // +1 on every change to CS_CONNECTED (a reconnect) public: @@ -93,6 +94,12 @@ class basicTransport } void setConnectionStatus(CONNECTION_STATUS ES); + // Changes on every (re)connect: a user bound to this transport sees a reconnect it did not notice + // as a short CS_DISCONNECTED (a device behind it may have lost its state meanwhile) + uint32_t getConnectEpoch(void) + { + return m_nConnectEpoch; + } CONNECTION_STATUS getConnectionStatus(void); virtual int getRawHandle(void)