Skip to content
This repository was archived by the owner on Apr 23, 2023. It is now read-only.

Commit b34f44b

Browse files
committed
1 parent b10c85a commit b34f44b

File tree

8 files changed

+1120
-1278
lines changed

8 files changed

+1120
-1278
lines changed

.vscode/settings.json

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"files.associations": {
3+
"*.txt": "plaintext",
4+
"*.tcc": "cpp",
5+
"optional": "cpp",
6+
"istream": "cpp",
7+
"ostream": "cpp",
8+
"ratio": "cpp",
9+
"system_error": "cpp",
10+
"array": "cpp",
11+
"functional": "cpp",
12+
"regex": "cpp",
13+
"tuple": "cpp",
14+
"type_traits": "cpp",
15+
"utility": "cpp"
16+
}
17+
}

cpp_utils/BLEAddress.cpp

+36-39
Original file line numberDiff line numberDiff line change
@@ -7,25 +7,26 @@
77
#include "sdkconfig.h"
88
#if defined(CONFIG_BT_ENABLED)
99

10-
#include "BLEAddress.h"
11-
#include <string>
12-
#include <sstream>
13-
#include <iomanip>
14-
#include <string.h>
1510
#include <stdio.h>
11+
#include <string.h>
12+
13+
#include <iomanip>
14+
#include <sstream>
15+
#include <string>
16+
17+
#include "BLEAddress.h"
1618
#ifdef ARDUINO_ARCH_ESP32
1719
#include "esp32-hal-log.h"
1820
#endif
1921

20-
2122
/**
2223
* @brief Create an address from the native ESP32 representation.
2324
* @param [in] address The native representation.
2425
*/
25-
BLEAddress::BLEAddress(esp_bd_addr_t address) {
26-
memcpy(m_address, address, ESP_BD_ADDR_LEN);
27-
} // BLEAddress
28-
26+
BLEAddress::BLEAddress(esp_bd_addr_t address, esp_ble_wl_addr_type_t type) {
27+
m_type = type;
28+
memcpy(m_address, address, ESP_BD_ADDR_LEN);
29+
} // BLEAddress
2930

3031
/**
3132
* @brief Create an address from a hex string
@@ -38,38 +39,32 @@ BLEAddress::BLEAddress(esp_bd_addr_t address) {
3839
*
3940
* @param [in] stringAddress The hex representation of the address.
4041
*/
41-
BLEAddress::BLEAddress(std::string stringAddress) {
42-
if (stringAddress.length() != 17) return;
43-
44-
int data[6];
45-
sscanf(stringAddress.c_str(), "%x:%x:%x:%x:%x:%x", &data[0], &data[1], &data[2], &data[3], &data[4], &data[5]);
46-
m_address[0] = (uint8_t) data[0];
47-
m_address[1] = (uint8_t) data[1];
48-
m_address[2] = (uint8_t) data[2];
49-
m_address[3] = (uint8_t) data[3];
50-
m_address[4] = (uint8_t) data[4];
51-
m_address[5] = (uint8_t) data[5];
52-
} // BLEAddress
42+
BLEAddress::BLEAddress(std::string stringAddress, esp_ble_wl_addr_type_t type) {
43+
m_type = type;
44+
if (stringAddress.length() != 17) return;
5345

46+
int data[6];
47+
sscanf(stringAddress.c_str(), "%x:%x:%x:%x:%x:%x", &data[0], &data[1], &data[2], &data[3], &data[4], &data[5]);
48+
m_address[0] = (uint8_t)data[0];
49+
m_address[1] = (uint8_t)data[1];
50+
m_address[2] = (uint8_t)data[2];
51+
m_address[3] = (uint8_t)data[3];
52+
m_address[4] = (uint8_t)data[4];
53+
m_address[5] = (uint8_t)data[5];
54+
} // BLEAddress
5455

5556
/**
5657
* @brief Determine if this address equals another.
5758
* @param [in] otherAddress The other address to compare against.
5859
* @return True if the addresses are equal.
5960
*/
60-
bool BLEAddress::equals(BLEAddress otherAddress) {
61-
return memcmp(otherAddress.getNative(), m_address, 6) == 0;
62-
} // equals
63-
61+
bool BLEAddress::equals(BLEAddress otherAddress) { return memcmp(otherAddress.getNative(), m_address, 6) == 0 && m_type == otherAddress.m_type; } // equals
6462

