Add a workaround for esp32s3 and esp32c3 tx power bug (#252)

* Add a workaround for esp32s3 and esp32c3 tx power bug

This adds a workaround to get the tx power when the function returns error due to a bug introduced in some versions of esp-idf.

* Added error checking, return value will be 0xFF if there was an error.
This commit is contained in:
h2zero 2024-12-04 10:46:33 -07:00 committed by GitHub
parent b5b46661e0
commit a55489fee2
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -425,10 +425,19 @@ std::vector<NimBLEClient*> NimBLEDevice::getConnectedClients() {
# ifndef CONFIG_IDF_TARGET_ESP32P4
/**
* @brief Get the transmission power.
* @return The power level currently used in esp_power_level_t.
* @return The power level currently used in esp_power_level_t or a negative value on error.
*/
esp_power_level_t NimBLEDevice::getPowerLevel(esp_ble_power_type_t powerType) {
return esp_ble_tx_power_get(powerType);
esp_power_level_t pwr = esp_ble_tx_power_get(powerType);
# if defined(CONFIG_IDF_TARGET_ESP32C3) || defined(CONFIG_IDF_TARGET_ESP32S3)
// workaround for bug when "enhanced tx power" was added
if (pwr == 0xFF) {
pwr = esp_ble_tx_power_get(ESP_BLE_PWR_TYPE_CONN_HDL3);
}
# endif
return pwr;
} // getPowerLevel
/**
@ -479,15 +488,19 @@ bool NimBLEDevice::setPower(int8_t dbm) {
/**
* @brief Get the transmission power.
* @return The power level currently used in dbm.
* @note ESP32S3 only returns 0xFF as of IDF 5, so this function will return 20dbm.
* @return The power level currently used in dbm or 0xFF on error.
*/
int NimBLEDevice::getPower() {
# ifdef ESP_PLATFORM
# ifdef CONFIG_IDF_TARGET_ESP32P4
return 0xFF; // CONFIG_IDF_TARGET_ESP32P4 does not support esp_ble_tx_power_get
# else
int pwr = esp_ble_tx_power_get(ESP_BLE_PWR_TYPE_DEFAULT);
int pwr = getPowerLevel();
if (pwr < 0) {
NIMBLE_LOGE(LOG_TAG, "esp_ble_tx_power_get failed rc=%d", pwr);
return 0xFF;
}
if (pwr < ESP_PWR_LVL_N0) {
return -3 * (ESP_PWR_LVL_N0 - pwr);
}