#include "ProtoInnerCan.h"
#include "ProtoC2C.h"

#include "CnCpp.h"
#include "record.h"
#include "Runtime.h"
#include "server.h"

extern "C"{
	#include "uart_key_drv.h"
	#include "uart_linkage_drv.h"
}

void TProtoC2C::MasterDoEpMsg(unsigned char aCmd, unsigned char *Data)
{
	int i;
	unsigned char aCn, aP0, aP1;
	
	aCn = Data[1];
	aP0 = Data[2];
	aP1 = Data[3];
	
	if( (aCn == 0) || (aCn >dNETWORK_MACHINE_COUNT) )return;
	if( (aP0 <1) || (aP0 >dPORT_MAX_COUNT) )return;
	if( (aP1 <1) || (aP1 >dEP_MAX_COUNT_PER_PORT) )return;
	
	if(aCmd == cmAutoStart){
		sIm.Splite.mType = etLinkageStart;
		sIm.Splite.iByte0 = 0x01;
		if(CtlCenter.IsMsgReseting == 0){
			CtlCenter.iePushOneMsgLinkageStart(sIm);
			ExRecord.SetStateCodeStart();
			FecBusCRT.SetStateCodeStart();
			Record.LinkageAdd(sIm);
		}
	}else
	if(aCmd == cmAutoStop){
		sIm.Splite.mType = etLinkageStart;
		//sIm.Splite.iByte0 = 0x09;	//make Msg Show On eventshow
		if(CtlCenter.IsMsgReseting == 0){
			//CtlCenter.iePushOneMsgLinkageStart(sIm);	//make Msg Show On eventshow
			CtlCenter.iePushOneMsgLinkageStartCancel(sIm);
			ExRecord.SetStateCodeStart();
			FecBusCRT.SetStateCodeStart();
			sIm.Splite.iByte0 = 0xB1;
			Record.LinkageAdd(sIm);
		}
	}else
	if(aCmd == dNetworkCmd_AutoStartDelay){
		sIm.Splite.mType = etLinkageStart;
		sIm.Splite.iByte0 = 0x0A;
		unsigned short aDl;
		aDl = RxMsg.Data.CarryByte;
		aDl &= 0x000F;
		aDl <<= 8;
		aDl |= RxMsg.Data.Data[4];
		sSetNwDevDelayStartPoint(RxMsg.Data.Data[1], RxMsg.Data.Data[2], RxMsg.Data.Data[3], aDl);
		if(CtlCenter.IsMsgReseting == 0){
			CtlCenter.iePushOneMsgLinkageStart(sIm);
		}
	}else
	if(aCmd == cmAck){
		sIm.Splite.mType = etLinkageFeedBack;
		sIm.Splite.iByte0 = 0x03;
		if(CtlCenter.IsMsgReseting == 0){
			Record.LinkageAdd(sIm);
			CtlCenter.iePushOneMsgLinkageFb(sIm);
			ExRecord.SetStateCodeStart();
			FecBusCRT.SetStateCodeStart();
		}
	}else
	if(aCmd == cmAckStop){
		sIm.Splite.mType = etLinkageFeedBack;
		sIm.Splite.iByte0 = 0xB3;
		if(CtlCenter.IsMsgReseting == 0){
			Record.LinkageAdd(sIm);
			CtlCenter.iePushOneMsgLinkageFbCancel(sIm);
			ExRecord.SetStateCodeStart();
			FecBusCRT.SetStateCodeStart();
		}
	}		
}

