Files
mcu-codegen-template/App/Protocol/jb_protocol.h
T

305 lines
17 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-28 15:42:40
*
* 帧格式: [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 2000 /* ACK 重传超时(ms) */
#define SEND_MAX_NUM 3 /* 最大重传次数 */
#define REPORT_DEBOUNCE 2000 /* 变化上报防抖时间(ms) */
#define REPORT_PERIOD 600000 /* 定时全量上报周期(ms) */
/* Device identity: change these values for the target product. */
#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
{
/* ── 设备身份信息(2) ── */
CMD_IDENTITY_REQ = 0x01, /* BLE → MCU: 请求身份信息 */
CMD_IDENTITY_ACK = 0x02, /* MCU → BLE: 身份应答 */
/* ── 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: 上报应答 */
/* ── 配对 / 心跳 / 状态(6) ── */
CMD_PAIRING_REQ = 0x10, /* MCU → BLE: 请求配对 */
CMD_PAIRING_ACK = 0x11, /* BLE → MCU: 配对应答 */
CMD_HEARTBEAT = 0x12, /* BLE → MCU: 心跳 */
CMD_HEARTBEAT_ACK = 0x13, /* MCU → BLE: 心跳应答 */
CMD_WORK_STATE = 0x14, /* BLE → MCU: 状态同步(含RSSI) */
CMD_WORK_STATE_ACK= 0x15, /* MCU → BLE: 状态应答 */
/* ── BLE 模组控制(4) ── */
CMD_RESET_BLE = 0x16, /* MCU → BLE: 重置 BLE(恢复出厂) */
CMD_RESET_BLE_ACK = 0x17, /* BLE → MCU: 重置应答 */
CMD_RESTART_BLE = 0x18, /* MCU → BLE: 重启 BLE */
CMD_RESTART_BLE_ACK=0x19, /* BLE → MCU: 重启应答 */
CMD_RESTART_MCU = 0x1A, /* BLE → MCU: 通知 MCU 重启 */
CMD_RESTART_MCU_ACK=0x1B, /* MCU → BLE: 重启应答 */
/* ── 时间 / 产测(4) ── */
CMD_TIME_REQ = 0x1C, /* MCU → BLE: 获取网络时间 */
CMD_TIME_ACK = 0x1D, /* BLE → MCU: 时间应答 */
CMD_PRODUCTION = 0x20, /* MCU → BLE: 产测模式 */
CMD_PRODUCTION_ACK= 0x21, /* BLE → MCU: 产测应答 */
/* ── 拍照 / 录像 / 音频(6) ── */
CMD_PHOTO_REQ = 0x22, /* MCU → BLE: 开启拍照 */
CMD_PHOTO_ACK = 0x23, /* BLE → MCU: 拍照应答 */
CMD_RECORD_REQ = 0x24, /* MCU → BLE: 开启录像 */
CMD_RECORD_ACK = 0x25, /* BLE → MCU: 录像应答 */
CMD_AUDIO_REQ = 0x26, /* MCU → BLE: 启动音频 */
CMD_AUDIO_ACK = 0x27, /* 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: 停止应答 */
/* ── MCU 信息(2) ── */
CMD_MCU_INFO_REQ = 0x40, /* BLE → MCU: 请求 MCU 信息 */
CMD_MCU_INFO_ACK = 0x41, /* MCU → 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);
/* MCU → BLE 主动请求 / 通知 */
int32_t jbRequestPairing(uint8_t cfg_mode, uint16_t timeout_sec,
const uint8_t *param, uint8_t param_len);
int32_t jbRequestTime(void);
int32_t jbRequestBleReset(void);
int32_t jbRequestBleRestart(void);
int32_t jbRequestProduction(uint8_t mode);
int32_t jbRequestPhoto(void);
int32_t jbRequestRecord(uint16_t duration_sec);
int32_t jbRequestAudio(uint8_t audio_id);
int32_t jbNotifyOtaStop(void);
int32_t jbSendOtaResult(uint8_t result, const uint8_t *mcuVersion);
int32_t jbProtocolHandle(jb_data_point_t *data);
/* ════════════════════════════════════════════════════════════════
* 硬件抽象(用户实现)
* ════════════════════════════════════════════════════════════════ */
uint32_t jbGetTimerCount(void); /* ms 计数器 */
int32_t uartWrite(uint8_t *buf, uint32_t len); /* UART 发送 */
/* ════════════════════════════════════════════════════════════════
* 用户回调(用户实现)
* ════════════════════════════════════════════════════════════════ */
/* 0x03 APP 下发控制事件 */
int8_t jbEventProcess(jb_event_info_t *info, jb_data_point_t *data);
/* BLE → MCU 事件回调 */
void jbOnWorkState(uint8_t state, int8_t rssi); /* 0x14 状态(含RSSI) */
void jbOnBleRestartRequest(void); /* 0x1A 请求重启 */
int8_t jbOnOtaNotify(uint32_t fwVersion, uint32_t fwLength, uint32_t fwCrc32); /* 0x30, 返回0=接受,非0=拒绝码 */
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 jbOnTimeSync(uint32_t timestamp); /* 0x1D 时间同步 */
void jbOnAck(uint8_t cmd, uint8_t result); /* 任意请求 ACK(结果) */
#endif /* _JB_PROTOCOL_H */