#include "RuntimeData.h"
#include "Runtime.h"
#include "record.h"
#include "EncodeConv.h"
#include "CnCpp.h"


#define dNetStateCtlUsingByte			32768
#define dNetStateCtlUsingInt			8192
#define dNetStateCtlPortUsingByte		512

#define dNetDayTimeCtlUsingByte		65536
#define dNetDayTimeCtlUsingInt		16384
#define dNetDayTimePortUsingByte		1024

#define dNetYearMonthCtlUsingByte		32768
#define dNetYearMonthCtlUsingInt		8192
#define dNetYearMonthPortUsingByte		512

extern "C"{
	#include "uart_key_drv.h"
	#include "uart_linkage_drv.h"
	#include "uart_printer_drv.h"
	#include "fdcan_task.h"
	#include "task.h"
}

TSysAuthorize	SysAuthorize;


unsigned int InnerCanStartUpCmpl = 0;

const char VerString[32] = {"V0.10\0"};
const char VerDateTimeString[32] = {"a2024-12-05 19:30:00\0"};

volatile int IsEventAddBlock = 0;
TManualEventMsgAdd ManualEventMsgAdd;

unsigned char SystemPermission = 1;
int SystemPermissionTick =0;

TPermissionDt PermissionDt;
TPriterSetting PriterSetting;

unsigned char PrjName[36];
unsigned char CtlName[36];
class TDateTime	PrjBuildDateTime;

class TDateTime DateTime;
class TDateTime TrialEndDateTime;

unsigned char RunState = TRunModeCfg::RunModeRun;

unsigned char OuterCanTxState = 0;
TInnerCanRxBuf		InnerCanRxBuf;
TExtCanRxBufUsingUart	ExtCanRxBufUsingUart;

char CommomGuiTempText[64];

unsigned int GraphUsing	= 0;
unsigned int GraphAddr =2;
unsigned int GraphLineType =0;
//unsigned int GraphDevBrd	= 9600;
//unsigned short GraphDevFrameIntervalMs	= 20;
//unsigned short GraphDevHalfDuplex		= 1;
//unsigned short GraphDevReplyDelayUs		= 4;
//unsigned short GraphDevReplyRetainTimeMs	= 80;

TBroadcast	 Broadcast;
TScreenAndKeyLock ScreenAndKeyLock;

unsigned char AlramVaOnOff = 0;
unsigned int AlarmSoundWhat = 0x07;
unsigned int IsMute = 0;
unsigned int IsMuteFire = 0;
unsigned int IsMuteLnk= 0;
unsigned int IsMuteSv= 0;
unsigned int IsMuteFault= 0;
unsigned int IsMuteOpa= 0;

unsigned char PumpOnOff = 0;
unsigned char PumpFeedBack = 0;

unsigned int IsSystemError = 0;
unsigned int IsShowingMsgReset = 0;
unsigned int IsShowTipPopup =0;
unsigned int AllowAuto;
unsigned int AllowManual;
unsigned char LinkageManualGo = 0;

unsigned char PowerSuplyStateOld =0;
unsigned char PowerSuplyState =0;
unsigned char PowerSuplyCommOld =0;
unsigned char PowerSuplyComm =0;
unsigned char PowerBkStateOld =0;
unsigned char PowerBkState =0;
unsigned char BatStateOld =0;
unsigned char BatState =0;
unsigned char PowerSuplyOutputOld =0;
unsigned char PowerSuplyOutput =0;
unsigned char Bat1UnderVoltageOld =0;
unsigned char Bat2UnderVoltageOld =0;
unsigned char Bat1UnderVoltage =0;
unsigned char Bat2UnderVoltage =0;


unsigned int  FireVaTrenOned =0;

unsigned int C2cCompletedTick = 0;
unsigned int GolbalDelayTick = 0;

unsigned char IsStartDelay = 0;
unsigned char NwIsSelfCheck = 0;
unsigned char NwSelfCheckTick[32];
unsigned int NwIsNormalCheck =0;
unsigned int NwIsStartDelay =0;
unsigned int NwIsFbMiss =0;

unsigned int LanguageEnCn;
unsigned int TimeSecHasRun =0;

unsigned int SysIsAuthState = 0;
unsigned int SysRemainAuthTimeHour = 0;
unsigned int IsDulAddrChecking = 0;

TTextAnsi TextAnsi;
TTextUtf8 TextUtf8;

int PsBatDataAva = 0;
int PsBatDataHasUpdate = 0;
TPsBatData		PsBatData, PsBatDataOld;
TKeyPadData		KeyPadData;
TInspec		InspecData;

//TDisplayPadRt	DisplayPadRt;
//TBusPadRt		BusPadRt;
TDesigePrmData	DesigePrmData;
TMaintainData 	MaintainData;
//TRunModeData	RunModeData;
TAreaSetting	AreaSetting;
TBroadcastAlramAlternating	BroadcastAlramAlternating;

int TrialCountDownDay;
int TrialEndDay;

int IsHotKeyCheckingPermission = 0;

static TRecordCommon aIm;

TTextInputMode CnInputMode;

int IsMaintainTip =0;
int LastMaintainTip =0;
int MaintainHourTick=0;
int MaintainHourRdy=0;
TLastMaintain LastMaintain;

int StartUpCompleted =0;
unsigned int TickNoKey4PopupTip =0;

TRtReadWriteBuf256Bytes RtBuf256Bytes;

class TDoPrint 	DoPrint;
class TexRecord 	ExRecord;
class TFecBusCRT 	FecBusCRT;
class TFecBus 	FecStd;
class TRecord		Record;
class TConfig 	Config;
class TControlCenter 	CtlCenter;
class TCtrlStartUp	CtrlStartUp;
class TPinyinT9		PinyinT9;
class TKeyPad		KeyPad;
class TProtoC2C		ProtoC2C;
class TInterLink		InterLink;
class TDevPrinter		DevPrinter;
class TProtoCrt		Crt;
class TGeneralDev		GeneralDev;
//class TRtUserCode		RtUserCode;
class TRemoteData		RemoteData;
class TMqttData 	MqttData;

