2010-04-30 15:43:53 +02:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2010, Swedish Institute of Computer Science.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. Neither the name of the Institute nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
* This file is part of the Contiki operating system.
|
|
|
|
*/
|
2014-11-08 01:15:42 +01:00
|
|
|
|
2010-04-30 15:43:53 +02:00
|
|
|
/**
|
|
|
|
* \file
|
|
|
|
* RPL timer management.
|
|
|
|
*
|
|
|
|
* \author Joakim Eriksson <joakime@sics.se>, Nicolas Tsiftes <nvt@sics.se>
|
|
|
|
*/
|
|
|
|
|
2014-11-08 01:15:42 +01:00
|
|
|
/**
|
|
|
|
* \addtogroup uip6
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
2010-04-30 15:43:53 +02:00
|
|
|
#include "contiki-conf.h"
|
2011-02-11 16:21:17 +01:00
|
|
|
#include "net/rpl/rpl-private.h"
|
2016-02-01 18:02:33 +01:00
|
|
|
#include "net/rpl/rpl-ns.h"
|
2015-11-29 21:44:39 +01:00
|
|
|
#include "net/link-stats.h"
|
2011-12-09 13:15:04 +01:00
|
|
|
#include "net/ipv6/multicast/uip-mcast6.h"
|
2010-04-30 15:43:53 +02:00
|
|
|
#include "lib/random.h"
|
2010-06-14 14:44:37 +02:00
|
|
|
#include "sys/ctimer.h"
|
2010-04-30 15:43:53 +02:00
|
|
|
|
2010-11-03 16:41:23 +01:00
|
|
|
#define DEBUG DEBUG_NONE
|
2013-11-22 09:17:54 +01:00
|
|
|
#include "net/ip/uip-debug.h"
|
2010-04-30 15:43:53 +02:00
|
|
|
|
2015-09-21 18:22:45 +02:00
|
|
|
/* A configurable function called after update of the RPL DIO interval */
|
|
|
|
#ifdef RPL_CALLBACK_NEW_DIO_INTERVAL
|
|
|
|
void RPL_CALLBACK_NEW_DIO_INTERVAL(uint8_t dio_interval);
|
|
|
|
#endif /* RPL_CALLBACK_NEW_DIO_INTERVAL */
|
|
|
|
|
2015-11-29 21:46:41 +01:00
|
|
|
#ifdef RPL_PROBING_SELECT_FUNC
|
|
|
|
rpl_parent_t *RPL_PROBING_SELECT_FUNC(rpl_dag_t *dag);
|
|
|
|
#endif /* RPL_PROBING_SELECT_FUNC */
|
|
|
|
|
|
|
|
#ifdef RPL_PROBING_DELAY_FUNC
|
|
|
|
clock_time_t RPL_PROBING_DELAY_FUNC(rpl_dag_t *dag);
|
|
|
|
#endif /* RPL_PROBING_DELAY_FUNC */
|
|
|
|
|
2012-11-25 11:47:02 +01:00
|
|
|
/*---------------------------------------------------------------------------*/
|
2010-04-30 15:43:53 +02:00
|
|
|
static struct ctimer periodic_timer;
|
|
|
|
|
|
|
|
static void handle_periodic_timer(void *ptr);
|
2011-07-11 15:50:51 +02:00
|
|
|
static void new_dio_interval(rpl_instance_t *instance);
|
2010-04-30 15:43:53 +02:00
|
|
|
static void handle_dio_timer(void *ptr);
|
|
|
|
|
|
|
|
static uint16_t next_dis;
|
2010-05-09 19:52:37 +02:00
|
|
|
|
|
|
|
/* dio_send_ok is true if the node is ready to send DIOs */
|
|
|
|
static uint8_t dio_send_ok;
|
|
|
|
|
2012-11-25 11:47:02 +01:00
|
|
|
/*---------------------------------------------------------------------------*/
|
2010-04-30 15:43:53 +02:00
|
|
|
static void
|
|
|
|
handle_periodic_timer(void *ptr)
|
|
|
|
{
|
2016-02-01 18:02:33 +01:00
|
|
|
rpl_dag_t *dag = rpl_get_any_dag();
|
|
|
|
|
2015-09-30 09:39:44 +02:00
|
|
|
rpl_purge_dags();
|
2016-06-12 11:32:12 +02:00
|
|
|
if(dag != NULL) {
|
|
|
|
if(RPL_IS_STORING(dag->instance)) {
|
|
|
|
rpl_purge_routes();
|
|
|
|
}
|
|
|
|
if(RPL_IS_NON_STORING(dag->instance)) {
|
|
|
|
rpl_ns_periodic();
|
|
|
|
}
|
2016-02-01 18:02:33 +01:00
|
|
|
}
|
2010-06-14 14:44:37 +02:00
|
|
|
rpl_recalculate_ranks();
|
2010-04-30 15:43:53 +02:00
|
|
|
|
|
|
|
/* handle DIS */
|
2014-03-18 08:11:34 +01:00
|
|
|
#if RPL_DIS_SEND
|
2010-04-30 15:43:53 +02:00
|
|
|
next_dis++;
|
2016-02-01 18:02:33 +01:00
|
|
|
if(dag == NULL && next_dis >= RPL_DIS_INTERVAL) {
|
2010-05-05 00:55:32 +02:00
|
|
|
next_dis = 0;
|
|
|
|
dis_output(NULL);
|
|
|
|
}
|
2010-04-30 15:43:53 +02:00
|
|
|
#endif
|
|
|
|
ctimer_reset(&periodic_timer);
|
|
|
|
}
|
2012-11-25 11:47:02 +01:00
|
|
|
/*---------------------------------------------------------------------------*/
|
2010-04-30 15:43:53 +02:00
|
|
|
static void
|
2011-07-11 15:50:51 +02:00
|
|
|
new_dio_interval(rpl_instance_t *instance)
|
2010-04-30 15:43:53 +02:00
|
|
|
{
|
2010-12-15 13:12:27 +01:00
|
|
|
uint32_t time;
|
2012-11-26 19:35:39 +01:00
|
|
|
clock_time_t ticks;
|
2010-04-30 15:43:53 +02:00
|
|
|
|
2010-05-05 00:55:32 +02:00
|
|
|
/* TODO: too small timer intervals for many cases */
|
2011-07-11 15:50:51 +02:00
|
|
|
time = 1UL << instance->dio_intcurrent;
|
2010-05-05 00:55:32 +02:00
|
|
|
|
2010-12-15 13:12:27 +01:00
|
|
|
/* Convert from milliseconds to CLOCK_TICKS. */
|
2012-11-26 19:35:39 +01:00
|
|
|
ticks = (time * CLOCK_SECOND) / 1000;
|
|
|
|
instance->dio_next_delay = ticks;
|
2010-05-05 00:55:32 +02:00
|
|
|
|
2010-04-30 15:43:53 +02:00
|
|
|
/* random number between I/2 and I */
|
2012-11-26 19:35:39 +01:00
|
|
|
ticks = ticks / 2 + (ticks / 2 * (uint32_t)random_rand()) / RANDOM_RAND_MAX;
|
2010-05-05 00:55:32 +02:00
|
|
|
|
2010-12-15 13:12:27 +01:00
|
|
|
/*
|
|
|
|
* The intervals must be equally long among the nodes for Trickle to
|
|
|
|
* operate efficiently. Therefore we need to calculate the delay between
|
|
|
|
* the randomized time and the start time of the next interval.
|
|
|
|
*/
|
2012-11-26 19:35:39 +01:00
|
|
|
instance->dio_next_delay -= ticks;
|
2011-07-11 15:50:51 +02:00
|
|
|
instance->dio_send = 1;
|
2010-04-30 15:43:53 +02:00
|
|
|
|
|
|
|
#if RPL_CONF_STATS
|
|
|
|
/* keep some stats */
|
2011-07-11 15:50:51 +02:00
|
|
|
instance->dio_totint++;
|
|
|
|
instance->dio_totrecv += instance->dio_counter;
|
2010-10-28 22:39:06 +02:00
|
|
|
ANNOTATE("#A rank=%u.%u(%u),stats=%d %d %d %d,color=%s\n",
|
2011-07-11 15:50:51 +02:00
|
|
|
DAG_RANK(instance->current_dag->rank, instance),
|
|
|
|
(10 * (instance->current_dag->rank % instance->min_hoprankinc)) / instance->min_hoprankinc,
|
|
|
|
instance->current_dag->version,
|
|
|
|
instance->dio_totint, instance->dio_totsend,
|
|
|
|
instance->dio_totrecv,instance->dio_intcurrent,
|
|
|
|
instance->current_dag->rank == ROOT_RANK(instance) ? "BLUE" : "ORANGE");
|
2010-04-30 15:43:53 +02:00
|
|
|
#endif /* RPL_CONF_STATS */
|
|
|
|
|
|
|
|
/* reset the redundancy counter */
|
2011-07-11 15:50:51 +02:00
|
|
|
instance->dio_counter = 0;
|
2010-04-30 15:43:53 +02:00
|
|
|
|
|
|
|
/* schedule the timer */
|
2012-11-26 19:35:39 +01:00
|
|
|
PRINTF("RPL: Scheduling DIO timer %lu ticks in future (Interval)\n", ticks);
|
|
|
|
ctimer_set(&instance->dio_timer, ticks, &handle_dio_timer, instance);
|
2015-09-21 18:22:45 +02:00
|
|
|
|
|
|
|
#ifdef RPL_CALLBACK_NEW_DIO_INTERVAL
|
|
|
|
RPL_CALLBACK_NEW_DIO_INTERVAL(instance->dio_intcurrent);
|
|
|
|
#endif /* RPL_CALLBACK_NEW_DIO_INTERVAL */
|
2010-04-30 15:43:53 +02:00
|
|
|
}
|
2012-11-25 11:47:02 +01:00
|
|
|
/*---------------------------------------------------------------------------*/
|
2010-04-30 15:43:53 +02:00
|
|
|
static void
|
|
|
|
handle_dio_timer(void *ptr)
|
|
|
|
{
|
2011-07-11 15:50:51 +02:00
|
|
|
rpl_instance_t *instance;
|
2010-04-30 15:43:53 +02:00
|
|
|
|
2011-07-11 15:50:51 +02:00
|
|
|
instance = (rpl_instance_t *)ptr;
|
2010-04-30 15:43:53 +02:00
|
|
|
|
|
|
|
PRINTF("RPL: DIO Timer triggered\n");
|
2010-05-09 19:52:37 +02:00
|
|
|
if(!dio_send_ok) {
|
|
|
|
if(uip_ds6_get_link_local(ADDR_PREFERRED) != NULL) {
|
|
|
|
dio_send_ok = 1;
|
|
|
|
} else {
|
2010-06-14 14:44:37 +02:00
|
|
|
PRINTF("RPL: Postponing DIO transmission since link local address is not ok\n");
|
2011-07-11 15:50:51 +02:00
|
|
|
ctimer_set(&instance->dio_timer, CLOCK_SECOND, &handle_dio_timer, instance);
|
2010-05-09 19:52:37 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2010-04-30 15:43:53 +02:00
|
|
|
|
2011-07-11 15:50:51 +02:00
|
|
|
if(instance->dio_send) {
|
2010-04-30 15:43:53 +02:00
|
|
|
/* send DIO if counter is less than desired redundancy */
|
2014-09-10 16:57:27 +02:00
|
|
|
if(instance->dio_redundancy != 0 && instance->dio_counter < instance->dio_redundancy) {
|
2010-04-30 15:43:53 +02:00
|
|
|
#if RPL_CONF_STATS
|
2011-07-11 15:50:51 +02:00
|
|
|
instance->dio_totsend++;
|
2010-04-30 15:43:53 +02:00
|
|
|
#endif /* RPL_CONF_STATS */
|
2011-07-11 15:50:51 +02:00
|
|
|
dio_output(instance, NULL);
|
2010-04-30 15:43:53 +02:00
|
|
|
} else {
|
2016-03-09 11:32:02 +01:00
|
|
|
PRINTF("RPL: Suppressing DIO transmission (%d >= %d)\n",
|
2011-07-11 15:50:51 +02:00
|
|
|
instance->dio_counter, instance->dio_redundancy);
|
2010-04-30 15:43:53 +02:00
|
|
|
}
|
2011-07-11 15:50:51 +02:00
|
|
|
instance->dio_send = 0;
|
2012-01-04 11:22:11 +01:00
|
|
|
PRINTF("RPL: Scheduling DIO timer %lu ticks in future (sent)\n",
|
2011-07-11 15:50:51 +02:00
|
|
|
instance->dio_next_delay);
|
|
|
|
ctimer_set(&instance->dio_timer, instance->dio_next_delay, handle_dio_timer, instance);
|
2010-04-30 15:43:53 +02:00
|
|
|
} else {
|
|
|
|
/* check if we need to double interval */
|
2011-07-11 15:50:51 +02:00
|
|
|
if(instance->dio_intcurrent < instance->dio_intmin + instance->dio_intdoubl) {
|
|
|
|
instance->dio_intcurrent++;
|
|
|
|
PRINTF("RPL: DIO Timer interval doubled %d\n", instance->dio_intcurrent);
|
2010-04-30 15:43:53 +02:00
|
|
|
}
|
2011-07-11 15:50:51 +02:00
|
|
|
new_dio_interval(instance);
|
2010-04-30 15:43:53 +02:00
|
|
|
}
|
2015-05-06 11:34:52 +02:00
|
|
|
|
|
|
|
#if DEBUG
|
|
|
|
rpl_print_neighbor_list();
|
|
|
|
#endif
|
2010-04-30 15:43:53 +02:00
|
|
|
}
|
2012-11-25 11:47:02 +01:00
|
|
|
/*---------------------------------------------------------------------------*/
|
2010-04-30 15:43:53 +02:00
|
|
|
void
|
|
|
|
rpl_reset_periodic_timer(void)
|
|
|
|
{
|
2012-11-26 19:36:51 +01:00
|
|
|
next_dis = RPL_DIS_INTERVAL / 2 +
|
|
|
|
((uint32_t)RPL_DIS_INTERVAL * (uint32_t)random_rand()) / RANDOM_RAND_MAX -
|
|
|
|
RPL_DIS_START_DELAY;
|
2010-04-30 15:43:53 +02:00
|
|
|
ctimer_set(&periodic_timer, CLOCK_SECOND, handle_periodic_timer, NULL);
|
|
|
|
}
|
2012-11-25 11:47:02 +01:00
|
|
|
/*---------------------------------------------------------------------------*/
|
2011-07-11 15:50:51 +02:00
|
|
|
/* Resets the DIO timer in the instance to its minimal interval. */
|
2010-04-30 15:43:53 +02:00
|
|
|
void
|
2012-01-04 11:22:11 +01:00
|
|
|
rpl_reset_dio_timer(rpl_instance_t *instance)
|
2010-04-30 15:43:53 +02:00
|
|
|
{
|
2011-07-11 15:50:51 +02:00
|
|
|
#if !RPL_LEAF_ONLY
|
2011-11-16 09:49:22 +01:00
|
|
|
/* Do not reset if we are already on the minimum interval,
|
|
|
|
unless forced to do so. */
|
2012-01-04 11:22:11 +01:00
|
|
|
if(instance->dio_intcurrent > instance->dio_intmin) {
|
2011-07-11 15:50:51 +02:00
|
|
|
instance->dio_counter = 0;
|
|
|
|
instance->dio_intcurrent = instance->dio_intmin;
|
|
|
|
new_dio_interval(instance);
|
2010-04-30 15:43:53 +02:00
|
|
|
}
|
2010-10-28 22:39:06 +02:00
|
|
|
#if RPL_CONF_STATS
|
|
|
|
rpl_stats.resets++;
|
2011-07-11 15:50:51 +02:00
|
|
|
#endif /* RPL_CONF_STATS */
|
|
|
|
#endif /* RPL_LEAF_ONLY */
|
2010-04-30 15:43:53 +02:00
|
|
|
}
|
2012-11-25 11:47:02 +01:00
|
|
|
/*---------------------------------------------------------------------------*/
|
2013-11-20 14:02:38 +01:00
|
|
|
static void handle_dao_timer(void *ptr);
|
|
|
|
static void
|
|
|
|
set_dao_lifetime_timer(rpl_instance_t *instance)
|
|
|
|
{
|
|
|
|
if(rpl_get_mode() == RPL_MODE_FEATHER) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set up another DAO within half the expiration time, if such a
|
|
|
|
time has been configured */
|
|
|
|
if(instance->lifetime_unit != 0xffff && instance->default_lifetime != 0xff) {
|
|
|
|
clock_time_t expiration_time;
|
|
|
|
expiration_time = (clock_time_t)instance->default_lifetime *
|
|
|
|
(clock_time_t)instance->lifetime_unit *
|
|
|
|
CLOCK_SECOND / 2;
|
2015-09-20 21:02:30 +02:00
|
|
|
/* make the time for the re registration be betwen 1/2 - 3/4 of lifetime */
|
|
|
|
expiration_time = expiration_time + (random_rand() % (expiration_time / 2));
|
2013-11-20 14:02:38 +01:00
|
|
|
PRINTF("RPL: Scheduling DAO lifetime timer %u ticks in the future\n",
|
|
|
|
(unsigned)expiration_time);
|
|
|
|
ctimer_set(&instance->dao_lifetime_timer, expiration_time,
|
|
|
|
handle_dao_timer, instance);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2010-04-30 15:43:53 +02:00
|
|
|
static void
|
|
|
|
handle_dao_timer(void *ptr)
|
|
|
|
{
|
2011-07-11 15:50:51 +02:00
|
|
|
rpl_instance_t *instance;
|
2011-12-09 13:15:04 +01:00
|
|
|
#if RPL_CONF_MULTICAST
|
|
|
|
uip_mcast6_route_t *mcast_route;
|
|
|
|
uint8_t i;
|
|
|
|
#endif
|
2010-05-25 23:58:54 +02:00
|
|
|
|
2011-07-11 15:50:51 +02:00
|
|
|
instance = (rpl_instance_t *)ptr;
|
2010-05-09 21:12:47 +02:00
|
|
|
|
2011-11-16 09:49:22 +01:00
|
|
|
if(!dio_send_ok && uip_ds6_get_link_local(ADDR_PREFERRED) == NULL) {
|
|
|
|
PRINTF("RPL: Postpone DAO transmission\n");
|
2011-07-11 15:50:51 +02:00
|
|
|
ctimer_set(&instance->dao_timer, CLOCK_SECOND, handle_dao_timer, instance);
|
2010-05-09 21:12:47 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-06-28 17:08:44 +02:00
|
|
|
/* Send the DAO to the DAO parent set -- the preferred parent in our case. */
|
2011-07-11 15:50:51 +02:00
|
|
|
if(instance->current_dag->preferred_parent != NULL) {
|
2010-04-30 15:43:53 +02:00
|
|
|
PRINTF("RPL: handle_dao_timer - sending DAO\n");
|
2011-06-28 17:08:44 +02:00
|
|
|
/* Set the route lifetime to the default value. */
|
2011-07-11 15:50:51 +02:00
|
|
|
dao_output(instance->current_dag->preferred_parent, instance->default_lifetime);
|
2011-12-09 13:15:04 +01:00
|
|
|
|
|
|
|
#if RPL_CONF_MULTICAST
|
|
|
|
/* Send DAOs for multicast prefixes only if the instance is in MOP 3 */
|
|
|
|
if(instance->mop == RPL_MOP_STORING_MULTICAST) {
|
|
|
|
/* Send a DAO for own multicast addresses */
|
|
|
|
for(i = 0; i < UIP_DS6_MADDR_NB; i++) {
|
|
|
|
if(uip_ds6_if.maddr_list[i].isused
|
|
|
|
&& uip_is_addr_mcast_global(&uip_ds6_if.maddr_list[i].ipaddr)) {
|
|
|
|
dao_output_target(instance->current_dag->preferred_parent,
|
|
|
|
&uip_ds6_if.maddr_list[i].ipaddr, RPL_MCAST_LIFETIME);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Iterate over multicast routes and send DAOs */
|
|
|
|
mcast_route = uip_mcast6_route_list_head();
|
|
|
|
while(mcast_route != NULL) {
|
|
|
|
/* Don't send if it's also our own address, done that already */
|
|
|
|
if(uip_ds6_maddr_lookup(&mcast_route->group) == NULL) {
|
|
|
|
dao_output_target(instance->current_dag->preferred_parent,
|
|
|
|
&mcast_route->group, RPL_MCAST_LIFETIME);
|
|
|
|
}
|
|
|
|
mcast_route = list_item_next(mcast_route);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2010-05-09 21:12:47 +02:00
|
|
|
} else {
|
2011-06-28 17:08:44 +02:00
|
|
|
PRINTF("RPL: No suitable DAO parent\n");
|
2010-04-30 15:43:53 +02:00
|
|
|
}
|
2013-11-20 14:02:38 +01:00
|
|
|
|
2011-07-11 15:50:51 +02:00
|
|
|
ctimer_stop(&instance->dao_timer);
|
2013-11-20 14:02:38 +01:00
|
|
|
|
|
|
|
if(etimer_expired(&instance->dao_lifetime_timer.etimer)) {
|
|
|
|
set_dao_lifetime_timer(instance);
|
|
|
|
}
|
2010-04-30 15:43:53 +02:00
|
|
|
}
|
2012-11-25 11:47:02 +01:00
|
|
|
/*---------------------------------------------------------------------------*/
|
2013-11-20 14:02:38 +01:00
|
|
|
static void
|
|
|
|
schedule_dao(rpl_instance_t *instance, clock_time_t latency)
|
2010-04-30 15:43:53 +02:00
|
|
|
{
|
|
|
|
clock_time_t expiration_time;
|
2010-05-24 18:38:56 +02:00
|
|
|
|
2013-11-20 14:10:39 +01:00
|
|
|
if(rpl_get_mode() == RPL_MODE_FEATHER) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-07-11 15:50:51 +02:00
|
|
|
expiration_time = etimer_expiration_time(&instance->dao_timer.etimer);
|
2010-04-30 15:43:53 +02:00
|
|
|
|
2011-07-11 15:50:51 +02:00
|
|
|
if(!etimer_expired(&instance->dao_timer.etimer)) {
|
2010-04-30 15:43:53 +02:00
|
|
|
PRINTF("RPL: DAO timer already scheduled\n");
|
|
|
|
} else {
|
2013-11-20 14:02:38 +01:00
|
|
|
if(latency != 0) {
|
|
|
|
expiration_time = latency / 2 +
|
|
|
|
(random_rand() % (latency));
|
|
|
|
} else {
|
|
|
|
expiration_time = 0;
|
|
|
|
}
|
2010-12-17 16:24:25 +01:00
|
|
|
PRINTF("RPL: Scheduling DAO timer %u ticks in the future\n",
|
|
|
|
(unsigned)expiration_time);
|
2011-07-11 15:50:51 +02:00
|
|
|
ctimer_set(&instance->dao_timer, expiration_time,
|
|
|
|
handle_dao_timer, instance);
|
2013-11-20 14:02:38 +01:00
|
|
|
|
|
|
|
set_dao_lifetime_timer(instance);
|
2010-04-30 15:43:53 +02:00
|
|
|
}
|
|
|
|
}
|
2012-11-25 11:47:02 +01:00
|
|
|
/*---------------------------------------------------------------------------*/
|
2013-11-20 14:02:38 +01:00
|
|
|
void
|
|
|
|
rpl_schedule_dao(rpl_instance_t *instance)
|
|
|
|
{
|
2015-11-18 14:06:18 +01:00
|
|
|
schedule_dao(instance, RPL_DAO_DELAY);
|
2013-11-20 14:02:38 +01:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
void
|
|
|
|
rpl_schedule_dao_immediately(rpl_instance_t *instance)
|
|
|
|
{
|
|
|
|
schedule_dao(instance, 0);
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
void
|
|
|
|
rpl_cancel_dao(rpl_instance_t *instance)
|
|
|
|
{
|
|
|
|
ctimer_stop(&instance->dao_timer);
|
|
|
|
ctimer_stop(&instance->dao_lifetime_timer);
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2015-09-17 22:42:19 +02:00
|
|
|
static void
|
|
|
|
handle_unicast_dio_timer(void *ptr)
|
|
|
|
{
|
|
|
|
rpl_instance_t *instance = (rpl_instance_t *)ptr;
|
|
|
|
uip_ipaddr_t *target_ipaddr = rpl_get_parent_ipaddr(instance->unicast_dio_target);
|
|
|
|
|
|
|
|
if(target_ipaddr != NULL) {
|
|
|
|
dio_output(instance, target_ipaddr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
void
|
|
|
|
rpl_schedule_unicast_dio_immediately(rpl_instance_t *instance)
|
|
|
|
{
|
|
|
|
ctimer_set(&instance->unicast_dio_timer, 0,
|
|
|
|
handle_unicast_dio_timer, instance);
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2015-05-06 11:35:42 +02:00
|
|
|
#if RPL_WITH_PROBING
|
2015-11-29 21:46:41 +01:00
|
|
|
clock_time_t
|
|
|
|
get_probing_delay(rpl_dag_t *dag)
|
|
|
|
{
|
|
|
|
if(dag != NULL && dag->instance != NULL
|
|
|
|
&& dag->instance->urgent_probing_target != NULL) {
|
|
|
|
/* Urgent probing needed (to find out if a neighbor may become preferred parent) */
|
|
|
|
return random_rand() % (CLOCK_SECOND * 10);
|
|
|
|
} else {
|
|
|
|
/* Else, use normal probing interval */
|
|
|
|
return ((RPL_PROBING_INTERVAL) / 2) + random_rand() % (RPL_PROBING_INTERVAL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
rpl_parent_t *
|
2015-05-06 11:35:42 +02:00
|
|
|
get_probing_target(rpl_dag_t *dag)
|
|
|
|
{
|
2015-11-29 21:46:41 +01:00
|
|
|
/* Returns the next probing target. The current implementation probes the urgent
|
|
|
|
* probing target if any, or the preferred parent if its link statistics need refresh.
|
2015-05-08 10:19:14 +02:00
|
|
|
* Otherwise, it picks at random between:
|
2015-11-29 21:46:41 +01:00
|
|
|
* (1) selecting the best parent with non-fresh link statistics
|
2015-05-08 10:19:14 +02:00
|
|
|
* (2) selecting the least recently updated parent
|
|
|
|
*/
|
2015-05-06 11:35:42 +02:00
|
|
|
|
|
|
|
rpl_parent_t *p;
|
2015-05-08 10:19:14 +02:00
|
|
|
rpl_parent_t *probing_target = NULL;
|
|
|
|
rpl_rank_t probing_target_rank = INFINITE_RANK;
|
2015-11-29 21:46:41 +01:00
|
|
|
clock_time_t probing_target_age = 0;
|
|
|
|
clock_time_t clock_now = clock_time();
|
2015-05-06 11:35:42 +02:00
|
|
|
|
|
|
|
if(dag == NULL ||
|
2015-11-29 21:46:41 +01:00
|
|
|
dag->instance == NULL) {
|
2015-05-06 11:35:42 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2015-11-29 21:46:41 +01:00
|
|
|
/* There is an urgent probing target */
|
|
|
|
if(dag->instance->urgent_probing_target != NULL) {
|
|
|
|
return dag->instance->urgent_probing_target;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* The preferred parent needs probing */
|
|
|
|
if(dag->preferred_parent != NULL && !rpl_parent_is_fresh(dag->preferred_parent)) {
|
|
|
|
return dag->preferred_parent;
|
2015-05-06 11:35:42 +02:00
|
|
|
}
|
|
|
|
|
2015-11-29 21:46:41 +01:00
|
|
|
/* With 50% probability: probe best non-fresh parent */
|
|
|
|
if(random_rand() % 2 == 0) {
|
2015-05-08 10:19:14 +02:00
|
|
|
p = nbr_table_head(rpl_parents);
|
|
|
|
while(p != NULL) {
|
2015-11-29 21:46:41 +01:00
|
|
|
if(p->dag == dag && !rpl_parent_is_fresh(p)) {
|
2015-05-08 10:19:14 +02:00
|
|
|
/* p is in our dag and needs probing */
|
2015-11-29 21:46:41 +01:00
|
|
|
rpl_rank_t p_rank = rpl_rank_via_parent(p);
|
2015-05-08 10:19:14 +02:00
|
|
|
if(probing_target == NULL
|
|
|
|
|| p_rank < probing_target_rank) {
|
2015-05-06 11:35:42 +02:00
|
|
|
probing_target = p;
|
|
|
|
probing_target_rank = p_rank;
|
|
|
|
}
|
|
|
|
}
|
2015-05-08 10:19:14 +02:00
|
|
|
p = nbr_table_next(rpl_parents, p);
|
2015-05-06 11:35:42 +02:00
|
|
|
}
|
2015-05-10 13:28:00 +02:00
|
|
|
}
|
|
|
|
|
2015-11-29 21:46:41 +01:00
|
|
|
/* If we still do not have a probing target: pick the least recently updated parent */
|
2015-05-10 13:28:00 +02:00
|
|
|
if(probing_target == NULL) {
|
2015-05-08 10:19:14 +02:00
|
|
|
p = nbr_table_head(rpl_parents);
|
|
|
|
while(p != NULL) {
|
2015-11-29 21:46:41 +01:00
|
|
|
const struct link_stats *stats =rpl_get_parent_link_stats(p);
|
|
|
|
if(p->dag == dag && stats != NULL) {
|
2015-05-08 10:19:14 +02:00
|
|
|
if(probing_target == NULL
|
2015-11-29 21:46:41 +01:00
|
|
|
|| clock_now - stats->last_tx_time > probing_target_age) {
|
2015-05-08 10:19:14 +02:00
|
|
|
probing_target = p;
|
2015-11-29 21:46:41 +01:00
|
|
|
probing_target_age = clock_now - stats->last_tx_time;
|
2015-05-08 10:19:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
p = nbr_table_next(rpl_parents, p);
|
|
|
|
}
|
2015-05-06 11:35:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return probing_target;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static void
|
|
|
|
handle_probing_timer(void *ptr)
|
|
|
|
{
|
|
|
|
rpl_instance_t *instance = (rpl_instance_t *)ptr;
|
|
|
|
rpl_parent_t *probing_target = RPL_PROBING_SELECT_FUNC(instance->current_dag);
|
2015-09-17 22:42:59 +02:00
|
|
|
uip_ipaddr_t *target_ipaddr = rpl_get_parent_ipaddr(probing_target);
|
2015-05-06 11:35:42 +02:00
|
|
|
|
|
|
|
/* Perform probing */
|
2015-09-17 22:42:59 +02:00
|
|
|
if(target_ipaddr != NULL) {
|
2016-02-02 17:16:03 +01:00
|
|
|
const struct link_stats *stats = rpl_get_parent_link_stats(probing_target);
|
|
|
|
(void)stats;
|
2015-11-29 21:46:41 +01:00
|
|
|
PRINTF("RPL: probing %u %s last tx %u min ago\n",
|
2015-11-29 21:48:53 +01:00
|
|
|
rpl_get_parent_lladdr(probing_target)->u8[7],
|
2015-11-29 21:46:41 +01:00
|
|
|
instance->urgent_probing_target != NULL ? "(urgent)" : "",
|
2016-02-02 17:16:03 +01:00
|
|
|
probing_target != NULL ?
|
|
|
|
(unsigned)((clock_time() - stats->last_tx_time) / (60 * CLOCK_SECOND)) : 0
|
2015-11-29 21:46:41 +01:00
|
|
|
);
|
|
|
|
/* Send probe, e.g. unicast DIO or DIS */
|
2015-09-17 22:42:59 +02:00
|
|
|
RPL_PROBING_SEND_FUNC(instance, target_ipaddr);
|
2015-11-29 21:46:41 +01:00
|
|
|
instance->urgent_probing_target = NULL;
|
2015-05-06 11:35:42 +02:00
|
|
|
}
|
2014-05-30 11:01:20 +02:00
|
|
|
|
2015-05-06 11:35:42 +02:00
|
|
|
/* Schedule next probing */
|
|
|
|
rpl_schedule_probing(instance);
|
|
|
|
|
|
|
|
#if DEBUG
|
|
|
|
rpl_print_neighbor_list();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
void
|
|
|
|
rpl_schedule_probing(rpl_instance_t *instance)
|
|
|
|
{
|
2015-11-29 21:46:41 +01:00
|
|
|
ctimer_set(&instance->probing_timer, RPL_PROBING_DELAY_FUNC(instance->current_dag),
|
2015-05-06 11:35:42 +02:00
|
|
|
handle_probing_timer, instance);
|
|
|
|
}
|
|
|
|
#endif /* RPL_WITH_PROBING */
|
2014-05-30 11:01:20 +02:00
|
|
|
/** @}*/
|