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+
2734static const char * LOG_TAG = " NimBLEScan" ;
2835static 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,110 @@ 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+ ble_npl_hw_enter_critical ();
116+ m_pWaitingListHead = pDev;
117+ m_pWaitingListTail = pDev;
118+ ble_npl_hw_exit_critical (0 );
119+ return ;
120+ }
121+
122+ ble_npl_hw_enter_critical ();
123+ m_pWaitingListTail->m_pNextWaiting = pDev;
124+ m_pWaitingListTail = pDev;
125+ ble_npl_hw_exit_critical (0 );
126+ }
127+
128+ /* *
129+ * @brief Remove a device from the waiting list.
130+ * @param [in] pDev The device to remove from the list.
131+ */
132+ void NimBLEScan::removeWaitingDevice (NimBLEAdvertisedDevice* pDev) {
133+ if (pDev == nullptr ) {
134+ return ;
135+ }
136+
137+ if (pDev->m_pNextWaiting == pDev) {
138+ return ; // Not in the list
139+ }
140+
141+ ble_npl_hw_enter_critical ();
142+ if (m_pWaitingListHead == pDev) {
143+ m_pWaitingListHead = pDev->m_pNextWaiting ;
144+ if (m_pWaitingListHead == nullptr ) {
145+ m_pWaitingListTail = nullptr ;
146+ } else {
147+ ble_npl_hw_exit_critical (0 );
148+ resetWaitingTimer ();
149+ }
150+ } else {
151+ NimBLEAdvertisedDevice* current = m_pWaitingListHead;
152+ while (current != nullptr ) {
153+ if (current->m_pNextWaiting == pDev) {
154+ current->m_pNextWaiting = pDev->m_pNextWaiting ;
155+ if (m_pWaitingListTail == pDev) {
156+ m_pWaitingListTail = current;
157+ }
158+ break ;
159+ }
160+ current = current->m_pNextWaiting ;
161+ }
162+ }
163+
164+ ble_npl_hw_exit_critical (0 );
165+ pDev->m_pNextWaiting = pDev; // Restore sentinel: self-pointer means "not in list"
166+ }
167+
168+ /* *
169+ * @brief Clear all devices from the waiting list.
170+ */
171+ void NimBLEScan::clearWaitingList () {
172+ // Stop the timer and remove any pending timeout events since we're clearing
173+ // the list and won't be processing any more timeouts for these devices
174+ ble_npl_callout_stop (&m_srTimer);
175+ ble_npl_eventq_remove (nimble_port_get_dflt_eventq (), &m_srTimeoutEvent);
176+
177+ ble_npl_hw_enter_critical ();
178+ NimBLEAdvertisedDevice* current = m_pWaitingListHead;
179+ while (current != nullptr ) {
180+ NimBLEAdvertisedDevice* next = current->m_pNextWaiting ;
181+ current->m_pNextWaiting = current; // Restore sentinel
182+ current = next;
183+ }
184+ m_pWaitingListHead = nullptr ;
185+ m_pWaitingListTail = nullptr ;
186+ ble_npl_hw_exit_critical (0 );
187+ }
188+
189+ /* *
190+ * @brief Reset the timer for the next waiting device at the head of the FIFO list.
191+ */
192+ void NimBLEScan::resetWaitingTimer () {
193+ if (m_srTimeoutTicks == 0 || m_pWaitingListHead == nullptr ) {
194+ ble_npl_callout_stop (&m_srTimer);
195+ return ;
196+ }
197+
198+ ble_npl_time_t now = ble_npl_time_get ();
199+ ble_npl_time_t elapsed = now - m_pWaitingListHead->m_time ;
200+ ble_npl_time_t nextTime = elapsed >= m_srTimeoutTicks ? 1 : m_srTimeoutTicks - elapsed;
201+ ble_npl_callout_reset (&m_srTimer, nextTime);
47202}
48203
49204/* *
@@ -101,6 +256,8 @@ int NimBLEScan::handleGapEvent(ble_gap_event* event, void* arg) {
101256 // If we haven't seen this device before; create a new instance and insert it in the vector.
102257 // Otherwise just update the relevant parameters of the already known device.
103258 if (advertisedDevice == nullptr ) {
259+ pScan->m_stats .incDevCount ();
260+
104261 // Check if we have reach the scan results limit, ignore this one if so.
105262 // We still need to store each device when maxResults is 0 to be able to append the scan results
106263 if (pScan->m_maxResults > 0 && pScan->m_maxResults < 0xFF &&
@@ -109,19 +266,34 @@ int NimBLEScan::handleGapEvent(ble_gap_event* event, void* arg) {
109266 }
110267
111268 if (isLegacyAdv && event_type == BLE_HCI_ADV_RPT_EVTYPE_SCAN_RSP) {
269+ pScan->m_stats .incOrphanedSrCount ();
112270 NIMBLE_LOGI (LOG_TAG, " Scan response without advertisement: %s" , advertisedAddress.toString ().c_str ());
113271 }
114272
115273 advertisedDevice = new NimBLEAdvertisedDevice (event, event_type);
116274 pScan->m_scanResults .m_deviceVec .push_back (advertisedDevice);
275+ advertisedDevice->m_time = ble_npl_time_get ();
117276 NIMBLE_LOGI (LOG_TAG, " New advertiser: %s" , advertisedAddress.toString ().c_str ());
118277 } else {
119278 advertisedDevice->update (event, event_type);
120279 if (isLegacyAdv) {
121280 if (event_type == BLE_HCI_ADV_RPT_EVTYPE_SCAN_RSP) {
281+ pScan->m_stats .recordSrTime (ble_npl_time_get () - advertisedDevice->m_time );
122282 NIMBLE_LOGI (LOG_TAG, " Scan response from: %s" , advertisedAddress.toString ().c_str ());
283+ // Remove device from waiting list since we got the response
284+ pScan->removeWaitingDevice (advertisedDevice);
123285 } else {
286+ pScan->m_stats .incDupCount ();
124287 NIMBLE_LOGI (LOG_TAG, " Duplicate; updated: %s" , advertisedAddress.toString ().c_str ());
288+ // Restart scan-response timeout when we see a new non-scan-response
289+ // legacy advertisement during active scanning for a scannable device.
290+ advertisedDevice->m_time = ble_npl_time_get ();
291+ advertisedDevice->m_callbackSent = 0 ;
292+ // Re-add to the tail so FIFO timeout order matches advertisement order.
293+ if (pScan->m_srTimeoutTicks && advertisedDevice->isScannable ()) {
294+ pScan->removeWaitingDevice (advertisedDevice);
295+ pScan->addWaitingDevice (advertisedDevice);
296+ }
125297 }
126298 }
127299 }
@@ -147,6 +319,12 @@ int NimBLEScan::handleGapEvent(ble_gap_event* event, void* arg) {
147319 advertisedDevice->m_callbackSent ++;
148320 // got the scan response report the full data.
149321 pScan->m_pScanCallbacks ->onResult (advertisedDevice);
322+ } else if (pScan->m_srTimeoutTicks && isLegacyAdv && advertisedDevice->isScannable ()) {
323+ // Add to waiting list for scan response and start the timer
324+ pScan->addWaitingDevice (advertisedDevice);
325+ if (pScan->m_pWaitingListHead == advertisedDevice) {
326+ pScan->resetWaitingTimer ();
327+ }
150328 }
151329
152330 // If not storing results and we have invoked the callback, delete the device.
@@ -158,14 +336,34 @@ int NimBLEScan::handleGapEvent(ble_gap_event* event, void* arg) {
158336 }
159337
160338 case BLE_GAP_EVENT_DISC_COMPLETE: {
339+ pScan->clearWaitingList ();
340+ // If we have any scannable devices that haven't received a scan response,
341+ // we should trigger the callback with whatever data we have since the scan is complete
342+ // and we won't be getting any more updates for these devices.
343+
344+ // Make a copy in case the callback modifies the vector (e.g. by calling clearResults)
345+ std::vector<NimBLEAdvertisedDevice*> pending{};
346+ pending.reserve (pScan->m_scanResults .m_deviceVec .size ());
347+ for (const auto & dev : pScan->m_scanResults .m_deviceVec ) {
348+ if (dev->isScannable () && dev->m_callbackSent < 2 ) {
349+ pScan->m_stats .incMissedSrCount ();
350+ dev->m_callbackSent = 2 ;
351+ pending.push_back (dev);
352+ }
353+ }
354+
355+ for (const auto & dev : pending) {
356+ pScan->m_pScanCallbacks ->onResult (dev);
357+ }
358+
161359 NIMBLE_LOGD (LOG_TAG, " discovery complete; reason=%d" , event->disc_complete .reason );
360+ NIMBLE_LOGD (LOG_TAG, " %s" , pScan->getStatsString ().c_str ());
162361
362+ pScan->m_pScanCallbacks ->onScanEnd (pScan->m_scanResults , event->disc_complete .reason );
163363 if (pScan->m_maxResults == 0 ) {
164364 pScan->clearResults ();
165365 }
166366
167- pScan->m_pScanCallbacks ->onScanEnd (pScan->m_scanResults , event->disc_complete .reason );
168-
169367 if (pScan->m_pTaskData != nullptr ) {
170368 NimBLEUtils::taskRelease (*pScan->m_pTaskData , event->disc_complete .reason );
171369 }
@@ -178,6 +376,25 @@ int NimBLEScan::handleGapEvent(ble_gap_event* event, void* arg) {
178376 }
179377} // handleGapEvent
180378
379+ /* *
380+ * @brief Set the scan response timeout.
381+ * @param [in] timeoutMs The timeout in milliseconds to wait for a scan response, default: max advertising interval (10.24s)
382+ * @details If a scan response is not received within the timeout period,
383+ * a dummy scan response with null data will be sent to the scan event handler
384+ * which will trigger the callback with whatever data was in the advertisement.
385+ * If set to 0, no dummy scan response will be sent and the callback will only
386+ * be triggered when a scan response is received from the advertiser or when the scan completes.
387+ */
388+ void NimBLEScan::setScanResponseTimeout (uint32_t timeoutMs) {
389+ if (timeoutMs == 0 ) {
390+ ble_npl_callout_stop (&m_srTimer);
391+ m_srTimeoutTicks = 0 ;
392+ return ;
393+ }
394+
395+ ble_npl_time_ms_to_ticks (timeoutMs, &m_srTimeoutTicks);
396+ } // setScanResponseTimeout
397+
181398/* *
182399 * @brief Should we perform an active or passive scan?
183400 * The default is a passive scan. An active scan means that we will request a scan response.
@@ -323,11 +540,13 @@ bool NimBLEScan::start(uint32_t duration, bool isContinue, bool restart) {
323540
324541 if (!isContinue) {
325542 clearResults ();
543+ m_stats.reset ();
326544 }
327545 }
328546 } else { // Don't clear results while scanning is active
329547 if (!isContinue) {
330548 clearResults ();
549+ m_stats.reset ();
331550 }
332551 }
333552
@@ -394,6 +613,8 @@ bool NimBLEScan::stop() {
394613 return false ;
395614 }
396615
616+ clearWaitingList ();
617+
397618 if (m_maxResults == 0 ) {
398619 clearResults ();
399620 }
@@ -414,6 +635,7 @@ void NimBLEScan::erase(const NimBLEAddress& address) {
414635 NIMBLE_LOGD (LOG_TAG, " erase device: %s" , address.toString ().c_str ());
415636 for (auto it = m_scanResults.m_deviceVec .begin (); it != m_scanResults.m_deviceVec .end (); ++it) {
416637 if ((*it)->getAddress () == address) {
638+ removeWaitingDevice (*it);
417639 delete *it;
418640 m_scanResults.m_deviceVec .erase (it);
419641 break ;
@@ -429,6 +651,7 @@ void NimBLEScan::erase(const NimBLEAdvertisedDevice* device) {
429651 NIMBLE_LOGD (LOG_TAG, " erase device: %s" , device->getAddress ().toString ().c_str ());
430652 for (auto it = m_scanResults.m_deviceVec .begin (); it != m_scanResults.m_deviceVec .end (); ++it) {
431653 if ((*it) == device) {
654+ removeWaitingDevice (*it);
432655 delete *it;
433656 m_scanResults.m_deviceVec .erase (it);
434657 break ;
@@ -483,6 +706,7 @@ NimBLEScanResults NimBLEScan::getResults() {
483706 * @brief Clear the stored results of the scan.
484707 */
485708void NimBLEScan::clearResults () {
709+ clearWaitingList ();
486710 if (m_scanResults.m_deviceVec .size ()) {
487711 std::vector<NimBLEAdvertisedDevice*> vSwap{};
488712 ble_npl_hw_enter_critical ();
0 commit comments