Module_4g.c 50 KB

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