Module_4g.c 37 KB

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