/*******************************************************************************
* Function Name  : UDPServer_Internal_Protocol - 
* Description    : UDP服务器相关代码 包括发送机制
*******************************************************************************/
#include "includes.h"

UDP_SERVER_Information_t UDPServer_Info;

#define UDP_SERVER_Port		6125		//网络通讯端口

void UDP_Server_Info_Init(void)
{
	memset(&UDPServer_Info,0,sizeof(UDP_SERVER_Information_t));
	UDPServer_Info.frame = 0x8000;
	
	
}

/*******************************************************************************
* Function Name  : UDP_Server_Add_UUID
* Description    : 添加UUID - 16Byte
* Input          : 
	data	:UUID存放的指针
* Return         : None
*******************************************************************************/
void UDP_Server_Add_UUID(uint8_t *data)
{
	for(uint8_t i=0;i<16;i++)
	{
		data[i] = i;
	}
}

/*******************************************************************************
* Function Name  : UDP_Server_Data_To_Server
* Description    : UDP数据发送给服务器，并记录发送时间，重发使用
* Input          : 
	data	:需要打包的数据
	data_len:数据长度
* Return         : None
*******************************************************************************/
uint8_t UDP_Server_Data_To_Server(uint8_t *data,uint16_t data_len)
{
	uint8_t rev = 0;
	UINT32 send_len = data_len;
	
	rev = CH57xNET_SocketUdpSendTo(net_info.SocketId[0], data, &send_len, server_info.udp_ip, UDP_SERVER_Port);
	
	return rev;
}

/*******************************************************************************
* Function Name  : UDP_Server_Data_To_Server
* Description    : UDP数据发送给服务器，并记录发送时间，重发使用
* Input          : 
	data	:需要打包的数据
	data_len:数据长度
* Return         : None
*******************************************************************************/
uint8_t UDP_Server_Data_To_Server2(uint32_t data_addr,uint16_t data_len)
{
	uint8_t rev = 0;
	UINT32 send_len = data_len;
	uint8_t data[send_len];
	memset(data,0,sizeof(data));
	
	SRAM_Read_Buff(data,send_len,data_addr);
	
	//Dbg_Print_Buff(DBG_BIT_Debug_STATUS_bit,"Data Buff:",data,send_len);
	rev = CH57xNET_SocketUdpSendTo(net_info.SocketId[0], data, &send_len, server_info.udp_ip, UDP_SERVER_Port);
	
	return rev;
}

/*******************************************************************************
* Function Name  : UDP_Server_Data_To_Server
* Description    : UDP数据发送给服务器，并记录发送时间，重发使用
* Input          : 
	data	:需要打包的数据
	data_len:数据长度
* Return         : None
*******************************************************************************/
uint8_t UDP_Server_RetryData_To_Server(uint32_t data_addr,uint16_t data_len)
{
	uint8_t rev = 0;
	uint16_t crc_val = 0;
	UINT32 send_len = data_len;
	uint8_t data[send_len];
	memset(data,0,sizeof(data));
	
	SRAM_Read_Buff(data,send_len,data_addr);
	
	UDPServer_Info.retry_num ++;
	data[US_Retry] = UDPServer_Info.retry_num;
	
	crc_val = UDP_Data_Pack_CRC16(data,send_len);
	data[US_CRC_L] = crc_val & 0xFF;
	data[US_CRC_H] = (crc_val >> 8) & 0xFF;
	Dbg_Print_Buff(DBG_BIT_Debug_STATUS_bit,"Data Buff:",data,send_len);
	rev = CH57xNET_SocketUdpSendTo(net_info.SocketId[0], data, &send_len, server_info.udp_ip, UDP_SERVER_Port);
	
	return rev;
}


/*******************************************************************************
* Function Name  : UDP_Data_Put_In_SRAM_Buffer
* Description    : UDP数据放入到SRAM中，临时缓存(重发使用)
* Input          : 
	data	:需要打包的数据
	data_len:数据长度 - 数据长度不能超过550Byte
* Return         : 
	0x00：写入成功
	0x01：写入失败
*******************************************************************************/
uint8_t UDP_Data_Put_In_SRAM_Buffer(uint8_t *data,uint16_t data_len)
{
	if(data_len >= UDP_Server_DataLen_Max) return 0x01;
	uint32_t write_add = SRAM_Read_DW(SRAM_UDP_Server_WriteTXData_Addr);
	
	if((write_add < SRAM_UDP_Server_TXDataBuff_Start_Address) || (write_add > SRAM_UDP_Server_TXDataBuff_End_Address)) {
		write_add = SRAM_UDP_Server_TXDataBuff_Start_Address;
	}
	
	SRAM_Write_Word(data_len,write_add);
	SRAM_Write_Buff(data,data_len,write_add+2);
	
	write_add += SRAM_UDP_Server_DataBuff_Size;
	if(write_add >= SRAM_UDP_Server_TXDataBuff_End_Address) write_add = SRAM_UDP_Server_TXDataBuff_Start_Address;
	SRAM_Write_DW(write_add,SRAM_UDP_Server_WriteTXData_Addr);
	return 0x00;
}

