mirror of
https://github.com/espressif/openthread.git
synced 2026-09-02 07:10:08 +00:00
[domain] add domain name (#4815)
This commit is contained in:
@@ -474,6 +474,37 @@ const char *otThreadGetNetworkName(otInstance *aInstance);
|
||||
*/
|
||||
otError otThreadSetNetworkName(otInstance *aInstance, const char *aNetworkName);
|
||||
|
||||
/**
|
||||
* Get the Thread Domain Name.
|
||||
*
|
||||
* This function is only availble since Thread 1.2.
|
||||
*
|
||||
* @param[in] aInstance A pointer to an OpenThread instance.
|
||||
*
|
||||
* @returns A pointer to the Thread Domain Name.
|
||||
*
|
||||
* @sa otThreadSetDomainName
|
||||
*
|
||||
*/
|
||||
const char *otThreadGetDomainName(otInstance *aInstance);
|
||||
|
||||
/**
|
||||
* Set the Thread Domain Name.
|
||||
*
|
||||
* This function is only availble since Thread 1.2.
|
||||
* This function succeeds only when Thread protocols are disabled.
|
||||
*
|
||||
* @param[in] aInstance A pointer to an OpenThread instance.
|
||||
* @param[in] aDomainName A pointer to the Thread Domain Name.
|
||||
*
|
||||
* @retval OT_ERROR_NONE Successfully set the Thread Domain Name.
|
||||
* @retval OT_ERROR_INVALID_STATE Thread protocols are enabled.
|
||||
*
|
||||
* @sa otThreadGetDomainName
|
||||
*
|
||||
*/
|
||||
otError otThreadSetDomainName(otInstance *aInstance, const char *aDomainName);
|
||||
|
||||
/**
|
||||
* Get the thrKeySequenceCounter.
|
||||
*
|
||||
|
||||
@@ -45,6 +45,7 @@ Done
|
||||
* [diag](#diag)
|
||||
* [discover](#discover-channel)
|
||||
* [dns](#dns-resolve-hostname-dns-server-ip-dns-server-port)
|
||||
* [domainname](#domainname)
|
||||
* [eidcache](#eidcache)
|
||||
* [eui64](#eui64)
|
||||
* [extaddr](#extaddr)
|
||||
@@ -555,6 +556,25 @@ The latter two parameters have following default values:
|
||||
> DNS response for ipv6.google.com - 2a00:1450:401b:801:0:0:0:200e TTL: 300
|
||||
```
|
||||
|
||||
### domainname
|
||||
|
||||
Get the Thread Domain Name for Thread 1.2 device.
|
||||
|
||||
```bash
|
||||
> domainname
|
||||
Thread
|
||||
Done
|
||||
```
|
||||
|
||||
### domainname \<name\>
|
||||
|
||||
Set the Thread Domain Name for Thread 1.2 device.
|
||||
|
||||
```bash
|
||||
> domainname Test\ Thread
|
||||
Done
|
||||
```
|
||||
|
||||
### eidcache
|
||||
|
||||
Print the EID-to-RLOC cache entries.
|
||||
|
||||
+22
-1
@@ -141,6 +141,9 @@ const struct Command Interpreter::sCommands[] = {
|
||||
#if OPENTHREAD_CONFIG_DNS_CLIENT_ENABLE
|
||||
{"dns", &Interpreter::ProcessDns},
|
||||
#endif
|
||||
#if (OPENTHREAD_CONFIG_THREAD_VERSION >= OT_THREAD_VERSION_1_2)
|
||||
{"domainname", &Interpreter::ProcessDomainName},
|
||||
#endif
|
||||
#if OPENTHREAD_FTD
|
||||
{"eidcache", &Interpreter::ProcessEidCache},
|
||||
#endif
|
||||
@@ -579,6 +582,24 @@ exit:
|
||||
}
|
||||
#endif // OPENTHREAD_FTD && OPENTHREAD_CONFIG_BACKBONE_ROUTER_ENABLE
|
||||
|
||||
void Interpreter::ProcessDomainName(uint8_t aArgsLength, char *aArgs[])
|
||||
{
|
||||
otError error = OT_ERROR_NONE;
|
||||
|
||||
if (aArgsLength == 0)
|
||||
{
|
||||
const char *domainName = otThreadGetDomainName(mInstance);
|
||||
mServer->OutputFormat("%s\r\n", static_cast<const char *>(domainName));
|
||||
}
|
||||
else
|
||||
{
|
||||
SuccessOrExit(error = otThreadSetDomainName(mInstance, aArgs[0]));
|
||||
}
|
||||
|
||||
exit:
|
||||
AppendResult(error);
|
||||
}
|
||||
|
||||
#endif // (OPENTHREAD_CONFIG_THREAD_VERSION >= OT_THREAD_VERSION_1_2)
|
||||
|
||||
void Interpreter::ProcessBufferInfo(uint8_t aArgsLength, char *aArgs[])
|
||||
@@ -2061,7 +2082,7 @@ void Interpreter::ProcessNetworkName(uint8_t aArgsLength, char *aArgs[])
|
||||
if (aArgsLength == 0)
|
||||
{
|
||||
const char *networkName = otThreadGetNetworkName(mInstance);
|
||||
mServer->OutputFormat("%.*s\r\n", OT_NETWORK_NAME_MAX_SIZE, static_cast<const char *>(networkName));
|
||||
mServer->OutputFormat("%s\r\n", static_cast<const char *>(networkName));
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
+4
-1
@@ -205,10 +205,13 @@ private:
|
||||
void ProcessChannel(uint8_t aArgsLength, char *aArgs[]);
|
||||
#if (OPENTHREAD_CONFIG_THREAD_VERSION >= OT_THREAD_VERSION_1_2)
|
||||
void ProcessBackboneRouter(uint8_t aArgsLength, char *aArgs[]);
|
||||
|
||||
#if OPENTHREAD_FTD && OPENTHREAD_CONFIG_BACKBONE_ROUTER_ENABLE
|
||||
otError ProcessBackboneRouterLocal(uint8_t aArgsLength, char *aArgs[]);
|
||||
#endif
|
||||
#endif
|
||||
|
||||
void ProcessDomainName(uint8_t aArgsLength, char *aArgs[]);
|
||||
#endif // (OPENTHREAD_CONFIG_THREAD_VERSION >= OT_THREAD_VERSION_1_2)
|
||||
|
||||
#if OPENTHREAD_FTD
|
||||
void ProcessChild(uint8_t aArgsLength, char *aArgs[]);
|
||||
|
||||
@@ -200,6 +200,28 @@ exit:
|
||||
return error;
|
||||
}
|
||||
|
||||
#if (OPENTHREAD_CONFIG_THREAD_VERSION >= OT_THREAD_VERSION_1_2)
|
||||
const char *otThreadGetDomainName(otInstance *aInstance)
|
||||
{
|
||||
Instance &instance = *static_cast<Instance *>(aInstance);
|
||||
|
||||
return instance.Get<Mac::Mac>().GetDomainName().GetAsCString();
|
||||
}
|
||||
|
||||
otError otThreadSetDomainName(otInstance *aInstance, const char *aDomainName)
|
||||
{
|
||||
otError error = OT_ERROR_NONE;
|
||||
Instance &instance = *static_cast<Instance *>(aInstance);
|
||||
|
||||
VerifyOrExit(instance.Get<Mle::MleRouter>().IsDisabled(), error = OT_ERROR_INVALID_STATE);
|
||||
|
||||
error = instance.Get<Mac::Mac>().SetDomainName(aDomainName);
|
||||
|
||||
exit:
|
||||
return error;
|
||||
}
|
||||
#endif // (OPENTHREAD_CONFIG_THREAD_VERSION >= OT_THREAD_VERSION_1_2)
|
||||
|
||||
uint32_t otThreadGetKeySequenceCounter(otInstance *aInstance)
|
||||
{
|
||||
Instance &instance = *static_cast<Instance *>(aInstance);
|
||||
|
||||
+43
-4
@@ -67,6 +67,10 @@ const otExtendedPanId Mac::sExtendedPanidInit = {
|
||||
|
||||
const char Mac::sNetworkNameInit[] = "OpenThread";
|
||||
|
||||
#if (OPENTHREAD_CONFIG_THREAD_VERSION >= OT_THREAD_VERSION_1_2)
|
||||
const char Mac::sDomainNameInit[] = "Thread";
|
||||
#endif
|
||||
|
||||
Mac::Mac(Instance &aInstance)
|
||||
: InstanceLocator(aInstance)
|
||||
, mEnabled(true)
|
||||
@@ -98,6 +102,9 @@ Mac::Mac(Instance &aInstance)
|
||||
, mRadioChannel(OPENTHREAD_CONFIG_DEFAULT_CHANNEL)
|
||||
, mSupportedChannelMask(Get<Radio>().GetSupportedChannelMask())
|
||||
, mNetworkName()
|
||||
#if (OPENTHREAD_CONFIG_THREAD_VERSION >= OT_THREAD_VERSION_1_2)
|
||||
, mDomainName()
|
||||
#endif
|
||||
, mScanChannel(Radio::kChannelMin)
|
||||
, mScanDuration(0)
|
||||
, mScanChannelMask()
|
||||
@@ -129,6 +136,9 @@ Mac::Mac(Instance &aInstance)
|
||||
|
||||
SetExtendedPanId(static_cast<const ExtendedPanId &>(sExtendedPanidInit));
|
||||
SetNetworkName(sNetworkNameInit);
|
||||
#if (OPENTHREAD_CONFIG_THREAD_VERSION >= OT_THREAD_VERSION_1_2)
|
||||
SetDomainName(sDomainNameInit);
|
||||
#endif
|
||||
SetPanId(mPanId);
|
||||
SetExtAddress(randomExtAddress);
|
||||
SetShortAddress(GetShortAddress());
|
||||
@@ -452,7 +462,7 @@ void Mac::SetSupportedChannelMask(const ChannelMask &aMask)
|
||||
|
||||
otError Mac::SetNetworkName(const char *aNameString)
|
||||
{
|
||||
// When setting Network Name from a string, we treat it as `Data`
|
||||
// When setting Network Name from a string, we treat it as `NameData`
|
||||
// with `kMaxSize + 1` chars. `NetworkName::Set(data)` will look
|
||||
// for null char in the data (within its given size) to calculate
|
||||
// the name's length and ensure that the name fits in `kMaxSize`
|
||||
@@ -460,14 +470,14 @@ otError Mac::SetNetworkName(const char *aNameString)
|
||||
// longer than `kMaxSize` is correctly rejected (returning error
|
||||
// `OT_ERROR_INVALID_ARGS`).
|
||||
|
||||
NetworkName::Data data(aNameString, NetworkName::kMaxSize + 1);
|
||||
NameData data(aNameString, NetworkName::kMaxSize + 1);
|
||||
|
||||
return SetNetworkName(data);
|
||||
}
|
||||
|
||||
otError Mac::SetNetworkName(const NetworkName::Data &aName)
|
||||
otError Mac::SetNetworkName(const NameData &aNameData)
|
||||
{
|
||||
otError error = mNetworkName.Set(aName);
|
||||
otError error = mNetworkName.Set(aNameData);
|
||||
|
||||
if (error == OT_ERROR_ALREADY)
|
||||
{
|
||||
@@ -483,6 +493,35 @@ exit:
|
||||
return error;
|
||||
}
|
||||
|
||||
#if (OPENTHREAD_CONFIG_THREAD_VERSION >= OT_THREAD_VERSION_1_2)
|
||||
otError Mac::SetDomainName(const char *aNameString)
|
||||
{
|
||||
// When setting Domain Name from a string, we treat it as `NameData`
|
||||
// with `kMaxSize + 1` chars. `DomainName::Set(data)` will look
|
||||
// for null char in the data (within its given size) to calculate
|
||||
// the name's length and ensure that the name fits in `kMaxSize`
|
||||
// chars. The `+ 1` ensures that a `aNameString` with length
|
||||
// longer than `kMaxSize` is correctly rejected (returning error
|
||||
// `OT_ERROR_INVALID_ARGS`).
|
||||
|
||||
NameData data(aNameString, DomainName::kMaxSize + 1);
|
||||
|
||||
return SetDomainName(data);
|
||||
}
|
||||
|
||||
otError Mac::SetDomainName(const NameData &aNameData)
|
||||
{
|
||||
otError error = mDomainName.Set(aNameData);
|
||||
|
||||
if (error == OT_ERROR_ALREADY)
|
||||
{
|
||||
error = OT_ERROR_NONE;
|
||||
}
|
||||
|
||||
return error;
|
||||
}
|
||||
#endif // (OPENTHREAD_CONFIG_THREAD_VERSION >= OT_THREAD_VERSION_1_2)
|
||||
|
||||
void Mac::SetPanId(PanId aPanId)
|
||||
{
|
||||
SuccessOrExit(Get<Notifier>().Update(mPanId, aPanId, OT_CHANGED_THREAD_PANID));
|
||||
|
||||
+41
-5
@@ -367,7 +367,39 @@ public:
|
||||
* @retval OT_ERROR_INVALID_ARGS Given name is too long.
|
||||
*
|
||||
*/
|
||||
otError SetNetworkName(const NetworkName::Data &aNameData);
|
||||
otError SetNetworkName(const NameData &aNameData);
|
||||
|
||||
#if (OPENTHREAD_CONFIG_THREAD_VERSION >= OT_THREAD_VERSION_1_2)
|
||||
/**
|
||||
* This method returns the Thread Domain Name.
|
||||
*
|
||||
* @returns The Thread Domain Name.
|
||||
*
|
||||
*/
|
||||
const DomainName &GetDomainName(void) const { return mDomainName; }
|
||||
|
||||
/**
|
||||
* This method sets the Thread Domain Name.
|
||||
*
|
||||
* @param[in] aNameString A pointer to a string character array. Must be null terminated.
|
||||
*
|
||||
* @retval OT_ERROR_NONE Successfully set the Thread Domain Name.
|
||||
* @retval OT_ERROR_INVALID_ARGS Given name is too long.
|
||||
*
|
||||
*/
|
||||
otError SetDomainName(const char *aNameString);
|
||||
|
||||
/**
|
||||
* This method sets the Thread Domain Name.
|
||||
*
|
||||
* @param[in] aNameData A name data (pointer to char buffer and length).
|
||||
*
|
||||
* @retval OT_ERROR_NONE Successfully set the Thread Domain Name.
|
||||
* @retval OT_ERROR_INVALID_ARGS Given name is too long.
|
||||
*
|
||||
*/
|
||||
otError SetDomainName(const NameData &aNameData);
|
||||
#endif // (OPENTHREAD_CONFIG_THREAD_VERSION >= OT_THREAD_VERSION_1_2)
|
||||
|
||||
/**
|
||||
* This method returns the IEEE 802.15.4 PAN ID.
|
||||
@@ -737,6 +769,7 @@ private:
|
||||
static const otExtAddress sMode2ExtAddress;
|
||||
static const otExtendedPanId sExtendedPanidInit;
|
||||
static const char sNetworkNameInit[];
|
||||
static const char sDomainNameInit[];
|
||||
|
||||
bool mEnabled : 1;
|
||||
bool mPendingActiveScan : 1;
|
||||
@@ -769,10 +802,13 @@ private:
|
||||
ChannelMask mSupportedChannelMask;
|
||||
ExtendedPanId mExtendedPanId;
|
||||
NetworkName mNetworkName;
|
||||
uint8_t mScanChannel;
|
||||
uint16_t mScanDuration;
|
||||
ChannelMask mScanChannelMask;
|
||||
uint8_t mMaxFrameRetriesDirect;
|
||||
#if (OPENTHREAD_CONFIG_THREAD_VERSION >= OT_THREAD_VERSION_1_2)
|
||||
DomainName mDomainName;
|
||||
#endif
|
||||
uint8_t mScanChannel;
|
||||
uint16_t mScanDuration;
|
||||
ChannelMask mScanChannelMask;
|
||||
uint8_t mMaxFrameRetriesDirect;
|
||||
#if OPENTHREAD_FTD
|
||||
uint8_t mMaxFrameRetriesIndirect;
|
||||
#endif
|
||||
|
||||
@@ -1312,18 +1312,18 @@ public:
|
||||
/**
|
||||
* This method gets the Network Name field.
|
||||
*
|
||||
* @returns The Network Name field as `NetworkName::Data`.
|
||||
* @returns The Network Name field as `NameData`.
|
||||
*
|
||||
*/
|
||||
NetworkName::Data GetNetworkName(void) const { return NetworkName::Data(mNetworkName, sizeof(mNetworkName)); }
|
||||
NameData GetNetworkName(void) const { return NameData(mNetworkName, sizeof(mNetworkName)); }
|
||||
|
||||
/**
|
||||
* This method sets the Network Name field.
|
||||
*
|
||||
* @param[in] aNameData The Network Name (as a `NetworkName::Data`).
|
||||
* @param[in] aNameData The Network Name (as a `NameData`).
|
||||
*
|
||||
*/
|
||||
void SetNetworkName(const NetworkName::Data &aNameData) { aNameData.CopyTo(mNetworkName, sizeof(mNetworkName)); }
|
||||
void SetNetworkName(const NameData &aNameData) { aNameData.CopyTo(mNetworkName, sizeof(mNetworkName)); }
|
||||
|
||||
/**
|
||||
* This method returns the Extended PAN ID field.
|
||||
|
||||
@@ -105,7 +105,7 @@ ExtendedPanId::InfoString ExtendedPanId::ToString(void) const
|
||||
return InfoString("%02x%02x%02x%02x%02x%02x%02x%02x", m8[0], m8[1], m8[2], m8[3], m8[4], m8[5], m8[6], m8[7]);
|
||||
}
|
||||
|
||||
uint8_t NetworkName::Data::CopyTo(char *aBuffer, uint8_t aMaxSize) const
|
||||
uint8_t NameData::CopyTo(char *aBuffer, uint8_t aMaxSize) const
|
||||
{
|
||||
uint8_t len = GetLength();
|
||||
|
||||
@@ -121,14 +121,14 @@ uint8_t NetworkName::Data::CopyTo(char *aBuffer, uint8_t aMaxSize) const
|
||||
return len;
|
||||
}
|
||||
|
||||
NetworkName::Data NetworkName::GetAsData(void) const
|
||||
NameData NetworkName::GetAsData(void) const
|
||||
{
|
||||
uint8_t len = static_cast<uint8_t>(StringLength(m8, kMaxSize + 1));
|
||||
|
||||
return Data(m8, len);
|
||||
return NameData(m8, len);
|
||||
}
|
||||
|
||||
otError NetworkName::Set(const Data &aNameData)
|
||||
otError NetworkName::Set(const NameData &aNameData)
|
||||
{
|
||||
otError error = OT_ERROR_NONE;
|
||||
uint8_t newLen = static_cast<uint8_t>(StringLength(aNameData.GetBuffer(), aNameData.GetLength()));
|
||||
@@ -145,5 +145,31 @@ exit:
|
||||
return error;
|
||||
}
|
||||
|
||||
#if (OPENTHREAD_CONFIG_THREAD_VERSION >= OT_THREAD_VERSION_1_2)
|
||||
NameData DomainName::GetAsData(void) const
|
||||
{
|
||||
uint8_t len = static_cast<uint8_t>(StringLength(m8, kMaxSize + 1));
|
||||
|
||||
return NameData(m8, len);
|
||||
}
|
||||
|
||||
otError DomainName::Set(const NameData &aNameData)
|
||||
{
|
||||
otError error = OT_ERROR_NONE;
|
||||
uint8_t newLen = static_cast<uint8_t>(StringLength(aNameData.GetBuffer(), aNameData.GetLength()));
|
||||
|
||||
VerifyOrExit(newLen <= kMaxSize, error = OT_ERROR_INVALID_ARGS);
|
||||
|
||||
// Ensure the new name does not match the current one.
|
||||
VerifyOrExit(memcmp(m8, aNameData.GetBuffer(), newLen) || (m8[newLen] != '\0'), error = OT_ERROR_ALREADY);
|
||||
|
||||
memcpy(m8, aNameData.GetBuffer(), newLen);
|
||||
m8[newLen] = '\0';
|
||||
|
||||
exit:
|
||||
return error;
|
||||
}
|
||||
#endif // (OPENTHREAD_CONFIG_THREAD_VERSION >= OT_THREAD_VERSION_1_2)
|
||||
|
||||
} // namespace Mac
|
||||
} // namespace ot
|
||||
|
||||
+114
-61
@@ -40,6 +40,7 @@
|
||||
#include <string.h>
|
||||
|
||||
#include <openthread/link.h>
|
||||
#include <openthread/thread.h>
|
||||
|
||||
#include "common/string.hpp"
|
||||
|
||||
@@ -498,6 +499,63 @@ public:
|
||||
|
||||
} OT_TOOL_PACKED_END;
|
||||
|
||||
/**
|
||||
* This class represents a name string as data (pointer to a char buffer along with a length).
|
||||
*
|
||||
* @note The char array does NOT need to be null terminated.
|
||||
*
|
||||
*/
|
||||
class NameData
|
||||
{
|
||||
public:
|
||||
/**
|
||||
* This constructor initializes the NameData object.
|
||||
*
|
||||
* @param[in] aBuffer A pointer to a `char` buffer (does not need to be null terminated).
|
||||
* @param[in] aLength The length (number of chars) in the buffer.
|
||||
*
|
||||
*/
|
||||
NameData(const char *aBuffer, uint8_t aLength)
|
||||
: mBuffer(aBuffer)
|
||||
, mLength(aLength)
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* This method returns the pointer to char buffer (not necessarily null terminated).
|
||||
*
|
||||
* @returns The pointer to the char buffer.
|
||||
*
|
||||
*/
|
||||
const char *GetBuffer(void) const { return mBuffer; }
|
||||
|
||||
/**
|
||||
* This method returns the length (number of chars in buffer).
|
||||
*
|
||||
* @returns The name length.
|
||||
*
|
||||
*/
|
||||
uint8_t GetLength(void) const { return mLength; }
|
||||
|
||||
/**
|
||||
* This method copies the name data into a given char buffer with a given size.
|
||||
*
|
||||
* The given buffer is cleared (`memset` to zero) before copying the name into it. The copied string
|
||||
* in @p aBuffer is NOT necessarily null terminated.
|
||||
*
|
||||
* @param[out] aBuffer A pointer to a buffer where to copy the name into.
|
||||
* @param[in] aMaxSize Size of @p aBuffer (maximum number of chars to write into @p aBuffer).
|
||||
*
|
||||
* @returns The actual number of chars copied into @p aBuffer.
|
||||
*
|
||||
*/
|
||||
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.
|
||||
*
|
||||
@@ -510,63 +568,6 @@ public:
|
||||
kMaxSize = OT_NETWORK_NAME_MAX_SIZE, // Maximum number of chars in Network Name (excludes null char).
|
||||
};
|
||||
|
||||
/**
|
||||
* This class represents an IEEE802.15.4 Network Name as Data (pointer to a char buffer along with a length).
|
||||
*
|
||||
* @note The char array does NOT need to be null terminated.
|
||||
*
|
||||
*/
|
||||
class Data
|
||||
{
|
||||
public:
|
||||
/**
|
||||
* This constructor initializes the Data object.
|
||||
*
|
||||
* @param[in] aBuffer A pointer to a `char` buffer (does not need to be null terminated).
|
||||
* @param[in] aLength The length (number of chars) in the buffer.
|
||||
*
|
||||
*/
|
||||
Data(const char *aBuffer, uint8_t aLength)
|
||||
: mBuffer(aBuffer)
|
||||
, mLength(aLength)
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* This method returns the pointer to char buffer (not necessarily null terminated).
|
||||
*
|
||||
* @returns The pointer to the char buffer.
|
||||
*
|
||||
*/
|
||||
const char *GetBuffer(void) const { return mBuffer; }
|
||||
|
||||
/**
|
||||
* This method returns the length (number of chars in buffer).
|
||||
*
|
||||
* @returns The name length.
|
||||
*
|
||||
*/
|
||||
uint8_t GetLength(void) const { return mLength; }
|
||||
|
||||
/**
|
||||
* This method copies the name data into a given char buffer with a given size.
|
||||
*
|
||||
* The given buffer is cleared (`memset` to zero) before copying the Network Name into it. The copied string
|
||||
* in @p aBuffer is NOT necessarily null terminated.
|
||||
*
|
||||
* @param[out] aBuffer A pointer to a buffer where to copy the Network Name into.
|
||||
* @param[in] aMaxSize Size of @p aBuffer (maximum number of chars to write into @p aBuffer).
|
||||
*
|
||||
* @returns The actual number of chars copied into @p aBuffer.
|
||||
*
|
||||
*/
|
||||
uint8_t CopyTo(char *aBuffer, uint8_t aMaxSize) const;
|
||||
|
||||
private:
|
||||
const char *mBuffer;
|
||||
uint8_t mLength;
|
||||
};
|
||||
|
||||
/**
|
||||
* This constructor initializes the IEEE802.15.4 Network Name as an empty string.
|
||||
*
|
||||
@@ -582,12 +583,12 @@ public:
|
||||
const char *GetAsCString(void) const { return m8; }
|
||||
|
||||
/**
|
||||
* This method gets the IEEE802.15.4 Network Name as Data.
|
||||
* This method gets the IEEE802.15.4 Network Name as NameData.
|
||||
*
|
||||
* @returns The Network Name as Data.
|
||||
* @returns The Network Name as NameData.
|
||||
*
|
||||
*/
|
||||
Data GetAsData(void) const;
|
||||
NameData GetAsData(void) const;
|
||||
|
||||
/**
|
||||
* This method sets the IEEE 802.15.4 Network Name.
|
||||
@@ -599,9 +600,61 @@ public:
|
||||
* @retval OT_ERROR_INVALID_ARGS Given name is too long.
|
||||
*
|
||||
*/
|
||||
otError Set(const Data &aNameData);
|
||||
otError Set(const NameData &aNameData);
|
||||
};
|
||||
|
||||
#if (OPENTHREAD_CONFIG_THREAD_VERSION >= OT_THREAD_VERSION_1_2)
|
||||
/**
|
||||
* This structure represents a Thread Domain Name.
|
||||
*
|
||||
*/
|
||||
class DomainName
|
||||
{
|
||||
public:
|
||||
enum
|
||||
{
|
||||
kMaxSize = 16, // Maximum number of chars in Domain Name (excludes null char).
|
||||
};
|
||||
|
||||
/**
|
||||
* This constructor initializes the Thread Domain Name as an empty string.
|
||||
*
|
||||
*/
|
||||
DomainName(void) { m8[0] = '\0'; }
|
||||
|
||||
/**
|
||||
* This method gets the Thread Domain Name as a null terminated C string.
|
||||
*
|
||||
* @returns The Domain Name as a null terminated C string array.
|
||||
*
|
||||
*/
|
||||
const char *GetAsCString(void) const { return m8; }
|
||||
|
||||
/**
|
||||
* This method gets the Thread Domain Name as NameData.
|
||||
*
|
||||
* @returns The Domain Name as NameData.
|
||||
*
|
||||
*/
|
||||
NameData GetAsData(void) const;
|
||||
|
||||
/**
|
||||
* This method sets the Thread Domain Name.
|
||||
*
|
||||
* @param[in] aNameData A reference to name data.
|
||||
*
|
||||
* @retval OT_ERROR_NONE Successfully set the Thread Domain Name.
|
||||
* @retval OT_ERROR_ALREADY The name is already set to the same string.
|
||||
* @retval OT_ERROR_INVALID_ARGS Given name is too long.
|
||||
*
|
||||
*/
|
||||
otError Set(const NameData &aNameData);
|
||||
|
||||
private:
|
||||
char m8[kMaxSize + 1]; ///< Byte values.
|
||||
};
|
||||
#endif // (OPENTHREAD_CONFIG_THREAD_VERSION >= OT_THREAD_VERSION_1_2)
|
||||
|
||||
/**
|
||||
* @}
|
||||
*
|
||||
|
||||
@@ -83,7 +83,7 @@ bool Tlv::IsValid(const Tlv &aTlv)
|
||||
return rval;
|
||||
}
|
||||
|
||||
Mac::NetworkName::Data NetworkNameTlv::GetNetworkName(void) const
|
||||
Mac::NameData NetworkNameTlv::GetNetworkName(void) const
|
||||
{
|
||||
uint8_t len = GetLength();
|
||||
|
||||
@@ -92,10 +92,10 @@ Mac::NetworkName::Data NetworkNameTlv::GetNetworkName(void) const
|
||||
len = sizeof(mNetworkName);
|
||||
}
|
||||
|
||||
return Mac::NetworkName::Data(mNetworkName, len);
|
||||
return Mac::NameData(mNetworkName, len);
|
||||
}
|
||||
|
||||
void NetworkNameTlv::SetNetworkName(const Mac::NetworkName::Data &aNameData)
|
||||
void NetworkNameTlv::SetNetworkName(const Mac::NameData &aNameData)
|
||||
{
|
||||
uint8_t len;
|
||||
|
||||
|
||||
@@ -406,18 +406,18 @@ public:
|
||||
/**
|
||||
* This method gets the Network Name value.
|
||||
*
|
||||
* @returns The Network Name value (as `NetworkName::Data`).
|
||||
* @returns The Network Name value (as `NameData`).
|
||||
*
|
||||
*/
|
||||
Mac::NetworkName::Data GetNetworkName(void) const;
|
||||
Mac::NameData GetNetworkName(void) const;
|
||||
|
||||
/**
|
||||
* This method sets the Network Name value.
|
||||
*
|
||||
* @param[in] aNameData A Network Name value (as `NetworkName::Data`).
|
||||
* @param[in] aNameData A Network Name value (as `NameData`).
|
||||
*
|
||||
*/
|
||||
void SetNetworkName(const Mac::NetworkName::Data &aNameData);
|
||||
void SetNetworkName(const Mac::NameData &aNameData);
|
||||
|
||||
private:
|
||||
char mNetworkName[Mac::NetworkName::kMaxSize];
|
||||
|
||||
@@ -179,55 +179,55 @@ void TestMacNetworkName(void)
|
||||
|
||||
CompareNetworkName(networkName, kEmptyName);
|
||||
|
||||
SuccessOrQuit(networkName.Set(Mac::NetworkName::Data(kName1, sizeof(kName1))), "NetworkName::Set() failed");
|
||||
SuccessOrQuit(networkName.Set(Mac::NameData(kName1, sizeof(kName1))), "NetworkName::Set() failed");
|
||||
CompareNetworkName(networkName, kName1);
|
||||
|
||||
VerifyOrQuit(networkName.Set(Mac::NetworkName::Data(kName1, sizeof(kName1))) == OT_ERROR_ALREADY,
|
||||
VerifyOrQuit(networkName.Set(Mac::NameData(kName1, sizeof(kName1))) == OT_ERROR_ALREADY,
|
||||
"NetworkName::Set() accepted same name without returning OT_ERROR_ALREADY");
|
||||
CompareNetworkName(networkName, kName1);
|
||||
|
||||
VerifyOrQuit(networkName.Set(Mac::NetworkName::Data(kName1, sizeof(kName1) - 1)) == OT_ERROR_ALREADY,
|
||||
VerifyOrQuit(networkName.Set(Mac::NameData(kName1, sizeof(kName1) - 1)) == OT_ERROR_ALREADY,
|
||||
"NetworkName::Set() accepted same name without returning OT_ERROR_ALREADY");
|
||||
|
||||
SuccessOrQuit(networkName.Set(Mac::NetworkName::Data(kName2, sizeof(kName2))), "NetworkName::Set() failed");
|
||||
SuccessOrQuit(networkName.Set(Mac::NameData(kName2, sizeof(kName2))), "NetworkName::Set() failed");
|
||||
CompareNetworkName(networkName, kName2);
|
||||
|
||||
SuccessOrQuit(networkName.Set(Mac::NetworkName::Data(kEmptyName, 0)), "NetworkName::Set() failed");
|
||||
SuccessOrQuit(networkName.Set(Mac::NameData(kEmptyName, 0)), "NetworkName::Set() failed");
|
||||
CompareNetworkName(networkName, kEmptyName);
|
||||
|
||||
SuccessOrQuit(networkName.Set(Mac::NetworkName::Data(kLongName, sizeof(kLongName))), "NetworkName::Set() failed");
|
||||
SuccessOrQuit(networkName.Set(Mac::NameData(kLongName, sizeof(kLongName))), "NetworkName::Set() failed");
|
||||
CompareNetworkName(networkName, kLongName);
|
||||
|
||||
VerifyOrQuit(networkName.Set(Mac::NetworkName::Data(kLongName, sizeof(kLongName) - 1)) == OT_ERROR_ALREADY,
|
||||
VerifyOrQuit(networkName.Set(Mac::NameData(kLongName, sizeof(kLongName) - 1)) == OT_ERROR_ALREADY,
|
||||
"NetworkName::Set() accepted same name without returning OT_ERROR_ALREADY");
|
||||
|
||||
SuccessOrQuit(networkName.Set(Mac::NetworkName::Data(NULL, 0)), "NetworkName::Set() failed");
|
||||
SuccessOrQuit(networkName.Set(Mac::NameData(NULL, 0)), "NetworkName::Set() failed");
|
||||
CompareNetworkName(networkName, kEmptyName);
|
||||
|
||||
SuccessOrQuit(networkName.Set(Mac::NetworkName::Data(kName1, sizeof(kName1))), "NetworkName::Set() failed");
|
||||
SuccessOrQuit(networkName.Set(Mac::NameData(kName1, sizeof(kName1))), "NetworkName::Set() failed");
|
||||
|
||||
VerifyOrQuit(networkName.Set(Mac::NetworkName::Data(kTooLongName, sizeof(kTooLongName))) == OT_ERROR_INVALID_ARGS,
|
||||
VerifyOrQuit(networkName.Set(Mac::NameData(kTooLongName, sizeof(kTooLongName))) == OT_ERROR_INVALID_ARGS,
|
||||
"NetworkName::Set() accepted an invalid (too long) name");
|
||||
|
||||
CompareNetworkName(networkName, kName1);
|
||||
|
||||
memset(buffer, 'a', sizeof(buffer));
|
||||
len = networkName.GetAsData().CopyTo(buffer, 1);
|
||||
VerifyOrQuit(len == 1, "NetworkName::Data::CopyTo() failed");
|
||||
VerifyOrQuit(buffer[0] == kName1[0], "NetworkName::Data::CopyTo() failed");
|
||||
VerifyOrQuit(buffer[1] == 'a', "NetworkName::Data::CopyTo() failed");
|
||||
VerifyOrQuit(len == 1, "NameData::CopyTo() failed");
|
||||
VerifyOrQuit(buffer[0] == kName1[0], "NameData::CopyTo() failed");
|
||||
VerifyOrQuit(buffer[1] == 'a', "NameData::CopyTo() failed");
|
||||
|
||||
memset(buffer, 'a', sizeof(buffer));
|
||||
len = networkName.GetAsData().CopyTo(buffer, sizeof(kName1) - 1);
|
||||
VerifyOrQuit(len == sizeof(kName1) - 1, "NetworkName::Data::CopyTo() failed");
|
||||
VerifyOrQuit(memcmp(buffer, kName1, sizeof(kName1) - 1) == 0, "NetworkName::Data::CopyTo() failed");
|
||||
VerifyOrQuit(buffer[sizeof(kName1)] == 'a', "NetworkName::Data::CopyTo() failed");
|
||||
VerifyOrQuit(len == sizeof(kName1) - 1, "NameData::CopyTo() failed");
|
||||
VerifyOrQuit(memcmp(buffer, kName1, sizeof(kName1) - 1) == 0, "NameData::CopyTo() failed");
|
||||
VerifyOrQuit(buffer[sizeof(kName1)] == 'a', "NameData::CopyTo() failed");
|
||||
|
||||
memset(buffer, 'a', sizeof(buffer));
|
||||
len = networkName.GetAsData().CopyTo(buffer, sizeof(buffer));
|
||||
VerifyOrQuit(len == sizeof(kName1) - 1, "NetworkName::Data::CopyTo() failed");
|
||||
VerifyOrQuit(memcmp(buffer, kName1, sizeof(kName1) - 1) == 0, "NetworkName::Data::CopyTo() failed");
|
||||
VerifyOrQuit(buffer[sizeof(kName1)] == 0, "NetworkName::Data::CopyTo() failed");
|
||||
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");
|
||||
}
|
||||
|
||||
void TestMacHeader(void)
|
||||
|
||||
Reference in New Issue
Block a user