[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.
This commit is contained in:
Abtin Keshavarzian
2021-09-23 09:18:29 -07:00
committed by GitHub
parent a66703773f
commit 8d268663ef
3 changed files with 33 additions and 36 deletions
+18 -23
View File
@@ -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<kWithUint8Length> 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<uint8_t>(StringLength(m8, kMaxSize + 1));
return NameData(m8, len);
return NameData(m8, static_cast<uint8_t>(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<uint8_t>(StringLength(aNameData.GetBuffer(), aNameData.GetLength()));
Error error = kErrorNone;
NameData data = aNameData;
uint8_t newLen = static_cast<uint8_t>(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
+8 -13
View File
@@ -43,6 +43,7 @@
#include <openthread/thread.h>
#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<kWithUint8Length>
{
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<const char *>(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<kWithUint8Length>::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<NetworkName>
{
public:
/**
+7
View File
@@ -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)