libcoap  4.1.1
 All Data Structures Files Functions Variables Typedefs Macros Groups Pages
coap_time.h
Go to the documentation of this file.
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 
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 
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 
68 typedef int coap_tick_diff_t;
69 
70 #define COAP_TICKS_PER_SECOND CLOCK_SECOND
71 
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 
99 typedef int coap_tick_diff_t;
100 
101 #define COAP_TICKS_PER_SECOND 1024
102 
104 extern time_t clock_offset;
105 #endif /* WITH_POSIX */
106 
107 #ifndef coap_clock_init
108 static inline 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 
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 
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 
159 #ifdef __cplusplus
160 }
161 #endif
162 
163 
164 #endif /* _COAP_TIME_H_ */
static void coap_clock_init_impl(void)
Definition: coap_time.h:109
static void coap_ticks_impl(coap_tick_t *t)
Definition: coap_time.h:126
static 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:153
static 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:144