Skip to content

Commit 715b973

Browse files
plskeggsaescolar
authored andcommitted
net: lib: coap: Make use of ZSOCK_MSG_TRUNC configurable
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 <peter.skeggs@nordicsemi.no>
1 parent 0ed8866 commit 715b973

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

subsys/net/lib/coap/Kconfig

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,14 @@ config COAP_CLIENT_MAX_REQUESTS
152152
help
153153
Maximum number of CoAP requests a single client can handle at a time
154154

155+
config COAP_CLIENT_TRUNCATE_MSGS
156+
bool "Receive notification when blocks are truncated"
157+
default y
158+
help
159+
Include ZSOCK_MSG_TRUNC in flags passed to zsock_recvfrom() to
160+
receive network stack notifications about block truncation.
161+
Otherwise it happens silently.
162+
155163
endif # COAP_CLIENT
156164

157165
config COAP_SERVER

subsys/net/lib/coap/coap_client.c

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -592,14 +592,21 @@ static int recv_response(struct coap_client *client, struct coap_packet *respons
592592
int total_len;
593593
int available_len;
594594
int ret;
595+
int flags = ZSOCK_MSG_DONTWAIT;
596+
597+
if (IS_ENABLED(CONFIG_COAP_CLIENT_TRUNCATE_MSGS)) {
598+
flags |= ZSOCK_MSG_TRUNC;
599+
}
595600

596601
memset(client->recv_buf, 0, sizeof(client->recv_buf));
597-
total_len = receive(client->fd, client->recv_buf, sizeof(client->recv_buf),
598-
ZSOCK_MSG_DONTWAIT | ZSOCK_MSG_TRUNC, &client->address,
599-
&client->socklen);
602+
total_len = receive(client->fd, client->recv_buf, sizeof(client->recv_buf), flags,
603+
&client->address, &client->socklen);
600604

601605
if (total_len < 0) {
602606
LOG_ERR("Error reading response: %d", errno);
607+
if (errno == EOPNOTSUPP) {
608+
return -errno;
609+
}
603610
return -EINVAL;
604611
} else if (total_len == 0) {
605612
LOG_ERR("Zero length recv");
@@ -937,6 +944,10 @@ static void coap_client_recv(void *coap_cl, void *a, void *b)
937944
LOG_ERR("Error receiving response");
938945
clients[i]->response_ready = false;
939946
k_mutex_unlock(&clients[i]->lock);
947+
if (ret == -EOPNOTSUPP) {
948+
LOG_ERR("Socket misconfigured.");
949+
goto idle;
950+
}
940951
continue;
941952
}
942953

0 commit comments

Comments
 (0)