
#ifndef __GOOSE_APP_H
#define __GOOSE_APP_H



#define GOOSE_EQU_SIDE   8 		// goose 通信单侧设备数量
#define GOOSE_EQU_SIDE_N   4 		// goose 通信负荷侧设备个数

#define GOOSE_EQU_MAX  (GOOSE_EQU_SIDE+GOOSE_EQU_SIDE_N)   // goose 通信支持的设备总数

#define GOOSE_RECV_GOLBNUM  16
enum
{
	GOOSE_CMD_TICK=0x11, //心跳报文 11 
	GOOSE_CMD_DATA,		// 突变报文 12
	GOOSE_CMD_GZGLOK,		//故障隔离成功 
};

enum
{
	GOOSE_FIND_LL_NONE=0,  // 寻找联络开关
	GOOSE_FIND_LL_QD, // 寻找联络开关 启动
	GOOSE_FIND_LL_OK,		// 联络开关确认
};

#define FA_CMD_BUF_MAX	32 

enum
{
	GOOSE_SEND_TBL_GZ=0,  // 节点故障
	GOOSE_SEND_TBL_GL,  // 隔离成功
	GOOSE_SEND_TBL_JT,  // 开关拒跳
	GOOSE_SEND_TBL_GLBS,  // 过流闭锁
#if defined GD_AREA_ZHONGSHAN || defined GD_AREA_ZHONGSHAN_2020  //中山要求通信异常，全系统均异常
	GOOSE_SEND_TBL_STATUS,  // 通信状态
	GOOSE_SEND_TBL_FG,  // 通信复归
#endif
	GOOSE_SEND_TBL_NUMBER,  // 零序闭锁
};

enum
{
	GOOSE_RECV_TBL_M01=0,  //M侧节点故障
	GOOSE_RECV_TBL_M02,  //M侧节点故障
	GOOSE_RECV_TBL_M03,  //M侧节点故障
	GOOSE_RECV_TBL_M04,  //M侧节点故障
	GOOSE_RECV_TBL_M05,  //M侧节点故障
	GOOSE_RECV_TBL_M06,  //M侧节点故障
	GOOSE_RECV_TBL_M07,  //M侧节点故障
	GOOSE_RECV_TBL_M08,  //M侧节点故障

	GOOSE_RECV_TBL_N01,  //N侧节点故障
	GOOSE_RECV_TBL_N02,  //N侧节点故障
	GOOSE_RECV_TBL_N03,  //N侧节点故障
	GOOSE_RECV_TBL_N04,  //N侧节点故障

  	GOOSE_RECV_TBL_GL01,  //开关隔离成功
  	GOOSE_RECV_TBL_GL02,  //开关隔离成功
  	GOOSE_RECV_TBL_GL03,  //开关隔离成功
  	GOOSE_RECV_TBL_GL04,  //开关隔离成功
  	GOOSE_RECV_TBL_GL05,  //开关隔离成功
  	GOOSE_RECV_TBL_GL06,  //开关隔离成功
  	GOOSE_RECV_TBL_GL07,  //开关隔离成功
  	GOOSE_RECV_TBL_GL08,  //开关隔离成功
  	GOOSE_RECV_TBL_GL09,  //开关隔离成功
  	GOOSE_RECV_TBL_GL10,  //开关隔离成功
  	GOOSE_RECV_TBL_GL11,  //开关隔离成功
  	GOOSE_RECV_TBL_GL12,  //开关隔离成功
  	
  	
 	GOOSE_RECV_TBL_JT01,  //开关拒跳
 	GOOSE_RECV_TBL_JT02,  //开关拒跳
 	GOOSE_RECV_TBL_JT03,  //开关拒跳
 	GOOSE_RECV_TBL_JT04,  //开关拒跳
 	GOOSE_RECV_TBL_JT05,  //开关拒跳
 	GOOSE_RECV_TBL_JT06,  //开关拒跳
 	GOOSE_RECV_TBL_JT07,  //开关拒跳
 	GOOSE_RECV_TBL_JT08,  //开关拒跳
 	GOOSE_RECV_TBL_JT09,  //开关拒跳
 	GOOSE_RECV_TBL_JT10,  //开关拒跳
 	GOOSE_RECV_TBL_JT11,  //开关拒跳
 	GOOSE_RECV_TBL_JT12,  //开关拒跳
  	
