From 6eff68336943ec65fc991f9c06b7186bdda9cc71 Mon Sep 17 00:00:00 2001 From: Jonathan Hui Date: Tue, 18 Jan 2022 18:07:22 -0800 Subject: [PATCH] [cli] add ability to show local network data (#7316) --- src/cli/README_NETDATA.md | 23 ++++++- src/cli/cli_network_data.cpp | 127 ++++++++++++++++++++++++++++++----- src/cli/cli_network_data.hpp | 12 ++-- 3 files changed, 138 insertions(+), 24 deletions(-) diff --git a/src/cli/README_NETDATA.md b/src/cli/README_NETDATA.md index 5554a9f4b..b119de193 100644 --- a/src/cli/README_NETDATA.md +++ b/src/cli/README_NETDATA.md @@ -244,7 +244,7 @@ Done ### show -Usage: `netdata show [-x]` +Usage: `netdata show [local] [-x]` Print Network Data received from the Leader. @@ -257,7 +257,7 @@ Services: Done ``` -Print Network Data as hex-encoded TLVs. +Print Network Data received from the Leader as hex-encoded TLVs. ```bash > netdata show -x @@ -265,6 +265,25 @@ Print Network Data as hex-encoded TLVs. Done ``` +Print local Network Data to sync with Leader. + +```bash +> netdata show local +Prefixes: +fd00:dead:beef:cafe::/64 paros med dc00 +Routes: +Services: +Done +``` + +Print local Network Data to sync with Leader as hex-encoded TLVs. + +```bash +> netdata show local -x +08040b02174703140040fd00deadbeefcafe0504dc00330007021140 +Done +``` + ### netdata steeringdata check \|\ Check whether the steering data includes a joiner. diff --git a/src/cli/cli_network_data.cpp b/src/cli/cli_network_data.cpp index 0634aa18b..627d46425 100644 --- a/src/cli/cli_network_data.cpp +++ b/src/cli/cli_network_data.cpp @@ -345,52 +345,124 @@ exit: return error; } -void NetworkData::OutputPrefixes(void) +otError NetworkData::GetNextPrefix(otNetworkDataIterator *aIterator, otBorderRouterConfig *aConfig, bool aLocal) +{ + otError error; + + if (aLocal) + { +#if OPENTHREAD_CONFIG_BORDER_ROUTER_ENABLE + error = otBorderRouterGetNextOnMeshPrefix(GetInstancePtr(), aIterator, aConfig); +#else + error = OT_ERROR_NOT_FOUND; +#endif + } + else + { + error = otNetDataGetNextOnMeshPrefix(GetInstancePtr(), aIterator, aConfig); + } + + return error; +} + +void NetworkData::OutputPrefixes(bool aLocal) { otNetworkDataIterator iterator = OT_NETWORK_DATA_ITERATOR_INIT; otBorderRouterConfig config; OutputLine("Prefixes:"); - while (otNetDataGetNextOnMeshPrefix(GetInstancePtr(), &iterator, &config) == OT_ERROR_NONE) + while (GetNextPrefix(&iterator, &config, aLocal) == OT_ERROR_NONE) { OutputPrefix(config); } } -void NetworkData::OutputRoutes(void) +otError NetworkData::GetNextRoute(otNetworkDataIterator *aIterator, otExternalRouteConfig *aConfig, bool aLocal) +{ + otError error; + + if (aLocal) + { +#if OPENTHREAD_CONFIG_BORDER_ROUTER_ENABLE + error = otBorderRouterGetNextRoute(GetInstancePtr(), aIterator, aConfig); +#else + error = OT_ERROR_NOT_FOUND; +#endif + } + else + { + error = otNetDataGetNextRoute(GetInstancePtr(), aIterator, aConfig); + } + + return error; +} + +void NetworkData::OutputRoutes(bool aLocal) { otNetworkDataIterator iterator = OT_NETWORK_DATA_ITERATOR_INIT; otExternalRouteConfig config; OutputLine("Routes:"); - while (otNetDataGetNextRoute(GetInstancePtr(), &iterator, &config) == OT_ERROR_NONE) + while (GetNextRoute(&iterator, &config, aLocal) == OT_ERROR_NONE) { OutputRoute(config); } } -void NetworkData::OutputServices(void) +otError NetworkData::GetNextService(otNetworkDataIterator *aIterator, otServiceConfig *aConfig, bool aLocal) +{ + otError error; + + if (aLocal) + { +#if OPENTHREAD_CONFIG_TMF_NETDATA_SERVICE_ENABLE + error = otServerGetNextService(GetInstancePtr(), aIterator, aConfig); +#else + error = OT_ERROR_NOT_FOUND; +#endif + } + else + { + error = otNetDataGetNextService(GetInstancePtr(), aIterator, aConfig); + } + + return error; +} + +void NetworkData::OutputServices(bool aLocal) { otNetworkDataIterator iterator = OT_NETWORK_DATA_ITERATOR_INIT; otServiceConfig config; OutputLine("Services:"); - while (otNetDataGetNextService(GetInstancePtr(), &iterator, &config) == OT_ERROR_NONE) + while (GetNextService(&iterator, &config, aLocal) == OT_ERROR_NONE) { OutputService(config); } } -otError NetworkData::OutputBinary(void) +otError NetworkData::OutputBinary(bool aLocal) { - otError error = OT_ERROR_NONE; + otError error; uint8_t data[255]; uint8_t len = sizeof(data); - SuccessOrExit(error = otNetDataGet(GetInstancePtr(), false, data, &len)); + if (aLocal) + { +#if OPENTHREAD_CONFIG_BORDER_ROUTER_ENABLE + error = otBorderRouterGetNetData(GetInstancePtr(), false, data, &len); +#else + error = OT_ERROR_NOT_IMPLEMENTED; +#endif + } + else + { + error = otNetDataGet(GetInstancePtr(), false, data, &len); + } + SuccessOrExit(error); OutputBytesLine(data, static_cast(len)); @@ -400,20 +472,39 @@ exit: otError NetworkData::ProcessShow(Arg aArgs[]) { - otError error = OT_ERROR_INVALID_ARGS; + otError error = OT_ERROR_INVALID_ARGS; + bool local = false; + bool binary = false; - if (aArgs[0].IsEmpty()) + for (uint8_t i = 0; !aArgs[i].IsEmpty(); i++) { - OutputPrefixes(); - OutputRoutes(); - OutputServices(); + if (aArgs[i] == "local") + { + local = true; + } + else if (aArgs[i] == "-x") + { + binary = true; + } + else + { + ExitNow(error = OT_ERROR_INVALID_ARGS); + } + } + + if (binary) + { + error = OutputBinary(local); + } + else + { + OutputPrefixes(local); + OutputRoutes(local); + OutputServices(local); error = OT_ERROR_NONE; } - else if (aArgs[0] == "-x") - { - error = OutputBinary(); - } +exit: return error; } diff --git a/src/cli/cli_network_data.hpp b/src/cli/cli_network_data.hpp index a45484a06..d52170ce9 100644 --- a/src/cli/cli_network_data.hpp +++ b/src/cli/cli_network_data.hpp @@ -109,10 +109,14 @@ private: otError ProcessShow(Arg aArgs[]); otError ProcessSteeringData(Arg aArgs[]); - otError OutputBinary(void); - void OutputPrefixes(void); - void OutputRoutes(void); - void OutputServices(void); + otError GetNextPrefix(otNetworkDataIterator *aIterator, otBorderRouterConfig *aConfig, bool aLocal); + otError GetNextRoute(otNetworkDataIterator *aIterator, otExternalRouteConfig *aConfig, bool aLocal); + otError GetNextService(otNetworkDataIterator *aIterator, otServiceConfig *aConfig, bool aLocal); + + otError OutputBinary(bool aLocal); + void OutputPrefixes(bool aLocal); + void OutputRoutes(bool aLocal); + void OutputServices(bool aLocal); void OutputPreference(signed int aPreference); static constexpr Command sCommands[] = {