mirror of
https://github.com/esphome/esphome.git
synced 2026-11-18 02:55:59 +00:00
Compare commits
4
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
05ffece389 | ||
|
|
5d18732dee | ||
|
|
bb736033c5 | ||
|
|
5a0094aece |
No files matched your search
@@ -358,6 +358,10 @@ class EthernetComponent final : public Component {
|
||||
// to be called at runtime after enable_on_boot:false without re-allocating, and
|
||||
// ensures setup() skips the heavy init when enable_on_boot_ is false.
|
||||
bool ethernet_initialized_{false};
|
||||
// PHY link state, written by the event task; the connect timeout retries only with a link.
|
||||
std::atomic<bool> link_up_{false};
|
||||
// Last link state loop() saw; a rising edge restarts the connect timeout.
|
||||
bool link_was_up_{false};
|
||||
#endif
|
||||
#if LWIP_IPV6
|
||||
uint8_t ipv6_count_{0};
|
||||
|
||||
@@ -137,6 +137,13 @@ void EthernetComponent::loop() {
|
||||
}
|
||||
#endif
|
||||
|
||||
const bool link_up = this->link_up_.load(std::memory_order_relaxed);
|
||||
if (link_up && !this->link_was_up_) {
|
||||
// Give DHCP a full window after the link comes up before the timeout below restarts it
|
||||
this->connect_begin_ = now;
|
||||
}
|
||||
this->link_was_up_ = link_up;
|
||||
|
||||
switch (this->state_) {
|
||||
case EthernetComponentState::STOPPED:
|
||||
if (this->started_) {
|
||||
@@ -160,8 +167,15 @@ void EthernetComponent::loop() {
|
||||
this->connect_trigger_.trigger();
|
||||
#endif
|
||||
} else if (now - this->connect_begin_ > 15000) {
|
||||
ESP_LOGW(TAG, "Connecting failed; reconnecting");
|
||||
this->start_connect_();
|
||||
if (link_up) {
|
||||
ESP_LOGW(TAG, "Connecting failed; reconnecting");
|
||||
this->start_connect_();
|
||||
} else {
|
||||
// Restarting DHCP without a link only empties lwIP's DNS servers (espressif/esp-idf#19082);
|
||||
// the DHCP client already running resumes when the link comes up.
|
||||
ESP_LOGD(TAG, "Connecting failed: no link");
|
||||
this->connect_begin_ = now;
|
||||
}
|
||||
}
|
||||
break;
|
||||
case EthernetComponentState::CONNECTED:
|
||||
@@ -765,10 +779,12 @@ void EthernetComponent::eth_event_handler(void *arg, esp_event_base_t event_base
|
||||
event_name = "ETH stopped";
|
||||
global_eth_component->started_ = false;
|
||||
global_eth_component->connected_ = false;
|
||||
global_eth_component->link_up_.store(false, std::memory_order_relaxed);
|
||||
global_eth_component->enable_loop_soon_any_context(); // Enable loop when connection state changes
|
||||
break;
|
||||
case ETHERNET_EVENT_CONNECTED:
|
||||
event_name = "ETH connected";
|
||||
global_eth_component->link_up_.store(true, std::memory_order_relaxed);
|
||||
// For static IP configurations, GOT_IP event may not fire, so notify IP listeners here
|
||||
#if defined(USE_ETHERNET_IP_STATE_LISTENERS) && defined(USE_ETHERNET_MANUAL_IP)
|
||||
global_eth_component->ip_state_pending_.store(1, std::memory_order_release);
|
||||
@@ -785,6 +801,7 @@ void EthernetComponent::eth_event_handler(void *arg, esp_event_base_t event_base
|
||||
case ETHERNET_EVENT_DISCONNECTED:
|
||||
event_name = "ETH disconnected";
|
||||
global_eth_component->connected_ = false;
|
||||
global_eth_component->link_up_.store(false, std::memory_order_relaxed);
|
||||
global_eth_component->enable_loop_soon_any_context(); // Enable loop when connection state changes
|
||||
break;
|
||||
default:
|
||||
|
||||
@@ -12,7 +12,9 @@
|
||||
#include "esphome/core/application.h"
|
||||
#include "esphome/core/helpers.h"
|
||||
#include "esp_netif_net_stack.h"
|
||||
#include "lwip/dns.h"
|
||||
#include "lwip/netif.h"
|
||||
#include "ip_address.h"
|
||||
#ifdef USE_ETHERNET
|
||||
#include "esphome/components/ethernet/ethernet_component.h"
|
||||
#endif
|
||||
@@ -72,6 +74,18 @@ static esp_netif_t *connected_ethernet_netif() {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
// True when lwIP's global DNS table holds the netif's own servers. Call with the lwIP core lock held.
|
||||
static bool dns_is_netifs(esp_netif_t *netif) {
|
||||
for (auto type : {ESP_NETIF_DNS_MAIN, ESP_NETIF_DNS_BACKUP}) {
|
||||
esp_netif_dns_info_t own{};
|
||||
if (esp_netif_get_dns_info(netif, type, &own) != ESP_OK)
|
||||
return true;
|
||||
if (IPAddress(&own.ip) != IPAddress(dns_getserver(type)))
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
void NetworkComponent::loop() {
|
||||
// Pin the default route to the first connected interface in the user's priority
|
||||
// order; ESP-IDF's own route_prio selection would always favor WiFi.
|
||||
@@ -102,6 +116,8 @@ void NetworkComponent::loop() {
|
||||
this->default_netif_ = nullptr;
|
||||
return;
|
||||
}
|
||||
// Set when only lwIP's DNS table needs repairing; logged at debug, not as an interface change.
|
||||
bool dns_only = false;
|
||||
if (best == this->default_netif_) {
|
||||
// Same winner as the last assert. Still re-assert if lwIP's default route is
|
||||
// not the winner's netif: a winner whose netif bounced down and up between two
|
||||
@@ -115,12 +131,17 @@ void NetworkComponent::loop() {
|
||||
return;
|
||||
this->last_route_check_ = now;
|
||||
bool route_is_ours;
|
||||
bool dns_is_ours;
|
||||
{
|
||||
LwIPLock lock;
|
||||
route_is_ours = static_cast<void *>(netif_default) == esp_netif_get_netif_impl(best);
|
||||
// DHCP starting on another netif empties lwIP's DNS table, and the manual default
|
||||
// stops ESP-IDF from refilling it (espressif/esp-idf#19082); re-asserting copies it back.
|
||||
dns_is_ours = route_is_ours && dns_is_netifs(best);
|
||||
}
|
||||
if (route_is_ours)
|
||||
if (dns_is_ours)
|
||||
return;
|
||||
dns_only = route_is_ours;
|
||||
}
|
||||
esp_err_t err = esp_netif_set_default_netif(best);
|
||||
if (err != ESP_OK) {
|
||||
@@ -133,7 +154,11 @@ void NetworkComponent::loop() {
|
||||
return;
|
||||
}
|
||||
this->default_netif_ = best;
|
||||
ESP_LOGI(TAG, "Default interface: %s", esp_netif_get_desc(best));
|
||||
if (dns_only) {
|
||||
ESP_LOGD(TAG, "DNS servers restored from %s", esp_netif_get_desc(best));
|
||||
} else {
|
||||
ESP_LOGI(TAG, "Default interface: %s", esp_netif_get_desc(best));
|
||||
}
|
||||
}
|
||||
#endif // USE_NETWORK_DEFAULT_ROUTE
|
||||
|
||||
|
||||
Reference in new issue
Block a user