/*******************************************************************************
* Function Name  : UDP_NetServer_Data_Packing
* Description    : UDP服务器协议数据打包函数并放入到发送缓存中
* Input          : 
	data_cmd:数据命令
	data	:需要打包的数据
	data_len:数据长度
* Return         : None
*******************************************************************************/
void UDP_NetServer_Data_Packing(uint16_t data_cmd,uint8_t *data,uint16_t data_len)
{
	uint16_t crc_val = 0;
	uint16_t temp_len = data_len+US_Para;
	uint8_t temp_data[550];
	memset(temp_data,0,sizeof(temp_data));
	
	if((UDPServer_Info.frame >= 0xFFFF) || (UDPServer_Info.frame < 0x8000)) 	UDPServer_Info.frame = 0x8000;
	
	temp_data[US_Head_L] = UDP_Server_Head_L;
	temp_data[US_Head_H] = UDP_Server_Head_H;
	temp_data[US_Len_L] = temp_len & 0xFF;
	temp_data[US_Len_H] = (temp_len >> 8) & 0xFF;
	temp_data[US_Frame_L] = UDPServer_Info.frame & 0xFF;
	temp_data[US_Frame_H] = (UDPServer_Info.frame >> 8) & 0xFF;
	temp_data[US_Retry] = 0;
	temp_data[US_Encrption] = UDPServer_Info.encrption;
	temp_data[US_MACAddr_1] = MACAddr[0];
	temp_data[US_MACAddr_2] = MACAddr[1];
	temp_data[US_MACAddr_3] = MACAddr[2];
	temp_data[US_MACAddr_4] = MACAddr[3];
	temp_data[US_MACAddr_5] = MACAddr[4];
	temp_data[US_MACAddr_6] = MACAddr[5];
	temp_data[US_CMD_L] = data_cmd & 0xFF;
	temp_data[US_CMD_H] = (data_cmd >> 8) & 0xFF;
	
	for(uint16_t i = 0;i<data_len;i++)
	{
		temp_data[US_Para + i] = data[i];
	}
	
	//CRC校验
	crc_val = UDP_Data_Pack_CRC16(temp_data,temp_len);
	temp_data[US_CRC_L] = crc_val & 0xFF;
	temp_data[US_CRC_H] = (crc_val >> 8) & 0xFF;
	
	Dbg_Println(DBG_BIT_Debug_STATUS_bit,"CRC:%04X,Frame:%04X,Len:%ld",crc_val,UDPServer_Info.frame,temp_len);
	Dbg_Print_Buff(DBG_BIT_Debug_STATUS_bit,"Data Buff:",temp_data,temp_len);
	UDPServer_Info.frame++;
	
	//将数据填入SRAM缓冲区中
	UDP_Data_Put_In_SRAM_Buffer(temp_data,temp_len);
}

/*******************************************************************************
* Function Name  : UDP_NetServer_ReplyData_Packing
* Description    : UDP服务器协议回复数据 - 按照协议封装包头，并校验CRC
* Input          : 
	data_cmd:数据命令
	frame	:回复帧号
	data	:需要打包的数据
	data_len:数据长度
* Return         : None
*******************************************************************************/
void UDP_NetServer_ReplyData_Packing(uint16_t data_cmd,uint16_t frame,uint8_t *data,uint16_t data_len)
{
	uint16_t crc_val = 0;

	data[US_Head_L] = UDP_Server_Head_L;
	data[US_Head_H] = UDP_Server_Head_H;
	data[US_Len_L] = data_len & 0xFF;
	data[US_Len_H] = (data_len >> 8) & 0xFF;
	data[US_Frame_L] = frame & 0xFF;
	data[US_Frame_H] = (frame >> 8) & 0xFF;
	data[US_Retry] = 0;
	data[US_Encrption] = UDPServer_Info.encrption;
	data[US_MACAddr_1] = MACAddr[0];
	data[US_MACAddr_2] = MACAddr[1];
	data[US_MACAddr_3] = MACAddr[2];
	data[US_MACAddr_4] = MACAddr[3];
	data[US_MACAddr_5] = MACAddr[4];
	data[US_MACAddr_6] = MACAddr[5];
	data[US_CMD_L] = data_cmd & 0xFF;
	data[US_CMD_H] = (data_cmd >> 8) & 0xFF;
	
	//CRC校验
	crc_val = UDP_Data_Pack_CRC16(data,data_len);
	data[US_CRC_L] = crc_val & 0xFF;
	data[US_CRC_H] = (crc_val >> 8) & 0xFF;
	
	Dbg_Println(DBG_BIT_Debug_STATUS_bit,"CRC:%04X,Frame:%04X",crc_val,frame,data_len);
	Dbg_Print_Buff(DBG_BIT_Debug_STATUS_bit,"Data Buff:",data,data_len);
}

/*******************************************************************************
* Function Name  : UDP_NetServer_Heartbeat_Packing
* Description    : UDP服务器协议 - 心跳包打包函数
* Input          : 
* Return         : None
*******************************************************************************/
void UDP_NetServer_Heartbeat_Packing(void)
{
	uint8_t buff[20];
	memset(buff,0,sizeof(buff));
	
	/*内网IP*/
	buff[0] = net_info.local_ip[0];
	buff[1] = net_info.local_ip[1];
	buff[2] = net_info.local_ip[2];
	buff[3] = net_info.local_ip[3];
	
	/*UUID 16Byte*/
	for(uint8_t i=0;i<16;i++)
	{
		buff[4 + i] = i;
	}
	
	Dbg_Println(DBG_BIT_Debug_STATUS_bit,"新服务器 - 心跳包发送");
	UDP_NetServer_Data_Packing(UDP_Server_CMD_Heartbeat,buff,20);
}



