#ifndef BSP_HMI_H_
#define BSP_HMI_H_

#ifdef __cplusplus
extern "C" {
#endif

#include "kit_macro.h"

#define HMI_BRROCAST_TYPE       (0xFu)
#define HMI_BRROCAST_ADDR       (0xFFu)
#define HMI_DATA_POS            (6u)
#define HMI_FUNC_CODE_POS	    (5u)
#define HMI_DEVICE_TYPE_POS	(3u)
#define HMI_SLAVE_ADDR_POS	(4u)
#define HMI_BUF_LEN_POS     (1u)

#define HMI_PROC_BRROCAST_TYPE       (0xFFu)
#define HMI_PROC_BRROCAST_ADDR       (0xFFu)

#define HMI_PROTOCOL_DATA_POS    (11u)
#define HMI_PROTOCOL_START_POS	 (0u)  //起始字符（stx）
#define HMI_PROTOCOL_BUF_LEN_POS (2u) //字节长度 (bytelen)
#define HMI_PROTOCOL_SEND_TYPE_POS     (4u) //发送类型
#define HMI_PROTOCOL_RECV_TYPE_POS     (5u) //接收类型
#define HMI_PROTOCOL_CMD_POS     (6u) //命令类型
#define HMI_PROTOCOL_FUN_POS     (7u) //功能类型
#define HMI_PROTOCOL_SRLI_POS    (8u) //序号码 (serial1)
#define HMI_PROTOCOL_SRLII_POS   (9u) //序号码 (serial1)
#define HMI_PROTOCOL_SRLIII_POS  (10u)//序号码 (serial1)

#define HMI_PROTOCOL_MIN_LEN     (13u)//私有协议请求长度


//命令码
typedef enum
{
  
    kHmiMainFun_Info = 0x01,
    kHmiMainFun_Cfg,
	kHmiMainFun_Ctrl,
}HmiMainFunc;


//子功能码
typedef enum
{
	kHmiSubFun_0x10= 0x10,
    kHmiSubFun_0x01 = 0x01,
    kHmiSubFun_0x02 = 0x02,
}HmiSubFunc;




typedef enum
{
    kHmiMainCmd_Test = 0x01,
    kHmiMainCmd_Info,
    kHmiMainCmd_Cfg,
    kHmiMainCmd_Boot,
    kHmiMainCmd_Log,
    kHmiMainCmd_Tool,
}HmiMainCmd;


typedef enum
{
    kHmiToolSubCmd_Reserve,
    kHmiToolSubCmd_HandShake,
    kHmiToolSubCmd_TestItem,
    kHmiToolSubCmd_End,
}HmiToolSubCmd;

typedef enum
{
    kHmiOccupy_None,
    kHmiOccupy_Request,
    kHmiOccupy_Forward,
    kHmiOccupy_End,
}HmiOccupy;

struct _HmiItem;
typedef bool (*HmiSendCall)(uint8_t comm_dev, uint8_t *buf, int32_t len);
typedef void (*Fun07Call)(struct _HmiItem *item, uint8_t sub_fun, uint8_t *buf, int32_t len);
typedef void (*HmiFuncCall)(struct _HmiItem *item, uint8_t main_fun, uint8_t sub_fun, uint8_t *buf, uint16_t len);
typedef void (*HmiRxIntCtrlCall)(uint8_t comm_dev, bool is_en);

typedef struct _HmiItem
{
	uint8_t  rcv_addr;
	uint8_t  rcv_addr1;
	uint8_t  rcv_addr2;
	uint8_t  rcv_addr3;
	uint8_t  main_func;
	uint8_t  sub_func;
    uint8_t  snd_type;
    uint8_t  rcv_type;
	uint8_t  comm_dev;
    uint8_t  dev_addr;
	HmiOccupy occupy;
    uint16_t time_out;

    uint16_t occupy_dly;
    uint16_t buf_pos;
    uint16_t buf_len;
    uint32_t tick;
    uint8_t  * comm_buf;
    struct _HmiItem *fwd_item;
    HmiFuncCall func_call;
    HmiSendCall send_call;
    HmiRxIntCtrlCall ctrl_rx_int_call;
    Fun07Call  fun_07_call;
	

} HmiItem;


#define HMI_STATIC_INIT(_name, _buf, _buf_len, _comm_dev,_send_call, _ctrl_rx_int_call, _func_call)  \
    HmiItem _name = \
    {   \
		0,  \
		0,  \
		0,  \
		0,  \
		0,  \
		0,  \
		0,  \
        0,  \
		(_comm_dev),  \
        0,  \
        (kHmiOccupy_None), \
        0,  \
        0,  \
        0,  \
        (_buf_len),   \
        0,  \
		_buf,\
        NULL,   \
        _func_call,    \
        _send_call,    \
        _ctrl_rx_int_call, \
    }
    
    
void bsp_hmi_neg_send(HmiItem *item, uint16_t ex_code);
void bsp_hmi_set_dev_addr(HmiItem *item, uint8_t addr);
void bsp_hmi_set_comm_dev(HmiItem *item, uint8_t dev);
void bsp_hmi_set_fun_07_call(HmiItem *item, Fun07Call call);
void bsp_hmi_pos_send(HmiItem *item, uint8_t *buf,uint16_t len);
	
void bsp_hmi_analyse(HmiItem *item, bool is_reflesh);
void bsp_hmi_push_data(HmiItem *item, uint8_t *buf, uint16_t len);
bool bsp_hmi_forward(HmiItem *rcv_item, HmiItem *fwd_item, uint16_t len);
bool bsp_hmi_request(HmiItem *item, uint8_t master_type, uint8_t slave_type, uint8_t slave_addr, uint8_t main_fun, uint8_t sub_fun, uint8_t *buf, uint16_t len, uint16_t time_out);

//默认协议发送接口
void bsp_hmi_send(HmiItem *item, uint8_t *buf,uint16_t len);
	
#ifdef __cplusplus
}
#endif

#endif
