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.

822 lines
17 KiB

la_mojo: fix memory leaks and heap corruption Continuous leaks: - protocolParser: on package-pool overflow the oldest package was removed but never freed while the newest (still pooled) one was freed. Every packet a handler does not consume (e.g. other sensors on a shared bus) leaked once the pool was full. Free the removed package, free pooled packages in the destructor and getPackage(), use memmove for the overlapping buffer shifts. - SQLite_Connector::Query: loop only while sqlite3_step() returns SQLITE_ROW. Looping until SQLITE_DONE spun forever allocating rows on SQLITE_BUSY/ERROR. Set a busy timeout, close handles after a failed open, fix escapeBinary() writing into uninitialised buffers. - basicThread/basicDualThread: threads that ended by themselves were never joined, leaking their stacks on every stream drop/restart. Always join once (detach when called from the thread itself); check pthread_create result correctly. - curlRequest: free the header list; initialise m_pPostData and the content type; curlRequestsManager deletes requests it cannot queue. - kernelModuleModemManager: free the phonebook ID list on every reload. - deviceNodeFactory: do not append the same pending node every cycle. - streamHandler: free frames left in the encoding queue on destruction. Heap corruption: - kernelModuleFactory: module that failed to start was deleted twice. - mojoRemoteClient: basicRing::push() returns an ID (may be 0), not a bool; compare with -1. Fix packetExtractor and destructor leaks. - utilizeBasicEvent: payloads are malloc'ed, release with free(). - videoAVLIBSource: avoid sws context double free, NULL check order. - trim(): memmove for overlapping copy. Smaller leaks: dropSession duplicate query, LRF upload data, report backupper / fly hunter settings, Kafka consumer handle, BVMS uniq ID. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16
6 days ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
la_mojo: fix memory leaks and heap corruption Continuous leaks: - protocolParser: on package-pool overflow the oldest package was removed but never freed while the newest (still pooled) one was freed. Every packet a handler does not consume (e.g. other sensors on a shared bus) leaked once the pool was full. Free the removed package, free pooled packages in the destructor and getPackage(), use memmove for the overlapping buffer shifts. - SQLite_Connector::Query: loop only while sqlite3_step() returns SQLITE_ROW. Looping until SQLITE_DONE spun forever allocating rows on SQLITE_BUSY/ERROR. Set a busy timeout, close handles after a failed open, fix escapeBinary() writing into uninitialised buffers. - basicThread/basicDualThread: threads that ended by themselves were never joined, leaking their stacks on every stream drop/restart. Always join once (detach when called from the thread itself); check pthread_create result correctly. - curlRequest: free the header list; initialise m_pPostData and the content type; curlRequestsManager deletes requests it cannot queue. - kernelModuleModemManager: free the phonebook ID list on every reload. - deviceNodeFactory: do not append the same pending node every cycle. - streamHandler: free frames left in the encoding queue on destruction. Heap corruption: - kernelModuleFactory: module that failed to start was deleted twice. - mojoRemoteClient: basicRing::push() returns an ID (may be 0), not a bool; compare with -1. Fix packetExtractor and destructor leaks. - utilizeBasicEvent: payloads are malloc'ed, release with free(). - videoAVLIBSource: avoid sws context double free, NULL check order. - trim(): memmove for overlapping copy. Smaller leaks: dropSession duplicate query, LRF upload data, report backupper / fly hunter settings, Kafka consumer handle, BVMS uniq ID. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16
6 days ago
​
​
​
​
​
​
la_mojo: fix memory leaks and heap corruption Continuous leaks: - protocolParser: on package-pool overflow the oldest package was removed but never freed while the newest (still pooled) one was freed. Every packet a handler does not consume (e.g. other sensors on a shared bus) leaked once the pool was full. Free the removed package, free pooled packages in the destructor and getPackage(), use memmove for the overlapping buffer shifts. - SQLite_Connector::Query: loop only while sqlite3_step() returns SQLITE_ROW. Looping until SQLITE_DONE spun forever allocating rows on SQLITE_BUSY/ERROR. Set a busy timeout, close handles after a failed open, fix escapeBinary() writing into uninitialised buffers. - basicThread/basicDualThread: threads that ended by themselves were never joined, leaking their stacks on every stream drop/restart. Always join once (detach when called from the thread itself); check pthread_create result correctly. - curlRequest: free the header list; initialise m_pPostData and the content type; curlRequestsManager deletes requests it cannot queue. - kernelModuleModemManager: free the phonebook ID list on every reload. - deviceNodeFactory: do not append the same pending node every cycle. - streamHandler: free frames left in the encoding queue on destruction. Heap corruption: - kernelModuleFactory: module that failed to start was deleted twice. - mojoRemoteClient: basicRing::push() returns an ID (may be 0), not a bool; compare with -1. Fix packetExtractor and destructor leaks. - utilizeBasicEvent: payloads are malloc'ed, release with free(). - videoAVLIBSource: avoid sws context double free, NULL check order. - trim(): memmove for overlapping copy. Smaller leaks: dropSession duplicate query, LRF upload data, report backupper / fly hunter settings, Kafka consumer handle, BVMS uniq ID. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16
6 days ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
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
12 hours ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
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
12 hours ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
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
12 hours ago
​
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
12 hours ago
​
​
​
​
​
​
​
​
​
​
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
12 hours ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #include "deviceNodeFactory.h"
  2. #include <algorithm>
  3. #ifdef USE_MYSQL
  4. #include "MySQL_dataBinder.h"
  5. #else
  6. #include "SQLite_dataBinder.h"
  7. #endif
  8. #include "deviceNode2D.h"
  9. #include "deviceNode3D.h"
  10. #include "deviceNodeEthernet2Serial.h"
  11. #include "deviceNodePresetCamControl.h"
  12. #include "deviceNodePTZCamControl.h"
  13. #include "deviceNodeStaticCamControl.h"
  14. #ifdef MEM_DEBUG
  15. #include "../alg/generic/heapDebug.h"
  16. #endif
  17. #define LOG(X) m_pLog->Log("NFAC", this, X)
  18. #define LOG_ERROR(X) m_pLog->Log("NFAC", this, _LOG_ERROR, X)
  19. deviceNodeFactory * deviceNodeFactory::_instance = NULL;
  20. /**
  21. * Returns the single instance of the object.
  22. */
  23. deviceNodeFactory * deviceNodeFactory::getInstance()
  24. {
  25. if (NULL == _instance)
  26. {
  27. _instance = new deviceNodeFactory();
  28. };
  29. return _instance;
  30. }
  31. deviceNodeFactory::deviceNodeFactory() :
  32. basicThread()
  33. {
  34. m_pLog = basicLogCollection::getInstance();
  35. #ifdef USE_MYSQL
  36. m_pDBC = mysqlDBC::getInstance();
  37. #else
  38. m_pDBC = sqliteDBC::getInstance();
  39. #endif
  40. m_nPendingInitCounter = 0;
  41. init();
  42. }
  43. deviceNodeFactory::~deviceNodeFactory()
  44. {
  45. waitThreadEnd(true);
  46. unloadNodes();
  47. _instance = NULL;
  48. }
  49. bool deviceNodeFactory::unloadNodes(void)
  50. {
  51. if (m_pDBC != NULL)
  52. {
  53. if (m_vDeviceNodes.size() > 0)
  54. {
  55. for (vectorDeviceNodes::iterator index = m_vDeviceNodes.begin(); index < m_vDeviceNodes.end(); index++)
  56. {
  57. LOG(" Unload Node... BEGIN");
  58. switch ((*index)->getNodeType())
  59. {
  60. case ARGUS_ANALOG_STATIC_CAMERA:
  61. LOG(" Static Analog Cam");
  62. delete((deviceNodeStaticCamControl *) (*index));
  63. break;
  64. case ARGUS_ANALOG_PTZ_CAMERA:
  65. LOG(" PTZ Analog Cam");
  66. delete((deviceNodePTZCamControl *) (*index));
  67. break;
  68. case ARGUS_IP_PTZ_CAMERA:
  69. {
  70. LOG(" PTZ IP Cam");
  71. delete((deviceNodePTZCamControl *) (*index));
  72. break;
  73. };
  74. case ARGUS_IP_STATIC_CAMERA:
  75. {
  76. LOG(" Static IP Cam");
  77. delete((deviceNodeStaticCamControl *) (*index));
  78. break;
  79. };
  80. case ARGUS_IP_PRESET_CAMERA:
  81. {
  82. LOG(" Preset IP Cam");
  83. delete((deviceNodePresetCamControl *) (*index));
  84. break;
  85. };
  86. case ARGUS_ANALOG_PRESET_CAMERA:
  87. {
  88. LOG(" Preset Analog Cam");
  89. // delete( (deviceNodeStaticCamControl*) (*index) );
  90. break;
  91. };
  92. case ARGUS_IP_UART_GATEWAY:
  93. {
  94. LOG(" UART Gateway");
  95. delete((deviceETH2UART *) (*index));
  96. break;
  97. };
  98. case ARGUS_ALS_SENSOR:
  99. case ARGUS_PLS_200_SENSOR:
  100. case ARGUS_PLS_360_SENSOR:
  101. case ARGUS_LFS_SENSOR:
  102. {
  103. LOG(" 2D LRF");
  104. delete((device2DSensor *) (*index));
  105. LOG(" 2D LRF done");
  106. break;
  107. };
  108. case ARGUS_3D_SENSOR:
  109. {
  110. LOG(" 3D LRF");
  111. delete((device3DSensor *) (*index));
  112. break;
  113. };
  114. default: break;
  115. };
  116. LOG(" Unload Node... DONE!");
  117. };
  118. m_vDeviceNodes.clear();
  119. m_mDeviceNodesIDS.clear();
  120. };
  121. };
  122. return true;
  123. }
  124. /*
  125. bool deviceNodeFactory::loadNodes( uint32_t SiteID ){
  126. LOG("Node Loader");
  127. if(m_pDBC == NULL) return false;
  128. m_nSiteID = SiteID;
  129. DBC_INT_LIST *tList = m_pDBC->listNodes( SiteID );
  130. if( tList != NULL ) {
  131. for( DBC_INT_LIST::iterator idx = tList->begin(); idx < tList->end(); idx++ ){
  132. uint32_t id = *idx;
  133. char msg[64];
  134. sprintf( msg, "Creating Node[%d]", id );
  135. createNode( id );
  136. };
  137. tList->clear();
  138. delete( tList );
  139. return true;
  140. };
  141. return false;
  142. };
  143. */
  144. bool deviceNodeFactory::loadNodes()
  145. {
  146. if (m_pDBC == NULL)
  147. {
  148. return false;
  149. }
  150. DBC_INT_LIST * tList = m_pDBC->listNodes(false);
  151. m_nPendingInitCounter = 0;
  152. if (tList != NULL)
  153. {
  154. for (DBC_INT_LIST::iterator idx = tList->begin(); idx < tList->end(); idx++)
  155. {
  156. createNode(*idx);
  157. }
  158. tList->clear();
  159. delete(tList);
  160. }
  161. #ifdef DEBUG
  162. else
  163. {
  164. LOG("No Nodes Defined");
  165. }
  166. #endif
  167. return true;
  168. }
  169. bool deviceNodeFactory::reloadNodes()
  170. {
  171. if (! unloadNodes())
  172. {
  173. return false;
  174. }
  175. return loadNodes();
  176. }
  177. bool deviceNodeFactory::reloadNode(uint32_t NodeID)
  178. {
  179. deleteNode(NodeID);
  180. deviceNode * tNode = createNode(NodeID);
  181. if (tNode)
  182. {
  183. return true;
  184. };
  185. return false;
  186. }
  187. deviceNode * deviceNodeFactory::createNode(uint32_t id)
  188. {
  189. deviceNode * tVal = NULL;
  190. if (m_pDBC != NULL)
  191. {
  192. DBC_NODE_DATA * tData = NULL;
  193. tData = m_pDBC->getNodeData(id);
  194. if (tData != NULL)
  195. {
  196. #ifdef DEBUG
  197. char tB[256];
  198. sprintf(tB, "Node id: %d AUX id: %d", id, tData->aux_id);
  199. LOG(tB);
  200. sprintf(tB, "Node type: %d", tData->type);
  201. LOG(tB);
  202. sprintf(tB, "Node Name: %s", tData->name);
  203. LOG(tB);
  204. #endif
  205. switch (tData->type)
  206. {
  207. case ARGUS_ANALOG_STATIC_CAMERA:
  208. {
  209. #ifdef DEBUG
  210. LOG("Creating Analog Static Camera");
  211. #endif
  212. deviceNodeStaticCamControl * tCamera;
  213. tCamera = new deviceNodeStaticCamControl(id);
  214. if (tCamera)
  215. {
  216. LOG("Starting Service");
  217. tCamera->startService();
  218. tVal = (deviceNode *) tCamera;
  219. LOG("Success");
  220. };
  221. break;
  222. };
  223. case ARGUS_ANALOG_PTZ_CAMERA:
  224. {
  225. #ifdef DEBUG
  226. LOG("Creating Analog PTZ Camera");
  227. #endif
  228. deviceNodePTZCamControl * tCamera;
  229. tCamera = new deviceNodePTZCamControl(id);
  230. if (tCamera)
  231. {
  232. LOG("Starting Service");
  233. tCamera->startService();
  234. tVal = (deviceNode *) tCamera;
  235. LOG("Success");
  236. };
  237. break;
  238. };
  239. case ARGUS_IP_STATIC_CAMERA:
  240. {
  241. #ifdef DEBUG
  242. LOG("Creating IP Static Camera");
  243. #endif
  244. deviceNodeStaticCamControl * tCamera;
  245. tCamera = new deviceNodeStaticCamControl(id);
  246. if (tCamera)
  247. {
  248. #ifdef DEBUG
  249. LOG("Starting Service");
  250. #endif
  251. if (tCamera->startService())
  252. {
  253. tVal = (deviceNode *) tCamera;
  254. #ifdef DEBUG
  255. LOG("Success");
  256. #endif
  257. }
  258. else
  259. {
  260. #ifdef DEBUG
  261. LOG_ERROR("Error Sterting Service");
  262. #endif
  263. delete(tCamera);
  264. };
  265. };
  266. break;
  267. };
  268. case ARGUS_IP_PTZ_CAMERA:
  269. {
  270. #ifdef DEBUG
  271. LOG("Creating IP PTZ Camera");
  272. #endif
  273. deviceNodePTZCamControl * tCamera;
  274. tCamera = new deviceNodePTZCamControl(id);
  275. if (tCamera)
  276. {
  277. #ifdef DEBUG
  278. LOG("Starting Service");
  279. #endif
  280. if (tCamera->startService())
  281. {
  282. tVal = (deviceNode *) tCamera;
  283. #ifdef DEBUG
  284. LOG("Success");
  285. #endif
  286. }
  287. else
  288. {
  289. #ifdef DEBUG
  290. LOG_ERROR("Error Starting Service!");
  291. #endif
  292. delete(tCamera);
  293. };
  294. };
  295. break;
  296. };
  297. case ARGUS_IP_PRESET_CAMERA:
  298. {
  299. #ifdef DEBUG
  300. LOG("Creating IP Preset Camera");
  301. #endif
  302. deviceNodePresetCamControl * tCamera;
  303. tCamera = new deviceNodePresetCamControl(id);
  304. if (tCamera)
  305. {
  306. #ifdef DEBUG
  307. LOG("Starting Service");
  308. #endif
  309. if (tCamera->startService())
  310. {
  311. tVal = (deviceNode *) tCamera;
  312. #ifdef DEBUG
  313. LOG("Success");
  314. #endif
  315. }
  316. else
  317. {
  318. #ifdef DEBUG
  319. LOG_ERROR("Error Starting Service!");
  320. #endif
  321. delete(tCamera);
  322. };
  323. };
  324. break;
  325. };
  326. case ARGUS_IP_UART_GATEWAY:
  327. {
  328. #ifdef DEBUG
  329. LOG("Creating Ethernet to UART Gateway");
  330. #endif
  331. deviceETH2UART * tGW;
  332. tGW = new deviceETH2UART(id);
  333. if (tGW)
  334. {
  335. if (tGW->create())
  336. {
  337. tVal = (deviceNode *) tGW;
  338. }
  339. else
  340. {
  341. delete(tGW);
  342. };
  343. };
  344. break;
  345. };
  346. case ARGUS_IP_VIDEO_SERVER:
  347. #ifdef DEBUG
  348. LOG("Creating Network Video Encoder");
  349. #endif
  350. break;
  351. case ARGUS_LFS_SENSOR:
  352. case ARGUS_ALS_SENSOR:
  353. case ARGUS_PLS_200_SENSOR:
  354. case ARGUS_PLS_360_SENSOR:
  355. {
  356. #ifdef DEBUG
  357. LOG("Creating 2D Sensor");
  358. #endif
  359. device2DSensor * tSensor;
  360. tSensor = new device2DSensor(id);
  361. if (tSensor)
  362. {
  363. #ifdef DEBUG
  364. LOG("Starting Service");
  365. #endif
  366. if (tSensor->startService())
  367. {
  368. #ifdef DEBUG
  369. LOG("Success");
  370. #endif
  371. tVal = (deviceNode *) tSensor;
  372. }
  373. else
  374. {
  375. delete(tSensor);
  376. };
  377. };
  378. break;
  379. };
  380. case ARGUS_3D_SENSOR:
  381. {
  382. #ifdef DEBUG
  383. LOG("Creating 3D Sensor");
  384. #endif
  385. device3DSensor * t3DRadar;
  386. t3DRadar = new device3DSensor(id);
  387. if (t3DRadar)
  388. {
  389. LOG("Success");
  390. t3DRadar->startService();
  391. tVal = (deviceNode *) t3DRadar;
  392. };
  393. break;
  394. };
  395. case ARGUS_JAMER:
  396. #ifdef DEBUG
  397. LOG("Creating RF Jamer");
  398. #endif
  399. break;
  400. case ARGUS_NET_PLS_200:
  401. #ifdef DEBUG
  402. LOG("Creating Network-PLS200 Sensor");
  403. #endif
  404. break;
  405. case ARGUS_DRONE_CONTROL:
  406. #ifdef DEBUG
  407. LOG("Creating Drone Controll");
  408. #endif
  409. break;
  410. };
  411. free(tData);
  412. };
  413. };
  414. if (tVal != NULL)
  415. {
  416. #ifdef DEBUG
  417. char tB[64];
  418. sprintf(tB, "Node PTR: %p", (void *)tVal);
  419. LOG(tB);
  420. #endif
  421. m_vDeviceNodes.push_back(tVal);
  422. m_mDeviceNodesIDS[id] = tVal;
  423. // Node is alive now - it is no longer pending
  424. m_nPendingNodes.erase(std::remove(m_nPendingNodes.begin(), m_nPendingNodes.end(), (int) id), m_nPendingNodes.end());
  425. }
  426. else
  427. {
  428. #ifdef DEBUG_EXTRA
  429. LOG("Node Creation Failure -> Push to Pending Mode!");
  430. #endif
  431. // Add only once: this runs on every retry and the list was never trimmed
  432. if (std::find(m_nPendingNodes.begin(), m_nPendingNodes.end(), (int) id) == m_nPendingNodes.end())
  433. {
  434. m_nPendingNodes.push_back(id);
  435. }
  436. m_nPendingInitCounter = FACTORY_NODE_INIT_PENDING_COUNT;
  437. };
  438. return tVal;
  439. }
  440. void deviceNodeFactory::deleteNode(deviceNode * ptr)
  441. {
  442. if (ptr == NULL)
  443. {
  444. return;
  445. }
  446. // if some troubles move this to an end of the function!
  447. for (vectorDeviceNodes::iterator index = m_vDeviceNodes.begin(); index != m_vDeviceNodes.end(); index++)
  448. {
  449. if ((*index) == ptr)
  450. {
  451. m_mDeviceNodesIDS.erase(ptr->getNodeID());
  452. m_vDeviceNodes.erase(index);
  453. break;
  454. };
  455. };
  456. switch (ptr->getNodeType())
  457. {
  458. case ARGUS_ANALOG_STATIC_CAMERA:
  459. #ifdef DEBUG
  460. LOG("Destroying Analog Static Camera");
  461. #endif
  462. break;
  463. case ARGUS_ANALOG_PTZ_CAMERA:
  464. #ifdef DEBUG
  465. LOG("Destroying Analog PTZ Camera");
  466. #endif
  467. break;
  468. case ARGUS_IP_STATIC_CAMERA:
  469. #ifdef DEBUG
  470. LOG("Destroying Network Static Camera");
  471. #endif
  472. // SLEEP(2000);
  473. LOG("Actual Destroying Network Static Camera");
  474. delete((deviceNodePTZCamControl *) ptr);
  475. break;
  476. case ARGUS_IP_PTZ_CAMERA:
  477. #ifdef DEBUG
  478. LOG("Destroying Network PTZ Camera");
  479. #endif
  480. // SLEEP(2000);
  481. delete((deviceNodePTZCamControl *) ptr);
  482. #ifdef DEBUG
  483. LOG("Done Successfuly");
  484. #endif
  485. break;
  486. case ARGUS_IP_UART_GATEWAY:
  487. #ifdef DEBUG
  488. LOG("Destroying Ethernet to UART Gateway");
  489. #endif
  490. // Was not deleted: the object kept running (still validated by the hardware manager, still
  491. // connected to the gateway) next to the new node created for the same id
  492. delete((deviceETH2UART *) ptr);
  493. break;
  494. case ARGUS_IP_VIDEO_SERVER:
  495. #ifdef DEBUG
  496. LOG("Destroying Network Video Encoder");
  497. #endif
  498. break;
  499. case ARGUS_LFS_SENSOR:
  500. #ifdef DEBUG
  501. LOG("Destroying LFS Sensor");
  502. #endif
  503. delete((device2DSensor *) ptr);
  504. break;
  505. case ARGUS_ALS_SENSOR:
  506. #ifdef DEBUG
  507. LOG("Destroying ALS Sensor");
  508. #endif
  509. delete((device2DSensor *) ptr);
  510. break;
  511. case ARGUS_PLS_200_SENSOR:
  512. #ifdef DEBUG
  513. LOG("Destroying PLS200 Sensor");
  514. #endif
  515. delete((device2DSensor *) ptr);
  516. break;
  517. case ARGUS_PLS_360_SENSOR:
  518. #ifdef DEBUG
  519. LOG("Destroying PLS360 Sensor");
  520. #endif
  521. delete((device2DSensor *) ptr);
  522. break;
  523. case ARGUS_3D_SENSOR:
  524. #ifdef DEBUG
  525. LOG("Destroying 3D Sensor");
  526. #endif
  527. delete((device3DSensor *) ptr);
  528. break;
  529. case ARGUS_JAMER:
  530. #ifdef DEBUG
  531. LOG("Destroying RF Jamer");
  532. #endif
  533. break;
  534. case ARGUS_NET_PLS_200:
  535. #ifdef DEBUG
  536. LOG("Destroying Network-PLS200 Sensor");
  537. #endif
  538. break;
  539. case ARGUS_DRONE_CONTROL:
  540. #ifdef DEBUG
  541. LOG("Destroying Drone Controll");
  542. #endif
  543. break;
  544. default: break;
  545. };
  546. }
  547. /*
  548. @id Database ID of Node to be deleted
  549. */
  550. deviceNode * deviceNodeFactory::getNode(uint32_t id)
  551. {
  552. mapDeviceNode2Id::iterator it;
  553. it = m_mDeviceNodesIDS.find(id);
  554. if (it != m_mDeviceNodesIDS.end())
  555. {
  556. return m_mDeviceNodesIDS[id];
  557. };
  558. return NULL;
  559. }
  560. /*
  561. @id Database ID of Node to be deleted
  562. */
  563. void deviceNodeFactory::deleteNode(uint32_t id)
  564. {
  565. deviceNode * tNode = getNode(id);
  566. deleteNode(tNode);
  567. }
  568. bool deviceNodeFactory::validate(void)
  569. {
  570. return (m_bThreadStatus < TH_ENDED);
  571. }
  572. void deviceNodeFactory::m_fThreadProc(void)
  573. {
  574. if (m_pDBC == NULL)
  575. {
  576. m_bThreadStatus = TH_ENDED;
  577. }
  578. else
  579. {
  580. pthread_setname_np( pthread_self(), "NodeFactory" );
  581. m_bThreadStatus = TH_WORKING;
  582. while (m_bThreadStatus < TH_ENDED)
  583. {
  584. DBC_INT_LIST * tList = NULL;
  585. #ifdef DEBUG
  586. #ifdef MEM_DEBUG
  587. size_t memStart, memEnd, memDiff;
  588. char MEM_MSG[256];
  589. memStart = getCurrentRSS();
  590. #endif
  591. #endif
  592. if (m_nPendingInitCounter)
  593. {
  594. m_nPendingInitCounter--;
  595. }
  596. if (m_vDeviceNodes.size())
  597. {
  598. DBC_INT_LIST tL;
  599. for (vectorDeviceNodes::iterator idx = m_vDeviceNodes.begin(); idx < m_vDeviceNodes.end(); idx++)
  600. {
  601. tL.push_back((*idx)->getNodeID());
  602. }
  603. tList = m_pDBC->listNewNodes(&tL);
  604. //ToDo: Must be Written ad method to Delete on the fly Disabled Nodes
  605. // To provide this this performance without restarting App
  606. // tL may be used to query Database if a Node is disabled
  607. tL.clear();
  608. }
  609. else
  610. {
  611. tList = m_pDBC->listNewNodes(NULL);
  612. };
  613. if (tList != NULL)
  614. {
  615. #ifdef DEBUG_EXTRA
  616. LOG("Nodes are Added!");
  617. #endif
  618. for (DBC_INT_LIST::iterator idx = tList->begin(); idx != tList->end(); idx++)
  619. {
  620. if (m_nPendingNodes.size())
  621. {
  622. bool mustAdd = true;
  623. for (DBC_INT_LIST::iterator dx = m_nPendingNodes.begin(); dx != m_nPendingNodes.end(); dx++)
  624. {
  625. if ((*dx == *idx) && (m_nPendingInitCounter))
  626. {
  627. mustAdd = false;
  628. }
  629. };
  630. if (mustAdd)
  631. {
  632. createNode(*idx);
  633. #ifdef DEBUG_EXTRA
  634. }
  635. else
  636. {
  637. LOG("Node is in PENDING mode!");
  638. #endif
  639. };
  640. }
  641. else
  642. {
  643. createNode(*idx);
  644. };
  645. };
  646. tList->clear();
  647. delete(tList);
  648. #ifdef DEBUG_EXTRA
  649. }
  650. else
  651. {
  652. LOG("No New Nodes");
  653. #endif
  654. };
  655. #ifdef MEM_DEBUG
  656. memEnd = getCurrentRSS();
  657. memDiff = memEnd - memStart;
  658. sprintf(MEM_MSG, "MEM START: %d MEM DIF1: %d", memStart, memDiff);
  659. LOG(MEM_MSG);
  660. #endif
  661. if (m_vDeviceNodes.size())
  662. {
  663. // Get Full List
  664. tList = m_pDBC->listNodes();
  665. if (tList != NULL)
  666. {
  667. // deleteNode() erases from m_vDeviceNodes: collect first, delete after the loop
  668. // (erasing inside the loop invalidated the iterator and skipped the next node)
  669. vectorDeviceNodes tToDelete;
  670. for (vectorDeviceNodes::iterator idx = m_vDeviceNodes.begin(); idx < m_vDeviceNodes.end(); idx++)
  671. {
  672. bool tRemoved = true;
  673. // Check if Removed By changes in DB
  674. for (DBC_INT_LIST::iterator idz = tList->begin(); idz < tList->end(); idz++)
  675. {
  676. if ((*idz) == (int)(*idx)->getNodeID())
  677. {
  678. tRemoved = false;
  679. }
  680. }
  681. // If Not - check validation
  682. if (! tRemoved)
  683. {
  684. #ifdef DEBUG_EXTRA
  685. LOG("Node Among Active Nodes.... make Validation");
  686. #endif
  687. tRemoved = ! (*idx)->validate();
  688. };
  689. // If Node is Invalid - Removing
  690. if (tRemoved)
  691. {
  692. #ifdef DEBUG_EXTRA
  693. LOG("Node not passed Validation!");
  694. #endif
  695. tToDelete.push_back(*idx);
  696. };
  697. };
  698. for (vectorDeviceNodes::iterator idx = tToDelete.begin(); idx != tToDelete.end(); idx++)
  699. {
  700. deleteNode(*idx);
  701. };
  702. tList->clear();
  703. delete(tList);
  704. }
  705. else
  706. {
  707. #ifdef DEUBG_EXTRA
  708. LOG("All Modules Must be Deleted!");
  709. #endif
  710. vectorDeviceNodes tToDelete(m_vDeviceNodes); // deleteNode() erases from m_vDeviceNodes
  711. for (vectorDeviceNodes::iterator idx = tToDelete.begin(); idx != tToDelete.end(); idx++)
  712. {
  713. deleteNode(*idx);
  714. }
  715. m_vDeviceNodes.clear();
  716. m_mDeviceNodesIDS.clear();
  717. };
  718. #ifdef DEBUG_EXTRA
  719. }
  720. else
  721. {
  722. LOG("No Nodes to Validate");
  723. #endif
  724. };
  725. #ifdef DEBUG
  726. #ifdef MEM_DEBUG
  727. memEnd = getCurrentRSS();
  728. memDiff = memEnd - memStart;
  729. sprintf(MEM_MSG, "MEM START: %d MEM DIF2: %d", memStart, memDiff);
  730. LOG(MEM_MSG);
  731. #endif
  732. #endif
  733. // Sleep for 1 second
  734. for (int i = (1000 / DEFAULT_THREAD_SLEEP_TIME); i && (m_bThreadStatus < TH_ENDED); i--)
  735. {
  736. SLEEP(DEFAULT_THREAD_SLEEP_TIME);
  737. }
  738. };
  739. };
  740. LOG("EXIT FROM CTRL THREAD");
  741. }
  742. uint32_t deviceNodeFactory::getNodesCount(ARGUS_NODE_TYPE type)
  743. {
  744. uint32_t RetVal = 0;
  745. if (m_vDeviceNodes.size())
  746. {
  747. for (vectorDeviceNodes::iterator index = m_vDeviceNodes.begin(); index < m_vDeviceNodes.end(); index++)
  748. {
  749. if ((*index)->getNodeType() == type)
  750. {
  751. RetVal++;
  752. }
  753. }
  754. }
  755. return RetVal;
  756. }
  757. uint32_t deviceNodeFactory::fetchNodes(ARGUS_NODE_TYPE type, deviceNode **& RA)
  758. {
  759. uint32_t RetVal = 0;
  760. if (RA == NULL)
  761. {
  762. uint32_t cnt = getNodesCount(type);
  763. if (cnt > 0)
  764. {
  765. RA = (deviceNode **) calloc(cnt, sizeof(deviceNode *));
  766. if (RA == NULL)
  767. {
  768. return 0;
  769. }
  770. // memset( RA, 0, cnt * sizeof(deviceNode *) );
  771. for (vectorDeviceNodes::iterator index = m_vDeviceNodes.begin(); index < m_vDeviceNodes.end(); index++)
  772. {
  773. if ((*index)->getNodeType() == type)
  774. {
  775. RA[RetVal] = (*index);
  776. RetVal++;
  777. };
  778. };
  779. };
  780. }
  781. else
  782. {
  783. if (m_vDeviceNodes.size())
  784. {
  785. for (vectorDeviceNodes::iterator index = m_vDeviceNodes.begin(); index < m_vDeviceNodes.end(); index++)
  786. {
  787. if ((*index)->getNodeType() == type)
  788. {
  789. RA[RetVal] = (*index);
  790. RetVal++;
  791. };
  792. };
  793. };
  794. };
  795. return RetVal;
  796. }
  797. #undef LOG
  798. #undef LOG_ERROR