diff --git a/components/console_cmd_ping/console_ping.c b/components/console_cmd_ping/console_ping.c index 1e3d54ac17a..f8053226254 100644 --- a/components/console_cmd_ping/console_ping.c +++ b/components/console_cmd_ping/console_ping.c @@ -17,6 +17,7 @@ static const char *TAG = "console_ping"; +SemaphoreHandle_t sync_semaphore; static void cmd_ping_on_ping_success(esp_ping_handle_t hdl, void *args) { @@ -68,8 +69,10 @@ static void cmd_ping_on_ping_end(esp_ping_handle_t hdl, void *args) printf("%" PRIu32 " packets transmitted, %" PRIu32 " received, %" PRIu32 "%% packet loss, time %" PRIu32 "ms\n", transmitted, received, loss, total_time_ms); // delete the ping sessions, so that we clean up all resources and can create a new ping session - // we don't have to call delete function in the callback, instead we can call delete function from other tasks - esp_ping_delete_session(hdl); + ESP_ERROR_CHECK(esp_ping_delete_session(hdl)); + + /* Give the semaphore as ping task is done */ + xSemaphoreGive(sync_semaphore); } static struct { @@ -152,8 +155,13 @@ 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_ERROR_CHECK(esp_ping_new_session(&config, &cbs, &ping)); + ESP_ERROR_CHECK(esp_ping_start(ping)); + + /* Wait till the ping task is done */ + if (xSemaphoreTake(sync_semaphore, portMAX_DELAY) != pdTRUE) { + ESP_LOGE(TAG, "Error in xSemaphoreTake\n"); + } return 0; } @@ -167,6 +175,7 @@ static int do_ping_cmd(int argc, char **argv) esp_err_t console_cmd_ping_register(void) { esp_err_t ret; + sync_semaphore = xSemaphoreCreateBinary(); ping_args.timeout = arg_dbl0("W", "timeout", "", "Time to wait for a response, in seconds"); ping_args.interval = arg_dbl0("i", "interval", "", "Wait interval seconds between sending each packet"); diff --git a/components/console_cmd_ping/console_ping.h b/components/console_cmd_ping/console_ping.h index 1c6df02c94d..58a2d92e9f8 100644 --- a/components/console_cmd_ping/console_ping.h +++ b/components/console_cmd_ping/console_ping.h @@ -3,8 +3,14 @@ * * SPDX-License-Identifier: Apache-2.0 */ + +#pragma once + #include "console_simple_init.h" +#ifdef __cplusplus +extern "C" { +#endif /** * @brief Registers the ping command. @@ -13,3 +19,7 @@ * - esp_err_t */ esp_err_t console_cmd_ping_register(void); + +#ifdef __cplusplus +} +#endif