#include "includes.h" unsigned char buBiaoSosSendNow=0; unsigned char g_ucModemSentTcpCt=0; FEEDBACK_DEF sutFeed; static void buBiaoReg(unsigned char socketID); static void buBiaoAuth(unsigned char socketID); static void buBiaoLocation(unsigned char socketID); static void buBiaoTime(unsigned char socketID); static void buBiaoHeart(unsigned char socketID); static void buBiaoAck(unsigned char socketID); static void buBiaoSoS(unsigned char socketID); void buBiaoLogic(unsigned char socketID){ static unsigned short tick=0; static unsigned char retry=0; static unsigned char autTick=0; static unsigned short timeReqCnt=0; static unsigned short heartTick=0; static unsigned short locationTick=0; static unsigned char locationHold=0; unsigned char tcpSendAlready=0; unsigned short temp; char info[30]; if(buBiaoAddrChange){ buBiaoAddrChange=0; SocketParaUpdate(socketID,showGIP,1395); } if(tcpSocketGetStatus(socketID)) return; if(sutGpsInfo.isGpsAuthOk==0){//未鉴权 timeReqCnt=500; if(++autTick>=10*3){ //15S autTick=0; if(++retry>=3){//鉴权3次都没成功,注册 retry=0; buBiaoReg(socketID); }else buBiaoAuth(socketID);//鉴权 } return; } //已鉴权了,定时发心跳,获取时间,发送定位 if(g_ucModemSentTcpCt>6){ //如果发了3秒数据包都没响应,则主动关闭连接 g_ucModemSentTcpCt=0; snprintf(info, sizeof(info), "AT+TCPCLOSE=%d\r\n",socketID+1); ModemSendAt(info); sutGpsInfo.isGpsAuthOk=0; return; } if(sutGpsInfo.isGpsValid && g_uiGpsStat){ temp=GPS_TimeTable[newPara.gpsTimeIndex]*2; if(++locationTick>=temp){ //定位包时间到了 locationTick=0; locationHold=1; } if(locationHold && isSendDataEnable()){ locationHold=0; tcpSendAlready=1; // snprintf(info, sizeof(info), "AT+GPS=\"OFF\"\r\n"); // ModemSendAt(info); // SlwTrace(INF,info,0); buBiaoLocation(socketID); } } if(++timeReqCnt>=360 && !tcpSendAlready){ //获取时间到了 if(isSendDataEnable()){ timeReqCnt=0; tcpSendAlready=1; buBiaoTime(socketID); } } if(sutFeed.feedBackStart!=0 && !tcpSendAlready){ //需要主动应用服务器 sutFeed.feedBackStart=0; tcpSendAlready=1; buBiaoAck(socketID); } if(buBiaoSosSendNow && !tcpSendAlready){ buBiaoSosSendNow=0; tcpSendAlready=1; buBiaoSoS(socketID); } if(++heartTick>=120 && !tcpSendAlready){ //发送心跳时间到了 if(isSendDataEnable()){ heartTick=0; buBiaoHeart(socketID); } } if(tcpSendAlready){ g_ucModemSentTcpCt++; heartTick=0;//已发了其它包,心跳包重置计数 } } void buBiaoDataProcess(unsigned char *pMsg, unsigned short tempLen){ char buf[20]; char buf2[50]; char result[10]; unsigned short cmd,ackCmd,smsLen; unsigned char Result,i,buBiaoResponsed; short targetIndex; SUTDS ds; USER_RTC_DEF tmpTime; cmd = (pMsg[1]<<8) | (pMsg[2]&0xff); buBiaoResponsed=1; snprintf(buf2, sizeof(buf2), "\r\ncmd=%04x", cmd); SlwTrace(INF, buf2,1); switch(cmd) {//布标命令返回 case TS_TERMINAL_REGISTRATION_REPLY://终端注册应答 //7E 8100 000A 000166000016 0250 0100 00 20161118165030 E07E 成功 //7E 8100 0003 000166000016 0251 0100 01 A07E 已注册 Result = pMsg[15]; snprintf(buf2, sizeof(buf2), "\r\nresult=%02x", Result); SlwTrace(INF, buf2,1); switch(Result) { case 0://00注册成功 if(0 != memcmp(newPara.authPara.authCode, pMsg+16, 7)){ memcpy(newPara.authPara.authCode, pMsg+16,7); //save sys ModifyConfigureFile(0); } snprintf(result, sizeof(result), "Ok"); break; case 1://01 车辆已注册 snprintf(result, sizeof(result), "Car Exist");break; case 2://02 无此车辆 snprintf(result, sizeof(Result), "No car");break; case 3://03 终端已经被注册 snprintf(result, sizeof(result), "Device Exist");break; case 4://04 数据库无此终端 snprintf(result, sizeof(result), "No Record");break; default://无效值 //统称失败 snprintf(result, sizeof(result), "Faile"); break; } snprintf(buf2, sizeof(buf2), "\r\n[Register]%d %s", Result,result); SlwTrace(INF, buf2,1); break; case TS_TEXT_INFORMATION_ISSUED://文本下发 //7E83000005000166000009000001 31323334ED7E smsLen = pMsg[3]; smsLen <<= 8; smsLen &= 0xff00; smsLen |= pMsg[4]; smsLen -= 1;//去掉后面较验码 if(SMS_SIGNAL_LEN <= smsLen) smsLen = SMS_SIGNAL_LEN; memset(sutMess.GBKMess1, 0, sizeof(sutMess.GBKMess1)); memcpy(sutMess.GBKMess1, pMsg+14, smsLen); sutMess.len = smsLen; ////////////////////////// sutFeed.feedID=TS_SETTING_TERMINAL_PARAMETERS; ds.Data.ucData.b1=pMsg[11]; ds.Data.ucData.b2=pMsg[12]; sutFeed.feedliushuiID=ds.Data.usData; sutFeed.result=2; ////////////////////////// if(sutMess.len > 0) SetGotNewMessage(); break; case TS_PLATFORM_UNIVERSAL_ANSWER://通用平台应答 //7E8001000500016600001602400000010201B57E //buf[0]=0x7e; //if(-1 != (targetIndex=FindTargetIndex((char *)pMsg, '\r', buf, 1,2))) targetIndex = tempLen;//这个长度是去掉了两个7E的长度 { ackCmd=(pMsg[targetIndex-3]<<8) | (pMsg[targetIndex-2]&0xff); Result = pMsg[targetIndex-1]; snprintf(buf2, sizeof(buf2), "\r\nLen=%d,ackCmd=%04x,result=%02x", tempLen,ackCmd,Result); SlwTrace(INF, buf2,1); switch(ackCmd) { case TS_TERMINAL_AUTHENTICATION://终端鉴权应答 if(Result == 0) {//鉴权成功 sutGpsInfo.isGpsAuthOk=1; snprintf(buf2, sizeof(buf2), "\r\n[ATH]Ok"); enableDataSend(); //TSGpsServerConect();//鉴权成功后发一包终端应答包即可以支持文本接收了 }else { sutGpsInfo.isGpsAuthOk=0; snprintf(buf2, sizeof(buf2), "\r\n[ATH]Faile"); } SlwTrace(INF, buf2,1); break; case TS_LOCATION_INFORMATION_REPORTING://位置包应答 SlwTrace(INF, "\r\nLocation ACK",1); break; case TS_TERMINAL_HEARTBEAT://心跳应答 SlwTrace(INF, "\r\nHearTick ACK",1); break; case TS_SEND_SOS_SMS://SOS应答 SlwTrace(INF, "\r\nSOS ACK",1); break; } } break; case TS_TERMINAL_REQTIME://时间ACK //7e0f020007001899000022003a e20704130e2f02 427e SlwTrace(INF, "\r\nTimeAck",1); ds.Data.ucData.b1=pMsg[13]; ds.Data.ucData.b2=pMsg[14]; tmpTime.year=ds.Data.usData; tmpTime.month=pMsg[15]; tmpTime.day=pMsg[16]; tmpTime.hour=pMsg[17]; tmpTime.min=pMsg[18]; tmpTime.second=pMsg[19]; RTC_UserSetTime(tmpTime); printf("\r\nNewTime:%02d-%02d-%02d %02d:%02d:%02d\r\n", tmpTime.year,tmpTime.month,tmpTime.day, tmpTime.hour,tmpTime.min,tmpTime.second); break; default: buBiaoResponsed=0; break; } g_ucModemSentTcpCt=0; /////////////处理其它类型指令 } static int TSGpsPacket(unsigned char *Buffer,unsigned long PSN,unsigned short ID,unsigned char *pData,int DataLen); static void buBiaoReg(unsigned char socketID){ unsigned char info[100]; unsigned short PacketLen; REGISTRATION Regist; SlwTrace(INF, "buBiaoReg",1); memset((unsigned char *)&Regist, 0, sizeof(REGISTRATION)); Regist.TerID[0]=(unsigned char)((sutProductPara.PSN>>24)&0xff); Regist.TerID[1]=(unsigned char)((sutProductPara.PSN>>16)&0xff); Regist.TerID[2]=(unsigned char)((sutProductPara.PSN>>8)&0xff); Regist.TerID[3]=(unsigned char)(sutProductPara.PSN&0xff); PacketLen=TSGpsPacket(info,sutProductPara.PSN,TS_TERMINAL_REGISTRATION,(unsigned char *)&Regist,sizeof(REGISTRATION)); tcpSocketSendData(socketID, info, PacketLen); } static void buBiaoAuth(unsigned char socketID){ unsigned char info[80]; unsigned short PacketLen; SlwTrace(INF, "buBiaoAuth",1); PacketLen=TSGpsPacket(info,sutProductPara.PSN,TS_TERMINAL_AUTHENTICATION,newPara.authPara.authCode,sizeof(newPara.authPara.authCode));//实际是返回的鉴权信息 tcpSocketSendData(socketID, info, PacketLen); PrintBufferData(info,PacketLen); } static void buBiaoLocation(unsigned char socketID){ unsigned char info[80]; unsigned short PacketLen; SUT_GPS_SEND_DATA sutGpsSendData; SlwTrace(INF, "buBiaoLocation",1); TSMakeGpsSendData(&sutGpsSendData); PacketLen=TSGpsPacket(info,sutProductPara.PSN,TS_LOCATION_INFORMATION_REPORTING,(unsigned char *)&sutGpsSendData,sizeof(SUT_GPS_SEND_DATA)); tcpSocketSendData(socketID, info, PacketLen); } static void buBiaoTime(unsigned char socketID){ unsigned char info[50]; unsigned short PacketLen; SlwTrace(INF, "buBiaoTime",1); PacketLen=TSGpsPacket(info,sutProductPara.PSN,TS_TERMINAL_REQTIME,0,0); tcpSocketSendData(socketID, info, PacketLen); } static void buBiaoHeart(unsigned char socketID){ unsigned char info[100]; unsigned short PacketLen; SlwTrace(INF, "buBiaoHeart",1); PacketLen=TSGpsPacket(info,sutProductPara.PSN,TS_TERMINAL_HEARTBEAT,0,0); tcpSocketSendData(socketID, info, PacketLen); } static void buBiaoAck(unsigned char socketID){ int len; unsigned char info[100]; unsigned char temp[5]; SUTDS ds; SlwTrace(INF, "buBiaoAck",1); memcpy(temp, (unsigned char*)&sutFeed.feedliushuiID,2); ds.Data.usData=sutFeed.feedID; temp[2] = ds.Data.ucData.b2; temp[3] = ds.Data.ucData.b1; temp[4] = sutFeed.result; len=TSGpsPacket(info,sutProductPara.PSN,TS_TERMINAL_UNIVERSAL_ANSWER,temp,sizeof(temp)); tcpSocketSendData(socketID, info, len); } static void buBiaoSoS(unsigned char socketID){ unsigned char info[50]; unsigned short PacketLen; SlwTrace(INF, "buBiaoSoS",1); PacketLen=TSGpsPacket(info,sutProductPara.PSN,TS_SEND_SOS_SMS,0,0); tcpSocketSendData(socketID, info, PacketLen); } static unsigned char TSGetCheckSum(unsigned char *pData,int len){ int i; unsigned char sum= pData[1]; //sum=0; for(i=1;i>8)&0xff; pBuf[i++]=ID&0xff; //消息体属性 msgBodyAttrib.EncrypType=NO_ENCRYP;//NO_ENCRYP; msgBodyAttrib.msgBodyLength=(DataLen); pBuf[i++]=msgBodyAttrib.EncrypType; pBuf[i++]=msgBodyAttrib.msgBodyLength; //SIM卡号 pBuf[i++]=0; // for (j=0;j<10;j+=2) { pBuf[i++]=(Psnsv[j]<<4)|(Psnsv[j+1]&0x0F); } //消息流水号 pBuf[i++] = (liushiID>>8)&0xff; pBuf[i++] = liushiID&0xff; liushiID++; if(pData!=NULL){ for(j=0;jTime[0] = sutGpsInfo.year; sendGpsData->Time[1] = sutGpsInfo.month; sendGpsData->Time[2] = sutGpsInfo.day; sendGpsData->Time[3] = sutGpsInfo.hour; sendGpsData->Time[4] = sutGpsInfo.minu; sendGpsData->Time[5] = sutGpsInfo.sec; sendGpsData->St=0x00; sendGpsData->status=0; sendGpsData->status |=(1<<24);//b0 1 acc sendGpsData->status |=(1<<25);//b1 1 定位 if(sutGpsInfo.NSFlag=='S') sendGpsData->status |=(1<<26);//b2 1 南纬 if(sutGpsInfo.EWFlag=='W') sendGpsData->status |=(1<<27);//b3 1 西经 sendGpsData->status |=(1<<28);//b4 1 运营 sendGpsData->Mileag=((sutGpsInfo.StatisticsMile>>8)&0xFF)|(((sutGpsInfo.StatisticsMile)&0xFF)<<8); sendGpsData->SSFF=((sutGpsInfo.speed>>8)&0xFF)|(((sutGpsInfo.speed)&0xFF)<<8); sendGpsData->Acpect=sutGpsInfo.aspect; GetCoordinates(1); sendGpsData->WWWW[0] = sutGpsInfo.testWD>>24; sendGpsData->WWWW[1] = sutGpsInfo.testWD>>16; sendGpsData->WWWW[2] = sutGpsInfo.testWD>>8; sendGpsData->WWWW[3] = sutGpsInfo.testWD; sendGpsData->JJJJ[0] = sutGpsInfo.testJD>>24; sendGpsData->JJJJ[1] = sutGpsInfo.testJD>>16; sendGpsData->JJJJ[2] = sutGpsInfo.testJD>>8; sendGpsData->JJJJ[3] = sutGpsInfo.testJD; } void TSGpsDataInit(void) { memset((unsigned char *)&sutGpsInfo, 0, sizeof(SUT_GPS_INF)); printf("Gpstime=%d\r\n",GPS_TimeTable[newPara.gpsTimeIndex]); } void PrintBufferData(unsigned char *data, unsigned short length) { unsigned short i; printf("Data[%d]:\r\n",length); for(i=0;i