libcoap 4.3.5-develop-ea01661
Loading...
Searching...
No Matches
coap_time.h
Go to the documentation of this file.
1/*
2 * coap_time.h -- Clock Handling
3 *
4 * Copyright (C) 2010-2025 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
27#if defined(WITH_LWIP)
28
29#include <stdint.h>
30#include <lwip/sys.h>
31#elif defined(WITH_CONTIKI)
32#include "clock.h"
33#elif defined(RIOT_VERSION)
34#include <xtimer.h>
35#else /* !WITH_LWIP && !WITH_CONTIKI && !RIOT_VERSION */
36#include <stdint.h>
37#endif
38
39#ifdef __cplusplus
40extern "C" {
41#endif
42
43#if defined(WITH_LWIP)
44
45/* lwIP provides ms in sys_now */
46#define COAP_TICKS_PER_SECOND 1000
47
48typedef uint32_t coap_tick_t;
49typedef uint32_t coap_time_t;
50typedef int32_t coap_tick_diff_t;
51
53coap_ticks_impl(coap_tick_t *t) {
54 *t = sys_now();
55}
56
58coap_clock_init_impl(void) {
59}
60
61#define coap_clock_init coap_clock_init_impl
62#define coap_ticks coap_ticks_impl
63
66 return t / COAP_TICKS_PER_SECOND;
67}
68
69COAP_STATIC_INLINE uint64_t
71 return (uint64_t)t * 1000000 / COAP_TICKS_PER_SECOND;
72}
73
74#elif defined(WITH_CONTIKI)
75
76typedef clock_time_t coap_tick_t;
77typedef clock_time_t coap_time_t;
78
84typedef int coap_tick_diff_t;
85
86#define COAP_TICKS_PER_SECOND CLOCK_SECOND
87
89coap_clock_init(void) {
90}
91
94 *t = clock_time();
95}
96
99 return t / COAP_TICKS_PER_SECOND;
100}
101
102COAP_STATIC_INLINE uint64_t
104 return (uint64_t)t * 1000000 / COAP_TICKS_PER_SECOND;
105}
106
107#elif defined(RIOT_VERSION)
108
109#ifdef XTIMER_HZ
110#define COAP_TICKS_PER_SECOND (XTIMER_HZ)
111#else /* XTIMER_HZ */
112#define COAP_TICKS_PER_SECOND (1000000U)
113#endif /* XTIMER_HZ */
114
115typedef uint64_t coap_tick_t;
116typedef int64_t coap_tick_diff_t;
117typedef uint32_t coap_time_t;
118
119static inline void
120coap_clock_init(void) {}
121
122static inline void
124#ifdef MODULE_ZTIMER64_XTIMER_COMPAT
125 *t = xtimer_now_usec64();
126#else /* MODULE_ZTIMER64_XTIMER_COMPAT */
127 *t = xtimer_now_usec();
128#endif /* MODULE_ZTIMER64_XTIMER_COMPAT */
129}
130
131static inline coap_time_t
133 return t / 1000000UL;
134}
135
136static inline uint64_t
138 return t;
139}
140
141static inline coap_tick_t
142coap_ticks_from_rt_us(uint64_t t) {
143 return t / 1000000UL;
144}
145#else /* !WITH_LWIP && !WITH_CONTIKI && !RIOT_VERSION */
146
147
152typedef uint64_t coap_tick_t;
153
157typedef time_t coap_time_t;
158
164typedef int64_t coap_tick_diff_t;
165
167#define COAP_TICKS_PER_SECOND ((coap_tick_t)(1000U))
168
173
178
190
200
209#endif
210
217 return ((coap_tick_diff_t)(a - b)) < 0;
218}
219
226 return a == b || coap_time_lt(a,b);
227}
228
229/* Can delay up to 24 hrs before next wakeup (coap_tick_t can be 4 bytes or int64 */
230#define COAP_MAX_DELAY_TICKS (24 * 60 * 60 * COAP_TICKS_PER_SECOND)
231
234#ifdef __cplusplus
235}
236#endif
237
238#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:164
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:157
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:152
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:216
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:225
#define COAP_TICKS_PER_SECOND
Use ms resolution on POSIX systems.
Definition coap_time.h:167
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:57