void TProtoC2C::MasterDoDirectPadNwPutLnkState(unsigned char aCmd, unsigned char *Data)
{
	int i;
	unsigned char aCn, aP0, aNode;
	
	aCn = Data[1];
	aP0 = Data[2];
	aNode = Data[3];
	
	if( (aCn == 0) || (aCn >dNETWORK_MACHINE_COUNT) )return;
	if( (aP0 <dADDR_DIRECTPAD_START) || (aP0 >dADDR_DIRECTPAD_END) )return;
	if( (aNode <1) || (aNode >dDIRECTCTL_KEY_MAX_COUNT) )return;
	
	sIm.Splite.Year = ProtoC2C.RecDateTime[Data[1]].Dt.Year;
	sIm.Splite.Month = ProtoC2C.RecDateTime[Data[1]].Dt.Mon;
	sIm.Splite.Day = ProtoC2C.RecDateTime[Data[1]].Dt.Day;
	sIm.Splite.Hour = ProtoC2C.RecDateTime[Data[1]].Dt.Hour;
	sIm.Splite.Minute = ProtoC2C.RecDateTime[Data[1]].Dt.Min;
	sIm.Splite.Second = ProtoC2C.RecDateTime[Data[1]].Dt.Sec;
	
	sIm.Splite.PCtlNum = Data[1];
	sIm.Splite.P0 = Data[2];
	sIm.Splite.P1 = Data[3];
	sIm.Splite.P2 = 0;
	sIm.Splite.dType = Data[5];
	
	if(aCmd == cmAutoStart){
		sIm.Splite.mType = etLinkageStart;
		sIm.Splite.iByte0 = 0x01;
		if(CtlCenter.IsMsgReseting == 0){
			CtlCenter.iePushOneMsgLinkageStart(sIm);
			ExRecord.SetStateCodeStart();
			FecBusCRT.SetStateCodeStart();
			Record.LinkageAdd(sIm);
		}
	}else
	if(aCmd == cmAutoStop){
		sIm.Splite.mType = etLinkageStart;
		sIm.Splite.iByte0 = 0x09;
		if(CtlCenter.IsMsgReseting == 0){
			CtlCenter.iePushOneMsgLinkageStart(sIm);
			ExRecord.SetStateCodeStart();
			FecBusCRT.SetStateCodeStart();
			Record.LinkageAdd(sIm);
		}
	}else
	if(aCmd == dNetworkCmd_AutoStartDelay){
		sIm.Splite.mType = etLinkageStart;
		sIm.Splite.iByte0 = 0x0A;
		unsigned short aDl;
		aDl = RxMsg.Data.CarryByte;
		aDl &= 0x000F;
		aDl <<= 8;
		aDl |= RxMsg.Data.Data[4];
		sSetNwDirectPadDelayStartPoint(RxMsg.Data.Data[1], RxMsg.Data.Data[2] - dADDR_DIRECTPAD_START + 1, RxMsg.Data.Data[3], aDl);
		if(CtlCenter.IsMsgReseting == 0){
			CtlCenter.iePushOneMsgLinkageStart(sIm);
		}
	}else
	if(aCmd == cmAck){
		sIm.Splite.mType = etLinkageFeedBack;
		sIm.Splite.iByte0 = 0x03;
		if(CtlCenter.IsMsgReseting == 0){
			Record.LinkageAdd(sIm);
			CtlCenter.iePushOneMsgLinkageFb(sIm);
			ExRecord.SetStateCodeStart();
			FecBusCRT.SetStateCodeStart();
		}
	}else
	if(aCmd == cmAckStop){
		sIm.Splite.mType = etLinkageFeedBack;
		sIm.Splite.iByte0 = 0xB3;
		if(CtlCenter.IsMsgReseting == 0){
			Record.LinkageAdd(sIm);
			CtlCenter.iePushOneMsgLinkageFbCancel(sIm);
			ExRecord.SetStateCodeStart();
			FecBusCRT.SetStateCodeStart();
		}
	}		
}

void TProtoC2C::MasterDoMaskAdd(int aInx)
{
	volatile unsigned int sAddr;
	sAddr = dAddrSdRam_MsgMask + aInx* 24;
	
	MaskMsg.Body.iByte0 = 0;
	MaskMsg.Body.State = 1;
	
	*(volatile unsigned int *)(sAddr + 0) = MaskMsg.D32[0];
	*(volatile unsigned int *)(sAddr + 4) = MaskMsg.D32[1];
	*(volatile unsigned int *)(sAddr + 8) = MaskMsg.D32[2];
	*(volatile unsigned int *)(sAddr + 12) = MaskMsg.D32[3];
	*(volatile unsigned int *)(sAddr + 16) = MaskMsg.D32[4];
	*(volatile unsigned int *)(sAddr + 20) = MaskMsg.D32[5];
}

