2010-03-12 13:19:45 +00: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 14:35:07 +00:00
|
|
|
* $Id: neighbor-info.c,v 1.18 2010/12/15 14:35:07 nvt-se Exp $
|
2010-03-12 13:19:45 +00:00
|
|
|
*/
|
|
|
|
/**
|
|
|
|
* \file
|
|
|
|
* A generic module for management of neighbor information.
|
|
|
|
*
|
|
|
|
* \author Nicolas Tsiftes <nvt@sics.se>
|
|
|
|
*/
|
|
|
|
|
2010-03-12 13:23:50 +00:00
|
|
|
#include "net/neighbor-info.h"
|
|
|
|
#include "net/neighbor-attr.h"
|
2010-03-12 13:19:45 +00:00
|
|
|
|
2010-12-15 14:35:07 +00:00
|
|
|
#define DEBUG DEBUG_NONE
|
|
|
|
#include "net/uip-debug.h"
|
2010-03-12 13:19:45 +00:00
|
|
|
|
2010-10-21 19:46:21 +00:00
|
|
|
#define ETX_LIMIT 15
|
2010-03-12 13:19:45 +00:00
|
|
|
#define ETX_SCALE 100
|
2010-10-28 08:54:54 +00:00
|
|
|
#define ETX_ALPHA 90
|
|
|
|
#define ETX_FIRST_GUESS 5
|
|
|
|
|
|
|
|
#define NOACK_PACKET_ETX 8
|
2010-03-12 13:19:45 +00:00
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
NEIGHBOR_ATTRIBUTE(uint8_t, etx, NULL);
|
|
|
|
|
|
|
|
static neighbor_info_subscriber_t subscriber_callback;
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static void
|
|
|
|
update_etx(const rimeaddr_t *dest, int packet_etx)
|
|
|
|
{
|
|
|
|
uint8_t *etxp;
|
|
|
|
uint8_t recorded_etx, new_etx;
|
|
|
|
|
|
|
|
etxp = (uint8_t *)neighbor_attr_get_data(&etx, dest);
|
|
|
|
if(etxp == NULL || *etxp == 0) {
|
2010-09-15 15:10:25 +00:00
|
|
|
recorded_etx = ETX2FIX(ETX_FIRST_GUESS);
|
2010-03-12 13:19:45 +00:00
|
|
|
} else {
|
|
|
|
recorded_etx = *etxp;
|
|
|
|
}
|
|
|
|
|
2010-03-19 16:55:29 +00:00
|
|
|
/* Update the EWMA of the ETX for the neighbor. */
|
2010-09-15 15:10:25 +00:00
|
|
|
packet_etx = ETX2FIX(packet_etx);
|
|
|
|
new_etx = ((uint16_t)recorded_etx * ETX_ALPHA +
|
|
|
|
(uint16_t)packet_etx * (ETX_SCALE - ETX_ALPHA)) / ETX_SCALE;
|
2010-10-28 08:54:54 +00:00
|
|
|
PRINTF("neighbor-info: ETX changed from %d to %d (packet ETX = %d) %d\n",
|
|
|
|
FIX2ETX(recorded_etx), FIX2ETX(new_etx), FIX2ETX(packet_etx),
|
|
|
|
dest->u8[7]);
|
2010-03-12 13:19:45 +00:00
|
|
|
|
2010-05-24 16:38:17 +00:00
|
|
|
if(neighbor_attr_has_neighbor(dest)) {
|
2010-03-17 15:08:46 +00:00
|
|
|
neighbor_attr_set_data(&etx, dest, &new_etx);
|
|
|
|
if(new_etx != recorded_etx && subscriber_callback != NULL) {
|
2010-10-22 13:50:13 +00:00
|
|
|
subscriber_callback(dest, 1, new_etx);
|
2010-03-17 15:08:46 +00:00
|
|
|
}
|
2010-03-12 13:19:45 +00: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:
|
|
|
|
if(subscriber_callback != NULL) {
|
2010-12-15 14:35:07 +00:00
|
|
|
subscriber_callback(addr, 1, ETX2FIX(ETX_FIRST_GUESS));
|
2010-03-12 13:19:45 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
void
|
|
|
|
neighbor_info_packet_sent(int status, int numtx)
|
|
|
|
{
|
|
|
|
const rimeaddr_t *dest;
|
|
|
|
uint8_t packet_etx;
|
|
|
|
|
|
|
|
dest = packetbuf_addr(PACKETBUF_ADDR_RECEIVER);
|
|
|
|
if(rimeaddr_cmp(dest, &rimeaddr_null)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-06-08 19:25:40 +00:00
|
|
|
PRINTF("neighbor-info: packet sent to %d.%d, status=%d, numtx=%d\n",
|
2010-03-12 13:19:45 +00:00
|
|
|
dest->u8[sizeof(*dest) - 2], dest->u8[sizeof(*dest) - 1],
|
|
|
|
status, numtx);
|
|
|
|
|
|
|
|
switch(status) {
|
|
|
|
case MAC_TX_OK:
|
2010-05-24 16:38:17 +00:00
|
|
|
packet_etx = numtx;
|
2010-03-17 15:08:46 +00:00
|
|
|
add_neighbor(dest);
|
2010-03-12 13:19:45 +00:00
|
|
|
break;
|
2010-10-28 08:54:54 +00:00
|
|
|
case MAC_TX_COLLISION:
|
|
|
|
packet_etx = numtx;
|
|
|
|
break;
|
2010-03-12 13:19:45 +00:00
|
|
|
case MAC_TX_NOACK:
|
2010-10-28 08:54:54 +00:00
|
|
|
packet_etx = NOACK_PACKET_ETX;
|
|
|
|
/* error and collissions will not cause high hits ??? */
|
|
|
|
break;
|
|
|
|
case MAC_TX_ERR:
|
2010-03-12 13:19:45 +00:00
|
|
|
default:
|
2010-10-28 08:54:54 +00:00
|
|
|
packet_etx = 0;
|
2010-06-14 06:51:58 +00:00
|
|
|
break;
|
2010-03-12 13:19:45 +00:00
|
|
|
}
|
|
|
|
|
2010-12-15 14:35:07 +00:00
|
|
|
if(packet_etx > 0) {
|
|
|
|
update_etx(dest, packet_etx);
|
|
|
|
}
|
2010-03-12 13:19:45 +00: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) {
|
2010-05-24 16:38:17 +00:00
|
|
|
neighbor_attr_register(&etx);
|
2010-03-12 13:19:45 +00:00
|
|
|
subscriber_callback = s;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2010-10-22 11:34:24 +00:00
|
|
|
uint8_t
|
|
|
|
neighbor_info_get_etx(const rimeaddr_t *addr)
|
|
|
|
{
|
|
|
|
uint8_t *etxp;
|
2010-10-22 13:50:13 +00:00
|
|
|
|
2010-10-22 11:34:24 +00:00
|
|
|
etxp = (uint8_t *)neighbor_attr_get_data(&etx, addr);
|
2010-10-22 13:50:13 +00:00
|
|
|
return etxp == NULL ? 0 : *etxp;
|
2010-10-22 11:34:24 +00:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|