Contiki 3.x
rpl.h
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  * \file
32  * Public API declarations for ContikiRPL.
33  * \author
34  * Joakim Eriksson <joakime@sics.se> & Nicolas Tsiftes <nvt@sics.se>
35  *
36  */
37 
38 #ifndef RPL_H
39 #define RPL_H
40 
41 #include "rpl-conf.h"
42 
43 #include "lib/list.h"
44 #include "net/ip/uip.h"
45 #include "net/ipv6/uip-ds6.h"
46 #include "sys/ctimer.h"
47 
48 /*---------------------------------------------------------------------------*/
49 typedef uint16_t rpl_rank_t;
50 typedef uint16_t rpl_ocp_t;
51 /*---------------------------------------------------------------------------*/
52 /* DAG Metric Container Object Types, to be confirmed by IANA. */
53 #define RPL_DAG_MC_NONE 0 /* Local identifier for empty MC */
54 #define RPL_DAG_MC_NSA 1 /* Node State and Attributes */
55 #define RPL_DAG_MC_ENERGY 2 /* Node Energy */
56 #define RPL_DAG_MC_HOPCOUNT 3 /* Hop Count */
57 #define RPL_DAG_MC_THROUGHPUT 4 /* Throughput */
58 #define RPL_DAG_MC_LATENCY 5 /* Latency */
59 #define RPL_DAG_MC_LQL 6 /* Link Quality Level */
60 #define RPL_DAG_MC_ETX 7 /* Expected Transmission Count */
61 #define RPL_DAG_MC_LC 8 /* Link Color */
62 
63 /* DAG Metric Container flags. */
64 #define RPL_DAG_MC_FLAG_P 0x8
65 #define RPL_DAG_MC_FLAG_C 0x4
66 #define RPL_DAG_MC_FLAG_O 0x2
67 #define RPL_DAG_MC_FLAG_R 0x1
68 
69 /* DAG Metric Container aggregation mode. */
70 #define RPL_DAG_MC_AGGR_ADDITIVE 0
71 #define RPL_DAG_MC_AGGR_MAXIMUM 1
72 #define RPL_DAG_MC_AGGR_MINIMUM 2
73 #define RPL_DAG_MC_AGGR_MULTIPLICATIVE 3
74 
75 /* The bit index within the flags field of
76  the rpl_metric_object_energy structure. */
77 #define RPL_DAG_MC_ENERGY_INCLUDED 3
78 #define RPL_DAG_MC_ENERGY_TYPE 1
79 #define RPL_DAG_MC_ENERGY_ESTIMATION 0
80 
81 #define RPL_DAG_MC_ENERGY_TYPE_MAINS 0
82 #define RPL_DAG_MC_ENERGY_TYPE_BATTERY 1
83 #define RPL_DAG_MC_ENERGY_TYPE_SCAVENGING 2
84 
85 struct rpl_metric_object_energy {
86  uint8_t flags;
87  uint8_t energy_est;
88 };
89 
90 /* Logical representation of a DAG Metric Container. */
91 struct rpl_metric_container {
92  uint8_t type;
93  uint8_t flags;
94  uint8_t aggr;
95  uint8_t prec;
96  uint8_t length;
97  union metric_object {
98  struct rpl_metric_object_energy energy;
99  uint16_t etx;
100  } obj;
101 };
102 typedef struct rpl_metric_container rpl_metric_container_t;
103 /*---------------------------------------------------------------------------*/
104 struct rpl_instance;
105 struct rpl_dag;
106 /*---------------------------------------------------------------------------*/
107 #define RPL_PARENT_FLAG_UPDATED 0x1
108 #define RPL_PARENT_FLAG_LINK_METRIC_VALID 0x2
109 
110 struct rpl_parent {
111  struct rpl_parent *next;
112  struct rpl_dag *dag;
113 #if RPL_DAG_MC != RPL_DAG_MC_NONE
114  rpl_metric_container_t mc;
115 #endif /* RPL_DAG_MC != RPL_DAG_MC_NONE */
116  rpl_rank_t rank;
117  uint16_t link_metric;
118  uint8_t dtsn;
119  uint8_t flags;
120 };
121 typedef struct rpl_parent rpl_parent_t;
122 /*---------------------------------------------------------------------------*/
123 /* RPL DIO prefix suboption */
124 struct rpl_prefix {
125  uip_ipaddr_t prefix;
126  uint32_t lifetime;
127  uint8_t length;
128  uint8_t flags;
129 };
130 typedef struct rpl_prefix rpl_prefix_t;
131 /*---------------------------------------------------------------------------*/
132 /* Directed Acyclic Graph */
133 struct rpl_dag {
134  uip_ipaddr_t dag_id;
135  rpl_rank_t min_rank; /* should be reset per DAG iteration! */
136  uint8_t version;
137  uint8_t grounded;
138  uint8_t preference;
139  uint8_t used;
140  /* live data for the DAG */
141  uint8_t joined;
142  rpl_parent_t *preferred_parent;
143  rpl_rank_t rank;
144  struct rpl_instance *instance;
145  rpl_prefix_t prefix_info;
146 };
147 typedef struct rpl_dag rpl_dag_t;
148 typedef struct rpl_instance rpl_instance_t;
149 /*---------------------------------------------------------------------------*/
150 /*
151  * API for RPL objective functions (OF)
152  *
153  * reset(dag)
154  *
155  * Resets the objective function state for a specific DAG. This function is
156  * called when doing a global repair on the DAG.
157  *
158  * neighbor_link_callback(parent, known, etx)
159  *
160  * Receives link-layer neighbor information. The parameter "known" is set
161  * either to 0 or 1. The "etx" parameter specifies the current
162  * ETX(estimated transmissions) for the neighbor.
163  *
164  * best_parent(parent1, parent2)
165  *
166  * Compares two parents and returns the best one, according to the OF.
167  *
168  * best_dag(dag1, dag2)
169  *
170  * Compares two DAGs and returns the best one, according to the OF.
171  *
172  * calculate_rank(parent, base_rank)
173  *
174  * Calculates a rank value using the parent rank and a base rank.
175  * If "parent" is NULL, the objective function selects a default increment
176  * that is adds to the "base_rank". Otherwise, the OF uses information known
177  * about "parent" to select an increment to the "base_rank".
178  *
179  * update_metric_container(dag)
180  *
181  * Updates the metric container for outgoing DIOs in a certain DAG.
182  * If the objective function of the DAG does not use metric containers,
183  * the function should set the object type to RPL_DAG_MC_NONE.
184  */
185 struct rpl_of {
186  void (*reset)(struct rpl_dag *);
187  void (*neighbor_link_callback)(rpl_parent_t *, int, int);
188  rpl_parent_t *(*best_parent)(rpl_parent_t *, rpl_parent_t *);
189  rpl_dag_t *(*best_dag)(rpl_dag_t *, rpl_dag_t *);
190  rpl_rank_t (*calculate_rank)(rpl_parent_t *, rpl_rank_t);
191  void (*update_metric_container)( rpl_instance_t *);
192  rpl_ocp_t ocp;
193 };
194 typedef struct rpl_of rpl_of_t;
195 
196 /* Declare the selected objective function. */
197 extern rpl_of_t RPL_OF;
198 /*---------------------------------------------------------------------------*/
199 /* Instance */
200 struct rpl_instance {
201  /* DAG configuration */
202  rpl_metric_container_t mc;
203  rpl_of_t *of;
204  rpl_dag_t *current_dag;
205  rpl_dag_t dag_table[RPL_MAX_DAG_PER_INSTANCE];
206  /* The current default router - used for routing "upwards" */
207  uip_ds6_defrt_t *def_route;
208  uint8_t instance_id;
209  uint8_t used;
210  uint8_t dtsn_out;
211  uint8_t mop;
212  uint8_t dio_intdoubl;
213  uint8_t dio_intmin;
214  uint8_t dio_redundancy;
215  uint8_t default_lifetime;
216  uint8_t dio_intcurrent;
217  uint8_t dio_send; /* for keeping track of which mode the timer is in */
218  uint8_t dio_counter;
219  rpl_rank_t max_rankinc;
220  rpl_rank_t min_hoprankinc;
221  uint16_t lifetime_unit; /* lifetime in seconds = l_u * d_l */
222 #if RPL_CONF_STATS
223  uint16_t dio_totint;
224  uint16_t dio_totsend;
225  uint16_t dio_totrecv;
226 #endif /* RPL_CONF_STATS */
227  clock_time_t dio_next_delay; /* delay for completion of dio interval */
228  struct ctimer dio_timer;
229  struct ctimer dao_timer;
230  struct ctimer dao_lifetime_timer;
231 };
232 
233 /*---------------------------------------------------------------------------*/
234 /* Public RPL functions. */
235 void rpl_init(void);
236 void uip_rpl_input(void);
237 rpl_dag_t *rpl_set_root(uint8_t instance_id, uip_ipaddr_t *dag_id);
238 int rpl_set_prefix(rpl_dag_t *dag, uip_ipaddr_t *prefix, unsigned len);
239 int rpl_repair_root(uint8_t instance_id);
240 int rpl_set_default_route(rpl_instance_t *instance, uip_ipaddr_t *from);
241 rpl_dag_t *rpl_get_any_dag(void);
242 rpl_instance_t *rpl_get_instance(uint8_t instance_id);
243 void rpl_update_header_empty(void);
244 int rpl_update_header_final(uip_ipaddr_t *addr);
245 int rpl_verify_header(int);
246 void rpl_insert_header(void);
247 void rpl_remove_header(void);
248 uint8_t rpl_invert_header(void);
249 uip_ipaddr_t *rpl_get_parent_ipaddr(rpl_parent_t *nbr);
250 rpl_rank_t rpl_get_parent_rank(uip_lladdr_t *addr);
251 uint16_t rpl_get_parent_link_metric(const uip_lladdr_t *addr);
252 void rpl_dag_init(void);
253 
254 
255 /**
256  * RPL modes
257  *
258  * The RPL module can be in either of three modes: mesh mode
259  * (RPL_MODE_MESH), feater mode (RPL_MODE_FEATHER), and leaf mode
260  * (RPL_MODE_LEAF). In mesh mode, nodes forward data for other nodes,
261  * and are reachable by others. In feather mode, nodes can forward
262  * data for other nodes, but are not reachable themselves. In leaf
263  * mode, nodes do not forward data for others, but are reachable by
264  * others. */
265 enum rpl_mode {
266  RPL_MODE_MESH = 0,
267  RPL_MODE_FEATHER = 1,
268  RPL_MODE_LEAF = 2,
269 };
270 
271 /**
272  * Set the RPL mode
273  *
274  * \param mode The new RPL mode
275  * \retval The previous RPL mode
276  */
277 enum rpl_mode rpl_set_mode(enum rpl_mode mode);
278 
279 /**
280  * Get the RPL mode
281  *
282  * \retval The RPL mode
283  */
284 enum rpl_mode rpl_get_mode(void);
285 
286 /*---------------------------------------------------------------------------*/
287 #endif /* RPL_H */
Linked list manipulation routines.
Header file for the uIP TCP/IP stack.
Network interface and stateless autoconfiguration (RFC 4862)
enum rpl_mode rpl_get_mode(void)
Get the RPL mode.
Definition: rpl.c:67
802.3 address
Definition: uip.h:135
Header file for the callback timer
enum rpl_mode rpl_set_mode(enum rpl_mode m)
Set the RPL mode.
Definition: rpl.c:73
An entry in the default router list.