#ifndef INTERLINK_H_
#define INTERLINK_H_

#include "gvalue.h"
#include "my_string.h"
#include "record.h"
#include "ProtoC2C.h"

#define dLNKNONE		0x00
#define dLNKMATCH		0x11
#define dLNKDISMATCH	0x10
#define dLNKSTART		0x21
#define dLNKSTOP		0x20

#define dSdAddrLinkageExp		0xC040'0000

#define dLinkageOutputBufMaxCOunt	2048

#define dStart			1
#define dMSearchTarget	2
#define dMSearchSource	3
#define dMSearchPath    4
#define dSDearchPathStart	5
#define dSDearchPathEnd		6
#define dSearchLogic		7

#define dToFindStart	99
#define dFoundStart		100
#define dFoundLogic		102

#define dLevelCount		6
#define dLevelEntryCount	24

typedef struct{
	unsigned int IsMask;
	unsigned int HasInput;
	unsigned int IsAllFire;
	unsigned int Asterisk[8];
	unsigned int Hashtag[8];
	unsigned int nWildcard;
	unsigned int ParCnt;
	struct{
		unsigned int Sym;	//0 Direct; 1 Wildcard; 2 ByFloor
		TUserCodeStr BaseNum;
		unsigned int Logic;
		unsigned int dType;
		unsigned int Active;
		unsigned int Illegal;
		unsigned int ThresholdCnt;
		//unsigned char WildcardAva[8];
	}Entry[24];
}TLinkageRunTimeInput;

typedef struct{
	unsigned int Action;
	unsigned int DoStop;
	unsigned int Asterisk[8];
	unsigned int Hashtag[8];
	unsigned int nWildcard;
	struct{
		unsigned int Sym;	//0 Direct; 1 Wildcard; 2 ByFloor
		TUserCodeStr BaseNum;
		unsigned int DelaySec;
		unsigned int dType;
		unsigned int Active;
		unsigned int Illegal;
			//unsigned char WildcardAva[8];
	}Entry[24];
}TLinkageRunTimeOutput;

typedef struct{
	int Count;
	int WriteIndex;
	int ReadIndex;
	union{
		unsigned int D32;
		struct{
			unsigned char CtlNum;
			unsigned char P0;
			unsigned char P1;
			unsigned char Action;
		}Spl;
	}Body[dLinkageOutputBufMaxCOunt];
}TOutputBuf;

typedef struct{
	int Count;
	int WriteIndex;
	int ReadIndex;
	union{
		unsigned int D32;
		struct{
			unsigned char CtlNum;
			unsigned char P0;
			unsigned char P1;
			unsigned char Action;
		}Spl;
	}Body[dLinkageOutputBufMaxCOunt];
}TExtOutputBuf;


class TInterLink{
	public:
		TLinkageRunTimeInput eInput;
		TLinkageRunTimeOutput eOutput;
		unsigned int Crc[dLINKAGE_MAX_COUNT];
		unsigned char CrcFlag[dLINKAGE_MAX_COUNT];
		
		union{
			unsigned int D32[256];
			char Exp[1024];
		}Exp;
		int ShouldGo;
		int AllowOld;
		int LinkIndex;
		int LinkCount;
		int LinkCurrent;
		unsigned short FireInxPrv;
		unsigned short PrcFireInx[dLINKAGE_MAX_COUNT];
		unsigned short PrcFireOutputInx[dLINKAGE_MAX_COUNT];
		unsigned short LinkNumList[dLINKAGE_MAX_COUNT];
		unsigned short CrcCheckInx[dLINKAGE_MAX_COUNT];
		unsigned char LinkHasRdyGoTabel[dLINKAGE_MAX_COUNT];
		unsigned char LinkHasStartTabel[dLINKAGE_MAX_COUNT];
		unsigned char LinkForceGoTabel[dLINKAGE_MAX_COUNT];
		unsigned char LinkMaskTable[dLINKAGE_MAX_COUNT];

		int ForceDoOutput;
		int FirstDoOutput;
		int NewMatchDoOutput;
		int NewMatchFound;
		int ForceInx;
		int Forcing;
		
		int DoEntryTickDiff;
		int DoEbtryLastTick;;
		int DoEntryPerSecond;
		int DoEntryShouldRestart;
		
