#ifndef PROTOFECBUSCRT_H_
#define PROTOFECBUSCRT_H_

#include "gType.h"
#include "TDateTime.h"
#include "ProtoFecBus.h"
#include "record.h"
#include "Runtime.h"

#define dFecCRT_BufCount_Tx	10240
#define dFecCRT_BufEntrySize_Tx	96

#define FECBUS_HEAD 	0x7E
#define FECBUS_END 	0x7E

typedef union{
	unsigned short D16;
	unsigned char D8[2];
	struct{
		unsigned char LoByte;
		unsigned char HiByte;
	}body;
}TCrcValue;

typedef union{
	unsigned int D32[24];
	unsigned char D8[96];
	struct{
		unsigned int avalibel;
		unsigned int UsingMsgIndex;
		unsigned int FrameNum;
		unsigned int FrameTick;
		union{
			unsigned int da32[5];
			unsigned char da8[20];
			struct{
				unsigned char Header;
				unsigned char FrmType;
				unsigned char DestAddr;
				unsigned char Priority;
				unsigned char SrcAddr;
				unsigned char MsgIndex;
				unsigned char FrmIndex;
				unsigned char Dlen;
				unsigned char DPFC[8];	//Data PlusIn Function Code
				unsigned char CrcLoByte;
				unsigned char CrcHiByte;
				unsigned char Eop;
				unsigned char rsv;
			}body;
		}Section[4];
	}Prm;
}TFecBusMsgUart;

class TFecBusCRT{
	public:
		unsigned int SendMaskFlag;
	
		TFecEventBufBody FecBusCRTBufTemp;
		TFecBusMsgUart FecBusMsgUartTemp;
	
		unsigned int RxGetFlag;
		TFecBusMsgUart RxMsg;
		TFecAckCode RxMsgState;
	
		unsigned int TxSetFlag;
		TFecBusMsgUart TxMsg;
	
		unsigned int InTxIndex;
		unsigned int OutTxIndex;
		//TFecBusMsgUart TxCRTEntryMsg;
	
		struct{
			unsigned char State;
			unsigned char AckOk;
			unsigned char OnGoing;
			unsigned char Order;
			unsigned char RetryTimes;
			unsigned char RetryFLAG;
			unsigned short TimeOutTick;
		}OnTransmite;
		
		union{
			unsigned int D32;
			struct{
				unsigned char OffLine;
				unsigned char ReadWriteFail;
				unsigned char OffLineTick;
				unsigned char Rsv0;
			}Body;
		}ErrState, ErrReport;
		
		unsigned int OnlineTick;
		
		struct{
			unsigned int Active;
			unsigned int InxFire;
			unsigned int InxStart;
			unsigned int InxSv;
			unsigned int InxFault;
			unsigned int InxMask;
			TRecordCommon aRc;
		}Upload;
		
		//unsigned int CrtActive;
		//unsigned char LineType;
		//unsigned char CrtNum;
		
		TEventMsg EvMsg;
	
	public:
		TFecBusCRT(){};
		void Init();
		
		void ResetCallBack();
			
		void MsgFrmIndexInc();
			
		unsigned short Crc16Data(unsigned char* pdata,unsigned int len);
		unsigned int CheckCrc16(unsigned char* pdata,unsigned int len);	
		
		void getFecBusCRTTime(TRecordCommon aRC);
		void getFecBusCRTStateCode(unsigned short StateCode);
		void setFecBusCRTStateCode(unsigned short StateCode);
		
		void SetStateCodeAll();
		void SetStateCodeAutoManual();
		void SetStateCodePsOnActive();
		void SetStateCodePsError();
		void SetStateCodeFire();
		void SetStateCodeStart();
		void SetStateCodeFeedBack();
		void SetStateCodeSv();	
		void SetStateCodeFault();	
		void SetStateCodeMask();			
			
		void getFecBusCRTDeviceCode(unsigned char dType);
			
		void getFecBusCRTEpFaultEvent(unsigned char aType, unsigned char State);
			
		void getFecBusCRTFaultBrdDeviceCode(unsigned char aType);
		void getFecBusCRTFaultBrdEventCode(unsigned char aType, unsigned char State);
		void getFecBusCRTFaultDevDeviceCode(unsigned char aType);
		void getFecBusCRTFaultDevEventCode(unsigned char aType, unsigned char State);
			
		void oBufInsert();
		void iBufInsert();
		void iBufProcess();
		void oBufProcess();
			
		void iFecBusBufInsert(unsigned char * pdata);
		void iFecBusMsgInsert(TFecBusMsgUart * pMsg);
		
		void iSetFecBusBufEnd(TFecBusMsgUart * pMsg,unsigned char Priority);
		void iSetFecBusMsg00(TFecBusMsgUart * pMsg);
		void iSetFecBusMsg01(TFecBusMsgUart * pMsg, unsigned char aCtlNum);
		void iSetFecBusMsg02(TFecBusMsgUart * pMsg, unsigned char aCtlNum);
		void iSetFecBusMsg03(TFecBusMsgUart * pMsg, unsigned char aCtlNum);
		void iSetFecBusMsg05(TFecBusMsgUart * pMsg, TFecEventBufBody * pEvent);
		void iSetFecBusMsg06(TFecBusMsgUart * pMsg, TFecEventBufBody * pEvent);
		void iSetFecBusMsg15(TFecBusMsgUart * pMsg);
		
		void setFecBusCRTMsg01(unsigned char aCtlNum);
		void setFecBusCRTMsg02(unsigned char aCtlNum);
		void setFecBusCRTMsg03(unsigned char aCtlNum);
		void rpyFecBusCRTMsg15(void);
		
		//interface
		void iFecBusCRTFireInsert(TRecordCommon aRC);
		void iFecBusCRTFaultInsert(TRecordCommon aRC);
		
		void iFecBusCRTLinkageInsert(TRecordCommon aRC);
		void iFecBusCRTOperateInsert(TRecordCommon aRC);
		void iFecBusCRTSvInsert(TRecordCommon aRC);
		//---<
		void ShipMsg();
		
		void oCmdInsert();
		
		void Upload2CrtTask(void);
		void UploadMask2CrtTask(void);
		
		void Task10Ms();
		void Task1000Ms();
};

#endif









