📄 packagedeal.c
字号:
GpsSendOK();
}
break;
case 64: //收到APC信息的应答
printf("Receive APC back Info & APCSendOK!\n");
if(!(CurDispatch_SendInfo[i].DataContent[25])) //为主动上发信息
{
APCSendOK();
}
break;
case 65: //收到car state信息的应答
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;
//MB_DEB("%s() received data :",__FUNCTION__);
//MbDump(respString,respStringLen);
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;
//for (i=0;i<bReceiveLength;i++)
//{
// printf(" i:%d",i);
// printf(" bReceiveBuffer:%02x\r\n",bReceiveBuffer[i] & 0xFF);
//}
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);
//printf("DeviceNo:%s\n",DeviceNo);
//DISPATCH_TxtFile(Dispatch_SureFile,DispatchGPS,0,"w",DispatchID);
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.CarState=0;
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)
{
/*
//修改共享内存(注册成功)
IPC_ProcState_Read(PROCID_DISPATCH,CurDispatch_Info,&LocalDispatch_Info);
//LocalDispatch_Info.CarState=0;
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);
//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)));
*/
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;
}
//printf("Local hour is: %d\n",locald->tm_hour);
//printf("Local hour is: %d\n",locald->tm_min);
//locald=gmtime(&now);
//printf("UTC hour is: %d\n",locald->tm_hour);
//printf("UTC hour is: %d\n",locald->tm_min);
}
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,".");
strcat(ServerIP,strIP3);
strcat(ServerIP,".");
strcat(ServerIP,strIP4);
Port=BytesToUshort(bReceiveBuffer,IdNum+12);
DataLength=NumtoStrL(Port,strid1);
memset(tmpbuf,0,sizeof(tmpbuf));
memcpy(tmpbuf,strid1,DataLength);
//数据内容不能超过数据长度
if (bReceiveLength>IdNum+12)
{
IniSave("SERVER","SERVERIP",ServerIP);
IniSave("SERVER","SERVERPORT",tmpbuf);
}
else
{
ReapFlag=1;
}
}
else if (OperNum==32)
{
//设置GPS发送间隔
memset(Timer1,0,4);
DataLength=NumtoStrL((bReceiveBuffer[IdNum+8] & 0xFF),Timer1); //车机移动时发送间隔
memset(Timer2,0,4);
DataLength=NumtoStrL((bReceiveBuffer[IdNum+9] & 0xFF),Timer2); //车机静止时发送间隔
//数据内容不能超过数据长度
if (bReceiveLength>IdNum+9)
{
IniSave("SERVER","TIMER_M",Timer1);
IniSave("SERVER","TIMER_S",Timer2);
WaitTime_S = atol(Timer2);
WaitTime_M = atol(Timer1);
printf("New Set WaitTime_S is %d,WaitTime_M is %d",WaitTime_S,WaitTime_M);
}
else
{
ReapFlag=1;
}
}
else if (OperNum==37)
{
//设置内置信息(业务和报警菜单)
DataType=(bReceiveBuffer[IdNum+8] & 0xFF);
DataCount=(bReceiveBuffer[IdNum+9] & 0xFF);
memset(GPSMenuInfo,0,sizeof(GPSMenuInfo));
printf("DataLength is %d,type is %d!\n",DataLength,DataType);
if (DataType==1)
{
//报警菜单信息
for (i=0;i<DataCount;i++)
{
for (j=0;j<33;j++)
{
if (j==0)
{
NumtoStr(bReceiveBuffer[IdNum+10+i*33+j],GPSMenuInfo[i].infotype,3);
}
else
{
GPSMenuInfo[i].content[j-1]=bReceiveBuffer[IdNum+10+i*33+j];
}
}
}
//数据内容不能超过数据长度
if (bReceiveLength>IdNum+9+i*33)
{
DISPATCH_MenuFile(Dispatch_EveMenuFile,GPSMenuInfo,DataCount,"w");
}
else
{
ReapFlag=1;
}
}
else if (DataType==2)
{
//业务数据
for (i=0;i<DataCount;i++)
{
for (j=0;j<33;j++)
{
if (j==0)
{
NumtoStr(bReceiveBuffer[IdNum+10+i*33+j],GPSMenuInfo[i].infotype,3);
}
else
{
GPSMenuInfo[i].content[j-1]=bReceiveBuffer[IdNum+10+i*33+j];
}
}
}
//数据内容不能超过数据长度
if (bReceiveLength>IdNum+9+i*33)
{
DISPATCH_MenuFile(Dispatch_BusMenuFile,GPSMenuInfo,DataCount,"w");
}
else
{
ReapFlag=1;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -