//#include "Gui.h"
#include "RuntimeData.h"
#include "Runtime.h"
#include "server.h"
#include "rtc.h"

unsigned int Sys_1s_Rdy = 0;
unsigned int KeyRdy = 0;
unsigned char KeyValue;
unsigned char TimeUpdata = 0;


unsigned char NewBatState;

extern "C" unsigned int GetSystemErrorState(void)
{
	return IsSystemError;
}

extern "C" void Sys_1s_Load_Rdy(void)
{
	Sys_1s_Rdy = 1;
	Record.Task1000Ms();
}

extern "C" void LcdTopCover(unsigned int aClr)
{
	//LcdTopFill(aClr);
}

extern "C" void SystemDataTimeUpdata(u8 aY, u8 aM, u8 aD, u8 aH, u8 aMin, u8 aSec)
{
	DateTime.Date.Split.Year = aY;
	DateTime.Date.Split.Month = aM;
	DateTime.Date.Split.Day = aD;
	
	DateTime.Time.Split.Hour = aH;
	DateTime.Time.Split.Minute = aMin;
	DateTime.Time.Split.Second = aSec;
	
	TimeUpdata = 1;
}

extern "C" void LoadPsBatStateViaKeyPad(unsigned char *p)
{
	int i;
	for(i=0; i<12; i++){
		PsBatData.D8[i] = p[i];
	}
	PsBatData.Body.HasUpdate = 1;
}

extern "C" void LoadPsBatStateViaKeyPadSub1(unsigned char *p)
{
	int i;
	unsigned char ErrByte;
	ErrByte = p[0];
	for(i=0; i<12; i++){
		PowerPrmValue.Sub1.D8[i] = p[i+1];
	}
	if(ErrByte & 0x01){
		if(PowerPrmValue.Sub1.Body.PowerLineFault == 0){
			CtlCenter.ieAddDevFault(19, 1);
			PowerPrmValue.Sub1.Body.PowerLineFault =1;
		}
	}else{
		if(PowerPrmValue.Sub1.Body.PowerLineFault){
			CtlCenter.ieAddDevFaultCancel(19, 1);
			PowerPrmValue.Sub1.Body.PowerLineFault =0;
		}
	}
	
	if(ErrByte & 0x02){
		if(PowerPrmValue.Sub1.Body.BatFault == 0){
			CtlCenter.ieAddDevFault(20, 1);
			PowerPrmValue.Sub1.Body.BatFault =1;
		}
	}else{
		if(PowerPrmValue.Sub1.Body.BatFault){
			CtlCenter.ieAddDevFaultCancel(20, 1);
			PowerPrmValue.Sub1.Body.BatFault =0;
		}
	}
	
	if(ErrByte & 0x04){
		if(PowerPrmValue.Sub1.Body.OutputFault == 0){
			CtlCenter.ieAddDevFault(23, 1);
			PowerPrmValue.Sub1.Body.OutputFault =1;
		}
	}else{
		if(PowerPrmValue.Sub1.Body.OutputFault){
			CtlCenter.ieAddDevFaultCancel(23, 1);
			PowerPrmValue.Sub1.Body.OutputFault =0;
		}
	}
	
	if(ErrByte & 0x10){
		if(PowerPrmValue.Sub1.Body.Bat1VoltageLow == 0){
			CtlCenter.ieAddDevFault(26,1);
			PowerPrmValue.Sub1.Body.Bat1VoltageLow =1;
		}
	}else{
		if(PowerPrmValue.Sub1.Body.Bat1VoltageLow){
			CtlCenter.ieAddDevFaultCancel(26,1);
			PowerPrmValue.Sub1.Body.Bat1VoltageLow =0;
		}
	}
	
	if(ErrByte & 0x20){
		if(PowerPrmValue.Sub1.Body.Bat2VoltageLow == 0){
			CtlCenter.ieAddDevFault(27,1);
			PowerPrmValue.Sub1.Body.Bat2VoltageLow =1;
		}
	}else{
		if(PowerPrmValue.Sub1.Body.Bat2VoltageLow){
			CtlCenter.ieAddDevFaultCancel(27,1);
			PowerPrmValue.Sub1.Body.Bat2VoltageLow =0;
		}
	}
	
	if(ErrByte & 0x80){
		if(PowerPrmValue.Sub1.Body.OfflineFault == 0){
			CtlCenter.ieAddDevFault(21, 1);
			PowerPrmValue.Sub1.Body.OfflineFault =1;
		}
	}else{
		if(PowerPrmValue.Sub1.Body.OfflineFault){
			CtlCenter.ieAddDevFaultCancel(21, 1);
			PowerPrmValue.Sub1.Body.OfflineFault =0;
		}
	}
}

