mirror of
https://github.com/obgm/libcoap.git
synced 2026-11-12 03:40:39 +03:00
If we correctly determine the presence of the sys/time.h and time.h files using the HAVE_TIME_H and HAVE_SYS_TIME_H macros, then the LWIP compilation will result in a duplicate function error. Perhaps previous versions of LWIP did not have these files, in which case everything would have worked correctly. These changes add the necessary ifdefs and keep the optimal timer function for LWIP. Adds missed #include <time.h> lines to examples and documentation. Adds the detection of the sys/select.h header to the CMake and autoconf.
239 lines
5.2 KiB
C
239 lines
5.2 KiB
C
/*
|
|
* coap_time.h -- Clock Handling
|
|
*
|
|
* Copyright (C) 2010-2025 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>
|
|
#else /* !WITH_LWIP && !WITH_CONTIKI && !RIOT_VERSION */
|
|
#include <stdint.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 */
|
|
#define COAP_MAX_DELAY_TICKS (24 * 60 * 60 * COAP_TICKS_PER_SECOND)
|
|
|
|
/** @} */
|
|
|
|
#ifdef __cplusplus
|
|
}
|
|
#endif
|
|
|
|
#endif /* COAP_TIME_H_ */
|