#ifndef ______00IKONKE_UART_HANDLER_H_____
#define ______00IKONKE_UART_HANDLER_H_____

#include "app/framework/include/af.h"
#include "././00ikonke-app/general/ikk-debug.h"
#include <00ikonke-app/general/ikk-opt-tunnel.h>
#include <00ikonke-app/driver/ikk-uart.h>
#define BUILD_UINT16(lowByte, highByte) \
          ((uint16_t)(((lowByte) & 0x00FF) + (((highByte) & 0x00FF) << 8)))

#define HIGH_UINT16(a) (((a) >> 8) & 0xFF)
#define LOW_UINT16(a) ((a) & 0xFF)


#define     LENGTH_INDEX									0X02
#define     SEQENCE_INDEX									0X04
#define     DATA_FIELD_INDEX								0X05
#define     CHANNEL_ID_INDEX								0X06
#define     OPCODE_INDEX									0X07
#define 	POLAD_INDEX										0X09

#define 	MSG_HEAD_1										0XAA
#define 	MSG_HEAD_2										0X55

#define 	CMD_NET_REQUEST									0X1000
#define 	CMD_NET_QUERY									0X1001
#define 	CMD_NET_NOTIFY									0X1002
#define 	CMD_MCU_VERSION_QUERY							0X101D

#define 	OPCODE_GENERAL_MODEID_REPORT                    0x100A    //设备modeid上报
#define 	OPCODE_GENERAL_SWITCH               			0x1010    //执行开关动作
#define 	OPCODE_GENERAL_OPERATION_MODE       			0x1011    //设定运行模式
#define 	OPCODE_GENERAL_SET_TEMPERATURE      			0x1012    //设定温度
#define 	OPCODE_GENERAL_SET_WIND_SPEED       			0x1013    //设定风速
#define 	OPCODE_GENERAL_ERROR_CODE      					0x1014    //故障码
#define 	OPCODE_GENERAL_SET_CHILD_LOCK     				0x1015    //童锁
#define 	OPCODE_GENERAL_SET_DELAY_OFF_TIME         		0x1016    //延时关机
#define 	OPCODE_GENERAL_SET_SWING_MODE  					0x1017    //设置摆风模式
#define 	OPCODE_GENERAL_SET_SCREEN_SAVE_TIME           	0x1018    //设置屏保时间
#define 	OPCODE_GENERAL_SET_FILTER_CLEANING_TIME         0x1019    //设置滤网清洗时间
#define 	OPCODE_GENERAL_CURRENT_REAL_TEMPER         		0x101A    //当前环境温度
#define 	OPCODE_GENERAL_SET_TEMPER_CALIBRATION        	0x101B    //设置校准温度
#define 	OPCODE_GENERAL_SET_NO_DISTURB_MODE        		0x101C    //设置勿扰模式
#define 	OPCODE_GENERAL_STATUS_QUERY                     0x101D    //查询设备状态
#define 	OPCODE_GENERAL_BING_DEVICE                      0x101E    //绑定设备
#define 	OPCODE_GENERAL_MCU_DATA_QUERY                   0x101F    //同步mcu信息

#define 	OPCODE_GENERAL_REPORT_STATUS        			0x1020    //状态改变上报
#define 	OPCODE_GENERAL_LCD_OFF        					0x1030    //息屏命令下发
#define 	OPCODE_GENERAL_PROXIMITY_TRAGGER_REPORT        	0x1031    //接近感应有人触发
#define 	OPCODE_GENERAL_OTA_CONTROL        				0x1040    //ota 相关

#define 	Contro_OnOFF(x)									CMD_Control_Emplement(CMD_ONOFF,(uint8_t *)&x,1)
#define 	Contro_System_Mode(x)							CMD_Control_Emplement(CMD_RUNNING_MODE,(uint8_t *)&x,1)
#define 	Contro_Target_Temperature(x)					CMD_Control_Emplement(CMD_TARGET_TEMPERATURE,(uint8_t *)&x,2)
#define 	Contro_Fan_Level(x)								CMD_Control_Emplement(CMD_FAN_SETTING,(uint8_t *)&x,1)
#define 	Contro_Child_Lock(x)							CMD_Control_Emplement(CMD_CHILD_LOCK,(uint8_t *)&x,1)
#define 	Contro_Delay_Off_Time(x)						CMD_Control_Emplement(CMD_DELAY_OFF,(uint8_t *)&x,1)
#define 	Contro_Swing_Mode(x)							CMD_Control_Emplement(CMD_SWING_MODE,(uint8_t *)&x,1)
#define 	Contro_Screen_Save_Time(x)						CMD_Control_Emplement(CMD_SCREEN_SAVE_TIME,(uint8_t *)&x,1)
#define 	Contro_Filter_Wash_Time(x)						CMD_Control_Emplement(CMD_FILTER_WASH_TIME,(uint8_t *)&x,2)
#define 	Contro_Calibrate_Temper(x)						CMD_Control_Emplement(CMD_CALIBRATION_TEMPERATURE,(uint8_t *)&x,1)
#define 	Contro_No_Disturb_Mode(x)						CMD_Control_Emplement(CMD_NO_DISTURB_MODE,(uint8_t *)&x,1)


#define 	Uart_Send(st,data) 				uart_send_data(st,0x30,0xff,data,true)
#define		Uart_Resp(st,seq,data)			uart_send_data(st,0x20,seq,data,false)


enum{
	TP_SUCCESS,
	TP_FAILUER,
};

