From 585620c102ef098c8731bafe76a330437de4d9f9 Mon Sep 17 00:00:00 2001
From: adamdunkels <adamdunkels>
Date: Wed, 11 Feb 2009 11:08:53 +0000
Subject: [PATCH] Reverted the recent change from random_rand() to rand(). It
 turned out that since libc rand() returns a signed int, there were frequently
 problems with timer values wrapping. By reverting to random_rand(), we can
 provide a random generator that returns an unsigned and the timer problems
 are solved.

---
 apps/shell/shell-rime.c            | 6 +++---
 apps/shell/shell-time.c            | 6 +++---
 core/net/mac/lpp.c                 | 4 ++--
 core/net/rime/ipolite.c            | 6 +++---
 core/net/rime/neighbor-discovery.c | 8 ++++----
 core/net/rime/netflood.c           | 4 ++--
 core/net/rime/polite.c             | 6 +++---
 core/net/rime/trickle.c            | 6 +++---
 examples/rime/example-collect.c    | 4 ++--
 platform/sky/contiki-sky-main.c    | 7 +++----
 10 files changed, 28 insertions(+), 29 deletions(-)

diff --git a/apps/shell/shell-rime.c b/apps/shell/shell-rime.c
index 49e27dfa0..a599363cd 100644
--- a/apps/shell/shell-rime.c
+++ b/apps/shell/shell-rime.c
@@ -28,7 +28,7 @@
  *
  * This file is part of the Contiki operating system.
  *
- * $Id: shell-rime.c,v 1.11 2009/02/09 20:54:49 adamdunkels Exp $
+ * $Id: shell-rime.c,v 1.12 2009/02/11 11:08:53 adamdunkels Exp $
  */
 
 /**
@@ -45,7 +45,7 @@
 #include "dev/leds.h"
 
 #include "lib/crc16.h"
-#include "lib/rand.h"
+#include "lib/random.h"
 
 #include "net/rime.h"
 #include "net/rime/neighbor.h"
@@ -461,7 +461,7 @@ recv_netflood(struct netflood_conn *c, rimeaddr_t *from,
   
   msg = rimebuf_dataptr();
   if(msg->type == NETFLOOD_TYPE_NODES) {
-    ctimer_set(&ctimer, rand() % (CLOCK_SECOND * 8),
+    ctimer_set(&ctimer, random_rand() % (CLOCK_SECOND * 8),
 	       send_collect, NULL);
   }
   return 1;
diff --git a/apps/shell/shell-time.c b/apps/shell/shell-time.c
index 9876226e2..6ffe86819 100644
--- a/apps/shell/shell-time.c
+++ b/apps/shell/shell-time.c
@@ -28,7 +28,7 @@
  *
  * This file is part of the Contiki operating system.
  *
- * $Id: shell-time.c,v 1.6 2009/02/09 20:54:49 adamdunkels Exp $
+ * $Id: shell-time.c,v 1.7 2009/02/11 11:08:55 adamdunkels Exp $
  */
 
 /**
@@ -44,7 +44,7 @@
 #include "sys/clock.h"
 #include "net/rime/timesynch.h"
 
-#include "lib/rand.h"
+#include "lib/random.h"
 
 #include <stdio.h>
 #include <string.h>
@@ -318,7 +318,7 @@ PROCESS_THREAD(shell_randwait_process, ev, data)
   /*  printf("randwait %d command '%s'\n",
       maxwait, command);*/
 
-  etimer_set(&etimer, rand() % (CLOCK_SECOND * maxwait));
+  etimer_set(&etimer, random_rand() % (CLOCK_SECOND * maxwait));
   PROCESS_WAIT_UNTIL(etimer_expired(&etimer));
 
 /*   printf("Starting '%s' child %p (%s)\n", command, randwait_command.child, */
diff --git a/core/net/mac/lpp.c b/core/net/mac/lpp.c
index e2a838c0d..660a9d961 100644
--- a/core/net/mac/lpp.c
+++ b/core/net/mac/lpp.c
@@ -28,7 +28,7 @@
  *
  * This file is part of the Contiki operating system.
  *
- * $Id: lpp.c,v 1.7 2009/02/08 20:14:18 adamdunkels Exp $
+ * $Id: lpp.c,v 1.8 2009/02/11 11:08:56 adamdunkels Exp $
  */
 
 /**
@@ -196,7 +196,7 @@ dutycycle(void *ptr)
       /* There is a bit of randomness here right now to avoid collisions
 	 due to synchronization effects. Not sure how needed it is
 	 though. XXX */
