📄 packagedealnew.c
字号:
//修改共享内存()
IPC_ProcState_Read(PROCID_DISPATCH,CurDispatch_Info,&LocalDispatch_Info);
//LocalDispatch_Info.CarState=0;
LocalDispatch_Info.CommState=0;
LocalDispatch_Info.Proc_Info.PID = getpid();
time(&(LocalDispatch_Info.Proc_Info.ctime));
//Share mem write
IPC_ProcState_Write(PROCID_DISPATCH,CurDispatch_Info,&LocalDispatch_Info);
//IPC_ProcState_Read(PROCID_DISPATCH,CurDispatch_Info,&LocalDispatch_Info);
//put DISPATCH data and com status to sharemem
//printf("DISPATCH read success!\r\n");
//printf("DISPATCH Data CarState:%d\r\n",LocalDispatch_Info.CarState);
//printf("DISPATCH Data Proc_Info.CommState:%d\r\n",LocalDispatch_Info.CommState);
//printf("DISPATCH Data Proc_Info.PID:%d\r\n",LocalDispatch_Info.Proc_Info.PID);
//printf("DISPATCH Data Proc_Info.ctime:%s\r\n",ctime(&(LocalDispatch_Info.Proc_Info.ctime)));
}
else
{
// printf("Here send out data!");
Waitresp = 1;
}
free(echoString);
return 0;
}
}
return 1;
}
//GPS回复信息
int RepeatSendInfo(int sockFD, Package_Info commInter,struct sockaddr_in echoServAddr)
{
char RecvDataAll[ECHOMAX];
unsigned short echoStringLen; /* Length of string to echo */
char *echoString; /* String to send to echo server */
Dispatch_Info LocalDispatch_Info; //Point the share mem
echoStringLen=SendMessageCreate(commInter,RecvDataAll);
echoString=malloc(echoStringLen);
memcpy(echoString,RecvDataAll,echoStringLen);
if (echoStringLen > ECHOMAX) /* Check input length */
printf("word too length!\r\n");
/* Send the string to the server */
if (sendto(sockFD, echoString, echoStringLen, 0, (struct sockaddr *)
&echoServAddr, sizeof(echoServAddr)) != echoStringLen)
{
printf("RepeatSendInfo sendto fail!\r\n");
//修改共享内存()
IPC_ProcState_Read(PROCID_DISPATCH,CurDispatch_Info,&LocalDispatch_Info);
//LocalDispatch_Info.CarState=0;
LocalDispatch_Info.CommState=0;
LocalDispatch_Info.Proc_Info.PID = getpid();
time(&(LocalDispatch_Info.Proc_Info.ctime));
//Share mem write
IPC_ProcState_Write(PROCID_DISPATCH,CurDispatch_Info,&LocalDispatch_Info);
//IPC_ProcState_Read(PROCID_DISPATCH,CurDispatch_Info,&LocalDispatch_Info);
//put DISPATCH data and com status to sharemem
//printf("DISPATCH read success!\r\n");
//printf("DISPATCH Data CarState:%d\r\n",LocalDispatch_Info.CarState);
//printf("DISPATCH Data Proc_Info.CommState:%d\r\n",LocalDispatch_Info.CommState);
//printf("DISPATCH Data Proc_Info.PID:%d\r\n",LocalDispatch_Info.Proc_Info.PID);
//printf("DISPATCH Data Proc_Info.ctime:%s\r\n",ctime(&(LocalDispatch_Info.Proc_Info.ctime)));
return 0;
}
free(echoString);
return 1;
}
//从内存中读出一个没有成功发送的数据包
int Get_UnSendInfo(Package_Info *SendInfo)
{
int i;
// printf("Search in buffer!\n");
for(i=0;i<100;i++)//ECHOMAX;i++)
{
// printf("*%d*",CurDispatch_SendInfo[i].SerialNo);
if(CurDispatch_SendInfo[i].SerialNo!=0)
{
memcpy(SendInfo,&(CurDispatch_SendInfo[i]),sizeof(Package_Info));
return 1;
}
}
return 0;
}
//处理发送队列,参数SendType的值为1时,则把要发送的数据加入到内存的队列中;
//参数SendType的值为2时,则要把发送的数据从内存队列中删除
void Deal_SendInfo(Package_Info SendInfo,int SendType)
{
Package_Info tmpSendInfo[ECHOMAX];
int i,j,k,OldSend;
memset(tmpSendInfo,0,sizeof(tmpSendInfo));
if (SendType==1)
{
j=0;
for (i=0;i<ECHOMAX;i++)
{
//把当前队列中所有未发送的数据全部复制一份到临时队列中
if (CurDispatch_SendInfo[i].SerialNo!=0)
{
tmpSendInfo[j].BeginEnd=CurDispatch_SendInfo[i].BeginEnd;
tmpSendInfo[j].ByteLength=CurDispatch_SendInfo[i].ByteLength;
memcpy(tmpSendInfo[j].SerialNum,CurDispatch_SendInfo[i].SerialNum,sizeof(CurDispatch_SendInfo[i].SerialNum));
tmpSendInfo[j].AppType=CurDispatch_SendInfo[i].AppType;
tmpSendInfo[j].OperationNo=CurDispatch_SendInfo[i].OperationNo;
tmpSendInfo[j].SerialNo=CurDispatch_SendInfo[i].SerialNo;
tmpSendInfo[j].DataType=CurDispatch_SendInfo[i].DataType;
tmpSendInfo[j].DataLength=CurDispatch_SendInfo[i].DataLength;
memcpy(tmpSendInfo[j].DataIP,CurDispatch_SendInfo[i].DataIP,sizeof(CurDispatch_SendInfo[i].DataIP));
tmpSendInfo[j].DataPort=CurDispatch_SendInfo[i].DataPort;
tmpSendInfo[j].ContentType=CurDispatch_SendInfo[i].ContentType;
tmpSendInfo[j].CheckFlag=CurDispatch_SendInfo[i].CheckFlag;
memcpy(tmpSendInfo[j].DataContent,CurDispatch_SendInfo[i].DataContent,sizeof(CurDispatch_SendInfo[i].DataContent));
j++;
}
}
//清空当前队列
memset(&CurDispatch_SendInfo,0,sizeof(CurDispatch_SendInfo));
//生成新的流水号
staticSendSeriNO=staticSendSeriNO+1;
SendInfo.SerialNo=staticSendSeriNO;
OldSend=0;
for (i=0;i<j;i++)
{
//把临时队列中的数据重新拷贝回到当前队列中
CurDispatch_SendInfo[i].BeginEnd=tmpSendInfo[i].BeginEnd;
CurDispatch_SendInfo[i].ByteLength=tmpSendInfo[i].ByteLength;
memcpy(CurDispatch_SendInfo[i].SerialNum,tmpSendInfo[i].SerialNum,sizeof(tmpSendInfo[i].SerialNum));
CurDispatch_SendInfo[i].AppType=tmpSendInfo[i].AppType;
CurDispatch_SendInfo[i].OperationNo=tmpSendInfo[i].OperationNo;
CurDispatch_SendInfo[i].SerialNo=tmpSendInfo[i].SerialNo;
CurDispatch_SendInfo[i].DataType=tmpSendInfo[i].DataType;
CurDispatch_SendInfo[i].DataLength=tmpSendInfo[i].DataLength;
memcpy(CurDispatch_SendInfo[i].DataIP,tmpSendInfo[i].DataIP,sizeof(tmpSendInfo[i].DataIP));
CurDispatch_SendInfo[i].DataPort=tmpSendInfo[i].DataPort;
CurDispatch_SendInfo[i].ContentType=tmpSendInfo[i].ContentType;
CurDispatch_SendInfo[i].CheckFlag=tmpSendInfo[i].CheckFlag;
memcpy(CurDispatch_SendInfo[i].DataContent,tmpSendInfo[i].DataContent,sizeof(tmpSendInfo[i].DataContent));
//要发送的数据与之前的数据一模一样
if ((SendInfo.BeginEnd==tmpSendInfo[i].BeginEnd) && (SendInfo.ByteLength==tmpSendInfo[i].ByteLength) &&
(SendInfo.AppType==tmpSendInfo[i].AppType) && (SendInfo.OperationNo==tmpSendInfo[i].OperationNo) &&
(SendInfo.DataType==tmpSendInfo[i].DataType) && (SendInfo.DataLength==tmpSendInfo[i].DataLength) &&
(SendInfo.DataPort==tmpSendInfo[i].DataPort) && (SendInfo.ContentType==tmpSendInfo[i].ContentType))
{
OldSend=1; //标记为次数据已发送过
staticSendSeriNO=staticSendSeriNO-1; //由于数据已经存在缓冲区内,流水号减一
for(k=0;k<12;k++)
{
if (SendInfo.SerialNum[k]!=tmpSendInfo[i].SerialNum[k])
{
OldSend=0;
break;
}
}
for(k=0;k<15;k++)
{
if ((SendInfo.DataIP[k]!=tmpSendInfo[i].DataIP[k]) || (OldSend==0))
{
OldSend=0;
break;
}
}
for(k=0;k<ECHOMAX;k++)
{
if ((SendInfo.DataContent[k]!=tmpSendInfo[i].DataContent[k]) || (OldSend==0))
{
OldSend=0;
break;
}
}
if (OldSend==0)
{
staticSendSeriNO=staticSendSeriNO+1; //最终判断数据未发送,再恢复加一
}
}
}
if (((unsigned short)(SendInfo.OperationNo & 0xFF)==200))
{
if (OldSend!=1) //如果没发送过,把发送缓冲中的首个数据设置为注册数据
{
CurDispatch_SendInfo[0].BeginEnd=SendInfo.BeginEnd;
CurDispatch_SendInfo[0].ByteLength=SendInfo.ByteLength;
memcpy(CurDispatch_SendInfo[0].SerialNum,SendInfo.SerialNum,sizeof(SendInfo.SerialNum));
CurDispatch_SendInfo[0].AppType=SendInfo.AppType;
CurDispatch_SendInfo[0].OperationNo=SendInfo.OperationNo;
CurDispatch_SendInfo[0].SerialNo=SendInfo.SerialNo;
CurDispatch_SendInfo[0].DataType=SendInfo.DataType;
CurDispatch_SendInfo[0].DataLength=SendInfo.DataLength;
memcpy(CurDispatch_SendInfo[0].DataIP,SendInfo.DataIP,sizeof(SendInfo.DataIP));
CurDispatch_SendInfo[0].DataPort=SendInfo.DataPort;
CurDispatch_SendInfo[0].ContentType=SendInfo.ContentType;
CurDispatch_SendInfo[0].CheckFlag=SendInfo.CheckFlag;
memcpy(CurDispatch_SendInfo[0].DataContent,SendInfo.DataContent,sizeof(SendInfo.DataContent));
}
}
else if (OldSend!=1) //如果没发送过,把当前数据加入到队列中
{
if ((SendInfo.SerialNo!=0) && (j<ECHOMAX))
{
CurDispatch_SendInfo[j].BeginEnd=SendInfo.BeginEnd;
CurDispatch_SendInfo[j].ByteLength=SendInfo.ByteLength;
memcpy(CurDispatch_SendInfo[j].SerialNum,SendInfo.SerialNum,sizeof(SendInfo.SerialNum));
CurDispatch_SendInfo[j].AppType=SendInfo.AppType;
CurDispatch_SendInfo[j].OperationNo=SendInfo.OperationNo;
CurDispatch_SendInfo[j].SerialNo=SendInfo.SerialNo;
CurDispatch_SendInfo[j].DataType=SendInfo.DataType;
CurDispatch_SendInfo[j].DataLength=SendInfo.DataLength;
memcpy(CurDispatch_SendInfo[j].DataIP,SendInfo.DataIP,sizeof(SendInfo.DataIP));
CurDispatch_SendInfo[j].DataPort=SendInfo.DataPort;
CurDispatch_SendInfo[j].ContentType=SendInfo.ContentType;
CurDispatch_SendInfo[j].CheckFlag=SendInfo.CheckFlag;
memcpy(CurDispatch_SendInfo[j].DataContent,SendInfo.DataContent,sizeof(SendInfo.DataContent));
}
}
}
else if (SendType==2) //从内存中删除服务器已收到的数据
{
for (i=0;i<ECHOMAX;i++)
{
if (CurDispatch_SendInfo[i].SerialNo==SendInfo.SerialNo)
{ //从内存中删除已发送成功的数据时,根据数据类型作相应处理
switch (SendInfo.OperationNo)
{
case 63 : //收到GPS信息应答后,把文件中已发送的GPS记录标记为已发送
printf("Receive GPS back Info & GpsSendOK!\n");
if(!(CurDispatch_SendInfo[i].DataContent[19]&0x18))//||(CurDispatch_SendInfo[i].DataContent[18]))) //为主动上发信息
{
// printf("");
GpsSendOK();
}
break;
case 64: //收到APC信息的应答,把文件中已发送的APC记录标记为已发送
printf("Receive APC back Info & APCSendOK!\n");
if(!(CurDispatch_SendInfo[i].DataContent[25])) //为主动上发信息
{
APCSendOK();
}
break;
case 65: //收到car state信息的应答,把文件中已发送的CR记录标记为已发送
printf("Receive carstate back Info & carstate SendOK!\n");
if(!(CurDispatch_SendInfo[i].DataContent[0])) //为主动上发信息
{
CRSendOK();
}
break;
default :
break;
}
memset(&CurDispatch_SendInfo[i],0,sizeof(CurDispatch_SendInfo[i]));
break;
}
}
}
return;
}
//信息处理(有拆包的功能)
void OnRecievedData(char *respString,int respStringLen,struct sockaddr_in echoClntAddr,int fd)
{
char SecondCmdByte[respStringLen];
Package_Info recvPackage;
int respStringLens,DeESCStringLens;
char echoBuffer[ECHOMAX];
int i,BeginEnd;
recvPackage.BeginEnd=HEAD_GPS_PACKAGE;
respStringLens=respStringLen;
//for (i=0;i<respStringLens;i++)
//{
// printf(" echoBuffer:%d",i);
// printf(" echoBuffer:%02x\r\n",echoBuffer[i] & 0xFF);
//}
BeginEnd=0;
for (i=0;i<respStringLens;i++)
{
if (respString[i]==recvPackage.BeginEnd)
{
if (respString[i+1]==recvPackage.BeginEnd)
{
//说明该信息包为空、是两个包的连接处或者是数据包被拆分了
if (BeginEnd>0)
{
memset(echoBuffer,0,ECHOMAX);
DeESCStringLens=DeESC(SecondCmdByte,echoBuffer,BeginEnd);
ProcessCmdList(echoBuffer,echoClntAddr,DeESCStringLens,fd);
}
else
{
BeginEnd=0;
memset(SecondCmdByte,0,respStringLen);
}
}
else if (respString[i+1]!=recvPackage.BeginEnd)
{
if (BeginEnd>0)
{
memset(echoBuffer,0,ECHOMAX);
DeESCStringLens=DeESC(SecondCmdByte,echoBuffer,BeginEnd);
ProcessCmdList(echoBuffer,echoClntAddr,DeESCStringLens,fd);
}
else
{
BeginEnd=0;
memset(SecondCmdByte,0,respStringLen);
}
}
}
else
{
SecondCmdByte[BeginEnd]=respString[i];
BeginEnd++;
}
}
}
//将内存中的指令进行处理(一条完整的消息信息)
void ProcessCmdList(byte *bReceiveBuffer,struct sockaddr_in echoClntAddr,int bReceiveLength,int fd)
{
unsigned short IdNum,AppType,OperNum,SerialNo,FlagByte,DispatchID,
DataLength,DataType,IP1,IP2,IP3,IP4,Port,DataCount;
unsigned int id1,id2,ReapFlag;
char *DeviceNo,strid1[9],strid2[5],*ServerIP,strIP1[4],strIP2[4],strIP3[4],strIP4[4],shorttime[5];
char *Timer1,*Timer2,tmpbuf[128];
Dispatch_Info LocalDispatch_Info; //Point the share mem
Package_Info commInter;
GPS_Txt_Info DispatchGPS[1];
GPS_Menu_Info GPSMenuInfo[ECHOMAX];
time_t now;
struct tm *locald;
int i,j;
DeviceNo=malloc(15);
ServerIP=malloc(16);
Timer1=malloc(4); //发送间隔从0~255最多有三位
Timer2=malloc(4);
ReapFlag=0;
IdNum=(bReceiveBuffer[0] & 0xFF);
id1 =BytesToUInt( bReceiveBuffer,1);
id2=BytesToUshort(bReceiveBuffer ,5);
NumtoStr(id1,strid1,8);
NumtoStr(id2,strid2,4);
strid1[8]='\0';
strid2[4]='\0';
//获取数据包的信息
memset(DeviceNo,0,15);
AppType=(bReceiveBuffer[IdNum+1] & 0xFF);
OperNum =(bReceiveBuffer[IdNum+2] & 0xFF);
SerialNo =BytesToUshort(bReceiveBuffer,IdNum+3);
FlagByte=(bReceiveBuffer[IdNum+5] & 0xFF);
DataLength=BytesToUshort(bReceiveBuffer,IdNum+6);
if ((FlagByte & 0x01)==1)
{
//说明是回复信息.
printf("back info!\r\n");
Waitresp = 0;
//修改共享内存,收到回复信息说明网络服务通信成功
IPC_ProcState_Read(PROCID_DISPATCH,CurDispatch_Info,&LocalDispatch_Info);
LocalDispatch_Info.CommState=1;
LocalDispatch_Info.Proc_Info.PID = getpid();
time(&(LocalDispatch_Info.Proc_Info.ctime));
//Share mem write
IPC_ProcState_Write(PROCID_DISPATCH,CurDispatch_Info,&LocalDispatch_Info);
IPC_ProcState_Read(PROCID_DISPATCH,CurDispatch_Info,&LocalDispatch_Info);
if (OperNum==200)
{
memset(&commInter,0,sizeof(commInter));
commInter.SerialNo=SerialNo;
Deal_SendInfo(commInter,2);
}
else if (OperNum==61)
{
//确认信息
DispatchID=0;
memset(&commInter,0,sizeof(commInter));
for (i=0;i<ECHOMAX;i++)
{
if (CurDispatch_SendInfo[i].SerialNo==SerialNo)
{
DispatchID=BytesToUshort(CurDispatch_SendInfo[i].DataContent,0);
break;
}
}
memset(DispatchGPS,0,sizeof(DispatchGPS));
DISPATCH_TxtFile(Dispatch_SureFile,DispatchGPS,0,"w",DispatchID);
commInter.SerialNo=SerialNo;
Deal_SendInfo(commInter,2);
}
else if ((OperNum==63)||(OperNum==64)||(OperNum==65)) //发送的GPS/APC/CR信息收到
{
commInter.SerialNo=SerialNo;
commInter.OperationNo=OperNum;
Deal_SendInfo(commInter,2);
}
return;
}
else //以下部分为收到来自服务器的指令
{
if (CheckData(bReceiveBuffer,bReceiveLength)==TRUE)
{
printf("receive from server,OperNum is %d!\n",OperNum);
if (OperNum==11)
{
//收到调度信息
DataType=(bReceiveBuffer[IdNum+8] & 0xFF);
memset(DispatchGPS,0,sizeof(DispatchGPS));
//memset(strid2,0,sizeof(strid2));
//DataCount=NumtoStrL(1,strid2);
//1:不需要确认的信息;2:需要确认的信息
memset(strid2,0,sizeof(strid2));
DataCount=NumtoStrL(DataType,strid2);
memcpy(DispatchGPS[0].infotype,strid2,DataCount);
//memcpy(DispatchGPS[0].infotype,strid2,DataCount);
memset(strid2,0,sizeof(strid2));
DataCount=NumtoStrL(SerialNo,strid2);
memcpy(DispatchGPS[0].infoid,strid2,DataCount);
now=time(NULL);
locald=localtime(&now);
NumtoStr(locald->tm_hour,strid2,2);
shorttime[0]=strid2[0];
shorttime[1]=strid2[1];
shorttime[2]=':';
NumtoStr(locald->tm_min,strid2,2);
shorttime[3]=strid2[0];
shorttime[4]=strid2[1];
memcpy(DispatchGPS[0].shorttime,shorttime,sizeof(shorttime));
for (i=0;i<DataLength-1;i++)
{
DispatchGPS[0].content[i]=bReceiveBuffer[IdNum+9+i];
}
//数据内容不能超过数据长度
if (bReceiveLength>IdNum+8+i)
{
DISPATCH_TxtFile(Dispatch_SureFile,DispatchGPS,1,"w",0);
memset(tmpbuf,0,sizeof(tmpbuf));
tmpbuf[0]=0x00;
tmpbuf[1]=0x06;
MSGQ_Send(PROCID_KEYB,tmpbuf,2);
}
else
{
ReapFlag=1;
}
}
else if (OperNum==31)
{
//设置通信服务器的IP和Port
IP1=(bReceiveBuffer[IdNum+8] & 0xFF);
IP2=(bReceiveBuffer[IdNum+9] & 0xFF);
IP3=(bReceiveBuffer[IdNum+10] & 0xFF);
IP4=(bReceiveBuffer[IdNum+11] & 0xFF);
NumtoStr(IP1,strIP1,3);
NumtoStr(IP2,strIP2,3);
NumtoStr(IP3,strIP3,3);
NumtoStr(IP4,strIP4,3);
strIP1[3]='\0';
strIP2[3]='\0';
strIP3[3]='\0';
strIP4[3]='\0';
memset(ServerIP,0,16);
strcat(ServerIP,strIP1);
strcat(ServerIP,".");
strcat(ServerIP,strIP2);
strcat(ServerIP,".");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -