#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"
	#include "user_eeprom.h"
}

void TProtoC2C::DelayMs(unsigned int aMs)
{
	volatile unsigned int mTick;
	volatile unsigned int Tick;
	for(Tick=0; Tick<aMs; Tick++){
		for(mTick=0; mTick<160000; mTick++);
		sys_error_led_task();
	}
}

void TProtoC2C::WriteMaskCtlNum(unsigned char aStart, unsigned char aEnd)
{
	volatile unsigned int aWait;
	unsigned int aAddr;
	unsigned char tData[4];
	int i;
	//Read ExistTable 
	tData[0] = aStart;
	tData[1] = aEnd;
	aAddr = dEepAddrCtlMaskNum + 0;
	DelayMs(10);
	eeprom_write_byte_via_page(0, aAddr, tData, 2);
	DelayMs(10);
}

void TProtoC2C::ReadMaskCtlNum()
{
	unsigned int aAddr;
	int i;
	unsigned char tData[4];
	//Read ExistTable 
	aAddr = dEepAddrCtlMaskNum;
	DelayMs(10);
	eeprom_read_byte(0, aAddr, tData, 2);
	
	CtlMask.StartNum = tData[0];
	CtlMask.EndNum = tData[1];

	if( (CtlMask.StartNum) || (CtlMask.EndNum) ){
		if( (CtlMask.StartNum ==0) || (CtlMask.StartNum > dNETWORK_MACHINE_COUNT) ){
			CtlMask.StartNum =0;
			CtlMask.EndNum =0;
			return;
		}
		if( (CtlMask.EndNum ==0) || (CtlMask.EndNum > dNETWORK_MACHINE_COUNT) ){
			CtlMask.StartNum =0;
			CtlMask.EndNum =0;
			return;
		}
		if( CtlMask.StartNum > CtlMask.EndNum){
			CtlMask.StartNum =0;
			CtlMask.EndNum =0;
			return;
		}
	}
}

void TProtoC2C::FillDescp()
{
	unsigned int Inx, CtlNum,x, aP0, aP1, aAddr;
	aP0 = oEntry.rBody.Split.P0;
	aP1 = oEntry.rBody.Split.P1;
	if( (aP0 ==0) || (aP0>dPORT_MAX_COUNT) )return;
	if( (aP1 ==0) || (aP0>250) )return;
	
	aP0--;
	aP1--;
	
	TxMsg.Data.Routing =0;
	
	TxMsg.Data.Cmd = dNetworkCmd_DevDescp;
	TxMsg.Data.Order = 0;	// On Setup
	
	aAddr = dAddrSdRam_Descp;
	for(x=0;x<8;x++)DescpBuf.D32[x] = *(volatile unsigned int *)(aAddr + (aP0 * 8192) + (aP1 * 32) + (x * 4));
	
	TxMsg.Data.Len = 8;
	TxMsg.Data.Data[1] =MainCtl.fData.Split.MyNum;
		
	TxMsg.Data.Data[0] =1;
	for(x=2;x<8;x++)TxMsg.Data.Data[x] = DescpBuf.D8[x-2];
	oBufInsert(TxMsg);
		
	TxMsg.Data.Data[0] =2;
	for(x=2;x<8;x++)TxMsg.Data.Data[x] = DescpBuf.D8[x+4];
	oBufInsert(TxMsg);
		
	TxMsg.Data.Data[0] =3;
	for(x=2;x<8;x++)TxMsg.Data.Data[x] = DescpBuf.D8[x+10];
	oBufInsert(TxMsg);
		
	TxMsg.Data.Data[0] =4;
	for(x=2;x<8;x++)TxMsg.Data.Data[x] = DescpBuf.D8[x+16];
	oBufInsert(TxMsg);
		
	TxMsg.Data.Data[0] =5;
	for(x=2;x<8;x++)TxMsg.Data.Data[x] = DescpBuf.D8[x+22];
	oBufInsert(TxMsg);
	
	TxMsg.Data.Len = 6;
	TxMsg.Data.Data[0] =6;
	
	TxMsg.Data.Data[2] = DescpBuf.D8[30];
	TxMsg.Data.Data[3] = DescpBuf.D8[31];
	TxMsg.Data.Data[4] = oEntry.rBody.Split.P0;
	TxMsg.Data.Data[5] = oEntry.rBody.Split.P1;
	oBufInsert(TxMsg);
}

void TProtoC2C::ProcessDevDescpRec()
{
	unsigned int Inx, CtlNum,x, aP, aP0, aP1;
	volatile unsigned int aAddr;
	Inx =  RxMsg.Data.Data[0];
	CtlNum = RxMsg.Data.Data[1];
	if( (CtlNum ==0) || (CtlNum>32) )return;
	if(Inx == 1){
		for(x=2;x<8;x++)OnSetup.Descp.D8[x-2] = RxMsg.Data.Data[x];
		OnSetup.DescpPiece |= 0x00000001;
	}else
	if(Inx == 2){
		for(x=2;x<8;x++)OnSetup.Descp.D8[x+4] = RxMsg.Data.Data[x];
		OnSetup.DescpPiece |= 0x00000002;
	}else
	if(Inx == 3){
		for(x=2;x<8;x++)OnSetup.Descp.D8[x+10] = RxMsg.Data.Data[x];
		OnSetup.DescpPiece |= 0x00000004;
	}else
	if(Inx == 4){
		for(x=2;x<8;x++)OnSetup.Descp.D8[x+16] = RxMsg.Data.Data[x];
		OnSetup.DescpPiece |= 0x00000008;
	}else
	if(Inx == 5){
		for(x=2;x<8;x++)OnSetup.Descp.D8[x+22] = RxMsg.Data.Data[x];
		OnSetup.DescpPiece |= 0x000000010;
	}else
	if(Inx == 6){
		OnSetup.Descp.D8[30] = RxMsg.Data.Data[2];
		OnSetup.Descp.D8[31] = RxMsg.Data.Data[3];
		aP0 = RxMsg.Data.Data[4];
		aP1 = RxMsg.Data.Data[5];
		if(OnSetup.DescpPiece == 0x0000001F){
			if( (aP0 != 0) && (aP0 <=dPORT_MAX_COUNT) ){
				if( (aP1 != 0) && (aP1 <=250) ){
					aP = CtlNum -1;
					aP0--;
					aP1--;
					aAddr = dAddrSdRam_DescpNw;
					for(x=0; x<8; x++){
						*(volatile unsigned int *)(aAddr + (aP*dONE_CTRL_DESCP_BYTES)+(aP0*8192) + (aP1*32) + (x*4)) = OnSetup.Descp.D32[x];
					}	
				}
			}
			
			OnSetup.DescpPiece = 0;
		}else{
			//Fail Ack ReSend
			
			
		}
	}
}

void TProtoC2C::SaveCfgData(unsigned char *data)
{
	unsigned int Inx, CtlNum;
	volatile unsigned int aAddr;
	Inx =  RxMsg.Data.Data[0];
	CtlNum = RxMsg.Data.Data[1];
	if( (CtlNum ==0) || (CtlNum>32) )return;
	
	if(Inx == 0){
		MasterNum = CtlNum;
		CfgData.CtlCount = RxMsg.Data.Data[3];
	}else
	if(Inx == 1){
		CfgData.CtlList[ 0] = RxMsg.Data.Data[4];
		CfgData.CtlList[ 1] = RxMsg.Data.Data[5];
		CfgData.CtlList[ 2] = RxMsg.Data.Data[6];
		CfgData.CtlList[ 3] = RxMsg.Data.Data[7];
	}else
	if(Inx == 2){
		CfgData.CtlList[ 4] = RxMsg.Data.Data[4];
		CfgData.CtlList[ 5] = RxMsg.Data.Data[5];
		CfgData.CtlList[ 6] = RxMsg.Data.Data[6];
		CfgData.CtlList[ 7] = RxMsg.Data.Data[7];
	}else
	if(Inx == 3){
		CfgData.CtlList[ 8] = RxMsg.Data.Data[4];
		CfgData.CtlList[ 9] = RxMsg.Data.Data[5];
		CfgData.CtlList[10] = RxMsg.Data.Data[6];
		CfgData.CtlList[11] = RxMsg.Data.Data[7];
	}else
	if(Inx == 4){
		CfgData.CtlList[12] = RxMsg.Data.Data[4];
		CfgData.CtlList[13] = RxMsg.Data.Data[5];
		CfgData.CtlList[14] = RxMsg.Data.Data[6];
		CfgData.CtlList[15] = RxMsg.Data.Data[7];
	}else
	if(Inx == 5){
		CfgData.CtlList[16] = RxMsg.Data.Data[4];
		CfgData.CtlList[17] = RxMsg.Data.Data[5];
		CfgData.CtlList[18] = RxMsg.Data.Data[6];
		CfgData.CtlList[19] = RxMsg.Data.Data[7];
	}else
	if(Inx == 6){
		CfgData.CtlList[20] = RxMsg.Data.Data[4];
		CfgData.CtlList[21] = RxMsg.Data.Data[5];
		CfgData.CtlList[22] = RxMsg.Data.Data[6];
		CfgData.CtlList[23] = RxMsg.Data.Data[7];
	}else
	if(Inx == 7){
		CfgData.CtlList[24] = RxMsg.Data.Data[4];
		CfgData.CtlList[25] = RxMsg.Data.Data[5];
		CfgData.CtlList[26] = RxMsg.Data.Data[6];
		CfgData.CtlList[27] = RxMsg.Data.Data[7];
	}else
	if(Inx == 8){
		CfgData.CtlList[28] = RxMsg.Data.Data[4];
		CfgData.CtlList[29] = RxMsg.Data.Data[5];
		CfgData.CtlList[30] = RxMsg.Data.Data[6];
		CfgData.CtlList[31] = RxMsg.Data.Data[7];
	}else
	if(Inx == 9){
		if( (MainCtl.fData.Split.MyNum <RxMsg.Data.Data[4]) || (MainCtl.fData.Split.MyNum > RxMsg.Data.Data[5]) ){
			if( (RxMsg.Data.Data[1] < ProtoC2C.CtlMask.StartNum) || (RxMsg.Data.Data[1] > ProtoC2C.CtlMask.EndNum) ){
				CtlNum = RxMsg.Data.Data[1];
				CfgData.CtlCount = RxMsg.Data.Data[3];
				int IncludeMe =0;
				for(int i=0; (i<CfgData.CtlCount) && (i <dNETWORK_MACHINE_COUNT); i++){
					if(CfgData.CtlList[i] == MainCtl.fData.Split.MyNum){
						IncludeMe =1;
						break;
					}
				}
				if(CfgData.CtlCount){
					if(IncludeMe){
						Record.SaveNwCfg(CtlNum, CfgData.CtlCount, CfgData.CtlList);
						Record.ReadNwCfg();
						Record.OperateAdd_RegCanNetwork(2, CtlNum, CfgData.CtlCount);
						SyncCheckOnline();
					}
				}
			}
		}
	}
}


void TProtoC2C::SaveM2MCfgData(unsigned char *data)
{
	unsigned int Inx, CtlNum;
	volatile unsigned int aAddr;
	Inx =  RxMsg.Data.Data[0];
	CtlNum = RxMsg.Data.Data[1];
	if( (CtlNum ==0) || (CtlNum>32) )return;
	
	if(Inx == 0){
		TProtoM2M::sCnt = RxMsg.Data.Data[3];
		TProtoM2M::sRecPiece = 0x01;
	}else
	if(Inx == 1){
		TProtoM2M::sList[ 0] = RxMsg.Data.Data[4];
		TProtoM2M::sList[ 1] = RxMsg.Data.Data[5];
		TProtoM2M::sList[ 2] = RxMsg.Data.Data[6];
		TProtoM2M::sList[ 3] = RxMsg.Data.Data[7];
		TProtoM2M::sRecPiece |= 0x02;
	}else
	if(Inx == 2){
		TProtoM2M::sList[ 4] = RxMsg.Data.Data[4];
		TProtoM2M::sList[ 5] = RxMsg.Data.Data[5];
		TProtoM2M::sList[ 6] = RxMsg.Data.Data[6];
		TProtoM2M::sList[ 7] = RxMsg.Data.Data[7];
		TProtoM2M::sRecPiece |= 0x04;

		if(TProtoM2M::sRecPiece != 0x07){
			TProtoM2M::sRecPiece = 0;
			return;
		}
		TProtoM2M::sRecPiece = 0;
		if( (RxMsg.Data.Data[1] < ProtoC2C.CtlMask.StartNum) || (RxMsg.Data.Data[1] > ProtoC2C.CtlMask.EndNum) ){
			CtlNum = RxMsg.Data.Data[1];
			TProtoM2M::sCnt = RxMsg.Data.Data[3];
			int IncludeMe =0;
			for(int i=0; (i<TProtoM2M::sCnt) && (i <dNETWORK_MACHINE_MASTER_COUNT); i++){
				if(TProtoM2M::sList[i] == MainCtl.fData.Split.MyNum){
					IncludeMe =1;
					break;
				}
			}
			if(TProtoM2M::sCnt){
				if(IncludeMe){
					Record.SaveNwM2MCfg(TProtoM2M::sCnt, TProtoM2M::sList);
					Record.ReadNwM2MCfg();
					Record.OperateAdd_RegCanNetwork(3, CtlNum, TProtoM2M::sCnt);
					SyncCheckM2MOnline();
				}
			}
		}
	}
}

