Skip to content

Commit b5a2e8c

Browse files
committed
Fix for issue espressif#4158: Crash with stack trace originating in Bluedroid
Improved configuration of scan response data in 'BLEAdvertising' avoids the crash: - Added member variable 'm_scanRespData' to configure scan response differently from advertising data - Initialization of 'm_scanRespData' in BLEAdvertising constructor - Use of 'm_scanRespData' within BLEAdvertising::start() to configure the scan response - 'Flags' and 'Appearance' are cleared in the scan response data - With this fix, device names of up to 29 characters can be used without causing a crash.
1 parent b92c58d commit b5a2e8c

File tree

2 files changed

+13
-5
lines changed

2 files changed

+13
-5
lines changed

libraries/BLE/src/BLEAdvertising.cpp

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@
2828
* @brief Construct a default advertising object.
2929
*
3030
*/
31-
BLEAdvertising::BLEAdvertising() {
31+
BLEAdvertising::BLEAdvertising()
32+
: m_scanRespData{}
33+
{
3234
m_advData.set_scan_rsp = false;
3335
m_advData.include_name = true;
3436
m_advData.include_txpower = true;
@@ -215,10 +217,15 @@ void BLEAdvertising::start() {
215217
}
216218

217219
if (!m_customScanResponseData && m_scanResp) {
218-
m_advData.set_scan_rsp = true;
219-
m_advData.include_name = m_scanResp;
220-
m_advData.include_txpower = m_scanResp;
221-
errRc = ::esp_ble_gap_config_adv_data(&m_advData);
220+
// Set the configuration for scan response.
221+
m_scanRespData = m_advData; // Copy the content of m_advData.
222+
m_scanRespData.set_scan_rsp = true; // Define this struct as scan response data
223+
m_scanRespData.include_name = true; // Caution: This may lead to a crash if the device name has more than 29 characters
224+
m_scanRespData.include_txpower = true;
225+
m_scanRespData.appearance = 0; // If defined the 'Appearance' attribute is already included in the advertising data
226+
m_scanRespData.flag = 0; // 'Flags' attribute should no be included in the scan response
227+
228+
errRc = ::esp_ble_gap_config_adv_data(&m_scanRespData);
222229
if (errRc != ESP_OK) {
223230
log_e("<< esp_ble_gap_config_adv_data (Scan response): rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
224231
return;

libraries/BLE/src/BLEAdvertising.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ class BLEAdvertising {
6868

6969
private:
7070
esp_ble_adv_data_t m_advData;
71+
esp_ble_adv_data_t m_scanRespData; // Used for configuration of scan response data when m_scanResp is true
7172
esp_ble_adv_params_t m_advParams;
7273
std::vector<BLEUUID> m_serviceUUIDs;
7374
bool m_customAdvData = false; // Are we using custom advertising data?

0 commit comments

Comments
 (0)