1 /* 2 * coap_time.h -- Clock Handling 3 * 4 * Copyright (C) 2010-2019 Olaf Bergmann <bergmann@tzi.org> 5 * 6 * SPDX-License-Identifier: BSD-2-Clause 7 * 8 * This file is part of the CoAP library libcoap. Please see README for terms 9 * of use. 10 */ 11 12 /** 13 * @file coap_time.h 14 * @brief Clock Handling 15 */ 16 17 #ifndef COAP_TIME_H_ 18 #define COAP_TIME_H_ 19 20 /** 21 * @defgroup clock Clock Handling 22 * Default implementation of internal clock. 23 * @{ 24 */ 25 26 #if defined(WITH_LWIP) 27 28 #include <stdint.h> 29 #include <lwip/sys.h> 30 31 /* lwIP provides ms in sys_now */ 32 #define COAP_TICKS_PER_SECOND 1000 33 34 typedef uint32_t coap_tick_t; 35 typedef uint32_t coap_time_t; 36 typedef int32_t coap_tick_diff_t; 37 coap_ticks_impl(coap_tick_t * t)38COAP_STATIC_INLINE void coap_ticks_impl(coap_tick_t *t) { 39 *t = sys_now(); 40 } 41 coap_clock_init_impl(void)42COAP_STATIC_INLINE void coap_clock_init_impl(void) { 43 } 44 45 #define coap_clock_init coap_clock_init_impl 46 #define coap_ticks coap_ticks_impl 47 coap_ticks_to_rt(coap_tick_t t)48COAP_STATIC_INLINE coap_time_t coap_ticks_to_rt(coap_tick_t t) { 49 return t / COAP_TICKS_PER_SECOND; 50 } 51 coap_ticks_to_rt_us(coap_tick_t t)52COAP_STATIC_INLINE uint64_t coap_ticks_to_rt_us(coap_tick_t t) { 53 return (uint64_t)t * 1000000 / COAP_TICKS_PER_SECOND; 54 } 55 56 #elif defined(WITH_CONTIKI) 57 58 #include "clock.h" 59 60 typedef clock_time_t coap_tick_t; 61 typedef clock_time_t coap_time_t; 62 63 /** 64 * This data type is used to represent the difference between two clock_tick_t 65 * values. This data type must have the same size in memory as coap_tick_t to 66 * allow wrapping. 67 */ 68 typedef int coap_tick_diff_t; 69 70 #define COAP_TICKS_PER_SECOND CLOCK_SECOND 71 coap_clock_init(void)72COAP_STATIC_INLINE void coap_clock_init(void) { 73 clock_init(); 74 } 75 coap_ticks(coap_tick_t * t)76COAP_STATIC_INLINE void coap_ticks(coap_tick_t *t) { 77 *t = clock_time(); 78 } 79 coap_ticks_to_rt(coap_tick_t t)80COAP_STATIC_INLINE coap_time_t coap_ticks_to_rt(coap_tick_t t) { 81 return t / COAP_TICKS_PER_SECOND; 82 } 83 coap_ticks_to_rt_us(coap_tick_t t)84COAP_STATIC_INLINE uint64_t coap_ticks_to_rt_us(coap_tick_t t) { 85 return (uint64_t)t * 1000000 / COAP_TICKS_PER_SECOND; 86 } 87 88 #elif defined(RIOT_VERSION) 89 #include <xtimer.h> 90 91 #define COAP_TICKS_PER_SECOND (XTIMER_HZ) 92 93 typedef uint64_t coap_tick_t; 94 typedef int64_t coap_tick_diff_t; 95 typedef uint32_t coap_time_t; 96 coap_clock_init(void)97static inline void coap_clock_init(void) {} 98 coap_ticks(coap_tick_t * t)99static inline void coap_ticks(coap_tick_t *t) { 100 *t = xtimer_now_usec64(); 101 } 102 coap_ticks_to_rt(coap_tick_t t)103static inline coap_time_t coap_ticks_to_rt(coap_tick_t t) { 104 return t / 1000000UL; 105 } 106 coap_ticks_to_rt_us(coap_tick_t t)107static inline uint64_t coap_ticks_to_rt_us(coap_tick_t t) { 108 return t; 109 } 110 coap_ticks_from_rt_us(uint64_t t)111static inline coap_tick_t coap_ticks_from_rt_us(uint64_t t) { 112 return t / 1000000UL; 113 } 114 #else /* !WITH_LWIP && !WITH_CONTIKI && !RIOT_VERSION */ 115 116 #include <stdint.h> 117 118 /** 119 * This data type represents internal timer ticks with COAP_TICKS_PER_SECOND 120 * resolution. 121 */ 122 typedef uint64_t coap_tick_t; 123 124 /** 125 * CoAP time in seconds since epoch. 126 */ 127 typedef time_t coap_time_t; 128 129 /** 130 * This data type is used to represent the difference between two clock_tick_t 131 * values. This data type must have the same size in memory as coap_tick_t to 132 * allow wrapping. 133 */ 134 typedef int64_t coap_tick_diff_t; 135 136 /** Use ms resolution on POSIX systems */ 137 #define COAP_TICKS_PER_SECOND ((coap_tick_t)(1000U)) 138 139 /** 140 * Initializes the internal clock. 141 */ 142 void coap_clock_init(void); 143 144 /** 145 * Sets @p t to the internal time with COAP_TICKS_PER_SECOND resolution. 146 */ 147 void coap_ticks(coap_tick_t *t); 148 149 /** 150 * Helper function that converts coap ticks to wallclock time. On POSIX, this 151 * function returns the number of seconds since the epoch. On other systems, it 152 * may be the calculated number of seconds since last reboot or so. 153 * 154 * @param t Internal system ticks. 155 * 156 * @return The number of seconds that has passed since a specific reference 157 * point (seconds since epoch on POSIX). 158 */ 159 coap_time_t coap_ticks_to_rt(coap_tick_t t); 160 161 /** 162 * Helper function that converts coap ticks to POSIX wallclock time in us. 163 * 164 * @param t Internal system ticks. 165 * 166 * @return The number of seconds that has passed since a specific reference 167 * point (seconds since epoch on POSIX). 168 */ 169 uint64_t coap_ticks_to_rt_us(coap_tick_t t); 170 171 /** 172 * Helper function that converts POSIX wallclock time in us to coap ticks. 173 * 174 * @param t POSIX time is us 175 * 176 * @return coap ticks 177 */ 178 coap_tick_t coap_ticks_from_rt_us(uint64_t t); 179 #endif 180 181 /** 182 * Returns @c 1 if and only if @p a is less than @p b where less is defined on a 183 * signed data type. 184 */ coap_time_lt(coap_tick_t a,coap_tick_t b)185COAP_STATIC_INLINE int coap_time_lt(coap_tick_t a, coap_tick_t b) { 186 return ((coap_tick_diff_t)(a - b)) < 0; 187 } 188 189 /** 190 * Returns @c 1 if and only if @p a is less than or equal @p b where less is 191 * defined on a signed data type. 192 */ coap_time_le(coap_tick_t a,coap_tick_t b)193COAP_STATIC_INLINE int coap_time_le(coap_tick_t a, coap_tick_t b) { 194 return a == b || coap_time_lt(a,b); 195 } 196 197 /** @} */ 198 199 #endif /* COAP_TIME_H_ */ 200