diff --git a/esphome/components/bluetooth_proxy/bluetooth_proxy.cpp b/esphome/components/bluetooth_proxy/bluetooth_proxy.cpp index eead2a8aea..3cb4d9c0ad 100644 --- a/esphome/components/bluetooth_proxy/bluetooth_proxy.cpp +++ b/esphome/components/bluetooth_proxy/bluetooth_proxy.cpp @@ -109,8 +109,7 @@ void BluetoothProxy::loop() { // after sending them to save memory. If something actually needs them // it can parse them again. for (auto &characteristic : service->characteristics) { - characteristic->parsed = false; - characteristic->descriptors.clear(); + characteristic->release_descriptors(); } connection->send_service_++; } diff --git a/esphome/components/esp32_ble_client/ble_characteristic.cpp b/esphome/components/esp32_ble_client/ble_characteristic.cpp index ce27628c77..2fd7fe9871 100644 --- a/esphome/components/esp32_ble_client/ble_characteristic.cpp +++ b/esphome/components/esp32_ble_client/ble_characteristic.cpp @@ -16,6 +16,13 @@ BLECharacteristic::~BLECharacteristic() { delete desc; // NOLINT(cppcoreguidelines-owning-memory) } +void BLECharacteristic::release_descriptors() { + this->parsed = false; + for (auto &desc : this->descriptors) + delete desc; // NOLINT(cppcoreguidelines-owning-memory) + this->descriptors.clear(); +} + void BLECharacteristic::parse_descriptors() { this->parsed = true; uint16_t offset = 0; diff --git a/esphome/components/esp32_ble_client/ble_characteristic.h b/esphome/components/esp32_ble_client/ble_characteristic.h index 746ed600d8..a014788e65 100644 --- a/esphome/components/esp32_ble_client/ble_characteristic.h +++ b/esphome/components/esp32_ble_client/ble_characteristic.h @@ -24,6 +24,7 @@ class BLECharacteristic { esp_gatt_char_prop_t properties; std::vector descriptors; void parse_descriptors(); + void release_descriptors(); BLEDescriptor *get_descriptor(espbt::ESPBTUUID uuid); BLEDescriptor *get_descriptor(uint16_t uuid); BLEDescriptor *get_descriptor_by_handle(uint16_t handle); diff --git a/esphome/components/esp32_ble_client/ble_client_base.cpp b/esphome/components/esp32_ble_client/ble_client_base.cpp index c6ee9e174c..9254e13277 100644 --- a/esphome/components/esp32_ble_client/ble_client_base.cpp +++ b/esphome/components/esp32_ble_client/ble_client_base.cpp @@ -76,6 +76,13 @@ void BLEClientBase::disconnect() { } } +void BLEClientBase::release_services() { + for (auto &svc : this->services_) + delete svc; // NOLINT(cppcoreguidelines-owning-memory) + this->services_.clear(); + esp_ble_gattc_cache_clean(this->remote_bda_); +} + bool BLEClientBase::gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t esp_gattc_if, esp_ble_gattc_cb_param_t *param) { if (event == ESP_GATTC_REG_EVT && this->app_id != param->reg.app_id) @@ -132,10 +139,7 @@ bool BLEClientBase::gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_ return false; ESP_LOGV(TAG, "[%d] [%s] ESP_GATTC_DISCONNECT_EVT, reason %d", this->connection_index_, this->address_str_.c_str(), param->disconnect.reason); - for (auto &svc : this->services_) - delete svc; // NOLINT(cppcoreguidelines-owning-memory) - this->services_.clear(); - esp_ble_gattc_cache_clean(this->remote_bda_); + this->release_services(); this->set_state(espbt::ClientState::IDLE); break; } diff --git a/esphome/components/esp32_ble_client/ble_client_base.h b/esphome/components/esp32_ble_client/ble_client_base.h index 1e4b015824..c97f3c2747 100644 --- a/esphome/components/esp32_ble_client/ble_client_base.h +++ b/esphome/components/esp32_ble_client/ble_client_base.h @@ -34,6 +34,7 @@ class BLEClientBase : public espbt::ESPBTClient, public Component { void gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param) override; void connect() override; void disconnect(); + void release_services(); bool connected() { return this->state_ == espbt::ClientState::ESTABLISHED; } diff --git a/esphome/components/esp32_ble_client/ble_service.cpp b/esphome/components/esp32_ble_client/ble_service.cpp index 7ccf686d9a..b22d2a1788 100644 --- a/esphome/components/esp32_ble_client/ble_service.cpp +++ b/esphome/components/esp32_ble_client/ble_service.cpp @@ -29,6 +29,13 @@ BLEService::~BLEService() { delete chr; // NOLINT(cppcoreguidelines-owning-memory) } +void BLEService::release_characteristics() { + this->parsed = false; + for (auto &chr : this->characteristics) + delete chr; // NOLINT(cppcoreguidelines-owning-memory) + this->characteristics.clear(); +} + void BLEService::parse_characteristics() { this->parsed = true; uint16_t offset = 0; diff --git a/esphome/components/esp32_ble_client/ble_service.h b/esphome/components/esp32_ble_client/ble_service.h index 1b1e86611f..41fc3e838b 100644 --- a/esphome/components/esp32_ble_client/ble_service.h +++ b/esphome/components/esp32_ble_client/ble_service.h @@ -25,6 +25,7 @@ class BLEService { std::vector characteristics; BLEClientBase *client; void parse_characteristics(); + void release_characteristics(); BLECharacteristic *get_characteristic(espbt::ESPBTUUID uuid); BLECharacteristic *get_characteristic(uint16_t uuid); };