void TProtoC2C::ClearCfgData(unsigned char *data)
{
	unsigned int Inx, CtlNum,x, aP, aP0, aP1;
	volatile unsigned int aAddr;	

	Inx =  RxMsg.Data.Data[0];
	CtlNum = RxMsg.Data.Data[1];
	if( (CtlNum ==0) || (CtlNum>32) )return;
	
	if(Inx == 0){
		CfgData.CtlCount = 0;
	}else
	if(Inx == 1){
		CfgData.CtlList[ 0] = RxMsg.Data.Data[4];
		CfgData.CtlList[ 1] = RxMsg.Data.Data[5];
		CfgData.CtlList[ 2] = RxMsg.Data.Data[6];
		CfgData.CtlList[ 3] = RxMsg.Data.Data[7];
	}else
	if(Inx == 2){
		CfgData.CtlList[ 4] = RxMsg.Data.Data[4];
		CfgData.CtlList[ 5] = RxMsg.Data.Data[5];
		CfgData.CtlList[ 6] = RxMsg.Data.Data[6];
		CfgData.CtlList[ 7] = RxMsg.Data.Data[7];
	}else
	if(Inx == 3){
		CfgData.CtlList[ 8] = RxMsg.Data.Data[4];
		CfgData.CtlList[ 9] = RxMsg.Data.Data[5];
		CfgData.CtlList[10] = RxMsg.Data.Data[6];
		CfgData.CtlList[11] = RxMsg.Data.Data[7];
	}else
	if(Inx == 4){
		CfgData.CtlList[12] = RxMsg.Data.Data[4];
		CfgData.CtlList[13] = RxMsg.Data.Data[5];
		CfgData.CtlList[14] = RxMsg.Data.Data[6];
		CfgData.CtlList[15] = RxMsg.Data.Data[7];
	}else
	if(Inx == 5){
		CfgData.CtlList[16] = RxMsg.Data.Data[4];
		CfgData.CtlList[17] = RxMsg.Data.Data[5];
		CfgData.CtlList[18] = RxMsg.Data.Data[6];
		CfgData.CtlList[19] = RxMsg.Data.Data[7];
	}else
	if(Inx == 6){
		CfgData.CtlList[20] = RxMsg.Data.Data[4];
		CfgData.CtlList[21] = RxMsg.Data.Data[5];
		CfgData.CtlList[22] = RxMsg.Data.Data[6];
		CfgData.CtlList[23] = RxMsg.Data.Data[7];
	}else
	if(Inx == 7){
		CfgData.CtlList[24] = RxMsg.Data.Data[4];
		CfgData.CtlList[25] = RxMsg.Data.Data[5];
		CfgData.CtlList[26] = RxMsg.Data.Data[6];
		CfgData.CtlList[27] = RxMsg.Data.Data[7];
	}else
	if(Inx == 8){
		CfgData.CtlList[28] = RxMsg.Data.Data[4];
		CfgData.CtlList[29] = RxMsg.Data.Data[5];
		CfgData.CtlList[30] = RxMsg.Data.Data[6];
		CfgData.CtlList[31] = RxMsg.Data.Data[7];
	}else
	if(Inx == 9){
		if( (MainCtl.fData.Split.MyNum <RxMsg.Data.Data[4]) || (MainCtl.fData.Split.MyNum > RxMsg.Data.Data[5]) ){
			if( (RxMsg.Data.Data[1] < this->CtlMask.StartNum) || (RxMsg.Data.Data[1] > this->CtlMask.EndNum) ){
				CtlNum = RxMsg.Data.Data[1];
				int IncludeMe =0;
				for(int i=0; i<dNETWORK_MACHINE_COUNT; i++){
					if(CfgData.CtlList[i] == MainCtl.fData.Split.MyNum){
						IncludeMe =1;
						break;
					}
				}
				if(CtlNum == this->MasterNum){
					for(x=0; x<dNETWORK_MACHINE_COUNT; x++)SlaveList[x] =0;
					CtlNum =0;
					Record.SaveNwCfg(CtlNum, 0, SlaveList);
					Record.ReadNwCfg();
					Record.OperateAdd_RegCanNetwork(2, CtlNum, 0);	
				}
			}
		}
	}
}


