2007-03-31 20:31:27 +02:00
|
|
|
/**
|
|
|
|
* \addtogroup rimeneighbor
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
2007-02-28 17:38:51 +01: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.
|
|
|
|
*
|
2007-11-17 11:32:54 +01:00
|
|
|
* $Id: neighbor.c,v 1.11 2007/11/17 10:32:54 adamdunkels Exp $
|
2007-02-28 17:38:51 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \file
|
|
|
|
* Radio neighborhood management
|
|
|
|
* \author
|
|
|
|
* Adam Dunkels <adam@sics.se>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <limits.h>
|
2007-03-24 14:57:04 +01:00
|
|
|
#include <stdio.h>
|
2007-02-28 17:38:51 +01:00
|
|
|
|
|
|
|
#include "contiki.h"
|
|
|
|
#include "net/rime/neighbor.h"
|
2007-03-22 18:34:43 +01:00
|
|
|
#include "net/rime/ctimer.h"
|
2007-02-28 17:38:51 +01:00
|
|
|
|
|
|
|
#define MAX_NEIGHBORS 5
|
|
|
|
|
2007-11-13 21:39:29 +01:00
|
|
|
#define RTMETRIC_MAX 64
|
2007-02-28 17:38:51 +01:00
|
|
|
|
|
|
|
static struct neighbor neighbors[MAX_NEIGHBORS];
|
2007-03-22 18:34:43 +01:00
|
|
|
|
|
|
|
static struct ctimer t;
|
|
|
|
|
2007-05-22 23:15:17 +02:00
|
|
|
static int max_time = 30;
|
2007-02-28 17:38:51 +01:00
|
|
|
/*---------------------------------------------------------------------------*/
|
2007-03-22 18:34:43 +01:00
|
|
|
static void
|
|
|
|
periodic(void *ptr)
|
2007-02-28 17:38:51 +01:00
|
|
|
{
|
|
|
|
int i;
|
2007-11-13 21:39:29 +01:00
|
|
|
|
|
|
|
/* Go through all neighbors and remove old ones. */
|
2007-02-28 17:38:51 +01:00
|
|
|
|
|
|
|
for(i = 0; i < MAX_NEIGHBORS; ++i) {
|
2007-03-15 20:43:07 +01:00
|
|
|
if(!rimeaddr_cmp(&neighbors[i].addr, &rimeaddr_null) &&
|
2007-02-28 17:38:51 +01:00
|
|
|
neighbors[i].time < max_time) {
|
|
|
|
neighbors[i].time++;
|
|
|
|
if(neighbors[i].time == max_time) {
|
2007-11-13 21:39:29 +01:00
|
|
|
neighbors[i].rtmetric = RTMETRIC_MAX;
|
2007-02-28 17:38:51 +01:00
|
|
|
/* printf("%d: removing old neighbor %d\n", node_id, neighbors[i].nodeid);*/
|
2007-03-15 20:43:07 +01:00
|
|
|
rimeaddr_copy(&neighbors[i].addr, &rimeaddr_null);
|
2007-02-28 17:38:51 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-05-22 23:15:17 +02:00
|
|
|
/* printf("neighbor periodic\n");*/
|
2007-03-22 18:34:43 +01:00
|
|
|
ctimer_set(&t, CLOCK_SECOND, periodic, NULL);
|
2007-02-28 17:38:51 +01:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
void
|
|
|
|
neighbor_init(void)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for(i = 0; i < MAX_NEIGHBORS; ++i) {
|
2007-03-15 20:43:07 +01:00
|
|
|
rimeaddr_copy(&neighbors[i].addr, &rimeaddr_null);
|
2007-02-28 17:38:51 +01:00
|
|
|
}
|
2007-03-22 18:34:43 +01:00
|
|
|
|
2007-03-23 00:57:08 +01:00
|
|
|
ctimer_set(&t, CLOCK_SECOND, periodic, NULL);
|
2007-02-28 17:38:51 +01:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
struct neighbor *
|
2007-03-15 20:43:07 +01:00
|
|
|
neighbor_find(rimeaddr_t *addr)
|
2007-02-28 17:38:51 +01:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for(i = 0; i < MAX_NEIGHBORS; ++i) {
|
2007-03-15 20:43:07 +01:00
|
|
|
if(rimeaddr_cmp(&neighbors[i].addr, addr)) {
|
2007-02-28 17:38:51 +01:00
|
|
|
return &neighbors[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
void
|
2007-11-17 11:32:54 +01:00
|
|
|
neighbor_update(struct neighbor *n, u8_t rtmetric)
|
2007-11-13 21:39:29 +01:00
|
|
|
{
|
|
|
|
if(n != NULL) {
|
|
|
|
n->rtmetric = rtmetric;
|
|
|
|
n->time = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
void
|
|
|
|
neighbor_timedout_etx(struct neighbor *n, u8_t etx)
|
|
|
|
{
|
|
|
|
if(n != NULL) {
|
|
|
|
n->etxs[n->etxptr] = etx;
|
|
|
|
n->etxptr = (n->etxptr + 1) % NEIGHBOR_NUM_ETXS;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
void
|
|
|
|
neighbor_update_etx(struct neighbor *n, u8_t etx)
|
2007-02-28 17:38:51 +01:00
|
|
|
{
|
|
|
|
if(n != NULL) {
|
2007-11-13 21:39:29 +01:00
|
|
|
n->etxs[n->etxptr] = etx;
|
|
|
|
n->etxptr = (n->etxptr + 1) % NEIGHBOR_NUM_ETXS;
|
2007-02-28 17:38:51 +01:00
|
|
|
n->time = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2007-11-13 21:39:29 +01: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 17:38:51 +01:00
|
|
|
void
|
2007-11-13 21:39:29 +01:00
|
|
|
neighbor_add(rimeaddr_t *addr, u8_t nrtmetric, u8_t netx)
|
2007-02-28 17:38:51 +01:00
|
|
|
{
|
|
|
|
int i, n;
|
2007-11-13 21:39:29 +01:00
|
|
|
u8_t rtmetric;
|
|
|
|
u8_t etx;
|
2007-02-28 17:38:51 +01:00
|
|
|
|
|
|
|
/* Find the first unused entry or the used entry with the highest
|
2007-11-13 21:39:29 +01:00
|
|
|
rtmetric and highest etx. */
|
|
|
|
rtmetric = 0;
|
|
|
|
etx = 0;
|
2007-02-28 17:38:51 +01:00
|
|
|
|
2007-03-15 10:56:15 +01:00
|
|
|
n = 0;
|
2007-02-28 17:38:51 +01:00
|
|
|
for(i = 0; i < MAX_NEIGHBORS; ++i) {
|
2007-03-15 20:43:07 +01:00
|
|
|
if(rimeaddr_cmp(&neighbors[i].addr, &rimeaddr_null) ||
|
|
|
|
rimeaddr_cmp(&neighbors[i].addr, addr)) {
|
2007-02-28 17:38:51 +01:00
|
|
|
n = i;
|
|
|
|
break;
|
|
|
|
}
|
2007-03-15 20:43:07 +01:00
|
|
|
if(!rimeaddr_cmp(&neighbors[i].addr, &rimeaddr_null)) {
|
2007-11-13 21:39:29 +01:00
|
|
|
if(neighbors[i].rtmetric > rtmetric) {
|
|
|
|
rtmetric = neighbors[i].rtmetric;
|
|
|
|
etx = neighbor_etx(&neighbors[i]);
|
2007-02-28 17:38:51 +01:00
|
|
|
n = i;
|
2007-11-13 21:39:29 +01:00
|
|
|
} else if(neighbors[i].rtmetric == rtmetric) {
|
|
|
|
if(neighbor_etx(&neighbors[i]) > etx) {
|
|
|
|
rtmetric = neighbors[i].rtmetric;
|
|
|
|
etx = neighbor_etx(&neighbors[i]);
|
2007-02-28 17:38:51 +01:00
|
|
|
n = i;
|
2007-11-13 21:39:29 +01:00
|
|
|
/* printf("%d: found worst neighbor %d with rtmetric %d, signal %d\n",
|
|
|
|
node_id, neighbors[n].nodeid, rtmetric, signal);*/
|
2007-02-28 17:38:51 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-11-13 21:39:29 +01:00
|
|
|
/* printf("%d: adding neighbor %d with rtmetric %d, signal %d at %d\n",
|
|
|
|
node_id, neighbors[n].nodeid, rtmetric, signal, n);*/
|
2007-02-28 17:38:51 +01:00
|
|
|
|
|
|
|
neighbors[n].time = 0;
|
2007-03-15 20:43:07 +01:00
|
|
|
rimeaddr_copy(&neighbors[i].addr, addr);
|
2007-11-13 21:39:29 +01:00
|
|
|
neighbors[n].rtmetric = nrtmetric;
|
|
|
|
for(i = 0; i < NEIGHBOR_NUM_ETXS; ++i) {
|
|
|
|
neighbors[n].etxs[i] = netx;
|
|
|
|
}
|
|
|
|
neighbors[n].etxptr = 0;
|
2007-02-28 17:38:51 +01:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
void
|
2007-03-15 20:43:07 +01:00
|
|
|
neighbor_remove(rimeaddr_t *addr)
|
2007-02-28 17:38:51 +01:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for(i = 0; i < MAX_NEIGHBORS; ++i) {
|
2007-03-15 20:43:07 +01:00
|
|
|
if(rimeaddr_cmp(&neighbors[i].addr, addr)) {
|
2007-03-22 18:34:43 +01:00
|
|
|
printf("%d: removing %d @ %d\n", rimeaddr_node_addr.u16[0], addr->u16[0], i);
|
2007-03-15 20:43:07 +01:00
|
|
|
rimeaddr_copy(&neighbors[i].addr, &rimeaddr_null);
|
2007-11-13 21:39:29 +01:00
|
|
|
neighbors[i].rtmetric = RTMETRIC_MAX;
|
2007-02-28 17:38:51 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
struct neighbor *
|
|
|
|
neighbor_best(void)
|
|
|
|
{
|
|
|
|
int i, found;
|
|
|
|
int lowest, best;
|
2007-11-13 21:39:29 +01:00
|
|
|
u8_t rtmetric;
|
|
|
|
u8_t etx;
|
2007-02-28 17:38:51 +01:00
|
|
|
|
2007-11-13 21:39:29 +01:00
|
|
|
rtmetric = RTMETRIC_MAX;
|
2007-02-28 17:38:51 +01:00
|
|
|
lowest = 0;
|
|
|
|
found = 0;
|
|
|
|
|
|
|
|
/* printf("%d: ", node_id);*/
|
|
|
|
|
2007-11-13 21:39:29 +01:00
|
|
|
/* Find the lowest rtmetric. */
|
2007-02-28 17:38:51 +01:00
|
|
|
for(i = 0; i < MAX_NEIGHBORS; ++i) {
|
2007-11-13 21:39:29 +01:00
|
|
|
/* printf("%d:%d ", neighbors[i].nodeid, neighbors[i].rtmetric);*/
|
2007-03-15 20:43:07 +01:00
|
|
|
if(!rimeaddr_cmp(&neighbors[i].addr, &rimeaddr_null) &&
|
2007-11-13 21:39:29 +01:00
|
|
|
rtmetric > neighbors[i].rtmetric) {
|
|
|
|
rtmetric = neighbors[i].rtmetric;
|
2007-02-28 17:38:51 +01:00
|
|
|
lowest = i;
|
|
|
|
found = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* printf("\n");*/
|
|
|
|
|
2007-11-13 21:39:29 +01:00
|
|
|
/* Find the neighbor with lowest etx of the ones that
|
|
|
|
have the lowest rtmetric. */
|
2007-02-28 17:38:51 +01:00
|
|
|
if(found) {
|
2007-11-13 21:39:29 +01:00
|
|
|
etx = 0;
|
2007-02-28 17:38:51 +01:00
|
|
|
best = lowest;
|
|
|
|
for(i = 0; i < MAX_NEIGHBORS; ++i) {
|
2007-03-15 20:43:07 +01:00
|
|
|
if(!rimeaddr_cmp(&neighbors[i].addr, &rimeaddr_null) &&
|
2007-11-13 21:39:29 +01:00
|
|
|
rtmetric == neighbors[i].rtmetric &&
|
|
|
|
neighbor_etx(&neighbors[i]) < etx) {
|
|
|
|
etx = neighbor_etx(&neighbors[i]);
|
2007-02-28 17:38:51 +01:00
|
|
|
best = i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return &neighbors[best];
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2007-03-25 14:06:28 +02:00
|
|
|
void
|
|
|
|
neighbor_set_lifetime(int seconds)
|
|
|
|
{
|
|
|
|
max_time = seconds;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2007-03-31 20:31:27 +02:00
|
|
|
/** @} */
|