Преглед изворни кода

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>
Maxim Uvarov пре 2 година
родитељ
комит
4e6dd9c576
2 измењених фајлова са 33 додато и 1 уклоњено
  1. 32 1
      contrib/apps/ping/ping.c
  2. 1 0
      contrib/apps/ping/ping.h

+ 32 - 1
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 */

+ 1 - 0
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);