Skip to content

Commit

Permalink
Pull request project-chip#1498: Cherry pick/mpc enable mDNS discovery
Browse files Browse the repository at this point in the history
Merge in WMN_TOOLS/matter from cherry-pick/mpc-enable-mDNS-discovery to RC_2.3.0-1.3

Squashed commit of the following:

commit e84099fa71ddb00c7e8c6e956c86d9f0b4e9348e
Author: shasaicha <[email protected]>
Date:   Wed Jan 17 21:32:35 2024 +0530

    Adds check before calling Resolver Init

commit 2df60ff2f807afaaab8a1b218d6bf6ca79a8c42b
Author: Suhas Shankar <[email protected]>
Date:   Mon Oct 16 13:26:08 2023 +0000

    Pull request project-chip#1237: Pull request project-chip#1024: MATTER-2541: Added Operational Node discorvery to the mDNS resolver.

    Merge in WMN_TOOLS/matter from cherry-pick/mpc-enable-mDNS-discovery to RC_2.2.0-1.2

    Squashed commit of the following:

    commit fffa064f6f3fecb2f9c7ad529301aa7135298792
    Author: Suhas Shankar <[email protected]>
    Date:   Thu Aug 17 15:55:06 2023 +0000

        Pull request project-chip#1024: Added Operational Node discorvery to the mDNS resolver.

        enables MPC node discovery as well

        Squashed commit of the following:

        commit 523637915ef3edf7e110c370200f0cee71541b13
        Author: Anders Lynge Esbensen <[email protected]>
        Date:   Sun Mar 12 14:10:04 2023 +0100

            Added Operational Node discorvery to the mDNS resolver.

            Added new delegate and interface function for discovering operational
            devices.
  • Loading branch information
ShekarKurva SaiChandra authored and kurvaSai committed May 29, 2024
1 parent 9ce9b24 commit b8f80db
Show file tree
Hide file tree
Showing 9 changed files with 138 additions and 3 deletions.
4 changes: 4 additions & 0 deletions src/controller/tests/TestCommissionableNodeController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
* limitations under the License.
*/

#include "lib/dnssd/Resolver.h"
#include <controller/CHIPCommissionableNodeController.h>
#include <lib/support/CHIPMemString.h>
#include <lib/support/UnitTestRegistration.h>
Expand Down Expand Up @@ -43,7 +44,10 @@ class MockResolver : public Resolver
{
return CHIP_ERROR_NOT_IMPLEMENTED;
}