TPowerPrmValue 	PowerPrmValue;
TFecBusCfg		FecBusCfg;

int SlaveInRemoteCtl = 0;

void TaskTimeUpData()
{
	#define stRadix 16
	int d,e;
	if(TimeUpdata){
		TimeUpdata = 0;
		if(LiteSelfCheck.IsShowing == 0){
			STextTimeShow.Text[0] = '2';
			STextTimeShow.Text[1] = '0';
			
			d = DateTime.Date.Split.Year /stRadix % 10;
			e = DateTime.Date.Split.Year % stRadix % 10;
			STextTimeShow.Text[2] = d + '0';
			STextTimeShow.Text[3] = e + '0';
			
			STextTimeShow.Text[4] = '-';
			
			d = DateTime.Date.Split.Month /stRadix % 10;
			e = DateTime.Date.Split.Month % stRadix % 10;
			STextTimeShow.Text[5] = d + '0';
			STextTimeShow.Text[6] = e + '0';
			STextTimeShow.Text[7] = '-';
			
			d = DateTime.Date.Split.Day /stRadix % 10;
			e = DateTime.Date.Split.Day % stRadix % 10;
			STextTimeShow.Text[8] = d + '0';
			STextTimeShow.Text[9] = e + '0';
			
			STextTimeShow.Text[10] = ' ';
			
			d = DateTime.Time.Split.Hour /stRadix % 10;
			e = DateTime.Time.Split.Hour % stRadix % 10;
			STextTimeShow.Text[11] = d + '0';
			STextTimeShow.Text[12] = e + '0';
			STextTimeShow.Text[13] = ':';

			d = DateTime.Time.Split.Minute /stRadix % 10;
			e = DateTime.Time.Split.Minute % stRadix % 10;
			STextTimeShow.Text[14] = d + '0';
			STextTimeShow.Text[15] = e + '0';
			STextTimeShow.Text[16] = ':';
			
			d = DateTime.Time.Split.Second /stRadix % 10;
			e = DateTime.Time.Split.Second % stRadix % 10;
			STextTimeShow.Text[17] = d + '0';
			STextTimeShow.Text[18] = e + '0';
			STextTimeShow.Text[19] = 0;
			STextTimeShow.Text[20] = 0;
			
			//if(DtStartupCrt == 0){
			//	DtCrt[0] = 0;
			//}
			
			STextTimeShow.FontSize = 24;//816;
			STextTimeShow.TextDraw();
		}
		InnerCan.TaskSecondUpdate();
		ProtoC2C.TaskSecondUpdate();
	}	
}

void PreventEventShow()
{
	EventShow.KeyJab();
}

void LoopCheck1S()
{
	SysLoopTick = 0;
	if(sys_tick_1s_rdy){
		sys_tick_1s_rdy = 0;
		UART7->TDR = KEY_UART_HEAD;
		UART7->TDR = 0x42;
		UART7->TDR = 0x04;
		UART7->TDR = 0xAA;
		UART7->TDR = 0x55;
		UART7->TDR = 0x00;
		UART7->TDR = 0x00;
		unsigned char check_sum = KEY_UART_HEAD+0x42+0x04+0xAA+0x55;
		UART7->TDR = check_sum;
		UART7->TDR = KEY_UART_END;
		CtlCenter.PrcBroadcastAlternating();
		DoUpdataRtc();
		TaskTimeUpData();
	}
	PreventEventShow();
}

void InspecDataInit()
{
	int i;
	InspecData.tick = 0;
	InspecData.IsStartCheck = 0;
	for(i=0; i<dPORT_MAX_COUNT; i++){
		InspecData.ErrPort[i] = 0;
	}
	for(i=0; i<dHANDCTL_MAX_COUNT; i++){
		InspecData.ErrBusPad[i] = 0;
	}
	for(i=0; i<dDIRECTCTL_MAX_COUNT ; i++){
		InspecData.ErrDirectPad[i] = 0;
	}
	InspecData.ErrKeyBoard = 0;
	InspecData.ErrExStroge = 0;
	
	for(i=0; i<dPORT_MAX_COUNT; i++){
		InspecData.Port[i] = 0;
	}
	for(i=0; i<dHANDCTL_MAX_COUNT; i++){
		InspecData.BusPad[i] = 0;
	}
	for(i=0; i<dDIRECTCTL_MAX_COUNT ; i++){
		InspecData.DirectPad[i] = 0;
	}
	InspecData.KeyBoard = 0;
	InspecData.ExStroge = 0;
}

void InspecDataRestart()
{
	int i;
	InspecData.tick = 0;
	for(i=0; i<dPORT_MAX_COUNT; i++){
		InspecData.Port[i] = 0;
	}
	for(i=0; i<dHANDCTL_MAX_COUNT; i++){
		InspecData.BusPad[i] = 0;
	}
	for(i=0; i<dDIRECTCTL_MAX_COUNT ; i++){
		InspecData.DirectPad[i] = 0;
	}
	InspecData.KeyBoard = 0;
	InspecData.ExStroge = 0;
}

