From fbe814d905513df97030826faea10fe654cdcecf Mon Sep 17 00:00:00 2001 From: Jon Shallow Date: Sat, 10 Feb 2018 20:54:17 +0000 Subject: [PATCH 1/3] net.h: Add function definitions for ping and TCP pong handlers If the application needs to see CoAP ping requests or CoAP TCP pong responses then there needs to be a handler that can be set up that is called appropriately. --- include/coap/net.h | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/include/coap/net.h b/include/coap/net.h index 4ee99540..c6411d50 100644 --- a/include/coap/net.h +++ b/include/coap/net.h @@ -76,6 +76,18 @@ typedef void (*coap_nack_handler_t)(struct coap_context_t *, coap_nack_reason_t reason, const coap_tid_t id); +/** Message handler that is used as call-back in coap_context_t */ +typedef void (*coap_ping_handler_t)(struct coap_context_t *, + coap_session_t *session, + coap_pdu_t *received, + const coap_tid_t id); + +/** Message handler that is used as call-back in coap_context_t */ +typedef void (*coap_pong_handler_t)(struct coap_context_t *, + coap_session_t *session, + coap_pdu_t *received, + const coap_tid_t id); + #define COAP_MID_CACHE_SIZE 3 typedef struct { unsigned char flags[COAP_MID_CACHE_SIZE]; @@ -122,6 +134,8 @@ typedef struct coap_context_t { coap_response_handler_t response_handler; coap_nack_handler_t nack_handler; + coap_ping_handler_t ping_handler; + coap_pong_handler_t pong_handler; /** * Callback function that is used to signal events to the @@ -178,6 +192,31 @@ coap_register_nack_handler(coap_context_t *context, context->nack_handler = handler; } +/** + * Registers a new message handler that is called whenever a CoAP Ping + * message is received. + * + * @param context The context to register the handler for. + * @param handler The ping handler to register. + */ +COAP_STATIC_INLINE void +coap_register_ping_handler(coap_context_t *context, + coap_ping_handler_t handler) { + context->ping_handler = handler; +} + +/** + * Registers a new message handler that is called whenever a CoAP Pong + * message is received. + * + * @param context The context to register the handler for. + * @param handler The pong handler to register. + */ +COAP_STATIC_INLINE void +coap_register_pong_handler(coap_context_t *context, + coap_pong_handler_t handler) { + context->pong_handler = handler; +} /** * Registers the option type @p type with the given context object @p ctx. From 1615d4e3b7850556fe99852f5478fb4b23d75a3e Mon Sep 17 00:00:00 2001 From: Jon Shallow Date: Sat, 10 Feb 2018 20:57:27 +0000 Subject: [PATCH 2/3] net.c: Include ping request / pong response handler support If the application needs to see CoAP ping requests or CoAP TCP pong responses then this update provides calling the configured handlers at the appropriate points. --- src/net.c | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/net.c b/src/net.c index bebbf3a1..3ff47163 100644 --- a/src/net.c +++ b/src/net.c @@ -2010,6 +2010,10 @@ handle_signaling(coap_context_t *context, coap_session_t *session, coap_add_option(pong, COAP_SIGNALING_OPTION_CUSTODY, 0, NULL); coap_send(session, pong); } + } else if (pdu->code == COAP_SIGNALING_PONG) { + if (context->pong_handler) { + context->pong_handler(context, session, pdu, pdu->tid); + } } else if (pdu->code == COAP_SIGNALING_RELEASE || pdu->code == COAP_SIGNALING_ABORT) { coap_session_disconnected(session, COAP_NACK_RST); @@ -2112,6 +2116,12 @@ coap_dispatch(coap_context_t *context, coap_session_t *session, else if (COAP_PDU_IS_RESPONSE(pdu)) handle_response(context, session, sent ? sent->pdu : NULL, pdu); else { + if (COAP_PDU_IS_EMPTY(pdu)) { + if (context->ping_handler) { + context->ping_handler(context, session, + pdu, pdu->tid); + } + } debug("dropped message with invalid code (%d.%02d)\n", COAP_RESPONSE_CLASS(pdu->code), pdu->code & 0x1f); From b02019d439cef35cd98262f12664fb6baea909de Mon Sep 17 00:00:00 2001 From: Jon Shallow Date: Mon, 12 Feb 2018 19:59:34 +0000 Subject: [PATCH 3/3] net.c: Include call to ping handler if COAP_SIGNALING_PING is received Include missing ping potential intercept handler code. --- src/net.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/net.c b/src/net.c index 3ff47163..ac960c51 100644 --- a/src/net.c +++ b/src/net.c @@ -2006,14 +2006,17 @@ handle_signaling(coap_context_t *context, coap_session_t *session, coap_session_connected(session); } else if (pdu->code == COAP_SIGNALING_PING) { coap_pdu_t *pong = coap_pdu_init(COAP_MESSAGE_CON, COAP_SIGNALING_PONG, 0, 1); + if (context->ping_handler) { + context->ping_handler(context, session, pdu, pdu->tid); + } if (pong) { coap_add_option(pong, COAP_SIGNALING_OPTION_CUSTODY, 0, NULL); coap_send(session, pong); } } else if (pdu->code == COAP_SIGNALING_PONG) { - if (context->pong_handler) { - context->pong_handler(context, session, pdu, pdu->tid); - } + if (context->pong_handler) { + context->pong_handler(context, session, pdu, pdu->tid); + } } else if (pdu->code == COAP_SIGNALING_RELEASE || pdu->code == COAP_SIGNALING_ABORT) { coap_session_disconnected(session, COAP_NACK_RST);