Skip to content

Commit 84f557f

Browse files
committed
Add a timer for scan responses
This ensures that the callback will be called within the configured time (in ms) when devices fail to respond to a scan response request within that time. * Adds stats when debug logging to help tune the scan response timeout/scan parameters.
1 parent b5110a2 commit 84f557f

4 files changed

Lines changed: 318 additions & 16 deletions

File tree

src/NimBLEAdvertisedDevice.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ NimBLEAdvertisedDevice::NimBLEAdvertisedDevice(const ble_gap_event* event, uint8
5252
m_advLength{event->disc.length_data},
5353
m_payload(event->disc.data, event->disc.data + event->disc.length_data) {
5454
# endif
55+
m_pNextWaiting = this; // initialize sentinel: self-pointer means "not in list"
5556
} // NimBLEAdvertisedDevice
5657

5758
/**

src/NimBLEAdvertisedDevice.h

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -158,11 +158,13 @@ class NimBLEAdvertisedDevice {
158158
uint8_t findAdvField(uint8_t type, uint8_t index = 0, size_t* data_loc = nullptr) const;
159159
size_t findServiceData(uint8_t index, uint8_t* bytes) const;
160160

161-
NimBLEAddress m_address{};
162-
uint8_t m_advType{};
163-
int8_t m_rssi{};
164-
uint8_t m_callbackSent{};
165-
uint16_t m_advLength{};
161+
NimBLEAddress m_address{};
162+
uint8_t m_advType{};
163+
int8_t m_rssi{};
164+
uint8_t m_callbackSent{};
165+
uint16_t m_advLength{};
166+
ble_npl_time_t m_time{};
167+
NimBLEAdvertisedDevice* m_pNextWaiting{}; // intrusive list node; self-pointer means "not in list", set in ctor
166168

167169
# if MYNEWT_VAL(BLE_EXT_ADV)
168170
bool m_isLegacyAdv{};

src/NimBLEScan.cpp

Lines changed: 208 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,60 @@
2020

2121
# include "NimBLEDevice.h"
2222
# include "NimBLELog.h"
23+
# if defined(CONFIG_NIMBLE_CPP_IDF)
24+
# include "nimble/nimble_port.h"
25+
# else
26+
# include "nimble/porting/nimble/include/nimble/nimble_port.h"
27+
# endif
2328

2429
# include <string>
2530
# include <climits>
2631

32+
# define DEFAULT_SCAN_RESP_TIMEOUT_MS 10240 // max advertising interval (10.24s)
33+
2734
static const char* LOG_TAG = "NimBLEScan";
2835
static NimBLEScanCallbacks defaultScanCallbacks;
2936

37+
/**
38+
* @brief Calls the onResult callback with the given device,
39+
* this is used to provide a scan result to the callbacks when a device hasn't responded to
40+
* the scan request in time. This is called by the host task from the default event queue.
41+
*/
42+
void NimBLEScan::forceResultCallback(ble_npl_event* ev) {
43+
auto pScan = NimBLEDevice::getScan();
44+
pScan->m_stats.incMissedSrCount();
45+
NimBLEAdvertisedDevice* pDev = static_cast<NimBLEAdvertisedDevice*>(ble_npl_event_get_arg(ev));
46+
pDev->m_callbackSent = 2;
47+
pScan->m_pScanCallbacks->onResult(pDev);
48+
if (pScan->m_maxResults == 0) {
49+
pScan->erase(pDev);
50+
}
51+
}
52+
53+
/**
54+
* @brief This will schedule an event to run in the host task that will call forceResultCallback
55+
* which will call the onResult callback with the current data.
56+
*/
57+
void NimBLEScan::srTimerCb(ble_npl_event* event) {
58+
auto* pScan = NimBLEDevice::getScan();
59+
auto curDev = pScan->m_pWaitingListHead;
60+
61+
if (curDev == nullptr) {
62+
ble_npl_callout_stop(&pScan->m_srTimer);
63+
return;
64+
}
65+
66+
NIMBLE_LOGI(LOG_TAG, "Scan response timeout for: %s", curDev->getAddress().toString().c_str());
67+
68+
// Schedule callback for this device
69+
ble_npl_event_set_arg(&pScan->m_srTimeoutEvent, curDev);
70+
ble_npl_eventq_put(nimble_port_get_dflt_eventq(), &pScan->m_srTimeoutEvent);
71+
72+
// Remove this device from the waiting list and reset the timer for the next one (if any)
73+
pScan->removeWaitingDevice(curDev);
74+
pScan->resetWaitingTimer();
75+
}
76+
3077
/**
3178
* @brief Scan constructor.
3279
*/
@@ -35,7 +82,11 @@ NimBLEScan::NimBLEScan()
3582
// default interval + window, no whitelist scan filter,not limited scan, no scan response, filter_duplicates
3683
m_scanParams{0, 0, BLE_HCI_SCAN_FILT_NO_WL, 0, 1, 1},
3784
m_pTaskData{nullptr},
38-
m_maxResults{0xFF} {}
85+
m_maxResults{0xFF} {
86+
ble_npl_callout_init(&m_srTimer, nimble_port_get_dflt_eventq(), NimBLEScan::srTimerCb, nullptr);
87+
ble_npl_event_init(&m_srTimeoutEvent, forceResultCallback, NULL);
88+
ble_npl_time_ms_to_ticks(DEFAULT_SCAN_RESP_TIMEOUT_MS, &m_srTimeoutTicks);
89+
} // NimBLEScan::NimBLEScan
3990

4091
/**
4192
* @brief Scan destructor, release any allocated resources.
@@ -44,6 +95,99 @@ NimBLEScan::~NimBLEScan() {
4495
for (const auto& dev : m_scanResults.m_deviceVec) {
4596
delete dev;
4697
}
98+
99+
ble_npl_callout_deinit(&m_srTimer);
100+
ble_npl_eventq_remove(nimble_port_get_dflt_eventq(), &m_srTimeoutEvent);
101+
ble_npl_event_deinit(&m_srTimeoutEvent);
102+
}
103+
104+
/**
105+
* @brief Add a device to the waiting list for scan responses.
106+
* @param [in] pDev The device to add to the list.
107+
*/
108+
void NimBLEScan::addWaitingDevice(NimBLEAdvertisedDevice* pDev) {
109+
if (pDev == nullptr || pDev->m_pNextWaiting != pDev) {
110+
return; // Invalid or already in list (self-pointer is the "not in list" sentinel)
111+
}
112+
113+
pDev->m_pNextWaiting = nullptr;
114+
if (m_pWaitingListTail == nullptr) {
115+
m_pWaitingListHead = pDev;
116+
m_pWaitingListTail = pDev;
117+
return;
118+
}
119+
120+
m_pWaitingListTail->m_pNextWaiting = pDev;
121+
m_pWaitingListTail = pDev;
122+
}
123+
124+
/**
125+
* @brief Remove a device from the waiting list.
126+
* @param [in] pDev The device to remove from the list.
127+
*/
128+
void NimBLEScan::removeWaitingDevice(NimBLEAdvertisedDevice* pDev) {
129+
if (pDev == nullptr) {
130+
return;
131+
}
132+
133+
if (pDev->m_pNextWaiting == pDev) {
134+
return; // Not in the list
135+
}
136+
137+
if (m_pWaitingListHead == pDev) {
138+
m_pWaitingListHead = pDev->m_pNextWaiting;
139+
if (m_pWaitingListHead == nullptr) {
140+
m_pWaitingListTail = nullptr;
141+
}
142+
} else {
143+
NimBLEAdvertisedDevice* current = m_pWaitingListHead;
144+
while (current != nullptr) {
145+
if (current->m_pNextWaiting == pDev) {
146+
current->m_pNextWaiting = pDev->m_pNextWaiting;
147+
if (m_pWaitingListTail == pDev) {
148+
m_pWaitingListTail = current;
149+
}
150+
break;
151+
}
152+
current = current->m_pNextWaiting;
153+
}
154+
}
155+
156+
pDev->m_pNextWaiting = pDev; // Restore sentinel: self-pointer means "not in list"
157+
}
158+
159+
/**
160+
* @brief Clear all devices from the waiting list.
161+
*/
162+
void NimBLEScan::clearWaitingList() {
163+
// Stop the timer and remove any pending timeout events since we're clearing
164+
// the list and won't be processing any more timeouts for these devices
165+
ble_npl_callout_stop(&m_srTimer);
166+
ble_npl_eventq_remove(nimble_port_get_dflt_eventq(), &m_srTimeoutEvent);
167+
168+
NimBLEAdvertisedDevice* current = m_pWaitingListHead;
169+
while (current != nullptr) {
170+
NimBLEAdvertisedDevice* next = current->m_pNextWaiting;
171+
current->m_pNextWaiting = current; // Restore sentinel
172+
current = next;
173+
}
174+
m_pWaitingListHead = nullptr;
175+
m_pWaitingListTail = nullptr;
176+
}
177+
178+
/**
179+
* @brief Reset the timer for the next waiting device at the head of the FIFO list.
180+
*/
181+
void NimBLEScan::resetWaitingTimer() {
182+
if (m_srTimeoutTicks == 0 || m_pWaitingListHead == nullptr) {
183+
ble_npl_callout_stop(&m_srTimer);
184+
return;
185+
}
186+
187+
ble_npl_time_t now = ble_npl_time_get();
188+
ble_npl_time_t elapsed = now - m_pWaitingListHead->m_time;
189+
ble_npl_time_t nextTime = elapsed >= m_srTimeoutTicks ? 1 : m_srTimeoutTicks - elapsed;
190+
ble_npl_callout_reset(&m_srTimer, nextTime);
47191
}
48192

49193
/**
@@ -101,6 +245,8 @@ int NimBLEScan::handleGapEvent(ble_gap_event* event, void* arg) {
101245
// If we haven't seen this device before; create a new instance and insert it in the vector.
102246
// Otherwise just update the relevant parameters of the already known device.
103247
if (advertisedDevice == nullptr) {
248+
pScan->m_stats.incDevCount();
249+
104250
// Check if we have reach the scan results limit, ignore this one if so.
105251
// We still need to store each device when maxResults is 0 to be able to append the scan results
106252
if (pScan->m_maxResults > 0 && pScan->m_maxResults < 0xFF &&
@@ -109,19 +255,38 @@ int NimBLEScan::handleGapEvent(ble_gap_event* event, void* arg) {
109255
}
110256

111257
if (isLegacyAdv && event_type == BLE_HCI_ADV_RPT_EVTYPE_SCAN_RSP) {
258+
pScan->m_stats.incOrphanedSrCount();
112259
NIMBLE_LOGI(LOG_TAG, "Scan response without advertisement: %s", advertisedAddress.toString().c_str());
113260
}
114261

115262
advertisedDevice = new NimBLEAdvertisedDevice(event, event_type);
116263
pScan->m_scanResults.m_deviceVec.push_back(advertisedDevice);
264+
advertisedDevice->m_time = ble_npl_time_get();
117265
NIMBLE_LOGI(LOG_TAG, "New advertiser: %s", advertisedAddress.toString().c_str());
118266
} else {
119267
advertisedDevice->update(event, event_type);
120268
if (isLegacyAdv) {
121269
if (event_type == BLE_HCI_ADV_RPT_EVTYPE_SCAN_RSP) {
270+
pScan->m_stats.recordSrTime(ble_npl_time_get() - advertisedDevice->m_time);
122271
NIMBLE_LOGI(LOG_TAG, "Scan response from: %s", advertisedAddress.toString().c_str());
272+
// Remove device from waiting list since we got the response
273+
pScan->removeWaitingDevice(advertisedDevice);
123274
} else {
275+
pScan->m_stats.incDupCount();
124276
NIMBLE_LOGI(LOG_TAG, "Duplicate; updated: %s", advertisedAddress.toString().c_str());
277+
// Restart scan-response timeout when we see a new non-scan-response
278+
// legacy advertisement during active scanning for a scannable device.
279+
advertisedDevice->m_time = ble_npl_time_get();
280+
advertisedDevice->m_callbackSent = 0;
281+
// Re-add to the tail so FIFO timeout order matches advertisement order.
282+
if (pScan->m_srTimeoutTicks && advertisedDevice->isScannable()) {
283+
bool wasHead = pScan->m_pWaitingListHead == advertisedDevice;
284+
pScan->removeWaitingDevice(advertisedDevice);
285+
pScan->addWaitingDevice(advertisedDevice);
286+
if (wasHead) {
287+
pScan->resetWaitingTimer();
288+
}
289+
}
125290
}
126291
}
127292
}
@@ -147,6 +312,12 @@ int NimBLEScan::handleGapEvent(ble_gap_event* event, void* arg) {
147312
advertisedDevice->m_callbackSent++;
148313
// got the scan response report the full data.
149314
pScan->m_pScanCallbacks->onResult(advertisedDevice);
315+
} else if (pScan->m_srTimeoutTicks && isLegacyAdv && advertisedDevice->isScannable()) {
316+
// Add to waiting list for scan response and start the timer
317+
pScan->addWaitingDevice(advertisedDevice);
318+
if (pScan->m_pWaitingListHead == advertisedDevice) {
319+
pScan->resetWaitingTimer();
320+
}
150321
}
151322

152323
// If not storing results and we have invoked the callback, delete the device.
@@ -158,14 +329,22 @@ int NimBLEScan::handleGapEvent(ble_gap_event* event, void* arg) {
158329
}
159330

160331
case BLE_GAP_EVENT_DISC_COMPLETE: {
332+
pScan->clearWaitingList();
333+
for (const auto& dev : pScan->m_scanResults.m_deviceVec) {
334+
if (dev->isScannable() && dev->m_callbackSent < 2) {
335+
pScan->m_stats.incMissedSrCount();
336+
pScan->m_pScanCallbacks->onResult(dev);
337+
}
338+
}
339+
161340
NIMBLE_LOGD(LOG_TAG, "discovery complete; reason=%d", event->disc_complete.reason);
341+
NIMBLE_LOGD(LOG_TAG, "%s", pScan->getStatsString().c_str());
162342

343+
pScan->m_pScanCallbacks->onScanEnd(pScan->m_scanResults, event->disc_complete.reason);
163344
if (pScan->m_maxResults == 0) {
164345
pScan->clearResults();
165346
}
166347

167-
pScan->m_pScanCallbacks->onScanEnd(pScan->m_scanResults, event->disc_complete.reason);
168-
169348
if (pScan->m_pTaskData != nullptr) {
170349
NimBLEUtils::taskRelease(*pScan->m_pTaskData, event->disc_complete.reason);
171350
}
@@ -178,6 +357,25 @@ int NimBLEScan::handleGapEvent(ble_gap_event* event, void* arg) {
178357
}
179358
} // handleGapEvent
180359

360+
/**
361+
* @brief Set the scan response timeout.
362+
* @param [in] timeoutMs The timeout in milliseconds to wait for a scan response, default: max advertising interval (10.24s)
363+
* @details If a scan response is not received within the timeout period,
364+
* a dummy scan response with null data will be sent to the scan event handler
365+
* which will trigger the callback with whatever data was in the advertisement.
366+
* If set to 0, no dummy scan response will be sent and the callback will only
367+
* be triggered when a scan response is received from the advertiser or when the scan completes.
368+
*/
369+
void NimBLEScan::setScanResponseTimeout(uint32_t timeoutMs) {
370+
if (timeoutMs == 0) {
371+
ble_npl_callout_stop(&m_srTimer);
372+
m_srTimeoutTicks = 0;
373+
return;
374+
}
375+
376+
ble_npl_time_ms_to_ticks(timeoutMs, &m_srTimeoutTicks);
377+
} // setScanResponseTimeout
378+
181379
/**
182380
* @brief Should we perform an active or passive scan?
183381
* The default is a passive scan. An active scan means that we will request a scan response.
@@ -323,11 +521,13 @@ bool NimBLEScan::start(uint32_t duration, bool isContinue, bool restart) {
323521

324522
if (!isContinue) {
325523
clearResults();
524+
m_stats.reset();
326525
}
327526
}
328527
} else { // Don't clear results while scanning is active
329528
if (!isContinue) {
330529
clearResults();
530+
m_stats.reset();
331531
}
332532
}
333533

@@ -394,6 +594,8 @@ bool NimBLEScan::stop() {
394594
return false;
395595
}
396596

597+
clearWaitingList();
598+
397599
if (m_maxResults == 0) {
398600
clearResults();
399601
}
@@ -414,6 +616,7 @@ void NimBLEScan::erase(const NimBLEAddress& address) {
414616
NIMBLE_LOGD(LOG_TAG, "erase device: %s", address.toString().c_str());
415617
for (auto it = m_scanResults.m_deviceVec.begin(); it != m_scanResults.m_deviceVec.end(); ++it) {
416618
if ((*it)->getAddress() == address) {
619+
removeWaitingDevice(*it);
417620
delete *it;
418621
m_scanResults.m_deviceVec.erase(it);
419622
break;
@@ -429,6 +632,7 @@ void NimBLEScan::erase(const NimBLEAdvertisedDevice* device) {
429632
NIMBLE_LOGD(LOG_TAG, "erase device: %s", device->getAddress().toString().c_str());
430633
for (auto it = m_scanResults.m_deviceVec.begin(); it != m_scanResults.m_deviceVec.end(); ++it) {
431634
if ((*it) == device) {
635+
removeWaitingDevice(*it);
432636
delete *it;
433637
m_scanResults.m_deviceVec.erase(it);
434638
break;
@@ -483,6 +687,7 @@ NimBLEScanResults NimBLEScan::getResults() {
483687
* @brief Clear the stored results of the scan.
484688
*/
485689
void NimBLEScan::clearResults() {
690+
clearWaitingList();
486691
if (m_scanResults.m_deviceVec.size()) {
487692
std::vector<NimBLEAdvertisedDevice*> vSwap{};
488693
ble_npl_hw_enter_critical();

0 commit comments

Comments
 (0)