[esp32_ble_client] Conditionally compile BLE service classes to reduce flash usage (#10114)

This commit is contained in:
J. Nick Koston 2025-08-06 22:46:34 -10:00 committed by GitHub
parent 37a9ad6a0d
commit 1ba76f5f2e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 30 additions and 0 deletions

View File

@ -5,6 +5,7 @@
#include "esphome/core/log.h" #include "esphome/core/log.h"
#ifdef USE_ESP32 #ifdef USE_ESP32
#ifdef USE_ESP32_BLE_DEVICE
namespace esphome::esp32_ble_client { 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 } // namespace esphome::esp32_ble_client
#endif // USE_ESP32_BLE_DEVICE
#endif // USE_ESP32 #endif // USE_ESP32

View File

@ -1,6 +1,9 @@
#pragma once #pragma once
#include "esphome/core/defines.h"
#ifdef USE_ESP32 #ifdef USE_ESP32
#ifdef USE_ESP32_BLE_DEVICE
#include "esphome/components/esp32_ble_tracker/esp32_ble_tracker.h" #include "esphome/components/esp32_ble_tracker/esp32_ble_tracker.h"
@ -34,4 +37,5 @@ class BLECharacteristic {
} // namespace esphome::esp32_ble_client } // namespace esphome::esp32_ble_client
#endif // USE_ESP32_BLE_DEVICE
#endif // USE_ESP32 #endif // USE_ESP32

View File

@ -209,9 +209,11 @@ void BLEClientBase::unconditional_disconnect() {
} }
void BLEClientBase::release_services() { void BLEClientBase::release_services() {
#ifdef USE_ESP32_BLE_DEVICE
for (auto &svc : this->services_) for (auto &svc : this->services_)
delete svc; // NOLINT(cppcoreguidelines-owning-memory) delete svc; // NOLINT(cppcoreguidelines-owning-memory)
this->services_.clear(); this->services_.clear();
#endif
#ifndef CONFIG_BT_GATTC_CACHE_NVS_FLASH #ifndef CONFIG_BT_GATTC_CACHE_NVS_FLASH
esp_ble_gattc_cache_clean(this->remote_bda_); esp_ble_gattc_cache_clean(this->remote_bda_);
#endif #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. // as they use the ESP APIs to get services.
break; break;
} }
#ifdef USE_ESP32_BLE_DEVICE
BLEService *ble_service = new BLEService(); // NOLINT(cppcoreguidelines-owning-memory) BLEService *ble_service = new BLEService(); // NOLINT(cppcoreguidelines-owning-memory)
ble_service->uuid = espbt::ESPBTUUID::from_uuid(param->search_res.srvc_id.uuid); ble_service->uuid = espbt::ESPBTUUID::from_uuid(param->search_res.srvc_id.uuid);
ble_service->start_handle = param->search_res.start_handle; ble_service->start_handle = param->search_res.start_handle;
ble_service->end_handle = param->search_res.end_handle; ble_service->end_handle = param->search_res.end_handle;
ble_service->client = this; ble_service->client = this;
this->services_.push_back(ble_service); this->services_.push_back(ble_service);
#endif
break; break;
} }
case ESP_GATTC_SEARCH_CMPL_EVT: { 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->connection_type_ == espbt::ConnectionType::V3_WITH_CACHE) {
this->restore_medium_conn_params_(); this->restore_medium_conn_params_();
} else { } else {
#ifdef USE_ESP32_BLE_DEVICE
for (auto &svc : this->services_) { for (auto &svc : this->services_) {
ESP_LOGV(TAG, "[%d] [%s] Service UUID: %s", this->connection_index_, this->address_str_.c_str(), ESP_LOGV(TAG, "[%d] [%s] Service UUID: %s", this->connection_index_, this->address_str_.c_str(),
svc->uuid.to_string().c_str()); svc->uuid.to_string().c_str());
ESP_LOGV(TAG, "[%d] [%s] start_handle: 0x%x end_handle: 0x%x", this->connection_index_, 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); 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()); ESP_LOGI(TAG, "[%d] [%s] Service discovery complete", this->connection_index_, this->address_str_.c_str());
this->state_ = espbt::ClientState::ESTABLISHED; this->state_ = espbt::ClientState::ESTABLISHED;
@ -581,6 +587,7 @@ float BLEClientBase::parse_char_value(uint8_t *value, uint16_t length) {
return NAN; return NAN;
} }
#ifdef USE_ESP32_BLE_DEVICE
BLEService *BLEClientBase::get_service(espbt::ESPBTUUID uuid) { BLEService *BLEClientBase::get_service(espbt::ESPBTUUID uuid) {
for (auto *svc : this->services_) { for (auto *svc : this->services_) {
if (svc->uuid == uuid) if (svc->uuid == uuid)
@ -657,6 +664,7 @@ BLEDescriptor *BLEClientBase::get_descriptor(uint16_t handle) {
} }
return nullptr; return nullptr;
} }
#endif // USE_ESP32_BLE_DEVICE
} // namespace esphome::esp32_ble_client } // namespace esphome::esp32_ble_client

View File

@ -5,7 +5,9 @@
#include "esphome/components/esp32_ble_tracker/esp32_ble_tracker.h" #include "esphome/components/esp32_ble_tracker/esp32_ble_tracker.h"
#include "esphome/core/component.h" #include "esphome/core/component.h"
#ifdef USE_ESP32_BLE_DEVICE
#include "ble_service.h" #include "ble_service.h"
#endif
#include <array> #include <array>
#include <string> #include <string>
@ -67,6 +69,7 @@ class BLEClientBase : public espbt::ESPBTClient, public Component {
} }
const std::string &address_str() const { return this->address_str_; } const std::string &address_str() const { return this->address_str_; }
#ifdef USE_ESP32_BLE_DEVICE
BLEService *get_service(espbt::ESPBTUUID uuid); BLEService *get_service(espbt::ESPBTUUID uuid);
BLEService *get_service(uint16_t uuid); BLEService *get_service(uint16_t uuid);
BLECharacteristic *get_characteristic(espbt::ESPBTUUID service, espbt::ESPBTUUID chr); 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); BLEDescriptor *get_descriptor(uint16_t handle);
// Get the configuration descriptor for the given characteristic handle. // Get the configuration descriptor for the given characteristic handle.
BLEDescriptor *get_config_descriptor(uint16_t handle); BLEDescriptor *get_config_descriptor(uint16_t handle);
#endif
float parse_char_value(uint8_t *value, uint16_t length); 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) // Group 2: Container types (grouped for memory optimization)
std::string address_str_{}; std::string address_str_{};
#ifdef USE_ESP32_BLE_DEVICE
std::vector<BLEService *> services_; std::vector<BLEService *> services_;
#endif
// Group 3: 4-byte types // Group 3: 4-byte types
int gattc_if_; int gattc_if_;

