Skip to content

Commit cfe283f

Browse files
[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 <damian.krolik@nordicsemi.no>
1 parent 96329ae commit cfe283f

13 files changed

+180
-16
lines changed

config/nrfconnect/chip-module/CMakeLists.txt

+5-3
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,8 @@ matter_add_gn_arg_bool ("chip_system_config_provide_statistics" CONFIG_CHIP_ST
140140
matter_add_gn_arg_bool ("chip_enable_icd_server" CONFIG_CHIP_ENABLE_ICD_SUPPORT)
141141
matter_add_gn_arg_bool ("chip_enable_factory_data" CONFIG_CHIP_FACTORY_DATA)
142142
matter_add_gn_arg_bool ("chip_enable_read_client" CONFIG_CHIP_ENABLE_READ_CLIENT)
143+
matter_add_gn_arg_bool ("chip_mdns_minimal" CONFIG_WIFI_NRF700X)
144+
matter_add_gn_arg_bool ("chip_mdns_platform" CONFIG_NET_L2_OPENTHREAD)
143145

144146
if (CONFIG_CHIP_ENABLE_ICD_SUPPORT)
145147
matter_add_gn_arg_bool ("chip_enable_icd_lit" CONFIG_CHIP_ICD_LIT_SUPPORT)
@@ -157,10 +159,10 @@ if (CONFIG_CHIP_ROTATING_DEVICE_ID)
157159
matter_add_gn_arg_bool("chip_enable_additional_data_advertising" TRUE)
158160
endif()
159161

160-
if (CONFIG_NET_L2_OPENTHREAD)
161-
matter_add_gn_arg_string("chip_mdns" "platform")
162-
elseif(CONFIG_WIFI_NRF700X)
162+
if(CONFIG_WIFI_NRF700X)
163163
matter_add_gn_arg_string("chip_mdns" "minimal")
164+
elseif (CONFIG_NET_L2_OPENTHREAD)
165+
matter_add_gn_arg_string("chip_mdns" "platform")
164166
else()
165167
matter_add_gn_arg_string("chip_mdns" "none")
166168
endif()

src/lib/dnssd/Advertiser.cpp

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
*
3+
* Copyright (c) 2024 Project CHIP Authors
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
#include "Advertiser.h"
19+
20+
namespace chip {
21+
namespace Dnssd {
22+
23+
ServiceAdvertiser * ServiceAdvertiser::sInstance = nullptr;
24+
25+
ServiceAdvertiser & ServiceAdvertiser::Instance()
26+
{
27+
if (sInstance == nullptr)
28+
{
29+
sInstance = &GetDefaultAdvertiser();
30+
}
31+
32+
return *sInstance;
33+
}
34+
35+
void ServiceAdvertiser::SetInstance(ServiceAdvertiser & advertiser)
36+
{
37+
sInstance = &advertiser;
38+
}
39+
40+
} // namespace Dnssd
41+
} // namespace chip

src/lib/dnssd/Advertiser.h

+20-1
Original file line numberDiff line numberDiff line change
@@ -375,9 +375,28 @@ class ServiceAdvertiser
375375
*/
376376
virtual CHIP_ERROR UpdateCommissionableInstanceName() = 0;
377377

378-
/// Provides the system-wide implementation of the service advertiser
378+
/**
379+
* Returns the system-wide implementation of the service advertiser.
380+
*
381+
* The method returns a reference to the advertiser object configured by
382+
* a user using the \c ServiceAdvertiser::SetInstance() method, or the
383+
* default advertiser returned by the \c GetDefaultAdvertiser() function.
384+
*/
379385
static ServiceAdvertiser & Instance();
386+
387+
/**
388+
* Sets the system-wide implementation of the service advertiser.
389+
*/
390+
static void SetInstance(ServiceAdvertiser & advertiser);
391+
392+
private:
393+
static ServiceAdvertiser * sInstance;
380394
};
381395

396+
/**
397+
* Returns the default implementation of the service advertiser.
398+
*/
399+
extern ServiceAdvertiser & GetDefaultAdvertiser();
400+
382401
} // namespace Dnssd
383402
} // namespace chip

src/lib/dnssd/Advertiser_ImplMinimalMdns.cpp

+5-1
Original file line numberDiff line numberDiff line change
@@ -975,10 +975,14 @@ void AdvertiserMinMdns::AdvertiseRecords(BroadcastAdvertiseType type)
975975
AdvertiserMinMdns gAdvertiser;
976976
} // namespace
977977

