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); } 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; } diff --git a/sys/net/application_layer/nanocoap/sock.c b/sys/net/application_layer/nanocoap/sock.c index da72be37f4..1dc27d4a71 100644 --- a/sys/net/application_layer/nanocoap/sock.c +++ b/sys/net/application_layer/nanocoap/sock.c @@ -25,9 +25,11 @@ #include #include +#include "atomic_utils.h" #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" @@ -45,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) { @@ -88,9 +97,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; @@ -127,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; } @@ -231,7 +238,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 +316,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);