新增COM离线时对控制/读取/OTA相关JB命令的本地自动应答,经BLE回传网关

This commit is contained in:
2026-07-31 16:06:14 +08:00
parent 2260768167
commit b5d590c6f1
4 changed files with 105 additions and 18 deletions
+81 -1
View File
@@ -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 bytesCOM 开则透传 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);
}