[BREAKING] Refactor NimBLEAddress - use NimBLE core representation.

This simplifies the NimBLEAddress code by directly using the NimBLE core `ble_addr_t` type to hold the address
and allows using NimBLE core functions and macros to replace code in some methods.

* `getNative()` replaced with `getBase()` and now returns a pointer to `const ble_addr_t` instead of a pointer to the address value.
* Adds `isNrpa()` method to test if an address is random non-resolvable.
* Adds `isStatic()` method to test if an address is random static.
* Adds `isPublic()` method to test if an address is a public address.
* Adds `isNull()` methods to test if an address is NULL.
* Adds `getValue()` method which returns a read-only pointer to the address value.
* Adds `reverseByteOrder()` method which will reverse the byte order of the address value.
* `equals()` method and == operator will now also test if the address types are the same.
* Code cleanup.
This commit is contained in:
h2zero 2024-07-04 19:17:13 -06:00 committed by h2zero
parent 21e1217e4c
commit 6279817143
10 changed files with 171 additions and 206 deletions

View file

@ -14,90 +14,65 @@
#include "nimconfig.h" #include "nimconfig.h"
#if defined(CONFIG_BT_ENABLED) #if defined(CONFIG_BT_ENABLED)
#include <algorithm>
# include "NimBLEAddress.h" # include "NimBLEAddress.h"
#include "NimBLEUtils.h"
# include "NimBLELog.h" # include "NimBLELog.h"
# include <algorithm>
static const char* LOG_TAG = "NimBLEAddress"; static const char* LOG_TAG = "NimBLEAddress";
/************************************************* /*************************************************
* NOTE: NimBLE address bytes are in INVERSE ORDER! * NOTE: NimBLE address bytes are in INVERSE ORDER!
* We will accomodate that fact in these methods. * We will accommodate that fact in these methods.
*************************************************/ *************************************************/
/** /**
* @brief Create an address from the native NimBLE representation. * @brief Create an address from the native NimBLE representation.
* @param [in] address The native NimBLE address. * @param [in] address The native NimBLE address.
*/ */
NimBLEAddress::NimBLEAddress(ble_addr_t address) { NimBLEAddress::NimBLEAddress(ble_addr_t address) : ble_addr_t{address} {}
memcpy(m_address, address.val, 6);
m_addrType = address.type;
} // NimBLEAddress
/** /**
* @brief Create a blank address, i.e. 00:00:00:00:00:00, type 0. * @brief Create an address from a hex string.
*/
NimBLEAddress::NimBLEAddress() {
NimBLEAddress("");
} // NimBLEAddress
/**
* @brief Create an address from a hex string
* *
* A hex string is of the format: * A hex string is of the format:
* ``` * ```
* 00:00:00:00:00:00 * 00:00:00:00:00:00
* ``` * ```
* which is 17 characters in length. * which is 17 characters in length.
* * @param [in] addr The hex string representation of the address.
* @param [in] stringAddress The hex string representation of the address.
* @param [in] type The type of the address. * @param [in] type The type of the address.
*/ */
NimBLEAddress::NimBLEAddress(const std::string &stringAddress, uint8_t type) { NimBLEAddress::NimBLEAddress(const std::string& addr, uint8_t type) {
m_addrType = type; this->type = type;
if (stringAddress.length() == 0) { if (addr.length() == BLE_DEV_ADDR_LEN) {
memset(m_address, 0, 6); std::reverse_copy(addr.data(), addr.data() + BLE_DEV_ADDR_LEN, this->val);
return; return;
} }
if (stringAddress.length() == 6) { if (addr.length() == 17) {
std::reverse_copy(stringAddress.data(), stringAddress.data() + 6, m_address); std::string mac{addr};
mac.erase(std::remove(mac.begin(), mac.end(), ':'), mac.end());
uint64_t address = std::stoul(mac, nullptr, 16);
memcpy(this->val, &address, sizeof this->val);
return; return;
} }
if (stringAddress.length() != 17) { *this = NimBLEAddress{};
memset(m_address, 0, sizeof m_address); // "00:00:00:00:00:00" represents an invalid address NIMBLE_LOGE(LOG_TAG, "Invalid address '%s'", addr.c_str());
NIMBLE_LOGD(LOG_TAG, "Invalid address '%s'", stringAddress.c_str());
return;
}
int data[6];
if(sscanf(stringAddress.c_str(), "%x:%x:%x:%x:%x:%x", &data[5], &data[4], &data[3], &data[2], &data[1], &data[0]) != 6) {
memset(m_address, 0, sizeof m_address); // "00:00:00:00:00:00" represents an invalid address
NIMBLE_LOGD(LOG_TAG, "Invalid address '%s'", stringAddress.c_str());
}
for(size_t index = 0; index < sizeof m_address; index++) {
m_address[index] = data[index];
}
} // NimBLEAddress } // NimBLEAddress
/** /**
* @brief Constructor for compatibility with bluedroid esp library using native ESP representation. * @brief Constructor for compatibility with bluedroid esp library using native ESP representation.
* @param [in] address A uint8_t[6] or esp_bd_addr_t containing the address. * @param [in] address A uint8_t[6] or esp_bd_addr_t containing the address.
* @param [in] type The type of the address. * @param [in] type The type of the address.
*/ */
NimBLEAddress::NimBLEAddress(uint8_t address[6], uint8_t type) { NimBLEAddress::NimBLEAddress(const uint8_t address[BLE_DEV_ADDR_LEN], uint8_t type) {
std::reverse_copy(address, address + sizeof m_address, m_address); std::reverse_copy(address, address + BLE_DEV_ADDR_LEN, this->val);
m_addrType = type; this->type = type;
} // NimBLEAddress } // NimBLEAddress
/** /**
* @brief Constructor for address using a hex value.\n * @brief Constructor for address using a hex value.\n
* Use the same byte order, so use 0xa4c1385def16 for "a4:c1:38:5d:ef:16" * Use the same byte order, so use 0xa4c1385def16 for "a4:c1:38:5d:ef:16"
@ -105,11 +80,10 @@ NimBLEAddress::NimBLEAddress(uint8_t address[6], uint8_t type) {
* @param [in] type The type of the address. * @param [in] type The type of the address.
*/ */
NimBLEAddress::NimBLEAddress(const uint64_t& address, uint8_t type) { NimBLEAddress::NimBLEAddress(const uint64_t& address, uint8_t type) {
memcpy(m_address, &address, sizeof m_address); memcpy(this->val, &address, sizeof this->val);
m_addrType = type; this->type = type;
} // NimBLEAddress } // NimBLEAddress
/** /**
* @brief Determine if this address equals another. * @brief Determine if this address equals another.
* @param [in] otherAddress The other address to compare against. * @param [in] otherAddress The other address to compare against.
@ -119,43 +93,72 @@ bool NimBLEAddress::equals(const NimBLEAddress &otherAddress) const {
return *this == otherAddress; return *this == otherAddress;
} // equals } // equals
/** /**
* @brief Get the native representation of the address. * @brief Get the NimBLE base struct of the address.
* @return a pointer to the uint8_t[6] array of the address. * @return A read only reference to the NimBLE base struct of the address.
*/ */
const uint8_t *NimBLEAddress::getNative() const { const ble_addr_t* NimBLEAddress::getBase() const {
return m_address; return reinterpret_cast<const ble_addr_t*>(this);
} // getNative } // getBase
/** /**
* @brief Get the address type. * @brief Get the address type.
* @return The address type. * @return The address type.
*/ */
uint8_t NimBLEAddress::getType() const { uint8_t NimBLEAddress::getType() const {
return m_addrType; return this->type;
} // getType } // getType
/**
* @brief Get the address value.
* @return A read only reference to the address value.
*/
const uint8_t* NimBLEAddress::getVal() const {
return this->val;
} // getVal
/** /**
* @brief Determine if this address is a Resolvable Private Address. * @brief Determine if this address is a Resolvable Private Address.
* @return True if the address is a RPA. * @return True if the address is a RPA.
*/ */
bool NimBLEAddress::isRpa() const { bool NimBLEAddress::isRpa() const {
return (m_addrType && ((m_address[5] & 0xc0) == 0x40)); return BLE_ADDR_IS_RPA(this);
} // isRpa } // isRpa
/**
* @brief Determine if this address is a Non-Resolvable Private Address.
* @return True if the address is a NRPA.
*/
bool NimBLEAddress::isNrpa() const {
return BLE_ADDR_IS_NRPA(this);
} // isNrpa
/**
* @brief Determine if this address is a Static Address.
* @return True if the address is a Static Address.
*/
bool NimBLEAddress::isStatic() const {
return BLE_ADDR_IS_STATIC(this);
} // isStatic
/**
* @brief Determine if this address is a Public Address.
* @return True if the address is a Public Address.
*/
bool NimBLEAddress::isPublic() const {
return this->type == BLE_ADDR_PUBLIC;
} // isPublic
/**
* @brief Determine if this address is a NULL Address.
* @return True if the address is a NULL Address.
*/
bool NimBLEAddress::isNull() const {
return *this == NimBLEAddress{};
} // isNull
/** /**
* @brief Convert a BLE address to a string. * @brief Convert a BLE address to a string.
*
* A string representation of an address is in the format:
*
* ```
* xx:xx:xx:xx:xx:xx
* ```
*
* @return The string representation of the address. * @return The string representation of the address.
* @deprecated Use std::string() operator instead. * @deprecated Use std::string() operator instead.
*/ */
@ -163,14 +166,25 @@ std::string NimBLEAddress::toString() const {
return std::string(*this); return std::string(*this);
} // toString } // toString
/**
* @brief Reverse the byte order of the address.
* @return A reference to this address.
*/
const NimBLEAddress& NimBLEAddress::reverseByteOrder() {
std::reverse(this->val, this->val + BLE_DEV_ADDR_LEN);
return *this;
} // reverseByteOrder
/** /**
* @brief Convenience operator to check if this address is equal to another. * @brief Convenience operator to check if this address is equal to another.
*/ */
bool NimBLEAddress::operator==(const NimBLEAddress& rhs) const { bool NimBLEAddress::operator==(const NimBLEAddress& rhs) const {
return memcmp(rhs.m_address, m_address, sizeof m_address) == 0; if (this->type != rhs.type) {
} // operator == return false;
}
return memcmp(rhs.val, this->val, sizeof this->val) == 0;
} // operator ==
/** /**
* @brief Convenience operator to check if this address is not equal to another. * @brief Convenience operator to check if this address is not equal to another.
@ -179,27 +193,30 @@ bool NimBLEAddress::operator !=(const NimBLEAddress & rhs) const {
return !this->operator==(rhs); return !this->operator==(rhs);
} // operator != } // operator !=
/** /**
* @brief Convienience operator to convert this address to string representation. * @brief Convenience operator to convert this address to string representation.
* @details This allows passing NimBLEAddress to functions * @details This allows passing NimBLEAddress to functions that accept std::string and/or it's methods as a parameter.
* that accept std::string and/or or it's methods as a parameter.
*/ */
NimBLEAddress::operator std::string() const { NimBLEAddress::operator std::string() const {
char buffer[18]; char buffer[18];
snprintf(buffer, sizeof(buffer), "%02x:%02x:%02x:%02x:%02x:%02x", snprintf(buffer,
m_address[5], m_address[4], m_address[3], sizeof(buffer),
m_address[2], m_address[1], m_address[0]); "%02x:%02x:%02x:%02x:%02x:%02x",
return std::string(buffer); this->val[5],
this->val[4],
this->val[3],
this->val[2],
this->val[1],
this->val[0]);
return std::string{buffer};
} // operator std::string } // operator std::string
/** /**
* @brief Convenience operator to convert the native address representation to uint_64. * @brief Convenience operator to convert the native address representation to uint_64.
*/ */
NimBLEAddress::operator uint64_t() const { NimBLEAddress::operator uint64_t() const {
uint64_t address = 0; uint64_t address = 0;
memcpy(&address, m_address, sizeof m_address); memcpy(&address, this->val, sizeof this->val);
return address; return address;
} // operator uint64_t } // operator uint64_t

View file

@ -12,8 +12,8 @@
* Author: kolban * Author: kolban
*/ */
#ifndef COMPONENTS_NIMBLEADDRESS_H_ #ifndef NIMBLE_CPP_ADDRESS_H_
#define COMPONENTS_NIMBLEADDRESS_H_ #define NIMBLE_CPP_ADDRESS_H_
#include "nimconfig.h" #include "nimconfig.h"
#if defined(CONFIG_BT_ENABLED) #if defined(CONFIG_BT_ENABLED)
@ -29,35 +29,39 @@
/**************************/ /**************************/
# include <string> # include <string>
#include <algorithm>
/** /**
* @brief A %BLE device address. * @brief A %BLE device address.
* *
* Every %BLE device has a unique address which can be used to identify it and form connections. * Every %BLE device has a unique address which can be used to identify it and form connections.
*/ */
class NimBLEAddress { class NimBLEAddress : private ble_addr_t {
public: public:
NimBLEAddress(); /**
NimBLEAddress(ble_addr_t address); * @brief Create a blank address, i.e. 00:00:00:00:00:00, type 0.
NimBLEAddress(uint8_t address[6], uint8_t type = BLE_ADDR_PUBLIC); */
NimBLEAddress() = default;
NimBLEAddress(const ble_addr_t address);
NimBLEAddress(const uint8_t address[BLE_DEV_ADDR_LEN], uint8_t type = BLE_ADDR_PUBLIC);
NimBLEAddress(const std::string& stringAddress, uint8_t type = BLE_ADDR_PUBLIC); NimBLEAddress(const std::string& stringAddress, uint8_t type = BLE_ADDR_PUBLIC);
NimBLEAddress(const uint64_t& address, uint8_t type = BLE_ADDR_PUBLIC); NimBLEAddress(const uint64_t& address, uint8_t type = BLE_ADDR_PUBLIC);
bool isRpa() const; bool isRpa() const;
bool isNrpa() const;
bool isStatic() const;
bool isPublic() const;
bool isNull() const;
bool equals(const NimBLEAddress& otherAddress) const; bool equals(const NimBLEAddress& otherAddress) const;
const uint8_t* getNative() const; const ble_addr_t* getBase() const;
std::string toString() const; std::string toString() const;
uint8_t getType() const; uint8_t getType() const;
const uint8_t* getVal() const;
const NimBLEAddress& reverseByteOrder();
bool operator==(const NimBLEAddress& rhs) const; bool operator==(const NimBLEAddress& rhs) const;
bool operator!=(const NimBLEAddress& rhs) const; bool operator!=(const NimBLEAddress& rhs) const;
operator std::string() const; operator std::string() const;
operator uint64_t() const; operator uint64_t() const;
private:
uint8_t m_address[6];
uint8_t m_addrType;
}; };
#endif /* CONFIG_BT_ENABLED */ #endif /* CONFIG_BT_ENABLED */
#endif /* COMPONENTS_NIMBLEADDRESS_H_ */ #endif /* NIMBLE_CPP_ADDRESS_H_ */

View file

@ -302,7 +302,7 @@ NimBLEAddress NimBLEAdvertisedDevice::getTargetAddress(uint8_t index) {
} }
} }
return NimBLEAddress(""); return NimBLEAddress{};
} }

View file

@ -162,7 +162,7 @@ private:
uint8_t findAdvField(uint8_t type, uint8_t index = 0, size_t * data_loc = nullptr); uint8_t findAdvField(uint8_t type, uint8_t index = 0, size_t * data_loc = nullptr);
size_t findServiceData(uint8_t index, uint8_t* bytes); size_t findServiceData(uint8_t index, uint8_t* bytes);
NimBLEAddress m_address = NimBLEAddress(""); NimBLEAddress m_address;
uint8_t m_advType; uint8_t m_advType;
int m_rssi; int m_rssi;
time_t m_timestamp; time_t m_timestamp;

View file

@ -643,15 +643,9 @@ bool NimBLEAdvertising::start(uint32_t duration, advCompleteCB_t advCompleteCB,
m_advDataSet = true; m_advDataSet = true;
} }
ble_addr_t peerAddr;
if (dirAddr != nullptr) {
memcpy(&peerAddr.val, dirAddr->getNative(), 6);
peerAddr.type = dirAddr->getType();
}
#if defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL) #if defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL)
rc = ble_gap_adv_start(NimBLEDevice::m_own_addr_type, rc = ble_gap_adv_start(NimBLEDevice::m_own_addr_type,
(dirAddr != nullptr) ? &peerAddr : NULL, (dirAddr != nullptr) ? dirAddr->getBase() : NULL,
duration, duration,
&m_advParams, &m_advParams,
(pServer != nullptr) ? NimBLEServer::handleGapEvent : (pServer != nullptr) ? NimBLEServer::handleGapEvent :

View file

@ -199,16 +199,14 @@ bool NimBLEClient::connect(const NimBLEAddress &address, bool deleteAttributes)
return false; return false;
} }
ble_addr_t peerAddr_t; const ble_addr_t* peerAddr = address.getBase();
memcpy(&peerAddr_t.val, address.getNative(),6); if(ble_gap_conn_find_by_addr(peerAddr, NULL) == 0) {
peerAddr_t.type = address.getType();
if(ble_gap_conn_find_by_addr(&peerAddr_t, NULL) == 0) {
NIMBLE_LOGE(LOG_TAG, "A connection to %s already exists", NIMBLE_LOGE(LOG_TAG, "A connection to %s already exists",
address.toString().c_str()); address.toString().c_str());
return false; return false;
} }
if(address == NimBLEAddress("")) { if(address.isNull()) {
NIMBLE_LOGE(LOG_TAG, "Invalid peer address;(NULL)"); NIMBLE_LOGE(LOG_TAG, "Invalid peer address;(NULL)");
return false; return false;
} else { } else {
@ -227,7 +225,7 @@ bool NimBLEClient::connect(const NimBLEAddress &address, bool deleteAttributes)
do { do {
#if CONFIG_BT_NIMBLE_EXT_ADV #if CONFIG_BT_NIMBLE_EXT_ADV
rc = ble_gap_ext_connect(NimBLEDevice::m_own_addr_type, rc = ble_gap_ext_connect(NimBLEDevice::m_own_addr_type,
&peerAddr_t, peerAddr,
m_connectTimeout, m_connectTimeout,
m_phyMask, m_phyMask,
&m_pConnParams, &m_pConnParams,
@ -237,7 +235,7 @@ bool NimBLEClient::connect(const NimBLEAddress &address, bool deleteAttributes)
this); this);
#else #else
rc = ble_gap_connect(NimBLEDevice::m_own_addr_type, &peerAddr_t, rc = ble_gap_connect(NimBLEDevice::m_own_addr_type, peerAddr,
m_connectTimeout, &m_pConnParams, m_connectTimeout, &m_pConnParams,
NimBLEClient::handleGapEvent, this); NimBLEClient::handleGapEvent, this);
#endif #endif
@ -557,7 +555,7 @@ uint16_t NimBLEClient::getConnId() {
void NimBLEClient::clearConnection() { void NimBLEClient::clearConnection() {
m_conn_id = BLE_HS_CONN_HANDLE_NONE; m_conn_id = BLE_HS_CONN_HANDLE_NONE;
m_connEstablished = false; m_connEstablished = false;
m_peerAddress = NimBLEAddress(); m_peerAddress = NimBLEAddress{};
} // clearConnection } // clearConnection
/** /**

View file

@ -56,6 +56,8 @@
#include "NimBLELog.h" #include "NimBLELog.h"
#include <algorithm>
static const char* LOG_TAG = "NimBLEDevice"; static const char* LOG_TAG = "NimBLEDevice";
/** /**
@ -453,7 +455,7 @@ int NimBLEDevice::getPower() {
*/ */
/* STATIC*/ /* STATIC*/
NimBLEAddress NimBLEDevice::getAddress() { NimBLEAddress NimBLEDevice::getAddress() {
ble_addr_t addr = {BLE_ADDR_PUBLIC, 0}; ble_addr_t addr{};
if(BLE_HS_ENOADDR == ble_hs_id_copy_addr(BLE_ADDR_PUBLIC, addr.val, NULL)) { if(BLE_HS_ENOADDR == ble_hs_id_copy_addr(BLE_ADDR_PUBLIC, addr.val, NULL)) {
NIMBLE_LOGD(LOG_TAG, "Public address not found, checking random"); NIMBLE_LOGD(LOG_TAG, "Public address not found, checking random");
@ -596,16 +598,7 @@ bool NimBLEDevice::deleteAllBonds() {
*/ */
/*STATIC*/ /*STATIC*/
bool NimBLEDevice::deleteBond(const NimBLEAddress &address) { bool NimBLEDevice::deleteBond(const NimBLEAddress &address) {
ble_addr_t delAddr; return ble_gap_unpair(address.getBase()) == 0;
memcpy(&delAddr.val, address.getNative(),6);
delAddr.type = address.getType();
int rc = ble_gap_unpair(&delAddr);
if (rc != 0) {
return false;
}
return true;
} }
@ -682,27 +675,15 @@ bool NimBLEDevice::onWhiteList(const NimBLEAddress & address) {
*/ */
/*STATIC*/ /*STATIC*/
bool NimBLEDevice::whiteListAdd(const NimBLEAddress & address) { bool NimBLEDevice::whiteListAdd(const NimBLEAddress & address) {
if (NimBLEDevice::onWhiteList(address)) { if (!NimBLEDevice::onWhiteList(address)) {
return true;
}
m_whiteList.push_back(address); m_whiteList.push_back(address);
std::vector<ble_addr_t> wlVec; int rc = ble_gap_wl_set(reinterpret_cast<ble_addr_t*>(&m_whiteList[0]), m_whiteList.size());
wlVec.reserve(m_whiteList.size());
for (auto &it : m_whiteList) {
ble_addr_t wlAddr;
memcpy(&wlAddr.val, it.getNative(), 6);
wlAddr.type = it.getType();
wlVec.push_back(wlAddr);
}
int rc = ble_gap_wl_set(&wlVec[0], wlVec.size());
if (rc != 0) { if (rc != 0) {
NIMBLE_LOGE(LOG_TAG, "Failed adding to whitelist rc=%d", rc); NIMBLE_LOGE(LOG_TAG, "Failed adding to whitelist rc=%d", rc);
m_whiteList.pop_back(); m_whiteList.pop_back();
return false; return false;
} }
}
return true; return true;
} }
@ -715,34 +696,15 @@ bool NimBLEDevice::whiteListAdd(const NimBLEAddress & address) {
*/ */
/*STATIC*/ /*STATIC*/
bool NimBLEDevice::whiteListRemove(const NimBLEAddress & address) { bool NimBLEDevice::whiteListRemove(const NimBLEAddress & address) {
if (!NimBLEDevice::onWhiteList(address)) { auto it = std::find(m_whiteList.begin(), m_whiteList.end(), address);
return true; if (it != m_whiteList.end()) {
} m_whiteList.erase(it);
int rc = ble_gap_wl_set(reinterpret_cast<ble_addr_t*>(&m_whiteList[0]), m_whiteList.size());
std::vector<ble_addr_t> wlVec;
wlVec.reserve(m_whiteList.size());
for (auto &it : m_whiteList) {
if (it != address) {
ble_addr_t wlAddr;
memcpy(&wlAddr.val, it.getNative(), 6);
wlAddr.type = it.getType();
wlVec.push_back(wlAddr);
}
}
int rc = ble_gap_wl_set(&wlVec[0], wlVec.size());
if (rc != 0) { if (rc != 0) {
m_whiteList.push_back(address);
NIMBLE_LOGE(LOG_TAG, "Failed removing from whitelist rc=%d", rc); NIMBLE_LOGE(LOG_TAG, "Failed removing from whitelist rc=%d", rc);
return false; return false;
} }
// Don't remove from the list unless NimBLE returned success
for (auto it = m_whiteList.begin(); it < m_whiteList.end(); ++it) {
if ((*it) == address) {
m_whiteList.erase(it);
break;
}
} }
return true; return true;

View file

@ -159,7 +159,7 @@ public:
#endif #endif
#if defined( CONFIG_BT_NIMBLE_ROLE_CENTRAL) #if defined( CONFIG_BT_NIMBLE_ROLE_CENTRAL)
static NimBLEClient* createClient(NimBLEAddress peerAddress = NimBLEAddress("")); static NimBLEClient* createClient(NimBLEAddress peerAddress = NimBLEAddress{});
static bool deleteClient(NimBLEClient* pClient); static bool deleteClient(NimBLEClient* pClient);
static NimBLEClient* getClientByID(uint16_t conn_id); static NimBLEClient* getClientByID(uint16_t conn_id);
static NimBLEClient* getClientByPeerAddress(const NimBLEAddress &peer_addr); static NimBLEClient* getClientByPeerAddress(const NimBLEAddress &peer_addr);

View file

@ -100,12 +100,8 @@ bool NimBLEExtAdvertising::setInstanceData(uint8_t inst_id, NimBLEExtAdvertiseme
if (rc != 0) { if (rc != 0) {
NIMBLE_LOGE(LOG_TAG, "Invalid advertisement data: rc = %d", rc); NIMBLE_LOGE(LOG_TAG, "Invalid advertisement data: rc = %d", rc);
} else { } else {
if (adv.m_advAddress != NimBLEAddress("")) { if (!adv.m_advAddress.isNull()) {
ble_addr_t addr; rc = ble_gap_ext_adv_set_addr(inst_id, adv.m_advAddress.getBase());
memcpy(&addr.val, adv.m_advAddress.getNative(), 6);
// Custom advertising address must be random.
addr.type = BLE_OWN_ADDR_RANDOM;
rc = ble_gap_ext_adv_set_addr(inst_id, &addr);
} }
if (rc != 0) { if (rc != 0) {
@ -388,7 +384,7 @@ void NimBLEExtAdvertisingCallbacks::onScanRequest(NimBLEExtAdvertising *pAdv,
* * BLE_HCI_LE_PHY_CODED * * BLE_HCI_LE_PHY_CODED
*/ */
NimBLEExtAdvertisement::NimBLEExtAdvertisement(uint8_t priPhy, uint8_t secPhy) NimBLEExtAdvertisement::NimBLEExtAdvertisement(uint8_t priPhy, uint8_t secPhy)
: m_advAddress("") : m_advAddress{}
{ {
memset (&m_params, 0, sizeof(m_params)); memset (&m_params, 0, sizeof(m_params));
m_params.own_addr_type = NimBLEDevice::m_own_addr_type; m_params.own_addr_type = NimBLEDevice::m_own_addr_type;
@ -493,10 +489,7 @@ void NimBLEExtAdvertisement::setScanFilter(bool scanRequestWhitelistOnly, bool c
* @param [in] addr The address of the peer to direct the advertisements. * @param [in] addr The address of the peer to direct the advertisements.
*/ */
void NimBLEExtAdvertisement::setDirectedPeer(const NimBLEAddress & addr) { void NimBLEExtAdvertisement::setDirectedPeer(const NimBLEAddress & addr) {
ble_addr_t peerAddr; m_params.peer = *addr.getBase();
memcpy(&peerAddr.val, addr.getNative(), 6);
peerAddr.type = addr.getType();
m_params.peer = peerAddr;
} // setDirectedPeer } // setDirectedPeer

View file

@ -28,6 +28,7 @@
#endif #endif
#include <limits.h> #include <limits.h>
#include <algorithm>
#define NIMBLE_SERVER_GET_PEER_NAME_ON_CONNECT_CB 0 #define NIMBLE_SERVER_GET_PEER_NAME_ON_CONNECT_CB 0
#define NIMBLE_SERVER_GET_PEER_NAME_ON_AUTH_CB 1 #define NIMBLE_SERVER_GET_PEER_NAME_ON_AUTH_CB 1
@ -324,12 +325,8 @@ NimBLEConnInfo NimBLEServer::getPeerInfo(size_t index) {
* @param [in] address The address of the peer. * @param [in] address The address of the peer.
*/ */
NimBLEConnInfo NimBLEServer::getPeerInfo(const NimBLEAddress& address) { NimBLEConnInfo NimBLEServer::getPeerInfo(const NimBLEAddress& address) {
ble_addr_t peerAddr;
memcpy(&peerAddr.val, address.getNative(),6);
peerAddr.type = address.getType();
NimBLEConnInfo peerInfo; NimBLEConnInfo peerInfo;
int rc = ble_gap_conn_find_by_addr(&peerAddr, &peerInfo.m_desc); int rc = ble_gap_conn_find_by_addr(address.getBase(), &peerInfo.m_desc);
if (rc != 0) { if (rc != 0) {
NIMBLE_LOGE(LOG_TAG, "Peer info not found"); NIMBLE_LOGE(LOG_TAG, "Peer info not found");
} }