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

📄 application.c

📁 AVR单片机基础上的以太网协议编程
💻 C
📖 第 1 页 / 共 2 页
字号:
	Som->Active_Message_Time.Date=0;
	Som->Active_Message_Time.Millisecond=MIB_Mrt->Max_Response_Time;
	Som->Message_Length=sizeof(struct EM_SetDeviceAttribute);
	Som->Max_Retrasmit_Number=Max_Resend;
	
	//使用EPA套接字映射接口对报文进行打包,并作相关处理
	Socket_Enpacket();
}

/**************************************************************************
**
**    函数: void Send_EM_SetDeviceAttribute_Result_Positive(void)
**    描述: 该函数对上位机发的设置设备属性报文作正响应            
**
**************************************************************************/
void Send_EM_SetDeviceAttribute_Result_Positive(void)
{  
    memcpy(S_SME_Sdp->Destination_IP_Address,Dest_IP,4);
	if(R_SME_Sda->Device_Redundancy_Number!=0)
	    S_SME_Sdp->Max_Redundancy_Number=R_SME_Sda->Max_Redundancy_Number;
	
	//将数据传送给EPA套接字映射接口对象
	memcpy(Som->Local_IP_Address,My_IP,4);
	memcpy(Som->Remote_IP_Address,Dest_IP,4);
	Som->Active_UDP_Port=EPA_Port;
	Som->Active_ServiceID=EM_SetDeviceAttribute_ID;
	Som->Active_MessageID=R_She->MessageID;
	Som->Active_Message_Time.Date=0;
	Som->Active_Message_Time.Millisecond=0;
	Som->Message_Length=sizeof(struct EM_SetDeviceAttribute_Result_Positive);
		
	//使用EPA套接字映射接口对报文进行打包,并作相关处理
	Socket_Enpacket();
}

/**************************************************************************
**
**    函数: void Send_DomainDownload(void)
**    描述: 该函数通过域下载服务向下位机发组态信息,下位机不会调用            
**
**************************************************************************/
void Send_DomainDownload(void)
{
    struct Configuration *S_Con=(struct Configuration *)(S_AAE_Ddl->Load_Data);
	DomainDownload_MessageID++;   //域下载报文标识加1
	
	S_AAE_Ddl->Source_AppID=htons(AAE_AppID);
	S_AAE_Ddl->Destination_AppID=htons(MIB_AppID);
	S_AAE_Ddl->Destination_ObjectID=htons(Domain_Application_Information_ObjectID);
	S_AAE_Ddl->Data_Number=htons(0);
	S_AAE_Ddl->More_Follows=FALSE;
	S_AAE_Ddl->Data_Length=htons(sizeof(struct Configuration));
	S_Con->ServiceID=Configuration_ID;
	S_Con->Mrt_Response_Time=htonl((ulong)Response_Time);//变字节序,将一个32位数从主机字节顺序转换成网络字节顺序
	S_Con->Csm_Macro_Cycle=htonl((ulong)Macro_Cycle);
	S_Con->Csm_NonPeriodic_Offset=htonl((ulong)NonPeriodic_Offset);
	S_Con->Lo_Sendtime_Offset=htonl((ulong)Sendtime_Offset);
	
	//将数据传送给EPA套接字映射接口对象
	memcpy(Som->Local_IP_Address,My_IP,4);
	memcpy(Som->Remote_IP_Address,Dest_IP,4);
	Som->Active_UDP_Port=EPA_Port;
	Som->Active_ServiceID=DomainDownload_ID;
	Som->Active_MessageID=DomainDownload_MessageID;
	Som->Active_Message_Time.Date=0;
	Som->Active_Message_Time.Millisecond=MIB_Mrt->Max_Response_Time;
	Som->Message_Length=sizeof(struct DomainDownload);
	Som->Max_Retrasmit_Number=Max_Resend;
	
	//使用EPA套接字映射接口对报文进行打包,并作相关处理
	Socket_Enpacket();			
}

/**************************************************************************
**
**    函数: void Send_DomainDownload_Result_Positive(void)
**    描述: 该函数对上位机发的域下载报文作正响应            
**
**************************************************************************/
void Send_DomainDownload_Result_Positive(void)
{
    S_AAE_Ddp->Destination_AppID=htons(AAE_AppID);
		
    //将数据传送给EPA套接字映射接口对象
	memcpy(Som->Local_IP_Address,My_IP,4);
	memcpy(Som->Remote_IP_Address,Dest_IP,4);
	Som->Active_UDP_Port=EPA_Port;
	Som->Active_ServiceID=DomainDownload_ID;
	Som->Active_MessageID=R_She->MessageID;
	Som->Active_Message_Time.Date=0;
	Som->Active_Message_Time.Millisecond=0;
	Som->Message_Length=sizeof(struct DomainDownload_Result_Positive);
		
	//使用EPA套接字映射接口对报文进行打包,并作相关处理
	Socket_Enpacket();
}

