diff --git a/src/core/mac/mac.cpp b/src/core/mac/mac.cpp index fd5c3763a..d5d7f6250 100644 --- a/src/core/mac/mac.cpp +++ b/src/core/mac/mac.cpp @@ -207,8 +207,8 @@ otError Mac::ConvertBeaconToActiveScanResult(Frame *aBeaconFrame, otActiveScanRe VerifyOrExit(aBeaconFrame->GetType() == Frame::kFcfFrameBeacon, error = OT_ERROR_PARSE); SuccessOrExit(error = aBeaconFrame->GetSrcAddr(address)); - VerifyOrExit(address.mLength == sizeof(address.mExtAddress), error = OT_ERROR_PARSE); - aResult.mExtAddress = address.mExtAddress; + VerifyOrExit(address.IsExtended(), error = OT_ERROR_PARSE); + aResult.mExtAddress = address.GetExtended(); aBeaconFrame->GetSrcPanId(aResult.mPanId); aResult.mChannel = aBeaconFrame->GetChannel(); @@ -1178,7 +1178,7 @@ void Mac::HandleTransmitDone(otRadioFrame *aFrame, otRadioFrame *aAckFrame, otEr mCounters.mTxErrBusyChannel++; } - if (dstAddr.mShortAddress == kShortAddrBroadcast) + if (dstAddr.IsBroadcast()) { mCounters.mTxBroadcast++; } @@ -1449,7 +1449,7 @@ otError Mac::ProcessReceiveSecurity(Frame &aFrame, const Address &aSrcAddr, Neig { case Frame::kKeyIdMode0: VerifyOrExit((macKey = keyManager.GetKek()) != NULL, error = OT_ERROR_SECURITY); - extAddress = &aSrcAddr.mExtAddress; + extAddress = &aSrcAddr.GetExtended(); break; case Frame::kKeyIdMode1: @@ -1506,7 +1506,7 @@ otError Mac::ProcessReceiveSecurity(Frame &aFrame, const Address &aSrcAddr, Neig } } - extAddress = &aSrcAddr.mExtAddress; + extAddress = &aSrcAddr.GetExtended(); break; @@ -1620,19 +1620,19 @@ void Mac::HandleReceivedFrame(Frame *aFrame, otError aError) neighbor = GetNetif().GetMle().GetNeighbor(srcaddr); // Destination Address Filtering - switch (dstaddr.mLength) + switch (dstaddr.GetType()) { - case 0: + case Address::kTypeNone: break; - case sizeof(ShortAddress): + case Address::kTypeShort: aFrame->GetDstPanId(panid); VerifyOrExit((panid == kShortAddrBroadcast || panid == mPanId) && - ((mRxOnWhenIdle && dstaddr.mShortAddress == kShortAddrBroadcast) || - dstaddr.mShortAddress == mShortAddress), error = OT_ERROR_DESTINATION_ADDRESS_FILTERED); + ((mRxOnWhenIdle && dstaddr.IsBroadcast()) || + dstaddr.GetShort() == mShortAddress), error = OT_ERROR_DESTINATION_ADDRESS_FILTERED); // Allow multicasts from neighbor routers if FFD - if (neighbor == NULL && dstaddr.mShortAddress == kShortAddrBroadcast && + if (neighbor == NULL && dstaddr.IsBroadcast() && (GetNetif().GetMle().GetDeviceMode() & Mle::ModeTlv::kModeFFD)) { neighbor = GetNetif().GetMle().GetRxOnlyNeighborRouter(srcaddr); @@ -1640,63 +1640,57 @@ void Mac::HandleReceivedFrame(Frame *aFrame, otError aError) break; - case sizeof(ExtAddress): + case Address::kTypeExtended: aFrame->GetDstPanId(panid); - VerifyOrExit(panid == mPanId && dstaddr.mExtAddress == mExtAddress, + VerifyOrExit(panid == mPanId && dstaddr.GetExtended() == mExtAddress, error = OT_ERROR_DESTINATION_ADDRESS_FILTERED); break; } // Source Address Filtering - switch (srcaddr.mLength) + switch (srcaddr.GetType()) { - case 0: + case Address::kTypeNone: break; - case sizeof(ShortAddress): - otLogDebgMac(GetInstance(), "Received frame from short address 0x%04x", srcaddr.mShortAddress); + case Address::kTypeShort: + otLogDebgMac(GetInstance(), "Received frame from short address 0x%04x", srcaddr.GetShort()); if (neighbor == NULL) { ExitNow(error = OT_ERROR_UNKNOWN_NEIGHBOR); } - srcaddr.mLength = sizeof(srcaddr.mExtAddress); - srcaddr.mExtAddress = neighbor->GetExtAddress(); - break; + srcaddr.SetExtended(neighbor->GetExtAddress()); - case sizeof(ExtAddress): - break; + // fall through - default: - ExitNow(error = OT_ERROR_INVALID_SOURCE_ADDRESS); - } + case Address::kTypeExtended: - // Duplicate Address Protection - if (srcaddr.mExtAddress == mExtAddress) - { - ExitNow(error = OT_ERROR_INVALID_SOURCE_ADDRESS); - } + // Duplicate Address Protection + if (srcaddr.GetExtended() == mExtAddress) + { + ExitNow(error = OT_ERROR_INVALID_SOURCE_ADDRESS); + } #if OPENTHREAD_ENABLE_MAC_FILTER - // Source filter Processing. - if (srcaddr.mLength != 0) - { - // check if filtered out by whitelist or blacklist. - SuccessOrExit(error = mFilter.Apply(srcaddr.mExtAddress, rssi)); + // Source filter Processing. Check if filtered out by whitelist or blacklist. + SuccessOrExit(error = mFilter.Apply(srcaddr.GetExtended(), rssi)); // override with the rssi in setting if (rssi != OT_MAC_FILTER_FIXED_RSS_DISABLED) { aFrame->mRssi = rssi; } - } #endif // OPENTHREAD_ENABLE_MAC_FILTER + break; + } + // Increment counters - if (dstaddr.mShortAddress == kShortAddrBroadcast) + if (dstaddr.IsBroadcast()) { // Broadcast frame mCounters.mRxBroadcast++; @@ -1768,7 +1762,7 @@ void Mac::HandleReceivedFrame(Frame *aFrame, otError aError) case kOperationWaitingForData: - if (dstaddr.mLength != 0) + if (!dstaddr.IsNone()) { mReceiveTimer.Stop(); diff --git a/src/core/mac/mac_frame.cpp b/src/core/mac/mac_frame.cpp index 9c7e40d55..d5c124e9b 100644 --- a/src/core/mac/mac_frame.cpp +++ b/src/core/mac/mac_frame.cpp @@ -53,23 +53,40 @@ bool ExtAddress::operator!=(const ExtAddress &aOther) const return memcmp(m8, aOther.m8, sizeof(ExtAddress)) != 0; } +void Address::SetExtended(const uint8_t *aBuffer, bool aReverse) +{ + mType = kTypeExtended; + + if (aReverse) + { + for (unsigned int i = 0; i < sizeof(ExtAddress); i++) + { + mShared.mExtAddress.m8[i] = aBuffer[sizeof(ExtAddress) - 1 - i]; + } + } + else + { + memcpy(mShared.mExtAddress.m8, aBuffer, sizeof(ExtAddress)); + } +} + const char *Address::ToString(char *aBuf, uint16_t aSize) const { - switch (mLength) + switch (mType) { - case sizeof(ShortAddress): - snprintf(aBuf, aSize, "0x%04x", mShortAddress); - break; - - case sizeof(ExtAddress): - snprintf(aBuf, aSize, "%02x%02x%02x%02x%02x%02x%02x%02x", - mExtAddress.m8[0], mExtAddress.m8[1], mExtAddress.m8[2], mExtAddress.m8[3], - mExtAddress.m8[4], mExtAddress.m8[5], mExtAddress.m8[6], mExtAddress.m8[7]); - break; - - default: + case kTypeNone: snprintf(aBuf, aSize, "None"); break; + + case kTypeShort: + snprintf(aBuf, aSize, "0x%04x", GetShort()); + break; + + case kTypeExtended: + snprintf(aBuf, aSize, "%02x%02x%02x%02x%02x%02x%02x%02x", + GetExtended().m8[0], GetExtended().m8[1], GetExtended().m8[2], GetExtended().m8[3], + GetExtended().m8[4], GetExtended().m8[5], GetExtended().m8[6], GetExtended().m8[7]); + break; } return aBuf; @@ -265,26 +282,15 @@ otError Frame::GetDstAddr(Address &aAddress) const switch (GetFrameControlField() & kFcfDstAddrMask) { case kFcfDstAddrShort: - aAddress.mLength = sizeof(ShortAddress); - aAddress.mShortAddress = Encoding::LittleEndian::ReadUint16(GetPsdu() + index); + aAddress.SetShort(Encoding::LittleEndian::ReadUint16(GetPsdu() + index)); break; case kFcfDstAddrExt: - { - const uint8_t *buf = GetPsdu() + index; - - aAddress.mLength = sizeof(ExtAddress); - - for (unsigned int i = 0; i < sizeof(ExtAddress); i++) - { - aAddress.mExtAddress.m8[i] = buf[sizeof(ExtAddress) - 1 - i]; - } - + aAddress.SetExtended(GetPsdu() + index, /* reverse */ true); break; - } default: - aAddress.mLength = 0; + aAddress.SetNone(); break; } @@ -320,14 +326,14 @@ otError Frame::SetDstAddr(const Address &aAddress) { otError error = OT_ERROR_NONE; - switch (aAddress.mLength) + switch (aAddress.GetType()) { - case sizeof(ShortAddress): - error = SetDstAddr(aAddress.mShortAddress); + case Address::kTypeShort: + error = SetDstAddr(aAddress.GetShort()); break; - case sizeof(ExtAddress): - error = SetDstAddr(aAddress.mExtAddress); + case Address::kTypeExtended: + error = SetDstAddr(aAddress.GetExtended()); break; default: @@ -432,26 +438,15 @@ otError Frame::GetSrcAddr(Address &address) const switch (fcf & kFcfSrcAddrMask) { case kFcfSrcAddrShort: - address.mLength = sizeof(ShortAddress); - address.mShortAddress = Encoding::LittleEndian::ReadUint16(GetPsdu() + index); + address.SetShort(Encoding::LittleEndian::ReadUint16(GetPsdu() + index)); break; case kFcfSrcAddrExt: - { - const uint8_t *buf = GetPsdu() + index; - - address.mLength = sizeof(ExtAddress); - - for (unsigned int i = 0; i < sizeof(ExtAddress); i++) - { - address.mExtAddress.m8[i] = buf[sizeof(ExtAddress) - 1 - i]; - } - + address.SetExtended(GetPsdu() + index, /* reverse */ true); break; - } default: - address.mLength = 0; + address.SetNone(); break; } @@ -491,14 +486,14 @@ otError Frame::SetSrcAddr(const Address &aAddress) { otError error = OT_ERROR_NONE; - switch (aAddress.mLength) + switch (aAddress.GetType()) { - case sizeof(ShortAddress): - error = SetSrcAddr(aAddress.mShortAddress); + case Address::kTypeShort: + error = SetSrcAddr(aAddress.GetShort()); break; - case sizeof(ExtAddress): - error = SetSrcAddr(aAddress.mExtAddress); + case Address::kTypeExtended: + error = SetSrcAddr(aAddress.GetExtended()); break; default: @@ -987,15 +982,8 @@ const char *Frame::ToInfoString(char *aBuf, uint16_t aSize) const break; } - if (GetSrcAddr(src) != OT_ERROR_NONE) - { - src.mLength = 0; - } - - if (GetDstAddr(dst) != OT_ERROR_NONE) - { - dst.mLength = 0; - } + GetSrcAddr(src); + GetDstAddr(dst); snprintf(aBuf, aSize, "len:%d, seqnum:%d, type:%s, src:%s, dst:%s, sec:%s, ackreq:%s", GetLength(), GetSequence(), typeStr, src.ToString(srcStringBuffer, sizeof(srcStringBuffer)), diff --git a/src/core/mac/mac_frame.hpp b/src/core/mac/mac_frame.hpp index 377b6e048..b25325e02 100644 --- a/src/core/mac/mac_frame.hpp +++ b/src/core/mac/mac_frame.hpp @@ -109,6 +109,14 @@ public: } } + /** + * This method toggles the Group bit. + * + */ + void ToggleGroup(void) { + m8[0] ^= kGroupFlag; + } + /** * This method indicates whether or not the Local bit is set. * @@ -133,6 +141,14 @@ public: } } + /** + * This method toggles the Local bit. + * + */ + void ToggleLocal(void) { + m8[0] ^= kLocalFlag; + } + /** * This method evaluates whether or not the Extended Addresses match. * @@ -164,33 +180,172 @@ private: } OT_TOOL_PACKED_END; /** - * This structure represents an IEEE 802.15.4 Short or Extended Address. + * This class represents an IEEE 802.15.4 Short or Extended Address. * */ -struct Address +class Address { +public: enum { kAddressStringSize = 18, ///< Max chars needed for a string representation of address (@sa ToString()). }; - uint8_t mLength; ///< Length of address in bytes. + /** + * This enumeration specifies the IEEE 802.15.4 Address type. + * + */ + enum Type + { + kTypeNone, ///< No address. + kTypeShort, ///< IEEE 802.15.4 Short Address. + kTypeExtended, ///< IEEE 802.15.4 Extended Address. + }; + + /** + * This constructor initializes an Address. + * + */ + Address(void): mType(kTypeNone) { } + + /** + * This method gets the address type (Short Address, Extended Address, or none). + * + * @returns The address type. + * + */ + Type GetType(void) const { return mType; } + + /** + * This method indicates whether or not there is an address. + * + * @returns TRUE if there is no address (i.e. address type is `kTypeNone`), FALSE otherwise. + * + */ + bool IsNone(void) const { return (mType == kTypeNone); } + + /** + * This method indicates whether or not the Address is a Short Address. + * + * @returns TRUE if it is a Short Address, FALSE otherwise. + * + */ + bool IsShort(void) const { return (mType == kTypeShort); } + + /** + * This method indicates whether or not the Address is an Extended Address. + * + * @returns TRUE if it is an Extended Address, FALSE otherwise. + * + */ + bool IsExtended(void) const { return (mType == kTypeExtended); } + + /** + * This method gets the address as a Short Address. + * + * This method MUST be used only if the address type is Short Address. + * + * @returns The Short Address. + * + */ + ShortAddress GetShort(void) const { return mShared.mShortAddress; } + + /** + * This method gets the address as an Extended Address. + * + * This method MUST be used only if the address type is Extended Address. + * + * @returns A constant reference to the Extended Address. + * + */ + const ExtAddress &GetExtended(void) const { return mShared.mExtAddress; } + + /** + * This method gets the address as an Extended Address. + * + * This method MUST be used only if the address type is Extended Address. + * + * @returns A reference to the Extended Address. + * + */ + ExtAddress &GetExtended(void) { return mShared.mExtAddress; } + + /** + * This method sets the address to none (i.e., clears the address). + * + * Address type will be updated to `kTypeNone`. + * + */ + void SetNone(void) { mType = kTypeNone; } + + /** + * This method sets the address with a Short Address. + * + * The type is also updated to indicate that address is Short. + * + * @param[in] aShortAddress A Short Address + * + */ + void SetShort(ShortAddress aShortAddress) { mShared.mShortAddress = aShortAddress; mType = kTypeShort; } + + /** + * This method sets the address with an Extended Address. + * + * The type is also updated to indicate that the address is Extended. + * + * @param[in] aExtAddress An Extended Address + * + */ + void SetExtended(const ExtAddress &aExtAddress) { mShared.mExtAddress = aExtAddress; mType = kTypeExtended; } + + /** + * This method sets the address with an Extended Address given as byte array. + * + * The type is also updated to indicate that the address is Extended. + * + * @param[in] aBuffer Pointer to a array containing the Extended Address. `OT_EXT_ADDRESS_SIZE` bytes from buffer + * are copied to form the Extended Address. + * @param[in] aReverse If `true` then `OT_EXT_ADDRESS_SIZE` bytes from @p aBuffer are copied in reverse order, + * otherwise they are copied as provided. + * + */ + void SetExtended(const uint8_t *aBuffer, bool aReverse); + + /** + * This method indicates whether or not the address is a Short Broadcast Address. + * + * @returns TRUE if address is Short Broadcast Address, FALSE otherwise. + * + */ + bool IsBroadcast(void) const { return ((mType == kTypeShort) && (GetShort() == kShortAddrBroadcast)); } + + /** + * This method indicates whether or not the address is a Short Invalid Address. + * + * @returns TRUE if address is Short Invalid Address, FALSE otherwise. + * + */ + bool IsShortAddrInvalid(void) const { return ((mType == kTypeShort) && (GetShort() == kShortAddrInvalid)); } + + /** + * This method converts an address to a NULL-terminated string. + * + * @param[out] aBuf A pointer to a character buffer. + * @param[in] aSize The maximum size of the buffer. + * + * @returns A pointer to the character string buffer. + * + */ + const char *ToString(char *aBuf, uint16_t aSize) const; + +private: union { ShortAddress mShortAddress; ///< The IEEE 802.15.4 Short Address. ExtAddress mExtAddress; ///< The IEEE 802.15.4 Extended Address. - }; + } mShared; - /** - * This method converts an address object to a NULL-terminated string. - * - * @param[out] aBuf A pointer to the buffer. - * @param[in] aSize The maximum size of the buffer. - * - * @returns A pointer to the char string buffer. - * - */ - const char *ToString(char *aBuf, uint16_t aSize) const; + Type mType; ///< The address type (Short, Extended, or none). }; /** diff --git a/src/core/net/ip6_address.cpp b/src/core/net/ip6_address.cpp index 1debc7c0e..66b39bb7b 100644 --- a/src/core/net/ip6_address.cpp +++ b/src/core/net/ip6_address.cpp @@ -170,7 +170,13 @@ void Address::SetIid(const Mac::ExtAddress &aEui64) void Address::ToExtAddress(Mac::ExtAddress &aExtAddress) const { memcpy(aExtAddress.m8, mFields.m8 + kInterfaceIdentifierOffset, sizeof(aExtAddress.m8)); - aExtAddress.m8[0] ^= 0x02; + aExtAddress.ToggleLocal(); +} + +void Address::ToExtAddress(Mac::Address &aMacAddress) const +{ + aMacAddress.SetExtended(mFields.m8 + kInterfaceIdentifierOffset, /* reverse */ false); + aMacAddress.GetExtended().ToggleLocal(); } uint8_t Address::GetScope(void) const diff --git a/src/core/net/ip6_address.hpp b/src/core/net/ip6_address.hpp index a4a8c252f..7f2b87635 100644 --- a/src/core/net/ip6_address.hpp +++ b/src/core/net/ip6_address.hpp @@ -305,6 +305,14 @@ public: */ void ToExtAddress(Mac::ExtAddress &aExtAddress) const; + /** + * This method converts the IPv6 Interface Identifier to an IEEE 802.15.4 MAC Address. + * + * @param[out] aMacAddress A reference to the MAC address. + * + */ + void ToExtAddress(Mac::Address &aMacAddress) const; + /** * This method returns the IPv6 address scope. * diff --git a/src/core/thread/lowpan.cpp b/src/core/thread/lowpan.cpp index 025d3b17e..3f4e158de 100644 --- a/src/core/thread/lowpan.cpp +++ b/src/core/thread/lowpan.cpp @@ -69,17 +69,17 @@ otError Lowpan::ComputeIid(const Mac::Address &aMacAddr, const Context &aContext { otError error = OT_ERROR_NONE; - switch (aMacAddr.mLength) + switch (aMacAddr.GetType()) { - case 2: + case Mac::Address::kTypeShort: aIpAddress.mFields.m16[4] = HostSwap16(0x0000); aIpAddress.mFields.m16[5] = HostSwap16(0x00ff); aIpAddress.mFields.m16[6] = HostSwap16(0xfe00); - aIpAddress.mFields.m16[7] = HostSwap16(aMacAddr.mShortAddress); + aIpAddress.mFields.m16[7] = HostSwap16(aMacAddr.GetShort()); break; - case Ip6::Address::kInterfaceIdentifierSize: - aIpAddress.SetIid(aMacAddr.mExtAddress); + case Mac::Address::kTypeExtended: + aIpAddress.SetIid(aMacAddr.GetExtended()); break; default: @@ -114,8 +114,7 @@ int Lowpan::CompressSourceIid(const Mac::Address &aMacAddr, const Ip6::Address & } else { - tmp.mLength = sizeof(tmp.mShortAddress); - tmp.mShortAddress = HostSwap16(aIpAddr.mFields.m16[7]); + tmp.SetShort(HostSwap16(aIpAddr.mFields.m16[7])); ComputeIid(tmp, aContext, ipaddr); if (memcmp(ipaddr.GetIid(), aIpAddr.GetIid(), Ip6::Address::kInterfaceIdentifierSize) == 0) @@ -151,8 +150,7 @@ int Lowpan::CompressDestinationIid(const Mac::Address &aMacAddr, const Ip6::Addr } else { - tmp.mLength = sizeof(tmp.mShortAddress); - tmp.mShortAddress = HostSwap16(aIpAddr.mFields.m16[7]); + tmp.SetShort(HostSwap16(aIpAddr.mFields.m16[7])); ComputeIid(tmp, aContext, ipaddr); if (memcmp(ipaddr.GetIid(), aIpAddr.GetIid(), Ip6::Address::kInterfaceIdentifierSize) == 0) diff --git a/src/core/thread/mesh_forwarder.cpp b/src/core/thread/mesh_forwarder.cpp index e95f38fbd..5c03148d7 100644 --- a/src/core/thread/mesh_forwarder.cpp +++ b/src/core/thread/mesh_forwarder.cpp @@ -67,8 +67,8 @@ MeshForwarder::MeshForwarder(Instance &aInstance): mSendMessage(NULL), mSendMessageIsARetransmission(false), mSendMessageMaxMacTxAttempts(Mac::kDirectFrameMacTxAttempts), - mMeshSource(Mac::kShortAddrInvalid), - mMeshDest(Mac::kShortAddrInvalid), + mMeshSource(), + mMeshDest(), mAddMeshHeader(false), mSendBusy(false), mScheduleTransmissionTask(aInstance, ScheduleTransmissionTask, this), @@ -89,8 +89,6 @@ MeshForwarder::MeshForwarder(Instance &aInstance): { mFragTag = static_cast(otPlatRandomGet()); GetNetif().GetMac().RegisterReceiver(mMacReceiver); - mMacSource.mLength = 0; - mMacDest.mLength = 0; mIpCounters.mTxSuccess = 0; mIpCounters.mRxSuccess = 0; @@ -315,17 +313,13 @@ otError MeshForwarder::PrepareDataPoll(void) if ((shortAddress == Mac::kShortAddrInvalid) || (parent != netif.GetMle().GetParent())) { - mMacSource.mLength = sizeof(mMacSource.mExtAddress); - mMacSource.mExtAddress = netif.GetMac().GetExtAddress(); - mMacDest.mLength = sizeof(mMacDest.mExtAddress); - mMacDest.mExtAddress = parent->GetExtAddress(); + mMacSource.SetExtended(netif.GetMac().GetExtAddress()); + mMacDest.SetExtended(parent->GetExtAddress()); } else { - mMacSource.mLength = sizeof(mMacSource.mShortAddress); - mMacSource.mShortAddress = shortAddress; - mMacDest.mLength = sizeof(mMacDest.mShortAddress); - mMacDest.mShortAddress = parent->GetRloc16(); + mMacSource.SetShort(shortAddress); + mMacDest.SetShort(parent->GetRloc16()); } exit: @@ -366,17 +360,15 @@ otError MeshForwarder::UpdateIp6Route(Message &aMessage) if (ip6Header.GetDestination().IsMulticast()) { - mMacDest.mLength = sizeof(mMacDest.mShortAddress); - // With the exception of MLE multicasts, a Thread End Device transmits multicasts, // as IEEE 802.15.4 unicasts to its parent. if (netif.GetMle().GetRole() == OT_DEVICE_ROLE_CHILD && !aMessage.IsSubTypeMle()) { - mMacDest.mShortAddress = netif.GetMle().GetNextHop(Mac::kShortAddrBroadcast); + mMacDest.SetShort(netif.GetMle().GetNextHop(Mac::kShortAddrBroadcast)); } else { - mMacDest.mShortAddress = Mac::kShortAddrBroadcast; + mMacDest.SetShort(Mac::kShortAddrBroadcast); } } else if (ip6Header.GetDestination().IsLinkLocal()) @@ -385,8 +377,7 @@ otError MeshForwarder::UpdateIp6Route(Message &aMessage) } else if (netif.GetMle().IsMinimalEndDevice()) { - mMacDest.mLength = sizeof(mMacDest.mShortAddress); - mMacDest.mShortAddress = netif.GetMle().GetNextHop(Mac::kShortAddrBroadcast); + mMacDest.SetShort(netif.GetMle().GetNextHop(Mac::kShortAddrBroadcast)); } else { @@ -437,16 +428,11 @@ otError MeshForwarder::GetMacSourceAddress(const Ip6::Address &aIp6Addr, Mac::Ad { ThreadNetif &netif = GetNetif(); - aIp6Addr.ToExtAddress(aMacAddr.mExtAddress); + aIp6Addr.ToExtAddress(aMacAddr); - if (aMacAddr.mExtAddress != netif.GetMac().GetExtAddress()) + if (aMacAddr.GetExtended() != netif.GetMac().GetExtAddress()) { - aMacAddr.mLength = sizeof(aMacAddr.mShortAddress); - aMacAddr.mShortAddress = netif.GetMac().GetShortAddress(); - } - else - { - aMacAddr.mLength = sizeof(aMacAddr.mExtAddress); + aMacAddr.SetShort(netif.GetMac().GetShortAddress()); } return OT_ERROR_NONE; @@ -456,8 +442,7 @@ otError MeshForwarder::GetMacDestinationAddress(const Ip6::Address &aIp6Addr, Ma { if (aIp6Addr.IsMulticast()) { - aMacAddr.mLength = sizeof(aMacAddr.mShortAddress); - aMacAddr.mShortAddress = Mac::kShortAddrBroadcast; + aMacAddr.SetShort(Mac::kShortAddrBroadcast); } else if (aIp6Addr.mFields.m16[0] == HostSwap16(0xfe80) && aIp6Addr.mFields.m16[1] == HostSwap16(0x0000) && @@ -467,18 +452,15 @@ otError MeshForwarder::GetMacDestinationAddress(const Ip6::Address &aIp6Addr, Ma aIp6Addr.mFields.m16[5] == HostSwap16(0x00ff) && aIp6Addr.mFields.m16[6] == HostSwap16(0xfe00)) { - aMacAddr.mLength = sizeof(aMacAddr.mShortAddress); - aMacAddr.mShortAddress = HostSwap16(aIp6Addr.mFields.m16[7]); + aMacAddr.SetShort(HostSwap16(aIp6Addr.mFields.m16[7])); } else if (GetNetif().GetMle().IsRoutingLocator(aIp6Addr)) { - aMacAddr.mLength = sizeof(aMacAddr.mShortAddress); - aMacAddr.mShortAddress = HostSwap16(aIp6Addr.mFields.m16[7]); + aMacAddr.SetShort(HostSwap16(aIp6Addr.mFields.m16[7])); } else { - aMacAddr.mLength = sizeof(aMacAddr.mExtAddress); - aIp6Addr.ToExtAddress(aMacAddr.mExtAddress); + aIp6Addr.ToExtAddress(aMacAddr); } return OT_ERROR_NONE; @@ -620,7 +602,8 @@ otError MeshForwarder::SendPoll(Message &aMessage, Mac::Frame &aFrame) // initialize MAC header fcf = Mac::Frame::kFcfFrameMacCmd | Mac::Frame::kFcfPanidCompression | Mac::Frame::kFcfFrameVersion2006; - if (mMacSource.mLength == sizeof(Mac::ShortAddress)) + + if (mMacSource.IsShort()) { fcf |= Mac::Frame::kFcfDstAddrShort | Mac::Frame::kFcfSrcAddrShort; } @@ -659,10 +642,8 @@ otError MeshForwarder::SendFragment(Message &aMessage, Mac::Frame &aFrame) if (mAddMeshHeader) { - meshSource.mLength = sizeof(meshSource.mShortAddress); - meshSource.mShortAddress = mMeshSource; - meshDest.mLength = sizeof(meshDest.mShortAddress); - meshDest.mShortAddress = mMeshDest; + meshSource.SetShort(mMeshSource); + meshDest.SetShort(mMeshDest); } else { @@ -672,11 +653,11 @@ otError MeshForwarder::SendFragment(Message &aMessage, Mac::Frame &aFrame) // initialize MAC header fcf = Mac::Frame::kFcfFrameData | Mac::Frame::kFcfFrameVersion2006; - fcf |= (mMacDest.mLength == 2) ? Mac::Frame::kFcfDstAddrShort : Mac::Frame::kFcfDstAddrExt; - fcf |= (mMacSource.mLength == 2) ? Mac::Frame::kFcfSrcAddrShort : Mac::Frame::kFcfSrcAddrExt; + fcf |= (mMacDest.IsShort()) ? Mac::Frame::kFcfDstAddrShort : Mac::Frame::kFcfDstAddrExt; + fcf |= (mMacSource.IsShort()) ? Mac::Frame::kFcfSrcAddrShort : Mac::Frame::kFcfSrcAddrExt; // all unicast frames request ACK - if (mMacDest.mLength == 8 || mMacDest.mShortAddress != Mac::kShortAddrBroadcast) + if (mMacDest.IsExtended() || !mMacDest.IsBroadcast()) { fcf |= Mac::Frame::kFcfAckRequest; } @@ -884,21 +865,16 @@ otError MeshForwarder::SendEmptyFrame(Mac::Frame &aFrame, bool aAckRequest) uint8_t secCtl; Mac::Address macSource; - macSource.mShortAddress = netif.GetMac().GetShortAddress(); + macSource.SetShort(netif.GetMac().GetShortAddress()); - if (macSource.mShortAddress != Mac::kShortAddrInvalid) + if (macSource.IsShortAddrInvalid()) { - macSource.mLength = sizeof(macSource.mShortAddress); - } - else - { - macSource.mLength = sizeof(macSource.mExtAddress); - macSource.mExtAddress = netif.GetMac().GetExtAddress(); + macSource.SetExtended(netif.GetMac().GetExtAddress()); } fcf = Mac::Frame::kFcfFrameData | Mac::Frame::kFcfFrameVersion2006; - fcf |= (mMacDest.mLength == 2) ? Mac::Frame::kFcfDstAddrShort : Mac::Frame::kFcfDstAddrExt; - fcf |= (macSource.mLength == 2) ? Mac::Frame::kFcfSrcAddrShort : Mac::Frame::kFcfSrcAddrExt; + fcf |= (mMacDest.IsShort()) ? Mac::Frame::kFcfDstAddrShort : Mac::Frame::kFcfDstAddrExt; + fcf |= (macSource.IsShort()) ? Mac::Frame::kFcfSrcAddrShort : Mac::Frame::kFcfSrcAddrExt; if (aAckRequest) { diff --git a/src/core/thread/mesh_forwarder_ftd.cpp b/src/core/thread/mesh_forwarder_ftd.cpp index 3e34cfda4..f52018e0f 100644 --- a/src/core/thread/mesh_forwarder_ftd.cpp +++ b/src/core/thread/mesh_forwarder_ftd.cpp @@ -425,13 +425,11 @@ otError MeshForwarder::GetIndirectTransmission(void) if (child.IsIndirectSourceMatchShort()) { - mMacSource.mLength = sizeof(mMacSource.mShortAddress); - mMacSource.mShortAddress = netif.GetMac().GetShortAddress(); + mMacSource.SetShort(netif.GetMac().GetShortAddress()); } else { - mMacSource.mLength = sizeof(mMacSource.mExtAddress); - mMacSource.mExtAddress = netif.GetMac().GetExtAddress(); + mMacSource.SetExtended(netif.GetMac().GetExtAddress()); } child.GetMacAddress(mMacDest); @@ -547,8 +545,8 @@ otError MeshForwarder::SendMesh(Message &aMessage, Mac::Frame &aFrame) aFrame.InitMacHeader(fcf, Mac::Frame::kKeyIdMode1 | Mac::Frame::kSecEncMic32); aFrame.SetDstPanId(netif.GetMac().GetPanId()); - aFrame.SetDstAddr(mMacDest.mShortAddress); - aFrame.SetSrcAddr(mMacSource.mShortAddress); + aFrame.SetDstAddr(mMacDest.GetShort()); + aFrame.SetSrcAddr(mMacSource.GetShort()); // write payload assert(aMessage.GetLength() <= aFrame.GetMaxPayloadLength()); @@ -729,10 +727,8 @@ otError MeshForwarder::UpdateMeshRoute(Message &aMessage) ExitNow(error = OT_ERROR_DROP); } - mMacDest.mLength = sizeof(mMacDest.mShortAddress); - mMacDest.mShortAddress = neighbor->GetRloc16(); - mMacSource.mLength = sizeof(mMacSource.mShortAddress); - mMacSource.mShortAddress = netif.GetMac().GetShortAddress(); + mMacDest.SetShort(neighbor->GetRloc16()); + mMacSource.SetShort(netif.GetMac().GetShortAddress()); mAddMeshHeader = true; mMeshDest = meshHeader.GetDestination(); @@ -824,16 +820,14 @@ otError MeshForwarder::UpdateIp6RouteFtd(Ip6::Header &ip6Header) VerifyOrExit(mMeshDest != Mac::kShortAddrInvalid, error = OT_ERROR_DROP); mMeshSource = netif.GetMac().GetShortAddress(); - mMacDest.mLength = sizeof(mMacDest.mShortAddress); SuccessOrExit(error = netif.GetMle().CheckReachability(mMeshSource, mMeshDest, ip6Header)); - mMacDest.mShortAddress = netif.GetMle().GetNextHop(mMeshDest); + mMacDest.SetShort(netif.GetMle().GetNextHop(mMeshDest)); - if (mMacDest.mShortAddress != mMeshDest) + if (mMacDest.GetShort() != mMeshDest) { // destination is not neighbor - mMacSource.mLength = sizeof(mMacSource.mShortAddress); - mMacSource.mShortAddress = mMeshSource; + mMacSource.SetShort(mMeshSource); mAddMeshHeader = true; } @@ -872,7 +866,7 @@ otError MeshForwarder::CheckReachability(uint8_t *aFrame, uint8_t aFrameLength, VerifyOrExit(netif.GetLowpan().DecompressBaseHeader(ip6Header, aMeshSource, aMeshDest, aFrame, aFrameLength) > 0, error = OT_ERROR_DROP); - error = netif.GetMle().CheckReachability(aMeshSource.mShortAddress, aMeshDest.mShortAddress, ip6Header); + error = netif.GetMle().CheckReachability(aMeshSource.GetShort(), aMeshDest.GetShort(), ip6Header); exit: return error; @@ -894,15 +888,13 @@ void MeshForwarder::HandleMesh(uint8_t *aFrame, uint8_t aFrameLength, const Mac: // Security Check: only process Mesh Header frames that had security enabled. VerifyOrExit(aLinkInfo.mLinkSecurity && meshHeader.IsValid(), error = OT_ERROR_SECURITY); - meshSource.mLength = sizeof(meshSource.mShortAddress); - meshSource.mShortAddress = meshHeader.GetSource(); - meshDest.mLength = sizeof(meshDest.mShortAddress); - meshDest.mShortAddress = meshHeader.GetDestination(); + meshSource.SetShort(meshHeader.GetSource()); + meshDest.SetShort(meshHeader.GetDestination()); UpdateRoutes(aFrame, aFrameLength, meshSource, meshDest); - if (meshDest.mShortAddress == netif.GetMac().GetShortAddress() || - netif.GetMle().IsMinimalChild(meshDest.mShortAddress)) + if (meshDest.GetShort() == netif.GetMac().GetShortAddress() || + netif.GetMle().IsMinimalChild(meshDest.GetShort())) { aFrame += meshHeader.GetHeaderLength(); aFrameLength -= meshHeader.GetHeaderLength(); @@ -922,7 +914,7 @@ void MeshForwarder::HandleMesh(uint8_t *aFrame, uint8_t aFrameLength, const Mac: } else if (meshHeader.GetHopsLeft() > 0) { - netif.GetMle().ResolveRoutingLoops(aMacSource.mShortAddress, meshDest.mShortAddress); + netif.GetMle().ResolveRoutingLoops(aMacSource.GetShort(), meshDest.GetShort()); SuccessOrExit(error = CheckReachability(aFrame, aFrameLength, meshSource, meshDest)); diff --git a/src/core/thread/mle.cpp b/src/core/thread/mle.cpp index 088407540..983dc228f 100644 --- a/src/core/thread/mle.cpp +++ b/src/core/thread/mle.cpp @@ -3427,14 +3427,17 @@ Neighbor *Mle::GetNeighbor(const Mac::Address &aAddress) { Neighbor *neighbor = NULL; - switch (aAddress.mLength) + switch (aAddress.GetType()) { - case 2: - neighbor = GetNeighbor(aAddress.mShortAddress); + case Mac::Address::kTypeShort: + neighbor = GetNeighbor(aAddress.GetShort()); break; - case 8: - neighbor = GetNeighbor(aAddress.mExtAddress); + case Mac::Address::kTypeExtended: + neighbor = GetNeighbor(aAddress.GetExtended()); + break; + + default: break; } diff --git a/src/core/thread/mle_router.cpp b/src/core/thread/mle_router.cpp index f88b1d2c5..da16990ff 100644 --- a/src/core/thread/mle_router.cpp +++ b/src/core/thread/mle_router.cpp @@ -3230,13 +3230,16 @@ Child *MleRouter::GetChild(const Mac::ExtAddress &aAddress) Child *MleRouter::GetChild(const Mac::Address &aAddress) { - switch (aAddress.mLength) + switch (aAddress.GetType()) { - case sizeof(aAddress.mShortAddress): - return GetChild(aAddress.mShortAddress); + case Mac::Address::kTypeShort: + return GetChild(aAddress.GetShort()); - case sizeof(aAddress.mExtAddress): - return GetChild(aAddress.mExtAddress); + case Mac::Address::kTypeExtended: + return GetChild(aAddress.GetExtended()); + + default: + break; } return NULL; @@ -3484,14 +3487,14 @@ Neighbor *MleRouter::GetNeighbor(const Mac::Address &aAddress) { Neighbor *rval = NULL; - switch (aAddress.mLength) + switch (aAddress.GetType()) { - case sizeof(aAddress.mShortAddress): - rval = GetNeighbor(aAddress.mShortAddress); + case Mac::Address::kTypeShort: + rval = GetNeighbor(aAddress.GetShort()); break; - case sizeof(aAddress.mExtAddress): - rval = GetNeighbor(aAddress.mExtAddress); + case Mac::Address::kTypeExtended: + rval = GetNeighbor(aAddress.GetExtended()); break; default: @@ -3503,7 +3506,7 @@ Neighbor *MleRouter::GetNeighbor(const Mac::Address &aAddress) Neighbor *MleRouter::GetNeighbor(const Ip6::Address &aAddress) { - Mac::Address macaddr; + Mac::Address macAddr; Lowpan::Context context; Child *child; Router *router; @@ -3515,16 +3518,14 @@ Neighbor *MleRouter::GetNeighbor(const Ip6::Address &aAddress) aAddress.mFields.m16[5] == HostSwap16(0x00ff) && aAddress.mFields.m16[6] == HostSwap16(0xfe00)) { - macaddr.mLength = sizeof(macaddr.mShortAddress); - macaddr.mShortAddress = HostSwap16(aAddress.mFields.m16[7]); + macAddr.SetShort(HostSwap16(aAddress.mFields.m16[7])); } else { - macaddr.mLength = sizeof(macaddr.mExtAddress); - aAddress.ToExtAddress(macaddr.mExtAddress); + aAddress.ToExtAddress(macAddr); } - ExitNow(rval = GetNeighbor(macaddr)); + ExitNow(rval = GetNeighbor(macAddr)); } if (GetNetif().GetNetworkDataLeader().GetContext(aAddress, context) != OT_ERROR_NONE) @@ -3586,16 +3587,16 @@ Neighbor *MleRouter::GetRxOnlyNeighborRouter(const Mac::Address &aAddress) VerifyOrExit(mRole == OT_DEVICE_ROLE_CHILD, rval = NULL); - switch (aAddress.mLength) + switch (aAddress.GetType()) { - case sizeof(aAddress.mShortAddress): - if (IsActiveRouter(aAddress.mShortAddress)) + case Mac::Address::kTypeShort: + if (IsActiveRouter(aAddress.GetShort())) { - uint8_t routerId = GetRouterId(aAddress.mShortAddress); + uint8_t routerId = GetRouterId(aAddress.GetShort()); Router *router = &mRouters[routerId]; if (router->GetState() == Neighbor::kStateValid && - router->GetRloc16() == aAddress.mShortAddress) + router->GetRloc16() == aAddress.GetShort()) { ExitNow(rval = router); } @@ -3603,7 +3604,7 @@ Neighbor *MleRouter::GetRxOnlyNeighborRouter(const Mac::Address &aAddress) break; - case sizeof(aAddress.mExtAddress): + case Mac::Address::kTypeExtended: for (int i = 0; i <= kMaxRouterId; i++) { if (i == mRouterId) @@ -3612,7 +3613,7 @@ Neighbor *MleRouter::GetRxOnlyNeighborRouter(const Mac::Address &aAddress) } if (mRouters[i].GetState() == Neighbor::kStateValid && - mRouters[i].GetExtAddress() == aAddress.mExtAddress) + mRouters[i].GetExtAddress() == aAddress.GetExtended()) { ExitNow(rval = &mRouters[i]); } diff --git a/src/core/thread/topology.cpp b/src/core/thread/topology.cpp index d67f97c60..ca3062ee1 100644 --- a/src/core/thread/topology.cpp +++ b/src/core/thread/topology.cpp @@ -228,13 +228,11 @@ const Mac::Address &Child::GetMacAddress(Mac::Address &aMacAddress) const { if (mUseShortAddress) { - aMacAddress.mShortAddress = GetRloc16(); - aMacAddress.mLength = sizeof(aMacAddress.mShortAddress); + aMacAddress.SetShort(GetRloc16()); } else { - aMacAddress.mExtAddress = GetExtAddress(); - aMacAddress.mLength = sizeof(aMacAddress.mExtAddress); + aMacAddress.SetExtended(GetExtAddress()); } return aMacAddress; diff --git a/tests/unit/test_lowpan.hpp b/tests/unit/test_lowpan.hpp index 6d265cfaa..6d4e58408 100644 --- a/tests/unit/test_lowpan.hpp +++ b/tests/unit/test_lowpan.hpp @@ -74,8 +74,7 @@ public: * */ void SetMacSource(const uint8_t *aAddress) { - mMacSource.mLength = OT_EXT_ADDRESS_SIZE; - memcpy(&mMacSource.mExtAddress, aAddress, mMacSource.mLength); + mMacSource.SetExtended(aAddress, /* reverse */ false); } /** @@ -85,8 +84,7 @@ public: * */ void SetMacSource(uint16_t aAddress) { - mMacSource.mLength = 2; - mMacSource.mShortAddress = aAddress; + mMacSource.SetShort(aAddress); } /** @@ -96,8 +94,7 @@ public: * */ void SetMacDestination(const uint8_t *aAddress) { - mMacDestination.mLength = OT_EXT_ADDRESS_SIZE; - memcpy(&mMacDestination.mExtAddress, aAddress, mMacDestination.mLength); + mMacDestination.SetExtended(aAddress, /* reverse */ false); } /** @@ -107,8 +104,7 @@ public: * */ void SetMacDestination(uint16_t aAddress) { - mMacDestination.mLength = 2; - mMacDestination.mShortAddress = aAddress; + mMacDestination.SetShort(aAddress); } /**