Module_4g.c 39 KB

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