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

[nrf noup] Allow selecting DNS-SD implementation at runtime #359

Merged
merged 2 commits into from
Dec 5, 2023
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 @@ -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
2 changes: 0 additions & 2 deletions src/app/CASESessionManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@
#include <transport/SessionDelegate.h>
#include <transport/SessionUpdateDelegate.h>

#include <lib/dnssd/ResolverProxy.h>

namespace chip {

class OperationalSessionSetupPoolDelegate;
Expand Down
2 changes: 0 additions & 2 deletions src/app/tests/suites/commands/discovery/DiscoveryCommands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,15 +117,13 @@ CHIP_ERROR DiscoveryCommands::SetupDiscoveryCommands()
ReturnErrorOnFailure(mDNSResolver.Init(chip::DeviceLayer::UDPEndPointManager()));
mReady = true;
}
mDNSResolver.SetOperationalDelegate(this);
mDNSResolver.SetCommissioningDelegate(this);
return CHIP_NO_ERROR;
}

CHIP_ERROR DiscoveryCommands::TearDownDiscoveryCommands()
{
mDNSResolver.StopDiscovery();
mDNSResolver.SetOperationalDelegate(nullptr);
mDNSResolver.SetCommissioningDelegate(nullptr);
return CHIP_NO_ERROR;
}
Expand Down
6 changes: 1 addition & 5 deletions src/app/tests/suites/commands/discovery/DiscoveryCommands.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

#include <app-common/zap-generated/tests/simulated-cluster-objects.h>

