diff --git a/Makefile.am b/Makefile.am index 2bda7f4a..e7089eb5 100644 --- a/Makefile.am +++ b/Makefile.am @@ -55,6 +55,7 @@ libcoap_@LIBCOAP_API_VERSION@_la_SOURCES = \ src/async.c \ src/block.c \ src/coap_dtls.c \ + src/coap_event.c \ src/coap_keystore.c \ src/coap_time.c \ src/debug.c \ @@ -84,6 +85,7 @@ libcoap_include_HEADERS = \ include/coap/block.h \ include/coap/coap.h \ include/coap/coap_dtls.h \ + include/coap/coap_event.h \ include/coap/coap_keystore.h \ include/coap/coap_io.h \ include/coap/coap_time.h \ diff --git a/include/coap/coap.h.in b/include/coap/coap.h.in index 8715a4c7..e12d74cd 100644 --- a/include/coap/coap.h.in +++ b/include/coap/coap.h.in @@ -37,6 +37,7 @@ extern "C" { #include "bits.h" #include "block.h" #include "coap_dtls.h" +#include "coap_event.h" #include "coap_keystore.h" #include "coap_io.h" #include "coap_time.h" diff --git a/include/coap/coap_event.h b/include/coap/coap_event.h new file mode 100644 index 00000000..a72a4360 --- /dev/null +++ b/include/coap/coap_event.h @@ -0,0 +1,64 @@ +/* + * coap_event.h -- libcoap Event API + * + * Copyright (C) 2016 Olaf Bergmann + * + * This file is part of the CoAP library libcoap. Please see README for terms + * of use. + */ + +#ifndef _COAP_EVENT_H_ +#define _COAP_EVENT_H_ + +struct coap_context_t; + +/** + * @defgroup events Event API + * API functions for event deliver from lower-layer library functions. + * @{ + */ + +/** + * Scalar type to represent different events, e.g. DTLS events or + * retransmission timeouts. + */ + typedef unsigned int coap_event_t; + +#define COAP_EVENT_DTLS_CLOSED 0x0000 +#define COAP_EVENT_DTLS_CONNECTED 0x01DE +#define COAP_EVENT_DTLS_RENEGOTIATE 0x01DF +#define COAP_EVENT_DTLS_ERROR 0x0200 + +/** + * Type for event handler functions that can be registered with a CoAP + * context using the unction coap_set_event_handler(). When called by + * the library, the first argument will be the coap_context_t object + * where the handler function has been registered. The second argument + * is the event type that may be complemented by event-specific data + * passed as the third argument. + */ +typedef int (*coap_event_handler_t)(struct coap_context_t *, + coap_event_t, + void *); + +/** + * Registers the function @p hnd as callback for events from the given + * CoAP context @p context. Any event handler that has previously been + * registered with @p context will be overwritten by this operation. + * + * @param context The CoAP context to register the event handler with. + * @param hnd The event handler to be registered. + */ +void coap_set_event_handler(struct coap_context_t *context, + coap_event_handler_t hnd); + +/** + * Clears the event handler registered with @p context. + * + * @param context The CoAP context whose event handler is to be removed. + */ +void coap_clear_event_handler(struct coap_context_t *context); + +/** @} */ + +#endif /* COAP_EVENT_H */ diff --git a/include/coap/net.h b/include/coap/net.h index 5fe5fabe..14d049f6 100644 --- a/include/coap/net.h +++ b/include/coap/net.h @@ -22,6 +22,7 @@ #include "coap_io.h" #include "coap_dtls.h" +#include "coap_event.h" #include "coap_keystore.h" #include "coap_time.h" #include "option.h" @@ -119,6 +120,12 @@ typedef struct coap_context_t { coap_response_handler_t response_handler; + /** + * Callback function that is used to signal events to the + * application. This field is set by coap_set_event_handler(). + */ + coap_event_handler_t handle_event; + ssize_t (*network_send)(struct coap_context_t *context, const coap_endpoint_t *local_interface, const coap_address_t *dst, @@ -396,6 +403,19 @@ int coap_handle_message(coap_context_t *ctx, const coap_address_t *remote, unsigned char *msg, size_t msg_len); +/** + * Invokes the event handler of @p context for the given @p event and + * @p data. + * + * @param context The CoAP context whose event handler is to be called. + * @param event The event to deliver. + * @param data Any data related to @p event. + * @return The result from the associated event handler or 0 if none was + * registered. + */ +int coap_handle_event(coap_context_t *context, + coap_event_t event, + void *data); /** * Calculates a unique transaction id from given arguments @p peer and @p pdu. * The id is returned in @p id. diff --git a/src/coap_event.c b/src/coap_event.c new file mode 100644 index 00000000..ac8dcb2b --- /dev/null +++ b/src/coap_event.c @@ -0,0 +1,21 @@ +/* + * coap_event.c -- libcoap Event API + * + * Copyright (C) 2016 Olaf Bergmann + * + * This file is part of the CoAP library libcoap. Please see README for terms + * of use. + */ + +#include "coap_event.h" +#include "net.h" + +void +coap_set_event_handler(struct coap_context_t *context, coap_event_handler_t hnd) { + context->handle_event = hnd; +} + +void +coap_clear_event_handler(struct coap_context_t *context) { + context->handle_event = NULL; +} diff --git a/src/net.c b/src/net.c index fab46f17..9357d852 100644 --- a/src/net.c +++ b/src/net.c @@ -1717,6 +1717,17 @@ coap_dispatch(coap_context_t *context, coap_queue_t *rcvd) { } } +int +coap_handle_event(coap_context_t *context, coap_event_t event, void *data) { + coap_log(LOG_DEBUG, "*** EVENT: 0x%04x\n", event); + + if (context->handle_event) { + return context->handle_event(context, event, data); + } else { + return 0; + } +} + int coap_can_exit( coap_context_t *context ) { return !context || (context->sendqueue == NULL);