  	GOOSE_RECV_TBL_GLBS01,  //过流闭锁01
  	GOOSE_RECV_TBL_GLBS02,  //过流闭锁02
  	GOOSE_RECV_TBL_GLBS03,  //过流闭锁03
  	GOOSE_RECV_TBL_GLBS04,  //过流闭锁04
  	GOOSE_RECV_TBL_GLBS05,  //过流闭锁05
  	GOOSE_RECV_TBL_GLBS06,  //过流闭锁06
	GOOSE_RECV_TBL_GLBS07,	//过流闭锁07
	GOOSE_RECV_TBL_GLBS08,	//过流闭锁08
	GOOSE_RECV_TBL_GLBS09,	//过流闭锁09
	GOOSE_RECV_TBL_GLBS10,	//过流闭锁10
	GOOSE_RECV_TBL_GLBS11,	//过流闭锁11
	GOOSE_RECV_TBL_GLBS12,	//过流闭锁12
#if defined GD_AREA_ZHONGSHAN || defined GD_AREA_ZHONGSHAN_2020  //中山要求通信异常，全系统均异常
  	GOOSE_RECV_TBL_STATUS01,  //节点通信异常状态
  	GOOSE_RECV_TBL_STATUS02,  //节点通信异常状态
  	GOOSE_RECV_TBL_STATUS03,  //节点通信异常状态
  	GOOSE_RECV_TBL_STATUS04,  //节点通信异常状态
  	GOOSE_RECV_TBL_STATUS05,  //节点通信异常状态
  	GOOSE_RECV_TBL_STATUS06,  //节点通信异常状态
  	GOOSE_RECV_TBL_STATUS07,  //节点通信异常状态
  	GOOSE_RECV_TBL_STATUS08,  //节点通信异常状态
  	GOOSE_RECV_TBL_STATUS09,  //节点通信异常状态
  	GOOSE_RECV_TBL_STATUS10,  //节点通信异常状态
  	GOOSE_RECV_TBL_STATUS11,  //节点通信异常状态
  	GOOSE_RECV_TBL_STATUS12,  //节点通信异常状态
	GOOSE_RECV_TBL_FG01,  //节点通信异常
	GOOSE_RECV_TBL_FG02,  //节点通信异常
	GOOSE_RECV_TBL_FG03,  //节点通信异常
	GOOSE_RECV_TBL_FG04,  //节点通信异常
	GOOSE_RECV_TBL_FG05,  //节点通信异常
	GOOSE_RECV_TBL_FG06,  //节点通信异常
	GOOSE_RECV_TBL_FG07,  //节点通信异常
	GOOSE_RECV_TBL_FG08,  //节点通信异常
	GOOSE_RECV_TBL_FG09,  //节点通信异常
	GOOSE_RECV_TBL_FG10,  //节点通信异常
	GOOSE_RECV_TBL_FG11,  //节点通信异常
	GOOSE_RECV_TBL_FG12,  //节点通信异常	
#endif
	GOOSE_RECV_TBL_NUMBER,  // 接收数据个数
};

#define GOOSE_RECV_LOGIC_NUM GOOSE_RECV_TBL_GLBS12 // 参与goose逻辑的虚遥信
struct gse_tbl
{
	char * 	name;
	char * 	sAddr;
};

// 保护动作跳闸标志
typedef union UGOOSE_STU
{
	DWORD  wfFlag;
	struct
	{
		DWORD	blxbs:1;		//零序闭锁
		DWORD	bglbs:1;		//过流闭锁
		DWORD	btw:1;		//跳位	
		DWORD   bfx:1;		//    节点反方向故障bit 4
		DWORD   bzx:1;		//    节点正方向故障bit 3
		DWORD   blx:1;		//   节点零序故障 bit 5
		DWORD   btzsb:1;		//    开关拒动 bit 2
		DWORD   bgzglok:1;		// 隔离ok    bit 1
		DWORD   bgl:1;		//    节点故障 bit 0
		DWORD   bcomerr:1;  //通信异常
		DWORD   bTstgz:1;
		DWORD   bTstglok:1;
		DWORD   bTstjt:1;
		DWORD   bTstglbs:1;
		DWORD   bTstcomerr:1;

		
	}bFlag;
}UGOOSE_STU;

