diff --git a/Makefile.am b/Makefile.am index ed52316b..def82a5e 100644 --- a/Makefile.am +++ b/Makefile.am @@ -105,13 +105,14 @@ libcoap_include_HEADERS = \ $(top_srcdir)/include/coap$(LIBCOAP_API_VERSION)/bits.h \ $(top_srcdir)/include/coap$(LIBCOAP_API_VERSION)/block.h \ $(top_builddir)/include/coap$(LIBCOAP_API_VERSION)/coap.h \ + $(top_srcdir)/include/coap$(LIBCOAP_API_VERSION)/coap_debug.h \ $(top_srcdir)/include/coap$(LIBCOAP_API_VERSION)/coap_dtls.h \ $(top_srcdir)/include/coap$(LIBCOAP_API_VERSION)/coap_event.h \ $(top_srcdir)/include/coap$(LIBCOAP_API_VERSION)/coap_hashkey.h \ $(top_srcdir)/include/coap$(LIBCOAP_API_VERSION)/coap_io.h \ + $(top_srcdir)/include/coap$(LIBCOAP_API_VERSION)/coap_mutex.h \ $(top_srcdir)/include/coap$(LIBCOAP_API_VERSION)/coap_session.h \ $(top_srcdir)/include/coap$(LIBCOAP_API_VERSION)/coap_time.h \ - $(top_srcdir)/include/coap$(LIBCOAP_API_VERSION)/coap_debug.h \ $(top_srcdir)/include/coap$(LIBCOAP_API_VERSION)/encode.h \ $(top_srcdir)/include/coap$(LIBCOAP_API_VERSION)/libcoap.h \ $(top_srcdir)/include/coap$(LIBCOAP_API_VERSION)/mem.h \ diff --git a/coap_config.h.contiki b/coap_config.h.contiki index b363ea93..f330bc6b 100644 --- a/coap_config.h.contiki +++ b/coap_config.h.contiki @@ -7,6 +7,10 @@ #define WITH_CONTIKI 1 +#ifndef COAP_CONSTRAINED_STACK +#define COAP_CONSTRAINED_STACK 1 +#endif + #define PACKAGE_STRING "libcoap" #define PACKAGE_NAME "libcoap" diff --git a/coap_config.h.lwip b/coap_config.h.lwip index 05c6e16d..01f01782 100644 --- a/coap_config.h.lwip +++ b/coap_config.h.lwip @@ -7,6 +7,10 @@ #define WITH_LWIP 1 +#ifndef COAP_CONSTRAINED_STACK +#define COAP_CONSTRAINED_STACK 1 +#endif + #define PACKAGE_NAME "libcoap-lwip" #define PACKAGE_VERSION "?" #define PACKAGE_STRING PACKAGE_NAME PACKAGE_VERSION diff --git a/include/coap2/coap_mutex.h b/include/coap2/coap_mutex.h new file mode 100644 index 00000000..99d7d335 --- /dev/null +++ b/include/coap2/coap_mutex.h @@ -0,0 +1,50 @@ +/* + * coap_mutex.h -- mutex utilities + * + * Copyright (C) 2019 Jon Shallow + * + * This file is part of the CoAP library libcoap. Please see README for terms + * of use. + */ + +/** + * @file coap_mutex.h + * @brief COAP mutex mechanism wrapper + */ + +#ifndef COAP_MUTEX_H_ +#define COAP_MUTEX_H_ + +#if defined(RIOT_VERSION) + +#include + +typedef mutex_t coap_mutex_t; +#define COAP_MUTEX_INITIALIZER MUTEX_INIT +#define coap_mutex_lock(a) mutex_lock(a) +#define coap_mutex_trylock(a) mutex_trylock(a) +#define coap_mutex_unlock(a) mutex_unlock(a) + +#elif defined(WITH_CONTIKI) + +/* CONTIKI does not support mutex */ + +typedef int coap_mutex_t; +#define COAP_MUTEX_INITIALIZER 0 +#define coap_mutex_lock(a) *(a) = 1 +#define coap_mutex_trylock(a) *(a) = 1 +#define coap_mutex_unlock(a) *(a) = 0 + +#else /* ! RIOT_VERSION && ! WITH_CONTIKI */ + +#include + +typedef pthread_mutex_t coap_mutex_t; +#define COAP_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER +#define coap_mutex_lock(a) pthread_mutex_lock(a) +#define coap_mutex_trylock(a) pthread_mutex_trylock(a) +#define coap_mutex_unlock(a) pthread_mutex_unlock(a) + +#endif /* ! RIOT_VERSION && ! WITH_CONTIKI */ + +#endif /* COAP_MUTEX_H_ */ diff --git a/src/coap_debug.c b/src/coap_debug.c index 39731ad9..52518ce7 100644 --- a/src/coap_debug.c +++ b/src/coap_debug.c @@ -37,6 +37,7 @@ #include "coap_debug.h" #include "encode.h" #include "net.h" +#include "coap_mutex.h" #ifdef WITH_LWIP # define fprintf(fd, ...) LWIP_PLATFORM_DIAG((__VA_ARGS__)) @@ -459,7 +460,14 @@ is_binary(int content_format) { void coap_show_pdu(coap_log_t level, const coap_pdu_t *pdu) { +#if COAP_CONSTRAINED_STACK + static coap_mutex_t static_show_pdu_mutex = COAP_MUTEX_INITIALIZER; + static unsigned char buf[1024]; /* need some space for output creation */ + static char outbuf[COAP_DEBUG_BUF_SIZE]; +#else /* ! COAP_CONSTRAINED_STACK */ unsigned char buf[1024]; /* need some space for output creation */ + char outbuf[COAP_DEBUG_BUF_SIZE]; +#endif /* ! COAP_CONSTRAINED_STACK */ size_t buf_len = 0; /* takes the number of bytes written to buf */ int encode = 0, have_options = 0, i; coap_opt_iterator_t opt_iter; @@ -467,13 +475,16 @@ coap_show_pdu(coap_log_t level, const coap_pdu_t *pdu) { int content_format = -1; size_t data_len; unsigned char *data; - char outbuf[COAP_DEBUG_BUF_SIZE]; int outbuflen = 0; /* Save time if not needed */ if (level > coap_get_log_level()) return; +#if COAP_CONSTRAINED_STACK + coap_mutex_lock(&static_show_pdu_mutex); +#endif /* COAP_CONSTRAINED_STACK */ + snprintf(outbuf, sizeof(outbuf), "v:%d t:%s c:%s i:%04x {", COAP_DEFAULT_VERSION, msg_type_string(pdu->type), msg_code_string(pdu->code), pdu->tid); @@ -646,6 +657,10 @@ coap_show_pdu(coap_log_t level, const coap_pdu_t *pdu) { outbuflen = strlen(outbuf); snprintf(&outbuf[outbuflen], sizeof(outbuf)-outbuflen, "\n"); COAP_DO_SHOW_OUTPUT_LINE; + +#if COAP_CONSTRAINED_STACK + coap_mutex_unlock(&static_show_pdu_mutex); +#endif /* COAP_CONSTRAINED_STACK */ } void coap_show_tls_version(coap_log_t level) @@ -750,16 +765,24 @@ coap_log_impl(coap_log_t level, const char *format, ...) { return; if (log_handler) { -#if defined(WITH_CONTIKI) || defined(WITH_LWIP) - char message[128]; -#else - char message[8 + 1024 * 2]; /* O/H + Max packet payload size * 2 */ -#endif +#if COAP_CONSTRAINED_STACK + static coap_mutex_t static_log_mutex = COAP_MUTEX_INITIALIZER; + static char message[COAP_DEBUG_BUF_SIZE]; +#else /* ! COAP_CONSTRAINED_STACK */ + char message[COAP_DEBUG_BUF_SIZE]; +#endif /* ! COAP_CONSTRAINED_STACK */ va_list ap; va_start(ap, format); +#if COAP_CONSTRAINED_STACK + coap_mutex_lock(&static_log_mutex); +#endif /* COAP_CONSTRAINED_STACK */ + vsnprintf( message, sizeof(message), format, ap); va_end(ap); log_handler(level, message); +#if COAP_CONSTRAINED_STACK + coap_mutex_unlock(&static_log_mutex); +#endif /* COAP_CONSTRAINED_STACK */ } else { char timebuf[32]; coap_tick_t now; diff --git a/src/coap_io.c b/src/coap_io.c index 2a4900b2..4da8611b 100644 --- a/src/coap_io.c +++ b/src/coap_io.c @@ -53,6 +53,7 @@ #include "pdu.h" #include "utlist.h" #include "resource.h" +#include "coap_mutex.h" #if !defined(WITH_CONTIKI) /* define generic PKTINFO for IPv4 */ @@ -1310,14 +1311,24 @@ coap_write(coap_context_t *ctx, int coap_run_once(coap_context_t *ctx, unsigned timeout_ms) { +#if COAP_CONSTRAINED_STACK + static coap_mutex_t static_mutex = COAP_MUTEX_INITIALIZER; + static fd_set readfds, writefds, exceptfds; + static coap_socket_t *sockets[64]; +#else /* ! COAP_CONSTRAINED_STACK */ fd_set readfds, writefds, exceptfds; + coap_socket_t *sockets[64]; +#endif /* ! COAP_CONSTRAINED_STACK */ coap_fd_t nfds = 0; struct timeval tv; coap_tick_t before, now; int result; - coap_socket_t *sockets[64]; unsigned int num_sockets = 0, i, timeout; +#if COAP_CONSTRAINED_STACK + coap_mutex_lock(&static_mutex); +#endif /* COAP_CONSTRAINED_STACK */ + coap_ticks(&before); timeout = coap_write(ctx, sockets, (unsigned int)(sizeof(sockets) / sizeof(sockets[0])), &num_sockets, before); @@ -1356,6 +1367,9 @@ coap_run_once(coap_context_t *ctx, unsigned timeout_ms) { if (errno != EINTR) { #endif coap_log(LOG_DEBUG, "%s", coap_socket_strerror()); +#if COAP_CONSTRAINED_STACK + coap_mutex_unlock(&static_mutex); +#endif /* COAP_CONSTRAINED_STACK */ return -1; } } @@ -1376,6 +1390,10 @@ coap_run_once(coap_context_t *ctx, unsigned timeout_ms) { coap_ticks(&now); coap_read(ctx, now); +#if COAP_CONSTRAINED_STACK + coap_mutex_unlock(&static_mutex); +#endif /* COAP_CONSTRAINED_STACK */ + return (int)(((now - before) * 1000) / COAP_TICKS_PER_SECOND); } diff --git a/src/net.c b/src/net.c index 106dbd6a..0f350d3d 100644 --- a/src/net.c +++ b/src/net.c @@ -53,6 +53,7 @@ #include "block.h" #include "net.h" #include "utlist.h" +#include "coap_mutex.h" #ifndef min #define min(a,b) ((a) < (b) ? (a) : (b)) @@ -1110,28 +1111,19 @@ coap_write_session(coap_context_t *ctx, coap_session_t *session, coap_tick_t now } } -#ifdef WITH_CONTIKI -COAP_STATIC_INLINE coap_packet_t * -coap_malloc_packet(void) { - return (coap_packet_t *)coap_malloc_type(COAP_PACKET, 0); -} - -void -coap_free_packet(coap_packet_t *packet) { - coap_free_type(COAP_PACKET, packet); -} -#endif /* WITH_CONTIKI */ - static void coap_read_session(coap_context_t *ctx, coap_session_t *session, coap_tick_t now) { -#ifdef WITH_CONTIKI - coap_packet_t *packet = coap_malloc_packet(); - if ( !packet ) - return; -#else /* WITH_CONTIKI */ +#if COAP_CONSTRAINED_STACK + static coap_mutex_t s_static_mutex = COAP_MUTEX_INITIALIZER; + static coap_packet_t s_packet; +#else /* ! COAP_CONSTRAINED_STACK */ coap_packet_t s_packet; +#endif /* ! COAP_CONSTRAINED_STACK */ coap_packet_t *packet = &s_packet; -#endif /* WITH_CONTIKI */ + +#if COAP_CONSTRAINED_STACK + coap_mutex_lock(&s_static_mutex); +#endif /* COAP_CONSTRAINED_STACK */ assert(session->sock.flags & (COAP_SOCKET_CONNECTED | COAP_SOCKET_MULTICAST)); @@ -1245,37 +1237,33 @@ coap_read_session(coap_context_t *ctx, coap_session_t *session, coap_tick_t now) if (bytes_read < 0) coap_session_disconnected(session, COAP_NACK_NOT_DELIVERABLE); } - -#ifdef WITH_CONTIKI - if ( packet ) - coap_free_packet( packet ); -#endif +#if COAP_CONSTRAINED_STACK + coap_mutex_unlock(&s_static_mutex); +#endif /* COAP_CONSTRAINED_STACK */ } static int coap_read_endpoint(coap_context_t *ctx, coap_endpoint_t *endpoint, coap_tick_t now) { ssize_t bytes_read = -1; int result = -1; /* the value to be returned */ -#ifdef WITH_CONTIKI - coap_packet_t *packet = coap_malloc_packet(); -#else /* WITH_CONTIKI */ - coap_packet_t s_packet; - coap_packet_t *packet = &s_packet; -#endif /* WITH_CONTIKI */ +#if COAP_CONSTRAINED_STACK + static coap_mutex_t e_static_mutex = COAP_MUTEX_INITIALIZER; + static coap_packet_t e_packet; +#else /* ! COAP_CONSTRAINED_STACK */ + coap_packet_t e_packet; +#endif /* ! COAP_CONSTRAINED_STACK */ + coap_packet_t *packet = &e_packet; assert(COAP_PROTO_NOT_RELIABLE(endpoint->proto)); assert(endpoint->sock.flags & COAP_SOCKET_BOUND); - if (packet) { - coap_address_init(&packet->src); - coap_address_copy(&packet->dst, &endpoint->bind_addr); - bytes_read = ctx->network_read(&endpoint->sock, packet); - } - else { - coap_log(LOG_WARNING, "* %s: Packet allocation failed\n", - coap_endpoint_str(endpoint)); - return -1; - } +#if COAP_CONSTRAINED_STACK + coap_mutex_lock(&e_static_mutex); +#endif /* COAP_CONSTRAINED_STACK */ + + coap_address_init(&packet->src); + coap_address_copy(&packet->dst, &endpoint->bind_addr); + bytes_read = ctx->network_read(&endpoint->sock, packet); if (bytes_read < 0) { coap_log(LOG_WARNING, "* %s: read failed\n", coap_endpoint_str(endpoint)); @@ -1289,12 +1277,9 @@ coap_read_endpoint(coap_context_t *ctx, coap_endpoint_t *endpoint, coap_tick_t n coap_endpoint_new_dtls_session(endpoint, packet, now); } } - -#ifdef WITH_CONTIKI - if (packet) - coap_free_packet(packet); -#endif - +#if COAP_CONSTRAINED_STACK + coap_mutex_unlock(&e_static_mutex); +#endif /* COAP_CONSTRAINED_STACK */ return result; }