From d1f829972652474a230a76b9889fbfbf8dfe3457 Mon Sep 17 00:00:00 2001 From: zwx Date: Thu, 14 Dec 2023 16:45:38 +0800 Subject: [PATCH] fix(openthread): add netif check when call udp api for ot --- .../openthread/src/port/esp_openthread_udp.c | 38 +++++++++++++------ 1 file changed, 27 insertions(+), 11 deletions(-) diff --git a/components/openthread/src/port/esp_openthread_udp.c b/components/openthread/src/port/esp_openthread_udp.c index 08c8e12e6c..3598c845f7 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-2022 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2021-2023 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -57,6 +57,7 @@ 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 { @@ -255,8 +256,13 @@ otError otPlatUdpBind(otUdpSocket *udp_socket) static void 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)); + struct netif* target = netif_get_by_index(task->netif_index); + if (target == NULL) { + task->err = ESP_FAIL; + ESP_LOGE(OT_PLAT_LOG_TAG, "Failed to bind udp on index%d netif", task->netif_index); + } else { + udp_bind_netif(task->pcb, target); + } xTaskNotifyGive(task->source_task); } @@ -276,16 +282,20 @@ static uint8_t get_netif_index(otNetifIdentifier netif_identifier) otError otPlatUdpBindToNetif(otUdpSocket *udp_socket, otNetifIdentifier netif_identifier) { + otError err = OT_ERROR_NONE; 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(); - - return OT_ERROR_NONE; + if (task.err != ESP_OK) { + err = OT_ERROR_FAILED; + } + return err; } static void udp_connect_task(void *ctx) @@ -424,14 +434,20 @@ exit: static void udp_multicast_join_leave_task(void *ctx) { udp_multicast_join_leave_task_t *task = (udp_multicast_join_leave_task_t *)ctx; + struct netif *target = netif_get_by_index(task->netif_index); - if (task->is_join) { - if (mld6_joingroup_netif(netif_get_by_index(task->netif_index), &task->addr) != ERR_OK) { - ESP_LOGE(OT_PLAT_LOG_TAG, "Failed to join multicast group"); - } + if (target == NULL) { + ESP_LOGE(OT_PLAT_LOG_TAG, "Failed to %s multicast group, index%d netif is not ready", + task->is_join ? "join" : "leave", task->netif_index); } else { - if (mld6_leavegroup_netif(netif_get_by_index(task->netif_index), &task->addr) != ERR_OK) { - ESP_LOGE(OT_PLAT_LOG_TAG, "Failed to leave multicast group"); + if (task->is_join) { + if (mld6_joingroup_netif(target, &task->addr) != ERR_OK) { + ESP_LOGE(OT_PLAT_LOG_TAG, "Failed to join multicast group"); + } + } else { + if (mld6_leavegroup_netif(target, &task->addr) != ERR_OK) { + ESP_LOGE(OT_PLAT_LOG_TAG, "Failed to leave multicast group"); + } } } free(task);