Skip to content

Commit

Permalink
platform/minimal-net: Implement better idle behavior.
Browse files Browse the repository at this point in the history
The minimal-net target, as currently written, wakes up the
CPU every millisecond to check for packets, and will only
react in real-time to input from stdin. If you are running
this on a laptop battery, your battery will quickly drain.

This change allows the CPU to idle when there is literally
nothing to do while still being responsive to input from
stein and/or incoming packets. This fix should significantly
improve performance while significantly improving power
usage. Win-win.

Also added `_xassert()` implementation so that the contiki-
provided `assert()` macro will work properly when used
on this platform.
  • Loading branch information
darconeous committed Jan 30, 2013
1 parent 0a88373 commit 092b6f3
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 9 deletions.
1 change: 0 additions & 1 deletion cpu/native/net/tapdev-drv.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ tapdev_output(void)
static void
pollhandler(void)
{
process_poll(&tapdev_process);
uip_len = tapdev_poll();

if(uip_len > 0) {
Expand Down
1 change: 1 addition & 0 deletions cpu/native/net/tapdev-drv.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,6 @@
PROCESS_NAME(tapdev_process);

uint8_t tapdev_output(void);
int tapdev_fd(void);

#endif /* __TAPDEV_DRV_H__ */
7 changes: 7 additions & 0 deletions cpu/native/net/tapdev.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 8 additions & 0 deletions cpu/native/net/tapdev6.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -161,6 +168,7 @@ tapdev_init(void)
/* gdk_input_add(fd, GDK_INPUT_READ,
read_callback, NULL);*/

atexit(&tapdev_exit);
}
/*---------------------------------------------------------------------------*/
static void
Expand Down
63 changes: 55 additions & 8 deletions platform/minimal-net/contiki-main.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,16 @@
*/

#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <time.h>
#include <sys/select.h>
#include <unistd.h>
#include <memory.h>

#include "contiki.h"
#include "contiki-net.h"
#include "lib/assert.h"

#include "dev/serial-line.h"

Expand Down Expand Up @@ -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);
Expand All @@ -290,36 +299,66 @@ 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

while(1) {
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;
if(read(STDIN_FILENO, &c, 1) > 0) {
serial_line_input_byte(c);
}
}
process_poll(&tapdev_process);
etimer_request_poll();
}

Expand All @@ -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)
{
Expand Down

0 comments on commit 092b6f3

Please sign in to comment.