From 9cbb718484a38218ef976556904d470a8fca0910 Mon Sep 17 00:00:00 2001 From: Zhi Wei Jian Date: Fri, 3 Apr 2026 14:02:35 +0800 Subject: [PATCH] fix(ble/bluedroid): Fixed privacy callback triggering multiple times (cherry picked from commit 5272b7e75b1fdb4613a5ffb92f8783897c38b3ba) Co-authored-by: zhiweijian --- .../bt/host/bluedroid/bta/dm/bta_dm_act.c | 22 +++++++++++++++++++ .../bluedroid/bta/dm/include/bta_dm_gap.h | 7 ++++++ .../host/bluedroid/stack/btm/btm_ble_addr.c | 3 +-- .../bt/host/bluedroid/stack/btm/btm_ble_gap.c | 5 +++-- .../bluedroid/stack/btm/btm_ble_privacy.c | 11 ++-------- .../bluedroid/stack/btm/include/btm_ble_int.h | 1 - 6 files changed, 35 insertions(+), 14 deletions(-) diff --git a/components/bt/host/bluedroid/bta/dm/bta_dm_act.c b/components/bt/host/bluedroid/bta/dm/bta_dm_act.c index 91760ab91d..3f3308c175 100644 --- a/components/bt/host/bluedroid/bta/dm/bta_dm_act.c +++ b/components/bt/host/bluedroid/bta/dm/bta_dm_act.c @@ -38,6 +38,7 @@ #include "bta/utl.h" #include "stack/gap_api.h" /* For GAP_BleReadPeerPrefConnParams */ #include +#include #include "device/controller.h" #include "bta_dm_gap.h" @@ -265,6 +266,27 @@ UINT8 *g_disc_raw_data_buf; #if (BLE_INCLUDED == TRUE) tBTM_BLE_LEGACY_GAP_CBACK ble_legacy_gap_cb; +static uint64_t ble_legacy_gap_oneshot_mask; + +void BTM_BleLegacyGapOneshotArm(tBTM_BLE_LEGACY_GAP_EVENT event) +{ + if (event < BTM_BLE_LEGACY_GAP_MAX_EVT) { + ble_legacy_gap_oneshot_mask |= (1ULL << event); + } +} + +void BTM_BleLegacyGapOneshotFireIfArmed(tBTM_BLE_LEGACY_GAP_EVENT event, tBTM_BLE_LEGACY_GAP_CB_PARAMS *params) +{ + if (event >= BTM_BLE_LEGACY_GAP_MAX_EVT) { + return; + } + uint64_t bit = (1ULL << event); + if ((ble_legacy_gap_oneshot_mask & bit) == 0) { + return; + } + ble_legacy_gap_oneshot_mask &= ~bit; + BTM_LegacyBleCallbackTrigger(event, params); +} void BTM_BleLegacyGapRegisterCallback(tBTM_BLE_LEGACY_GAP_CBACK cb) { diff --git a/components/bt/host/bluedroid/bta/dm/include/bta_dm_gap.h b/components/bt/host/bluedroid/bta/dm/include/bta_dm_gap.h index 35dce1be9f..de3d48033b 100644 --- a/components/bt/host/bluedroid/bta/dm/include/bta_dm_gap.h +++ b/components/bt/host/bluedroid/bta/dm/include/bta_dm_gap.h @@ -103,10 +103,17 @@ typedef union { #define BTM_BLE_LEGACY_GAP_SET_VENDOR_EVT_MASK_COMPLETE_EVT 0x15 #define BTM_BLE_LEGACY_GAP_SET_CHANNELS_COMPLETE_EVT 0x16 #define BTM_BLE_LEGACY_GAP_SET_PRIVACY_MODE_COMPLETE_EVT 0x17 +// BTM_BLE_LEGACY_GAP_MAX_EVT should not be modified +#define BTM_BLE_LEGACY_GAP_MAX_EVT 0x40 typedef void (*tBTM_BLE_LEGACY_GAP_CBACK)(tBTM_BLE_LEGACY_GAP_EVENT event, tBTM_BLE_LEGACY_GAP_CB_PARAMS *params); void BTM_BleLegacyGapRegisterCallback(tBTM_BLE_LEGACY_GAP_CBACK cb); void BTM_LegacyBleCallbackTrigger(tBTM_BLE_LEGACY_GAP_EVENT event, tBTM_BLE_LEGACY_GAP_CB_PARAMS *params); + +/* Arm legacy GAP one-shot: next HCI complete for this event may invoke the registered callback once. */ +void BTM_BleLegacyGapOneshotArm(tBTM_BLE_LEGACY_GAP_EVENT event); +/* If armed, disarm and call BTM_LegacyBleCallbackTrigger; otherwise no-op. */ +void BTM_BleLegacyGapOneshotFireIfArmed(tBTM_BLE_LEGACY_GAP_EVENT event, tBTM_BLE_LEGACY_GAP_CB_PARAMS *params); #endif // #if BLE_INCLUDED == TRUE #endif /* BTA_DM_GAP_H */ diff --git a/components/bt/host/bluedroid/stack/btm/btm_ble_addr.c b/components/bt/host/bluedroid/stack/btm/btm_ble_addr.c index e3ea81961d..052f21528b 100644 --- a/components/bt/host/bluedroid/stack/btm/btm_ble_addr.c +++ b/components/bt/host/bluedroid/stack/btm/btm_ble_addr.c @@ -80,10 +80,9 @@ static void btm_gen_resolve_paddr_cmpl(tSMP_ENC *p) BTM_TRACE_DEBUG("set random address failed"); status = BTM_SET_PRIVACY_FAIL; } - tBTM_BLE_LEGACY_GAP_CB_PARAMS cb_params = {0}; cb_params.status = status; - BTM_LegacyBleCallbackTrigger(BTM_BLE_LEGACY_GAP_SET_PRIVACY_EVT, &cb_params); + BTM_BleLegacyGapOneshotFireIfArmed(BTM_BLE_LEGACY_GAP_SET_PRIVACY_EVT, &cb_params); } /******************************************************************************* ** diff --git a/components/bt/host/bluedroid/stack/btm/btm_ble_gap.c b/components/bt/host/bluedroid/stack/btm/btm_ble_gap.c index 50d89d8b0b..2446251dd5 100644 --- a/components/bt/host/bluedroid/stack/btm/btm_ble_gap.c +++ b/components/bt/host/bluedroid/stack/btm/btm_ble_gap.c @@ -37,6 +37,7 @@ #include "hci/hci_layer.h" #if BLE_INCLUDED == TRUE #include "l2c_int.h" +#include "bta_dm_gap.h" #include "stack/gattdefs.h" #include "gatt_int.h" @@ -493,7 +494,7 @@ BOOLEAN BTM_BleConfigPrivacy(BOOLEAN privacy_mode) } if (random_cb){ - random_cb->cb_is_triggered = false; + BTM_BleLegacyGapOneshotArm(BTM_BLE_LEGACY_GAP_SET_PRIVACY_EVT); }else{ BTM_TRACE_ERROR("%s,random_cb = NULL", __func__); } @@ -4104,7 +4105,7 @@ BOOLEAN BTM_BleAddDevToResolvingList(BD_ADDR addr, BTM_TRACE_ERROR("Add device to resolving list error"); return FALSE; } - + BTM_BleLegacyGapOneshotArm(BTM_BLE_LEGACY_GAP_ADD_DEV_TO_RPA_LIST_EVT); return TRUE; } diff --git a/components/bt/host/bluedroid/stack/btm/btm_ble_privacy.c b/components/bt/host/bluedroid/stack/btm/btm_ble_privacy.c index 1bf66625fc..45f3af5bcd 100644 --- a/components/bt/host/bluedroid/stack/btm/btm_ble_privacy.c +++ b/components/bt/host/bluedroid/stack/btm/btm_ble_privacy.c @@ -298,7 +298,7 @@ void btm_ble_add_resolving_list_entry_complete(UINT8 *p, UINT16 evt_len) tBTM_BLE_LEGACY_GAP_CB_PARAMS cb_params = {0}; cb_params.status = status; - BTM_LegacyBleCallbackTrigger(BTM_BLE_LEGACY_GAP_ADD_DEV_TO_RPA_LIST_EVT, &cb_params); + BTM_BleLegacyGapOneshotFireIfArmed(BTM_BLE_LEGACY_GAP_ADD_DEV_TO_RPA_LIST_EVT, &cb_params); BTM_TRACE_DEBUG("%s status = %d", __func__, status); @@ -436,19 +436,12 @@ void btm_ble_set_addr_resolution_enable_complete(UINT8 *p, UINT16 evt_len) BTM_TRACE_DEBUG("%s status = %d", __func__, status); - tBTM_LE_RANDOM_CB *random_cb = &btm_cb.ble_ctr_cb.addr_mgnt_cb; - - if (random_cb->cb_is_triggered) { - return; - } - if (status != HCI_SUCCESS) { BTM_TRACE_ERROR("set local privacy failed with status: 0x%x", status); } - random_cb->cb_is_triggered = true; tBTM_BLE_LEGACY_GAP_CB_PARAMS cb_params = {0}; cb_params.status = (status == HCI_SUCCESS)? BTM_SUCCESS : BTM_ILLEGAL_VALUE; - BTM_LegacyBleCallbackTrigger(BTM_BLE_LEGACY_GAP_SET_PRIVACY_EVT, &cb_params); + BTM_BleLegacyGapOneshotFireIfArmed(BTM_BLE_LEGACY_GAP_SET_PRIVACY_EVT, &cb_params); } /******************************************************************************* diff --git a/components/bt/host/bluedroid/stack/btm/include/btm_ble_int.h b/components/bt/host/bluedroid/stack/btm/include/btm_ble_int.h index 3f200ae10b..02fa86b47e 100644 --- a/components/bt/host/bluedroid/stack/btm/include/btm_ble_int.h +++ b/components/bt/host/bluedroid/stack/btm/include/btm_ble_int.h @@ -203,7 +203,6 @@ typedef struct { tBTM_BLE_ADDR_CBACK *p_generate_cback; void *p; TIMER_LIST_ENT raddr_timer_ent; - bool cb_is_triggered; } tBTM_LE_RANDOM_CB; #define BTM_BLE_MAX_BG_CONN_DEV_NUM 10