Ethernet + IPv6 got addres but ping not work.

VBKesha
Posts: 3
Joined: Thu Sep 16, 2021 8:39 am

Ethernet + IPv6 got addres but ping not work.

Postby VBKesha » Thu Sep 16, 2021 9:02 am

Hello!
I try to start ESP32+Ethernet+IPv6. i use very easy code for this:

Code: Select all



#define E_ETH_PHY_ADDR 0
#define E_ETH_PHY_RST_GPIO -1
#define E_ETH_MDC_GPIO 23
#define E_ETH_MDIO_GPIO 18

static esp_eth_handle_t s_eth_handle = NULL;
static uint8_t s_eth_mac[6];
esp_netif_t *eth_netif;

static void eth_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data){
    uint32_t speed, duplex;
    esp_eth_handle_t eth_handle = *(esp_eth_handle_t *)event_data;

    switch (event_id) {
        case ETHERNET_EVENT_CONNECTED:
            printf("Ethernet Link Up\r\n");
            esp_eth_ioctl(eth_handle, ETH_CMD_G_SPEED, &speed);
            esp_eth_ioctl(eth_handle, ETH_CMD_G_DUPLEX_MODE, &duplex);
            printf("Link: %sMb\\s %s Duplex\r\n", ((speed == ETH_SPEED_100M) ? "100" : "10"), ((duplex == ETH_DUPLEX_HALF) ? "Half" : "Full"));
            s_eth_mac[0] = s_eth_mac[2] = s_eth_mac[3] = s_eth_mac[4] = s_eth_mac[5] = 0xAA;
            esp_eth_ioctl(eth_handle, ETH_CMD_G_MAC_ADDR, s_eth_mac);
            printf("MAC:%02X:%02X:%02X:%02X:%02X:%02X\r\n", s_eth_mac[0], s_eth_mac[1], s_eth_mac[2], s_eth_mac[3], s_eth_mac[4],s_eth_mac[5]);
            printf("Start DHCP: %d\r\n", esp_netif_dhcpc_start(eth_netif));
            esp_netif_create_ip6_linklocal(eth_netif);
	break;

        case ETHERNET_EVENT_DISCONNECTED:
            esp_netif_dhcpc_stop(eth_netif);
            printf("Link Down\r\n");
        break;

        case ETHERNET_EVENT_START:
            printf("Ethernet Start\r\n");
            break;
        case ETHERNET_EVENT_STOP:
            printf("Ethernet Stop\r\n");
        break;

        default:
        break;
    }
}


static void phyPowerUp(){
    gpio_config_t io_conf = {};
    io_conf.intr_type = GPIO_INTR_DISABLE;
    io_conf.mode = GPIO_MODE_OUTPUT;
    io_conf.pin_bit_mask = GPIO_SEL_12;
    io_conf.pull_down_en = 0;
    io_conf.pull_up_en = 0;
    gpio_config(&io_conf);

    gpio_set_level(12, 1);
}

esp_eth_phy_t *esp_eth_phy_new_lan87xx(const eth_phy_config_t *config);

void initEthernet(){
    phyPowerUp();
    ESP_ERROR_CHECK(esp_event_handler_register(ETH_EVENT, ESP_EVENT_ANY_ID, eth_event_handler, 0));
    eth_mac_config_t mac_config = ETH_MAC_DEFAULT_CONFIG();
    eth_phy_config_t phy_config = ETH_PHY_DEFAULT_CONFIG();
    phy_config.phy_addr =E_ETH_PHY_ADDR;
    phy_config.reset_gpio_num = E_ETH_PHY_RST_GPIO;
    esp_eth_phy_t *phy = esp_eth_phy_new_lan8720(&phy_config);


    mac_config.smi_mdc_gpio_num = E_ETH_MDC_GPIO;
    mac_config.smi_mdio_gpio_num = E_ETH_MDIO_GPIO;

    esp_eth_mac_t *mac = esp_eth_mac_new_esp32(&mac_config);
    esp_eth_config_t config = ETH_DEFAULT_CONFIG(mac, phy);

    printf("esp_eth_driver_install = %d\r\n", ESP_OK == esp_eth_driver_install(&config, &s_eth_handle)); // install driver
    
    esp_netif_config_t cfg = ESP_NETIF_DEFAULT_ETH();
    
    eth_netif = esp_netif_new(&cfg);

    esp_eth_set_default_handlers(eth_netif);
    printf("ETH NETIF_ATTACH: %d\r\n", esp_netif_attach(eth_netif, esp_eth_new_netif_glue(s_eth_handle)));
    printf("NETIF: %08X\r\n", (uint32_t)eth_netif);
    esp_eth_start(s_eth_handle);
}

