📄 application.c
字号:
/**************************************************************************
**
** 文件: Application.c
** 描述: 该文件完成应用层服务报文的收发
**
**************************************************************************/
//include library files
#include <stdio.h>
#include <string.h>
//include user defined files
#include "Define.h"
#include "Global_Variable.h"
#include "Extern_Function.h"
#include "EPA_ManInfBas.h"
#include "EPA_SysManEnt.h"
#include "EPA_AppAccEnt.h"
#include "IP_UDP.h"
#include "EPA_SocMapEnt.h"
//报文标识,反映服务报文被调用的次数
static uint EM_DeviceAnnunciation_MessageID=0; //设备声明报文标识
static uint EM_ClearDeviceAttribute_MessageID=0; //清除设备属性报文标识
static uint EM_SetDeviceAttribute_MessageID=0; //设置设备属性报文标识
static uint DomainDownload_MessageID=0; //域下载报文标识
static uint Distribute_MessageID=0; //信息分发报文标识
//设备声明的版本号,反映设备属性被修改的次数
static uint Annunciation_Version_Number_ID=0;
//通信宏周期的版本号,反映通信宏周期被组态的次数
static uint CommunicationCycle_Version_Number_ID=0;
//为EPA管理信息库开辟存储区
uchar MIB_Buf1[sizeof(struct EPA_MIB_Header)]; //EPA设备管理信息库的首部对象
uchar MIB_Buf2[sizeof(struct EPA_Device_Descriptor)]; //EPA设备描述对象
uchar MIB_Buf3[sizeof(struct Clock_Synchronization)]; //时间同步对象
uchar MIB_Buf4[sizeof(struct Max_Response_Time)]; //证实服务最大响应时间
uchar MIB_Buf5[sizeof(struct Communication_Schedule_Management)]; //通信调度管理对象
uchar MIB_Buf6[sizeof(struct Device_Application_Information)]; //设备应用信息对象
uchar MIB_Buf7[sizeof(struct FB_Application_Information_Header)]; //功能块应用信息对象首部
uchar MIB_Buf8[sizeof(struct Link_Object_Header)]; //链接对象首部
uchar MIB_Buf9[sizeof(struct Domain_Application_Object_Header)]; //域应用对象首部
uchar MIB_Buf10[sizeof(struct FB_Application_Information)]; //功能块应用信息对象
uchar MIB_Buf11[sizeof(struct Domain_Application_Information)]; //域应用信息对象
uchar MIB_Buf12[sizeof(struct Link_Object)]; //链接对象
/**************************************************************************
**
** 函数: void MIB_Init(void)
** 描述: 该函数完成组态前MIB的初始化
**
**************************************************************************/
void MIB_Init(void)
{
uchar i;
//设置EPA设备描述对象
memcpy(MIB_Edd->DeviceID,My_DeviceID,My_DeviceID_Len);
for(i=My_DeviceID_Len;i<32;i++)
MIB_Edd->DeviceID[i]=' '; //设备ID的不足部分用空格代替
for(i=0;i<32;i++)
MIB_Edd->PD_Tag[i]=' '; //物理位号为空
memcpy(MIB_Edd->Active_IP_Address,My_IP,4); //静态设定好本地设备的IP地址
MIB_Edd->Device_Type=Device_Type_ID; //设备类型
MIB_Edd->Status=Standby; //未组态
MIB_Edd->Device_Version=Device_Version_ID; //设备版本号
MIB_Edd->Annunciation_Interval=15; //默认的设备声明报文广播周期为15s
MIB_Edd->Annunciation_Version_Number=Annunciation_Version_Number_ID; //设备声明的版本号
MIB_Edd->Device_Redundancy_Number=0; //设备冗余号为0
MIB_Edd->Duplicate_Tag_Detected=FALSE; //PD Tag不冲突
//设置通信调度管理对象
MIB_Csm->Communication_Macro_Cycle=0xffffffff; //表示通信宏周期T未组态
MIB_Csm->NonPeriodic_Data_Transfer_Offset=0xffffffff; //表示周期报文发送阶段总时间Tp未被组态
MIB_Csm->CommunicationCycle_Version_Number=CommunicationCycle_Version_Number_ID; //通信宏周期的版本号
//设置链接对象
MIB_Lo->Service_Operation=1; //非0,表示是远程链接,而不是本地链接
MIB_Lo->Service_Role=Role_Transmitter; //表示本地通信角色是发送者
memcpy(MIB_Lo->Remote_IP_Address,Dest_IP,4);//填入远程IP地址
MIB_Lo->Send_Time_Offset=0xffffffff; //表示本地周期报文的发送时间Tdx未被组态
}
/**************************************************************************
**
** 函数: void MIB_Init_Operable(void)
** 描述: 该函数完成设备属性的组态,进入可操作状态
**
**************************************************************************/
void MIB_Init_Operable(void)
{
Annunciation_Version_Number_ID++; //设备声明的版本号加1
//设置EPA设备描述对象
memcpy(MIB_Edd->PD_Tag,R_SME_Sda->PD_Tag,32);
MIB_Edd->Annunciation_Interval=ntohs(R_SME_Sda->Annunciation_Interval);
MIB_Edd->Annunciation_Version_Number=Annunciation_Version_Number_ID;
MIB_Edd->Duplicate_Tag_Detected=R_SME_Sda->Duplicate_Tag_Detected;
if(R_SME_Sda->Device_Redundancy_Number!=0)
{
MIB_Edd->LAN_Redundancy_Port=ntohs(R_SME_Sda->LAN_Redundancy_Port);
MIB_Edd->Device_Redundancy_State=R_SME_Sda->Device_Redundancy_State;
MIB_Edd->Max_Redundancy_Number=R_SME_Sda->Max_Redundancy_Number;
memcpy(MIB_Edd->Active_IP_Address,R_SME_Sda->Active_IP_Address,4);
}
}
/**************************************************************************
**
** 函数: void MIB_Init_Ready(void)
** 描述: 该函数将上位机的组态信息填入MIB,进入已组态状态
**
**************************************************************************/
void MIB_Init_Ready(void)
{
CommunicationCycle_Version_Number_ID++; //通信宏周期的版本号加1
//组态证实服务最大响应时间
MIB_Mrt->Max_Response_Time=ntohl(R_Con->Mrt_Response_Time);
//组态通信调度管理对象
MIB_Csm->Communication_Macro_Cycle=ntohl(R_Con->Csm_Macro_Cycle);
MIB_Csm->NonPeriodic_Data_Transfer_Offset=ntohl(R_Con->Csm_NonPeriodic_Offset);
MIB_Csm->CommunicationCycle_Version_Number=CommunicationCycle_Version_Number_ID;
//组态链接对象
MIB_Lo->Send_Time_Offset=ntohl(R_Con->Lo_Sendtime_Offset);
}
/**************************************************************************
**
** 函数: void Send_EM_DeviceAnnunciation(void)
** 描述: 该函数完成设备声明报文的发送
**
**************************************************************************/
void Send_EM_DeviceAnnunciation(void)
{
uchar i;
EM_DeviceAnnunciation_MessageID++; //设备声明报文标识加1
memcpy(S_SME_Eda->DeviceID,MIB_Edd->DeviceID,32);
memcpy(S_SME_Eda->PD_Tag,MIB_Edd->PD_Tag,32);
S_SME_Eda->Status=MIB_Edd->Status;
S_SME_Eda->Device_Type=MIB_Edd->Device_Type;
S_SME_Eda->Annunciation_Version_Number=htons(MIB_Edd->Annunciation_Version_Number);
S_SME_Eda->Device_Redundancy_Number=MIB_Edd->Device_Redundancy_Number;
S_SME_Eda->Duplicate_Tag_Detected=MIB_Edd->Duplicate_Tag_Detected;
if(S_SME_Eda->Device_Redundancy_Number!=0)
{
S_SME_Eda->Device_Redundancy_State=MIB_Edd->Device_Redundancy_State;
S_SME_Eda->LAN_Redundancy_Port=htons(MIB_Edd->LAN_Redundancy_Port);
S_SME_Eda->Max_Redundancy_Number=MIB_Edd->Max_Redundancy_Number;
memcpy(S_SME_Eda->Active_IP_Address,MIB_Edd->Active_IP_Address,4);
}
//将数据传送给EPA套接字映射接口对象
memcpy(Som->Local_IP_Address,My_IP,4);
memcpy(Som->Remote_IP_Address,Bro_IP,4);
Som->Active_UDP_Port=EPA_Port;
Som->Active_ServiceID=EM_DeviceAnnunciation_ID;
Som->Active_MessageID=EM_DeviceAnnunciation_MessageID;
Som->Active_Message_Time.Date=0;
Som->Active_Message_Time.Millisecond=0;
Som->Message_Length=sizeof(struct EM_DeviceAnnunciation);
//使用EPA套接字映射接口对报文进行打包,并作相关处理
Socket_Enpacket();
}
/**************************************************************************
**
** 函数: void Send_EM_ClearDeviceAttribute(void)
** 描述: 该函数向下位机发清除设备属性报文,下位机不会调用该函数
**
**************************************************************************/
void Send_EM_ClearDeviceAttribute(void)
{
EM_ClearDeviceAttribute_MessageID++; //清除设备属性报文标识加1
memcpy(S_SME_Cda->Destination_IP_Address,Dest_IP,4);
memcpy(S_SME_Cda->DeviceID,R_SME_Eda->DeviceID,32);
memcpy(S_SME_Cda->PD_Tag,R_SME_Eda->PD_Tag,32);
//将数据传送给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_ClearDeviceAttribute_ID;
Som->Active_MessageID=EM_ClearDeviceAttribute_MessageID;
Som->Active_Message_Time.Date=0;
Som->Active_Message_Time.Millisecond=MIB_Mrt->Max_Response_Time;
Som->Message_Length=sizeof(struct EM_ClearDeviceAttribute);
Som->Max_Retrasmit_Number=Max_Resend;
//使用EPA套接字映射接口对报文进行打包,并作相关处理
Socket_Enpacket();
}
/**************************************************************************
**
** 函数: void Send_EM_ClearDeviceAttribute_Result_Positive(void)
** 描述: 该函数对上位机发的清除设备属性报文作正响应
**
**************************************************************************/
void Send_EM_ClearDeviceAttribute_Result_Positive(void)
{
memcpy(S_SME_Cdp->Destination_IP_Address,Dest_IP,4);
//将数据传送给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_ClearDeviceAttribute_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_ClearDeviceAttribute_Result_Positive);
//使用EPA套接字映射接口对报文进行打包,并作相关处理
Socket_Enpacket();
}
/**************************************************************************
**
** 函数: void Send_EM_SetDeviceAttribute(void)
** 描述: 该函数向下位机发设置设备属性报文,下位机不会调用该函数
**
**************************************************************************/
void Send_EM_SetDeviceAttribute(void)
{
uchar i;
EM_SetDeviceAttribute_MessageID++; //设置设备属性报文标识加1
memcpy(S_SME_Sda->Destination_IP_Address,Dest_IP,4);
memcpy(S_SME_Sda->DeviceID,R_SME_Eda->DeviceID,32);
memcpy(S_SME_Sda->PD_Tag,S_SME_Sda->DeviceID,32);
S_SME_Sda->Annunciation_Interval=htons(15);
S_SME_Sda->Duplicate_Tag_Detected=FALSE;
S_SME_Sda->Device_Redundancy_Number=0;
//将数据传送给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=EM_SetDeviceAttribute_MessageID;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -