Skip to content

Commit

Permalink
net: lib: coap: Make use of ZSOCK_MSG_TRUNC configurable
Browse files Browse the repository at this point in the history
Not all offloaded network stacks support this socket option so
control it using a Kconfig CONFIG_COAP_CLIENT_TRUNCATE_MSGS,
and enable it by default.

Signed-off-by: Pete Skeggs <[email protected]>
  • Loading branch information
plskeggs authored and aescolar committed Oct 2, 2024
1 parent 0ed8866 commit 715b973
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
8 changes: 8 additions & 0 deletions subsys/net/lib/coap/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,14 @@ config COAP_CLIENT_MAX_REQUESTS
help
Maximum number of CoAP requests a single client can handle at a time

config COAP_CLIENT_TRUNCATE_MSGS
bool "Receive notification when blocks are truncated"
default y
help
Include ZSOCK_MSG_TRUNC in flags passed to zsock_recvfrom() to
receive network stack notifications about block truncation.
Otherwise it happens silently.

endif # COAP_CLIENT

config COAP_SERVER
Expand Down
17 changes: 14 additions & 3 deletions subsys/net/lib/coap/coap_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -592,14 +592,21 @@ static int recv_response(struct coap_client *client, struct coap_packet *respons
int total_len;
int available_len;
int ret;
int flags = ZSOCK_MSG_DONTWAIT;

if (IS_ENABLED(CONFIG_COAP_CLIENT_TRUNCATE_MSGS)) {
flags |= ZSOCK_MSG_TRUNC;
}

memset(client->recv_buf, 0, sizeof(client->recv_buf));
total_len = receive(client->fd, client->recv_buf, sizeof(client->recv_buf),
ZSOCK_MSG_DONTWAIT | ZSOCK_MSG_TRUNC, &client->address,
&client->socklen);
total_len = receive(client->fd, client->recv_buf, sizeof(client->recv_buf), flags,
&client->address, &client->socklen);

if (total_len < 0) {
LOG_ERR("Error reading response: %d", errno);
if (errno == EOPNOTSUPP) {
return -errno;
}
return -EINVAL;
} else if (total_len == 0) {
LOG_ERR("Zero length recv");
Expand Down Expand Up @@ -937,6 +944,10 @@ static void coap_client_recv(void *coap_cl, void *a, void *b)
LOG_ERR("Error receiving response");
clients[i]->response_ready = false;
k_mutex_unlock(&clients[i]->lock);
if (ret == -EOPNOTSUPP) {
LOG_ERR("Socket misconfigured.");
goto idle;
}
continue;
}

Expand Down

0 comments on commit 715b973

Please sign in to comment.