static void ip_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data){
    ip_event_got_ip_t *event = (ip_event_got_ip_t *) event_data;
    ip_event_got_ip6_t *event1 = (ip_event_got_ip_t *) event_data;
    switch (event_id){
        case IP_EVENT_GOT_IP6:{
            esp_netif_ip6_info_t *ip_info = &event1->ip6_info;
            printf("Ethernet Got IPv6 Address\r\n");
            printf("ETH IPv6:"IPV6STR"\r\n", IPV62STR(event1->ip6_info.ip));
        }break;

        case IP_EVENT_ETH_GOT_IP:{
            esp_netif_ip_info_t *ip_info = &event->ip_info;
            printf("Ethernet Got IP Address\r\n");
            printf("ETH IP:"IPSTR"\r\n", IP2STR(&ip_info->ip));
            printf("ETH MASK:"IPSTR"\r\n", IP2STR(&ip_info->netmask));
            printf("ETH GW:"IPSTR"\r\n", IP2STR(&ip_info->gw));
            printf("IPV6 Enable: %d\r\n", esp_netif_create_ip6_linklocal(event->esp_netif));
        } break;
    }

}

void initIPStack(){
    printf("Start IP Stack\r\n");
    esp_netif_init();
    esp_event_handler_register(IP_EVENT, ESP_EVENT_ANY_ID, &ip_event_handler, NULL);
}

void app_main(void){
    ESP_ERROR_CHECK(esp_event_loop_create_default());
    initIPStack();
    initEthernet();
}
System start got IPv4 addres and IPv6. IPv4 work fine. But IPv6 not have trouble, system got valid address from router but not answer on ping. If i acirvate debug i see this message "ip6_input: packet not for us."
How i can fix this?

ESP_cermak
Posts: 69
Joined: Thu Nov 01, 2018 8:32 am

Re: Ethernet + IPv6 got addres but ping not work.

Postby ESP_cermak » Thu Sep 23, 2021 9:23 am

HI VBKesha,

Could you please try to update the `components/lwip/port/esp32/netif/ethernetif.c`, so that at the end of the `ethernet_low_level_init()` you'd enable MLD6 flag?

Code: Select all

    netif->flags |= NETIF_FLAG_MLD6;
Could you please check at once and let us know if it helped?

Thanks,
David

VBKesha
Posts: 3
Joined: Thu Sep 16, 2021 8:39 am

Re: Ethernet + IPv6 got addres but ping not work.

Postby VBKesha » Thu Sep 23, 2021 12:53 pm

ESP_cermak wrote:

Code: Select all

    netif->flags |= NETIF_FLAG_MLD6;
Thanks its worked. But can it issue in IDF or i can set this flag from user code without pacth components?

ESP_cermak
Posts: 69
Joined: Thu Nov 01, 2018 8:32 am

Re: Ethernet + IPv6 got addres but ping not work.

Postby ESP_cermak » Fri Sep 24, 2021 9:11 am

Thank you for testing this, just wanted to make sure it was the neighbour discovery problem.

Yes, you can update the netif flags in the app code:

Code: Select all

struct netif *lwip_netif = esp_netif_get_netif_impl(eth_netif);
netif_set_flags(lwip_netif, NETIF_FLAG_MLD6);
Before creating the link local address in:

Code: Select all

esp_netif_create_ip6_linklocal(eth_netif);
You'd have to add (some non-standard, but still public) includes:

Code: Select all

#include "esp_netif_net_stack.h"
#include "lwip/netif.h"


