From f560204bcce3cccfbe6a818a06ba22f0e53711cb Mon Sep 17 00:00:00 2001 From: Abtin Keshavarzian Date: Mon, 29 Sep 2025 18:08:40 -0700 Subject: [PATCH] [trel] simplify `Peer::Matches()` for name matching (#11976) Consolidates the two overloaded `Peer::Matches()` methods for service and host name matching into a single method. This change introduces a new `NameMatchType` enum, which is used to specify whether to match against a service name or a host name. This removes the need for the now-unnecessary `ServiceNameMatcher` and `HostNameMatcher` structs, simplifying the calling code and the `Peer` class. --- src/core/radio/trel_peer.cpp | 21 +++++++++++++++++---- src/core/radio/trel_peer.hpp | 23 ++++------------------- src/core/radio/trel_peer_discoverer.cpp | 12 ++++++------ 3 files changed, 27 insertions(+), 29 deletions(-) diff --git a/src/core/radio/trel_peer.cpp b/src/core/radio/trel_peer.cpp index 9563a1269..0e76f04d9 100644 --- a/src/core/radio/trel_peer.cpp +++ b/src/core/radio/trel_peer.cpp @@ -222,11 +222,24 @@ exit: return; } -bool Peer::Matches(const ServiceNameMatcher &aMatcher) const { return NameMatch(mServiceName, aMatcher.mServiceName); } - -bool Peer::Matches(const HostNameMatcher &aMatcher) const +bool Peer::Matches(NameMatchType aType, const char *aName) const { - return mResolvingHost && NameMatch(mHostName, aMatcher.mHostName); + bool matches = false; + + switch (aType) + { + case kMatchServiceName: + matches = NameMatch(mServiceName, aName); + break; + + case kMatchHostName: + VerifyOrExit(mResolvingHost); + matches = NameMatch(mHostName, aName); + break; + } + +exit: + return matches; } bool Peer::NameMatch(const Heap::String &aHeapString, const char *aName) diff --git a/src/core/radio/trel_peer.hpp b/src/core/radio/trel_peer.hpp index 7a9888a5f..19d757414 100644 --- a/src/core/radio/trel_peer.hpp +++ b/src/core/radio/trel_peer.hpp @@ -226,24 +226,10 @@ private: }; #if OPENTHREAD_CONFIG_TREL_MANAGE_DNSSD_ENABLE - struct ServiceNameMatcher + enum NameMatchType : uint8_t { - explicit ServiceNameMatcher(const char *SrerviceName) - : mServiceName(SrerviceName) - { - } - - const char *mServiceName; - }; - - struct HostNameMatcher - { - explicit HostNameMatcher(const char *aHostName) - : mHostName(aHostName) - { - } - - const char *mHostName; + kMatchServiceName, + kMatchHostName, }; class AddressArray : public Heap::Array @@ -271,8 +257,7 @@ private: #if OPENTHREAD_CONFIG_TREL_MANAGE_DNSSD_ENABLE void SetPort(uint16_t aPort); - bool Matches(const ServiceNameMatcher &aMatcher) const; - bool Matches(const HostNameMatcher &aMatcher) const; + bool Matches(NameMatchType aType, const char *aName) const; void SignalPeerRemoval(void); static bool NameMatch(const Heap::String &aHeapString, const char *aName); diff --git a/src/core/radio/trel_peer_discoverer.cpp b/src/core/radio/trel_peer_discoverer.cpp index c1a71f43a..ea3a3ffba 100644 --- a/src/core/radio/trel_peer_discoverer.cpp +++ b/src/core/radio/trel_peer_discoverer.cpp @@ -340,7 +340,7 @@ void PeerDiscoverer::HandleBrowseResult(const Dnssd::BrowseResult &aResult) VerifyOrExit(IsRunning()); - peer = Get().FindMatching(Peer::ServiceNameMatcher(aResult.mServiceInstance)); + peer = Get().FindMatching(Peer::kMatchServiceName, aResult.mServiceInstance); if (aResult.mTtl == 0) { @@ -416,7 +416,7 @@ void PeerDiscoverer::HandleSrvResult(const Dnssd::SrvResult &aResult) VerifyOrExit(IsRunning()); - peer = Get().FindMatching(Peer::ServiceNameMatcher(aResult.mServiceInstance)); + peer = Get().FindMatching(Peer::kMatchServiceName, aResult.mServiceInstance); VerifyOrExit(peer != nullptr); if (aResult.mTtl == 0) @@ -455,7 +455,7 @@ void PeerDiscoverer::HandleTxtResult(const Dnssd::TxtResult &aResult) VerifyOrExit(IsRunning()); - peer = Get().FindMatching(Peer::ServiceNameMatcher(aResult.mServiceInstance)); + peer = Get().FindMatching(Peer::kMatchServiceName, aResult.mServiceInstance); VerifyOrExit(peer != nullptr); peer->mTxtDataValidated = false; @@ -502,7 +502,7 @@ void PeerDiscoverer::StartHostAddressResolver(Peer &aPeer) VerifyOrExit(!aPeer.mResolvingHost); - sameHostPeer = Get().FindMatching(Peer::HostNameMatcher(aPeer.mHostName.AsCString())); + sameHostPeer = Get().FindMatching(Peer::kMatchHostName, aPeer.mHostName.AsCString()); aPeer.mResolvingHost = true; @@ -530,7 +530,7 @@ void PeerDiscoverer::StopHostAddressResolver(Peer &aPeer) aPeer.mResolvingHost = false; aPeer.mHostAddresses.Free(); - VerifyOrExit(!Get().ContainsMatching(Peer::HostNameMatcher(aPeer.mHostName.AsCString()))); + VerifyOrExit(!Get().ContainsMatching(Peer::kMatchHostName, aPeer.mHostName.AsCString())); Get().StopIp6AddressResolver(AddressResolver(aPeer)); @@ -613,7 +613,7 @@ void PeerDiscoverer::HandleAddressResult(const Dnssd::AddressResult &aResult) continue; } - if (peer.Matches(Peer::HostNameMatcher(aResult.mHostName))) + if (peer.Matches(Peer::kMatchHostName, aResult.mHostName)) { UpdatePeerAddresses(peer, sortedAddresses); UpdatePeerState(peer);