mirror of
https://github.com/espressif/openthread.git
synced 2026-08-28 04:49:54 +00:00
[mac] new public API to validate and set otNetworkName (#6703)
This commit adds a new public API `otNetworkNameFromString()` to set an `otNetworkName` instance from a given null terminated C string. The new API will also validate that the given name string follows UTF-8 encoding and its length is not longer than max network name size. This commit also simplifies the UTF-8 validation of `NetworkName` by moving the check from `Mac::SetNetworkName()` to a newly added method in `Mac::NetworkName` to set it from a C string. It also defines `DomainName` as a `typedef` of `NetworkName` which helps simplify its use and remove repeated code. Finally, it updates the `Cli::Dataset` to use the new public API for validating `otNetworkName`.
This commit is contained in:
+8
-20
@@ -41,7 +41,6 @@
|
||||
#include <openthread/dataset_updater.h>
|
||||
|
||||
#include "cli/cli.hpp"
|
||||
#include "common/string.hpp"
|
||||
|
||||
namespace ot {
|
||||
namespace Cli {
|
||||
@@ -100,7 +99,7 @@ otError Dataset::Print(otOperationalDataset &aDataset)
|
||||
if (aDataset.mComponents.mIsNetworkNamePresent)
|
||||
{
|
||||
mInterpreter.OutputFormat("Network Name: ");
|
||||
mInterpreter.OutputLine("%.*s", static_cast<uint16_t>(sizeof(aDataset.mNetworkName)), aDataset.mNetworkName.m8);
|
||||
mInterpreter.OutputLine("%s", aDataset.mNetworkName.m8);
|
||||
}
|
||||
|
||||
if (aDataset.mComponents.mIsPanIdPresent)
|
||||
@@ -441,19 +440,12 @@ otError Dataset::ProcessNetworkName(uint8_t aArgsLength, Arg aArgs[])
|
||||
{
|
||||
if (sDataset.mComponents.mIsNetworkNamePresent)
|
||||
{
|
||||
mInterpreter.OutputLine("%.*s", static_cast<uint16_t>(sizeof(sDataset.mNetworkName)),
|
||||
sDataset.mNetworkName.m8);
|
||||
mInterpreter.OutputLine("%s", sDataset.mNetworkName.m8);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
uint16_t length;
|
||||
|
||||
VerifyOrExit((length = aArgs[0].GetLength()) <= OT_NETWORK_NAME_MAX_SIZE, error = OT_ERROR_INVALID_ARGS);
|
||||
VerifyOrExit(IsValidUtf8String(aArgs[0].GetCString()), error = OT_ERROR_INVALID_ARGS);
|
||||
|
||||
memset(&sDataset.mNetworkName, 0, sizeof(sDataset.mNetworkName));
|
||||
memcpy(sDataset.mNetworkName.m8, aArgs[0].GetCString(), length);
|
||||
SuccessOrExit(error = otNetworkNameFromString(&sDataset.mNetworkName, aArgs[0].GetCString()));
|
||||
sDataset.mComponents.mIsNetworkNamePresent = true;
|
||||
}
|
||||
|
||||
@@ -536,14 +528,9 @@ otError Dataset::ProcessMgmtSetCommand(uint8_t aArgsLength, Arg aArgs[])
|
||||
}
|
||||
else if (aArgs[index] == "networkname")
|
||||
{
|
||||
uint16_t length;
|
||||
|
||||
VerifyOrExit(++index < aArgsLength, error = OT_ERROR_INVALID_ARGS);
|
||||
dataset.mComponents.mIsNetworkNamePresent = true;
|
||||
VerifyOrExit((length = aArgs[index].GetLength()) <= OT_NETWORK_NAME_MAX_SIZE,
|
||||
error = OT_ERROR_INVALID_ARGS);
|
||||
memset(&dataset.mNetworkName, 0, sizeof(sDataset.mNetworkName));
|
||||
memcpy(dataset.mNetworkName.m8, aArgs[index].GetCString(), length);
|
||||
SuccessOrExit(error = otNetworkNameFromString(&dataset.mNetworkName, aArgs[index].GetCString()));
|
||||
}
|
||||
else if (aArgs[index] == "extpanid")
|
||||
{
|
||||
@@ -809,9 +796,9 @@ void Dataset::OutputSecurityPolicy(const otSecurityPolicy &aSecurityPolicy)
|
||||
}
|
||||
}
|
||||
|
||||
Error Dataset::ParseSecurityPolicy(otSecurityPolicy &aSecurityPolicy, uint8_t aArgsLength, Arg aArgs[])
|
||||
otError Dataset::ParseSecurityPolicy(otSecurityPolicy &aSecurityPolicy, uint8_t aArgsLength, Arg aArgs[])
|
||||
{
|
||||
Error error;
|
||||
otError error;
|
||||
otSecurityPolicy policy;
|
||||
|
||||
memset(&policy, 0, sizeof(policy));
|
||||
@@ -865,10 +852,11 @@ Error Dataset::ParseSecurityPolicy(otSecurityPolicy &aSecurityPolicy, uint8_t aA
|
||||
}
|
||||
|
||||
exit:
|
||||
if (error == kErrorNone)
|
||||
if (error == OT_ERROR_NONE)
|
||||
{
|
||||
aSecurityPolicy = policy;
|
||||
}
|
||||
|
||||
return error;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user