Skip to content

Commit b9a59e4

Browse files
authored
Merge branch 'master' into refactor
2 parents 107a09a + 8158a16 commit b9a59e4

File tree

10 files changed

+116
-48
lines changed

10 files changed

+116
-48
lines changed

CHANGELOG.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,22 @@
11
# Changelog
22
All notable changes to this project will be documented in this file.
33

4+
## [2.1.0] 2025-01-12
5+
6+
## Fixed
7+
- Crash when retrieving descriptors if more than one exists.
8+
- Incorrect TX power value being advertised.
9+
- New user guide code for 2.x
10+
- Potential race condition if `NimBLEScan::clearResults1 is called from multiple tasks.
11+
12+
## Changed
13+
- If privacy is not enabled identity keys will not be shared.
14+
- `NimBLEDevice::setPower` and `NimBLEDevice::getPower` now take an additional parameter `NimBLETxPowerType` to set/get the power level for different operations.
15+
16+
## Added
17+
- Config option `CONFIG_NIMBLE_CPP_ADDR_FMT_EXCLUDE_DELIMITER`, if defined will remove the ":" delimiter from the BLE address string.
18+
- Config option `CONFIG_NIMBLE_CPP_ADDR_FMT_UPPERCASE` if defined will make the BLE address strings uppercase.
19+
420
## [2.0.3] 2025-01-05
521

622
## Fixed

docs/Doxyfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ PROJECT_NAME = esp-nimble-cpp
4848
# could be handy for archiving the generated documentation or if some version
4949
# control system is used.
5050

51-
PROJECT_NUMBER = 2.0.3
51+
PROJECT_NUMBER = 2.1.0
5252

5353
# Using the PROJECT_BRIEF tag one can provide an optional one line description
5454
# for a project that appears at the top of each page and should give viewer a

idf_component.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
## IDF Component Manager Manifest File
2-
version: "2.0.3"
2+
version: "2.1.0"
33
license: "Apache-2.0"
44
description: "C++ wrapper for the NimBLE BLE stack"
55
url: "https://github.com/h2zero/esp-nimble-cpp"

library.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "esp-nimble-cpp",
3-
"version": "2.0.3",
3+
"version": "2.1.0",
44
"description": "C++ wrapper for the NimBLE BLE stack",
55
"keywords": [
66
"BLE",

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: 34 additions & 9 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
@@ -877,17 +898,21 @@ bool NimBLEDevice::init(const std::string& deviceName) {
877898
nimble_port_init();
878899

879900
// Setup callbacks for host events
880-
ble_hs_cfg.reset_cb = NimBLEDevice::onReset;
881-
ble_hs_cfg.sync_cb = NimBLEDevice::onSync;
901+
ble_hs_cfg.reset_cb = NimBLEDevice::onReset;
902+
ble_hs_cfg.sync_cb = NimBLEDevice::onSync;
903+
ble_hs_cfg.store_status_cb = ble_store_util_status_rr; /*TODO: Implement handler for this*/
882904

883905
// Set initial security capabilities
884906
ble_hs_cfg.sm_io_cap = BLE_HS_IO_NO_INPUT_OUTPUT;
885907
ble_hs_cfg.sm_bonding = 0;
886908
ble_hs_cfg.sm_mitm = 0;
887909
ble_hs_cfg.sm_sc = 1;
888-
ble_hs_cfg.sm_our_key_dist = BLE_SM_PAIR_KEY_DIST_ENC | BLE_SM_PAIR_KEY_DIST_ID;
889-
ble_hs_cfg.sm_their_key_dist = BLE_SM_PAIR_KEY_DIST_ENC | BLE_SM_PAIR_KEY_DIST_ID;
890-
ble_hs_cfg.store_status_cb = ble_store_util_status_rr; /*TODO: Implement handler for this*/
910+
ble_hs_cfg.sm_our_key_dist = BLE_SM_PAIR_KEY_DIST_ENC;
911+
ble_hs_cfg.sm_their_key_dist = BLE_SM_PAIR_KEY_DIST_ENC;
912+
# if MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_PRIVACY)
913+
ble_hs_cfg.sm_our_key_dist |= BLE_SM_PAIR_KEY_DIST_ID;
914+
ble_hs_cfg.sm_their_key_dist |= BLE_SM_PAIR_KEY_DIST_ID;
915+
# endif
891916

892917
setDeviceName(deviceName);
893918
ble_store_config_init();

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);

src/NimBLEScan.cpp

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -468,19 +468,26 @@ NimBLEScanResults NimBLEScan::getResults() {
468468
* @brief Clear the stored results of the scan.
469469
*/
470470
void NimBLEScan::clearResults() {
471-
for (const auto& dev : m_scanResults.m_deviceVec) {
472-
delete dev;
471+
if (m_scanResults.m_deviceVec.size()) {
472+
std::vector<NimBLEAdvertisedDevice*> vSwap{};
473+
ble_npl_hw_enter_critical();
474+
vSwap.swap(m_scanResults.m_deviceVec);
475+
ble_npl_hw_exit_critical(0);
476+
for (const auto& dev : vSwap) {
477+
delete dev;
478+
}
473479
}
474-
std::vector<NimBLEAdvertisedDevice*>().swap(m_scanResults.m_deviceVec);
475480
} // clearResults
476481

477482
/**
478483
* @brief Dump the scan results to the log.
479484
*/
480485
void NimBLEScanResults::dump() const {
486+
#if CONFIG_NIMBLE_CPP_LOG_LEVEL >=3
481487
for (const auto& dev : m_deviceVec) {
482488
NIMBLE_LOGI(LOG_TAG, "- %s", dev->toString().c_str());
483489
}
490+
#endif
484491
} // dump
485492

486493
/**

0 commit comments

Comments
 (0)