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

Closes https://github.com/espressif/esp-idf/issues/14982
This commit is contained in:
David Cermak 2024-12-09 18:35:29 +01:00 committed by David Čermák
parent 76ea699a0a
commit 9d3c8f0532
2 changed files with 37 additions and 5 deletions

View File

@ -7,6 +7,7 @@
#include <stdlib.h>
#include <stdbool.h>
#include <sys/time.h>
#include <net/if.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "lwip/opt.h"
@ -25,6 +26,35 @@
#include "ping/ping_sock.h"
#include "esp_check.h"
#ifndef CONFIG_LWIP_NETIF_API
#include "lwip/priv/tcpip_priv.h"
// If POSIX NETIF_API not enabled, we need to supply the implementation of if_indextoname()
// using tcpip_callback()
struct tcpip_netif_name {
struct tcpip_api_call_data call;
u8_t ifindex;
char *ifname;
err_t err;
};
static void do_netif_index_to_name(void *ctx)
{
struct tcpip_netif_name *params = ctx;
params->err = 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_callback(do_netif_index_to_name, &params) != ERR_OK || params.err != ERR_OK) {
return NULL;
}
return ifname;
}
#endif // CONFIG_LWIP_NETIF_API == 0
const static char *TAG = "ping_sock";
#define PING_TIME_DIFF_MS(_end, _start) ((uint32_t)(((_end).tv_sec - (_start).tv_sec) * 1000 + \
@ -266,8 +296,8 @@ esp_err_t esp_ping_new_session(const esp_ping_config_t *config, const esp_ping_c
/* set if index */
if(config->interface) {
struct ifreq iface;
if(netif_index_to_name(config->interface, iface.ifr_name) == NULL) {
ESP_LOGE(TAG, "fail to find interface name with netif index %d", config->interface);
if (if_indextoname(config->interface, iface.ifr_name) == NULL) {
ESP_LOGE(TAG, "fail to find interface name with netif index %" PRIu32, config->interface);
goto err;
}
if(setsockopt(ep->sock, SOL_SOCKET, SO_BINDTODEVICE, &iface, sizeof(iface)) != 0) {

View File

@ -19,6 +19,9 @@
#include "argtable3/argtable3.h"
#include "protocol_examples_common.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)
{
@ -148,9 +151,8 @@ static int do_ping_cmd(int argc, char **argv)
.on_ping_end = cmd_ping_on_ping_end
};
esp_ping_handle_t ping;
esp_ping_new_session(&config, &cbs, &ping);
esp_ping_start(ping);
ESP_RETURN_ON_FALSE(esp_ping_new_session(&config, &cbs, &ping) == ESP_OK, -1, TAG, "esp_ping_new_session failed");
ESP_RETURN_ON_FALSE(esp_ping_start(ping) == ESP_OK, -1, TAG, "esp_ping_start() failed");
return 0;
}