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.

811 lines
16 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
23 hours 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
23 hours 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
23 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. break;
  491. case ARGUS_IP_VIDEO_SERVER:
  492. #ifdef DEBUG
  493. LOG("Destroying Network Video Encoder");
  494. #endif
  495. break;
  496. case ARGUS_LFS_SENSOR:
  497. #ifdef DEBUG
  498. LOG("Destroying LFS Sensor");
  499. #endif
  500. delete((device2DSensor *) ptr);
  501. break;
  502. case ARGUS_ALS_SENSOR:
  503. #ifdef DEBUG
  504. LOG("Destroying ALS Sensor");
  505. #endif
  506. delete((device2DSensor *) ptr);
  507. break;
  508. case ARGUS_PLS_200_SENSOR:
  509. #ifdef DEBUG
  510. LOG("Destroying PLS200 Sensor");
  511. #endif
  512. delete((device2DSensor *) ptr);
  513. break;
  514. case ARGUS_PLS_360_SENSOR:
  515. #ifdef DEBUG
  516. LOG("Destroying PLS360 Sensor");
  517. #endif
  518. delete((device2DSensor *) ptr);
  519. break;
  520. case ARGUS_3D_SENSOR:
  521. #ifdef DEBUG
  522. LOG("Destroying 3D Sensor");
  523. #endif
  524. delete((device3DSensor *) ptr);
  525. break;
  526. case ARGUS_JAMER:
  527. #ifdef DEBUG
  528. LOG("Destroying RF Jamer");
  529. #endif
  530. break;
  531. case ARGUS_NET_PLS_200:
  532. #ifdef DEBUG
  533. LOG("Destroying Network-PLS200 Sensor");
  534. #endif
  535. break;
  536. case ARGUS_DRONE_CONTROL:
  537. #ifdef DEBUG
  538. LOG("Destroying Drone Controll");
  539. #endif
  540. break;
  541. default: break;
  542. };
  543. }
  544. /*
  545. @id Database ID of Node to be deleted
  546. */
  547. deviceNode * deviceNodeFactory::getNode(uint32_t id)
  548. {
  549. mapDeviceNode2Id::iterator it;
  550. it = m_mDeviceNodesIDS.find(id);
  551. if (it != m_mDeviceNodesIDS.end())
  552. {
  553. return m_mDeviceNodesIDS[id];
  554. };
  555. return NULL;
  556. }
  557. /*
  558. @id Database ID of Node to be deleted
  559. */
  560. void deviceNodeFactory::deleteNode(uint32_t id)
  561. {
  562. deviceNode * tNode = getNode(id);
  563. deleteNode(tNode);
  564. }
  565. bool deviceNodeFactory::validate(void)
  566. {
  567. return (m_bThreadStatus < TH_ENDED);
  568. }
  569. void deviceNodeFactory::m_fThreadProc(void)
  570. {
  571. if (m_pDBC == NULL)
  572. {
  573. m_bThreadStatus = TH_ENDED;
  574. }
  575. else
  576. {
  577. pthread_setname_np( pthread_self(), "NodeFactory" );
  578. m_bThreadStatus = TH_WORKING;
  579. while (m_bThreadStatus < TH_ENDED)
  580. {
  581. DBC_INT_LIST * tList = NULL;
  582. #ifdef DEBUG
  583. #ifdef MEM_DEBUG
  584. size_t memStart, memEnd, memDiff;
  585. char MEM_MSG[256];
  586. memStart = getCurrentRSS();
  587. #endif
  588. #endif
  589. if (m_nPendingInitCounter)
  590. {
  591. m_nPendingInitCounter--;
  592. }
  593. if (m_vDeviceNodes.size())
  594. {
  595. DBC_INT_LIST tL;
  596. for (vectorDeviceNodes::iterator idx = m_vDeviceNodes.begin(); idx < m_vDeviceNodes.end(); idx++)
  597. {
  598. tL.push_back((*idx)->getNodeID());
  599. }
  600. tList = m_pDBC->listNewNodes(&tL);
  601. //ToDo: Must be Written ad method to Delete on the fly Disabled Nodes
  602. // To provide this this performance without restarting App
  603. // tL may be used to query Database if a Node is disabled
  604. tL.clear();
  605. }
  606. else
  607. {
  608. tList = m_pDBC->listNewNodes(NULL);
  609. };
  610. if (tList != NULL)
  611. {
  612. #ifdef DEBUG_EXTRA
  613. LOG("Nodes are Added!");
  614. #endif
  615. for (DBC_INT_LIST::iterator idx = tList->begin(); idx != tList->end(); idx++)
  616. {
  617. if (m_nPendingNodes.size())
  618. {
  619. bool mustAdd = true;
  620. for (DBC_INT_LIST::iterator dx = m_nPendingNodes.begin(); dx != m_nPendingNodes.end(); dx++)
  621. {
  622. if ((*dx == *idx) && (m_nPendingInitCounter))
  623. {
  624. mustAdd = false;
  625. }
  626. };
  627. if (mustAdd)
  628. {
  629. createNode(*idx);
  630. #ifdef DEBUG_EXTRA
  631. }
  632. else
  633. {
  634. LOG("Node is in PENDING mode!");
  635. #endif
  636. };
  637. }
  638. else
  639. {
  640. createNode(*idx);
  641. };
  642. };
  643. tList->clear();
  644. delete(tList);
  645. #ifdef DEBUG_EXTRA
  646. }
  647. else
  648. {
  649. LOG("No New Nodes");
  650. #endif
  651. };
  652. #ifdef MEM_DEBUG
  653. memEnd = getCurrentRSS();
  654. memDiff = memEnd - memStart;
  655. sprintf(MEM_MSG, "MEM START: %d MEM DIF1: %d", memStart, memDiff);
  656. LOG(MEM_MSG);
  657. #endif
  658. if (m_vDeviceNodes.size())
  659. {
  660. // Get Full List
  661. tList = m_pDBC->listNodes();
  662. if (tList != NULL)
  663. {
  664. for (vectorDeviceNodes::iterator idx = m_vDeviceNodes.begin(); idx < m_vDeviceNodes.end(); idx++)
  665. {
  666. bool tRemoved = true;
  667. // Check if Removed By changes in DB
  668. for (DBC_INT_LIST::iterator idz = tList->begin(); idz < tList->end(); idz++)
  669. {
  670. if ((*idz) == (int)(*idx)->getNodeID())
  671. {
  672. tRemoved = false;
  673. }
  674. }
  675. // If Not - check validation
  676. if (! tRemoved)
  677. {
  678. #ifdef DEBUG_EXTRA
  679. LOG("Node Among Active Nodes.... make Validation");
  680. #endif
  681. tRemoved = ! (*idx)->validate();
  682. };
  683. // If Node is Invalid - Removing
  684. if (tRemoved)
  685. {
  686. #ifdef DEBUG_EXTRA
  687. LOG("Node not passed Validation!");
  688. #endif
  689. deleteNode(*idx);
  690. };
  691. };
  692. tList->clear();
  693. delete(tList);
  694. }
  695. else
  696. {
  697. #ifdef DEUBG_EXTRA
  698. LOG("All Modules Must be Deleted!");
  699. #endif
  700. for (vectorDeviceNodes::iterator idx = m_vDeviceNodes.begin(); idx < m_vDeviceNodes.end(); idx++)
  701. {
  702. deleteNode(*idx);
  703. }
  704. m_vDeviceNodes.clear();
  705. m_mDeviceNodesIDS.clear();
  706. };
  707. #ifdef DEBUG_EXTRA
  708. }
  709. else
  710. {
  711. LOG("No Nodes to Validate");
  712. #endif
  713. };
  714. #ifdef DEBUG
  715. #ifdef MEM_DEBUG
  716. memEnd = getCurrentRSS();
  717. memDiff = memEnd - memStart;
  718. sprintf(MEM_MSG, "MEM START: %d MEM DIF2: %d", memStart, memDiff);
  719. LOG(MEM_MSG);
  720. #endif
  721. #endif
  722. // Sleep for 1 second
  723. for (int i = (1000 / DEFAULT_THREAD_SLEEP_TIME); i && (m_bThreadStatus < TH_ENDED); i--)
  724. {
  725. SLEEP(DEFAULT_THREAD_SLEEP_TIME);
  726. }
  727. };
  728. };
  729. LOG("EXIT FROM CTRL THREAD");
  730. }
  731. uint32_t deviceNodeFactory::getNodesCount(ARGUS_NODE_TYPE type)
  732. {
  733. uint32_t RetVal = 0;
  734. if (m_vDeviceNodes.size())
  735. {
  736. for (vectorDeviceNodes::iterator index = m_vDeviceNodes.begin(); index < m_vDeviceNodes.end(); index++)
  737. {
  738. if ((*index)->getNodeType() == type)
  739. {
  740. RetVal++;
  741. }
  742. }
  743. }
  744. return RetVal;
  745. }
  746. uint32_t deviceNodeFactory::fetchNodes(ARGUS_NODE_TYPE type, deviceNode **& RA)
  747. {
  748. uint32_t RetVal = 0;
  749. if (RA == NULL)
  750. {
  751. uint32_t cnt = getNodesCount(type);
  752. if (cnt > 0)
  753. {
  754. RA = (deviceNode **) calloc(cnt, sizeof(deviceNode *));
  755. if (RA == NULL)
  756. {
  757. return 0;
  758. }
  759. // memset( RA, 0, cnt * sizeof(deviceNode *) );
  760. for (vectorDeviceNodes::iterator index = m_vDeviceNodes.begin(); index < m_vDeviceNodes.end(); index++)
  761. {
  762. if ((*index)->getNodeType() == type)
  763. {
  764. RA[RetVal] = (*index);
  765. RetVal++;
  766. };
  767. };
  768. };
  769. }
  770. else
  771. {
  772. if (m_vDeviceNodes.size())
  773. {
  774. for (vectorDeviceNodes::iterator index = m_vDeviceNodes.begin(); index < m_vDeviceNodes.end(); index++)
  775. {
  776. if ((*index)->getNodeType() == type)
  777. {
  778. RA[RetVal] = (*index);
  779. RetVal++;
  780. };
  781. };
  782. };
  783. };
  784. return RetVal;
  785. }
  786. #undef LOG
  787. #undef LOG_ERROR