mirror of
https://github.com/espressif/openthread.git
synced 2026-07-29 15:17:47 +00:00
[cli] simplify arg checking/parsing with new Arg class (#6524)
This commit adds a new `Arg` class in `parse_cmdline` which represents
a single argument (wrapper over a C string). The `Arg` class is then
used in CLI modules. This helps simplify the CLI code mainly using
simpler syntax, e.g., using an overload of `==` operator to compare an
argument with a C string, or calling `ParseAs{Type}()` methods
directly on an `Arg` instance.
This commit is contained in:
+104
-112
@@ -42,13 +42,6 @@
|
||||
|
||||
#include "cli/cli.hpp"
|
||||
#include "common/string.hpp"
|
||||
#include "utils/parse_cmdline.hpp"
|
||||
|
||||
using ot::Utils::CmdLineParser::ParseAsHexString;
|
||||
using ot::Utils::CmdLineParser::ParseAsIp6Address;
|
||||
using ot::Utils::CmdLineParser::ParseAsUint16;
|
||||
using ot::Utils::CmdLineParser::ParseAsUint32;
|
||||
using ot::Utils::CmdLineParser::ParseAsUint64;
|
||||
|
||||
namespace ot {
|
||||
namespace Cli {
|
||||
@@ -134,7 +127,7 @@ otError Dataset::Print(otOperationalDataset &aDataset)
|
||||
return OT_ERROR_NONE;
|
||||
}
|
||||
|
||||
otError Dataset::Process(uint8_t aArgsLength, char *aArgs[])
|
||||
otError Dataset::Process(uint8_t aArgsLength, Arg aArgs[])
|
||||
{
|
||||
otError error = OT_ERROR_INVALID_COMMAND;
|
||||
const Command *command;
|
||||
@@ -144,7 +137,7 @@ otError Dataset::Process(uint8_t aArgsLength, char *aArgs[])
|
||||
ExitNow(error = Print(sDataset));
|
||||
}
|
||||
|
||||
command = Utils::LookupTable::Find(aArgs[0], sCommands);
|
||||
command = Utils::LookupTable::Find(aArgs[0].GetCString(), sCommands);
|
||||
VerifyOrExit(command != nullptr);
|
||||
|
||||
error = (this->*command->mHandler)(aArgsLength - 1, aArgs + 1);
|
||||
@@ -153,7 +146,7 @@ exit:
|
||||
return error;
|
||||
}
|
||||
|
||||
otError Dataset::ProcessHelp(uint8_t aArgsLength, char *aArgs[])
|
||||
otError Dataset::ProcessHelp(uint8_t aArgsLength, Arg aArgs[])
|
||||
{
|
||||
OT_UNUSED_VARIABLE(aArgsLength);
|
||||
OT_UNUSED_VARIABLE(aArgs);
|
||||
@@ -166,22 +159,22 @@ otError Dataset::ProcessHelp(uint8_t aArgsLength, char *aArgs[])
|
||||
return OT_ERROR_NONE;
|
||||
}
|
||||
|
||||
otError Dataset::ProcessInit(uint8_t aArgsLength, char *aArgs[])
|
||||
otError Dataset::ProcessInit(uint8_t aArgsLength, Arg aArgs[])
|
||||
{
|
||||
otError error = OT_ERROR_NONE;
|
||||
|
||||
VerifyOrExit(aArgsLength > 0, error = OT_ERROR_INVALID_ARGS);
|
||||
|
||||
if (strcmp(aArgs[0], "active") == 0)
|
||||
if (aArgs[0] == "active")
|
||||
{
|
||||
SuccessOrExit(error = otDatasetGetActive(mInterpreter.mInstance, &sDataset));
|
||||
}
|
||||
else if (strcmp(aArgs[0], "pending") == 0)
|
||||
else if (aArgs[0] == "pending")
|
||||
{
|
||||
SuccessOrExit(error = otDatasetGetPending(mInterpreter.mInstance, &sDataset));
|
||||
}
|
||||
#if OPENTHREAD_FTD
|
||||
else if (strcmp(aArgs[0], "new") == 0)
|
||||
else if (aArgs[0] == "new")
|
||||
{
|
||||
SuccessOrExit(error = otDatasetCreateNewNetwork(mInterpreter.mInstance, &sDataset));
|
||||
}
|
||||
@@ -195,7 +188,7 @@ exit:
|
||||
return error;
|
||||
}
|
||||
|
||||
otError Dataset::ProcessActive(uint8_t aArgsLength, char *aArgs[])
|
||||
otError Dataset::ProcessActive(uint8_t aArgsLength, Arg aArgs[])
|
||||
{
|
||||
otError error;
|
||||
|
||||
@@ -206,11 +199,11 @@ otError Dataset::ProcessActive(uint8_t aArgsLength, char *aArgs[])
|
||||
SuccessOrExit(error = otDatasetGetActive(mInterpreter.mInstance, &dataset));
|
||||
error = Print(dataset);
|
||||
}
|
||||
else if ((aArgsLength == 1) && (strcmp(aArgs[0], "-x") == 0))
|
||||
else if ((aArgsLength == 1) && (aArgs[0] == "-x"))
|
||||
{
|
||||
otOperationalDatasetTlvs dataset;
|
||||
|
||||
VerifyOrExit(strlen(aArgs[0]) <= OT_OPERATIONAL_DATASET_MAX_LENGTH * 2, error = OT_ERROR_NO_BUFS);
|
||||
VerifyOrExit(aArgs[0].GetLength() <= OT_OPERATIONAL_DATASET_MAX_LENGTH * 2, error = OT_ERROR_NO_BUFS);
|
||||
|
||||
SuccessOrExit(error = otDatasetGetActiveTlvs(mInterpreter.mInstance, &dataset));
|
||||
mInterpreter.OutputBytes(dataset.mTlvs, dataset.mLength);
|
||||
@@ -225,7 +218,7 @@ exit:
|
||||
return error;
|
||||
}
|
||||
|
||||
otError Dataset::ProcessPending(uint8_t aArgsLength, char *aArgs[])
|
||||
otError Dataset::ProcessPending(uint8_t aArgsLength, Arg aArgs[])
|
||||
{
|
||||
otError error;
|
||||
|
||||
@@ -236,11 +229,11 @@ otError Dataset::ProcessPending(uint8_t aArgsLength, char *aArgs[])
|
||||
SuccessOrExit(error = otDatasetGetPending(mInterpreter.mInstance, &dataset));
|
||||
error = Print(dataset);
|
||||
}
|
||||
else if ((aArgsLength == 1) && (strcmp(aArgs[0], "-x") == 0))
|
||||
else if ((aArgsLength == 1) && (aArgs[0] == "-x"))
|
||||
{
|
||||
otOperationalDatasetTlvs dataset;
|
||||
|
||||
VerifyOrExit(strlen(aArgs[0]) <= OT_OPERATIONAL_DATASET_MAX_LENGTH * 2, error = OT_ERROR_NO_BUFS);
|
||||
VerifyOrExit(aArgs[0].GetLength() <= OT_OPERATIONAL_DATASET_MAX_LENGTH * 2, error = OT_ERROR_NO_BUFS);
|
||||
|
||||
SuccessOrExit(error = otDatasetGetPendingTlvs(mInterpreter.mInstance, &dataset));
|
||||
mInterpreter.OutputBytes(dataset.mTlvs, dataset.mLength);
|
||||
@@ -255,7 +248,7 @@ exit:
|
||||
return error;
|
||||
}
|
||||
|
||||
otError Dataset::ProcessActiveTimestamp(uint8_t aArgsLength, char *aArgs[])
|
||||
otError Dataset::ProcessActiveTimestamp(uint8_t aArgsLength, Arg aArgs[])
|
||||
{
|
||||
otError error = OT_ERROR_NONE;
|
||||
|
||||
@@ -268,7 +261,7 @@ otError Dataset::ProcessActiveTimestamp(uint8_t aArgsLength, char *aArgs[])
|
||||
}
|
||||
else
|
||||
{
|
||||
SuccessOrExit(error = ParseAsUint64(aArgs[0], sDataset.mActiveTimestamp));
|
||||
SuccessOrExit(error = aArgs[0].ParseAsUint64(sDataset.mActiveTimestamp));
|
||||
sDataset.mComponents.mIsActiveTimestampPresent = true;
|
||||
}
|
||||
|
||||
@@ -276,7 +269,7 @@ exit:
|
||||
return error;
|
||||
}
|
||||
|
||||
otError Dataset::ProcessChannel(uint8_t aArgsLength, char *aArgs[])
|
||||
otError Dataset::ProcessChannel(uint8_t aArgsLength, Arg aArgs[])
|
||||
{
|
||||
otError error = OT_ERROR_NONE;
|
||||
|
||||
@@ -289,7 +282,7 @@ otError Dataset::ProcessChannel(uint8_t aArgsLength, char *aArgs[])
|
||||
}
|
||||
else
|
||||
{
|
||||
SuccessOrExit(error = ParseAsUint16(aArgs[0], sDataset.mChannel));
|
||||
SuccessOrExit(error = aArgs[0].ParseAsUint16(sDataset.mChannel));
|
||||
sDataset.mComponents.mIsChannelPresent = true;
|
||||
}
|
||||
|
||||
@@ -297,7 +290,7 @@ exit:
|
||||
return error;
|
||||
}
|
||||
|
||||
otError Dataset::ProcessChannelMask(uint8_t aArgsLength, char *aArgs[])
|
||||
otError Dataset::ProcessChannelMask(uint8_t aArgsLength, Arg aArgs[])
|
||||
{
|
||||
otError error = OT_ERROR_NONE;
|
||||
|
||||
@@ -310,7 +303,7 @@ otError Dataset::ProcessChannelMask(uint8_t aArgsLength, char *aArgs[])
|
||||
}
|
||||
else
|
||||
{
|
||||
SuccessOrExit(error = ParseAsUint32(aArgs[0], sDataset.mChannelMask));
|
||||
SuccessOrExit(error = aArgs[0].ParseAsUint32(sDataset.mChannelMask));
|
||||
sDataset.mComponents.mIsChannelMaskPresent = true;
|
||||
}
|
||||
|
||||
@@ -318,7 +311,7 @@ exit:
|
||||
return error;
|
||||
}
|
||||
|
||||
otError Dataset::ProcessClear(uint8_t aArgsLength, char *aArgs[])
|
||||
otError Dataset::ProcessClear(uint8_t aArgsLength, Arg aArgs[])
|
||||
{
|
||||
OT_UNUSED_VARIABLE(aArgsLength);
|
||||
OT_UNUSED_VARIABLE(aArgs);
|
||||
@@ -327,17 +320,17 @@ otError Dataset::ProcessClear(uint8_t aArgsLength, char *aArgs[])
|
||||
return OT_ERROR_NONE;
|
||||
}
|
||||
|
||||
otError Dataset::ProcessCommit(uint8_t aArgsLength, char *aArgs[])
|
||||
otError Dataset::ProcessCommit(uint8_t aArgsLength, Arg aArgs[])
|
||||
{
|
||||
otError error = OT_ERROR_NONE;
|
||||
|
||||
VerifyOrExit(aArgsLength > 0, error = OT_ERROR_INVALID_ARGS);
|
||||
|
||||
if (strcmp(aArgs[0], "active") == 0)
|
||||
if (aArgs[0] == "active")
|
||||
{
|
||||
SuccessOrExit(error = otDatasetSetActive(mInterpreter.mInstance, &sDataset));
|
||||
}
|
||||
else if (strcmp(aArgs[0], "pending") == 0)
|
||||
else if (aArgs[0] == "pending")
|
||||
{
|
||||
SuccessOrExit(error = otDatasetSetPending(mInterpreter.mInstance, &sDataset));
|
||||
}
|
||||
@@ -350,7 +343,7 @@ exit:
|
||||
return error;
|
||||
}
|
||||
|
||||
otError Dataset::ProcessDelay(uint8_t aArgsLength, char *aArgs[])
|
||||
otError Dataset::ProcessDelay(uint8_t aArgsLength, Arg aArgs[])
|
||||
{
|
||||
otError error = OT_ERROR_NONE;
|
||||
|
||||
@@ -363,7 +356,7 @@ otError Dataset::ProcessDelay(uint8_t aArgsLength, char *aArgs[])
|
||||
}
|
||||
else
|
||||
{
|
||||
SuccessOrExit(error = ParseAsUint32(aArgs[0], sDataset.mDelay));
|
||||
SuccessOrExit(error = aArgs[0].ParseAsUint32(sDataset.mDelay));
|
||||
sDataset.mComponents.mIsDelayPresent = true;
|
||||
}
|
||||
|
||||
@@ -371,7 +364,7 @@ exit:
|
||||
return error;
|
||||
}
|
||||
|
||||
otError Dataset::ProcessExtPanId(uint8_t aArgsLength, char *aArgs[])
|
||||
otError Dataset::ProcessExtPanId(uint8_t aArgsLength, Arg aArgs[])
|
||||
{
|
||||
otError error = OT_ERROR_NONE;
|
||||
|
||||
@@ -385,7 +378,7 @@ otError Dataset::ProcessExtPanId(uint8_t aArgsLength, char *aArgs[])
|
||||
}
|
||||
else
|
||||
{
|
||||
SuccessOrExit(error = ParseAsHexString(aArgs[0], sDataset.mExtendedPanId.m8));
|
||||
SuccessOrExit(error = aArgs[0].ParseAsHexString(sDataset.mExtendedPanId.m8));
|
||||
sDataset.mComponents.mIsExtendedPanIdPresent = true;
|
||||
}
|
||||
|
||||
@@ -393,7 +386,7 @@ exit:
|
||||
return error;
|
||||
}
|
||||
|
||||
otError Dataset::ProcessMasterKey(uint8_t aArgsLength, char *aArgs[])
|
||||
otError Dataset::ProcessMasterKey(uint8_t aArgsLength, Arg aArgs[])
|
||||
{
|
||||
otError error = OT_ERROR_NONE;
|
||||
|
||||
@@ -407,7 +400,7 @@ otError Dataset::ProcessMasterKey(uint8_t aArgsLength, char *aArgs[])
|
||||
}
|
||||
else
|
||||
{
|
||||
SuccessOrExit(error = ParseAsHexString(aArgs[0], sDataset.mMasterKey.m8));
|
||||
SuccessOrExit(error = aArgs[0].ParseAsHexString(sDataset.mMasterKey.m8));
|
||||
sDataset.mComponents.mIsMasterKeyPresent = true;
|
||||
}
|
||||
|
||||
@@ -415,7 +408,7 @@ exit:
|
||||
return error;
|
||||
}
|
||||
|
||||
otError Dataset::ProcessMeshLocalPrefix(uint8_t aArgsLength, char *aArgs[])
|
||||
otError Dataset::ProcessMeshLocalPrefix(uint8_t aArgsLength, Arg aArgs[])
|
||||
{
|
||||
otError error = OT_ERROR_NONE;
|
||||
|
||||
@@ -435,7 +428,7 @@ otError Dataset::ProcessMeshLocalPrefix(uint8_t aArgsLength, char *aArgs[])
|
||||
{
|
||||
otIp6Address prefix;
|
||||
|
||||
SuccessOrExit(error = ParseAsIp6Address(aArgs[0], prefix));
|
||||
SuccessOrExit(error = aArgs[0].ParseAsIp6Address(prefix));
|
||||
|
||||
memcpy(sDataset.mMeshLocalPrefix.m8, prefix.mFields.m8, sizeof(sDataset.mMeshLocalPrefix.m8));
|
||||
sDataset.mComponents.mIsMeshLocalPrefixPresent = true;
|
||||
@@ -445,7 +438,7 @@ exit:
|
||||
return error;
|
||||
}
|
||||
|
||||
otError Dataset::ProcessNetworkName(uint8_t aArgsLength, char *aArgs[])
|
||||
otError Dataset::ProcessNetworkName(uint8_t aArgsLength, Arg aArgs[])
|
||||
{
|
||||
otError error = OT_ERROR_NONE;
|
||||
|
||||
@@ -459,13 +452,13 @@ otError Dataset::ProcessNetworkName(uint8_t aArgsLength, char *aArgs[])
|
||||
}
|
||||
else
|
||||
{
|
||||
size_t length;
|
||||
uint16_t length;
|
||||
|
||||
VerifyOrExit((length = strlen(aArgs[0])) <= OT_NETWORK_NAME_MAX_SIZE, error = OT_ERROR_INVALID_ARGS);
|
||||
VerifyOrExit(IsValidUtf8String(aArgs[0]), error = OT_ERROR_INVALID_ARGS);
|
||||
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], length);
|
||||
memcpy(sDataset.mNetworkName.m8, aArgs[0].GetCString(), length);
|
||||
sDataset.mComponents.mIsNetworkNamePresent = true;
|
||||
}
|
||||
|
||||
@@ -473,7 +466,7 @@ exit:
|
||||
return error;
|
||||
}
|
||||
|
||||
otError Dataset::ProcessPanId(uint8_t aArgsLength, char *aArgs[])
|
||||
otError Dataset::ProcessPanId(uint8_t aArgsLength, Arg aArgs[])
|
||||
{
|
||||
otError error = OT_ERROR_NONE;
|
||||
|
||||
@@ -486,7 +479,7 @@ otError Dataset::ProcessPanId(uint8_t aArgsLength, char *aArgs[])
|
||||
}
|
||||
else
|
||||
{
|
||||
SuccessOrExit(error = ParseAsUint16(aArgs[0], sDataset.mPanId));
|
||||
SuccessOrExit(error = aArgs[0].ParseAsUint16(sDataset.mPanId));
|
||||
sDataset.mComponents.mIsPanIdPresent = true;
|
||||
}
|
||||
|
||||
@@ -494,7 +487,7 @@ exit:
|
||||
return error;
|
||||
}
|
||||
|
||||
otError Dataset::ProcessPendingTimestamp(uint8_t aArgsLength, char *aArgs[])
|
||||
otError Dataset::ProcessPendingTimestamp(uint8_t aArgsLength, Arg aArgs[])
|
||||
{
|
||||
otError error = OT_ERROR_NONE;
|
||||
|
||||
@@ -507,7 +500,7 @@ otError Dataset::ProcessPendingTimestamp(uint8_t aArgsLength, char *aArgs[])
|
||||
}
|
||||
else
|
||||
{
|
||||
SuccessOrExit(error = ParseAsUint64(aArgs[0], sDataset.mPendingTimestamp));
|
||||
SuccessOrExit(error = aArgs[0].ParseAsUint64(sDataset.mPendingTimestamp));
|
||||
sDataset.mComponents.mIsPendingTimestampPresent = true;
|
||||
}
|
||||
|
||||
@@ -515,7 +508,7 @@ exit:
|
||||
return error;
|
||||
}
|
||||
|
||||
otError Dataset::ProcessMgmtSetCommand(uint8_t aArgsLength, char *aArgs[])
|
||||
otError Dataset::ProcessMgmtSetCommand(uint8_t aArgsLength, Arg aArgs[])
|
||||
{
|
||||
otError error = OT_ERROR_NONE;
|
||||
otOperationalDataset dataset;
|
||||
@@ -528,88 +521,88 @@ otError Dataset::ProcessMgmtSetCommand(uint8_t aArgsLength, char *aArgs[])
|
||||
|
||||
for (uint8_t index = 1; index < aArgsLength; index++)
|
||||
{
|
||||
if (strcmp(aArgs[index], "activetimestamp") == 0)
|
||||
if (aArgs[index] == "activetimestamp")
|
||||
{
|
||||
VerifyOrExit(++index < aArgsLength, error = OT_ERROR_INVALID_ARGS);
|
||||
dataset.mComponents.mIsActiveTimestampPresent = true;
|
||||
SuccessOrExit(error = ParseAsUint64(aArgs[index], dataset.mActiveTimestamp));
|
||||
SuccessOrExit(error = aArgs[index].ParseAsUint64(dataset.mActiveTimestamp));
|
||||
}
|
||||
else if (strcmp(aArgs[index], "pendingtimestamp") == 0)
|
||||
else if (aArgs[index] == "pendingtimestamp")
|
||||
{
|
||||
VerifyOrExit(++index < aArgsLength, error = OT_ERROR_INVALID_ARGS);
|
||||
dataset.mComponents.mIsPendingTimestampPresent = true;
|
||||
SuccessOrExit(error = ParseAsUint64(aArgs[index], dataset.mPendingTimestamp));
|
||||
SuccessOrExit(error = aArgs[index].ParseAsUint64(dataset.mPendingTimestamp));
|
||||
}
|
||||
else if (strcmp(aArgs[index], "masterkey") == 0)
|
||||
else if (aArgs[index] == "masterkey")
|
||||
{
|
||||
VerifyOrExit(++index < aArgsLength, error = OT_ERROR_INVALID_ARGS);
|
||||
dataset.mComponents.mIsMasterKeyPresent = true;
|
||||
SuccessOrExit(error = ParseAsHexString(aArgs[index], dataset.mMasterKey.m8));
|
||||
SuccessOrExit(error = aArgs[index].ParseAsHexString(dataset.mMasterKey.m8));
|
||||
}
|
||||
else if (strcmp(aArgs[index], "networkname") == 0)
|
||||
else if (aArgs[index] == "networkname")
|
||||
{
|
||||
size_t length;
|
||||
uint16_t length;
|
||||
|
||||
VerifyOrExit(++index < aArgsLength, error = OT_ERROR_INVALID_ARGS);
|
||||
dataset.mComponents.mIsNetworkNamePresent = true;
|
||||
VerifyOrExit((length = strlen(aArgs[index])) <= OT_NETWORK_NAME_MAX_SIZE, error = OT_ERROR_INVALID_ARGS);
|
||||
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], length);
|
||||
memcpy(dataset.mNetworkName.m8, aArgs[index].GetCString(), length);
|
||||
}
|
||||
else if (strcmp(aArgs[index], "extpanid") == 0)
|
||||
else if (aArgs[index] == "extpanid")
|
||||
{
|
||||
VerifyOrExit(++index < aArgsLength, error = OT_ERROR_INVALID_ARGS);
|
||||
dataset.mComponents.mIsExtendedPanIdPresent = true;
|
||||
SuccessOrExit(error = ParseAsHexString(aArgs[index], dataset.mExtendedPanId.m8));
|
||||
SuccessOrExit(error = aArgs[index].ParseAsHexString(dataset.mExtendedPanId.m8));
|
||||
}
|
||||
else if (strcmp(aArgs[index], "localprefix") == 0)
|
||||
else if (aArgs[index] == "localprefix")
|
||||
{
|
||||
otIp6Address prefix;
|
||||
|
||||
VerifyOrExit(++index < aArgsLength, error = OT_ERROR_INVALID_ARGS);
|
||||
dataset.mComponents.mIsMeshLocalPrefixPresent = true;
|
||||
SuccessOrExit(error = ParseAsIp6Address(aArgs[index], prefix));
|
||||
SuccessOrExit(error = aArgs[index].ParseAsIp6Address(prefix));
|
||||
memcpy(dataset.mMeshLocalPrefix.m8, prefix.mFields.m8, sizeof(dataset.mMeshLocalPrefix.m8));
|
||||
}
|
||||
else if (strcmp(aArgs[index], "delaytimer") == 0)
|
||||
else if (aArgs[index] == "delaytimer")
|
||||
{
|
||||
VerifyOrExit(++index < aArgsLength, error = OT_ERROR_INVALID_ARGS);
|
||||
dataset.mComponents.mIsDelayPresent = true;
|
||||
SuccessOrExit(error = ParseAsUint32(aArgs[index], dataset.mDelay));
|
||||
SuccessOrExit(error = aArgs[index].ParseAsUint32(dataset.mDelay));
|
||||
}
|
||||
else if (strcmp(aArgs[index], "panid") == 0)
|
||||
else if (aArgs[index] == "panid")
|
||||
{
|
||||
VerifyOrExit(++index < aArgsLength, error = OT_ERROR_INVALID_ARGS);
|
||||
dataset.mComponents.mIsPanIdPresent = true;
|
||||
SuccessOrExit(error = ParseAsUint16(aArgs[index], dataset.mPanId));
|
||||
SuccessOrExit(error = aArgs[index].ParseAsUint16(dataset.mPanId));
|
||||
}
|
||||
else if (strcmp(aArgs[index], "channel") == 0)
|
||||
else if (aArgs[index] == "channel")
|
||||
{
|
||||
VerifyOrExit(++index < aArgsLength, error = OT_ERROR_INVALID_ARGS);
|
||||
dataset.mComponents.mIsChannelPresent = true;
|
||||
SuccessOrExit(error = ParseAsUint16(aArgs[index], dataset.mChannel));
|
||||
SuccessOrExit(error = aArgs[index].ParseAsUint16(dataset.mChannel));
|
||||
}
|
||||
else if (strcmp(aArgs[index], "channelmask") == 0)
|
||||
else if (aArgs[index] == "channelmask")
|
||||
{
|
||||
VerifyOrExit(++index < aArgsLength, error = OT_ERROR_INVALID_ARGS);
|
||||
dataset.mComponents.mIsChannelMaskPresent = true;
|
||||
SuccessOrExit(error = ParseAsUint32(aArgs[index], dataset.mChannelMask));
|
||||
SuccessOrExit(error = aArgs[index].ParseAsUint32(dataset.mChannelMask));
|
||||
}
|
||||
else if (strcmp(aArgs[index], "securitypolicy") == 0)
|
||||
else if (aArgs[index] == "securitypolicy")
|
||||
{
|
||||
VerifyOrExit(++index < aArgsLength, error = OT_ERROR_INVALID_ARGS);
|
||||
SuccessOrExit(error = ParseSecurityPolicy(dataset.mSecurityPolicy, aArgs[index],
|
||||
index + 1 < aArgsLength ? aArgs[index + 1] : nullptr));
|
||||
SuccessOrExit(error = ParseSecurityPolicy(dataset.mSecurityPolicy, aArgsLength - index, &aArgs[index]));
|
||||
dataset.mComponents.mIsSecurityPolicyPresent = true;
|
||||
++index;
|
||||
}
|
||||
else if (strcmp(aArgs[index], "-x") == 0)
|
||||
else if (aArgs[index] == "-x")
|
||||
{
|
||||
uint16_t length;
|
||||
|
||||
VerifyOrExit(++index < aArgsLength, error = OT_ERROR_INVALID_ARGS);
|
||||
length = sizeof(tlvs);
|
||||
SuccessOrExit(error = ParseAsHexString(aArgs[index], length, tlvs));
|
||||
SuccessOrExit(error = aArgs[index].ParseAsHexString(length, tlvs));
|
||||
tlvsLength = static_cast<uint8_t>(length);
|
||||
}
|
||||
else
|
||||
@@ -618,11 +611,11 @@ otError Dataset::ProcessMgmtSetCommand(uint8_t aArgsLength, char *aArgs[])
|
||||
}
|
||||
}
|
||||
|
||||
if (strcmp(aArgs[0], "active") == 0)
|
||||
if (aArgs[0] == "active")
|
||||
{
|
||||
SuccessOrExit(error = otDatasetSendMgmtActiveSet(mInterpreter.mInstance, &dataset, tlvs, tlvsLength));
|
||||
}
|
||||
else if (strcmp(aArgs[0], "pending") == 0)
|
||||
else if (aArgs[0] == "pending")
|
||||
{
|
||||
SuccessOrExit(error = otDatasetSendMgmtPendingSet(mInterpreter.mInstance, &dataset, tlvs, tlvsLength));
|
||||
}
|
||||
@@ -635,7 +628,7 @@ exit:
|
||||
return error;
|
||||
}
|
||||
|
||||
otError Dataset::ProcessMgmtGetCommand(uint8_t aArgsLength, char *aArgs[])
|
||||
otError Dataset::ProcessMgmtGetCommand(uint8_t aArgsLength, Arg aArgs[])
|
||||
{
|
||||
otError error = OT_ERROR_NONE;
|
||||
otOperationalDatasetComponents datasetComponents;
|
||||
@@ -650,59 +643,59 @@ otError Dataset::ProcessMgmtGetCommand(uint8_t aArgsLength, char *aArgs[])
|
||||
|
||||
for (uint8_t index = 1; index < aArgsLength; index++)
|
||||
{
|
||||
if (strcmp(aArgs[index], "activetimestamp") == 0)
|
||||
if (aArgs[index] == "activetimestamp")
|
||||
{
|
||||
datasetComponents.mIsActiveTimestampPresent = true;
|
||||
}
|
||||
else if (strcmp(aArgs[index], "pendingtimestamp") == 0)
|
||||
else if (aArgs[index] == "pendingtimestamp")
|
||||
{
|
||||
datasetComponents.mIsPendingTimestampPresent = true;
|
||||
}
|
||||
else if (strcmp(aArgs[index], "masterkey") == 0)
|
||||
else if (aArgs[index] == "masterkey")
|
||||
{
|
||||
datasetComponents.mIsMasterKeyPresent = true;
|
||||
}
|
||||
else if (strcmp(aArgs[index], "networkname") == 0)
|
||||
else if (aArgs[index] == "networkname")
|
||||
{
|
||||
datasetComponents.mIsNetworkNamePresent = true;
|
||||
}
|
||||
else if (strcmp(aArgs[index], "extpanid") == 0)
|
||||
else if (aArgs[index] == "extpanid")
|
||||
{
|
||||
datasetComponents.mIsExtendedPanIdPresent = true;
|
||||
}
|
||||
else if (strcmp(aArgs[index], "localprefix") == 0)
|
||||
else if (aArgs[index] == "localprefix")
|
||||
{
|
||||
datasetComponents.mIsMeshLocalPrefixPresent = true;
|
||||
}
|
||||
else if (strcmp(aArgs[index], "delaytimer") == 0)
|
||||
else if (aArgs[index] == "delaytimer")
|
||||
{
|
||||
datasetComponents.mIsDelayPresent = true;
|
||||
}
|
||||
else if (strcmp(aArgs[index], "panid") == 0)
|
||||
else if (aArgs[index] == "panid")
|
||||
{
|
||||
datasetComponents.mIsPanIdPresent = true;
|
||||
}
|
||||
else if (strcmp(aArgs[index], "channel") == 0)
|
||||
else if (aArgs[index] == "channel")
|
||||
{
|
||||
datasetComponents.mIsChannelPresent = true;
|
||||
}
|
||||
else if (strcmp(aArgs[index], "securitypolicy") == 0)
|
||||
else if (aArgs[index] == "securitypolicy")
|
||||
{
|
||||
datasetComponents.mIsSecurityPolicyPresent = true;
|
||||
}
|
||||
else if (strcmp(aArgs[index], "-x") == 0)
|
||||
else if (aArgs[index] == "-x")
|
||||
{
|
||||
uint16_t length;
|
||||
|
||||
VerifyOrExit(++index < aArgsLength, error = OT_ERROR_INVALID_ARGS);
|
||||
length = sizeof(tlvs);
|
||||
SuccessOrExit(error = ParseAsHexString(aArgs[index], length, tlvs));
|
||||
SuccessOrExit(error = aArgs[index].ParseAsHexString(length, tlvs));
|
||||
tlvsLength = static_cast<uint8_t>(length);
|
||||
}
|
||||
else if (strcmp(aArgs[index], "address") == 0)
|
||||
else if (aArgs[index] == "address")
|
||||
{
|
||||
VerifyOrExit(++index < aArgsLength, error = OT_ERROR_INVALID_ARGS);
|
||||
SuccessOrExit(error = ParseAsIp6Address(aArgs[index], address));
|
||||
SuccessOrExit(error = aArgs[index].ParseAsIp6Address(address));
|
||||
destAddrSpecified = true;
|
||||
}
|
||||
else
|
||||
@@ -711,12 +704,12 @@ otError Dataset::ProcessMgmtGetCommand(uint8_t aArgsLength, char *aArgs[])
|
||||
}
|
||||
}
|
||||
|
||||
if (strcmp(aArgs[0], "active") == 0)
|
||||
if (aArgs[0] == "active")
|
||||
{
|
||||
SuccessOrExit(error = otDatasetSendMgmtActiveGet(mInterpreter.mInstance, &datasetComponents, tlvs, tlvsLength,
|
||||
destAddrSpecified ? &address : nullptr));
|
||||
}
|
||||
else if (strcmp(aArgs[0], "pending") == 0)
|
||||
else if (aArgs[0] == "pending")
|
||||
{
|
||||
SuccessOrExit(error = otDatasetSendMgmtPendingGet(mInterpreter.mInstance, &datasetComponents, tlvs, tlvsLength,
|
||||
destAddrSpecified ? &address : nullptr));
|
||||
@@ -730,7 +723,7 @@ exit:
|
||||
return error;
|
||||
}
|
||||
|
||||
otError Dataset::ProcessPskc(uint8_t aArgsLength, char *aArgs[])
|
||||
otError Dataset::ProcessPskc(uint8_t aArgsLength, Arg aArgs[])
|
||||
{
|
||||
otError error = OT_ERROR_NONE;
|
||||
|
||||
@@ -744,14 +737,14 @@ otError Dataset::ProcessPskc(uint8_t aArgsLength, char *aArgs[])
|
||||
}
|
||||
else if (aArgsLength == 1)
|
||||
{
|
||||
SuccessOrExit(error = ParseAsHexString(aArgs[0], sDataset.mPskc.m8));
|
||||
SuccessOrExit(error = aArgs[0].ParseAsHexString(sDataset.mPskc.m8));
|
||||
}
|
||||
#if OPENTHREAD_FTD
|
||||
else if (aArgsLength == 2 && !strcmp(aArgs[0], "-p"))
|
||||
else if (aArgsLength == 2 && (aArgs[0] == "-p"))
|
||||
{
|
||||
SuccessOrExit(
|
||||
error = otDatasetGeneratePskc(
|
||||
aArgs[1],
|
||||
aArgs[1].GetCString(),
|
||||
(sDataset.mComponents.mIsNetworkNamePresent
|
||||
? &sDataset.mNetworkName
|
||||
: reinterpret_cast<const otNetworkName *>(otThreadGetNetworkName(mInterpreter.mInstance))),
|
||||
@@ -821,17 +814,17 @@ void Dataset::OutputSecurityPolicy(const otSecurityPolicy &aSecurityPolicy)
|
||||
}
|
||||
}
|
||||
|
||||
Error Dataset::ParseSecurityPolicy(otSecurityPolicy &aSecurityPolicy, const char *aRotation, const char *aFlags)
|
||||
Error Dataset::ParseSecurityPolicy(otSecurityPolicy &aSecurityPolicy, uint8_t aArgsLength, Arg aArgs[])
|
||||
{
|
||||
Error error;
|
||||
otSecurityPolicy policy;
|
||||
|
||||
memset(&policy, 0, sizeof(policy));
|
||||
SuccessOrExit(error = ParseAsUint16(aRotation, policy.mRotationTime));
|
||||
SuccessOrExit(error = aArgs[0].ParseAsUint16(policy.mRotationTime));
|
||||
|
||||
VerifyOrExit(aFlags != nullptr);
|
||||
VerifyOrExit(aArgsLength >= 1);
|
||||
|
||||
for (const char *flag = aFlags; *flag != '\0'; flag++)
|
||||
for (const char *flag = aArgs[1].GetCString(); *flag != '\0'; flag++)
|
||||
{
|
||||
switch (*flag)
|
||||
{
|
||||
@@ -884,7 +877,7 @@ exit:
|
||||
return error;
|
||||
}
|
||||
|
||||
otError Dataset::ProcessSecurityPolicy(uint8_t aArgsLength, char *aArgs[])
|
||||
otError Dataset::ProcessSecurityPolicy(uint8_t aArgsLength, Arg aArgs[])
|
||||
{
|
||||
otError error = OT_ERROR_NONE;
|
||||
|
||||
@@ -898,8 +891,7 @@ otError Dataset::ProcessSecurityPolicy(uint8_t aArgsLength, char *aArgs[])
|
||||
}
|
||||
else
|
||||
{
|
||||
SuccessOrExit(
|
||||
error = ParseSecurityPolicy(sDataset.mSecurityPolicy, aArgs[0], aArgsLength > 1 ? aArgs[1] : nullptr));
|
||||
SuccessOrExit(error = ParseSecurityPolicy(sDataset.mSecurityPolicy, aArgsLength, aArgs));
|
||||
sDataset.mComponents.mIsSecurityPolicyPresent = true;
|
||||
}
|
||||
|
||||
@@ -907,18 +899,18 @@ exit:
|
||||
return error;
|
||||
}
|
||||
|
||||
otError Dataset::ProcessSet(uint8_t aArgsLength, char *aArgs[])
|
||||
otError Dataset::ProcessSet(uint8_t aArgsLength, Arg aArgs[])
|
||||
{
|
||||
otError error = OT_ERROR_NONE;
|
||||
MeshCoP::Dataset::Type datasetType;
|
||||
|
||||
VerifyOrExit(aArgsLength == 2, error = OT_ERROR_INVALID_ARGS);
|
||||
|
||||
if (strcmp(aArgs[0], "active") == 0)
|
||||
if (aArgs[0] == "active")
|
||||
{
|
||||
datasetType = MeshCoP::Dataset::Type::kActive;
|
||||
}
|
||||
else if (strcmp(aArgs[0], "pending") == 0)
|
||||
else if (aArgs[0] == "pending")
|
||||
{
|
||||
datasetType = MeshCoP::Dataset::Type::kPending;
|
||||
}
|
||||
@@ -932,7 +924,7 @@ otError Dataset::ProcessSet(uint8_t aArgsLength, char *aArgs[])
|
||||
MeshCoP::Dataset::Info datasetInfo;
|
||||
uint16_t tlvsLength = MeshCoP::Dataset::kMaxSize;
|
||||
|
||||
SuccessOrExit(error = ParseAsHexString(aArgs[1], tlvsLength, dataset.GetBytes()));
|
||||
SuccessOrExit(error = aArgs[1].ParseAsHexString(tlvsLength, dataset.GetBytes()));
|
||||
dataset.SetSize(tlvsLength);
|
||||
VerifyOrExit(dataset.IsValid(), error = OT_ERROR_INVALID_ARGS);
|
||||
dataset.ConvertTo(datasetInfo);
|
||||
@@ -954,7 +946,7 @@ exit:
|
||||
|
||||
#if OPENTHREAD_CONFIG_DATASET_UPDATER_ENABLE && OPENTHREAD_FTD
|
||||
|
||||
otError Dataset::ProcessUpdater(uint8_t aArgsLength, char *aArgs[])
|
||||
otError Dataset::ProcessUpdater(uint8_t aArgsLength, Arg aArgs[])
|
||||
{
|
||||
otError error = OT_ERROR_NONE;
|
||||
|
||||
@@ -964,11 +956,11 @@ otError Dataset::ProcessUpdater(uint8_t aArgsLength, char *aArgs[])
|
||||
ExitNow();
|
||||
}
|
||||
|
||||
if (strcmp(aArgs[0], "start") == 0)
|
||||
if (aArgs[0] == "start")
|
||||
{
|
||||
error = otDatasetUpdaterRequestUpdate(mInterpreter.mInstance, &sDataset, &Dataset::HandleDatasetUpdater, this);
|
||||
}
|
||||
else if (strcmp(aArgs[0], "cancel") == 0)
|
||||
else if (aArgs[0] == "cancel")
|
||||
{
|
||||
otDatasetUpdaterCancelUpdate(mInterpreter.mInstance);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user