void TProtoC2C::ClearM2MCfgData(unsigned char *data)
{
	unsigned int Inx, CtlNum,x, aP, aP0, aP1;
	volatile unsigned int aAddr;	

	Inx =  RxMsg.Data.Data[0];
	CtlNum = RxMsg.Data.Data[1];
	if( (CtlNum ==0) || (CtlNum>32) )return;
	if(MainCtl.fData.Split.NetworkMode != 1)return;
	
	if(Inx == 0){
		TProtoM2M::sCnt = 0;
		TProtoM2M::sRecPiece = 1;
	}else
	if(Inx == 1){
		TProtoM2M::sList[0] =0;
		TProtoM2M::sList[1] =0;
		TProtoM2M::sList[2] =0;
		TProtoM2M::sList[3] =0;
		TProtoM2M::sRecPiece |= 0x02;
	}else
	if(Inx == 2){
		TProtoM2M::sList[4] =0;
		TProtoM2M::sList[5] =0;
		TProtoM2M::sList[6] =0;
		TProtoM2M::sList[7] =0;
		TProtoM2M::sRecPiece |= 0x04;

		if(TProtoM2M::sRecPiece != 0x07){
			TProtoM2M::sRecPiece = 0;
			return;
		}
		TProtoM2M::sRecPiece = 0;
		if( (MainCtl.fData.Split.MyNum <RxMsg.Data.Data[4]) || (MainCtl.fData.Split.MyNum > RxMsg.Data.Data[5]) ){
			if( (RxMsg.Data.Data[1] < ProtoC2C.CtlMask.StartNum) || (RxMsg.Data.Data[1] > ProtoC2C.CtlMask.EndNum) ){
				CtlNum = RxMsg.Data.Data[1];
				int IncludeInitiator =0;
				for(int i=0; (i<dNETWORK_MACHINE_MASTER_COUNT) and (i<M2MList.Cnt); i++){
					if(M2MList.Items[i] == CtlNum){
						IncludeInitiator =1;
						break;
					}
				}
				if(IncludeInitiator){
					for(x=0; (x<dNETWORK_MACHINE_MASTER_COUNT); x++)TProtoM2M::sList[x] =0;
					Record.SaveNwM2MCfg(0, TProtoM2M::sList);
					Record.ReadNwM2MCfg();
					Record.OperateAdd_RegCanNetwork(3, CtlNum, 0);	
				}
			}
		}
	}
}

void TProtoC2C::DateTimeSet()
{
	//CmdC2C_RpyType		:    	D0=Len, D1=Pri, D2=CMD, 		D3=CTLBYTE D4=SubCmd 		D5=CtlNum  D6=Type	(Master or Slave, See Define)

	//TxMsg.Data.Pri = 1;
	//TxMsg.Data.Cmd = dNetworkCmd_SetDateTime;
	//TxMsg.Data.Len = 8;
	//TxMsg.Data.Data[0] =0;
	//TxMsg.Data.Data[1] =0;
	//TxMsg.Data.Data[2] =DateTime.Date.Split.Year;
	//TxMsg.Data.Data[3] =DateTime.Date.Split.Month;
	//TxMsg.Data.Data[4] = DateTime.Date.Split.Day;
	//TxMsg.Data.Data[5] = DateTime.Time.Split.Hour;
	//TxMsg.Data.Data[6] = DateTime.Time.Split.Minute;
	//TxMsg.Data.Data[7] = DateTime.Time.Split.Second;
	
	//oBufInsert();
	
	Record.OperateAdd_SetNwDateTime(TxMsg.Data.Data[2],TxMsg.Data.Data[3], TxMsg.Data.Data[4], TxMsg.Data.Data[5], TxMsg.Data.Data[6], TxMsg.Data.Data[7]);
}

