refactoring: arp gratuitous for esp8266

This commit is contained in:
Yurii
2023-12-20 08:12:39 +03:00
parent 4e980b6e5b
commit f4fe8c7366

View File

@@ -4,7 +4,11 @@
#include <UnsignedShortParameter.h> #include <UnsignedShortParameter.h>
#include <CheckboxParameter.h> #include <CheckboxParameter.h>
#include <HeaderParameter.h> #include <HeaderParameter.h>
#include <netif/etharp.h> #ifdef ARDUINO_ARCH_ESP8266
extern "C" {
#include "lwip/etharp.h"
}
#endif
WiFiManager wm; WiFiManager wm;
WiFiManagerParameter* wmHostname; WiFiManagerParameter* wmHostname;
@@ -47,7 +51,7 @@ HeaderParameter* wmExtPumpHeader;
extern EEManager eeSettings; extern EEManager eeSettings;
#if USE_TELNET #if USE_TELNET
extern ESPTelnetStream TelnetStream; extern ESPTelnetStream TelnetStream;
#endif #endif
const char S_WIFI[] PROGMEM = "WIFI"; const char S_WIFI[] PROGMEM = "WIFI";
@@ -164,7 +168,7 @@ protected:
const char* getTaskName() { const char* getTaskName() {
return "WifiManager"; return "WifiManager";
} }
/*int getTaskCore() { /*int getTaskCore() {
return 1; return 1;
}*/ }*/
@@ -174,9 +178,9 @@ protected:
} }
void setup() { void setup() {
#ifdef WOKWI #ifdef WOKWI
WiFi.begin("Wokwi-GUEST", "", 6); WiFi.begin("Wokwi-GUEST", "", 6);
#endif #endif
wm.setDebugOutput(settings.debug, (wm_debuglevel_t) WM_DEBUG_MODE); wm.setDebugOutput(settings.debug, (wm_debuglevel_t) WM_DEBUG_MODE);
wm.setTitle(PROJECT_NAME); wm.setTitle(PROJECT_NAME);
@@ -210,7 +214,6 @@ protected:
task->disable(); task->disable();
} }
} }
//this->delay(10);
}); });
wm.setConfigPortalTimeout(wm.getWiFiIsSaved() ? 180 : 0); wm.setConfigPortalTimeout(wm.getWiFiIsSaved() ? 180 : 0);
wm.setDisableConfigPortal(false); wm.setDisableConfigPortal(false);
@@ -224,6 +227,10 @@ protected:
if (wm.getWebPortalActive()) { if (wm.getWebPortalActive()) {
wm.stopWebPortal(); wm.stopWebPortal();
#ifdef ARDUINO_ARCH_ESP8266
::yield();
#endif
} }
/*wm.setCaptivePortalEnable(true); /*wm.setCaptivePortalEnable(true);
@@ -232,13 +239,16 @@ protected:
wm.startConfigPortal(AP_SSID, AP_PASSWORD); wm.startConfigPortal(AP_SSID, AP_PASSWORD);
}*/ }*/
#if USE_TELNET #if USE_TELNET
TelnetStream.stop(); TelnetStream.stop();
#endif #ifdef ARDUINO_ARCH_ESP8266
::yield();
#endif
#endif
Log.sinfoln(FPSTR(S_WIFI), F("Disconnected")); Log.sinfoln(FPSTR(S_WIFI), F("Disconnected"));
} }
if (WiFi.status() != WL_CONNECTED && !wm.getConfigPortalActive()) { if (WiFi.status() != WL_CONNECTED && !wm.getConfigPortalActive()) {
if (millis() - this->lastReconnecting > 5000) { if (millis() - this->lastReconnecting > 5000) {
Log.sinfoln(FPSTR(S_WIFI), F("Reconnecting...")); Log.sinfoln(FPSTR(S_WIFI), F("Reconnecting..."));
@@ -247,35 +257,44 @@ protected:
this->lastReconnecting = millis(); this->lastReconnecting = millis();
} }
} }
if (!connected && WiFi.status() == WL_CONNECTED) { if (!connected && WiFi.status() == WL_CONNECTED) {
connected = true; connected = true;
wm.setConfigPortalTimeout(180); wm.setConfigPortalTimeout(180);
if (wm.getConfigPortalActive()) { if (wm.getConfigPortalActive()) {
wm.stopConfigPortal(); wm.stopConfigPortal();
#ifdef ARDUINO_ARCH_ESP8266
::yield();
#endif
} }
wm.setCaptivePortalEnable(false); wm.setCaptivePortalEnable(false);
if (!wm.getWebPortalActive()) { if (!wm.getWebPortalActive()) {
wm.startWebPortal(); wm.startWebPortal();
#ifdef ARDUINO_ARCH_ESP8266
::yield();
#endif
} }
#if USE_TELNET #if USE_TELNET
TelnetStream.begin(23, false); TelnetStream.begin(23, false);
#endif #ifdef ARDUINO_ARCH_ESP8266
::yield();
#endif
#endif
Log.sinfoln(FPSTR(S_WIFI), F("Connected. IP: %s, RSSI: %hhd"), WiFi.localIP().toString().c_str(), WiFi.RSSI()); Log.sinfoln(FPSTR(S_WIFI), F("Connected. IP: %s, RSSI: %hhd"), WiFi.localIP().toString().c_str(), WiFi.RSSI());
} }
#if defined(ARDUINO_ARCH_ESP8266) #ifdef ARDUINO_ARCH_ESP8266
if (connected && millis() - lastArpGratuitous > 60000) { if (connected && millis() - lastArpGratuitous > 60000) {
arpGratuitous(); stationKeepAliveNow();
lastArpGratuitous = millis(); lastArpGratuitous = millis();
}
::yield(); ::yield();
#endif }
#endif
wm.process(); wm.process();
} }
@@ -518,11 +537,23 @@ protected:
eeSettings.update(); eeSettings.update();
} }
static void arpGratuitous() { #ifdef ARDUINO_ARCH_ESP8266
struct netif* netif = netif_list; /**
while (netif) { * @brief
etharp_gratuitous(netif); * https://github.com/arendst/Tasmota/blob/e6515883f0ee5451931b6280ff847b117de5a231/tasmota/tasmota_support/support_wifi.ino#L1196
netif = netif->next; */
static void stationKeepAliveNow(void) {
for (netif* interface = netif_list; interface != nullptr; interface = interface->next) {
if (
(interface->flags & NETIF_FLAG_LINK_UP)
&& (interface->flags & NETIF_FLAG_UP)
&& interface->num == STATION_IF
&& (!ip4_addr_isany_val(*netif_ip4_addr(interface)))
) {
etharp_gratuitous(interface);
break;
}
} }
} }
#endif
}; };