6563
/**
6664
* @brief Return the native representation of the address.
6765
* @return The native representation of the address.
6866
*/
69-
esp_bd_addr_t *BLEAddress::getNative() {
70-
return &m_address;
71-
} // getNative
72-
67+
esp_bd_addr_t *BLEAddress::getNative() { return &m_address; } // getNative
7368

7469
/**
7570
* @brief Convert a BLE address to a string.
@@ -83,13 +78,15 @@ esp_bd_addr_t *BLEAddress::getNative() {
8378
* @return The string representation of the address.
8479
*/
8580
std::string BLEAddress::toString() {
86-
std::stringstream stream;
87-
stream << std::setfill('0') << std::setw(2) << std::hex << (int) ((uint8_t*) (m_address))[0] << ':';
88-
stream << std::setfill('0') << std::setw(2) << std::hex << (int) ((uint8_t*) (m_address))[1] << ':';
89-
stream << std::setfill('0') << std::setw(2) << std::hex << (int) ((uint8_t*) (m_address))[2] << ':';
90-
stream << std::setfill('0') << std::setw(2) << std::hex << (int) ((uint8_t*) (m_address))[3] << ':';
91-
stream << std::setfill('0') << std::setw(2) << std::hex << (int) ((uint8_t*) (m_address))[4] << ':';
92-
stream << std::setfill('0') << std::setw(2) << std::hex << (int) ((uint8_t*) (m_address))[5];
93-
return stream.str();
94-
} // toString
81+
std::stringstream stream;
82+
stream << std::setfill('0') << std::setw(2) << std::hex << (int)((uint8_t *)(m_address))[0] << ':';
83+
stream << std::setfill('0') << std::setw(2) << std::hex << (int)((uint8_t *)(m_address))[1] << ':';
84+
stream << std::setfill('0') << std::setw(2) << std::hex << (int)((uint8_t *)(m_address))[2] << ':';
85+
stream << std::setfill('0') << std::setw(2) << std::hex << (int)((uint8_t *)(m_address))[3] << ':';
86+
stream << std::setfill('0') << std::setw(2) << std::hex << (int)((uint8_t *)(m_address))[4] << ':';
87+
stream << std::setfill('0') << std::setw(2) << std::hex << (int)((uint8_t *)(m_address))[5];
88+
return stream.str();
89+
} // toString
90+
91+
esp_ble_wl_addr_type_t BLEAddress::getType() const { return m_type; }
9592
#endif

cpp_utils/BLEAddress.h

+12-10
Original file line numberDiff line numberDiff line change
@@ -9,25 +9,27 @@
99
#define COMPONENTS_CPP_UTILS_BLEADDRESS_H_
1010
#include "sdkconfig.h"
1111
#if defined(CONFIG_BT_ENABLED)
12-
#include <esp_gap_ble_api.h> // ESP32 BLE
13-
#include <string>
12+
#include <esp_gap_ble_api.h> // ESP32 BLE
1413

14+
#include <string>
1515

1616
/**
1717
* @brief A %BLE device address.
1818
*
1919
* Every %BLE device has a unique address which can be used to identify it and form connections.
2020
*/
2121
class BLEAddress {
22-
public:
23-
BLEAddress(esp_bd_addr_t address);
24-
BLEAddress(std::string stringAddress);
25-
bool equals(BLEAddress otherAddress);
26-
esp_bd_addr_t* getNative();
27-
std::string toString();
22+
public:
23+
BLEAddress(esp_bd_addr_t address, esp_ble_wl_addr_type_t type = BLE_WL_ADDR_TYPE_RANDOM);
24+
BLEAddress(std::string stringAddress, esp_ble_wl_addr_type_t type = BLE_WL_ADDR_TYPE_RANDOM);
25+
bool equals(BLEAddress otherAddress);
26+
esp_bd_addr_t* getNative();
27+
std::string toString();
28+
esp_ble_wl_addr_type_t getType() const;
2829

29-
private:
30-
esp_bd_addr_t m_address;
30+
private:
31+
esp_bd_addr_t m_address;
32+
esp_ble_wl_addr_type_t m_type;
3133
};
3234

3335
#endif /* CONFIG_BT_ENABLED */

0 commit comments

Comments
 (0)