libcoap 4.3.5-develop-490e4e0
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#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
39extern "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
47typedef uint32_t coap_tick_t;
48typedef uint32_t coap_time_t;
49typedef int32_t coap_tick_diff_t;
50
52coap_ticks_impl(coap_tick_t *t) {
53 *t = sys_now();
54}
55
57coap_clock_init_impl(void) {
58}
59
60#define coap_clock_init coap_clock_init_impl
61#define coap_ticks coap_ticks_impl
62
65 return t / COAP_TICKS_PER_SECOND;
66}
67
68COAP_STATIC_INLINE uint64_t
70 return (uint64_t)t * 1000000 / COAP_TICKS_PER_SECOND;
71}
72
73#elif defined(WITH_CONTIKI)
74
75typedef clock_time_t coap_tick_t;
76typedef clock_time_t coap_time_t;
77
83typedef int coap_tick_diff_t;
84
85#define COAP_TICKS_PER_SECOND CLOCK_SECOND
86
88coap_clock_init(void) {
89}
90
93 *t = clock_time();
94}
95
98 return t / COAP_TICKS_PER_SECOND;
99}
100
101COAP_STATIC_INLINE uint64_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
114typedef uint64_t coap_tick_t;
115typedef int64_t coap_tick_diff_t;
116typedef uint32_t coap_time_t;
117
119coap_clock_init(void) {}
120
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
132 return t / 1000000UL;
133}
134
135COAP_STATIC_INLINE uint64_t
137 return t;
138}
139
141coap_ticks_from_rt_us(uint64_t t) {
142 return t / 1000000UL;
143}
144
145#else /* !WITH_LWIP && !WITH_CONTIKI && !RIOT_VERSION */
146
151typedef uint64_t coap_tick_t;
152
156typedef time_t coap_time_t;
157
163typedef int64_t coap_tick_diff_t;
164
166#define COAP_TICKS_PER_SECOND ((coap_tick_t)(1000U))
167
171void coap_clock_init(void);
172
176void coap_ticks(coap_tick_t *t);
177
189
199
208
209#endif /* !WITH_LWIP && !WITH_CONTIKI && !RIOT_VERSION */
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.
Definition coap_time.c:133
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:163
void coap_ticks(coap_tick_t *t)
Sets t to the internal time with COAP_TICKS_PER_SECOND resolution.
Definition coap_time.c:90
time_t coap_time_t
CoAP time in seconds since epoch.
Definition coap_time.h:156
void coap_clock_init(void)
Initializes the internal clock.
Definition coap_time.c:68
uint64_t coap_tick_t
This data type represents internal timer ticks with COAP_TICKS_PER_SECOND resolution.
Definition coap_time.h:151
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.
Definition coap_time.c:123
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:166
uint64_t coap_ticks_to_rt_us(coap_tick_t t)
Helper function that converts coap ticks to POSIX wallclock time in us.
Definition coap_time.c:128
#define COAP_STATIC_INLINE
Definition libcoap.h:57