ESPHome 2026.12.0-dev
Loading...
Searching...
No Matches
network_component.cpp
Go to the documentation of this file.
1#include "network_component.h"
2
4#if defined(USE_NETWORK) && defined(USE_ESP32)
5#include "esphome/core/log.h"
6#include "esp_err.h"
7#include "esp_netif.h"
8#include "esp_event.h"
9#include "lwip/opt.h"
10
11#ifdef USE_NETWORK_DEFAULT_ROUTE
14#include "esp_netif_net_stack.h"
15#include "lwip/dns.h"
16#include "lwip/netif.h"
17#include "ip_address.h"
18#ifdef USE_ETHERNET
20#endif
21#ifdef USE_WIFI
23#endif
24#endif
25
26namespace esphome::network {
27
28ESPHOME_LOG_TAG(TAG, "network");
29
31 // Initialize ESP-IDF network interfaces and ensure the default event loop exists
32 esp_err_t err;
33 err = esp_netif_init();
34 if (err != ESP_OK) {
35 ESP_LOGE(TAG, "esp_netif_init failed: (%d) %s", err, esp_err_to_name(err));
36 this->mark_failed();
37 return;
38 }
39 err = esp_event_loop_create_default();
40 // ESP_ERR_INVALID_STATE is returned if the default loop already exists,
41 // which is fine since we just want to make sure it exists
42 if (err != ESP_OK && err != ESP_ERR_INVALID_STATE) {
43 ESP_LOGE(TAG, "esp_event_loop_create_default failed: (%d) %s", err, esp_err_to_name(err));
44 this->mark_failed();
45 return;
46 }
47}
48
50 // The effective compile-time lwIP value, so the log reflects tcp_send_buffer
51 // or the high performance bundle when either changed it.
52 ESP_LOGCONFIG(TAG,
53 "Network:\n"
54 " TCP send buffer: %d bytes",
55 TCP_SND_BUF);
56}
57
58#ifdef USE_NETWORK_DEFAULT_ROUTE
59static esp_netif_t *connected_wifi_netif() {
60#ifdef USE_WIFI
61 auto *wifi = wifi::global_wifi_component;
62 if (wifi != nullptr && wifi->is_connected())
63 return wifi->get_esp_netif_sta();
64#endif
65 return nullptr;
66}
67
68static esp_netif_t *connected_ethernet_netif() {
69#ifdef USE_ETHERNET
71 if (eth != nullptr && eth->is_connected())
72 return eth->get_esp_netif();
73#endif
74 return nullptr;
75}
76
77// True when lwIP's global DNS table holds the netif's own servers. Call with the lwIP core lock held.
78static bool dns_is_netifs(esp_netif_t *netif) {
79 for (auto type : {ESP_NETIF_DNS_MAIN, ESP_NETIF_DNS_BACKUP}) {
80 esp_netif_dns_info_t own{};
81 if (esp_netif_get_dns_info(netif, type, &own) != ESP_OK)
82 return true;
83 if (IPAddress(&own.ip) != IPAddress(dns_getserver(type)))
84 return false;
85 }
86 return true;
87}
88
90 // Pin the default route to the first connected interface in the user's priority
91 // order; ESP-IDF's own route_prio selection would always favor WiFi.
92 // USE_NETWORK_PRIMARY_INTERFACE_WIFI is emitted for a wifi-first priority list;
93 // it selects the reported address in util.cpp and doubles as the route-order
94 // pivot here — the two uses must stay in sync.
95 esp_netif_t *wifi = connected_wifi_netif();
96 esp_netif_t *ethernet = connected_ethernet_netif();
97#ifdef USE_NETWORK_PRIMARY_INTERFACE_WIFI
98 esp_netif_t *best = wifi != nullptr ? wifi : ethernet;
99#else
100 esp_netif_t *best = ethernet != nullptr ? ethernet : wifi;
101#endif
103 if (best != nullptr) {
105 }
106 if (active != this->active_interface_) {
107 this->active_interface_ = active;
108#ifdef NETWORK_ACTIVE_INTERFACE_LISTENER_COUNT
109 for (auto *listener : this->active_interface_listeners_)
110 listener->on_active_interface(active);
111#endif
112 }
113 if (best == nullptr) {
114 // Forget the last winner: stopping its netif cleared lwIP's default route and
115 // IDF's manual override suppresses re-election, so reconnect must re-assert it.
116 this->default_netif_ = nullptr;
117 return;
118 }
119 // Set when only lwIP's DNS table needs repairing; logged at debug, not as an interface change.
120 bool dns_only = false;
121 if (best == this->default_netif_) {
122 // Same winner as the last assert. Still re-assert if lwIP's default route is
123 // not the winner's netif: a winner whose netif bounced down and up between two
124 // polls would otherwise stay routeless (stopping a netif nulls lwIP's
125 // netif_default). Checking lwIP directly keeps this independent of IDF's
126 // re-election bookkeeping (esp_netif_get_default_netif() cannot detect it).
127 // Throttled: LwIPLock is the global lwIP core mutex, and this branch runs on
128 // every pass once the route has settled.
131 return;
132 this->last_route_check_ = now;
133 bool route_is_ours;
134 bool dns_is_ours;
135 {
137 route_is_ours = static_cast<void *>(netif_default) == esp_netif_get_netif_impl(best);
138 // DHCP starting on another netif empties lwIP's DNS table, and the manual default
139 // stops ESP-IDF from refilling it (espressif/esp-idf#19082); re-asserting copies it back.
140 dns_is_ours = route_is_ours && dns_is_netifs(best);
141 }
142 if (dns_is_ours)
143 return;
144 dns_only = route_is_ours;
145 }
146 esp_err_t err = esp_netif_set_default_netif(best);
147 if (err != ESP_OK) {
148 ESP_LOGW(TAG, "Failed to set default interface: (%d) %s", err, esp_err_to_name(err));
149 // Cache the intent anyway: subsequent passes take the same-winner branch
150 // above, so retries are throttled to ROUTE_CHECK_INTERVAL_MS and the lwIP
151 // verification keeps re-attempting until the route is actually ours.
152 this->default_netif_ = best;
154 return;
155 }
156 this->default_netif_ = best;
157 if (dns_only) {
158 ESP_LOGD(TAG, "DNS servers restored from %s", esp_netif_get_desc(best));
159 } else {
160 ESP_LOGI(TAG, "Default interface: %s", esp_netif_get_desc(best));
161 }
162}
163#endif // USE_NETWORK_DEFAULT_ROUTE
164
165} // namespace esphome::network
166#endif
uint32_t IRAM_ATTR HOT get_loop_component_start_time() const
Get the cached time in milliseconds from when the current component started its loop execution.
void mark_failed()
Mark this component as failed.
Helper class to lock the lwIP TCPIP core when making lwIP API calls from non-TCPIP threads.
Definition helpers.h:2548
esp_netif_t * get_esp_netif()
esp_netif handle, used by network for default-route arbitration.
StaticVector< ActiveInterfaceListener *, NETWORK_ACTIVE_INTERFACE_LISTENER_COUNT > active_interface_listeners_
static constexpr uint32_t ROUTE_CHECK_INTERVAL_MS
esp_netif_t * get_esp_netif_sta()
esp_netif handle of the station interface, used by network for default-route arbitration.
uint8_t type
EthernetComponent * global_eth_component
ESPHOME_LOG_TAG(TAG, "network")
NetworkInterface
The interface holding the default route.
WiFiComponent * global_wifi_component
Application App
Global storage of Application pointer - only one Application can exist.
struct esp_netif_obj esp_netif_t
static void uint32_t
SemaphoreHandle_t lock