From 0aa8cde1ad66773b74abc56fa267c3449175e2c0 Mon Sep 17 00:00:00 2001 From: Jon Shallow Date: Wed, 3 Jul 2019 16:54:57 +0100 Subject: [PATCH] coap_time.c: Make coap_ticks() more accurate than to the nearest second With define Q(frac,fval) ((coap_tick_t)(((1 << (frac)) * (fval)))) and tmp = SHR_FP(tv.tv_nsec * Q(FRAC, (COAP_TICKS_PER_SECOND/1000000000.0)), FRAC); The Q function always returns 0 in error - e.g. (gdb) p ((coap_tick_t)(((1 << (FRAC))*((COAP_TICKS_PER_SECOND/1000000000.0))))) $6 = 0 (gdb) p ((((1 << (FRAC))*((COAP_TICKS_PER_SECOND/1000000000.0))))) $7 = 0.001024 whereas we need the value 0.001024 to multiple with tv_nsec. This change fixes this by moving the coap_tick_t from Q to SHR_FP. Found when debugging #374 --- src/coap_time.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/coap_time.c b/src/coap_time.c index 0931b00f..0193319a 100644 --- a/src/coap_time.c +++ b/src/coap_time.c @@ -68,13 +68,13 @@ coap_clock_init(void) { } /* creates a Qx.frac from fval */ -#define Q(frac,fval) ((coap_tick_t)(((1 << (frac)) * (fval)))) +#define Q(frac,fval) ((1 << (frac)) * (fval)) /* number of frac bits for sub-seconds */ #define FRAC 10 /* rounds val up and right shifts by frac positions */ -#define SHR_FP(val,frac) (((val) + (1 << ((frac) - 1))) >> (frac)) +#define SHR_FP(val,frac) (((coap_tick_t)((val) + (1 << ((frac) - 1)))) >> (frac)) void coap_ticks(coap_tick_t *t) {