15#ifdef USE_ESP32_FRAMEWORK_ARDUINO
25#if defined(USE_ESP32) && defined(USE_WIFI_WPA2_EAP)
26#if (ESP_IDF_VERSION_MAJOR >= 5) && (ESP_IDF_VERSION_MINOR >= 1)
27#include <esp_eap_client.h>
34#include <ESP8266WiFi.h>
35#include <ESP8266WiFiType.h>
37#if defined(USE_ESP8266) && USE_ARDUINO_VERSION_CODE < VERSION_CODE(2, 4, 0)
39#include <user_interface.h>
47#include "cyw43_country.h"
48#include "pico/cyw43_arch.h"
54#if defined(USE_ESP32) && defined(USE_WIFI_RUNTIME_POWER_SAVE)
55#include <freertos/FreeRTOS.h>
56#include <freertos/semphr.h>
62static constexpr int8_t WIFI_RSSI_DISCONNECTED = -127;
65static constexpr size_t SSID_BUFFER_SIZE = 33;
107#ifdef USE_WIFI_FAST_CONNECT
130#ifdef USE_WIFI_WPA2_EAP
158 void set_ssid(
const std::string &ssid);
162#ifdef USE_WIFI_WPA2_EAP
168#ifdef USE_WIFI_MANUAL_IP
172 const std::string &
get_ssid()
const;
176#ifdef USE_WIFI_WPA2_EAP
182#ifdef USE_WIFI_MANUAL_IP
190#ifdef USE_WIFI_WPA2_EAP
193#ifdef USE_WIFI_MANUAL_IP
205 WiFiScanResult(
const bssid_t &bssid, std::string ssid, uint8_t channel, int8_t rssi,
bool with_auth,
bool is_hidden);
212 const std::string &
get_ssid()
const;
351 void save_wifi_sta(
const std::string &ssid,
const std::string &password);
356 void setup()
override;
365 void loop()
override;
371#ifdef USE_WIFI_11KV_SUPPORT
387 if (it.bssid == bssid)
394 if (it.bssid == bssid)
401 if (it.bssid == bssid) {
416 const char *
wifi_ssid_to(std::span<char, SSID_BUFFER_SIZE> buffer);
429#ifdef USE_WIFI_LISTENERS
450#ifdef USE_WIFI_RUNTIME_POWER_SAVE
533 if (this->
sta_.empty())
535 for (
const auto &ap : this->
sta_) {
570#ifdef USE_WIFI_FAST_CONNECT
581#ifdef USE_ESP32_FRAMEWORK_ARDUINO
606#ifdef USE_WIFI_LISTENERS
613#ifdef USE_WIFI_FAST_CONNECT
649#ifdef USE_WIFI_11KV_SUPPORT
658#if defined(USE_ESP32) && defined(USE_WIFI_RUNTIME_POWER_SAVE)
672 const char *use_address_{
""};
BedjetMode mode
BedJet operating mode.
Fixed-capacity vector - allocates once at runtime, never reallocates This avoids std::vector template...
StringRef is a reference to a string owned by something else.
uint8_t get_channel() const
const std::string & get_ssid() const
void set_ssid(const std::string &ssid)
const optional< EAPAuth > & get_eap() const
const std::string & get_password() const
void set_bssid(const bssid_t &bssid)
void set_channel(uint8_t channel)
optional< ManualIP > manual_ip_
void set_eap(optional< EAPAuth > eap_auth)
void set_password(const std::string &password)
void set_manual_ip(optional< ManualIP > manual_ip)
const optional< ManualIP > & get_manual_ip() const
int8_t get_priority() const
void set_hidden(bool hidden)
const bssid_t & get_bssid() const
void set_priority(int8_t priority)
This component is responsible for managing the ESP WiFi interface.
bool is_captive_portal_active_()
bool did_scan_this_cycle_
Trigger * get_connect_trigger() const
bool error_from_callback_
void add_sta(const WiFiAP &ap)
bool load_fast_connect_settings_(WiFiAP ¶ms)
void add_connect_state_listener(WiFiConnectStateListener *listener)
Add a listener for WiFi connection state changes.
bool wifi_apply_power_save_()
void set_ap(const WiFiAP &ap)
Setup an Access Point that should be created if no connection to a station can be made.
bool request_high_performance()
Request high-performance mode (no power saving) for improved WiFi latency.
ESPPreferenceObject pref_
void set_sta(const WiFiAP &ap)
WiFiPowerSaveMode power_save_
std::vector< WiFiIPStateListener * > ip_state_listeners_
bool has_sta_priority(const bssid_t &bssid)
const WiFiAP * get_selected_sta_() const
int8_t get_sta_priority(const bssid_t bssid)
void log_and_adjust_priority_for_failed_connect_()
Log failed connection and decrease BSSID priority to avoid repeated attempts.
std::vector< WiFiScanResultsListener * > scan_results_listeners_
void init_sta(size_t count)
void check_connecting_finished()
void save_wifi_sta(const std::string &ssid, const std::string &password)
WiFiComponentState state_
wifi_scan_vector_t< WiFiScanResult > scan_result_
bool skip_cooldown_next_cycle_
WiFiPowerSaveMode configured_power_save_
void set_sta_priority(const bssid_t bssid, int8_t priority)
void print_connect_params_()
void loop() override
Reconnect WiFi if required.
void start_connecting(const WiFiAP &ap)
void set_enable_on_boot(bool enable_on_boot)
void advance_to_next_target_or_increment_retry_()
Advance to next target (AP/SSID) within current phase, or increment retry counter Called when staying...
void add_power_save_listener(WiFiPowerSaveListener *listener)
Add a listener for WiFi power save mode changes.
bool wifi_sta_ip_config_(const optional< ManualIP > &manual_ip)
void check_scanning_finished()
static int s_wifi_scan_result(void *env, const cyw43_ev_scan_result_t *result)
SemaphoreHandle_t high_performance_semaphore_
network::IPAddress get_dns_address(int num)
static void wifi_event_callback(System_Event_t *event)
WiFiComponent()
Construct a WiFiComponent.
void wifi_process_event_(IDFWiFiEvent *data)
std::vector< WiFiSTAPriority > sta_priorities_
int8_t selected_sta_index_
void save_fast_connect_settings_()
void set_min_auth_mode(WifiMinAuthMode min_auth_mode)
bool wifi_start_ap_(const WiFiAP &ap)
int32_t get_wifi_channel()
const char * wifi_ssid_to(std::span< char, SSID_BUFFER_SIZE > buffer)
Write SSID to buffer without heap allocation.
void start_connecting(const WiFiAP &ap, bool)
network::IPAddress wifi_subnet_mask_()
bool has_saved_wifi_settings_
network::IPAddress wifi_soft_ap_ip()
void set_passive_scan(bool passive)
void wifi_event_callback_(arduino_event_id_t event, arduino_event_info_t info)
network::IPAddress wifi_gateway_ip_()
static void s_wifi_scan_done_callback(void *arg, STATUS status)
void set_power_save_mode(WiFiPowerSaveMode power_save)
bool is_ap_active() const
int8_t find_next_hidden_sta_(int8_t start_index)
Find next SSID that wasn't in scan results (might be hidden) Returns index of next potentially hidden...
Trigger * connect_trigger_
void add_ip_state_listener(WiFiIPStateListener *listener)
Add a listener for IP state changes.
ESPPreferenceObject fast_connect_pref_
void clear_priorities_if_all_min_()
Clear BSSID priority tracking if all priorities are at minimum (saves memory)
void wifi_scan_result(void *env, const cyw43_ev_scan_result_t *result)
WiFiRetryPhase determine_next_phase_()
Determine next retry phase based on current state and failure conditions.
network::IPAddress wifi_dns_ip_(int num)
float get_loop_priority() const override
bool wifi_apply_hostname_()
bool wifi_sta_pre_setup_()
network::IPAddresses get_ip_addresses()
Trigger * disconnect_trigger_
float get_setup_priority() const override
WIFI setup_priority.
void set_output_power(float output_power)
bool is_esp32_improv_active_()
FixedVector< WiFiAP > sta_
int8_t find_first_non_hidden_index_() const
Find the index of the first non-hidden network Returns where EXPLICIT_HIDDEN phase would have stopped...
bool ssid_was_seen_in_scan_(const std::string &ssid) const
Check if an SSID was seen in the most recent scan results Used to skip hidden mode for SSIDs we know ...
bool wifi_ap_ip_config_(const optional< ManualIP > &manual_ip)
bool needs_scan_results_() const
Check if we need valid scan results for the current phase but don't have any Returns true if the phas...
void add_scan_results_listener(WiFiScanResultsListener *listener)
Add a listener for WiFi scan results.
WiFiRetryPhase retry_phase_
bool transition_to_phase_(WiFiRetryPhase new_phase)
Transition to a new retry phase with logging Returns true if a scan was started (caller should wait),...
Trigger * get_disconnect_trigger() const
WiFiAP build_params_for_current_phase_()
bool is_high_performance_mode_
void dump_config() override
void set_ap_timeout(uint32_t ap_timeout)
bool release_high_performance()
Release a high-performance mode request.
WifiMinAuthMode min_auth_mode_
bool wifi_apply_output_power_(float output_power)
const char * get_use_address() const
WiFiSTAConnectStatus wifi_sta_connect_status_()
bool wifi_sta_connect_(const WiFiAP &ap)
bool went_through_explicit_hidden_phase_() const
Check if we went through EXPLICIT_HIDDEN phase (first network is marked hidden) Used in RETRY_HIDDEN ...
bool wifi_mode_(optional< bool > sta, optional< bool > ap)
void set_reboot_timeout(uint32_t reboot_timeout)
bool can_proceed() override
network::IPAddresses wifi_sta_ip_addresses()
uint8_t num_ipv6_addresses_
void set_keep_scan_results(bool keep_scan_results)
void start_initial_connection_()
Start initial connection - either scan or connect directly to hidden networks.
bool wifi_scan_start_(bool passive)
bool all_networks_hidden_() const
bool handled_connected_state_
void wifi_scan_done_callback_()
void setup() override
Setup WiFi interface.
void set_use_address(const char *use_address)
std::vector< WiFiConnectStateListener * > connect_state_listeners_
void reset_selected_ap_to_first_if_invalid_()
std::vector< WiFiPowerSaveListener * > power_save_listeners_
const wifi_scan_vector_t< WiFiScanResult > & get_scan_result() const
Listener interface for WiFi connection state changes.
virtual void on_wifi_connect_state(StringRef ssid, std::span< const uint8_t, 6 > bssid)=0
Listener interface for WiFi IP state changes.
virtual void on_ip_state(const network::IPAddresses &ips, const network::IPAddress &dns1, const network::IPAddress &dns2)=0
Listener interface for WiFi power save mode changes.
virtual void on_wifi_power_save(WiFiPowerSaveMode mode)=0
const std::string & get_ssid() const
int8_t get_priority() const
uint8_t get_channel() const
bool get_with_auth() const
const bssid_t & get_bssid() const
WiFiScanResult(const bssid_t &bssid, std::string ssid, uint8_t channel, int8_t rssi, bool with_auth, bool is_hidden)
bool matches(const WiFiAP &config) const
void set_matches(bool matches)
void set_priority(int8_t priority)
bool get_is_hidden() const
bool operator==(const WiFiScanResult &rhs) const
Listener interface for WiFi scan results.
virtual void on_wifi_scan_results(const wifi_scan_vector_t< WiFiScanResult > &results)=0
std::array< IPAddress, 5 > IPAddresses
std::array< uint8_t, 6 > bssid_t
@ WIFI_MIN_AUTH_MODE_WPA2
@ WIFI_MIN_AUTH_MODE_WPA3
std::vector< T > wifi_scan_vector_t
struct esphome::wifi::SavedWifiSettings PACKED
@ ERROR_NETWORK_NOT_FOUND
WiFiRetryPhase
Tracks the current retry strategy/phase for WiFi connection attempts.
@ RETRY_HIDDEN
Retry networks not found in scan (might be hidden)
@ RESTARTING_ADAPTER
Restarting WiFi adapter to clear stuck state.
@ INITIAL_CONNECT
Initial connection attempt (varies based on fast_connect setting)
@ EXPLICIT_HIDDEN
Explicitly hidden networks (user marked as hidden, try before scanning)
@ FAST_CONNECT_CYCLING_APS
Fast connect mode: cycling through configured APs (config-only, no scan)
@ SCAN_CONNECTING
Scan-based: connecting to best AP from scan results.
WiFiComponent * global_wifi_component
@ WIFI_COMPONENT_STATE_DISABLED
WiFi is disabled.
@ WIFI_COMPONENT_STATE_AP
WiFi is in AP-only mode and internal AP is already enabled.
@ WIFI_COMPONENT_STATE_STA_CONNECTING
WiFi is in STA(+AP) mode and currently connecting to an AP.
@ WIFI_COMPONENT_STATE_OFF
Nothing has been initialized yet.
@ WIFI_COMPONENT_STATE_STA_SCANNING
WiFi is in STA-only mode and currently scanning for APs.
@ WIFI_COMPONENT_STATE_COOLDOWN
WiFi is in cooldown mode because something went wrong, scanning will begin after a short period of ti...
@ WIFI_COMPONENT_STATE_STA_CONNECTED
WiFi is in STA(+AP) mode and successfully connected.
esp_eap_ttls_phase2_types ttls_phase_2
Struct for setting static IPs in WiFiComponent.
network::IPAddress static_ip
network::IPAddress dns1
The first DNS server. 0.0.0.0 for default.
network::IPAddress gateway
network::IPAddress dns2
The second DNS server. 0.0.0.0 for default.
network::IPAddress subnet