Skip to content

Commit 728c2df

Browse files
committed
[BREAKING] Remove NimBLEAddress type default value.
When constructing a NimBLEAddress via string or other non `ble_addr_t` parameters it is important that the address type be specified. This will help prevent issues where applications are not able to connect or identify scanned devices when comparing their addresses.
1 parent dbf6c38 commit 728c2df

File tree

3 files changed

+19
-17
lines changed

3 files changed

+19
-17
lines changed

src/NimBLEAddress.cpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,9 @@ NimBLEAddress::NimBLEAddress(ble_addr_t address) : ble_addr_t{address} {}
4545
* ```
4646
* which is 17 characters in length.
4747
* @param [in] addr The hex string representation of the address.
48-
* @param [in] type The type of the address.
48+
* @param [in] type The type of the address, should be one of:
49+
* * BLE_ADDR_PUBLIC (0)
50+
* * BLE_ADDR_RANDOM (1)
4951
*/
5052
NimBLEAddress::NimBLEAddress(const std::string& addr, uint8_t type) {
5153
this->type = type;
@@ -70,7 +72,9 @@ NimBLEAddress::NimBLEAddress(const std::string& addr, uint8_t type) {
7072
/**
7173
* @brief Constructor for compatibility with bluedroid esp library using native ESP representation.
7274
* @param [in] address A uint8_t[6] or esp_bd_addr_t containing the address.
73-
* @param [in] type The type of the address.
75+
* @param [in] type The type of the address should be one of:
76+
* * BLE_ADDR_PUBLIC (0)
77+
* * BLE_ADDR_RANDOM (1)
7478
*/
7579
NimBLEAddress::NimBLEAddress(const uint8_t address[BLE_DEV_ADDR_LEN], uint8_t type) {
7680
std::reverse_copy(address, address + BLE_DEV_ADDR_LEN, this->val);
@@ -81,7 +85,9 @@ NimBLEAddress::NimBLEAddress(const uint8_t address[BLE_DEV_ADDR_LEN], uint8_t ty
8185
* @brief Constructor for address using a hex value.\n
8286
* Use the same byte order, so use 0xa4c1385def16 for "a4:c1:38:5d:ef:16"
8387
* @param [in] address uint64_t containing the address.
84-
* @param [in] type The type of the address.
88+
* @param [in] type The type of the address should be one of:
89+
* * BLE_ADDR_PUBLIC (0)
90+
* * BLE_ADDR_RANDOM (1)
8591
*/
8692
NimBLEAddress::NimBLEAddress(const uint64_t& address, uint8_t type) {
8793
memcpy(this->val, &address, sizeof this->val);

src/NimBLEAddress.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@
3434
# include <string>
3535

3636
/**
37-
* @brief A %BLE device address.
37+
* @brief A BLE device address.
3838
*
39-
* Every %BLE device has a unique address which can be used to identify it and form connections.
39+
* Every BLE device has a unique address which can be used to identify it and form connections.
4040
*/
4141
class NimBLEAddress : private ble_addr_t {
4242
public:
@@ -45,9 +45,9 @@ class NimBLEAddress : private ble_addr_t {
4545
*/
4646
NimBLEAddress() = default;
4747
NimBLEAddress(const ble_addr_t address);
48-
NimBLEAddress(const uint8_t address[BLE_DEV_ADDR_LEN], uint8_t type = BLE_ADDR_PUBLIC);
49-
NimBLEAddress(const std::string& stringAddress, uint8_t type = BLE_ADDR_PUBLIC);
50-
NimBLEAddress(const uint64_t& address, uint8_t type = BLE_ADDR_PUBLIC);
48+
NimBLEAddress(const uint8_t address[BLE_DEV_ADDR_LEN], uint8_t type);
49+
NimBLEAddress(const std::string& stringAddress, uint8_t type);
50+
NimBLEAddress(const uint64_t& address, uint8_t type);
5151

5252
bool isRpa() const;
5353
bool isNrpa() const;

src/NimBLEDevice.cpp

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -614,18 +614,14 @@ bool NimBLEDevice::isBonded(const NimBLEAddress& address) {
614614
/**
615615
* @brief Get the address of a bonded peer device by index.
616616
* @param [in] index The index to retrieve the peer address of.
617-
* @returns NimBLEAddress of the found bonded peer or nullptr if not found.
617+
* @returns NimBLEAddress of the found bonded peer or null address if not found.
618618
*/
619619
NimBLEAddress NimBLEDevice::getBondedAddress(int index) {
620620
ble_addr_t peer_id_addrs[MYNEWT_VAL(BLE_STORE_MAX_BONDS)];
621621
int num_peers, rc;
622622
rc = ble_store_util_bonded_peers(&peer_id_addrs[0], &num_peers, MYNEWT_VAL(BLE_STORE_MAX_BONDS));
623-
if (rc != 0) {
624-
return nullptr;
625-
}
626-
627-
if (index > num_peers || index < 0) {
628-
return nullptr;
623+
if (rc != 0 || index > num_peers || index < 0) {
624+
return NimBLEAddress{};
629625
}
630626

631627
return NimBLEAddress(peer_id_addrs[index]);
@@ -704,12 +700,12 @@ size_t NimBLEDevice::getWhiteListCount() {
704700
/**
705701
* @brief Gets the address at the vector index.
706702
* @param [in] index The vector index to retrieve the address from.
707-
* @returns The NimBLEAddress at the whitelist index or nullptr if not found.
703+
* @returns The NimBLEAddress at the whitelist index or null address if not found.
708704
*/
709705
NimBLEAddress NimBLEDevice::getWhiteListAddress(size_t index) {
710706
if (index > m_whiteList.size()) {
711707
NIMBLE_LOGE(LOG_TAG, "Invalid index; %u", index);
712-
return nullptr;
708+
return NimBLEAddress{};
713709
}
714710

715711
return m_whiteList[index];

0 commit comments

Comments
 (0)