Contiki 2.5
rpl-timers.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  * RPL timer management.
38  *
39  * \author Joakim Eriksson <joakime@sics.se>, Nicolas Tsiftes <nvt@sics.se>
40  */
41 
42 #include "contiki-conf.h"
43 #include "net/rpl/rpl-private.h"
44 #include "lib/random.h"
45 #include "sys/ctimer.h"
46 
47 #define DEBUG DEBUG_NONE
48 #include "net/uip-debug.h"
49 
50 /************************************************************************/
51 static struct ctimer periodic_timer;
52 
53 static void handle_periodic_timer(void *ptr);
54 static void new_dio_interval(rpl_dag_t *dag);
55 static void handle_dio_timer(void *ptr);
56 
57 static uint16_t next_dis;
58 
59 /* dio_send_ok is true if the node is ready to send DIOs */
60 static uint8_t dio_send_ok;
61 
62 /************************************************************************/
63 static void
64 handle_periodic_timer(void *ptr)
65 {
66  rpl_purge_routes();
67  rpl_recalculate_ranks();
68 
69  /* handle DIS */
70 #ifdef RPL_DIS_SEND
71  next_dis++;
72  if(rpl_get_dag(RPL_ANY_INSTANCE) == NULL && next_dis >= RPL_DIS_INTERVAL) {
73  next_dis = 0;
74  dis_output(NULL);
75  }
76 #endif
77  ctimer_reset(&periodic_timer);
78 }
79 /************************************************************************/
80 static void
81 new_dio_interval(rpl_dag_t *dag)
82 {
83  uint32_t time;
84 
85  /* TODO: too small timer intervals for many cases */
86  time = 1UL << dag->dio_intcurrent;
87 
88  /* Convert from milliseconds to CLOCK_TICKS. */
89  time = (time * CLOCK_SECOND) / 1000;
90 
91  dag->dio_next_delay = time;
92 
93  /* random number between I/2 and I */
94  time = time >> 1;
95  time += (time * random_rand()) / RANDOM_RAND_MAX;
96 
97  /*
98  * The intervals must be equally long among the nodes for Trickle to
99  * operate efficiently. Therefore we need to calculate the delay between
100  * the randomized time and the start time of the next interval.
101  */
102  dag->dio_next_delay -= time;
103  dag->dio_send = 1;
104 
105 #if RPL_CONF_STATS
106  /* keep some stats */
107  dag->dio_totint++;
108  dag->dio_totrecv += dag->dio_counter;
109  ANNOTATE("#A rank=%u.%u(%u),stats=%d %d %d %d,color=%s\n",
110  DAG_RANK(dag->rank, dag),
111  (10 * (dag->rank % dag->min_hoprankinc)) / dag->min_hoprankinc,
112  dag->version,
113  dag->dio_totint, dag->dio_totsend,
114  dag->dio_totrecv,dag->dio_intcurrent,
115  dag->rank == ROOT_RANK(dag) ? "BLUE" : "ORANGE");
116 #endif /* RPL_CONF_STATS */
117 
118  /* reset the redundancy counter */
119  dag->dio_counter = 0;
120 
121  /* schedule the timer */
122  PRINTF("RPL: Scheduling DIO timer %lu ticks in future (Interval)\n", time);
123  ctimer_set(&dag->dio_timer, time, &handle_dio_timer, dag);
124 }
125 /************************************************************************/
126 static void
127 handle_dio_timer(void *ptr)
128 {
129  rpl_dag_t *dag;
130 
131  dag = (rpl_dag_t *)ptr;
132 
133  PRINTF("RPL: DIO Timer triggered\n");
134  if(!dio_send_ok) {
135  if(uip_ds6_get_link_local(ADDR_PREFERRED) != NULL) {
136  dio_send_ok = 1;
137  } else {
138  PRINTF("RPL: Postponing DIO transmission since link local address is not ok\n");
139  ctimer_set(&dag->dio_timer, CLOCK_SECOND, &handle_dio_timer, dag);
140  return;
141  }
142  }
143 
144  if(dag->dio_send) {
145  /* send DIO if counter is less than desired redundancy */
146  if(dag->dio_counter < dag->dio_redundancy) {
147 #if RPL_CONF_STATS
148  dag->dio_totsend++;
149 #endif /* RPL_CONF_STATS */
150  dio_output(dag, NULL);
151  } else {
152  PRINTF("RPL: Supressing DIO transmission (%d >= %d)\n",
153  dag->dio_counter, dag->dio_redundancy);
154  }
155  dag->dio_send = 0;
156  PRINTF("RPL: Scheduling DIO timer %u ticks in future (sent)\n",
157  dag->dio_next_delay);
158  ctimer_set(&dag->dio_timer, dag->dio_next_delay, handle_dio_timer, dag);
159  } else {
160  /* check if we need to double interval */
161  if(dag->dio_intcurrent < dag->dio_intmin + dag->dio_intdoubl) {
162  dag->dio_intcurrent++;
163  PRINTF("RPL: DIO Timer interval doubled %d\n", dag->dio_intcurrent);
164  }
165  new_dio_interval(dag);
166  }
167 }
168 /************************************************************************/
169 void
170 rpl_reset_periodic_timer(void)
171 {
172  next_dis = RPL_DIS_INTERVAL - RPL_DIS_START_DELAY;
173  ctimer_set(&periodic_timer, CLOCK_SECOND, handle_periodic_timer, NULL);
174 }
175 /************************************************************************/
176 /* Resets the DIO timer in the DAG to its minimal interval. */
177 void
178 rpl_reset_dio_timer(rpl_dag_t *dag, uint8_t force)
179 {
180  /* only reset if not just reset or started */
181  if(force || dag->dio_intcurrent > dag->dio_intmin) {
182  dag->dio_counter = 0;
183  dag->dio_intcurrent = dag->dio_intmin;
184  new_dio_interval(dag);
185  }
186 #if RPL_CONF_STATS
187  rpl_stats.resets++;
188 #endif
189 }
190 /************************************************************************/
191 static void
192 handle_dao_timer(void *ptr)
193 {
194  rpl_dag_t *dag;
195 
196  dag = (rpl_dag_t *)ptr;
197 
198  if (!dio_send_ok && uip_ds6_get_link_local(ADDR_PREFERRED) == NULL) {
199  PRINTF("RPL: Postpone DAO transmission... \n");
200  ctimer_set(&dag->dao_timer, CLOCK_SECOND, handle_dao_timer, dag);
201  return;
202  }
203 
204  /* Send the DAO to the DAO parent set -- the preferred parent in our case. */
205  if(dag->preferred_parent != NULL) {
206  PRINTF("RPL: handle_dao_timer - sending DAO\n");
207  /* Set the route lifetime to the default value. */
208  dao_output(dag->preferred_parent, dag->default_lifetime);
209  } else {
210  PRINTF("RPL: No suitable DAO parent\n");
211  }
212  ctimer_stop(&dag->dao_timer);
213 }
214 /************************************************************************/
215 void
216 rpl_schedule_dao(rpl_dag_t *dag)
217 {
218  clock_time_t expiration_time;
219 
220  expiration_time = etimer_expiration_time(&dag->dao_timer.etimer);
221 
222  if(!etimer_expired(&dag->dao_timer.etimer)) {
223  PRINTF("RPL: DAO timer already scheduled\n");
224  } else {
225  expiration_time = DEFAULT_DAO_LATENCY / 2 +
226  (random_rand() % (DEFAULT_DAO_LATENCY));
227  PRINTF("RPL: Scheduling DAO timer %u ticks in the future\n",
228  (unsigned)expiration_time);
229  ctimer_set(&dag->dao_timer, expiration_time,
230  handle_dao_timer, dag);
231  }
232 }
233 /************************************************************************/