rpl_dag_root module simplification

This commit is contained in:
Simon Duquennoy 2017-12-09 04:36:14 -08:00
parent 3a39b51056
commit 14ccd337c9
25 changed files with 42 additions and 220 deletions

View File

@ -77,7 +77,7 @@ PROCESS_THREAD(node_process, ev, data)
#endif
if(is_coordinator) {
rpl_dag_root_init_dag_immediately();
rpl_dag_root_start();
}
NETSTACK_MAC.on();

View File

@ -65,7 +65,7 @@ PROCESS_THREAD(node_process, ev, data)
#endif
if(is_coordinator) {
rpl_dag_root_init_dag_immediately();
rpl_dag_root_start();
}
NETSTACK_MAC.on();

View File

@ -73,7 +73,7 @@ PROCESS_THREAD(node_process, ev, data)
#endif
if(is_coordinator) {
rpl_dag_root_init_dag_immediately();
rpl_dag_root_start();
}
NETSTACK_MAC.on();

View File

@ -14,7 +14,7 @@ PROCESS_THREAD(router_node_process, ev, data)
PROCESS_BEGIN();
/* Set us up as a RPL root node. */
rpl_dag_root_init_dag_delay();
rpl_dag_root_start();
/* Initialize the IP64 module so we'll start translating packets */
ip64_init();

View File

@ -236,8 +236,8 @@ setup_network(void)
uip_ds6_set_addr_iid(&ipaddr, &uip_lladdr);
#endif
rpl_dag_root_init(&ipaddr, &ipaddr);
rpl_dag_root_init_dag_immediately();
rpl_dag_root_set_prefix(&ipaddr, &ipaddr);
rpl_dag_root_start();
#endif /* UIP_CONF_ROUTER */
PRINTF("IPv6 addresses: ");

View File

@ -111,7 +111,7 @@ PROCESS_THREAD(rpl_root_process, ev, data)
PRINTF("Multicast Engine: '%s'\n", UIP_MCAST6.name);
rpl_dag_root_init_dag_immediately();
rpl_dag_root_start();
prepare_mcast();

View File

@ -129,7 +129,7 @@ PROCESS_THREAD(start_app, ev, data)
/* Start net stack */
if(is_coordinator) {
rpl_dag_root_init_dag_immediately();
rpl_dag_root_start();
}
NETSTACK_MAC.on();
printf("Starting RPL node\n");

View File

@ -349,7 +349,7 @@ PROCESS_THREAD(start_app, ev, data)
/* Start net stack */
if(is_coordinator) {
rpl_dag_root_init_dag_immediately();
rpl_dag_root_start();
}
NETSTACK_MAC.on();
printf("Starting RPL node\n");

View File

@ -350,7 +350,7 @@ PROCESS_THREAD(start_app, ev, data)
/* Start net stack */
if(is_coordinator) {
rpl_dag_root_init_dag_immediately();
rpl_dag_root_start();
}
NETSTACK_MAC.on();
printf("Starting RPL node\n");

View File

@ -96,7 +96,7 @@ PROCESS_THREAD(node_process, ev, data)
is_coordinator = node_role > role_6ln;
if(is_coordinator) {
rpl_dag_root_init_dag_immediately();
rpl_dag_root_start();
}
NETSTACK_MAC.on();

View File

@ -169,7 +169,7 @@ PROCESS_THREAD(node_process, ev, data)
is_coordinator = node_role > role_6ln;
if(is_coordinator) {
rpl_dag_root_init_dag_immediately();
rpl_dag_root_start();
}
NETSTACK_MAC.on();

View File

@ -104,7 +104,7 @@ PROCESS_THREAD(start_app, ev, data)
/* Start network stack */
if(is_coordinator) {
rpl_dag_root_init_dag_immediately();
rpl_dag_root_start();
}
NETSTACK_MAC.on();
printf("Starting RPL node\n");

View File

@ -156,8 +156,8 @@ PROCESS_THREAD(border_router_process, ev, data)
uip_debug_ipaddr_print(&prefix);
PRINTF("\n");
rpl_dag_root_init(&prefix, NULL);
rpl_dag_root_init_dag_immediately();
rpl_dag_root_set_prefix(&prefix, NULL);
rpl_dag_root_start();
coap_engine_init();
coap_activate_resource(&resource_get_rssi, "Get-RSSI");

View File

@ -145,7 +145,7 @@ PROCESS_THREAD(start_app, ev, data)
/* Start network stack */
if(is_coordinator) {
rpl_dag_root_init_dag_immediately();
rpl_dag_root_start();
}
NETSTACK_MAC.on();
printf("Starting RPL node\n");

View File

