Rewrote the neighbor code to use memb and a list instead of a static array
This commit is contained in:
parent
b001c14bf2
commit
2886e03f57
2 changed files with 158 additions and 81 deletions
|
@ -33,7 +33,7 @@
|
||||||
*
|
*
|
||||||
* This file is part of the Contiki operating system.
|
* This file is part of the Contiki operating system.
|
||||||
*
|
*
|
||||||
* $Id: neighbor.c,v 1.13 2007/12/09 15:44:21 adamdunkels Exp $
|
* $Id: neighbor.c,v 1.14 2008/02/03 20:44:11 adamdunkels Exp $
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -47,50 +47,75 @@
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
#include "contiki.h"
|
#include "contiki.h"
|
||||||
|
#include "lib/memb.h"
|
||||||
|
#include "lib/list.h"
|
||||||
#include "net/rime/neighbor.h"
|
#include "net/rime/neighbor.h"
|
||||||
#include "net/rime/ctimer.h"
|
#include "net/rime/ctimer.h"
|
||||||
#include "net/rime/collect.h"
|
#include "net/rime/collect.h"
|
||||||
|
|
||||||
#define MAX_NEIGHBORS 5
|
#define MAX_NEIGHBORS 8
|
||||||
|
|
||||||
#define RTMETRIC_MAX COLLECT_MAX_DEPTH
|
#define RTMETRIC_MAX COLLECT_MAX_DEPTH
|
||||||
|
|
||||||
static struct neighbor neighbors[MAX_NEIGHBORS];
|
MEMB(neighbors_mem, struct neighbor, MAX_NEIGHBORS);
|
||||||
|
LIST(neighbors_list);
|
||||||
|
|
||||||
|
/*static struct neighbor neighbors[MAX_NEIGHBORS];*/
|
||||||
|
|
||||||
static struct ctimer t;
|
static struct ctimer t;
|
||||||
|
|
||||||
static int max_time = 30;
|
static int max_time = 120;
|
||||||
|
|
||||||
|
#define DEBUG 0
|
||||||
|
#if DEBUG
|
||||||
|
#include <stdio.h>
|
||||||
|
#define PRINTF(...) printf(__VA_ARGS__)
|
||||||
|
#else
|
||||||
|
#define PRINTF(...)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
/*---------------------------------------------------------------------------*/
|
/*---------------------------------------------------------------------------*/
|
||||||
static void
|
static void
|
||||||
periodic(void *ptr)
|
periodic(void *ptr)
|
||||||
{
|
{
|
||||||
int i;
|
struct neighbor *n, *next;
|
||||||
|
|
||||||
/* Go through all neighbors and remove old ones. */
|
/* Go through all neighbors and remove old ones. */
|
||||||
|
for(n = list_head(neighbors_list); n != NULL; n = next) {
|
||||||
for(i = 0; i < MAX_NEIGHBORS; ++i) {
|
next = NULL;
|
||||||
if(!rimeaddr_cmp(&neighbors[i].addr, &rimeaddr_null) &&
|
/* for(i = 0; i < MAX_NEIGHBORS; ++i) {*/
|
||||||
neighbors[i].time < max_time) {
|
if(!rimeaddr_cmp(&n->addr, &rimeaddr_null) &&
|
||||||
neighbors[i].time++;
|
n->time < max_time) {
|
||||||
if(neighbors[i].time == max_time) {
|
n->time++;
|
||||||
neighbors[i].rtmetric = RTMETRIC_MAX;
|
if(n->time == max_time) {
|
||||||
/* printf("%d: removing old neighbor %d\n", node_id, neighbors[i].nodeid);*/
|
n->rtmetric = RTMETRIC_MAX;
|
||||||
rimeaddr_copy(&neighbors[i].addr, &rimeaddr_null);
|
PRINTF("%d.%d: removing old neighbor %d.%d\n",
|
||||||
|
rimeaddr_node_addr.u8[0],rimeaddr_node_addr.u8[1],
|
||||||
|
n->addr.u8[0], n->addr.u8[1]);
|
||||||
|
rimeaddr_copy(&n->addr, &rimeaddr_null);
|
||||||
|
next = n->next;
|
||||||
|
list_remove(neighbors_list, n);
|
||||||
|
memb_free(&neighbors_mem, n);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if(next == NULL) {
|
||||||
|
next = n->next;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
/* printf("neighbor periodic\n");*/
|
/* PRINTF("neighbor periodic\n");*/
|
||||||
ctimer_set(&t, CLOCK_SECOND, periodic, NULL);
|
ctimer_set(&t, CLOCK_SECOND, periodic, NULL);
|
||||||
}
|
}
|
||||||
/*---------------------------------------------------------------------------*/
|
/*---------------------------------------------------------------------------*/
|
||||||
void
|
void
|
||||||
neighbor_init(void)
|
neighbor_init(void)
|
||||||
{
|
{
|
||||||
int i;
|
|
||||||
|
memb_init(&neighbors_mem);
|
||||||
for(i = 0; i < MAX_NEIGHBORS; ++i) {
|
list_init(neighbors_list);
|
||||||
|
/* for(i = 0; i < MAX_NEIGHBORS; ++i) {
|
||||||
rimeaddr_copy(&neighbors[i].addr, &rimeaddr_null);
|
rimeaddr_copy(&neighbors[i].addr, &rimeaddr_null);
|
||||||
}
|
}*/
|
||||||
|
|
||||||
ctimer_set(&t, CLOCK_SECOND, periodic, NULL);
|
ctimer_set(&t, CLOCK_SECOND, periodic, NULL);
|
||||||
}
|
}
|
||||||
|
@ -98,11 +123,10 @@ neighbor_init(void)
|
||||||
struct neighbor *
|
struct neighbor *
|
||||||
neighbor_find(rimeaddr_t *addr)
|
neighbor_find(rimeaddr_t *addr)
|
||||||
{
|
{
|
||||||
int i;
|
struct neighbor *n;
|
||||||
|
for(n = list_head(neighbors_list); n != NULL; n = n->next) {
|
||||||
for(i = 0; i < MAX_NEIGHBORS; ++i) {
|
if(rimeaddr_cmp(&n->addr, addr)) {
|
||||||
if(rimeaddr_cmp(&neighbors[i].addr, addr)) {
|
return n;
|
||||||
return &neighbors[i];
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return NULL;
|
return NULL;
|
||||||
|
@ -151,107 +175,145 @@ neighbor_etx(struct neighbor *n)
|
||||||
void
|
void
|
||||||
neighbor_add(rimeaddr_t *addr, u8_t nrtmetric, u8_t netx)
|
neighbor_add(rimeaddr_t *addr, u8_t nrtmetric, u8_t netx)
|
||||||
{
|
{
|
||||||
int i, n;
|
uint16_t rtmetric;
|
||||||
u8_t rtmetric;
|
uint16_t etx;
|
||||||
u8_t etx;
|
struct neighbor *n, *max;
|
||||||
|
int i;
|
||||||
|
|
||||||
/* Find the first unused entry or the used entry with the highest
|
PRINTF("neighbor_add: adding %d.%d\n", addr->u8[0], addr->u8[1]);
|
||||||
rtmetric and highest etx. */
|
|
||||||
rtmetric = 0;
|
/* Check if the neighbor is already on the list. */
|
||||||
etx = 0;
|
for(n = list_head(neighbors_list); n != NULL; n = n->next) {
|
||||||
|
if(rimeaddr_cmp(&n->addr, &rimeaddr_null) ||
|
||||||
n = 0;
|
rimeaddr_cmp(&n->addr, addr)) {
|
||||||
for(i = 0; i < MAX_NEIGHBORS; ++i) {
|
PRINTF("neighbor_add: already on list %d.%d\n", addr->u8[0], addr->u8[1]);
|
||||||
if(rimeaddr_cmp(&neighbors[i].addr, &rimeaddr_null) ||
|
|
||||||
rimeaddr_cmp(&neighbors[i].addr, addr)) {
|
|
||||||
n = i;
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if(!rimeaddr_cmp(&neighbors[i].addr, &rimeaddr_null)) {
|
}
|
||||||
if(neighbors[i].rtmetric > rtmetric) {
|
|
||||||
rtmetric = neighbors[i].rtmetric;
|
/* If the neighbor was not on the list, we try to allocate memory
|
||||||
etx = neighbor_etx(&neighbors[i]);
|
for it. */
|
||||||
n = i;
|
if(n == NULL) {
|
||||||
} else if(neighbors[i].rtmetric == rtmetric) {
|
PRINTF("neighbor_add: not on list, allocating %d.%d\n", addr->u8[0], addr->u8[1]);
|
||||||
if(neighbor_etx(&neighbors[i]) > etx) {
|
n = memb_alloc(&neighbors_mem);
|
||||||
rtmetric = neighbors[i].rtmetric;
|
if(n != NULL) {
|
||||||
etx = neighbor_etx(&neighbors[i]);
|
list_add(neighbors_list, n);
|
||||||
n = i;
|
}
|
||||||
/* printf("%d: found worst neighbor %d with rtmetric %d, signal %d\n",
|
}
|
||||||
|
|
||||||
|
/* If we could not allocate memory, we try to recycle an old
|
||||||
|
neighbor */
|
||||||
|
if(n == NULL) {
|
||||||
|
PRINTF("neighbor_add: not on list, not allocated, recycling %d.%d\n", addr->u8[0], addr->u8[1]);
|
||||||
|
/* Find the first unused entry or the used entry with the highest
|
||||||
|
rtmetric and highest etx. */
|
||||||
|
rtmetric = 0;
|
||||||
|
etx = 0;
|
||||||
|
max = NULL;
|
||||||
|
|
||||||
|
for(n = list_head(neighbors_list); n != NULL; n = n->next) {
|
||||||
|
if(!rimeaddr_cmp(&n->addr, &rimeaddr_null)) {
|
||||||
|
if(n->rtmetric > rtmetric) {
|
||||||
|
rtmetric = n->rtmetric;
|
||||||
|
etx = neighbor_etx(n);
|
||||||
|
max = n;
|
||||||
|
} else if(n->rtmetric == rtmetric) {
|
||||||
|
if(neighbor_etx(n) > etx) {
|
||||||
|
rtmetric = n->rtmetric;
|
||||||
|
etx = neighbor_etx(n);
|
||||||
|
max = n;
|
||||||
|
/* PRINTF("%d: found worst neighbor %d with rtmetric %d, signal %d\n",
|
||||||
node_id, neighbors[n].nodeid, rtmetric, signal);*/
|
node_id, neighbors[n].nodeid, rtmetric, signal);*/
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
n = max;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/* printf("%d: adding neighbor %d with rtmetric %d, signal %d at %d\n",
|
/* PRINTF("%d: adding neighbor %d with rtmetric %d, signal %d at %d\n",
|
||||||
node_id, neighbors[n].nodeid, rtmetric, signal, n);*/
|
node_id, neighbors[n].nodeid, rtmetric, signal, n);*/
|
||||||
|
if(n != NULL) {
|
||||||
neighbors[n].time = 0;
|
n->time = 0;
|
||||||
rimeaddr_copy(&neighbors[i].addr, addr);
|
rimeaddr_copy(&n->addr, addr);
|
||||||
neighbors[n].rtmetric = nrtmetric;
|
n->rtmetric = nrtmetric;
|
||||||
for(i = 0; i < NEIGHBOR_NUM_ETXS; ++i) {
|
for(i = 0; i < NEIGHBOR_NUM_ETXS; ++i) {
|
||||||
neighbors[n].etxs[i] = netx;
|
n->etxs[i] = netx;
|
||||||
|
}
|
||||||
|
n->etxptr = 0;
|
||||||
}
|
}
|
||||||
neighbors[n].etxptr = 0;
|
|
||||||
}
|
}
|
||||||
/*---------------------------------------------------------------------------*/
|
/*---------------------------------------------------------------------------*/
|
||||||
void
|
void
|
||||||
neighbor_remove(rimeaddr_t *addr)
|
neighbor_remove(rimeaddr_t *addr)
|
||||||
{
|
{
|
||||||
int i;
|
struct neighbor *n;
|
||||||
|
|
||||||
|
for(n = list_head(neighbors_list); n != NULL; n = n->next) {
|
||||||
|
if(rimeaddr_cmp(&n->addr, addr)) {
|
||||||
|
PRINTF("%d: removing %d\n", rimeaddr_node_addr.u16[0], addr->u16[0]);
|
||||||
|
rimeaddr_copy(&n->addr, &rimeaddr_null);
|
||||||
|
n->rtmetric = RTMETRIC_MAX;
|
||||||
|
list_remove(neighbors_list, n);
|
||||||
|
memb_free(&neighbors_mem, n);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/* int i;
|
||||||
|
|
||||||
for(i = 0; i < MAX_NEIGHBORS; ++i) {
|
for(i = 0; i < MAX_NEIGHBORS; ++i) {
|
||||||
if(rimeaddr_cmp(&neighbors[i].addr, addr)) {
|
if(rimeaddr_cmp(&neighbors[i].addr, addr)) {
|
||||||
printf("%d: removing %d @ %d\n", rimeaddr_node_addr.u16[0], addr->u16[0], i);
|
PRINTF("%d: removing %d @ %d\n", rimeaddr_node_addr.u16[0], addr->u16[0], i);
|
||||||
rimeaddr_copy(&neighbors[i].addr, &rimeaddr_null);
|
rimeaddr_copy(&neighbors[i].addr, &rimeaddr_null);
|
||||||
neighbors[i].rtmetric = RTMETRIC_MAX;
|
neighbors[i].rtmetric = RTMETRIC_MAX;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}*/
|
||||||
}
|
}
|
||||||
/*---------------------------------------------------------------------------*/
|
/*---------------------------------------------------------------------------*/
|
||||||
struct neighbor *
|
struct neighbor *
|
||||||
neighbor_best(void)
|
neighbor_best(void)
|
||||||
{
|
{
|
||||||
int i, found;
|
int found;
|
||||||
int lowest, best;
|
/* int lowest, best;*/
|
||||||
|
struct neighbor *n, *lowest, *best;
|
||||||
u8_t rtmetric;
|
u8_t rtmetric;
|
||||||
u8_t etx;
|
u8_t etx;
|
||||||
|
|
||||||
rtmetric = RTMETRIC_MAX;
|
rtmetric = RTMETRIC_MAX;
|
||||||
lowest = 0;
|
lowest = best = NULL;
|
||||||
found = 0;
|
found = 0;
|
||||||
|
|
||||||
/* printf("%d: ", node_id);*/
|
/* PRINTF("%d: ", node_id);*/
|
||||||
|
|
||||||
/* Find the lowest rtmetric. */
|
/* Find the lowest rtmetric. */
|
||||||
for(i = 0; i < MAX_NEIGHBORS; ++i) {
|
for(n = list_head(neighbors_list); n != NULL; n = n->next) {
|
||||||
/* printf("%d:%d ", neighbors[i].nodeid, neighbors[i].rtmetric);*/
|
if(!rimeaddr_cmp(&n->addr, &rimeaddr_null) &&
|
||||||
if(!rimeaddr_cmp(&neighbors[i].addr, &rimeaddr_null) &&
|
rtmetric > n->rtmetric) {
|
||||||
rtmetric > neighbors[i].rtmetric) {
|
rtmetric = n->rtmetric;
|
||||||
rtmetric = neighbors[i].rtmetric;
|
lowest = n;
|
||||||
lowest = i;
|
|
||||||
found = 1;
|
found = 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
/* printf("\n");*/
|
|
||||||
|
/* PRINTF("\n");*/
|
||||||
|
|
||||||
/* Find the neighbor with lowest etx of the ones that
|
/* Find the neighbor with lowest etx of the ones that
|
||||||
have the lowest rtmetric. */
|
have the lowest rtmetric. */
|
||||||
if(found) {
|
if(found) {
|
||||||
etx = 0;
|
etx = 0;
|
||||||
best = lowest;
|
best = lowest;
|
||||||
for(i = 0; i < MAX_NEIGHBORS; ++i) {
|
for(n = list_head(neighbors_list); n != NULL; n = n->next) {
|
||||||
if(!rimeaddr_cmp(&neighbors[i].addr, &rimeaddr_null) &&
|
/* for(i = 0; i < MAX_NEIGHBORS; ++i) {*/
|
||||||
rtmetric == neighbors[i].rtmetric &&
|
if(!rimeaddr_cmp(&n->addr, &rimeaddr_null) &&
|
||||||
neighbor_etx(&neighbors[i]) < etx) {
|
rtmetric == n->rtmetric &&
|
||||||
etx = neighbor_etx(&neighbors[i]);
|
neighbor_etx(n) < etx) {
|
||||||
best = i;
|
etx = neighbor_etx(n);
|
||||||
|
best = n;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return &neighbors[best];
|
return best;
|
||||||
}
|
}
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
@ -265,13 +327,27 @@ neighbor_set_lifetime(int seconds)
|
||||||
int
|
int
|
||||||
neighbor_num(void)
|
neighbor_num(void)
|
||||||
{
|
{
|
||||||
return MAX_NEIGHBORS;
|
PRINTF("neighbor_num %d\n", list_length(neighbors_list));
|
||||||
|
return list_length(neighbors_list);
|
||||||
}
|
}
|
||||||
/*---------------------------------------------------------------------------*/
|
/*---------------------------------------------------------------------------*/
|
||||||
struct neighbor *
|
struct neighbor *
|
||||||
neighbor_get(int num)
|
neighbor_get(int num)
|
||||||
{
|
{
|
||||||
return &neighbors[num];
|
int i;
|
||||||
|
struct neighbor *n;
|
||||||
|
|
||||||
|
PRINTF("neighbor_get %d\n", num);
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
for(n = list_head(neighbors_list); n != NULL; n = n->next) {
|
||||||
|
if(i == num) {
|
||||||
|
PRINTF("neighbor_get found %d.%d\n", n->addr.u8[0], n->addr.u8[1]);
|
||||||
|
return n;
|
||||||
|
}
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
return NULL;
|
||||||
}
|
}
|
||||||
/*---------------------------------------------------------------------------*/
|
/*---------------------------------------------------------------------------*/
|
||||||
/** @} */
|
/** @} */
|
||||||
|
|
|
@ -38,7 +38,7 @@
|
||||||
*
|
*
|
||||||
* This file is part of the Contiki operating system.
|
* This file is part of the Contiki operating system.
|
||||||
*
|
*
|
||||||
* $Id: neighbor.h,v 1.9 2007/12/09 15:43:51 adamdunkels Exp $
|
* $Id: neighbor.h,v 1.10 2008/02/03 20:44:11 adamdunkels Exp $
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -57,6 +57,7 @@
|
||||||
#define NEIGHBOR_NUM_ETXS 8
|
#define NEIGHBOR_NUM_ETXS 8
|
||||||
|
|
||||||
struct neighbor {
|
struct neighbor {
|
||||||
|
struct neighbor *next;
|
||||||
uint16_t time;
|
uint16_t time;
|
||||||
rimeaddr_t addr;
|
rimeaddr_t addr;
|
||||||
uint16_t rtmetric;
|
uint16_t rtmetric;
|
||||||
|
|
Loading…
Reference in a new issue