/*******************************************************************************
* Function Name  : UDP_NetServer_Task
* Description    : UDP服务器相关任务
* Input          : 
* Return         : None
*******************************************************************************/
void UDP_NetServer_Task(void)
{
	static uint32_t net_tick = 0;
	static uint32_t test_tick = 0;
	static uint32_t udptest_tick = 0;
	
	/*心跳包在没有接收到一段时间内，发送心跳包*/
	if((UDPServer_Info.test_flag == 0x00) && (SysTick_1ms - test_tick > 20000))
	{
		test_tick = SysTick_1ms;
		UDP_NetServer_Heartbeat_Packing();
	}else if((UDPServer_Info.test_flag == 0x01) && (SysTick_1ms - udptest_tick > UDPServer_Info.test_time)){
		udptest_tick = SysTick_1ms;
		Dbg_Println(DBG_BIT_Debug_STATUS_bit,"UDP_NetServer 测试数据发送 ");
		
	}
	
	switch(UDPServer_Info.send_state)
	{
		case USS_IDLE:
			if(SysTick_1ms - net_tick > 10)
			{
				net_tick = SysTick_1ms;
				
				uint32_t w_addr = SRAM_Read_DW(SRAM_UDP_Server_WriteTXData_Addr);
				uint32_t r_addr = SRAM_Read_DW(SRAM_UDP_Server_ReadTXData_Addr);
				
				if((w_addr < SRAM_UDP_Server_TXDataBuff_Start_Address) || (w_addr > SRAM_UDP_Server_TXDataBuff_End_Address)) {
					w_addr = SRAM_UDP_Server_TXDataBuff_Start_Address;
					SRAM_Write_DW(w_addr,SRAM_UDP_Server_WriteTXData_Addr);
				}
				if((r_addr < SRAM_UDP_Server_TXDataBuff_Start_Address) || (r_addr > SRAM_UDP_Server_TXDataBuff_End_Address)) {
					r_addr = SRAM_UDP_Server_TXDataBuff_Start_Address;
					SRAM_Write_DW(r_addr,SRAM_UDP_Server_ReadTXData_Addr);
				}
			
				if(w_addr != r_addr)	//当前缓冲区有数据没处理
				{
					Dbg_Println(DBG_BIT_Debug_STATUS_bit,"UDP_NetServer 缓冲区中有数据需要处理w:%08X r:%08X",w_addr,r_addr);
					UDPServer_Info.retry_num = 0;
					UDPServer_Info.send_state = USS_SEND;
				}
			
			}
			
			break;
		case USS_SEND:
		{
			Dbg_Println(DBG_BIT_Debug_STATUS_bit,"UDP_NetServer 数据发送 ");
			uint32_t r_addr = SRAM_Read_DW(SRAM_UDP_Server_ReadTXData_Addr);
			uint16_t data_len = 0;
			if((r_addr < SRAM_UDP_Server_TXDataBuff_Start_Address) || (r_addr > SRAM_UDP_Server_TXDataBuff_End_Address)) 
			{
				r_addr = SRAM_UDP_Server_TXDataBuff_Start_Address;
				SRAM_Write_DW(r_addr,SRAM_UDP_Server_ReadTXData_Addr);
			}
			
			data_len = SRAM_Read_Word(r_addr);
			
			if(data_len >= UDP_Server_DataLen_Max) 	//数据长度不对，这包数据直接丢弃
			{
				Dbg_Println(DBG_BIT_Debug_STATUS_bit,"UDP_NetServer 数据长度不对，这包数据直接丢弃");
				UDPServer_Info.send_state = USS_IDLE;
				r_addr += SRAM_UDP_Server_DataBuff_Size;
				if(r_addr >= SRAM_UDP_Server_TXDataBuff_End_Address) r_addr = SRAM_UDP_Server_TXDataBuff_Start_Address;
				SRAM_Write_DW(r_addr,SRAM_UDP_Server_ReadTXData_Addr);
				break;
			}
			
			UDP_Server_Data_To_Server2(r_addr+2,data_len);
			UDPServer_Info.send_tick = SysTick_1ms;
			
//			/*发送成功后读取地址归为*/
//			r_addr += SRAM_UDP_Server_DataBuff_Size;
//			if(r_addr >= SRAM_UDP_Server_TXDataBuff_End_Address) r_addr = SRAM_UDP_Server_TXDataBuff_Start_Address;
//			SRAM_Write_DW(r_addr,SRAM_UDP_Server_ReadTXData_Addr);
			
			UDPServer_Info.send_state = USS_SENDWAIT;
		}
			break;
		case USS_SENDWAIT:
			if(SysTick_1ms - UDPServer_Info.send_tick > UDP_Server_DataSendWait_Max)
			{
				if(UDPServer_Info.retry_num >= UDP_Server_DataRetry_Max)
				{
					
					/*发送成功后读取地址归为*/
					uint32_t r_addr = SRAM_Read_DW(SRAM_UDP_Server_ReadTXData_Addr);
					if((r_addr < SRAM_UDP_Server_TXDataBuff_Start_Address) || (r_addr > SRAM_UDP_Server_TXDataBuff_End_Address)) {
						r_addr = SRAM_UDP_Server_TXDataBuff_Start_Address;
					}

					r_addr += SRAM_UDP_Server_DataBuff_Size;
					if(r_addr >= SRAM_UDP_Server_TXDataBuff_End_Address) r_addr = SRAM_UDP_Server_TXDataBuff_Start_Address;
					SRAM_Write_DW(r_addr,SRAM_UDP_Server_ReadTXData_Addr);
					
					Dbg_Println(DBG_BIT_Debug_STATUS_bit,"UDP_NetServer 数据重发，已达上限 r_addr:%08X",r_addr);
					UDPServer_Info.send_state = USS_IDLE;
				}else {
					Dbg_Println(DBG_BIT_Debug_STATUS_bit,"UDP_NetServer 数据重发 %d",UDPServer_Info.retry_num);
					UDPServer_Info.send_state = USS_RETRY;
				}
				
			}
			break;
		case USS_RETRY:
			{
				uint32_t r_addr = SRAM_Read_DW(SRAM_UDP_Server_ReadTXData_Addr);
				uint16_t data_len = SRAM_Read_Word(r_addr);
				if((r_addr < SRAM_UDP_Server_TXDataBuff_Start_Address) || (r_addr > SRAM_UDP_Server_TXDataBuff_End_Address)) 
				{
					r_addr = SRAM_UDP_Server_TXDataBuff_Start_Address;
					SRAM_Write_DW(r_addr,SRAM_UDP_Server_ReadTXData_Addr);
				}
				if(data_len >= UDP_Server_DataLen_Max) 	//数据长度不对，这包数据直接丢弃
				{
					Dbg_Println(DBG_BIT_Debug_STATUS_bit,"UDP_NetServer 数据长度不对，这包数据直接丢弃");
					UDPServer_Info.send_state = USS_IDLE;
					r_addr += SRAM_UDP_Server_DataBuff_Size;
					if(r_addr >= SRAM_UDP_Server_TXDataBuff_End_Address) r_addr = SRAM_UDP_Server_TXDataBuff_Start_Address;
					SRAM_Write_DW(r_addr,SRAM_UDP_Server_ReadTXData_Addr);
					break;
				}
				
				UDP_Server_RetryData_To_Server(r_addr+2,data_len);
				UDPServer_Info.send_tick = SysTick_1ms;
				UDPServer_Info.send_state = USS_SENDWAIT;
			}
			break;
		default:
			UDPServer_Info.send_state = USS_IDLE;
			break;
	}
	
}

