www.pudn.com > src.rar > iec_103.h, change:2013-12-17,size:12371b


//DL/T667-1999(neq IEC60870-5-103:1997)规约头文件 
#ifndef _IEC103_H_  
#define _IEC103_H_  
 
#include <sys/time.h> 
#include <pthread.h> 
#include "iec_103appdata.h"  
  
#define CH_MAX_IED_NUM 20 
 
// 规约链路层固定格式信息  
const unsigned char	BROADCAST_ADD	= 0xFF;  
const unsigned char	INVALID_ADD		= 0xFF;  
  
const int	GENER_GROUPENTRY= 256;	// 通用分类服务组和条目号最大值  
const int	PTL_WATCH_LEN	= 1800000;	// 30s = 30*60*1000  
const int   MAX_INIT_NUM	= 25;	// 单元通讯异常最大初始化次数  
const int	LINK_SERIAL_ERROR = 2000;  
  
// 链路层控制帧  
struct LinkCtrl  
{  
	union  
	{  
		unsigned char		lnkCtrl;  
		struct   
		{  
			unsigned char	res:1,  
					prm:1,		// 0-监视方向、1-控制方向  
					fcbAcd:1,	//  
					fcvDfc:1,	//  
					funcode:4;	// 功能码  
		};  
	};  
};  
  
// 链路帧格式  
struct LPDU_Fix_Fmt  
{  
	unsigned char	head;  
	struct LinkCtrl ctrl;  
	unsigned char	addr;  
	unsigned char	sum;  
	unsigned char	tail;  
};  
  
struct LPDU_Vol_Head  
{  
	unsigned char	head1;  
	unsigned char	len1;  
	unsigned char	len2;  
	unsigned char	head2;  
	struct LinkCtrl ctrl;  
	unsigned char	addr;  
};  
  
// 功能类型+信息序号  
struct FunInf   
{  
	unsigned char	fun;  
	unsigned char	inf;  
};  
// 可变结构限定词  
struct VSQ   
{  
	union  
	{  
		unsigned char		vsq;  
		struct   
		{  
			unsigned char	sq:1,  
					num:7;  
		};  
	};  
};  
  
// 应用层数据帧  
struct ASDU_Head  
{  
	unsigned char		type;  
	struct VSQ			vsq;  
	unsigned char		cot;  
	unsigned char		addr;  
  
	union  
	{  
		struct FunInf	funInf;  
		struct   
		{  
			unsigned char		fun;  
			unsigned char		inf;  
		};	   
	};  
};  
  
// ASDU_1_Fmt  
struct ASDU_1_Fmt  
{  
	struct ASDU_Head	head;  
	DPI			dpi;  
	struct TIME_4		tm;  
	SIN			sin;  
};  
  
// ASDU_2_Fmt  
struct ASDU_2_Fmt  
{  
	struct ASDU_Head	head;  
	DPI			dpi;  
	unsigned short	ret;  
	unsigned short		fan;  
	struct TIME_4		tm;  
	SIN			sin;  
};  
  
//GC数据  
struct ASDU_21_Fmt  
{  
	struct ASDU_Head	head;  
	unsigned char		rii;  
	NOG			nog;  
	unsigned char		group;  
	unsigned char		entry;  
	KOD			kod;  
};  
  
//GD数据帧  
struct ASDU_10_Head  
{  
	struct ASDU_Head	head;  
	  
	unsigned char		rii;  
	struct NGD			ngd;  
};  
  
// 通用分类数据  
struct ASDU_10_Item   
{  
	struct GIN			gin;  
	unsigned char		kod;  
	struct GDD			gdd;  
	unsigned char		byVal;  
};  
  
//GI数据帧  
struct ASDU_11_Head  
{  
	struct ASDU_Head	head;  
  
	unsigned char		rii;  
	unsigned char		group;  
	unsigned char		entry;  
	struct NDE			nde;  
};  
  
