diff --git a/config/nrfconnect/chip-module/CMakeLists.txt b/config/nrfconnect/chip-module/CMakeLists.txt index 5756074545..0b8ce0be6e 100644 --- a/config/nrfconnect/chip-module/CMakeLists.txt +++ b/config/nrfconnect/chip-module/CMakeLists.txt @@ -136,6 +136,8 @@ matter_add_gn_arg_bool ("chip_malloc_sys_heap" CONFIG_CHIP_MA matter_add_gn_arg_bool ("chip_enable_wifi" CONFIG_WIFI_NRF700X) matter_add_gn_arg_bool ("chip_system_config_provide_statistics" CONFIG_CHIP_STATISTICS) matter_add_gn_arg_bool ("chip_enable_icd_server" CONFIG_CHIP_ENABLE_ICD_SUPPORT) +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_FACTORY_DATA) matter_add_gn_arg_bool("chip_use_transitional_commissionable_data_provider" FALSE) @@ -149,10 +151,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 0000000000..1f3d1b3669 --- /dev/null +++ b/src/lib/dnssd/Advertiser.cpp @@ -0,0 +1,41 @@ +/* + * + * Copyright (c) 2023 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 322ee3c871..b0f3d8a3bc 100644 --- a/src/lib/dnssd/Advertiser.h +++ b/src/lib/dnssd/Advertiser.h @@ -349,9 +349,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 26c0a37d0d..2403502aae 100644 --- a/src/lib/dnssd/Advertiser_ImplMinimalMdns.cpp +++ b/src/lib/dnssd/Advertiser_ImplMinimalMdns.cpp @@ -948,10 +948,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 9493524769..2c1ed7f09b 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 68b90f1f8a..69df6e45c9 100644 --- a/src/lib/dnssd/BUILD.gn +++ b/src/lib/dnssd/BUILD.gn @@ -30,9 +30,11 @@ static_library("dnssd") { ] sources = [ + "Advertiser.cpp", "Advertiser.h", "IPAddressSorter.cpp", "IPAddressSorter.h", + "Resolver.cpp", "Resolver.h", "ResolverProxy.cpp", "ResolverProxy.h", @@ -42,12 +44,17 @@ 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", @@ -63,15 +70,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 577678f858..1fd7e08618 100644 --- a/src/lib/dnssd/Discovery_ImplPlatform.cpp +++ b/src/lib/dnssd/Discovery_ImplPlatform.cpp @@ -744,15 +744,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 0000000000..4ed4de3e0a --- /dev/null +++ b/src/lib/dnssd/Resolver.cpp @@ -0,0 +1,41 @@ +/* + * + * Copyright (c) 2023 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 787cfa195d..b4f992ef21 100644 --- a/src/lib/dnssd/Resolver.h +++ b/src/lib/dnssd/Resolver.h @@ -481,10 +481,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 6446196536..b74d0ae87d 100644 --- a/src/lib/dnssd/Resolver_ImplMinimalMdns.cpp +++ b/src/lib/dnssd/Resolver_ImplMinimalMdns.cpp @@ -758,10 +758,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 9a1f40f300..36e2bd9aea 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 cf1dc07325..6ca09a5985 100644 --- a/src/platform/device.gni +++ b/src/platform/device.gni @@ -122,6 +122,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 5a11b4f3d0..df5e49f3a8 100644 --- a/src/platform/nrfconnect/BUILD.gn +++ b/src/platform/nrfconnect/BUILD.gn @@ -86,7 +86,7 @@ static_library("nrfconnect") { "ThreadStackManagerImpl.h", ] - if (chip_mdns == "platform") { + if (chip_mdns_platform) { sources += [ "../OpenThread/DnssdImpl.cpp", "../OpenThread/OpenThreadDnssdImpl.cpp",