From 084f0287a3f3c277a75424fb1e672a2c06335899 Mon Sep 17 00:00:00 2001 From: Benjamin Valentin Date: Wed, 19 Oct 2022 15:52:40 +0200 Subject: [PATCH] nanocoap_sock: support handling empty ACKs with separate response --- sys/include/net/coap.h | 8 ++++++++ sys/net/application_layer/nanocoap/sock.c | 11 ++++++++++- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/sys/include/net/coap.h b/sys/include/net/coap.h index 1d31ccd7e9..34eb47b911 100644 --- a/sys/include/net/coap.h +++ b/sys/include/net/coap.h @@ -251,6 +251,14 @@ extern "C" { #define CONFIG_COAP_RANDOM_FACTOR_1000 (1500) #endif +/** + * @brief Timeout in milliseconds for a separate (deferred) response + * sent after an empty ACK. + */ +#ifndef CONFIG_COAP_SEPARATE_RESPONSE_TIMEOUT_MS +#define CONFIG_COAP_SEPARATE_RESPONSE_TIMEOUT_MS (10 * MS_PER_SEC) +#endif + /** @brief Maximum number of retransmissions for a confirmable request */ #ifndef CONFIG_COAP_MAX_RETRANSMIT #define CONFIG_COAP_MAX_RETRANSMIT (4) diff --git a/sys/net/application_layer/nanocoap/sock.c b/sys/net/application_layer/nanocoap/sock.c index 75ad443983..eb81fee1cd 100644 --- a/sys/net/application_layer/nanocoap/sock.c +++ b/sys/net/application_layer/nanocoap/sock.c @@ -224,8 +224,17 @@ ssize_t nanocoap_sock_request_cb(nanocoap_sock_t *sock, coap_pkt_t *pkt, case COAP_TYPE_CON: _send_ack(sock, pkt); /* fall-through */ - case COAP_TYPE_NON: case COAP_TYPE_ACK: + if (cb && coap_get_code(pkt) == COAP_CODE_EMPTY) { + /* empty ACK, wait for separate response */ + state = STATE_RESPONSE_RCVD; + deadline = _deadline_from_interval(CONFIG_COAP_SEPARATE_RESPONSE_TIMEOUT_MS + * US_PER_MS); + DEBUG("nanocoap: wait for separate response\n"); + continue; + } + /* fall-through */ + case COAP_TYPE_NON: /* call user callback */ if (cb) { res = cb(arg, pkt);