Skip to content
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

WiFi: ARP gratuitous API for wifi station mode #6889

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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions libraries/ESP8266WiFi/keywords.txt
Expand Up @@ -108,6 +108,9 @@ psk KEYWORD2
BSSID KEYWORD2
BSSIDstr KEYWORD2
RSSI KEYWORD2
stationKeepAliveSetIntervalMs KEYWORD2
stationKeepAliveStop KEYWORD2
stationKeepAliveNow KEYWORD2
enableInsecureWEP KEYWORD2
getListenInterval KEYWORD2
isSleepLevelMax KEYWORD2
Expand Down
63 changes: 62 additions & 1 deletion libraries/ESP8266WiFi/src/ESP8266WiFiSTA.cpp
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,61 @@ int32_t ESP8266WiFiSTAClass::RSSI(void) {
return wifi_station_get_rssi();
}

void ESP8266WiFiSTAClass::stationKeepAliveNow ()
{
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 if->num properly */
&& (!ip_addr_isany(&interface->ip_addr))
#else
&& interface->num == STATION_IF
&& (!ip4_addr_isany_val(*netif_ip4_addr(interface)))
#endif
)
d-a-v marked this conversation as resolved.
Show resolved Hide resolved
{
etharp_gratuitous(interface);
break;
}
}

void ESP8266WiFiSTAClass::stationKeepAliveSetIntervalMs (uint32_t ms)
{
uint32_t us;
if (ms > std::numeric_limits<uint32_t>::max() / 1000)
// for sanity only, 49 days is not really meaningful
us = std::numeric_limits<uint32_t>::max();
else
us = ms * 1000;

// cancel possibly already scheduled future events
_keepStationState++;

if (us)
{
// send one now
stationKeepAliveNow();

// schedule next ones
int checkState = _keepStationState;
schedule_recurrent_function_us([&, checkState]()
{
// this recurring scheduled function will be cancelled
// when this->_keepStationState != checkState
if (checkState != this->_keepStationState)
// cancel this recurring event
return false;

// send gratuitous ARP
this->stationKeepAliveNow();

// keep on with next event
return true;
}, us);
}
}

// -----------------------------------------------------------------------------------------------------------------------
// -------------------------------------------------- STA remote configure -----------------------------------------------
Expand Down
17 changes: 15 additions & 2 deletions libraries/ESP8266WiFi/src/ESP8266WiFiSTA.h
Expand Up @@ -41,8 +41,8 @@ class ESP8266WiFiSTAClass {
wl_status_t begin(const String& ssid, const String& passphrase = emptyString, int32_t channel = 0, const uint8_t* bssid = NULL, bool connect = true);
wl_status_t begin();

//The argument order for ESP is not the same as for Arduino. However, there is compatibility code under the hood
//to detect Arduino arg order, and handle it correctly. Be aware that the Arduino default value handling doesn't
//The argument order for ESP is not the same as for Arduino. However, there is compatibility code under the hood
//to detect Arduino arg order, and handle it correctly. Be aware that the Arduino default value handling doesn't
//work here (see Arduino docs for gway/subnet defaults). In other words: at least 3 args must always be given.
bool config(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dns1 = (uint32_t)0x00000000, IPAddress dns2 = (uint32_t)0x00000000);

Expand Down Expand Up @@ -83,13 +83,26 @@ class ESP8266WiFiSTAClass {

int32_t RSSI();

// disable(0) or enable/update automatic sending of Gratuitous ARP packets
// a gratuitous ARP packet is sent at start, then
// based on a time interval in milliseconds, default 1s
void stationKeepAliveSetIntervalMs (uint32_t ms = 1000);

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

// immediately send one gratuitous ARP from STA
static void stationKeepAliveNow ();

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

protected:

static bool _useStaticIp;
static bool _useInsecureWEP;

int _keepStationState = 0;

// ----------------------------------------------------------------------------------------------
// ------------------------------------ STA remote configure -----------------------------------
// ----------------------------------------------------------------------------------------------
Expand Down
7 changes: 7 additions & 0 deletions tests/host/common/MocklwIP.cpp
Expand Up @@ -26,4 +26,11 @@ const ip_addr_t* sntp_getserver(u8_t)
return IP_ADDR_ANY;
}

err_t etharp_request(struct netif *netif, const ip4_addr_t *ipaddr)
{
(void)netif;
(void)ipaddr;
return ERR_OK;
}

} // extern "C"