This commit is contained in:
Simon Duquennoy 2015-11-21 00:08:00 +01:00
parent 27da5b8ab5
commit 7eabf8d391
9 changed files with 85 additions and 73 deletions

View file

@ -45,6 +45,7 @@
#include "contiki.h"
#include "lib/list.h"
#include "lib/memb.h"
#include "lib/random.h"
#include "net/queuebuf.h"
#include "net/mac/rdc.h"
#include "net/mac/tsch/tsch.h"
@ -76,24 +77,6 @@ LIST(neighbor_list);
struct tsch_neighbor *n_broadcast;
struct tsch_neighbor *n_eb;
/*---------------------------------------------------------------------------*/
/**
* A pseudo-random generator with better properties than msp430-libc's default
**/
static uint32_t tsch_random_seed;
static void
tsch_random_init(uint32_t x)
{
tsch_random_seed = x;
}
static uint8_t
tsch_random_byte(uint8_t window)
{
tsch_random_seed = tsch_random_seed * 1103515245 + 12345;
return (tsch_random_seed / 65536) & window;
}
/*---------------------------------------------------------------------------*/
/* Add a TSCH neighbor */
struct tsch_neighbor *
@ -162,7 +145,15 @@ tsch_queue_update_time_source(const linkaddr_t *new_addr)
if(!tsch_is_locked()) {
if(!tsch_is_coordinator) {
struct tsch_neighbor *old_time_src = tsch_queue_get_time_source();
struct tsch_neighbor *new_time_src = new_addr ? tsch_queue_add_nbr(new_addr) : NULL;
struct tsch_neighbor *new_time_src = NULL;
if(new_addr != NULL) {
/* Get/add neighbor, return 0 in case of failure */
new_time_src = tsch_queue_add_nbr(new_addr);
if(new_time_src == NULL) {
return 0;
}
}
if(new_time_src != old_time_src) {
PRINTF("TSCH: update time source: %u -> %u\n",
@ -181,9 +172,9 @@ tsch_queue_update_time_source(const linkaddr_t *new_addr)
#ifdef TSCH_CALLBACK_NEW_TIME_SOURCE
TSCH_CALLBACK_NEW_TIME_SOURCE(old_time_src, new_time_src);
#endif
return 1;
}
return 1;
}
}
return 0;
@ -434,8 +425,10 @@ tsch_queue_backoff_inc(struct tsch_neighbor *n)
{
/* Increment exponent */
n->backoff_exponent = MIN(n->backoff_exponent + 1, TSCH_MAC_MAX_BE);
/* Pick a window (number of shared slots to skip) */
n->backoff_window = tsch_random_byte((1 << n->backoff_exponent) - 1);
/* Pick a window (number of shared slots to skip). Ignore least significant
* few bits, which, on some embedded implementations of rand (e.g. msp430-libc),
* are known to have poor pseudo-random properties. */
n->backoff_window = (random_rand() >> 6) % (1 << n->backoff_exponent);
/* Add one to the window as we will decrement it at the end of the current slot
* through tsch_queue_update_all_backoff_windows */
n->backoff_window++;
@ -464,8 +457,6 @@ void
tsch_queue_init(void)
{
list_init(neighbor_list);
tsch_random_init(*((uint32_t *)&linkaddr_node_addr) +
*((uint32_t *)&linkaddr_node_addr + 1));
memb_init(&neighbor_memb);
memb_init(&packet_memb);
/* Add virtual EB and the broadcast neighbors */