Skip to content

Commit

Permalink
[diag] use CmdLineParser to parse numbers in diag commands
Browse files Browse the repository at this point in the history
  • Loading branch information
zhanglongxia committed Dec 10, 2024
1 parent a0f861d commit 035ccd3
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 71 deletions.
110 changes: 42 additions & 68 deletions src/core/diags/factory_diags.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,15 +82,15 @@ Diags::Diags(Instance &aInstance)

Error Diags::ProcessChannel(uint8_t aArgsLength, char *aArgs[])
{
Error error = kErrorNone;
long value;
Error error = kErrorNone;
uint8_t channel;

VerifyOrExit(aArgsLength == 1, error = kErrorInvalidArgs);

SuccessOrExit(error = ParseLong(aArgs[0], value));
VerifyOrExit(value >= Radio::kChannelMin && value <= Radio::kChannelMax, error = kErrorInvalidArgs);
SuccessOrExit(error = Utils::CmdLineParser::ParseAsUint8(aArgs[0], channel));
VerifyOrExit(channel >= Radio::kChannelMin && channel <= Radio::kChannelMax, error = kErrorInvalidArgs);

otPlatDiagChannelSet(static_cast<uint8_t>(value));
otPlatDiagChannelSet(channel);

exit:
AppendErrorResult(error);
Expand All @@ -99,14 +99,14 @@ Error Diags::ProcessChannel(uint8_t aArgsLength, char *aArgs[])

Error Diags::ProcessPower(uint8_t aArgsLength, char *aArgs[])
{
Error error = kErrorNone;
long value;
Error error = kErrorNone;
int8_t power;

VerifyOrExit(aArgsLength == 1, error = kErrorInvalidArgs);

SuccessOrExit(error = ParseLong(aArgs[0], value));
SuccessOrExit(error = Utils::CmdLineParser::ParseAsInt8(aArgs[0], power));

otPlatDiagTxPowerSet(static_cast<int8_t>(value));
otPlatDiagTxPowerSet(power);

exit:
AppendErrorResult(error);
Expand All @@ -127,12 +127,11 @@ Error Diags::ProcessEcho(uint8_t aArgsLength, char *aArgs[])
static constexpr uint16_t kOutputLen = OPENTHREAD_CONFIG_DIAG_OUTPUT_BUFFER_SIZE;
static constexpr uint16_t kOutputMaxLen = kOutputLen - kReservedLen;
char output[kOutputLen];
long value;
uint32_t i;
uint32_t number;

SuccessOrExit(error = ParseLong(aArgs[1], value));
number = Min(static_cast<uint32_t>(value), static_cast<uint32_t>(kOutputMaxLen));
SuccessOrExit(error = Utils::CmdLineParser::ParseAsUint32(aArgs[1], number));
number = Min(number, static_cast<uint32_t>(kOutputMaxLen));

for (i = 0; i < number; i++)
{
Expand Down Expand Up @@ -242,14 +241,11 @@ Error Diags::ProcessFrame(uint8_t aArgsLength, char *aArgs[])
}
else if (StringMatch(aArgs[0], "-p"))
{
long value;

aArgs++;
aArgsLength--;

VerifyOrExit(aArgsLength > 1, error = kErrorInvalidArgs);
SuccessOrExit(error = ParseLong(aArgs[0], value));
txPower = static_cast<int8_t>(value);
SuccessOrExit(error = Utils::CmdLineParser::ParseAsInt8(aArgs[0], txPower));
}
else if (StringMatch(aArgs[0], "-c"))
{
Expand Down Expand Up @@ -294,12 +290,12 @@ Error Diags::ProcessChannel(uint8_t aArgsLength, char *aArgs[])
}
else
{
long value;
uint8_t channel;

SuccessOrExit(error = ParseLong(aArgs[0], value));
VerifyOrExit(value >= Radio::kChannelMin && value <= Radio::kChannelMax, error = kErrorInvalidArgs);
SuccessOrExit(error = Utils::CmdLineParser::ParseAsUint8(aArgs[0], channel));
VerifyOrExit(channel >= Radio::kChannelMin && channel <= Radio::kChannelMax, error = kErrorInvalidArgs);

mChannel = static_cast<uint8_t>(value);
mChannel = channel;
IgnoreError(Get<Radio>().Receive(mChannel));
otPlatDiagChannelSet(mChannel);

Expand All @@ -323,11 +319,11 @@ Error Diags::ProcessPower(uint8_t aArgsLength, char *aArgs[])
}
else
{
long value;
int8_t txPower;

SuccessOrExit(error = ParseLong(aArgs[0], value));
SuccessOrExit(error = Utils::CmdLineParser::ParseAsInt8(aArgs[0], txPower));

mTxPower = static_cast<int8_t>(value);
mTxPower = txPower;
SuccessOrExit(error = Get<Radio>().SetTransmitPower(mTxPower));
otPlatDiagTxPowerSet(mTxPower);

Expand All @@ -354,30 +350,31 @@ Error Diags::ProcessRepeat(uint8_t aArgsLength, char *aArgs[])
}
else
{
long value;
uint32_t txPeriod;
uint8_t txLength;

VerifyOrExit(aArgsLength >= 1, error = kErrorInvalidArgs);

SuccessOrExit(error = ParseLong(aArgs[0], value));
mTxPeriod = static_cast<uint32_t>(value);
SuccessOrExit(error = Utils::CmdLineParser::ParseAsUint32(aArgs[0], txPeriod));
mTxPeriod = txPeriod;

if (aArgsLength >= 2)
{
SuccessOrExit(error = ParseLong(aArgs[1], value));
SuccessOrExit(error = Utils::CmdLineParser::ParseAsUint8(aArgs[1], txLength));
mIsTxPacketSet = false;
}
else if (mIsTxPacketSet)
{
value = mTxPacket->mLength;
txLength = mTxPacket->mLength;
}
else
{
ExitNow(error = kErrorInvalidArgs);
}

VerifyOrExit(value <= OT_RADIO_FRAME_MAX_SIZE, error = kErrorInvalidArgs);
VerifyOrExit(value >= OT_RADIO_FRAME_MIN_SIZE, error = kErrorInvalidArgs);
mTxLen = static_cast<uint8_t>(value);
VerifyOrExit((txLength >= OT_RADIO_FRAME_MIN_SIZE) && (txLength <= OT_RADIO_FRAME_MAX_SIZE),
error = kErrorInvalidArgs);
mTxLen = txLength;

mRepeatActive = true;
uint32_t now = otPlatAlarmMilliGetNow();
Expand All @@ -393,32 +390,33 @@ Error Diags::ProcessRepeat(uint8_t aArgsLength, char *aArgs[])

Error Diags::ProcessSend(uint8_t aArgsLength, char *aArgs[])
{
Error error = kErrorNone;
long value;
Error error = kErrorNone;
uint32_t txPackets;
uint8_t txLength;

VerifyOrExit(otPlatDiagModeGet(), error = kErrorInvalidState);
VerifyOrExit(aArgsLength >= 1, error = kErrorInvalidArgs);

SuccessOrExit(error = ParseLong(aArgs[0], value));
mTxPackets = static_cast<uint32_t>(value);
SuccessOrExit(error = Utils::CmdLineParser::ParseAsUint32(aArgs[0], txPackets));
mTxPackets = txPackets;

if (aArgsLength >= 2)
{
SuccessOrExit(ParseLong(aArgs[1], value));
SuccessOrExit(error = Utils::CmdLineParser::ParseAsUint8(aArgs[1], txLength));
mIsTxPacketSet = false;
}
else if (mIsTxPacketSet)
{
value = mTxPacket->mLength;
txLength = mTxPacket->mLength;
}
else
{
ExitNow(error = kErrorInvalidArgs);
}

VerifyOrExit(value <= OT_RADIO_FRAME_MAX_SIZE, error = kErrorInvalidArgs);
VerifyOrExit(value >= OT_RADIO_FRAME_MIN_SIZE, error = kErrorInvalidArgs);
mTxLen = static_cast<uint8_t>(value);
VerifyOrExit(txLength <= OT_RADIO_FRAME_MAX_SIZE, error = kErrorInvalidArgs);
VerifyOrExit(txLength >= OT_RADIO_FRAME_MIN_SIZE, error = kErrorInvalidArgs);
mTxLen = txLength;

Output("sending %#x packet(s), length %#x\r\nstatus 0x%02x\r\n", static_cast<int>(mTxPackets),
static_cast<int>(mTxLen), error);
Expand Down Expand Up @@ -795,29 +793,25 @@ Error Diags::ProcessRawPowerSetting(uint8_t aArgsLength, char *aArgs[])
Error Diags::ProcessGpio(uint8_t aArgsLength, char *aArgs[])
{
Error error = kErrorInvalidArgs;
long value;
uint32_t gpio;
bool level;
otGpioMode mode;

if ((aArgsLength == 2) && StringMatch(aArgs[0], "get"))
{
SuccessOrExit(error = ParseLong(aArgs[1], value));
gpio = static_cast<uint32_t>(value);
SuccessOrExit(error = Utils::CmdLineParser::ParseAsUint32(aArgs[1], gpio));
SuccessOrExit(error = otPlatDiagGpioGet(gpio, &level));
Output("%d\r\n", level);
}
else if ((aArgsLength == 3) && StringMatch(aArgs[0], "set"))
{
SuccessOrExit(error = ParseLong(aArgs[1], value));
gpio = static_cast<uint32_t>(value);
SuccessOrExit(error = ParseBool(aArgs[2], level));
SuccessOrExit(error = Utils::CmdLineParser::ParseAsUint32(aArgs[1], gpio));
SuccessOrExit(error = Utils::CmdLineParser::ParseAsBool(aArgs[2], level));
SuccessOrExit(error = otPlatDiagGpioSet(gpio, level));
}
else if ((aArgsLength >= 2) && StringMatch(aArgs[0], "mode"))
{
SuccessOrExit(error = ParseLong(aArgs[1], value));
gpio = static_cast<uint32_t>(value);
SuccessOrExit(error = Utils::CmdLineParser::ParseAsUint32(aArgs[1], gpio));

if (aArgsLength == 2)
{
Expand Down Expand Up @@ -854,26 +848,6 @@ void Diags::AppendErrorResult(Error aError)
}
}

Error Diags::ParseLong(char *aString, long &aLong)
{
char *endptr;
aLong = strtol(aString, &endptr, 0);
return (*endptr == '\0') ? kErrorNone : kErrorParse;
}

Error Diags::ParseBool(char *aString, bool &aBool)
{
Error error;
long value;

SuccessOrExit(error = ParseLong(aString, value));
VerifyOrExit((value == 0) || (value == 1), error = kErrorParse);
aBool = static_cast<bool>(value);

exit:
return error;
}

Error Diags::ParseCmd(char *aString, uint8_t &aArgsLength, char *aArgs[])
{
Error error;
Expand Down
3 changes: 0 additions & 3 deletions src/core/diags/factory_diags.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -204,9 +204,6 @@ class Diags : public InstanceLocator, private NonCopyable
void AppendErrorResult(Error aError);
void ResetTxPacket(void);

static Error ParseLong(char *aString, long &aLong);
static Error ParseBool(char *aString, bool &aBool);

static const struct Command sCommands[];

#if OPENTHREAD_FTD || OPENTHREAD_MTD || (OPENTHREAD_RADIO && OPENTHREAD_RADIO_CLI)
Expand Down

0 comments on commit 035ccd3

Please sign in to comment.