@@ -60,8 +60,9 @@ extern "C" void ble_gap_rx_adv_report(ble_gap_disc_desc* desc);
6060 * provide the scan result to the callbacks when a device hasn't responded to the
6161 * scan request in time. This is called by the host task from the default event queue.
6262 */
63- static void sendDummyScanResponse (ble_npl_event* ev) {
63+ void NimBLEScan:: sendDummyScanResponse (ble_npl_event* ev) {
6464 (void )ev;
65+ NimBLEDevice::getScan ()->m_stats .incMissedSrCount ();
6566# if MYNEWT_VAL(BLE_EXT_ADV)
6667 ble_gap_rx_ext_adv_report (&dummyDesc);
6768# else
@@ -128,7 +129,7 @@ NimBLEScan::NimBLEScan()
128129 m_maxResults{0xFF } {
129130 ble_npl_callout_init (&m_srTimer, nimble_port_get_dflt_eventq (), NimBLEScan::srTimerCb, this );
130131 ble_npl_event_init (&dummySrTimerEvent, sendDummyScanResponse, NULL );
131- ble_npl_time_ms_to_ticks (MYNEWT_VAL ( NIMBLE_CPP_SCAN_RSP_TIMEOUT ) , &m_srTimeoutTicks);
132+ ble_npl_time_ms_to_ticks (BLE_GAP_SCAN_FAST_WINDOW , &m_srTimeoutTicks);
132133} // NimBLEScan::NimBLEScan
133134
134135/* *
@@ -198,6 +199,8 @@ int NimBLEScan::handleGapEvent(ble_gap_event* event, void* arg) {
198199 // If we haven't seen this device before; create a new instance and insert it in the vector.
199200 // Otherwise just update the relevant parameters of the already known device.
200201 if (advertisedDevice == nullptr ) {
202+ pScan->m_stats .incDevCount ();
203+
201204 // Check if we have reach the scan results limit, ignore this one if so.
202205 // We still need to store each device when maxResults is 0 to be able to append the scan results
203206 if (pScan->m_maxResults > 0 && pScan->m_maxResults < 0xFF &&
@@ -206,6 +209,7 @@ int NimBLEScan::handleGapEvent(ble_gap_event* event, void* arg) {
206209 }
207210
208211 if (isLegacyAdv && event_type == BLE_HCI_ADV_RPT_EVTYPE_SCAN_RSP ) {
212+ pScan->m_stats .incOrphanedSrCount ();
209213 NIMBLE_LOGI (LOG_TAG , " Scan response without advertisement: %s" , advertisedAddress.toString ().c_str ());
210214 }
211215
@@ -218,7 +222,14 @@ int NimBLEScan::handleGapEvent(ble_gap_event* event, void* arg) {
218222 if (isLegacyAdv) {
219223 if (event_type == BLE_HCI_ADV_RPT_EVTYPE_SCAN_RSP ) {
220224 NIMBLE_LOGI (LOG_TAG , " Scan response from: %s" , advertisedAddress.toString ().c_str ());
225+ if (!pScan->m_stats .recordSrTime (ble_npl_time_get () - advertisedDevice->m_time ,
226+ pScan->m_scanParams .window * 625 / 1000 )) {
227+ NIMBLE_LOGD (LOG_TAG ,
228+ " Abnormal scan response time, stats ignored for device: %s" ,
229+ advertisedAddress.toString ().c_str ());
230+ }
221231 } else {
232+ pScan->m_stats .incDupCount ();
222233 NIMBLE_LOGI (LOG_TAG , " Duplicate; updated: %s" , advertisedAddress.toString ().c_str ());
223234 // Restart scan-response timeout when we see a new non-scan-response
224235 // legacy advertisement during active scanning for a scannable device.
@@ -269,6 +280,13 @@ int NimBLEScan::handleGapEvent(ble_gap_event* event, void* arg) {
269280
270281 case BLE_GAP_EVENT_DISC_COMPLETE : {
271282 ble_npl_callout_stop (&pScan->m_srTimer );
283+ for (const auto & dev : pScan->m_scanResults .m_deviceVec ) {
284+ if (dev->isScannable () && dev->m_callbackSent < 2 ) {
285+ pScan->m_stats .incMissedSrCount ();
286+ pScan->m_pScanCallbacks ->onResult (dev);
287+ }
288+ }
289+
272290 NIMBLE_LOGD (LOG_TAG , " discovery complete; reason=%d" , event->disc_complete .reason );
273291
274292 pScan->m_pScanCallbacks ->onScanEnd (pScan->m_scanResults , event->disc_complete .reason );
@@ -437,6 +455,8 @@ void NimBLEScan::setPeriod(uint32_t periodMs) {
437455 * @return True if scan started or false if there was an error.
438456 */
439457bool NimBLEScan::start (uint32_t duration, bool isContinue, bool restart) {
458+ m_stats.reset ();
459+
440460 NIMBLE_LOGD (LOG_TAG , " >> start: duration=%" PRIu32, duration);
441461 if (isScanning ()) {
442462 if (restart) {
0 commit comments