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.

827 lines
19 KiB

2 years ago
2 years ago
​
2 years ago
​
​
​
​
​
2 years ago
2 years ago
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
​
​
​
​
​
​
2 years 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
​
​
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
​
​
​
​
​
​
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
4 months ago
​
​
​
​
​
​
4 months ago
​
​
​
​
​
​
4 months ago
​
​
​
​
​
​
4 months ago
​
​
​
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
2 years ago
​
​
​
​
​
​
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
2 years ago
​
​
​
​
​
​
​
​
​
2 years ago
​
​
​
2 years ago
​
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
2 years ago
​
  1. #include "kernelModuleModemManager.h"
  2. #include "../../components/base/basicFunctionality.h"
  3. #define LOG(X) kernelModule::m_pLog->Log("MOD_MODEM", this, X)
  4. #ifdef DEBUG
  5. #define LOG_ERROR(X) \
  6. { \
  7. char _tBfrErrMSG2w34[256]; \
  8. sprintf(_tBfrErrMSG2w34, "In file %s[%d] in func:%s %s", __FILE__, __LINE__, __func__, X); \
  9. kernelModule::m_pLog->Log("MOD_MODEM", this, _LOG_ERROR, _tBfrErrMSG2w34); \
  10. }
  11. #else
  12. #define LOG_ERROR(X) kernelModule::m_pLog->Log("MOD_MODEM", this, _LOG_ERROR, X)
  13. #endif
  14. #define SLOG(X) DATA->parent->m_pLog->Log("SMOD_MODEM", DATA->parent, X)
  15. kernelModuleModemManager::kernelModuleModemManager(uint32_t moduleID) :
  16. kernelModule(moduleID, KERNEL_MODULE_MODEM_MANAGER), basicThread()
  17. {
  18. m_cMyWanIP[0] = (char) 0;
  19. moduleActive = false;
  20. sessionStarted = false;
  21. m_bEnabled = true;
  22. if (checkCmdExists("mmcli") > 0)
  23. {
  24. LOG("ModemManager Present!");
  25. DBC_MODEM_MANAGER_DATA * tData = m_pDBC->getModemManagerData(m_nDBAUXID);
  26. if (tData)
  27. {
  28. memcpy(&m_sMMData, tData, sizeof(DBC_MODEM_MANAGER_DATA));
  29. free(tData);
  30. // Hangup all currently active calls
  31. if( m_sMMData.active_call > -1 )
  32. {
  33. char tCMD[128];
  34. sprintf(tCMD, "mmcli -m %d --call %d --hangup", m_sMMData.modem_id, m_sMMData.active_call);
  35. system(tCMD);
  36. sprintf(tCMD, "mmcli -m %d --voice-delete-call=%d", m_sMMData.modem_id, m_sMMData.active_call);
  37. system(tCMD);
  38. };
  39. moduleActive = true;
  40. }
  41. else
  42. {
  43. LOG_ERROR("No ModemManager Data Fetched from DB!");
  44. m_bEnabled = false;
  45. };
  46. }
  47. else
  48. {
  49. LOG_ERROR("No ModemManager installed in the System!");
  50. };
  51. }
  52. kernelModuleModemManager::~kernelModuleModemManager()
  53. {
  54. // if ( m_vUserData.size() ) {
  55. // for ( VECTOR_MDM_USER_DATA::iterator idx = m_vUserData.begin(); idx < m_vUserData.end(); idx++ ) {
  56. // if ( !(*idx)->finished ) pthread_join( (*idx)->handler, NULL);
  57. // };
  58. //};
  59. }
  60. bool kernelModuleModemManager::reloadSettings()
  61. {
  62. // LOG("Reload");
  63. if (getWM() == MDM_IDLE_WM)
  64. {
  65. DBC_INT_LIST * pList = m_pDBC->listPhonebook();
  66. if (pList)
  67. {
  68. if (m_vPhoneBook.size())
  69. {
  70. for (VECTOR_MDM_PHONE_BOOK::iterator idx = m_vPhoneBook.begin(); idx < m_vPhoneBook.end(); idx++)
  71. {
  72. free(*idx);
  73. };
  74. m_vPhoneBook.clear();
  75. };
  76. if (pList->size())
  77. {
  78. for (DBC_INT_LIST::iterator idx = pList->begin(); idx < pList->end(); idx++)
  79. {
  80. DBC_MODEM_PHONEBOOK_RECORD * tPtr = m_pDBC->getModemPhonebookRecord(*idx);
  81. if (tPtr)
  82. {
  83. #ifdef DEBUG_EXTRA
  84. char tB[200];
  85. sprintf(tB, "PBN: %s, LBL: %s MSG: %s", tPtr->number, tPtr->label, tPtr->custom_msg);
  86. LOG(tB);
  87. #endif
  88. m_vPhoneBook.push_back(tPtr);
  89. };
  90. };
  91. }
  92. else
  93. {
  94. moduleActive = false;
  95. }
  96. delete (pList); // list returned by listPhonebook() is owned by the caller
  97. };
  98. return true;
  99. };
  100. return false;
  101. }
  102. bool kernelModuleModemManager::startService()
  103. {
  104. sessionStarted = false;
  105. LOG("Start Service");
  106. if (! m_bEnabled)
  107. {
  108. LOG_ERROR("Module is Disabled!");
  109. return false;
  110. };
  111. // Load PhoneBook
  112. setWM(MDM_IDLE_WM);
  113. if (m_sMMData.sms_enabled)
  114. {
  115. LOG("SMS Enabled!");
  116. }
  117. else
  118. {
  119. LOG("SMS DISABLED!");
  120. }
  121. // Get current WAN IP Address
  122. if (updateWanIP())
  123. {
  124. LOG("WAN IP OK!");
  125. }
  126. else
  127. {
  128. LOG("WAN IP NOK!");
  129. }
  130. return moduleActive = init();
  131. }
  132. bool kernelModuleModemManager::stopService(bool force)
  133. {
  134. LOG("Stop Service");
  135. setWM(MDM_STOP_WM);
  136. if ( force ) {
  137. m_bEnabled = false;
  138. waitThreadEnd( force );
  139. } else {
  140. m_bEnabled = !uninit();
  141. };
  142. return true;
  143. }
  144. void kernelModuleModemManager::m_fThreadProc(void)
  145. {
  146. uint32_t nCurrentPhoneBookId = 0;
  147. int32_t nCurrentCallId = 0;
  148. uint64_t lNextWindowAction = 0;
  149. pthread_setname_np( pthread_self(), "ModemManager" );
  150. m_bThreadStatus = TH_WORKING;
  151. while (m_bThreadStatus < TH_ENDED)
  152. {
  153. uint8_t bCWM = getWM();
  154. switch (bCWM)
  155. {
  156. case MDM_IDLE_WM:
  157. {
  158. break;
  159. };
  160. case MDM_INIT_CALL_WM:
  161. {
  162. if (nCurrentPhoneBookId < m_vPhoneBook.size())
  163. {
  164. DBC_MODEM_PHONEBOOK_RECORD * tRECORD = m_vPhoneBook.at(nCurrentPhoneBookId);
  165. if (tRECORD)
  166. {
  167. m_bCurrentMode = tRECORD->mode;
  168. m_pDBC->updateModemManagerActivePhoneBookID(m_sMMData.id, tRECORD->id);
  169. switch (m_bCurrentMode)
  170. {
  171. case 0:
  172. {
  173. if (m_sMMData.sms_enabled)
  174. {
  175. nCurrentCallId = -1;
  176. LOG("Start SMS");
  177. char CMSG[512];
  178. char CMD[1024];
  179. sprintf(CMSG, "Alarm Message %s System at http://%s/", tRECORD->custom_msg, m_cMyWanIP);
  180. sprintf(CMD, "mmcli -m %d --messaging-create-sms=\"text='%s',number='%s'\"", m_sMMData.modem_id, CMSG, tRECORD->number);
  181. LOG(CMD);
  182. FILE * fd = popen(CMD, "r");
  183. if (fd)
  184. {
  185. char tB[1024];
  186. if (fread(tB, 1, 1023, fd) > 0)
  187. {
  188. CSTR tS = strstr(tB, "SMS/");
  189. if (tS)
  190. {
  191. nCurrentCallId = atoi(tS + 4);
  192. };
  193. };
  194. pclose(fd);
  195. }
  196. else
  197. {
  198. LOG_ERROR("Cannot Start POPEN 1!");
  199. }
  200. if (nCurrentCallId > -1)
  201. {
  202. sprintf(CMD, "mmcli -s %d --send", nCurrentCallId);
  203. system(CMD);
  204. m_pDBC->updateModemManagerActiveCall(m_sMMData.id, nCurrentCallId);
  205. lNextWindowAction = getMilliseconds() + m_sMMData.call_hangup_wait * 1000;
  206. // Need to wait till calling
  207. setWM(MDM_MODEM_IS_BUSY_WM);
  208. }
  209. else
  210. {
  211. setWM(MDM_FETCH_NEXT_WM);
  212. };
  213. m_bCurrentMode = 2;
  214. continue;
  215. };
  216. if (m_sMMData.call_enabled)
  217. {
  218. nCurrentCallId = -1;
  219. LOG("Start Call");
  220. char CMD[128];
  221. sprintf(CMD, "mmcli -m %d --voice-create-call='number=%s'", m_sMMData.modem_id, tRECORD->number);
  222. LOG(CMD);
  223. FILE * fd = popen(CMD, "r");
  224. if (fd)
  225. {
  226. char tB[1024];
  227. if (fread(tB, 1, 1023, fd) > 0)
  228. {
  229. CSTR tS = strstr(tB, "Call/");
  230. if (tS)
  231. {
  232. nCurrentCallId = atoi(tS + 5);
  233. };
  234. };
  235. pclose(fd);
  236. }
  237. else
  238. {
  239. LOG_ERROR("Cannot Start POPEN 1!");
  240. }
  241. if (nCurrentCallId > -1)
  242. {
  243. sprintf(CMD, "mmcli -m %d --call %d --start", m_sMMData.modem_id, nCurrentCallId);
  244. system(CMD);
  245. m_pDBC->updateModemManagerActiveCall(m_sMMData.id, nCurrentCallId);
  246. lNextWindowAction = getMilliseconds() + m_sMMData.call_hangup_wait * 1000;
  247. // Need to wait till calling
  248. setWM(MDM_MODEM_IS_BUSY_WM);
  249. }
  250. else
  251. {
  252. setWM(MDM_FETCH_NEXT_WM);
  253. };
  254. m_bCurrentMode = 1;
  255. continue;
  256. };
  257. setWM(MDM_FETCH_NEXT_WM);
  258. break;
  259. };
  260. case 1:
  261. {
  262. if (! m_sMMData.call_enabled)
  263. {
  264. setWM(MDM_FETCH_NEXT_WM);
  265. }
  266. nCurrentCallId = -1;
  267. LOG("Start Call");
  268. char CMD[128];
  269. sprintf(CMD, "mmcli -m %d --voice-create-call='number=%s'", m_sMMData.modem_id, tRECORD->number);
  270. LOG(CMD);
  271. FILE * fd = popen(CMD, "r");
  272. if (fd)
  273. {
  274. char tB[1024];
  275. if (fread(tB, 1, 1023, fd) > 0)
  276. {
  277. CSTR tS = strstr(tB, "Call/");
  278. if (tS)
  279. {
  280. nCurrentCallId = atoi(tS + 5);
  281. };
  282. };
  283. pclose(fd);
  284. }
  285. else
  286. {
  287. LOG_ERROR("Cannot Start POPEN 1!");
  288. }
  289. if (nCurrentCallId > -1)
  290. {
  291. sprintf(CMD, "mmcli -m %d --call %d --start", m_sMMData.modem_id, nCurrentCallId);
  292. system(CMD);
  293. m_pDBC->updateModemManagerActiveCall(m_sMMData.id, nCurrentCallId);
  294. lNextWindowAction = getMilliseconds() + m_sMMData.call_hangup_wait * 1000;
  295. // Need to wait till calling
  296. setWM(MDM_MODEM_IS_BUSY_WM);
  297. }
  298. else
  299. {
  300. setWM(MDM_FETCH_NEXT_WM);
  301. };
  302. break;
  303. };
  304. case 2:
  305. {
  306. if (! m_sMMData.sms_enabled)
  307. {
  308. setWM(MDM_FETCH_NEXT_WM);
  309. }
  310. nCurrentCallId = -1;
  311. LOG("Start SMS");
  312. char CMSG[512];
  313. char CMD[1024];
  314. sprintf(CMSG, "Alarm Message %s System at http://%s/", tRECORD->custom_msg, m_cMyWanIP);
  315. sprintf(CMD, "mmcli -m %d --messaging-create-sms=\"text='%s',number='%s'\"", m_sMMData.modem_id, CMSG, tRECORD->number);
  316. LOG(CMD);
  317. FILE * fd = popen(CMD, "r");
  318. if (fd)
  319. {
  320. char tB[1024];
  321. if (fread(tB, 1, 1023, fd) > 0)
  322. {
  323. CSTR tS = strstr(tB, "SMS/");
  324. if (tS)
  325. {
  326. nCurrentCallId = atoi(tS + 4);
  327. };
  328. };
  329. pclose(fd);
  330. }
  331. else
  332. {
  333. LOG_ERROR("Cannot Start POPEN 1!");
  334. }
  335. if (nCurrentCallId > -1)
  336. {
  337. sprintf(CMD, "mmcli -s %d --send", nCurrentCallId);
  338. system(CMD);
  339. m_pDBC->updateModemManagerActiveCall(m_sMMData.id, nCurrentCallId);
  340. lNextWindowAction = getMilliseconds() + m_sMMData.call_hangup_wait * 1000;
  341. // Need to wait till calling
  342. setWM(MDM_MODEM_IS_BUSY_WM);
  343. }
  344. else
  345. {
  346. setWM(MDM_FETCH_NEXT_WM);
  347. };
  348. break;
  349. };
  350. };
  351. } else {
  352. char ttB[65];
  353. sprintf(ttB, "No Phone Book Record [%d]!", nCurrentPhoneBookId);
  354. LOG_ERROR(ttB);
  355. //sessionStarted = false;
  356. setWM(MDM_IDLE_WM);
  357. };
  358. } else {
  359. LOG("Finished Fetching all call rcpt or there no rcpt!");
  360. nCurrentPhoneBookId = 0;
  361. setWM(MDM_IDLE_WM);
  362. };
  363. break;
  364. };
  365. case MDM_MODEM_IS_BUSY_WM:
  366. {
  367. if (m_bCurrentMode == 1)
  368. {
  369. // Finishing Call
  370. char CMD[128];
  371. bool call_is_answered = false;
  372. bool call_is_terminated = false;
  373. bool call_is_finished = false;
  374. sprintf(CMD, "mmcli -m %d --voice-list-calls", m_sMMData.modem_id);
  375. FILE * fd = popen(CMD, "r");
  376. if (fd)
  377. {
  378. char tB[1024];
  379. if (fread(tB, 1, 1023, fd) > 0)
  380. {
  381. CSTR tS = strstr(tB, "(active)");
  382. if (tS)
  383. {
  384. call_is_answered = true;
  385. };
  386. tS = strstr(tB, "(terminated)");
  387. if (tS)
  388. {
  389. call_is_terminated = true;
  390. };
  391. };
  392. pclose(fd);
  393. }
  394. else
  395. {
  396. LOG_ERROR("Cannot Start POPEN 2!");
  397. }
  398. if (call_is_answered || call_is_terminated)
  399. {
  400. if (call_is_answered)
  401. {
  402. LOG("Call is Answered!!");
  403. // Send Voice Message to user
  404. };
  405. call_is_finished = true;
  406. }
  407. else
  408. {
  409. if (lNextWindowAction < getMilliseconds())
  410. {
  411. call_is_finished = true;
  412. }
  413. };
  414. if (call_is_finished)
  415. {
  416. LOG("Hangup Call");
  417. sprintf(CMD, "mmcli -m %d --call %d --hangup", m_sMMData.modem_id, nCurrentCallId);
  418. LOG(CMD);
  419. system(CMD);
  420. sprintf(CMD, "mmcli -m %d --voice-delete-call=%d", m_sMMData.modem_id, nCurrentCallId);
  421. LOG(CMD);
  422. system(CMD);
  423. m_pDBC->updateModemManagerActiveCall(m_sMMData.id, -1);
  424. setWM(MDM_FETCH_NEXT_WM);
  425. };
  426. }
  427. else
  428. {
  429. // Finishing SMS
  430. char CMD[128];
  431. bool sms_is_sent = false;
  432. sprintf(CMD, "mmcli -m %d --messaging-list-sms", m_sMMData.modem_id);
  433. FILE * fd = popen(CMD, "r");
  434. if (fd)
  435. {
  436. char tB[1024];
  437. if (fread(tB, 1, 1023, fd) > 0)
  438. {
  439. CSTR tS = strstr(tB, "(sent)");
  440. if (tS)
  441. {
  442. sms_is_sent = true;
  443. };
  444. };
  445. pclose(fd);
  446. }
  447. else
  448. {
  449. LOG_ERROR("Cannot Start POPEN 2!");
  450. }
  451. if (sms_is_sent)
  452. {
  453. LOG("SMS is Sent!!");
  454. }
  455. else
  456. {
  457. if (lNextWindowAction < getMilliseconds())
  458. {
  459. sms_is_sent = true;
  460. }
  461. };
  462. if (sms_is_sent)
  463. {
  464. sprintf(CMD, "mmcli -m %d --messaging-delete-sms=%d", m_sMMData.modem_id, nCurrentCallId);
  465. LOG(CMD);
  466. system(CMD);
  467. m_pDBC->updateModemManagerActiveCall(m_sMMData.id, -1);
  468. setWM(MDM_FETCH_NEXT_WM);
  469. };
  470. };
  471. break;
  472. };
  473. case MDM_FETCH_NEXT_WM:
  474. {
  475. if (nCurrentPhoneBookId < m_vPhoneBook.size())
  476. {
  477. nCurrentPhoneBookId++;
  478. setWM(MDM_INIT_CALL_WM);
  479. }
  480. else
  481. {
  482. nCurrentPhoneBookId = 0;
  483. m_pDBC->updateModemManagerActivePhoneBookID(m_sMMData.id, 0);
  484. lNextWindowAction = getMilliseconds() + m_sMMData.time_window;
  485. setWM(MDM_WAIT_WINDOW_WM);
  486. };
  487. break;
  488. };
  489. case MDM_WAIT_WINDOW_WM:
  490. {
  491. if (lNextWindowAction < getMilliseconds())
  492. {
  493. sessionStarted = false;
  494. setWM(MDM_IDLE_WM);
  495. };
  496. break;
  497. };
  498. case MDM_STOP_WM:
  499. {
  500. m_bThreadStatus = TH_ENDED;
  501. break;
  502. }
  503. };
  504. SLEEP(DEFAULT_THREAD_SLEEP_TIME);
  505. };
  506. }
  507. bool kernelModuleModemManager::dispatchEvent(EVENT_CONTAINER * Evt){
  508. if (moduleActive){
  509. if (Evt == NULL) return false;
  510. switch (Evt->m_eType){
  511. case EVT_TARGET_1D:
  512. case EVT_TARGET_3D_WHITE:
  513. case EVT_TARGET_3D_BLACK:
  514. case EVT_TARGET_2D_WHITE:
  515. case EVT_TARGET_2D_BLACK:{
  516. if (!sessionStarted) {
  517. //Only Work On Session Start -> No need to follow each detection
  518. sessionStarted = true;
  519. setWM(MDM_INIT_CALL_WM);
  520. };
  521. break;
  522. };
  523. case EVT_LRF_START_SESSION:{
  524. if (!sessionStarted) {
  525. sessionStarted = true;
  526. setWM(MDM_INIT_CALL_WM);
  527. };
  528. break;
  529. };
  530. case EVT_LRF_END_SESSION_TO:
  531. case EVT_LRF_END_SESSION_MA:{
  532. sessionStarted = false;
  533. break;
  534. };
  535. default: break;
  536. };
  537. };
  538. return true;
  539. }
  540. bool kernelModuleModemManager::validate()
  541. {
  542. reloadSettings();
  543. if (moduleActive)
  544. {
  545. if (m_sMMData.sms_enabled)
  546. {
  547. // Get current WAN IP Address
  548. return updateWanIP();
  549. #ifdef DEBUG_EXTRA
  550. }
  551. else
  552. {
  553. LOG("SMS Disabled!");
  554. #endif
  555. };
  556. #ifdef DEBUG_EXTRA
  557. }
  558. else
  559. {
  560. LOG("Module is Disabled");
  561. #endif
  562. };
  563. return true;
  564. }
  565. #define TLOG_ERROR(X) DATA->parent->m_pLog->Log("MOD_MODEM_T", DATA->parent, _LOG_ERROR, X)
  566. void * kernelModuleModemManager::callProc(void * ptr)
  567. {
  568. MDM_CALL_DATA * DATA = (MDM_CALL_DATA *) ptr;
  569. if (DATA)
  570. {
  571. char CMD[128];
  572. uint32_t callID = 0;
  573. sprintf(CMD, "mmcli -m %d --voice-create-call='number=%s'", DATA->modem_id, DATA->target);
  574. /*
  575. Successfully created new call: /org/freedesktop/ModemManager1/Call/2
  576. */
  577. SLOG(CMD);
  578. FILE * fd = popen(CMD, "r");
  579. if (fd)
  580. {
  581. char tB[1024];
  582. if (fread(tB, 1, 1023, fd) > 0)
  583. {
  584. CSTR tS = strstr(tB, "Call/");
  585. if (tS)
  586. {
  587. callID = atoi(tS + 5);
  588. };
  589. };
  590. pclose(fd);
  591. }
  592. else
  593. {
  594. SLOG("Cannot Start POPEN 1!");
  595. };
  596. if (callID)
  597. {
  598. sprintf(CMD, "mmcli -m %d --call %d --start", DATA->modem_id, callID);
  599. SLOG(CMD);
  600. system(CMD);
  601. // Wait X seconds then hang call or play voice message
  602. uint8_t bTO = 60; // 15 seconds
  603. bool call_is_answered = false;
  604. bool call_is_terminated = false;
  605. sprintf(CMD, "mmcli -m %d --voice-list-calls", DATA->modem_id);
  606. while (bTO && ! call_is_answered && ! call_is_terminated)
  607. {
  608. fd = popen(CMD, "r");
  609. if (fd)
  610. {
  611. char tB[1024];
  612. if (fread(tB, 1, 1023, fd) > 0)
  613. {
  614. CSTR tS = strstr(tB, "(active)");
  615. if (tS)
  616. {
  617. call_is_answered = true;
  618. };
  619. tS = strstr(tB, "(terminated)");
  620. if (tS)
  621. {
  622. call_is_terminated = true;
  623. };
  624. };
  625. pclose(fd);
  626. }
  627. else
  628. {
  629. SLOG("Cannot Start POPEN 2!");
  630. }
  631. SLEEP(250);
  632. bTO--;
  633. };
  634. if (bTO && ! call_is_terminated)
  635. {
  636. if (DATA->parent->m_sMMData.voice_sink[0])
  637. {
  638. // Play Wave
  639. // u-law (8000 Hz, 8-bits per sample, mono, 64k)
  640. // play -t raw -r 8000 -c 1 -e u-law -
  641. // SLEEP(DATA->parent->m_sMMData.call_hangup_wait * 1000);
  642. // unlink("/tmp/argus_voice.wav");
  643. };
  644. };
  645. sprintf(CMD, "mmcli -m %d --call %d --hangup", DATA->modem_id, callID);
  646. SLOG(CMD);
  647. system(CMD);
  648. sprintf(CMD, "mmcli -m %d --voice-delete-call=%d", DATA->modem_id, callID);
  649. SLOG(CMD);
  650. system(CMD);
  651. }
  652. else
  653. {
  654. SLOG("Call is not registered at Modem!");
  655. }
  656. };
  657. pthread_exit(NULL);
  658. return NULL;
  659. }
  660. void * kernelModuleModemManager::smsProc(void * ptr)
  661. {
  662. MDM_SMS_DATA * DATA = (MDM_SMS_DATA *) ptr;
  663. if (DATA)
  664. {
  665. char CMD[128];
  666. uint32_t smsID = 0;
  667. sprintf(CMD, "mmcli -m %d --messaging-create-sms=\"text='%s',number='%s'\"", DATA->modem_id, DATA->text, DATA->target);
  668. FILE * fd = popen(CMD, "r");
  669. if (fd)
  670. {
  671. char tB[1024];
  672. if (fread(tB, 1, 1023, fd) > 0)
  673. {
  674. CSTR tS = strstr(tB, "SMS/");
  675. if (tS)
  676. {
  677. smsID = atoi(tS + 4);
  678. };
  679. };
  680. pclose(fd);
  681. };
  682. if (smsID)
  683. {
  684. // Wait X seconds then hang call
  685. // SLEEP( DATA->parent->m_sMMData.call_hangup_wait * 1000 );
  686. sprintf(CMD, "mmcli -s %d --send", smsID);
  687. fd = popen(CMD, "r");
  688. if (fd)
  689. {
  690. pclose(fd);
  691. }
  692. else
  693. {
  694. TLOG_ERROR("Cannot Execute Call Hang Command!");
  695. };
  696. }
  697. else
  698. {
  699. TLOG_ERROR("Call is not registered at Modem!");
  700. }
  701. };
  702. pthread_exit(NULL);
  703. return NULL;
  704. }
  705. uint32_t kernelModuleModemManager::startCall(CSTR number)
  706. {
  707. MDM_CALL_DATA * DATA = (MDM_CALL_DATA *) malloc(sizeof(MDM_CALL_DATA));
  708. if (DATA)
  709. {
  710. LOG(number);
  711. memset(DATA, 0, sizeof(MDM_CALL_DATA));
  712. DATA->modem_id = m_sMMData.modem_id;
  713. DATA->parent = this;
  714. strncpy(DATA->target, number, 64);
  715. int RetVal = pthread_create(&DATA->handler, NULL, callProc, (void *) DATA);
  716. if (RetVal == -1)
  717. {
  718. LOG_ERROR("Cannot start Call Procedure.");
  719. free(DATA);
  720. return 0;
  721. };
  722. // Push Data into vector
  723. m_vUserData.push_back((MDM_USER_DATA *) DATA);
  724. }
  725. else
  726. {
  727. LOG_ERROR("Cannot allocate Call data.");
  728. };
  729. return 0;
  730. }
  731. void kernelModuleModemManager::sendSMS(CSTR number, CSTR text) {
  732. if ( number || text ) LOG("SMSM");
  733. //todo: Make code
  734. }
  735. void kernelModuleModemManager::hangupCall(uint32_t call_id) {
  736. if ( call_id ) call_id = 0;
  737. //todo: Make code
  738. }
  739. bool kernelModuleModemManager::updateWanIP()
  740. {
  741. char CMD[128];
  742. //uint32_t smsID = 0;
  743. bool bDone = false;
  744. sprintf(CMD, "ifconfig");
  745. FILE * fd = popen(CMD, "r");
  746. if (fd)
  747. {
  748. CSTR tB = NULL;
  749. size_t bSize = 0;
  750. bool bIfMeet = false;
  751. while (! feof(fd))
  752. {
  753. ssize_t rv = getline(&tB, &bSize, fd);
  754. if (rv)
  755. {
  756. if (! bDone)
  757. {
  758. if (bIfMeet)
  759. {
  760. CSTR ssp = strstr(tB, "inet ");
  761. if (ssp)
  762. {
  763. CSTR nsp = strstr(tB, "netmask");
  764. int msl = nsp - ssp - 7;
  765. strncpy(m_cMyWanIP, ssp + 5, msl);
  766. m_cMyWanIP[msl] = (char) 0;
  767. bDone = true;
  768. };
  769. }
  770. else
  771. {
  772. if (strstr(tB, "wwan"))
  773. {
  774. bIfMeet = true;
  775. }
  776. if (strstr(tB, "wwx"))
  777. {
  778. bIfMeet = true;
  779. }
  780. };
  781. };
  782. };
  783. if (tB)
  784. {
  785. free(tB);
  786. tB = NULL;
  787. };
  788. };
  789. pclose(fd);
  790. };
  791. #ifdef DEBUG_EXTRA
  792. if (bDone)
  793. {
  794. sprintf(CMD, "WAN IP4: %s", m_cMyWanIP);
  795. LOG(CMD);
  796. }
  797. else
  798. {
  799. LOG_ERROR("Cannot Find WAN IP4 Address!");
  800. };
  801. #endif
  802. return bDone;
  803. }
  804. #undef SLOG
  805. #undef LOG
  806. #undef LOG_ERROR