Module_4g.c 44 KB

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