Skip to content

WiFi: ARP gratuitous API for wifi station mode #6889

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 20 commits into from
Apr 9, 2020
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions libraries/ESP8266WiFi/keywords.txt
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,9 @@ psk KEYWORD2
BSSID KEYWORD2
BSSIDstr KEYWORD2
RSSI KEYWORD2
stationKeepAliveSetIntervalMs KEYWORD2
stationKeepAliveStop KEYWORD2
stationKeepAliveEnabled KEYWORD2
enableInsecureWEP KEYWORD2
getListenInterval KEYWORD2
isSleepLevelMax KEYWORD2
Expand Down
54 changes: 53 additions & 1 deletion libraries/ESP8266WiFi/src/ESP8266WiFiSTA.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#include "mem.h"
#include "user_interface.h"
#include "smartconfig.h"
#include "Schedule.h"

extern "C" {
#include "lwip/err.h"
Expand All @@ -43,6 +44,12 @@ extern "C" {
#if LWIP_IPV6
#include "lwip/netif.h" // struct netif
#endif
#if LWIP_VERSION_MAJOR == 1
#include "netif/wlan_lwip_if.h" // eagle_lwip_getif()
#include "netif/etharp.h" // gratuitous arp
#else
#include "lwip/etharp.h" // gratuitous arp
#endif
}

#include "debug.h"
Expand Down Expand Up @@ -89,7 +96,7 @@ static bool sta_config_equal(const station_config& lhs, const station_config& rh
return false;
}
}

if(lhs.threshold.rssi != rhs.threshold.rssi) {
return false;
}
Expand Down Expand Up @@ -704,7 +711,52 @@ int32_t ESP8266WiFiSTAClass::RSSI(void) {
return wifi_station_get_rssi();
}

void ESP8266WiFiSTAClass::stationKeepAliveNow ()
{
if (_keepStationAliveUs > 0)
{
for (netif* interface = netif_list; interface != nullptr; interface = interface->next)
if (
(interface->flags & NETIF_FLAG_LINK_UP)
&& (interface->flags & NETIF_FLAG_UP)
#if LWIP_VERSION_MAJOR == 1
&& interface == eagle_lwip_getif(STATION_IF) /* lwip1 does not set num properly */
&& (!ip_addr_isany(&interface->ip_addr))
#else
&& interface->num == STATION_IF
&& (!ip4_addr_isany_val(*netif_ip4_addr(interface)))
#endif
)
{
etharp_gratuitous(interface);
break;
}

// re/schedule with a possibly updated interval
schedule_recurrent_function_us([&]()
{
stationKeepAliveNow();
return false;
}, _keepStationAliveUs);
}
else
// mark disabled (0) if user cancelled it (<0)
_keepStationAliveUs = 0;
}

void ESP8266WiFiSTAClass::stationKeepAliveSetIntervalMs (int ms)
{
bool wasEnabled = (_keepStationAliveUs > 0);
int us = ms * 1000;
if (wasEnabled)
_keepStationAliveUs = ms <= 0? /*disable*/-1: /*update*/us;
else if (ms > 0)
{
_keepStationAliveUs = us;
// start
stationKeepAliveNow();
}
}

// -----------------------------------------------------------------------------------------------------------------------
// -------------------------------------------------- STA remote configure -----------------------------------------------
Expand Down
14 changes: 14 additions & 0 deletions libraries/ESP8266WiFi/src/ESP8266WiFiSTA.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,27 @@ class ESP8266WiFiSTAClass {

int32_t RSSI();

// enable or update automatic sending of Gratuitous ARP packets
// based on a time interval in milliseconds
void stationKeepAliveSetIntervalMs (int ms = 1000);

// request for stopping arp gratuitous packets
void stationKeepAliveStop () { stationKeepAliveSetIntervalMs(0); }

// allows to check whether the gratuitous ARP service is still running
bool stationKeepAliveEnabled () const { return _keepStationAliveUs != 0; }


static void enableInsecureWEP (bool enable = true) { _useInsecureWEP = enable; }

protected:

int _keepStationAliveUs = 0;
static bool _useStaticIp;
static bool _useInsecureWEP;

void stationKeepAliveNow ();

// ----------------------------------------------------------------------------------------------
// ------------------------------------ STA remote configure -----------------------------------
// ----------------------------------------------------------------------------------------------
Expand Down