Module_4g.c 44 KB

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