diff --git a/esphome/components/ble_client/connect_backoff.h b/esphome/components/ble_client/connect_backoff.h index 6acd74e4f7..f99c0fdd94 100644 --- a/esphome/components/ble_client/connect_backoff.h +++ b/esphome/components/ble_client/connect_backoff.h @@ -12,24 +12,29 @@ namespace esphome::ble_client { /// Reconnect backoff after repeated connect/discovery failures, shared by -/// both engines (wrap-safe start+failures pair; the duration is derived). +/// both engines. 256 ms ticks in a uint16_t keep it 4 bytes; the ~4.7 h tick +/// wrap can at worst reinstate one stale hold-off of a minute. class ConnectBackoff { public: - bool holding_off() const { return this->failures_ != 0 && millis() - this->start_ < this->failures_ * STEP_MS; } + bool holding_off() const { + return this->failures_ != 0 && static_cast(now_() - this->start_) < this->failures_ * STEP_TICKS; + } void register_failure(const char *address_str) { if (this->failures_ < MAX_STEPS) this->failures_++; - this->start_ = millis(); - esph_log_w("ble_client", "[%s] Holding off reconnect for %u s", address_str, this->failures_ * (STEP_MS / 1000)); + this->start_ = now_(); + esph_log_w("ble_client", "[%s] Holding off reconnect for %u s", address_str, this->failures_ * 10u); } void reset() { this->failures_ = 0; } private: - // Capped so a flapping peer retries within a minute at worst. - static constexpr uint32_t STEP_MS = 10000; + // ~10 s per consecutive failure, capped so a flapping peer retries within + // a minute at worst. + static constexpr uint16_t STEP_TICKS = 40; // x 256 ms static constexpr uint8_t MAX_STEPS = 6; + static uint16_t now_() { return static_cast(millis() >> 8); } - uint32_t start_{0}; + uint16_t start_{0}; uint8_t failures_{0}; }; diff --git a/esphome/components/bluetooth_connection/gatt_service_table_bluedroid.h b/esphome/components/bluetooth_connection/gatt_service_table_bluedroid.h index 79c6847e52..96f4fb3815 100644 --- a/esphome/components/bluetooth_connection/gatt_service_table_bluedroid.h +++ b/esphome/components/bluetooth_connection/gatt_service_table_bluedroid.h @@ -66,8 +66,8 @@ class BluedroidServiceTable { uint16_t char_total_{0}; uint16_t desc_total_{0}; // Walk context, set by build(). - esp_gatt_if_t gattc_if_{}; uint16_t conn_id_{0}; + esp_gatt_if_t gattc_if_{}; // uint8_t width uint8_t log_index_{0}; };