void TProtoC2C::MasterDoMaskAdd(TRecordMaskMsg aMaskMsg, int aInx)
{
	volatile unsigned int sAddr;
	TRecordMaskMsg tMaskMsg;
	
	sAddr = dAddrSdRam_MsgMask + aInx* 24;
	
	tMaskMsg.D32[0] = aMaskMsg.D32[0]; 
	tMaskMsg.D32[1] = aMaskMsg.D32[1]; 
	tMaskMsg.D32[2] = aMaskMsg.D32[2]; 
	tMaskMsg.D32[3] = aMaskMsg.D32[3];
	tMaskMsg.D32[4] = aMaskMsg.D32[4];
	tMaskMsg.D32[5] = aMaskMsg.D32[5]; 
	
	tMaskMsg.Body.iByte0 = 0;
	tMaskMsg.Body.State = 1;
	
	*(volatile unsigned int *)(sAddr + 0) = tMaskMsg.D32[0];
	*(volatile unsigned int *)(sAddr + 4) = tMaskMsg.D32[1];
	*(volatile unsigned int *)(sAddr + 8) = tMaskMsg.D32[2];
	*(volatile unsigned int *)(sAddr + 12) = tMaskMsg.D32[3];
	*(volatile unsigned int *)(sAddr + 16) = tMaskMsg.D32[4];
	*(volatile unsigned int *)(sAddr + 20) = tMaskMsg.D32[5];
}

void TProtoC2C::MasterDoMaskListGet(unsigned char *Data)
{
	volatile unsigned int IsFound;
	unsigned int FirstNullIndex, Cnt;
	volatile int i;
	TDMix aAddr;
	
	if(  (Data[2] == 0) || (Data[2] >dPORT_MAX_COUNT) )return;
	if(  (Data[3] == 0) || (Data[3] >250) )return;
	
	aAddr.D8[0] = Data[1];
	aAddr.D8[1] = Data[2];
	aAddr.D8[2] = Data[3];
	aAddr.D8[3] = 0;
	
	IsFound = 0;
	if(1){
		for(i=0; i<CtlCenter.ieMsgMask.Count; i++){
			//To Find Dual, And Clear it
			GetMsgViaIndex(i);
			if( MaskMsg.D32[2] ==  aAddr.D32 ){
				IsFound = 1;
			}
		}
		if(IsFound == 0){
			MaskMsg.D32[0] = 0;
			MaskMsg.D32[1] = 0;
			MaskMsg.D32[2] = 0;
			MaskMsg.D32[3] = 0;
			MaskMsg.D32[4] = 0;
			MaskMsg.D32[5] = 0;
			MaskMsg.Body.PCtlNum = Data[1];
			MaskMsg.Body.P0 = Data[2];
			MaskMsg.Body.P1 = Data[3];
			
			MaskMsg.Body.Second = 	RecDateTime[Data[1]].Dt.Sec;
			MaskMsg.Body.Minute = 	RecDateTime[Data[1]].Dt.Min;
			MaskMsg.Body.Hour = 	RecDateTime[Data[1]].Dt.Hour;
			MaskMsg.Body.Day =  	RecDateTime[Data[1]].Dt.Day;
			MaskMsg.Body.Month =  	RecDateTime[Data[1]].Dt.Mon;
			MaskMsg.Body.Year =  	RecDateTime[Data[1]].Dt.Year;
			
			aAddr.D8[0] = RecUserCodeAreaNum[Data[1]].UC.Bytes[0];
			aAddr.D8[1] = Data[0];
			aAddr.D8[2] = Data[6];
			aAddr.D8[3] = Data[7];
			MaskMsg.Body.UserCode = aAddr.D32;
			
			MaskMsg.Body.IsMask = 0xA5;
			MaskMsg.Body.dType = Data[5];
			
			MasterDoMaskAdd(CtlCenter.ieMsgMask.Count);
			CtlCenter.ieMsgMask.Count++;
			CtlCenter.ieMsgMask.ReflashIndex++;
			CtlCenter.ieGot = 1;
			//EventShow.MsgList[4].Count = CtlCenter.ieMsgMask.Count;
			//EventShow.MsgList[4].NewCount = CtlCenter.ieMsgMask.Count;
			//EventShow.TryGetMaskData(2, 20240428);
		}
	}
	ExRecord.SetStateCodeMask();
	FecBusCRT.SetStateCodeMask();
}

