#ifndef __CONTROL_COMM_H__
#define __CONTROL_COMM_H__

#include "mini58series.h"
#include "CC2530_transparent_analyze.h"
typedef int32_t  s32;
typedef int16_t  s16;
typedef int8_t   s8;

typedef uint32_t u32;
typedef uint16_t u16;
typedef uint8_t  u8;

typedef signed   char       INT8S,  S8;
typedef unsigned char       BYTE,   INT8U;
typedef signed   short int  INT16S;
typedef unsigned short int  WORD,   INT16U, U16;
typedef signed   long       LONG,   INT32S, S32;
typedef unsigned long       DWORD,  INT32U, U32;



//#define   DEVICE_TYPE_AIR_CONDITION       1
//#define   DEVICE_TYPE_FLOOR_HEATING       1
//#define   DEV_TYPE         0   //1:代表水地暖     0代表电地暖

#define   DEVICE_TYPE_FAN       1





#define   ABS(a,b)         ((a>b) ?(a-b) :(b-a))
typedef struct Power_Off_Time
{
	u16 time_ms;
	u8 time_sec;												
	u8 time_min;												
	u8 time_hour;	
	
}TIMEING_OPTION;

typedef struct 
{
				u8		blacklight_flag;
				u8		blacklight_display_flag;
	volatile 	u32 	time_blacklight_ms;
	volatile 	u16 	time_blacklight_s;
}BACKLIGHT_CONTROL;
typedef struct 
{
				u8		zigbee_uart_flag;
				u8      zigbee_uart_display_flag;
	volatile 	u32 	zigbee_uart_ms;
	volatile 	u16 	zigbee_uart_s;
}ZIGBEE_UART_CONTROL;

extern  TIMEING_OPTION  _Time_Power_Off;

#define TEMP_SUCCESS        0X0000

#define SWITCH_ON           1
#define SWITCH_OFF          0
#define RUN_FLAG_1          1
#define RUN_FLAG_0          0
#define ALWAYS_RUN          0xFF 

#define CLEAR_TO_ZERO       0
#define SET_FLAG_0          0
#define SET_FLAG_1          1

#define Min_1               60000
#define Sec_15              15000
#define Sec_10              10000
#define Sec_8               8000
#define Sec_5               5000
#define Sec_3               3000
#define Sec_2               2000
#define Sec_1_5             1500
#define Sec_1               1000
#define Ms_700              700
#define Ms_500              500
#define Ms_300              300
#define Ms_200              200
#define Ms_100              100
#define Ms_50               50
#define Ms_20               20
#define Ms_15               15
#define Ms_10               10
#define Ms_5                5
#define Ms_2                2
#define Ms_1                1
#define TIMER_ZERO          0  //当设置时间为0时，表面此任务不执行
                               //但此任务可以在需要时开起

#define task_Hour_48        172800                               
#define Task_Hour_8         28800
#define Task_Min_1          60
#define Task_Sec_30         30
#define Task_Sec_15         15
#define Task_Sec_10         10
#define Task_Sec_5          5
#define Task_Sec_2          2

#define Task_leave_net_3s   60
#define Task_leave_net_8s   160

#define ENABLE_FAST_AGING_TEST		0

#if ENABLE_FAST_AGING_TEST
	#define FAST_AGING_STEP1_TIME   (60)				//1min
	#define FAST_AGING_STEP2_TIME   (3*60)				//2.5min
#else
	#define FAST_AGING_STEP1_TIME   (15*60)			//15min
	#define FAST_AGING_STEP2_TIME   (60*60*4)		//4h
#endif


struct task_components_t
{
    u8  serial_number;             // 任务编号
    u8  task_switch;               // 任务开关
    u8  run_flag;                  // 执行任务标志---0不执行，1执行
    u8  external_open_flag;        // 任务已开启标志（避免多次开启此任务，选用）
    u16 timer;                     // 计时器
    u16 interval_time;             // 任务运行间隔时间(和计时器时间相同，为不变值)
    void (*task_hook)(void);       // 要运行的任务函数
};  

extern struct task_components_t task_comps[];

typedef union
{
    u16 u16; // element specifier for accessing whole u16
    s16 i16; // element specifier for accessing whole i16

    struct
    {
      u8 u8L; // element specifier for accessing low u8
      u8 u8H; // element specifier for accessing high u8
    } s16_t; // element spec. for acc. struct with low or high u8
} nt16;

typedef enum
{
    Ack = 0,
    NO_ACK = 1,
} etI2cAck;


typedef enum
{
    ACK_ERROR               = 0x01,
    TIME_OUT_ERROR          = 0x02,
    CHECKSUM_ERROR          = 0x04,
    UNIT_ERROR              = 0x08
} etError;


struct device_t
{
	u8  channel;
	u8  product_id;
	u16 type;
};


//typedef struct Strainer_Net_Time
//{
//	u8  time_sec;												
//	u8  time_min;												
//	u8  time_hour;
//	
//}STRAINER_OPTION;

//extern STRAINER_OPTION Strainer_Net_Used_Time;


#define PRODUCTID  92
#define OPERATEID  15002

#define HI_UINT16(a) (((a) >> 8) & 0xFF)
#define LO_UINT16(a) ((a) & 0xFF)

