Files
JBao_Sprayer_FW/apps/usr_jb_proto/Protocol/jb_protocol.h
T
2026-09-09 10:29:41 +08:00

309 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 数: 28 | Bitmap: 4B
* 协议版本: V1.0 | 生成时间: 2026-09-09 10:22:58
*
* 帧格式: [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 28
#define DP_BITMAP_SIZE 4
#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: 取自 Excel 设备信息表的 ProdtCode / productCode。
* JB_CATEGORY_CODE: 必须 4 字符 ASCII,供 BLE 广播 prodcode 使用
* JB_PRODUCT_CODE : 必须 6 字符 ASCII,供 BLE 广播 pcode 使用
* 模板未填时回退占位 XXXX / XXXXXX(仍需用户修改)。
*/
#define JB_PROTOCOL_VERSION 0x0100 /* V1.0 */
#define JB_CATEGORY_CODE "CBR2"
#define JB_PRODUCT_CODE "PRCBR2"
#define JB_DEVICE_SN 0x00
#define JB_DEVICE_CAPABILITY 0ULL
/* ════════════════════════════════════════════════════════════════
* 命令码(见《SOC方案接入协议通讯文档》)
* ════════════════════════════════════════════════════════════════ */
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_CONNECTING (1 << 0) /* 配网中 */
#define WS_CONFIGURED (1 << 1) /* 已配网 */
#define WS_RECONNECTING (1 << 2) /* 断线重连中 */
#define WS_CLOUD_CONNECTED (1 << 3) /* 已连接到云 */
#define WS_APP_ONLINE (1 << 4) /* App 在线 */
#define WS_APP_DIRECT (1 << 5) /* App 直连 */
#define WS_OTA_UPGRADING (1 << 6) /* 模组 OTA 升级中 */
#define WS_RESERVED (1 << 7) /* 保留 */
/* ════════════════════════════════════════════════════════════════
* 设备能力位图 (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) /* 日志功能 */
/* ════════════════════════════════════════════════════════════════
* DP ID 枚举(从 Excel 自动生成)
* ════════════════════════════════════════════════════════════════ */
typedef enum
{
DP_ID_POWER = 0, /* 总开关 (bool, RW) */
DP_ID_SPRAY_SWITCH = 1, /* 喷淋开关 (bool, RW) */
DP_ID_MODE = 2, /* 模式 (enum, RW) */
DP_ID_ONCE_SPRAY_TIME = 3, /* 单次喷淋时长 (uint8, RW) */
DP_ID_CONTINUOUS_SPRAY_TIME = 4, /* 连续喷淋时长 (uint8, RW) */
DP_ID_CURRENT_TEMP = 5, /* 当前温度 (float, WO) */
DP_ID_CURRENT_HUMI = 6, /* 当前湿度 (uint8, WO) */
DP_ID_SPRAY_CONFIG = 7, /* 喷淋配置 (raw, RW) */
DP_ID_LINKAGE_DELETE = 8, /* 联动删除 (bool, RW) */
DP_ID_LINKAGE_CONFIG = 9, /* 联动配置 (raw, RW) */
DP_ID_SPRAY_PLAN_EN = 10, /* 喷淋计划总开关 (bool, RW) */
DP_ID_SPRAY_PLAN1 = 11, /* 喷淋计划1 (raw, RW) */
DP_ID_TIME_CONFIG2 = 12, /* 喷淋计划2 (raw, RW) */
DP_ID_TIME_CONFIG3 = 13, /* 喷淋计划3 (raw, RW) */
DP_ID_TIME_CONFIG4 = 14, /* 喷淋计划4 (raw, RW) */
DP_ID_TIME_CONFIG5 = 15, /* 喷淋计划5 (raw, RW) */
DP_ID_TIME_CONFIG6 = 16, /* 喷淋计划6 (raw, RW) */
DP_ID_TIME_CONFIG7 = 17, /* 喷淋计划7 (raw, RW) */
DP_ID_RUN_RECORD = 18, /* 运行记录 (raw, RO) */
DP_ID_HEARTBEAT = 19, /* 心跳 (bool, WO) */
DP_ID_FAULT_EN = 20, /* 设备故障使能 (uint8, RW) */
DP_ID_FAULT_CODE = 21, /* 设备故障 (uint8, RO) */
DP_ID_ALARM_EN = 22, /* 设备告警使能 (uint8, RW) */
DP_ID_ALARM_CODE = 23, /* 设备告警 (uint8, RO) */
DP_ID_FACTORY_RESET = 24, /* 恢复出厂设置 (bool, WO) */
DP_ID_RESERVA_DP1 = 25, /* 预留DP1 (uint32, RW) */
DP_ID_RESERVA_DP2 = 26, /* 预留DP2 (uint32, RW) */
DP_ID_RESERVA_DP3 = 27, /* 预留DP3 (uint32, RW) */
DP_ID_MAX
} dp_id_t;
/* DP 数据长度(字节) */
#define DP_LEN_POWER 1
#define DP_LEN_SPRAY_SWITCH 1
#define DP_LEN_MODE 1
#define DP_LEN_ONCE_SPRAY_TIME 1
#define DP_LEN_CONTINUOUS_SPRAY_TIME 1
#define DP_LEN_CURRENT_TEMP 2
#define DP_LEN_CURRENT_HUMI 1
#define DP_LEN_SPRAY_CONFIG 8
#define DP_LEN_LINKAGE_DELETE 1
#define DP_LEN_LINKAGE_CONFIG 20
#define DP_LEN_SPRAY_PLAN_EN 1
#define DP_LEN_SPRAY_PLAN1 15
#define DP_LEN_TIME_CONFIG2 15
#define DP_LEN_TIME_CONFIG3 15
#define DP_LEN_TIME_CONFIG4 15
#define DP_LEN_TIME_CONFIG5 15
#define DP_LEN_TIME_CONFIG6 15
#define DP_LEN_TIME_CONFIG7 15
#define DP_LEN_RUN_RECORD 124
#define DP_LEN_HEARTBEAT 1
#define DP_LEN_FAULT_EN 1
#define DP_LEN_FAULT_CODE 1
#define DP_LEN_ALARM_EN 1
#define DP_LEN_ALARM_CODE 1
#define DP_LEN_FACTORY_RESET 1
#define DP_LEN_RESERVA_DP1 4
#define DP_LEN_RESERVA_DP2 4
#define DP_LEN_RESERVA_DP3 4
/* ════════════════════════════════════════════════════════════════
* DP 枚举值定义
* ════════════════════════════════════════════════════════════════ */
/* 模式 */
typedef enum
{
MODE_0 = 0, /* 关闭模式 */
MODE_1 = 1, /* 单次喷淋 */
MODE_2 = 2, /* 连续喷淋 */
} MODE_t;
/* ════════════════════════════════════════════════════════════════
* 数据结构
* ════════════════════════════════════════════════════════════════ */
/* 数据点 — 所有 DP 的当前值 */
typedef struct
{
uint8_t power; /* DP0 总开关 */
uint8_t spray_switch; /* DP1 喷淋开关 */
uint8_t mode; /* DP2 模式 */
uint8_t once_spray_time;/* DP3 单次喷淋时长 */
uint8_t continuous_spray_time;/* DP4 连续喷淋时长 */
float current_temp; /* DP5 当前温度 (float, scale=0.1) */
uint8_t current_humi; /* DP6 当前湿度 */
uint8_t spray_config[8];/* DP7 喷淋配置 */
uint8_t linkage_delete;/* DP8 联动删除 */
uint8_t linkage_config[20];/* DP9 联动配置 */
uint8_t spray_plan_en;/* DP10 喷淋计划总开关 */
uint8_t spray_plan1[15];/* DP11 喷淋计划1 */
uint8_t time_config2[15];/* DP12 喷淋计划2 */
uint8_t time_config3[15];/* DP13 喷淋计划3 */
uint8_t time_config4[15];/* DP14 喷淋计划4 */
uint8_t time_config5[15];/* DP15 喷淋计划5 */
uint8_t time_config6[15];/* DP16 喷淋计划6 */
uint8_t time_config7[15];/* DP17 喷淋计划7 */
uint8_t run_record[124];/* DP18 运行记录 */
uint8_t heartbeat; /* DP19 心跳 */
uint8_t fault_en; /* DP20 设备故障使能 */
uint8_t fault_code; /* DP21 设备故障 */
uint8_t alarm_en; /* DP22 设备告警使能 */
uint8_t alarm_code; /* DP23 设备告警 */
uint8_t factory_reset;/* DP24 恢复出厂设置 */
uint32_t reserva_dp1; /* DP25 预留DP1 */
uint32_t reserva_dp2; /* DP26 预留DP2 */
uint32_t reserva_dp3; /* DP27 预留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 事件回调 */
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 jbOnAck(uint8_t cmd, uint8_t result); /* 任意请求 ACK(结果) */
#endif /* _JB_PROTOCOL_H */