diff --git a/src/cli/cli.cpp b/src/cli/cli.cpp index ec8dd16e5..0ee7eb60c 100644 --- a/src/cli/cli.cpp +++ b/src/cli/cli.cpp @@ -230,8 +230,12 @@ otError Interpreter::ParseEnableOrDisable(const Arg &aArg, bool &aEnable) otError Interpreter::ParseJoinerDiscerner(Arg &aArg, otJoinerDiscerner &aDiscerner) { - otError error = OT_ERROR_NONE; - char * separator = strstr(aArg.GetCString(), "/"); + otError error; + char * separator; + + VerifyOrExit(!aArg.IsEmpty(), error = OT_ERROR_INVALID_ARGS); + + separator = strstr(aArg.GetCString(), "/"); VerifyOrExit(separator != nullptr, error = OT_ERROR_NOT_FOUND); @@ -295,9 +299,8 @@ exit: #endif // OPENTHREAD_CONFIG_PING_SENDER_ENABLE -otError Interpreter::ProcessHelp(uint8_t aArgsLength, Arg aArgs[]) +otError Interpreter::ProcessHelp(Arg aArgs[]) { - OT_UNUSED_VARIABLE(aArgsLength); OT_UNUSED_VARIABLE(aArgs); for (const Command &command : sCommands) @@ -314,12 +317,10 @@ otError Interpreter::ProcessHelp(uint8_t aArgsLength, Arg aArgs[]) } #if OPENTHREAD_CONFIG_BORDER_AGENT_ENABLE -otError Interpreter::ProcessBorderAgent(uint8_t aArgsLength, Arg aArgs[]) +otError Interpreter::ProcessBorderAgent(Arg aArgs[]) { otError error = OT_ERROR_NONE; - VerifyOrExit(aArgsLength == 1, error = OT_ERROR_INVALID_ARGS); - if (aArgs[0] == "port") { OutputLine("%hu", otBorderAgentGetUdpPort(mInstance)); @@ -356,13 +357,11 @@ exit: #endif #if OPENTHREAD_CONFIG_BORDER_ROUTING_ENABLE -otError Interpreter::ProcessBorderRouting(uint8_t aArgsLength, Arg aArgs[]) +otError Interpreter::ProcessBorderRouting(Arg aArgs[]) { otError error = OT_ERROR_NONE; bool enable; - VerifyOrExit(aArgsLength == 1, error = OT_ERROR_INVALID_ARGS); - SuccessOrExit(error = ParseEnableOrDisable(aArgs[0], enable)); error = otBorderRoutingSetEnabled(mInstance, enable); @@ -372,13 +371,12 @@ exit: #endif #if (OPENTHREAD_CONFIG_THREAD_VERSION >= OT_THREAD_VERSION_1_2) -otError Interpreter::ProcessBackboneRouter(uint8_t aArgsLength, Arg aArgs[]) +otError Interpreter::ProcessBackboneRouter(Arg aArgs[]) { - OT_UNUSED_VARIABLE(aArgs); otError error = OT_ERROR_INVALID_COMMAND; otBackboneRouterConfig config; - if (aArgsLength == 0) + if (aArgs[0].IsEmpty()) { if (otBackboneRouterGetPrimary(mInstance, &config) == OT_ERROR_NONE) { @@ -400,7 +398,7 @@ otError Interpreter::ProcessBackboneRouter(uint8_t aArgsLength, Arg aArgs[]) { if (aArgs[0] == "mgmt") { - if (aArgsLength < 2) + if (aArgs[1].IsEmpty()) { ExitNow(error = OT_ERROR_INVALID_COMMAND); } @@ -411,14 +409,13 @@ otError Interpreter::ProcessBackboneRouter(uint8_t aArgsLength, Arg aArgs[]) otIp6InterfaceIdentifier *mlIid = nullptr; otIp6InterfaceIdentifier iid; - VerifyOrExit((aArgsLength == 3 || aArgsLength == 4), error = OT_ERROR_INVALID_ARGS); - SuccessOrExit(error = aArgs[2].ParseAsUint8(status)); - if (aArgsLength == 4) + if (!aArgs[3].IsEmpty()) { SuccessOrExit(error = aArgs[3].ParseAsHexString(iid.mFields.m8)); mlIid = &iid; + VerifyOrExit(aArgs[4].IsEmpty(), error = OT_ERROR_INVALID_ARGS); } otBackboneRouterConfigNextDuaRegistrationResponse(mInstance, mlIid, status); @@ -428,12 +425,12 @@ otError Interpreter::ProcessBackboneRouter(uint8_t aArgsLength, Arg aArgs[]) #if OPENTHREAD_CONFIG_BACKBONE_ROUTER_MULTICAST_ROUTING_ENABLE else if (aArgs[1] == "mlr") { - error = ProcessBackboneRouterMgmtMlr(aArgsLength - 2, aArgs + 2); + error = ProcessBackboneRouterMgmtMlr(aArgs + 2); ExitNow(); } #endif } - SuccessOrExit(error = ProcessBackboneRouterLocal(aArgsLength, aArgs)); + SuccessOrExit(error = ProcessBackboneRouterLocal(aArgs)); } exit: @@ -444,21 +441,20 @@ exit: #if OPENTHREAD_FTD && OPENTHREAD_CONFIG_BACKBONE_ROUTER_ENABLE #if OPENTHREAD_CONFIG_BACKBONE_ROUTER_MULTICAST_ROUTING_ENABLE -otError Interpreter::ProcessBackboneRouterMgmtMlr(uint8_t aArgsLength, Arg aArgs[]) +otError Interpreter::ProcessBackboneRouterMgmtMlr(Arg aArgs[]) { otError error = OT_ERROR_INVALID_COMMAND; - VerifyOrExit(aArgsLength >= 1); - if (aArgs[0] == "listener") { - if (aArgsLength == 1) + if (aArgs[1].IsEmpty()) { PrintMulticastListenersTable(); - error = OT_ERROR_NONE; + ExitNow(error = OT_ERROR_NONE); } + #if OPENTHREAD_CONFIG_REFERENCE_DEVICE_ENABLE - else if (aArgs[1] == "clear") + if (aArgs[1] == "clear") { otBackboneRouterMulticastListenerClear(mInstance); error = OT_ERROR_NONE; @@ -468,28 +464,21 @@ otError Interpreter::ProcessBackboneRouterMgmtMlr(uint8_t aArgsLength, Arg aArgs otIp6Address address; uint32_t timeout = 0; - VerifyOrExit(aArgsLength == 3 || aArgsLength == 4, error = OT_ERROR_INVALID_ARGS); - SuccessOrExit(error = aArgs[2].ParseAsIp6Address(address)); - if (aArgsLength == 4) + if (!aArgs[3].IsEmpty()) { SuccessOrExit(error = aArgs[3].ParseAsUint32(timeout)); + VerifyOrExit(aArgs[4].IsEmpty(), error = OT_ERROR_INVALID_ARGS); } error = otBackboneRouterMulticastListenerAdd(mInstance, &address, timeout); } -#endif } -#if OPENTHREAD_CONFIG_REFERENCE_DEVICE_ENABLE else if (aArgs[0] == "response") { - error = ProcessSet(aArgsLength - 1, aArgs + 1, otBackboneRouterConfigNextMulticastListenerRegistrationResponse); - } + error = ProcessSet(aArgs + 1, otBackboneRouterConfigNextMulticastListenerRegistrationResponse); #endif - else - { - error = OT_ERROR_INVALID_COMMAND; } exit: @@ -510,7 +499,7 @@ void Interpreter::PrintMulticastListenersTable(void) #endif // OPENTHREAD_CONFIG_BACKBONE_ROUTER_MULTICAST_ROUTING_ENABLE -otError Interpreter::ProcessBackboneRouterLocal(uint8_t aArgsLength, Arg aArgs[]) +otError Interpreter::ProcessBackboneRouterLocal(Arg aArgs[]) { otError error = OT_ERROR_NONE; otBackboneRouterConfig config; @@ -522,8 +511,7 @@ otError Interpreter::ProcessBackboneRouterLocal(uint8_t aArgsLength, Arg aArgs[] } else if (aArgs[0] == "jitter") { - error = ProcessGetSet(aArgsLength - 1, aArgs + 1, otBackboneRouterGetRegistrationJitter, - otBackboneRouterSetRegistrationJitter); + error = ProcessGetSet(aArgs + 1, otBackboneRouterGetRegistrationJitter, otBackboneRouterSetRegistrationJitter); } else if (aArgs[0] == "register") { @@ -548,7 +536,7 @@ otError Interpreter::ProcessBackboneRouterLocal(uint8_t aArgsLength, Arg aArgs[] { otBackboneRouterGetConfig(mInstance, &config); - if (aArgsLength == 1) + if (aArgs[1].IsEmpty()) { OutputLine("seqno: %d", config.mSequenceNumber); OutputLine("delay: %d secs", config.mReregistrationDelay); @@ -557,21 +545,22 @@ otError Interpreter::ProcessBackboneRouterLocal(uint8_t aArgsLength, Arg aArgs[] else { // Set local Backbone Router configuration. - for (int argCur = 1; argCur < aArgsLength; argCur++) + for (Arg *arg = &aArgs[1]; !arg->IsEmpty(); arg++) { - VerifyOrExit(argCur + 1 < aArgsLength, error = OT_ERROR_INVALID_ARGS); - - if (aArgs[argCur] == "seqno") + if (*arg == "seqno") { - SuccessOrExit(error = aArgs[++argCur].ParseAsUint8(config.mSequenceNumber)); + arg++; + SuccessOrExit(error = arg->ParseAsUint8(config.mSequenceNumber)); } - else if (aArgs[argCur] == "delay") + else if (*arg == "delay") { - SuccessOrExit(error = aArgs[++argCur].ParseAsUint16(config.mReregistrationDelay)); + arg++; + SuccessOrExit(error = arg->ParseAsUint16(config.mReregistrationDelay)); } - else if (aArgs[argCur] == "timeout") + else if (*arg == "timeout") { - SuccessOrExit(error = aArgs[++argCur].ParseAsUint32(config.mMlrTimeout)); + arg++; + SuccessOrExit(error = arg->ParseAsUint32(config.mMlrTimeout)); } else { @@ -592,11 +581,11 @@ exit: } #endif // OPENTHREAD_FTD && OPENTHREAD_CONFIG_BACKBONE_ROUTER_ENABLE -otError Interpreter::ProcessDomainName(uint8_t aArgsLength, Arg aArgs[]) +otError Interpreter::ProcessDomainName(Arg aArgs[]) { otError error = OT_ERROR_NONE; - if (aArgsLength == 0) + if (aArgs[0].IsEmpty()) { OutputLine("%s", otThreadGetDomainName(mInstance)); } @@ -610,41 +599,37 @@ exit: } #if OPENTHREAD_CONFIG_DUA_ENABLE -otError Interpreter::ProcessDua(uint8_t aArgsLength, Arg aArgs[]) +otError Interpreter::ProcessDua(Arg aArgs[]) { otError error = OT_ERROR_NONE; - VerifyOrExit(aArgsLength >= 1 && (aArgs[0] == "iid"), error = OT_ERROR_INVALID_COMMAND); - - switch (aArgsLength) + if (aArgs[0] == "iid") { - case 1: - { - const otIp6InterfaceIdentifier *iid = otThreadGetFixedDuaInterfaceIdentifier(mInstance); - - if (iid != nullptr) + if (aArgs[1].IsEmpty()) { - OutputBytes(iid->mFields.m8, sizeof(otIp6InterfaceIdentifier)); - OutputLine(""); + const otIp6InterfaceIdentifier *iid = otThreadGetFixedDuaInterfaceIdentifier(mInstance); + + if (iid != nullptr) + { + OutputBytes(iid->mFields.m8, sizeof(otIp6InterfaceIdentifier)); + OutputLine(""); + } } - break; - } - case 2: - if (aArgs[1] == "clear") + else if (aArgs[1] == "clear") { - SuccessOrExit(error = otThreadSetFixedDuaInterfaceIdentifier(mInstance, nullptr)); + error = otThreadSetFixedDuaInterfaceIdentifier(mInstance, nullptr); } else { otIp6InterfaceIdentifier iid; SuccessOrExit(error = aArgs[1].ParseAsHexString(iid.mFields.m8)); - SuccessOrExit(error = otThreadSetFixedDuaInterfaceIdentifier(mInstance, &iid)); + error = otThreadSetFixedDuaInterfaceIdentifier(mInstance, &iid); } - break; - default: - error = OT_ERROR_INVALID_ARGS; - break; + } + else + { + error = OT_ERROR_INVALID_COMMAND; } exit: @@ -654,9 +639,8 @@ exit: #endif // (OPENTHREAD_CONFIG_THREAD_VERSION >= OT_THREAD_VERSION_1_2) -otError Interpreter::ProcessBufferInfo(uint8_t aArgsLength, Arg aArgs[]) +otError Interpreter::ProcessBufferInfo(Arg aArgs[]) { - OT_UNUSED_VARIABLE(aArgsLength); OT_UNUSED_VARIABLE(aArgs); struct BufferInfoName @@ -693,12 +677,12 @@ otError Interpreter::ProcessBufferInfo(uint8_t aArgsLength, Arg aArgs[]) return OT_ERROR_NONE; } -otError Interpreter::ProcessCcaThreshold(uint8_t aArgsLength, Arg aArgs[]) +otError Interpreter::ProcessCcaThreshold(Arg aArgs[]) { otError error = OT_ERROR_NONE; int8_t cca; - if (aArgsLength == 0) + if (aArgs[0].IsEmpty()) { SuccessOrExit(error = otPlatRadioGetCcaEnergyDetectThreshold(mInstance, &cca)); OutputLine("%d dBm", cca); @@ -706,18 +690,18 @@ otError Interpreter::ProcessCcaThreshold(uint8_t aArgsLength, Arg aArgs[]) else { SuccessOrExit(error = aArgs[0].ParseAsInt8(cca)); - SuccessOrExit(error = otPlatRadioSetCcaEnergyDetectThreshold(mInstance, cca)); + error = otPlatRadioSetCcaEnergyDetectThreshold(mInstance, cca); } exit: return error; } -otError Interpreter::ProcessChannel(uint8_t aArgsLength, Arg aArgs[]) +otError Interpreter::ProcessChannel(Arg aArgs[]) { otError error; - error = ProcessGetSet(aArgsLength, aArgs, otLinkGetChannel, otLinkSetChannel); + error = ProcessGetSet(aArgs, otLinkGetChannel, otLinkSetChannel); VerifyOrExit(error != OT_ERROR_NONE); @@ -732,7 +716,7 @@ otError Interpreter::ProcessChannel(uint8_t aArgsLength, Arg aArgs[]) #if OPENTHREAD_CONFIG_CHANNEL_MONITOR_ENABLE else if (aArgs[0] == "monitor") { - if (aArgsLength == 1) + if (aArgs[1].IsEmpty()) { OutputLine("enabled: %d", otChannelMonitorIsEnabled(mInstance)); if (otChannelMonitorIsEnabled(mInstance)) @@ -781,7 +765,7 @@ otError Interpreter::ProcessChannel(uint8_t aArgsLength, Arg aArgs[]) #if OPENTHREAD_CONFIG_CHANNEL_MANAGER_ENABLE && OPENTHREAD_FTD else if (aArgs[0] == "manager") { - if (aArgsLength == 1) + if (aArgs[1].IsEmpty()) { OutputLine("channel: %d", otChannelManagerGetRequestedChannel(mInstance)); OutputLine("auto: %d", otChannelManagerGetAutoChannelSelectionEnabled(mInstance)); @@ -800,14 +784,13 @@ otError Interpreter::ProcessChannel(uint8_t aArgsLength, Arg aArgs[]) } else if (aArgs[1] == "change") { - error = ProcessSet(aArgsLength - 2, &aArgs[2], otChannelManagerRequestChannelChange); + error = ProcessSet(aArgs + 2, otChannelManagerRequestChannelChange); } #if OPENTHREAD_CONFIG_CHANNEL_MONITOR_ENABLE else if (aArgs[1] == "select") { bool enable; - VerifyOrExit(aArgsLength > 2, error = OT_ERROR_INVALID_ARGS); SuccessOrExit(error = aArgs[2].ParseAsBool(enable)); error = otChannelManagerRequestChannelSelect(mInstance, enable); } @@ -816,29 +799,28 @@ otError Interpreter::ProcessChannel(uint8_t aArgsLength, Arg aArgs[]) { bool enable; - VerifyOrExit(aArgsLength > 2, error = OT_ERROR_INVALID_ARGS); SuccessOrExit(error = aArgs[2].ParseAsBool(enable)); otChannelManagerSetAutoChannelSelectionEnabled(mInstance, enable); } else if (aArgs[1] == "delay") { - error = ProcessSet(aArgsLength - 2, aArgs + 2, otChannelManagerSetDelay); + error = ProcessSet(aArgs + 2, otChannelManagerSetDelay); } else if (aArgs[1] == "interval") { - error = ProcessSet(aArgsLength - 2, aArgs + 2, otChannelManagerSetAutoChannelSelectionInterval); + error = ProcessSet(aArgs + 2, otChannelManagerSetAutoChannelSelectionInterval); } else if (aArgs[1] == "supported") { - error = ProcessSet(aArgsLength - 2, aArgs + 2, otChannelManagerSetSupportedChannels); + error = ProcessSet(aArgs + 2, otChannelManagerSetSupportedChannels); } else if (aArgs[1] == "favored") { - error = ProcessSet(aArgsLength - 2, aArgs + 2, otChannelManagerSetFavoredChannels); + error = ProcessSet(aArgs + 2, otChannelManagerSetFavoredChannels); } else if (aArgs[1] == "threshold") { - error = ProcessSet(aArgsLength - 2, aArgs + 2, otChannelManagerSetCcaFailureRateThreshold); + error = ProcessSet(aArgs + 2, otChannelManagerSetCcaFailureRateThreshold); } } #endif // OPENTHREAD_CONFIG_CHANNEL_MANAGER_ENABLE && OPENTHREAD_FTD @@ -848,15 +830,13 @@ exit: } #if OPENTHREAD_FTD -otError Interpreter::ProcessChild(uint8_t aArgsLength, Arg aArgs[]) +otError Interpreter::ProcessChild(Arg aArgs[]) { otError error = OT_ERROR_NONE; otChildInfo childInfo; uint16_t childId; bool isTable; - VerifyOrExit(aArgsLength > 0, error = OT_ERROR_INVALID_ARGS); - isTable = (aArgs[0] == "table"); if (isTable || (aArgs[0] == "list")) @@ -958,11 +938,11 @@ exit: return error; } -otError Interpreter::ProcessChildIp(uint8_t aArgsLength, Arg aArgs[]) +otError Interpreter::ProcessChildIp(Arg aArgs[]) { otError error = OT_ERROR_NONE; - if (aArgsLength == 0) + if (aArgs[0].IsEmpty()) { uint16_t maxChildren = otThreadGetMaxAllowedChildren(mInstance); @@ -991,10 +971,9 @@ otError Interpreter::ProcessChildIp(uint8_t aArgsLength, Arg aArgs[]) else if (aArgs[0] == "max") { #if !OPENTHREAD_CONFIG_REFERENCE_DEVICE_ENABLE - error = ProcessGet(aArgsLength - 1, otThreadGetMaxChildIpAddresses); + error = ProcessGet(aArgs + 1, otThreadGetMaxChildIpAddresses); #else - error = - ProcessGetSet(aArgsLength - 1, aArgs + 1, otThreadGetMaxChildIpAddresses, otThreadSetMaxChildIpAddresses); + error = ProcessGetSet(aArgs + 1, otThreadGetMaxChildIpAddresses, otThreadSetMaxChildIpAddresses); #endif } else @@ -1005,62 +984,58 @@ otError Interpreter::ProcessChildIp(uint8_t aArgsLength, Arg aArgs[]) return error; } -otError Interpreter::ProcessChildMax(uint8_t aArgsLength, Arg aArgs[]) +otError Interpreter::ProcessChildMax(Arg aArgs[]) { - return ProcessGetSet(aArgsLength, aArgs, otThreadGetMaxAllowedChildren, otThreadSetMaxAllowedChildren); + return ProcessGetSet(aArgs, otThreadGetMaxAllowedChildren, otThreadSetMaxAllowedChildren); } #endif // OPENTHREAD_FTD #if OPENTHREAD_CONFIG_CHILD_SUPERVISION_ENABLE -otError Interpreter::ProcessChildSupervision(uint8_t aArgsLength, Arg aArgs[]) +otError Interpreter::ProcessChildSupervision(Arg aArgs[]) { otError error = OT_ERROR_INVALID_ARGS; - VerifyOrExit(aArgsLength > 0); - if (aArgs[0] == "checktimeout") { - error = ProcessGetSet(aArgsLength - 1, aArgs + 1, otChildSupervisionGetCheckTimeout, - otChildSupervisionSetCheckTimeout); + error = ProcessGetSet(aArgs + 1, otChildSupervisionGetCheckTimeout, otChildSupervisionSetCheckTimeout); } #if OPENTHREAD_FTD else if (aArgs[0] == "interval") { - error = ProcessGetSet(aArgsLength - 1, aArgs + 1, otChildSupervisionGetInterval, otChildSupervisionSetInterval); + error = ProcessGetSet(aArgs + 1, otChildSupervisionGetInterval, otChildSupervisionSetInterval); } #endif -exit: return error; } #endif // OPENTHREAD_CONFIG_CHILD_SUPERVISION_ENABLE -otError Interpreter::ProcessChildTimeout(uint8_t aArgsLength, Arg aArgs[]) +otError Interpreter::ProcessChildTimeout(Arg aArgs[]) { - return ProcessGetSet(aArgsLength, aArgs, otThreadGetChildTimeout, otThreadSetChildTimeout); + return ProcessGetSet(aArgs, otThreadGetChildTimeout, otThreadSetChildTimeout); } #if OPENTHREAD_CONFIG_COAP_API_ENABLE -otError Interpreter::ProcessCoap(uint8_t aArgsLength, Arg aArgs[]) +otError Interpreter::ProcessCoap(Arg aArgs[]) { - return mCoap.Process(aArgsLength, aArgs); + return mCoap.Process(aArgs); } #endif #if OPENTHREAD_CONFIG_COAP_SECURE_API_ENABLE -otError Interpreter::ProcessCoapSecure(uint8_t aArgsLength, Arg aArgs[]) +otError Interpreter::ProcessCoapSecure(Arg aArgs[]) { - return mCoapSecure.Process(aArgsLength, aArgs); + return mCoapSecure.Process(aArgs); } #endif #if OPENTHREAD_CONFIG_PLATFORM_RADIO_COEX_ENABLE -otError Interpreter::ProcessCoexMetrics(uint8_t aArgsLength, Arg aArgs[]) +otError Interpreter::ProcessCoexMetrics(Arg aArgs[]) { otError error = OT_ERROR_NONE; bool enable; - if (aArgsLength == 0) + if (aArgs[0].IsEmpty()) { OutputEnabledDisabledStatus(otPlatRadioIsCoexEnabled(mInstance)); } @@ -1130,24 +1105,24 @@ exit: #endif // OPENTHREAD_CONFIG_PLATFORM_RADIO_COEX_ENABLE #if OPENTHREAD_FTD -otError Interpreter::ProcessContextIdReuseDelay(uint8_t aArgsLength, Arg aArgs[]) +otError Interpreter::ProcessContextIdReuseDelay(Arg aArgs[]) { - return ProcessGetSet(aArgsLength, aArgs, otThreadGetContextIdReuseDelay, otThreadSetContextIdReuseDelay); + return ProcessGetSet(aArgs, otThreadGetContextIdReuseDelay, otThreadSetContextIdReuseDelay); } #endif -otError Interpreter::ProcessCounters(uint8_t aArgsLength, Arg aArgs[]) +otError Interpreter::ProcessCounters(Arg aArgs[]) { otError error = OT_ERROR_NONE; - if (aArgsLength == 0) + if (aArgs[0].IsEmpty()) { OutputLine("mac"); OutputLine("mle"); } else if (aArgs[0] == "mac") { - if (aArgsLength == 1) + if (aArgs[1].IsEmpty()) { struct MacCounterName { @@ -1206,7 +1181,7 @@ otError Interpreter::ProcessCounters(uint8_t aArgsLength, Arg aArgs[]) OutputLine(kIndentSize, "%s: %u", counter.mName, macCounters->*counter.mValuePtr); } } - else if ((aArgsLength == 2) && (aArgs[1] == "reset")) + else if ((aArgs[1] == "reset") && aArgs[2].IsEmpty()) { otLinkResetCounters(mInstance); } @@ -1217,7 +1192,7 @@ otError Interpreter::ProcessCounters(uint8_t aArgsLength, Arg aArgs[]) } else if (aArgs[0] == "mle") { - if (aArgsLength == 1) + if (aArgs[1].IsEmpty()) { struct MleCounterName { @@ -1244,7 +1219,7 @@ otError Interpreter::ProcessCounters(uint8_t aArgsLength, Arg aArgs[]) OutputLine("%s: %d", counter.mName, mleCounters->*counter.mValuePtr); } } - else if ((aArgsLength == 2) && (aArgs[1] == "reset")) + else if ((aArgs[1] == "reset") && aArgs[2].IsEmpty()) { otThreadResetMleCounters(mInstance); } @@ -1263,32 +1238,33 @@ exit: } #if OPENTHREAD_CONFIG_MAC_CSL_RECEIVER_ENABLE -otError Interpreter::ProcessCsl(uint8_t aArgsLength, Arg aArgs[]) +otError Interpreter::ProcessCsl(Arg aArgs[]) { - otError error = OT_ERROR_INVALID_ARGS; + otError error = OT_ERROR_NONE; + ; - if (aArgsLength == 0) + if (aArgs[0].IsEmpty()) { OutputLine("Channel: %u", otLinkCslGetChannel(mInstance)); OutputLine("Period: %u(in units of 10 symbols), %ums", otLinkCslGetPeriod(mInstance), otLinkCslGetPeriod(mInstance) * kUsPerTenSymbols / 1000); OutputLine("Timeout: %us", otLinkCslGetTimeout(mInstance)); - error = OT_ERROR_NONE; } - else if (aArgsLength == 2) + else if (aArgs[0] == "channel") { - if (aArgs[0] == "channel") - { - error = ProcessSet(aArgsLength - 1, aArgs + 1, otLinkCslSetChannel); - } - else if (aArgs[0] == "period") - { - error = ProcessSet(aArgsLength - 1, aArgs + 1, otLinkCslSetPeriod); - } - else if (aArgs[0] == "timeout") - { - error = ProcessSet(aArgsLength - 1, aArgs + 1, otLinkCslSetTimeout); - } + error = ProcessSet(aArgs + 1, otLinkCslSetChannel); + } + else if (aArgs[0] == "period") + { + error = ProcessSet(aArgs + 1, otLinkCslSetPeriod); + } + else if (aArgs[0] == "timeout") + { + error = ProcessSet(aArgs + 1, otLinkCslSetTimeout); + } + else + { + error = OT_ERROR_INVALID_ARGS; } return error; @@ -1296,15 +1272,15 @@ otError Interpreter::ProcessCsl(uint8_t aArgsLength, Arg aArgs[]) #endif // OPENTHREAD_CONFIG_MAC_CSL_RECEIVER_ENABLE #if OPENTHREAD_FTD -otError Interpreter::ProcessDelayTimerMin(uint8_t aArgsLength, Arg aArgs[]) +otError Interpreter::ProcessDelayTimerMin(Arg aArgs[]) { otError error = OT_ERROR_NONE; - if (aArgsLength == 0) + if (aArgs[0].IsEmpty()) { OutputLine("%d", (otDatasetGetDelayTimerMinimal(mInstance) / 1000)); } - else if (aArgsLength == 1) + else if (aArgs[1].IsEmpty()) { uint32_t delay; SuccessOrExit(error = aArgs[0].ParseAsUint32(delay)); @@ -1320,12 +1296,12 @@ exit: } #endif -otError Interpreter::ProcessDiscover(uint8_t aArgsLength, Arg aArgs[]) +otError Interpreter::ProcessDiscover(Arg aArgs[]) { otError error = OT_ERROR_NONE; uint32_t scanChannels = 0; - if (aArgsLength > 0) + if (!aArgs[0].IsEmpty()) { uint8_t channel; @@ -1390,7 +1366,7 @@ void Interpreter::OutputDnsTxtData(const uint8_t *aTxtData, uint16_t aTxtDataLen OutputFormat("]"); } -otError Interpreter::ProcessDns(uint8_t aArgsLength, Arg aArgs[]) +otError Interpreter::ProcessDns(Arg aArgs[]) { OT_UNUSED_VARIABLE(aArgs); @@ -1400,14 +1376,14 @@ otError Interpreter::ProcessDns(uint8_t aArgsLength, Arg aArgs[]) otDnsQueryConfig *config = &queryConfig; #endif - if (aArgsLength == 0) + if (aArgs[0].IsEmpty()) { error = OT_ERROR_INVALID_ARGS; } #if OPENTHREAD_CONFIG_REFERENCE_DEVICE_ENABLE else if (aArgs[0] == "compression") { - if (aArgsLength == 1) + if (aArgs[1].IsEmpty()) { OutputEnabledDisabledStatus(otDnsIsNameCompressionEnabled()); } @@ -1415,7 +1391,6 @@ otError Interpreter::ProcessDns(uint8_t aArgsLength, Arg aArgs[]) { bool enable; - VerifyOrExit(aArgsLength == 2, error = OT_ERROR_INVALID_ARGS); SuccessOrExit(error = ParseEnableOrDisable(aArgs[1], enable)); otDnsSetNameCompressionEnabled(enable); } @@ -1424,7 +1399,7 @@ otError Interpreter::ProcessDns(uint8_t aArgsLength, Arg aArgs[]) #if OPENTHREAD_CONFIG_DNS_CLIENT_ENABLE else if (aArgs[0] == "config") { - if (aArgsLength == 1) + if (aArgs[1].IsEmpty()) { const otDnsQueryConfig *defaultConfig = otDnsClientGetDefaultConfig(mInstance); @@ -1438,14 +1413,14 @@ otError Interpreter::ProcessDns(uint8_t aArgsLength, Arg aArgs[]) } else { - SuccessOrExit(error = GetDnsConfig(aArgsLength, aArgs, config, 1)); + SuccessOrExit(error = GetDnsConfig(aArgs + 1, config)); otDnsClientSetDefaultConfig(mInstance, config); } } else if (aArgs[0] == "resolve") { - VerifyOrExit(aArgsLength >= 2, error = OT_ERROR_INVALID_ARGS); - SuccessOrExit(error = GetDnsConfig(aArgsLength, aArgs, config, 2)); + VerifyOrExit(!aArgs[1].IsEmpty(), error = OT_ERROR_INVALID_ARGS); + SuccessOrExit(error = GetDnsConfig(aArgs + 2, config)); SuccessOrExit(error = otDnsClientResolveAddress(mInstance, aArgs[1].GetCString(), &Interpreter::HandleDnsAddressResponse, this, config)); error = OT_ERROR_PENDING; @@ -1453,16 +1428,16 @@ otError Interpreter::ProcessDns(uint8_t aArgsLength, Arg aArgs[]) #if OPENTHREAD_CONFIG_DNS_CLIENT_SERVICE_DISCOVERY_ENABLE else if (aArgs[0] == "browse") { - VerifyOrExit(aArgsLength >= 2, error = OT_ERROR_INVALID_ARGS); - SuccessOrExit(error = GetDnsConfig(aArgsLength, aArgs, config, 2)); + VerifyOrExit(!aArgs[1].IsEmpty(), error = OT_ERROR_INVALID_ARGS); + SuccessOrExit(error = GetDnsConfig(aArgs + 2, config)); SuccessOrExit(error = otDnsClientBrowse(mInstance, aArgs[1].GetCString(), &Interpreter::HandleDnsBrowseResponse, this, config)); error = OT_ERROR_PENDING; } else if (aArgs[0] == "service") { - VerifyOrExit(aArgsLength >= 3, error = OT_ERROR_INVALID_ARGS); - SuccessOrExit(error = GetDnsConfig(aArgsLength, aArgs, config, 3)); + VerifyOrExit(!aArgs[2].IsEmpty(), error = OT_ERROR_INVALID_ARGS); + SuccessOrExit(error = GetDnsConfig(aArgs + 3, config)); SuccessOrExit(error = otDnsClientResolveService(mInstance, aArgs[1].GetCString(), aArgs[2].GetCString(), &Interpreter::HandleDnsServiceResponse, this, config)); error = OT_ERROR_PENDING; @@ -1480,32 +1455,32 @@ exit: #if OPENTHREAD_CONFIG_DNS_CLIENT_ENABLE -otError Interpreter::GetDnsConfig(uint8_t aArgsLength, Arg aArgs[], otDnsQueryConfig *&aConfig, uint8_t aStartArgsIndex) +otError Interpreter::GetDnsConfig(Arg aArgs[], otDnsQueryConfig *&aConfig) { - // This method gets the optional config from given `aArgs` after the - // `aStartArgsIndex`. The format: `[server IPv6 address] [server - // port] [timeout] [max tx attempt] [recursion desired]`. + // This method gets the optional DNS config from `aArgs[]`. + // The format: `[server IPv6 address] [server port] [timeout] + // [max tx attempt] [recursion desired]`. otError error = OT_ERROR_NONE; bool recursionDesired; memset(aConfig, 0, sizeof(otDnsQueryConfig)); - VerifyOrExit(aArgsLength > aStartArgsIndex, aConfig = nullptr); + VerifyOrExit(!aArgs[0].IsEmpty(), aConfig = nullptr); - SuccessOrExit(error = aArgs[aStartArgsIndex].ParseAsIp6Address(aConfig->mServerSockAddr.mAddress)); + SuccessOrExit(error = aArgs[0].ParseAsIp6Address(aConfig->mServerSockAddr.mAddress)); - VerifyOrExit(aArgsLength > aStartArgsIndex + 1); - SuccessOrExit(error = aArgs[aStartArgsIndex + 1].ParseAsUint16(aConfig->mServerSockAddr.mPort)); + VerifyOrExit(!aArgs[1].IsEmpty()); + SuccessOrExit(error = aArgs[1].ParseAsUint16(aConfig->mServerSockAddr.mPort)); - VerifyOrExit(aArgsLength > aStartArgsIndex + 2); - SuccessOrExit(error = aArgs[aStartArgsIndex + 2].ParseAsUint32(aConfig->mResponseTimeout)); + VerifyOrExit(!aArgs[2].IsEmpty()); + SuccessOrExit(error = aArgs[2].ParseAsUint32(aConfig->mResponseTimeout)); - VerifyOrExit(aArgsLength > aStartArgsIndex + 3); - SuccessOrExit(error = aArgs[aStartArgsIndex + 3].ParseAsUint8(aConfig->mMaxTxAttempts)); + VerifyOrExit(!aArgs[3].IsEmpty()); + SuccessOrExit(error = aArgs[3].ParseAsUint8(aConfig->mMaxTxAttempts)); - VerifyOrExit(aArgsLength > aStartArgsIndex + 4); - SuccessOrExit(error = aArgs[aStartArgsIndex + 4].ParseAsBool(recursionDesired)); + VerifyOrExit(!aArgs[4].IsEmpty()); + SuccessOrExit(error = aArgs[4].ParseAsBool(recursionDesired)); aConfig->mRecursionFlag = recursionDesired ? OT_DNS_FLAG_RECURSION_DESIRED : OT_DNS_FLAG_NO_RECURSION; exit: @@ -1677,9 +1652,8 @@ void Interpreter::OutputEidCacheEntry(const otCacheEntryInfo &aEntry) OutputLine(""); } -otError Interpreter::ProcessEidCache(uint8_t aArgsLength, Arg aArgs[]) +otError Interpreter::ProcessEidCache(Arg aArgs[]) { - OT_UNUSED_VARIABLE(aArgsLength); OT_UNUSED_VARIABLE(aArgs); otCacheEntryIterator iterator; @@ -1698,14 +1672,14 @@ exit: } #endif -otError Interpreter::ProcessEui64(uint8_t aArgsLength, Arg aArgs[]) +otError Interpreter::ProcessEui64(Arg aArgs[]) { OT_UNUSED_VARIABLE(aArgs); otError error = OT_ERROR_NONE; otExtAddress extAddress; - VerifyOrExit(aArgsLength == 0, error = OT_ERROR_INVALID_ARGS); + VerifyOrExit(aArgs[0].IsEmpty(), error = OT_ERROR_INVALID_ARGS); otLinkGetFactoryAssignedIeeeEui64(mInstance, &extAddress); OutputBytes(extAddress.m8, OT_EXT_ADDRESS_SIZE); @@ -1715,11 +1689,11 @@ exit: return error; } -otError Interpreter::ProcessExtAddress(uint8_t aArgsLength, Arg aArgs[]) +otError Interpreter::ProcessExtAddress(Arg aArgs[]) { otError error = OT_ERROR_NONE; - if (aArgsLength == 0) + if (aArgs[0].IsEmpty()) { const uint8_t *extAddress = reinterpret_cast(otLinkGetExtendedAddress(mInstance)); OutputBytes(extAddress, OT_EXT_ADDRESS_SIZE); @@ -1737,36 +1711,32 @@ exit: return error; } -otError Interpreter::ProcessLog(uint8_t aArgsLength, Arg aArgs[]) +otError Interpreter::ProcessLog(Arg aArgs[]) { otError error = OT_ERROR_NONE; - VerifyOrExit(aArgsLength >= 1, error = OT_ERROR_INVALID_ARGS); - if (aArgs[0] == "level") { - if (aArgsLength == 1) + if (aArgs[1].IsEmpty()) { OutputLine("%d", otLoggingGetLevel()); } -#if OPENTHREAD_CONFIG_LOG_LEVEL_DYNAMIC_ENABLE - else if (aArgsLength == 2) + else { +#if OPENTHREAD_CONFIG_LOG_LEVEL_DYNAMIC_ENABLE uint8_t level; SuccessOrExit(error = aArgs[1].ParseAsUint8(level)); - SuccessOrExit(error = otLoggingSetLevel(static_cast(level))); - } + error = otLoggingSetLevel(static_cast(level)); +#else + error = OT_ERROR_INVALID_ARGS; #endif - else - { - ExitNow(error = OT_ERROR_INVALID_ARGS); } } #if (OPENTHREAD_CONFIG_LOG_OUTPUT == OPENTHREAD_CONFIG_LOG_OUTPUT_DEBUG_UART) && OPENTHREAD_POSIX else if (aArgs[0] == "filename") { - VerifyOrExit(aArgsLength == 1, error = OT_ERROR_INVALID_ARGS); + VerifyOrExit(aArgs[1].IsEmpty(), error = OT_ERROR_INVALID_ARGS); SuccessOrExit(error = otPlatDebugUart_logfile(aArgs[1].GetCString())); } #endif @@ -1779,11 +1749,11 @@ exit: return error; } -otError Interpreter::ProcessExtPanId(uint8_t aArgsLength, Arg aArgs[]) +otError Interpreter::ProcessExtPanId(Arg aArgs[]) { otError error = OT_ERROR_NONE; - if (aArgsLength == 0) + if (aArgs[0].IsEmpty()) { const uint8_t *extPanId = reinterpret_cast(otThreadGetExtendedPanId(mInstance)); OutputBytes(extPanId, OT_EXT_PAN_ID_SIZE); @@ -1801,9 +1771,8 @@ exit: return error; } -otError Interpreter::ProcessFactoryReset(uint8_t aArgsLength, Arg aArgs[]) +otError Interpreter::ProcessFactoryReset(Arg aArgs[]) { - OT_UNUSED_VARIABLE(aArgsLength); OT_UNUSED_VARIABLE(aArgs); otInstanceFactoryReset(mInstance); @@ -1812,19 +1781,15 @@ otError Interpreter::ProcessFactoryReset(uint8_t aArgsLength, Arg aArgs[]) } #if OPENTHREAD_FTD && OPENTHREAD_CONFIG_REFERENCE_DEVICE_ENABLE -otError Interpreter::ProcessFake(uint8_t aArgsLength, Arg aArgs[]) +otError Interpreter::ProcessFake(Arg aArgs[]) { otError error = OT_ERROR_INVALID_COMMAND; - VerifyOrExit(aArgsLength >= 1); - if (aArgs[0] == "/a/an") { otIp6Address destination, target; otIp6InterfaceIdentifier mlIid; - VerifyOrExit(aArgsLength == 4, error = OT_ERROR_INVALID_ARGS); - SuccessOrExit(error = aArgs[1].ParseAsIp6Address(destination)); SuccessOrExit(error = aArgs[2].ParseAsIp6Address(target)); SuccessOrExit(error = aArgs[3].ParseAsHexString(mlIid.mFields.m8)); @@ -1837,8 +1802,6 @@ otError Interpreter::ProcessFake(uint8_t aArgsLength, Arg aArgs[]) otIp6InterfaceIdentifier mlIid; uint32_t timeSinceLastTransaction; - VerifyOrExit(aArgsLength == 4, error = OT_ERROR_INVALID_ARGS); - SuccessOrExit(error = aArgs[1].ParseAsIp6Address(target)); SuccessOrExit(error = aArgs[2].ParseAsHexString(mlIid.mFields.m8)); SuccessOrExit(error = aArgs[3].ParseAsUint32(timeSinceLastTransaction)); @@ -1846,16 +1809,17 @@ otError Interpreter::ProcessFake(uint8_t aArgsLength, Arg aArgs[]) error = otThreadSendProactiveBackboneNotification(mInstance, &target, &mlIid, timeSinceLastTransaction); } #endif + exit: return error; } #endif -otError Interpreter::ProcessFem(uint8_t aArgsLength, Arg aArgs[]) +otError Interpreter::ProcessFem(Arg aArgs[]) { otError error = OT_ERROR_NONE; - if (aArgsLength == 0) + if (aArgs[0].IsEmpty()) { int8_t lnaGain; @@ -1864,7 +1828,7 @@ otError Interpreter::ProcessFem(uint8_t aArgsLength, Arg aArgs[]) } else if (aArgs[0] == "lnagain") { - if (aArgsLength == 1) + if (aArgs[1].IsEmpty()) { int8_t lnaGain; @@ -1888,11 +1852,11 @@ exit: return error; } -otError Interpreter::ProcessIfconfig(uint8_t aArgsLength, Arg aArgs[]) +otError Interpreter::ProcessIfconfig(Arg aArgs[]) { otError error = OT_ERROR_NONE; - if (aArgsLength == 0) + if (aArgs[0].IsEmpty()) { if (otIp6IsEnabled(mInstance)) { @@ -1920,31 +1884,28 @@ exit: return error; } -otError Interpreter::ProcessIpAddrAdd(uint8_t aArgsLength, Arg aArgs[]) +otError Interpreter::ProcessIpAddrAdd(Arg aArgs[]) { otError error; otNetifAddress aAddress; - VerifyOrExit(aArgsLength > 0, error = OT_ERROR_INVALID_ARGS); - SuccessOrExit(error = aArgs[0].ParseAsIp6Address(aAddress.mAddress)); aAddress.mPrefixLength = 64; aAddress.mPreferred = true; aAddress.mValid = true; aAddress.mAddressOrigin = OT_ADDRESS_ORIGIN_MANUAL; - error = otIp6AddUnicastAddress(mInstance, &aAddress); + + error = otIp6AddUnicastAddress(mInstance, &aAddress); exit: return error; } -otError Interpreter::ProcessIpAddrDel(uint8_t aArgsLength, Arg aArgs[]) +otError Interpreter::ProcessIpAddrDel(Arg aArgs[]) { otError error; otIp6Address address; - VerifyOrExit(aArgsLength > 0, error = OT_ERROR_INVALID_ARGS); - SuccessOrExit(error = aArgs[0].ParseAsIp6Address(address)); error = otIp6RemoveUnicastAddress(mInstance, &address); @@ -1952,11 +1913,11 @@ exit: return error; } -otError Interpreter::ProcessIpAddr(uint8_t aArgsLength, Arg aArgs[]) +otError Interpreter::ProcessIpAddr(Arg aArgs[]) { otError error = OT_ERROR_NONE; - if (aArgsLength == 0) + if (aArgs[0].IsEmpty()) { const otNetifAddress *unicastAddrs = otIp6GetUnicastAddresses(mInstance); @@ -1970,11 +1931,11 @@ otError Interpreter::ProcessIpAddr(uint8_t aArgsLength, Arg aArgs[]) { if (aArgs[0] == "add") { - SuccessOrExit(error = ProcessIpAddrAdd(aArgsLength - 1, aArgs + 1)); + SuccessOrExit(error = ProcessIpAddrAdd(aArgs + 1)); } else if (aArgs[0] == "del") { - SuccessOrExit(error = ProcessIpAddrDel(aArgsLength - 1, aArgs + 1)); + SuccessOrExit(error = ProcessIpAddrDel(aArgs + 1)); } else if (aArgs[0] == "linklocal") { @@ -2001,13 +1962,11 @@ exit: return error; } -otError Interpreter::ProcessIpMulticastAddrAdd(uint8_t aArgsLength, Arg aArgs[]) +otError Interpreter::ProcessIpMulticastAddrAdd(Arg aArgs[]) { otError error; otIp6Address address; - VerifyOrExit(aArgsLength > 0, error = OT_ERROR_INVALID_ARGS); - SuccessOrExit(error = aArgs[0].ParseAsIp6Address(address)); error = otIp6SubscribeMulticastAddress(mInstance, &address); @@ -2015,13 +1974,11 @@ exit: return error; } -otError Interpreter::ProcessIpMulticastAddrDel(uint8_t aArgsLength, Arg aArgs[]) +otError Interpreter::ProcessIpMulticastAddrDel(Arg aArgs[]) { otError error; otIp6Address address; - VerifyOrExit(aArgsLength > 0, error = OT_ERROR_INVALID_ARGS); - SuccessOrExit(error = aArgs[0].ParseAsIp6Address(address)); error = otIp6UnsubscribeMulticastAddress(mInstance, &address); @@ -2029,11 +1986,11 @@ exit: return error; } -otError Interpreter::ProcessMulticastPromiscuous(uint8_t aArgsLength, Arg aArgs[]) +otError Interpreter::ProcessMulticastPromiscuous(Arg aArgs[]) { otError error = OT_ERROR_NONE; - if (aArgsLength == 0) + if (aArgs[0].IsEmpty()) { OutputEnabledDisabledStatus(otIp6IsMulticastPromiscuousEnabled(mInstance)); } @@ -2049,11 +2006,11 @@ exit: return error; } -otError Interpreter::ProcessIpMulticastAddr(uint8_t aArgsLength, Arg aArgs[]) +otError Interpreter::ProcessIpMulticastAddr(Arg aArgs[]) { otError error = OT_ERROR_NONE; - if (aArgsLength == 0) + if (aArgs[0].IsEmpty()) { for (const otNetifMulticastAddress *addr = otIp6GetMulticastAddresses(mInstance); addr; addr = addr->mNext) { @@ -2065,15 +2022,15 @@ otError Interpreter::ProcessIpMulticastAddr(uint8_t aArgsLength, Arg aArgs[]) { if (aArgs[0] == "add") { - SuccessOrExit(error = ProcessIpMulticastAddrAdd(aArgsLength - 1, aArgs + 1)); + SuccessOrExit(error = ProcessIpMulticastAddrAdd(aArgs + 1)); } else if (aArgs[0] == "del") { - SuccessOrExit(error = ProcessIpMulticastAddrDel(aArgsLength - 1, aArgs + 1)); + SuccessOrExit(error = ProcessIpMulticastAddrDel(aArgs + 1)); } else if (aArgs[0] == "promiscuous") { - SuccessOrExit(error = ProcessMulticastPromiscuous(aArgsLength - 1, aArgs + 1)); + SuccessOrExit(error = ProcessMulticastPromiscuous(aArgs + 1)); } else if (aArgs[0] == "llatn") { @@ -2095,32 +2052,24 @@ exit: return error; } -otError Interpreter::ProcessKeySequence(uint8_t aArgsLength, Arg aArgs[]) +otError Interpreter::ProcessKeySequence(Arg aArgs[]) { - otError error = OT_ERROR_NONE; - - VerifyOrExit(aArgsLength == 1 || aArgsLength == 2, error = OT_ERROR_INVALID_ARGS); + otError error = OT_ERROR_INVALID_ARGS; if (aArgs[0] == "counter") { - error = ProcessGetSet(aArgsLength - 1, aArgs + 1, otThreadGetKeySequenceCounter, otThreadSetKeySequenceCounter); + error = ProcessGetSet(aArgs + 1, otThreadGetKeySequenceCounter, otThreadSetKeySequenceCounter); } else if (aArgs[0] == "guardtime") { - error = ProcessGetSet(aArgsLength - 1, aArgs + 1, otThreadGetKeySwitchGuardTime, otThreadSetKeySwitchGuardTime); - } - else - { - ExitNow(error = OT_ERROR_INVALID_ARGS); + error = ProcessGetSet(aArgs + 1, otThreadGetKeySwitchGuardTime, otThreadSetKeySwitchGuardTime); } -exit: return error; } -otError Interpreter::ProcessLeaderData(uint8_t aArgsLength, Arg aArgs[]) +otError Interpreter::ProcessLeaderData(Arg aArgs[]) { - OT_UNUSED_VARIABLE(aArgsLength); OT_UNUSED_VARIABLE(aArgs); otError error; @@ -2139,13 +2088,11 @@ exit: } #if OPENTHREAD_FTD -otError Interpreter::ProcessPartitionId(uint8_t aArgsLength, Arg aArgs[]) +otError Interpreter::ProcessPartitionId(Arg aArgs[]) { - OT_UNUSED_VARIABLE(aArgs); - otError error = OT_ERROR_INVALID_COMMAND; - if (aArgsLength == 0) + if (aArgs[0].IsEmpty()) { OutputLine("%u", otThreadGetPartitionId(mInstance)); error = OT_ERROR_NONE; @@ -2153,17 +2100,16 @@ otError Interpreter::ProcessPartitionId(uint8_t aArgsLength, Arg aArgs[]) #if OPENTHREAD_CONFIG_REFERENCE_DEVICE_ENABLE else if (aArgs[0] == "preferred") { - error = ProcessGetSet(aArgsLength - 1, aArgs + 1, otThreadGetPreferredLeaderPartitionId, - otThreadSetPreferredLeaderPartitionId); + error = ProcessGetSet(aArgs + 1, otThreadGetPreferredLeaderPartitionId, otThreadSetPreferredLeaderPartitionId); } #endif return error; } -otError Interpreter::ProcessLeaderWeight(uint8_t aArgsLength, Arg aArgs[]) +otError Interpreter::ProcessLeaderWeight(Arg aArgs[]) { - return ProcessGetSet(aArgsLength, aArgs, otThreadGetLocalLeaderWeight, otThreadSetLocalLeaderWeight); + return ProcessGetSet(aArgs, otThreadGetLocalLeaderWeight, otThreadSetLocalLeaderWeight); } #endif // OPENTHREAD_FTD @@ -2298,41 +2244,37 @@ const char *Interpreter::LinkMetricsStatusToStr(uint8_t aStatus) return linkMetricsStatusText[strIndex]; } -otError Interpreter::ProcessLinkMetrics(uint8_t aArgsLength, Arg aArgs[]) +otError Interpreter::ProcessLinkMetrics(Arg aArgs[]) { otError error = OT_ERROR_INVALID_COMMAND; - VerifyOrExit(aArgsLength >= 1); - if (aArgs[0] == "query") { - error = ProcessLinkMetricsQuery(aArgsLength - 1, aArgs + 1); + error = ProcessLinkMetricsQuery(aArgs + 1); } else if (aArgs[0] == "mgmt") { - error = ProcessLinkMetricsMgmt(aArgsLength - 1, aArgs + 1); + error = ProcessLinkMetricsMgmt(aArgs + 1); } else if (aArgs[0] == "probe") { - error = ProcessLinkMetricsProbe(aArgsLength - 1, aArgs + 1); + error = ProcessLinkMetricsProbe(aArgs + 1); } -exit: return error; } -otError Interpreter::ProcessLinkMetricsQuery(uint8_t aArgsLength, Arg aArgs[]) +otError Interpreter::ProcessLinkMetricsQuery(Arg aArgs[]) { - otError error = OT_ERROR_INVALID_ARGS; + otError error; otIp6Address address; otLinkMetrics linkMetrics; - VerifyOrExit(aArgsLength == 3); - SuccessOrExit(error = aArgs[0].ParseAsIp6Address(address)); if (aArgs[1] == "single") { + VerifyOrExit(!aArgs[2].IsEmpty(), error = OT_ERROR_INVALID_ARGS); SuccessOrExit(error = ParseLinkMetricsFlags(linkMetrics, aArgs[2])); error = otLinkMetricsQuery(mInstance, &address, /* aSeriesId */ 0, &linkMetrics, &Interpreter::HandleLinkMetricsReport, this); @@ -2340,9 +2282,14 @@ otError Interpreter::ProcessLinkMetricsQuery(uint8_t aArgsLength, Arg aArgs[]) else if (aArgs[1] == "forward") { uint8_t seriesId; + SuccessOrExit(error = aArgs[2].ParseAsUint8(seriesId)); error = otLinkMetricsQuery(mInstance, &address, seriesId, nullptr, &Interpreter::HandleLinkMetricsReport, this); } + else + { + error = OT_ERROR_INVALID_ARGS; + } exit: return error; @@ -2383,15 +2330,13 @@ exit: return error; } -otError Interpreter::ProcessLinkMetricsMgmt(uint8_t aArgsLength, Arg aArgs[]) +otError Interpreter::ProcessLinkMetricsMgmt(Arg aArgs[]) { - otError error = OT_ERROR_INVALID_ARGS; + otError error; otIp6Address address; otLinkMetricsSeriesFlags seriesFlags; bool clear = false; - VerifyOrExit(aArgsLength >= 2); - SuccessOrExit(error = aArgs[0].ParseAsIp6Address(address)); memset(&seriesFlags, 0, sizeof(otLinkMetricsSeriesFlags)); @@ -2401,10 +2346,9 @@ otError Interpreter::ProcessLinkMetricsMgmt(uint8_t aArgsLength, Arg aArgs[]) uint8_t seriesId; otLinkMetrics linkMetrics; - VerifyOrExit(aArgsLength >= 4); - memset(&linkMetrics, 0, sizeof(otLinkMetrics)); SuccessOrExit(error = aArgs[2].ParseAsUint8(seriesId)); + VerifyOrExit(!aArgs[3].IsEmpty(), error = OT_ERROR_INVALID_ARGS); for (const char *arg = aArgs[3].GetCString(); *arg != '\0'; arg++) { @@ -2427,7 +2371,7 @@ otError Interpreter::ProcessLinkMetricsMgmt(uint8_t aArgsLength, Arg aArgs[]) break; case 'X': - VerifyOrExit(arg == aArgs[3].GetCString() && *(arg + 1) == '\0' && aArgsLength == 4, + VerifyOrExit(arg == aArgs[3].GetCString() && *(arg + 1) == '\0' && aArgs[4].IsEmpty(), error = OT_ERROR_INVALID_ARGS); // Ensure the flags only contain 'X' clear = true; break; @@ -2439,7 +2383,7 @@ otError Interpreter::ProcessLinkMetricsMgmt(uint8_t aArgsLength, Arg aArgs[]) if (!clear) { - VerifyOrExit(aArgsLength == 5); + VerifyOrExit(!aArgs[4].IsEmpty() && aArgs[5].IsEmpty(), error = OT_ERROR_INVALID_ARGS); SuccessOrExit(error = ParseLinkMetricsFlags(linkMetrics, aArgs[4])); } @@ -2453,8 +2397,6 @@ otError Interpreter::ProcessLinkMetricsMgmt(uint8_t aArgsLength, Arg aArgs[]) otLinkMetrics linkMetrics; otLinkMetrics * pLinkMetrics = &linkMetrics; - VerifyOrExit(aArgsLength >= 3); - if (aArgs[2] == "clear") { enhAckFlags = OT_LINK_METRICS_ENH_ACK_CLEAR; @@ -2463,10 +2405,10 @@ otError Interpreter::ProcessLinkMetricsMgmt(uint8_t aArgsLength, Arg aArgs[]) else if (aArgs[2] == "register") { enhAckFlags = OT_LINK_METRICS_ENH_ACK_REGISTER; - VerifyOrExit(aArgsLength >= 4); + VerifyOrExit(!aArgs[3].IsEmpty(), error = OT_ERROR_INVALID_ARGS); SuccessOrExit(error = ParseLinkMetricsFlags(linkMetrics, aArgs[3])); #if OPENTHREAD_CONFIG_REFERENCE_DEVICE_ENABLE - if (aArgsLength > 4 && (aArgs[4] == "r")) + if (aArgs[4] == "r") { linkMetrics.mReserved = true; } @@ -2474,27 +2416,29 @@ otError Interpreter::ProcessLinkMetricsMgmt(uint8_t aArgsLength, Arg aArgs[]) } else { - ExitNow(); + ExitNow(error = OT_ERROR_INVALID_ARGS); } error = otLinkMetricsConfigEnhAckProbing(mInstance, &address, enhAckFlags, pLinkMetrics, &Interpreter::HandleLinkMetricsMgmtResponse, this, &Interpreter::HandleLinkMetricsEnhAckProbingIe, this); } + else + { + error = OT_ERROR_INVALID_ARGS; + } exit: return error; } -otError Interpreter::ProcessLinkMetricsProbe(uint8_t aArgsLength, Arg aArgs[]) +otError Interpreter::ProcessLinkMetricsProbe(Arg aArgs[]) { otError error = OT_ERROR_INVALID_ARGS; otIp6Address address; uint8_t seriesId = 0; uint8_t length = 0; - VerifyOrExit(aArgsLength == 3); - SuccessOrExit(error = aArgs[0].ParseAsIp6Address(address)); SuccessOrExit(error = aArgs[1].ParseAsUint8(seriesId)); SuccessOrExit(error = aArgs[2].ParseAsUint8(length)); @@ -2507,11 +2451,11 @@ exit: #endif // OPENTHREAD_CONFIG_MLE_LINK_METRICS_INITIATOR_ENABLE #if OPENTHREAD_FTD -otError Interpreter::ProcessPskc(uint8_t aArgsLength, Arg aArgs[]) +otError Interpreter::ProcessPskc(Arg aArgs[]) { otError error = OT_ERROR_NONE; - if (aArgsLength == 0) + if (aArgs[0].IsEmpty()) { const otPskc *pskc = otThreadGetPskc(mInstance); @@ -2522,7 +2466,7 @@ otError Interpreter::ProcessPskc(uint8_t aArgsLength, Arg aArgs[]) { otPskc pskc; - if (aArgsLength == 1) + if (aArgs[1].IsEmpty()) { SuccessOrExit(error = aArgs[0].ParseAsHexString(pskc.m8)); } @@ -2547,12 +2491,12 @@ exit: #endif // OPENTHREAD_FTD #if OPENTHREAD_CONFIG_REFERENCE_DEVICE_ENABLE -otError Interpreter::ProcessMlIid(uint8_t aArgsLength, Arg aArgs[]) +otError Interpreter::ProcessMlIid(Arg aArgs[]) { otError error = OT_ERROR_NONE; otIp6InterfaceIdentifier iid; - VerifyOrExit(aArgsLength == 1, error = OT_ERROR_INVALID_ARGS); + VerifyOrExit(aArgs[1].IsEmpty(), error = OT_ERROR_INVALID_ARGS); SuccessOrExit(error = aArgs[0].ParseAsHexString(iid.mFields.m8)); SuccessOrExit(error = otIp6SetMeshLocalIid(mInstance, &iid)); @@ -2564,53 +2508,47 @@ exit: #if OPENTHREAD_FTD && OPENTHREAD_CONFIG_TMF_PROXY_MLR_ENABLE && OPENTHREAD_CONFIG_COMMISSIONER_ENABLE -otError Interpreter::ProcessMlr(uint8_t aArgsLength, Arg aArgs[]) +otError Interpreter::ProcessMlr(Arg aArgs[]) { - otError error = OT_ERROR_NONE; - - VerifyOrExit(aArgsLength > 0, error = OT_ERROR_INVALID_COMMAND); + otError error = OT_ERROR_INVALID_COMMAND; if (aArgs[0] == "reg") { - error = ProcessMlrReg(aArgsLength - 1, aArgs + 1); - } - else - { - error = OT_ERROR_INVALID_COMMAND; + error = ProcessMlrReg(aArgs + 1); } -exit: return error; } -otError Interpreter::ProcessMlrReg(uint8_t aArgsLength, Arg aArgs[]) +otError Interpreter::ProcessMlrReg(Arg aArgs[]) { otError error = OT_ERROR_NONE; otIp6Address addresses[kIp6AddressesNumMax]; uint32_t timeout; - uint8_t i; + bool hasTimeout = false; + uint8_t numAddresses = 0; - VerifyOrExit(aArgsLength >= 1, error = OT_ERROR_INVALID_ARGS); - VerifyOrExit(aArgsLength <= kIp6AddressesNumMax + 1, error = OT_ERROR_INVALID_ARGS); - - for (i = 0; i < aArgsLength && i < kIp6AddressesNumMax; i++) + while (aArgs->ParseAsIp6Address(addresses[numAddresses]) == OT_ERROR_NONE) { - if (aArgs[i].ParseAsIp6Address(addresses[i]) != OT_ERROR_NONE) + aArgs++; + numAddresses++; + + if (numAddresses == OT_ARRAY_LENGTH(addresses)) { break; } } - VerifyOrExit(i > 0 && (i == aArgsLength || i == aArgsLength - 1), error = OT_ERROR_INVALID_ARGS); - - if (i == aArgsLength - 1) + if (aArgs->ParseAsUint32(timeout) == OT_ERROR_NONE) { - // Parse the last argument as a timeout in seconds - SuccessOrExit(error = aArgs[i].ParseAsUint32(timeout)); + aArgs++; + hasTimeout = true; } - SuccessOrExit(error = otIp6RegisterMulticastListeners(mInstance, addresses, i, - i == aArgsLength - 1 ? &timeout : nullptr, + VerifyOrExit(aArgs->IsEmpty() && (numAddresses > 0), error = OT_ERROR_INVALID_ARGS); + + SuccessOrExit(error = otIp6RegisterMulticastListeners(mInstance, addresses, numAddresses, + hasTimeout ? &timeout : nullptr, Interpreter::HandleMlrRegResult, this)); error = OT_ERROR_PENDING; @@ -2649,14 +2587,14 @@ void Interpreter::HandleMlrRegResult(otError aError, #endif // (OPENTHREAD_FTD && OPENTHREAD_CONFIG_TMF_PROXY_MLR_ENABLE) && OPENTHREAD_CONFIG_COMMISSIONER_ENABLE -otError Interpreter::ProcessMode(uint8_t aArgsLength, Arg aArgs[]) +otError Interpreter::ProcessMode(Arg aArgs[]) { otError error = OT_ERROR_NONE; otLinkModeConfig linkMode; memset(&linkMode, 0, sizeof(otLinkModeConfig)); - if (aArgsLength == 0) + if (aArgs[0].IsEmpty()) { linkMode = otThreadGetLinkMode(mInstance); @@ -2717,13 +2655,13 @@ exit: return error; } -otError Interpreter::ProcessMultiRadio(uint8_t aArgsLength, Arg aArgs[]) +otError Interpreter::ProcessMultiRadio(Arg aArgs[]) { otError error = OT_ERROR_NONE; OT_UNUSED_VARIABLE(aArgs); - if (aArgsLength == 0) + if (aArgs[0].IsEmpty()) { bool isFirst = true; @@ -2744,8 +2682,6 @@ otError Interpreter::ProcessMultiRadio(uint8_t aArgsLength, Arg aArgs[]) { otMultiRadioNeighborInfo multiRadioInfo; - VerifyOrExit(aArgsLength == 2, error = OT_ERROR_INVALID_ARGS); - if (aArgs[1] == "list") { otNeighborInfoIterator iterator = OT_NEIGHBOR_INFO_ITERATOR_INIT; @@ -2806,15 +2742,13 @@ void Interpreter::OutputMultiRadioInfo(const otMultiRadioNeighborInfo &aMultiRad #endif // OPENTHREAD_CONFIG_MULTI_RADIO #if OPENTHREAD_FTD -otError Interpreter::ProcessNeighbor(uint8_t aArgsLength, Arg aArgs[]) +otError Interpreter::ProcessNeighbor(Arg aArgs[]) { otError error = OT_ERROR_NONE; otNeighborInfo neighborInfo; bool isTable; otNeighborInfoIterator iterator = OT_NEIGHBOR_INFO_ITERATOR_INIT; - VerifyOrExit(aArgsLength > 0, error = OT_ERROR_INVALID_ARGS); - isTable = (aArgs[0] == "table"); if (isTable || (aArgs[0] == "list")) @@ -2858,17 +2792,15 @@ otError Interpreter::ProcessNeighbor(uint8_t aArgsLength, Arg aArgs[]) } else { - ExitNow(error = OT_ERROR_INVALID_ARGS); + error = OT_ERROR_INVALID_ARGS; } -exit: return error; } #endif // OPENTHREAD_FTD -otError Interpreter::ProcessNetstat(uint8_t aArgsLength, Arg aArgs[]) +otError Interpreter::ProcessNetstat(Arg aArgs[]) { - OT_UNUSED_VARIABLE(aArgsLength); OT_UNUSED_VARIABLE(aArgs); static const char *const kNetstatTableTitles[] = {"Local Address", "Peer Address"}; @@ -2903,12 +2835,12 @@ otError Interpreter::ProcessServiceList(void) return OT_ERROR_NONE; } -otError Interpreter::ProcessService(uint8_t aArgsLength, Arg aArgs[]) +otError Interpreter::ProcessService(Arg aArgs[]) { otError error = OT_ERROR_INVALID_COMMAND; otServiceConfig cfg; - if (aArgsLength == 0) + if (aArgs[0].IsEmpty()) { error = ProcessServiceList(); } @@ -2916,8 +2848,6 @@ otError Interpreter::ProcessService(uint8_t aArgsLength, Arg aArgs[]) { uint16_t length; - VerifyOrExit(aArgsLength > 2, error = OT_ERROR_INVALID_ARGS); - SuccessOrExit(error = aArgs[1].ParseAsUint32(cfg.mEnterpriseNumber)); length = sizeof(cfg.mServiceData); @@ -2927,8 +2857,6 @@ otError Interpreter::ProcessService(uint8_t aArgsLength, Arg aArgs[]) if (aArgs[0] == "add") { - VerifyOrExit(aArgsLength > 3, error = OT_ERROR_INVALID_ARGS); - length = sizeof(cfg.mServerConfig.mServerData); SuccessOrExit(error = aArgs[3].ParseAsHexString(length, cfg.mServerConfig.mServerData)); VerifyOrExit(length > 0, error = OT_ERROR_INVALID_ARGS); @@ -2949,23 +2877,23 @@ exit: } #endif // OPENTHREAD_CONFIG_TMF_NETDATA_SERVICE_ENABLE -otError Interpreter::ProcessNetworkData(uint8_t aArgsLength, Arg aArgs[]) +otError Interpreter::ProcessNetworkData(Arg aArgs[]) { - return mNetworkData.Process(aArgsLength, aArgs); + return mNetworkData.Process(aArgs); } #if OPENTHREAD_FTD -otError Interpreter::ProcessNetworkIdTimeout(uint8_t aArgsLength, Arg aArgs[]) +otError Interpreter::ProcessNetworkIdTimeout(Arg aArgs[]) { - return ProcessGetSet(aArgsLength, aArgs, otThreadGetNetworkIdTimeout, otThreadSetNetworkIdTimeout); + return ProcessGetSet(aArgs, otThreadGetNetworkIdTimeout, otThreadSetNetworkIdTimeout); } #endif -otError Interpreter::ProcessNetworkKey(uint8_t aArgsLength, Arg aArgs[]) +otError Interpreter::ProcessNetworkKey(Arg aArgs[]) { otError error = OT_ERROR_NONE; - if (aArgsLength == 0) + if (aArgs[0].IsEmpty()) { OutputBytes(otThreadGetNetworkKey(mInstance)->m8); OutputLine(""); @@ -2982,11 +2910,11 @@ exit: return error; } -otError Interpreter::ProcessNetworkName(uint8_t aArgsLength, Arg aArgs[]) +otError Interpreter::ProcessNetworkName(Arg aArgs[]) { otError error = OT_ERROR_NONE; - if (aArgsLength == 0) + if (aArgs[0].IsEmpty()) { OutputLine("%s", otThreadGetNetworkName(mInstance)); } @@ -3000,11 +2928,11 @@ exit: } #if OPENTHREAD_CONFIG_TIME_SYNC_ENABLE -otError Interpreter::ProcessNetworkTime(uint8_t aArgsLength, Arg aArgs[]) +otError Interpreter::ProcessNetworkTime(Arg aArgs[]) { otError error = OT_ERROR_NONE; - if (aArgsLength == 0) + if (aArgs[0].IsEmpty()) { uint64_t time; otNetworkTimeStatus networkTimeStatus; @@ -3034,7 +2962,7 @@ otError Interpreter::ProcessNetworkTime(uint8_t aArgsLength, Arg aArgs[]) OutputLine("Time Sync Period: %ds", otNetworkTimeGetSyncPeriod(mInstance)); OutputLine("XTAL Threshold: %dppm", otNetworkTimeGetXtalThreshold(mInstance)); } - else if (aArgsLength == 2) + else { uint16_t period; uint16_t xtalThreshold; @@ -3044,35 +2972,30 @@ otError Interpreter::ProcessNetworkTime(uint8_t aArgsLength, Arg aArgs[]) SuccessOrExit(error = otNetworkTimeSetSyncPeriod(mInstance, period)); SuccessOrExit(error = otNetworkTimeSetXtalThreshold(mInstance, xtalThreshold)); } - else - { - ExitNow(error = OT_ERROR_INVALID_ARGS); - } exit: return error; } #endif // OPENTHREAD_CONFIG_TIME_SYNC_ENABLE -otError Interpreter::ProcessPanId(uint8_t aArgsLength, Arg aArgs[]) +otError Interpreter::ProcessPanId(Arg aArgs[]) { otError error = OT_ERROR_NONE; - if (aArgsLength == 0) + if (aArgs[0].IsEmpty()) { OutputLine("0x%04x", otLinkGetPanId(mInstance)); } else { - error = ProcessSet(aArgsLength, aArgs, otLinkSetPanId); + error = ProcessSet(aArgs, otLinkSetPanId); } return error; } -otError Interpreter::ProcessParent(uint8_t aArgsLength, Arg aArgs[]) +otError Interpreter::ProcessParent(Arg aArgs[]) { - OT_UNUSED_VARIABLE(aArgsLength); OT_UNUSED_VARIABLE(aArgs); otError error = OT_ERROR_NONE; @@ -3092,9 +3015,9 @@ exit: } #if OPENTHREAD_FTD -otError Interpreter::ProcessParentPriority(uint8_t aArgsLength, Arg aArgs[]) +otError Interpreter::ProcessParentPriority(Arg aArgs[]) { - return ProcessGetSet(aArgsLength, aArgs, otThreadGetParentPriority, otThreadSetParentPriority); + return ProcessGetSet(aArgs, otThreadGetParentPriority, otThreadSetParentPriority); } #endif @@ -3140,13 +3063,11 @@ void Interpreter::HandlePingStatistics(const otPingSenderStatistics *aStatistics OutputResult(OT_ERROR_NONE); } -otError Interpreter::ProcessPing(uint8_t aArgsLength, Arg aArgs[]) +otError Interpreter::ProcessPing(Arg aArgs[]) { otError error = OT_ERROR_NONE; otPingSenderConfig config; - VerifyOrExit(aArgsLength > 0, error = OT_ERROR_INVALID_ARGS); - if (aArgs[0] == "stop") { otPingSenderStop(mInstance); @@ -3155,67 +3076,64 @@ otError Interpreter::ProcessPing(uint8_t aArgsLength, Arg aArgs[]) memset(&config, 0, sizeof(config)); - if (aArgsLength >= 2) + if (aArgs[0] == "-I") { - if (aArgs[0] == "-I") - { - SuccessOrExit(error = aArgs[1].ParseAsIp6Address(config.mSource)); + SuccessOrExit(error = aArgs[1].ParseAsIp6Address(config.mSource)); + #if !OPENTHREAD_CONFIG_REFERENCE_DEVICE_ENABLE + { + bool valid = false; + const otNetifAddress *unicastAddrs = otIp6GetUnicastAddresses(mInstance); + + for (const otNetifAddress *addr = unicastAddrs; addr; addr = addr->mNext) { - bool valid = false; - const otNetifAddress *unicastAddrs = otIp6GetUnicastAddresses(mInstance); - - SuccessOrExit(error = aArgs[1].ParseAsIp6Address(config.mSource)); - - for (const otNetifAddress *addr = unicastAddrs; addr; addr = addr->mNext) + if (otIp6IsAddressEqual(&addr->mAddress, &config.mSource)) { - if (otIp6IsAddressEqual(&addr->mAddress, &config.mSource)) - { - valid = true; - break; - } + valid = true; + break; } - - VerifyOrExit(valid, error = OT_ERROR_INVALID_ARGS); } -#endif - aArgs += 2; - aArgsLength -= 2; + + VerifyOrExit(valid, error = OT_ERROR_INVALID_ARGS); } +#endif + + aArgs += 2; } SuccessOrExit(error = aArgs[0].ParseAsIp6Address(config.mDestination)); - if (aArgsLength > 1) + if (!aArgs[1].IsEmpty()) { SuccessOrExit(error = aArgs[1].ParseAsUint16(config.mSize)); } - if (aArgsLength > 2) + if (!aArgs[2].IsEmpty()) { SuccessOrExit(error = aArgs[2].ParseAsUint16(config.mCount)); } - if (aArgsLength > 3) + if (!aArgs[3].IsEmpty()) { SuccessOrExit(error = ParsePingInterval(aArgs[3], config.mInterval)); } - if (aArgsLength > 4) + if (!aArgs[4].IsEmpty()) { SuccessOrExit(error = aArgs[4].ParseAsUint8(config.mHopLimit)); config.mAllowZeroHopLimit = (config.mHopLimit == 0); } - if (aArgsLength > 5) + if (!aArgs[5].IsEmpty()) { uint32_t timeout; + SuccessOrExit(error = ParsePingInterval(aArgs[5], timeout)); VerifyOrExit(timeout <= NumericLimits::kMax, error = OT_ERROR_INVALID_ARGS); config.mTimeout = static_cast(timeout); } - VerifyOrExit(aArgsLength <= 6, error = OT_ERROR_INVALID_ARGS); + VerifyOrExit(aArgs[6].IsEmpty(), error = OT_ERROR_INVALID_ARGS); config.mReplyCallback = Interpreter::HandlePingReply; config.mStatisticsCallback = Interpreter::HandlePingStatistics; @@ -3229,16 +3147,16 @@ exit: #endif // OPENTHREAD_CONFIG_PING_SENDER_ENABLE -otError Interpreter::ProcessPollPeriod(uint8_t aArgsLength, Arg aArgs[]) +otError Interpreter::ProcessPollPeriod(Arg aArgs[]) { - return ProcessGetSet(aArgsLength, aArgs, otLinkGetPollPeriod, otLinkSetPollPeriod); + return ProcessGetSet(aArgs, otLinkGetPollPeriod, otLinkSetPollPeriod); } -otError Interpreter::ProcessPromiscuous(uint8_t aArgsLength, Arg aArgs[]) +otError Interpreter::ProcessPromiscuous(Arg aArgs[]) { otError error = OT_ERROR_NONE; - if (aArgsLength == 0) + if (aArgs[0].IsEmpty()) { OutputEnabledDisabledStatus(otLinkIsPromiscuous(mInstance) && otPlatRadioGetPromiscuous(mInstance)); } @@ -3339,36 +3257,33 @@ void Interpreter::HandleLinkPcapReceive(const otRadioFrame *aFrame, bool aIsTx) } #if OPENTHREAD_CONFIG_BORDER_ROUTER_ENABLE -otError Interpreter::ProcessPrefixAdd(uint8_t aArgsLength, Arg aArgs[]) +otError Interpreter::ProcessPrefixAdd(Arg aArgs[]) { otError error = OT_ERROR_NONE; otBorderRouterConfig config; - uint8_t argcur = 0; - - VerifyOrExit(aArgsLength > 0, error = OT_ERROR_INVALID_ARGS); memset(&config, 0, sizeof(otBorderRouterConfig)); - SuccessOrExit(error = aArgs[argcur].ParseAsIp6Prefix(config.mPrefix)); - argcur++; + SuccessOrExit(error = aArgs[0].ParseAsIp6Prefix(config.mPrefix)); + aArgs++; - for (; argcur < aArgsLength; argcur++) + for (; !aArgs->IsEmpty(); aArgs++) { - if (aArgs[argcur] == "high") + if (*aArgs == "high") { config.mPreference = OT_ROUTE_PREFERENCE_HIGH; } - else if (aArgs[argcur] == "med") + else if (*aArgs == "med") { config.mPreference = OT_ROUTE_PREFERENCE_MED; } - else if (aArgs[argcur] == "low") + else if (*aArgs == "low") { config.mPreference = OT_ROUTE_PREFERENCE_LOW; } else { - for (char *arg = aArgs[argcur].GetCString(); *arg != '\0'; arg++) + for (char *arg = aArgs->GetCString(); *arg != '\0'; arg++) { switch (*arg) { @@ -3422,13 +3337,11 @@ exit: return error; } -otError Interpreter::ProcessPrefixRemove(uint8_t aArgsLength, Arg aArgs[]) +otError Interpreter::ProcessPrefixRemove(Arg aArgs[]) { otError error = OT_ERROR_NONE; otIp6Prefix prefix; - VerifyOrExit(aArgsLength > 0, error = OT_ERROR_INVALID_ARGS); - SuccessOrExit(error = aArgs[0].ParseAsIp6Prefix(prefix)); error = otBorderRouterRemoveOnMeshPrefix(mInstance, &prefix); @@ -3461,21 +3374,21 @@ exit: return OT_ERROR_NONE; } -otError Interpreter::ProcessPrefix(uint8_t aArgsLength, Arg aArgs[]) +otError Interpreter::ProcessPrefix(Arg aArgs[]) { otError error = OT_ERROR_NONE; - if (aArgsLength == 0) + if (aArgs[0].IsEmpty()) { SuccessOrExit(error = ProcessPrefixList()); } else if (aArgs[0] == "add") { - SuccessOrExit(error = ProcessPrefixAdd(aArgsLength - 1, aArgs + 1)); + SuccessOrExit(error = ProcessPrefixAdd(aArgs + 1)); } else if (aArgs[0] == "remove") { - SuccessOrExit(error = ProcessPrefixRemove(aArgsLength - 1, aArgs + 1)); + SuccessOrExit(error = ProcessPrefixRemove(aArgs + 1)); } else if (aArgs[0] == "meshlocal") { @@ -3493,19 +3406,18 @@ exit: #endif // OPENTHREAD_CONFIG_BORDER_ROUTER_ENABLE #if OPENTHREAD_FTD -otError Interpreter::ProcessPreferRouterId(uint8_t aArgsLength, Arg aArgs[]) +otError Interpreter::ProcessPreferRouterId(Arg aArgs[]) { - return ProcessSet(aArgsLength, aArgs, otThreadSetPreferredRouterId); + return ProcessSet(aArgs, otThreadSetPreferredRouterId); } #endif -otError Interpreter::ProcessRcp(uint8_t aArgsLength, Arg aArgs[]) +otError Interpreter::ProcessRcp(Arg aArgs[]) { otError error = OT_ERROR_NONE; const char *version = otPlatRadioGetVersionString(mInstance); VerifyOrExit(version != otGetVersionString(), error = OT_ERROR_NOT_IMPLEMENTED); - VerifyOrExit(aArgsLength > 0, error = OT_ERROR_INVALID_ARGS); if (aArgs[0] == "version") { @@ -3513,19 +3425,19 @@ otError Interpreter::ProcessRcp(uint8_t aArgsLength, Arg aArgs[]) } else { - ExitNow(error = OT_ERROR_INVALID_ARGS); + error = OT_ERROR_INVALID_ARGS; } exit: return error; } -otError Interpreter::ProcessRegion(uint8_t aArgsLength, Arg aArgs[]) +otError Interpreter::ProcessRegion(Arg aArgs[]) { otError error = OT_ERROR_NONE; uint16_t regionCode; - if (aArgsLength == 0) + if (aArgs[0].IsEmpty()) { SuccessOrExit(error = otPlatRadioGetRegion(mInstance, ®ionCode)); OutputLine("%c%c", regionCode >> 8, regionCode & 0xff); @@ -3544,15 +3456,14 @@ exit: } #if OPENTHREAD_FTD -otError Interpreter::ProcessReleaseRouterId(uint8_t aArgsLength, Arg aArgs[]) +otError Interpreter::ProcessReleaseRouterId(Arg aArgs[]) { - return ProcessSet(aArgsLength, aArgs, otThreadReleaseRouterId); + return ProcessSet(aArgs, otThreadReleaseRouterId); } #endif -otError Interpreter::ProcessReset(uint8_t aArgsLength, Arg aArgs[]) +otError Interpreter::ProcessReset(Arg aArgs[]) { - OT_UNUSED_VARIABLE(aArgsLength); OT_UNUSED_VARIABLE(aArgs); otInstanceReset(mInstance); @@ -3560,9 +3471,8 @@ otError Interpreter::ProcessReset(uint8_t aArgsLength, Arg aArgs[]) return OT_ERROR_NONE; } -otError Interpreter::ProcessRloc16(uint8_t aArgsLength, Arg aArgs[]) +otError Interpreter::ProcessRloc16(Arg aArgs[]) { - OT_UNUSED_VARIABLE(aArgsLength); OT_UNUSED_VARIABLE(aArgs); OutputLine("%04x", otThreadGetRloc16(mInstance)); @@ -3571,38 +3481,35 @@ otError Interpreter::ProcessRloc16(uint8_t aArgsLength, Arg aArgs[]) } #if OPENTHREAD_CONFIG_BORDER_ROUTER_ENABLE -otError Interpreter::ProcessRouteAdd(uint8_t aArgsLength, Arg aArgs[]) +otError Interpreter::ProcessRouteAdd(Arg aArgs[]) { otError error = OT_ERROR_NONE; otExternalRouteConfig config; - uint8_t argcur = 0; memset(&config, 0, sizeof(otExternalRouteConfig)); - VerifyOrExit(aArgsLength > 0, error = OT_ERROR_INVALID_ARGS); + SuccessOrExit(error = aArgs[0].ParseAsIp6Prefix(config.mPrefix)); + aArgs++; - SuccessOrExit(error = aArgs[argcur].ParseAsIp6Prefix(config.mPrefix)); - argcur++; - - for (; argcur < aArgsLength; argcur++) + for (; !aArgs->IsEmpty(); aArgs++) { - if (aArgs[argcur] == "s") + if (*aArgs == "s") { config.mStable = true; } - else if (aArgs[argcur] == "n") + else if (*aArgs == "n") { config.mNat64 = true; } - else if (aArgs[argcur] == "high") + else if (*aArgs == "high") { config.mPreference = OT_ROUTE_PREFERENCE_HIGH; } - else if (aArgs[argcur] == "med") + else if (*aArgs == "med") { config.mPreference = OT_ROUTE_PREFERENCE_MED; } - else if (aArgs[argcur] == "low") + else if (*aArgs == "low") { config.mPreference = OT_ROUTE_PREFERENCE_LOW; } @@ -3618,13 +3525,11 @@ exit: return error; } -otError Interpreter::ProcessRouteRemove(uint8_t aArgsLength, Arg aArgs[]) +otError Interpreter::ProcessRouteRemove(Arg aArgs[]) { otError error = OT_ERROR_NONE; otIp6Prefix prefix; - VerifyOrExit(aArgsLength > 0, error = OT_ERROR_INVALID_ARGS); - SuccessOrExit(error = aArgs[0].ParseAsIp6Prefix(prefix)); error = otBorderRouterRemoveRoute(mInstance, &prefix); @@ -3646,21 +3551,21 @@ otError Interpreter::ProcessRouteList(void) return OT_ERROR_NONE; } -otError Interpreter::ProcessRoute(uint8_t aArgsLength, Arg aArgs[]) +otError Interpreter::ProcessRoute(Arg aArgs[]) { otError error = OT_ERROR_NONE; - if (aArgsLength == 0) + if (aArgs[0].IsEmpty()) { SuccessOrExit(error = ProcessRouteList()); } else if (aArgs[0] == "add") { - SuccessOrExit(error = ProcessRouteAdd(aArgsLength - 1, aArgs + 1)); + SuccessOrExit(error = ProcessRouteAdd(aArgs + 1)); } else if (aArgs[0] == "remove") { - SuccessOrExit(error = ProcessRouteRemove(aArgsLength - 1, aArgs + 1)); + SuccessOrExit(error = ProcessRouteRemove(aArgs + 1)); } else { @@ -3673,15 +3578,13 @@ exit: #endif // OPENTHREAD_CONFIG_BORDER_ROUTER_ENABLE #if OPENTHREAD_FTD -otError Interpreter::ProcessRouter(uint8_t aArgsLength, Arg aArgs[]) +otError Interpreter::ProcessRouter(Arg aArgs[]) { otError error = OT_ERROR_NONE; otRouterInfo routerInfo; uint16_t routerId; bool isTable; - VerifyOrExit(aArgsLength > 0, error = OT_ERROR_INVALID_ARGS); - isTable = (aArgs[0] == "table"); if (isTable || (aArgs[0] == "list")) @@ -3761,16 +3664,16 @@ exit: return error; } -otError Interpreter::ProcessRouterDowngradeThreshold(uint8_t aArgsLength, Arg aArgs[]) +otError Interpreter::ProcessRouterDowngradeThreshold(Arg aArgs[]) { - return ProcessGetSet(aArgsLength, aArgs, otThreadGetRouterDowngradeThreshold, otThreadSetRouterDowngradeThreshold); + return ProcessGetSet(aArgs, otThreadGetRouterDowngradeThreshold, otThreadSetRouterDowngradeThreshold); } -otError Interpreter::ProcessRouterEligible(uint8_t aArgsLength, Arg aArgs[]) +otError Interpreter::ProcessRouterEligible(Arg aArgs[]) { otError error = OT_ERROR_NONE; - if (aArgsLength == 0) + if (aArgs[0].IsEmpty()) { OutputEnabledDisabledStatus(otThreadIsRouterEligible(mInstance)); } @@ -3786,46 +3689,43 @@ exit: return error; } -otError Interpreter::ProcessRouterSelectionJitter(uint8_t aArgsLength, Arg aArgs[]) +otError Interpreter::ProcessRouterSelectionJitter(Arg aArgs[]) { - return ProcessGetSet(aArgsLength, aArgs, otThreadGetRouterSelectionJitter, otThreadSetRouterSelectionJitter); + return ProcessGetSet(aArgs, otThreadGetRouterSelectionJitter, otThreadSetRouterSelectionJitter); } -otError Interpreter::ProcessRouterUpgradeThreshold(uint8_t aArgsLength, Arg aArgs[]) +otError Interpreter::ProcessRouterUpgradeThreshold(Arg aArgs[]) { - return ProcessGetSet(aArgsLength, aArgs, otThreadGetRouterUpgradeThreshold, otThreadSetRouterUpgradeThreshold); + return ProcessGetSet(aArgs, otThreadGetRouterUpgradeThreshold, otThreadSetRouterUpgradeThreshold); } #endif // OPENTHREAD_FTD -otError Interpreter::ProcessScan(uint8_t aArgsLength, Arg aArgs[]) +otError Interpreter::ProcessScan(Arg aArgs[]) { otError error = OT_ERROR_NONE; uint32_t scanChannels = 0; uint16_t scanDuration = 0; bool energyScan = false; - uint8_t index = 0; - if (aArgsLength > 0) + if (aArgs[0] == "energy") { - if (aArgs[index] == "energy") + energyScan = true; + aArgs++; + + if (!aArgs->IsEmpty()) { - energyScan = true; - index++; - - if (aArgsLength > 1) - { - SuccessOrExit(error = aArgs[index++].ParseAsUint16(scanDuration)); - } + SuccessOrExit(error = aArgs->ParseAsUint16(scanDuration)); + aArgs++; } + } - if (index < aArgsLength) - { - uint8_t channel; + if (!aArgs->IsEmpty()) + { + uint8_t channel; - SuccessOrExit(error = aArgs[index++].ParseAsUint8(channel)); - VerifyOrExit(channel < sizeof(scanChannels) * CHAR_BIT, error = OT_ERROR_INVALID_ARGS); - scanChannels = 1 << channel; - } + SuccessOrExit(error = aArgs->ParseAsUint8(channel)); + VerifyOrExit(channel < sizeof(scanChannels) * CHAR_BIT, error = OT_ERROR_INVALID_ARGS); + scanChannels = 1 << channel; } if (energyScan) @@ -3913,9 +3813,8 @@ exit: return; } -otError Interpreter::ProcessSingleton(uint8_t aArgsLength, Arg aArgs[]) +otError Interpreter::ProcessSingleton(Arg aArgs[]) { - OT_UNUSED_VARIABLE(aArgsLength); OT_UNUSED_VARIABLE(aArgs); if (otThreadIsSingleton(mInstance)) @@ -3931,20 +3830,18 @@ otError Interpreter::ProcessSingleton(uint8_t aArgsLength, Arg aArgs[]) } #if OPENTHREAD_CONFIG_SNTP_CLIENT_ENABLE -otError Interpreter::ProcessSntp(uint8_t aArgsLength, Arg aArgs[]) +otError Interpreter::ProcessSntp(Arg aArgs[]) { otError error = OT_ERROR_NONE; uint16_t port = OT_SNTP_DEFAULT_SERVER_PORT; Ip6::MessageInfo messageInfo; otSntpQuery query; - VerifyOrExit(aArgsLength > 0, error = OT_ERROR_INVALID_ARGS); - if (aArgs[0] == "query") { VerifyOrExit(!mSntpQueryingInProgress, error = OT_ERROR_BUSY); - if (aArgsLength > 1) + if (!aArgs[1].IsEmpty()) { SuccessOrExit(error = aArgs[1].ParseAsIp6Address(messageInfo.GetPeerAddr())); } @@ -3954,7 +3851,7 @@ otError Interpreter::ProcessSntp(uint8_t aArgsLength, Arg aArgs[]) SuccessOrExit(error = messageInfo.GetPeerAddr().FromString(OT_SNTP_DEFAULT_SERVER_IP)); } - if (aArgsLength > 2) + if (!aArgs[2].IsEmpty()) { SuccessOrExit(error = aArgs[2].ParseAsUint16(port)); } @@ -3966,14 +3863,13 @@ otError Interpreter::ProcessSntp(uint8_t aArgsLength, Arg aArgs[]) SuccessOrExit(error = otSntpClientQuery(mInstance, &query, &Interpreter::HandleSntpResponse, this)); mSntpQueryingInProgress = true; + error = OT_ERROR_PENDING; } else { - ExitNow(error = OT_ERROR_INVALID_COMMAND); + error = OT_ERROR_INVALID_COMMAND; } - error = OT_ERROR_PENDING; - exit: return error; } @@ -4004,11 +3900,11 @@ void Interpreter::HandleSntpResponse(uint64_t aTime, otError aResult) #endif // OPENTHREAD_CONFIG_SNTP_CLIENT_ENABLE #if OPENTHREAD_CONFIG_SRP_CLIENT_ENABLE || OPENTHREAD_CONFIG_SRP_SERVER_ENABLE -otError Interpreter::ProcessSrp(uint8_t aArgsLength, Arg aArgs[]) +otError Interpreter::ProcessSrp(Arg aArgs[]) { otError error = OT_ERROR_NONE; - if (aArgsLength == 0) + if (aArgs[0].IsEmpty()) { #if OPENTHREAD_CONFIG_SRP_CLIENT_ENABLE OutputLine("client"); @@ -4022,13 +3918,13 @@ otError Interpreter::ProcessSrp(uint8_t aArgsLength, Arg aArgs[]) #if OPENTHREAD_CONFIG_SRP_CLIENT_ENABLE if (aArgs[0] == "client") { - ExitNow(error = mSrpClient.Process(aArgsLength - 1, aArgs + 1)); + ExitNow(error = mSrpClient.Process(aArgs + 1)); } #endif #if OPENTHREAD_CONFIG_SRP_SERVER_ENABLE if (aArgs[0] == "server") { - ExitNow(error = mSrpServer.Process(aArgsLength - 1, aArgs + 1)); + ExitNow(error = mSrpServer.Process(aArgs + 1)); } #endif @@ -4039,11 +3935,11 @@ exit: } #endif // OPENTHREAD_CONFIG_SRP_CLIENT_ENABLE || OPENTHREAD_CONFIG_SRP_SERVER_ENABLE -otError Interpreter::ProcessState(uint8_t aArgsLength, Arg aArgs[]) +otError Interpreter::ProcessState(Arg aArgs[]) { otError error = OT_ERROR_NONE; - if (aArgsLength == 0) + if (aArgs[0].IsEmpty()) { switch (otThreadGetDeviceRole(mInstance)) { @@ -4105,19 +4001,17 @@ exit: return error; } -otError Interpreter::ProcessThread(uint8_t aArgsLength, Arg aArgs[]) +otError Interpreter::ProcessThread(Arg aArgs[]) { otError error = OT_ERROR_NONE; - VerifyOrExit(aArgsLength > 0, error = OT_ERROR_INVALID_ARGS); - if (aArgs[0] == "start") { - SuccessOrExit(error = otThreadSetEnabled(mInstance, true)); + error = otThreadSetEnabled(mInstance, true); } else if (aArgs[0] == "stop") { - SuccessOrExit(error = otThreadSetEnabled(mInstance, false)); + error = otThreadSetEnabled(mInstance, false); } else if (aArgs[0] == "version") { @@ -4125,24 +4019,23 @@ otError Interpreter::ProcessThread(uint8_t aArgsLength, Arg aArgs[]) } else { - ExitNow(error = OT_ERROR_INVALID_COMMAND); + error = OT_ERROR_INVALID_COMMAND; } -exit: return error; } -otError Interpreter::ProcessDataset(uint8_t aArgsLength, Arg aArgs[]) +otError Interpreter::ProcessDataset(Arg aArgs[]) { - return mDataset.Process(aArgsLength, aArgs); + return mDataset.Process(aArgs); } -otError Interpreter::ProcessTxPower(uint8_t aArgsLength, Arg aArgs[]) +otError Interpreter::ProcessTxPower(Arg aArgs[]) { otError error = OT_ERROR_NONE; int8_t power; - if (aArgsLength == 0) + if (aArgs[0].IsEmpty()) { SuccessOrExit(error = otPlatRadioGetTransmitPower(mInstance, &power)); OutputLine("%d dBm", power); @@ -4157,32 +4050,28 @@ exit: return error; } -otError Interpreter::ProcessUdp(uint8_t aArgsLength, Arg aArgs[]) +otError Interpreter::ProcessUdp(Arg aArgs[]) { - return mUdp.Process(aArgsLength, aArgs); + return mUdp.Process(aArgs); } -otError Interpreter::ProcessUnsecurePort(uint8_t aArgsLength, Arg aArgs[]) +otError Interpreter::ProcessUnsecurePort(Arg aArgs[]) { otError error = OT_ERROR_NONE; - VerifyOrExit(aArgsLength >= 1, error = OT_ERROR_INVALID_ARGS); - if (aArgs[0] == "add") { - error = ProcessSet(aArgsLength - 1, aArgs + 1, otIp6AddUnsecurePort); + error = ProcessSet(aArgs + 1, otIp6AddUnsecurePort); } else if (aArgs[0] == "remove") { - VerifyOrExit(aArgsLength == 2, error = OT_ERROR_INVALID_ARGS); - if (aArgs[1] == "all") { otIp6RemoveAllUnsecurePorts(mInstance); } else { - error = ProcessSet(aArgsLength - 1, aArgs + 1, otIp6RemoveUnsecurePort); + error = ProcessSet(aArgs + 1, otIp6RemoveUnsecurePort); } } else if (aArgs[0] == "get") @@ -4211,11 +4100,11 @@ exit: return error; } -otError Interpreter::ProcessVersion(uint8_t aArgsLength, Arg aArgs[]) +otError Interpreter::ProcessVersion(Arg aArgs[]) { otError error = OT_ERROR_NONE; - if (aArgsLength == 0) + if (aArgs[0].IsEmpty()) { OutputLine("%s", otGetVersionString()); ExitNow(); @@ -4235,32 +4124,32 @@ exit: } #if OPENTHREAD_CONFIG_COMMISSIONER_ENABLE && OPENTHREAD_FTD -otError Interpreter::ProcessCommissioner(uint8_t aArgsLength, Arg aArgs[]) +otError Interpreter::ProcessCommissioner(Arg aArgs[]) { - return mCommissioner.Process(aArgsLength, aArgs); + return mCommissioner.Process(aArgs); } #endif #if OPENTHREAD_CONFIG_JOINER_ENABLE -otError Interpreter::ProcessJoiner(uint8_t aArgsLength, Arg aArgs[]) +otError Interpreter::ProcessJoiner(Arg aArgs[]) { - return mJoiner.Process(aArgsLength, aArgs); + return mJoiner.Process(aArgs); } #endif #if OPENTHREAD_FTD -otError Interpreter::ProcessJoinerPort(uint8_t aArgsLength, Arg aArgs[]) +otError Interpreter::ProcessJoinerPort(Arg aArgs[]) { - return ProcessGetSet(aArgsLength, aArgs, otThreadGetJoinerUdpPort, otThreadSetJoinerUdpPort); + return ProcessGetSet(aArgs, otThreadGetJoinerUdpPort, otThreadSetJoinerUdpPort); } #endif #if OPENTHREAD_CONFIG_MAC_FILTER_ENABLE -otError Interpreter::ProcessMacFilter(uint8_t aArgsLength, Arg aArgs[]) +otError Interpreter::ProcessMacFilter(Arg aArgs[]) { otError error = OT_ERROR_NONE; - if (aArgsLength == 0) + if (aArgs[0].IsEmpty()) { PrintMacFilter(); } @@ -4268,11 +4157,11 @@ otError Interpreter::ProcessMacFilter(uint8_t aArgsLength, Arg aArgs[]) { if (aArgs[0] == "addr") { - error = ProcessMacFilterAddress(aArgsLength - 1, aArgs + 1); + error = ProcessMacFilterAddress(aArgs + 1); } else if (aArgs[0] == "rss") { - error = ProcessMacFilterRss(aArgsLength - 1, aArgs + 1); + error = ProcessMacFilterRss(aArgs + 1); } else { @@ -4342,7 +4231,7 @@ void Interpreter::PrintMacFilter(void) } } -otError Interpreter::ProcessMacFilterAddress(uint8_t aArgsLength, Arg aArgs[]) +otError Interpreter::ProcessMacFilterAddress(Arg aArgs[]) { otError error = OT_ERROR_NONE; otExtAddress extAddr; @@ -4350,7 +4239,7 @@ otError Interpreter::ProcessMacFilterAddress(uint8_t aArgsLength, Arg aArgs[]) otMacFilterIterator iterator = OT_MAC_FILTER_ITERATOR_INIT; otMacFilterAddressMode mode = otLinkFilterGetAddressMode(mInstance); - if (aArgsLength == 0) + if (aArgs[0].IsEmpty()) { if (mode == OT_MAC_FILTER_ADDRESS_MODE_DISABLED) { @@ -4382,45 +4271,41 @@ otError Interpreter::ProcessMacFilterAddress(uint8_t aArgsLength, Arg aArgs[]) { if (aArgs[0] == "disable") { - VerifyOrExit(aArgsLength == 1, error = OT_ERROR_INVALID_ARGS); + VerifyOrExit(aArgs[1].IsEmpty(), error = OT_ERROR_INVALID_ARGS); otLinkFilterSetAddressMode(mInstance, OT_MAC_FILTER_ADDRESS_MODE_DISABLED); } else if (aArgs[0] == "allowlist") { - VerifyOrExit(aArgsLength == 1, error = OT_ERROR_INVALID_ARGS); + VerifyOrExit(aArgs[1].IsEmpty(), error = OT_ERROR_INVALID_ARGS); otLinkFilterSetAddressMode(mInstance, OT_MAC_FILTER_ADDRESS_MODE_ALLOWLIST); } else if (aArgs[0] == "denylist") { - VerifyOrExit(aArgsLength == 1, error = OT_ERROR_INVALID_ARGS); + VerifyOrExit(aArgs[1].IsEmpty(), error = OT_ERROR_INVALID_ARGS); otLinkFilterSetAddressMode(mInstance, OT_MAC_FILTER_ADDRESS_MODE_DENYLIST); } else if (aArgs[0] == "add") { - VerifyOrExit(aArgsLength >= 2, error = OT_ERROR_INVALID_ARGS); SuccessOrExit(error = aArgs[1].ParseAsHexString(extAddr.m8)); error = otLinkFilterAddAddress(mInstance, &extAddr); VerifyOrExit(error == OT_ERROR_NONE || error == OT_ERROR_ALREADY); - if (aArgsLength > 2) + if (!aArgs[2].IsEmpty()) { int8_t rss; - VerifyOrExit(aArgsLength == 3, error = OT_ERROR_INVALID_ARGS); SuccessOrExit(error = aArgs[2].ParseAsInt8(rss)); SuccessOrExit(error = otLinkFilterAddRssIn(mInstance, &extAddr, rss)); } } else if (aArgs[0] == "remove") { - VerifyOrExit(aArgsLength == 2, error = OT_ERROR_INVALID_ARGS); SuccessOrExit(error = aArgs[1].ParseAsHexString(extAddr.m8)); otLinkFilterRemoveAddress(mInstance, &extAddr); } else if (aArgs[0] == "clear") { - VerifyOrExit(aArgsLength == 1, error = OT_ERROR_INVALID_ARGS); otLinkFilterClearAddresses(mInstance); } else @@ -4433,7 +4318,7 @@ exit: return error; } -otError Interpreter::ProcessMacFilterRss(uint8_t aArgsLength, Arg aArgs[]) +otError Interpreter::ProcessMacFilterRss(Arg aArgs[]) { otError error = OT_ERROR_NONE; otMacFilterEntry entry; @@ -4441,7 +4326,7 @@ otError Interpreter::ProcessMacFilterRss(uint8_t aArgsLength, Arg aArgs[]) otExtAddress extAddr; int8_t rss; - if (aArgsLength == 0) + if (aArgs[0].IsEmpty()) { while (otLinkFilterGetNextRssIn(mInstance, &iterator, &entry) == OT_ERROR_NONE) { @@ -4473,8 +4358,6 @@ otError Interpreter::ProcessMacFilterRss(uint8_t aArgsLength, Arg aArgs[]) { uint8_t linkQuality; - VerifyOrExit(aArgsLength == 3, error = OT_ERROR_INVALID_ARGS); - SuccessOrExit(error = aArgs[2].ParseAsUint8(linkQuality)); VerifyOrExit(linkQuality <= 3, error = OT_ERROR_INVALID_ARGS); rss = otLinkConvertLinkQualityToRss(mInstance, linkQuality); @@ -4491,7 +4374,6 @@ otError Interpreter::ProcessMacFilterRss(uint8_t aArgsLength, Arg aArgs[]) } else if (aArgs[0] == "add") { - VerifyOrExit(aArgsLength == 3, error = OT_ERROR_INVALID_ARGS); SuccessOrExit(error = aArgs[2].ParseAsInt8(rss)); if (aArgs[1] == "*") @@ -4506,8 +4388,6 @@ otError Interpreter::ProcessMacFilterRss(uint8_t aArgsLength, Arg aArgs[]) } else if (aArgs[0] == "remove") { - VerifyOrExit(aArgsLength == 2, error = OT_ERROR_INVALID_ARGS); - if (aArgs[1] == "*") { otLinkFilterClearDefaultRssIn(mInstance); @@ -4534,20 +4414,18 @@ exit: #endif // OPENTHREAD_CONFIG_MAC_FILTER_ENABLE -otError Interpreter::ProcessMac(uint8_t aArgsLength, Arg aArgs[]) +otError Interpreter::ProcessMac(Arg aArgs[]) { otError error = OT_ERROR_NONE; - VerifyOrExit(aArgsLength > 0, error = OT_ERROR_INVALID_ARGS); - if (aArgs[0] == "retries") { - error = ProcessMacRetries(aArgsLength - 1, aArgs + 1); + error = ProcessMacRetries(aArgs + 1); } #if OPENTHREAD_CONFIG_REFERENCE_DEVICE_ENABLE else if (aArgs[0] == "send") { - error = ProcessMacSend(aArgsLength - 1, aArgs + 1); + error = ProcessMacSend(aArgs + 1); } #endif else @@ -4555,26 +4433,21 @@ otError Interpreter::ProcessMac(uint8_t aArgsLength, Arg aArgs[]) error = OT_ERROR_INVALID_COMMAND; } -exit: return error; } -otError Interpreter::ProcessMacRetries(uint8_t aArgsLength, Arg aArgs[]) +otError Interpreter::ProcessMacRetries(Arg aArgs[]) { otError error = OT_ERROR_NONE; - VerifyOrExit(aArgsLength > 0 && aArgsLength <= 2, error = OT_ERROR_INVALID_ARGS); - if (aArgs[0] == "direct") { - error = - ProcessGetSet(aArgsLength - 1, aArgs + 1, otLinkGetMaxFrameRetriesDirect, otLinkSetMaxFrameRetriesDirect); + error = ProcessGetSet(aArgs + 1, otLinkGetMaxFrameRetriesDirect, otLinkSetMaxFrameRetriesDirect); } #if OPENTHREAD_FTD else if (aArgs[0] == "indirect") { - error = ProcessGetSet(aArgsLength - 1, aArgs + 1, otLinkGetMaxFrameRetriesIndirect, - otLinkSetMaxFrameRetriesIndirect); + error = ProcessGetSet(aArgs + 1, otLinkGetMaxFrameRetriesIndirect, otLinkSetMaxFrameRetriesIndirect); } #endif else @@ -4582,16 +4455,15 @@ otError Interpreter::ProcessMacRetries(uint8_t aArgsLength, Arg aArgs[]) error = OT_ERROR_INVALID_ARGS; } -exit: return error; } #if OPENTHREAD_CONFIG_REFERENCE_DEVICE_ENABLE -otError Interpreter::ProcessMacSend(uint8_t aArgsLength, Arg aArgs[]) +otError Interpreter::ProcessMacSend(Arg aArgs[]) { otError error = OT_ERROR_INVALID_ARGS; - VerifyOrExit(aArgsLength == 1); + VerifyOrExit(aArgs[1].IsEmpty()); if (aArgs[0] == "datarequest") { @@ -4608,7 +4480,7 @@ exit: #endif #if OPENTHREAD_CONFIG_DIAG_ENABLE -otError Interpreter::ProcessDiag(uint8_t aArgsLength, Arg aArgs[]) +otError Interpreter::ProcessDiag(Arg aArgs[]) { otError error; char * args[kMaxArgs]; @@ -4618,9 +4490,9 @@ otError Interpreter::ProcessDiag(uint8_t aArgsLength, Arg aArgs[]) output[0] = '\0'; output[sizeof(output) - 1] = '\0'; - Arg::CopyArgsToStringArray(aArgs, aArgsLength, args); + Arg::CopyArgsToStringArray(aArgs, args); - error = otDiagProcessCmd(mInstance, aArgsLength, args, output, sizeof(output) - 1); + error = otDiagProcessCmd(mInstance, Arg::GetArgsLength(aArgs), args, output, sizeof(output) - 1); OutputFormat("%s", output); @@ -4630,8 +4502,7 @@ otError Interpreter::ProcessDiag(uint8_t aArgsLength, Arg aArgs[]) void Interpreter::ProcessLine(char *aBuf) { - Arg args[kMaxArgs]; - uint8_t argsLength; + Arg args[kMaxArgs + 1]; const Command *command; otError error = OT_ERROR_NONE; @@ -4639,35 +4510,43 @@ void Interpreter::ProcessLine(char *aBuf) VerifyOrExit(StringLength(aBuf, kMaxLineLength) <= kMaxLineLength - 1, error = OT_ERROR_PARSE); - SuccessOrExit(error = Utils::CmdLineParser::ParseCmd(aBuf, argsLength, args, kMaxArgs)); - VerifyOrExit(argsLength >= 1); + error = Utils::CmdLineParser::ParseCmd(aBuf, args); + + if (error != OT_ERROR_NONE) + { + OutputLine("Error: too many args (max %d)", kMaxArgs); + ExitNow(); + } + + VerifyOrExit(!args[0].IsEmpty()); #if OPENTHREAD_CONFIG_DIAG_ENABLE - VerifyOrExit((!otDiagIsEnabled(mInstance) || (args[0] == "diag")), { + if (otDiagIsEnabled(mInstance) && (args[0] != "diag")) + { OutputLine("under diagnostics mode, execute 'diag stop' before running any other commands."); - error = OT_ERROR_INVALID_STATE; - }); + ExitNow(error = OT_ERROR_INVALID_STATE); + } #endif command = Utils::LookupTable::Find(args[0].GetCString(), sCommands); if (command != nullptr) { - error = (this->*command->mHandler)(argsLength - 1, &args[1]); + error = (this->*command->mHandler)(args + 1); } else { - error = ProcessUserCommands(argsLength, args); + error = ProcessUserCommands(args); } exit: - if (error != OT_ERROR_NONE || argsLength > 0) + if ((error != OT_ERROR_NONE) || !args[0].IsEmpty()) { OutputResult(error); } } -otError Interpreter::ProcessUserCommands(uint8_t aArgsLength, Arg aArgs[]) +otError Interpreter::ProcessUserCommands(Arg aArgs[]) { otError error = OT_ERROR_INVALID_COMMAND; @@ -4677,8 +4556,8 @@ otError Interpreter::ProcessUserCommands(uint8_t aArgsLength, Arg aArgs[]) { char *args[kMaxArgs]; - Arg::CopyArgsToStringArray(aArgs, aArgsLength, args); - mUserCommands[i].mCommand(mUserCommandsContext, aArgsLength - 1, args + 1); + Arg::CopyArgsToStringArray(aArgs, args); + mUserCommands[i].mCommand(mUserCommandsContext, Arg::GetArgsLength(aArgs) - 1, args + 1); error = OT_ERROR_NONE; break; } @@ -4694,31 +4573,26 @@ void Interpreter::OutputPrefix(const otMeshLocalPrefix &aPrefix) } #if OPENTHREAD_FTD || OPENTHREAD_CONFIG_TMF_NETWORK_DIAG_MTD_ENABLE -otError Interpreter::ProcessNetworkDiagnostic(uint8_t aArgsLength, Arg aArgs[]) +otError Interpreter::ProcessNetworkDiagnostic(Arg aArgs[]) { otError error = OT_ERROR_NONE; otIp6Address address; uint8_t tlvTypes[OT_NETWORK_DIAGNOSTIC_TYPELIST_MAX_ENTRIES]; - uint8_t count = 0; - uint8_t argsIndex = 0; - - // Include operation, address and type tlv list. - VerifyOrExit(aArgsLength > 2, error = OT_ERROR_INVALID_ARGS); + uint8_t count = 0; SuccessOrExit(error = aArgs[1].ParseAsIp6Address(address)); - argsIndex = 2; - - while (argsIndex < aArgsLength && count < sizeof(tlvTypes)) + for (Arg *arg = &aArgs[2]; !arg->IsEmpty(); arg++) { - SuccessOrExit(error = aArgs[argsIndex++].ParseAsUint8(tlvTypes[count++])); + VerifyOrExit(count < sizeof(tlvTypes), error = OT_ERROR_INVALID_ARGS); + SuccessOrExit(error = arg->ParseAsUint8(tlvTypes[count++])); } if (aArgs[0] == "get") { SuccessOrExit(error = otThreadSendDiagnosticGet(mInstance, &address, tlvTypes, count, &Interpreter::HandleDiagnosticGetResponse, this)); - ExitNow(error = OT_ERROR_PENDING); + error = OT_ERROR_PENDING; } else if (aArgs[0] == "reset") { diff --git a/src/cli/cli.hpp b/src/cli/cli.hpp index 270764f73..2ae46ae5a 100644 --- a/src/cli/cli.hpp +++ b/src/cli/cli.hpp @@ -311,7 +311,7 @@ private: struct Command { const char *mName; - otError (Interpreter::*mHandler)(uint8_t aArgsLength, Arg aArgs[]); + otError (Interpreter::*mHandler)(Arg aArgs[]); }; template using GetHandler = ValueType (&)(otInstance *); @@ -321,7 +321,7 @@ private: // Returns format string to output a `ValueType` (e.g., "%u" for `uint16_t`). template static constexpr const char *FormatStringFor(void); - template otError ProcessGet(uint8_t aArgsLength, GetHandler aGetHandler) + template otError ProcessGet(Arg aArgs[], GetHandler aGetHandler) { static_assert( TypeTraits::IsSame::kValue || TypeTraits::IsSame::kValue || @@ -331,44 +331,35 @@ private: otError error = OT_ERROR_NONE; - VerifyOrExit(aArgsLength == 0, error = OT_ERROR_INVALID_ARGS); + VerifyOrExit(aArgs[0].IsEmpty(), error = OT_ERROR_INVALID_ARGS); OutputLine(FormatStringFor(), aGetHandler(mInstance)); exit: return error; } - template otError ParseValue(uint8_t aArgsLength, Arg aArgs[], ValueType &aValue) - { - otError error = OT_ERROR_INVALID_ARGS; - - VerifyOrExit(aArgsLength == 1); - error = aArgs[0].ParseAs(aValue); - - exit: - return error; - } - - template - otError ProcessSet(uint8_t aArgsLength, Arg aArgs[], SetHandler aSetHandler) + template otError ProcessSet(Arg aArgs[], SetHandler aSetHandler) { otError error; ValueType value; - SuccessOrExit(error = ParseValue(aArgsLength, aArgs, value)); + SuccessOrExit(error = aArgs[0].ParseAs(value)); + VerifyOrExit(aArgs[1].IsEmpty(), error = OT_ERROR_INVALID_ARGS); + aSetHandler(mInstance, value); exit: return error; } - template - otError ProcessSet(uint8_t aArgsLength, Arg aArgs[], SetHandlerFailable aSetHandler) + template otError ProcessSet(Arg aArgs[], SetHandlerFailable aSetHandler) { otError error; ValueType value; - SuccessOrExit(error = ParseValue(aArgsLength, aArgs, value)); + SuccessOrExit(error = aArgs[0].ParseAs(value)); + VerifyOrExit(aArgs[1].IsEmpty(), error = OT_ERROR_INVALID_ARGS); + error = aSetHandler(mInstance, value); exit: @@ -376,30 +367,24 @@ private: } template - otError ProcessGetSet(uint8_t aArgsLength, - Arg aArgs[], - GetHandler aGetHandler, - SetHandler aSetHandler) + otError ProcessGetSet(Arg aArgs[], GetHandler aGetHandler, SetHandler aSetHandler) { - otError error = ProcessGet(aArgsLength, aGetHandler); + otError error = ProcessGet(aArgs, aGetHandler); VerifyOrExit(error != OT_ERROR_NONE); - error = ProcessSet(aArgsLength, aArgs, aSetHandler); + error = ProcessSet(aArgs, aSetHandler); exit: return error; } template - otError ProcessGetSet(uint8_t aArgsLength, - Arg aArgs[], - GetHandler aGetHandler, - SetHandlerFailable aSetHandler) + otError ProcessGetSet(Arg aArgs[], GetHandler aGetHandler, SetHandlerFailable aSetHandler) { - otError error = ProcessGet(aArgsLength, aGetHandler); + otError error = ProcessGet(aArgs, aGetHandler); VerifyOrExit(error != OT_ERROR_NONE); - error = ProcessSet(aArgsLength, aArgs, aSetHandler); + error = ProcessSet(aArgs, aSetHandler); exit: return error; @@ -418,121 +403,120 @@ private: #endif static otError ParseJoinerDiscerner(Arg &aArg, otJoinerDiscerner &aDiscerner); - otError ProcessUserCommands(uint8_t aArgsLength, Arg aArgs[]); - otError ProcessHelp(uint8_t aArgsLength, Arg aArgs[]); - otError ProcessCcaThreshold(uint8_t aArgsLength, Arg aArgs[]); - otError ProcessBufferInfo(uint8_t aArgsLength, Arg aArgs[]); - otError ProcessChannel(uint8_t aArgsLength, Arg aArgs[]); + otError ProcessUserCommands(Arg aArgs[]); + otError ProcessHelp(Arg aArgs[]); + otError ProcessCcaThreshold(Arg aArgs[]); + otError ProcessBufferInfo(Arg aArgs[]); + otError ProcessChannel(Arg aArgs[]); #if OPENTHREAD_CONFIG_BORDER_AGENT_ENABLE - otError ProcessBorderAgent(uint8_t aArgsLength, Arg aArgs[]); + otError ProcessBorderAgent(Arg aArgs[]); #endif #if OPENTHREAD_CONFIG_BORDER_ROUTING_ENABLE - otError ProcessBorderRouting(uint8_t aArgsLength, Arg aArgs[]); + otError ProcessBorderRouting(Arg aArgs[]); #endif #if (OPENTHREAD_CONFIG_THREAD_VERSION >= OT_THREAD_VERSION_1_2) - otError ProcessBackboneRouter(uint8_t aArgsLength, Arg aArgs[]); + otError ProcessBackboneRouter(Arg aArgs[]); #if OPENTHREAD_FTD && OPENTHREAD_CONFIG_BACKBONE_ROUTER_ENABLE - otError ProcessBackboneRouterLocal(uint8_t aArgsLength, Arg aArgs[]); + otError ProcessBackboneRouterLocal(Arg aArgs[]); #if OPENTHREAD_CONFIG_BACKBONE_ROUTER_MULTICAST_ROUTING_ENABLE - otError ProcessBackboneRouterMgmtMlr(uint8_t aArgsLength, Arg aArgs[]); + otError ProcessBackboneRouterMgmtMlr(Arg aArgs[]); void PrintMulticastListenersTable(void); #endif #endif - otError ProcessDomainName(uint8_t aArgsLength, Arg aArgs[]); + otError ProcessDomainName(Arg aArgs[]); #if OPENTHREAD_CONFIG_DUA_ENABLE - otError ProcessDua(uint8_t aArgsLength, Arg aArgs[]); + otError ProcessDua(Arg aArgs[]); #endif #endif // (OPENTHREAD_CONFIG_THREAD_VERSION >= OT_THREAD_VERSION_1_2) #if OPENTHREAD_FTD - otError ProcessChild(uint8_t aArgsLength, Arg aArgs[]); - otError ProcessChildIp(uint8_t aArgsLength, Arg aArgs[]); - otError ProcessChildMax(uint8_t aArgsLength, Arg aArgs[]); + otError ProcessChild(Arg aArgs[]); + otError ProcessChildIp(Arg aArgs[]); + otError ProcessChildMax(Arg aArgs[]); #endif #if OPENTHREAD_CONFIG_CHILD_SUPERVISION_ENABLE - otError ProcessChildSupervision(uint8_t aArgsLength, Arg aArgs[]); + otError ProcessChildSupervision(Arg aArgs[]); #endif - otError ProcessChildTimeout(uint8_t aArgsLength, Arg aArgs[]); + otError ProcessChildTimeout(Arg aArgs[]); #if OPENTHREAD_CONFIG_COAP_API_ENABLE - otError ProcessCoap(uint8_t aArgsLength, Arg aArgs[]); + otError ProcessCoap(Arg aArgs[]); #endif #if OPENTHREAD_CONFIG_COAP_SECURE_API_ENABLE - otError ProcessCoapSecure(uint8_t aArgsLength, Arg aArgs[]); + otError ProcessCoapSecure(Arg aArgs[]); #endif #if OPENTHREAD_CONFIG_PLATFORM_RADIO_COEX_ENABLE - otError ProcessCoexMetrics(uint8_t aArgsLength, Arg aArgs[]); + otError ProcessCoexMetrics(Arg aArgs[]); #endif #if OPENTHREAD_CONFIG_COMMISSIONER_ENABLE && OPENTHREAD_FTD - otError ProcessCommissioner(uint8_t aArgsLength, Arg aArgs[]); + otError ProcessCommissioner(Arg aArgs[]); #endif #if OPENTHREAD_FTD - otError ProcessContextIdReuseDelay(uint8_t aArgsLength, Arg aArgs[]); + otError ProcessContextIdReuseDelay(Arg aArgs[]); #endif - otError ProcessCounters(uint8_t aArgsLength, Arg aArgs[]); - otError ProcessCsl(uint8_t aArgsLength, Arg aArgs[]); + otError ProcessCounters(Arg aArgs[]); + otError ProcessCsl(Arg aArgs[]); #if OPENTHREAD_FTD - otError ProcessDelayTimerMin(uint8_t aArgsLength, Arg aArgs[]); + otError ProcessDelayTimerMin(Arg aArgs[]); #endif #if OPENTHREAD_CONFIG_DIAG_ENABLE - otError ProcessDiag(uint8_t aArgsLength, Arg aArgs[]); + otError ProcessDiag(Arg aArgs[]); #endif - otError ProcessDiscover(uint8_t aArgsLength, Arg aArgs[]); - otError ProcessDns(uint8_t aArgsLength, Arg aArgs[]); + otError ProcessDiscover(Arg aArgs[]); + otError ProcessDns(Arg aArgs[]); #if OPENTHREAD_FTD void OutputEidCacheEntry(const otCacheEntryInfo &aEntry); - otError ProcessEidCache(uint8_t aArgsLength, Arg aArgs[]); + otError ProcessEidCache(Arg aArgs[]); #endif - otError ProcessEui64(uint8_t aArgsLength, Arg aArgs[]); + otError ProcessEui64(Arg aArgs[]); #if OPENTHREAD_POSIX && !defined(FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION) - otError ProcessExit(uint8_t aArgsLength, Arg aArgs[]); + otError ProcessExit(Arg aArgs[]); #endif - otError ProcessLog(uint8_t aArgsLength, Arg aArgs[]); - otError ProcessExtAddress(uint8_t aArgsLength, Arg aArgs[]); - otError ProcessExtPanId(uint8_t aArgsLength, Arg aArgs[]); - otError ProcessFactoryReset(uint8_t aArgsLength, Arg aArgs[]); + otError ProcessLog(Arg aArgs[]); + otError ProcessExtAddress(Arg aArgs[]); + otError ProcessExtPanId(Arg aArgs[]); + otError ProcessFactoryReset(Arg aArgs[]); #if OPENTHREAD_FTD && OPENTHREAD_CONFIG_REFERENCE_DEVICE_ENABLE - otError ProcessFake(uint8_t aArgsLength, Arg aArgs[]); + otError ProcessFake(Arg aArgs[]); #endif - otError ProcessFem(uint8_t aArgsLength, Arg aArgs[]); - otError ProcessIfconfig(uint8_t aArgsLength, Arg aArgs[]); - otError ProcessIpAddr(uint8_t aArgsLength, Arg aArgs[]); - otError ProcessIpAddrAdd(uint8_t aArgsLength, Arg aArgs[]); - otError ProcessIpAddrDel(uint8_t aArgsLength, Arg aArgs[]); - otError ProcessIpMulticastAddr(uint8_t aArgsLength, Arg aArgs[]); - otError ProcessIpMulticastAddrAdd(uint8_t aArgsLength, Arg aArgs[]); - otError ProcessIpMulticastAddrDel(uint8_t aArgsLength, Arg aArgs[]); - otError ProcessMulticastPromiscuous(uint8_t aArgsLength, Arg aArgs[]); + otError ProcessFem(Arg aArgs[]); + otError ProcessIfconfig(Arg aArgs[]); + otError ProcessIpAddr(Arg aArgs[]); + otError ProcessIpAddrAdd(Arg aArgs[]); + otError ProcessIpAddrDel(Arg aArgs[]); + otError ProcessIpMulticastAddr(Arg aArgs[]); + otError ProcessIpMulticastAddrAdd(Arg aArgs[]); + otError ProcessIpMulticastAddrDel(Arg aArgs[]); + otError ProcessMulticastPromiscuous(Arg aArgs[]); #if OPENTHREAD_CONFIG_JOINER_ENABLE - otError ProcessJoiner(uint8_t aArgsLength, Arg aArgs[]); + otError ProcessJoiner(Arg aArgs[]); #endif #if OPENTHREAD_FTD - otError ProcessJoinerPort(uint8_t aArgsLength, Arg aArgs[]); + otError ProcessJoinerPort(Arg aArgs[]); #endif - otError ProcessKeySequence(uint8_t aArgsLength, Arg aArgs[]); - otError ProcessLeaderData(uint8_t aArgsLength, Arg aArgs[]); + otError ProcessKeySequence(Arg aArgs[]); + otError ProcessLeaderData(Arg aArgs[]); #if OPENTHREAD_FTD - otError ProcessPartitionId(uint8_t aArgsLength, Arg aArgs[]); - otError ProcessLeaderWeight(uint8_t aArgsLength, Arg aArgs[]); + otError ProcessPartitionId(Arg aArgs[]); + otError ProcessLeaderWeight(Arg aArgs[]); #endif #if OPENTHREAD_CONFIG_REFERENCE_DEVICE_ENABLE - otError ProcessMlIid(uint8_t aArgsLength, Arg aArgs[]); + otError ProcessMlIid(Arg aArgs[]); #endif #if OPENTHREAD_CONFIG_MLE_LINK_METRICS_INITIATOR_ENABLE - otError ProcessLinkMetrics(uint8_t aArgsLength, Arg aArgs[]); - otError ProcessLinkMetricsQuery(uint8_t aArgsLength, Arg aArgs[]); - otError ProcessLinkMetricsMgmt(uint8_t aArgsLength, Arg aArgs[]); - otError ProcessLinkMetricsProbe(uint8_t aArgsLength, Arg aArgs[]); - + otError ProcessLinkMetrics(Arg aArgs[]); + otError ProcessLinkMetricsQuery(Arg aArgs[]); + otError ProcessLinkMetricsMgmt(Arg aArgs[]); + otError ProcessLinkMetricsProbe(Arg aArgs[]); otError ParseLinkMetricsFlags(otLinkMetrics &aLinkMetrics, const Arg &aFlags); #endif #if OPENTHREAD_FTD && OPENTHREAD_CONFIG_TMF_PROXY_MLR_ENABLE && OPENTHREAD_CONFIG_COMMISSIONER_ENABLE - otError ProcessMlr(uint8_t aArgsLength, Arg aArgs[]); + otError ProcessMlr(Arg aArgs[]); - otError ProcessMlrReg(uint8_t aArgsLength, Arg aArgs[]); + otError ProcessMlrReg(Arg aArgs[]); static void HandleMlrRegResult(void * aContext, otError aError, @@ -544,101 +528,101 @@ private: const otIp6Address *aFailedAddresses, uint8_t aFailedAddressNum); #endif - otError ProcessMode(uint8_t aArgsLength, Arg aArgs[]); - otError ProcessMultiRadio(uint8_t aArgsLength, Arg aArgs[]); + otError ProcessMode(Arg aArgs[]); + otError ProcessMultiRadio(Arg aArgs[]); #if OPENTHREAD_CONFIG_MULTI_RADIO void OutputMultiRadioInfo(const otMultiRadioNeighborInfo &aMultiRadioInfo); #endif #if OPENTHREAD_FTD - otError ProcessNeighbor(uint8_t aArgsLength, Arg aArgs[]); + otError ProcessNeighbor(Arg aArgs[]); #endif - otError ProcessNetworkData(uint8_t aArgsLength, Arg aArgs[]); + otError ProcessNetworkData(Arg aArgs[]); otError ProcessNetworkDataPrefix(void); otError ProcessNetworkDataRoute(void); otError ProcessNetworkDataService(void); void OutputPrefix(const otMeshLocalPrefix &aPrefix); - otError ProcessNetstat(uint8_t aArgsLength, Arg aArgs[]); + otError ProcessNetstat(Arg aArgs[]); #if OPENTHREAD_CONFIG_TMF_NETDATA_SERVICE_ENABLE - otError ProcessService(uint8_t aArgsLength, Arg aArgs[]); + otError ProcessService(Arg aArgs[]); otError ProcessServiceList(void); #endif #if OPENTHREAD_FTD || OPENTHREAD_CONFIG_TMF_NETWORK_DIAG_MTD_ENABLE - otError ProcessNetworkDiagnostic(uint8_t aArgsLength, Arg aArgs[]); + otError ProcessNetworkDiagnostic(Arg aArgs[]); #endif #if OPENTHREAD_FTD - otError ProcessNetworkIdTimeout(uint8_t aArgsLength, Arg aArgs[]); + otError ProcessNetworkIdTimeout(Arg aArgs[]); #endif - otError ProcessNetworkKey(uint8_t aArgsLength, Arg aArgs[]); - otError ProcessNetworkName(uint8_t aArgsLength, Arg aArgs[]); + otError ProcessNetworkKey(Arg aArgs[]); + otError ProcessNetworkName(Arg aArgs[]); #if OPENTHREAD_CONFIG_TIME_SYNC_ENABLE - otError ProcessNetworkTime(uint8_t aArgsLength, Arg aArgs[]); + otError ProcessNetworkTime(Arg aArgs[]); #endif - otError ProcessPanId(uint8_t aArgsLength, Arg aArgs[]); - otError ProcessParent(uint8_t aArgsLength, Arg aArgs[]); + otError ProcessPanId(Arg aArgs[]); + otError ProcessParent(Arg aArgs[]); #if OPENTHREAD_FTD - otError ProcessParentPriority(uint8_t aArgsLength, Arg aArgs[]); + otError ProcessParentPriority(Arg aArgs[]); #endif #if OPENTHREAD_CONFIG_PING_SENDER_ENABLE - otError ProcessPing(uint8_t aArgsLength, Arg aArgs[]); + otError ProcessPing(Arg aArgs[]); #endif - otError ProcessPollPeriod(uint8_t aArgsLength, Arg aArgs[]); + otError ProcessPollPeriod(Arg aArgs[]); #if OPENTHREAD_CONFIG_BORDER_ROUTER_ENABLE - otError ProcessPrefix(uint8_t aArgsLength, Arg aArgs[]); - otError ProcessPrefixAdd(uint8_t aArgsLength, Arg aArgs[]); - otError ProcessPrefixRemove(uint8_t aArgsLength, Arg aArgs[]); + otError ProcessPrefix(Arg aArgs[]); + otError ProcessPrefixAdd(Arg aArgs[]); + otError ProcessPrefixRemove(Arg aArgs[]); otError ProcessPrefixList(void); #endif - otError ProcessPromiscuous(uint8_t aArgsLength, Arg aArgs[]); + otError ProcessPromiscuous(Arg aArgs[]); #if OPENTHREAD_FTD - otError ProcessPreferRouterId(uint8_t aArgsLength, Arg aArgs[]); - otError ProcessPskc(uint8_t aArgsLength, Arg aArgs[]); + otError ProcessPreferRouterId(Arg aArgs[]); + otError ProcessPskc(Arg aArgs[]); #endif - otError ProcessRcp(uint8_t aArgsLength, Arg aArgs[]); - otError ProcessRegion(uint8_t aArgsLength, Arg aArgs[]); + otError ProcessRcp(Arg aArgs[]); + otError ProcessRegion(Arg aArgs[]); #if OPENTHREAD_FTD - otError ProcessReleaseRouterId(uint8_t aArgsLength, Arg aArgs[]); + otError ProcessReleaseRouterId(Arg aArgs[]); #endif - otError ProcessReset(uint8_t aArgsLength, Arg aArgs[]); + otError ProcessReset(Arg aArgs[]); #if OPENTHREAD_CONFIG_BORDER_ROUTER_ENABLE - otError ProcessRoute(uint8_t aArgsLength, Arg aArgs[]); - otError ProcessRouteAdd(uint8_t aArgsLength, Arg aArgs[]); - otError ProcessRouteRemove(uint8_t aArgsLength, Arg aArgs[]); + otError ProcessRoute(Arg aArgs[]); + otError ProcessRouteAdd(Arg aArgs[]); + otError ProcessRouteRemove(Arg aArgs[]); otError ProcessRouteList(void); #endif #if OPENTHREAD_FTD - otError ProcessRouter(uint8_t aArgsLength, Arg aArgs[]); - otError ProcessRouterDowngradeThreshold(uint8_t aArgsLength, Arg aArgs[]); - otError ProcessRouterEligible(uint8_t aArgsLength, Arg aArgs[]); - otError ProcessRouterSelectionJitter(uint8_t aArgsLength, Arg aArgs[]); - otError ProcessRouterUpgradeThreshold(uint8_t aArgsLength, Arg aArgs[]); + otError ProcessRouter(Arg aArgs[]); + otError ProcessRouterDowngradeThreshold(Arg aArgs[]); + otError ProcessRouterEligible(Arg aArgs[]); + otError ProcessRouterSelectionJitter(Arg aArgs[]); + otError ProcessRouterUpgradeThreshold(Arg aArgs[]); #endif - otError ProcessRloc16(uint8_t aArgsLength, Arg aArgs[]); - otError ProcessScan(uint8_t aArgsLength, Arg aArgs[]); - otError ProcessSingleton(uint8_t aArgsLength, Arg aArgs[]); + otError ProcessRloc16(Arg aArgs[]); + otError ProcessScan(Arg aArgs[]); + otError ProcessSingleton(Arg aArgs[]); #if OPENTHREAD_CONFIG_SNTP_CLIENT_ENABLE - otError ProcessSntp(uint8_t aArgsLength, Arg aArgs[]); + otError ProcessSntp(Arg aArgs[]); #endif #if OPENTHREAD_CONFIG_SRP_CLIENT_ENABLE || OPENTHREAD_CONFIG_SRP_SERVER_ENABLE - otError ProcessSrp(uint8_t aArgsLength, Arg aArgs[]); + otError ProcessSrp(Arg aArgs[]); #endif - otError ProcessState(uint8_t aArgsLength, Arg aArgs[]); - otError ProcessThread(uint8_t aArgsLength, Arg aArgs[]); - otError ProcessDataset(uint8_t aArgsLength, Arg aArgs[]); - otError ProcessTxPower(uint8_t aArgsLength, Arg aArgs[]); - otError ProcessUdp(uint8_t aArgsLength, Arg aArgs[]); - otError ProcessUnsecurePort(uint8_t aArgsLength, Arg aArgs[]); - otError ProcessVersion(uint8_t aArgsLength, Arg aArgs[]); + otError ProcessState(Arg aArgs[]); + otError ProcessThread(Arg aArgs[]); + otError ProcessDataset(Arg aArgs[]); + otError ProcessTxPower(Arg aArgs[]); + otError ProcessUdp(Arg aArgs[]); + otError ProcessUnsecurePort(Arg aArgs[]); + otError ProcessVersion(Arg aArgs[]); #if OPENTHREAD_CONFIG_MAC_FILTER_ENABLE - otError ProcessMacFilter(uint8_t aArgsLength, Arg aArgs[]); + otError ProcessMacFilter(Arg aArgs[]); void PrintMacFilter(void); - otError ProcessMacFilterAddress(uint8_t aArgsLength, Arg aArgs[]); - otError ProcessMacFilterRss(uint8_t aArgsLength, Arg aArgs[]); + otError ProcessMacFilterAddress(Arg aArgs[]); + otError ProcessMacFilterRss(Arg aArgs[]); #endif - otError ProcessMac(uint8_t aArgsLength, Arg aArgs[]); - otError ProcessMacRetries(uint8_t aArgsLength, Arg aArgs[]); + otError ProcessMac(Arg aArgs[]); + otError ProcessMacRetries(Arg aArgs[]); #if OPENTHREAD_CONFIG_REFERENCE_DEVICE_ENABLE - otError ProcessMacSend(uint8_t aArgsLength, Arg aArgs[]); + otError ProcessMacSend(Arg aArgs[]); #endif #if OPENTHREAD_CONFIG_PING_SENDER_ENABLE @@ -669,7 +653,7 @@ private: void OutputDnsTxtData(const uint8_t *aTxtData, uint16_t aTxtDataLength); #if OPENTHREAD_CONFIG_DNS_CLIENT_ENABLE - otError GetDnsConfig(uint8_t aArgsLength, Arg aArgs[], otDnsQueryConfig *&aConfig, uint8_t aStartArgsIndex); + otError GetDnsConfig(Arg aArgs[], otDnsQueryConfig *&aConfig); static void HandleDnsAddressResponse(otError aError, const otDnsAddressResponse *aResponse, void *aContext); void HandleDnsAddressResponse(otError aError, const otDnsAddressResponse *aResponse); #if OPENTHREAD_CONFIG_DNS_CLIENT_SERVICE_DISCOVERY_ENABLE diff --git a/src/cli/cli_coap.cpp b/src/cli/cli_coap.cpp index c25f837e4..8ed09274c 100644 --- a/src/cli/cli_coap.cpp +++ b/src/cli/cli_coap.cpp @@ -145,18 +145,16 @@ void Coap::PrintPayload(otMessage *aMessage) const } #if OPENTHREAD_CONFIG_COAP_OBSERVE_API_ENABLE -otError Coap::ProcessCancel(uint8_t aArgsLength, Arg aArgs[]) +otError Coap::ProcessCancel(Arg aArgs[]) { - OT_UNUSED_VARIABLE(aArgsLength); OT_UNUSED_VARIABLE(aArgs); return CancelResourceSubscription(); } #endif -otError Coap::ProcessHelp(uint8_t aArgsLength, Arg aArgs[]) +otError Coap::ProcessHelp(Arg aArgs[]) { - OT_UNUSED_VARIABLE(aArgsLength); OT_UNUSED_VARIABLE(aArgs); for (const Command &command : sCommands) @@ -167,11 +165,11 @@ otError Coap::ProcessHelp(uint8_t aArgsLength, Arg aArgs[]) return OT_ERROR_NONE; } -otError Coap::ProcessResource(uint8_t aArgsLength, Arg aArgs[]) +otError Coap::ProcessResource(Arg aArgs[]) { otError error = OT_ERROR_NONE; - if (aArgsLength > 0) + if (!aArgs[0].IsEmpty()) { VerifyOrExit(aArgs[0].GetLength() < kMaxUriLength, error = OT_ERROR_INVALID_ARGS); @@ -183,7 +181,7 @@ otError Coap::ProcessResource(uint8_t aArgsLength, Arg aArgs[]) mResource.mReceiveHook = &Coap::BlockwiseReceiveHook; mResource.mTransmitHook = &Coap::BlockwiseTransmitHook; - if (aArgsLength > 1) + if (!aArgs[1].IsEmpty()) { SuccessOrExit(error = aArgs[1].ParseAsUint32(mBlockCount)); } @@ -206,7 +204,7 @@ exit: return error; } -otError Coap::ProcessSet(uint8_t aArgsLength, Arg aArgs[]) +otError Coap::ProcessSet(Arg aArgs[]) { #if OPENTHREAD_CONFIG_COAP_OBSERVE_API_ENABLE otMessage * notificationMessage = nullptr; @@ -214,7 +212,7 @@ otError Coap::ProcessSet(uint8_t aArgsLength, Arg aArgs[]) #endif otError error = OT_ERROR_NONE; - if (aArgsLength > 0) + if (!aArgs[0].IsEmpty()) { VerifyOrExit(aArgs[0].GetLength() < sizeof(mResourceContent), error = OT_ERROR_INVALID_ARGS); strncpy(mResourceContent, aArgs[0].GetCString(), sizeof(mResourceContent)); @@ -268,17 +266,15 @@ exit: return error; } -otError Coap::ProcessStart(uint8_t aArgsLength, Arg aArgs[]) +otError Coap::ProcessStart(Arg aArgs[]) { - OT_UNUSED_VARIABLE(aArgsLength); OT_UNUSED_VARIABLE(aArgs); return otCoapStart(mInterpreter.mInstance, OT_DEFAULT_COAP_PORT); } -otError Coap::ProcessStop(uint8_t aArgsLength, Arg aArgs[]) +otError Coap::ProcessStop(Arg aArgs[]) { - OT_UNUSED_VARIABLE(aArgsLength); OT_UNUSED_VARIABLE(aArgs); #if OPENTHREAD_CONFIG_COAP_BLOCKWISE_TRANSFER_ENABLE @@ -290,14 +286,12 @@ otError Coap::ProcessStop(uint8_t aArgsLength, Arg aArgs[]) return otCoapStop(mInterpreter.mInstance); } -otError Coap::ProcessParameters(uint8_t aArgsLength, Arg aArgs[]) +otError Coap::ProcessParameters(Arg aArgs[]) { otError error = OT_ERROR_NONE; bool * defaultTxParameters; otCoapTxParameters *txParameters; - VerifyOrExit(aArgsLength > 0, error = OT_ERROR_INVALID_ARGS); - if (aArgs[0] == "request") { txParameters = &mRequestTxParameters; @@ -313,7 +307,7 @@ otError Coap::ProcessParameters(uint8_t aArgsLength, Arg aArgs[]) ExitNow(error = OT_ERROR_INVALID_ARGS); } - if (aArgsLength > 1) + if (!aArgs[1].IsEmpty()) { if (aArgs[1] == "default") { @@ -321,8 +315,6 @@ otError Coap::ProcessParameters(uint8_t aArgsLength, Arg aArgs[]) } else { - VerifyOrExit(aArgsLength >= 5, error = OT_ERROR_INVALID_ARGS); - SuccessOrExit(error = aArgs[1].ParseAsUint32(txParameters->mAckTimeout)); SuccessOrExit(error = aArgs[2].ParseAsUint8(txParameters->mAckRandomFactorNumerator)); SuccessOrExit(error = aArgs[3].ParseAsUint8(txParameters->mAckRandomFactorDenominator)); @@ -352,37 +344,37 @@ exit: return error; } -otError Coap::ProcessGet(uint8_t aArgsLength, Arg aArgs[]) +otError Coap::ProcessGet(Arg aArgs[]) { - return ProcessRequest(aArgsLength, aArgs, OT_COAP_CODE_GET); + return ProcessRequest(aArgs, OT_COAP_CODE_GET); } -otError Coap::ProcessPost(uint8_t aArgsLength, Arg aArgs[]) +otError Coap::ProcessPost(Arg aArgs[]) { - return ProcessRequest(aArgsLength, aArgs, OT_COAP_CODE_POST); + return ProcessRequest(aArgs, OT_COAP_CODE_POST); } -otError Coap::ProcessPut(uint8_t aArgsLength, Arg aArgs[]) +otError Coap::ProcessPut(Arg aArgs[]) { - return ProcessRequest(aArgsLength, aArgs, OT_COAP_CODE_PUT); + return ProcessRequest(aArgs, OT_COAP_CODE_PUT); } -otError Coap::ProcessDelete(uint8_t aArgsLength, Arg aArgs[]) +otError Coap::ProcessDelete(Arg aArgs[]) { - return ProcessRequest(aArgsLength, aArgs, OT_COAP_CODE_DELETE); + return ProcessRequest(aArgs, OT_COAP_CODE_DELETE); } #if OPENTHREAD_CONFIG_COAP_OBSERVE_API_ENABLE -otError Coap::ProcessObserve(uint8_t aArgsLength, Arg aArgs[]) +otError Coap::ProcessObserve(Arg aArgs[]) { - return ProcessRequest(aArgsLength, aArgs, OT_COAP_CODE_GET, /* aCoapObserve */ true); + return ProcessRequest(aArgs, OT_COAP_CODE_GET, /* aCoapObserve */ true); } #endif #if OPENTHREAD_CONFIG_COAP_OBSERVE_API_ENABLE -otError Coap::ProcessRequest(uint8_t aArgsLength, Arg aArgs[], otCoapCode aCoapCode, bool aCoapObserve) +otError Coap::ProcessRequest(Arg aArgs[], otCoapCode aCoapCode, bool aCoapObserve) #else -otError Coap::ProcessRequest(uint8_t aArgsLength, Arg aArgs[], otCoapCode aCoapCode) +otError Coap::ProcessRequest(Arg aArgs[], otCoapCode aCoapCode) #endif { otError error = OT_ERROR_NONE; @@ -407,15 +399,14 @@ otError Coap::ProcessRequest(uint8_t aArgsLength, Arg aArgs[], otCoapCode aCoapC } #endif - VerifyOrExit(aArgsLength > 1, error = OT_ERROR_INVALID_ARGS); - SuccessOrExit(error = aArgs[0].ParseAsIp6Address(coapDestinationIp)); + VerifyOrExit(!aArgs[1].IsEmpty(), error = OT_ERROR_INVALID_ARGS); VerifyOrExit(aArgs[1].GetLength() < kMaxUriLength, error = OT_ERROR_INVALID_ARGS); strncpy(coapUri, aArgs[1].GetCString(), sizeof(coapUri) - 1); // CoAP-Type - if (aArgsLength > 2) + if (!aArgs[2].IsEmpty()) { if (aArgs[2] == "con") { @@ -504,7 +495,7 @@ otError Coap::ProcessRequest(uint8_t aArgsLength, Arg aArgs[], otCoapCode aCoapC } #endif - if (aArgsLength > 3) + if (!aArgs[3].IsEmpty()) { #if OPENTHREAD_CONFIG_COAP_BLOCKWISE_TRANSFER_ENABLE if (coapBlock) @@ -585,17 +576,21 @@ exit: return error; } -otError Coap::Process(uint8_t aArgsLength, Arg aArgs[]) +otError Coap::Process(Arg aArgs[]) { otError error = OT_ERROR_INVALID_ARGS; const Command *command; - VerifyOrExit(aArgsLength != 0, IgnoreError(ProcessHelp(0, nullptr))); + if (aArgs[0].IsEmpty()) + { + IgnoreError(ProcessHelp(aArgs)); + ExitNow(); + } command = Utils::LookupTable::Find(aArgs[0].GetCString(), sCommands); VerifyOrExit(command != nullptr, error = OT_ERROR_INVALID_COMMAND); - error = (this->*command->mHandler)(aArgsLength - 1, aArgs + 1); + error = (this->*command->mHandler)(aArgs + 1); exit: return error; diff --git a/src/cli/cli_coap.hpp b/src/cli/cli_coap.hpp index 79a8750b9..2723910fd 100644 --- a/src/cli/cli_coap.hpp +++ b/src/cli/cli_coap.hpp @@ -68,11 +68,10 @@ public: /** * This method interprets a list of CLI arguments. * - * @param[in] aArgsLength The number of elements in @p aArgs. * @param[in] aArgs An array of command line arguments. * */ - otError Process(uint8_t aArgsLength, Arg aArgs[]); + otError Process(Arg aArgs[]); private: enum @@ -84,7 +83,7 @@ private: struct Command { const char *mName; - otError (Coap::*mHandler)(uint8_t aArgsLength, Arg aArgs[]); + otError (Coap::*mHandler)(Arg aArgs[]); }; #if OPENTHREAD_CONFIG_COAP_BLOCKWISE_TRANSFER_ENABLE @@ -102,27 +101,27 @@ private: void PrintPayload(otMessage *aMessage) const; - otError ProcessHelp(uint8_t aArgsLength, Arg aArgs[]); + otError ProcessHelp(Arg aArgs[]); #if OPENTHREAD_CONFIG_COAP_OBSERVE_API_ENABLE - otError ProcessCancel(uint8_t aArgsLength, Arg aArgs[]); + otError ProcessCancel(Arg aArgs[]); #endif - otError ProcessDelete(uint8_t aArgsLength, Arg aArgs[]); - otError ProcessGet(uint8_t aArgsLength, Arg aArgs[]); + otError ProcessDelete(Arg aArgs[]); + otError ProcessGet(Arg aArgs[]); #if OPENTHREAD_CONFIG_COAP_OBSERVE_API_ENABLE - otError ProcessObserve(uint8_t aArgsLength, Arg aArgs[]); + otError ProcessObserve(Arg aArgs[]); #endif - otError ProcessParameters(uint8_t aArgsLength, Arg aArgs[]); - otError ProcessPost(uint8_t aArgsLength, Arg aArgs[]); - otError ProcessPut(uint8_t aArgsLength, Arg aArgs[]); - otError ProcessResource(uint8_t aArgsLength, Arg aArgs[]); - otError ProcessSet(uint8_t aArgsLength, Arg aArgs[]); - otError ProcessStart(uint8_t aArgsLength, Arg aArgs[]); - otError ProcessStop(uint8_t aArgsLength, Arg aArgs[]); + otError ProcessParameters(Arg aArgs[]); + otError ProcessPost(Arg aArgs[]); + otError ProcessPut(Arg aArgs[]); + otError ProcessResource(Arg aArgs[]); + otError ProcessSet(Arg aArgs[]); + otError ProcessStart(Arg aArgs[]); + otError ProcessStop(Arg aArgs[]); #if OPENTHREAD_CONFIG_COAP_OBSERVE_API_ENABLE - otError ProcessRequest(uint8_t aArgsLength, Arg aArgs[], otCoapCode aCoapCode, bool aCoapObserve = false); + otError ProcessRequest(Arg aArgs[], otCoapCode aCoapCode, bool aCoapObserve = false); #else - otError ProcessRequest(uint8_t aArgsLength, Arg aArgs[], otCoapCode aCoapCode); + otError ProcessRequest(Arg aArgs[], otCoapCode aCoapCode); #endif static void HandleRequest(void *aContext, otMessage *aMessage, const otMessageInfo *aMessageInfo); diff --git a/src/cli/cli_coap_secure.cpp b/src/cli/cli_coap_secure.cpp index de1ccaec1..2b2505a42 100644 --- a/src/cli/cli_coap_secure.cpp +++ b/src/cli/cli_coap_secure.cpp @@ -91,9 +91,8 @@ void CoapSecure::PrintPayload(otMessage *aMessage) const mInterpreter.OutputLine(""); } -otError CoapSecure::ProcessHelp(uint8_t aArgsLength, Arg aArgs[]) +otError CoapSecure::ProcessHelp(Arg aArgs[]) { - OT_UNUSED_VARIABLE(aArgsLength); OT_UNUSED_VARIABLE(aArgs); for (const Command &command : sCommands) @@ -104,11 +103,11 @@ otError CoapSecure::ProcessHelp(uint8_t aArgsLength, Arg aArgs[]) return OT_ERROR_NONE; } -otError CoapSecure::ProcessResource(uint8_t aArgsLength, Arg aArgs[]) +otError CoapSecure::ProcessResource(Arg aArgs[]) { otError error = OT_ERROR_NONE; - if (aArgsLength > 0) + if (!aArgs[0].IsEmpty()) { VerifyOrExit(aArgs[0].GetLength() < kMaxUriLength, error = OT_ERROR_INVALID_ARGS); @@ -120,7 +119,7 @@ otError CoapSecure::ProcessResource(uint8_t aArgsLength, Arg aArgs[]) mResource.mReceiveHook = &CoapSecure::BlockwiseReceiveHook; mResource.mTransmitHook = &CoapSecure::BlockwiseTransmitHook; - if (aArgsLength > 1) + if (!aArgs[1].IsEmpty()) { SuccessOrExit(error = aArgs[1].ParseAsUint32(mBlockCount)); } @@ -142,11 +141,11 @@ exit: return error; } -otError CoapSecure::ProcessSet(uint8_t aArgsLength, Arg aArgs[]) +otError CoapSecure::ProcessSet(Arg aArgs[]) { otError error = OT_ERROR_NONE; - if (aArgsLength > 0) + if (!aArgs[0].IsEmpty()) { VerifyOrExit(aArgs[0].GetLength() < sizeof(mResourceContent), error = OT_ERROR_INVALID_ARGS); strncpy(mResourceContent, aArgs[0].GetCString(), sizeof(mResourceContent)); @@ -161,12 +160,12 @@ exit: return error; } -otError CoapSecure::ProcessStart(uint8_t aArgsLength, Arg aArgs[]) +otError CoapSecure::ProcessStart(Arg aArgs[]) { - otError error; + otError error = OT_ERROR_NONE; bool verifyPeerCert = true; - if (aArgsLength > 0) + if (!aArgs[0].IsEmpty()) { if (aArgs[0] == "false") { @@ -185,15 +184,14 @@ otError CoapSecure::ProcessStart(uint8_t aArgsLength, Arg aArgs[]) otCoapSecureSetDefaultHandler(mInterpreter.mInstance, &CoapSecure::DefaultHandler, this); #endif - SuccessOrExit(error = otCoapSecureStart(mInterpreter.mInstance, OT_DEFAULT_COAP_SECURE_PORT)); + error = otCoapSecureStart(mInterpreter.mInstance, OT_DEFAULT_COAP_SECURE_PORT); exit: return error; } -otError CoapSecure::ProcessStop(uint8_t aArgsLength, Arg aArgs[]) +otError CoapSecure::ProcessStop(Arg aArgs[]) { - OT_UNUSED_VARIABLE(aArgsLength); OT_UNUSED_VARIABLE(aArgs); #if OPENTHREAD_CONFIG_COAP_BLOCKWISE_TRANSFER_ENABLE @@ -215,27 +213,27 @@ otError CoapSecure::ProcessStop(uint8_t aArgsLength, Arg aArgs[]) return OT_ERROR_NONE; } -otError CoapSecure::ProcessGet(uint8_t aArgsLength, Arg aArgs[]) +otError CoapSecure::ProcessGet(Arg aArgs[]) { - return ProcessRequest(aArgsLength, aArgs, OT_COAP_CODE_GET); + return ProcessRequest(aArgs, OT_COAP_CODE_GET); } -otError CoapSecure::ProcessPost(uint8_t aArgsLength, Arg aArgs[]) +otError CoapSecure::ProcessPost(Arg aArgs[]) { - return ProcessRequest(aArgsLength, aArgs, OT_COAP_CODE_POST); + return ProcessRequest(aArgs, OT_COAP_CODE_POST); } -otError CoapSecure::ProcessPut(uint8_t aArgsLength, Arg aArgs[]) +otError CoapSecure::ProcessPut(Arg aArgs[]) { - return ProcessRequest(aArgsLength, aArgs, OT_COAP_CODE_PUT); + return ProcessRequest(aArgs, OT_COAP_CODE_PUT); } -otError CoapSecure::ProcessDelete(uint8_t aArgsLength, Arg aArgs[]) +otError CoapSecure::ProcessDelete(Arg aArgs[]) { - return ProcessRequest(aArgsLength, aArgs, OT_COAP_CODE_DELETE); + return ProcessRequest(aArgs, OT_COAP_CODE_DELETE); } -otError CoapSecure::ProcessRequest(uint8_t aArgsLength, Arg aArgs[], otCoapCode aCoapCode) +otError CoapSecure::ProcessRequest(Arg aArgs[], otCoapCode aCoapCode) { otError error = OT_ERROR_NONE; otMessage *message = nullptr; @@ -250,12 +248,12 @@ otError CoapSecure::ProcessRequest(uint8_t aArgsLength, Arg aArgs[], otCoapCode BlockType coapBlockType = (aCoapCode == OT_COAP_CODE_GET) ? kBlockType2 : kBlockType1; #endif - if (aArgsLength > 0) + if (!aArgs[0].IsEmpty()) { strncpy(coapUri, aArgs[0].GetCString(), sizeof(coapUri) - 1); } - if (aArgsLength > 1) + if (!aArgs[1].IsEmpty()) { if (aArgs[1] == "con") { @@ -328,7 +326,7 @@ otError CoapSecure::ProcessRequest(uint8_t aArgsLength, Arg aArgs[], otCoapCode } #endif - if (aArgsLength > 2) + if (!aArgs[2].IsEmpty()) { #if OPENTHREAD_CONFIG_COAP_BLOCKWISE_TRANSFER_ENABLE if (coapBlock) @@ -386,18 +384,16 @@ exit: return error; } -otError CoapSecure::ProcessConnect(uint8_t aArgsLength, Arg aArgs[]) +otError CoapSecure::ProcessConnect(Arg aArgs[]) { otError error; otSockAddr sockaddr; - VerifyOrExit(aArgsLength > 0, error = OT_ERROR_INVALID_ARGS); - memset(&sockaddr, 0, sizeof(sockaddr)); SuccessOrExit(error = aArgs[0].ParseAsIp6Address(sockaddr.mAddress)); sockaddr.mPort = OT_DEFAULT_COAP_SECURE_PORT; - if (aArgsLength > 1) + if (!aArgs[1].IsEmpty()) { SuccessOrExit(error = aArgs[1].ParseAsUint16(sockaddr.mPort)); } @@ -408,9 +404,8 @@ exit: return error; } -otError CoapSecure::ProcessDisconnect(uint8_t aArgsLength, Arg aArgs[]) +otError CoapSecure::ProcessDisconnect(Arg aArgs[]) { - OT_UNUSED_VARIABLE(aArgsLength); OT_UNUSED_VARIABLE(aArgs); otCoapSecureDisconnect(mInterpreter.mInstance); @@ -419,12 +414,12 @@ otError CoapSecure::ProcessDisconnect(uint8_t aArgsLength, Arg aArgs[]) } #ifdef MBEDTLS_KEY_EXCHANGE_PSK_ENABLED -otError CoapSecure::ProcessPsk(uint8_t aArgsLength, Arg aArgs[]) +otError CoapSecure::ProcessPsk(Arg aArgs[]) { otError error = OT_ERROR_NONE; uint16_t length; - VerifyOrExit(aArgsLength > 1, error = OT_ERROR_INVALID_ARGS); + VerifyOrExit(!aArgs[1].IsEmpty(), error = OT_ERROR_INVALID_ARGS); length = aArgs[0].GetLength(); VerifyOrExit(length <= sizeof(mPsk), error = OT_ERROR_INVALID_ARGS); @@ -445,9 +440,8 @@ exit: #endif // MBEDTLS_KEY_EXCHANGE_PSK_ENABLED #ifdef MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED -otError CoapSecure::ProcessX509(uint8_t aArgsLength, Arg aArgs[]) +otError CoapSecure::ProcessX509(Arg aArgs[]) { - OT_UNUSED_VARIABLE(aArgsLength); OT_UNUSED_VARIABLE(aArgs); otCoapSecureSetCertificate(mInterpreter.mInstance, reinterpret_cast(OT_CLI_COAPS_X509_CERT), @@ -463,17 +457,21 @@ otError CoapSecure::ProcessX509(uint8_t aArgsLength, Arg aArgs[]) } #endif -otError CoapSecure::Process(uint8_t aArgsLength, Arg aArgs[]) +otError CoapSecure::Process(Arg aArgs[]) { otError error = OT_ERROR_INVALID_ARGS; const Command *command; - VerifyOrExit(aArgsLength != 0, IgnoreError(ProcessHelp(0, nullptr))); + if (aArgs[0].IsEmpty()) + { + IgnoreError(ProcessHelp(aArgs)); + ExitNow(); + } command = Utils::LookupTable::Find(aArgs[0].GetCString(), sCommands); VerifyOrExit(command != nullptr, error = OT_ERROR_INVALID_COMMAND); - error = (this->*command->mHandler)(aArgsLength - 1, aArgs + 1); + error = (this->*command->mHandler)(aArgs + 1); exit: return error; diff --git a/src/cli/cli_coap_secure.hpp b/src/cli/cli_coap_secure.hpp index 1a197ff95..b8480d90e 100644 --- a/src/cli/cli_coap_secure.hpp +++ b/src/cli/cli_coap_secure.hpp @@ -74,11 +74,10 @@ public: /** * This method interprets a list of CLI arguments. * - * @param[in] aArgsLength The number of elements in @p aArgs. * @param[in] aArgs An array of command line arguments. * */ - otError Process(uint8_t aArgsLength, Arg aArgs[]); + otError Process(Arg aArgs[]); private: enum @@ -92,7 +91,7 @@ private: struct Command { const char *mName; - otError (CoapSecure::*mHandler)(uint8_t aArgsLength, Arg aArgs[]); + otError (CoapSecure::*mHandler)(Arg aArgs[]); }; #if OPENTHREAD_CONFIG_COAP_BLOCKWISE_TRANSFER_ENABLE @@ -105,21 +104,21 @@ private: void PrintPayload(otMessage *aMessage) const; - otError ProcessConnect(uint8_t aArgsLength, Arg aArgs[]); - otError ProcessDelete(uint8_t aArgsLength, Arg aArgs[]); - otError ProcessDisconnect(uint8_t aArgsLength, Arg aArgs[]); - otError ProcessGet(uint8_t aArgsLength, Arg aArgs[]); - otError ProcessHelp(uint8_t aArgsLength, Arg aArgs[]); - otError ProcessPost(uint8_t aArgsLength, Arg aArgs[]); - otError ProcessPsk(uint8_t aArgsLength, Arg aArgs[]); - otError ProcessPut(uint8_t aArgsLength, Arg aArgs[]); - otError ProcessResource(uint8_t aArgsLength, Arg aArgs[]); - otError ProcessSet(uint8_t aArgsLength, Arg aArgs[]); - otError ProcessStart(uint8_t aArgsLength, Arg aArgs[]); - otError ProcessStop(uint8_t aArgsLength, Arg aArgs[]); - otError ProcessX509(uint8_t aArgsLength, Arg aArgs[]); + otError ProcessConnect(Arg aArgs[]); + otError ProcessDelete(Arg aArgs[]); + otError ProcessDisconnect(Arg aArgs[]); + otError ProcessGet(Arg aArgs[]); + otError ProcessHelp(Arg aArgs[]); + otError ProcessPost(Arg aArgs[]); + otError ProcessPsk(Arg aArgs[]); + otError ProcessPut(Arg aArgs[]); + otError ProcessResource(Arg aArgs[]); + otError ProcessSet(Arg aArgs[]); + otError ProcessStart(Arg aArgs[]); + otError ProcessStop(Arg aArgs[]); + otError ProcessX509(Arg aArgs[]); - otError ProcessRequest(uint8_t aArgsLength, Arg aArgs[], otCoapCode aCoapCode); + otError ProcessRequest(Arg aArgs[], otCoapCode aCoapCode); void Stop(void); diff --git a/src/cli/cli_commissioner.cpp b/src/cli/cli_commissioner.cpp index 1e4bef9aa..0a2a2e310 100644 --- a/src/cli/cli_commissioner.cpp +++ b/src/cli/cli_commissioner.cpp @@ -42,9 +42,8 @@ namespace Cli { constexpr Commissioner::Command Commissioner::sCommands[]; -otError Commissioner::ProcessHelp(uint8_t aArgsLength, Arg aArgs[]) +otError Commissioner::ProcessHelp(Arg aArgs[]) { - OT_UNUSED_VARIABLE(aArgsLength); OT_UNUSED_VARIABLE(aArgs); for (const Command &command : sCommands) @@ -55,7 +54,7 @@ otError Commissioner::ProcessHelp(uint8_t aArgsLength, Arg aArgs[]) return OT_ERROR_NONE; } -otError Commissioner::ProcessAnnounce(uint8_t aArgsLength, Arg aArgs[]) +otError Commissioner::ProcessAnnounce(Arg aArgs[]) { otError error; uint32_t mask; @@ -63,20 +62,18 @@ otError Commissioner::ProcessAnnounce(uint8_t aArgsLength, Arg aArgs[]) uint16_t period; otIp6Address address; - VerifyOrExit(aArgsLength > 3, error = OT_ERROR_INVALID_ARGS); - SuccessOrExit(error = aArgs[0].ParseAsUint32(mask)); SuccessOrExit(error = aArgs[1].ParseAsUint8(count)); SuccessOrExit(error = aArgs[2].ParseAsUint16(period)); SuccessOrExit(error = aArgs[3].ParseAsIp6Address(address)); - SuccessOrExit(error = otCommissionerAnnounceBegin(mInterpreter.mInstance, mask, count, period, &address)); + error = otCommissionerAnnounceBegin(mInterpreter.mInstance, mask, count, period, &address); exit: return error; } -otError Commissioner::ProcessEnergy(uint8_t aArgsLength, Arg aArgs[]) +otError Commissioner::ProcessEnergy(Arg aArgs[]) { otError error; uint32_t mask; @@ -85,29 +82,27 @@ otError Commissioner::ProcessEnergy(uint8_t aArgsLength, Arg aArgs[]) uint16_t scanDuration; otIp6Address address; - VerifyOrExit(aArgsLength > 4, error = OT_ERROR_INVALID_ARGS); - SuccessOrExit(error = aArgs[0].ParseAsUint32(mask)); SuccessOrExit(error = aArgs[1].ParseAsUint8(count)); SuccessOrExit(error = aArgs[2].ParseAsUint16(period)); SuccessOrExit(error = aArgs[3].ParseAsUint16(scanDuration)); SuccessOrExit(error = aArgs[4].ParseAsIp6Address(address)); - SuccessOrExit(error = otCommissionerEnergyScan(mInterpreter.mInstance, mask, count, period, scanDuration, &address, - &Commissioner::HandleEnergyReport, this)); + error = otCommissionerEnergyScan(mInterpreter.mInstance, mask, count, period, scanDuration, &address, + &Commissioner::HandleEnergyReport, this); exit: return error; } -otError Commissioner::ProcessJoiner(uint8_t aArgsLength, Arg aArgs[]) +otError Commissioner::ProcessJoiner(Arg aArgs[]) { - otError error; + otError error = OT_ERROR_NONE; otExtAddress addr; const otExtAddress *addrPtr = nullptr; otJoinerDiscerner discerner; - VerifyOrExit(aArgsLength > 1, error = OT_ERROR_INVALID_ARGS); + VerifyOrExit(!aArgs[1].IsEmpty(), error = OT_ERROR_INVALID_ARGS); memset(&discerner, 0, sizeof(discerner)); @@ -115,91 +110,93 @@ otError Commissioner::ProcessJoiner(uint8_t aArgsLength, Arg aArgs[]) { // Intentionally empty } - else if ((error = Interpreter::ParseJoinerDiscerner(aArgs[1], discerner)) == OT_ERROR_NOT_FOUND) + else { - SuccessOrExit(error = aArgs[1].ParseAsHexString(addr.m8)); - addrPtr = &addr; - } - else if (error != OT_ERROR_NONE) - { - ExitNow(); + error = Interpreter::ParseJoinerDiscerner(aArgs[1], discerner); + + if (error == OT_ERROR_NOT_FOUND) + { + error = aArgs[1].ParseAsHexString(addr.m8); + addrPtr = &addr; + } + + SuccessOrExit(error); } if (aArgs[0] == "add") { - VerifyOrExit(aArgsLength > 2, error = OT_ERROR_INVALID_ARGS); - // Timeout parameter is optional - if not specified, use default value. uint32_t timeout = kDefaultJoinerTimeout; - if (aArgsLength > 3) + VerifyOrExit(!aArgs[2].IsEmpty(), error = OT_ERROR_INVALID_ARGS); + + if (!aArgs[3].IsEmpty()) { SuccessOrExit(error = aArgs[3].ParseAsUint32(timeout)); } if (discerner.mLength) { - SuccessOrExit(error = otCommissionerAddJoinerWithDiscerner(mInterpreter.mInstance, &discerner, - aArgs[2].GetCString(), timeout)); + error = otCommissionerAddJoinerWithDiscerner(mInterpreter.mInstance, &discerner, aArgs[2].GetCString(), + timeout); } else { - SuccessOrExit(error = - otCommissionerAddJoiner(mInterpreter.mInstance, addrPtr, aArgs[2].GetCString(), timeout)); + error = otCommissionerAddJoiner(mInterpreter.mInstance, addrPtr, aArgs[2].GetCString(), timeout); } } else if (aArgs[0] == "remove") { if (discerner.mLength) { - SuccessOrExit(error = otCommissionerRemoveJoinerWithDiscerner(mInterpreter.mInstance, &discerner)); + error = otCommissionerRemoveJoinerWithDiscerner(mInterpreter.mInstance, &discerner); } else { - SuccessOrExit(error = otCommissionerRemoveJoiner(mInterpreter.mInstance, addrPtr)); + error = otCommissionerRemoveJoiner(mInterpreter.mInstance, addrPtr); } } else { - ExitNow(error = OT_ERROR_INVALID_ARGS); + error = OT_ERROR_INVALID_ARGS; } exit: return error; } -otError Commissioner::ProcessMgmtGet(uint8_t aArgsLength, Arg aArgs[]) +otError Commissioner::ProcessMgmtGet(Arg aArgs[]) { otError error = OT_ERROR_NONE; uint8_t tlvs[32]; uint8_t length = 0; - for (uint8_t index = 0; index < aArgsLength; index++) + for (; !aArgs->IsEmpty(); aArgs++) { VerifyOrExit(static_cast(length) < sizeof(tlvs), error = OT_ERROR_NO_BUFS); - if (aArgs[index] == "locator") + if (*aArgs == "locator") { tlvs[length++] = OT_MESHCOP_TLV_BORDER_AGENT_RLOC; } - else if (aArgs[index] == "sessionid") + else if (*aArgs == "sessionid") { tlvs[length++] = OT_MESHCOP_TLV_COMM_SESSION_ID; } - else if (aArgs[index] == "steeringdata") + else if (*aArgs == "steeringdata") { tlvs[length++] = OT_MESHCOP_TLV_STEERING_DATA; } - else if (aArgs[index] == "joinerudpport") + else if (*aArgs == "joinerudpport") { tlvs[length++] = OT_MESHCOP_TLV_JOINER_UDP_PORT; } - else if (aArgs[index] == "-x") + else if (*aArgs == "-x") { uint16_t readLength; - VerifyOrExit(++index < aArgsLength, error = OT_ERROR_INVALID_ARGS); + aArgs++; readLength = static_cast(sizeof(tlvs) - length); - SuccessOrExit(error = aArgs[index].ParseAsHexString(readLength, tlvs + length)); + SuccessOrExit(error = aArgs->ParseAsHexString(readLength, tlvs + length)); length += static_cast(readLength); } else @@ -208,60 +205,60 @@ otError Commissioner::ProcessMgmtGet(uint8_t aArgsLength, Arg aArgs[]) } } - SuccessOrExit(error = otCommissionerSendMgmtGet(mInterpreter.mInstance, tlvs, static_cast(length))); + error = otCommissionerSendMgmtGet(mInterpreter.mInstance, tlvs, static_cast(length)); exit: return error; } -otError Commissioner::ProcessMgmtSet(uint8_t aArgsLength, Arg aArgs[]) +otError Commissioner::ProcessMgmtSet(Arg aArgs[]) { otError error; otCommissioningDataset dataset; uint8_t tlvs[32]; uint8_t tlvsLength = 0; - VerifyOrExit(aArgsLength > 0, error = OT_ERROR_INVALID_ARGS); + VerifyOrExit(!aArgs[0].IsEmpty(), error = OT_ERROR_INVALID_ARGS); memset(&dataset, 0, sizeof(dataset)); - for (uint8_t index = 0; index < aArgsLength; index++) + for (; !aArgs->IsEmpty(); aArgs++) { - if (aArgs[index] == "locator") + if (*aArgs == "locator") { - VerifyOrExit(++index < aArgsLength, error = OT_ERROR_INVALID_ARGS); + aArgs++; dataset.mIsLocatorSet = true; - SuccessOrExit(error = aArgs[index].ParseAsUint16(dataset.mLocator)); + SuccessOrExit(error = aArgs->ParseAsUint16(dataset.mLocator)); } - else if (aArgs[index] == "sessionid") + else if (*aArgs == "sessionid") { - VerifyOrExit(++index < aArgsLength, error = OT_ERROR_INVALID_ARGS); + aArgs++; dataset.mIsSessionIdSet = true; - SuccessOrExit(error = aArgs[index].ParseAsUint16(dataset.mSessionId)); + SuccessOrExit(error = aArgs->ParseAsUint16(dataset.mSessionId)); } - else if (aArgs[index] == "steeringdata") + else if (*aArgs == "steeringdata") { uint16_t length; - VerifyOrExit(++index < aArgsLength, error = OT_ERROR_INVALID_ARGS); + aArgs++; dataset.mIsSteeringDataSet = true; length = sizeof(dataset.mSteeringData.m8); - SuccessOrExit(error = aArgs[index].ParseAsHexString(length, dataset.mSteeringData.m8)); + SuccessOrExit(error = aArgs->ParseAsHexString(length, dataset.mSteeringData.m8)); dataset.mSteeringData.mLength = static_cast(length); } - else if (aArgs[index] == "joinerudpport") + else if (*aArgs == "joinerudpport") { - VerifyOrExit(++index < aArgsLength, error = OT_ERROR_INVALID_ARGS); + aArgs++; dataset.mIsJoinerUdpPortSet = true; - SuccessOrExit(error = aArgs[index].ParseAsUint16(dataset.mJoinerUdpPort)); + SuccessOrExit(error = aArgs->ParseAsUint16(dataset.mJoinerUdpPort)); } - else if (aArgs[index] == "-x") + else if (*aArgs == "-x") { uint16_t length; - VerifyOrExit(++index < aArgsLength, error = OT_ERROR_INVALID_ARGS); + aArgs++; length = sizeof(tlvs); - SuccessOrExit(error = aArgs[index].ParseAsHexString(length, tlvs)); + SuccessOrExit(error = aArgs->ParseAsHexString(length, tlvs)); tlvsLength = static_cast(length); } else @@ -270,41 +267,39 @@ otError Commissioner::ProcessMgmtSet(uint8_t aArgsLength, Arg aArgs[]) } } - SuccessOrExit(error = otCommissionerSendMgmtSet(mInterpreter.mInstance, &dataset, tlvs, tlvsLength)); + error = otCommissionerSendMgmtSet(mInterpreter.mInstance, &dataset, tlvs, tlvsLength); exit: return error; } -otError Commissioner::ProcessPanId(uint8_t aArgsLength, Arg aArgs[]) +otError Commissioner::ProcessPanId(Arg aArgs[]) { otError error; uint16_t panId; uint32_t mask; otIp6Address address; - VerifyOrExit(aArgsLength > 2, error = OT_ERROR_INVALID_ARGS); - SuccessOrExit(error = aArgs[0].ParseAsUint16(panId)); SuccessOrExit(error = aArgs[1].ParseAsUint32(mask)); SuccessOrExit(error = aArgs[2].ParseAsIp6Address(address)); - SuccessOrExit(error = otCommissionerPanIdQuery(mInterpreter.mInstance, panId, mask, &address, - &Commissioner::HandlePanIdConflict, this)); + error = otCommissionerPanIdQuery(mInterpreter.mInstance, panId, mask, &address, &Commissioner::HandlePanIdConflict, + this); exit: return error; } -otError Commissioner::ProcessProvisioningUrl(uint8_t aArgsLength, Arg aArgs[]) +otError Commissioner::ProcessProvisioningUrl(Arg aArgs[]) { - return otCommissionerSetProvisioningUrl(mInterpreter.mInstance, - (aArgsLength > 0) ? aArgs[0].GetCString() : nullptr); + // If aArgs[0] is empty, `GetCString() will return `nullptr` + /// which will correctly clear the provisioning URL. + return otCommissionerSetProvisioningUrl(mInterpreter.mInstance, aArgs[0].GetCString()); } -otError Commissioner::ProcessSessionId(uint8_t aArgsLength, Arg aArgs[]) +otError Commissioner::ProcessSessionId(Arg aArgs[]) { - OT_UNUSED_VARIABLE(aArgsLength); OT_UNUSED_VARIABLE(aArgs); mInterpreter.OutputLine("%d", otCommissionerGetSessionId(mInterpreter.mInstance)); @@ -312,9 +307,8 @@ otError Commissioner::ProcessSessionId(uint8_t aArgsLength, Arg aArgs[]) return OT_ERROR_NONE; } -otError Commissioner::ProcessStart(uint8_t aArgsLength, Arg aArgs[]) +otError Commissioner::ProcessStart(Arg aArgs[]) { - OT_UNUSED_VARIABLE(aArgsLength); OT_UNUSED_VARIABLE(aArgs); return otCommissionerStart(mInterpreter.mInstance, &Commissioner::HandleStateChanged, @@ -394,17 +388,15 @@ void Commissioner::HandleJoinerEvent(otCommissionerJoinerEvent aEvent, mInterpreter.OutputLine(""); } -otError Commissioner::ProcessStop(uint8_t aArgsLength, Arg aArgs[]) +otError Commissioner::ProcessStop(Arg aArgs[]) { - OT_UNUSED_VARIABLE(aArgsLength); OT_UNUSED_VARIABLE(aArgs); return otCommissionerStop(mInterpreter.mInstance); } -otError Commissioner::ProcessState(uint8_t aArgsLength, Arg aArgs[]) +otError Commissioner::ProcessState(Arg aArgs[]) { - OT_UNUSED_VARIABLE(aArgsLength); OT_UNUSED_VARIABLE(aArgs); mInterpreter.OutputLine(StateToString(otCommissionerGetState(mInterpreter.mInstance))); @@ -412,17 +404,21 @@ otError Commissioner::ProcessState(uint8_t aArgsLength, Arg aArgs[]) return OT_ERROR_NONE; } -otError Commissioner::Process(uint8_t aArgsLength, Arg aArgs[]) +otError Commissioner::Process(Arg aArgs[]) { otError error = OT_ERROR_INVALID_COMMAND; const Command *command; - VerifyOrExit(aArgsLength != 0, IgnoreError(ProcessHelp(0, nullptr))); + if (aArgs[0].IsEmpty()) + { + IgnoreError(ProcessHelp(aArgs)); + ExitNow(); + } command = Utils::LookupTable::Find(aArgs[0].GetCString(), sCommands); VerifyOrExit(command != nullptr); - error = (this->*command->mHandler)(aArgsLength - 1, aArgs + 1); + error = (this->*command->mHandler)(aArgs + 1); exit: return error; diff --git a/src/cli/cli_commissioner.hpp b/src/cli/cli_commissioner.hpp index 3dd537984..2ce82c8e5 100644 --- a/src/cli/cli_commissioner.hpp +++ b/src/cli/cli_commissioner.hpp @@ -71,11 +71,10 @@ public: /** * This method interprets a list of CLI arguments. * - * @param[in] aArgsLength The number of elements in @p aArgs. * @param[in] aArgs An array of command line arguments. * */ - otError Process(uint8_t aArgsLength, Arg aArgs[]); + otError Process(Arg aArgs[]); private: enum @@ -86,21 +85,21 @@ private: struct Command { const char *mName; - otError (Commissioner::*mHandler)(uint8_t aArgsLength, Arg aArgs[]); + otError (Commissioner::*mHandler)(Arg aArgs[]); }; - otError ProcessHelp(uint8_t aArgsLength, Arg aArgs[]); - otError ProcessAnnounce(uint8_t aArgsLength, Arg aArgs[]); - otError ProcessEnergy(uint8_t aArgsLength, Arg aArgs[]); - otError ProcessJoiner(uint8_t aArgsLength, Arg aArgs[]); - otError ProcessMgmtGet(uint8_t aArgsLength, Arg aArgs[]); - otError ProcessMgmtSet(uint8_t aArgsLength, Arg aArgs[]); - otError ProcessPanId(uint8_t aArgsLength, Arg aArgs[]); - otError ProcessProvisioningUrl(uint8_t aArgsLength, Arg aArgs[]); - otError ProcessSessionId(uint8_t aArgsLength, Arg aArgs[]); - otError ProcessStart(uint8_t aArgsLength, Arg aArgs[]); - otError ProcessState(uint8_t aArgsLength, Arg aArgs[]); - otError ProcessStop(uint8_t aArgsLength, Arg aArgs[]); + otError ProcessHelp(Arg aArgs[]); + otError ProcessAnnounce(Arg aArgs[]); + otError ProcessEnergy(Arg aArgs[]); + otError ProcessJoiner(Arg aArgs[]); + otError ProcessMgmtGet(Arg aArgs[]); + otError ProcessMgmtSet(Arg aArgs[]); + otError ProcessPanId(Arg aArgs[]); + otError ProcessProvisioningUrl(Arg aArgs[]); + otError ProcessSessionId(Arg aArgs[]); + otError ProcessStart(Arg aArgs[]); + otError ProcessState(Arg aArgs[]); + otError ProcessStop(Arg aArgs[]); static void HandleStateChanged(otCommissionerState aState, void *aContext); void HandleStateChanged(otCommissionerState aState); diff --git a/src/cli/cli_dataset.cpp b/src/cli/cli_dataset.cpp index 902aca89b..440852709 100644 --- a/src/cli/cli_dataset.cpp +++ b/src/cli/cli_dataset.cpp @@ -124,12 +124,12 @@ otError Dataset::Print(otOperationalDataset &aDataset) return OT_ERROR_NONE; } -otError Dataset::Process(uint8_t aArgsLength, Arg aArgs[]) +otError Dataset::Process(Arg aArgs[]) { otError error = OT_ERROR_INVALID_COMMAND; const Command *command; - if (aArgsLength == 0) + if (aArgs[0].IsEmpty()) { ExitNow(error = Print(sDataset)); } @@ -137,15 +137,14 @@ otError Dataset::Process(uint8_t aArgsLength, Arg aArgs[]) command = Utils::LookupTable::Find(aArgs[0].GetCString(), sCommands); VerifyOrExit(command != nullptr); - error = (this->*command->mHandler)(aArgsLength - 1, aArgs + 1); + error = (this->*command->mHandler)(aArgs + 1); exit: return error; } -otError Dataset::ProcessHelp(uint8_t aArgsLength, Arg aArgs[]) +otError Dataset::ProcessHelp(Arg aArgs[]) { - OT_UNUSED_VARIABLE(aArgsLength); OT_UNUSED_VARIABLE(aArgs); for (const Command &command : sCommands) @@ -156,100 +155,81 @@ otError Dataset::ProcessHelp(uint8_t aArgsLength, Arg aArgs[]) return OT_ERROR_NONE; } -otError Dataset::ProcessInit(uint8_t aArgsLength, Arg aArgs[]) +otError Dataset::ProcessInit(Arg aArgs[]) { - otError error = OT_ERROR_NONE; - - VerifyOrExit(aArgsLength > 0, error = OT_ERROR_INVALID_ARGS); + otError error = OT_ERROR_INVALID_ARGS; if (aArgs[0] == "active") { - SuccessOrExit(error = otDatasetGetActive(mInterpreter.mInstance, &sDataset)); + error = otDatasetGetActive(mInterpreter.mInstance, &sDataset); } else if (aArgs[0] == "pending") { - SuccessOrExit(error = otDatasetGetPending(mInterpreter.mInstance, &sDataset)); + error = otDatasetGetPending(mInterpreter.mInstance, &sDataset); } #if OPENTHREAD_FTD else if (aArgs[0] == "new") { - SuccessOrExit(error = otDatasetCreateNewNetwork(mInterpreter.mInstance, &sDataset)); + error = otDatasetCreateNewNetwork(mInterpreter.mInstance, &sDataset); } #endif - else - { - ExitNow(error = OT_ERROR_INVALID_ARGS); - } -exit: return error; } -otError Dataset::ProcessActive(uint8_t aArgsLength, Arg aArgs[]) +otError Dataset::ProcessActive(Arg aArgs[]) { - otError error; + otError error = OT_ERROR_INVALID_ARGS; - if (aArgsLength == 0) + if (aArgs[0].IsEmpty()) { otOperationalDataset dataset; SuccessOrExit(error = otDatasetGetActive(mInterpreter.mInstance, &dataset)); error = Print(dataset); } - else if ((aArgsLength == 1) && (aArgs[0] == "-x")) + else if (aArgs[0] == "-x") { otOperationalDatasetTlvs dataset; - 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); mInterpreter.OutputLine(""); } - else - { - ExitNow(error = OT_ERROR_INVALID_ARGS); - } exit: return error; } -otError Dataset::ProcessPending(uint8_t aArgsLength, Arg aArgs[]) +otError Dataset::ProcessPending(Arg aArgs[]) { - otError error; + otError error = OT_ERROR_INVALID_ARGS; - if (aArgsLength == 0) + if (aArgs[0].IsEmpty()) { otOperationalDataset dataset; SuccessOrExit(error = otDatasetGetPending(mInterpreter.mInstance, &dataset)); error = Print(dataset); } - else if ((aArgsLength == 1) && (aArgs[0] == "-x")) + else if (aArgs[0] == "-x") { otOperationalDatasetTlvs dataset; - 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); mInterpreter.OutputLine(""); } - else - { - ExitNow(error = OT_ERROR_INVALID_ARGS); - } exit: return error; } -otError Dataset::ProcessActiveTimestamp(uint8_t aArgsLength, Arg aArgs[]) +otError Dataset::ProcessActiveTimestamp(Arg aArgs[]) { otError error = OT_ERROR_NONE; - if (aArgsLength == 0) + if (aArgs[0].IsEmpty()) { if (sDataset.mComponents.mIsActiveTimestampPresent) { @@ -266,11 +246,11 @@ exit: return error; } -otError Dataset::ProcessChannel(uint8_t aArgsLength, Arg aArgs[]) +otError Dataset::ProcessChannel(Arg aArgs[]) { otError error = OT_ERROR_NONE; - if (aArgsLength == 0) + if (aArgs[0].IsEmpty()) { if (sDataset.mComponents.mIsChannelPresent) { @@ -287,11 +267,11 @@ exit: return error; } -otError Dataset::ProcessChannelMask(uint8_t aArgsLength, Arg aArgs[]) +otError Dataset::ProcessChannelMask(Arg aArgs[]) { otError error = OT_ERROR_NONE; - if (aArgsLength == 0) + if (aArgs[0].IsEmpty()) { if (sDataset.mComponents.mIsChannelMaskPresent) { @@ -308,43 +288,35 @@ exit: return error; } -otError Dataset::ProcessClear(uint8_t aArgsLength, Arg aArgs[]) +otError Dataset::ProcessClear(Arg aArgs[]) { - OT_UNUSED_VARIABLE(aArgsLength); OT_UNUSED_VARIABLE(aArgs); memset(&sDataset, 0, sizeof(sDataset)); return OT_ERROR_NONE; } -otError Dataset::ProcessCommit(uint8_t aArgsLength, Arg aArgs[]) +otError Dataset::ProcessCommit(Arg aArgs[]) { - otError error = OT_ERROR_NONE; - - VerifyOrExit(aArgsLength > 0, error = OT_ERROR_INVALID_ARGS); + otError error = OT_ERROR_INVALID_ARGS; if (aArgs[0] == "active") { - SuccessOrExit(error = otDatasetSetActive(mInterpreter.mInstance, &sDataset)); + error = otDatasetSetActive(mInterpreter.mInstance, &sDataset); } else if (aArgs[0] == "pending") { - SuccessOrExit(error = otDatasetSetPending(mInterpreter.mInstance, &sDataset)); - } - else - { - ExitNow(error = OT_ERROR_INVALID_ARGS); + error = otDatasetSetPending(mInterpreter.mInstance, &sDataset); } -exit: return error; } -otError Dataset::ProcessDelay(uint8_t aArgsLength, Arg aArgs[]) +otError Dataset::ProcessDelay(Arg aArgs[]) { otError error = OT_ERROR_NONE; - if (aArgsLength == 0) + if (aArgs[0].IsEmpty()) { if (sDataset.mComponents.mIsDelayPresent) { @@ -361,11 +333,11 @@ exit: return error; } -otError Dataset::ProcessExtPanId(uint8_t aArgsLength, Arg aArgs[]) +otError Dataset::ProcessExtPanId(Arg aArgs[]) { otError error = OT_ERROR_NONE; - if (aArgsLength == 0) + if (aArgs[0].IsEmpty()) { if (sDataset.mComponents.mIsExtendedPanIdPresent) { @@ -383,11 +355,11 @@ exit: return error; } -otError Dataset::ProcessMeshLocalPrefix(uint8_t aArgsLength, Arg aArgs[]) +otError Dataset::ProcessMeshLocalPrefix(Arg aArgs[]) { otError error = OT_ERROR_NONE; - if (aArgsLength == 0) + if (aArgs[0].IsEmpty()) { if (sDataset.mComponents.mIsMeshLocalPrefixPresent) { @@ -410,11 +382,11 @@ exit: return error; } -otError Dataset::ProcessNetworkKey(uint8_t aArgsLength, Arg aArgs[]) +otError Dataset::ProcessNetworkKey(Arg aArgs[]) { otError error = OT_ERROR_NONE; - if (aArgsLength == 0) + if (aArgs[0].IsEmpty()) { if (sDataset.mComponents.mIsNetworkKeyPresent) { @@ -432,11 +404,11 @@ exit: return error; } -otError Dataset::ProcessNetworkName(uint8_t aArgsLength, Arg aArgs[]) +otError Dataset::ProcessNetworkName(Arg aArgs[]) { otError error = OT_ERROR_NONE; - if (aArgsLength == 0) + if (aArgs[0].IsEmpty()) { if (sDataset.mComponents.mIsNetworkNamePresent) { @@ -453,11 +425,11 @@ exit: return error; } -otError Dataset::ProcessPanId(uint8_t aArgsLength, Arg aArgs[]) +otError Dataset::ProcessPanId(Arg aArgs[]) { otError error = OT_ERROR_NONE; - if (aArgsLength == 0) + if (aArgs[0].IsEmpty()) { if (sDataset.mComponents.mIsPanIdPresent) { @@ -474,11 +446,11 @@ exit: return error; } -otError Dataset::ProcessPendingTimestamp(uint8_t aArgsLength, Arg aArgs[]) +otError Dataset::ProcessPendingTimestamp(Arg aArgs[]) { otError error = OT_ERROR_NONE; - if (aArgsLength == 0) + if (aArgs[0].IsEmpty()) { if (sDataset.mComponents.mIsPendingTimestampPresent) { @@ -495,96 +467,94 @@ exit: return error; } -otError Dataset::ProcessMgmtSetCommand(uint8_t aArgsLength, Arg aArgs[]) +otError Dataset::ProcessMgmtSetCommand(Arg aArgs[]) { otError error = OT_ERROR_NONE; otOperationalDataset dataset; uint8_t tlvs[128]; uint8_t tlvsLength = 0; - VerifyOrExit(aArgsLength > 0, error = OT_ERROR_INVALID_ARGS); - memset(&dataset, 0, sizeof(dataset)); - for (uint8_t index = 1; index < aArgsLength; index++) + for (Arg *arg = &aArgs[1]; !arg->IsEmpty(); arg++) { - if (aArgs[index] == "activetimestamp") + if (*arg == "activetimestamp") { - VerifyOrExit(++index < aArgsLength, error = OT_ERROR_INVALID_ARGS); + arg++; dataset.mComponents.mIsActiveTimestampPresent = true; - SuccessOrExit(error = aArgs[index].ParseAsUint64(dataset.mActiveTimestamp)); + SuccessOrExit(error = arg->ParseAsUint64(dataset.mActiveTimestamp)); } - else if (aArgs[index] == "pendingtimestamp") + else if (*arg == "pendingtimestamp") { - VerifyOrExit(++index < aArgsLength, error = OT_ERROR_INVALID_ARGS); + arg++; dataset.mComponents.mIsPendingTimestampPresent = true; - SuccessOrExit(error = aArgs[index].ParseAsUint64(dataset.mPendingTimestamp)); + SuccessOrExit(error = arg->ParseAsUint64(dataset.mPendingTimestamp)); } - else if (aArgs[index] == "networkkey") + else if (*arg == "networkkey") { - VerifyOrExit(++index < aArgsLength, error = OT_ERROR_INVALID_ARGS); + arg++; dataset.mComponents.mIsNetworkKeyPresent = true; - SuccessOrExit(error = aArgs[index].ParseAsHexString(dataset.mNetworkKey.m8)); + SuccessOrExit(error = arg->ParseAsHexString(dataset.mNetworkKey.m8)); } - else if (aArgs[index] == "networkname") + else if (*arg == "networkname") { - VerifyOrExit(++index < aArgsLength, error = OT_ERROR_INVALID_ARGS); + arg++; dataset.mComponents.mIsNetworkNamePresent = true; - SuccessOrExit(error = otNetworkNameFromString(&dataset.mNetworkName, aArgs[index].GetCString())); + VerifyOrExit(!arg->IsEmpty(), error = OT_ERROR_INVALID_ARGS); + SuccessOrExit(error = otNetworkNameFromString(&dataset.mNetworkName, arg->GetCString())); } - else if (aArgs[index] == "extpanid") + else if (*arg == "extpanid") { - VerifyOrExit(++index < aArgsLength, error = OT_ERROR_INVALID_ARGS); + arg++; dataset.mComponents.mIsExtendedPanIdPresent = true; - SuccessOrExit(error = aArgs[index].ParseAsHexString(dataset.mExtendedPanId.m8)); + SuccessOrExit(error = arg->ParseAsHexString(dataset.mExtendedPanId.m8)); } - else if (aArgs[index] == "localprefix") + else if (*arg == "localprefix") { otIp6Address prefix; - VerifyOrExit(++index < aArgsLength, error = OT_ERROR_INVALID_ARGS); + arg++; dataset.mComponents.mIsMeshLocalPrefixPresent = true; - SuccessOrExit(error = aArgs[index].ParseAsIp6Address(prefix)); + SuccessOrExit(error = arg->ParseAsIp6Address(prefix)); memcpy(dataset.mMeshLocalPrefix.m8, prefix.mFields.m8, sizeof(dataset.mMeshLocalPrefix.m8)); } - else if (aArgs[index] == "delaytimer") + else if (*arg == "delaytimer") { - VerifyOrExit(++index < aArgsLength, error = OT_ERROR_INVALID_ARGS); + arg++; dataset.mComponents.mIsDelayPresent = true; - SuccessOrExit(error = aArgs[index].ParseAsUint32(dataset.mDelay)); + SuccessOrExit(error = arg->ParseAsUint32(dataset.mDelay)); } - else if (aArgs[index] == "panid") + else if (*arg == "panid") { - VerifyOrExit(++index < aArgsLength, error = OT_ERROR_INVALID_ARGS); + arg++; dataset.mComponents.mIsPanIdPresent = true; - SuccessOrExit(error = aArgs[index].ParseAsUint16(dataset.mPanId)); + SuccessOrExit(error = arg->ParseAsUint16(dataset.mPanId)); } - else if (aArgs[index] == "channel") + else if (*arg == "channel") { - VerifyOrExit(++index < aArgsLength, error = OT_ERROR_INVALID_ARGS); + arg++; dataset.mComponents.mIsChannelPresent = true; - SuccessOrExit(error = aArgs[index].ParseAsUint16(dataset.mChannel)); + SuccessOrExit(error = arg->ParseAsUint16(dataset.mChannel)); } - else if (aArgs[index] == "channelmask") + else if (*arg == "channelmask") { - VerifyOrExit(++index < aArgsLength, error = OT_ERROR_INVALID_ARGS); + arg++; dataset.mComponents.mIsChannelMaskPresent = true; - SuccessOrExit(error = aArgs[index].ParseAsUint32(dataset.mChannelMask)); + SuccessOrExit(error = arg->ParseAsUint32(dataset.mChannelMask)); } - else if (aArgs[index] == "securitypolicy") + else if (*arg == "securitypolicy") { - VerifyOrExit(++index < aArgsLength, error = OT_ERROR_INVALID_ARGS); - SuccessOrExit(error = ParseSecurityPolicy(dataset.mSecurityPolicy, aArgsLength - index, &aArgs[index])); + arg++; + SuccessOrExit(error = ParseSecurityPolicy(dataset.mSecurityPolicy, arg)); dataset.mComponents.mIsSecurityPolicyPresent = true; - ++index; } - else if (aArgs[index] == "-x") + else if (*arg == "-x") { uint16_t length; - VerifyOrExit(++index < aArgsLength, error = OT_ERROR_INVALID_ARGS); + arg++; length = sizeof(tlvs); - SuccessOrExit(error = aArgs[index].ParseAsHexString(length, tlvs)); + SuccessOrExit(error = arg->ParseAsHexString(length, tlvs)); tlvsLength = static_cast(length); } else @@ -595,22 +565,22 @@ otError Dataset::ProcessMgmtSetCommand(uint8_t aArgsLength, Arg aArgs[]) if (aArgs[0] == "active") { - SuccessOrExit(error = otDatasetSendMgmtActiveSet(mInterpreter.mInstance, &dataset, tlvs, tlvsLength)); + error = otDatasetSendMgmtActiveSet(mInterpreter.mInstance, &dataset, tlvs, tlvsLength); } else if (aArgs[0] == "pending") { - SuccessOrExit(error = otDatasetSendMgmtPendingSet(mInterpreter.mInstance, &dataset, tlvs, tlvsLength)); + error = otDatasetSendMgmtPendingSet(mInterpreter.mInstance, &dataset, tlvs, tlvsLength); } else { - ExitNow(error = OT_ERROR_INVALID_ARGS); + error = OT_ERROR_INVALID_ARGS; } exit: return error; } -otError Dataset::ProcessMgmtGetCommand(uint8_t aArgsLength, Arg aArgs[]) +otError Dataset::ProcessMgmtGetCommand(Arg aArgs[]) { otError error = OT_ERROR_NONE; otOperationalDatasetComponents datasetComponents; @@ -619,65 +589,63 @@ otError Dataset::ProcessMgmtGetCommand(uint8_t aArgsLength, Arg aArgs[]) bool destAddrSpecified = false; otIp6Address address; - VerifyOrExit(aArgsLength > 0, error = OT_ERROR_INVALID_ARGS); - memset(&datasetComponents, 0, sizeof(datasetComponents)); - for (uint8_t index = 1; index < aArgsLength; index++) + for (Arg *arg = &aArgs[1]; !arg->IsEmpty(); arg++) { - if (aArgs[index] == "activetimestamp") + if (*arg == "activetimestamp") { datasetComponents.mIsActiveTimestampPresent = true; } - else if (aArgs[index] == "pendingtimestamp") + else if (*arg == "pendingtimestamp") { datasetComponents.mIsPendingTimestampPresent = true; } - else if (aArgs[index] == "networkkey") + else if (*arg == "networkkey") { datasetComponents.mIsNetworkKeyPresent = true; } - else if (aArgs[index] == "networkname") + else if (*arg == "networkname") { datasetComponents.mIsNetworkNamePresent = true; } - else if (aArgs[index] == "extpanid") + else if (*arg == "extpanid") { datasetComponents.mIsExtendedPanIdPresent = true; } - else if (aArgs[index] == "localprefix") + else if (*arg == "localprefix") { datasetComponents.mIsMeshLocalPrefixPresent = true; } - else if (aArgs[index] == "delaytimer") + else if (*arg == "delaytimer") { datasetComponents.mIsDelayPresent = true; } - else if (aArgs[index] == "panid") + else if (*arg == "panid") { datasetComponents.mIsPanIdPresent = true; } - else if (aArgs[index] == "channel") + else if (*arg == "channel") { datasetComponents.mIsChannelPresent = true; } - else if (aArgs[index] == "securitypolicy") + else if (*arg == "securitypolicy") { datasetComponents.mIsSecurityPolicyPresent = true; } - else if (aArgs[index] == "-x") + else if (*arg == "-x") { uint16_t length; - VerifyOrExit(++index < aArgsLength, error = OT_ERROR_INVALID_ARGS); + arg++; length = sizeof(tlvs); - SuccessOrExit(error = aArgs[index].ParseAsHexString(length, tlvs)); + SuccessOrExit(error = arg->ParseAsHexString(length, tlvs)); tlvsLength = static_cast(length); } - else if (aArgs[index] == "address") + else if (*arg == "address") { - VerifyOrExit(++index < aArgsLength, error = OT_ERROR_INVALID_ARGS); - SuccessOrExit(error = aArgs[index].ParseAsIp6Address(address)); + arg++; + SuccessOrExit(error = arg->ParseAsIp6Address(address)); destAddrSpecified = true; } else @@ -688,28 +656,28 @@ otError Dataset::ProcessMgmtGetCommand(uint8_t aArgsLength, Arg aArgs[]) if (aArgs[0] == "active") { - SuccessOrExit(error = otDatasetSendMgmtActiveGet(mInterpreter.mInstance, &datasetComponents, tlvs, tlvsLength, - destAddrSpecified ? &address : nullptr)); + error = otDatasetSendMgmtActiveGet(mInterpreter.mInstance, &datasetComponents, tlvs, tlvsLength, + destAddrSpecified ? &address : nullptr); } else if (aArgs[0] == "pending") { - SuccessOrExit(error = otDatasetSendMgmtPendingGet(mInterpreter.mInstance, &datasetComponents, tlvs, tlvsLength, - destAddrSpecified ? &address : nullptr)); + error = otDatasetSendMgmtPendingGet(mInterpreter.mInstance, &datasetComponents, tlvs, tlvsLength, + destAddrSpecified ? &address : nullptr); } else { - ExitNow(error = OT_ERROR_INVALID_ARGS); + error = OT_ERROR_INVALID_ARGS; } exit: return error; } -otError Dataset::ProcessPskc(uint8_t aArgsLength, Arg aArgs[]) +otError Dataset::ProcessPskc(Arg aArgs[]) { otError error = OT_ERROR_NONE; - if (aArgsLength == 0) + if (aArgs[0].IsEmpty()) { if (sDataset.mComponents.mIsPskcPresent) { @@ -717,30 +685,31 @@ otError Dataset::ProcessPskc(uint8_t aArgsLength, Arg aArgs[]) mInterpreter.OutputLine(""); } } - else if (aArgsLength == 1) - { - SuccessOrExit(error = aArgs[0].ParseAsHexString(sDataset.mPskc.m8)); - } -#if OPENTHREAD_FTD - else if (aArgsLength == 2 && (aArgs[0] == "-p")) - { - SuccessOrExit( - error = otDatasetGeneratePskc( - aArgs[1].GetCString(), - (sDataset.mComponents.mIsNetworkNamePresent - ? &sDataset.mNetworkName - : reinterpret_cast(otThreadGetNetworkName(mInterpreter.mInstance))), - (sDataset.mComponents.mIsExtendedPanIdPresent ? &sDataset.mExtendedPanId - : otThreadGetExtendedPanId(mInterpreter.mInstance)), - &sDataset.mPskc)); - } -#endif else { - ExitNow(error = OT_ERROR_INVALID_ARGS); - } +#if OPENTHREAD_FTD + if (aArgs[0] == "-p") + { + VerifyOrExit(!aArgs[1].IsEmpty(), error = OT_ERROR_INVALID_ARGS); - sDataset.mComponents.mIsPskcPresent = true; + SuccessOrExit( + error = otDatasetGeneratePskc( + aArgs[1].GetCString(), + (sDataset.mComponents.mIsNetworkNamePresent + ? &sDataset.mNetworkName + : reinterpret_cast(otThreadGetNetworkName(mInterpreter.mInstance))), + (sDataset.mComponents.mIsExtendedPanIdPresent ? &sDataset.mExtendedPanId + : otThreadGetExtendedPanId(mInterpreter.mInstance)), + &sDataset.mPskc)); + } + else +#endif + { + SuccessOrExit(error = aArgs[0].ParseAsHexString(sDataset.mPskc.m8)); + } + + sDataset.mComponents.mIsPskcPresent = true; + } exit: return error; @@ -796,17 +765,19 @@ void Dataset::OutputSecurityPolicy(const otSecurityPolicy &aSecurityPolicy) } } -otError Dataset::ParseSecurityPolicy(otSecurityPolicy &aSecurityPolicy, uint8_t aArgsLength, Arg aArgs[]) +otError Dataset::ParseSecurityPolicy(otSecurityPolicy &aSecurityPolicy, Arg *&aArgs) { otError error; otSecurityPolicy policy; memset(&policy, 0, sizeof(policy)); - SuccessOrExit(error = aArgs[0].ParseAsUint16(policy.mRotationTime)); - VerifyOrExit(aArgsLength >= 2); + SuccessOrExit(error = aArgs->ParseAsUint16(policy.mRotationTime)); + aArgs++; - for (const char *flag = aArgs[1].GetCString(); *flag != '\0'; flag++) + VerifyOrExit(!aArgs->IsEmpty()); + + for (const char *flag = aArgs->GetCString(); *flag != '\0'; flag++) { switch (*flag) { @@ -851,6 +822,8 @@ otError Dataset::ParseSecurityPolicy(otSecurityPolicy &aSecurityPolicy, uint8_t } } + aArgs++; + exit: if (error == OT_ERROR_NONE) { @@ -860,11 +833,11 @@ exit: return error; } -otError Dataset::ProcessSecurityPolicy(uint8_t aArgsLength, Arg aArgs[]) +otError Dataset::ProcessSecurityPolicy(Arg aArgs[]) { otError error = OT_ERROR_NONE; - if (aArgsLength == 0) + if (aArgs[0].IsEmpty()) { if (sDataset.mComponents.mIsSecurityPolicyPresent) { @@ -874,7 +847,9 @@ otError Dataset::ProcessSecurityPolicy(uint8_t aArgsLength, Arg aArgs[]) } else { - SuccessOrExit(error = ParseSecurityPolicy(sDataset.mSecurityPolicy, aArgsLength, aArgs)); + Arg *arg = &aArgs[0]; + + SuccessOrExit(error = ParseSecurityPolicy(sDataset.mSecurityPolicy, arg)); sDataset.mComponents.mIsSecurityPolicyPresent = true; } @@ -882,13 +857,11 @@ exit: return error; } -otError Dataset::ProcessSet(uint8_t aArgsLength, Arg aArgs[]) +otError Dataset::ProcessSet(Arg aArgs[]) { otError error = OT_ERROR_NONE; MeshCoP::Dataset::Type datasetType; - VerifyOrExit(aArgsLength == 2, error = OT_ERROR_INVALID_ARGS); - if (aArgs[0] == "active") { datasetType = MeshCoP::Dataset::Type::kActive; @@ -929,17 +902,15 @@ exit: #if OPENTHREAD_CONFIG_DATASET_UPDATER_ENABLE && OPENTHREAD_FTD -otError Dataset::ProcessUpdater(uint8_t aArgsLength, Arg aArgs[]) +otError Dataset::ProcessUpdater(Arg aArgs[]) { otError error = OT_ERROR_NONE; - if (aArgsLength == 0) + if (aArgs[0].IsEmpty()) { mInterpreter.OutputEnabledDisabledStatus(otDatasetUpdaterIsUpdateOngoing(mInterpreter.mInstance)); - ExitNow(); } - - if (aArgs[0] == "start") + else if (aArgs[0] == "start") { error = otDatasetUpdaterRequestUpdate(mInterpreter.mInstance, &sDataset, &Dataset::HandleDatasetUpdater, this); } @@ -952,7 +923,6 @@ otError Dataset::ProcessUpdater(uint8_t aArgsLength, Arg aArgs[]) error = OT_ERROR_INVALID_ARGS; } -exit: return error; } diff --git a/src/cli/cli_dataset.hpp b/src/cli/cli_dataset.hpp index f2deef82a..e131336a2 100644 --- a/src/cli/cli_dataset.hpp +++ b/src/cli/cli_dataset.hpp @@ -65,51 +65,50 @@ public: /** * This method interprets a list of CLI arguments. * - * @param[in] aArgsLength The number of elements in @p aArgs. * @param[in] aArgs An array of command line arguments. * */ - otError Process(uint8_t aArgsLength, Arg aArgs[]); + otError Process(Arg aArgs[]); private: struct Command { const char *mName; - otError (Dataset::*mHandler)(uint8_t aArgsLength, Arg aArgs[]); + otError (Dataset::*mHandler)(Arg aArgs[]); }; otError Print(otOperationalDataset &aDataset); - otError ProcessHelp(uint8_t aArgsLength, Arg aArgs[]); - otError ProcessActive(uint8_t aArgsLength, Arg aArgs[]); - otError ProcessActiveTimestamp(uint8_t aArgsLength, Arg aArgs[]); - otError ProcessChannel(uint8_t aArgsLength, Arg aArgs[]); - otError ProcessChannelMask(uint8_t aArgsLength, Arg aArgs[]); - otError ProcessClear(uint8_t aArgsLength, Arg aArgs[]); - otError ProcessCommit(uint8_t aArgsLength, Arg aArgs[]); - otError ProcessDelay(uint8_t aArgsLength, Arg aArgs[]); - otError ProcessExtPanId(uint8_t aArgsLength, Arg aArgs[]); - otError ProcessInit(uint8_t aArgsLength, Arg aArgs[]); - otError ProcessMeshLocalPrefix(uint8_t aArgsLength, Arg aArgs[]); - otError ProcessNetworkKey(uint8_t aArgsLength, Arg aArgs[]); - otError ProcessNetworkName(uint8_t aArgsLength, Arg aArgs[]); - otError ProcessPanId(uint8_t aArgsLength, Arg aArgs[]); - otError ProcessPending(uint8_t aArgsLength, Arg aArgs[]); - otError ProcessPendingTimestamp(uint8_t aArgsLength, Arg aArgs[]); - otError ProcessMgmtSetCommand(uint8_t aArgsLength, Arg aArgs[]); - otError ProcessMgmtGetCommand(uint8_t aArgsLength, Arg aArgs[]); - otError ProcessPskc(uint8_t aArgsLength, Arg aArgs[]); - otError ProcessSecurityPolicy(uint8_t aArgsLength, Arg aArgs[]); - otError ProcessSet(uint8_t aArgsLength, Arg aArgs[]); + otError ProcessHelp(Arg aArgs[]); + otError ProcessActive(Arg aArgs[]); + otError ProcessActiveTimestamp(Arg aArgs[]); + otError ProcessChannel(Arg aArgs[]); + otError ProcessChannelMask(Arg aArgs[]); + otError ProcessClear(Arg aArgs[]); + otError ProcessCommit(Arg aArgs[]); + otError ProcessDelay(Arg aArgs[]); + otError ProcessExtPanId(Arg aArgs[]); + otError ProcessInit(Arg aArgs[]); + otError ProcessMeshLocalPrefix(Arg aArgs[]); + otError ProcessNetworkName(Arg aArgs[]); + otError ProcessNetworkKey(Arg aArgs[]); + otError ProcessPanId(Arg aArgs[]); + otError ProcessPending(Arg aArgs[]); + otError ProcessPendingTimestamp(Arg aArgs[]); + otError ProcessMgmtSetCommand(Arg aArgs[]); + otError ProcessMgmtGetCommand(Arg aArgs[]); + otError ProcessPskc(Arg aArgs[]); + otError ProcessSecurityPolicy(Arg aArgs[]); + otError ProcessSet(Arg aArgs[]); #if OPENTHREAD_CONFIG_DATASET_UPDATER_ENABLE && OPENTHREAD_FTD - otError ProcessUpdater(uint8_t aArgsLength, Arg aArgs[]); + otError ProcessUpdater(Arg aArgs[]); static void HandleDatasetUpdater(otError aError, void *aContext); void HandleDatasetUpdater(otError aError); #endif void OutputSecurityPolicy(const otSecurityPolicy &aSecurityPolicy); - otError ParseSecurityPolicy(otSecurityPolicy &aSecurityPolicy, uint8_t aArgsLength, Arg aArgs[]); + otError ParseSecurityPolicy(otSecurityPolicy &aSecurityPolicy, Arg *&aArgs); static constexpr Command sCommands[] = { {"active", &Dataset::ProcessActive}, diff --git a/src/cli/cli_joiner.cpp b/src/cli/cli_joiner.cpp index b89c1b5ae..ea0a351c1 100644 --- a/src/cli/cli_joiner.cpp +++ b/src/cli/cli_joiner.cpp @@ -44,11 +44,20 @@ namespace Cli { constexpr Joiner::Command Joiner::sCommands[]; -otError Joiner::ProcessDiscerner(uint8_t aArgsLength, Arg aArgs[]) +otError Joiner::ProcessDiscerner(Arg aArgs[]) { - otError error = OT_ERROR_NONE; + otError error = OT_ERROR_INVALID_ARGS; - if (aArgsLength == 1) + if (aArgs[0].IsEmpty()) + { + const otJoinerDiscerner *discerner = otJoinerGetDiscerner(mInterpreter.mInstance); + + VerifyOrExit(discerner != nullptr, error = OT_ERROR_NOT_FOUND); + + mInterpreter.OutputLine("0x%llx/%u", static_cast(discerner->mValue), discerner->mLength); + error = OT_ERROR_NONE; + } + else { otJoinerDiscerner discerner; @@ -56,35 +65,21 @@ otError Joiner::ProcessDiscerner(uint8_t aArgsLength, Arg aArgs[]) if (aArgs[0] == "clear") { - SuccessOrExit(error = otJoinerSetDiscerner(mInterpreter.mInstance, nullptr)); + error = otJoinerSetDiscerner(mInterpreter.mInstance, nullptr); } else { - VerifyOrExit(OT_ERROR_NONE == Interpreter::ParseJoinerDiscerner(aArgs[0], discerner), - error = OT_ERROR_INVALID_ARGS); - SuccessOrExit(error = otJoinerSetDiscerner(mInterpreter.mInstance, &discerner)); + SuccessOrExit(Interpreter::ParseJoinerDiscerner(aArgs[0], discerner)); + error = otJoinerSetDiscerner(mInterpreter.mInstance, &discerner); } } - else if (aArgsLength == 0) - { - const otJoinerDiscerner *discerner = otJoinerGetDiscerner(mInterpreter.mInstance); - - VerifyOrExit(discerner != nullptr, error = OT_ERROR_NOT_FOUND); - - mInterpreter.OutputLine("0x%llx/%u", static_cast(discerner->mValue), discerner->mLength); - } - else - { - error = OT_ERROR_INVALID_ARGS; - } exit: return error; } -otError Joiner::ProcessHelp(uint8_t aArgsLength, Arg aArgs[]) +otError Joiner::ProcessHelp(Arg aArgs[]) { - OT_UNUSED_VARIABLE(aArgsLength); OT_UNUSED_VARIABLE(aArgs); for (const Command &command : sCommands) @@ -95,9 +90,8 @@ otError Joiner::ProcessHelp(uint8_t aArgsLength, Arg aArgs[]) return OT_ERROR_NONE; } -otError Joiner::ProcessId(uint8_t aArgsLength, Arg aArgs[]) +otError Joiner::ProcessId(Arg aArgs[]) { - OT_UNUSED_VARIABLE(aArgsLength); OT_UNUSED_VARIABLE(aArgs); mInterpreter.OutputExtAddress(*otJoinerGetId(mInterpreter.mInstance)); @@ -106,28 +100,27 @@ otError Joiner::ProcessId(uint8_t aArgsLength, Arg aArgs[]) return OT_ERROR_NONE; } -otError Joiner::ProcessStart(uint8_t aArgsLength, Arg aArgs[]) +otError Joiner::ProcessStart(Arg aArgs[]) { - otError error; - const char *provisioningUrl = nullptr; + otError error; - VerifyOrExit(aArgsLength > 0, error = OT_ERROR_INVALID_ARGS); + VerifyOrExit(!aArgs[0].IsEmpty(), error = OT_ERROR_INVALID_ARGS); - if (aArgsLength > 1) - { - provisioningUrl = aArgs[1].GetCString(); - } - - error = otJoinerStart(mInterpreter.mInstance, aArgs[0].GetCString(), provisioningUrl, PACKAGE_NAME, - OPENTHREAD_CONFIG_PLATFORM_INFO, PACKAGE_VERSION, nullptr, &Joiner::HandleCallback, this); + error = otJoinerStart(mInterpreter.mInstance, + aArgs[0].GetCString(), // aPskd + aArgs[1].GetCString(), // aProvisioningUrl (nullptr if aArgs[1] is empty) + PACKAGE_NAME, // aVendorName + OPENTHREAD_CONFIG_PLATFORM_INFO, // aVendorModel + PACKAGE_VERSION, // aVendorSwVersion + nullptr, // aVendorData + &Joiner::HandleCallback, this); exit: return error; } -otError Joiner::ProcessStop(uint8_t aArgsLength, Arg aArgs[]) +otError Joiner::ProcessStop(Arg aArgs[]) { - OT_UNUSED_VARIABLE(aArgsLength); OT_UNUSED_VARIABLE(aArgs); otJoinerStop(mInterpreter.mInstance); @@ -135,17 +128,21 @@ otError Joiner::ProcessStop(uint8_t aArgsLength, Arg aArgs[]) return OT_ERROR_NONE; } -otError Joiner::Process(uint8_t aArgsLength, Arg aArgs[]) +otError Joiner::Process(Arg aArgs[]) { otError error = OT_ERROR_INVALID_COMMAND; const Command *command; - VerifyOrExit(aArgsLength != 0, IgnoreError(ProcessHelp(0, nullptr))); + if (aArgs[0].IsEmpty()) + { + IgnoreError(ProcessHelp(aArgs)); + ExitNow(); + } command = Utils::LookupTable::Find(aArgs[0].GetCString(), sCommands); VerifyOrExit(command != nullptr); - error = (this->*command->mHandler)(aArgsLength - 1, aArgs + 1); + error = (this->*command->mHandler)(aArgs + 1); exit: return error; diff --git a/src/cli/cli_joiner.hpp b/src/cli/cli_joiner.hpp index fd6add6c0..422cff34e 100644 --- a/src/cli/cli_joiner.hpp +++ b/src/cli/cli_joiner.hpp @@ -71,24 +71,23 @@ public: /** * This method interprets a list of CLI arguments. * - * @param[in] aArgsLength The number of elements in @p aArgs. * @param[in] aArgs A pointer to an array of command line arguments. * */ - otError Process(uint8_t aArgsLength, Arg aArgs[]); + otError Process(Arg aArgs[]); private: struct Command { const char *mName; - otError (Joiner::*mHandler)(uint8_t aArgsLength, Arg aArgs[]); + otError (Joiner::*mHandler)(Arg aArgs[]); }; - otError ProcessDiscerner(uint8_t aArgsLength, Arg aArgs[]); - otError ProcessHelp(uint8_t aArgsLength, Arg aArgs[]); - otError ProcessId(uint8_t aArgsLength, Arg aArgs[]); - otError ProcessStart(uint8_t aArgsLength, Arg aArgs[]); - otError ProcessStop(uint8_t aArgsLength, Arg aArgs[]); + otError ProcessDiscerner(Arg aArgs[]); + otError ProcessHelp(Arg aArgs[]); + otError ProcessId(Arg aArgs[]); + otError ProcessStart(Arg aArgs[]); + otError ProcessStop(Arg aArgs[]); static void HandleCallback(otError aError, void *aContext); void HandleCallback(otError aError); diff --git a/src/cli/cli_network_data.cpp b/src/cli/cli_network_data.cpp index 8459509e9..2aae3c6f1 100644 --- a/src/cli/cli_network_data.cpp +++ b/src/cli/cli_network_data.cpp @@ -203,9 +203,8 @@ void NetworkData::OutputService(const otServiceConfig &aConfig) mInterpreter.OutputLine(" %04x", aConfig.mServerConfig.mRloc16); } -otError NetworkData::ProcessHelp(uint8_t aArgsLength, Arg aArgs[]) +otError NetworkData::ProcessHelp(Arg aArgs[]) { - OT_UNUSED_VARIABLE(aArgsLength); OT_UNUSED_VARIABLE(aArgs); for (const Command &command : sCommands) @@ -217,9 +216,8 @@ otError NetworkData::ProcessHelp(uint8_t aArgsLength, Arg aArgs[]) } #if OPENTHREAD_CONFIG_BORDER_ROUTER_ENABLE || OPENTHREAD_CONFIG_TMF_NETDATA_SERVICE_ENABLE -otError NetworkData::ProcessRegister(uint8_t aArgsLength, Arg aArgs[]) +otError NetworkData::ProcessRegister(Arg aArgs[]) { - OT_UNUSED_VARIABLE(aArgsLength); OT_UNUSED_VARIABLE(aArgs); otError error = OT_ERROR_NONE; @@ -235,27 +233,24 @@ exit: } #endif -otError NetworkData::ProcessSteeringData(uint8_t aArgsLength, Arg aArgs[]) +otError NetworkData::ProcessSteeringData(Arg aArgs[]) { - otError error = OT_ERROR_INVALID_ARGS; + otError error; otExtAddress addr; otJoinerDiscerner discerner; - VerifyOrExit((aArgsLength > 1) && (aArgs[0] == "check")); - - discerner.mLength = 0; + VerifyOrExit(aArgs[0] == "check", error = OT_ERROR_INVALID_ARGS); error = Interpreter::ParseJoinerDiscerner(aArgs[1], discerner); if (error == OT_ERROR_NOT_FOUND) { - SuccessOrExit(error = aArgs[1].ParseAsHexString(addr.m8)); - } - else if (error != OT_ERROR_NONE) - { - ExitNow(); + discerner.mLength = 0; + error = aArgs[1].ParseAsHexString(addr.m8); } + SuccessOrExit(error); + if (discerner.mLength) { error = otNetDataSteeringDataCheckJoinerWithDiscerner(mInterpreter.mInstance, &discerner); @@ -323,11 +318,11 @@ exit: return error; } -otError NetworkData::ProcessShow(uint8_t aArgsLength, Arg aArgs[]) +otError NetworkData::ProcessShow(Arg aArgs[]) { - otError error; + otError error = OT_ERROR_INVALID_ARGS; - if (aArgsLength == 0) + if (aArgs[0].IsEmpty()) { OutputPrefixes(); OutputRoutes(); @@ -338,25 +333,25 @@ otError NetworkData::ProcessShow(uint8_t aArgsLength, Arg aArgs[]) { error = OutputBinary(); } - else - { - error = OT_ERROR_INVALID_ARGS; - } return error; } -otError NetworkData::Process(uint8_t aArgsLength, Arg aArgs[]) +otError NetworkData::Process(Arg aArgs[]) { otError error = OT_ERROR_INVALID_COMMAND; const Command *command; - VerifyOrExit(aArgsLength != 0, IgnoreError(ProcessHelp(0, nullptr))); + if (aArgs[0].IsEmpty()) + { + IgnoreError(ProcessHelp(aArgs)); + ExitNow(); + } command = Utils::LookupTable::Find(aArgs[0].GetCString(), sCommands); VerifyOrExit(command != nullptr); - error = (this->*command->mHandler)(aArgsLength - 1, aArgs + 1); + error = (this->*command->mHandler)(aArgs + 1); exit: return error; diff --git a/src/cli/cli_network_data.hpp b/src/cli/cli_network_data.hpp index be4858477..bb7903103 100644 --- a/src/cli/cli_network_data.hpp +++ b/src/cli/cli_network_data.hpp @@ -66,11 +66,10 @@ public: /** * This method interprets a list of CLI arguments. * - * @param[in] aArgsLength The number of elements in @p aArgs. * @param[in] aArgs An array of command line arguments. * */ - otError Process(uint8_t aArgsLength, Arg aArgs[]); + otError Process(Arg aArgs[]); /** * This method outputs the prefix config. @@ -100,15 +99,15 @@ private: struct Command { const char *mName; - otError (NetworkData::*mHandler)(uint8_t aArgsLength, Arg aArgs[]); + otError (NetworkData::*mHandler)(Arg aArgs[]); }; - otError ProcessHelp(uint8_t aArgsLength, Arg aArgs[]); + otError ProcessHelp(Arg aArgs[]); #if OPENTHREAD_CONFIG_BORDER_ROUTER_ENABLE || OPENTHREAD_CONFIG_TMF_NETDATA_SERVICE_ENABLE - otError ProcessRegister(uint8_t aArgsLength, Arg aArgs[]); + otError ProcessRegister(Arg aArgs[]); #endif - otError ProcessShow(uint8_t aArgsLength, Arg aArgs[]); - otError ProcessSteeringData(uint8_t aArgsLength, Arg aArgs[]); + otError ProcessShow(Arg aArgs[]); + otError ProcessSteeringData(Arg aArgs[]); otError OutputBinary(void); void OutputPrefixes(void); diff --git a/src/cli/cli_srp_client.cpp b/src/cli/cli_srp_client.cpp index b2875c512..7ccfc5ddf 100644 --- a/src/cli/cli_srp_client.cpp +++ b/src/cli/cli_srp_client.cpp @@ -66,17 +66,21 @@ SrpClient::SrpClient(Interpreter &aInterpreter) otSrpClientSetCallback(mInterpreter.mInstance, SrpClient::HandleCallback, this); } -otError SrpClient::Process(uint8_t aArgsLength, Arg aArgs[]) +otError SrpClient::Process(Arg aArgs[]) { otError error = OT_ERROR_INVALID_COMMAND; const Command *command; - VerifyOrExit(aArgsLength != 0, IgnoreError(ProcessHelp(0, nullptr))); + if (aArgs[0].IsEmpty()) + { + IgnoreError(ProcessHelp(aArgs)); + ExitNow(); + } command = Utils::LookupTable::Find(aArgs[0].GetCString(), sCommands); VerifyOrExit(command != nullptr); - error = (this->*command->mHandler)(aArgsLength - 1, aArgs + 1); + error = (this->*command->mHandler)(aArgs + 1); exit: return error; @@ -84,19 +88,17 @@ exit: #if OPENTHREAD_CONFIG_SRP_CLIENT_AUTO_START_API_ENABLE -otError SrpClient::ProcessAutoStart(uint8_t aArgsLength, Arg aArgs[]) +otError SrpClient::ProcessAutoStart(Arg aArgs[]) { otError error = OT_ERROR_NONE; bool enable; - if (aArgsLength == 0) + if (aArgs[0].IsEmpty()) { mInterpreter.OutputEnabledDisabledStatus(otSrpClientIsAutoStartModeEnabled(mInterpreter.mInstance)); ExitNow(); } - VerifyOrExit(aArgsLength == 1, error = OT_ERROR_INVALID_ARGS); - SuccessOrExit(error = Interpreter::ParseEnableOrDisable(aArgs[0], enable)); if (enable) @@ -114,26 +116,24 @@ exit: #endif // OPENTHREAD_CONFIG_SRP_CLIENT_AUTO_START_API_ENABLE -otError SrpClient::ProcessCallback(uint8_t aArgsLength, Arg aArgs[]) +otError SrpClient::ProcessCallback(Arg aArgs[]) { otError error = OT_ERROR_NONE; - if (aArgsLength == 0) + if (aArgs[0].IsEmpty()) { mInterpreter.OutputEnabledDisabledStatus(mCallbackEnabled); ExitNow(); } - VerifyOrExit(aArgsLength == 1, error = OT_ERROR_INVALID_ARGS); error = Interpreter::ParseEnableOrDisable(aArgs[0], mCallbackEnabled); exit: return error; } -otError SrpClient::ProcessHelp(uint8_t aArgsLength, Arg aArgs[]) +otError SrpClient::ProcessHelp(Arg aArgs[]) { - OT_UNUSED_VARIABLE(aArgsLength); OT_UNUSED_VARIABLE(aArgs); for (const Command &command : sCommands) @@ -144,11 +144,11 @@ otError SrpClient::ProcessHelp(uint8_t aArgsLength, Arg aArgs[]) return OT_ERROR_NONE; } -otError SrpClient::ProcessHost(uint8_t aArgsLength, Arg aArgs[]) +otError SrpClient::ProcessHost(Arg aArgs[]) { otError error = OT_ERROR_NONE; - if (aArgsLength == 0) + if (aArgs[0].IsEmpty()) { OutputHostInfo(0, *otSrpClientGetHostInfo(mInterpreter.mInstance)); ExitNow(); @@ -156,7 +156,7 @@ otError SrpClient::ProcessHost(uint8_t aArgsLength, Arg aArgs[]) if (aArgs[0] == "name") { - if (aArgsLength == 1) + if (aArgs[1].IsEmpty()) { const char *name = otSrpClientGetHostInfo(mInterpreter.mInstance)->mName; mInterpreter.OutputLine("%s", (name != nullptr) ? name : "(null)"); @@ -167,7 +167,7 @@ otError SrpClient::ProcessHost(uint8_t aArgsLength, Arg aArgs[]) uint16_t size; char * hostName; - VerifyOrExit(aArgsLength == 2, error = OT_ERROR_INVALID_ARGS); + VerifyOrExit(aArgs[2].IsEmpty(), error = OT_ERROR_INVALID_ARGS); hostName = otSrpClientBuffersGetHostNameString(mInterpreter.mInstance, &size); len = aArgs[1].GetLength(); @@ -187,13 +187,13 @@ otError SrpClient::ProcessHost(uint8_t aArgsLength, Arg aArgs[]) } else if (aArgs[0] == "state") { - VerifyOrExit(aArgsLength == 1, error = OT_ERROR_INVALID_ARGS); + VerifyOrExit(aArgs[1].IsEmpty(), error = OT_ERROR_INVALID_ARGS); mInterpreter.OutputLine("%s", otSrpClientItemStateToString(otSrpClientGetHostInfo(mInterpreter.mInstance)->mState)); } else if (aArgs[0] == "address") { - if (aArgsLength == 1) + if (aArgs[1].IsEmpty()) { const otSrpClientHostInfo *hostInfo = otSrpClientGetHostInfo(mInterpreter.mInstance); @@ -205,7 +205,7 @@ otError SrpClient::ProcessHost(uint8_t aArgsLength, Arg aArgs[]) } else { - uint8_t numAddresses = aArgsLength - 1; + uint8_t numAddresses = 0; otIp6Address addresses[kMaxHostAddresses]; uint8_t arrayLength; otIp6Address *hostAddressArray; @@ -218,11 +218,16 @@ otError SrpClient::ProcessHost(uint8_t aArgsLength, Arg aArgs[]) // a previous list before we know it is safe to set/change // the address list. - VerifyOrExit(numAddresses <= arrayLength, error = OT_ERROR_NO_BUFS); - - for (uint8_t index = 1; index < aArgsLength; index++) + if (arrayLength > kMaxHostAddresses) { - SuccessOrExit(error = aArgs[index].ParseAsIp6Address(addresses[index - 1])); + arrayLength = kMaxHostAddresses; + } + + for (Arg *arg = &aArgs[1]; !arg->IsEmpty(); arg++) + { + VerifyOrExit(numAddresses < arrayLength, error = OT_ERROR_NO_BUFS); + SuccessOrExit(error = arg->ParseAsIp6Address(addresses[numAddresses])); + numAddresses++; } SuccessOrExit(error = otSrpClientSetHostAddresses(mInterpreter.mInstance, addresses, numAddresses)); @@ -235,17 +240,17 @@ otError SrpClient::ProcessHost(uint8_t aArgsLength, Arg aArgs[]) { bool removeKeyLease = false; - if (aArgsLength > 1) + if (!aArgs[1].IsEmpty()) { - VerifyOrExit(aArgsLength == 2, error = OT_ERROR_INVALID_ARGS); SuccessOrExit(error = aArgs[1].ParseAsBool(removeKeyLease)); + VerifyOrExit(aArgs[2].IsEmpty(), error = OT_ERROR_INVALID_ARGS); } error = otSrpClientRemoveHostAndServices(mInterpreter.mInstance, removeKeyLease); } else if (aArgs[0] == "clear") { - VerifyOrExit(aArgsLength == 1, error = OT_ERROR_INVALID_ARGS); + VerifyOrExit(aArgs[1].IsEmpty(), error = OT_ERROR_INVALID_ARGS); otSrpClientClearHostAndServices(mInterpreter.mInstance); otSrpClientBuffersFreeAllServices(mInterpreter.mInstance); } @@ -258,23 +263,22 @@ exit: return error; } -otError SrpClient::ProcessLeaseInterval(uint8_t aArgsLength, Arg aArgs[]) +otError SrpClient::ProcessLeaseInterval(Arg aArgs[]) { - return mInterpreter.ProcessGetSet(aArgsLength, aArgs, otSrpClientGetLeaseInterval, otSrpClientSetLeaseInterval); + return mInterpreter.ProcessGetSet(aArgs, otSrpClientGetLeaseInterval, otSrpClientSetLeaseInterval); } -otError SrpClient::ProcessKeyLeaseInterval(uint8_t aArgsLength, Arg aArgs[]) +otError SrpClient::ProcessKeyLeaseInterval(Arg aArgs[]) { - return mInterpreter.ProcessGetSet(aArgsLength, aArgs, otSrpClientGetKeyLeaseInterval, - otSrpClientSetKeyLeaseInterval); + return mInterpreter.ProcessGetSet(aArgs, otSrpClientGetKeyLeaseInterval, otSrpClientSetKeyLeaseInterval); } -otError SrpClient::ProcessServer(uint8_t aArgsLength, Arg aArgs[]) +otError SrpClient::ProcessServer(Arg aArgs[]) { otError error = OT_ERROR_NONE; const otSockAddr *serverSockAddr = otSrpClientGetServerAddress(mInterpreter.mInstance); - if (aArgsLength == 0) + if (aArgs[0].IsEmpty()) { char string[OT_IP6_SOCK_ADDR_STRING_SIZE]; @@ -283,7 +287,7 @@ otError SrpClient::ProcessServer(uint8_t aArgsLength, Arg aArgs[]) ExitNow(); } - VerifyOrExit(aArgsLength == 1, error = OT_ERROR_INVALID_ARGS); + VerifyOrExit(aArgs[1].IsEmpty(), error = OT_ERROR_INVALID_ARGS); if (aArgs[0] == "address") { @@ -303,12 +307,12 @@ exit: return error; } -otError SrpClient::ProcessService(uint8_t aArgsLength, Arg aArgs[]) +otError SrpClient::ProcessService(Arg aArgs[]) { otError error = OT_ERROR_NONE; bool isRemove; - if (aArgsLength == 0) + if (aArgs[0].IsEmpty()) { OutputServiceList(0, otSrpClientGetServices(mInterpreter.mInstance)); ExitNow(); @@ -316,7 +320,7 @@ otError SrpClient::ProcessService(uint8_t aArgsLength, Arg aArgs[]) if (aArgs[0] == "add") { - error = ProcessServiceAdd(aArgsLength, aArgs); + error = ProcessServiceAdd(aArgs); } else if ((isRemove = (aArgs[0] == "remove")) || (aArgs[0] == "clear")) { @@ -324,7 +328,7 @@ otError SrpClient::ProcessService(uint8_t aArgsLength, Arg aArgs[]) const otSrpClientService *service; - VerifyOrExit(aArgsLength == 3, error = OT_ERROR_INVALID_ARGS); + VerifyOrExit(!aArgs[2].IsEmpty() && aArgs[3].IsEmpty(), error = OT_ERROR_INVALID_ARGS); for (service = otSrpClientGetServices(mInterpreter.mInstance); service != nullptr; service = service->mNext) { @@ -356,14 +360,14 @@ otError SrpClient::ProcessService(uint8_t aArgsLength, Arg aArgs[]) bool enable; - if (aArgsLength == 1) + if (aArgs[1].IsEmpty()) { mInterpreter.OutputEnabledDisabledStatus(otSrpClientIsServiceKeyRecordEnabled(mInterpreter.mInstance)); ExitNow(); } - VerifyOrExit(aArgsLength == 2, error = OT_ERROR_INVALID_ARGS); SuccessOrExit(error = Interpreter::ParseEnableOrDisable(aArgs[1], enable)); + VerifyOrExit(aArgs[2].IsEmpty(), error = OT_ERROR_INVALID_ARGS); otSrpClientSetServiceKeyRecordEnabled(mInterpreter.mInstance, enable); } #endif // OPENTHREAD_CONFIG_REFERENCE_DEVICE_ENABLE @@ -376,7 +380,7 @@ exit: return error; } -otError SrpClient::ProcessServiceAdd(uint8_t aArgsLength, Arg aArgs[]) +otError SrpClient::ProcessServiceAdd(Arg aArgs[]) { // `add` [priority] [weight] [txt] @@ -385,31 +389,32 @@ otError SrpClient::ProcessServiceAdd(uint8_t aArgsLength, Arg aArgs[]) char * string; otError error; - VerifyOrExit(4 <= aArgsLength && aArgsLength <= 7, error = OT_ERROR_INVALID_ARGS); - entry = otSrpClientBuffersAllocateService(mInterpreter.mInstance); VerifyOrExit(entry != nullptr, error = OT_ERROR_NO_BUFS); + SuccessOrExit(error = aArgs[3].ParseAsUint16(entry->mService.mPort)); + + // Successfully parsing aArgs[3] indicates that aArgs[1] and + // aArgs[2] are also non-empty. + string = otSrpClientBuffersGetServiceEntryInstanceNameString(entry, &size); SuccessOrExit(error = CopyString(string, size, aArgs[1].GetCString())); string = otSrpClientBuffersGetServiceEntryServiceNameString(entry, &size); SuccessOrExit(error = CopyString(string, size, aArgs[2].GetCString())); - SuccessOrExit(error = aArgs[3].ParseAsUint16(entry->mService.mPort)); - - if (aArgsLength >= 5) + if (!aArgs[4].IsEmpty()) { SuccessOrExit(error = aArgs[4].ParseAsUint16(entry->mService.mPriority)); } - if (aArgsLength >= 6) + if (!aArgs[5].IsEmpty()) { SuccessOrExit(error = aArgs[5].ParseAsUint16(entry->mService.mWeight)); } - if (aArgsLength >= 7) + if (!aArgs[6].IsEmpty()) { uint8_t *txtBuffer; @@ -417,6 +422,7 @@ otError SrpClient::ProcessServiceAdd(uint8_t aArgsLength, Arg aArgs[]) entry->mTxtEntry.mValueLength = size; SuccessOrExit(error = aArgs[6].ParseAsHexString(entry->mTxtEntry.mValueLength, txtBuffer)); + VerifyOrExit(aArgs[7].IsEmpty(), error = OT_ERROR_INVALID_ARGS); } else { @@ -480,15 +486,14 @@ void SrpClient::OutputService(uint8_t aIndentSize, const otSrpClientService &aSe aService.mPort, aService.mPriority, aService.mWeight); } -otError SrpClient::ProcessStart(uint8_t aArgsLength, Arg aArgs[]) +otError SrpClient::ProcessStart(Arg aArgs[]) { otError error = OT_ERROR_NONE; otSockAddr serverSockAddr; - VerifyOrExit(aArgsLength == 2, error = OT_ERROR_INVALID_ARGS); - SuccessOrExit(error = aArgs[0].ParseAsIp6Address(serverSockAddr.mAddress)); SuccessOrExit(error = aArgs[1].ParseAsUint16(serverSockAddr.mPort)); + VerifyOrExit(aArgs[2].IsEmpty(), error = OT_ERROR_INVALID_ARGS); error = otSrpClientStart(mInterpreter.mInstance, &serverSockAddr); @@ -496,13 +501,11 @@ exit: return error; } -otError SrpClient::ProcessState(uint8_t aArgsLength, Arg aArgs[]) +otError SrpClient::ProcessState(Arg aArgs[]) { - OT_UNUSED_VARIABLE(aArgs); - otError error = OT_ERROR_NONE; - VerifyOrExit(aArgsLength == 0, error = OT_ERROR_INVALID_ARGS); + VerifyOrExit(aArgs[0].IsEmpty(), error = OT_ERROR_INVALID_ARGS); mInterpreter.OutputEnabledDisabledStatus(otSrpClientIsRunning(mInterpreter.mInstance)); @@ -510,13 +513,11 @@ exit: return error; } -otError SrpClient::ProcessStop(uint8_t aArgsLength, Arg aArgs[]) +otError SrpClient::ProcessStop(Arg aArgs[]) { - OT_UNUSED_VARIABLE(aArgs); - otError error = OT_ERROR_NONE; - VerifyOrExit(aArgsLength == 0, error = OT_ERROR_INVALID_ARGS); + VerifyOrExit(aArgs[0].IsEmpty(), error = OT_ERROR_INVALID_ARGS); otSrpClientStop(mInterpreter.mInstance); exit: diff --git a/src/cli/cli_srp_client.hpp b/src/cli/cli_srp_client.hpp index e66d79bee..335fb35bd 100644 --- a/src/cli/cli_srp_client.hpp +++ b/src/cli/cli_srp_client.hpp @@ -70,11 +70,10 @@ public: /** * This method interprets a list of CLI arguments. * - * @param[in] aArgsLength The number of elements in @p aArgs. - * @param[in] aArgs A pointer to an array of command line arguments. + * @param[in] aArgs A pointer an array of command line arguments. * */ - otError Process(uint8_t aArgsLength, Arg aArgs[]); + otError Process(Arg aArgs[]); private: enum : uint8_t @@ -86,21 +85,21 @@ private: struct Command { const char *mName; - otError (SrpClient::*mHandler)(uint8_t aArgsLength, Arg aArgs[]); + otError (SrpClient::*mHandler)(Arg aArgs[]); }; - otError ProcessAutoStart(uint8_t aArgsLength, Arg aArgs[]); - otError ProcessCallback(uint8_t aArgsLength, Arg aArgs[]); - otError ProcessHelp(uint8_t aArgsLength, Arg aArgs[]); - otError ProcessHost(uint8_t aArgsLength, Arg aArgs[]); - otError ProcessLeaseInterval(uint8_t aArgsLength, Arg aArgs[]); - otError ProcessKeyLeaseInterval(uint8_t aArgsLength, Arg aArgs[]); - otError ProcessServer(uint8_t aArgsLength, Arg aArgs[]); - otError ProcessService(uint8_t aArgsLength, Arg aArgs[]); - otError ProcessServiceAdd(uint8_t aArgsLength, Arg aArgs[]); - otError ProcessStart(uint8_t aArgsLength, Arg aArgs[]); - otError ProcessState(uint8_t aArgsLength, Arg aArgs[]); - otError ProcessStop(uint8_t aArgsLength, Arg aArgs[]); + otError ProcessAutoStart(Arg aArgs[]); + otError ProcessCallback(Arg aArgs[]); + otError ProcessHelp(Arg aArgs[]); + otError ProcessHost(Arg aArgs[]); + otError ProcessLeaseInterval(Arg aArgs[]); + otError ProcessKeyLeaseInterval(Arg aArgs[]); + otError ProcessServer(Arg aArgs[]); + otError ProcessService(Arg aArgs[]); + otError ProcessServiceAdd(Arg aArgs[]); + otError ProcessStart(Arg aArgs[]); + otError ProcessState(Arg aArgs[]); + otError ProcessStop(Arg aArgs[]); void OutputHostInfo(uint8_t aIndentSize, const otSrpClientHostInfo &aHostInfo); void OutputServiceList(uint8_t aIndentSize, const otSrpClientService *aServices); diff --git a/src/cli/cli_srp_server.cpp b/src/cli/cli_srp_server.cpp index 9913db5f6..2cd33e824 100644 --- a/src/cli/cli_srp_server.cpp +++ b/src/cli/cli_srp_server.cpp @@ -45,42 +45,44 @@ namespace Cli { constexpr SrpServer::Command SrpServer::sCommands[]; -otError SrpServer::Process(uint8_t aArgsLength, Arg aArgs[]) +otError SrpServer::Process(Arg aArgs[]) { otError error = OT_ERROR_INVALID_COMMAND; const Command *command; - VerifyOrExit(aArgsLength != 0, IgnoreError(ProcessHelp(0, nullptr))); + if (aArgs[0].IsEmpty()) + { + IgnoreError(ProcessHelp(aArgs)); + ExitNow(); + } command = Utils::LookupTable::Find(aArgs[0].GetCString(), sCommands); VerifyOrExit(command != nullptr); - error = (this->*command->mHandler)(aArgsLength - 1, aArgs + 1); + error = (this->*command->mHandler)(aArgs + 1); exit: return error; } -otError SrpServer::ProcessDomain(uint8_t aArgsLength, Arg aArgs[]) +otError SrpServer::ProcessDomain(Arg aArgs[]) { otError error = OT_ERROR_NONE; - if (aArgsLength > 0) - { - SuccessOrExit(error = otSrpServerSetDomain(mInterpreter.mInstance, aArgs[0].GetCString())); - } - else + if (aArgs[0].IsEmpty()) { mInterpreter.OutputLine("%s", otSrpServerGetDomain(mInterpreter.mInstance)); } + else + { + error = otSrpServerSetDomain(mInterpreter.mInstance, aArgs[0].GetCString()); + } -exit: return error; } -otError SrpServer::ProcessEnable(uint8_t aArgsLength, Arg aArgs[]) +otError SrpServer::ProcessEnable(Arg aArgs[]) { - OT_UNUSED_VARIABLE(aArgsLength); OT_UNUSED_VARIABLE(aArgs); otSrpServerSetEnabled(mInterpreter.mInstance, /* aEnabled */ true); @@ -88,9 +90,8 @@ otError SrpServer::ProcessEnable(uint8_t aArgsLength, Arg aArgs[]) return OT_ERROR_NONE; } -otError SrpServer::ProcessDisable(uint8_t aArgsLength, Arg aArgs[]) +otError SrpServer::ProcessDisable(Arg aArgs[]) { - OT_UNUSED_VARIABLE(aArgsLength); OT_UNUSED_VARIABLE(aArgs); otSrpServerSetEnabled(mInterpreter.mInstance, /* aEnabled */ false); @@ -98,20 +99,12 @@ otError SrpServer::ProcessDisable(uint8_t aArgsLength, Arg aArgs[]) return OT_ERROR_NONE; } -otError SrpServer::ProcessLease(uint8_t aArgsLength, Arg aArgs[]) +otError SrpServer::ProcessLease(Arg aArgs[]) { otError error = OT_ERROR_NONE; otSrpServerLeaseConfig leaseConfig; - if (aArgsLength == 4) - { - SuccessOrExit(error = aArgs[0].ParseAsUint32(leaseConfig.mMinLease)); - SuccessOrExit(error = aArgs[1].ParseAsUint32(leaseConfig.mMaxLease)); - SuccessOrExit(error = aArgs[2].ParseAsUint32(leaseConfig.mMinKeyLease)); - SuccessOrExit(error = aArgs[3].ParseAsUint32(leaseConfig.mMaxKeyLease)); - error = otSrpServerSetLeaseConfig(mInterpreter.mInstance, &leaseConfig); - } - else if (aArgsLength == 0) + if (aArgs[0].IsEmpty()) { otSrpServerGetLeaseConfig(mInterpreter.mInstance, &leaseConfig); mInterpreter.OutputLine("min lease: %u", leaseConfig.mMinLease); @@ -121,21 +114,25 @@ otError SrpServer::ProcessLease(uint8_t aArgsLength, Arg aArgs[]) } else { - ExitNow(error = OT_ERROR_INVALID_ARGS); + SuccessOrExit(error = aArgs[0].ParseAsUint32(leaseConfig.mMinLease)); + SuccessOrExit(error = aArgs[1].ParseAsUint32(leaseConfig.mMaxLease)); + SuccessOrExit(error = aArgs[2].ParseAsUint32(leaseConfig.mMinKeyLease)); + SuccessOrExit(error = aArgs[3].ParseAsUint32(leaseConfig.mMaxKeyLease)); + VerifyOrExit(aArgs[4].IsEmpty(), error = OT_ERROR_INVALID_ARGS); + + error = otSrpServerSetLeaseConfig(mInterpreter.mInstance, &leaseConfig); } exit: return error; } -otError SrpServer::ProcessHost(uint8_t aArgsLength, Arg aArgs[]) +otError SrpServer::ProcessHost(Arg aArgs[]) { - OT_UNUSED_VARIABLE(aArgs); - otError error = OT_ERROR_NONE; const otSrpServerHost *host; - VerifyOrExit(aArgsLength == 0, error = OT_ERROR_INVALID_ARGS); + VerifyOrExit(aArgs[0].IsEmpty(), error = OT_ERROR_INVALID_ARGS); host = nullptr; while ((host = otSrpServerGetNextHost(mInterpreter.mInstance, host)) != nullptr) @@ -192,14 +189,12 @@ void SrpServer::OutputHostAddresses(const otSrpServerHost *aHost) mInterpreter.OutputFormat("]"); } -otError SrpServer::ProcessService(uint8_t aArgsLength, Arg aArgs[]) +otError SrpServer::ProcessService(Arg aArgs[]) { - OT_UNUSED_VARIABLE(aArgs); - otError error = OT_ERROR_NONE; const otSrpServerHost *host; - VerifyOrExit(aArgsLength == 0, error = OT_ERROR_INVALID_ARGS); + VerifyOrExit(aArgs[0].IsEmpty(), error = OT_ERROR_INVALID_ARGS); host = nullptr; while ((host = otSrpServerGetNextHost(mInterpreter.mInstance, host)) != nullptr) @@ -240,9 +235,8 @@ exit: return error; } -otError SrpServer::ProcessHelp(uint8_t aArgsLength, Arg aArgs[]) +otError SrpServer::ProcessHelp(Arg aArgs[]) { - OT_UNUSED_VARIABLE(aArgsLength); OT_UNUSED_VARIABLE(aArgs); for (const Command &command : sCommands) diff --git a/src/cli/cli_srp_server.hpp b/src/cli/cli_srp_server.hpp index 1e8e4c46b..6f5dee766 100644 --- a/src/cli/cli_srp_server.hpp +++ b/src/cli/cli_srp_server.hpp @@ -71,29 +71,28 @@ public: /** * This method interprets a list of CLI arguments. * - * @param[in] aArgsLength The number of elements in @p aArgs. * @param[in] aArgs A pointer to an array of command line arguments. * * @retval OT_ERROR_NONE Successfully executed the CLI command. * @retval ... Failed to execute the CLI command. * */ - otError Process(uint8_t aArgsLength, Arg aArgs[]); + otError Process(Arg aArgs[]); private: struct Command { const char *mName; - otError (SrpServer::*mHandler)(uint8_t aArgsLength, Arg aArgs[]); + otError (SrpServer::*mHandler)(Arg aArgs[]); }; - otError ProcessDomain(uint8_t aArgsLength, Arg aArgs[]); - otError ProcessEnable(uint8_t aArgsLength, Arg aArgs[]); - otError ProcessDisable(uint8_t aArgsLength, Arg aArgs[]); - otError ProcessLease(uint8_t aArgsLength, Arg aArgs[]); - otError ProcessHost(uint8_t aArgsLength, Arg aArgs[]); - otError ProcessService(uint8_t aArgsLength, Arg aArgs[]); - otError ProcessHelp(uint8_t aArgsLength, Arg aArgs[]); + otError ProcessDomain(Arg aArgs[]); + otError ProcessEnable(Arg aArgs[]); + otError ProcessDisable(Arg aArgs[]); + otError ProcessLease(Arg aArgs[]); + otError ProcessHost(Arg aArgs[]); + otError ProcessService(Arg aArgs[]); + otError ProcessHelp(Arg aArgs[]); void OutputHostAddresses(const otSrpServerHost *aHost); diff --git a/src/cli/cli_udp.cpp b/src/cli/cli_udp.cpp index 02d3e1219..8e0a4737a 100644 --- a/src/cli/cli_udp.cpp +++ b/src/cli/cli_udp.cpp @@ -51,9 +51,8 @@ UdpExample::UdpExample(Interpreter &aInterpreter) memset(&mSocket, 0, sizeof(mSocket)); } -otError UdpExample::ProcessHelp(uint8_t aArgsLength, Arg aArgs[]) +otError UdpExample::ProcessHelp(Arg aArgs[]) { - OT_UNUSED_VARIABLE(aArgsLength); OT_UNUSED_VARIABLE(aArgs); for (const Command &command : sCommands) @@ -64,15 +63,14 @@ otError UdpExample::ProcessHelp(uint8_t aArgsLength, Arg aArgs[]) return OT_ERROR_NONE; } -otError UdpExample::ProcessBind(uint8_t aArgsLength, Arg aArgs[]) +otError UdpExample::ProcessBind(Arg aArgs[]) { otError error; otSockAddr sockaddr; - VerifyOrExit(aArgsLength == 2, error = OT_ERROR_INVALID_ARGS); - SuccessOrExit(error = aArgs[0].ParseAsIp6Address(sockaddr.mAddress)); SuccessOrExit(error = aArgs[1].ParseAsUint16(sockaddr.mPort)); + VerifyOrExit(aArgs[2].IsEmpty(), error = OT_ERROR_INVALID_ARGS); error = otUdpBind(mInterpreter.mInstance, &mSocket, &sockaddr); @@ -80,15 +78,14 @@ exit: return error; } -otError UdpExample::ProcessConnect(uint8_t aArgsLength, Arg aArgs[]) +otError UdpExample::ProcessConnect(Arg aArgs[]) { otError error; otSockAddr sockaddr; - VerifyOrExit(aArgsLength == 2, error = OT_ERROR_INVALID_ARGS); - SuccessOrExit(error = aArgs[0].ParseAsIp6Address(sockaddr.mAddress)); SuccessOrExit(error = aArgs[1].ParseAsUint16(sockaddr.mPort)); + VerifyOrExit(aArgs[2].IsEmpty(), error = OT_ERROR_INVALID_ARGS); error = otUdpConnect(mInterpreter.mInstance, &mSocket, &sockaddr); @@ -96,17 +93,15 @@ exit: return error; } -otError UdpExample::ProcessClose(uint8_t aArgsLength, Arg aArgs[]) +otError UdpExample::ProcessClose(Arg aArgs[]) { - OT_UNUSED_VARIABLE(aArgsLength); OT_UNUSED_VARIABLE(aArgs); return otUdpClose(mInterpreter.mInstance, &mSocket); } -otError UdpExample::ProcessOpen(uint8_t aArgsLength, Arg aArgs[]) +otError UdpExample::ProcessOpen(Arg aArgs[]) { - OT_UNUSED_VARIABLE(aArgsLength); OT_UNUSED_VARIABLE(aArgs); otError error; @@ -118,12 +113,11 @@ exit: return error; } -otError UdpExample::ProcessSend(uint8_t aArgsLength, Arg aArgs[]) +otError UdpExample::ProcessSend(Arg aArgs[]) { otError error = OT_ERROR_NONE; otMessage * message = nullptr; otMessageInfo messageInfo; - uint8_t argIndex = 0; otMessageSettings messageSettings = {mLinkSecurityEnabled, OT_MESSAGE_PRIORITY_NORMAL}; memset(&messageInfo, 0, sizeof(messageInfo)); @@ -135,47 +129,43 @@ otError UdpExample::ProcessSend(uint8_t aArgsLength, Arg aArgs[]) // send // send - VerifyOrExit(aArgsLength >= 1 && aArgsLength <= 4, error = OT_ERROR_INVALID_ARGS); - - if (aArgsLength > 2) + if (!aArgs[2].IsEmpty()) { - SuccessOrExit(error = aArgs[argIndex++].ParseAsIp6Address(messageInfo.mPeerAddr)); - SuccessOrExit(error = aArgs[argIndex++].ParseAsUint16(messageInfo.mPeerPort)); + SuccessOrExit(error = aArgs[0].ParseAsIp6Address(messageInfo.mPeerAddr)); + SuccessOrExit(error = aArgs[1].ParseAsUint16(messageInfo.mPeerPort)); + aArgs += 2; } message = otUdpNewMessage(mInterpreter.mInstance, &messageSettings); VerifyOrExit(message != nullptr, error = OT_ERROR_NO_BUFS); - if (aArgs[argIndex] == "-s") + if (aArgs[0] == "-s") { // Auto-generated payload with a given length uint16_t payloadLength; - argIndex++; - VerifyOrExit(argIndex < aArgsLength, error = OT_ERROR_INVALID_ARGS); - SuccessOrExit(error = aArgs[argIndex].ParseAsUint16(payloadLength)); + SuccessOrExit(error = aArgs[1].ParseAsUint16(payloadLength)); SuccessOrExit(error = PrepareAutoGeneratedPayload(*message, payloadLength)); } - else if (aArgs[argIndex] == "-x") + else if (aArgs[0] == "-x") { // Binary hex data payload - argIndex++; - VerifyOrExit(argIndex < aArgsLength, error = OT_ERROR_INVALID_ARGS); - SuccessOrExit(error = PrepareHexStringPaylod(*message, aArgs[argIndex].GetCString())); + VerifyOrExit(!aArgs[1].IsEmpty(), error = OT_ERROR_INVALID_ARGS); + SuccessOrExit(error = PrepareHexStringPaylod(*message, aArgs[1].GetCString())); } else { // Text payload (same as without specifying the type) - if (aArgs[argIndex] == "-t") + if (aArgs[0] == "-t") { - argIndex++; + aArgs++; } - VerifyOrExit(argIndex < aArgsLength, error = OT_ERROR_INVALID_ARGS); - SuccessOrExit(error = otMessageAppend(message, aArgs[argIndex].GetCString(), aArgs[argIndex].GetLength())); + VerifyOrExit(!aArgs[0].IsEmpty(), error = OT_ERROR_INVALID_ARGS); + SuccessOrExit(error = otMessageAppend(message, aArgs[0].GetCString(), aArgs[0].GetLength())); } SuccessOrExit(error = otUdpSend(mInterpreter.mInstance, &mSocket, message, &messageInfo)); @@ -191,11 +181,11 @@ exit: return error; } -otError UdpExample::ProcessLinkSecurity(uint8_t aArgsLength, Arg aArgs[]) +otError UdpExample::ProcessLinkSecurity(Arg aArgs[]) { otError error = OT_ERROR_NONE; - if (aArgsLength == 0) + if (aArgs[0].IsEmpty()) { mInterpreter.OutputEnabledDisabledStatus(mLinkSecurityEnabled); } @@ -264,17 +254,21 @@ exit: return error; } -otError UdpExample::Process(uint8_t aArgsLength, Arg aArgs[]) +otError UdpExample::Process(Arg aArgs[]) { otError error = OT_ERROR_INVALID_ARGS; const Command *command; - VerifyOrExit(aArgsLength != 0, IgnoreError(ProcessHelp(0, nullptr))); + if (aArgs[0].IsEmpty()) + { + IgnoreError(ProcessHelp(aArgs)); + ExitNow(); + } command = Utils::LookupTable::Find(aArgs[0].GetCString(), sCommands); VerifyOrExit(command != nullptr, error = OT_ERROR_INVALID_COMMAND); - error = (this->*command->mHandler)(aArgsLength - 1, aArgs + 1); + error = (this->*command->mHandler)(aArgs + 1); exit: return error; diff --git a/src/cli/cli_udp.hpp b/src/cli/cli_udp.hpp index a81a20341..9b8d590d7 100644 --- a/src/cli/cli_udp.hpp +++ b/src/cli/cli_udp.hpp @@ -66,26 +66,25 @@ public: /** * This method interprets a list of CLI arguments. * - * @param[in] aArgsLength The number of elements in @p aArgs. * @param[in] aArgs An array of command line arguments. * */ - otError Process(uint8_t aArgsLength, Arg aArgs[]); + otError Process(Arg aArgs[]); private: struct Command { const char *mName; - otError (UdpExample::*mHandler)(uint8_t aArgsLength, Arg aArgs[]); + otError (UdpExample::*mHandler)(Arg aArgs[]); }; - otError ProcessHelp(uint8_t aArgsLength, Arg aArgs[]); - otError ProcessBind(uint8_t aArgsLength, Arg aArgs[]); - otError ProcessClose(uint8_t aArgsLength, Arg aArgs[]); - otError ProcessConnect(uint8_t aArgsLength, Arg aArgs[]); - otError ProcessOpen(uint8_t aArgsLength, Arg aArgs[]); - otError ProcessSend(uint8_t aArgsLength, Arg aArgs[]); - otError ProcessLinkSecurity(uint8_t aArgsLength, Arg aArgs[]); + otError ProcessHelp(Arg aArgs[]); + otError ProcessBind(Arg aArgs[]); + otError ProcessClose(Arg aArgs[]); + otError ProcessConnect(Arg aArgs[]); + otError ProcessOpen(Arg aArgs[]); + otError ProcessSend(Arg aArgs[]); + otError ProcessLinkSecurity(Arg aArgs[]); static otError PrepareAutoGeneratedPayload(otMessage &aMessage, uint16_t aPayloadLength); static otError PrepareHexStringPaylod(otMessage &aMessage, const char *aHexString); diff --git a/src/core/diags/factory_diags.cpp b/src/core/diags/factory_diags.cpp index 6b6d109f5..a1737fe86 100644 --- a/src/core/diags/factory_diags.cpp +++ b/src/core/diags/factory_diags.cpp @@ -520,10 +520,11 @@ Error Diags::ParseLong(char *aString, long &aLong) Error Diags::ParseCmd(char *aString, uint8_t &aArgsLength, char *aArgs[]) { Error error; - Utils::CmdLineParser::Arg args[kMaxArgs]; + Utils::CmdLineParser::Arg args[kMaxArgs + 1]; - SuccessOrExit(error = Utils::CmdLineParser::ParseCmd(aString, aArgsLength, args, aArgsLength)); - Utils::CmdLineParser::Arg::CopyArgsToStringArray(args, aArgsLength, aArgs); + SuccessOrExit(error = Utils::CmdLineParser::ParseCmd(aString, args)); + aArgsLength = Utils::CmdLineParser::Arg::GetArgsLength(args); + Utils::CmdLineParser::Arg::CopyArgsToStringArray(args, aArgs); exit: return error; @@ -544,8 +545,7 @@ void Diags::ProcessLine(const char *aString, char *aOutput, size_t aOutputMaxLen VerifyOrExit(StringLength(aString, kMaxCommandBuffer) < kMaxCommandBuffer, error = kErrorNoBufs); strcpy(buffer, aString); - argCount = kMaxArgs; - error = ParseCmd(buffer, argCount, args); + error = ParseCmd(buffer, argCount, args); exit: diff --git a/src/core/utils/parse_cmdline.cpp b/src/core/utils/parse_cmdline.cpp index 5026b39a8..8a500ff84 100644 --- a/src/core/utils/parse_cmdline.cpp +++ b/src/core/utils/parse_cmdline.cpp @@ -85,12 +85,11 @@ exit: return error; } -Error ParseCmd(char *aCommandString, uint8_t &aArgsLength, Arg *aArgs, uint8_t aArgsLengthMax) +Error ParseCmd(char *aCommandString, Arg aArgs[], uint8_t aArgsMaxLength) { - Error error = kErrorNone; - char *cmd; - - aArgsLength = 0; + Error error = kErrorNone; + uint8_t index = 0; + char * cmd; for (cmd = aCommandString; *cmd; cmd++) { @@ -104,15 +103,23 @@ Error ParseCmd(char *aCommandString, uint8_t &aArgsLength, Arg *aArgs, uint8_t a *cmd = '\0'; } - if ((*cmd != '\0') && ((aArgsLength == 0) || (*(cmd - 1) == '\0'))) + if ((*cmd != '\0') && ((index == 0) || (*(cmd - 1) == '\0'))) { - VerifyOrExit(aArgsLength < aArgsLengthMax, error = kErrorInvalidArgs); + if (index == aArgsMaxLength - 1) + { + error = kErrorInvalidArgs; + break; + } - aArgs[aArgsLength++].SetCString(cmd); + aArgs[index++].SetCString(cmd); } } -exit: + while (index < aArgsMaxLength) + { + aArgs[index++].Clear(); + } + return error; } @@ -158,6 +165,8 @@ Error ParseAsUint64(const char *aString, uint64_t &aUint64) kMaxDecBeforeOverlow = (0xffffffffffffffffULL / 10), }; + VerifyOrExit(aString != nullptr, error = kErrorInvalidArgs); + if (cur[0] == '0' && (cur[1] == 'x' || cur[1] == 'X')) { cur += 2; @@ -215,6 +224,8 @@ Error ParseAsInt32(const char *aString, int32_t &aInt32) uint64_t value; bool isNegavtive = false; + VerifyOrExit(aString != nullptr, error = kErrorInvalidArgs); + if (*aString == '-') { aString++; @@ -248,6 +259,11 @@ exit: } #if OPENTHREAD_FTD || OPENTHREAD_MTD +Error ParseAsIp6Address(const char *aString, otIp6Address &aAddress) +{ + return (aString != nullptr) ? otIp6AddressFromString(aString, &aAddress) : kErrorInvalidArgs; +} + Error ParseAsIp6Prefix(const char *aString, otIp6Prefix &aPrefix) { enum : uint8_t @@ -259,6 +275,8 @@ Error ParseAsIp6Prefix(const char *aString, otIp6Prefix &aPrefix) char string[kMaxIp6AddressStringSize]; const char *prefixLengthStr; + VerifyOrExit(aString != nullptr); + prefixLengthStr = StringFind(aString, '/'); VerifyOrExit(prefixLengthStr != nullptr); @@ -359,14 +377,39 @@ Error ParseAsHexStringSegment(const char *&aString, uint16_t &aSize, uint8_t *aB return ParseHexString(aString, aSize, aBuffer, kModeAllowPartial); } -void Arg::CopyArgsToStringArray(Arg aArgs[], uint8_t aArgsLength, char *aStrings[]) +//--------------------------------------------------------------------------------------------------------------------- +// Arg class + +uint16_t Arg::GetLength(void) const { - for (uint8_t i = 0; i < aArgsLength; i++) + return IsEmpty() ? 0 : static_cast(strlen(mString)); +} + +bool Arg::operator==(const char *aString) const +{ + return !IsEmpty() && (strcmp(mString, aString) == 0); +} + +void Arg::CopyArgsToStringArray(Arg aArgs[], char *aStrings[]) +{ + for (uint8_t i = 0; !aArgs[i].IsEmpty(); i++) { aStrings[i] = aArgs[i].GetCString(); } } +uint8_t Arg::GetArgsLength(Arg aArgs[]) +{ + uint8_t length = 0; + + while (!aArgs[length].IsEmpty()) + { + length++; + } + + return length; +} + } // namespace CmdLineParser } // namespace Utils } // namespace ot diff --git a/src/core/utils/parse_cmdline.hpp b/src/core/utils/parse_cmdline.hpp index dda5fe696..88b0cc8c4 100644 --- a/src/core/utils/parse_cmdline.hpp +++ b/src/core/utils/parse_cmdline.hpp @@ -180,10 +180,7 @@ otError ParseAsBool(const char *aString, bool &aBool); * @retval kErrorInvalidArgs The string does not contain valid IPv6 address. * */ -inline otError ParseAsIp6Address(const char *aString, otIp6Address &aAddress) -{ - return otIp6AddressFromString(aString, &aAddress); -} +otError ParseAsIp6Address(const char *aString, otIp6Address &aAddress); /** * This function parses a string as an IPv6 prefix. @@ -295,17 +292,32 @@ class Arg { public: /** - * This method returns the length (number of characters) in the argument C string. - * - * @returns The argument string length. + * This method clears the argument. * */ - uint16_t GetLength(void) const { return static_cast(strlen(mString)); } + void Clear(void) { mString = nullptr; } + + /** + * This method indicates whether or not the argument is empty (i.e., reached the end of argument list). + * + * @retval TRUE The argument is empty. + * @retval FALSE The argument is not empty. + * + */ + bool IsEmpty(void) const { return (mString == nullptr); } + + /** + * This method returns the length (number of characters) in the argument C string. + * + * @returns The argument string length if argument is not empty, zero otherwise. + * + */ + uint16_t GetLength(void) const; /** * This method gets the argument as a C string. * - * @returns A pointer to the argument as a C string. + * @returns A pointer to the argument as a C string, or `nullptr` if argument is empty. * */ const char *GetCString(void) const { return mString; } @@ -313,7 +325,7 @@ public: /** * This method gets the argument as C string. * - * @returns A pointer to the argument as a C string. + * @returns A pointer to the argument as a C string, or `nullptr` if argument is empty. * */ char *GetCString(void) { return mString; } @@ -329,21 +341,24 @@ public: /** * This method overload the operator `==` to evaluate whether the argument is equal to a given C string. * - * @param[in] aString The C string to compare with. + * If the argument is empty (`IsEmpty()` is `true`) then comparing it using operator `==` with any C string will + * return false. * - * @retval TRUE If the argument is equal to @p aString. - * @retval FALSE If the argument is not equal to @p aString. + * @param[in] aString The C string to compare with (MUST not be `nullptr`). + * + * @retval TRUE If the argument is not empty and is equal to @p aString. + * @retval FALSE If the argument is not equal to @p aString, or if the argument is empty. * */ - bool operator==(const char *aString) const { return (strcmp(mString, aString) == 0); } + bool operator==(const char *aString) const; /** * This method overload the operator `!=` to evaluate whether the argument is unequal to a given C string. * - * @param[in] aString The C string to compare with. + * @param[in] aString The C string to compare with (MUST not be `nullptr`). * - * @retval TRUE If the argument is not equal to @p aString. - * @retval FALSE If the argument is equal to @p aString. + * @retval TRUE If the argument is not equal to @p aString, or if the argument is empty. + * @retval FALSE If the argument is not empty and equal to @p aString. * */ bool operator!=(const char *aString) const { return !(*this == aString); } @@ -356,7 +371,7 @@ public: * @param[out] aUint8 A reference to an `uint8_t` variable to output the parsed value. * * @retval kErrorNone The argument was parsed successfully. - * @retval kErrorInvalidArgs The argument does not contain valid number (e.g., value out of range). + * @retval kErrorInvalidArgs The argument is empty or does not contain valid number (e.g., value out of range). * */ otError ParseAsUint8(uint8_t &aUint8) const { return CmdLineParser::ParseAsUint8(mString, aUint8); } @@ -369,7 +384,7 @@ public: * @param[out] aUint16 A reference to an `uint16_t` variable to output the parsed value. * * @retval kErrorNone The argument was parsed successfully. - * @retval kErrorInvalidArgs The argument does not contain valid number (e.g., value out of range). + * @retval kErrorInvalidArgs The argument is empty or does not contain valid number (e.g., value out of range). * */ otError ParseAsUint16(uint16_t &aUint16) const { return CmdLineParser::ParseAsUint16(mString, aUint16); } @@ -382,7 +397,7 @@ public: * @param[out] aUint32 A reference to an `uint32_t` variable to output the parsed value. * * @retval kErrorNone The argument was parsed successfully. - * @retval kErrorInvalidArgs The argument does not contain valid number (e.g., value out of range). + * @retval kErrorInvalidArgs The argument is empty or does not contain valid number (e.g., value out of range). * */ otError ParseAsUint32(uint32_t &aUint32) const { return CmdLineParser::ParseAsUint32(mString, aUint32); } @@ -395,7 +410,7 @@ public: * @param[out] aUint64 A reference to an `uint64_t` variable to output the parsed value. * * @retval kErrorNone The argument was parsed successfully. - * @retval kErrorInvalidArgs The argument does not contain valid number (e.g., value out of range). + * @retval kErrorInvalidArgs The argument is empty or does not contain valid number (e.g., value out of range). * */ otError ParseAsUint64(uint64_t &aUint64) const { return CmdLineParser::ParseAsUint64(mString, aUint64); } @@ -409,7 +424,7 @@ public: * @param[out] aInt8 A reference to an `int8_t` variable to output the parsed value. * * @retval kErrorNone The argument was parsed successfully. - * @retval kErrorInvalidArgs The argument does not contain valid number (e.g., value out of range). + * @retval kErrorInvalidArgs The argument is empty or does not contain valid number (e.g., value out of range). * */ otError ParseAsInt8(int8_t &aInt8) const { return CmdLineParser::ParseAsInt8(mString, aInt8); } @@ -423,7 +438,7 @@ public: * @param[out] aInt16 A reference to an `int16_t` variable to output the parsed value. * * @retval kErrorNone The argument was parsed successfully. - * @retval kErrorInvalidArgs The argument does not contain valid number (e.g., value out of range). + * @retval kErrorInvalidArgs The argument is empty or does not contain valid number (e.g., value out of range). * */ otError ParseAsInt16(int16_t &aInt16) const { return CmdLineParser::ParseAsInt16(mString, aInt16); } @@ -437,7 +452,7 @@ public: * @param[out] aInt32 A reference to an `int32_t` variable to output the parsed value. * * @retval kErrorNone The argument was parsed successfully. - * @retval kErrorInvalidArgs The argument does not contain valid number (e.g., value out of range). + * @retval kErrorInvalidArgs The argument is empty or does not contain valid number (e.g., value out of range). * */ otError ParseAsInt32(int32_t &aInt32) const { return CmdLineParser::ParseAsInt32(mString, aInt32); } @@ -450,7 +465,7 @@ public: * @param[out] aBool A reference to a `bool` variable to output the parsed value. * * @retval kErrorNone The argument was parsed successfully. - * @retval kErrorInvalidArgs The argument does not contain valid number. + * @retval kErrorInvalidArgs The argument is empty or does not contain valid number. * */ otError ParseAsBool(bool &aBool) const { return CmdLineParser::ParseAsBool(mString, aBool); } @@ -462,7 +477,7 @@ public: * @param[out] aAddress A reference to an `otIp6Address` to output the parsed IPv6 address. * * @retval kErrorNone The argument was parsed successfully. - * @retval kErrorInvalidArgs The argument does not contain valid IPv6 address. + * @retval kErrorInvalidArgs The argument is empty or does not contain valid IPv6 address. * */ otError ParseAsIp6Address(otIp6Address &aAddress) const @@ -478,7 +493,7 @@ public: * @param[out] aPrefix A reference to an `otIp6Prefix` to output the parsed IPv6 prefix. * * @retval kErrorNone The argument was parsed successfully. - * @retval kErrorInvalidArgs The argument does not contain a valid IPv6 prefix. + * @retval kErrorInvalidArgs The argument is empty or does not contain a valid IPv6 prefix. * */ otError ParseAsIp6Prefix(otIp6Prefix &aPrefix) const { return CmdLineParser::ParseAsIp6Prefix(mString, aPrefix); } @@ -493,7 +508,7 @@ public: * @param[out] aValue A reference to output the parsed value. * * @retval kErrorNone The argument was parsed successfully. - * @retval kErrorInvalidArgs The argument does not contain a valid value. + * @retval kErrorInvalidArgs The argument is empty or does not contain a valid value. * */ template otError ParseAs(Type &aValue) const; @@ -509,7 +524,7 @@ public: * @param[in] aSize The expected size of byte sequence (number of bytes after parsing). * * @retval kErrorNone The argument was parsed successfully. - * @retval kErrorInvalidArgs The argument does not contain valid hex bytes and/or not @p aSize bytes. + * @retval kErrorInvalidArgs The argument is empty or does not contain valid hex bytes and/or not @p aSize bytes. * */ otError ParseAsHexString(uint8_t *aBuffer, uint16_t aSize) const @@ -529,7 +544,7 @@ public: * @param[out] aBuffer A reference to a byte array to output the parsed byte sequence. * * @retval kErrorNone The argument was parsed successfully. - * @retval kErrorInvalidArgs The argument does not contain valid hex bytes and/or not @p aSize bytes. + * @retval kErrorInvalidArgs The argument is empty or does not contain valid hex bytes and/or not @p aSize bytes. * */ template otError ParseAsHexString(uint8_t (&aBuffer)[kBufferSize]) @@ -561,13 +576,22 @@ public: * @note this method only copies the string pointer value (i.e., `GetString()` pointer) from `aArgs` array to the * @p aStrings array (the content of strings are not copied). * - * @param[in] aArgs A pointer to an `Arg` array. - * @param[in] aArgsLength Number of entries in the @p aArgs array. + * @param[in] aArgs An `Arg` array. * @param[out] aStrings An `char *` array to populate with the argument string pointers. The @p aString array - * MUST contain at least @p aArgsLength entries. + * MUST contain at least same number of entries as in @p aArgs array. * */ - static void CopyArgsToStringArray(Arg aArgs[], uint8_t aArgsLength, char *aStrings[]); + static void CopyArgsToStringArray(Arg aArgs[], char *aStrings[]); + + /** + * This static method returns the length of argument array, i.e. number of consecutive non-empty arguments. + * + * @param[in] aArgs An `Arg` array. + * + * @returns Number of non-empty arguments in the array. + * + */ + static uint8_t GetArgsLength(Arg aArgs[]); private: char *mString; @@ -576,17 +600,31 @@ private: /** * This function parses a given command line string and breaks it into an argument list. * - * Note: this method may change the input @p aCommandString, it will put a '\0' by the end of each argument, - * and @p aArgs will point to the arguments in the input @p aCommandString. Backslash ('\') can be used - * to escape separators (' ', '\t', '\r', '\n') and the backslash itself. + * This function may change the input @p aCommandString, it will put a '\0' by the end of each argument, and @p aArgs + * will point to the arguments in the input @p aCommandString. Backslash ('\') can be used to escape separators + * (' ', '\t', '\r', '\n') and the backslash itself. + * + * As the arguments are parsed, the @p aArgs array entries are populated. Any remaining @p aArgs entries in the array + * will be cleared and marked as empty. So the number of arguments can be determined by going through @p aArgs array + * entries till we get to an empty `Arg` (i.e., `Arg::IsEmpty()` returns `true). + * + * This function ensures that the last entry in @p aArgs array is always used to indicate the end (always marked as + * empty), so the @p aArgs array should have one more entry than the desired max number of arguments. * * @param[in] aCommandString A null-terminated input string. - * @param[out] aArgsLength The argument counter of the command line. - * @param[out] aArgs The argument vector of the command line. - * @param[in] aArgsLengthMax The maximum argument counter. + * @param[out] aArgs The argument array. + * @param[in] aArgsMaxLength The max length of @p aArgs array. + * + * @retval OT_ERROR_NONE The command line parsed successfully and @p aArgs array is populated. + * @retval OT_ERROR_INVALID_ARGS Too many arguments in @p aCommandString and could not fit in @p aArgs array. * */ -otError ParseCmd(char *aCommandString, uint8_t &aArgsLength, Arg aArgs[], uint8_t aArgsLengthMax); +otError ParseCmd(char *aCommandString, Arg aArgs[], uint8_t aArgsMaxLength); + +template inline otError ParseCmd(char *aCommandString, Arg (&aArgs)[kLength]) +{ + return ParseCmd(aCommandString, aArgs, kLength); +} //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // Specializations of `Arg::ParseAs()` method.