diff --git a/apps/usr_le_code/usr_dengle_sched.c b/apps/usr_le_code/usr_dengle_sched.c index 223b153..6907a96 100644 --- a/apps/usr_le_code/usr_dengle_sched.c +++ b/apps/usr_le_code/usr_dengle_sched.c @@ -501,7 +501,7 @@ Dengle_Ret_t dengle_sched_usb_set(const uint8_t *payload, uint16_t payload_len) if (!s_inited) { return DENGLE_ERR_NOT_INIT; } - if (!payload || payload_len < 9) { + if (!payload || payload_len < 1) { return DENGLE_ERR_PARAM; } diff --git a/apps/usr_le_code/usr_le_recieve.c b/apps/usr_le_code/usr_le_recieve.c index 7370e14..320d971 100644 --- a/apps/usr_le_code/usr_le_recieve.c +++ b/apps/usr_le_code/usr_le_recieve.c @@ -360,24 +360,12 @@ void usr_data_ana(bleproto_appdata_desc_t *desc) printf("E_BLEPROTO_SERVICE_ID_RUNCMD_V2 cmd=%d bytes_len=%d\n", pkt->cmd, pkt->device_bytes_len); -#if TCFG_USB_SLAVE_CDC_ENABLE - /* 透传 device bytes 到 USB CDC(串口助手可见) */ - if (pkt->bytes_data && pkt->device_bytes_len > 0) - { - u32 wlen = cdc_write_data(0, (u8 *)pkt->bytes_data, pkt->device_bytes_len); - printf("cdc_write_data %u/%u\n", wlen, (u32)pkt->device_bytes_len); - if (wlen == 0) - { - printf("cdc write fail: USB not ready(DTR) or CDC not started\n"); - } - } - else - { + /* COM 打开→透传 CDC;COM 关闭→对指定 JB 命令本地自动应答回 BLE */ + if (pkt->bytes_data && pkt->device_bytes_len > 0) { + usr_jb_on_runcmd_bytes(pkt->bytes_data, pkt->device_bytes_len); + } else { printf("runcmd_v2 no bytes payload\n"); } -#else - printf("TCFG_USB_SLAVE_CDC_ENABLE not enable\n"); -#endif break; } default: diff --git a/apps/usr_le_code/usr_usb_jb.c b/apps/usr_le_code/usr_usb_jb.c index 43cce8a..7823f1f 100644 --- a/apps/usr_le_code/usr_usb_jb.c +++ b/apps/usr_le_code/usr_usb_jb.c @@ -90,6 +90,9 @@ static uint8_t s_ble_txbuf[USR_JB_BLE_TX_SZ]; static uint8_t s_last_ble_msgid; /* 最近一次 BLE RX 的 msgid */ static uint8_t s_rsp_buf[JB_RSP_BUF_SZ]; +static uint8_t usr_jb_checksum(const uint8_t *frame, uint16_t total_len); +static uint8_t usr_jb_need_com_offline_auto_ack(uint8_t cmd); + void usr_ble_rx_msgid_save(uint8_t msgid) { s_last_ble_msgid = msgid & 0x0F; @@ -126,6 +129,7 @@ static int usr_jb_map_ble_pkt(uint8_t jb_cmd, ble_proto_packet_t *pkt) case 0x04: /* CONTROL_ACK */ case 0x06: /* READ_ACK */ case 0x31: /* OTA_NOTIFY_ACK */ + case 0x33: /* OTA_DATA_ACK */ case 0x37: /* OTA_STOP_BLE_ACK */ pkt->cmd = USR_JB_BLE_CMD_ACK; pkt->gateway_int_cnt = 2; @@ -200,6 +204,46 @@ static void usr_jb_forward_to_ble(const uint8_t *frame, uint16_t len) } } +/** + * @brief 是否属于 COM 离线时需本地自动应答的命令 + */ +static uint8_t usr_jb_need_com_offline_auto_ack(uint8_t cmd) +{ + switch (cmd) { + case USR_JB_CMD_CTRL: /* 0x03 → ACK 0x04,映射表已支持 */ + case USR_JB_CMD_READ: /* 0x05 → ACK 0x06,映射表已支持 */ + case USR_JB_CMD_OTA_NOTIFY: /* 0x30 → ACK 0x31 */ + case USR_JB_CMD_OTA_DATA: /* 0x32 → ACK 0x33 */ + case USR_JB_CMD_OTA_STOP_BLE: /* 0x36 → ACK 0x37 */ + return 1; + default: + return 0; + } +} + +/** + * @brief 组简单 JB 应答帧(cmd+1 / 同 SN / result),再走 forward 发 BLE + */ +static void usr_jb_send_simple_ack_frame(uint8_t req_cmd, uint8_t sn, uint8_t result) +{ + uint8_t frame[8]; + uint16_t len_field = 4; /* CMD+SN+result+CKSUM */ + uint16_t total = len_field + 4; + + frame[0] = PKT_HEAD_0; + frame[1] = PKT_HEAD_1; + frame[2] = (uint8_t)(len_field >> 8); + frame[3] = (uint8_t)(len_field & 0xFF); + frame[4] = (uint8_t)(req_cmd + 1); + frame[5] = sn; + frame[6] = result; + frame[7] = usr_jb_checksum(frame, total); + + printf("usr_jb auto-ack req=0x%02x rsp=0x%02x sn=%u result=0x%02x\n", + req_cmd, frame[4], sn, result); + usr_jb_forward_to_ble(frame, total); +} + /*============================================================================*/ /* 环形缓冲 / 校验 */ /*============================================================================*/ @@ -621,7 +665,21 @@ static void usr_jb_process_timeout(void *priv) break; default: - /* 其它命令:整帧透传到 BLE(按 JB cmd 映射 ble_proto cmd) */ + /* + * COM 未打开:0x03/0x05 等需本地自动应答回 BLE; + * COM 已打开或其它命令:按原逻辑透传(由映射表决定能否发出) + */ +#if TCFG_USB_SLAVE_CDC_ENABLE + if (!cdc_is_com_open() && usr_jb_need_com_offline_auto_ack(cmd)) { + usr_jb_send_simple_ack_frame(cmd, sn, 0x00); + break; + } +#else + if (usr_jb_need_com_offline_auto_ack(cmd)) { + usr_jb_send_simple_ack_frame(cmd, sn, 0x00); + break; + } +#endif usr_jb_forward_to_ble(pkt, pkt_len); break; } @@ -676,3 +734,25 @@ void usr_usb_jb_feed(uint8_t *buf, uint32_t len) s_process_timer = sys_timeout_add(NULL, usr_jb_process_timeout, 20); } } + +/** + * @brief BLE RUNCMD_V2 bytes:COM 开则透传 CDC,关则走 feed 统一解析/自动应答 + */ +void usr_jb_on_runcmd_bytes(const uint8_t *data, uint16_t len) +{ + if (!data || len == 0) { + return; + } + +#if TCFG_USB_SLAVE_CDC_ENABLE + if (cdc_is_com_open()) { + u32 wlen = cdc_write_data(0, (u8 *)data, len); + printf("usr_jb runcmd -> CDC %u/%u\n", wlen, (u32)len); + return; + } +#endif + + /* COM 未打开:与 USB 入环同一路径,由 process 里 get_one_packet + auto-ack 处理 */ + printf("usr_jb runcmd COM closed, feed for local auto-ack len=%u\n", len); + usr_usb_jb_feed((uint8_t *)data, len); +} diff --git a/apps/usr_le_code/usr_usb_jb.h b/apps/usr_le_code/usr_usb_jb.h index 4b1d195..7d67c23 100644 --- a/apps/usr_le_code/usr_usb_jb.h +++ b/apps/usr_le_code/usr_usb_jb.h @@ -25,6 +25,15 @@ #define USR_JB_CMD_DENGLE_CLR (0x72) /**< USB 清除全部 Dengle;ACK=0x73 */ #define USR_JB_CMD_DENGLE_QRY (0x74) /**< USB 查询 Dengle 配置;RSP=0x75 */ +/** COM 未打开时,BLE 下发需设备本地自动应答的 JB 命令 */ +#define USR_JB_CMD_CTRL (0x03) /**< → ACK 0x04 */ +#define USR_JB_CMD_READ (0x05) /**< → ACK 0x06 */ + +/** OTA 相关命令 */ +#define USR_JB_CMD_OTA_NOTIFY (0x30) /**< → ACK 0x31 */ +#define USR_JB_CMD_OTA_DATA (0x32) /**< → ACK 0x33 */ +#define USR_JB_CMD_OTA_STOP_BLE (0x36) /**< → ACK 0x37 */ + /*============================================================================*/ /* 外部接口 */ /*============================================================================*/ @@ -65,4 +74,14 @@ uint8_t usr_ble_rx_msgid_get(void); */ void usr_usb_jb_feed(uint8_t *buf, uint32_t len); +/** + * @brief BLE RUNCMD_V2 下发的 JB 字节流处理 + * @note COM 已打开 → 透传 CDC 给上位机; + * COM 未打开 → 调用 usr_usb_jb_feed,统一走 get_one_packet 解析后本地自动应答 + * @param data bytes_data + * @param len 长度 + * @return void + */ +void usr_jb_on_runcmd_bytes(const uint8_t *data, uint16_t len); + #endif /* __USR_USB_JB_H__ */