Initial merge-commit of the OIC code. Should successfully do discovery for single...
[platform/upstream/iotivity.git] / 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 #ifndef coap_clock_init
108 static inline void
109 coap_clock_init_impl(void) {
110 #ifdef HAVE_TIME_H
111   clock_offset = time(NULL);
112 #else
113 #  ifdef __GNUC__
114     /* Issue a warning when using gcc. Other prepropressors do 
115      *  not seem to have a similar feature. */ 
116 #   warning "cannot initialize clock"
117 #  endif
118   clock_offset = 0;
119 #endif
120 }
121 #define coap_clock_init coap_clock_init_impl
122 #endif /* coap_clock_init */
123
124 #ifndef coap_ticks
125 static inline void
126 coap_ticks_impl(coap_tick_t *t) {
127 #ifdef HAVE_SYS_TIME_H
128   struct timeval tv;
129   gettimeofday(&tv, NULL);
130   *t = (tv.tv_sec - clock_offset) * COAP_TICKS_PER_SECOND 
131     + (tv.tv_usec * COAP_TICKS_PER_SECOND / 1000000);
132 #else
133 #error "clock not implemented"
134 #endif
135 }
136 #define coap_ticks coap_ticks_impl
137 #endif /* coap_ticks */
138
139 /**
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.
142  */
143 static inline
144 int coap_time_lt(coap_tick_t a, coap_tick_t b) {
145   return ((coap_tick_diff_t)(a - b)) < 0;
146 }
147
148 /**
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.
151  */
152 static inline
153 int coap_time_le(coap_tick_t a, coap_tick_t b) {
154   return a == b || coap_time_lt(a,b);
155 }
156
157 /** @} */
158
159 #ifdef __cplusplus
160 }
161 #endif
162
163
164 #endif /* _COAP_TIME_H_ */