Merge branch 'fix/lwip_ping_getnetif_threadsafe_v5.1' into 'release/v5.1'

fix(lwip): Fix ping session calling thread unsafe API (v5.1)

See merge request espressif/esp-idf!36382
This commit is contained in:
Jiang Jiang Jian
2025-02-21 11:00:52 +08:00
2 changed files with 36 additions and 6 deletions

View File

@@ -1,5 +1,5 @@
/* /*
* SPDX-FileCopyrightText: 2019-2024 Espressif Systems (Shanghai) CO LTD * SPDX-FileCopyrightText: 2019-2025 Espressif Systems (Shanghai) CO LTD
* *
* SPDX-License-Identifier: Apache-2.0 * SPDX-License-Identifier: Apache-2.0
*/ */
@@ -7,6 +7,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <stdbool.h> #include <stdbool.h>
#include <sys/time.h> #include <sys/time.h>
#include <net/if.h>
#include "freertos/FreeRTOS.h" #include "freertos/FreeRTOS.h"
#include "freertos/task.h" #include "freertos/task.h"
#include "lwip/opt.h" #include "lwip/opt.h"
@@ -25,6 +26,33 @@
#include "ping/ping_sock.h" #include "ping/ping_sock.h"
#include "esp_check.h" #include "esp_check.h"
#ifndef CONFIG_LWIP_NETIF_API
// If POSIX NETIF_API not enabled, we need to supply the implementation of if_indextoname()
// using tcpip_api_call()
#include "lwip/priv/tcpip_priv.h"
struct tcpip_netif_name {
struct tcpip_api_call_data call;
u8_t ifindex;
char *ifname;
};
static err_t do_netif_index_to_name(struct tcpip_api_call_data *msg)
{
struct tcpip_netif_name *params = __containerof(msg, struct tcpip_netif_name, call);
return netif_index_to_name(params->ifindex, params->ifname) ? ERR_OK : ERR_IF;
}
char *if_indextoname(unsigned int ifindex, char *ifname)
{
struct tcpip_netif_name params = { .ifindex = ifindex, .ifname = ifname };
if (tcpip_api_call(do_netif_index_to_name, &params.call) != ERR_OK) {
return NULL;
}
return ifname;
}
#endif // CONFIG_LWIP_NETIF_API == 0
const static char *TAG = "ping_sock"; const static char *TAG = "ping_sock";
#define PING_TIME_DIFF_MS(_end, _start) ((uint32_t)(((_end).tv_sec - (_start).tv_sec) * 1000 + \ #define PING_TIME_DIFF_MS(_end, _start) ((uint32_t)(((_end).tv_sec - (_start).tv_sec) * 1000 + \
@@ -266,8 +294,8 @@ esp_err_t esp_ping_new_session(const esp_ping_config_t *config, const esp_ping_c
/* set if index */ /* set if index */
if(config->interface) { if(config->interface) {
struct ifreq iface; struct ifreq iface;
if(netif_index_to_name(config->interface, iface.ifr_name) == NULL) { if (if_indextoname(config->interface, iface.ifr_name) == NULL) {
ESP_LOGE(TAG, "fail to find interface name with netif index %d", config->interface); ESP_LOGE(TAG, "fail to find interface name with netif index %" PRIu32, config->interface);
goto err; goto err;
} }
if(setsockopt(ep->sock, SOL_SOCKET, SO_BINDTODEVICE, &iface, sizeof(iface)) != 0) { if(setsockopt(ep->sock, SOL_SOCKET, SO_BINDTODEVICE, &iface, sizeof(iface)) != 0) {

View File

@@ -19,6 +19,9 @@
#include "argtable3/argtable3.h" #include "argtable3/argtable3.h"
#include "protocol_examples_common.h" #include "protocol_examples_common.h"
#include "ping/ping_sock.h" #include "ping/ping_sock.h"
#include "esp_check.h"
const static char *TAG = "echo_example";
static void cmd_ping_on_ping_success(esp_ping_handle_t hdl, void *args) static void cmd_ping_on_ping_success(esp_ping_handle_t hdl, void *args)
{ {
@@ -148,9 +151,8 @@ static int do_ping_cmd(int argc, char **argv)
.on_ping_end = cmd_ping_on_ping_end .on_ping_end = cmd_ping_on_ping_end
}; };
esp_ping_handle_t ping; esp_ping_handle_t ping;
esp_ping_new_session(&config, &cbs, &ping); ESP_RETURN_ON_FALSE(esp_ping_new_session(&config, &cbs, &ping) == ESP_OK, -1, TAG, "esp_ping_new_session failed");
esp_ping_start(ping); ESP_RETURN_ON_FALSE(esp_ping_start(ping) == ESP_OK, -1, TAG, "esp_ping_start() failed");
return 0; return 0;
} }