extern "C" void LoadPsBatStateViaKeyPadSub2(unsigned char *p)
{
	int i;
	unsigned char ErrByte;
	ErrByte = p[0];
	for(i=0; i<12; i++){
		PowerPrmValue.Sub2.D8[i] = p[i+1];
	}
	if(ErrByte & 0x01){
		if(PowerPrmValue.Sub2.Body.PowerLineFault == 0){
			CtlCenter.ieAddDevFault(19, 2);
			PowerPrmValue.Sub2.Body.PowerLineFault =1;
		}
	}else{
		if(PowerPrmValue.Sub2.Body.PowerLineFault){
			CtlCenter.ieAddDevFaultCancel(19, 2);
			PowerPrmValue.Sub2.Body.PowerLineFault =0;
		}
	}
	
	if(ErrByte & 0x02){
		if(PowerPrmValue.Sub2.Body.BatFault == 0){
			CtlCenter.ieAddDevFault(20, 2);
			PowerPrmValue.Sub2.Body.BatFault =1;
		}
	}else{
		if(PowerPrmValue.Sub2.Body.BatFault){
			CtlCenter.ieAddDevFaultCancel(20, 2);
			PowerPrmValue.Sub2.Body.BatFault =0;
		}
	}
	
	if(ErrByte & 0x04){
		if(PowerPrmValue.Sub2.Body.OutputFault == 0){
			CtlCenter.ieAddDevFault(23, 2);
			PowerPrmValue.Sub2.Body.OutputFault =1;
		}
	}else{
		if(PowerPrmValue.Sub2.Body.OutputFault){
			CtlCenter.ieAddDevFaultCancel(23, 2);
			PowerPrmValue.Sub2.Body.OutputFault =0;
		}
	}
	
	if(ErrByte & 0x10){
		if(PowerPrmValue.Sub2.Body.Bat1VoltageLow == 0){
			CtlCenter.ieAddDevFault(24,2);
			PowerPrmValue.Sub2.Body.Bat1VoltageLow =1;
		}
	}else{
		if(PowerPrmValue.Sub2.Body.Bat1VoltageLow){
			CtlCenter.ieAddDevFaultCancel(24,2);
			PowerPrmValue.Sub2.Body.Bat1VoltageLow =0;
		}
	}
	
	if(ErrByte & 0x20){
		if(PowerPrmValue.Sub2.Body.Bat2VoltageLow == 0){
			CtlCenter.ieAddDevFault(25,2);
			PowerPrmValue.Sub2.Body.Bat2VoltageLow =1;
		}
	}else{
		if(PowerPrmValue.Sub2.Body.Bat2VoltageLow){
			CtlCenter.ieAddDevFaultCancel(25,2);
			PowerPrmValue.Sub2.Body.Bat2VoltageLow =0;
		}
	}
	
	if(ErrByte & 0x80){
		if(PowerPrmValue.Sub2.Body.OfflineFault == 0){
			CtlCenter.ieAddDevFault(21, 2);
			PowerPrmValue.Sub2.Body.OfflineFault =1;
		}
	}else{
		if(PowerPrmValue.Sub2.Body.OfflineFault){
			CtlCenter.ieAddDevFaultCancel(21, 2);
			PowerPrmValue.Sub2.Body.OfflineFault =0;
		}
	}
}

extern "C" void KeyPadAskPowerCount()
{
	KeyPad.IsSendPowerSuplyCount = 1;
}

extern "C" void LoadBatStateViaKeyPad(unsigned char aBatSatte)
{
	KeyPadData.BatState 	= aBatSatte;
	PowerSuplyState 		= aBatSatte & 0x01;
	PowerBkState 		= aBatSatte & 0x02;
	PowerSuplyOutput		= aBatSatte & 0x04;
	BatState 			= aBatSatte & 0x40;
	PowerSuplyComm 		= aBatSatte & 0x80;
	KeyPadData.HasUpdate = 1;
}

