neighbor-info.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2010, Swedish Institute of Computer Science.
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions
00007  * are met:
00008  * 1. Redistributions of source code must retain the above copyright
00009  *    notice, this list of conditions and the following disclaimer.
00010  * 2. Redistributions in binary form must reproduce the above copyright
00011  *    notice, this list of conditions and the following disclaimer in the
00012  *    documentation and/or other materials provided with the distribution.
00013  * 3. Neither the name of the Institute nor the names of its contributors
00014  *    may be used to endorse or promote products derived from this software
00015  *    without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
00018  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00019  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00020  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
00021  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00022  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00023  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00024  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00025  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00026  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00027  * SUCH DAMAGE.
00028  *
00029  * This file is part of the Contiki operating system.
00030  *
00031  * $Id: neighbor-info.c,v 1.18 2010/12/15 14:35:07 nvt-se Exp $
00032  */
00033 /**
00034  * \file
00035  *         A generic module for management of neighbor information.
00036  *
00037  * \author Nicolas Tsiftes <nvt@sics.se>
00038  */
00039 
00040 #include "net/neighbor-info.h"
00041 #include "net/neighbor-attr.h"
00042 
00043 #define DEBUG DEBUG_NONE
00044 #include "net/uip-debug.h"
00045 
00046 #define ETX_LIMIT               15
00047 #define ETX_SCALE               100
00048 #define ETX_ALPHA               90
00049 #define ETX_NOACK_PENALTY       ETX_LIMIT
00050 /*---------------------------------------------------------------------------*/
00051 NEIGHBOR_ATTRIBUTE(link_metric_t, etx, NULL);
00052 
00053 static neighbor_info_subscriber_t subscriber_callback;
00054 /*---------------------------------------------------------------------------*/
00055 static void
00056 update_metric(const rimeaddr_t *dest, int packet_metric)
00057 {
00058   link_metric_t *metricp;
00059   link_metric_t recorded_metric, new_metric;
00060 
00061   metricp = (link_metric_t *)neighbor_attr_get_data(&etx, dest);
00062   packet_metric = NEIGHBOR_INFO_ETX2FIX(packet_metric);
00063   if(metricp == NULL || *metricp == 0) {
00064     recorded_metric = NEIGHBOR_INFO_ETX2FIX(ETX_LIMIT);
00065     new_metric = packet_metric;
00066   } else {
00067     recorded_metric = *metricp;
00068     /* Update the EWMA of the ETX for the neighbor. */
00069     new_metric = ((uint16_t)recorded_metric * ETX_ALPHA +
00070                (uint16_t)packet_metric * (ETX_SCALE - ETX_ALPHA)) / ETX_SCALE;
00071   }
00072 
00073   PRINTF("neighbor-info: ETX changed from %d to %d (packet ETX = %d) %d\n",
00074          NEIGHBOR_INFO_FIX2ETX(recorded_metric),
00075          NEIGHBOR_INFO_FIX2ETX(new_metric),
00076          NEIGHBOR_INFO_FIX2ETX(packet_metric),
00077          dest->u8[7]);
00078 
00079   if(neighbor_attr_has_neighbor(dest)) {
00080     neighbor_attr_set_data(&etx, dest, &new_metric);
00081     if(new_metric != recorded_metric && subscriber_callback != NULL) {
00082       subscriber_callback(dest, 1, new_metric);
00083     }
00084   }
00085 }
00086 /*---------------------------------------------------------------------------*/
00087 static void
00088 add_neighbor(const rimeaddr_t *addr)
00089 {
00090   switch(neighbor_attr_add_neighbor(addr)) {
00091   case -1:
00092     PRINTF("neighbor-info: failed to add a node.\n");
00093     break;
00094   case 0:
00095     PRINTF("neighbor-info: The neighbor is already known\n");
00096     break;
00097   default:
00098     break;
00099   }
00100 }
00101 /*---------------------------------------------------------------------------*/
00102 void
00103 neighbor_info_packet_sent(int status, int numtx)
00104 {
00105   const rimeaddr_t *dest;
00106   link_metric_t packet_metric;
00107 
00108   dest = packetbuf_addr(PACKETBUF_ADDR_RECEIVER);
00109   if(rimeaddr_cmp(dest, &rimeaddr_null)) {
00110     return;
00111   }
00112 
00113   PRINTF("neighbor-info: packet sent to %d.%d, status=%d, numtx=%d\n",
00114         dest->u8[sizeof(*dest) - 2], dest->u8[sizeof(*dest) - 1],
00115         status, numtx);
00116 
00117   switch(status) {
00118   case MAC_TX_OK:
00119     packet_metric = numtx;
00120     add_neighbor(dest);
00121     break;
00122   case MAC_TX_COLLISION:
00123     packet_metric = numtx;
00124     break;
00125   case MAC_TX_NOACK:
00126     packet_metric = ETX_NOACK_PENALTY;
00127     break;
00128   default:
00129     /* Do not penalize the ETX when collisions or transmission
00130        errors occur. */
00131     return;
00132   }
00133 
00134   update_metric(dest, packet_metric);
00135 }
00136 /*---------------------------------------------------------------------------*/
00137 void
00138 neighbor_info_packet_received(void)
00139 {
00140   const rimeaddr_t *src;
00141 
00142   src = packetbuf_addr(PACKETBUF_ADDR_SENDER);
00143   if(rimeaddr_cmp(src, &rimeaddr_null)) {
00144     return;
00145   }
00146 
00147   PRINTF("neighbor-info: packet received from %d.%d\n",
00148         src->u8[sizeof(*src) - 2], src->u8[sizeof(*src) - 1]);
00149 
00150   add_neighbor(src);
00151 }
00152 /*---------------------------------------------------------------------------*/
00153 int
00154 neighbor_info_subscribe(neighbor_info_subscriber_t s)
00155 {
00156   if(subscriber_callback == NULL) {
00157     neighbor_attr_register(&etx);
00158     subscriber_callback = s;
00159     return 1;
00160   }
00161 
00162   return 0;
00163 }
00164 /*---------------------------------------------------------------------------*/
00165 link_metric_t
00166 neighbor_info_get_metric(const rimeaddr_t *addr)
00167 {
00168   link_metric_t *metricp;
00169 
00170   metricp = (link_metric_t *)neighbor_attr_get_data(&etx, addr);
00171   return metricp == NULL ? ETX_LIMIT : *metricp;
00172 }
00173 /*---------------------------------------------------------------------------*/

Generated on Mon Apr 11 14:23:29 2011 for Contiki 2.5 by  doxygen 1.6.1