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.

425 lines
9.1 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
1 day ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #include "protocolBVMS.h"
  2. #include "../../alg/generic/md5.h"
  3. #define LOG(X) m_pLog->Log("PBVMS", this, X)
  4. #define LOG_ERROR(X) m_pLog->Log("PBVMS", this, _LOG_ERROR, X)
  5. CSTR systemStatusMode[] = {"Operational", "Degraded"};
  6. CSTR systemFaultCode[] = {"Critical", "Warning", "Info"};
  7. CSTR getSHTimeStamp(void){
  8. CSTR RetVal;
  9. #ifdef __linux__
  10. RetVal = (CSTR) malloc(64);
  11. if (RetVal != NULL){
  12. sprintf(RetVal, "%llu", getTimeValue());
  13. };
  14. #else
  15. RetVal = (CSTR) malloc(64);
  16. if (RetVal != NULL){
  17. sprintf(RetVal, "%I64d", getTimeValue());
  18. };
  19. #endif
  20. return RetVal;
  21. }
  22. CSTR protocolBVMS::buildURL(CSTR templ, int dim, CSTR devID){
  23. CSTR RetVal;
  24. char tpl[256];
  25. RetVal = (CSTR) malloc(256);
  26. sprintf(tpl, "http://%s:%d%s", m_pServerIP, m_nServerPort, templ);
  27. sprintf(RetVal, tpl, dim, devID);
  28. return RetVal;
  29. }
  30. CSTR protocolBVMS::buildUniqID(void){
  31. CSTR RetVal;
  32. RetVal = (CSTR) malloc(64);
  33. if (RetVal)
  34. {
  35. // both getSHTimeStamp() and MD5String() return malloc'ed strings - free them
  36. CSTR tTS = getSHTimeStamp();
  37. CSTR tMD5 = tTS ? MD5String(tTS) : NULL;
  38. snprintf(RetVal, 64, "%s", tMD5 ? tMD5 : "");
  39. free(tMD5);
  40. free(tTS);
  41. };
  42. return RetVal;
  43. }
  44. protocolBVMS::protocolBVMS( CSTR server, uint16_t port ){
  45. m_pServerIP = strdup( server );
  46. m_nServerPort = port;
  47. }
  48. /*
  49. 1.2 GHz
  50. Number of Cores
  51. Quad-Core
  52. Type
  53. Tegra 3
  54. Processor Number
  55. T30SL
  56. Manufacturer
  57. NVIDIA
  58. Verify that you have the following installed on your system
  59. Ubuntu 16.04 LTS (with GCC 5.4)
  60. NVIDIA Display Driver ver. 375
  61. NVIDIA VideoSDK 7.1
  62. cuDNN 6 & TensorRT 2.1
  63. NVIDIA CUDA 8.0
  64. Download the DeepStream 1.0 tar file (below)
  65. Extract the package at the location of your choice
  66. Follow the usage instructions from the Deepstream_User_guide.pdf in the downloaded package.
  67. */
  68. protocolBVMS::~protocolBVMS()
  69. {
  70. //if (m_pScannerName != NULL)
  71. //{
  72. // free(m_pScannerName);
  73. //}
  74. //if (m_pServerIP != NULL)
  75. //{
  76. // free(m_pServerIP);
  77. //}
  78. }
  79. void protocolBVMS::setScannerName(CSTR Name)
  80. {
  81. //sprintf(m_pScannerName, "%s", Name);
  82. }
  83. void protocolBVMS::setProtocolType(bool DeviceIs3D)
  84. {
  85. // if (DeviceIs3D)
  86. // {
  87. // m_nScannerDimentions = 3;
  88. // }
  89. // else
  90. // {
  91. // m_nScannerDimentions = 2;
  92. // };
  93. }
  94. bool protocolBVMS::buildStatusPackage(unsigned char * Buffer, int & BufferSize, bool Status)
  95. {
  96. if (Buffer != NULL)
  97. {
  98. CSTR tBuf;
  99. tBuf = getSHTimeStamp();
  100. if (tBuf != NULL)
  101. {
  102. if (Status)
  103. {
  104. // sprintf((char *) (Buffer + BufferSize), PACKAGE_ENVELOPE_TEMPLATE_STATUS, systemStatusMode[0], tBuf);
  105. }
  106. else
  107. {
  108. // sprintf((char *) (Buffer + BufferSize), PACKAGE_ENVELOPE_TEMPLATE_STATUS, systemStatusMode[1], tBuf);
  109. };
  110. BufferSize = strlen((char *) Buffer);
  111. free(tBuf);
  112. return true;
  113. };
  114. };
  115. return false;
  116. }
  117. bool protocolBVMS::buildFaultPackage(unsigned char * Buffer, int & BufferSize, unsigned char Type, CSTR Description)
  118. {
  119. if (Buffer != NULL)
  120. {
  121. CSTR tBuf;
  122. tBuf = getSHTimeStamp();
  123. if (tBuf != NULL)
  124. {
  125. // sprintf((char *) (Buffer + BufferSize), PACKAGE_ENVELOPE_TEMPLATE_FAULT, systemFaultCode[Type], tBuf, Description);
  126. BufferSize = strlen((char *) Buffer);
  127. free(tBuf);
  128. return true;
  129. };
  130. };
  131. return false;
  132. }
  133. /*
  134. bool protocolBVMS::buildEvent2DPackage(unsigned char * Buffer, int & BufferSize, CSTR msgUID, int targetID, int Angle, int Range, int Width)
  135. {
  136. if (Buffer != NULL)
  137. {
  138. CSTR tBuf;
  139. tBuf = getSHTimeStamp();
  140. if (tBuf != NULL)
  141. {
  142. // sprintf((char *) (Buffer + BufferSize), PACKAGE_ENVELOPE_TEMPLATE_EVT2D, msgUID, tBuf, targetID, Angle, Range, Width);
  143. BufferSize = strlen((char *) Buffer);
  144. free(tBuf);
  145. return true;
  146. };
  147. };
  148. return false;
  149. }
  150. bool protocolBVMS::buildEvent3DPackage(unsigned char * Buffer, int & BufferSize, CSTR msgUID, int targetID, int Angle1, int Angle2, int Range)
  151. {
  152. if (Buffer != NULL)
  153. {
  154. CSTR tBuf;
  155. tBuf = getSHTimeStamp();
  156. if (tBuf != NULL)
  157. {
  158. //sprintf((char *) (Buffer + BufferSize), PACKAGE_ENVELOPE_TEMPLATE_EVT3D, msgUID, tBuf, targetID, Angle1, Angle2, Range);
  159. BufferSize = strlen((char *) Buffer);
  160. free(tBuf);
  161. return true;
  162. };
  163. };
  164. return false;
  165. }
  166. */
  167. bool protocolBVMS::buildClockSyncPackage(unsigned char * Buffer, int & BufferSize)
  168. {
  169. if (Buffer != NULL)
  170. {
  171. CSTR tBuf;
  172. tBuf = getSHTimeStamp();
  173. if (tBuf != NULL)
  174. {
  175. //sprintf((char *) (Buffer + BufferSize), PACKAGE_ENVELOPE_TEMPLATE_CLOCKSYNC, tBuf);
  176. BufferSize = strlen((char *) Buffer);
  177. free(tBuf);
  178. return true;
  179. };
  180. };
  181. return false;
  182. }
  183. bool protocolBVMS::buildCallBackPackage(unsigned char * Buffer, int & BufferSize, CSTR CallBackPath)
  184. {
  185. if (Buffer != NULL)
  186. {
  187. //sprintf((char *) (Buffer + BufferSize), PACKAGE_ENVELOPE_TEMPLATE_REG_CALL, CallBackPath);
  188. BufferSize = strlen((char *) Buffer);
  189. return true;
  190. };
  191. return false;
  192. }
  193. /*
  194. bool protocolBVMS::Alarm2D(int Angle, int Range, int Width, int ID)
  195. {
  196. bool RetVal = false;
  197. char Buffer[1024];
  198. int BufferSize = 0;
  199. CSTR URL;
  200. CSTR UID;
  201. if (! m_bEnabled)
  202. {
  203. return false;
  204. }
  205. UID = buildUniqID();
  206. if (UID != NULL)
  207. {
  208. //URL = buildURL(SERVICE_URL_TEMPLATE_EVENT, m_nScannerDimentions, m_pScannerName);
  209. RetVal = buildEvent2DPackage((unsigned char *) Buffer, BufferSize, UID, ID, Angle, Range, Width);
  210. if (URL != NULL)
  211. {
  212. RetVal = issuePost(URL, Buffer, BufferSize);
  213. free(URL);
  214. };
  215. free(UID);
  216. };
  217. return RetVal;
  218. }
  219. bool protocolBVMS::Alarm3D(int Angle1, int Angle2, int Range, int ID)
  220. {
  221. bool RetVal = false;
  222. char Buffer[1024];
  223. int BufferSize = 0;
  224. CSTR URL;
  225. CSTR UID;
  226. if (! m_bEnabled)
  227. {
  228. return false;
  229. }
  230. UID = buildUniqID();
  231. if (UID != NULL)
  232. {
  233. //URL = buildURL(SERVICE_URL_TEMPLATE_EVENT, m_nScannerDimentions, m_pScannerName);
  234. LOG(URL);
  235. RetVal = buildEvent3DPackage((unsigned char *) Buffer, BufferSize, UID, ID, Angle1, Angle2, Range);
  236. if (URL != NULL)
  237. {
  238. RetVal = issuePost(URL, Buffer, BufferSize);
  239. free(URL);
  240. };
  241. free(UID);
  242. };
  243. return RetVal;
  244. }
  245. */
  246. bool protocolBVMS::VisibilityChanged(bool Worsened)
  247. {
  248. bool RetVal = false;
  249. char Buffer[1024];
  250. int BufferSize = 0;
  251. CSTR URL;
  252. //URL = buildURL(SERVICE_URL_TEMPLATE_STATUS, m_nScannerDimentions, m_pScannerName);
  253. RetVal = buildStatusPackage((unsigned char *) Buffer, BufferSize, ! Worsened);
  254. if (URL != NULL)
  255. {
  256. RetVal = issuePost(URL, Buffer, BufferSize);
  257. free(URL);
  258. };
  259. return RetVal;
  260. }
  261. bool protocolBVMS::StatusUpdate(void)
  262. {
  263. bool RetVal = false;
  264. char Buffer[1024];
  265. int BufferSize = 0;
  266. CSTR URL;
  267. //if (! m_bEnabled)
  268. //{
  269. // return false;
  270. //}
  271. //URL = buildURL(SERVICE_URL_TEMPLATE_STATUS, m_nScannerDimentions, m_pScannerName);
  272. RetVal = buildStatusPackage((unsigned char *) Buffer, BufferSize, m_bCurrentStatus);
  273. if (URL != NULL)
  274. {
  275. RetVal = issuePost(URL, Buffer, BufferSize);
  276. free(URL);
  277. };
  278. return RetVal;
  279. }
  280. bool protocolBVMS::ErrorReporting(int lvl, CSTR Descr)
  281. {
  282. bool RetVal = false;
  283. char Buffer[1024];
  284. int BufferSize = 0;
  285. CSTR URL;
  286. //if (! m_bEnabled)
  287. //{
  288. // return false;
  289. //}
  290. //URL = buildURL(SERVICE_URL_TEMPLATE_FAULT, m_nScannerDimentions, m_pScannerName);
  291. RetVal = buildFaultPackage((unsigned char *) Buffer, BufferSize, lvl, Descr);
  292. if (URL != NULL)
  293. {
  294. RetVal = issuePost(URL, Buffer, BufferSize);
  295. free(URL);
  296. };
  297. return RetVal;
  298. }
  299. bool protocolBVMS::StartTimeSynch(void)
  300. {
  301. bool RetVal = false;
  302. char Buffer[1024];
  303. int BufferSize = 0;
  304. CSTR URL = NULL;
  305. //if (! m_bEnabled)
  306. //{
  307. // return false;
  308. //}
  309. //URL = buildURL(SERVICE_URL_TEMPLATE_CLOCKSYNC, m_nScannerDimentions, m_pScannerName);
  310. RetVal = buildClockSyncPackage((unsigned char *) Buffer, BufferSize);
  311. if (URL != NULL)
  312. {
  313. RetVal = issuePost(URL, Buffer, BufferSize);
  314. free(URL);
  315. };
  316. return RetVal;
  317. }
  318. bool protocolBVMS::packetHandler(protocolPackage * Package)
  319. {
  320. // LOG("PACKET HANDLER");
  321. //if (! m_bEnabled)
  322. //{
  323. // return false;
  324. //}
  325. if (Package == NULL)
  326. {
  327. return false;
  328. }
  329. if (Package->m_nSize == 0)
  330. {
  331. return false;
  332. }
  333. if (Package->m_pData == NULL)
  334. {
  335. return false;
  336. }
  337. if (Package->m_nCode != 200)
  338. {
  339. return false;
  340. }
  341. /*
  342. try
  343. {
  344. XMLNode node1 = XMLNode::parseString((const char *) Package->m_pData, "root");
  345. if (node1.isEmpty())
  346. {
  347. // char BUBU[128];
  348. // sprintf( BUBU, "The package[%d:%d] XML data is not a valid XML data. Cann't find root element.", strlen(pPackage), Package->m_nSize );
  349. // LOG_ERROR(BUBU);
  350. }
  351. else
  352. {
  353. LOG(node1.getName());
  354. if (strstr(node1.getName(), "ClockSync") != NULL)
  355. {
  356. #ifdef __linux__
  357. unsigned long long t0, t1, t2, t3;
  358. #else
  359. unsigned __int64 t0, t1, t2, t3;
  360. #endif
  361. XMLNode node3 = node1.getChildNode("t0", 0);
  362. XMLNode node4 = node1.getChildNode("t1", 0);
  363. XMLNode node5 = node1.getChildNode("t2", 0);
  364. t3 = getTimeValue();
  365. #ifdef __linux__
  366. t0 = atoll(node3.getText(0));
  367. t1 = atoll(node4.getText(0));
  368. t2 = atoll(node5.getText(0));
  369. #else
  370. t0 = _atoi64(node3.getText(0));
  371. t1 = _atoi64(node4.getText(0));
  372. t2 = _atoi64(node5.getText(0));
  373. #endif
  374. int offset = ((t1 - t0) + (t2 - t3)) / 2;
  375. int latency = (t3 - t0) - (t2 - t1);
  376. int jagajaga = offset + latency;
  377. // if( 0 & correctSHTime( jagajaga ) ){
  378. // LOG("Time is Synced");
  379. // g_iTimeDiff = 0;
  380. // } else {
  381. // LOG_ERROR("Time Sync Unucessfull - Will use DIFF!");
  382. correctSoftTime(jagajaga);
  383. //};
  384. };
  385. };
  386. }
  387. catch (exception e)
  388. {
  389. };
  390. */
  391. return true;
  392. }
  393. #undef LOG
  394. #undef LOG_ERROR