@ -247,7 +247,7 @@ PT_THREAD(generate_routes(struct httpd_state *s))
#if WEBSERVER_CONF_LOADTIME
numticks = clock_time() - numticks + 1;
ADD(" <i>(%u.%02u sec)</i>", numticks / CLOCK_SECOND,
ADD(" <i>(%u.%02u sec)</i>", numticks / CLOCK_SECOND,
(100 * (numticks % CLOCK_SECOND)) / CLOCK_SECOND));
#endif
@ -307,7 +307,7 @@ PROCESS_THREAD(router_node_process, ev, data)
leds_off(LEDS_DHCP);
/* Set us up as a RPL root node. */
rpl_dag_root_init_dag_delay();
rpl_dag_root_start();
/* ... and do nothing more. */
while(1) {

View File

@ -71,7 +71,7 @@ PROCESS_THREAD(udp_server_process, ev, data)
PROCESS_BEGIN();
/* Initialize DAG root */
rpl_dag_root_init_dag_immediately();
rpl_dag_root_start();
/* Initialize UDP connection */
simple_udp_register(&udp_conn, UDP_SERVER_PORT, NULL,

View File

@ -41,105 +41,6 @@
#define DEBUG DEBUG_NONE
#include "net/ipv6/uip-debug.h"
#define RPL_DAG_GRACE_PERIOD (CLOCK_SECOND * 20 * 1)
#if (UIP_MAX_ROUTES != 0)
static struct uip_ds6_notification n;
#endif /* (UIP_MAX_ROUTES != 0) */
static uint8_t to_become_root;
static struct ctimer c;
/*---------------------------------------------------------------------------*/
static const uip_ipaddr_t *
dag_root(void)
{
rpl_dag_t *dag;
dag = rpl_get_any_dag();
if(dag != NULL) {
return &dag->dag_id;
}
return NULL;
}
/*---------------------------------------------------------------------------*/
static const uip_ipaddr_t *
get_global_address(void)
{
int i;
uint8_t state;
uip_ipaddr_t *ipaddr = NULL;
for(i = 0; i < UIP_DS6_ADDR_NB; i++) {
state = uip_ds6_if.addr_list[i].state;
if(uip_ds6_if.addr_list[i].isused &&
state == ADDR_PREFERRED &&
!uip_is_addr_linklocal(&uip_ds6_if.addr_list[i].ipaddr)) {
ipaddr = &uip_ds6_if.addr_list[i].ipaddr;
}
}
return ipaddr;
}
/*---------------------------------------------------------------------------*/
static void
create_dag_callback(void *ptr)
{
const uip_ipaddr_t *root, *ipaddr;
root = dag_root();
ipaddr = get_global_address();
if(root == NULL || uip_ipaddr_cmp(root, ipaddr)) {
/* The RPL network we are joining is one that we created, so we
become root. */
if(to_become_root) {
rpl_dag_root_init_dag_immediately();
to_become_root = 0;
}
} else {
rpl_dag_t *dag;
dag = rpl_get_any_dag();
#if DEBUG
printf("Found a network we did not create\n");
printf("version %d grounded %d preference %d used %d joined %d rank %d\n",
dag->version, dag->grounded,
dag->preference, dag->used,
dag->joined, dag->rank);
#endif /* DEBUG */
/* We found a RPL network that we did not create so we just join
it without becoming root. But if the network has an infinite
rank, we assume the network has broken, and we become the new
root of the network. */
if(dag->rank == RPL_INFINITE_RANK) {
if(to_become_root) {
rpl_dag_root_init_dag_immediately();
to_become_root = 0;
}
}
/* Try again after the grace period */
ctimer_set(&c, RPL_DAG_GRACE_PERIOD, create_dag_callback, NULL);
}
}
#if (UIP_MAX_ROUTES != 0)
/*---------------------------------------------------------------------------*/
static void
route_callback(int event, uip_ipaddr_t *route, uip_ipaddr_t *ipaddr,
int numroutes)
{
if(event == UIP_DS6_NOTIFICATION_DEFRT_ADD) {
if(route != NULL && ipaddr != NULL &&
!uip_is_addr_unspecified(route) &&
!uip_is_addr_unspecified(ipaddr)) {
if(to_become_root) {
ctimer_set(&c, 0, create_dag_callback, NULL);
}
}
}
}
#endif /* (UIP_MAX_ROUTES != 0) */
/*---------------------------------------------------------------------------*/
static void
set_global_address(uip_ipaddr_t *prefix, uip_ipaddr_t *iid)
@ -175,29 +76,25 @@ set_global_address(uip_ipaddr_t *prefix, uip_ipaddr_t *iid)
}
/*---------------------------------------------------------------------------*/
void
rpl_dag_root_init(uip_ipaddr_t *prefix, uip_ipaddr_t *iid)
rpl_dag_root_set_prefix(uip_ipaddr_t *prefix, uip_ipaddr_t *iid)
{
static uint8_t initialized = 0;
if(!initialized) {
to_become_root = 0;
set_global_address(prefix, iid);
#if (UIP_MAX_ROUTES != 0)
uip_ds6_notification_add(&n, route_callback);
#endif /* (UIP_MAX_ROUTES != 0) */
initialized = 1;
}
}
/*---------------------------------------------------------------------------*/
int
rpl_dag_root_init_dag_immediately(void)
rpl_dag_root_start(void)
{
struct uip_ds6_addr *root_if;
int i;
uint8_t state;
uip_ipaddr_t *ipaddr = NULL;
rpl_dag_root_init(NULL, NULL);
rpl_dag_root_set_prefix(NULL, NULL);
for(i = 0; i < UIP_DS6_ADDR_NB; i++) {
state = uip_ds6_if.addr_list[i].state;
@ -222,38 +119,25 @@ rpl_dag_root_init_dag_immediately(void)
if(RPL_IS_STORING(dag->instance)) {
rpl_remove_routes(dag);
}
if(dag->instance != NULL &&
dag->instance->def_route != NULL) {
uip_ds6_defrt_rm(dag->instance->def_route);
if(dag->instance != NULL && dag->instance->def_route != NULL) {
uip_ds6_defrt_rm(dag->instance->def_route);
dag->instance->def_route = NULL;
}
uip_ip6addr(&prefix, UIP_DS6_DEFAULT_PREFIX, 0, 0, 0, 0, 0, 0, 0);
rpl_set_prefix(dag, &prefix, 64);
PRINTF("rpl_dag_root_init_dag: created a new RPL dag\n");
PRINTF("rpl_dag_root_set_prefix_dag: created a new RPL dag\n");
return 0;
} else {
PRINTF("rpl_dag_root_init_dag: failed to create a new RPL DAG\n");
PRINTF("rpl_dag_root_set_prefix_dag: failed to create a new RPL DAG\n");
return -1;
}
} else {
PRINTF("rpl_dag_root_init_dag: failed to create a new RPL DAG, no preferred IP address found\n");
PRINTF("rpl_dag_root_set_prefix_dag: failed to create a new RPL DAG, no preferred IP address found\n");
return -2;
}
}
/*---------------------------------------------------------------------------*/
void
rpl_dag_root_init_dag_delay(void)
{
rpl_dag_root_init(NULL, NULL);
ctimer_set(&c, RPL_DAG_GRACE_PERIOD, create_dag_callback, NULL);
to_become_root = 1;
/* Send a DIS packet to request RPL info from neighbors. */
dis_output(NULL);
}
/*---------------------------------------------------------------------------*/
int
rpl_dag_root_is_root(void)
{

View File

@ -32,9 +32,8 @@
#ifndef RPL_DAG_ROOT_H_
#define RPL_DAG_ROOT_H_
void rpl_dag_root_init(uip_ipaddr_t *prefix, uip_ipaddr_t *iid);
void rpl_dag_root_init_dag_delay(void);
int rpl_dag_root_init_dag_immediately(void);
void rpl_dag_root_set_prefix(uip_ipaddr_t *prefix, uip_ipaddr_t *iid);
int rpl_dag_root_start(void);
int rpl_dag_root_is_root(void);

View File

@ -48,48 +48,6 @@
#define LOG_MODULE "RPL"
#define LOG_LEVEL LOG_LEVEL_RPL
#define RPL_DAG_GRACE_PERIOD (CLOCK_SECOND * 20 * 1)
static uint8_t to_become_root;
static struct ctimer c;
/*---------------------------------------------------------------------------*/
static void
create_dag_callback(void *ptr)
{
const uip_ipaddr_t *root, *ipaddr;
root = curr_instance.used ? &curr_instance.dag.dag_id : NULL;
ipaddr = rpl_get_global_address();
if(root == NULL || uip_ipaddr_cmp(root, ipaddr)) {
/* The RPL network we are joining is one that we created, so we
become root. */
if(to_become_root) {
rpl_dag_root_init_dag_immediately();
to_become_root = 0;
}
} else {
LOG_WARN("found a network we did not create: version %d grounded %d preference %d rank %d\n",
curr_instance.dag.version, curr_instance.dag.grounded,
curr_instance.dag.preference, curr_instance.dag.rank);
/* We found a RPL network that we did not create so we just join
it without becoming root. But if the network has an infinite
rank, we assume the network has broken, and we become the new
root of the network. */
if(curr_instance.dag.rank == RPL_INFINITE_RANK) {
if(to_become_root) {
rpl_dag_root_init_dag_immediately();
to_become_root = 0;
}
}
/* Try again after the grace period */
ctimer_set(&c, RPL_DAG_GRACE_PERIOD, create_dag_callback, NULL);
}
}
/*---------------------------------------------------------------------------*/
static void
set_global_address(uip_ipaddr_t *prefix, uip_ipaddr_t *iid)
@ -125,26 +83,25 @@ set_global_address(uip_ipaddr_t *prefix, uip_ipaddr_t *iid)
}
/*---------------------------------------------------------------------------*/
void
rpl_dag_root_init(uip_ipaddr_t *prefix, uip_ipaddr_t *iid)
rpl_dag_root_set_prefix(uip_ipaddr_t *prefix, uip_ipaddr_t *iid)
{
static uint8_t initialized = 0;
if(!initialized) {
to_become_root = 0;
set_global_address(prefix, iid);
initialized = 1;
}
}
/*---------------------------------------------------------------------------*/
int
rpl_dag_root_init_dag_immediately(void)
rpl_dag_root_start(void)
{
struct uip_ds6_addr *root_if;
int i;
uint8_t state;
uip_ipaddr_t *ipaddr = NULL;
rpl_dag_root_init(NULL, NULL);
rpl_dag_root_set_prefix(NULL, NULL);
for(i = 0; i < UIP_DS6_ADDR_NB; i++) {
state = uip_ds6_if.addr_list[i].state;
@ -170,18 +127,6 @@ rpl_dag_root_init_dag_immediately(void)
}
}
/*---------------------------------------------------------------------------*/
void
rpl_dag_root_init_dag_delay(void)
{
rpl_dag_root_init(NULL, NULL);
ctimer_set(&c, RPL_DAG_GRACE_PERIOD, create_dag_callback, NULL);
to_become_root = 1;
/* Send a DIS packet to request RPL info from neighbors. */
rpl_icmp6_dis_output(NULL);
}
/*---------------------------------------------------------------------------*/
int
rpl_dag_root_is_root(void)
{

View File

@ -49,20 +49,14 @@
* \param prefix The prefix. If NULL, UIP_DS6_DEFAULT_PREFIX is used instead
* \param iid The IID. If NULL, it will be built from uip_ds6_set_addr_iid.
*/
void rpl_dag_root_init(uip_ipaddr_t *prefix, uip_ipaddr_t *iid);
void rpl_dag_root_set_prefix(uip_ipaddr_t *prefix, uip_ipaddr_t *iid);
/**
* Initializes the node as DAG root after a delay. Until the delay, the node
* gets a chance to hear another DAG and join it instead.
*/
void rpl_dag_root_init_dag_delay(void);
/**
* Initializes the node as DAG root immediatly.
* Set the node as root and start a DAG
*
* \return 0 in case of success, -1 otherwise
*/
int rpl_dag_root_init_dag_immediately(void);
int rpl_dag_root_start(void);
/**
* Tells whether we are DAG root or not

View File

@ -64,8 +64,8 @@ void
set_prefix_64(uip_ipaddr_t *prefix_64)
{
prefix_set = 1;
rpl_dag_root_init(prefix_64, NULL);
rpl_dag_root_init_dag_immediately();
rpl_dag_root_set_prefix(prefix_64, NULL);
rpl_dag_root_start();
}
/*---------------------------------------------------------------------------*/
void

View File

@ -324,8 +324,8 @@ PT_THREAD(cmd_rpl_set_root(struct pt *pt, shell_output_func output, char *args))
SHELL_OUTPUT(output, "Setting as DAG root with prefix ");
shell_output_6addr(output, &prefix);
SHELL_OUTPUT(output, "/64\n");
rpl_dag_root_init(&prefix, NULL);
rpl_dag_root_init_dag_immediately();
rpl_dag_root_set_prefix(&prefix, NULL);
rpl_dag_root_start();
} else {
SHELL_OUTPUT(output, "Node is already a DAG root\n");
}

View File

@ -40,7 +40,7 @@ PROCESS_THREAD(udp_process, ev, data)
PROCESS_BEGIN();
rpl_dag_root_init_dag_immediately();
rpl_dag_root_start();
simple_udp_register(&broadcast_connection, UDP_PORT,
NULL, UDP_PORT,

View File

@ -74,7 +74,7 @@ PROCESS_THREAD(unicast_receiver_process, ev, data)
{
PROCESS_BEGIN();
rpl_dag_root_init_dag_immediately();
rpl_dag_root_start();
simple_udp_register(&unicast_connection, UDP_PORT,
NULL, UDP_PORT, receiver);

View File

@ -72,7 +72,7 @@ PROCESS_THREAD(unicast_receiver_process, ev, data)
{
PROCESS_BEGIN();
rpl_dag_root_init_dag_immediately();
rpl_dag_root_start();
simple_udp_register(&unicast_connection, UDP_PORT,
NULL, UDP_PORT, receiver);