From 97a3f7648fafb04ba1f688c6ff3d6754b63602fc Mon Sep 17 00:00:00 2001 From: Yang Sun Date: Sat, 14 Sep 2024 00:54:33 +0800 Subject: [PATCH] [trel] add cli to get TREL UDP port (#10702) The test software for certification currently uses the mDNS packets of trel service responses to find out the trel port, and then use the port number to determine which packets in a capture should be decoded as TREL packets. However this may not be reliable since it depends on when the capture starts. Added a cli to get trel port, so this can be used by a THCI function. --- include/openthread/instance.h | 2 +- include/openthread/trel.h | 10 ++++++++++ src/cli/README.md | 10 ++++++++++ src/cli/cli.cpp | 14 ++++++++++++++ src/core/api/trel_api.cpp | 2 ++ src/core/radio/trel_interface.hpp | 8 ++++++++ .../border_router/test_trel_connectivity.py | 2 ++ tests/scripts/thread-cert/node.py | 5 +++++ 8 files changed, 52 insertions(+), 1 deletion(-) diff --git a/include/openthread/instance.h b/include/openthread/instance.h index eba6c33d4..1a68a8d4a 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 (440) +#define OPENTHREAD_API_VERSION (441) /** * @addtogroup api-instance diff --git a/include/openthread/trel.h b/include/openthread/trel.h index e9a0bf00a..dd186fa59 100644 --- a/include/openthread/trel.h +++ b/include/openthread/trel.h @@ -184,6 +184,16 @@ const otTrelCounters *otTrelGetCounters(otInstance *aInstance); */ void otTrelResetCounters(otInstance *aInstance); +/** + * Gets the UDP port of the TREL interface. + * + * @param[in] aInstance A pointer to an OpenThread instance. + * + * @returns UDP port of the TREL interface. + * + */ +uint16_t otTrelGetUdpPort(otInstance *aInstance); + /** * @} * diff --git a/src/cli/README.md b/src/cli/README.md index 0b7c15f98..47af9a6a0 100644 --- a/src/cli/README.md +++ b/src/cli/README.md @@ -3948,6 +3948,16 @@ Done Done ``` +### trel port + +Get the TREL UDP port number. + +```bash +> trel port +49154 +Done +``` + ### tvcheck enable Enable thread version check when upgrading to router or leader. diff --git a/src/cli/cli.cpp b/src/cli/cli.cpp index 10994ca3c..5da53f96f 100644 --- a/src/cli/cli.cpp +++ b/src/cli/cli.cpp @@ -7577,6 +7577,20 @@ template <> otError Interpreter::Process(Arg aArgs[]) error = OT_ERROR_INVALID_ARGS; } } + /** + * @cli trel port + * @code + * trel port + * 49153 + * Done + * @endcode + * @par api_copy + * #otTrelGetUdpPort + */ + else if (aArgs[0] == "port") + { + OutputLine("%hu", otTrelGetUdpPort(GetInstancePtr())); + } else { error = OT_ERROR_INVALID_ARGS; diff --git a/src/core/api/trel_api.cpp b/src/core/api/trel_api.cpp index 09344d3d1..94e29a279 100644 --- a/src/core/api/trel_api.cpp +++ b/src/core/api/trel_api.cpp @@ -82,4 +82,6 @@ const otTrelCounters *otTrelGetCounters(otInstance *aInstance) void otTrelResetCounters(otInstance *aInstance) { AsCoreType(aInstance).Get().ResetCounters(); } +uint16_t otTrelGetUdpPort(otInstance *aInstance) { return AsCoreType(aInstance).Get().GetUdpPort(); } + #endif // OPENTHREAD_CONFIG_RADIO_LINK_TREL_ENABLE diff --git a/src/core/radio/trel_interface.hpp b/src/core/radio/trel_interface.hpp index 70b4eb0b5..a2e86a35a 100644 --- a/src/core/radio/trel_interface.hpp +++ b/src/core/radio/trel_interface.hpp @@ -256,6 +256,14 @@ public: */ void ResetCounters(void); + /** + * Returns the TREL UDP port. + * + * @returns The TREL UDP port. + * + */ + uint16_t GetUdpPort(void) const { return mUdpPort; } + private: #if OPENTHREAD_CONFIG_TREL_PEER_TABLE_SIZE != 0 static constexpr uint16_t kPeerTableSize = OPENTHREAD_CONFIG_TREL_PEER_TABLE_SIZE; diff --git a/tests/scripts/thread-cert/border_router/test_trel_connectivity.py b/tests/scripts/thread-cert/border_router/test_trel_connectivity.py index 20ed03932..42a3a1870 100755 --- a/tests/scripts/thread-cert/border_router/test_trel_connectivity.py +++ b/tests/scripts/thread-cert/border_router/test_trel_connectivity.py @@ -160,6 +160,8 @@ class TestTrelConnectivity(thread_cert.TestCase): self.assertTrue(counters['Outbound']['bytes'] == 0) self.assertTrue(counters['Outbound']['failures'] == 0) + self.assertGreater(br1.get_trel_port(), 0) + def verify(self, pv: PacketVerifier): pkts: PacketFilter = pv.pkts BR1_RLOC16 = pv.vars['BR1_RLOC16'] diff --git a/tests/scripts/thread-cert/node.py b/tests/scripts/thread-cert/node.py index 6da0f635b..39e801e89 100755 --- a/tests/scripts/thread-cert/node.py +++ b/tests/scripts/thread-cert/node.py @@ -1451,6 +1451,11 @@ class NodeImpl: self.send_command(cmd) self._expect_done() + def get_trel_port(self): + cmd = 'trel port' + self.send_command(cmd) + return int(self._expect_command_output()[0]) + def set_epskc(self, keystring: str, timeout=120000, port=0): cmd = 'ba ephemeralkey set ' + keystring + ' ' + str(timeout) + ' ' + str(port) self.send_command(cmd)