[mle-types] add MeshLocalPrefix class (#4679)

This commit is contained in:
Abtin Keshavarzian
2020-03-13 22:06:04 -07:00
committed by Jonathan Hui
parent 052047a15f
commit ab81a256e4
11 changed files with 100 additions and 41 deletions
+7 -9
View File
@@ -67,18 +67,16 @@ const otExtendedPanId *otThreadGetExtendedPanId(otInstance *aInstance)
otError otThreadSetExtendedPanId(otInstance *aInstance, const otExtendedPanId *aExtendedPanId)
{
otError error = OT_ERROR_NONE;
Instance & instance = *static_cast<Instance *>(aInstance);
otMeshLocalPrefix prefix;
otError error = OT_ERROR_NONE;
Instance & instance = *static_cast<Instance *>(aInstance);
const Mac::ExtendedPanId &extPanId = *static_cast<const Mac::ExtendedPanId *>(aExtendedPanId);
Mle::MeshLocalPrefix prefix;
VerifyOrExit(instance.Get<Mle::MleRouter>().GetRole() == OT_DEVICE_ROLE_DISABLED, error = OT_ERROR_INVALID_STATE);
instance.Get<Mac::Mac>().SetExtendedPanId(*static_cast<const Mac::ExtendedPanId *>(aExtendedPanId));
instance.Get<Mac::Mac>().SetExtendedPanId(extPanId);
prefix.m8[0] = 0xfd;
memcpy(&prefix.m8[1], aExtendedPanId->m8, 5);
prefix.m8[6] = 0x00;
prefix.m8[7] = 0x00;
prefix.SetFromExtendedPanId(extPanId);
instance.Get<Mle::MleRouter>().SetMeshLocalPrefix(prefix);
instance.Get<MeshCoP::ActiveDataset>().Clear();
@@ -166,7 +164,7 @@ otError otThreadSetMeshLocalPrefix(otInstance *aInstance, const otMeshLocalPrefi
VerifyOrExit(instance.Get<Mle::MleRouter>().GetRole() == OT_DEVICE_ROLE_DISABLED, error = OT_ERROR_INVALID_STATE);
instance.Get<Mle::MleRouter>().SetMeshLocalPrefix(*aMeshLocalPrefix);
instance.Get<Mle::MleRouter>().SetMeshLocalPrefix(*static_cast<const Mle::MeshLocalPrefix *>(aMeshLocalPrefix));
instance.Get<MeshCoP::ActiveDataset>().Clear();
instance.Get<MeshCoP::PendingDataset>().Clear();
+1 -1
View File
@@ -311,7 +311,7 @@ otError Dataset::Set(const otOperationalDataset &aDataset)
{
MeshCoP::MeshLocalPrefixTlv tlv;
tlv.Init();
tlv.SetMeshLocalPrefix(aDataset.mMeshLocalPrefix);
tlv.SetMeshLocalPrefix(static_cast<const Mle::MeshLocalPrefix &>(aDataset.mMeshLocalPrefix));
Set(tlv);
}
+4 -3
View File
@@ -51,6 +51,7 @@
#include "net/ip6_address.hpp"
#include "radio/radio.hpp"
#include "thread/key_manager.hpp"
#include "thread/mle_types.hpp"
namespace ot {
namespace MeshCoP {
@@ -604,7 +605,7 @@ public:
* @returns The Mesh Local Prefix value.
*
*/
const otMeshLocalPrefix &GetMeshLocalPrefix(void) const { return mMeshLocalPrefix; }
const Mle::MeshLocalPrefix &GetMeshLocalPrefix(void) const { return mMeshLocalPrefix; }
/**
* This method sets the Mesh Local Prefix value.
@@ -612,10 +613,10 @@ public:
* @param[in] aMeshLocalPrefix A pointer to the Mesh Local Prefix value.
*
*/
void SetMeshLocalPrefix(const otMeshLocalPrefix &aMeshLocalPrefix) { mMeshLocalPrefix = aMeshLocalPrefix; }
void SetMeshLocalPrefix(const Mle::MeshLocalPrefix &aMeshLocalPrefix) { mMeshLocalPrefix = aMeshLocalPrefix; }
private:
otMeshLocalPrefix mMeshLocalPrefix;
Mle::MeshLocalPrefix mMeshLocalPrefix;
} OT_TOOL_PACKED_END;
/**
+12 -2
View File
@@ -40,6 +40,7 @@
#include "common/string.hpp"
#include "mac/mac_types.hpp"
#include "thread/mle_types.hpp"
namespace ot {
namespace Ip6 {
@@ -77,8 +78,6 @@ public:
{
kInterfaceIdentifierSize = 8, ///< Interface Identifier size in bytes.
kIp6AddressStringSize = 40, ///< Max buffer size in bytes to store an IPv6 address in string format.
kMeshLocalPrefixLength = 64, ///< Length of Thread mesh local prefix.
kMeshLocalPrefixSize = 8, ///< Mesh local prefix size in bytes.
};
/**
@@ -282,6 +281,17 @@ public:
*/
void SetPrefix(const uint8_t *aPrefix, uint8_t aPrefixLength);
/**
* This method sets the IPv6 address prefix to the given Mesh Local Prefix.
*
* @param[in] aMeshLocalPrefix A Mesh Local Prefix.
*
*/
void SetPrefix(const Mle::MeshLocalPrefix &aMeshLocalPrefix)
{
SetPrefix(aMeshLocalPrefix.m8, Mle::MeshLocalPrefix::kLength);
}
/**
* This method returns a pointer to the Interface Identifier.
*
+11 -18
View File
@@ -114,7 +114,7 @@ Mle::Mle(Instance &aInstance)
, mParentResponseCb(NULL)
, mParentResponseCbContext(NULL)
{
otMeshLocalPrefix meshLocalPrefix;
MeshLocalPrefix meshLocalPrefix;
mParent.Init(aInstance);
mParentCandidate.Init(aInstance);
@@ -158,10 +158,7 @@ Mle::Mle(Instance &aInstance)
#endif
// initialize Mesh Local Prefix
meshLocalPrefix.m8[0] = 0xfd;
memcpy(&meshLocalPrefix.m8[1], Get<Mac::Mac>().GetExtendedPanId().m8, 5);
meshLocalPrefix.m8[6] = 0x00;
meshLocalPrefix.m8[7] = 0x00;
meshLocalPrefix.SetFromExtendedPanId(Get<Mac::Mac>().GetExtendedPanId());
// mesh-local 64
mMeshLocal64.Clear();
@@ -897,13 +894,9 @@ void Mle::UpdateLinkLocalAddress(void)
Get<Notifier>().Signal(OT_CHANGED_THREAD_LL_ADDR);
}
void Mle::SetMeshLocalPrefix(const otMeshLocalPrefix &aMeshLocalPrefix)
void Mle::SetMeshLocalPrefix(const MeshLocalPrefix &aMeshLocalPrefix)
{
if (memcmp(mMeshLocal64.GetAddress().mFields.m8, aMeshLocalPrefix.m8, sizeof(aMeshLocalPrefix)) == 0)
{
Get<Notifier>().SignalIfFirst(OT_CHANGED_THREAD_ML_ADDR);
ExitNow();
}
VerifyOrExit(GetMeshLocalPrefix() != aMeshLocalPrefix, Get<Notifier>().SignalIfFirst(OT_CHANGED_THREAD_ML_ADDR));
if (Get<ThreadNetif>().IsUp())
{
@@ -915,9 +908,9 @@ void Mle::SetMeshLocalPrefix(const otMeshLocalPrefix &aMeshLocalPrefix)
Get<ThreadNetif>().UnsubscribeMulticast(mRealmLocalAllThreadNodes);
}
mMeshLocal64.GetAddress().SetPrefix(aMeshLocalPrefix.m8, Ip6::Address::kMeshLocalPrefixLength);
mMeshLocal16.GetAddress().SetPrefix(aMeshLocalPrefix.m8, Ip6::Address::kMeshLocalPrefixLength);
mLeaderAloc.GetAddress().SetPrefix(aMeshLocalPrefix.m8, Ip6::Address::kMeshLocalPrefixLength);
mMeshLocal64.GetAddress().SetPrefix(aMeshLocalPrefix);
mMeshLocal16.GetAddress().SetPrefix(aMeshLocalPrefix);
mLeaderAloc.GetAddress().SetPrefix(aMeshLocalPrefix);
// Just keep mesh local prefix if network interface is down
VerifyOrExit(Get<ThreadNetif>().IsUp());
@@ -937,7 +930,7 @@ void Mle::ApplyMeshLocalPrefix(void)
if (HostSwap16(mServiceAlocs[i].GetAddress().mFields.m16[7]) != Mac::kShortAddrInvalid)
{
Get<ThreadNetif>().RemoveUnicastAddress(mServiceAlocs[i]);
mServiceAlocs[i].GetAddress().SetPrefix(GetMeshLocalPrefix().m8, Ip6::Address::kMeshLocalPrefixLength);
mServiceAlocs[i].GetAddress().SetPrefix(GetMeshLocalPrefix());
Get<ThreadNetif>().AddUnicastAddress(mServiceAlocs[i]);
}
}
@@ -1033,7 +1026,7 @@ otError Mle::GetLeaderAddress(Ip6::Address &aAddress) const
VerifyOrExit(GetRloc16() != Mac::kShortAddrInvalid, error = OT_ERROR_DETACHED);
aAddress.SetPrefix(GetMeshLocalPrefix().m8, Ip6::Address::kMeshLocalPrefixLength);
aAddress.SetPrefix(GetMeshLocalPrefix());
aAddress.mFields.m16[4] = HostSwap16(0x0000);
aAddress.mFields.m16[5] = HostSwap16(0x00ff);
aAddress.mFields.m16[6] = HostSwap16(0xfe00);
@@ -1062,7 +1055,7 @@ otError Mle::GetServiceAloc(uint8_t aServiceId, Ip6::Address &aAddress) const
VerifyOrExit(GetRloc16() != Mac::kShortAddrInvalid, error = OT_ERROR_DETACHED);
aAddress.SetPrefix(GetMeshLocalPrefix().m8, Ip6::Address::kMeshLocalPrefixLength);
aAddress.SetPrefix(GetMeshLocalPrefix());
aAddress.mFields.m16[4] = HostSwap16(0x0000);
aAddress.mFields.m16[5] = HostSwap16(0x00ff);
aAddress.mFields.m16[6] = HostSwap16(0xfe00);
@@ -3971,7 +3964,7 @@ bool Mle::IsAnycastLocator(const Ip6::Address &aAddress) const
bool Mle::IsMeshLocalAddress(const Ip6::Address &aAddress) const
{
return aAddress.PrefixMatch(GetMeshLocal16()) >= Ip6::Address::kMeshLocalPrefixLength;
return aAddress.PrefixMatch(GetMeshLocal16()) >= MeshLocalPrefix::kLength;
}
otError Mle::CheckReachability(uint16_t aMeshSource, uint16_t aMeshDest, Ip6::Header &aIp6Header)
+3 -3
View File
@@ -645,9 +645,9 @@ public:
* @returns A reference to the Mesh Local Prefix.
*
*/
const otMeshLocalPrefix &GetMeshLocalPrefix(void) const
const MeshLocalPrefix &GetMeshLocalPrefix(void) const
{
return reinterpret_cast<const otMeshLocalPrefix &>(mMeshLocal16.GetAddress());
return reinterpret_cast<const MeshLocalPrefix &>(mMeshLocal16.GetAddress());
}
/**
@@ -656,7 +656,7 @@ public:
* @param[in] aMeshLocalPrefix A reference to the Mesh Local Prefix.
*
*/
void SetMeshLocalPrefix(const otMeshLocalPrefix &aMeshLocalPrefix);
void SetMeshLocalPrefix(const MeshLocalPrefix &aMeshLocalPrefix);
/**
* This method applies the Mesh Local Prefix.
+8
View File
@@ -62,5 +62,13 @@ DeviceMode::InfoString DeviceMode::ToString(void) const
IsFullNetworkData() ? "yes" : "no");
}
void MeshLocalPrefix::SetFromExtendedPanId(const Mac::ExtendedPanId &aExtendedPanId)
{
m8[0] = 0xfd;
memcpy(&m8[1], aExtendedPanId.m8, 5);
m8[6] = 0x00;
m8[7] = 0x00;
}
} // namespace Mle
} // namespace ot
+48
View File
@@ -36,12 +36,14 @@
#include "openthread-core-config.h"
#include <limits.h>
#include <stdint.h>
#include <string.h>
#include <openthread/thread.h>
#include "common/string.hpp"
#include "mac/mac_types.hpp"
namespace ot {
namespace Mle {
@@ -403,6 +405,52 @@ private:
uint8_t mMode;
};
/**
* This class represents a Mesh Local Prefix.
*
*/
OT_TOOL_PACKED_BEGIN
class MeshLocalPrefix : public otMeshLocalPrefix
{
public:
enum
{
kSize = OT_MESH_LOCAL_PREFIX_SIZE, ///< Size in bytes.
kLength = OT_MESH_LOCAL_PREFIX_SIZE * CHAR_BIT, ///< Length of Mesh Local Prefix in bits.
};
/**
* This method evaluates whether or not two Mesh Local Prefixes match.
*
* @param[in] aOther The Mesh Local Prefix to compare.
*
* @retval TRUE If the Mesh Local Prefixes match.
* @retval FALSE If the Mesh Local Prefixes do not match.
*
*/
bool operator==(const MeshLocalPrefix &aOther) const { return memcmp(m8, aOther.m8, sizeof(*this)) == 0; }
/**
* This method evaluates whether or not two Mesh Local Prefixes match.
*
* @param[in] aOther The Mesh Local Prefix to compare.
*
* @retval TRUE If the Mesh Local Prefixes do not match.
* @retval FALSE If the Mesh Local Prefixes match.
*
*/
bool operator!=(const MeshLocalPrefix &aOther) const { return !(*this == aOther); }
/**
* This method derives and sets the Mesh Local Prefix from an Extended PAN ID.
*
* @param[in] aExtendedPanId An Extended PAN ID.
*
*/
void SetFromExtendedPanId(const Mac::ExtendedPanId &aExtendedPanId);
} OT_TOOL_PACKED_END;
/**
* This class represents the Thread Leader Data.
*
+2 -1
View File
@@ -39,6 +39,7 @@
#include "common/locator-getters.hpp"
#include "common/logging.hpp"
#include "mac/mac_types.hpp"
#include "thread/mle_types.hpp"
#include "thread/thread_netif.hpp"
#if OPENTHREAD_CONFIG_BORDER_ROUTER_ENABLE || OPENTHREAD_CONFIG_TMF_NETDATA_SERVICE_ENABLE
@@ -68,7 +69,7 @@ otError Local::AddOnMeshPrefix(const uint8_t *aPrefix, uint8_t aPrefixLength, in
error = OT_ERROR_INVALID_ARGS);
VerifyOrExit(Ip6::Address::PrefixMatch(aPrefix, Get<Mle::MleRouter>().GetMeshLocalPrefix().m8, prefixLengthBytes) <
Ip6::Address::kMeshLocalPrefixLength,
Mle::MeshLocalPrefix::kLength,
error = OT_ERROR_INVALID_ARGS);
RemoveOnMeshPrefix(aPrefix, aPrefixLength);
+1 -2
View File
@@ -152,8 +152,7 @@ otError Child::GetMeshLocalIp6Address(Ip6::Address &aAddress) const
VerifyOrExit(!IsAllZero(mMeshLocalIid, sizeof(mMeshLocalIid)), error = OT_ERROR_NOT_FOUND);
memcpy(aAddress.mFields.m8, Get<Mle::MleRouter>().GetMeshLocalPrefix().m8, Ip6::Address::kMeshLocalPrefixSize);
aAddress.SetPrefix(Get<Mle::MleRouter>().GetMeshLocalPrefix());
aAddress.SetIid(mMeshLocalIid);
exit:
+3 -2
View File
@@ -106,10 +106,11 @@ void TestIphcVector::GetUncompressedStream(Message &aMessage)
* This function initializes Thread Interface.
*
*/
static void Init()
static void Init(void)
{
otMeshLocalPrefix meshLocalPrefix = {{0xfd, 0x00, 0xca, 0xfe, 0xfa, 0xce, 0x12, 0x34}};
sInstance->Get<Mle::MleRouter>().SetMeshLocalPrefix(meshLocalPrefix);
sInstance->Get<Mle::MleRouter>().SetMeshLocalPrefix(static_cast<Mle::MeshLocalPrefix &>(meshLocalPrefix));
// Emulate global prefixes with contextes.
uint8_t mockNetworkData[] = {