From b5a2e8c21fb24ff1f04e66e4d957ac80e8117c6b Mon Sep 17 00:00:00 2001 From: Ernst Sikora Date: Sat, 18 Jul 2020 14:34:48 +0200 Subject: [PATCH 1/3] 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. --- libraries/BLE/src/BLEAdvertising.cpp | 17 ++++++++++++----- libraries/BLE/src/BLEAdvertising.h | 1 + 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/libraries/BLE/src/BLEAdvertising.cpp b/libraries/BLE/src/BLEAdvertising.cpp index 3d86a5072a6..70d4435a303 100644 --- a/libraries/BLE/src/BLEAdvertising.cpp +++ b/libraries/BLE/src/BLEAdvertising.cpp @@ -28,7 +28,9 @@ * @brief Construct a default advertising object. * */ -BLEAdvertising::BLEAdvertising() { +BLEAdvertising::BLEAdvertising() +: m_scanRespData{} +{ m_advData.set_scan_rsp = false; m_advData.include_name = true; m_advData.include_txpower = true; @@ -215,10 +217,15 @@ void BLEAdvertising::start() { } if (!m_customScanResponseData && m_scanResp) { - m_advData.set_scan_rsp = true; - m_advData.include_name = m_scanResp; - m_advData.include_txpower = m_scanResp; - errRc = ::esp_ble_gap_config_adv_data(&m_advData); + // Set the configuration for scan response. + m_scanRespData = m_advData; // Copy the content of m_advData. + m_scanRespData.set_scan_rsp = true; // Define this struct as scan response data + m_scanRespData.include_name = true; // Caution: This may lead to a crash if the device name has more than 29 characters + m_scanRespData.include_txpower = true; + m_scanRespData.appearance = 0; // If defined the 'Appearance' attribute is already included in the advertising data + m_scanRespData.flag = 0; // 'Flags' attribute should no be included in the scan response + + errRc = ::esp_ble_gap_config_adv_data(&m_scanRespData); if (errRc != ESP_OK) { log_e("<< esp_ble_gap_config_adv_data (Scan response): rc=%d %s", errRc, GeneralUtils::errorToString(errRc)); return; diff --git a/libraries/BLE/src/BLEAdvertising.h b/libraries/BLE/src/BLEAdvertising.h index 94bed945094..675ef643601 100644 --- a/libraries/BLE/src/BLEAdvertising.h +++ b/libraries/BLE/src/BLEAdvertising.h @@ -68,6 +68,7 @@ class BLEAdvertising { private: esp_ble_adv_data_t m_advData; + esp_ble_adv_data_t m_scanRespData; // Used for configuration of scan response data when m_scanResp is true esp_ble_adv_params_t m_advParams; std::vector m_serviceUUIDs; bool m_customAdvData = false; // Are we using custom advertising data? From 7a7a96ec5ac2d8168d1197c8896e2ba678d629d9 Mon Sep 17 00:00:00 2001 From: Me No Dev Date: Mon, 2 Nov 2020 19:22:45 +0200 Subject: [PATCH 2/3] tabs to spaces in the header --- libraries/BLE/src/BLEAdvertising.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/libraries/BLE/src/BLEAdvertising.h b/libraries/BLE/src/BLEAdvertising.h index 675ef643601..f1f0c66498f 100644 --- a/libraries/BLE/src/BLEAdvertising.h +++ b/libraries/BLE/src/BLEAdvertising.h @@ -30,7 +30,7 @@ class BLEAdvertisementData { void setPartialServices(BLEUUID uuid); void setServiceData(BLEUUID uuid, std::string data); void setShortName(std::string name); - void addData(std::string data); // Add data to the payload. + void addData(std::string data); // Add data to the payload. std::string getPayload(); // Retrieve the current advert payload. private: @@ -68,13 +68,13 @@ class BLEAdvertising { private: esp_ble_adv_data_t m_advData; - esp_ble_adv_data_t m_scanRespData; // Used for configuration of scan response data when m_scanResp is true + esp_ble_adv_data_t m_scanRespData; // Used for configuration of scan response data when m_scanResp is true esp_ble_adv_params_t m_advParams; std::vector m_serviceUUIDs; bool m_customAdvData = false; // Are we using custom advertising data? bool m_customScanResponseData = false; // Are we using custom scan response data? FreeRTOS::Semaphore m_semaphoreSetAdv = FreeRTOS::Semaphore("startAdvert"); - bool m_scanResp = true; + bool m_scanResp = true; }; #endif /* CONFIG_BT_ENABLED */ From 140b179aed8e81b1a60a100a139248635aff5297 Mon Sep 17 00:00:00 2001 From: Me No Dev Date: Mon, 2 Nov 2020 19:25:35 +0200 Subject: [PATCH 3/3] properly copy esp_ble_adv_data_t --- libraries/BLE/src/BLEAdvertising.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/BLE/src/BLEAdvertising.cpp b/libraries/BLE/src/BLEAdvertising.cpp index 70d4435a303..58f371ed981 100644 --- a/libraries/BLE/src/BLEAdvertising.cpp +++ b/libraries/BLE/src/BLEAdvertising.cpp @@ -218,7 +218,7 @@ void BLEAdvertising::start() { if (!m_customScanResponseData && m_scanResp) { // Set the configuration for scan response. - m_scanRespData = m_advData; // Copy the content of m_advData. + memcpy(&m_scanRespData, &m_advData, sizeof(esp_ble_adv_data_t)); // Copy the content of m_advData. m_scanRespData.set_scan_rsp = true; // Define this struct as scan response data m_scanRespData.include_name = true; // Caution: This may lead to a crash if the device name has more than 29 characters m_scanRespData.include_txpower = true;