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
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2014-09-24 23:04:25 +00:00
|
|
|
/**
|
|
|
|
* \ingroup platform
|
|
|
|
*
|
|
|
|
* \defgroup native_platform Native platform
|
|
|
|
*
|
|
|
|
* Platform running in the host (Windows or Linux) environment.
|
|
|
|
*
|
|
|
|
* Used mainly for development and debugging.
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
2007-05-22 21:33:31 +00:00
|
|
|
#include <stdio.h>
|
2011-12-19 12:50:50 +00:00
|
|
|
#include <string.h>
|
2008-01-08 08:08:57 +00:00
|
|
|
#include <unistd.h>
|
2007-11-28 12:54:41 +00:00
|
|
|
#include <sys/select.h>
|
2014-04-22 12:38:08 +00:00
|
|
|
#include <errno.h>
|
2007-05-22 21:33:31 +00:00
|
|
|
|
2012-01-08 18:37:56 +00:00
|
|
|
#ifdef __CYGWIN__
|
|
|
|
#include "net/wpcap-drv.h"
|
|
|
|
#endif /* __CYGWIN__ */
|
|
|
|
|
2006-06-17 22:41:10 +00:00
|
|
|
#include "contiki.h"
|
2010-02-23 18:43:21 +00:00
|
|
|
#include "net/netstack.h"
|
2006-06-17 22:41:10 +00:00
|
|
|
|
2009-03-17 15:56:32 +00:00
|
|
|
#include "dev/serial-line.h"
|
2008-01-08 08:08:57 +00:00
|
|
|
|
2017-09-22 13:06:44 +00:00
|
|
|
#include "net/ipv6/uip.h"
|
2017-10-01 19:40:51 +00:00
|
|
|
#include "net/ipv6/uip-debug.h"
|
2017-05-13 11:09:58 +00:00
|
|
|
#include "net/queuebuf.h"
|
2006-06-17 22:41:10 +00:00
|
|
|
|
|
|
|
#include "dev/button-sensor.h"
|
|
|
|
#include "dev/pir-sensor.h"
|
|
|
|
#include "dev/vib-sensor.h"
|
|
|
|
|
2014-12-01 20:02:57 +00:00
|
|
|
#if NETSTACK_CONF_WITH_IPV6
|
2013-11-22 08:17:54 +00:00
|
|
|
#include "net/ipv6/uip-ds6.h"
|
2014-12-01 20:02:57 +00:00
|
|
|
#endif /* NETSTACK_CONF_WITH_IPV6 */
|
2011-12-19 12:50:50 +00:00
|
|
|
|
2017-10-12 07:15:39 +00:00
|
|
|
#if BUILD_WITH_SHELL
|
|
|
|
#include "serial-shell.h"
|
|
|
|
#endif /* BUILD_WITH_SHELL */
|
|
|
|
|
2012-01-18 16:44:31 +00:00
|
|
|
#ifdef SELECT_CONF_MAX
|
|
|
|
#define SELECT_MAX SELECT_CONF_MAX
|
|
|
|
#else
|
|
|
|
#define SELECT_MAX 8
|
|
|
|
#endif
|
2011-12-19 12:50:50 +00:00
|
|
|
|
2012-01-18 16:44:31 +00:00
|
|
|
static const struct select_callback *select_callback[SELECT_MAX];
|
|
|
|
static int select_max = 0;
|
2006-06-17 22:41:10 +00:00
|
|
|
|
|
|
|
SENSORS(&pir_sensor, &vib_sensor, &button_sensor);
|
|
|
|
|
2011-12-19 12:50:50 +00:00
|
|
|
static uint8_t serial_id[] = {0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08};
|
2015-10-01 10:01:05 +00:00
|
|
|
#if !NETSTACK_CONF_WITH_IPV6
|
2012-01-06 11:59:43 +00:00
|
|
|
static uint16_t node_id = 0x0102;
|
2015-10-01 10:01:05 +00:00
|
|
|
#endif /* !NETSTACK_CONF_WITH_IPV6 */
|
2006-06-17 22:41:10 +00:00
|
|
|
/*---------------------------------------------------------------------------*/
|
2012-01-18 16:44:31 +00:00
|
|
|
int
|
|
|
|
select_set_callback(int fd, const struct select_callback *callback)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
if(fd >= 0 && fd < SELECT_MAX) {
|
|
|
|
/* Check that the callback functions are set */
|
|
|
|
if(callback != NULL &&
|
|
|
|
(callback->set_fd == NULL || callback->handle_fd == NULL)) {
|
|
|
|
callback = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
select_callback[fd] = callback;
|
|
|
|
|
|
|
|
/* Update fd max */
|
|
|
|
if(callback != NULL) {
|
|
|
|
if(fd > select_max) {
|
|
|
|
select_max = fd;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
select_max = 0;
|
|
|
|
for(i = SELECT_MAX - 1; i > 0; i--) {
|
|
|
|
if(select_callback[i] != NULL) {
|
|
|
|
select_max = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static int
|
|
|
|
stdin_set_fd(fd_set *rset, fd_set *wset)
|
|
|
|
{
|
|
|
|
FD_SET(STDIN_FILENO, rset);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
static void
|
|
|
|
stdin_handle_fd(fd_set *rset, fd_set *wset)
|
|
|
|
{
|
|
|
|
char c;
|
|
|
|
if(FD_ISSET(STDIN_FILENO, rset)) {
|
|
|
|
if(read(STDIN_FILENO, &c, 1) > 0) {
|
|
|
|
serial_line_input_byte(c);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const static struct select_callback stdin_fd = {
|
|
|
|
stdin_set_fd, stdin_handle_fd
|
|
|
|
};
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2011-12-19 12:50:50 +00:00
|
|
|
static void
|
2017-06-22 16:27:04 +00:00
|
|
|
set_lladdr(void)
|
2011-12-19 12:50:50 +00:00
|
|
|
{
|
2013-12-12 22:58:52 +00:00
|
|
|
linkaddr_t addr;
|
2011-12-19 12:50:50 +00:00
|
|
|
int i;
|
|
|
|
|
2013-12-12 22:58:52 +00:00
|
|
|
memset(&addr, 0, sizeof(linkaddr_t));
|
2014-12-01 20:02:57 +00:00
|
|
|
#if NETSTACK_CONF_WITH_IPV6
|
2011-12-19 12:50:50 +00:00
|
|
|
memcpy(addr.u8, serial_id, sizeof(addr.u8));
|
|
|
|
#else
|
|
|
|
if(node_id == 0) {
|
2013-12-12 22:58:52 +00:00
|
|
|
for(i = 0; i < sizeof(linkaddr_t); ++i) {
|
2011-12-19 12:50:50 +00:00
|
|
|
addr.u8[i] = serial_id[7 - i];
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
addr.u8[0] = node_id & 0xff;
|
|
|
|
addr.u8[1] = node_id >> 8;
|
|
|
|
}
|
|
|
|
#endif
|
2013-12-12 22:58:52 +00:00
|
|
|
linkaddr_set_node_addr(&addr);
|
2017-06-22 16:27:04 +00:00
|
|
|
printf("Contiki started with address ");
|
2011-12-19 12:50:50 +00:00
|
|
|
for(i = 0; i < sizeof(addr.u8) - 1; i++) {
|
|
|
|
printf("%d.", addr.u8[i]);
|
|
|
|
}
|
|
|
|
printf("%d\n", addr.u8[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-10-01 19:40:51 +00:00
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static void
|
|
|
|
set_global_address(void)
|
|
|
|
{
|
|
|
|
static uip_ipaddr_t ipaddr;
|
|
|
|
static uip_ipaddr_t *prefix = NULL;
|
|
|
|
int i;
|
|
|
|
uint8_t state;
|
|
|
|
|
|
|
|
/* Assign a unique local address (RFC4193,
|
|
|
|
http://tools.ietf.org/html/rfc4193). */
|
|
|
|
if(prefix == NULL) {
|
|
|
|
uip_ip6addr(&ipaddr, UIP_DS6_DEFAULT_PREFIX, 0, 0, 0, 0, 0, 0, 0);
|
|
|
|
} else {
|
|
|
|
memcpy(&ipaddr, prefix, 8);
|
|
|
|
}
|
|
|
|
/* Assumes that the uip_lladdr is set */
|
|
|
|
uip_ds6_set_addr_iid(&ipaddr, &uip_lladdr);
|
|
|
|
uip_ds6_addr_add(&ipaddr, 0, ADDR_AUTOCONF);
|
|
|
|
|
|
|
|
/* set the PREFIX::1 address to the IF */
|
|
|
|
uip_ip6addr(&ipaddr, UIP_DS6_DEFAULT_PREFIX, 0, 0, 0, 0, 0, 0, 1);
|
|
|
|
uip_ds6_defrt_add(&ipaddr, 0);
|
|
|
|
|
|
|
|
printf("IPv6 addresses: ");
|
|
|
|
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)) {
|
|
|
|
uip_debug_ipaddr_print(&uip_ds6_if.addr_list[i].ipaddr);
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-19 12:50:50 +00:00
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
int contiki_argc = 0;
|
|
|
|
char **contiki_argv;
|
|
|
|
|
2006-06-17 22:41:10 +00:00
|
|
|
int
|
2011-12-19 12:50:50 +00:00
|
|
|
main(int argc, char **argv)
|
2006-06-17 22:41:10 +00:00
|
|
|
{
|
2014-12-01 20:02:57 +00:00
|
|
|
#if NETSTACK_CONF_WITH_IPV6
|
2012-01-08 18:37:56 +00:00
|
|
|
#if UIP_CONF_IPV6_RPL
|
2012-01-18 16:44:31 +00:00
|
|
|
printf(CONTIKI_VERSION_STRING " started with IPV6, RPL\n");
|
2012-01-08 18:37:56 +00:00
|
|
|
#else
|
2012-01-18 16:44:31 +00:00
|
|
|
printf(CONTIKI_VERSION_STRING " started with IPV6\n");
|
2012-01-08 18:37:56 +00:00
|
|
|
#endif
|
|
|
|
#else
|
2012-01-18 16:44:31 +00:00
|
|
|
printf(CONTIKI_VERSION_STRING " started\n");
|
2012-01-08 18:37:56 +00:00
|
|
|
#endif
|
2011-12-19 12:50:50 +00:00
|
|
|
|
|
|
|
/* crappy way of remembering and accessing argc/v */
|
|
|
|
contiki_argc = argc;
|
|
|
|
contiki_argv = argv;
|
|
|
|
|
2012-03-27 09:09:54 +00:00
|
|
|
/* native under windows is hardcoded to use the first one or two args */
|
|
|
|
/* for wpcap configuration so this needs to be "removed" from */
|
|
|
|
/* contiki_args (used by the native-border-router) */
|
|
|
|
#ifdef __CYGWIN__
|
|
|
|
contiki_argc--;
|
|
|
|
contiki_argv++;
|
|
|
|
#ifdef UIP_FALLBACK_INTERFACE
|
|
|
|
contiki_argc--;
|
|
|
|
contiki_argv++;
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
2006-06-17 22:41:10 +00:00
|
|
|
process_init();
|
2012-01-18 16:44:31 +00:00
|
|
|
process_start(&etimer_process, NULL);
|
2010-02-03 16:47:26 +00:00
|
|
|
ctimer_init();
|
2014-04-22 12:35:01 +00:00
|
|
|
rtimer_init();
|
2010-02-23 18:43:21 +00:00
|
|
|
|
2017-06-22 16:27:04 +00:00
|
|
|
set_lladdr();
|
2012-01-18 16:44:31 +00:00
|
|
|
|
|
|
|
netstack_init();
|
2017-05-17 13:09:19 +00:00
|
|
|
printf("MAC %s NETWORK %s\n", NETSTACK_MAC.name, NETSTACK_NETWORK.name);
|
2011-12-19 12:50:50 +00:00
|
|
|
|
2014-12-01 20:02:57 +00:00
|
|
|
#if NETSTACK_CONF_WITH_IPV6
|
2014-04-22 12:40:04 +00:00
|
|
|
queuebuf_init();
|
|
|
|
|
2011-12-19 12:50:50 +00:00
|
|
|
memcpy(&uip_lladdr.addr, serial_id, sizeof(uip_lladdr.addr));
|
|
|
|
|
|
|
|
process_start(&tcpip_process, NULL);
|
2012-01-08 18:37:56 +00:00
|
|
|
#ifdef __CYGWIN__
|
|
|
|
process_start(&wpcap_process, NULL);
|
|
|
|
#endif
|
2017-10-01 19:40:51 +00:00
|
|
|
|
2011-12-19 12:50:50 +00:00
|
|
|
printf("Tentative link-local IPv6 address ");
|
|
|
|
{
|
|
|
|
uip_ds6_addr_t *lladdr;
|
|
|
|
int i;
|
|
|
|
lladdr = uip_ds6_get_link_local(-1);
|
|
|
|
for(i = 0; i < 7; ++i) {
|
|
|
|
printf("%02x%02x:", lladdr->ipaddr.u8[i * 2],
|
|
|
|
lladdr->ipaddr.u8[i * 2 + 1]);
|
|
|
|
}
|
|
|
|
/* make it hardcoded... */
|
|
|
|
lladdr->state = ADDR_AUTOCONF;
|
|
|
|
|
|
|
|
printf("%02x%02x\n", lladdr->ipaddr.u8[14], lladdr->ipaddr.u8[15]);
|
|
|
|
}
|
2017-10-01 19:40:51 +00:00
|
|
|
|
|
|
|
set_global_address();
|
|
|
|
|
2017-06-16 15:02:42 +00:00
|
|
|
#endif /* NETSTACK_CONF_WITH_IPV6 */
|
2011-12-19 12:50:50 +00:00
|
|
|
|
2010-02-23 18:43:21 +00:00
|
|
|
serial_line_init();
|
2013-03-19 18:54:09 +00:00
|
|
|
|
2017-10-12 07:15:39 +00:00
|
|
|
#if BUILD_WITH_SHELL
|
|
|
|
serial_shell_init();
|
|
|
|
#endif /* BUILD_WITH_SHELL */
|
|
|
|
|
2008-02-10 22:36:36 +00:00
|
|
|
autostart_start(autostart_processes);
|
2013-03-19 18:54:09 +00:00
|
|
|
|
2008-01-08 08:08:57 +00:00
|
|
|
/* Make standard output unbuffered. */
|
|
|
|
setvbuf(stdout, (char *)NULL, _IONBF, 0);
|
2012-01-18 16:44:31 +00:00
|
|
|
|
|
|
|
select_set_callback(STDIN_FILENO, &stdin_fd);
|
2006-06-17 22:41:10 +00:00
|
|
|
while(1) {
|
2011-12-19 12:50:50 +00:00
|
|
|
fd_set fdr;
|
|
|
|
fd_set fdw;
|
|
|
|
int maxfd;
|
2012-01-18 16:44:31 +00:00
|
|
|
int i;
|
2012-01-04 16:32:46 +00:00
|
|
|
int retval;
|
2007-11-28 12:54:41 +00:00
|
|
|
struct timeval tv;
|
2012-01-18 16:44:31 +00:00
|
|
|
|
|
|
|
retval = process_run();
|
2008-01-08 08:08:57 +00:00
|
|
|
|
2007-11-28 12:54:41 +00:00
|
|
|
tv.tv_sec = 0;
|
2012-01-18 16:44:31 +00:00
|
|
|
tv.tv_usec = retval ? 1 : 1000;
|
2008-01-08 08:08:57 +00:00
|
|
|
|
2011-12-19 12:50:50 +00:00
|
|
|
FD_ZERO(&fdr);
|
|
|
|
FD_ZERO(&fdw);
|
2012-01-18 16:44:31 +00:00
|
|
|
maxfd = 0;
|
|
|
|
for(i = 0; i <= select_max; i++) {
|
|
|
|
if(select_callback[i] != NULL && select_callback[i]->set_fd(&fdr, &fdw)) {
|
|
|
|
maxfd = i;
|
|
|
|
}
|
|
|
|
}
|
2011-12-19 12:50:50 +00:00
|
|
|
|
2012-01-18 16:44:31 +00:00
|
|
|
retval = select(maxfd + 1, &fdr, &fdw, NULL, &tv);
|
|
|
|
if(retval < 0) {
|
2014-04-22 12:38:08 +00:00
|
|
|
if(errno != EINTR) {
|
|
|
|
perror("select");
|
|
|
|
}
|
2012-01-04 16:32:46 +00:00
|
|
|
} else if(retval > 0) {
|
|
|
|
/* timeout => retval == 0 */
|
2012-01-18 16:44:31 +00:00
|
|
|
for(i = 0; i <= maxfd; i++) {
|
|
|
|
if(select_callback[i] != NULL) {
|
|
|
|
select_callback[i]->handle_fd(&fdr, &fdw);
|
|
|
|
}
|
2008-01-08 08:08:57 +00:00
|
|
|
}
|
|
|
|
}
|
2011-12-19 12:50:50 +00:00
|
|
|
|
2006-06-17 22:41:10 +00:00
|
|
|
etimer_request_poll();
|
|
|
|
}
|
2012-01-18 16:44:31 +00:00
|
|
|
|
2006-06-17 22:41:10 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2008-01-08 08:08:57 +00:00
|
|
|
void
|
|
|
|
log_message(char *m1, char *m2)
|
2006-06-17 22:41:10 +00:00
|
|
|
{
|
2012-11-12 21:24:52 +00:00
|
|
|
fprintf(stderr, "%s%s\n", m1, m2);
|
2006-06-17 22:41:10 +00:00
|
|
|
}
|
2008-01-08 08:08:57 +00:00
|
|
|
/*---------------------------------------------------------------------------*/
|
2006-06-17 22:41:10 +00:00
|
|
|
void
|
|
|
|
uip_log(char *m)
|
|
|
|
{
|
2012-11-12 21:24:52 +00:00
|
|
|
fprintf(stderr, "%s\n", m);
|
2006-06-17 22:41:10 +00:00
|
|
|
}
|
2008-01-08 08:08:57 +00:00
|
|
|
/*---------------------------------------------------------------------------*/
|
2014-09-24 23:04:25 +00:00
|
|
|
/** @} */
|