From 8e0af4267b9e178c15dc9812b70a535d6156dd35 Mon Sep 17 00:00:00 2001 From: Jiacheng Guo Date: Tue, 8 Jun 2021 18:12:53 +0800 Subject: [PATCH] openthread: move implementation-specific functions to private headers --- .../openthread/include/esp_openthread.h | 63 +------------- .../include/esp_openthread_defaults.h | 57 ++++++++++++ .../include/esp_openthread_netif_glue.h | 21 ----- components/openthread/port/esp_openthread.cpp | 2 +- .../port/esp_openthread_platform.cpp | 44 +++++----- .../esp_openthread_netif_glue_priv.h | 21 +++++ .../private_include/esp_openthread_platform.h | 87 +++++++++++++++++++ examples/openthread/ot_cli/main/esp_ot_cli.c | 46 ++-------- 8 files changed, 193 insertions(+), 148 deletions(-) create mode 100644 components/openthread/include/esp_openthread_defaults.h create mode 100644 components/openthread/private_include/esp_openthread_platform.h diff --git a/components/openthread/include/esp_openthread.h b/components/openthread/include/esp_openthread.h index 916555f0a6..a29b3efa36 100644 --- a/components/openthread/include/esp_openthread.h +++ b/components/openthread/include/esp_openthread.h @@ -44,8 +44,6 @@ esp_err_t esp_openthread_init(const esp_openthread_platform_config_t *init_confi * * @note Thie function will not return unless error happens when running the OpenThread stack. * - * @param[in] init_config The initialization configuration. - * * @return * - ESP_OK on success * - ESP_ERR_NO_MEM if allocation has failed @@ -55,7 +53,7 @@ esp_err_t esp_openthread_init(const esp_openthread_platform_config_t *init_confi esp_err_t esp_openthread_launch_mainloop(void); /** - * This function performs OpenThread stack and platform driver deinitialization. + * @brief This function performs OpenThread stack and platform driver deinitialization. * * @return * - ESP_OK on success @@ -64,37 +62,6 @@ esp_err_t esp_openthread_launch_mainloop(void); */ esp_err_t esp_openthread_deinit(void); -/** - * @brief Initializes the platform-specific support for the OpenThread stack. - * - * @note This function is not called by and will not call the OpenThread library. - * The user needs to call otInstanceInitSingle to intialize the OpenThread - * stack after calling this function. - * - * @param[in] init_config The initialization configuration. - * - * @return - * - ESP_OK on success - * - ESP_ERR_NO_MEM if allocation has failed - * - ESP_ERR_INVALID_ARG if radio or host connection mode not supported - * - ESP_ERR_INVALID_STATE if already initialized - * - */ -esp_err_t esp_openthread_platform_init(const esp_openthread_platform_config_t *init_config); - -/** - * This function performs all platform-specific deinitialization for OpenThread's drivers. - * - * @note This function is not called by the OpenThread library. Instead, the user should - * call this function when deinitialization of OpenThread's drivers is most appropriate. - * - * @return - * - ESP_OK on success - * - ESP_ERR_INVALID_STATE if not initialized - * - */ -esp_err_t esp_openthread_platform_deinit(void); - /** * @brief This function acquires the underlying OpenThread instance. * @@ -105,34 +72,6 @@ esp_err_t esp_openthread_platform_deinit(void); */ otInstance *esp_openthread_get_instance(void); -/** - * @brief This function updates the platform fds and timeouts - * - * @note This function will not update the OpenThread core stack pending events. - * The users need to call `otTaskletsArePending` to check whether there being - * pending OpenThread tasks. - * - * @param[inout] mainloop The main loop context. - * - */ -void esp_openthread_platform_update(esp_openthread_mainloop_context_t *mainloop); - -/** - * @brief This function performs the OpenThread related platform process (radio, uart, alarm etc.) - * - * @note This function will call the OpenThread core stack process functions. - * The users need to call `otTaskletsProcess` by self. - * - * @param[in] instance The OpenThread instance. - * @param[in] mainloop The main loop context. - * - * @return - * - ESP_OK on success - * - ESP_FAIL on failure - * - */ -esp_err_t esp_openthread_platform_process(otInstance *instance, const esp_openthread_mainloop_context_t *mainloop); - #ifdef __cplusplus } // end of extern "C" #endif diff --git a/components/openthread/include/esp_openthread_defaults.h b/components/openthread/include/esp_openthread_defaults.h new file mode 100644 index 0000000000..cbbfc15ac5 --- /dev/null +++ b/components/openthread/include/esp_openthread_defaults.h @@ -0,0 +1,57 @@ +// Copyright 2021 Espressif Systems (Shanghai) CO LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at + +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License + +#pragma once + +#include "esp_openthread_types.h" + +#define ESP_OPENTHREAD_DEFAULT_RADIO_UART_RCP_CONFIG(pin_rx, pin_tx) \ + { \ + .radio_mode = RADIO_MODE_UART_RCP, \ + .radio_uart_config = { \ + .port = 1, \ + .uart_config = \ + { \ + .baud_rate = 115200, \ + .data_bits = UART_DATA_8_BITS, \ + .parity = UART_PARITY_DISABLE, \ + .stop_bits = UART_STOP_BITS_1, \ + .flow_ctrl = UART_HW_FLOWCTRL_DISABLE, \ + .rx_flow_ctrl_thresh = 0, \ + .source_clk = UART_SCLK_APB, \ + }, \ + .rx_pin = pin_rx, \ + .tx_pin = pin_tx, \ + }, \ + } + +#define ESP_OPENTHREAD_DEFAULT_UART_HOST_CONFIG() \ + { \ + .host_connection_mode = HOST_CONNECTION_MODE_UART, \ + .host_uart_config = { \ + .port = 0, \ + .uart_config = \ + { \ + .baud_rate = 115200, \ + .data_bits = UART_DATA_8_BITS, \ + .parity = UART_PARITY_DISABLE, \ + .stop_bits = UART_STOP_BITS_1, \ + .flow_ctrl = UART_HW_FLOWCTRL_DISABLE, \ + .rx_flow_ctrl_thresh = 0, \ + .source_clk = UART_SCLK_APB, \ + }, \ + .rx_pin = UART_PIN_NO_CHANGE, \ + .tx_pin = UART_PIN_NO_CHANGE, \ + }, \ + } diff --git a/components/openthread/include/esp_openthread_netif_glue.h b/components/openthread/include/esp_openthread_netif_glue.h index 035f201fd4..319362a172 100644 --- a/components/openthread/include/esp_openthread_netif_glue.h +++ b/components/openthread/include/esp_openthread_netif_glue.h @@ -38,27 +38,6 @@ void *esp_openthread_netif_glue_init(void); */ void esp_openthread_netif_glue_deinit(void); -/** - * @brief This function updates the netif fds and timeouts to the main loop. - * - * @param[inout] mainloop The main loop context. - * - */ -void esp_openthread_netif_glue_update(esp_openthread_mainloop_context_t *mainloop); - -/** - * @brief This function performs the netif process. - * - * @param[in] instance The OpenThread instance. - * - * @return - * - ESP_OK on success - * - ESP_FAIL on OpenThread failure - * - ESP_ERR_NO_MEM on memory allocation failure - * - */ -esp_err_t esp_openthread_netif_glue_process(otInstance *instance, const esp_openthread_mainloop_context_t *mainloop); - #ifdef __cplusplus } #endif diff --git a/components/openthread/port/esp_openthread.cpp b/components/openthread/port/esp_openthread.cpp index 67e3504ae7..f3b6d37acd 100644 --- a/components/openthread/port/esp_openthread.cpp +++ b/components/openthread/port/esp_openthread.cpp @@ -16,11 +16,11 @@ #include "esp_openthread_common_macro.h" #include "esp_openthread_lock.h" #include "esp_openthread_netif_glue_priv.h" +#include "esp_openthread_platform.h" #include "esp_openthread_types.h" #include "freertos/FreeRTOS.h" #include "openthread/instance.h" #include "openthread/tasklet.h" -#include "platform/logging.h" static void esp_openthread_state_callback(otChangedFlags changed_flags, void *ctx) { diff --git a/components/openthread/port/esp_openthread_platform.cpp b/components/openthread/port/esp_openthread_platform.cpp index a489c31c8a..a5e736f8e5 100644 --- a/components/openthread/port/esp_openthread_platform.cpp +++ b/components/openthread/port/esp_openthread_platform.cpp @@ -11,7 +11,7 @@ // See the License for the specific language governing permissions and // limitations under the License -#include "esp_openthread.h" +#include "esp_openthread_platform.h" #include "esp_check.h" #include "esp_err.h" @@ -20,6 +20,7 @@ #include "esp_openthread_common_macro.h" #include "esp_openthread_lock.h" #include "esp_openthread_netif_glue.h" +#include "esp_openthread_netif_glue_priv.h" #include "esp_openthread_radio_uart.h" #include "esp_openthread_types.h" #include "esp_openthread_uart.h" @@ -30,8 +31,6 @@ #include "freertos/queue.h" #include "openthread/cli.h" #include "openthread/instance.h" -#include "openthread/platform/alarm-milli.h" -#include "openthread/platform/time.h" #include "openthread/tasklet.h" static esp_openthread_platform_config_t s_platform_config; @@ -39,33 +38,29 @@ static bool s_openthread_platform_initialized = false; esp_err_t esp_openthread_platform_init(const esp_openthread_platform_config_t *config) { - if (config->radio_config.radio_mode != RADIO_MODE_UART_RCP) { - otLogCritPlat("Radio mode not supported"); - return ESP_ERR_INVALID_ARG; - } - if (config->host_config.host_connection_mode != HOST_CONNECTION_MODE_NONE && - config->host_config.host_connection_mode != HOST_CONNECTION_MODE_UART) { - otLogCritPlat("Host connection mode not supported"); - return ESP_ERR_INVALID_ARG; - } - if (s_openthread_platform_initialized) { - return ESP_ERR_INVALID_STATE; - } + ESP_RETURN_ON_FALSE(config->radio_config.radio_mode == RADIO_MODE_UART_RCP, ESP_ERR_INVALID_ARG, OT_PLAT_LOG_TAG, + "Radio mode not supported"); + ESP_RETURN_ON_FALSE(config->host_config.host_connection_mode == HOST_CONNECTION_MODE_NONE || + config->host_config.host_connection_mode == HOST_CONNECTION_MODE_UART, + ESP_ERR_INVALID_ARG, OT_PLAT_LOG_TAG, "Host connection mode not supported"); + ESP_RETURN_ON_FALSE(!s_openthread_platform_initialized, ESP_ERR_INVALID_STATE, OT_PLAT_LOG_TAG, + "OpenThread platform already initialized"); - esp_err_t error = ESP_OK; + esp_err_t ret = ESP_OK; s_platform_config = *config; - SuccessOrExit(error = esp_openthread_lock_init()); + ESP_GOTO_ON_ERROR(esp_openthread_lock_init(), exit, OT_PLAT_LOG_TAG, "esp_openthread_lock_init failed"); if (config->host_config.host_connection_mode == HOST_CONNECTION_MODE_UART) { - SuccessOrExit(error = esp_openthread_uart_init(config)); + ESP_GOTO_ON_ERROR(esp_openthread_uart_init(config), exit, OT_PLAT_LOG_TAG, "esp_openthread_uart_init failed"); } - SuccessOrExit(error = esp_openthread_radio_init(config)); + ESP_GOTO_ON_ERROR(esp_openthread_radio_init(config), exit, OT_PLAT_LOG_TAG, "esp_openthread_radio_init failed"); + exit: - if (error != ESP_OK) { + if (ret != ESP_OK) { esp_openthread_platform_deinit(); } - return error; + return ret; } otInstance *esp_openthread_get_instance(void) @@ -75,14 +70,15 @@ otInstance *esp_openthread_get_instance(void) esp_err_t esp_openthread_platform_deinit(void) { - if (!s_openthread_platform_initialized) { - return ESP_ERR_INVALID_STATE; - } + ESP_RETURN_ON_FALSE(s_openthread_platform_initialized, ESP_ERR_INVALID_STATE, OT_PLAT_LOG_TAG, + "OpenThread platform not initialized"); + esp_openthread_radio_deinit(); if (s_platform_config.host_config.host_connection_mode == HOST_CONNECTION_MODE_UART) { esp_openthread_uart_deinit(); } esp_openthread_lock_deinit(); + return ESP_OK; } diff --git a/components/openthread/private_include/esp_openthread_netif_glue_priv.h b/components/openthread/private_include/esp_openthread_netif_glue_priv.h index 4a193474f6..b9c06f5e45 100644 --- a/components/openthread/private_include/esp_openthread_netif_glue_priv.h +++ b/components/openthread/private_include/esp_openthread_netif_glue_priv.h @@ -29,6 +29,27 @@ extern "C" { */ void esp_openthread_netif_glue_state_callback(otChangedFlags changed_flags); +/** + * @brief This function updates the netif fds and timeouts to the main loop. + * + * @param[inout] mainloop The main loop context. + * + */ +void esp_openthread_netif_glue_update(esp_openthread_mainloop_context_t *mainloop); + +/** + * @brief This function performs the netif process. + * + * @param[in] instance The OpenThread instance. + * + * @return + * - ESP_OK on success + * - ESP_FAIL on OpenThread failure + * - ESP_ERR_NO_MEM on memory allocation failure + * + */ +esp_err_t esp_openthread_netif_glue_process(otInstance *instance, const esp_openthread_mainloop_context_t *mainloop); + #ifdef __cplusplus } #endif diff --git a/components/openthread/private_include/esp_openthread_platform.h b/components/openthread/private_include/esp_openthread_platform.h new file mode 100644 index 0000000000..05bcabb813 --- /dev/null +++ b/components/openthread/private_include/esp_openthread_platform.h @@ -0,0 +1,87 @@ +// Copyright 2021 Espressif Systems (Shanghai) CO LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at + +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License + +#pragma once + +#include "esp_err.h" +#include "esp_openthread_types.h" +#include "openthread/error.h" +#include "openthread/instance.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @brief Initializes the platform-specific support for the OpenThread stack. + * + * @note This function is not called by and will not call the OpenThread library. + * The user needs to call otInstanceInitSingle to intialize the OpenThread + * stack after calling this function. + * + * @param[in] init_config The initialization configuration. + * + * @return + * - ESP_OK on success + * - ESP_ERR_NO_MEM if allocation has failed + * - ESP_ERR_INVALID_ARG if radio or host connection mode not supported + * - ESP_ERR_INVALID_STATE if already initialized + * + */ +esp_err_t esp_openthread_platform_init(const esp_openthread_platform_config_t *init_config); + +/** + * This function performs all platform-specific deinitialization for OpenThread's drivers. + * + * @note This function is not called by the OpenThread library. Instead, the user should + * call this function when deinitialization of OpenThread's drivers is most appropriate. + * + * @return + * - ESP_OK on success + * - ESP_ERR_INVALID_STATE if not initialized + * + */ +esp_err_t esp_openthread_platform_deinit(void); + +/** + * @brief This function updates the platform fds and timeouts + * + * @note This function will not update the OpenThread core stack pending events. + * The users need to call `otTaskletsArePending` to check whether there being + * pending OpenThread tasks. + * + * @param[inout] mainloop The main loop context. + * + */ +void esp_openthread_platform_update(esp_openthread_mainloop_context_t *mainloop); + +/** + * @brief This function performs the OpenThread related platform process (radio, uart, alarm etc.) + * + * @note This function will call the OpenThread core stack process functions. + * The users need to call `otTaskletsProcess` by self. + * + * @param[in] instance The OpenThread instance. + * @param[in] mainloop The main loop context. + * + * @return + * - ESP_OK on success + * - ESP_FAIL on failure + * + */ +esp_err_t esp_openthread_platform_process(otInstance *instance, const esp_openthread_mainloop_context_t *mainloop); + +#ifdef __cplusplus +} // end of extern "C" +#endif diff --git a/examples/openthread/ot_cli/main/esp_ot_cli.c b/examples/openthread/ot_cli/main/esp_ot_cli.c index 757485f9fd..c1adb45e5d 100644 --- a/examples/openthread/ot_cli/main/esp_ot_cli.c +++ b/examples/openthread/ot_cli/main/esp_ot_cli.c @@ -21,6 +21,7 @@ #include "esp_netif.h" #include "esp_netif_types.h" #include "esp_openthread.h" +#include "esp_openthread_defaults.h" #include "esp_openthread_lock.h" #include "esp_openthread_netif_glue.h" #include "esp_openthread_types.h" @@ -55,46 +56,8 @@ static esp_netif_t *init_openthread_netif(void) static void ot_task_worker(void *aContext) { esp_openthread_platform_config_t config = { - .radio_config = - { - .radio_mode = RADIO_MODE_UART_RCP, - .radio_uart_config = - { - .port = 1, - .uart_config = - { - .baud_rate = 115200, - .data_bits = UART_DATA_8_BITS, - .parity = UART_PARITY_DISABLE, - .stop_bits = UART_STOP_BITS_1, - .flow_ctrl = UART_HW_FLOWCTRL_DISABLE, - .rx_flow_ctrl_thresh = 0, - .source_clk = UART_SCLK_APB, - }, - .rx_pin = 4, - .tx_pin = 5, - }, - }, - .host_config = - { - .host_connection_mode = HOST_CONNECTION_MODE_UART, - .host_uart_config = - { - .port = 0, - .uart_config = - { - .baud_rate = 115200, - .data_bits = UART_DATA_8_BITS, - .parity = UART_PARITY_DISABLE, - .stop_bits = UART_STOP_BITS_1, - .flow_ctrl = UART_HW_FLOWCTRL_DISABLE, - .rx_flow_ctrl_thresh = 0, - .source_clk = UART_SCLK_APB, - }, - .rx_pin = UART_PIN_NO_CHANGE, - .tx_pin = UART_PIN_NO_CHANGE, - }, - }, + .radio_config = ESP_OPENTHREAD_DEFAULT_RADIO_UART_RCP_CONFIG(4, 5), + .host_config = ESP_OPENTHREAD_DEFAULT_UART_HOST_CONFIG(), }; esp_netif_t *openthread_netif; @@ -123,6 +86,9 @@ static void ot_task_worker(void *aContext) void app_main(void) { + // Used eventfds: + // * netif + // * radio driver esp_vfs_eventfd_config_t eventfd_config = { .max_fds = 2, };