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