diff --git a/apps/usr_jb_proto/Protocol/jb_protocol.h b/apps/usr_jb_proto/Protocol/jb_protocol.h index c3c70af..c959eb5 100644 --- a/apps/usr_jb_proto/Protocol/jb_protocol.h +++ b/apps/usr_jb_proto/Protocol/jb_protocol.h @@ -26,7 +26,7 @@ /* ⚠ 生成器给的是 128,必须改回 256:DP15 run_record 124B,加 bitmap(4B) 与 * cmd/sn/cksum 开销后 128B 组不出完整帧(规格书第 0 章第 1 条 / 第 19 章)。 * 每次从生成器重新拷贝 jb_protocol.h 后都要重新确认本值。 */ -#define MAX_PACKAGE_LEN 256 +#define MAX_PACKAGE_LEN 512 #define JB_OTA_MAX_PAYLOAD 96 /* OTA 单包 Payload 上限(Byte); 须 <= MAX_PACKAGE_LEN - 11(0x32 帧开销) */ #define DP_COUNT 25 diff --git a/apps/usr_le_code/bleproto_packer.c b/apps/usr_le_code/bleproto_packer.c index b1c6159..7f08bb2 100644 --- a/apps/usr_le_code/bleproto_packer.c +++ b/apps/usr_le_code/bleproto_packer.c @@ -673,51 +673,89 @@ int bleproto_appdata_serialize(bleproto_appdata_t *appdata, int bleproto_appdata_wrap_raw(uint8_t *txbuf, uint16_t txbuf_size, + uint8_t packetlen, const bleproto_appdata_header_t *header, const uint8_t *payload, uint16_t payload_len) { - uint16_t total; + uint8_t frame_max; + uint16_t totalframe; + uint16_t need; + uint16_t offset = 0; + uint16_t data_offset = 0; + uint16_t i; - if (!txbuf || !header) { - BLEPROTO_T_E("Invalid parameters"); + if (!txbuf || !header || packetlen <= BLEPROTO_APPDATA_HEADR_LEN) { + printf("Invalid parameters"); return -1; } if (payload_len > 0 && !payload) { - BLEPROTO_T_E("Invalid payload"); - return -1; - } - /* 单帧 this_frame_len 为 uint8,超过 255 需走分包 serialize */ - if (payload_len > 255u) { - BLEPROTO_T_E("payload too long for single frame"); + printf("Invalid payload"); return -1; } - total = (uint16_t)(BLEPROTO_APPDATA_HEADR_LEN + payload_len); - if (txbuf_size < total) { - BLEPROTO_T_E("txbuf too short"); + /* 与 serialize 相同:一帧总长 packetlen,其中 8B 头,其余为 payload */ + frame_max = (uint8_t)(packetlen - BLEPROTO_APPDATA_HEADR_LEN); + + if (payload_len == 0) { + totalframe = 1; + } else { + totalframe = (uint16_t)((payload_len + frame_max - 1u) / frame_max); + } + if (totalframe > 255u) { + printf("payload too long, frames %u>255\n", (unsigned)totalframe); return -1; } - /* buf[0]: version[2:0] | datafmt[3:3] | msgtype[7:4] */ - txbuf[0] = UTILS_BITFIELD_SET(header->version, 2, 0) | - UTILS_BITFIELD_SET(header->datafmt, 3, 3) | - UTILS_BITFIELD_SET(header->msgtype, 7, 4); - /* buf[1]: msgid[3:0] | encrypt[4:4] | reserve[7:5] */ - txbuf[1] = UTILS_BITFIELD_SET(header->msgid, 3, 0) | - UTILS_BITFIELD_SET(header->encrypt, 4, 4) | - UTILS_BITFIELD_SET(header->reserve, 7, 5); - txbuf[2] = 1; /* totalframe */ - txbuf[3] = 0; /* frameseq:单帧为 0 */ - txbuf[4] = (uint8_t)payload_len; /* this frame payload len */ - txbuf[5] = header->serviceid; - proto_bytes_put_le16(&txbuf[6], payload_len); - - if (payload_len > 0) { - memcpy(&txbuf[BLEPROTO_APPDATA_HEADR_LEN], payload, payload_len); + need = (uint16_t)((uint16_t)totalframe * BLEPROTO_APPDATA_HEADR_LEN + payload_len); + if (txbuf_size < need) { + printf("txbuf too short %u<%u\n", (unsigned)txbuf_size, (unsigned)need); + return -1; } - return (int)total; + for (i = 0; i < totalframe; i++) { + uint8_t this_len; + uint8_t frameseq; + + if (totalframe == 1u) { + this_len = (uint8_t)payload_len; + frameseq = 0; /* 单帧序号为 0 */ + } else if ((uint16_t)(i + 1u) < totalframe) { + this_len = frame_max; + frameseq = (uint8_t)(i + 1u); /* 分包从 1 起 */ + } else { + this_len = (uint8_t)(payload_len - data_offset); + frameseq = (uint8_t)(i + 1u); + } + + /* buf[0]: version[2:0] | datafmt[3:3] | msgtype[7:4] */ + txbuf[offset + 0] = UTILS_BITFIELD_SET(header->version, 2, 0) | + UTILS_BITFIELD_SET(header->datafmt, 3, 3) | + UTILS_BITFIELD_SET(header->msgtype, 7, 4); + /* buf[1]: msgid[3:0] | encrypt[4:4] | reserve[7:5] */ + txbuf[offset + 1] = UTILS_BITFIELD_SET(header->msgid, 3, 0) | + UTILS_BITFIELD_SET(header->encrypt, 4, 4) | + UTILS_BITFIELD_SET(header->reserve, 7, 5); + txbuf[offset + 2] = (uint8_t)totalframe; + txbuf[offset + 3] = frameseq; + txbuf[offset + 4] = this_len; + txbuf[offset + 5] = header->serviceid; + proto_bytes_put_le16(&txbuf[offset + 6], payload_len); + + offset = (uint16_t)(offset + BLEPROTO_APPDATA_HEADR_LEN); + if (this_len != 0) { + memcpy(&txbuf[offset], &payload[data_offset], this_len); + offset = (uint16_t)(offset + this_len); + data_offset = (uint16_t)(data_offset + this_len); + } + } + + if (totalframe > 1u) { + printf("wrap_raw split %uB -> %u frames, out %uB\n", + (unsigned)payload_len, (unsigned)totalframe, (unsigned)offset); + } + + return (int)offset; } int bleproto_appdata_deserialize(uint8_t * buf, diff --git a/apps/usr_le_code/bleproto_packer.h b/apps/usr_le_code/bleproto_packer.h index 8ec82c1..e4c1d9c 100644 --- a/apps/usr_le_code/bleproto_packer.h +++ b/apps/usr_le_code/bleproto_packer.h @@ -586,17 +586,21 @@ int bleproto_appdata_serialize(bleproto_appdata_t *appdata, uint16_t size); /** - * @brief 将已打包好的 payload 包装成单帧 appdata(不分包) - * @param txbuf 输出缓冲 + * @brief 将已打包好的 payload 包装成 appdata(超过单帧则按 packetlen 分包) + * @param txbuf 输出缓冲(须能放下 每帧8B头 × 帧数 + payload) * @param txbuf_size 输出缓冲大小 + * @param packetlen BLE GATT 单包有效长度,与 serialize 相同(本板 247) * @param header 应用层头(datalen/totalframe/frameseq 由本函数填写) * @param payload 已序列化的 service 数据;可为 NULL(payload_len==0) - * @param payload_len payload 长度,须 <=255(单帧 pthis_frame_len 为 u8) + * @param payload_len 应用数据总长度 * @return >0 总字节数;<0 失败 - * @note 小包场景(JB 55AA 透传等)用此接口,避免 bleproto_appdata_t 大缓冲 + * @note 切帧规则与 serialize 一致:单帧 payload = packetlen - 8; + * 不超过则 totalframe=1、frameseq=0;超过则中间帧填满、末帧剩余, + * frameseq 从 1 递增。避免走 bleproto_appdata_t 大缓冲。 */ int bleproto_appdata_wrap_raw(uint8_t *txbuf, uint16_t txbuf_size, + uint8_t packetlen, const bleproto_appdata_header_t *header, const uint8_t *payload, uint16_t payload_len); diff --git a/apps/usr_le_code/usr_le_recieve.c b/apps/usr_le_code/usr_le_recieve.c index 4e0ddfe..0b554b4 100644 --- a/apps/usr_le_code/usr_le_recieve.c +++ b/apps/usr_le_code/usr_le_recieve.c @@ -2,11 +2,12 @@ * @file usr_le_recieve.c * @brief BLE 收包入口:bleproto 解析后分发配网 / 对时 / JB 协议 / 组应答 * @author cyWu <1917507415@qq.com> - * @date 2026.08.19 - * @version V1.1.0 + * @date 2026.09.07 + * @version V1.2.0 * @history * - V1.0.0, 2026.08.03, cyWu, 首次发布 * - V1.1.0, 2026.08.19, cyWu, 对齐模组结构;废弃 JSON OTA;修 AUTHDELETE serviceid + * - V1.2.0, 2026.09.07, cyWu, JB 上报超 255 走 wrap_raw 自动分包;加大 txbuf ******************************************************************************/ #include "system/includes.h" @@ -27,15 +28,21 @@ #define LOG_INFO_ENABLE #include "debug.h" -/* 拼 GATT 分包:业务包 ≈ bleproto头 + V2 开销 + JB 帧(<=128),1024 足够 */ +/* 收:GATT 拼包缓冲。发:V2 + bleproto 多帧(单帧总长 247,与 ATT 对齐) */ #define USR_LE_RX_BUF_LEN 1024 -#define USR_LE_V2_WRAP_OVERHEAD 32u /* V2 头开销,JB 帧最大 128 → 约 160 */ +#define USR_LE_V2_WRAP_OVERHEAD 32u /* V2 头开销 */ +#define USR_LE_V2_MAX_LEN (512u + USR_LE_V2_WRAP_OVERHEAD) +#define USR_LE_APPDATA_PACKETLEN 247u /* 对齐 ATT_LOCAL_PAYLOAD_SIZE:8B头+239B数据 */ +#define USR_LE_APPDATA_FRAME_PAYLOAD (USR_LE_APPDATA_PACKETLEN - 8u) +#define USR_LE_TX_BUF_LEN (USR_LE_V2_MAX_LEN + \ + 8u * ((USR_LE_V2_MAX_LEN + USR_LE_APPDATA_FRAME_PAYLOAD - 1u) / \ + USR_LE_APPDATA_FRAME_PAYLOAD)) #define USR_LE_V2_CMD_REPORT 100u /* REPORTCMD_V2 内层 cmd */ #define USR_LE_V2_CMD_RUN 110u /* RUNCMD_V2 内层 cmd */ #define USR_LE_JB_CMD_OFF 4u /* 55 AA | LEN_H LEN_L | CMD */ static int encode_len = 0; -static u8 txbuf[384] = {0}; +static u8 txbuf[USR_LE_TX_BUF_LEN]; static u8 rxbuf[USR_LE_RX_BUF_LEN] = {0}; static u8 s_last_runcmd_msgid = 0; /* RUNCMD_V2 应答须回填最近 msgid */ @@ -75,7 +82,7 @@ static void usr_le_fill_rsp_header(bleproto_appdata_desc_t *out, */ static void usr_le_encode_and_send(bleproto_appdata_desc_t *desc) { - encode_len = bleproto_appdata_encode(txbuf, sizeof(txbuf), sizeof(txbuf), desc); + encode_len = bleproto_appdata_encode(txbuf, sizeof(txbuf),USR_LE_APPDATA_PACKETLEN, desc); if (encode_len > 0) { printf("encode_len = %d\n", encode_len); @@ -365,7 +372,8 @@ void ble_send_data(u8 *data, u16 len) return; } - elen = bleproto_appdata_wrap_raw(txbuf, sizeof(txbuf), &hdr, v2_buf, (uint16_t)plen); + elen = bleproto_appdata_wrap_raw(txbuf, sizeof(txbuf), USR_LE_APPDATA_PACKETLEN, + &hdr, v2_buf, (uint16_t)plen); printf("ble_send_data jb_cmd=0x%02X active=%d encode_len=%d msgid=%d\n", jb_cmd, is_active_send, elen, hdr.msgid); if (elen <= 0)