From 50392b625625a3ba7675f8e6788fed4827609094 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Cenk=20G=C3=BCndo=C4=9Fan?= <cnkgndgn@gmail.com>
Date: Fri, 4 Sep 2015 13:19:19 +0200
Subject: [PATCH] rpl: switch to xtimer

---
 sys/include/net/gnrc/rpl/structs.h        | 18 ++++++-----
 sys/net/gnrc/routing/rpl/gnrc_rpl.c       | 37 ++++++++++-------------
 sys/net/gnrc/routing/rpl/gnrc_rpl_dodag.c | 25 ++++++++-------
 3 files changed, 38 insertions(+), 42 deletions(-)

diff --git a/sys/include/net/gnrc/rpl/structs.h b/sys/include/net/gnrc/rpl/structs.h
index 84d8ab0e1e..165832e536 100644
--- a/sys/include/net/gnrc/rpl/structs.h
+++ b/sys/include/net/gnrc/rpl/structs.h
@@ -28,7 +28,7 @@ extern "C" {
 #endif
 
 #include "net/ipv6/addr.h"
-#include "vtimer.h"
+#include "xtimer.h"
 #include "trickle.h"
 
 /**
@@ -170,13 +170,13 @@ typedef struct gnrc_rpl_parent gnrc_rpl_parent_t;
  * @brief Parent representation
  */
 struct gnrc_rpl_parent {
-    gnrc_rpl_parent_t *next;          /**< pointer to the next parent */
+    gnrc_rpl_parent_t *next;        /**< pointer to the next parent */
     uint8_t state;                  /**< 0 for unsued, 1 for used */
     ipv6_addr_t addr;               /**< link-local IPv6 address of this parent */
     uint16_t rank;                  /**< rank of the parent */
     uint8_t dtsn;                   /**< last seen dtsn of this parent */
-    gnrc_rpl_dodag_t *dodag;          /**< DODAG the parent belongs to */
-    timex_t lifetime;               /**< lifetime of this parent */
+    gnrc_rpl_dodag_t *dodag;        /**< DODAG the parent belongs to */
+    uint64_t lifetime;              /**< lifetime of this parent */
     double  link_metric;            /**< metric of the link */
     uint8_t link_metric_type;       /**< type of the metric */
 };
@@ -236,10 +236,12 @@ struct gnrc_rpl_dodag {
     uint8_t dao_counter;            /**< amount of retried DAOs */
     bool dao_ack_received;          /**< flag to check for DAO-ACK */
     uint8_t dodag_conf_counter;     /**< limitation of the sending of DODAG_CONF options */
-    timex_t dao_time;               /**< time to schedule the next DAO */
-    vtimer_t dao_timer;             /**< timer to schedule the next DAO */
-    timex_t cleanup_time;           /**< time to schedula a DODAG cleanup */
-    vtimer_t cleanup_timer;         /**< timer to schedula a DODAG cleanup */
+    msg_t dao_msg;                  /**< msg_t for firing a dao */
+    uint64_t dao_time;              /**< time to schedule the next DAO */
+    xtimer_t dao_timer;             /**< timer to schedule the next DAO */
+    msg_t cleanup_msg;              /**< msg_t for firing a cleanup */
+    uint32_t cleanup_time;          /**< time to schedula a DODAG cleanup */
+    xtimer_t cleanup_timer;         /**< timer to schedula a DODAG cleanup */
     trickle_t trickle;              /**< trickle representation */
 };
 
diff --git a/sys/net/gnrc/routing/rpl/gnrc_rpl.c b/sys/net/gnrc/routing/rpl/gnrc_rpl.c
index b0e979da8c..5e975d0424 100644
--- a/sys/net/gnrc/routing/rpl/gnrc_rpl.c
+++ b/sys/net/gnrc/routing/rpl/gnrc_rpl.c
@@ -30,8 +30,9 @@ static char addr_str[IPV6_ADDR_MAX_STR_LEN];
 
 static char _stack[GNRC_RPL_STACK_SIZE];
 kernel_pid_t gnrc_rpl_pid = KERNEL_PID_UNDEF;
-static timex_t _lt_time;
-static vtimer_t _lt_timer;
+static uint32_t _lt_time = GNRC_RPL_LIFETIME_UPDATE_STEP * SEC_IN_USEC;
+static xtimer_t _lt_timer;
+static msg_t _lt_msg = { .type = GNRC_RPL_MSG_TYPE_LIFETIME_UPDATE };
 static msg_t _msg_q[GNRC_RPL_MSG_QUEUE_SIZE];
 static gnrc_netreg_entry_t _me_reg;
 
@@ -63,8 +64,7 @@ kernel_pid_t gnrc_rpl_init(kernel_pid_t if_pid)
         gnrc_netreg_register(GNRC_NETTYPE_ICMPV6, &_me_reg);
 
         gnrc_rpl_of_manager_init();
-        _lt_time = timex_set(GNRC_RPL_LIFETIME_UPDATE_STEP, 0);
-        vtimer_set_msg(&_lt_timer, _lt_time, gnrc_rpl_pid, GNRC_RPL_MSG_TYPE_LIFETIME_UPDATE, NULL);
+        xtimer_set_msg(&_lt_timer, _lt_time, &_lt_msg, gnrc_rpl_pid);
     }
 
     /* register all_RPL_nodes multicast address */
