From 182a8c8853d691e97d28205204b0704700a69df2 Mon Sep 17 00:00:00 2001 From: Kamil Kasperczyk <66371704+kkasperczyk-no@users.noreply.github.com> Date: Thu, 4 Apr 2024 07:21:54 +0200 Subject: [PATCH] [dnssd] Allow selecting DNS-SD implementation at runtime (#32829) Add Resolver::SetInstance() and ServiceAdvertiser::SetInstance() methods for dynamically changing the system-wide DNS-SD implementation used by Matter. Also, allow for building "minimal" and "platform" DNS-SD implementations together. Co-authored-by: Damian Krolik --- config/nrfconnect/chip-module/CMakeLists.txt | 8 ++-- src/lib/dnssd/Advertiser.cpp | 41 ++++++++++++++++++++ src/lib/dnssd/Advertiser.h | 21 +++++++++- src/lib/dnssd/Advertiser_ImplMinimalMdns.cpp | 6 ++- src/lib/dnssd/Advertiser_ImplNone.cpp | 6 ++- src/lib/dnssd/BUILD.gn | 26 +++++++++++-- src/lib/dnssd/Discovery_ImplPlatform.cpp | 8 +++- src/lib/dnssd/Resolver.cpp | 41 ++++++++++++++++++++ src/lib/dnssd/Resolver.h | 19 ++++++++- src/lib/dnssd/Resolver_ImplMinimalMdns.cpp | 6 ++- src/lib/dnssd/Resolver_ImplNone.cpp | 6 ++- src/platform/device.gni | 6 +++ src/platform/nrfconnect/BUILD.gn | 2 +- 13 files changed, 180 insertions(+), 16 deletions(-) create mode 100644 src/lib/dnssd/Advertiser.cpp create mode 100644 src/lib/dnssd/Resolver.cpp diff --git a/config/nrfconnect/chip-module/CMakeLists.txt b/config/nrfconnect/chip-module/CMakeLists.txt index d132d8249d76b1..98295406628cc1 100644 --- a/config/nrfconnect/chip-module/CMakeLists.txt +++ b/config/nrfconnect/chip-module/CMakeLists.txt @@ -140,6 +140,8 @@ matter_add_gn_arg_bool ("chip_system_config_provide_statistics" CONFIG_CHIP_ST matter_add_gn_arg_bool ("chip_enable_icd_server" CONFIG_CHIP_ENABLE_ICD_SUPPORT) matter_add_gn_arg_bool ("chip_enable_factory_data" CONFIG_CHIP_FACTORY_DATA) matter_add_gn_arg_bool ("chip_enable_read_client" CONFIG_CHIP_ENABLE_READ_CLIENT) +matter_add_gn_arg_bool ("chip_mdns_minimal" CONFIG_WIFI_NRF700X) +matter_add_gn_arg_bool ("chip_mdns_platform" CONFIG_NET_L2_OPENTHREAD) if (CONFIG_CHIP_ENABLE_ICD_SUPPORT) matter_add_gn_arg_bool ("chip_enable_icd_lit" CONFIG_CHIP_ICD_LIT_SUPPORT) @@ -157,10 +159,10 @@ if (CONFIG_CHIP_ROTATING_DEVICE_ID) matter_add_gn_arg_bool("chip_enable_additional_data_advertising" TRUE) endif() -if (CONFIG_NET_L2_OPENTHREAD) - matter_add_gn_arg_string("chip_mdns" "platform") -elseif(CONFIG_WIFI_NRF700X) +if(CONFIG_WIFI_NRF700X) matter_add_gn_arg_string("chip_mdns" "minimal") +elseif (CONFIG_NET_L2_OPENTHREAD) + matter_add_gn_arg_string("chip_mdns" "platform") else() matter_add_gn_arg_string("chip_mdns" "none") endif() diff --git a/src/lib/dnssd/Advertiser.cpp b/src/lib/dnssd/Advertiser.cpp new file mode 100644 index 00000000000000..1898d115ce10ce --- /dev/null +++ b/src/lib/dnssd/Advertiser.cpp @@ -0,0 +1,41 @@ +/* + * + * Copyright (c) 2024 Project CHIP Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "Advertiser.h" + +namespace chip { +namespace Dnssd { + +ServiceAdvertiser * ServiceAdvertiser::sInstance = nullptr; + +ServiceAdvertiser & ServiceAdvertiser::Instance() +{ + if (sInstance == nullptr) + { + sInstance = &GetDefaultAdvertiser(); + } + + return *sInstance; +} + +void ServiceAdvertiser::SetInstance(ServiceAdvertiser & advertiser) +{ + sInstance = &advertiser; +} + +} // namespace Dnssd +} // namespace chip diff --git a/src/lib/dnssd/Advertiser.h b/src/lib/dnssd/Advertiser.h index ddd8df259b1364..64fa9ec3fd5d33 100644 --- a/src/lib/dnssd/Advertiser.h +++ b/src/lib/dnssd/Advertiser.h @@ -375,9 +375,28 @@ class ServiceAdvertiser */ virtual CHIP_ERROR UpdateCommissionableInstanceName() = 0; - /// Provides the system-wide implementation of the service advertiser + /** + * Returns the system-wide implementation of the service advertiser. + * + * The method returns a reference to the advertiser object configured by + * a user using the \c ServiceAdvertiser::SetInstance() method, or the + * default advertiser returned by the \c GetDefaultAdvertiser() function. + */ static ServiceAdvertiser & Instance(); + + /** + * Sets the system-wide implementation of the service advertiser. + */ + static void SetInstance(ServiceAdvertiser & advertiser); + +private: + static ServiceAdvertiser * sInstance; }; +/** + * Returns the default implementation of the service advertiser. + */ +extern ServiceAdvertiser & GetDefaultAdvertiser(); + } // namespace Dnssd } // namespace chip diff --git a/src/lib/dnssd/Advertiser_ImplMinimalMdns.cpp b/src/lib/dnssd/Advertiser_ImplMinimalMdns.cpp index 4c80723738bcaa..f29ff25d63f0e1 100644 --- a/src/lib/dnssd/Advertiser_ImplMinimalMdns.cpp +++ b/src/lib/dnssd/Advertiser_ImplMinimalMdns.cpp @@ -975,10 +975,14 @@ void AdvertiserMinMdns::AdvertiseRecords(BroadcastAdvertiseType type) AdvertiserMinMdns gAdvertiser; } // namespace -ServiceAdvertiser & ServiceAdvertiser::Instance() +#if CHIP_DNSSD_DEFAULT_MINIMAL + +ServiceAdvertiser & GetDefaultAdvertiser() { return gAdvertiser; } +#endif // CHIP_DNSSD_DEFAULT_MINIMAL + } // namespace Dnssd } // namespace chip diff --git a/src/lib/dnssd/Advertiser_ImplNone.cpp b/src/lib/dnssd/Advertiser_ImplNone.cpp index 94935247694e53..2c1ed7f09b575b 100644 --- a/src/lib/dnssd/Advertiser_ImplNone.cpp +++ b/src/lib/dnssd/Advertiser_ImplNone.cpp @@ -77,10 +77,14 @@ NoneAdvertiser gAdvertiser; } // namespace -ServiceAdvertiser & ServiceAdvertiser::Instance() +#if CHIP_DNSSD_DEFAULT_NONE + +ServiceAdvertiser & GetDefaultAdvertiser() { return gAdvertiser; } +#endif // CHIP_DNSSD_DEFAULT_NONE + } // namespace Dnssd } // namespace chip diff --git a/src/lib/dnssd/BUILD.gn b/src/lib/dnssd/BUILD.gn index 405c499b18d14e..9447821ddcab19 100644 --- a/src/lib/dnssd/BUILD.gn +++ b/src/lib/dnssd/BUILD.gn @@ -68,9 +68,11 @@ static_library("dnssd") { ] sources = [ + "Advertiser.cpp", "Advertiser.h", "IPAddressSorter.cpp", "IPAddressSorter.h", + "Resolver.cpp", "Resolver.h", "ResolverProxy.cpp", "ResolverProxy.h", @@ -78,12 +80,18 @@ static_library("dnssd") { "TxtFields.h", ] + assert( + chip_mdns == "none" || chip_mdns == "minimal" || chip_mdns == "platform", + "Please select a valid value for chip_mdns") + if (chip_mdns == "none") { sources += [ "Advertiser_ImplNone.cpp", "Resolver_ImplNone.cpp", ] - } else if (chip_mdns == "minimal") { + } + + if (chip_mdns_minimal) { sources += [ "ActiveResolveAttempts.cpp", "ActiveResolveAttempts.h", @@ -100,15 +108,25 @@ static_library("dnssd") { "${chip_root}/src/tracing", "${chip_root}/src/tracing:macros", ] - } else if (chip_mdns == "platform") { + } + + if (chip_mdns_platform) { sources += [ "Discovery_ImplPlatform.cpp", "Discovery_ImplPlatform.h", ] public_deps += [ "${chip_root}/src/platform" ] - } else { - assert(false, "Unknown Dnssd advertiser implementation.") } + _chip_dnssd_default_none = chip_mdns == "none" + _chip_dnssd_default_minimal = chip_mdns == "minimal" + _chip_dnssd_default_platform = chip_mdns == "platform" + + defines = [ + "CHIP_DNSSD_DEFAULT_NONE=${_chip_dnssd_default_none}", + "CHIP_DNSSD_DEFAULT_MINIMAL=${_chip_dnssd_default_minimal}", + "CHIP_DNSSD_DEFAULT_PLATFORM=${_chip_dnssd_default_platform}", + ] + cflags = [ "-Wconversion" ] } diff --git a/src/lib/dnssd/Discovery_ImplPlatform.cpp b/src/lib/dnssd/Discovery_ImplPlatform.cpp index 3369f8c00b3b3b..6904480a4d1691 100644 --- a/src/lib/dnssd/Discovery_ImplPlatform.cpp +++ b/src/lib/dnssd/Discovery_ImplPlatform.cpp @@ -769,15 +769,19 @@ DiscoveryImplPlatform & DiscoveryImplPlatform::GetInstance() return sManager.get(); } -ServiceAdvertiser & chip::Dnssd::ServiceAdvertiser::Instance() +#if CHIP_DNSSD_DEFAULT_PLATFORM + +ServiceAdvertiser & GetDefaultAdvertiser() { return DiscoveryImplPlatform::GetInstance(); } -Resolver & chip::Dnssd::Resolver::Instance() +Resolver & GetDefaultResolver() { return DiscoveryImplPlatform::GetInstance(); } +#endif // CHIP_DNSSD_DEFAULT_PLATFORM + } // namespace Dnssd } // namespace chip diff --git a/src/lib/dnssd/Resolver.cpp b/src/lib/dnssd/Resolver.cpp new file mode 100644 index 00000000000000..19b94464d9d7b6 --- /dev/null +++ b/src/lib/dnssd/Resolver.cpp @@ -0,0 +1,41 @@ +/* + * + * Copyright (c) 2024 Project CHIP Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "Resolver.h" + +namespace chip { +namespace Dnssd { + +Resolver * Resolver::sInstance = nullptr; + +Resolver & Resolver::Instance() +{ + if (sInstance == nullptr) + { + sInstance = &GetDefaultResolver(); + } + + return *sInstance; +} + +void Resolver::SetInstance(Resolver & resolver) +{ + sInstance = &resolver; +} + +} // namespace Dnssd +} // namespace chip diff --git a/src/lib/dnssd/Resolver.h b/src/lib/dnssd/Resolver.h index d2541205bafd6d..6f9827b82218a6 100644 --- a/src/lib/dnssd/Resolver.h +++ b/src/lib/dnssd/Resolver.h @@ -206,10 +206,27 @@ class Resolver virtual CHIP_ERROR ReconfirmRecord(const char * hostname, Inet::IPAddress address, Inet::InterfaceId interfaceId) = 0; /** - * Provides the system-wide implementation of the service resolver + * Returns the system-wide implementation of the service resolver. + * + * The method returns a reference to the resolver object configured by + * a user using the \c Resolver::SetInstance() method, or the default + * resolver returned by the \c GetDefaultResolver() function. */ static Resolver & Instance(); + + /** + * Overrides the default implementation of the service resolver + */ + static void SetInstance(Resolver & resolver); + +private: + static Resolver * sInstance; }; +/** + * Returns the default implementation of the service resolver. + */ +extern Resolver & GetDefaultResolver(); + } // namespace Dnssd } // namespace chip diff --git a/src/lib/dnssd/Resolver_ImplMinimalMdns.cpp b/src/lib/dnssd/Resolver_ImplMinimalMdns.cpp index 7625286dd31b38..93a637839a91de 100644 --- a/src/lib/dnssd/Resolver_ImplMinimalMdns.cpp +++ b/src/lib/dnssd/Resolver_ImplMinimalMdns.cpp @@ -772,10 +772,14 @@ MinMdnsResolver gResolver; } // namespace -Resolver & chip::Dnssd::Resolver::Instance() +#if CHIP_DNSSD_DEFAULT_MINIMAL + +Resolver & GetDefaultResolver() { return gResolver; } +#endif // CHIP_DNSSD_DEFAULT_MINIMAL + } // namespace Dnssd } // namespace chip diff --git a/src/lib/dnssd/Resolver_ImplNone.cpp b/src/lib/dnssd/Resolver_ImplNone.cpp index 9a1f40f3008dff..36e2bd9aead89f 100644 --- a/src/lib/dnssd/Resolver_ImplNone.cpp +++ b/src/lib/dnssd/Resolver_ImplNone.cpp @@ -59,10 +59,14 @@ NoneResolver gResolver; } // namespace -Resolver & chip::Dnssd::Resolver::Instance() +#if CHIP_DNSSD_DEFAULT_NONE + +Resolver & GetDefaultResolver() { return gResolver; } +#endif // CHIP_DNSSD_DEFAULT_NONE + } // namespace Dnssd } // namespace chip diff --git a/src/platform/device.gni b/src/platform/device.gni index 5dc4c812c0c648..01358d4880f876 100644 --- a/src/platform/device.gni +++ b/src/platform/device.gni @@ -126,6 +126,12 @@ if (chip_device_platform == "nxp" && chip_enable_openthread) { chip_mdns = "minimal" } +declare_args() { + # Select DNS-SD components to build + chip_mdns_minimal = chip_mdns == "minimal" + chip_mdns_platform = chip_mdns == "platform" +} + _chip_device_layer = "none" if (chip_device_platform == "cc13x2_26x2") { _chip_device_layer = "cc13xx_26xx/cc13x2_26x2" diff --git a/src/platform/nrfconnect/BUILD.gn b/src/platform/nrfconnect/BUILD.gn index 7fe9dc2c822ea2..5eb866391a0aff 100644 --- a/src/platform/nrfconnect/BUILD.gn +++ b/src/platform/nrfconnect/BUILD.gn @@ -91,7 +91,7 @@ static_library("nrfconnect") { "ThreadStackManagerImpl.h", ] - if (chip_mdns == "platform") { + if (chip_mdns_platform) { sources += [ "../OpenThread/DnssdImpl.cpp", "../OpenThread/OpenThreadDnssdImpl.cpp",