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-03-24 14:57:04 +01:00
|
|
|
* $Id: neighbor.c,v 1.6 2007/03/24 13:57:04 oliverschmidt 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
|
|
|
|
|
|
|
|
#define HOPCOUNT_MAX 32
|
|
|
|
|
|
|
|
static struct neighbor neighbors[MAX_NEIGHBORS];
|
2007-03-22 18:34:43 +01:00
|
|
|
|
|
|
|
static struct ctimer t;
|
|
|
|
|
|
|
|
static int max_time = 20;
|
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;
|
|
|
|
|
|
|
|
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) {
|
|
|
|
neighbors[i].hopcount = HOPCOUNT_MAX;
|
|
|
|
/* 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-03-22 18:34:43 +01:00
|
|
|
printf("neighbor periodic\n");
|
|
|
|
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
|
|
|
|
neighbor_update(struct neighbor *n, u8_t hopcount, u16_t signal)
|
|
|
|
{
|
|
|
|
if(n != NULL) {
|
|
|
|
n->hopcount = hopcount;
|
|
|
|
n->signal = signal;
|
|
|
|
n->time = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
void
|
2007-03-15 20:43:07 +01:00
|
|
|
neighbor_add(rimeaddr_t *addr, u8_t nhopcount, u16_t nsignal)
|
2007-02-28 17:38:51 +01:00
|
|
|
{
|
|
|
|
int i, n;
|
|
|
|
u8_t hopcount;
|
|
|
|
u16_t signal;
|
|
|
|
|
|
|
|
/* Find the first unused entry or the used entry with the highest
|
|
|
|
hopcount and lowest signal strength. */
|
|
|
|
hopcount = 0;
|
|
|
|
signal = USHRT_MAX;
|
|
|
|
|
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-02-28 17:38:51 +01:00
|
|
|
if(neighbors[i].hopcount > hopcount) {
|
|
|
|
hopcount = neighbors[i].hopcount;
|
|
|
|
signal = neighbors[i].signal;
|
|
|
|
n = i;
|
|
|
|
} else if(neighbors[i].hopcount == hopcount) {
|
|
|
|
if(neighbors[i].signal < signal) {
|
|
|
|
hopcount = neighbors[i].hopcount;
|
|
|
|
signal = neighbors[i].signal;
|
|
|
|
n = i;
|
|
|
|
/* printf("%d: found worst neighbor %d with hopcount %d, signal %d\n",
|
|
|
|
node_id, neighbors[n].nodeid, hopcount, signal);*/
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* printf("%d: adding neighbor %d with hopcount %d, signal %d at %d\n",
|
|
|
|
node_id, neighbors[n].nodeid, hopcount, signal, n);*/
|
|
|
|
|
|
|
|
neighbors[n].time = 0;
|
2007-03-15 20:43:07 +01:00
|
|
|
rimeaddr_copy(&neighbors[i].addr, addr);
|
2007-02-28 17:38:51 +01:00
|
|
|
neighbors[n].hopcount = nhopcount;
|
|
|
|
neighbors[n].signal = nsignal;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
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-02-28 17:38:51 +01:00
|
|
|
neighbors[i].hopcount = HOPCOUNT_MAX;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
struct neighbor *
|
|
|
|
neighbor_best(void)
|
|
|
|
{
|
|
|
|
int i, found;
|
|
|
|
int lowest, best;
|
|
|
|
u8_t hopcount;
|
|
|
|
u16_t signal;
|
|
|
|
|
|
|
|
hopcount = HOPCOUNT_MAX;
|
|
|
|
lowest = 0;
|
|
|
|
found = 0;
|
|
|
|
|
|
|
|
/* printf("%d: ", node_id);*/
|
|
|
|
|
|
|
|
/* Find the lowest hopcount. */
|
|
|
|
for(i = 0; i < MAX_NEIGHBORS; ++i) {
|
|
|
|
/* printf("%d:%d ", neighbors[i].nodeid, neighbors[i].hopcount);*/
|
2007-03-15 20:43:07 +01:00
|
|
|
if(!rimeaddr_cmp(&neighbors[i].addr, &rimeaddr_null) &&
|
2007-02-28 17:38:51 +01:00
|
|
|
hopcount > neighbors[i].hopcount) {
|
|
|
|
hopcount = neighbors[i].hopcount;
|
|
|
|
lowest = i;
|
|
|
|
found = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* printf("\n");*/
|
|
|
|
|
|
|
|
/* Find the neighbor with highest signal strength of the ones that
|
|
|
|
have the lowest hopcount. */
|
|
|
|
if(found) {
|
|
|
|
signal = 0;
|
|
|
|
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-02-28 17:38:51 +01:00
|
|
|
hopcount == neighbors[i].hopcount &&
|
|
|
|
neighbors[i].signal > signal) {
|
|
|
|
signal = neighbors[i].signal;
|
|
|
|
best = i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return &neighbors[best];
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|