From 8ff85260612291fe6a408df311dc8f673590dd7c Mon Sep 17 00:00:00 2001 From: Abtin Keshavarzian Date: Sun, 20 Dec 2020 16:18:58 -0800 Subject: [PATCH] [posix] disable trel platform when empty/no trel interface name is given (#5971) This commit changes the trel platform implementation under posix such that if the interface name used for trel is empty, the platform layer would disable trel. This is done by having all `otPlatTrel` functions returning immediately without performing any action. This commit also changes the `OPENTHREAD_CONFIG_POSIX_APP_TREL_INTERFACE_NAME` to be empty string. This is the posix config option specifying the interface name to be used by the trel platform layer. Note that the trel interface name can be given using this build-time config, or through the `-trel-interface` (or `-t`) input arg option. When the input arg is not used, the interface name is determined from the build-time config `POSIX_APP_TREL_INTERFACE_NAME`. This commit therefore makes the default behavior when `-t` is not used to disable trel platform. --- src/posix/platform/openthread-posix-config.h | 4 +-- src/posix/platform/trel_udp6.cpp | 30 ++++++++++++++++--- .../openthread-core-toranj-config-posix.h | 8 +++++ tests/toranj/openthread-core-toranj-config.h | 8 ----- 4 files changed, 36 insertions(+), 14 deletions(-) diff --git a/src/posix/platform/openthread-posix-config.h b/src/posix/platform/openthread-posix-config.h index e122b8b05..321dd2eab 100644 --- a/src/posix/platform/openthread-posix-config.h +++ b/src/posix/platform/openthread-posix-config.h @@ -50,11 +50,11 @@ /** * @def OPENTHREAD_CONFIG_POSIX_APP_TREL_INTERFACE_NAME * - * Defines the default interface name used for TREL UDP6 platform. + * Defines the default interface name used for TREL UDP6 platform. Empty string disables TREL platform. * */ #ifndef OPENTHREAD_CONFIG_POSIX_APP_TREL_INTERFACE_NAME -#define OPENTHREAD_CONFIG_POSIX_APP_TREL_INTERFACE_NAME "trel" +#define OPENTHREAD_CONFIG_POSIX_APP_TREL_INTERFACE_NAME "" #endif /** diff --git a/src/posix/platform/trel_udp6.cpp b/src/posix/platform/trel_udp6.cpp index d21f16641..1b3ff0ce9 100644 --- a/src/posix/platform/trel_udp6.cpp +++ b/src/posix/platform/trel_udp6.cpp @@ -71,6 +71,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 sEnabled = false; static int sInterfaceIndex = -1; static int sMulticastSocket = -1; static int sSocket = -1; @@ -413,9 +414,13 @@ exit: void otPlatTrelUdp6Init(otInstance *aInstance, const otIp6Address *aUnicastAddress, uint16_t aUdpPort) { + OT_UNUSED_VARIABLE(aInstance); + int val; struct sockaddr_in6 sockAddr; + VerifyOrExit(sEnabled); + otLogDebgPlat("[trel] otPlatTrelUdp6Init(%s, port:%d)", Ip6AddrToString(aUnicastAddress), aUdpPort); sUdpPort = aUdpPort; @@ -453,11 +458,16 @@ void otPlatTrelUdp6Init(otInstance *aInstance, const otIp6Address *aUnicastAddre PrepareSocket(); - OT_UNUSED_VARIABLE(aInstance); +exit: + return; } void otPlatTrelUdp6UpdateAddress(otInstance *aInstance, const otIp6Address *aUnicastAddress) { + OT_UNUSED_VARIABLE(aInstance); + + VerifyOrExit(sEnabled); + assert(sSocket >= 0); otLogDebgPlat("[trel] otPlatTrelUdp6UpdateAddress(%s)", Ip6AddrToString(aUnicastAddress)); @@ -473,14 +483,16 @@ void otPlatTrelUdp6UpdateAddress(otInstance *aInstance, const otIp6Address *aUni PrepareSocket(); exit: - OT_UNUSED_VARIABLE(aInstance); + return; } void otPlatTrelUdp6SubscribeMulticastAddress(otInstance *aInstance, const otIp6Address *aMulticastAddress) { + OT_UNUSED_VARIABLE(aInstance); + struct ipv6_mreq mr; - OT_UNUSED_VARIABLE(aInstance); + VerifyOrExit(sEnabled); assert(sMulticastSocket != -1); @@ -489,6 +501,9 @@ void otPlatTrelUdp6SubscribeMulticastAddress(otInstance *aInstance, const otIp6A VerifyOrDie(setsockopt(sMulticastSocket, IPPROTO_IPV6, IPV6_JOIN_GROUP, &mr, sizeof(mr)) == 0, OT_EXIT_ERROR_ERRNO); otLogDebgPlat("[trel] otPlatTrelUdp6SubscribeMulticastAddress(%s)", Ip6AddrToString(aMulticastAddress)); + +exit: + return; } otError otPlatTrelUdp6SendTo(otInstance * aInstance, @@ -500,6 +515,8 @@ otError otPlatTrelUdp6SendTo(otInstance * aInstance, otError error = OT_ERROR_NONE; + VerifyOrExit(sEnabled); + assert(aLength <= TREL_MAX_PACKET_SIZE); otLogDebgPlat("[trel] otPlatTrelUdp6SendTo(%s) %s", Ip6AddrToString(aDestAddress), @@ -524,6 +541,7 @@ otError otPlatTrelUdp6SendTo(otInstance * aInstance, } } +exit: return error; } @@ -545,6 +563,9 @@ void platformTrelInit(const char *aInterfaceName) otLogDebgPlat("[trel] platformTrelInit(InterfaceName:\"%s\")", sInterfaceName); InitPacketQueue(); + + // Disable trel platform when interface name is empty. + sEnabled = (sInterfaceName[0] != '\0'); } void platformTrelDeinit(void) @@ -564,6 +585,8 @@ void platformTrelDeinit(void) void platformTrelUpdateFdSet(fd_set *aReadFdSet, fd_set *aWriteFdSet, int *aMaxFd, struct timeval *aTimeout) { + OT_UNUSED_VARIABLE(aTimeout); + assert((aReadFdSet != NULL) && (aWriteFdSet != NULL) && (aMaxFd != NULL) && (aTimeout != NULL)); VerifyOrExit((sSocket >= 0) && (sMulticastSocket >= 0)); @@ -586,7 +609,6 @@ void platformTrelUpdateFdSet(fd_set *aReadFdSet, fd_set *aWriteFdSet, int *aMaxF } exit: - OT_UNUSED_VARIABLE(aTimeout); return; } diff --git a/tests/toranj/openthread-core-toranj-config-posix.h b/tests/toranj/openthread-core-toranj-config-posix.h index 67e65fa3d..fae209594 100644 --- a/tests/toranj/openthread-core-toranj-config-posix.h +++ b/tests/toranj/openthread-core-toranj-config-posix.h @@ -53,6 +53,14 @@ */ #define OPENTHREAD_CONFIG_BORDER_ROUTER_ENABLE 1 +/** + * @def OPENTHREAD_CONFIG_POSIX_APP_TREL_INTERFACE_NAME + * + * Defines the default interface name used for TREL UDP6 platform. Empty string disables TREL platform. + * + */ +#define OPENTHREAD_CONFIG_POSIX_APP_TREL_INTERFACE_NAME "trel" + /** * @def OPENTHREAD_POSIX_CONFIG_RCP_PTY_ENABLE * diff --git a/tests/toranj/openthread-core-toranj-config.h b/tests/toranj/openthread-core-toranj-config.h index d0534a330..0d0c328dd 100644 --- a/tests/toranj/openthread-core-toranj-config.h +++ b/tests/toranj/openthread-core-toranj-config.h @@ -470,14 +470,6 @@ */ #define OPENTHREAD_CONFIG_DNS_CLIENT_ENABLE 1 -/** - * @def OPENTHREAD_CONFIG_POSIX_APP_TREL_INTERFACE_NAME - * - * Defines the default interface name used for TREL UDP6 platform. - * - */ -#define OPENTHREAD_CONFIG_POSIX_APP_TREL_INTERFACE_NAME "trel" - #if OPENTHREAD_RADIO /** * @def OPENTHREAD_CONFIG_MAC_SOFTWARE_ACK_TIMEOUT_ENABLE