Files
JBao_Magic_Box_FW/apps/usr_le_code/bleproto_api.h
T

304 lines
12 KiB
C

/****************************************************************************/
/* bleproto_api.h
*
* Copyright (C) 2021 四川迈科创智科技有限公司
*
****************************************************************************/
/**
* @page bleproto_api_guides API Guides
*
* @ingroup bleproto_api_guides
* @addtogroup bleproto_api bleproto
*
* wiki
* ====
* - https://gogos.mkcziot.com:13000/iot-device/cziot-sdk/src/master/doc/wiki/ble/mk蓝牙设备开发指南.md
*
* @{
*/
#ifndef BLEPROTO_API_H_INCLUDE
#define BLEPROTO_API_H_INCLUDE
/****************************************************************************/
/* Included Files */
/****************************************************************************/
/* system */
//#include <stdio.h>
/*#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
#include <inttypes.h>*/
/* group */
#include "bleproto_packer.h"
#include "ble_proto.h"
/****************************************************************************/
/* Configure Definitions */
/****************************************************************************/
/****************************************************************************/
/* Pre-processor Definitions */
/****************************************************************************/
/* api version */
#define BLEPROTO_API_VERSION "1.1.4"
/****************************************************************************/
/* Public Types */
/****************************************************************************/
/**
* @brief 应用数据描述
*/
typedef struct bleproto_appdata_desc {
/** 包头 */
bleproto_appdata_header_t header;
/**
* 根据
* - header.msgtype:请求(0=req)、响应(1=rsp)
* - header.serviceid @ref bleproto_service_id_e
* datast取对应的member
*
* 比如 header.msgtype = 0(请求req), header.serviceid = E_BLEPROTO_SERVICE_ID_DEVICEINFO
* datast 对应 deviceinfo_req
*/
union {
bleproto_deviceinfo_req_t deviceinfo_req;
bleproto_deviceinfo_rsp_t deviceinfo_rsp;
bleproto_authsetup_req_t authsetup_req;
bleproto_authsetup_rsp_t authsetup_rsp;
bleproto_authdelete_req_t authdelete_req;
bleproto_authdelete_rsp_t authdelete_rsp;
bleproto_runcmd_req_t runcmd_req;
bleproto_runcmd_rsp_t runcmd_rsp;
bleproto_reportcmd_req_t reportcmd_req;
bleproto_reportcmd_rsp_t reportcmd_rsp;
bleproto_doaction_req_t doaction_req;
bleproto_doaction_rsp_t doaction_rsp;
bleproto_otanotify_req_t otanotify_req;
bleproto_otanotify_rsp_t otanotify_rsp;
bleproto_otadata_req_t otadata_req;
bleproto_otadata_rsp_t otadata_rsp;
ble_proto_packet_t runcmd_v2_req;
ble_proto_packet_t runcmd_v2_rsp;
ble_proto_packet_t reportcmd_v2_req;
ble_proto_packet_t reportcmd_v2_rsp;
ble_proto_packet_t otadata_v2_req;
ble_proto_packet_t otadata_v2_rsp;
} datast;
} bleproto_appdata_desc_t;
/****************************************************************************/
/* Public Data */
/****************************************************************************/
/****************************************************************************/
#ifdef __cplusplus
extern "C" {
#endif
/****************************************************************************/
/* Public Function Prototypes */
/****************************************************************************/
/**
* @brief Get library version
*
* @return The string of library version
*/
const char *libbleproto_version(void);
/**
* @brief 序列化请求注册绑定广播数据(广播数据长度为31)
*
* @param[out] data - 序列化数据缓冲区, 固定为31个字节.
* @param[in] txpower - 蓝牙发射功率[-127,128]dBm
* @param[in] manucode - 厂商编码, 现在固定填0
* @param[in] pcode - 平台pcode, 比如pcode为AAABBB
* pcode[0] = 'A' = 0x41
* pcode[1] = 'A' = 0x41
* pcode[2] = 'A' = 0x41
* pcode[3] = 'B' = 0x42
* pcode[4] = 'B' = 0x42
* pcode[5] = 'B' = 0x42
* @param[in] prodcode - 产品型号, 比如 宠物喂食器 CBFD,
* prodcode[0] = 'C' = 0x43
* prodcode[1] = 'B' = 0x42
* prodcode[2] = 'F' = 0x46
* prodcode[3] = 'D' = 0x44
*/
uint8_t bleproto_advdata_encode4bind(
uint8_t data[31],
int8_t txpower,
uint8_t manucode[BLEPROTO_ADV_TLV_LENGTH_MANUCODE],
uint8_t pcode[BLE_PROTO_ADV_LENGTH_PCODE],
uint8_t prodcode[BLEPROTO_ADV_TLV_LENGTH_PRODCODE]);
/**
* @brief 序列化心跳广播数据(广播数据长度为31)
*
* @param[out] data - 序列化数据缓冲区, 固定为31个字节.
* @param[in] txpower - 蓝牙发射功率[-127,128]dBm
* @param[in] gwmac - 主控设备mac地址(子设备绑定到主控设备,authStep会下发主控设备的mac地址),
* 例如mac地址 "11:22:33:44:55:66"
* gwmac[0] = 0x11
* gwmac[1] = 0x22
* gwmac[2] = 0x33
* gwmac[3] = 0x44
* gwmac[4] = 0x55
* gwmac[5] = 0x66
* @param[in] manucode - 厂商编码, 现在固定填0
* @param[in] pcode - 平台pcode, 比如pcode为AAABBB
* pcode[0] = 'A' = 0x41
* pcode[1] = 'A' = 0x41
* pcode[2] = 'A' = 0x41
* pcode[3] = 'B' = 0x42
* pcode[4] = 'B' = 0x42
* pcode[5] = 'B' = 0x42
* @param[in] prodcode - 产品型号, 比如 宠物喂食器 CBFD,
* prodcode[0] = 'C' = 0x43
* prodcode[1] = 'B' = 0x42
* prodcode[2] = 'F' = 0x46
* prodcode[3] = 'D' = 0x44
*/
void bleproto_advdata_encode4heartbeat(
uint8_t data[31],
int8_t txpower,
uint8_t gwmac[6],
uint8_t manucode[BLEPROTO_ADV_TLV_LENGTH_MANUCODE],
uint8_t pcode[BLE_PROTO_ADV_LENGTH_PCODE],
uint8_t prodcode[BLEPROTO_ADV_TLV_LENGTH_PRODCODE]);
/**
* @brief 序列化回连广播数据(广播数据长度为31)
*
* @param[out] data - 序列化数据缓冲区, 固定为31个字节.
* @param[in] txpower - 蓝牙发射功率[-127,128]dBm
* @param[in] gwmac - 主控设备mac地址(子设备绑定到主控设备,authStep会下发主控设备的mac地址),
* 例如mac地址 "11:22:33:44:55:66"
* gwmac[0] = 0x11
* gwmac[1] = 0x22
* gwmac[2] = 0x33
* gwmac[3] = 0x44
* gwmac[4] = 0x55
* gwmac[5] = 0x66
* @param[in] manucode - 厂商编码, 现在固定填0
* @param[in] pcode - 平台pcode, 比如pcode为AAABBB
* pcode[0] = 'A' = 0x41
* pcode[1] = 'A' = 0x41
* pcode[2] = 'A' = 0x41
* pcode[3] = 'B' = 0x42
* pcode[4] = 'B' = 0x42
* pcode[5] = 'B' = 0x42
* @param[in] prodcode - 产品型号, 比如 宠物喂食器 CBFD,
* prodcode[0] = 'C' = 0x43
* prodcode[1] = 'B' = 0x42
* prodcode[2] = 'F' = 0x46
* prodcode[3] = 'D' = 0x44
*
* @return 小于0表示失败,大于0表示序列化后的长度(31)
*/
uint8_t bleproto_advdata_encode4reconn(
uint8_t data[31],
int8_t txpower,
uint8_t gwmac[6],
uint8_t manucode[BLEPROTO_ADV_TLV_LENGTH_MANUCODE],
uint8_t pcode[BLE_PROTO_ADV_LENGTH_PCODE],
uint8_t prodcode[BLEPROTO_ADV_TLV_LENGTH_PRODCODE]);
/**
* @brief 反序列化广播数据(广播数据长度为31)
*
* @param[in] data - 反序列化数据指针
* @param[in] len - 反序列化数据长度
* @param[out] adv - 广播数据结构体
*
* @return 小于0表示失败,大于0表示反序列化使用data的长度.
*/
int bleproto_advdata_decode(const uint8_t * data,
uint8_t len,
bleproto_adv_t *adv);
/**
* @brief 序列化应用数据结构体到txbuf
*
* @note packetlen说明
*
* ```c
* ble4.0 通常协商的mtu=27, packetlen = 27 - 4 - 3 = 20
* ble4.2 通常协商的mtu=251, packelen = 251 - 4 - 3 = 244
* ble5.0 通常协商的mtu=251, packelen = 251 - 4 - 3 = 244
* ```
*
* 如果报文数据长度超过 BLE GATT单包传输有效长度,需要按照文档方式分包,
* +----------------+
* | 1 | 第一包按照packetlen填满
* +----------------+
* | |
* | ... | 中间包按照packetlen填满
* | |
* +----------------+
* | N | 最后一包长度 = totallen - (packetlen * (N -1))
* +----------------+
*
* 需要按照每包的顺序发给对端
*
* @param[out] txbuf - 发送数据缓冲区指针
* @param[in] size - 发送数据缓冲器长度
* @param[in] packetlen - BLE GATT单包传输有效长度, 查看上面说明
* @param[in] desc - 应用数据结构体描述
*
* @return 小于0表示失败,大于0表示序列化后的长度
*/
int bleproto_appdata_encode(uint8_t * txbuf,
uint16_t size,
uint8_t packetlen,
bleproto_appdata_desc_t *desc);
/**
* @brief 反序列化rxbuf到应用数据结构体
*
*
* 如果报文数据长度超过 BLE GATT单包传输有效长度,需要按照文档方式分包,
* +----------------+
* | 1 | 第一包按照packetlen填满
* +----------------+
* | |
* | ... | 中间包按照packetlen填满
* | |
* +----------------+
* | N | 最后一包长度 = totallen - (packetlen * (N -1))
* +----------------+
*
* 因此接收到数据
* 1. 函数返回等于0,以为还有分包数据还没有接收完成,
* 接收到下包数据 append 在buf尾部,再调用函数的解析数据包
* 2. 函数返回大于0,表示收到数据解析了多少字节,需要将头上对应长度数据移除掉
* 3. 函数返回小于0,表示收到非法的数据包,无法解析
*
* @param[in] rxbuf - 接收缓冲区指针
* @param[in] len - 接收缓冲区数据长度.
* @param[out] desc - 应用数据结构体.
*
* @return 大于0表示解析成功, 返回当前解析了多少个字节,
* 等于0表示解析未完成, 只接受到分包的部分数据, 需要继续接收数据
* 小于0表示解析失败
*/
int bleproto_appdata_decode(uint8_t* rxbuf,
uint16_t len,
bleproto_appdata_desc_t *desc);
/****************************************************************************/
#ifdef __cplusplus
}
#endif
/****************************************************************************/
#endif /* BLEPROTO_API_H_INCLUDE */
/****************************************************************************/
/* */
/* End of file. */
/* */
/****************************************************************************/