Skip to content

Commit b0402a1

Browse files
committed
[nrf fromtree] net: lib: coap_client: Stop polling on unneeded sockets
poll() only for sockets that have traffic ongoing or have some lifetime left. On socket failures during a poll(), stop listening for the socket. Application can recover by reconnecting the socket. Signed-off-by: Seppo Takalo <[email protected]> (cherry picked from commit f0c6efe)
1 parent 7bd04f5 commit b0402a1

File tree

1 file changed

+43
-13
lines changed

1 file changed

+43
-13
lines changed

subsys/net/lib/coap/coap_client.c

Lines changed: 43 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -532,6 +532,17 @@ static void coap_client_resend_handler(struct coap_client *client)
532532
k_mutex_unlock(&client->lock);
533533
}
534534

535+
static struct coap_client *get_client(int sock)
536+
{
537+
for (int i = 0; i < num_clients; i++) {
538+
if (clients[i]->fd == sock) {
539+
return clients[i];
540+
}
541+
}
542+
543+
return NULL;
544+
}
545+
535546
static int handle_poll(void)
536547
{
537548
int ret = 0;
@@ -541,10 +552,16 @@ static int handle_poll(void)
541552

542553
/* Use periodic timeouts */
543554
for (int i = 0; i < num_clients; i++) {
544-
fds[i].fd = clients[i]->fd;
545-
fds[i].events = (has_ongoing_exchange(clients[i]) ? ZSOCK_POLLIN : 0) |
546-
(has_timeout_expired(clients[i]) ? ZSOCK_POLLOUT : 0);
547-
fds[i].revents = 0;
555+
short events = (has_ongoing_exchange(clients[i]) ? ZSOCK_POLLIN : 0) |
556+
(has_timeout_expired(clients[i]) ? ZSOCK_POLLOUT : 0);
557+
558+
if (events == 0) {
559+
/* Skip this socket */
560+
continue;
561+
}
562+
fds[nfds].fd = clients[i]->fd;
563+
fds[nfds].events = events;
564+
fds[nfds].revents = 0;
548565
nfds++;
549566
}
550567

@@ -559,42 +576,49 @@ static int handle_poll(void)
559576
}
560577

561578
for (int i = 0; i < nfds; i++) {
579+
struct coap_client *client = get_client(fds[i].fd);
580+
581+
if (!client) {
582+
LOG_ERR("No client found for socket %d", fds[i].fd);
583+
continue;
584+
}
585+
562586
if (fds[i].revents & ZSOCK_POLLOUT) {
563-
coap_client_resend_handler(clients[i]);
587+
coap_client_resend_handler(client);
564588
}
565589
if (fds[i].revents & ZSOCK_POLLIN) {
566590
struct coap_packet response;
567591
bool response_truncated = false;
568592

569-
ret = recv_response(clients[i], &response, &response_truncated);
593+
ret = recv_response(client, &response, &response_truncated);
570594
if (ret < 0) {
571595
if (ret == -EAGAIN) {
572596
continue;
573597
}
574598
LOG_ERR("Error receiving response");
575-
cancel_requests_with(clients[i], -EIO);
599+
cancel_requests_with(client, -EIO);
576600
continue;
577601
}
578602

579-
k_mutex_lock(&clients[i]->lock, K_FOREVER);
580-
ret = handle_response(clients[i], &response, response_truncated);
603+
k_mutex_lock(&client->lock, K_FOREVER);
604+
ret = handle_response(client, &response, response_truncated);
581605
if (ret < 0) {
582606
LOG_ERR("Error handling response");
583607
}
584608

585-
k_mutex_unlock(&clients[i]->lock);
609+
k_mutex_unlock(&client->lock);
586610
}
587611
if (fds[i].revents & ZSOCK_POLLERR) {
588612
LOG_ERR("Error in poll for socket %d", fds[i].fd);
589-
cancel_requests_with(clients[i], -EIO);
613+
cancel_requests_with(client, -EIO);
590614
}
591615
if (fds[i].revents & ZSOCK_POLLHUP) {
592616
LOG_ERR("Error in poll: POLLHUP for socket %d", fds[i].fd);
593-
cancel_requests_with(clients[i], -EIO);
617+
cancel_requests_with(client, -EIO);
594618
}
595619
if (fds[i].revents & ZSOCK_POLLNVAL) {
596620
LOG_ERR("Error in poll: POLLNVAL - fd %d not open", fds[i].fd);
597-
cancel_requests_with(clients[i], -EIO);
621+
cancel_requests_with(client, -EIO);
598622
}
599623
}
600624

@@ -974,6 +998,12 @@ static void cancel_requests_with(struct coap_client *client, int error)
974998
report_callback_error(&client->requests[i], error);
975999
release_internal_request(&client->requests[i]);
9761000
}
1001+
/* If our socket has failed, clear all requests, even completed ones,
1002+
* so that our handle_poll() does not poll() anymore for this socket.
1003+
*/
1004+
if (error == -EIO) {
1005+
reset_internal_request(&client->requests[i]);
1006+
}
9771007
}
9781008
k_mutex_unlock(&client->lock);
9791009

0 commit comments

Comments
 (0)