ProtocolPack.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797
  1. /********************************************************************************
  2. * File Name: ProtocolPack.c
  3. * Function Describe: ЭÒé·â°ü
  4. * Explain:
  5. * Writer: ShiLiangWen
  6. * Date: 2019-2-9
  7. *******************************************************************************/
  8. #include "includes.h"
  9. #include "ProtocolPack.h"
  10. #include "devMsg.h"
  11. #include "para.h"
  12. #include "main.h"
  13. /*
  14. ²¿±êЭÒé
  15. |±êʶλ|ÏûϢͷ|ÏûÏ¢Ìå|¼ìÑéÂë|±êʶ|
  16. ·¢ËÍÏûϢʱ£ºÏûÏ¢·â×°¡ª¡ª>¼ÆËã²¢Ìî³äУÑéÂ롪¡ª>תÒå
  17. ½ÓÊÕÏûϢʱ£º×ªÒ廹ԭ¡ª¡ª>Ñé֤УÑéÂ롪¡ª>½âÎöÏûÏ¢
  18. */
  19. #define CRC16_INIT 0xffff
  20. #define PROTO_REDE 0x5d
  21. SUT_PROPACKET sutPropacket;
  22. char *iccid=app.CCID;
  23. unsigned short crc16Check(unsigned short initValue,unsigned char *pBuf,int i){
  24. int j,k;
  25. unsigned short crc16=initValue;
  26. for(j=0;j<i;j++){
  27. crc16^=pBuf[j];
  28. for(k=0;k<8;k++){
  29. if(crc16 & 0x01) crc16 = (crc16 >>1)^0xa001;
  30. else crc16 >>= 1;
  31. }
  32. }
  33. return crc16;
  34. }
  35. /********************************************************
  36. 10½øÖÆתBCD
  37. ********************************************************/
  38. void DecToBCD(unsigned int Dec, unsigned char *Bcd, int length)
  39. {
  40. int i;
  41. int temp;
  42. for(i=length-1; i>=0; i--)
  43. {
  44. temp = Dec%100;
  45. Bcd[i] = ((temp/10)<<4) + ((temp%10) & 0x0F);
  46. Dec /= 100;
  47. }
  48. }
  49. unsigned int overBytes=0;//超出Buffer的字节数
  50. unsigned int getOverBytes(void){return overBytes;}
  51. /*********************************************************************************
  52. ½«·â°üBufÊý¾Ý½øÐÐתÒ壬ͷβ¼Ó±êʶ0x7e,ÖмäÓÐ0x7e µÄ°´ÈçϹæÔòת»»
  53. 0x7e <¡ª¡ª¡ª¡ª> 0x7d ºó½ô¸úÒ»¸ö 0x02£»
  54. 0x7d <¡ª¡ª¡ª¡ª> 0x7d ºó½ô¸úÒ»¸ö 0x01
  55. ·µ»Ø£º
  56. 0--תÒåʧ°Ü£¬Buf¿Õ¼ä²»¹»
  57. >0 תÒåºóBufÓÐЧÊý¾Ý³¤¶È
  58. *********************************************************************************/
  59. int PacketConvert(unsigned char *Buf,int BufLen,int DataLen)
  60. {
  61. int i,j,num,len;
  62. //ÏÈͳ¼ÆBufÖеÄDataÓжàÉÙ¸ö0x7e¡¢0x7d
  63. num=0;
  64. for(i=0;i<DataLen;i++){
  65. if(Buf[i]==PROTO_FLAG || Buf[i]==PROTO_REDE)num++;
  66. }
  67. //À©Õ¹ºóµÄ³¤¶È
  68. len=DataLen+num+2;//¼Ó2ÊÇÒòΪͷβ¶¼Òª¼Ó0x7e
  69. if(len>BufLen){//À©Õ¹ºóµÄ³¤¶È´óÓÚ»º´æ³¤¶È£¬×ªÒåʧ°Ü
  70. overBytes=len-BufLen;
  71. wlog_warn("PacketConvert bufOver:%d,%d,%d,%d,%d",overBytes,BufLen,len,DataLen,num);
  72. return PACKET_CONVERT_LEN_OVER;
  73. }
  74. //β²¿Ìí¼Ó0x7e
  75. Buf[len-1]=PROTO_FLAG;
  76. //ÖмäתÒå
  77. j=len-2;
  78. for(i=DataLen-1;i>0;i--){
  79. if(Buf[i]==PROTO_FLAG){
  80. Buf[j--]=0x02;
  81. Buf[j--]=PROTO_REDE;
  82. }else if(Buf[i]==PROTO_REDE){
  83. Buf[j--]=0x01;
  84. Buf[j--]=PROTO_REDE;
  85. }else{
  86. Buf[j--]=Buf[i];
  87. }
  88. }
  89. //i=0 µÄÖµÒ²Òª´¦ÀíÒ»ÏÂ
  90. if(Buf[0]==PROTO_FLAG){
  91. Buf[j--]=0x02;
  92. Buf[j--]=PROTO_REDE;
  93. }else if(Buf[0]==PROTO_REDE){
  94. Buf[j--]=0x01;
  95. Buf[j--]=PROTO_REDE;
  96. }else{
  97. Buf[j--]=Buf[0];
  98. }
  99. //Í·²¿Ìí¼Ó0x7e
  100. Buf[0]=PROTO_FLAG;
  101. return len;
  102. }
  103. #if 0
  104. void PacketConvertTest(void)
  105. {
  106. int i,len;
  107. unsigned char buf[10]={0x09,0x29,0x7e,0x03,0x7d,0x04};
  108. printf("PacketConvertTest begin!\r\n");
  109. printf("buf1=");
  110. for(i=0;i<6;i++){
  111. printf("%02X ",buf[i]);
  112. }
  113. len=PacketConvert(buf,sizeof(buf),6);
  114. printf("\r\nbuf2=");
  115. for(i=0;i<len;i++){
  116. printf("%02X ",buf[i]);
  117. }
  118. printf("\r\nPacketConvertTest end!\r\n");
  119. }
  120. #endif
  121. /*********************************************************************************
  122. ½«·â°üBufÊý¾Ý½øÐнâתÒ壬ȥµôͷβ±êʶ0x7e,ÖмäÓÐ0x7e µÄ°´ÈçϹæÔòת»»
  123. 0x7e <¡ª¡ª¡ª¡ª> 0x7d ºó½ô¸úÒ»¸ö 0x02£»
  124. 0x7d <¡ª¡ª¡ª¡ª> 0x7d ºó½ô¸úÒ»¸ö 0x01
  125. 0x7d 0x02<¡ª¡ª¡ª¡ª> 0x7e
  126. 0x7d 0x01<¡ª¡ª¡ª¡ª> 0x7d
  127. ·µ»Ø£º
  128. <0 ЭÒé´íÎó
  129. >0 ½âתÒåºóµÄÊý¾Ý³¤¶È
  130. *********************************************************************************/
  131. int PacketDeConvert(unsigned char *pMsg,int DataLen)
  132. {
  133. unsigned char *w,*p,*n;
  134. int i,j;
  135. if(pMsg[0]!=PROTO_FLAG || pMsg[DataLen-1]!=PROTO_FLAG)return -1;
  136. i=0;
  137. j=0;
  138. w=pMsg;
  139. p=pMsg;
  140. n=p+1;
  141. while(i<(DataLen-1)){
  142. if(*p==PROTO_FLAG){
  143. if(i==0){
  144. p++;
  145. n++;
  146. i++;
  147. }else{
  148. return j;
  149. }
  150. }else if(*p==PROTO_REDE){
  151. if(*n==0x01){
  152. *w=PROTO_REDE;
  153. w++;
  154. j++;
  155. p+=2;
  156. n+=2;
  157. i+=2;
  158. }else if(*n==0x02){
  159. *w=PROTO_FLAG;
  160. w++;
  161. j++;
  162. p+=2;
  163. n+=2;
  164. i+=2;
  165. }else return -3;
  166. }else{
  167. *w=*p;
  168. w++;
  169. j++;
  170. p++;
  171. n++;
  172. i++;
  173. }
  174. }
  175. return j;
  176. }
  177. #if 0
  178. void PacketDeConvertTest(void)
  179. {
  180. int i,len;
  181. unsigned char buf[]={
  182. 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,
  183. 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
  184. };
  185. SlwTrace(INF,"PacketDeConvertTest begin!\r\n");
  186. TraceData(buf,sizeof(buf));
  187. len=PacketDeConvert(buf,sizeof(buf));
  188. if(len>0){
  189. TraceData(buf,len);
  190. }else{
  191. sprintf((char *)buf,"len=%d\r\n",len);
  192. SlwTrace(DEBUG,(char *)buf);
  193. }
  194. SlwTrace(INF,"PacketDeConvertTest end!\r\n");
  195. }
  196. #endif
  197. /********************************************************************************
  198. ÏûÏ¢·â×°¡ª¡ª>¼ÆËã²¢Ìî³äУÑéÂë-->תÒå
  199. *********************************************************************************/
  200. int Packet(unsigned short MsgID,unsigned short MsgNum ,unsigned char *pData,int DataLen)
  201. {
  202. SUTDS ds;
  203. unsigned char *pBuf=sutPropacket.pSendBuf;
  204. unsigned char check=0;
  205. int i,j;
  206. if((DataLen+14)>sutPropacket.SendBufLen)return 0;
  207. i=0;
  208. ds.Data.usData=MsgID;
  209. pBuf[i++] = ds.Data.ucData.b2;
  210. pBuf[i++] = ds.Data.ucData.b1;
  211. //ÏûÏ¢ÌåÊôÐÔ
  212. //·Ö°ü=ÎÞ£¬¼ÓÃÜ=ÎÞ£¬³¤¶È=DataLen
  213. ds.Data.usData=DataLen;
  214. pBuf[i++] = ds.Data.ucData.b2;
  215. pBuf[i++] = ds.Data.ucData.b1;
  216. //É豸±àÂë
  217. for(j=0;j<6;j++){
  218. pBuf[i++]=sutPropacket.DeviceID[j];
  219. }
  220. //ÏûÏ¢Á÷Ë®ºÅ
  221. ds.Data.usData=MsgNum;
  222. pBuf[i++] = ds.Data.ucData.b2;
  223. pBuf[i++] = ds.Data.ucData.b1;
  224. //ÏûÏ¢Ìå
  225. if(DataLen>0){
  226. for(j=0;j<DataLen;j++){
  227. pBuf[i++]=pData[j];
  228. }
  229. }
  230. //УÑéÂë
  231. for(j=0;j<i;j++){
  232. check^=pBuf[j];
  233. }
  234. pBuf[i++]=check;
  235. return PacketConvert(pBuf,sutPropacket.SendBufLen,i);
  236. }
  237. /*打包消息头*/
  238. int PackHead(unsigned char *pBuf,unsigned short msgID,unsigned short MsgNum){
  239. int i=0,j;
  240. SUTDS ds;
  241. //消息ID
  242. ds.Data.usData=msgID;
  243. pBuf[i++]=ds.Data.ucData.b2;
  244. pBuf[i++]=ds.Data.ucData.b1;
  245. //消息长度,先置0
  246. pBuf[i++]=0;
  247. pBuf[i++]=0;
  248. //终端号码
  249. for(j=0;j<6;j++) pBuf[i++]=sutPropacket.DeviceID[j];
  250. //流水号
  251. ds.Data.usData=MsgNum;
  252. pBuf[i++] = ds.Data.ucData.b2;
  253. pBuf[i++] = ds.Data.ucData.b1;
  254. return i;
  255. }
  256. /*计算消息体长度及CRC16并填充buf*/
  257. int fillDlenAndCrc16(unsigned short smslen,unsigned char *lenbuf,unsigned char *src,int len){
  258. SUTDS ds;
  259. unsigned short crc16;
  260. int index;
  261. ds.Data.usData=smslen;
  262. lenbuf[0]=ds.Data.ucData.b2;
  263. lenbuf[1]=ds.Data.ucData.b1;
  264. crc16=crc16Check(CRC16_INIT,src,len);
  265. ds.Data.usData=crc16;
  266. index=len;
  267. src[index++]=ds.Data.ucData.b2;
  268. src[index++]=ds.Data.ucData.b1;
  269. return index;
  270. }
  271. /*打包位置信息包*/
  272. int PacketPosition(unsigned short MsgNum,int max_len){
  273. unsigned char *pBuf=sutPropacket.pSendBuf;
  274. int i,devlen,gnsslen,nearlen;
  275. int leftsize;
  276. //打包消息头
  277. i=PackHead(pBuf,MSGID_POSITION,MsgNum);
  278. //打包设备状态信息条数及设备状态信息
  279. leftsize=max_len;//用户数据可以填充的字节数
  280. devlen=GetDeviceStatusInfo(pBuf+i,leftsize-3);//减3是保证设备,GNSS,NEAR都有一个字节去装条数
  281. i += devlen;
  282. //打包GNSS位置信息条数及GNSS位置信息
  283. leftsize -= devlen;//填充了设备信息后,剩下用户可填充数
  284. if(leftsize<=0) goto PACKET_NOW;
  285. gnsslen=GetGnssInfo(pBuf+i,leftsize-2);//减2是保证GNSS,NEAR都有一个字节去装条数
  286. i += gnsslen;
  287. //打包附近设备信息条数及附近设备信息
  288. leftsize -= gnsslen;//填充了GNSS信息后,剩下用户可填充数
  289. if(leftsize<=0) goto PACKET_NOW;
  290. nearlen=GetNearbyDeviceInfo(pBuf+i,leftsize-1);//减1是保证NEAR都有一个字节去装条数
  291. i += nearlen;
  292. if(devlen==1 && gnsslen==1 && nearlen==1) return -1;//数据发完了
  293. PACKET_NOW:
  294. //打包消息体及CRC16
  295. i=fillDlenAndCrc16(i-12,pBuf+2,pBuf,i);
  296. return PacketConvert(pBuf,sutPropacket.SendBufLen,i);
  297. }
  298. /********************************************************************************
  299. 打包告警包
  300. *********************************************************************************/
  301. int PacketWarn(unsigned short MsgNum, DEVICEINFO_DEF *devinfo){
  302. unsigned char *pBuf=sutPropacket.pSendBuf;
  303. int i,k;
  304. //打包消息头
  305. i=PackHead(pBuf,MSGID_POSITION,MsgNum);
  306. //模拟一包空的设备信息,并填充告警状态
  307. pBuf[i++]=1;
  308. k=fillDevInfo(pBuf+i,devinfo);
  309. i += k;
  310. pBuf[i++]=0;//即GNSS个数为0
  311. pBuf[i++]=0;//即NEAR个数为0
  312. //打包消息体及CRC16
  313. i=fillDlenAndCrc16(i-12,pBuf+2,pBuf,i);
  314. return PacketConvert(pBuf,sutPropacket.SendBufLen,i);
  315. } /********************************************************************************
  316. 打包心跳包
  317. *********************************************************************************/
  318. int PacketTick(unsigned short MsgNum){
  319. unsigned char *pBuf=sutPropacket.pSendBuf;
  320. int i;
  321. //打包消息头
  322. i=PackHead(pBuf,MSGID_POSITION,MsgNum);
  323. //模拟空的主体数据包
  324. pBuf[i++]=0;//即设备个数为0
  325. pBuf[i++]=0;//即GNSS个数为0
  326. pBuf[i++]=0;//即NEAR个数为0
  327. //打包消息体及CRC16
  328. i=fillDlenAndCrc16(i-12,pBuf+2,pBuf,i);
  329. return PacketConvert(pBuf,sutPropacket.SendBufLen,i);
  330. }
  331. /*打包业务登陆包*/
  332. int PacketLogin(unsigned short MsgNum){
  333. unsigned char *pBuf=sutPropacket.pSendBuf;
  334. int i,j;
  335. if(NULL==pBuf){
  336. wlog_info("buffer null");
  337. return 0;
  338. }
  339. //打包消息头
  340. i=PackHead(pBuf,MSGID_LOGIN,MsgNum);
  341. //打包设备类型长度及设备类型值
  342. pBuf[i++]=strlen(APP_DEVICE_TYPE);
  343. memcpy(pBuf+i,APP_DEVICE_TYPE,strlen(APP_DEVICE_TYPE));
  344. i += strlen(APP_DEVICE_TYPE);
  345. //打包版本号
  346. pBuf[i++]=(SOFTWARE_BUILD_DATE>>16)&0xff;//0x190412
  347. pBuf[i++]=(SOFTWARE_BUILD_DATE>>8)&0xff;
  348. pBuf[i++]=SOFTWARE_BUILD_DATE&0xff;
  349. pBuf[i++]=(SOFTWARE_BUILD_TIME>>8)&0xff;//0x0001
  350. pBuf[i++]=SOFTWARE_BUILD_TIME&0xff;
  351. //打包ICCID长度及ICCID值
  352. pBuf[i++]=20;
  353. for(j=0;j<20;j++)pBuf[i++]=*(iccid+j);
  354. //打包MAC
  355. char buf[30];
  356. snprintf(buf,sizeof(buf),"%012d",sut_psn.psn);
  357. for(j=0;j<12;j++) pBuf[i++]=buf[j];
  358. //打包消息体及CRC16
  359. i=fillDlenAndCrc16(i-12,pBuf+2,pBuf,i);
  360. return PacketConvert(pBuf,sutPropacket.SendBufLen,i);
  361. }
  362. /*打包IOT验证包*/
  363. int PacketIOT(unsigned short MsgNum){
  364. SUTDS ds;
  365. unsigned char *pBuf=sutPropacket.pSendBuf;
  366. int i,j,k;
  367. if(NULL==pBuf){
  368. wlog_info("buffer null");
  369. return 0;
  370. }
  371. //打包消息头
  372. i=PackHead(pBuf,MSGID_IOT,MsgNum);
  373. //打包设备类型长度及设备类型值
  374. pBuf[i++]=strlen(APP_DEVICE_TYPE);
  375. memcpy(pBuf+i,APP_DEVICE_TYPE,strlen(APP_DEVICE_TYPE));
  376. i += strlen(APP_DEVICE_TYPE);
  377. //打包版本号
  378. pBuf[i++]=(SOFTWARE_BUILD_DATE>>16)&0xff;//0x190412
  379. pBuf[i++]=(SOFTWARE_BUILD_DATE>>8)&0xff;
  380. pBuf[i++]=SOFTWARE_BUILD_DATE&0xff;
  381. pBuf[i++]=(SOFTWARE_BUILD_TIME>>8)&0xff;//0x0001
  382. pBuf[i++]=SOFTWARE_BUILD_TIME&0xff;
  383. //打包ICCID长度及ICCID值
  384. pBuf[i++]=20;
  385. for(j=0;j<20;j++)pBuf[i++]=*(iccid+j);//ICCID
  386. //打包MAC
  387. char buf[200];
  388. snprintf(buf,sizeof(buf),"%012d",sut_psn.psn);
  389. for(j=0;j<12;j++) pBuf[i++]=buf[j];
  390. //打包运营维护信息长度及运营商维护信息
  391. snprintf(buf,sizeof(buf),"{\"Model\":\"%s\",\"SVer\":\"%s\",\"HVer\":\"%d\",\"Sensor\":\"%s\",\"OpVer\":\"%s\",\"FmVer\":\"%s\",\"Btime\":\"%s\"}",
  392. APP_NAME, //Model
  393. APP_VERSION, //SVer
  394. APP_HARDVERSION,//HVer
  395. SENSOR_MODEL, //Sensor
  396. app.opVersion, //opVersion 2.01
  397. app.fmVersion, //fmVersion SV60003.W3.20.25.1.0T13S0702_M61A2_MS5700_OPENCPU_TNF
  398. app.mbtime); //Btime 2020-09-02_18:08:00
  399. k=strlen(buf);
  400. ds.Data.usData=k;
  401. pBuf[i++] = ds.Data.ucData.b2;
  402. pBuf[i++] = ds.Data.ucData.b1;
  403. for(j=0;j<k;j++)pBuf[i++]=buf[j];
  404. //打包消息体及CRC16
  405. i=fillDlenAndCrc16(i-12,pBuf+2,pBuf,i);
  406. return PacketConvert(pBuf,sutPropacket.SendBufLen,i);
  407. }
  408. /*打包升级命令响应包*/
  409. int PackeUpgradeAck(unsigned short MsgNum,unsigned short ackliushui,unsigned char result){
  410. SUTDS ds;
  411. unsigned char *pBuf=sutPropacket.pSendBuf;
  412. int i,j,k;
  413. if(NULL==pBuf){
  414. wlog_info("buffer null");
  415. return 0;
  416. }
  417. //打包消息头
  418. i=PackHead(pBuf,MSGID_IOT,MsgNum);
  419. //打包流水号
  420. ds.Data.usData=ackliushui;
  421. pBuf[i++] = ds.Data.ucData.b2;
  422. pBuf[i++] = ds.Data.ucData.b1;
  423. //打包结果
  424. pBuf[i++] = result;
  425. //打包消息体及CRC16
  426. i=fillDlenAndCrc16(i-12,pBuf+2,pBuf,i);
  427. return PacketConvert(pBuf,sutPropacket.SendBufLen,i);
  428. }
  429. /*打包获取排班信息*/
  430. int PackeGetArrangeList(unsigned short MsgNum,unsigned char *bcd3){
  431. unsigned char *pBuf=sutPropacket.pSendBuf;
  432. int i,j,k;
  433. if(NULL==pBuf){
  434. wlog_info("buffer null");
  435. return 0;
  436. }
  437. //打包消息头
  438. i=PackHead(pBuf,MSGID_ARRANGE,MsgNum);
  439. //当前日期
  440. for(j=0;j<3;j++) pBuf[i++]=bcd3[j];
  441. //打包消息体及CRC16
  442. i=fillDlenAndCrc16(i-12,pBuf+2,pBuf,i);
  443. return PacketConvert(pBuf,sutPropacket.SendBufLen,i);
  444. }
  445. /*打包下行命令响应信息*/
  446. int PackeFeedBackDownCmd(unsigned short MsgNum,unsigned char result){
  447. unsigned char *pBuf=sutPropacket.pSendBuf;
  448. int i,j,k;
  449. if(NULL==pBuf){
  450. wlog_info("buffer null");
  451. return 0;
  452. }
  453. //打包消息头
  454. i=PackHead(pBuf,MSGID_DOWNCMD,MsgNum);
  455. //结果
  456. pBuf[i++]=result;
  457. //打包消息体及CRC16
  458. i=fillDlenAndCrc16(i-12,pBuf+2,pBuf,i);
  459. return PacketConvert(pBuf,sutPropacket.SendBufLen,i);
  460. }
  461. /*打包语音事件上报命令包*/
  462. int PackeVoiceEvent(unsigned short MsgNum, unsigned char eventType,unsigned int vid){
  463. unsigned char *pBuf=sutPropacket.pSendBuf;
  464. int i;
  465. SUTDL dl;
  466. if(NULL==pBuf){
  467. wlog_info("buffer null");
  468. return 0;
  469. }
  470. //打包消息头
  471. i=PackHead(pBuf,MSGID_VOICE_EVENT,MsgNum);
  472. //打包vid
  473. dl.Data.ulData=vid;
  474. pBuf[i++] = dl.Data.ucData.b4;
  475. pBuf[i++] = dl.Data.ucData.b3;
  476. pBuf[i++] = dl.Data.ucData.b2;
  477. pBuf[i++] = dl.Data.ucData.b1;
  478. //业务事件类型
  479. pBuf[i++]=eventType;
  480. //打包消息体及CRC16
  481. i=fillDlenAndCrc16(i-12,pBuf+2,pBuf,i);
  482. return PacketConvert(pBuf,sutPropacket.SendBufLen,i);
  483. }
  484. /*打包语音包头信息命令包*/
  485. int PackeVoiceInfo(unsigned short MsgNum,unsigned char vType,unsigned short vLen,unsigned char vPacketNum,unsigned int vid){
  486. unsigned char *pBuf=sutPropacket.pSendBuf;
  487. int i;
  488. SUTDL dl;
  489. SUTDS ds;
  490. if(NULL==pBuf){
  491. wlog_info("buffer null");
  492. return 0;
  493. }
  494. //打包消息头
  495. i=PackHead(pBuf,MSGID_VOICE_INFO,MsgNum);
  496. //打包vid
  497. dl.Data.ulData=vid;
  498. pBuf[i++] = dl.Data.ucData.b4;
  499. pBuf[i++] = dl.Data.ucData.b3;
  500. pBuf[i++] = dl.Data.ucData.b2;
  501. pBuf[i++] = dl.Data.ucData.b1;
  502. //语音类型
  503. pBuf[i++] = vType;
  504. //长度
  505. ds.Data.usData=vLen;
  506. pBuf[i++] = ds.Data.ucData.b2;
  507. pBuf[i++] = ds.Data.ucData.b1;
  508. //分包数
  509. pBuf[i++]=vPacketNum;
  510. //打包消息体及CRC16
  511. i=fillDlenAndCrc16(i-12,pBuf+2,pBuf,i);
  512. return PacketConvert(pBuf,sutPropacket.SendBufLen,i);
  513. }
  514. /*打包到语音数据信息命令包*/
  515. int PackeVoiceData(unsigned short MsgNum, unsigned char subPacketIndex,unsigned short vLen,unsigned char *vData,unsigned int vid){
  516. unsigned char *pBuf=sutPropacket.pSendBuf;
  517. int i,j;
  518. SUTDL dl;
  519. SUTDS ds;
  520. if(NULL==pBuf){
  521. wlog_info("buffer null");
  522. return 0;
  523. }
  524. //打包消息头
  525. i=PackHead(pBuf,MSGID_VOICE_DATA,MsgNum);
  526. //打包vid
  527. dl.Data.ulData=vid;
  528. pBuf[i++] = dl.Data.ucData.b4;
  529. pBuf[i++] = dl.Data.ucData.b3;
  530. pBuf[i++] = dl.Data.ucData.b2;
  531. pBuf[i++] = dl.Data.ucData.b1;
  532. //分包索引
  533. pBuf[i++] = subPacketIndex;
  534. //长度
  535. ds.Data.usData=vLen;
  536. pBuf[i++] = ds.Data.ucData.b2;
  537. pBuf[i++] = ds.Data.ucData.b1;
  538. //数据体
  539. for(j=0;j<vLen;j++) pBuf[i++] = vData[j];
  540. //打包消息体及CRC16
  541. i=fillDlenAndCrc16(i-12,pBuf+2,pBuf,i);
  542. return PacketConvert(pBuf,sutPropacket.SendBufLen,i);
  543. }
  544. /*打包到语音验证信息命令包*/
  545. int PackeVoiceAuth(unsigned short MsgNum, unsigned int vid){
  546. unsigned char *pBuf=sutPropacket.pSendBuf;
  547. int i;
  548. SUTDL dl;
  549. if(NULL==pBuf){
  550. wlog_info("buffer null");
  551. return 0;
  552. }
  553. //打包消息头
  554. i=PackHead(pBuf,MSGID_VOICE_AUTH,MsgNum);
  555. //打包vid
  556. dl.Data.ulData=vid;
  557. pBuf[i++] = dl.Data.ucData.b4;
  558. pBuf[i++] = dl.Data.ucData.b3;
  559. pBuf[i++] = dl.Data.ucData.b2;
  560. pBuf[i++] = dl.Data.ucData.b1;
  561. //打包消息体及CRC16
  562. i=fillDlenAndCrc16(i-12,pBuf+2,pBuf,i);
  563. return PacketConvert(pBuf,sutPropacket.SendBufLen,i);
  564. }
  565. int PackeVoiceLoadInfoAck(unsigned short MsgNum,unsigned char result,unsigned short liushui,unsigned int fid){
  566. unsigned char *pBuf=sutPropacket.pSendBuf;
  567. int i;
  568. SUTDS ds;
  569. SUTDL dl;
  570. if(NULL==pBuf){
  571. wlog_info("buffer null");
  572. return 0;
  573. }
  574. //打包消息头
  575. i=PackHead(pBuf,MSGID_VOICE_LOAD_INFO,MsgNum);
  576. //流水
  577. ds.Data.usData=liushui;
  578. pBuf[i++] = ds.Data.ucData.b2;
  579. pBuf[i++] = ds.Data.ucData.b1;
  580. //结果
  581. pBuf[i++] = result;
  582. //FID
  583. dl.Data.ulData=fid;
  584. pBuf[i++] = dl.Data.ucData.b4;
  585. pBuf[i++] = dl.Data.ucData.b3;
  586. pBuf[i++] = dl.Data.ucData.b2;
  587. pBuf[i++] = dl.Data.ucData.b1;
  588. //打包消息体及CRC16
  589. i=fillDlenAndCrc16(i-12,pBuf+2,pBuf,i);
  590. return PacketConvert(pBuf,sutPropacket.SendBufLen,i);
  591. }
  592. int PackeVoiceLoadAuthAck(unsigned short MsgNum,unsigned char result,unsigned short liushui,unsigned char *userData,unsigned char num,unsigned int fid){
  593. unsigned char *pBuf=sutPropacket.pSendBuf;
  594. int i,j;
  595. SUTDS ds;
  596. SUTDL dl;
  597. if(NULL==pBuf){
  598. wlog_info("buffer null");
  599. return 0;
  600. }
  601. //打包消息头
  602. i=PackHead(pBuf,MSGID_VOICE_LOAD_AUTH,MsgNum);
  603. //流水
  604. ds.Data.usData=liushui;
  605. pBuf[i++] = ds.Data.ucData.b2;
  606. pBuf[i++] = ds.Data.ucData.b1;
  607. //结果
  608. pBuf[i++] = result;
  609. //FID
  610. dl.Data.ulData=fid;
  611. pBuf[i++] = dl.Data.ucData.b4;
  612. pBuf[i++] = dl.Data.ucData.b3;
  613. pBuf[i++] = dl.Data.ucData.b2;
  614. pBuf[i++] = dl.Data.ucData.b1;
  615. //缺少索引的个数
  616. pBuf[i++] = num;
  617. //缺少索引的值
  618. for(j=0;j<num;j++) pBuf[i++]=userData[j];
  619. //打包消息体及CRC16
  620. i=fillDlenAndCrc16(i-12,pBuf+2,pBuf,i);
  621. return PacketConvert(pBuf,sutPropacket.SendBufLen,i);
  622. }
  623. int PackeVoiceLoadTtsAck(unsigned short MsgNum,unsigned char result,unsigned short liushui,unsigned int fid){
  624. unsigned char *pBuf=sutPropacket.pSendBuf;
  625. int i,j;
  626. SUTDS ds;
  627. SUTDL dl;
  628. if(NULL==pBuf){
  629. wlog_info("buffer null");
  630. return 0;
  631. }
  632. //打包消息头
  633. i=PackHead(pBuf,MSGID_VOICE_LOAD_TTS,MsgNum);
  634. //流水
  635. ds.Data.usData=liushui;
  636. pBuf[i++] = ds.Data.ucData.b2;
  637. pBuf[i++] = ds.Data.ucData.b1;
  638. //结果
  639. pBuf[i++] = result;
  640. //FID
  641. dl.Data.ulData=fid;
  642. pBuf[i++] = dl.Data.ucData.b4;
  643. pBuf[i++] = dl.Data.ucData.b3;
  644. pBuf[i++] = dl.Data.ucData.b2;
  645. pBuf[i++] = dl.Data.ucData.b1;
  646. //打包消息体及CRC16
  647. i=fillDlenAndCrc16(i-12,pBuf+2,pBuf,i);
  648. return PacketConvert(pBuf,sutPropacket.SendBufLen,i);
  649. }
  650. /*
  651. ¼ì²é½ÓÊÕµ½µÄÊý¾Ý°üÊÇ·ñ·ûºÏÒªÇó
  652. ÊäÈ룺pData/len Èç:pData=7E8001000500000000000000000000020000867E len=20
  653. Êä³ö£º
  654. УÑéʧ°Ü£¬·µ»ØСÓÚ0µÄÖµ,Èç-1
  655. УÑé³É¹¦£¬·µ»ØÓ¦´ðID£¬Èç0x8001
  656. Èç¹û³É¹¦£¬Ó¦´øÏûÏ¢ID·ÅÔÚAckID£¬Ó¦´ðÏûÏ¢Á÷Ë®ºÅ´æ·Åµ½AckNum
  657. */
  658. int CheckPacket(unsigned char *pData,unsigned short DataLen,unsigned short *smsLen)
  659. {
  660. unsigned short MsgID,tlen;
  661. int i;
  662. unsigned short checksum=0,tcrc;
  663. int len;
  664. //pData=7E8001000500000000000000000000020000867E
  665. //TraceData(pData,DataLen);
  666. len=PacketDeConvert(pData,DataLen);
  667. if(len<0){
  668. return -1;
  669. }
  670. //TraceData(pData,len);
  671. //pData=8001 0005 000000000000 0000 0000 0200 00 86
  672. // ID ÊôÐÔ É豸±àºÅ Á÷Ë®ºÅ Ó¦´ðÁ÷Ë®ºÅ Ó¦´ðID ½á¹û УÑé
  673. #ifndef SKIP_SMSLEN
  674. tlen=((unsigned short)pData[2]<<8)|pData[3];
  675. if(len != (2+2+6+2+tlen+2)) return -2;
  676. *smsLen=tlen;
  677. #endif
  678. //УÑéÂëÑéÖ¤
  679. #ifndef SKIP_CRC16
  680. checksum=crc16Check(CRC16_INIT,pData,len-2);
  681. tcrc=((unsigned short)pData[len-2]<<8)|pData[len-1];
  682. if(checksum!=tcrc){
  683. wlog_warn("crc16:%04x,%04x",checksum,tcrc);
  684. return -3;
  685. }
  686. #endif
  687. //É豸±àºÅÑéÖ¤
  688. for(i=0;i<6;i++){
  689. if(sutPropacket.DeviceID[i]!=pData[4+i])break;
  690. }
  691. if(i<6)return -4;
  692. MsgID=((unsigned short)pData[0]<<8)|pData[1];
  693. return MsgID;
  694. }
  695. void ProtocolPackInit(
  696. unsigned long DeviceId,
  697. unsigned char *pSendBuf,unsigned short SendBufLen,
  698. unsigned char *pRecvBuf,unsigned short RecvBufLen)
  699. {
  700. sutPropacket.DeviceID[0]=0;
  701. DecToBCD(DeviceId,&sutPropacket.DeviceID[1],5);
  702. sutPropacket.pSendBuf=pSendBuf;
  703. sutPropacket.SendBufLen=SendBufLen;
  704. sutPropacket.pRecvBuf=pRecvBuf;
  705. sutPropacket.RecvBufLen=RecvBufLen;
  706. wlog_info("psn:%d,bcdpsn:%02x%02x%02x%02x%02x%02x",
  707. DeviceId,
  708. sutPropacket.DeviceID[0],
  709. sutPropacket.DeviceID[1],
  710. sutPropacket.DeviceID[2],
  711. sutPropacket.DeviceID[3],
  712. sutPropacket.DeviceID[4],
  713. sutPropacket.DeviceID[5]);
  714. }
  715. /*预计算长度为buf_size的buf数据中被转义后需要多少个包来装,每包的长度最大为max_size_pack*/
  716. /*对数据分包进行部标协议预处理得到包数及每包的buf长度数据*/
  717. /*packetInfo 包括总数,每个包的索引包号及包长*/
  718. void calPostPackNum(unsigned char *buf, unsigned int buf_size, unsigned int max_size_pack,PACKETLIST_DEF *packetInfo){
  719. unsigned int bsize=buf_size;
  720. unsigned char *data=buf;
  721. unsigned int pack_size=max_size_pack;
  722. unsigned int tmp;
  723. int ret;
  724. unsigned char index=0;
  725. packetInfo->packetnum=0;
  726. while(bsize != 0){
  727. tmp=bsize;
  728. if(tmp>max_size_pack) tmp=max_size_pack;
  729. P_RETRY:
  730. ret=PackeVoiceData(PROTO_FLAG,index,tmp,data,PROTO_FLAG);//尝试填充
  731. if(ret==PACKET_CONVERT_LEN_OVER){//转义后长度不够,缩小%5再尝试
  732. tmp -=getOverBytes();
  733. goto P_RETRY;
  734. }
  735. //合格,记录一下
  736. packetInfo->pInfo[packetInfo->packetnum].packetindex=index;
  737. packetInfo->pInfo[packetInfo->packetnum].packetlen=tmp;
  738. packetInfo->pInfo[packetInfo->packetnum].packetdata=data;
  739. wlog_info("prePacket[%d]:index=%d,len=%d,addr=%x",packetInfo->packetnum,index,tmp,data);
  740. //移动
  741. packetInfo->packetnum++;
  742. index ++;
  743. data += tmp;
  744. bsize -= tmp;
  745. }
  746. }