978-
ServiceAdvertiser & ServiceAdvertiser::Instance()
978+
#if CHIP_DNSSD_DEFAULT_MINIMAL
979+
980+
ServiceAdvertiser & GetDefaultAdvertiser()
979981
{
980982
return gAdvertiser;
981983
}
982984

985+
#endif // CHIP_DNSSD_DEFAULT_MINIMAL
986+
983987
} // namespace Dnssd
984988
} // namespace chip

src/lib/dnssd/Advertiser_ImplNone.cpp

+5-1
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,14 @@ NoneAdvertiser gAdvertiser;
7777

7878
} // namespace
7979

80-
ServiceAdvertiser & ServiceAdvertiser::Instance()
80+
#if CHIP_DNSSD_DEFAULT_NONE
81+
82+
ServiceAdvertiser & GetDefaultAdvertiser()
8183
{
8284
return gAdvertiser;
8385
}
8486

87+
#endif // CHIP_DNSSD_DEFAULT_NONE
88+
8589
} // namespace Dnssd
8690
} // namespace chip

src/lib/dnssd/BUILD.gn

+22-4
Original file line numberDiff line numberDiff line change
@@ -68,22 +68,30 @@ static_library("dnssd") {
6868
]
6969

7070
sources = [
71+
"Advertiser.cpp",
7172
"Advertiser.h",
7273
"IPAddressSorter.cpp",
7374
"IPAddressSorter.h",
75+
"Resolver.cpp",
7476
"Resolver.h",
7577
"ResolverProxy.cpp",
7678
"ResolverProxy.h",
7779
"TxtFields.cpp",
7880
"TxtFields.h",
7981
]
8082

83+
assert(
84+
chip_mdns == "none" || chip_mdns == "minimal" || chip_mdns == "platform",
85+
"Please select a valid value for chip_mdns")
86+
8187
if (chip_mdns == "none") {
8288
sources += [
8389
"Advertiser_ImplNone.cpp",
8490
"Resolver_ImplNone.cpp",
8591
]
86-
} else if (chip_mdns == "minimal") {
92+
}
93+
94+
if (chip_mdns_minimal) {
8795
sources += [
8896
"ActiveResolveAttempts.cpp",
8997
"ActiveResolveAttempts.h",
@@ -100,15 +108,25 @@ static_library("dnssd") {
100108
"${chip_root}/src/tracing",
101109
"${chip_root}/src/tracing:macros",
102110
]
103-
} else if (chip_mdns == "platform") {
111+
}
112+
113+
if (chip_mdns_platform) {
104114
sources += [
105115
"Discovery_ImplPlatform.cpp",
106116
"Discovery_ImplPlatform.h",
107117
]
108118
public_deps += [ "${chip_root}/src/platform" ]
109-
} else {
110-
assert(false, "Unknown Dnssd advertiser implementation.")
111119
}
112120

121+
_chip_dnssd_default_none = chip_mdns == "none"
122+
_chip_dnssd_default_minimal = chip_mdns == "minimal"
123+
_chip_dnssd_default_platform = chip_mdns == "platform"
124+
125+
defines = [
126+
"CHIP_DNSSD_DEFAULT_NONE=${_chip_dnssd_default_none}",
127+
"CHIP_DNSSD_DEFAULT_MINIMAL=${_chip_dnssd_default_minimal}",
128+
"CHIP_DNSSD_DEFAULT_PLATFORM=${_chip_dnssd_default_platform}",
129+
]
130+
113131
cflags = [ "-Wconversion" ]
114132
}

src/lib/dnssd/Discovery_ImplPlatform.cpp

+6-2
Original file line numberDiff line numberDiff line change
@@ -769,15 +769,19 @@ DiscoveryImplPlatform & DiscoveryImplPlatform::GetInstance()
769769
return sManager.get();
770770
}
771771

772-
ServiceAdvertiser & chip::Dnssd::ServiceAdvertiser::Instance()
772+
#if CHIP_DNSSD_DEFAULT_PLATFORM
773+
774+
ServiceAdvertiser & GetDefaultAdvertiser()
773775
{
774776
return DiscoveryImplPlatform::GetInstance();
775777
}
776778