extern "C" void SetOuterCanTxMsgState(unsigned char aSatte)
{
	OuterCanTxState = aSatte;
}

extern "C" void LoadOuterCanRxMsg(unsigned int aIde, unsigned char *p, unsigned char Len)
{
	unsigned int c;
	if(Len >8)return;
	ProtoC2C.iBuf.WriteIndex++;
	if(ProtoC2C.iBuf.WriteIndex >= dNetworkInBufMaxLength)ProtoC2C.iBuf.WriteIndex = 0;
	
	ProtoC2C.iBuf.Body[ProtoC2C.iBuf.WriteIndex].D32[0] = aIde;
	ProtoC2C.iBuf.Body[ProtoC2C.iBuf.WriteIndex].Data.Len = Len;
	ProtoC2C.iBuf.Body[ProtoC2C.iBuf.WriteIndex].Data.Identification 	= static_cast<unsigned char>((aIde >> 24) & 0x00000001);
	ProtoC2C.iBuf.Body[ProtoC2C.iBuf.WriteIndex].Data.Routing 		= static_cast<unsigned char>((aIde >> 25) & 0x00000003);
	ProtoC2C.iBuf.Body[ProtoC2C.iBuf.WriteIndex].Data.Priority 		= static_cast<unsigned char>((aIde >> 27) & 0x00000003);
	for(c=0; c<Len; c++){
		ProtoC2C.iBuf.Body[ProtoC2C.iBuf.WriteIndex].Data.Data[c] = *p++;
	}
	ProtoC2C.iBufInCount++;
}

extern "C" void LoadInnerCanRxMsg(unsigned int aIde, unsigned char *p, unsigned char Len)
{
	unsigned int c;
	if(Len >8)return;
	InnerCanRxBuf.WriteIndex++;
	if(InnerCanRxBuf.WriteIndex > (dInnerCanRxBufMaxCount -1))InnerCanRxBuf.WriteIndex = 0;
	
	InnerCanRxBuf.Body[InnerCanRxBuf.WriteIndex].Ide.D32 = aIde;
	InnerCanRxBuf.Body[InnerCanRxBuf.WriteIndex].Len = Len;
	for(c=0; c<Len; c++){
		InnerCanRxBuf.Body[InnerCanRxBuf.WriteIndex].Da[c] = *p++;
	}
}

extern "C" void LoadExRecordViaMemory(unsigned char *p)
{
	unsigned int i;
	if((*(p+1)) > 32)return;
	ExRecord.RxGetFlag = 1;
	ExRecord.RxMsg.D8[0] = *p;
	p++;
	ExRecord.RxMsg.D8[1] = *p;
	p++;
	for(i=0;i<ExRecord.RxMsg.DS.Len;i++){
		ExRecord.RxMsg.D8[i + 2] = *p;
		p++;
	}
	
	ExRecord.iBufProcess();
}

extern "C" void LoadDebugViaFECTest(unsigned char * p)
{
	unsigned char i,len;
	len = p[3] + 4;
	if(len > 20)return;
	FecStd.SetDebug.Flag = 1;
	for(i=0;i<len;i++){
		FecStd.SetDebug.Prm.D8[i] = p[i];
	}
}

extern "C" void LoadStdViaFECBus(unsigned char * p)
{
	unsigned int i;
	unsigned int len;
	unsigned int section;
	
	len = p[7];
	section = p[6];
	if((len > 8)||(section > 127))return;
	len+=8;
	
	if(section){
		FecStd.RxMsg.UsingMsgIndex = 1;
		FecStd.RxMsg.FrmCount = section;
	}else{
		FecStd.RxGetFlag = 1;
		FecStd.RxMsgState = ackRecDataCorrect;
	}
	
	for(i=0;i<len;i++){
		FecStd.RxMsg.body[section].D8[i] = p[i];
	}
	//crc16 and end(don't care)
	
	FecStd.iBufProcess();
}

extern "C" void SetRpyFECStdCRCError(void)
{
	//FecStd.RxGetFlag = 1;
	//FecStd.RxMsgState = ackCrcError;
}

