1 /* 2 * coap_time.h -- Clock Handling 3 * 4 * Copyright (C) 2010-2019 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 #if defined(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)36COAP_STATIC_INLINE void coap_ticks_impl(coap_tick_t *t) { 37 *t = sys_now(); 38 } 39 coap_clock_init_impl(void)40COAP_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)46COAP_STATIC_INLINE coap_time_t coap_ticks_to_rt(coap_tick_t t) { 47 return t / COAP_TICKS_PER_SECOND; 48 } 49 coap_ticks_to_rt_us(coap_tick_t t)50COAP_STATIC_INLINE uint64_t coap_ticks_to_rt_us(coap_tick_t t) { 51 return (uint64_t)t * 1000000 / COAP_TICKS_PER_SECOND; 52 } 53 54 #elif defined(WITH_CONTIKI) 55 56 #include "clock.h" 57 58 typedef clock_time_t coap_tick_t; 59 typedef clock_time_t coap_time_t; 60 61 /** 62 * This data type is used to represent the difference between two clock_tick_t 63 * values. This data type must have the same size in memory as coap_tick_t to 64 * allow wrapping. 65 */ 66 typedef int coap_tick_diff_t; 67 68 #define COAP_TICKS_PER_SECOND CLOCK_SECOND 69 coap_clock_init(void)70COAP_STATIC_INLINE void coap_clock_init(void) { 71 clock_init(); 72 } 73 coap_ticks(coap_tick_t * t)74COAP_STATIC_INLINE void coap_ticks(coap_tick_t *t) { 75 *t = clock_time(); 76 } 77 coap_ticks_to_rt(coap_tick_t t)78COAP_STATIC_INLINE coap_time_t coap_ticks_to_rt(coap_tick_t t) { 79 return t / COAP_TICKS_PER_SECOND; 80 } 81 coap_ticks_to_rt_us(coap_tick_t t)82COAP_STATIC_INLINE uint64_t coap_ticks_to_rt_us(coap_tick_t t) { 83 return (uint64_t)t * 1000000 / COAP_TICKS_PER_SECOND; 84 } 85 86 #else 87 #include <stdint.h> 88 89 /** 90 * This data type represents internal timer ticks with COAP_TICKS_PER_SECOND 91 * resolution. 92 */ 93 typedef uint64_t coap_tick_t; 94 95 /** 96 * CoAP time in seconds since epoch. 97 */ 98 typedef time_t coap_time_t; 99 100 /** 101 * This data type is used to represent the difference between two clock_tick_t 102 * values. This data type must have the same size in memory as coap_tick_t to 103 * allow wrapping. 104 */ 105 typedef int64_t coap_tick_diff_t; 106 107 /** Use ms resolution on POSIX systems */ 108 #define COAP_TICKS_PER_SECOND ((coap_tick_t)(1000U)) 109 110 /** 111 * Initializes the internal clock. 112 */ 113 void coap_clock_init(void); 114 115 /** 116 * Sets @p t to the internal time with COAP_TICKS_PER_SECOND resolution. 117 */ 118 void coap_ticks(coap_tick_t *t); 119 120 /** 121 * Helper function that converts coap ticks to wallclock time. On POSIX, this 122 * function returns the number of seconds since the epoch. On other systems, it 123 * may be the calculated number of seconds since last reboot or so. 124 * 125 * @param t Internal system ticks. 126 * 127 * @return The number of seconds that has passed since a specific reference 128 * point (seconds since epoch on POSIX). 129 */ 130 coap_time_t coap_ticks_to_rt(coap_tick_t t); 131 132 /** 133 * Helper function that converts coap ticks to POSIX wallclock time in us. 134 * 135 * @param t Internal system ticks. 136 * 137 * @return The number of seconds that has passed since a specific reference 138 * point (seconds since epoch on POSIX). 139 */ 140 uint64_t coap_ticks_to_rt_us(coap_tick_t t); 141 142 /** 143 * Helper function that converts POSIX wallclock time in us to coap ticks. 144 * 145 * @param t POSIX time is us 146 * 147 * @return coap ticks 148 */ 149 coap_tick_t coap_ticks_from_rt_us(uint64_t t); 150 #endif 151 152 /** 153 * Returns @c 1 if and only if @p a is less than @p b where less is defined on a 154 * signed data type. 155 */ coap_time_lt(coap_tick_t a,coap_tick_t b)156COAP_STATIC_INLINE int coap_time_lt(coap_tick_t a, coap_tick_t b) { 157 return ((coap_tick_diff_t)(a - b)) < 0; 158 } 159 160 /** 161 * Returns @c 1 if and only if @p a is less than or equal @p b where less is 162 * defined on a signed data type. 163 */ coap_time_le(coap_tick_t a,coap_tick_t b)164COAP_STATIC_INLINE int coap_time_le(coap_tick_t a, coap_tick_t b) { 165 return a == b || coap_time_lt(a,b); 166 } 167 168 /** @} */ 169 170 #endif /* COAP_TIME_H_ */ 171