ESPHome 2025.12.0-dev
Loading...
Searching...
No Matches
wifi_component_pico_w.cpp
Go to the documentation of this file.
1
2#include "wifi_component.h"
3
4#ifdef USE_WIFI
5#ifdef USE_RP2040
6
7#include "lwip/dns.h"
8#include "lwip/err.h"
9#include "lwip/netif.h"
10#include <AddrList.h>
11
13#include "esphome/core/hal.h"
15#include "esphome/core/log.h"
16#include "esphome/core/util.h"
17
18namespace esphome {
19namespace wifi {
20
21static const char *const TAG = "wifi_pico_w";
22
23bool WiFiComponent::wifi_mode_(optional<bool> sta, optional<bool> ap) {
24 if (sta.has_value()) {
25 if (sta.value()) {
26 cyw43_wifi_set_up(&cyw43_state, CYW43_ITF_STA, true, CYW43_COUNTRY_WORLDWIDE);
27 }
28 }
29 if (ap.has_value()) {
30 if (ap.value()) {
31 cyw43_wifi_set_up(&cyw43_state, CYW43_ITF_AP, true, CYW43_COUNTRY_WORLDWIDE);
32 }
33 }
34 return true;
35}
36
38 uint32_t pm;
39 switch (this->power_save_) {
41 pm = CYW43_PERFORMANCE_PM;
42 break;
44 pm = CYW43_DEFAULT_PM;
45 break;
47 pm = CYW43_AGGRESSIVE_PM;
48 break;
49 }
50 int ret = cyw43_wifi_pm(&cyw43_state, pm);
51 return ret == 0;
52}
53
54// TODO: The driver doesnt seem to have an API for this
55bool WiFiComponent::wifi_apply_output_power_(float output_power) { return true; }
56
57bool WiFiComponent::wifi_sta_connect_(const WiFiAP &ap) {
58#ifdef USE_WIFI_MANUAL_IP
59 if (!this->wifi_sta_ip_config_(ap.get_manual_ip()))
60 return false;
61#else
62 if (!this->wifi_sta_ip_config_({}))
63 return false;
64#endif
65
66 auto ret = WiFi.begin(ap.get_ssid().c_str(), ap.get_password().c_str());
67 if (ret != WL_CONNECTED)
68 return false;
69
70 return true;
71}
72
73bool WiFiComponent::wifi_sta_pre_setup_() { return this->wifi_mode_(true, {}); }
74
75bool WiFiComponent::wifi_sta_ip_config_(const optional<ManualIP> &manual_ip) {
76 if (!manual_ip.has_value()) {
77 return true;
78 }
79
80 IPAddress ip_address = manual_ip->static_ip;
81 IPAddress gateway = manual_ip->gateway;
82 IPAddress subnet = manual_ip->subnet;
83
84 IPAddress dns = manual_ip->dns1;
85
86 WiFi.config(ip_address, dns, gateway, subnet);
87 return true;
88}
89
91 WiFi.setHostname(App.get_name().c_str());
92 return true;
93}
94const char *get_auth_mode_str(uint8_t mode) {
95 // TODO:
96 return "UNKNOWN";
97}
98const char *get_disconnect_reason_str(uint8_t reason) {
99 // TODO:
100 return "UNKNOWN";
101}
102
104 int status = cyw43_tcpip_link_status(&cyw43_state, CYW43_ITF_STA);
105 switch (status) {
106 case CYW43_LINK_JOIN:
107 case CYW43_LINK_NOIP:
109 case CYW43_LINK_UP:
111 case CYW43_LINK_FAIL:
112 case CYW43_LINK_BADAUTH:
114 case CYW43_LINK_NONET:
116 }
118}
119
120int WiFiComponent::s_wifi_scan_result(void *env, const cyw43_ev_scan_result_t *result) {
122 return 0;
123}
124
125void WiFiComponent::wifi_scan_result(void *env, const cyw43_ev_scan_result_t *result) {
126 bssid_t bssid;
127 std::copy(result->bssid, result->bssid + 6, bssid.begin());
128 std::string ssid(reinterpret_cast<const char *>(result->ssid));
129 WiFiScanResult res(bssid, ssid, result->channel, result->rssi, result->auth_mode != CYW43_AUTH_OPEN, ssid.empty());
130 if (std::find(this->scan_result_.begin(), this->scan_result_.end(), res) == this->scan_result_.end()) {
131 this->scan_result_.push_back(res);
132 }
133}
134
135bool WiFiComponent::wifi_scan_start_(bool passive) {
136 this->scan_result_.clear();
137 this->scan_done_ = false;
138 cyw43_wifi_scan_options_t scan_options = {0};
139 scan_options.scan_type = passive ? 1 : 0;
140 int err = cyw43_wifi_scan(&cyw43_state, &scan_options, nullptr, &s_wifi_scan_result);
141 if (err) {
142 ESP_LOGV(TAG, "cyw43_wifi_scan failed");
143 }
144 return err == 0;
145 return true;
146}
147
148#ifdef USE_WIFI_AP
149bool WiFiComponent::wifi_ap_ip_config_(const optional<ManualIP> &manual_ip) {
150 esphome::network::IPAddress ip_address, gateway, subnet, dns;
151 if (manual_ip.has_value()) {
152 ip_address = manual_ip->static_ip;
153 gateway = manual_ip->gateway;
154 subnet = manual_ip->subnet;
155 dns = manual_ip->static_ip;
156 } else {
157 ip_address = network::IPAddress(192, 168, 4, 1);
158 gateway = network::IPAddress(192, 168, 4, 1);
159 subnet = network::IPAddress(255, 255, 255, 0);
160 dns = network::IPAddress(192, 168, 4, 1);
161 }
162 WiFi.config(ip_address, dns, gateway, subnet);
163 return true;
164}
165
166bool WiFiComponent::wifi_start_ap_(const WiFiAP &ap) {
167 if (!this->wifi_mode_({}, true))
168 return false;
169#ifdef USE_WIFI_MANUAL_IP
170 if (!this->wifi_ap_ip_config_(ap.get_manual_ip())) {
171 ESP_LOGV(TAG, "wifi_ap_ip_config_ failed");
172 return false;
173 }
174#else
175 if (!this->wifi_ap_ip_config_({})) {
176 ESP_LOGV(TAG, "wifi_ap_ip_config_ failed");
177 return false;
178 }
179#endif
180
181 WiFi.beginAP(ap.get_ssid().c_str(), ap.get_password().c_str(), ap.get_channel().value_or(1));
182
183 return true;
184}
185
186network::IPAddress WiFiComponent::wifi_soft_ap_ip() { return {(const ip_addr_t *) WiFi.localIP()}; }
187#endif // USE_WIFI_AP
188
190 int err = cyw43_wifi_leave(&cyw43_state, CYW43_ITF_STA);
191 return err == 0;
192}
193
195 bssid_t bssid{};
196 uint8_t raw_bssid[6];
197 WiFi.BSSID(raw_bssid);
198 for (size_t i = 0; i < bssid.size(); i++)
199 bssid[i] = raw_bssid[i];
200 return bssid;
201}
202std::string WiFiComponent::wifi_ssid() { return WiFi.SSID().c_str(); }
203int8_t WiFiComponent::wifi_rssi() { return WiFi.status() == WL_CONNECTED ? WiFi.RSSI() : WIFI_RSSI_DISCONNECTED; }
204int32_t WiFiComponent::get_wifi_channel() { return WiFi.channel(); }
205
207 network::IPAddresses addresses;
208 uint8_t index = 0;
209 for (auto addr : addrList) {
210 addresses[index++] = addr.ipFromNetifNum();
211 }
212 return addresses;
213}
214network::IPAddress WiFiComponent::wifi_subnet_mask_() { return {(const ip_addr_t *) WiFi.subnetMask()}; }
215network::IPAddress WiFiComponent::wifi_gateway_ip_() { return {(const ip_addr_t *) WiFi.gatewayIP()}; }
216network::IPAddress WiFiComponent::wifi_dns_ip_(int num) {
217 const ip_addr_t *dns_ip = dns_getserver(num);
218 return network::IPAddress(dns_ip);
219}
220
222 if (this->state_ == WIFI_COMPONENT_STATE_STA_SCANNING && !cyw43_wifi_scan_active(&cyw43_state)) {
223 this->scan_done_ = true;
224 ESP_LOGV(TAG, "Scan done");
225 }
226}
227
229
230} // namespace wifi
231} // namespace esphome
232
233#endif
234#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().
wifi_scan_vector_t< WiFiScanResult > scan_result_
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 wifi_scan_result(void *env, const cyw43_ev_scan_result_t *result)
network::IPAddress wifi_dns_ip_(int num)
bool wifi_ap_ip_config_(const optional< ManualIP > &manual_ip)
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:144
const char *const TAG
Definition spi.cpp:8
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.
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
Application App
Global storage of Application pointer - only one Application can exist.