void RuntimeDataInit()
{
	int i;
	CtlCenter.MsgReset();
	for(i=0; i<dInnerCanRxBufMaxCount; i++){
		InnerCanRxBuf.Body[i].Ide.D32 = 0;
		InnerCanRxBuf.Body[i].Len = 0;
		InnerCanRxBuf.Body[i].Da[0] = 0;
		InnerCanRxBuf.Body[i].Da[1] = 0;
		InnerCanRxBuf.Body[i].Da[2] = 0;
		InnerCanRxBuf.Body[i].Da[3] = 0;	
		InnerCanRxBuf.Body[i].Da[4] = 0;
		InnerCanRxBuf.Body[i].Da[5] = 0;
		InnerCanRxBuf.Body[i].Da[6] = 0;
		InnerCanRxBuf.Body[i].Da[7] = 0;
	}
	
	RunState =TRunModeCfg::RunModeRun;
	
	PsBatData.Body.HasUpdate = 0;
	KeyPadData.HasUpdate = 0;
	
	for(i=0;i<20;i++)NwSelfCheckTick[i] = 0;
	NwIsNormalCheck =0;
	
	KeyPad.Init();
	DevPrinter.Init();
	Crt.Init();
	GeneralDev.Init();
	
	InspecDataRestart();
	
	//RtUserCode.Init();

	PsBatData.Body.PsVoltageCh1.D16 	= 0;
	PsBatData.Body.PsVoltageCh2.D16	= 0;
	PsBatData.Body.BkVoltage.D16		= 0;
	PsBatData.Body.BatVoltage1.D16	= 0;
	PsBatData.Body.BatVoltage2.D16	= 0;
	PsBatData.Body.BatEnerger1		= 0;
	PsBatData.Body.BatEnerger2		= 0;
	
	PowerPrmValue.Sub1.D32[0] =0;
	PowerPrmValue.Sub1.D32[1] =0;
	PowerPrmValue.Sub1.D32[2] =0;
	PowerPrmValue.Sub1.D32[3] =0;
	PowerPrmValue.Sub1.D32[4] =0;
	
	PowerPrmValue.Sub2.D32[0] =0;
	PowerPrmValue.Sub2.D32[1] =0;
	PowerPrmValue.Sub2.D32[2] =0;
	PowerPrmValue.Sub2.D32[3] =0;
	PowerPrmValue.Sub2.D32[4] =0;
	
	BroadcastAlramAlternating.Active =0;
	BroadcastAlramAlternating.Sync =0;
	BroadcastAlramAlternating.Tick =0;
	BroadcastAlramAlternating.InBroadcast =0;
}

void SelfCheckCountDown()
{
	int i;
	NwIsSelfCheck = 0;
	if(MainCtl.fData.Split.NetworkMode == 1){
		for(i=1; i<=dNETWORK_MACHINE_COUNT; i++){
			if(NwSelfCheckTick[i]){
				NwSelfCheckTick[i]--;
				NwIsSelfCheck = 1;
			}
		}
	}
}

void RtManualPushStartMsg(unsigned char aType, unsigned char SubType, unsigned int aPath)
{
	unsigned char aP0;
	
	aIm.Splite.Year 		= DateTime.Date.Split.Year;
	aIm.Splite.Month 		= DateTime.Date.Split.Month;
	aIm.Splite.Day 		= DateTime.Date.Split.Day;
	aIm.Splite.Hour 		= DateTime.Time.Split.Hour;
	aIm.Splite.Minute 	= DateTime.Time.Split.Minute;
	aIm.Splite.Second 	= DateTime.Time.Split.Second;
	aIm.D32[2] = aPath;
	aIm.Splite.mType = etLinkageStart;
	aIm.Splite.dType = aType;
	//SubType 1:Start    2:AreaStart  5:ManualStart	 6:ManualAreaStart
	aIm.Splite.iByte0 = SubType;
	aIm.Splite.Data1 = 0;			
	CtlCenter.ieManualPushOneMsgLinkageStart(aIm);
	ExRecord.SetStateCodeStart();
	FecBusCRT.SetStateCodeStart();
	/////////////////////EventShow.MsgStartAddCountDownItem(aIm.D32[2]);
}

/*
void RtManualPushStartCancelMsg(unsigned char aType, unsigned char SubType, unsigned int aPath)
{
	ManualEventMsgAdd.mType = etLinkageStartCancel; 
	//SubType 1:Start    2:AreaStart
	ManualEventMsgAdd.SubType = SubType;
	ManualEventMsgAdd.Path = aPath;

				aIm.Splite.Year 		= DateTime.Date.Split.Year;
				aIm.Splite.Month 		= DateTime.Date.Split.Month;
				aIm.Splite.Day 		= DateTime.Date.Split.Day;
				aIm.Splite.Hour 		= DateTime.Time.Split.Hour;
				aIm.Splite.Minute 	= DateTime.Time.Split.Minute;
				aIm.Splite.Second 	= DateTime.Time.Split.Second;
				
				aIm.D32[2] = ManualEventMsgAdd.Path;
				aIm.Splite.mType = ManualEventMsgAdd.mType;
				aIm.Splite.dType = ManualEventMsgAdd.dType;
				//SubType 1:Start    2:AreaStart
				aIm.Splite.iByte0 =  ManualEventMsgAdd.SubType;
				aIm.Splite.Data1 = 0;
				//CtlCenter.ieManualPushOneMsgLinkageStartCancel(sIm);

	IsEvtntAdd = 1;
	while(IsEvtntAdd == 1);
}*/

void RtManualPushFbMissMsg(unsigned char aType, unsigned int aPath)
{
	aIm.Splite.Year 		= DateTime.Date.Split.Year;
	aIm.Splite.Month 		= DateTime.Date.Split.Month;
	aIm.Splite.Day 		= DateTime.Date.Split.Day;
	aIm.Splite.Hour 		= DateTime.Time.Split.Hour;
	aIm.Splite.Minute 	= DateTime.Time.Split.Minute;
	aIm.Splite.Second 	= DateTime.Time.Split.Second;
	aIm.D32[2] = aPath;
	aIm.Splite.mType = etLinkageFeedBack;
	aIm.Splite.dType = aType;
	//SubType 1:Start    2:AreaStart
	aIm.Splite.iByte0 = 0x04;
				
	CtlCenter.ieManualPushOneMsgLinkageFb(aIm);
}

