PUD: determine the best gateway on a regular schedule
authorFerry Huberts <ferry.huberts@pelagic.nl>
Tue, 6 Mar 2012 14:25:54 +0000 (15:25 +0100)
committerFerry Huberts <ferry.huberts@pelagic.nl>
Wed, 14 Mar 2012 13:28:43 +0000 (14:28 +0100)
Signed-off-by: Ferry Huberts <ferry.huberts@pelagic.nl>
lib/pud/src/receiver.c
lib/pud/src/timers.c
lib/pud/src/timers.h

index f9a66a6..baf3a36 100644 (file)
@@ -347,6 +347,18 @@ static void pud_uplink_timer_callback(void *context __attribute__ ((unused))) {
        txToAllOlsrInterfaces(UPLINK);
 }
 
+/**
+ The gateway timer callback
+
+ @param context
+ unused
+ */
+static void pud_gateway_timer_callback(void *context __attribute__ ((unused))) {
+       (void) pthread_mutex_lock(&transmitGpsInformation.mutex);
+       getBestUplinkGateway(&transmitGpsInformation.bestGateway);
+       (void) pthread_mutex_unlock(&transmitGpsInformation.mutex);
+}
+
 /**
  Detemine whether we are moving from the gateway.
 
@@ -718,8 +730,6 @@ bool receiverUpdateGpsInformation(unsigned char * rxBuffer, size_t rxCount) {
        }
 
        (void) pthread_mutex_lock(&transmitGpsInformation.mutex);
-       getBestUplinkGateway(&transmitGpsInformation.bestGateway);
-
        txPosition = transmitGpsInformation.txPosition;
        txGateway = transmitGpsInformation.txGateway;
        bestGateway = transmitGpsInformation.bestGateway;
@@ -946,8 +956,18 @@ bool startReceiver(void) {
                return false;
        }
 
+       if (!initGatewayTimer()) {
+               stopReceiver();
+               return false;
+       }
+
        restartOlsrTimer();
        restartUplinkTimer();
+       if (!restartGatewayTimer(getGatewayDeterminationInterval(), &pud_gateway_timer_callback)) {
+               pudError(0, "Could not start gateway timer");
+               stopReceiver();
+               return false;
+       }
 
        return true;
 }
@@ -956,6 +976,7 @@ bool startReceiver(void) {
  Stop the receiver
  */
 void stopReceiver(void) {
+       destroyGatewayTimer();
        destroyUplinkTxTimer();
        destroyOlsrTxTimer();
 
index 4fad585..c552c12 100644 (file)
@@ -192,3 +192,94 @@ void destroyUplinkTxTimer(void) {
                pud_uplink_tx_timer_cookie = NULL;
        }
 }
+
+/*
+ * Best Gateway Timer
+ */
+
+/** The timer cookie, used to trace back the originator in debug */
+static struct olsr_cookie_info *pud_gateway_timer_cookie = NULL;
+
+/** The timer */
+static struct timer_entry * pud_gateway_timer = NULL;
+
+/**
+ Start the gateway 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 startGatewayTimer(unsigned long long interval, timer_cb_func cb_func) {
+       if (pud_gateway_timer == NULL) {
+               pud_gateway_timer = olsr_start_timer(interval * MSEC_PER_SEC, 0,
+                               OLSR_TIMER_PERIODIC, cb_func, NULL,
+                               pud_gateway_timer_cookie);
+               if (pud_gateway_timer == NULL) {
+                       return false;
+               }
+       }
+
+       return true;
+}
+
+/**
+ Stop the gateway timer
+ */
+static void stopGatewayTimer(void) {
+       if (pud_gateway_timer != NULL) {
+               olsr_stop_timer(pud_gateway_timer);
+               pud_gateway_timer = NULL;
+       }
+}
+
+/**
+ Restart the gateway 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 restartGatewayTimer(unsigned long long interval, timer_cb_func cb_func) {
+       stopGatewayTimer();
+       return startGatewayTimer(interval, cb_func);
+}
+
+/**
+ Initialise the gateway timer.
+
+ @return
+ - false on failure
+ - true otherwise
+ */
+int initGatewayTimer(void) {
+       if (pud_gateway_timer_cookie == NULL) {
+               pud_gateway_timer_cookie = olsr_alloc_cookie(
+                               PUD_PLUGIN_ABBR ": gateway timer", OLSR_COOKIE_TYPE_TIMER);
+               if (pud_gateway_timer_cookie == NULL) {
+                       return false;
+               }
+       }
+       return true;
+}
+
+/**
+ Destroy the gateway timer.
+ */
+void destroyGatewayTimer(void) {
+       stopGatewayTimer();
+       if (pud_gateway_timer_cookie != NULL) {
+               olsr_free_cookie(pud_gateway_timer_cookie);
+               pud_gateway_timer_cookie = NULL;
+       }
+}
index cb14ce9..bfc2f2a 100644 (file)
@@ -16,4 +16,8 @@ int initUplinkTxTimer(void);
 void destroyUplinkTxTimer(void);
 int restartUplinkTxTimer(unsigned long long interval, timer_cb_func cb_func);
 
+int initGatewayTimer(void);
+void destroyGatewayTimer(void);
+int restartGatewayTimer(unsigned long long interval, timer_cb_func cb_func);
+
 #endif /* _PUD_TIMERS_H_ */