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

Multiple Controllers: DND-SD should be able to support multiple consu… #12261

Closed
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
2 changes: 1 addition & 1 deletion examples/chip-tool/commands/discover/Commands.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class Resolve : public DiscoverCommand, public chip::Dnssd::ResolverDelegate
CHIP_ERROR RunCommand(NodeId remoteId, uint64_t fabricId) override
{
ReturnErrorOnFailure(chip::Dnssd::Resolver::Instance().Init(&chip::DeviceLayer::InetLayer()));
chip::Dnssd::Resolver::Instance().SetResolverDelegate(this);
chip::Dnssd::Resolver::Instance().RegisterResolverDelegate(this);
ChipLogProgress(chipTool, "Dnssd: Searching for NodeId: %" PRIx64 " FabricId: %" PRIx64 " ...", remoteId, fabricId);
return chip::Dnssd::Resolver::Instance().ResolveNodeId(chip::PeerId().SetNodeId(remoteId).SetCompressedFabricId(fabricId),
chip::Inet::IPAddressType::kAny);
Expand Down
2 changes: 1 addition & 1 deletion src/controller/AbstractDnssdDiscoveryController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ CHIP_ERROR AbstractDnssdDiscoveryController::SetUpNodeDiscovery()
#if CONFIG_DEVICE_LAYER
ReturnErrorOnFailure(mResolver->Init(&DeviceLayer::InetLayer()));
#endif
mResolver->SetResolverDelegate(this);
mResolver->RegisterResolverDelegate(this);

auto discoveredNodes = GetDiscoveredNodes();
for (auto & discoveredNode : discoveredNodes)
Expand Down
11 changes: 8 additions & 3 deletions src/controller/CHIPDeviceController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ CHIP_ERROR DeviceController::Init(ControllerInitParams params)

#if CHIP_DEVICE_CONFIG_ENABLE_DNSSD
Dnssd::Resolver::Instance().Init(params.systemState->InetLayer());
Dnssd::Resolver::Instance().SetResolverDelegate(this);
Dnssd::Resolver::Instance().RegisterResolverDelegate(this);
RegisterDeviceAddressUpdateDelegate(params.deviceAddressUpdateDelegate);
RegisterDeviceDiscoveryDelegate(params.deviceDiscoveryDelegate);
#endif // CHIP_DEVICE_CONFIG_ENABLE_DNSSD
Expand Down Expand Up @@ -234,7 +234,7 @@ CHIP_ERROR DeviceController::Shutdown()
mSystemState = nullptr;

#if CHIP_DEVICE_CONFIG_ENABLE_DNSSD
Dnssd::Resolver::Instance().SetResolverDelegate(nullptr);
Dnssd::Resolver::Instance().UnregisterResolverDelegate(this);
mDeviceAddressUpdateDelegate = nullptr;
mDeviceDiscoveryDelegate = nullptr;
#endif // CHIP_DEVICE_CONFIG_ENABLE_DNSSD
Expand Down Expand Up @@ -1519,9 +1519,11 @@ void BasicFailure(void * context, uint8_t status)
#if CHIP_DEVICE_CONFIG_ENABLE_DNSSD
void DeviceCommissioner::OnNodeIdResolved(const chip::Dnssd::ResolvedNodeData & nodeData)
{
VerifyOrReturn(mState == State::Initialized);
VerifyOrReturn(GetCompressedFabricId() == nodeData.mPeerId.GetCompressedFabricId());

ChipLogProgress(Controller, "OperationalDiscoveryComplete for device ID 0x" ChipLogFormatX64,
ChipLogValueX64(nodeData.mPeerId.GetNodeId()));
VerifyOrReturn(mState == State::Initialized);

if (mDeviceBeingCommissioned != nullptr && mDeviceBeingCommissioned->GetDeviceId() == nodeData.mPeerId.GetNodeId())
{
Expand All @@ -1540,6 +1542,9 @@ void DeviceCommissioner::OnNodeIdResolved(const chip::Dnssd::ResolvedNodeData &

void DeviceCommissioner::OnNodeIdResolutionFailed(const chip::PeerId & peer, CHIP_ERROR error)
{
VerifyOrReturn(mState == State::Initialized);
VerifyOrReturn(GetCompressedFabricId() == peer.GetCompressedFabricId());

if (mDeviceBeingCommissioned != nullptr)
{
CommissioneeDeviceProxy * device = mDeviceBeingCommissioned;
Expand Down
2 changes: 1 addition & 1 deletion src/controller/python/chip/discovery/NodeResolution.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ extern "C" ChipError::StorageType pychip_discovery_resolve(uint64_t fabricId, ui
chip::python::ChipMainThreadScheduleAndWait([&] {
result = Resolver::Instance().Init(&chip::DeviceLayer::InetLayer());
ReturnOnFailure(result);
Resolver::Instance().SetResolverDelegate(&gPythonResolverDelegate);
Resolver::Instance().RegisterResolverDelegate(&gPythonResolverDelegate);

result = Resolver::Instance().ResolveNodeId(chip::PeerId().SetCompressedFabricId(fabricId).SetNodeId(nodeId),
chip::Inet::IPAddressType::kAny);
Expand Down
3 changes: 2 additions & 1 deletion src/controller/tests/TestCommissionableNodeController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ class MockResolver : public Resolver
public:
CHIP_ERROR Init(chip::Inet::InetLayer * inetLayer) override { return InitStatus; }
void Shutdown() override {}
void SetResolverDelegate(ResolverDelegate *) override {}
void RegisterResolverDelegate(ResolverDelegate *) override {}
void UnregisterResolverDelegate(ResolverDelegate *) override {}
CHIP_ERROR ResolveNodeId(const PeerId & peerId, Inet::IPAddressType type) override { return ResolveNodeIdStatus; }
CHIP_ERROR FindCommissioners(DiscoveryFilter filter = DiscoveryFilter()) override { return FindCommissionersStatus; }
CHIP_ERROR FindCommissionableNodes(DiscoveryFilter filter = DiscoveryFilter()) override { return CHIP_ERROR_NOT_IMPLEMENTED; }
Expand Down
11 changes: 11 additions & 0 deletions src/lib/core/CHIPConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -2400,6 +2400,16 @@ extern const char CHIP_NON_PRODUCTION_MARKER[];
#define CHIP_COMMISSIONING_HINT_INDEX_PRESS_RESET_UNTIL_BLINK_WITH_POWER 11
#endif

/**
* @def CHIP_CONFIG_MDNS_MAX_RESOLVER_DELEGATES
*
* @brief
* Define the maximum number of delegates that will be informed of node address resolution.
*/
#ifndef CHIP_CONFIG_MDNS_MAX_RESOLVER_DELEGATES
#define CHIP_CONFIG_MDNS_MAX_RESOLVER_DELEGATES 4
#endif
andy31415 marked this conversation as resolved.
Show resolved Hide resolved

/**
* @def CHIP_CONFIG_MDNS_CACHE_SIZE
*
Expand All @@ -2412,6 +2422,7 @@ extern const char CHIP_NON_PRODUCTION_MARKER[];
#ifndef CHIP_CONFIG_MDNS_CACHE_SIZE
#define CHIP_CONFIG_MDNS_CACHE_SIZE 20
#endif

/**
* @name Interaction Model object pool configuration.
*
Expand Down
77 changes: 66 additions & 11 deletions src/lib/dnssd/Discovery_ImplPlatform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,30 @@ CHIP_ERROR DiscoveryImplPlatform::FinalizeServiceUpdate()
return ChipDnssdFinalizeServiceUpdate();
}

void DiscoveryImplPlatform::RegisterResolverDelegate(ResolverDelegate * delegate)
{
for (auto & resolverDelegate : mResolverDelegates)
{
if (resolverDelegate == nullptr || resolverDelegate == delegate)
woody-apple marked this conversation as resolved.
Show resolved Hide resolved
{
resolverDelegate = delegate;
break;
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we can return an error or print some error message when we run out of empty slots?

}

void DiscoveryImplPlatform::UnregisterResolverDelegate(ResolverDelegate * delegate)
{
for (auto & resolverDelegate : mResolverDelegates)
{
if (resolverDelegate == delegate)
{
resolverDelegate = nullptr;
break;
}
}
}

CHIP_ERROR DiscoveryImplPlatform::ResolveNodeId(const PeerId & peerId, Inet::IPAddressType type)
{
ReturnErrorOnFailure(InitImpl());
Expand All @@ -468,7 +492,13 @@ CHIP_ERROR DiscoveryImplPlatform::ResolveNodeId(const PeerId & peerId, Inet::IPA
ResolvedNodeData nodeData;
if (sDnssdCache.Lookup(peerId, nodeData) == CHIP_NO_ERROR)
{
mResolverDelegate->OnNodeIdResolved(nodeData);
for (auto & resolverDelegate : mResolverDelegates)
{
if (resolverDelegate != nullptr)
{
resolverDelegate->OnNodeIdResolved(nodeData);
}
}
return CHIP_NO_ERROR;
}
#endif
Expand Down Expand Up @@ -524,7 +554,13 @@ void DiscoveryImplPlatform::HandleNodeResolve(void * context, DnssdService * res
ByteSpan val(result->mTextEntries[i].mData, result->mTextEntries[i].mDataSize);
FillNodeDataFromTxt(key, val, data);
}
mgr->mResolverDelegate->OnNodeDiscoveryComplete(data);
for (auto & resolverDelegate : mgr->mResolverDelegates)
{
if (resolverDelegate != nullptr)
{
resolverDelegate->OnNodeDiscoveryComplete(data);
}
}
}

CHIP_ERROR DiscoveryImplPlatform::FindCommissionableNodes(DiscoveryFilter filter)
Expand All @@ -551,22 +587,29 @@ void DiscoveryImplPlatform::HandleNodeIdResolve(void * context, DnssdService * r
{
DiscoveryImplPlatform * mgr = static_cast<DiscoveryImplPlatform *>(context);

if (mgr->mResolverDelegate == nullptr)
{
return;
}

if (error != CHIP_NO_ERROR)
{
ChipLogError(Discovery, "Node ID resolved failed with %s", chip::ErrorStr(error));
mgr->mResolverDelegate->OnNodeIdResolutionFailed(PeerId(), error);
for (auto & resolverDelegate : mgr->mResolverDelegates)
{
if (resolverDelegate != nullptr)
{
resolverDelegate->OnNodeIdResolutionFailed(PeerId(), error);
}
}
return;
}

if (result == nullptr)
{
ChipLogError(Discovery, "Node ID resolve not found");
mgr->mResolverDelegate->OnNodeIdResolutionFailed(PeerId(), CHIP_ERROR_UNKNOWN_RESOURCE_ID);
for (auto & resolverDelegate : mgr->mResolverDelegates)
{
if (resolverDelegate != nullptr)
{
resolverDelegate->OnNodeIdResolutionFailed(PeerId(), CHIP_ERROR_UNKNOWN_RESOURCE_ID);
}
}
return;
}

Expand All @@ -576,7 +619,13 @@ void DiscoveryImplPlatform::HandleNodeIdResolve(void * context, DnssdService * r
if (error != CHIP_NO_ERROR)
{
ChipLogError(Discovery, "Node ID resolved failed with %s", chip::ErrorStr(error));
mgr->mResolverDelegate->OnNodeIdResolutionFailed(PeerId(), error);
for (auto & resolverDelegate : mgr->mResolverDelegates)
{
if (resolverDelegate != nullptr)
{
resolverDelegate->OnNodeIdResolutionFailed(PeerId(), error);
}
}
return;
}

Expand Down Expand Up @@ -607,7 +656,13 @@ void DiscoveryImplPlatform::HandleNodeIdResolve(void * context, DnssdService * r
ChipLogError(Discovery, "DnssdCache insert failed with %s", chip::ErrorStr(error));
}
#endif
mgr->mResolverDelegate->OnNodeIdResolved(nodeData);
for (auto & resolverDelegate : mgr->mResolverDelegates)
{
if (resolverDelegate != nullptr)
{
resolverDelegate->OnNodeIdResolved(nodeData);
}
}
}

DiscoveryImplPlatform & DiscoveryImplPlatform::GetInstance()
Expand Down
7 changes: 4 additions & 3 deletions src/lib/dnssd/Discovery_ImplPlatform.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ class DiscoveryImplPlatform : public ServiceAdvertiser, public Resolver
CHIP_ERROR GetCommissionableInstanceName(char * instanceName, size_t maxLength) override;

// Members that implement Resolver interface.
void SetResolverDelegate(ResolverDelegate * delegate) override { mResolverDelegate = delegate; }
void RegisterResolverDelegate(ResolverDelegate * delegate) override;
void UnregisterResolverDelegate(ResolverDelegate * delegate) override;
CHIP_ERROR ResolveNodeId(const PeerId & peerId, Inet::IPAddressType type) override;
CHIP_ERROR FindCommissionableNodes(DiscoveryFilter filter = DiscoveryFilter()) override;
CHIP_ERROR FindCommissioners(DiscoveryFilter filter = DiscoveryFilter()) override;
Expand Down Expand Up @@ -83,8 +84,8 @@ class DiscoveryImplPlatform : public ServiceAdvertiser, public Resolver
bool mIsCommissionerPublishing = false;
uint8_t mCommissionableInstanceName[sizeof(uint64_t)];

bool mDnssdInitialized = false;
ResolverDelegate * mResolverDelegate = nullptr;
bool mDnssdInitialized = false;
ResolverDelegate * mResolverDelegates[CHIP_CONFIG_MDNS_MAX_RESOLVER_DELEGATES] = {};

static DiscoveryImplPlatform sManager;
#if CHIP_CONFIG_MDNS_CACHE_SIZE > 0
Expand Down
10 changes: 7 additions & 3 deletions src/lib/dnssd/Resolver.h
Original file line number Diff line number Diff line change
Expand Up @@ -268,10 +268,14 @@ class Resolver
virtual void Shutdown() = 0;

/**
* Registers a resolver delegate. If nullptr is passed, the previously registered delegate
* is unregistered.
* Registers a resolver delegate.
woody-apple marked this conversation as resolved.
Show resolved Hide resolved
*/
virtual void SetResolverDelegate(ResolverDelegate * delegate) = 0;
virtual void RegisterResolverDelegate(ResolverDelegate * delegate) = 0;

/**
* Unregisters a resolver delegate.
*/
virtual void UnregisterResolverDelegate(ResolverDelegate * delegate) = 0;

/**
* Requests resolution of the given operational node service.
Expand Down
3 changes: 2 additions & 1 deletion src/lib/dnssd/Resolver_ImplMinimalMdns.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,8 @@ class MinMdnsResolver : public Resolver, public MdnsPacketDelegate
///// Resolver implementation
CHIP_ERROR Init(chip::Inet::InetLayer * inetLayer) override;
void Shutdown() override;
void SetResolverDelegate(ResolverDelegate * delegate) override { mDelegate = delegate; }
void RegisterResolverDelegate(ResolverDelegate * delegate) override { mDelegate = delegate; }
void UnregisterResolverDelegate(ResolverDelegate * delegate) override { mDelegate = nullptr; }
CHIP_ERROR ResolveNodeId(const PeerId & peerId, Inet::IPAddressType type) override;
CHIP_ERROR FindCommissionableNodes(DiscoveryFilter filter = DiscoveryFilter()) override;
CHIP_ERROR FindCommissioners(DiscoveryFilter filter = DiscoveryFilter()) override;
Expand Down
3 changes: 2 additions & 1 deletion src/lib/dnssd/Resolver_ImplNone.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ class NoneResolver : public Resolver
public:
CHIP_ERROR Init(chip::Inet::InetLayer *) override { return CHIP_NO_ERROR; }
void Shutdown() override {}
void SetResolverDelegate(ResolverDelegate *) override {}
void RegisterResolverDelegate(ResolverDelegate *) override {}
void UnregisterResolverDelegate(ResolverDelegate *) override {}

CHIP_ERROR ResolveNodeId(const PeerId & peerId, Inet::IPAddressType type) override
{
Expand Down
2 changes: 1 addition & 1 deletion src/lib/shell/commands/Dns.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ CHIP_ERROR DnsHandler(int argc, char ** argv)
}

Dnssd::Resolver::Instance().Init(&DeviceLayer::InetLayer());
Dnssd::Resolver::Instance().SetResolverDelegate(&sDnsShellResolverDelegate);
Dnssd::Resolver::Instance().RegisterResolverDelegate(&sDnsShellResolverDelegate);

return sShellDnsSubcommands.ExecCommand(argc, argv);
}
Expand Down