You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

126 lines
4.9 KiB

#ifndef __DEVICE_NODE_ETHERNET2SERIAL_HEADER__
#define __DEVICE_NODE_ETHERNET2SERIAL_HEADER__
#include "../../data/transport/transportSocket.h"
//#include "../components/base/basicTransport.h"
#include "deviceNode.h"
#include "hardwareResource.h"
#include <mutex>
// Timeouts for a gateway reached over the Internet (the 1 s connect timeout was a LAN value)
#ifndef E2UART_CONNECT_TIMEOUT_MS
#define E2UART_CONNECT_TIMEOUT_MS 5000 // TCP connect
#endif
#ifndef E2UART_PING_TIMEOUT_MS
#define E2UART_PING_TIMEOUT_MS 2000 // wait for one ICMP echo reply
#endif
#ifndef E2UART_PING_ATTEMPTS
#define E2UART_PING_ATTEMPTS 3 // echo requests before the device counts as unreachable
#endif
#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 2500 // first byte of the reply (below the sensor's LGS_NETWORK_TO_COUNTER, 3 s)
#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
// TCP keepalive: a dead link is detected in about IDLE + INTVL * CNT seconds
#define E2UART_KEEPALIVE_IDLE_S 20
#define E2UART_KEEPALIVE_INTVL_S 5
#define E2UART_KEEPALIVE_CNT 4
// Sent data not acknowledged by the gateway for this long: the kernel drops the connection (a dead
// mobile / NAT path otherwise looks alive for many minutes - keepalive only probes idle connections)
#ifndef E2UART_TCP_USER_TIMEOUT_MS
#define E2UART_TCP_USER_TIMEOUT_MS 10000
#endif
// 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)
// 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
#define E2UART_NO_REPLY_RECONNECT 5
#endif
#ifdef SERIAL_PING_PONG
class deviceETH2UART : public deviceNode, public hardwareResource, public basicTransport, public basicThread
#else
class deviceETH2UART : public deviceNode, public hardwareResource, public basicTransport, public basicDualThread
#endif
{
private:
// basicLogCollection * m_pLog;
DBC_UART_DATA m_sUartData;
int m_iDeviceFD;
int m_iMasterFD;
#ifdef __linux__
int m_iInFID;
int m_iOutFID;
#else
DCB PortDCB;
HANDLE m_ioFID;
#endif
// create() / validate() / reconnect() / closePort() run on the node factory thread AND on the
// hardware manager thread (the class is a deviceNode and a hardwareResource): one at a time
std::recursive_mutex m_mConnLock;
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;
uint64_t m_nNextPingMs;
uint64_t m_nNextConnectMs;
uint32_t m_nConnectBackoffMs;
bool pingDevice();
bool pingOnce(int sockfd, uint16_t id, uint16_t seq, char * reason, size_t reasonSize);
bool linkAlive(void);
bool connectSOL(void); // create_SOL() with the reconnect back-off
bool create_SOL();
bool closePort();
int sendBlock(unsigned char * Data, int DataSize);
int recptBlock(unsigned char * Buffer, int * BufferLength);
protected:
#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)
void tcpInfoText(char * buf, size_t size); // unacked data / retransmits / last ACK of the connection
#else
U8 m_pInputBuffer[MAXIMAL_SOCKET_IO_BUFFER_LENGTH];
U8 m_pOutputBuffer[MAXIMAL_SOCKET_IO_BUFFER_LENGTH];
int m_nInputBufferSize;
int m_nOutputBufferSize;
virtual void m_fThreadAProc(void);
virtual void m_fThreadBProc(void);
#endif
public:
explicit deviceETH2UART(uint32_t nodeID);
~deviceETH2UART();
virtual bool create(void);
virtual bool validate(void);
#ifdef SERIAL_PING_PONG
virtual bool sendData(uint8_t * Data, int DataSize, protocolParser *src = NULL );
#else
virtual bool sendData(uint8_t * Data, int DataSize);
#endif
virtual bool reconnect(void);
virtual bool isNetworkLink(void)
{
return true;
}
};
#endif