From 035ccd3cec0d3b01c130866e10412f08106685ef Mon Sep 17 00:00:00 2001 From: Zhanglong Xia Date: Fri, 6 Dec 2024 12:37:36 +0800 Subject: [PATCH] [diag] use CmdLineParser to parse numbers in diag commands --- src/core/diags/factory_diags.cpp | 110 ++++++++++++------------------- src/core/diags/factory_diags.hpp | 3 - 2 files changed, 42 insertions(+), 71 deletions(-) diff --git a/src/core/diags/factory_diags.cpp b/src/core/diags/factory_diags.cpp index ab88579f34d..c879f862887 100644 --- a/src/core/diags/factory_diags.cpp +++ b/src/core/diags/factory_diags.cpp @@ -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(value)); + otPlatDiagChannelSet(channel); exit: AppendErrorResult(error); @@ -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(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(value), static_cast(kOutputMaxLen)); + SuccessOrExit(error = Utils::CmdLineParser::ParseAsUint32(aArgs[1], number)); + number = Min(number, static_cast(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(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(value); + mChannel = channel; IgnoreError(Get().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(value); + mTxPower = txPower; SuccessOrExit(error = Get().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(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(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 @@ 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(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(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(mTxPackets), static_cast(mTxLen), error); @@ -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(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(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(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(value); - -exit: - return error; -} - Error Diags::ParseCmd(char *aString, uint8_t &aArgsLength, char *aArgs[]) { Error error; diff --git a/src/core/diags/factory_diags.hpp b/src/core/diags/factory_diags.hpp index a3f87042c2c..a538edf93fe 100644 --- a/src/core/diags/factory_diags.hpp +++ b/src/core/diags/factory_diags.hpp @@ -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)