Coverage Report

Created: 2026-02-09 06:24

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/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
#endif /* !WITH_LWIP && !WITH_CONTIKI && !RIOT_VERSION */
35
36
#ifdef __cplusplus
37
extern "C" {
38
#endif
39
40
#if defined(WITH_LWIP)
41
42
/* lwIP provides ms in sys_now */
43
#define COAP_TICKS_PER_SECOND 1000
44
45
typedef uint32_t coap_tick_t;
46
typedef uint32_t coap_time_t;
47
typedef int32_t coap_tick_diff_t;
48
49
COAP_STATIC_INLINE void
50
coap_ticks_impl(coap_tick_t *t) {
51
  *t = sys_now();
52
}
53
54
COAP_STATIC_INLINE void
55
coap_clock_init_impl(void) {
56
}
57
58
#define coap_clock_init coap_clock_init_impl
59
#define coap_ticks coap_ticks_impl
60
61
COAP_STATIC_INLINE coap_time_t
62
coap_ticks_to_rt(coap_tick_t t) {
63
  return t / COAP_TICKS_PER_SECOND;
64
}
65
66
COAP_STATIC_INLINE uint64_t
67
coap_ticks_to_rt_us(coap_tick_t t) {
68
  return (uint64_t)t * 1000000 / COAP_TICKS_PER_SECOND;
69
}
70
71
#elif defined(WITH_CONTIKI)
72
73
typedef clock_time_t coap_tick_t;
74
typedef clock_time_t coap_time_t;
75
76
/**
77
 * This data type is used to represent the difference between two clock_tick_t
78
 * values. This data type must have the same size in memory as coap_tick_t to
79
 * allow wrapping.
80
 */
81
typedef int coap_tick_diff_t;
82
83
#define COAP_TICKS_PER_SECOND CLOCK_SECOND
84
85
COAP_STATIC_INLINE void
86
coap_clock_init(void) {
87
}
88
89
COAP_STATIC_INLINE void
90
coap_ticks(coap_tick_t *t) {
91
  *t = clock_time();
92
}
93
94
COAP_STATIC_INLINE coap_time_t
95
coap_ticks_to_rt(coap_tick_t t) {
96
  return t / COAP_TICKS_PER_SECOND;
97
}
98
99
COAP_STATIC_INLINE uint64_t
100
coap_ticks_to_rt_us(coap_tick_t t) {
101
  return (uint64_t)t * 1000000 / COAP_TICKS_PER_SECOND;
102
}
103
104
#elif defined(RIOT_VERSION)
105
106
#ifdef XTIMER_HZ
107
#define COAP_TICKS_PER_SECOND (XTIMER_HZ)
108
#else /* XTIMER_HZ */
109
#define COAP_TICKS_PER_SECOND (1000000U)
110
#endif /* XTIMER_HZ */
111
112
typedef uint64_t coap_tick_t;
113
typedef int64_t coap_tick_diff_t;
114
typedef uint32_t coap_time_t;
115
116
COAP_STATIC_INLINE void
117
coap_clock_init(void) {}
118
119
COAP_STATIC_INLINE void
120
coap_ticks(coap_tick_t *t) {
121
#ifdef MODULE_ZTIMER64_XTIMER_COMPAT
122
  *t = xtimer_now_usec64();
123
#else /* MODULE_ZTIMER64_XTIMER_COMPAT */
124
  *t = xtimer_now_usec();
125
#endif /* MODULE_ZTIMER64_XTIMER_COMPAT */
126
}
127
128
COAP_STATIC_INLINE coap_time_t
129
coap_ticks_to_rt(coap_tick_t t) {
130
  return t / 1000000UL;
131
}
132
133
COAP_STATIC_INLINE uint64_t
134
coap_ticks_to_rt_us(coap_tick_t t) {
135
  return t;
136
}
137
138
COAP_STATIC_INLINE coap_tick_t
139
coap_ticks_from_rt_us(uint64_t t) {
140
  return t / 1000000UL;
141
}
142
143
#else /* !WITH_LWIP && !WITH_CONTIKI && !RIOT_VERSION */
144
145
/**
146
 * This data type represents internal timer ticks with COAP_TICKS_PER_SECOND
147
 * resolution.
148
 */
149
typedef uint64_t coap_tick_t;
150
151
/**
152
 * CoAP time in seconds since epoch.
153
 */
154
typedef time_t coap_time_t;
155
156
/**
157
 * This data type is used to represent the difference between two clock_tick_t
158
 * values. This data type must have the same size in memory as coap_tick_t to
159
 * allow wrapping.
160
 */
161
typedef int64_t coap_tick_diff_t;
162
163
/** Use ms resolution on POSIX systems */
164
24.4k
#define COAP_TICKS_PER_SECOND ((coap_tick_t)(1000U))
165
166
/**
167
 * Initializes the internal clock.
168
 */
169
void coap_clock_init(void);
170
171
/**
172
 * Sets @p t to the internal time with COAP_TICKS_PER_SECOND resolution.
173
 */
174
void coap_ticks(coap_tick_t *t);
175
176
/**
177
 * Helper function that converts coap ticks to wallclock time. On POSIX, this
178
 * function returns the number of seconds since the epoch. On other systems, it
179
 * may be the calculated number of seconds since last reboot or so.
180
 *
181
 * @param t Internal system ticks.
182
 *
183
 * @return  The number of seconds that has passed since a specific reference
184
 *          point (seconds since epoch on POSIX).
185
 */
186
coap_time_t coap_ticks_to_rt(coap_tick_t t);
187
188
/**
189
* Helper function that converts coap ticks to POSIX wallclock time in us.
190
*
191
* @param t Internal system ticks.
192
*
193
* @return  The number of seconds that has passed since a specific reference
194
*          point (seconds since epoch on POSIX).
195
*/
196
uint64_t coap_ticks_to_rt_us(coap_tick_t t);
197
198
/**
199
* Helper function that converts POSIX wallclock time in us to coap ticks.
200
*
201
* @param t POSIX time is us
202
*
203
* @return  coap ticks
204
*/
205
coap_tick_t coap_ticks_from_rt_us(uint64_t t);
206
207
#endif /* !WITH_LWIP && !WITH_CONTIKI && !RIOT_VERSION */
208
209
/**
210
 * Returns @c 1 if and only if @p a is less than @p b where less is defined on a
211
 * signed data type.
212
 */
213
COAP_STATIC_INLINE int
214
0
coap_time_lt(coap_tick_t a, coap_tick_t b) {
215
0
  return ((coap_tick_diff_t)(a - b)) < 0;
216
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_openssl.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_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_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_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
217
218
/**
219
 * Returns @c 1 if and only if @p a is less than or equal @p b where less is
220
 * defined on a signed data type.
221
 */
222
COAP_STATIC_INLINE int
223
0
coap_time_le(coap_tick_t a, coap_tick_t b) {
224
0
  return a == b || coap_time_lt(a,b);
225
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_openssl.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_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_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_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
226
227
/* Can delay up to 24 hrs before next wakeup (coap_tick_t can be 4 bytes or int64 */
228
#if defined(RIOT_VERSION)
229
#define COAP_MAX_DELAY_TICKS (24UL * 60 * 60 * COAP_TICKS_PER_SECOND)
230
#else /* ! RIOT_VERSION */
231
0
#define COAP_MAX_DELAY_TICKS (24 * 60 * 60 * COAP_TICKS_PER_SECOND)
232
#endif /* ! RIOT_VERSION */
233
234
/** @} */
235
236
#ifdef __cplusplus
237
}
238
#endif
239
240
#endif /* COAP_TIME_H_ */