void InitAllNetworkEpData()
{
	/*int i,x,e;
	unsigned int sAddr;
	for(i=0;i<dNETWORK_MACHINE_COUNT; i++){
		for(x=0; x<dPORT_MAX_COUNT; x++){
			sAddr = dAddrSdRam_NetEpState + (i * dNetStateCtlUsingByte) + (x * dNetStateCtlPortUsingByte);
			for(e=0; e<128; e++){
				*(volatile unsigned int *)(sAddr + e * 4) = 0;
			}
		}
	}*/
}

unsigned char GetNetworkEpState(unsigned char aCtlNum, unsigned char aP0, unsigned char aP1)
{
	/*unsigned char Pn,P0,P1;
	unsigned int aAddr;
	unsigned int iPn, iP0, iP1;
	
	Pn = aCtlNum;
	P0 = aP0;
	P1 = aP1;
	Pn--;
	P0--;
	iPn = Pn; iP0 =P0; iP1 =P1;
	aAddr = dAddrSdRam_NetEpState + (iPn * dNetStateCtlUsingByte) + (iP0 * dNetStateCtlPortUsingByte) + (iP1 * 2); 
	return *(volatile unsigned char *)(aAddr);*/
	return 0;
}

void SetNetworkEpState(unsigned char aCtlNum, unsigned char aP0, unsigned char aP1, unsigned char aSta)
{
	/*
	unsigned char Pn,P0,P1;
	unsigned int aAddr;
	unsigned int iPn, iP0, iP1;
	
	Pn = aCtlNum;
	P0 = aP0;
	P1 = aP1;
	Pn--;
	P0--;
	iPn = Pn; iP0 =P0; iP1 =P1;
	aAddr = dAddrSdRam_NetEpState + (iPn * dNetStateCtlUsingByte) + (iP0 * dNetStateCtlPortUsingByte) + (iP1 * 2); 
	*(volatile unsigned char *)(aAddr) = aSta;*/
}

unsigned char GetNetworkEpDType(unsigned char aCtlNum, unsigned char aP0, unsigned char aP1)
{
	unsigned char Pn,P0,P1;
	unsigned int aAddr;
	unsigned int iPn, iP0, iP1;
	
	Pn = aCtlNum;
	P0 = aP0;
	P1 = aP1;
	Pn--;
	P0--;
	iPn = Pn; iP0 =P0; iP1 =P1;
	aAddr = dAddrSdRam_NetEpState + (iPn * dNetStateCtlUsingByte) + (iP0 * dNetStateCtlPortUsingByte) + (iP1 * 2 +1); 
	return *(volatile unsigned char *)(aAddr);
}

void SetNetworkEpDType(unsigned char aCtlNum, unsigned char aP0, unsigned char aP1, unsigned char dType)
{
	unsigned char Pn,P0,P1;
	unsigned int aAddr;
	unsigned int iPn, iP0, iP1;
	
	Pn = aCtlNum;
	P0 = aP0;
	P1 = aP1;
	Pn--;
	P0--;
	iPn = Pn; iP0 =P0; iP1 =P1;
	aAddr = dAddrSdRam_NetEpState + (iPn * dNetStateCtlUsingByte) + (iP0 * dNetStateCtlPortUsingByte) + (iP1 * 2 +1); 
	*(volatile unsigned char *)(aAddr) = dType;
}

unsigned short GetNetworkEpData(unsigned char aCtlNum, unsigned char aP0, unsigned char aP1)
{
	/*unsigned char Pn,P0,P1;
	unsigned int aAddr;
	unsigned int iPn, iP0, iP1;
	
	Pn = aCtlNum;
	P0 = aP0;
	P1 = aP1;
	Pn--;
	P0--;
	iPn = Pn; iP0 =P0; iP1 =P1;
	aAddr = dAddrSdRam_NetEpState + (iPn * dNetStateCtlUsingByte) + (iP0 * dNetStateCtlPortUsingByte) + (iP1 * 2); 
	return *(volatile unsigned short *)(aAddr);*/
	return 0;
}

void SetNetworkEpData(unsigned char aCtlNum, unsigned char aP0, unsigned char aP1, unsigned short aData)
{
	/*unsigned char Pn,P0,P1;
	unsigned int aAddr;
	unsigned int iPn, iP0, iP1;
	
	Pn = aCtlNum;
	P0 = aP0;
	P1 = aP1;
	Pn--;
	P0--;
	iPn = Pn; iP0 =P0; iP1 =P1;
	aAddr = dAddrSdRam_NetEpState + (iPn * dNetStateCtlUsingByte) + (iP0 * dNetStateCtlPortUsingByte) + (iP1 * 2); 
	*(volatile unsigned short *)(aAddr) = aData;*/	
}

void ResetOneNetworkEpState(unsigned int aCtlNum)
{
	/*int i,x,e;
	unsigned int ClearValue;
	unsigned int Cv;
	unsigned int sAddr;
	
	if( (aCtlNum == 0) || (aCtlNum > dNETWORK_MACHINE_COUNT) ) return;
	
	Cv = 0;
	Cv |= (dEpStateBitFire + dEpStateBitStart + dEpStateBitFeedBack + dEpStateBitFbMiss + dEpStateBitFault + dEpStateBitSv + dEpStateBitDelayStart);
	Cv <<= 16;
	Cv |= (dEpStateBitFire + dEpStateBitStart + dEpStateBitFeedBack + dEpStateBitFbMiss + dEpStateBitFault + dEpStateBitSv + dEpStateBitDelayStart);
	Cv |= 0xFF00FF00;
	ClearValue = 0xFFFFFFFF;
	ClearValue &= ~Cv;
	i = aCtlNum-1;
		for(x=0; x<dPORT_MAX_COUNT; x++){
			sAddr = dAddrSdRam_NetEpState + (i * dNetStateCtlUsingByte) + (x * dNetStateCtlPortUsingByte);
			for(e=0; e<=127; e++){
				Cv = *(volatile unsigned int *)(sAddr + e * 4);
				Cv &= ClearValue;
				*(volatile unsigned int *)(sAddr + e * 4) = Cv;
			}
		}*/
	
}

