/*
 * dbn_ble_srv.h
 *
 *  Created on: Aug 27, 2024
 *      Author: Thinkpad
 */

#ifndef INCLUDE_DBN_BLE_SRV_H_
#define INCLUDE_DBN_BLE_SRV_H_

#include "config.h"
#include <stdint.h>

/*
 * Magic  |       Header      |  Data  |   CheckByte
 *        | Addr/Sub Len CMD  |        |    Xor  Sum
 * 1Byte  | 1B       1B   1B  |    xx  |    1B  1B
 *
 * * Len = len(CMD) + len(Data)
*/
#define     MAGIC_BYTE_DBN_DEFAULT              0x8F
#define     MAGIC_BYTE_DBN_TRANSPARENT          0x7F        // 用于 显示屏协议、地感协议、探头协议等
#define     MAGIC_BYTE_DBN_SUB_OTA              0x9F        // 用于 透传子设备OTA升级


#define     CMD_DBN_UPDATE_DEV_SERIAL            0x09
#define     CMD_DBN_GET_DEV_INFO                 0x10
#define     CMD_DBN_SET_LSSC_NET                 0x11
#define     CMD_DBN_GET_LSSC_NET                 0x12
#define     CMD_DBN_SET_IOT_NET                  0x13
#define     CMD_DBN_GET_IOT_NET                  0x14
#define     CMD_DBN_SET_IOT_TOPIC                0x15
#define     CMD_DBN_GET_IOT_TOPIC                0x16
#define     CMD_DBN_SET_SUB_TRAFFIC_PARAM        0x17
#define     CMD_DBN_GET_SUB_TRAFFIC_PARAM        0x18


#define     CMD_DBN_CHECK_PASS                   0x1C
#define     CMD_DBN_MODIFY_PASS                  0x1D



#define     CMD_DBN_SET_FACTORY                  0x1E
#define     CMD_DBN_RESET_DEV                    0x1F

#define     CMD_DBN_SET_NOTIFY                   0x20
#define     CMD_DBN_SET_NOTIFY_LOOP              0x21
#define     CMD_DBN_SET_SUB_CODE                 0x22   //使能 子功能
#define     CMD_DBN_SET_CJQ_PARAM                0x23   // 设置 车检器参数
#define     CMD_DBN_GET_CJQ_PARAM                0x24   // 读取 车检器参数


#define     CMD_DBN_RW_UART_BAUD                 0x31 


#define     CMD_DBN_LOOP_SAMPLE_PARAM            0x87
#define     CMD_DBN_LOOP_BALANCE_PARAM           0x88
#define     CMD_DBN_LOOP_RELEASE_PLANB           0x89
#define     CMD_DBN_LOOP_SENS_LIST               0x8A
#define     CMD_DBN_SET_CJQ_FACTORY              0x92


#define CMD_SUB_SENS_REPORT             0xC0          //设备主动上报传感信息
// #define CMD_REQUIRE_DEV_MAC_ADDR        0xC1
// #define CMD_CHANGE_DEV_ADDR_BY_MAC      0xC2
// #define CMD_SET_LED                     0xC3
// #define CMD_SET_RELAY                   0xC4
#define CMD_SENS_ACS_ENABLE             0xC5         // 设备主动上报  使能




#define MAX_BLE_DAT_RESPONSE_LEN                (BLE_BUFF_MAX_LEN - 4) // 14  //返回 的所有字节数不能超过20个字节，除去 magic、header和ckb校验字节，剩下数据的字节数为14
#define MAX_BLE_Notify_Buf_LEN                  BLE_BUFF_MAX_LEN  //24
#define MAX_BLE_BUF_LEN                         BLE_BUFF_MAX_LEN    //128
#define MAX_BLE_TMP_BUF_LEN                     BLE_BUFF_MAX_LEN

typedef struct _BLE_Notify_Buf_
{
    uint8_t flag;   //0 idle, 1 buf ready
    uint8_t len;
    uint8_t buf[MAX_BLE_Notify_Buf_LEN];
}BLE_Notify_Buf;

typedef struct _BUF_DBN_BLE_
{
    uint8_t flag;
    uint8_t magic;
    uint8_t cmd;
    uint8_t dat_len ;
    uint8_t dat_offset;
    uint8_t pkg_amount;
    uint8_t pkg_seq;
    uint8_t dat[MAX_BLE_BUF_LEN];
} Buf_DBN_BLE;

extern Buf_DBN_BLE g_buf_ble_response;

extern BLE_Notify_Buf  g_notify_buftemp;
extern uint8_t g_flag_notify_temp;
extern BLE_Notify_Buf  g_ble_rcv_buf;


extern uint16_t loop1_LPCNT;
extern uint8_t g_freq_sens;
extern uint8_t loop1_SensLevel;
extern uint8_t g_freq_level;
extern uint32_t g_sys_freq;
extern float g_freq_tmp;



void clear_ble_notify_buf(BLE_Notify_Buf * buf);
uint8_t set_response_tran_to_notify(uint8_t *dat_ori, uint8_t dat_len, BLE_Notify_Buf * notify_dst);

void poll_dbn_ble(void);
void report_sens_acs(void);

void set_factory_param(void);
void factory_dev(void);
void para_store_init(void);


#endif /* INCLUDE_DBN_BLE_SRV_H_ */
