Module_4g.c 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513
  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.09","2021-03-11"};
  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_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. 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[256];
  800. char isPppGetGw = FALSE;
  801. for(int idx=0;idx<ARRAY_SIZE(valid_Internet);idx++)
  802. {
  803. sprintf(cmd, "ping -c 1 -w 3 -I ppp0 %s", valid_Internet[idx]);
  804. fp = popen(cmd, "r");
  805. if(fp != NULL)
  806. {
  807. while(fgets(buf, sizeof(buf), fp) != NULL)
  808. {
  809. if(strstr(buf, "transmitted") > 0)
  810. {
  811. //sscanf(buf, "%*s%*s%*s%*s%*s%s", tmp);
  812. if(strstr(buf,"100%") != NULL)
  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. // Check ppp0 gateway
  826. if(result == TRUE)
  827. {
  828. fp = popen("ip route", "r");
  829. if(fp == NULL)
  830. result = FAIL;
  831. else
  832. {
  833. while(fgets(buf, sizeof(buf), fp) != NULL)
  834. {
  835. if((strstr(buf, "default") != NULL) && (strstr(buf, "ppp0") != NULL))
  836. {
  837. isPppGetGw = TRUE;
  838. break;
  839. }
  840. if(strstr(buf, "ppp0") != NULL)
  841. {
  842. break;
  843. }
  844. }
  845. if((isPppGetGw == FALSE) && (strstr(buf, "ppp0") != NULL))
  846. {
  847. sscanf(buf, "%s", tmp);
  848. DEBUG_INFO("ppp0 does not get default route gateway force assign to %s\n", tmp);
  849. sprintf(cmd,"route add default gw %s ppp0 &", tmp);
  850. system(cmd);
  851. }
  852. }
  853. pclose(fp);
  854. memset(buf, 0x00, sizeof(buf));
  855. }
  856. return result;
  857. }
  858. //==========================================
  859. // Unbind USB port
  860. //==========================================
  861. int isModuleUnbind(void)
  862. {
  863. int result = FAIL;
  864. FILE *fp;
  865. char cmd[256];
  866. strcpy(cmd, "echo '1-1'> /sys/bus/usb/drivers/usb/unbind");
  867. fp = popen(cmd, "r");
  868. if(fp == NULL)
  869. {
  870. result = FAIL;
  871. }
  872. else
  873. {
  874. result = PASS;
  875. #ifdef SystemLogMessage
  876. DEBUG_INFO("Unbind USB for dongle.\n");
  877. #endif
  878. }
  879. return result;
  880. }
  881. //==========================================
  882. // Bind USB port
  883. //==========================================
  884. int isModuleBind(void)
  885. {
  886. int result = FAIL;
  887. FILE *fp;
  888. char cmd[256];
  889. strcpy(cmd, "echo '1-1'> /sys/bus/usb/drivers/usb/bind");
  890. fp = popen(cmd, "r");
  891. if(fp == NULL)
  892. {
  893. result = FAIL;
  894. }
  895. else
  896. {
  897. #ifdef SystemLogMessage
  898. DEBUG_INFO("Bind USB for dongle.\n");
  899. #endif
  900. result = PASS;
  901. }
  902. return result;
  903. }
  904. //==========================================
  905. // Dongle reset process
  906. //==========================================
  907. int rstModule(void)
  908. {
  909. int result = PASS;
  910. int uart;
  911. char rx[512];
  912. if((uart = openPort(portName[Dongle.Model])) != FAIL)
  913. {
  914. //==============================
  915. // Reset module
  916. //==============================
  917. switch(Dongle.Model)
  918. {
  919. case DONGLE_QUECTEL:
  920. default:
  921. if(at_command(uart, "at+cfun=1,1\r", rx) <= 0)
  922. {
  923. result = FAIL;
  924. }
  925. DEBUG_INFO("Dongle hardware reset...\n");
  926. break;
  927. case DONGLE_UBLOX:
  928. if(at_command(uart, "at+cfun=1,1\r", rx) <= 0)
  929. {
  930. result = FAIL;
  931. }
  932. DEBUG_INFO("Dongle hardware reset...\n");
  933. break;
  934. }
  935. }
  936. else
  937. {
  938. #ifdef SystemLogMessage
  939. DEBUG_ERROR("%s open fail.\n", portName[Dongle.Model]);
  940. #endif
  941. result = FAIL;
  942. }
  943. close(uart);
  944. sleep(40);
  945. return result;
  946. }
  947. //==========================================
  948. // AT command send/receive
  949. //==========================================
  950. int at_command(int uart, char* cmd, char* rx)
  951. {
  952. int len;
  953. //sleep(2); //required to make flush work, for some reason
  954. //DEBUG_INFO("cmd : %s \n", cmd);
  955. tcflush(uart,TCIOFLUSH);
  956. if(write(uart, cmd, strlen(cmd)) >= sizeof(cmd))
  957. {
  958. usleep(500000);
  959. len = read(uart, rx, 512);
  960. //DEBUG_INFO("rx : %s \n", rx);
  961. }
  962. else
  963. {
  964. #ifdef SystemLogMessage
  965. DEBUG_ERROR("AT command %s response fail.\n", cmd);
  966. #endif
  967. }
  968. return len;
  969. }
  970. //==========================================
  971. // Dongle communication port open
  972. //==========================================
  973. int openPort(char *tty)
  974. {
  975. int uart = open(tty, O_RDWR | O_NOCTTY | O_NDELAY);
  976. if(uart!=FAIL)
  977. {
  978. if((set_interface_attribs(uart, B115200, 0) != PASS) || (set_blocking(uart, 0) != PASS))
  979. uart = FAIL;
  980. }
  981. return uart;
  982. }
  983. //==========================================
  984. // Port parameter set
  985. //==========================================
  986. int set_interface_attribs (int fd, int speed, int parity)
  987. {
  988. int result = FAIL;
  989. struct termios tty;
  990. memset (&tty, 0, sizeof tty);
  991. if (tcgetattr (fd, &tty) == 0)
  992. {
  993. cfsetospeed (&tty, speed);
  994. cfsetispeed (&tty, speed);
  995. tty.c_cflag = (tty.c_cflag & ~CSIZE) | CS8; // 8-bit chars
  996. // disable IGNBRK for mismatched speed tests; otherwise receive break
  997. // as \000 chars
  998. tty.c_iflag &= ~IGNBRK; // disable break processing
  999. tty.c_lflag = 0; // no signaling chars, no echo,
  1000. // no canonical processing
  1001. tty.c_oflag = 0; // no remapping, no delays
  1002. tty.c_cc[VMIN] = 0; // read doesn't block
  1003. tty.c_cc[VTIME] = 10; // 1 seconds read timeout
  1004. tty.c_iflag &= ~(IXON | IXOFF | IXANY); // shut off xon/xoff ctrl
  1005. tty.c_cflag |= (CLOCAL | CREAD); // ignore modem controls,
  1006. // enable reading
  1007. tty.c_cflag &= ~(PARENB | PARODD); // shut off parity
  1008. tty.c_cflag |= parity;
  1009. tty.c_cflag &= ~CSTOPB;
  1010. tty.c_cflag &= ~CRTSCTS;
  1011. if (tcsetattr (fd, TCSANOW, &tty) == 0)
  1012. result = PASS;
  1013. }
  1014. return result;
  1015. }
  1016. int set_blocking (int fd, int should_block)
  1017. {
  1018. int result = FAIL;
  1019. struct termios tty;
  1020. memset (&tty, 0, sizeof tty);
  1021. if (tcgetattr (fd, &tty) == 0)
  1022. {
  1023. tty.c_cc[VMIN] = should_block ? 1 : 0;
  1024. tty.c_cc[VTIME] = 5; // 0.5 seconds read timeout
  1025. if (tcsetattr (fd, TCSANOW, &tty) == 0)
  1026. result = PASS;
  1027. }
  1028. return result;
  1029. }
  1030. //==========================================
  1031. // Common routine
  1032. //==========================================
  1033. void trim_s(char *s, unsigned char len)
  1034. {
  1035. for(unsigned char i = 0 ; i < len; i++)
  1036. {
  1037. if (!((s[i]>='a') && (s[i]<='z')) && !((s[i]>='A') && (s[i]<='Z'))&& !((s[i]>='0') && (s[i]<='9')))
  1038. {
  1039. s[i] = s[i + 1];
  1040. strncpy(s + i, s + i + 1, len);
  1041. i -= 1;
  1042. len -= 1;
  1043. }
  1044. }
  1045. s[len + 1] = '\0';
  1046. }
  1047. void substr(char *dest, const char* src, unsigned int start, unsigned int cnt)
  1048. {
  1049. strncpy(dest, src + start, cnt);
  1050. dest[cnt] = 0;
  1051. }
  1052. //==========================================
  1053. // Main process
  1054. //==========================================
  1055. int main(void)
  1056. {
  1057. //==========================================
  1058. //Initialization share memory
  1059. //==========================================
  1060. if(InitShareMemory() == FAIL)
  1061. {
  1062. #ifdef SystemLogMessage
  1063. DEBUG_ERROR("InitShareMemory NG\n");
  1064. #endif
  1065. if(ShmStatusCodeData!=NULL)
  1066. {
  1067. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FailToCreateShareMemory=1;
  1068. }
  1069. sleep(5);
  1070. return 0;
  1071. }
  1072. DEBUG_INFO("========================================\n");
  1073. DEBUG_INFO("Latest Firmware Version : [ %s ].\n", Version_And_Date[0]);
  1074. DEBUG_INFO("Latest Upgrade Date : [ %s ].\n", Version_And_Date[1]);
  1075. DEBUG_INFO("========================================\n");
  1076. for(;;)
  1077. {
  1078. TOP:
  1079. switch(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomEnabled)
  1080. {
  1081. case ENABLE_4G:
  1082. DEBUG_INFO("4G module : [ Enable ].\n");
  1083. if(((Dongle.Model = Check4GModem()) != FAIL))
  1084. {
  1085. ShmStatusCodeData->FaultCode.FaultEvents.bits.Telecom4GModuleBroken = 0;
  1086. Dongle.cnt_SearchModuleFail = 0;
  1087. if(isReadInfo() == PASS)
  1088. {
  1089. if(strcmp((char*)ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModelName, Dongle.MODELNAME) != 0)
  1090. memset(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModelName, 0x00, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModelName));
  1091. memcpy(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModelName, Dongle.MODELNAME, sizeof Dongle.MODELNAME);
  1092. if(strcmp((char*)ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSoftwareVer, Dongle.REVISION) != 0)
  1093. memset(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSoftwareVer, 0x00, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSoftwareVer));
  1094. memcpy(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSoftwareVer, Dongle.REVISION, sizeof Dongle.REVISION);
  1095. if(strcmp((char*)ShmSysConfigAndInfo->SysInfo.TelcomModemFwRev, Dongle.REVISION) != 0)
  1096. memset(ShmSysConfigAndInfo->SysInfo.TelcomModemFwRev, 0x00, ARRAY_SIZE(ShmSysConfigAndInfo->SysInfo.TelcomModemFwRev));
  1097. memcpy(ShmSysConfigAndInfo->SysInfo.TelcomModemFwRev, Dongle.REVISION, sizeof Dongle.REVISION);
  1098. if(strcmp((char*)ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModemImei, Dongle.IMEI) != 0)
  1099. memset(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModemImei, 0x00, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModemImei));
  1100. memcpy(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModemImei, Dongle.IMEI, sizeof Dongle.IMEI);
  1101. ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomRssi = Dongle.CSQ;
  1102. ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModemMode = Dongle.MODE;
  1103. DEBUG_INFO("========================================\n");
  1104. DEBUG_INFO("Status: Device info readable...\n");
  1105. DEBUG_INFO("========================================\n");
  1106. DEBUG_INFO("Device MANUFACTURER: %s\n", Dongle.MANUFACTURER);
  1107. DEBUG_INFO("Device MODEL: %s\n", ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModelName);
  1108. DEBUG_INFO("Device REVISION: %s\n", ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSoftwareVer);
  1109. DEBUG_INFO("Device IMEI: %s\n", ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModemImei);
  1110. DEBUG_INFO("Device RSSI: %d\n", ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomRssi);
  1111. DEBUG_INFO("Device MODE: %d\n", ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModemMode);
  1112. DEBUG_INFO("========================================\n");
  1113. Dongle.cnt_ReadInfoFail = 0;
  1114. if(isReadSimInfo() == PASS)
  1115. {
  1116. ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSimStatus = 1;
  1117. if(strcmp((char*)ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSimIccid, Dongle.ICCID) != 0)
  1118. memset(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSimIccid, 0x00, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSimIccid));
  1119. memcpy(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSimIccid, Dongle.ICCID, sizeof Dongle.ICCID);
  1120. if(strcmp((char*)ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSimImsi, Dongle.IMSI) != 0)
  1121. memset(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSimImsi, 0x00, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSimImsi));
  1122. memcpy(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSimImsi, Dongle.IMSI, sizeof Dongle.IMSI);
  1123. DEBUG_INFO("========================================\n");
  1124. DEBUG_INFO("Status: SIM card info readable...\n");
  1125. DEBUG_INFO("========================================\n");
  1126. DEBUG_INFO("Device IMSI: %s\n", ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSimImsi);
  1127. DEBUG_INFO("Device ICCID: %.20s\n", ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSimIccid);
  1128. DEBUG_INFO("TelcomSimStatus: %d\n", ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSimStatus);
  1129. DEBUG_INFO("========================================\n");
  1130. Dongle.cnt_ReadSimInfoFail = 0;
  1131. if(isPppUp() == PASS)
  1132. {
  1133. DEBUG_INFO("========================================\n");
  1134. DEBUG_INFO("Status: PPP interface found...\n");
  1135. DEBUG_INFO("========================================\n");
  1136. DEBUG_INFO("PPP IP: %s\n", ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomIpAddress);
  1137. DEBUG_INFO("========================================\n");
  1138. Dongle.cnt_pppFail = 0;
  1139. if(isReachableInternet() == PASS)
  1140. {
  1141. ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomNetworkConn = 1;
  1142. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.Telecom4GModuleCommFail = 0;
  1143. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectVia4Gi = 0;
  1144. ShmStatusCodeData->InfoCode.InfoEvents.bits.ApnDisconnectVia4Gi = 0;
  1145. Dongle.cnt_InternetFail = 0;
  1146. // Update Rssi Value
  1147. if(CheckSignalRssi() != PASS)
  1148. DEBUG_INFO("No RSSI\n");
  1149. else
  1150. ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomRssi = Dongle.CSQ;
  1151. DEBUG_INFO("========================================\n");
  1152. DEBUG_INFO("Status: 4G Device connecting...\n");
  1153. DEBUG_INFO("========================================\n");
  1154. DEBUG_INFO("Network connection: %d\n", ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomNetworkConn);
  1155. DEBUG_INFO("Dongle internet valid result: Pass\n");
  1156. DEBUG_INFO("========================================\n");
  1157. // Escape to the top
  1158. if(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomEnabled == DISABLE_4G_MODULE)
  1159. {
  1160. goto TOP;
  1161. }
  1162. sleep(CheckInternetInterval);
  1163. }
  1164. else
  1165. {
  1166. DEBUG_INFO("Dongle internet valid result: Fail %d time\n", Dongle.cnt_InternetFail);
  1167. ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomNetworkConn = 0;
  1168. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectVia4Gi = 1;
  1169. ShmStatusCodeData->InfoCode.InfoEvents.bits.ApnDisconnectVia4Gi = 1;
  1170. Dongle.cnt_InternetFail++;
  1171. if(Dongle.cnt_InternetFail > 3)
  1172. {
  1173. system("killall 4GDetection");
  1174. system("killall pppd");
  1175. sleep(2);
  1176. Dongle.cnt_InternetFail = 0;
  1177. rstModule();
  1178. }
  1179. sleep(DisconnInterval);
  1180. }
  1181. }
  1182. else
  1183. {
  1184. if(Load4gConfiguration() == FAIL)
  1185. {
  1186. DEBUG_ERROR("4G configuration value NG.\n");
  1187. if(ShmStatusCodeData!=NULL)
  1188. {
  1189. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.Telecom4GModuleCommFail=1;
  1190. }
  1191. }
  1192. else
  1193. {
  1194. DEBUG_WARN("PPP interface not found.\n");
  1195. memset(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomIpAddress, 0 , sizeof ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomIpAddress);
  1196. Dongle.cnt_pppFail++;
  1197. if(Dongle.cnt_pppFail > 3)
  1198. {
  1199. system("killall 4GDetection");
  1200. system("killall pppd");
  1201. sleep(2);
  1202. Dongle.cnt_pppFail = 0;
  1203. rstModule();
  1204. }
  1205. system("killall 4GDetection");
  1206. system("killall pppd");
  1207. sleep(2);
  1208. if(Dongle.Model == DONGLE_QUECTEL)
  1209. {
  1210. system("/root/ppp/4GDetection /dev/ttyUSB3 &");
  1211. printf("4GDetection for primary device.\n");
  1212. }
  1213. else if(Dongle.Model == DONGLE_UBLOX)
  1214. {
  1215. system("/root/ppp/4GDetection /dev/ttyACM0 &");
  1216. printf("4GDetection for second device.\n");
  1217. }
  1218. else
  1219. {}
  1220. }
  1221. sleep(CheckConnectionInterval);
  1222. }
  1223. }
  1224. else
  1225. {
  1226. DEBUG_ERROR("SIM card info read error fail: %d\n", Dongle.cnt_ReadSimInfoFail);
  1227. ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSimStatus = 0;
  1228. memset(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSimImsi, 0, sizeof ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSimImsi);
  1229. memset(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSimIccid, 0, sizeof ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSimIccid);
  1230. memset(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomIpAddress, 0 , sizeof ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomIpAddress);
  1231. DEBUG_INFO("========================================\n");
  1232. DEBUG_INFO("Status: Read Sim card info fail...\n");
  1233. DEBUG_INFO("Device ICCID: %.20s\n", ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSimIccid);
  1234. DEBUG_INFO("Device IMSI: %s\n", ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSimImsi);
  1235. DEBUG_INFO("========================================\n");
  1236. Dongle.cnt_ReadSimInfoFail ++;
  1237. if(Dongle.cnt_ReadSimInfoFail > 3)
  1238. {
  1239. system("killall 4GDetection");
  1240. system("killall pppd");
  1241. sleep(2);
  1242. Dongle.cnt_ReadSimInfoFail = 0;
  1243. rstModule();
  1244. }
  1245. sleep(CheckSimInterval);
  1246. }
  1247. }
  1248. else
  1249. {
  1250. DEBUG_ERROR("Device info read error fail: %d\n", Dongle.cnt_ReadInfoFail);
  1251. Dongle.MODE = NO_SERVICE;
  1252. memset(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModelName, 0, sizeof ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModelName);
  1253. memset(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSoftwareVer, 0, sizeof ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSoftwareVer);
  1254. memset(ShmSysConfigAndInfo->SysInfo.TelcomModemFwRev, 0, sizeof ShmSysConfigAndInfo->SysInfo.TelcomModemFwRev);
  1255. memset(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModemImei, 0, sizeof ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModemImei);
  1256. ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomRssi = 0;
  1257. ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModemMode = 0;
  1258. DEBUG_INFO("========================================\n");
  1259. DEBUG_INFO("Status: Read device info fail...\n");
  1260. DEBUG_INFO("Device MANUFACTURER: %s\n", Dongle.MANUFACTURER);
  1261. DEBUG_INFO("Device MODEL: %s\n", ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModelName);
  1262. DEBUG_INFO("Device REVISION: %s\n", ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSoftwareVer);
  1263. DEBUG_INFO("Device IMEI: %s\n", ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModemImei);
  1264. DEBUG_INFO("Device RSSI: %d\n", ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomRssi);
  1265. DEBUG_INFO("Device MODE: %d\n", ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModemMode);
  1266. DEBUG_INFO("========================================\n");
  1267. Dongle.cnt_ReadInfoFail++;
  1268. if(Dongle.cnt_ReadInfoFail > 3)
  1269. {
  1270. system("killall 4GDetection");
  1271. system("killall pppd");
  1272. sleep(2);
  1273. Dongle.cnt_ReadInfoFail = 0;
  1274. rstModule();
  1275. }
  1276. sleep(CheckModemInfoInterval);
  1277. }
  1278. }
  1279. else
  1280. {
  1281. DEBUG_ERROR("Device search error fail: %d\n", Dongle.cnt_SearchModuleFail);
  1282. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectVia4Gi = 1;
  1283. ShmStatusCodeData->InfoCode.InfoEvents.bits.ApnDisconnectVia4Gi = 1;
  1284. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.Telecom4GModuleCommFail = 0;
  1285. ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomNetworkConn = 0;
  1286. ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomRssi = 0;
  1287. ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSimStatus = 0;
  1288. ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModemMode = 0;
  1289. memset(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModemImei, 0, sizeof ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModemImei);
  1290. memset(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSimImsi, 0, sizeof ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSimImsi);
  1291. memset(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomIpAddress, 0, sizeof ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomIpAddress);
  1292. memset(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSimIccid, 0, sizeof ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSimIccid);
  1293. memset(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSoftwareVer, 0, sizeof ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSoftwareVer);
  1294. memset(ShmSysConfigAndInfo->SysInfo.TelcomModemFwRev, 0, sizeof ShmSysConfigAndInfo->SysInfo.TelcomModemFwRev);
  1295. memset(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModelName, 0, sizeof ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModelName);
  1296. Dongle.cnt_SearchModuleFail++;
  1297. if(Dongle.cnt_SearchModuleFail > 3)
  1298. {
  1299. DEBUG_ERROR("4G Module was broken.\n");
  1300. if(isModuleUnbind() == PASS)
  1301. {
  1302. isModuleBind();
  1303. }
  1304. ShmStatusCodeData->FaultCode.FaultEvents.bits.Telecom4GModuleBroken = 1;
  1305. Dongle.cnt_SearchModuleFail = 0;
  1306. }
  1307. sleep(CheckModemInterval);
  1308. }
  1309. break;
  1310. case DISABLE_4G_MODULE:
  1311. default:
  1312. DEBUG_INFO("4G module : [ Disable ].\n");
  1313. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectVia4Gi = 0;
  1314. ShmStatusCodeData->InfoCode.InfoEvents.bits.ApnDisconnectVia4Gi= 0;
  1315. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.Telecom4GModuleCommFail = 0;
  1316. ShmStatusCodeData->FaultCode.FaultEvents.bits.Telecom4GModuleBroken = 0;
  1317. ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomNetworkConn = 0;
  1318. ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomRssi = 0;
  1319. ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSimStatus = 0;
  1320. ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModemMode = 0;
  1321. memset(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModemImei, 0, sizeof ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModemImei);
  1322. memset(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSimImsi, 0, sizeof ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSimImsi);
  1323. memset(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomIpAddress, 0, sizeof ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomIpAddress);
  1324. memset(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSimIccid, 0, sizeof ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSimIccid);
  1325. memset(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSoftwareVer, 0, sizeof ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSoftwareVer);
  1326. memset(ShmSysConfigAndInfo->SysInfo.TelcomModemFwRev, 0, sizeof ShmSysConfigAndInfo->SysInfo.TelcomModemFwRev);
  1327. memset(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModelName, 0, sizeof ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModelName);
  1328. system("killall 4GDetection");
  1329. system("killall pppd");
  1330. system("ifconfig ppp0 down");
  1331. sleep(SystemInterval);
  1332. break;
  1333. }
  1334. }
  1335. return 0;
  1336. }