#include "includes.h"   


/*******************************************************************************
* Function Name  : Addtion_Regular_Service_State
* Description    : 添加服务状态到BUFF
*******************************************************************************/
void Addtion_Regular_Service_State(uint8_t *data,uint8_t type,uint8_t addr,uint16_t loop,uint16_t start)
{
	data[0] = type;
	data[1] = addr;
	data[2] = (loop & 0xFF);
	data[3] = ((loop >> 8) & 0xFF);
	data[4] = (start & 0xFF);
	data[5] = ((start >> 8) & 0xFF);
}

/*******************************************************************************
* Function Name  : Udp_Internal_Regular_Service_State 
* Description    : 定期向服务器上报服务信息
	服务信息包含：空调、取电、清理、勿扰、SOS、门磁状态
*******************************************************************************/
void Udp_Internal_Regular_Service_State(void)
{
	uint8_t temp = 0;
	uint8_t back_data[100];  //一次发生最大长度
//	uint16_t temp3 = 0;
	UINT32 len = 0;
//	uint32_t deal_addr = 0;
	uint32_t write_addr = SRAM_Read_DW(SRAM_UDP_SendData_Writeaddr),read_addr = SRAM_Read_DW(SRAM_UDP_SendData_Readaddr);
	
	memset(back_data,0,100);
	
	len = 62;		//数据长度加上包长
	Dbg_Println(DBG_BIT_NET_STATUS_bit,"Udp_Internal_Regular_Service_State len:%ld --------\r\n",len);
	
	UDP_Add_Header(back_data,In_RoomState_Cmd,len,0xFFFF);        
	back_data[15] = Get_Authorize_Lock_Status();		//系统锁
	back_data[16] = 0x00;		//卡类型
	back_data[17] = 0x00;		//门磁状态
	back_data[18] = 0x00;		//门锁电量
	back_data[19] = 0x00;		//门锁电量
	back_data[20] = 0x00;		//主机温度
	back_data[21] = 0x00;		//空调数量
	back_data[22] = 0x06;		//RCU上报的设备数量
	
	/*空调状态*/
//	deal_addr = Find_Device_List_Information(DEV_C7T_TEMP_TYPE,0x00);
//	temp3 = Get_C7T_Temp_New_Status(deal_addr);
//	Addtion_Regular_Service_State(&back_data[23],DEV_RS485_TEMPCTRL1,0x00,0x0000,temp3);			//本项目使用的温控器地址是0x00 C7T
	/*取电状态*/
	temp = SRAM_Read_Byte(SRAM_LOG_Device_Card_Status);
	if(temp == 0x01)
	{
		Addtion_Regular_Service_State(&back_data[29],Dev_Host_Service,0x00,0x0001,0x0001);
	}else {
		Addtion_Regular_Service_State(&back_data[29],Dev_Host_Service,0x00,0x0001,0x0002);
	}
	/*清理状态*/
	if(SRAM_Read_Byte(SRAM_Global_Clear_Start) == 0x01)		//当前处于清理状态，关闭清理
	{
		Addtion_Regular_Service_State(&back_data[35],Dev_Host_Service,0x00,0x0003,0x0001);
	}else {
		Addtion_Regular_Service_State(&back_data[35],Dev_Host_Service,0x00,0x0003,0x0002);
	}
	/*勿扰状态*/
	if(SRAM_Read_Byte(SRAM_Global_Do_Not_Disturb_Start) == 0x01)	//当前处于勿扰状态，关闭勿扰
	{
		Addtion_Regular_Service_State(&back_data[41],Dev_Host_Service,0x00,0x0002,0x0001);
	}else {
		Addtion_Regular_Service_State(&back_data[41],Dev_Host_Service,0x00,0x0002,0x0002);
	}
	/*SOS状态*/
	if(SRAM_Read_Byte(SRAM_Global_SOS_Status_Flag) == 0x01)	
	{
		Addtion_Regular_Service_State(&back_data[47],Dev_Host_Service,0x00,0x0008,0x0001);
	}else {
		Addtion_Regular_Service_State(&back_data[47],Dev_Host_Service,0x00,0x0008,0x0002);
	}
	
	/*门磁状态*/
	if((SRAM_Read_Word(SRAM_LOG_Device_C5IO_DI_Status) & 0x000C) == 0x00)		
	{
		Dbg_Println(DBG_BIT_NET_STATUS_bit,"Regular_Service_State UDP DOOR OPEN !");
		Addtion_Regular_Service_State(&back_data[53],Dev_Host_Service,0x00,0x000D,0x0001);
	}else {
		Dbg_Println(DBG_BIT_NET_STATUS_bit,"Regular_Service_State UDP DOOR CLOSE !");
		Addtion_Regular_Service_State(&back_data[53],Dev_Host_Service,0x00,0x000D,0x0002);
	}
	
	NetCRC16(&back_data[0],len);
	
//	Dbg_Println(DBG_BIT_NET_STATUS_bit,"Regular_Service_State:");
//	for(uint16_t j=0;j<len;j++)
//	{
//		DBG_Printf("%02X ",back_data[j]);
//	}
//	DBG_Printf("\r\n");
	
	CH57xNET_SocketUdpSendTo(net_info.SocketId[0], &back_data[0], &len, server_info.dis_ip, server_info.dis_port);
}

/*******************************************************************************
* Function Name  : Udp_Scan_Roomstate 
* Description    : 添加房间设备状态变化数据 - 向上报缓冲区中写入数据
*******************************************************************************/
void Udp_Addtion_Roomstate(uint8_t type,uint8_t addr,uint16_t loop,uint16_t start)
{
	uint8_t back_data[8];  
	uint32_t write_addr = SRAM_Read_DW(SRAM_UDP_SendData_Writeaddr),read_addr = SRAM_Read_DW(SRAM_UDP_SendData_Readaddr);
	
	memset(back_data,0,8);
	/*判断地址是否在范围内*/
	if( (write_addr < SRAM_UDP_SendData_Startaddr) || (write_addr > SRAM_UDP_SendData_Endaddr) )	write_addr = SRAM_UDP_SendData_Startaddr;
	if( (read_addr < SRAM_UDP_SendData_Startaddr) || (read_addr > SRAM_UDP_SendData_Endaddr) )	read_addr = SRAM_UDP_SendData_Startaddr;
	
	back_data[0] = type;
	back_data[1] = addr;
	back_data[2] = (loop & 0xFF);
	back_data[3] = ((loop >> 8) & 0xFF);
	back_data[4] = (start & 0xFF);
	back_data[5] = ((start >> 8) & 0xFF);
	
	SRAM_DMA_Write_Buff(back_data,0x06,write_addr);
	write_addr += 0x06;			//地址偏移
	
	if(write_addr >= SRAM_UDP_SendData_Endaddr)
	{
		Dbg_Println(DBG_BIT_NET_STATUS_bit,"SRAM_UDP_SendData_Writeaddr overstep %08X!!!\r\n",write_addr);
		write_addr = SRAM_UDP_SendData_Startaddr;
	}
	SRAM_Write_DW(write_addr,SRAM_UDP_SendData_Writeaddr);
}

