1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2024-12-29 04:50:03 +01:00

Merge pull request #17977 from benpicco/nanocoap-fixes-min

nanocoap: follow-up fixes (uncontroversial parts)
This commit is contained in:
Marian Buschsieweke 2022-04-22 06:58:30 +02:00 committed by GitHub
commit d909d41df1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 19 additions and 14 deletions

View File

@ -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);
}

View File

@ -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;
}

View File

@ -25,9 +25,11 @@
#include <string.h>
#include <stdio.h>
#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);