View File

@ -1,6 +1,9 @@
#pragma once #pragma once
#include "esphome/core/defines.h"
#ifdef USE_ESP32 #ifdef USE_ESP32
#ifdef USE_ESP32_BLE_DEVICE
#include "esphome/components/esp32_ble_tracker/esp32_ble_tracker.h" #include "esphome/components/esp32_ble_tracker/esp32_ble_tracker.h"
@ -20,4 +23,5 @@ class BLEDescriptor {
} // namespace esphome::esp32_ble_client } // namespace esphome::esp32_ble_client
#endif // USE_ESP32_BLE_DEVICE
#endif // USE_ESP32 #endif // USE_ESP32

View File

@ -4,6 +4,7 @@
#include "esphome/core/log.h" #include "esphome/core/log.h"
#ifdef USE_ESP32 #ifdef USE_ESP32
#ifdef USE_ESP32_BLE_DEVICE
namespace esphome::esp32_ble_client { namespace esphome::esp32_ble_client {
@ -72,4 +73,5 @@ void BLEService::parse_characteristics() {
} // namespace esphome::esp32_ble_client } // namespace esphome::esp32_ble_client
#endif // USE_ESP32_BLE_DEVICE
#endif // USE_ESP32 #endif // USE_ESP32

View File

@ -1,6 +1,9 @@
#pragma once #pragma once
#include "esphome/core/defines.h"
#ifdef USE_ESP32 #ifdef USE_ESP32
#ifdef USE_ESP32_BLE_DEVICE
#include "esphome/components/esp32_ble_tracker/esp32_ble_tracker.h" #include "esphome/components/esp32_ble_tracker/esp32_ble_tracker.h"
@ -31,4 +34,5 @@ class BLEService {
} // namespace esphome::esp32_ble_client } // namespace esphome::esp32_ble_client
#endif // USE_ESP32_BLE_DEVICE
#endif // USE_ESP32 #endif // USE_ESP32