libcoap
4.3.5
Toggle main menu visibility
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-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
16
17
#ifndef COAP_TIME_H_
18
#define COAP_TIME_H_
19
26
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
40
coap_ticks_impl(
coap_tick_t
*t) {
41
*t = sys_now();
42
}
43
44
COAP_STATIC_INLINE
void
45
coap_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
}
80
81
COAP_STATIC_INLINE
void
82
coap_ticks
(
coap_tick_t
*t) {
83
*t = clock_time();
84
}
85
86
COAP_STATIC_INLINE
coap_time_t
87
coap_ticks_to_rt
(
coap_tick_t
t) {
88
return
t /
COAP_TICKS_PER_SECOND
;
89
}
90
91
COAP_STATIC_INLINE
uint64_t
92
coap_ticks_to_rt_us
(
coap_tick_t
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
105
typedef
uint64_t
coap_tick_t
;
106
typedef
int64_t
coap_tick_diff_t
;
107
typedef
uint32_t
coap_time_t
;
108
109
static
inline
void
110
coap_clock_init
(
void
) {}
111
112
static
inline
void
113
coap_ticks
(
coap_tick_t
*t) {
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
121
static
inline
coap_time_t
122
coap_ticks_to_rt
(
coap_tick_t
t) {
123
return
t / 1000000UL;
124
}
125
126
static
inline
uint64_t
127
coap_ticks_to_rt_us
(
coap_tick_t
t) {
128
return
t;
129
}
130
131
static
inline
coap_tick_t
132
coap_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
143
typedef
uint64_t
coap_tick_t
;
144
148
typedef
time_t
coap_time_t
;
149
155
typedef
int64_t
coap_tick_diff_t
;
156
158
#define COAP_TICKS_PER_SECOND ((coap_tick_t)(1000U))
159
163
void
coap_clock_init
(
void
);
164
168
void
coap_ticks
(
coap_tick_t
*t);
169
180
coap_time_t
coap_ticks_to_rt
(
coap_tick_t
t);
181
190
uint64_t
coap_ticks_to_rt_us
(
coap_tick_t
t);
191
199
coap_tick_t
coap_ticks_from_rt_us
(uint64_t t);
200
#endif
201
206
COAP_STATIC_INLINE
int
207
coap_time_lt
(
coap_tick_t
a,
coap_tick_t
b) {
208
return
((
coap_tick_diff_t
)(a - b)) < 0;
209
}
210
215
COAP_STATIC_INLINE
int
216
coap_time_le
(
coap_tick_t
a,
coap_tick_t
b) {
217
return
a == b ||
coap_time_lt
(a,b);
218
}
219
221
222
#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:155
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:148
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:143
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:207
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:216
COAP_TICKS_PER_SECOND
#define COAP_TICKS_PER_SECOND
Use ms resolution on POSIX systems.
Definition
coap_time.h:158
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
include
coap3
coap_time.h
Generated on
for libcoap by
1.17.0