Module_4g.c 40 KB

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