Contiki 2.5
neighbor-info.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2010, Swedish Institute of Computer Science.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the Institute nor the names of its contributors
14  * may be used to endorse or promote products derived from this software
15  * without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * This file is part of the Contiki operating system.
30  *
31  * $Id: neighbor-info.c,v 1.18 2010/12/15 14:35:07 nvt-se Exp $
32  */
33 /**
34  * \file
35  * A generic module for management of neighbor information.
36  *
37  * \author Nicolas Tsiftes <nvt@sics.se>
38  */
39 
40 #include "net/neighbor-info.h"
41 #include "net/neighbor-attr.h"
42 
43 #define DEBUG DEBUG_NONE
44 #include "net/uip-debug.h"
45 
46 #define ETX_LIMIT 15
47 #define ETX_SCALE 100
48 #define ETX_ALPHA 90
49 #define ETX_NOACK_PENALTY ETX_LIMIT
50 /*---------------------------------------------------------------------------*/
51 NEIGHBOR_ATTRIBUTE(link_metric_t, etx, NULL);
52 
53 static neighbor_info_subscriber_t subscriber_callback;
54 /*---------------------------------------------------------------------------*/
55 static void
56 update_metric(const rimeaddr_t *dest, int packet_metric)
57 {
58  link_metric_t *metricp;
59  link_metric_t recorded_metric, new_metric;
60 
61  metricp = (link_metric_t *)neighbor_attr_get_data(&etx, dest);
62  packet_metric = NEIGHBOR_INFO_ETX2FIX(packet_metric);
63  if(metricp == NULL || *metricp == 0) {
64  recorded_metric = NEIGHBOR_INFO_ETX2FIX(ETX_LIMIT);
65  new_metric = packet_metric;
66  } else {
67  recorded_metric = *metricp;
68  /* Update the EWMA of the ETX for the neighbor. */
69  new_metric = ((uint16_t)recorded_metric * ETX_ALPHA +
70  (uint16_t)packet_metric * (ETX_SCALE - ETX_ALPHA)) / ETX_SCALE;
71  }
72 
73  PRINTF("neighbor-info: ETX changed from %d to %d (packet ETX = %d) %d\n",
74  NEIGHBOR_INFO_FIX2ETX(recorded_metric),
75  NEIGHBOR_INFO_FIX2ETX(new_metric),
76  NEIGHBOR_INFO_FIX2ETX(packet_metric),
77  dest->u8[7]);
78 
79  if(neighbor_attr_has_neighbor(dest)) {
80  neighbor_attr_set_data(&etx, dest, &new_metric);
81  if(new_metric != recorded_metric && subscriber_callback != NULL) {
82  subscriber_callback(dest, 1, new_metric);
83  }
84  }
85 }
86 /*---------------------------------------------------------------------------*/
87 static void
88 add_neighbor(const rimeaddr_t *addr)
89 {
90  switch(neighbor_attr_add_neighbor(addr)) {
91  case -1:
92  PRINTF("neighbor-info: failed to add a node.\n");
93  break;
94  case 0:
95  PRINTF("neighbor-info: The neighbor is already known\n");
96  break;
97  default:
98  break;
99  }
100 }
101 /*---------------------------------------------------------------------------*/
102 void
103 neighbor_info_packet_sent(int status, int numtx)
104 {
105  const rimeaddr_t *dest;
106  link_metric_t packet_metric;
107 
108  dest = packetbuf_addr(PACKETBUF_ADDR_RECEIVER);
109  if(rimeaddr_cmp(dest, &rimeaddr_null)) {
110  return;
111  }
112 
113  PRINTF("neighbor-info: packet sent to %d.%d, status=%d, numtx=%d\n",
114  dest->u8[sizeof(*dest) - 2], dest->u8[sizeof(*dest) - 1],
115  status, numtx);
116 
117  switch(status) {
118  case MAC_TX_OK:
119  packet_metric = numtx;
120  add_neighbor(dest);
121  break;
122  case MAC_TX_COLLISION:
123  packet_metric = numtx;
124  break;
125  case MAC_TX_NOACK:
126  packet_metric = ETX_NOACK_PENALTY;
127  break;
128  default:
129  /* Do not penalize the ETX when collisions or transmission
130  errors occur. */
131  return;
132  }
133 
134  update_metric(dest, packet_metric);
135 }
136 /*---------------------------------------------------------------------------*/
137 void
139 {
140  const rimeaddr_t *src;
141 
142  src = packetbuf_addr(PACKETBUF_ADDR_SENDER);
143  if(rimeaddr_cmp(src, &rimeaddr_null)) {
144  return;
145  }
146 
147  PRINTF("neighbor-info: packet received from %d.%d\n",
148  src->u8[sizeof(*src) - 2], src->u8[sizeof(*src) - 1]);
149 
150  add_neighbor(src);
151 }
152 /*---------------------------------------------------------------------------*/
153 int
154 neighbor_info_subscribe(neighbor_info_subscriber_t s)
155 {
156  if(subscriber_callback == NULL) {
157  neighbor_attr_register(&etx);
158  subscriber_callback = s;
159  return 1;
160  }
161 
162  return 0;
163 }
164 /*---------------------------------------------------------------------------*/
165 link_metric_t
166 neighbor_info_get_metric(const rimeaddr_t *addr)
167 {
168  link_metric_t *metricp;
169 
170  metricp = (link_metric_t *)neighbor_attr_get_data(&etx, addr);
171  return metricp == NULL ? ETX_LIMIT : *metricp;
172 }
173 /*---------------------------------------------------------------------------*/