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.
This commit is contained in:
adamdunkels 2009-02-11 11:08:53 +00:00
parent b80d3cfdb9
commit 585620c102
10 changed files with 28 additions and 29 deletions

View file

@ -28,7 +28,7 @@
* *
* This file is part of the Contiki operating system. * 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 "dev/leds.h"
#include "lib/crc16.h" #include "lib/crc16.h"
#include "lib/rand.h" #include "lib/random.h"
#include "net/rime.h" #include "net/rime.h"
#include "net/rime/neighbor.h" #include "net/rime/neighbor.h"
@ -461,7 +461,7 @@ recv_netflood(struct netflood_conn *c, rimeaddr_t *from,
msg = rimebuf_dataptr(); msg = rimebuf_dataptr();
if(msg->type == NETFLOOD_TYPE_NODES) { if(msg->type == NETFLOOD_TYPE_NODES) {
ctimer_set(&ctimer, rand() % (CLOCK_SECOND * 8), ctimer_set(&ctimer, random_rand() % (CLOCK_SECOND * 8),
send_collect, NULL); send_collect, NULL);
} }
return 1; return 1;

View file

@ -28,7 +28,7 @@
* *
* This file is part of the Contiki operating system. * 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 "sys/clock.h"
#include "net/rime/timesynch.h" #include "net/rime/timesynch.h"
#include "lib/rand.h" #include "lib/random.h"
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
@ -318,7 +318,7 @@ PROCESS_THREAD(shell_randwait_process, ev, data)
/* printf("randwait %d command '%s'\n", /* printf("randwait %d command '%s'\n",
maxwait, command);*/ maxwait, command);*/
etimer_set(&etimer, rand() % (CLOCK_SECOND * maxwait)); etimer_set(&etimer, random_rand() % (CLOCK_SECOND * maxwait));
PROCESS_WAIT_UNTIL(etimer_expired(&etimer)); PROCESS_WAIT_UNTIL(etimer_expired(&etimer));
/* printf("Starting '%s' child %p (%s)\n", command, randwait_command.child, */ /* printf("Starting '%s' child %p (%s)\n", command, randwait_command.child, */

View file

@ -28,7 +28,7 @@
* *
* This file is part of the Contiki operating system. * 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 /* There is a bit of randomness here right now to avoid collisions
due to synchronization effects. Not sure how needed it is due to synchronization effects. Not sure how needed it is
though. XXX */ 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); (void (*)(void *))dutycycle, t);
PT_YIELD(&pt); PT_YIELD(&pt);
} else { } else {

View file

@ -33,7 +33,7 @@
* *
* This file is part of the Contiki operating system. * 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.h"
#include "net/rime/ipolite.h" #include "net/rime/ipolite.h"
#include "lib/rand.h" #include "lib/random.h"
#include <string.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(); c->q = queuebuf_new_from_rimebuf();
if(c->q != NULL) { if(c->q != NULL) {
ctimer_set(&c->t, ctimer_set(&c->t,
interval / 2 + (rand() % (interval / 2)), interval / 2 + (random_rand() % (interval / 2)),
send, c); send, c);
return 1; return 1;
} }

View file

@ -33,7 +33,7 @@
* *
* This file is part of the Contiki operating system. * 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 "dev/radio-sensor.h"
#include "lib/rand.h" #include "lib/random.h"
#if CONTIKI_TARGET_NETSIM #if CONTIKI_TARGET_NETSIM
#include "ether.h" #include "ether.h"
@ -129,7 +129,7 @@ send_timer(void *ptr)
struct neighbor_discovery_conn *tc = ptr; struct neighbor_discovery_conn *tc = ptr;
ctimer_set(&tc->send_timer, 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); send_adv, tc);
ctimer_set(&tc->interval_timer, ctimer_set(&tc->interval_timer,
tc->max_interval, tc->max_interval,
@ -179,7 +179,7 @@ neighbor_discovery_start(struct neighbor_discovery_conn *c, uint16_t val)
} }
c->val = val; c->val = val;
ctimer_set(&c->interval_timer, interval, send_timer, c); 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); send_adv, c);
} }
/*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/

View file

@ -33,7 +33,7 @@
* *
* This file is part of the Contiki operating system. * 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 "net/rime/netflood.h"
#include "lib/rand.h"
#include <string.h> #include <string.h>
#define HOPS_MAX 16 #define HOPS_MAX 16

View file

@ -33,7 +33,7 @@
* *
* This file is part of the Contiki operating system. * 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.h"
#include "net/rime/polite.h" #include "net/rime/polite.h"
#include "lib/rand.h" #include "lib/random.h"
#include <string.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->hdrsize = hdrsize;
c->q = queuebuf_new_from_rimebuf(); c->q = queuebuf_new_from_rimebuf();
if(c->q != NULL) { 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 1;
} }
return 0; return 0;

View file

@ -33,7 +33,7 @@
* *
* This file is part of the Contiki operating system. * 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 "net/rime/trickle.h"
#include "lib/rand.h" #include "lib/random.h"
#if CONTIKI_TARGET_NETSIM #if CONTIKI_TARGET_NETSIM
#include "ether.h" #include "ether.h"
@ -116,7 +116,7 @@ run_trickle(struct trickle_conn *c)
while(1) { while(1) {
interval = c->interval << c->interval_scaling; interval = c->interval << c->interval_scaling;
set_timer(c, &c->interval_timer, interval); 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; c->duplicates = 0;
PT_YIELD(&c->pt); /* Wait until listen timeout */ PT_YIELD(&c->pt); /* Wait until listen timeout */

View file

@ -28,7 +28,7 @@
* *
* This file is part of the Contiki operating system. * 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 /* Send a packet every 16 seconds; first wait 8 seconds, than a
random time between 0 and 8 seconds. */ 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(); PROCESS_WAIT_EVENT();
if(etimer_expired(&et)) { if(etimer_expired(&et)) {

View file

@ -26,7 +26,7 @@
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE. * 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> #include <signal.h>
@ -52,11 +52,10 @@
#include "net/mac/nullmac.h" #include "net/mac/nullmac.h"
#include "net/mac/xmac.h" #include "net/mac/xmac.h"
#include "net/mac/lpp.h"
#include "net/rime.h" #include "net/rime.h"
#include "lib/rand.h"
#include "node-id.h" #include "node-id.h"
#include "cfs-coffee-arch.h" #include "cfs-coffee-arch.h"
#include "cfs/cfs-coffee.h" #include "cfs/cfs-coffee.h"
@ -239,7 +238,7 @@ main(int argc, char **argv)
rime_init(xmac_init(&cc2420_driver)); rime_init(xmac_init(&cc2420_driver));
#endif #endif
srand(ds2411_id[0] + node_id); random_init(ds2411_id[0] + node_id);
printf(CONTIKI_VERSION_STRING " started. "); printf(CONTIKI_VERSION_STRING " started. ");
if(node_id > 0) { if(node_id > 0) {