Skip to content

Commit 385f0d5

Browse files
committed
[Bugfix] Scan duplicate filter not resetting on scan restart.
This fixes a bug caused by the arduino core enabling mesh/mesh filtering which sets the incorrect mode for normal BLE scanning. In Mesh filtering mode the controller does not flush the device cache so subsequent restarts of the scan will not provide duplicates as expected.
1 parent 54e6a51 commit 385f0d5

2 files changed

Lines changed: 18 additions & 17 deletions

File tree

src/NimBLEDevice.cpp

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -101,15 +101,11 @@ ble_gap_event_listener NimBLEDevice::m_listener{};
101101
std::vector<NimBLEAddress> NimBLEDevice::m_whiteList{};
102102
uint8_t NimBLEDevice::m_ownAddrType{BLE_OWN_ADDR_PUBLIC};
103103

104-
# ifdef ESP_PLATFORM
105-
# if CONFIG_BTDM_BLE_SCAN_DUPL
106-
uint16_t NimBLEDevice::m_scanDuplicateSize{CONFIG_BTDM_SCAN_DUPL_CACHE_SIZE};
107-
uint8_t NimBLEDevice::m_scanFilterMode{CONFIG_BTDM_SCAN_DUPL_TYPE};
104+
# if NIMBLE_CPP_SCAN_DUPL_ENABLED
105+
uint16_t NimBLEDevice::m_scanDuplicateSize{100};
106+
uint8_t NimBLEDevice::m_scanFilterMode{0};
108107
uint16_t NimBLEDevice::m_scanDuplicateResetTime{0};
109-
# elif CONFIG_BT_LE_SCAN_DUPL
110-
uint16_t NimBLEDevice::m_scanDuplicateSize{CONFIG_BT_LE_LL_DUP_SCAN_LIST_COUNT};
111-
uint8_t NimBLEDevice::m_scanFilterMode{CONFIG_BT_LE_SCAN_DUPL_TYPE};
112-
uint16_t NimBLEDevice::m_scanDuplicateResetTime{0};
108+
# if SOC_ESP_NIMBLE_CONTROLLER
113109
extern "C" int ble_vhci_disc_duplicate_set_max_cache_size(int max_cache_size);
114110
extern "C" int ble_vhci_disc_duplicate_set_period_refresh_time(int refresh_period_time);
115111
extern "C" int ble_vhci_disc_duplicate_mode_disable(int mode);
@@ -918,23 +914,25 @@ bool NimBLEDevice::init(const std::string& deviceName) {
918914
bt_cfg.nimble_max_connections = MYNEWT_VAL(BLE_MAX_CONNECTIONS);
919915
# endif
920916

921-
# if CONFIG_BTDM_BLE_SCAN_DUPL
917+
# if NIMBLE_CPP_SCAN_DUPL_ENABLED
918+
# if !SOC_ESP_NIMBLE_CONTROLLER
922919
bt_cfg.normal_adv_size = m_scanDuplicateSize;
923920
bt_cfg.scan_duplicate_type = m_scanFilterMode;
924-
# if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 0, 0)
921+
bt_cfg.scan_duplicate_mode = 0; // Ensure normal filter mode, could be set to mesh in default config
922+
# if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 0, 0)
925923
bt_cfg.dup_list_refresh_period = m_scanDuplicateResetTime;
926-
# endif
927-
# elif CONFIG_BT_LE_SCAN_DUPL
924+
# endif
925+
# else // SOC_ESP_NIMBLE_CONTROLLER
928926
bt_cfg.ble_ll_rsp_dup_list_count = m_scanDuplicateSize;
929927
bt_cfg.ble_ll_adv_dup_list_count = m_scanDuplicateSize;
930-
# endif
928+
# endif // SOC_ESP_NIMBLE_CONTROLLER
931929
err = esp_bt_controller_init(&bt_cfg);
932930
if (err != ESP_OK) {
933931
NIMBLE_LOGE(LOG_TAG, "esp_bt_controller_init() failed; err=%d", err);
934932
return false;
935933
}
936934

937-
# if CONFIG_BT_LE_SCAN_DUPL
935+
# if SOC_ESP_NIMBLE_CONTROLLER
938936
int mode = (1UL << 4); // FILTER_DUPLICATE_EXCEPTION_FOR_MESH
939937
switch (m_scanFilterMode) {
940938
case 1:
@@ -944,14 +942,15 @@ bool NimBLEDevice::init(const std::string& deviceName) {
944942
mode |= ((1UL << 2) | (1UL << 3)); // FILTER_DUPLICATE_ADDRESS | FILTER_DUPLICATE_ADVDATA
945943
break;
946944
default:
947-
mode |= (1UL << 0) | (1UL << 2); // FILTER_DUPLICATE_PDUTYPE | FILTER_DUPLICATE_ADDRESS
945+
mode |= ((1UL << 0) | (1UL << 2)); // FILTER_DUPLICATE_PDUTYPE | FILTER_DUPLICATE_ADDRESS
948946
}
949947

950948
ble_vhci_disc_duplicate_mode_disable(0xFFFFFFFF);
951949
ble_vhci_disc_duplicate_mode_enable(mode);
952950
ble_vhci_disc_duplicate_set_max_cache_size(m_scanDuplicateSize);
953951
ble_vhci_disc_duplicate_set_period_refresh_time(m_scanDuplicateResetTime);
954-
# endif
952+
# endif // SOC_ESP_NIMBLE_CONTROLLER
953+
# endif // NIMBLE_CPP_SCAN_DUPL_ENABLED
955954

956955
err = esp_bt_controller_enable(ESP_BT_MODE_BLE);
957956
if (err != ESP_OK) {

src/NimBLEDevice.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
# ifndef CONFIG_IDF_TARGET_ESP32P4
2525
# include <esp_bt.h>
2626
# endif
27+
# define NIMBLE_CPP_SCAN_DUPL_ENABLED \
28+
(CONFIG_BTDM_BLE_SCAN_DUPL || CONFIG_BT_LE_SCAN_DUPL || CONFIG_BT_CTRL_BLE_SCAN_DUPL)
2729
# endif
2830

2931
# if defined(CONFIG_NIMBLE_CPP_IDF)
@@ -243,7 +245,7 @@ class NimBLEDevice {
243245
# endif
244246

245247
# ifdef ESP_PLATFORM
246-
# if CONFIG_BTDM_BLE_SCAN_DUPL || CONFIG_BT_LE_SCAN_DUPL
248+
# if NIMBLE_CPP_SCAN_DUPL_ENABLED
247249
static uint16_t m_scanDuplicateSize;
248250
static uint8_t m_scanFilterMode;
249251
static uint16_t m_scanDuplicateResetTime;

0 commit comments

Comments
 (0)