1
0
mirror of https://github.com/esp8266/Arduino.git synced 2025-04-19 23:22:16 +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
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 71 additions and 41 deletions

View File

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

View File

@ -25,44 +25,54 @@
#include <Schedule.h> #include <Schedule.h>
#include <debug.h> #include <debug.h>
#define NETIF_STATUS_CB_SIZE 3 static constexpr size_t LwipIntfCallbacks = 3;
static int netifStatusChangeListLength = 0; static LwipIntf::CBType callbacks[LwipIntfCallbacks];
LwipIntf::CBType netifStatusChangeList[NETIF_STATUS_CB_SIZE]; static size_t size = 0;
// override empty weak function from glue-lwip
extern "C" void netif_status_changed(struct netif* netif) extern "C" void netif_status_changed(struct netif* netif)
{ {
// override the default empty weak function for (size_t index = 0; index < size; ++index)
for (int i = 0; i < netifStatusChangeListLength; i++)
{ {
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) #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 #endif
return false; return false;
} }
netifStatusChangeList[netifStatusChangeListLength++] = cb; bool LwipIntf::stateUpCB(LwipIntf::CBType cb)
return true; {
return statusChangeCB(
[cb](netif* interface)
{
if (netif_is_up(interface))
{
cb(interface);
}
});
} }
bool LwipIntf::stateUpCB(LwipIntf::CBType&& cb) bool LwipIntf::stateDownCB(LwipIntf::CBType cb)
{ {
return stateChangeSysCB( return statusChangeCB(
[cb](netif* nif) [cb](netif* interface)
{ {
if (netif_is_up(nif)) if (!netif_is_up(interface))
schedule_function(
[cb, nif]()
{ {
cb(nif); cb(interface);
}); }
}); });
} }

View File

@ -27,7 +27,7 @@
#include "ESP8266mDNS.h" #include "ESP8266mDNS.h"
#include "LEAmDNS_Priv.h" #include "LEAmDNS_Priv.h"
#include <LwipIntf.h> // LwipIntf::stateUpCB() #include <LwipIntf.h>
#include <lwip/igmp.h> #include <lwip/igmp.h>
#include <lwip/prot/dns.h> #include <lwip/prot/dns.h>
@ -54,7 +54,7 @@ namespace MDNSImplementation
*/ */
MDNSResponder::MDNSResponder(void) : MDNSResponder::MDNSResponder(void) :
m_pServices(0), m_pUDPContext(0), m_pcHostname(0), m_pServiceQueries(0), m_pServices(0), m_pUDPContext(0), m_pcHostname(0), m_pServiceQueries(0),
m_fnServiceTxtCallback(0) m_fnServiceTxtCallback(0), m_bLwipCb(false), m_bRestarting(false)
{ {
} }
@ -89,17 +89,34 @@ namespace MDNSImplementation
bResult = _restart(); bResult = _restart();
} }
LwipIntf::stateUpCB( if (bResult && !m_bLwipCb)
[this](netif* intf)
{ {
if (IPAddress(intf->ip_addr).isSet()) bool bCallback = LwipIntf::statusChangeCB(
[this](netif*)
{ {
DEBUG_EX_INFO(DEBUG_OUTPUT.printf_P( if (m_bRestarting)
PSTR("[MDNSResponder] new Interface '%c%c' is UP! restarting\n"), {
intf->name[0], intf->name[1])); return;
_restart();
} }
m_bRestarting = true;
schedule_function(
[this]()
{
DEBUG_EX_INFO(
DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] begin: restarting "
"after interface status changed\n")););
_restart();
m_bRestarting = false;
}); });
});
DEBUG_EX_ERR(if (!bCallback) {
DEBUG_OUTPUT.printf_P(
PSTR("[MDNSResponder] begin: FAILED LwipIntf::statusChangeCB!\n"));
});
m_bLwipCb = bCallback;
}
DEBUG_EX_ERR(if (!bResult) { DEBUG_EX_ERR(if (!bResult) {
DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] begin: FAILED for '%s'!\n"), DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] begin: FAILED for '%s'!\n"),
(p_pcHostname ?: "-")); (p_pcHostname ?: "-"));
@ -1281,7 +1298,7 @@ namespace MDNSImplementation
// Join multicast group(s) // Join multicast group(s)
for (netif* pNetIf = netif_list; pNetIf; pNetIf = pNetIf->next) for (netif* pNetIf = netif_list; pNetIf; pNetIf = pNetIf->next)
{ {
if (netif_is_up(pNetIf)) if (netif_is_up(pNetIf) && IPAddress(pNetIf->ip_addr).isSet())
{ {
#ifdef MDNS_IP4_SUPPORT #ifdef MDNS_IP4_SUPPORT
ip_addr_t multicast_addr_V4 = DNS_MQUERY_IPV4_GROUP_INIT; ip_addr_t multicast_addr_V4 = DNS_MQUERY_IPV4_GROUP_INIT;
@ -1337,7 +1354,7 @@ namespace MDNSImplementation
for (netif* pNetIf = netif_list; pNetIf; pNetIf = pNetIf->next) for (netif* pNetIf = netif_list; pNetIf; pNetIf = pNetIf->next)
{ {
if (netif_is_up(pNetIf)) if (netif_is_up(pNetIf) && IPAddress(pNetIf->ip_addr).isSet())
{ {
bResult = true; bResult = true;

View File

@ -1174,6 +1174,8 @@ namespace MDNSImplementation
stcMDNSServiceQuery* m_pServiceQueries; stcMDNSServiceQuery* m_pServiceQueries;
MDNSDynamicServiceTxtCallbackFunc m_fnServiceTxtCallback; MDNSDynamicServiceTxtCallbackFunc m_fnServiceTxtCallback;
stcProbeInformation m_HostProbeInformation; stcProbeInformation m_HostProbeInformation;
bool m_bLwipCb;
bool m_bRestarting;
/** CONTROL **/ /** CONTROL **/
/* MAINTENANCE */ /* MAINTENANCE */

View File

@ -2228,7 +2228,7 @@ namespace MDNSImplementation
stcMDNS_RRDomain reverseIP4Domain; stcMDNS_RRDomain reverseIP4Domain;
for (netif* pNetIf = netif_list; pNetIf; pNetIf = pNetIf->next) for (netif* pNetIf = netif_list; pNetIf; pNetIf = pNetIf->next)
{ {
if (netif_is_up(pNetIf)) if (netif_is_up(pNetIf) && IPAddress(pNetIf->ip_addr).isSet())
{ {
if ((_buildDomainForReverseIP4(pNetIf->ip_addr, reverseIP4Domain)) if ((_buildDomainForReverseIP4(pNetIf->ip_addr, reverseIP4Domain))
&& (p_RRHeader.m_Domain == reverseIP4Domain)) && (p_RRHeader.m_Domain == reverseIP4Domain))

View File

@ -122,7 +122,7 @@ namespace MDNSImplementation
for (netif* pNetIf = netif_list; pNetIf; pNetIf = pNetIf->next) for (netif* pNetIf = netif_list; pNetIf; pNetIf = pNetIf->next)
{ {
if (netif_is_up(pNetIf)) if (netif_is_up(pNetIf) && IPAddress(pNetIf->ip_addr).isSet())
{ {
IPAddress fromIPAddress; IPAddress fromIPAddress;
// fromIPAddress = _getResponseMulticastInterface(); // fromIPAddress = _getResponseMulticastInterface();