Module_4g.c 36 KB

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