#include "SlaveRegByRemote.h"
#include "gType.h"
#include "gvalue.h"
#include "ProtoC2C.h"
#include "Runtime.h"
#include "Gui.h"

extern "C"{
	#include "user_norflash.h"
	//#include "user_eeprom.h"
	#include "fdcan_task.h"
	#include "uart_key_drv.h"
}

void TSlaveRegByRemote::Init()
{
	IsShow = 0;
}

void TSlaveRegByRemote::sDelayMs(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 TSlaveRegByRemote::RenderBackGround(void)
{
	RectFillRender(ContentGeometry.Left, ContentGeometry.Top, ContentGeometry.Right, ContentGeometry.Bottom, ContentColor);
}

void TSlaveRegByRemote::RenderBackGround(unsigned int aClr)
{
	RectFillRender(ContentGeometry.Left, ContentGeometry.Top, ContentGeometry.Right, ContentGeometry.Bottom, aClr);
}

void TSlaveRegByRemote::DrawSelf(void)
{
	RenderBackGround();
	VertLineRender(Rect.Left,		Rect.Top, 		Rect.Height -1,	0xFFFFFFFF);
	VertLineRender(Rect.Right -1,		Rect.Top +1,	Rect.Height -2,	0xFF808080);
	VertLineRender(Rect.Right,		Rect.Top,		Rect.Height,	0xFF404040);
	
	HorizLineRender(Rect.Left, 		Rect.Top, 		Rect.Width - 1, 	0xFFFFFFFF);
	HorizLineRender(Rect.Left +1, 	Rect.Bottom -1,  	Rect.Width - 2, 	0xFF808080);
	HorizLineRender(Rect.Left,		Rect.Bottom,	Rect.Width,	0xFF404040);	
	RectFillRender(Rect.Left+1, Rect.Top+1, Rect.Right-2, Rect.Bottom-2, clFrmFace);
	
	TextRender_string24(Rect.Left + 10, Rect.Top +4, clNearWhite, clFrmFace, "正在执行集中机请求的设备注册");
}

unsigned char TSlaveRegByRemote::C2CGetData(unsigned char aSubCmd, unsigned char *pData)
{
	if(aSubCmd == CmdC2C_DevRegStartSeq){
		if(pData[3] == MainCtl.fData.Split.MyNum){
			if( pData[0] == 0 ){
				if( (pData[4] == TRegister::dNetDevRegCmdSeq[0][0]) && (pData[5] == TRegister::dNetDevRegCmdSeq[0][1]) && (pData[6] == TRegister::dNetDevRegCmdSeq[0][2]) && (pData[7] == TRegister::dNetDevRegCmdSeq[0][3]) ){
					StartSeqIdf = 0x01;
				}
			}
			if( pData[0] == 1 ){
				if( (pData[4] == TRegister::dNetDevRegCmdSeq[1][0]) && (pData[5] == TRegister::dNetDevRegCmdSeq[1][1]) && (pData[6] == TRegister::dNetDevRegCmdSeq[1][2]) && (pData[7] == TRegister::dNetDevRegCmdSeq[1][3])  ){
					if(StartSeqIdf == 1){
						StartSeqIdf |= 0x02;
					}
				}
			}
			if( pData[0] == 2 ){
				if( (pData[4] == TRegister::dNetDevRegCmdSeq[2][0]) && (pData[5] == TRegister::dNetDevRegCmdSeq[2][1]) && (pData[6] == TRegister::dNetDevRegCmdSeq[2][2]) && (pData[7] == TRegister::dNetDevRegCmdSeq[2][3])  ){
					if(StartSeqIdf == 3){
						StartSeqIdf = 0x04;
					}
				}
			}
			if( pData[0] == 3 ){
				if( (pData[4] == TRegister::dNetDevRegCmdSeq[3][0]) && (pData[5] == TRegister::dNetDevRegCmdSeq[3][1]) && (pData[6] == TRegister::dNetDevRegCmdSeq[3][2]) ){
					if(StartSeqIdf == 7){
						if(pData[7] < dPORT_MAX_COUNT){
							if(CheckIsRemoteRegFeasible()){
								//Start here
								return pData[7];
							}
						}
					}
				}
			}
		}
	}
	return 255;
}

void TSlaveRegByRemote::ReplyStartState(unsigned char aSta)
{
	ProtoC2C.TxMsg.Data.Routing =0;
	ProtoC2C.TxMsg.Data.Priority =1;
	ProtoC2C.TxMsg.Data.Identification =0;
	
	ProtoC2C.TxMsg.Data.Cmd = dNetworkCmd;
	ProtoC2C.TxMsg.Data.Len = 8;
	ProtoC2C.TxMsg.Data.Order = 0;

	ProtoC2C.TxMsg.Data.Data[0] = 0;
	ProtoC2C.TxMsg.Data.Data[1] = MainCtl.fData.Split.MyNum;
	ProtoC2C.TxMsg.Data.Data[2] = CmdC2C_DevRegRpyStart;
	ProtoC2C.TxMsg.Data.Data[3] = aSta;
	ProtoC2C.TxMsg.Data.Data[4] = 0;
	ProtoC2C.TxMsg.Data.Data[5] = 0;
	ProtoC2C.TxMsg.Data.Data[6] = 0;
	ProtoC2C.TxMsg.Data.Data[7] = 0;
	ProtoC2C.oBufInsert();
}

void TSlaveRegByRemote::Task1000Ms()
{
	
}





