IEEE 802.15.4 subsystem
Sign in or create your account | Project List | Help
IEEE 802.15.4 subsystem Git Source Tree
Root/
| 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 | |
| 21 | void 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 | |
| 36 | int 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 | |
| 52 | int 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 | } |
| 69 |
