This is an automated email from the ASF dual-hosted git repository. jerpelea pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/nuttx.git
commit aff987ed08392a3a32eb9d15a30a8cbe5868a716 Author: zhangyuan29 <[email protected]> AuthorDate: Mon Aug 17 21:39:27 2026 +0800 drivers/serial/netdev_console.c: Use socket poll instead of timed polling. The netdev console pulled RX data by rescheduling a work queue every CONFIG_CONSOLE_NETDEV_DELAY_USEC, waking the LPWORK thread periodically even when no data arrived, which is unfriendly to low power. Replace the timed polling with a psock_poll callback so RX data is pulled on demand when the socket becomes readable, and drop the now unused CONSOLE_NETDEV_DELAY_USEC option. Assisted-by: Claude:claude-opus-4-8 Signed-off-by: zhangyuan29 <[email protected]> --- drivers/serial/Kconfig | 4 --- drivers/serial/netdev_console.c | 63 +++++++++++++++++++++++++---------------- 2 files changed, 38 insertions(+), 29 deletions(-) diff --git a/drivers/serial/Kconfig b/drivers/serial/Kconfig index 87ea8f2078e..50974888f2d 100644 --- a/drivers/serial/Kconfig +++ b/drivers/serial/Kconfig @@ -1079,10 +1079,6 @@ config CONSOLE_NETDEV_TXBUFSIZE int "CONSOLE netdev transmit buffer size" default 256 -config CONSOLE_NETDEV_DELAY_USEC - int "CONSOLE netdev polling interval in microseconds" - default 1000 - endif endif # SERIAL diff --git a/drivers/serial/netdev_console.c b/drivers/serial/netdev_console.c index 69dc4212d0a..2e43973a566 100644 --- a/drivers/serial/netdev_console.c +++ b/drivers/serial/netdev_console.c @@ -33,12 +33,12 @@ #include <net/if.h> #include <sys/types.h> #include <sys/socket.h> +#include <nuttx/poll.h> #include <netinet/in.h> #include <nuttx/signal.h> #include <nuttx/net/net.h> #include <nuttx/serial/serial.h> -#include <nuttx/wqueue.h> #ifdef CONFIG_NET_CAN # include <nuttx/can.h> @@ -47,12 +47,6 @@ # endif #endif -/**************************************************************************** - * Pre-processor definitions - ****************************************************************************/ - -#define CONSOLE_WORK_DELAY USEC2TICK(CONFIG_CONSOLE_NETDEV_DELAY_USEC) - /**************************************************************************** * Private Types ****************************************************************************/ @@ -65,7 +59,7 @@ struct netdev_consol_s uint16_t rxdgram_len; uint8_t rxdgram[CONFIG_CONSOLE_NETDEV_RXBUFSIZE]; bool rxint; - struct work_s work; + struct pollfd_s fds; }; /**************************************************************************** @@ -97,7 +91,7 @@ static void netdev_console_send(FAR struct uart_dev_s *dev, int ch); static void netdev_console_txint(FAR struct uart_dev_s *dev, bool enable); static bool netdev_console_txready(FAR struct uart_dev_s *dev); static bool netdev_console_txempty(FAR struct uart_dev_s *dev); -static void netdev_console_work(FAR void *arg); +static void netdev_console_poll(FAR struct pollfd_s *fds); /**************************************************************************** * Private Data @@ -297,6 +291,25 @@ static int netdev_console_attach(FAR struct uart_dev_s *dev) #endif } + if (ret < 0) + { + return ret; + } + + /* Register the poll callback so that received data wakes us up on demand + * instead of polling the socket periodically from a work queue. + */ + + priv->fds.arg = priv; + priv->fds.events = POLLIN; + priv->fds.cb = netdev_console_poll; + ret = psock_poll(&priv->psock, &priv->fds, true); + if (ret < 0) + { + psock_close(&priv->psock); + nerr("ERROR: Failed to setup socket poll: %d\n", ret); + } + return ret; } @@ -310,6 +323,7 @@ static void netdev_console_detach(FAR struct uart_dev_s *dev) priv->rxdgram_off = 0; priv->rxdgram_len = 0; + psock_poll(&priv->psock, &priv->fds, false); psock_close(&priv->psock); } @@ -322,14 +336,6 @@ static void netdev_console_rxint(FAR struct uart_dev_s *dev, bool enable) FAR struct netdev_consol_s *priv = (FAR struct netdev_consol_s *)dev; priv->rxint = enable; - if (enable) - { - work_queue(LPWORK, &priv->work, netdev_console_work, dev, 0); - } - else - { - work_cancel(LPWORK, &priv->work); - } } #ifdef CONFIG_SERIAL_IFLOWCONTROL @@ -501,20 +507,27 @@ static bool netdev_console_txempty(FAR struct uart_dev_s *dev) } /**************************************************************************** - * Name: netdev_console_work + * Name: netdev_console_poll + * + * Description: + * Socket poll callback. Invoked by the network stack when the console + * socket becomes readable, so that received data is pulled on demand + * rather than by periodically polling from a work queue. + * ****************************************************************************/ -static void netdev_console_work(FAR void *arg) +static void netdev_console_poll(FAR struct pollfd_s *fds) { - FAR struct netdev_consol_s *priv = arg; + FAR struct netdev_consol_s *priv = fds->arg; - if (priv->rxint) + if ((fds->revents & POLLIN) != 0) { - uart_dmarxfree(&priv->dev); + fds->revents &= ~POLLIN; + if (priv->rxint) + { + uart_dmarxfree(&priv->dev); + } } - - work_queue_next(LPWORK, &priv->work, netdev_console_work, arg, - CONSOLE_WORK_DELAY); } /****************************************************************************
