2012-03-05 15:47:01 +00:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "contiki.h"
|
|
|
|
#include "contiki-lib.h"
|
|
|
|
#include "contiki-net.h"
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#define DEBUG DEBUG_PRINT
|
|
|
|
#include "net/uip-debug.h"
|
|
|
|
#include "net/rpl/rpl.h"
|
|
|
|
#include "dev/watchdog.h"
|
|
|
|
#include "dev/slip.h"
|
|
|
|
#include "dev/leds.h"
|
2012-04-30 16:04:55 +00:00
|
|
|
#include "dev/cc2530-rf.h"
|
2012-09-18 09:43:32 +00:00
|
|
|
#include "debug.h"
|
2012-03-05 15:47:01 +00:00
|
|
|
|
|
|
|
static uint8_t prefix_set;
|
2012-09-18 09:43:32 +00:00
|
|
|
|
|
|
|
#if DEBUG
|
|
|
|
#define PUTSTRING(...) putstring(__VA_ARGS__)
|
|
|
|
#define PUTHEX(...) puthex(__VA_ARGS__)
|
|
|
|
#define PUTBIN(...) putbin(__VA_ARGS__)
|
|
|
|
#define PUTDEC(...) putdec(__VA_ARGS__)
|
|
|
|
#define PUTCHAR(...) putchar(__VA_ARGS__)
|
|
|
|
#else
|
|
|
|
#define PUTSTRING(...)
|
|
|
|
#define PUTHEX(...)
|
|
|
|
#define PUTBIN(...)
|
|
|
|
#define PUTDEC(...)
|
|
|
|
#define PUTCHAR(...)
|
|
|
|
#endif
|
2012-03-05 15:47:01 +00:00
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
PROCESS(border_router_process, "Border Router process");
|
|
|
|
AUTOSTART_PROCESSES(&border_router_process);
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static void
|
2012-09-18 09:43:32 +00:00
|
|
|
print_local_addresses(void) CC_NON_BANKED
|
2012-03-05 15:47:01 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
uint8_t state;
|
|
|
|
|
2012-09-18 09:43:32 +00:00
|
|
|
PUTSTRING("Router's IPv6 addresses:\n");
|
2012-03-05 15:47:01 +00:00
|
|
|
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)) {
|
2012-09-18 09:43:32 +00:00
|
|
|
PUTSTRING(" ");
|
2012-03-05 15:47:01 +00:00
|
|
|
PRINT6ADDR(&uip_ds6_if.addr_list[i].ipaddr);
|
2012-09-18 09:43:32 +00:00
|
|
|
PUTCHAR('\n');
|
2012-12-16 15:27:01 +00:00
|
|
|
if(state == ADDR_TENTATIVE) {
|
2012-03-05 15:47:01 +00:00
|
|
|
uip_ds6_if.addr_list[i].state = ADDR_PREFERRED;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2012-09-18 09:43:32 +00:00
|
|
|
static void
|
|
|
|
request_prefix(void) CC_NON_BANKED
|
|
|
|
{
|
2012-03-05 15:47:01 +00:00
|
|
|
/* mess up uip_buf with a dirty request... */
|
|
|
|
uip_buf[0] = '?';
|
|
|
|
uip_buf[1] = 'P';
|
|
|
|
uip_len = 2;
|
|
|
|
slip_send();
|
|
|
|
uip_len = 0;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
/* Set our prefix when we receive one over SLIP */
|
|
|
|
void
|
2012-09-18 09:43:32 +00:00
|
|
|
set_prefix_64(uip_ipaddr_t *prefix_64)
|
|
|
|
{
|
2012-03-05 15:47:01 +00:00
|
|
|
rpl_dag_t *dag;
|
|
|
|
uip_ipaddr_t ipaddr;
|
|
|
|
memcpy(&ipaddr, prefix_64, 16);
|
|
|
|
prefix_set = 1;
|
|
|
|
uip_ds6_set_addr_iid(&ipaddr, &uip_lladdr);
|
|
|
|
uip_ds6_addr_add(&ipaddr, 0, ADDR_AUTOCONF);
|
|
|
|
|
|
|
|
/* Become root of a new DODAG with ID our global v6 address */
|
|
|
|
dag = rpl_set_root(RPL_DEFAULT_INSTANCE, &ipaddr);
|
|
|
|
if(dag != NULL) {
|
|
|
|
rpl_set_prefix(dag, &ipaddr, 64);
|
2012-09-18 09:43:32 +00:00
|
|
|
PUTSTRING("Created a new RPL dag with ID: ");
|
2012-03-05 15:47:01 +00:00
|
|
|
PRINT6ADDR(&dag->dag_id);
|
2012-09-18 09:43:32 +00:00
|
|
|
PUTCHAR('\n');
|
2012-03-05 15:47:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
PROCESS_THREAD(border_router_process, ev, data)
|
|
|
|
{
|
|
|
|
static struct etimer et;
|
|
|
|
|
|
|
|
PROCESS_BEGIN();
|
2012-09-18 09:43:32 +00:00
|
|
|
PUTSTRING("Border Router started\n");
|
2012-03-05 15:47:01 +00:00
|
|
|
prefix_set = 0;
|
|
|
|
|
2012-09-18 09:43:32 +00:00
|
|
|
leds_on(LEDS_GREEN);
|
2012-03-05 15:47:01 +00:00
|
|
|
|
|
|
|
/* Request prefix until it has been received */
|
|
|
|
while(!prefix_set) {
|
2012-09-18 09:43:32 +00:00
|
|
|
leds_on(LEDS_RED);
|
|
|
|
PUTSTRING("Prefix request.\n");
|
2012-03-05 15:47:01 +00:00
|
|
|
etimer_set(&et, CLOCK_SECOND);
|
|
|
|
request_prefix();
|
2012-09-18 09:43:32 +00:00
|
|
|
leds_off(LEDS_RED);
|
2012-03-05 15:47:01 +00:00
|
|
|
PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&et));
|
|
|
|
}
|
|
|
|
/* We have created a new DODAG when we reach here */
|
2012-09-18 09:43:32 +00:00
|
|
|
PUTSTRING("On Channel ");
|
|
|
|
PUTDEC(cc2530_rf_channel_get());
|
|
|
|
PUTCHAR('\n');
|
2012-03-05 15:47:01 +00:00
|
|
|
|
|
|
|
print_local_addresses();
|
|
|
|
|
2012-09-18 09:43:32 +00:00
|
|
|
leds_off(LEDS_GREEN);
|
2012-03-05 15:47:01 +00:00
|
|
|
|
|
|
|
PROCESS_EXIT();
|
|
|
|
|
|
|
|
PROCESS_END();
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|