@@ -33,6 +33,14 @@ static const char* LOG_TAG = "NimBLEScan";
3333static NimBLEScanCallbacks defaultScanCallbacks;
3434static 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)
3745struct 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{
5563extern " 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 ();
0 commit comments