/*******************************************************************************
* Function Name  : Data_Conver_To_Uint16
* Description    : 将数组中的两位大端模式的数据转换为uint16_t的数据
* Input          : 
* Return         : None
*******************************************************************************/
uint16_t Data_Conver_To_Uint16(uint8_t *data)
{
	uint16_t temp_data = data[1];
	temp_data <<= 8;
	temp_data |= data[0];
	
	return temp_data;
}

/*******************************************************************************
* Function Name  : UDP_NetServer_Data_CheckCRC
* Description    : UDP服务器数据校验CRC
* Input          : 
* Return         : 
	0x00:CRC正确
	0x01:CRC错误
*******************************************************************************/
uint8_t UDP_NetServer_Data_CheckCRC(UINT8 *data, UINT32 len)
{
	uint16_t data_crc = 0;
	uint16_t crc_val = 0;
	
	data_crc = Data_Conver_To_Uint16(&data[US_CRC_L]);
	
	crc_val = UDP_Data_Pack_CRC16(data,len);
	
	if(crc_val == data_crc) return 0x00;
	
	return 0x01;
}

/*******************************************************************************
* Function Name  : UDP_Server_CMD_Search_Data_Processing
* Description    : UDP服务器命令处理 - 搜索命令处理
* Input          : 
* Return         : None
*******************************************************************************/
uint8_t UDP_Server_CMD_Search_Data_Processing(UINT8 *data, UINT32 len, uint8_t* ip, uint16_t port)
{
	uint16_t cmd_frame = 0;

	
	uint16_t back_len = 449 + US_Para;			//整包数据长度
	uint8_t back_data[back_len + 2];
	
	memset(back_data,0,sizeof(back_data));
	
	cmd_frame = data[US_Frame_H];
	cmd_frame <<= 8;
	cmd_frame |= data[US_Frame_L];
	
	if(cmd_frame >= 0x8000) {
		Dbg_Print(DBG_BIT_Debug_STATUS_bit,"搜索命令 帧号不对\r\n");
		return 0x01;
	}
	
	if(cmd_frame == UDPServer_Info.rev_frame)	{
		Dbg_Print(DBG_BIT_Debug_STATUS_bit,"搜索命令 帧号相同，不处理\r\n");
		return 0x001;
	}
	
	/*回复内容*/
	back_data[US_Para] = 0x00;
	memcpy(&back_data[US_Para + 1],MCU_TYPE,sizeof(MCU_TYPE));   	//机型编号
	memcpy(&back_data[US_Para + 17],net_info.local_ip,4);   		//IP地址
	memcpy(&back_data[US_Para + 21],server_info.dis_ip,4);   		//服务器IP地址
	memcpy(&back_data[US_Para + 25],net_info.mask,4);   			//子网掩码
	memcpy(&back_data[US_Para + 29],net_info.gwip_addr,4);   		//网关
	back_data[US_Para + 33] = (6000 + MACAddr[5]) & 0xFF;			
	back_data[US_Para + 34] = ((6000 + MACAddr[5]) >> 8) & 0xFF;
	memcpy(&back_data[US_Para + 37],DNS_SERVER_IP,4);   			//DNS地址
	UDP_ADD_SoftwareVer(&back_data[US_Para+41]);					//机型编号 - 软件版本号
	/*RTC时间*/
	back_data[US_Para+61] = HEX_Conversion_To_DEC(RTC_Raw_Data.year);
	back_data[US_Para+62] = HEX_Conversion_To_DEC(RTC_Raw_Data.month);
	back_data[US_Para+63] = HEX_Conversion_To_DEC(RTC_Raw_Data.day);
	back_data[US_Para+64] = HEX_Conversion_To_DEC(RTC_Raw_Data.hour);
	back_data[US_Para+65] = HEX_Conversion_To_DEC(RTC_Raw_Data.minute);
	back_data[US_Para+66] = HEX_Conversion_To_DEC(RTC_Raw_Data.second);
	
	/*Launcher版本 只有Launcher_C1F_V04 以上版本才可以读取到Launcher版本，以下版本是读取乱码*/
	MCU_Flash_Read((uint8_t *)&back_data[US_Para+67],20,Launcher_SoftwareVer_Addr);

	UDP_Add_Mac(&back_data[US_Para+87]);			//MAC地址
	SRAM_Read_Buff(&back_data[US_Para+93],4,SRAM_Register_Start_ADDRESS + Register_ProjectCode_OFFSET);		//项目编码
	SRAM_Read_Buff(&back_data[US_Para+97],4,SRAM_Register_Start_ADDRESS + Register_RoomNumber_OFFSET);
	SRAM_Read_Buff(&back_data[US_Para+101],4,SRAM_Register_Start_ADDRESS + Register_HouseType_OFFSET);
	SRAM_Read_Buff(&back_data[US_Para+105],4,SRAM_Register_Start_ADDRESS + Register_ConfigVersion_OFFSET);
	SRAM_Read_Buff(&back_data[US_Para+109],4,SRAM_Register_Start_ADDRESS + Register_RoomRent_OFFSET);
	SRAM_Read_Buff(&back_data[US_Para+113],4,SRAM_Register_Start_ADDRESS + Register_SeasonStatus_OFFSET);
	back_data[US_Para+117] = Get_Authorize_Lock_Status();		//获取授权状态
	SRAM_Read_Buff(&back_data[US_Para+121],4,SRAM_Register_Start_ADDRESS + Register_MandateUTC_OFFSET);
	SRAM_Read_Buff(&back_data[US_Para+125],4,SRAM_Register_Start_ADDRESS + Register_MandateExpiresTime_OFFSET);
	SRAM_Read_Buff(&back_data[US_Para+129],128,SRAM_Register_Start_ADDRESS + Register_RoomNumNote_OFFSET);
	Flash_Read(&back_data[US_Para+257],64,FLASH_MCU_Model_Revision_ADDRESS);
	Flash_Read(&back_data[US_Para+321],64,FLASH_MCU_Control_Revision_ADDRESS);
	//新增配置的酒店名称和房型名称 2022-07-13
	Read_LogicFile_Information(0x04,(uint8_t *)&back_data[US_Para+385]);	//查询LOGIC文件中的酒店别名 - 32Byte
	Read_LogicFile_Information(0x07,(uint8_t *)&back_data[US_Para+417]);	//查询LOGIC文件中的房型名称 - 32Byte

	
	//封装数据
	UDP_NetServer_ReplyData_Packing(UDP_Server_CMD_Search,cmd_frame,back_data,back_len);
	//发送数据
	UDP_Server_Data_To_Server(back_data,back_len);
	
	return 0x00;
}

