/src/libcoap/include/coap3/coap_time.h
Line | Count | Source |
1 | | /* |
2 | | * coap_time.h -- Clock Handling |
3 | | * |
4 | | * Copyright (C) 2010-2026 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 | | |
12 | | /** |
13 | | * @file coap_time.h |
14 | | * @brief Clock Handling |
15 | | */ |
16 | | |
17 | | #ifndef COAP_TIME_H_ |
18 | | #define COAP_TIME_H_ |
19 | | |
20 | | /** |
21 | | * @ingroup application_api |
22 | | * @defgroup clock Clock Handling |
23 | | * API for internal clock assess |
24 | | * @{ |
25 | | */ |
26 | | |
27 | | #if defined(WITH_LWIP) |
28 | | #include <stdint.h> |
29 | | #include <lwip/sys.h> |
30 | | #elif defined(WITH_CONTIKI) |
31 | | #include "clock.h" |
32 | | #elif defined(RIOT_VERSION) |
33 | | #include <xtimer.h> |
34 | | #else /* !WITH_LWIP && !WITH_CONTIKI && !RIOT_VERSION */ |
35 | | #include <stdint.h> |
36 | | #endif /* !WITH_LWIP && !WITH_CONTIKI && !RIOT_VERSION */ |
37 | | |
38 | | #ifdef __cplusplus |
39 | | extern "C" { |
40 | | #endif |
41 | | |
42 | | #if defined(WITH_LWIP) |
43 | | |
44 | | /* lwIP provides ms in sys_now */ |
45 | | #define COAP_TICKS_PER_SECOND 1000 |
46 | | |
47 | | typedef uint32_t coap_tick_t; |
48 | | typedef uint32_t coap_time_t; |
49 | | typedef int32_t coap_tick_diff_t; |
50 | | |
51 | | COAP_STATIC_INLINE void |
52 | | coap_ticks_impl(coap_tick_t *t) { |
53 | | *t = sys_now(); |
54 | | } |
55 | | |
56 | | COAP_STATIC_INLINE void |
57 | | coap_clock_init_impl(void) { |
58 | | } |
59 | | |
60 | | #define coap_clock_init coap_clock_init_impl |
61 | | #define coap_ticks coap_ticks_impl |
62 | | |
63 | | COAP_STATIC_INLINE coap_time_t |
64 | | coap_ticks_to_rt(coap_tick_t t) { |
65 | | return t / COAP_TICKS_PER_SECOND; |
66 | | } |
67 | | |
68 | | COAP_STATIC_INLINE uint64_t |
69 | | coap_ticks_to_rt_us(coap_tick_t t) { |
70 | | return (uint64_t)t * 1000000 / COAP_TICKS_PER_SECOND; |
71 | | } |
72 | | |
73 | | #elif defined(WITH_CONTIKI) |
74 | | |
75 | | typedef clock_time_t coap_tick_t; |
76 | | typedef clock_time_t coap_time_t; |
77 | | |
78 | | /** |
79 | | * This data type is used to represent the difference between two clock_tick_t |
80 | | * values. This data type must have the same size in memory as coap_tick_t to |
81 | | * allow wrapping. |
82 | | */ |
83 | | typedef int coap_tick_diff_t; |
84 | | |
85 | | #define COAP_TICKS_PER_SECOND CLOCK_SECOND |
86 | | |
87 | | COAP_STATIC_INLINE void |
88 | | coap_clock_init(void) { |
89 | | } |
90 | | |
91 | | COAP_STATIC_INLINE void |
92 | | coap_ticks(coap_tick_t *t) { |
93 | | *t = clock_time(); |
94 | | } |
95 | | |
96 | | COAP_STATIC_INLINE coap_time_t |
97 | | coap_ticks_to_rt(coap_tick_t t) { |
98 | | return t / COAP_TICKS_PER_SECOND; |
99 | | } |
100 | | |
101 | | COAP_STATIC_INLINE uint64_t |
102 | | coap_ticks_to_rt_us(coap_tick_t t) { |
103 | | return (uint64_t)t * 1000000 / COAP_TICKS_PER_SECOND; |
104 | | } |
105 | | |
106 | | #elif defined(RIOT_VERSION) |
107 | | |
108 | | #ifdef XTIMER_HZ |
109 | | #define COAP_TICKS_PER_SECOND (XTIMER_HZ) |
110 | | #else /* XTIMER_HZ */ |
111 | | #define COAP_TICKS_PER_SECOND (1000000U) |
112 | | #endif /* XTIMER_HZ */ |
113 | | |
114 | | typedef uint64_t coap_tick_t; |
115 | | typedef int64_t coap_tick_diff_t; |
116 | | typedef uint32_t coap_time_t; |
117 | | |
118 | | COAP_STATIC_INLINE void |
119 | | coap_clock_init(void) {} |
120 | | |
121 | | COAP_STATIC_INLINE void |
122 | | coap_ticks(coap_tick_t *t) { |
123 | | #ifdef MODULE_ZTIMER64_XTIMER_COMPAT |
124 | | *t = xtimer_now_usec64(); |
125 | | #else /* MODULE_ZTIMER64_XTIMER_COMPAT */ |
126 | | *t = xtimer_now_usec(); |
127 | | #endif /* MODULE_ZTIMER64_XTIMER_COMPAT */ |
128 | | } |
129 | | |
130 | | COAP_STATIC_INLINE coap_time_t |
131 | | coap_ticks_to_rt(coap_tick_t t) { |
132 | | return t / 1000000UL; |
133 | | } |
134 | | |
135 | | COAP_STATIC_INLINE uint64_t |
136 | | coap_ticks_to_rt_us(coap_tick_t t) { |
137 | | return t; |
138 | | } |
139 | | |
140 | | COAP_STATIC_INLINE coap_tick_t |
141 | | coap_ticks_from_rt_us(uint64_t t) { |
142 | | return t / 1000000UL; |
143 | | } |
144 | | |
145 | | #else /* !WITH_LWIP && !WITH_CONTIKI && !RIOT_VERSION */ |
146 | | |
147 | | /** |
148 | | * This data type represents internal timer ticks with COAP_TICKS_PER_SECOND |
149 | | * resolution. |
150 | | */ |
151 | | typedef uint64_t coap_tick_t; |
152 | | |
153 | | /** |
154 | | * CoAP time in seconds since epoch. |
155 | | */ |
156 | | typedef time_t coap_time_t; |
157 | | |
158 | | /** |
159 | | * This data type is used to represent the difference between two clock_tick_t |
160 | | * values. This data type must have the same size in memory as coap_tick_t to |
161 | | * allow wrapping. |
162 | | */ |
163 | | typedef int64_t coap_tick_diff_t; |
164 | | |
165 | | /** Use ms resolution on POSIX systems */ |
166 | 23.6k | #define COAP_TICKS_PER_SECOND ((coap_tick_t)(1000U)) |
167 | | |
168 | | /** |
169 | | * Initializes the internal clock. |
170 | | */ |
171 | | void coap_clock_init(void); |
172 | | |
173 | | /** |
174 | | * Sets @p t to the internal time with COAP_TICKS_PER_SECOND resolution. |
175 | | */ |
176 | | void coap_ticks(coap_tick_t *t); |
177 | | |
178 | | /** |
179 | | * Helper function that converts coap ticks to wallclock time. On POSIX, this |
180 | | * function returns the number of seconds since the epoch. On other systems, it |
181 | | * may be the calculated number of seconds since last reboot or so. |
182 | | * |
183 | | * @param t Internal system ticks. |
184 | | * |
185 | | * @return The number of seconds that has passed since a specific reference |
186 | | * point (seconds since epoch on POSIX). |
187 | | */ |
188 | | coap_time_t coap_ticks_to_rt(coap_tick_t t); |
189 | | |
190 | | /** |
191 | | * Helper function that converts coap ticks to POSIX wallclock time in us. |
192 | | * |
193 | | * @param t Internal system ticks. |
194 | | * |
195 | | * @return The number of seconds that has passed since a specific reference |
196 | | * point (seconds since epoch on POSIX). |
197 | | */ |
198 | | uint64_t coap_ticks_to_rt_us(coap_tick_t t); |
199 | | |
200 | | /** |
201 | | * Helper function that converts POSIX wallclock time in us to coap ticks. |
202 | | * |
203 | | * @param t POSIX time is us |
204 | | * |
205 | | * @return coap ticks |
206 | | */ |
207 | | coap_tick_t coap_ticks_from_rt_us(uint64_t t); |
208 | | |
209 | | #endif /* !WITH_LWIP && !WITH_CONTIKI && !RIOT_VERSION */ |
210 | | |
211 | | /** |
212 | | * Returns @c 1 if and only if @p a is less than @p b where less is defined on a |
213 | | * signed data type. |
214 | | */ |
215 | | COAP_STATIC_INLINE int |
216 | 0 | coap_time_lt(coap_tick_t a, coap_tick_t b) { |
217 | 0 | return ((coap_tick_diff_t)(a - b)) < 0; |
218 | 0 | } Unexecuted instantiation: coap_debug.c:coap_time_lt Unexecuted instantiation: coap_encode.c:coap_time_lt Unexecuted instantiation: coap_net.c:coap_time_lt Unexecuted instantiation: coap_netif.c:coap_time_lt Unexecuted instantiation: coap_notls.c:coap_time_lt Unexecuted instantiation: coap_option.c:coap_time_lt Unexecuted instantiation: coap_oscore.c:coap_time_lt Unexecuted instantiation: coap_pdu.c:coap_time_lt Unexecuted instantiation: coap_proxy.c:coap_time_lt Unexecuted instantiation: coap_prng.c:coap_time_lt Unexecuted instantiation: coap_resource.c:coap_time_lt Unexecuted instantiation: coap_session.c:coap_time_lt Unexecuted instantiation: coap_sha1.c:coap_time_lt Unexecuted instantiation: coap_str.c:coap_time_lt Unexecuted instantiation: coap_strm_posix.c:coap_time_lt Unexecuted instantiation: coap_subscribe.c:coap_time_lt Unexecuted instantiation: coap_threadsafe.c:coap_time_lt Unexecuted instantiation: coap_time.c:coap_time_lt Unexecuted instantiation: coap_uri.c:coap_time_lt Unexecuted instantiation: coap_ws.c:coap_time_lt Unexecuted instantiation: oscore.c:coap_time_lt Unexecuted instantiation: oscore_cbor.c:coap_time_lt Unexecuted instantiation: oscore_context.c:coap_time_lt Unexecuted instantiation: oscore_cose.c:coap_time_lt Unexecuted instantiation: oscore_crypto.c:coap_time_lt Unexecuted instantiation: coap_address.c:coap_time_lt Unexecuted instantiation: coap_async.c:coap_time_lt Unexecuted instantiation: coap_block.c:coap_time_lt Unexecuted instantiation: coap_cache.c:coap_time_lt Unexecuted instantiation: coap_dgrm_posix.c:coap_time_lt Unexecuted instantiation: coap_dtls.c:coap_time_lt Unexecuted instantiation: coap_hashkey.c:coap_time_lt Unexecuted instantiation: coap_io.c:coap_time_lt Unexecuted instantiation: coap_io_posix.c:coap_time_lt Unexecuted instantiation: coap_layers.c:coap_time_lt Unexecuted instantiation: coap_mem.c:coap_time_lt |
219 | | |
220 | | /** |
221 | | * Returns @c 1 if and only if @p a is less than or equal @p b where less is |
222 | | * defined on a signed data type. |
223 | | */ |
224 | | COAP_STATIC_INLINE int |
225 | 0 | coap_time_le(coap_tick_t a, coap_tick_t b) { |
226 | 0 | return a == b || coap_time_lt(a,b); |
227 | 0 | } Unexecuted instantiation: coap_debug.c:coap_time_le Unexecuted instantiation: coap_encode.c:coap_time_le Unexecuted instantiation: coap_net.c:coap_time_le Unexecuted instantiation: coap_netif.c:coap_time_le Unexecuted instantiation: coap_notls.c:coap_time_le Unexecuted instantiation: coap_option.c:coap_time_le Unexecuted instantiation: coap_oscore.c:coap_time_le Unexecuted instantiation: coap_pdu.c:coap_time_le Unexecuted instantiation: coap_proxy.c:coap_time_le Unexecuted instantiation: coap_prng.c:coap_time_le Unexecuted instantiation: coap_resource.c:coap_time_le Unexecuted instantiation: coap_session.c:coap_time_le Unexecuted instantiation: coap_sha1.c:coap_time_le Unexecuted instantiation: coap_str.c:coap_time_le Unexecuted instantiation: coap_strm_posix.c:coap_time_le Unexecuted instantiation: coap_subscribe.c:coap_time_le Unexecuted instantiation: coap_threadsafe.c:coap_time_le Unexecuted instantiation: coap_time.c:coap_time_le Unexecuted instantiation: coap_uri.c:coap_time_le Unexecuted instantiation: coap_ws.c:coap_time_le Unexecuted instantiation: oscore.c:coap_time_le Unexecuted instantiation: oscore_cbor.c:coap_time_le Unexecuted instantiation: oscore_context.c:coap_time_le Unexecuted instantiation: oscore_cose.c:coap_time_le Unexecuted instantiation: oscore_crypto.c:coap_time_le Unexecuted instantiation: coap_address.c:coap_time_le Unexecuted instantiation: coap_async.c:coap_time_le Unexecuted instantiation: coap_block.c:coap_time_le Unexecuted instantiation: coap_cache.c:coap_time_le Unexecuted instantiation: coap_dgrm_posix.c:coap_time_le Unexecuted instantiation: coap_dtls.c:coap_time_le Unexecuted instantiation: coap_hashkey.c:coap_time_le Unexecuted instantiation: coap_io.c:coap_time_le Unexecuted instantiation: coap_io_posix.c:coap_time_le Unexecuted instantiation: coap_layers.c:coap_time_le Unexecuted instantiation: coap_mem.c:coap_time_le |
228 | | |
229 | | /* Can delay up to 24 hrs before next wakeup (coap_tick_t can be 4 bytes or int64 */ |
230 | | #if defined(RIOT_VERSION) |
231 | | #define COAP_MAX_DELAY_TICKS (24UL * 60 * 60 * COAP_TICKS_PER_SECOND) |
232 | | #else /* ! RIOT_VERSION */ |
233 | 0 | #define COAP_MAX_DELAY_TICKS (24 * 60 * 60 * COAP_TICKS_PER_SECOND) |
234 | | #endif /* ! RIOT_VERSION */ |
235 | | |
236 | | /** @} */ |
237 | | |
238 | | #ifdef __cplusplus |
239 | | } |
240 | | #endif |
241 | | |
242 | | #endif /* COAP_TIME_H_ */ |