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 <maxim.uvarov@linaro.org>
Signed-off-by: Simon Goldschmidt <goldsimon@gmx.de>
This commit is contained in:
Maxim Uvarov 2023-09-15 16:42:33 +02:00 committed by Simon Goldschmidt
parent 36cb95c4d2
commit 4e6dd9c576
2 changed files with 33 additions and 1 deletions

View File

@ -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 */

View File

@ -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);