typedef struct FA_GOOSE
{
	UGOOSE_STU ug;
	DWORD ug_save;
	UGOOSE_STU ugside[GOOSE_EQU_MAX];

	DWORD   dTrecv[GOOSE_RECV_GOLBNUM];
	int           recvgolbnum;

	/*同61850模块交互的数据*/
	int outval[GOOSE_SEND_TBL_NUMBER];
	int inval[GOOSE_RECV_TBL_NUMBER];
	bool bSend;
	int  commstate;	//goose 模块发送的通信状态==0 通信正常 ==1 通信异常
	int commerr;
	int syscomerr;
	bool bComErr;		//保护用
	bool bNside;
	TIMERELAY	tRstTime;	// goose 信息延时复归
	TIMERELAY	tGooseStillTime;	// goose 投入延迟复归	
#if defined GD_AREA_ZHONGSHAN || defined GD_AREA_ZHONGSHAN_2020
	u32  commerrst; //综合本机后的状态
	u32  commerrfg;
	u32  commerrstsave; // 和本机状态比较，本机状态大于保存值，需发送，说明有新的中断信息
	u32  commerrfgsave;
	TIMERELAY	tErrfgtime;	// 发送复归后，复归值清零时间继电器
#endif
}FA_GOOSE;



typedef union UGOCDIR
{
    DWORD  wfFlag;
    struct
    {
   	DWORD	bYY1:1;		  
	DWORD	bYY2:1;	   
	DWORD	bWY1:1;		  
	DWORD	bWY2:1;	   
                                              
        DWORD    bZXA:1;    //A相正方向动作	     
        DWORD    bZXB:1;    //B相正方向动作	     
        DWORD    bZXC:1;    //C相正方向动作
	 DWORD    bZX:1;		//线路正向故障

        
        DWORD    bFXA:1;    //A反方向动作
        DWORD    bFXB:1;    //B相反方向动作	     
        DWORD    bFXC:1;    //C相反方向动作	
	 DWORD     bFX:1;		//线路反向故障
        
        DWORD    bWXA:1;    //A相无方向动作	     
        DWORD    bWXB:1;    //B相无方向动作
        DWORD    bWXC:1;    //C相无方向动作
	 DWORD    bWX:1;		//线路无方向故障
        
        DWORD    bQDA:1;    // A相启动   
        DWORD    bQDB:1;    // B相启动        
        DWORD    bQDC:1;    // C相启动   
        DWORD    bQDLX:1;
        
	 DWORD    bLX:1;           //线路零序故障
	 DWORD    bGZ:1;		//线路有故障
	 DWORD    bGLBS:1;
	 DWORD    bCYY:1;

	 DWORD    bgzglok:1;
	 DWORD    btzsb:1;
	 DWORD    bcomerr:1;	 
	 DWORD    bcomerrbak:1;	 

	 DWORD	 bllWY1:1;	   
	 DWORD	 bllWY2:1;	
        
   }bFlag;
}UGOCDIR;     



