Module_4g.c 51 KB

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