#ifndef _PROTOCOL_H_
#define _PROTOCOL_H_


#include "stdint.h"

#define PROTOCOL_BUF_MAX (512)

#define PROTOCOL_HEAD    (0x527A)

#define PROTOCOL_FRAME_HEAD_SIZE (7)  // 最小帧长：头(2) + 地址(2) + 命令(1) + 长度(2) + CRC(2) 

#define PROTOCOL_FRAME_MIN_SIZE  (9)  // 最小帧长：头(2) + 地址(2) + 命令(1) + 长度(2) + CRC(2) 
// 错误码定义
typedef enum {
    PROTOCOL_SUCCESS = 0,
    PROTOCOL_ERR_INVALID_PARAM,
    PROTOCOL_ERR_BUFFER_OVERFLOW,
    PROTOCOL_ERR_CRC
}protocol_err_t;

typedef enum
{
  PROTOCOL_CMD_GET_AD =0x81,
  PROTOCOL_CMD_GET_TP =0x82,
    
  PROTOCOL_CMD_SET_ID =0x83,
  PROTOCOL_CMD_GET_ID =0x84,
    
  PROTOCOL_CMD_SET_TP_OFFSET =0x8A,
  PROTOCOL_CMD_GET_TP_OFFSET =0x8B,
    
  PROTOCOL_CMD_RESET_CHANNEL =0x8F,
    
}protocol_cmd_t;

typedef struct __attribute__((packed)) {
    uint8_t status;
    float   value;    
} ch_data_t;

typedef struct __attribute__((packed)) {
    uint8_t dev_status;
    ch_data_t channels[8]; 
    uint8_t   reserved[7];    
} ad_datas_t;

typedef struct __attribute__((packed)) {
    uint8_t dev_status;
    ch_data_t channels[8];
    uint8_t   reserved[7];        
} tp_datas_t;

typedef struct __attribute__((packed)) {
    uint8_t id;
    uint8_t reserved[3];    
} id_data_t;

typedef struct __attribute__((packed)) {
    uint8_t id;
    union{
      uint8_t bytes[4];
      float value;    
    }prams;
    
} offset_set_data_t,offset_get_data_t;

typedef struct __attribute__((packed)) {
    uint8_t id;    
}offset_get_param_t;

extern  int protocol_pack_frame(
    uint16_t slave_addr,
    uint8_t command,const 
    uint8_t *payload,
    uint16_t payload_len,
    uint8_t *buffer,uint16_t buffer_size,
    uint16_t *packed_len
);

extern int protocol_unpack_frame(    
    const uint8_t *frame,
    uint16_t frame_len,
    uint16_t *slave_addr,
    uint8_t *command,
    uint8_t *payload,
    uint16_t *payload_len);
    
#endif