void ResetAllNetworkEpState()
{
	int i,x,e;
	unsigned int ClearValue;
	unsigned int Cv;
	unsigned int sAddr;
	Cv = 0;
	Cv |= (dEpStateBitFire + dEpStateBitStart + dEpStateBitFeedBack + dEpStateBitFbMiss + dEpStateBitFault + dEpStateBitSv + dEpStateBitDelayStart);
	Cv <<= 16;
	Cv |= (dEpStateBitFire + dEpStateBitStart + dEpStateBitFeedBack + dEpStateBitFbMiss + dEpStateBitFault + dEpStateBitSv + dEpStateBitDelayStart);
	Cv |= 0xFF00FF00;
	ClearValue = 0xFFFFFFFF;
	ClearValue &= ~Cv;
	for(i=0;i<dNETWORK_MACHINE_COUNT; i++){
		for(x=0; x<dPORT_MAX_COUNT; x++){
			sAddr = dAddrSdRam_NetEpState + (i * dNetStateCtlUsingByte) + (x * dNetStateCtlPortUsingByte);
			for(e=0; e<=127; e++){
				//Cv = *(volatile unsigned int *)(sAddr + e * 4);
				//Cv &= ClearValue;
				//*(volatile unsigned int *)(sAddr + e * 4) = Cv;
			}
		}
	}		
}

int CheckIsFrozeScreen()
{
	if( ((Register.IsOnShow == 0) && (Register.IsOnGo == 0)) && (LiteSelfCheck.IsShowing == 0) )return 0;
	else return 1;
}

int Conv2Utf8Text(TRecordCommon aRC)
{
	volatile unsigned int aAddr ,i;
	unsigned int aCn, aP0, aP1;
		
	for(i=0; i<9; i++){TextAnsi.D32[i] = 0;}
	
	if(aRC.Splite.PCtlNum == MainCtl.fData.Split.MyNum){
		aP0 = aRC.Splite.P0;
		aP1 = aRC.Splite.P1;
		if(aP0 && aP1){
			if(aP0 <= dPORT_MAX_COUNT){
				aP0--;
				aP1--;
			}else
			if( (aP0 >= dADDR_DIRECTPAD_START) && (aP0 <= dADDR_DIRECTPAD_END) ){
				aP0 = aP0 - dADDR_DIRECTPAD_START;
				aP1 = aP0 * dDIRECTCTL_KEY_MAX_COUNT + (aP1);
				aP0 = 40;
			}
			aAddr = dAddrSdRam_Descp + (aP0 * 256 * 32) + (aP1 * 32);
			for(i=0; i<8; i++){
				TextAnsi.D32[i] = *(volatile unsigned int *)(aAddr + i *4);
			}
		}
	}else
	if( (aRC.Splite.PCtlNum) && (aRC.Splite.PCtlNum <= dNETWORK_MACHINE_COUNT) ){
		aCn = aRC.Splite.PCtlNum -1;
		aP0 = aRC.Splite.P0;
		aP1 = aRC.Splite.P1;
		if(aP0 && aP1){
			if(aP0 <= dPORT_MAX_COUNT){
				aP0--;
			}else
			if( (aP0 >= dADDR_DIRECTPAD_START) && (aP0 <= dADDR_DIRECTPAD_END) ){
				aP0 = aP0 - dADDR_DIRECTPAD_START;
				aP1 = aP0 * dDIRECTCTL_KEY_MAX_COUNT + (aP1);
				aP0 = 40;
			}
			aAddr = dAddrSdRam_DescpNw + (aCn*dONE_CTRL_DESCP_BYTES)+(aP0*8192) + (aP1*32);
			for(i=7; i<15; i++){
				TextAnsi.D32[i] = *(volatile unsigned int *)(aAddr + (i-7) *4);
			}
		}				
	}
	
	return CP936ToUTF8(TextAnsi.Text, TextUtf8.Text);
}

int Check4ModifiedDate2ClearLastMaintain(unsigned char aYear, unsigned char aMonth, unsigned char aDay)
{
	TDMix Now, Prv;
	Now.D8[0] = aDay;
	Now.D8[1] = aMonth;
	Now.D8[2] = aYear;
	Now.D8[3] = 0x20;
	
	Prv.D8[0] = LastMaintain.Day;
	Prv.D8[1] = LastMaintain.Month;
	Prv.D8[2] = LastMaintain.Year;
	Prv.D8[3] = 0x20;
	
	if(Now.D32 < Prv.D32){
		return 1;
	}else{
		return 0;
	}
}

