From 8d391a4f5faec7b48fdf06625dfdd29abae072cb Mon Sep 17 00:00:00 2001 From: Benjamin Valentin Date: Thu, 14 Apr 2022 15:18:16 +0200 Subject: [PATCH 1/5] nanocoap_sock: use random timeout --- sys/net/application_layer/nanocoap/sock.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/sys/net/application_layer/nanocoap/sock.c b/sys/net/application_layer/nanocoap/sock.c index da72be37f4..fba50b8f5d 100644 --- a/sys/net/application_layer/nanocoap/sock.c +++ b/sys/net/application_layer/nanocoap/sock.c @@ -28,6 +28,7 @@ #include "net/nanocoap_sock.h" #include "net/sock/util.h" #include "net/sock/udp.h" +#include "random.h" #include "sys/uio.h" #include "timex.h" @@ -88,9 +89,8 @@ ssize_t nanocoap_sock_request_cb(nanocoap_sock_t *sock, coap_pkt_t *pkt, unsigned state = STATE_SEND_REQUEST; - /* TODO: timeout random between between ACK_TIMEOUT and (ACK_TIMEOUT * ACK_RANDOM_FACTOR) */ - uint32_t timeout = CONFIG_COAP_ACK_TIMEOUT_MS * US_PER_MS; - + uint32_t timeout = random_uint32_range(CONFIG_COAP_ACK_TIMEOUT_MS * US_PER_MS, + CONFIG_COAP_ACK_TIMEOUT_MS * CONFIG_COAP_RANDOM_FACTOR_1000); /* add 1 for initial transmit */ unsigned tries_left = CONFIG_COAP_MAX_RETRANSMIT + 1; From 76e2500b7d560ff75b1d58f7bfe2568d2dc7c6f8 Mon Sep 17 00:00:00 2001 From: Benjamin Valentin Date: Thu, 14 Apr 2022 15:40:29 +0200 Subject: [PATCH 2/5] nanocoap_sock: use random message IDs --- sys/net/application_layer/nanocoap/sock.c | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/sys/net/application_layer/nanocoap/sock.c b/sys/net/application_layer/nanocoap/sock.c index fba50b8f5d..04fa9a9e6c 100644 --- a/sys/net/application_layer/nanocoap/sock.c +++ b/sys/net/application_layer/nanocoap/sock.c @@ -25,6 +25,7 @@ #include #include +#include "atomic_utils.h" #include "net/nanocoap_sock.h" #include "net/sock/util.h" #include "net/sock/udp.h" @@ -46,6 +47,13 @@ typedef struct { bool more; } _block_ctx_t; +static uint16_t _get_id(void) +{ + __attribute__((section(".noinit"))) + static uint16_t id; + return atomic_fetch_add_u16(&id, 1); +} + int nanocoap_sock_connect(nanocoap_sock_t *sock, sock_udp_ep_t *local, sock_udp_ep_t *remote) { if (!remote->port) { @@ -231,7 +239,7 @@ ssize_t nanocoap_sock_get(nanocoap_sock_t *sock, const char *path, void *buf, si }; pkt.hdr = buf; - pktpos += coap_build_hdr(pkt.hdr, COAP_TYPE_CON, NULL, 0, COAP_METHOD_GET, 1); + pktpos += coap_build_hdr(pkt.hdr, COAP_TYPE_CON, NULL, 0, COAP_METHOD_GET, _get_id()); pktpos += coap_opt_put_uri_path(pktpos, 0, path); pkt.payload = pktpos; pkt.payload_len = 0; @@ -309,8 +317,7 @@ static int _fetch_block(coap_pkt_t *pkt, sock_udp_t *sock, uint8_t *pktpos = (void *)pkt->hdr; uint16_t lastonum = 0; - pktpos += coap_build_hdr(pkt->hdr, COAP_TYPE_CON, NULL, 0, COAP_METHOD_GET, - num); + pktpos += coap_build_hdr(pkt->hdr, COAP_TYPE_CON, NULL, 0, COAP_METHOD_GET, _get_id()); pktpos += coap_opt_put_uri_pathquery(pktpos, &lastonum, path); pktpos += coap_opt_put_uint(pktpos, lastonum, COAP_OPT_BLOCK2, (num << 4) | blksize); From 845a757eed4daf3499504ace1e20e6ab5e3a62c2 Mon Sep 17 00:00:00 2001 From: Benjamin Valentin Date: Tue, 12 Apr 2022 23:22:46 +0200 Subject: [PATCH 3/5] nanocoap: don't set lastonum if no option was added --- sys/net/application_layer/nanocoap/nanocoap.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/sys/net/application_layer/nanocoap/nanocoap.c b/sys/net/application_layer/nanocoap/nanocoap.c index bc04d68bb9..290e91bef3 100644 --- a/sys/net/application_layer/nanocoap/nanocoap.c +++ b/sys/net/application_layer/nanocoap/nanocoap.c @@ -812,13 +812,12 @@ size_t coap_opt_put_uri_pathquery(uint8_t *buf, uint16_t *lastonum, const char * size_t bytes_out = coap_opt_put_string_with_len(buf, *lastonum, COAP_OPT_URI_PATH, uri, len, '/'); - if (query) { buf += bytes_out; bytes_out += coap_opt_put_uri_query(buf, COAP_OPT_URI_PATH, query + 1); *lastonum = COAP_OPT_URI_QUERY; } - else { + else if (bytes_out) { *lastonum = COAP_OPT_URI_PATH; } From 5c1ff580d6eb237bc824399234eb8fbc21d23ef2 Mon Sep 17 00:00:00 2001 From: Benjamin Valentin Date: Wed, 20 Apr 2022 15:23:40 +0200 Subject: [PATCH 4/5] nanocoap_sock: don't decrement retries twice --- sys/net/application_layer/nanocoap/sock.c | 1 - 1 file changed, 1 deletion(-) diff --git a/sys/net/application_layer/nanocoap/sock.c b/sys/net/application_layer/nanocoap/sock.c index 04fa9a9e6c..1dc27d4a71 100644 --- a/sys/net/application_layer/nanocoap/sock.c +++ b/sys/net/application_layer/nanocoap/sock.c @@ -135,7 +135,6 @@ ssize_t nanocoap_sock_request_cb(nanocoap_sock_t *sock, coap_pkt_t *pkt, if (res == -ETIMEDOUT) { DEBUG("nanocoap: timeout\n"); timeout *= 2; - tries_left--; state = STATE_SEND_REQUEST; continue; } From e627d2575249465fd86991530d21976d2524482d Mon Sep 17 00:00:00 2001 From: Benjamin Valentin Date: Thu, 21 Apr 2022 14:38:16 +0200 Subject: [PATCH 5/5] nanocoap: constify get functions --- sys/include/net/nanocoap.h | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/sys/include/net/nanocoap.h b/sys/include/net/nanocoap.h index f8fdd7a5e3..1091fc4df0 100644 --- a/sys/include/net/nanocoap.h +++ b/sys/include/net/nanocoap.h @@ -366,7 +366,7 @@ static inline unsigned coap_get_code(const coap_pkt_t *pkt) * * @returns raw message code */ -static inline unsigned coap_get_code_raw(coap_pkt_t *pkt) +static inline unsigned coap_get_code_raw(const coap_pkt_t *pkt) { return (unsigned)pkt->hdr->code; } @@ -378,7 +378,7 @@ static inline unsigned coap_get_code_raw(coap_pkt_t *pkt) * * @returns message ID */ -static inline unsigned coap_get_id(coap_pkt_t *pkt) +static inline unsigned coap_get_id(const coap_pkt_t *pkt) { return ntohs(pkt->hdr->id); } @@ -429,7 +429,7 @@ static inline unsigned coap_get_total_len(const coap_pkt_t *pkt) * @returns COAP_TYPE_ACK * @returns COAP_TYPE_RST */ -static inline unsigned coap_get_type(coap_pkt_t *pkt) +static inline unsigned coap_get_type(const coap_pkt_t *pkt) { return (pkt->hdr->ver_t_tkl & 0x30) >> 4; } @@ -441,7 +441,7 @@ static inline unsigned coap_get_type(coap_pkt_t *pkt) * * @returns CoAP version number */ -static inline unsigned coap_get_ver(coap_pkt_t *pkt) +static inline unsigned coap_get_ver(const coap_pkt_t *pkt) { return (pkt->hdr->ver_t_tkl & 0x60) >> 6; } @@ -453,7 +453,7 @@ static inline unsigned coap_get_ver(coap_pkt_t *pkt) * * @returns pointer to first byte after the header */ -static inline uint8_t *coap_hdr_data_ptr(coap_hdr_t *hdr) +static inline uint8_t *coap_hdr_data_ptr(const coap_hdr_t *hdr) { return ((uint8_t *)hdr) + sizeof(coap_hdr_t); }