⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 busdispatch.c

📁 基于嵌入式linux下
💻 C
📖 第 1 页 / 共 3 页
字号:
			if (DISPATCH_Exec(PPP_OffFile)==0)
			{
				//printf("ppp-off success!\r\n");
			}
			
			sleep(4);
		}
		else
		{
			sleep(2);
		}
		
		//修改共享内存
		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);
	}
	
	while(1)		//main loop begins here!
	{
		//拨号处理
		pppCount=0;
		while((PPP_Status()==0) && (pppCount<3))
		{		
			if (GPRS_Reset()==0)
			{
				//调用外部可执行文件(进行拨号)
				MB_DEB("Begin ppp-init!");
				if (DISPATCH_Exec(PPP_InitOnFile)==0)
				{
					MB_DEB("ppp-init On success!");
				}
			}
			else
			{
				MB_DEB("ShutDown Modem!");
				GPRS_ShutDown();
			}
			
			//修改共享内存
			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);
			
			sleep(10);
			
			//修改共享内存
			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);
			
			pppCount++;
		}
		
		
		if (PPP_Status())
		{
//			printf("Begin socket init!,RegTimes is %d\n",RegTimes);
			if(RegTimes>3)
			{
				RegTimes = 0;
				break;
			}
			else
				RegTimes++;
			sockFD= socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
	
			if (sockFD<0)
			{
				printf("socket fail!\r\n");
				return;
			}
			
			/* Construct the server address structure */
		    memset(&echoServAddr, 0, sizeof(echoServAddr));    /* Zero out structure */
		    
		    echoServAddr.sin_family = AF_INET;                 /* Internet addr family */
		    echoServAddr.sin_addr.s_addr = inet_addr(SocketServer);  /* Server IP address */
		    echoServAddr.sin_port   = htons(SocketPort);     /* Server port */
			
			memset(&commInter,0,sizeof(Package_Info));
			//形成注册信息包	
		    commInter.BeginEnd=HEAD_GPS_PACKAGE;
		    commInter.ByteLength=0x06;
		    
		    memcpy(commInter.SerialNum,GPSID_Send,12);
		    commInter.AppType=0x16;
		    commInter.OperationNo=0xC8;
		    commInter.SerialNo=staticSendSeriNO;
		    commInter.DataType=0x02;
		    commInter.DataLength=0x06;
		    memcpy(commInter.DataIP,Dispatch_DataIP,16);

		    commInter.DataPort=8000;
		    
		    commInter.ContentType='\0';
		    memset(commInter.DataContent,0,sizeof(commInter.DataContent));

//			printf("Regester Data Dispatch_DataIP:%s\r\n",Dispatch_DataIP);
//		    printf("Regester Data SocketServer!%s\r\n",SocketServer);
//			printf("Regester Data SocketPort!%d\r\n",SocketPort);
			
			MB_DEB("Begin regist!");

		    RegesterInfo(sockFD,commInter,echoServAddr);	    
			MB_DEB("Send out regist info!");
		    selectRet = Select_Read(sockFD,WaitTime_M,echoServAddr);	//检测是否收到注册的应答?
		    IPC_ProcState_Read(PROCID_DISPATCH,CurDispatch_Info,&LocalDispatch_Info);
		    
		
			//只有网络时连通的才进行业务处理
			//while (LocalDispatch_Info.CommState==1)
			while ((PPP_Status()) && (LocalDispatch_Info.CommState==1))
			{	
				MB_DEB("Regist success!");
				RegTimes = 0;
				//开始进入网络连通后的处理
				while(Get_UnSendInfo(&commInter))		//如果内存中还有未发送数据,则全部发送
				{
					//从内存中发送之前未送出的数据
//					printf("Send data from ram!\n");
					SocketReSendInfo(sockFD,commInter,echoServAddr);
					selectRet = Select_Read(sockFD,WaitTime_M,echoServAddr);
					if(selectRet == DISPATCH_READ_TIMEOUT)
					{
						break;
					}
				}
				selectRet = Select_Read(sockFD,WaitTime_M,echoServAddr);
				while(GetFileAPCInfo((char *)tmpAPC))		//从APC文件中读出数据
				{
					char tmp[10];
					memset(tmp,0,sizeof(tmp));
					memcpy(tmp,tmpAPC+25,sizeof(tmp));
					tmpAPC[25] = 0x0;		//数据标志,表示为主动上发的数据
					memcpy(tmpAPC+26,tmp,sizeof(tmp));
					printf("Send data from APC file!\n");

					memcpy(commInter.DataContent,tmpAPC,sizeof(tmpAPC));
					//形成APC数据的包头信息
					commInter.BeginEnd=HEAD_GPS_PACKAGE;
					commInter.ByteLength=0x06;
					memcpy(commInter.SerialNum,GPSID_Send,12);
					commInter.AppType=0x16;
					commInter.OperationNo=0x40;		//客流量计数据的操作码64
					commInter.SerialNo=staticSendSeriNO;
					commInter.DataType=0x02;
					commInter.DataLength=0x24;		//客流量计数据的长度
					memcpy(commInter.DataContent,tmpAPC,sizeof(tmpAPC));
					//memcpy(commInter.DataContent,tmpGps,sizeof(tmpGps));

					SocketSendInfo(sockFD,commInter,echoServAddr);				    
					selectRet = Select_Read(sockFD,WaitTime_M,echoServAddr);

					if(selectRet == DISPATCH_READ_TIMEOUT)
					{
						break;
					}
				}
				while(GetFileCRInfo((char *)(tmpCR+1)))			//从carstate文件中读出数据,从第二Byte写入,首byte用于存放数据标志
				{
					printf("Send data from catstate file!\n");
					tmpCR[0] = 0;			//数据上发的标志,表示为主动上发的数据
					memcpy(commInter.DataContent,tmpCR,sizeof(tmpCR));
					//形成CR数据的包头信息
					commInter.BeginEnd=HEAD_GPS_PACKAGE;
					commInter.ByteLength=0x06;
					memcpy(commInter.SerialNum,GPSID_Send,12);
					commInter.AppType=0x16;
					commInter.OperationNo=0x41;		//carstate数据的操作码65
					commInter.SerialNo=staticSendSeriNO;
					commInter.DataType=0x02;
					commInter.DataLength=sizeof(tmpCR);
					memcpy(commInter.DataContent,tmpCR,sizeof(tmpCR));
					//memcpy(commInter.DataContent,tmpGps,sizeof(tmpGps));

					SocketSendInfo(sockFD,commInter,echoServAddr);				    
					selectRet = Select_Read(sockFD,WaitTime_M,echoServAddr);

					if(selectRet == DISPATCH_READ_TIMEOUT)
					{
						break;
					}
				}
				now = time(NULL);
				//select 以超时方式结束后,肯定有GPS数据可以发,此时检查GPS文件
				if ((now - Begin)>=WaitTime_M)// || (difftime(time(NULL),Begin)<=-WaitTime_M))
				{
					Begin=time(NULL);
					MB_DEB("Time to send out!");
					memset(&commInter,0,sizeof(Package_Info));
					memset(tmpGps,0,sizeof(tmpGps));

				    while(GetFileGpsInfo((char *)tmpGps))		//能够从本地的GPS文件中读出GPS信息时
					{
						MB_DEB("Send data from GPS file!");
						memcpy(commInter.DataContent,tmpGps,sizeof(tmpGps));
	//				    printf("Didpatch data Dispatch_DataIP:%s\r\n",Dispatch_DataIP);
	//		    		MB_DEB("Didpatch data SocketServer!%s",SocketServer);
	//					printf("Didpatch data SocketPort!%d\r\n",SocketPort);
//						printf("Send up data every 8 seconds!\r\n");

						//形成GPS数据的包头信息
						commInter.BeginEnd=HEAD_GPS_PACKAGE;
						commInter.ByteLength=0x06;
						memcpy(commInter.SerialNum,GPSID_Send,12);
						commInter.AppType=0x16;
						commInter.OperationNo=0x3F;
						commInter.SerialNo=staticSendSeriNO;
						commInter.DataType=0x02;
						commInter.DataLength=0x19;
						if (CurCarState>0)
						{
							tmpGps[16]=CurCarState / 256;
							tmpGps[17]=0xff & CurCarState;
						}
						if (CurCarState>0)
						{
							tmpGps[19] = tmpGps[19] | 0x04;
						}
						
						memcpy(commInter.DataContent,tmpGps,sizeof(tmpGps));						
						SocketSendInfo(sockFD,commInter,echoServAddr);				    
						selectRet = Select_Read(sockFD,WaitTime_M,echoServAddr);						
	
						if(selectRet == DISPATCH_READ_TIMEOUT)
						{
							break;
						}
					}
				}
				//处理socket读取结果
//				printf("socket return handle!\n");
				switch(selectRet)
				{
					case DISPATCH_READ_TIMEOUT:
						//修改共享内存
						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);
						printf("LocalDispatch_Info.CommState\%d\r\n",0);
						break;
					case DISPATCH_READ_UNERROR:
						break;
					case DISPATCH_READ_SUCCESS:		//Read success 				
						break;
					case EINTR:		//MsgQueue handle
						break;
					default:
						break;
				}
				
				if(ReceiveMsgQ)
				{
					printf("receive msg!\r\n");
					ReceiveMsgQ = 0;
					while(1)
					{
						memset(tmpbuf,0,sizeof(tmpbuf));
						if(MSGQ_Read(PROCID_DISPATCH,tmpbuf)>0)
							Dispatch_MsgHandle(tmpbuf,echoServAddr,sockFD);
						else
							break;
					}
				}
				
				//修改共享内存
				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);
			}
			
			close(sockFD);
	
			//修改共享内存
			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);
			
			if(ReceiveMsgQ)
			{
				printf("receive msg!\r\n");
				ReceiveMsgQ = 0;
				while(1)
				{
					memset(tmpbuf,0,sizeof(tmpbuf));
					if(MSGQ_Read(PROCID_DISPATCH,tmpbuf)>0)
						Dispatch_MsgHandle(tmpbuf,echoServAddr,sockFD);
					else
						break;
				}
			}
		}
	} 
	
	if (PPP_Status())
	{
		if (DISPATCH_Exec(PPP_OffFile)==0)
		{
			//printf("ppp-off success!\r\n");
		}
	}
	
	free(Dispatch_DataIP);
	free(GPSID_Send);
	free(ServerPort);
	free(SocketServer);
		
	return;	
}

