diff --git a/examples/mesh/ip_internal_network/main/mesh_main.c b/examples/mesh/ip_internal_network/main/mesh_main.c index 72e4a82..70088e7 100644 --- a/examples/mesh/ip_internal_network/main/mesh_main.c +++ b/examples/mesh/ip_internal_network/main/mesh_main.c @@ -16,6 +16,7 @@ #include "mesh_netif.h" #include "driver/gpio.h" #include "freertos/semphr.h" +#include /******************************************************* * Macros @@ -32,6 +33,7 @@ * Constants *******************************************************/ static const char *MESH_TAG = "mesh_main"; +static const char *TAG = "ping"; static const uint8_t MESH_ID[6] = { 0x77, 0x77, 0x77, 0x77, 0x77, 0x76}; /******************************************************* @@ -380,6 +382,44 @@ void ip_event_handler(void *arg, esp_event_base_t event_base, esp_mesh_comm_mqtt_task_start(); } +static void test_on_ping_success(esp_ping_handle_t hdl, void *args) +{ + // optionally, get callback arguments + // const char* str = (const char*) args; + // printf("%s\r\n", str); // "foo" + uint8_t ttl; + uint16_t seqno; + uint32_t elapsed_time, recv_len; + ip_addr_t target_addr; + esp_ping_get_profile(hdl, ESP_PING_PROF_SEQNO, &seqno, sizeof(seqno)); + esp_ping_get_profile(hdl, ESP_PING_PROF_TTL, &ttl, sizeof(ttl)); + esp_ping_get_profile(hdl, ESP_PING_PROF_IPADDR, &target_addr, sizeof(target_addr)); + esp_ping_get_profile(hdl, ESP_PING_PROF_SIZE, &recv_len, sizeof(recv_len)); + esp_ping_get_profile(hdl, ESP_PING_PROF_TIMEGAP, &elapsed_time, sizeof(elapsed_time)); + ESP_LOGW(TAG, "%d bytes from %s icmp_seq=%d ttl=%d time=%d ms", + recv_len, ipaddr_ntoa(&target_addr), seqno, ttl, elapsed_time); +} + +static void test_on_ping_timeout(esp_ping_handle_t hdl, void *args) +{ + uint16_t seqno; + ip_addr_t target_addr; + esp_ping_get_profile(hdl, ESP_PING_PROF_SEQNO, &seqno, sizeof(seqno)); + esp_ping_get_profile(hdl, ESP_PING_PROF_IPADDR, &target_addr, sizeof(target_addr)); + ESP_LOGW(TAG, "From %s icmp_seq=%d timeout", ipaddr_ntoa(&target_addr), seqno); +} + +static void test_on_ping_end(esp_ping_handle_t hdl, void *args) +{ + uint32_t transmitted; + uint32_t received; + uint32_t total_time_ms; + + esp_ping_get_profile(hdl, ESP_PING_PROF_REQUEST, &transmitted, sizeof(transmitted)); + esp_ping_get_profile(hdl, ESP_PING_PROF_REPLY, &received, sizeof(received)); + esp_ping_get_profile(hdl, ESP_PING_PROF_DURATION, &total_time_ms, sizeof(total_time_ms)); + ESP_LOGW(TAG, "%d packets transmitted, %d received, time %dms", transmitted, received, total_time_ms); +} void app_main(void) { @@ -424,4 +464,31 @@ void app_main(void) ESP_ERROR_CHECK(esp_mesh_start()); ESP_LOGI(MESH_TAG, "mesh starts successfully, heap:%d, %s\n", esp_get_free_heap_size(), esp_mesh_is_root_fixed() ? "root fixed" : "root not fixed"); + + + esp_ping_config_t ping_config = ESP_PING_DEFAULT_CONFIG(); + IP_ADDR4(&ping_config.target_addr, 8,8,8,8); // target IP address + ping_config.count = ESP_PING_COUNT_INFINITE; // ping in infinite mode, esp_ping_stop can stop it + ping_config.timeout_ms = 3000; + + /* set callback functions */ + esp_ping_callbacks_t cbs; + cbs.on_ping_success = test_on_ping_success; + cbs.on_ping_timeout = test_on_ping_timeout; + cbs.on_ping_end = test_on_ping_end; + cbs.cb_args = "foo"; // arguments that will feed to all callback functions, can be NULL + + esp_ping_handle_t ping; + esp_ping_new_session(&ping_config, &cbs, &ping); + esp_ping_start(ping); + +#define DUMP_INTERVAL_MS (1000*60*2) + int64_t last_dump = 0; + while (true) { + if((xTaskGetTickCount() - last_dump) >= DUMP_INTERVAL_MS) { + last_dump = xTaskGetTickCount(); + esp_wifi_statis_dump(0xFFFF); + } + vTaskDelay(1000); + } }