libcoap 4.3.2rc2

coap_time.h
Go to the documentation of this file.
1/*
2 * coap_time.h -- Clock Handling
3 *
4 * Copyright (C) 2010-2023 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
35 typedef uint32_t coap_tick_t;
36 typedef uint32_t coap_time_t;
37 typedef int32_t coap_tick_diff_t;
38
39 COAP_STATIC_INLINE void
40coap_ticks_impl(coap_tick_t *t) {
41 *t = sys_now();
42}
43
44 COAP_STATIC_INLINE void
45coap_clock_init_impl(void) {
46}
47
48#define coap_clock_init coap_clock_init_impl
49#define coap_ticks coap_ticks_impl
50
51 COAP_STATIC_INLINE coap_time_t
52 coap_ticks_to_rt(coap_tick_t t) {
53 return t / COAP_TICKS_PER_SECOND;
54}
55
56 COAP_STATIC_INLINE uint64_t
57 coap_ticks_to_rt_us(coap_tick_t t) {
58 return (uint64_t)t * 1000000 / COAP_TICKS_PER_SECOND;
59}
60
61#elif defined(WITH_CONTIKI)
62
63#include "clock.h"
64
65 typedef clock_time_t coap_tick_t;
66 typedef clock_time_t coap_time_t;
67
73 typedef int coap_tick_diff_t;
74
75#define COAP_TICKS_PER_SECOND CLOCK_SECOND
76
77 COAP_STATIC_INLINE void
78 coap_clock_init(void) {
79 clock_init();
80}
81
82 COAP_STATIC_INLINE void
83 coap_ticks(coap_tick_t *t) {
84 *t = clock_time();
85}
86
87 COAP_STATIC_INLINE coap_time_t
88 coap_ticks_to_rt(coap_tick_t t) {
89 return t / COAP_TICKS_PER_SECOND;
90}
91
92 COAP_STATIC_INLINE uint64_t
93 coap_ticks_to_rt_us(coap_tick_t t) {
94 return (uint64_t)t * 1000000 / COAP_TICKS_PER_SECOND;
95}
96
97#elif defined(RIOT_VERSION)
98#include <xtimer.h>
99
100#ifdef XTIMER_HZ
101#define COAP_TICKS_PER_SECOND (XTIMER_HZ)
102#else /* XTIMER_HZ */
103#define COAP_TICKS_PER_SECOND (1000000U)
104#endif /* XTIMER_HZ */
105
106 typedef uint64_t coap_tick_t;
107 typedef int64_t coap_tick_diff_t;
108 typedef uint32_t coap_time_t;
109
110 static inline void
111 coap_clock_init(void) {}
112
113 static inline void
114 coap_ticks(coap_tick_t *t) {
115#ifdef MODULE_ZTIMER64_XTIMER_COMPAT
116 *t = xtimer_now_usec64();
117#else /* MODULE_ZTIMER64_XTIMER_COMPAT */
118 *t = xtimer_now_usec();
119#endif /* MODULE_ZTIMER64_XTIMER_COMPAT */
120}
121
122 static inline coap_time_t
123 coap_ticks_to_rt(coap_tick_t t) {
124 return t / 1000000UL;
125}
126
127 static inline uint64_t
128 coap_ticks_to_rt_us(coap_tick_t t) {
129 return t;
130}
131
132 static inline coap_tick_t
133 coap_ticks_from_rt_us(uint64_t t) {
134 return t / 1000000UL;
135}
136#else /* !WITH_LWIP && !WITH_CONTIKI && !RIOT_VERSION */
137
138#include <stdint.h>
139
144 typedef uint64_t coap_tick_t;
145
149 typedef time_t coap_time_t;
150
156 typedef int64_t coap_tick_diff_t;
157
159#define COAP_TICKS_PER_SECOND ((coap_tick_t)(1000U))
160
164 void coap_clock_init(void);
165
169 void coap_ticks(coap_tick_t *t);
170
181 coap_time_t coap_ticks_to_rt(coap_tick_t t);
182
191uint64_t coap_ticks_to_rt_us(coap_tick_t t);
192
200 coap_tick_t coap_ticks_from_rt_us(uint64_t t);
201#endif
202
207 COAP_STATIC_INLINE int
208 coap_time_lt(coap_tick_t a, coap_tick_t b) {
209 return ((coap_tick_diff_t)(a - b)) < 0;
210}
211
216 COAP_STATIC_INLINE int
217 coap_time_le(coap_tick_t a, coap_tick_t b) {
218 return a == b || coap_time_lt(a,b);
219}
220
223#endif /* COAP_TIME_H_ */
coap_ticks_from_rt_us
coap_tick_t coap_ticks_from_rt_us(uint64_t t)
Helper function that converts POSIX wallclock time in us to coap ticks.
coap_tick_diff_t
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:156
coap_ticks
void coap_ticks(coap_tick_t *t)
Sets t to the internal time with COAP_TICKS_PER_SECOND resolution.
coap_time_t
time_t coap_time_t
CoAP time in seconds since epoch.
Definition: coap_time.h:149
coap_clock_init
void coap_clock_init(void)
Initializes the internal clock.
coap_tick_t
uint64_t coap_tick_t
This data type represents internal timer ticks with COAP_TICKS_PER_SECOND resolution.
Definition: coap_time.h:144
coap_time_lt
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:208
coap_ticks_to_rt
coap_time_t coap_ticks_to_rt(coap_tick_t t)
Helper function that converts coap ticks to wallclock time.
coap_time_le
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:217
COAP_TICKS_PER_SECOND
#define COAP_TICKS_PER_SECOND
Use ms resolution on POSIX systems.
Definition: coap_time.h:159
coap_ticks_to_rt_us
uint64_t coap_ticks_to_rt_us(coap_tick_t t)
Helper function that converts coap ticks to POSIX wallclock time in us.
COAP_STATIC_INLINE
#define COAP_STATIC_INLINE
Definition: libcoap.h:53