void Check4MaintainToShow()
{
	TDMix16 Now;
	TDMix16 tChk, tMt;

	int Active =0;
	if(MaintainData.fDa.IsCustomTime){
		if(LastMaintain.Year < (DateTime.Date.Split.Year-1)){
			//Make Active
			IsMaintainTip = 1;
			return;
		}else
		if(LastMaintain.Year < DateTime.Date.Split.Year){
			//Check 3 target
			tMt.D8[0] = LastMaintain.Day;
			tMt.D8[1] = LastMaintain.Month;
			
			tChk.D8[0] = MaintainData.fDa.Day0; 
			tChk.D8[1] = MaintainData.fDa.Month0;
			if(tMt.D16 < tChk.D16){
				IsMaintainTip = 1;
				return;
			}
			tChk.D8[0] = MaintainData.fDa.Day1; 
			tChk.D8[1] = MaintainData.fDa.Month1;
			if(tMt.D16 < tChk.D16){
				IsMaintainTip = 1;
				return;
			}
			tChk.D8[0] = MaintainData.fDa.Day2; 
			tChk.D8[1] = MaintainData.fDa.Month2;
			if(tMt.D16 < tChk.D16){
				IsMaintainTip = 1;
				return;
			}
		}else{
			//
			Now.D8[0] = DateTime.Date.Split.Day;
			Now.D8[1] = DateTime.Date.Split.Month;
			tMt.D8[0] = LastMaintain.Day;
			tMt.D8[1] = LastMaintain.Month;
			tChk.D8[0] = MaintainData.fDa.Day0; 
			tChk.D8[1] = MaintainData.fDa.Month0;
			if(Now.D16 >= tChk.D16){
				if(tMt.D16 < tChk.D16){
					IsMaintainTip = 1;
					return;
				}
			}
			tChk.D8[0] = MaintainData.fDa.Day1; 
			tChk.D8[1] = MaintainData.fDa.Month1;
			if(Now.D16 >= tChk.D16){
				if(tMt.D16 < tChk.D16){
					IsMaintainTip = 1;
					return;
				}
			}
			tChk.D8[0] = MaintainData.fDa.Day2; 
			tChk.D8[1] = MaintainData.fDa.Month2;
			if(Now.D16 >= tChk.D16){
				if(tMt.D16 < tChk.D16){
					IsMaintainTip = 1;
					return;
				}
			}
		}
	}else{
		if(MaintainData.fDa.IsHalfYear){
			if(LastMaintain.Year < (DateTime.Date.Split.Year-1)){
				//Make Active
				IsMaintainTip = 1;
				return;
			}else
			if(LastMaintain.Year < DateTime.Date.Split.Year){
				//Check 3 target
				tMt.D8[0] = LastMaintain.Day;
				tMt.D8[1] = LastMaintain.Month;
				
				tChk.D8[0] = 0x05; 
				tChk.D8[1] = 0x01;
				if(tMt.D16 < tChk.D16){
					IsMaintainTip = 1;
					return;
				}
				tChk.D8[0] = 0x05; 
				tChk.D8[1] = 0x07;
				if(tMt.D16 < tChk.D16){
					IsMaintainTip = 1;
					return;
				}
			}else{
				//
				Now.D8[0] = DateTime.Date.Split.Day;
				Now.D8[1] = DateTime.Date.Split.Month;
				tMt.D8[0] = LastMaintain.Day;
				tMt.D8[1] = LastMaintain.Month;
				tChk.D8[0] = 0x05; 
				tChk.D8[1] = 0x01;
				if(Now.D16 >= tChk.D16){
					if(tMt.D16 < tChk.D16){
						IsMaintainTip = 1;
						return;
					}
				}
				tChk.D8[0] = 0x05; 
				tChk.D8[1] = 0x07;
				if(Now.D16 >= tChk.D16){
					if(tMt.D16 < tChk.D16){
						IsMaintainTip = 1;
						return;
					}
				}
			}			
		}else{
			if(LastMaintain.Year < (DateTime.Date.Split.Year-1)){
				//Make Active
				IsMaintainTip = 1;
				return;
			}else
			if(LastMaintain.Year < DateTime.Date.Split.Year){
				//Check 3 target
				tMt.D8[0] = LastMaintain.Day;
				tMt.D8[1] = LastMaintain.Month;
				tChk.D8[0] = 0x05; 
				tChk.D8[1] = 0x07;
				if(tMt.D16 < tChk.D16){
					IsMaintainTip = 1;
					return;
				}
			}else{
				//
				Now.D8[0] = DateTime.Date.Split.Day;
				Now.D8[1] = DateTime.Date.Split.Month;
				tMt.D8[0] = LastMaintain.Day;
				tMt.D8[1] = LastMaintain.Month;
				tChk.D8[0] = 0x05; 
				tChk.D8[1] = 0x07;
				if(Now.D16 >= tChk.D16){
					if(tMt.D16 < tChk.D16){
						IsMaintainTip = 1;
						return;
					}
				}
			}
		}
	}
	
	IsMaintainTip = 0;
}

