Loading...
Searching...
No Matches
coap_time.h
Go to the documentation of this file.
/*
* coap_time.h -- Clock Handling
*
* Copyright (C) 2010-2024 Olaf Bergmann <bergmann@tzi.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*
* 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) {
}
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;
}
#elif defined(RIOT_VERSION)
#include <xtimer.h>
#ifdef XTIMER_HZ
#define COAP_TICKS_PER_SECOND (XTIMER_HZ)
#else /* XTIMER_HZ */
#define COAP_TICKS_PER_SECOND (1000000U)
#endif /* XTIMER_HZ */
typedef uint64_t coap_tick_t;
typedef int64_t coap_tick_diff_t;
typedef uint32_t coap_time_t;
static inline void
coap_clock_init(void) {}
static inline void
coap_ticks(coap_tick_t *t) {
#ifdef MODULE_ZTIMER64_XTIMER_COMPAT
*t = xtimer_now_usec64();
#else /* MODULE_ZTIMER64_XTIMER_COMPAT */
*t = xtimer_now_usec();
#endif /* MODULE_ZTIMER64_XTIMER_COMPAT */
}
static inline coap_time_t
coap_ticks_to_rt(coap_tick_t t) {
return t / 1000000UL;
}
static inline uint64_t
coap_ticks_to_rt_us(coap_tick_t t) {
return t;
}
static inline coap_tick_t
coap_ticks_from_rt_us(uint64_t t) {
return t / 1000000UL;
}
#else /* !WITH_LWIP && !WITH_CONTIKI && !RIOT_VERSION */
#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:155
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:148
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:143
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:207
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:216
COAP_TICKS_PER_SECOND
#define COAP_TICKS_PER_SECOND
Use ms resolution on POSIX systems.
Definition coap_time.h:158
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:53