typedef union UGOCFLAG
{
    DWORD  wfFlag;
    struct
    {
		DWORD  bglQD:1;
		DWORD  bglTz:1;		
		DWORD  bglTzH:1;	  //跳闸保持信号
		DWORD  bglQDWave:1;

#if defined GD_AREA_ZHONGSHAN || defined GD_AREA_ZHONGSHAN_2020 || defined GD_AREA_MAIN_2020
		DWORD  bgl2QD:1;
		DWORD  bgl2Tz:1;		
		DWORD  bgl2TzH:1;	  //跳闸保持信号
		DWORD  bgl2QDWave:1;
#endif	
		
		DWORD  blxQD:1;
		DWORD  blxTz:1;
		DWORD  blxTzH:1;
		DWORD  blxQDWave:1;

		DWORD  bgzglQD:1;
		DWORD  bgzglTz:1;  //故障隔离跳闸
		DWORD  bgzglTzH:1;  //故障隔离跳闸
		DWORD  bextQD:1;
		
		DWORD	bextTz:1;
		DWORD	bextTzH:1;
		DWORD	bsdQD:1;		   //首端失压 故障隔离
		DWORD	bsdTz:1;		  	 // 跳闸	
		
		DWORD	bsdTzH:1;		  	 // 跳闸	
		DWORD	berrQD:1;		   //通信异常失压 故障隔离
		DWORD	berrTz:1;		   // 跳闸	
		DWORD	berrTzH:1;		   // 跳闸	

		DWORD	bllQD:1 ;			// 联络合闸启动
		DWORD	bllWY_1:1 ;			// 联络单侧失压
		DWORD   bllhz:1; 			//联络合闸动作
 		DWORD   bllhzH:1; 			//联络合闸动作
 		
 		DWORD   bExtbs:1;
   		DWORD  bXBbs:1;
   		DWORD  bXBbs2:1;  //过流2谐波闭锁
    
   }bFlag;
}UGOCFLAG;     

typedef union UCDFLAG
{
    WORD  wfFlag;
    struct
    {
                                                 
	WORD	bQD:1;	
	WORD	bCD:1; 	  
	WORD	bFD:1;       
   }bFlag;
}UCDFLAG;     


typedef struct FA_CD
{
	UCDFLAG sta;
 	TIMERELAY	tCDTime;  		// 充电时间
  	TIMERELAY	tWYTime;  		// 放电失压时间
}FA_CD;

enum
{
	FAG_GL1=0, 	// 过流1段
	FAG_GL2,		// 过流2段
	FAG_LX,		// 零序
	FAG_BHNUMBER
};



