4gModem.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  1. #include <sys/types.h>
  2. #include <sys/stat.h>
  3. #include <fcntl.h>
  4. #include <linux/termios.h>
  5. #include <stdio.h>
  6. #include <string.h>
  7. #include <time.h>
  8. #include <stdlib.h>
  9. #include <sys/ipc.h>
  10. #include <sys/shm.h>
  11. #include <sys/mman.h>
  12. #include <linux/sockios.h>
  13. #include <linux/socket.h>
  14. #include <sys/socket.h>
  15. #include <sys/time.h>
  16. #include <sys/timeb.h>
  17. #include <netinet/in.h>
  18. #include <unistd.h>
  19. #include "define.h"
  20. //#define debug
  21. #define CheckModemInterval 30 //sec
  22. #define CheckSimInterval 10 //sec
  23. #define CheckConnectionInterval 5 //sec
  24. #define DisconnInterval 60 //sec
  25. struct SysConfigAndInfo *ShmSysConfigAndInfo;
  26. struct StatusCodeData *ShmStatusCodeData;
  27. struct FanModuleData *ShmFanModuleData;
  28. #ifdef SystemLogMessage
  29. int StoreLogMsg(unsigned char *DataString)
  30. {
  31. unsigned char Buf[256];
  32. time_t CurrentTime;
  33. struct tm *tm;
  34. memset(Buf,0,sizeof(Buf));
  35. CurrentTime = time(NULL);
  36. tm=localtime(&CurrentTime);
  37. sprintf(Buf,"echo \"%04d.%02d.%02d %02d:%02d:%02d - %s\" >> /Storage/SystemLog/[%04d.%02d]SystemLog",
  38. tm->tm_year+1900,tm->tm_mon+1,tm->tm_mday,tm->tm_hour,tm->tm_min,tm->tm_sec,
  39. DataString,
  40. tm->tm_year+1900,tm->tm_mon+1);
  41. system(Buf);
  42. #ifdef Debug
  43. printf("%s \n",DataString);
  44. #endif
  45. }
  46. #endif
  47. int DiffTimeb(struct timeb ST, struct timeb ET)
  48. {
  49. //return milli-second
  50. unsigned int StartTime,StopTime;
  51. StartTime=(unsigned int)ST.time;
  52. StopTime=(unsigned int)ET.time;
  53. return (StopTime-StartTime)*1000+ET.millitm-ST.millitm;
  54. }
  55. /**************************************************************************************/
  56. /**************************Init all share memory *********************************/
  57. /**************************************************************************************/
  58. int InitShareMemory()
  59. {
  60. int MeterSMId;
  61. //creat ShmSysConfigAndInfo
  62. if ((MeterSMId = shmget(ShmSysConfigAndInfoKey, sizeof(struct SysConfigAndInfo), 0777)) < 0)
  63. {
  64. #ifdef SystemLogMessage
  65. StoreLogMsg("[4gModem]InitShareMemory:shmget ShmSysConfigAndInfo NG");
  66. #endif
  67. return 0;
  68. }
  69. else if ((ShmSysConfigAndInfo = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  70. {
  71. #ifdef SystemLogMessage
  72. StoreLogMsg("[4gModem]InitShareMemory:shmat ShmSysConfigAndInfo NG");
  73. #endif
  74. return 0;
  75. }
  76. //creat ShmStatusCodeData
  77. if ((MeterSMId = shmget(ShmStatusCodeKey, sizeof(struct StatusCodeData), 0777)) < 0)
  78. {
  79. #ifdef SystemLogMessage
  80. StoreLogMsg("[4gModem]InitShareMemory:shmget ShmStatusCodeData NG");
  81. #endif
  82. return 0;
  83. }
  84. else if ((ShmStatusCodeData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  85. {
  86. #ifdef SystemLogMessage
  87. StoreLogMsg("[4gModem]InitShareMemory:shmat ShmStatusCodeData NG");
  88. #endif
  89. return 0;
  90. }
  91. return 1;
  92. }
  93. int Check4GModem()
  94. {
  95. //return 0: No 4G modem equipped
  96. //return 1: Qutel 4G modem equipped
  97. //return 2: ublox 4G modem equipped
  98. int Rtn1,Rtn2;
  99. //check Qutel 4G modem
  100. Rtn1=Rtn2=-1;
  101. Rtn1=access("/dev/ttyUSB0",R_OK);
  102. if(Rtn1==0)
  103. {
  104. Rtn2=access("/dev/ttyUSB2",R_OK);
  105. if(Rtn2==0)
  106. {
  107. #ifdef SystemLogMessage
  108. StoreLogMsg("[4gModem]Check4GModem: Qutel 4G modem be found");
  109. #endif
  110. return 1;
  111. }
  112. }
  113. //check ublox 4G modem
  114. Rtn1=Rtn2=-1;
  115. Rtn1=access("/dev/ttyACM0",R_OK);
  116. if(Rtn1==0)
  117. {
  118. Rtn2=access("/dev/ttyACM2",R_OK);
  119. if(Rtn2==0)
  120. {
  121. #ifdef SystemLogMessage
  122. StoreLogMsg("[4gModem]Check4GModem: ublox 4G modem be found");
  123. #endif
  124. return 2;
  125. }
  126. }
  127. #ifdef SystemLogMessage
  128. StoreLogMsg("[4gModem]Check4GModem: No 4G modem be found");
  129. #endif
  130. return 0;
  131. }
  132. int InitComPort(unsigned char ModemModel)
  133. {
  134. int UsbFd;
  135. struct termios tios;
  136. if(ModemModel==1) //Qutel 4G modem equipped
  137. UsbFd = open("/dev/ttyUSB2", O_RDWR|O_NOCTTY);
  138. else if(ModemModel==2) //ublox 4G modem equipped
  139. UsbFd = open("/dev/ttyACM2", O_RDWR|O_NOCTTY);
  140. else
  141. {
  142. #ifdef SystemLogMessage
  143. StoreLogMsg("[4gModem]InitComPort: Unknow ModemModel ");
  144. #endif
  145. return -1;
  146. }
  147. if(UsbFd<0)
  148. {
  149. #ifdef SystemLogMessage
  150. StoreLogMsg("[4gModem]InitComPort: UsbFd open failed");
  151. #endif
  152. return -1;
  153. }
  154. ioctl (UsbFd, TCGETS, &tios);
  155. tios.c_cflag = B115200 | CS8 | CLOCAL | CREAD;
  156. tios.c_lflag = 0;
  157. tios.c_iflag = 0;
  158. tios.c_oflag = 0;
  159. tios.c_cc[VMIN]=0;
  160. tios.c_cc[VTIME]=100;
  161. tios.c_lflag=0;
  162. tcflush(UsbFd, TCIFLUSH);
  163. ioctl (UsbFd, TCSETS, &tios);
  164. return UsbFd;
  165. }
  166. void GetModelName(int Fd, unsigned char *ModelName)
  167. {
  168. //AT command processing
  169. }
  170. void GetModelVersion(int Fd, unsigned char *ModelVersion)
  171. {
  172. //AT command processing
  173. }
  174. void GetModelImei(int Fd, unsigned char *ModelImei)
  175. {
  176. //AT command processing
  177. }
  178. void GetSimImsi(int Fd, unsigned char *SimImsi)
  179. {
  180. //AT command processing
  181. }
  182. unsigned char GetModemMode(int Fd)
  183. {
  184. int Mode;
  185. //AT command processing
  186. return Mode;
  187. }
  188. int GetSignalRssi(int Fd)
  189. {
  190. int RssiValue;
  191. //AT command processing
  192. return RssiValue;
  193. }
  194. int GetPPP0Info(unsigned char *IPaddress)
  195. {
  196. int fd,rd=0;
  197. unsigned int address;
  198. char buf[128],addr[32],*sptr,*eptr;
  199. system("ifconfig ppp0 | grep \"inet addr:\" > /mnt/GetPPP0Info");
  200. fd = open("/mnt/GetPPP0Info", O_RDONLY);
  201. if(fd<0)
  202. {
  203. system("rm -f /mnt/GetPPP0Info");
  204. return 0;
  205. }
  206. memset(addr,0,sizeof(addr));
  207. memset(buf,0,sizeof(buf));
  208. rd=read(fd,buf,sizeof(buf));
  209. if(rd<=0)
  210. {
  211. close(fd);
  212. system("rm -f /mnt/GetPPP0Info");
  213. return 0;
  214. }
  215. close(fd);
  216. if((sptr=strstr(buf,"inet addr:"))==NULL)
  217. {
  218. close(fd);
  219. system("rm -f /mnt/GetPPP0Info");
  220. return 0;
  221. }
  222. sptr+=strlen("inet addr:");
  223. if((eptr=strstr(buf," P-t-P:"))==NULL)
  224. {
  225. close(fd);
  226. system("rm -f /mnt/GetPPP0Info");
  227. return 0;
  228. }
  229. memset(IPaddress,0,strlen(IPaddress));
  230. strncpy(IPaddress,sptr,eptr-sptr);
  231. #ifdef SystemLogMessage
  232. {
  233. unsigned char Buffer[128];
  234. memset(Buffer,0,sizeof(Buffer));
  235. sprintf(Buffer,"[4gModem]GetPPP0Info: 4G IP Address = %s",IPaddress);
  236. StoreLogMsg(Buffer);
  237. }
  238. #endif
  239. return 1;
  240. }
  241. int Load4gConfiguration()
  242. {
  243. unsigned char CopyTmp[128];
  244. if(strlen(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomApn)<=0)
  245. return 0;
  246. system("cat /dev/null > /root/ppp/auto-apn.conf");
  247. system("chmod 777 /root/ppp/auto-apn.conf");
  248. system("cat /dev/null > /etc/ppp/options");
  249. system("chmod 777 /etc/ppp/options");
  250. system("cat /dev/null > /etc/ppp/pap-secrets");
  251. system("chmod 777 /etc/ppp/pap-secrets");
  252. system("cat /dev/null > /etc/ppp/chap-secrets");
  253. system("chmod 777 /etc/ppp/chap-secrets");
  254. memset(CopyTmp,0,sizeof(CopyTmp));
  255. sprintf(CopyTmp,"echo \"APN=\"%s\"\" > /root/ppp/auto-apn.conf",ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomApn);
  256. system(CopyTmp);
  257. system("echo \"ACCOUNT=\" >> /root/ppp/auto-apn.conf");
  258. system("echo \"PASSWORD=\" >> /root/ppp/auto-apn.conf");
  259. if(strlen(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomChapPapId)>0)
  260. {
  261. memset(CopyTmp,0,sizeof(CopyTmp));
  262. sprintf(CopyTmp,"echo \"name %s \" > /etc/ppp/options",ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomChapPapId);
  263. system(CopyTmp);
  264. memset(CopyTmp,0,sizeof(CopyTmp));
  265. sprintf(CopyTmp,"echo \"%s * %s \" > /etc/ppp/pap-secrets",
  266. ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomChapPapId,
  267. ((strlen(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomChapPapPwd)>0)?ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomChapPapPwd : " "));
  268. system(CopyTmp);
  269. memset(CopyTmp,0,sizeof(CopyTmp));
  270. sprintf(CopyTmp,"echo \"%s * %s \" > /etc/ppp/chap-secrets",
  271. ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomChapPapId,
  272. ((strlen(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomChapPapPwd)>0)?ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomChapPapPwd : " "));
  273. system(CopyTmp);
  274. }
  275. return 1;
  276. }
  277. void ResetModem()
  278. {
  279. //power cycle modem by GPIO
  280. }
  281. /**************************************************************/
  282. /************** main function********************************/
  283. /*************************************************************/
  284. int main(int argc,char *argv[])
  285. {
  286. unsigned char ModuleModel=0;//0: None, 1: Qutel, 2: ublox SARA-U201
  287. int UsbFd=-1;
  288. unsigned int StartTime,EndTime,Tmp;
  289. unsigned char TmpIpAddr[16];
  290. //Initialization
  291. if(InitShareMemory()==0)
  292. {
  293. #ifdef SystemLogMessage
  294. StoreLogMsg("[4gModem]main:InitShareMemory NG");
  295. #endif
  296. if(ShmStatusCodeData!=NULL)
  297. {
  298. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FailToCreateShareMemory=1;
  299. }
  300. sleep(5);
  301. return 0;
  302. }
  303. ResetModem();
  304. ReCheckModem:
  305. //ResetModem();
  306. UsbFd=-1;
  307. ModuleModel=0;
  308. Load4gConfiguration();
  309. while(1)
  310. {
  311. ModuleModel=Check4GModem();
  312. if(ModuleModel<=0)
  313. {
  314. sleep(CheckModemInterval);
  315. continue;
  316. }
  317. if(UsbFd>0)
  318. close(UsbFd);
  319. UsbFd=InitComPort(ModuleModel);
  320. if(UsbFd<0)
  321. {
  322. sleep(CheckModemInterval);
  323. continue;
  324. }
  325. /**** Record some modem information and dial up if SIM card ready ****/
  326. memset(&ShmSysConfigAndInfo->SysConfig.TelecomInterface,0,sizeof(struct TeleConfigData));
  327. while(1)
  328. {
  329. if(strlen(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModelName)<=0)
  330. GetModelName(UsbFd,ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModelName);
  331. if(strlen(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSoftwareVer)<=0)
  332. GetModelVersion(UsbFd,ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSoftwareVer);
  333. if(strlen(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModemImei)<=0)
  334. GetModelImei(UsbFd,ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModemImei);
  335. GetSimImsi(UsbFd,ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSimImsi);
  336. if(strlen(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSimImsi)<=0)
  337. {
  338. ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSimStatus=0;
  339. sleep(CheckSimInterval);
  340. if(Check4GModem()<=0)
  341. goto ReCheckModem;
  342. }
  343. else
  344. {
  345. ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSimStatus=1;
  346. if(strlen(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomApn)<=0)
  347. {
  348. sleep(DisconnInterval);
  349. goto ReCheckModem;
  350. }
  351. system("killall 4GDetection");
  352. sleep(2);
  353. if(ModuleModel==1)
  354. system("/root/ppp/4GDetection /dev/ttyUSB0 &");
  355. else if(ModuleModel==2)
  356. system("/root/ppp/4GDetection /dev/ttyACM0 &");
  357. break;
  358. }
  359. }
  360. /***** Periodically check RSSI and connection*****/
  361. StartTime=time((time_t*)NULL);
  362. EndTime=time((time_t*)NULL);
  363. while(1)
  364. {
  365. if((time((time_t*)NULL)-StartTime)>=CheckConnectionInterval)
  366. {
  367. //check IP address
  368. memset(TmpIpAddr,0,sizeof(TmpIpAddr));
  369. if(GetPPP0Info(TmpIpAddr)<=0)
  370. {
  371. ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomNetworkConn=0;
  372. if((time((time_t*)NULL)-EndTime)>=DisconnInterval)
  373. goto ReCheckModem;
  374. }
  375. else
  376. {
  377. ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomNetworkConn=1;
  378. if(strstr(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomIpAddress,TmpIpAddr)==NULL)
  379. {
  380. #ifdef SystemLogMessage
  381. {
  382. unsigned char Buffer[128];
  383. memset(Buffer,0,sizeof(Buffer));
  384. sprintf(Buffer,"[4gModem]main: 4G IP Address changed from %s to %s",ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomIpAddress,TmpIpAddr);
  385. StoreLogMsg(Buffer);
  386. }
  387. #endif
  388. memset(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomIpAddress,0,sizeof(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomIpAddress));
  389. strcpy(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomIpAddress,TmpIpAddr);
  390. system("route del default");
  391. system("route add default dev ppp0");
  392. }
  393. EndTime=time((time_t*)NULL);
  394. }
  395. //check RSSI
  396. Tmp=GetSignalRssi(UsbFd);
  397. if(abs(Tmp-ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomRssi)>=5)
  398. {
  399. #ifdef SystemLogMessage
  400. {
  401. unsigned char Buffer[128];
  402. memset(Buffer,0,sizeof(Buffer));
  403. sprintf(Buffer,"[4gModem]main: RSSI changed from %d to %d",ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomRssi,Tmp);
  404. StoreLogMsg(Buffer);
  405. }
  406. #endif
  407. ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomRssi=Tmp;
  408. }
  409. //check Mode
  410. Tmp=GetModemMode(UsbFd);
  411. if(Tmp!=ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModemMode)
  412. {
  413. #ifdef SystemLogMessage
  414. {
  415. unsigned char Buffer[128];
  416. memset(Buffer,0,sizeof(Buffer));
  417. sprintf(Buffer,"[4gModem]main: Modem mode changed from %d to %d",ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModemMode,Tmp);
  418. StoreLogMsg(Buffer);
  419. }
  420. #endif
  421. ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModemMode=Tmp;
  422. }
  423. StartTime=time((time_t*)NULL);
  424. }
  425. }
  426. }//main while loop
  427. }