Skip to content

Commit b6717fd

Browse files
committed
[Bugfix] Extended advertisements not reporting full data.
Extended advertisement reports would be truncated incorrectly as the handler was not checking the data status. Correct advertisement length and set status on update.
1 parent a237bdd commit b6717fd

File tree

3 files changed

+37
-5
lines changed

3 files changed

+37
-5
lines changed

src/NimBLEAdvertisedDevice.cpp

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ NimBLEAdvertisedDevice::NimBLEAdvertisedDevice(const ble_gap_event* event, uint8
3838
m_callbackSent{0},
3939
m_advLength{event->ext_disc.length_data},
4040
m_isLegacyAdv{!!(event->ext_disc.props & BLE_HCI_ADV_LEGACY_MASK)},
41+
m_dataStatus{event->ext_disc.data_status},
4142
m_sid{event->ext_disc.sid},
4243
m_primPhy{event->ext_disc.prim_phy},
4344
m_secPhy{event->ext_disc.sec_phy},
@@ -60,7 +61,16 @@ NimBLEAdvertisedDevice::NimBLEAdvertisedDevice(const ble_gap_event* event, uint8
6061
void NimBLEAdvertisedDevice::update(const ble_gap_event* event, uint8_t eventType) {
6162
# if CONFIG_BT_NIMBLE_EXT_ADV
6263
const auto& disc = event->ext_disc;
63-
m_isLegacyAdv = disc.props & BLE_HCI_ADV_LEGACY_MASK;
64+
if (m_dataStatus == BLE_GAP_EXT_ADV_DATA_STATUS_INCOMPLETE) {
65+
m_payload.reserve(m_advLength + disc.length_data);
66+
m_payload.insert(m_payload.end(), disc.data, disc.data + disc.length_data);
67+
m_dataStatus = disc.data_status;
68+
m_advLength = m_payload.size();
69+
return;
70+
}
71+
72+
m_dataStatus = disc.data_status;
73+
m_isLegacyAdv = disc.props & BLE_HCI_ADV_LEGACY_MASK;
6474
# else
6575
const auto& disc = event->disc;
6676
# endif
@@ -617,6 +627,18 @@ uint8_t NimBLEAdvertisedDevice::getSecondaryPhy() const {
617627
uint16_t NimBLEAdvertisedDevice::getPeriodicInterval() const {
618628
return m_periodicItvl;
619629
} // getPeriodicInterval
630+
631+
/**
632+
* @brief Get the advertisement data status.
633+
* @return The advertisement data status.
634+
* One of:
635+
* * BLE_GAP_EXT_ADV_DATA_STATUS_COMPLETE (0) - Complete extended advertising data
636+
* * BLE_GAP_EXT_ADV_DATA_STATUS_INCOMPLETE (1) - Incomplete extended advertising data, more to come
637+
* * BLE_GAP_EXT_ADV_DATA_STATUS_TRUNCATED (2) - Incomplete extended advertising data, no more to come
638+
*/
639+
uint8_t NimBLEAdvertisedDevice::getDataStatus() const {
640+
return m_dataStatus;
641+
} // getDataStatus
620642
# endif
621643

622644
uint8_t NimBLEAdvertisedDevice::findAdvField(uint8_t type, uint8_t index, size_t* data_loc) const {

src/NimBLEAdvertisedDevice.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ class NimBLEAdvertisedDevice {
9292
uint8_t getPrimaryPhy() const;
9393
uint8_t getSecondaryPhy() const;
9494
uint16_t getPeriodicInterval() const;
95+
uint8_t getDataStatus() const;
9596
# endif
9697
operator NimBLEAddress() const;
9798

@@ -165,6 +166,7 @@ class NimBLEAdvertisedDevice {
165166

166167
# if CONFIG_BT_NIMBLE_EXT_ADV
167168
bool m_isLegacyAdv{};
169+
uint8_t m_dataStatus{};
168170
uint8_t m_sid{};
169171
uint8_t m_primPhy{};
170172
uint8_t m_secPhy{};

src/NimBLEScan.cpp

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -115,12 +115,20 @@ int NimBLEScan::handleGapEvent(ble_gap_event* event, void* arg) {
115115
advertisedDevice = new NimBLEAdvertisedDevice(event, event_type);
116116
pScan->m_scanResults.m_deviceVec.push_back(advertisedDevice);
117117
NIMBLE_LOGI(LOG_TAG, "New advertiser: %s", advertisedAddress.toString().c_str());
118+
#if CONFIG_BT_NIMBLE_EXT_ADV
119+
if (advertisedDevice->getDataStatus() == BLE_GAP_EXT_ADV_DATA_STATUS_INCOMPLETE) {
120+
NIMBLE_LOGI(LOG_TAG, " EXT ADV data incomplete, waiting for more...");
121+
return 0;
122+
}
123+
#endif
118124
} else {
119125
advertisedDevice->update(event, event_type);
120-
if (isLegacyAdv && event_type == BLE_HCI_ADV_RPT_EVTYPE_SCAN_RSP) {
121-
NIMBLE_LOGI(LOG_TAG, "Scan response from: %s", advertisedAddress.toString().c_str());
122-
} else {
123-
NIMBLE_LOGI(LOG_TAG, "Duplicate; updated: %s", advertisedAddress.toString().c_str());
126+
if (isLegacyAdv) {
127+
if (event_type == BLE_HCI_ADV_RPT_EVTYPE_SCAN_RSP) {
128+
NIMBLE_LOGI(LOG_TAG, "Scan response from: %s", advertisedAddress.toString().c_str());
129+
} else {
130+
NIMBLE_LOGI(LOG_TAG, "Duplicate; updated: %s", advertisedAddress.toString().c_str());
131+
}
124132
}
125133
}
126134

0 commit comments

Comments
 (0)