/*******************************************************************************
* Function Name  : Udp_Scan_Roomstate 
* Description    : 扫描房间设备状态是否变化
*******************************************************************************/
void Udp_Scan_Roomstate(void)
{
	uint8_t temp1 = 0,temp2 = 0;
	uint8_t back_data[20];  //一次发生最大长度

	uint32_t write_addr = SRAM_Read_DW(SRAM_UDP_SendData_Writeaddr),read_addr = SRAM_Read_DW(SRAM_UDP_SendData_Readaddr);
	
	memset(back_data,0,20);
	
	/*判断地址是否在范围内*/
	if( (write_addr < SRAM_UDP_SendData_Startaddr) || (write_addr > SRAM_UDP_SendData_Endaddr) )	write_addr = SRAM_UDP_SendData_Startaddr;
	if( (read_addr < SRAM_UDP_SendData_Startaddr) || (read_addr > SRAM_UDP_SendData_Endaddr) )	read_addr = SRAM_UDP_SendData_Startaddr;
	
	/*继电器变化标志位 - 3Byte*/
	for(uint8_t i = 0;i<3;i++)
	{
		temp1 = SRAM_Read_Byte(SRAM_LOG_Device_C5IO_Relay_Status + i);
		temp2 = SRAM_Read_Byte(SRAM_UDP_Device_C5IO_Relay_Status + i);
		
		if(temp1 == temp2) continue;
		
		Dbg_Println(DBG_BIT_NET_STATUS_bit,"LOG_C5IO:%d,UDP_C5IO:%d\r\n",temp1,temp2);
		for(uint8_t j=0;j<8;j++)
		{
			if( (temp1 & (0x01<<j)) != (temp2 & (0x01<<j)) )
			{
				Dbg_Println(DBG_BIT_NET_STATUS_bit,"Device Relay :%d\r\n",i*8+j+1);
				/*将数据写入设备动作缓冲区*/
				back_data[0] = Dev_Host_HVout;	//继电器设备类型
				back_data[1] = 0x00;			//继电器设备地址
				back_data[2] = i*8+j+1;			//继电器回路
				back_data[3] = 0x00;			//继电器回路
				if((temp1 & (0x01<<j)) == 0x00)
				{
					Dbg_Println(DBG_BIT_NET_STATUS_bit,"Device Relay :%d 继电器关\r\n",back_data[2]);
					back_data[4] = 0x02;	//继电器关状态
					back_data[5] = 0x00;
				}else
				{
					Dbg_Println(DBG_BIT_NET_STATUS_bit,"Device Relay :%d 继电器开\r\n",back_data[2]);
					back_data[4] = 0x01;	//继电器开状态
					back_data[5] = 0x00;
				}
				
//				Dbg_Println(DBG_BIT_NET_STATUS_bit,"RelayState:");
//				for(uint16_t j=0;j<6;j++)
//				{
//					Dbg_Println(DBG_BIT_NET_STATUS_bit,"%02X ",back_data[j]);
//				}
//				Dbg_Println(DBG_BIT_NET_STATUS_bit,"\r\n");
				
				SRAM_DMA_Write_Buff(back_data,0x06,write_addr);
				write_addr += 0x06;			//地址偏移
				SRAM_DMA_Read_Buff(back_data,0x06,read_addr);
				
//				Dbg_Println(DBG_BIT_NET_STATUS_bit,"RelayreadState:");
//				for(uint16_t j=0;j<6;j++)
//				{
//					Dbg_Println(DBG_BIT_NET_STATUS_bit,"%02X ",back_data[j]);
//				}
//				Dbg_Println(DBG_BIT_NET_STATUS_bit,"\r\n");
				
				if(write_addr >= SRAM_UDP_SendData_Endaddr)
				{
					Dbg_Println(DBG_BIT_NET_STATUS_bit,"SRAM_UDP_SendData_Writeaddr overstep %08X!!!\r\n",write_addr);
					write_addr = SRAM_UDP_SendData_Startaddr;
				}
				SRAM_Write_DW(write_addr,SRAM_UDP_SendData_Writeaddr);
			}
		}
		/*比较完成后，保存变量状态*/
		SRAM_Write_Byte(temp1,SRAM_UDP_Device_C5IO_Relay_Status + i);
	}
	
	/*温控器上报 - 2Byte     改为定期上报*/
//	temp3 = SRAM_Read_Word(SRAM_LOG_Device_Temp_Status);
//	temp4 = SRAM_Read_Word(SRAM_UDP_Device_Temp_Status);
//	if(temp3 != temp4)
//	{
//		Dbg_Println(DBG_BIT_NET_STATUS_bit,"LOG_Temp:%04X,UDP_Temp:%04X\r\n",temp3,temp4);

//		/*将数据写入设备动作缓冲区*/
//		back_data[0] = DEV_RS485_TEMPCTRL1;	//空调类型
//		back_data[1] = 0x01;				//空调设备地址
//		back_data[2] = 0x00;				//回路
//		back_data[3] = 0x00;				//回路
//		back_data[4] = temp3 & 0xFF;				//空调状态
//		back_data[5] = (temp3 >> 8) & 0xFF;			//空调状态
//		
////		Dbg_Println(DBG_BIT_NET_STATUS_bit,"RelayState:");
////		for(uint16_t j=0;j<6;j++)
////		{
////			Dbg_Println(DBG_BIT_NET_STATUS_bit,"%02X ",back_data[j]);
////		}
////		Dbg_Println(DBG_BIT_NET_STATUS_bit,"\r\n");
//		
//		
//		SRAM_DMA_Write_Buff(back_data,0x06,write_addr);
//		write_addr += 0x06;			//地址偏移
//		
//		if(write_addr >= SRAM_UDP_SendData_Endaddr)
//		{
//			Dbg_Println(DBG_BIT_NET_STATUS_bit,"SRAM_UDP_SendData_Writeaddr overstep %08X!!!\r\n",write_addr);
//			write_addr = SRAM_UDP_SendData_Startaddr;
//		}
//		SRAM_Write_DW(write_addr,SRAM_UDP_SendData_Writeaddr);
//		
//		/*比较完成后，保存变量状态*/
//		SRAM_Write_Word(temp3,SRAM_UDP_Device_Temp_Status);
//	}

}

