/******************************************************************************

                  版权所有 (C), 2013-2023, 科大智能电气有限公司

 ******************************************************************************
  文 件 名   : ASDU.h
  版 本 号   : V1.0
  生成日期   : 2022年04月07日
  最近修改   :
  功能描述   : 101及104规约的公共部分ASUD域相关数据处理
  函数列表   :
  修改历史   :
  1.日    期   : 2022年04月07日
    修改内容   : 创建文件
*******************************************************************************/
#ifndef __ASDU_H
#define __ASDU_H
/**************************************文件包含*******************************/
#include <stdio.h>
#include "lib60870_common.h"
#include "lib60870_public.h"
#include "lib60870_process.h"
#include "gb101.h"

#ifdef __cplusplus
extern "C" {
#endif

/**************************************宏定义*********************************/

#define MAX_FLOATYC_CNT 48
#define INTYC_MAX_CNT   80
#define MAX_Yx_CNT      127
#define MAX_Dd_CNT      48
#define MAX_Param_CNT   4
#define MAX_COS_CNT     60
/************************************数据结构定义*****************************/

typedef union
{
	/*带品质描述词的单点信息*/
	struct
	{
		unsigned char ucSPI: 1;  /*0=开 1=合*/
		unsigned char ucRES: 3;  /*备用*/
		unsigned char ucBL : 1;   /*0=未被锁定 1=锁定*/
		unsigned char ucSB : 1;   /*0=未被取代 1=取代*/
		unsigned char ucNT : 1;   /*0=当前值 1=非当前值*/
		unsigned char ucIV : 1;   /*0=有效 1=无效*/
	} SIQ;

	/*带品质描述词的双点信息*/
	struct
	{
		unsigned char ucDPI: 2;  /*0=不确定或中间状态 1=确定状态开 2=确定状态合 3=不确定*/
		unsigned char ucRES: 2;  /*备用*/
		unsigned char ucBL : 1;   /*0=未被锁定 1=锁定*/
		unsigned char ucSB : 1;   /*0=未被取代 1=取代*/
		unsigned char ucNT : 1;   /*0=当前值 1=非当前值*/
		unsigned char ucIV : 1;   /*0=有效 1=无效*/
	} DIQ;

	/*品质描述词*/
	struct
	{
		unsigned char ucOV : 1;  /*0=未溢出 1=溢出*/
		unsigned char ucRES: 3;  /*备用*/
		unsigned char ucBL : 1;   /*0=未被锁定 1=锁定*/
		unsigned char ucSB : 1;   /*0=未被取代 1=取代*/
		unsigned char ucNT : 1;   /*0=当前值 1=非当前值*/
		unsigned char ucIV : 1;   /*0=有效 1=无效*/
	} QDS;

	/*继电保护设备事件的品质描述词*/
	struct
	{
		unsigned char ucRES: 3;  /*备用*/
		unsigned char ucEI : 1;  /*0=动作时间有效 1=动作时间无效*/
		unsigned char ucBL : 1;   /*0=未被锁定 1=锁定*/
		unsigned char ucSB : 1;   /*0=未被取代 1=取代*/
		unsigned char ucNT : 1;   /*0=当前值 1=非当前值*/
		unsigned char ucIV : 1;   /*0=有效 1=无效*/
	} QDP;

	/*带瞬变状态指示的值*/
	struct
	{
		unsigned char ucI7 : 7;  /*瞬变状态*/
		unsigned char ucBS1: 1;  /*0=动作时间有效 1=动作时间无效*/
	} VTI;


	/*单命令*/
	struct
	{
		unsigned char ucSCS: 1;  /*0=开 1=合*/
		unsigned char ucRES: 1;  /*备用*/
		unsigned char ucQU : 5;   /*0=无另外定义 1=短脉冲持续时间 2=长脉冲持续时间 3=持续输出*/
		unsigned char ucSE : 1;   /*0=执行 1=选择*/
	} SCO;

	/*双命令*/
	struct
	{
		unsigned char ucDCS: 2;  /*0=不允许 1=开 2=合 3=不允许*/
		unsigned char ucQU : 5;   /*0=无另外定义 1=短脉冲持续时间 2=长脉冲持续时间 3=持续输出*/
		unsigned char ucSE : 1;   /*0=执行 1=选择*/
	} DCO;

	/*步调节命令*/
	struct
	{
		unsigned char ucRCS: 2;  /*0=不允许 1=降一步 2=升一步 3=不允许*/
		unsigned char ucQU : 5;   /*0=无另外定义 1=短脉冲持续时间 2=长脉冲持续时间 3=持续输出*/
		unsigned char ucSE : 1;   /*0=执行 1=选择*/
	} RCO;

	/*初始化原因*/
	struct
	{
		unsigned char ucUI7: 7;  /*0=当地电源合上 1=当地手动复位 2=远方复位*/
		unsigned char ucBS1: 1;  /*0=未改变当地参数初始化 1=改变当地参数初始化*/
	} COI;

	/*设定命令限定词*/
	struct
	{
		unsigned char ucQL: 7;  /*0=缺省*/
		unsigned char ucSE: 1;  /*0=执行 1=选择*/
	} QOS;

	unsigned char ucBits;
} ASDU_CMD_T;

/**************************************数据声明******************************/


/**************************************内部函数声明***************************/
unsigned char DecodeASDUCheck(CS10x *pstSelf, unsigned char *pucPayload);
unsigned char DecodeCheck_M_EI_NA(CS10x *pstSelf, unsigned char *pucPayload);
unsigned char DecodeCheck_C_IC_NA(CS10x *pstSelf, unsigned char *pucPayload);
unsigned char DecodeCheck_C_CI_NA(CS10x *pstSelf, unsigned char *pucPayload);
unsigned char DecodeCheck_C_CS_NA(CS10x *pstSelf, unsigned char *pucPayload);
unsigned char DecodeCheck_C_RP_NA(CS10x *pstSelf, unsigned char *pucPayload);
unsigned char DecodeCheck_C_TS_NA(CS10x *pstSelf, unsigned char *pucPayload);
unsigned char DecodeCheck_F_FR_NA(CS10x *pstSelf, unsigned char *pucPayload);
unsigned char DecodeCheck_C_SR_NA(CS10x *pstSelf, unsigned char *pucPayload);
unsigned char DecodeCheck_C_RR_NA(CS10x *pstSelf, unsigned char *pucPayload);
unsigned char DecodeCheck_C_RS_NA(CS10x *pstSelf);
unsigned char DecodeCheck_C_WS_NA(CS10x *pstSelf);
unsigned char DecodeCheck_M_SPDP_NA(CS10x *pstSelf);
unsigned char DecodeCheck_M_ME_NANBNC(CS10x *pstSelf);
unsigned char DecodeCheck_M_IT_NB(CS10x *pstSelf);
unsigned char DecodeCheck_M_IT_TC(CS10x *pstSelf);
unsigned char DecodeCheck_C_SCDC_NA(CS10x *pstSelf);
unsigned char DecodeCheck_M_FT_NA(CS10x *pstSelf);
unsigned char DecodeCheck_M_DPSP_TB(CS10x *pstSelf);
unsigned char DecodeCheck_F_SR_NA(CS10x *pstSelf);
unsigned char DecodeCheck_C_CD_NA(CS10x *pstSelf, unsigned char *pucPayload);
unsigned char DecodeCheck_C_RS_NA_NANWANG(CS10x *pstSelf);
unsigned char DecodeCheck_C_WS_NA_YZ(CS10x *pstSelf);
unsigned char DecodeCheck_C_WS_NA_GH(CS10x *pstSelf, unsigned char *pucPayload);
unsigned char DecodeCheck_C_SP_NA(CS10x *pstSelf);
void Do_SetTime(CS10x *pstSelf, unsigned short usReason);
void Do_Summon(CS10x *pstSelf, unsigned short usReason);
void Do_DdSummon(CS10x *pstSelf, unsigned short usReason);
void Do_Reset_Process(CS10x *pstSelf, unsigned short usReason);
void Do_DataTest(CS10x *pstSelf, unsigned short usReason);
void Do_InitEnd(CS10x *pstSelf, unsigned short usReason);
void Do_Yx(CS10x *pstSelf, unsigned short usReason);
void Do_Yc(CS10x *pstSelf, unsigned short usReason);
void Do_DdNoTime(CS10x *pstSelf, unsigned short usReason);
void Do_DdWithTime(CS10x *pstSelf, unsigned short usReason);
void Do_YKPro(CS10x *pstSelf, unsigned short usReason);
void Do_SOE(CS10x *pstSelf);
void Do_FaultEvent(CS10x *pstSelf);
void Do_FilePro_Slave(CS10x *pstSelf, unsigned char ucType, unsigned char *pucBuf, unsigned short usFileDateLen);
void Do_FilePro_Master(CS10x *pstSelf, unsigned char ucType, unsigned char *pucBuf);
void Do_FilePro(CS10x *pstSelf, unsigned short usReason);
void Do_Anomaly(CS10x *pstSelf, unsigned short usReason);
void Do_ChgConst(CS10x *pstSelf, unsigned short usReason);
void Do_ReadConst(CS10x *pstSelf, unsigned short usReason);
void Do_ParamPreread(CS10x *pstSelf, unsigned short usReason);
void Do_ParamPreset(CS10x *pstSelf, unsigned short usReason);

unsigned short  CS10x_Send_Yx(CS10x *pstSelf, unsigned int uiParam);
unsigned short  CS10x_Send_SummonYx(CS10x *pstSelf, unsigned int uiParam);
unsigned short  CS10x_Send_COS(CS10x *pstSelf, unsigned int uiParam);

unsigned short  CS10x_Send_Yc(CS10x *pstSelf, unsigned int uiParam);
unsigned short  CS10x_Send_SummonYc(CS10x *pstSelf, unsigned int uiParam);
unsigned short  CS10x_Send_ChangeYc(CS10x *pstSelf, unsigned int uiParam);

unsigned short  CS10x_Send_SOE(CS10x *pstSelf, unsigned int uiParam);
unsigned short  CS10x_Send_FaultEvent(CS10x *pstSelf, unsigned int uiParam);

unsigned short  CS10x_Send_DdData(CS10x *pstSelf, unsigned int uiParam);
unsigned short  CS10x_Send_SummonDd_Data(CS10x *pstSelf, unsigned int uiParam);
unsigned short  CS10x_Send_TimeDd_Data(CS10x *pstSelf, unsigned int uiParam);

unsigned short  CS10x_Send_SynTime(CS10x *pstSelf, unsigned int uiParam);
unsigned short  CS10x_Send_Summon(CS10x *pstSelf, unsigned int uiParam);
unsigned short  CS10x_Send_DdSummon(CS10x *pstSelf, unsigned int uiParam);
unsigned short  CS10x_Send_ResetProcess(CS10x *pstSelf, unsigned int uiParam);
unsigned short  CS10x_Send_DataTest(CS10x *pstSelf, unsigned int uiParam);
unsigned short  CS10x_Send_InitEnd(CS10x *pstSelf, unsigned int uiParam);
unsigned short  CS10x_Send_FilePro(CS10x *pstSelf, unsigned int uiParam);
unsigned short  CS10x_Send_ChgConst(CS10x *pstSelf, unsigned int uiParam);
unsigned short  CS10x_Send_ReadConst(CS10x *pstSelf, unsigned int uiParam);
unsigned short  CS10x_Send_ParamPreread(CS10x *pstSelf, unsigned int uiParam);
unsigned short  CS10x_Send_ParamPreset(CS10x *pstSelf, unsigned int uiParam);
unsigned short  CS10x_Send_Yk(CS10x *pstSelf, unsigned int uiParam);
unsigned short  CS10x_Send_DdSummon_Data(CS10x *pstSelf, unsigned int uiParam);
unsigned short CS10x_Send_Anomaly(CS10x *pstSelf, unsigned int param);
unsigned short CS10x_Send_Update(CS10x *pstSelf, unsigned int uiParam);
void Do_Update(CS10x *pstSelf, unsigned short usReason);
unsigned short GetPayloadOffset(CS10x *pstSelf);
void CheckFileDiff(CS10x *pstSelf);
unsigned short DecodeAsduMember(CS10x *pstSelf, unsigned char * pucAsdu, unsigned short usAsduSz);
unsigned short GetTaskTItype(CS10x *pstSelf, unsigned short usDataType);
/************************************外部接口函数声明*************************/
extern unsigned short GB10x_EncodeAsdu(CS10xHandle_t pstSelf, unsigned char * pucBuf);
extern void DecodeAsduInfoObj(CS10x *pstSelf);
extern unsigned short EncodeAsdu(CS10x *pstSelf);
extern void DoAckFrame(CS10x *pstSelf);
/********************************文件传输接口**************************************/
void Revc_ReadFS_CallDir_S(CS10x *pstSelf, unsigned char *pucBuf, tFILEDIR_RD_SET *pstRdSet);       //召唤目录

void Revc_ReadFS_AckCallDir_M(CS10x *pstSelf, unsigned char *pucBuf, tFILEDIR_RD_SET *pstRdSet); //召唤目录确认

void Revc_ReadFS_SelectFile_S(CS10x *pstSelf, unsigned char *pucBuf, tFILEDIR_RD_SET *pstRdSet); //读文件激活

void Revc_ReadFS_AckSelectFile_M(CS10x *pstSelf, unsigned char *pucBuf, tFILEDIR_RD_SET *pstRdSet); //读文件激活确认

void Revc_ReadFS_CallFileSeg_M(CS10x *pstSelf, unsigned char *pucBuf, tFILEDIR_RD_SET *pstRdSet);   //读文件数据

void Revc_ReadFS_AckFileSeg_S(CS10x *pstSelf, unsigned char *pucBuf, tFILEDIR_RD_SET *pstRdSet);    //读文件数据响应

void Recv_WriteFS_SelectFile_S(CS10x *pstSelf, unsigned char *pucBuf, tFILE_WD_SET  *pstWdSet);

void Recv_WriteFS_AckSelectFile_M(CS10x *pstSelf, unsigned char *pucBuf, tFILE_WD_SET *pstWdSet);

void Recv_WriteFS_DownFileSeg_S(CS10x *pstSelf, unsigned char *pucBuf, tFILE_WD_SET *pstWdSet, unsigned short usFileDateLen);

void Recv_WriteFS_AckDownFileSeg_M(CS10x *pstSelf, unsigned char *pucBuf, tFILE_WD_SET *pstWdSet);

unsigned short Send_ReadFS_CallDir_M(CS10x *pstSelf, unsigned short usFileProType,  ASDU_Element *pstAsdus, tFILEDIR_RD_SET *pstRdSet);

unsigned short Send_ReadFS_AckCallDir_S(CS10x *pstSelf, unsigned short usFileProType, ASDU_Element *pstAsdus, tFILEDIR_RD_SET *pstRdSet);

unsigned short Send_ReadFS_SelectFile_M(CS10x *pstSelf, unsigned short usFileProType, ASDU_Element *pstAsdus, tFILEDIR_RD_SET *pstRdSet);

unsigned short Send_ReadFS_AckSelectFile_S(CS10x *pstSelf, unsigned short usFileProType, ASDU_Element *pstAsdus, tFILEDIR_RD_SET *pstRdSet);

unsigned short Send_ReadFS_CallFileSeg_S(CS10x *pstSelf, unsigned short usFileProType, ASDU_Element *pstAsdus, tFILEDIR_RD_SET *pstRdSet);

unsigned short Send_ReadFS_AckFileSeg_M(CS10x *pstSelf, unsigned short usFileProType,  ASDU_Element *pstAsdus, tFILEDIR_RD_SET *pstRdSet);

unsigned short Send_WriteFS_SelectFile_M(CS10x *pstSelf, unsigned short usFileProType,  ASDU_Element *pstAsdus, tFILE_WD_SET *pstWdSet);

unsigned short Send_WriteFS_AckSelectFile_S(CS10x *pstSelf, unsigned short usFileProType, ASDU_Element *pstAsdus, tFILE_WD_SET *pstWdSet);

unsigned short Send_WriteFS_DownFileSeg_M(CS10x *pstSelf, unsigned short usFileProType,  ASDU_Element *pstAsdus, tFILE_WD_SET *pstWdSet);

unsigned short Send_WriteFS_AckDownFileSeg_S(CS10x *pstSelf, unsigned short usFileProType, ASDU_Element *pstAsdus, tFILE_WD_SET *pstWdSet);

void Do_ObtainDelay(CS10x *pstSelf, unsigned short usReason);

unsigned short CS10x_Send_ObtainDelay(CS10x *pstSelf, unsigned int uiParam);

void Do_ParamRead_NanWang(CS10x *pstSelf, unsigned short usReason);

unsigned short CS10x_Send_ParamRead_NanWang(CS10x *pstSelf, unsigned int uiParam);

unsigned short CS10x_Send_ParamRdResponse_NanWang(CS10x *pstSelf, unsigned int uiParam);

unsigned short CS10x_Send_ParamSolidify_NanWang(CS10x *pstSelf, unsigned int uiParam);

void Do_ParamPreset_NanWang(CS10x *pstSelf, unsigned short usReason);

void DoParamSolidify_NanWang(CS10x *pstSelf, unsigned short usReason);

unsigned short CS10x_Send_ParamPreset_NanWang(CS10x *pstSelf, unsigned int uiParam);

void Do_ParamWrite_Nanwang(CS10x *pstSelf, unsigned short usReason);

unsigned short CS10x_Send_ParamWrite_Nanwang(CS10x *pstSelf, unsigned int uiParam);

#ifdef __cplusplus
}
#endif


#endif