diff --git a/esphome/components/bluetooth_connection/bluetooth_connection_bluedroid.cpp b/esphome/components/bluetooth_connection/bluetooth_connection_bluedroid.cpp index 577eed93d0..668bb7958d 100644 --- a/esphome/components/bluetooth_connection/bluetooth_connection_bluedroid.cpp +++ b/esphome/components/bluetooth_connection/bluetooth_connection_bluedroid.cpp @@ -79,8 +79,7 @@ void BluedroidGattClient::loop() { // The loop only drives the bootstrap and the disconnect safety timeout. this->disable_loop(); } else if (st == ClientState::DISCONNECTING && - static_cast(static_cast(millis() >> 8) - this->disconnecting_tick_) > - (ble_device_base::GATT_DISCONNECT_TIMEOUT_MS >> 8)) { + millis() - this->disconnecting_started_ > ble_device_base::GATT_DISCONNECT_TIMEOUT_MS) { ESP_LOGE(TAG, "[%d] Timeout waiting for CLOSE_EVT, forcing IDLE", this->connection_index_); // Release before idling: unconditional disconnect does not release, and a // lost CLOSE/DISCONNECT would otherwise leak the table and the cache. @@ -438,7 +437,7 @@ void BluedroidGattClient::set_idle_() { } void BluedroidGattClient::set_disconnecting_() { - this->disconnecting_tick_ = static_cast(millis() >> 8); + this->disconnecting_started_ = millis(); this->set_state_(ClientState::DISCONNECTING); // The loop may be disabled while idle; the safety timeout needs it. this->enable_loop(); diff --git a/esphome/components/bluetooth_connection/bluetooth_connection_bluedroid.h b/esphome/components/bluetooth_connection/bluetooth_connection_bluedroid.h index 897ba2f2de..9dd83c168f 100644 --- a/esphome/components/bluetooth_connection/bluetooth_connection_bluedroid.h +++ b/esphome/components/bluetooth_connection/bluetooth_connection_bluedroid.h @@ -124,7 +124,7 @@ class BluedroidGattClient final : public esp32_ble_tracker::ESPBTClient, public uint8_t *table_storage_{nullptr}; #endif // Group 2: 4-byte types - int gattc_if_{ESP_GATT_IF_NONE}; + uint32_t disconnecting_started_{0}; // Group 3: arrays esp_bd_addr_t remote_bda_{}; @@ -132,10 +132,6 @@ class BluedroidGattClient final : public esp32_ble_tracker::ESPBTClient, public // Group 4: 2-byte types uint16_t conn_id_{0xFFFF}; uint16_t service_total_{0}; - // 256 ms ticks (millis() >> 8), wrap-safe for the 10 s net; a uint16 keeps - // the object on the 48-byte boundary (a full uint32 costs 4 B of field - // plus 4 B of per-slot storage padding). - uint16_t disconnecting_tick_{0}; #ifdef USE_BLE_GATT_SERVICE_TABLE // Filled element counts of the materialized table (0 when none). uint16_t table_char_total_{0}; @@ -143,6 +139,9 @@ class BluedroidGattClient final : public esp32_ble_tracker::ESPBTClient, public #endif // Group 5: 1-byte types + // esp_gatt_if_t is a uint8_t; the narrow type lands the object on the + // 48-byte boundary (an int cost 3 bytes of field plus per-slot padding). + esp_gatt_if_t gattc_if_{ESP_GATT_IF_NONE}; // Stored narrow (the enum is 4 bytes); widened at the esp_ble_gattc_open call. uint8_t remote_addr_type_{0}; esp32_ble_tracker::ConnectionType connection_type_{esp32_ble_tracker::ConnectionType::V3_WITHOUT_CACHE};