ESPHome 2026.3.0-dev
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 "lwip/dns.h"
7#include "lwip/err.h"
8#include "lwip/netif.h"
9#include <AddrList.h>
10
12#include "esphome/core/hal.h"
14#include "esphome/core/log.h"
15#include "esphome/core/util.h"
16
17namespace esphome::wifi {
18
19static const char *const TAG = "wifi_pico_w";
20
21// Track previous state for detecting changes
22static bool s_sta_was_connected = false; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
23static bool s_sta_had_ip = false; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
24static size_t s_scan_result_count = 0; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
25
26bool WiFiComponent::wifi_mode_(optional<bool> sta, optional<bool> ap) {
27 if (sta.has_value()) {
28 if (sta.value()) {
29 cyw43_wifi_set_up(&cyw43_state, CYW43_ITF_STA, true, CYW43_COUNTRY_WORLDWIDE);
30 }
31 }
32
33 bool ap_state = false;
34 if (ap.has_value()) {
35 if (ap.value()) {
36 cyw43_wifi_set_up(&cyw43_state, CYW43_ITF_AP, true, CYW43_COUNTRY_WORLDWIDE);
37 ap_state = true;
38 }
39 }
40 this->ap_started_ = ap_state;
41 return true;
42}
43
45 uint32_t pm;
46 switch (this->power_save_) {
48 pm = CYW43_PERFORMANCE_PM;
49 break;
51 pm = CYW43_DEFAULT_PM;
52 break;
54 pm = CYW43_AGGRESSIVE_PM;
55 break;
56 }
57 int ret = cyw43_wifi_pm(&cyw43_state, pm);
58 bool success = ret == 0;
59#ifdef USE_WIFI_POWER_SAVE_LISTENERS
60 if (success) {
61 for (auto *listener : this->power_save_listeners_) {
62 listener->on_wifi_power_save(this->power_save_);
63 }
64 }
65#endif
66 return success;
67}
68
69// TODO: The driver doesn't seem to have an API for this
70bool WiFiComponent::wifi_apply_output_power_(float output_power) { return true; }
71
72bool WiFiComponent::wifi_sta_connect_(const WiFiAP &ap) {
73#ifdef USE_WIFI_MANUAL_IP
74 if (!this->wifi_sta_ip_config_(ap.get_manual_ip()))
75 return false;
76#else
77 if (!this->wifi_sta_ip_config_({}))
78 return false;
79#endif
80
81 // Use beginNoBlock to avoid WiFi.begin()'s additional 2x timeout wait loop on top of
82 // CYW43::begin()'s internal blocking join. CYW43::begin() blocks for up to 10 seconds
83 // (default timeout) to complete the join - this is required because the LwipIntfDev netif
84 // setup depends on begin() succeeding. beginNoBlock() skips the outer wait loop, saving
85 // up to 20 additional seconds of blocking per attempt.
86 auto ret = WiFi.beginNoBlock(ap.ssid_.c_str(), ap.password_.c_str());
87 if (ret == WL_IDLE_STATUS)
88 return false;
89
90 return true;
91}
92
93bool WiFiComponent::wifi_sta_pre_setup_() { return this->wifi_mode_(true, {}); }
94
95bool WiFiComponent::wifi_sta_ip_config_(const optional<ManualIP> &manual_ip) {
96 if (!manual_ip.has_value()) {
97 return true;
98 }
99
100 IPAddress ip_address = manual_ip->static_ip;
101 IPAddress gateway = manual_ip->gateway;
102 IPAddress subnet = manual_ip->subnet;
103
104 IPAddress dns = manual_ip->dns1;
105
106 WiFi.config(ip_address, dns, gateway, subnet);
107 return true;
108}
109
111 WiFi.setHostname(App.get_name().c_str());
112 return true;
113}
114const char *get_auth_mode_str(uint8_t mode) {
115 // TODO:
116 return "UNKNOWN";
117}
118const char *get_disconnect_reason_str(uint8_t reason) {
119 // TODO:
120 return "UNKNOWN";
121}
122
124 // Use cyw43_wifi_link_status instead of cyw43_tcpip_link_status because the Arduino
125 // framework's __wrap_cyw43_cb_tcpip_init is a no-op — the SDK's internal netif
126 // (cyw43_state.netif[]) is never initialized. cyw43_tcpip_link_status checks that netif's
127 // flags and would only fall through to cyw43_wifi_link_status when the flags aren't set.
128 // Using cyw43_wifi_link_status directly gives us the actual WiFi radio join state.
129 int status = cyw43_wifi_link_status(&cyw43_state, CYW43_ITF_STA);
130 switch (status) {
131 case CYW43_LINK_JOIN:
132 // WiFi joined, check if we have an IP address via the Arduino framework's WiFi class
133 if (WiFi.status() == WL_CONNECTED) {
135 }
137 case CYW43_LINK_FAIL:
138 case CYW43_LINK_BADAUTH:
140 case CYW43_LINK_NONET:
142 }
144}
145
146int WiFiComponent::s_wifi_scan_result(void *env, const cyw43_ev_scan_result_t *result) {
148 return 0;
149}
150
151void WiFiComponent::wifi_scan_result(void *env, const cyw43_ev_scan_result_t *result) {
152 s_scan_result_count++;
153
154 // CYW43 scan results have ssid as a 32-byte buffer that is NOT null-terminated.
155 // Use ssid_len to create a properly terminated copy for string operations.
156 uint8_t len = std::min(result->ssid_len, static_cast<uint8_t>(sizeof(result->ssid)));
157 char ssid_buf[33]; // 32 max + null terminator
158 memcpy(ssid_buf, result->ssid, len);
159 ssid_buf[len] = '\0';
160
161 // Skip networks that don't match any configured network (unless full results needed)
162 if (!this->needs_full_scan_results_() && !this->matches_configured_network_(ssid_buf, result->bssid)) {
163 this->log_discarded_scan_result_(ssid_buf, result->bssid, result->rssi, result->channel);
164 return;
165 }
166
167 bssid_t bssid;
168 std::copy(result->bssid, result->bssid + 6, bssid.begin());
169 WiFiScanResult res(bssid, ssid_buf, len, result->channel, result->rssi, result->auth_mode != CYW43_AUTH_OPEN,
170 len == 0);
171 if (std::find(this->scan_result_.begin(), this->scan_result_.end(), res) == this->scan_result_.end()) {
172 this->scan_result_.push_back(res);
173 }
174}
175
176bool WiFiComponent::wifi_scan_start_(bool passive) {
177 this->scan_result_.clear();
178 this->scan_done_ = false;
179 s_scan_result_count = 0;
180 cyw43_wifi_scan_options_t scan_options = {0};
181 scan_options.scan_type = passive ? 1 : 0;
182 int err = cyw43_wifi_scan(&cyw43_state, &scan_options, nullptr, &s_wifi_scan_result);
183 if (err) {
184 ESP_LOGV(TAG, "cyw43_wifi_scan failed");
185 }
186 return err == 0;
187}
188
189#ifdef USE_WIFI_AP
190bool WiFiComponent::wifi_ap_ip_config_(const optional<ManualIP> &manual_ip) {
191 esphome::network::IPAddress ip_address, gateway, subnet, dns;
192 if (manual_ip.has_value()) {
193 ip_address = manual_ip->static_ip;
194 gateway = manual_ip->gateway;
195 subnet = manual_ip->subnet;
196 dns = manual_ip->static_ip;
197 } else {
198 ip_address = network::IPAddress(192, 168, 4, 1);
199 gateway = network::IPAddress(192, 168, 4, 1);
200 subnet = network::IPAddress(255, 255, 255, 0);
201 dns = network::IPAddress(192, 168, 4, 1);
202 }
203 WiFi.config(ip_address, dns, gateway, subnet);
204 return true;
205}
206
207bool WiFiComponent::wifi_start_ap_(const WiFiAP &ap) {
208 if (!this->wifi_mode_({}, true))
209 return false;
210#ifdef USE_WIFI_MANUAL_IP
211 if (!this->wifi_ap_ip_config_(ap.get_manual_ip())) {
212 ESP_LOGV(TAG, "wifi_ap_ip_config_ failed");
213 return false;
214 }
215#else
216 if (!this->wifi_ap_ip_config_({})) {
217 ESP_LOGV(TAG, "wifi_ap_ip_config_ failed");
218 return false;
219 }
220#endif
221
222 WiFi.beginAP(ap.ssid_.c_str(), ap.password_.c_str(), ap.has_channel() ? ap.get_channel() : 1);
223
224 return true;
225}
226
227network::IPAddress WiFiComponent::wifi_soft_ap_ip() { return {(const ip_addr_t *) WiFi.localIP()}; }
228#endif // USE_WIFI_AP
229
231 // Use Arduino WiFi.disconnect() instead of raw cyw43_wifi_leave() to properly
232 // clean up the lwIP netif, DHCP client, and internal Arduino state.
233 WiFi.disconnect();
234 return true;
235}
236
238 bssid_t bssid{};
239 uint8_t raw_bssid[6];
240 WiFi.BSSID(raw_bssid);
241 for (size_t i = 0; i < bssid.size(); i++)
242 bssid[i] = raw_bssid[i];
243 return bssid;
244}
245std::string WiFiComponent::wifi_ssid() { return WiFi.SSID().c_str(); }
246const char *WiFiComponent::wifi_ssid_to(std::span<char, SSID_BUFFER_SIZE> buffer) {
247 // TODO: Find direct CYW43 API to avoid Arduino String allocation
248 String ssid = WiFi.SSID();
249 size_t len = std::min(static_cast<size_t>(ssid.length()), SSID_BUFFER_SIZE - 1);
250 memcpy(buffer.data(), ssid.c_str(), len);
251 buffer[len] = '\0';
252 return buffer.data();
253}
254int8_t WiFiComponent::wifi_rssi() { return WiFi.status() == WL_CONNECTED ? WiFi.RSSI() : WIFI_RSSI_DISCONNECTED; }
255int32_t WiFiComponent::get_wifi_channel() { return WiFi.channel(); }
256
258 network::IPAddresses addresses;
259 uint8_t index = 0;
260 for (auto addr : addrList) {
261 addresses[index++] = addr.ipFromNetifNum();
262 }
263 return addresses;
264}
265network::IPAddress WiFiComponent::wifi_subnet_mask_() { return {(const ip_addr_t *) WiFi.subnetMask()}; }
266network::IPAddress WiFiComponent::wifi_gateway_ip_() { return {(const ip_addr_t *) WiFi.gatewayIP()}; }
267network::IPAddress WiFiComponent::wifi_dns_ip_(int num) {
268 const ip_addr_t *dns_ip = dns_getserver(num);
269 return network::IPAddress(dns_ip);
270}
271
272// Pico W uses polling for connection state detection.
273// Connect state listener notifications are deferred until after the state machine
274// transitions (in check_connecting_finished) so that conditions like wifi.connected
275// return correct values in automations.
277 // Handle scan completion
278 if (this->state_ == WIFI_COMPONENT_STATE_STA_SCANNING && !cyw43_wifi_scan_active(&cyw43_state)) {
279 this->scan_done_ = true;
280 bool needs_full = this->needs_full_scan_results_();
281 ESP_LOGV(TAG, "Scan complete: %zu found, %zu stored%s", s_scan_result_count, this->scan_result_.size(),
282 needs_full ? "" : " (filtered)");
283#ifdef USE_WIFI_SCAN_RESULTS_LISTENERS
285#endif
286 }
287
288 // Poll for connection state changes
289 // The arduino-pico WiFi library doesn't have event callbacks like ESP8266/ESP32,
290 // so we need to poll the link status to detect state changes.
291 // Use WiFi.connected() which checks both the WiFi link and IP address via the
292 // Arduino framework's own netif (not the SDK's uninitialized one).
293 bool is_connected = WiFi.connected();
294
295 // Detect connection state change
296 if (is_connected && !s_sta_was_connected) {
297 // Just connected
298 s_sta_was_connected = true;
299 ESP_LOGV(TAG, "Connected");
300#ifdef USE_WIFI_CONNECT_STATE_LISTENERS
301 // Defer listener notification until state machine reaches STA_CONNECTED
302 // This ensures wifi.connected condition returns true in listener automations
303 this->pending_.connect_state = true;
304#endif
305 // For static IP configurations, notify IP listeners immediately as the IP is already configured
306#if defined(USE_WIFI_IP_STATE_LISTENERS) && defined(USE_WIFI_MANUAL_IP)
307 if (const WiFiAP *config = this->get_selected_sta_(); config && config->get_manual_ip().has_value()) {
308 s_sta_had_ip = true;
310 }
311#endif
312 } else if (!is_connected && s_sta_was_connected) {
313 // Just disconnected
314 s_sta_was_connected = false;
315 s_sta_had_ip = false;
316 ESP_LOGV(TAG, "Disconnected");
317#ifdef USE_WIFI_CONNECT_STATE_LISTENERS
319#endif
320 }
321
322 // Detect IP address changes (only when connected)
323 if (is_connected) {
324 bool has_ip = false;
325 // Check for any IP address (IPv4 or IPv6)
326 for (auto addr : addrList) {
327 has_ip = true;
328 break;
329 }
330
331 if (has_ip && !s_sta_had_ip) {
332 // Just got IP address
333 s_sta_had_ip = true;
334 ESP_LOGV(TAG, "Got IP address");
335#ifdef USE_WIFI_IP_STATE_LISTENERS
337#endif
338 }
339 }
340}
341
343
344} // namespace esphome::wifi
345#endif
346#endif
BedjetMode mode
BedJet operating mode.
uint8_t status
Definition bl0942.h:8
const std::string & get_name() const
Get the name of this Application set by pre_setup().
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::@175 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:817
Application App
Global storage of Application pointer - only one Application can exist.