1 /* coap_time.h -- Clock Handling
3 * Copyright (C) 2010--2013 Olaf Bergmann <bergmann@tzi.org>
5 * This file is part of the CoAP library libcoap. Please see
6 * README for terms of use.
11 * @brief Clock Handling
18 ** Make sure we can call this stuff from C++.
28 * @defgroup clock Clock Handling
29 * Default implementation of internal clock. You should redefine this if
30 * you do not have time() and gettimeofday().
39 /* lwIP provides ms in sys_now */
40 #define COAP_TICKS_PER_SECOND 1000
42 typedef uint32_t coap_tick_t;
44 static inline void coap_ticks_impl(coap_tick_t *t)
49 static inline void coap_clock_init_impl(void)
53 #define coap_clock_init coap_clock_init_impl
55 #define coap_ticks coap_ticks_impl
61 typedef clock_time_t coap_tick_t;
64 * This data type is used to represent the difference between two
65 * clock_tick_t values. This data type must have the same size in
66 * memory as coap_tick_t to allow wrapping.
68 typedef int coap_tick_diff_t;
70 #define COAP_TICKS_PER_SECOND CLOCK_SECOND
72 /** Set at startup to initialize the internal clock (time in seconds). */
73 extern clock_time_t clock_offset;
76 contiki_clock_init_impl(void) {
78 clock_offset = clock_time();
81 #define coap_clock_init contiki_clock_init_impl
84 contiki_ticks_impl(coap_tick_t *t) {
88 #define coap_ticks contiki_ticks_impl
90 #endif /* WITH_CONTIKI */
92 typedef unsigned int coap_tick_t;
95 * This data type is used to represent the difference between two
96 * clock_tick_t values. This data type must have the same size in
97 * memory as coap_tick_t to allow wrapping.
99 typedef int coap_tick_diff_t;
101 #define COAP_TICKS_PER_SECOND 1024
103 /** Set at startup to initialize the internal clock (time in seconds). */
104 extern time_t clock_offset;
105 #endif /* WITH_POSIX */
109 typedef time_t coap_tick_t;
112 * This data type is used to represent the difference between two
113 * clock_tick_t values. This data type must have the same size in
114 * memory as coap_tick_t to allow wrapping.
116 typedef int coap_tick_diff_t;
118 /* TODO: Ticks per second value for Arduino needs verification from
120 #define COAP_TICKS_PER_SECOND 1000
122 extern time_t clock_offset;
124 #endif /* WITH_ARDUINO */
126 #ifndef coap_clock_init
128 coap_clock_init_impl(void) {
130 clock_offset = time(NULL);
133 clock_offset = now();
136 /* Issue a warning when using gcc. Other prepropressors do
137 * not seem to have a similar feature. */
138 # warning "cannot initialize clock"
141 # endif /* WITH_ARDUINO */
142 #endif /* HAVE_TIME */
144 #define coap_clock_init coap_clock_init_impl
145 #endif /* coap_clock_init */
149 coap_ticks_impl(coap_tick_t *t) {
150 #ifdef HAVE_SYS_TIME_H
152 gettimeofday(&tv, NULL);
153 *t = (tv.tv_sec - clock_offset) * COAP_TICKS_PER_SECOND
154 + (tv.tv_usec * COAP_TICKS_PER_SECOND / 1000000);
159 *t = (tv - clock_offset)*COAP_TICKS_PER_SECOND;
161 # error "clock not implemented"
162 # endif /* WITH_ARDUINO */
163 #endif /* HAVE_SYS_TIME_H */
165 #define coap_ticks coap_ticks_impl
166 #endif /* coap_ticks */
169 * Returns @c 1 if and only if @p a is less than @p b where less is
170 * defined on a signed data type.
173 int coap_time_lt(coap_tick_t a, coap_tick_t b) {
174 return ((coap_tick_diff_t)(a - b)) < 0;
178 * Returns @c 1 if and only if @p a is less than or equal @p b where
179 * less is defined on a signed data type.
182 int coap_time_le(coap_tick_t a, coap_tick_t b) {
183 return a == b || coap_time_lt(a,b);
193 #endif /* _COAP_TIME_H_ */