void TProtoC2C::GetMsgViaIndex(int aInx)
{
	volatile unsigned int sAddr;
	if(aInx < CtlCenter.ieMsgMask.Count){
		sAddr = dAddrSdRam_MsgMask + aInx* 24;
		MaskMsg.D32[0] = *(volatile unsigned int *)(sAddr + 0);
		MaskMsg.D32[1] = *(volatile unsigned int *)(sAddr + 4);
		MaskMsg.D32[2] = *(volatile unsigned int *)(sAddr + 8);
		MaskMsg.D32[3] = *(volatile unsigned int *)(sAddr + 12);
		MaskMsg.D32[4] = *(volatile unsigned int *)(sAddr + 16);
		MaskMsg.D32[5] = *(volatile unsigned int *)(sAddr + 20);
	}
}

void TProtoC2C::ClearMsgViaIndex(int aInx)
{
	volatile unsigned int sAddr;
	if(aInx < CtlCenter.ieMsgMask.Count){
		sAddr = dAddrSdRam_MsgMask + aInx* 24;
		*(volatile unsigned int *)(sAddr + 0) = 0;
		*(volatile unsigned int *)(sAddr + 4) = 0;
		*(volatile unsigned int *)(sAddr + 8) = 0;
		*(volatile unsigned int *)(sAddr + 12) = 0;
		*(volatile unsigned int *)(sAddr + 16) = 0;
		*(volatile unsigned int *)(sAddr + 20) = 0;
	}
}

void TProtoC2C::MasterDoMaskListRemove(unsigned char aCtlNum)
{
	unsigned char P0, P1;
	volatile unsigned int sAddr, IsFound;
	unsigned int FirstNullIndex, Cnt;
	volatile int i;

	FirstNullIndex = ieMSG_MASK_MAX_COUNT +1000;
	Cnt = 0;
	for(i=0; i<CtlCenter.ieMsgMask.Count; i++){
		//To Find Dual, And Clear it
		GetMsgViaIndex(i);
		if(MaskMsg.D32[2] == 0){
			if(FirstNullIndex > ieMSG_MASK_MAX_COUNT)FirstNullIndex = i;
		}else
		if( MaskMsg.Body.PCtlNum == aCtlNum ){
				ClearMsgViaIndex(i);
				if(FirstNullIndex > ieMSG_MASK_MAX_COUNT){
					FirstNullIndex = i;
				}
				CtlCenter.ieMsgMask.ReflashIndex++;
				CtlCenter.ieGot = 1;
		}else{
			if(FirstNullIndex < ieMSG_MASK_MAX_COUNT){
				MasterDoMaskAdd(FirstNullIndex);
				ClearMsgViaIndex(i);
				FirstNullIndex++;
			}
			Cnt++;
		}
	}
	CtlCenter.ieMsgMask.Count = Cnt;
	ExRecord.SetStateCodeMask();
	FecBusCRT.SetStateCodeMask();
}