We will add a simpler API into the esp_netif to update these flags (will include this in the main config options). This flag is enabled by default for wifi netifs, but not used on ethernet interfaces, as the IPv6 still works (if packets are sent directly to that address) okay but is not discoverable by neighbours.

VBKesha
Posts: 3
Joined: Thu Sep 16, 2021 8:39 am

Re: Ethernet + IPv6 got addres but ping not work.

Postby VBKesha » Fri Sep 24, 2021 9:28 am

Thanks work fine!

pierpiero
Posts: 1
Joined: Tue Jun 06, 2023 8:46 am

Re: Ethernet + IPv6 got addres but ping not work.

Postby pierpiero » Mon Jun 19, 2023 10:57 am

Hello, I am using the netif eth spi component with the KSZ8851SNL chip from Microchip. My device is able to obtain an IPv6 address, but I am unable to ping either from a host PC or ping the host PC.

Specifically, the ping fails with the error code ERR_RTE because the local_ip used is always the IPv6 address 0000:0000:0000:0000:0000:0000:0000:0000.

MySetup:
ESPP: ESP32S3
ESP_IDF: v5.0.2
Ethernet Chip: KSZ8851SNL

this is my code
  1. /* Ethernet Basic Example
  2.  
  3.    This example code is in the Public Domain (or CC0 licensed, at your option.)
  4.  
  5.    Unless required by applicable law or agreed to in writing, this
  6.    software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  7.    CONDITIONS OF ANY KIND, either express or implied.
  8. */
  9. #include <stdio.h>
  10. #include <string.h>
  11. #include "freertos/FreeRTOS.h"
  12. #include "freertos/task.h"
  13. #include "esp_netif.h"
  14. #include "esp_eth.h"
  15. #include "esp_event.h"
  16. #include "esp_log.h"
  17. #include "driver/gpio.h"
  18. #include "sdkconfig.h"
  19. #include "esp_netif_net_stack.h"
  20. #include "esp_mac.h"
  21. #include "lwip/netif.h"
  22. #include "ping/ping_sock.h"
  23. #include "netdb.h"
  24.  
  25. #include "driver/spi_master.h"
  26.  
  27. #define USE_IPV6_ADDRESS
  28.  
  29. #define color_default "\x1B[0m"
  30. #define color_red "\x1B[1;31m"
  31. #define color_green "\x1B[1;32m"
  32. #define color_yellow "\x1B[1;33m"
  33. #define color_blue "\x1B[1;34m"
  34. #define color_magenta "\x1B[1;35m"
  35. #define color_cyan "\x1B[1;36m"
  36. #define color_white "\x1B[1;37m"
  37.  
  38. static void cmd_ping_on_ping_end(esp_ping_handle_t hdl, void *args);
  39. static void cmd_ping_on_ping_timeout(esp_ping_handle_t hdl, void *args);
  40. static void cmd_ping_on_ping_success(esp_ping_handle_t hdl, void *args);
  41. static int do_ping_cmd(void);
  42. static bool example_is_our_netif(const char *prefix, esp_netif_t *netif);
  43.  
  44. static const char *TAG = "eth_ipv6";
  45.  
  46. typedef struct
  47. {
  48.     uint8_t spi_cs_gpio;
  49.     uint8_t int_gpio;
  50.     int8_t phy_reset_gpio;
  51.     uint8_t phy_addr;
  52. } spi_eth_module_config_t;
  53.  
  54.  
  55. /** Event handler for Ethernet events */
  56. static void eth_event_handler(void *arg, esp_event_base_t event_base,
  57.                               int32_t event_id, void *event_data)
  58. {
  59.     uint8_t mac_addr[6] = {0};
  60.     /* we can get the ethernet driver handle from event data */
  61.     esp_eth_handle_t eth_handle = *(esp_eth_handle_t *)event_data;
  62.  
  63.     switch (event_id)
  64.     {
  65.     case ETHERNET_EVENT_CONNECTED:
  66.         ESP_LOGI(TAG, "Ethernet Link Up");
  67.         esp_eth_ioctl(eth_handle, ETH_CMD_G_MAC_ADDR, mac_addr);
  68.         ESP_LOGI(TAG, "Ethernet HW Addr %02x:%02x:%02x:%02x:%02x:%02x",
  69.                  mac_addr[0], mac_addr[1], mac_addr[2], mac_addr[3], mac_addr[4], mac_addr[5]);
  70.  
  71.         struct netif *lwip_netif = esp_netif_get_netif_impl(arg);
  72.         netif_set_flags(lwip_netif, NETIF_FLAG_MLD6);
  73.         ESP_ERROR_CHECK(esp_netif_create_ip6_linklocal(arg));
  74.         break;
  75.     case ETHERNET_EVENT_DISCONNECTED:
  76.         ESP_LOGI(TAG, "Ethernet Link Down");
  77.         break;
  78.     case ETHERNET_EVENT_START:
  79.         ESP_LOGI(TAG, "Ethernet Started");
  80.         break;
  81.     case ETHERNET_EVENT_STOP:
  82.         ESP_LOGI(TAG, "Ethernet Stopped");
  83.         break;
  84.     default:
  85.         break;
  86.     }
  87. }
  88.  
  89. /** Event handler for IP_EVENT_ETH_GOT_IP */
  90. static void got_ip_event_handler(void *arg, esp_event_base_t event_base,
  91.                                  int32_t event_id, void *event_data)
  92. {
  93.     ip_event_got_ip_t *event = (ip_event_got_ip_t *)event_data;
  94.     const esp_netif_ip_info_t *ip_info = &event->ip_info;
  95.  
  96.     ESP_LOGI(TAG, "Ethernet Got IP Address");
  97.     ESP_LOGI(TAG, "~~~~~~~~~~~");
  98.     ESP_LOGI(TAG, "ETHIP:" IPSTR, IP2STR(&ip_info->ip));
  99.     ESP_LOGI(TAG, "ETHMASK:" IPSTR, IP2STR(&ip_info->netmask));
  100.     ESP_LOGI(TAG, "ETHGW:" IPSTR, IP2STR(&ip_info->gw));
  101.     ESP_LOGI(TAG, "~~~~~~~~~~~");
  102. }
  103.  
  104. static void got_ip6_event_handler(void *arg, esp_event_base_t event_base,
  105.                                   int32_t event_id, void *event_data)
  106. {
  107.     ip_event_got_ip6_t *event = (ip_event_got_ip6_t *)event_data;
  108.  
  109.     if (!example_is_our_netif("eth0", event->esp_netif))
  110.     {
  111.         ESP_LOGE(TAG, "Is not my interface %s", esp_netif_get_desc(event->esp_netif));
  112.         return;
  113.     }
  114.  
  115.     const esp_netif_ip6_info_t *ip6_info = &event->ip6_info;
  116.  
  117.     printf("%08lX %08lX %08lX %08lX\n", ip6_info->ip.addr[0], ip6_info->ip.addr[1], ip6_info->ip.addr[2], ip6_info->ip.addr[3]);
  118.  
  119.     ESP_LOGI(TAG, "Ethernet Got IP6 Address");
  120.     ESP_LOGI(TAG, "~~~~~~~~~~~");
  121.     ESP_LOGI(TAG, "ETHIP6:" IPV6STR, IPV62STR(ip6_info->ip));
  122.     ESP_LOGI(TAG, "Zone: %d", ip6_info->ip.zone);
  123.     ESP_LOGI(TAG, "~~~~~~~~~~~");
  124.  
  125.     do_ping_cmd();
  126. }
  127.  
  128. static bool example_is_our_netif(const char *prefix, esp_netif_t *netif)
  129. {
  130.     return strncmp(prefix, esp_netif_get_desc(netif), strlen(prefix) - 1) == 0;
  131. }
  132.  
  133. void app_main(void)
  134. {
  135.     // Initialize TCP/IP network interface (should be called only once in application)
  136.     ESP_ERROR_CHECK(esp_netif_init());
  137.     // Create default event loop that running in background
  138.     ESP_ERROR_CHECK(esp_event_loop_create_default());
  139.  
  140.     esp_log_level_set(TAG, ESP_LOG_DEBUG);
  141.     esp_log_level_set("esp_netif_lwip", ESP_LOG_DEBUG);
  142.  
  143.     // Create instance(s) of esp-netif for SPI Ethernet(s)
  144.     esp_netif_inherent_config_t esp_netif_config = ESP_NETIF_INHERENT_DEFAULT_ETH();
  145.     esp_netif_config_t cfg_spi = {
  146.         .base = &esp_netif_config,
  147.         .stack = ESP_NETIF_NETSTACK_DEFAULT_ETH};
  148.     esp_netif_t *eth_netif_spi = NULL;
  149.  
  150.     strcat(strcpy(if_key_str, "ETH_SPI_0"), num_str);
  151.     strcat(strcpy(if_desc_str, "eth0"), num_str);
  152.     esp_netif_config.if_key = if_key_str;
  153.     esp_netif_config.if_desc = if_desc_str;
  154.     esp_netif_config.route_prio = 30;
  155.     eth_netif_spi[i] = esp_netif_new(&cfg_spi);
  156.  
  157.     // Init MAC and PHY configs to default
  158.     eth_mac_config_t mac_config_spi = ETH_MAC_DEFAULT_CONFIG();
  159.     eth_phy_config_t phy_config_spi = ETH_PHY_DEFAULT_CONFIG();
  160.  
  161.     // Install GPIO ISR handler to be able to service SPI Eth modlues interrupts
  162.     gpio_install_isr_service(0);
  163.  
  164.     // Init SPI bus
  165.     spi_bus_config_t buscfg = {
  166.         .miso_io_num = CONFIG_EXAMPLE_ETH_SPI_MISO_GPIO,
  167.         .mosi_io_num = CONFIG_EXAMPLE_ETH_SPI_MOSI_GPIO,
  168.         .sclk_io_num = CONFIG_EXAMPLE_ETH_SPI_SCLK_GPIO,
  169.         .quadwp_io_num = -1,
  170.         .quadhd_io_num = -1,
  171.     };
  172.     ESP_ERROR_CHECK(spi_bus_initialize(CONFIG_EXAMPLE_ETH_SPI_HOST, &buscfg, SPI_DMA_CH_AUTO));
  173.  
  174.     // Init specific SPI Ethernet module configuration from Kconfig (CS GPIO, Interrupt GPIO, etc.)
  175.     spi_eth_module_config_t spi_eth_module_config;
  176.     spi_eth_module_config.spi_cs_gpio = CONFIG_EXAMPLE_ETH_SPI_CS0_GPIO;
  177.     spi_eth_module_config.int_gpio = CONFIG_EXAMPLE_ETH_SPI_INT0_GPIO;
  178.     spi_eth_module_config.phy_reset_gpio = CONFIG_EXAMPLE_ETH_SPI_PHY_RST0_GPIO;
  179.     spi_eth_module_config.phy_addr = CONFIG_EXAMPLE_ETH_SPI_PHY_ADDR0;
  180.  
  181.     // Configure SPI interface and Ethernet driver for specific SPI module
  182.     esp_eth_mac_t *mac_spi;
  183.     esp_eth_phy_t *phy_spi;
  184.     esp_eth_handle_t eth_handle_spi = NULL;
  185.     spi_device_interface_config_t spi_devcfg = {
  186.         .mode = 0,
  187.         .clock_speed_hz = CONFIG_EXAMPLE_ETH_SPI_CLOCK_MHZ * 1000 * 1000,
  188.         .queue_size = 20};
  189.  
  190.     // Set SPI module Chip Select GPIO
  191.     spi_devcfg.spics_io_num = spi_eth_module_config.spi_cs_gpio;
  192.     // Set remaining GPIO numbers and configuration used by the SPI module
  193.     phy_config_spi.phy_addr = spi_eth_module_config.phy_addr;
  194.     phy_config_spi.reset_gpio_num = spi_eth_module_config.phy_reset_gpio;
  195.  
  196.     ESP_LOGI(TAG, "CONFIG_EXAMPLE_USE_KSZ8851SNL");
  197.     eth_ksz8851snl_config_t ksz8851snl_config = ETH_KSZ8851SNL_DEFAULT_CONFIG(CONFIG_EXAMPLE_ETH_SPI_HOST, &spi_devcfg);
  198.     ksz8851snl_config.int_gpio_num = spi_eth_module_config.int_gpio;
  199.     mac_spi = esp_eth_mac_new_ksz8851snl(&ksz8851snl_config, &mac_config_spi);
  200.     phy_spi = esp_eth_phy_new_ksz8851snl(&phy_config_spi);
  201.  
  202.     esp_eth_config_t eth_config_spi = ETH_DEFAULT_CONFIG(mac_spi, phy_spi);
  203.     ESP_ERROR_CHECK(esp_eth_driver_install(&eth_config_spi, &eth_handle_spi));
  204.  
  205.     uint8_t mac[6];
  206.     esp_read_mac(mac, ESP_MAC_ETH);
  207.     ESP_ERROR_CHECK(esp_eth_ioctl(eth_handle_spi, ETH_CMD_S_MAC_ADDR, mac));
  208.  
  209.     // attach Ethernet driver to TCP/IP stack
  210.     ESP_ERROR_CHECK(esp_netif_attach(eth_netif_spi, esp_eth_new_netif_glue(eth_handle_spi)));
  211.  
  212.     // Register user defined event handers
  213.     ESP_ERROR_CHECK(esp_event_handler_register(ETH_EVENT, ESP_EVENT_ANY_ID, &eth_event_handler, eth_netif_spi));
  214.  
  215.     // Register user defined event handers
  216.     ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_ETH_GOT_IP, got_ip_event_handler, NULL));
  217.     ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_GOT_IP6, got_ip6_event_handler, NULL));
  218.  
  219.     /* start Ethernet driver state machine */
  220.     ESP_ERROR_CHECK(esp_eth_start(eth_handle_spi));
  221. }
  222.  
  223. #define EXAMPLE_PING_IP_V6 "fd00::c0ed:8d79:672f:e9b1"
  224. #define EXAMPLE_PING_IP_V4 "192.168.81.68"
  225. // #define EXAMPLE_PING_IP "192.168.81.1"
  226. #define EXAMPLE_PING_INTERVAL 1
  227. #define EXAMPLE_PING_COUNT 5
  228.  
  229. static int do_ping_cmd(void)
  230. {
  231.     esp_ping_config_t config = ESP_PING_DEFAULT_CONFIG();
  232.     static esp_ping_handle_t ping;
  233.  
  234.     config.interval_ms = (uint32_t)(EXAMPLE_PING_INTERVAL * 1000);
  235.     config.count = (uint32_t)(EXAMPLE_PING_COUNT);
  236.  
  237.     // parse IP address
  238.     ip_addr_t target_addr;
  239.     struct addrinfo hint;
  240.     struct addrinfo *res = NULL;
  241.     memset(&hint, 0, sizeof(hint));
  242.     memset(&target_addr, 0, sizeof(target_addr));
  243.  
  244.     /* convert domain name to IP address */
  245.     if (getaddrinfo(EXAMPLE_PING_IP_V6, NULL, &hint, &res) != 0)
  246.     {
  247.         printf("ping: unknown host %s\n", EXAMPLE_PING_IP_V6);
  248.         return 1;
  249.     }
  250.     if (res->ai_family == AF_INET)
  251.     {
  252.         struct in_addr addr4 = ((struct sockaddr_in *)(res->ai_addr))->sin_addr;
  253.         inet_addr_to_ip4addr(ip_2_ip4(&target_addr), &addr4);
  254.         printf(color_magenta IPSTR "\n" color_default, IP2STR(&target_addr.u_addr.ip4));
  255.         printf(color_magenta "Type %d\n" color_default, target_addr.type);
  256.     }
  257.     else
  258.     {
  259.         struct in6_addr addr6 = ((struct sockaddr_in6 *)(res->ai_addr))->sin6_addr;
  260.         inet6_addr_to_ip6addr(ip_2_ip6(&target_addr), &addr6);
  261.         IP_SET_TYPE(&target_addr, IPADDR_TYPE_V6);
  262.         printf(color_magenta IPV6STR "\n" color_default, IPV62STR(target_addr.u_addr.ip6));
  263.         printf(color_magenta "Type %d\n" color_default, target_addr.type);
  264.     }
  265.     freeaddrinfo(res);
  266.  
  267.  
  268.     config.target_addr = target_addr;
  269.  
  270.     /* set callback functions */
  271.     esp_ping_callbacks_t cbs = {
  272.         .on_ping_success = cmd_ping_on_ping_success,
  273.         .on_ping_timeout = cmd_ping_on_ping_timeout,
  274.         .on_ping_end = cmd_ping_on_ping_end,
  275.         .cb_args = NULL};
  276.  
  277.     esp_ping_new_session(&config, &cbs, &ping);
  278.     esp_ping_start(ping);
  279.  
  280.     return 0;
  281. }
  282.  
  283. static void cmd_ping_on_ping_success(esp_ping_handle_t hdl, void *args)
  284. {
  285.     uint8_t ttl;
  286.     uint16_t seqno;
  287.     uint32_t elapsed_time, recv_len;
  288.     ip_addr_t target_addr;
  289.     esp_ping_get_profile(hdl, ESP_PING_PROF_SEQNO, &seqno, sizeof(seqno));
  290.     esp_ping_get_profile(hdl, ESP_PING_PROF_TTL, &ttl, sizeof(ttl));
  291.     esp_ping_get_profile(hdl, ESP_PING_PROF_IPADDR, &target_addr, sizeof(target_addr));
  292.     esp_ping_get_profile(hdl, ESP_PING_PROF_SIZE, &recv_len, sizeof(recv_len));
  293.     esp_ping_get_profile(hdl, ESP_PING_PROF_TIMEGAP, &elapsed_time, sizeof(elapsed_time));
  294.     printf("%ld bytes from %s icmp_seq=%d ttl=%d time=%ld ms\n",
  295.            recv_len, ipaddr_ntoa((ip_addr_t *)&target_addr), seqno, ttl, elapsed_time);
  296. }
  297.  
  298. static void cmd_ping_on_ping_timeout(esp_ping_handle_t hdl, void *args)
  299. {
  300.     uint16_t seqno;
  301.     ip_addr_t target_addr;
  302.     esp_ping_get_profile(hdl, ESP_PING_PROF_SEQNO, &seqno, sizeof(seqno));
  303.     esp_ping_get_profile(hdl, ESP_PING_PROF_IPADDR, &target_addr, sizeof(target_addr));
  304.     printf("From %s icmp_seq=%d timeout\n", ipaddr_ntoa((ip_addr_t *)&target_addr), seqno);
  305. }
  306.  
  307. static void cmd_ping_on_ping_end(esp_ping_handle_t hdl, void *args)
  308. {
  309.     ip_addr_t target_addr;
  310.     uint32_t transmitted;
  311.     uint32_t received;
  312.     uint32_t total_time_ms;
  313.     esp_ping_get_profile(hdl, ESP_PING_PROF_REQUEST, &transmitted, sizeof(transmitted));
  314.     esp_ping_get_profile(hdl, ESP_PING_PROF_REPLY, &received, sizeof(received));
  315.     esp_ping_get_profile(hdl, ESP_PING_PROF_IPADDR, &target_addr, sizeof(target_addr));
  316.     esp_ping_get_profile(hdl, ESP_PING_PROF_DURATION, &total_time_ms, sizeof(total_time_ms));
  317.     uint32_t loss = (uint32_t)((1 - ((float)received) / transmitted) * 100);
  318.     if (IP_IS_V4(&target_addr))
  319.     {
  320.         printf("\n--- %s ping statistics ---\n", inet_ntoa(*ip_2_ip4(&target_addr)));
  321.     }
  322.     else
  323.     {
  324.         printf("\n--- %s ping statistics ---\n", inet6_ntoa(*ip_2_ip6(&target_addr)));
  325.     }
  326.     printf("%ld packets transmitted, %ld received, %ld%% packet loss, time %ldms\n",
  327.            transmitted, received, loss, total_time_ms);
  328.     // delete the ping sessions, so that we clean up all resources and can create a new ping session
  329.     // we don't have to call delete function in the callback, instead we can call delete function from other tasks
  330.     esp_ping_delete_session(hdl);
  331. }

Who is online

Users browsing this forum: No registered users and 298 guests