Module_4g.c 49 KB

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