/*******************************************************************************
* Function Name  : Udp_Internal_RoomState 
* Description    : 房间设备动作上报
*******************************************************************************/
void Udp_Internal_RoomState(void)
{
	if((server_info.frame_no < 0xfffe) && (server_info.frame_no >= 0x8000))      
	{
		server_info.frame_no++;
	}
	else
	{
		server_info.frame_no = 0x8000;
	}
	
	uint8_t temp = 0;
	uint8_t back_data[200];  //一次发生最大长度
	uint16_t temp3 = 0,temp4 = 0,temp5 = 0;
	UINT32 len = 0;
	uint32_t write_addr = SRAM_Read_DW(SRAM_UDP_SendData_Writeaddr),read_addr = SRAM_Read_DW(SRAM_UDP_SendData_Readaddr);
	
	memset(back_data,0,200);
	
	/*判断地址是否在范围内*/
	if( (write_addr < SRAM_UDP_SendData_Startaddr) || (write_addr > SRAM_UDP_SendData_Endaddr) )	write_addr = SRAM_UDP_SendData_Startaddr;
	if( (read_addr < SRAM_UDP_SendData_Startaddr) || (read_addr > SRAM_UDP_SendData_Endaddr) )	read_addr = SRAM_UDP_SendData_Startaddr;
	//Dbg_Println(DBG_BIT_NET_STATUS_bit,"write_addr: %08X,read_addr: %08X!!!\r\n",write_addr,read_addr);
	
	/*数据打包 - 发送*/
	if(write_addr > read_addr)
	{
		len = write_addr - read_addr;
		if(len > SRAM_UDP_SendData_Size)	len = SRAM_UDP_SendData_Size;
	}
	else if(write_addr < read_addr)
	{
		len = SRAM_UDP_SendData_Endaddr - read_addr;
		len += write_addr - SRAM_UDP_SendData_Startaddr;
		
		if(len > SRAM_UDP_SendData_Size)	len = SRAM_UDP_SendData_Size;
	}
	else {
		//Dbg_Println(DBG_BIT_NET_STATUS_bit,"UDP Not Start Change\r\n");
		return ;
	}
	
//	udp_heart_tick = SysTick_1s;
	
	//len = temp*6 + 26;
	temp3 = len;	//临时保存数据长度
	temp = len/6;	//获取设备数
	len += 26;		//数据长度加上包长
	Dbg_Println(DBG_BIT_NET_STATUS_bit,"UDP Relay_Status change :%d ，len:%ld --------\r\n",temp,len);
	
	UDP_Add_Header(back_data,In_RoomState_Cmd,len,server_info.frame_no);        
	back_data[15] = Get_Authorize_Lock_Status();		//系统锁
	back_data[16] = 0x00;		//卡类型
	back_data[17] = 0x00;		//门磁状态
	back_data[18] = 0x00;		//门锁电量
	back_data[19] = 0x00;		//门锁电量
	back_data[20] = 0x00;		//主机温度
	back_data[21] = 0x00;		//空调数量
	back_data[22] = temp;		//RCU上报的设备数量
	if(read_addr + temp3 > SRAM_UDP_SendData_Endaddr)
	{
		temp4 = SRAM_UDP_SendData_Endaddr - read_addr;
		SRAM_DMA_Read_Buff(&back_data[23],temp4,read_addr);
		temp5 = temp3 - temp4;
		SRAM_DMA_Read_Buff(&back_data[23+temp4],temp5,read_addr);
		
		read_addr = SRAM_UDP_SendData_Startaddr;
		read_addr += temp5;
		
	}else {
		SRAM_DMA_Read_Buff(&back_data[23],temp3,read_addr);
		read_addr += temp3;
		if(read_addr >= SRAM_UDP_SendData_Endaddr) read_addr = SRAM_UDP_SendData_Startaddr;
	}
	
	SRAM_Write_DW(read_addr,SRAM_UDP_SendData_Tempaddr);	//先临时保存读取地址，当收到回复报文时才将读取地址复位
	
	NetCRC16(&back_data[0],len);
	
//	Dbg_Println(DBG_BIT_NET_STATUS_bit,"RoomState:");
//	for(uint16_t j=0;j<len;j++)
//	{
//		Dbg_Println(DBG_BIT_NET_STATUS_bit,"%02X ",back_data[j]);
//	}
//	Dbg_Println(DBG_BIT_NET_STATUS_bit,"\r\n");
	
	CH57xNET_SocketUdpSendTo(net_info.SocketId[0], &back_data[0], &len, server_info.dis_ip, server_info.dis_port);
	
	LOG_NET_COMM_Send_Record(0x01,server_info.dis_ip,server_info.dis_port,&back_data[0],len);
}

/*************************
**函数名：Udp_Internal_Read_MCU_System_Process
**参  数：data：接受到的数据   len：长度   ip：网络地址  port：端口
**作  用：UDP 读取主机系统参数信息 数据处理       
**返回值：无        
************************/
uint8_t Udp_Internal_Read_MCU_System_Process(uint8_t* data, uint16_t DataLen, uint8_t *ip,uint16_t port)
{
	/*CRC校验*/
	uint16_t data_crc = data[DataLen-2] + (data[DataLen-1] << 8);
	if(NetCRC16_2(data,DataLen-2) != data_crc) return 0xF0;
	
	Dbg_Print(DBG_BIT_NET_STATUS_bit,"查询MCU系统参数信息：");
	
//	for(uint16_t k = 0;k<DataLen;k++)
//	{
//		DBG_Printf("%02X ",data[k]);
//	}
//	
//	DBG_Printf("\n");
	
	uint16_t pack_frame = 0;
	uint32_t read_register = 0,temp_offset = 0,temp_data = 0;
	uint8_t register_num = data[HEAD_LEN+4];
	
	if(register_num > 28)	register_num = 28;		//读取寄存器个数
	
	Dbg_Print(DBG_BIT_NET_STATUS_bit,"register_num:%d\r\n",register_num);
	
	UINT32 back_len = register_num*8 + 402;			//整包数据长度		
	uint8_t back_data[back_len + 2];						//这数据长度根据需要读取的数据长度来决定
	
	memset(back_data,0,sizeof(back_data));
	
	pack_frame = data[FRAME_NO_OFFSET] + (data[FRAME_NO_OFFSET+1]<<8);   //帧号
	
	/*数据打包*/
	UDP_Add_Header(back_data,In_Read_MCUSystem_Cmd,back_len,pack_frame);            	//添加头部
	back_data[HEAD_LEN] = 0x00;										//本机按键状态	
	
	memcpy(&back_data[HEAD_LEN + 1],MCU_TYPE,sizeof(MCU_TYPE));   	//机型编号
	memcpy(&back_data[HEAD_LEN + 17],net_info.local_ip,4);   		//IP地址
	memcpy(&back_data[HEAD_LEN + 21],server_info.dis_ip,4);   		//服务器IP地址
	memcpy(&back_data[HEAD_LEN + 25],net_info.mask,4);   			//子网掩码
	memcpy(&back_data[HEAD_LEN + 29],net_info.gwip_addr,4);   		//网关
	UDP_Add_Port(&back_data[HEAD_LEN + 33]);						//MCU主机默认端口为3341
	memcpy(&back_data[HEAD_LEN + 37],DNS_SERVER_IP,4);   			//网关
	UDP_ADD_SoftwareVer(&back_data[HEAD_LEN+41]);					//机型编号 - 软件版本号
	/*RTC时间*/
	back_data[HEAD_LEN+61] = HEX_Conversion_To_DEC(RTC_Raw_Data.year);
	back_data[HEAD_LEN+62] = HEX_Conversion_To_DEC(RTC_Raw_Data.month);
	back_data[HEAD_LEN+63] = HEX_Conversion_To_DEC(RTC_Raw_Data.day);
	back_data[HEAD_LEN+64] = HEX_Conversion_To_DEC(RTC_Raw_Data.hour);
	back_data[HEAD_LEN+65] = HEX_Conversion_To_DEC(RTC_Raw_Data.minute);
	back_data[HEAD_LEN+66] = HEX_Conversion_To_DEC(RTC_Raw_Data.second);
	/*保留20Byte*/
	UDP_Add_Mac(&back_data[HEAD_LEN+87]);			//MAC地址
	SRAM_Read_Buff(&back_data[HEAD_LEN+93],4,SRAM_Register_Start_ADDRESS + Register_ProjectCode_OFFSET);		//项目编码
	SRAM_Read_Buff(&back_data[HEAD_LEN+97],4,SRAM_Register_Start_ADDRESS + Register_RoomNumber_OFFSET);
	SRAM_Read_Buff(&back_data[HEAD_LEN+101],4,SRAM_Register_Start_ADDRESS + Register_HouseType_OFFSET);
	SRAM_Read_Buff(&back_data[HEAD_LEN+105],4,SRAM_Register_Start_ADDRESS + Register_ConfigVersion_OFFSET);
	SRAM_Read_Buff(&back_data[HEAD_LEN+109],4,SRAM_Register_Start_ADDRESS + Register_RoomRent_OFFSET);
	SRAM_Read_Buff(&back_data[HEAD_LEN+113],4,SRAM_Register_Start_ADDRESS + Register_SeasonStatus_OFFSET);
	//SRAM_Read_Buff(&back_data[HEAD_LEN+117],4,SRAM_Register_Start_ADDRESS + Register_MandateLock_OFFSET);
	back_data[HEAD_LEN+117] = Get_Authorize_Lock_Status();		//获取授权状态
	SRAM_Read_Buff(&back_data[HEAD_LEN+121],4,SRAM_Register_Start_ADDRESS + Register_MandateUTC_OFFSET);
	SRAM_Read_Buff(&back_data[HEAD_LEN+125],4,SRAM_Register_Start_ADDRESS + Register_MandateExpiresTime_OFFSET);
	SRAM_Read_Buff(&back_data[HEAD_LEN+129],128,SRAM_Register_Start_ADDRESS + Register_RoomNumNote_OFFSET);
	Flash_Read(&back_data[HEAD_LEN+257],64,FLASH_MCU_Model_Revision_ADDRESS);
	Flash_Read(&back_data[HEAD_LEN+321],64,FLASH_MCU_Control_Revision_ADDRESS);

	//读取寄存器地址
	for(uint8_t i=0;i<register_num;i++)
	{
		temp_offset = data[udp_packdata+i*4+8];
		temp_offset <<= 8;
		temp_offset |= data[udp_packdata+i*4+7];
		temp_offset <<= 8;
		temp_offset |= data[udp_packdata+i*4+6];
		temp_offset <<= 8;
		temp_offset |= data[udp_packdata+i*4+5];
		
		back_data[HEAD_LEN+388+i*8] = data[udp_packdata+i*4+8];
		back_data[HEAD_LEN+387+i*8] = data[udp_packdata+i*4+7];
		back_data[HEAD_LEN+386+i*8] = data[udp_packdata+i*4+6];
		back_data[HEAD_LEN+385+i*8] = data[udp_packdata+i*4+5];
		
		read_register = SRAM_Register_Start_ADDRESS + temp_offset;
		Dbg_Println(DBG_BIT_NET_STATUS_bit,"temp_offset:%08X data:%d",read_register,temp_data);
		
		if(read_register > SRAM_Register_End_ADDRESS)	
		{
			Dbg_Println(DBG_BIT_NET_STATUS_bit,"Not In The Right Range");
			continue;
		}
		
		temp_data = SRAM_Read_DW(read_register);					//读取寄存器内容
		back_data[HEAD_LEN+392+i*8] = ((temp_data >> 24) & 0xFF);		
		back_data[HEAD_LEN+391+i*8] = ((temp_data >> 16) & 0xFF);
		back_data[HEAD_LEN+390+i*8] = ((temp_data >> 8) & 0xFF);
		back_data[HEAD_LEN+389+i*8] = (temp_data & 0xFF);
	}
	
	Dbg_Print(DBG_BIT_NET_STATUS_bit,"回复查询MCU系统参数信息：");
	
//	for(uint16_t k = 0;k<back_len;k++)
//	{
//		DBG_Printf("%02X ",back_data[k]);
//	}
//	
//	DBG_Printf("\n");
	//数据打包
	NetCRC16(&back_data[0],back_len);
	CH57xNET_SocketUdpSendTo(net_info.SocketId[0], &back_data[0], &back_len, ip, port);    //SOCK_UDPS,
	
	return 0;
}