void TProtoC2C::DateTimeReq()
{
	//CmdC2C_RpyType		:    	D0=Len, D1=Pri, D2=CMD, 		D3=CTLBYTE D4=SubCmd 		D5=CtlNum  D6=Type	(Master or Slave, See Define)
	TxMsg.Data.Routing =0;
	TxMsg.Data.Priority = 1;
	TxMsg.Data.Cmd = dNetworkCmd_ReqDateTime;
	TxMsg.Data.Len = 8;
	TxMsg.Data.Data[0] =0;
	TxMsg.Data.Data[1] =0;
	TxMsg.Data.Data[1] = MainCtl.fData.Split.MyNum;
	TxMsg.Data.Data[2] =DateTime.Date.Split.Year;
	TxMsg.Data.Data[3] =DateTime.Date.Split.Month;
	TxMsg.Data.Data[4] = DateTime.Date.Split.Day;
	TxMsg.Data.Data[5] = DateTime.Time.Split.Hour;
	TxMsg.Data.Data[6] = DateTime.Time.Split.Minute;
	TxMsg.Data.Data[7] = DateTime.Time.Split.Second;
	
	oBufInsert();
	
	Record.OperateAdd_ReqNwDateTime();
}

void TProtoC2C::PortExistAsk(unsigned char aNum, unsigned char aP0)
{
	TxMsg.Data.Routing =0;
	TxMsg.Data.Priority = 1;
	TxMsg.Data.Cmd = dNetworkCmd_PortExistAsk;
	TxMsg.Data.Len = 4;
	TxMsg.Data.Data[0] =1;
	TxMsg.Data.Data[1] = aNum;
	TxMsg.Data.Data[2] = aP0;
	TxMsg.Data.Data[3] = 0;
	
	oBufInsert();
}

void TProtoC2C::DescpOnePortAsk(unsigned char aNum, unsigned char aP0)
{
	
	TxMsg.Data.Routing =0;
	TxMsg.Data.Priority = 1;
	TxMsg.Data.Cmd = dNetworkCmd_DescpOnePortAsk;
	TxMsg.Data.Len = 4;
	TxMsg.Data.Data[0] =1;
	TxMsg.Data.Data[1] = aNum;
	TxMsg.Data.Data[2] = aP0;
	TxMsg.Data.Data[3] = 0;
	
	oBufInsert();
}

void TProtoC2C::DescpOnePortRpy(unsigned char aNum, unsigned char aP0)
{
	int i;
	for(i=1; i<251; i++)
		DescpListRpy(aNum, aP0, i);
}

void TProtoC2C::DescpAllDirectPadRpy(unsigned char aNum)
{
	int i, x;
	for(i=0; i<dDIRECTCTL_MAX_COUNT; i++){
		for(x=0; x<dDIRECTCTL_KEY_MAX_COUNT; x++)DescpListDirectPadRpy(aNum, i+1, x+1);
	}
}

void TProtoC2C::DescpListAsk(unsigned char aNum, unsigned char aP0, unsigned char Inx, unsigned char *pAddrList)
{
	TxMsg.Data.Routing =0;
	TxMsg.Data.Priority = 1;
	TxMsg.Data.Cmd = dNetworkCmd_DescpAsk;
	TxMsg.Data.Len = 8;
	TxMsg.Data.Data[0] = 1;
	TxMsg.Data.Data[1] = aNum;
	TxMsg.Data.Data[2] = aP0;
	TxMsg.Data.Data[3] = Inx;
	TxMsg.Data.Data[4] = pAddrList[0];
	TxMsg.Data.Data[5] = pAddrList[1];
	TxMsg.Data.Data[6] = pAddrList[2];
	TxMsg.Data.Data[7] = pAddrList[3];
	
	oBufInsert();	
}