// 校时命令  
struct ASDU_6_Fmt  
{  
	struct ASDU_Head	head;  
	struct TIME_7		dataTime;  
};  
  
// 总查询启动  
struct ASDU_7_Fmt  
{  
	struct ASDU_Head	head;  
	SCN			scn;  
};  
  
// 一般命令  
struct ASDU_20_Fmt  
{  
	struct ASDU_Head	head;  
	unsigned char		dco;  
	unsigned char		rii;  
};  
  
// 遥控命令  
struct ASDU_64_Fmt  
{  
	struct ASDU_Head	head;  
	struct DCC			dcc;  
	unsigned char		rii;  
};  
  
// 升降命令  
struct ASDU_65_Fmt  
{  
	struct ASDU_Head	head;  
	struct RCC			rcc;  
	unsigned char		rii;  
};  
  
// 同期遥控命令  
struct ASDU_67_Fmt  
{  
	struct ASDU_Head	head;  
	struct CCC			ccc;  
	unsigned char		rii;  
};  
  
// 电能脉冲计数量的召唤命令  
struct ASDU_88_Fmt  
{  
	struct ASDU_Head	head;  
	struct QCC			qcc;  
	unsigned char		rii;  
};  
  
// 通用分类命令响应帧数据头  
struct TaskRespHead  
{  
	unsigned short		bufLen;  
	unsigned short		fmtNum;  
};  
  
// 通用分类组帧  
struct ModifyProtSettingHead  
{  
	unsigned short		bufLen;  
	unsigned short		fmtNum;  
};  
  
// 链路层帧控制域之功能码  
enum LinkCtrlCode  
{   
	// 主->子  
	ResCU_code = 0,  
	SendCon_code = 3,  
	NoReply_code = 4,  
	ResFcb_code = 7,  
	QueryLink_code = 9,  
	CallClass1_code = 10,  
	CallClass2_code = 11,  
	// 子->主  
	AfmCon_code = 0,  
	BusyCon_code = 1,  
	RespData_code = 8,  
	RespNone_code = 9,  
	RespLink_code = 11,  
	SrvNoWrk_code = 14,  
	SrvNoFin_code = 15,  
};  
// 链路服务级别  
enum LinkSvrCls   
{   
	UndefSvrCls = 0,	// 无效数据  
	SendNoreply = 1,  
	SendConfirm = 2,  
	RequestRespond = 3,  
};  
  
// 服务原语  
struct CommPrim {  
	unsigned char		byCode;			// 功能码  
	unsigned char		lnkAddr;		// 链路层地址域  
  
	unsigned char		*pbyApp;		// APDU数据指针  
	unsigned char		byLen;			// APDU数据长度  
};  
  
// 应用层数据域类型标识  
enum TypIdent   
{	   
	InvalidIdent = 0,  
	// 兼容范围内的类型标识:监视方向  
	M_TM_TA_3 = 1,  
	M_TMR_TA_3 = 2,  
	M_MEI_NA_3 = 3,  
	M_TME_TA_3 = 4,  
	M_IRC_NA_3 = 5,  
	M_SYN_TA_3 = 6,  
	M_TGI_NA_3 = 8,  
	M_MEII_NA_3 = 9,  
	M_GD_NTA_3 = 10,  
	M_GI_NTA_3 = 11,  
	M_LRT_TA_3 = 23,  
	M_RTD_TA_3 = 26,  
	M_RTC_NA_3 = 27,  
	M_RTT_NA_3 = 28,  
	M_TDT_TA_3 = 29,  
	M_TDN_NA_3 = 30,  
	M_EOT_NA_3 = 31,  
	// 专用范围内的类型标识:监视方向  
	M_MEIII_TA_3 = 32,  
	M_MEIV_TA_3 = 33,  
	M_MEV_TA_3 = 34,  
	M_MEVI_TA_3 = 35,  
	M_MEVII_NA_3 = 50,  
	M_IT_NA_3 = 36,  
	M_IT_TA_3 = 37,  
	M_ST_NA_3 = 38,  
	M_ST_TA_3 = 39,  
	M_SP_NA_3 = 40,  
	M_SP_TA_3 = 41,  
	M_DP_NA_3 = 42,  
	M_DP_TA_3 = 43,  
	M_SS_NA_3 = 44,  
	M_SS_TA_3 = 45,  
	M_DS_NA_3 = 46,  
	M_DS_TA_3 = 47,  
	M_WL_TA_3 = 48,  
  
