Skip to content

Commit f8a7035

Browse files
tautologyclubdleach02
authored andcommitted
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]>
1 parent 832bbd8 commit f8a7035

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
@@ -550,19 +550,24 @@ static int handle_poll(void)
550550

551551
} else {
552552
for (int i = 0; i < nfds; i++) {
553+
553554
if (fds[i].revents & ZSOCK_POLLERR) {
554555
LOG_ERR("Error in poll for socket %d", fds[i].fd);
556+
clients[i]->socket_error = -EIO;
555557
}
556558
if (fds[i].revents & ZSOCK_POLLHUP) {
557559
LOG_ERR("Error in poll: POLLHUP for socket %d", fds[i].fd);
560+
clients[i]->socket_error = -ENOTCONN;
558561
}
559562
if (fds[i].revents & ZSOCK_POLLNVAL) {
560563
LOG_ERR("Error in poll: POLLNVAL - fd %d not open",
561564
fds[i].fd);
565+
clients[i]->socket_error = -EINVAL;
562566
}
563567
if (fds[i].revents & ZSOCK_POLLIN) {
564568
clients[i]->response_ready = true;
565569
}
570+
566571
}
567572

568573
return 0;
@@ -918,7 +923,24 @@ void coap_client_cancel_requests(struct coap_client *client)
918923
k_sleep(K_MSEC(COAP_PERIODIC_TIMEOUT));
919924
}
920925

921-
static void coap_client_recv(void *coap_cl, void *a, void *b)
926+
static void signal_socket_error(struct coap_client *cli)
927+
{
928+
for (int i = 0; i < CONFIG_COAP_CLIENT_MAX_REQUESTS; i++) {
929+
struct coap_client_internal_request *req = &cli->requests[i];
930+
931+
if (!req->request_ongoing) {
932+
continue;
933+
}
934+
935+
req->request_ongoing = false;
936+
if (req->coap_request.cb) {
937+
req->coap_request.cb(cli->socket_error, 0, NULL, 0,
938+
true, req->coap_request.user_data);
939+
}
940+
}
941+
}
942+
943+
void coap_client_recv(void *coap_cl, void *a, void *b)
922944
{
923945
int ret;
924946

@@ -959,6 +981,12 @@ static void coap_client_recv(void *coap_cl, void *a, void *b)
959981
clients[i]->response_ready = false;
960982
k_mutex_unlock(&clients[i]->lock);
961983
}
984+
985+
if (clients[i]->socket_error) {
986+
signal_socket_error(clients[i]);
987+
clients[i]->socket_error = 0;
988+
}
989+
962990
}
963991

964992
/* There are more messages coming */

0 commit comments

Comments
 (0)