From fb1054d6cff8dd2ad94aed56194e0aebd27d1744 Mon Sep 17 00:00:00 2001 From: Olaf Bergmann Date: Fri, 26 Feb 2016 18:04:21 +0100 Subject: [PATCH] run.[hc]: Add new functions for main loop This modification provides two new functions, coap_run_once() and coap_run(), that can be used by applications to drive the library's I/O logic. --- Makefile.am | 4 +- include/coap/coap.h.in | 1 + include/coap/run.h | 37 ++++++++++++++++ libcoap-1.map | 2 + libcoap-1.sym | 2 + src/platform/posix/run.c | 96 ++++++++++++++++++++++++++++++++++++++++ 6 files changed, 141 insertions(+), 1 deletion(-) create mode 100644 include/coap/run.h create mode 100644 src/platform/posix/run.c diff --git a/Makefile.am b/Makefile.am index ab553fb4..979dc075 100644 --- a/Makefile.am +++ b/Makefile.am @@ -67,7 +67,8 @@ libcoap_@LIBCOAP_API_VERSION@_la_SOURCES = \ src/subscribe.c \ src/uri.c \ $(platform_src)/coap_io.c \ - $(platform_src)/prng.c + $(platform_src)/prng.c \ + $(platform_src)/run.c ## Define the list of public header files and their install location. ## The list defined here will be used within the include/Makefile.am @@ -92,6 +93,7 @@ libcoap_include_HEADERS = \ include/coap/pdu.h \ include/coap/prng.h \ include/coap/resource.h \ + include/coap/run.h \ include/coap/str.h \ include/coap/subscribe.h \ include/coap/uri.h \ diff --git a/include/coap/coap.h.in b/include/coap/coap.h.in index 76ebc5eb..f11e3476 100644 --- a/include/coap/coap.h.in +++ b/include/coap/coap.h.in @@ -46,6 +46,7 @@ extern "C" { #include "pdu.h" #include "prng.h" #include "resource.h" +#include "run.h" #include "str.h" #include "subscribe.h" #include "uri.h" diff --git a/include/coap/run.h b/include/coap/run.h new file mode 100644 index 00000000..4b92abf7 --- /dev/null +++ b/include/coap/run.h @@ -0,0 +1,37 @@ +/* + * run.h -- CoAP main loop + * + * Copyright (C) 2016 Olaf Bergmann + * + * This file is part of the CoAP library libcoap. Please see README for terms + * of use. + */ + +#ifndef _COAP_RUN_H_ +#define _COAP_RUN_H_ + +#include "net.h" + +/** + * Execute a single send/receive step. This function sends out any due + * message from the sendqueue and then waits up to @p timeout_ms + * milliseconds for incoming messages on the endpoints that are + * attached to @p ctx. The return value is less than zero on error or + * the number of milliseconds that have passed while waiting. + * + * @param ctx The context to operate on. + * @param timeout_ms The timeout in milliseconds. + * @return The number of milliseconds that have passed since the + * operation has started or a value less than zero on error. + */ +int coap_run_once(coap_context_t *ctx, unsigned int timeout_ms); + +/** + * Simple main loop. This function infinitely runs coap_run_once() + * on the given context @p ctx. + * + * @param ctx The context to operate on. + */ +void coap_run(coap_context_t *ctx); + +#endif /* COAP_RUN_H */ diff --git a/libcoap-1.map b/libcoap-1.map index bf67af3c..9832311e 100644 --- a/libcoap-1.map +++ b/libcoap-1.map @@ -95,6 +95,8 @@ global: coap_resource_init; coap_response_phrase; coap_retransmit; + coap_run; + coap_run_once; coap_send; coap_send_ack; coap_send_confirmed; diff --git a/libcoap-1.sym b/libcoap-1.sym index 15107525..6fa02d82 100644 --- a/libcoap-1.sym +++ b/libcoap-1.sym @@ -93,6 +93,8 @@ coap_remove_from_queue coap_resource_init coap_response_phrase coap_retransmit +coap_run +coap_run_once coap_send coap_send_ack coap_send_confirmed diff --git a/src/platform/posix/run.c b/src/platform/posix/run.c new file mode 100644 index 00000000..f86d155e --- /dev/null +++ b/src/platform/posix/run.c @@ -0,0 +1,96 @@ +/* run.c -- CoAP main loop + * + * Copyright (C) 2016 Olaf Bergmann + * + * This file is part of the CoAP library libcoap. Please see + * README for terms of use. + */ + +#include "coap_config.h" +#include "coap.h" + +#include + +#ifdef HAVE_SYS_SELECT_H +#include +#else +#include +#include +#include +#endif /* HAVE_SYS_SELECT_H */ + +int +coap_run_once(coap_context_t *ctx, unsigned int timeout_ms) { + fd_set readfds; + struct timeval tv; + coap_endpoint_t *ep; + coap_queue_t *nextpdu; + coap_tick_t now; + unsigned long max_wait; + int maxfd = -1; + int result; + + FD_ZERO(&readfds); + LL_FOREACH(ctx->endpoint, ep) { + FD_SET(ep->handle.fd, &readfds); + if (maxfd < ep->handle.fd) { + maxfd = ep->handle.fd; + } + } + + coap_ticks(&now); + + max_wait = timeout_ms * COAP_TICKS_PER_SECOND / 1000; + while (1) { + nextpdu = coap_peek_next(ctx); + + while (nextpdu && nextpdu->t <= now - ctx->sendqueue_basetime) { + coap_retransmit(ctx, coap_pop_next(ctx)); + nextpdu = coap_peek_next(ctx); + } + + if (nextpdu && ((max_wait == 0) || nextpdu->t < max_wait)) { + /* set timeout if there is a pdu to send */ + tv.tv_usec = ((nextpdu->t) % COAP_TICKS_PER_SECOND) * 1000000 / COAP_TICKS_PER_SECOND; + tv.tv_sec = (nextpdu->t) / COAP_TICKS_PER_SECOND; + } else { + tv.tv_usec = (max_wait % COAP_TICKS_PER_SECOND) * 1000000 / COAP_TICKS_PER_SECOND; + tv.tv_sec = max_wait / COAP_TICKS_PER_SECOND; + } + + result = select(maxfd + 1, &readfds, 0, 0, (nextpdu || (max_wait > 0)) ? &tv : NULL); + + if (result < 0) { /* error */ + if (errno != EINTR) { + coap_log(LOG_DEBUG, strerror(errno)); + } + return -1; + } else if (result > 0) { /* read from socket */ + coap_tick_t past = now; + LL_FOREACH(ctx->endpoint, ep) { + if (FD_ISSET(ep->handle.fd, &readfds)) { + coap_read(ctx); /* read received data */ + } + } + coap_ticks(&now); + return ((now - past) * 1000) / COAP_TICKS_PER_SECOND; + } else { /* timeout */ + coap_tick_t past = now; + coap_ticks(&now); + if (past + max_wait <= now) { + return (now - past); + } else { + max_wait -= (now - past); + } + } + } + + /* never reached */ +} + +void +coap_run(coap_context_t *ctx) { + while (1) { + coap_run_once(ctx, 0); + } +}