libcoap  4.3.0rc2
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  * This file is part of the CoAP library libcoap. Please see README for terms
7  * of use.
8  */
9 
15 #ifndef COAP_TIME_H_
16 #define COAP_TIME_H_
17 
24 #if defined(WITH_LWIP)
25 
26 #include <stdint.h>
27 #include <lwip/sys.h>
28 
29 /* lwIP provides ms in sys_now */
30 #define COAP_TICKS_PER_SECOND 1000
31 
32 typedef uint32_t coap_tick_t;
33 typedef uint32_t coap_time_t;
34 typedef int32_t coap_tick_diff_t;
35 
36 COAP_STATIC_INLINE void coap_ticks_impl(coap_tick_t *t) {
37  *t = sys_now();
38 }
39 
40 COAP_STATIC_INLINE void coap_clock_init_impl(void) {
41 }
42 
43 #define coap_clock_init coap_clock_init_impl
44 #define coap_ticks coap_ticks_impl
45 
47  return t / COAP_TICKS_PER_SECOND;
48 }
49 
51  return (uint64_t)t * 1000000 / COAP_TICKS_PER_SECOND;
52 }
53 
54 #elif defined(WITH_CONTIKI)
55 
56 #include "clock.h"
57 
58 typedef clock_time_t coap_tick_t;
59 typedef clock_time_t coap_time_t;
60 
66 typedef int coap_tick_diff_t;
67 
68 #define COAP_TICKS_PER_SECOND CLOCK_SECOND
69 
71  clock_init();
72 }
73 
75  *t = clock_time();
76 }
77 
79  return t / COAP_TICKS_PER_SECOND;
80 }
81 
83  return (uint64_t)t * 1000000 / COAP_TICKS_PER_SECOND;
84 }
85 
86 #elif defined(RIOT_VERSION)
87 #include <xtimer.h>
88 
89 #define COAP_TICKS_PER_SECOND (XTIMER_HZ)
90 
91 typedef uint64_t coap_tick_t;
92 typedef int64_t coap_tick_diff_t;
93 typedef uint32_t coap_time_t;
94 
95 static inline void coap_clock_init(void) {}
96 
97 static inline void coap_ticks(coap_tick_t *t) {
98  *t = xtimer_now_usec64();
99 }
100 
101 static inline coap_time_t coap_ticks_to_rt(coap_tick_t t) {
102  return t / 1000000UL;
103 }
104 
105 static inline uint64_t coap_ticks_to_rt_us(coap_tick_t t) {
106  return t;
107 }
108 
109 static inline coap_tick_t coap_ticks_from_rt_us(uint64_t t) {
110  return t / 1000000UL;
111 }
112 #else /* !WITH_LWIP && !WITH_CONTIKI && !RIOT_VERSION */
113 
114 #include <stdint.h>
115 
120 typedef uint64_t coap_tick_t;
121 
125 typedef time_t coap_time_t;
126 
132 typedef int64_t coap_tick_diff_t;
133 
135 #define COAP_TICKS_PER_SECOND ((coap_tick_t)(1000U))
136 
140 void coap_clock_init(void);
141 
146 
158 
168 
177 #endif
178 
184  return ((coap_tick_diff_t)(a - b)) < 0;
185 }
186 
192  return a == b || coap_time_lt(a,b);
193 }
194 
197 #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:132
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:125
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:120
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:183
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:191
#define COAP_TICKS_PER_SECOND
Use ms resolution on POSIX systems.
Definition: coap_time.h:135
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:38