diff --git a/libraries/BLE/src/BLEDevice.cpp b/libraries/BLE/src/BLEDevice.cpp index 473f3ef032e..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,14 +407,20 @@ 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 - - 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; + return false; }; #ifdef CONFIG_BLE_SMP_ENABLE // Check that BLE SMP (security) is configured in make menuconfig @@ -422,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 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.