/**************************************************************************
**
**    函数: void Send_Distribute(uchar *,uchar)
**    描述: 该函数从串口接收数据,完成信息分发报文的发送
            入口参数为从串口接收到的数据地址和长度          
**
**************************************************************************/
void Send_Distribute(uchar *dat,uchar datlen)
{ 
    Distribute_MessageID++; //信息分发报文标识加1
	
    S_AAE_Dis->Source_AppID=htons(AAE_AppID);
    S_AAE_Dis->Source_ObjectID=htons(Distribute_ObjectID);
    S_AAE_Dis->Data[0]=datlen;
    memcpy(S_AAE_Dis->Data+1,dat,datlen);
	
    //将数据传送给EPA套接字映射接口对象
	memcpy(Som->Local_IP_Address,My_IP,4);
	memcpy(Som->Remote_IP_Address,MIB_Lo->Remote_IP_Address,4);
	Som->Active_UDP_Port=EPA_Port;
	Som->Active_ServiceID=Distribute_ID;
	Som->Active_MessageID=Distribute_MessageID;
	Som->Active_Message_Time.Date=0;
	Som->Active_Message_Time.Millisecond=MIB_Mrt->Max_Response_Time;
	Som->Message_Length=sizeof(struct Distribute);
	Som->Max_Retrasmit_Number=Max_Resend;
	
	//使用EPA套接字映射接口对报文进行打包,并作相关处理
	Socket_Enpacket();
}

