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.

113 lines
2.8 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
Ethernet2UART gateway: fix the reconnect loop, leaked connections and ping check From the la_mojo log of a gateway reached over the Internet (2.54.237.28:8234): deviceETH2UART - In TCP mode, link health is the TCP connection itself (socket, I/O thread, SO_ERROR, TCP keepalive). ICMP isn't used there: gateways on mobile networks often don't answer ping, and the working connection was closed every second ("No Ping to device!"). - Ping (UDP modes) up to 3 echoes x 2000 ms, at most every 10 s. Only an echo reply from the gateway with our id and sequence counts. A signal (EINTR) no longer ends the wait. The old single recvfrom() with SO_RCVTIMEO returned EINTR at the first signal, even with SA_RESTART. - TCP connect timeout 1 s -> 5 s (E2UART_CONNECT_TIMEOUT_MS), and EINTR in select() continues waiting. - create_SOL() closes the previous socket and I/O thread first. It overwrote the socket (the connection leaked) and init() failed while the old thread ran, so each reconnect left an open TCP connection at the gateway and the node stayed invalid although "Connected". If init() fails, the new socket is closed. - create()/validate()/reconnect() are serialised: they run on the node factory thread and on the hardware manager thread. - Registered with the hardware manager once. Re-creation from the hardware manager locked its non-recursive list mutex again (deadlock). - Reconnect back-off 2 s -> 30 s. - recv() == 0 on TCP means the gateway closed the connection (a stale errno was checked before). - Destructor resigns from the hardware manager before closing. deviceNodeFactory - deleteNode() deletes the gateway object. It was only removed from the list and kept running next to the new node created for the same id. - Nodes are deleted after the validation loop (erasing inside it skipped the next node). deviceNode::validate - Compares the configuration only. "online" changes whenever another object of the same node id is created or deleted. hardwareManager::assignResource - Ignores a resource that is already assigned. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16
15 hours ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #include "deviceNode.h"
  2. #ifdef USE_MYSQL
  3. #include "MySQL_dataBinder.h"
  4. #else
  5. #include "SQLite_dataBinder.h"
  6. #endif
  7. #define LOG(X) m_pLog->Log("NODE", this, X)
  8. #ifdef DEBUG
  9. #define LOG_ERROR(X) \
  10. { \
  11. char _tBfrErrMSG2w34[256]; \
  12. sprintf(_tBfrErrMSG2w34, "In file %s[%d] in func:%s %s", __FILE__, __LINE__, __func__, X); \
  13. m_pLog->Log("NODE", this, _LOG_ERROR, _tBfrErrMSG2w34); \
  14. }
  15. #else
  16. #define LOG_ERROR(X) m_pLog->Log("NODE", this, _LOG_ERROR, X);
  17. #endif
  18. deviceNode::deviceNode(uint32_t id) :
  19. cmdNodeConveyor(),
  20. basicEngine()
  21. {
  22. m_pLog = basicLogCollection::getInstance();
  23. #ifdef USE_MYSQL
  24. m_pDBC = mysqlDBC::getInstance();
  25. #else
  26. m_pDBC = sqliteDBC::getInstance();
  27. #endif
  28. if (m_pDBC != NULL)
  29. {
  30. DBC_NODE_DATA * tData = m_pDBC->getNodeData(id);
  31. if (tData != NULL)
  32. {
  33. memcpy(&m_sNodeData, tData, sizeof(DBC_NODE_DATA));
  34. m_sNodeData.online = m_pDBC->setNodeOnline(m_sNodeData.node_id);
  35. free(tData);
  36. }
  37. else
  38. {
  39. memset(&m_sNodeData, 0, sizeof(DBC_NODE_DATA));
  40. m_sNodeData.type = ARGUS_UNKNOWN;
  41. m_sNodeData.node_id = id;
  42. };
  43. }
  44. else
  45. {
  46. memset(&m_sNodeData, 0, sizeof(DBC_NODE_DATA));
  47. m_sNodeData.type = ARGUS_UNKNOWN;
  48. m_sNodeData.node_id = id;
  49. };
  50. }
  51. deviceNode::deviceNode(DBC_NODE_DATA * Data) :
  52. cmdNodeConveyor(),
  53. basicEngine()
  54. {
  55. m_pLog = basicLogCollection::getInstance();
  56. #ifdef USE_MYSQL
  57. m_pDBC = mysqlDBC::getInstance();
  58. #else
  59. m_pDBC = sqliteDBC::getInstance();
  60. #endif
  61. if (Data)
  62. {
  63. memcpy(&m_sNodeData, Data, sizeof(DBC_NODE_DATA));
  64. if (m_pDBC)
  65. {
  66. m_sNodeData.online = m_pDBC->setNodeOnline(m_sNodeData.node_id);
  67. };
  68. }
  69. else
  70. {
  71. memset(&m_sNodeData, 0, sizeof(DBC_NODE_DATA));
  72. m_sNodeData.type = ARGUS_UNKNOWN;
  73. };
  74. }
  75. deviceNode::~deviceNode()
  76. {
  77. m_pDBC->setNodeOffline(m_sNodeData.node_id);
  78. }
  79. bool deviceNode::validate()
  80. {
  81. DBC_NODE_DATA * tDATA = m_pDBC->getNodeData(m_sNodeData.node_id);
  82. if (tDATA)
  83. {
  84. // Configuration only: "online" is written by every node object of this id (setNodeOnline /
  85. // setNodeOffline) - deleting a second object of the same node made this one "changed"
  86. bool tChanged = (m_sNodeData.aux_id != tDATA->aux_id) || (m_sNodeData.aux_parameter != tDATA->aux_parameter) || (m_sNodeData.type != tDATA->type) ||
  87. (m_sNodeData.enabled != tDATA->enabled) || (m_sNodeData.tss != tDATA->tss) || (m_sNodeData.tsm != tDATA->tsm) ||
  88. (strncmp(m_sNodeData.name, tDATA->name, sizeof(m_sNodeData.name)) != 0);
  89. if (tChanged)
  90. {
  91. free(tDATA);
  92. LOG("Node Settings Changed!");
  93. return false;
  94. };
  95. free(tDATA);
  96. }
  97. else
  98. {
  99. LOG("Node is Deleted!");
  100. return false;
  101. };
  102. return true;
  103. }
  104. #undef LOG
  105. #undef LOG_ERROR