libcoap  4.2.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  * 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 
74 COAP_STATIC_INLINE void coap_ticks(coap_tick_t *t) {
75  *t = clock_time();
76 }
77 
78 COAP_STATIC_INLINE coap_time_t coap_ticks_to_rt(coap_tick_t t) {
79  return t / COAP_TICKS_PER_SECOND;
80 }
81 
82 COAP_STATIC_INLINE uint64_t coap_ticks_to_rt_us(coap_tick_t t) {
83  return (uint64_t)t * 1000000 / COAP_TICKS_PER_SECOND;
84 }
85 
86 #else
87 #include <stdint.h>
88 
93 typedef uint64_t coap_tick_t;
94 
98 typedef time_t coap_time_t;
99 
105 typedef int64_t coap_tick_diff_t;
106 
108 #define COAP_TICKS_PER_SECOND ((coap_tick_t)(1000U))
109 
113 void coap_clock_init(void);
114 
118 void coap_ticks(coap_tick_t *t);
119 
130 coap_time_t coap_ticks_to_rt(coap_tick_t t);
131 
140 uint64_t coap_ticks_to_rt_us(coap_tick_t t);
141 
149 coap_tick_t coap_ticks_from_rt_us(uint64_t t);
150 #endif
151 
156 COAP_STATIC_INLINE int coap_time_lt(coap_tick_t a, coap_tick_t b) {
157  return ((coap_tick_diff_t)(a - b)) < 0;
158 }
159 
164 COAP_STATIC_INLINE int coap_time_le(coap_tick_t a, coap_tick_t b) {
165  return a == b || coap_time_lt(a,b);
166 }
167 
170 #endif /* COAP_TIME_H_ */
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:156
void coap_clock_init(void)
Initializes the internal clock.
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:164
#define COAP_TICKS_PER_SECOND
Use ms resolution on POSIX systems.
Definition: coap_time.h:108
uint64_t coap_tick_t
This data type represents internal timer ticks with COAP_TICKS_PER_SECOND resolution.
Definition: coap_time.h:93
time_t coap_time_t
CoAP time in seconds since epoch.
Definition: coap_time.h:98
void coap_ticks(coap_tick_t *t)
Sets t to the internal time with COAP_TICKS_PER_SECOND resolution.
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
unsigned int uint32_t
Definition: uthash.h:78
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:105