/**************************************************************************
**
**    函数: void Receive_Application_Data(void)
**    描述: 该函数对接收到的应用层服务报文作相应的处理          
**
**************************************************************************/
void Receive_Application_Data(void)
{   
    uchar i;
	
	//收到设备声明请求,发设置设备属性请求或清除设备属性请求
    if(((R_She->ServiceID&0xc0)==Message_REQ)&&((R_She->ServiceID&0x3f)==EM_DeviceAnnunciation_ID))
	{
	    memcpy(Dest_MAC,R_Ehe->Ether_Sour_MAC,6);
		memcpy(Dest_IP,R_Pip->IP_Sour_IP,4);
		for(i=0;i<32;i++)  //判断物理位号是否为空
        if(R_SME_Eda->PD_Tag[i]!=' ')
		break;
		if(i==32)          //物理位号为空,发设置设备属性请求
		Send_EM_SetDeviceAttribute();
		else               //物理位号不为空,发清除设备属性请求
		Send_EM_ClearDeviceAttribute();
	}
	
	//收到设置设备属性请求,填入MIB中的EPA设备描述对象,删除设备声明报文的定时器,并回正响应
	else if(((R_She->ServiceID&0xc0)==Message_REQ)&&((R_She->ServiceID&0x3f)==EM_SetDeviceAttribute_ID))
    {
	   if(memcmp(R_SME_Sda->Destination_IP_Address,My_IP,4)==0\
	      &&memcmp(R_SME_Sda->DeviceID,My_DeviceID,My_DeviceID_Len)==0) //判断是否对本地的设备属性进行设置
       {
	       memcpy(Dest_MAC,R_Ehe->Ether_Sour_MAC,6);
		   memcpy(Dest_IP,R_Pip->IP_Sour_IP,4);
		   MIB_Init_Operable();
		   Delete_DeviceAnnunciation_Timer();
	       Send_EM_SetDeviceAttribute_Result_Positive();
	   }
	}
	
	//收到清除设备属性请求,恢复可操作前的状态,并回正响应
	else if(((R_She->ServiceID&0xc0)==Message_REQ)&&((R_She->ServiceID &0x3f)==EM_ClearDeviceAttribute_ID))
    {
	    if(memcmp(R_SME_Cda->Destination_IP_Address,My_IP,4)==0\
	       &&memcmp(R_SME_Cda->DeviceID,MIB_Edd->DeviceID,32)==0\
           &&memcmp(R_SME_Cda->PD_Tag,MIB_Edd->PD_Tag,32)==0)   //判断是否对本地的设备属性进行清除
		{
		    memcpy(Dest_MAC,R_Ehe->Ether_Sour_MAC,6);
		    memcpy(Dest_IP,R_Pip->IP_Sour_IP,4);
			MIB_Init();
		    Send_EM_ClearDeviceAttribute_Result_Positive();
	    }
	}
    
	//收到设置设备属性的正响应,删除证实报文的定时器,通过域下载服务发组态信息
	else if(((R_She->ServiceID&0xc0)==Message_REP)&&((R_She->ServiceID&0x3f)==EM_SetDeviceAttribute_ID))
	{
	    if(memcmp(R_SME_Sdp->Destination_IP_Address,My_IP,4)==0) //判断是否是正确的正响应
		{
		    Delete_Confirmation_Timer();
			memcpy(Dest_MAC,R_Ehe->Ether_Sour_MAC,6);
		    memcpy(Dest_IP,R_Pip->IP_Sour_IP,4);
		    Send_DomainDownload();
		}
	}
	
	//收到清除设备属性的正响应,删除证实报文的定时器,发设置设备属性请求
	else if(((R_She->ServiceID&0xc0)==Message_REP)&&((R_She->ServiceID &0x3f)==EM_ClearDeviceAttribute_ID))
	{
	    if(memcmp(R_SME_Cdp->Destination_IP_Address,My_IP,4)==0) //判断是否是正确的正响应
		{
		    Delete_Confirmation_Timer();
			memcpy(Dest_MAC,R_Ehe->Ether_Sour_MAC,6);
		    memcpy(Dest_IP,R_Pip->IP_Sour_IP,4);
		    Send_EM_SetDeviceAttribute();
		}
	}
	
	//收到域下载请求,用组态信息更新本地的MIB,置标志位,并回正响应
	else if(((R_She->ServiceID&0xc0)==Message_REQ)&&((R_She->ServiceID &0x3f)==DomainDownload_ID))
	{
	    if(ntohs(R_AAE_Ddl->Data_Number)==0)
		{   
		    DomainDownload_Buf[0]=ntohs(R_AAE_Ddl->Data_Length)>>8;
			DomainDownload_Buf[1]=ntohs(R_AAE_Ddl->Data_Length)&0x00ff;
			memcpy(DomainDownload_Buf+2,R_AAE_Ddl->Load_Data,ntohs(R_AAE_Ddl->Data_Length));
		}
		else
		{
		    DomainDownload_Buf[0]+=ntohs(R_AAE_Ddl->Data_Length)>>8;
			DomainDownload_Buf[1]+=ntohs(R_AAE_Ddl->Data_Length)&0x00ff;
			memcpy(DomainDownload_Buf+2+(DomainDownload_Buf[0]<<8)+DomainDownload_Buf[1]\
			       -ntohs(R_AAE_Ddl->Data_Length),R_AAE_Ddl->Load_Data,ntohs(R_AAE_Ddl->Data_Length));
		}
		if(R_AAE_Ddl->More_Follows=FALSE)   //本次域下载结束
		{			
		    if(R_Con->ServiceID==Configuration_ID) //下载的域是组态信息
			{
			    memcpy(Dest_MAC,R_Ehe->Ether_Sour_MAC,6);
		        memcpy(Dest_IP,R_Pip->IP_Sour_IP,4);
			    MIB_Init_Ready();
		        Ready_Ed=TRUE;		        
			}
		}
		Send_DomainDownload_Result_Positive();
	}
	
	//收到域下载的正响应,删除证实报文的定时器
	else if(((R_She->ServiceID&0xc0)==Message_REP)&&((R_She->ServiceID &0x3f)==DomainDownload_ID))
	{
	    if(ntohs(R_AAE_Ddp->Destination_AppID)==AAE_AppID) //判断是否是正确的正响应
		Delete_Confirmation_Timer();
		//判断是否还有后续数据,若有,则调用域下载服务,继续发送后续数据
	}
	
	//收到信息分发请求,存储消息数据
	else if(((R_She->ServiceID &0xc0) ==Message_REQ) && ((R_She->ServiceID &0x3f) ==Distribute_ID))
	{
	    if(ntohs(R_AAE_Dis->Source_AppID)==AAE_AppID&&ntohs(R_AAE_Dis->Source_ObjectID)==Distribute_ObjectID)
		memcpy(Dis_Data,R_AAE_Dis->Data,Dis_DataLen); //是正确的信息分发请求,存储数据
	}
}

/******************* End Of File **********************/

⌨️ 快捷键说明

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