From dab8825b8aaa86d6b6cd078c87a5dee7f0780a89 Mon Sep 17 00:00:00 2001 From: "Christian W. Zuckschwerdt" Date: Thu, 4 Feb 2021 13:45:58 +0100 Subject: [PATCH] minor: fix Secplus v1 time diff --- include/compat_time.h | 9 +++++++++ src/compat_time.c | 38 +++++++++++++++++++++++++------------- src/devices/secplus_v1.c | 4 ++-- 3 files changed, 36 insertions(+), 15 deletions(-) diff --git a/include/compat_time.h b/include/compat_time.h index 461e15af5..2f34b60dd 100644 --- a/include/compat_time.h +++ b/include/compat_time.h @@ -16,6 +16,15 @@ #include #endif +/** Subtract `struct timeval` values. + + @param[out] result time difference result + @param x first time value + @param y second time value + @return 1 if the difference is negative, otherwise 0. +*/ +int timeval_subtract(struct timeval *result, struct timeval *x, struct timeval *y); + // platform-specific functions #ifdef _WIN32 diff --git a/src/compat_time.c b/src/compat_time.c index c8f45eea5..2bdf3ae1a 100644 --- a/src/compat_time.c +++ b/src/compat_time.c @@ -3,23 +3,13 @@ // issue: is not available on Windows systems // solution: provide a compatible version for Windows systems -#ifndef _WIN32 -// Linux variant - -// just so the compilation unit isn't empty -int _compat_time(void) -{ - return 0; -} +#include "compat_time.h" -#else -// Windows variant +#ifdef _WIN32 #include #include -#include "compat_time.h" - #if defined(_MSC_VER) || defined(_MSC_EXTENSIONS) #define DELTA_EPOCH_IN_MICROSECS 11644473600000000Ui64 #else @@ -43,4 +33,26 @@ int gettimeofday(struct timeval *tv, void *tz) return 0; } -#endif // _WIN32 / !_WIN32 +#endif // _WIN32 + +int timeval_subtract(struct timeval *result, struct timeval *x, struct timeval *y) +{ + // Perform the carry for the later subtraction by updating y + if (x->tv_usec < y->tv_usec) { + int nsec = (y->tv_usec - x->tv_usec) / 1000000 + 1; + y->tv_usec -= 1000000 * nsec; + y->tv_sec += nsec; + } + if (x->tv_usec - y->tv_usec > 1000000) { + int nsec = (x->tv_usec - y->tv_usec) / 1000000; + y->tv_usec += 1000000 * nsec; + y->tv_sec -= nsec; + } + + // Compute the time difference, tv_usec is certainly positive + result->tv_sec = x->tv_sec - y->tv_sec; + result->tv_usec = x->tv_usec - y->tv_usec; + + // Return 1 if result is negative + return x->tv_sec < y->tv_sec; +} diff --git a/src/devices/secplus_v1.c b/src/devices/secplus_v1.c index 85ff37813..271c32542 100644 --- a/src/devices/secplus_v1.c +++ b/src/devices/secplus_v1.c @@ -18,7 +18,7 @@ Security+ 1.0 is described in [US patent application US6980655B2](https://paten */ #include "decoder.h" -#include +#include "compat_time.h" /** @fn int _decode_v1_half(uint8_t *bits, uint8_t *result) @@ -212,7 +212,7 @@ static int secplus_v1_callback(r_device *decoder, bitbuffer_t *bitbuffer) struct timeval cur_tv; struct timeval res_tv; gettimeofday(&cur_tv, NULL); - timersub(&cur_tv, &cached_tv, &res_tv); + timeval_subtract(&res_tv, &cur_tv, &cached_tv); if (decoder->verbose > 1) fprintf(stderr, "%s res %12ld %8ld\n", __func__, res_tv.tv_sec, (long)res_tv.tv_usec);