iotivity 0.9.0
[platform/upstream/iotivity.git] / resource / csdk / libcoap-4.1.1 / coap_time.h
1 /* coap_time.h -- Clock Handling
2  *
3  * Copyright (C) 2010--2013 Olaf Bergmann <bergmann@tzi.org>
4  *
5  * This file is part of the CoAP library libcoap. Please see
6  * README for terms of use. 
7  */
8
9 /** 
10  * @file coap_time.h
11  * @brief Clock Handling
12  */
13
14 #ifndef _COAP_TIME_H_
15 #define _COAP_TIME_H_
16
17 /*
18 ** Make sure we can call this stuff from C++.
19 */
20 #ifdef __cplusplus
21 extern "C" {
22 #endif
23
24
25 #include "config.h"
26
27 /**
28  * @defgroup clock Clock Handling
29  * Default implementation of internal clock. You should redefine this if
30  * you do not have time() and gettimeofday().
31  * @{
32  */
33
34 #ifdef WITH_LWIP
35
36 #include <stdint.h>
37 #include <lwip/sys.h>
38
39 /* lwIP provides ms in sys_now */
40 #define COAP_TICKS_PER_SECOND 1000
41
42 typedef uint32_t coap_tick_t;
43
44 static inline void coap_ticks_impl(coap_tick_t *t)
45 {
46         *t = sys_now();
47 }
48
49 static inline void coap_clock_init_impl(void)
50 {
51 }
52
53 #define coap_clock_init coap_clock_init_impl
54
55 #define coap_ticks coap_ticks_impl
56
57 #endif
58 #ifdef WITH_CONTIKI
59 #include "clock.h"
60
61 typedef clock_time_t coap_tick_t;
62
63 /**
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.
67  */
68 typedef int coap_tick_diff_t;
69
70 #define COAP_TICKS_PER_SECOND CLOCK_SECOND
71
72 /** Set at startup to initialize the internal clock (time in seconds). */
73 extern clock_time_t clock_offset;
74
75 static inline void
76 contiki_clock_init_impl(void) {
77   clock_init();
78   clock_offset = clock_time();
79 }
80
81 #define coap_clock_init contiki_clock_init_impl
82
83 static inline void
84 contiki_ticks_impl(coap_tick_t *t) {
85   *t = clock_time();
86 }
87
88 #define coap_ticks contiki_ticks_impl
89
90 #endif /* WITH_CONTIKI */
91 #ifdef WITH_POSIX
92 typedef unsigned int coap_tick_t; 
93
94 /**
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.
98  */
99 typedef int coap_tick_diff_t;
100
101 #define COAP_TICKS_PER_SECOND 1024
102
103 /** Set at startup to initialize the internal clock (time in seconds). */
104 extern time_t clock_offset;
105 #endif /* WITH_POSIX */
106
107 #ifdef WITH_ARDUINO
108 #include "Time.h"
109 typedef time_t coap_tick_t;
110
111 /**
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.
115  */
116 typedef int coap_tick_diff_t;
117
118 /* TODO: Ticks per second value for Arduino needs verification from 
119  * documentation */
120 #define COAP_TICKS_PER_SECOND 1000
121
122 extern time_t clock_offset;
123
124 #endif /* WITH_ARDUINO */
125
126 #ifndef coap_clock_init
127 static inline void
128 coap_clock_init_impl(void) {
129 #ifdef HAVE_TIME_H
130   clock_offset = time(NULL);
131 #else
132 #  ifdef WITH_ARDUINO
133      clock_offset = now();
134 #  else
135 #    ifdef __GNUC__
136       /* Issue a warning when using gcc. Other prepropressors do 
137        *  not seem to have a similar feature. */ 
138 #     warning "cannot initialize clock"
139 #    endif
140      clock_offset = 0;
141 #  endif /* WITH_ARDUINO */
142 #endif /* HAVE_TIME */
143 }
144 #define coap_clock_init coap_clock_init_impl
145 #endif /* coap_clock_init */
146
147 #ifndef coap_ticks
148 static inline void
149 coap_ticks_impl(coap_tick_t *t) {
150 #ifdef HAVE_SYS_TIME_H
151   struct timeval tv;
152   gettimeofday(&tv, NULL);
153   *t = (tv.tv_sec - clock_offset) * COAP_TICKS_PER_SECOND 
154     + (tv.tv_usec * COAP_TICKS_PER_SECOND / 1000000);
155 #else
156 #  ifdef WITH_ARDUINO
157     coap_tick_t tv;
158     tv = now();
159     *t = (tv - clock_offset)*COAP_TICKS_PER_SECOND;
160 #  else
161 #    error "clock not implemented"
162 #  endif /* WITH_ARDUINO */
163 #endif /* HAVE_SYS_TIME_H */
164 }
165 #define coap_ticks coap_ticks_impl
166 #endif /* coap_ticks */
167
168 /**
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.
171  */
172 static inline
173 int coap_time_lt(coap_tick_t a, coap_tick_t b) {
174   return ((coap_tick_diff_t)(a - b)) < 0;
175 }
176
177 /**
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.
180  */
181 static inline
182 int coap_time_le(coap_tick_t a, coap_tick_t b) {
183   return a == b || coap_time_lt(a,b);
184 }
185
186 /** @} */
187
188 #ifdef __cplusplus
189 }
190 #endif
191
192
193 #endif /* _COAP_TIME_H_ */