libcoap  4.1.2
coap_time.h
Go to the documentation of this file.
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 
15 #ifndef _COAP_TIME_H_
16 #define _COAP_TIME_H_
17 
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 
36 static inline void coap_ticks_impl(coap_tick_t *t) {
37  *t = sys_now();
38 }
39 
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 
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 
62 typedef int coap_tick_diff_t;
63 
64 #define COAP_TICKS_PER_SECOND CLOCK_SECOND
65 
66 static inline void coap_clock_init(void) {
67  clock_init();
68 }
69 
70 static inline void coap_ticks(coap_tick_t *t) {
71  *t = clock_time();
72 }
73 
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 
84 typedef unsigned long coap_tick_t;
85 
89 typedef time_t coap_time_t;
90 
96 typedef long coap_tick_diff_t;
97 
99 #define COAP_TICKS_PER_SECOND 1000
100 
104 void coap_clock_init(void);
105 
109 void coap_ticks(coap_tick_t *t);
110 
122 #endif /* WITH_POSIX */
123 
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 
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 
142 #endif /* _COAP_TIME_H_ */
void coap_clock_init(void)
Initializes the internal clock.
Definition: coap_time.c:27
coap_time_t coap_ticks_to_rt(coap_tick_t t)
Helper function that converts coap ticks to wallclock time.
Definition: coap_time.c:82
unsigned long coap_tick_t
This data type represents internal timer ticks with COAP_TICKS_PER_SECOND resolution.
Definition: coap_time.h:84
#define COAP_TICKS_PER_SECOND
Use ms resolution on POSIX systems.
Definition: coap_time.h:99
long coap_tick_diff_t
This data type is used to represent the difference between two clock_tick_t values.
Definition: coap_time.h:96
time_t coap_time_t
CoAP time in seconds since epoch.
Definition: coap_time.h:89
void coap_ticks(coap_tick_t *t)
Sets t to the internal time with COAP_TICKS_PER_SECOND resolution.
Definition: coap_time.c:49
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:136
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:128