123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501 |
- #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<len-1;i++){
- sum=sum^pData[i+1];
- }
- return sum;
- }
- static int escapse(unsigned char *pData,int DataLen);
- /**********************
- **消息头12字节,消息体属性2个字节,这两个字节的16位。低10位是消息体长度
- 因此消息消息最长小于1024字节,Bit13是分包标志,看终端发到平台一个消息,可能很长,一个消息体发不完,分包发出去那么
- Bit12置为1,消息头长度变成16字节,消息头的后4个字节为消息包分装项内容,总共
- 有多少个包,这次发送的消息包是分包中的第几个消息包
- sutProductPara.APN
- **************************/
- static int TSGpsPacket(unsigned char *Buffer,unsigned long PSN,unsigned short ID,unsigned char *pData,int DataLen){
- AVL_MSG_BODY_ATTRIB msgBodyAttrib;
-
- static unsigned short liushiID=0;
- unsigned char *pBuf=Buffer,sum;
- int j,flag = 0,i=0;
- int talpage=1,MSnum = 0;
- char Psnsv[12];
- static unsigned short snum = 1;
- unsigned short Len=DataLen+6;
- unsigned short pageItem=DataLen+12;
- snprintf(Psnsv, sizeof(Psnsv),"%010u",(unsigned int)PSN);
- pBuf[i++]=0x7e;
- pBuf[i++]=(ID>>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;j<DataLen;j++){//0
- pBuf[i++]= pData[j];
- }
- }
- sum = TSGetCheckSum(pBuf,i);
-
- pBuf[i++] = sum;
- //对整体数据,除sum进行转义
- i=escapse(&pBuf[1],i-1);//头的7E不转义
- i++;
- pBuf[i++] = 0x7e;
- return i;
- }
- static int escapse(unsigned char *pData,int DataLen) {
-
- unsigned char *p= (unsigned char *)malloc(DataLen * 2);
- unsigned char * q = p;
- int data_len = 0;
- int i;
- if (DataLen == 0) {
- return 0;
- }
-
- memset(p, 0, DataLen * 2);
-
- for (i = 0; i < DataLen; i ++) {
- if (pData[i] == 0x7d) {
- *q ++ = 0x7d;
- *q ++ = 0x01;
- data_len += 2;
- } else if (pData[i] == 0x7e) {
- *q ++ = 0x7d;
- *q ++ = 0x02;
- data_len += 2;
- } else {
- *q ++ = pData[i];
- data_len += 1;
- }
- }
- memcpy(pData, p, data_len);
- free(p);
- DataLen = data_len;
- //free(p);
- return DataLen;
- }
- // replace 0x7d 0x01 with 0x7d and 0x7d 0x02 with 0x7e 接收
- //长度不会变大,只会变小或不变
- int reduce(unsigned char *pData,int DataLen)
- {
- unsigned char *p = pData;
- int data_len=0;
- int i;
-
- if(DataLen==0) return 0;
- for(i=0;i<DataLen-1;i++)
- {
- if(pData[i] == 0x7d && pData[i+1] == 0x01)
- {
- *p++ = 0x7d;i++;
- }else if(pData[i] == 0x7d && pData[i+1] == 0x02)
- {
- *p++ = 0x7e;i++;
- }else *p++=pData[i];
- data_len++;
- }
-
- return data_len;
- }
- void TSMakeGpsSendData(SUT_GPS_SEND_DATA *sendGpsData)
- {
- char sv[17];
- char data[3];
- char buf[10];
- int buf1[3];
- int j,temp,num;
- char *pTemp;
- char WWWW[5];
- static int Table[4]={0};
- static int Table1[4]={0};
- sendGpsData->Time[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<length;i++) printf("%02x",data[i]);
- printf("\r\n");
- }
|