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 #include "coap_internal.h"
10
11 #ifdef HAVE_TIME_H
12 #include <time.h>
13 #ifdef HAVE_SYS_TIME_H
14 #include <sys/time.h>
15 #endif
16 #ifdef HAVE_UNISTD_H
17 #include <unistd.h> /* _POSIX_TIMERS */
18 #endif
19 #ifdef HAVE_WINSOCK2_H
20 #include <winsock2.h>
21 #include <stdint.h>
22 #endif
23
24 static coap_tick_t coap_clock_offset = 0;
25
26 #if _POSIX_TIMERS && !defined(__APPLE__)
27 /* _POSIX_TIMERS is > 0 when clock_gettime() is available */
28
29 /* Use real-time clock for correct timestamps in coap_log(). */
30 #define COAP_CLOCK CLOCK_REALTIME
31 #endif
32
33 #ifdef HAVE_WINSOCK2_H
34 static int
gettimeofday(struct timeval * tp,TIME_ZONE_INFORMATION * tzp)35 gettimeofday(struct timeval *tp, TIME_ZONE_INFORMATION *tzp) {
36 (void)tzp;
37 static const uint64_t s_tUnixEpoch = 116444736000000000Ui64;
38
39 FILETIME file_time;
40 ULARGE_INTEGER time;
41 uint64_t tUsSinceUnicEpoch;
42
43 GetSystemTimeAsFileTime( &file_time );
44 time.LowPart = file_time.dwLowDateTime;
45 time.HighPart = file_time.dwHighDateTime;
46 tUsSinceUnicEpoch = ( time.QuadPart - s_tUnixEpoch ) / 10;
47
48 tp->tv_sec = (long)(tUsSinceUnicEpoch / 1000000);
49 tp->tv_usec = (long)(tUsSinceUnicEpoch % 1000000);
50 return 0;
51 }
52 #endif
53
54 void
coap_clock_init(void)55 coap_clock_init(void) {
56 #ifdef COAP_CLOCK
57 struct timespec tv;
58 clock_gettime(COAP_CLOCK, &tv);
59 #else /* _POSIX_TIMERS */
60 struct timeval tv;
61 gettimeofday(&tv, NULL);
62 #endif /* not _POSIX_TIMERS */
63
64 coap_clock_offset = tv.tv_sec;
65 }
66
67 /* creates a Qx.frac from fval */
68 #define Q(frac,fval) ((1 << (frac)) * (fval))
69
70 /* number of frac bits for sub-seconds */
71 #define FRAC 10
72
73 /* rounds val up and right shifts by frac positions */
74 #define SHR_FP(val,frac) (((coap_tick_t)((val) + (1 << ((frac) - 1)))) >> (frac))
75
76 void
coap_ticks(coap_tick_t * t)77 coap_ticks(coap_tick_t *t) {
78 coap_tick_t tmp;
79
80 #ifdef COAP_CLOCK
81 struct timespec tv;
82 clock_gettime(COAP_CLOCK, &tv);
83 /* Possible errors are (see clock_gettime(2)):
84 * EFAULT tp points outside the accessible address space.
85 * EINVAL The clk_id specified is not supported on this system.
86 * Both cases should not be possible here.
87 */
88
89 tmp = SHR_FP(tv.tv_nsec * Q(FRAC, (COAP_TICKS_PER_SECOND/1000000000.0)), FRAC);
90 #else /* _POSIX_TIMERS */
91 /* Fall back to gettimeofday() */
92
93 struct timeval tv;
94 gettimeofday(&tv, NULL);
95 /* Possible errors are (see gettimeofday(2)):
96 * EFAULT One of tv or tz pointed outside the accessible address space.
97 * EINVAL Timezone (or something else) is invalid.
98 * Both cases should not be possible here.
99 */
100
101 tmp = SHR_FP(tv.tv_usec * Q(FRAC, (COAP_TICKS_PER_SECOND/1000000.0)), FRAC);
102 #endif /* not _POSIX_TIMERS */
103
104 /* Finally, convert temporary FP representation to multiple of
105 * COAP_TICKS_PER_SECOND */
106 *t = tmp + (tv.tv_sec - coap_clock_offset) * COAP_TICKS_PER_SECOND;
107 }
108
109 coap_time_t
coap_ticks_to_rt(coap_tick_t t)110 coap_ticks_to_rt(coap_tick_t t) {
111 return coap_clock_offset + (t / COAP_TICKS_PER_SECOND);
112 }
113
coap_ticks_to_rt_us(coap_tick_t t)114 uint64_t coap_ticks_to_rt_us(coap_tick_t t) {
115 return (uint64_t)coap_clock_offset * 1000000 + (uint64_t)t * 1000000 / COAP_TICKS_PER_SECOND;
116 }
117
coap_ticks_from_rt_us(uint64_t t)118 coap_tick_t coap_ticks_from_rt_us(uint64_t t) {
119 return (coap_tick_t)((t - (uint64_t)coap_clock_offset * 1000000) * COAP_TICKS_PER_SECOND / 1000000);
120 }
121
122 #undef Q
123 #undef FRAC
124 #undef SHR_FP
125
126 #else /* HAVE_TIME_H */
127
128 /* make compilers happy that do not like empty modules */
dummy(void)129 COAP_STATIC_INLINE void dummy(void)
130 {
131 }
132
133 #endif /* not HAVE_TIME_H */
134
135