	M_DC_NA_3 = 64,  
	M_RC_NA_3 = 65,  
	M_SE_NA_3 = 66,  
	M_CC_NA_3 = 67,  
	M_ASDU_70 = 70,  
	M_CI_NA_3 = 88,  
	// 兼容范围内的类型标识:控制方向  
	C_SYN_TA_3 = 6,  
	C_IGI_NA_3 = 7,  
	C_GD_NA_3 = 10,  
	C_GRC_NA_3 = 20,  
	C_GC_NA_3 = 21,  
	C_ODT_NA_3 = 24,  
	C_ADT_NA_3 = 25,  
	//专用范围内的类型标识:控制方向  
	C_DC_NA_3 = 64,  
	C_RC_NA_3 = 65,  
	C_SE_NA_3 = 66,  
	C_CC_NA_3 = 67,  
	C_CI_NA_3 = 88,  
};	// end TypIdent  
  
// 应用数据传输原因  
enum COT_Code {   
	// 监测方向  
	M_AUTO = 1,  
	M_CYCLE = 2,  
	M_RET_FCB = 3,  
	M_RET_CU = 4,  
	M_INIT = 5,  
	M_PWR = 6,  
	M_TEST_MODAL = 7,  
	M_SYN_TIM = 8,  
	M_CALL = 9,  
	M_CALL_END = 10,  
	M_LOCAL_OP = 11,  
	M_FAR_OP = 12,  
	M_AFM_ORD = 20,  
	M_NEG_ORD = 21,  
	M_TRN_FAU = 31,  
	M_AFM_GENWR = 40,  
	M_NEG_GENWR = 41,  
	M_AFM_GENRD = 42,  
	M_NEG_GENRD = 43,  
	M_AFM_GEN = 44,  
	// 控制方向  
	C_SYN_TIM = 8,  
	C_QUERY_STAR = 9,  
	C_NML_CMD = 20,  
	C_FAULT_TRN = 31,  
	C_GEN_WRITE = 40,  
	C_GEN_READ = 42,  
	C_FAR_OP = 12,  
	C_CYCLE = 2,  
};	// COT_Code  
typedef unsigned char COT;  
// 功能类型  
enum FunType {   
	// 兼容范围  
	DisPrt_fun = 128,  
	CurPrt_fun = 160,  
	TraPrt_fun = 176,  
	LinPrt_fun = 192,  
	Gen_fun = 254,  
	Glb_fun = 255,  
	// 专用范围  
	BU1_fun = 1,  
	BU2_fun = 2,  
	BU3_fun = 3,  
	BU4_fun = 4,  
	BU5_fun = 5,  
	BU6_fun = 6,  
	DisPrt1_fun = 130,  
	CurPrt1_fun = 133,  
	CurPrt2_fun = 136,  
	CurPrt3_fun = 139,  
	CurPrt4_fun = 142,  
	Traprt1_fun = 146,  
	LinPrt1_fun = 152,  
	LP1_fun = 178,  
	AP1_fun = 184,  
	TP1_fun = 194,  
	TP2_fun = 198,  
	BS1_fun = 210,  
	GR1_fun = 226,  
	GR2_fun = 230,  
	LP2_fun = 242,  
	AP2_fun = 248,  
}; // end FunCode  
  
// 通用分类服务 (功能码+信息序号)  
enum GenerFunInf {	  
	GEN_FUN = 254,			// 通用分类服务功能码  
  