enum{
	OTA_STATUS_START,
	OTA_STATUS_SUCCESS,
	OTA_STATUS_FAILURE,
	OTA_STATUS_TIMEOUT,
};
typedef struct
{
	#pragma pack(2)
	uint8_t    	error;								//error code
	uint8_t    	on_off;								//开关
	uint8_t    	lock_flage;                       	//儿童锁显示图标
	uint8_t    	system_type; 						//系统类型
	uint16_t   	set_temperature;				    //设置温度
	int16_t   	real_temperature;				    //环境温度
	uint8_t    	wind_speed;                       	//风速   0 关 1 低 2 中 3 高，4 开，5，自动 ，6 智能 7，中低，8 中高 ，9 超高
	uint8_t    	air_wind_direct;					//风向  0 下 1，中 2，高
	uint8_t    	power_off_time;                   	//倒计时时间
	uint16_t   	filter_wash_time;					//滤网清洗时间
	uint16_t   	screen_save_time;					//屏保时间
	int8_t    	correct_temperature;              	//温度校准值
	uint8_t    	no_disturb_mode;					//勿扰模式
	uint8_t		air_brand;							//空调品牌
	uint8_t		air_type;							//空调类型
}__attribute__((packed))AIR_ATTR_ST;
typedef struct
{
	uint8_t    	error;								//error code
	uint8_t    	on_off;								//开关
	uint8_t    	lock_flage;                       	//儿童锁显示图标
	uint8_t    	system_type; 						//系统类型
	uint16_t   	set_temperature;				    //设置温度
	int16_t   	real_temperature;				    //环境温度
	uint8_t    	power_off_time;                   	//倒计时时间
	uint16_t   	screen_save_time;					//屏保时间
	int8_t    	correct_temperature;              	//温度校准值
	uint8_t    	no_disturb_mode;					//勿扰模式
}__attribute__((packed))FLOOR_HEATING_ATTR;

typedef struct
{
	uint8_t    	error;								//error code
	uint8_t    	on_off;								//开关
	uint8_t    	lock_flage;                       	//儿童锁显示图标
	uint8_t    	system_type; 						//系统类型
	int16_t   	real_temperature;				    //环境温度
	uint8_t    	wind_speed;                       	//风速   0 关 1 低 2 中 3 高，4 开，5，自动 ，6 智能 7，中低，8 中高 ，9 超高
	uint8_t    	power_off_time;                   	//倒计时时间
	uint16_t   	filter_wash_time;					//滤网清洗时间
	uint16_t   	screen_save_time;					//屏保时间
	int8_t    	correct_temperature;              	//温度校准值
	uint8_t    	no_disturb_mode;					//勿扰模式
}__attribute__((packed))FAN_ATTR_ST;

typedef enum
{
	CMD_ERROR_CODE    		 = 0,
	CMD_CHILD_LOCK    	 		,
	CMD_WIND_SPEED    	 		,
	CMD_DELAY_OFF     		 	,
	CMD_WIND_DIRECT     	 	,
	CMD_SCREEN_SAVE     	 	,
	CMD_FILTER_CLEAN     	 	,
	CMD_SYSTEM     	 			,
	CMD_TARGET_TEMPER     	 	,
	CMD_CURRENT_TEMPER     	 	,
	CMD_CALI_TEMPER     	 	,
	CMD_ON_OFF     	 			,
	CMD_NO_DISTURB_MODE     	,
}CMD_TYPE;

#pragma pack(1)
typedef struct
{
	AIR_ATTR_ST 			air_condition;
	FAN_ATTR_ST 			fan;
	FLOOR_HEATING_ATTR		floor_heating;
	uint8_t res[3];
}DEVICE_ATTR_ST;
#pragma pack()


extern DEVICE_ATTR_ST  sDevice_attr_new;
extern DEVICE_ATTR_ST  sDevice_attr_last;
extern UMsgNodeSt 		g_sMsgNode;
typedef struct
{
	uint8_t  ucData_Field_Len;
	uint8_t  ucChannel_ID;
	uint16_t uiOpcode;
	uint8_t  uiPaload[50];
}DATA_FIELDED;
void  Query_MCU_Info(void);
void  Binging_Info_Notify(uint8_t bind_or_unbind,uint8_t device_type);
void Set_No_Disturb_Mode(uint8_t channel_id,uint8_t mode);
void Set_LCD_OFF(void);
void Set_OTA_CMD(uint8_t OTA_Status);
void Rewite_Attr_Witchout_Report(void);


void  	Ack(UMsgNodeSt *uMsgNode);
uint16_t Swap_16U_Data(uint16_t data);
void 	Command_Process_Handler(DATA_FIELDED *Data_Field);
void 	Control_MCU(UMsgNodeSt *uMsgNode,uint8_t channel_id,CMD_TYPE Cmd_Type,uint16_t Data );
void 	CMD_Control_Emplement(UMsgNodeSt *uMsgNode,uint8_t channel_id,uint16_t Opcode,uint8_t *Pload,uint8_t Pload_Len);
uint8_t uart_send_data(UMsgNodeSt *uMsgNode,uint8_t ucControl_Field,uint8_t uiSeq, DATA_FIELDED *dataField,bool isGetResult);
uint16_t check_sum( uint8_t* msg, uint8_t len);
bool 	MsgCRC16(uint8_t *pdat, int length );
void Push_Netwaork_Status(uint8_t network_status);

#endif /* 00IKONKE_APP_BL55072A_H_ */