/*************************
**函数名：Udp_Internal_Device_Control_Process
**参  数：data：接受到的数据   len：长度   ip：网络地址  port：端口
**作  用：UDP 网络控制设备 数据处理       
**返回值：无        
************************/
uint8_t Udp_Internal_Device_Control_Process(uint8_t* data, uint16_t DataLen, uint8_t *ip,uint16_t port)
{
	/*CRC校验*/
	uint16_t data_crc = data[DataLen-2] + (data[DataLen-1] << 8);
	if(NetCRC16_2(data,DataLen-2) != data_crc) return 0xF0;
	
	uint16_t temp = 0,frame_id = 0;
	static uint16_t last_frame_id = 0;
	uint32_t read_addr = 0;
	
	temp = DataLen - 16 - 2;
	Dbg_Println(DBG_BIT_NET_STATUS_bit,"服务器下发设备控制数：%d,数据长度：%d\r\n",data[CMD_OFFSET + 7],temp);
	
	frame_id = data[FRAME_NO_OFFSET];
	frame_id |= data[FRAME_NO_OFFSET + 1] << 8;
	
	if(last_frame_id != frame_id)
	{
		last_frame_id = frame_id;
		/*将数据拷贝到SRAM缓冲区中*/
		SRAM_Write_Byte(data[CMD_OFFSET + 7],SRAM_UDP_RecvData_ControlNum);
		SRAM_DMA_Write_Buff(&data[CMD_OFFSET + 8],temp,SRAM_UDP_RecvData_Startaddr);
		read_addr = SRAM_UDP_RecvData_Startaddr + temp;
		SRAM_Write_DW(read_addr,SRAM_UDP_RecvData_Writeaddr);
	}
	
	Udp_Internal_Process_Ack(In_DevCtr_Cmd,frame_id,ip,port);
	
	return 0;
}

/*************************
**函数名：Udp_Internal_ReadRegister_Process
**参  数：data：接受到的数据   len：长度   ip：网络地址  port：端口
**作  用：UDP 读取映射寄存器 数据处理       
**返回值：无        
************************/
uint8_t Udp_Internal_ReadRegister_Process(uint8_t* data, uint16_t DataLen, uint8_t *ip,uint16_t port)
{
	/*CRC校验*/
	uint16_t data_crc = data[DataLen-2] + (data[DataLen-1] << 8);
	if(NetCRC16_2(data,DataLen-2) != data_crc) return 0xF0;
	
	uint8_t i = 0;
	uint32_t read_register = 0,temp_offset = 0,temp_data = 0;
	uint16_t pack_frame = 0;
	UINT32 back_len = data[udp_packdata]*8+18;		//整包数据长度
	if(back_len > 512) back_len=512;				
	uint8_t back_data[back_len + 2];				//这数据长度根据需要读取的数据长度来决定
	
	memset(back_data,0,sizeof(back_data));
	pack_frame = data[FRAME_NO_OFFSET] + (data[FRAME_NO_OFFSET+1]<<8);   //帧号
	
	/*数据打包*/
	UDP_Add_Header(back_data,In_ReadRegister_Cmd,back_len,pack_frame);            	//添加头部
	
	if(data[udp_packdata] > 35)	back_data[udp_packdata] = 35;	//读取寄存器个数,最大35个
	else back_data[udp_packdata] = data[udp_packdata];		
	/*读取寄存器内容*/
	for(i = 0;i<back_data[udp_packdata];i++)
	{
		temp_offset = data[udp_packdata+i*4+4];
		temp_offset <<= 8;
		temp_offset |= data[udp_packdata+i*4+3];
		temp_offset <<= 8;
		temp_offset |= data[udp_packdata+i*4+2];
		temp_offset <<= 8;
		temp_offset |= data[udp_packdata+i*4+1];
		
		back_data[udp_packdata+i*8+4] = data[udp_packdata+i*4+4];
		back_data[udp_packdata+i*8+3] = data[udp_packdata+i*4+3];
		back_data[udp_packdata+i*8+2] = data[udp_packdata+i*4+2];
		back_data[udp_packdata+i*8+1] = data[udp_packdata+i*4+1];
		
		read_register = SRAM_Register_Start_ADDRESS + temp_offset;
		
		
		if(read_register > SRAM_Register_End_ADDRESS)	
		{
			Dbg_Println(DBG_BIT_NET_STATUS_bit,"Not In The Right Range");
			continue;
		}
		
		temp_data = SRAM_Read_DW(read_register);					//读取寄存器内容
		Dbg_Println(DBG_BIT_NET_STATUS_bit,"temp_offset:%08X data:%08X",read_register,temp_data);
		back_data[udp_packdata+i*8+8] = ((temp_data >> 24) & 0xFF);		
		back_data[udp_packdata+i*8+7] = ((temp_data >> 16) & 0xFF);
		back_data[udp_packdata+i*8+6] = ((temp_data >> 8) & 0xFF);
		back_data[udp_packdata+i*8+5] = (temp_data & 0xFF);
	}
	
	//数据打包
	NetCRC16(&back_data[0],back_len);
	CH57xNET_SocketUdpSendTo(net_info.SocketId[0], &back_data[0], &back_len, ip, port);    //SOCK_UDPS,
	
	return 0x00;
}



