Skip to content

Commit

Permalink
nanocoap_sock: cosmetic & functional cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
benpicco committed Apr 20, 2022
1 parent 90ca43d commit ba15a6e
Showing 1 changed file with 27 additions and 42 deletions.
69 changes: 27 additions & 42 deletions sys/net/application_layer/nanocoap/sock.c
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,9 @@ ssize_t nanocoap_sock_request_cb(nanocoap_sock_t *sock, coap_pkt_t *pkt,
coap_request_cb_t cb, void *arg)
{
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;
unsigned id = coap_get_id(pkt);
const void *pdu = pkt->hdr;
const size_t pdu_len = coap_get_total_len(pkt);
const unsigned id = coap_get_id(pkt);
void *payload, *ctx = NULL;

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

res = sock_udp_send(sock, buf, pdu_len, NULL);
res = sock_udp_send(sock, pdu, pdu_len, NULL);
if (res <= 0) {
DEBUG("nanocoap: error sending coap request, %d\n", (int)res);
return res;
Expand All @@ -133,7 +133,7 @@ ssize_t nanocoap_sock_request_cb(nanocoap_sock_t *sock, coap_pkt_t *pkt,
state = STATE_AWAIT_RESPONSE;
/* fall-through */
case STATE_AWAIT_RESPONSE:
DEBUG("nanocoap: waiting for response (timeout: %lu µs)\n", timeout);
DEBUG("nanocoap: waiting for response (timeout: %"PRIu32" µs)\n", timeout);
tmp = sock_udp_recv_buf(sock, &payload, &ctx, timeout, NULL);
if (tmp == 0) {
/* no more data */
Expand Down Expand Up @@ -167,6 +167,7 @@ ssize_t nanocoap_sock_request_cb(nanocoap_sock_t *sock, coap_pkt_t *pkt,
continue;
}

DEBUG("nanocoap: response code=%i\n", coap_get_code(pkt));
switch (coap_get_type(pkt)) {
case COAP_TYPE_RST:
/* TODO: handle different? */
Expand All @@ -180,7 +181,7 @@ ssize_t nanocoap_sock_request_cb(nanocoap_sock_t *sock, coap_pkt_t *pkt,
if (cb) {
res = cb(arg, pkt);
} else {
res = 0;
res = _get_error(pkt);
}
break;
}
Expand Down Expand Up @@ -242,32 +243,23 @@ static int _get_cb(void *arg, coap_pkt_t *pkt)

ssize_t nanocoap_sock_get(nanocoap_sock_t *sock, const char *path, void *buf, size_t len)
{
ssize_t res;
coap_pkt_t pkt;
uint8_t *pktpos = buf;
coap_pkt_t pkt = {
.hdr = buf,
};

struct iovec ctx = {
.iov_base = buf,
.iov_len = len,
};

pkt.hdr = buf;
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;

res = nanocoap_sock_request_cb(sock, &pkt, _get_cb, &ctx);
if (res < 0) {
return res;
}

DEBUG("code=%i\n", coap_get_code(&pkt));
if (_get_error(&pkt)) {
return _get_error(&pkt);
}

return res;
return nanocoap_sock_request_cb(sock, &pkt, _get_cb, &ctx);
}

ssize_t nanocoap_request(coap_pkt_t *pkt, sock_udp_ep_t *local,
Expand Down Expand Up @@ -319,33 +311,30 @@ static int _block_cb(void *arg, coap_pkt_t *pkt)
block2.more = false;
}

ctx->more = block2.more;
ctx->more = block2.more > 0;
return ctx->callback(ctx->arg, block2.offset, pkt->payload, pkt->payload_len, block2.more);
}

static int _fetch_block(coap_pkt_t *pkt, sock_udp_t *sock,
static int _fetch_block(nanocoap_sock_t *sock, uint8_t *buf, size_t len,
const char *path, coap_blksize_t blksize, unsigned num,
_block_ctx_t *ctx)
{
uint8_t *pktpos = (void *)pkt->hdr;
coap_pkt_t pkt = {
.hdr = (void *)buf,
};
uint16_t lastonum = 0;

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);
buf += coap_build_hdr(pkt.hdr, COAP_TYPE_CON, NULL, 0, COAP_METHOD_GET, _get_id());
buf += coap_opt_put_uri_pathquery(buf, &lastonum, path);
buf += coap_opt_put_uint(buf, lastonum, COAP_OPT_BLOCK2, (num << 4) | blksize);

pkt->payload = pktpos;
pkt->payload_len = 0;
(void)len;
assert((uintptr_t)buf - (uintptr_t)pkt.hdr < len);

int res = nanocoap_sock_request_cb(sock, pkt, _block_cb, ctx);
if (res < 0) {
return res;
}

DEBUG("code=%i\n", coap_get_code(pkt));
pkt.payload = buf;
pkt.payload_len = 0;

return _get_error(pkt);
return nanocoap_sock_request_cb(sock, &pkt, _block_cb, ctx);
}

int nanocoap_sock_get_blockwise(nanocoap_sock_t *sock, const char *path,
Expand All @@ -362,13 +351,9 @@ int nanocoap_sock_get_blockwise(nanocoap_sock_t *sock, const char *path,

unsigned num = 0;
while (ctx.more) {
coap_pkt_t pkt = {
.hdr = (void *)buf,
};

DEBUG("fetching block %u\n", num);
int res = _fetch_block(&pkt, sock, path, blksize, num, &ctx);

int res = _fetch_block(sock, buf, sizeof(buf), path, blksize, num, &ctx);
if (res) {
DEBUG("error fetching block %u: %d\n", num, res);
return res;
Expand Down

0 comments on commit ba15a6e

Please sign in to comment.