libcoap  4.0.3
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
coap_time.h
Go to the documentation of this file.
1 /* coap_time.h -- Clock Handling
2  *
3  * Copyright (C) 2010,2011 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 #include "config.h"
18 
26 #ifdef WITH_CONTIKI
27 #include "clock.h"
28 
29 typedef clock_time_t coap_tick_t;
30 
31 #define COAP_TICKS_PER_SECOND CLOCK_SECOND
32 
34 extern clock_time_t clock_offset;
35 
36 static inline void
37 contiki_clock_init_impl(void) {
38  clock_init();
39  clock_offset = clock_time();
40 }
41 
42 #define coap_clock_init contiki_clock_init_impl
43 
44 static inline void
45 contiki_ticks_impl(coap_tick_t *t) {
46  *t = clock_time();
47 }
48 
49 #define coap_ticks contiki_ticks_impl
50 
51 #else /* WITH_CONTIKI */
52 typedef unsigned int coap_tick_t;
53 
54 #define COAP_TICKS_PER_SECOND 1024
55 
57 extern time_t clock_offset;
58 #endif
59 
60 #ifndef coap_clock_init
61 static inline void
63 #ifdef HAVE_TIME_H
64  clock_offset = time(NULL);
65 #else
66 # ifdef __GNUC__
67  /* Issue a warning when using gcc. Other prepropressors do
68  * not seem to have a similar feature. */
69 # warning "cannot initialize clock"
70 # endif
71  clock_offset = 0;
72 #endif
73 }
74 #define coap_clock_init coap_clock_init_impl
75 #endif /* coap_clock_init */
76 
77 #ifndef coap_ticks
78 static inline void
80 #ifdef HAVE_SYS_TIME_H
81  struct timeval tv;
82  gettimeofday(&tv, NULL);
83  *t = (tv.tv_sec - clock_offset) * COAP_TICKS_PER_SECOND
84  + (tv.tv_usec * COAP_TICKS_PER_SECOND / 1000000);
85 #else
86 #error "clock not implemented"
87 #endif
88 }
89 #define coap_ticks coap_ticks_impl
90 #endif /* coap_ticks */
91 
94 #endif /* _COAP_TIME_H_ */