minor: fix Secplus v1 time diff

This commit is contained in:
Christian W. Zuckschwerdt 2021-02-04 13:45:58 +01:00
parent f3917ef095
commit dab8825b8a
3 changed files with 36 additions and 15 deletions

View file

@ -16,6 +16,15 @@
#include <sys/time.h>
#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

View file

@ -3,23 +3,13 @@
// issue: <sys/time.h> is not available on Windows systems
// solution: provide a compatible version for Windows systems
#ifndef _WIN32
// Linux variant
#include "compat_time.h"
// just so the compilation unit isn't empty
int _compat_time(void)
{
return 0;
}
#else
// Windows variant
#ifdef _WIN32
#include <stdbool.h>
#include <stddef.h>
#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;
}

View file

@ -18,7 +18,7 @@ Security+ 1.0 is described in [US patent application US6980655B2](https://paten
*/
#include "decoder.h"
#include <sys/time.h>
#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);