typedef struct  TGOC_T
{
	UGOCDIR        psta;			// 公用动作信息
//#if defined GD_AREA_ZHONGSHAN || defined GD_AREA_ZHONGSHAN_2020
#if defined GD_AREA_ZHONGSHAN || defined GD_AREA_ZHONGSHAN_2020 || defined GD_AREA_MAIN_2020
	UGOCDIR        psta2;			// 过流2段用动作信息
#endif
	UGOCFLAG     sta;			// 故障动作信息

	TIMERELAY	tTZX_300ms;	// 正相方向判断保持2秒
	TIMERELAY	tTFX_300ms;	// 反相方向判断保持2秒
	TIMERELAY	tTWX_300ms;	// 无方向判断报纸2秒
	TIMERELAY	tTLX_300ms;	// 零序故障保持2秒
	TIMERELAY	tTGLBS_300ms;	// 零序故障保持2秒
	TIMERELAY	tTHz1s;	//  合闸位置保持1秒
	
	FA_CD 		tglcd;  //故障隔离充电
	FA_CD 		tllcd;	//故障联络充电
	FA_CD           tqccd; // 故障切除充电
	bool     		b_side_gz;
	bool     		b_side_tzsb;
	bool			bTzout;
	bool               bslowEn;  //缓动型条件满足
	bool               bgooseGz;
	bool               bgooseBsHz;

	bool			bextTzLed;  // 动作灯		
	bool			bglTzLed;  // 动作灯
	bool			blxTzLed;  // 动作灯
	bool			bsdTzLed;  // 动作灯
	bool			berrTzLed;  // 动作灯
	bool			bgzglTzLed;  // 故障隔离动作灯
	
	TIMERELAY	tglFz25ms;  		// 幅值启动25ms
	TIMERELAY 	tglQDTime; 		// 时间继电器
	TIMERELAY 	tglQD25ms; // 启动25ms,防干扰时间继电器,用于速断和零序加速，防止误出口

	TIMERELAY 	tglslowTime; 		// 时间继电器
	TIMERELAY 	tlxslowTime; 		// 时间继电器


//#if defined GD_AREA_ZHONGSHAN || defined GD_AREA_ZHONGSHAN_2020
#if defined GD_AREA_ZHONGSHAN || defined GD_AREA_ZHONGSHAN_2020 || defined GD_AREA_MAIN_2020

	TIMERELAY	tgl2Fz25ms;  		// 幅值启动25ms
	TIMERELAY 	tgl2QDTime; 		// 时间继电器
	TIMERELAY 	tgl2QD25ms; // 启动25ms,防干扰时间继电器,用于速断和零序加速，防止误出口
	TIMERELAY 	tgl2slowTime; 		// 时间继电器

	TIMERELAY	tTZX2_300ms; // 正相方向判断保持2秒
	TIMERELAY	tTFX2_300ms; // 反相方向判断保持2秒
	TIMERELAY	tTWX2_300ms; // 无方向判断报纸2秒
	
#endif

#ifdef GD_AREA_ZHONGSHAN_2020
		TOC_T tOc[FAG_BHNUMBER];  // 合闸加速跳 过流1
#endif
	TIMERELAY	tlxFz25ms;  		// 幅值启动25ms
	TIMERELAY 	tlxQDTime; 		// 时间继电器
	TIMERELAY 	tlxQD25ms; // 启动25ms,防干扰时间继电器,用于速断和零序加速，防止误出口

	TIMERELAY 	tgzglTime; 		// 故障隔离时间
	TIMERELAY 	tgoose15ms_gzgl; // 等待10ms goose 通信时间
	TIMERELAY 	tsdlostTime; 		// 首端失压时间	
	TIMERELAY 	terrlostTime; 		//通信异常失压时间
	
	TIMERELAY 	tllhzTime; 		//联络合闸时间
	TIMERELAY 	tllQdTime; 		//联络合闸时间
	TIMERELAY 	tgoosedelayms_ll; //联络 等待goose 通信时间

	TIMERELAY 	tllWY1Time; 		//联络合闸时间
	TIMERELAY 	tllWY2Time; 		//联络合闸时间

	TIMERELAY 	tllYY1Time; 		//联络有压时间
	TIMERELAY 	tllYY2Time; 		//联络有压时间
	

	TIMERELAY 	tTzcheckTime; 		//跳闸失败检查时间
	TIMERELAY 	tHzcheckTime; 		//跳闸失败检查时间

	TIMERELAY	tCYY3sdelayms;

	TIMERELAY	tgzglokextTime;
	TIMERELAY	ttzsbextTime;
	TIMERELAY	tgzglokForbitTime;  //故障隔离成功发送后，闭锁再次发送时间
	TIMERELAY	textbsTime;
	
}TGOC_T;

extern const struct gse_tbl g_goose_send_tbl[GOOSE_SEND_TBL_NUMBER];

extern const struct gse_tbl g_goose_recv_tbl[GOOSE_RECV_TBL_NUMBER]; 

extern FA_GOOSE tFAg;
extern int g_bgseEnable;
void fa_g_unpack(void);
void fa_g_packet(bool bfirst);
void fa_g_init(void);
void GOC_dir_check(int sw,u32  dStep);
void fag_oc_gl(int sw,u32  dStep);
void fag_oc_lx(int sw,u32  dStep);
void fa_g_app_time(int sw);
void fag_bh(int sw,DWORD dStep);
// sunxi 20190418 void goose_send_packet(u8 type,u8 *mac,u32 gData,struct timespec *pts,bool btick);
void XBCalc_goc(int sw);               // goose 谐波计算:处理标志:谐波闭锁动作		

void fa_g_decode(int commstate); //通信异常判断 
void fa_g_extinit(int num,int firstaddr); // app_goose.c中调用fa_g.c中的变量初始化

void fa_g_gocbrecvdT(int index); // app_goose.c中接收后调用，用于通信超时处理
#ifdef GD_AREA_ZHONGSHAN_2020
void fag_oc_acc(int sw,u32  dStep);  // 分布式合闸加速跳闸
#endif
#endif



 //========================== 本文件结束 ===============================
