#ifndef PROTOCRT_H_
#define PROTOCRT_H_

#include "gType.h"
#include "ProtoInnerCan.h"
#include "TDateTime.h"
#include "record.h"

#define dCRT_CMD_OPA	201
#define dCRT_CMD_ACK	202

#define dCRT_BufLen_Tx	1024
#define dCRT_BufLen_Rx	4096

#define dCRT_BufEntrySize_Tx	32*3
#define dCRT_BufEntrySize_Rx	32

typedef union{
	unsigned int D32[8];
	struct{
		unsigned char Cmd;	//Cmd 
		unsigned char MsgType;	// 0:Fire, 1:Linkage, 2:Sv, 3:Fault, 4:Mask,  5:Operate
		unsigned char Ack;	 
		unsigned char OpaCode;	
		
		TRecordCommon Msg;
	}Splite;
}TCrtMsg;


class TProtoCrt{
	public:
		volatile struct{
			int Count;
			unsigned int WriteIndex;
			unsigned int ReadIndex;
			TCrtMsg wBody;
			TCrtMsg rBody;
		}iBuf, oBuf;
		
		TRecordCommon TxOperateBody;
		
		union {
			unsigned int D32[4];
			unsigned char D8[16];
		}tData;
		
		struct{
			unsigned char State;
			unsigned char OnGoing;
			unsigned char CrtAckState;
			unsigned int WaitTick;
			unsigned int NoCommTick;
			unsigned int RetryTimes;
		}OnTransmite;
		
		struct{
			unsigned char TransferBoardOffline;
			unsigned char CrtOffline;
		}Err, ErrReport;
	
	public:
		TProtoCrt(){};
		void Init();
		void dInit();
		
		void PublishEventMute();
		void PublishEventReset();
		
		void iBufInsertDump();
		void iBufInsert();	
		void iBufInsert(unsigned int *pD);
		void iBufInsert(TRecordCommon aNsBuf);
		void iBufProcess();
			
		void PublishOperate(TRecordCommon aIm);
			
		void oBufInsert();
		void oBufInsert(unsigned int *pD);
		void oBufInsert(unsigned char aCmd, unsigned char aMsgType, TRecordCommon aBufBody);
		void oBufTryTxNewMsg();
		void oBufProcess();
			
		void PrcErrMsg();
		void DoCrtReqOperate();
	
		void ResetCoomm();
		void Task10Ms();
};


























#endif


