From 5632c271b8c268807735aaafeceaeaf7cfc11d68 Mon Sep 17 00:00:00 2001 From: Yakun Xu Date: Wed, 22 Apr 2020 14:23:12 +0800 Subject: [PATCH] [posix] use enumerations for RCP bus (#4871) This commit changes the way to define RCP bus type. It now defines two enumerations for UART and SPI. This ensures only a single bus type is enabled, and helps keep compatible with projects using UART bus. --- Android.mk | 4 +- src/posix/Makefile-posix | 6 +-- src/posix/main.c | 6 +-- src/posix/platform/CMakeLists.txt | 2 +- src/posix/platform/hdlc_interface.cpp | 4 +- src/posix/platform/hdlc_interface.hpp | 4 +- src/posix/platform/openthread-posix-config.h | 44 ++++++++++--------- src/posix/platform/radio.cpp | 16 +++---- src/posix/platform/spi_interface.cpp | 4 +- src/posix/platform/spi_interface.hpp | 4 +- .../openthread-core-toranj-config-posix.h | 8 +--- 11 files changed, 49 insertions(+), 53 deletions(-) diff --git a/Android.mk b/Android.mk index 9989ad1b4..804ff8102 100644 --- a/Android.mk +++ b/Android.mk @@ -74,9 +74,9 @@ OPENTHREAD_PUBLIC_CFLAGS += -DOPENTHREAD_CONFIG_UDP_FORWARD_ENABLE=1 endif ifeq ($(USE_OT_RCP_BUS), spi) -OPENTHREAD_PUBLIC_CFLAGS += -DOPENTHREAD_POSIX_CONFIG_RCP_SPI_ENABLE=1 +OPENTHREAD_PUBLIC_CFLAGS += -DOPENTHREAD_POSIX_CONFIG_RCP_BUS=OT_POSIX_RCP_BUS_SPI else -OPENTHREAD_PUBLIC_CFLAGS += -DOPENTHREAD_POSIX_CONFIG_RCP_UART_ENABLE=1 +OPENTHREAD_PUBLIC_CFLAGS += -DOPENTHREAD_POSIX_CONFIG_RCP_BUS=OT_POSIX_RCP_BUS_UART endif # Enable all optional features for CI tests. diff --git a/src/posix/Makefile-posix b/src/posix/Makefile-posix index 1d3931d0c..0a5f4077f 100644 --- a/src/posix/Makefile-posix +++ b/src/posix/Makefile-posix @@ -103,10 +103,10 @@ ifneq ($(READLINE),) configure_OPTIONS += --with-readline=$(READLINE) endif -ifeq ($(RCP_SPI),1) -COMMONCFLAGS += -DOPENTHREAD_POSIX_CONFIG_RCP_SPI_ENABLE=1 +ifeq ($(RCP_BUS),spi) +COMMONCFLAGS += -DOPENTHREAD_POSIX_CONFIG_RCP_BUS=OT_POSIX_RCP_BUS_SPI else -COMMONCFLAGS += -DOPENTHREAD_POSIX_CONFIG_RCP_UART_ENABLE=1 +COMMONCFLAGS += -DOPENTHREAD_POSIX_CONFIG_RCP_BUS=OT_POSIX_RCP_BUS_UART endif ifeq ($(VIRTUAL_TIME),1) diff --git a/src/posix/main.c b/src/posix/main.c index 3f3e4b927..49400fb95 100644 --- a/src/posix/main.c +++ b/src/posix/main.c @@ -124,7 +124,7 @@ static const struct option kOptions[] = {{"debug-level", required_argument, NULL {"ncp-dataset", no_argument, NULL, ARG_RESTORE_NCP_DATASET}, {"time-speed", required_argument, NULL, 's'}, {"verbose", no_argument, NULL, 'v'}, -#if OPENTHREAD_POSIX_CONFIG_RCP_SPI_ENABLE +#if OPENTHREAD_POSIX_CONFIG_RCP_BUS == OT_POSIX_RCP_BUS_SPI {"gpio-int-dev", required_argument, NULL, ARG_SPI_GPIO_INT_DEV}, {"gpio-int-line", required_argument, NULL, ARG_SPI_GPIO_INT_LINE}, {"gpio-reset-dev", required_argument, NULL, ARG_SPI_GPIO_RESET_DEV}, @@ -135,7 +135,7 @@ static const struct option kOptions[] = {{"debug-level", required_argument, NULL {"spi-reset-delay", required_argument, NULL, ARG_SPI_RESET_DELAY}, {"spi-align-allowance", required_argument, NULL, ARG_SPI_ALIGN_ALLOWANCE}, {"spi-small-packet", required_argument, NULL, ARG_SPI_SMALL_PACKET}, -#endif +#endif // OPENTHREAD_POSIX_CONFIG_RCP_BUS == OT_POSIX_RCP_BUS_SPI {0, 0, 0, 0}}; static void PrintUsage(const char *aProgramName, FILE *aStream, int aExitCode) @@ -154,7 +154,7 @@ static void PrintUsage(const char *aProgramName, FILE *aStream, int aExitCode) " -s --time-speed factor Time speed up factor.\n" " -v --verbose Also log to stderr.\n", aProgramName); -#if OPENTHREAD_POSIX_CONFIG_RCP_SPI_ENABLE +#if OPENTHREAD_POSIX_CONFIG_RCP_BUS == OT_POSIX_RCP_BUS_SPI fprintf(aStream, " --gpio-int-dev[=gpio-device-path]\n" " Specify a path to the Linux sysfs-exported GPIO device for the\n" diff --git a/src/posix/platform/CMakeLists.txt b/src/posix/platform/CMakeLists.txt index d6458f80c..6af5f42f1 100644 --- a/src/posix/platform/CMakeLists.txt +++ b/src/posix/platform/CMakeLists.txt @@ -45,7 +45,7 @@ endif() list(APPEND OT_PLATFORM_DEFINES "OPENTHREAD_POSIX=1" - "OPENTHREAD_POSIX_CONFIG_RCP_UART_ENABLE=1" + "OPENTHREAD_POSIX_CONFIG_RCP_BUS=OT_POSIX_RCP_BUS_UART" ) set(OT_PLATFORM_DEFINES ${OT_PLATFORM_DEFINES} PARENT_SCOPE) diff --git a/src/posix/platform/hdlc_interface.cpp b/src/posix/platform/hdlc_interface.cpp index aaa450f4f..343662de8 100644 --- a/src/posix/platform/hdlc_interface.cpp +++ b/src/posix/platform/hdlc_interface.cpp @@ -118,7 +118,7 @@ #endif // __APPLE__ -#if OPENTHREAD_POSIX_CONFIG_RCP_UART_ENABLE +#if OPENTHREAD_POSIX_CONFIG_RCP_BUS == OT_POSIX_RCP_BUS_UART using ot::Spinel::SpinelInterface; @@ -644,4 +644,4 @@ void HdlcInterface::HandleHdlcFrame(otError aError) } // namespace Posix } // namespace ot -#endif // OPENTHREAD_POSIX_CONFIG_RCP_UART_ENABLE +#endif // OPENTHREAD_POSIX_CONFIG_RCP_BUS == OT_POSIX_RCP_BUS_UART diff --git a/src/posix/platform/hdlc_interface.hpp b/src/posix/platform/hdlc_interface.hpp index 915fa7263..5441800b8 100644 --- a/src/posix/platform/hdlc_interface.hpp +++ b/src/posix/platform/hdlc_interface.hpp @@ -39,7 +39,7 @@ #include "lib/hdlc/hdlc.hpp" #include "lib/spinel/spinel_interface.hpp" -#if OPENTHREAD_POSIX_CONFIG_RCP_UART_ENABLE +#if OPENTHREAD_POSIX_CONFIG_RCP_BUS == OT_POSIX_RCP_BUS_UART namespace ot { namespace Posix { @@ -224,5 +224,5 @@ private: } // namespace Posix } // namespace ot -#endif // OPENTHREAD_POSIX_CONFIG_RCP_UART_ENABLE +#endif // OPENTHREAD_POSIX_CONFIG_RCP_BUS == OT_POSIX_RCP_BUS_UART #endif // POSIX_APP_HDLC_INTERFACE_HPP_ diff --git a/src/posix/platform/openthread-posix-config.h b/src/posix/platform/openthread-posix-config.h index 021f37c70..25202ee4a 100644 --- a/src/posix/platform/openthread-posix-config.h +++ b/src/posix/platform/openthread-posix-config.h @@ -57,26 +57,6 @@ #define OPENTHREAD_POSIX_CONFIG_DAEMON_SOCKET_BASENAME "/tmp/openthread" #endif -/** - * @def OPENTHREAD_POSIX_CONFIG_RCP_UART_ENABLE - * - * Define as 1 to enable UART interface to RCP. - * - */ -#ifndef OPENTHREAD_POSIX_CONFIG_RCP_UART_ENABLE -#define OPENTHREAD_POSIX_CONFIG_RCP_UART_ENABLE 0 -#endif - -/** - * @def OPENTHREAD_POSIX_CONFIG_RCP_SPI_ENABLE - * - * Define as 1 to enable SPI interface to RCP. - * - */ -#ifndef OPENTHREAD_POSIX_CONFIG_RCP_SPI_ENABLE -#define OPENTHREAD_POSIX_CONFIG_RCP_SPI_ENABLE 0 -#endif - /** * @def OPENTHREAD_POSIX_CONFIG_DAEMON_ENABLE * @@ -87,4 +67,28 @@ #define OPENTHREAD_POSIX_CONFIG_DAEMON_ENABLE 0 #endif +/** + * RCP bus UART. + * + * @note This value is also for simulated UART bus. + * + */ +#define OT_POSIX_RCP_BUS_UART 1 + +/** + * RCP bus SPI. + * + */ +#define OT_POSIX_RCP_BUS_SPI 2 + +/** + * @def OPENTHREAD_POSIX_CONFIG_RCP_BUS + * + * This setting configures what type of RCP bus to use. + * + */ +#ifndef OPENTHREAD_POSIX_CONFIG_RCP_BUS +#define OPENTHREAD_POSIX_CONFIG_RCP_BUS OT_POSIX_RCP_BUS_UART +#endif + #endif // OPENTHREAD_PLATFORM_CONFIG_H_ diff --git a/src/posix/platform/radio.cpp b/src/posix/platform/radio.cpp index c7b0562a0..46758d775 100644 --- a/src/posix/platform/radio.cpp +++ b/src/posix/platform/radio.cpp @@ -34,23 +34,21 @@ #include "platform-posix.h" #include "lib/spinel/radio_spinel.hpp" -#if OPENTHREAD_POSIX_CONFIG_RCP_UART_ENABLE +#if OPENTHREAD_POSIX_CONFIG_RCP_BUS == OT_POSIX_RCP_BUS_UART #include "hdlc_interface.hpp" -#elif OPENTHREAD_POSIX_CONFIG_RCP_SPI_ENABLE -#include "spi_interface.hpp" -#else -#error "Please enable either OPENTHREAD_POSIX_CONFIG_RCP_UART_ENABLE or OPENTHREAD_POSIX_CONFIG_RCP_SPI_ENABLE." -#endif -#if OPENTHREAD_POSIX_CONFIG_RCP_UART_ENABLE #if OPENTHREAD_POSIX_VIRTUAL_TIME static ot::Spinel::RadioSpinel sRadioSpinel; #else static ot::Spinel::RadioSpinel sRadioSpinel; #endif // OPENTHREAD_POSIX_VIRTUAL_TIME -#elif OPENTHREAD_POSIX_CONFIG_RCP_SPI_ENABLE +#elif OPENTHREAD_POSIX_CONFIG_RCP_BUS == OT_POSIX_RCP_BUS_SPI +#include "spi_interface.hpp" + static ot::Spinel::RadioSpinel sRadioSpinel; -#endif // OPENTHREAD_POSIX_CONFIG_RCP_SPI_ENABLE +#else +#error "OPENTHREAD_POSIX_CONFIG_RCP_BUS only allows OT_POSIX_RCP_BUS_UART and OT_POSIX_RCP_BUS_SPI!" +#endif void otPlatRadioGetIeeeEui64(otInstance *aInstance, uint8_t *aIeeeEui64) { diff --git a/src/posix/platform/spi_interface.cpp b/src/posix/platform/spi_interface.cpp index 0440f7271..35437e90b 100644 --- a/src/posix/platform/spi_interface.cpp +++ b/src/posix/platform/spi_interface.cpp @@ -54,7 +54,7 @@ #include #include -#if OPENTHREAD_POSIX_CONFIG_RCP_SPI_ENABLE +#if OPENTHREAD_POSIX_CONFIG_RCP_BUS == OT_POSIX_RCP_BUS_SPI #include #include #include @@ -792,4 +792,4 @@ void SpiInterface::LogStats(void) } // namespace Posix } // namespace ot -#endif // OPENTHREAD_POSIX_CONFIG_RCP_SPI_ENABLE +#endif // OPENTHREAD_POSIX_CONFIG_RCP_BUS == OT_POSIX_RCP_BUS_SPI diff --git a/src/posix/platform/spi_interface.hpp b/src/posix/platform/spi_interface.hpp index 07c25331a..944badbab 100644 --- a/src/posix/platform/spi_interface.hpp +++ b/src/posix/platform/spi_interface.hpp @@ -42,7 +42,7 @@ #include -#if OPENTHREAD_POSIX_CONFIG_RCP_SPI_ENABLE +#if OPENTHREAD_POSIX_CONFIG_RCP_BUS == OT_POSIX_RCP_BUS_SPI #include "ncp/ncp_spi.hpp" @@ -231,5 +231,5 @@ private: } // namespace Posix } // namespace ot -#endif // OPENTHREAD_POSIX_CONFIG_RCP_SPI_ENABLE +#endif // OPENTHREAD_POSIX_CONFIG_RCP_BUS == OT_POSIX_RCP_BUS_SPI #endif // POSIX_APP_SPI_INTERFACE_HPP_ diff --git a/tests/toranj/openthread-core-toranj-config-posix.h b/tests/toranj/openthread-core-toranj-config-posix.h index 85da1a3c7..67e65fa3d 100644 --- a/tests/toranj/openthread-core-toranj-config-posix.h +++ b/tests/toranj/openthread-core-toranj-config-posix.h @@ -61,12 +61,6 @@ */ #define OPENTHREAD_POSIX_CONFIG_RCP_PTY_ENABLE 1 -/** - * @def OPENTHREAD_POSIX_CONFIG_RCP_UART_ENABLE - * - * Define as 1 to enable UART interface to RCP. - * - */ -#define OPENTHREAD_POSIX_CONFIG_RCP_UART_ENABLE 1 +#define OPENTHREAD_POSIX_CONFIG_RCP_BUS OT_POSIX_RCP_BUS_UART #endif /* OPENTHREAD_CORE_TORANJ_CONFIG_POSIX_H_ */