Skip to content

Commit e615deb

Browse files
committed
Add Scan duplicate cache reset time feature.
Adds the ability to set a time for the scan duplicate filter cache to reset which will start reporting devices previously seen again.
1 parent e29d1ce commit e615deb

File tree

3 files changed

+73
-8
lines changed

3 files changed

+73
-8
lines changed

src/NimBLEDevice.cpp

Lines changed: 58 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@
6666
# if defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL)
6767
# include "NimBLEServer.h"
6868
# if CONFIG_BT_NIMBLE_L2CAP_COC_MAX_NUM > 0
69-
# include "NimBLEL2CAPServer.h"
69+
# include "NimBLEL2CAPServer.h"
7070
# endif
7171
# endif
7272

@@ -113,9 +113,18 @@ std::vector<NimBLEAddress> NimBLEDevice::m_whiteList{};
113113
uint8_t NimBLEDevice::m_ownAddrType{BLE_OWN_ADDR_PUBLIC};
114114

115115
# ifdef ESP_PLATFORM
116-
# ifdef CONFIG_BTDM_BLE_SCAN_DUPL
116+
# if CONFIG_BTDM_BLE_SCAN_DUPL
117117
uint16_t NimBLEDevice::m_scanDuplicateSize{CONFIG_BTDM_SCAN_DUPL_CACHE_SIZE};
118118
uint8_t NimBLEDevice::m_scanFilterMode{CONFIG_BTDM_SCAN_DUPL_TYPE};
119+
uint16_t NimBLEDevice::m_scanDuplicateResetTime{0};
120+
# elif CONFIG_BT_LE_SCAN_DUPL
121+
uint16_t NimBLEDevice::m_scanDuplicateSize{CONFIG_BT_LE_LL_DUP_SCAN_LIST_COUNT};
122+
uint8_t NimBLEDevice::m_scanFilterMode{CONFIG_BT_LE_SCAN_DUPL_TYPE};
123+
uint16_t NimBLEDevice::m_scanDuplicateResetTime{0};
124+
extern "C" int ble_vhci_disc_duplicate_set_max_cache_size(int max_cache_size);
125+
extern "C" int ble_vhci_disc_duplicate_set_period_refresh_time(int refresh_period_time);
126+
extern "C" int ble_vhci_disc_duplicate_mode_disable(int mode);
127+
extern "C" int ble_vhci_disc_duplicate_mode_enable(int mode);
119128
# endif
120129
# endif
121130

@@ -258,7 +267,7 @@ NimBLEScan* NimBLEDevice::getScan() {
258267
} // getScan
259268

260269
# ifdef ESP_PLATFORM
261-
# ifdef CONFIG_BTDM_BLE_SCAN_DUPL
270+
# if CONFIG_BTDM_BLE_SCAN_DUPL || CONFIG_BT_LE_SCAN_DUPL
262271
/**
263272
* @brief Set the duplicate filter cache size for filtering scanned devices.
264273
* @param [in] size The number of advertisements filtered before the cache is reset.\n
@@ -305,7 +314,26 @@ void NimBLEDevice::setScanFilterMode(uint8_t mode) {
305314

306315
m_scanFilterMode = mode;
307316
}
308-
# endif // CONFIG_BTDM_BLE_SCAN_DUPL
317+
318+
/**
319+
* @brief Set the time in seconds to reset the duplicate cache.
320+
* @param [in] time The time in seconds to reset the cache.
321+
* @details When the cache is reset all scanned devices will be reported again
322+
* even if already seen in the current scan. If set to 0 the cache will never be reset.
323+
*/
324+
void NimBLEDevice::setScanDuplicateCacheResetTime(uint16_t time) {
325+
if (m_initialized) {
326+
NIMBLE_LOGE(LOG_TAG, "Cannot change scan cache reset time while initialized");
327+
return;
328+
} else if (time > 1000) {
329+
NIMBLE_LOGE(LOG_TAG, "Invalid scan cache reset time");
330+
return;
331+
}
332+
333+
NIMBLE_LOGD(LOG_TAG, "Set duplicate cache reset time to: %u", time);
334+
m_scanDuplicateResetTime = time;
335+
}
336+
# endif // CONFIG_BTDM_BLE_SCAN_DUPL || CONFIG_BT_LE_SCAN_DUPL
309337
# endif // ESP_PLATFORM
310338
# endif // #if defined(CONFIG_BT_NIMBLE_ROLE_OBSERVER)
311339

