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.

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