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/* Special value that signal the RSSI is not initialized */
67#define LINK_STATS_RSSI_UNKNOWN 0x7fff
68
69typedef uint16_t link_packet_stat_t;
70
71struct link_packet_counter {
72 /* total attempts to transmit unicast packets */
73 link_packet_stat_t num_packets_tx;
74 /* total ACKs for unicast packets */
75 link_packet_stat_t num_packets_acked;
76 /* total number of unicast and broadcast packets received */
77 link_packet_stat_t num_packets_rx;
78 /* total number of packets dropped before transmission due to insufficient memory */
79 link_packet_stat_t num_queue_drops;
80};
81
82
83/* All statistics of a given link */
84struct link_stats {
85 clock_time_t last_tx_time; /* Last Tx timestamp */
86 uint16_t etx; /* ETX using ETX_DIVISOR as fixed point divisor. Zero if not yet measured. */
87 int16_t rssi; /* RSSI (received signal strength). LINK_STATS_RSSI_UNKNOWN if not yet measured. */
88 uint8_t freshness; /* Freshness of the statistics. Zero if no packets sent yet. */
89#if LINK_STATS_ETX_FROM_PACKET_COUNT
90 uint8_t tx_count; /* Tx count, used for ETX calculation */
91 uint8_t ack_count; /* ACK count, used for ETX calculation */
92#endif /* LINK_STATS_ETX_FROM_PACKET_COUNT */
93
94#if LINK_STATS_PACKET_COUNTERS
95 struct link_packet_counter cnt_current; /* packets in the current period */
96 struct link_packet_counter cnt_total; /* packets in total */
97#endif
98};
99
100/* Returns the neighbor's link statistics */
101const struct link_stats *link_stats_from_lladdr(const linkaddr_t *lladdr);
102/* Returns the address of the neighbor */
103const linkaddr_t *link_stats_get_lladdr(const struct link_stats *);
104/* Are the statistics fresh? */
105int link_stats_is_fresh(const struct link_stats *stats);
106/* Resets link-stats module */
107void link_stats_reset(void);
108/* Initializes link-stats module */
109void link_stats_init(void);
110/* Packet sent callback. Updates statistics for transmissions on a given link */
111void link_stats_packet_sent(const linkaddr_t *lladdr, int status, int numtx);
112/* Packet input callback. Updates statistics for receptions on a given link */
113void link_stats_input_callback(const linkaddr_t *lladdr);
114
115#endif /* LINK_STATS_H_ */
Header file for the link-layer address representation.