Module_4g.c 48 KB

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