Skip to content

Add a timer for scan responses#947

Open
h2zero wants to merge 1 commit intomasterfrom
scan-response-timer
Open

Add a timer for scan responses#947
h2zero wants to merge 1 commit intomasterfrom
scan-response-timer

Conversation

@h2zero
Copy link
Owner

@h2zero h2zero commented Apr 25, 2025

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.

Closes #591

@h2zero h2zero force-pushed the scan-response-timer branch 4 times, most recently from 64bae68 to f47363e Compare April 26, 2025 00:38
@h2zero h2zero marked this pull request as ready for review April 26, 2025 00:40
@h2zero h2zero force-pushed the scan-response-timer branch 3 times, most recently from 96d0ef0 to b8438ac Compare May 1, 2025 01:03
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.
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds an optional (and default-enabled) scan-response timeout to ensure active scanning still produces onResult callbacks even when a peripheral never sends a scan response.

Changes:

  • Introduces CONFIG_NIMBLE_CPP_SCAN_RSP_TIMEOUT configuration (default 30ms, 0 to disable).
  • Adds a host-queue callout timer to trigger a “dummy scan response” path when the timeout elapses.
  • Extends scan state tracking by storing a per-device timestamp used by the timeout logic.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 7 comments.

File Description
src/nimconfig.h Adds user-facing config and default for scan response timeout.
src/NimBLEScan.h Adds scan-response timeout timer callback declaration and callout member.
src/NimBLEScan.cpp Implements timer logic and enqueues a dummy scan response event to complete results.
src/NimBLEAdvertisedDevice.h Adds per-device timestamp storage used for timeout scheduling.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

You can also share your feedback on Copilot code review. Take the survey.

Comment on lines +41 to +42
static ble_gap_disc_desc dummyDesc{
.event_type = BLE_HCI_ADV_RPT_EVTYPE_SCAN_RSP, .length_data = 0, .addr{}, .rssi = 127, .data = nullptr, .direct_addr{}};
Comment on lines +41 to +54
static ble_gap_disc_desc dummyDesc{
.event_type = BLE_HCI_ADV_RPT_EVTYPE_SCAN_RSP, .length_data = 0, .addr{}, .rssi = 127, .data = nullptr, .direct_addr{}};

extern "C" void ble_gap_rx_adv_report(ble_gap_disc_desc* desc);

/**
* @brief Sends dummy (null) scan response data to the scan event handler in order to
* provide the scan result to the callbacks when a device hasn't responded to the
* scan request in time. This is called by the host task from the default event queue.
*/
static void sendDummyScanResponse(ble_npl_event* ev) {
(void)ev;
ble_gap_rx_adv_report(&dummyDesc);
}
# if SR_TIMEOUT
static ble_npl_event dummySrTimerEvent;
static ble_gap_disc_desc dummyDesc{
.event_type = BLE_HCI_ADV_RPT_EVTYPE_SCAN_RSP, .length_data = 0, .addr{}, .rssi = 127, .data = nullptr, .direct_addr{}};
Comment on lines +65 to +76
ble_npl_time_t now = ble_npl_time_get();

for (auto& dev : pScan->m_scanResults.m_deviceVec) {
if (dev->m_callbackSent < 2 && dev->isScannable()) {
if (!curDev || (now - dev->m_time > now - curDev->m_time)) {
nextDev = curDev;
curDev = dev;
continue;
}

if (!nextDev || now - dev->m_time > now - nextDev->m_time) {
nextDev = dev;
Comment on lines +90 to +99
auto nextTime = now - nextDev->m_time;
if (nextTime >= SR_TIMEOUT) {
nextTime = 1;
} else {
nextTime = SR_TIMEOUT - nextTime;
}

ble_npl_time_t ticks;
ble_npl_time_ms_to_ticks(nextTime, &ticks);
ble_npl_callout_reset(&pScan->m_srTimer, ticks);
advertisedDevice->m_time = ble_npl_time_get();
# endif
} else {
advertisedDevice->update(event, event_type);
Comment on lines 216 to +231
// If not active scanning or scan response is not available
// or extended advertisement scanning, report the result to the callback now.
if (pScan->m_scanParams.passive || !isLegacyAdv || !advertisedDevice->isScannable()) {
advertisedDevice->m_callbackSent++;
pScan->m_pScanCallbacks->onResult(advertisedDevice);
} else if (isLegacyAdv && event_type == BLE_HCI_ADV_RPT_EVTYPE_SCAN_RSP) {
advertisedDevice->m_callbackSent++;
// got the scan response report the full data.
pScan->m_pScanCallbacks->onResult(advertisedDevice);
# if SR_TIMEOUT
} else if (isLegacyAdv && !ble_npl_callout_is_active(&pScan->m_srTimer)) {
// Start the timer to wait for the scan response.
ble_npl_time_t ticks;
ble_npl_time_ms_to_ticks(SR_TIMEOUT, &ticks);
ble_npl_callout_reset(&pScan->m_srTimer, ticks);
# endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

NimBLE_Scan_Continuous.ino possible heap memory leak?

2 participants