ESPHome 2026.3.0
Loading...
Searching...
No Matches
wifi_component_pico_w.cpp
Go to the documentation of this file.
1#include "wifi_component.h"
2
3#ifdef USE_WIFI
4#ifdef USE_RP2040
5
6#include <cassert>
7
8#include "lwip/dns.h"
9#include "lwip/err.h"
10#include "lwip/netif.h"
11#include <AddrList.h>
12
14#include "esphome/core/hal.h"
16#include "esphome/core/log.h"
17#include "esphome/core/util.h"
18
19namespace esphome::wifi {
20
21static const char *const TAG = "wifi_pico_w";
22
23// Check if STA is fully connected (WiFi joined + has IP address).
24// Do NOT use WiFi.status() or WiFi.connected() for this — in AP-only mode they
25// unconditionally return true regardless of STA state, causing false positives
26// when the fallback AP is active.
27static bool wifi_sta_connected() {
28 int link = cyw43_wifi_link_status(&cyw43_state, CYW43_ITF_STA);
29 IPAddress local = WiFi.localIP();
30 if (link == CYW43_LINK_JOIN && local.isSet()) {
31 // Verify the IP is a real STA IP, not the AP's IP leaking through
32 IPAddress ap_ip = WiFi.softAPIP();
33 if (local == ap_ip) {
34 ESP_LOGV(TAG, "wifi_sta_connected: localIP %s matches AP IP, ignoring", local.toString().c_str());
35 return false;
36 }
37 return true;
38 }
39 return false;
40}
41
42// Track previous state for detecting changes
43static bool s_sta_was_connected = false; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
44static bool s_sta_had_ip = false; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
45static size_t s_scan_result_count = 0; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
46
47bool WiFiComponent::wifi_mode_(optional<bool> sta, optional<bool> ap) {
48 if (sta.has_value()) {
49 if (sta.value()) {
50 cyw43_wifi_set_up(&cyw43_state, CYW43_ITF_STA, true, CYW43_COUNTRY_WORLDWIDE);
51 } else {
52 // Leave the STA network so the radio is free for scanning.
53 // Use cyw43_wifi_leave directly to avoid corrupting Arduino framework state.
54 cyw43_wifi_leave(&cyw43_state, CYW43_ITF_STA);
55 }
56 }
57
58 if (ap.has_value()) {
59 if (ap.value()) {
60 cyw43_wifi_set_up(&cyw43_state, CYW43_ITF_AP, true, CYW43_COUNTRY_WORLDWIDE);
61 } else {
62 cyw43_wifi_set_up(&cyw43_state, CYW43_ITF_AP, false, CYW43_COUNTRY_WORLDWIDE);
63 }
64 this->ap_started_ = ap.value();
65 }
66 return true;
67}
68
70 uint32_t pm;
71 switch (this->power_save_) {
73 pm = CYW43_PERFORMANCE_PM;
74 break;
76 pm = CYW43_DEFAULT_PM;
77 break;
79 pm = CYW43_AGGRESSIVE_PM;
80 break;
81 }
82 int ret = cyw43_wifi_pm(&cyw43_state, pm);
83 bool success = ret == 0;
84#ifdef USE_WIFI_POWER_SAVE_LISTENERS
85 if (success) {
86 for (auto *listener : this->power_save_listeners_) {
87 listener->on_wifi_power_save(this->power_save_);
88 }
89 }
90#endif
91 return success;
92}
93
94// TODO: The driver doesn't seem to have an API for this
95bool WiFiComponent::wifi_apply_output_power_(float output_power) { return true; }
96
97bool WiFiComponent::wifi_sta_connect_(const WiFiAP &ap) {
98#ifdef USE_WIFI_MANUAL_IP
99 if (!this->wifi_sta_ip_config_(ap.get_manual_ip()))
100 return false;
101#else
102 if (!this->wifi_sta_ip_config_({}))
103 return false;
104#endif
105
106 // Use beginNoBlock to avoid WiFi.begin()'s additional 2x timeout wait loop on top of
107 // CYW43::begin()'s internal blocking join. CYW43::begin() blocks for up to 10 seconds
108 // (default timeout) to complete the join - this is required because the LwipIntfDev netif
109 // setup depends on begin() succeeding. beginNoBlock() skips the outer wait loop, saving
110 // up to 20 additional seconds of blocking per attempt.
111 auto ret = WiFi.beginNoBlock(ap.ssid_.c_str(), ap.password_.c_str());
112 if (ret == WL_IDLE_STATUS)
113 return false;
114
115 return true;
116}
117
118bool WiFiComponent::wifi_sta_pre_setup_() { return this->wifi_mode_(true, {}); }
119
120bool WiFiComponent::wifi_sta_ip_config_(const optional<ManualIP> &manual_ip) {
121 if (!manual_ip.has_value()) {
122 return true;
123 }
124
125 IPAddress ip_address = manual_ip->static_ip;
126 IPAddress gateway = manual_ip->gateway;
127 IPAddress subnet = manual_ip->subnet;
128
129 IPAddress dns = manual_ip->dns1;
130
131 WiFi.config(ip_address, dns, gateway, subnet);
132 return true;
133}
134
136 WiFi.setHostname(App.get_name().c_str());
137 return true;
138}
139const char *get_auth_mode_str(uint8_t mode) {
140 // TODO:
141 return "UNKNOWN";
142}
143const char *get_disconnect_reason_str(uint8_t reason) {
144 // TODO:
145 return "UNKNOWN";
146}
147
149 // Use cyw43_wifi_link_status instead of cyw43_tcpip_link_status because the Arduino
150 // framework's __wrap_cyw43_cb_tcpip_init is a no-op — the SDK's internal netif
151 // (cyw43_state.netif[]) is never initialized. cyw43_tcpip_link_status checks that netif's
152 // flags and would only fall through to cyw43_wifi_link_status when the flags aren't set.
153 // Using cyw43_wifi_link_status directly gives us the actual WiFi radio join state.
154 int status = cyw43_wifi_link_status(&cyw43_state, CYW43_ITF_STA);
155 switch (status) {
156 case CYW43_LINK_JOIN:
157 // WiFi joined, check if STA has an IP address via wifi_sta_connected()
158 if (wifi_sta_connected()) {
160 }
162 case CYW43_LINK_FAIL:
163 case CYW43_LINK_BADAUTH:
165 case CYW43_LINK_NONET:
167 }
169}
170
171int WiFiComponent::s_wifi_scan_result(void *env, const cyw43_ev_scan_result_t *result) {
173 return 0;
174}
175
176void WiFiComponent::wifi_scan_result(void *env, const cyw43_ev_scan_result_t *result) {
177 s_scan_result_count++;
178
179 // CYW43 scan results have ssid as a 32-byte buffer that is NOT null-terminated.
180 // Use ssid_len to create a properly terminated copy for string operations.
181 uint8_t len = std::min(result->ssid_len, static_cast<uint8_t>(sizeof(result->ssid)));
182 char ssid_buf[33]; // 32 max + null terminator
183 memcpy(ssid_buf, result->ssid, len);
184 ssid_buf[len] = '\0';
185
186 // Skip networks that don't match any configured network (unless full results needed)
187 if (!this->needs_full_scan_results_() && !this->matches_configured_network_(ssid_buf, result->bssid)) {
188 this->log_discarded_scan_result_(ssid_buf, result->bssid, result->rssi, result->channel);
189 return;
190 }
191
192 bssid_t bssid;
193 std::copy(result->bssid, result->bssid + 6, bssid.begin());
194 WiFiScanResult res(bssid, ssid_buf, len, result->channel, result->rssi, result->auth_mode != CYW43_AUTH_OPEN,
195 len == 0);
196 if (std::find(this->scan_result_.begin(), this->scan_result_.end(), res) == this->scan_result_.end()) {
197 this->scan_result_.push_back(res);
198 }
199}
200
201bool WiFiComponent::wifi_scan_start_(bool passive) {
202 this->scan_result_.clear();
203 this->scan_done_ = false;
204 s_scan_result_count = 0;
205 cyw43_wifi_scan_options_t scan_options = {0};
206 scan_options.scan_type = passive ? 1 : 0;
207 int err = cyw43_wifi_scan(&cyw43_state, &scan_options, nullptr, &s_wifi_scan_result);
208 if (err) {
209 ESP_LOGV(TAG, "cyw43_wifi_scan failed");
210 }
211 return err == 0;
212}
213
214#ifdef USE_WIFI_AP
215bool WiFiComponent::wifi_ap_ip_config_(const optional<ManualIP> &manual_ip) {
216 // AP IP is configured by WiFi.beginAP() internally using defaults (192.168.4.1).
217 // Manual AP IP has never worked on RP2040 — WiFi.config() configures the STA
218 // interface, not the AP. This is now rejected at config validation time.
219 return true;
220}
221
222bool WiFiComponent::wifi_start_ap_(const WiFiAP &ap) {
223 if (!this->wifi_mode_({}, true))
224 return false;
225#ifdef USE_WIFI_MANUAL_IP
226 if (!this->wifi_ap_ip_config_(ap.get_manual_ip())) {
227 ESP_LOGV(TAG, "wifi_ap_ip_config_ failed");
228 return false;
229 }
230#else
231 if (!this->wifi_ap_ip_config_({})) {
232 ESP_LOGV(TAG, "wifi_ap_ip_config_ failed");
233 return false;
234 }
235#endif
236
237 // Pass nullptr for empty password — CYW43 uses the password pointer (not length)
238 // to choose between OPEN and WPA2 auth mode.
239 const char *ap_password = ap.password_.empty() ? nullptr : ap.password_.c_str();
240 WiFi.beginAP(ap.ssid_.c_str(), ap_password, ap.has_channel() ? ap.get_channel() : 1);
241
242 return true;
243}
244
245network::IPAddress WiFiComponent::wifi_soft_ap_ip() { return {(const ip_addr_t *) WiFi.softAPIP()}; }
246#endif // USE_WIFI_AP
247
249 // Use cyw43_wifi_leave() directly instead of WiFi.disconnect().
250 // WiFi.disconnect() sets _wifiHWInitted=false in the Arduino framework. beginAP()
251 // uses _wifiHWInitted to determine AP+STA vs AP-only mode — with it false,
252 // beginAP() enters AP-only mode (IP 192.168.42.1) instead of AP_STA mode
253 // (IP 192.168.4.1). In AP-only mode, _beginInternal() redirects all subsequent
254 // STA connect attempts to beginAP(), creating an infinite loop.
255 cyw43_wifi_leave(&cyw43_state, CYW43_ITF_STA);
256 return true;
257}
258
260 bssid_t bssid{};
261 uint8_t raw_bssid[6];
262 WiFi.BSSID(raw_bssid);
263 for (size_t i = 0; i < bssid.size(); i++)
264 bssid[i] = raw_bssid[i];
265 return bssid;
266}
267std::string WiFiComponent::wifi_ssid() { return WiFi.SSID().c_str(); }
268const char *WiFiComponent::wifi_ssid_to(std::span<char, SSID_BUFFER_SIZE> buffer) {
269 // TODO: Find direct CYW43 API to avoid Arduino String allocation
270 String ssid = WiFi.SSID();
271 size_t len = std::min(static_cast<size_t>(ssid.length()), SSID_BUFFER_SIZE - 1);
272 memcpy(buffer.data(), ssid.c_str(), len);
273 buffer[len] = '\0';
274 return buffer.data();
275}
276int8_t WiFiComponent::wifi_rssi() { return this->is_connected_() ? WiFi.RSSI() : WIFI_RSSI_DISCONNECTED; }
277int32_t WiFiComponent::get_wifi_channel() { return WiFi.channel(); }
278
280 network::IPAddresses addresses;
281 uint8_t index = 0;
282 // Filter out AP interface addresses — addrList includes all lwIP netifs.
283 // The AP netif IP lingers even after the AP radio is disabled.
284 IPAddress ap_ip = WiFi.softAPIP();
285 for (auto addr : addrList) {
286 IPAddress ip(addr.ipFromNetifNum());
287 if (ip == ap_ip) {
288 continue;
289 }
290 assert(index < addresses.size());
291 addresses[index++] = ip;
292 }
293 return addresses;
294}
295network::IPAddress WiFiComponent::wifi_subnet_mask_() { return {(const ip_addr_t *) WiFi.subnetMask()}; }
296network::IPAddress WiFiComponent::wifi_gateway_ip_() { return {(const ip_addr_t *) WiFi.gatewayIP()}; }
297network::IPAddress WiFiComponent::wifi_dns_ip_(int num) {
298 const ip_addr_t *dns_ip = dns_getserver(num);
299 return network::IPAddress(dns_ip);
300}
301
302// Pico W uses polling for connection state detection.
303// Connect state listener notifications are deferred until after the state machine
304// transitions (in check_connecting_finished) so that conditions like wifi.connected
305// return correct values in automations.
307 // Handle scan completion
308 if (this->state_ == WIFI_COMPONENT_STATE_STA_SCANNING && !cyw43_wifi_scan_active(&cyw43_state)) {
309 this->scan_done_ = true;
310 bool needs_full = this->needs_full_scan_results_();
311 ESP_LOGV(TAG, "Scan complete: %zu found, %zu stored%s", s_scan_result_count, this->scan_result_.size(),
312 needs_full ? "" : " (filtered)");
313#ifdef USE_WIFI_SCAN_RESULTS_LISTENERS
315#endif
316 }
317
318 // Poll for connection state changes
319 // The arduino-pico WiFi library doesn't have event callbacks like ESP8266/ESP32,
320 // so we need to poll the link status to detect state changes.
321 bool is_connected = wifi_sta_connected();
322
323 // Detect connection state change
324 if (is_connected && !s_sta_was_connected) {
325 // Just connected
326 s_sta_was_connected = true;
327 ESP_LOGV(TAG, "Connected");
328#ifdef USE_WIFI_CONNECT_STATE_LISTENERS
329 // Defer listener notification until state machine reaches STA_CONNECTED
330 // This ensures wifi.connected condition returns true in listener automations
331 this->pending_.connect_state = true;
332#endif
333 // For static IP configurations, notify IP listeners immediately as the IP is already configured
334#if defined(USE_WIFI_IP_STATE_LISTENERS) && defined(USE_WIFI_MANUAL_IP)
335 if (const WiFiAP *config = this->get_selected_sta_(); config && config->get_manual_ip().has_value()) {
336 s_sta_had_ip = true;
338 }
339#endif
340 } else if (!is_connected && s_sta_was_connected) {
341 // Just disconnected
342 s_sta_was_connected = false;
343 s_sta_had_ip = false;
344 ESP_LOGV(TAG, "Disconnected");
345#ifdef USE_WIFI_CONNECT_STATE_LISTENERS
347#endif
348 }
349
350 // Detect IP address changes (only when connected)
351 if (is_connected) {
352 bool has_ip = false;
353 // Check for any IP address (IPv4 or IPv6)
354 for (auto addr : addrList) {
355 has_ip = true;
356 break;
357 }
358
359 if (has_ip && !s_sta_had_ip) {
360 // Just got IP address
361 s_sta_had_ip = true;
362 ESP_LOGV(TAG, "Got IP address");
363#ifdef USE_WIFI_IP_STATE_LISTENERS
365#endif
366 }
367 }
368}
369
371
372} // namespace esphome::wifi
373#endif
374#endif
BedjetMode mode
BedJet operating mode.
uint8_t status
Definition bl0942.h:8
const StringRef & get_name() const
Get the name of this Application set by pre_setup().
constexpr const char * c_str() const
Definition string_ref.h:73
const optional< ManualIP > & get_manual_ip() const
void notify_scan_results_listeners_()
Notify scan results listeners with current scan results.
const WiFiAP * get_selected_sta_() const
WiFiSTAConnectStatus wifi_sta_connect_status_() const
struct esphome::wifi::WiFiComponent::@176 pending_
wifi_scan_vector_t< WiFiScanResult > scan_result_
void notify_ip_state_listeners_()
Notify IP state listeners with current addresses.
bool wifi_sta_ip_config_(const optional< ManualIP > &manual_ip)
static int s_wifi_scan_result(void *env, const cyw43_ev_scan_result_t *result)
void notify_disconnect_state_listeners_()
Notify connect state listeners of disconnection.
void log_discarded_scan_result_(const char *ssid, const uint8_t *bssid, int8_t rssi, uint8_t channel)
Log a discarded scan result at VERBOSE level (skipped during roaming scans to avoid log overflow)
ESPDEPRECATED("Use wifi_ssid_to() instead. Removed in 2026.9.0", "2026.3.0") std const char * wifi_ssid_to(std::span< char, SSID_BUFFER_SIZE > buffer)
Write SSID to buffer without heap allocation.
void wifi_scan_result(void *env, const cyw43_ev_scan_result_t *result)
network::IPAddress wifi_dns_ip_(int num)
bool matches_configured_network_(const char *ssid, const uint8_t *bssid) const
Check if network matches any configured network (for scan result filtering) Matches by SSID when conf...
bool wifi_ap_ip_config_(const optional< ManualIP > &manual_ip)
bool needs_full_scan_results_() const
Check if full scan results are needed (captive portal active, improv, listeners)
StaticVector< WiFiPowerSaveListener *, ESPHOME_WIFI_POWER_SAVE_LISTENERS > power_save_listeners_
bool wifi_apply_output_power_(float output_power)
bool wifi_mode_(optional< bool > sta, optional< bool > ap)
network::IPAddresses wifi_sta_ip_addresses()
in_addr ip_addr_t
Definition ip_address.h:22
std::array< IPAddress, 5 > IPAddresses
Definition ip_address.h:187
const char *const TAG
Definition spi.cpp:7
std::array< uint8_t, 6 > bssid_t
const LogString * get_auth_mode_str(uint8_t mode)
const LogString * get_disconnect_reason_str(uint8_t reason)
WiFiComponent * global_wifi_component
@ WIFI_COMPONENT_STATE_STA_SCANNING
WiFi is in STA-only mode and currently scanning for APs.
std::string size_t len
Definition helpers.h:892
Application App
Global storage of Application pointer - only one Application can exist.
static void uint32_t