1
0
mirror of https://github.com/esphome/esphome.git synced 2025-09-20 12:12:24 +01:00
This commit is contained in:
J. Nick Koston
2025-08-01 10:29:24 -10:00
parent 7d06013608
commit d3cbe21fa3
4 changed files with 13 additions and 33 deletions

View File

@@ -78,14 +78,24 @@ void BluetoothConnection::dump_config() {
BLEClientBase::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) { void BluetoothConnection::set_address(uint64_t address) {
// If we're clearing an address (disconnecting), update the pre-allocated message // If we're clearing an address (disconnecting), update the pre-allocated message
if (address == 0 && this->address_ != 0) { 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 // If we're setting a new address (connecting), update the pre-allocated message
else if (address != 0 && this->address_ == 0) { 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 // Call parent implementation to actually set the address

View File

@@ -32,6 +32,7 @@ class BluetoothConnection : public esp32_ble_client::BLEClientBase {
bool supports_efficient_uuids_() const; bool supports_efficient_uuids_() const;
void send_service_for_discovery_(); void send_service_for_discovery_();
void reset_connection_(esp_err_t reason); 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 // Memory optimized layout for 32-bit systems
// Group 1: Pointers (4 bytes each, naturally aligned) // Group 1: Pointers (4 bytes each, naturally aligned)

View File

@@ -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. 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) BluetoothProxy *global_bluetooth_proxy = nullptr; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
} // namespace esphome::bluetooth_proxy } // namespace esphome::bluetooth_proxy

View File

@@ -133,10 +133,6 @@ class BluetoothProxy : public esp32_ble_tracker::ESPBTDeviceListener, public Com
BluetoothConnection *get_connection_(uint64_t address, bool reserve); 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 // Memory optimized layout for 32-bit systems
// Group 1: Pointers (4 bytes each, naturally aligned) // Group 1: Pointers (4 bytes each, naturally aligned)
api::APIConnection *api_connection_{nullptr}; api::APIConnection *api_connection_{nullptr};