Skip to content

Commit

Permalink
feat(console): Added ping command to console component
Browse files Browse the repository at this point in the history
  • Loading branch information
espressif-abhikroy committed Oct 23, 2023
1 parent 5c324ab commit 44f6771
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
17 changes: 13 additions & 4 deletions components/console_cmd_ping/console_ping.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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;
}
Expand All @@ -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", "<t>", "Time to wait for a response, in seconds");
ping_args.interval = arg_dbl0("i", "interval", "<t>", "Wait interval seconds between sending each packet");
Expand Down
10 changes: 10 additions & 0 deletions components/console_cmd_ping/console_ping.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -13,3 +19,7 @@
* - esp_err_t
*/
esp_err_t console_cmd_ping_register(void);

#ifdef __cplusplus
}
#endif

0 comments on commit 44f6771

Please sign in to comment.