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.
This commit is contained in:
Olaf Bergmann committed 2016-04-08 15:14:20 +02:00
1 parent 3d3eb02ad8
commit 63f51418c5
6 files changed
+141 -1

No files matched your search

+3 -1
View File
@@ -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 \
+1
View File
@@ -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"
+37
View File
@@ -0,0 +1,37 @@
/*
* run.h -- CoAP main loop
*
* Copyright (C) 2016 Olaf Bergmann <bergmann@tzi.org>
*
* 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 */
+2
View File
@@ -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;
+2
View File
@@ -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
+96
View File
@@ -0,0 +1,96 @@
/* run.c -- CoAP main loop
*
* Copyright (C) 2016 Olaf Bergmann <bergmann@tzi.org>
*
* This file is part of the CoAP library libcoap. Please see
* README for terms of use.
*/
#include "coap_config.h"
#include "coap.h"
#include <errno.h>
#ifdef HAVE_SYS_SELECT_H
#include <sys/select.h>
#else
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
#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);
}
}