diff --git a/cpu/native/net/tapdev-drv.c b/cpu/native/net/tapdev-drv.c index 0e4c0a29d..a4f50899e 100644 --- a/cpu/native/net/tapdev-drv.c +++ b/cpu/native/net/tapdev-drv.c @@ -59,7 +59,6 @@ tapdev_output(void) static void pollhandler(void) { - process_poll(&tapdev_process); uip_len = tapdev_poll(); if(uip_len > 0) { diff --git a/cpu/native/net/tapdev-drv.h b/cpu/native/net/tapdev-drv.h index 25014ea29..3241fc344 100644 --- a/cpu/native/net/tapdev-drv.h +++ b/cpu/native/net/tapdev-drv.h @@ -38,5 +38,6 @@ PROCESS_NAME(tapdev_process); uint8_t tapdev_output(void); +int tapdev_fd(void); #endif /* __TAPDEV_DRV_H__ */ diff --git a/cpu/native/net/tapdev.c b/cpu/native/net/tapdev.c index cc294d7a7..377b4c225 100644 --- a/cpu/native/net/tapdev.c +++ b/cpu/native/net/tapdev.c @@ -69,6 +69,13 @@ static unsigned long lasttime; #define BUF ((struct uip_eth_hdr *)&uip_buf[0]) +/*---------------------------------------------------------------------------*/ +int +tapdev_fd(void) +{ + return fd; +} + /*---------------------------------------------------------------------------*/ static void remove_route(void) diff --git a/cpu/native/net/tapdev6.c b/cpu/native/net/tapdev6.c index e687ada10..441d2fee5 100644 --- a/cpu/native/net/tapdev6.c +++ b/cpu/native/net/tapdev6.c @@ -85,6 +85,13 @@ static unsigned long lasttime; static void do_send(void); uint8_t tapdev_send(uip_lladdr_t *lladdr); +/*---------------------------------------------------------------------------*/ +int +tapdev_fd(void) +{ + return fd; +} + uint16_t tapdev_poll(void) @@ -161,6 +168,7 @@ tapdev_init(void) /* gdk_input_add(fd, GDK_INPUT_READ, read_callback, NULL);*/ + atexit(&tapdev_exit); } /*---------------------------------------------------------------------------*/ static void diff --git a/platform/minimal-net/contiki-main.c b/platform/minimal-net/contiki-main.c index 64301d38f..6562b3ecd 100644 --- a/platform/minimal-net/contiki-main.c +++ b/platform/minimal-net/contiki-main.c @@ -33,6 +33,8 @@ */ #include +#include +#include #include #include #include @@ -40,6 +42,7 @@ #include "contiki.h" #include "contiki-net.h" +#include "lib/assert.h" #include "dev/serial-line.h" @@ -267,9 +270,15 @@ main(void) (ipaddr.u16[2] != 0) || (ipaddr.u16[3] != 0)) { #if UIP_CONF_ROUTER - uip_ds6_prefix_add(&ipaddr, UIP_DEFAULT_PREFIX_LEN, 0, 0, 0, 0); + if(!uip_ds6_prefix_add(&ipaddr, UIP_DEFAULT_PREFIX_LEN, 0, 0, 0, 0)) { + fprintf(stderr,"uip_ds6_prefix_add() failed.\n"); + exit(EXIT_FAILURE); + } #else /* UIP_CONF_ROUTER */ - uip_ds6_prefix_add(&ipaddr, UIP_DEFAULT_PREFIX_LEN, 0); + if(!uip_ds6_prefix_add(&ipaddr, UIP_DEFAULT_PREFIX_LEN, 0)) { + fprintf(stderr,"uip_ds6_prefix_add() failed.\n"); + exit(EXIT_FAILURE); + } #endif /* UIP_CONF_ROUTER */ uip_ds6_set_addr_iid(&ipaddr, &uip_lladdr); @@ -290,14 +299,17 @@ main(void) #if UIP_CONF_IPV6 && !RPL_BORDER_ROUTER /* Border router process prints addresses later */ { - uint8_t i; + int i = 0; + int interface_count = 0; for(i = 0; i < UIP_DS6_ADDR_NB; i++) { if(uip_ds6_if.addr_list[i].isused) { - printf("IPV6 Addresss: "); - sprint_ip6(uip_ds6_if.addr_list[i].ipaddr); - printf("\n"); + printf("IPV6 Addresss: "); + sprint_ip6(uip_ds6_if.addr_list[i].ipaddr); + printf("\n"); + interface_count++; } } + assert(0 < interface_count); } #endif @@ -305,14 +317,40 @@ main(void) fd_set fds; int n; struct timeval tv; + clock_time_t next_event; n = process_run(); + next_event = etimer_next_expiration_time() - clock_time(); + +#if DEBUG_SLEEP + if(n > 0) + printf("sleep: %d events pending\n",n); + else + printf("sleep: next event @ T-%.03f\n",(double)next_event / (double)CLOCK_SECOND); +#endif + +#ifdef __CYGWIN__ + /* wpcap doesn't appear to support select, so + * we can't idle the process on windows. */ + next_event = 0; +#endif + + if(next_event > (CLOCK_SECOND * 2)) + next_event = CLOCK_SECOND * 2; + tv.tv_sec = n ? 0 : (next_event / CLOCK_SECOND); + tv.tv_usec = n ? 0 : ((next_event % 1000) * 1000); - tv.tv_sec = 0; - tv.tv_usec = 1000; FD_ZERO(&fds); FD_SET(STDIN_FILENO, &fds); +#ifdef __CYGWIN__ select(1, &fds, NULL, NULL, &tv); +#else + FD_SET(tapdev_fd(), &fds); + if(0 > select(tapdev_fd() + 1, &fds, NULL, NULL, &tv)) { + perror("Call to select() failed."); + exit(EXIT_FAILURE); + } +#endif if(FD_ISSET(STDIN_FILENO, &fds)) { char c; @@ -320,6 +358,7 @@ main(void) serial_line_input_byte(c); } } + process_poll(&tapdev_process); etimer_request_poll(); } @@ -338,6 +377,14 @@ uip_log(char *m) printf("uIP: '%s'\n", m); } /*---------------------------------------------------------------------------*/ +void +_xassert(const char *file, int line) +{ + fprintf(stderr, "%s:%u: failed assertion\n", file, line); + abort(); +} + + unsigned short sensors_light1(void) {