Module_4g.c 36 KB

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