From 13f935e289f68ca13a7d673604f91315923f4aa4 Mon Sep 17 00:00:00 2001 From: Jon Shallow Date: Tue, 19 Feb 2019 08:54:08 -1200 Subject: [PATCH] coap-client: Clean exit on detection of DTLS closure If there is an issue during DTLS setup, the coap-client will keep on retrying and then getting the same failure. examples/client.c: This fix introduces using coap events into coap-client, and on detection of a close event, the coap-client closes down --- examples/client.c | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/examples/client.c b/examples/client.c index 409d6591..47079f89 100644 --- a/examples/client.c +++ b/examples/client.c @@ -307,6 +307,23 @@ check_token(coap_pdu_t *received) { memcmp(received->token, the_token.s, the_token.length) == 0; } +static int +event_handler(coap_context_t *ctx UNUSED_PARAM, + coap_event_t event, + struct coap_session_t *session UNUSED_PARAM) { + + switch(event) { + case COAP_EVENT_DTLS_CLOSED: + case COAP_EVENT_TCP_CLOSED: + case COAP_EVENT_SESSION_CLOSED: + quit = 1; + break; + default: + break; + } + return 0; +} + static void message_handler(struct coap_context_t *ctx, coap_session_t *session, @@ -1405,6 +1422,7 @@ main(int argc, char **argv) { coap_register_option(ctx, COAP_OPTION_BLOCK2); coap_register_response_handler(ctx, message_handler); + coap_register_event_handler(ctx, event_handler); /* construct CoAP message */