Skip to content

Commit

Permalink
[nrf noup] Allow selecting DNS-SD implementation at runtime
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Damian-Nordic committed Nov 30, 2023
1 parent 2bc71bf commit 7a2cd55
Show file tree
Hide file tree
Showing 13 changed files with 179 additions and 16 deletions.
8 changes: 5 additions & 3 deletions config/nrfconnect/chip-module/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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()
Expand Down
41 changes: 41 additions & 0 deletions src/lib/dnssd/Advertiser.cpp
Original file line number Diff line number Diff line change
@@ -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
21 changes: 20 additions & 1 deletion src/lib/dnssd/Advertiser.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 5 additions & 1 deletion src/lib/dnssd/Advertiser_ImplMinimalMdns.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 5 additions & 1 deletion src/lib/dnssd/Advertiser_ImplNone.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
25 changes: 21 additions & 4 deletions src/lib/dnssd/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,11 @@ static_library("dnssd") {
]

sources = [
"Advertiser.cpp",
"Advertiser.h",
"IPAddressSorter.cpp",
"IPAddressSorter.h",
"Resolver.cpp",
"Resolver.h",
"ResolverProxy.cpp",
"ResolverProxy.h",
Expand All @@ -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",
Expand All @@ -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" ]
}
8 changes: 6 additions & 2 deletions src/lib/dnssd/Discovery_ImplPlatform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
41 changes: 41 additions & 0 deletions src/lib/dnssd/Resolver.cpp
Original file line number Diff line number Diff line change
@@ -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
19 changes: 18 additions & 1 deletion src/lib/dnssd/Resolver.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 5 additions & 1 deletion src/lib/dnssd/Resolver_ImplMinimalMdns.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 5 additions & 1 deletion src/lib/dnssd/Resolver_ImplNone.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 6 additions & 0 deletions src/platform/device.gni
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion src/platform/nrfconnect/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ static_library("nrfconnect") {
"ThreadStackManagerImpl.h",
]

if (chip_mdns == "platform") {
if (chip_mdns_platform) {
sources += [
"../OpenThread/DnssdImpl.cpp",
"../OpenThread/OpenThreadDnssdImpl.cpp",
Expand Down

0 comments on commit 7a2cd55

Please sign in to comment.