-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcoap_time.h
164 lines (129 loc) · 3.32 KB
/
coap_time.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/* coap_time.h -- Clock Handling
*
* Copyright (C) 2010--2013 Olaf Bergmann <[email protected]>
*
* This file is part of the CoAP library libcoap. Please see
* README for terms of use.
*/
/**
* @file coap_time.h
* @brief Clock Handling
*/
#ifndef _COAP_TIME_H_
#define _COAP_TIME_H_
/*
** Make sure we can call this stuff from C++.
*/
#ifdef __cplusplus
extern "C" {
#endif
#include "config.h"
/**
* @defgroup clock Clock Handling
* Default implementation of internal clock. You should redefine this if
* you do not have time() and gettimeofday().
* @{
*/
#ifdef WITH_LWIP
#include <stdint.h>
#include <lwip/sys.h>
/* lwIP provides ms in sys_now */
#define COAP_TICKS_PER_SECOND 1000
typedef uint32_t coap_tick_t;
static inline void coap_ticks_impl(coap_tick_t *t)
{
*t = sys_now();
}
static inline void coap_clock_init_impl(void)
{
}
#define coap_clock_init coap_clock_init_impl
#define coap_ticks coap_ticks_impl
#endif
#ifdef WITH_CONTIKI
#include "clock.h"
typedef clock_time_t coap_tick_t;
/**
* This data type is used to represent the difference between two
* clock_tick_t values. This data type must have the same size in
* memory as coap_tick_t to allow wrapping.
*/
typedef int coap_tick_diff_t;
#define COAP_TICKS_PER_SECOND CLOCK_SECOND
/** Set at startup to initialize the internal clock (time in seconds). */
extern clock_time_t clock_offset;
static inline void
contiki_clock_init_impl(void) {
clock_init();
clock_offset = clock_time();
}
#define coap_clock_init contiki_clock_init_impl
static inline void
contiki_ticks_impl(coap_tick_t *t) {
*t = clock_time();
}
#define coap_ticks contiki_ticks_impl
#endif /* WITH_CONTIKI */
#ifdef WITH_POSIX
typedef unsigned int coap_tick_t;
/**
* This data type is used to represent the difference between two
* clock_tick_t values. This data type must have the same size in
* memory as coap_tick_t to allow wrapping.
*/
typedef int coap_tick_diff_t;
#define COAP_TICKS_PER_SECOND 1024
/** Set at startup to initialize the internal clock (time in seconds). */
extern time_t clock_offset;
#endif /* WITH_POSIX */
#ifndef coap_clock_init
static inline void
coap_clock_init_impl(void) {
#ifdef HAVE_TIME_H
clock_offset = time(NULL);
#else
# ifdef __GNUC__
/* Issue a warning when using gcc. Other prepropressors do
* not seem to have a similar feature. */
# warning "cannot initialize clock"
# endif
clock_offset = 0;
#endif
}
#define coap_clock_init coap_clock_init_impl
#endif /* coap_clock_init */
#ifndef coap_ticks
static inline void
coap_ticks_impl(coap_tick_t *t) {
#ifdef HAVE_SYS_TIME_H
struct timeval tv;
gettimeofday(&tv, NULL);
*t = (tv.tv_sec - clock_offset) * COAP_TICKS_PER_SECOND
+ (tv.tv_usec * COAP_TICKS_PER_SECOND / 1000000);
#else
#error "clock not implemented"
#endif
}
#define coap_ticks coap_ticks_impl
#endif /* coap_ticks */
/**
* Returns @c 1 if and only if @p a is less than @p b where less is
* defined on a signed data type.
*/
static inline
int coap_time_lt(coap_tick_t a, coap_tick_t b) {
return ((coap_tick_diff_t)(a - b)) < 0;
}
/**
* Returns @c 1 if and only if @p a is less than or equal @p b where
* less is defined on a signed data type.
*/
static inline
int coap_time_le(coap_tick_t a, coap_tick_t b) {
return a == b || coap_time_lt(a,b);
}
/** @} */
#ifdef __cplusplus
}
#endif
#endif /* _COAP_TIME_H_ */