Skip to content

Commit 113a873

Browse files
authored
Store address type in NimBLEAddress. (#104)
1 parent 05c68ed commit 113a873

File tree

5 files changed

+44
-41
lines changed

5 files changed

+44
-41
lines changed

src/NimBLEAddress.cpp

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ static const char* LOG_TAG = "NimBLEAddress";
3333
*/
3434
NimBLEAddress::NimBLEAddress(ble_addr_t address) {
3535
memcpy(m_address, address.val, 6);
36+
m_addrType = address.type;
3637
} // NimBLEAddress
3738

3839

@@ -46,8 +47,11 @@ NimBLEAddress::NimBLEAddress(ble_addr_t address) {
4647
* which is 17 characters in length.
4748
*
4849
* @param [in] stringAddress The hex string representation of the address.
50+
* @param [in] type The type of the address.
4951
*/
50-
NimBLEAddress::NimBLEAddress(const std::string &stringAddress) {
52+
NimBLEAddress::NimBLEAddress(const std::string &stringAddress, uint8_t type) {
53+
m_addrType = type;
54+
5155
if (stringAddress.length() == 0) {
5256
memset(m_address, 0, 6);
5357
return;
@@ -78,19 +82,23 @@ NimBLEAddress::NimBLEAddress(const std::string &stringAddress) {
7882
/**
7983
* @brief Constructor for compatibility with bluedroid esp library using native ESP representation.
8084
* @param [in] address A uint8_t[6] or esp_bd_addr_t containing the address.
85+
* @param [in] type The type of the address.
8186
*/
82-
NimBLEAddress::NimBLEAddress(uint8_t address[6]) {
87+
NimBLEAddress::NimBLEAddress(uint8_t address[6], uint8_t type) {
8388
std::reverse_copy(address, address + sizeof m_address, m_address);
89+
m_addrType = type;
8490
} // NimBLEAddress
8591

8692

8793
/**
8894
* @brief Constructor for address using a hex value.\n
8995
* Use the same byte order, so use 0xa4c1385def16 for "a4:c1:38:5d:ef:16"
9096
* @param [in] address uint64_t containing the address.
97+
* @param [in] type The type of the address.
9198
*/
92-
NimBLEAddress::NimBLEAddress(const uint64_t &address) {
99+
NimBLEAddress::NimBLEAddress(const uint64_t &address, uint8_t type) {
93100
memcpy(m_address, &address, sizeof m_address);
101+
m_addrType = type;
94102
} // NimBLEAddress
95103

96104

@@ -113,6 +121,15 @@ const uint8_t *NimBLEAddress::getNative() const {
113121
} // getNative
114122

115123

124+
/**
125+
* @brief Get the address type.
126+
* @return The address type.
127+
*/
128+
uint8_t NimBLEAddress::getType() const {
129+
return m_addrType;
130+
} // getType
131+
132+
116133
/**
117134
* @brief Convert a BLE address to a string.
118135
*
@@ -152,8 +169,11 @@ bool NimBLEAddress::operator !=(const NimBLEAddress & rhs) const {
152169
* that accept std::string and/or or it's methods as a parameter.
153170
*/
154171
NimBLEAddress::operator std::string() const {
155-
char buffer[18];
156-
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]);
172+
char buffer[26];
173+
snprintf(buffer, sizeof(buffer), "%02x:%02x:%02x:%02x:%02x:%02x type: %d",
174+
m_address[5], m_address[4], m_address[3],
175+
m_address[2], m_address[1], m_address[0],
176+
m_addrType);
157177
return std::string(buffer);
158178
} // operator std::string
159179

src/NimBLEAddress.h

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,13 @@
3434
class NimBLEAddress {
3535
public:
3636
NimBLEAddress(ble_addr_t address);
37-
NimBLEAddress(uint8_t address[6]);
38-
NimBLEAddress(const std::string &stringAddress);
39-
NimBLEAddress(const uint64_t &address);
40-
bool equals(const NimBLEAddress &otherAddress) const;
41-
const uint8_t* getNative() const;
42-
std::string toString() const;
37+
NimBLEAddress(uint8_t address[6], uint8_t type = BLE_ADDR_PUBLIC);
38+
NimBLEAddress(const std::string &stringAddress, uint8_t type = BLE_ADDR_PUBLIC);
39+
NimBLEAddress(const uint64_t &address, uint8_t type = BLE_ADDR_PUBLIC);
40+
bool equals(const NimBLEAddress &otherAddress) const;
41+
const uint8_t* getNative() const;
42+
std::string toString() const;
43+
uint8_t getType() const;
4344

4445
bool operator ==(const NimBLEAddress & rhs) const;
4546
bool operator !=(const NimBLEAddress & rhs) const;
@@ -48,6 +49,7 @@ class NimBLEAddress {
4849

4950
private:
5051
uint8_t m_address[6];
52+
uint8_t m_addrType;
5153
};
5254

5355
#endif /* CONFIG_BT_ENABLED */

src/NimBLEAdvertisedDevice.cpp

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ NimBLEAddress NimBLEAdvertisedDevice::getAddress() {
7070
*/
7171
uint8_t NimBLEAdvertisedDevice::getAdvType() {
7272
return m_advType;
73-
} // getAddress
73+
} // getAdvType
7474

7575

7676
/**
@@ -584,7 +584,7 @@ uint8_t* NimBLEAdvertisedDevice::getPayload() {
584584
* * BLE_ADDR_RANDOM_ID (0x03)
585585
*/
586586
uint8_t NimBLEAdvertisedDevice::getAddressType() {
587-
return m_addressType;
587+
return m_address.getType();
588588
} // getAddressType
589589

590590

@@ -597,19 +597,6 @@ time_t NimBLEAdvertisedDevice::getTimestamp() {
597597
} // getTimestamp
598598

599599

600-
/**
601-
* @brief Set the advertised device address type.
602-
* @param [in] type The address type of the device:
603-
* * BLE_ADDR_PUBLIC (0x00)
604-
* * BLE_ADDR_RANDOM (0x01)
605-
* * BLE_ADDR_PUBLIC_ID (0x02)
606-
* * BLE_ADDR_RANDOM_ID (0x03)
607-
*/
608-
void NimBLEAdvertisedDevice::setAddressType(uint8_t type) {
609-
m_addressType = type;
610-
} // setAddressType
611-
612-
613600
/**
614601
* @brief Get the length of the payload advertised by the device.
615602
* @return The size of the payload in bytes.

src/NimBLEAdvertisedDevice.h

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -111,19 +111,15 @@ class NimBLEAdvertisedDevice {
111111
size_t getPayloadLength();
112112
uint8_t getAddressType();
113113
time_t getTimestamp();
114-
void setAddressType(uint8_t type);
115-
116-
117-
bool isAdvertisingService(const NimBLEUUID &uuid) const;
118-
bool haveAppearance();
119-
bool haveManufacturerData();
120-
bool haveName();
121-
bool haveRSSI();
122-
bool haveServiceData();
123-
bool haveServiceUUID();
124-
bool haveTXPower();
125-
126-
std::string toString();
114+
bool isAdvertisingService(const NimBLEUUID &uuid) const;
115+
bool haveAppearance();
116+
bool haveManufacturerData();
117+
bool haveName();
118+
bool haveRSSI();
119+
bool haveServiceData();
120+
bool haveServiceUUID();
121+
bool haveTXPower();
122+
std::string toString();
127123

128124
private:
129125
friend class NimBLEScan;
@@ -158,7 +154,6 @@ class NimBLEAdvertisedDevice {
158154
int8_t m_txPower;
159155
uint8_t* m_payload;
160156
size_t m_payloadLength;
161-
uint8_t m_addressType;
162157
time_t m_timestamp;
163158
bool m_callbackSent;
164159

src/NimBLEScan.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,6 @@ NimBLEScan::~NimBLEScan() {
9090
// Otherwise just update the relevant parameters of the already known device.
9191
if(advertisedDevice == nullptr){
9292
advertisedDevice = new NimBLEAdvertisedDevice();
93-
advertisedDevice->setAddressType(event->disc.addr.type);
9493
advertisedDevice->setAddress(advertisedAddress);
9594
advertisedDevice->setAdvType(event->disc.event_type);
9695
pScan->m_scanResults.m_advertisedDevicesVector.push_back(advertisedDevice);

0 commit comments

Comments
 (0)