void TProtoC2C::MasterDoAddRemoveMask(unsigned char aCtlNum, unsigned char aP0, unsigned char aP1, unsigned char aType, unsigned int aUc, unsigned int IsAdd)
{
	volatile unsigned int IsFound;
	unsigned int FirstNullIndex, Cnt;
	volatile int i;
	TDMix aAddr;
	
	if(  (aP0 == 0) || (aP0 >dPORT_MAX_COUNT) )return;
	if(  (aP1 == 0) || (aP1 >250) )return;
	
	aAddr.D8[0] = aCtlNum;
	aAddr.D8[1] = aP0;
	aAddr.D8[2] = aP1;
	aAddr.D8[3] = 0;
	
	IsFound = 0;
	if(IsAdd){
		for(i=0; i<CtlCenter.ieMsgMask.Count; i++){
			//To Find Dual, And Clear it
			GetMsgViaIndex(i);
			if( MaskMsg.D32[2] ==  aAddr.D32 ){
				IsFound = 1;
			}
		}
		if(IsFound == 0){
			MaskMsg.D32[0] = 0;
			MaskMsg.D32[1] = 0;
			MaskMsg.D32[2] = 0;
			MaskMsg.D32[3] = 0;
			MaskMsg.Body.PCtlNum = aCtlNum;
			MaskMsg.Body.P0 = aP0;
			MaskMsg.Body.P1 = aP1;
			
			MaskMsg.Body.Second = 	RecDateTime[aCtlNum].Dt.Sec;
			MaskMsg.Body.Minute = 	RecDateTime[aCtlNum].Dt.Min;
			MaskMsg.Body.Hour = 	RecDateTime[aCtlNum].Dt.Hour;
			MaskMsg.Body.Day =  	RecDateTime[aCtlNum].Dt.Day;
			MaskMsg.Body.Month =  	RecDateTime[aCtlNum].Dt.Mon;
			MaskMsg.Body.Year =  	RecDateTime[aCtlNum].Dt.Year;
			
			MaskMsg.Body.UserCode = aUc;
			
			MaskMsg.Body.IsMask = 0xA5;
			MaskMsg.Body.dType = aType;
			
			MasterDoMaskAdd(CtlCenter.ieMsgMask.Count);
			CtlCenter.ieMsgMask.Count++;
			CtlCenter.ieMsgMask.ReflashIndex++;
			CtlCenter.ieGot = 1;
			//EventShow.MsgList[4].Count = CtlCenter.ieMsgMask.Count;
			//EventShow.MsgList[4].NewCount = CtlCenter.ieMsgMask.Count;
			//EventShow.TryGetMaskData(2, 20240428);
		}
	}else{
		FirstNullIndex = ieMSG_MASK_MAX_COUNT +1000;
		Cnt = 0;
		for(i=0; i<CtlCenter.ieMsgMask.Count; i++){
			//To Find Dual, And Clear it
			GetMsgViaIndex(i);
			if(MaskMsg.D32[2] == 0){
				if(FirstNullIndex > ieMSG_MASK_MAX_COUNT)FirstNullIndex = i;
			}else
			if( MaskMsg.D32[2] ==  aAddr.D32 ){
					ClearMsgViaIndex(i);
					if(FirstNullIndex > ieMSG_MASK_MAX_COUNT){
						FirstNullIndex = i;
					}
					CtlCenter.ieMsgMask.ReflashIndex++;
					CtlCenter.ieGot = 1;
			}else{
				if(FirstNullIndex < ieMSG_MASK_MAX_COUNT){
					MasterDoMaskAdd(FirstNullIndex);
					ClearMsgViaIndex(i);
					FirstNullIndex++;
				}
				Cnt++;
			}
		}
		CtlCenter.ieMsgMask.Count = Cnt;
		//EventShow.MsgList[4].Count = CtlCenter.ieMsgMask.Count;
		//EventShow.MsgList[4].NewCount = CtlCenter.ieMsgMask.Count;
		//EventShow.TryGetMaskData(2, 20240428);
	}
	ExRecord.SetStateCodeMask();
	FecBusCRT.SetStateCodeMask();
}

void TProtoC2C::MasterDoCheckOnlineTask()
{
	int i, Num, IsLost;

	CheckOnlineTick++;
	if(CheckOnlineHasSend)CheckOnlineAfterSendTick++;
	if(CheckOnlineTick > 4000){
		CheckOnlineTick =0;
		if( (CheckOnlineHasStart) && (CheckOnlineAfterSendTick > 2000) ){
			IsLost = 0;
			for(i=1; i<=dNETWORK_MACHINE_COUNT; i++){
				if(SlaveOnlineList[i] == 0){
					IsLost = 1;
					if( ReportSlaveOffline[i] ==0){
						AddMasterCommFault(i);
						ReportSlaveOffline[i] = 1;
					}
				}else{
					if( ReportSlaveOffline[i]){
						RemoveMasterCommFault(i);
						ReportSlaveOffline[i] = 0;
					}
				}
			}
			
			if(IsLost || ErrReport){
				if(ErrHasReport == 0){
					ErrHasReport = 1;
					ErrReport = 0;
					//AddRemoveNwCommFault(MainCtl.fData.Split.MyNum, 1);
				}
			}else{
				if(ErrHasReport){
					ErrHasReport = 0;
					//AddRemoveNwCommFault(MainCtl.fData.Split.MyNum, 0);
				}
			}
		}else{
			//Network Is Busy
		}
		
		for(i=1; i<=dNETWORK_MACHINE_COUNT; i++){
			SlaveOnlineList[i] = 1;
		}
		for(i=0; i<SlaveCount; i++){
			Num = SlaveList[i];
			if(Num <= dNETWORK_MACHINE_COUNT)SlaveOnlineList[Num] = 0;
		}
		
		if(1){
			oEntry.wBody.Split.Cmd = dNetworkCmd_CheckOnline;
			oEntry.wBody.Split.CtlNum = MainCtl.fData.Split.MyNum;
			oEntry.wBody.Split.P0 = 0xA5;
			oEntry.wBody.Split.P1 = 0x5A;
			oBufMsgInsertBasicService();
			
			CheckOnlineAfterSendTick = 0;
			CheckOnlineHasSend = 0;
			CheckOnlineHasStart = 1;
		}
	}
}

