Module_4g.c 44 KB

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