From 5598848980ae80cfaac134e89796cb711e321464 Mon Sep 17 00:00:00 2001 From: kangping Date: Thu, 29 Jul 2021 12:02:55 +0800 Subject: [PATCH] [cli] add commands for getting Border Router OMR and on-link prefix (#6878) This commit adds the 'br omrprefix' and 'br onlinkprefix' for getting OMR and on-link prefix of current Border Router. --- include/openthread/border_router.h | 31 +++++++++++++++++++ include/openthread/instance.h | 2 +- src/cli/README.md | 20 +++++++++++++ src/cli/cli.cpp | 35 ++++++++++++++++++++-- src/cli/cli.hpp | 1 + src/cli/cli_network_data.cpp | 13 ++------ src/cli/cli_network_data.hpp | 1 - src/core/api/border_router_api.cpp | 15 ++++++++++ src/core/border_router/routing_manager.cpp | 22 ++++++++++++++ src/core/border_router/routing_manager.hpp | 29 ++++++++++++++++++ 10 files changed, 154 insertions(+), 15 deletions(-) diff --git a/include/openthread/border_router.h b/include/openthread/border_router.h index 2b6d3f413..7774727b4 100644 --- a/include/openthread/border_router.h +++ b/include/openthread/border_router.h @@ -86,6 +86,37 @@ otError otBorderRoutingInit(otInstance *aInstance, uint32_t aInfraIfIndex, bool */ otError otBorderRoutingSetEnabled(otInstance *aInstance, bool aEnabled); +/** + * This method returns the off-mesh-routable (OMR) prefix. + * + * The randomly generated 64-bit prefix will be published + * in the Thread network if there isn't already an OMR prefix. + * + * @param[in] aInstance A pointer to an OpenThread instance. + * @param[out] aPrefix A pointer to where the prefix will be output to. + * + * @retval OT_ERROR_INVALID_STATE The Border Routing Manager is not initialized yet. + * @retval OT_ERROR_NONE Successfully retrieved the OMR prefix. + * + */ +otError otBorderRoutingGetOmrPrefix(otInstance *aInstance, otIp6Prefix *aPrefix); + +/** + * This method returns the on-link prefix for the adjacent infrastructure link. + * + * The randomly generated 64-bit prefix will be advertised + * on the infrastructure link if there isn't already a usable + * on-link prefix being advertised on the link. + * + * @param[in] aInstance A pointer to an OpenThread instance. + * @param[out] aPrefix A pointer to where the prefix will be output to. + * + * @retval OT_ERROR_INVALID_STATE The Border Routing Manager is not initialized yet. + * @retval OT_ERROR_NONE Successfully retrieved the on-link prefix. + * + */ +otError otBorderRoutingGetOnLinkPrefix(otInstance *aInstance, otIp6Prefix *aPrefix); + /** * This method provides a full or stable copy of the local Thread Network Data. * diff --git a/include/openthread/instance.h b/include/openthread/instance.h index b87c9855f..1287a5ba0 100644 --- a/include/openthread/instance.h +++ b/include/openthread/instance.h @@ -53,7 +53,7 @@ extern "C" { * @note This number versions both OpenThread platform and user APIs. * */ -#define OPENTHREAD_API_VERSION (148) +#define OPENTHREAD_API_VERSION (149) /** * @addtogroup api-instance diff --git a/src/cli/README.md b/src/cli/README.md index 60d9e9db4..56bee0ea8 100644 --- a/src/cli/README.md +++ b/src/cli/README.md @@ -356,6 +356,26 @@ Done Done ``` +### br omrprefix + +Get the randomly generated off-mesh-routable prefix of the Border Router. + +```bash +> br omrprefix +fdfc:1ff5:1512:5622::/64 +Done +``` + +### br onlinkprefix + +Get the randomly generated on-link prefix of the Border Router. + +```bash +> br onlinkprefix +fd41:2650:a6f5:0::/64 +Done +``` + ### bufferinfo Show the current message buffer information. diff --git a/src/cli/cli.cpp b/src/cli/cli.cpp index ec3ca84b8..7f6e97adb 100644 --- a/src/cli/cli.cpp +++ b/src/cli/cli.cpp @@ -370,8 +370,30 @@ otError Interpreter::ProcessBorderRouting(Arg aArgs[]) otError error = OT_ERROR_NONE; bool enable; - SuccessOrExit(error = ParseEnableOrDisable(aArgs[0], enable)); - error = otBorderRoutingSetEnabled(mInstance, enable); + if (ParseEnableOrDisable(aArgs[0], enable) == OT_ERROR_NONE) + { + SuccessOrExit(error = otBorderRoutingSetEnabled(mInstance, enable)); + } + else if (aArgs[0] == "omrprefix") + { + otIp6Prefix omrPrefix; + + SuccessOrExit(error = otBorderRoutingGetOmrPrefix(mInstance, &omrPrefix)); + OutputIp6Prefix(omrPrefix); + OutputLine(""); + } + else if (aArgs[0] == "onlinkprefix") + { + otIp6Prefix onLinkPrefix; + + SuccessOrExit(error = otBorderRoutingGetOnLinkPrefix(mInstance, &onLinkPrefix)); + OutputIp6Prefix(onLinkPrefix); + OutputLine(""); + } + else + { + ExitNow(error = OT_ERROR_INVALID_COMMAND); + } exit: return error; @@ -4643,6 +4665,15 @@ void Interpreter::OutputPrefix(const otMeshLocalPrefix &aPrefix) (aPrefix.m8[4] << 8) | aPrefix.m8[5], (aPrefix.m8[6] << 8) | aPrefix.m8[7]); } +void Interpreter::OutputIp6Prefix(const otIp6Prefix &aPrefix) +{ + char string[OT_IP6_PREFIX_STRING_SIZE]; + + otIp6PrefixToString(&aPrefix, string, sizeof(string)); + + OutputFormat("%s", string); +} + #if OPENTHREAD_FTD || OPENTHREAD_CONFIG_TMF_NETWORK_DIAG_MTD_ENABLE otError Interpreter::ProcessNetworkDiagnostic(Arg aArgs[]) { diff --git a/src/cli/cli.hpp b/src/cli/cli.hpp index c74c9e67e..2eafb7fd4 100644 --- a/src/cli/cli.hpp +++ b/src/cli/cli.hpp @@ -555,6 +555,7 @@ private: otError ProcessNetworkDataRoute(void); otError ProcessNetworkDataService(void); void OutputPrefix(const otMeshLocalPrefix &aPrefix); + void OutputIp6Prefix(const otIp6Prefix &aPrefix); otError ProcessNetstat(Arg aArgs[]); #if OPENTHREAD_CONFIG_TMF_NETDATA_SERVICE_ENABLE diff --git a/src/cli/cli_network_data.cpp b/src/cli/cli_network_data.cpp index d6e2eb5a8..67352f367 100644 --- a/src/cli/cli_network_data.cpp +++ b/src/cli/cli_network_data.cpp @@ -63,7 +63,7 @@ void NetworkData::OutputPrefix(const otBorderRouterConfig &aConfig) char flagsString[kMaxFlagStringSize]; char *flagsPtr = &flagsString[0]; - OutputIp6Prefix(aConfig.mPrefix); + mInterpreter.OutputIp6Prefix(aConfig.mPrefix); if (aConfig.mPreferred) { @@ -135,7 +135,7 @@ void NetworkData::OutputRoute(const otExternalRouteConfig &aConfig) char flagsString[kMaxFlagStringSize]; char *flagsPtr = &flagsString[0]; - OutputIp6Prefix(aConfig.mPrefix); + mInterpreter.OutputIp6Prefix(aConfig.mPrefix); if (aConfig.mStable) { @@ -159,15 +159,6 @@ void NetworkData::OutputRoute(const otExternalRouteConfig &aConfig) mInterpreter.OutputLine(" %04x", aConfig.mRloc16); } -void NetworkData::OutputIp6Prefix(const otIp6Prefix &aPrefix) -{ - char string[OT_IP6_PREFIX_STRING_SIZE]; - - otIp6PrefixToString(&aPrefix, string, sizeof(string)); - - mInterpreter.OutputFormat("%s", string); -} - void NetworkData::OutputPreference(signed int aPreference) { switch (aPreference) diff --git a/src/cli/cli_network_data.hpp b/src/cli/cli_network_data.hpp index 8c1927d9f..908887618 100644 --- a/src/cli/cli_network_data.hpp +++ b/src/cli/cli_network_data.hpp @@ -117,7 +117,6 @@ private: void OutputPrefixes(void); void OutputRoutes(void); void OutputServices(void); - void OutputIp6Prefix(const otIp6Prefix &aPrefix); void OutputPreference(signed int aPreference); static constexpr Command sCommands[] = { diff --git a/src/core/api/border_router_api.cpp b/src/core/api/border_router_api.cpp index 21e083b1f..e096a99d3 100644 --- a/src/core/api/border_router_api.cpp +++ b/src/core/api/border_router_api.cpp @@ -57,6 +57,21 @@ otError otBorderRoutingSetEnabled(otInstance *aInstance, bool aEnabled) return instance.Get().SetEnabled(aEnabled); } + +otError otBorderRoutingGetOmrPrefix(otInstance *aInstance, otIp6Prefix *aPrefix) +{ + Instance &instance = *static_cast(aInstance); + + return instance.Get().GetOmrPrefix(static_cast(*aPrefix)); +} + +otError otBorderRoutingGetOnLinkPrefix(otInstance *aInstance, otIp6Prefix *aPrefix) +{ + Instance &instance = *static_cast(aInstance); + + return instance.Get().GetOnLinkPrefix(static_cast(*aPrefix)); +} + #endif otError otBorderRouterGetNetData(otInstance *aInstance, bool aStable, uint8_t *aData, uint8_t *aDataLength) diff --git a/src/core/border_router/routing_manager.cpp b/src/core/border_router/routing_manager.cpp index d4260e8d1..ef0836adc 100644 --- a/src/core/border_router/routing_manager.cpp +++ b/src/core/border_router/routing_manager.cpp @@ -116,6 +116,28 @@ exit: return error; } +Error RoutingManager::GetOmrPrefix(Ip6::Prefix &aPrefix) +{ + Error error = kErrorNone; + + VerifyOrExit(IsInitialized(), error = kErrorInvalidState); + aPrefix = mLocalOmrPrefix; + +exit: + return error; +} + +Error RoutingManager::GetOnLinkPrefix(Ip6::Prefix &aPrefix) +{ + Error error = kErrorNone; + + VerifyOrExit(IsInitialized(), error = kErrorInvalidState); + aPrefix = mLocalOnLinkPrefix; + +exit: + return error; +} + Error RoutingManager::LoadOrGenerateRandomOmrPrefix(void) { Error error = kErrorNone; diff --git a/src/core/border_router/routing_manager.hpp b/src/core/border_router/routing_manager.hpp index 719a3a0dd..44eb52713 100644 --- a/src/core/border_router/routing_manager.hpp +++ b/src/core/border_router/routing_manager.hpp @@ -109,6 +109,35 @@ public: */ Error SetEnabled(bool aEnabled); + /** + * This method returns the off-mesh-routable (OMR) prefix. + * + * The randomly generated 64-bit prefix will be published + * in the Thread network if there isn't already a OMR prefix. + * + * @param[in] aPrefix A reference to where the prefix will be output to. + * + * @retval kErrorInvalidState The Border Routing Manager is not initialized yet. + * @retval kErrorNone Successfully retrieved the OMR prefix. + * + */ + Error GetOmrPrefix(Ip6::Prefix &aPrefix); + + /** + * This method returns the on-link prefix for the adjacent infrastructure link. + * + * The randomly generated 64-bit prefix will be advertised + * on the infrastructure link if there isn't already a usable + * on-link prefix being advertised on the link. + * + * @param[in] aPrefix A reference to where the prefix will be output to. + * + * @retval kErrorInvalidState The Border Routing Manager is not initialized yet. + * @retval kErrorNone Successfully retrieved the on-link prefix. + * + */ + Error GetOnLinkPrefix(Ip6::Prefix &aPrefix); + /** * This method receives an ICMPv6 message on the infrastructure interface. *