libcoap  4.2.0
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 
50 #elif defined(WITH_CONTIKI)
51 
52 #include "clock.h"
53 
54 typedef clock_time_t coap_tick_t;
55 typedef clock_time_t coap_time_t;
56 
62 typedef int coap_tick_diff_t;
63 
64 #define COAP_TICKS_PER_SECOND CLOCK_SECOND
65 
67  clock_init();
68 }
69 
71  *t = clock_time();
72 }
73 
75  return t / COAP_TICKS_PER_SECOND;
76 }
77 
78 #else
79 #include <stdint.h>
80 
85 typedef uint64_t coap_tick_t;
86 
90 typedef time_t coap_time_t;
91 
97 typedef int64_t coap_tick_diff_t;
98 
100 #define COAP_TICKS_PER_SECOND ((coap_tick_t)(1000U))
101 
105 void coap_clock_init(void);
106 
110 void coap_ticks(coap_tick_t *t);
111 
123 
132 uint64_t coap_ticks_to_rt_us(coap_tick_t t);
133 
142 #endif
143 
149  return ((coap_tick_diff_t)(a - b)) < 0;
150 }
151 
157  return a == b || coap_time_lt(a,b);
158 }
159 
162 #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:148
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:156
#define COAP_TICKS_PER_SECOND
Use ms resolution on POSIX systems.
Definition: coap_time.h:100
uint64_t coap_tick_t
This data type represents internal timer ticks with COAP_TICKS_PER_SECOND resolution.
Definition: coap_time.h:85
time_t coap_time_t
CoAP time in seconds since epoch.
Definition: coap_time.h:90
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:97