2006-06-18 00:41:10 +02: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
|
|
|
|
*
|
2011-01-21 15:19:57 +01:00
|
|
|
* $Id: contiki-main.c,v 1.14 2011/01/21 14:19:57 nvt-se Exp $
|
2006-06-18 00:41:10 +02:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2007-05-22 23:33:31 +02:00
|
|
|
#include <stdio.h>
|
2011-12-19 13:50:50 +01:00
|
|
|
#include <string.h>
|
2008-01-08 09:08:57 +01:00
|
|
|
#include <unistd.h>
|
2007-11-28 13:54:41 +01:00
|
|
|
#include <sys/select.h>
|
2007-05-22 23:33:31 +02:00
|
|
|
|
2012-01-08 19:37:56 +01:00
|
|
|
#ifdef __CYGWIN__
|
|
|
|
#include "net/wpcap-drv.h"
|
|
|
|
#endif /* __CYGWIN__ */
|
|
|
|
|
2006-06-18 00:41:10 +02:00
|
|
|
#include "contiki.h"
|
2010-02-23 19:43:21 +01:00
|
|
|
#include "net/netstack.h"
|
2006-06-18 00:41:10 +02:00
|
|
|
|
2009-03-17 16:56:32 +01:00
|
|
|
#include "dev/serial-line.h"
|
2008-01-08 09:08:57 +01:00
|
|
|
|
2006-06-18 00:41:10 +02:00
|
|
|
#include "net/uip.h"
|
|
|
|
|
|
|
|
#include "dev/button-sensor.h"
|
|
|
|
#include "dev/pir-sensor.h"
|
|
|
|
#include "dev/vib-sensor.h"
|
|
|
|
|
2011-12-19 13:50:50 +01:00
|
|
|
#if WITH_UIP6
|
|
|
|
#include "net/uip-ds6.h"
|
|
|
|
#endif /* WITH_UIP6 */
|
|
|
|
|
|
|
|
#include "net/rime.h"
|
|
|
|
|
|
|
|
|
2010-02-23 19:43:21 +01:00
|
|
|
PROCINIT(&etimer_process, &tcpip_process);
|
2006-06-18 00:41:10 +02:00
|
|
|
|
|
|
|
SENSORS(&pir_sensor, &vib_sensor, &button_sensor);
|
|
|
|
|
2011-12-19 13:50:50 +01:00
|
|
|
static uint8_t serial_id[] = {0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08};
|
2012-01-06 12:59:43 +01:00
|
|
|
static uint16_t node_id = 0x0102;
|
2006-06-18 00:41:10 +02:00
|
|
|
/*---------------------------------------------------------------------------*/
|
2011-12-19 13:50:50 +01:00
|
|
|
static void
|
|
|
|
set_rime_addr(void)
|
|
|
|
{
|
|
|
|
rimeaddr_t addr;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
memset(&addr, 0, sizeof(rimeaddr_t));
|
|
|
|
#if UIP_CONF_IPV6
|
|
|
|
memcpy(addr.u8, serial_id, sizeof(addr.u8));
|
|
|
|
#else
|
|
|
|
if(node_id == 0) {
|
|
|
|
for(i = 0; i < sizeof(rimeaddr_t); ++i) {
|
|
|
|
addr.u8[i] = serial_id[7 - i];
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
addr.u8[0] = node_id & 0xff;
|
|
|
|
addr.u8[1] = node_id >> 8;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
rimeaddr_set_node_addr(&addr);
|
|
|
|
printf("Rime started with address ");
|
|
|
|
for(i = 0; i < sizeof(addr.u8) - 1; i++) {
|
|
|
|
printf("%d.", addr.u8[i]);
|
|
|
|
}
|
|
|
|
printf("%d\n", addr.u8[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
int contiki_argc = 0;
|
|
|
|
char **contiki_argv;
|
|
|
|
|
2006-06-18 00:41:10 +02:00
|
|
|
int
|
2011-12-19 13:50:50 +01:00
|
|
|
main(int argc, char **argv)
|
2006-06-18 00:41:10 +02:00
|
|
|
{
|
2012-01-08 19:37:56 +01:00
|
|
|
#if UIP_CONF_IPV6
|
|
|
|
#if UIP_CONF_IPV6_RPL
|
|
|
|
printf("Starting Contiki IPV6, RPL\n");
|
|
|
|
#else
|
|
|
|
printf("Starting Contiki IPV6\n");
|
|
|
|
#endif
|
|
|
|
#else
|
|
|
|
printf("Starting Contiki IPV4\n");
|
|
|
|
#endif
|
2011-12-19 13:50:50 +01:00
|
|
|
|
|
|
|
/* crappy way of remembering and accessing argc/v */
|
|
|
|
contiki_argc = argc;
|
|
|
|
contiki_argv = argv;
|
|
|
|
|
2006-06-18 00:41:10 +02:00
|
|
|
process_init();
|
2010-02-03 17:47:26 +01:00
|
|
|
ctimer_init();
|
2010-02-23 19:43:21 +01:00
|
|
|
|
2011-12-19 13:50:50 +01:00
|
|
|
set_rime_addr();
|
|
|
|
|
|
|
|
#if WITH_UIP6
|
|
|
|
memcpy(&uip_lladdr.addr, serial_id, sizeof(uip_lladdr.addr));
|
|
|
|
|
|
|
|
process_start(&tcpip_process, NULL);
|
2012-01-08 19:37:56 +01:00
|
|
|
#ifdef __CYGWIN__
|
|
|
|
process_start(&wpcap_process, NULL);
|
|
|
|
#endif
|
2011-12-19 13:50:50 +01: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]);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
queuebuf_init();
|
|
|
|
|
2010-02-23 19:43:21 +01:00
|
|
|
netstack_init();
|
2010-02-03 17:47:26 +01:00
|
|
|
|
2006-06-18 00:41:10 +02:00
|
|
|
procinit_init();
|
2010-02-23 19:43:21 +01:00
|
|
|
|
|
|
|
serial_line_init();
|
2006-06-18 00:41:10 +02:00
|
|
|
|
2008-02-10 23:36:36 +01:00
|
|
|
autostart_start(autostart_processes);
|
2010-02-23 19:43:21 +01:00
|
|
|
|
2008-01-08 09:08:57 +01:00
|
|
|
/* Make standard output unbuffered. */
|
|
|
|
setvbuf(stdout, (char *)NULL, _IONBF, 0);
|
|
|
|
|
2006-06-18 00:41:10 +02:00
|
|
|
while(1) {
|
2011-12-19 13:50:50 +01:00
|
|
|
fd_set fdr;
|
|
|
|
fd_set fdw;
|
|
|
|
int maxfd;
|
2012-01-04 17:32:46 +01:00
|
|
|
int retval;
|
2007-11-28 13:54:41 +01:00
|
|
|
struct timeval tv;
|
2007-11-27 00:28:33 +01:00
|
|
|
|
2012-01-04 17:32:46 +01:00
|
|
|
process_run();
|
2008-01-08 09:08:57 +01:00
|
|
|
|
2007-11-28 13:54:41 +01:00
|
|
|
tv.tv_sec = 0;
|
2012-01-04 17:32:46 +01:00
|
|
|
tv.tv_usec = 1000;
|
2008-01-08 09:08:57 +01:00
|
|
|
|
2011-12-19 13:50:50 +01:00
|
|
|
FD_ZERO(&fdr);
|
|
|
|
FD_ZERO(&fdw);
|
|
|
|
FD_SET(STDIN_FILENO, &fdr);
|
|
|
|
maxfd = STDIN_FILENO;
|
|
|
|
|
|
|
|
#ifdef SELECT_CALLBACK
|
|
|
|
maxfd = SELECT_CALLBACK->set_fd(maxfd, &fdr, &fdw);
|
|
|
|
#endif
|
2012-01-04 17:32:46 +01:00
|
|
|
if((retval = select(maxfd + 1, &fdr, &fdw, NULL, &tv)) < 0) {
|
2011-01-21 15:19:57 +01:00
|
|
|
perror("select");
|
2012-01-04 17:32:46 +01:00
|
|
|
} else if(retval > 0) {
|
|
|
|
/* timeout => retval == 0 */
|
2011-12-19 13:50:50 +01:00
|
|
|
if(FD_ISSET(STDIN_FILENO, &fdr)) {
|
|
|
|
char c;
|
|
|
|
if(read(STDIN_FILENO, &c, 1) > 0) {
|
|
|
|
serial_line_input_byte(c);
|
|
|
|
}
|
2008-01-08 09:08:57 +01:00
|
|
|
}
|
2011-12-19 13:50:50 +01:00
|
|
|
#ifdef SELECT_CALLBACK
|
|
|
|
SELECT_CALLBACK->handle_fd(&fdr, &fdw);
|
|
|
|
#endif
|
2008-01-08 09:08:57 +01:00
|
|
|
}
|
2011-12-19 13:50:50 +01:00
|
|
|
|
2006-06-18 00:41:10 +02:00
|
|
|
etimer_request_poll();
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2008-01-08 09:08:57 +01:00
|
|
|
void
|
|
|
|
log_message(char *m1, char *m2)
|
2006-06-18 00:41:10 +02:00
|
|
|
{
|
|
|
|
printf("%s%s\n", m1, m2);
|
|
|
|
}
|
2008-01-08 09:08:57 +01:00
|
|
|
/*---------------------------------------------------------------------------*/
|
2006-06-18 00:41:10 +02:00
|
|
|
void
|
|
|
|
uip_log(char *m)
|
|
|
|
{
|
|
|
|
printf("%s\n", m);
|
|
|
|
}
|
2008-01-08 09:08:57 +01:00
|
|
|
/*---------------------------------------------------------------------------*/
|