void TProtoC2C::PortExistRpy(unsigned char aNum, unsigned char aP0)
{
	unsigned char P0;
	if(aP0)P0=aP0-1;else return;
	if(P0 > dPORT_MAX_COUNT)return;
	if(aNum == MainCtl.fData.Split.MyNum){
		if(aP0 <= dPORT_MAX_COUNT){
			if(MainCtl.fData.Split.ExistTablePort[P0]){
				TxMsg.Data.Data[3] = 0xFF;
			}else{
				TxMsg.Data.Data[3] = 0x10;
			}
		}else
		if(aP0 == (dPORT_MAX_COUNT +1)){
			TxMsg.Data.Data[3] = 0xFF;
		}
		TxMsg.Data.Routing =0;
		TxMsg.Data.Priority = 1;
		TxMsg.Data.Cmd = dNetworkCmd_PortExistRpy;
		TxMsg.Data.Len = 4;
		TxMsg.Data.Data[0] =1;
		TxMsg.Data.Data[1] = aNum;
		TxMsg.Data.Data[2] = aP0;
		oBufInsert();
	}
}

void TProtoC2C::DescpListRpy(unsigned char aNum, unsigned char aP0, unsigned char Addr)
{
	unsigned char P0, P1;
	char *p;
	unsigned int i, aAddr, Dok;

	if( (aP0 ==0) || (aP0>dPORT_MAX_COUNT) )return;
	if( (Addr ==0) || (Addr>250) )return;
	
	P0 =aP0-1;
	P1 =Addr-1;
	Dok = 0;
	if(aNum == MainCtl.fData.Split.MyNum){
		if(MainCtl.fData.Split.ExistTablePort[P0]){
			if( Port[P0].ExistTable[P1] ){
				aAddr = dAddrSdRam_Descp + (P0 * 256 * 32) + (P1 * 32);
				for(i=0; i<8; i++){
					DescpBuf.D32[i] = *(volatile unsigned int *)(aAddr + (i *4));
				}
				if( DescpBuf.D8[0] != '\0') Dok = 1;
			}else{
				Dok = 0;
			}
		}
		if( (DescpBuf.D8[0] == 0xFF) && (DescpBuf.D8[1] == 0xFF) ) Dok = 0;
		
		TxMsg.Data.Cmd = dNetworkCmd_DescpRpy;
		TxMsg.Data.Routing =0;
		TxMsg.Data.Priority = 1;
		TxMsg.Data.Len = 8;
		TxMsg.Data.Order = 0;
		TxMsg.Data.Identification = 0;
		TxMsg.Data.Data[1] = aNum;
		TxMsg.Data.Data[2] = aP0;
		TxMsg.Data.Data[3] = Addr;
		if(Dok){
			for(i=0; i<8; i++){
				TxMsg.Data.Data[0] = i;
				TxMsg.Data.Data[4] = DescpBuf.D8[i*4 +0];
				TxMsg.Data.Data[5] = DescpBuf.D8[i*4 +1];
				TxMsg.Data.Data[6] = DescpBuf.D8[i*4 +2];
				TxMsg.Data.Data[7] = DescpBuf.D8[i*4 +3];
				oBufInsert();
			}
		}else{
			TxMsg.Data.Data[0] = 0x33;
			TxMsg.Data.Data[4] = 0;
			TxMsg.Data.Data[5] = 0;
			TxMsg.Data.Data[6] = 0;
			TxMsg.Data.Data[7] = 0;
			oBufInsert();
		}
	}	
}

