Contiki 2.5
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/uip-debug.h"
46 
47 #include "net/neighbor-info.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_rank_t calculate_rank(rpl_parent_t *, rpl_rank_t);
52 static void update_metric_container(rpl_dag_t *);
53 
54 rpl_of_t rpl_of0 = {
55  reset,
56  NULL,
57  best_parent,
58  calculate_rank,
59  update_metric_container,
60  0
61 };
62 
63 #define DEFAULT_RANK_INCREMENT DEFAULT_MIN_HOPRANKINC
64 
65 #define MIN_DIFFERENCE (NEIGHBOR_INFO_ETX_DIVISOR + NEIGHBOR_INFO_ETX_DIVISOR / 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 ? p->dag->min_hoprankinc : DEFAULT_RANK_INCREMENT;
85 
86  if((rpl_rank_t)(base_rank + increment) < base_rank) {
87  PRINTF("RPL: OF0 rank %d incremented to infinite rank due to wrapping\n",
88  base_rank);
89  return INFINITE_RANK;
90  }
91  return base_rank + increment;
92 
93 }
94 
95 static rpl_parent_t *
96 best_parent(rpl_parent_t *p1, rpl_parent_t *p2)
97 {
98  rpl_rank_t r1, r2;
99  rpl_dag_t *dag;
100 
101  PRINTF("RPL: Comparing parent ");
102  PRINT6ADDR(&p1->addr);
103  PRINTF(" (confidence %d, rank %d) with parent ",
104  p1->link_metric, p1->rank);
105  PRINT6ADDR(&p2->addr);
106  PRINTF(" (confidence %d, rank %d)\n",
107  p2->link_metric, p2->rank);
108 
109 
110  r1 = DAG_RANK(p1->rank, (rpl_dag_t *)p1->dag) * NEIGHBOR_INFO_ETX_DIVISOR +
111  p1->link_metric;
112  r2 = DAG_RANK(p2->rank, (rpl_dag_t *)p1->dag) * NEIGHBOR_INFO_ETX_DIVISOR +
113  p2->link_metric;
114  /* Compare two parents by looking both and their rank and at the ETX
115  for that parent. We choose the parent that has the most
116  favourable combination. */
117 
118  dag = (rpl_dag_t *)p1->dag; /* Both parents must be in the same DAG. */
119  if(r1 < r2 + MIN_DIFFERENCE &&
120  r1 > r2 - MIN_DIFFERENCE) {
121  return dag->preferred_parent;
122  } else if(r1 < r2) {
123  return p1;
124  } else {
125  return p2;
126  }
127 }
128 
129 static void
130 update_metric_container(rpl_dag_t *dag)
131 {
132  dag->mc.type = RPL_DAG_MC_NONE;
133 }