[Ethernet] Fix getting DNS from DHCP switch from WiFi to Ethernet

This commit is contained in:
TD-er
2024-04-09 16:53:13 +02:00
parent a08c8e0ecb
commit 78af1dc35c
3 changed files with 36 additions and 9 deletions
+9 -2
View File
@@ -176,18 +176,25 @@ void EthernetEventData_t::markDisconnect() {
}
lastConnectMoment.clear();
processedDisconnect = false;
#if ESP_IDF_VERSION_MAJOR >= 5
WiFi.STA.setDefault();
#endif
}
void EthernetEventData_t::markConnected() {
lastConnectMoment.setNow();
processedConnect = false;
#if ESP_IDF_VERSION_MAJOR >= 5
ETH.setDefault();
#endif
#if FEATURE_USE_IPV6
ETH.enableIPv6(true);
// workaround for the race condition in LWIP, see https://github.com/espressif/arduino-esp32/pull/9016#discussion_r1451774885
/*
// workaround for the race condition in LWIP, see https://github.com/espressif/arduino-esp32/pull/9016#discussion_r1451774885
{
uint32_t i = 5; // try 5 times only
while (esp_netif_create_ip6_linklocal(get_esp_interface_netif(ESP_IF_ETH)) != ESP_OK) {
while (esp_netif_create_ip6_linklocal(ETH.netif()) != ESP_OK) {
delay(1);
if (i-- == 0) {
// addLog(LOG_LEVEL_ERROR, ">>>> HELP");
@@ -17,6 +17,7 @@
# include "../Globals/NetworkState.h"
# include "../Globals/Settings.h"
# include "../Helpers/LongTermTimer.h"
# include "../Helpers/Network.h"
# include "../Helpers/Networking.h"
# include "../Helpers/PeriodicalActions.h"
@@ -164,14 +165,19 @@ void processEthernetGotIP() {
IPAddress dns0 = ETH.dnsIP(0);
IPAddress dns1 = ETH.dnsIP(1);
const LongTermTimer::Duration dhcp_duration = EthEventData.lastConnectMoment.timeDiff(EthEventData.lastGetIPmoment);
#if ESP_IDF_VERSION_MAJOR >= 5
const bool mustRequestDHCP = (!dns0 && !dns1) && !ethUseStaticIP();
#endif
if (!dns0 && !dns1) {
addLog(LOG_LEVEL_ERROR, F("ETH : No DNS server received via DHCP, use cached DNS IP"));
setDNS(0, EthEventData.dns0_cache);
setDNS(1, EthEventData.dns1_cache);
} else {
EthEventData.dns0_cache = dns0;
EthEventData.dns1_cache = dns1;
if (!ethUseStaticIP()) {
if (!dns0 && !dns1) {
addLog(LOG_LEVEL_ERROR, F("ETH : No DNS server received via DHCP, use cached DNS IP"));
if (EthEventData.dns0_cache) setDNS(0, EthEventData.dns0_cache);
if (EthEventData.dns1_cache) setDNS(1, EthEventData.dns1_cache);
} else {
EthEventData.dns0_cache = dns0;
EthEventData.dns1_cache = dns1;
}
}
if (loglevelActiveFor(LOG_LEVEL_INFO))
@@ -244,6 +250,14 @@ void processEthernetGotIP() {
logConnectionStatus();
EthEventData.processedGotIP = true;
#if ESP_IDF_VERSION_MAJOR >= 5
if (mustRequestDHCP /*&& EthEventData.lastConnectMoment.millisPassedSince() < 10000*/) {
// FIXME TD-er: Must add some check other than fixed timeout here to not constantly make DHCP requests.
// Force new DHCP request.
ETH.config();
}
#endif
EthEventData.setEthGotIP();
CheckRunningServices();
}
+6
View File
@@ -46,6 +46,9 @@ void setNetworkMedium(NetworkMedium_t new_medium) {
// ETH.end();
if (new_medium == NetworkMedium_t::WIFI) {
WiFiEventData.clearAll();
#if ESP_IDF_VERSION_MAJOR >= 5
WiFi.STA.setDefault();
#endif
}
#endif
break;
@@ -53,6 +56,9 @@ void setNetworkMedium(NetworkMedium_t new_medium) {
WiFiEventData.timerAPoff.setMillisFromNow(WIFI_AP_OFF_TIMER_DURATION);
WiFiEventData.timerAPstart.clear();
if (new_medium == NetworkMedium_t::Ethernet) {
#if ESP_IDF_VERSION_MAJOR >= 5
ETH.setDefault();
#endif
WifiDisconnect();
}
break;