From 5167731ca7134764bce9bc13c397f792402f2d12 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Damian=20Kr=C3=B3lik?= <66667989+Damian-Nordic@users.noreply.github.com> Date: Tue, 14 Feb 2023 17:53:28 +0100 Subject: [PATCH] [mdns] Spawn kDnssdInitialized event after initializing mDNS server (#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 --- .../nrfconnect/main/AppTask.cpp | 2 +- .../nrfconnect/main/AppTask.cpp | 2 +- .../nxp/k32w/k32w0/main/AppTask.cpp | 2 +- .../nrfconnect/main/AppTask.cpp | 2 +- .../lighting-app/nrfconnect/main/AppTask.cpp | 2 +- .../nxp/k32w/k32w0/main/AppTask.cpp | 2 +- examples/lock-app/nrfconnect/main/AppTask.cpp | 2 +- .../cc13x2x7_26x2x7/main/DeviceCallbacks.cpp | 2 +- examples/pump-app/nrfconnect/main/AppTask.cpp | 2 +- .../nrfconnect/main/AppTask.cpp | 2 +- .../window-app/nrfconnect/main/AppTask.cpp | 2 +- src/app/server/Dnssd.cpp | 2 +- src/app/server/Server.cpp | 4 +++- src/include/platform/CHIPDeviceEvent.h | 4 ++-- src/lib/dnssd/Advertiser_ImplMinimalMdns.cpp | 2 +- src/lib/dnssd/Discovery_ImplPlatform.cpp | 2 +- src/lib/dnssd/MinimalMdnsServer.cpp | 2 +- src/lib/dnssd/minimal_mdns/Server.cpp | 20 ++++++++++++++++++- src/lib/dnssd/minimal_mdns/Server.h | 4 +++- 19 files changed, 42 insertions(+), 20 deletions(-) diff --git a/examples/all-clusters-app/nrfconnect/main/AppTask.cpp b/examples/all-clusters-app/nrfconnect/main/AppTask.cpp index 1d5a001dd1e9b1..0cee0e74ba78c8 100644 --- a/examples/all-clusters-app/nrfconnect/main/AppTask.cpp +++ b/examples/all-clusters-app/nrfconnect/main/AppTask.cpp @@ -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 diff --git a/examples/all-clusters-minimal-app/nrfconnect/main/AppTask.cpp b/examples/all-clusters-minimal-app/nrfconnect/main/AppTask.cpp index b0e23d9c4c54e4..fc565a94d1c12f 100644 --- a/examples/all-clusters-minimal-app/nrfconnect/main/AppTask.cpp +++ b/examples/all-clusters-minimal-app/nrfconnect/main/AppTask.cpp @@ -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 diff --git a/examples/contact-sensor-app/nxp/k32w/k32w0/main/AppTask.cpp b/examples/contact-sensor-app/nxp/k32w/k32w0/main/AppTask.cpp index 91c8a240169627..7c3f30d33e6644 100644 --- a/examples/contact-sensor-app/nxp/k32w/k32w0/main/AppTask.cpp +++ b/examples/contact-sensor-app/nxp/k32w/k32w0/main/AppTask.cpp @@ -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); diff --git a/examples/light-switch-app/nrfconnect/main/AppTask.cpp b/examples/light-switch-app/nrfconnect/main/AppTask.cpp index d2f7a6a6581c8d..1adb749a1cc931 100644 --- a/examples/light-switch-app/nrfconnect/main/AppTask.cpp +++ b/examples/light-switch-app/nrfconnect/main/AppTask.cpp @@ -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 diff --git a/examples/lighting-app/nrfconnect/main/AppTask.cpp b/examples/lighting-app/nrfconnect/main/AppTask.cpp index 8948ae1d3b3d33..485d000d5cfb75 100644 --- a/examples/lighting-app/nrfconnect/main/AppTask.cpp +++ b/examples/lighting-app/nrfconnect/main/AppTask.cpp @@ -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 */ diff --git a/examples/lighting-app/nxp/k32w/k32w0/main/AppTask.cpp b/examples/lighting-app/nxp/k32w/k32w0/main/AppTask.cpp index 9a66623836036b..dca537f2d97881 100644 --- a/examples/lighting-app/nxp/k32w/k32w0/main/AppTask.cpp +++ b/examples/lighting-app/nxp/k32w/k32w0/main/AppTask.cpp @@ -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); diff --git a/examples/lock-app/nrfconnect/main/AppTask.cpp b/examples/lock-app/nrfconnect/main/AppTask.cpp index fc6c6bece1faf2..c6b6a1fd5b6654 100644 --- a/examples/lock-app/nrfconnect/main/AppTask.cpp +++ b/examples/lock-app/nrfconnect/main/AppTask.cpp @@ -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 diff --git a/examples/pump-app/cc13x2x7_26x2x7/main/DeviceCallbacks.cpp b/examples/pump-app/cc13x2x7_26x2x7/main/DeviceCallbacks.cpp index a276405355482f..8cda813016e45e 100644 --- a/examples/pump-app/cc13x2x7_26x2x7/main/DeviceCallbacks.cpp +++ b/examples/pump-app/cc13x2x7_26x2x7/main/DeviceCallbacks.cpp @@ -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; diff --git a/examples/pump-app/nrfconnect/main/AppTask.cpp b/examples/pump-app/nrfconnect/main/AppTask.cpp index ac94f703c2ce2f..94a7088dbc7644 100644 --- a/examples/pump-app/nrfconnect/main/AppTask.cpp +++ b/examples/pump-app/nrfconnect/main/AppTask.cpp @@ -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 diff --git a/examples/pump-controller-app/nrfconnect/main/AppTask.cpp b/examples/pump-controller-app/nrfconnect/main/AppTask.cpp index 2b11e7cbbb37ac..b97b11eabc1baf 100644 --- a/examples/pump-controller-app/nrfconnect/main/AppTask.cpp +++ b/examples/pump-controller-app/nrfconnect/main/AppTask.cpp @@ -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 diff --git a/examples/window-app/nrfconnect/main/AppTask.cpp b/examples/window-app/nrfconnect/main/AppTask.cpp index 119f8346c4cdda..deb0cffb5e1a44 100644 --- a/examples/window-app/nrfconnect/main/AppTask.cpp +++ b/examples/window-app/nrfconnect/main/AppTask.cpp @@ -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 diff --git a/src/app/server/Dnssd.cpp b/src/app/server/Dnssd.cpp index 40f0da6f8ccca5..fc1faeb25b328a 100644 --- a/src/app/server/Dnssd.cpp +++ b/src/app/server/Dnssd.cpp @@ -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: diff --git a/src/app/server/Server.cpp b/src/app/server/Server.cpp index c1d6d25414eef7..c3fd69e80817e0 100644 --- a/src/app/server/Server.cpp +++ b/src/app/server/Server.cpp @@ -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) { @@ -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); } diff --git a/src/include/platform/CHIPDeviceEvent.h b/src/include/platform/CHIPDeviceEvent.h index 5d9dd5ee312f35..591794cbc21705 100644 --- a/src/include/platform/CHIPDeviceEvent.h +++ b/src/include/platform/CHIPDeviceEvent.h @@ -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. diff --git a/src/lib/dnssd/Advertiser_ImplMinimalMdns.cpp b/src/lib/dnssd/Advertiser_ImplMinimalMdns.cpp index d38b8f5ff1e37d..47ee3b10e7d073 100644 --- a/src/lib/dnssd/Advertiser_ImplMinimalMdns.cpp +++ b/src/lib/dnssd/Advertiser_ImplMinimalMdns.cpp @@ -337,7 +337,7 @@ CHIP_ERROR AdvertiserMinMdns::Init(chip::Inet::EndPointManager * udpEndPointManager, uint16_t port) { - GlobalMinimalMdnsServer::Server().Shutdown(); + GlobalMinimalMdnsServer::Server().ShutdownEndpoints(); UniquePtr endpoints = GetAddressPolicy()->GetListenEndpoints(); diff --git a/src/lib/dnssd/minimal_mdns/Server.cpp b/src/lib/dnssd/minimal_mdns/Server.cpp index 9db53bee0c4c6c..ea6dc693739d86 100644 --- a/src/lib/dnssd/minimal_mdns/Server.cpp +++ b/src/lib/dnssd/minimal_mdns/Server.cpp @@ -21,6 +21,7 @@ #include #include +#include namespace mdns { namespace Minimal { @@ -190,6 +191,12 @@ ServerBase::~ServerBase() } void ServerBase::Shutdown() +{ + ShutdownEndpoints(); + mIsInitialized = false; +} + +void ServerBase::ShutdownEndpoints() { mEndpoints.ReleaseAll(); } @@ -216,7 +223,7 @@ bool ServerBase::IsListening() const CHIP_ERROR ServerBase::Listen(chip::Inet::EndPointManager * 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; @@ -271,6 +278,17 @@ CHIP_ERROR ServerBase::Listen(chip::Inet::EndPointManager