nes-proj/examples/ipv6/rpl-tsch/node.c
2017-07-07 15:23:32 +02:00

178 lines
5.4 KiB
C

/*
* Copyright (c) 2015, SICS Swedish ICT.
* 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.
*
*/
/**
* \file
* A RPL+TSCH node able to act as either a simple node (6ln),
* DAG Root (6dr) or DAG Root with security (6dr-sec)
* Press use button at startup to configure.
*
* \author Simon Duquennoy <simonduq@sics.se>
*/
#include "contiki.h"
#include "node-id.h"
#include "rpl.h"
#include "rpl-dag-root.h"
#include "net/ipv6/uip-ds6-route.h"
#include "net/mac/tsch/tsch.h"
#if UIP_CONF_IPV6_RPL_LITE == 0
#include "rpl-private.h"
#endif /* UIP_CONF_IPV6_RPL_LITE == 0 */
#if WITH_ORCHESTRA
#include "orchestra.h"
#endif /* WITH_ORCHESTRA */
#if WITH_SHELL
#include "serial-shell.h"
#endif /* WITH_SHELL */
#define DEBUG DEBUG_PRINT
#include "net/ip/uip-debug.h"
/*---------------------------------------------------------------------------*/
PROCESS(node_process, "RPL Node");
AUTOSTART_PROCESSES(&node_process);
/*---------------------------------------------------------------------------*/
static void
print_network_status(void)
{
int i;
uint8_t state;
uip_ds6_defrt_t *default_route;
#if RPL_WITH_STORING
uip_ds6_route_t *route;
#endif /* RPL_WITH_STORING */
#if RPL_WITH_NON_STORING
rpl_ns_node_t *link;
#endif /* RPL_WITH_NON_STORING */
PRINTF("--- Network status ---\n");
/* Our IPv6 addresses */
PRINTF("- Server IPv6 addresses:\n");
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_TENTATIVE || state == ADDR_PREFERRED)) {
PRINTF("-- ");
PRINT6ADDR(&uip_ds6_if.addr_list[i].ipaddr);
PRINTF("\n");
}
}
/* Our default route */
PRINTF("- Default route:\n");
default_route = uip_ds6_defrt_lookup(uip_ds6_defrt_choose());
if(default_route != NULL) {
PRINTF("-- ");
PRINT6ADDR(&default_route->ipaddr);
PRINTF(" (lifetime: %lu seconds)\n", (unsigned long)default_route->lifetime.interval);
} else {
PRINTF("-- None\n");
}
#if RPL_WITH_STORING
/* Our routing entries */
PRINTF("- Routing entries (%u in total):\n", uip_ds6_route_num_routes());
route = uip_ds6_route_head();
while(route != NULL) {
PRINTF("-- ");
PRINT6ADDR(&route->ipaddr);
PRINTF(" via ");
PRINT6ADDR(uip_ds6_route_nexthop(route));
PRINTF(" (lifetime: %lu seconds)\n", (unsigned long)route->state.lifetime);
route = uip_ds6_route_next(route);
}
#endif
#if RPL_WITH_NON_STORING
/* Our routing links */
PRINTF("- Routing links (%u in total):\n", rpl_ns_num_nodes());
link = rpl_ns_node_head();
while(link != NULL) {
uip_ipaddr_t child_ipaddr;
uip_ipaddr_t parent_ipaddr;
rpl_ns_get_node_global_addr(&child_ipaddr, link);
rpl_ns_get_node_global_addr(&parent_ipaddr, link->parent);
PRINTF("-- ");
PRINT6ADDR(&child_ipaddr);
if(link->parent == NULL) {
memset(&parent_ipaddr, 0, sizeof(parent_ipaddr));
PRINTF(" to DODAG root ");
} else {
PRINTF(" to ");
PRINT6ADDR(&parent_ipaddr);
}
PRINTF(" (lifetime: %lu seconds)\n", (unsigned long)link->lifetime);
link = rpl_ns_node_next(link);
}
#endif
PRINTF("----------------------\n");
}
/*---------------------------------------------------------------------------*/
PROCESS_THREAD(node_process, ev, data)
{
static struct etimer et;
int is_coordinator;
PROCESS_BEGIN();
is_coordinator = 0;
#if WITH_SHELL
serial_shell_init();
#endif /* WITH_SHELL */
#if CONTIKI_TARGET_COOJA
is_coordinator = (node_id == 1);
#endif
if(is_coordinator) {
rpl_dag_root_init_dag_immediately();
}
NETSTACK_MAC.on();
#if WITH_ORCHESTRA
orchestra_init();
#endif /* WITH_ORCHESTRA */
/* Print out routing tables every minute */
etimer_set(&et, CLOCK_SECOND * 60);
while(1) {
print_network_status();
PROCESS_YIELD_UNTIL(etimer_expired(&et));
etimer_reset(&et);
}
PROCESS_END();
}
/*---------------------------------------------------------------------------*/