1 /* coap_time.c -- Clock Handling
2  *
3  * Copyright (C) 2015 Olaf Bergmann <bergmann@tzi.org>
4  *
5  * This file is part of the CoAP library libcoap. Please see
6  * README for terms of use.
7  */
8 
9 #ifdef WITH_POSIX
10 #include <time.h>
11 #include <sys/time.h>
12 #include <unistd.h>  /* _POSIX_TIMERS */
13 
14 #include "coap_config.h"
15 #include "coap_time.h"
16 
17 static coap_time_t coap_clock_offset = 0;
18 
19 #if _POSIX_TIMERS && !defined(__APPLE__)
20   /* _POSIX_TIMERS is > 0 when clock_gettime() is available */
21 
22   /* Use real-time clock for correct timestamps in coap_log(). */
23 #define COAP_CLOCK CLOCK_REALTIME
24 #endif
25 
26 void
coap_clock_init(void)27 coap_clock_init(void) {
28 #ifdef COAP_CLOCK
29   struct timespec tv;
30   clock_gettime(COAP_CLOCK, &tv);
31 #else /* _POSIX_TIMERS */
32   struct timeval tv;
33   gettimeofday(&tv, NULL);
34 #endif /* not _POSIX_TIMERS */
35 
36   coap_clock_offset = tv.tv_sec;
37 }
38 
39 /* creates a Qx.frac from fval */
40 #define Q(frac,fval) ((coap_tick_t)(((1 << (frac)) * (fval))))
41 
42 /* number of frac bits for sub-seconds */
43 #define FRAC 10
44 
45 /* rounds val up and right shifts by frac positions */
46 #define SHR_FP(val,frac) (((val) + (1 << ((frac) - 1))) >> (frac))
47 
48 void
coap_ticks(coap_tick_t * t)49 coap_ticks(coap_tick_t *t) {
50   unsigned long tmp;
51 
52 #ifdef COAP_CLOCK
53   struct timespec tv;
54   clock_gettime(COAP_CLOCK, &tv);
55   /* Possible errors are (see clock_gettime(2)):
56    *  EFAULT tp points outside the accessible address space.
57    *  EINVAL The clk_id specified is not supported on this system.
58    * Both cases should not be possible here.
59    */
60 
61   tmp = SHR_FP(tv.tv_nsec * Q(FRAC, (COAP_TICKS_PER_SECOND/1000000000.0)), FRAC);
62 #else /* _POSIX_TIMERS */
63   /* Fall back to gettimeofday() */
64 
65   struct timeval tv;
66   gettimeofday(&tv, NULL);
67   /* Possible errors are (see gettimeofday(2)):
68    *  EFAULT One of tv or tz pointed outside the accessible address space.
69    *  EINVAL Timezone (or something else) is invalid.
70    * Both cases should not be possible here.
71    */
72 
73   tmp = SHR_FP(tv.tv_usec * Q(FRAC, (COAP_TICKS_PER_SECOND/1000000.0)), FRAC);
74 #endif /* not _POSIX_TIMERS */
75 
76   /* Finally, convert temporary FP representation to multiple of
77    * COAP_TICKS_PER_SECOND */
78   *t = tmp + (tv.tv_sec - coap_clock_offset) * COAP_TICKS_PER_SECOND;
79 }
80 
81 coap_time_t
coap_ticks_to_rt(coap_tick_t t)82 coap_ticks_to_rt(coap_tick_t t) {
83   return coap_clock_offset + (t / COAP_TICKS_PER_SECOND);
84 }
85 
86 #undef Q
87 #undef FRAC
88 #undef SHR_FP
89 
90 #else /* WITH_POSIX */
91 
92 /* make compilers happy that do not like empty modules */
dummy()93 static inline void dummy()
94 {
95 }
96 
97 #endif /* not WITH_POSIX */
98 
99