/******************************************************************************** * 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>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;iBufLen){//À©Õ¹ºóµÄ³¤¶È´óÓÚ»º´æ³¤¶È£¬×ªÒåʧ°Ü 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 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>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;jpacketnum=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; } }