From d680743fe99b17bbbbc4dcb8dffe177860e1538e Mon Sep 17 00:00:00 2001 From: Benjamin Valentin Date: Wed, 5 Jan 2022 22:38:16 +0100 Subject: [PATCH] net/coap: specify timeout in milliseconds This allows for more flexibility when choosing timeouts. --- sys/include/net/coap.h | 12 ++++++------ sys/include/net/gcoap.h | 2 +- sys/net/application_layer/Kconfig.coap | 10 +++++----- sys/net/application_layer/gcoap/gcoap.c | 10 +++++----- sys/net/application_layer/nanocoap/sock.c | 2 +- 5 files changed, 18 insertions(+), 18 deletions(-) diff --git a/sys/include/net/coap.h b/sys/include/net/coap.h index 168211d6bd..4b8c1eb0c1 100644 --- a/sys/include/net/coap.h +++ b/sys/include/net/coap.h @@ -183,16 +183,16 @@ extern "C" { * @{ */ /** - * @brief Timeout in seconds for a response to a confirmable request + * @brief Timeout in milliseconds for a response to a confirmable request * * This value is for the response to the *initial* confirmable message. The * timeout doubles for subsequent retries. To avoid synchronization of resends * across hosts, the actual timeout is chosen randomly between - * @ref CONFIG_COAP_ACK_TIMEOUT and - * (@ref CONFIG_COAP_ACK_TIMEOUT * @ref CONFIG_COAP_RANDOM_FACTOR_1000 / 1000). + * @ref CONFIG_COAP_ACK_TIMEOUT_MS and + * (@ref CONFIG_COAP_ACK_TIMEOUT_MS * @ref CONFIG_COAP_RANDOM_FACTOR_1000 / 1000). */ -#ifndef CONFIG_COAP_ACK_TIMEOUT -#define CONFIG_COAP_ACK_TIMEOUT (2U) +#ifndef CONFIG_COAP_ACK_TIMEOUT_MS +#define CONFIG_COAP_ACK_TIMEOUT_MS (2000U) #endif /** @@ -202,7 +202,7 @@ extern "C" { * ([RFC 7252, section 4.2](https://tools.ietf.org/html/rfc7252#section-4.2)) * multiplied by 1000, to avoid floating point arithmetic. * - * See @ref CONFIG_COAP_ACK_TIMEOUT + * See @ref CONFIG_COAP_ACK_TIMEOUT_MS */ #ifndef CONFIG_COAP_RANDOM_FACTOR_1000 #define CONFIG_COAP_RANDOM_FACTOR_1000 (1500) diff --git a/sys/include/net/gcoap.h b/sys/include/net/gcoap.h index 13410fb7b5..ba9b5b95ad 100644 --- a/sys/include/net/gcoap.h +++ b/sys/include/net/gcoap.h @@ -531,7 +531,7 @@ extern "C" { * In normal operations the timeout between retransmissions doubles. When * CONFIG_GCOAP_NO_RETRANS_BACKOFF is defined this doubling does not happen. * - * @see CONFIG_COAP_ACK_TIMEOUT + * @see CONFIG_COAP_ACK_TIMEOUT_MS */ #define CONFIG_GCOAP_NO_RETRANS_BACKOFF #endif diff --git a/sys/net/application_layer/Kconfig.coap b/sys/net/application_layer/Kconfig.coap index e82af26784..5f4582d4da 100644 --- a/sys/net/application_layer/Kconfig.coap +++ b/sys/net/application_layer/Kconfig.coap @@ -18,15 +18,15 @@ menuconfig KCONFIG_COAP if KCONFIG_COAP -config COAP_ACK_TIMEOUT - int "Timeout in seconds for a response to a confirmable request" - default 2 +config COAP_ACK_TIMEOUT_MS + int "Timeout in milliseconds for a response to a confirmable request" + default 2000 help This value is for the response to the initial confirmable message. The timeout doubles for subsequent retries. To avoid synchronization of resends across hosts, the actual timeout is chosen randomly between - @ref CONFIG_COAP_ACK_TIMEOUT and - (@ref CONFIG_COAP_ACK_TIMEOUT * @ref CONFIG_COAP_RANDOM_FACTOR_1000 / 1000). + @ref CONFIG_COAP_ACK_TIMEOUT_MS and + (@ref CONFIG_COAP_ACK_TIMEOUT_MS * @ref CONFIG_COAP_RANDOM_FACTOR_1000 / 1000). The default of 2 seconds is taken from [RFC 7252, section 4.8](https://tools.ietf.org/html/rfc7252#section-4.8). diff --git a/sys/net/application_layer/gcoap/gcoap.c b/sys/net/application_layer/gcoap/gcoap.c index 370b47a142..346db30d7a 100644 --- a/sys/net/application_layer/gcoap/gcoap.c +++ b/sys/net/application_layer/gcoap/gcoap.c @@ -47,7 +47,7 @@ #define NO_IMMEDIATE_REPLY (-1) /* End of the range to pick a random timeout */ -#define TIMEOUT_RANGE_END (CONFIG_COAP_ACK_TIMEOUT * CONFIG_COAP_RANDOM_FACTOR_1000 / 1000) +#define TIMEOUT_RANGE_END (CONFIG_COAP_ACK_TIMEOUT_MS * CONFIG_COAP_RANDOM_FACTOR_1000 / 1000) /* Internal functions */ static void *_event_loop(void *arg); @@ -472,9 +472,9 @@ static void _on_resp_timeout(void *arg) { #else unsigned i = CONFIG_COAP_MAX_RETRANSMIT - memo->send_limit; #endif - uint32_t timeout = ((uint32_t)CONFIG_COAP_ACK_TIMEOUT << i) * MS_PER_SEC; + uint32_t timeout = (uint32_t)CONFIG_COAP_ACK_TIMEOUT_MS << i; #if CONFIG_COAP_RANDOM_FACTOR_1000 > 1000 - uint32_t end = ((uint32_t)TIMEOUT_RANGE_END << i) * MS_PER_SEC; + uint32_t end = (uint32_t)TIMEOUT_RANGE_END << i; timeout = random_uint32_range(timeout, end); #endif event_timeout_set(&memo->resp_evt_tmout, timeout); @@ -1158,9 +1158,9 @@ ssize_t gcoap_req_send(const uint8_t *buf, size_t len, } if (memo->msg.data.pdu_buf) { memo->send_limit = CONFIG_COAP_MAX_RETRANSMIT; - timeout = (uint32_t)CONFIG_COAP_ACK_TIMEOUT * MS_PER_SEC; + timeout = (uint32_t)CONFIG_COAP_ACK_TIMEOUT_MS; #if CONFIG_COAP_RANDOM_FACTOR_1000 > 1000 - timeout = random_uint32_range(timeout, TIMEOUT_RANGE_END * MS_PER_SEC); + timeout = random_uint32_range(timeout, TIMEOUT_RANGE_END); #endif memo->state = GCOAP_MEMO_RETRANSMIT; } diff --git a/sys/net/application_layer/nanocoap/sock.c b/sys/net/application_layer/nanocoap/sock.c index 415c7d4105..e984626005 100644 --- a/sys/net/application_layer/nanocoap/sock.c +++ b/sys/net/application_layer/nanocoap/sock.c @@ -49,7 +49,7 @@ ssize_t nanocoap_request(sock_udp_t *sock, coap_pkt_t *pkt, size_t len) /* TODO: timeout random between between ACK_TIMEOUT and (ACK_TIMEOUT * * ACK_RANDOM_FACTOR) */ - uint32_t timeout = CONFIG_COAP_ACK_TIMEOUT * US_PER_SEC; + uint32_t timeout = CONFIG_COAP_ACK_TIMEOUT_MS * US_PER_MS; /* add 1 for initial transmit */ unsigned tries_left = CONFIG_COAP_MAX_RETRANSMIT + 1;