extern "C" void LoadCRTViaFECBbus(unsigned char * p)
{
	unsigned int i;
	unsigned int len;
	unsigned int section;
	
	len = p[7];
	section = p[6];
	if((len > 8)||(section > 3))return;
	len+=8;
	
	if(section){
		FecBusCRT.RxMsg.Prm.UsingMsgIndex = 1;
	}else{
		FecBusCRT.RxGetFlag = 1;
		FecBusCRT.RxMsgState = ackRecDataCorrect;
	}
	
	for(i=0;i<len;i++){
		FecBusCRT.RxMsg.Prm.Section[section].da8[i] = p[i];
	}
	//crc16 and end
	FecBusCRT.RxMsg.Prm.Section[section].da8[16] = p[len];
	FecBusCRT.RxMsg.Prm.Section[section].da8[17] = p[len+1];
	FecBusCRT.RxMsg.Prm.Section[section].da8[18] = p[len+2];
	
	FecBusCRT.iBufProcess();
}

extern "C" void GetCfgViaUartRx(void)
{
	Config.uartRxFlag = 1;
}

extern "C" void GetWifiViaUartRx(void)
{
	MqttData.RxFlag = 1;
}

extern "C" void GetCfgViaUsbCdcRx(void)
{
	Config.usbCdcRxFlag = 1;
}

extern "C" unsigned int UsbConfigGetState(void)
{
	return Config.usbConState;
}

extern "C" void UsbConfigSetState(unsigned int aState)
{
	Config.usbConState = aState;
}

extern "C" void PrcDevPrinterState(int aOffline, int aNoPaper)
{
	DevPrinter.SetState(aOffline, aNoPaper);
}

extern "C" void CppGuiInit(void)
{
	RtDataInit();
	RuntimeDataInit();
	//Debug Run Here
	//Debug_data_init();
	//End Debug Run
	Service_Init();
	StartupPasswdGuiCheck();
	GuiStart();
	PageIdle.Show();
}

extern "C" void CppFreRun(void)
{
	Service_Gui();
	Service();
}

extern "C" void CppIn(void)
{
	static unsigned int i;
	
	//STextCaption.Show();
	//STextAdjust.Show();
	//STextAllowAuto.Show(); 
	//STextAllowManual.Show();
	//STextTimeShow.Show();
}

extern "C" void Cpp_1Ms_Task(void)
{
	Service_Time_1mS();	
}

extern "C" void Cpp_10Ms_Task(void)
{
	Service_Time_10mS();	
}

extern "C" void Cpp_100Ms_Task(void)
{
	Service_Time_100mS();	
}

extern "C" void KeyPadSilenceTickClear(void)
{
	KeyPad.SilenceTick = 0;
}

//板卡定义:
//0x01:显示板
//0x02:按键板
//0x03:存储单元
//0x04:多线盘
//0x05:总线盘
//0x06:回路母板
//0x07:回路子卡
//0x08:电源
//0x09:打印机
//0x0A:联网接收卡
extern "C" void GetHwInfo(unsigned char type, unsigned char isVer, unsigned char * pData)
{
	if((0x02 == type)||(0x08 == type)){
		if(isVer)
			HwInf.KeyPadUartPushData(cmQueryVer,pData);
		else
			HwInf.KeyPadUartPushData(cmQueryBoardModel,pData);
	}else
	if(0x03 == type){
		if(isVer)
			HwInf.ExtMemUartPushData(cmQueryVer,pData);
		else
			HwInf.ExtMemUartPushData(cmQueryBoardModel,pData);
	}else
	if(0x0A == type){
		if(isVer)
			HwInf.CommBrdUartPushData(cmQueryVer,pData);
		else
			HwInf.CommBrdUartPushData(cmQueryBoardModel,pData);
	}
}

void DataTimeSet(u8 aY, u8 aM, u8 aD, u8 aH, u8 aMin, u8 aSec, u8 aWk)
{
	//aWk 1..7
	RTC_DataTimeSet(aY, aM, aD, aH, aMin, aSec, aWk);
	TimeUpdata = 1;
}

extern "C" void GetPowerCount(unsigned char aMain, unsigned char aSub1, unsigned char aSub2, unsigned char dump)
{
	//Save Power Cnt
	PowerSuplyInf.SavePwrCount(aMain, aSub1, aSub2);
}















