Skip to content

Commit 32724c5

Browse files
committed
Set default timeout to default window time and add debug stats
1 parent a4191e7 commit 32724c5

3 files changed

Lines changed: 54 additions & 14 deletions

File tree

src/NimBLEScan.cpp

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,14 @@ static const char* LOG_TAG = "NimBLEScan";
3333
static NimBLEScanCallbacks defaultScanCallbacks;
3434
static ble_npl_event dummySrTimerEvent;
3535

36+
# if MYNEWT_VAL(NIMBLE_CPP_LOG_LEVEL) >= 4
37+
static uint32_t srMinMs = UINT32_MAX;
38+
static uint32_t srMaxMs = 0;
39+
static uint32_t srTotalMs = 0;
40+
static uint32_t srCount = 0;
41+
static uint32_t lateSrCount = 0;
42+
# endif
43+
3644
# if MYNEWT_VAL(BLE_EXT_ADV)
3745
struct ble_gap_ext_disc_desc dummyDesc{.props = BLE_HCI_ADV_LEGACY_MASK,
3846
.data_status = BLE_GAP_EXT_ADV_DATA_STATUS_COMPLETE,
@@ -55,6 +63,32 @@ static ble_gap_disc_desc dummyDesc{
5563
extern "C" void ble_gap_rx_adv_report(ble_gap_disc_desc* desc);
5664
# endif
5765

66+
static void srStats(uint32_t ticks, uint16_t window) {
67+
# if MYNEWT_VAL(NIMBLE_CPP_LOG_LEVEL) >= 4
68+
uint32_t ms;
69+
ble_npl_time_ticks_to_ms(ticks, &ms);
70+
if (ms >= (window ? window : BLE_GAP_SCAN_FAST_WINDOW) * 5) {
71+
NIMBLE_LOGD(LOG_TAG, "Scan response time %" PRIu32 " outside scan window, not updating stats", ms);
72+
return;
73+
}
74+
75+
if (ms < srMinMs) {
76+
srMinMs = ms;
77+
}
78+
if (ms > srMaxMs) {
79+
srMaxMs = ms;
80+
}
81+
srTotalMs += ms;
82+
srCount++;
83+
# endif
84+
}
85+
86+
void srMissed() {
87+
# if MYNEWT_VAL(NIMBLE_CPP_LOG_LEVEL) >= 4
88+
lateSrCount++;
89+
# endif
90+
}
91+
5892
/**
5993
* @brief Sends dummy (null) scan response data to the scan event handler in order to
6094
* provide the scan result to the callbacks when a device hasn't responded to the
@@ -128,7 +162,7 @@ NimBLEScan::NimBLEScan()
128162
m_maxResults{0xFF} {
129163
ble_npl_callout_init(&m_srTimer, nimble_port_get_dflt_eventq(), NimBLEScan::srTimerCb, this);
130164
ble_npl_event_init(&dummySrTimerEvent, sendDummyScanResponse, NULL);
131-
ble_npl_time_ms_to_ticks(MYNEWT_VAL(NIMBLE_CPP_SCAN_RSP_TIMEOUT), &m_srTimeoutTicks);
165+
ble_npl_time_ms_to_ticks(BLE_GAP_SCAN_FAST_WINDOW, &m_srTimeoutTicks);
132166
} // NimBLEScan::NimBLEScan
133167

134168
/**
@@ -206,6 +240,7 @@ int NimBLEScan::handleGapEvent(ble_gap_event* event, void* arg) {
206240
}
207241

208242
if (isLegacyAdv && event_type == BLE_HCI_ADV_RPT_EVTYPE_SCAN_RSP) {
243+
srMissed();
209244
NIMBLE_LOGI(LOG_TAG, "Scan response without advertisement: %s", advertisedAddress.toString().c_str());
210245
}
211246

@@ -217,6 +252,7 @@ int NimBLEScan::handleGapEvent(ble_gap_event* event, void* arg) {
217252
advertisedDevice->update(event, event_type);
218253
if (isLegacyAdv) {
219254
if (event_type == BLE_HCI_ADV_RPT_EVTYPE_SCAN_RSP) {
255+
srStats(ble_npl_time_get() - advertisedDevice->m_time, pScan->m_scanParams.window * 625 / 1000);
220256
NIMBLE_LOGI(LOG_TAG, "Scan response from: %s", advertisedAddress.toString().c_str());
221257
} else {
222258
NIMBLE_LOGI(LOG_TAG, "Duplicate; updated: %s", advertisedAddress.toString().c_str());
@@ -270,7 +306,23 @@ int NimBLEScan::handleGapEvent(ble_gap_event* event, void* arg) {
270306
case BLE_GAP_EVENT_DISC_COMPLETE: {
271307
ble_npl_callout_stop(&pScan->m_srTimer);
272308
NIMBLE_LOGD(LOG_TAG, "discovery complete; reason=%d", event->disc_complete.reason);
273-
309+
# if MYNEWT_VAL(NIMBLE_CPP_LOG_LEVEL) >= 4
310+
if (srCount > 0) {
311+
NIMBLE_LOGD(LOG_TAG,
312+
"Scan response stats: count=%" PRIu32 ", min=%" PRIu32 " ms, max=%" PRIu32
313+
" ms, avg=%" PRIu32 " ms, missed=%" PRIu32,
314+
srCount,
315+
srMinMs,
316+
srMaxMs,
317+
srTotalMs / srCount,
318+
lateSrCount);
319+
}
320+
srCount = 0;
321+
srMinMs = UINT32_MAX;
322+
srMaxMs = 0;
323+
srTotalMs = 0;
324+
lateSrCount = 0;
325+
# endif
274326
pScan->m_pScanCallbacks->onScanEnd(pScan->m_scanResults, event->disc_complete.reason);
275327
if (pScan->m_maxResults == 0) {
276328
pScan->clearResults();

src/nimconfig.h

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -69,14 +69,6 @@
6969
/** @brief Un-comment to change the stack size for the NimBLE host task */
7070
// #define MYNEWT_VAL_NIMBLE_HOST_TASK_STACK_SIZE 4096
7171

72-
/**
73-
* @brief Un-comment to change the default scan response timeout, in milliseconds.
74-
* @details This is the time to wait for a scan response before calling the onResult scan callback,
75-
* lower values increase callback rates but will lose data more often, higher values give full data more
76-
* often. Setting this to 0 will disable the scan response timeout.
77-
*/
78-
// #define MYNEWT_VAL_NIMBLE_CPP_SCAN_RSP_TIMEOUT (200)
79-
8072
/**
8173
* @brief Un-comment to change the bit used to block tasks during BLE operations
8274
* that call NimBLEUtils::taskWait. This should be different than any other

src/syscfg/syscfg.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2023,8 +2023,4 @@
20232023
#define MYNEWT_VAL_NIMBLE_CPP_ENABLE_ADVERTISEMENT_TYPE_TEXT (0)
20242024
#endif
20252025

2026-
#ifndef MYNEWT_VAL_NIMBLE_CPP_SCAN_RSP_TIMEOUT
2027-
#define MYNEWT_VAL_NIMBLE_CPP_SCAN_RSP_TIMEOUT (30)
2028-
#endif
2029-
20302026
#endif /* H_SYSCFG_ */

0 commit comments

Comments
 (0)