Contiki-NG
link-stats.h
1/*
2 * Copyright (c) 2015, SICS Swedish ICT.
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 *
30 * Authors: Simon Duquennoy <simonduq@sics.se>
31 */
32
33#ifndef LINK_STATS_H_
34#define LINK_STATS_H_
35
36#include "net/linkaddr.h"
37
38/* ETX fixed point divisor. 128 is the value used by RPL (RFC 6551 and RFC 6719) */
39#ifdef LINK_STATS_CONF_ETX_DIVISOR
40#define LINK_STATS_ETX_DIVISOR LINK_STATS_CONF_ETX_DIVISOR
41#else /* LINK_STATS_CONF_ETX_DIVISOR */
42#define LINK_STATS_ETX_DIVISOR 128
43#endif /* LINK_STATS_CONF_ETX_DIVISOR */
44
45/* Option to infer the initial ETX from the RSSI of previously received packets. */
46#ifdef LINK_STATS_CONF_INIT_ETX_FROM_RSSI
47#define LINK_STATS_INIT_ETX_FROM_RSSI LINK_STATS_CONF_INIT_ETX_FROM_RSSI
48#else /* LINK_STATS_CONF_INIT_ETX_FROM_RSSI */
49#define LINK_STATS_INIT_ETX_FROM_RSSI 1
50#endif /* LINK_STATS_CONF_INIT_ETX_FROM_RSSI */
51
52/* Option to use packet and ACK count for ETX estimation, instead of EWMA */
53#ifdef LINK_STATS_CONF_ETX_FROM_PACKET_COUNT
54#define LINK_STATS_ETX_FROM_PACKET_COUNT LINK_STATS_CONF_ETX_FROM_PACKET_COUNT
55#else /* LINK_STATS_CONF_ETX_FROM_PACKET_COUNT */
56#define LINK_STATS_ETX_FROM_PACKET_COUNT 0
57#endif /* LINK_STATS_ETX_FROM_PACKET_COUNT */
58
59/* Store and periodically print packet counters? */
60#ifdef LINK_STATS_CONF_PACKET_COUNTERS
61#define LINK_STATS_PACKET_COUNTERS LINK_STATS_CONF_PACKET_COUNTERS
62#else /* LINK_STATS_CONF_PACKET_COUNTERS */
63#define LINK_STATS_PACKET_COUNTERS 0
64#endif /* LINK_STATS_PACKET_COUNTERS */
65
66/* Maximal initial ETX value when guessed from RSSI */
67#ifdef LINK_STATS_CONF_ETX_INIT_MAX
68#define LINK_STATS_ETX_INIT_MAX LINK_STATS_CONF_ETX_INIT_MAX
69#else /* LINK_STATS_CONF_ETX_INIT_MAX */
70#define LINK_STATS_ETX_INIT_MAX 3
71#endif /* LINK_STATS_ETX_INIT_MAX */
72
73/* "Good" RSSI value when ETX is guessed from RSSI */
74#ifdef LINK_STATS_CONF_RSSI_HIGH
75#define LINK_STATS_RSSI_HIGH LINK_STATS_CONF_RSSI_HIGH
76#else /* LINK_STATS_CONF_RSSI_HIGH */
77#define LINK_STATS_RSSI_HIGH -60
78#endif /* LINK_STATS_RSSI_HIGH */
79
80/* "Bad" RSSI value when ETX is guessed from RSSI */
81#ifdef LINK_STATS_CONF_RSSI_LOW
82#define LINK_STATS_RSSI_LOW LINK_STATS_CONF_RSSI_LOW
83#else /* LINK_STATS_CONF_RSSI_LOW */
84#define LINK_STATS_RSSI_LOW -90
85#endif /* LINK_STATS_RSSI_LOW */
86
87/* Special value that signal the RSSI is not initialized */
88#define LINK_STATS_RSSI_UNKNOWN 0x7fff
89
90typedef uint16_t link_packet_stat_t;
91
92struct link_packet_counter {
93 /* total attempts to transmit unicast packets */
94 link_packet_stat_t num_packets_tx;
95 /* total ACKs for unicast packets */
96 link_packet_stat_t num_packets_acked;
97 /* total number of unicast and broadcast packets received */
98 link_packet_stat_t num_packets_rx;
99 /* total number of packets dropped before transmission due to insufficient memory */
100 link_packet_stat_t num_queue_drops;
101};
102
103
104/* All statistics of a given link */
105struct link_stats {
106 clock_time_t last_tx_time; /* Last Tx timestamp */
107 uint16_t etx; /* ETX using ETX_DIVISOR as fixed point divisor. Zero if not yet measured. */
108 int16_t rssi; /* RSSI (received signal strength). LINK_STATS_RSSI_UNKNOWN if not yet measured. */
109 uint8_t freshness; /* Freshness of the statistics. Zero if no packets sent yet. */
110#if LINK_STATS_ETX_FROM_PACKET_COUNT
111 uint8_t tx_count; /* Tx count, used for ETX calculation */
112 uint8_t ack_count; /* ACK count, used for ETX calculation */
113#endif /* LINK_STATS_ETX_FROM_PACKET_COUNT */
114
115#if LINK_STATS_PACKET_COUNTERS
116 struct link_packet_counter cnt_current; /* packets in the current period */
117 struct link_packet_counter cnt_total; /* packets in total */
118#endif
119};
120
121/* Returns the neighbor's link statistics */
122const struct link_stats *link_stats_from_lladdr(const linkaddr_t *lladdr);
123/* Returns the address of the neighbor */
124const linkaddr_t *link_stats_get_lladdr(const struct link_stats *);
125/* Are the statistics fresh? */
126int link_stats_is_fresh(const struct link_stats *stats);
127/* Resets link-stats module */
128void link_stats_reset(void);
129/* Initializes link-stats module */
130void link_stats_init(void);
131/* Packet sent callback. Updates statistics for transmissions on a given link */
132void link_stats_packet_sent(const linkaddr_t *lladdr, int status, int numtx);
133/* Packet input callback. Updates statistics for receptions on a given link */
134void link_stats_input_callback(const linkaddr_t *lladdr);
135
136#endif /* LINK_STATS_H_ */
Header file for the link-layer address representation.