📄 msgpkt.c
字号:
/***************************************************************************** FileName: msgpkt.c* Revision: 2008/08/04* Author: Du Biao***************************************************************************** DESCRIPTION: process pkt send,read and check** COPYRIGHT: (C) HEBEI FAR-EAST HARRIS COMMUNICATIONS COMPANY LTD.* HISTORY: 08/08/04: Initial verison by Du Biao**/#include <stdio.h>#include <string.h>#include <stdlib.h>#include "../inc/version.h"#include "../inc/gendef.h"#include "../inc/channel.h"#include "../inc/msgpkt.h"//#undef MSGPKT_DEBUG#define MSGPKT_DEBUG#ifdef MSGPKT_DEBUG #define MSGPKT_DPRINTF( x... ) printf("msgpkt_pthread: \t" ##x)#else #define MAGPKT_DPRINTF( x... )#endif/************************************************************************* PktCalcCRC*************************************************************************FUNCTION: get the date CRCINPUTS: pData: data which will do CRC; iDatalength:length of pDataOUTPUTS: noneRETURN: 0 is OK, otherwise error**/unsigned char PktCalcCRC(unsigned char *pData,int iDataLength){ int i; int iDemoLength; unsigned char CRC = 0; iDemoLength = iDataLength*8; //debug for( i = 0; i < iDemoLength; i++) { /*if bit i in telegram XOR bit to shift out*/ if(((pData[i >> 3]>>(i & 7))^CRC) & 1) { CRC=(CRC >> 1)^0xCF; } else { CRC >>= 1; } } return CRC;/* } else { MSGPKT_DPRINTF("CRC ERROR over %d\n", (iDemoLength >> 3)); }*/ }/************************************************************************* Sendpkt*************************************************************************FUNCTION: build and sendpkt to channelINPUTS: dev: device handle,event: msg type, info: buffer to send infolen:length of infoOUTPUTS: noneRETURN: -1 is error, otherwise ok**/int SendPkt(int dev,U8 event,FILE_DATA* info,U32 infolen){ PKT outpkt;//char aaa[100]; if(infolen > MAX_FILE_DATA_LEN+7) { MSGPKT_DPRINTF(" pkt send is too large\n"); return -1; } memset((U8*)&outpkt,'\0',sizeof(PKT)); outpkt.Start = START_BYTE; outpkt.MsgType = event; if((info != NULL) && (infolen != 0)) { //memcpy(outpkt.Msg, info, infolen); outpkt.Msg.Index = info->Index; outpkt.Msg.Sum = info->Sum; outpkt.Msg.BCC = info->BCC; outpkt.Msg.DataLen = info->DataLen; memset(outpkt.Msg.Data,'0',MAX_FILE_DATA_LEN); memcpy(outpkt.Msg.Data, info->Data, outpkt.Msg.DataLen); }#if 0 MSGPKT_DPRINTF("111hello_tempbuffer.Index is %d\n",outpkt.Msg.Index); MSGPKT_DPRINTF("111hello_tempbuffer.Sum is %d\n",outpkt.Msg.Sum); MSGPKT_DPRINTF("111hello_tempbuffer.DataLen is %d\n",outpkt.Msg.DataLen); MSGPKT_DPRINTF("111hello_tempbuffer.Data is %s\n",outpkt.Msg.Data); MSGPKT_DPRINTF("111hello_tempbuffer.BCC is %d\n",outpkt.Msg.BCC); //MSGPKT_DPRINTF("CHN_Send BUFF is %s\n", &outpkt);#endif return CHN_Send(dev,(U8 *)&outpkt, MAX_FILE_DATA_LEN+9);}/*************************************************************************readpkt*************************************************************************FUNCTION: build and read pkt from channelINPUTS: dev: device handle, pkt: msg to read, infolen: length of msg to sendOUTPUTS: noneRETURN: -1 is error, otherwise ok**/int ReadPkt(int dev,PKT* pkt,U32 infolen){ return CHN_Recv(dev, (U8*) pkt, infolen);}/************************************************************************* CheckPkt*************************************************************************FUNCTION: check the pkt received or get ready for sendINPUTS: pkt: the msg to checkOUTPUTS: noneRETURN: -1 is error, otherwise ok**/U8 CheckPkt(PKT *pkt){ U8 bcc; int info_len; //info_len = ntohs(pkt->Msg.DataLen); info_len = pkt->Msg.DataLen; //printf("check pkt before crc %d,%d\n",pkt->Msg.DataLen,sizeof(pkt->Msg.Data)); bcc = PktCalcCRC(pkt->Msg.Data,info_len); //printf("check pkt after crc\n"); if(bcc == pkt->Msg.BCC) { return 0; } else { return -1; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -