Skip to content

Commit ec3b453

Browse files
committed
fixup! sys/net/nanocoap: implement CoAP over TCP
1 parent 7851f53 commit ec3b453

File tree

1 file changed

+33
-15
lines changed
  • sys/net/application_layer/nanocoap

1 file changed

+33
-15
lines changed

sys/net/application_layer/nanocoap/sock.c

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1683,17 +1683,23 @@ static int _nanocoap_server_send_separate_udp(const nanocoap_server_response_ctx
16831683
return sock_udp_sendv_aux(NULL, &head, remote, aux_out_ptr);
16841684
}
16851685

1686-
static int _tcp_send_all(sock_tcp_t *sock, const void *_data, size_t len)
1686+
/* TODO: Implement sock_tcp_writev() to avoid sending the CoAP header and
1687+
* payload in separate TCP segments even if they would fit in a single one. */
1688+
static int _tcp_writev(sock_tcp_t *sock, const iolist_t *iol)
16871689
{
1688-
const uint8_t *data = _data;
1689-
ssize_t tmp;
1690-
while (len) {
1691-
tmp = sock_tcp_write(sock, data, len);
1692-
if (tmp < 0) {
1693-
return tmp;
1690+
while (iol) {
1691+
const uint8_t *data = iol->iol_base;
1692+
size_t len = iol->iol_len;
1693+
while (len) {
1694+
ssize_t tmp = sock_tcp_write(sock, data, len);
1695+
if (tmp < 0) {
1696+
return tmp;
1697+
}
1698+
data += tmp;
1699+
len -= tmp;
16941700
}
1695-
data += tmp;
1696-
len -= tmp;
1701+
1702+
iol = iol->iol_next;
16971703
}
16981704

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

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

1722+
size_t data_len = (len > 0) ? (len + 1) : 0;
1723+
size_t shrunk = coap_finalize_tcp_header(rbuf, data_len);
1724+
17161725
if (rbuf_len < 0) {
17171726
return rbuf_len;
17181727
}
17191728

1720-
size_t shrunk = coap_finalize_tcp_header(rbuf, len);
1721-
1722-
int retval = _tcp_send_all(sock, rbuf + shrunk, rbuf_len - shrunk);
1723-
if (retval) {
1724-
return retval;
1729+
if (len) {
1730+
rbuf[rbuf_len++] = 0xff; /*payload marker */
17251731
}
1726-
return _tcp_send_all(sock, payload, len);
1732+
1733+
iolist_t data = {
1734+
.iol_base = (void *)payload,
1735+
.iol_len = len,
1736+
};
1737+
1738+
iolist_t head = {
1739+
.iol_next = &data,
1740+
.iol_base = rbuf + shrunk,
1741+
.iol_len = rbuf_len - shrunk,
1742+
};
1743+
1744+
return _tcp_writev(sock, &head);
17271745
}
17281746

17291747
int nanocoap_server_send_separate(const nanocoap_server_response_ctx_t *ctx,

0 commit comments

Comments
 (0)