	ReadTitle = 240,		// 所有标题  
	ReadGroupAttrVal = 241,	// 组属性或值  
	ReadEntryDir = 243,		// 条目目录  
	ReadEntryAttrVal = 244,	// 条目属性或值  
	GenericQuery = 245,		// 通用分类数据总查询  
	WriteEntry = 248,		// 写条目  
	WriteEntryAfm = 249,	// 写条目确认  
	WriteEntryExec = 250,	// 写条目执行  
	WriteEntryCancel = 251,	// 写条目停止  
};  
  
// 通用分类命令结构  
struct C_GC_CMD   
{  
	unsigned char	byAdd;  
	unsigned char	inf;  
	RII		rii;  
	struct GIN		gin;  
	KOD		kod;  
};  
// 一般命令数据描述  
struct GRC_CMD {  
	unsigned char fun;  
	unsigned char inf;  
	enum DCO dco;  
	RII	rii;  
};  
// 故障录波数据描述  
struct FaultData{  
	TOO too;  
	TOV tov;  
	FAN fan;  
	ACC acc;  
};  
// 控制命令信息  
union ReserveCmd  
{  
	struct DCC		dcc;    
	struct RCC		rcc;  
	struct CCC		ccc;  
};  
  
/***  
*Task103 - 规约任务描述  
*  
* 根据不同的 (任务类型+设备类型) 调用不同的组帧和解帧函数  
*****/  
// 通用分类服务读命令  
struct GenerReadDesc   
{					  
	unsigned char				readGrp[256];		// 读分类命令组号  
	unsigned char				grpNum;				// 组数  
	unsigned char				grpCnt;				// 组计数  
};  
  
// 通用分类服务写命令   
struct GenerWriteDesc  
{  
	unsigned short				wSendPos;		// 发送位置计数	  
	unsigned short				wSendFmtCnt;	// 发送帧计数  
	unsigned short				wSendNum;		// 发送数  
	unsigned char				rii;			// 设置返回码   
}; // end GenerWriteDesc  
  
// 控制命令  
struct ControlTask   
{  
	union ReserveCmd	oldCmd;  
	struct FunInf		funInf;  
};  
// 通道任务  
struct ChanTaskCmd {  
	unsigned char	byAdd;	// 通道任务节点号	  
};  
  
enum eInitStep  
{  
	eInitStart,  
	eInitTime,  
	eInitGenAsk,  
	eInitEnd,  
};  
  
// 运行状态描述  
struct RealRunIED  
{  
	unsigned char byAddr; 
	unsigned short ycAddr; 
	unsigned short yxAddr; 
	char *devname; 
	unsigned char	byNodeId;		// 单元节点号  
//	CIED    *pDevData;		// 测控数据区  
  
	unsigned short	query:1,		// 总查询: 0-定时未到 1-定时已到  
			queryFull:1,	// 总召唤/召唤脉冲计数量  
			linkRes:1,		// 复位链路  
			linkQuery:1,	// 流量控制  
			acd:1,			// 请求1级数据  
			fcb:1,			// 帧计数位  
			recvCode:4,		// 接收功能码  
			sendCode:4,		// 发送功能码  
			mode:2;			// 状态:   0-正常 1-测试模式 2-监测闭锁 3-初始化状态  
  
	unsigned short	lnkNoRecvCnt;	// 通讯故障计数  
  
	SIN		sin;			// 返回信息标识  
  
	unsigned char    bCommInitFlag;	// 通讯初始化标志false:初始化已经结束true:初始化还没有完成  
	int     nASDU5Cnt;		// 收到的asdu5帧计数  
	enum eInitStep step;  
}; // end DevRun  
  
