From c0a62c0be116e8afc3c399b89f3ce8e6e6eb11dd Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Wed, 6 Aug 2025 17:40:12 -1000 Subject: [PATCH 1/2] [esp32_ble_client] Avoid iterating empty services vector for bluetooth_proxy connections (#10110) --- .../esp32_ble_client/ble_client_base.cpp | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/esphome/components/esp32_ble_client/ble_client_base.cpp b/esphome/components/esp32_ble_client/ble_client_base.cpp index 2f78a479a3..a7c2ced397 100644 --- a/esphome/components/esp32_ble_client/ble_client_base.cpp +++ b/esphome/components/esp32_ble_client/ble_client_base.cpp @@ -376,7 +376,7 @@ bool BLEClientBase::gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_ this->service_count_++; if (this->connection_type_ == espbt::ConnectionType::V3_WITHOUT_CACHE) { // V3 clients don't need services initialized since - // they only request by handle after receiving the services. + // as they use the ESP APIs to get services. break; } BLEService *ble_service = new BLEService(); // NOLINT(cppcoreguidelines-owning-memory) @@ -391,21 +391,20 @@ bool BLEClientBase::gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_ if (this->conn_id_ != param->search_cmpl.conn_id) return false; this->log_gattc_event_("SEARCH_CMPL"); - for (auto &svc : this->services_) { - ESP_LOGV(TAG, "[%d] [%s] Service UUID: %s", this->connection_index_, this->address_str_.c_str(), - svc->uuid.to_string().c_str()); - ESP_LOGV(TAG, "[%d] [%s] start_handle: 0x%x end_handle: 0x%x", this->connection_index_, - this->address_str_.c_str(), svc->start_handle, svc->end_handle); - } - ESP_LOGI(TAG, "[%d] [%s] Service discovery complete", this->connection_index_, this->address_str_.c_str()); - // For V3 connections, restore to medium connection parameters after service discovery // This balances performance with bandwidth usage after the critical discovery phase if (this->connection_type_ == espbt::ConnectionType::V3_WITHOUT_CACHE || this->connection_type_ == espbt::ConnectionType::V3_WITH_CACHE) { this->restore_medium_conn_params_(); + } else { + for (auto &svc : this->services_) { + ESP_LOGV(TAG, "[%d] [%s] Service UUID: %s", this->connection_index_, this->address_str_.c_str(), + svc->uuid.to_string().c_str()); + ESP_LOGV(TAG, "[%d] [%s] start_handle: 0x%x end_handle: 0x%x", this->connection_index_, + this->address_str_.c_str(), svc->start_handle, svc->end_handle); + } } - + ESP_LOGI(TAG, "[%d] [%s] Service discovery complete", this->connection_index_, this->address_str_.c_str()); this->state_ = espbt::ClientState::ESTABLISHED; break; } From c5065f21b80b74f2a248c0a3d0e2189966395596 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Wed, 6 Aug 2025 19:53:35 -1000 Subject: [PATCH 2/2] condtional --- .../components/esp32_ble_client/ble_characteristic.cpp | 2 ++ esphome/components/esp32_ble_client/ble_characteristic.h | 4 ++++ esphome/components/esp32_ble_client/ble_client_base.cpp | 8 ++++++++ esphome/components/esp32_ble_client/ble_client_base.h | 6 ++++++ esphome/components/esp32_ble_client/ble_descriptor.h | 4 ++++ esphome/components/esp32_ble_client/ble_service.cpp | 2 ++ esphome/components/esp32_ble_client/ble_service.h | 4 ++++ 7 files changed, 30 insertions(+) diff --git a/esphome/components/esp32_ble_client/ble_characteristic.cpp b/esphome/components/esp32_ble_client/ble_characteristic.cpp index 8a3d313303..36229c23c3 100644 --- a/esphome/components/esp32_ble_client/ble_characteristic.cpp +++ b/esphome/components/esp32_ble_client/ble_characteristic.cpp @@ -5,6 +5,7 @@ #include "esphome/core/log.h" #ifdef USE_ESP32 +#ifdef USE_ESP32_BLE_DEVICE namespace esphome::esp32_ble_client { @@ -94,4 +95,5 @@ esp_err_t BLECharacteristic::write_value(uint8_t *new_val, int16_t new_val_size) } // namespace esphome::esp32_ble_client +#endif // USE_ESP32_BLE_DEVICE #endif // USE_ESP32 diff --git a/esphome/components/esp32_ble_client/ble_characteristic.h b/esphome/components/esp32_ble_client/ble_characteristic.h index d55e69f47a..1428b42739 100644 --- a/esphome/components/esp32_ble_client/ble_characteristic.h +++ b/esphome/components/esp32_ble_client/ble_characteristic.h @@ -1,6 +1,9 @@ #pragma once +#include "esphome/core/defines.h" + #ifdef USE_ESP32 +#ifdef USE_ESP32_BLE_DEVICE #include "esphome/components/esp32_ble_tracker/esp32_ble_tracker.h" @@ -34,4 +37,5 @@ class BLECharacteristic { } // namespace esphome::esp32_ble_client +#endif // USE_ESP32_BLE_DEVICE #endif // USE_ESP32 diff --git a/esphome/components/esp32_ble_client/ble_client_base.cpp b/esphome/components/esp32_ble_client/ble_client_base.cpp index a7c2ced397..37b41326f7 100644 --- a/esphome/components/esp32_ble_client/ble_client_base.cpp +++ b/esphome/components/esp32_ble_client/ble_client_base.cpp @@ -209,9 +209,11 @@ void BLEClientBase::unconditional_disconnect() { } void BLEClientBase::release_services() { +#ifdef USE_ESP32_BLE_DEVICE for (auto &svc : this->services_) delete svc; // NOLINT(cppcoreguidelines-owning-memory) this->services_.clear(); +#endif #ifndef CONFIG_BT_GATTC_CACHE_NVS_FLASH esp_ble_gattc_cache_clean(this->remote_bda_); #endif @@ -379,12 +381,14 @@ bool BLEClientBase::gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_ // as they use the ESP APIs to get services. break; } +#ifdef USE_ESP32_BLE_DEVICE BLEService *ble_service = new BLEService(); // NOLINT(cppcoreguidelines-owning-memory) ble_service->uuid = espbt::ESPBTUUID::from_uuid(param->search_res.srvc_id.uuid); ble_service->start_handle = param->search_res.start_handle; ble_service->end_handle = param->search_res.end_handle; ble_service->client = this; this->services_.push_back(ble_service); +#endif break; } case ESP_GATTC_SEARCH_CMPL_EVT: { @@ -397,12 +401,14 @@ bool BLEClientBase::gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_ this->connection_type_ == espbt::ConnectionType::V3_WITH_CACHE) { this->restore_medium_conn_params_(); } else { +#ifdef USE_ESP32_BLE_DEVICE for (auto &svc : this->services_) { ESP_LOGV(TAG, "[%d] [%s] Service UUID: %s", this->connection_index_, this->address_str_.c_str(), svc->uuid.to_string().c_str()); ESP_LOGV(TAG, "[%d] [%s] start_handle: 0x%x end_handle: 0x%x", this->connection_index_, this->address_str_.c_str(), svc->start_handle, svc->end_handle); } +#endif } ESP_LOGI(TAG, "[%d] [%s] Service discovery complete", this->connection_index_, this->address_str_.c_str()); this->state_ = espbt::ClientState::ESTABLISHED; @@ -581,6 +587,7 @@ float BLEClientBase::parse_char_value(uint8_t *value, uint16_t length) { return NAN; } +#ifdef USE_ESP32_BLE_DEVICE BLEService *BLEClientBase::get_service(espbt::ESPBTUUID uuid) { for (auto *svc : this->services_) { if (svc->uuid == uuid) @@ -657,6 +664,7 @@ BLEDescriptor *BLEClientBase::get_descriptor(uint16_t handle) { } return nullptr; } +#endif // USE_ESP32_BLE_DEVICE } // namespace esphome::esp32_ble_client diff --git a/esphome/components/esp32_ble_client/ble_client_base.h b/esphome/components/esp32_ble_client/ble_client_base.h index 6bdf84e18f..093d04640b 100644 --- a/esphome/components/esp32_ble_client/ble_client_base.h +++ b/esphome/components/esp32_ble_client/ble_client_base.h @@ -5,7 +5,9 @@ #include "esphome/components/esp32_ble_tracker/esp32_ble_tracker.h" #include "esphome/core/component.h" +#ifdef USE_ESP32_BLE_DEVICE #include "ble_service.h" +#endif #include #include @@ -67,6 +69,7 @@ class BLEClientBase : public espbt::ESPBTClient, public Component { } const std::string &address_str() const { return this->address_str_; } +#ifdef USE_ESP32_BLE_DEVICE BLEService *get_service(espbt::ESPBTUUID uuid); BLEService *get_service(uint16_t uuid); BLECharacteristic *get_characteristic(espbt::ESPBTUUID service, espbt::ESPBTUUID chr); @@ -77,6 +80,7 @@ class BLEClientBase : public espbt::ESPBTClient, public Component { BLEDescriptor *get_descriptor(uint16_t handle); // Get the configuration descriptor for the given characteristic handle. BLEDescriptor *get_config_descriptor(uint16_t handle); +#endif float parse_char_value(uint8_t *value, uint16_t length); @@ -103,7 +107,9 @@ class BLEClientBase : public espbt::ESPBTClient, public Component { // Group 2: Container types (grouped for memory optimization) std::string address_str_{}; +#ifdef USE_ESP32_BLE_DEVICE std::vector services_; +#endif // Group 3: 4-byte types int gattc_if_; diff --git a/esphome/components/esp32_ble_client/ble_descriptor.h b/esphome/components/esp32_ble_client/ble_descriptor.h index 015a1243ed..fb2b78a7b1 100644 --- a/esphome/components/esp32_ble_client/ble_descriptor.h +++ b/esphome/components/esp32_ble_client/ble_descriptor.h @@ -1,6 +1,9 @@ #pragma once +#include "esphome/core/defines.h" + #ifdef USE_ESP32 +#ifdef USE_ESP32_BLE_DEVICE #include "esphome/components/esp32_ble_tracker/esp32_ble_tracker.h" @@ -20,4 +23,5 @@ class BLEDescriptor { } // namespace esphome::esp32_ble_client +#endif // USE_ESP32_BLE_DEVICE #endif // USE_ESP32 diff --git a/esphome/components/esp32_ble_client/ble_service.cpp b/esphome/components/esp32_ble_client/ble_service.cpp index 0defefd6ac..accaad15e1 100644 --- a/esphome/components/esp32_ble_client/ble_service.cpp +++ b/esphome/components/esp32_ble_client/ble_service.cpp @@ -4,6 +4,7 @@ #include "esphome/core/log.h" #ifdef USE_ESP32 +#ifdef USE_ESP32_BLE_DEVICE namespace esphome::esp32_ble_client { @@ -72,4 +73,5 @@ void BLEService::parse_characteristics() { } // namespace esphome::esp32_ble_client +#endif // USE_ESP32_BLE_DEVICE #endif // USE_ESP32 diff --git a/esphome/components/esp32_ble_client/ble_service.h b/esphome/components/esp32_ble_client/ble_service.h index 1b8b5a36dc..00ecc777e7 100644 --- a/esphome/components/esp32_ble_client/ble_service.h +++ b/esphome/components/esp32_ble_client/ble_service.h @@ -1,6 +1,9 @@ #pragma once +#include "esphome/core/defines.h" + #ifdef USE_ESP32 +#ifdef USE_ESP32_BLE_DEVICE #include "esphome/components/esp32_ble_tracker/esp32_ble_tracker.h" @@ -31,4 +34,5 @@ class BLEService { } // namespace esphome::esp32_ble_client +#endif // USE_ESP32_BLE_DEVICE #endif // USE_ESP32