ESPHome  2024.9.0
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"
14 #include "esphome/core/helpers.h"
15 #include "esphome/core/log.h"
16 #include "esphome/core/util.h"
17 
18 namespace esphome {
19 namespace wifi {
20 
21 static const char *const TAG = "wifi_pico_w";
22 
23 bool 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
55 bool WiFiComponent::wifi_apply_output_power_(float output_power) { return true; }
56 
57 bool WiFiComponent::wifi_sta_connect_(const WiFiAP &ap) {
58  if (!this->wifi_sta_ip_config_(ap.get_manual_ip()))
59  return false;
60 
61  auto ret = WiFi.begin(ap.get_ssid().c_str(), ap.get_password().c_str());
62  if (ret != WL_CONNECTED)
63  return false;
64 
65  return true;
66 }
67 
68 bool WiFiComponent::wifi_sta_pre_setup_() { return this->wifi_mode_(true, {}); }
69 
70 bool WiFiComponent::wifi_sta_ip_config_(optional<ManualIP> manual_ip) {
71  if (!manual_ip.has_value()) {
72  return true;
73  }
74 
75  IPAddress ip_address = manual_ip->static_ip;
76  IPAddress gateway = manual_ip->gateway;
77  IPAddress subnet = manual_ip->subnet;
78 
79  IPAddress dns = manual_ip->dns1;
80 
81  WiFi.config(ip_address, dns, gateway, subnet);
82  return true;
83 }
84 
86  WiFi.setHostname(App.get_name().c_str());
87  return true;
88 }
89 const char *get_auth_mode_str(uint8_t mode) {
90  // TODO:
91  return "UNKNOWN";
92 }
93 const char *get_disconnect_reason_str(uint8_t reason) {
94  // TODO:
95  return "UNKNOWN";
96 }
97 
99  int status = cyw43_tcpip_link_status(&cyw43_state, CYW43_ITF_STA);
100  switch (status) {
101  case CYW43_LINK_JOIN:
102  case CYW43_LINK_NOIP:
104  case CYW43_LINK_UP:
106  case CYW43_LINK_FAIL:
107  case CYW43_LINK_BADAUTH:
109  case CYW43_LINK_NONET:
111  }
113 }
114 
115 int WiFiComponent::s_wifi_scan_result(void *env, const cyw43_ev_scan_result_t *result) {
117  return 0;
118 }
119 
120 void WiFiComponent::wifi_scan_result(void *env, const cyw43_ev_scan_result_t *result) {
121  bssid_t bssid;
122  std::copy(result->bssid, result->bssid + 6, bssid.begin());
123  std::string ssid(reinterpret_cast<const char *>(result->ssid));
124  WiFiScanResult res(bssid, ssid, result->channel, result->rssi, result->auth_mode != CYW43_AUTH_OPEN, ssid.empty());
125  if (std::find(this->scan_result_.begin(), this->scan_result_.end(), res) == this->scan_result_.end()) {
126  this->scan_result_.push_back(res);
127  }
128 }
129 
130 bool WiFiComponent::wifi_scan_start_(bool passive) {
131  this->scan_result_.clear();
132  this->scan_done_ = false;
133  cyw43_wifi_scan_options_t scan_options = {0};
134  scan_options.scan_type = passive ? 1 : 0;
135  int err = cyw43_wifi_scan(&cyw43_state, &scan_options, nullptr, &s_wifi_scan_result);
136  if (err) {
137  ESP_LOGV(TAG, "cyw43_wifi_scan failed!");
138  }
139  return err == 0;
140  return true;
141 }
142 
143 #ifdef USE_WIFI_AP
145  esphome::network::IPAddress ip_address, gateway, subnet, dns;
146  if (manual_ip.has_value()) {
147  ip_address = manual_ip->static_ip;
148  gateway = manual_ip->gateway;
149  subnet = manual_ip->subnet;
150  dns = manual_ip->static_ip;
151  } else {
152  ip_address = network::IPAddress(192, 168, 4, 1);
153  gateway = network::IPAddress(192, 168, 4, 1);
154  subnet = network::IPAddress(255, 255, 255, 0);
155  dns = network::IPAddress(192, 168, 4, 1);
156  }
157  WiFi.config(ip_address, dns, gateway, subnet);
158  return true;
159 }
160 
161 bool WiFiComponent::wifi_start_ap_(const WiFiAP &ap) {
162  if (!this->wifi_mode_({}, true))
163  return false;
164  if (!this->wifi_ap_ip_config_(ap.get_manual_ip())) {
165  ESP_LOGV(TAG, "wifi_ap_ip_config_ failed!");
166  return false;
167  }
168 
169  WiFi.beginAP(ap.get_ssid().c_str(), ap.get_password().c_str(), ap.get_channel().value_or(1));
170 
171  return true;
172 }
173 
174 network::IPAddress WiFiComponent::wifi_soft_ap_ip() { return {(const ip_addr_t *) WiFi.localIP()}; }
175 #endif // USE_WIFI_AP
176 
178  int err = cyw43_wifi_leave(&cyw43_state, CYW43_ITF_STA);
179  return err == 0;
180 }
181 
183  bssid_t bssid{};
184  uint8_t raw_bssid[6];
185  WiFi.BSSID(raw_bssid);
186  for (size_t i = 0; i < bssid.size(); i++)
187  bssid[i] = raw_bssid[i];
188  return bssid;
189 }
190 std::string WiFiComponent::wifi_ssid() { return WiFi.SSID().c_str(); }
191 int8_t WiFiComponent::wifi_rssi() { return WiFi.RSSI(); }
192 int32_t WiFiComponent::wifi_channel_() { return WiFi.channel(); }
193 
195  network::IPAddresses addresses;
196  uint8_t index = 0;
197  for (auto addr : addrList) {
198  addresses[index++] = addr.ipFromNetifNum();
199  }
200  return addresses;
201 }
202 network::IPAddress WiFiComponent::wifi_subnet_mask_() { return {(const ip_addr_t *) WiFi.subnetMask()}; }
203 network::IPAddress WiFiComponent::wifi_gateway_ip_() { return {(const ip_addr_t *) WiFi.gatewayIP()}; }
205  const ip_addr_t *dns_ip = dns_getserver(num);
206  return network::IPAddress(dns_ip);
207 }
208 
210  if (this->state_ == WIFI_COMPONENT_STATE_STA_SCANNING && !cyw43_wifi_scan_active(&cyw43_state)) {
211  this->scan_done_ = true;
212  ESP_LOGV(TAG, "Scan done!");
213  }
214 }
215 
217 
218 } // namespace wifi
219 } // namespace esphome
220 
221 #endif
222 #endif
std::array< uint8_t, 6 > bssid_t
const std::string & get_password() const
WiFiPowerSaveMode power_save_
network::IPAddress wifi_dns_ip_(int num)
bool wifi_mode_(optional< bool > sta, optional< bool > ap)
bool wifi_apply_output_power_(float output_power)
bool wifi_sta_ip_config_(optional< ManualIP > manual_ip)
bool has_value() const
Definition: optional.h:87
std::vector< WiFiScanResult > scan_result_
WiFi is in STA-only mode and currently scanning for APs.
const char *const TAG
Definition: spi.cpp:8
const optional< ManualIP > & get_manual_ip() const
const optional< uint8_t > & get_channel() const
BedjetMode mode
BedJet operating mode.
Definition: bedjet_codec.h:181
Application App
Global storage of Application pointer - only one Application can exist.
bool wifi_ap_ip_config_(optional< ManualIP > manual_ip)
WiFiComponent * global_wifi_component
const std::string & get_name() const
Get the name of this Application set by pre_setup().
Definition: application.h:202
std::array< IPAddress, 5 > IPAddresses
Definition: ip_address.h:141
uint8_t status
Definition: bl0942.h:74
const char * get_auth_mode_str(uint8_t mode)
in_addr ip_addr_t
Definition: ip_address.h:22
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
const std::string & get_ssid() const
const char * get_disconnect_reason_str(uint8_t reason)
static int s_wifi_scan_result(void *env, const cyw43_ev_scan_result_t *result)
value_type value_or(U const &v) const
Definition: optional.h:93
void wifi_scan_result(void *env, const cyw43_ev_scan_result_t *result)