Module_4g.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342
  1. /*
  2. * Module_4g.c
  3. *
  4. * Created on: 2019年11月29日
  5. * Author: Eason Yang
  6. */
  7. #include <sys/types.h>
  8. #include <sys/stat.h>
  9. #include <sys/time.h>
  10. #include <sys/timeb.h>
  11. #include <sys/ipc.h>
  12. #include <sys/shm.h>
  13. #include <sys/mman.h>
  14. #include <unistd.h>
  15. #include <stdarg.h>
  16. #include <stdio.h> /*標準輸入輸出定義*/
  17. #include <stdlib.h> /*標準函數庫定義*/
  18. #include <unistd.h> /*Unix 標準函數定義*/
  19. #include <fcntl.h> /*檔控制定義*/
  20. #include <termios.h> /*PPSIX 終端控制定義*/
  21. #include <errno.h> /*錯誤號定義*/
  22. #include <errno.h>
  23. #include <string.h>
  24. #include <time.h>
  25. #include <ctype.h>
  26. #include "define.h"
  27. #define DEBUG_INFO(format, args...) StoreLogMsg("[%s:%d][%s][Info] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
  28. #define DEBUG_WARN(format, args...) StoreLogMsg("[%s:%d][%s][Warn] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
  29. #define DEBUG_ERROR(format, args...) StoreLogMsg("[%s:%d][%s][Error] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
  30. #define ARRAY_SIZE(A) (sizeof(A) / sizeof(A[0]))
  31. #define PASS 1
  32. #define FAIL -1
  33. #define DONGLE_QUECTEL 1
  34. #define DONGLE_UBLOX 2
  35. // Define Module mode
  36. #define NO_SERVICE 0
  37. #define CDMA 1
  38. #define GMS_GPRS 2
  39. #define WCDMA 3
  40. #define GMS_WCDMA 4
  41. #define TD_SCDMA 5
  42. #define UNKNOW 6
  43. // Define interval
  44. #define SystemInterval 30 // Seconds
  45. #define CheckModemInterval 30 // Seconds
  46. #define CheckSimInterval 30 // Seconds
  47. #define CheckModemInfoInterval 30 // Seconds
  48. #define CheckConnectionInterval 30 // Seconds
  49. #define CheckInternetInterval 30 // Seconds
  50. #define DisconnInterval 60 // Seconds
  51. int Check4GModem(void);
  52. int isPppUp(void);
  53. int isReadInfo(void);
  54. int isReadSimInfo(void);
  55. int isReachableInternet(void);
  56. int isModuleUnbind(void);
  57. int isModuleBind(void);
  58. int rstModule(void);
  59. int Load4gConfiguration(void);
  60. int CheckSignalRssi(void);
  61. int at_command(int uart, char* cmd, char* rx);
  62. int openPort(char *tty);
  63. int set_interface_attribs (int fd, int speed, int parity);
  64. int set_blocking (int fd, int should_block);
  65. void trim_s(char *s, unsigned char len);
  66. void substr(char *dest, const char* src, unsigned int start, unsigned int cnt);
  67. char *portName[3] = {"/dev/ttyUSB2", "/dev/ttyUSB2", "/dev/ttyACM2"};
  68. char *valid_Internet[2] = {"8.8.8.8", "www.baidu.com"};
  69. pid_t pid;
  70. struct dongle_info
  71. {
  72. int Model;
  73. char ICCID[20];
  74. char IMSI[16];
  75. char IMEI[16];
  76. char MANUFACTURER[8];
  77. char MODELNAME[10];
  78. char REVISION[18];
  79. unsigned char MODE;
  80. int CSQ;
  81. int cnt_InternetFail;
  82. int cnt_ReadInfoFail;
  83. int cnt_pppFail;
  84. int cnt_SearchModuleFail;
  85. int cnt_ReadSimInfoFail;
  86. }Dongle;
  87. struct SysConfigAndInfo *ShmSysConfigAndInfo;
  88. struct StatusCodeData *ShmStatusCodeData;
  89. struct FanModuleData *ShmFanModuleData;
  90. int StoreLogMsg(const char *fmt, ...)
  91. {
  92. char Buf[4096+256];
  93. char buffer[4096];
  94. time_t CurrentTime;
  95. struct tm *tm;
  96. va_list args;
  97. va_start(args, fmt);
  98. int rc = vsnprintf(buffer, sizeof(buffer), fmt, args);
  99. va_end(args);
  100. memset(Buf,0,sizeof(Buf));
  101. CurrentTime = time(NULL);
  102. tm=localtime(&CurrentTime);
  103. sprintf(Buf,"echo \"[%04d.%02d.%02d %02d:%02d:%02d] - %s\" >> /Storage/SystemLog/[%04d.%02d]4g_SystemLog",
  104. tm->tm_year+1900,tm->tm_mon+1,tm->tm_mday,tm->tm_hour,tm->tm_min,tm->tm_sec,
  105. buffer,
  106. tm->tm_year+1900,tm->tm_mon+1);
  107. #ifdef SystemLogMessage
  108. system(Buf);
  109. #endif
  110. #ifdef ConsloePrintLog
  111. printf("[%04d.%02d.%02d %02d:%02d:%02d] - %s", tm->tm_year+1900,tm->tm_mon+1,tm->tm_mday,tm->tm_hour,tm->tm_min,tm->tm_sec, buffer);
  112. #endif
  113. return rc;
  114. }
  115. int DiffTimeb(struct timeb ST, struct timeb ET)
  116. {
  117. //return milli-second
  118. unsigned int StartTime,StopTime;
  119. StartTime=(unsigned int)ST.time;
  120. StopTime=(unsigned int)ET.time;
  121. return (StopTime-StartTime)*1000+ET.millitm-ST.millitm;
  122. }
  123. //==========================================
  124. // Init all share memory
  125. //==========================================
  126. int InitShareMemory()
  127. {
  128. int result = PASS;
  129. int MeterSMId;
  130. //creat ShmSysConfigAndInfo
  131. if ((MeterSMId = shmget(ShmSysConfigAndInfoKey, sizeof(struct SysConfigAndInfo), 0777)) < 0)
  132. {
  133. #ifdef SystemLogMessage
  134. DEBUG_ERROR("shmget ShmSysConfigAndInfo NG\n");
  135. #endif
  136. result = FAIL;
  137. }
  138. else if ((ShmSysConfigAndInfo = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  139. {
  140. #ifdef SystemLogMessage
  141. DEBUG_ERROR("shmat ShmSysConfigAndInfo NG\n");
  142. #endif
  143. result = FAIL;
  144. }
  145. else
  146. {}
  147. //creat ShmStatusCodeData
  148. if ((MeterSMId = shmget(ShmStatusCodeKey, sizeof(struct StatusCodeData), 0777)) < 0)
  149. {
  150. #ifdef SystemLogMessage
  151. DEBUG_ERROR("shmget ShmStatusCodeData NG\n");
  152. #endif
  153. result = FAIL;
  154. }
  155. else if ((ShmStatusCodeData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  156. {
  157. #ifdef SystemLogMessage
  158. DEBUG_ERROR("shmat ShmStatusCodeData NG\n");
  159. #endif
  160. result = FAIL;
  161. }
  162. else
  163. {}
  164. return result;
  165. }
  166. //==========================================
  167. // Check dongle model
  168. //==========================================
  169. int Check4GModem(void)
  170. {
  171. int result = FAIL;
  172. if((access("/dev/ttyUSB0", F_OK) != -1) &&
  173. (access("/dev/ttyUSB1", F_OK) != -1) &&
  174. (access("/dev/ttyUSB2", F_OK) != -1) &&
  175. (access("/dev/ttyUSB3", F_OK) != -1))
  176. {
  177. result = DONGLE_QUECTEL;
  178. }
  179. else if((access("/dev/ttyACM0", F_OK) != -1) &&
  180. (access("/dev/ttyACM1", F_OK) != -1) &&
  181. (access("/dev/ttyACM2", F_OK) != -1) &&
  182. (access("/dev/ttyACM3", F_OK) != -1) &&
  183. (access("/dev/ttyACM4", F_OK) != -1) &&
  184. (access("/dev/ttyACM5", F_OK) != -1))
  185. {
  186. result = DONGLE_UBLOX;
  187. }
  188. else
  189. {}
  190. if(result == DONGLE_QUECTEL)
  191. {
  192. #ifdef SystemLogMessage
  193. DEBUG_WARN("Quectel 4G modem be found\n");
  194. #endif
  195. }
  196. else if(result == DONGLE_UBLOX)
  197. {
  198. #ifdef SystemLogMessage
  199. DEBUG_WARN("Ublox 4G modem be found\n");
  200. #endif
  201. }
  202. else
  203. {
  204. #ifdef SystemLogMessage
  205. DEBUG_WARN("No 4G modem be found\n");
  206. #endif
  207. }
  208. return result;
  209. }
  210. //==========================================
  211. // Check ppp interface status
  212. //==========================================
  213. int isPppUp(void)
  214. {
  215. int result = FAIL;
  216. FILE *fp;
  217. char cmd[256];
  218. char buf[512];
  219. char tmp[512];
  220. strcpy(cmd, "ifconfig");;
  221. fp = popen(cmd, "r");
  222. if(fp != NULL)
  223. {
  224. while(fgets(buf, sizeof(buf), fp) != NULL)
  225. {
  226. if(strstr(buf, "ppp") > 0)
  227. {
  228. result = PASS;
  229. }
  230. if(strstr(buf, "addr:") > 0)
  231. {
  232. sscanf(buf, "%*s%s", tmp);
  233. substr((char*)ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomIpAddress, tmp, strspn(tmp, "addr:"), strlen(buf)-strspn(tmp, "addr:"));
  234. }
  235. }
  236. }
  237. pclose(fp);
  238. return result;
  239. }
  240. //==========================================
  241. // Check dongle info read status
  242. //==========================================
  243. int isReadInfo(void)
  244. {
  245. int result = PASS;
  246. int uart;
  247. char rx[512];
  248. char tmp[512];
  249. char Lenght;
  250. int modeStatus;
  251. if((uart = openPort(portName[Dongle.Model])) != FAIL)
  252. {
  253. switch(Dongle.Model)
  254. {
  255. case DONGLE_QUECTEL:
  256. default:
  257. //==============================
  258. // Set don't echo command
  259. //==============================
  260. if(at_command(uart, "ate0\r", rx) <= 0)
  261. result = FAIL;
  262. //==============================
  263. // Read Manufacturer
  264. //==============================
  265. Lenght = at_command(uart, "at+gmi\r", rx);
  266. if(Lenght > 0)
  267. {
  268. memset(tmp, 0, sizeof tmp);
  269. memcpy(tmp, rx, strcspn(rx,"OK"));
  270. memset(rx, 0, sizeof rx);
  271. trim_s(tmp,Lenght);
  272. strcpy(Dongle.MANUFACTURER, tmp);
  273. }
  274. else
  275. result = FAIL;
  276. //==============================
  277. // Read Model
  278. //==============================
  279. Lenght = at_command(uart, "at+gmm\r", rx);
  280. if(Lenght > 0)
  281. {
  282. memset(tmp, 0, sizeof tmp);
  283. memcpy(tmp, rx, strcspn(rx,"OK"));
  284. memset(rx, 0, sizeof rx);
  285. trim_s(tmp,Lenght);
  286. strncpy(Dongle.MODELNAME, tmp, strlen(tmp));
  287. }
  288. else
  289. result = FAIL;
  290. //==============================
  291. // Read Revision
  292. //==============================
  293. Lenght = at_command(uart, "at+gmr\r", rx);
  294. if(Lenght > 0)
  295. {
  296. memset(tmp, 0, sizeof tmp);
  297. memcpy(tmp, rx, strcspn(rx, "OK"));
  298. memset(rx, 0, sizeof rx);
  299. trim_s(tmp,Lenght);
  300. strncpy(Dongle.REVISION, tmp, strlen(tmp));
  301. }
  302. else
  303. result = FAIL;
  304. //==============================
  305. // Read IMEI
  306. //==============================
  307. Lenght = at_command(uart, "at+gsn\r", rx);
  308. if(Lenght > 0)
  309. {
  310. memset(tmp, 0, sizeof tmp);
  311. memcpy(tmp, rx, strcspn(rx, "OK"));
  312. memset(rx, 0, sizeof rx);
  313. trim_s(tmp,Lenght);
  314. strncpy(Dongle.IMEI, tmp, strlen(tmp));
  315. }
  316. else
  317. result = FAIL;
  318. //==============================
  319. // Read CSQ
  320. //==============================
  321. Lenght = at_command(uart, "at+csq\r", rx);
  322. if(Lenght > 0)
  323. {
  324. memset(tmp, 0, sizeof tmp);
  325. memcpy(tmp, rx, strcspn(rx,","));
  326. strncpy(rx, tmp + strcspn(tmp,":")+1, 10);
  327. Dongle.CSQ = atoi(rx);
  328. }
  329. else
  330. result = FAIL;
  331. //==============================
  332. // Read Mode
  333. //==============================
  334. Lenght = at_command(uart, "at+qcfg= \"nwscanmode\"\r", rx);
  335. if(Lenght > 0)
  336. {
  337. memset(tmp, 0, sizeof tmp);
  338. memcpy(tmp, rx, strcspn(rx,"OK"));
  339. strncpy(rx, tmp + strcspn(tmp,",")+1, 1);
  340. modeStatus = atoi(rx);
  341. if(modeStatus == 0)
  342. Dongle.MODE = UNKNOW;
  343. else if(modeStatus == 1)
  344. Dongle.MODE = GMS_GPRS;
  345. else if(modeStatus == 2)
  346. Dongle.MODE = WCDMA;
  347. else if(modeStatus == 3)
  348. Dongle.MODE = UNKNOW;
  349. else if(modeStatus == 4)
  350. Dongle.MODE = TD_SCDMA;
  351. else if(modeStatus == 5)
  352. Dongle.MODE = UNKNOW;
  353. else if(modeStatus == 6)
  354. Dongle.MODE = CDMA;
  355. else
  356. Dongle.MODE = UNKNOW;
  357. }
  358. else
  359. result = FAIL;
  360. break;
  361. case DONGLE_UBLOX:
  362. //==============================
  363. // Set don't echo command
  364. //==============================
  365. if(at_command(uart, "ate0\r", rx) <= 0)
  366. result = FAIL;
  367. //==============================
  368. // Read Manufacturer
  369. //==============================
  370. Lenght = at_command(uart, "at+cgmi\r", rx);
  371. if(Lenght > 0)
  372. {
  373. memset(tmp, 0, sizeof tmp);
  374. memcpy(tmp, rx, strcspn(rx,"OK"));
  375. memset(rx, 0, sizeof rx);
  376. strncpy(Dongle.MANUFACTURER, tmp+2, 6);
  377. }
  378. else
  379. result = FAIL;
  380. //==============================
  381. // Read Model
  382. //==============================
  383. Lenght = at_command(uart, "at+cgmm\r", rx);
  384. if(Lenght > 0)
  385. {
  386. memset(tmp, 0, sizeof tmp);
  387. memcpy(tmp, rx, strcspn(rx,"OK"));
  388. memset(rx, 0, sizeof rx);
  389. strncpy(Dongle.MODELNAME , tmp+2, 9);
  390. }
  391. else
  392. result = FAIL;
  393. //==============================
  394. // Read Revision
  395. //==============================
  396. Lenght = at_command(uart, "at+cgmr\r", rx);
  397. if(Lenght > 0)
  398. {
  399. memset(tmp, 0, sizeof tmp);
  400. memcpy(tmp, rx, strcspn(rx, "OK"));
  401. memset(rx, 0, sizeof rx);
  402. strncpy(Dongle.REVISION, tmp+2, 5);
  403. }
  404. else
  405. result = FAIL;
  406. //==============================
  407. // Read IMEI
  408. //==============================
  409. Lenght = at_command(uart, "at+cgsn\r", rx);
  410. if(Lenght > 0)
  411. {
  412. memset(tmp, 0, sizeof tmp);
  413. memcpy(tmp, rx, strcspn(rx, "OK"));
  414. memset(rx, 0, sizeof rx);
  415. trim_s(tmp,Lenght);
  416. strncpy(Dongle.IMEI, tmp, strlen(tmp));
  417. }
  418. else
  419. result = FAIL;
  420. //==============================
  421. // Read CSQ
  422. //==============================
  423. Lenght = at_command(uart, "at+csq\r", rx);
  424. if(Lenght > 0)
  425. {
  426. memset(tmp, 0, sizeof tmp);
  427. memcpy(tmp, rx, strcspn(rx,","));
  428. strncpy(rx, tmp + strcspn(tmp,":")+1, 10);
  429. Dongle.CSQ = atoi(rx);
  430. }
  431. else
  432. result = FAIL;
  433. break;
  434. }
  435. }
  436. else
  437. {
  438. #ifdef SystemLogMessage
  439. DEBUG_ERROR("%s open fail.\n", portName[Dongle.Model]);
  440. #endif
  441. result = FAIL;
  442. }
  443. close(uart);
  444. return result;
  445. }
  446. //==========================================
  447. // Read sim card information
  448. //==========================================
  449. int isReadSimInfo(void)
  450. {
  451. int result = PASS;
  452. int uart;
  453. char rx[512];
  454. char tmp[512];
  455. char Lenght;
  456. if((uart = openPort(portName[Dongle.Model])) != FAIL)
  457. {
  458. switch(Dongle.Model)
  459. {
  460. case DONGLE_QUECTEL:
  461. default:
  462. //==============================
  463. // Set don't echo command
  464. //==============================
  465. if(at_command(uart, "ate0\r", rx) <= 0)
  466. result = FAIL;
  467. //==============================
  468. // Read IMSI
  469. //==============================
  470. Lenght = at_command(uart, "at+cimi\r", rx);
  471. if(Lenght > 0)
  472. {
  473. memset(tmp, 0, sizeof tmp);
  474. if (strstr(rx, "ERROR"))
  475. {
  476. memset(Dongle.IMSI, 0, sizeof Dongle.IMSI);
  477. result = FAIL;
  478. }
  479. else
  480. {
  481. memcpy(tmp, rx, strcspn(rx, "OK"));
  482. trim_s(tmp, Lenght);
  483. memset(rx, 0, sizeof rx);
  484. strncpy(Dongle.IMSI, tmp, strlen(tmp));
  485. }
  486. }
  487. else
  488. result = FAIL;
  489. //==============================
  490. // Read CCID
  491. //==============================
  492. Lenght = at_command(uart, "at+qccid\r", rx);
  493. if(Lenght > 0)
  494. {
  495. memset(tmp, 0, sizeof tmp);
  496. if (strstr(rx, "ERROR"))
  497. {
  498. memset(Dongle.ICCID, 0, sizeof Dongle.ICCID);
  499. result = FAIL;
  500. }
  501. else
  502. {
  503. memcpy(tmp, rx, strcspn(rx, "OK"));
  504. memset(rx, 0, sizeof rx);
  505. strncpy(Dongle.ICCID, tmp + strcspn(tmp, ":") + 2, 20);
  506. }
  507. }
  508. else
  509. result = FAIL;
  510. break;
  511. case DONGLE_UBLOX:
  512. //==============================
  513. // Set don't echo command
  514. //==============================
  515. if (at_command(uart, "ate0\r", rx) <= 0)
  516. result = FAIL;
  517. //==============================
  518. // Read IMSI
  519. //==============================
  520. Lenght = at_command(uart, "at+cimi\r", rx);
  521. if(Lenght > 0)
  522. {
  523. memset(tmp, 0, sizeof tmp);
  524. if(strstr(rx, "ERROR"))
  525. {
  526. memset(Dongle.IMSI, 0, sizeof Dongle.IMSI);
  527. result = FAIL;
  528. }
  529. else
  530. {
  531. memcpy(tmp, rx, strcspn(rx,"OK"));
  532. memset(rx, 0, sizeof rx);
  533. trim_s(tmp,Lenght);
  534. strncpy(Dongle.IMSI , tmp, strlen(tmp));
  535. }
  536. }
  537. else
  538. result = FAIL;
  539. //==============================
  540. // Read CCID
  541. //==============================
  542. Lenght = at_command(uart, "at+ccid\r", rx);
  543. if(Lenght > 0)
  544. {
  545. memset(tmp, 0, sizeof tmp);
  546. if (strstr(rx, "ERROR"))
  547. {
  548. memset(Dongle.ICCID, 0, sizeof Dongle.ICCID);
  549. result = FAIL;
  550. }
  551. else
  552. {
  553. memcpy(tmp, rx, strcspn(rx, "OK"));
  554. memset(rx, 0, sizeof rx);
  555. strncpy(Dongle.ICCID, tmp + strcspn(tmp, ":") + 2, 20);
  556. }
  557. }
  558. else
  559. result = FAIL;
  560. break;
  561. }
  562. }
  563. else
  564. {
  565. #ifdef SystemLogMessage
  566. DEBUG_ERROR("%s open fail.\n", portName[Dongle.Model]);
  567. #endif
  568. result = FAIL;
  569. }
  570. close(uart);
  571. return result;
  572. }
  573. //==========================================
  574. // Read signal information
  575. //==========================================
  576. int CheckSignalRssi(void)
  577. {
  578. int result = PASS;
  579. int uart;
  580. char rx[512];
  581. char tmp[512];
  582. char Lenght;
  583. if((uart = openPort(portName[Dongle.Model])) != FAIL)
  584. {
  585. switch (Dongle.Model)
  586. {
  587. case DONGLE_UBLOX:
  588. //==============================
  589. // Set don't echo command
  590. //==============================
  591. if (at_command(uart, "ate0\r", rx) <= 0)
  592. result = FAIL;
  593. //==============================
  594. // Read CSQ
  595. //==============================
  596. Lenght = at_command(uart, "at+csq\r", rx);
  597. if (Lenght > 0)
  598. {
  599. memset(tmp, 0, sizeof tmp);
  600. memcpy(tmp, rx, strcspn(rx, ","));
  601. strncpy(rx, tmp + strcspn(tmp, ":") + 1, 10);
  602. Dongle.CSQ = atoi(rx);
  603. }
  604. else
  605. result = FAIL;
  606. break;
  607. case DONGLE_QUECTEL:
  608. default:
  609. //==============================
  610. // Set don't echo command
  611. //==============================
  612. if (at_command(uart, "ate0\r", rx) <= 0)
  613. result = FAIL;
  614. //==============================
  615. // Read CSQ
  616. //==============================
  617. Lenght = at_command(uart, "at+csq\r", rx);
  618. if (Lenght > 0)
  619. {
  620. memset(tmp, 0, sizeof tmp);
  621. memcpy(tmp, rx, strcspn(rx, ","));
  622. strncpy(rx, tmp + strcspn(tmp, ":") + 1, 10);
  623. Dongle.CSQ = atoi(rx);
  624. }
  625. else
  626. result = FAIL;
  627. break;
  628. }
  629. }
  630. else
  631. {
  632. #ifdef SystemLogMessage
  633. DEBUG_ERROR("%s open fail.\n", portName[Dongle.Model]);
  634. #endif
  635. result = FAIL;
  636. }
  637. close(uart);
  638. return result;
  639. }
  640. //==========================================
  641. // Init 4G dongle configuration
  642. //==========================================
  643. int Load4gConfiguration()
  644. {
  645. int result = FAIL;
  646. unsigned char CopyTmp[1024];
  647. if(strlen((char*)ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomApn) > 0)
  648. {
  649. system("cat /dev/null > /root/ppp/auto-apn.conf");
  650. system("chmod 600 /root/ppp/auto-apn.conf");
  651. system("cat /dev/null > /etc/ppp/pap-secrets");
  652. system("chmod 600 /etc/ppp/pap-secrets");
  653. system("cat /dev/null > /etc/ppp/chap-secrets");
  654. system("chmod 600 /etc/ppp/chap-secrets");
  655. system("cat /dev/null > /etc/ppp/auth");
  656. system("chmod 600 /etc/ppp/auth");
  657. memset(CopyTmp,0,sizeof(CopyTmp));
  658. sprintf((char*)CopyTmp,"echo APN=\"%s\" > /root/ppp/auto-apn.conf",ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomApn);
  659. system((char*)CopyTmp);
  660. memset(CopyTmp,0,sizeof(CopyTmp));
  661. sprintf((char*)CopyTmp,"echo ACCOUNT=\"%s\" >> /root/ppp/auto-apn.conf",ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomChapPapId);
  662. system((char*)CopyTmp);
  663. memset(CopyTmp,0,sizeof(CopyTmp));
  664. sprintf((char*)CopyTmp,"echo PASSWORD=\"%s\" >> /root/ppp/auto-apn.conf",ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomChapPapPwd);
  665. system((char*)CopyTmp);
  666. memset(CopyTmp,0,sizeof(CopyTmp));
  667. sprintf((char*)CopyTmp,"echo %s > /etc/ppp/auth",ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomChapPapId);
  668. system((char*)CopyTmp);
  669. sprintf((char*)CopyTmp,"echo %s >> /etc/ppp/auth",ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomChapPapPwd);
  670. system((char*)CopyTmp);
  671. if(strlen((char*)ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomChapPapId)>0)
  672. {
  673. memset(CopyTmp,0,sizeof(CopyTmp));
  674. if(strlen((char*)ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomChapPapPwd)>0)
  675. sprintf((char*)CopyTmp,"echo \"%s * %s \" > /etc/ppp/pap-secrets", ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomChapPapId, ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomChapPapPwd);
  676. else
  677. sprintf((char*)CopyTmp,"echo \"%s * \\<Your\\ Password\\> \" > /etc/ppp/pap-secrets", ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomChapPapId);
  678. system((char*)CopyTmp);
  679. memset(CopyTmp,0,sizeof(CopyTmp));
  680. if(strlen((char*)ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomChapPapPwd)>0)
  681. sprintf((char*)CopyTmp,"echo \"%s * %s \" > /etc/ppp/chap-secrets", ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomChapPapId, ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomChapPapPwd);
  682. else
  683. sprintf((char*)CopyTmp,"echo \"%s * \\<Your\\ Password\\> \" > /etc/ppp/chap-secrets", ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomChapPapId);
  684. system((char*)CopyTmp);
  685. }
  686. result = PASS;
  687. }
  688. return result;
  689. }
  690. //==========================================
  691. // Check internet access status
  692. //==========================================
  693. int isReachableInternet(void)
  694. {
  695. int result = FAIL;
  696. FILE *fp;
  697. char cmd[256];
  698. char buf[512];
  699. char tmp[512];
  700. for(int idx=0;idx<ARRAY_SIZE(valid_Internet);idx++)
  701. {
  702. strcpy(cmd, "ping -c 1 -w 3 ");
  703. strcat(cmd, valid_Internet[idx]);
  704. fp = popen(cmd, "r");
  705. if(fp != NULL)
  706. {
  707. while(fgets(buf, sizeof(buf), fp) != NULL)
  708. {
  709. if(strstr(buf, "transmitted") > 0)
  710. {
  711. sscanf(buf, "%*s%*s%*s%*s%*s%s", tmp);
  712. if(strcmp(tmp,"100%") != 0)
  713. {
  714. result = PASS;
  715. }
  716. }
  717. }
  718. }
  719. pclose(fp);
  720. }
  721. return result;
  722. }
  723. //==========================================
  724. // Unbind USB port
  725. //==========================================
  726. int isModuleUnbind(void)
  727. {
  728. int result = FAIL;
  729. FILE *fp;
  730. char cmd[256];
  731. strcpy(cmd, "echo '1-1'> /sys/bus/usb/drivers/usb/unbind");
  732. fp = popen(cmd, "r");
  733. if(fp == NULL)
  734. {
  735. result = FAIL;
  736. }
  737. else
  738. {
  739. result = PASS;
  740. #ifdef SystemLogMessage
  741. DEBUG_INFO("Unbind USB for dongle.\n");
  742. #endif
  743. }
  744. return result;
  745. }
  746. //==========================================
  747. // Bind USB port
  748. //==========================================
  749. int isModuleBind(void)
  750. {
  751. int result = FAIL;
  752. FILE *fp;
  753. char cmd[256];
  754. strcpy(cmd, "echo '1-1'> /sys/bus/usb/drivers/usb/bind");
  755. fp = popen(cmd, "r");
  756. if(fp == NULL)
  757. {
  758. result = FAIL;
  759. }
  760. else
  761. {
  762. #ifdef SystemLogMessage
  763. DEBUG_INFO("Bind USB for dongle.\n");
  764. #endif
  765. result = PASS;
  766. }
  767. return result;
  768. }
  769. //==========================================
  770. // Dongle reset process
  771. //==========================================
  772. int rstModule(void)
  773. {
  774. int result = PASS;
  775. int uart;
  776. char rx[512];
  777. if((uart = openPort(portName[Dongle.Model])) != FAIL)
  778. {
  779. //==============================
  780. // Reset module
  781. //==============================
  782. switch(Dongle.Model)
  783. {
  784. case DONGLE_QUECTEL:
  785. default:
  786. if(at_command(uart, "at+cfun=1,1\r", rx) <= 0)
  787. {
  788. result = FAIL;
  789. }
  790. break;
  791. case DONGLE_UBLOX:
  792. if(at_command(uart, "at+cfun=1,1\r", rx) <= 0)
  793. {
  794. result = FAIL;
  795. }
  796. break;
  797. }
  798. }
  799. else
  800. {
  801. #ifdef SystemLogMessage
  802. DEBUG_ERROR("%s open fail.\n", portName[Dongle.Model]);
  803. #endif
  804. result = FAIL;
  805. }
  806. close(uart);
  807. sleep(40);
  808. return result;
  809. }
  810. //==========================================
  811. // AT command send/receive
  812. //==========================================
  813. int at_command(int uart, char* cmd, char* rx)
  814. {
  815. int len;
  816. //sleep(2); //required to make flush work, for some reason
  817. tcflush(uart,TCIOFLUSH);
  818. if(write(uart, cmd, strlen(cmd)) >= sizeof(cmd))
  819. {
  820. usleep(500000);
  821. len = read(uart, rx, 512);
  822. }
  823. else
  824. {
  825. #ifdef SystemLogMessage
  826. DEBUG_ERROR("AT command %s response fail.\n", cmd);
  827. #endif
  828. }
  829. return len;
  830. }
  831. //==========================================
  832. // Dongle communication port open
  833. //==========================================
  834. int openPort(char *tty)
  835. {
  836. int uart = open(tty, O_RDWR | O_NOCTTY | O_NDELAY);
  837. if(uart!=FAIL)
  838. {
  839. if((set_interface_attribs(uart, B115200, 0) != PASS) || (set_blocking(uart, 0) != PASS))
  840. uart = FAIL;
  841. }
  842. return uart;
  843. }
  844. //==========================================
  845. // Port parameter set
  846. //==========================================
  847. int set_interface_attribs (int fd, int speed, int parity)
  848. {
  849. int result = FAIL;
  850. struct termios tty;
  851. memset (&tty, 0, sizeof tty);
  852. if (tcgetattr (fd, &tty) == 0)
  853. {
  854. cfsetospeed (&tty, speed);
  855. cfsetispeed (&tty, speed);
  856. tty.c_cflag = (tty.c_cflag & ~CSIZE) | CS8; // 8-bit chars
  857. // disable IGNBRK for mismatched speed tests; otherwise receive break
  858. // as \000 chars
  859. tty.c_iflag &= ~IGNBRK; // disable break processing
  860. tty.c_lflag = 0; // no signaling chars, no echo,
  861. // no canonical processing
  862. tty.c_oflag = 0; // no remapping, no delays
  863. tty.c_cc[VMIN] = 0; // read doesn't block
  864. tty.c_cc[VTIME] = 10; // 1 seconds read timeout
  865. tty.c_iflag &= ~(IXON | IXOFF | IXANY); // shut off xon/xoff ctrl
  866. tty.c_cflag |= (CLOCAL | CREAD); // ignore modem controls,
  867. // enable reading
  868. tty.c_cflag &= ~(PARENB | PARODD); // shut off parity
  869. tty.c_cflag |= parity;
  870. tty.c_cflag &= ~CSTOPB;
  871. tty.c_cflag &= ~CRTSCTS;
  872. if (tcsetattr (fd, TCSANOW, &tty) == 0)
  873. result = PASS;
  874. }
  875. return result;
  876. }
  877. int set_blocking (int fd, int should_block)
  878. {
  879. int result = FAIL;
  880. struct termios tty;
  881. memset (&tty, 0, sizeof tty);
  882. if (tcgetattr (fd, &tty) == 0)
  883. {
  884. tty.c_cc[VMIN] = should_block ? 1 : 0;
  885. tty.c_cc[VTIME] = 5; // 0.5 seconds read timeout
  886. if (tcsetattr (fd, TCSANOW, &tty) == 0)
  887. result = PASS;
  888. }
  889. return result;
  890. }
  891. //==========================================
  892. // Common routine
  893. //==========================================
  894. void trim_s(char *s, unsigned char len)
  895. {
  896. for(unsigned char i = 0 ; i < len; i++)
  897. {
  898. if (!((s[i]>='a') && (s[i]<='z')) && !((s[i]>='A') && (s[i]<='Z'))&& !((s[i]>='0') && (s[i]<='9')))
  899. {
  900. s[i] = s[i + 1];
  901. strncpy(s + i, s + i + 1, len);
  902. i -= 1;
  903. len -= 1;
  904. }
  905. }
  906. s[len + 1] = '\0';
  907. }
  908. void substr(char *dest, const char* src, unsigned int start, unsigned int cnt)
  909. {
  910. strncpy(dest, src + start, cnt);
  911. dest[cnt] = 0;
  912. }
  913. //==========================================
  914. // Main process
  915. //==========================================
  916. int main(void)
  917. {
  918. //==========================================
  919. //Initialization share memory
  920. //==========================================
  921. if(InitShareMemory() == FAIL)
  922. {
  923. #ifdef SystemLogMessage
  924. DEBUG_ERROR("InitShareMemory NG\n");
  925. #endif
  926. if(ShmStatusCodeData!=NULL)
  927. {
  928. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FailToCreateShareMemory=1;
  929. }
  930. sleep(5);
  931. return 0;
  932. }
  933. else if(Load4gConfiguration() == FAIL)
  934. {
  935. DEBUG_ERROR("4G configuration value NG\n");
  936. if(ShmStatusCodeData!=NULL)
  937. {
  938. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.Telecom4GModuleCommFail=1;
  939. }
  940. sleep(5);
  941. return 0;
  942. }
  943. for(;;)
  944. {
  945. if(((Dongle.Model = Check4GModem()) != FAIL))
  946. {
  947. system("pkill Module_Wifi");
  948. ShmStatusCodeData->FaultCode.FaultEvents.bits.Telecom4GModuleBroken = 0;
  949. Dongle.cnt_SearchModuleFail = 0;
  950. if(isReadInfo() == PASS)
  951. {
  952. memcpy(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModelName, Dongle.MODELNAME, sizeof Dongle.MODELNAME);
  953. memcpy(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSoftwareVer, Dongle.REVISION, sizeof Dongle.REVISION);
  954. memcpy(ShmSysConfigAndInfo->SysInfo.TelcomModemFwRev, Dongle.REVISION, sizeof Dongle.REVISION);
  955. memcpy(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModemImei, Dongle.IMEI, sizeof Dongle.IMEI);
  956. ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomRssi = Dongle.CSQ;
  957. ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModemMode = Dongle.MODE;
  958. #ifdef SystemLogMessage
  959. DEBUG_INFO("========================================\n");
  960. DEBUG_INFO("Status: Device info readable...\n");
  961. DEBUG_INFO("Device MANUFACTURER: %s\n", Dongle.MANUFACTURER);
  962. DEBUG_INFO("Device MODEL: %s\n", ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModelName);
  963. DEBUG_INFO("Device REVISION: %s\n", ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSoftwareVer);
  964. DEBUG_INFO("Device IMEI: %s\n", ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModemImei);
  965. DEBUG_INFO("Device RSSI: %d\n", ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomRssi);
  966. DEBUG_INFO("Device MODE: %d\n", ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModemMode);
  967. DEBUG_INFO("========================================\n");
  968. #endif
  969. Dongle.cnt_ReadInfoFail = 0;
  970. if(isReadSimInfo() == PASS)
  971. {
  972. ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSimStatus = 1;
  973. memcpy(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSimIccid, Dongle.ICCID, sizeof Dongle.ICCID);
  974. memcpy(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSimImsi, Dongle.IMSI, sizeof Dongle.IMSI);
  975. #ifdef SystemLogMessage
  976. DEBUG_INFO("========================================\n");
  977. DEBUG_INFO("Status: SIM card info readable...\n");
  978. DEBUG_INFO("Device IMSI: %s\n", ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSimImsi);
  979. DEBUG_INFO("Device ICCID: %.20s\n", ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSimIccid);
  980. DEBUG_INFO("TelcomSimStatus: %d\n", ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSimStatus);
  981. DEBUG_INFO("========================================\n");
  982. #endif
  983. Dongle.cnt_ReadSimInfoFail = 0;
  984. if(isPppUp() == PASS)
  985. {
  986. #ifdef SystemLogMessage
  987. DEBUG_INFO("PPP IP: %s\n", ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomIpAddress);
  988. #endif
  989. Dongle.cnt_pppFail = 0;
  990. do
  991. {
  992. if(isReachableInternet() == PASS)
  993. {
  994. ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomNetworkConn = 1;
  995. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.Telecom4GModuleCommFail = 0;
  996. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectVia4Gi = 0;
  997. ShmStatusCodeData->InfoCode.InfoEvents.bits.ApnDisconnectVia4Gi = 0;
  998. if(CheckSignalRssi() != PASS)
  999. {
  1000. #ifdef SystemLogMessage
  1001. DEBUG_INFO("No RSSI\n");
  1002. #endif
  1003. }
  1004. else
  1005. {
  1006. ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomRssi = Dongle.CSQ;
  1007. }
  1008. #ifdef SystemLogMessage
  1009. DEBUG_INFO("================================\n");
  1010. DEBUG_INFO("Status: 4G Device connecting.\n");
  1011. DEBUG_INFO("================================\n");
  1012. DEBUG_INFO("Device MANUFACTURER: %s\n", Dongle.MANUFACTURER);
  1013. DEBUG_INFO("Device MODEL: %s\n", ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModelName);
  1014. DEBUG_INFO("Device REVISION: %s\n", ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSoftwareVer);
  1015. DEBUG_INFO("Device IMEI: %s\n", ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModemImei);
  1016. DEBUG_INFO("Device IMSI: %s\n", ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSimImsi);
  1017. DEBUG_INFO("Device RSSI: %d\n", ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomRssi);
  1018. DEBUG_INFO("Device ICCID: %.20s\n", ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSimIccid);
  1019. DEBUG_INFO("Device MODE: %d\n", ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModemMode);
  1020. DEBUG_INFO("Network connection: %d\n", ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomNetworkConn);
  1021. DEBUG_INFO("================================\n");
  1022. #endif
  1023. #ifdef SystemLogMessage
  1024. DEBUG_INFO("Dongle internet valid result: Pass\n");
  1025. #endif
  1026. Dongle.cnt_InternetFail = 0;
  1027. sleep(CheckInternetInterval);
  1028. }
  1029. else
  1030. {
  1031. #ifdef SystemLogMessage
  1032. DEBUG_INFO("Dongle internet valid result: Fail %d time\n", Dongle.cnt_InternetFail);
  1033. #endif
  1034. Dongle.cnt_InternetFail++;
  1035. sleep(DisconnInterval);
  1036. }
  1037. }while(Dongle.cnt_InternetFail < 3);
  1038. ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomNetworkConn = 0;
  1039. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectVia4Gi = 1;
  1040. ShmStatusCodeData->InfoCode.InfoEvents.bits.ApnDisconnectVia4Gi = 1;
  1041. }
  1042. else
  1043. {
  1044. #ifdef SystemLogMessage
  1045. DEBUG_WARN("PPP interface not found.\n");
  1046. #endif
  1047. memset(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomIpAddress, 0 , sizeof ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomIpAddress);
  1048. Dongle.cnt_pppFail++;
  1049. if(Dongle.cnt_pppFail > 5)
  1050. {
  1051. #ifdef SystemLogMessage
  1052. DEBUG_INFO("Dongle hardware reset...\n");
  1053. #endif
  1054. rstModule();
  1055. }
  1056. system("killall 4GDetection");
  1057. sleep(2);
  1058. if(Dongle.Model == DONGLE_QUECTEL)
  1059. {
  1060. system("/root/ppp/4GDetection /dev/ttyUSB3 &");
  1061. printf("4GDetection for primary device.\n");
  1062. }
  1063. else if(Dongle.Model == DONGLE_UBLOX)
  1064. {
  1065. system("/root/ppp/4GDetection /dev/ttyACM0 &");
  1066. printf("4GDetection for second device.\n");
  1067. }
  1068. else
  1069. {}
  1070. sleep(CheckConnectionInterval);
  1071. }
  1072. }
  1073. else
  1074. {
  1075. #ifdef SystemLogMessage
  1076. DEBUG_ERROR("SIM card info read error fail: %d\n", Dongle.cnt_ReadSimInfoFail);
  1077. #endif
  1078. ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSimStatus = 0;
  1079. memset(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSimImsi, 0, sizeof ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSimImsi);
  1080. memset(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSimIccid, 0, sizeof ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSimIccid);
  1081. memset(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomIpAddress, 0 , sizeof ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomIpAddress);
  1082. #ifdef SystemLogMessage
  1083. DEBUG_INFO("========================================\n");
  1084. DEBUG_INFO("Status: Read Sim card info fail...\n");
  1085. DEBUG_INFO("Device ICCID: %.20s\n", ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSimIccid);
  1086. DEBUG_INFO("Device IMSI: %s\n", ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSimImsi);
  1087. DEBUG_INFO("========================================\n");
  1088. #endif
  1089. Dongle.cnt_ReadSimInfoFail ++;
  1090. if(Dongle.cnt_ReadSimInfoFail > 3)
  1091. {
  1092. #ifdef SystemLogMessage
  1093. DEBUG_INFO("Dongle hardware reset...\n");
  1094. #endif
  1095. Dongle.cnt_ReadSimInfoFail = 0;
  1096. rstModule();
  1097. }
  1098. sleep(CheckSimInterval);
  1099. }
  1100. }
  1101. else
  1102. {
  1103. #ifdef SystemLogMessage
  1104. DEBUG_ERROR("Device info read error fail: %d\n", Dongle.cnt_ReadInfoFail);
  1105. #endif
  1106. Dongle.MODE = NO_SERVICE;
  1107. memset(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModelName, 0, sizeof ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModelName);
  1108. memset(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSoftwareVer, 0, sizeof ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSoftwareVer);
  1109. memset(ShmSysConfigAndInfo->SysInfo.TelcomModemFwRev, 0, sizeof ShmSysConfigAndInfo->SysInfo.TelcomModemFwRev);
  1110. memset(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModemImei, 0, sizeof ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModemImei);
  1111. ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomRssi = 0;
  1112. ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModemMode = 0;
  1113. #ifdef SystemLogMessage
  1114. DEBUG_INFO("========================================\n");
  1115. DEBUG_INFO("Status: Read device info fail...\n");
  1116. DEBUG_INFO("Device MANUFACTURER: %s\n", Dongle.MANUFACTURER);
  1117. DEBUG_INFO("Device MODEL: %s\n", ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModelName);
  1118. DEBUG_INFO("Device REVISION: %s\n", ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSoftwareVer);
  1119. DEBUG_INFO("Device IMEI: %s\n", ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModemImei);
  1120. DEBUG_INFO("Device RSSI: %d\n", ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomRssi);
  1121. DEBUG_INFO("Device MODE: %d\n", ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModemMode);
  1122. DEBUG_INFO("========================================\n");
  1123. #endif
  1124. Dongle.cnt_ReadInfoFail++;
  1125. if(Dongle.cnt_ReadInfoFail > 3)
  1126. {
  1127. #ifdef SystemLogMessage
  1128. DEBUG_INFO("Device hardware reset...\n");
  1129. #endif
  1130. Dongle.cnt_ReadInfoFail = 0;
  1131. rstModule();
  1132. }
  1133. sleep(CheckModemInfoInterval);
  1134. }
  1135. }
  1136. else
  1137. {
  1138. //==========================================
  1139. // Module valid fail process
  1140. //==========================================
  1141. #ifdef SystemLogMessage
  1142. DEBUG_ERROR("Device search error fail: %d\n", Dongle.cnt_SearchModuleFail);
  1143. #endif
  1144. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectVia4Gi = 1;
  1145. ShmStatusCodeData->InfoCode.InfoEvents.bits.ApnDisconnectVia4Gi = 1;
  1146. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.Telecom4GModuleCommFail = 0;
  1147. ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomNetworkConn = 0;
  1148. ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomRssi = 0;
  1149. ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSimStatus = 0;
  1150. ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModemMode = 0;
  1151. memset(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModemImei, 0, sizeof ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModemImei);
  1152. memset(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSimImsi, 0, sizeof ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSimImsi);
  1153. memset(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomIpAddress, 0, sizeof ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomIpAddress);
  1154. memset(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSimIccid, 0, sizeof ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSimIccid);
  1155. memset(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSoftwareVer, 0, sizeof ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSoftwareVer);
  1156. memset(ShmSysConfigAndInfo->SysInfo.TelcomModemFwRev, 0, sizeof ShmSysConfigAndInfo->SysInfo.TelcomModemFwRev);
  1157. memset(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModelName, 0, sizeof ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModelName);
  1158. Dongle.cnt_SearchModuleFail++;
  1159. if(Dongle.cnt_SearchModuleFail > 3)
  1160. {
  1161. #ifdef SystemLogMessage
  1162. DEBUG_ERROR("4G Module was broken.\n");
  1163. #endif
  1164. if(isModuleUnbind() == PASS)
  1165. {
  1166. isModuleBind();
  1167. }
  1168. ShmStatusCodeData->FaultCode.FaultEvents.bits.Telecom4GModuleBroken = 1;
  1169. Dongle.cnt_SearchModuleFail = 0;
  1170. }
  1171. sleep(CheckModemInterval);
  1172. }
  1173. //sleep(SystemInterval);
  1174. }
  1175. return 0;
  1176. }