PUD: move OLSR tx timer into timers.{c,h}
authorFerry Huberts <f.huberts@mindef.nl>
Thu, 14 Jul 2011 13:03:00 +0000 (15:03 +0200)
committerFerry Huberts <f.huberts@mindef.nl>
Tue, 19 Jul 2011 11:08:31 +0000 (13:08 +0200)
Signed-off-by: Ferry Huberts <f.huberts@mindef.nl>
lib/pud/src/receiver.c
lib/pud/src/timers.c [new file with mode: 0644]
lib/pud/src/timers.h [new file with mode: 0644]

index 377445e..35c6f80 100644 (file)
@@ -5,17 +5,16 @@
 #include "gpsConversion.h"
 #include "configuration.h"
 #include "dump.h"
+#include "timers.h"
 #include "nmeaTools.h"
 #include "posAvg.h"
 #include "networkInterfaces.h"
 
 /* OLSRD includes */
-#include "olsr_protocol.h"
-#include "interfaces.h"
 #include "net_olsr.h"
-#include "olsr.h"
 
 /* System includes */
+#include <stddef.h>
 #include <nmea/parser.h>
 #include <pthread.h>
 #include <nmea/info.h>
@@ -25,9 +24,6 @@
 #include <sys/timeb.h>
 #include <math.h>
 
-/* Forward declaration */
-static int restartOlsrTxTimer(unsigned long long interval);
-
 /*
  * NMEA parser
  */
@@ -237,6 +233,20 @@ static void txToAllOlsrInterfaces(void) {
        }
 }
 
+/*
+ * Timer Callbacks
+ */
+
+/**
+ The OLSR tx timer callback
+
+ @param context
+ unused
+ */
+static void pud_olsr_tx_timer_callback(void *context __attribute__ ((unused))) {
+       txToAllOlsrInterfaces();
+}
+
 /**
  Detemine whether we are moving by comparing fields from the average
  position against those of the last transmitted position.
@@ -525,7 +535,7 @@ static void detemineMoving(PositionUpdateEntry * avg,
 
 /**
  Update the latest GPS information. This function is called when a packet is
- receiver from a rxNonOlsr interface, containing one or more NMEA strings with
+ received from a rxNonOlsr interface, containing one or more NMEA strings with
  GPS information.
 
  @param rxBuffer
@@ -711,10 +721,9 @@ bool receiverUpdateGpsInformation(unsigned char * rxBuffer, size_t rxCount) {
        if (updateTransmitGpsInformation) {
                if (!restartOlsrTxTimer(
                                (state.externalState == STATIONARY) ? getUpdateIntervalStationary()
-                               : getUpdateIntervalMoving())) {
-                       pudError(0, "Could not restart receiver timer, no position"
-                                       " updates will be sent to the OLSR network");
-                       goto end;
+                               : getUpdateIntervalMoving(), &pud_olsr_tx_timer_callback)) {
+                       pudError(0, "Could not restart OLSR tx timer, no periodic"
+                                       " position updates will be sent to the OLSR network");
                }
 
                /* do an immediate transmit */
@@ -727,75 +736,6 @@ bool receiverUpdateGpsInformation(unsigned char * rxBuffer, size_t rxCount) {
        return retval;
 }
 
-/*
- * OLSR Tx Timer
- */
-
-/** The timer cookie, used to trace back the originator in debug */
-static struct olsr_cookie_info *pud_olsr_tx_timer_cookie = NULL;
-
-/** The timer */
-static struct timer_entry * pud_olsr_tx_timer = NULL;
-
-/**
- The timer callback
-
- @param context
- unused
- */
-static void pud_olsr_tx_timer_callback(void *context __attribute__ ((unused))) {
-       txToAllOlsrInterfaces();
-}
-
-/**
- Start the receiver timer. Does nothing when the timer is already running.
-
- @param interval
- The interval in seconds
-
- @return
- - false on failure
- - true otherwise
- */
-static int startOlsrTxTimer(unsigned long long interval) {
-       if (pud_olsr_tx_timer == NULL) {
-               pud_olsr_tx_timer = olsr_start_timer(interval * MSEC_PER_SEC, 0,
-                               OLSR_TIMER_PERIODIC, &pud_olsr_tx_timer_callback, NULL,
-                               pud_olsr_tx_timer_cookie);
-               if (pud_olsr_tx_timer == NULL) {
-                       stopReceiver();
-                       return false;
-               }
-       }
-
-       return true;
-}
-
-/**
- Stop the receiver timer
- */
-static void stopOlsrTxTimer(void) {
-       if (pud_olsr_tx_timer != NULL) {
-               olsr_stop_timer(pud_olsr_tx_timer);
-               pud_olsr_tx_timer = NULL;
-       }
-}
-
-/**
- Restart the receiver timer
-
- @param interval
- The interval in seconds
-
- @return
- - false on failure
- - true otherwise
- */
-static int restartOlsrTxTimer(unsigned long long interval) {
-       stopOlsrTxTimer();
-       return startOlsrTxTimer(interval);
-}
-
 /*
  * Receiver start/stop
  */
