Skip to content

Commit 8848053

Browse files
tautologyclubSeppoTakalo
authored andcommitted
[nrf fromtree] net: coap_client: signal socket error to user
Before this patch, any unexpected socket error during poll (caused by LTE disconnects for instance) would lead to a infinite loop because the error state was never cleared, handled or even signaled to the user. Signed-off-by: Benjamin Lindqvist <[email protected]> (cherry picked from commit f8a7035)
1 parent d59b845 commit 8848053

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

include/zephyr/net/coap_client.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ struct coap_client {
115115
struct coap_client_internal_request requests[CONFIG_COAP_CLIENT_MAX_REQUESTS];
116116
struct coap_option echo_option;
117117
bool send_echo;
118+
int socket_error;
118119
};
119120
/** @endcond */
120121

subsys/net/lib/coap/coap_client.c

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -510,19 +510,24 @@ static int handle_poll(void)
510510

511511
} else {
512512
for (int i = 0; i < nfds; i++) {
513+
513514
if (fds[i].revents & ZSOCK_POLLERR) {
514515
LOG_ERR("Error in poll for socket %d", fds[i].fd);
516+
clients[i]->socket_error = -EIO;
515517
}
516518
if (fds[i].revents & ZSOCK_POLLHUP) {
517519
LOG_ERR("Error in poll: POLLHUP for socket %d", fds[i].fd);
520+
clients[i]->socket_error = -ENOTCONN;
518521
}
519522
if (fds[i].revents & ZSOCK_POLLNVAL) {
520523
LOG_ERR("Error in poll: POLLNVAL - fd %d not open",
521524
fds[i].fd);
525+
clients[i]->socket_error = -EINVAL;
522526
}
523527
if (fds[i].revents & ZSOCK_POLLIN) {
524528
clients[i]->response_ready = true;
525529
}
530+
526531
}
527532

528533
return 0;
@@ -877,7 +882,24 @@ void coap_client_cancel_requests(struct coap_client *client)
877882
k_sleep(K_MSEC(COAP_PERIODIC_TIMEOUT));
878883
}
879884

880-
static void coap_client_recv(void *coap_cl, void *a, void *b)
885+
static void signal_socket_error(struct coap_client *cli)
886+
{
887+
for (int i = 0; i < CONFIG_COAP_CLIENT_MAX_REQUESTS; i++) {
888+
struct coap_client_internal_request *req = &cli->requests[i];
889+
890+
if (!req->request_ongoing) {
891+
continue;
892+
}
893+
894+
req->request_ongoing = false;
895+
if (req->coap_request.cb) {
896+
req->coap_request.cb(cli->socket_error, 0, NULL, 0,
897+
true, req->coap_request.user_data);
898+
}
899+
}
900+
}
901+
902+
void coap_client_recv(void *coap_cl, void *a, void *b)
881903
{
882904
int ret;
883905

@@ -918,6 +940,12 @@ static void coap_client_recv(void *coap_cl, void *a, void *b)
918940
clients[i]->response_ready = false;
919941
k_mutex_unlock(&clients[i]->lock);
920942
}
943+
944+
if (clients[i]->socket_error) {
945+
signal_socket_error(clients[i]);
946+
clients[i]->socket_error = 0;
947+
}
948+
921949
}
922950

923951
/* There are more messages coming */

0 commit comments

Comments
 (0)