libcoap  4.1.2
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 #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 
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
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
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 
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 */
93 static inline void dummy()
94 {
95 }
96 
97 #endif /* not WITH_POSIX */
98 
#define Q(frac, fval)
Definition: coap_time.c:40
#define FRAC
Definition: coap_time.c:43
Clock Handling.
void coap_clock_init(void)
Initializes the internal clock.
Definition: coap_time.c:27
coap_time_t coap_ticks_to_rt(coap_tick_t t)
Helper function that converts coap ticks to wallclock time.
Definition: coap_time.c:82
unsigned long coap_tick_t
This data type represents internal timer ticks with COAP_TICKS_PER_SECOND resolution.
Definition: coap_time.h:84
#define COAP_TICKS_PER_SECOND
Use ms resolution on POSIX systems.
Definition: coap_time.h:99
time_t coap_time_t
CoAP time in seconds since epoch.
Definition: coap_time.h:89
void coap_ticks(coap_tick_t *t)
Sets t to the internal time with COAP_TICKS_PER_SECOND resolution.
Definition: coap_time.c:49
static coap_time_t coap_clock_offset
Definition: coap_time.c:17
#define SHR_FP(val, frac)
Definition: coap_time.c:46