coap_time.h
Go to the documentation of this file.
/*
* coap_time.h -- Clock Handling
*
* Copyright (C) 2010-2019 Olaf Bergmann <bergmann@tzi.org>
*
* This file is part of the CoAP library libcoap. Please see README for terms
* of use.
*/
#ifndef COAP_TIME_H_
#define COAP_TIME_H_
#if defined(WITH_LWIP)
#include <stdint.h>
#include <lwip/sys.h>
/* lwIP provides ms in sys_now */
#define COAP_TICKS_PER_SECOND 1000
typedef uint32_t coap_tick_t;
typedef uint32_t coap_time_t;
typedef int32_t coap_tick_diff_t;
COAP_STATIC_INLINE void coap_ticks_impl(coap_tick_t *t) {
*t = sys_now();
}
COAP_STATIC_INLINE void coap_clock_init_impl(void) {
}
#define coap_clock_init coap_clock_init_impl
#define coap_ticks coap_ticks_impl
COAP_STATIC_INLINE coap_time_t coap_ticks_to_rt(coap_tick_t t) {
return t / COAP_TICKS_PER_SECOND;
}
COAP_STATIC_INLINE uint64_t coap_ticks_to_rt_us(coap_tick_t t) {
return (uint64_t)t * 1000000 / COAP_TICKS_PER_SECOND;
}
#elif defined(WITH_CONTIKI)
#include "clock.h"
typedef clock_time_t coap_tick_t;
typedef clock_time_t coap_time_t;
typedef int coap_tick_diff_t;
#define COAP_TICKS_PER_SECOND CLOCK_SECOND
COAP_STATIC_INLINE void coap_clock_init(void) {
clock_init();
}
COAP_STATIC_INLINE void coap_ticks(coap_tick_t *t) {
*t = clock_time();
}
COAP_STATIC_INLINE coap_time_t coap_ticks_to_rt(coap_tick_t t) {
return t / COAP_TICKS_PER_SECOND;
}
COAP_STATIC_INLINE uint64_t coap_ticks_to_rt_us(coap_tick_t t) {
return (uint64_t)t * 1000000 / COAP_TICKS_PER_SECOND;
}
#else
#include <stdint.h>
typedef uint64_t coap_tick_t;
typedef time_t coap_time_t;
typedef int64_t coap_tick_diff_t;
#define COAP_TICKS_PER_SECOND ((coap_tick_t)(1000U))
void coap_clock_init(void);
void coap_ticks(coap_tick_t *t);
coap_time_t coap_ticks_to_rt(coap_tick_t t);
uint64_t coap_ticks_to_rt_us(coap_tick_t t);
coap_tick_t coap_ticks_from_rt_us(uint64_t t);
#endif
COAP_STATIC_INLINE int coap_time_lt(coap_tick_t a, coap_tick_t b) {
return ((coap_tick_diff_t)(a - b)) < 0;
}
COAP_STATIC_INLINE int coap_time_le(coap_tick_t a, coap_tick_t b) {
return a == b || coap_time_lt(a,b);
}
#endif /* COAP_TIME_H_ */
coap_ticks_from_rt_us
coap_tick_t coap_ticks_from_rt_us(uint64_t t)
Helper function that converts POSIX wallclock time in us to coap ticks.
coap_tick_diff_t
int64_t coap_tick_diff_t
This data type is used to represent the difference between two clock_tick_t values.
Definition: coap_time.h:105
coap_ticks
void coap_ticks(coap_tick_t *t)
Sets t to the internal time with COAP_TICKS_PER_SECOND resolution.
coap_time_t
time_t coap_time_t
CoAP time in seconds since epoch.
Definition: coap_time.h:98
coap_clock_init
void coap_clock_init(void)
Initializes the internal clock.
coap_tick_t
uint64_t coap_tick_t
This data type represents internal timer ticks with COAP_TICKS_PER_SECOND resolution.
Definition: coap_time.h:93
coap_time_lt
COAP_STATIC_INLINE 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:156
coap_ticks_to_rt
coap_time_t coap_ticks_to_rt(coap_tick_t t)
Helper function that converts coap ticks to wallclock time.
coap_time_le
COAP_STATIC_INLINE 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:164
COAP_TICKS_PER_SECOND
#define COAP_TICKS_PER_SECOND
Use ms resolution on POSIX systems.
Definition: coap_time.h:108
coap_ticks_to_rt_us
uint64_t coap_ticks_to_rt_us(coap_tick_t t)
Helper function that converts coap ticks to POSIX wallclock time in us.
COAP_STATIC_INLINE
#define COAP_STATIC_INLINE
Definition: libcoap.h:38
uint32_t
unsigned int uint32_t
Definition: uthash.h:78