void TProtoC2C::ClearCheckNormalDataSlave()
{
	int x,i;
	for(x=0;x<dNETWORK_MACHINE_COUNT;x++){
		CheckNormalDataSlaveGotPiece[x] =0;
		IsCheckNormalDataSlaveDone[x] =0;
		for(i=0; i<60; i++)CheckNormalDataSlave[x].D32[i] =0;
	}
}

void TProtoC2C::CheckNormalSlave(unsigned char aCtlNum)
{
	oEntry.wBody.Split.Cmd = dNetworkCmd_CheckNormalData;
	
	oEntry.wBody.Split.Year = 0;
	oEntry.wBody.Split.CtlNum = MainCtl.fData.Split.MyNum;
	oEntry.wBody.Split.P0 = aCtlNum;
	oEntry.wBody.Split.P1 = 0x5A;
			
	oBufMsgInsertNormal();
}

void TProtoC2C::MasterGetCheckNormalData(unsigned char *Data)
{
	int ix;
	if(Data[1] == 0)return;
	if(Data[1] > dNETWORK_MACHINE_COUNT)return;
	if( (Data[0]) && (Data[0] < 16)){
		ix = Data[0] * 6 -6; 
		CheckNormalDataSlave[Data[1]-1].D8[ix +0] = Data[2];
		CheckNormalDataSlave[Data[1]-1].D8[ix +1] = Data[3];
		CheckNormalDataSlave[Data[1]-1].D8[ix +2] = Data[4];
		CheckNormalDataSlave[Data[1]-1].D8[ix +3] = Data[5];
		CheckNormalDataSlave[Data[1]-1].D8[ix +4] = Data[6];
		CheckNormalDataSlave[Data[1]-1].D8[ix +5] = Data[7];
		CheckNormalDataSlaveGotPiece[Data[1]] |= 1<< Data[0];
	}
	if(CheckNormalDataSlaveGotPiece[Data[1]] == 0x0000FFFE)IsCheckNormalDataSlaveDone[Data[1]-1] = 1;
}

void TProtoC2C::MasterDoDevRemoteStart(unsigned char CtlNum, unsigned char aP0, unsigned char aP1, unsigned char IsSatrt)
{
	oEntry.wBody.Split.Cmd = dNetworkCmd_RemoteStartStop;
	
	oEntry.wBody.Split.Prm = CtlNum;
	oEntry.wBody.Split.CtlNum = MainCtl.fData.Split.MyNum;
	oEntry.wBody.Split.P0 = aP0;
	oEntry.wBody.Split.P1 = aP1;
	oEntry.wBody.Split.dType = IsSatrt;

	oBufMsgInsertLink();
}

void TProtoC2C::MasterDoRemoteLnkStartStopViaUserCode(unsigned int aUc, unsigned char aType, unsigned short aDelaySec, unsigned char aSta)
{
	oEntry.wBody.Split.Cmd = dNetworkCmd_RemoteStartStopViaUserCode;
	
	oEntry.wBody.Split.Prm = aSta;	//0:Off,1:Match,2:Start,3:DelayStart
	oEntry.wBody.Split.CtlNum = MainCtl.fData.Split.MyNum;
	oEntry.wBody.Split.UserCode = aUc;
	oEntry.wBody.Split.dType = aType;
	oEntry.wBody.Split.Prm1 = static_cast<unsigned char>(aDelaySec & 0x00FF);
	oEntry.wBody.Split.Prm2 = static_cast<unsigned char>( (aDelaySec >> 8) & 0x00FF);
	oBufMsgInsertLink();	
}

