?? packagedealnew.c
字號(hào):
//修改共享內(nèi)存()
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回復(fù)信息
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");
//修改共享內(nèi)存()
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;
}
//從內(nèi)存中讀出一個(gè)沒有成功發(fā)送的數(shù)據(jù)包
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;
}
//處理發(fā)送隊(duì)列,參數(shù)SendType的值為1時(shí),則把要發(fā)送的數(shù)據(jù)加入到內(nèi)存的隊(duì)列中;
//參數(shù)SendType的值為2時(shí),則要把發(fā)送的數(shù)據(jù)從內(nèi)存隊(duì)列中刪除
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++)
{
//把當(dāng)前隊(duì)列中所有未發(fā)送的數(shù)據(jù)全部復(fù)制一份到臨時(shí)隊(duì)列中
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++;
}
}
//清空當(dāng)前隊(duì)列
memset(&CurDispatch_SendInfo,0,sizeof(CurDispatch_SendInfo));
//生成新的流水號(hào)
staticSendSeriNO=staticSendSeriNO+1;
SendInfo.SerialNo=staticSendSeriNO;
OldSend=0;
for (i=0;i<j;i++)
{
//把臨時(shí)隊(duì)列中的數(shù)據(jù)重新拷貝回到當(dāng)前隊(duì)列中
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));
//要發(fā)送的數(shù)據(jù)與之前的數(shù)據(jù)一模一樣
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; //標(biāo)記為次數(shù)據(jù)已發(fā)送過
staticSendSeriNO=staticSendSeriNO-1; //由于數(shù)據(jù)已經(jīng)存在緩沖區(qū)內(nèi),流水號(hào)減一
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; //最終判斷數(shù)據(jù)未發(fā)送,再恢復(fù)加一
}
}
}
if (((unsigned short)(SendInfo.OperationNo & 0xFF)==200))
{
if (OldSend!=1) //如果沒發(fā)送過,把發(fā)送緩沖中的首個(gè)數(shù)據(jù)設(shè)置為注冊(cè)數(shù)據(jù)
{
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) //如果沒發(fā)送過,把當(dāng)前數(shù)據(jù)加入到隊(duì)列中
{
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) //從內(nèi)存中刪除服務(wù)器已收到的數(shù)據(jù)
{
for (i=0;i<ECHOMAX;i++)
{
if (CurDispatch_SendInfo[i].SerialNo==SendInfo.SerialNo)
{ //從內(nèi)存中刪除已發(fā)送成功的數(shù)據(jù)時(shí),根據(jù)數(shù)據(jù)類型作相應(yīng)處理
switch (SendInfo.OperationNo)
{
case 63 : //收到GPS信息應(yīng)答后,把文件中已發(fā)送的GPS記錄標(biāo)記為已發(fā)送
printf("Receive GPS back Info & GpsSendOK!\n");
if(!(CurDispatch_SendInfo[i].DataContent[19]&0x18))//||(CurDispatch_SendInfo[i].DataContent[18]))) //為主動(dòng)上發(fā)信息
{
// printf("");
GpsSendOK();
}
break;
case 64: //收到APC信息的應(yīng)答,把文件中已發(fā)送的APC記錄標(biāo)記為已發(fā)送
printf("Receive APC back Info & APCSendOK!\n");
if(!(CurDispatch_SendInfo[i].DataContent[25])) //為主動(dòng)上發(fā)信息
{
APCSendOK();
}
break;
case 65: //收到car state信息的應(yīng)答,把文件中已發(fā)送的CR記錄標(biāo)記為已發(fā)送
printf("Receive carstate back Info & carstate SendOK!\n");
if(!(CurDispatch_SendInfo[i].DataContent[0])) //為主動(dòng)上發(fā)信息
{
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)
{
//說明該信息包為空、是兩個(gè)包的連接處或者是數(shù)據(jù)包被拆分了
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++;
}
}
}
//將內(nèi)存中的指令進(jìn)行處理(一條完整的消息信息)
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); //發(fā)送間隔從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';
//獲取數(shù)據(jù)包的信息
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)
{
//說明是回復(fù)信息.
printf("back info!\r\n");
Waitresp = 0;
//修改共享內(nèi)存,收到回復(fù)信息說明網(wǎng)絡(luò)服務(wù)通信成功
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)
{
//確認(rèn)信息
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)) //發(fā)送的GPS/APC/CR信息收到
{
commInter.SerialNo=SerialNo;
commInter.OperationNo=OperNum;
Deal_SendInfo(commInter,2);
}
return;
}
else //以下部分為收到來自服務(wù)器的指令
{
if (CheckData(bReceiveBuffer,bReceiveLength)==TRUE)
{
printf("receive from server,OperNum is %d!\n",OperNum);
if (OperNum==11)
{
//收到調(diào)度信息
DataType=(bReceiveBuffer[IdNum+8] & 0xFF);
memset(DispatchGPS,0,sizeof(DispatchGPS));
//memset(strid2,0,sizeof(strid2));
//DataCount=NumtoStrL(1,strid2);
//1:不需要確認(rèn)的信息;2:需要確認(rèn)的信息
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];
}
//數(shù)據(jù)內(nèi)容不能超過數(shù)據(jù)長度
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)
{
//設(shè)置通信服務(wù)器的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,".");
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -