Skip to content

Commit

Permalink
minor: fix Secplus v1 time diff
Browse files Browse the repository at this point in the history
  • Loading branch information
zuckschwerdt committed Feb 5, 2021
1 parent f3917ef commit dab8825
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 15 deletions.
9 changes: 9 additions & 0 deletions include/compat_time.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
38 changes: 25 additions & 13 deletions src/compat_time.c
Original file line number Diff line number Diff line change
Expand Up @@ -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

// just so the compilation unit isn't empty
int _compat_time(void)
{
return 0;
}
#include "compat_time.h"

#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
Expand All @@ -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;
}
4 changes: 2 additions & 2 deletions src/devices/secplus_v1.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit dab8825

Please sign in to comment.