/*******************************************************************************
* Function Name  : UDP_Server_CMD_Register_Data_Processing
* Description    : UDP服务器命令处理 - 注册命令处理(设置产品密钥)
* Input          : 
* Return         : None
*******************************************************************************/
uint8_t UDP_Server_CMD_Register_Data_Processing(UINT8 *data, UINT32 len, uint8_t* ip, uint16_t port)
{
	uint8_t rev = 0;
	uint8_t temp_data = 0;
	uint16_t cmd_frame = 0;
	uint16_t back_len = 272 + US_Para;	
	uint8_t back_data[back_len + 2];
	memset(back_data,0,sizeof(back_data));
	
	cmd_frame = data[US_Frame_H];
	cmd_frame <<= 8;
	cmd_frame |= data[US_Frame_L];
	/*判断帧号*/
	if(cmd_frame >= 0x8000) {
		Dbg_Print(DBG_BIT_Debug_STATUS_bit,"注册命令 帧号不对\r\n");
		return 0x01;
	}
	
	if(cmd_frame == UDPServer_Info.rev_frame)	{
		Dbg_Print(DBG_BIT_Debug_STATUS_bit,"注册命令 帧号相同，不处理\r\n");
		return 0x001;
	}
	
	UDPServer_Info.rev_frame = cmd_frame;
	
	/*数据内容 - MAC地址 4Byte + 产品密钥 64Byte*/
	
	/*判断数据内容中MAC地址是否正确*/
	for(temp_data=0;temp_data<6;temp_data++)
	{
		if(data[US_Para + temp_data] != MACAddr[temp_data]) break;
	}
	if(temp_data != 6) {
		Dbg_Print(DBG_BIT_Debug_STATUS_bit,"注册命令 MAC地址不对\r\n");
		rev = 0x02;
	}else {
		rev = 0x01;
	}
	
	/*数据处理 - 先校验MAC地址 MAC一致，便将产品密钥保存 64Byte*/
	if(rev == 0x01)
	{
		Flash_Write(&data[US_Para+6],64,FLASH_MCU_ProductKey_ADDRESS); 		//产品密钥 64Byte
	}
	
	/*回复内容*/
	back_data[US_Para] = rev;
	Flash_Read(&back_data[US_Para+1],64,FLASH_MCU_Model_Revision_ADDRESS);
	Flash_Read(&back_data[US_Para+65],64,FLASH_MCU_Control_Revision_ADDRESS);
	UDP_Add_Mac(&back_data[US_Para+129]);										//MAC地址
	UDP_Server_Add_UUID(&back_data[US_Para+135]);								//添加UUID
	Flash_Read(&back_data[US_Para+151],64,FLASH_MCU_ProductKey_ADDRESS); 		//产品密钥 64Byte
	
	//封装数据
	UDP_NetServer_ReplyData_Packing(UDP_Server_CMD_Search,cmd_frame,back_data,back_len);
	//发送数据
	UDP_Server_Data_To_Server(back_data,back_len);
	
	return 0x00;
}


/*******************************************************************************
* Function Name  : UDP_Server_CMD_Heartbeat_Data_Processing
* Description    : UDP服务器命令处理 - 心跳包命令回复处理
* Input          : 
* Return         : None
*******************************************************************************/
uint8_t UDP_Server_CMD_Heartbeat_ReplyData_Processing(UINT8 *data, UINT32 len, uint8_t* ip, uint16_t port)
{
	uint32_t test_temp = 0;
	S_RTC test_rtc;
	
	/*清除缓冲区中的数据*/
	uint32_t r_addr = SRAM_Read_DW(SRAM_UDP_Server_ReadTXData_Addr);
	if((r_addr < SRAM_UDP_Server_TXDataBuff_Start_Address) || (r_addr > SRAM_UDP_Server_TXDataBuff_End_Address)) 
	{
		/*缓冲区地址不再有效范围内*/
		r_addr = SRAM_UDP_Server_TXDataBuff_Start_Address;
		SRAM_Write_DW(r_addr,SRAM_UDP_Server_ReadTXData_Addr);
	}
	
	if((data[US_Frame_L] == SRAM_Read_Byte(r_addr + 2 + US_Frame_L)) && (data[US_Frame_H] == SRAM_Read_Byte(r_addr + 2 + US_Frame_H)))
	{
		/*帧号相同*/
		r_addr += SRAM_UDP_Server_DataBuff_Size;
		if(r_addr >= SRAM_UDP_Server_TXDataBuff_End_Address) r_addr = SRAM_UDP_Server_TXDataBuff_Start_Address;
		SRAM_Write_DW(r_addr,SRAM_UDP_Server_ReadTXData_Addr);
		UDPServer_Info.send_state = USS_IDLE;
		
		test_temp = SysTick_1ms - UDPServer_Info.send_tick;
		
		Dbg_Println(DBG_BIT_Debug_STATUS_bit,"新服务器 - 心跳包应答数据 r_addr:%08X 时间间隔:%dms",r_addr,test_temp);
	}else {
		Dbg_Println(DBG_BIT_Debug_STATUS_bit,"新服务器 - 心跳包应答数据 帧号不同%02X%02X - %02X%02X",data[US_Frame_H],data[US_Frame_L],SRAM_Read_Byte(r_addr + 2 + US_Frame_H),SRAM_Read_Byte(r_addr + 2 + US_Frame_L));
	}
	
	/*处理数据 UTC数据 4Byte + TFTP服务器IP 4Byte*/
	test_temp = data[US_Para + 3];
	test_temp <<= 8;
	test_temp |= data[US_Para + 2];
	test_temp <<= 8;
	test_temp |= data[US_Para + 1];
	test_temp <<= 8;
	test_temp |= data[US_Para + 0];
	
	Unix_Conversion_To_RTC(&test_rtc,test_temp);
	Dbg_Println(DBG_BIT_Debug_STATUS_bit,"UTC时间: %08X - 20%02X-%02X-%02X %02X:%02X:%02X %02X",test_temp,test_rtc.year,test_rtc.month,test_rtc.day,test_rtc.hour,test_rtc.minute,test_rtc.second,test_rtc.week);
	Dbg_Println(DBG_BIT_Debug_STATUS_bit,"TFTP服务器IP: %02X.%02X.%02X.%02X",data[US_Para + 4],data[US_Para + 5],data[US_Para + 6],data[US_Para + 7]);
	
	return 0x00;
}


