Module_4g.c 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379
  1. /*
  2. * Module_4g.c
  3. *
  4. * Created on: 2019-11-29
  5. * Update on: 2020-09-15
  6. * Author: Eason Yang
  7. * Version: V0.05
  8. */
  9. #include <sys/types.h>
  10. #include <sys/stat.h>
  11. #include <sys/time.h>
  12. #include <sys/timeb.h>
  13. #include <sys/ipc.h>
  14. #include <sys/shm.h>
  15. #include <sys/mman.h>
  16. #include <unistd.h>
  17. #include <stdarg.h>
  18. #include <stdio.h> /*標準輸入輸出定義*/
  19. #include <stdlib.h> /*標準函數庫定義*/
  20. #include <unistd.h> /*Unix 標準函數定義*/
  21. #include <fcntl.h> /*檔控制定義*/
  22. #include <termios.h> /*PPSIX 終端控制定義*/
  23. #include <errno.h> /*錯誤號定義*/
  24. #include <errno.h>
  25. #include <string.h>
  26. #include <time.h>
  27. #include <ctype.h>
  28. #include "define.h"
  29. #define DEBUG_INFO(format, args...) StoreLogMsg("[%s:%d][%s][Info] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
  30. #define DEBUG_WARN(format, args...) StoreLogMsg("[%s:%d][%s][Warn] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
  31. #define DEBUG_ERROR(format, args...) StoreLogMsg("[%s:%d][%s][Error] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
  32. #define ARRAY_SIZE(A) (sizeof(A) / sizeof(A[0]))
  33. #define PASS 1
  34. #define FAIL -1
  35. #define DONGLE_QUECTEL 1
  36. #define DONGLE_UBLOX 2
  37. // Define Module mode
  38. #define NO_SERVICE 0
  39. #define CDMA 1
  40. #define GMS_GPRS 2
  41. #define WCDMA 3
  42. #define GMS_WCDMA 4
  43. #define TD_SCDMA 5
  44. #define UNKNOW 6
  45. // Define interval
  46. #define SystemInterval 30 // Seconds
  47. #define CheckModemInterval 30 // Seconds
  48. #define CheckSimInterval 30 // Seconds
  49. #define CheckModemInfoInterval 30 // Seconds
  50. #define CheckConnectionInterval 30 // Seconds
  51. #define CheckInternetInterval 30 // Seconds
  52. #define DisconnInterval 60 // Seconds
  53. // Define Telecom mode
  54. #define DISABLE_4G_MODULE 0
  55. #define ENABLE_4G 1
  56. int Check4GModem(void);
  57. int isPppUp(void);
  58. int isReadInfo(void);
  59. int isReadSimInfo(void);
  60. int isReachableInternet(void);
  61. int isModuleUnbind(void);
  62. int isModuleBind(void);
  63. int rstModule(void);
  64. int Load4gConfiguration(void);
  65. int CheckSignalRssi(void);
  66. int at_command(int uart, char* cmd, char* rx);
  67. int openPort(char *tty);
  68. int set_interface_attribs (int fd, int speed, int parity);
  69. int set_blocking (int fd, int should_block);
  70. void trim_s(char *s, unsigned char len);
  71. void substr(char *dest, const char* src, unsigned int start, unsigned int cnt);
  72. char *LATEST_FIRMWARE_VERSION = "V0.05";
  73. char *LATEST_UPGRAGE_DATE = "2020-09-15";
  74. char *portName[3] = {"/dev/ttyUSB2", "/dev/ttyUSB2", "/dev/ttyACM2"};
  75. char *valid_Internet[2] = {"8.8.8.8", "180.76.76.76"};
  76. pid_t pid;
  77. struct dongle_info
  78. {
  79. int Model;
  80. char ICCID[20];
  81. char IMSI[16];
  82. char IMEI[16];
  83. char MANUFACTURER[8];
  84. char MODELNAME[10];
  85. char REVISION[18];
  86. unsigned char MODE;
  87. int CSQ;
  88. int cnt_InternetFail;
  89. int cnt_ReadInfoFail;
  90. int cnt_pppFail;
  91. int cnt_SearchModuleFail;
  92. int cnt_ReadSimInfoFail;
  93. }Dongle;
  94. struct SysConfigAndInfo *ShmSysConfigAndInfo;
  95. struct StatusCodeData *ShmStatusCodeData;
  96. struct FanModuleData *ShmFanModuleData;
  97. struct OCPP16Data *ShmOCPP16Data;
  98. int StoreLogMsg(const char *fmt, ...)
  99. {
  100. char Buf[4096+256];
  101. char buffer[4096];
  102. time_t CurrentTime;
  103. struct tm *tm;
  104. va_list args;
  105. va_start(args, fmt);
  106. int rc = vsnprintf(buffer, sizeof(buffer), fmt, args);
  107. va_end(args);
  108. memset(Buf,0,sizeof(Buf));
  109. CurrentTime = time(NULL);
  110. tm=localtime(&CurrentTime);
  111. sprintf(Buf,"echo -n \"[%04d.%02d.%02d %02d:%02d:%02d] - %s\" >> /Storage/SystemLog/[%04d.%02d]4g_SystemLog",
  112. tm->tm_year+1900,tm->tm_mon+1,tm->tm_mday,tm->tm_hour,tm->tm_min,tm->tm_sec,
  113. buffer,
  114. tm->tm_year+1900,tm->tm_mon+1);
  115. #ifdef SystemLogMessage
  116. system(Buf);
  117. #endif
  118. #ifdef ConsloePrintLog
  119. 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);
  120. #endif
  121. return rc;
  122. }
  123. int DiffTimeb(struct timeb ST, struct timeb ET)
  124. {
  125. //return milli-second
  126. unsigned int StartTime,StopTime;
  127. StartTime=(unsigned int)ST.time;
  128. StopTime=(unsigned int)ET.time;
  129. return (StopTime-StartTime)*1000+ET.millitm-ST.millitm;
  130. }
  131. //==========================================
  132. // Init all share memory
  133. //==========================================
  134. int InitShareMemory()
  135. {
  136. int result = PASS;
  137. int MeterSMId;
  138. //creat ShmSysConfigAndInfo
  139. if ((MeterSMId = shmget(ShmSysConfigAndInfoKey, sizeof(struct SysConfigAndInfo), 0777)) < 0)
  140. {
  141. #ifdef SystemLogMessage
  142. DEBUG_ERROR("shmget ShmSysConfigAndInfo NG\n");
  143. #endif
  144. result = FAIL;
  145. }
  146. else if ((ShmSysConfigAndInfo = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  147. {
  148. #ifdef SystemLogMessage
  149. DEBUG_ERROR("shmat ShmSysConfigAndInfo NG\n");
  150. #endif
  151. result = FAIL;
  152. }
  153. else
  154. {}
  155. //creat ShmStatusCodeData
  156. if ((MeterSMId = shmget(ShmStatusCodeKey, sizeof(struct StatusCodeData), 0777)) < 0)
  157. {
  158. #ifdef SystemLogMessage
  159. DEBUG_ERROR("shmget ShmStatusCodeData NG\n");
  160. #endif
  161. result = FAIL;
  162. }
  163. else if ((ShmStatusCodeData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  164. {
  165. #ifdef SystemLogMessage
  166. DEBUG_ERROR("shmat ShmStatusCodeData NG\n");
  167. #endif
  168. result = FAIL;
  169. }
  170. else
  171. {}
  172. //creat ShmOCPP16Data
  173. if ((MeterSMId = shmget(ShmOcppModuleKey, sizeof(struct OCPP16Data), 0777)) < 0)
  174. {
  175. #ifdef SystemLogMessage
  176. DEBUG_ERROR("shmget ShmOCPP16Data NG");
  177. #endif
  178. result = FAIL;
  179. }
  180. else if ((ShmOCPP16Data = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  181. {
  182. #ifdef SystemLogMessage
  183. DEBUG_ERROR("shmat ShmOCPP16Data NG");
  184. #endif
  185. result = FAIL;
  186. }
  187. else
  188. {}
  189. return result;
  190. }
  191. //==========================================
  192. // Check dongle model
  193. //==========================================
  194. int Check4GModem(void)
  195. {
  196. int result = FAIL;
  197. if((access("/dev/ttyUSB0", F_OK) != -1) &&
  198. (access("/dev/ttyUSB1", F_OK) != -1) &&
  199. (access("/dev/ttyUSB2", F_OK) != -1) &&
  200. (access("/dev/ttyUSB3", F_OK) != -1))
  201. {
  202. result = DONGLE_QUECTEL;
  203. }
  204. else if((access("/dev/ttyACM0", F_OK) != -1) &&
  205. (access("/dev/ttyACM1", F_OK) != -1) &&
  206. (access("/dev/ttyACM2", F_OK) != -1) &&
  207. (access("/dev/ttyACM3", F_OK) != -1) &&
  208. (access("/dev/ttyACM4", F_OK) != -1) &&
  209. (access("/dev/ttyACM5", F_OK) != -1))
  210. {
  211. result = DONGLE_UBLOX;
  212. }
  213. else
  214. {}
  215. if(result == DONGLE_QUECTEL)
  216. {
  217. #ifdef SystemLogMessage
  218. DEBUG_WARN("Quectel 4G modem be found\n");
  219. #endif
  220. }
  221. else if(result == DONGLE_UBLOX)
  222. {
  223. #ifdef SystemLogMessage
  224. DEBUG_WARN("Ublox 4G modem be found\n");
  225. #endif
  226. }
  227. else
  228. {
  229. #ifdef SystemLogMessage
  230. DEBUG_WARN("No 4G modem be found\n");
  231. #endif
  232. }
  233. return result;
  234. }
  235. //==========================================
  236. // Check ppp interface status
  237. //==========================================
  238. int isPppUp(void)
  239. {
  240. int result = FAIL;
  241. FILE *fp;
  242. char cmd[256];
  243. char buf[512];
  244. char tmp[512];
  245. strcpy(cmd, "ifconfig ppp0");;
  246. fp = popen(cmd, "r");
  247. if(fp != NULL)
  248. {
  249. while(fgets(buf, sizeof(buf), fp) != NULL)
  250. {
  251. if(strstr(buf, "ppp") > 0)
  252. {
  253. result = PASS;
  254. }
  255. if(strstr(buf, "addr:") > 0)
  256. {
  257. sscanf(buf, "%*s%s", tmp);
  258. substr((char*)ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomIpAddress, tmp, strspn(tmp, "addr:"), (strlen(tmp) - strspn(tmp, "addr:")));
  259. }
  260. }
  261. }
  262. pclose(fp);
  263. return result;
  264. }
  265. //==========================================
  266. // Check dongle info read status
  267. //==========================================
  268. int isReadInfo(void)
  269. {
  270. int result = PASS;
  271. int uart;
  272. char rx[512];
  273. char tmp[512];
  274. char Lenght;
  275. int modeStatus;
  276. if((uart = openPort(portName[Dongle.Model])) != FAIL)
  277. {
  278. switch(Dongle.Model)
  279. {
  280. case DONGLE_QUECTEL:
  281. default:
  282. //==============================
  283. // Set don't echo command
  284. //==============================
  285. if(at_command(uart, "ate0\r", rx) <= 0)
  286. result = FAIL;
  287. //==============================
  288. // Read Manufacturer
  289. //==============================
  290. Lenght = at_command(uart, "at+gmi\r", rx);
  291. if(Lenght > 0)
  292. {
  293. memset(tmp, 0, sizeof tmp);
  294. memcpy(tmp, rx, strcspn(rx,"OK"));
  295. memset(rx, 0, sizeof rx);
  296. trim_s(tmp,Lenght);
  297. strcpy(Dongle.MANUFACTURER, tmp);
  298. }
  299. else
  300. result = FAIL;
  301. //==============================
  302. // Read Model
  303. //==============================
  304. Lenght = at_command(uart, "at+gmm\r", rx);
  305. if(Lenght > 0)
  306. {
  307. memset(tmp, 0, sizeof tmp);
  308. memcpy(tmp, rx, strcspn(rx,"OK"));
  309. memset(rx, 0, sizeof rx);
  310. trim_s(tmp,Lenght);
  311. strncpy(Dongle.MODELNAME, tmp, strlen(tmp));
  312. }
  313. else
  314. result = FAIL;
  315. //==============================
  316. // Read Revision
  317. //==============================
  318. Lenght = at_command(uart, "at+gmr\r", rx);
  319. if(Lenght > 0)
  320. {
  321. memset(tmp, 0, sizeof tmp);
  322. memcpy(tmp, rx, strcspn(rx, "OK"));
  323. memset(rx, 0, sizeof rx);
  324. trim_s(tmp,Lenght);
  325. strncpy(Dongle.REVISION, tmp, strlen(tmp));
  326. }
  327. else
  328. result = FAIL;
  329. //==============================
  330. // Read IMEI
  331. //==============================
  332. Lenght = at_command(uart, "at+gsn\r", rx);
  333. if(Lenght > 0)
  334. {
  335. memset(tmp, 0, sizeof tmp);
  336. memcpy(tmp, rx, strcspn(rx, "OK"));
  337. memset(rx, 0, sizeof rx);
  338. trim_s(tmp,Lenght);
  339. strncpy(Dongle.IMEI, tmp, strlen(tmp));
  340. }
  341. else
  342. result = FAIL;
  343. //==============================
  344. // Read CSQ
  345. //==============================
  346. Lenght = at_command(uart, "at+csq\r", rx);
  347. if(Lenght > 0)
  348. {
  349. memset(tmp, 0, sizeof tmp);
  350. memcpy(tmp, rx, strcspn(rx,","));
  351. strncpy(rx, tmp + strcspn(tmp,":")+1, 10);
  352. Dongle.CSQ = atoi(rx);
  353. }
  354. else
  355. result = FAIL;
  356. //==============================
  357. // Read Mode
  358. //==============================
  359. Lenght = at_command(uart, "at+qcfg= \"nwscanmode\"\r", rx);
  360. if(Lenght > 0)
  361. {
  362. memset(tmp, 0, sizeof tmp);
  363. memcpy(tmp, rx, strcspn(rx,"OK"));
  364. strncpy(rx, tmp + strcspn(tmp,",")+1, 1);
  365. modeStatus = atoi(rx);
  366. if(modeStatus == 0)
  367. Dongle.MODE = UNKNOW;
  368. else if(modeStatus == 1)
  369. Dongle.MODE = GMS_GPRS;
  370. else if(modeStatus == 2)
  371. Dongle.MODE = WCDMA;
  372. else if(modeStatus == 3)
  373. Dongle.MODE = UNKNOW;
  374. else if(modeStatus == 4)
  375. Dongle.MODE = TD_SCDMA;
  376. else if(modeStatus == 5)
  377. Dongle.MODE = UNKNOW;
  378. else if(modeStatus == 6)
  379. Dongle.MODE = CDMA;
  380. else
  381. Dongle.MODE = UNKNOW;
  382. }
  383. else
  384. result = FAIL;
  385. break;
  386. case DONGLE_UBLOX:
  387. //==============================
  388. // Set don't echo command
  389. //==============================
  390. if(at_command(uart, "ate0\r", rx) <= 0)
  391. result = FAIL;
  392. //==============================
  393. // Read Manufacturer
  394. //==============================
  395. Lenght = at_command(uart, "at+cgmi\r", rx);
  396. if(Lenght > 0)
  397. {
  398. memset(tmp, 0, sizeof tmp);
  399. memcpy(tmp, rx, strcspn(rx,"OK"));
  400. memset(rx, 0, sizeof rx);
  401. strncpy(Dongle.MANUFACTURER, tmp+2, 6);
  402. }
  403. else
  404. result = FAIL;
  405. //==============================
  406. // Read Model
  407. //==============================
  408. Lenght = at_command(uart, "at+cgmm\r", rx);
  409. if(Lenght > 0)
  410. {
  411. memset(tmp, 0, sizeof tmp);
  412. memcpy(tmp, rx, strcspn(rx,"OK"));
  413. memset(rx, 0, sizeof rx);
  414. strncpy(Dongle.MODELNAME , tmp+2, 9);
  415. }
  416. else
  417. result = FAIL;
  418. //==============================
  419. // Read Revision
  420. //==============================
  421. Lenght = at_command(uart, "at+cgmr\r", rx);
  422. if(Lenght > 0)
  423. {
  424. memset(tmp, 0, sizeof tmp);
  425. memcpy(tmp, rx, strcspn(rx, "OK"));
  426. memset(rx, 0, sizeof rx);
  427. strncpy(Dongle.REVISION, tmp+2, 5);
  428. }
  429. else
  430. result = FAIL;
  431. //==============================
  432. // Read IMEI
  433. //==============================
  434. Lenght = at_command(uart, "at+cgsn\r", rx);
  435. if(Lenght > 0)
  436. {
  437. memset(tmp, 0, sizeof tmp);
  438. memcpy(tmp, rx, strcspn(rx, "OK"));
  439. memset(rx, 0, sizeof rx);
  440. trim_s(tmp,Lenght);
  441. strncpy(Dongle.IMEI, tmp, strlen(tmp));
  442. }
  443. else
  444. result = FAIL;
  445. //==============================
  446. // Read CSQ
  447. //==============================
  448. Lenght = at_command(uart, "at+csq\r", rx);
  449. if(Lenght > 0)
  450. {
  451. memset(tmp, 0, sizeof tmp);
  452. memcpy(tmp, rx, strcspn(rx,","));
  453. strncpy(rx, tmp + strcspn(tmp,":")+1, 10);
  454. Dongle.CSQ = atoi(rx);
  455. }
  456. else
  457. result = FAIL;
  458. break;
  459. }
  460. }
  461. else
  462. {
  463. #ifdef SystemLogMessage
  464. DEBUG_ERROR("%s open fail.\n", portName[Dongle.Model]);
  465. #endif
  466. result = FAIL;
  467. }
  468. close(uart);
  469. return result;
  470. }
  471. //==========================================
  472. // Read sim card information
  473. //==========================================
  474. int isReadSimInfo(void)
  475. {
  476. int result = PASS;
  477. int uart;
  478. char rx[512];
  479. char tmp[512];
  480. char Lenght;
  481. if((uart = openPort(portName[Dongle.Model])) != FAIL)
  482. {
  483. switch(Dongle.Model)
  484. {
  485. case DONGLE_QUECTEL:
  486. default:
  487. //==============================
  488. // Set don't echo command
  489. //==============================
  490. if(at_command(uart, "ate0\r", rx) <= 0)
  491. result = FAIL;
  492. //==============================
  493. // Read IMSI
  494. //==============================
  495. Lenght = at_command(uart, "at+cimi\r", rx);
  496. if(Lenght > 0)
  497. {
  498. memset(tmp, 0, sizeof tmp);
  499. if (strstr(rx, "ERROR"))
  500. {
  501. memset(Dongle.IMSI, 0, sizeof Dongle.IMSI);
  502. result = FAIL;
  503. }
  504. else
  505. {
  506. memcpy(tmp, rx, strcspn(rx, "OK"));
  507. trim_s(tmp, Lenght);
  508. memset(rx, 0, sizeof rx);
  509. strncpy(Dongle.IMSI, tmp, strlen(tmp));
  510. }
  511. }
  512. else
  513. result = FAIL;
  514. //==============================
  515. // Read CCID
  516. //==============================
  517. Lenght = at_command(uart, "at+qccid\r", rx);
  518. if(Lenght > 0)
  519. {
  520. memset(tmp, 0, sizeof tmp);
  521. if (strstr(rx, "ERROR"))
  522. {
  523. memset(Dongle.ICCID, 0, sizeof Dongle.ICCID);
  524. result = FAIL;
  525. }
  526. else
  527. {
  528. memcpy(tmp, rx, strcspn(rx, "OK"));
  529. memset(rx, 0, sizeof rx);
  530. strncpy(Dongle.ICCID, tmp + strcspn(tmp, ":") + 2, 20);
  531. }
  532. }
  533. else
  534. result = FAIL;
  535. break;
  536. case DONGLE_UBLOX:
  537. //==============================
  538. // Set don't echo command
  539. //==============================
  540. if(at_command(uart, "ate0\r", rx) <= 0)
  541. result = FAIL;
  542. //==============================
  543. // Read IMSI
  544. //==============================
  545. Lenght = at_command(uart, "at+cimi\r", rx);
  546. if(Lenght > 0)
  547. {
  548. memset(tmp, 0, sizeof tmp);
  549. if(strstr(rx, "ERROR"))
  550. {
  551. memset(Dongle.IMSI, 0, sizeof Dongle.IMSI);
  552. result = FAIL;
  553. }
  554. else
  555. {
  556. memcpy(tmp, rx, strcspn(rx,"OK"));
  557. memset(rx, 0, sizeof rx);
  558. trim_s(tmp,Lenght);
  559. strncpy(Dongle.IMSI , tmp, strlen(tmp));
  560. }
  561. }
  562. else
  563. result = FAIL;
  564. //==============================
  565. // Read CCID
  566. //==============================
  567. Lenght = at_command(uart, "at+ccid\r", rx);
  568. if(Lenght > 0)
  569. {
  570. memset(tmp, 0, sizeof tmp);
  571. if(strstr(rx, "ERROR"))
  572. {
  573. memset(Dongle.ICCID, 0, sizeof Dongle.ICCID);
  574. result = FAIL;
  575. }
  576. else
  577. {
  578. memcpy(tmp, rx, strcspn(rx, "OK"));
  579. memset(rx, 0, sizeof rx);
  580. strncpy(Dongle.ICCID, tmp + strcspn(tmp, ":") + 2, 20);
  581. }
  582. }
  583. else
  584. result = FAIL;
  585. break;
  586. }
  587. }
  588. else
  589. {
  590. #ifdef SystemLogMessage
  591. DEBUG_ERROR("%s open fail.\n", portName[Dongle.Model]);
  592. #endif
  593. result = FAIL;
  594. }
  595. close(uart);
  596. return result;
  597. }
  598. //==========================================
  599. // Read signal information
  600. //==========================================
  601. int CheckSignalRssi(void)
  602. {
  603. int result = PASS;
  604. int uart;
  605. char rx[512];
  606. char tmp[512];
  607. char Lenght;
  608. if((uart = openPort(portName[Dongle.Model])) != FAIL)
  609. {
  610. switch (Dongle.Model)
  611. {
  612. case DONGLE_UBLOX:
  613. //==============================
  614. // Set don't echo command
  615. //==============================
  616. if(at_command(uart, "ate0\r", rx) <= 0)
  617. result = FAIL;
  618. //==============================
  619. // Read CSQ
  620. //==============================
  621. Lenght = at_command(uart, "at+csq\r", rx);
  622. if(Lenght > 0)
  623. {
  624. memset(tmp, 0, sizeof tmp);
  625. memcpy(tmp, rx, strcspn(rx, ","));
  626. strncpy(rx, tmp + strcspn(tmp, ":") + 1, 10);
  627. Dongle.CSQ = atoi(rx);
  628. }
  629. else
  630. result = FAIL;
  631. break;
  632. case DONGLE_QUECTEL:
  633. default:
  634. //==============================
  635. // Set don't echo command
  636. //==============================
  637. if(at_command(uart, "ate0\r", rx) <= 0)
  638. result = FAIL;
  639. //==============================
  640. // Read CSQ
  641. //==============================
  642. Lenght = at_command(uart, "at+csq\r", rx);
  643. if(Lenght > 0)
  644. {
  645. memset(tmp, 0, sizeof tmp);
  646. memcpy(tmp, rx, strcspn(rx, ","));
  647. strncpy(rx, tmp + strcspn(tmp, ":") + 1, 10);
  648. Dongle.CSQ = atoi(rx);
  649. }
  650. else
  651. result = FAIL;
  652. break;
  653. }
  654. }
  655. else
  656. {
  657. #ifdef SystemLogMessage
  658. DEBUG_ERROR("%s open fail.\n", portName[Dongle.Model]);
  659. #endif
  660. result = FAIL;
  661. }
  662. close(uart);
  663. return result;
  664. }
  665. //==========================================
  666. // Init 4G dongle configuration
  667. //==========================================
  668. int Load4gConfiguration()
  669. {
  670. int result = FAIL;
  671. unsigned char CopyTmp[1024];
  672. if(strlen((char*)ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomApn) > 0)
  673. {
  674. system("cat /dev/null > /root/ppp/auto-apn.conf");
  675. system("chmod 600 /root/ppp/auto-apn.conf");
  676. system("cat /dev/null > /etc/ppp/pap-secrets");
  677. system("chmod 600 /etc/ppp/pap-secrets");
  678. system("cat /dev/null > /etc/ppp/chap-secrets");
  679. system("chmod 600 /etc/ppp/chap-secrets");
  680. system("cat /dev/null > /etc/ppp/auth");
  681. system("chmod 600 /etc/ppp/auth");
  682. memset(CopyTmp,0,sizeof(CopyTmp));
  683. sprintf((char*)CopyTmp,"echo APN=\"%s\" > /root/ppp/auto-apn.conf",ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomApn);
  684. system((char*)CopyTmp);
  685. memset(CopyTmp,0,sizeof(CopyTmp));
  686. sprintf((char*)CopyTmp,"echo ACCOUNT=\"%s\" >> /root/ppp/auto-apn.conf",ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomChapPapId);
  687. system((char*)CopyTmp);
  688. memset(CopyTmp,0,sizeof(CopyTmp));
  689. sprintf((char*)CopyTmp,"echo PASSWORD=\"%s\" >> /root/ppp/auto-apn.conf",ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomChapPapPwd);
  690. system((char*)CopyTmp);
  691. memset(CopyTmp,0,sizeof(CopyTmp));
  692. sprintf((char*)CopyTmp,"echo %s > /etc/ppp/auth",ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomChapPapId);
  693. system((char*)CopyTmp);
  694. sprintf((char*)CopyTmp,"echo %s >> /etc/ppp/auth",ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomChapPapPwd);
  695. system((char*)CopyTmp);
  696. if(strlen((char*)ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomChapPapId)>0)
  697. {
  698. memset(CopyTmp,0,sizeof(CopyTmp));
  699. if(strlen((char*)ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomChapPapPwd)>0)
  700. sprintf((char*)CopyTmp,"echo \"%s * %s \" > /etc/ppp/pap-secrets", ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomChapPapId, ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomChapPapPwd);
  701. else
  702. sprintf((char*)CopyTmp,"echo \"%s * \\<Your\\ Password\\> \" > /etc/ppp/pap-secrets", ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomChapPapId);
  703. system((char*)CopyTmp);
  704. memset(CopyTmp,0,sizeof(CopyTmp));
  705. if(strlen((char*)ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomChapPapPwd)>0)
  706. sprintf((char*)CopyTmp,"echo \"%s * %s \" > /etc/ppp/chap-secrets", ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomChapPapId, ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomChapPapPwd);
  707. else
  708. sprintf((char*)CopyTmp,"echo \"%s * \\<Your\\ Password\\> \" > /etc/ppp/chap-secrets", ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomChapPapId);
  709. system((char*)CopyTmp);
  710. }
  711. result = PASS;
  712. }
  713. return result;
  714. }
  715. //==========================================
  716. // Check internet access status
  717. //==========================================
  718. int isReachableInternet(void)
  719. {
  720. int result = FAIL;
  721. FILE *fp;
  722. char cmd[256];
  723. char buf[512];
  724. //char tmp[512];
  725. for(int idx=0;idx<ARRAY_SIZE(valid_Internet);idx++)
  726. {
  727. sprintf(cmd, "ping -c 1 -w 3 -I ppp0 %s", valid_Internet[idx]);
  728. fp = popen(cmd, "r");
  729. if(fp != NULL)
  730. {
  731. while(fgets(buf, sizeof(buf), fp) != NULL)
  732. {
  733. if(strstr(buf, "transmitted") > 0)
  734. {
  735. //sscanf(buf, "%*s%*s%*s%*s%*s%s", tmp);
  736. if(strstr(buf,"100%") != NULL)
  737. {
  738. }
  739. else
  740. {
  741. result = PASS;
  742. }
  743. //DEBUG_INFO("%s",buf);
  744. //DEBUG_INFO("%s\n",tmp);
  745. }
  746. }
  747. }
  748. pclose(fp);
  749. }
  750. return result;
  751. }
  752. //==========================================
  753. // Unbind USB port
  754. //==========================================
  755. int isModuleUnbind(void)
  756. {
  757. int result = FAIL;
  758. FILE *fp;
  759. char cmd[256];
  760. strcpy(cmd, "echo '1-1'> /sys/bus/usb/drivers/usb/unbind");
  761. fp = popen(cmd, "r");
  762. if(fp == NULL)
  763. {
  764. result = FAIL;
  765. }
  766. else
  767. {
  768. result = PASS;
  769. #ifdef SystemLogMessage
  770. DEBUG_INFO("Unbind USB for dongle.\n");
  771. #endif
  772. }
  773. return result;
  774. }
  775. //==========================================
  776. // Bind USB port
  777. //==========================================
  778. int isModuleBind(void)
  779. {
  780. int result = FAIL;
  781. FILE *fp;
  782. char cmd[256];
  783. strcpy(cmd, "echo '1-1'> /sys/bus/usb/drivers/usb/bind");
  784. fp = popen(cmd, "r");
  785. if(fp == NULL)
  786. {
  787. result = FAIL;
  788. }
  789. else
  790. {
  791. #ifdef SystemLogMessage
  792. DEBUG_INFO("Bind USB for dongle.\n");
  793. #endif
  794. result = PASS;
  795. }
  796. return result;
  797. }
  798. //==========================================
  799. // Dongle reset process
  800. //==========================================
  801. int rstModule(void)
  802. {
  803. int result = PASS;
  804. int uart;
  805. char rx[512];
  806. if((uart = openPort(portName[Dongle.Model])) != FAIL)
  807. {
  808. //==============================
  809. // Reset module
  810. //==============================
  811. switch(Dongle.Model)
  812. {
  813. case DONGLE_QUECTEL:
  814. default:
  815. if(at_command(uart, "at+cfun=1,1\r", rx) <= 0)
  816. {
  817. result = FAIL;
  818. }
  819. DEBUG_INFO("Dongle hardware reset...\n");
  820. break;
  821. case DONGLE_UBLOX:
  822. if(at_command(uart, "at+cfun=1,1\r", rx) <= 0)
  823. {
  824. result = FAIL;
  825. }
  826. DEBUG_INFO("Dongle hardware reset...\n");
  827. break;
  828. }
  829. }
  830. else
  831. {
  832. #ifdef SystemLogMessage
  833. DEBUG_ERROR("%s open fail.\n", portName[Dongle.Model]);
  834. #endif
  835. result = FAIL;
  836. }
  837. close(uart);
  838. sleep(40);
  839. return result;
  840. }
  841. //==========================================
  842. // AT command send/receive
  843. //==========================================
  844. int at_command(int uart, char* cmd, char* rx)
  845. {
  846. int len;
  847. //sleep(2); //required to make flush work, for some reason
  848. tcflush(uart,TCIOFLUSH);
  849. if(write(uart, cmd, strlen(cmd)) >= sizeof(cmd))
  850. {
  851. usleep(500000);
  852. len = read(uart, rx, 512);
  853. }
  854. else
  855. {
  856. #ifdef SystemLogMessage
  857. DEBUG_ERROR("AT command %s response fail.\n", cmd);
  858. #endif
  859. }
  860. return len;
  861. }
  862. //==========================================
  863. // Dongle communication port open
  864. //==========================================
  865. int openPort(char *tty)
  866. {
  867. int uart = open(tty, O_RDWR | O_NOCTTY | O_NDELAY);
  868. if(uart!=FAIL)
  869. {
  870. if((set_interface_attribs(uart, B115200, 0) != PASS) || (set_blocking(uart, 0) != PASS))
  871. uart = FAIL;
  872. }
  873. return uart;
  874. }
  875. //==========================================
  876. // Port parameter set
  877. //==========================================
  878. int set_interface_attribs (int fd, int speed, int parity)
  879. {
  880. int result = FAIL;
  881. struct termios tty;
  882. memset (&tty, 0, sizeof tty);
  883. if (tcgetattr (fd, &tty) == 0)
  884. {
  885. cfsetospeed (&tty, speed);
  886. cfsetispeed (&tty, speed);
  887. tty.c_cflag = (tty.c_cflag & ~CSIZE) | CS8; // 8-bit chars
  888. // disable IGNBRK for mismatched speed tests; otherwise receive break
  889. // as \000 chars
  890. tty.c_iflag &= ~IGNBRK; // disable break processing
  891. tty.c_lflag = 0; // no signaling chars, no echo,
  892. // no canonical processing
  893. tty.c_oflag = 0; // no remapping, no delays
  894. tty.c_cc[VMIN] = 0; // read doesn't block
  895. tty.c_cc[VTIME] = 10; // 1 seconds read timeout
  896. tty.c_iflag &= ~(IXON | IXOFF | IXANY); // shut off xon/xoff ctrl
  897. tty.c_cflag |= (CLOCAL | CREAD); // ignore modem controls,
  898. // enable reading
  899. tty.c_cflag &= ~(PARENB | PARODD); // shut off parity
  900. tty.c_cflag |= parity;
  901. tty.c_cflag &= ~CSTOPB;
  902. tty.c_cflag &= ~CRTSCTS;
  903. if (tcsetattr (fd, TCSANOW, &tty) == 0)
  904. result = PASS;
  905. }
  906. return result;
  907. }
  908. int set_blocking (int fd, int should_block)
  909. {
  910. int result = FAIL;
  911. struct termios tty;
  912. memset (&tty, 0, sizeof tty);
  913. if (tcgetattr (fd, &tty) == 0)
  914. {
  915. tty.c_cc[VMIN] = should_block ? 1 : 0;
  916. tty.c_cc[VTIME] = 5; // 0.5 seconds read timeout
  917. if (tcsetattr (fd, TCSANOW, &tty) == 0)
  918. result = PASS;
  919. }
  920. return result;
  921. }
  922. //==========================================
  923. // Common routine
  924. //==========================================
  925. void trim_s(char *s, unsigned char len)
  926. {
  927. for(unsigned char i = 0 ; i < len; i++)
  928. {
  929. if (!((s[i]>='a') && (s[i]<='z')) && !((s[i]>='A') && (s[i]<='Z'))&& !((s[i]>='0') && (s[i]<='9')))
  930. {
  931. s[i] = s[i + 1];
  932. strncpy(s + i, s + i + 1, len);
  933. i -= 1;
  934. len -= 1;
  935. }
  936. }
  937. s[len + 1] = '\0';
  938. }
  939. void substr(char *dest, const char* src, unsigned int start, unsigned int cnt)
  940. {
  941. strncpy(dest, src + start, cnt);
  942. dest[cnt] = 0;
  943. }
  944. //==========================================
  945. // Main process
  946. //==========================================
  947. int main(void)
  948. {
  949. //==========================================
  950. //Initialization share memory
  951. //==========================================
  952. if(InitShareMemory() == FAIL)
  953. {
  954. #ifdef SystemLogMessage
  955. DEBUG_ERROR("InitShareMemory NG\n");
  956. #endif
  957. if(ShmStatusCodeData!=NULL)
  958. {
  959. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FailToCreateShareMemory=1;
  960. }
  961. sleep(5);
  962. return 0;
  963. }
  964. DEBUG_INFO("4G Module latest Firmware Version : %s\n",LATEST_FIRMWARE_VERSION);
  965. DEBUG_INFO("Date for last upgrade : %s\n",LATEST_UPGRAGE_DATE);
  966. for(;;)
  967. {
  968. TOP:
  969. switch(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomEnabled)
  970. {
  971. case ENABLE_4G:
  972. DEBUG_INFO("Enable 4G module.\n");
  973. if(((Dongle.Model = Check4GModem()) != FAIL))
  974. {
  975. ShmStatusCodeData->FaultCode.FaultEvents.bits.Telecom4GModuleBroken = 0;
  976. Dongle.cnt_SearchModuleFail = 0;
  977. if(isReadInfo() == PASS)
  978. {
  979. memcpy(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModelName, Dongle.MODELNAME, sizeof Dongle.MODELNAME);
  980. memcpy(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSoftwareVer, Dongle.REVISION, sizeof Dongle.REVISION);
  981. memcpy(ShmSysConfigAndInfo->SysInfo.TelcomModemFwRev, Dongle.REVISION, sizeof Dongle.REVISION);
  982. memcpy(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModemImei, Dongle.IMEI, sizeof Dongle.IMEI);
  983. ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomRssi = Dongle.CSQ;
  984. ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModemMode = Dongle.MODE;
  985. DEBUG_INFO("========================================\n");
  986. DEBUG_INFO("Status: Device info readable...\n");
  987. DEBUG_INFO("========================================\n");
  988. DEBUG_INFO("Device MANUFACTURER: %s\n", Dongle.MANUFACTURER);
  989. DEBUG_INFO("Device MODEL: %s\n", ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModelName);
  990. DEBUG_INFO("Device REVISION: %s\n", ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSoftwareVer);
  991. DEBUG_INFO("Device IMEI: %s\n", ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModemImei);
  992. DEBUG_INFO("Device RSSI: %d\n", ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomRssi);
  993. DEBUG_INFO("Device MODE: %d\n", ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModemMode);
  994. DEBUG_INFO("========================================\n");
  995. Dongle.cnt_ReadInfoFail = 0;
  996. if(isReadSimInfo() == PASS)
  997. {
  998. ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSimStatus = 1;
  999. memcpy(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSimIccid, Dongle.ICCID, sizeof Dongle.ICCID);
  1000. memcpy(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSimImsi, Dongle.IMSI, sizeof Dongle.IMSI);
  1001. DEBUG_INFO("========================================\n");
  1002. DEBUG_INFO("Status: SIM card info readable...\n");
  1003. DEBUG_INFO("========================================\n");
  1004. DEBUG_INFO("Device IMSI: %s\n", ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSimImsi);
  1005. DEBUG_INFO("Device ICCID: %.20s\n", ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSimIccid);
  1006. DEBUG_INFO("TelcomSimStatus: %d\n", ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSimStatus);
  1007. DEBUG_INFO("========================================\n");
  1008. Dongle.cnt_ReadSimInfoFail = 0;
  1009. if(isPppUp() == PASS)
  1010. {
  1011. DEBUG_INFO("========================================\n");
  1012. DEBUG_INFO("Status: PPP interface found...\n");
  1013. DEBUG_INFO("========================================\n");
  1014. DEBUG_INFO("PPP IP: %s\n", ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomIpAddress);
  1015. DEBUG_INFO("========================================\n");
  1016. Dongle.cnt_pppFail = 0;
  1017. if(isReachableInternet() == PASS)
  1018. {
  1019. ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomNetworkConn = 1;
  1020. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.Telecom4GModuleCommFail = 0;
  1021. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectVia4Gi = 0;
  1022. ShmStatusCodeData->InfoCode.InfoEvents.bits.ApnDisconnectVia4Gi = 0;
  1023. Dongle.cnt_InternetFail = 0;
  1024. // Update Rssi Value
  1025. if(CheckSignalRssi() != PASS)
  1026. DEBUG_INFO("No RSSI\n");
  1027. else
  1028. ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomRssi = Dongle.CSQ;
  1029. DEBUG_INFO("========================================\n");
  1030. DEBUG_INFO("Status: 4G Device connecting...\n");
  1031. DEBUG_INFO("========================================\n");
  1032. DEBUG_INFO("Network connection: %d\n", ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomNetworkConn);
  1033. DEBUG_INFO("Dongle internet valid result: Pass\n");
  1034. DEBUG_INFO("========================================\n");
  1035. // Escape to the top
  1036. if(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomEnabled == DISABLE_4G_MODULE)
  1037. {
  1038. goto TOP;
  1039. }
  1040. sleep(CheckInternetInterval);
  1041. }
  1042. else
  1043. {
  1044. DEBUG_INFO("Dongle internet valid result: Fail %d time\n", Dongle.cnt_InternetFail);
  1045. ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomNetworkConn = 0;
  1046. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectVia4Gi = 1;
  1047. ShmStatusCodeData->InfoCode.InfoEvents.bits.ApnDisconnectVia4Gi = 1;
  1048. Dongle.cnt_InternetFail++;
  1049. if(Dongle.cnt_InternetFail > 3)
  1050. {
  1051. system("killall 4GDetection");
  1052. system("killall pppd");
  1053. sleep(2);
  1054. Dongle.cnt_InternetFail = 0;
  1055. rstModule();
  1056. }
  1057. sleep(DisconnInterval);
  1058. }
  1059. }
  1060. else
  1061. {
  1062. if(Load4gConfiguration() == FAIL)
  1063. {
  1064. DEBUG_ERROR("4G configuration value NG.\n");
  1065. if(ShmStatusCodeData!=NULL)
  1066. {
  1067. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.Telecom4GModuleCommFail=1;
  1068. }
  1069. }
  1070. else
  1071. {
  1072. DEBUG_WARN("PPP interface not found.\n");
  1073. memset(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomIpAddress, 0 , sizeof ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomIpAddress);
  1074. Dongle.cnt_pppFail++;
  1075. if(Dongle.cnt_pppFail > 3)
  1076. {
  1077. system("killall 4GDetection");
  1078. system("killall pppd");
  1079. sleep(2);
  1080. Dongle.cnt_pppFail = 0;
  1081. rstModule();
  1082. }
  1083. system("killall 4GDetection");
  1084. system("killall pppd");
  1085. sleep(2);
  1086. if(Dongle.Model == DONGLE_QUECTEL)
  1087. {
  1088. system("/root/ppp/4GDetection /dev/ttyUSB3 &");
  1089. printf("4GDetection for primary device.\n");
  1090. }
  1091. else if(Dongle.Model == DONGLE_UBLOX)
  1092. {
  1093. system("/root/ppp/4GDetection /dev/ttyACM0 &");
  1094. printf("4GDetection for second device.\n");
  1095. }
  1096. else
  1097. {}
  1098. }
  1099. sleep(CheckConnectionInterval);
  1100. }
  1101. }
  1102. else
  1103. {
  1104. DEBUG_ERROR("SIM card info read error fail: %d\n", Dongle.cnt_ReadSimInfoFail);
  1105. ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSimStatus = 0;
  1106. memset(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSimImsi, 0, sizeof ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSimImsi);
  1107. memset(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSimIccid, 0, sizeof ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSimIccid);
  1108. memset(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomIpAddress, 0 , sizeof ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomIpAddress);
  1109. DEBUG_INFO("========================================\n");
  1110. DEBUG_INFO("Status: Read Sim card info fail...\n");
  1111. DEBUG_INFO("Device ICCID: %.20s\n", ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSimIccid);
  1112. DEBUG_INFO("Device IMSI: %s\n", ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSimImsi);
  1113. DEBUG_INFO("========================================\n");
  1114. Dongle.cnt_ReadSimInfoFail ++;
  1115. if(Dongle.cnt_ReadSimInfoFail > 3)
  1116. {
  1117. system("killall 4GDetection");
  1118. system("killall pppd");
  1119. sleep(2);
  1120. Dongle.cnt_ReadSimInfoFail = 0;
  1121. rstModule();
  1122. }
  1123. sleep(CheckSimInterval);
  1124. }
  1125. }
  1126. else
  1127. {
  1128. DEBUG_ERROR("Device info read error fail: %d\n", Dongle.cnt_ReadInfoFail);
  1129. Dongle.MODE = NO_SERVICE;
  1130. memset(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModelName, 0, sizeof ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModelName);
  1131. memset(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSoftwareVer, 0, sizeof ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSoftwareVer);
  1132. memset(ShmSysConfigAndInfo->SysInfo.TelcomModemFwRev, 0, sizeof ShmSysConfigAndInfo->SysInfo.TelcomModemFwRev);
  1133. memset(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModemImei, 0, sizeof ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModemImei);
  1134. ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomRssi = 0;
  1135. ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModemMode = 0;
  1136. DEBUG_INFO("========================================\n");
  1137. DEBUG_INFO("Status: Read device info fail...\n");
  1138. DEBUG_INFO("Device MANUFACTURER: %s\n", Dongle.MANUFACTURER);
  1139. DEBUG_INFO("Device MODEL: %s\n", ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModelName);
  1140. DEBUG_INFO("Device REVISION: %s\n", ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSoftwareVer);
  1141. DEBUG_INFO("Device IMEI: %s\n", ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModemImei);
  1142. DEBUG_INFO("Device RSSI: %d\n", ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomRssi);
  1143. DEBUG_INFO("Device MODE: %d\n", ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModemMode);
  1144. DEBUG_INFO("========================================\n");
  1145. Dongle.cnt_ReadInfoFail++;
  1146. if(Dongle.cnt_ReadInfoFail > 3)
  1147. {
  1148. system("killall 4GDetection");
  1149. system("killall pppd");
  1150. sleep(2);
  1151. Dongle.cnt_ReadInfoFail = 0;
  1152. rstModule();
  1153. }
  1154. sleep(CheckModemInfoInterval);
  1155. }
  1156. }
  1157. else
  1158. {
  1159. DEBUG_ERROR("Device search error fail: %d\n", Dongle.cnt_SearchModuleFail);
  1160. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectVia4Gi = 1;
  1161. ShmStatusCodeData->InfoCode.InfoEvents.bits.ApnDisconnectVia4Gi = 1;
  1162. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.Telecom4GModuleCommFail = 0;
  1163. ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomNetworkConn = 0;
  1164. ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomRssi = 0;
  1165. ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSimStatus = 0;
  1166. ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModemMode = 0;
  1167. memset(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModemImei, 0, sizeof ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModemImei);
  1168. memset(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSimImsi, 0, sizeof ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSimImsi);
  1169. memset(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomIpAddress, 0, sizeof ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomIpAddress);
  1170. memset(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSimIccid, 0, sizeof ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSimIccid);
  1171. memset(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSoftwareVer, 0, sizeof ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSoftwareVer);
  1172. memset(ShmSysConfigAndInfo->SysInfo.TelcomModemFwRev, 0, sizeof ShmSysConfigAndInfo->SysInfo.TelcomModemFwRev);
  1173. memset(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModelName, 0, sizeof ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModelName);
  1174. Dongle.cnt_SearchModuleFail++;
  1175. if(Dongle.cnt_SearchModuleFail > 3)
  1176. {
  1177. DEBUG_ERROR("4G Module was broken.\n");
  1178. if(isModuleUnbind() == PASS)
  1179. {
  1180. isModuleBind();
  1181. }
  1182. ShmStatusCodeData->FaultCode.FaultEvents.bits.Telecom4GModuleBroken = 1;
  1183. Dongle.cnt_SearchModuleFail = 0;
  1184. }
  1185. sleep(CheckModemInterval);
  1186. }
  1187. break;
  1188. case DISABLE_4G_MODULE:
  1189. default:
  1190. DEBUG_INFO("Disable 4G module.\n");
  1191. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectVia4Gi = 0;
  1192. ShmStatusCodeData->InfoCode.InfoEvents.bits.ApnDisconnectVia4Gi= 0;
  1193. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.Telecom4GModuleCommFail = 0;
  1194. ShmStatusCodeData->FaultCode.FaultEvents.bits.Telecom4GModuleBroken = 0;
  1195. ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomNetworkConn = 0;
  1196. ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomRssi = 0;
  1197. ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSimStatus = 0;
  1198. ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModemMode = 0;
  1199. memset(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModemImei, 0, sizeof ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModemImei);
  1200. memset(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSimImsi, 0, sizeof ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSimImsi);
  1201. memset(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomIpAddress, 0, sizeof ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomIpAddress);
  1202. memset(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSimIccid, 0, sizeof ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSimIccid);
  1203. memset(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSoftwareVer, 0, sizeof ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSoftwareVer);
  1204. memset(ShmSysConfigAndInfo->SysInfo.TelcomModemFwRev, 0, sizeof ShmSysConfigAndInfo->SysInfo.TelcomModemFwRev);
  1205. memset(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModelName, 0, sizeof ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModelName);
  1206. system("killall 4GDetection");
  1207. system("killall pppd");
  1208. system("ifconfig ppp0 down");
  1209. sleep(SystemInterval);
  1210. break;
  1211. }
  1212. //sleep(SystemInterval);
  1213. }
  1214. return 0;
  1215. }