From 9ea2cc4bcfcced7d03a8235f3a58c7d1d61dd29e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Damian=20Kr=C3=B3lik?= <66667989+Damian-Nordic@users.noreply.github.com> Date: Wed, 6 Oct 2021 08:17:52 +0200 Subject: [PATCH] [DNS-SD] Redesign ServiceAdvertiser and Resolver interfaces (#10181) * [DNS-SD] Redesign ServiceAdvertiser and Resolver interfaces * Change Resolver::SetResolverDelegate's return type to void. * Replace ServiceAdvertiser::Start with Init, and ServiceAdvertiser::StopPublishDevice with RemoveServices as the purpose of the former methods was unclear and the code in DiscoveryImplPlatform::Start would do the same as in DiscoveryImplPlatform::StopPublishDevice. Now Init is meant for one-time operations and RemoveServices for removing advertised services so they can be refreshed. * Likewise, replace Resolver::Start with Init. * Add ServiceAdvertiser::CompleteServiceUpdate which should be called after publishing all desired services with Advertise methods. This may come in handy in other platforms, too, but is particularly useful for Thread-based platforms using SRP instead of mDNS. Such platforms cannot immediately remove and re-add DNS services. Instead, they must communicate with the SRP server to complete any of these operations. CompleteServiceUpdate method allows Thread-based platform to remove services which have been marked for removal with the RemoveServices method and have not been re-added with subsequent Advertise calls. * Address some review comments * Rename CompleteServiceUpdate to FinalizeServiceUpdate --- .../chip-tool/commands/discover/Commands.h | 7 +-- examples/minimal-mdns/advertiser.cpp | 2 +- src/app/server/Mdns.cpp | 33 ++++++---- src/app/server/Server.cpp | 2 +- .../AbstractMdnsDiscoveryController.cpp | 4 +- .../AbstractMdnsDiscoveryController.h | 2 - src/controller/CHIPDeviceController.cpp | 6 +- .../python/chip/discovery/NodeResolution.cpp | 8 +-- .../TestCommissionableNodeController.cpp | 28 +++------ src/include/platform/ThreadStackManager.h | 12 +++- src/lib/mdns/Advertiser.h | 62 +++++++++++++++---- src/lib/mdns/Advertiser_ImplMinimalMdns.cpp | 18 ++++-- src/lib/mdns/Advertiser_ImplNone.cpp | 17 +++-- src/lib/mdns/Discovery_ImplPlatform.cpp | 54 +++++++--------- src/lib/mdns/Discovery_ImplPlatform.h | 31 +++------- src/lib/mdns/Resolver.h | 55 ++++++++++++---- src/lib/mdns/Resolver_ImplMinimalMdns.cpp | 18 ++---- src/lib/mdns/Resolver_ImplNone.cpp | 7 +-- src/lib/mdns/minimal/tests/TestAdvertiser.cpp | 18 ++++-- src/lib/mdns/platform/Mdns.h | 37 ++++++----- src/lib/mdns/platform/tests/TestPlatform.cpp | 14 ++++- src/lib/shell/commands/Dns.cpp | 2 +- src/platform/Darwin/MdnsImpl.cpp | 6 +- src/platform/ESP32/MdnsImpl.cpp | 6 +- src/platform/Linux/MdnsImpl.cpp | 6 +- ...nericThreadStackManagerImpl_OpenThread.cpp | 30 +++++++-- ...GenericThreadStackManagerImpl_OpenThread.h | 5 +- src/platform/OpenThread/MdnsImpl.cpp | 15 ++--- src/platform/Tizen/MdnsImpl.cpp | 7 ++- src/platform/android/MdnsImpl.cpp | 8 +-- src/platform/fake/MdnsImpl.cpp | 8 +-- 31 files changed, 308 insertions(+), 220 deletions(-) diff --git a/examples/chip-tool/commands/discover/Commands.h b/examples/chip-tool/commands/discover/Commands.h index 0879bf7877df1d..e3317409fdfcc0 100644 --- a/examples/chip-tool/commands/discover/Commands.h +++ b/examples/chip-tool/commands/discover/Commands.h @@ -24,8 +24,6 @@ #include #include -constexpr uint16_t kMdnsPort = 5353; - class Resolve : public DiscoverCommand, public chip::Mdns::ResolverDelegate { public: @@ -34,9 +32,8 @@ class Resolve : public DiscoverCommand, public chip::Mdns::ResolverDelegate /////////// DiscoverCommand Interface ///////// CHIP_ERROR RunCommand(NodeId remoteId, uint64_t fabricId) override { - ReturnErrorOnFailure(chip::Mdns::Resolver::Instance().StartResolver(&chip::DeviceLayer::InetLayer, kMdnsPort)); - ReturnErrorOnFailure(chip::Mdns::Resolver::Instance().SetResolverDelegate(nullptr)); - ReturnErrorOnFailure(chip::Mdns::Resolver::Instance().SetResolverDelegate(this)); + ReturnErrorOnFailure(chip::Mdns::Resolver::Instance().Init(&chip::DeviceLayer::InetLayer)); + chip::Mdns::Resolver::Instance().SetResolverDelegate(this); ChipLogProgress(chipTool, "Mdns: Searching for NodeId: %" PRIx64 " FabricId: %" PRIx64 " ...", remoteId, fabricId); return chip::Mdns::Resolver::Instance().ResolveNodeId(chip::PeerId().SetNodeId(remoteId).SetCompressedFabricId(fabricId), chip::Inet::kIPAddressType_Any); diff --git a/examples/minimal-mdns/advertiser.cpp b/examples/minimal-mdns/advertiser.cpp index 74ea7163d1d3dd..4cce1d19911d00 100644 --- a/examples/minimal-mdns/advertiser.cpp +++ b/examples/minimal-mdns/advertiser.cpp @@ -255,7 +255,7 @@ int main(int argc, char ** args) return 1; } - if (chip::Mdns::ServiceAdvertiser::Instance().Start(&DeviceLayer::InetLayer, Mdns::kMdnsPort) != CHIP_NO_ERROR) + if (chip::Mdns::ServiceAdvertiser::Instance().Init(&DeviceLayer::InetLayer) != CHIP_NO_ERROR) { fprintf(stderr, "FAILED to start MDNS advertisement\n"); return 1; diff --git a/src/app/server/Mdns.cpp b/src/app/server/Mdns.cpp index ca94651553839e..26b946c9a4602b 100644 --- a/src/app/server/Mdns.cpp +++ b/src/app/server/Mdns.cpp @@ -148,17 +148,17 @@ bool MdnsServer::OnExpiration(uint64_t expirationMs) ChipLogDetail(Discovery, "OnExpiration - valid time out"); - // reset advertising - CHIP_ERROR err; - err = chip::Mdns::ServiceAdvertiser::Instance().StopPublishDevice(); + CHIP_ERROR err = Mdns::ServiceAdvertiser::Instance().Init(&chip::DeviceLayer::InetLayer); if (err != CHIP_NO_ERROR) { - ChipLogError(Discovery, "Failed to stop ServiceAdvertiser: %s", chip::ErrorStr(err)); + ChipLogError(Discovery, "Failed to initialize advertiser: %s", chip::ErrorStr(err)); } - err = chip::Mdns::ServiceAdvertiser::Instance().Start(&chip::DeviceLayer::InetLayer, chip::Mdns::kMdnsPort); + + // reset advertising + err = Mdns::ServiceAdvertiser::Instance().RemoveServices(); if (err != CHIP_NO_ERROR) { - ChipLogError(Discovery, "Failed to start ServiceAdvertiser: %s", chip::ErrorStr(err)); + ChipLogError(Discovery, "Failed to remove advertised services: %s", chip::ErrorStr(err)); } // restart operational (if needed) @@ -176,6 +176,12 @@ bool MdnsServer::OnExpiration(uint64_t expirationMs) } #endif // CHIP_DEVICE_CONFIG_ENABLE_COMMISSIONER_DISCOVERY + err = Mdns::ServiceAdvertiser::Instance().FinalizeServiceUpdate(); + if (err != CHIP_NO_ERROR) + { + ChipLogError(Discovery, "Failed to finalize service update: %s", chip::ErrorStr(err)); + } + return true; } @@ -406,17 +412,16 @@ void MdnsServer::StartServer(chip::Mdns::CommissioningMode mode) ClearTimeouts(); - CHIP_ERROR err; - err = chip::Mdns::ServiceAdvertiser::Instance().StopPublishDevice(); + CHIP_ERROR err = Mdns::ServiceAdvertiser::Instance().Init(&chip::DeviceLayer::InetLayer); if (err != CHIP_NO_ERROR) { - ChipLogError(Discovery, "Failed to stop ServiceAdvertiser: %s", chip::ErrorStr(err)); + ChipLogError(Discovery, "Failed to initialize advertiser: %s", chip::ErrorStr(err)); } - err = chip::Mdns::ServiceAdvertiser::Instance().Start(&chip::DeviceLayer::InetLayer, chip::Mdns::kMdnsPort); + err = Mdns::ServiceAdvertiser::Instance().RemoveServices(); if (err != CHIP_NO_ERROR) { - ChipLogError(Discovery, "Failed to start ServiceAdvertiser: %s", chip::ErrorStr(err)); + ChipLogError(Discovery, "Failed to remove advertised services: %s", chip::ErrorStr(err)); } err = AdvertiseOperational(); @@ -471,6 +476,12 @@ void MdnsServer::StartServer(chip::Mdns::CommissioningMode mode) ChipLogError(Discovery, "Failed to advertise commissioner: %s", chip::ErrorStr(err)); } #endif // CHIP_DEVICE_CONFIG_ENABLE_COMMISSIONER_DISCOVERY + + err = Mdns::ServiceAdvertiser::Instance().FinalizeServiceUpdate(); + if (err != CHIP_NO_ERROR) + { + ChipLogError(Discovery, "Failed to finalize service update: %s", chip::ErrorStr(err)); + } } #if CHIP_ENABLE_ROTATING_DEVICE_ID diff --git a/src/app/server/Server.cpp b/src/app/server/Server.cpp index ae9d67928a39db..b74f9f4f8ef520 100644 --- a/src/app/server/Server.cpp +++ b/src/app/server/Server.cpp @@ -187,7 +187,7 @@ CHIP_ERROR Server::Init(AppDelegate * delegate, uint16_t secureServicePort, uint void Server::Shutdown() { - chip::Mdns::ServiceAdvertiser::Instance().StopPublishDevice(); + chip::Mdns::ServiceAdvertiser::Instance().Shutdown(); chip::app::InteractionModelEngine::GetInstance()->Shutdown(); mExchangeMgr.Shutdown(); mSessions.Shutdown(); diff --git a/src/controller/AbstractMdnsDiscoveryController.cpp b/src/controller/AbstractMdnsDiscoveryController.cpp index c660411ba44cba..cf4db349d31e89 100644 --- a/src/controller/AbstractMdnsDiscoveryController.cpp +++ b/src/controller/AbstractMdnsDiscoveryController.cpp @@ -66,10 +66,10 @@ void AbstractMdnsDiscoveryController::OnNodeDiscoveryComplete(const chip::Mdns:: CHIP_ERROR AbstractMdnsDiscoveryController::SetUpNodeDiscovery() { - ReturnErrorOnFailure(mResolver->SetResolverDelegate(this)); #if CONFIG_DEVICE_LAYER - ReturnErrorOnFailure(mResolver->StartResolver(&DeviceLayer::InetLayer, kMdnsPort)); + ReturnErrorOnFailure(mResolver->Init(&DeviceLayer::InetLayer)); #endif + mResolver->SetResolverDelegate(this); auto discoveredNodes = GetDiscoveredNodes(); for (auto & discoveredNode : discoveredNodes) diff --git a/src/controller/AbstractMdnsDiscoveryController.h b/src/controller/AbstractMdnsDiscoveryController.h index 6fcdd9a7d6dbe0..184fe7aed8bb23 100644 --- a/src/controller/AbstractMdnsDiscoveryController.h +++ b/src/controller/AbstractMdnsDiscoveryController.h @@ -27,8 +27,6 @@ namespace chip { namespace Controller { -constexpr uint16_t kMdnsPort = 5353; - /** * @brief * Convenient superclass for controller implementations that need to discover diff --git a/src/controller/CHIPDeviceController.cpp b/src/controller/CHIPDeviceController.cpp index ddc3cc02c8c99d..77b3383895bb5f 100644 --- a/src/controller/CHIPDeviceController.cpp +++ b/src/controller/CHIPDeviceController.cpp @@ -135,10 +135,10 @@ CHIP_ERROR DeviceController::Init(ControllerInitParams params) params.systemState->ExchangeMgr()->SetDelegate(this); #if CHIP_DEVICE_CONFIG_ENABLE_MDNS - ReturnErrorOnFailure(Mdns::Resolver::Instance().SetResolverDelegate(this)); + Mdns::Resolver::Instance().Init(params.systemState->InetLayer()); + Mdns::Resolver::Instance().SetResolverDelegate(this); RegisterDeviceAddressUpdateDelegate(params.deviceAddressUpdateDelegate); RegisterDeviceDiscoveryDelegate(params.deviceDiscoveryDelegate); - Mdns::Resolver::Instance().StartResolver(params.systemState->InetLayer(), kMdnsPort); #endif // CHIP_DEVICE_CONFIG_ENABLE_MDNS InitDataModelHandler(params.systemState->ExchangeMgr()); @@ -217,7 +217,7 @@ CHIP_ERROR DeviceController::Shutdown() mState = State::NotInitialized; #if CHIP_DEVICE_CONFIG_ENABLE_MDNS - Mdns::Resolver::Instance().ShutdownResolver(); + Mdns::Resolver::Instance().Shutdown(); #endif // CHIP_DEVICE_CONFIG_ENABLE_MDNS mStorageDelegate = nullptr; diff --git a/src/controller/python/chip/discovery/NodeResolution.cpp b/src/controller/python/chip/discovery/NodeResolution.cpp index 727f07e39777eb..48fd194ecded08 100644 --- a/src/controller/python/chip/discovery/NodeResolution.cpp +++ b/src/controller/python/chip/discovery/NodeResolution.cpp @@ -33,8 +33,6 @@ namespace { using DiscoverSuccessCallback = void (*)(uint64_t fabricId, uint64_t nodeId, uint32_t interfaceId, const char * ip, uint16_t port); using DiscoverFailureCallback = void (*)(uint64_t fabricId, uint64_t nodeId, ChipError::StorageType error_code); -constexpr uint16_t kMdnsPort = 5353; - class PythonResolverDelegate : public ResolverDelegate { public: @@ -95,11 +93,9 @@ extern "C" ChipError::StorageType pychip_discovery_resolve(uint64_t fabricId, ui CHIP_ERROR result = CHIP_NO_ERROR; chip::python::ChipMainThreadScheduleAndWait([&] { - result = Resolver::Instance().StartResolver(&chip::DeviceLayer::InetLayer, kMdnsPort); - ReturnOnFailure(result); - - result = Resolver::Instance().SetResolverDelegate(&gPythonResolverDelegate); + result = Resolver::Instance().Init(&chip::DeviceLayer::InetLayer); ReturnOnFailure(result); + Resolver::Instance().SetResolverDelegate(&gPythonResolverDelegate); result = Resolver::Instance().ResolveNodeId(chip::PeerId().SetCompressedFabricId(fabricId).SetNodeId(nodeId), chip::Inet::IPAddressType::kIPAddressType_Any); diff --git a/src/controller/tests/TestCommissionableNodeController.cpp b/src/controller/tests/TestCommissionableNodeController.cpp index 4da8c790fe9415..fa59a2dfb3aa7e 100644 --- a/src/controller/tests/TestCommissionableNodeController.cpp +++ b/src/controller/tests/TestCommissionableNodeController.cpp @@ -31,17 +31,16 @@ namespace { class MockResolver : public Resolver { public: - CHIP_ERROR SetResolverDelegate(ResolverDelegate *) override { return SetResolverDelegateStatus; } - CHIP_ERROR StartResolver(chip::Inet::InetLayer * inetLayer, uint16_t port) override { return StartResolverStatus; } - void ShutdownResolver() override {} + CHIP_ERROR Init(chip::Inet::InetLayer * inetLayer) override { return InitStatus; } + void Shutdown() override {} + void SetResolverDelegate(ResolverDelegate *) override {} CHIP_ERROR ResolveNodeId(const PeerId & peerId, Inet::IPAddressType type) override { return ResolveNodeIdStatus; } CHIP_ERROR FindCommissioners(DiscoveryFilter filter = DiscoveryFilter()) override { return FindCommissionersStatus; } CHIP_ERROR FindCommissionableNodes(DiscoveryFilter filter = DiscoveryFilter()) override { return CHIP_ERROR_NOT_IMPLEMENTED; } - CHIP_ERROR SetResolverDelegateStatus = CHIP_NO_ERROR; - CHIP_ERROR StartResolverStatus = CHIP_NO_ERROR; - CHIP_ERROR ResolveNodeIdStatus = CHIP_NO_ERROR; - CHIP_ERROR FindCommissionersStatus = CHIP_NO_ERROR; + CHIP_ERROR InitStatus = CHIP_NO_ERROR; + CHIP_ERROR ResolveNodeIdStatus = CHIP_NO_ERROR; + CHIP_ERROR FindCommissionersStatus = CHIP_NO_ERROR; }; } // namespace @@ -143,18 +142,10 @@ void TestDiscoverCommissioners_HappyCaseWithDiscoveryFilter(nlTestSuite * inSuit CHIP_NO_ERROR); } -void TestDiscoverCommissioners_SetResolverDelegateError_ReturnsError(nlTestSuite * inSuite, void * inContext) +void TestDiscoverCommissioners_InitError_ReturnsError(nlTestSuite * inSuite, void * inContext) { MockResolver resolver; - resolver.SetResolverDelegateStatus = CHIP_ERROR_INTERNAL; - CommissionableNodeController controller(&resolver); - NL_TEST_ASSERT(inSuite, controller.DiscoverCommissioners() != CHIP_NO_ERROR); -} - -void TestDiscoverCommissioners_StartResolverError_ReturnsError(nlTestSuite * inSuite, void * inContext) -{ - MockResolver resolver; - resolver.StartResolverStatus = CHIP_ERROR_INTERNAL; + resolver.InitStatus = CHIP_ERROR_INTERNAL; CommissionableNodeController controller(&resolver); NL_TEST_ASSERT(inSuite, controller.DiscoverCommissioners() != CHIP_NO_ERROR); } @@ -178,8 +169,7 @@ const nlTest sTests[] = NL_TEST_DEF("TestGetDiscoveredCommissioner_NoNodesDiscovered_ReturnsNullptr", TestGetDiscoveredCommissioner_NoNodesDiscovered_ReturnsNullptr), NL_TEST_DEF("TestDiscoverCommissioners_HappyCase", TestDiscoverCommissioners_HappyCase), NL_TEST_DEF("TestDiscoverCommissioners_HappyCaseWithDiscoveryFilter", TestDiscoverCommissioners_HappyCaseWithDiscoveryFilter), - NL_TEST_DEF("TestDiscoverCommissioners_SetResolverDelegateError_ReturnsError", TestDiscoverCommissioners_SetResolverDelegateError_ReturnsError), - NL_TEST_DEF("TestDiscoverCommissioners_StartResolverError_ReturnsError", TestDiscoverCommissioners_StartResolverError_ReturnsError), + NL_TEST_DEF("TestDiscoverCommissioners_InitError_ReturnsError", TestDiscoverCommissioners_InitError_ReturnsError), NL_TEST_DEF("TestDiscoverCommissioners_FindCommissionersError_ReturnsError", TestDiscoverCommissioners_FindCommissionersError_ReturnsError), NL_TEST_SENTINEL() }; diff --git a/src/include/platform/ThreadStackManager.h b/src/include/platform/ThreadStackManager.h index 918354cb3cd5c9..e55428a346eca1 100644 --- a/src/include/platform/ThreadStackManager.h +++ b/src/include/platform/ThreadStackManager.h @@ -99,7 +99,8 @@ class ThreadStackManager const Span & aSubTypes, const Span & aTxtEntries, uint32_t aLeaseInterval, uint32_t aKeyLeaseInterval); CHIP_ERROR RemoveSrpService(const char * aInstanceName, const char * aName); - CHIP_ERROR RemoveAllSrpServices(); + CHIP_ERROR InvalidateAllSrpServices(); ///< Mark all SRP services as invalid + CHIP_ERROR RemoveInvalidSrpServices(); ///< Remove SRP services marked as invalid CHIP_ERROR SetupSrpHost(const char * aHostName); #if CHIP_DEVICE_CONFIG_ENABLE_THREAD_DNS_CLIENT @@ -259,9 +260,14 @@ inline CHIP_ERROR ThreadStackManager::RemoveSrpService(const char * aInstanceNam return static_cast(this)->_RemoveSrpService(aInstanceName, aName); } -inline CHIP_ERROR ThreadStackManager::RemoveAllSrpServices() +inline CHIP_ERROR ThreadStackManager::InvalidateAllSrpServices() { - return static_cast(this)->_RemoveAllSrpServices(); + return static_cast(this)->_InvalidateAllSrpServices(); +} + +inline CHIP_ERROR ThreadStackManager::RemoveInvalidSrpServices() +{ + return static_cast(this)->_RemoveInvalidSrpServices(); } inline CHIP_ERROR ThreadStackManager::SetupSrpHost(const char * aHostName) diff --git a/src/lib/mdns/Advertiser.h b/src/lib/mdns/Advertiser.h index 1fabef103694b9..224ae8479102e5 100644 --- a/src/lib/mdns/Advertiser.h +++ b/src/lib/mdns/Advertiser.h @@ -285,30 +285,68 @@ class CommissionAdvertisingParameters : public BaseAdvertisingParams DiscoveryImplPlatform::sMdnsCache; DiscoveryImplPlatform::DiscoveryImplPlatform() = default; -CHIP_ERROR DiscoveryImplPlatform::Init() +CHIP_ERROR DiscoveryImplPlatform::InitImpl() { - if (!mMdnsInitialized) - { - ReturnErrorOnFailure(ChipMdnsInit(HandleMdnsInit, HandleMdnsError, this)); - mCommissionInstanceName = GetRandU64(); - mMdnsInitialized = true; - } + ReturnErrorCodeIf(mMdnsInitialized, CHIP_NO_ERROR); + ReturnErrorOnFailure(ChipMdnsInit(HandleMdnsInit, HandleMdnsError, this)); + mCommissionInstanceName = GetRandU64(); + mMdnsInitialized = true; return CHIP_NO_ERROR; } -CHIP_ERROR DiscoveryImplPlatform::Start(Inet::InetLayer * inetLayer, uint16_t port) +void DiscoveryImplPlatform::Shutdown() { - ReturnErrorOnFailure(Init()); - - CHIP_ERROR error = ChipMdnsStopPublish(); - - if (error != CHIP_NO_ERROR) - { - ChipLogError(Discovery, "Failed to initialize platform mdns: %s", ErrorStr(error)); - } - - return error; + VerifyOrReturn(mMdnsInitialized); + ChipMdnsShutdown(); } void DiscoveryImplPlatform::HandleMdnsInit(void * context, CHIP_ERROR initError) @@ -101,6 +91,7 @@ void DiscoveryImplPlatform::HandleMdnsError(void * context, CHIP_ERROR error) { publisher->Advertise(publisher->mCommissionerAdvertisingParams); } + publisher->FinalizeServiceUpdate(); } else { @@ -441,28 +432,25 @@ CHIP_ERROR DiscoveryImplPlatform::Advertise(const OperationalAdvertisingParamete return error; } -CHIP_ERROR DiscoveryImplPlatform::StopPublishDevice() +CHIP_ERROR DiscoveryImplPlatform::RemoveServices() { - CHIP_ERROR error = ChipMdnsStopPublish(); + ReturnErrorOnFailure(ChipMdnsRemoveServices()); - if (error == CHIP_NO_ERROR) - { - mIsOperationalPublishing = false; - mIsCommissionableNodePublishing = false; - mIsCommissionerPublishing = false; - } - return error; + mIsOperationalPublishing = false; + mIsCommissionableNodePublishing = false; + mIsCommissionerPublishing = false; + + return CHIP_NO_ERROR; } -CHIP_ERROR DiscoveryImplPlatform::SetResolverDelegate(ResolverDelegate * delegate) +CHIP_ERROR DiscoveryImplPlatform::FinalizeServiceUpdate() { - mResolverDelegate = delegate; - return CHIP_NO_ERROR; + return ChipMdnsFinalizeServiceUpdate(); } CHIP_ERROR DiscoveryImplPlatform::ResolveNodeId(const PeerId & peerId, Inet::IPAddressType type) { - ReturnErrorOnFailure(Init()); + ReturnErrorOnFailure(InitImpl()); #if CHIP_CONFIG_MDNS_CACHE_SIZE > 0 Inet::IPAddress addr; @@ -541,7 +529,7 @@ void DiscoveryImplPlatform::HandleNodeResolve(void * context, MdnsService * resu CHIP_ERROR DiscoveryImplPlatform::FindCommissionableNodes(DiscoveryFilter filter) { - ReturnErrorOnFailure(Init()); + ReturnErrorOnFailure(InitImpl()); char serviceName[kMaxCommisisonableServiceNameSize]; ReturnErrorOnFailure(MakeServiceTypeName(serviceName, sizeof(serviceName), filter, DiscoveryType::kCommissionableNode)); @@ -551,7 +539,7 @@ CHIP_ERROR DiscoveryImplPlatform::FindCommissionableNodes(DiscoveryFilter filter CHIP_ERROR DiscoveryImplPlatform::FindCommissioners(DiscoveryFilter filter) { - ReturnErrorOnFailure(Init()); + ReturnErrorOnFailure(InitImpl()); char serviceName[kMaxCommisisonerServiceNameSize]; ReturnErrorOnFailure(MakeServiceTypeName(serviceName, sizeof(serviceName), filter, DiscoveryType::kCommissionerNode)); diff --git a/src/lib/mdns/Discovery_ImplPlatform.h b/src/lib/mdns/Discovery_ImplPlatform.h index 0db90a695a1c62..39c28ff14f3889 100644 --- a/src/lib/mdns/Discovery_ImplPlatform.h +++ b/src/lib/mdns/Discovery_ImplPlatform.h @@ -36,35 +36,21 @@ namespace Mdns { class DiscoveryImplPlatform : public ServiceAdvertiser, public Resolver { public: - CHIP_ERROR Init(); + // Members that implement both ServiceAdveriser and Resolver interfaces. + CHIP_ERROR Init(Inet::InetLayer *) override { return InitImpl(); } + void Shutdown() override; - /// Starts the service advertiser if not yet started. Otherwise, removes all existing services. - CHIP_ERROR Start(Inet::InetLayer * inetLayer, uint16_t port) override; - - /// Starts the service resolver if not yet started - CHIP_ERROR StartResolver(Inet::InetLayer * inetLayer, uint16_t port) override { return Init(); } - void ShutdownResolver() override { ChipMdnsShutdown(); } - - /// Advertises the CHIP node as an operational node + // Members that implement ServiceAdvertiser interface. + CHIP_ERROR RemoveServices() override; CHIP_ERROR Advertise(const OperationalAdvertisingParameters & params) override; - - /// Advertises the CHIP node as a commissioner/commissionable node CHIP_ERROR Advertise(const CommissionAdvertisingParameters & params) override; - - /// This function stops publishing the device on mDNS. - CHIP_ERROR StopPublishDevice() override; - - /// Returns DNS-SD instance name formatted as hex string + CHIP_ERROR FinalizeServiceUpdate() override; CHIP_ERROR GetCommissionableInstanceName(char * instanceName, size_t maxLength) override; - /// Registers a resolver delegate if none has been registered before - CHIP_ERROR SetResolverDelegate(ResolverDelegate * delegate) override; - - /// Requests resolution of a node ID to its address + // Members that implement Resolver interface. + void SetResolverDelegate(ResolverDelegate * delegate) override { mResolverDelegate = delegate; } CHIP_ERROR ResolveNodeId(const PeerId & peerId, Inet::IPAddressType type) override; - CHIP_ERROR FindCommissionableNodes(DiscoveryFilter filter = DiscoveryFilter()) override; - CHIP_ERROR FindCommissioners(DiscoveryFilter filter = DiscoveryFilter()) override; static DiscoveryImplPlatform & GetInstance(); @@ -75,6 +61,7 @@ class DiscoveryImplPlatform : public ServiceAdvertiser, public Resolver DiscoveryImplPlatform(const DiscoveryImplPlatform &) = delete; DiscoveryImplPlatform & operator=(const DiscoveryImplPlatform &) = delete; + CHIP_ERROR InitImpl(); CHIP_ERROR PublishUnprovisionedDevice(chip::Inet::IPAddressType addressType, chip::Inet::InterfaceId interface); CHIP_ERROR PublishProvisionedDevice(chip::Inet::IPAddressType addressType, chip::Inet::InterfaceId interface); diff --git a/src/lib/mdns/Resolver.h b/src/lib/mdns/Resolver.h index 944f80bf4767c5..c9b31f687d9784 100644 --- a/src/lib/mdns/Resolver.h +++ b/src/lib/mdns/Resolver.h @@ -244,32 +244,61 @@ class ResolverDelegate virtual void OnNodeDiscoveryComplete(const DiscoveredNodeData & nodeData) = 0; }; -/// Interface for resolving CHIP services +/** + * Interface for resolving CHIP DNS-SD services + */ class Resolver { public: virtual ~Resolver() {} - /// Ensures that the resolver is started. - /// Must be called before any ResolveNodeId calls. - /// - /// Unsual name to allow base MDNS classes to implement both Advertiser and Resolver interfaces. - virtual CHIP_ERROR StartResolver(chip::Inet::InetLayer * inetLayer, uint16_t port) = 0; - virtual void ShutdownResolver() = 0; + /** + * Initializes the resolver. + * + * The method must be called before other methods of this class. + * If the resolver has already been initialized, the method exits immediately with no error. + */ + virtual CHIP_ERROR Init(chip::Inet::InetLayer * inetLayer) = 0; + + /** + * Shuts down the resolver if it has been initialized before. + */ + virtual void Shutdown() = 0; - /// Registers a resolver delegate if none has been registered before - virtual CHIP_ERROR SetResolverDelegate(ResolverDelegate * delegate) = 0; + /** + * Registers a resolver delegate. If nullptr is passed, the previously registered delegate + * is unregistered. + */ + virtual void SetResolverDelegate(ResolverDelegate * delegate) = 0; - /// Requests resolution of a node ID to its address + /** + * Requests resolution of the given operational node service. + * + * When the operation succeeds or fails, and a resolver delegate has been registered, + * the result of the operation is passed to the delegate's `OnNodeIdResolved` or + * `OnNodeIdResolutionFailed` method, respectively. + */ virtual CHIP_ERROR ResolveNodeId(const PeerId & peerId, Inet::IPAddressType type) = 0; - // Finds all nodes with the given filter that are currently in commissioning mode. + /** + * Finds all commissionable nodes matching the given filter. + * + * Whenever a new matching node is found and a resolver delegate has been registered, + * the node information is passed to the delegate's `OnNodeDiscoveryComplete` method. + */ virtual CHIP_ERROR FindCommissionableNodes(DiscoveryFilter filter = DiscoveryFilter()) = 0; - // Finds all nodes with the given filter that are currently acting as Commissioners. + /** + * Finds all commissioner nodes matching the given filter. + * + * Whenever a new matching node is found and a resolver delegate has been registered, + * the node information is passed to the delegate's `OnNodeDiscoveryComplete` method. + */ virtual CHIP_ERROR FindCommissioners(DiscoveryFilter filter = DiscoveryFilter()) = 0; - /// Provides the system-wide implementation of the service resolver + /** + * Provides the system-wide implementation of the service resolver + */ static Resolver & Instance(); }; diff --git a/src/lib/mdns/Resolver_ImplMinimalMdns.cpp b/src/lib/mdns/Resolver_ImplMinimalMdns.cpp index 1b5032575af4de..a3374ed7f034b2 100644 --- a/src/lib/mdns/Resolver_ImplMinimalMdns.cpp +++ b/src/lib/mdns/Resolver_ImplMinimalMdns.cpp @@ -341,9 +341,9 @@ class MinMdnsResolver : public Resolver, public MdnsPacketDelegate void OnMdnsPacketData(const BytesRange & data, const chip::Inet::IPPacketInfo * info) override; ///// Resolver implementation - CHIP_ERROR StartResolver(chip::Inet::InetLayer * inetLayer, uint16_t port) override; - void ShutdownResolver() override; - CHIP_ERROR SetResolverDelegate(ResolverDelegate * delegate) override; + CHIP_ERROR Init(chip::Inet::InetLayer * inetLayer) override; + void Shutdown() override; + void SetResolverDelegate(ResolverDelegate * delegate) override { mDelegate = delegate; } CHIP_ERROR ResolveNodeId(const PeerId & peerId, Inet::IPAddressType type) override; CHIP_ERROR FindCommissionableNodes(DiscoveryFilter filter = DiscoveryFilter()) override; CHIP_ERROR FindCommissioners(DiscoveryFilter filter = DiscoveryFilter()) override; @@ -398,7 +398,7 @@ void MinMdnsResolver::OnMdnsPacketData(const BytesRange & data, const chip::Inet } } -CHIP_ERROR MinMdnsResolver::StartResolver(chip::Inet::InetLayer * inetLayer, uint16_t port) +CHIP_ERROR MinMdnsResolver::Init(chip::Inet::InetLayer * inetLayer) { /// Note: we do not double-check the port as we assume the APP will always use /// the same inetLayer and port for mDNS. @@ -409,20 +409,14 @@ CHIP_ERROR MinMdnsResolver::StartResolver(chip::Inet::InetLayer * inetLayer, uin mSystemLayer = inetLayer->SystemLayer(); - return GlobalMinimalMdnsServer::Instance().StartServer(inetLayer, port); + return GlobalMinimalMdnsServer::Instance().StartServer(inetLayer, kMdnsPort); } -void MinMdnsResolver::ShutdownResolver() +void MinMdnsResolver::Shutdown() { GlobalMinimalMdnsServer::Instance().ShutdownServer(); } -CHIP_ERROR MinMdnsResolver::SetResolverDelegate(ResolverDelegate * delegate) -{ - mDelegate = delegate; - return CHIP_NO_ERROR; -} - CHIP_ERROR MinMdnsResolver::SendQuery(mdns::Minimal::FullQName qname, mdns::Minimal::QType type) { System::PacketBufferHandle buffer = System::PacketBufferHandle::New(kMdnsMaxPacketSize); diff --git a/src/lib/mdns/Resolver_ImplNone.cpp b/src/lib/mdns/Resolver_ImplNone.cpp index bd0f097622327d..613d93b0d3287c 100644 --- a/src/lib/mdns/Resolver_ImplNone.cpp +++ b/src/lib/mdns/Resolver_ImplNone.cpp @@ -26,10 +26,9 @@ namespace { class NoneResolver : public Resolver { public: - CHIP_ERROR SetResolverDelegate(ResolverDelegate *) override { return CHIP_NO_ERROR; } - - CHIP_ERROR StartResolver(chip::Inet::InetLayer * inetLayer, uint16_t port) override { return CHIP_NO_ERROR; } - void ShutdownResolver() override {} + CHIP_ERROR Init(chip::Inet::InetLayer *) override { return CHIP_NO_ERROR; } + void Shutdown() override {} + void SetResolverDelegate(ResolverDelegate *) override {} CHIP_ERROR ResolveNodeId(const PeerId & peerId, Inet::IPAddressType type) override { diff --git a/src/lib/mdns/minimal/tests/TestAdvertiser.cpp b/src/lib/mdns/minimal/tests/TestAdvertiser.cpp index 25953cbcd266bc..a6fcdbd32e074b 100644 --- a/src/lib/mdns/minimal/tests/TestAdvertiser.cpp +++ b/src/lib/mdns/minimal/tests/TestAdvertiser.cpp @@ -212,7 +212,8 @@ CHIP_ERROR SendQuery(FullQName qname) void OperationalAdverts(nlTestSuite * inSuite, void * inContext) { auto & mdnsAdvertiser = chip::Mdns::ServiceAdvertiser::Instance(); - mdnsAdvertiser.StopPublishDevice(); + NL_TEST_ASSERT(inSuite, mdnsAdvertiser.RemoveServices() == CHIP_NO_ERROR); + auto & server = reinterpret_cast(GlobalMinimalMdnsServer::Server()); server.SetTestSuite(inSuite); server.Reset(); @@ -220,6 +221,7 @@ void OperationalAdverts(nlTestSuite * inSuite, void * inContext) // Start a single operational advertiser ChipLogProgress(Discovery, "Testing single operational advertiser"); NL_TEST_ASSERT(inSuite, mdnsAdvertiser.Advertise(operationalParams1) == CHIP_NO_ERROR); + NL_TEST_ASSERT(inSuite, mdnsAdvertiser.FinalizeServiceUpdate() == CHIP_NO_ERROR); // Test for PTR response to _services request. ChipLogProgress(Discovery, "Checking response to _services._dns-sd._udp.local"); @@ -256,6 +258,7 @@ void OperationalAdverts(nlTestSuite * inSuite, void * inContext) // If we try to re-advertise with the same operational parameters, we should not get duplicates NL_TEST_ASSERT(inSuite, mdnsAdvertiser.Advertise(operationalParams1) == CHIP_NO_ERROR); + NL_TEST_ASSERT(inSuite, mdnsAdvertiser.FinalizeServiceUpdate() == CHIP_NO_ERROR); ChipLogProgress(Discovery, "Testing single operational advertiser with Advertise called twice"); // We should get a single PTR back for _services ChipLogProgress(Discovery, "Checking response to _services._dns-sd._udp.local"); @@ -281,6 +284,7 @@ void OperationalAdverts(nlTestSuite * inSuite, void * inContext) server.Reset(); // Mac is the same, peer id is different NL_TEST_ASSERT(inSuite, mdnsAdvertiser.Advertise(operationalParams2) == CHIP_NO_ERROR); + NL_TEST_ASSERT(inSuite, mdnsAdvertiser.FinalizeServiceUpdate() == CHIP_NO_ERROR); // For now, we'll get back two copies of the PTR. Not sure if that's totally correct, but for now, that's expected. ChipLogProgress(Discovery, "Checking response to _services._dns-sd._udp.local"); @@ -336,7 +340,8 @@ void OperationalAdverts(nlTestSuite * inSuite, void * inContext) void CommissionableAdverts(nlTestSuite * inSuite, void * inContext) { auto & mdnsAdvertiser = chip::Mdns::ServiceAdvertiser::Instance(); - mdnsAdvertiser.StopPublishDevice(); + NL_TEST_ASSERT(inSuite, mdnsAdvertiser.RemoveServices() == CHIP_NO_ERROR); + auto & server = reinterpret_cast(GlobalMinimalMdnsServer::Server()); server.SetTestSuite(inSuite); server.Reset(); @@ -345,6 +350,7 @@ void CommissionableAdverts(nlTestSuite * inSuite, void * inContext) ChipLogProgress(Discovery, "Testing commissionable advertiser"); // Start very basic - only the mandatory values (short and long discriminator and commissioning modes) NL_TEST_ASSERT(inSuite, mdnsAdvertiser.Advertise(commissionableNodeParamsSmall) == CHIP_NO_ERROR); + NL_TEST_ASSERT(inSuite, mdnsAdvertiser.FinalizeServiceUpdate() == CHIP_NO_ERROR); // Test for PTR response to _services request. ChipLogProgress(Discovery, "Checking response to _services._dns-sd._udp.local for small parameters"); @@ -383,6 +389,7 @@ void CommissionableAdverts(nlTestSuite * inSuite, void * inContext) // Add more parameters, check that the subtypes and TXT values get set correctly. // Also check that we get proper values when the discriminators are small (no leading 0's) NL_TEST_ASSERT(inSuite, mdnsAdvertiser.Advertise(commissionableNodeParamsLargeBasic) == CHIP_NO_ERROR); + NL_TEST_ASSERT(inSuite, mdnsAdvertiser.FinalizeServiceUpdate() == CHIP_NO_ERROR); ChipLogProgress(Discovery, "Checking response to _services._dns-sd._udp.local for large basic parameters"); server.Reset(); server.AddExpectedRecord(&ptrCommissionableNodeService); @@ -414,6 +421,7 @@ void CommissionableAdverts(nlTestSuite * inSuite, void * inContext) NL_TEST_ASSERT(inSuite, server.GetHeaderFound()); NL_TEST_ASSERT(inSuite, mdnsAdvertiser.Advertise(commissionableNodeParamsLargeEnhanced) == CHIP_NO_ERROR); + NL_TEST_ASSERT(inSuite, mdnsAdvertiser.FinalizeServiceUpdate() == CHIP_NO_ERROR); ChipLogProgress(Discovery, "Checking response to _services._dns-sd._udp.local for large enhanced parameters"); server.Reset(); server.AddExpectedRecord(&ptrCommissionableNodeService); @@ -448,7 +456,8 @@ void CommissionableAdverts(nlTestSuite * inSuite, void * inContext) void CommissionableAndOperationalAdverts(nlTestSuite * inSuite, void * inContext) { auto & mdnsAdvertiser = chip::Mdns::ServiceAdvertiser::Instance(); - mdnsAdvertiser.StopPublishDevice(); + NL_TEST_ASSERT(inSuite, mdnsAdvertiser.RemoveServices() == CHIP_NO_ERROR); + auto & server = reinterpret_cast(GlobalMinimalMdnsServer::Server()); server.SetTestSuite(inSuite); server.Reset(); @@ -457,6 +466,7 @@ void CommissionableAndOperationalAdverts(nlTestSuite * inSuite, void * inContext NL_TEST_ASSERT(inSuite, mdnsAdvertiser.Advertise(operationalParams1) == CHIP_NO_ERROR); NL_TEST_ASSERT(inSuite, mdnsAdvertiser.Advertise(operationalParams2) == CHIP_NO_ERROR); NL_TEST_ASSERT(inSuite, mdnsAdvertiser.Advertise(commissionableNodeParamsLargeEnhanced) == CHIP_NO_ERROR); + NL_TEST_ASSERT(inSuite, mdnsAdvertiser.FinalizeServiceUpdate() == CHIP_NO_ERROR); // Services listing should have two operational ptrs, the base commissionable node ptr and the various _sub ptrs ChipLogProgress(Discovery, "Checking response to _services._dns-sd._udp.local"); @@ -543,7 +553,7 @@ int TestAdvertiser(void) CheckOnlyServer server(&theSuite); test::ServerSwapper swapper(&server); auto & mdnsAdvertiser = chip::Mdns::ServiceAdvertiser::Instance(); - mdnsAdvertiser.Start(nullptr, CHIP_PORT); + mdnsAdvertiser.Init(nullptr); nlTestRunner(&theSuite, &server); return nlTestRunnerStats(&theSuite); } diff --git a/src/lib/mdns/platform/Mdns.h b/src/lib/mdns/platform/Mdns.h index 93b4a0b501f82a..2b074dfc11c53a 100644 --- a/src/lib/mdns/platform/Mdns.h +++ b/src/lib/mdns/platform/Mdns.h @@ -129,7 +129,18 @@ CHIP_ERROR ChipMdnsInit(MdnsAsyncReturnCallback initCallback, MdnsAsyncReturnCal CHIP_ERROR ChipMdnsShutdown(); /** - * This function publishes an service via mDNS. + * Removes or marks all services being advertised for removal. + * + * Depending on the implementation, the function may either stop advertising existing services + * immediately, or mark them for removal upon the subsequent `ChipMdnsFinalizeServiceUpdate` call. + * + * @retval CHIP_NO_ERROR The removal succeeds. + * @retval Error code The removal fails. + */ +CHIP_ERROR ChipMdnsRemoveServices(); + +/** + * This function publishes a service via DNS-SD. * * Calling the function again with the same service name, type, protocol, * interface and port but different text will update the text published. @@ -145,25 +156,17 @@ CHIP_ERROR ChipMdnsShutdown(); CHIP_ERROR ChipMdnsPublishService(const MdnsService * service); /** - * This function stops publishing all services via mDNS. - * - * @retval CHIP_NO_ERROR The publish stops successfully. - * @retval Error code Stopping the publish fails. + * Finalizes updating advertised services. * - */ -CHIP_ERROR ChipMdnsStopPublish(); - -/** - * This function stops publishing a specific service via mDNS. - * - * @param[in] service The service entry. - * - * @retval CHIP_NO_ERROR The service stop succeeds. - * @retval CHIP_ERROR_INVALID_ARGUMENT The service is nullptr. - * @retval Error code The service stop fails. + * This function can be used by some implementations to apply changes made with the + * `ChipMdnsRemoveServices` and `ChipMdnsPublishService` functions in case they could + * not be applied immediately (like in case of, but not limited to, implementations + * using SRP or requiring asynchronous interactions with a DNS-SD implementation). * + * @retval CHIP_NO_ERROR The service update finalization succeeds. + * @retval Error code The service update finalization fails. */ -CHIP_ERROR ChipMdnsStopPublishService(const MdnsService * service); +CHIP_ERROR ChipMdnsFinalizeServiceUpdate(); /** * This function browses the services published by mdns diff --git a/src/lib/mdns/platform/tests/TestPlatform.cpp b/src/lib/mdns/platform/tests/TestPlatform.cpp index 58b585f0dd01fd..6f95be2db8ba3a 100644 --- a/src/lib/mdns/platform/tests/TestPlatform.cpp +++ b/src/lib/mdns/platform/tests/TestPlatform.cpp @@ -20,6 +20,7 @@ #include #include #include +#include #include #include @@ -156,7 +157,8 @@ void TestStub(nlTestSuite * inSuite, void * inContext) // without an expected event. ChipLogError(Discovery, "Test platform returns error correctly"); DiscoveryImplPlatform & mdnsPlatform = DiscoveryImplPlatform::GetInstance(); - NL_TEST_ASSERT(inSuite, mdnsPlatform.Init() == CHIP_NO_ERROR); + NL_TEST_ASSERT(inSuite, mdnsPlatform.Init(&DeviceLayer::InetLayer) == CHIP_NO_ERROR); + NL_TEST_ASSERT(inSuite, mdnsPlatform.RemoveServices() == CHIP_NO_ERROR); OperationalAdvertisingParameters params; NL_TEST_ASSERT(inSuite, mdnsPlatform.Advertise(params) == CHIP_ERROR_UNEXPECTED_EVENT); } @@ -166,7 +168,8 @@ void TestOperational(nlTestSuite * inSuite, void * inContext) ChipLogError(Discovery, "Test operational"); test::Reset(); DiscoveryImplPlatform & mdnsPlatform = DiscoveryImplPlatform::GetInstance(); - NL_TEST_ASSERT(inSuite, mdnsPlatform.Init() == CHIP_NO_ERROR); + NL_TEST_ASSERT(inSuite, mdnsPlatform.Init(&DeviceLayer::InetLayer) == CHIP_NO_ERROR); + NL_TEST_ASSERT(inSuite, mdnsPlatform.RemoveServices() == CHIP_NO_ERROR); operationalCall1.callType = test::CallType::kStart; NL_TEST_ASSERT(inSuite, test::AddExpectedCall(operationalCall1) == CHIP_NO_ERROR); @@ -177,6 +180,8 @@ void TestOperational(nlTestSuite * inSuite, void * inContext) operationalCall2.callType = test::CallType::kStart; NL_TEST_ASSERT(inSuite, test::AddExpectedCall(operationalCall2) == CHIP_NO_ERROR); NL_TEST_ASSERT(inSuite, mdnsPlatform.Advertise(operationalParams2) == CHIP_NO_ERROR); + + NL_TEST_ASSERT(inSuite, mdnsPlatform.FinalizeServiceUpdate() == CHIP_NO_ERROR); } void TestCommissionableNode(nlTestSuite * inSuite, void * inContext) @@ -184,7 +189,8 @@ void TestCommissionableNode(nlTestSuite * inSuite, void * inContext) ChipLogError(Discovery, "Test commissionable"); test::Reset(); DiscoveryImplPlatform & mdnsPlatform = DiscoveryImplPlatform::GetInstance(); - NL_TEST_ASSERT(inSuite, mdnsPlatform.Init() == CHIP_NO_ERROR); + NL_TEST_ASSERT(inSuite, mdnsPlatform.Init(&DeviceLayer::InetLayer) == CHIP_NO_ERROR); + NL_TEST_ASSERT(inSuite, mdnsPlatform.RemoveServices() == CHIP_NO_ERROR); commissionableSmall.callType = test::CallType::kStart; NL_TEST_ASSERT(inSuite, @@ -210,6 +216,8 @@ void TestCommissionableNode(nlTestSuite * inSuite, void * inContext) sizeof(commissionableLargeEnhanced.instanceName)) == CHIP_NO_ERROR); NL_TEST_ASSERT(inSuite, test::AddExpectedCall(commissionableLargeEnhanced) == CHIP_NO_ERROR); NL_TEST_ASSERT(inSuite, mdnsPlatform.Advertise(commissionableNodeParamsLargeEnhanced) == CHIP_NO_ERROR); + + NL_TEST_ASSERT(inSuite, mdnsPlatform.FinalizeServiceUpdate() == CHIP_NO_ERROR); } const nlTest sTests[] = { diff --git a/src/lib/shell/commands/Dns.cpp b/src/lib/shell/commands/Dns.cpp index 5172e153787cce..601b6d226da19b 100644 --- a/src/lib/shell/commands/Dns.cpp +++ b/src/lib/shell/commands/Dns.cpp @@ -218,7 +218,7 @@ CHIP_ERROR DnsHandler(int argc, char ** argv) return CHIP_NO_ERROR; } - Mdns::Resolver::Instance().StartResolver(&DeviceLayer::InetLayer, Mdns::kMdnsPort); + Mdns::Resolver::Instance().Init(&DeviceLayer::InetLayer); Mdns::Resolver::Instance().SetResolverDelegate(&sDnsShellResolverDelegate); return sShellDnsSubcommands.ExecCommand(argc, argv); diff --git a/src/platform/Darwin/MdnsImpl.cpp b/src/platform/Darwin/MdnsImpl.cpp index 58b95dbba694fd..9e413b555fab8c 100644 --- a/src/platform/Darwin/MdnsImpl.cpp +++ b/src/platform/Darwin/MdnsImpl.cpp @@ -525,7 +525,7 @@ CHIP_ERROR ChipMdnsPublishService(const MdnsService * service) return Register(interfaceId, regtype.c_str(), service->mName, service->mPort, &record); } -CHIP_ERROR ChipMdnsStopPublish() +CHIP_ERROR ChipMdnsRemoveServices() { GenericContext * sdCtx = nullptr; if (CHIP_ERROR_KEY_NOT_FOUND == MdnsContexts::GetInstance().Get(ContextType::Register, &sdCtx)) @@ -536,9 +536,9 @@ CHIP_ERROR ChipMdnsStopPublish() return MdnsContexts::GetInstance().Removes(ContextType::Register); } -CHIP_ERROR ChipMdnsStopPublishService(const MdnsService * service) +CHIP_ERROR ChipMdnsFinalizeServiceUpdate() { - return CHIP_ERROR_NOT_IMPLEMENTED; + return CHIP_NO_ERROR; } CHIP_ERROR ChipMdnsBrowse(const char * type, MdnsServiceProtocol protocol, chip::Inet::IPAddressType addressType, diff --git a/src/platform/ESP32/MdnsImpl.cpp b/src/platform/ESP32/MdnsImpl.cpp index b4d6ba67bf3b7d..4c512aab050b85 100644 --- a/src/platform/ESP32/MdnsImpl.cpp +++ b/src/platform/ESP32/MdnsImpl.cpp @@ -108,14 +108,14 @@ CHIP_ERROR ChipMdnsPublishService(const MdnsService * service) return error; } -CHIP_ERROR ChipMdnsStopPublish() +CHIP_ERROR ChipMdnsRemoveServices() { return mdns_service_remove_all() == ESP_OK ? CHIP_NO_ERROR : CHIP_ERROR_INTERNAL; } -CHIP_ERROR ChipMdnsStopPublishService(const MdnsService * service) +CHIP_ERROR ChipMdnsFinalizeServiceUpdate() { - return CHIP_ERROR_NOT_IMPLEMENTED; + return CHIP_NO_ERROR; } CHIP_ERROR ChipMdnsBrowse(const char * /*type*/, MdnsServiceProtocol /*protocol*/, chip::Inet::IPAddressType addressType, diff --git a/src/platform/Linux/MdnsImpl.cpp b/src/platform/Linux/MdnsImpl.cpp index 145623bc665598..c05a2d269b9bb7 100644 --- a/src/platform/Linux/MdnsImpl.cpp +++ b/src/platform/Linux/MdnsImpl.cpp @@ -793,14 +793,14 @@ CHIP_ERROR ChipMdnsPublishService(const MdnsService * service) return MdnsAvahi::GetInstance().PublishService(*service); } -CHIP_ERROR ChipMdnsStopPublish() +CHIP_ERROR ChipMdnsRemoveServices() { return MdnsAvahi::GetInstance().StopPublish(); } -CHIP_ERROR ChipMdnsStopPublishService(const MdnsService * service) +CHIP_ERROR ChipMdnsFinalizeServiceUpdate() { - return CHIP_ERROR_NOT_IMPLEMENTED; + return CHIP_NO_ERROR; } CHIP_ERROR ChipMdnsBrowse(const char * type, MdnsServiceProtocol protocol, chip::Inet::IPAddressType addressType, diff --git a/src/platform/OpenThread/GenericThreadStackManagerImpl_OpenThread.cpp b/src/platform/OpenThread/GenericThreadStackManagerImpl_OpenThread.cpp index 94e7885e5cb0ab..46372f0e015609 100644 --- a/src/platform/OpenThread/GenericThreadStackManagerImpl_OpenThread.cpp +++ b/src/platform/OpenThread/GenericThreadStackManagerImpl_OpenThread.cpp @@ -1739,7 +1739,24 @@ CHIP_ERROR GenericThreadStackManagerImpl_OpenThread::_RemoveSrpServic } template -CHIP_ERROR GenericThreadStackManagerImpl_OpenThread::_RemoveAllSrpServices() +CHIP_ERROR GenericThreadStackManagerImpl_OpenThread::_InvalidateAllSrpServices() +{ + Impl()->LockThreadStack(); + + for (typename SrpClient::Service & service : mSrpClient.mServices) + { + if (service.IsUsed()) + { + service.mIsInvalid = true; + } + } + + Impl()->UnlockThreadStack(); + return CHIP_NO_ERROR; +} + +template +CHIP_ERROR GenericThreadStackManagerImpl_OpenThread::_RemoveInvalidSrpServices() { CHIP_ERROR error = CHIP_NO_ERROR; @@ -1747,11 +1764,12 @@ CHIP_ERROR GenericThreadStackManagerImpl_OpenThread::_RemoveAllSrpSer for (typename SrpClient::Service & service : mSrpClient.mServices) { - if (!service.IsUsed()) - continue; - - error = MapOpenThreadError(otSrpClientRemoveService(mOTInst, &service.mService)); - SuccessOrExit(error); + if (service.IsUsed() && service.mIsInvalid) + { + ChipLogProgress(DeviceLayer, "removing srp service: %s.%s", service.mService.mInstanceName, service.mService.mName); + error = MapOpenThreadError(otSrpClientRemoveService(mOTInst, &service.mService)); + SuccessOrExit(error); + } } exit: diff --git a/src/platform/OpenThread/GenericThreadStackManagerImpl_OpenThread.h b/src/platform/OpenThread/GenericThreadStackManagerImpl_OpenThread.h index 62755b7c09a2d4..8ea2f432bd6ac2 100644 --- a/src/platform/OpenThread/GenericThreadStackManagerImpl_OpenThread.h +++ b/src/platform/OpenThread/GenericThreadStackManagerImpl_OpenThread.h @@ -104,7 +104,9 @@ class GenericThreadStackManagerImpl_OpenThread const Span & aSubTypes, const Span & aTxtEntries, uint32_t aLeaseInterval, uint32_t aKeyLeaseInterval); CHIP_ERROR _RemoveSrpService(const char * aInstanceName, const char * aName); - CHIP_ERROR _RemoveAllSrpServices(); + CHIP_ERROR _InvalidateAllSrpServices(); + CHIP_ERROR _RemoveInvalidSrpServices(); + CHIP_ERROR _SetupSrpHost(const char * aHostName); #if CHIP_DEVICE_CONFIG_ENABLE_THREAD_DNS_CLIENT CHIP_ERROR _DnsBrowse(const char * aServiceName, DnsBrowseCallback aCallback, void * aContext); @@ -161,6 +163,7 @@ class GenericThreadStackManagerImpl_OpenThread struct Service { otSrpClientService mService; + bool mIsInvalid; uint8_t mServiceBuffer[kServiceBufferSize]; #if OPENTHREAD_API_VERSION >= 132 const char * mSubTypes[kSubTypeMaxNumber + 1]; // extra entry for null terminator diff --git a/src/platform/OpenThread/MdnsImpl.cpp b/src/platform/OpenThread/MdnsImpl.cpp index 80ea8c6457a9db..2c5867d058a120 100644 --- a/src/platform/OpenThread/MdnsImpl.cpp +++ b/src/platform/OpenThread/MdnsImpl.cpp @@ -62,25 +62,20 @@ CHIP_ERROR ChipMdnsPublishService(const MdnsService * service) #endif // CHIP_DEVICE_CONFIG_ENABLE_THREAD_SRP_CLIENT } -CHIP_ERROR ChipMdnsStopPublish() +CHIP_ERROR ChipMdnsRemoveServices() { #if CHIP_DEVICE_CONFIG_ENABLE_THREAD_SRP_CLIENT - return ThreadStackMgr().RemoveAllSrpServices(); + ThreadStackMgr().InvalidateAllSrpServices(); + return CHIP_NO_ERROR; #else return CHIP_ERROR_NOT_IMPLEMENTED; #endif // CHIP_DEVICE_CONFIG_ENABLE_THREAD_SRP_CLIENT } -CHIP_ERROR ChipMdnsStopPublishService(const MdnsService * service) +CHIP_ERROR ChipMdnsFinalizeServiceUpdate() { #if CHIP_DEVICE_CONFIG_ENABLE_THREAD_SRP_CLIENT - if (service == nullptr) - return CHIP_ERROR_INVALID_ARGUMENT; - - char serviceType[chip::Mdns::kMdnsTypeAndProtocolMaxSize + 1]; - snprintf(serviceType, sizeof(serviceType), "%s.%s", service->mType, GetProtocolString(service->mProtocol)); - - return ThreadStackMgr().RemoveSrpService(service->mName, serviceType); + return ThreadStackMgr().RemoveInvalidSrpServices(); #else return CHIP_ERROR_NOT_IMPLEMENTED; #endif // CHIP_DEVICE_CONFIG_ENABLE_THREAD_SRP_CLIENT diff --git a/src/platform/Tizen/MdnsImpl.cpp b/src/platform/Tizen/MdnsImpl.cpp index 174307855434db..e4ae1fd4e1b8bc 100644 --- a/src/platform/Tizen/MdnsImpl.cpp +++ b/src/platform/Tizen/MdnsImpl.cpp @@ -46,7 +46,12 @@ CHIP_ERROR ChipMdnsPublishService(const MdnsService * service) return CHIP_ERROR_NOT_IMPLEMENTED; } -CHIP_ERROR ChipMdnsStopPublish() +CHIP_ERROR ChipMdnsRemoveServices() +{ + return CHIP_ERROR_NOT_IMPLEMENTED; +} + +CHIP_ERROR ChipMdnsFinalizeServiceUpdate() { return CHIP_ERROR_NOT_IMPLEMENTED; } diff --git a/src/platform/android/MdnsImpl.cpp b/src/platform/android/MdnsImpl.cpp index c67e9168066f0f..7520685e922eb3 100644 --- a/src/platform/android/MdnsImpl.cpp +++ b/src/platform/android/MdnsImpl.cpp @@ -57,17 +57,17 @@ CHIP_ERROR ChipMdnsShutdown() return CHIP_NO_ERROR; } -CHIP_ERROR ChipMdnsPublishService(const MdnsService * service) +CHIP_ERROR ChipMdnsRemoveServices() { return CHIP_ERROR_NOT_IMPLEMENTED; } -CHIP_ERROR ChipMdnsStopPublish() +CHIP_ERROR ChipMdnsPublishService(const MdnsService * service) { - return CHIP_NO_ERROR; + return CHIP_ERROR_NOT_IMPLEMENTED; } -CHIP_ERROR ChipMdnsStopPublishService(const MdnsService * service) +CHIP_ERROR ChipMdnsFinalizeServiceUpdate() { return CHIP_ERROR_NOT_IMPLEMENTED; } diff --git a/src/platform/fake/MdnsImpl.cpp b/src/platform/fake/MdnsImpl.cpp index af2e2c96a6fc7f..0d5e1571763dd9 100644 --- a/src/platform/fake/MdnsImpl.cpp +++ b/src/platform/fake/MdnsImpl.cpp @@ -105,14 +105,14 @@ CHIP_ERROR ChipMdnsPublishService(const MdnsService * service) return test::CheckExpected(test::CallType::kStart, service); } -CHIP_ERROR ChipMdnsStopPublish() +CHIP_ERROR ChipMdnsRemoveServices() { - return CHIP_ERROR_NOT_IMPLEMENTED; + return CHIP_NO_ERROR; } -CHIP_ERROR ChipMdnsStopPublishService(const MdnsService * service) +CHIP_ERROR ChipMdnsFinalizeServiceUpdate() { - return test::CheckExpected(test::CallType::kStart, service); + return CHIP_NO_ERROR; } CHIP_ERROR ChipMdnsBrowse(const char * type, MdnsServiceProtocol protocol, chip::Inet::IPAddressType addressType,