Skip to content

Fixes Maximum BLE Device Length #7901

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 22 additions & 15 deletions libraries/BLE/src/BLEDevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -330,21 +330,21 @@ 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.

esp_err_t errRc = ESP_OK;
#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
Expand All @@ -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
Expand All @@ -377,56 +377,63 @@ 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;
}
}

if (bt_state != ESP_BLUEDROID_STATUS_ENABLED) {
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

#ifdef CONFIG_GATTS_ENABLE // Check that BLE server is configured in make menuconfig
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
esp_ble_io_cap_t iocap = ESP_IO_CAP_NONE;
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


Expand Down
2 changes: 1 addition & 1 deletion libraries/BLE/src/BLEDevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down