Module_4g.c 48 KB

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