2007-03-31 18:31:27 +00:00
|
|
|
/**
|
|
|
|
* \addtogroup rimeneighbor
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
2007-02-28 16:38:51 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2006, Swedish Institute of Computer Science.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. Neither the name of the Institute nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
* This file is part of the Contiki operating system.
|
|
|
|
*
|
2010-02-03 20:38:33 +00:00
|
|
|
* $Id: neighbor.c,v 1.22 2010/02/03 20:38:33 adamdunkels Exp $
|
2007-02-28 16:38:51 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \file
|
|
|
|
* Radio neighborhood management
|
|
|
|
* \author
|
|
|
|
* Adam Dunkels <adam@sics.se>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <limits.h>
|
2007-03-24 13:57:04 +00:00
|
|
|
#include <stdio.h>
|
2007-02-28 16:38:51 +00:00
|
|
|
|
|
|
|
#include "contiki.h"
|
2008-02-03 20:44:11 +00:00
|
|
|
#include "lib/memb.h"
|
|
|
|
#include "lib/list.h"
|
2007-02-28 16:38:51 +00:00
|
|
|
#include "net/rime/neighbor.h"
|
2007-03-22 17:34:43 +00:00
|
|
|
#include "net/rime/ctimer.h"
|
2007-12-09 15:44:21 +00:00
|
|
|
#include "net/rime/collect.h"
|
2007-02-28 16:38:51 +00:00
|
|
|
|
2009-10-21 21:25:10 +00:00
|
|
|
#ifdef NEIGHBOR_CONF_MAX_NEIGHBORS
|
|
|
|
#define MAX_NEIGHBORS NEIGHBOR_CONF_MAX_NEIGHBORS
|
|
|
|
#else /* NEIGHBOR_CONF_MAX_NEIGHBORS */
|
2008-02-03 20:44:11 +00:00
|
|
|
#define MAX_NEIGHBORS 8
|
2009-10-21 21:25:10 +00:00
|
|
|
#endif /* NEIGHBOR_CONF_MAX_NEIGHBORS */
|
2007-02-28 16:38:51 +00:00
|
|
|
|
2007-12-09 15:44:21 +00:00
|
|
|
#define RTMETRIC_MAX COLLECT_MAX_DEPTH
|
2007-02-28 16:38:51 +00:00
|
|
|
|
2008-02-03 20:44:11 +00:00
|
|
|
MEMB(neighbors_mem, struct neighbor, MAX_NEIGHBORS);
|
|
|
|
LIST(neighbors_list);
|
|
|
|
|
|
|
|
/*static struct neighbor neighbors[MAX_NEIGHBORS];*/
|
2007-03-22 17:34:43 +00:00
|
|
|
|
|
|
|
static struct ctimer t;
|
|
|
|
|
2008-02-03 20:44:11 +00:00
|
|
|
static int max_time = 120;
|
|
|
|
|
|
|
|
#define DEBUG 0
|
|
|
|
#if DEBUG
|
|
|
|
#include <stdio.h>
|
|
|
|
#define PRINTF(...) printf(__VA_ARGS__)
|
|
|
|
#else
|
|
|
|
#define PRINTF(...)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2007-02-28 16:38:51 +00:00
|
|
|
/*---------------------------------------------------------------------------*/
|
2007-03-22 17:34:43 +00:00
|
|
|
static void
|
|
|
|
periodic(void *ptr)
|
2008-02-03 20:44:11 +00:00
|
|
|
{
|
|
|
|
struct neighbor *n, *next;
|
2007-11-13 20:39:29 +00:00
|
|
|
|
|
|
|
/* Go through all neighbors and remove old ones. */
|
2008-02-03 20:44:11 +00:00
|
|
|
for(n = list_head(neighbors_list); n != NULL; n = next) {
|
|
|
|
next = NULL;
|
|
|
|
/* for(i = 0; i < MAX_NEIGHBORS; ++i) {*/
|
|
|
|
if(!rimeaddr_cmp(&n->addr, &rimeaddr_null) &&
|
|
|
|
n->time < max_time) {
|
|
|
|
n->time++;
|
|
|
|
if(n->time == max_time) {
|
|
|
|
n->rtmetric = RTMETRIC_MAX;
|
|
|
|
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);
|
2007-02-28 16:38:51 +00:00
|
|
|
}
|
|
|
|
}
|
2008-02-03 20:44:11 +00:00
|
|
|
if(next == NULL) {
|
|
|
|
next = n->next;
|
|
|
|
}
|
2007-02-28 16:38:51 +00:00
|
|
|
}
|
2008-02-03 20:44:11 +00:00
|
|
|
/* PRINTF("neighbor periodic\n");*/
|
2007-03-22 17:34:43 +00:00
|
|
|
ctimer_set(&t, CLOCK_SECOND, periodic, NULL);
|
2007-02-28 16:38:51 +00:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
void
|
|
|
|
neighbor_init(void)
|
|
|
|
{
|
2010-02-03 20:38:33 +00:00
|
|
|
static uint8_t initialized = 0;
|
|
|
|
if(initialized == 0) {
|
|
|
|
initialized = 1;
|
|
|
|
memb_init(&neighbors_mem);
|
|
|
|
list_init(neighbors_list);
|
|
|
|
ctimer_set(&t, CLOCK_SECOND, periodic, NULL);
|
|
|
|
}
|
2007-02-28 16:38:51 +00:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
struct neighbor *
|
2009-11-08 19:40:16 +00:00
|
|
|
neighbor_find(const rimeaddr_t *addr)
|
2007-02-28 16:38:51 +00:00
|
|
|
{
|
2008-02-03 20:44:11 +00:00
|
|
|
struct neighbor *n;
|
|
|
|
for(n = list_head(neighbors_list); n != NULL; n = n->next) {
|
|
|
|
if(rimeaddr_cmp(&n->addr, addr)) {
|
|
|
|
return n;
|
2007-02-28 16:38:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
void
|
2008-02-24 22:05:27 +00:00
|
|
|
neighbor_update(struct neighbor *n, uint8_t rtmetric)
|
2007-11-13 20:39:29 +00:00
|
|
|
{
|
|
|
|
if(n != NULL) {
|
|
|
|
n->rtmetric = rtmetric;
|
|
|
|
n->time = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
void
|
2008-02-24 22:05:27 +00:00
|
|
|
neighbor_timedout_etx(struct neighbor *n, uint8_t etx)
|
2007-11-13 20:39:29 +00:00
|
|
|
{
|
|
|
|
if(n != NULL) {
|
2008-11-12 10:47:17 +00:00
|
|
|
n->etxs[n->etxptr] += etx;
|
2009-10-22 13:11:20 +00:00
|
|
|
if(n->etxs[n->etxptr] > RTMETRIC_MAX / NEIGHBOR_ETX_SCALE) {
|
|
|
|
n->etxs[n->etxptr] = RTMETRIC_MAX / NEIGHBOR_ETX_SCALE;
|
|
|
|
}
|
2007-11-13 20:39:29 +00:00
|
|
|
n->etxptr = (n->etxptr + 1) % NEIGHBOR_NUM_ETXS;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
void
|
2008-02-24 22:05:27 +00:00
|
|
|
neighbor_update_etx(struct neighbor *n, uint8_t etx)
|
2007-02-28 16:38:51 +00:00
|
|
|
{
|
|
|
|
if(n != NULL) {
|
2007-11-13 20:39:29 +00:00
|
|
|
n->etxs[n->etxptr] = etx;
|
|
|
|
n->etxptr = (n->etxptr + 1) % NEIGHBOR_NUM_ETXS;
|
2007-02-28 16:38:51 +00:00
|
|
|
n->time = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2007-11-13 20:39:29 +00:00
|
|
|
uint8_t
|
|
|
|
neighbor_etx(struct neighbor *n)
|
|
|
|
{
|
|
|
|
int i, etx;
|
|
|
|
|
|
|
|
etx = 0;
|
|
|
|
for(i = 0; i < NEIGHBOR_NUM_ETXS; ++i) {
|
|
|
|
etx += n->etxs[i];
|
|
|
|
}
|
|
|
|
return NEIGHBOR_ETX_SCALE * etx / NEIGHBOR_NUM_ETXS;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2007-02-28 16:38:51 +00:00
|
|
|
void
|
2009-11-08 19:40:16 +00:00
|
|
|
neighbor_add(const rimeaddr_t *addr, uint8_t nrtmetric, uint8_t netx)
|
2007-02-28 16:38:51 +00:00
|
|
|
{
|
2008-02-03 20:44:11 +00:00
|
|
|
uint16_t rtmetric;
|
|
|
|
uint16_t etx;
|
|
|
|
struct neighbor *n, *max;
|
|
|
|
int i;
|
2007-02-28 16:38:51 +00:00
|
|
|
|
2008-02-03 20:44:11 +00:00
|
|
|
PRINTF("neighbor_add: adding %d.%d\n", addr->u8[0], addr->u8[1]);
|
|
|
|
|
|
|
|
/* Check if the neighbor is already on the list. */
|
|
|
|
for(n = list_head(neighbors_list); n != NULL; n = n->next) {
|
|
|
|
if(rimeaddr_cmp(&n->addr, &rimeaddr_null) ||
|
|
|
|
rimeaddr_cmp(&n->addr, addr)) {
|
|
|
|
PRINTF("neighbor_add: already on list %d.%d\n", addr->u8[0], addr->u8[1]);
|
2007-02-28 16:38:51 +00:00
|
|
|
break;
|
|
|
|
}
|
2008-02-03 20:44:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* If the neighbor was not on the list, we try to allocate memory
|
|
|
|
for it. */
|
|
|
|
if(n == NULL) {
|
|
|
|
PRINTF("neighbor_add: not on list, allocating %d.%d\n", addr->u8[0], addr->u8[1]);
|
|
|
|
n = memb_alloc(&neighbors_mem);
|
|
|
|
if(n != NULL) {
|
|
|
|
list_add(neighbors_list, 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",
|
2007-11-13 20:39:29 +00:00
|
|
|
node_id, neighbors[n].nodeid, rtmetric, signal);*/
|
2008-02-03 20:44:11 +00:00
|
|
|
}
|
2007-02-28 16:38:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-02-03 20:44:11 +00:00
|
|
|
n = max;
|
2007-02-28 16:38:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-02-03 20:44:11 +00:00
|
|
|
/* PRINTF("%d: adding neighbor %d with rtmetric %d, signal %d at %d\n",
|
2007-11-13 20:39:29 +00:00
|
|
|
node_id, neighbors[n].nodeid, rtmetric, signal, n);*/
|
2008-02-03 20:44:11 +00:00
|
|
|
if(n != NULL) {
|
|
|
|
n->time = 0;
|
|
|
|
rimeaddr_copy(&n->addr, addr);
|
|
|
|
n->rtmetric = nrtmetric;
|
|
|
|
for(i = 0; i < NEIGHBOR_NUM_ETXS; ++i) {
|
|
|
|
n->etxs[i] = netx;
|
|
|
|
}
|
|
|
|
n->etxptr = 0;
|
2007-11-13 20:39:29 +00:00
|
|
|
}
|
2007-02-28 16:38:51 +00:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
void
|
2009-11-08 19:40:16 +00:00
|
|
|
neighbor_remove(const rimeaddr_t *addr)
|
2007-02-28 16:38:51 +00:00
|
|
|
{
|
2008-02-03 20:44:11 +00:00
|
|
|
struct neighbor *n;
|
|
|
|
|
|
|
|
for(n = list_head(neighbors_list); n != NULL; n = n->next) {
|
|
|
|
if(rimeaddr_cmp(&n->addr, addr)) {
|
2009-03-21 20:53:06 +00:00
|
|
|
PRINTF("%d.%d: removing %d.%d\n",
|
|
|
|
rimeaddr_node_addr.u8[0], rimeaddr_node_addr.u8[1],
|
|
|
|
addr->u8[0], addr->u8[1]);
|
2008-02-03 20:44:11 +00:00
|
|
|
rimeaddr_copy(&n->addr, &rimeaddr_null);
|
|
|
|
n->rtmetric = RTMETRIC_MAX;
|
|
|
|
list_remove(neighbors_list, n);
|
|
|
|
memb_free(&neighbors_mem, n);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* int i;
|
2007-02-28 16:38:51 +00:00
|
|
|
|
|
|
|
for(i = 0; i < MAX_NEIGHBORS; ++i) {
|
2007-03-15 19:43:07 +00:00
|
|
|
if(rimeaddr_cmp(&neighbors[i].addr, addr)) {
|
2008-02-03 20:44:11 +00:00
|
|
|
PRINTF("%d: removing %d @ %d\n", rimeaddr_node_addr.u16[0], addr->u16[0], i);
|
2007-03-15 19:43:07 +00:00
|
|
|
rimeaddr_copy(&neighbors[i].addr, &rimeaddr_null);
|
2007-11-13 20:39:29 +00:00
|
|
|
neighbors[i].rtmetric = RTMETRIC_MAX;
|
2007-02-28 16:38:51 +00:00
|
|
|
return;
|
|
|
|
}
|
2008-02-03 20:44:11 +00:00
|
|
|
}*/
|
2007-02-28 16:38:51 +00:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
struct neighbor *
|
|
|
|
neighbor_best(void)
|
|
|
|
{
|
2008-02-03 20:44:11 +00:00
|
|
|
int found;
|
|
|
|
/* int lowest, best;*/
|
2008-11-12 10:47:17 +00:00
|
|
|
struct neighbor *n, *best;
|
2009-04-06 21:19:00 +00:00
|
|
|
uint16_t rtmetric;
|
2007-02-28 16:38:51 +00:00
|
|
|
|
2007-11-13 20:39:29 +00:00
|
|
|
rtmetric = RTMETRIC_MAX;
|
2008-11-12 10:47:17 +00:00
|
|
|
best = NULL;
|
2007-02-28 16:38:51 +00:00
|
|
|
found = 0;
|
|
|
|
|
2008-02-03 20:44:11 +00:00
|
|
|
/* PRINTF("%d: ", node_id);*/
|
2007-02-28 16:38:51 +00:00
|
|
|
|
2007-11-13 20:39:29 +00:00
|
|
|
/* Find the lowest rtmetric. */
|
2008-02-03 20:44:11 +00:00
|
|
|
for(n = list_head(neighbors_list); n != NULL; n = n->next) {
|
|
|
|
if(!rimeaddr_cmp(&n->addr, &rimeaddr_null) &&
|
2008-11-12 10:47:17 +00:00
|
|
|
rtmetric > n->rtmetric + neighbor_etx(n)) {
|
|
|
|
rtmetric = n->rtmetric + neighbor_etx(n);
|
|
|
|
best = n;
|
2007-02-28 16:38:51 +00:00
|
|
|
}
|
|
|
|
}
|
2008-02-03 20:44:11 +00:00
|
|
|
|
2008-11-12 10:47:17 +00:00
|
|
|
return best;
|
2007-02-28 16:38:51 +00:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2007-03-25 12:06:28 +00:00
|
|
|
void
|
|
|
|
neighbor_set_lifetime(int seconds)
|
|
|
|
{
|
|
|
|
max_time = seconds;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2007-11-26 23:30:13 +00:00
|
|
|
int
|
|
|
|
neighbor_num(void)
|
|
|
|
{
|
2008-02-03 20:44:11 +00:00
|
|
|
PRINTF("neighbor_num %d\n", list_length(neighbors_list));
|
|
|
|
return list_length(neighbors_list);
|
2007-11-26 23:30:13 +00:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
struct neighbor *
|
|
|
|
neighbor_get(int num)
|
|
|
|
{
|
2008-02-03 20:44:11 +00:00
|
|
|
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;
|
2007-11-26 23:30:13 +00:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2007-03-31 18:31:27 +00:00
|
|
|
/** @} */
|