libcoap  4.3.1
coap_time.h
Go to the documentation of this file.
1 /*
2  * coap_time.h -- Clock Handling
3  *
4  * Copyright (C) 2010-2019 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 coap_ticks_impl(coap_tick_t *t) {
40  *t = sys_now();
41 }
42 
43 COAP_STATIC_INLINE void coap_clock_init_impl(void) {
44 }
45 
46 #define coap_clock_init coap_clock_init_impl
47 #define coap_ticks coap_ticks_impl
48 
50  return t / COAP_TICKS_PER_SECOND;
51 }
52 
54  return (uint64_t)t * 1000000 / COAP_TICKS_PER_SECOND;
55 }
56 
57 #elif defined(WITH_CONTIKI)
58 
59 #include "clock.h"
60 
61 typedef clock_time_t coap_tick_t;
62 typedef clock_time_t coap_time_t;
63 
69 typedef int coap_tick_diff_t;
70 
71 #define COAP_TICKS_PER_SECOND CLOCK_SECOND
72 
74  clock_init();
75 }
76 
78  *t = clock_time();
79 }
80 
82  return t / COAP_TICKS_PER_SECOND;
83 }
84 
86  return (uint64_t)t * 1000000 / COAP_TICKS_PER_SECOND;
87 }
88 
89 #elif defined(RIOT_VERSION)
90 #include <xtimer.h>
91 
92 #ifdef XTIMER_HZ
93 #define COAP_TICKS_PER_SECOND (XTIMER_HZ)
94 #else /* XTIMER_HZ */
95 #define COAP_TICKS_PER_SECOND (XTIMER_HZ_BASE)
96 #endif /* XTIMER_HZ */
97 
98 typedef uint64_t coap_tick_t;
99 typedef int64_t coap_tick_diff_t;
100 typedef uint32_t coap_time_t;
101 
102 static inline void coap_clock_init(void) {}
103 
104 static inline void coap_ticks(coap_tick_t *t) {
105  *t = xtimer_now_usec64();
106 }
107 
108 static inline coap_time_t coap_ticks_to_rt(coap_tick_t t) {
109  return t / 1000000UL;
110 }
111 
112 static inline uint64_t coap_ticks_to_rt_us(coap_tick_t t) {
113  return t;
114 }
115 
116 static inline coap_tick_t coap_ticks_from_rt_us(uint64_t t) {
117  return t / 1000000UL;
118 }
119 #else /* !WITH_LWIP && !WITH_CONTIKI && !RIOT_VERSION */
120 
121 #include <stdint.h>
122 
127 typedef uint64_t coap_tick_t;
128 
132 typedef time_t coap_time_t;
133 
139 typedef int64_t coap_tick_diff_t;
140 
142 #define COAP_TICKS_PER_SECOND ((coap_tick_t)(1000U))
143 
147 void coap_clock_init(void);
148 
153 
165 
175 
184 #endif
185 
191  return ((coap_tick_diff_t)(a - b)) < 0;
192 }
193 
199  return a == b || coap_time_lt(a,b);
200 }
201 
204 #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:139
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:132
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:127
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:190
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:198
#define COAP_TICKS_PER_SECOND
Use ms resolution on POSIX systems.
Definition: coap_time.h:142
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:45