From 4e6dd9c5765a11d3a81d7cb39d8c2cb48d15385b Mon Sep 17 00:00:00 2001 From: Maxim Uvarov Date: Fri, 15 Sep 2023 16:42:33 +0200 Subject: [PATCH] ping: add stop function ping in raw mode does some set up and sets timeout, but clean up procedure is missing. That is needed for case if PING_RESULT() macro is used for application exit. Also implement stop functionality when using sockets. Running ping is stopped when calling ping_init() again. Signed-off-by: Maxim Uvarov Signed-off-by: Simon Goldschmidt --- contrib/apps/ping/ping.c | 33 ++++++++++++++++++++++++++++++++- contrib/apps/ping/ping.h | 1 + 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/contrib/apps/ping/ping.c b/contrib/apps/ping/ping.c index 143fb83e8..bd0688f32 100644 --- a/contrib/apps/ping/ping.c +++ b/contrib/apps/ping/ping.c @@ -268,7 +268,7 @@ ping_thread(void *arg) LWIP_ASSERT("setting receive timeout failed", ret == 0); LWIP_UNUSED_ARG(ret); - while (1) { + while (ping_target != NULL) { if (ping_send(s, ping_target) == ERR_OK) { LWIP_DEBUGF( PING_DEBUG, ("ping: send ")); ip_addr_debug_print(PING_DEBUG, ping_target); @@ -285,6 +285,7 @@ ping_thread(void *arg) } sys_msleep(PING_DELAY); } + lwip_close(s); } #else /* PING_USE_SOCKETS */ @@ -376,16 +377,35 @@ void ping_send_now(void) { LWIP_ASSERT("ping_pcb != NULL", ping_pcb != NULL); + LWIP_ASSERT("ping_target != NULL", ping_target != NULL); ping_send(ping_pcb, ping_target); } +static void +ping_raw_stop(void) +{ + sys_untimeout(ping_timeout, ping_pcb); + if (ping_pcb != NULL) { + raw_remove(ping_pcb); + ping_pcb = NULL; + } +} + #endif /* PING_USE_SOCKETS */ +/** + * Initialize thread (socket mode) or timer (callback mode) to cyclically send pings + * to a target. + * Running ping is implicitly stopped. + */ void ping_init(const ip_addr_t* ping_addr) { + LWIP_ASSERT("ping_target != NULL", ping_target != NULL); ping_target = ping_addr; + ping_stop(); + #if PING_USE_SOCKETS sys_thread_new("ping_thread", ping_thread, NULL, DEFAULT_THREAD_STACKSIZE, DEFAULT_THREAD_PRIO); #else /* PING_USE_SOCKETS */ @@ -393,4 +413,15 @@ ping_init(const ip_addr_t* ping_addr) #endif /* PING_USE_SOCKETS */ } +/** + * Stop sending more pings. + */ +void ping_stop(void) +{ +#if !PING_USE_SOCKETS + ping_raw_stop(); +#endif /* !PING_USE_SOCKETS */ + ping_target = NULL; +} + #endif /* LWIP_RAW */ diff --git a/contrib/apps/ping/ping.h b/contrib/apps/ping/ping.h index 1f21c7b09..b6e06080e 100644 --- a/contrib/apps/ping/ping.h +++ b/contrib/apps/ping/ping.h @@ -11,6 +11,7 @@ #endif void ping_init(const ip_addr_t* ping_addr); +void ping_stop(void); #if !PING_USE_SOCKETS void ping_send_now(void);