/*************************
**函数名：Udp_Internal_WriteRegister_Process
**参  数：data：接受到的数据   len：长度   ip：网络地址  port：端口
**作  用：UDP 设置映射寄存器 数据处理       
**返回值：无        
************************/
uint8_t Udp_Internal_WriteRegister_Process(uint8_t* data, uint16_t DataLen, uint8_t *ip,uint16_t port)
{
	/*CRC校验*/
	uint16_t data_crc = data[DataLen-2] + (data[DataLen-1] << 8);
	if(NetCRC16_2(data,DataLen-2) != data_crc) return 0xF0;
	
	uint8_t i = 0,temp = 0,temp_num = 0;
	uint32_t read_register = 0,temp_offset = 0,temp_data = 0;
	uint16_t pack_frame = 0;
	UINT32 back_len = WriteRegister_Cmd_Send_Len;		//整包数据长度				
	uint8_t back_data[back_len + 2];						//这数据长度根据需要读取的数据长度来决定
	
	memset(back_data,0,sizeof(back_data));
	
	pack_frame = data[FRAME_NO_OFFSET] + (data[FRAME_NO_OFFSET+1]<<8);   //帧号
	
	/*数据打包*/
	UDP_Add_Header(back_data,In_WriteRegister_Cmd,back_len,pack_frame);            	//添加头部

	/*设置寄存器内容*/
	temp = 0x01;
	
	if(data[udp_packdata] > 35)	temp_num = 35;		//设置寄存器个数,最大35个
	else temp_num = data[udp_packdata];	
	
	for(i = 0;i<temp_num;i++)
	{
		temp_offset = data[udp_packdata+i*8+4];
		temp_offset <<= 8;
		temp_offset |= data[udp_packdata+i*8+3];
		temp_offset <<= 8;
		temp_offset |= data[udp_packdata+i*8+2];
		temp_offset <<= 8;
		temp_offset |= data[udp_packdata+i*8+1];
		
		temp_data = data[udp_packdata+i*8+8];
		temp_data <<= 8;
		temp_data |= data[udp_packdata+i*8+7];
		temp_data <<= 8;
		temp_data |= data[udp_packdata+i*8+6];
		temp_data <<= 8;
		temp_data |= data[udp_packdata+i*8+5];
		
		read_register = SRAM_Register_Start_ADDRESS + temp_offset;
		Dbg_Println(DBG_BIT_NET_STATUS_bit,"temp_offset:%08X data:%08X",read_register,temp_data);
		
		if(read_register > SRAM_Register_End_ADDRESS)	
		{
			temp = 0x02;		//写入失败
			Dbg_Println(DBG_BIT_NET_STATUS_bit,"Not In The Right Range");
			continue;
		}
		

		/*授权时间写入时，重新设置一下本地时间 如果本地没有RTC时间则将直接使用UTC时间保存在本地*/
		if(read_register == (SRAM_Register_Start_ADDRESS + Register_MandateUTC_OFFSET))
		{
			S_RTC write_rtc;
			memset(&write_rtc,0,sizeof(write_rtc));
			Unix_Conversion_To_RTC(&write_rtc,temp_data);
			RTC_WriteDate(write_rtc);
		}
		
		/*授权到期时间刷新*/
		if(read_register == (SRAM_Register_Start_ADDRESS + Register_MandateExpiresTime_OFFSET))
		{
			BLV_Set_Authorize_Status(temp_data,0);		
		}
		
		/*设置MCU锁定状态*/
		if(read_register == (SRAM_Register_Start_ADDRESS + Register_MandateLock_OFFSET))
		{
			Dbg_Println(DBG_BIT_NET_STATUS_bit,"Set_Authorize_Lock_Status:%08X",temp_data);
			if(temp_data == 0x00000001)
			{
				Set_Authorize_Lock_Status(0x01);
			}else if(temp_data == 0x00000000)
			{
				Set_Authorize_Lock_Status(0x00);
			}		
		}
		
		SRAM_Write_DW(temp_data,read_register);
		if(temp_data != SRAM_Read_DW(read_register))	temp = 0x02;		//写入失败
	}
	
	Retain_Flash_Register_Data();		//将数据保存到Flash中
	
	back_data[udp_packdata] = temp;		//写入结果
	
	//数据打包
	NetCRC16(&back_data[0],back_len);
	CH57xNET_SocketUdpSendTo(net_info.SocketId[0], &back_data[0], &back_len, ip, port);    //SOCK_UDPS,
	
	return 0x00;
}

/*************************
**函数名：Udp_Internal_BLVPCTest_Process
**参  数：data：接受到的数据   len：长度   ip：网络地址  port：端口
**作  用：PC 下发测试命令处理 
	该函数主要是将MCU管理的设备在线状态回复PC端，测试主要是检测的设备线路是否正常
**返回值：无        
************************/
uint8_t Udp_Internal_BLVPCTest_Process(uint8_t* data, uint16_t DataLen, uint8_t *ip,uint16_t port)
{
	/*CRC校验*/
	uint16_t data_crc = data[DataLen-2] + (data[DataLen-1] << 8);
	if(NetCRC16_2(data,DataLen-2) != data_crc) return 0xF0;
	
	uint16_t pack_frame = 0;
	uint8_t device_num = Find_The_Number_Of_Device_In_The_List();
	UINT32 back_len = device_num * 3 + 18;
	uint8_t back_data[back_len + 2];
	
	memset(back_data,0,sizeof(back_data));
	
	pack_frame = data[FRAME_NO_OFFSET] + (data[FRAME_NO_OFFSET+1]<<8);   //帧号
	
	UDP_Add_Header(back_data,In_BLVPCTest_Cmd,back_len,pack_frame);            	//添加头部
	back_data[udp_packdata] = device_num;
	/*获取设备数*/
	Gets_the_state_of_all_devices(&back_data[udp_packdata + 1],device_num);
	
	NetCRC16(&back_data[0],back_len);
	CH57xNET_SocketUdpSendTo(net_info.SocketId[0], &back_data[0], &back_len, ip, port);    //SOCK_UDPS,
	
	return 0x00;
}


