Skip to content

Commit

Permalink
[mdns] Spawn kDnssdInitialized event after initializing mDNS server (#…
Browse files Browse the repository at this point in the history
…25050)

It turns out that when no network interfaces are up
MinMdnsResolver::IsInitialized() returns false even after
calling MinMdnsResolver::Init(). Moreover, the application
has no means to be notified when the resolver gets ready to
process DNS-SD queries.

Rename kDnssdPlatformInitialized event to kDnssdInitialized
and spawn it in the minimal mDNS implementation when the
server becomes initialized, similarly to what the platform
DNS-SD implementation does.

This is needed to have a consistent way to notify the
application that it can begin communication with other nodes
e.g. to resume persistent subscriptions.

Signed-off-by: Damian Krolik <damian.krolik@nordicsemi.no>
  • Loading branch information
Damian-Nordic authored and pull[bot] committed Aug 3, 2023
1 parent 10b2529 commit 18be4df
Show file tree
Hide file tree
Showing 19 changed files with 42 additions and 20 deletions.
2 changes: 1 addition & 1 deletion examples/all-clusters-app/nrfconnect/main/AppTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,7 @@ void AppTask::ChipEventHandler(const ChipDeviceEvent * event, intptr_t /* arg */
UpdateStatusLED();
break;
#if defined(CONFIG_NET_L2_OPENTHREAD)
case DeviceEventType::kDnssdPlatformInitialized:
case DeviceEventType::kDnssdInitialized:
#if CONFIG_CHIP_OTA_REQUESTOR
InitBasicOTARequestor();
#endif // CONFIG_CHIP_OTA_REQUESTOR
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,7 @@ void AppTask::ChipEventHandler(const ChipDeviceEvent * event, intptr_t /* arg */
sIsNetworkEnabled = ConnectivityMgr().IsThreadEnabled();
UpdateStatusLED();
break;
case DeviceEventType::kDnssdPlatformInitialized:
case DeviceEventType::kDnssdInitialized:
#if CONFIG_CHIP_OTA_REQUESTOR
InitBasicOTARequestor();
#endif
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -623,7 +623,7 @@ void AppTask::MatterEventHandler(const ChipDeviceEvent * event, intptr_t)
}

#if CHIP_DEVICE_CONFIG_ENABLE_OTA_REQUESTOR
if (event->Type == DeviceEventType::kDnssdPlatformInitialized)
if (event->Type == DeviceEventType::kDnssdInitialized)
{
K32W_LOG("Dnssd platform initialized.");
PlatformMgr().ScheduleWork(AppTask::InitOTA, 0);
Expand Down
2 changes: 1 addition & 1 deletion examples/light-switch-app/nrfconnect/main/AppTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,7 @@ void AppTask::ChipEventHandler(const ChipDeviceEvent * event, intptr_t /* arg */
UpdateStatusLED();
break;
#if defined(CONFIG_NET_L2_OPENTHREAD)
case DeviceEventType::kDnssdPlatformInitialized:
case DeviceEventType::kDnssdInitialized:
#if CONFIG_CHIP_OTA_REQUESTOR
InitBasicOTARequestor();
#endif // CONFIG_CHIP_OTA_REQUESTOR
Expand Down
2 changes: 1 addition & 1 deletion examples/lighting-app/nrfconnect/main/AppTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -579,7 +579,7 @@ void AppTask::ChipEventHandler(const ChipDeviceEvent * event, intptr_t /* arg */
UpdateStatusLED();
break;
#if defined(CONFIG_NET_L2_OPENTHREAD)
case DeviceEventType::kDnssdPlatformInitialized:
case DeviceEventType::kDnssdInitialized:
#if CONFIG_CHIP_OTA_REQUESTOR
InitBasicOTARequestor();
#endif /* CONFIG_CHIP_OTA_REQUESTOR */
Expand Down
2 changes: 1 addition & 1 deletion examples/lighting-app/nxp/k32w/k32w0/main/AppTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -601,7 +601,7 @@ void AppTask::MatterEventHandler(const ChipDeviceEvent * event, intptr_t)
}

#if CHIP_DEVICE_CONFIG_ENABLE_OTA_REQUESTOR
if (event->Type == DeviceEventType::kDnssdPlatformInitialized)
if (event->Type == DeviceEventType::kDnssdInitialized)
{
K32W_LOG("Dnssd platform initialized.");
PlatformMgr().ScheduleWork(AppTask::InitOTA, 0);
Expand Down
2 changes: 1 addition & 1 deletion examples/lock-app/nrfconnect/main/AppTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -532,7 +532,7 @@ void AppTask::ChipEventHandler(const ChipDeviceEvent * event, intptr_t /* arg */
UpdateStatusLED();
break;
#if defined(CONFIG_NET_L2_OPENTHREAD)
case DeviceEventType::kDnssdPlatformInitialized:
case DeviceEventType::kDnssdInitialized:
#if CONFIG_CHIP_OTA_REQUESTOR
InitBasicOTARequestor();
#endif // CONFIG_CHIP_OTA_REQUESTOR
Expand Down
2 changes: 1 addition & 1 deletion examples/pump-app/cc13x2x7_26x2x7/main/DeviceCallbacks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ void DeviceCallbacks::DeviceEventCallback(const ChipDeviceEvent * event, intptr_
PLAT_LOG("## Operational network enabled");
break;

case DeviceEventType::kDnssdPlatformInitialized:
case DeviceEventType::kDnssdInitialized:
PLAT_LOG("## Dnssd platform initialized");
break;

Expand Down
2 changes: 1 addition & 1 deletion examples/pump-app/nrfconnect/main/AppTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,7 @@ void AppTask::ChipEventHandler(const ChipDeviceEvent * event, intptr_t /* arg */
sIsNetworkEnabled = ConnectivityMgr().IsThreadEnabled();
UpdateStatusLED();
break;
case DeviceEventType::kDnssdPlatformInitialized:
case DeviceEventType::kDnssdInitialized:
#if CONFIG_CHIP_OTA_REQUESTOR
InitBasicOTARequestor();
#endif
Expand Down
2 changes: 1 addition & 1 deletion examples/pump-controller-app/nrfconnect/main/AppTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,7 @@ void AppTask::ChipEventHandler(const ChipDeviceEvent * event, intptr_t /* arg */
sIsNetworkEnabled = ConnectivityMgr().IsThreadEnabled();
UpdateStatusLED();
break;
case DeviceEventType::kDnssdPlatformInitialized:
case DeviceEventType::kDnssdInitialized:
#if CONFIG_CHIP_OTA_REQUESTOR
InitBasicOTARequestor();
#endif
Expand Down
2 changes: 1 addition & 1 deletion examples/window-app/nrfconnect/main/AppTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -549,7 +549,7 @@ void AppTask::ChipEventHandler(const ChipDeviceEvent * event, intptr_t /* arg */
sIsNetworkEnabled = ConnectivityMgr().IsThreadEnabled();
UpdateStatusLED();
break;
case DeviceEventType::kDnssdPlatformInitialized:
case DeviceEventType::kDnssdInitialized:
#if CONFIG_CHIP_OTA_REQUESTOR
InitBasicOTARequestor();
#endif
Expand Down
2 changes: 1 addition & 1 deletion src/app/server/Dnssd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ void OnPlatformEvent(const DeviceLayer::ChipDeviceEvent * event)
{
switch (event->Type)
{
case DeviceLayer::DeviceEventType::kDnssdPlatformInitialized:
case DeviceLayer::DeviceEventType::kDnssdInitialized:
case DeviceLayer::DeviceEventType::kDnssdRestartNeeded:
#if CHIP_DEVICE_CONFIG_ENABLE_SED
case DeviceLayer::DeviceEventType::kSEDIntervalChange:
Expand Down
4 changes: 3 additions & 1 deletion src/app/server/Server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ void Server::OnPlatformEvent(const DeviceLayer::ChipDeviceEvent & event)
{
switch (event.Type)
{
case DeviceEventType::kDnssdPlatformInitialized:
case DeviceEventType::kDnssdInitialized:
// Platform DNS-SD implementation uses kPlatformDnssdInitialized event to signal that it's ready.
if (!mIsDnssdReady)
{
Expand All @@ -402,6 +402,8 @@ void Server::CheckServerReadyEvent()
// are ready, and emit the 'server ready' event if so.
if (mIsDnssdReady)
{
ChipLogError(AppServer, "Server initialization complete");

ChipDeviceEvent event = { .Type = DeviceEventType::kServerReady };
PlatformMgr().PostEventOrDie(&event);
}
Expand Down
4 changes: 2 additions & 2 deletions src/include/platform/CHIPDeviceEvent.h
Original file line number Diff line number Diff line change
Expand Up @@ -212,9 +212,9 @@ enum PublicEventTypes
kOperationalNetworkEnabled,

/**
* Signals that DNS-SD platform layer was initialized and is ready to operate.
* Signals that DNS-SD has been initialized and is ready to operate.
*/
kDnssdPlatformInitialized,
kDnssdInitialized,

/**
* Signals that DNS-SD backend was restarted and services must be published again.
Expand Down
2 changes: 1 addition & 1 deletion src/lib/dnssd/Advertiser_ImplMinimalMdns.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ CHIP_ERROR AdvertiserMinMdns::Init(chip::Inet::EndPointManager<chip::Inet::UDPEn
// is true. But we don't handle updates to our set of interfaces right now,
// so rely on the logic in this function to shut down and restart the
// GlobalMinimalMdnsServer to handle that.
GlobalMinimalMdnsServer::Server().Shutdown();
GlobalMinimalMdnsServer::Server().ShutdownEndpoints();

if (!mIsInitialized)
{
Expand Down
2 changes: 1 addition & 1 deletion src/lib/dnssd/Discovery_ImplPlatform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,7 @@ void DiscoveryImplPlatform::HandleDnssdInit(void * context, CHIP_ERROR initError

// Post an event that will start advertising
DeviceLayer::ChipDeviceEvent event;
event.Type = DeviceLayer::DeviceEventType::kDnssdPlatformInitialized;
event.Type = DeviceLayer::DeviceEventType::kDnssdInitialized;

CHIP_ERROR error = DeviceLayer::PlatformMgr().PostEvent(&event);
if (error != CHIP_NO_ERROR)
Expand Down
2 changes: 1 addition & 1 deletion src/lib/dnssd/MinimalMdnsServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ GlobalMinimalMdnsServer & GlobalMinimalMdnsServer::Instance()
CHIP_ERROR GlobalMinimalMdnsServer::StartServer(chip::Inet::EndPointManager<chip::Inet::UDPEndPoint> * udpEndPointManager,
uint16_t port)
{
GlobalMinimalMdnsServer::Server().Shutdown();
GlobalMinimalMdnsServer::Server().ShutdownEndpoints();

UniquePtr<ListenIterator> endpoints = GetAddressPolicy()->GetListenEndpoints();

Expand Down
20 changes: 19 additions & 1 deletion src/lib/dnssd/minimal_mdns/Server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include <utility>

#include <lib/dnssd/minimal_mdns/core/DnsHeader.h>
#include <platform/CHIPDeviceLayer.h>

namespace mdns {
namespace Minimal {
Expand Down Expand Up @@ -190,6 +191,12 @@ ServerBase::~ServerBase()
}

void ServerBase::Shutdown()
{
ShutdownEndpoints();
mIsInitialized = false;
}

void ServerBase::ShutdownEndpoints()
{
mEndpoints.ReleaseAll();
}
Expand All @@ -216,7 +223,7 @@ bool ServerBase::IsListening() const
CHIP_ERROR ServerBase::Listen(chip::Inet::EndPointManager<chip::Inet::UDPEndPoint> * udpEndPointManager, ListenIterator * it,
uint16_t port)
{
Shutdown(); // ensure everything starts fresh
ShutdownEndpoints(); // ensure everything starts fresh

chip::Inet::InterfaceId interfaceId = chip::Inet::InterfaceId::Null();
chip::Inet::IPAddressType addressType;
Expand Down Expand Up @@ -271,6 +278,17 @@ CHIP_ERROR ServerBase::Listen(chip::Inet::EndPointManager<chip::Inet::UDPEndPoin
mEndpoints.CreateObject(interfaceId, addressType, std::move(endPointHolder));
}
#endif

// If at least one interface is used by the mDNS server, notify the application that DNS-SD is ready.
if (!mIsInitialized)
{
#if !CHIP_DEVICE_LAYER_NONE
chip::DeviceLayer::ChipDeviceEvent event{};
event.Type = chip::DeviceLayer::DeviceEventType::kDnssdInitialized;
chip::DeviceLayer::PlatformMgr().PostEventOrDie(&event);
#endif
mIsInitialized = true;
}
}

return autoShutdown.ReturnSuccess();
Expand Down
4 changes: 3 additions & 1 deletion src/lib/dnssd/minimal_mdns/Server.h
Original file line number Diff line number Diff line change
Expand Up @@ -138,9 +138,10 @@ class ServerBase
}
virtual ~ServerBase();

/// Closes all currently open endpoints
/// Closes all currently open endpoints and resets the 'initialized' flag
void Shutdown();

void ShutdownEndpoints();
void ShutdownEndpoint(EndpointInfo & aEndpoint);

/// Listen on the given interfaces/address types.
Expand Down Expand Up @@ -206,6 +207,7 @@ class ServerBase
#if INET_CONFIG_ENABLE_IPV4
chip::Inet::IPAddress mIpv4BroadcastAddress;
#endif
bool mIsInitialized = false;
};

// The PoolImpl impl is used as a base class because its destructor must be called after ServerBase's destructor.
Expand Down

0 comments on commit 18be4df

Please sign in to comment.