@@ -835,13 +775,9 @@ bool startReceiver(void) {
 
        initPositionAverageList(&positionAverageList, getAverageDepth());
 
-       if (pud_olsr_tx_timer_cookie == NULL) {
-               pud_olsr_tx_timer_cookie = olsr_alloc_cookie(
-                               PUD_PLUGIN_ABBR ": receiver", OLSR_COOKIE_TYPE_TIMER);
-               if (pud_olsr_tx_timer_cookie == NULL) {
-                       stopReceiver();
-                       return false;
-               }
+       if (!initOlsrTxTimer()) {
+               stopReceiver();
+               return false;
        }
 
        return true;
@@ -851,12 +787,7 @@ bool startReceiver(void) {
  Stop the receiver
  */
 void stopReceiver(void) {
-       stopOlsrTxTimer();
-
-       if (pud_olsr_tx_timer_cookie != NULL) {
-               olsr_free_cookie(pud_olsr_tx_timer_cookie);
-               pud_olsr_tx_timer_cookie = NULL;
-       }
+       destroyOlsrTxTimer();
 
        destroyPositionAverageList(&positionAverageList);
 
diff --git a/lib/pud/src/timers.c b/lib/pud/src/timers.c
new file mode 100644 (file)
index 0000000..e30949c
--- /dev/null
@@ -0,0 +1,102 @@
+#include "timers.h"
+
+/* Plugin includes */
+#include "pud.h"
+
+/* OLSRD includes */
+#include "olsr_cookie.h"
+
+/* System includes */
+#include <stddef.h>
+#include <stdbool.h>
+
+/*
+ * OLSR Tx Timer
+ */
+
+/** The timer cookie, used to trace back the originator in debug */
+static struct olsr_cookie_info *pud_olsr_tx_timer_cookie = NULL;
+
+/** The timer */
+static struct timer_entry * pud_olsr_tx_timer = NULL;
+
+/**
+ Start the OLSR tx timer. Does nothing when the timer is already running.
+
+ @param interval
+ The interval in seconds
+ @param cb_func
+ The callback function to call when the timer expires
+
+ @return
+ - false on failure
+ - true otherwise
+ */
+static int startOlsrTxTimer(unsigned long long interval, timer_cb_func cb_func) {
+       if (pud_olsr_tx_timer == NULL) {
+               pud_olsr_tx_timer = olsr_start_timer(interval * MSEC_PER_SEC, 0,
+                               OLSR_TIMER_PERIODIC, cb_func, NULL,
+                               pud_olsr_tx_timer_cookie);
+               if (pud_olsr_tx_timer == NULL) {
+                       return false;
+               }
+       }
+
+       return true;
+}
+
+/**
+ Stop the OLSR tx timer
+ */
+static void stopOlsrTxTimer(void) {
+       if (pud_olsr_tx_timer != NULL) {
+               olsr_stop_timer(pud_olsr_tx_timer);
+               pud_olsr_tx_timer = NULL;
+       }
+}
+
+/**
+ Restart the OLSR tx timer
+
+ @param interval
+ The interval in seconds
+ @param cb_func
+ The callback function to call when the timer expires
+
+ @return
+ - false on failure
+ - true otherwise
+ */
+int restartOlsrTxTimer(unsigned long long interval, timer_cb_func cb_func) {
+       stopOlsrTxTimer();
+       return startOlsrTxTimer(interval, cb_func);
+}
+
+/**
+ Initialise the OLSR tx timer.
+
+ @return
+ - false on failure
+ - true otherwise
+ */
+int initOlsrTxTimer(void) {
+       if (pud_olsr_tx_timer_cookie == NULL) {
+               pud_olsr_tx_timer_cookie = olsr_alloc_cookie(
+                               PUD_PLUGIN_ABBR ": OLSR tx timer", OLSR_COOKIE_TYPE_TIMER);
+               if (pud_olsr_tx_timer_cookie == NULL) {
+                       return false;
+               }
+       }
+       return true;
+}
+
+/**
+ Destroy the OLSR tx timer.
+ */
+void destroyOlsrTxTimer(void) {
+       stopOlsrTxTimer();
+       if (pud_olsr_tx_timer_cookie != NULL) {
+               olsr_free_cookie(pud_olsr_tx_timer_cookie);
+               pud_olsr_tx_timer_cookie = NULL;
+       }
+}
diff --git a/lib/pud/src/timers.h b/lib/pud/src/timers.h
new file mode 100644 (file)
index 0000000..6fd6cf2
--- /dev/null
@@ -0,0 +1,15 @@
+#ifndef _PUD_TIMERS_H_
+#define _PUD_TIMERS_H_
+
+/* Plugin includes */
+
+/* OLSRD includes */
+#include "scheduler.h"
+
+/* System includes */
+
+int initOlsrTxTimer(void);
+void destroyOlsrTxTimer(void);
+int restartOlsrTxTimer(unsigned long long interval, timer_cb_func cb_func);
+
+#endif /* _PUD_TIMERS_H_ */