From 2057ca8846ae0d029ab26063bba3d39906aaa071 Mon Sep 17 00:00:00 2001 From: Abtin Keshavarzian Date: Thu, 30 Jun 2022 15:05:56 -0700 Subject: [PATCH] [cli] add helper method `ParsePreference()` (#7858) This commit adds a helper method `ParsePreference()` to parse a given CLI argument string as a route preference comparing it against "high", "med", or "low". This commit also moves the `PreferenceToString()` method from `Cli::NetworkData` to `Cli::Interpreter` class. --- src/cli/cli.cpp | 98 ++++++++++++++++++++++-------------- src/cli/cli.hpp | 24 +++++++++ src/cli/cli_history.cpp | 4 +- src/cli/cli_network_data.cpp | 29 +---------- src/cli/cli_network_data.hpp | 10 ---- 5 files changed, 87 insertions(+), 78 deletions(-) diff --git a/src/cli/cli.cpp b/src/cli/cli.cpp index 535f9581d..e7dda6f45 100644 --- a/src/cli/cli.cpp +++ b/src/cli/cli.cpp @@ -401,6 +401,55 @@ exit: #endif // OPENTHREAD_CONFIG_PING_SENDER_ENABLE +otError Interpreter::ParsePreference(const Arg &aArg, otRoutePreference &aPreference) +{ + otError error = OT_ERROR_NONE; + + if (aArg == "high") + { + aPreference = OT_ROUTE_PREFERENCE_HIGH; + } + else if (aArg == "med") + { + aPreference = OT_ROUTE_PREFERENCE_MED; + } + else if (aArg == "low") + { + aPreference = OT_ROUTE_PREFERENCE_LOW; + } + else + { + error = OT_ERROR_INVALID_ARGS; + } + + return error; +} + +const char *Interpreter::PreferenceToString(signed int aPreference) +{ + const char *str = ""; + + switch (aPreference) + { + case OT_ROUTE_PREFERENCE_LOW: + str = "low"; + break; + + case OT_ROUTE_PREFERENCE_MED: + str = "med"; + break; + + case OT_ROUTE_PREFERENCE_HIGH: + str = "high"; + break; + + default: + break; + } + + return str; +} + #if OPENTHREAD_CONFIG_HISTORY_TRACKER_ENABLE template <> otError Interpreter::Process(Arg aArgs[]) { @@ -560,30 +609,13 @@ template <> otError Interpreter::Process(Arg aArgs[]) { if (aArgs[1].IsEmpty()) { - OutputLine("%s", - NetworkData::PreferenceToString(otBorderRoutingGetRouteInfoOptionPreference(GetInstancePtr()))); + OutputLine("%s", PreferenceToString(otBorderRoutingGetRouteInfoOptionPreference(GetInstancePtr()))); } else { otRoutePreference preference; - if (aArgs[1] == "high") - { - preference = OT_ROUTE_PREFERENCE_HIGH; - } - else if (aArgs[1] == "med") - { - preference = OT_ROUTE_PREFERENCE_MED; - } - else if (aArgs[1] == "low") - { - preference = OT_ROUTE_PREFERENCE_LOW; - } - else - { - ExitNow(error = OT_ERROR_INVALID_ARGS); - } - + SuccessOrExit(error = ParsePreference(aArgs[1], preference)); otBorderRoutingSetRouteInfoOptionPreference(GetInstancePtr(), preference); } } @@ -3602,17 +3634,11 @@ otError Interpreter::ParsePrefix(Arg aArgs[], otBorderRouterConfig &aConfig) for (; !aArgs->IsEmpty(); aArgs++) { - if (*aArgs == "high") + otRoutePreference preference; + + if (ParsePreference(*aArgs, preference) == OT_ERROR_NONE) { - aConfig.mPreference = OT_ROUTE_PREFERENCE_HIGH; - } - else if (*aArgs == "med") - { - aConfig.mPreference = OT_ROUTE_PREFERENCE_MED; - } - else if (*aArgs == "low") - { - aConfig.mPreference = OT_ROUTE_PREFERENCE_LOW; + aConfig.mPreference = preference; } else { @@ -3883,6 +3909,8 @@ otError Interpreter::ParseRoute(Arg aArgs[], otExternalRouteConfig &aConfig) for (; !aArgs->IsEmpty(); aArgs++) { + otRoutePreference preference; + if (*aArgs == "s") { aConfig.mStable = true; @@ -3891,17 +3919,9 @@ otError Interpreter::ParseRoute(Arg aArgs[], otExternalRouteConfig &aConfig) { aConfig.mNat64 = true; } - else if (*aArgs == "high") + else if (ParsePreference(*aArgs, preference) == OT_ERROR_NONE) { - aConfig.mPreference = OT_ROUTE_PREFERENCE_HIGH; - } - else if (*aArgs == "med") - { - aConfig.mPreference = OT_ROUTE_PREFERENCE_MED; - } - else if (*aArgs == "low") - { - aConfig.mPreference = OT_ROUTE_PREFERENCE_LOW; + aConfig.mPreference = preference; } else { diff --git a/src/cli/cli.hpp b/src/cli/cli.hpp index 196703a9e..f4aadbbe8 100644 --- a/src/cli/cli.hpp +++ b/src/cli/cli.hpp @@ -47,6 +47,7 @@ #include #include #include +#include #include #include #if OPENTHREAD_CONFIG_TCP_ENABLE && OPENTHREAD_CONFIG_CLI_TCP_ENABLE @@ -211,6 +212,29 @@ public: */ static const char *AddressOriginToString(uint8_t aOrigin); + /** + * This static method parses a given argument string as a route preference comparing it against "high", "med", or + * "low". + * + * @param[in] aArg The argument string to parse. + * @param[out] aPreference Reference to a `otRoutePreference` to return the parsed preference. + * + * @retval OT_ERROR_NONE Successfully parsed @p aArg and updated @p aPreference. + * @retval OT_ERROR_INVALID_ARG @p aArg is not a valid preference string "high", "med", or "low". + * + */ + static otError ParsePreference(const Arg &aArg, otRoutePreference &aPreference); + + /** + * This static method converts a route preference value to human-readable string. + * + * @param[in] aPreference The preference value to convert (`OT_ROUTE_PREFERENCE_*` values). + * + * @returns A string representation @p aPreference. + * + */ + static const char *PreferenceToString(signed int aPreference); + protected: static Interpreter *sInterpreter; diff --git a/src/cli/cli_history.cpp b/src/cli/cli_history.cpp index af011ca33..d3c25a3e1 100644 --- a/src/cli/cli_history.cpp +++ b/src/cli/cli_history.cpp @@ -602,7 +602,7 @@ template <> otError History::Process(Arg aArgs[]) OutputLine(isList ? "%s -> event:%s prefix:%s flags:%s pref:%s rloc16:0x%04x" : "| %20s | %-7s | %-43s | %-9s | %-4s | 0x%04x |", ageString, Stringify(info->mEvent, kSimpleEventStrings), prefixString, flagsString, - NetworkData::PreferenceToString(info->mPrefix.mPreference), info->mPrefix.mRloc16); + Interpreter::PreferenceToString(info->mPrefix.mPreference), info->mPrefix.mRloc16); } exit: @@ -652,7 +652,7 @@ template <> otError History::Process(Arg aArgs[]) OutputLine(isList ? "%s -> event:%s route:%s flags:%s pref:%s rloc16:0x%04x" : "| %20s | %-7s | %-43s | %-9s | %-4s | 0x%04x |", ageString, Stringify(info->mEvent, kSimpleEventStrings), prefixString, flagsString, - NetworkData::PreferenceToString(info->mRoute.mPreference), info->mRoute.mRloc16); + Interpreter::PreferenceToString(info->mRoute.mPreference), info->mRoute.mRloc16); } exit: diff --git a/src/cli/cli_network_data.cpp b/src/cli/cli_network_data.cpp index 449b5faf8..1c3de9071 100644 --- a/src/cli/cli_network_data.cpp +++ b/src/cli/cli_network_data.cpp @@ -108,7 +108,7 @@ void NetworkData::OutputPrefix(const otBorderRouterConfig &aConfig) OutputFormat(" %s", flagsString); } - OutputLine(" %s %04x", PreferenceToString(aConfig.mPreference), aConfig.mRloc16); + OutputLine(" %s %04x", Interpreter::PreferenceToString(aConfig.mPreference), aConfig.mRloc16); } void NetworkData::RouteFlagsToString(const otExternalRouteConfig &aConfig, FlagsString &aString) @@ -141,32 +141,7 @@ void NetworkData::OutputRoute(const otExternalRouteConfig &aConfig) OutputFormat(" %s", flagsString); } - OutputLine(" %s %04x", PreferenceToString(aConfig.mPreference), aConfig.mRloc16); -} - -const char *NetworkData::PreferenceToString(signed int aPreference) -{ - const char *str = ""; - - switch (aPreference) - { - case OT_ROUTE_PREFERENCE_LOW: - str = "low"; - break; - - case OT_ROUTE_PREFERENCE_MED: - str = "med"; - break; - - case OT_ROUTE_PREFERENCE_HIGH: - str = "high"; - break; - - default: - break; - } - - return str; + OutputLine(" %s %04x", Interpreter::PreferenceToString(aConfig.mPreference), aConfig.mRloc16); } void NetworkData::OutputService(const otServiceConfig &aConfig) diff --git a/src/cli/cli_network_data.hpp b/src/cli/cli_network_data.hpp index 0fb2098df..d46ba9ecd 100644 --- a/src/cli/cli_network_data.hpp +++ b/src/cli/cli_network_data.hpp @@ -125,16 +125,6 @@ public: */ static void RouteFlagsToString(const otExternalRouteConfig &aConfig, FlagsString &aString); - /** - * This static method converts a route preference value to human-readable string. - * - * @param[in] aPreference The preference value to convert (`OT_ROUTE_PREFERENCE_*` values). - * - * @returns A string representation @p aPreference. - * - */ - static const char *PreferenceToString(signed int aPreference); - private: using Command = CommandEntry;