		char StrTmp[12];

		int LinkAva;
		int DoTick;
		
		unsigned char dType;
		
		union{
			unsigned int D32[2];
			unsigned char D8[8];
		}Data;
		unsigned char eData[16];
		union{
			unsigned char D8[256];
			unsigned int  D32[64];
		}ReadWriteData;
		
		TOutputBuf oBuf;
		
		TRecordCommon aIm;
		TC2CBufBody	aC2cNetBuf;
		
		union {
			unsigned int EntryInt32[5];
			char Str[20];
		}EntryChar;
	private:
		
	
	public:
		TInterLink(void){for(int i=0; i<dLINKAGE_MAX_COUNT; i++ )CrcFlag[i] =0;	}
		void Init(void);
		
		void ClearStrTmp();
		void ClearState();
		void ClearRuntime();
		void TryGetTargetPath();
		unsigned int TryGetDelaySec();
		void TryGetSourcePath();
		int LoadLinkageExp();
		void StartAnaly(char *p);
		void GoAction();
		void GetTargetPath(char *p);
		
		void WaitChipIdle(unsigned int aCnt);
		void LoadExpCount();
		int RuntimeReloadExpression2Ram(unsigned int aInx);
		void LoadExpression2Ram();
		void ReScanExpViaRam();
		
		void ResetByClearKey();
		
		void FmlTransmite(unsigned int FmlIndex, unsigned int PortNum);
		void FmlLoad(unsigned int Index, unsigned char *p, unsigned int length);
		void FmlSave(unsigned int Index, unsigned char *p, unsigned int length);
		void FmlReceive(unsigned char *p, unsigned int length);

		void LinkTransmite(unsigned int Index, unsigned int PortNum);
		void LinkLoad(unsigned int Index, unsigned char *p);
		void LinkSave(unsigned int Index, unsigned char *p);
		void LinkReceive(unsigned char *p);
		
		unsigned int GetNwLinkInput(unsigned char aCtlNum, unsigned char aP0, unsigned char aP1);
		
		void ForceLoadLinkageExp();
		
		void FmlAnalysis(void);
		void Inspect(void);
		
		void ReadRecord(unsigned int aIndex);
		void SaveRecord(unsigned int aIndex);
		void ProcessCountDown(void);
		
		void BusPadCheckStart();
		void DoLnk();
		
		void SendOutputBuf();
		///void SendExtOutputBuf();
		
		void LoadInputState();
		void SetOutputState();
		
		void GetFireStateByTypeViaMsg(unsigned char aDType, unsigned int aEty);
		int GetFireStateViaPath(unsigned int aCode, unsigned char aDType);
		int GetFireStateViaMsg(TUserCodeStr aCode, unsigned char aDType, unsigned int aEty);
		int SetOutputStateViaUserCode(TUserCodeStr aCode, unsigned char aDType,  unsigned int aEty, unsigned int aDelaySec, unsigned int aDoStop);
		//void PrcWildCardInput(unsigned int aEty);
		//void PrcWildCardOutput(unsigned int aEty);
		void LoadOneEntryInput(unsigned int aEty);
		void CheckGoRunLogic();
		void ActiveEntryOutput(unsigned int aEty);
		void CanNetMasterSendOutput(TUserCodeStr aCode, unsigned char aDType, unsigned int aDelaySec, unsigned int aDoStop);
		void CanNetMasterSendOutputSimulate(TUserCodeStr aCode, unsigned char aDType, unsigned int aDelaySec, unsigned int aDoStop);
		
		void LoadExpSettingBytes(unsigned int aInx);
		int LoadExp(unsigned int aInx);
		int QueueLoadExp();
		
		int SetOutputStateViaUserCodeSimulate(TUserCodeStr aCode, unsigned char aDType, unsigned int aEty, unsigned int aDelaySec, unsigned int aDoStop);
		
		int CheckEpHwTypeCanOutput(unsigned char aType);
		
		void CheckRamCrc(unsigned short aInx);
		void CheckStorageCrc(unsigned short aInx);
		void CheckStorageCrc();
		void RecallCrcFaultMsg();
			
		void Task1Ms();
		void Task10Ms();

};



#endif


