Skip to content

Commit 4a7ef5c

Browse files
SeppoTakalokartben
authored andcommitted
net: lwm2m: Convert tickeless to use eventfd()
For waking up the poll() use eventfd() instead of socketpair() so we save some buffer space and one file descriptor. Signed-off-by: Seppo Takalo <seppo.takalo@nordicsemi.no>
1 parent e4c21da commit 4a7ef5c

File tree

3 files changed

+19
-31
lines changed

3 files changed

+19
-31
lines changed
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1-
CONFIG_NET_SOCKETPAIR=y
1+
CONFIG_ZVFS_EVENTFD=y
2+
CONFIG_ZVFS_EVENTFD_MAX=2
23
CONFIG_LWM2M_TICKLESS=y

subsys/net/lib/lwm2m/lwm2m_engine.c

Lines changed: 15 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ LOG_MODULE_REGISTER(LOG_MODULE_NAME);
3333
#include <zephyr/sys/printk.h>
3434
#include <zephyr/types.h>
3535
#include <zephyr/posix/fcntl.h>
36+
#include <zephyr/zvfs/eventfd.h>
3637

3738
#if defined(CONFIG_LWM2M_DTLS_SUPPORT)
3839
#include <zephyr/net/tls_credentials.h>
@@ -105,7 +106,6 @@ static struct zsock_pollfd sock_fds[MAX_POLL_FD];
105106

106107
static struct lwm2m_ctx *sock_ctx[MAX_POLL_FD];
107108
static int sock_nfds;
108-
static int control_sock;
109109

110110
/* Resource wrappers */
111111
#if defined(CONFIG_LWM2M_COAP_BLOCK_TRANSFER)
@@ -128,7 +128,7 @@ static int lwm2m_socket_update(struct lwm2m_ctx *ctx);
128128
void lwm2m_engine_wake_up(void)
129129
{
130130
if (IS_ENABLED(CONFIG_LWM2M_TICKLESS)) {
131-
zsock_send(control_sock, &(char){0}, 1, 0);
131+
zvfs_eventfd_write(sock_fds[MAX_POLL_FD - 1].fd, 1);
132132
}
133133
}
134134

@@ -870,12 +870,12 @@ static void socket_loop(void *p1, void *p2, void *p3)
870870
for (i = 0; i < MAX_POLL_FD; i++) {
871871
short revents = sock_fds[i].revents;
872872

873-
if ((revents & ZSOCK_POLLIN) && sock_fds[i].fd != -1 &&
874-
sock_ctx[i] == NULL) {
873+
if (IS_ENABLED(CONFIG_LWM2M_TICKLESS) && (revents & ZSOCK_POLLIN) &&
874+
i == (MAX_POLL_FD - 1)) {
875875
/* This is the control socket, just read and ignore the data */
876-
char tmp;
876+
zvfs_eventfd_t tmp;
877877

878-
zsock_recv(sock_fds[i].fd, &tmp, 1, 0);
878+
zvfs_eventfd_read(sock_fds[i].fd, &tmp);
879879
continue;
880880
}
881881
if (sock_ctx[i] != NULL && sock_ctx[i]->sock_fd < 0) {
@@ -1348,31 +1348,17 @@ static int lwm2m_engine_init(void)
13481348
}
13491349

13501350
if (IS_ENABLED(CONFIG_LWM2M_TICKLESS)) {
1351-
/* Create socketpair that is used to wake zsock_poll() in the main loop */
1352-
int s[2];
1353-
int ret = zsock_socketpair(AF_UNIX, SOCK_STREAM, 0, s);
1351+
/* Create eventfd that is used to wake zsock_poll() in the main loop */
1352+
int efd = zvfs_eventfd(0, ZVFS_EFD_NONBLOCK);
13541353

1355-
if (ret) {
1356-
LOG_ERR("Error; socketpair() returned %d", ret);
1357-
return ret;
1358-
}
1359-
/* Last poll-handle is reserved for control socket */
1360-
sock_fds[MAX_POLL_FD - 1].fd = s[0];
1361-
control_sock = s[1];
1362-
ret = zsock_fcntl(s[0], F_SETFL, O_NONBLOCK);
1363-
if (ret) {
1364-
LOG_ERR("zsock_fcntl() %d", ret);
1365-
zsock_close(s[0]);
1366-
zsock_close(s[1]);
1367-
return ret;
1368-
}
1369-
ret = zsock_fcntl(s[1], F_SETFL, O_NONBLOCK);
1370-
if (ret) {
1371-
LOG_ERR("zsock_fcntl() %d", ret);
1372-
zsock_close(s[0]);
1373-
zsock_close(s[1]);
1374-
return ret;
1354+
if (efd == -1) {
1355+
int err = errno;
1356+
1357+
LOG_ERR("Error; eventfd() returned %d", err);
1358+
return -err;
13751359
}
1360+
/* Last poll-handle is reserved for control eventfd */
1361+
sock_fds[MAX_POLL_FD - 1].fd = efd;
13761362
}
13771363

13781364
lwm2m_clear_block_contexts();

tests/net/lib/lwm2m/interop/prj.conf

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ CONFIG_LWM2M_IPSO_SUPPORT=y
1919
CONFIG_LWM2M_SHELL=y
2020

2121
CONFIG_LWM2M_TICKLESS=y
22-
CONFIG_NET_SOCKETPAIR=y
22+
CONFIG_ZVFS_EVENTFD=y
23+
CONFIG_ZVFS_EVENTFD_MAX=2
2324

2425
#Enable test objects
2526
CONFIG_LWM2M_PORTFOLIO_OBJ_SUPPORT=y

0 commit comments

Comments
 (0)