void TProtoC2C::MasterDoRemoteLnkStartStopViaUserCodeSimulate(unsigned int aUc, unsigned char aType, unsigned short aDelaySec, unsigned char aSta)
{
	oEntry.wBody.Split.Cmd = dNetworkCmd_RemoteStartStopViaUserCodeSimulate;
	
	oEntry.wBody.Split.Prm = aSta;	//0:Off,1:Match,2:Start,3:DelayStart
	oEntry.wBody.Split.CtlNum = MainCtl.fData.Split.MyNum;
	oEntry.wBody.Split.UserCode = aUc;
	oEntry.wBody.Split.dType = aType;
	oEntry.wBody.Split.Prm1 = static_cast<unsigned char>(aDelaySec & 0x00FF);
	oEntry.wBody.Split.Prm2 = static_cast<unsigned char>( (aDelaySec >> 8) & 0x00FF);
	oBufMsgInsertLink();	
}

void TProtoC2C::MasterDoPublishFire(unsigned char CtlNum, unsigned char aP0, unsigned char aP1, unsigned char aType, unsigned int aUc)
{
	if(MainCtl.fData.Split.NetworkMode != 1)return;
	oEntry.wBody.Split.Cmd = dNetworkCmd_PublishFire;
	
	oEntry.wBody.Split.Prm = CtlNum;
	oEntry.wBody.Split.CtlNum = MainCtl.fData.Split.MyNum;
	oEntry.wBody.Split.P0 = aP0;
	oEntry.wBody.Split.P1 = aP1;
	oEntry.wBody.Split.dType = aType;
	oEntry.wBody.Split.UserCode = aUc;
	oBufMsgInsertFire();
}

void TProtoC2C::MasterDoPublishFireCancel(unsigned char CtlNum, unsigned char aP0, unsigned char aP1, unsigned char aType, unsigned int aUc)
{
	oEntry.wBody.Split.Cmd = dNetworkCmd_PublishFireCancel;
	
	oEntry.wBody.Split.Prm = CtlNum;
	oEntry.wBody.Split.CtlNum = MainCtl.fData.Split.MyNum;
	oEntry.wBody.Split.P0 = aP0;
	oEntry.wBody.Split.P1 = aP1;
	oEntry.wBody.Split.dType = aType;
	oEntry.wBody.Split.UserCode = aUc;

	oBufMsgInsertFire();
}

void TProtoC2C::MasterDoPublishClearNwCtlFire(unsigned char CtlNum)
{
	oEntry.wBody.Split.Cmd = dNetworkCmd_ClearCtlFire;
	
	oEntry.wBody.Split.Prm = CtlNum;
	oEntry.wBody.Split.CtlNum = MainCtl.fData.Split.MyNum;
	oEntry.wBody.Split.P0 = 0;
	oEntry.wBody.Split.P1 = 0;
	oEntry.wBody.Split.dType = 0;

	oBufMsgInsertNormal();
}

void TProtoC2C::sSetNwDevDelayStartPoint(unsigned char aNum, unsigned char aP0, unsigned char aP1, unsigned int aCountDown)
{
	unsigned int aAddr;
	unsigned int NumOffSet, P0OffSet, P1OffSet;
	unsigned int TickZeroPoint;
	if(aNum > dNETWORK_MACHINE_COUNT)return;
	if(aP0 > dPORT_MAX_COUNT)return;
	
	NumOffSet = (static_cast<unsigned int>(aNum)) * 44'000;
	P0OffSet = (static_cast<unsigned int>(aP0)) * 1024;
	P1OffSet = (static_cast<unsigned int>(aP1)) * 4;
	
	aAddr = dAddrSdram_NwDelayStart + NumOffSet + P0OffSet + P1OffSet;
	if(aCountDown <= 600){
		TickZeroPoint = TProtoC2C::DevDelayStartTick + aCountDown;
	}else{
		TickZeroPoint = TProtoC2C::DevDelayStartTick + 600;
	}
	*(volatile unsigned int *)aAddr = TickZeroPoint;
}

