Skip to content

Commit a49c1f5

Browse files
authored
[mle] introduce ComposeMeshLocalAddress() helper method (openthread#13225)
This commit introduces a new `ComposeMeshLocalAddress()` helper method in the `Mle` class. This method constructs a full IPv6 Address by combining the Mesh-Local Prefix with a provided Interface Identifier (IID). By using this helper, we eliminate the repetitive two-step process of calling `SetPrefix()` followed by `SetIid()` previously scattered across `Commissioner`, `AddressResolver`, `Child`, and unit tests. This improves code readability and correctly encapsulates address composition logic within the `Mle` module.
1 parent c238bd9 commit a49c1f5

6 files changed

Lines changed: 18 additions & 9 deletions

File tree

src/core/meshcop/commissioner.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -858,8 +858,7 @@ template <> void Commissioner::HandleTmf<kUriRelayRx>(Coap::Msg &aMsg)
858858
aMsg.mMessage.SetOffset(offsetRange.GetOffset());
859859
SuccessOrExit(error = aMsg.mMessage.SetLength(offsetRange.GetEndOffset()));
860860

861-
joinerMessageInfo.SetPeerAddr(Get<Mle::Mle>().GetMeshLocalEid());
862-
joinerMessageInfo.GetPeerAddr().SetIid(mJoinerIid);
861+
Get<Mle::Mle>().ComposeMeshLocalAddress(mJoinerIid, joinerMessageInfo.GetPeerAddr());
863862
joinerMessageInfo.SetPeerPort(mJoinerPort);
864863

865864
Get<Tmf::SecureAgent>().HandleReceive(aMsg.mMessage, joinerMessageInfo);

src/core/thread/address_resolver.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,7 @@ Error AddressResolver::GetNextCacheEntry(EntryInfo &aInfo, Iterator &aIterator)
123123
VerifyOrExit(entry->IsLastTransactionTimeValid());
124124

125125
aInfo.mLastTransTime = entry->GetLastTransactionTime();
126-
AsCoreType(&aInfo.mMeshLocalEid).SetPrefix(Get<Mle::Mle>().GetMeshLocalPrefix());
127-
AsCoreType(&aInfo.mMeshLocalEid).SetIid(entry->GetMeshLocalIid());
126+
Get<Mle::Mle>().ComposeMeshLocalAddress(entry->GetMeshLocalIid(), AsCoreType(&aInfo.mMeshLocalEid));
128127

129128
ExitNow();
130129
}

src/core/thread/child.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,7 @@ Error Child::GetMeshLocalIp6Address(Ip6::Address &aAddress) const
110110

111111
VerifyOrExit(!mMeshLocalIid.IsUnspecified(), error = kErrorNotFound);
112112

113-
aAddress.SetPrefix(Get<Mle::Mle>().GetMeshLocalPrefix());
114-
aAddress.SetIid(mMeshLocalIid);
113+
Get<Mle::Mle>().ComposeMeshLocalAddress(mMeshLocalIid, aAddress);
115114

116115
exit:
117116
return error;

src/core/thread/mle.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -902,6 +902,12 @@ void Mle::ComposeServiceAloc(uint8_t aServiceId, Ip6::Address &aAddress) const
902902
ComposeAloc(Aloc16::FromServiceId(aServiceId), aAddress);
903903
}
904904

905+
void Mle::ComposeMeshLocalAddress(const Ip6::InterfaceIdentifier &aIid, Ip6::Address &aAddress) const
906+
{
907+
aAddress.SetPrefix(mMeshLocalPrefix);
908+
aAddress.SetIid(aIid);
909+
}
910+
905911
const LeaderData &Mle::GetLeaderData(void)
906912
{
907913
mLeaderData.SetDataVersion(Get<NetworkData::Leader>().GetVersion(NetworkData::kFullSet));

src/core/thread/mle.hpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -611,6 +611,14 @@ class Mle : public InstanceLocator, private NonCopyable
611611
*/
612612
void ComposeServiceAloc(uint8_t aServiceId, Ip6::Address &aAddress) const;
613613

614+
/**
615+
* Composes a Mesh-Local IPv6 Address from a given Interface Identifier.
616+
*
617+
* @param[in] aIid The Interface Identifier to use.
618+
* @param[out] aAddress A reference to the IPv6 Address to populate.
619+
*/
620+
void ComposeMeshLocalAddress(const Ip6::InterfaceIdentifier &aIid, Ip6::Address &aAddress) const;
621+
614622
/**
615623
* Returns the most recently received Leader Data.
616624
*

tests/unit/test_child.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,9 +123,7 @@ void TestChildIp6Address(void)
123123
numAddresses = 0;
124124

125125
// First addresses uses the mesh local prefix (mesh-local address).
126-
addresses[numAddresses] = sInstance->Get<Mle::Mle>().GetMeshLocalEid();
127-
addresses[numAddresses].SetIid(meshLocalIid);
128-
126+
sInstance->Get<Mle::Mle>().ComposeMeshLocalAddress(meshLocalIid, addresses[numAddresses]);
129127
numAddresses++;
130128

131129
for (const char *ip6Address : ip6Addresses)

0 commit comments

Comments
 (0)