1
0
mirror of https://github.com/esp8266/Arduino.git synced 2025-06-13 13:01:55 +03:00

Generic netif status callback and mDNS (#8705)

* sprinkle IPAddress(...).isSet() across our loops to avoid polling on a
  stopped interface. status callback and netif_is_up **does not
  guarantee and we could use the interface**!
* register *one* status callback per instance, e.g. when begin() is called
  multiple times (also notice a subtle issue with schedule function when
  instance is delete'ed)
* consistent LwipIntf callback signature. no need for rvalue, just pass
  stdfunc as-is and let the compiler figure it out
This commit is contained in:
Max Prokhorov
2022-11-01 03:01:40 +03:00
committed by GitHub
parent 78444a5037
commit 04494f0729
6 changed files with 71 additions and 41 deletions

View File

@ -34,8 +34,6 @@ class LwipIntf
public:
using CBType = std::function<void(netif*)>;
static bool stateUpCB(LwipIntf::CBType&& cb);
// reorder WiFi.config() parameters for a esp8266/official Arduino dual-compatibility API
// args | esp order arduino order
// ---- + --------- -------------
@ -67,8 +65,11 @@ public:
// ESP32 API compatibility
const char* getHostname();
protected:
static bool stateChangeSysCB(LwipIntf::CBType&& cb);
// whenever netif status callback is called
static bool statusChangeCB(LwipIntf::CBType);
static bool stateUpCB(LwipIntf::CBType);
static bool stateDownCB(LwipIntf::CBType);
};
#endif // _LWIPINTF_H

View File

@ -25,44 +25,54 @@
#include <Schedule.h>
#include <debug.h>
#define NETIF_STATUS_CB_SIZE 3
static constexpr size_t LwipIntfCallbacks = 3;
static int netifStatusChangeListLength = 0;
LwipIntf::CBType netifStatusChangeList[NETIF_STATUS_CB_SIZE];
static LwipIntf::CBType callbacks[LwipIntfCallbacks];
static size_t size = 0;
// override empty weak function from glue-lwip
extern "C" void netif_status_changed(struct netif* netif)
{
// override the default empty weak function
for (int i = 0; i < netifStatusChangeListLength; i++)
for (size_t index = 0; index < size; ++index)
{
netifStatusChangeList[i](netif);
callbacks[index](netif);
}
}
bool LwipIntf::stateChangeSysCB(LwipIntf::CBType&& cb)
bool LwipIntf::statusChangeCB(LwipIntf::CBType cb)
{
if (netifStatusChangeListLength >= NETIF_STATUS_CB_SIZE)
if (size < LwipIntfCallbacks)
{
callbacks[size++] = std::move(cb);
return true;
}
#if defined(DEBUG_ESP_CORE)
DEBUGV("NETIF_STATUS_CB_SIZE is too low\n");
DEBUGV("LwipIntf::CB %zu/%zu, cannot add more!\n", size, size);
#endif
return false;
}
netifStatusChangeList[netifStatusChangeListLength++] = cb;
return true;
return false;
}
bool LwipIntf::stateUpCB(LwipIntf::CBType&& cb)
bool LwipIntf::stateUpCB(LwipIntf::CBType cb)
{
return stateChangeSysCB(
[cb](netif* nif)
return statusChangeCB(
[cb](netif* interface)
{
if (netif_is_up(nif))
schedule_function(
[cb, nif]()
{
cb(nif);
});
if (netif_is_up(interface))
{
cb(interface);
}
});
}
bool LwipIntf::stateDownCB(LwipIntf::CBType cb)
{
return statusChangeCB(
[cb](netif* interface)
{
if (!netif_is_up(interface))
{
cb(interface);
}
});
}