Contiki 3.x
rpl-of0.c
Go to the documentation of this file.
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 
32 /**
33  * \file
34  * An implementation of RPL's objective function 0.
35  *
36  * \author Joakim Eriksson <joakime@sics.se>, Nicolas Tsiftes <nvt@sics.se>
37  */
38 
39 /**
40  * \addtogroup uip6
41  * @{
42  */
43 
44 #include "net/rpl/rpl-private.h"
45 
46 #define DEBUG DEBUG_NONE
47 #include "net/ip/uip-debug.h"
48 
49 static void reset(rpl_dag_t *);
50 static rpl_parent_t *best_parent(rpl_parent_t *, rpl_parent_t *);
51 static rpl_dag_t *best_dag(rpl_dag_t *, rpl_dag_t *);
52 static rpl_rank_t calculate_rank(rpl_parent_t *, rpl_rank_t);
53 static void update_metric_container(rpl_instance_t *);
54 
55 rpl_of_t rpl_of0 = {
56  reset,
57  NULL,
58  best_parent,
59  best_dag,
60  calculate_rank,
61  update_metric_container,
62  0
63 };
64 
65 #define DEFAULT_RANK_INCREMENT RPL_MIN_HOPRANKINC
66 
67 #define MIN_DIFFERENCE (RPL_MIN_HOPRANKINC + RPL_MIN_HOPRANKINC / 2)
68 
69 static void
70 reset(rpl_dag_t *dag)
71 {
72  PRINTF("RPL: Resetting OF0\n");
73 }
74 
75 static rpl_rank_t
76 calculate_rank(rpl_parent_t *p, rpl_rank_t base_rank)
77 {
78  rpl_rank_t increment;
79  if(base_rank == 0) {
80  if(p == NULL) {
81  return INFINITE_RANK;
82  }
83  base_rank = p->rank;
84  }
85 
86  increment = p != NULL ?
87  p->dag->instance->min_hoprankinc :
88  DEFAULT_RANK_INCREMENT;
89 
90  if((rpl_rank_t)(base_rank + increment) < base_rank) {
91  PRINTF("RPL: OF0 rank %d incremented to infinite rank due to wrapping\n",
92  base_rank);
93  return INFINITE_RANK;
94  }
95  return base_rank + increment;
96 
97 }
98 
99 static rpl_dag_t *
100 best_dag(rpl_dag_t *d1, rpl_dag_t *d2)
101 {
102  if(d1->grounded) {
103  if (!d2->grounded) {
104  return d1;
105  }
106  } else if(d2->grounded) {
107  return d2;
108  }
109 
110  if(d1->preference < d2->preference) {
111  return d2;
112  } else {
113  if(d1->preference > d2->preference) {
114  return d1;
115  }
116  }
117 
118  if(d2->rank < d1->rank) {
119  return d2;
120  } else {
121  return d1;
122  }
123 }
124 
125 static rpl_parent_t *
126 best_parent(rpl_parent_t *p1, rpl_parent_t *p2)
127 {
128  rpl_rank_t r1, r2;
129  rpl_dag_t *dag;
130 
131  PRINTF("RPL: Comparing parent ");
132  PRINT6ADDR(rpl_get_parent_ipaddr(p1));
133  PRINTF(" (confidence %d, rank %d) with parent ",
134  p1->link_metric, p1->rank);
135  PRINT6ADDR(rpl_get_parent_ipaddr(p2));
136  PRINTF(" (confidence %d, rank %d)\n",
137  p2->link_metric, p2->rank);
138 
139 
140  r1 = DAG_RANK(p1->rank, p1->dag->instance) * RPL_MIN_HOPRANKINC +
141  p1->link_metric;
142  r2 = DAG_RANK(p2->rank, p1->dag->instance) * RPL_MIN_HOPRANKINC +
143  p2->link_metric;
144  /* Compare two parents by looking both and their rank and at the ETX
145  for that parent. We choose the parent that has the most
146  favourable combination. */
147 
148  dag = (rpl_dag_t *)p1->dag; /* Both parents must be in the same DAG. */
149  if(r1 < r2 + MIN_DIFFERENCE &&
150  r1 > r2 - MIN_DIFFERENCE) {
151  return dag->preferred_parent;
152  } else if(r1 < r2) {
153  return p1;
154  } else {
155  return p2;
156  }
157 }
158 
159 static void
160 update_metric_container(rpl_instance_t *instance)
161 {
162  instance->mc.type = RPL_DAG_MC_NONE;
163 }
164 
165 /** @}*/
#define NULL
The null pointer.
A set of debugging macros.