From 6a549d1cdb6c264b0afd8c754953ef26faa2a2d7 Mon Sep 17 00:00:00 2001 From: Abtin Keshavarzian Date: Tue, 6 Jun 2023 15:49:40 -0700 Subject: [PATCH] [cli] add helper `ProcessEnableDisable()` methods (#9138) --- src/cli/cli.cpp | 196 ++++++++++++++++++------------------- src/cli/cli.hpp | 11 +++ src/cli/cli_dns.cpp | 33 +++---- src/cli/cli_srp_client.cpp | 13 +-- src/cli/cli_srp_server.cpp | 38 +++---- 5 files changed, 132 insertions(+), 159 deletions(-) diff --git a/src/cli/cli.cpp b/src/cli/cli.cpp index d848e7f80..d3fb89e58 100644 --- a/src/cli/cli.cpp +++ b/src/cli/cli.cpp @@ -341,7 +341,6 @@ otError Interpreter::SetUserCommands(const otCliCommand *aCommands, uint8_t aLen return error; } -#if OPENTHREAD_FTD || OPENTHREAD_MTD otError Interpreter::ParseEnableOrDisable(const Arg &aArg, bool &aEnable) { otError error = OT_ERROR_NONE; @@ -362,6 +361,8 @@ otError Interpreter::ParseEnableOrDisable(const Arg &aArg, bool &aEnable) return error; } +#if OPENTHREAD_FTD || OPENTHREAD_MTD + otError Interpreter::ParseJoinerDiscerner(Arg &aArg, otJoinerDiscerner &aDiscerner) { otError error; @@ -610,12 +611,12 @@ template <> otError Interpreter::Process(Arg aArgs[]) { return mBr.Pr template <> otError Interpreter::Process(Arg aArgs[]) { otError error = OT_ERROR_NONE; - bool enable; if (aArgs[0].IsEmpty()) { ExitNow(error = OT_ERROR_INVALID_COMMAND); } + /** * @cli nat64 (enable,disable) * @code @@ -631,9 +632,8 @@ template <> otError Interpreter::Process(Arg aArgs[]) * #otNat64SetEnabled * */ - if (ParseEnableOrDisable(aArgs[0], enable) == OT_ERROR_NONE) + if (ProcessEnableDisable(aArgs, otNat64SetEnabled) == OT_ERROR_NONE) { - otNat64SetEnabled(GetInstancePtr(), enable); } /** * @cli nat64 state @@ -1131,32 +1131,13 @@ exit: #if OPENTHREAD_FTD && OPENTHREAD_CONFIG_REFERENCE_DEVICE_ENABLE template <> otError Interpreter::Process(Arg aArgs[]) { - otError error = OT_ERROR_NONE; - bool enable; - - VerifyOrExit(!aArgs[0].IsEmpty(), error = OT_ERROR_INVALID_COMMAND); - - SuccessOrExit(error = ParseEnableOrDisable(aArgs[0], enable)); - otThreadSetCcmEnabled(GetInstancePtr(), enable); - -exit: - return error; + return ProcessEnableDisable(aArgs, otThreadSetCcmEnabled); } template <> otError Interpreter::Process(Arg aArgs[]) { - otError error = OT_ERROR_NONE; - bool enable; - - VerifyOrExit(!aArgs[0].IsEmpty(), error = OT_ERROR_INVALID_COMMAND); - - SuccessOrExit(error = ParseEnableOrDisable(aArgs[0], enable)); - otThreadSetThreadVersionCheckEnabled(GetInstancePtr(), enable); - -exit: - return error; + return ProcessEnableDisable(aArgs, otThreadSetThreadVersionCheckEnabled); } - #endif /** @@ -1858,15 +1839,9 @@ template <> otError Interpreter::Process(Arg aArgs[]) { return mCo template <> otError Interpreter::Process(Arg aArgs[]) { otError error = OT_ERROR_NONE; - bool enable; - if (aArgs[0].IsEmpty()) + if (ProcessEnableDisable(aArgs, otPlatRadioIsCoexEnabled, otPlatRadioSetCoexEnabled) == OT_ERROR_NONE) { - OutputEnabledDisabledStatus(otPlatRadioIsCoexEnabled(GetInstancePtr())); - } - else if (ParseEnableOrDisable(aArgs[0], enable) == OT_ERROR_NONE) - { - error = otPlatRadioSetCoexEnabled(GetInstancePtr(), enable); } else if (aArgs[0] == "metrics") { @@ -3241,10 +3216,6 @@ template <> otError Interpreter::Process(Arg aArgs[]) */ else if (aArgs[0] == "promiscuous") { - if (aArgs[1].IsEmpty()) - { - OutputEnabledDisabledStatus(otIp6IsMulticastPromiscuousEnabled(GetInstancePtr())); - } /** * @cli ipmaddr promiscuous (enable,disable) * @code @@ -3259,13 +3230,8 @@ template <> otError Interpreter::Process(Arg aArgs[]) * @par api_copy * #otIp6SetMulticastPromiscuousEnabled */ - else - { - bool enable; - - SuccessOrExit(error = ParseEnableOrDisable(aArgs[1], enable)); - otIp6SetMulticastPromiscuousEnabled(GetInstancePtr(), enable); - } + error = + ProcessEnableDisable(aArgs + 1, otIp6IsMulticastPromiscuousEnabled, otIp6SetMulticastPromiscuousEnabled); } /** * @cli ipmaddr llatn @@ -5774,22 +5740,7 @@ template <> otError Interpreter::Process(Arg aArgs[]) #if OPENTHREAD_CONFIG_MAC_FILTER_ENABLE && OPENTHREAD_CONFIG_RADIO_LINK_IEEE_802_15_4_ENABLE template <> otError Interpreter::Process(Arg aArgs[]) { - otError error = OT_ERROR_NONE; - - if (aArgs[0].IsEmpty()) - { - OutputEnabledDisabledStatus(otLinkIsRadioFilterEnabled(GetInstancePtr())); - } - else - { - bool enable; - - SuccessOrExit(error = ParseEnableOrDisable(aArgs[0], enable)); - otLinkSetRadioFilterEnabled(GetInstancePtr(), enable); - } - -exit: - return error; + return ProcessEnableDisable(aArgs, otLinkIsRadioFilterEnabled, otLinkSetRadioFilterEnabled); } #endif @@ -6189,24 +6140,19 @@ template <> otError Interpreter::Process(Arg aA return ProcessGetSet(aArgs, otThreadGetRouterDowngradeThreshold, otThreadSetRouterDowngradeThreshold); } +/** + * @cli routereligible + * @code + * routereligible + * Enabled + * Done + * @endcode + * @sa otThreadIsRouterEligible + * @par + * Indicates whether the router role is enabled or disabled. + */ template <> otError Interpreter::Process(Arg aArgs[]) { - otError error = OT_ERROR_NONE; - /** - * @cli routereligible - * @code - * routereligible - * Enabled - * Done - * @endcode - * @sa otThreadIsRouterEligible - * @par - * Indicates whether the router role is enabled or disabled. - */ - if (aArgs[0].IsEmpty()) - { - OutputEnabledDisabledStatus(otThreadIsRouterEligible(GetInstancePtr())); - } /** * @cli routereligible (enable,disable) * @code @@ -6222,17 +6168,9 @@ template <> otError Interpreter::Process(Arg aArgs[]) * @par * Enables or disables the router role. */ - else - { - bool enable; - - SuccessOrExit(error = ParseEnableOrDisable(aArgs[0], enable)); - error = otThreadSetRouterEligible(GetInstancePtr(), enable); - } - -exit: - return error; + return ProcessEnableDisable(aArgs, otThreadIsRouterEligible, otThreadSetRouterEligible); } + /** * @cli routerselectionjitter * @code @@ -6766,27 +6704,13 @@ exit: template <> otError Interpreter::Process(Arg aArgs[]) { otError error = OT_ERROR_NONE; - bool enable; - if (aArgs[0].IsEmpty()) + if (ProcessEnableDisable(aArgs, otTrelIsEnabled, otTrelSetEnabled) == OT_ERROR_NONE) { - OutputEnabledDisabledStatus(otTrelIsEnabled(GetInstancePtr())); - } - else if (ParseEnableOrDisable(aArgs[0], enable) == OT_ERROR_NONE) - { - otTrelSetEnabled(GetInstancePtr(), enable); } else if (aArgs[0] == "filter") { - if (aArgs[1].IsEmpty()) - { - OutputEnabledDisabledStatus(otTrelIsFilterEnabled(GetInstancePtr())); - } - else - { - SuccessOrExit(error = ParseEnableOrDisable(aArgs[1], enable)); - otTrelSetFilterEnabled(GetInstancePtr(), enable); - } + error = ProcessEnableDisable(aArgs + 1, otTrelIsFilterEnabled, otTrelSetFilterEnabled); } else if (aArgs[0] == "peers") { @@ -7233,6 +7157,76 @@ void Interpreter::Initialize(otInstance *aInstance, otCliOutputCallback aCallbac Interpreter::sInterpreter = new (&sInterpreterRaw) Interpreter(instance, aCallback, aContext); } +otError Interpreter::ProcessEnableDisable(Arg aArgs[], SetEnabledHandler aSetEnabledHandler) +{ + otError error = OT_ERROR_NONE; + bool enable; + + if (ParseEnableOrDisable(aArgs[0], enable) == OT_ERROR_NONE) + { + aSetEnabledHandler(GetInstancePtr(), enable); + } + else + { + error = OT_ERROR_INVALID_COMMAND; + } + + return error; +} + +otError Interpreter::ProcessEnableDisable(Arg aArgs[], SetEnabledHandlerFailable aSetEnabledHandler) +{ + otError error = OT_ERROR_NONE; + bool enable; + + if (ParseEnableOrDisable(aArgs[0], enable) == OT_ERROR_NONE) + { + error = aSetEnabledHandler(GetInstancePtr(), enable); + } + else + { + error = OT_ERROR_INVALID_COMMAND; + } + + return error; +} + +otError Interpreter::ProcessEnableDisable(Arg aArgs[], + IsEnabledHandler aIsEnabledHandler, + SetEnabledHandler aSetEnabledHandler) +{ + otError error = OT_ERROR_NONE; + + if (aArgs[0].IsEmpty()) + { + OutputEnabledDisabledStatus(aIsEnabledHandler(GetInstancePtr())); + } + else + { + error = ProcessEnableDisable(aArgs, aSetEnabledHandler); + } + + return error; +} + +otError Interpreter::ProcessEnableDisable(Arg aArgs[], + IsEnabledHandler aIsEnabledHandler, + SetEnabledHandlerFailable aSetEnabledHandler) +{ + otError error = OT_ERROR_NONE; + + if (aArgs[0].IsEmpty()) + { + OutputEnabledDisabledStatus(aIsEnabledHandler(GetInstancePtr())); + } + else + { + error = ProcessEnableDisable(aArgs, aSetEnabledHandler); + } + + return error; +} + void Interpreter::OutputPrompt(void) { #if OPENTHREAD_CONFIG_CLI_PROMPT_ENABLE diff --git a/src/cli/cli.hpp b/src/cli/cli.hpp index 2a19e9463..fc89231e1 100644 --- a/src/cli/cli.hpp +++ b/src/cli/cli.hpp @@ -115,6 +115,7 @@ class Interpreter : public OutputImplementer, public Output friend class Joiner; friend class NetworkData; friend class SrpClient; + friend class SrpServer; #endif friend void otCliPlatLogv(otLogLevel, otLogRegion, const char *, va_list); friend void otCliAppendResult(otError aError); @@ -290,6 +291,9 @@ private: template using GetHandler = ValueType (&)(otInstance *); template using SetHandler = void (&)(otInstance *, ValueType); template using SetHandlerFailable = otError (&)(otInstance *, ValueType); + using IsEnabledHandler = bool (&)(otInstance *); + using SetEnabledHandler = void (&)(otInstance *, bool); + using SetEnabledHandlerFailable = otError (&)(otInstance *, bool); // Returns format string to output a `ValueType` (e.g., "%u" for `uint16_t`). template static constexpr const char *FormatStringFor(void); @@ -365,6 +369,13 @@ private: return error; } + otError ProcessEnableDisable(Arg aArgs[], SetEnabledHandler aSetEnabledHandler); + otError ProcessEnableDisable(Arg aArgs[], SetEnabledHandlerFailable aSetEnabledHandler); + otError ProcessEnableDisable(Arg aArgs[], IsEnabledHandler aIsEnabledHandler, SetEnabledHandler aSetEnabledHandler); + otError ProcessEnableDisable(Arg aArgs[], + IsEnabledHandler aIsEnabledHandler, + SetEnabledHandlerFailable aSetEnabledHandler); + void OutputPrompt(void); void OutputResult(otError aError); diff --git a/src/cli/cli_dns.cpp b/src/cli/cli_dns.cpp index c85cb2477..8d7e0a414 100644 --- a/src/cli/cli_dns.cpp +++ b/src/cli/cli_dns.cpp @@ -657,22 +657,18 @@ template <> otError Dns::Process(Arg aArgs[]) error = OT_ERROR_INVALID_ARGS; } #if OPENTHREAD_CONFIG_DNS_UPSTREAM_QUERY_ENABLE + /** + * @cli dns server upstream + * @code + * dns server upstream + * Enabled + * Done + * @endcode + * @par api_copy + * #otDnssdUpstreamQueryIsEnabled + */ else if (aArgs[0] == "upstream") { - /** - * @cli dns server upstream - * @code - * dns server upstream - * Enabled - * Done - * @endcode - * @par api_copy - * #otDnssdUpstreamQueryIsEnabled - */ - if (aArgs[1].IsEmpty()) - { - OutputEnabledDisabledStatus(otDnssdUpstreamQueryIsEnabled(GetInstancePtr())); - } /** * @cli dns server upstream {enable|disable} * @code @@ -683,13 +679,8 @@ template <> otError Dns::Process(Arg aArgs[]) * @par api_copy * #otDnssdUpstreamQuerySetEnabled */ - else - { - bool enable; - - SuccessOrExit(error = Interpreter::ParseEnableOrDisable(aArgs[1], enable)); - otDnssdUpstreamQuerySetEnabled(GetInstancePtr(), enable); - } + error = Interpreter::GetInterpreter().ProcessEnableDisable(aArgs + 1, otDnssdUpstreamQueryIsEnabled, + otDnssdUpstreamQuerySetEnabled); } #endif // OPENTHREAD_CONFIG_DNS_UPSTREAM_QUERY_ENABLE else diff --git a/src/cli/cli_srp_client.cpp b/src/cli/cli_srp_client.cpp index 0294c5306..290dec528 100644 --- a/src/cli/cli_srp_client.cpp +++ b/src/cli/cli_srp_client.cpp @@ -331,17 +331,8 @@ template <> otError SrpClient::Process(Arg aArgs[]) { // `key [enable/disable]` - bool enable; - - if (aArgs[1].IsEmpty()) - { - OutputEnabledDisabledStatus(otSrpClientIsServiceKeyRecordEnabled(GetInstancePtr())); - ExitNow(); - } - - SuccessOrExit(error = Interpreter::ParseEnableOrDisable(aArgs[1], enable)); - VerifyOrExit(aArgs[2].IsEmpty(), error = OT_ERROR_INVALID_ARGS); - otSrpClientSetServiceKeyRecordEnabled(GetInstancePtr(), enable); + error = Interpreter::GetInterpreter().ProcessEnableDisable(aArgs + 1, otSrpClientIsServiceKeyRecordEnabled, + otSrpClientSetServiceKeyRecordEnabled); } #endif // OPENTHREAD_CONFIG_REFERENCE_DEVICE_ENABLE else diff --git a/src/cli/cli_srp_server.cpp b/src/cli/cli_srp_server.cpp index 6a0a40276..50281ade1 100644 --- a/src/cli/cli_srp_server.cpp +++ b/src/cli/cli_srp_server.cpp @@ -75,24 +75,18 @@ template <> otError SrpServer::Process(Arg aArgs[]) } #if OPENTHREAD_CONFIG_BORDER_ROUTING_ENABLE +/** + * @cli srp server auto + * @code + * srp server auto + * Disabled + * Done + * @endcode + * @par api_copy + * #otSrpServerIsAutoEnableMode + */ template <> otError SrpServer::Process(Arg aArgs[]) { - otError error = OT_ERROR_NONE; - - /** - * @cli srp server auto - * @code - * srp server auto - * Disabled - * Done - * @endcode - * @par api_copy - * #otSrpServerIsAutoEnableMode - */ - if (aArgs[0].IsEmpty()) - { - OutputEnabledDisabledStatus(otSrpServerIsAutoEnableMode(GetInstancePtr())); - } /** * @cli srp server auto enable * @code @@ -102,16 +96,8 @@ template <> otError SrpServer::Process(Arg aArgs[]) * @par api_copy * #otSrpServerSetAutoEnableMode */ - else - { - bool enable; - - SuccessOrExit(error = Interpreter::ParseEnableOrDisable(aArgs[0], enable)); - otSrpServerSetAutoEnableMode(GetInstancePtr(), enable); - } - -exit: - return error; + return Interpreter::GetInterpreter().ProcessEnableDisable(aArgs, otSrpServerIsAutoEnableMode, + otSrpServerSetAutoEnableMode); } #endif