Browse Source

Sensor: re-initialise after a gateway reconnect; say why a gateway connection dropped

Field: the Ethernet2UART gateway connection dropped and reconnected
within a second ("Peer Closes Connection!" -> "Connected to ..."), but the
2D sensor stayed offline until la_mojo was restarted.

The sensor was re-initialised (MODE_DETECTOR + handshake) only if its
thread saw the transport's short CS_DISCONNECTED. On a fast reconnect it
did not, and stayed in idle mode, believing the device to be in detector
mode (m_bActualDeviceWM) and only polling getState2. A device that left
detector mode while the link was down was never set back. A restart
starts from MODE_NONE, which is why it helped.

- basicTransport::getConnectEpoch(): +1 on every change to CS_CONNECTED.
- device2DSensor remembers the epoch when it binds its port
  (connectMode). validateCommunicationRessource() (every loop pass
  above connection mode) runs the connection sequence again when the
  epoch changed: "Transport reconnected - re-initialising the sensor".
- deviceETH2UART: "Peer Closes Connection!" says why: "closed by the
  gateway" (FIN) or "error: <errno>" (e.g. Connection timed out = sent
  data not acknowledged within E2UART_TCP_USER_TIMEOUT_MS), plus the
  TCP_INFO figures.

Tested with the real gateway class: a device that goes silent (reconnect
after 5 unanswered requests) and a gateway that closes the connection.
After each reconnect the epoch changes (1 -> 2), so the sensor would
re-initialise, and requests are answered again. The close is logged as
"closed by the gateway; TCP: ...". ASan clean. The full 2D sensor
can't run here.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16
pull/26/head
Claude 6 hours ago
parent
commit
ec409b37ca
Failed to extract signature
  1. 16
      lib/App/common/deviceNode2D.cpp
  2. 1
      lib/App/common/deviceNode2D.h
  3. 21
      lib/App/common/deviceNodeEthernet2Serial.cpp
  4. 5
      lib/components/base/basicTransport.cpp
  5. 7
      lib/components/base/basicTransport.h

16
lib/App/common/deviceNode2D.cpp

@ -36,6 +36,7 @@ device2DSensor::device2DSensor(uint32_t id) :
protocolLGS_2D() protocolLGS_2D()
{ {
m_pPort = NULL; m_pPort = NULL;
m_nPortEpoch = 0;
m_bPortType = 0; m_bPortType = 0;
m_bOpRetryCnt = LGS_CMD_RETRY_COUNT; m_bOpRetryCnt = LGS_CMD_RETRY_COUNT;
m_nProcessingCmdId = 0; m_nProcessingCmdId = 0;
@ -302,6 +303,7 @@ void device2DSensor::connectMode()
updateDataStatus(ARGUS_DS_CONNECTING); updateDataStatus(ARGUS_DS_CONNECTING);
m_bActualDeviceWM = MODE_NONE; m_bActualDeviceWM = MODE_NONE;
m_bRequestedDeviceWM = MODE_NONE; m_bRequestedDeviceWM = MODE_NONE;
m_nPortEpoch = m_pPort->getConnectEpoch();
transactionSetResult(TRANSACTION_RESULT_OK); transactionSetResult(TRANSACTION_RESULT_OK);
break; break;
}; };
@ -3302,6 +3304,20 @@ void device2DSensor::proceedDBCommands()
bool device2DSensor::validateCommunicationRessource() 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) if (getConnectionStatus() != CS_CONNECTED)
{ {
#ifdef DEBUG_EXTRA #ifdef DEBUG_EXTRA

1
lib/App/common/deviceNode2D.h

@ -135,6 +135,7 @@ class device2DSensor : public deviceNodeSensor, public protocolLGS_2D
uint8_t m_bPortType; uint8_t m_bPortType;
basicTransport * m_pPort; basicTransport * m_pPort;
uint32_t m_nPortEpoch; // m_pPort->getConnectEpoch() when the sensor was (re)initialised
uint32_t m_nSleepTime; uint32_t m_nSleepTime;
uint8_t m_bInitialFlips; uint8_t m_bInitialFlips;
uint16_t m_nCurrentAngle; uint16_t m_nCurrentAngle;

21
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)) 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); shutdown(m_iDeviceFD, 2);
close(m_iDeviceFD); close(m_iDeviceFD);
*BufferLength = 0; *BufferLength = 0;

5
lib/components/base/basicTransport.cpp

@ -11,6 +11,7 @@ basicTransport::basicTransport() :
{ {
m_pLog = basicLogCollection::getInstance(); m_pLog = basicLogCollection::getInstance();
m_eConnectionStatus = CS_UNKNOWN; m_eConnectionStatus = CS_UNKNOWN;
m_nConnectEpoch = 0;
} }
basicTransport::~basicTransport() basicTransport::~basicTransport()
@ -48,6 +49,10 @@ bool basicTransport::removeRecipient(protocolParser * ProtoParser)
void basicTransport::setConnectionStatus(CONNECTION_STATUS CS) void basicTransport::setConnectionStatus(CONNECTION_STATUS CS)
{ {
m_mQueueProtectorMutex.lock(); m_mQueueProtectorMutex.lock();
if (CS == CS_CONNECTED && m_eConnectionStatus != CS_CONNECTED)
{
m_nConnectEpoch = m_nConnectEpoch + 1;
};
m_eConnectionStatus = CS; m_eConnectionStatus = CS;
for (parseQueue::iterator index = m_vParsers.begin(); index != m_vParsers.end(); index++) for (parseQueue::iterator index = m_vParsers.begin(); index != m_vParsers.end(); index++)
{ {

7
lib/components/base/basicTransport.h

@ -57,6 +57,7 @@ class basicTransport
basicMutex m_mBufferProtectorMutex; basicMutex m_mBufferProtectorMutex;
CONNECTION_STATUS m_eConnectionStatus; CONNECTION_STATUS m_eConnectionStatus;
volatile uint32_t m_nConnectEpoch; // +1 on every change to CS_CONNECTED (a reconnect)
public: public:
@ -93,6 +94,12 @@ class basicTransport
} }
void setConnectionStatus(CONNECTION_STATUS ES); 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); CONNECTION_STATUS getConnectionStatus(void);
virtual int getRawHandle(void) virtual int getRawHandle(void)

Loading…
Cancel
Save