ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
expected_disconnect.h
Go to the documentation of this file.
1#pragma once
2#include <algorithm>
3#include <array>
4#include <cstdint>
5
6namespace esphome::wifi {
7
14
15// Switching APs calls esp_wifi_disconnect(), which queues a leave event for the old AP, or with no BSSID for an
16// attempt that never associated. Remembers it so that event does not end the next attempt. Platform free for tests.
18 public:
19 void arm(const uint8_t *bssid) { this->arm_(bssid, ExpectedLeave::EXPECTED_LEAVE_AP); }
21
25 template<typename ReadBssid, typename Disconnect>
26 bool disconnect_and_expect_leave(ReadBssid &&read_bssid, Disconnect &&disconnect) {
27 this->clear();
28 std::array<uint8_t, 6> bssid{};
29 const bool had_ap = read_bssid(bssid.data());
30 if (!disconnect())
31 return false;
33 return true;
34 }
35
37 bool is_previous_ap(const uint8_t *bssid) const {
39 std::equal(this->bssid_.begin(), this->bssid_.end(), bssid);
40 }
41
44 ExpectedLeave consume_disconnect(const uint8_t *bssid, bool is_leave) {
45 const ExpectedLeave matched =
46 is_leave && this->is_previous_ap(bssid) ? this->expected_ : ExpectedLeave::EXPECTED_LEAVE_NONE;
47 this->clear();
48 return matched;
49 }
50
51 protected:
52 void arm_(const uint8_t *bssid, ExpectedLeave expected) {
53 std::copy_n(bssid, this->bssid_.size(), this->bssid_.begin());
54 this->expected_ = expected;
55 }
56
57 std::array<uint8_t, 6> bssid_{};
59};
60
61} // namespace esphome::wifi
void arm_(const uint8_t *bssid, ExpectedLeave expected)
bool is_previous_ap(const uint8_t *bssid) const
A connected event still queued from the previous AP; it keeps the expectation.
bool disconnect_and_expect_leave(ReadBssid &&read_bssid, Disconnect &&disconnect)
Disconnects and, once that succeeds, expects the leave event.
ExpectedLeave consume_disconnect(const uint8_t *bssid, bool is_leave)
Which expected leave a disconnect is.
ExpectedLeave
Which expected leave a disconnect matched.
@ EXPECTED_LEAVE_ATTEMPT
An attempt that never associated, so no BSSID.