/*******************************************************************************
* Function Name  : UDP_Server_CMD_Change_Report_Data_Processing
* Description    : UDP服务器命令处理 - 上报设备状态回复数据处理
* Input          : 
* Return         : None
*******************************************************************************/
uint8_t UDP_Server_CMD_Change_Report_ReplyData_Processing(UINT8 *data, UINT32 len, uint8_t* ip, uint16_t port)
{
	Dbg_Println(DBG_BIT_Debug_STATUS_bit,"新服务器 - 变化上报应答数据");
	/*清除缓冲区中的数据*/
	uint32_t r_addr = SRAM_Read_DW(SRAM_UDP_Server_ReadTXData_Addr);
	if((r_addr < SRAM_UDP_Server_TXDataBuff_Start_Address) || (r_addr > SRAM_UDP_Server_TXDataBuff_End_Address)) r_addr = SRAM_UDP_Server_TXDataBuff_Start_Address;
	
	if((data[US_Frame_L] == SRAM_Read_Byte(r_addr + 2 + US_Frame_L)) && (data[US_Frame_H] == SRAM_Read_Byte(r_addr + 2 + US_Frame_H)))
	{
		/*帧号相同*/
		r_addr += SRAM_UDP_Server_DataBuff_Size;
		if(r_addr >= SRAM_UDP_Server_TXDataBuff_End_Address) r_addr = SRAM_UDP_Server_TXDataBuff_Start_Address;
		SRAM_Write_DW(r_addr,SRAM_UDP_Server_ReadTXData_Addr);
		UDPServer_Info.send_state = USS_IDLE;
		
		Dbg_Println(DBG_BIT_Debug_STATUS_bit,"新服务器 - 心跳包应答数据 r_addr:%08X",r_addr);
	}else {
		Dbg_Println(DBG_BIT_Debug_STATUS_bit,"新服务器 - 心跳包应答数据 帧号不同%02X%02X - %02X%02X",data[US_Frame_H],data[US_Frame_L],SRAM_Read_Byte(r_addr + 2 + US_Frame_H),SRAM_Read_Byte(r_addr + 2 + US_Frame_L));
	}
	
	/*该回复数据没有内容处理*/
	
	return 0x00;
}


/*******************************************************************************
* Function Name  : UDP_Server_CMD_Control_Dev_Data_Processing
* Description    : UDP服务器命令处理 - 下发控制命令处理
* Input          : 
* Return         : None
*******************************************************************************/
uint8_t UDP_Server_CMD_Control_Dev_Data_Processing(UINT8 *data, UINT32 len, uint8_t* ip, uint16_t port)
{
	uint8_t deal_flag = 0x01,retry_flag = 0x00;
	uint16_t cmd_frame = 0,data_len = len - US_Para;
	uint16_t back_len = 1 + US_Para;	
	uint32_t read_addr = 0;
	uint8_t back_data[back_len + 2];
	memset(back_data,0,sizeof(back_data));
	
	cmd_frame = data[US_Frame_H];
	cmd_frame <<= 8;
	cmd_frame |= data[US_Frame_L];
	/*判断帧号*/
	if(cmd_frame >= 0x8000) {
		Dbg_Print(DBG_BIT_Debug_STATUS_bit,"下发控制命令 帧号不对\r\n");
		return 0x01;
	}
	
	if(cmd_frame == UDPServer_Info.rev_frame)	{
		
		Dbg_Print(DBG_BIT_Debug_STATUS_bit,"下发控制命令 帧号相同，不处理 重发次数:%d\r\n",data[US_Retry]);
		
		//测试使用 - 第6次重发再回复
		if(data[US_Retry] == 0x06)
		{
			retry_flag = 0x01;
		}
		deal_flag = 0x00;
	}
	UDPServer_Info.rev_frame = cmd_frame;

	if(deal_flag == 0x00)
	{
		/*将控制数据拷贝到SRAM缓冲区中 - 该缓冲区中只有一级缓存，新的数据会将老的数据全部替换*/
		SRAM_Write_Byte(data[CMD_OFFSET + 7],SRAM_UDP_RecvData_ControlNum);
		SRAM_DMA_Write_Buff(&data[CMD_OFFSET + 8],data_len,SRAM_UDP_RecvData_Startaddr);
		read_addr = SRAM_UDP_RecvData_Startaddr + data_len;
		SRAM_Write_DW(read_addr,SRAM_UDP_RecvData_Writeaddr);
	}
	
	/*回复内容*/
	back_data[US_Para] = 0x00;
	
	if(retry_flag == 0x01)
	{
		//封装数据
		UDP_NetServer_ReplyData_Packing(UDP_Server_CMD_Control_Dev,cmd_frame,back_data,back_len);
		//发送数据
		UDP_Server_Data_To_Server(back_data,back_len);
	}
	
	return 0x00;
}

