Skip to content

Commit 11530a8

Browse files
committed
Call the onResult callback directly instead of creating an rx event
1 parent 359b47d commit 11530a8

4 files changed

Lines changed: 26 additions & 63 deletions

File tree

src/NimBLEAdvertisedDevice.cpp

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -58,40 +58,30 @@ NimBLEAdvertisedDevice::NimBLEAdvertisedDevice(const ble_gap_event* event, uint8
5858
* @brief Update the advertisement data.
5959
* @param [in] event The advertisement event data.
6060
*/
61-
bool NimBLEAdvertisedDevice::update(const ble_gap_event* event, uint8_t eventType) {
61+
void NimBLEAdvertisedDevice::update(const ble_gap_event* event, uint8_t eventType) {
6262
# if MYNEWT_VAL(BLE_EXT_ADV)
6363
const auto& disc = event->ext_disc;
64-
if (!disc.length_data) { // dummy sr, just return, don't update anything
65-
return false;
66-
}
67-
6864
if (m_dataStatus == BLE_GAP_EXT_ADV_DATA_STATUS_INCOMPLETE) {
6965
m_payload.reserve(m_advLength + disc.length_data);
7066
m_payload.insert(m_payload.end(), disc.data, disc.data + disc.length_data);
7167
m_dataStatus = disc.data_status;
7268
m_advLength = m_payload.size();
73-
return true;
69+
return;
7470
}
7571

7672
m_dataStatus = disc.data_status;
7773
m_isLegacyAdv = disc.props & BLE_HCI_ADV_LEGACY_MASK;
7874
# else
7975
const auto& disc = event->disc;
8076
# endif
81-
82-
if (!disc.length_data) { // dummy sr, just return, don't update anything
83-
return false;
84-
}
85-
8677
m_rssi = disc.rssi;
8778
if (eventType == BLE_HCI_ADV_RPT_EVTYPE_SCAN_RSP && isLegacyAdvertisement()) {
8879
m_payload.insert(m_payload.end(), disc.data, disc.data + disc.length_data);
89-
return true;
80+
return;
9081
}
9182
m_advLength = disc.length_data;
9283
m_payload.assign(disc.data, disc.data + disc.length_data);
9384
m_callbackSent = 0; // new data, reset callback sent flag
94-
return true;
9585
} // update
9686

9787
/**

src/NimBLEAdvertisedDevice.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ class NimBLEAdvertisedDevice {
154154
friend class NimBLEScan;
155155

156156
NimBLEAdvertisedDevice(const ble_gap_event* event, uint8_t eventType);
157-
bool update(const ble_gap_event* event, uint8_t eventType);
157+
void update(const ble_gap_event* event, uint8_t eventType);
158158
uint8_t findAdvField(uint8_t type, uint8_t index = 0, size_t* data_loc = nullptr) const;
159159
size_t findServiceData(uint8_t index, uint8_t* bytes) const;
160160

src/NimBLEScan.cpp

Lines changed: 18 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -31,49 +31,23 @@
3131

3232
static const char* LOG_TAG = "NimBLEScan";
3333
static 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
*/
7852
void 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) {

src/NimBLEScan.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ class NimBLEScan {
143143
srCount ? srTotalMs / srCount : 0,
144144
orphanedSrCount,
145145
missedSrCount);
146-
146+
out.resize(strlen(out.c_str()));
147147
return out;
148148
}
149149

@@ -153,7 +153,7 @@ class NimBLEScan {
153153
uint32_t ms;
154154
ble_npl_time_ticks_to_ms(ticks, &ms);
155155
if (ms >= (window ? window : BLE_GAP_SCAN_FAST_WINDOW) * 5) {
156-
return false;
156+
//return false;
157157
}
158158

159159
if (ms < srMinMs) {
@@ -182,14 +182,15 @@ class NimBLEScan {
182182
static int handleGapEvent(ble_gap_event* event, void* arg);
183183
void onHostSync();
184184
static void srTimerCb(ble_npl_event* event);
185-
static void sendDummyScanResponse(ble_npl_event* ev);
185+
static void forceResultCallback(ble_npl_event* ev);
186186

187187
NimBLEScanCallbacks* m_pScanCallbacks;
188188
ble_gap_disc_params m_scanParams;
189189
NimBLEScanResults m_scanResults;
190190
NimBLETaskData* m_pTaskData;
191191
ble_npl_callout m_srTimer{};
192192
ble_npl_time_t m_srTimeoutTicks{};
193+
ble_npl_event m_srTimeoutEvent{};
193194
uint8_t m_maxResults;
194195

195196
# if MYNEWT_VAL(BLE_EXT_ADV)

0 commit comments

Comments
 (0)