123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797 |
- /********************************************************************************
- * File Name: ProtocolPack.c
- * Function Describe: ÐÒé·â°ü
- * Explain:
- * Writer: ShiLiangWen
- * Date: 2019-2-9
- *******************************************************************************/
- #include "includes.h"
- #include "ProtocolPack.h"
- #include "devMsg.h"
- #include "para.h"
- #include "main.h"
- /*
- ²¿±êÐÒé
- |±êʶλ|ÏûϢͷ|ÏûÏ¢Ìå|¼ìÑéÂë|±êʶ|
- ·¢ËÍÏûϢʱ£ºÏûÏ¢·â×°¡ª¡ª>¼ÆËã²¢Ìî³äУÑéÂ롪¡ª>תÒå
- ½ÓÊÕÏûϢʱ£º×ªÒ廹ԡª¡ª>Ñé֤УÑéÂ롪¡ª>½âÎöÏûÏ¢
- */
- #define CRC16_INIT 0xffff
- #define PROTO_REDE 0x5d
- SUT_PROPACKET sutPropacket;
- char *iccid=app.CCID;
- unsigned short crc16Check(unsigned short initValue,unsigned char *pBuf,int i){
- int j,k;
- unsigned short crc16=initValue;
- for(j=0;j<i;j++){
- crc16^=pBuf[j];
- for(k=0;k<8;k++){
- if(crc16 & 0x01) crc16 = (crc16 >>1)^0xa001;
- else crc16 >>= 1;
- }
- }
- return crc16;
- }
- /********************************************************
- 10½øÖÆתBCD
- ********************************************************/
- void DecToBCD(unsigned 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;
- }
- }
- unsigned int overBytes=0;//超出Buffer的字节数
- unsigned int getOverBytes(void){return overBytes;}
- /*********************************************************************************
- ½«·â°üBufÊý¾Ý½øÐÐתÒ壬ͷβ¼Ó±êʶ0x7e,ÖмäÓÐ0x7e µÄ°´ÈçϹæÔòת»»
- 0x7e <¡ª¡ª¡ª¡ª> 0x7d ºó½ô¸úÒ»¸ö 0x02£»
- 0x7d <¡ª¡ª¡ª¡ª> 0x7d ºó½ô¸úÒ»¸ö 0x01
- ·µ»Ø£º
- 0--תÒåʧ°Ü£¬Buf¿Õ¼ä²»¹»
- >0 תÒåºóBufÓÐЧÊý¾Ý³¤¶È
- *********************************************************************************/
- int PacketConvert(unsigned char *Buf,int BufLen,int DataLen)
- {
- int i,j,num,len;
- //ÏÈͳ¼ÆBufÖеÄDataÓжàÉÙ¸ö0x7e¡¢0x7d
- num=0;
- for(i=0;i<DataLen;i++){
- if(Buf[i]==PROTO_FLAG || Buf[i]==PROTO_REDE)num++;
- }
- //À©Õ¹ºóµÄ³¤¶È
- len=DataLen+num+2;//¼Ó2ÊÇÒòΪͷβ¶¼Òª¼Ó0x7e
- if(len>BufLen){//À©Õ¹ºóµÄ³¤¶È´óÓÚ»º´æ³¤¶È£¬×ªÒåʧ°Ü
- overBytes=len-BufLen;
- wlog_warn("PacketConvert bufOver:%d,%d,%d,%d,%d",overBytes,BufLen,len,DataLen,num);
- return PACKET_CONVERT_LEN_OVER;
- }
- //β²¿Ìí¼Ó0x7e
- Buf[len-1]=PROTO_FLAG;
- //ÖмäתÒå
- j=len-2;
- for(i=DataLen-1;i>0;i--){
- if(Buf[i]==PROTO_FLAG){
- Buf[j--]=0x02;
- Buf[j--]=PROTO_REDE;
- }else if(Buf[i]==PROTO_REDE){
- Buf[j--]=0x01;
- Buf[j--]=PROTO_REDE;
- }else{
- Buf[j--]=Buf[i];
- }
- }
- //i=0 µÄÖµÒ²Òª´¦ÀíÒ»ÏÂ
-
- if(Buf[0]==PROTO_FLAG){
- Buf[j--]=0x02;
- Buf[j--]=PROTO_REDE;
- }else if(Buf[0]==PROTO_REDE){
- Buf[j--]=0x01;
- Buf[j--]=PROTO_REDE;
- }else{
- Buf[j--]=Buf[0];
- }
-
- //Í·²¿Ìí¼Ó0x7e
- Buf[0]=PROTO_FLAG;
- return len;
- }
- #if 0
- void PacketConvertTest(void)
- {
- int i,len;
- unsigned char buf[10]={0x09,0x29,0x7e,0x03,0x7d,0x04};
- printf("PacketConvertTest begin!\r\n");
- printf("buf1=");
- for(i=0;i<6;i++){
- printf("%02X ",buf[i]);
- }
-
- len=PacketConvert(buf,sizeof(buf),6);
- printf("\r\nbuf2=");
- for(i=0;i<len;i++){
- printf("%02X ",buf[i]);
- }
- printf("\r\nPacketConvertTest end!\r\n");
- }
- #endif
- /*********************************************************************************
- ½«·â°üBufÊý¾Ý½øÐнâתÒ壬ȥµôͷβ±êʶ0x7e,ÖмäÓÐ0x7e µÄ°´ÈçϹæÔòת»»
- 0x7e <¡ª¡ª¡ª¡ª> 0x7d ºó½ô¸úÒ»¸ö 0x02£»
- 0x7d <¡ª¡ª¡ª¡ª> 0x7d ºó½ô¸úÒ»¸ö 0x01
- 0x7d 0x02<¡ª¡ª¡ª¡ª> 0x7e
- 0x7d 0x01<¡ª¡ª¡ª¡ª> 0x7d
- ·µ»Ø£º
- <0 ÐÒé´íÎó
- >0 ½âתÒåºóµÄÊý¾Ý³¤¶È
- *********************************************************************************/
- int PacketDeConvert(unsigned char *pMsg,int DataLen)
- {
- unsigned char *w,*p,*n;
- int i,j;
- if(pMsg[0]!=PROTO_FLAG || pMsg[DataLen-1]!=PROTO_FLAG)return -1;
-
- i=0;
- j=0;
- w=pMsg;
- p=pMsg;
- n=p+1;
- while(i<(DataLen-1)){
- if(*p==PROTO_FLAG){
- if(i==0){
- p++;
- n++;
- i++;
- }else{
- return j;
- }
- }else if(*p==PROTO_REDE){
- if(*n==0x01){
- *w=PROTO_REDE;
- w++;
- j++;
- p+=2;
- n+=2;
- i+=2;
- }else if(*n==0x02){
- *w=PROTO_FLAG;
- w++;
- j++;
- p+=2;
- n+=2;
- i+=2;
- }else return -3;
- }else{
- *w=*p;
- w++;
- j++;
- p++;
- n++;
- i++;
- }
- }
- return j;
- }
- #if 0
- void PacketDeConvertTest(void)
- {
- int i,len;
- unsigned char buf[]={
- 0x7E,0x89,0x29,0x00,0x0E,0x00,0x01,0x90,0x21,0x00,0x03,0x00,0x01,0x19,0x02,0x19,0x17,0x00,0x22,0x01,0x00,0x00,0x68,0x00,0x05,0x00,0x05,0x42,0x7E,
- 0x7E,0x89,0x29,0x00,0x0E,0x00,0x01,0x90,0x21,0x00,0x03,0x00,0x01,0x19,0x02,0x19,0x17,0x00,0x46,0x01,0x00,0x00,0x68,0x00,0x05,0x00,0x05,0x26,0x7E
- };
- SlwTrace(INF,"PacketDeConvertTest begin!\r\n");
- TraceData(buf,sizeof(buf));
- len=PacketDeConvert(buf,sizeof(buf));
- if(len>0){
- TraceData(buf,len);
- }else{
- sprintf((char *)buf,"len=%d\r\n",len);
- SlwTrace(DEBUG,(char *)buf);
- }
- SlwTrace(INF,"PacketDeConvertTest end!\r\n");
- }
- #endif
- /********************************************************************************
- ÏûÏ¢·â×°¡ª¡ª>¼ÆËã²¢Ìî³äУÑéÂë-->תÒå
- *********************************************************************************/
- int Packet(unsigned short MsgID,unsigned short MsgNum ,unsigned char *pData,int DataLen)
- {
- SUTDS ds;
- unsigned char *pBuf=sutPropacket.pSendBuf;
- unsigned char check=0;
- int i,j;
- if((DataLen+14)>sutPropacket.SendBufLen)return 0;
- i=0;
- ds.Data.usData=MsgID;
- pBuf[i++] = ds.Data.ucData.b2;
- pBuf[i++] = ds.Data.ucData.b1;
- //ÏûÏ¢ÌåÊôÐÔ
- //·Ö°ü=ÎÞ£¬¼ÓÃÜ=ÎÞ£¬³¤¶È=DataLen
- ds.Data.usData=DataLen;
- pBuf[i++] = ds.Data.ucData.b2;
- pBuf[i++] = ds.Data.ucData.b1;
- //É豸±àÂë
- for(j=0;j<6;j++){
- pBuf[i++]=sutPropacket.DeviceID[j];
- }
- //ÏûÏ¢Á÷Ë®ºÅ
- ds.Data.usData=MsgNum;
- pBuf[i++] = ds.Data.ucData.b2;
- pBuf[i++] = ds.Data.ucData.b1;
- //ÏûÏ¢Ìå
- if(DataLen>0){
- for(j=0;j<DataLen;j++){
- pBuf[i++]=pData[j];
- }
- }
- //УÑéÂë
- for(j=0;j<i;j++){
- check^=pBuf[j];
- }
- pBuf[i++]=check;
- return PacketConvert(pBuf,sutPropacket.SendBufLen,i);
- }
- /*打包消息头*/
- int PackHead(unsigned char *pBuf,unsigned short msgID,unsigned short MsgNum){
- int i=0,j;
- SUTDS ds;
- //消息ID
- ds.Data.usData=msgID;
- pBuf[i++]=ds.Data.ucData.b2;
- pBuf[i++]=ds.Data.ucData.b1;
- //消息长度,先置0
- pBuf[i++]=0;
- pBuf[i++]=0;
- //终端号码
- for(j=0;j<6;j++) pBuf[i++]=sutPropacket.DeviceID[j];
- //流水号
- ds.Data.usData=MsgNum;
- pBuf[i++] = ds.Data.ucData.b2;
- pBuf[i++] = ds.Data.ucData.b1;
- return i;
- }
- /*计算消息体长度及CRC16并填充buf*/
- int fillDlenAndCrc16(unsigned short smslen,unsigned char *lenbuf,unsigned char *src,int len){
- SUTDS ds;
- unsigned short crc16;
- int index;
- ds.Data.usData=smslen;
- lenbuf[0]=ds.Data.ucData.b2;
- lenbuf[1]=ds.Data.ucData.b1;
- crc16=crc16Check(CRC16_INIT,src,len);
- ds.Data.usData=crc16;
- index=len;
- src[index++]=ds.Data.ucData.b2;
- src[index++]=ds.Data.ucData.b1;
- return index;
- }
- /*打包位置信息包*/
- int PacketPosition(unsigned short MsgNum,int max_len){
- unsigned char *pBuf=sutPropacket.pSendBuf;
- int i,devlen,gnsslen,nearlen;
- int leftsize;
- //打包消息头
- i=PackHead(pBuf,MSGID_POSITION,MsgNum);
- //打包设备状态信息条数及设备状态信息
- leftsize=max_len;//用户数据可以填充的字节数
- devlen=GetDeviceStatusInfo(pBuf+i,leftsize-3);//减3是保证设备,GNSS,NEAR都有一个字节去装条数
- i += devlen;
- //打包GNSS位置信息条数及GNSS位置信息
- leftsize -= devlen;//填充了设备信息后,剩下用户可填充数
- if(leftsize<=0) goto PACKET_NOW;
- gnsslen=GetGnssInfo(pBuf+i,leftsize-2);//减2是保证GNSS,NEAR都有一个字节去装条数
- i += gnsslen;
- //打包附近设备信息条数及附近设备信息
- leftsize -= gnsslen;//填充了GNSS信息后,剩下用户可填充数
- if(leftsize<=0) goto PACKET_NOW;
- nearlen=GetNearbyDeviceInfo(pBuf+i,leftsize-1);//减1是保证NEAR都有一个字节去装条数
- i += nearlen;
- if(devlen==1 && gnsslen==1 && nearlen==1) return -1;//数据发完了
- PACKET_NOW:
- //打包消息体及CRC16
- i=fillDlenAndCrc16(i-12,pBuf+2,pBuf,i);
- return PacketConvert(pBuf,sutPropacket.SendBufLen,i);
- }
- /********************************************************************************
- 打包告警包
- *********************************************************************************/
- int PacketWarn(unsigned short MsgNum, DEVICEINFO_DEF *devinfo){
- unsigned char *pBuf=sutPropacket.pSendBuf;
- int i,k;
- //打包消息头
- i=PackHead(pBuf,MSGID_POSITION,MsgNum);
- //模拟一包空的设备信息,并填充告警状态
- pBuf[i++]=1;
- k=fillDevInfo(pBuf+i,devinfo);
- i += k;
- pBuf[i++]=0;//即GNSS个数为0
- pBuf[i++]=0;//即NEAR个数为0
- //打包消息体及CRC16
- i=fillDlenAndCrc16(i-12,pBuf+2,pBuf,i);
- return PacketConvert(pBuf,sutPropacket.SendBufLen,i);
- } /********************************************************************************
- 打包心跳包
- *********************************************************************************/
- int PacketTick(unsigned short MsgNum){
- unsigned char *pBuf=sutPropacket.pSendBuf;
- int i;
- //打包消息头
- i=PackHead(pBuf,MSGID_POSITION,MsgNum);
- //模拟空的主体数据包
- pBuf[i++]=0;//即设备个数为0
- pBuf[i++]=0;//即GNSS个数为0
- pBuf[i++]=0;//即NEAR个数为0
- //打包消息体及CRC16
- i=fillDlenAndCrc16(i-12,pBuf+2,pBuf,i);
- return PacketConvert(pBuf,sutPropacket.SendBufLen,i);
- }
- /*打包业务登陆包*/
- int PacketLogin(unsigned short MsgNum){
- unsigned char *pBuf=sutPropacket.pSendBuf;
- int i,j;
- if(NULL==pBuf){
- wlog_info("buffer null");
- return 0;
- }
- //打包消息头
- i=PackHead(pBuf,MSGID_LOGIN,MsgNum);
- //打包设备类型长度及设备类型值
- pBuf[i++]=strlen(APP_DEVICE_TYPE);
- memcpy(pBuf+i,APP_DEVICE_TYPE,strlen(APP_DEVICE_TYPE));
- i += strlen(APP_DEVICE_TYPE);
- //打包版本号
- pBuf[i++]=(SOFTWARE_BUILD_DATE>>16)&0xff;//0x190412
- pBuf[i++]=(SOFTWARE_BUILD_DATE>>8)&0xff;
- pBuf[i++]=SOFTWARE_BUILD_DATE&0xff;
- pBuf[i++]=(SOFTWARE_BUILD_TIME>>8)&0xff;//0x0001
- pBuf[i++]=SOFTWARE_BUILD_TIME&0xff;
- //打包ICCID长度及ICCID值
- pBuf[i++]=20;
- for(j=0;j<20;j++)pBuf[i++]=*(iccid+j);
- //打包MAC
- char buf[30];
- snprintf(buf,sizeof(buf),"%012d",sut_psn.psn);
- for(j=0;j<12;j++) pBuf[i++]=buf[j];
- //打包消息体及CRC16
- i=fillDlenAndCrc16(i-12,pBuf+2,pBuf,i);
- return PacketConvert(pBuf,sutPropacket.SendBufLen,i);
- }
- /*打包IOT验证包*/
- int PacketIOT(unsigned short MsgNum){
- SUTDS ds;
- unsigned char *pBuf=sutPropacket.pSendBuf;
- int i,j,k;
- if(NULL==pBuf){
- wlog_info("buffer null");
- return 0;
- }
- //打包消息头
- i=PackHead(pBuf,MSGID_IOT,MsgNum);
- //打包设备类型长度及设备类型值
- pBuf[i++]=strlen(APP_DEVICE_TYPE);
- memcpy(pBuf+i,APP_DEVICE_TYPE,strlen(APP_DEVICE_TYPE));
- i += strlen(APP_DEVICE_TYPE);
- //打包版本号
- pBuf[i++]=(SOFTWARE_BUILD_DATE>>16)&0xff;//0x190412
- pBuf[i++]=(SOFTWARE_BUILD_DATE>>8)&0xff;
- pBuf[i++]=SOFTWARE_BUILD_DATE&0xff;
- pBuf[i++]=(SOFTWARE_BUILD_TIME>>8)&0xff;//0x0001
- pBuf[i++]=SOFTWARE_BUILD_TIME&0xff;
- //打包ICCID长度及ICCID值
- pBuf[i++]=20;
- for(j=0;j<20;j++)pBuf[i++]=*(iccid+j);//ICCID
- //打包MAC
- char buf[200];
- snprintf(buf,sizeof(buf),"%012d",sut_psn.psn);
- for(j=0;j<12;j++) pBuf[i++]=buf[j];
- //打包运营维护信息长度及运营商维护信息
- snprintf(buf,sizeof(buf),"{\"Model\":\"%s\",\"SVer\":\"%s\",\"HVer\":\"%d\",\"Sensor\":\"%s\",\"OpVer\":\"%s\",\"FmVer\":\"%s\",\"Btime\":\"%s\"}",
- APP_NAME, //Model
- APP_VERSION, //SVer
- APP_HARDVERSION,//HVer
- SENSOR_MODEL, //Sensor
- app.opVersion, //opVersion 2.01
- app.fmVersion, //fmVersion SV60003.W3.20.25.1.0T13S0702_M61A2_MS5700_OPENCPU_TNF
- app.mbtime); //Btime 2020-09-02_18:08:00
- k=strlen(buf);
- ds.Data.usData=k;
- pBuf[i++] = ds.Data.ucData.b2;
- pBuf[i++] = ds.Data.ucData.b1;
- for(j=0;j<k;j++)pBuf[i++]=buf[j];
-
- //打包消息体及CRC16
- i=fillDlenAndCrc16(i-12,pBuf+2,pBuf,i);
- return PacketConvert(pBuf,sutPropacket.SendBufLen,i);
- }
- /*打包升级命令响应包*/
- int PackeUpgradeAck(unsigned short MsgNum,unsigned short ackliushui,unsigned char result){
- SUTDS ds;
- unsigned char *pBuf=sutPropacket.pSendBuf;
- int i,j,k;
- if(NULL==pBuf){
- wlog_info("buffer null");
- return 0;
- }
- //打包消息头
- i=PackHead(pBuf,MSGID_IOT,MsgNum);
- //打包流水号
- ds.Data.usData=ackliushui;
- pBuf[i++] = ds.Data.ucData.b2;
- pBuf[i++] = ds.Data.ucData.b1;
- //打包结果
- pBuf[i++] = result;
-
- //打包消息体及CRC16
- i=fillDlenAndCrc16(i-12,pBuf+2,pBuf,i);
- return PacketConvert(pBuf,sutPropacket.SendBufLen,i);
- }
- /*打包获取排班信息*/
- int PackeGetArrangeList(unsigned short MsgNum,unsigned char *bcd3){
- unsigned char *pBuf=sutPropacket.pSendBuf;
- int i,j,k;
- if(NULL==pBuf){
- wlog_info("buffer null");
- return 0;
- }
- //打包消息头
- i=PackHead(pBuf,MSGID_ARRANGE,MsgNum);
- //当前日期
- for(j=0;j<3;j++) pBuf[i++]=bcd3[j];
-
- //打包消息体及CRC16
- i=fillDlenAndCrc16(i-12,pBuf+2,pBuf,i);
- return PacketConvert(pBuf,sutPropacket.SendBufLen,i);
- }
- /*打包下行命令响应信息*/
- int PackeFeedBackDownCmd(unsigned short MsgNum,unsigned char result){
- unsigned char *pBuf=sutPropacket.pSendBuf;
- int i,j,k;
- if(NULL==pBuf){
- wlog_info("buffer null");
- return 0;
- }
- //打包消息头
- i=PackHead(pBuf,MSGID_DOWNCMD,MsgNum);
- //结果
- pBuf[i++]=result;
-
- //打包消息体及CRC16
- i=fillDlenAndCrc16(i-12,pBuf+2,pBuf,i);
- return PacketConvert(pBuf,sutPropacket.SendBufLen,i);
- }
- /*打包语音事件上报命令包*/
- int PackeVoiceEvent(unsigned short MsgNum, unsigned char eventType,unsigned int vid){
- unsigned char *pBuf=sutPropacket.pSendBuf;
- int i;
- SUTDL dl;
- if(NULL==pBuf){
- wlog_info("buffer null");
- return 0;
- }
- //打包消息头
- i=PackHead(pBuf,MSGID_VOICE_EVENT,MsgNum);
- //打包vid
- dl.Data.ulData=vid;
- pBuf[i++] = dl.Data.ucData.b4;
- pBuf[i++] = dl.Data.ucData.b3;
- pBuf[i++] = dl.Data.ucData.b2;
- pBuf[i++] = dl.Data.ucData.b1;
- //业务事件类型
- pBuf[i++]=eventType;
-
- //打包消息体及CRC16
- i=fillDlenAndCrc16(i-12,pBuf+2,pBuf,i);
- return PacketConvert(pBuf,sutPropacket.SendBufLen,i);
- }
- /*打包语音包头信息命令包*/
- int PackeVoiceInfo(unsigned short MsgNum,unsigned char vType,unsigned short vLen,unsigned char vPacketNum,unsigned int vid){
- unsigned char *pBuf=sutPropacket.pSendBuf;
- int i;
- SUTDL dl;
- SUTDS ds;
- if(NULL==pBuf){
- wlog_info("buffer null");
- return 0;
- }
- //打包消息头
- i=PackHead(pBuf,MSGID_VOICE_INFO,MsgNum);
- //打包vid
- dl.Data.ulData=vid;
- pBuf[i++] = dl.Data.ucData.b4;
- pBuf[i++] = dl.Data.ucData.b3;
- pBuf[i++] = dl.Data.ucData.b2;
- pBuf[i++] = dl.Data.ucData.b1;
- //语音类型
- pBuf[i++] = vType;
- //长度
- ds.Data.usData=vLen;
- pBuf[i++] = ds.Data.ucData.b2;
- pBuf[i++] = ds.Data.ucData.b1;
- //分包数
- pBuf[i++]=vPacketNum;
-
- //打包消息体及CRC16
- i=fillDlenAndCrc16(i-12,pBuf+2,pBuf,i);
- return PacketConvert(pBuf,sutPropacket.SendBufLen,i);
- }
- /*打包到语音数据信息命令包*/
- int PackeVoiceData(unsigned short MsgNum, unsigned char subPacketIndex,unsigned short vLen,unsigned char *vData,unsigned int vid){
- unsigned char *pBuf=sutPropacket.pSendBuf;
- int i,j;
- SUTDL dl;
- SUTDS ds;
- if(NULL==pBuf){
- wlog_info("buffer null");
- return 0;
- }
- //打包消息头
- i=PackHead(pBuf,MSGID_VOICE_DATA,MsgNum);
- //打包vid
- dl.Data.ulData=vid;
- pBuf[i++] = dl.Data.ucData.b4;
- pBuf[i++] = dl.Data.ucData.b3;
- pBuf[i++] = dl.Data.ucData.b2;
- pBuf[i++] = dl.Data.ucData.b1;
- //分包索引
- pBuf[i++] = subPacketIndex;
- //长度
- ds.Data.usData=vLen;
- pBuf[i++] = ds.Data.ucData.b2;
- pBuf[i++] = ds.Data.ucData.b1;
- //数据体
- for(j=0;j<vLen;j++) pBuf[i++] = vData[j];
-
- //打包消息体及CRC16
- i=fillDlenAndCrc16(i-12,pBuf+2,pBuf,i);
- return PacketConvert(pBuf,sutPropacket.SendBufLen,i);
- }
- /*打包到语音验证信息命令包*/
- int PackeVoiceAuth(unsigned short MsgNum, unsigned int vid){
- unsigned char *pBuf=sutPropacket.pSendBuf;
- int i;
- SUTDL dl;
- if(NULL==pBuf){
- wlog_info("buffer null");
- return 0;
- }
- //打包消息头
- i=PackHead(pBuf,MSGID_VOICE_AUTH,MsgNum);
- //打包vid
- dl.Data.ulData=vid;
- pBuf[i++] = dl.Data.ucData.b4;
- pBuf[i++] = dl.Data.ucData.b3;
- pBuf[i++] = dl.Data.ucData.b2;
- pBuf[i++] = dl.Data.ucData.b1;
-
- //打包消息体及CRC16
- i=fillDlenAndCrc16(i-12,pBuf+2,pBuf,i);
- return PacketConvert(pBuf,sutPropacket.SendBufLen,i);
- }
- int PackeVoiceLoadInfoAck(unsigned short MsgNum,unsigned char result,unsigned short liushui,unsigned int fid){
- unsigned char *pBuf=sutPropacket.pSendBuf;
- int i;
- SUTDS ds;
- SUTDL dl;
- if(NULL==pBuf){
- wlog_info("buffer null");
- return 0;
- }
- //打包消息头
- i=PackHead(pBuf,MSGID_VOICE_LOAD_INFO,MsgNum);
- //流水
- ds.Data.usData=liushui;
- pBuf[i++] = ds.Data.ucData.b2;
- pBuf[i++] = ds.Data.ucData.b1;
- //结果
- pBuf[i++] = result;
- //FID
- dl.Data.ulData=fid;
- pBuf[i++] = dl.Data.ucData.b4;
- pBuf[i++] = dl.Data.ucData.b3;
- pBuf[i++] = dl.Data.ucData.b2;
- pBuf[i++] = dl.Data.ucData.b1;
- //打包消息体及CRC16
- i=fillDlenAndCrc16(i-12,pBuf+2,pBuf,i);
- return PacketConvert(pBuf,sutPropacket.SendBufLen,i);
- }
- int PackeVoiceLoadAuthAck(unsigned short MsgNum,unsigned char result,unsigned short liushui,unsigned char *userData,unsigned char num,unsigned int fid){
- unsigned char *pBuf=sutPropacket.pSendBuf;
- int i,j;
- SUTDS ds;
- SUTDL dl;
- if(NULL==pBuf){
- wlog_info("buffer null");
- return 0;
- }
- //打包消息头
- i=PackHead(pBuf,MSGID_VOICE_LOAD_AUTH,MsgNum);
- //流水
- ds.Data.usData=liushui;
- pBuf[i++] = ds.Data.ucData.b2;
- pBuf[i++] = ds.Data.ucData.b1;
- //结果
- pBuf[i++] = result;
- //FID
- dl.Data.ulData=fid;
- pBuf[i++] = dl.Data.ucData.b4;
- pBuf[i++] = dl.Data.ucData.b3;
- pBuf[i++] = dl.Data.ucData.b2;
- pBuf[i++] = dl.Data.ucData.b1;
- //缺少索引的个数
- pBuf[i++] = num;
- //缺少索引的值
- for(j=0;j<num;j++) pBuf[i++]=userData[j];
- //打包消息体及CRC16
- i=fillDlenAndCrc16(i-12,pBuf+2,pBuf,i);
- return PacketConvert(pBuf,sutPropacket.SendBufLen,i);
- }
- int PackeVoiceLoadTtsAck(unsigned short MsgNum,unsigned char result,unsigned short liushui,unsigned int fid){
- unsigned char *pBuf=sutPropacket.pSendBuf;
- int i,j;
- SUTDS ds;
- SUTDL dl;
- if(NULL==pBuf){
- wlog_info("buffer null");
- return 0;
- }
- //打包消息头
- i=PackHead(pBuf,MSGID_VOICE_LOAD_TTS,MsgNum);
- //流水
- ds.Data.usData=liushui;
- pBuf[i++] = ds.Data.ucData.b2;
- pBuf[i++] = ds.Data.ucData.b1;
- //结果
- pBuf[i++] = result;
- //FID
- dl.Data.ulData=fid;
- pBuf[i++] = dl.Data.ucData.b4;
- pBuf[i++] = dl.Data.ucData.b3;
- pBuf[i++] = dl.Data.ucData.b2;
- pBuf[i++] = dl.Data.ucData.b1;
-
- //打包消息体及CRC16
- i=fillDlenAndCrc16(i-12,pBuf+2,pBuf,i);
- return PacketConvert(pBuf,sutPropacket.SendBufLen,i);
- }
- /*
- ¼ì²é½ÓÊÕµ½µÄÊý¾Ý°üÊÇ·ñ·ûºÏÒªÇó
- ÊäÈ룺pData/len Èç:pData=7E8001000500000000000000000000020000867E len=20
- Êä³ö£º
- УÑéʧ°Ü£¬·µ»ØСÓÚ0µÄÖµ,Èç-1
- УÑé³É¹¦£¬·µ»ØÓ¦´ðID£¬Èç0x8001
- Èç¹û³É¹¦£¬Ó¦´øÏûÏ¢ID·ÅÔÚAckID£¬Ó¦´ðÏûÏ¢Á÷Ë®ºÅ´æ·Åµ½AckNum
- */
- int CheckPacket(unsigned char *pData,unsigned short DataLen,unsigned short *smsLen)
- {
- unsigned short MsgID,tlen;
- int i;
- unsigned short checksum=0,tcrc;
- int len;
- //pData=7E8001000500000000000000000000020000867E
- //TraceData(pData,DataLen);
- len=PacketDeConvert(pData,DataLen);
- if(len<0){
- return -1;
- }
- //TraceData(pData,len);
- //pData=8001 0005 000000000000 0000 0000 0200 00 86
- // ID ÊôÐÔ É豸±àºÅ Á÷Ë®ºÅ Ó¦´ðÁ÷Ë®ºÅ Ó¦´ðID ½á¹û УÑé
- #ifndef SKIP_SMSLEN
- tlen=((unsigned short)pData[2]<<8)|pData[3];
- if(len != (2+2+6+2+tlen+2)) return -2;
- *smsLen=tlen;
- #endif
- //УÑéÂëÑéÖ¤
- #ifndef SKIP_CRC16
- checksum=crc16Check(CRC16_INIT,pData,len-2);
- tcrc=((unsigned short)pData[len-2]<<8)|pData[len-1];
- if(checksum!=tcrc){
- wlog_warn("crc16:%04x,%04x",checksum,tcrc);
- return -3;
- }
- #endif
- //É豸±àºÅÑéÖ¤
- for(i=0;i<6;i++){
- if(sutPropacket.DeviceID[i]!=pData[4+i])break;
- }
- if(i<6)return -4;
- MsgID=((unsigned short)pData[0]<<8)|pData[1];
- return MsgID;
- }
- void ProtocolPackInit(
- unsigned long DeviceId,
- unsigned char *pSendBuf,unsigned short SendBufLen,
- unsigned char *pRecvBuf,unsigned short RecvBufLen)
- {
- sutPropacket.DeviceID[0]=0;
- DecToBCD(DeviceId,&sutPropacket.DeviceID[1],5);
- sutPropacket.pSendBuf=pSendBuf;
- sutPropacket.SendBufLen=SendBufLen;
- sutPropacket.pRecvBuf=pRecvBuf;
- sutPropacket.RecvBufLen=RecvBufLen;
- wlog_info("psn:%d,bcdpsn:%02x%02x%02x%02x%02x%02x",
- DeviceId,
- sutPropacket.DeviceID[0],
- sutPropacket.DeviceID[1],
- sutPropacket.DeviceID[2],
- sutPropacket.DeviceID[3],
- sutPropacket.DeviceID[4],
- sutPropacket.DeviceID[5]);
- }
- /*预计算长度为buf_size的buf数据中被转义后需要多少个包来装,每包的长度最大为max_size_pack*/
- /*对数据分包进行部标协议预处理得到包数及每包的buf长度数据*/
- /*packetInfo 包括总数,每个包的索引包号及包长*/
- void calPostPackNum(unsigned char *buf, unsigned int buf_size, unsigned int max_size_pack,PACKETLIST_DEF *packetInfo){
- unsigned int bsize=buf_size;
- unsigned char *data=buf;
- unsigned int pack_size=max_size_pack;
- unsigned int tmp;
- int ret;
- unsigned char index=0;
- packetInfo->packetnum=0;
- while(bsize != 0){
- tmp=bsize;
- if(tmp>max_size_pack) tmp=max_size_pack;
- P_RETRY:
- ret=PackeVoiceData(PROTO_FLAG,index,tmp,data,PROTO_FLAG);//尝试填充
- if(ret==PACKET_CONVERT_LEN_OVER){//转义后长度不够,缩小%5再尝试
- tmp -=getOverBytes();
- goto P_RETRY;
- }
- //合格,记录一下
- packetInfo->pInfo[packetInfo->packetnum].packetindex=index;
- packetInfo->pInfo[packetInfo->packetnum].packetlen=tmp;
- packetInfo->pInfo[packetInfo->packetnum].packetdata=data;
- wlog_info("prePacket[%d]:index=%d,len=%d,addr=%x",packetInfo->packetnum,index,tmp,data);
- //移动
- packetInfo->packetnum++;
- index ++;
- data += tmp;
- bsize -= tmp;
- }
- }
|