#define DISABLE_DEBUG    					1							//lmm debug

/***************** 版本控制 ***************/
#define KK_PANEL_VERSION    "v1.1.8" 
#define PROTOCOL_VER 0
#define VER_1 1
#define VER_2 1
#define VER_3 8
/*****************************************/

////////////////////////非透传协议四个网络状态/////////////////////////////
#define OUT_LINE                0   //未入网
#define IN_LINE                 1   //已入网
#define IN_LINE_NETWORKING      2   //已入网且允许其他设备入网
//#define OUT_LINE_NETWORKING     3   //未入网且准备入网
#define JOIN_FAILED     		3
///////////////////////////////////////////////////////////////////////////
#define RECV_ACK_SUCC           0   //执行成功
#define RECV_ACK_FAILED         1   //执行失败


#define  RCV_MAX   100

////////////////////////////////透传类opcode////////////////////////////////////


#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_TRIAGGER_REPORT        0x1031    //接近感应触发上报
#define OPCODE_GENERAL_OTA_CONTROL        				0x1040    //ota 相关
#define OPCODE_GENERAL_OPCODE_TEST_PASS         	    0x01FF    //产测通过标志
////////////////////////////////通用透传类opcode////////////////////////////////////
#define OPCODE_GENERAL_STARTUP              0x0007       //上电上报
#define OPCODE_GENERAL_NIGHT_OFF            0x0011       //夜间模式
#define OPCODE_GENERAL_INDENTIFY_DEV        0x000C       //设备定位
#define OPCODE_GENERAL_GET_VERSION          0x0012       //查看版本号
#define OPCODE_GENERAL_GET_DEV_INFO         0x000B       //查看设备通道信息

////////////////////////////////非透传opcode////////////////////////////////
#define _OPCODE_CHECK_STATUS                0X0000      //查询设备状态
#define _OPCODE_NETWORK_BACK                0X0001      //设备退网
#define _OPCODE_FORCE_TO_RESTART            0X0002      //设备强制重启
#define _OPCODE_PUSH_NET_STATE              0X0003      //2530推送网络状态
#define _OPCODE_INQUIRE_NET_STATE           0X0004      //查询网络状态
#define _OPCODE_SET_DEV_RUN_STATE           0X0005      //配置设备运行状态入网状态
#define _OPCODE_UART_TEST                   0X0006      //串口测试
#define _OPCODE_SET_NET_STATE               0X1000      //配置设备入网
#define _OPCODE_QUERY_NET_STATE             0X1001      //查询当前网络状态
#define _OPCODE_PUSH_NETWORK_STATE          0X1002      //网络状态推送



enum{
	OTA_STATUS_START,
	OTA_STATUS_SUCCESS,
	OTA_STATUS_FAILURE,
	OTA_STATUS_TIMEOUT,
};
void  Send_Device_Data(u8 channel_id,u16 opcode ,u8 *arg,u8 len);
static void network_state_machine(TP_uc8 recv_network_state);
void state_change_report(TP_u16 opcode, TP_u8 channel);
void Setup_Task_Start(void);
u8 Get_Channel_Id(u8 device_type);
void delay_operate_calibrate_temper_Task_Start(void);
void operate_calibrate_temper_delay_2s_off_Task_Start(void);
void operate_calibrate_temper_delay_2s_off_Task_Stop(void);
u8  is_operate_calibrate_temper_delay_2s_off_Task_start(void);
void send_read_operateID_productID(void);
void aging_test_start(void);
void aging_test_stop(void);
u8 Get_Display_Status(void);
u8 IsLcdEnterScreenSave(void);
void ChildLockLedFlash(void);
void  zero_check_init(void);
void delay_operate_calibrate_temper_Task_Stop(void);
void delay_operate_delay_off_Task_Start(void);
void delay_operate_delay_off_Task_Stop(void);
void Reset_DelayOFF_timer(u8 device_type);
void task_exit_aging_test_start(void);

#define  Network_Control(join_or_leave)    Send_Device_Data(0,_OPCODE_SET_NET_STATE,&join_or_leave,1)
#define  proximity_triagger_report_zigbee()    do{	uint8_t res = 1;  \
													Send_Device_Data(0,OPCODE_GENERAL_PROXIMITY_TRIAGGER_REPORT,&res,1);}while(0);

#define	 Query_Device_Info(res)			   Send_Device_Data(0,OPCODE_GENERAL_STATUS_QUERY,&res,1);
void Update_Dev_Info(u8 channel_id);

extern void Delay_ms(u16 sec);

extern  void recover_current_state(void);

extern  void save_data(void);

extern  void temp_init(void);

extern void  synch_temp(void);

extern void check_error_report(TP_u16 opcode, TP_u8 channel);

extern void Ararm_Init(void);

extern void  Tap_Init(void);

void set_2530_net_state(void);

void CC2530_leave_net_check(void);



void task_CC2530_state_check(void);

void tm_change_report(TP_u16 opcode, TP_u8 channel);



static void send_ack(PROTOCOL_DEAL recv_data, TP_uc8* arg, TP_u8 len);

void power_on_report(void);
void Spake_Start(void);
void delay_confirm_system_control(u8 start);
void delay_confirm_fan_speed_control(u8 start);
void Print_Buff(u8 *data,u8 len);
#endif   


