coap_event.[hc]: pass library events to application

An application can register a callback function of type
coap_event_handler_t that will be called by the library on certain
events. Currently, the DTLS implementation invokes coap_handle_event()
with session-specific events such as session close or
renegotiation.
This commit is contained in:
Olaf Bergmann committed 2016-04-08 15:14:20 +02:00
1 parent 578b38dbfa
commit b734208ed2
6 files changed
+119

No files matched your search

+1
View File
@@ -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"
+64
View File
@@ -0,0 +1,64 @@
/*
* coap_event.h -- libcoap Event API
*
* 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_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 */
+20
View File
@@ -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.