Module_4g.c 42 KB

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