/*******************************************************************************
* Function Name  : UDP_Server_CMD_Starter_Test_Data_Processing
* Description    : UDP服务器命令处理 - 下发测试命令处理
* Input          : 
* Return         : None
*******************************************************************************/
uint8_t UDP_Server_CMD_Starter_Test_Data_Processing(UINT8 *data, UINT32 len, uint8_t* ip, uint16_t port)
{
	uint8_t deal_flag = 0x01;
	uint16_t cmd_frame = 0;
	uint16_t back_len = 1 + US_Para;	

	uint8_t back_data[back_len + 2];
	memset(back_data,0,sizeof(back_data));
	
	cmd_frame = data[US_Frame_H];
	cmd_frame <<= 8;
	cmd_frame |= data[US_Frame_L];
	/*判断帧号*/
	if(cmd_frame >= 0x8000) {
		Dbg_Print(DBG_BIT_Debug_STATUS_bit,"下发控制命令 帧号不对\r\n");
		return 0x01;
	}
	
	if(cmd_frame == UDPServer_Info.rev_frame)	{
		
		Dbg_Print(DBG_BIT_Debug_STATUS_bit,"下发控制命令 帧号相同，不处理 重发次数:%d\r\n",data[US_Retry]);
		
		deal_flag = 0x00;
	}
	UDPServer_Info.rev_frame = cmd_frame;

	if(deal_flag == 0x01)
	{
		/*设置测试标志位*/
		UDPServer_Info.test_flag = 0x01;
		UDPServer_Info.test_num = data[US_Para] + (data[US_Para + 1] << 8);
		UDPServer_Info.test_time = data[US_Para + 2];
		UDPServer_Info.test_sn = 0;
	}
	
	/*回复内容*/
	back_data[US_Para] = 0x00;
	
	//封装数据
	UDP_NetServer_ReplyData_Packing(UDP_Server_CMD_Control_Dev,cmd_frame,back_data,back_len);
	//发送数据
	UDP_Server_Data_To_Server(back_data,back_len);
	
	
	return 0;
}

/*******************************************************************************
* Function Name  : UDP_Server_CMD_Send_Test_Data_Packing
* Description    : UDP服务器命令处理 - 测试命令打包函数
* Input          : 
* Return         : None
*******************************************************************************/
uint8_t UDP_Server_CMD_Send_Test_Data_Packing()
{
	uint32_t temp_tick = 0;
	uint8_t buff[20];
	memset(buff,0,sizeof(buff));
	
	UDPServer_Info.test_sn++;
	
	if(UDPServer_Info.test_sn >= UDPServer_Info.test_num)	UDPServer_Info.test_flag = 0;
	
	buff[0] = (UDPServer_Info.test_num & 0xFF);
	buff[1] = (UDPServer_Info.test_num >> 8) & 0xFF;
	buff[2] = (UDPServer_Info.test_sn & 0xFF);
	buff[3] = (UDPServer_Info.test_sn >> 8) & 0xFF;
	
	/*UTC时间 4Byte*/
	temp_tick = RTC_Conversion_To_Unix(&RTC_Raw_Data);
	
	buff[4] = temp_tick & 0xFF;
	buff[5] = (temp_tick >> 8) & 0xFF;
	buff[6] = (temp_tick >> 16) & 0xFF;
	buff[7] = (temp_tick >> 24) & 0xFF;
	
	Dbg_Println(DBG_BIT_Debug_STATUS_bit,"%s",__func__);
	UDP_NetServer_Data_Packing(UDP_Server_CMD_Send_Test_Data,buff,8);
	
	return 0;
}

/*******************************************************************************
* Function Name  : UDP_Server_CMD_Send_Test_Data_Processing
* Description    : UDP服务器命令处理 - 测试命令回复
* Input          : 
* Return         : None
*******************************************************************************/
uint8_t UDP_Server_CMD_Send_Test_ReplyData_Processing(UINT8 *data, UINT32 len, uint8_t* ip, uint16_t port)
{
	Dbg_Println(DBG_BIT_Debug_STATUS_bit,"新服务器 - 测试命令回复");
	/*清除缓冲区中的数据*/
	uint32_t r_addr = SRAM_Read_DW(SRAM_UDP_Server_ReadTXData_Addr);
	if((r_addr < SRAM_UDP_Server_TXDataBuff_Start_Address) || (r_addr > SRAM_UDP_Server_TXDataBuff_End_Address)) r_addr = SRAM_UDP_Server_TXDataBuff_Start_Address;
	
	if((data[US_Frame_L] == SRAM_Read_Byte(r_addr + 2 + US_Frame_L)) && (data[US_Frame_H] == SRAM_Read_Byte(r_addr + 2 + US_Frame_H)))
	{
		/*帧号相同*/
		r_addr += SRAM_UDP_Server_DataBuff_Size;
		if(r_addr >= SRAM_UDP_Server_TXDataBuff_End_Address) r_addr = SRAM_UDP_Server_TXDataBuff_Start_Address;
		SRAM_Write_DW(r_addr,SRAM_UDP_Server_ReadTXData_Addr);
		UDPServer_Info.send_state = USS_IDLE;
		
		Dbg_Println(DBG_BIT_Debug_STATUS_bit,"新服务器 - 测试命令回复 r_addr:%08X",r_addr);
	}else {
		Dbg_Println(DBG_BIT_Debug_STATUS_bit,"新服务器 - 测试命令回复 帧号不同%02X%02X - %02X%02X",data[US_Frame_H],data[US_Frame_L],SRAM_Read_Byte(r_addr + 2 + US_Frame_H),SRAM_Read_Byte(r_addr + 2 + US_Frame_L));
	}
	
	/*该回复数据没有内容处理*/
	
	return 0x00;
}

