From 3f3c8dc63f2febd78f047acc40589a755ae7456d Mon Sep 17 00:00:00 2001 From: Olaf Bergmann Date: Fri, 8 Nov 2013 10:09:03 +0100 Subject: [PATCH] New data_type coap_time_diff_t and functions coap_time_lt(), coap_time_le() for time stamps with wrapping --- coap_time.h | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/coap_time.h b/coap_time.h index 13495642..b1e68ed2 100644 --- a/coap_time.h +++ b/coap_time.h @@ -28,6 +28,13 @@ typedef clock_time_t coap_tick_t; +/** + * This data type is used to represent the difference between two + * clock_tick_t values. This data type must have the same size in + * memory as coap_tick_t to allow wrapping. + */ +typedef int coap_tick_diff_t; + #define COAP_TICKS_PER_SECOND CLOCK_SECOND /** Set at startup to initialize the internal clock (time in seconds). */ @@ -51,6 +58,13 @@ contiki_ticks_impl(coap_tick_t *t) { #else /* WITH_CONTIKI */ typedef unsigned int coap_tick_t; +/** + * This data type is used to represent the difference between two + * clock_tick_t values. This data type must have the same size in + * memory as coap_tick_t to allow wrapping. + */ +typedef int coap_tick_diff_t; + #define COAP_TICKS_PER_SECOND 1024 /** Set at startup to initialize the internal clock (time in seconds). */ @@ -89,6 +103,16 @@ coap_ticks_impl(coap_tick_t *t) { #define coap_ticks coap_ticks_impl #endif /* coap_ticks */ +static inline +int coap_time_lt(coap_tick_t a, coap_tick_t b) { + return ((coap_tick_diff_t)(a - b)) < 0; +} + +static inline +int coap_time_le(coap_tick_t a, coap_tick_t b) { + return a == b || coap_time_lt(a,b); +} + /** @} */ #endif /* _COAP_TIME_H_ */