CHIP_ERROR StopDiscovery(DiscoveryContext &) override { return CHIP_ERROR_NOT_IMPLEMENTED; }
CHIP_ERROR DiscoverOperational(DiscoveryFilter filter, 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
25 changes: 25 additions & 0 deletions src/lib/dnssd/Discovery_ImplPlatform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -653,6 +653,31 @@ void DiscoveryImplPlatform::NodeIdResolutionNoLongerNeeded(const PeerId & peerId
ChipDnssdResolveNoLongerNeeded(name);
}

CHIP_ERROR DiscoveryImplPlatform::DiscoverOperational(DiscoveryFilter filter, DiscoveryContext & context)
{
ReturnErrorOnFailure(InitImpl());
StopDiscovery(context);

char serviceName[kMaxCommissionerServiceNameSize];
ReturnErrorOnFailure(MakeServiceTypeName(serviceName, sizeof(serviceName), filter, DiscoveryType::kOperational));

intptr_t browseIdentifier;
// Increase the reference count of the context to keep it alive until HandleNodeBrowse is called back.
CHIP_ERROR error = ChipDnssdBrowse(serviceName, DnssdServiceProtocol::kDnssdProtocolUdp, Inet::IPAddressType::kAny,
Inet::InterfaceId::Null(), HandleNodeBrowse, context.Retain(), &browseIdentifier);

if (error == CHIP_NO_ERROR)
{
context.SetBrowseIdentifier(browseIdentifier);
}
else
{
context.Release();
}

return CHIP_NO_ERROR;
}

CHIP_ERROR DiscoveryImplPlatform::DiscoverCommissionableNodes(DiscoveryFilter filter, DiscoveryContext & context)
{
ReturnErrorOnFailure(InitImpl());
Expand Down
3 changes: 3 additions & 0 deletions src/lib/dnssd/Discovery_ImplPlatform.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,14 @@ class DiscoveryImplPlatform : public ServiceAdvertiser, public Resolver

// Members that implement Resolver interface.
void SetOperationalDelegate(OperationalResolveDelegate * delegate) override { mOperationalDelegate = delegate; }

CHIP_ERROR ResolveNodeId(const PeerId & peerId) override;
void NodeIdResolutionNoLongerNeeded(const PeerId & peerId) override;
CHIP_ERROR DiscoverCommissionableNodes(DiscoveryFilter filter, DiscoveryContext & context) override;
CHIP_ERROR DiscoverCommissioners(DiscoveryFilter filter, DiscoveryContext & context) override;
CHIP_ERROR DiscoverOperational(DiscoveryFilter filter, DiscoveryContext & context) override;
CHIP_ERROR StopDiscovery(DiscoveryContext & context) override;

CHIP_ERROR ReconfirmRecord(const char * hostname, Inet::IPAddress address, Inet::InterfaceId interfaceId) override;

static DiscoveryImplPlatform & GetInstance();
Expand Down
43 changes: 42 additions & 1 deletion src/lib/dnssd/Resolver.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,19 @@ class OperationalResolveDelegate
virtual void OnOperationalNodeResolutionFailed(const PeerId & peerId, CHIP_ERROR error) = 0;
};

/// Callback for Browsing for nodes without resolving the DNS records
///
class OperationalBrowseDelegate
{
public:
virtual ~OperationalBrowseDelegate() = default;
/// Called within the CHIP event loop once a node is discovered.
///
/// May be called multiple times as more nodes send their answer to a
/// multicast discovery query
virtual void OnOperationalNodeDiscovered(const OperationalNodeData & operationalData) = 0;
};

/**
* Node discovery context class.
*
Expand Down Expand Up @@ -91,8 +104,22 @@ class DiscoveryContext : public ReferenceCounted<DiscoveryContext>
}
}

void SetOperationalBrowseDelegate(OperationalBrowseDelegate * delegate) { mBrowseOperationalDelegate = delegate; }

void OnOperationalNodeDiscovered(const OperationalNodeData & operationalData)
{
if (mBrowseOperationalDelegate != nullptr){
mBrowseOperationalDelegate->OnOperationalNodeDiscovered(operationalData);
}
else
{
ChipLogError(Discovery, "Missing Browse delegate. Data discarded");
}
}

private:
CommissioningResolveDelegate * mCommissioningDelegate = nullptr;
OperationalBrowseDelegate * mBrowseOperationalDelegate = nullptr;
Optional<intptr_t> mBrowseIdentifier;
};

Expand Down Expand Up @@ -192,7 +219,21 @@ class Resolver
virtual CHIP_ERROR DiscoverCommissioners(DiscoveryFilter filter, DiscoveryContext & context) = 0;

/**
* Stop discovery (of commissionable or commissioner nodes).
* @brief Start Browising for operational devices
*
* This will start periodical brwosing of operational devices. The when a new node is
* found the delegate set with SetOperationalBrowseDelegate is called. The mDNS queryies
* will continue unitil StopDiscovery is called.
*
* Note, this will locate all the Operational devices on the network. Typically an
* application would filter the found nodes by relevant fabrics.
*
* If needed a discovery filter may be specified to narrow the search.
*/
virtual CHIP_ERROR DiscoverOperational(DiscoveryFilter filter, DiscoveryContext & context) = 0;

/**
* Stop discovery (of operational, commissionable or commissioner nodes).
*
* Some back ends may not support stopping discovery, so consumers should
* not assume they will stop getting callbacks after calling this.
Expand Down
12 changes: 11 additions & 1 deletion src/lib/dnssd/ResolverProxy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ CHIP_ERROR ResolverProxy::Init(Inet::EndPointManager<Inet::UDPEndPoint> * udpEnd
{
VerifyOrReturnError(mContext == nullptr, CHIP_ERROR_INCORRECT_STATE);

ReturnErrorOnFailure(mResolver.Init(udpEndPoint));
if (!mResolver.IsInitialized())
{
ReturnErrorOnFailure(mResolver.Init(udpEndPoint));
}
mContext = Platform::New<DiscoveryContext>();
VerifyOrReturnError(mContext != nullptr, CHIP_ERROR_NO_MEMORY);

Expand Down Expand Up @@ -55,6 +58,13 @@ CHIP_ERROR ResolverProxy::DiscoverCommissioners(DiscoveryFilter filter)
return mResolver.DiscoverCommissioners(filter, *mContext);
}

CHIP_ERROR ResolverProxy::DiscoverOperational(DiscoveryFilter filter)
{
VerifyOrReturnError(mContext != nullptr, CHIP_ERROR_INCORRECT_STATE);

return mResolver.DiscoverOperational(filter, *mContext);
}

CHIP_ERROR ResolverProxy::StopDiscovery()
{
VerifyOrReturnError(mContext != nullptr, CHIP_ERROR_INCORRECT_STATE);
Expand Down
9 changes: 9 additions & 0 deletions src/lib/dnssd/ResolverProxy.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,17 @@ class ResolverProxy
}
}

void SetOperationalBrowseDelegate(OperationalBrowseDelegate * delegate)
{
if (mContext != nullptr)
{
mContext->SetOperationalBrowseDelegate(delegate);
}
}

CHIP_ERROR DiscoverCommissionableNodes(DiscoveryFilter filter = DiscoveryFilter());
CHIP_ERROR DiscoverCommissioners(DiscoveryFilter filter = DiscoveryFilter());
CHIP_ERROR DiscoverOperational(DiscoveryFilter filter = DiscoveryFilter());
CHIP_ERROR StopDiscovery();

private:
Expand Down
21 changes: 20 additions & 1 deletion src/lib/dnssd/Resolver_ImplMinimalMdns.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
*/

#include "Resolver.h"
#include "lib/dnssd/minimal_mdns/core/Constants.h"

#include <cstddef>
#include <limits>

#include <lib/core/CHIPConfig.h>
Expand Down Expand Up @@ -283,17 +285,20 @@ class MinMdnsResolver : public Resolver, public MdnsPacketDelegate
bool IsInitialized() override;
void Shutdown() override;
void SetOperationalDelegate(OperationalResolveDelegate * delegate) override { mOperationalDelegate = delegate; }

CHIP_ERROR ResolveNodeId(const PeerId & peerId) override;
void NodeIdResolutionNoLongerNeeded(const PeerId & peerId) override;
CHIP_ERROR DiscoverCommissionableNodes(DiscoveryFilter filter, DiscoveryContext & context) override;
CHIP_ERROR DiscoverCommissioners(DiscoveryFilter filter, DiscoveryContext & context) override;
CHIP_ERROR DiscoverOperational(DiscoveryFilter filter, DiscoveryContext & context) override;
CHIP_ERROR StopDiscovery(DiscoveryContext & context) override;
CHIP_ERROR ReconfirmRecord(const char * hostname, Inet::IPAddress address, Inet::InterfaceId interfaceId) override;

private:
OperationalResolveDelegate * mOperationalDelegate = nullptr;
DiscoveryContext * mDiscoveryContext = nullptr;
System::Layer * mSystemLayer = nullptr;

ActiveResolveAttempts mActiveResolves;
PacketParser mPacketParser;

Expand Down Expand Up @@ -440,14 +445,19 @@ void MinMdnsResolver::AdvancePendingResolverStates()
{
MATTER_TRACE_SCOPE("Active operational delegate call", "MinMdnsResolver");
ResolvedNodeData nodeData;

CHIP_ERROR err = resolver->Take(nodeData);
if (err != CHIP_NO_ERROR)
{
ChipLogError(Discovery, "Failed to take discovery result: %" CHIP_ERROR_FORMAT, err.Format());
}

mActiveResolves.Complete(nodeData.operationalData.peerId);

if (mDiscoveryContext != nullptr)
{
mDiscoveryContext->OnOperationalNodeDiscovered(nodeData.operationalData);
}

if (mOperationalDelegate != nullptr)
{
mOperationalDelegate->OnOperationalNodeResolved(nodeData);
Expand Down Expand Up @@ -701,6 +711,15 @@ CHIP_ERROR MinMdnsResolver::DiscoverCommissioners(DiscoveryFilter filter, Discov
return BrowseNodes(DiscoveryType::kCommissionerNode, filter);
}


CHIP_ERROR MinMdnsResolver::DiscoverOperational(DiscoveryFilter filter, DiscoveryContext & context)
{
// minmdns currently supports only one discovery context at a time so override the previous context
SetDiscoveryContext(&context);

return BrowseNodes(DiscoveryType::kOperational, filter);
}

CHIP_ERROR MinMdnsResolver::StopDiscovery(DiscoveryContext & context)
{
SetDiscoveryContext(nullptr);
Expand Down
6 changes: 6 additions & 0 deletions src/lib/dnssd/Resolver_ImplNone.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,17 @@ class NoneResolver : public Resolver
{
return CHIP_ERROR_NOT_IMPLEMENTED;
}

CHIP_ERROR DiscoverCommissioners(DiscoveryFilter filter, DiscoveryContext & context) override
{
return CHIP_ERROR_NOT_IMPLEMENTED;
}
CHIP_ERROR DiscoverOperational(DiscoveryFilter filter, DiscoveryContext & context) override
{
return CHIP_ERROR_NOT_IMPLEMENTED;
}
CHIP_ERROR StopDiscovery(DiscoveryContext & context) 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
18 changes: 18 additions & 0 deletions src/lib/shell/commands/Dns.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,21 @@ CHIP_ERROR BrowseCommissionerHandler(int argc, char ** argv)
return sResolverProxy.DiscoverCommissioners(filter);
}

CHIP_ERROR BrowseOpertionalHandler(int argc, char ** argv)
{
Dnssd::DiscoveryFilter filter;

if (!ParseSubType(argc, argv, filter))
{
streamer_printf(streamer_get(), "Invalid argument\r\n");
return CHIP_ERROR_INVALID_ARGUMENT;
}

streamer_printf(streamer_get(), "Browsing operational nodes...\r\n");

return sResolverProxy.DiscoverOperational(filter);
}

CHIP_ERROR BrowseHandler(int argc, char ** argv)
{
if (argc == 0)
Expand Down Expand Up @@ -262,6 +277,9 @@ void RegisterDnsCommands()
"Browse Matter commissionable nodes. Usage: dns browse commissionable [subtype]" },
{ &BrowseCommissionerHandler, "commissioner",
"Browse Matter commissioner nodes. Usage: dns browse commissioner [subtype]" },
{ &BrowseOpertionalHandler, "operational",
"Browse Matter all operationional nodes. Usage: dns browse operational [subtype]" },

};

static const shell_command_t sDnsSubCommands[] = {
Expand Down

0 comments on commit b8f80db

Please sign in to comment.