[nexus] simplify node lookup using LinkedList matching methods (#12849)

This commit updates the address-based node lookup methods in `Core`
to use the `FindMatching()` and `ContainsMatching()` methods provided
by the `LinkedList` class. This replaces manual `for` loops with
cleaner, built-in list operations.

To facilitate this, a new `AddressNetif` enum and a `Matches()` method
are added to the `Node` class. The `Matches()` method accepts an
`Ip6::Address` and an `AddressNetif` indicator, allowing it to check
if the node has the specified address on its Thread interface, its
Infrastructure interface, or any.

Additionally, a `const` overload for the `Get()` template method is
added to the `Node` class to ensure proper const-correctness.
This commit is contained in:
Abtin Keshavarzian
2026-04-07 13:23:25 -05:00
committed by GitHub
parent a24e841ad2
commit a44970bdb4
3 changed files with 40 additions and 34 deletions
+7 -33
View File
@@ -667,48 +667,22 @@ void Core::ProcessInfraIf(Node &aNode)
Node *Core::FindNodeByAddress(const Ip6::Address &aAddress)
{
Node *matchedNode = FindNodeByThreadAddress(aAddress);
if (matchedNode == nullptr)
{
matchedNode = FindNodeByInfraIfAddress(aAddress);
}
return matchedNode;
return mNodes.FindMatching(aAddress, Node::kAnyNetifAddress);
}
bool Core::IsThreadAddress(const Ip6::Address &aAddress) { return FindNodeByThreadAddress(aAddress) != nullptr; }
bool Core::IsThreadAddress(const Ip6::Address &aAddress)
{
return mNodes.ContainsMatching(aAddress, Node::kThreadNetifAddress);
}
Node *Core::FindNodeByThreadAddress(const Ip6::Address &aAddress)
{
Node *matchedNode = nullptr;
for (Node &node : mNodes)
{
if (node.Get<ThreadNetif>().HasUnicastAddress(aAddress))
{
matchedNode = &node;
break;
}
}
return matchedNode;
return mNodes.FindMatching(aAddress, Node::kThreadNetifAddress);
}
Node *Core::FindNodeByInfraIfAddress(const Ip6::Address &aAddress)
{
Node *matchedNode = nullptr;
for (Node &node : mNodes)
{
if (node.mInfraIf.HasAddress(aAddress))
{
matchedNode = &node;
break;
}
}
return matchedNode;
return mNodes.FindMatching(aAddress, Node::kInfraNetifAddress);
}
//---------------------------------------------------------------------------------------------------------------------
+22
View File
@@ -229,5 +229,27 @@ const Ip6::Address &Node::FindGlobalAddress(void)
return *matchedAddress;
}
bool Node::Matches(const Ip6::Address &aAddress, AddressNetif aNetif) const
{
bool matches = false;
switch (aNetif)
{
case kThreadNetifAddress:
matches = Get<ThreadNetif>().HasUnicastAddress(aAddress);
break;
case kInfraNetifAddress:
matches = mInfraIf.HasAddress(aAddress);
break;
case kAnyNetifAddress:
matches = Get<ThreadNetif>().HasUnicastAddress(aAddress) || mInfraIf.HasAddress(aAddress);
break;
}
return matches;
}
} // namespace Nexus
} // namespace ot
+11 -1
View File
@@ -120,13 +120,23 @@ public:
*/
const Ip6::Address &FindGlobalAddress(void);
enum AddressNetif : uint8_t
{
kThreadNetifAddress,
kInfraNetifAddress,
kAnyNetifAddress,
};
bool Matches(const Ip6::Address &aAddress, AddressNetif aNetif) const;
void SetName(const char *aName) { mName.Clear().Append("%s", aName); }
void SetName(const char *aPrefix, uint16_t aIndex);
const char *GetName(void) const { return mName.AsCString(); }
//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
template <typename Type> Type &Get(void) { return Instance::Get<Type>(); }
template <typename Type> Type &Get(void) { return Instance::Get<Type>(); }
template <typename Type> const Type &Get(void) const { return AsConst(AsNonConst(this)->Get<Type>()); }
Instance &GetInstance(void) { return *this; }