Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 22 additions & 10 deletions libraries/BLE/src/BLEAddress.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,7 @@

BLEAddress::BLEAddress() {
memset(m_address, 0, ESP_BD_ADDR_LEN);
#if defined(CONFIG_NIMBLE_ENABLED)
m_addrType = 0;
#endif
}

/**
Expand All @@ -63,11 +61,9 @@ bool BLEAddress::equals(const BLEAddress &otherAddress) const {
}

bool BLEAddress::operator==(const BLEAddress &otherAddress) const {
#if defined(CONFIG_NIMBLE_ENABLED)
if (m_addrType != otherAddress.m_addrType) {
return false;
}
#endif
return memcmp(otherAddress.m_address, m_address, ESP_BD_ADDR_LEN) == 0;
}

Expand Down Expand Up @@ -99,6 +95,22 @@ uint8_t *BLEAddress::getNative() {
return m_address;
}

/**
* @brief Return the address type.
* @return The address type.
*/
uint8_t BLEAddress::getType() const {
return m_addrType;
}

/**
* @brief Set the address type.
* @param [in] type The address type.
*/
void BLEAddress::setType(uint8_t type) {
m_addrType = type;
}

/**
* @brief Convert a BLE address to a string.
*
Expand Down Expand Up @@ -135,9 +147,11 @@ String BLEAddress::toString() const {
/**
* @brief Create an address from the native ESP32 representation.
* @param [in] address The native representation.
* @param [in] type The address type.
*/
BLEAddress::BLEAddress(esp_bd_addr_t address) {
BLEAddress::BLEAddress(esp_bd_addr_t address, uint8_t type) {
memcpy(m_address, address, ESP_BD_ADDR_LEN);
m_addrType = type;
}

/**
Expand All @@ -150,13 +164,15 @@ BLEAddress::BLEAddress(esp_bd_addr_t address) {
* which is 17 characters in length.
*
* @param [in] stringAddress The hex representation of the address.
* @param [in] type The address type.
*/
BLEAddress::BLEAddress(const String &stringAddress) {
BLEAddress::BLEAddress(const String &stringAddress, uint8_t type) {
if (stringAddress.length() != 17) {
return;
}

int data[6];
m_addrType = type;
sscanf(stringAddress.c_str(), "%x:%x:%x:%x:%x:%x", &data[0], &data[1], &data[2], &data[3], &data[4], &data[5]);

for (size_t index = 0; index < sizeof(m_address); index++) {
Expand Down Expand Up @@ -186,10 +202,6 @@ BLEAddress::BLEAddress(ble_addr_t address) {
m_addrType = address.type;
}

uint8_t BLEAddress::getType() const {
return m_addrType;
}

/**
* @brief Create an address from a hex string
*
Expand Down
14 changes: 4 additions & 10 deletions libraries/BLE/src/BLEAddress.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,15 +72,17 @@ class BLEAddress {
bool operator>(const BLEAddress &otherAddress) const;
bool operator>=(const BLEAddress &otherAddress) const;
uint8_t *getNative();
uint8_t getType() const;
void setType(uint8_t type);
String toString() const;

/***************************************************************************
* Bluedroid public declarations *
***************************************************************************/

#if defined(CONFIG_BLUEDROID_ENABLED)
BLEAddress(esp_bd_addr_t address);
BLEAddress(const String &stringAddress);
BLEAddress(esp_bd_addr_t address, uint8_t type = 0);
BLEAddress(const String &stringAddress, uint8_t type = 0);
#endif

/***************************************************************************
Expand All @@ -91,7 +93,6 @@ class BLEAddress {
BLEAddress(ble_addr_t address);
BLEAddress(const String &stringAddress, uint8_t type = BLE_ADDR_PUBLIC);
BLEAddress(uint8_t address[ESP_BD_ADDR_LEN], uint8_t type = BLE_ADDR_PUBLIC);
uint8_t getType() const;
#endif

private:
Expand All @@ -100,14 +101,7 @@ class BLEAddress {
***************************************************************************/

uint8_t m_address[ESP_BD_ADDR_LEN];

/***************************************************************************
* NimBLE private properties *
***************************************************************************/

#if defined(CONFIG_NIMBLE_ENABLED)
uint8_t m_addrType;
#endif
};

#endif /* CONFIG_BLUEDROID_ENABLED || CONFIG_NIMBLE_ENABLED */
Expand Down
6 changes: 2 additions & 4 deletions libraries/BLE/src/BLEAdvertisedDevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ BLEAdvertisedDevice::BLEAdvertisedDevice(const BLEAdvertisedDevice &other) {
m_pScan = other.m_pScan;
m_advType = other.m_advType;
m_address = other.m_address;
m_addressType = other.m_addressType;

#if defined(CONFIG_NIMBLE_ENABLED)
m_callbackSent = other.m_callbackSent;
Expand Down Expand Up @@ -130,7 +129,6 @@ BLEAdvertisedDevice &BLEAdvertisedDevice::operator=(const BLEAdvertisedDevice &o
m_pScan = other.m_pScan;
m_advType = other.m_advType;
m_address = other.m_address;
m_addressType = other.m_addressType;

#if defined(CONFIG_NIMBLE_ENABLED)
m_callbackSent = other.m_callbackSent;
Expand Down Expand Up @@ -768,7 +766,7 @@ uint8_t *BLEAdvertisedDevice::getPayload() {
}

uint8_t BLEAdvertisedDevice::getAddressType() {
return m_addressType;
return m_address.getType();
}

ble_frame_type_t BLEAdvertisedDevice::getFrameType() {
Expand All @@ -788,7 +786,7 @@ ble_frame_type_t BLEAdvertisedDevice::getFrameType() {
}

void BLEAdvertisedDevice::setAddressType(uint8_t type) {
m_addressType = type;
m_address.setType(type);
}

size_t BLEAdvertisedDevice::getPayloadLength() {
Expand Down
1 change: 0 additions & 1 deletion libraries/BLE/src/BLEAdvertisedDevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,6 @@ class BLEAdvertisedDevice {
std::vector<BLEUUID> m_serviceDataUUIDs;
uint8_t *m_payload;
size_t m_payloadLength = 0;
uint8_t m_addressType;
uint8_t m_advType;
bool m_isLegacyAdv;

Expand Down
12 changes: 11 additions & 1 deletion libraries/BLE/src/BLEClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,11 @@ void BLEClientCallbacks::onDisconnect(BLEClient *pClient) {
bool BLEClient::connect(BLEAddress address, uint8_t type, uint32_t timeoutMs) {
log_i(">> connect(%s)", address.toString().c_str());

// Use explicitly provided type, or fall back to the address's stored type
if (type == 0xFF) {
type = address.getType();
}

// Configuration for retry logic
const int maxRetries = 3;
const int retryDelayMs = 100;
Expand Down Expand Up @@ -893,9 +898,14 @@ bool BLEClient::connect(BLEAddress address, uint8_t type, uint32_t timeoutMs) {
return false;
}

// Use explicitly provided type, or fall back to the address's stored type
if (type == 0xFF) {
type = address.getType();
}

ble_addr_t peerAddr_t;
memcpy(&peerAddr_t.val, address.getNative(), 6);
peerAddr_t.type = address.getType();
peerAddr_t.type = type;
if (ble_gap_conn_find_by_addr(&peerAddr_t, NULL) == 0) {
log_e("A connection to %s already exists", address.toString().c_str());
return false;
Expand Down
2 changes: 1 addition & 1 deletion libraries/BLE/src/BLEClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ class BLEClient {
~BLEClient();
bool connect(BLEAdvertisedDevice *device);
bool connectTimeout(BLEAdvertisedDevice *device, uint32_t timeoutMS = portMAX_DELAY);
bool connect(BLEAddress address, uint8_t type = 0, uint32_t timeoutMS = portMAX_DELAY);
bool connect(BLEAddress address, uint8_t type = 0xFF, uint32_t timeoutMS = portMAX_DELAY);
bool secureConnection();
int disconnect(uint8_t reason = BLE_ERR_REM_USER_CONN_TERM);
BLEAddress getPeerAddress();
Expand Down
2 changes: 1 addition & 1 deletion libraries/BLE/src/BLEScan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ void BLEScan::handleGAPEvent(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_

// Examine our list of previously scanned addresses and, if we found this one already,
// ignore it.
BLEAddress advertisedAddress(param->scan_rst.bda);
BLEAddress advertisedAddress(param->scan_rst.bda, param->scan_rst.ble_addr_type);
bool found = false;
bool shouldDelete = true;

Expand Down
Loading