1
0
mirror of https://github.com/obgm/libcoap.git synced 2025-10-14 02:19:34 +08:00

Fix HAVE_TIME_H and HAVE_SYS_TIME_H ifdefs

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.
This commit is contained in:
raspopov
2025-10-01 18:26:25 +03:00
committed by Jon Shallow
parent 5ca624642e
commit 245354f0ef
15 changed files with 47 additions and 28 deletions

View File

@@ -287,6 +287,7 @@ check_include_file(pthread.h HAVE_PTHREAD_H)
check_include_file(stdlib.h HAVE_STDINT_H)
check_include_file(stdint.h HAVE_STDLIB_H)
check_include_file(sys/ioctl.h HAVE_SYS_IOCTL_H)
check_include_file(sys/select.h HAVE_SYS_SELECT_H)
check_include_file(sys/socket.h HAVE_SYS_SOCKET_H)
check_include_file(sys/stat.h HAVE_SYS_STAT_H)
check_include_file(sys/time.h HAVE_SYS_TIME_H)

View File

@@ -124,6 +124,9 @@
/* Define to 1 if you have the <sys/ioctl.h> header file. */
#cmakedefine HAVE_SYS_IOCTL_H @HAVE_SYS_IOCTL_H@
/* Define to 1 if you have the <sys/select.h> header file. */
#cmakedefine HAVE_SYS_SELECT_H @HAVE_SYS_SELECT_H@
/* Define to 1 if you have the <sys/socket.h> header file. */
#cmakedefine HAVE_SYS_SOCKET_H @HAVE_SYS_SOCKET_H@

View File

@@ -1103,7 +1103,7 @@ fi
# Checks for header files.
AC_CHECK_HEADERS([assert.h arpa/inet.h limits.h netdb.h netinet/in.h \
pthread.h errno.h winsock2.h ws2tcpip.h signal.h \
stdlib.h string.h strings.h sys/socket.h sys/time.h \
stdlib.h string.h strings.h sys/socket.h sys/select.h sys/time.h \
time.h unistd.h sys/unistd.h sys/ioctl.h net/if.h ifaddrs.h])
# For epoll, need two headers (sys/epoll.h sys/timerfd.h), but set up one #define

View File

@@ -14,6 +14,7 @@
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <ctype.h>
#include <inttypes.h>
#include <sys/types.h>

View File

@@ -32,6 +32,7 @@
#include "contiki-net.h"
#include "coap3/coap.h"
#include <time.h>
static coap_context_t *coap_context;

View File

