Files
JBao_Magic_Box_FW/apps/usr_le_code/bleproto_packer.h
T

703 lines
30 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/****************************************************************************/
/* bleproto_packer.h
*
* Copyright (C) 2020 wanshijie wanshijie@126.com
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
****************************************************************************/
/**
* @addtogroup bleproto_packer packer
* @ingroup bleproto_api
* @brief ble proto packer
*
* @{
*/
#ifndef BLEPROTO_PACKER_H_INCLUDE
#define BLEPROTO_PACKER_H_INCLUDE
/****************************************************************************/
/* Included Files */
/****************************************************************************/
#include "system/includes.h"
#if !defined(BLEPROTO_API_H_INCLUDE)
#error "Only 'bleproto/bleproto_api.h' can be included directly."
#endif /* BLEPROTO_API_H_INCLUDE */
/****************************************************************************/
/* Configure Definitions */
/****************************************************************************/
#define BLEPOROTO_RAM_LARGE 0 ///< 大內存模式
#define BLEPOROTO_RAM_MID 1 ///< 中等內存模式
#define BLEPOROTO_RAM_SMALL 2 ///< 小內存模式
/* default 內存模式 */
#ifndef BLEPOROTO_RAM_MODE
//#define BLEPOROTO_RAM_MODE BLEPOROTO_RAM_LARGE
//#define BLEPOROTO_RAM_MODE BLEPOROTO_RAM_MID
#define BLEPOROTO_RAM_MODE BLEPOROTO_RAM_SMALL
#endif /* BLEPOROTO_RAM_MODE */
/****************************************************************************/
/* Pre-processor Definitions */
/****************************************************************************/
///////////////////////////////////////////////////////////
// trace log define
///////////////////////////////////////////////////////////
#ifndef BLEPROTO_TRACE_ENABLE
#define BLEPROTO_TRACE_ENABLE 0//Jim
#endif /* BLEPROTO_TRACE_ENABLE */
/* trace default printf */
#ifndef BLEPROTO_TRACE_PRINTF
#define BLEPROTO_TRACE_PRINTF printf
#endif /* BLEPROTO_TRACE_PRINTF */
/* trace strfunc */
#if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 19901L
#define BLEPROTO_TRACE_STRFUNC ((const char *)(__func__))
#else
#define BLEPROTO_TRACE_STRFUNC ((const char *)(__FUNCTION__))
#endif /* __STDC_VERSION__ */
/* trace file line */
#define BLEPROTO_TRACE_INTLINE ((int)(__LINE__))
#if BLEPROTO_TRACE_ENABLE
#define BLEPROTO_TRACE(lvl, ...) \
do { \
BLEPROTO_TRACE_PRINTF("[%s][%s:%d]", \
lvl, \
BLEPROTO_TRACE_STRFUNC, \
BLEPROTO_TRACE_INTLINE); \
BLEPROTO_TRACE_PRINTF(__VA_ARGS__); \
BLEPROTO_TRACE_PRINTF("\r\n"); \
} while (0)
#else
#define BLEPROTO_TRACE(lvl, ...) \
do { \
} while (0)
#endif /* FLOG_TRACE_ENABLE */
/* trace log error */
#ifndef BLEPROTO_T_E
#define BLEPROTO_T_E(...) BLEPROTO_TRACE("E", __VA_ARGS__)
#endif /* BLEPROTO_T_E */
/* trace log debug */
#ifndef BLEPROTO_T_D
#define BLEPROTO_T_D(...) BLEPROTO_TRACE("D", __VA_ARGS__)
#endif /* BLEPROTO_T_D */
/* printf log slient */
#ifndef BLEPROTO_T_S
#define BLEPROTO_T_S(...)
#endif /* BLEPROTO_T_S */
// clang-format off
///////////////////////////////////////////////////////////
// ADV
///////////////////////////////////////////////////////////
/** ADV Data Max Size. */
#define BLEPROTO_ADV_MAX_SZ 31
/** ADV Type: Flags. */
#define BLEPROTO_ADV_TYPE_FLAGS 0x01
/** ADV Type: Service Data - 16-bit UUID. */
#define BLEPROTO_ADV_TYPE_SVC_DATA_UUID16 0x16
/** ADV Type: Manufacturer Specific Data. */
#define BLEPROTO_ADV_TYPE_MFG_DATA 0xff
/** ADV service uuid */
#define BLEPROTO_ADV_SERVICE_UUID_TOBIND 0xFDEE
/** ADV service spec data version */
#define BLEPORTO_ADV_SERVICE_SPEC_DATA_VER 0x01
/** ADV service spec data business */
#define BELPROTO_ADV_SERVICE_SPEC_DATA_BUSINESS_NEARFIND 0x01
/* ADV TLV Type */
#define BLEPROTO_ADV_TLV_TYPE_MANUCODE 0x10
#define BLEPROTO_ADV_TLV_TYPE_PRODCODE 0x11
/* ADV TLV Len */
#define BLEPROTO_ADV_TLV_LENGTH_MANUCODE 4
#define BLEPROTO_ADV_TLV_LENGTH_PRODCODE 4
/* ADV PCDE */
#define BLE_PROTO_ADV_LENGTH_PCODE 6
/** ADV flag value */
#define BLEPROTO_ADV_FLAG_HEARTBEAT 0x00
#define BLEPROTO_ADV_FLAG_RECONNECT 0x01
///////////////////////////////////////////////////////////
// APPDATA
///////////////////////////////////////////////////////////
/** APPDATA header len 8bytes */
#define BLEPROTO_APPDATA_HEADR_LEN 8
/** APPDATA version */
#define BLEPROTO_APPDATA_VERSION 0
/** APPDATA data fmt */
#define BLEPROTO_APPDATA_DATAFMT_JSON 0
#define BLEPROTO_APPDATA_DATAFMT_RAW 1
/** APPDATA msg type */
#define BLEPROTO_APPDATA_MSGTYPE_REQ 0
#define BLEPROTO_APPDATA_MSGTYPE_RSP 1
#if (BLEPOROTO_RAM_MODE == BLEPOROTO_RAM_LARGE)
/** APPDATA OTA data max size */
#define BLEPROTO_APPDATA_OTADATA_SZ (1024*16)
/** APPDATA data max size */
#define BLEPROTO_APPDATA_DATA_MAX_SZ (BLEPROTO_APPDATA_OTADATA_SZ + 16 + 16)
/** APP tx buf max size */
#define BLEPROTO_TXRXBUF_MAX_SZ (BLEPROTO_APPDATA_DATA_MAX_SZ + 1024 * 2)
#elif (BLEPOROTO_RAM_MODE == BLEPOROTO_RAM_MID)
/** APPDATA OTA data max size */
#define BLEPROTO_APPDATA_OTADATA_SZ (1024*4)
/** APPDATA data max size */
#define BLEPROTO_APPDATA_DATA_MAX_SZ (BLEPROTO_APPDATA_OTADATA_SZ + 16 + 16)
/** APP tx buf max size */
#define BLEPROTO_TXRXBUF_MAX_SZ (BLEPROTO_APPDATA_DATA_MAX_SZ + 1024 * 2)
#else
/** APPDATA OTA data max size */
#define BLEPROTO_APPDATA_OTADATA_SZ (1024+1024+256)
/** APPDATA data max size */
#define BLEPROTO_APPDATA_DATA_MAX_SZ (BLEPROTO_APPDATA_OTADATA_SZ + 16 + 16)
/** APP tx buf max size */
//#define BLEPROTO_TXRXBUF_MAX_SZ (BLEPROTO_APPDATA_DATA_MAX_SZ + 1024 * 2)
#endif /* BLEPOROTO_RAM_MODE */
// clang-format on
/****************************************************************************/
/* Public Types */
/****************************************************************************/
/**
* @brief 厂商编码-manucode(manufacturer code) TLV desc.
*/
typedef struct bleproto_adv_manucode {
// clang-format off
uint8_t type; ///< 厂商编码T, @ref BLEPROTO_ADV_TLV_TYPE_MANUCODE
uint8_t length; ///< 厂商编码L, @ref BLEPROTO_ADV_TLV_LEN_MANUCODE
uint8_t value[BLEPROTO_ADV_TLV_LENGTH_MANUCODE]; ///< 厂商信息V, 现在固定填写0x00000000
// clang-format off
} bleproto_adv_manucode_t;
/**
* @brief 产品型号-prodcode(product code) TLV desc.
*/
typedef struct bleproto_adv_prodcode {
// clang-format off
uint8_t type; ///< 产品型号T, @ref BLEPROTO_ADV_TLV_TYPE_PRODCODE
uint8_t length; ///< 产品型号L, @ref BLEPROTO_ADV_TLV_LENGTH_PRODCODE
uint8_t value[BLEPROTO_ADV_TLV_LENGTH_PRODCODE]; ///< 产品型号V, 平台定义,需要转换成ASCII码值
// clang-format on
} bleproto_adv_prodcode_t;
/**
* @brief 请求配对广播的Spec Data结构.
*/
typedef struct bleproto_adv_specdata {
// clang-format off
uint8_t version; ///< 蓝牙广播协议版本, 当前1 @ref BLEPORTO_ADV_SERVICE_SPEC_DATA_VER
uint8_t business; ///< 设备发现模式, 固定填写0x01,表示靠近发现 @ref BELPROTO_ADV_SERVICE_SPEC_DATA_BUSINESS_NEARFIND
uint8_t rfu[2]; ///< 保留
int8_t txpower; ///< 蓝牙发射功率[-127,128]dBm
bleproto_adv_manucode_t manucode; ///< 厂商编码
bleproto_adv_prodcode_t prodcode; ///< 产品型号
uint8_t separator; ///< 分隔符, 固定填写0xFF
uint8_t selfdata[6]; ///< 自定义数据
// clang-format on
} bleproto_adv_specdata_t;
/**
* @brief 请求注册广播数据.
*/
typedef struct bleproto_advdata_service {
// clang-format off
uint16_t uuid; ///< 服务uuid16 @ref BLEPROTO_ADV_SERVICE_UUID_TOBIND
bleproto_adv_specdata_t spec_data;///< 协议数据(24bytes)
// clang-format on
} bleproto_advdata_service_t;
/**
* @brief 心跳广播, 回连广播数据.
*/
typedef struct bleproto_advdata_mfg {
// clang-format off
uint8_t siot[6]; ///< 固定填写SIOT-1
uint8_t flag; ///< 0表示心跳,1表示请求回连 @ref BLEPROTO_ADV_FLAG_HEARTBEAT or @ref BLEPROTO_ADV_FLAG_RECONNECT
int8_t txpower; ///< 蓝牙发射功率[-127,128]dBm
uint8_t gwmac[6]; ///< gwmac "11:22:33:44:55:66", gwmac[6] = {0x11, 0x22, 0x33, 0x44, 0x55, 0x66 }
bleproto_adv_manucode_t manucode; ///< 厂商编码
bleproto_adv_prodcode_t prodcode; ///< 产品型号
// clang-format on
} bleproto_advdata_mfg_t;
/**
* @brief AD Structure1结构.
*/
typedef struct bleproto_adv1 {
// clang-format off
/**
* 表示AD Type与AD Data的总长度,
* 此处取值固定为0x02
*/
uint8_t length;
/**
* 表示该广播数据代表的含义
* 此处取值固定为0x01 @ref BLEPROTO_ADV_TYPE_FLAGS
*/
uint8_t ad_type;
/**
* 固定为0x06,表示LE通用可发现模式,且不支持BR/EDR
* 表示蓝牙设备的物理连接能力,
* bit0:LE受限可发现模式。
* bit1:LE通用可发现模式。
* bit2:不支持BR/EDR。
* bit3:对Same Device Capable(控制器)同时支持BLE和BR/EDR。
* bit4:对Same Device Capable(主机)同时支持BLE和BR/EDR。
* bit5~7:预留
*/
uint8_t ad_data;
// clang-format on
} bleproto_adv1_t;
/**
* @brief AD Structure2结构.
*/
typedef struct bleproto_adv2 {
// clang-format off
/**
* 表示AD Type与AD Data的总长度,
* 此处取值固定为0x1B = 27
*/
uint8_t length;
/** 广播数据代表的含义
* BLEPROTO_ADV_TYPE_SVC_DATA_UUID16 - 0x16表示蓝牙服务数据(bleproto_advdata_service_t),
* BLEPROTO_ADV_TYPE_MFG_DATA - 0xFF表示厂商数据(bleproto_advdata_mfg_t)
*/
uint8_t ad_type;
/** adv data */
union {
bleproto_advdata_service_t service_data; ///< 蓝牙服务数据(请求注册广播时携带)
bleproto_advdata_mfg_t mfg_data; ///< 厂商数据(心跳广播,回连广播时携带)
} ad_data;
// clang-format on
} bleproto_adv2_t;
/**
* @brief 广播数据总结构
*/
typedef struct bleproto_adv {
bleproto_adv1_t adv1; ///< ADV Structure1
bleproto_adv2_t adv2; ///< ADV Structure2
} bleproto_adv_t;
/**
* @brief 应用协议报文头(8字节)
*/
typedef struct bleproto_appdata_header {
// clang-format off
uint8_t version : 3; ///< byte0[2:0]:蓝牙BLE应用层协议的版本号,当前为0,根据业务演进递增 @ref BLEPROTO_APPDATA_VERSION
uint8_t datafmt : 1; ///< byte0[3:3]:数据格式, 当前只支持JSON字符串格式,取值固定为0, @ref BLEPROTO_APPDATA_DATAFMT_JSON
uint8_t msgtype : 4; ///< byte0[7:4]:消息类型, 包括:请求(0=req)、响应(1=rsp), @ref BLEPROTO_APPDATA_MSGTYPE_REQ
uint8_t msgid : 4; ///< byte1[3:0]:消息序号, 单调递增
uint8_t encrypt : 1; ///< byte1[4:4]:数据加密方式, 当前固定填写0,表示不加密
uint8_t reserve : 3; ///< byte1[7:5]:保留
uint8_t totalframe; ///< byte2 :总包数, 范围 1~255, 0x01:不分包时,取值为1, 0x02~0xFF:分包时,取值为≥2
uint8_t frameseq; ///< byte3 :分包包序号, 0x00:不分包时取值为0, 0x01~0xFF: 分包时, 取值为从1开始依次递增
uint8_t framelen; ///< byte4 :该包数据长度
uint8_t serviceid; ///< byte5 :应用数据服务id @ref bleproto_service_id_e
uint16_t datalen; ///< byte6-7 :应用数据总长度
// clang-format on
} bleproto_appdata_header_t;
/**
* @brief 应用数据
*/
typedef struct bleproto_appdata {
bleproto_appdata_header_t header;
uint8_t data[BLEPROTO_APPDATA_DATA_MAX_SZ];
} bleproto_appdata_t;
/**
* @brief service id定义
*/
typedef uint8_t bleproto_service_id_e;
enum {
E_BLEPROTO_SERVICE_ID_UNSPECIFIED = 0, ///< 保留
E_BLEPROTO_SERVICE_ID_DEVICEINFO = 1, ///< deviceInfo
E_BLEPROTO_SERVICE_ID_AUTHSETUP = 2, ///< authSetup
E_BLEPROTO_SERVICE_ID_AUTHDELETE = 3, ///< authDelete
E_BLEPROTO_SERVICE_ID_RUNCMD = 4, ///< runCmd
E_BLEPROTO_SERVICE_ID_REPORTCMD = 5, ///< reportCmd
E_BLEPROTO_SERVICE_ID_DOACTION = 6, ///< doAction
E_BLEPROTO_SERVICE_ID_OTANOTIFY = 7, ///< otaNotify
E_BLEPROTO_SERVICE_ID_OTADATA = 8, ///< otaData
E_BLEPROTO_SERVICE_ID_RUNCMD_V2 = 20, ///< runCmdV2
E_BLEPROTO_SERVICE_ID_REPORTCMD_V2 = 21, ///< reportCmdV2
E_BLEPROTO_SERVICE_ID_OTADATA_V2 = 22, ///< otaDataV2
};
/**
* @brief ycmd 定义
*/
#define YCMD_INT32_MAX_COUNT 32
#define YCMD_STRING_MAX_COUNT 16
#define YCMD_STRING_MAX_SIZE 32
#define YCMD_BYTES_MAX_COUNT 8
typedef struct bleproto_ycmd {
int32_t cmd_id;
uint16_t arg_int32_count;
int32_t arg_int32[YCMD_INT32_MAX_COUNT];
uint16_t arg_string_count;
char arg_string[YCMD_STRING_MAX_COUNT][YCMD_STRING_MAX_SIZE];
uint16_t arg_bytes_count;
uint8_t arg_bytes[YCMD_BYTES_MAX_COUNT];
uint64_t t;
} bleproto_ycmd_t;
//////////////////////////////////////////////////////////////////////
// E_BLEPROTO_SERVICE_ID_DEVICEINFO
//////////////////////////////////////////////////////////////////////
typedef struct bleproto_deviceinfo_req {
uint64_t t; ///< utcms时间戳
int gmtoff; ///< 时区偏移,单位秒,例如东八区(8x60x60=28800秒)
} bleproto_deviceinfo_req_t;
typedef struct bleproto_deviceinfo_rsp {
// clang-format off
char hwv[16]; ///< 硬件版本号
char swv[16]; ///< 软件版本号
uint8_t prototype; ///< 协议类型,固定填写为4,表示Ble设备
uint32_t hbcycle; ///< 设备心跳广播心跳周期,ms
uint8_t longcon; ///< 是否长链接, 1=长链接,0=短链接
// clang-format on
} bleproto_deviceinfo_rsp_t;
//////////////////////////////////////////////////////////////////////
// E_BLEPROTO_SERVICE_ID_AUTHSETUP
//////////////////////////////////////////////////////////////////////
typedef struct bleproto_authsetup_req {
// clang-format off
uint8_t gwmac[6]; ///< 主控mac地址, 设备需要保存
char devid[36 + 1]; ///< 主控分配的子设备id, 设备需要保存
char authcode[32 + 1]; ///< 主控分配的子设备认证码, 设备需要保存
uint8_t paired_flag;
// clang-format on
} bleproto_authsetup_req_t;
typedef struct bleproto_authsetup_rsp {
int32_t errcode; ///< 返回码,0表示成功, 其他表示失败
} bleproto_authsetup_rsp_t;
//////////////////////////////////////////////////////////////////////
// E_BLEPROTO_SERVICE_ID_AUTHDELETE
//////////////////////////////////////////////////////////////////////
typedef struct bleproto_authdelete_req {
char authcode[32 + 1]; ///< 子设备认证码
} bleproto_authdelete_req_t;
typedef struct bleproto_authdelete_rsp {
uint8_t gwmac[6]; ///< 绑定主控设备的mac地址
int32_t errcode; ///< 返回码,0表示成功, 其他表示失败
} bleproto_authdelete_rsp_t;
//////////////////////////////////////////////////////////////////////
// E_BLEPROTO_SERVICE_ID_RUNCMD
//////////////////////////////////////////////////////////////////////
typedef struct bleproto_runcmd_req {
bleproto_ycmd_t ycmd; ///< Request Ycmd
} bleproto_runcmd_req_t;
typedef struct bleproto_runcmd_rsp {
bleproto_ycmd_t ycmd; ///< Response Ycmd
} bleproto_runcmd_rsp_t;
//////////////////////////////////////////////////////////////////////
// E_BLEPROTO_SERVICE_ID_REPORTCMD
//////////////////////////////////////////////////////////////////////
typedef struct bleproto_reportcmd_req {
bleproto_ycmd_t ycmd; ///< Ycmd
} bleproto_reportcmd_req_t;
typedef struct bleproto_reportcmd_rsp {
int32_t errcode; ///< 返回码,0表示成功, 其他表示失败
} bleproto_reportcmd_rsp_t;
//////////////////////////////////////////////////////////////////////
// E_BLEPROTO_SERVICE_ID_DOACTION
//////////////////////////////////////////////////////////////////////
typedef struct bleproto_doaction_req {
int action; ///< 0:重启, 1:指示,收到命令设备指示灯闪烁或者音频发音
} bleproto_doaction_req_t;
typedef struct bleproto_doaction_rsp {
int32_t errcode; ///< 返回码,0表示成功, 其他表示失败
} bleproto_doaction_rsp_t;
//////////////////////////////////////////////////////////////////////
// E_BLEPROTO_SERVICE_ID_OTANOTIFY
//////////////////////////////////////////////////////////////////////
typedef struct bleproto_otanotify_req {
int id; ///< file_id
char version[16]; ///< 版本号
uint32_t size; ///< 固件大小
char md5[32 + 1]; ///< 固件的md5值
} bleproto_otanotify_req_t;
typedef struct bleproto_otanotify_rsp {
int32_t errcode; ///< 返回码,0表示成功, 其他表示失败
} bleproto_otanotify_rsp_t;
//////////////////////////////////////////////////////////////////////
// E_BLEPROTO_SERVICE_ID_OTADATA
//////////////////////////////////////////////////////////////////////
typedef struct bleproto_otadata_req {
int id; ///< file_id
int32_t offset; ///< 要读取数据的偏移
uint32_t maxsize; ///< 每次最大读取的大小
} bleproto_otadata_req_t;
typedef struct bleproto_otadata_rsp {
int32_t errcode; ///< 返回码,0表示成功, 其他表示失败
int id; ///< file_id
int32_t offset; ///< 当前读取到的数据偏移
uint32_t len; ///< 当次读取数据的大小
/**
* 固件数据指针(不内嵌大数组,避免 bleproto_appdata_desc_t 膨胀到 2KB+
* - decode:指向 appdata 内部缓冲(零拷贝),下次 decode 前有效
* - encode:由调用方提供可读缓冲
*/
const uint8_t *data;
} bleproto_otadata_rsp_t;
/****************************************************************************/
/* Public Data */
/****************************************************************************/
/****************************************************************************/
#ifdef __cplusplus
extern "C" {
#endif
/****************************************************************************/
/* Public Function Prototypes */
/****************************************************************************/
/**
* @brief 序列化广播数据(广播数据长度为31)
*
* @param[in] adv - 广播数据结构体.
* @param[out] data - 序列化数据缓冲区, 固定为31个字节.
* @param[in] size - 序列化数据缓冲区长度
*
* @return 小于0表示失败,大于0表示序列化后的长度(31)
*/
int bleproto_advdata_serialize(bleproto_adv_t *adv,
uint8_t * data,
uint8_t size);
/**
* @brief 反序列化广播数据(广播数据长度为31)
*
* @param[in] data - 反序列化数据指针
* @param[in] len - 反序列化数据长度
* @param[out] adv - 广播数据结构体
*
* @return 小于0表示失败,大于0表示反序列化使用data的长度(31)
*/
int bleproto_advdata_deserialize(const uint8_t * data,
uint8_t len,
bleproto_adv_t *adv);
/**
* @brief 计算序列化应用数据长度
*
* @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
* ```
*
* @param[in] appdata - 应用数据结构体
* @param[in] packetlen - BLE GATT单包传输有效长度, 查看上面说明
* @param[out] outlen - 计算返回总长度,每个包都含有包头
*
* @return 成功返回0,其他值表示失败
*/
int bleproto_appdata_serialize_len(bleproto_appdata_t *appdata,
uint8_t packetlen,
uint16_t * outlen);
/**
* @brief 序列化应用数据
*
* @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[in] appdata - 应用数据结构体
* @param[in] packetlen - BLE GATT单包传输有效长度, 查看上面说明
* @param[out] buf - 数据缓冲区指针
* @param[in] size - 数据缓冲区大小
*
* @return 小于0表示失败,大于0表示序列化后的长度
*/
int bleproto_appdata_serialize(bleproto_appdata_t *appdata,
uint8_t packetlen,
uint8_t * buf,
uint16_t size);
/**
* @brief 将已打包好的 payload 包装成单帧 appdata(不分包)
* @param txbuf 输出缓冲
* @param txbuf_size 输出缓冲大小
* @param header 应用层头(datalen/totalframe/frameseq 由本函数填写)
* @param payload 已序列化的 service 数据;可为 NULLpayload_len==0
* @param payload_len payload 长度,须 <=255(单帧 pthis_frame_len 为 u8
* @return >0 总字节数;<0 失败
* @note 小包场景(JB 55AA 透传等)用此接口,避免 bleproto_appdata_t 大缓冲
*/
int bleproto_appdata_wrap_raw(uint8_t *txbuf,
uint16_t txbuf_size,
const bleproto_appdata_header_t *header,
const uint8_t *payload,
uint16_t payload_len);
/**
* @brief 反序列化应用数据
*
*
* 如果报文数据长度超过 BLE GATT单包传输有效长度,需要按照文档方式分包,
* +----------------+
* | 1 | 第一包按照packetlen填满
* +----------------+
* | |
* | ... | 中间包按照packetlen填满
* | |
* +----------------+
* | N | 最后一包长度 = totallen - (packetlen * (N -1))
* +----------------+
*
* 因此接收到数据
* 1. 函数返回等于0,以为还有分包数据还没有接收完成,
* 接收到下包数据 append 在buf尾部,再调用函数的解析数据包
* 2. 函数返回大于0,表示收到数据解析了多少字节,需要将头上对应长度数据移除掉
* 3. 函数返回小于0,表示收到非法的数据包,无法解析
*
* @param[in] buf - 数据缓冲区指针.
* @param[in] len - 数据缓冲区长度.
* @param[out] app_data - 应用数据结构体.
*
* @return 大于0表示解析成功, 返回当前解析了多少个字节,
* 等于0表示解析未完成, 只接受到分包的部分数据, 需要继续接收数据
* 小于0表示解析失败
*/
int bleproto_appdata_deserialize(uint8_t * buf,
uint16_t len,
bleproto_appdata_t *appdata);
////////////////////////////////////////////////////////////////////////
// encjson 序列化, 成功返回json序列化的长度,失败返回小于0
// decjson 反序列化, 成功返回0, 其他值表示失败
////////////////////////////////////////////////////////////////////////
// clang-format off
int bleproto_encjson_ycmd(const bleproto_ycmd_t *ycmd, uint8_t data[BLEPROTO_APPDATA_DATA_MAX_SZ]);
int bleproto_decjson_ycmd(const uint8_t *data, uint16_t len, bleproto_ycmd_t *ycmd);
// clang-format on
////////////////////////////////////////////////////////////////////////
// encjson 序列化, 成功返回json序列化的长度,失败返回小于0
// decjson 反序列化, 成功返回0, 其他值表示失败
////////////////////////////////////////////////////////////////////////
// clang-format off
int bleproto_encjson_deviceinfo_req(const bleproto_deviceinfo_req_t *req, uint8_t data[BLEPROTO_APPDATA_DATA_MAX_SZ]);
int bleproto_encjson_deviceinfo_rsp(const bleproto_deviceinfo_rsp_t *rsp, uint8_t data[BLEPROTO_APPDATA_DATA_MAX_SZ]);
int bleproto_encjson_authsetup_req(const bleproto_authsetup_req_t *req, uint8_t data[BLEPROTO_APPDATA_DATA_MAX_SZ]);
int bleproto_encjson_authsetup_rsp(const bleproto_authsetup_rsp_t *rsp, uint8_t data[BLEPROTO_APPDATA_DATA_MAX_SZ]);
int bleproto_encjson_authdelete_req(const bleproto_authdelete_req_t *req, uint8_t data[BLEPROTO_APPDATA_DATA_MAX_SZ]);
int bleproto_encjson_authdelete_rsp(const bleproto_authdelete_rsp_t *rsp, uint8_t data[BLEPROTO_APPDATA_DATA_MAX_SZ]);
int bleproto_encjson_runcmd_req(const bleproto_runcmd_req_t *req, uint8_t data[BLEPROTO_APPDATA_DATA_MAX_SZ]);
int bleproto_encjson_runcmd_rsp(const bleproto_runcmd_rsp_t *rsp, uint8_t data[BLEPROTO_APPDATA_DATA_MAX_SZ]);
int bleproto_encjson_reportcmd_req(const bleproto_reportcmd_req_t *req, uint8_t data[BLEPROTO_APPDATA_DATA_MAX_SZ]);
int bleproto_encjson_reportcmd_rsp(const bleproto_reportcmd_rsp_t *rsp, uint8_t data[BLEPROTO_APPDATA_DATA_MAX_SZ]);
int bleproto_encjson_doaction_req(const bleproto_doaction_req_t *req, uint8_t data[BLEPROTO_APPDATA_DATA_MAX_SZ]);
int bleproto_encjson_doaction_rsp(const bleproto_doaction_rsp_t *rsp, uint8_t data[BLEPROTO_APPDATA_DATA_MAX_SZ]);
int bleproto_encjson_otanotify_req(const bleproto_otanotify_req_t *req, uint8_t data[BLEPROTO_APPDATA_DATA_MAX_SZ]);
int bleproto_encjson_otanotify_rsp(const bleproto_otanotify_rsp_t *rsp, uint8_t data[BLEPROTO_APPDATA_DATA_MAX_SZ]);
// otadata 序列化请求走json,回复走二进制
int bleproto_encjson_otadata_req(const bleproto_otadata_req_t *req, uint8_t data[BLEPROTO_APPDATA_DATA_MAX_SZ]);
int bleproto_encbin_otadata_rsp(const bleproto_otadata_rsp_t *rsp, uint8_t data[BLEPROTO_APPDATA_DATA_MAX_SZ]);
int bleproto_decjson_deviceinfo_req(const uint8_t *data, uint16_t len, bleproto_deviceinfo_req_t *req);
int bleproto_decjson_deviceinfo_rsp(const uint8_t *data, uint16_t len, bleproto_deviceinfo_rsp_t *rsp);
int bleproto_decjson_authsetup_req(const uint8_t *data, uint16_t len, bleproto_authsetup_req_t *req);
int bleproto_decjson_authsetup_rsp(const uint8_t *data, uint16_t len, bleproto_authsetup_rsp_t *rsp);
int bleproto_decjson_authdelete_req(const uint8_t *data, uint16_t len, bleproto_authdelete_req_t *req);
int bleproto_decjson_authdelete_rsp(const uint8_t *data, uint16_t len, bleproto_authdelete_rsp_t *rsp);
int bleproto_decjson_runcmd_req(const uint8_t *data, uint16_t len, bleproto_runcmd_req_t *req);
int bleproto_decjson_runcmd_rsp(const uint8_t *data, uint16_t len, bleproto_runcmd_rsp_t *rsp);
int bleproto_decjson_reportcmd_req(const uint8_t *data, uint16_t len, bleproto_reportcmd_req_t *req);
int bleproto_decjson_reportcmd_rsp(const uint8_t *data, uint16_t len, bleproto_reportcmd_rsp_t *rsp);
int bleproto_decjson_doaction_req(const uint8_t *data, uint16_t len, bleproto_doaction_req_t *req);
int bleproto_decjson_doaction_rsp(const uint8_t *data, uint16_t len, bleproto_doaction_rsp_t *rsp);
int bleproto_decjson_otanotify_req(const uint8_t *data, uint16_t len, bleproto_otanotify_req_t *req);
int bleproto_decjson_otanotify_rsp(const uint8_t *data, uint16_t len, bleproto_otanotify_rsp_t *rsp);
//otadata 反序列化请求走json,回复走二进制
int bleproto_decjson_otadata_req(const uint8_t *data, uint16_t len, bleproto_otadata_req_t *req);
int bleproto_debin_otadata_rsp(const uint8_t *data, uint16_t len, bleproto_otadata_rsp_t *rsp);
// clang-format on
/****************************************************************************/
#ifdef __cplusplus
}
#endif
/****************************************************************************/
#endif /* BLEPROTO_PACKER_H_INCLUDE */
/****************************************************************************/
/**
* @} (end addtogroup bleproto_packer)
*/
/****************************************************************************/
/* */
/* End of file. */
/* */
/****************************************************************************/