mirror of
https://github.com/espressif/openthread.git
synced 2026-08-05 10:27:49 +00:00
[diag] use CmdLineParser to parse numbers in diag commands (#11009)
This commit is contained in:
@@ -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);
|
||||
@@ -99,14 +99,14 @@ exit:
|
||||
|
||||
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);
|
||||
@@ -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++)
|
||||
{
|
||||
@@ -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"))
|
||||
{
|
||||
@@ -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);
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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();
|
||||
@@ -393,32 +390,33 @@ exit:
|
||||
|
||||
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);
|
||||
@@ -795,29 +793,25 @@ exit:
|
||||
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)
|
||||
{
|
||||
@@ -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;
|
||||
|
||||
@@ -204,9 +204,6 @@ private:
|
||||
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)
|
||||
|
||||
Reference in New Issue
Block a user