ESPHome 2026.12.0-dev
Loading...
Searching...
No Matches
relaxed_conn_params.h
Go to the documentation of this file.
1#pragma once
2
3#include <array>
4#include <cstdint>
5
7
10 public:
11 static constexpr uint8_t MAX_ADDRESSES = 4;
12
13 bool use_relaxed(uint64_t address) {
14 uint64_t *entry = this->find_(address);
15 return entry != nullptr && (*entry & RELAXED_FLAG) != 0;
16 }
18 bool fast_failed(uint64_t address) {
19 uint64_t *entry = this->find_(address);
20 if (entry != nullptr && *entry != 0) {
21 *entry |= RELAXED_FLAG;
22 return true;
23 }
24 if (entry == nullptr) {
25 this->erase_(this->entries_.data());
26 entry = &this->entries_.back();
27 }
28 *entry = address;
29 return false;
30 }
32 void fast_connected(uint64_t address) {
33 uint64_t *entry = this->find_(address);
34 if (entry != nullptr && *entry != 0 && (*entry & RELAXED_FLAG) == 0)
35 this->erase_(entry);
36 }
38 bool forget(uint64_t address) {
39 uint64_t *entry = this->find_(address);
40 if (entry == nullptr || *entry == 0)
41 return false;
42 this->erase_(entry);
43 return true;
44 }
45
46 protected:
47 // The matching entry, else the first free one; nullptr when full without a match
48 uint64_t *find_(uint64_t address) {
49 for (uint64_t &entry : this->entries_) {
50 if (entry == 0 || (entry & ~RELAXED_FLAG) == address)
51 return &entry;
52 }
53 return nullptr;
54 }
55 // Shifts the rest down to keep insertion order for oldest-first eviction
56 void erase_(uint64_t *entry) {
57 for (uint64_t *last = &this->entries_.back(); entry != last; entry++)
58 entry[0] = entry[1];
59 this->entries_.back() = 0;
60 }
61
62 static constexpr uint64_t RELAXED_FLAG = 1ULL << 63; // addresses are 48 bits
63
64 // In insertion order; 0 marks the free tail, as no address is 0
65 std::array<uint64_t, MAX_ADDRESSES> entries_{};
66};
67
68} // namespace esphome::bluetooth_connection
Addresses that failed at FAST twice in a row and now open with MEDIUM; the oldest is replaced when fu...
void fast_connected(uint64_t address)
A FAST link came up: forgets a single earlier failure. Relaxed entries stay.
bool fast_failed(uint64_t address)
A FAST attempt failed; returns true when it was the second in a row.
std::array< uint64_t, MAX_ADDRESSES > entries_
bool forget(uint64_t address)
Returns true when an entry was removed.