Skip to content

Commit 7493715

Browse files
committed
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.
1 parent b5b4666 commit 7493715

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

src/NimBLEDevice.cpp

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -479,15 +479,27 @@ bool NimBLEDevice::setPower(int8_t dbm) {
479479

480480
/**
481481
* @brief Get the transmission power.
482-
* @return The power level currently used in dbm.
483-
* @note ESP32S3 only returns 0xFF as of IDF 5, so this function will return 20dbm.
482+
* @return The power level currently used in dbm or 0xFF on error.
484483
*/
485484
int NimBLEDevice::getPower() {
486485
# ifdef ESP_PLATFORM
487486
# ifdef CONFIG_IDF_TARGET_ESP32P4
488487
return 0xFF; // CONFIG_IDF_TARGET_ESP32P4 does not support esp_ble_tx_power_get
489488
# else
490489
int pwr = esp_ble_tx_power_get(ESP_BLE_PWR_TYPE_DEFAULT);
490+
491+
# if defined(CONFIG_IDF_TARGET_ESP32C3) || defined(CONFIG_IDF_TARGET_ESP32S3)
492+
// workaround for bug when "enhanced tx power" was added
493+
if (pwr == 0xFF) {
494+
pwr = esp_ble_tx_power_get(ESP_BLE_PWR_TYPE_CONN_HDL3);
495+
}
496+
# endif
497+
498+
if (pwr < 0) {
499+
NIMBLE_LOGE(LOG_TAG, "esp_ble_tx_power_get failed rc=%d", pwr);
500+
return 0xFF;
501+
}
502+
491503
if (pwr < ESP_PWR_LVL_N0) {
492504
return -3 * (ESP_PWR_LVL_N0 - pwr);
493505
}

0 commit comments

Comments
 (0)