libcoap 4.3.4-develop-758a341
coap_time.h
Go to the documentation of this file.
1/*
2 * coap_time.h -- Clock Handling
3 *
4 * Copyright (C) 2010-2024 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
32/* lwIP provides ms in sys_now */
33#define COAP_TICKS_PER_SECOND 1000
34
35typedef uint32_t coap_tick_t;
36typedef uint32_t coap_time_t;
37typedef int32_t coap_tick_diff_t;
38
40coap_ticks_impl(coap_tick_t *t) {
41 *t = sys_now();
42}
43
45coap_clock_init_impl(void) {
46}
47
48#define coap_clock_init coap_clock_init_impl
49#define coap_ticks coap_ticks_impl
50
53 return t / COAP_TICKS_PER_SECOND;
54}
55
56COAP_STATIC_INLINE uint64_t
58 return (uint64_t)t * 1000000 / COAP_TICKS_PER_SECOND;
59}
60
61#elif defined(WITH_CONTIKI)
62
63#include "clock.h"
64
65typedef clock_time_t coap_tick_t;
66typedef clock_time_t coap_time_t;
67
73typedef int coap_tick_diff_t;
74
75#define COAP_TICKS_PER_SECOND CLOCK_SECOND
76
78coap_clock_init(void) {
79}
80
83 *t = clock_time();
84}
85
88 return t / COAP_TICKS_PER_SECOND;
89}
90
91COAP_STATIC_INLINE uint64_t
93 return (uint64_t)t * 1000000 / COAP_TICKS_PER_SECOND;
94}
95
96#elif defined(RIOT_VERSION)
97#include <xtimer.h>
98
99#ifdef XTIMER_HZ
100#define COAP_TICKS_PER_SECOND (XTIMER_HZ)
101#else /* XTIMER_HZ */
102#define COAP_TICKS_PER_SECOND (1000000U)
103#endif /* XTIMER_HZ */
104
105typedef uint64_t coap_tick_t;
106typedef int64_t coap_tick_diff_t;
107typedef uint32_t coap_time_t;
108
109static inline void
110coap_clock_init(void) {}
111
112static inline void
114#ifdef MODULE_ZTIMER64_XTIMER_COMPAT
115 *t = xtimer_now_usec64();
116#else /* MODULE_ZTIMER64_XTIMER_COMPAT */
117 *t = xtimer_now_usec();
118#endif /* MODULE_ZTIMER64_XTIMER_COMPAT */
119}
120
121static inline coap_time_t
123 return t / 1000000UL;
124}
125
126static inline uint64_t
128 return t;
129}
130
131static inline coap_tick_t
132coap_ticks_from_rt_us(uint64_t t) {
133 return t / 1000000UL;
134}
135#else /* !WITH_LWIP && !WITH_CONTIKI && !RIOT_VERSION */
136
137#include <stdint.h>
138
143typedef uint64_t coap_tick_t;
144
148typedef time_t coap_time_t;
149
155typedef int64_t coap_tick_diff_t;
156
158#define COAP_TICKS_PER_SECOND ((coap_tick_t)(1000U))
159
164
169
181
191
200#endif
201
208 return ((coap_tick_diff_t)(a - b)) < 0;
209}
210
217 return a == b || coap_time_lt(a,b);
218}
219
222#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:155
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:148
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:143
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:207
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:216
#define COAP_TICKS_PER_SECOND
Use ms resolution on POSIX systems.
Definition: coap_time.h:158
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:53