Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions sys/include/net/nanocoap_sock.h
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@
* @param[in] len Payload length
*
* @retval 0 Success
* @retval -ECANCELED Request contained no-response option that did match the given @p code

Check warning on line 313 in sys/include/net/nanocoap_sock.h

View workflow job for this annotation

GitHub Actions / static-tests

line is longer than 100 characters
* @retval <0 Negative errno code indicating the error
*/
int nanocoap_server_send_separate(const nanocoap_server_response_ctx_t *ctx,
Expand Down Expand Up @@ -339,7 +339,7 @@
* @param[in] msg_id Message ID to send
*
* @return Length of the header build in bytes
* @retval -ECANCELED Request contained no-response option that did match the given @p code

Check warning on line 342 in sys/include/net/nanocoap_sock.h

View workflow job for this annotation

GitHub Actions / static-tests

line is longer than 100 characters
* @retval <0 Negative errno code indicating the error
*/
ssize_t nanocoap_server_build_separate(const nanocoap_server_response_ctx_t *ctx,
Expand Down Expand Up @@ -476,18 +476,17 @@
}

/**
* @brief Start a nanocoap server instance
* @brief Start a nanoCoAP server instance
*
* This function only returns if there's an error binding to @p local, or if
* receiving of UDP packets fails.
* This function only returns if there's an error binding to @p local.
*
* @param[in] local local UDP endpoint to bind to
* @param[in] buf input buffer to use
* @param[in] buf response buffer to use
* @param[in] bufsize size of @p buf
*
* @returns -1 on error
* @returns return code of @see sock_udp_create on error
*/
int nanocoap_server(sock_udp_ep_t *local, uint8_t *buf, size_t bufsize);
int nanocoap_server(sock_udp_ep_t *local, void *buf, size_t bufsize);

/**
* @brief Create and start the nanoCoAP server thread
Expand Down
3 changes: 3 additions & 0 deletions sys/net/application_layer/nanocoap/nanocoap.c
Original file line number Diff line number Diff line change
Expand Up @@ -778,6 +778,9 @@
ntohs(pkt->hdr->id));
len += payload_len;

/* HACK: many CoAP handlers assume that the pkt buffer is also used for the response */
pkt->hdr = (void *)rbuf;

return len;
}

Expand Down Expand Up @@ -983,7 +986,7 @@
return offset;
}

size_t coap_put_option(uint8_t *buf, uint16_t lastonum, uint16_t onum, const void *odata, size_t olen)

Check warning on line 989 in sys/net/application_layer/nanocoap/nanocoap.c

View workflow job for this annotation

GitHub Actions / static-tests

line is longer than 100 characters
{
assert(lastonum <= onum);

Expand Down
23 changes: 16 additions & 7 deletions sys/net/application_layer/nanocoap/sock.c
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@

/* random timeout, deadline for receive retries */
uint32_t timeout = random_uint32_range((uint32_t)CONFIG_COAP_ACK_TIMEOUT_MS * US_PER_MS,
(uint32_t)CONFIG_COAP_ACK_TIMEOUT_MS * CONFIG_COAP_RANDOM_FACTOR_1000);

Check warning on line 251 in sys/net/application_layer/nanocoap/sock.c

View workflow job for this annotation

GitHub Actions / static-tests

line is longer than 100 characters
uint32_t deadline = _deadline_from_interval(timeout);

/* check if we expect a reply */
Expand Down Expand Up @@ -1131,7 +1131,7 @@
return (res < 0) ? (ssize_t)res : (ssize_t)_buf.len;
}

int nanocoap_server(sock_udp_ep_t *local, uint8_t *buf, size_t bufsize)
int nanocoap_server(sock_udp_ep_t *local, void *rsp_buf, size_t rsp_buf_len)
{
sock_udp_t sock;
sock_udp_ep_t remote;
Expand All @@ -1145,11 +1145,20 @@

ssize_t res = sock_udp_create(&sock, local, NULL, 0);
if (res != 0) {
return -1;
return res;
}

void *buf;
void *buf_ctx = NULL;

while (1) {

if (buf_ctx) {
/* free the buffer */
res = sock_udp_recv_buf_aux(&sock, &buf, &buf_ctx, 0, NULL, NULL);
assert(res == 0);
}

sock_udp_aux_rx_t *aux_in_ptr = NULL;
#ifdef MODULE_SOCK_AUX_LOCAL
sock_udp_aux_rx_t aux_in = {
Expand All @@ -1158,14 +1167,14 @@
aux_in_ptr = &aux_in;
#endif

res = sock_udp_recv_aux(&sock, buf, bufsize, SOCK_NO_TIMEOUT,
&remote, aux_in_ptr);
res = sock_udp_recv_buf_aux(&sock, &buf, &buf_ctx, SOCK_NO_TIMEOUT,
&remote, aux_in_ptr);
if (res <= 0) {
DEBUG("nanocoap: error receiving UDP packet %" PRIdSIZE "\n", res);
continue;
}
coap_pkt_t pkt;
if (coap_parse(&pkt, (uint8_t *)buf, res) < 0) {
if (coap_parse(&pkt, buf, res) < 0) {
DEBUG("nanocoap: error parsing packet\n");
continue;
}
Expand All @@ -1183,12 +1192,12 @@
}
ctx.local = &aux_in.local;
#endif
if ((res = coap_handle_req(&pkt, buf, bufsize, &ctx)) <= 0) {
if ((res = coap_handle_req(&pkt, rsp_buf, rsp_buf_len, &ctx)) <= 0) {
DEBUG("nanocoap: error handling request %" PRIdSIZE "\n", res);
continue;
}

sock_udp_send_aux(&sock, buf, res, &remote, aux_out_ptr);
sock_udp_send_aux(&sock, rsp_buf, res, &remote, aux_out_ptr);
}

return 0;
Expand Down Expand Up @@ -1396,7 +1405,7 @@
&& (_observer_pool[i].response.tkl == coap_get_token_len(req_pkt))
&& !memcmp(_observer_pool[i].response.token, coap_get_token(req_pkt),
_observer_pool[i].response.tkl)
&& sock_udp_ep_equal(&_observer_pool[i].response.remote, coap_request_ctx_get_remote_udp(req_ctx))) {

Check warning on line 1408 in sys/net/application_layer/nanocoap/sock.c

View workflow job for this annotation

GitHub Actions / static-tests

line is longer than 100 characters
DEBUG("nanocoap: observer at index %" PRIuSIZE " unregistered\n", i);
_observer_pool[i].resource = NULL;
}
Expand Down Expand Up @@ -1427,7 +1436,7 @@
if (_observer_pool[i].resource == res) {
uint8_t rbuf[sizeof(coap_hdr_t) + COAP_TOKEN_LENGTH_MAX + 1];

ssize_t hdr_len = nanocoap_server_build_separate(&_observer_pool[i].response, rbuf, sizeof(rbuf),

Check warning on line 1439 in sys/net/application_layer/nanocoap/sock.c

View workflow job for this annotation

GitHub Actions / static-tests

line is longer than 100 characters
COAP_CODE_CONTENT, COAP_TYPE_NON,
++_observer_pool[i].msg_id);
if (hdr_len < 0) {
Expand Down
Loading