From 17db30fa4f9eac2843b3dd080f46bf0513d07d0b Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sun, 21 Apr 2024 11:22:28 +0200 Subject: [PATCH] dry --- .../bluetooth_proxy/bluetooth_connection.cpp | 14 ++++++++------ .../bluetooth_proxy/bluetooth_connection.h | 1 + 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/esphome/components/bluetooth_proxy/bluetooth_connection.cpp b/esphome/components/bluetooth_proxy/bluetooth_connection.cpp index b85ec3947b..4fbbd6c4f8 100644 --- a/esphome/components/bluetooth_proxy/bluetooth_connection.cpp +++ b/esphome/components/bluetooth_proxy/bluetooth_connection.cpp @@ -22,18 +22,14 @@ bool BluetoothConnection::gattc_event_handler(esp_gattc_cb_event_t event, esp_ga case ESP_GATTC_DISCONNECT_EVT: { if (param->disconnect.conn_id != this->conn_id_) break; - this->proxy_->send_device_connection(this->address_, false, 0, param->disconnect.reason); - this->set_address(0); - this->proxy_->send_connections_free(); + this->handle_connection_close_(param->disconnect.reason); break; } case ESP_GATTC_OPEN_EVT: { if (param->open.conn_id != this->conn_id_) break; if (param->open.status != ESP_GATT_OK && param->open.status != ESP_GATT_ALREADY_OPEN) { - this->proxy_->send_device_connection(this->address_, false, 0, param->open.status); - this->set_address(0); - this->proxy_->send_connections_free(); + this->handle_connection_close_(param->open.status); } else if (this->connection_type_ == espbt::ConnectionType::V3_WITH_CACHE) { this->proxy_->send_device_connection(this->address_, true, this->mtu_); this->proxy_->send_connections_free(); @@ -153,6 +149,12 @@ bool BluetoothConnection::gattc_event_handler(esp_gattc_cb_event_t event, esp_ga return true; } +void BluetoothConnection::handle_connection_close_(esp_err_t error) { + this->proxy_->send_device_connection(this->address_, false, 0, error); + this->set_address(0); + this->proxy_->send_connections_free(); +} + void BluetoothConnection::gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param) { BLEClientBase::gap_event_handler(event, param); diff --git a/esphome/components/bluetooth_proxy/bluetooth_connection.h b/esphome/components/bluetooth_proxy/bluetooth_connection.h index e6ab3cbccc..9ab2a23c7a 100644 --- a/esphome/components/bluetooth_proxy/bluetooth_connection.h +++ b/esphome/components/bluetooth_proxy/bluetooth_connection.h @@ -26,6 +26,7 @@ class BluetoothConnection : public esp32_ble_client::BLEClientBase { protected: friend class BluetoothProxy; bool seen_mtu_or_services_{false}; + void handle_connection_close_(esp_err_t error); int16_t send_service_{-2}; BluetoothProxy *proxy_;