777-
Resolver & chip::Dnssd::Resolver::Instance()
779+
Resolver & GetDefaultResolver()
778780
{
779781
return DiscoveryImplPlatform::GetInstance();
780782
}
781783

784+
#endif // CHIP_DNSSD_DEFAULT_PLATFORM
785+
782786
} // namespace Dnssd
783787
} // namespace chip

src/lib/dnssd/Resolver.cpp

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
*
3+
* Copyright (c) 2024 Project CHIP Authors
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
#include "Resolver.h"
19+
20+
namespace chip {
21+
namespace Dnssd {
22+
23+
Resolver * Resolver::sInstance = nullptr;
24+
25+
Resolver & Resolver::Instance()
26+
{
27+
if (sInstance == nullptr)
28+
{
29+
sInstance = &GetDefaultResolver();
30+
}
31+
32+
return *sInstance;
33+
}
34+
35+
void Resolver::SetInstance(Resolver & resolver)
36+
{
37+
sInstance = &resolver;
38+
}
39+
40+
} // namespace Dnssd
41+
} // namespace chip

src/lib/dnssd/Resolver.h

+18-1
Original file line numberDiff line numberDiff line change
@@ -206,10 +206,27 @@ class Resolver
206206
virtual CHIP_ERROR ReconfirmRecord(const char * hostname, Inet::IPAddress address, Inet::InterfaceId interfaceId) = 0;
207207

208208
/**
209-
* Provides the system-wide implementation of the service resolver
209+
* Returns the system-wide implementation of the service resolver.
210+
*
211+
* The method returns a reference to the resolver object configured by
212+
* a user using the \c Resolver::SetInstance() method, or the default
213+
* resolver returned by the \c GetDefaultResolver() function.
210214
*/
211215
static Resolver & Instance();
216+
217+
/**
218+
* Overrides the default implementation of the service resolver
219+
*/
220+
static void SetInstance(Resolver & resolver);
221+
222+
private:
223+
static Resolver * sInstance;
212224
};
213225

226+
/**
227+
* Returns the default implementation of the service resolver.
228+
*/
229+
extern Resolver & GetDefaultResolver();
230+
214231
} // namespace Dnssd
215232
} // namespace chip

src/lib/dnssd/Resolver_ImplMinimalMdns.cpp

+5-1
Original file line numberDiff line numberDiff line change
@@ -758,10 +758,14 @@ MinMdnsResolver gResolver;
758758

759759
} // namespace
760760

761-
Resolver & chip::Dnssd::Resolver::Instance()
761+
#if CHIP_DNSSD_DEFAULT_MINIMAL
762+
763+
Resolver & GetDefaultResolver()
762764
{
763765
return gResolver;
764766
}
765767

768+
#endif // CHIP_DNSSD_DEFAULT_MINIMAL
769+
766770
} // namespace Dnssd
767771
} // namespace chip

src/lib/dnssd/Resolver_ImplNone.cpp

+5-1
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,14 @@ NoneResolver gResolver;
5959

6060
} // namespace
6161

62-
Resolver & chip::Dnssd::Resolver::Instance()
62+
#if CHIP_DNSSD_DEFAULT_NONE
63+
64+
Resolver & GetDefaultResolver()
6365
{
6466
return gResolver;
6567
}
6668

69+
#endif // CHIP_DNSSD_DEFAULT_NONE
70+
6771
} // namespace Dnssd
6872
} // namespace chip

src/platform/device.gni

+6
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,12 @@ if (chip_device_platform == "nxp" && chip_enable_openthread) {
126126
chip_mdns = "minimal"
127127
}
128128

129+
declare_args() {
130+
# Select DNS-SD components to build
131+
chip_mdns_minimal = chip_mdns == "minimal"
132+
chip_mdns_platform = chip_mdns == "platform"
133+
}
134+
129135
_chip_device_layer = "none"
130136
if (chip_device_platform == "cc13x2_26x2") {
131137
_chip_device_layer = "cc13xx_26xx/cc13x2_26x2"

src/platform/nrfconnect/BUILD.gn

+1-1
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ static_library("nrfconnect") {
9191
"ThreadStackManagerImpl.h",
9292
]
9393

94-
if (chip_mdns == "platform") {
94+
if (chip_mdns_platform) {
9595
sources += [
9696
"../OpenThread/DnssdImpl.cpp",
9797
"../OpenThread/OpenThreadDnssdImpl.cpp",

0 commit comments

Comments
 (0)