1
0
mirror of git://projects.qi-hardware.com/ben-wpan.git synced 2024-11-22 14:40:17 +02:00

tools/lib/timeout.h, tools/lib/timeout.c: added timeout/deadline functions

This commit is contained in:
Werner Almesberger 2011-06-20 19:02:04 -03:00
parent 106ef7ff7f
commit 4d4cec6a67
3 changed files with 99 additions and 1 deletions

View File

@ -19,7 +19,8 @@ OBJS_host = atusb.o atusb-spi.o atusb-common.o usbopen.o
OBJS_ben_jlime = atben.o OBJS_ben_jlime = atben.o
OBJS_ben_openwrt = atben.o OBJS_ben_openwrt = atben.o
OBJS = atrf.o atnet.o misctxrx.o cwtest.o netio.o daemon.o $(OBJS_$(TARGET)) OBJS = atrf.o atnet.o misctxrx.o cwtest.o netio.o daemon.o timeout.o \
$(OBJS_$(TARGET))
.PHONY: all clean spotless .PHONY: all clean spotless

68
tools/lib/timeout.c Normal file
View File

@ -0,0 +1,68 @@
/*
* lib/timeout.c - Set up AT86RF230/231 constant wave test mode
*
* Written 2011 by Werner Almesberger
* Copyright 2011 Werner Almesberger
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*/
#include <stdlib.h>
#include <stdio.h>
#include <sys/time.h>
#include "timeout.h"
void timeout_start(struct timeout *t, int ms)
{
if (gettimeofday(&t->end, NULL) < 0) {
perror("gettimeofday");
exit(1);
}
t->end.tv_sec += ms/1000;
t->end.tv_usec += 1000*(ms % 1000);
if (t->end.tv_usec > 999999) {
t->end.tv_sec++;
t->end.tv_usec -= 1000000;
}
}
int timeout_reached(const struct timeout *t)
{
struct timeval now;
if (gettimeofday(&now, NULL) < 0) {
perror("gettimeofday");
exit(1);
}
if (now.tv_sec > t->end.tv_sec)
return 1;
if (now.tv_sec < t->end.tv_sec)
return 0;
return now.tv_usec >= t->end.tv_usec;
}
int timeout_left_ms(const struct timeout *t)
{
struct timeval now;
int ms;
if (gettimeofday(&now, NULL) < 0) {
perror("gettimeofday");
exit(1);
}
now.tv_sec = t->end.tv_sec-now.tv_sec;
now.tv_usec = t->end.tv_usec-now.tv_usec;
if (now.tv_usec < 0) {
now.tv_sec--;
now.tv_usec += 1000000;
}
return now.tv_sec*1000+now.tv_usec/1000;
}

29
tools/lib/timeout.h Normal file
View File

@ -0,0 +1,29 @@
/*
* lib/timeout.h - ATRF driver API
*
* Written 2011 by Werner Almesberger
* Copyright 2011 Werner Almesberger
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*/
#ifndef TIMEOUT_H
#define TIMEOUT_H
#include <sys/time.h>
struct timeout {
struct timeval end;
};
void timeout_start(struct timeout *t, int ms);
int timeout_reached(const struct timeout *t);
int timeout_left_ms(const struct timeout *t);
#endif /* !TIMEOUT_H */