Contiki-Inga 3.x
rpl-of0.c
Go to the documentation of this file.
1 /**
2  * \addtogroup uip6
3  * @{
4  */
5 /*
6  * Copyright (c) 2010, Swedish Institute of Computer Science.
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  * notice, this list of conditions and the following disclaimer in the
16  * documentation and/or other materials provided with the distribution.
17  * 3. Neither the name of the Institute nor the names of its contributors
18  * may be used to endorse or promote products derived from this software
19  * without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  * This file is part of the Contiki operating system.
34  */
35 /**
36  * \file
37  * An implementation of RPL's objective function 0.
38  *
39  * \author Joakim Eriksson <joakime@sics.se>, Nicolas Tsiftes <nvt@sics.se>
40  */
41 
42 #include "net/rpl/rpl-private.h"
43 
44 #define DEBUG DEBUG_NONE
45 #include "net/ip/uip-debug.h"
46 
47 static void reset(rpl_dag_t *);
48 static rpl_parent_t *best_parent(rpl_parent_t *, rpl_parent_t *);
49 static rpl_dag_t *best_dag(rpl_dag_t *, rpl_dag_t *);
50 static rpl_rank_t calculate_rank(rpl_parent_t *, rpl_rank_t);
51 static void update_metric_container(rpl_instance_t *);
52 
53 rpl_of_t rpl_of0 = {
54  reset,
55  NULL,
56  best_parent,
57  best_dag,
58  calculate_rank,
59  update_metric_container,
60  0
61 };
62 
63 #define DEFAULT_RANK_INCREMENT RPL_MIN_HOPRANKINC
64 
65 #define MIN_DIFFERENCE (RPL_MIN_HOPRANKINC + RPL_MIN_HOPRANKINC / 2)
66 
67 static void
68 reset(rpl_dag_t *dag)
69 {
70  PRINTF("RPL: Resetting OF0\n");
71 }
72 
73 static rpl_rank_t
74 calculate_rank(rpl_parent_t *p, rpl_rank_t base_rank)
75 {
76  rpl_rank_t increment;
77  if(base_rank == 0) {
78  if(p == NULL) {
79  return INFINITE_RANK;
80  }
81  base_rank = p->rank;
82  }
83 
84  increment = p != NULL ?
85  p->dag->instance->min_hoprankinc :
86  DEFAULT_RANK_INCREMENT;
87 
88  if((rpl_rank_t)(base_rank + increment) < base_rank) {
89  PRINTF("RPL: OF0 rank %d incremented to infinite rank due to wrapping\n",
90  base_rank);
91  return INFINITE_RANK;
92  }
93  return base_rank + increment;
94 
95 }
96 
97 static rpl_dag_t *
98 best_dag(rpl_dag_t *d1, rpl_dag_t *d2)
99 {
100  if(d1->grounded) {
101  if (!d2->grounded) {
102  return d1;
103  }
104  } else if(d2->grounded) {
105  return d2;
106  }
107 
108  if(d1->preference < d2->preference) {
109  return d2;
110  } else {
111  if(d1->preference > d2->preference) {
112  return d1;
113  }
114  }
115 
116  if(d2->rank < d1->rank) {
117  return d2;
118  } else {
119  return d1;
120  }
121 }
122 
123 static rpl_parent_t *
124 best_parent(rpl_parent_t *p1, rpl_parent_t *p2)
125 {
126  rpl_rank_t r1, r2;
127  rpl_dag_t *dag;
128 
129  PRINTF("RPL: Comparing parent ");
130  PRINT6ADDR(rpl_get_parent_ipaddr(p1));
131  PRINTF(" (confidence %d, rank %d) with parent ",
132  p1->link_metric, p1->rank);
133  PRINT6ADDR(rpl_get_parent_ipaddr(p2));
134  PRINTF(" (confidence %d, rank %d)\n",
135  p2->link_metric, p2->rank);
136 
137 
138  r1 = DAG_RANK(p1->rank, p1->dag->instance) * RPL_MIN_HOPRANKINC +
139  p1->link_metric;
140  r2 = DAG_RANK(p2->rank, p1->dag->instance) * RPL_MIN_HOPRANKINC +
141  p2->link_metric;
142  /* Compare two parents by looking both and their rank and at the ETX
143  for that parent. We choose the parent that has the most
144  favourable combination. */
145 
146  dag = (rpl_dag_t *)p1->dag; /* Both parents must be in the same DAG. */
147  if(r1 < r2 + MIN_DIFFERENCE &&
148  r1 > r2 - MIN_DIFFERENCE) {
149  return dag->preferred_parent;
150  } else if(r1 < r2) {
151  return p1;
152  } else {
153  return p2;
154  }
155 }
156 
157 static void
158 update_metric_container(rpl_instance_t *instance)
159 {
160  instance->mc.type = RPL_DAG_MC_NONE;
161 }