libcoap 4.3.0
coap_time.c
Go to the documentation of this file.
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
26static 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
36static int
37gettimeofday(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
56void
57coap_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
78void
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
113 return coap_clock_offset + (t / COAP_TICKS_PER_SECOND);
114}
115
117 return (uint64_t)coap_clock_offset * 1000000 + (uint64_t)t * 1000000 / COAP_TICKS_PER_SECOND;
118}
119
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 */
132{
133}
134
135#endif /* not HAVE_TIME_H */
136
Pulls together all the internal only header files.
COAP_STATIC_INLINE void dummy(void)
Definition: coap_time.c:131
coap_tick_t coap_ticks_from_rt_us(uint64_t t)
Helper function that converts POSIX wallclock time in us to coap ticks.
void coap_ticks(coap_tick_t *t)
Sets t to the internal time with COAP_TICKS_PER_SECOND resolution.
time_t coap_time_t
CoAP time in seconds since epoch.
Definition: coap_time.h:127
void coap_clock_init(void)
Initializes the internal clock.
uint64_t coap_tick_t
This data type represents internal timer ticks with COAP_TICKS_PER_SECOND resolution.
Definition: coap_time.h:122
coap_time_t coap_ticks_to_rt(coap_tick_t t)
Helper function that converts coap ticks to wallclock time.
#define COAP_TICKS_PER_SECOND
Use ms resolution on POSIX systems.
Definition: coap_time.h:137
uint64_t coap_ticks_to_rt_us(coap_tick_t t)
Helper function that converts coap ticks to POSIX wallclock time in us.
#define COAP_STATIC_INLINE
Definition: libcoap.h:40
#define SHR_FP(val, frac)
#define Q(frac, fval)
creates a Qx.frac from fval in coap_fixed_point_t
Definition: net.c:87