@@ -217,44 +217,41 @@ static void *_event_loop(void *args)
 
 void _update_lifetime(void)
 {
-    timex_t now;
-    vtimer_now(&now);
+    uint64_t now = xtimer_now64();
     gnrc_rpl_parent_t *parent;
     for (uint8_t i = 0; i < GNRC_RPL_PARENTS_NUMOF; ++i) {
         parent = &gnrc_rpl_parents[i];
         if (parent->state != 0) {
-            if ((signed)(parent->lifetime.seconds - now.seconds) <= GNRC_RPL_LIFETIME_UPDATE_STEP) {
+            if ((int64_t)(parent->lifetime - now) <= (int64_t) (GNRC_RPL_LIFETIME_UPDATE_STEP
+                * SEC_IN_USEC)) {
                 gnrc_rpl_dodag_t *dodag = parent->dodag;
                 gnrc_rpl_parent_remove(parent);
                 gnrc_rpl_parent_update(dodag, NULL);
                 continue;
             }
-            else if (((signed)(parent->lifetime.seconds - now.seconds) <=
-                        GNRC_RPL_LIFETIME_UPDATE_STEP * 2)) {
+            else if ((int64_t)(parent->lifetime - now) <= (int64_t) (GNRC_RPL_LIFETIME_UPDATE_STEP
+                     * SEC_IN_USEC * 2)) {
                 gnrc_rpl_send_DIS(parent->dodag, &parent->addr);
             }
         }
     }
-    vtimer_remove(&_lt_timer);
-    vtimer_set_msg(&_lt_timer, _lt_time, gnrc_rpl_pid, GNRC_RPL_MSG_TYPE_LIFETIME_UPDATE, NULL);
+    xtimer_set_msg(&_lt_timer, _lt_time, &_lt_msg, gnrc_rpl_pid);
 }
 
 void gnrc_rpl_delay_dao(gnrc_rpl_dodag_t *dodag)
 {
-    dodag->dao_time = timex_set(GNRC_RPL_DEFAULT_DAO_DELAY, 0);
+    dodag->dao_time = GNRC_RPL_DEFAULT_DAO_DELAY * SEC_IN_USEC;
     dodag->dao_counter = 0;
     dodag->dao_ack_received = false;
-    vtimer_remove(&dodag->dao_timer);
-    vtimer_set_msg(&dodag->dao_timer, dodag->dao_time, gnrc_rpl_pid, GNRC_RPL_MSG_TYPE_DAO_HANDLE, dodag);
+    xtimer_set_msg64(&dodag->dao_timer, dodag->dao_time, &dodag->dao_msg, gnrc_rpl_pid);
 }
 
 void gnrc_rpl_long_delay_dao(gnrc_rpl_dodag_t *dodag)
 {
-    dodag->dao_time = timex_set(GNRC_RPL_REGULAR_DAO_INTERVAL, 0);
+    dodag->dao_time = GNRC_RPL_REGULAR_DAO_INTERVAL * SEC_IN_USEC;
     dodag->dao_counter = 0;
     dodag->dao_ack_received = false;
-    vtimer_remove(&dodag->dao_timer);
-    vtimer_set_msg(&dodag->dao_timer, dodag->dao_time, gnrc_rpl_pid, GNRC_RPL_MSG_TYPE_DAO_HANDLE, dodag);
+    xtimer_set_msg64(&dodag->dao_timer, dodag->dao_time, &dodag->dao_msg, gnrc_rpl_pid);
 }
 
 void _dao_handle_send(gnrc_rpl_dodag_t *dodag)
@@ -262,10 +259,8 @@ void _dao_handle_send(gnrc_rpl_dodag_t *dodag)
     if ((dodag->dao_ack_received == false) && (dodag->dao_counter < GNRC_RPL_DAO_SEND_RETRIES)) {
         dodag->dao_counter++;
         gnrc_rpl_send_DAO(dodag, NULL, dodag->default_lifetime);
-        dodag->dao_time = timex_set(GNRC_RPL_DEFAULT_WAIT_FOR_DAO_ACK, 0);
-        vtimer_remove(&dodag->dao_timer);
-        vtimer_set_msg(&dodag->dao_timer, dodag->dao_time,
-                       gnrc_rpl_pid, GNRC_RPL_MSG_TYPE_DAO_HANDLE, dodag);
+        dodag->dao_time = GNRC_RPL_DEFAULT_WAIT_FOR_DAO_ACK * SEC_IN_USEC;
+        xtimer_set_msg64(&dodag->dao_timer, dodag->dao_time, &dodag->dao_msg, gnrc_rpl_pid);
     }
     else if (dodag->dao_ack_received == false) {
         gnrc_rpl_long_delay_dao(dodag);
diff --git a/sys/net/gnrc/routing/rpl/gnrc_rpl_dodag.c b/sys/net/gnrc/routing/rpl/gnrc_rpl_dodag.c
index ca7ec39493..3a9dbc8fb4 100644
--- a/sys/net/gnrc/routing/rpl/gnrc_rpl_dodag.c
+++ b/sys/net/gnrc/routing/rpl/gnrc_rpl_dodag.c
@@ -161,7 +161,11 @@ bool gnrc_rpl_dodag_add(gnrc_rpl_instance_t *instance, ipv6_addr_t *dodag_id, gn
         (*dodag)->dao_ack_received = false;
         (*dodag)->dao_counter = 0;
         (*dodag)->parents = NULL;
-        (*dodag)->cleanup_time = timex_set(GNRC_RPL_CLEANUP_TIME, 0);
+        (*dodag)->dao_msg.type = GNRC_RPL_MSG_TYPE_DAO_HANDLE;
+        (*dodag)->dao_msg.content.ptr = (char *) (*dodag);
+        (*dodag)->cleanup_time = GNRC_RPL_CLEANUP_TIME * SEC_IN_USEC;
+        (*dodag)->cleanup_msg.type = GNRC_RPL_MSG_TYPE_CLEANUP_HANDLE;
+        (*dodag)->cleanup_msg.content.ptr = (char *) (*dodag);
         return true;
     }
 
@@ -177,8 +181,8 @@ bool gnrc_rpl_dodag_remove(gnrc_rpl_dodag_t *dodag)
     gnrc_rpl_instance_t *inst = dodag->instance;
     LL_DELETE(inst->dodags, dodag);
     trickle_stop(&dodag->trickle);
-    vtimer_remove(&dodag->dao_timer);
-    vtimer_remove(&dodag->cleanup_timer);
+    xtimer_remove(&dodag->dao_timer);
+    xtimer_remove(&dodag->cleanup_timer);
     memset(dodag, 0, sizeof(gnrc_rpl_dodag_t));
     if (inst->dodags == NULL) {
         gnrc_rpl_instance_remove(inst);
@@ -192,9 +196,7 @@ void gnrc_rpl_dodag_remove_all_parents(gnrc_rpl_dodag_t *dodag)
     LL_FOREACH_SAFE(dodag->parents, elt, tmp) {
         gnrc_rpl_parent_remove(elt);
     }
-    vtimer_remove(&dodag->cleanup_timer);
-    vtimer_set_msg(&dodag->cleanup_timer, dodag->cleanup_time, gnrc_rpl_pid,
-            GNRC_RPL_MSG_TYPE_CLEANUP_HANDLE, dodag);
+    xtimer_set_msg(&dodag->cleanup_timer, dodag->cleanup_time, &dodag->cleanup_msg, gnrc_rpl_pid);
 }
 
 gnrc_rpl_dodag_t *gnrc_rpl_dodag_get(gnrc_rpl_instance_t *instance, ipv6_addr_t *dodag_id)
@@ -325,9 +327,8 @@ void gnrc_rpl_local_repair(gnrc_rpl_dodag_t *dodag)
 
     if (dodag->my_rank != GNRC_RPL_INFINITE_RANK) {
         trickle_reset_timer(&dodag->trickle);
-        vtimer_remove(&dodag->cleanup_timer);
-        vtimer_set_msg(&dodag->cleanup_timer, dodag->cleanup_time, gnrc_rpl_pid,
-            GNRC_RPL_MSG_TYPE_CLEANUP_HANDLE, dodag);
+        xtimer_set_msg(&dodag->cleanup_timer, dodag->cleanup_time, &dodag->cleanup_msg,
+                       gnrc_rpl_pid);
     }
 
     dodag->my_rank = GNRC_RPL_INFINITE_RANK;
@@ -336,14 +337,12 @@ void gnrc_rpl_local_repair(gnrc_rpl_dodag_t *dodag)
 void gnrc_rpl_parent_update(gnrc_rpl_dodag_t *dodag, gnrc_rpl_parent_t *parent)
 {
     uint16_t old_rank = dodag->my_rank;
-    timex_t now;
-    vtimer_now(&now);
+    uint64_t now = xtimer_now64();
     ipv6_addr_t def = IPV6_ADDR_UNSPECIFIED;
 
     /* update Parent lifetime */
     if (parent != NULL) {
-        parent->lifetime.seconds = now.seconds + (dodag->default_lifetime * dodag->lifetime_unit);
-        parent->lifetime.microseconds = 0;
+        parent->lifetime = now + ((dodag->default_lifetime * dodag->lifetime_unit) * SEC_IN_USEC);
         if (parent == dodag->parents) {
             ipv6_addr_t all_RPL_nodes = GNRC_RPL_ALL_NODES_ADDR;
             kernel_pid_t if_id;
-- 
GitLab