//
int Select_Read(int fd,int WaitTime,struct sockaddr_in echoClntAddr)
{
	int maxfd;
	int	retr,ncount;
	fd_set readfds,writefds,exceptfds;
	struct timeval tvptr;
	char	buf[ECHOMAX];
	

	//char IP[15];
	
	//memcpy(IP,inet_ntoa(echoClntAddr.sin_addr),15);
    //printf("IP Address2:%s\r\n",IP);

	tvptr.tv_sec = WaitTime;
	tvptr.tv_usec = 0;
	FD_ZERO(&readfds);
	FD_ZERO(&exceptfds);
	FD_SET(fd,&readfds);
	FD_SET(fd,&exceptfds);
	
	retr = select(fd+1,&readfds,NULL,&exceptfds,&tvptr);
	if(retr==-1)
	{
		if (errno==EINTR)			//select is interrupted by signal
		{
			printf("sig int!\r\n");
			return EINTR;
		}
		return DISPATCH_READ_UNERROR;
	}
	else if(retr==0)
	{//select over time,no DISPATCH data in
//		printf("Select time out,DISPATCH !\r\n");
		if(Waitresp)
			printf("Send data time out!\r\n");
		return DISPATCH_READ_TIMEOUT;
	}
	else
	{
		if (FD_ISSET(fd,&readfds))
		{
			MB_DEB("receive data");
			{
				memset(buf,0,sizeof(buf));
				ncount=read(fd,buf,sizeof(buf));								
				if(ncount>0)
				{
					OnRecievedData(buf,ncount,echoClntAddr,fd);
					return DISPATCH_READ_SUCCESS;		//read success
				}
			}
		}
		if (FD_ISSET(fd,&exceptfds))
		{
			printf("error DISPATCH com!\r\n");
			return DISPATCH_READ_UNERROR;
		}
	}
}
//
void GPSFileInit(void)
{
	int num,retr;
	int i;
	char tmpbuf[52];
	int Oldest;

	Offset_UnSendGPS = 0;
	Offset_SendGPS = 0;
	Oldest = 0;
	GPS_FileSize = GetFileSize(GPSFile);
	if((GPS_fd=open(GPSFile,O_RDWR|O_CREAT))<0)
		return ;
	num = GPS_FileSize / GPS_RECORD_LEN;
	MB_DEB("%s: num is %d!",__FUNCTION__,num);
	if(num < RECORDMAX)
	{
		Offset_SendGPS = num;
		for(i=0;i<num;i++)
		{
			read(GPS_fd,tmpbuf,GPS_RECORD_LEN);
			if(tmpbuf[0]==0)
			{
				Offset_UnSendGPS = i;
				break;
			}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -