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

Track mDNS initialization state on each netif num #308

Closed
wants to merge 1 commit into from
Closed
Changes from all 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
46 changes: 41 additions & 5 deletions cores/common/arduino/libraries/common/mDNS/LwIPmDNS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include "mDNS.h"
#include <vector>
#include <map>

extern "C" {
#include <errno.h>
Expand Down Expand Up @@ -65,6 +66,16 @@ static const char *hostName;
NETIF_DECLARE_EXT_CALLBACK(netif_callback)
#endif

enum StateForNetIf
{
DISABLED = 0, // not yet enabled
ENABLED, // enabled, but no services added
CONFIGURED // enabled and services added
};

static std::map<uint8_t, StateForNetIf> sNetIfState; // netif->num --> state


mDNS::mDNS() {}

mDNS::~mDNS() {
Expand Down Expand Up @@ -171,12 +182,32 @@ static void mdns_netif_ext_status_callback(
netif_nsc_reason_t reason,
const netif_ext_callback_args_t *args
) {
auto stateIter = sNetIfState.find(netif->num);
if (stateIter == sNetIfState.end())
{
stateIter = sNetIfState.insert(std::make_pair(netif->num, StateForNetIf::DISABLED)).first;
}
auto currentState = sNetIfState[netif->num];
if (reason & LWIP_NSC_NETIF_REMOVED) {
LT_DM(MDNS, "Netif removed, stopping mDNS on netif %u", netif->num);
mdns_resp_remove_netif(netif);
if (StateForNetIf::DISABLED != stateIter->second)
{
LT_DM(MDNS, "Netif removed, stopping mDNS on netif %u", netif->num);
mdns_resp_remove_netif(netif);
sNetIfState.erase(stateIter);
}
} else if ((reason & LWIP_NSC_STATUS_CHANGED) || (reason & LWIP_NSC_NETIF_ADDED)) {
LT_DM(MDNS, "Netif changed/added, starting mDNS on netif %u", netif->num);
if (enableMDNS(netif) && sCachedServices.size() > 0) {
if (StateForNetIf::DISABLED == stateIter->second)
{
LT_DM(MDNS, "Starting mDNS on netif %u", netif->num);
if (enableMDNS(netif))
{
stateIter->second = StateForNetIf::ENABLED;
}
}

if ((StateForNetIf::ENABLED == stateIter->second) && (sCachedServices.size() > 0))
{
stateIter->second = StateForNetIf::CONFIGURED;
LT_DM(MDNS, "Adding services to netif %u", netif->num);
addServices(netif);
}
Expand All @@ -195,9 +226,14 @@ bool mDNS::begin(const char *hostname) {
mdns_resp_register_name_result_cb(mdnsStatusCallback);
#endif
mdns_resp_init();

struct netif *netif;
for (netif = netif_list; netif != NULL; netif = netif->next) {
enableMDNS(netif);
sNetIfState[netif->num] = StateForNetIf::DISABLED;
if (enableMDNS(netif))
{
sNetIfState[netif->num] = StateForNetIf::ENABLED;
}
}
return true;
}
Expand Down
Loading