Skip to content

Commit

Permalink
nanocoap_sock: ensure ctx is reset before receiving new frame
Browse files Browse the repository at this point in the history
  • Loading branch information
benpicco committed Apr 20, 2022
1 parent 23372ce commit 90ca43d
Showing 1 changed file with 20 additions and 7 deletions.
27 changes: 20 additions & 7 deletions sys/net/application_layer/nanocoap/sock.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ typedef struct {
bool more;
} _block_ctx_t;


static uint16_t _get_id(void)
{
__attribute__((section(".noinit")))
Expand Down Expand Up @@ -93,7 +92,7 @@ ssize_t nanocoap_sock_request_cb(nanocoap_sock_t *sock, coap_pkt_t *pkt,
ssize_t tmp, res = 0;
size_t pdu_len = (pkt->payload - (uint8_t *)pkt->hdr) + pkt->payload_len;
uint8_t *buf = (uint8_t*)pkt->hdr;
uint32_t id = coap_get_id(pkt);
unsigned id = coap_get_id(pkt);
void *payload, *ctx = NULL;

unsigned state = STATE_SEND_REQUEST;
Expand All @@ -106,9 +105,13 @@ ssize_t nanocoap_sock_request_cb(nanocoap_sock_t *sock, coap_pkt_t *pkt,
/* check if we expect a reply */
const bool confirmable = coap_get_type(pkt) == COAP_TYPE_CON;

/* try receiving another packet without re-request */
bool retry_rx = false;

while (1) {
switch (state) {
case STATE_SEND_REQUEST:
DEBUG("nanocoap: send %u bytes (%u tries left)\n", pdu_len, tries_left);
if (--tries_left == 0) {
DEBUG("nanocoap: maximum retries reached\n");
return -ETIMEDOUT;
Expand All @@ -120,16 +123,24 @@ ssize_t nanocoap_sock_request_cb(nanocoap_sock_t *sock, coap_pkt_t *pkt,
return res;
}

if (confirmable || cb) {
state = STATE_AWAIT_RESPONSE;
} else {
/* no response needed and no response handler given */
if (!confirmable && !cb) {
return 0;
}

/* ctx must have been released at this point */
assert(ctx == NULL);
state = STATE_AWAIT_RESPONSE;
/* fall-through */
case STATE_AWAIT_RESPONSE:
DEBUG("nanocoap: waiting for response (timeout: %lu µs)\n", timeout);
tmp = sock_udp_recv_buf(sock, &payload, &ctx, timeout, NULL);
if (tmp == 0) {
/* no more data */
if (retry_rx) {
retry_rx = false;
continue;
}
return res;
}
res = tmp;
Expand All @@ -147,10 +158,12 @@ ssize_t nanocoap_sock_request_cb(nanocoap_sock_t *sock, coap_pkt_t *pkt,
/* parse response */
if (coap_parse(pkt, payload, res) < 0) {
DEBUG("nanocoap: error parsing packet\n");
res = -EBADMSG;
retry_rx = true;
continue;
}
else if (coap_get_id(pkt) != id) {
res = -EBADMSG;
DEBUG("nanocoap: ID mismatch %u != %u\n", coap_get_id(pkt), id);
retry_rx = true;
continue;
}

Expand Down

0 comments on commit 90ca43d

Please sign in to comment.