Module_4g.c 47 KB

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