1
0
mirror of https://github.com/esphome/esphome.git synced 2025-10-01 09:32:21 +01:00

Port wifi_component_esp32_arduino from tcpip_adapter to esp_netif (#6476)

Co-authored-by: Jesse Hills <3060199+jesserockz@users.noreply.github.com>
This commit is contained in:
Faidon Liambotis
2024-05-16 05:49:40 +03:00
committed by GitHub
parent 7c243dafb3
commit 0bb2773c64
2 changed files with 219 additions and 170 deletions

View File

@@ -2,18 +2,18 @@
#ifdef USE_ESP_IDF
#include <freertos/FreeRTOS.h>
#include <freertos/task.h>
#include <freertos/event_groups.h>
#include <esp_event.h>
#include <esp_netif.h>
#include <esp_system.h>
#include <esp_wifi.h>
#include <esp_wifi_types.h>
#include <esp_event.h>
#include <esp_netif.h>
#include <freertos/FreeRTOS.h>
#include <freertos/event_groups.h>
#include <freertos/task.h>
#include <algorithm>
#include <cinttypes>
#include <utility>
#include <algorithm>
#ifdef USE_WIFI_WPA2_EAP
#include <esp_wpa2.h>
#endif
@@ -22,13 +22,14 @@
#include "dhcpserver/dhcpserver.h"
#endif // USE_WIFI_AP
#include "lwip/err.h"
#include "lwip/apps/sntp.h"
#include "lwip/dns.h"
#include "lwip/err.h"
#include "esphome/core/application.h"
#include "esphome/core/hal.h"
#include "esphome/core/helpers.h"
#include "esphome/core/log.h"
#include "esphome/core/hal.h"
#include "esphome/core/application.h"
#include "esphome/core/util.h"
namespace esphome {
@@ -196,8 +197,8 @@ bool WiFiComponent::wifi_mode_(optional<bool> sta, optional<bool> ap) {
bool current_sta = current_mode == WIFI_MODE_STA || current_mode == WIFI_MODE_APSTA;
bool current_ap = current_mode == WIFI_MODE_AP || current_mode == WIFI_MODE_APSTA;
bool set_sta = sta.has_value() ? *sta : current_sta;
bool set_ap = ap.has_value() ? *ap : current_ap;
bool set_sta = sta.value_or(current_sta);
bool set_ap = ap.value_or(current_ap);
wifi_mode_t set_mode;
if (set_sta && set_ap) {
@@ -438,6 +439,11 @@ bool WiFiComponent::wifi_sta_ip_config_(optional<ManualIP> manual_ip) {
}
if (!manual_ip.has_value()) {
// lwIP starts the SNTP client if it gets an SNTP server from DHCP. We don't need the time, and more importantly,
// the built-in SNTP client has a memory leak in certain situations. Disable this feature.
// https://github.com/esphome/issues/issues/2299
sntp_servermode_dhcp(false);
// No manual IP is set; use DHCP client
if (dhcp_status != ESP_NETIF_DHCP_STARTED) {
err = esp_netif_dhcpc_start(s_sta_netif);
@@ -455,13 +461,12 @@ bool WiFiComponent::wifi_sta_ip_config_(optional<ManualIP> manual_ip) {
info.netmask = manual_ip->subnet;
err = esp_netif_dhcpc_stop(s_sta_netif);
if (err != ESP_OK && err != ESP_ERR_ESP_NETIF_DHCP_ALREADY_STOPPED) {
ESP_LOGV(TAG, "esp_netif_dhcpc_stop failed: %s", esp_err_to_name(err));
return false;
ESP_LOGV(TAG, "Stopping DHCP client failed! %s", esp_err_to_name(err));
}
err = esp_netif_set_ip_info(s_sta_netif, &info);
if (err != ESP_OK) {
ESP_LOGV(TAG, "esp_netif_set_ip_info failed: %s", esp_err_to_name(err));
return false;
ESP_LOGV(TAG, "Setting manual IP info failed! %s", esp_err_to_name(err));
}
esp_netif_dns_info_t dns;
@@ -795,7 +800,7 @@ bool WiFiComponent::wifi_scan_start_(bool passive) {
return false;
}
scan_done_ = false;
this->scan_done_ = false;
return true;
}
@@ -818,13 +823,13 @@ bool WiFiComponent::wifi_ap_ip_config_(optional<ManualIP> manual_ip) {
info.netmask = network::IPAddress(255, 255, 255, 0);
}
err = esp_netif_dhcpc_stop(s_sta_netif);
err = esp_netif_dhcpc_stop(s_ap_netif);
if (err != ESP_OK && err != ESP_ERR_ESP_NETIF_DHCP_ALREADY_STOPPED) {
ESP_LOGV(TAG, "esp_netif_dhcpc_stop failed: %s", esp_err_to_name(err));
return false;
}
err = esp_netif_set_ip_info(s_sta_netif, &info);
err = esp_netif_set_ip_info(s_ap_netif, &info);
if (err != ESP_OK) {
ESP_LOGV(TAG, "esp_netif_set_ip_info failed! %d", err);
return false;
@@ -839,14 +844,14 @@ bool WiFiComponent::wifi_ap_ip_config_(optional<ManualIP> manual_ip) {
start_address += 100;
lease.end_ip = start_address;
ESP_LOGV(TAG, "DHCP server IP lease end: %s", start_address.str().c_str());
err = esp_netif_dhcps_option(s_sta_netif, ESP_NETIF_OP_SET, ESP_NETIF_REQUESTED_IP_ADDRESS, &lease, sizeof(lease));
err = esp_netif_dhcps_option(s_ap_netif, ESP_NETIF_OP_SET, ESP_NETIF_REQUESTED_IP_ADDRESS, &lease, sizeof(lease));
if (err != ESP_OK) {
ESP_LOGV(TAG, "esp_netif_dhcps_option failed! %d", err);
return false;
}
err = esp_netif_dhcps_start(s_sta_netif);
err = esp_netif_dhcps_start(s_ap_netif);
if (err != ESP_OK) {
ESP_LOGV(TAG, "esp_netif_dhcps_start failed! %d", err);
@@ -893,25 +898,26 @@ bool WiFiComponent::wifi_start_ap_(const WiFiAP &ap) {
return true;
}
#endif // USE_WIFI_AP
network::IPAddress WiFiComponent::wifi_soft_ap_ip() {
esp_netif_ip_info_t ip;
esp_netif_get_ip_info(s_sta_netif, &ip);
esp_netif_get_ip_info(s_ap_netif, &ip);
return network::IPAddress(&ip.ip);
}
#endif // USE_WIFI_AP
bool WiFiComponent::wifi_disconnect_() { return esp_wifi_disconnect(); }
bssid_t WiFiComponent::wifi_bssid() {
bssid_t bssid{};
wifi_ap_record_t info;
esp_err_t err = esp_wifi_sta_get_ap_info(&info);
bssid_t res{};
if (err != ESP_OK) {
ESP_LOGW(TAG, "esp_wifi_sta_get_ap_info failed: %s", esp_err_to_name(err));
return res;
return bssid;
}
std::copy(info.bssid, info.bssid + 6, res.begin());
return res;
std::copy(info.bssid, info.bssid + 6, bssid.begin());
return bssid;
}
std::string WiFiComponent::wifi_ssid() {
wifi_ap_record_t info{};
@@ -969,4 +975,4 @@ network::IPAddress WiFiComponent::wifi_dns_ip_(int num) {
} // namespace wifi
} // namespace esphome
#endif
#endif // USE_ESP_IDF