void CommonDelayMs(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 CoverDateTimeIllegal(TRecordBody Src, TDateTimeBytes *Dst)
{
	unsigned char aValTens, aValOnes, aVal;
	if ((Src.Splite.Year / 16) > 9) goto Illegal_Here;
	if ((Src.Splite.Year % 16) > 9) goto Illegal_Here;
	
	aValTens = Src.Splite.Month / 16 *10;
	aValOnes = Src.Splite.Month % 16;
	if( aValOnes > 9) goto Illegal_Here;
	if( (aValTens + aValOnes) == 0) goto Illegal_Here;
	if( (aValTens + aValOnes) > 12) goto Illegal_Here;
	
	aValTens = Src.Splite.Day / 16 *10;
	aValOnes = Src.Splite.Day % 16;
	if( aValOnes > 9) goto Illegal_Here;
	aVal = aValTens + aValOnes;
	if(aVal == 0) goto Illegal_Here;
	if(aVal > 31) goto Illegal_Here;
	
	aValTens = Src.Splite.Hour / 16 *10;
	aValOnes = Src.Splite.Hour % 16;
	if( aValOnes > 9) goto Illegal_Here;
	aVal = aValTens + aValOnes;
	if( aVal > 23) goto Illegal_Here;
	
	aValTens = Src.Splite.Minute / 16 *10;
	aValOnes = Src.Splite.Minute % 16;
	if( aValOnes > 9) goto Illegal_Here;
	if( (aValTens + aValOnes) > 59) goto Illegal_Here;
	
	aValTens = Src.Splite.Second / 16 *10;
	aValOnes = Src.Splite.Second % 16;
	if( aValOnes > 9) goto Illegal_Here;
	if( (aValTens + aValOnes) > 59) goto Illegal_Here;
	
	Dst->Year 		= Src.Splite.Year;
	Dst->Month 		= Src.Splite.Month;
	Dst->Day 		= Src.Splite.Day;
	Dst->Hour 		= Src.Splite.Hour;
	Dst->Minute 	= Src.Splite.Minute;
	Dst->Second 	= Src.Splite.Second;
	return;
	
	Illegal_Here:
		Dst->Year = 0x22;
		Dst->Month = 0x01;
		Dst->Day = 0x01;
		Dst->Hour = 0x08;
		Dst->Minute = 0x10;
		Dst->Second = 0x10;
}


/*
void FireSetEpDayTime(unsigned char aCtlNum, unsigned char aP0, unsigned char aP1, unsigned int aDt)
{
	unsigned char Pn,P0,P1;
	unsigned int aAddr;
	unsigned int iPn, iP0, iP1;
	
	Pn = aCtlNum;
	P0 = aP0;
	P1 = aP1;
	Pn--;
	P0--;
	P1--;
	iPn = Pn; iP0 =P0; iP1 =P1;
	aAddr = dAddrSdram_FireDayTime + (iPn * dNetDayTimeCtlUsingByte) + (iP0 * dNetDayTimePortUsingByte) + (iP1 * 4); 
	*(volatile unsigned short *)(aAddr) = aDt;
}

unsigned int FireGetEpDayTime(unsigned char aCtlNum, unsigned char aP0, unsigned char aP1)
{
	unsigned char Pn,P0,P1;
	unsigned int aAddr;
	unsigned int iPn, iP0, iP1;
	
	Pn = aCtlNum;
	P0 = aP0;
	P1 = aP1;
	Pn--;
	P0--;
	P1--;
	iPn = Pn; iP0 =P0; iP1 =P1;
	aAddr = dAddrSdram_FireDayTime + (iPn * dNetDayTimeCtlUsingByte) + (iP0 * dNetDayTimePortUsingByte) + (iP1 * 4); 
	return *(volatile unsigned short *)(aAddr);
}

void FireSetEpYearMonth(unsigned char aCtlNum, unsigned char aP0, unsigned char aP1, unsigned int aDt)
{
	unsigned char Pn,P0,P1;
	unsigned int aAddr;
	unsigned int iPn, iP0, iP1;
	
	Pn = aCtlNum;
	P0 = aP0;
	P1 = aP1;
	Pn--;
	P0--;
	P1--;
	iPn = Pn; iP0 =P0; iP1 =P1;
	aAddr = dAddrSdram_FireYearMonth + (iPn * dNetYearMonthCtlUsingByte) + (iP0 * dNetYearMonthPortUsingByte) + (iP1 * 2); 
	*(volatile unsigned short *)(aAddr) = aDt;
}

unsigned int FireGetEpYearMonth(unsigned char aCtlNum, unsigned char aP0, unsigned char aP1)
{
	unsigned char Pn,P0,P1;
	unsigned int aAddr;
	unsigned int iPn, iP0, iP1;
	
	Pn = aCtlNum;
	P0 = aP0;
	P1 = aP1;
	Pn--;
	P0--;
	P1--;
	iPn = Pn; iP0 =P0; iP1 =P1;
	aAddr = dAddrSdram_FireYearMonth + (iPn * dNetYearMonthCtlUsingByte) + (iP0 * dNetYearMonthPortUsingByte) + (iP1 * 2); 
	return *(volatile unsigned short *)(aAddr);
}


void LinkageStartSetEpDayTime(unsigned char aCtlNum, unsigned char aP0, unsigned char aP1, unsigned int aDt)
{
	unsigned char Pn,P0,P1;
	unsigned int aAddr;
	unsigned int iPn, iP0, iP1;
	
	Pn = aCtlNum;
	P0 = aP0;
	P1 = aP1;
	Pn--;
	P0--;
	P1--;
	iPn = Pn; iP0 =P0; iP1 =P1;
	aAddr = dAddrSdram_LinkageStartDayTime + (iPn * dNetDayTimeCtlUsingByte) + (iP0 * dNetDayTimePortUsingByte) + (iP1 * 4); 
	*(volatile unsigned short *)(aAddr) = aDt;
}

unsigned int LinkageStartGetEpDayTime(unsigned char aCtlNum, unsigned char aP0, unsigned char aP1)
{
	unsigned char Pn,P0,P1;
	unsigned int aAddr;
	unsigned int iPn, iP0, iP1;
	
	Pn = aCtlNum;
	P0 = aP0;
	P1 = aP1;
	Pn--;
	P0--;
	P1--;
	iPn = Pn; iP0 =P0; iP1 =P1;
	aAddr = dAddrSdram_LinkageStartDayTime + (iPn * dNetDayTimeCtlUsingByte) + (iP0 * dNetDayTimePortUsingByte) + (iP1 * 4); 
	return *(volatile unsigned short *)(aAddr);
}

void LinkageStartSetEpYearMonth(unsigned char aCtlNum, unsigned char aP0, unsigned char aP1, unsigned int aDt)
{
	unsigned char Pn,P0,P1;
	unsigned int aAddr;
	unsigned int iPn, iP0, iP1;
	
	Pn = aCtlNum;
	P0 = aP0;
	P1 = aP1;
	Pn--;
	P0--;
	P1--;
	iPn = Pn; iP0 =P0; iP1 =P1;
	aAddr = dAddrSdram_LinkageStartYearMonth + (iPn * dNetYearMonthCtlUsingByte) + (iP0 * dNetYearMonthPortUsingByte) + (iP1 * 2); 
	*(volatile unsigned short *)(aAddr) = aDt;
}

unsigned int LinkageStartGetEpYearMonth(unsigned char aCtlNum, unsigned char aP0, unsigned char aP1)
{
	unsigned char Pn,P0,P1;
	unsigned int aAddr;
	unsigned int iPn, iP0, iP1;
	
	Pn = aCtlNum;
	P0 = aP0;
	P1 = aP1;
	Pn--;
	P0--;
	P1--;
	iPn = Pn; iP0 =P0; iP1 =P1;
	aAddr = dAddrSdram_LinkageStartYearMonth + (iPn * dNetYearMonthCtlUsingByte) + (iP0 * dNetYearMonthPortUsingByte) + (iP1 * 2); 
	return *(volatile unsigned short *)(aAddr);
}

void LinkageFbSetEpDayTime(unsigned char aCtlNum, unsigned char aP0, unsigned char aP1, unsigned int aDt)
{
	unsigned char Pn,P0,P1;
	unsigned int aAddr;
	unsigned int iPn, iP0, iP1;
	
	Pn = aCtlNum;
	P0 = aP0;
	P1 = aP1;
	Pn--;
	P0--;
	P1--;
	iPn = Pn; iP0 =P0; iP1 =P1;
	aAddr = dAddrSdram_LinkageFbDayTime + (iPn * dNetDayTimeCtlUsingByte) + (iP0 * dNetDayTimePortUsingByte) + (iP1 * 4); 
	*(volatile unsigned short *)(aAddr) = aDt;
}

unsigned int LinkageFbGetEpDayTime(unsigned char aCtlNum, unsigned char aP0, unsigned char aP1)
{
	unsigned char Pn,P0,P1;
	unsigned int aAddr;
	unsigned int iPn, iP0, iP1;
	
	Pn = aCtlNum;
	P0 = aP0;
	P1 = aP1;
	Pn--;
	P0--;
	P1--;
	iPn = Pn; iP0 =P0; iP1 =P1;
	aAddr = dAddrSdram_LinkageFbDayTime + (iPn * dNetDayTimeCtlUsingByte) + (iP0 * dNetDayTimePortUsingByte) + (iP1 * 4); 
	return *(volatile unsigned short *)(aAddr);
}

void LinkageFbSetEpYearMonth(unsigned char aCtlNum, unsigned char aP0, unsigned char aP1, unsigned int aDt)
{
	unsigned char Pn,P0,P1;
	unsigned int aAddr;
	unsigned int iPn, iP0, iP1;
	
	Pn = aCtlNum;
	P0 = aP0;
	P1 = aP1;
	Pn--;
	P0--;
	P1--;
	iPn = Pn; iP0 =P0; iP1 =P1;
	aAddr = dAddrSdram_LinkageFbYearMonth + (iPn * dNetYearMonthCtlUsingByte) + (iP0 * dNetYearMonthPortUsingByte) + (iP1 * 2); 
	*(volatile unsigned short *)(aAddr) = aDt;
}

unsigned int LinkageFbGetEpYearMonth(unsigned char aCtlNum, unsigned char aP0, unsigned char aP1)
{
	unsigned char Pn,P0,P1;
	unsigned int aAddr;
	unsigned int iPn, iP0, iP1;
	
	Pn = aCtlNum;
	P0 = aP0;
	P1 = aP1;
	Pn--;
	P0--;
	P1--;
	iPn = Pn; iP0 =P0; iP1 =P1;
	aAddr = dAddrSdram_LinkageFbYearMonth + (iPn * dNetYearMonthCtlUsingByte) + (iP0 * dNetYearMonthPortUsingByte) + (iP1 * 2); 
	return *(volatile unsigned short *)(aAddr);
}


void FaultSetEpDayTime(unsigned char aCtlNum, unsigned char aP0, unsigned char aP1, unsigned int aDt)
{
	unsigned char Pn,P0,P1;
	unsigned int aAddr;
	unsigned int iPn, iP0, iP1;
	
	Pn = aCtlNum;
	P0 = aP0;
	P1 = aP1;
	Pn--;
	P0--;
	P1--;
	iPn = Pn; iP0 =P0; iP1 =P1;
	aAddr = dAddrSdram_FaultDayTime + (iPn * dNetDayTimeCtlUsingByte) + (iP0 * dNetDayTimePortUsingByte) + (iP1 * 4); 
	*(volatile unsigned short *)(aAddr) = aDt;
}

unsigned int FaultGetEpDayTime(unsigned char aCtlNum, unsigned char aP0, unsigned char aP1)
{
	unsigned char Pn,P0,P1;
	unsigned int aAddr;
	unsigned int iPn, iP0, iP1;
	
	Pn = aCtlNum;
	P0 = aP0;
	P1 = aP1;
	Pn--;
	P0--;
	P1--;
	iPn = Pn; iP0 =P0; iP1 =P1;
	aAddr = dAddrSdram_FaultDayTime + (iPn * dNetDayTimeCtlUsingByte) + (iP0 * dNetDayTimePortUsingByte) + (iP1 * 4); 
	return *(volatile unsigned short *)(aAddr);
}

void FaultSetEpYearMonth(unsigned char aCtlNum, unsigned char aP0, unsigned char aP1, unsigned int aDt)
{
	unsigned char Pn,P0,P1;
	unsigned int aAddr;
	unsigned int iPn, iP0, iP1;
	
	Pn = aCtlNum;
	P0 = aP0;
	P1 = aP1;
	Pn--;
	P0--;
	P1--;
	iPn = Pn; iP0 =P0; iP1 =P1;
	aAddr = dAddrSdram_FaultYearMonth + (iPn * dNetYearMonthCtlUsingByte) + (iP0 * dNetYearMonthPortUsingByte) + (iP1 * 2); 
	*(volatile unsigned short *)(aAddr) = aDt;
}

unsigned int FaultGetEpYearMonth(unsigned char aCtlNum, unsigned char aP0, unsigned char aP1)
{
	unsigned char Pn,P0,P1;
	unsigned int aAddr;
	unsigned int iPn, iP0, iP1;
	
	Pn = aCtlNum;
	P0 = aP0;
	P1 = aP1;
	Pn--;
	P0--;
	P1--;
	iPn = Pn; iP0 =P0; iP1 =P1;
	aAddr = dAddrSdram_FaultYearMonth + (iPn * dNetYearMonthCtlUsingByte) + (iP0 * dNetYearMonthPortUsingByte) + (iP1 * 2); 
	return *(volatile unsigned short *)(aAddr);
}
*/

