123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520 |
-
- /**********************************************************************************
- * File Name: TSGpsProcess.c
- * Function Describe:device for Gps Process for TS/T808
- * Relate Module:
- * Explain: 采用TCP或UDP格式传输
- * Writer: wuzhao
- * Date: 2015.12.14
- *外设类型:0x01,
- *命令类型:0x05
- ***********************************************************************************/
- #define THIS_FILE_ID 24
- //-------------------------------------------------------------------------------------------
- #include "includes.h"
- SUT_GPS_SEND_DATA sutGpsSendData;
- //SUT_PRODUCT_PARA TSsutProductPara;
- REGISTRATION Regist;
- AVL_MSG_BODY_ATTRIB msgBodyAttrib;
- MSG_HEAD msghead;
- MSG_PACKET msgPacket;
- AUTH auth;
- SIM SimData;
- unsigned int a;
- unsigned int b;
- char GPSDataBuf[8];
- char GPSDataBuf1[8];
- //*p_buf -- *m_data size -- m_size
- /**********************************************************************************
- GpsSendHead
- ***********************************************************************************/
- void TSGpsServerConect(void)
- {
- int len;
- unsigned char buf[100];
- unsigned char temp[5];
- SUTDS ds;
- SlwTrace(INF, "Device ACK",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(buf,sutProductPara.PSN,TS_TERMINAL_UNIVERSAL_ANSWER,temp,sizeof(temp));
- MC8332ModemSendTcpData(GPS_DATA_SOCKET,buf,len);
- }
- //从厂商编号到用户数据依次累加的累加和,取累加的档�8位作为校验码
- 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;
- }
- /*************************************************************************************
- *
- **************************************************************************************/
- // 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;
- }
- // loop every character, replace 0x7d with 0x7d 0x01 and 0x7e with 0x7d 0x02 发送
- 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;
- }
- /******************************************************/
- int* TenToTwo(int Datalen)
- {
- static int c[10]={0};
- int i=0;
- do {
- c[i++]=Datalen%2;
- Datalen/=2;
- } while(Datalen!=0);
-
- return c;
- }
- /**********************
- **消息头12字节,消息体属性2个字节,这两个字节的16位。低10位是消息体长度
- 因此消息消息最长小于1024字节,Bit13是分包标志,看终端发到平台一个消息,可能很长,一个消息体发不完,分包发出去那么
- Bit12置为1,消息头长度变成16字节,消息头的后4个字节为消息包分装项内容,总共
- 有多少个包,这次发送的消息包是分包中的第几个消息包
- sutProductPara.APN
- **************************/
- int TSGpsPacket(unsigned char *Buffer,unsigned long PSN,unsigned short ID,unsigned char *pData,int DataLen)
- {
- 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;
- for(j=0;j<6;j++){
- pBuf[i++]=SimData.SIM[j];
- }
-
- //消息流水号
- pBuf[i++] = (liushiID>>8)&0xff;
- pBuf[i++] = liushiID&0xff;
- liushiID++;
-
- if(pData!=NULL)
- {
- //snum++;
- //消息包封装项
- //msgPacket.msgPacketID=0x30;
- //msgPacket.msgTotalCount=0x30;
- //pBuf[i++] = msgPacket.msgPacketID;
- //pBuf[i++] = msgPacket.msgTotalCount;
- //MSnum++;
- //包封装项 4
- 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;
- }
- /*******************************************************
- GPRMC信息,与,之间的子信息处理,存储到相应的结构体中元素中
- *******************************************************/
- void TSGPS_GPRMC_Handle(unsigned char *ch,unsigned char ch1) //1处
- {
- unsigned char x,y;
- // send_byte('d');
- switch(ch1)
- {
- case 0: //时间
- for(x=0,y=0;x<8;x++)
- {
- if(x==2||x==5){ sutGpsInfo.Time[x]=0x3a;}
- else sutGpsInfo.Time[x]=ch[y++];
- }
- // send_byte(time[7]);
- break;
- case 1: //状态
- sutGpsInfo.Status=ch[0];
- break;
- case 2: //纬度
- for(x=10,y=0;x<17;x++)
- {
- if(x==12||x==15) {}
- else sutGpsInfo.Location[x]=ch[y++];
- }
- break;
- case 3: //南北纬
- sutGpsInfo.Location[9]=ch[0];
- break;
- case 4: //经度
- for(x=1,y=0;x<8;x++){
- if(x==4||x==7){}
- else sutGpsInfo.Location[x]=ch[y++];
- }
- break;
- case 5: //东西经
- sutGpsInfo.Location[0]=ch[0];
- break;
- case 6: //航速
- for(x=0;x<6;x++){
- sutGpsInfo.Speed[x+1]=ch[x];
- }
- sutGpsInfo.Speed[6]=0x20;
- sutGpsInfo.Speed[7]=0x20;
- break;
- case 7: //信息自己定义
- break;
- case 8: //信息自己定义
- break;
- case 9: //信息自己定义
- break;
- case 10: //信息自己定义
- break;
- case 11: //信息自己定义
- break;
- default:break;
- }
- }
- /*******************************************************
- 处理GPRMC信息
- *******************************************************/
- void TSGPS_GPRMC_Alysis(char *RMC_Data) //2处
- {
- unsigned char i,j,k;
- unsigned char count;
- unsigned char temp[10];
- //send_byte('a');
- for(i=0;i<strlen(RMC_Data);)
- {
- switch(RMC_Data[i])
- {
- case '$':
- j=i++; //标记$的起始位置
- while(RMC_Data[i]!=',') //RMC_Data[i]==','时结束循环。
- {i++;}
- // uart1_sendbytes(GPRMC[RMC_Count].RMC_ID);
- break;
- case ',':
- j=i++; //标记,的起始位置
- while((RMC_Data[i]!=',')&&(RMC_Data[i]!='*')) //RMC_Data[i]==','或者=='*'时结束循环。
- {i++;}
- if(j+1!=i)
- {
-
- for(j=j+1,k=0;j<i;j++)
- {
- temp[k++]=RMC_Data[j];
- }
- temp[k]='\0';
- // send_byte(temp[0]);
- }
- else temp[0]='\0';
- // send_byte('c');
- TSGPS_GPRMC_Handle(temp,count++);
- break;
- case '*':
- i=strlen(RMC_Data); //处理完成,软件结束。
- break;
- default:break;
-
- }
- }
- // send_byte('e');
- }
- void TSGpsDataInit(void)
- {
- unsigned char *p;
- char buf2[20];
- p=(unsigned char *)&sutGpsSendData;
- memset(p, 0, sizeof(SUT_GPS_SEND_DATA));
- p=(unsigned char *)&sutGpsInfo;
- memset(p, 0, sizeof(SUT_GPS_INF));
-
- sutGpsInfo.isGpsAuthOk = 0;//未鉴权
- }
- /*************************************************************************
- *检查GPS 服务器发过来的数据包是否正确
- 非法 返回0
- 合法 返回命令码
- **************************************************************************/
- //"7e8300 000c 000009771810000001 4130313233343536373839 b87e"
- unsigned char TSGpsServerCheckPack(unsigned char *data,unsigned short DataLen)//3处
- {
- unsigned char cmd;
- unsigned short len;
- unsigned long PSN;
-
- //判断数据包的包头是否正确
- // escapse(data,DataLen);
- if(data[0]!=TSGPS_PACK_HEAD)return 0;
- //判断数据包的校验是否正确
- if(data[DataLen-2] != TSGetCheckSum(data,DataLen-2))
- return 255;
- //判断数据包的结尾是否正确
- if(data[DataLen-1] != TSGPS_PACK_END)
- return 255;
- //获取数据包的长度 并判断长度是否正确
- len=(unsigned short)data[3]*256+data[4];
- if(DataLen!=(len+5))return 255;
- // //判断终端编号是否正确
- // PSN=(unsigned long)data[5]<<24;
- // PSN+=(unsigned long)data[6]<<16;
- // PSN+=(unsigned long)data[7]<<8;
- // PSN+=(unsigned long)data[8];
- // if(PSN!=sutNetPara.PSN) return 0;
- //解析当前命令
- cmd = data[2];
- sutGpsInfo.HostCmd = cmd;
- return cmd;
- }
- int turn(int a)
- {
- a = ~a + 1;
- return a;
- }
- /**************************************************************************************************
- *看下 如果一直发注册信息会如何
- ***************************************************************************************************/
- void TSGpsTimingSendPos(void)
- {
- unsigned char SendBuf[100];
- // unsigned char Jbuf[10];
- // unsigned char Wbuf[10];
-
- signed int Jtmp;
- signed int Wtmp;
-
- // SlwTrace(INF,"TSGpsTimingSendPos",1);
- // TSMakeGpsSendData();
- // PacketLen=TSGpsPacket(SendBuf,sutProductPara.PSN,TS_LOCATION_INFORMATION_REPORTING,(unsigned char *)&sutGpsSendData,sizeof(SUT_GPS_SEND_DATA));
- // MC8332ModemSendTcpData(GPS_DATA_SOCKET,SendBuf,PacketLen);
- /***************M20*************///打包发送GPS数据。
-
- //AT+POC=525&时间&经度&纬度&高度&速度&方位&卫星数
-
- //time= ((sutGpsInfo.hour/16*10+sutGpsInfo.hour%16)*3600+(sutGpsInfo.minu/16*10+sutGpsInfo.minu%16)*60+(sutGpsInfo.sec/16*10+sutGpsInfo.sec%16)); //时间
- GetCoordinates(1);
- // snprintf(Jbuf,sizeof(Jbuf),"%9d",sutGpsInfo.testJD); //经度 纬度
- // snprintf(Wbuf,sizeof(Wbuf),"%9d",sutGpsInfo.testWD);
- if(sutGpsInfo.EWFlag=='W') Jtmp= turn(sutGpsInfo.testJD);
- else Jtmp=sutGpsInfo.testJD;
- if(sutGpsInfo.NSFlag=='S') Wtmp= turn(sutGpsInfo.testWD);//S
- else Wtmp=sutGpsInfo.testWD;
- snprintf(SendBuf,sizeof(SendBuf),"AT+POC=525&%9d&%d&%d&%d&%d&%d&%d\r\n",sutGpsInfo.SendTime,Jtmp,Wtmp,sutGpsInfo.hight,sutGpsInfo.AvgSpeed,sutGpsInfo.aspect,sutGpsInfo.startnum);
- SlwTrace(INF,"GPS start/**************************************************************/",1);
- ModemSendAT(SendBuf);
- SlwTrace(INF,SendBuf,1);
- SlwTrace(INF,"GPS end/***************************************************************/",1);
- return ;
- }
- /*********************************************************
- *************************************************************/
- unsigned long power(int base, int times)
- {
- int i;
- unsigned long rslt = 1;
- for(i=0; i<times; i++){
- rslt *= base;
- }
- return rslt;
- }
- /********************************************************
- BCD转10进制
- ********************************************************/
- unsigned long BCDtoDec(const unsigned char *bcd, int length)
- {
- int i, tmp;
- unsigned long dec = 0;
- for(i=0; i<length; i++)
- {
- tmp = ((bcd[i]>>4)&0x0F)*10 + (bcd[i]&0x0F);
- dec += tmp * power(100, length-1-i);
- }
- return dec;
- }
- /********************************************************
- 10进制转BCD
- ********************************************************/
- int DectoBCD(int Dec, unsigned char *Bcd, int length)
- {
- int i;
- int temp;
- for(i=length-1; i>=0; i--)
- {
- temp = Dec%100;
- Bcd[i] = ((temp/10)<<4) + ((temp%10) & 0x0F);
- Dec /= 100;
- }
- return 0;
- }
- //#define GPS_DEBUG_GPS_SEND_DATA
- /***************************************************************************
- *MakeGpsSendData
- 根据sutGpsInfo生成sutGpsSendData
- ****************************************************************************/
- void TSMakeGpsSendData(void)//5处
- {
- 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};
- sutGpsSendData.Time[0] = sutGpsInfo.year;
- sutGpsSendData.Time[1] = sutGpsInfo.month;
- sutGpsSendData.Time[2] = sutGpsInfo.day;
- sutGpsSendData.Time[3] = sutGpsInfo.hour;
- sutGpsSendData.Time[4] = sutGpsInfo.minu;
- sutGpsSendData.Time[5] = sutGpsInfo.sec;
- sutGpsSendData.St=0x00;
- sutGpsSendData.status=0;
- if(sutGpsInfo.isGpsValid)
- {
- sutGpsSendData.status |=(1<<24);//b0 1 acc
- sutGpsSendData.status |=(1<<25);//b1 1 定位
- if(sutGpsInfo.NSFlag=='S')
- sutGpsSendData.status |=(1<<26);//b2 1 南纬
- if(sutGpsInfo.EWFlag=='W')
- sutGpsSendData.status |=(1<<27);//b3 1 西经
- sutGpsSendData.status |=(1<<28);//b4 1 运营
- }
- sutGpsSendData.Mileag=((sutGpsInfo.StatisticsMile>>8)&0xFF)|(((sutGpsInfo.StatisticsMile)&0xFF)<<8);
- sutGpsSendData.SSFF=((sutGpsInfo.speed>>8)&0xFF)|(((sutGpsInfo.speed)&0xFF)<<8);
- sutGpsSendData.Acpect=sutGpsInfo.aspect;
-
- GetCoordinates(1);
-
- sutGpsSendData.WWWW[0] = sutGpsInfo.testWD>>24;
- sutGpsSendData.WWWW[1] = sutGpsInfo.testWD>>16;
- sutGpsSendData.WWWW[2] = sutGpsInfo.testWD>>8;
- sutGpsSendData.WWWW[3] = sutGpsInfo.testWD;
- sutGpsSendData.JJJJ[0] = sutGpsInfo.testJD>>24;
- sutGpsSendData.JJJJ[1] = sutGpsInfo.testJD>>16;
- sutGpsSendData.JJJJ[2] = sutGpsInfo.testJD>>8;
- sutGpsSendData.JJJJ[3] = sutGpsInfo.testJD;
-
- }
- void AscStrTurnHexStr(char *AscStr, char *HexStr)
- {
- char *pAscStr=AscStr;
- int i=4;
- unsigned char d,h,l;
- while(i){
- d=(unsigned char)*pAscStr++;
- l=d&0x0f;
- h=d>>4;
- *HexStr++=AscToHex(h);
- *HexStr++=AscToHex(l);
- i--;
- }
- *HexStr=0;
- }
|