From 494e0ab170bd4545b263ed3ea5ad9c8f9ccf4eb2 Mon Sep 17 00:00:00 2001 From: Xu Si Yu Date: Wed, 29 May 2024 20:52:10 +0800 Subject: [PATCH] fix(openthread): remove the empty task for openthread tasklets --- .codespellrc | 2 +- components/openthread/src/esp_openthread.cpp | 14 +-- .../src/esp_openthread_platform.cpp | 5 +- .../src/esp_openthread_task_queue.c | 11 ++- .../openthread/src/port/esp_openthread_udp.c | 90 +++++++++---------- .../src/port/esp_spi_spinel_interface.cpp | 4 +- .../src/port/esp_uart_spinel_interface.cpp | 4 +- .../esp_radio_spinel_uart_interface.cpp | 4 +- 8 files changed, 65 insertions(+), 69 deletions(-) diff --git a/.codespellrc b/.codespellrc index 25a3e29489..ff26c69e4c 100644 --- a/.codespellrc +++ b/.codespellrc @@ -1,4 +1,4 @@ [codespell] skip = build,*.yuv,components/fatfs/src/*,alice.txt,*.rgb,components/wpa_supplicant/*,components/esp_wifi/* -ignore-words-list = ser,dout,rsource,fram,inout,shs,ans,aci,unstall,unstalling,hart,wheight,wel +ignore-words-list = ser,dout,rsource,fram,inout,shs,ans,aci,unstall,unstalling,hart,wheight,wel,ot write-changes = true diff --git a/components/openthread/src/esp_openthread.cpp b/components/openthread/src/esp_openthread.cpp index 60eba6153d..87c7b4986e 100644 --- a/components/openthread/src/esp_openthread.cpp +++ b/components/openthread/src/esp_openthread.cpp @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2021-2023 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2021-2024 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -129,7 +129,7 @@ esp_err_t esp_openthread_auto_start(otOperationalDatasetTlvs *datasetTlvs) memcpy(dataset.mMeshLocalPrefix.m8, prefix.mPrefix.mFields.m8, sizeof(dataset.mMeshLocalPrefix.m8)); dataset.mComponents.mIsMeshLocalPrefixPresent = true; } else { - ESP_LOGE("Falied to parse mesh local prefix", CONFIG_OPENTHREAD_MESH_LOCAL_PREFIX); + ESP_LOGE("Failed to parse mesh local prefix", CONFIG_OPENTHREAD_MESH_LOCAL_PREFIX); } // Network Key @@ -213,13 +213,3 @@ esp_err_t esp_openthread_deinit(void) otInstanceFinalize(esp_openthread_get_instance()); return esp_openthread_platform_deinit(); } - -static void stub_task(void *context) -{ - // this is a empty function used for ot-task signal pending -} - -void otTaskletsSignalPending(otInstance *aInstance) -{ - esp_openthread_task_queue_post(stub_task, NULL); -} diff --git a/components/openthread/src/esp_openthread_platform.cpp b/components/openthread/src/esp_openthread_platform.cpp index f6f29e278e..3e73ef362a 100644 --- a/components/openthread/src/esp_openthread_platform.cpp +++ b/components/openthread/src/esp_openthread_platform.cpp @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2021-2023 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2021-2024 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -34,7 +34,7 @@ static esp_openthread_platform_workflow_t *s_workflow_list = NULL; esp_err_t esp_openthread_platform_workflow_register(esp_openthread_update_func update_func, esp_openthread_process_func process_func, const char *name) { - uint8_t name_len = strnlen(name, WORKFLOW_MAX_NAMELEN); + uint8_t name_len = strnlen(name, WORKFLOW_MAX_NAMELEN - 1); esp_openthread_platform_workflow_t *current_workflow = s_workflow_list; esp_openthread_platform_workflow_t *before_workflow = NULL; esp_openthread_platform_workflow_t *add_workflow = @@ -42,6 +42,7 @@ esp_err_t esp_openthread_platform_workflow_register(esp_openthread_update_func u ESP_RETURN_ON_FALSE(add_workflow != NULL, ESP_ERR_NO_MEM, OT_PLAT_LOG_TAG, "Failed to alloc memory for esp_openthread_workflow"); strncpy(add_workflow->name, name, name_len); + add_workflow->name[name_len] = '\0'; add_workflow->update_func = update_func; add_workflow->process_func = process_func; add_workflow->next = NULL; diff --git a/components/openthread/src/esp_openthread_task_queue.c b/components/openthread/src/esp_openthread_task_queue.c index 4acfaad722..32bb4f2002 100644 --- a/components/openthread/src/esp_openthread_task_queue.c +++ b/components/openthread/src/esp_openthread_task_queue.c @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2021-2024 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -15,6 +15,7 @@ #include "freertos/FreeRTOS.h" #include "freertos/portmacro.h" #include "freertos/queue.h" +#include "openthread/tasklet.h" static QueueHandle_t s_task_queue = NULL; static int s_task_queue_event_fd = -1; @@ -37,6 +38,14 @@ esp_err_t esp_openthread_task_queue_init(const esp_openthread_platform_config_t &esp_openthread_task_queue_process, task_queue_workflow); } +void otTaskletsSignalPending(otInstance *aInstance) +{ + uint64_t val = 1; + ssize_t ret; + ret = write(s_task_queue_event_fd, &val, sizeof(val)); + assert(ret == sizeof(val)); +} + esp_err_t IRAM_ATTR esp_openthread_task_queue_post(esp_openthread_task_t task, void *arg) { task_storage_t task_storage = { diff --git a/components/openthread/src/port/esp_openthread_udp.c b/components/openthread/src/port/esp_openthread_udp.c index 852c0a920a..fd118b2a0f 100644 --- a/components/openthread/src/port/esp_openthread_udp.c +++ b/components/openthread/src/port/esp_openthread_udp.c @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2021-2023 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2021-2024 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -20,6 +20,7 @@ #include "esp_openthread_task_queue.h" #include "freertos/FreeRTOS.h" #include "freertos/task.h" +#include "lwip/err.h" #include "lwip/ip6.h" #include "lwip/ip6_addr.h" #include "lwip/ip_addr.h" @@ -40,24 +41,19 @@ typedef struct { } udp_recv_task_t; typedef struct { - TaskHandle_t source_task; otUdpSocket *socket; struct udp_pcb *pcb_ret; } udp_new_task_t; typedef struct { - TaskHandle_t source_task; struct udp_pcb *pcb; ip_addr_t addr; uint16_t port; - err_t ret; } udp_bind_connect_task_t; typedef struct { - TaskHandle_t source_task; struct udp_pcb *pcb; uint8_t netif_index; - esp_err_t err; } udp_bind_netif_task_t; typedef struct { @@ -78,13 +74,6 @@ typedef struct { ip6_addr_t addr; } udp_multicast_join_leave_task_t; -static void wait_for_task_notification(void) -{ - esp_openthread_task_switching_lock_release(); - ulTaskNotifyTake(pdTRUE, portMAX_DELAY); - esp_openthread_task_switching_lock_acquire(portMAX_DELAY); -} - static ip_addr_t map_openthread_addr_to_lwip_addr(const otIp6Address *address) { ip_addr_t addr; @@ -185,23 +174,25 @@ static void handle_udp_recv(void *ctx, struct udp_pcb *pcb, struct pbuf *p, cons } } -static void udp_new_task(void *ctx) +static esp_err_t udp_new_task(void *ctx) { udp_new_task_t *task = (udp_new_task_t *)ctx; - task->pcb_ret = udp_new(); + ESP_RETURN_ON_FALSE(task->pcb_ret != NULL, ESP_ERR_NO_MEM, OT_PLAT_LOG_TAG, "Failed to create a new UDP pcb"); udp_recv(task->pcb_ret, handle_udp_recv, task->socket); - xTaskNotifyGive(task->source_task); + return ESP_OK; } otError otPlatUdpSocket(otUdpSocket *udp_socket) { otError error = OT_ERROR_NONE; + esp_err_t err = ESP_OK; - udp_new_task_t task = { .source_task = xTaskGetCurrentTaskHandle(), .socket = udp_socket }; - tcpip_callback(udp_new_task, &task); - wait_for_task_notification(); - VerifyOrExit(task.pcb_ret != NULL, error = OT_ERROR_FAILED); + udp_new_task_t task = {.socket = udp_socket }; + esp_openthread_task_switching_lock_release(); + err = esp_netif_tcpip_exec(udp_new_task, &task); + esp_openthread_task_switching_lock_acquire(portMAX_DELAY); + VerifyOrExit(err == ESP_OK, error = OT_ERROR_FAILED); udp_socket->mHandle = task.pcb_ret; exit: @@ -220,24 +211,25 @@ otError otPlatUdpClose(otUdpSocket *udp_socket) struct udp_pcb *pcb = (struct udp_pcb *)udp_socket->mHandle; if (pcb) { + esp_openthread_task_switching_lock_release(); tcpip_callback(udp_close_task, pcb); + esp_openthread_task_switching_lock_acquire(portMAX_DELAY); } return OT_ERROR_NONE; } -static void udp_bind_task(void *ctx) +static esp_err_t udp_bind_task(void *ctx) { udp_bind_connect_task_t *task = (udp_bind_connect_task_t *)ctx; - - task->ret = udp_bind(task->pcb, &task->addr, task->port); - xTaskNotifyGive(task->source_task); + err_t ret = udp_bind(task->pcb, &task->addr, task->port); + return (ret == ERR_OK) ? ESP_OK : ESP_FAIL; } otError otPlatUdpBind(otUdpSocket *udp_socket) { + esp_err_t err = ESP_OK; udp_bind_connect_task_t task = { - .source_task = xTaskGetCurrentTaskHandle(), .pcb = (struct udp_pcb *)udp_socket->mHandle, .port = udp_socket->mSockName.mPort, }; @@ -247,17 +239,18 @@ otError otPlatUdpBind(otUdpSocket *udp_socket) task.addr.type = IPADDR_TYPE_ANY; #endif memcpy(ip_2_ip6(&task.addr)->addr, udp_socket->mSockName.mAddress.mFields.m8, sizeof(ip_2_ip6(&task.addr)->addr)); - tcpip_callback(udp_bind_task, &task); - wait_for_task_notification(); + esp_openthread_task_switching_lock_release(); + err = esp_netif_tcpip_exec(udp_bind_task, &task); + esp_openthread_task_switching_lock_acquire(portMAX_DELAY); - return task.ret == ERR_OK ? OT_ERROR_NONE : OT_ERROR_FAILED; + return err == ESP_OK ? OT_ERROR_NONE : OT_ERROR_FAILED; } -static void udp_bind_netif_task(void *ctx) +static esp_err_t udp_bind_netif_task(void *ctx) { udp_bind_netif_task_t *task = (udp_bind_netif_task_t *)ctx; udp_bind_netif(task->pcb, netif_get_by_index(task->netif_index)); - xTaskNotifyGive(task->source_task); + return ESP_OK; } static uint8_t get_netif_index(otNetifIdentifier netif_identifier) @@ -276,34 +269,30 @@ static uint8_t get_netif_index(otNetifIdentifier netif_identifier) otError otPlatUdpBindToNetif(otUdpSocket *udp_socket, otNetifIdentifier netif_identifier) { - otError err = OT_ERROR_NONE; + esp_err_t err = ESP_OK; udp_bind_netif_task_t task = { - .source_task = xTaskGetCurrentTaskHandle(), .pcb = (struct udp_pcb *)udp_socket->mHandle, .netif_index = get_netif_index(netif_identifier), - .err = ESP_OK, }; - tcpip_callback(udp_bind_netif_task, &task); - wait_for_task_notification(); - if (task.err != ESP_OK) { - err = OT_ERROR_FAILED; - } - return err; + esp_openthread_task_switching_lock_release(); + err = esp_netif_tcpip_exec(udp_bind_netif_task, &task); + esp_openthread_task_switching_lock_acquire(portMAX_DELAY); + + return err == ESP_OK ? OT_ERROR_NONE : OT_ERROR_FAILED; } -static void udp_connect_task(void *ctx) +static esp_err_t udp_connect_task(void *ctx) { udp_bind_connect_task_t *task = (udp_bind_connect_task_t *)ctx; - - task->ret = udp_connect(task->pcb, &task->addr, task->port); - xTaskNotifyGive(task->source_task); + err_t ret = udp_connect(task->pcb, &task->addr, task->port); + return (ret == ERR_OK) ? ESP_OK : ESP_FAIL; } otError otPlatUdpConnect(otUdpSocket *udp_socket) { + esp_err_t err = ESP_OK; udp_bind_connect_task_t task = { - .source_task = xTaskGetCurrentTaskHandle(), .pcb = (struct udp_pcb *)udp_socket->mHandle, .port = udp_socket->mPeerName.mPort, }; @@ -312,10 +301,11 @@ otError otPlatUdpConnect(otUdpSocket *udp_socket) if (ip_addr_isany_val(task.addr) && task.port == 0) { return OT_ERROR_NONE; } - tcpip_callback(udp_connect_task, &task); - wait_for_task_notification(); + esp_openthread_task_switching_lock_release(); + err = esp_netif_tcpip_exec(udp_connect_task, &task); + esp_openthread_task_switching_lock_acquire(portMAX_DELAY); - return task.ret == ERR_OK ? OT_ERROR_NONE : OT_ERROR_FAILED; + return err == ESP_OK ? OT_ERROR_NONE : OT_ERROR_FAILED; } static bool is_link_local(const otIp6Address *address) @@ -419,7 +409,9 @@ otError otPlatUdpSend(otUdpSocket *udp_socket, otMessage *message, const otMessa // If the destination address is a openthread mesh local address, set the netif OT. task->netif_index = get_netif_index(OT_NETIF_THREAD); } + esp_openthread_task_switching_lock_release(); tcpip_callback(udp_send_task, task); + esp_openthread_task_switching_lock_acquire(portMAX_DELAY); exit: return error; @@ -458,7 +450,9 @@ otError otPlatUdpJoinMulticastGroup(otUdpSocket *socket, otNetifIdentifier netif task->netif_index = get_netif_index(netif_id); task->addr.zone = task->netif_index; memcpy(task->addr.addr, addr->mFields.m8, sizeof(task->addr.addr)); + esp_openthread_task_switching_lock_release(); tcpip_callback(udp_multicast_join_leave_task, task); + esp_openthread_task_switching_lock_acquire(portMAX_DELAY); exit: return error; @@ -475,7 +469,9 @@ otError otPlatUdpLeaveMulticastGroup(otUdpSocket *socket, otNetifIdentifier neti task->netif_index = get_netif_index(netif_id); task->addr.zone = task->netif_index; memcpy(task->addr.addr, addr->mFields.m8, sizeof(task->addr.addr)); + esp_openthread_task_switching_lock_release(); tcpip_callback(udp_multicast_join_leave_task, task); + esp_openthread_task_switching_lock_acquire(portMAX_DELAY); exit: return error; diff --git a/components/openthread/src/port/esp_spi_spinel_interface.cpp b/components/openthread/src/port/esp_spi_spinel_interface.cpp index b89eff6779..e19acd9152 100644 --- a/components/openthread/src/port/esp_spi_spinel_interface.cpp +++ b/components/openthread/src/port/esp_spi_spinel_interface.cpp @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2022-2024 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -266,8 +266,8 @@ otError SpiSpinelInterface::WaitForFrame(uint64_t timeout_us) otError SpiSpinelInterface::HardwareReset(void) { if (mRcpFailureHandler) { - mRcpFailureHandler(); ConductSPITransaction(true, 0, 0); // clear + mRcpFailureHandler(); } return OT_ERROR_NONE; } diff --git a/components/openthread/src/port/esp_uart_spinel_interface.cpp b/components/openthread/src/port/esp_uart_spinel_interface.cpp index 2cfa42d5e4..6f683fdee4 100644 --- a/components/openthread/src/port/esp_uart_spinel_interface.cpp +++ b/components/openthread/src/port/esp_uart_spinel_interface.cpp @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2021-2023 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2021-2024 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -295,8 +295,8 @@ esp_err_t UartSpinelInterface::TryRecoverUart(void) otError UartSpinelInterface::HardwareReset(void) { if (mRcpFailureHandler) { - mRcpFailureHandler(); TryRecoverUart(); + mRcpFailureHandler(); } return OT_ERROR_NONE; } diff --git a/components/openthread/src/spinel/esp_radio_spinel_uart_interface.cpp b/components/openthread/src/spinel/esp_radio_spinel_uart_interface.cpp index d07a67065b..5680650059 100644 --- a/components/openthread/src/spinel/esp_radio_spinel_uart_interface.cpp +++ b/components/openthread/src/spinel/esp_radio_spinel_uart_interface.cpp @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2021-2023 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2021-2024 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -276,8 +276,8 @@ esp_err_t UartSpinelInterface::TryRecoverUart(void) otError UartSpinelInterface::HardwareReset(void) { if (mRcpFailureHandler) { - mRcpFailureHandler(); TryRecoverUart(); + mRcpFailureHandler(); } return OT_ERROR_NONE; }