/******************************************************************************** * File Name: MainTask.c * Function Describe:The main task for the system * Relate Module: * Explain: Hardware version is HS121 * Writer: ShiLiangWen * Date: 2015.7.8 *******************************************************************************/ #define THIS_FILE_ID 7 //--------------------------------------------------------------------------------- #include "includes.h" //OS ID OS_TID idMainTask=0; U64 stkMainTask[MAIN_TASK_STK_SIZE]; U32 g_ulSysTick=0; SUT_POSITION sutPosition; unsigned char g_ucUart1Activated=0; void FingerDaemonfinger(void); //====================================== //----- #ifdef USE_RUNNING_MAKE void RunningMake(unsigned short FileID,unsigned long line) { g_usFileID=FileID; g_ulFileLine=line; } #endif unsigned short g_usFileID=0; unsigned long g_ulFileLine=0; unsigned char g_ucSystemReset=0; /*********************************************** 强制复位 ************************************************/ void SystemReboot(void) { g_ucSystemReset=1; SlwTrace(DEBUG,"System reboot now!\r\n"); while(1); } /************************************************************************* 拷贝数据,并判断与原数据是否相同 相同返回0 不同返回1 **************************************************************************/ unsigned char CopyData(unsigned char *pDst,unsigned char *pSrc,unsigned char len) { unsigned char i; unsigned char flag=0; for(i=0;i=0)&&(aHex<=9)) aHex += 0x30; else if((aHex>=10)&&(aHex<=15))//A-F //aHex += 0x37; aHex += 0x57; else aHex = 0xff; return aHex; } void AscStrToHexStr(char *AscStr, char *HexStr) { char *pAscStr=AscStr; unsigned char d,h,l; while(0!=(d=(unsigned char)*pAscStr++)){ l=d&0x0f; h=d>>4; *HexStr++=AscToHex(h); *HexStr++=AscToHex(l); } *HexStr=0; } void PcMsgHandle(char *msg) { const char *CmdList[]={ "TM", //调试Modem "TG", //调试GPS "DEBUGLOG",//打印LOG "SSIM", //设置SIM卡配置信息 "RSIM", //读取SIM卡配置信息 "RVER",//读取版本号 "RSERVER",//读取服务器信息 "SETID",//设置PSN和AKEY "RPSN",//读PSN "REBOOT",//重启 // "TEST", // "FASTTEST", // "GNSS", "RPNAME", "SPNAME", "RDNAME", "SDNAME", "RASER", "SASER", ""//必须以空字串为结束 }; char needsave=0; unsigned short Port; unsigned long PSN,AKEY; char buf[150]; char tracebuf[50]; char bufIP[6]; //char strAccount[20]; //char strPassword[20]; int i,j,temp,len; char flag; const char *pCmd; char *pMsg; int CmdListNum=0; RunMake(THIS_FILE_ID); pMsg = msg; while(*CmdList[CmdListNum++]);//计算命令列表个数 CmdListNum--; RunMake(THIS_FILE_ID); //查找与命令列表相同的命令位置 for(i=0;i=20){ SlwTrace(INF,"RT+SSIM:FAIL[APN Invalid]\r\n"); break; }else strcpy(sutProductPara.CardAPN,buf); len=GetParaFromStr(msg,"NAME",buf); if(len<1 || len>=20){ SlwTrace(INF,"RT+SSIM:FAIL[NAME Invalid]\r\n"); break; }else strcpy(sutProductPara.CardUsrName,buf); len=GetParaFromStr(msg,"PASS",buf); if(len<1 || len>=20){ SlwTrace(INF,"RT+SSIM:FAIL[PASS Invalid]\r\n"); break; }else{ strcpy(sutProductPara.CardUsrPass,buf); } SaveProductParaToFlash(); SlwTrace(INF,"Set SIM Info OK!\r\n"); break; case 4://RT+RSIM sprintf(buf,"APN=%s;USR=%s;PASS=%s\r\n",sutProductPara.CardAPN,sutProductPara.CardUsrName,sutProductPara.CardUsrPass); SlwTrace(INF,buf); break; case 5://RT+RVER sprintf(buf,"HVer=%d SVer=%d\r\n",sutProductPara.HardwareVer,sutProductPara.SoftwareVer); SlwTrace(INF,buf); break; case 6://RT+RSERVER sprintf(buf,"AppS:%s:%d\r\n",sutProductPara.AppServer,sutProductPara.AppPort); SlwTrace(INF,buf); sprintf(buf,"ServerIP:%d.%d.%d.%d:%d\r\n",sutServerIp.AppServerIP[0],sutServerIp.AppServerIP[1],sutServerIp.AppServerIP[2],sutServerIp.AppServerIP[3],sutProductPara.AppPort); SlwTrace(INF,buf); sprintf(buf,"FotaS:%s:%d\r\n",sutProductPara.FotaServer,sutProductPara.FotaPort); SlwTrace(INF,buf); sprintf(buf,"FotaIP:%d.%d.%d.%d:%d\r\n",sutServerIp.FotaServerIP[0],sutServerIp.FotaServerIP[1],sutServerIp.FotaServerIP[2],sutServerIp.FotaServerIP[3],sutProductPara.FotaPort); SlwTrace(INF,buf); break; case 7://RT+SETID=PSN=190310005;AKEY=1412652393 len=GetParaFromStr(msg,"PSN",buf); if(len<1 || len>10){ SlwTrace(INF,"RT+SETID:FAIL[PSN Invalid]\r\n"); break; } PSN=atoll(buf); if(PSN==0){ SlwTrace(INF,"RT+SETID:FAIL[PSN Invalid]\r\n"); break; } len=GetParaFromStr(msg,"AKEY",buf); if(len<1 || len>10){ SlwTrace(INF,"RT+SETID:FAIL[AKEY Invalid]\r\n"); break; } AKEY=atoll(buf); if(AKEY!=getAKeyByPSN(PSN)){ SlwTrace(INF,"PSN=0\r\n"); return; } sutProductPara.PSN = PSN; sutProductPara.AKEY = AKEY; memset(sutProductPara.DeviceID,0,sizeof(sutProductPara.DeviceID)); DecToBCD(sutProductPara.PSN,&sutProductPara.DeviceID[1],5); sutProductPara.MAC[0]=((MY_MAC_HEAD&0xFF0000)>>16); sutProductPara.MAC[1]=((MY_MAC_HEAD&0x00FF00)>>8); sutProductPara.MAC[2]=((MY_MAC_HEAD&0x0000FF)); sutProductPara.MAC[3]=((sutProductPara.PSN&0xFF0000)>>16); sutProductPara.MAC[4]=((sutProductPara.PSN&0x00FF00)>>8); sutProductPara.MAC[5]=(sutProductPara.PSN&0x0000FF); SaveProductParaToFlash(); ReadProductParaFromFlash(); sprintf(buf,"PSN=%lu\r\nAKEY=%lu\r\n",sutProductPara.PSN,sutProductPara.AKEY); SlwTrace(INF,buf); sprintf(buf,"DeviceID=%x%02x%02x%02x%02x\r\n",sutProductPara.DeviceID[1],sutProductPara.DeviceID[2], sutProductPara.DeviceID[3],sutProductPara.DeviceID[4],sutProductPara.DeviceID[5]); SlwTrace(INF,buf); sprintf(buf,"MAC=%02X.%02X.%02X.%02X.%02X.%02X\r\n",sutProductPara.MAC[0],sutProductPara.MAC[1],sutProductPara.MAC[2], sutProductPara.MAC[3],sutProductPara.MAC[4],sutProductPara.MAC[5]); SlwTrace(INF,buf); break; case 8://RT+RPSN sprintf(buf,"PSN=%ld\r\n",sutProductPara.PSN); SlwTrace(INF,buf); break; case 9://REBOOT SystemReboot(); break; #if 0 case 10://TEST // sprintf(buf,"RTC:%d-%d-%d %d:%d:%d\r\n",sutRTC.Years,sutRTC.Months,sutRTC.Days,sutRTC.Hours,sutRTC.Minute,sutRTC.Seconds); // SlwTrace(INF,buf); // if(sutGpsInfo.isGpsValid){ // sprintf(buf,"GPS:[A] lat=%ld lng=%ld\r\n",sutGpsInfo.Latitude,sutGpsInfo.Longitude); // }else{ // sprintf(buf,"GPS:[V] lat=%ld lng=%ld\r\n",sutGpsInfo.Latitude,sutGpsInfo.Longitude); // } // SlwTrace(INF,buf); if(!sutGpsInfo.isGpsValid){//GPS模块未定位 SlwTrace(INF,"Test:GPS Fail!\r\n"); }else if(sutWLanStatus.Step=sizeof(sutProductPara.proName)) len=sizeof(sutProductPara.proName)-1; memcpy(sutProductPara.proName, msg+10,len); SaveProductParaToFlash(); SlwTrace(INF, "RT+SPNAME:OK\r\n"); break; case 12://RDNAME sprintf(buf, "RDNAME=%s\r\n", sutProductPara.devName); SlwTrace(INF, buf); break; case 13://SDNAME memset(sutProductPara.devName, 0, sizeof(sutProductPara.devName)); for(i=0;i=sizeof(sutProductPara.devName)) len=sizeof(sutProductPara.devName)-1; memcpy(sutProductPara.devName, msg+10,len); SaveProductParaToFlash(); SlwTrace(INF, "RT+SDNAME:OK\r\n"); break; case 14://RASER sprintf(buf, "RT+RASER=%s,%d\r\n", sutProductPara.authServer, sutProductPara.authPort); SlwTrace(INF, buf); break; case 15://SASER len=GetParaFromStr(msg,"IP",buf); if(len<1 || len>=20){ SlwTrace(INF,"RT+SASER:FAIL[IP Invalid]\r\n"); break; }else{ if(0!=strcmp(sutProductPara.authServer, buf)){ strcpy(sutProductPara.authServer,buf); needsave=1; } } len=GetParaFromStr(msg,"Port",buf); if(len<1 || len> 5){ SlwTrace(INF,"RT+SASER:FAIL[Port Invalid]\r\n"); break; }else{ if(sutProductPara.authPort != atoi(buf)){ sutProductPara.authPort=atoi(buf); needsave=1; } } if(needsave != 0) SaveProductParaToFlash(); snprintf(buf, sizeof(buf), "RT+SASER:IP=%s;Port=%d\r\n", sutProductPara.authServer, sutProductPara.authPort); SlwTrace(INF, buf); break; } RunMake(THIS_FILE_ID); //AB CD E5 02 54 4D 32 5C if(msg[0]=='T' && msg[1]=='M'){//0x544D printf("----TM----\r\n"); ComSelect(COM_SEL_MODEM); } } void ShowHex(unsigned char *pData,unsigned char len) { char temp[5]; char buf[100]; int i; if(len>(sizeof(buf)-3))len=(sizeof(buf)-3); buf[0]=0; for(i=0;ilastTick){ // lastTick=curTick+100; // } else continue; // FEED_EXTWATCHDOG();//喂狗 // IWDG_ReloadCounter();//喂狗 // sprintf(tracebuf,"%c%c%c%c%s",'U','N','A','!',"\r\n");//Unauthorized // SlwTrace(DEBUG,tracebuf); // if(sutProductPara.AKEY==getAKeyByPSN(sutProductPara.PSN))break; // } // } ///////////////// //GPS初始化 // GpsInit(); //trace SlwTrace(DEBUG,sutProductPara.ProductName); sprintf(tracebuf,"_V%d\r\n",sutProductPara.SoftwareVer); SlwTrace(DEBUG,tracebuf); sprintf(tracebuf,"PSN=%lu\r\n",sutProductPara.PSN); SlwTrace(DEBUG,tracebuf); FEED_EXTWATCHDOG(); idModemUartTask=os_tsk_create_user(ModemUartTask,4,&stkModemUartTask,sizeof(stkModemUartTask)); FEED_EXTWATCHDOG(); idModemTask=os_tsk_create_user(ModemTask,3,&stkModemTask,sizeof(stkModemTask)); FEED_EXTWATCHDOG(); //idWLanTask=os_tsk_create_user(WiredLanTask,2,&stkWLANTask,sizeof(stkWLANTask)); FEED_EXTWATCHDOG(); RunMake(THIS_FILE_ID); authNewInit(); while(1) { //----以下每10ms执行一次---- os_dly_wait(1); //处理TG和TM模式下 PC<-->GPS 或 PC<-->Modem if(g_ucUARTSel==COM_SEL_GPS || g_ucUARTSel==COM_SEL_MODEM){ r=MsgQueueAccept(&ModemMsgQueue,ModemMsgBuf,sizeof(ModemMsgBuf)); if(r>0){ sutModemStatus.UartInactiveCt=0; ModemMsgBuf[r]=0; if(g_ucUARTSel==COM_SEL_GPS){ IWDG_ReloadCounter();//喂狗 FEED_EXTWATCHDOG();//喂狗 Uart1Send((char *)ModemMsgBuf,r); FEED_EXTWATCHDOG();//喂狗 }else{ Uart1Send((char *)ModemMsgBuf,r); } } if(g_ucUARTSel==COM_SEL_MODEM && g_usUart1RecvLen>0){ Uart3Send((char *)RxBuffer1,g_usUart1RecvLen); g_usUart1RecvLen=0; } }else if(g_usUart1RecvLen){//正常模式下,处理UART1发过来的命令 if(RxBuffer1[0]=='R' && RxBuffer1[1]=='T' && RxBuffer1[2]=='+'){//处理电脑发过来的RT指令数据包 PcMsgHandle((char *)RxBuffer1); g_ucUart1Activated=1; }else authHandleData(CHL_UART1,RxBuffer1, g_usUart1RecvLen); RxBuffer1[0]=0; g_usUart1RecvLen=0; } #if 0 //处理GPS发过来的数据 if(g_usRx2Len>0){ process_nema((char *)RxBuffer2); memset(RxBuffer2,0,sizeof(RxBuffer2)); g_usRx2Len=0; GpsConversionTime(); // if(sutGpsInfo.isGpsValid){ // printf("GPS[A]=%ld,%ld,%d,%d\r\n",sutGpsInfo.Latitude,sutGpsInfo.Longitude,sutGpsInfo.Speed,sutGpsInfo.Aspect); // }else{ // printf("GPS[V]=%ld,%ld,%d,%d\r\n",sutGpsInfo.Latitude,sutGpsInfo.Longitude,sutGpsInfo.Speed,sutGpsInfo.Aspect); // } //printf("GPS:%02X%02X%02X %02X%02X%02X\r\n",sutGpsInfo.year,sutGpsInfo.month,sutGpsInfo.day,sutGpsInfo.hour,sutGpsInfo.minu,sutGpsInfo.sec); //printf("GPS:%02d-%02d-%02d %02d:%02d:%02d\r\n",sutGpsInfo.Years,sutGpsInfo.Months,sutGpsInfo.Days,sutGpsInfo.Hours,sutGpsInfo.Minute,sutGpsInfo.Seconds); if(sutGpsInfo.Years>2000){ if(sutRTC.Seconds>sutGpsInfo.Seconds)r=sutRTC.Seconds-sutGpsInfo.Seconds; else r=sutGpsInfo.Seconds-sutRTC.Seconds; if(sutRTC.Years!=sutGpsInfo.Years || r>3){ tsk_lock(); sutRTC.Years=sutGpsInfo.Years; sutRTC.Months=sutGpsInfo.Months; sutRTC.Days=sutGpsInfo.Days; sutRTC.Hours=sutGpsInfo.Hours; sutRTC.Minute=sutGpsInfo.Minute; sutRTC.Seconds=sutGpsInfo.Seconds; tsk_unlock(); } } } #endif //控制指示灯 LedProcess(0); //防护进程 FingerDaemonfinger(); //---以下控制每秒执行1次---- curTick=os_time_get(); if(curTick>lastTick){ lastTick=curTick+100; } else continue; //指示灯设置 SetLedIndicator(); //RTC加1秒 RTCIncSecond(); //存储控制 //StorageCtrlSavePos(); } } /******************************************************************* *GetParaFromStr *从Str中找到Para=后面至';'或非字符的字串并放入Value 返回Value的长度 举例:Str="GT+SMP=IP=192.168.1.1;Port=12345" 如果Para="Port" 则Value将被赋值为"12345",并返回5 如果Para="IP" 则Value将被赋值为"192.168.1.1",并返回11 要求Para长度不大于20字节 ,Value长度不大于40字节 ********************************************************************/ int GetParaFromStr(char *Str,char *Para,char *Value) { int ValueLen=0,ParaLen=0; char ParaTemp[22]; char *p; char d; int i=0; if(0==*Str || 0==*Para)return 0; while(0!=(d=*Para) && ParaLen<20){ ParaTemp[i++]=d; if(d=='=' || d==';')return 0; Para++; ParaLen++; } ParaTemp[ParaLen++]='='; ParaTemp[ParaLen]=0; p=strstr(Str,ParaTemp); if(p==0)return 0; p+=ParaLen; //-- while(*p>0x20 && ';'!=*p && ValueLen<40){ *Value=*p; p++; Value++; ValueLen++; } *Value=0; return ValueLen; } /************************************************************************************* FingerDaemonfinger 守护进程 用于防止通讯模块死机,或者与通讯模块通讯的串口异常而重新启动模块或通讯驱动。 此函数需要确保每秒调用一次 **************************************************************************************/ void FingerDaemonfinger(void) { int i; int OffLineMax; static unsigned char ext_dog_flag=0; //以下控制每秒执行一次,喂狗则0.5秒一次 static U32 os_old_time=0; U32 os_cur_time,os_def_time; os_cur_time=os_time_get(); os_def_time=os_cur_time-os_old_time; if(g_ucSystemReset==1)return;//g_ucSystemReset被设置为1 将不再喂狗,会导致MCU系统复位 if(os_def_time>100){ os_old_time=os_cur_time; }else{ if(os_def_time<50){ if(ext_dog_flag==0){ ext_dog_flag=1; FEED_EXTWATCHDOG();//喂狗 IWDG_ReloadCounter();//喂狗 } }else{ if(ext_dog_flag==1){ ext_dog_flag=0; FEED_EXTWATCHDOG();//喂狗 IWDG_ReloadCounter();//喂狗 } } return; } if(g_ucUARTSel!=COM_SEL_MCU)return;//非正常模式下不防护 //串口1防护 if(sutTestData.OnceRecvFlag){//曾经收到过,5分钟防护 if(++sutTestData.RecvIdelCt>300){ sutTestData.RecvIdelCt=0; SlwTrace(DEBUG,"Uart1RxIdelTimeout!\r\n"); os_tsk_delete(idModemTask); ModemSendAT("AT+QPOWD\r\n"); os_dly_wait(1000); SystemReboot(); } }else{//未曾收到过,1小时防护 // if(++sutTestData.RecvIdelCt>3600){ // sutTestData.RecvIdelCt=0; // SlwTrace(DEBUG,"Uart1RxIdelTimeout!\r\n"); // os_tsk_delete(idModemTask); // ModemSendAT("AT+QPOWD\r\n"); // os_dly_wait(1000); // SystemReboot(); // } } //GPS模块防护 //开机后超过1分钟没收到GPS或BD数据包,认为异常,应重启GPS模块 // if(++sutGpsInfo.GpsCheckWorkCt>60 || ++sutGpsInfo.BDCheckWorkCt>120){ // SlwTrace(DEBUG,"GNSS not work!reboot GNSS!\r\n"); // GpsInit(); // } //Modem防护 //3分钟没有收到串口消息,重启MCU if(++sutModemStatus.UartInactiveCt>180){ SlwTrace(DEBUG,"ModemUartTimeout!\r\n"); os_tsk_delete(idModemTask); SystemReboot(); } //1个小时登陆不上服务器,重启MCU tsk_lock(); if(sutModemStatus.ServerOfflineCt<65535)sutModemStatus.ServerOfflineCt++; if(sutWLanStatus.ServerOfflineCt<65535)sutWLanStatus.ServerOfflineCt++; tsk_unlock(); if(sutModemStatus.ServerOfflineCt>3600 && sutWLanStatus.ServerOfflineCt>3600){ SlwTrace(DEBUG,"ServerOfflineTimeout!\r\n"); os_tsk_delete(idModemTask); ModemSendAT("AT+QPOWD\r\n"); os_dly_wait(1000); SystemReboot(); } }