Skip to content

Commit a4191e7

Browse files
committed
Add setScanResponseTimeout method
1 parent 0504a9e commit a4191e7

2 files changed

Lines changed: 29 additions & 12 deletions

File tree

src/NimBLEScan.cpp

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@
3131

3232
static const char* LOG_TAG = "NimBLEScan";
3333
static NimBLEScanCallbacks defaultScanCallbacks;
34-
static ble_npl_event dummySrTimerEvent;
34+
static ble_npl_event dummySrTimerEvent;
3535

36-
# if MYNEWT_VAL(BLE_EXT_ADV)
36+
# if MYNEWT_VAL(BLE_EXT_ADV)
3737
struct ble_gap_ext_disc_desc dummyDesc{.props = BLE_HCI_ADV_LEGACY_MASK,
3838
.data_status = BLE_GAP_EXT_ADV_DATA_STATUS_COMPLETE,
3939
.legacy_event_type = BLE_HCI_ADV_RPT_EVTYPE_SCAN_RSP,
@@ -49,11 +49,11 @@ struct ble_gap_ext_disc_desc dummyDesc{.props = BLE_HCI_ADV_LEGACY_M
4949
.direct_addr{}};
5050

5151
extern "C" void ble_gap_rx_ext_adv_report(struct ble_gap_ext_disc_desc* desc);
52-
# else
52+
# else
5353
static ble_gap_disc_desc dummyDesc{
5454
.event_type = BLE_HCI_ADV_RPT_EVTYPE_SCAN_RSP, .length_data = 0, .addr{}, .rssi = 127, .data = nullptr, .direct_addr{}};
5555
extern "C" void ble_gap_rx_adv_report(ble_gap_disc_desc* desc);
56-
# endif
56+
# endif
5757

5858
/**
5959
* @brief Sends dummy (null) scan response data to the scan event handler in order to
@@ -62,11 +62,11 @@ extern "C" void ble_gap_rx_adv_report(ble_gap_disc_desc* desc);
6262
*/
6363
static void sendDummyScanResponse(ble_npl_event* ev) {
6464
(void)ev;
65-
# if MYNEWT_VAL(BLE_EXT_ADV)
65+
# if MYNEWT_VAL(BLE_EXT_ADV)
6666
ble_gap_rx_ext_adv_report(&dummyDesc);
67-
# else
67+
# else
6868
ble_gap_rx_adv_report(&dummyDesc);
69-
# endif
69+
# endif
7070
}
7171

7272
/**
@@ -96,9 +96,9 @@ void NimBLEScan::srTimerCb(ble_npl_event* event) {
9696

9797
// Add the event to the host queue
9898
if (curDev && now - curDev->m_time >= pScan->m_srTimeoutTicks) {
99-
# if MYNEWT_VAL(BLE_EXT_ADV)
99+
# if MYNEWT_VAL(BLE_EXT_ADV)
100100
dummyDesc.sid = curDev->getSetId();
101-
# endif
101+
# endif
102102
memcpy(&dummyDesc.addr, curDev->getAddress().getBase(), sizeof(dummyDesc.addr));
103103
NIMBLE_LOGI(LOG_TAG, "Scan response timeout for: %s", curDev->getAddress().toString().c_str());
104104
ble_npl_eventq_put(nimble_port_get_dflt_eventq(), &dummySrTimerEvent);
@@ -222,9 +222,10 @@ int NimBLEScan::handleGapEvent(ble_gap_event* event, void* arg) {
222222
NIMBLE_LOGI(LOG_TAG, "Duplicate; updated: %s", advertisedAddress.toString().c_str());
223223
// Restart scan-response timeout when we see a new non-scan-response
224224
// legacy advertisement during active scanning for a scannable device.
225-
advertisedDevice->m_time = ble_npl_time_get();
225+
advertisedDevice->m_time = ble_npl_time_get();
226226
advertisedDevice->m_callbackSent = 0;
227-
if (advertisedDevice->isScannable() && !ble_npl_callout_is_active(&pScan->m_srTimer)) {
227+
if (pScan->m_srTimeoutTicks && advertisedDevice->isScannable() &&
228+
!ble_npl_callout_is_active(&pScan->m_srTimer)) {
228229
ble_npl_callout_reset(&pScan->m_srTimer, pScan->m_srTimeoutTicks);
229230
}
230231
}
@@ -252,7 +253,8 @@ int NimBLEScan::handleGapEvent(ble_gap_event* event, void* arg) {
252253
advertisedDevice->m_callbackSent++;
253254
// got the scan response report the full data.
254255
pScan->m_pScanCallbacks->onResult(advertisedDevice);
255-
} else if (isLegacyAdv && advertisedDevice->isScannable() && !ble_npl_callout_is_active(&pScan->m_srTimer)) {
256+
} else if (pScan->m_srTimeoutTicks && isLegacyAdv && advertisedDevice->isScannable() &&
257+
!ble_npl_callout_is_active(&pScan->m_srTimer)) {
256258
// Start the timer to wait for the scan response.
257259
ble_npl_callout_reset(&pScan->m_srTimer, pScan->m_srTimeoutTicks);
258260
}
@@ -286,6 +288,20 @@ int NimBLEScan::handleGapEvent(ble_gap_event* event, void* arg) {
286288
}
287289
} // handleGapEvent
288290

291+
/**
292+
* @brief Set the scan response timeout.
293+
* @param [in] timeoutMs The timeout in milliseconds to wait for a scan response.
294+
*/
295+
void NimBLEScan::setScanResponseTimeout(uint32_t timeoutMs) {
296+
if (timeoutMs == 0) {
297+
ble_npl_callout_stop(&m_srTimer);
298+
m_srTimeoutTicks = 0;
299+
return;
300+
}
301+
302+
ble_npl_time_ms_to_ticks(timeoutMs, &m_srTimeoutTicks);
303+
} // setScanResponseTimeout
304+
289305
/**
290306
* @brief Should we perform an active or passive scan?
291307
* The default is a passive scan. An active scan means that we will request a scan response.

src/NimBLEScan.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ class NimBLEScan {
8282
void setMaxResults(uint8_t maxResults);
8383
void erase(const NimBLEAddress& address);
8484
void erase(const NimBLEAdvertisedDevice* device);
85+
void setScanResponseTimeout(uint32_t timeoutMs);
8586

8687
# if MYNEWT_VAL(BLE_EXT_ADV)
8788
enum Phy { SCAN_1M = 0x01, SCAN_CODED = 0x02, SCAN_ALL = 0x03 };

0 commit comments

Comments
 (0)