Files
libcoap/include/coap3/coap_time.h
T
Jon Shallow 152f6a8a58 Includes: Remove unnecessary include files
Include common system header files in coap_internal.h based on HAVE_*H
definitions.

Remove these system header files from include/*/*.h.

Add in system header files where appropriate in various apps.
2026-02-02 10:09:17 +00:00

241 lines
5.3 KiB
C

/*
* coap_time.h -- Clock Handling
*
* Copyright (C) 2010-2026 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.
*/
/**
* @file coap_time.h
* @brief Clock Handling
*/
#ifndef COAP_TIME_H_
#define COAP_TIME_H_
/**
* @ingroup application_api
* @defgroup clock Clock Handling
* API for internal clock assess
* @{
*/
#if defined(WITH_LWIP)
#include <stdint.h>
#include <lwip/sys.h>
#elif defined(WITH_CONTIKI)
#include "clock.h"
#elif defined(RIOT_VERSION)
#include <xtimer.h>
#endif /* !WITH_LWIP && !WITH_CONTIKI && !RIOT_VERSION */
#ifdef __cplusplus
extern "C" {
#endif
#if defined(WITH_LWIP)
/* 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)
typedef clock_time_t coap_tick_t;
typedef clock_time_t coap_time_t;
/**
* This data type is used to represent the difference between two clock_tick_t
* values. This data type must have the same size in memory as coap_tick_t to
* allow wrapping.
*/
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)
#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;
COAP_STATIC_INLINE void
coap_clock_init(void) {}
COAP_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 */
}
COAP_STATIC_INLINE coap_time_t
coap_ticks_to_rt(coap_tick_t t) {
return t / 1000000UL;
}
COAP_STATIC_INLINE uint64_t
coap_ticks_to_rt_us(coap_tick_t t) {
return t;
}
COAP_STATIC_INLINE coap_tick_t
coap_ticks_from_rt_us(uint64_t t) {
return t / 1000000UL;
}
#else /* !WITH_LWIP && !WITH_CONTIKI && !RIOT_VERSION */
/**
* This data type represents internal timer ticks with COAP_TICKS_PER_SECOND
* resolution.
*/
typedef uint64_t coap_tick_t;
/**
* CoAP time in seconds since epoch.
*/
typedef time_t coap_time_t;
/**
* This data type is used to represent the difference between two clock_tick_t
* values. This data type must have the same size in memory as coap_tick_t to
* allow wrapping.
*/
typedef int64_t coap_tick_diff_t;
/** Use ms resolution on POSIX systems */
#define COAP_TICKS_PER_SECOND ((coap_tick_t)(1000U))
/**
* Initializes the internal clock.
*/
void coap_clock_init(void);
/**
* Sets @p t to the internal time with COAP_TICKS_PER_SECOND resolution.
*/
void coap_ticks(coap_tick_t *t);
/**
* Helper function that converts coap ticks to wallclock time. On POSIX, this
* function returns the number of seconds since the epoch. On other systems, it
* may be the calculated number of seconds since last reboot or so.
*
* @param t Internal system ticks.
*
* @return The number of seconds that has passed since a specific reference
* point (seconds since epoch on POSIX).
*/
coap_time_t coap_ticks_to_rt(coap_tick_t t);
/**
* Helper function that converts coap ticks to POSIX wallclock time in us.
*
* @param t Internal system ticks.
*
* @return The number of seconds that has passed since a specific reference
* point (seconds since epoch on POSIX).
*/
uint64_t coap_ticks_to_rt_us(coap_tick_t t);
/**
* Helper function that converts POSIX wallclock time in us to coap ticks.
*
* @param t POSIX time is us
*
* @return coap ticks
*/
coap_tick_t coap_ticks_from_rt_us(uint64_t t);
#endif /* !WITH_LWIP && !WITH_CONTIKI && !RIOT_VERSION */
/**
* Returns @c 1 if and only if @p a is less than @p b where less is defined on a
* signed data type.
*/
COAP_STATIC_INLINE int
coap_time_lt(coap_tick_t a, coap_tick_t b) {
return ((coap_tick_diff_t)(a - b)) < 0;
}
/**
* Returns @c 1 if and only if @p a is less than or equal @p b where less is
* defined on a signed data type.
*/
COAP_STATIC_INLINE int
coap_time_le(coap_tick_t a, coap_tick_t b) {
return a == b || coap_time_lt(a,b);
}
/* Can delay up to 24 hrs before next wakeup (coap_tick_t can be 4 bytes or int64 */
#if defined(RIOT_VERSION)
#define COAP_MAX_DELAY_TICKS (24UL * 60 * 60 * COAP_TICKS_PER_SECOND)
#else /* ! RIOT_VERSION */
#define COAP_MAX_DELAY_TICKS (24 * 60 * 60 * COAP_TICKS_PER_SECOND)
#endif /* ! RIOT_VERSION */
/** @} */
#ifdef __cplusplus
}
#endif
#endif /* COAP_TIME_H_ */