Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[dnssd] Allow selecting DNS-SD implementation at runtime #32829

Merged
merged 1 commit into from
Apr 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions config/nrfconnect/chip-module/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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()
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) 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
21 changes: 20 additions & 1 deletion src/lib/dnssd/Advertiser.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 5 additions & 1 deletion src/lib/dnssd/Advertiser_ImplMinimalMdns.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
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
26 changes: 22 additions & 4 deletions src/lib/dnssd/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -68,22 +68,30 @@ static_library("dnssd") {
]

sources = [
"Advertiser.cpp",
"Advertiser.h",
"IPAddressSorter.cpp",
"IPAddressSorter.h",
"Resolver.cpp",
"Resolver.h",
"ResolverProxy.cpp",
"ResolverProxy.h",
"TxtFields.cpp",
"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 @@ -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" ]
}
8 changes: 6 additions & 2 deletions src/lib/dnssd/Discovery_ImplPlatform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
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) 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();
kkasperczyk-no marked this conversation as resolved.
Show resolved Hide resolved
}

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 @@ -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
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 @@ -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"
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 @@ -91,7 +91,7 @@ static_library("nrfconnect") {
"ThreadStackManagerImpl.h",
]

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