-	ctimer_set(t, OFF_TIME / 2 + (rand() % (OFF_TIME / 2)),
+	ctimer_set(t, OFF_TIME / 2 + (random_rand() % (OFF_TIME / 2)),
 		   (void (*)(void *))dutycycle, t);
 	PT_YIELD(&pt);
       } else {
diff --git a/core/net/rime/ipolite.c b/core/net/rime/ipolite.c
index 561a44228..609d70028 100644
--- a/core/net/rime/ipolite.c
+++ b/core/net/rime/ipolite.c
@@ -33,7 +33,7 @@
  *
  * This file is part of the Contiki operating system.
  *
- * $Id: ipolite.c,v 1.11 2009/02/07 16:16:31 adamdunkels Exp $
+ * $Id: ipolite.c,v 1.12 2009/02/11 11:08:56 adamdunkels Exp $
  */
 
 /**
@@ -45,7 +45,7 @@
 
 #include "net/rime.h"
 #include "net/rime/ipolite.h"
-#include "lib/rand.h"
+#include "lib/random.h"
 
 #include <string.h>
 
@@ -153,7 +153,7 @@ ipolite_send(struct ipolite_conn *c, clock_time_t interval, uint8_t hdrsize)
     c->q = queuebuf_new_from_rimebuf();
     if(c->q != NULL) {
       ctimer_set(&c->t,
-		 interval / 2 + (rand() % (interval / 2)),
+		 interval / 2 + (random_rand() % (interval / 2)),
 		 send, c);
       return 1;
     }
diff --git a/core/net/rime/neighbor-discovery.c b/core/net/rime/neighbor-discovery.c
index 624c7c75c..7d856e83e 100644
--- a/core/net/rime/neighbor-discovery.c
+++ b/core/net/rime/neighbor-discovery.c
@@ -33,7 +33,7 @@
  *
  * This file is part of the Contiki operating system.
  *
- * $Id: neighbor-discovery.c,v 1.12 2009/02/07 18:43:45 adamdunkels Exp $
+ * $Id: neighbor-discovery.c,v 1.13 2009/02/11 11:08:56 adamdunkels Exp $
  */
 
 /**
@@ -51,7 +51,7 @@
 
 #include "dev/radio-sensor.h"
 
-#include "lib/rand.h"
+#include "lib/random.h"
 
 #if CONTIKI_TARGET_NETSIM
 #include "ether.h"
@@ -129,7 +129,7 @@ send_timer(void *ptr)
   struct neighbor_discovery_conn *tc = ptr;
 
   ctimer_set(&tc->send_timer,
-	     tc->max_interval / 2 + rand() % (tc->max_interval / 2),
+	     tc->max_interval / 2 + random_rand() % (tc->max_interval / 2),
 	     send_adv, tc);
   ctimer_set(&tc->interval_timer,
 	     tc->max_interval,
@@ -179,7 +179,7 @@ neighbor_discovery_start(struct neighbor_discovery_conn *c, uint16_t val)
   }
   c->val = val;
   ctimer_set(&c->interval_timer, interval, send_timer, c);
-  ctimer_set(&c->send_timer, interval / 2 + rand() % (interval / 2),
+  ctimer_set(&c->send_timer, interval / 2 + random_rand() % (interval / 2),
 	     send_adv, c);
 }
 /*---------------------------------------------------------------------------*/
diff --git a/core/net/rime/netflood.c b/core/net/rime/netflood.c
index a1a9ecf3d..80d6b632e 100644
--- a/core/net/rime/netflood.c
+++ b/core/net/rime/netflood.c
@@ -33,7 +33,7 @@
  *
  * This file is part of the Contiki operating system.
  *
- * $Id: netflood.c,v 1.1 2008/07/03 22:25:22 adamdunkels Exp $
+ * $Id: netflood.c,v 1.2 2009/02/11 11:08:56 adamdunkels Exp $
  */
 
 /**
@@ -44,7 +44,7 @@
  */
 
 #include "net/rime/netflood.h"
-#include "lib/rand.h"
+
 #include <string.h>
 
 #define HOPS_MAX 16
diff --git a/core/net/rime/polite.c b/core/net/rime/polite.c
index bdeac9725..8c0c2441f 100644
--- a/core/net/rime/polite.c
+++ b/core/net/rime/polite.c
@@ -33,7 +33,7 @@
  *
  * This file is part of the Contiki operating system.
  *
- * $Id: polite.c,v 1.6 2009/02/07 16:16:31 adamdunkels Exp $
+ * $Id: polite.c,v 1.7 2009/02/11 11:08:56 adamdunkels Exp $
  */
 
 /**
@@ -45,7 +45,7 @@
 
 #include "net/rime.h"
 #include "net/rime/polite.h"
-#include "lib/rand.h"
+#include "lib/random.h"
 
 #include <string.h>
 
@@ -128,7 +128,7 @@ polite_send(struct polite_conn *c, clock_time_t interval, uint8_t hdrsize)
   c->hdrsize = hdrsize;
   c->q = queuebuf_new_from_rimebuf();
   if(c->q != NULL) {
-    ctimer_set(&c->t, interval / 2 + (rand() % (interval / 2)), send, c);
+    ctimer_set(&c->t, interval / 2 + (random_rand() % (interval / 2)), send, c);
     return 1;
   }
   return 0;
diff --git a/core/net/rime/trickle.c b/core/net/rime/trickle.c
index cfd922728..55b858c08 100644
--- a/core/net/rime/trickle.c
+++ b/core/net/rime/trickle.c
@@ -33,7 +33,7 @@
  *
  * This file is part of the Contiki operating system.
  *
- * $Id: trickle.c,v 1.14 2009/02/07 16:16:31 adamdunkels Exp $
+ * $Id: trickle.c,v 1.15 2009/02/11 11:08:56 adamdunkels Exp $
  */
 
 /**
@@ -44,7 +44,7 @@
  */
 
 #include "net/rime/trickle.h"
-#include "lib/rand.h"
+#include "lib/random.h"
 
 #if CONTIKI_TARGET_NETSIM
 #include "ether.h"
@@ -116,7 +116,7 @@ run_trickle(struct trickle_conn *c)
   while(1) {
     interval = c->interval << c->interval_scaling;
     set_timer(c, &c->interval_timer, interval);
-    set_timer(c, &c->t, interval / 2 + (rand() % (interval / 2)));
+    set_timer(c, &c->t, interval / 2 + (random_rand() % (interval / 2)));
 
     c->duplicates = 0;
     PT_YIELD(&c->pt); /* Wait until listen timeout */
diff --git a/examples/rime/example-collect.c b/examples/rime/example-collect.c
index f5777b5a8..53fd7dd55 100644
--- a/examples/rime/example-collect.c
+++ b/examples/rime/example-collect.c
@@ -28,7 +28,7 @@
  *
  * This file is part of the Contiki operating system.
  *
- * $Id: example-collect.c,v 1.4 2009/02/07 16:17:33 adamdunkels Exp $
+ * $Id: example-collect.c,v 1.5 2009/02/11 11:08:56 adamdunkels Exp $
  */
 
 /**
@@ -79,7 +79,7 @@ PROCESS_THREAD(example_collect_process, ev, data)
     /* Send a packet every 16 seconds; first wait 8 seconds, than a
        random time between 0 and 8 seconds. */
 
-    etimer_set(&et, CLOCK_SECOND * 16 + rand() % (CLOCK_SECOND * 16));
+    etimer_set(&et, CLOCK_SECOND * 16 + random_rand() % (CLOCK_SECOND * 16));
     PROCESS_WAIT_EVENT();
 
     if(etimer_expired(&et)) {
diff --git a/platform/sky/contiki-sky-main.c b/platform/sky/contiki-sky-main.c
index b62dcbe5a..11a71d610 100644
--- a/platform/sky/contiki-sky-main.c
+++ b/platform/sky/contiki-sky-main.c
@@ -26,7 +26,7 @@
  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  * SUCH DAMAGE.
  *
- * @(#)$Id: contiki-sky-main.c,v 1.46 2009/02/07 16:49:36 adamdunkels Exp $
+ * @(#)$Id: contiki-sky-main.c,v 1.47 2009/02/11 11:08:56 adamdunkels Exp $
  */
 
 #include <signal.h>
@@ -52,11 +52,10 @@
 
 #include "net/mac/nullmac.h"
 #include "net/mac/xmac.h"
+#include "net/mac/lpp.h"
 
 #include "net/rime.h"
 
-#include "lib/rand.h"
-
 #include "node-id.h"
 #include "cfs-coffee-arch.h"
 #include "cfs/cfs-coffee.h"
@@ -239,7 +238,7 @@ main(int argc, char **argv)
   rime_init(xmac_init(&cc2420_driver));
 #endif
 
-  srand(ds2411_id[0] + node_id);
+  random_init(ds2411_id[0] + node_id);
 
   printf(CONTIKI_VERSION_STRING " started. ");
   if(node_id > 0) {