Date:2011-06-21 00:02:04 (9 years 1 month ago)
Author:Werner Almesberger
Commit:4d4cec6a6750e91c6da9849928a8c80ec47c6991
Message:tools/lib/timeout.h, tools/lib/timeout.c: added timeout/deadline functions

Files: tools/lib/Makefile (1 diff)
tools/lib/timeout.c (1 diff)
tools/lib/timeout.h (1 diff)

Change Details

tools/lib/Makefile
1919OBJS_ben_jlime = atben.o
2020OBJS_ben_openwrt = atben.o
2121
22OBJS = atrf.o atnet.o misctxrx.o cwtest.o netio.o daemon.o $(OBJS_$(TARGET))
22OBJS = atrf.o atnet.o misctxrx.o cwtest.o netio.o daemon.o timeout.o \
23       $(OBJS_$(TARGET))
2324
2425.PHONY: all clean spotless
2526
tools/lib/timeout.c
1/*
2 * lib/timeout.c - Set up AT86RF230/231 constant wave test mode
3 *
4 * Written 2011 by Werner Almesberger
5 * Copyright 2011 Werner Almesberger
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 */
12
13
14#include <stdlib.h>
15#include <stdio.h>
16#include <sys/time.h>
17
18#include "timeout.h"
19
20
21void timeout_start(struct timeout *t, int ms)
22{
23    if (gettimeofday(&t->end, NULL) < 0) {
24        perror("gettimeofday");
25        exit(1);
26    }
27    t->end.tv_sec += ms/1000;
28    t->end.tv_usec += 1000*(ms % 1000);
29    if (t->end.tv_usec > 999999) {
30        t->end.tv_sec++;
31        t->end.tv_usec -= 1000000;
32    }
33}
34
35
36int timeout_reached(const struct timeout *t)
37{
38    struct timeval now;
39
40    if (gettimeofday(&now, NULL) < 0) {
41        perror("gettimeofday");
42        exit(1);
43    }
44    if (now.tv_sec > t->end.tv_sec)
45        return 1;
46    if (now.tv_sec < t->end.tv_sec)
47        return 0;
48    return now.tv_usec >= t->end.tv_usec;
49}
50
51
52int timeout_left_ms(const struct timeout *t)
53{
54    struct timeval now;
55    int ms;
56
57    if (gettimeofday(&now, NULL) < 0) {
58        perror("gettimeofday");
59        exit(1);
60    }
61    now.tv_sec = t->end.tv_sec-now.tv_sec;
62    now.tv_usec = t->end.tv_usec-now.tv_usec;
63    if (now.tv_usec < 0) {
64        now.tv_sec--;
65        now.tv_usec += 1000000;
66    }
67    return now.tv_sec*1000+now.tv_usec/1000;
68}
tools/lib/timeout.h
1/*
2 * lib/timeout.h - ATRF driver API
3 *
4 * Written 2011 by Werner Almesberger
5 * Copyright 2011 Werner Almesberger
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 */
12
13
14#ifndef TIMEOUT_H
15#define TIMEOUT_H
16
17#include <sys/time.h>
18
19
20struct timeout {
21    struct timeval end;
22};
23
24
25void timeout_start(struct timeout *t, int ms);
26int timeout_reached(const struct timeout *t);
27int timeout_left_ms(const struct timeout *t);
28
29#endif /* !TIMEOUT_H */

Archive Download the corresponding diff file



interactive