From d009e77cd3d150cb8d4ee7b4cb3e7e48410719c5 Mon Sep 17 00:00:00 2001 From: Buke Po Date: Sat, 29 Jul 2017 09:40:00 +0800 Subject: [PATCH] [mac] decouple mac frame from ip6 address (#2039) --- src/core/mac/mac_frame.cpp | 7 ------- src/core/mac/mac_frame.hpp | 8 -------- src/core/net/ip6_address.cpp | 6 ++++++ src/core/net/ip6_address.hpp | 8 ++++++++ src/core/thread/mesh_forwarder.cpp | 4 ++-- src/core/thread/mle.cpp | 8 ++++---- src/core/thread/mle_router.cpp | 16 ++++++++-------- 7 files changed, 28 insertions(+), 29 deletions(-) diff --git a/src/core/mac/mac_frame.cpp b/src/core/mac/mac_frame.cpp index 3b3d73f3b..76dab953e 100644 --- a/src/core/mac/mac_frame.cpp +++ b/src/core/mac/mac_frame.cpp @@ -40,17 +40,10 @@ #include "common/code_utils.hpp" #include "common/debug.hpp" -#include "net/ip6_address.hpp" namespace ot { namespace Mac { -void ExtAddress::Set(const Ip6::Address &aIpAddress) -{ - memcpy(m8, aIpAddress.GetIid(), sizeof(m8)); - m8[0] ^= 0x02; -} - const char *Address::ToString(char *aBuf, uint16_t aSize) const { switch (mLength) diff --git a/src/core/mac/mac_frame.hpp b/src/core/mac/mac_frame.hpp index 8d90f4167..d52a43a63 100644 --- a/src/core/mac/mac_frame.hpp +++ b/src/core/mac/mac_frame.hpp @@ -131,14 +131,6 @@ public: } } - /** - * This method converts an IPv6 Interface Identifier to an IEEE 802.15.4 Extended Address. - * - * @param[in] aIpAddress A reference to the IPv6 address. - * - */ - void Set(const Ip6::Address &aIpAddress); - private: enum { diff --git a/src/core/net/ip6_address.cpp b/src/core/net/ip6_address.cpp index a63889ce1..325f30542 100644 --- a/src/core/net/ip6_address.cpp +++ b/src/core/net/ip6_address.cpp @@ -159,6 +159,12 @@ void Address::SetIid(const Mac::ExtAddress &aEui64) mFields.m8[kInterfaceIdentifierOffset] ^= 0x02; } +void Address::ToExtAddress(Mac::ExtAddress &aExtAddress) const +{ + memcpy(aExtAddress.m8, mFields.m8 + kInterfaceIdentifierOffset, sizeof(aExtAddress.m8)); + aExtAddress.m8[0] ^= 0x02; +} + uint8_t Address::GetScope(void) const { if (IsMulticast()) diff --git a/src/core/net/ip6_address.hpp b/src/core/net/ip6_address.hpp index 6988aa6e5..19ffe90d9 100644 --- a/src/core/net/ip6_address.hpp +++ b/src/core/net/ip6_address.hpp @@ -279,6 +279,14 @@ public: */ void SetIid(const Mac::ExtAddress &aEui64); + /** + * This method converts the IPv6 Interface Identifier to an IEEE 802.15.4 Extended Address. + * + * @param[out] aExtAddress A reference to the extended address. + * + */ + void ToExtAddress(Mac::ExtAddress &aExtAddress) const; + /** * This method returns the IPv6 address scope. * diff --git a/src/core/thread/mesh_forwarder.cpp b/src/core/thread/mesh_forwarder.cpp index ff22e3cd0..abfb577de 100644 --- a/src/core/thread/mesh_forwarder.cpp +++ b/src/core/thread/mesh_forwarder.cpp @@ -954,7 +954,7 @@ otError MeshForwarder::GetMacSourceAddress(const Ip6::Address &aIp6Addr, Mac::Ad ThreadNetif &netif = GetNetif(); aMacAddr.mLength = sizeof(aMacAddr.mExtAddress); - aMacAddr.mExtAddress.Set(aIp6Addr); + aIp6Addr.ToExtAddress(aMacAddr.mExtAddress); if (memcmp(&aMacAddr.mExtAddress, netif.GetMac().GetExtAddress(), sizeof(aMacAddr.mExtAddress)) != 0) { @@ -991,7 +991,7 @@ otError MeshForwarder::GetMacDestinationAddress(const Ip6::Address &aIp6Addr, Ma else { aMacAddr.mLength = sizeof(aMacAddr.mExtAddress); - aMacAddr.mExtAddress.Set(aIp6Addr); + aIp6Addr.ToExtAddress(aMacAddr.mExtAddress); } return OT_ERROR_NONE; diff --git a/src/core/thread/mle.cpp b/src/core/thread/mle.cpp index 4a7776387..a744b8292 100644 --- a/src/core/thread/mle.cpp +++ b/src/core/thread/mle.cpp @@ -2054,7 +2054,7 @@ void Mle::HandleUdpReceive(Message &aMessage, const Ip6::MessageInfo &aMessageIn VerifyOrExit(messageTagLength == sizeof(messageTag)); SuccessOrExit(aMessage.SetLength(aMessage.GetLength() - sizeof(messageTag))); - macAddr.Set(aMessageInfo.GetPeerAddr()); + aMessageInfo.GetPeerAddr().ToExtAddress(macAddr); GenerateNonce(macAddr, frameCounter, Mac::Frame::kSecEncMic32, nonce); aesCcm.SetKey(mleKey, 16); @@ -2261,7 +2261,7 @@ otError Mle::HandleAdvertisement(const Message &aMessage, const Ip6::MessageInfo SuccessOrExit(error = netif.GetMle().HandleAdvertisement(aMessage, aMessageInfo)); } - macAddr.Set(aMessageInfo.GetPeerAddr()); + aMessageInfo.GetPeerAddr().ToExtAddress(macAddr); isNeighbor = false; @@ -2653,7 +2653,7 @@ otError Mle::HandleParentResponse(const Message &aMessage, const Ip6::MessageInf memcpy(mChildIdRequest.mChallenge, challenge.GetChallenge(), challenge.GetLength()); mChildIdRequest.mChallengeLength = challenge.GetLength(); - mParentCandidate.GetExtAddress().Set(aMessageInfo.GetPeerAddr()); + aMessageInfo.GetPeerAddr().ToExtAddress(mParentCandidate.GetExtAddress()); mParentCandidate.SetRloc16(sourceAddress.GetRloc16()); mParentCandidate.SetLinkFrameCounter(linkFrameCounter.GetFrameCounter()); mParentCandidate.SetMleFrameCounter(mleFrameCounter.GetFrameCounter()); @@ -3068,7 +3068,7 @@ otError Mle::HandleDiscoveryResponse(const Message &aMessage, const Ip6::Message result.mChannel = threadMessageInfo->mChannel; result.mRssi = threadMessageInfo->mRss; result.mLqi = threadMessageInfo->mLqi; - static_cast(&result.mExtAddress)->Set(aMessageInfo.GetPeerAddr()); + aMessageInfo.GetPeerAddr().ToExtAddress(*static_cast(&result.mExtAddress)); // process MeshCoP TLVs while (offset < end) diff --git a/src/core/thread/mle_router.cpp b/src/core/thread/mle_router.cpp index 4138210da..3c4822aa3 100644 --- a/src/core/thread/mle_router.cpp +++ b/src/core/thread/mle_router.cpp @@ -676,7 +676,7 @@ otError MleRouter::HandleLinkRequest(const Message &aMessage, const Ip6::Message VerifyOrExit(mParentRequestState == kParentIdle, error = OT_ERROR_INVALID_STATE); - macAddr.Set(aMessageInfo.GetPeerAddr()); + aMessageInfo.GetPeerAddr().ToExtAddress(macAddr); // Challenge SuccessOrExit(error = Tlv::GetTlv(aMessage, Tlv::kChallenge, sizeof(challenge), challenge)); @@ -880,7 +880,7 @@ otError MleRouter::HandleLinkAccept(const Message &aMessage, const Ip6::MessageI ChallengeTlv challenge; TlvRequestTlv tlvRequest; - macAddr.Set(aMessageInfo.GetPeerAddr()); + aMessageInfo.GetPeerAddr().ToExtAddress(macAddr); // Version SuccessOrExit(error = Tlv::GetTlv(aMessage, Tlv::kVersion, sizeof(version), version)); @@ -1270,7 +1270,7 @@ otError MleRouter::HandleAdvertisement(const Message &aMessage, const Ip6::Messa uint8_t routerId; uint8_t routerCount; - macAddr.Set(aMessageInfo.GetPeerAddr()); + aMessageInfo.GetPeerAddr().ToExtAddress(macAddr); // Source Address SuccessOrExit(error = Tlv::GetTlv(aMessage, Tlv::kSourceAddress, sizeof(sourceAddress), sourceAddress)); @@ -1661,7 +1661,7 @@ otError MleRouter::HandleParentRequest(const Message &aMessage, const Ip6::Messa (mRouters[GetLeaderId()].GetCost() + GetLinkCost(mRouters[GetLeaderId()].GetNextHop()) < kMaxRouteCost), error = OT_ERROR_DROP); - macAddr.Set(aMessageInfo.GetPeerAddr()); + aMessageInfo.GetPeerAddr().ToExtAddress(macAddr); // Version SuccessOrExit(error = Tlv::GetTlv(aMessage, Tlv::kVersion, sizeof(version), version)); @@ -2049,7 +2049,7 @@ otError MleRouter::HandleChildIdRequest(const Message &aMessage, const Ip6::Mess VerifyOrExit(mRole >= OT_DEVICE_ROLE_CHILD, error = OT_ERROR_INVALID_STATE); // Find Child - macAddr.Set(aMessageInfo.GetPeerAddr()); + aMessageInfo.GetPeerAddr().ToExtAddress(macAddr); VerifyOrExit((child = FindChild(macAddr)) != NULL, error = OT_ERROR_ALREADY); @@ -2226,7 +2226,7 @@ otError MleRouter::HandleChildUpdateRequest(const Message &aMessage, const Ip6:: VerifyOrExit(mode.IsValid(), error = OT_ERROR_PARSE); // Find Child - macAddr.Set(aMessageInfo.GetPeerAddr()); + aMessageInfo.GetPeerAddr().ToExtAddress(macAddr); child = FindChild(macAddr); tlvs[tlvslength++] = Tlv::kSourceAddress; @@ -2333,7 +2333,7 @@ otError MleRouter::HandleChildUpdateResponse(const Message &aMessage, const Ip6: otLogInfoMle(GetInstance(), "Received Child Update Response from child"); // Find Child - macAddr.Set(aMessageInfo.GetPeerAddr()); + aMessageInfo.GetPeerAddr().ToExtAddress(macAddr); VerifyOrExit((child = FindChild(macAddr)) != NULL, error = OT_ERROR_NOT_FOUND); @@ -3275,7 +3275,7 @@ Neighbor *MleRouter::GetNeighbor(const Ip6::Address &aAddress) else { macaddr.mLength = sizeof(macaddr.mExtAddress); - macaddr.mExtAddress.Set(aAddress); + aAddress.ToExtAddress(macaddr.mExtAddress); } ExitNow(rval = GetNeighbor(macaddr));