/*******************************************************************************
* Function Name  : UDP_Server_CMD_Read_RCU_Data_Processing
* Description    : UDP服务器命令处理 - 读取RCU数据命令处理
* Input          : 
* Return         : None
*******************************************************************************/
uint8_t UDP_Server_CMD_Read_RCU_Data_Processing(UINT8 *data, UINT32 len, uint8_t* ip, uint16_t port)
{
	uint8_t deal_flag = 0x01,read_flag = 0x00,result = 0x02;
	uint16_t cmd_frame = 0;
	uint16_t back_len = 530 + US_Para;	
	
	uint32_t read_addr = 0,read_len = 0;
	uint8_t back_data[back_len + 2];
	memset(back_data,0,sizeof(back_data));
	
	cmd_frame = data[US_Frame_H];
	cmd_frame <<= 8;
	cmd_frame |= data[US_Frame_L];
	/*判断帧号*/
	if(cmd_frame >= 0x8000) {
		Dbg_Print(DBG_BIT_Debug_STATUS_bit,"下发控制命令 帧号不对\r\n");
		return 0x01;
	}
	
	if(cmd_frame == UDPServer_Info.rev_frame)	{
		
		Dbg_Print(DBG_BIT_Debug_STATUS_bit,"下发控制命令 帧号相同，不处理 重发次数:%d\r\n",data[US_Retry]);
		
		deal_flag = 0x00;
	}
	UDPServer_Info.rev_frame = cmd_frame;
	
	back_len = US_Para + 1;
	
	if(deal_flag == 0x00)
	{
		/*读取RCU数据 - 读取区域 4Byte*/
		read_flag = data[US_Para];
		
		/*读取RCU数据 - 读取地址 4Byte*/
		read_addr = data[US_Para + 4];
		read_addr <<= 8;
		read_addr |= data[US_Para + 3];
		read_addr <<= 8;
		read_addr |= data[US_Para + 2];
		read_addr <<= 8;
		read_addr |= data[US_Para + 1];

		/*读取RCU数据 - 读取长度 4Byte - 目前最长的读取长度为512Byte*/
		read_len = data[US_Para + 8];
		read_len <<= 8;
		read_len |= data[US_Para + 7];
		read_len <<= 8;
		read_len |= data[US_Para + 6];
		read_len <<= 8;
		read_len |= data[US_Para + 5];
		if(read_len >= 512)	read_len = 512;
		
		/*读取RCU*/
		switch(read_flag)
		{
			case 0x01:	//SRAM
				if(read_addr >= SRAM_ADDRESS_MAX) {
					Dbg_Println(DBG_BIT_Debug_STATUS_bit,"读取地址不在有效范围");
					break;
				}
				SRAM_Read_Buff(&back_data[US_Para+1],read_len,read_addr);
				back_len += read_len;
				result = 0x01;
				break;
			case 0x02:	//Flash
				if(read_addr >= Flash_ADDRESS_MAX) {
					Dbg_Println(DBG_BIT_Debug_STATUS_bit,"读取地址不在有效范围");
					break;
				}
				Flash_Read(&back_data[US_Para+1],read_len,read_addr);
				back_len += read_len;
				result = 0x01;
				break;
			default:
				
				break;
		}
	}
	
	/*回复内容*/
	back_data[US_Para] = result;
	
	//封装数据
	UDP_NetServer_ReplyData_Packing(UDP_Server_CMD_Read_RCU_Data,cmd_frame,back_data,back_len);
	//发送数据
	UDP_Server_Data_To_Server(back_data,back_len);

	
	return 0x00;
}

/*******************************************************************************
* Function Name  : UDP_NetServer_Data_Analysis
* Description    : UDP服务器数据处理任务
* Input          : 
* Return         : None
*******************************************************************************/
void UDP_NetServer_Data_Analysis(UINT8 *data, UINT32 len, uint8_t* ip, uint16_t port)
{
	if((data[US_Head_L] != UDP_Server_Head_L) || (data[US_Head_H] != UDP_Server_Head_H)) {
		Dbg_Println(DBG_BIT_Debug_STATUS_bit,"新UDP服务器 - 数据头错误");
		return;
	}
	
	if(Data_Conver_To_Uint16(&data[US_Len_L]) != len)	{
		Dbg_Println(DBG_BIT_Debug_STATUS_bit,"新UDP服务器 - 长度错误 %04X - %04X",len,Data_Conver_To_Uint16(&data[US_Len_L]));
		return;
	}
	
	if(UDP_NetServer_Data_CheckCRC(data,len)) 	{
		Dbg_Println(DBG_BIT_Debug_STATUS_bit,"新UDP服务器 - CRC错误");
		return;
	}
	
	/*如果还需要解密。。。待处理*/
	
	switch(Data_Conver_To_Uint16(&data[US_CMD_L]))
	{
		case UDP_Server_CMD_Search:
			UDP_Server_CMD_Search_Data_Processing(data,len,ip,port);
			break;
		case UDP_Server_CMD_Register:
			UDP_Server_CMD_Register_Data_Processing(data,len,ip,port);
			break;
		case UDP_Server_CMD_Heartbeat:		//心跳包应答回复
			UDP_Server_CMD_Heartbeat_ReplyData_Processing(data,len,ip,port);
			break;
		case UDP_Server_CMD_Change_Report:	//动作变化上报回复
			UDP_Server_CMD_Change_Report_ReplyData_Processing(data,len,ip,port);
			break;
		case UDP_Server_CMD_Control_Dev:
			UDP_Server_CMD_Control_Dev_Data_Processing(data,len,ip,port);
			break;
		case UDP_Server_CMD_Starter_Test:
			UDP_Server_CMD_Starter_Test_Data_Processing(data,len,ip,port);
			break;
		case UDP_Server_CMD_Send_Test_Data:
			UDP_Server_CMD_Send_Test_ReplyData_Processing(data,len,ip,port);
			break;
		case UDP_Server_CMD_Read_RCU_Data:
			
			break;
		default:
			Dbg_Println(DBG_BIT_Debug_STATUS_bit,"新服务器 - 未知命令:%04X",Data_Conver_To_Uint16(&data[US_CMD_L]));
			break;
	}
	
}
