1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2024-12-29 04:50:03 +01:00
RIOT/examples/rpl_udp/rpl.c

201 lines
5.7 KiB
C
Raw Normal View History

2014-02-03 22:47:31 +01:00
/*
* Copyright (C) 2013, 2014 INRIA
2014-02-03 22:47:31 +01:00
*
* This file is subject to the terms and conditions of the GNU Lesser
* General Public License v2.1. See the file LICENSE in the top level
* directory for more details.
2014-02-03 22:47:31 +01:00
*/
/**
* @ingroup examples
* @{
*
* @file
* @brief UDP RPL example application
*
* @author Oliver Hahm <oliver.hahm@inria.fr>
*
* @}
*/
2014-01-06 10:10:47 +01:00
#include <stdio.h>
#include <string.h>
#include "vtimer.h"
#include "thread.h"
2014-02-13 16:06:25 +01:00
#include "net_if.h"
2014-01-06 10:10:47 +01:00
#include "sixlowpan.h"
#include "udp.h"
2014-01-06 10:10:47 +01:00
#include "rpl.h"
2014-08-06 13:48:18 +02:00
#include "rpl/rpl_dodag.h"
2014-08-04 17:52:39 +02:00
#include "rpl_udp.h"
#include "transceiver.h"
2014-01-06 10:10:47 +01:00
2014-03-03 00:25:38 +01:00
#define ENABLE_DEBUG (0)
#include "debug.h"
#ifndef UNASSIGNED_CHANNEL
#define UNASSIGNED_CHANNEL INT_MIN
#endif
#define TRANSCEIVER TRANSCEIVER_DEFAULT
2014-01-06 10:10:47 +01:00
static char monitor_stack_buffer[MONITOR_STACK_SIZE];
2014-01-06 10:10:47 +01:00
radio_address_t id;
static uint8_t is_root = 0;
2014-01-06 10:10:47 +01:00
int rpl_udp_init(int argc, char **argv)
2014-01-06 10:10:47 +01:00
{
transceiver_command_t tcmd;
msg_t m;
int32_t chan = UNASSIGNED_CHANNEL;
2014-01-06 10:10:47 +01:00
2014-02-24 23:39:23 +01:00
if (argc != 2) {
printf("Usage: %s (r|n|h)\n", argv[0]);
2014-01-06 10:10:47 +01:00
printf("\tr\tinitialize as root\n");
printf("\tn\tinitialize as node router\n");
printf("\th\tinitialize as non-routing node (host-mode)\n");
return 1;
2014-01-06 10:10:47 +01:00
}
2014-02-24 23:39:23 +01:00
char command = argv[1][0];
if ((command == 'n') || (command == 'r') || (command == 'h')) {
printf("INFO: Initialize as %srouting %s on address %d\n",
((command == 'h') ? "non-" : ""),
(((command == 'n') || (command == 'h')) ? "node" : "root"), id);
#if (defined(MODULE_CC110X) || defined(MODULE_CC110X_LEGACY) || defined(MODULE_CC110X_LEGACY_CSMA))
2014-01-06 10:10:47 +01:00
if (!id || (id > 255)) {
printf("ERROR: address not a valid 8 bit integer\n");
return 1;
2014-01-06 10:10:47 +01:00
}
#endif
2014-01-06 10:10:47 +01:00
2014-03-03 10:59:19 +01:00
DEBUGF("Setting HW address to %u\n", id);
2014-02-13 16:06:25 +01:00
net_if_set_hardware_address(0, id);
tcmd.transceivers = TRANSCEIVER;
tcmd.data = &chan;
m.type = GET_CHANNEL;
m.content.ptr = (void *) &tcmd;
msg_send_receive(&m, &m, transceiver_pid);
if( chan == UNASSIGNED_CHANNEL ) {
DEBUGF("The channel has not been set yet.");
/* try to set the channel to 10 (RADIO_CHANNEL) */
chan = RADIO_CHANNEL;
}
m.type = SET_CHANNEL;
msg_send_receive(&m, &m, transceiver_pid);
if( chan == UNASSIGNED_CHANNEL ) {
puts("ERROR: channel NOT set! Aborting initialization.");
return 1;
}
printf("Channel set to %" PRIi32 "\n", chan);
/* global address */
ipv6_addr_t global_addr, global_prefix;
ipv6_addr_init(&global_prefix, 0xabcd, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0);
ipv6_addr_set_by_eui64(&global_addr, 0, &global_prefix);
if (command != 'h') {
DEBUGF("Initializing RPL for interface 0\n");
uint8_t state = SIXLOWERROR_VALUE;
if (command == 'n') {
/*
* no global address specified, we'll use auto address config
* initiated by the root node
*/
state = rpl_init(0, NULL);
}
else if (command == 'r') {
rpl_options_t rpl_opts = {
.instance_id = 0,
.prefix = global_prefix,
.prefix_len = 64,
.prefix_flags = RPL_PREFIX_INFO_AUTO_ADDR_CONF,
/* autonomous address-configuration */
};
/* use specific global address */
state = rpl_init(0, &global_addr);
rpl_init_root(&rpl_opts);
is_root = 1;
}
if (state != SIXLOWERROR_SUCCESS) {
puts("Error initializing RPL");
}
else {
puts("6LoWPAN and RPL initialized.");
}
ipv6_iface_set_routing_provider(rpl_get_next_hop);
2014-01-06 10:10:47 +01:00
}
else {
puts("6LoWPAN initialized.");
2014-01-06 10:10:47 +01:00
}
2014-03-03 10:59:19 +01:00
DEBUGF("Start monitor\n");
kernel_pid_t monitor_pid = thread_create(monitor_stack_buffer,
sizeof(monitor_stack_buffer),
THREAD_PRIORITY_MAIN - 2,
CREATE_STACKTEST,
rpl_udp_monitor,
NULL,
"monitor");
2014-03-03 10:59:19 +01:00
DEBUGF("Register at transceiver %02X\n", TRANSCEIVER);
2014-01-06 10:10:47 +01:00
transceiver_register(TRANSCEIVER, monitor_pid);
ipv6_register_packet_handler(monitor_pid);
//sixlowpan_lowpan_register(monitor_pid);
}
else {
printf("ERROR: Unknown command '%c'\n", command);
return 1;
2014-01-06 10:10:47 +01:00
}
if (command != 'h') {
ipv6_init_as_router();
}
puts("Transport layer initialized");
2014-01-06 10:10:47 +01:00
/* start transceiver watchdog */
return 0;
2014-01-06 10:10:47 +01:00
}
int rpl_udp_dodag(int argc, char **argv)
2014-01-06 10:10:47 +01:00
{
2014-02-24 23:39:23 +01:00
(void) argc;
(void) argv;
2014-01-06 10:10:47 +01:00
printf("---------------------------\n");
rpl_dodag_t *mydodag = rpl_get_my_dodag();
if (mydodag == NULL) {
printf("Not part of a dodag\n");
2014-01-18 01:15:29 +01:00
printf("---------------------------\n");
return 1;
2014-01-06 10:10:47 +01:00
}
printf("Part of Dodag:\n");
2014-02-13 16:06:25 +01:00
printf("%s\n", ipv6_addr_to_str(addr_str, IPV6_MAX_ADDR_STR_LEN,
(&mydodag->dodag_id)));
2014-01-06 10:10:47 +01:00
printf("my rank: %d\n", mydodag->my_rank);
2014-01-06 10:10:47 +01:00
if (!is_root) {
printf("my preferred parent:\n");
2014-02-13 16:06:25 +01:00
printf("%s\n", ipv6_addr_to_str(addr_str, IPV6_MAX_ADDR_STR_LEN,
(&mydodag->my_preferred_parent->addr)));
2014-01-06 10:10:47 +01:00
}
2014-01-18 01:15:29 +01:00
printf("---------------------------\n");
return 0;
2014-01-06 10:10:47 +01:00
}