Module_4g.c 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868
  1. /*
  2. * Module_4g.c
  3. *
  4. * Created on: 2019-11-29
  5. * Update on: 2023-03-06
  6. * Author: Eason Yang / Folus Wen
  7. * Version: V0.18
  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 120 // 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.18","2023-03-06"};
  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\n");
  203. result = FAIL;
  204. }
  205. else if ((ShmOCPP16Data = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  206. {
  207. DEBUG_ERROR("shmat ShmOCPP16Data NG\n");
  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. if(actReq != NETWORK_MODE_GSM)
  1228. {
  1229. // Disable 2G band
  1230. if((strstr(Dongle.REVISION, "EC21AF") != NULL) || (strstr(Dongle.REVISION, "EC25AF") != NULL))
  1231. {
  1232. fprintf(fpDst, " OK \"AT+QCFG=\\\"band\\\",3F0,42000000000000381A\" \\\n");
  1233. }
  1234. else if((strstr(Dongle.REVISION, "EC21EU") != NULL) || (strstr(Dongle.REVISION, "EC25EU") != NULL))
  1235. {
  1236. fprintf(fpDst, " OK \"AT+QCFG=\\\"band\\\",3F0,1A0080800C5\" \\\n");
  1237. }
  1238. else if((strstr(Dongle.REVISION, "EC21J") != NULL) || (strstr(Dongle.REVISION, "EC25J") != NULL))
  1239. {
  1240. fprintf(fpDst, " OK \"AT+QCFG=\\\"band\\\",3F0,10002060085\" \\\n");
  1241. }
  1242. else
  1243. {
  1244. fprintf(fpDst, " OK \"AT+QCFG=\\\"band\\\",3F0\" \\\n");
  1245. }
  1246. }
  1247. else
  1248. {
  1249. // Enable 2G band
  1250. if((strstr(Dongle.REVISION, "EC21AF") != NULL) || (strstr(Dongle.REVISION, "EC25AF") != NULL))
  1251. {
  1252. fprintf(fpDst, " OK \"AT+QCFG=\\\"band\\\",3FF,42000000000000381A\" \\\n");
  1253. }
  1254. else if((strstr(Dongle.REVISION, "EC21EU") != NULL) || (strstr(Dongle.REVISION, "EC25EU") != NULL))
  1255. {
  1256. fprintf(fpDst, " OK \"AT+QCFG=\\\"band\\\",3FF,1A0080800C5\" \\\n");
  1257. }
  1258. else if((strstr(Dongle.REVISION, "EC21J") != NULL) || (strstr(Dongle.REVISION, "EC25J") != NULL))
  1259. {
  1260. fprintf(fpDst, " OK \"AT+QCFG=\\\"band\\\",3FF,10002060085\" \\\n");
  1261. }
  1262. else
  1263. {
  1264. fprintf(fpDst, " OK \"AT+QCFG=\\\"band\\\",3FF\" \\\n");
  1265. }
  1266. }
  1267. }
  1268. memset(str, 0x00, ARRAY_SIZE(str));
  1269. }
  1270. }
  1271. else if(Dongle.Model == DONGLE_THALES)
  1272. {
  1273. while (fgets(str, ARRAY_SIZE(str), fpOrg) != NULL)
  1274. {
  1275. fprintf(fpDst,"%s", str);
  1276. if(strstr(str, "ATE1") != NULL)
  1277. {
  1278. switch(actReq)
  1279. {
  1280. case NETWORK_MODE_GSM:
  1281. fprintf(fpDst, " OK \"AT\\^SCFG=\\\"Radio\\/Band\\/2G\\\",\\\"0000000f\\\"\" \\\n");
  1282. fprintf(fpDst, " OK \\\\d\\\\d\\\\d \\\n");
  1283. fprintf(fpDst, " \"\" \"AT\\^SCFG=\\\"Radio\\/Band\\/3G\\\",\\\"0\\\"\" \\\n");
  1284. fprintf(fpDst, " OK \\\\d\\\\d\\\\d \\\n");
  1285. fprintf(fpDst, " \"\" \"AT\\^SCFG=\\\"Radio\\/Band\\/4G\\\",\\\"0\\\",\\\"0\\\"\" \\\n");
  1286. break;
  1287. case NETWORK_MODE_UMTS:
  1288. fprintf(fpDst, " OK \"AT\\^SCFG=\\\"Radio\\/Band\\/2G\\\",\\\"0\\\"\" \\\n");
  1289. fprintf(fpDst, " OK \\\\d\\\\d\\\\d \\\n");
  1290. fprintf(fpDst, " \"\" \"AT\\^SCFG=\\\"Radio\\/Band\\/3G\\\",\\\"000400bf\\\"\" \\\n");
  1291. fprintf(fpDst, " OK \\\\d\\\\d\\\\d \\\n");
  1292. fprintf(fpDst, " \"\" \"AT\\^SCFG=\\\"Radio\\/Band\\/4G\\\",\\\"0\\\",\\\"0\\\"\" \\\n");
  1293. break;
  1294. case NETWORK_MODE_LTE:
  1295. fprintf(fpDst, " OK \"AT\\^SCFG=\\\"Radio\\/Band\\/2G\\\",\\\"0\\\"\" \\\n");
  1296. fprintf(fpDst, " OK \\\\d\\\\d\\\\d \\\n");
  1297. fprintf(fpDst, " \"\" \"AT\\^SCFG=\\\"Radio\\/Band\\/3G\\\",\\\"0\\\"\" \\\n");
  1298. fprintf(fpDst, " OK \\\\d\\\\d\\\\d \\\n");
  1299. fprintf(fpDst, " \"\" \"AT\\^SCFG=\\\"Radio\\/Band\\/4G\\\",\\\"0a0e18df\\\",\\\"00000002000001a0\\\"\" \\\n");
  1300. break;
  1301. case NETWORK_MODE_AUTO:
  1302. fprintf(fpDst, " OK \"AT\\^SCFG=\\\"Radio\\/Band\\/2G\\\",\\\"0000000f\\\"\" \\\n");
  1303. fprintf(fpDst, " OK \\\\d\\\\d\\\\d \\\n");
  1304. fprintf(fpDst, " \"\" \"AT\\^SCFG=\\\"Radio\\/Band\\/3G\\\",\\\"000400bf\\\"\" \\\n");
  1305. fprintf(fpDst, " OK \\\\d\\\\d\\\\d \\\n");
  1306. fprintf(fpDst, " \"\" \"AT\\^SCFG=\\\"Radio\\/Band\\/4G\\\",\\\"0a0e18df\\\",\\\"00000002000001a0\\\"\" \\\n");
  1307. default:
  1308. break;
  1309. }
  1310. }
  1311. memset(str, 0x00, ARRAY_SIZE(str));
  1312. }
  1313. }
  1314. fclose(fpOrg);
  1315. fclose(fpDst);
  1316. }
  1317. //==========================================
  1318. // Main process
  1319. //==========================================
  1320. int main(void)
  1321. {
  1322. unsigned char Previous_Module_Mode = 0xff;
  1323. int connectStep=0;
  1324. int preConnectStep=-1;
  1325. int delayInterval = DefaultInterval;
  1326. //==========================================
  1327. //Initialization share memory
  1328. //==========================================
  1329. if(InitShareMemory() == FAIL)
  1330. {
  1331. DEBUG_ERROR("InitShareMemory NG\n");
  1332. if(ShmStatusCodeData!=NULL)
  1333. {
  1334. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FailToCreateShareMemory=1;
  1335. }
  1336. sleep(5);
  1337. return 0;
  1338. }
  1339. DEBUG_INFO("========================================\n");
  1340. DEBUG_INFO("Latest Firmware Version : [ %s ].\n", Version_And_Date[0]);
  1341. DEBUG_INFO("Latest Upgrade Date : [ %s ].\n", Version_And_Date[1]);
  1342. for(;;)
  1343. {
  1344. if(Previous_Module_Mode != ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomEnabled)
  1345. {
  1346. Previous_Module_Mode = ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomEnabled;
  1347. DEBUG_INFO("========================================\n");
  1348. DEBUG_INFO("4G module : [ %s ]\n", (ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomEnabled?"Enable":"Disable"));
  1349. }
  1350. if(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomEnabled)
  1351. {
  1352. // Enable 4G
  1353. switch(connectStep)
  1354. {
  1355. case CONNECT_STEP_READ_MODULE_INFO:
  1356. if(preConnectStep != connectStep)
  1357. {
  1358. preConnectStep = connectStep;
  1359. DEBUG_INFO("----------------------------------------\n");
  1360. DEBUG_INFO("Into CONNECT_STEP_READ_MODULE_INFO.\n");
  1361. }
  1362. if(isReadInfo(connectStep) == PASS)
  1363. {
  1364. if(strcmp((char*)ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModelName, Dongle.MODELNAME) != 0)
  1365. memset(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModelName, 0x00, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModelName));
  1366. memcpy(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModelName, Dongle.MODELNAME, sizeof Dongle.MODELNAME);
  1367. if(strcmp((char*)ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSoftwareVer, Dongle.REVISION) != 0)
  1368. memset(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSoftwareVer, 0x00, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSoftwareVer));
  1369. memcpy(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSoftwareVer, Dongle.REVISION, sizeof Dongle.REVISION);
  1370. if(strcmp((char*)ShmSysConfigAndInfo->SysInfo.TelcomModemFwRev, Dongle.REVISION) != 0)
  1371. memset(ShmSysConfigAndInfo->SysInfo.TelcomModemFwRev, 0x00, ARRAY_SIZE(ShmSysConfigAndInfo->SysInfo.TelcomModemFwRev));
  1372. memcpy(ShmSysConfigAndInfo->SysInfo.TelcomModemFwRev, Dongle.REVISION, sizeof Dongle.REVISION);
  1373. if(strcmp((char*)ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModemImei, Dongle.IMEI) != 0)
  1374. memset(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModemImei, 0x00, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModemImei));
  1375. memcpy(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModemImei, Dongle.IMEI, sizeof Dongle.IMEI);
  1376. DEBUG_INFO("Device MANUFACTURER: %s\n", Dongle.MANUFACTURER);
  1377. DEBUG_INFO("Device MODEL: %s\n", ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModelName);
  1378. DEBUG_INFO("Device REVISION: %s\n", ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSoftwareVer);
  1379. DEBUG_INFO("Device IMEI: %s\n", ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModemImei);
  1380. Dongle.cnt_ReadSimInfoFail = 0;
  1381. connectStep = CONNECT_STEP_READ_SIM_INFO;
  1382. delayInterval = DefaultInterval;
  1383. }
  1384. else
  1385. {
  1386. DEBUG_ERROR("Device info read error fail: %d\n", Dongle.cnt_ReadInfoFail);
  1387. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectVia4Gi = 1;
  1388. ShmStatusCodeData->InfoCode.InfoEvents.bits.ApnDisconnectVia4Gi = 1;
  1389. Dongle.MODE = NET_MODE_NO_SERVICE;
  1390. memset(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModelName, 0, sizeof ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModelName);
  1391. memset(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSoftwareVer, 0, sizeof ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSoftwareVer);
  1392. memset(ShmSysConfigAndInfo->SysInfo.TelcomModemFwRev, 0, sizeof ShmSysConfigAndInfo->SysInfo.TelcomModemFwRev);
  1393. memset(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModemImei, 0, sizeof ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModemImei);
  1394. ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomRssi = 0;
  1395. ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModemMode = 0;
  1396. Dongle.cnt_ReadInfoFail++;
  1397. if(Dongle.cnt_ReadInfoFail > 3)
  1398. {
  1399. system("killall 4GDetection");
  1400. system("killall pppd");
  1401. sleep(2);
  1402. Dongle.cnt_ReadInfoFail = 0;
  1403. rstModule();
  1404. connectStep = CONNECT_STEP_CHECK_MODULE;
  1405. }
  1406. delayInterval = CheckModemInfoInterval;
  1407. }
  1408. break;
  1409. case CONNECT_STEP_READ_SIM_INFO:
  1410. if(preConnectStep != connectStep)
  1411. {
  1412. preConnectStep = connectStep;
  1413. DEBUG_INFO("----------------------------------------\n");
  1414. DEBUG_INFO("Into CONNECT_STEP_READ_SIM_INFO.\n");
  1415. }
  1416. if(isReadSimInfo() == PASS)
  1417. {
  1418. ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSimStatus = 1;
  1419. if(strcmp((char*)ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSimIccid, Dongle.ICCID) != 0)
  1420. memset(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSimIccid, 0x00, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSimIccid));
  1421. memcpy(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSimIccid, Dongle.ICCID, sizeof Dongle.ICCID);
  1422. if(strcmp((char*)ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSimImsi, Dongle.IMSI) != 0)
  1423. memset(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSimImsi, 0x00, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSimImsi));
  1424. memcpy(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSimImsi, Dongle.IMSI, sizeof Dongle.IMSI);
  1425. DEBUG_INFO("Device IMSI: %s\n", ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSimImsi);
  1426. DEBUG_INFO("Device ICCID: %.20s\n", ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSimIccid);
  1427. DEBUG_INFO("TelcomSimStatus: %d\n", ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSimStatus);
  1428. Dongle.cnt_ReadSimInfoFail = 0;
  1429. connectStep = CONNECT_STEP_CONNECTING;
  1430. delayInterval = DefaultInterval;
  1431. }
  1432. else
  1433. {
  1434. DEBUG_ERROR("SIM card info read error fail: %d\n", Dongle.cnt_ReadSimInfoFail);
  1435. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectVia4Gi = 1;
  1436. ShmStatusCodeData->InfoCode.InfoEvents.bits.ApnDisconnectVia4Gi = 1;
  1437. ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSimStatus = 0;
  1438. memset(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSimImsi, 0, sizeof ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSimImsi);
  1439. memset(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSimIccid, 0, sizeof ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSimIccid);
  1440. memset(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomIpAddress, 0 , sizeof ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomIpAddress);
  1441. Dongle.cnt_ReadSimInfoFail ++;
  1442. if(Dongle.cnt_ReadSimInfoFail > 3)
  1443. {
  1444. system("killall 4GDetection");
  1445. system("killall pppd");
  1446. sleep(2);
  1447. Dongle.cnt_ReadSimInfoFail = 0;
  1448. rstModule();
  1449. connectStep = CONNECT_STEP_CHECK_MODULE;
  1450. }
  1451. delayInterval = CheckSimInterval;
  1452. }
  1453. break;
  1454. case CONNECT_STEP_CONNECTING:
  1455. if(preConnectStep != connectStep)
  1456. {
  1457. preConnectStep = connectStep;
  1458. DEBUG_INFO("----------------------------------------\n");
  1459. DEBUG_INFO("Into CONNECT_STEP_CONNECTING.\n");
  1460. }
  1461. if(isPppUp() == PASS)
  1462. {
  1463. ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomNetworkConn = 1;
  1464. ShmStatusCodeData->InfoCode.InfoEvents.bits.ApnDisconnectVia4Gi = 0;
  1465. DEBUG_INFO("PPP IP: %s\n", ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomIpAddress);
  1466. DEBUG_INFO("Telecom network connection: %d\n", ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomNetworkConn);
  1467. DEBUG_INFO("Apn connnection status: %s\n", (ShmStatusCodeData->InfoCode.InfoEvents.bits.ApnDisconnectVia4Gi == 0)? "Connected":"Disconnected");
  1468. Dongle.cnt_pppFail = 0;
  1469. connectStep = CONNECT_STEP_CONNECTED;
  1470. delayInterval = DefaultInterval;
  1471. }
  1472. else
  1473. {
  1474. ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomNetworkConn = 0;
  1475. ShmStatusCodeData->InfoCode.InfoEvents.bits.ApnDisconnectVia4Gi = 1;
  1476. if(Load4gConfiguration() == FAIL)
  1477. {
  1478. DEBUG_ERROR("4G configuration value NG.\n");
  1479. if(ShmStatusCodeData!=NULL)
  1480. {
  1481. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.Telecom4GModuleCommFail=1;
  1482. }
  1483. }
  1484. else
  1485. {
  1486. memset(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomIpAddress, 0 , sizeof ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomIpAddress);
  1487. DEBUG_INFO("Apn connnection status: %s\n", (ShmStatusCodeData->InfoCode.InfoEvents.bits.ApnDisconnectVia4Gi == 0)? "Connected":"Disconnected");
  1488. Dongle.cnt_pppFail++;
  1489. if(Dongle.cnt_pppFail > 3)
  1490. {
  1491. system("killall 4GDetection");
  1492. system("killall pppd");
  1493. sleep(2);
  1494. Dongle.cnt_pppFail = 0;
  1495. rstModule();
  1496. connectStep = CONNECT_STEP_CHECK_MODULE;
  1497. }
  1498. system("killall 4GDetection");
  1499. system("killall pppd");
  1500. sleep(2);
  1501. /*
  1502. * TODO:
  1503. * 1. Act priority check
  1504. */
  1505. configNetworkMode(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomNetworkType);
  1506. if(Dongle.Model == DONGLE_QUECTEL)
  1507. {
  1508. system("/root/ppp/4GDetection /dev/ttyUSB3 &");
  1509. printf("4GDetection for primary device.\n");
  1510. }
  1511. else if(Dongle.Model == DONGLE_THALES)
  1512. {
  1513. system("/root/ppp/4GDetection /dev/ttyACM0 &");
  1514. printf("4GDetection for second device.\n");
  1515. }
  1516. else
  1517. {}
  1518. }
  1519. delayInterval = CheckConnectionInterval;
  1520. }
  1521. break;
  1522. case CONNECT_STEP_CONNECTED:
  1523. if(preConnectStep != connectStep)
  1524. {
  1525. preConnectStep = connectStep;
  1526. DEBUG_INFO("----------------------------------------\n");
  1527. DEBUG_INFO("Into CONNECT_STEP_CONNECTED.\n");
  1528. }
  1529. if((isReadInfo(connectStep) == PASS) && (isReachableInternet() == PASS))
  1530. {
  1531. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.Telecom4GModuleCommFail = 0;
  1532. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectVia4Gi = 0;
  1533. Dongle.cnt_InternetFail = 0;
  1534. DEBUG_INFO("Dongle internet valid result: Pass\n");
  1535. delayInterval = CheckInternetInterval;
  1536. }
  1537. else
  1538. {
  1539. DEBUG_INFO("Dongle internet valid result: Fail %d time\n", Dongle.cnt_InternetFail);
  1540. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectVia4Gi = 1;
  1541. Dongle.cnt_InternetFail++;
  1542. if((Dongle.cnt_InternetFail > 3) && !ShmSysConfigAndInfo->SysInfo.OcppConnStatus)
  1543. {
  1544. system("killall 4GDetection");
  1545. system("killall pppd");
  1546. sleep(2);
  1547. Dongle.cnt_InternetFail = 0;
  1548. rstModule();
  1549. connectStep = CONNECT_STEP_CHECK_MODULE;
  1550. }
  1551. delayInterval = DisconnInterval;
  1552. }
  1553. if(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomRssi != csq2rssi(Dongle.CSQ))
  1554. {
  1555. ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomRssi = csq2rssi(Dongle.CSQ);
  1556. DEBUG_INFO("RSSI: %d dBm (CSQ:%d)\n", ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomRssi, Dongle.CSQ);
  1557. }
  1558. if(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModemMode != Dongle.MODE)
  1559. {
  1560. ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModemMode = Dongle.MODE;
  1561. DEBUG_INFO("Device MODE: %d\n", ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModemMode);
  1562. DEBUG_INFO("Device Act: %s\n", Dongle.act);
  1563. DEBUG_INFO("Device Band: %s\n", Dongle.band);
  1564. DEBUG_INFO("Device Channel: %d\n", Dongle.channel);
  1565. }
  1566. if(Dongle.Model == DONGLE_QUECTEL)
  1567. {
  1568. DEBUG_INFO("TX counter: %d bytes\n", Dongle.bytesTx);
  1569. DEBUG_INFO("RX counter: %d bytes\n", Dongle.bytesRx);
  1570. DEBUG_INFO("Delta counter: %d bytes\n", (Dongle.bytesTx+Dongle.bytesRx)-Dongle.lastCounter);
  1571. Dongle.lastCounter = (Dongle.bytesTx+Dongle.bytesRx);
  1572. }
  1573. DEBUG_INFO("++++++++++++++++++++++++++++++++++++++++\n");
  1574. break;
  1575. default:
  1576. case CONNECT_STEP_CHECK_MODULE:
  1577. if(preConnectStep != connectStep)
  1578. {
  1579. preConnectStep = connectStep;
  1580. DEBUG_INFO("----------------------------------------\n");
  1581. DEBUG_INFO("Into CONNECT_STEP_CHECK_MODULE.\n");
  1582. }
  1583. if(((Dongle.Model = Check4GModem()) != FAIL))
  1584. {
  1585. ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModemMode = 0;
  1586. ShmStatusCodeData->FaultCode.FaultEvents.bits.Telecom4GModuleBroken = 0;
  1587. Dongle.cnt_SearchModuleFail = 0;
  1588. connectStep = CONNECT_STEP_READ_MODULE_INFO;
  1589. delayInterval = DefaultInterval;
  1590. }
  1591. else
  1592. {
  1593. DEBUG_ERROR("Device search error fail: %d\n", Dongle.cnt_SearchModuleFail);
  1594. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectVia4Gi = 1;
  1595. ShmStatusCodeData->InfoCode.InfoEvents.bits.ApnDisconnectVia4Gi = 1;
  1596. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.Telecom4GModuleCommFail = 0;
  1597. ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomNetworkConn = 0;
  1598. ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomRssi = 0;
  1599. ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSimStatus = 0;
  1600. ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModemMode = 0;
  1601. memset(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModemImei, 0, sizeof ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModemImei);
  1602. memset(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSimImsi, 0, sizeof ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSimImsi);
  1603. memset(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomIpAddress, 0, sizeof ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomIpAddress);
  1604. memset(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSimIccid, 0, sizeof ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSimIccid);
  1605. memset(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSoftwareVer, 0, sizeof ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSoftwareVer);
  1606. memset(ShmSysConfigAndInfo->SysInfo.TelcomModemFwRev, 0, sizeof ShmSysConfigAndInfo->SysInfo.TelcomModemFwRev);
  1607. memset(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModelName, 0, sizeof ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModelName);
  1608. Dongle.cnt_SearchModuleFail++;
  1609. if(Dongle.cnt_SearchModuleFail > 3)
  1610. {
  1611. DEBUG_ERROR("4G Module was broken.\n");
  1612. if(isModuleUnbind() == PASS)
  1613. {
  1614. isModuleBind();
  1615. }
  1616. ShmStatusCodeData->FaultCode.FaultEvents.bits.Telecom4GModuleBroken = 1;
  1617. Dongle.cnt_SearchModuleFail = 0;
  1618. }
  1619. delayInterval = CheckModemInterval;
  1620. }
  1621. break;
  1622. }
  1623. }
  1624. else
  1625. {
  1626. // Disable 4G
  1627. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectVia4Gi = 1;
  1628. ShmStatusCodeData->InfoCode.InfoEvents.bits.ApnDisconnectVia4Gi= 0;
  1629. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.Telecom4GModuleCommFail = 0;
  1630. ShmStatusCodeData->FaultCode.FaultEvents.bits.Telecom4GModuleBroken = 0;
  1631. ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomNetworkConn = 0;
  1632. ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomRssi = 0;
  1633. ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSimStatus = 0;
  1634. ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModemMode = 0;
  1635. memset(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModemImei, 0, sizeof ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModemImei);
  1636. memset(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSimImsi, 0, sizeof ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSimImsi);
  1637. memset(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomIpAddress, 0, sizeof ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomIpAddress);
  1638. memset(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSimIccid, 0, sizeof ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSimIccid);
  1639. memset(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSoftwareVer, 0, sizeof ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSoftwareVer);
  1640. memset(ShmSysConfigAndInfo->SysInfo.TelcomModemFwRev, 0, sizeof ShmSysConfigAndInfo->SysInfo.TelcomModemFwRev);
  1641. memset(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModelName, 0, sizeof ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModelName);
  1642. if(system("pidof -s 4GDetection > /dev/null") == 0)
  1643. {
  1644. DEBUG_INFO("4GDetection running, stop it.\n");
  1645. system("killall 4GDetection");
  1646. }
  1647. if(system("pidof -s pppd > /dev/null") == 0)
  1648. {
  1649. DEBUG_INFO("pppd running, stop it.\n");
  1650. system("killall pppd");
  1651. }
  1652. if(isPppUp() == PASS)
  1653. {
  1654. system("ifconfig ppp0 down");
  1655. }
  1656. connectStep = CONNECT_STEP_CHECK_MODULE;
  1657. delayInterval = DefaultInterval;
  1658. }
  1659. sleep(delayInterval);
  1660. }
  1661. return 0;
  1662. }