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