// Flash参数  
struct FlashPara103  
{  
	// 通用参数	  
	unsigned short	byInitNum;		// 初始化重传次数  
	unsigned short	dwFrameOver;	// 帧超时间隔  
	unsigned short	dwQuery;		// 总查询间隔  
	unsigned short    wTimeOver;		// 校时间隔  
	// 保护遥信复归  
	unsigned short	bOrdAfmReset;	// 确认复归(复归命令肯定确认时,保护信号复归)  
	unsigned short	bOrderReset;	// 命令复归(复归命令毋须确认,保护信号即可复归)  
	unsigned short	bEventVirtualYX;// 故障事项虚遥信  
	unsigned short    bCommAddr;		// 使用单元地址还是cpu地址标志0:单元地址1:cpu地址  
	unsigned short    byBHCPUAddr;    // 保护cpu公共地址  
	unsigned short    byCKCPUAddr;	// 测控cpu公共地址  
//	unsigned short    byBhResetFun;   // 保护复归fun  
//	unsigned short    byBhResetInf;   // 保护复归inf  
};  
  
// 规约参数  
struct PtlPara103   
{  
	// 通用参数	  
	unsigned char	byInitNum;		// 初始化重传次数  
	unsigned short	dwFrameOver;	// 帧超时间隔  
	unsigned short	dwQuery;		// 总查询间隔  
	unsigned short    wTimeOver;		// 校时间隔  
	// 保护遥信复归  
	unsigned char	bOrdAfmReset;	// 确认复归(复归命令肯定确认时,保护信号复归)  
	unsigned char	bOrderReset;	// 命令复归(复归命令毋须确认,保护信号即可复归)  
	unsigned char	bEventVirtualYX;// 故障事项虚遥信  
	unsigned char    bCommAddr;		// 使用单元地址还是cpu地址标志0:单元地址1:cpu地址  
	unsigned char    byBHCPUAddr;    // 保护cpu公共地址  
	unsigned char    byCKCPUAddr;	// 测控cpu公共地址  
//	unsigned char    byBhResetFun;   // 保护复归fun  
//	unsigned char    byBhResetInf;   // 保护复归inf  
};	// end PtlPara103  
  
// 规约运行处理步骤  
enum PtlRunStep  
{  
	PtlRun_PackASDU = 1,	// 应用层组帧	  
	PtlRun_SendLPDU,		// 链路层发送  
	PtlRun_RecvLPDU,		// 链路层接收  
	PtlRun_UnpackASDU,		// 应用层解帧  
  
	PtlRun_NoLPDU,			// 链路无接收数据  
};  
 struct timer 
 { 
	struct timeval time_base; 
	long long time_out; 
 }; 
  
// 规约数据  
struct PtlData103   
{	  
	int fd; 
	pthread_mutex_t *lock; 
 
	 struct timer frame_timer; 
	 struct timer query_timer; 
	 struct timer synctm_timer; 
 
	 int AdjustTimeFlag; 
	 
	// 系统资源  
	struct RealRunIED *m_RunIed[CH_MAX_IED_NUM];;	// 设备运行期描述  
	 
	unsigned char        m_addrtoID[255];			// 通过设备地址查设备ID  
	unsigned char		m_byCycCnt;					// 循环查询ID  
  
	enum PtlRunStep	m_runStep;					// 运行步骤  
  
	// 服务原语  
	struct CommPrim	m_prmSend;					// 发送(应用层=>链路层)  
	struct CommPrim	m_prmRecv;					// 接收(链路层=>应用层)  
  
	enum LinkSvrCls	m_lnkSvrCls;				// 链路服务类型  
  
	union ReserveCmd	m_OldCmd;					// 遥控命令备用区  
  
	// 链路层数据区  
	unsigned short		m_wLinkLen;					// 链路数据帧长  
  
	unsigned char		m_bySend[300];  
	unsigned short		m_wSendNum;  
	  
	unsigned char		m_bySendCnt;				// 链路数据发送计数  
	unsigned char		m_bySin;					// 公用bySin  
  
	unsigned short		m_lnkSerialError;			// 链路连续出错  
  
};	// end PtlData103  
  
#endif