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];