From 277462a8fe3eb15bb17ebf3b61b6b774def0a53b Mon Sep 17 00:00:00 2001 From: skiinder Date: Sat, 28 Mar 2026 13:59:59 +0800 Subject: [PATCH] =?UTF-8?q?feat(ble):=20=E6=B7=BB=E5=8A=A0=E5=BF=AB?= =?UTF-8?q?=E9=80=9F=E5=B9=BF=E5=91=8A=E9=85=8D=E7=BD=AE=E5=B9=B6=E4=BC=98?= =?UTF-8?q?=E5=8C=96=E8=BF=9E=E6=8E=A5=E7=8A=B6=E6=80=81=E6=A3=80=E6=9F=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 添加了BLE快速广告相关的配置选项到prj.conf中,包括快速广告间隔、超时等参数。 同时修复了ble_bond_module中的连接状态检查逻辑,避免在挂起后保留LE连接时进行不必要的 断开操作。 在ble_hid_module和usb_hid_module中改进了HID传输事件处理逻辑,确保在相应模式未激活 或连接未建立时正确提交传输完成事件,提高了设备响应的准确性。 BREAKING CHANGE: 广告行为在连接保持情况下有所改变,可能影响配对流程。 --- prj.conf | 4 ++++ src/modules/ble_bond_module.c | 28 +++++++++++++++++++++++++++- src/modules/ble_hid_module.c | 14 ++++++++------ src/modules/usb_hid_module.c | 10 +++++++++- 4 files changed, 48 insertions(+), 8 deletions(-) diff --git a/prj.conf b/prj.conf index 54cd056..f0ce0ec 100644 --- a/prj.conf +++ b/prj.conf @@ -34,6 +34,10 @@ CONFIG_BT_SETTINGS=y CONFIG_CAF_BLE_STATE=y CONFIG_CAF_BLE_ADV=y +CONFIG_CAF_BLE_ADV_FAST_ADV=y +CONFIG_CAF_BLE_ADV_FAST_INT_MIN=0x0030 +CONFIG_CAF_BLE_ADV_FAST_INT_MAX=0x0060 +CONFIG_CAF_BLE_ADV_FAST_ADV_TIMEOUT=180 CONFIG_CAF_MODULE_SUSPEND_EVENTS=y CONFIG_CAF_SETTINGS_LOADER=y CONFIG_BT_ADV_PROV_FLAGS=y diff --git a/src/modules/ble_bond_module.c b/src/modules/ble_bond_module.c index 9ccf683..6b54959 100644 --- a/src/modules/ble_bond_module.c +++ b/src/modules/ble_bond_module.c @@ -215,6 +215,23 @@ static void disconnect_le_conn_cb(struct bt_conn *conn, void *user_data) } } +static void mark_le_conn_found_cb(struct bt_conn *conn, void *user_data) +{ + bool *found = user_data; + + ARG_UNUSED(conn); + *found = true; +} + +static bool has_any_le_conn(void) +{ + bool found = false; + + bt_conn_foreach(BT_CONN_TYPE_LE, mark_le_conn_found_cb, &found); + + return found; +} + struct peer_bond_lookup { const bt_addr_le_t *peer_addr; bool found; @@ -532,7 +549,16 @@ static bool app_event_handler(const struct app_event_header *aeh) if (bond.state == BLE_BOND_STATE_STANDBY) { bond.state = BLE_BOND_STATE_IDLE; module_set_state(MODULE_STATE_READY); - submit_peer_op_event(PEER_OPERATION_SELECTED, bond.storage.cur_peer_id); + /* + * If a LE link survived suspend, keep it untouched. CAF ble_adv + * treats PEER_OPERATION_SELECTED as a real identity switch and + * will disconnect the current peer. If no link exists, re-emit + * the selection so advertising resumes on the selected slot. + */ + if (!has_any_le_conn()) { + submit_peer_op_event(PEER_OPERATION_SELECTED, + bond.storage.cur_peer_id); + } } return false; } diff --git a/src/modules/ble_hid_module.c b/src/modules/ble_hid_module.c index 2deb6ec..ac6b284 100644 --- a/src/modules/ble_hid_module.c +++ b/src/modules/ble_hid_module.c @@ -52,11 +52,6 @@ static bool ble_hid_is_connected(void) return ble_hid.link.conn != NULL; } -static bool ble_hid_is_active(void) -{ - return ble_hid.policy.ble_mode_selected && ble_hid_is_connected(); -} - static bool ble_hid_is_boot_mode(void) { return ble_hid.link.protocol_mode == BT_HIDS_PM_BOOT; @@ -232,7 +227,12 @@ static void handle_ble_peer_event(const struct ble_peer_event *event) static bool handle_hid_tx_event(const struct hid_tx_event *event) { - if (!ble_hid_is_active()) { + if (!ble_hid.policy.ble_mode_selected) { + return false; + } + + if (!ble_hid_is_connected()) { + hid_tx_done_event_submit(event->kind, false); return false; } @@ -242,6 +242,7 @@ static bool handle_hid_tx_event(const struct hid_tx_event *event) int err; if (!ble_hid_is_boot_mode()) { + hid_tx_done_event_submit(HID_TX_KIND_BOOT, false); return false; } @@ -263,6 +264,7 @@ static bool handle_hid_tx_event(const struct hid_tx_event *event) } if (!ble_hid_is_report_mode()) { + hid_tx_done_event_submit(HID_TX_KIND_REPORT, false); return false; } diff --git a/src/modules/usb_hid_module.c b/src/modules/usb_hid_module.c index e3c79a8..d5386d3 100644 --- a/src/modules/usb_hid_module.c +++ b/src/modules/usb_hid_module.c @@ -711,11 +711,17 @@ static bool handle_wake_up_event(void) static bool handle_hid_tx_event(const struct hid_tx_event *event) { - if (!g_usb_hid.policy.usb_mode_selected || !usb_hid_stack_is_active()) + if (!g_usb_hid.policy.usb_mode_selected) { return false; } + if (!usb_hid_stack_is_active()) + { + submit_usb_tx_done(event->kind, false); + return false; + } + if (event->kind == HID_TX_KIND_BOOT) { const uint8_t *payload = hid_tx_event_get_data(event); @@ -724,6 +730,7 @@ static bool handle_hid_tx_event(const struct hid_tx_event *event) if (g_usb_hid.current_protocol != HID_PROTO_BOOT) { + submit_usb_tx_done(HID_TX_KIND_BOOT, false); return false; } @@ -762,6 +769,7 @@ static bool handle_hid_tx_event(const struct hid_tx_event *event) */ if (g_usb_hid.current_protocol != HID_PROTO_REPORT) { + submit_usb_tx_done(HID_TX_KIND_REPORT, false); return false; }