@@ -900,16 +928,39 @@ bool NimBLEDevice::init(const std::string& deviceName) {
900928
bt_cfg.nimble_max_connections = CONFIG_BT_NIMBLE_MAX_CONNECTIONS;
901929
# endif
902930

903-
# ifdef CONFIG_BTDM_BLE_SCAN_DUPL
904-
bt_cfg.normal_adv_size = m_scanDuplicateSize;
905-
bt_cfg.scan_duplicate_type = m_scanFilterMode;
931+
# if CONFIG_BTDM_BLE_SCAN_DUPL
932+
bt_cfg.normal_adv_size = m_scanDuplicateSize;
933+
bt_cfg.scan_duplicate_type = m_scanFilterMode;
934+
bt_cfg.dup_list_refresh_period = m_scanDuplicateResetTime;
935+
# elif CONFIG_BT_LE_SCAN_DUPL
936+
bt_cfg.ble_ll_rsp_dup_list_count = m_scanDuplicateSize;
937+
bt_cfg.ble_ll_adv_dup_list_count = m_scanDuplicateSize;
906938
# endif
907939
err = esp_bt_controller_init(&bt_cfg);
908940
if (err != ESP_OK) {
909941
NIMBLE_LOGE(LOG_TAG, "esp_bt_controller_init() failed; err=%d", err);
910942
return false;
911943
}
912944

945+
# if CONFIG_BT_LE_SCAN_DUPL
946+
int mode = (1UL << 4); // FILTER_DUPLICATE_EXCEPTION_FOR_MESH
947+
switch (m_scanFilterMode) {
948+
case 1:
949+
mode |= (1UL << 3); // FILTER_DUPLICATE_ADVDATA
950+
break;
951+
case 2:
952+
mode |= ((1UL << 2) | (1UL << 3)); // FILTER_DUPLICATE_ADDRESS | FILTER_DUPLICATE_ADVDATA
953+
break;
954+
default:
955+
mode |= (1UL << 0) | (1UL << 2); // FILTER_DUPLICATE_PDUTYPE | FILTER_DUPLICATE_ADDRESS
956+
}
957+
958+
ble_vhci_disc_duplicate_mode_disable(0xFFFFFFFF);
959+
ble_vhci_disc_duplicate_mode_enable(mode);
960+
ble_vhci_disc_duplicate_set_max_cache_size(m_scanDuplicateSize);
961+
ble_vhci_disc_duplicate_set_period_refresh_time(m_scanDuplicateResetTime);
962+
# endif
963+
913964
err = esp_bt_controller_enable(ESP_BT_MODE_BLE);
914965
if (err != ESP_OK) {
915966
NIMBLE_LOGE(LOG_TAG, "esp_bt_controller_enable() failed; err=%d", err);

src/NimBLEDevice.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ class NimBLEDevice {
138138
static void setDeviceCallbacks(NimBLEDeviceCallbacks* cb);
139139
static void setScanDuplicateCacheSize(uint16_t cacheSize);
140140
static void setScanFilterMode(uint8_t type);
141+
static void setScanDuplicateCacheResetTime(uint16_t time);
141142
static bool setCustomGapHandler(gap_event_handler handler);
142143
static void setSecurityAuth(bool bonding, bool mitm, bool sc);
143144
static void setSecurityAuth(uint8_t auth);
@@ -248,9 +249,10 @@ class NimBLEDevice {
248249
# endif
249250

250251
# ifdef ESP_PLATFORM
251-
# ifdef CONFIG_BTDM_BLE_SCAN_DUPL
252+
# if CONFIG_BTDM_BLE_SCAN_DUPL || CONFIG_BT_LE_SCAN_DUPL
252253
static uint16_t m_scanDuplicateSize;
253254
static uint8_t m_scanFilterMode;
255+
static uint16_t m_scanDuplicateResetTime;
254256
# endif
255257
# endif
256258

src/nimconfig_rename.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,3 +79,15 @@
7979
#if !defined(CONFIG_BTDM_SCAN_DUPL_TYPE_DATA_DEVICE) && defined(CONFIG_BT_CTRL_SCAN_DUPL_TYPE_DATA_DEVICE)
8080
#define CONFIG_BTDM_SCAN_DUPL_TYPE_DATA_DEVICE CONFIG_BT_CTRL_SCAN_DUPL_TYPE_DATA_DEVICE
8181
#endif
82+
83+
#ifdef CONFIG_BT_LE_LL_CFG_FEAT_LE_CODED_PHY
84+
#define CONFIG_BT_NIMBLE_LL_CFG_FEAT_LE_CODED_PHY CONFIG_BT_LE_LL_CFG_FEAT_LE_CODED_PHY
85+
#endif
86+
87+
#ifdef CONFIG_BT_LE_LL_CFG_FEAT_LE_2M_PHY
88+
#define CONFIG_BT_NIMBLE_LL_CFG_FEAT_LE_2M_PHY CONFIG_BT_LE_LL_CFG_FEAT_LE_2M_PHY
89+
#endif
90+
91+
#ifdef CONFIG_BT_LE_50_FEATURE_SUPPORT
92+
#define CONFIG_BT_NIMBLE_50_FEATURE_SUPPORT CONFIG_BT_LE_50_FEATURE_SUPPORT
93+
#endif

0 commit comments

Comments
 (0)