class DiscoveryCommands : public chip::Dnssd::CommissioningResolveDelegate, public chip::Dnssd::OperationalResolveDelegate
class DiscoveryCommands : public chip::Dnssd::CommissioningResolveDelegate
{
public:
DiscoveryCommands(){};
Expand Down Expand Up @@ -64,10 +64,6 @@ class DiscoveryCommands : public chip::Dnssd::CommissioningResolveDelegate, publ
/////////// CommissioningDelegate Interface /////////
void OnNodeDiscovered(const chip::Dnssd::DiscoveredNodeData & nodeData) override;

/////////// OperationalDelegate Interface /////////
void OnOperationalNodeResolved(const chip::Dnssd::ResolvedNodeData & nodeData) override{};
void OnOperationalNodeResolutionFailed(const chip::PeerId & peerId, CHIP_ERROR error) override{};

private:
bool mReady = false;
chip::Dnssd::ResolverProxy mDNSResolver;
Expand Down
3 changes: 1 addition & 2 deletions src/controller/AbstractDnssdDiscoveryController.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ namespace Controller {
class DLL_EXPORT AbstractDnssdDiscoveryController : public Dnssd::CommissioningResolveDelegate
{
public:
AbstractDnssdDiscoveryController() {}
~AbstractDnssdDiscoveryController() override { mDNSResolver.Shutdown(); }
explicit AbstractDnssdDiscoveryController(Dnssd::Resolver * resolver = nullptr) : mDNSResolver(resolver) {}

void OnNodeDiscovered(const chip::Dnssd::DiscoveredNodeData & nodeData) override;
CHIP_ERROR StopDiscovery() { return mDNSResolver.StopDiscovery(); };
Expand Down
26 changes: 4 additions & 22 deletions src/controller/CHIPCommissionableNodeController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,30 +32,12 @@ CHIP_ERROR CommissionableNodeController::DiscoverCommissioners(Dnssd::DiscoveryF
{
ReturnErrorOnFailure(SetUpNodeDiscovery());

if (mResolver == nullptr)
{
#if CONFIG_DEVICE_LAYER
mDNSResolver.Shutdown(); // reset if already inited
ReturnErrorOnFailure(mDNSResolver.Init(DeviceLayer::UDPEndPointManager()));
mDNSResolver.Shutdown(); // reset if already inited
ReturnErrorOnFailure(mDNSResolver.Init(DeviceLayer::UDPEndPointManager()));
#endif
mDNSResolver.SetCommissioningDelegate(this);
return mDNSResolver.DiscoverCommissioners(discoveryFilter);
}

#if CONFIG_DEVICE_LAYER
ReturnErrorOnFailure(mResolver->Init(DeviceLayer::UDPEndPointManager()));
#endif
return mResolver->DiscoverCommissioners(discoveryFilter);
}

CHIP_ERROR CommissionableNodeController::StopDiscovery()
{
if (mResolver == nullptr)
{
return AbstractDnssdDiscoveryController::StopDiscovery();
}

return mResolver->StopDiscovery();
mDNSResolver.SetCommissioningDelegate(this);
return mDNSResolver.DiscoverCommissioners(discoveryFilter);
}

CommissionableNodeController::~CommissionableNodeController()
Expand Down
5 changes: 1 addition & 4 deletions src/controller/CHIPCommissionableNodeController.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,12 @@ namespace Controller {
class DLL_EXPORT CommissionableNodeController : public AbstractDnssdDiscoveryController
{
public:
CommissionableNodeController(chip::Dnssd::Resolver * resolver = nullptr) : mResolver(resolver) {}
CommissionableNodeController(chip::Dnssd::Resolver * resolver = nullptr) : AbstractDnssdDiscoveryController(resolver) {}
~CommissionableNodeController() override;

void RegisterDeviceDiscoveryDelegate(DeviceDiscoveryDelegate * delegate) { mDeviceDiscoveryDelegate = delegate; }
CHIP_ERROR DiscoverCommissioners(Dnssd::DiscoveryFilter discoveryFilter = Dnssd::DiscoveryFilter());

CHIP_ERROR StopDiscovery();

/**
* @return
* Pointer to DiscoveredNodeData at index idx in the list of commissioners discovered
Expand All @@ -57,7 +55,6 @@ class DLL_EXPORT CommissionableNodeController : public AbstractDnssdDiscoveryCon
DiscoveredNodeList GetDiscoveredNodes() override { return DiscoveredNodeList(mDiscoveredCommissioners); }

private:
Dnssd::Resolver * mResolver = nullptr;
Dnssd::DiscoveredNodeData mDiscoveredCommissioners[CHIP_DEVICE_CONFIG_MAX_DISCOVERED_NODES];
};

Expand Down
7 changes: 3 additions & 4 deletions src/controller/tests/TestCommissionableNodeController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,14 @@ class MockResolver : public Resolver
bool IsInitialized() override { return true; }
void Shutdown() override {}
void SetOperationalDelegate(OperationalResolveDelegate * delegate) override {}
void SetCommissioningDelegate(CommissioningResolveDelegate * delegate) override {}
CHIP_ERROR ResolveNodeId(const PeerId & peerId) override { return ResolveNodeIdStatus; }
void NodeIdResolutionNoLongerNeeded(const PeerId & peerId) override {}
CHIP_ERROR DiscoverCommissioners(DiscoveryFilter filter = DiscoveryFilter()) override { return DiscoverCommissionersStatus; }
CHIP_ERROR DiscoverCommissionableNodes(DiscoveryFilter filter = DiscoveryFilter()) override
CHIP_ERROR DiscoverCommissioners(DiscoveryFilter filter, DiscoveryContext &) override { return DiscoverCommissionersStatus; }
CHIP_ERROR DiscoverCommissionableNodes(DiscoveryFilter filter, DiscoveryContext &) override
{
return CHIP_ERROR_NOT_IMPLEMENTED;
}
CHIP_ERROR StopDiscovery() override { return CHIP_ERROR_NOT_IMPLEMENTED; }
CHIP_ERROR StopDiscovery(DiscoveryContext &) override { return CHIP_ERROR_NOT_IMPLEMENTED; }
CHIP_ERROR ReconfirmRecord(const char * hostname, Inet::IPAddress address, Inet::InterfaceId interfaceId) override
{
return CHIP_ERROR_NOT_IMPLEMENTED;
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
27 changes: 23 additions & 4 deletions src/lib/dnssd/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -30,22 +30,31 @@ static_library("dnssd") {
]

sources = [
"Advertiser.cpp",
"Advertiser.h",
"IPAddressSorter.cpp",
"IPAddressSorter.h",
"Resolver.cpp",
"Resolver.h",
"ResolverProxy.cpp",
"ResolverProxy.h",
"ServiceNaming.cpp",
"ServiceNaming.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 @@ -61,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" ]
}
Loading
Loading