diff --git a/ChangeLog b/ChangeLog index 4e4fe6b9..c606e247 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,8 @@ +2015-03-05 Olaf Bergmann + + * src/coap_time.c (coap_ticks): Changed POSIX implementation + to fixed point arithmetic and removed clock_offset. + 2015-02-21 Olaf Bergmann * net.c (coap_send_confirmed): Use fixed point arithmetic diff --git a/Makefile.am b/Makefile.am index 6944660b..219f2c64 100644 --- a/Makefile.am +++ b/Makefile.am @@ -36,6 +36,7 @@ libcoap_@LIBCOAP_API_VERSION@_la_SOURCES = \ src/async.c \ src/block.c \ src/coap_io.c \ + src/coap_time.c \ src/debug.c \ src/encode.c \ src/hashkey.c \ diff --git a/examples/server.c b/examples/server.c index 50af753c..fb4bc7ea 100644 --- a/examples/server.c +++ b/examples/server.c @@ -37,6 +37,7 @@ static int quit = 0; /* changeable clock base (see handle_put_time()) */ +static time_t clock_offset; static time_t my_clock_base = 0; struct coap_resource_t *time_resource = NULL; @@ -360,6 +361,8 @@ main(int argc, char **argv) { int opt; coap_log_t log_level = LOG_WARNING; + clock_offset = time(NULL); + while ((opt = getopt(argc, argv, "A:p:v:")) != -1) { switch (opt) { case 'A' : diff --git a/include/coap/coap_time.h b/include/coap/coap_time.h index 0bd9546d..cf56f155 100644 --- a/include/coap/coap_time.h +++ b/include/coap/coap_time.h @@ -26,8 +26,7 @@ extern "C" { /** * @defgroup clock Clock Handling - * Default implementation of internal clock. You should redefine this if - * you do not have time() and gettimeofday(). + * Default implementation of internal clock. * @{ */ @@ -40,6 +39,7 @@ extern "C" { #define COAP_TICKS_PER_SECOND 1000 typedef uint32_t coap_tick_t; +typedef uint32_t coap_time_t; typedef int32_t coap_tick_diff_t; static inline void coap_ticks_impl(coap_tick_t *t) @@ -55,11 +55,15 @@ static inline void coap_clock_init_impl(void) #define coap_ticks coap_ticks_impl +static inline coap_time_t coap_ticks_to_rt(coap_tick_t t) { + return t / COAP_TICKS_PER_SECOND; +} #endif #ifdef WITH_CONTIKI #include "clock.h" typedef clock_time_t coap_tick_t; +typedef clock_time_t coap_time_t; /** * This data type is used to represent the difference between two @@ -70,73 +74,63 @@ typedef int coap_tick_diff_t; #define COAP_TICKS_PER_SECOND CLOCK_SECOND -/** Set at startup to initialize the internal clock (time in seconds). */ -extern clock_time_t clock_offset; - static inline void -contiki_clock_init_impl(void) { +coap_clock_init(void) { clock_init(); - clock_offset = clock_time(); } -#define coap_clock_init contiki_clock_init_impl - static inline void -contiki_ticks_impl(coap_tick_t *t) { +coap_ticks(coap_tick_t *t) { *t = clock_time(); } -#define coap_ticks contiki_ticks_impl - +static inline coap_time_t coap_ticks_to_rt(coap_tick_t t) { + return t / COAP_TICKS_PER_SECOND; +} #endif /* WITH_CONTIKI */ #ifdef WITH_POSIX -typedef unsigned int coap_tick_t; +/** + * This data type represents internal timer ticks with + * COAP_TICKS_PER_SECOND resolution. + */ +typedef unsigned long coap_tick_t; + +/** CoAP time in seconds since epoch. */ +typedef time_t coap_time_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; +typedef long coap_tick_diff_t; -#define COAP_TICKS_PER_SECOND 1024 +/** Use ms resolution on POSIX systems */ +#define COAP_TICKS_PER_SECOND 1000 -/** Set at startup to initialize the internal clock (time in seconds). */ -extern time_t clock_offset; +/** Initializes the internal clock. */ +static inline void coap_clock_init(void) +{ +} + +/** Sets @p t to the internal time with COAP_TICKS_PER_SECOND resolution. */ +void coap_ticks(coap_tick_t *t); + +/** + * Helper function that converts coap ticks to wallclock time. On POSIX, + * this function returns the number of seconds since the epoch. On other + * systems, it may be the calculated number of seconds since last reboot + * or so. + * + * @param t Internal system ticks + * @return The number of seconds that has passed since a specific reference + * point (seconds since epoch on POSIX). + */ +static inline coap_time_t coap_ticks_to_rt(coap_tick_t t) { + return t / COAP_TICKS_PER_SECOND; +} #endif /* WITH_POSIX */ -#ifndef coap_clock_init -static inline void -coap_clock_init_impl(void) { -#ifdef HAVE_TIME_H - clock_offset = time(NULL); -#else -# ifdef __GNUC__ - /* Issue a warning when using gcc. Other prepropressors do - * not seem to have a similar feature. */ -# warning "cannot initialize clock" -# endif - clock_offset = 0; -#endif -} -#define coap_clock_init coap_clock_init_impl -#endif /* coap_clock_init */ - -#ifndef coap_ticks -static inline void -coap_ticks_impl(coap_tick_t *t) { -#ifdef HAVE_SYS_TIME_H - struct timeval tv; - gettimeofday(&tv, NULL); - *t = (tv.tv_sec - clock_offset) * COAP_TICKS_PER_SECOND - + (tv.tv_usec * COAP_TICKS_PER_SECOND / 1000000); -#else -#error "clock not implemented" -#endif -} -#define coap_ticks coap_ticks_impl -#endif /* coap_ticks */ - /** * Returns @c 1 if and only if @p a is less than @p b where less is * defined on a signed data type. diff --git a/src/coap_time.c b/src/coap_time.c new file mode 100644 index 00000000..8c24e507 --- /dev/null +++ b/src/coap_time.c @@ -0,0 +1,78 @@ +/* coap_time.c -- Clock Handling + * + * Copyright (C) 2015 Olaf Bergmann + * + * This file is part of the CoAP library libcoap. Please see + * README for terms of use. + */ + +#ifdef WITH_POSIX +#include +#include +#include /* _POSIX_TIMERS */ + +#include "coap_config.h" +#include "coap_time.h" + +#if _POSIX_TIMERS + /* _POSIX_TIMERS is > 0 when clock_gettime() is available */ + + /* Use real-time clock for correct timestamps in coap_log(). */ +#define COAP_CLOCK CLOCK_REALTIME +#endif + +/* creates a Qx.frac from fval */ +#define Q(frac,fval) ((coap_tick_t)(((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)) + +void +coap_ticks(coap_tick_t *t) { + unsigned long tmp; + +#ifdef COAP_CLOCK + struct timespec tv; + clock_gettime(COAP_CLOCK, &tv); + /* Possible errors are (see clock_gettime(2)): + * EFAULT tp points outside the accessible address space. + * EINVAL The clk_id specified is not supported on this system. + * Both cases should not be possible here. + */ + + tmp = SHR_FP(tv.tv_nsec * Q(FRAC, (COAP_TICKS_PER_SECOND/1000000000.0)), FRAC); +#else /* _POSIX_TIMERS */ + /* Fall back to gettimeofday() */ + + struct timeval tv; + gettimeofday(&tv, NULL); + /* Possible errors are (see gettimeofday(2)): + * EFAULT One of tv or tz pointed outside the accessible address space. + * EINVAL Timezone (or something else) is invalid. + * Both cases should not be possible here. + */ + + tmp = tv.tv_usec * Q(FRAC, (COAP_TICKS_PER_SECOND/1000000.0)); +#endif /* not _POSIX_TIMERS */ + + /* Finally, convert temporary FP representation to multiple of + * COAP_TICKS_PER_SECOND */ + *t = tmp + tv.tv_sec * COAP_TICKS_PER_SECOND; +} + +#undef Q +#undef FRAC +#undef SHR_FP + +#else /* WITH_POSIX */ + +/* make compilers happy that do not like empty modules */ +static inline void dummy() +{ +} + +#endif /* not WITH_POSIX */ + diff --git a/src/debug.c b/src/debug.c index 89d346ff..811437d6 100644 --- a/src/debug.c +++ b/src/debug.c @@ -69,7 +69,7 @@ static char *loglevels[] = { static inline size_t print_timestamp(char *s, size_t len, coap_tick_t t) { struct tm *tmp; - time_t now = clock_offset + (t / COAP_TICKS_PER_SECOND); + time_t now = coap_ticks_to_rt(t); tmp = localtime(&now); return strftime(s, len, "%b %d %H:%M:%S", tmp); } @@ -80,7 +80,7 @@ static inline size_t print_timestamp(char *s, size_t len, coap_tick_t t) { #ifdef HAVE_SNPRINTF return snprintf(s, len, "%u.%03u", - (unsigned int)(clock_offset + (t / COAP_TICKS_PER_SECOND)), + (unsigned int)coap_ticks_to_rt(t), (unsigned int)(t % COAP_TICKS_PER_SECOND)); #else /* HAVE_SNPRINTF */ /* @todo do manual conversion of timestamp */