From 48d304e7ac696a07a9376709581276a9baae1119 Mon Sep 17 00:00:00 2001 From: jcmichelou Date: Mon, 14 May 2018 19:21:55 +0200 Subject: [PATCH] io, net: Pong response may have same timestamp as ping. Because of time accuracy of COAP_TICKS_PER_SECOND (usually 1000 ticks per second on platform POSIX), the pong response may have the same timestamp as the ping request. To be reliable, the pong timestamp must be tracked separately from the general last RX/TX timestamp. --- include/coap/coap_session.h | 1 + src/coap_io.c | 2 +- src/net.c | 2 ++ 3 files changed, 4 insertions(+), 1 deletion(-) diff --git a/include/coap/coap_session.h b/include/coap/coap_session.h index 150b6735..60cba3a5 100644 --- a/include/coap/coap_session.h +++ b/include/coap/coap_session.h @@ -77,6 +77,7 @@ typedef struct coap_session_t { coap_tick_t last_rx_tx; coap_tick_t last_tx_rst; coap_tick_t last_ping; + coap_tick_t last_pong; coap_tick_t csm_tx; uint8_t *psk_identity; size_t psk_identity_len; diff --git a/src/coap_io.c b/src/coap_io.c index 67f72a5a..f1fde0b5 100644 --- a/src/coap_io.c +++ b/src/coap_io.c @@ -1105,7 +1105,7 @@ coap_write(coap_context_t *ctx, ) { coap_tick_t s_timeout; if (s->last_rx_tx + ctx->ping_timeout * COAP_TICKS_PER_SECOND <= now) { - if ( (s->last_ping > 0 && s->last_ping == s->last_rx_tx) + if ((s->last_ping > 0 && s->last_pong < s->last_ping) || coap_session_send_ping(s) == COAP_INVALID_TID) { /* Make sure the session object is not deleted in the callback */ diff --git a/src/net.c b/src/net.c index 62ad488d..88c551ad 100644 --- a/src/net.c +++ b/src/net.c @@ -694,6 +694,7 @@ coap_send_pdu(coap_session_t *session, coap_pdu_t *pdu, coap_queue_t *node) { return -1; } session->last_ping = 0; + session->last_pong = 0; session->csm_tx = 0; coap_ticks( &session->last_rx_tx ); if ((session->sock.flags & COAP_SOCKET_WANT_CONNECT) != 0) { @@ -2095,6 +2096,7 @@ handle_signaling(coap_context_t *context, coap_session_t *session, coap_send(session, pong); } } else if (pdu->code == COAP_SIGNALING_PONG) { + session->last_pong = session->last_rx_tx; if (context->pong_handler) { context->pong_handler(context, session, pdu, pdu->tid); }