From 3ecfb45c80df2f87317b73cfcdac716d8b0028c6 Mon Sep 17 00:00:00 2001 From: Abtin Keshavarzian Date: Wed, 9 Aug 2017 10:53:37 -0700 Subject: [PATCH] [mac-frame] const-qualify the getter methods and other enhancements (#2062) --- src/core/common/encoding.hpp | 114 +++++++ src/core/mac/mac_frame.cpp | 557 ++++++++++++++++------------------- src/core/mac/mac_frame.hpp | 104 +++++-- 3 files changed, 438 insertions(+), 337 deletions(-) diff --git a/src/core/common/encoding.hpp b/src/core/common/encoding.hpp index 1a55e8673..c89a5f56d 100644 --- a/src/core/common/encoding.hpp +++ b/src/core/common/encoding.hpp @@ -99,6 +99,63 @@ inline uint64_t HostSwap64(uint64_t v) { return Swap64(v); } #endif // LITTLE_ENDIAN +/** + * This function reads a `uint16_t` value from a given buffer assuming big-ending encoding. + * + * @param[in] aBuffer Pointer to buffer to read from. + * + * @returns The `uint16_t` value read from buffer. + * + */ +inline uint16_t ReadUint16(const uint8_t *aBuffer) +{ + return static_cast((aBuffer[0] << 8) | aBuffer[1]); +} + +/** + * This function reads a `uint32_t` value from a given buffer assuming big-ending encoding. + * + * @param[in] aBuffer Pointer to buffer to read from. + * + * @returns The `uint32_t` value read from buffer. + * + */ +inline uint32_t ReadUint32(const uint8_t *aBuffer) +{ + return ((static_cast(aBuffer[0]) << 24) | + (static_cast(aBuffer[1]) << 16) | + (static_cast(aBuffer[2]) << 8) | + (static_cast(aBuffer[3]) << 0)); +} + +/** + * This function writes a `uint16_t` value to a given buffer using big-ending encoding. + * + * @param[in] aValue The value to write to buffer. + * @param[out] aBuffer Pointer to buffer where the value will be written. + * + */ +inline void WriteUint16(uint16_t aValue, uint8_t *aBuffer) +{ + aBuffer[0] = (aValue >> 8) & 0xff; + aBuffer[1] = (aValue >> 0) & 0xff; +} + +/** + * This function writes a `uint32_t` value to a given buffer using big-ending encoding. + * + * @param[in] aValue The value to write to buffer. + * @param[out] aBuffer Pointer to buffer where the value will be written. + * + */ +inline void WriteUint32(uint32_t aValue, uint8_t *aBuffer) +{ + aBuffer[0] = (aValue >> 24) & 0xff; + aBuffer[1] = (aValue >> 16) & 0xff; + aBuffer[2] = (aValue >> 8) & 0xff; + aBuffer[3] = (aValue >> 0) & 0xff; +} + } // namespace BigEndian namespace LittleEndian { @@ -117,6 +174,63 @@ inline uint64_t HostSwap64(uint64_t v) { return v; } #endif +/** + * This function reads a `uint16_t` value from a given buffer assuming little-ending encoding. + * + * @param[in] aBuffer Pointer to buffer to read from. + * + * @returns The `uint16_t` value read from buffer. + * + */ +inline uint16_t ReadUint16(const uint8_t *aBuffer) +{ + return static_cast(aBuffer[0] | (aBuffer[1] << 8)); +} + +/** + * This function reads a `uint32_t` value from a given buffer assuming little-ending encoding. + * + * @param[in] aBuffer Pointer to buffer to read from. + * + * @returns The `uint32_t` value read from buffer. + * + */ +inline uint32_t ReadUint32(const uint8_t *aBuffer) +{ + return ((static_cast(aBuffer[0]) << 0) | + (static_cast(aBuffer[1]) << 8) | + (static_cast(aBuffer[2]) << 16) | + (static_cast(aBuffer[3]) << 24)); +} + +/** + * This function writes a `uint16_t` value to a given buffer using little-ending encoding. + * + * @param[in] aValue The value to write to buffer. + * @param[out] aBuffer Pointer to buffer where the value will be written. + * + */ +inline void WriteUint16(uint16_t aValue, uint8_t *aBuffer) +{ + aBuffer[0] = (aValue >> 0) & 0xff; + aBuffer[1] = (aValue >> 8) & 0xff; +} + +/** + * This function writes a `uint32_t` value to a given buffer using little-ending encoding. + * + * @param[in] aValue The value to write to buffer. + * @param[out] aBuffer Pointer to buffer where the value will be written. + * + */ +inline void WriteUint32(uint32_t aValue, uint8_t *aBuffer) +{ + aBuffer[0] = (aValue >> 0) & 0xff; + aBuffer[1] = (aValue >> 8) & 0xff; + aBuffer[2] = (aValue >> 16) & 0xff; + aBuffer[3] = (aValue >> 24) & 0xff; +} + } // namespace LittleEndian } // namespace Encoding } // namespace ot diff --git a/src/core/mac/mac_frame.cpp b/src/core/mac/mac_frame.cpp index 76dab953e..8d89218a8 100644 --- a/src/core/mac/mac_frame.cpp +++ b/src/core/mac/mac_frame.cpp @@ -72,24 +72,23 @@ otError Frame::InitMacHeader(uint16_t aFcf, uint8_t aSecurityControl) uint8_t length = 0; // Frame Control Field - bytes[0] = aFcf & 0xff; - bytes[1] = aFcf >> 8; + Encoding::LittleEndian::WriteUint16(aFcf, bytes); length += kFcfSize; // Sequence Number length += kDsnSize; - // Destinatinon PAN + Address - switch (aFcf & Frame::kFcfDstAddrMask) + // Destination PAN + Address + switch (aFcf & kFcfDstAddrMask) { - case Frame::kFcfDstAddrNone: + case kFcfDstAddrNone: break; - case Frame::kFcfDstAddrShort: + case kFcfDstAddrShort: length += sizeof(PanId) + sizeof(ShortAddress); break; - case Frame::kFcfDstAddrExt: + case kFcfDstAddrExt: length += sizeof(PanId) + sizeof(ExtAddress); break; @@ -98,13 +97,13 @@ otError Frame::InitMacHeader(uint16_t aFcf, uint8_t aSecurityControl) } // Source PAN + Address - switch (aFcf & Frame::kFcfSrcAddrMask) + switch (aFcf & kFcfSrcAddrMask) { - case Frame::kFcfSrcAddrNone: + case kFcfSrcAddrNone: break; - case Frame::kFcfSrcAddrShort: - if ((aFcf & Frame::kFcfPanidCompression) == 0) + case kFcfSrcAddrShort: + if ((aFcf & kFcfPanidCompression) == 0) { length += sizeof(PanId); } @@ -112,8 +111,8 @@ otError Frame::InitMacHeader(uint16_t aFcf, uint8_t aSecurityControl) length += sizeof(ShortAddress); break; - case Frame::kFcfSrcAddrExt: - if ((aFcf & Frame::kFcfPanidCompression) == 0) + case kFcfSrcAddrExt: + if ((aFcf & kFcfPanidCompression) == 0) { length += sizeof(PanId); } @@ -126,7 +125,7 @@ otError Frame::InitMacHeader(uint16_t aFcf, uint8_t aSecurityControl) } // Security Header - if (aFcf & Frame::kFcfSecurityEnabled) + if (aFcf & kFcfSecurityEnabled) { bytes[length] = aSecurityControl; @@ -166,7 +165,12 @@ otError Frame::InitMacHeader(uint16_t aFcf, uint8_t aSecurityControl) return OT_ERROR_NONE; } -otError Frame::ValidatePsdu(void) +uint16_t Frame::GetFrameControlField(void) const +{ + return Encoding::LittleEndian::ReadUint16(GetPsdu()); +} + +otError Frame::ValidatePsdu(void) const { otError error = OT_ERROR_PARSE; uint8_t offset = kFcfSize + kDsnSize; @@ -175,19 +179,19 @@ otError Frame::ValidatePsdu(void) VerifyOrExit((offset + footerLength) <= GetPsduLength()); - fcf = static_cast((GetPsdu()[1] << 8) | GetPsdu()[0]); + fcf = GetFrameControlField(); - // Destinatinon PAN + Address - switch (fcf & Frame::kFcfDstAddrMask) + // Destination PAN + Address + switch (fcf & kFcfDstAddrMask) { - case Frame::kFcfDstAddrNone: + case kFcfDstAddrNone: break; - case Frame::kFcfDstAddrShort: + case kFcfDstAddrShort: offset += sizeof(PanId) + sizeof(ShortAddress); break; - case Frame::kFcfDstAddrExt: + case kFcfDstAddrExt: offset += sizeof(PanId) + sizeof(ExtAddress); break; @@ -196,13 +200,13 @@ otError Frame::ValidatePsdu(void) } // Source PAN + Address - switch (fcf & Frame::kFcfSrcAddrMask) + switch (fcf & kFcfSrcAddrMask) { - case Frame::kFcfSrcAddrNone: + case kFcfSrcAddrNone: break; - case Frame::kFcfSrcAddrShort: - if ((fcf & Frame::kFcfPanidCompression) == 0) + case kFcfSrcAddrShort: + if ((fcf & kFcfPanidCompression) == 0) { offset += sizeof(PanId); } @@ -210,8 +214,8 @@ otError Frame::ValidatePsdu(void) offset += sizeof(ShortAddress); break; - case Frame::kFcfSrcAddrExt: - if ((fcf & Frame::kFcfPanidCompression) == 0) + case kFcfSrcAddrExt: + if ((fcf & kFcfPanidCompression) == 0) { offset += sizeof(PanId); } @@ -226,7 +230,7 @@ otError Frame::ValidatePsdu(void) VerifyOrExit((offset + footerLength) <= GetPsduLength()); // Security Header - if (fcf & Frame::kFcfSecurityEnabled) + if (fcf & kFcfSecurityEnabled) { uint8_t secControl = GetPsdu()[offset]; @@ -289,96 +293,50 @@ exit: return error; } -uint8_t Frame::GetType(void) -{ - return GetPsdu()[0] & Frame::kFcfFrameTypeMask; -} - -bool Frame::GetSecurityEnabled(void) -{ - return (GetPsdu()[0] & Frame::kFcfSecurityEnabled) != 0; -} - -bool Frame::GetAckRequest(void) -{ - return (GetPsdu()[0] & Frame::kFcfAckRequest) != 0; -} - void Frame::SetAckRequest(bool aAckRequest) { if (aAckRequest) { - GetPsdu()[0] |= Frame::kFcfAckRequest; + GetPsdu()[0] |= kFcfAckRequest; } else { - GetPsdu()[0] &= ~Frame::kFcfAckRequest; + GetPsdu()[0] &= ~kFcfAckRequest; } } -bool Frame::GetFramePending(void) -{ - return (GetPsdu()[0] & Frame::kFcfFramePending) != 0; -} - void Frame::SetFramePending(bool aFramePending) { if (aFramePending) { - GetPsdu()[0] |= Frame::kFcfFramePending; + GetPsdu()[0] |= kFcfFramePending; } else { - GetPsdu()[0] &= ~Frame::kFcfFramePending; + GetPsdu()[0] &= ~kFcfFramePending; } } -uint8_t *Frame::FindSequence(void) +uint8_t Frame::FindDstPanIdIndex(void) const { - uint8_t *cur = GetPsdu(); + uint8_t index = 0; - // Frame Control Field - cur += kFcfSize; + VerifyOrExit((GetFrameControlField() & kFcfDstAddrMask) != kFcfDstAddrNone, index = kInvalidIndex); - return cur; -} - -uint8_t Frame::GetSequence(void) -{ - uint8_t *buf = FindSequence(); - return buf[0]; -} - -void Frame::SetSequence(uint8_t aSequence) -{ - uint8_t *buf = FindSequence(); - buf[0] = aSequence; -} - -uint8_t *Frame::FindDstPanId(void) -{ - uint8_t *cur = GetPsdu(); - uint16_t fcf = static_cast((GetPsdu()[1] << 8) | GetPsdu()[0]); - - VerifyOrExit((fcf & Frame::kFcfDstAddrMask) != Frame::kFcfDstAddrNone, cur = NULL); - - // Frame Control Field - cur += kFcfSize; - // Sequence Number - cur += kDsnSize; + // Frame Control Field and Sequence Number + index = kFcfSize + kDsnSize; exit: - return cur; + return index; } -otError Frame::GetDstPanId(PanId &aPanId) +otError Frame::GetDstPanId(PanId &aPanId) const { otError error = OT_ERROR_NONE; - uint8_t *buf; + uint8_t index = FindDstPanIdIndex(); - VerifyOrExit((buf = FindDstPanId()) != NULL, error = OT_ERROR_PARSE); - - aPanId = static_cast((buf[1] << 8) | buf[0]); + VerifyOrExit(index != kInvalidIndex, error = OT_ERROR_PARSE); + aPanId = Encoding::LittleEndian::ReadUint16(GetPsdu() + index); exit: return error; @@ -386,47 +344,37 @@ exit: otError Frame::SetDstPanId(PanId aPanId) { - uint8_t *buf; + uint8_t index = FindDstPanIdIndex(); - buf = FindDstPanId(); - assert(buf != NULL); - - buf[0] = aPanId & 0xff; - buf[1] = aPanId >> 8; + assert(index != kInvalidIndex); + Encoding::LittleEndian::WriteUint16(aPanId, GetPsdu() + index); return OT_ERROR_NONE; } -uint8_t *Frame::FindDstAddr(void) +uint8_t Frame::FindDstAddrIndex(void) const { - uint8_t *cur = GetPsdu(); - - // Frame Control Field - cur += kFcfSize; - // Sequence Number - cur += kDsnSize; - // Destination PAN - cur += sizeof(PanId); - - return cur; + return kFcfSize + kDsnSize + sizeof(PanId); } -otError Frame::GetDstAddr(Address &aAddress) +otError Frame::GetDstAddr(Address &aAddress) const { otError error = OT_ERROR_NONE; - uint8_t *buf; - uint16_t fcf = static_cast((GetPsdu()[1] << 8) | GetPsdu()[0]); + uint8_t index = FindDstAddrIndex(); - VerifyOrExit(buf = FindDstAddr(), error = OT_ERROR_PARSE); + VerifyOrExit(index != kInvalidIndex, error = OT_ERROR_PARSE); - switch (fcf & Frame::kFcfDstAddrMask) + switch (GetFrameControlField() & kFcfDstAddrMask) { - case Frame::kFcfDstAddrShort: + case kFcfDstAddrShort: aAddress.mLength = sizeof(ShortAddress); - aAddress.mShortAddress = static_cast((buf[1] << 8) | buf[0]); + aAddress.mShortAddress = Encoding::LittleEndian::ReadUint16(GetPsdu() + index); break; - case Frame::kFcfDstAddrExt: + case kFcfDstAddrExt: + { + const uint8_t *buf = GetPsdu() + index; + aAddress.mLength = sizeof(ExtAddress); for (unsigned int i = 0; i < sizeof(ExtAddress); i++) @@ -435,6 +383,7 @@ otError Frame::GetDstAddr(Address &aAddress) } break; + } default: aAddress.mLength = 0; @@ -447,29 +396,19 @@ exit: otError Frame::SetDstAddr(ShortAddress aShortAddress) { - uint8_t *buf; - uint16_t fcf = static_cast((GetPsdu()[1] << 8) | GetPsdu()[0]); - - assert((fcf & Frame::kFcfDstAddrMask) == Frame::kFcfDstAddrShort); - - buf = FindDstAddr(); - assert(buf != NULL); - - buf[0] = aShortAddress & 0xff; - buf[1] = aShortAddress >> 8; + assert((GetFrameControlField() & kFcfDstAddrMask) == kFcfDstAddrShort); + Encoding::LittleEndian::WriteUint16(aShortAddress, GetPsdu() + FindDstAddrIndex()); return OT_ERROR_NONE; } otError Frame::SetDstAddr(const ExtAddress &aExtAddress) { - uint8_t *buf; - uint16_t fcf = static_cast((GetPsdu()[1] << 8) | GetPsdu()[0]); + uint8_t index = FindDstAddrIndex(); + uint8_t *buf = GetPsdu() + index; - assert((fcf & Frame::kFcfDstAddrMask) == Frame::kFcfDstAddrExt); - - buf = FindDstAddr(); - assert(buf != NULL); + assert((GetFrameControlField() & kFcfDstAddrMask) == kFcfDstAddrExt); + assert(index != kInvalidIndex); for (unsigned int i = 0; i < sizeof(ExtAddress); i++) { @@ -479,46 +418,43 @@ otError Frame::SetDstAddr(const ExtAddress &aExtAddress) return OT_ERROR_NONE; } -uint8_t *Frame::FindSrcPanId(void) +uint8_t Frame::FindSrcPanIdIndex(void) const { - uint8_t *cur = GetPsdu(); - uint16_t fcf = static_cast((GetPsdu()[1] << 8) | GetPsdu()[0]); + uint8_t index = 0; + uint16_t fcf = GetFrameControlField(); - VerifyOrExit((fcf & Frame::kFcfDstAddrMask) != Frame::kFcfDstAddrNone || - (fcf & Frame::kFcfSrcAddrMask) != Frame::kFcfSrcAddrNone, cur = NULL); + VerifyOrExit((fcf & kFcfDstAddrMask) != kFcfDstAddrNone || + (fcf & kFcfSrcAddrMask) != kFcfSrcAddrNone, index = kInvalidIndex); - // Frame Control Field - cur += kFcfSize; - // Sequence Number - cur += kDsnSize; + // Frame Control Field and Sequence Number + index += kFcfSize + kDsnSize; - if ((fcf & Frame::kFcfPanidCompression) == 0) + if ((fcf & kFcfPanidCompression) == 0) { // Destination PAN + Address - switch (fcf & Frame::kFcfDstAddrMask) + switch (fcf & kFcfDstAddrMask) { - case Frame::kFcfDstAddrShort: - cur += sizeof(PanId) + sizeof(ShortAddress); + case kFcfDstAddrShort: + index += sizeof(PanId) + sizeof(ShortAddress); break; - case Frame::kFcfDstAddrExt: - cur += sizeof(PanId) + sizeof(ExtAddress); + case kFcfDstAddrExt: + index += sizeof(PanId) + sizeof(ExtAddress); break; } } exit: - return cur; + return index; } -otError Frame::GetSrcPanId(PanId &aPanId) +otError Frame::GetSrcPanId(PanId &aPanId) const { otError error = OT_ERROR_NONE; - uint8_t *buf; + uint8_t index = FindSrcPanIdIndex(); - VerifyOrExit((buf = FindSrcPanId()) != NULL, error = OT_ERROR_PARSE); - - aPanId = static_cast((buf[1] << 8) | buf[0]); + VerifyOrExit(index != kInvalidIndex, error = OT_ERROR_PARSE); + aPanId = Encoding::LittleEndian::ReadUint16(GetPsdu() + index); exit: return error; @@ -527,64 +463,63 @@ exit: otError Frame::SetSrcPanId(PanId aPanId) { otError error = OT_ERROR_NONE; - uint8_t *buf; + uint8_t index = FindSrcPanIdIndex(); - VerifyOrExit((buf = FindSrcPanId()) != NULL, error = OT_ERROR_PARSE); - - buf[0] = aPanId & 0xff; - buf[1] = aPanId >> 8; + VerifyOrExit(index != kInvalidIndex, error = OT_ERROR_PARSE); + Encoding::LittleEndian::WriteUint16(aPanId, GetPsdu() + index); exit: return error; } -uint8_t *Frame::FindSrcAddr(void) +uint8_t Frame::FindSrcAddrIndex(void) const { - uint8_t *cur = GetPsdu(); - uint16_t fcf = static_cast((GetPsdu()[1] << 8) | GetPsdu()[0]); + uint8_t index = 0; + uint16_t fcf = GetFrameControlField(); - // Frame Control Field - cur += kFcfSize; - // Sequence Number - cur += kDsnSize; + // Frame Control Field and Sequence Number + index += kFcfSize + kDsnSize; // Destination PAN + Address - switch (fcf & Frame::kFcfDstAddrMask) + switch (fcf & kFcfDstAddrMask) { - case Frame::kFcfDstAddrShort: - cur += sizeof(PanId) + sizeof(ShortAddress); + case kFcfDstAddrShort: + index += sizeof(PanId) + sizeof(ShortAddress); break; - case Frame::kFcfDstAddrExt: - cur += sizeof(PanId) + sizeof(ExtAddress); + case kFcfDstAddrExt: + index += sizeof(PanId) + sizeof(ExtAddress); break; } // Source PAN - if ((fcf & Frame::kFcfPanidCompression) == 0) + if ((fcf & kFcfPanidCompression) == 0) { - cur += sizeof(PanId); + index += sizeof(PanId); } - return cur; + return index; } -otError Frame::GetSrcAddr(Address &address) +otError Frame::GetSrcAddr(Address &address) const { otError error = OT_ERROR_NONE; - uint8_t *buf; - uint16_t fcf = static_cast((GetPsdu()[1] << 8) | GetPsdu()[0]); + uint8_t index = FindSrcAddrIndex(); + uint16_t fcf = GetFrameControlField(); - VerifyOrExit((buf = FindSrcAddr()) != NULL, error = OT_ERROR_PARSE); + VerifyOrExit(index != kInvalidIndex, error = OT_ERROR_PARSE); - switch (fcf & Frame::kFcfSrcAddrMask) + switch (fcf & kFcfSrcAddrMask) { - case Frame::kFcfSrcAddrShort: + case kFcfSrcAddrShort: address.mLength = sizeof(ShortAddress); - address.mShortAddress = static_cast((buf[1] << 8) | buf[0]); + address.mShortAddress = Encoding::LittleEndian::ReadUint16(GetPsdu() + index); break; - case Frame::kFcfSrcAddrExt: + case kFcfSrcAddrExt: + { + const uint8_t *buf = GetPsdu() + index; + address.mLength = sizeof(ExtAddress); for (unsigned int i = 0; i < sizeof(ExtAddress); i++) @@ -593,6 +528,7 @@ otError Frame::GetSrcAddr(Address &address) } break; + } default: address.mLength = 0; @@ -605,29 +541,23 @@ exit: otError Frame::SetSrcAddr(ShortAddress aShortAddress) { - uint8_t *buf; - uint16_t fcf = static_cast((GetPsdu()[1] << 8) | GetPsdu()[0]); + uint8_t index = FindSrcAddrIndex(); - assert((fcf & Frame::kFcfSrcAddrMask) == Frame::kFcfSrcAddrShort); + assert((GetFrameControlField() & kFcfSrcAddrMask) == kFcfSrcAddrShort); + assert(index != kInvalidIndex); - buf = FindSrcAddr(); - assert(buf != NULL); - - buf[0] = aShortAddress & 0xff; - buf[1] = aShortAddress >> 8; + Encoding::LittleEndian::WriteUint16(aShortAddress, GetPsdu() + index); return OT_ERROR_NONE; } otError Frame::SetSrcAddr(const ExtAddress &aExtAddress) { - uint8_t *buf; - uint16_t fcf = static_cast((GetPsdu()[1] << 8) | GetPsdu()[0]); + uint8_t index = FindSrcAddrIndex(); + uint8_t *buf = GetPsdu() + index; - assert((fcf & Frame::kFcfSrcAddrMask) == Frame::kFcfSrcAddrExt); - - buf = FindSrcAddr(); - assert(buf != NULL); + assert((GetFrameControlField() & kFcfSrcAddrMask) == kFcfSrcAddrExt); + assert(index != kInvalidIndex); for (unsigned int i = 0; i < sizeof(aExtAddress); i++) { @@ -637,96 +567,91 @@ otError Frame::SetSrcAddr(const ExtAddress &aExtAddress) return OT_ERROR_NONE; } -uint8_t *Frame::FindSecurityHeader(void) +uint8_t Frame::FindSecurityHeaderIndex(void) const { - uint8_t *cur = GetPsdu(); - uint16_t fcf = static_cast((GetPsdu()[1] << 8) | GetPsdu()[0]); + uint8_t index = 0; + uint16_t fcf = GetFrameControlField(); - VerifyOrExit((fcf & Frame::kFcfSecurityEnabled) != 0, cur = NULL); + VerifyOrExit((fcf & kFcfSecurityEnabled) != 0, index = kInvalidIndex); - // Frame Control Field - cur += kFcfSize; - // Sequence Number - cur += kDsnSize; + // Frame Control Field and Sequence Number + index += kFcfSize + kDsnSize; // Destination PAN + Address - switch (fcf & Frame::kFcfDstAddrMask) + switch (fcf & kFcfDstAddrMask) { - case Frame::kFcfDstAddrShort: - cur += sizeof(PanId) + sizeof(ShortAddress); + case kFcfDstAddrShort: + index += sizeof(PanId) + sizeof(ShortAddress); break; - case Frame::kFcfDstAddrExt: - cur += sizeof(PanId) + sizeof(ExtAddress); + case kFcfDstAddrExt: + index += sizeof(PanId) + sizeof(ExtAddress); break; } // Source PAN + Address - switch (fcf & Frame::kFcfSrcAddrMask) + switch (fcf & kFcfSrcAddrMask) { - case Frame::kFcfSrcAddrShort: - if ((fcf & Frame::kFcfPanidCompression) == 0) + case kFcfSrcAddrShort: + if ((fcf & kFcfPanidCompression) == 0) { - cur += sizeof(PanId); + index += sizeof(PanId); } - cur += sizeof(ShortAddress); + index += sizeof(ShortAddress); break; - case Frame::kFcfSrcAddrExt: - if ((fcf & Frame::kFcfPanidCompression) == 0) + case kFcfSrcAddrExt: + if ((fcf & kFcfPanidCompression) == 0) { - cur += sizeof(PanId); + index += sizeof(PanId); } - cur += sizeof(ExtAddress); + index += sizeof(ExtAddress); break; } exit: - return cur; + return index; } -otError Frame::GetSecurityLevel(uint8_t &aSecurityLevel) +otError Frame::GetSecurityLevel(uint8_t &aSecurityLevel) const { otError error = OT_ERROR_NONE; - uint8_t *buf; + uint8_t index = FindSecurityHeaderIndex(); - VerifyOrExit((buf = FindSecurityHeader()) != NULL, error = OT_ERROR_PARSE); + VerifyOrExit(index != kInvalidIndex, error = OT_ERROR_PARSE); - aSecurityLevel = buf[0] & kSecLevelMask; + aSecurityLevel = GetPsdu()[index] & kSecLevelMask; exit: return error; } -otError Frame::GetKeyIdMode(uint8_t &aKeyIdMode) +otError Frame::GetKeyIdMode(uint8_t &aKeyIdMode) const { otError error = OT_ERROR_NONE; - uint8_t *buf; + uint8_t index = FindSecurityHeaderIndex(); - VerifyOrExit((buf = FindSecurityHeader()) != NULL, error = OT_ERROR_PARSE); + VerifyOrExit(index != kInvalidIndex, error = OT_ERROR_PARSE); - aKeyIdMode = buf[0] & kKeyIdModeMask; + aKeyIdMode = GetPsdu()[index] & kKeyIdModeMask; exit: return error; } -otError Frame::GetFrameCounter(uint32_t &aFrameCounter) +otError Frame::GetFrameCounter(uint32_t &aFrameCounter) const { otError error = OT_ERROR_NONE; - uint8_t *buf; + uint8_t index = FindSecurityHeaderIndex(); - VerifyOrExit((buf = FindSecurityHeader()) != NULL, error = OT_ERROR_PARSE); + VerifyOrExit(index != kInvalidIndex, error = OT_ERROR_PARSE); // Security Control - buf += kSecurityControlSize; + index += kSecurityControlSize; - aFrameCounter = ((static_cast(buf[3]) << 24) | - (static_cast(buf[2]) << 16) | - (static_cast(buf[1]) << 8) | - (static_cast(buf[0]))); + aFrameCounter = Encoding::LittleEndian::ReadUint32(GetPsdu() + index); exit: return error; @@ -734,28 +659,24 @@ exit: otError Frame::SetFrameCounter(uint32_t aFrameCounter) { - uint8_t *buf; + uint8_t index = FindSecurityHeaderIndex(); - buf = FindSecurityHeader(); - assert(buf != NULL); + assert(index != kInvalidIndex); // Security Control - buf += kSecurityControlSize; + index += kSecurityControlSize; - buf[0] = aFrameCounter & 0xff; - buf[1] = (aFrameCounter >> 8) & 0xff; - buf[2] = (aFrameCounter >> 16) & 0xff; - buf[3] = (aFrameCounter >> 24) & 0xff; + Encoding::LittleEndian::WriteUint32(aFrameCounter, GetPsdu() + index); return OT_ERROR_NONE; } -const uint8_t *Frame::GetKeySource(void) +const uint8_t *Frame::GetKeySource(void) const { - uint8_t *buf; + uint8_t index = FindSecurityHeaderIndex(); + const uint8_t *buf = GetPsdu() + index; - buf = FindSecurityHeader(); - assert(buf != NULL); + assert(index != kInvalidIndex); // Security Control buf += kSecurityControlSize + kFrameCounterSize; @@ -792,10 +713,10 @@ uint8_t Frame::GetKeySourceLength(uint8_t aKeyIdMode) void Frame::SetKeySource(const uint8_t *aKeySource) { uint8_t keySourceLength; - uint8_t *buf; + uint8_t index = FindSecurityHeaderIndex(); + uint8_t *buf = GetPsdu() + index; - buf = FindSecurityHeader(); - assert(buf != NULL); + assert(index != kInvalidIndex); keySourceLength = GetKeySourceLength(buf[0] & kKeyIdModeMask); @@ -804,13 +725,14 @@ void Frame::SetKeySource(const uint8_t *aKeySource) memcpy(buf, aKeySource, keySourceLength); } -otError Frame::GetKeyId(uint8_t &aKeyId) +otError Frame::GetKeyId(uint8_t &aKeyId) const { otError error = OT_ERROR_NONE; uint8_t keySourceLength; - uint8_t *buf; + uint8_t index = FindSecurityHeaderIndex(); + const uint8_t *buf = GetPsdu() + index; - VerifyOrExit((buf = FindSecurityHeader()) != NULL, error = OT_ERROR_PARSE); + VerifyOrExit(index != kInvalidIndex); keySourceLength = GetKeySourceLength(buf[0] & kKeyIdModeMask); @@ -825,10 +747,10 @@ exit: otError Frame::SetKeyId(uint8_t aKeyId) { uint8_t keySourceLength; - uint8_t *buf; + uint8_t index = FindSecurityHeaderIndex(); + uint8_t *buf = GetPsdu() + index; - buf = FindSecurityHeader(); - assert(buf != NULL); + assert(index != kInvalidIndex); keySourceLength = GetKeySourceLength(buf[0] & kKeyIdModeMask); @@ -839,13 +761,14 @@ otError Frame::SetKeyId(uint8_t aKeyId) return OT_ERROR_NONE; } -otError Frame::GetCommandId(uint8_t &aCommandId) +otError Frame::GetCommandId(uint8_t &aCommandId) const { otError error = OT_ERROR_NONE; - uint8_t *buf; + uint8_t index = FindPayloadIndex(); - VerifyOrExit((buf = GetPayload()) != NULL, error = OT_ERROR_PARSE); - aCommandId = buf[-1]; + VerifyOrExit(index != kInvalidIndex, error = OT_ERROR_PARSE); + + aCommandId = (GetPsdu() + index)[-1]; exit: return error; @@ -854,16 +777,17 @@ exit: otError Frame::SetCommandId(uint8_t aCommandId) { otError error = OT_ERROR_NONE; - uint8_t *buf; + uint8_t index = FindPayloadIndex(); - VerifyOrExit((buf = GetPayload()) != NULL, error = OT_ERROR_PARSE); - buf[-1] = aCommandId; + VerifyOrExit(index != kInvalidIndex, error = OT_ERROR_PARSE); + + (GetPsdu() + index)[-1] = aCommandId; exit: return error; } -bool Frame::IsDataRequestCommand(void) +bool Frame::IsDataRequestCommand(void) const { bool isDataRequest = false; uint8_t commandId = 0; @@ -876,19 +800,19 @@ exit: return isDataRequest; } -uint8_t Frame::GetHeaderLength(void) +uint8_t Frame::GetHeaderLength(void) const { return static_cast(GetPayload() - GetPsdu()); } -uint8_t Frame::GetFooterLength(void) +uint8_t Frame::GetFooterLength(void) const { uint8_t footerLength = 0; - uint8_t *cur; + uint8_t index = FindSecurityHeaderIndex(); - VerifyOrExit((cur = FindSecurityHeader()) != NULL); + VerifyOrExit(index != kInvalidIndex); - switch (cur[0] & kSecLevelMask) + switch ((GetPsdu() + index)[0] & kSecLevelMask) { case kSecNone: case kSecEnc: @@ -918,12 +842,12 @@ exit: return footerLength; } -uint8_t Frame::GetMaxPayloadLength(void) +uint8_t Frame::GetMaxPayloadLength(void) const { return kMTU - (GetHeaderLength() + GetFooterLength()); } -uint8_t Frame::GetPayloadLength(void) +uint8_t Frame::GetPayloadLength(void) const { return GetPsduLength() - (GetHeaderLength() + GetFooterLength()); } @@ -934,91 +858,86 @@ otError Frame::SetPayloadLength(uint8_t aLength) return OT_ERROR_NONE; } -uint8_t *Frame::GetHeader(void) +uint8_t Frame::FindPayloadIndex(void) const { - return GetPsdu(); -} - -uint8_t *Frame::GetPayload(void) -{ - uint8_t *cur = GetPsdu(); - uint16_t fcf = static_cast((GetPsdu()[1] << 8) | GetPsdu()[0]); + uint8_t index = 0; + uint16_t fcf = GetFrameControlField(); uint8_t securityControl; // Frame Control - cur += kFcfSize; + index += kFcfSize; // Sequence Number - cur += kDsnSize; + index += kDsnSize; // Destination PAN + Address - switch (fcf & Frame::kFcfDstAddrMask) + switch (fcf & kFcfDstAddrMask) { - case Frame::kFcfDstAddrNone: + case kFcfDstAddrNone: break; - case Frame::kFcfDstAddrShort: - cur += sizeof(PanId) + sizeof(ShortAddress); + case kFcfDstAddrShort: + index += sizeof(PanId) + sizeof(ShortAddress); break; - case Frame::kFcfDstAddrExt: - cur += sizeof(PanId) + sizeof(ExtAddress); + case kFcfDstAddrExt: + index += sizeof(PanId) + sizeof(ExtAddress); break; default: - ExitNow(cur = NULL); + ExitNow(index = kInvalidIndex); } // Source PAN + Address - switch (fcf & Frame::kFcfSrcAddrMask) + switch (fcf & kFcfSrcAddrMask) { - case Frame::kFcfSrcAddrNone: + case kFcfSrcAddrNone: break; - case Frame::kFcfSrcAddrShort: - if ((fcf & Frame::kFcfPanidCompression) == 0) + case kFcfSrcAddrShort: + if ((fcf & kFcfPanidCompression) == 0) { - cur += sizeof(PanId); + index += sizeof(PanId); } - cur += sizeof(ShortAddress); + index += sizeof(ShortAddress); break; - case Frame::kFcfSrcAddrExt: - if ((fcf & Frame::kFcfPanidCompression) == 0) + case kFcfSrcAddrExt: + if ((fcf & kFcfPanidCompression) == 0) { - cur += sizeof(PanId); + index += sizeof(PanId); } - cur += sizeof(ExtAddress); + index += sizeof(ExtAddress); break; default: - ExitNow(cur = NULL); + ExitNow(index = kInvalidIndex); } // Security Control + Frame Counter + Key Identifier - if ((fcf & Frame::kFcfSecurityEnabled) != 0) + if ((fcf & kFcfSecurityEnabled) != 0) { - securityControl = *cur; + securityControl = *(GetPsdu() + index); - cur += kSecurityControlSize + kFrameCounterSize; + index += kSecurityControlSize + kFrameCounterSize; switch (securityControl & kKeyIdModeMask) { case kKeyIdMode0: - cur += kKeySourceSizeMode0; + index += kKeySourceSizeMode0; break; case kKeyIdMode1: - cur += kKeySourceSizeMode1 + kKeyIndexSize; + index += kKeySourceSizeMode1 + kKeyIndexSize; break; case kKeyIdMode2: - cur += kKeySourceSizeMode2 + kKeyIndexSize; + index += kKeySourceSizeMode2 + kKeyIndexSize; break; case kKeyIdMode3: - cur += kKeySourceSizeMode3 + kKeyIndexSize; + index += kKeySourceSizeMode3 + kKeyIndexSize; break; } } @@ -1026,11 +945,34 @@ uint8_t *Frame::GetPayload(void) // Command ID if ((fcf & kFcfFrameTypeMask) == kFcfFrameMacCmd) { - cur += kCommandIdSize; + index += kCommandIdSize; } exit: - return cur; + return index; + +} + +uint8_t *Frame::GetPayload(void) +{ + uint8_t index = FindPayloadIndex(); + uint8_t *payload = GetPsdu() + index; + + VerifyOrExit(index != kInvalidIndex, payload = NULL); + +exit: + return payload; +} + +const uint8_t *Frame::GetPayload(void) const +{ + uint8_t index = FindPayloadIndex(); + const uint8_t *payload = GetPsdu() + index; + + VerifyOrExit(index != kInvalidIndex, payload = NULL); + +exit: + return payload; } uint8_t *Frame::GetFooter(void) @@ -1038,7 +980,12 @@ uint8_t *Frame::GetFooter(void) return GetPsdu() + GetPsduLength() - GetFooterLength(); } -const char *Frame::ToInfoString(char *aBuf, uint16_t aSize) +const uint8_t *Frame::GetFooter(void) const +{ + return GetPsdu() + GetPsduLength() - GetFooterLength(); +} + +const char *Frame::ToInfoString(char *aBuf, uint16_t aSize) const { uint8_t type, commandId; Address src, dst; diff --git a/src/core/mac/mac_frame.hpp b/src/core/mac/mac_frame.hpp index d52a43a63..a3c8a935c 100644 --- a/src/core/mac/mac_frame.hpp +++ b/src/core/mac/mac_frame.hpp @@ -268,7 +268,7 @@ public: * @retval OT_ERROR_PARSE Failed to parse through the MAC header. * */ - otError ValidatePsdu(void); + otError ValidatePsdu(void) const; /** * This method returns the IEEE 802.15.4 Frame Type. @@ -276,7 +276,7 @@ public: * @returns The IEEE 802.15.4 Frame Type. * */ - uint8_t GetType(void); + uint8_t GetType(void) const { return GetPsdu()[0] & kFcfFrameTypeMask; } /** * This method indicates whether or not security is enabled. @@ -285,7 +285,7 @@ public: * @retval FALSE If security is not enabled. * */ - bool GetSecurityEnabled(void); + bool GetSecurityEnabled(void) const { return (GetPsdu()[0] & kFcfSecurityEnabled) != 0; } /** * This method indicates whether or not the Frame Pending bit is set. @@ -294,7 +294,7 @@ public: * @retval FALSE If the Frame Pending bit is not set. * */ - bool GetFramePending(void); + bool GetFramePending(void) const { return (GetPsdu()[0] & kFcfFramePending) != 0; } /** * This method sets the Frame Pending bit. @@ -311,7 +311,7 @@ public: * @retval FALSE If the Ack Request bit is not set. * */ - bool GetAckRequest(void); + bool GetAckRequest(void) const { return (GetPsdu()[0] & kFcfAckRequest) != 0; } /** * This method sets the Ack Request bit. @@ -327,7 +327,7 @@ public: * @returns The Sequence Number value. * */ - uint8_t GetSequence(void); + uint8_t GetSequence(void) const { return GetPsdu()[kSequenceIndex]; } /** * This method sets the Sequence Number value. @@ -335,7 +335,7 @@ public: * @param[in] aSequence The Sequence Number value. * */ - void SetSequence(uint8_t aSequence); + void SetSequence(uint8_t aSequence) { GetPsdu()[kSequenceIndex] = aSequence; } /** * This method gets the Destination PAN Identifier. @@ -345,7 +345,7 @@ public: * @retval OT_ERROR_NONE Successfully retrieved the Destination PAN Identifier. * */ - otError GetDstPanId(PanId &aPanId); + otError GetDstPanId(PanId &aPanId) const; /** * This method sets the Destination PAN Identifier. @@ -365,7 +365,7 @@ public: * @retval OT_ERROR_NONE Successfully retrieved the Destination Address. * */ - otError GetDstAddr(Address &aAddress); + otError GetDstAddr(Address &aAddress) const; /** * This method sets the Destination Address. @@ -395,7 +395,7 @@ public: * @retval OT_ERROR_NONE Successfully retrieved the Source PAN Identifier. * */ - otError GetSrcPanId(PanId &aPanId); + otError GetSrcPanId(PanId &aPanId) const; /** * This method sets the Source PAN Identifier. @@ -415,7 +415,7 @@ public: * @retval OT_ERROR_NONE Successfully retrieved the Source Address. * */ - otError GetSrcAddr(Address &aAddress); + otError GetSrcAddr(Address &aAddress) const; /** * This method gets the Source Address. @@ -445,7 +445,7 @@ public: * @retval OT_ERROR_NONE Successfully retrieved the Security Level Identifier. * */ - otError GetSecurityLevel(uint8_t &aSecurityLevel); + otError GetSecurityLevel(uint8_t &aSecurityLevel) const; /** * This method gets the Key Identifier Mode. @@ -455,7 +455,7 @@ public: * @retval OT_ERROR_NONE Successfully retrieved the Key Identifier Mode. * */ - otError GetKeyIdMode(uint8_t &aKeyIdMode); + otError GetKeyIdMode(uint8_t &aKeyIdMode) const; /** * This method gets the Frame Counter. @@ -465,7 +465,7 @@ public: * @retval OT_ERROR_NONE Successfully retrieved the Frame Counter. * */ - otError GetFrameCounter(uint32_t &aFrameCounter); + otError GetFrameCounter(uint32_t &aFrameCounter) const; /** * This method sets the Frame Counter. @@ -483,7 +483,7 @@ public: * @returns A pointer to the Key Source. * */ - const uint8_t *GetKeySource(void); + const uint8_t *GetKeySource(void) const; /** * This method sets the Key Source. @@ -501,7 +501,7 @@ public: * @retval OT_ERROR_NONE Successfully retrieved the Key Identifier. * */ - otError GetKeyId(uint8_t &aKeyId); + otError GetKeyId(uint8_t &aKeyId) const; /** * This method sets the Key Identifier. @@ -521,7 +521,7 @@ public: * @retval OT_ERROR_NONE Successfully retrieved the Command ID. * */ - otError GetCommandId(uint8_t &aCommandId); + otError GetCommandId(uint8_t &aCommandId) const; /** * This method sets the Command ID. @@ -539,7 +539,7 @@ public: * @returns TRUE if frame is a MAC Data Request command, FALSE otherwise. * */ - bool IsDataRequestCommand(void); + bool IsDataRequestCommand(void) const; /** * This method returns the MAC Frame Length. @@ -566,7 +566,7 @@ public: * @returns The MAC header size. * */ - uint8_t GetHeaderLength(void); + uint8_t GetHeaderLength(void) const; /** * This method returns the MAC footer size. @@ -574,7 +574,7 @@ public: * @returns The MAC footer size. * */ - uint8_t GetFooterLength(void); + uint8_t GetFooterLength(void) const; /** * This method returns the current MAC Payload length. @@ -582,7 +582,7 @@ public: * @returns The current MAC Payload length. * */ - uint8_t GetPayloadLength(void); + uint8_t GetPayloadLength(void) const; /** * This method returns the maximum MAC Payload length for the given MAC header and footer. @@ -590,7 +590,7 @@ public: * @returns The maximum MAC Payload length for the given MAC header and footer. * */ - uint8_t GetMaxPayloadLength(void); + uint8_t GetMaxPayloadLength(void) const; /** * This method sets the MAC Payload length. @@ -657,7 +657,6 @@ public: */ uint8_t GetMaxTxAttempts(void) const { return mMaxTxAttempts; } - /** * This method set the maximum number of transmit attempts for frame. * @@ -724,13 +723,29 @@ public: */ uint8_t *GetPsdu(void) { return mPsdu; } + /** + * This const method returns a pointer to the PSDU. + * + * @returns A pointer to the PSDU. + * + */ + const uint8_t *GetPsdu(void) const { return mPsdu; } + /** * This method returns a pointer to the MAC Header. * * @returns A pointer to the MAC Header. * */ - uint8_t *GetHeader(void); + uint8_t *GetHeader(void) { return GetPsdu(); } + + /** + * This const method returns a pointer to the MAC Header. + * + * @returns A pointer to the MAC Header. + * + */ + const uint8_t *GetHeader(void) const { return GetPsdu(); } /** * This method returns a pointer to the MAC Payload. @@ -740,6 +755,14 @@ public: */ uint8_t *GetPayload(void); + /** + * This const method returns a pointer to the MAC Payload. + * + * @returns A pointer to the MAC Payload. + * + */ + const uint8_t *GetPayload(void) const; + /** * This method returns a pointer to the MAC Footer. * @@ -748,6 +771,14 @@ public: */ uint8_t *GetFooter(void); + /** + * This const method returns a pointer to the MAC Footer. + * + * @returns A pointer to the MAC Footer. + * + */ + const uint8_t *GetFooter(void) const; + /** * This method returns information about the frame object as a NULL-terminated string. * @@ -757,16 +788,25 @@ public: * @returns A pointer to the char string buffer. * */ - const char *ToInfoString(char *aBuf, uint16_t aSize); + const char *ToInfoString(char *aBuf, uint16_t aSize) const; private: - uint8_t *FindSequence(void); - uint8_t *FindDstPanId(void); - uint8_t *FindDstAddr(void); - uint8_t *FindSrcPanId(void); - uint8_t *FindSrcAddr(void); - uint8_t *FindSecurityHeader(void); + enum + { + kInvalidIndex = 0xff, + kSequenceIndex = kFcfSize, + }; + + uint16_t GetFrameControlField(void) const; + uint8_t FindDstPanIdIndex(void) const; + uint8_t FindDstAddrIndex(void) const; + uint8_t FindSrcPanIdIndex(void) const; + uint8_t FindSrcAddrIndex(void) const; + uint8_t FindSecurityHeaderIndex(void) const; + uint8_t FindPayloadIndex(void) const; + static uint8_t GetKeySourceLength(uint8_t aKeyIdMode); + } OT_TOOL_PACKED_END; OT_TOOL_PACKED_BEGIN @@ -806,7 +846,7 @@ public: * @retval A pointer to the beacon payload address. * */ - uint8_t *GetPayload() { return reinterpret_cast(this) + sizeof(*this); } + uint8_t *GetPayload(void) { return reinterpret_cast(this) + sizeof(*this); } private: uint16_t mSuperframeSpec;