Browse Source

Ethernet2UART gateway: never lose a sensor query around a reconnect

The sensor answers only when queried, and it re-sends its query after
every attempt without reply (idle mode: getState2() in stage 1 again).
The gateway must deliver each of these queries:

- Queued requests are no longer dropped by closePort(). A query queued
  while the I/O thread ended for a reconnect is sent on the new
  connection (the previous commit dropped it, so the sensor stayed
  unqueried for that attempt).
- sendData() queues while the connection is up, while the I/O thread is
  still starting (TH_ARMED: the first query right after a connect was
  refused with "Trying to Send Data Before Thread is Started!"), while
  the thread has ended and the reconnect is pending, and while
  create_SOL() is connecting. It refuses only without any connection.
- At most E2UART_MAX_QUEUED (16) requests wait; the oldest are dropped.
  A failed reconnect clears the queue, because those queries are stale.
- The "NO reply" debug line shows the request bytes (first 12), so a
  repeated query can be recognised in the log.

Tested with a fake device that goes silent after 3 replies: the query
sent while the thread was starting and the query queued during the
reconnect were both answered, and nothing was refused (ASan clean).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16
pull/24/head
Claude 12 hours ago
parent
commit
15c4b9b6a3
Failed to extract signature
  1. 71
      lib/App/common/deviceNodeEthernet2Serial.cpp
  2. 6
      lib/App/common/deviceNodeEthernet2Serial.h

71
lib/App/common/deviceNodeEthernet2Serial.cpp

