From d4d81c39c930ccd5124e9ca5eb82570413a848d0 Mon Sep 17 00:00:00 2001 From: Li Cao Date: Fri, 23 Apr 2021 10:14:56 +0800 Subject: [PATCH] [cli] add cli to get All Thread Nodes multicast address (#6500) This commit adds APIs to get Link/Realm Local All Thread Nodes multicast addresses. This commit also adds cli command to get meshlocal prefix alone for writing tests. --- include/openthread/instance.h | 2 +- include/openthread/thread.h | 34 ++++++++++++++++++++++++++++ src/cli/README.md | 30 ++++++++++++++++++++++++ src/cli/cli.cpp | 21 +++++++++++++++++ src/cli/cli.hpp | 1 + src/core/api/thread_api.cpp | 14 ++++++++++++ tests/scripts/expect/_common.exp | 9 ++++++++ tests/scripts/expect/cli-ipmaddr.exp | 9 ++++++++ tools/otci/otci/otci.py | 8 +++++++ tools/otci/tests/test_otci.py | 2 ++ 10 files changed, 129 insertions(+), 1 deletion(-) diff --git a/include/openthread/instance.h b/include/openthread/instance.h index 1d4393617..dfd1d0b13 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 (106) +#define OPENTHREAD_API_VERSION (107) /** * @addtogroup api-instance diff --git a/include/openthread/thread.h b/include/openthread/thread.h index 43ae7a601..8c1792cfe 100644 --- a/include/openthread/thread.h +++ b/include/openthread/thread.h @@ -466,6 +466,40 @@ otError otThreadSetMeshLocalPrefix(otInstance *aInstance, const otMeshLocalPrefi */ const otIp6Address *otThreadGetLinkLocalIp6Address(otInstance *aInstance); +/** + * This function returns the Thread Link-Local All Thread Nodes multicast address. + * + * The address is a link-local Unicast Prefix-Based Multcast Address [RFC 3306], with: + * - flgs set to 3 (P = 1 and T = 1) + * - scop set to 2 + * - plen set to 64 + * - network prefix set to the Mesh Local Prefix + * - group ID set to 1 + * + * @param[in] aInstance A pointer to an OpenThread instance. + * + * @returns A pointer to Thread Link-Local All Thread Nodes multicast address. + * + */ +const otIp6Address *otThreadGetLinkLocalAllThreadNodesMulticastAddress(otInstance *aInstance); + +/** + * This function returns the Thread Realm-Local All Thread Nodes multicast address. + * + * The address is a realm-local Unicast Prefix-Based Multcast Address [RFC 3306], with: + * - flgs set to 3 (P = 1 and T = 1) + * - scop set to 3 + * - plen set to 64 + * - network prefix set to the Mesh Local Prefix + * - group ID set to 1 + * + * @param[in] aInstance A pointer to an OpenThread instance. + * + * @returns A pointer to Thread Realm-Local All Thread Nodes multicast address. + * + */ +const otIp6Address *otThreadGetRealmLocalAllThreadNodesMulticastAddress(otInstance *aInstance); + /** * Get the Thread Network Name. * diff --git a/src/cli/README.md b/src/cli/README.md index 522a44b07..25c615be6 100644 --- a/src/cli/README.md +++ b/src/cli/README.md @@ -1307,6 +1307,16 @@ Unsubscribe the Thread interface to the IPv6 multicast address. Done ``` +### ipmaddr llatn + +Get the Link-Local All Thread Nodes multicast address. + +``` +> ipmaddr llatn +ff32:40:fdde:ad00:beef:0:0:1 +Done +``` + ### ipmaddr promiscuous Get multicast promiscuous mode. @@ -1335,6 +1345,16 @@ Disable multicast promiscuous mode. Done ``` +### ipmaddr rlatn + +Get the Realm-Local All Thread Nodes multicast address. + +``` +> ipmaddr rlatn +ff33:40:fdde:ad00:beef:0:0:1 +Done +``` + ### joinerport \ Set the Joiner port. @@ -1995,6 +2015,16 @@ Done Done ``` +### prefix meshlocal + +Get the mesh local prefix. + +```bash +> prefix meshlocal +fdde:ad00:beef:0::/64 +Done +``` + ### prefix remove \ Invalidate a prefix in the Network Data. diff --git a/src/cli/cli.cpp b/src/cli/cli.cpp index a1dd41413..2a4c2d851 100644 --- a/src/cli/cli.cpp +++ b/src/cli/cli.cpp @@ -2091,6 +2091,16 @@ otError Interpreter::ProcessIpMulticastAddr(uint8_t aArgsLength, char *aArgs[]) { SuccessOrExit(error = ProcessMulticastPromiscuous(aArgsLength - 1, aArgs + 1)); } + else if (strcmp(aArgs[0], "llatn") == 0) + { + OutputIp6Address(*otThreadGetLinkLocalAllThreadNodesMulticastAddress(mInstance)); + OutputLine(""); + } + else if (strcmp(aArgs[0], "rlatn") == 0) + { + OutputIp6Address(*otThreadGetRealmLocalAllThreadNodesMulticastAddress(mInstance)); + OutputLine(""); + } else { ExitNow(error = OT_ERROR_INVALID_COMMAND); @@ -3612,6 +3622,11 @@ otError Interpreter::ProcessPrefix(uint8_t aArgsLength, char *aArgs[]) { SuccessOrExit(error = ProcessPrefixRemove(aArgsLength - 1, aArgs + 1)); } + else if (strcmp(aArgs[0], "meshlocal") == 0) + { + OutputPrefix(*otThreadGetMeshLocalPrefix(mInstance)); + OutputLine(""); + } else { ExitNow(error = OT_ERROR_INVALID_COMMAND); @@ -4876,6 +4891,12 @@ exit: return; } +void Interpreter::OutputPrefix(const otMeshLocalPrefix &aPrefix) +{ + OutputFormat("%x:%x:%x:%x::/64", (aPrefix.m8[0] << 8) | aPrefix.m8[1], (aPrefix.m8[2] << 8) | aPrefix.m8[3], + (aPrefix.m8[4] << 8) | aPrefix.m8[5], (aPrefix.m8[6] << 8) | aPrefix.m8[7]); +} + #if OPENTHREAD_FTD || OPENTHREAD_CONFIG_TMF_NETWORK_DIAG_MTD_ENABLE otError Interpreter::ProcessNetworkDiagnostic(uint8_t aArgsLength, char *aArgs[]) { diff --git a/src/cli/cli.hpp b/src/cli/cli.hpp index 561469790..f15d70beb 100644 --- a/src/cli/cli.hpp +++ b/src/cli/cli.hpp @@ -463,6 +463,7 @@ private: otError ProcessNetworkDataPrefix(void); otError ProcessNetworkDataRoute(void); otError ProcessNetworkDataService(void); + void OutputPrefix(const otMeshLocalPrefix &aPrefix); void OutputPrefix(const otBorderRouterConfig &aConfig); void OutputRoute(const otExternalRouteConfig &aConfig); void OutputService(const otServiceConfig &aConfig); diff --git a/src/core/api/thread_api.cpp b/src/core/api/thread_api.cpp index 7c304e8b6..580b01f08 100644 --- a/src/core/api/thread_api.cpp +++ b/src/core/api/thread_api.cpp @@ -179,6 +179,20 @@ const otIp6Address *otThreadGetLinkLocalIp6Address(otInstance *aInstance) return &instance.Get().GetLinkLocalAddress(); } +const otIp6Address *otThreadGetLinkLocalAllThreadNodesMulticastAddress(otInstance *aInstance) +{ + Instance &instance = *static_cast(aInstance); + + return &instance.Get().GetLinkLocalAllThreadNodesAddress(); +} + +const otIp6Address *otThreadGetRealmLocalAllThreadNodesMulticastAddress(otInstance *aInstance) +{ + Instance &instance = *static_cast(aInstance); + + return &instance.Get().GetRealmLocalAllThreadNodesAddress(); +} + const char *otThreadGetNetworkName(otInstance *aInstance) { Instance &instance = *static_cast(aInstance); diff --git a/tests/scripts/expect/_common.exp b/tests/scripts/expect/_common.exp index c97f211ac..33cdacd65 100644 --- a/tests/scripts/expect/_common.exp +++ b/tests/scripts/expect/_common.exp @@ -172,6 +172,15 @@ proc get_extaddr {} { return $rval } +proc get_meshlocal_prefix {} { + send "prefix meshlocal\n" + expect -re {[\r\n ](([0-9a-fA-F]{1,4}:){3}[0-9a-fA-f]{1,4})::/64(?=[\r\n>])} + set rval $expect_out(1,string) + expect_line "Done" + + return $rval +} + proc get_rloc16 {} { send "rloc16\n" expect "rloc16" diff --git a/tests/scripts/expect/cli-ipmaddr.exp b/tests/scripts/expect/cli-ipmaddr.exp index a4e324f0c..d44013d3b 100755 --- a/tests/scripts/expect/cli-ipmaddr.exp +++ b/tests/scripts/expect/cli-ipmaddr.exp @@ -40,6 +40,15 @@ expect "ff0e:0:0:0:0:0:0:1" expect_line "Done" set addr [get_ipaddr mleid] +set prefix [get_meshlocal_prefix] + +send "ipmaddr llatn\n" +expect "ff32:40:$prefix:0:1" +expect_line "Done" +send "ipmaddr rlatn\n" +expect "ff33:40:$prefix:0:1" +expect_line "Done" + switch_node 2 send "ping ff0e::1\n" expect_line "Done" diff --git a/tools/otci/otci/otci.py b/tools/otci/otci/otci.py index 53ac63ca7..08283e72d 100644 --- a/tools/otci/otci/otci.py +++ b/tools/otci/otci/otci.py @@ -1468,6 +1468,14 @@ class OTCI(object): """Disable multicast promiscuous mode.""" self.execute_command('ipmaddr promiscuous disable') + def get_ipmaddr_llatn(self) -> Ip6Addr: + """Get Link Local All Thread Nodes Multicast Address""" + return self.__parse_ip6addr(self.execute_command('ipmaddr llatn')) + + def get_ipmaddr_rlatn(self) -> Ip6Addr: + """Get Realm Local All Thread Nodes Multicast Address""" + return self.__parse_ip6addr(self.execute_command('ipmaddr rlatn')) + # # Backbone Router Utilities # diff --git a/tools/otci/tests/test_otci.py b/tools/otci/tests/test_otci.py index 43be74a46..0e0b2161c 100644 --- a/tools/otci/tests/test_otci.py +++ b/tools/otci/tests/test_otci.py @@ -196,6 +196,8 @@ class TestOTCI(unittest.TestCase): self.assertTrue(leader.get_ipaddr_rloc()) self.assertTrue(leader.get_ipaddr_linklocal()) self.assertTrue(leader.get_ipaddr_mleid()) + self.assertTrue(leader.get_ipmaddr_llatn()) + self.assertTrue(leader.get_ipmaddr_rlatn()) leader.add_ipaddr('2001::1') leader.del_ipaddr('2001::1')