libcoap 4.3.5-develop-0bcd592
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-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
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#endif /* !WITH_LWIP && !WITH_CONTIKI && !RIOT_VERSION */
35
36#ifdef __cplusplus
37extern "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
45typedef uint32_t coap_tick_t;
46typedef uint32_t coap_time_t;
47typedef int32_t coap_tick_diff_t;
48
50coap_ticks_impl(coap_tick_t *t) {
51 *t = sys_now();
52}
53
55coap_clock_init_impl(void) {
56}
57
58#define coap_clock_init coap_clock_init_impl
59#define coap_ticks coap_ticks_impl
60
63 return t / COAP_TICKS_PER_SECOND;
64}
65
66COAP_STATIC_INLINE uint64_t
68 return (uint64_t)t * 1000000 / COAP_TICKS_PER_SECOND;
69}
70
71#elif defined(WITH_CONTIKI)
72
73typedef clock_time_t coap_tick_t;
74typedef clock_time_t coap_time_t;
75
81typedef int coap_tick_diff_t;
82
83#define COAP_TICKS_PER_SECOND CLOCK_SECOND
84
86coap_clock_init(void) {
87}
88
91 *t = clock_time();
92}
93
96 return t / COAP_TICKS_PER_SECOND;
97}
98
99COAP_STATIC_INLINE uint64_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
112typedef uint64_t coap_tick_t;
113typedef int64_t coap_tick_diff_t;
114typedef uint32_t coap_time_t;
115
117coap_clock_init(void) {}
118
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
130 return t / 1000000UL;
131}
132
133COAP_STATIC_INLINE uint64_t
135 return t;
136}
137
139coap_ticks_from_rt_us(uint64_t t) {
140 return t / 1000000UL;
141}
142
143#else /* !WITH_LWIP && !WITH_CONTIKI && !RIOT_VERSION */
144
149typedef uint64_t coap_tick_t;
150
154typedef time_t coap_time_t;
155
161typedef int64_t coap_tick_diff_t;
162
164#define COAP_TICKS_PER_SECOND ((coap_tick_t)(1000U))
165
169void coap_clock_init(void);
170
174void coap_ticks(coap_tick_t *t);
175
187
197
206
207#endif /* !WITH_LWIP && !WITH_CONTIKI && !RIOT_VERSION */
208
215 return ((coap_tick_diff_t)(a - b)) < 0;
216}
217
224 return a == b || coap_time_lt(a,b);
225}
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#define COAP_MAX_DELAY_TICKS (24 * 60 * 60 * COAP_TICKS_PER_SECOND)
232#endif /* ! RIOT_VERSION */
233
236#ifdef __cplusplus
237}
238#endif
239
240#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:161
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:154
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:149
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:214
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:223
#define COAP_TICKS_PER_SECOND
Use ms resolution on POSIX systems.
Definition coap_time.h:164
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