3131
3232static const char * LOG_TAG = " NimBLEScan" ;
3333static NimBLEScanCallbacks defaultScanCallbacks;
34- static ble_npl_event dummySrTimerEvent;
35-
36- # if MYNEWT_VAL(BLE_EXT_ADV)
37- struct ble_gap_ext_disc_desc dummyDesc{.props = BLE_HCI_ADV_LEGACY_MASK ,
38- .data_status = BLE_GAP_EXT_ADV_DATA_STATUS_COMPLETE ,
39- .legacy_event_type = BLE_HCI_ADV_RPT_EVTYPE_SCAN_RSP ,
40- .addr{},
41- .rssi = 127 ,
42- .tx_power = 127 ,
43- .sid = 0 ,
44- .prim_phy = BLE_HCI_LE_PHY_1M ,
45- .sec_phy = BLE_HCI_LE_PHY_1M ,
46- .periodic_adv_itvl = 0 ,
47- .length_data = 0 ,
48- .data = nullptr ,
49- .direct_addr{}};
50-
51- extern " C" void ble_gap_rx_ext_adv_report (struct ble_gap_ext_disc_desc * desc);
52- # else
53- static ble_gap_disc_desc dummyDesc{
54- .event_type = BLE_HCI_ADV_RPT_EVTYPE_SCAN_RSP , .length_data = 0 , .addr {}, .rssi = 127 , .data = nullptr , .direct_addr {}};
55- extern " C" void ble_gap_rx_adv_report (ble_gap_disc_desc* desc);
56- # endif
5734
5835/* *
59- * @brief Sends dummy (null) scan response data to the scan event handler in order to
60- * provide the scan result to the callbacks when a device hasn't responded to the
61- * scan request in time. This is called by the host task from the default event queue.
62- */
63- void NimBLEScan::sendDummyScanResponse (ble_npl_event* ev) {
64- (void )ev;
65- NimBLEDevice::getScan ()->m_stats .incMissedSrCount ();
66- # if MYNEWT_VAL(BLE_EXT_ADV)
67- ble_gap_rx_ext_adv_report (&dummyDesc);
68- # else
69- ble_gap_rx_adv_report (&dummyDesc);
70- # endif
36+ * @brief Calls the onResult callback with the given device,
37+ * this is used to provide a scan result to the callbacks when a device hasn't responded to
38+ * the scan request in time. This is called by the host task from the default event queue.
39+ */
40+ void NimBLEScan::forceResultCallback (ble_npl_event* ev) {
41+ auto pScan = NimBLEDevice::getScan ();
42+ pScan->m_stats .incMissedSrCount ();
43+ NimBLEAdvertisedDevice* pDev = static_cast <NimBLEAdvertisedDevice*>(ble_npl_event_get_arg (ev));
44+ pDev->m_callbackSent = 2 ;
45+ pScan->m_pScanCallbacks ->onResult (pDev);
7146}
7247
7348/* *
74- * @brief This will schedule an event to run in the host task that will call sendDummyScanResponse
75- * which will send a null data scan response to the scan event handler if the device
76- * hasn't responded to a scan response request within the timeout period.
49+ * @brief This will schedule an event to run in the host task that will call forceResultCallback
50+ * which will call the onResult callback with the current data.
7751 */
7852void NimBLEScan::srTimerCb (ble_npl_event* event) {
7953 NimBLEScan* pScan = static_cast <NimBLEScan*>(ble_npl_event_get_arg (event));
@@ -97,12 +71,9 @@ void NimBLEScan::srTimerCb(ble_npl_event* event) {
9771
9872 // Add the event to the host queue
9973 if (curDev && now - curDev->m_time >= pScan->m_srTimeoutTicks ) {
100- # if MYNEWT_VAL(BLE_EXT_ADV)
101- dummyDesc.sid = curDev->getSetId ();
102- # endif
103- memcpy (&dummyDesc.addr , curDev->getAddress ().getBase (), sizeof (dummyDesc.addr ));
10474 NIMBLE_LOGI (LOG_TAG , " Scan response timeout for: %s" , curDev->getAddress ().toString ().c_str ());
105- ble_npl_eventq_put (nimble_port_get_dflt_eventq (), &dummySrTimerEvent);
75+ ble_npl_event_set_arg (&pScan->m_srTimeoutEvent , curDev);
76+ ble_npl_eventq_put (nimble_port_get_dflt_eventq (), &pScan->m_srTimeoutEvent );
10677 }
10778
10879 // Restart the timer for the next device that we are expecting a scan response from
@@ -128,7 +99,7 @@ NimBLEScan::NimBLEScan()
12899 m_pTaskData{nullptr },
129100 m_maxResults{0xFF } {
130101 ble_npl_callout_init (&m_srTimer, nimble_port_get_dflt_eventq (), NimBLEScan::srTimerCb, this );
131- ble_npl_event_init (&dummySrTimerEvent, sendDummyScanResponse , NULL );
102+ ble_npl_event_init (&m_srTimeoutEvent, forceResultCallback , NULL );
132103 ble_npl_time_ms_to_ticks (BLE_GAP_SCAN_FAST_WINDOW , &m_srTimeoutTicks);
133104} // NimBLEScan::NimBLEScan
134105
@@ -141,7 +112,7 @@ NimBLEScan::~NimBLEScan() {
141112 }
142113
143114 ble_npl_callout_deinit (&m_srTimer);
144- ble_npl_event_deinit (&dummySrTimerEvent );
115+ ble_npl_event_deinit (&m_srTimeoutEvent );
145116}
146117
147118/* *
@@ -223,7 +194,7 @@ int NimBLEScan::handleGapEvent(ble_gap_event* event, void* arg) {
223194 if (event_type == BLE_HCI_ADV_RPT_EVTYPE_SCAN_RSP ) {
224195 NIMBLE_LOGI (LOG_TAG , " Scan response from: %s" , advertisedAddress.toString ().c_str ());
225196 if (!pScan->m_stats .recordSrTime (ble_npl_time_get () - advertisedDevice->m_time ,
226- pScan->m_scanParams .window * 625 / 1000 )) {
197+ pScan->m_scanParams .window * 625 / 1000 )) {
227198 NIMBLE_LOGD (LOG_TAG ,
228199 " Abnormal scan response time, stats ignored for device: %s" ,
229200 advertisedAddress.toString ().c_str ());
@@ -288,6 +259,7 @@ int NimBLEScan::handleGapEvent(ble_gap_event* event, void* arg) {
288259 }
289260
290261 NIMBLE_LOGD (LOG_TAG , " discovery complete; reason=%d" , event->disc_complete .reason );
262+ NIMBLE_LOGD (LOG_TAG , " %s" , pScan->getStatsString ().c_str ());
291263
292264 pScan->m_pScanCallbacks ->onScanEnd (pScan->m_scanResults , event->disc_complete .reason );
293265 if (pScan->m_maxResults == 0 ) {
0 commit comments