libcoap  4.3.0
coap_time.h
Go to the documentation of this file.
1 /*
2  * coap_time.h -- Clock Handling
3  *
4  * Copyright (C) 2010-2019 Olaf Bergmann <bergmann@tzi.org>
5  *
6  * SPDX-License-Identifier: BSD-2-Clause
7  *
8  * This file is part of the CoAP library libcoap. Please see README for terms
9  * of use.
10  */
11 
17 #ifndef COAP_TIME_H_
18 #define COAP_TIME_H_
19 
26 #if defined(WITH_LWIP)
27 
28 #include <stdint.h>
29 #include <lwip/sys.h>
30 
31 /* lwIP provides ms in sys_now */
32 #define COAP_TICKS_PER_SECOND 1000
33 
34 typedef uint32_t coap_tick_t;
35 typedef uint32_t coap_time_t;
36 typedef int32_t coap_tick_diff_t;
37 
38 COAP_STATIC_INLINE void coap_ticks_impl(coap_tick_t *t) {
39  *t = sys_now();
40 }
41 
42 COAP_STATIC_INLINE void coap_clock_init_impl(void) {
43 }
44 
45 #define coap_clock_init coap_clock_init_impl
46 #define coap_ticks coap_ticks_impl
47 
49  return t / COAP_TICKS_PER_SECOND;
50 }
51 
53  return (uint64_t)t * 1000000 / COAP_TICKS_PER_SECOND;
54 }
55 
56 #elif defined(WITH_CONTIKI)
57 
58 #include "clock.h"
59 
60 typedef clock_time_t coap_tick_t;
61 typedef clock_time_t coap_time_t;
62 
68 typedef int coap_tick_diff_t;
69 
70 #define COAP_TICKS_PER_SECOND CLOCK_SECOND
71 
73  clock_init();
74 }
75 
77  *t = clock_time();
78 }
79 
81  return t / COAP_TICKS_PER_SECOND;
82 }
83 
85  return (uint64_t)t * 1000000 / COAP_TICKS_PER_SECOND;
86 }
87 
88 #elif defined(RIOT_VERSION)
89 #include <xtimer.h>
90 
91 #define COAP_TICKS_PER_SECOND (XTIMER_HZ)
92 
93 typedef uint64_t coap_tick_t;
94 typedef int64_t coap_tick_diff_t;
95 typedef uint32_t coap_time_t;
96 
97 static inline void coap_clock_init(void) {}
98 
99 static inline void coap_ticks(coap_tick_t *t) {
100  *t = xtimer_now_usec64();
101 }
102 
103 static inline coap_time_t coap_ticks_to_rt(coap_tick_t t) {
104  return t / 1000000UL;
105 }
106 
107 static inline uint64_t coap_ticks_to_rt_us(coap_tick_t t) {
108  return t;
109 }
110 
111 static inline coap_tick_t coap_ticks_from_rt_us(uint64_t t) {
112  return t / 1000000UL;
113 }
114 #else /* !WITH_LWIP && !WITH_CONTIKI && !RIOT_VERSION */
115 
116 #include <stdint.h>
117 
122 typedef uint64_t coap_tick_t;
123 
127 typedef time_t coap_time_t;
128 
134 typedef int64_t coap_tick_diff_t;
135 
137 #define COAP_TICKS_PER_SECOND ((coap_tick_t)(1000U))
138 
142 void coap_clock_init(void);
143 
148 
160 
170 
179 #endif
180 
186  return ((coap_tick_diff_t)(a - b)) < 0;
187 }
188 
194  return a == b || coap_time_lt(a,b);
195 }
196 
199 #endif /* COAP_TIME_H_ */
coap_tick_t coap_ticks_from_rt_us(uint64_t t)
Helper function that converts POSIX wallclock time in us to coap ticks.
int64_t coap_tick_diff_t
This data type is used to represent the difference between two clock_tick_t values.
Definition: coap_time.h:134
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_STATIC_INLINE int coap_time_lt(coap_tick_t a, coap_tick_t b)
Returns 1 if and only if a is less than b where less is defined on a signed data type.
Definition: coap_time.h:185
coap_time_t coap_ticks_to_rt(coap_tick_t t)
Helper function that converts coap ticks to wallclock time.
COAP_STATIC_INLINE int coap_time_le(coap_tick_t a, coap_tick_t b)
Returns 1 if and only if a is less than or equal b where less is defined on a signed data type.
Definition: coap_time.h:193
#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