Skip to content

Commit 8c1bcc9

Browse files
sanastasiouh2zero
authored andcommitted
Fix incorrect TX power setting for advertising.
* Adds a new enum class to specify the tx power type to set (unused by non-esp devices). * When all type is specified it is now ensured that all the tx power types are set to the specified level. * Correctly sets the power level advertised for extended advertising with legacy pdu selected.
1 parent 1b5e444 commit 8c1bcc9

5 files changed

+78
-37
lines changed

src/NimBLEAdvertisementData.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ bool NimBLEAdvertisementData::addTxPower() {
110110
data[0] = BLE_HS_ADV_TX_PWR_LVL_LEN + 1;
111111
data[1] = BLE_HS_ADV_TYPE_TX_PWR_LVL;
112112
# ifndef CONFIG_IDF_TARGET_ESP32P4
113-
data[2] = NimBLEDevice::getPower();
113+
data[2] = NimBLEDevice::getPower(NimBLETxPowerType::Advertise);
114114
# else
115115
data[2] = 0;
116116
# endif

src/NimBLEDevice.cpp

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -464,17 +464,33 @@ bool NimBLEDevice::setPowerLevel(esp_power_level_t powerLevel, esp_ble_power_typ
464464
* @param [in] dbm The power level to set in dBm.
465465
* @return True if the power level was set successfully.
466466
*/
467-
bool NimBLEDevice::setPower(int8_t dbm) {
467+
bool NimBLEDevice::setPower(int8_t dbm, NimBLETxPowerType type) {
468468
# ifdef ESP_PLATFORM
469469
# ifdef CONFIG_IDF_TARGET_ESP32P4
470470
return false; // CONFIG_IDF_TARGET_ESP32P4 does not support esp_ble_tx_power_set
471471
# else
472472
if (dbm % 3 == 2) {
473473
dbm++; // round up to the next multiple of 3 to be able to target 20dbm
474474
}
475-
return setPowerLevel(static_cast<esp_power_level_t>(dbm / 3 + ESP_PWR_LVL_N0));
475+
476+
bool success = false;
477+
esp_power_level_t espPwr = static_cast<esp_power_level_t>(dbm / 3 + ESP_PWR_LVL_N0);
478+
if (type == NimBLETxPowerType::All) {
479+
success = setPowerLevel(espPwr, ESP_BLE_PWR_TYPE_ADV);
480+
success &= setPowerLevel(espPwr, ESP_BLE_PWR_TYPE_SCAN);
481+
success &= setPowerLevel(espPwr, ESP_BLE_PWR_TYPE_DEFAULT);
482+
} else if (type == NimBLETxPowerType::Advertise) {
483+
success = setPowerLevel(espPwr, ESP_BLE_PWR_TYPE_ADV);
484+
} else if (type == NimBLETxPowerType::Scan) {
485+
success = setPowerLevel(espPwr, ESP_BLE_PWR_TYPE_SCAN);
486+
} else if (type == NimBLETxPowerType::Connection) {
487+
success = setPowerLevel(espPwr, ESP_BLE_PWR_TYPE_DEFAULT);
488+
}
489+
490+
return success;
476491
# endif
477492
# else
493+
(void)type; // unused
478494
NIMBLE_LOGD(LOG_TAG, ">> setPower: %d", dbm);
479495
ble_hci_vs_set_tx_pwr_cp cmd{dbm};
480496
ble_hci_vs_set_tx_pwr_rp rsp{0};
@@ -493,12 +509,16 @@ bool NimBLEDevice::setPower(int8_t dbm) {
493509
* @brief Get the transmission power.
494510
* @return The power level currently used in dbm or 0xFF on error.
495511
*/
496-
int NimBLEDevice::getPower() {
512+
int NimBLEDevice::getPower(NimBLETxPowerType type) {
497513
# ifdef ESP_PLATFORM
498514
# ifdef CONFIG_IDF_TARGET_ESP32P4
499515
return 0xFF; // CONFIG_IDF_TARGET_ESP32P4 does not support esp_ble_tx_power_get
500516
# else
501-
int pwr = getPowerLevel();
517+
esp_ble_power_type_t espPwr = type == NimBLETxPowerType::Advertise ? ESP_BLE_PWR_TYPE_ADV
518+
: type == NimBLETxPowerType::Scan ? ESP_BLE_PWR_TYPE_SCAN
519+
: ESP_BLE_PWR_TYPE_DEFAULT;
520+
521+
int pwr = getPowerLevel(espPwr);
502522
if (pwr < 0) {
503523
NIMBLE_LOGE(LOG_TAG, "esp_ble_tx_power_get failed rc=%d", pwr);
504524
return 0xFF;
@@ -515,6 +535,7 @@ int NimBLEDevice::getPower() {
515535
return 0;
516536
# endif
517537
# else
538+
(void)type; // unused
518539
return ble_phy_txpwr_get();
519540
# endif
520541
} // getPower

src/NimBLEDevice.h

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,13 @@ class NimBLEAddress;
101101
# define NIMBLE_MAX_CONNECTIONS CONFIG_NIMBLE_MAX_CONNECTIONS
102102
# endif
103103

104+
enum class NimBLETxPowerType {
105+
All = 0,
106+
Advertise = 1,
107+
Scan = 2,
108+
Connection = 3
109+
};
110+
104111
typedef int (*gap_event_handler)(ble_gap_event* event, void* arg);
105112

106113
/**
@@ -138,8 +145,8 @@ class NimBLEDevice {
138145
static void onReset(int reason);
139146
static void onSync(void);
140147
static void host_task(void* param);
141-
static int getPower();
142-
static bool setPower(int8_t dbm);
148+
static int getPower(NimBLETxPowerType type = NimBLETxPowerType::All);
149+
static bool setPower(int8_t dbm, NimBLETxPowerType type = NimBLETxPowerType::All);
143150

144151
# ifdef ESP_PLATFORM
145152
# ifndef CONFIG_IDF_TARGET_ESP32P4

src/NimBLEExtAdvertising.cpp

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,7 @@ NimBLEExtAdvertisement::NimBLEExtAdvertisement(uint8_t priPhy, uint8_t secPhy) {
359359
m_params.own_addr_type = NimBLEDevice::m_ownAddrType;
360360
m_params.primary_phy = priPhy;
361361
m_params.secondary_phy = secPhy;
362-
m_params.tx_power = 127;
362+
m_params.tx_power = NimBLEDevice::getPower(NimBLETxPowerType::Advertise);
363363
} // NimBLEExtAdvertisement
364364

365365
/**
@@ -1014,8 +1014,22 @@ bool NimBLEExtAdvertisement::setPreferredParams(uint16_t minInterval, uint16_t m
10141014
/**
10151015
* @brief Adds Tx power level to the advertisement data.
10161016
*/
1017-
void NimBLEExtAdvertisement::addTxPower() {
1017+
bool NimBLEExtAdvertisement::addTxPower() {
1018+
if (m_params.legacy_pdu) {
1019+
m_params.include_tx_power = 0;
1020+
uint8_t data[3];
1021+
data[0] = BLE_HS_ADV_TX_PWR_LVL_LEN + 1;
1022+
data[1] = BLE_HS_ADV_TYPE_TX_PWR_LVL;
1023+
# ifndef CONFIG_IDF_TARGET_ESP32P4
1024+
data[2] = NimBLEDevice::getPower(NimBLETxPowerType::Advertise);
1025+
# else
1026+
data[2] = 0;
1027+
# endif
1028+
return addData(data, 3);
1029+
}
1030+
10181031
m_params.include_tx_power = 1;
1032+
return true;
10191033
} // addTxPower
10201034

10211035
/**

src/NimBLEExtAdvertising.h

Lines changed: 27 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -46,34 +46,33 @@ class NimBLEUUID;
4646
class NimBLEExtAdvertisement {
4747
public:
4848
NimBLEExtAdvertisement(uint8_t priPhy = BLE_HCI_LE_PHY_1M, uint8_t secPhy = BLE_HCI_LE_PHY_1M);
49-
bool setAppearance(uint16_t appearance);
50-
bool addServiceUUID(const NimBLEUUID& serviceUUID);
51-
bool addServiceUUID(const char* serviceUUID);
52-
bool removeServiceUUID(const NimBLEUUID& serviceUUID);
53-
bool removeServiceUUID(const char* serviceUUID);
54-
bool removeServices();
55-
bool setCompleteServices(const NimBLEUUID& uuid);
56-
bool setCompleteServices16(const std::vector<NimBLEUUID>& uuids);
57-
bool setCompleteServices32(const std::vector<NimBLEUUID>& uuids);
58-
bool setFlags(uint8_t flag);
59-
bool setManufacturerData(const uint8_t* data, size_t length);
60-
bool setManufacturerData(const std::string& data);
61-
bool setManufacturerData(const std::vector<uint8_t>& data);
62-
bool setURI(const std::string& uri);
63-
bool setName(const std::string& name, bool isComplete = true);
64-
bool setPartialServices(const NimBLEUUID& uuid);
65-
bool setPartialServices16(const std::vector<NimBLEUUID>& uuids);
66-
bool setPartialServices32(const std::vector<NimBLEUUID>& uuids);
67-
bool setServiceData(const NimBLEUUID& uuid, const uint8_t* data, size_t length);
68-
bool setServiceData(const NimBLEUUID& uuid, const std::string& data);
69-
bool setServiceData(const NimBLEUUID& uuid, const std::vector<uint8_t>& data);
70-
bool setShortName(const std::string& name);
71-
bool setData(const uint8_t* data, size_t length);
72-
bool addData(const uint8_t* data, size_t length);
73-
bool addData(const std::string& data);
74-
bool setPreferredParams(uint16_t min, uint16_t max);
75-
76-
void addTxPower();
49+
bool setAppearance(uint16_t appearance);
50+
bool addServiceUUID(const NimBLEUUID& serviceUUID);
51+
bool addServiceUUID(const char* serviceUUID);
52+
bool removeServiceUUID(const NimBLEUUID& serviceUUID);
53+
bool removeServiceUUID(const char* serviceUUID);
54+
bool removeServices();
55+
bool setCompleteServices(const NimBLEUUID& uuid);
56+
bool setCompleteServices16(const std::vector<NimBLEUUID>& uuids);
57+
bool setCompleteServices32(const std::vector<NimBLEUUID>& uuids);
58+
bool setFlags(uint8_t flag);
59+
bool setManufacturerData(const uint8_t* data, size_t length);
60+
bool setManufacturerData(const std::string& data);
61+
bool setManufacturerData(const std::vector<uint8_t>& data);
62+
bool setURI(const std::string& uri);
63+
bool setName(const std::string& name, bool isComplete = true);
64+
bool setPartialServices(const NimBLEUUID& uuid);
65+
bool setPartialServices16(const std::vector<NimBLEUUID>& uuids);
66+
bool setPartialServices32(const std::vector<NimBLEUUID>& uuids);
67+
bool setServiceData(const NimBLEUUID& uuid, const uint8_t* data, size_t length);
68+
bool setServiceData(const NimBLEUUID& uuid, const std::string& data);
69+
bool setServiceData(const NimBLEUUID& uuid, const std::vector<uint8_t>& data);
70+
bool setShortName(const std::string& name);
71+
bool setData(const uint8_t* data, size_t length);
72+
bool addData(const uint8_t* data, size_t length);
73+
bool addData(const std::string& data);
74+
bool setPreferredParams(uint16_t min, uint16_t max);
75+
bool addTxPower();
7776
void setLegacyAdvertising(bool enable);
7877
void setConnectable(bool enable);
7978
void setScannable(bool enable);

0 commit comments

Comments
 (0)