Skip to content

Commit 7c05721

Browse files
authored
Fix for issue #4158: BLEAdvertising - Crash with stack trace originating in Bluedroid (#4182)
* Fix for issue #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 f57c367 commit 7c05721

File tree

2 files changed

+15
-7
lines changed

2 files changed

+15
-7
lines changed

Diff for: libraries/BLE/src/BLEAdvertising.cpp

+12-5
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+
memcpy(&m_scanRespData, &m_advData, sizeof(esp_ble_adv_data_t)); // 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;

Diff for: libraries/BLE/src/BLEAdvertising.h

+3-2
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class BLEAdvertisementData {
3030
void setPartialServices(BLEUUID uuid);
3131
void setServiceData(BLEUUID uuid, std::string data);
3232
void setShortName(std::string name);
33-
void addData(std::string data); // Add data to the payload.
33+
void addData(std::string data); // Add data to the payload.
3434
std::string getPayload(); // Retrieve the current advert payload.
3535

3636
private:
@@ -68,12 +68,13 @@ 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?
7475
bool m_customScanResponseData = false; // Are we using custom scan response data?
7576
FreeRTOS::Semaphore m_semaphoreSetAdv = FreeRTOS::Semaphore("startAdvert");
76-
bool m_scanResp = true;
77+
bool m_scanResp = true;
7778

7879
};
7980
#endif /* CONFIG_BT_ENABLED */

0 commit comments

Comments
 (0)