MainTask.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665
  1. /********************************************************************************
  2. * File Name: MainTask.c
  3. * Function Describe:The main task for the system
  4. * Relate Module:
  5. * Explain: Hardware version is HS121
  6. * Writer: ShiLiangWen
  7. * Date: 2015.7.8
  8. *******************************************************************************/
  9. #define THIS_FILE_ID 7
  10. //---------------------------------------------------------------------------------
  11. #include "includes.h"
  12. //OS ID
  13. OS_TID idMainTask=0;
  14. U64 stkMainTask[MAIN_TASK_STK_SIZE];
  15. U32 g_ulSysTick=0;
  16. SUT_POSITION sutPosition;
  17. unsigned char g_ucUart1Activated=0;
  18. void FingerDaemonfinger(void);
  19. //======================================
  20. //-----
  21. #ifdef USE_RUNNING_MAKE
  22. void RunningMake(unsigned short FileID,unsigned long line)
  23. {
  24. g_usFileID=FileID;
  25. g_ulFileLine=line;
  26. }
  27. #endif
  28. unsigned short g_usFileID=0;
  29. unsigned long g_ulFileLine=0;
  30. unsigned char g_ucSystemReset=0;
  31. /***********************************************
  32. 强制复位
  33. ************************************************/
  34. void SystemReboot(void)
  35. {
  36. g_ucSystemReset=1;
  37. SlwTrace(DEBUG,"System reboot now!\r\n");
  38. while(1);
  39. }
  40. /*************************************************************************
  41. 拷贝数据,并判断与原数据是否相同
  42. 相同返回0
  43. 不同返回1
  44. **************************************************************************/
  45. unsigned char CopyData(unsigned char *pDst,unsigned char *pSrc,unsigned char len)
  46. {
  47. unsigned char i;
  48. unsigned char flag=0;
  49. for(i=0;i<len;i++){
  50. if(*pDst!=*pSrc){
  51. flag=1;
  52. *pDst=*pSrc;
  53. }
  54. pDst++;
  55. pSrc++;
  56. }
  57. return flag;
  58. }
  59. /*
  60. */
  61. unsigned char AscToHex(unsigned char aHex)
  62. {
  63. if((aHex>=0)&&(aHex<=9))
  64. aHex += 0x30;
  65. else if((aHex>=10)&&(aHex<=15))//A-F
  66. //aHex += 0x37;
  67. aHex += 0x57;
  68. else aHex = 0xff;
  69. return aHex;
  70. }
  71. void AscStrToHexStr(char *AscStr, char *HexStr)
  72. {
  73. char *pAscStr=AscStr;
  74. unsigned char d,h,l;
  75. while(0!=(d=(unsigned char)*pAscStr++)){
  76. l=d&0x0f;
  77. h=d>>4;
  78. *HexStr++=AscToHex(h);
  79. *HexStr++=AscToHex(l);
  80. }
  81. *HexStr=0;
  82. }
  83. void PcMsgHandle(char *msg)
  84. {
  85. const char *CmdList[]={
  86. "TM", //调试Modem
  87. "TG", //调试GPS
  88. "DEBUGLOG",//打印LOG
  89. "SSIM", //设置SIM卡配置信息
  90. "RSIM", //读取SIM卡配置信息
  91. "RVER",//读取版本号
  92. "RSERVER",//读取服务器信息
  93. "SETID",//设置PSN和AKEY
  94. "RPSN",//读PSN
  95. "REBOOT",//重启
  96. // "TEST",
  97. // "FASTTEST",
  98. // "GNSS",
  99. "RPNAME",
  100. "SPNAME",
  101. "RDNAME",
  102. "SDNAME",
  103. "RASER",
  104. "SASER",
  105. ""//必须以空字串为结束
  106. };
  107. char needsave=0;
  108. unsigned short Port;
  109. unsigned long PSN,AKEY;
  110. char buf[150];
  111. char tracebuf[50];
  112. char bufIP[6];
  113. //char strAccount[20];
  114. //char strPassword[20];
  115. int i,j,temp,len;
  116. char flag;
  117. const char *pCmd;
  118. char *pMsg;
  119. int CmdListNum=0;
  120. RunMake(THIS_FILE_ID);
  121. pMsg = msg;
  122. while(*CmdList[CmdListNum++]);//计算命令列表个数
  123. CmdListNum--;
  124. RunMake(THIS_FILE_ID);
  125. //查找与命令列表相同的命令位置
  126. for(i=0;i<CmdListNum;i++){
  127. pCmd=CmdList[i];
  128. pMsg=&msg[3];
  129. flag=0;
  130. j=0;
  131. while(*pCmd){
  132. if(*pCmd!=*pMsg){
  133. flag=1;
  134. break;
  135. }
  136. pCmd++;
  137. pMsg++;
  138. j++;
  139. }
  140. if(!flag){
  141. break;
  142. }
  143. }
  144. RunMake(THIS_FILE_ID);
  145. if(i==CmdListNum)return;
  146. //处理命令
  147. switch(i)
  148. {
  149. case 0://"RT+TM"
  150. SlwTrace(INF,"RT+TM:OK\r\n");
  151. ComSelect(COM_SEL_MODEM);
  152. break;
  153. case 1://"RT+TG"
  154. SlwTrace(INF,"RT+TG:OK\r\n");
  155. ComSelect(COM_SEL_GPS);
  156. break;
  157. case 2://"RT+DEBUGLOG"
  158. if(sutDeviceConfig.DebugPrintEn == 0){
  159. SlwTrace(INF,"RT+DEBUGLOG:ON\r\n");
  160. sutDeviceConfig.DebugPrintEn=1;
  161. }else{
  162. SlwTrace(INF,"RT+DEBUGLOG:OFF\r\n");
  163. sutDeviceConfig.DebugPrintEn=0;
  164. }
  165. SaveDeviceConfigToFlash();
  166. break;
  167. case 3://RT+SSIM=APN=CMNET;NAME=card;PASS=card;
  168. len=GetParaFromStr(msg,"APN",buf);
  169. if(len<1 || len>=20){
  170. SlwTrace(INF,"RT+SSIM:FAIL[APN Invalid]\r\n");
  171. break;
  172. }else strcpy(sutProductPara.CardAPN,buf);
  173. len=GetParaFromStr(msg,"NAME",buf);
  174. if(len<1 || len>=20){
  175. SlwTrace(INF,"RT+SSIM:FAIL[NAME Invalid]\r\n");
  176. break;
  177. }else strcpy(sutProductPara.CardUsrName,buf);
  178. len=GetParaFromStr(msg,"PASS",buf);
  179. if(len<1 || len>=20){
  180. SlwTrace(INF,"RT+SSIM:FAIL[PASS Invalid]\r\n");
  181. break;
  182. }else{
  183. strcpy(sutProductPara.CardUsrPass,buf);
  184. }
  185. SaveProductParaToFlash();
  186. SlwTrace(INF,"Set SIM Info OK!\r\n");
  187. break;
  188. case 4://RT+RSIM
  189. sprintf(buf,"APN=%s;USR=%s;PASS=%s\r\n",sutProductPara.CardAPN,sutProductPara.CardUsrName,sutProductPara.CardUsrPass);
  190. SlwTrace(INF,buf);
  191. break;
  192. case 5://RT+RVER
  193. sprintf(buf,"HVer=%d SVer=%d\r\n",sutProductPara.HardwareVer,sutProductPara.SoftwareVer);
  194. SlwTrace(INF,buf);
  195. break;
  196. case 6://RT+RSERVER
  197. sprintf(buf,"AppS:%s:%d\r\n",sutProductPara.AppServer,sutProductPara.AppPort);
  198. SlwTrace(INF,buf);
  199. sprintf(buf,"ServerIP:%d.%d.%d.%d:%d\r\n",sutServerIp.AppServerIP[0],sutServerIp.AppServerIP[1],sutServerIp.AppServerIP[2],sutServerIp.AppServerIP[3],sutProductPara.AppPort);
  200. SlwTrace(INF,buf);
  201. sprintf(buf,"FotaS:%s:%d\r\n",sutProductPara.FotaServer,sutProductPara.FotaPort);
  202. SlwTrace(INF,buf);
  203. sprintf(buf,"FotaIP:%d.%d.%d.%d:%d\r\n",sutServerIp.FotaServerIP[0],sutServerIp.FotaServerIP[1],sutServerIp.FotaServerIP[2],sutServerIp.FotaServerIP[3],sutProductPara.FotaPort);
  204. SlwTrace(INF,buf);
  205. break;
  206. case 7://RT+SETID=PSN=190310005;AKEY=1412652393
  207. len=GetParaFromStr(msg,"PSN",buf);
  208. if(len<1 || len>10){
  209. SlwTrace(INF,"RT+SETID:FAIL[PSN Invalid]\r\n");
  210. break;
  211. }
  212. PSN=atoll(buf);
  213. if(PSN==0){
  214. SlwTrace(INF,"RT+SETID:FAIL[PSN Invalid]\r\n");
  215. break;
  216. }
  217. len=GetParaFromStr(msg,"AKEY",buf);
  218. if(len<1 || len>10){
  219. SlwTrace(INF,"RT+SETID:FAIL[AKEY Invalid]\r\n");
  220. break;
  221. }
  222. AKEY=atoll(buf);
  223. if(AKEY!=getAKeyByPSN(PSN)){
  224. SlwTrace(INF,"PSN=0\r\n");
  225. return;
  226. }
  227. sutProductPara.PSN = PSN;
  228. sutProductPara.AKEY = AKEY;
  229. memset(sutProductPara.DeviceID,0,sizeof(sutProductPara.DeviceID));
  230. DecToBCD(sutProductPara.PSN,&sutProductPara.DeviceID[1],5);
  231. sutProductPara.MAC[0]=((MY_MAC_HEAD&0xFF0000)>>16);
  232. sutProductPara.MAC[1]=((MY_MAC_HEAD&0x00FF00)>>8);
  233. sutProductPara.MAC[2]=((MY_MAC_HEAD&0x0000FF));
  234. sutProductPara.MAC[3]=((sutProductPara.PSN&0xFF0000)>>16);
  235. sutProductPara.MAC[4]=((sutProductPara.PSN&0x00FF00)>>8);
  236. sutProductPara.MAC[5]=(sutProductPara.PSN&0x0000FF);
  237. SaveProductParaToFlash();
  238. ReadProductParaFromFlash();
  239. sprintf(buf,"PSN=%lu\r\nAKEY=%lu\r\n",sutProductPara.PSN,sutProductPara.AKEY);
  240. SlwTrace(INF,buf);
  241. sprintf(buf,"DeviceID=%x%02x%02x%02x%02x\r\n",sutProductPara.DeviceID[1],sutProductPara.DeviceID[2],
  242. sutProductPara.DeviceID[3],sutProductPara.DeviceID[4],sutProductPara.DeviceID[5]);
  243. SlwTrace(INF,buf);
  244. sprintf(buf,"MAC=%02X.%02X.%02X.%02X.%02X.%02X\r\n",sutProductPara.MAC[0],sutProductPara.MAC[1],sutProductPara.MAC[2],
  245. sutProductPara.MAC[3],sutProductPara.MAC[4],sutProductPara.MAC[5]);
  246. SlwTrace(INF,buf);
  247. break;
  248. case 8://RT+RPSN
  249. sprintf(buf,"PSN=%ld\r\n",sutProductPara.PSN);
  250. SlwTrace(INF,buf);
  251. break;
  252. case 9://REBOOT
  253. SystemReboot();
  254. break;
  255. #if 0
  256. case 10://TEST
  257. // sprintf(buf,"RTC:%d-%d-%d %d:%d:%d\r\n",sutRTC.Years,sutRTC.Months,sutRTC.Days,sutRTC.Hours,sutRTC.Minute,sutRTC.Seconds);
  258. // SlwTrace(INF,buf);
  259. // if(sutGpsInfo.isGpsValid){
  260. // sprintf(buf,"GPS:[A] lat=%ld lng=%ld\r\n",sutGpsInfo.Latitude,sutGpsInfo.Longitude);
  261. // }else{
  262. // sprintf(buf,"GPS:[V] lat=%ld lng=%ld\r\n",sutGpsInfo.Latitude,sutGpsInfo.Longitude);
  263. // }
  264. // SlwTrace(INF,buf);
  265. if(!sutGpsInfo.isGpsValid){//GPS模块未定位
  266. SlwTrace(INF,"Test:GPS Fail!\r\n");
  267. }else if(sutWLanStatus.Step<WL_STEP_IDEL){//网络模块DHCP未分配内网IP
  268. SlwTrace(INF,"Test:RJ45 Fail!\r\n");
  269. }else if(sutModemStatus.ServerStatus!=OPENED){//4G模块未登陆过服务器
  270. SlwTrace(INF,"Test:4G Fail!\r\n");
  271. }else SlwTrace(INF,"Test:OK!\r\n");
  272. break;
  273. case 11://FASTTEST
  274. if(!sutGpsInfo.isGpsWork){//GPS模块未工作
  275. SlwTrace(INF,"FastTest:GPS Error!\r\n");
  276. }else if(sutWLanStatus.Step<WL_STEP_GET_PHYLINK){//网络模块未工作
  277. SlwTrace(INF,"FastTest:RJ45 Error!\r\n");
  278. }else if(sutModemStatus.OnceWorked==0){//4G模块未曾工作过
  279. SlwTrace(INF,"FastTest:4G Error!\r\n");
  280. }else SlwTrace(INF,"FastTest:OK!\r\n");
  281. break;
  282. case 12://GNSS
  283. sprintf(buf,"GNSS=%lu,%lu\r\n",sutGpsInfo.Longitude,sutGpsInfo.Latitude);
  284. SlwTrace(INF,buf);
  285. //sprintf(buf,"GNSS=%0.6f,%0.6f\r\n",(double)sutGpsInfo.Longitude/1000000.0,(double)sutGpsInfo.Latitude/1000000.0);
  286. //SlwTrace(INF,buf);
  287. break;
  288. #endif
  289. case 10://RPNAME
  290. sprintf(buf, "RPNAME=%s\r\n", sutProductPara.proName);
  291. SlwTrace(INF, buf);
  292. break;
  293. case 11://RT+SPNAME=
  294. memset(sutProductPara.proName, 0, sizeof(sutProductPara.proName));
  295. for(i=0;i<strlen(msg);i++) if(msg[i]=='\r') msg[i]=0;
  296. len=strlen(msg+10);
  297. if(len>=sizeof(sutProductPara.proName)) len=sizeof(sutProductPara.proName)-1;
  298. memcpy(sutProductPara.proName, msg+10,len);
  299. SaveProductParaToFlash();
  300. SlwTrace(INF, "RT+SPNAME:OK\r\n");
  301. break;
  302. case 12://RDNAME
  303. sprintf(buf, "RDNAME=%s\r\n", sutProductPara.devName);
  304. SlwTrace(INF, buf);
  305. break;
  306. case 13://SDNAME
  307. memset(sutProductPara.devName, 0, sizeof(sutProductPara.devName));
  308. for(i=0;i<strlen(msg);i++) if(msg[i]=='\r') msg[i]=0;
  309. len=strlen(msg+10);
  310. if(len>=sizeof(sutProductPara.devName)) len=sizeof(sutProductPara.devName)-1;
  311. memcpy(sutProductPara.devName, msg+10,len);
  312. SaveProductParaToFlash();
  313. SlwTrace(INF, "RT+SDNAME:OK\r\n");
  314. break;
  315. case 14://RASER
  316. sprintf(buf, "RT+RASER=%s,%d\r\n", sutProductPara.authServer, sutProductPara.authPort);
  317. SlwTrace(INF, buf);
  318. break;
  319. case 15://SASER
  320. len=GetParaFromStr(msg,"IP",buf);
  321. if(len<1 || len>=20){
  322. SlwTrace(INF,"RT+SASER:FAIL[IP Invalid]\r\n");
  323. break;
  324. }else{
  325. if(0!=strcmp(sutProductPara.authServer, buf)){
  326. strcpy(sutProductPara.authServer,buf);
  327. needsave=1;
  328. }
  329. }
  330. len=GetParaFromStr(msg,"Port",buf);
  331. if(len<1 || len> 5){
  332. SlwTrace(INF,"RT+SASER:FAIL[Port Invalid]\r\n");
  333. break;
  334. }else{
  335. if(sutProductPara.authPort != atoi(buf)){
  336. sutProductPara.authPort=atoi(buf);
  337. needsave=1;
  338. }
  339. }
  340. if(needsave != 0) SaveProductParaToFlash();
  341. snprintf(buf, sizeof(buf), "RT+SASER:IP=%s;Port=%d\r\n", sutProductPara.authServer, sutProductPara.authPort);
  342. SlwTrace(INF, buf);
  343. break;
  344. }
  345. RunMake(THIS_FILE_ID);
  346. //AB CD E5 02 54 4D 32 5C
  347. if(msg[0]=='T' && msg[1]=='M'){//0x544D
  348. printf("----TM----\r\n");
  349. ComSelect(COM_SEL_MODEM);
  350. }
  351. }
  352. void ShowHex(unsigned char *pData,unsigned char len)
  353. {
  354. char temp[5];
  355. char buf[100];
  356. int i;
  357. if(len>(sizeof(buf)-3))len=(sizeof(buf)-3);
  358. buf[0]=0;
  359. for(i=0;i<len;i++){
  360. sprintf(temp,"%02X",(int)pData[i]);
  361. strcat(buf,temp);
  362. }
  363. strcat(buf,"\r\n");
  364. SlwTrace(DEBUG,buf);
  365. }
  366. /*******************************************************************
  367. *MainTask
  368. *主任务
  369. ********************************************************************/
  370. __task void MainTask(void)
  371. {
  372. int r;
  373. char tracebuf[20];
  374. unsigned char SecCt=0;
  375. static unsigned char sucRefreshTimeCt=0;
  376. static unsigned long lastTick=100;
  377. unsigned long curTick;
  378. //Obtain own system task identification number. */
  379. idMainTask = os_tsk_self();
  380. ReadProductParaFromFlash();//读取产品参数
  381. Uart1Init(0);
  382. //SPI FLASH init...
  383. if(!AT25DF_Init()){
  384. SecCt=20;
  385. SlwTrace(DEBUG,"sFlash Failed!");
  386. while(SecCt--){
  387. os_dly_wait(50);
  388. FEED_EXTWATCHDOG();//喂狗
  389. }
  390. }
  391. ReadDeviceConfigFromFlash();//读取设备配置参数
  392. ReadServerIP();
  393. //StorageFiFoInit();
  394. //Led Init
  395. LedInit();
  396. SlwTrace(DEBUG," \r\nMain Task Start!\r\n");
  397. /////////////////
  398. //testAKeyByPSN();
  399. //鉴权
  400. // if(sutProductPara.AKEY!=getAKeyByPSN(sutProductPara.PSN)){
  401. // while(1){
  402. // os_dly_wait(1);
  403. // if(g_usUart1RecvLen){
  404. // if(RxBuffer1[0]=='R' && RxBuffer1[1]=='T' && RxBuffer1[2]=='+'){//处理电脑发过来的RT指令数据包
  405. // PcMsgHandle((char *)RxBuffer1);
  406. // }
  407. // g_usUart1RecvLen=0;
  408. // }
  409. // curTick=os_time_get();
  410. // if(curTick>lastTick){
  411. // lastTick=curTick+100;
  412. // } else continue;
  413. // FEED_EXTWATCHDOG();//喂狗
  414. // IWDG_ReloadCounter();//喂狗
  415. // sprintf(tracebuf,"%c%c%c%c%s",'U','N','A','!',"\r\n");//Unauthorized
  416. // SlwTrace(DEBUG,tracebuf);
  417. // if(sutProductPara.AKEY==getAKeyByPSN(sutProductPara.PSN))break;
  418. // }
  419. // }
  420. /////////////////
  421. //GPS初始化
  422. // GpsInit();
  423. //trace
  424. SlwTrace(DEBUG,sutProductPara.ProductName);
  425. sprintf(tracebuf,"_V%d\r\n",sutProductPara.SoftwareVer);
  426. SlwTrace(DEBUG,tracebuf);
  427. sprintf(tracebuf,"PSN=%lu\r\n",sutProductPara.PSN);
  428. SlwTrace(DEBUG,tracebuf);
  429. FEED_EXTWATCHDOG();
  430. idModemUartTask=os_tsk_create_user(ModemUartTask,4,&stkModemUartTask,sizeof(stkModemUartTask));
  431. FEED_EXTWATCHDOG();
  432. idModemTask=os_tsk_create_user(ModemTask,3,&stkModemTask,sizeof(stkModemTask));
  433. FEED_EXTWATCHDOG();
  434. //idWLanTask=os_tsk_create_user(WiredLanTask,2,&stkWLANTask,sizeof(stkWLANTask));
  435. FEED_EXTWATCHDOG();
  436. RunMake(THIS_FILE_ID);
  437. authNewInit();
  438. while(1)
  439. {
  440. //----以下每10ms执行一次----
  441. os_dly_wait(1);
  442. //处理TG和TM模式下 PC<-->GPS 或 PC<-->Modem
  443. if(g_ucUARTSel==COM_SEL_GPS || g_ucUARTSel==COM_SEL_MODEM){
  444. r=MsgQueueAccept(&ModemMsgQueue,ModemMsgBuf,sizeof(ModemMsgBuf));
  445. if(r>0){
  446. sutModemStatus.UartInactiveCt=0;
  447. ModemMsgBuf[r]=0;
  448. if(g_ucUARTSel==COM_SEL_GPS){
  449. IWDG_ReloadCounter();//喂狗
  450. FEED_EXTWATCHDOG();//喂狗
  451. Uart1Send((char *)ModemMsgBuf,r);
  452. FEED_EXTWATCHDOG();//喂狗
  453. }else{
  454. Uart1Send((char *)ModemMsgBuf,r);
  455. }
  456. }
  457. if(g_ucUARTSel==COM_SEL_MODEM && g_usUart1RecvLen>0){
  458. Uart3Send((char *)RxBuffer1,g_usUart1RecvLen);
  459. g_usUart1RecvLen=0;
  460. }
  461. }else if(g_usUart1RecvLen){//正常模式下,处理UART1发过来的命令
  462. if(RxBuffer1[0]=='R' && RxBuffer1[1]=='T' && RxBuffer1[2]=='+'){//处理电脑发过来的RT指令数据包
  463. PcMsgHandle((char *)RxBuffer1);
  464. g_ucUart1Activated=1;
  465. }else authHandleData(CHL_UART1,RxBuffer1, g_usUart1RecvLen);
  466. RxBuffer1[0]=0;
  467. g_usUart1RecvLen=0;
  468. }
  469. #if 0
  470. //处理GPS发过来的数据
  471. if(g_usRx2Len>0){
  472. process_nema((char *)RxBuffer2);
  473. memset(RxBuffer2,0,sizeof(RxBuffer2));
  474. g_usRx2Len=0;
  475. GpsConversionTime();
  476. // if(sutGpsInfo.isGpsValid){
  477. // printf("GPS[A]=%ld,%ld,%d,%d\r\n",sutGpsInfo.Latitude,sutGpsInfo.Longitude,sutGpsInfo.Speed,sutGpsInfo.Aspect);
  478. // }else{
  479. // printf("GPS[V]=%ld,%ld,%d,%d\r\n",sutGpsInfo.Latitude,sutGpsInfo.Longitude,sutGpsInfo.Speed,sutGpsInfo.Aspect);
  480. // }
  481. //printf("GPS:%02X%02X%02X %02X%02X%02X\r\n",sutGpsInfo.year,sutGpsInfo.month,sutGpsInfo.day,sutGpsInfo.hour,sutGpsInfo.minu,sutGpsInfo.sec);
  482. //printf("GPS:%02d-%02d-%02d %02d:%02d:%02d\r\n",sutGpsInfo.Years,sutGpsInfo.Months,sutGpsInfo.Days,sutGpsInfo.Hours,sutGpsInfo.Minute,sutGpsInfo.Seconds);
  483. if(sutGpsInfo.Years>2000){
  484. if(sutRTC.Seconds>sutGpsInfo.Seconds)r=sutRTC.Seconds-sutGpsInfo.Seconds;
  485. else r=sutGpsInfo.Seconds-sutRTC.Seconds;
  486. if(sutRTC.Years!=sutGpsInfo.Years || r>3){
  487. tsk_lock();
  488. sutRTC.Years=sutGpsInfo.Years;
  489. sutRTC.Months=sutGpsInfo.Months;
  490. sutRTC.Days=sutGpsInfo.Days;
  491. sutRTC.Hours=sutGpsInfo.Hours;
  492. sutRTC.Minute=sutGpsInfo.Minute;
  493. sutRTC.Seconds=sutGpsInfo.Seconds;
  494. tsk_unlock();
  495. }
  496. }
  497. }
  498. #endif
  499. //控制指示灯
  500. LedProcess(0);
  501. //防护进程
  502. FingerDaemonfinger();
  503. //---以下控制每秒执行1次----
  504. curTick=os_time_get();
  505. if(curTick>lastTick){
  506. lastTick=curTick+100;
  507. } else continue;
  508. //指示灯设置
  509. SetLedIndicator();
  510. //RTC加1秒
  511. RTCIncSecond();
  512. //存储控制
  513. //StorageCtrlSavePos();
  514. }
  515. }
  516. /*******************************************************************
  517. *GetParaFromStr
  518. *从Str中找到Para=后面至';'或非字符的字串并放入Value
  519. 返回Value的长度
  520. 举例:Str="GT+SMP=IP=192.168.1.1;Port=12345"
  521. 如果Para="Port" 则Value将被赋值为"12345",并返回5
  522. 如果Para="IP" 则Value将被赋值为"192.168.1.1",并返回11
  523. 要求Para长度不大于20字节 ,Value长度不大于40字节
  524. ********************************************************************/
  525. int GetParaFromStr(char *Str,char *Para,char *Value)
  526. {
  527. int ValueLen=0,ParaLen=0;
  528. char ParaTemp[22];
  529. char *p;
  530. char d;
  531. int i=0;
  532. if(0==*Str || 0==*Para)return 0;
  533. while(0!=(d=*Para) && ParaLen<20){
  534. ParaTemp[i++]=d;
  535. if(d=='=' || d==';')return 0;
  536. Para++;
  537. ParaLen++;
  538. }
  539. ParaTemp[ParaLen++]='=';
  540. ParaTemp[ParaLen]=0;
  541. p=strstr(Str,ParaTemp);
  542. if(p==0)return 0;
  543. p+=ParaLen;
  544. //--
  545. while(*p>0x20 && ';'!=*p && ValueLen<40){
  546. *Value=*p;
  547. p++;
  548. Value++;
  549. ValueLen++;
  550. }
  551. *Value=0;
  552. return ValueLen;
  553. }
  554. /*************************************************************************************
  555. FingerDaemonfinger
  556. 守护进程
  557. 用于防止通讯模块死机,或者与通讯模块通讯的串口异常而重新启动模块或通讯驱动。
  558. 此函数需要确保每秒调用一次
  559. **************************************************************************************/
  560. void FingerDaemonfinger(void)
  561. {
  562. int i;
  563. int OffLineMax;
  564. static unsigned char ext_dog_flag=0;
  565. //以下控制每秒执行一次,喂狗则0.5秒一次
  566. static U32 os_old_time=0;
  567. U32 os_cur_time,os_def_time;
  568. os_cur_time=os_time_get();
  569. os_def_time=os_cur_time-os_old_time;
  570. if(g_ucSystemReset==1)return;//g_ucSystemReset被设置为1 将不再喂狗,会导致MCU系统复位
  571. if(os_def_time>100){
  572. os_old_time=os_cur_time;
  573. }else{
  574. if(os_def_time<50){
  575. if(ext_dog_flag==0){
  576. ext_dog_flag=1;
  577. FEED_EXTWATCHDOG();//喂狗
  578. IWDG_ReloadCounter();//喂狗
  579. }
  580. }else{
  581. if(ext_dog_flag==1){
  582. ext_dog_flag=0;
  583. FEED_EXTWATCHDOG();//喂狗
  584. IWDG_ReloadCounter();//喂狗
  585. }
  586. }
  587. return;
  588. }
  589. if(g_ucUARTSel!=COM_SEL_MCU)return;//非正常模式下不防护
  590. //串口1防护
  591. if(sutTestData.OnceRecvFlag){//曾经收到过,5分钟防护
  592. if(++sutTestData.RecvIdelCt>300){
  593. sutTestData.RecvIdelCt=0;
  594. SlwTrace(DEBUG,"Uart1RxIdelTimeout!\r\n");
  595. os_tsk_delete(idModemTask);
  596. ModemSendAT("AT+QPOWD\r\n");
  597. os_dly_wait(1000);
  598. SystemReboot();
  599. }
  600. }else{//未曾收到过,1小时防护
  601. // if(++sutTestData.RecvIdelCt>3600){
  602. // sutTestData.RecvIdelCt=0;
  603. // SlwTrace(DEBUG,"Uart1RxIdelTimeout!\r\n");
  604. // os_tsk_delete(idModemTask);
  605. // ModemSendAT("AT+QPOWD\r\n");
  606. // os_dly_wait(1000);
  607. // SystemReboot();
  608. // }
  609. }
  610. //GPS模块防护
  611. //开机后超过1分钟没收到GPS或BD数据包,认为异常,应重启GPS模块
  612. // if(++sutGpsInfo.GpsCheckWorkCt>60 || ++sutGpsInfo.BDCheckWorkCt>120){
  613. // SlwTrace(DEBUG,"GNSS not work!reboot GNSS!\r\n");
  614. // GpsInit();
  615. // }
  616. //Modem防护
  617. //3分钟没有收到串口消息,重启MCU
  618. if(++sutModemStatus.UartInactiveCt>180){
  619. SlwTrace(DEBUG,"ModemUartTimeout!\r\n");
  620. os_tsk_delete(idModemTask);
  621. SystemReboot();
  622. }
  623. //1个小时登陆不上服务器,重启MCU
  624. tsk_lock();
  625. if(sutModemStatus.ServerOfflineCt<65535)sutModemStatus.ServerOfflineCt++;
  626. if(sutWLanStatus.ServerOfflineCt<65535)sutWLanStatus.ServerOfflineCt++;
  627. tsk_unlock();
  628. if(sutModemStatus.ServerOfflineCt>3600 && sutWLanStatus.ServerOfflineCt>3600){
  629. SlwTrace(DEBUG,"ServerOfflineTimeout!\r\n");
  630. os_tsk_delete(idModemTask);
  631. ModemSendAT("AT+QPOWD\r\n");
  632. os_dly_wait(1000);
  633. SystemReboot();
  634. }
  635. }