osd-contiki/examples/sensinode/border-router/border-router.c

151 lines
4.7 KiB
C
Raw Normal View History

2012-03-05 17:28:06 +01: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/ip/uip-debug.h"
2012-03-05 17:28:06 +01:00
#include "net/rpl/rpl.h"
#include "dev/watchdog.h"
#include "dev/slip.h"
#include "dev/leds.h"
#include "dev/cc2430_rf.h"
#include "debug.h"
2012-03-05 17:28:06 +01:00
static uint8_t prefix_set;
#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 17:28:06 +01:00
/*---------------------------------------------------------------------------*/
PROCESS(border_router_process, "Border Router process");
AUTOSTART_PROCESSES(&border_router_process);
/*---------------------------------------------------------------------------*/
static void
print_local_addresses(void) CC_NON_BANKED
2012-03-05 17:28:06 +01:00
{
int i;
uint8_t state;
PUTSTRING("Router's IPv6 addresses:\n");
2012-03-05 17:28:06 +01: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
2012-12-16 16:29:45 +01:00
== ADDR_PREFERRED)) {
PUTSTRING(" ");
2012-03-05 17:28:06 +01:00
PRINT6ADDR(&uip_ds6_if.addr_list[i].ipaddr);
PUTCHAR('\n');
2012-12-16 16:29:45 +01:00
if(state == ADDR_TENTATIVE) {
2012-03-05 17:28:06 +01:00
uip_ds6_if.addr_list[i].state = ADDR_PREFERRED;
}
}
}
}
/*---------------------------------------------------------------------------*/
static void
request_prefix(void) CC_NON_BANKED
{
2012-03-05 17:28:06 +01:00
/* mess up uip_buf with a dirty request... */
uip_buf[0] = '?';
uip_buf[1] = 'P';
uip_len = 2;
slip_send();
uip_clear_buf();
2012-03-05 17:28:06 +01:00
}
/*---------------------------------------------------------------------------*/
/* Set our prefix when we receive one over SLIP */
void
set_prefix_64(uip_ipaddr_t *prefix_64)
{
2012-03-05 17:28:06 +01: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);
PUTSTRING("Created a new RPL dag with ID: ");
2012-03-05 17:28:06 +01:00
PRINT6ADDR(&dag->dag_id);
PUTCHAR('\n');
2012-03-05 17:28:06 +01:00
}
}
/*---------------------------------------------------------------------------*/
PROCESS_THREAD(border_router_process, ev, data)
{
static struct etimer et;
PROCESS_BEGIN();
PUTSTRING("Border Router started\n");
2012-03-05 17:28:06 +01:00
prefix_set = 0;
leds_on(LEDS_RED);
/* Request prefix until it has been received */
while(!prefix_set) {
leds_on(LEDS_GREEN);
PUTSTRING("Prefix request.\n");
2012-03-05 17:28:06 +01:00
etimer_set(&et, CLOCK_SECOND);
request_prefix();
leds_off(LEDS_GREEN);
PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&et));
}
/* We have created a new DODAG when we reach here */
PUTSTRING("On Channel ");
PUTDEC(cc2430_rf_channel_get());
PUTCHAR('\n');
2012-03-05 17:28:06 +01:00
print_local_addresses();
leds_off(LEDS_RED);
PROCESS_EXIT();
PROCESS_END();
}
/*---------------------------------------------------------------------------*/