From 8d268663eff28a97e912b5dcf3dc25508cb6dfce Mon Sep 17 00:00:00 2001 From: Abtin Keshavarzian Date: Thu, 23 Sep 2021 09:18:29 -0700 Subject: [PATCH] [mac] update `Mac::NameData` to use `Data` (#7026) This commit updates `Mac::NameData` to use `Data` as its base class and then use its method in the `NameData` implementation. --- src/core/mac/mac_types.cpp | 41 +++++++++++++++-------------------- src/core/mac/mac_types.hpp | 21 +++++++----------- tests/unit/test_mac_frame.cpp | 7 ++++++ 3 files changed, 33 insertions(+), 36 deletions(-) diff --git a/src/core/mac/mac_types.cpp b/src/core/mac/mac_types.cpp index ef1c0c414..3a3bdc3e0 100644 --- a/src/core/mac/mac_types.cpp +++ b/src/core/mac/mac_types.cpp @@ -121,25 +121,18 @@ ExtendedPanId::InfoString ExtendedPanId::ToString(void) const uint8_t NameData::CopyTo(char *aBuffer, uint8_t aMaxSize) const { - uint8_t len = GetLength(); + MutableData destData; - memset(aBuffer, 0, aMaxSize); + destData.Init(aBuffer, aMaxSize); + destData.ClearBytes(); + IgnoreError(destData.CopyBytesFrom(*this)); - if (len > aMaxSize) - { - len = aMaxSize; - } - - memcpy(aBuffer, GetBuffer(), len); - - return len; + return destData.GetLength(); } NameData NetworkName::GetAsData(void) const { - uint8_t len = static_cast(StringLength(m8, kMaxSize + 1)); - - return NameData(m8, len); + return NameData(m8, static_cast(StringLength(m8, kMaxSize + 1))); } Error NetworkName::Set(const char *aNameString) @@ -165,15 +158,21 @@ exit: Error NetworkName::Set(const NameData &aNameData) { - Error error = kErrorNone; - uint8_t newLen = static_cast(StringLength(aNameData.GetBuffer(), aNameData.GetLength())); + Error error = kErrorNone; + NameData data = aNameData; + uint8_t newLen = static_cast(StringLength(data.GetBuffer(), data.GetLength())); VerifyOrExit((0 < newLen) && (newLen <= kMaxSize), error = kErrorInvalidArgs); - // Ensure the new name does not match the current one. - VerifyOrExit(memcmp(m8, aNameData.GetBuffer(), newLen) || (m8[newLen] != '\0'), error = kErrorAlready); + data.SetLength(newLen); - memcpy(m8, aNameData.GetBuffer(), newLen); + // Ensure the new name does not match the current one. + if (data.MatchesBytesIn(m8) && m8[newLen] == '\0') + { + ExitNow(error = kErrorAlready); + } + + data.CopyBytesTo(m8); m8[newLen] = '\0'; exit: @@ -182,11 +181,7 @@ exit: bool NetworkName::operator==(const NetworkName &aOther) const { - NameData data = GetAsData(); - NameData otherData = aOther.GetAsData(); - - return (data.GetLength() == otherData.GetLength()) && - (memcmp(data.GetBuffer(), otherData.GetBuffer(), data.GetLength()) == 0); + return GetAsData() == aOther.GetAsData(); } #if OPENTHREAD_CONFIG_MULTI_RADIO diff --git a/src/core/mac/mac_types.hpp b/src/core/mac/mac_types.hpp index f04dfed17..b1e53b78a 100644 --- a/src/core/mac/mac_types.hpp +++ b/src/core/mac/mac_types.hpp @@ -43,6 +43,7 @@ #include #include "common/clearable.hpp" +#include "common/data.hpp" #include "common/equatable.hpp" #include "common/string.hpp" #include "crypto/storage.hpp" @@ -583,8 +584,10 @@ public: * @note The char array does NOT need to be null terminated. * */ -class NameData +class NameData : private Data { + friend class NetworkName; + public: /** * This constructor initializes the NameData object. @@ -593,11 +596,7 @@ public: * @param[in] aLength The length (number of chars) in the buffer. * */ - NameData(const char *aBuffer, uint8_t aLength) - : mBuffer(aBuffer) - , mLength(aLength) - { - } + NameData(const char *aBuffer, uint8_t aLength) { Init(aBuffer, aLength); } /** * This method returns the pointer to char buffer (not necessarily null terminated). @@ -605,7 +604,7 @@ public: * @returns The pointer to the char buffer. * */ - const char *GetBuffer(void) const { return mBuffer; } + const char *GetBuffer(void) const { return reinterpret_cast(GetBytes()); } /** * This method returns the length (number of chars in buffer). @@ -613,7 +612,7 @@ public: * @returns The name length. * */ - uint8_t GetLength(void) const { return mLength; } + uint8_t GetLength(void) const { return Data::GetLength(); } /** * This method copies the name data into a given char buffer with a given size. @@ -628,17 +627,13 @@ public: * */ uint8_t CopyTo(char *aBuffer, uint8_t aMaxSize) const; - -private: - const char *mBuffer; - uint8_t mLength; }; /** * This structure represents an IEEE802.15.4 Network Name. * */ -class NetworkName : public otNetworkName +class NetworkName : public otNetworkName, public Unequatable { public: /** diff --git a/tests/unit/test_mac_frame.cpp b/tests/unit/test_mac_frame.cpp index 84826f1a0..b5d5796d6 100644 --- a/tests/unit/test_mac_frame.cpp +++ b/tests/unit/test_mac_frame.cpp @@ -175,6 +175,7 @@ void TestMacNetworkName(void) char buffer[sizeof(kTooLongName) + 2]; uint8_t len; Mac::NetworkName networkName; + Mac::NetworkName networkName2; CompareNetworkName(networkName, kEmptyName); @@ -224,6 +225,12 @@ void TestMacNetworkName(void) VerifyOrQuit(len == sizeof(kName1) - 1, "NameData::CopyTo() failed"); VerifyOrQuit(memcmp(buffer, kName1, sizeof(kName1) - 1) == 0, "NameData::CopyTo() failed"); VerifyOrQuit(buffer[sizeof(kName1)] == 0, "NameData::CopyTo() failed"); + + SuccessOrQuit(networkName2.Set(Mac::NameData(kName1, sizeof(kName1)))); + VerifyOrQuit(networkName == networkName2); + + SuccessOrQuit(networkName2.Set(kName2)); + VerifyOrQuit(networkName != networkName2); } void TestMacHeader(void)