2006-06-17 22:41:10 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2002, Adam Dunkels.
|
|
|
|
* 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. The name of the author may not be used to endorse or promote
|
|
|
|
* products derived from this software without specific prior
|
|
|
|
* written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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 OS
|
|
|
|
*
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2007-05-22 21:19:34 +00:00
|
|
|
#include <stdio.h>
|
2007-11-26 23:28:33 +00:00
|
|
|
#include <time.h>
|
2007-11-28 14:16:12 +00:00
|
|
|
#include <sys/select.h>
|
2007-05-22 21:19:34 +00:00
|
|
|
#include <unistd.h>
|
2010-02-02 17:51:55 +00:00
|
|
|
#include <memory.h>
|
2007-05-22 21:19:34 +00:00
|
|
|
|
2006-06-17 22:41:10 +00:00
|
|
|
#include "contiki.h"
|
2009-03-06 00:13:56 +00:00
|
|
|
#include "contiki-net.h"
|
|
|
|
|
2009-03-17 15:56:32 +00:00
|
|
|
#include "dev/serial-line.h"
|
2006-06-17 22:41:10 +00:00
|
|
|
|
|
|
|
#include "net/uip.h"
|
2007-03-26 02:53:54 +00:00
|
|
|
#ifdef __CYGWIN__
|
2007-05-20 21:29:39 +00:00
|
|
|
#include "net/wpcap-drv.h"
|
2008-01-04 21:53:32 +00:00
|
|
|
#else /* __CYGWIN__ */
|
2007-05-20 21:29:39 +00:00
|
|
|
#include "net/tapdev-drv.h"
|
2008-01-04 21:53:32 +00:00
|
|
|
#endif /* __CYGWIN__ */
|
2006-06-17 22:41:10 +00:00
|
|
|
|
2007-03-26 02:53:54 +00:00
|
|
|
#ifdef __CYGWIN__
|
2009-03-17 15:56:32 +00:00
|
|
|
PROCINIT(&etimer_process, &tcpip_process, &wpcap_process, &serial_line_process);
|
2008-01-04 21:53:32 +00:00
|
|
|
#else /* __CYGWIN__ */
|
2009-03-17 15:56:32 +00:00
|
|
|
PROCINIT(&etimer_process, &tapdev_process, &tcpip_process, &serial_line_process);
|
2008-01-04 21:53:32 +00:00
|
|
|
#endif /* __CYGWIN__ */
|
2006-06-17 22:41:10 +00:00
|
|
|
|
2011-06-28 12:19:48 +00:00
|
|
|
#if RPL_BORDER_ROUTER
|
|
|
|
#include "net/rpl/rpl.h"
|
|
|
|
|
2012-01-25 14:41:39 +00:00
|
|
|
static uint16_t dag_id[] = {0x1111, 0x1100, 0, 0, 0, 0, 0, 0x0011};
|
2011-06-28 12:19:48 +00:00
|
|
|
|
|
|
|
PROCESS(border_router_process, "RPL Border Router");
|
|
|
|
PROCESS_THREAD(border_router_process, ev, data)
|
|
|
|
{
|
|
|
|
|
|
|
|
PROCESS_BEGIN();
|
|
|
|
|
|
|
|
PROCESS_PAUSE();
|
|
|
|
|
2012-01-25 14:34:10 +00:00
|
|
|
{
|
|
|
|
rpl_dag_t *dag;
|
|
|
|
char buf[sizeof(dag_id)];
|
|
|
|
memcpy(buf,dag_id,sizeof(dag_id));
|
|
|
|
dag = rpl_set_root((uip_ip6addr_t *)buf);
|
|
|
|
|
|
|
|
/* Assign separate addresses to the uip stack and the host network
|
|
|
|
interface, but with the same prefix E.g. bbbb::ff:fe00:200 to
|
|
|
|
the stack and bbbb::1 to the host *fallback* network interface
|
|
|
|
Otherwise the host will trap packets intended for the stack,
|
|
|
|
just as the stack will trap packets intended for the host
|
|
|
|
$ifconfig usb0 -arp on Ubuntu to skip the neighbor
|
|
|
|
solicitations. Add explicit neighbors on other OSs */
|
|
|
|
|
|
|
|
if(dag != NULL) {
|
|
|
|
printf("Created a new RPL dag\n");
|
|
|
|
|
2011-06-28 12:19:48 +00:00
|
|
|
#if UIP_CONF_ROUTER_RECEIVE_RA
|
2012-01-25 14:34:10 +00:00
|
|
|
/* Contiki stack will shut down until assigned an address from the
|
|
|
|
interface RA Currently this requires changes in the core
|
|
|
|
rpl-icmp6.c to pass the link-local RA broadcast.
|
|
|
|
*/
|
|
|
|
|
2011-06-28 12:19:48 +00:00
|
|
|
#else
|
2012-01-25 14:34:10 +00:00
|
|
|
{
|
|
|
|
static void sprint_ip6(uip_ip6addr_t addr);
|
|
|
|
int i;
|
|
|
|
uip_ip6addr_t ipaddr;
|
2011-06-28 12:19:48 +00:00
|
|
|
#ifdef HARD_CODED_ADDRESS
|
2012-01-25 14:34:10 +00:00
|
|
|
uiplib_ipaddrconv(HARD_CODED_ADDRESS, &ipaddr);
|
2011-06-28 12:19:48 +00:00
|
|
|
#else
|
2012-01-25 14:34:10 +00:00
|
|
|
uip_ip6addr(&ipaddr, 0xbbbb, 0, 0, 0, 0, 0, 0, 0x1);
|
2011-06-28 12:19:48 +00:00
|
|
|
#endif
|
2012-01-25 14:34:10 +00:00
|
|
|
uip_ds6_set_addr_iid(&ipaddr, &uip_lladdr);
|
|
|
|
uip_ds6_addr_add(&ipaddr, 0, ADDR_AUTOCONF);
|
|
|
|
rpl_set_prefix(dag, &ipaddr, 64);
|
|
|
|
|
|
|
|
for(i = 0; i < UIP_DS6_ADDR_NB; i++) {
|
|
|
|
if(uip_ds6_if.addr_list[i].isused) {
|
|
|
|
printf("IPV6 Address: ");
|
|
|
|
sprint_ip6(uip_ds6_if.addr_list[i].ipaddr);
|
|
|
|
printf("\n");
|
2011-06-28 12:19:48 +00:00
|
|
|
}
|
|
|
|
}
|
2012-01-25 14:34:10 +00:00
|
|
|
}
|
2011-06-28 12:19:48 +00:00
|
|
|
#endif
|
2012-01-25 14:34:10 +00:00
|
|
|
}
|
2011-06-28 12:19:48 +00:00
|
|
|
}
|
|
|
|
/* The border router runs with a 100% duty cycle in order to ensure high
|
|
|
|
packet reception rates. */
|
2012-01-25 14:34:10 +00:00
|
|
|
/* NETSTACK_MAC.off(1); */
|
2011-06-28 12:19:48 +00:00
|
|
|
|
|
|
|
while(1) {
|
|
|
|
PROCESS_YIELD();
|
|
|
|
/* Local and global dag repair can be done from ? */
|
2012-01-25 14:34:10 +00:00
|
|
|
/* rpl_set_prefix(rpl_get_dag(RPL_ANY_INSTANCE), &ipaddr, 64);
|
|
|
|
rpl_repair_dag(rpl_get_dag(RPL_ANY_INSTANCE)); */
|
2011-06-28 12:19:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
PROCESS_END();
|
|
|
|
}
|
|
|
|
#endif /* RPL_BORDER_ROUTER */
|
|
|
|
|
2010-02-02 17:51:55 +00:00
|
|
|
#if UIP_CONF_IPV6
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2012-01-25 14:34:10 +00:00
|
|
|
static void
|
2010-02-02 17:51:55 +00:00
|
|
|
sprint_ip6(uip_ip6addr_t addr)
|
|
|
|
{
|
|
|
|
unsigned char i = 0;
|
|
|
|
unsigned char zerocnt = 0;
|
|
|
|
unsigned char numprinted = 0;
|
2012-03-21 20:27:52 +00:00
|
|
|
unsigned char notskipped = 0;
|
2010-02-02 17:51:55 +00:00
|
|
|
char thestring[40];
|
2012-01-25 14:34:10 +00:00
|
|
|
char *result = thestring;
|
2010-02-02 17:51:55 +00:00
|
|
|
|
2012-01-25 14:34:10 +00:00
|
|
|
*result++ = '[';
|
|
|
|
while(numprinted < 8) {
|
|
|
|
if((addr.u16[i] == 0) && (zerocnt == 0)) {
|
|
|
|
while(addr.u16[zerocnt + i] == 0) {
|
|
|
|
zerocnt++;
|
|
|
|
}
|
2012-03-21 20:27:52 +00:00
|
|
|
if(zerocnt == 1 && notskipped) {
|
2010-02-02 17:51:55 +00:00
|
|
|
*result++ = '0';
|
|
|
|
numprinted++;
|
2012-03-21 20:27:52 +00:00
|
|
|
notskipped = 1;
|
|
|
|
continue;
|
2010-02-02 17:51:55 +00:00
|
|
|
}
|
|
|
|
i += zerocnt;
|
|
|
|
numprinted += zerocnt;
|
|
|
|
} else {
|
2010-10-19 18:29:03 +00:00
|
|
|
result += sprintf(result, "%x", (unsigned int)(uip_ntohs(addr.u16[i])));
|
2010-02-02 17:51:55 +00:00
|
|
|
i++;
|
|
|
|
numprinted++;
|
|
|
|
}
|
2012-01-25 14:34:10 +00:00
|
|
|
if(numprinted != 8) {
|
|
|
|
*result++ = ':';
|
|
|
|
}
|
2010-02-02 17:51:55 +00:00
|
|
|
}
|
|
|
|
*result++=']';
|
|
|
|
*result=0;
|
2012-01-25 14:34:10 +00:00
|
|
|
printf("%s", thestring);
|
2010-02-02 17:51:55 +00:00
|
|
|
}
|
|
|
|
#endif /* UIP_CONF_IPV6 */
|
2006-06-17 22:41:10 +00:00
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
int
|
|
|
|
main(void)
|
|
|
|
{
|
2011-07-24 15:53:36 +00:00
|
|
|
clock_init();
|
2011-06-28 12:19:48 +00:00
|
|
|
#if UIP_CONF_IPV6
|
2012-01-25 14:34:10 +00:00
|
|
|
/* A hard coded address overrides the stack default MAC address to
|
|
|
|
allow multiple instances. uip6.c defines it as
|
|
|
|
{0x00,0x06,0x98,0x00,0x02,0x32} giving an ipv6 address of
|
|
|
|
[fe80::206:98ff:fe00:232] We make it simpler, {0x02,0x00,0x00 + the
|
|
|
|
last three bytes of the hard coded address (if any are nonzero).
|
|
|
|
HARD_CODED_ADDRESS can be defined in the contiki-conf.h file, or
|
|
|
|
here to allow quick builds using different addresses. If
|
|
|
|
HARD_CODED_ADDRESS has a prefix it also applied, unless built as a
|
|
|
|
RPL end node. E.g. bbbb::12:3456 becomes fe80::ff:fe12:3456 and
|
|
|
|
prefix bbbb::/64 if non-RPL ::10 becomes fe80::ff:fe00:10 and
|
|
|
|
prefix awaits RA or RPL formation bbbb:: gives an address of
|
|
|
|
bbbb::206:98ff:fe00:232 if non-RPL */
|
2011-06-28 12:19:48 +00:00
|
|
|
#ifdef HARD_CODED_ADDRESS
|
2012-01-25 14:34:10 +00:00
|
|
|
{
|
2011-06-28 12:19:48 +00:00
|
|
|
uip_ipaddr_t ipaddr;
|
|
|
|
uiplib_ipaddrconv(HARD_CODED_ADDRESS, &ipaddr);
|
2012-01-25 14:34:10 +00:00
|
|
|
if((ipaddr.u8[13] != 0) ||
|
|
|
|
(ipaddr.u8[14] != 0) ||
|
|
|
|
(ipaddr.u8[15] != 0)) {
|
|
|
|
if(sizeof(uip_lladdr) == 6) { /* Minimal-net uses ethernet MAC */
|
|
|
|
uip_lladdr.addr[0] = 0x02;
|
|
|
|
uip_lladdr.addr[1] = 0;
|
|
|
|
uip_lladdr.addr[2] = 0;
|
|
|
|
uip_lladdr.addr[3] = ipaddr.u8[13];
|
|
|
|
uip_lladdr.addr[4] = ipaddr.u8[14];
|
|
|
|
uip_lladdr.addr[5] = ipaddr.u8[15];
|
|
|
|
}
|
|
|
|
}
|
2011-06-28 12:19:48 +00:00
|
|
|
}
|
2012-01-25 14:34:10 +00:00
|
|
|
#endif /* HARD_CODED_ADDRESS */
|
|
|
|
#endif /* UIP_CONF_IPV6 */
|
2006-06-17 22:41:10 +00:00
|
|
|
|
|
|
|
process_init();
|
2011-06-30 16:12:07 +00:00
|
|
|
/* procinit_init initializes RPL which sets a ctimer for the first DIS */
|
|
|
|
/* We must start etimers and ctimers,before calling it */
|
|
|
|
process_start(&etimer_process, NULL);
|
2009-04-29 11:49:27 +00:00
|
|
|
ctimer_init();
|
|
|
|
|
2011-06-28 12:19:48 +00:00
|
|
|
#if RPL_BORDER_ROUTER
|
|
|
|
process_start(&border_router_process, NULL);
|
|
|
|
printf("Border Router Process started\n");
|
|
|
|
#elif UIP_CONF_IPV6_RPL
|
|
|
|
printf("RPL enabled\n");
|
|
|
|
#endif
|
|
|
|
|
2012-02-11 20:19:28 +00:00
|
|
|
procinit_init();
|
|
|
|
autostart_start(autostart_processes);
|
|
|
|
|
2010-02-02 17:51:55 +00:00
|
|
|
/* Set default IP addresses if not specified */
|
2009-03-06 00:13:56 +00:00
|
|
|
#if !UIP_CONF_IPV6
|
2012-01-25 14:34:10 +00:00
|
|
|
{
|
|
|
|
uip_ipaddr_t addr;
|
2006-06-17 22:41:10 +00:00
|
|
|
|
2012-01-25 14:34:10 +00:00
|
|
|
uip_gethostaddr(&addr);
|
|
|
|
if(addr.u8[0] == 0) {
|
|
|
|
uip_ipaddr(&addr, 10,1,1,1);
|
|
|
|
}
|
|
|
|
printf("IP Address: %d.%d.%d.%d\n", uip_ipaddr_to_quad(&addr));
|
|
|
|
uip_sethostaddr(&addr);
|
|
|
|
|
|
|
|
uip_getnetmask(&addr);
|
|
|
|
if(addr.u8[0] == 0) {
|
|
|
|
uip_ipaddr(&addr, 255,0,0,0);
|
2012-01-25 14:41:39 +00:00
|
|
|
uip_setnetmask(&addr);
|
2012-01-25 14:34:10 +00:00
|
|
|
}
|
|
|
|
printf("Subnet Mask: %d.%d.%d.%d\n", uip_ipaddr_to_quad(&addr));
|
|
|
|
|
|
|
|
uip_getdraddr(&addr);
|
|
|
|
if(addr.u8[0] == 0) {
|
|
|
|
uip_ipaddr(&addr, 10,1,1,100);
|
|
|
|
uip_setdraddr(&addr);
|
|
|
|
}
|
|
|
|
printf("Def. Router: %d.%d.%d.%d\n", uip_ipaddr_to_quad(&addr));
|
2010-02-02 17:51:55 +00:00
|
|
|
}
|
2011-06-28 12:19:48 +00:00
|
|
|
#else /* UIP_CONF_IPV6 */
|
|
|
|
|
|
|
|
#if !UIP_CONF_IPV6_RPL
|
2012-01-25 14:34:10 +00:00
|
|
|
{
|
|
|
|
uip_ipaddr_t ipaddr;
|
2011-08-03 16:30:20 +00:00
|
|
|
#ifdef HARD_CODED_ADDRESS
|
2012-01-25 14:34:10 +00:00
|
|
|
uiplib_ipaddrconv(HARD_CODED_ADDRESS, &ipaddr);
|
2011-08-03 16:30:20 +00:00
|
|
|
#else
|
2012-01-25 14:34:10 +00:00
|
|
|
uip_ip6addr(&ipaddr, 0xaaaa, 0, 0, 0, 0, 0, 0, 0);
|
2011-08-03 16:30:20 +00:00
|
|
|
#endif
|
2012-01-25 14:34:10 +00:00
|
|
|
if((ipaddr.u16[0] != 0) ||
|
|
|
|
(ipaddr.u16[1] != 0) ||
|
|
|
|
(ipaddr.u16[2] != 0) ||
|
|
|
|
(ipaddr.u16[3] != 0)) {
|
2010-03-18 20:11:54 +00:00
|
|
|
#if UIP_CONF_ROUTER
|
2012-01-25 14:34:10 +00:00
|
|
|
uip_ds6_prefix_add(&ipaddr, UIP_DEFAULT_PREFIX_LEN, 0, 0, 0, 0);
|
2010-03-18 20:11:54 +00:00
|
|
|
#else /* UIP_CONF_ROUTER */
|
2012-01-25 14:34:10 +00:00
|
|
|
uip_ds6_prefix_add(&ipaddr, UIP_DEFAULT_PREFIX_LEN, 0);
|
2010-03-18 20:11:54 +00:00
|
|
|
#endif /* UIP_CONF_ROUTER */
|
2011-06-28 12:19:48 +00:00
|
|
|
|
2012-01-25 14:34:10 +00:00
|
|
|
uip_ds6_set_addr_iid(&ipaddr, &uip_lladdr);
|
|
|
|
uip_ds6_addr_add(&ipaddr, 0, ADDR_AUTOCONF);
|
|
|
|
}
|
2010-03-18 20:11:54 +00:00
|
|
|
}
|
2012-01-25 14:34:10 +00:00
|
|
|
#endif /* !UIP_CONF_IPV6_RPL */
|
|
|
|
|
2012-02-11 20:19:28 +00:00
|
|
|
#endif /* !UIP_CONF_IPV6 */
|
|
|
|
|
|
|
|
// procinit_init();
|
|
|
|
// autostart_start(autostart_processes);
|
|
|
|
|
|
|
|
/* Make standard output unbuffered. */
|
|
|
|
setvbuf(stdout, (char *)NULL, _IONBF, 0);
|
|
|
|
|
|
|
|
printf("\n*******%s online*******\n",CONTIKI_VERSION_STRING);
|
|
|
|
|
|
|
|
#if UIP_CONF_IPV6 && !RPL_BORDER_ROUTER /* Border router process prints addresses later */
|
2012-01-25 14:34:10 +00:00
|
|
|
{
|
|
|
|
uint8_t i;
|
|
|
|
for(i = 0; i < UIP_DS6_ADDR_NB; i++) {
|
2012-03-21 20:27:52 +00:00
|
|
|
if(uip_ds6_if.addr_list[i].isused) {
|
2012-01-25 14:34:10 +00:00
|
|
|
printf("IPV6 Addresss: ");
|
|
|
|
sprint_ip6(uip_ds6_if.addr_list[i].ipaddr);
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-02-11 20:19:28 +00:00
|
|
|
#endif
|
2011-07-24 15:53:36 +00:00
|
|
|
|
2006-06-17 22:41:10 +00:00
|
|
|
while(1) {
|
2009-03-06 00:13:56 +00:00
|
|
|
fd_set fds;
|
2006-06-17 22:41:10 +00:00
|
|
|
int n;
|
2007-11-28 12:54:41 +00:00
|
|
|
struct timeval tv;
|
2007-11-26 23:28:33 +00:00
|
|
|
|
2006-06-17 22:41:10 +00:00
|
|
|
n = process_run();
|
2007-11-26 23:28:33 +00:00
|
|
|
|
2007-11-28 12:54:41 +00:00
|
|
|
tv.tv_sec = 0;
|
2012-01-25 14:34:10 +00:00
|
|
|
tv.tv_usec = 1000;
|
2009-03-06 00:13:56 +00:00
|
|
|
FD_ZERO(&fds);
|
|
|
|
FD_SET(STDIN_FILENO, &fds);
|
|
|
|
select(1, &fds, NULL, NULL, &tv);
|
|
|
|
|
|
|
|
if(FD_ISSET(STDIN_FILENO, &fds)) {
|
|
|
|
char c;
|
|
|
|
if(read(STDIN_FILENO, &c, 1) > 0) {
|
2009-03-17 15:56:32 +00:00
|
|
|
serial_line_input_byte(c);
|
2009-03-06 00:13:56 +00:00
|
|
|
}
|
|
|
|
}
|
2006-06-17 22:41:10 +00:00
|
|
|
etimer_request_poll();
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2012-01-25 14:41:39 +00:00
|
|
|
void
|
|
|
|
log_message(char *m1, char *m2)
|
2006-06-17 22:41:10 +00:00
|
|
|
{
|
|
|
|
printf("%s%s\n", m1, m2);
|
|
|
|
}
|
2007-11-25 15:00:32 +00:00
|
|
|
/*---------------------------------------------------------------------------*/
|
2006-06-17 22:41:10 +00:00
|
|
|
void
|
|
|
|
uip_log(char *m)
|
|
|
|
{
|
|
|
|
printf("uIP: '%s'\n", m);
|
|
|
|
}
|
2007-11-25 15:00:32 +00:00
|
|
|
/*---------------------------------------------------------------------------*/
|
2006-06-17 22:41:10 +00:00
|
|
|
unsigned short
|
|
|
|
sensors_light1(void)
|
|
|
|
{
|
|
|
|
static unsigned short count;
|
|
|
|
return count++;
|
|
|
|
}
|
2007-11-25 15:00:32 +00:00
|
|
|
/*---------------------------------------------------------------------------*/
|