2010-03-12 14:19:45 +01:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2010, 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-12-15 15:35:07 +01:00
|
|
|
* $Id: neighbor-info.c,v 1.18 2010/12/15 14:35:07 nvt-se Exp $
|
2010-03-12 14:19:45 +01:00
|
|
|
*/
|
|
|
|
/**
|
|
|
|
* \file
|
|
|
|
* A generic module for management of neighbor information.
|
|
|
|
*
|
|
|
|
* \author Nicolas Tsiftes <nvt@sics.se>
|
|
|
|
*/
|
|
|
|
|
2010-03-12 14:23:50 +01:00
|
|
|
#include "net/neighbor-info.h"
|
|
|
|
#include "net/neighbor-attr.h"
|
2011-07-11 14:19:14 +02:00
|
|
|
#include "net/uip-ds6.h"
|
|
|
|
#include "net/uip-nd6.h"
|
2010-03-12 14:19:45 +01:00
|
|
|
|
2010-12-15 15:35:07 +01:00
|
|
|
#define DEBUG DEBUG_NONE
|
|
|
|
#include "net/uip-debug.h"
|
2010-03-12 14:19:45 +01:00
|
|
|
|
2010-10-21 21:46:21 +02:00
|
|
|
#define ETX_LIMIT 15
|
2010-03-12 14:19:45 +01:00
|
|
|
#define ETX_SCALE 100
|
2010-10-28 10:54:54 +02:00
|
|
|
#define ETX_ALPHA 90
|
2011-03-16 13:26:52 +01:00
|
|
|
#define ETX_NOACK_PENALTY ETX_LIMIT
|
2010-03-12 14:19:45 +01:00
|
|
|
/*---------------------------------------------------------------------------*/
|
2012-01-10 16:04:56 +01:00
|
|
|
NEIGHBOR_ATTRIBUTE_GLOBAL(link_metric_t, attr_etx, NULL);
|
|
|
|
NEIGHBOR_ATTRIBUTE_GLOBAL(unsigned long, attr_timestamp, NULL);
|
2010-03-12 14:19:45 +01:00
|
|
|
|
|
|
|
static neighbor_info_subscriber_t subscriber_callback;
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static void
|
2011-03-17 20:29:38 +01:00
|
|
|
update_metric(const rimeaddr_t *dest, int packet_metric)
|
2010-03-12 14:19:45 +01:00
|
|
|
{
|
2011-03-17 20:29:38 +01:00
|
|
|
link_metric_t *metricp;
|
|
|
|
link_metric_t recorded_metric, new_metric;
|
2012-01-10 16:04:56 +01:00
|
|
|
unsigned long time;
|
2012-10-05 17:24:23 +02:00
|
|
|
int first_update = 0;
|
2011-03-17 20:29:38 +01:00
|
|
|
|
2012-01-10 16:04:56 +01:00
|
|
|
metricp = (link_metric_t *)neighbor_attr_get_data(&attr_etx, dest);
|
2011-03-17 20:29:38 +01:00
|
|
|
packet_metric = NEIGHBOR_INFO_ETX2FIX(packet_metric);
|
|
|
|
if(metricp == NULL || *metricp == 0) {
|
|
|
|
recorded_metric = NEIGHBOR_INFO_ETX2FIX(ETX_LIMIT);
|
|
|
|
new_metric = packet_metric;
|
2012-10-05 17:24:23 +02:00
|
|
|
first_update = 1;
|
2010-03-12 14:19:45 +01:00
|
|
|
} else {
|
2011-03-17 20:29:38 +01:00
|
|
|
recorded_metric = *metricp;
|
2011-03-16 13:26:52 +01:00
|
|
|
/* Update the EWMA of the ETX for the neighbor. */
|
2011-03-17 20:29:38 +01:00
|
|
|
new_metric = ((uint16_t)recorded_metric * ETX_ALPHA +
|
|
|
|
(uint16_t)packet_metric * (ETX_SCALE - ETX_ALPHA)) / ETX_SCALE;
|
2010-03-12 14:19:45 +01:00
|
|
|
}
|
|
|
|
|
2010-10-28 10:54:54 +02:00
|
|
|
PRINTF("neighbor-info: ETX changed from %d to %d (packet ETX = %d) %d\n",
|
2011-03-17 20:29:38 +01:00
|
|
|
NEIGHBOR_INFO_FIX2ETX(recorded_metric),
|
|
|
|
NEIGHBOR_INFO_FIX2ETX(new_metric),
|
|
|
|
NEIGHBOR_INFO_FIX2ETX(packet_metric),
|
2010-10-28 10:54:54 +02:00
|
|
|
dest->u8[7]);
|
2010-03-12 14:19:45 +01:00
|
|
|
|
2010-05-24 18:38:17 +02:00
|
|
|
if(neighbor_attr_has_neighbor(dest)) {
|
2012-01-10 16:04:56 +01:00
|
|
|
time = clock_seconds();
|
|
|
|
neighbor_attr_set_data(&attr_etx, dest, &new_metric);
|
|
|
|
neighbor_attr_set_data(&attr_timestamp, dest, &time);
|
2012-10-05 17:24:23 +02:00
|
|
|
if((first_update || new_metric != recorded_metric) && subscriber_callback != NULL) {
|
2011-03-17 20:29:38 +01:00
|
|
|
subscriber_callback(dest, 1, new_metric);
|
2010-03-17 16:08:46 +01:00
|
|
|
}
|
2010-03-12 14:19:45 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static void
|
|
|
|
add_neighbor(const rimeaddr_t *addr)
|
|
|
|
{
|
|
|
|
switch(neighbor_attr_add_neighbor(addr)) {
|
|
|
|
case -1:
|
|
|
|
PRINTF("neighbor-info: failed to add a node.\n");
|
|
|
|
break;
|
|
|
|
case 0:
|
|
|
|
PRINTF("neighbor-info: The neighbor is already known\n");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
void
|
|
|
|
neighbor_info_packet_sent(int status, int numtx)
|
|
|
|
{
|
|
|
|
const rimeaddr_t *dest;
|
2011-03-17 20:29:38 +01:00
|
|
|
link_metric_t packet_metric;
|
2011-07-11 14:19:14 +02:00
|
|
|
#if UIP_DS6_LL_NUD
|
2011-12-08 15:38:35 +01:00
|
|
|
uip_ds6_nbr_t *nbr;
|
2011-07-11 14:19:14 +02:00
|
|
|
#endif /* UIP_DS6_LL_NUD */
|
2010-03-12 14:19:45 +01:00
|
|
|
|
|
|
|
dest = packetbuf_addr(PACKETBUF_ADDR_RECEIVER);
|
|
|
|
if(rimeaddr_cmp(dest, &rimeaddr_null)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-10-10 22:10:21 +02:00
|
|
|
packet_metric = numtx;
|
|
|
|
|
|
|
|
PRINTF("neighbor-info: packet sent to %d.%d, status=%d, metric=%u\n",
|
2010-03-12 14:19:45 +01:00
|
|
|
dest->u8[sizeof(*dest) - 2], dest->u8[sizeof(*dest) - 1],
|
2011-10-10 22:10:21 +02:00
|
|
|
status, (unsigned)packet_metric);
|
2010-03-12 14:19:45 +01:00
|
|
|
|
|
|
|
switch(status) {
|
|
|
|
case MAC_TX_OK:
|
2010-03-17 16:08:46 +01:00
|
|
|
add_neighbor(dest);
|
2011-07-11 14:19:14 +02:00
|
|
|
#if UIP_DS6_LL_NUD
|
2011-12-08 15:38:35 +01:00
|
|
|
nbr = uip_ds6_nbr_ll_lookup((uip_lladdr_t *)dest);
|
|
|
|
if(nbr != NULL &&
|
|
|
|
(nbr->state == STALE || nbr->state == DELAY || nbr->state == PROBE)) {
|
2011-07-11 14:19:14 +02:00
|
|
|
nbr->state = REACHABLE;
|
|
|
|
stimer_set(&nbr->reachable, UIP_ND6_REACHABLE_TIME / 1000);
|
2011-12-08 15:38:35 +01:00
|
|
|
PRINTF("neighbor-info : received a link layer ACK : ");
|
2011-07-11 14:19:14 +02:00
|
|
|
PRINTLLADDR((uip_lladdr_t *)dest);
|
|
|
|
PRINTF(" is reachable.\n");
|
|
|
|
}
|
|
|
|
#endif /* UIP_DS6_LL_NUD */
|
2010-03-12 14:19:45 +01:00
|
|
|
break;
|
|
|
|
case MAC_TX_NOACK:
|
2011-03-17 20:29:38 +01:00
|
|
|
packet_metric = ETX_NOACK_PENALTY;
|
2010-10-28 10:54:54 +02:00
|
|
|
break;
|
2010-03-12 14:19:45 +01:00
|
|
|
default:
|
2011-03-16 13:26:52 +01:00
|
|
|
/* Do not penalize the ETX when collisions or transmission
|
|
|
|
errors occur. */
|
|
|
|
return;
|
2010-03-12 14:19:45 +01:00
|
|
|
}
|
|
|
|
|
2011-03-17 20:29:38 +01:00
|
|
|
update_metric(dest, packet_metric);
|
2010-03-12 14:19:45 +01:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
void
|
|
|
|
neighbor_info_packet_received(void)
|
|
|
|
{
|
|
|
|
const rimeaddr_t *src;
|
|
|
|
|
|
|
|
src = packetbuf_addr(PACKETBUF_ADDR_SENDER);
|
|
|
|
if(rimeaddr_cmp(src, &rimeaddr_null)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
PRINTF("neighbor-info: packet received from %d.%d\n",
|
|
|
|
src->u8[sizeof(*src) - 2], src->u8[sizeof(*src) - 1]);
|
|
|
|
|
|
|
|
add_neighbor(src);
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
int
|
|
|
|
neighbor_info_subscribe(neighbor_info_subscriber_t s)
|
|
|
|
{
|
|
|
|
if(subscriber_callback == NULL) {
|
2012-01-10 16:04:56 +01:00
|
|
|
neighbor_attr_register(&attr_etx);
|
|
|
|
neighbor_attr_register(&attr_timestamp);
|
2010-03-12 14:19:45 +01:00
|
|
|
subscriber_callback = s;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2011-03-17 20:29:38 +01:00
|
|
|
link_metric_t
|
|
|
|
neighbor_info_get_metric(const rimeaddr_t *addr)
|
2010-10-22 13:34:24 +02:00
|
|
|
{
|
2011-03-17 20:29:38 +01:00
|
|
|
link_metric_t *metricp;
|
2010-10-22 15:50:13 +02:00
|
|
|
|
2012-01-10 16:04:56 +01:00
|
|
|
metricp = (link_metric_t *)neighbor_attr_get_data(&attr_etx, addr);
|
2011-03-17 20:29:38 +01:00
|
|
|
return metricp == NULL ? ETX_LIMIT : *metricp;
|
2010-10-22 13:34:24 +02:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|