Store address type in NimBLEAddress. (#25)

This commit is contained in:
h2zero 2020-07-30 14:57:47 -06:00 committed by GitHub
parent abdf6cda35
commit 1a5beaa7c0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 44 additions and 41 deletions

View file

@ -33,6 +33,7 @@ static const char* LOG_TAG = "NimBLEAddress";
*/ */
NimBLEAddress::NimBLEAddress(ble_addr_t address) { NimBLEAddress::NimBLEAddress(ble_addr_t address) {
memcpy(m_address, address.val, 6); memcpy(m_address, address.val, 6);
m_addrType = address.type;
} // NimBLEAddress } // NimBLEAddress
@ -46,8 +47,11 @@ NimBLEAddress::NimBLEAddress(ble_addr_t address) {
* which is 17 characters in length. * which is 17 characters in length.
* *
* @param [in] stringAddress 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.
*/ */
NimBLEAddress::NimBLEAddress(const std::string &stringAddress) { NimBLEAddress::NimBLEAddress(const std::string &stringAddress, uint8_t type) {
m_addrType = type;
if (stringAddress.length() == 0) { if (stringAddress.length() == 0) {
memset(m_address, 0, 6); memset(m_address, 0, 6);
return; return;
@ -78,9 +82,11 @@ NimBLEAddress::NimBLEAddress(const std::string &stringAddress) {
/** /**
* @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.
*/ */
NimBLEAddress::NimBLEAddress(uint8_t address[6]) { NimBLEAddress::NimBLEAddress(uint8_t address[6], uint8_t type) {
std::reverse_copy(address, address + sizeof m_address, m_address); std::reverse_copy(address, address + sizeof m_address, m_address);
m_addrType = type;
} // NimBLEAddress } // NimBLEAddress
@ -88,9 +94,11 @@ NimBLEAddress::NimBLEAddress(uint8_t address[6]) {
* @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"
* @param [in] address uint64_t containing the address. * @param [in] address uint64_t containing the address.
* @param [in] type The type of the address.
*/ */
NimBLEAddress::NimBLEAddress(const uint64_t &address) { NimBLEAddress::NimBLEAddress(const uint64_t &address, uint8_t type) {
memcpy(m_address, &address, sizeof m_address); memcpy(m_address, &address, sizeof m_address);
m_addrType = type;
} // NimBLEAddress } // NimBLEAddress
@ -113,6 +121,15 @@ const uint8_t *NimBLEAddress::getNative() const {
} // getNative } // getNative
/**
* @brief Get the address type.
* @return The address type.
*/
uint8_t NimBLEAddress::getType() const {
return m_addrType;
} // getType
/** /**
* @brief Convert a BLE address to a string. * @brief Convert a BLE address to a string.
* *
@ -152,8 +169,11 @@ bool NimBLEAddress::operator !=(const NimBLEAddress & rhs) const {
* that accept std::string and/or 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[26];
sprintf(buffer, "%02x:%02x:%02x:%02x:%02x:%02x", m_address[5], m_address[4], m_address[3], m_address[2], m_address[1], m_address[0]); snprintf(buffer, sizeof(buffer), "%02x:%02x:%02x:%02x:%02x:%02x type: %d",
m_address[5], m_address[4], m_address[3],
m_address[2], m_address[1], m_address[0],
m_addrType);
return std::string(buffer); return std::string(buffer);
} // operator std::string } // operator std::string

View file

@ -34,12 +34,13 @@
class NimBLEAddress { class NimBLEAddress {
public: public:
NimBLEAddress(ble_addr_t address); NimBLEAddress(ble_addr_t address);
NimBLEAddress(uint8_t address[6]); NimBLEAddress(uint8_t address[6], uint8_t type = BLE_ADDR_PUBLIC);
NimBLEAddress(const std::string &stringAddress); NimBLEAddress(const std::string &stringAddress, uint8_t type = BLE_ADDR_PUBLIC);
NimBLEAddress(const uint64_t &address); NimBLEAddress(const uint64_t &address, uint8_t type = BLE_ADDR_PUBLIC);
bool equals(const NimBLEAddress &otherAddress) const; bool equals(const NimBLEAddress &otherAddress) const;
const uint8_t* getNative() const; const uint8_t* getNative() const;
std::string toString() const; std::string toString() const;
uint8_t getType() const;
bool operator ==(const NimBLEAddress & rhs) const; bool operator ==(const NimBLEAddress & rhs) const;
bool operator !=(const NimBLEAddress & rhs) const; bool operator !=(const NimBLEAddress & rhs) const;
@ -48,6 +49,7 @@ public:
private: private:
uint8_t m_address[6]; uint8_t m_address[6];
uint8_t m_addrType;
}; };
#endif /* CONFIG_BT_ENABLED */ #endif /* CONFIG_BT_ENABLED */

View file

@ -70,7 +70,7 @@ NimBLEAddress NimBLEAdvertisedDevice::getAddress() {
*/ */
uint8_t NimBLEAdvertisedDevice::getAdvType() { uint8_t NimBLEAdvertisedDevice::getAdvType() {
return m_advType; return m_advType;
} // getAddress } // getAdvType
/** /**
@ -584,7 +584,7 @@ uint8_t* NimBLEAdvertisedDevice::getPayload() {
* * BLE_ADDR_RANDOM_ID (0x03) * * BLE_ADDR_RANDOM_ID (0x03)
*/ */
uint8_t NimBLEAdvertisedDevice::getAddressType() { uint8_t NimBLEAdvertisedDevice::getAddressType() {
return m_addressType; return m_address.getType();
} // getAddressType } // getAddressType
@ -597,19 +597,6 @@ time_t NimBLEAdvertisedDevice::getTimestamp() {
} // getTimestamp } // getTimestamp
/**
* @brief Set the advertised device address type.
* @param [in] type The address type of the device:
* * BLE_ADDR_PUBLIC (0x00)
* * BLE_ADDR_RANDOM (0x01)
* * BLE_ADDR_PUBLIC_ID (0x02)
* * BLE_ADDR_RANDOM_ID (0x03)
*/
void NimBLEAdvertisedDevice::setAddressType(uint8_t type) {
m_addressType = type;
} // setAddressType
/** /**
* @brief Get the length of the payload advertised by the device. * @brief Get the length of the payload advertised by the device.
* @return The size of the payload in bytes. * @return The size of the payload in bytes.

View file

@ -111,9 +111,6 @@ public:
size_t getPayloadLength(); size_t getPayloadLength();
uint8_t getAddressType(); uint8_t getAddressType();
time_t getTimestamp(); time_t getTimestamp();
void setAddressType(uint8_t type);
bool isAdvertisingService(const NimBLEUUID &uuid) const; bool isAdvertisingService(const NimBLEUUID &uuid) const;
bool haveAppearance(); bool haveAppearance();
bool haveManufacturerData(); bool haveManufacturerData();
@ -122,7 +119,6 @@ public:
bool haveServiceData(); bool haveServiceData();
bool haveServiceUUID(); bool haveServiceUUID();
bool haveTXPower(); bool haveTXPower();
std::string toString(); std::string toString();
private: private:
@ -158,7 +154,6 @@ private:
int8_t m_txPower; int8_t m_txPower;
uint8_t* m_payload; uint8_t* m_payload;
size_t m_payloadLength; size_t m_payloadLength;
uint8_t m_addressType;
time_t m_timestamp; time_t m_timestamp;
bool m_callbackSent; bool m_callbackSent;

View file

@ -90,7 +90,6 @@ NimBLEScan::~NimBLEScan() {
// Otherwise just update the relevant parameters of the already known device. // Otherwise just update the relevant parameters of the already known device.
if(advertisedDevice == nullptr){ if(advertisedDevice == nullptr){
advertisedDevice = new NimBLEAdvertisedDevice(); advertisedDevice = new NimBLEAdvertisedDevice();
advertisedDevice->setAddressType(event->disc.addr.type);
advertisedDevice->setAddress(advertisedAddress); advertisedDevice->setAddress(advertisedAddress);
advertisedDevice->setAdvType(event->disc.event_type); advertisedDevice->setAdvType(event->disc.event_type);
pScan->m_scanResults.m_advertisedDevicesVector.push_back(advertisedDevice); pScan->m_scanResults.m_advertisedDevicesVector.push_back(advertisedDevice);