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