/*************************
**函数名：Udp_Config_Sram_to_Flash
**参  数：data：接受到的数据   len：长度   ip：网络地址  port：端口
**作  用：
**返回值：
	0x01:写入成功
	0x02:写入失败		
************************/
uint8_t Udp_Config_Sram_to_Flash(void)
{
	uint8_t temp_num = 0,i = 0;
	uint16_t sram_len = 0,write_len = 0;
	uint32_t sram_addr = 0,write_addr = 0,read_addr = 0;
	uint8_t config_data[256];
	memset(config_data,0,256);
	
	/*读取SRAM中保存的CONFIG文件*/
	sram_addr = SRAM_Read_DW(SRAM_IAP_CONFIG_DataSize_ADDRESS);		
	if((sram_addr < SRAM_IAP_CONFIG_START_ADDRESS) && (sram_addr > SRAM_IAP_CONFIG_END_ADDRESS)) return 0x02;
	sram_len = sram_addr - SRAM_IAP_CONFIG_START_ADDRESS;
	
	if(sram_len%256 == 0)
	{
		temp_num = sram_len/256;
	}else {
		temp_num = sram_len/256 + 1;
	}
	
	read_addr = SRAM_IAP_CONFIG_START_ADDRESS;
	write_addr = FLASH_CONFIG_RUN_START_ADDRESS;
	if(sram_len > 256) write_len = 256;
	else write_len = sram_len;
	
	/*将配置数据写入Flash中*/
	for(i = 0;i<temp_num;i++)
	{
		SRAM_DMA_Read_Buff(config_data,write_len,read_addr);
		Flash_Write(config_data,write_len,write_addr);
		
		Dbg_Println(DBG_BIT_NET_STATUS_bit,"BLVConfig write Flash addr: %08X Len:%d temp_num:%d i:%d",write_addr,write_len,temp_num,i);
		
		read_addr += write_len;
		write_addr += write_len;
		sram_len = sram_addr - read_addr;
		if(sram_len > 256)	write_len = 256;
		else if((sram_len <= 256) && (sram_len != 0x00))	write_len = sram_len;
		else if(sram_len == 0x00)	break;
	}
	
	Dbg_Println(DBG_BIT_NET_STATUS_bit,"BLVConfig write Flash addr: %08X Len:%d temp_num:%d  i:%d",write_addr,write_len,temp_num, i);
	
	if(i+1 == temp_num)	//写入成功
	{
		/*将数据长度写入到Flash存放config文件头里*/
		
		write_len = sram_addr - SRAM_IAP_CONFIG_START_ADDRESS;
		write_addr = FLASH_CONFIG_RUN_START_ADDRESS + write_len;
		
		Dbg_Println(DBG_BIT_NET_STATUS_bit,"BLVConfig write Flash Succ :%08X!!!!",write_addr);
		
		config_data[0] = (write_addr & 0xFF);
		config_data[1] = ((write_addr >> 8) & 0xFF);
		config_data[2] = ((write_addr >> 16) & 0xFF);
		config_data[3] = ((write_addr >> 24) & 0xFF);
		Flash_Write(config_data,4,FLASH_CONFIG_DataSize_ADDRESS);
		
		return 0x01;
	}else 		//写入失败
	{		
		Dbg_Println(DBG_BIT_NET_STATUS_bit,"BLVConfig write Flash Fail i:%d,Num:%d!!!!",i,temp_num);
		write_addr = FLASH_CONFIG_RUN_START_ADDRESS;
		config_data[0] = (write_addr & 0xFF);
		config_data[1] = ((write_addr >> 8) & 0xFF);
		config_data[2] = ((write_addr >> 16) & 0xFF);
		config_data[3] = ((write_addr >> 24) & 0xFF);
		Flash_Write(config_data,4,FLASH_CONFIG_DataSize_ADDRESS);
		return 0x02;
	}
}

/*************************
**函数名：Udp_Internal_BLVConfig_Process
**参  数：data：接受到的数据   len：长度   ip：网络地址  port：端口
**作  用：BLV-C1配置下发数据处理
**返回值：
	0x00：数据处理成功
	0xF0：接收数据的CRC校验失败
************************/
uint8_t Udp_Internal_BLVConfig_Process(uint8_t* data, uint16_t DataLen, uint8_t *ip,uint16_t port)
{
	/*CRC校验*/
	uint16_t data_crc = data[DataLen-2] + (data[DataLen-1] << 8);
	if(NetCRC16_2(data,DataLen-2) != data_crc) return 0xF0;
	
	uint8_t temp = 0,temp_flag = 0;
	uint8_t back_data[BLVConfig_Cmd_Send_Len + 2]={0};
	UINT32 sendlen = BLVConfig_Cmd_Send_Len;
	uint16_t pack_frame = 0;
	uint16_t save_len = 0;
	uint32_t save_addr = 0;
	
	memset(back_data,0,sizeof(back_data));
	
	pack_frame = data[FRAME_NO_OFFSET] + (data[FRAME_NO_OFFSET+1]<<8);   //帧号
	
	Dbg_Println(DBG_BIT_NET_STATUS_bit,"BLVConfig_Process pack_frame : %04X \r\n",pack_frame);
	
	/*将设备配置数据存入SRAM临时保存起来，写入完毕之后一次性将数据保存到Flash中*/
	

	if(data[HEAD_LEN + 1] == 0x01)		/*当前为第一包*/
	{
		save_addr = SRAM_IAP_CONFIG_START_ADDRESS;				//配置起始地址
	}else if(data[HEAD_LEN + 1] < data[HEAD_LEN])
	{
		save_addr = SRAM_Read_DW(SRAM_IAP_CONFIG_DataSize_ADDRESS);
		if((save_addr < SRAM_IAP_CONFIG_START_ADDRESS) && (save_addr > SRAM_IAP_CONFIG_END_ADDRESS))	temp = 0x02;

	}else if(data[HEAD_LEN + 1] == data[HEAD_LEN])	/*当前为最后一包*/
	{
		temp_flag = 0x01;
		save_addr = SRAM_Read_DW(SRAM_IAP_CONFIG_DataSize_ADDRESS);
		Dbg_Println(DBG_BIT_NET_STATUS_bit,"BLVConfig_Process write addr:%08X\r\n",SRAM_Read_DW(SRAM_IAP_CONFIG_DataSize_ADDRESS));	
		if((save_addr < SRAM_IAP_CONFIG_START_ADDRESS) && (save_addr > SRAM_IAP_CONFIG_END_ADDRESS))	temp = 0x02;
	}else {
		temp = 0x02;
	}
	
	Dbg_Println(DBG_BIT_NET_STATUS_bit,"BLVConfig_Process NUM :%d pack_num : %d addr:%08X\r\n",data[HEAD_LEN],data[HEAD_LEN + 1],save_addr);		//当前包序号
	
	save_len = (data[FRAME_LEN_OFFSET] + (data[FRAME_LEN_OFFSET + 1] << 8)) - HEAD_LEN - 4;		//保存数据长度	
	if((save_len <= 490) && (temp != 0x02))
	{
		SRAM_DMA_Write_Buff(&data[HEAD_LEN + 2],save_len,save_addr);		//写入配置数据内容
		save_addr += save_len;
		SRAM_Write_DW(save_addr,SRAM_IAP_CONFIG_DataSize_ADDRESS);
		Dbg_Println(DBG_BIT_NET_STATUS_bit,"BLVConfig_Process write addr:%08X\r\n",SRAM_Read_DW(SRAM_IAP_CONFIG_DataSize_ADDRESS));	

		/*将配置数据从SRAM写入Flash中*/
		if(temp_flag == 0x01)
		{
			temp = Udp_Config_Sram_to_Flash();		//将数据写入Flash中
			Dbg_Println(DBG_BIT_NET_STATUS_bit,"Udp_Config_Sram_to_Flash rev:%d",temp);	
		}

		temp = 0x01;
	}else {
		//长度不在范围内，写入失败
		temp = 0x02;
	}
	
	UDP_Add_Header(back_data,In_BLVConfig_Cmd,BLVConfig_Cmd_Send_Len,pack_frame);            	//添加头部
	back_data[HEAD_LEN] = temp;
	
	NetCRC16(&back_data[0],BLVConfig_Cmd_Send_Len);
	CH57xNET_SocketUdpSendTo(net_info.SocketId[0], &back_data[0], &sendlen, ip, port);    //SOCK_UDPS,
	
	return 0x00;
}

