Files
soc-codegen-template/apps/usr_jb_proto/Protocol/jb_protocol.h
T
2026-08-19 16:05:36 +08:00

267 lines
15 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.
/* ================================================================
* jb_protocol.h — 见宝 IOT 协议层(自动生成)
* 设备: 侠客猫激光 | DP 数: 11 | Bitmap: 2B
* 协议版本: V1.0 | 生成时间: 2026-07-31 17:08:51
*
* 帧格式: [55 AA] [LEN_H LEN_L] [CMD] [SN] [payload...] [CKSUM]
* LEN = CMD(1) + SN(1) + payload + CKSUM(1) = payload + 3
* CKSUM = sum(LEN_H .. payload_last) & 0xFF
* Bitmap = 每个 DP 占 1 bitbit 序号 = DP ID0 起始)
* Values = 按 bitmap 置位顺序排列,每个 DP 占完整 byte_length
* ================================================================
*/
#ifndef _JB_PROTOCOL_H
#define _JB_PROTOCOL_H
#include <stdint.h>
#include <stddef.h>
#include <string.h>
/* ════════════════════════════════════════════════════════════════
* 协议常量
* ════════════════════════════════════════════════════════════════ */
#define PKT_HEAD_0 0x55
#define PKT_HEAD_1 0xAA
#define MAX_PACKAGE_LEN 128
#define JB_OTA_MAX_PAYLOAD 96 /* OTA 单包 Payload 上限(Byte); 须 <= MAX_PACKAGE_LEN - 11(0x32 帧开销) */
#define DP_COUNT 11
#define DP_BITMAP_SIZE 2
#define SEND_MAX_TIME 2500 /* ACK 重传超时(ms)BLE Indication往返常需数百ms~1s,过短会误重传导致双应答 */
#define SEND_MAX_NUM 3 /* 最大重传次数 */
#define REPORT_DEBOUNCE 2000 /* 变化上报防抖时间(ms) */
#define REPORT_PERIOD 600000 /* 定时全量上报周期(ms) */
/* Device identity: change these values for the target product.
* JB_CATEGORY_CODE: 必须 4 字符 ASCII,供 BLE 广播 prodcode 使用
* JB_PRODUCT_CODE : 必须 6 字符 ASCII,供 BLE 广播 pcode 使用
*/
#define JB_PROTOCOL_VERSION 0x0100 /* V1.0 */
#define JB_CATEGORY_CODE "CBC0"
#define JB_PRODUCT_CODE "PICBC0"
#define JB_DEVICE_SN 0x00
#define JB_DEVICE_CAPABILITY 0ULL
/* ════════════════════════════════════════════════════════════════
* 命令码(见《MCU方案接入协议通讯文档》)
* ════════════════════════════════════════════════════════════════ */
typedef enum
{
/* ── APP-BLE 数据(6) ── */
CMD_CONTROL = 0x03, /* APP/BLE → MCU: 下发控制 */
CMD_CONTROL_ACK = 0x04, /* MCU → APP/BLE: 控制应答 */
CMD_READ = 0x05, /* APP/BLE → MCU: 读取请求 */
CMD_READ_ACK = 0x06, /* MCU → APP/BLE: 读取应答 */
CMD_REPORT = 0x07, /* MCU → APP/BLE: 主动上报 */
CMD_REPORT_ACK = 0x08, /* APP/BLE → MCU: 上报应答 */
/* ── OTA(10) ── */
CMD_OTA_NOTIFY = 0x30, /* BLE → MCU: OTA 升级请求 */
CMD_OTA_NOTIFY_ACK = 0x31, /* MCU → BLE: OTA 应答(含MaxLen) */
CMD_OTA_DATA = 0x32, /* BLE → MCU: OTA 数据 */
CMD_OTA_DATA_ACK = 0x33, /* MCU → BLE: OTA 数据应答 */
CMD_OTA_COMPLETE = 0x34, /* MCU → BLE: OTA 完成 */
CMD_OTA_COMPLETE_ACK = 0x35, /* BLE → MCU: OTA 完成应答 */
CMD_OTA_STOP_BLE = 0x36, /* BLE → MCU: 停止 OTA */
CMD_OTA_STOP_BLE_ACK = 0x37, /* MCU → BLE: 停止应答 */
CMD_OTA_STOP_MCU = 0x38, /* MCU → BLE: 通知停止 OTA */
CMD_OTA_STOP_MCU_ACK = 0x39, /* BLE → MCU: 停止应答 */
} jb_cmd_t;
/* ════════════════════════════════════════════════════════════════
* 结果码
* ════════════════════════════════════════════════════════════════ */
typedef enum
{
JB_OK = 0x00,
JB_PARAM_ERR = 0x01,
JB_BUSY = 0x02,
JB_NOT_SUPPORT = 0x03,
JB_CRC_ERR = 0x04,
JB_FAIL = 0x05,
/* ── OTA 专用错误码 ── */
JB_OTA_NO_NEED = 0x20, /* 无需升级(版本一致) */
JB_OTA_VER_LOW = 0x21, /* OTA 版本低于当前版本 */
JB_OTA_SPACE_ERR = 0x22, /* Flash 空间不足 */
JB_OTA_STATE_ERR = 0x23, /* OTA 状态错误 */
JB_OTA_FILE_ERR = 0x24, /* OTA 文件检验失败 */
JB_OTA_FAIL = 0x25, /* 升级失败 */
} jb_result_t;
/* ════════════════════════════════════════════════════════════════
* BLE 工作状态位 (0x14)
* ════════════════════════════════════════════════════════════════ */
#define WS_CONFIGURED (1 << 0) /* 已配网 */
#define WS_INTERNET (1 << 1) /* 已连外网 */
#define WS_APP_ONLINE (1 << 2) /* App 在线 */
#define WS_BLE_CONNECTED (1 << 3) /* 手机 BLE 已连接 */
#define WS_OTA_UPGRADING (1 << 4) /* OTA 升级中 */
#define WS_PAIRING_MODE (1 << 5) /* 配对模式激活 */
/* ════════════════════════════════════════════════════════════════
* 设备能力位图 (0x02 Capability Bitmap, 8B 大端)
* ════════════════════════════════════════════════════════════════ */
#define CAP_BLE_OTA (1ULL << 0) /* BLE OTA */
#define CAP_MCU_OTA (1ULL << 1) /* MCU OTA */
#define CAP_APP_DIRECT (1ULL << 2) /* APP 直连 BLE */
#define CAP_IPC_GATEWAY (1ULL << 3) /* IPC 网关连接 */
#define CAP_PRODUCTION (1ULL << 4) /* 产测模式 */
#define CAP_PARAM_CFG (1ULL << 5) /* 参数配置 */
#define CAP_LOG (1ULL << 6) /* 日志功能 */
/* ════════════════════════════════════════════════════════════════
* 身份信息 (0x02) / MCU 信息 (0x41)
* ════════════════════════════════════════════════════════════════ */
/* ════════════════════════════════════════════════════════════════
* DP ID 枚举(从 Excel 自动生成)
* ════════════════════════════════════════════════════════════════ */
typedef enum
{
DP_ID_POWER = 0, /* 开关 (bool, RW) */
DP_ID_PLAY_MODE = 1, /* 自动模式 (enum, RW) */
DP_ID_HAND_MODE = 2, /* 手动模式 (enum, WO) */
DP_ID_ALM_LOW_BATTERY = 3, /* 低电量报警 (bool, RO) */
DP_ID_BEEP_TRIG = 4, /* 声音开关 (bool, RW) */
DP_ID_BATTERY = 5, /* 电量上报 (uint8, RO) */
DP_ID_SHAKE_WAKE = 6, /* 震动触发开机 (bool, RW) */
DP_ID_NO_DISTURB_ENABLE = 7, /* 勿扰模式开关 (raw, RW) */
DP_ID_RESERVA_DP1 = 8, /* 预留DP1 (uint32, RW) */
DP_ID_RESERVA_DP2 = 9, /* 预留DP2 (uint32, RW) */
DP_ID_RESERVA_DP3 = 10, /* 预留DP3 (uint32, RW) */
DP_ID_MAX
} dp_id_t;
/* DP 数据长度(字节) */
#define DP_LEN_POWER 1
#define DP_LEN_PLAY_MODE 1
#define DP_LEN_HAND_MODE 1
#define DP_LEN_ALM_LOW_BATTERY 1
#define DP_LEN_BEEP_TRIG 1
#define DP_LEN_BATTERY 1
#define DP_LEN_SHAKE_WAKE 1
#define DP_LEN_NO_DISTURB_ENABLE 13
#define DP_LEN_RESERVA_DP1 4
#define DP_LEN_RESERVA_DP2 4
#define DP_LEN_RESERVA_DP3 4
/* ════════════════════════════════════════════════════════════════
* DP 枚举值定义
* ════════════════════════════════════════════════════════════════ */
/* 自动模式 */
typedef enum
{
PLAY_MODE_0 = 0, /* 关 */
PLAY_MODE_1 = 1, /* mode1 */
PLAY_MODE_2 = 2, /* mode2 */
PLAY_MODE_3 = 3, /* mode3 */
PLAY_MODE_4 = 4, /* mode4 */
} PLAY_MODE_t;
/* 手动模式 */
typedef enum
{
HAND_MODE_0 = 0, /* 关 */
HAND_MODE_1 = 1, /* 顺时针 */
HAND_MODE_2 = 2, /* 逆时针 */
} HAND_MODE_t;
/* ════════════════════════════════════════════════════════════════
* 数据结构
* ════════════════════════════════════════════════════════════════ */
/* 数据点 — 所有 DP 的当前值 */
typedef struct
{
uint8_t power; /* DP0 开关 */
uint8_t play_mode; /* DP1 自动模式 */
uint8_t hand_mode; /* DP2 手动模式 */
uint8_t alm_low_battery;/* DP3 低电量报警 */
uint8_t beep_trig; /* DP4 声音开关 */
uint8_t battery; /* DP5 电量上报 */
uint8_t shake_wake; /* DP6 震动触发开机 */
uint8_t no_disturb_enable[13];/* DP7 勿扰模式开关 */
uint32_t reserva_dp1; /* DP8 预留DP1 */
uint32_t reserva_dp2; /* DP9 预留DP2 */
uint32_t reserva_dp3; /* DP10 预留DP3 */
} jb_data_point_t;
/* 控制事件信息 */
typedef struct
{
uint8_t count;
uint8_t dp_ids[DP_ID_MAX];
} jb_event_info_t;
/* ACK 等待 / 重传管理器 */
typedef struct
{
uint8_t active; /* 是否有等待中的请求 */
uint8_t sn; /* 帧序列号 */
uint8_t retry;
uint8_t buf[MAX_PACKAGE_LEN];
uint16_t len;
uint32_t sendTime;
} jb_wait_ack_t;
/* 协议全局状态 */
typedef struct
{
uint8_t sn; /* 设备 SN 计数器 */
uint8_t rxBuf[MAX_PACKAGE_LEN];
jb_wait_ack_t waitAck; /* ACK 重传管理器 */
jb_data_point_t lastData; /* 上次上报快照 */
uint32_t lastReportTime; /* 上次上报时刻 */
uint32_t lastChangeTime; /* 上次变化上报时刻 */
} jb_protocol_t;
/* ════════════════════════════════════════════════════════════════
* 全局实例
* ════════════════════════════════════════════════════════════════ */
extern jb_protocol_t jbProtocol;
/* ════════════════════════════════════════════════════════════════
* 协议层 API(自动生成)
* ════════════════════════════════════════════════════════════════ */
void jbInit(void);
int32_t jbPutData(uint8_t *buf, uint32_t len);
int32_t jbNotifyOtaStop(void);
int32_t jbSendOtaResult(uint8_t result, const uint8_t *mcuVersion);
int32_t jbProtocolHandle(jb_data_point_t *data);
/*
* 网关应答确认(主动上报 / OTA 共用,REPORTCMD_V2,非 55AA
* cmd: ble_proto 包 cmd
* gateway_int_cnt: gateway int 个数
* gateway_int0: gateway_int[0]
* 成功条件: cmd==0 && gateway_int_cnt==1 && gateway_int0==0
* 返回: 1=成功并清除等待; 0=未命中/条件不符
*/
int8_t jbReportAckCheck(uint16_t cmd, uint8_t gateway_int_cnt, int32_t gateway_int0);
/* ════════════════════════════════════════════════════════════════
* 硬件抽象(用户实现)
* ════════════════════════════════════════════════════════════════ */
uint32_t jbGetTimerCount(void); /* ms 计数器(定时器由 SOC SDK 提供) */
/* 发送接口:经 SOC SDK 提供的 BLE 发送封装(由用户/SDK 实现,非本生成代码范围)*/
extern void ble_send_data(uint8_t *data, uint16_t len);
/* ════════════════════════════════════════════════════════════════
* 用户回调(用户实现)
* ════════════════════════════════════════════════════════════════ */
/* 0x03 APP 下发控制事件 */
int8_t jbEventProcess(jb_event_info_t *info, jb_data_point_t *data);
/* BLE → MCU 事件回调 */
void jbOnOtaNotify(uint32_t fwVersion, uint32_t fwLength, uint32_t fwCrc32); /* 0x30 */
void jbOnOtaData(const uint8_t *data, uint16_t len,uint16_t shardIdx, uint16_t totalShards); /* 0x32 */
void jbOnOtaStop(void); /* 0x36 / 0x38 停止 */
/* MCU → BLE 应答回调(收到 BLE 应答时触发)*/
void jbOnAck(uint8_t cmd, uint8_t result); /* 任意请求 ACK(结果) */
#endif /* _JB_PROTOCOL_H */