unsigned int TProtoC2C::GetNwDevDelayCountDown(unsigned char aNum, unsigned char aP0, unsigned char aP1)
{
	unsigned int aAddr;
	unsigned int NumOffSet, P0OffSet, P1OffSet;
	unsigned int TickZeroPoint;
	unsigned int DelayCountDown;
	if(aNum > dNETWORK_MACHINE_COUNT)return 0;
	if(aP0 > dPORT_MAX_COUNT)return 0;
	
	NumOffSet = (static_cast<unsigned int>(aNum)) * 44'000;
	P0OffSet = (static_cast<unsigned int>(aP0)) * 1024;
	P1OffSet = (static_cast<unsigned int>(aP1)) * 4;
	
	aAddr = dAddrSdram_NwDelayStart + NumOffSet + P0OffSet + P1OffSet;
	TickZeroPoint = *(volatile unsigned int *)aAddr;
	DelayCountDown = TickZeroPoint - TProtoC2C::DevDelayStartTick;
	if(DelayCountDown > 600){
		DelayCountDown = 0;
	}
	return DelayCountDown;
}

void TProtoC2C::sSetNwDirectPadDelayStartPoint(unsigned int aNum, unsigned int aDirectPad, unsigned int aNode, unsigned int aCountDown)
{
	unsigned int aAddr;
	unsigned int NumOffSet, OffSet;
	unsigned int TickZeroPoint;
	if(aNum > dNETWORK_MACHINE_COUNT)return;
	if(aDirectPad > dDIRECTCTL_MAX_COUNT)return;
	if(aNode > dDIRECTCTL_KEY_MAX_COUNT)return;
	
	NumOffSet = aNum * 44'000 + 42'000;
	OffSet = aDirectPad * dDIRECTCTL_KEY_MAX_COUNT * 4 + (aNode *4 );
	
	aAddr = dAddrSdram_NwDelayStart + NumOffSet + OffSet;
	if(aCountDown <= 600){
		TickZeroPoint = TProtoC2C::DevDelayStartTick + aCountDown;
	}else{
		TickZeroPoint = TProtoC2C::DevDelayStartTick + 600;
	}
	*(volatile unsigned int *)aAddr = TickZeroPoint;
}

unsigned int TProtoC2C::GetNwDirectPadDelayStartPoint(unsigned int aNum, unsigned int aDirectPad, unsigned int aNode)
{
	unsigned int aAddr;
	unsigned int NumOffSet, OffSet;
	unsigned int TickZeroPoint;
	unsigned int DelayCountDown;
	if(aNum > dNETWORK_MACHINE_COUNT)return 0;
	if(aDirectPad > dDIRECTCTL_MAX_COUNT)return 0;
	if(aNode > dDIRECTCTL_KEY_MAX_COUNT)return 0;
	
	NumOffSet = aNum * 44'000 + 42'000;
	OffSet = aDirectPad * dDIRECTCTL_KEY_MAX_COUNT * 4 + (aNode *4 );
	
	aAddr = dAddrSdram_NwDelayStart + NumOffSet + OffSet;
	TickZeroPoint = *(volatile unsigned int *)aAddr;
	DelayCountDown = TickZeroPoint - TProtoC2C::DevDelayStartTick;
	if(DelayCountDown > 600){
		DelayCountDown = 0;
	}
	return DelayCountDown;
}

void TProtoC2C::ClearNwDelayStartPoint(unsigned int aNum)
{
	unsigned int aAddr;
	unsigned int NumOffSet, i;
	if(aNum > 16)return;

	NumOffSet = aNum * 44'000;
	aAddr = dAddrSdram_NwDelayStart + NumOffSet;
	for(i=0; i<10240; i++){
		*(volatile unsigned int *)aAddr = 0;
		aAddr += 4;
	}
	
	NumOffSet = aNum * 44'000 + 42'000;
	aAddr = dAddrSdram_NwDelayStart + NumOffSet;
	for(i=0; i<200; i++){
		*(volatile unsigned int *)aAddr = 0;
		aAddr += 4;
	}
}

void TProtoC2C::Task1000Ms()
{
	TProtoC2C::DevDelayStartTick++;
}





















