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 */
107 #ifndef coap_clock_init
109 coap_clock_init_impl(void) {
111 clock_offset = time(NULL);
114 /* Issue a warning when using gcc. Other prepropressors do
115 * not seem to have a similar feature. */
116 # warning "cannot initialize clock"
121 #define coap_clock_init coap_clock_init_impl
122 #endif /* coap_clock_init */
126 coap_ticks_impl(coap_tick_t *t) {
127 #ifdef HAVE_SYS_TIME_H
129 gettimeofday(&tv, NULL);
130 *t = (tv.tv_sec - clock_offset) * COAP_TICKS_PER_SECOND
131 + (tv.tv_usec * COAP_TICKS_PER_SECOND / 1000000);
133 #error "clock not implemented"
136 #define coap_ticks coap_ticks_impl
137 #endif /* coap_ticks */
140 * Returns @c 1 if and only if @p a is less than @p b where less is
141 * defined on a signed data type.
144 int coap_time_lt(coap_tick_t a, coap_tick_t b) {
145 return ((coap_tick_diff_t)(a - b)) < 0;
149 * Returns @c 1 if and only if @p a is less than or equal @p b where
150 * less is defined on a signed data type.
153 int coap_time_le(coap_tick_t a, coap_tick_t b) {
154 return a == b || coap_time_lt(a,b);
164 #endif /* _COAP_TIME_H_ */