From 3965e9044197d859a905e784c4f517d8a3d88865 Mon Sep 17 00:00:00 2001 From: Rodrigo Garcia Date: Mon, 27 Feb 2023 22:18:12 -0300 Subject: [PATCH 1/5] Fixes Maximum BLE Device Length fixes IDF esp_ble_gap_set_device_name() that allows name length bigger than Advertsing payload space. --- libraries/BLE/src/BLEDevice.cpp | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/libraries/BLE/src/BLEDevice.cpp b/libraries/BLE/src/BLEDevice.cpp index 473f3ef032e..e26673d602c 100644 --- a/libraries/BLE/src/BLEDevice.cpp +++ b/libraries/BLE/src/BLEDevice.cpp @@ -410,8 +410,14 @@ gatts_event_handler BLEDevice::m_customGattsHandler = nullptr; return; } #endif // CONFIG_GATTS_ENABLE - - errRc = ::esp_ble_gap_set_device_name(deviceName.c_str()); + + // makes sure it fails with any device name that has more than the possible advertising payload length + if (deviceName.length() > ESP_BLE_ADV_DATA_LEN_MAX - 2) { // 1 byte for Length + 1 bytes for ID + deviceName = "bad length name: max 29 bytes"; + errRC = ESP_ERR_INVALID_ARG; + } else + errRc = ::esp_ble_gap_set_device_name(deviceName.c_str()); + } if (errRc != ESP_OK) { log_e("esp_ble_gap_set_device_name: rc=%d %s", errRc, GeneralUtils::errorToString(errRc)); return; From 1680bf3450cbea91631fd4543d74df6d77d5289a Mon Sep 17 00:00:00 2001 From: Rodrigo Garcia Date: Mon, 27 Feb 2023 22:28:28 -0300 Subject: [PATCH 2/5] Minor typo --- libraries/BLE/src/BLEDevice.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/BLE/src/BLEDevice.cpp b/libraries/BLE/src/BLEDevice.cpp index e26673d602c..14bd9eb1c2f 100644 --- a/libraries/BLE/src/BLEDevice.cpp +++ b/libraries/BLE/src/BLEDevice.cpp @@ -414,7 +414,7 @@ gatts_event_handler BLEDevice::m_customGattsHandler = nullptr; // makes sure it fails with any device name that has more than the possible advertising payload length if (deviceName.length() > ESP_BLE_ADV_DATA_LEN_MAX - 2) { // 1 byte for Length + 1 bytes for ID deviceName = "bad length name: max 29 bytes"; - errRC = ESP_ERR_INVALID_ARG; + errRc = ESP_ERR_INVALID_ARG; } else errRc = ::esp_ble_gap_set_device_name(deviceName.c_str()); } From bcbdee203a67633fdb7e32ce3e71a6e9acdada72 Mon Sep 17 00:00:00 2001 From: Rodrigo Garcia Date: Mon, 27 Feb 2023 22:38:42 -0300 Subject: [PATCH 3/5] typo "{" --- libraries/BLE/src/BLEDevice.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/BLE/src/BLEDevice.cpp b/libraries/BLE/src/BLEDevice.cpp index 14bd9eb1c2f..4ecb5d799b6 100644 --- a/libraries/BLE/src/BLEDevice.cpp +++ b/libraries/BLE/src/BLEDevice.cpp @@ -415,7 +415,7 @@ gatts_event_handler BLEDevice::m_customGattsHandler = nullptr; if (deviceName.length() > ESP_BLE_ADV_DATA_LEN_MAX - 2) { // 1 byte for Length + 1 bytes for ID deviceName = "bad length name: max 29 bytes"; errRc = ESP_ERR_INVALID_ARG; - } else + } else { errRc = ::esp_ble_gap_set_device_name(deviceName.c_str()); } if (errRc != ESP_OK) { From 6ca24562a1853d6cf1bc60b8ab8ff09d22c7bcae Mon Sep 17 00:00:00 2001 From: Rodrigo Garcia Date: Mon, 27 Feb 2023 22:50:18 -0300 Subject: [PATCH 4/5] Changes init() to return a boolean --- libraries/BLE/src/BLEDevice.cpp | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/libraries/BLE/src/BLEDevice.cpp b/libraries/BLE/src/BLEDevice.cpp index 4ecb5d799b6..2be68b5bcf1 100644 --- a/libraries/BLE/src/BLEDevice.cpp +++ b/libraries/BLE/src/BLEDevice.cpp @@ -330,7 +330,7 @@ gatts_event_handler BLEDevice::m_customGattsHandler = nullptr; * @brief Initialize the %BLE environment. * @param deviceName The device name of the device. */ -/* STATIC */ void BLEDevice::init(std::string deviceName) { +/* STATIC */ bool BLEDevice::init(std::string deviceName) { if(!initialized){ initialized = true; // Set the initialization flag to ensure we are only initialized once. @@ -338,13 +338,13 @@ gatts_event_handler BLEDevice::m_customGattsHandler = nullptr; #ifdef ARDUINO_ARCH_ESP32 if (!btStart()) { errRc = ESP_FAIL; - return; + return false; } #else errRc = ::nvs_flash_init(); if (errRc != ESP_OK) { log_e("nvs_flash_init: rc=%d %s", errRc, GeneralUtils::errorToString(errRc)); - return; + return false; } #ifndef CONFIG_BT_CLASSIC_ENABLED @@ -354,20 +354,20 @@ gatts_event_handler BLEDevice::m_customGattsHandler = nullptr; errRc = esp_bt_controller_init(&bt_cfg); if (errRc != ESP_OK) { log_e("esp_bt_controller_init: rc=%d %s", errRc, GeneralUtils::errorToString(errRc)); - return; + return false; } #ifndef CONFIG_BT_CLASSIC_ENABLED errRc = esp_bt_controller_enable(ESP_BT_MODE_BLE); if (errRc != ESP_OK) { log_e("esp_bt_controller_enable: rc=%d %s", errRc, GeneralUtils::errorToString(errRc)); - return; + return false; } #else errRc = esp_bt_controller_enable(ESP_BT_MODE_BTDM); if (errRc != ESP_OK) { log_e("esp_bt_controller_enable: rc=%d %s", errRc, GeneralUtils::errorToString(errRc)); - return; + return false; } #endif #endif @@ -377,7 +377,7 @@ gatts_event_handler BLEDevice::m_customGattsHandler = nullptr; errRc = esp_bluedroid_init(); if (errRc != ESP_OK) { log_e("esp_bluedroid_init: rc=%d %s", errRc, GeneralUtils::errorToString(errRc)); - return; + return false; } } @@ -385,21 +385,21 @@ gatts_event_handler BLEDevice::m_customGattsHandler = nullptr; errRc = esp_bluedroid_enable(); if (errRc != ESP_OK) { log_e("esp_bluedroid_enable: rc=%d %s", errRc, GeneralUtils::errorToString(errRc)); - return; + return false; } } errRc = esp_ble_gap_register_callback(BLEDevice::gapEventHandler); if (errRc != ESP_OK) { log_e("esp_ble_gap_register_callback: rc=%d %s", errRc, GeneralUtils::errorToString(errRc)); - return; + return false; } #ifdef CONFIG_GATTC_ENABLE // Check that BLE client is configured in make menuconfig errRc = esp_ble_gattc_register_callback(BLEDevice::gattClientEventHandler); if (errRc != ESP_OK) { log_e("esp_ble_gattc_register_callback: rc=%d %s", errRc, GeneralUtils::errorToString(errRc)); - return; + return false; } #endif // CONFIG_GATTC_ENABLE @@ -407,7 +407,7 @@ gatts_event_handler BLEDevice::m_customGattsHandler = nullptr; errRc = esp_ble_gatts_register_callback(BLEDevice::gattServerEventHandler); if (errRc != ESP_OK) { log_e("esp_ble_gatts_register_callback: rc=%d %s", errRc, GeneralUtils::errorToString(errRc)); - return; + return false; } #endif // CONFIG_GATTS_ENABLE @@ -420,7 +420,7 @@ gatts_event_handler BLEDevice::m_customGattsHandler = nullptr; } if (errRc != ESP_OK) { log_e("esp_ble_gap_set_device_name: rc=%d %s", errRc, GeneralUtils::errorToString(errRc)); - return; + return false; }; #ifdef CONFIG_BLE_SMP_ENABLE // Check that BLE SMP (security) is configured in make menuconfig @@ -428,11 +428,12 @@ gatts_event_handler BLEDevice::m_customGattsHandler = nullptr; errRc = ::esp_ble_gap_set_security_param(ESP_BLE_SM_IOCAP_MODE, &iocap, sizeof(uint8_t)); if (errRc != ESP_OK) { log_e("esp_ble_gap_set_security_param: rc=%d %s", errRc, GeneralUtils::errorToString(errRc)); - return; + return false; }; #endif // CONFIG_BLE_SMP_ENABLE } vTaskDelay(200 / portTICK_PERIOD_MS); // Delay for 200 msecs as a workaround to an apparent Arduino environment issue. + return true; } // init From 794a108fa15b4b29b9a62a49eb6ff4c64ad60d46 Mon Sep 17 00:00:00 2001 From: Rodrigo Garcia Date: Mon, 27 Feb 2023 22:51:38 -0300 Subject: [PATCH 5/5] Changes init() to return a boolean --- libraries/BLE/src/BLEDevice.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/BLE/src/BLEDevice.h b/libraries/BLE/src/BLEDevice.h index 9b9cdf03d15..77fcc21bae5 100644 --- a/libraries/BLE/src/BLEDevice.h +++ b/libraries/BLE/src/BLEDevice.h @@ -36,7 +36,7 @@ class BLEDevice { static BLEAddress getAddress(); // Retrieve our own local BD address. static BLEScan* getScan(); // Get the scan object static std::string getValue(BLEAddress bdAddress, BLEUUID serviceUUID, BLEUUID characteristicUUID); // Get the value of a characteristic of a service on a server. - static void init(std::string deviceName); // Initialize the local BLE environment. + static bool init(std::string deviceName); // Initialize the local BLE environment. static void setPower(esp_power_level_t powerLevel, esp_ble_power_type_t powerType=ESP_BLE_PWR_TYPE_DEFAULT); // Set our power level. static void setValue(BLEAddress bdAddress, BLEUUID serviceUUID, BLEUUID characteristicUUID, std::string value); // Set the value of a characteristic on a service on a server. static std::string toString(); // Return a string representation of our device.