Skip to content

Commit e3797e7

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 30c612a commit e3797e7

File tree

2 files changed

+30
-5
lines changed

2 files changed

+30
-5
lines changed

src/NimBLEDevice.cpp

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -116,9 +116,11 @@ uint8_t NimBLEDevice::m_ownAddrType{BLE_OWN_ADDR_PUBLIC};
116116
# 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};
119120
# elif CONFIG_BT_LE_SCAN_DUPL
120-
uint16_t NimBLEDevice::m_scanDuplicateSize{CONFIG_BT_LE_LL_DUP_SCAN_LIST_COUNT};
121-
uint8_t NimBLEDevice::m_scanFilterMode{CONFIG_BT_LE_SCAN_DUPL_TYPE};
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};
122124
extern "C" int ble_vhci_disc_duplicate_set_max_cache_size(int max_cache_size);
123125
extern "C" int ble_vhci_disc_duplicate_set_period_refresh_time(int refresh_period_time);
124126
extern "C" int ble_vhci_disc_duplicate_mode_disable(int mode);
@@ -312,7 +314,26 @@ void NimBLEDevice::setScanFilterMode(uint8_t mode) {
312314

313315
m_scanFilterMode = mode;
314316
}
315-
# 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
316337
# endif // ESP_PLATFORM
317338
# endif // #if defined(CONFIG_BT_NIMBLE_ROLE_OBSERVER)
318339

@@ -908,8 +929,9 @@ bool NimBLEDevice::init(const std::string& deviceName) {
908929
# endif
909930

910931
# if CONFIG_BTDM_BLE_SCAN_DUPL
911-
bt_cfg.normal_adv_size = m_scanDuplicateSize;
912-
bt_cfg.scan_duplicate_type = m_scanFilterMode;
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;
913935
# elif CONFIG_BT_LE_SCAN_DUPL
914936
bt_cfg.ble_ll_rsp_dup_list_count = m_scanDuplicateSize;
915937
bt_cfg.ble_ll_adv_dup_list_count = m_scanDuplicateSize;
@@ -936,6 +958,7 @@ bool NimBLEDevice::init(const std::string& deviceName) {
936958
ble_vhci_disc_duplicate_mode_disable(0xFFFFFFFF);
937959
ble_vhci_disc_duplicate_mode_enable(mode);
938960
ble_vhci_disc_duplicate_set_max_cache_size(m_scanDuplicateSize);
961+
ble_vhci_disc_duplicate_set_period_refresh_time(m_scanDuplicateResetTime);
939962
# endif
940963

941964
err = esp_bt_controller_enable(ESP_BT_MODE_BLE);

src/NimBLEDevice.h

Lines changed: 2 additions & 0 deletions
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);
@@ -251,6 +252,7 @@ class NimBLEDevice {
251252
# 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

0 commit comments

Comments
 (0)