ESPHome 2026.2.2
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 auto ret = WiFi.begin(ap.ssid_.c_str(), ap.password_.c_str());
82 if (ret != WL_CONNECTED)
83 return false;
84
85 return true;
86}
87
88bool WiFiComponent::wifi_sta_pre_setup_() { return this->wifi_mode_(true, {}); }
89
90bool WiFiComponent::wifi_sta_ip_config_(const optional<ManualIP> &manual_ip) {
91 if (!manual_ip.has_value()) {
92 return true;
93 }
94
95 IPAddress ip_address = manual_ip->static_ip;
96 IPAddress gateway = manual_ip->gateway;
97 IPAddress subnet = manual_ip->subnet;
98
99 IPAddress dns = manual_ip->dns1;
100
101 WiFi.config(ip_address, dns, gateway, subnet);
102 return true;
103}
104
106 WiFi.setHostname(App.get_name().c_str());
107 return true;
108}
109const char *get_auth_mode_str(uint8_t mode) {
110 // TODO:
111 return "UNKNOWN";
112}
113const char *get_disconnect_reason_str(uint8_t reason) {
114 // TODO:
115 return "UNKNOWN";
116}
117
119 int status = cyw43_tcpip_link_status(&cyw43_state, CYW43_ITF_STA);
120 switch (status) {
121 case CYW43_LINK_JOIN:
122 case CYW43_LINK_NOIP:
124 case CYW43_LINK_UP:
126 case CYW43_LINK_FAIL:
127 case CYW43_LINK_BADAUTH:
129 case CYW43_LINK_NONET:
131 }
133}
134
135int WiFiComponent::s_wifi_scan_result(void *env, const cyw43_ev_scan_result_t *result) {
137 return 0;
138}
139
140void WiFiComponent::wifi_scan_result(void *env, const cyw43_ev_scan_result_t *result) {
141 s_scan_result_count++;
142 const char *ssid_cstr = reinterpret_cast<const char *>(result->ssid);
143
144 // Skip networks that don't match any configured network (unless full results needed)
145 if (!this->needs_full_scan_results_() && !this->matches_configured_network_(ssid_cstr, result->bssid)) {
146 this->log_discarded_scan_result_(ssid_cstr, result->bssid, result->rssi, result->channel);
147 return;
148 }
149
150 bssid_t bssid;
151 std::copy(result->bssid, result->bssid + 6, bssid.begin());
152 WiFiScanResult res(bssid, ssid_cstr, strlen(ssid_cstr), result->channel, result->rssi,
153 result->auth_mode != CYW43_AUTH_OPEN, ssid_cstr[0] == '\0');
154 if (std::find(this->scan_result_.begin(), this->scan_result_.end(), res) == this->scan_result_.end()) {
155 this->scan_result_.push_back(res);
156 }
157}
158
159bool WiFiComponent::wifi_scan_start_(bool passive) {
160 this->scan_result_.clear();
161 this->scan_done_ = false;
162 s_scan_result_count = 0;
163 cyw43_wifi_scan_options_t scan_options = {0};
164 scan_options.scan_type = passive ? 1 : 0;
165 int err = cyw43_wifi_scan(&cyw43_state, &scan_options, nullptr, &s_wifi_scan_result);
166 if (err) {
167 ESP_LOGV(TAG, "cyw43_wifi_scan failed");
168 }
169 return err == 0;
170 return true;
171}
172
173#ifdef USE_WIFI_AP
174bool WiFiComponent::wifi_ap_ip_config_(const optional<ManualIP> &manual_ip) {
175 esphome::network::IPAddress ip_address, gateway, subnet, dns;
176 if (manual_ip.has_value()) {
177 ip_address = manual_ip->static_ip;
178 gateway = manual_ip->gateway;
179 subnet = manual_ip->subnet;
180 dns = manual_ip->static_ip;
181 } else {
182 ip_address = network::IPAddress(192, 168, 4, 1);
183 gateway = network::IPAddress(192, 168, 4, 1);
184 subnet = network::IPAddress(255, 255, 255, 0);
185 dns = network::IPAddress(192, 168, 4, 1);
186 }
187 WiFi.config(ip_address, dns, gateway, subnet);
188 return true;
189}
190
191bool WiFiComponent::wifi_start_ap_(const WiFiAP &ap) {
192 if (!this->wifi_mode_({}, true))
193 return false;
194#ifdef USE_WIFI_MANUAL_IP
195 if (!this->wifi_ap_ip_config_(ap.get_manual_ip())) {
196 ESP_LOGV(TAG, "wifi_ap_ip_config_ failed");
197 return false;
198 }
199#else
200 if (!this->wifi_ap_ip_config_({})) {
201 ESP_LOGV(TAG, "wifi_ap_ip_config_ failed");
202 return false;
203 }
204#endif
205
206 WiFi.beginAP(ap.ssid_.c_str(), ap.password_.c_str(), ap.has_channel() ? ap.get_channel() : 1);
207
208 return true;
209}
210
211network::IPAddress WiFiComponent::wifi_soft_ap_ip() { return {(const ip_addr_t *) WiFi.localIP()}; }
212#endif // USE_WIFI_AP
213
215 int err = cyw43_wifi_leave(&cyw43_state, CYW43_ITF_STA);
216 return err == 0;
217}
218
220 bssid_t bssid{};
221 uint8_t raw_bssid[6];
222 WiFi.BSSID(raw_bssid);
223 for (size_t i = 0; i < bssid.size(); i++)
224 bssid[i] = raw_bssid[i];
225 return bssid;
226}
227std::string WiFiComponent::wifi_ssid() { return WiFi.SSID().c_str(); }
228const char *WiFiComponent::wifi_ssid_to(std::span<char, SSID_BUFFER_SIZE> buffer) {
229 // TODO: Find direct CYW43 API to avoid Arduino String allocation
230 String ssid = WiFi.SSID();
231 size_t len = std::min(static_cast<size_t>(ssid.length()), SSID_BUFFER_SIZE - 1);
232 memcpy(buffer.data(), ssid.c_str(), len);
233 buffer[len] = '\0';
234 return buffer.data();
235}
236int8_t WiFiComponent::wifi_rssi() { return WiFi.status() == WL_CONNECTED ? WiFi.RSSI() : WIFI_RSSI_DISCONNECTED; }
237int32_t WiFiComponent::get_wifi_channel() { return WiFi.channel(); }
238
240 network::IPAddresses addresses;
241 uint8_t index = 0;
242 for (auto addr : addrList) {
243 addresses[index++] = addr.ipFromNetifNum();
244 }
245 return addresses;
246}
247network::IPAddress WiFiComponent::wifi_subnet_mask_() { return {(const ip_addr_t *) WiFi.subnetMask()}; }
248network::IPAddress WiFiComponent::wifi_gateway_ip_() { return {(const ip_addr_t *) WiFi.gatewayIP()}; }
249network::IPAddress WiFiComponent::wifi_dns_ip_(int num) {
250 const ip_addr_t *dns_ip = dns_getserver(num);
251 return network::IPAddress(dns_ip);
252}
253
254// Pico W uses polling for connection state detection.
255// Connect state listener notifications are deferred until after the state machine
256// transitions (in check_connecting_finished) so that conditions like wifi.connected
257// return correct values in automations.
259 // Handle scan completion
260 if (this->state_ == WIFI_COMPONENT_STATE_STA_SCANNING && !cyw43_wifi_scan_active(&cyw43_state)) {
261 this->scan_done_ = true;
262 bool needs_full = this->needs_full_scan_results_();
263 ESP_LOGV(TAG, "Scan complete: %zu found, %zu stored%s", s_scan_result_count, this->scan_result_.size(),
264 needs_full ? "" : " (filtered)");
265#ifdef USE_WIFI_SCAN_RESULTS_LISTENERS
267#endif
268 }
269
270 // Poll for connection state changes
271 // The arduino-pico WiFi library doesn't have event callbacks like ESP8266/ESP32,
272 // so we need to poll the link status to detect state changes
273 auto status = cyw43_tcpip_link_status(&cyw43_state, CYW43_ITF_STA);
274 bool is_connected = (status == CYW43_LINK_UP);
275
276 // Detect connection state change
277 if (is_connected && !s_sta_was_connected) {
278 // Just connected
279 s_sta_was_connected = true;
280 ESP_LOGV(TAG, "Connected");
281#ifdef USE_WIFI_CONNECT_STATE_LISTENERS
282 // Defer listener notification until state machine reaches STA_CONNECTED
283 // This ensures wifi.connected condition returns true in listener automations
284 this->pending_.connect_state = true;
285#endif
286 // For static IP configurations, notify IP listeners immediately as the IP is already configured
287#if defined(USE_WIFI_IP_STATE_LISTENERS) && defined(USE_WIFI_MANUAL_IP)
288 if (const WiFiAP *config = this->get_selected_sta_(); config && config->get_manual_ip().has_value()) {
289 s_sta_had_ip = true;
291 }
292#endif
293 } else if (!is_connected && s_sta_was_connected) {
294 // Just disconnected
295 s_sta_was_connected = false;
296 s_sta_had_ip = false;
297 ESP_LOGV(TAG, "Disconnected");
298#ifdef USE_WIFI_CONNECT_STATE_LISTENERS
300#endif
301 }
302
303 // Detect IP address changes (only when connected)
304 if (is_connected) {
305 bool has_ip = false;
306 // Check for any IP address (IPv4 or IPv6)
307 for (auto addr : addrList) {
308 has_ip = true;
309 break;
310 }
311
312 if (has_ip && !s_sta_had_ip) {
313 // Just got IP address
314 s_sta_had_ip = true;
315 ESP_LOGV(TAG, "Got IP address");
316#ifdef USE_WIFI_IP_STATE_LISTENERS
318#endif
319 }
320 }
321}
322
324
325} // namespace esphome::wifi
326#endif
327#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
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)
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)
WiFiSTAConnectStatus wifi_sta_connect_status_()
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:188
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:692
Application App
Global storage of Application pointer - only one Application can exist.