void TProtoC2C::DescpListDirectPadRpy(unsigned char aNum, unsigned char aPadNum, unsigned char aKey)
{
	unsigned char P0, P1;
	char *p;
	unsigned int i, aAddr, Dok;
	unsigned int OffSet;

	if( (aPadNum ==0) || (aPadNum > dDIRECTCTL_MAX_COUNT) )return;
	if( (aKey ==0) || (aKey > dDIRECTCTL_KEY_MAX_COUNT) )return;
	
	OffSet = (aPadNum-1) * 8 + (aKey);
	
	Dok = 0;
	if(aNum == MainCtl.fData.Split.MyNum){
		aAddr = dAddrSdRam_Descp + 327680 + (OffSet * 32);
		for(i=0; i<8; i++){
			DescpBuf.D32[i] = *(volatile unsigned int *)(aAddr + (i *4));
		}
		if( DescpBuf.D8[0] != '\0') Dok = 1;
		if( (DescpBuf.D8[0] == 0xFF) && (DescpBuf.D8[1] == 0xFF) ) Dok = 0;
		
		TxMsg.Data.Cmd = dNetworkCmd_DescpRpy;
		TxMsg.Data.Routing =0;
		TxMsg.Data.Priority = 1;
		TxMsg.Data.Len = 8;
		TxMsg.Data.Order = 0;
		TxMsg.Data.Identification = 0;
		TxMsg.Data.Data[1] = aNum;
		TxMsg.Data.Data[2] = 41;
		TxMsg.Data.Data[3] = OffSet;
		if(Dok){
			for(i=0; i<8; i++){
				TxMsg.Data.Data[0] = i;
				TxMsg.Data.Data[4] = DescpBuf.D8[i*4 +0];
				TxMsg.Data.Data[5] = DescpBuf.D8[i*4 +1];
				TxMsg.Data.Data[6] = DescpBuf.D8[i*4 +2];
				TxMsg.Data.Data[7] = DescpBuf.D8[i*4 +3];
				oBufInsert();
			}
		}else{
			TxMsg.Data.Data[0] = 0x33;
			TxMsg.Data.Data[4] = 0;
			TxMsg.Data.Data[5] = 0;
			TxMsg.Data.Data[6] = 0;
			TxMsg.Data.Data[7] = 0;
			oBufInsert();
		}
	}	
}

void TProtoC2C::DescpListDirectPadRpy(unsigned char aNum, unsigned char Inx)
{
	unsigned char P0, P1;
	char *p;
	unsigned int i, aAddr, Dok;
	unsigned int OffSet;

	if( (Inx ==0) || (Inx > 160) )return;
	
	OffSet = Inx;
	
	Dok = 0;
	if(aNum == MainCtl.fData.Split.MyNum){
		aAddr = dAddrSdRam_Descp + 327680 + (OffSet * 32);
		for(i=0; i<8; i++){
			DescpBuf.D32[i] = *(volatile unsigned int *)(aAddr + (i *4));
		}
		if( DescpBuf.D8[0] != '\0') Dok = 1;
		if( (DescpBuf.D8[0] == 0xFF) && (DescpBuf.D8[1] == 0xFF) ) Dok = 0;
		
		TxMsg.Data.Cmd = dNetworkCmd_DescpRpy;
		TxMsg.Data.Routing = 0;
		TxMsg.Data.Priority = 1;
		TxMsg.Data.Len = 8;
		TxMsg.Data.Order = 0;
		TxMsg.Data.Identification = 0;
		TxMsg.Data.Data[1] = aNum;
		TxMsg.Data.Data[2] = 41;
		TxMsg.Data.Data[3] = OffSet;
		if(Dok){
			for(i=0; i<8; i++){
				TxMsg.Data.Data[0] = i;
				TxMsg.Data.Data[4] = DescpBuf.D8[i*4 +0];
				TxMsg.Data.Data[5] = DescpBuf.D8[i*4 +1];
				TxMsg.Data.Data[6] = DescpBuf.D8[i*4 +2];
				TxMsg.Data.Data[7] = DescpBuf.D8[i*4 +3];
				oBufInsert();
			}
		}else{
			TxMsg.Data.Data[0] = 0x33;
			TxMsg.Data.Data[4] = 0;
			TxMsg.Data.Data[5] = 0;
			TxMsg.Data.Data[6] = 0;
			TxMsg.Data.Data[7] = 0;
			oBufInsert();
		}
	}	
}

void TProtoC2C::TaskSecondUpdate()
{
	if( 	(DateTime.Time.Split.Hour == 1) &&
		(DateTime.Time.Split.Minute == 2 ) &&
		(DateTime.Time.Split.Second == 3 ) ){
			ShouldSyncTime = 1;
	}
	if(ShouldSyncTime){
		DateTimeSet();
		ShouldSyncTime = 0;
	}
}

void TProtoC2C::SyncCheckOnline()
{
	if(MainCtl.fData.Split.NetworkMode == 1){
		CheckOnlineTick =0;
		CheckOnlineHasStart =0;
		CheckOnlineAfterSendTick =0;
		
		for(int i=1; i<=dNETWORK_MACHINE_COUNT; i++){
			SlaveOnlineList[i] = 1;
		}
	}else
	if(MainCtl.fData.Split.NetworkMode == 2){
		CheckOnlineUpdateTick =0;
	}
}


























