From 1fa1f29a3594383280c2b6243eb2e8664edb48ad Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 1 Dec 2026 08:04:12 +0000 Subject: [PATCH] Ethernet2UART gateway: wait long enough for the device's reply over the Internet "NODSEN: Communication Wait State TO!" with a sensor behind the gateway: in SERIAL_PING_PONG mode the I/O thread waited only 20 x 5 ms (~100 ms, a serial-line value) for the reply to a request, and did not read the socket while no request was queued. A reply slower than that, which is normal over a mobile link, stayed in the socket. It was read with the next request and given to that request's parser, so the sensor ran one reply behind, retried, and after 16 failed attempts reported the timeout. deviceETH2UART I/O thread (SERIAL_PING_PONG) - Waits up to E2UART_REPLY_TIMEOUT_MS (800 ms, below the sensor's 1 s per attempt) for the reply to start, and E2UART_REPLY_GAP_MS (150 ms) between the pieces of a reply split over several TCP segments. It is done as soon as the parser has a complete packet and no more data is waiting (as before). - Reads the socket also between requests. Late data goes to the parser of the last request at once. - Gives data only to a parser that is still bound to the transport (a sensor that disconnected or was deleted is no longer called). Serial resource casts (deviceNode2D, deviceNodeCamera) - dynamic_cast instead of static_cast through hardwareResourceSerialProvider. The resource can be a deviceETH2UART, and the old cast was only right because of a matching base-class layout. A NULL transport isn't bound. Tested with a fake device answering after 50/300/700 ms, also split into two TCP pieces. The old code delivered nothing, or the previous request's answer. Now each request gets its own answer (ASan clean). Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16 --- lib/App/common/deviceNode2D.cpp | 4 +- lib/App/common/deviceNodeCamera.cpp | 16 +- lib/App/common/deviceNodeEthernet2Serial.cpp | 185 +++++++++++-------- lib/App/common/deviceNodeEthernet2Serial.h | 11 ++ 4 files changed, 133 insertions(+), 83 deletions(-) diff --git a/lib/App/common/deviceNode2D.cpp b/lib/App/common/deviceNode2D.cpp index 7f4ad497..d845274f 100644 --- a/lib/App/common/deviceNode2D.cpp +++ b/lib/App/common/deviceNode2D.cpp @@ -280,14 +280,14 @@ void device2DSensor::connectMode() #endif if (tRES->isValid()) { - m_pPort = static_cast(static_cast(tRES)); + m_pPort = dynamic_cast(tRES); #ifdef DEBUG_EXTRA // Bind To Proto char tB[64]; sprintf(tB, "PortOBJ: %p", m_pPort); LOG(tB); #endif - if (! setTransport(m_pPort, false)) + if (m_pPort == NULL || ! setTransport(m_pPort, false)) { LOG_ERROR("Binding Port Failure!"); } diff --git a/lib/App/common/deviceNodeCamera.cpp b/lib/App/common/deviceNodeCamera.cpp index f8daa0ad..8a72d09d 100644 --- a/lib/App/common/deviceNodeCamera.cpp +++ b/lib/App/common/deviceNodeCamera.cpp @@ -362,8 +362,8 @@ bool deviceNodeCamera::create() { if (tRES->isValid()) { - tTransport = static_cast(static_cast(tRES)); - if (m_pProtocol->setTransport(tTransport)) + tTransport = dynamic_cast(tRES); + if (tTransport != NULL && m_pProtocol->setTransport(tTransport)) { if (! m_pProtocol->create()) { @@ -426,8 +426,8 @@ bool deviceNodeCamera::create() { if (tRES->isValid()) { - tTransport = static_cast(static_cast(tRES)); - if (m_pProtocol->setTransport(tTransport)) + tTransport = dynamic_cast(tRES); + if (tTransport != NULL && m_pProtocol->setTransport(tTransport)) { if (! m_pProtocol->create()) { @@ -1304,8 +1304,8 @@ void deviceNodeCamera::m_fThreadProc(void) { if (tRES->isValid()) { - tTransport = static_cast(static_cast(tRES)); - if (m_pProtocol->setTransport(tTransport)) + tTransport = dynamic_cast(tRES); + if (tTransport != NULL && m_pProtocol->setTransport(tTransport)) { if (! m_pProtocol->create()) { @@ -1368,8 +1368,8 @@ void deviceNodeCamera::m_fThreadProc(void) { if (tRES->isValid()) { - tTransport = static_cast(static_cast(tRES)); - if (m_pProtocol->setTransport(tTransport)) + tTransport = dynamic_cast(tRES); + if (tTransport != NULL && m_pProtocol->setTransport(tTransport)) { if (! m_pProtocol->create()) { diff --git a/lib/App/common/deviceNodeEthernet2Serial.cpp b/lib/App/common/deviceNodeEthernet2Serial.cpp index b7e612fc..43e45ee2 100644 --- a/lib/App/common/deviceNodeEthernet2Serial.cpp +++ b/lib/App/common/deviceNodeEthernet2Serial.cpp @@ -46,6 +46,9 @@ deviceETH2UART::deviceETH2UART(uint32_t nodeID) : m_iInFID = -1; m_iOutFID = -1; m_bRegistered = false; +#ifdef SERIAL_PING_PONG + m_pLastParser = NULL; +#endif m_bLastPingOk = true; m_nNextPingMs = 0; m_nNextConnectMs = 0; @@ -852,100 +855,136 @@ bool deviceETH2UART::closePort() return true; } #ifdef SERIAL_PING_PONG +bool deviceETH2UART::isRecipient(protocolParser * p) +{ + if (p == NULL) + { + return false; + }; + m_mQueueProtectorMutex.lock(); + bool tVal = std::find(m_vParsers.begin(), m_vParsers.end(), p) != m_vParsers.end(); + m_mQueueProtectorMutex.unlock(); + return tVal; +} + void deviceETH2UART::m_fThreadProc(void) { - int index; int RetVal; m_bThreadStatus = TH_WORKING; #ifdef DEBUG LOG("Sender Thread Started!"); #endif - pthread_setname_np( pthread_self(), "E2S IO" ); + pthread_setname_np( pthread_self(), "E2S IO" ); + SIO_TZ * tIdle = (SIO_TZ *) malloc(sizeof(SIO_TZ)); // receive buffer for data between requests while (m_bThreadStatus < TH_ENDED){ + SIO_TZ * tIOData = NULL; m_mBufferProtectorMutex.lock(); if (! m_qIO.empty()){ - SIO_TZ * tIOData; tIOData = m_qIO.front(); - m_mBufferProtectorMutex.unlock(); - if( tIOData ){ - // Remove from Queue - m_mBufferProtectorMutex.lock(); - m_qIO.pop(); - m_mBufferProtectorMutex.unlock(); - - index = 0; - //Sending Data to line - while ((index < tIOData->out_size) && (m_bThreadStatus < TH_ENDED)){ + m_qIO.pop(); + }; + m_mBufferProtectorMutex.unlock(); + + if (tIOData == NULL){ + // No request: still read the socket. A reply that came after the reply window (Internet + // delay) was left in the socket and read with the next request, as that request's reply. + // Now it goes to the parser of the request it answers, as soon as it arrives. + if (tIdle != NULL){ + tIdle->inp_size = 0; + RetVal = recptBlock(tIdle->inp_buffer, &tIdle->inp_size); + if (RetVal == -1){ + setConnectionStatus(CS_DISCONNECTED); + m_bThreadStatus = TH_ENDED; + LOG_ERROR("Connection Droped on Reception Data"); + } else if (tIdle->inp_size > 0){ + if (isRecipient(m_pLastParser)){ + m_pLastParser->parse(tIdle->inp_buffer, tIdle->inp_size); + } #ifdef DEBUG_COMMUNICATION - char TT[128]; - sprintf(TT, "Send \t%d bytes for \t%p", ( tIOData->out_size - index ), tIOData->boundParser ); - LOG(TT); + else { + LOG_ERROR("Data without request dropped"); + } #endif - RetVal = sendBlock(tIOData->out_buffer + index, tIOData->out_size - index); - if (RetVal > 0){ - index += RetVal; - } else { - // if 0 or -(error) - if (RetVal == 0) { - usleep(100); - } else { - setConnectionStatus(CS_DISCONNECTED); - // m_nOutputBufferSize = 0; - tIOData->out_size = 0; - m_bThreadStatus = TH_ENDED; - LOG_ERROR("Connection Droped on Sending Data"); - }; - }; + continue; // more may follow + }; + }; + SLEEP(5); + continue; + }; + + //Sending Data to line + int index = 0; + while ((index < tIOData->out_size) && (m_bThreadStatus < TH_ENDED)){ + #ifdef DEBUG_COMMUNICATION + char TT[128]; + sprintf(TT, "Send \t%d bytes for \t%p", ( tIOData->out_size - index ), tIOData->boundParser ); + LOG(TT); + #endif + RetVal = sendBlock(tIOData->out_buffer + index, tIOData->out_size - index); + if (RetVal > 0){ + index += RetVal; + } else { + // if 0 or -(error) + if (RetVal == 0) { + usleep(100); + } else { + setConnectionStatus(CS_DISCONNECTED); + tIOData->out_size = 0; + m_bThreadStatus = TH_ENDED; + LOG_ERROR("Connection Droped on Sending Data"); }; - //Try to receive data from line - tIOData->out_size = 0; - bool tDataOnline = false; - uint8_t tTOCounter = 20; - do { - RetVal = recptBlock( tIOData->inp_buffer, &tIOData->inp_size ); - if( RetVal == -1 ){ - setConnectionStatus(CS_DISCONNECTED); - tIOData->inp_size = 0; - m_bThreadStatus = TH_ENDED; - LOG_ERROR("Connection Droped on Reception Data"); - } else if( tIOData->inp_size ){ - tDataOnline = true; - if( tIOData->boundParser == NULL ){ - - } else { - #ifdef DEBUG_COMMUNICATION - char tB[128]; - sprintf( tB, "Read \t%d bytes for \t%p", tIOData->inp_size, tIOData->boundParser ); - LOG( tB ); - #endif - tDataOnline = tIOData->boundParser->parse( tIOData->inp_buffer, tIOData->inp_size ); - }; - } else { - if (tTOCounter) { - tTOCounter--; - SLEEP(5); + }; + }; + // A parser detached since the request was queued (sensor disconnected / deleted) gets nothing + protocolParser * tParser = isRecipient(tIOData->boundParser) ? tIOData->boundParser : NULL; + if (tParser != NULL){ + m_pLastParser = tParser; + }; + //Try to receive the reply: up to E2UART_REPLY_TIMEOUT_MS for its start, then while it keeps + //coming (E2UART_REPLY_GAP_MS between chunks); done when the parser has a complete packet and + //no more data is waiting + tIOData->out_size = 0; + bool tDataOnline = false; + uint64_t tDeadline = e2uNowMs() + E2UART_REPLY_TIMEOUT_MS; + do { + RetVal = recptBlock( tIOData->inp_buffer, &tIOData->inp_size ); + if( RetVal == -1 ){ + setConnectionStatus(CS_DISCONNECTED); + tIOData->inp_size = 0; + m_bThreadStatus = TH_ENDED; + LOG_ERROR("Connection Droped on Reception Data"); + } else if( tIOData->inp_size ){ + uint64_t tGapEnd = e2uNowMs() + E2UART_REPLY_GAP_MS; + if (tGapEnd > tDeadline){ + tDeadline = tGapEnd; + }; + if( tParser == NULL ){ + tDataOnline = true; + } else { #ifdef DEBUG_COMMUNICATION - } else { - LOG_ERROR("PACK TIMEOUT!"); + char tB[128]; + sprintf( tB, "Read \t%d bytes for \t%p", tIOData->inp_size, tParser ); + LOG( tB ); #endif - }; - }; - - } while ((m_bThreadStatus < TH_ENDED) && tTOCounter && ! (tDataOnline && RetVal == 0)); // Pack1 conditions -> White Space after Data. - // Free Package - #ifdef DEBUG_COMMUNICATION - LOG("Finishing Read Package"); - #endif - free(tIOData); + tDataOnline = tParser->parse( tIOData->inp_buffer, tIOData->inp_size ) || tDataOnline; + }; } else { - // Mutex already unlocked! + if (tDataOnline){ + break; // complete packet and the socket is empty (as before: white space after data) + }; SLEEP(5); }; - } else { - m_mBufferProtectorMutex.unlock(); - SLEEP(5); + } while ((m_bThreadStatus < TH_ENDED) && (e2uNowMs() < tDeadline)); + #ifdef DEBUG_COMMUNICATION + if (! tDataOnline){ + LOG_ERROR("PACK TIMEOUT!"); }; + LOG("Finishing Read Package"); + #endif + // Free Package + free(tIOData); }; + free(tIdle); } bool deviceETH2UART::sendData(uint8_t * Data, int DataSize, protocolParser *src ){ diff --git a/lib/App/common/deviceNodeEthernet2Serial.h b/lib/App/common/deviceNodeEthernet2Serial.h index eb341037..8ee3ac84 100644 --- a/lib/App/common/deviceNodeEthernet2Serial.h +++ b/lib/App/common/deviceNodeEthernet2Serial.h @@ -21,6 +21,15 @@ #ifndef E2UART_PING_INTERVAL_MS #define E2UART_PING_INTERVAL_MS 10000 // UDP modes: ping at most this often (validate() runs every second) #endif +// Reply of the device to one request (SERIAL_PING_PONG I/O thread). The 20 x 5 ms (~100 ms) of the +// serial line were too short over the Internet: a later reply was read only with the NEXT request +// and given to that request's parser (one reply behind -> retries -> "Communication Wait State TO!") +#ifndef E2UART_REPLY_TIMEOUT_MS + #define E2UART_REPLY_TIMEOUT_MS 800 // first byte of the reply (below the sensor's 1 s per attempt) +#endif +#ifndef E2UART_REPLY_GAP_MS + #define E2UART_REPLY_GAP_MS 150 // a started but incomplete reply: wait this long for the rest +#endif // Reconnect back-off after a failed connect: 2 s, 4 s, ... up to 30 s #define E2UART_RECONNECT_MIN_MS 2000 #define E2UART_RECONNECT_MAX_MS 30000 @@ -70,6 +79,8 @@ #ifdef SERIAL_PING_PONG virtual void m_fThreadProc(void); Q_SIO_TZ m_qIO; + protocolParser * m_pLastParser; // parser of the last request: gets data that arrives between requests + bool isRecipient(protocolParser * p); // still bound to this transport (not deleted / detached) #else U8 m_pInputBuffer[MAXIMAL_SOCKET_IO_BUFFER_LENGTH]; U8 m_pOutputBuffer[MAXIMAL_SOCKET_IO_BUFFER_LENGTH];