From 934448db6a16bea3654e08a0e6f56928593ae36d Mon Sep 17 00:00:00 2001 From: Simon Lin Date: Wed, 25 Aug 2021 12:55:01 +0800 Subject: [PATCH] [cli] add CLI command to enable/disable TREL (#6949) --- include/openthread/instance.h | 2 +- include/openthread/platform/trel-udp6.h | 1 + src/cli/README.md | 25 ++++++++++++++++ src/cli/cli.cpp | 18 +++++++++++ src/cli/cli.hpp | 6 ++++ src/posix/platform/trel_udp6.cpp | 40 +++++++++++++++++++++++-- 6 files changed, 89 insertions(+), 3 deletions(-) diff --git a/include/openthread/instance.h b/include/openthread/instance.h index 6a1afdcb6..bdb8394dc 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 (157) +#define OPENTHREAD_API_VERSION (158) /** * @addtogroup api-instance diff --git a/include/openthread/platform/trel-udp6.h b/include/openthread/platform/trel-udp6.h index 9bc834e82..4c0f9e21d 100644 --- a/include/openthread/platform/trel-udp6.h +++ b/include/openthread/platform/trel-udp6.h @@ -137,6 +137,7 @@ extern void otPlatTrelUdp6HandleReceived(otInstance *aInstance, uint8_t *aBuffer * @param[in] aEnable Indicates whether to enable/disable the TREL interface. * * @retval OT_ERROR_NONE Successfully changed the TREL interface test status (enabled/disabled). + * @retval OT_ERROR_FAILED Failed to enable the TREL interface. * @retval OT_ERROR_NOT_IMPLEMENTED This function is not provided by the platform. * */ diff --git a/src/cli/README.md b/src/cli/README.md index de08bae70..f157f18f3 100644 --- a/src/cli/README.md +++ b/src/cli/README.md @@ -106,6 +106,7 @@ Done - [state](#state) - [srp](README_SRP.md) - [thread](#thread-start) +- [trel](#trel-enable) - [txpower](#txpower) - [udp](README_UDP.md) - [unsecureport](#unsecureport-add-port) @@ -2513,6 +2514,30 @@ Get the Thread Version number. Done ``` +### trel enable + +Enable TREL radio link. + +`OPENTHREAD_CONFIG_RADIO_LINK_TREL_ENABLE` is required. + +Note: TREL radio link can be enabled only when a valid TREL URL was specified. + +```bash +> trel enable +Done +``` + +### trel disable + +Disable TREL radio link. + +`OPENTHREAD_CONFIG_RADIO_LINK_TREL_ENABLE` is required. + +```bash +> trel disable +Done +``` + ### txpower Get the transmit power in dBm. diff --git a/src/cli/cli.cpp b/src/cli/cli.cpp index 9c8e5493c..58aa7a11f 100644 --- a/src/cli/cli.cpp +++ b/src/cli/cli.cpp @@ -81,6 +81,9 @@ #if (OPENTHREAD_CONFIG_LOG_OUTPUT == OPENTHREAD_CONFIG_LOG_OUTPUT_DEBUG_UART) && OPENTHREAD_POSIX #include #endif +#if OPENTHREAD_CONFIG_RADIO_LINK_TREL_ENABLE +#include +#endif #include "common/logging.hpp" #include "common/new.hpp" @@ -4544,6 +4547,21 @@ exit: } #endif +#if OPENTHREAD_CONFIG_RADIO_LINK_TREL_ENABLE +otError Interpreter::ProcessTrel(Arg aArgs[]) +{ + otError error; + bool enable; + + SuccessOrExit(error = ParseEnableOrDisable(aArgs[0], enable)); + + error = otPlatTrelUdp6SetTestMode(mInstance, enable); + +exit: + return error; +} +#endif + #if OPENTHREAD_CONFIG_DIAG_ENABLE otError Interpreter::ProcessDiag(Arg aArgs[]) { diff --git a/src/cli/cli.hpp b/src/cli/cli.hpp index f2ca60e89..0651dc982 100644 --- a/src/cli/cli.hpp +++ b/src/cli/cli.hpp @@ -667,6 +667,9 @@ private: #if OPENTHREAD_CONFIG_REFERENCE_DEVICE_ENABLE otError ProcessMacSend(Arg aArgs[]); #endif +#if OPENTHREAD_CONFIG_RADIO_LINK_TREL_ENABLE + otError ProcessTrel(Arg aArgs[]); +#endif #if OPENTHREAD_CONFIG_PING_SENDER_ENABLE static void HandlePingReply(const otPingSenderReply *aReply, void *aContext); @@ -931,6 +934,9 @@ private: {"tcp", &Interpreter::ProcessTcp}, #endif {"thread", &Interpreter::ProcessThread}, +#if OPENTHREAD_CONFIG_RADIO_LINK_TREL_ENABLE + {"trel", &Interpreter::ProcessTrel}, +#endif {"txpower", &Interpreter::ProcessTxPower}, {"udp", &Interpreter::ProcessUdp}, {"unsecureport", &Interpreter::ProcessUnsecurePort}, diff --git a/src/posix/platform/trel_udp6.cpp b/src/posix/platform/trel_udp6.cpp index 37eaebdaf..cc5372cf2 100644 --- a/src/posix/platform/trel_udp6.cpp +++ b/src/posix/platform/trel_udp6.cpp @@ -84,6 +84,7 @@ static TxPacket sTxPacketPool[TREL_PACKET_POOL_SIZE]; static TxPacket * sFreeTxPacketHead; // A singly linked list of free/available `TxPacket` from pool. static TxPacket * sTxPacketQueueTail; // A circular linked list for queued tx packets. static char sInterfaceName[IFNAMSIZ + 1]; +static bool sInitialized = false; static bool sEnabled = false; static int sInterfaceIndex = -1; static int sMulticastSocket = -1; @@ -398,7 +399,10 @@ static void ReceivePacket(int aSocket, otInstance *aInstance) Ip6AddrToString(&sockAddr.sin6_addr), ntohs(sockAddr.sin6_port), sockAddr.sin6_scope_id, BufferToString(sRxPacketBuffer, sRxPacketLength)); - otPlatTrelUdp6HandleReceived(aInstance, sRxPacketBuffer, sRxPacketLength); + if (sEnabled) + { + otPlatTrelUdp6HandleReceived(aInstance, sRxPacketBuffer, sRxPacketLength); + } } static void InitPacketQueue(void) @@ -620,11 +624,37 @@ exit: return error; } +otError otPlatTrelUdp6SetTestMode(otInstance *aInstance, bool aEnable) +{ + OT_UNUSED_VARIABLE(aInstance); + + otError error = OT_ERROR_NONE; + + VerifyOrExit(aEnable != sEnabled); + + if (aEnable) + { + VerifyOrExit(sInitialized, error = OT_ERROR_FAILED); + } + + sEnabled = aEnable; + + if (!sEnabled) + { + InitPacketQueue(); + } + +exit: + return error; +} + //--------------------------------------------------------------------------------------------------------------------- // platformTrel system void platformTrelInit(const char *aTrelUrl) { + assert(!sInitialized); + if (aTrelUrl != NULL) { ot::Posix::RadioUrl url(aTrelUrl); @@ -642,11 +672,14 @@ void platformTrelInit(const char *aTrelUrl) InitPacketQueue(); // Disable trel platform when interface name is empty. - sEnabled = (sInterfaceName[0] != '\0'); + sInitialized = (sInterfaceName[0] != '\0'); + sEnabled = sInitialized; } void platformTrelDeinit(void) { + assert(sInitialized); + if (sSocket != -1) { close(sSocket); @@ -662,6 +695,9 @@ void platformTrelDeinit(void) RemoveUnicastAddress(&sInterfaceAddress); } + sInitialized = false; + sEnabled = false; + otLogDebgPlat("[trel] platformTrelDeinit()"); }