1 /*
2  * coap_time.h -- Clock Handling
3  *
4  * Copyright (C) 2010-2013 Olaf Bergmann <bergmann@tzi.org>
5  *
6  * This file is part of the CoAP library libcoap. Please see README for terms
7  * of use.
8  */
9 
10 /**
11  * @file coap_time.h
12  * @brief Clock Handling
13  */
14 
15 #ifndef _COAP_TIME_H_
16 #define _COAP_TIME_H_
17 
18 /**
19  * @defgroup clock Clock Handling
20  * Default implementation of internal clock.
21  * @{
22  */
23 
24 #ifdef WITH_LWIP
25 
26 #include <stdint.h>
27 #include <lwip/sys.h>
28 
29 /* lwIP provides ms in sys_now */
30 #define COAP_TICKS_PER_SECOND 1000
31 
32 typedef uint32_t coap_tick_t;
33 typedef uint32_t coap_time_t;
34 typedef int32_t coap_tick_diff_t;
35 
coap_ticks_impl(coap_tick_t * t)36 static inline void coap_ticks_impl(coap_tick_t *t) {
37   *t = sys_now();
38 }
39 
coap_clock_init_impl(void)40 static inline void coap_clock_init_impl(void) {
41 }
42 
43 #define coap_clock_init coap_clock_init_impl
44 #define coap_ticks coap_ticks_impl
45 
coap_ticks_to_rt(coap_tick_t t)46 static inline coap_time_t coap_ticks_to_rt(coap_tick_t t) {
47   return t / COAP_TICKS_PER_SECOND;
48 }
49 #endif
50 
51 #ifdef WITH_CONTIKI
52 #include "clock.h"
53 
54 typedef clock_time_t coap_tick_t;
55 typedef clock_time_t coap_time_t;
56 
57 /**
58  * This data type is used to represent the difference between two clock_tick_t
59  * values. This data type must have the same size in memory as coap_tick_t to
60  * allow wrapping.
61  */
62 typedef int coap_tick_diff_t;
63 
64 #define COAP_TICKS_PER_SECOND CLOCK_SECOND
65 
coap_clock_init(void)66 static inline void coap_clock_init(void) {
67   clock_init();
68 }
69 
coap_ticks(coap_tick_t * t)70 static inline void coap_ticks(coap_tick_t *t) {
71   *t = clock_time();
72 }
73 
coap_ticks_to_rt(coap_tick_t t)74 static inline coap_time_t coap_ticks_to_rt(coap_tick_t t) {
75   return t / COAP_TICKS_PER_SECOND;
76 }
77 #endif /* WITH_CONTIKI */
78 
79 #ifdef WITH_POSIX
80 /**
81  * This data type represents internal timer ticks with COAP_TICKS_PER_SECOND
82  * resolution.
83  */
84 typedef unsigned long coap_tick_t;
85 
86 /**
87  * CoAP time in seconds since epoch.
88  */
89 typedef time_t coap_time_t;
90 
91 /**
92  * This data type is used to represent the difference between two clock_tick_t
93  * values. This data type must have the same size in memory as coap_tick_t to
94  * allow wrapping.
95  */
96 typedef long coap_tick_diff_t;
97 
98 /** Use ms resolution on POSIX systems */
99 #define COAP_TICKS_PER_SECOND 1000
100 
101 /**
102  * Initializes the internal clock.
103  */
104 void coap_clock_init(void);
105 
106 /**
107  * Sets @p t to the internal time with COAP_TICKS_PER_SECOND resolution.
108  */
109 void coap_ticks(coap_tick_t *t);
110 
111 /**
112  * Helper function that converts coap ticks to wallclock time. On POSIX, this
113  * function returns the number of seconds since the epoch. On other systems, it
114  * may be the calculated number of seconds since last reboot or so.
115  *
116  * @param t Internal system ticks.
117  *
118  * @return  The number of seconds that has passed since a specific reference
119  *          point (seconds since epoch on POSIX).
120  */
121 coap_time_t coap_ticks_to_rt(coap_tick_t t);
122 #endif /* WITH_POSIX */
123 
124 /**
125  * Returns @c 1 if and only if @p a is less than @p b where less is defined on a
126  * signed data type.
127  */
coap_time_lt(coap_tick_t a,coap_tick_t b)128 static inline int coap_time_lt(coap_tick_t a, coap_tick_t b) {
129   return ((coap_tick_diff_t)(a - b)) < 0;
130 }
131 
132 /**
133  * Returns @c 1 if and only if @p a is less than or equal @p b where less is
134  * defined on a signed data type.
135  */
coap_time_le(coap_tick_t a,coap_tick_t b)136 static inline int coap_time_le(coap_tick_t a, coap_tick_t b) {
137   return a == b || coap_time_lt(a,b);
138 }
139 
140 /** @} */
141 
142 #endif /* _COAP_TIME_H_ */
143