@ -52,6 +52,7 @@ deviceETH2UART::deviceETH2UART(uint32_t nodeID) :
m_iInFID = -1; m_iInFID = -1;
m_iOutFID = -1; m_iOutFID = -1;
m_bRegistered = false; m_bRegistered = false;
m_bConnecting = false;
#ifdef SERIAL_PING_PONG #ifdef SERIAL_PING_PONG
m_pLastParser = NULL; m_pLastParser = NULL;
#endif #endif
@ -87,6 +88,15 @@ deviceETH2UART::~deviceETH2UART()
} }
std::lock_guard<std::recursive_mutex> tLock(m_mConnLock); std::lock_guard<std::recursive_mutex> tLock(m_mConnLock);
closePort(); closePort();
#ifdef SERIAL_PING_PONG
m_mBufferProtectorMutex.lock();
while (! m_qIO.empty())
{
free(m_qIO.front());
m_qIO.pop();
};
m_mBufferProtectorMutex.unlock();
#endif
} }
bool deviceETH2UART::create(void) bool deviceETH2UART::create(void)
@ -181,6 +191,16 @@ bool deviceETH2UART::connectSOL(void)
m_bLastPingOk = true; m_bLastPingOk = true;
return true; return true;
}; };
#ifdef SERIAL_PING_PONG
// No connection: the queued requests are stale by the next attempt (the protocols query again)
m_mBufferProtectorMutex.lock();
while (! m_qIO.empty())
{
free(m_qIO.front());
m_qIO.pop();
};
m_mBufferProtectorMutex.unlock();
#endif
m_nConnectBackoffMs = (m_nConnectBackoffMs == 0) ? E2UART_RECONNECT_MIN_MS : m_nConnectBackoffMs * 2; m_nConnectBackoffMs = (m_nConnectBackoffMs == 0) ? E2UART_RECONNECT_MIN_MS : m_nConnectBackoffMs * 2;
if (m_nConnectBackoffMs > E2UART_RECONNECT_MAX_MS) if (m_nConnectBackoffMs > E2UART_RECONNECT_MAX_MS)
{ {
@ -428,6 +448,12 @@ bool deviceETH2UART::create_SOL()
{ {
closePort(); closePort();
}; };
struct connectingFlag
{
volatile bool & f;
explicit connectingFlag(volatile bool & x) : f(x) { f = true; }
~connectingFlag() { f = false; }
} tConnecting(m_bConnecting);
switch (m_sUartData.mode) switch (m_sUartData.mode)
{ {
@ -810,16 +836,8 @@ bool deviceETH2UART::closePort()
{ {
uninit(); uninit();
}; };
#ifdef SERIAL_PING_PONG
// Requests not sent on the closed connection: drop them (the protocols re-send after a reconnect)
m_mBufferProtectorMutex.lock();
while (! m_qIO.empty())
{
free(m_qIO.front());
m_qIO.pop();
};
m_mBufferProtectorMutex.unlock();
#endif
// Requests still queued are kept: a query of the sensor queued during a reconnect is sent on the new
// connection (dropping it left the sensor without a query - it only answers when asked)
if (m_iDeviceFD != -1) if (m_iDeviceFD != -1)
{ {
switch (m_sUartData.mode) switch (m_sUartData.mode)
@ -975,6 +993,17 @@ void deviceETH2UART::m_fThreadProc(void)
//Sending Data to line //Sending Data to line
#ifdef DEBUG_COMMUNICATION #ifdef DEBUG_COMMUNICATION
int tSentBytes = tIOData->out_size; int tSentBytes = tIOData->out_size;
char tReqHex[3 * 12 + 4];
{
int tN = (tSentBytes < 12) ? tSentBytes : 12;
for (int k = 0; k < tN; k++){
snprintf(tReqHex + 3 * k, 4, "%02X ", tIOData->out_buffer[k]);
};
tReqHex[tN ? 3 * tN - 1 : 0] = 0;
if (tSentBytes > 12){
strcat(tReqHex, "..");
};
}
uint64_t tSendMs = e2uNowMs(); uint64_t tSendMs = e2uNowMs();
tReqNo++; tReqNo++;
tStReq++; tStReq++;
@ -1051,7 +1080,7 @@ void deviceETH2UART::m_fThreadProc(void)
tStNoReply++; tStNoReply++;
char tTcp[160]; char tTcp[160];
tcpInfoText(tTcp, sizeof(tTcp)); tcpInfoText(tTcp, sizeof(tTcp));
snprintf(tDbg, sizeof(tDbg), "IO: request #%u (%d B): NO reply in %llu ms - %s", tReqNo, tSentBytes, (unsigned long long) (tDoneMs - tSendMs), tTcp);
snprintf(tDbg, sizeof(tDbg), "IO: request #%u [%s]: NO reply in %llu ms - %s", tReqNo, tReqHex, (unsigned long long) (tDoneMs - tSendMs), tTcp);
LOG(tDbg); LOG(tDbg);
} else { } else {
uint64_t tFirst = tFirstByteMs - tSendMs; uint64_t tFirst = tFirstByteMs - tSendMs;
@ -1089,12 +1118,11 @@ void deviceETH2UART::m_fThreadProc(void)
} }
bool deviceETH2UART::sendData(uint8_t * Data, int DataSize, protocolParser *src ){ bool deviceETH2UART::sendData(uint8_t * Data, int DataSize, protocolParser *src ){
if ( m_bThreadStatus != TH_WORKING ) {
if( m_bThreadStatus < TH_WORKING ) {
LOG_ERROR("Trying to Send Data Before Thread is Started!");
} else {
LOG_ERROR("Trying to Send Data After Thread is Dead!");
}
// Queued (sent as soon as the I/O thread runs) while the connection is up, starting (TH_ARMED: the
// first query right after a connect was refused), ended for a reconnect that validate() will do,
// or being opened. Refused only without any connection.
if ( m_bThreadStatus == TH_PRE_INIT && ! m_bConnecting ) {
LOG_ERROR("Trying to Send Data without connection to the gateway!");
return false; return false;
}; };
if (DataSize > MAXIMAL_SERIAL_IO_BUFFER_LENGTH) { if (DataSize > MAXIMAL_SERIAL_IO_BUFFER_LENGTH) {
@ -1106,9 +1134,18 @@ bool deviceETH2UART::sendData(uint8_t * Data, int DataSize, protocolParser *src
tData->out_size = DataSize; tData->out_size = DataSize;
tData->boundParser = src; tData->boundParser = src;
memcpy( tData->out_buffer, Data, DataSize ); memcpy( tData->out_buffer, Data, DataSize );
int tDropped = 0;
m_mBufferProtectorMutex.lock(); m_mBufferProtectorMutex.lock();
while ((int) m_qIO.size() >= E2UART_MAX_QUEUED){
free(m_qIO.front());
m_qIO.pop();
tDropped++;
};
m_qIO.push(tData); m_qIO.push(tData);
m_mBufferProtectorMutex.unlock(); m_mBufferProtectorMutex.unlock();
if (tDropped){
LOG_ERROR("Request queue full - oldest request dropped");
};
return true; return true;
} else { } else {
LOG_ERROR("MemAlloc Error!"); LOG_ERROR("MemAlloc Error!");

6
lib/App/common/deviceNodeEthernet2Serial.h

@ -44,6 +44,11 @@
#endif #endif
// Requests in a row without any reply byte: reconnect to the gateway (the device went silent while // Requests in a row without any reply byte: reconnect to the gateway (the device went silent while
// the TCP connection stayed up - a new connection resets the gateway session / NAT mapping) // the TCP connection stayed up - a new connection resets the gateway session / NAT mapping)
// Requests waiting for the I/O thread (e.g. queries of the sensor during a reconnect): the oldest are
// dropped beyond this (a long outage must not grow the queue without limit)
#ifndef E2UART_MAX_QUEUED
#define E2UART_MAX_QUEUED 16
#endif
#ifndef E2UART_NO_REPLY_RECONNECT #ifndef E2UART_NO_REPLY_RECONNECT
#define E2UART_NO_REPLY_RECONNECT 5 #define E2UART_NO_REPLY_RECONNECT 5
#endif #endif
@ -71,6 +76,7 @@
// hardware manager thread (the class is a deviceNode and a hardwareResource): one at a time // hardware manager thread (the class is a deviceNode and a hardwareResource): one at a time
std::recursive_mutex m_mConnLock; std::recursive_mutex m_mConnLock;
bool m_bRegistered; // assigned to the hardware manager (once) bool m_bRegistered; // assigned to the hardware manager (once)
volatile bool m_bConnecting; // create_SOL() is opening the connection (requests are queued meanwhile)
bool m_bLastPingOk; bool m_bLastPingOk;
uint64_t m_nNextPingMs; uint64_t m_nNextPingMs;
uint64_t m_nNextConnectMs; uint64_t m_nNextConnectMs;

Loading…
Cancel
Save