Skip to content

Commit

Permalink
Fix descriptors not being deleted (esphome#4104)
Browse files Browse the repository at this point in the history
  • Loading branch information
bdraco authored Nov 28, 2022
1 parent 1166d93 commit 75573a3
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 6 deletions.
3 changes: 1 addition & 2 deletions esphome/components/bluetooth_proxy/bluetooth_proxy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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_++;
}
Expand Down
7 changes: 7 additions & 0 deletions esphome/components/esp32_ble_client/ble_characteristic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions esphome/components/esp32_ble_client/ble_characteristic.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class BLECharacteristic {
esp_gatt_char_prop_t properties;
std::vector<BLEDescriptor *> 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);
Expand Down
12 changes: 8 additions & 4 deletions esphome/components/esp32_ble_client/ble_client_base.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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;
}
Expand Down
1 change: 1 addition & 0 deletions esphome/components/esp32_ble_client/ble_client_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -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; }

Expand Down
7 changes: 7 additions & 0 deletions esphome/components/esp32_ble_client/ble_service.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions esphome/components/esp32_ble_client/ble_service.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class BLEService {
std::vector<BLECharacteristic *> characteristics;
BLEClientBase *client;
void parse_characteristics();
void release_characteristics();
BLECharacteristic *get_characteristic(espbt::ESPBTUUID uuid);
BLECharacteristic *get_characteristic(uint16_t uuid);
};
Expand Down

0 comments on commit 75573a3

Please sign in to comment.