/**
  * @name  		服务器下发设备动作处理函数
  * @param          无
  * @retval 		无
  */
void Udp_Internal_Dev_Control_Processing(void)
{
	static uint32_t udp_dev_Control_tick = 0;
	if(SysTick_1ms - udp_dev_Control_tick > 10)
	{
		udp_dev_Control_tick = SysTick_1ms;
		
		uint32_t write_addr = SRAM_Read_DW(SRAM_UDP_RecvData_Writeaddr);
//		uint32_t dev_addr;
		uint8_t num = 0;
//		, temp = 0,temp1 = 0,temp2 = 0;
//		uint16_t temp3 = 0;
		
		if(write_addr != SRAM_UDP_RecvData_Startaddr)	//写地址不为空表示缓冲区中有数据
		{
			
			num = SRAM_Read_Byte(SRAM_UDP_RecvData_ControlNum);
			for(uint8_t i=0;i<num;i++)
			{
				{
					uint8_t temp[6];    //设备绝对回路加控制
					
					SRAM_Read_Buff((uint8_t *)&temp, 0x06, SRAM_UDP_RecvData_Startaddr + i*6); //拷贝6个字节
					switch(temp[0])
					{
						case Dev_Host_Invalid:  //场景
							break;
						default:           //其他设备，只要回路不为0，就减1
							if(0x00 != temp[2])  //回路不为0
							{
								temp[2]--;
							}
							break;
					}
					DevActionCtrl(temp, 0x06);    //设备场景控制
				}
//				temp = SRAM_Read_Byte(SRAM_UDP_RecvData_Startaddr + i*6);
//				switch(temp)
//				{
//					case Dev_Host_Invalid:		//场景
//						/*控制设备参数*/
//						temp1 = SRAM_Read_Byte(SRAM_UDP_RecvData_Startaddr + i*6 + 2);	//设备回路
//						temp2 = SRAM_Read_Byte(SRAM_UDP_RecvData_Startaddr + i*6 + 4);	//设备控制
//						Dbg_Println(DBG_BIT_NET_STATUS_bit,"Udp_Dev_Host_Invalid %d,%d ",temp1,temp2);
//					
//						if(temp1 == Project_Bright_Mode)			//明亮模式
//						{
//							SRAM_Write_Byte(0x00,SRAM_Global_NightMode_Start);				//退出起夜模式
//							
//							Project_LightMode_Control(Project_Bright_Mode,0x01);
//						}else if(temp1 == Project_Warm_Mode)		//温馨模式
//						{
//							SRAM_Write_Byte(0x00,SRAM_Global_NightMode_Start);				//退出起夜模式
//							
//							Project_LightMode_Control(Project_Warm_Mode,0x01);
//						}else if(temp1 == Project_Neight_Mode)		//睡眠模式
//						{
//							
//							Project_LightMode_Control(Project_Neight_Mode,0x01);
//						}else if(temp1 == Project_Hypnotic_Mode)	//助眠模式
//						{
//							SRAM_Write_Byte(0x00,SRAM_Global_NightMode_Start);				//退出起夜模式
//							
//							Project_LightMode_Control(Project_Hypnotic_Mode,0x01);
//						}else if(temp1 == Project_Romance_Mode)	//浪漫模式
//						{
//							SRAM_Write_Byte(0x00,SRAM_Global_NightMode_Start);				//退出起夜模式
//							
//							Project_LightMode_Control(Project_Romance_Mode,0x01);
//						}else if(temp1 == Project_Reading_Mode)	//阅读模式
//						{
//							SRAM_Write_Byte(0x00,SRAM_Global_NightMode_Start);				//退出起夜模式
//							
//							Project_LightMode_Control(Project_Reading_Mode,0x01);
//						}else if(temp1 == Project_Relaxation_Mode)	//休闲模式
//						{
//							SRAM_Write_Byte(0x00,SRAM_Global_NightMode_Start);				//退出起夜模式
//							
//							Project_LightMode_Control(Project_Relaxation_Mode,0x01);
//						}else if(temp1 == Project_Viewing_Mode)	//观影模式
//						{
//							SRAM_Write_Byte(0x00,SRAM_Global_NightMode_Start);				//退出起夜模式
//							
//							Project_LightMode_Control(Project_Viewing_Mode,0x01);
//						}
//						
//						break;
//					case Dev_Host_HVout:		//本地继电器
//						dev_addr = Find_Device_List_Information(DEV_C5IO_Type,0x00);		//C5IO 设备类型为0x6,设备地址默认为0x00;
//						if((dev_addr < SRAM_Device_List_Start_Addr) || (dev_addr > SRAM_Device_List_End_Addr))
//						{
//							Dbg_Println(DBG_BIT_NET_STATUS_bit,"Udp_Internal_Dev_Control Devaddr Fail%08X --------\r\n",dev_addr);
//							break;
//						}
//						/*控制设备参数*/
//						temp1 = SRAM_Read_Byte(SRAM_UDP_RecvData_Startaddr + i*6 + 2);	//设备回路
//						temp2 = SRAM_Read_Byte(SRAM_UDP_RecvData_Startaddr + i*6 + 4);	//设备控制
//						Dbg_Println(DBG_BIT_NET_STATUS_bit,"Udp_Internal_Dev_Control C5IO Loop:%02X  start:%02X--------\r\n",temp1,temp2);
//						//
//						temp1 -= 1;
//						BUS_C5IO_Control_Relay(dev_addr,temp1,temp2);
//						
//						break;
//					case Dev_NodeCurtain:		//干接点窗帘控制
//						dev_addr = Find_Device_List_Information(DEV_C5IO_Type,0x00);		//C5IO 设备类型为0x6,设备地址默认为0x00;
//						if((dev_addr < SRAM_Device_List_Start_Addr) || (dev_addr > SRAM_Device_List_End_Addr))
//						{
//							Dbg_Println(DBG_BIT_NET_STATUS_bit,"Udp_Internal_Dev_Control Devaddr Fail%08X --------\r\n",dev_addr);
//							break ;
//						}
//						/*控制设备参数*/
//						temp1 = SRAM_Read_Byte(SRAM_UDP_RecvData_Startaddr + i*6 + 1);		//设备地址
//						Dbg_Println(DBG_BIT_NET_STATUS_bit,"Udp Dev_NodeCurtain %02X --------\r\n",temp1);
//						switch(temp1)
//						{
//							case 0x01:		//窗帘
//								temp2 = SRAM_Read_Byte(SRAM_UDP_RecvData_Startaddr + i*6 + 4);	//设备控制
//								Dbg_Println(DBG_BIT_NET_STATUS_bit,"Udp_Internal_Dev_Control C5IO Loop:%02X  start:%02X--------\r\n",temp1,temp2);
//								
//								if(temp2 == 0x01)		//窗帘开
//								{
//									/*控制窗帘*/
//									Project_Curtain_Control(0x01,0x01);
//								}else if(temp2 == 0x02)	//窗帘关
//								{
//									/*控制窗帘*/
//									Project_Curtain_Control(0x01,0x02);
//								}else if(temp2 == 0x06)	//窗帘停
//								{
//									/*控制窗帘*/
//									Project_Curtain_Control(0x01,0x00);
//								}
//								break;
//							case 0x02:		//窗纱
//								temp2 = SRAM_Read_Byte(SRAM_UDP_RecvData_Startaddr + i*6 + 4);	//设备控制
//								Dbg_Println(DBG_BIT_NET_STATUS_bit,"Udp_Internal_Dev_Control C5IO Loop:%02X  start:%02X--------\r\n",temp1,temp2);
//								
//								if(temp2 == 0x01)		//窗纱开
//								{
//									/*控制窗帘*/
//									Project_Curtain_Control(0x02,0x01);
//								}else if(temp2 == 0x02)	//窗纱关
//								{
//									/*控制窗帘*/
//									Project_Curtain_Control(0x02,0x02);
//								}else if(temp2 == 0x06)	//窗纱停
//								{
//									/*控制窗帘*/
//									Project_Curtain_Control(0x02,0x00);
//								}
//								break;
//						}
//						break;
//					case DEV_RS485_TEMPCTRL1:	//空调控制
//						
//						temp3 = SRAM_Read_Word(SRAM_UDP_RecvData_Startaddr + i*6 + 4);	//设备控制
//						Dbg_Println(DBG_BIT_NET_STATUS_bit,"BLW_C7T_TEMP_Group_Control_State:%04X \r\n",temp3);
//						
//						Project_Temp_Control(0x01,temp3);
//						break;
//					case Dev_Host_Service:		//控制服务信息 - 清理勿扰
//						dev_addr = Find_Device_List_Information(DEV_C5IO_Type,0x00);		//
//						if((dev_addr < SRAM_Device_List_Start_Addr) || (dev_addr > SRAM_Device_List_End_Addr))
//						{
//							Dbg_Println(DBG_BIT_NET_STATUS_bit,"Udp_Internal_Dev_Control Devaddr Fail%08X --------\r\n",dev_addr);
//							break ;
//						}
//						/*控制设备参数*/
//						temp1 = SRAM_Read_Byte(SRAM_UDP_RecvData_Startaddr + i*6 + 2);	//设备回路
//						temp2 = SRAM_Read_Byte(SRAM_UDP_RecvData_Startaddr + i*6 + 4);	//设备控制
//						
//						if(temp1 == 0x02)			//勿扰
//						{
//							if(temp2 == 0x02)
//							{
//								SRAM_Write_Byte(0x00,SRAM_Service_Do_Not_Disturb_State);
//							}else if(temp2 == 0x01)
//							{
//								SRAM_Write_Byte(0x01,SRAM_Service_Do_Not_Disturb_State);
//							}
//							
//							
//						}else if(temp1 == 0x03)		//清理
//						{
//							if(temp2 == 0x02)
//							{
//								SRAM_Write_Byte(0x00,SRAM_Service_Clear_State);
//							}else if(temp2 == 0x01)
//							{
//								SRAM_Write_Byte(0x01,SRAM_Service_Clear_State);
//							}
//						}else if(temp1 == 0x06)
//						{
//							if(temp2 == 0x01)
//							{
//								SRAM_Write_Byte(0x01,SRAM_Service_Check_Out_State);
//							}else if(temp2 == 0x02)
//							{
//								SRAM_Write_Byte(0x00,SRAM_Service_Check_Out_State);
//							}
//						}
//						
//						break;
//					
//				}

			}
			
			/*数据处理完毕，写地址归位*/
			write_addr = SRAM_UDP_RecvData_Startaddr;
			SRAM_Write_DW(write_addr,SRAM_UDP_RecvData_Writeaddr);
		}
	}
}