@@ -19,13 +19,10 @@
#include <stdlib.h>
#include <string.h>
#ifndef _WIN32
#ifdef HAVE_SYS_SELECT_H
#if !defined(WITH_LWIP) && !defined(WITH_CONTIKI) && !defined(RIOT_VERSION) && !defined(_WIN32)
#include <sys/select.h>
#endif /* HAVE_SYS_SELECT_H */
#include <sys/time.h>
#endif
#include <time.h>
#endif /* ! WITH_LWIP && ! WITH_CONTIKI && ! RIOT_VERSION && ! _WIN32 */
#ifdef WITH_LWIP
#include <lwip/ip_addr.h>
@@ -794,7 +791,7 @@ void *coap_context_get_app_data(const coap_context_t *context);
*/
COAP_API int coap_io_process(coap_context_t *ctx, uint32_t timeout_ms);
#if !defined(RIOT_VERSION) && !defined(WITH_CONTIKI)
#if !defined(WITH_LWIP) && !defined(RIOT_VERSION) && !defined(WITH_CONTIKI)
/**
* The main message processing loop with additional fds for internal select.
*
@@ -825,7 +822,7 @@ COAP_API int coap_io_process(coap_context_t *ctx, uint32_t timeout_ms);
COAP_API int coap_io_process_with_fds(coap_context_t *ctx, uint32_t timeout_ms,
int nfds, fd_set *readfds, fd_set *writefds,
fd_set *exceptfds);
#endif /* ! RIOT_VERSION && ! WITH_CONTIKI */
#endif /* ! WITH_LWIP && ! RIOT_VERSION && ! WITH_CONTIKI */
/**
* Check to see if there is any i/o pending for the @p context.

View File

@@ -25,7 +25,6 @@
*/
#if defined(WITH_LWIP)
#include <stdint.h>
#include <lwip/sys.h>
#elif defined(WITH_CONTIKI)
@@ -34,7 +33,7 @@
#include <xtimer.h>
#else /* !WITH_LWIP && !WITH_CONTIKI && !RIOT_VERSION */
#include <stdint.h>
#endif
#endif /* !WITH_LWIP && !WITH_CONTIKI && !RIOT_VERSION */
#ifdef __cplusplus
extern "C" {
@@ -116,10 +115,10 @@ typedef uint64_t coap_tick_t;
typedef int64_t coap_tick_diff_t;
typedef uint32_t coap_time_t;
static inline void
COAP_STATIC_INLINE void
coap_clock_init(void) {}
static inline void
COAP_STATIC_INLINE void
coap_ticks(coap_tick_t *t) {
#ifdef MODULE_ZTIMER64_XTIMER_COMPAT
*t = xtimer_now_usec64();
@@ -128,22 +127,22 @@ coap_ticks(coap_tick_t *t) {
#endif /* MODULE_ZTIMER64_XTIMER_COMPAT */
}
static inline coap_time_t
COAP_STATIC_INLINE coap_time_t
coap_ticks_to_rt(coap_tick_t t) {
return t / 1000000UL;
}
static inline uint64_t
COAP_STATIC_INLINE uint64_t
coap_ticks_to_rt_us(coap_tick_t t) {
return t;
}
static inline coap_tick_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 */
#else /* !WITH_LWIP && !WITH_CONTIKI && !RIOT_VERSION */
/**
* This data type represents internal timer ticks with COAP_TICKS_PER_SECOND
@@ -206,7 +205,8 @@ uint64_t coap_ticks_to_rt_us(coap_tick_t t);
* @return coap ticks
*/
coap_tick_t coap_ticks_from_rt_us(uint64_t t);
#endif
#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

View File

@@ -475,6 +475,7 @@ main(int argc, char *argv[]) {
#include <coap@LIBCOAP_API_VERSION@/coap.h>
#include <stdio.h>
#include <time.h>
static void
hnd_get_time(coap_resource_t *resource, coap_session_t *session,

View File

@@ -452,6 +452,7 @@ EXAMPLES
#include <coap@LIBCOAP_API_VERSION@/coap.h>
#include <stdio.h>
#include <time.h>
static void
hnd_get_time(coap_resource_t *resource, coap_session_t *session,

View File

@@ -105,6 +105,7 @@ EXAMPLES
#include <inttypes.h>
#include <stdio.h>
#include <signal.h>
#include <time.h>
#include <coap@LIBCOAP_API_VERSION@/coap.h>
#include <coap3/coap_defines.h>

View File

@@ -163,6 +163,8 @@ EXAMPLES
#include <coap@LIBCOAP_API_VERSION@/coap.h>
#include <stdio.h>
#include <time.h>
#include <sys/time.h>
coap_resource_t *time_resource = NULL;

View File

@@ -682,6 +682,7 @@ error:
#include <coap@LIBCOAP_API_VERSION@/coap.h>
#include <stdio.h>
#include <time.h>
static void
hnd_get_time(coap_resource_t *resource, coap_session_t *session,

View File

@@ -283,6 +283,8 @@ EXAMPLES
#include <coap@LIBCOAP_API_VERSION@/coap.h>
#include <stdio.h>
#include <time.h>
#include <sys/time.h>
coap_resource_t *time_resource = NULL;

View File

@@ -54,6 +54,13 @@
#define IN_MULTICAST(Address) (0)
#endif /* RIOT_VERSION */
#if defined(_WIN32)
#include <iphlpapi.h>
#if !defined(__MINGW32__)
#pragma comment(lib, "iphlpapi.lib")
#endif /* ! __MINGW32__ */
#endif /* _WIN32 */
uint16_t
coap_address_get_port(const coap_address_t *addr) {
assert(addr != NULL);
@@ -265,11 +272,6 @@ coap_is_bcast(const coap_address_t *a) {
#if defined(_WIN32)
#include <iphlpapi.h>
#if !defined(__MINGW32__)
#pragma comment(lib, "iphlpapi.lib")
#endif /* ! __MINGW32__ */
int i;
coap_tick_t now;

View File

@@ -15,18 +15,24 @@
#include "coap3/coap_libcoap_build.h"
#if !defined(WITH_LWIP) && !defined(WITH_CONTIKI) && !defined(RIOT_VERSION)
#ifdef HAVE_TIME_H
#include <time.h>
#endif /* HAVE_TIME_H */
#ifdef HAVE_SYS_TIME_H
#include <sys/time.h>
#endif
#endif /* HAVE_SYS_TIME_H */
#ifdef HAVE_UNISTD_H
#include <unistd.h> /* _POSIX_TIMERS */
#endif
#endif /* HAVE_UNISTD_H */
#ifdef HAVE_WINSOCK2_H
#include <winsock2.h>
#include <stdint.h>
#endif
#endif /* HAVE_WINSOCK2_H */
static coap_tick_t coap_clock_offset = 0;
@@ -132,7 +138,7 @@ coap_ticks_from_rt_us(uint64_t t) {
#undef FRAC
#undef SHR_FP
#else /* HAVE_TIME_H */
#else /* WITH_LWIP || WITH_CONTIKI || RIOT_VERSION */
#ifdef __clang__
/* Make compilers happy that do not like empty modules. As this function is
@@ -144,4 +150,4 @@ COAP_STATIC_INLINE void
dummy(void) {
}
#endif /* not HAVE_TIME_H */
#endif /* WITH_LWIP || WITH_CONTIKI || RIOT_VERSION */