22
22
# include " NimBLEDevice.h"
23
23
# include " NimBLELog.h"
24
24
25
+ # if defined(CONFIG_NIMBLE_CPP_IDF)
26
+ # include " nimble/nimble_port.h"
27
+ # else
28
+ # include " nimble/porting/nimble/include/nimble/nimble_port.h"
29
+ # endif
30
+
25
31
# include < string>
26
32
# include < climits>
27
33
34
+ # define SR_TIMEOUT CONFIG_NIMBLE_CPP_SCAN_RSP_TIMEOUT
35
+
28
36
static const char * LOG_TAG = " NimBLEScan" ;
29
37
static NimBLEScanCallbacks defaultScanCallbacks;
30
38
39
+ # if SR_TIMEOUT
40
+ static ble_npl_event dummySrTimerEvent;
41
+ static ble_gap_disc_desc dummyDesc{
42
+ .event_type = BLE_HCI_ADV_RPT_EVTYPE_SCAN_RSP, .length_data = 0 , .addr {0 }, .rssi = 127 , .data = nullptr , .direct_addr {0 }};
43
+
44
+ extern " C" void ble_gap_rx_adv_report (ble_gap_disc_desc* desc);
45
+
46
+ /* *
47
+ * @brief Sends dummy(null) scan response data to the scan event handler in order to
48
+ * provide the scan result to the callbacks when a device hasn't responded to the
49
+ * scan request in time. This is called by the host task from the default event queue.
50
+ */
51
+ static void sendDummyScanResponse (ble_npl_event* ev) {
52
+ (void )ev;
53
+ ble_gap_rx_adv_report (&dummyDesc);
54
+ }
55
+
56
+ /* *
57
+ * @brief This will schedule an event in to run in the host task that will send
58
+ * dummy (null) data to the scan event handler as a scan response if the device
59
+ * hasn't responded to a scan response request within the timeout period.
60
+ */
61
+ void NimBLEScan::srTimerCb (ble_npl_event* event) {
62
+ NimBLEScan* pScan = (NimBLEScan*)ble_npl_event_get_arg (event);
63
+ NimBLEAdvertisedDevice* curDev = nullptr ;
64
+ NimBLEAdvertisedDevice* nextDev = nullptr ;
65
+ ble_npl_time_t now = ble_npl_time_get ();
66
+
67
+ for (auto & dev : pScan->m_scanResults .m_deviceVec ) {
68
+ if (dev->m_callbackSent < 2 && dev->isScannable ()) {
69
+ if (!curDev || (now - dev->m_time > now - curDev->m_time )) {
70
+ nextDev = curDev;
71
+ curDev = dev;
72
+ continue ;
73
+ }
74
+
75
+ if (!nextDev || now - dev->m_time > now - nextDev->m_time ) {
76
+ nextDev = dev;
77
+ }
78
+ }
79
+ }
80
+
81
+ if (curDev) {
82
+ memcpy (&dummyDesc.addr , curDev->getAddress ().getBase (), sizeof (dummyDesc.addr ));
83
+ NIMBLE_LOGI (LOG_TAG, " Scan response timeout for: %s" , curDev->getAddress ().toString ().c_str ());
84
+ ble_npl_eventq_put (nimble_port_get_dflt_eventq (), &dummySrTimerEvent);
85
+ }
86
+
87
+ // Restart timer if there are more devices waiting for scan responses.
88
+ if (nextDev) {
89
+ auto nextTime = now - nextDev->m_time ;
90
+ if (nextTime >= SR_TIMEOUT) {
91
+ nextTime = 1 ;
92
+ } else {
93
+ nextTime = SR_TIMEOUT - nextTime;
94
+ }
95
+ ble_npl_time_t ticks;
96
+ ble_npl_time_ms_to_ticks (nextTime, &ticks);
97
+ ble_npl_callout_reset (&pScan->m_srTimer , ticks);
98
+ }
99
+ }
100
+ # endif
101
+
31
102
/* *
32
103
* @brief Scan constructor.
33
104
*/
@@ -36,13 +107,22 @@ NimBLEScan::NimBLEScan()
36
107
// default interval + window, no whitelist scan filter,not limited scan, no scan response, filter_duplicates
37
108
m_scanParams{0 , 0 , BLE_HCI_SCAN_FILT_NO_WL, 0 , 1 , 1 },
38
109
m_pTaskData{nullptr },
39
- m_maxResults{0xFF } {}
110
+ m_maxResults{0xFF } {
111
+ # if SR_TIMEOUT
112
+ ble_npl_callout_init (&m_srTimer, nimble_port_get_dflt_eventq (), NimBLEScan::srTimerCb, this );
113
+ ble_npl_event_init (&dummySrTimerEvent, sendDummyScanResponse, NULL );
114
+ # endif
115
+ }
40
116
41
117
/* *
42
118
* @brief Scan destructor, release any allocated resources.
43
119
*/
44
120
NimBLEScan::~NimBLEScan () {
45
121
clearResults ();
122
+ # if SR_TIMEOUT
123
+ ble_npl_callout_deinit (&m_srTimer);
124
+ ble_npl_event_deinit (&dummySrTimerEvent);
125
+ # endif
46
126
}
47
127
48
128
/* *
@@ -109,6 +189,9 @@ int NimBLEScan::handleGapEvent(ble_gap_event* event, void* arg) {
109
189
advertisedDevice = new NimBLEAdvertisedDevice (event, event_type);
110
190
pScan->m_scanResults .m_deviceVec .push_back (advertisedDevice);
111
191
NIMBLE_LOGI (LOG_TAG, " New advertiser: %s" , advertisedAddress.toString ().c_str ());
192
+ # if SR_TIMEOUT
193
+ advertisedDevice->m_time = ble_npl_time_get ();
194
+ # endif
112
195
} else {
113
196
advertisedDevice->update (event, event_type);
114
197
if (isLegacyAdv && event_type == BLE_HCI_ADV_RPT_EVTYPE_SCAN_RSP) {
@@ -132,6 +215,13 @@ int NimBLEScan::handleGapEvent(ble_gap_event* event, void* arg) {
132
215
advertisedDevice->m_callbackSent ++;
133
216
// got the scan response report the full data.
134
217
pScan->m_pScanCallbacks ->onResult (advertisedDevice);
218
+ # if SR_TIMEOUT
219
+ } else if (isLegacyAdv && !ble_npl_callout_is_active (&pScan->m_srTimer )) {
220
+ // Start the timer to wait for the scan response.
221
+ ble_npl_time_t ticks;
222
+ ble_npl_time_ms_to_ticks (SR_TIMEOUT, &ticks);
223
+ ble_npl_callout_reset (&pScan->m_srTimer , ticks);
224
+ # endif
135
225
}
136
226
137
227
// If not storing results and we have invoked the callback, delete the device.
@@ -144,13 +234,15 @@ int NimBLEScan::handleGapEvent(ble_gap_event* event, void* arg) {
144
234
145
235
case BLE_GAP_EVENT_DISC_COMPLETE: {
146
236
NIMBLE_LOGD (LOG_TAG, " discovery complete; reason=%d" , event->disc_complete .reason );
237
+ # if SR_TIMEOUT
238
+ ble_npl_callout_stop (&pScan->m_srTimer );
239
+ # endif
240
+ pScan->m_pScanCallbacks ->onScanEnd (pScan->m_scanResults , event->disc_complete .reason );
147
241
148
242
if (pScan->m_maxResults == 0 ) {
149
243
pScan->clearResults ();
150
244
}
151
245
152
- pScan->m_pScanCallbacks ->onScanEnd (pScan->m_scanResults , event->disc_complete .reason );
153
-
154
246
if (pScan->m_pTaskData != nullptr ) {
155
247
NimBLEUtils::taskRelease (*pScan->m_pTaskData , event->disc_complete .reason );
156
248
}
@@ -379,6 +471,10 @@ bool NimBLEScan::stop() {
379
471
return false ;
380
472
}
381
473
474
+ # if SR_TIMEOUT
475
+ ble_npl_callout_stop (&m_srTimer);
476
+ # endif
477
+
382
478
if (m_maxResults == 0 ) {
383
479
clearResults ();
384
480
}
@@ -483,11 +579,11 @@ void NimBLEScan::clearResults() {
483
579
* @brief Dump the scan results to the log.
484
580
*/
485
581
void NimBLEScanResults::dump () const {
486
- #if CONFIG_NIMBLE_CPP_LOG_LEVEL >=3
582
+ # if CONFIG_NIMBLE_CPP_LOG_LEVEL >= 3
487
583
for (const auto & dev : m_deviceVec) {
488
584
NIMBLE_LOGI (LOG_TAG, " - %s" , dev->toString ().c_str ());
489
585
}
490
- #endif
586
+ # endif
491
587
} // dump
492
588
493
589
/* *
0 commit comments