/*************************
**函数名：Udp_Internal_RoomRent_Process
**参  数：data：接受到的数据   len：长度   ip：网络地址  port：端口
**作  用：PC 下发测试命令处理 
	该函数主要是将房态信息保存在映射寄存器中
**返回值：无        
************************/
uint8_t Udp_Internal_RoomRent_Process(uint8_t* data, uint16_t DataLen, uint8_t *ip,uint16_t port)
{
	/*CRC校验*/
	uint16_t data_crc = data[DataLen-2] + (data[DataLen-1] << 8);
	uint16_t frame_id = 0;
	if(NetCRC16_2(data,DataLen-2) != data_crc) return 0xF0;
	
	uint32_t read_register = 0,temp_data = 0;
	
	frame_id = data[FRAME_NO_OFFSET];
	frame_id |= data[FRAME_NO_OFFSET + 1] << 8;

	temp_data = data[HEAD_LEN];

	read_register = SRAM_Register_Start_ADDRESS + Register_RoomRent_OFFSET;
	
	Dbg_Println(DBG_BIT_NET_STATUS_bit,"Udp_Internal_Dev_Control DEV_NET_ROOMSTATE %d",data[HEAD_LEN]);
	Dbg_Println(DBG_BIT_NET_STATUS_bit,"temp_offset:%08X data:%d",read_register,temp_data);
	
	if(temp_data == 0x02)
	{
		Dbg_Println(DBG_BIT_NET_STATUS_bit,"房间出租");
		SRAM_Write_Byte(0x01,SRAM_Global_Room_Rent_Flag);
		SRAM_Write_DW(SysTick_1ms,SRAM_Global_Room_Rent_Tick);
	}else if(temp_data == 0x04)
	{
		Dbg_Println(DBG_BIT_NET_STATUS_bit,"房间待租");
		SRAM_Write_Byte(0x00,SRAM_Global_Room_Rent_Flag);
	}else if(temp_data == 0x08)
	{
		Dbg_Println(DBG_BIT_NET_STATUS_bit,"房间退房");
		SRAM_Write_Byte(0x00,SRAM_Global_Room_Rent_Flag);
	}else if(temp_data == 0x10)
	{
		Dbg_Println(DBG_BIT_NET_STATUS_bit,"空房");
		SRAM_Write_Byte(0x00,SRAM_Global_Room_Rent_Flag);
	}

	SRAM_Write_DW(temp_data,read_register);
	
	Retain_Flash_Register_Data();		//将数据保存到Flash中
	
	Udp_Internal_Process_Ack(In_Set_RoomRent_Cmd,frame_id,ip,port);
	
	return 0x00;
}



