From d3cbe21fa392d78ce2d4ed437409642956690ae4 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Fri, 1 Aug 2025 10:29:24 -1000 Subject: [PATCH] preen --- .../bluetooth_proxy/bluetooth_connection.cpp | 14 ++++++++-- .../bluetooth_proxy/bluetooth_connection.h | 1 + .../bluetooth_proxy/bluetooth_proxy.cpp | 27 ------------------- .../bluetooth_proxy/bluetooth_proxy.h | 4 --- 4 files changed, 13 insertions(+), 33 deletions(-) diff --git a/esphome/components/bluetooth_proxy/bluetooth_connection.cpp b/esphome/components/bluetooth_proxy/bluetooth_connection.cpp index 554c312643..55ff47a725 100644 --- a/esphome/components/bluetooth_proxy/bluetooth_connection.cpp +++ b/esphome/components/bluetooth_proxy/bluetooth_connection.cpp @@ -78,14 +78,24 @@ void BluetoothConnection::dump_config() { BLEClientBase::dump_config(); } +void BluetoothConnection::update_allocated_slot_(uint64_t find_value, uint64_t set_value) { + auto &allocated = this->proxy_->connections_free_response_.allocated; + auto it = std::find(allocated.begin(), allocated.end(), find_value); + if (it != allocated.end()) { + *it = set_value; + } +} + void BluetoothConnection::set_address(uint64_t address) { // If we're clearing an address (disconnecting), update the pre-allocated message if (address == 0 && this->address_ != 0) { - this->proxy_->free_connection_(this->address_); + this->proxy_->connections_free_response_.free++; + this->update_allocated_slot_(this->address_, 0); } // If we're setting a new address (connecting), update the pre-allocated message else if (address != 0 && this->address_ == 0) { - this->proxy_->allocate_connection_(this, address); + this->proxy_->connections_free_response_.free--; + this->update_allocated_slot_(0, address); } // Call parent implementation to actually set the address diff --git a/esphome/components/bluetooth_proxy/bluetooth_connection.h b/esphome/components/bluetooth_proxy/bluetooth_connection.h index c6f8d37e4e..042868e7a4 100644 --- a/esphome/components/bluetooth_proxy/bluetooth_connection.h +++ b/esphome/components/bluetooth_proxy/bluetooth_connection.h @@ -32,6 +32,7 @@ class BluetoothConnection : public esp32_ble_client::BLEClientBase { bool supports_efficient_uuids_() const; void send_service_for_discovery_(); void reset_connection_(esp_err_t reason); + void update_allocated_slot_(uint64_t find_value, uint64_t set_value); // Memory optimized layout for 32-bit systems // Group 1: Pointers (4 bytes each, naturally aligned) diff --git a/esphome/components/bluetooth_proxy/bluetooth_proxy.cpp b/esphome/components/bluetooth_proxy/bluetooth_proxy.cpp index eff5c21729..3a9ed69677 100644 --- a/esphome/components/bluetooth_proxy/bluetooth_proxy.cpp +++ b/esphome/components/bluetooth_proxy/bluetooth_proxy.cpp @@ -483,33 +483,6 @@ void BluetoothProxy::bluetooth_scanner_set_mode(bool active) { true); // Set this to true to automatically start scanning again when it has cleaned up. } -void BluetoothProxy::allocate_connection_(BluetoothConnection *connection, uint64_t address) { - // Update pre-allocated message directly - this->connections_free_response_.free--; - - // Find first zero slot and set it - auto it = std::find(this->connections_free_response_.allocated.begin(), - this->connections_free_response_.allocated.end(), 0); - if (it != this->connections_free_response_.allocated.end()) { - *it = address; - } -} - -void BluetoothProxy::free_connection_(uint64_t address) { - if (address == 0) - return; // Safety check - - // Update pre-allocated message directly - this->connections_free_response_.free++; - - // Find the address and set to 0 - auto it = std::find(this->connections_free_response_.allocated.begin(), - this->connections_free_response_.allocated.end(), address); - if (it != this->connections_free_response_.allocated.end()) { - *it = 0; - } -} - BluetoothProxy *global_bluetooth_proxy = nullptr; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables) } // namespace esphome::bluetooth_proxy diff --git a/esphome/components/bluetooth_proxy/bluetooth_proxy.h b/esphome/components/bluetooth_proxy/bluetooth_proxy.h index bf82c5b873..8e7462c660 100644 --- a/esphome/components/bluetooth_proxy/bluetooth_proxy.h +++ b/esphome/components/bluetooth_proxy/bluetooth_proxy.h @@ -133,10 +133,6 @@ class BluetoothProxy : public esp32_ble_tracker::ESPBTDeviceListener, public Com BluetoothConnection *get_connection_(uint64_t address, bool reserve); - // Helper functions for connection state management - void allocate_connection_(BluetoothConnection *connection, uint64_t address); - void free_connection_(uint64_t address); - // Memory optimized layout for 32-bit systems // Group 1: Pointers (4 bytes each, naturally aligned) api::APIConnection *api_connection_{nullptr};