Skip to content

Commit

Permalink
fixup! sys/net/nanocoap: implement CoAP over TCP
Browse files Browse the repository at this point in the history
  • Loading branch information
maribu committed Dec 17, 2024
1 parent 7851f53 commit ec3b453
Showing 1 changed file with 33 additions and 15 deletions.
48 changes: 33 additions & 15 deletions sys/net/application_layer/nanocoap/sock.c
Original file line number Diff line number Diff line change
Expand Up @@ -1683,17 +1683,23 @@ static int _nanocoap_server_send_separate_udp(const nanocoap_server_response_ctx
return sock_udp_sendv_aux(NULL, &head, remote, aux_out_ptr);
}

static int _tcp_send_all(sock_tcp_t *sock, const void *_data, size_t len)
/* TODO: Implement sock_tcp_writev() to avoid sending the CoAP header and
* payload in separate TCP segments even if they would fit in a single one. */
static int _tcp_writev(sock_tcp_t *sock, const iolist_t *iol)
{
const uint8_t *data = _data;
ssize_t tmp;
while (len) {
tmp = sock_tcp_write(sock, data, len);
if (tmp < 0) {
return tmp;
while (iol) {
const uint8_t *data = iol->iol_base;
size_t len = iol->iol_len;
while (len) {
ssize_t tmp = sock_tcp_write(sock, data, len);
if (tmp < 0) {
return tmp;
}
data += tmp;
len -= tmp;
}
data += tmp;
len -= tmp;

iol = iol->iol_next;
}

return 0;
Expand All @@ -1713,17 +1719,29 @@ static int _nanocoap_server_send_separate_tcp(const nanocoap_server_response_ctx

ssize_t rbuf_len = coap_build_tcp_hdr(rbuf, sizeof(rbuf), ctx->token, ctx->tkl, code);

size_t data_len = (len > 0) ? (len + 1) : 0;
size_t shrunk = coap_finalize_tcp_header(rbuf, data_len);

if (rbuf_len < 0) {
return rbuf_len;
}

size_t shrunk = coap_finalize_tcp_header(rbuf, len);

int retval = _tcp_send_all(sock, rbuf + shrunk, rbuf_len - shrunk);
if (retval) {
return retval;
if (len) {
rbuf[rbuf_len++] = 0xff; /*payload marker */
}
return _tcp_send_all(sock, payload, len);

iolist_t data = {
.iol_base = (void *)payload,
.iol_len = len,
};

iolist_t head = {
.iol_next = &data,
.iol_base = rbuf + shrunk,
.iol_len = rbuf_len - shrunk,
};

return _tcp_writev(sock, &head);
}

int nanocoap_server_send_separate(const nanocoap_server_response_ctx_t *ctx,
Expand Down

0 comments on commit ec3b453

Please sign in to comment.