This is an automated email from the ASF dual-hosted git repository. xiaoxiang781216 pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/nuttx.git
commit 1c6d8639abbc6be6a42edf7f44bb9f309f2b93de Author: p-szafonimateusz <[email protected]> AuthorDate: Mon Nov 9 11:51:10 2026 +0100 net/can: Move can_input.c into can.c. Move the code of can_input.c into can.c without changes and remove can_input.c. Only the include lists and the file guards are merged; the functions are copied as they are. Assisted-by: Claude:claude-opus-5-5 Signed-off-by: p-szafonimateusz <[email protected]> --- .github/CODEOWNERS | 1 - Documentation/os/networking/socketcan.rst | 2 +- net/can/CMakeLists.txt | 2 +- net/can/Make.defs | 1 - net/can/can.c | 283 +++++++++++++++++++++++++ net/can/can_input.c | 333 ------------------------------ 6 files changed, 285 insertions(+), 337 deletions(-) diff --git a/.github/CODEOWNERS b/.github/CODEOWNERS index 95da9e2ea29..ca6e0183c82 100644 --- a/.github/CODEOWNERS +++ b/.github/CODEOWNERS @@ -15893,7 +15893,6 @@ net/can/Kconfig [email protected] [email protected] [email protected] raide net/can/can.c [email protected] [email protected] [email protected] [email protected] [email protected] net/can/can.h [email protected] [email protected] [email protected] [email protected] [email protected] net/can/can_getsockopt.c [email protected] [email protected] [email protected] [email protected] [email protected] -net/can/can_input.c [email protected] [email protected] [email protected] [email protected] [email protected] net/can/can_notifier.c [email protected] [email protected] net/can/can_poll.c [email protected] [email protected] [email protected] [email protected] [email protected] net/can/can_recvmsg.c [email protected] [email protected] [email protected] [email protected] [email protected] diff --git a/Documentation/os/networking/socketcan.rst b/Documentation/os/networking/socketcan.rst index 6ef87f71233..dbee99409ab 100644 --- a/Documentation/os/networking/socketcan.rst +++ b/Documentation/os/networking/socketcan.rst @@ -92,7 +92,7 @@ Files supporting SocketCAN can be found in the following locations: - **Header File**: ``include/nuttx/net/can.h`` - **Main Modules**: - - ``can.c`` - Connection management and callbacks + - ``can.c`` - Connection management, input and callbacks - ``can_sockif.c`` - Socket interface implementation - ``can_sendmsg.c`` - Message transmission - ``can_sendmsg_buffered.c`` - Message transmission with buffering diff --git a/net/can/CMakeLists.txt b/net/can/CMakeLists.txt index e7cc230b23e..93797acd90e 100644 --- a/net/can/CMakeLists.txt +++ b/net/can/CMakeLists.txt @@ -43,7 +43,7 @@ if(CONFIG_NET_CAN) list(APPEND SRCS can_bufpool.c) endif() - list(APPEND SRCS can.c can_input.c can_poll.c) + list(APPEND SRCS can.c can_poll.c) target_sources(net PRIVATE ${SRCS}) endif() diff --git a/net/can/Make.defs b/net/can/Make.defs index d296a6bc1e4..e2085265cef 100644 --- a/net/can/Make.defs +++ b/net/can/Make.defs @@ -50,7 +50,6 @@ endif endif NET_CSRCS += can.c -NET_CSRCS += can_input.c NET_CSRCS += can_poll.c # Include can build support diff --git a/net/can/can.c b/net/can/can.c index ee7317dfd2c..798c9bcf237 100644 --- a/net/can/can.c +++ b/net/can/can.c @@ -74,6 +74,98 @@ NET_BUFPOOL_DECLARE(g_can_connections, sizeof(struct can_conn_s), static dq_queue_t g_active_can_connections; +/**************************************************************************** + * Public Data + ****************************************************************************/ + +const uint8_t g_can_dlc_to_len[16] = +{ + 0, + 1, + 2, + 3, + 4, + 5, + 6, + 7, + 8, + 12, + 16, + 20, + 24, + 32, + 48, + 64, +}; +const uint8_t g_len_to_can_dlc[65] = +{ + 0, + 1, + 2, + 3, + 4, + 5, + 6, + 7, + 8, + 9, + 9, + 9, + 9, + 10, + 10, + 10, + 10, + 11, + 11, + 11, + 11, + 12, + 12, + 12, + 12, + 13, + 13, + 13, + 13, + 13, + 13, + 13, + 13, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 14, + 15, + 15, + 15, + 15, + 15, + 15, + 15, + 15, + 15, + 15, + 15, + 15, + 15, + 15, + 15, + 15, +}; + /**************************************************************************** * Private Functions ****************************************************************************/ @@ -127,6 +219,140 @@ can_data_event(FAR struct net_driver_s *dev, FAR struct can_conn_s *conn, return ret; } +/**************************************************************************** + * Name: can_input_conn + * + * Description: + * Handle incoming packet input + * + * Input Parameters: + * dev - The device driver structure containing the received packet + * conn - A pointer to the CAN connection structure + * + * Returned Value: + * OK The packet has been processed and can be deleted + * -EAGAIN There is a matching connection, but could not dispatch the packet + * yet. Useful when a packet arrives before a recv call is in + * place. + * + * Assumptions: + * This function can be called from an interrupt. + * + ****************************************************************************/ + +static int can_input_conn(FAR struct net_driver_s *dev, + FAR struct can_conn_s *conn) +{ + uint32_t flags; + uint16_t buflen = dev->d_len; + int ret = OK; + + /* Setup for the application callback */ + + dev->d_appdata = dev->d_buf; + dev->d_sndlen = 0; + dev->d_len = buflen; + + /* Perform the application callback */ + + flags = can_callback(dev, conn, CAN_NEWDATA); + + /* If the operation was successful, the CAN_NEWDATA flag is removed + * and thus the packet can be deleted (OK will be returned). + */ + + if ((flags & CAN_NEWDATA) != 0) + { + /* No.. the packet was not processed now. Return -EAGAIN so + * that the driver may retry again later. We still need to + * set d_len to zero so that the driver is aware that there + * is nothing to be sent. + */ + + nwarn("WARNING: Packet not processed\n"); + ret = -EAGAIN; + } + + return ret; +} + +/**************************************************************************** + * Name: can_in + * + * Description: + * Handle incoming packet input + * + * Input Parameters: + * dev - The device driver structure containing the received packet + * + * Returned Value: + * OK The packet has been processed and can be deleted + * -EAGAIN There is a matching connection, but could not dispatch the packet + * yet. Useful when a packet arrives before a recv call is in + * place. + * + * Assumptions: + * This function can be called from an interrupt. + * + ****************************************************************************/ + +static int can_in(FAR struct net_driver_s *dev) +{ + FAR struct can_conn_s *conn = can_active(dev, NULL); + FAR struct can_conn_s *nextconn; + int ret; + + if (conn == NULL) + { + /* There is no listener on the dev. Just drop the packet. */ + + return OK; + } + + /* Storing reception timestamp provided by realtime + * if timestamp no provided by hardware. + */ + +#ifdef CONFIG_NET_TIMESTAMP + if ((dev->d_features & NETDEV_RX_STAMP) == 0) + { + clock_gettime(CLOCK_REALTIME, &dev->d_iob->io_time); + } +#endif /* CONFIG_NET_TIMESTAMP */ + + can_conn_list_lock(); + + /* Do we have second connection that can hold this packet? */ + + while ((nextconn = can_active(dev, conn)) != NULL) + { + /* Yes... There are multiple listeners on the same dev. + * We need to clone the packet and deliver it to each listener. + */ + + FAR struct iob_s *iob = can_iob_clone(dev); + + if (iob == NULL) + { + nerr("ERROR: IOB clone failed.\n"); + break; /* We can still process one time without clone. */ + } + + can_input_conn(dev, conn); + + netdev_iob_replace(dev, iob); + conn = nextconn; + } + + /* We can deliver the packet directly to the last listener. */ + + ret = can_input_conn(dev, conn); + + can_conn_list_unlock(); + + return ret; +} + /**************************************************************************** * Public Functions ****************************************************************************/ @@ -254,6 +480,63 @@ errout: return ret; } +/**************************************************************************** + * Name: can_input + * + * Description: + * Handle incoming packet input + * + * Input Parameters: + * dev - The device driver structure containing the received packet + * + * Returned Value: + * OK The packet has been processed and can be deleted + * -EAGAIN There is a matching connection, but could not dispatch the packet + * yet. Useful when a packet arrives before a recv call is in + * place. + * + * Assumptions: + * This function can be called from an interrupt. + * + ****************************************************************************/ + +int can_input(FAR struct net_driver_s *dev) +{ + FAR uint8_t *buf; + int ret; + +#ifdef CONFIG_NET_STATISTICS + g_netstats.can.recv++; +#endif + netdev_lock(dev); + + if (dev->d_iob != NULL) + { + buf = dev->d_buf; + + /* Set the device buffer to l2 */ + + dev->d_buf = NETLLBUF; + ret = can_in(dev); + + dev->d_buf = buf; + + netdev_unlock(dev); + return ret; + } + + ret = netdev_input(dev, can_in, false); + if (ret < 0) + { +#ifdef CONFIG_NET_STATISTICS + g_netstats.can.drop++; +#endif + } + + netdev_unlock(dev); + return ret; +} + /**************************************************************************** * Name: can_alloc() * diff --git a/net/can/can_input.c b/net/can/can_input.c deleted file mode 100644 index 315186c5852..00000000000 --- a/net/can/can_input.c +++ /dev/null @@ -1,333 +0,0 @@ -/**************************************************************************** - * net/can/can_input.c - * - * SPDX-License-Identifier: Apache-2.0 - * - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. The - * ASF licenses this file to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance with the - * License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations - * under the License. - * - ****************************************************************************/ - -/**************************************************************************** - * Included Files - ****************************************************************************/ - -#include <nuttx/config.h> -#if defined(CONFIG_NET) && defined(CONFIG_NET_CAN) - -#include <errno.h> -#include <nuttx/debug.h> - -#include <nuttx/net/netdev.h> -#include <nuttx/net/can.h> -#include <nuttx/net/netstats.h> -#include <nuttx/net/net.h> - -#include "devif/devif.h" -#include "utils/utils.h" -#include "can/can.h" - -/**************************************************************************** - * Public Data - ****************************************************************************/ - -const uint8_t g_can_dlc_to_len[16] = -{ - 0, - 1, - 2, - 3, - 4, - 5, - 6, - 7, - 8, - 12, - 16, - 20, - 24, - 32, - 48, - 64, -}; -const uint8_t g_len_to_can_dlc[65] = -{ - 0, - 1, - 2, - 3, - 4, - 5, - 6, - 7, - 8, - 9, - 9, - 9, - 9, - 10, - 10, - 10, - 10, - 11, - 11, - 11, - 11, - 12, - 12, - 12, - 12, - 13, - 13, - 13, - 13, - 13, - 13, - 13, - 13, - 14, - 14, - 14, - 14, - 14, - 14, - 14, - 14, - 14, - 14, - 14, - 14, - 14, - 14, - 14, - 14, - 15, - 15, - 15, - 15, - 15, - 15, - 15, - 15, - 15, - 15, - 15, - 15, - 15, - 15, - 15, - 15, -}; - -/**************************************************************************** - * Private Functions - ****************************************************************************/ - -/**************************************************************************** - * Name: can_input_conn - * - * Description: - * Handle incoming packet input - * - * Input Parameters: - * dev - The device driver structure containing the received packet - * conn - A pointer to the CAN connection structure - * - * Returned Value: - * OK The packet has been processed and can be deleted - * -EAGAIN There is a matching connection, but could not dispatch the packet - * yet. Useful when a packet arrives before a recv call is in - * place. - * - * Assumptions: - * This function can be called from an interrupt. - * - ****************************************************************************/ - -static int can_input_conn(FAR struct net_driver_s *dev, - FAR struct can_conn_s *conn) -{ - uint32_t flags; - uint16_t buflen = dev->d_len; - int ret = OK; - - /* Setup for the application callback */ - - dev->d_appdata = dev->d_buf; - dev->d_sndlen = 0; - dev->d_len = buflen; - - /* Perform the application callback */ - - flags = can_callback(dev, conn, CAN_NEWDATA); - - /* If the operation was successful, the CAN_NEWDATA flag is removed - * and thus the packet can be deleted (OK will be returned). - */ - - if ((flags & CAN_NEWDATA) != 0) - { - /* No.. the packet was not processed now. Return -EAGAIN so - * that the driver may retry again later. We still need to - * set d_len to zero so that the driver is aware that there - * is nothing to be sent. - */ - - nwarn("WARNING: Packet not processed\n"); - ret = -EAGAIN; - } - - return ret; -} - -/**************************************************************************** - * Public Functions - ****************************************************************************/ - -/**************************************************************************** - * Name: can_in - * - * Description: - * Handle incoming packet input - * - * Input Parameters: - * dev - The device driver structure containing the received packet - * - * Returned Value: - * OK The packet has been processed and can be deleted - * -EAGAIN There is a matching connection, but could not dispatch the packet - * yet. Useful when a packet arrives before a recv call is in - * place. - * - * Assumptions: - * This function can be called from an interrupt. - * - ****************************************************************************/ - -static int can_in(FAR struct net_driver_s *dev) -{ - FAR struct can_conn_s *conn = can_active(dev, NULL); - FAR struct can_conn_s *nextconn; - int ret; - - if (conn == NULL) - { - /* There is no listener on the dev. Just drop the packet. */ - - return OK; - } - - /* Storing reception timestamp provided by realtime - * if timestamp no provided by hardware. - */ - -#ifdef CONFIG_NET_TIMESTAMP - if ((dev->d_features & NETDEV_RX_STAMP) == 0) - { - clock_gettime(CLOCK_REALTIME, &dev->d_iob->io_time); - } -#endif /* CONFIG_NET_TIMESTAMP */ - - can_conn_list_lock(); - - /* Do we have second connection that can hold this packet? */ - - while ((nextconn = can_active(dev, conn)) != NULL) - { - /* Yes... There are multiple listeners on the same dev. - * We need to clone the packet and deliver it to each listener. - */ - - FAR struct iob_s *iob = can_iob_clone(dev); - - if (iob == NULL) - { - nerr("ERROR: IOB clone failed.\n"); - break; /* We can still process one time without clone. */ - } - - can_input_conn(dev, conn); - - netdev_iob_replace(dev, iob); - conn = nextconn; - } - - /* We can deliver the packet directly to the last listener. */ - - ret = can_input_conn(dev, conn); - - can_conn_list_unlock(); - - return ret; -} - -/**************************************************************************** - * Name: can_input - * - * Description: - * Handle incoming packet input - * - * Input Parameters: - * dev - The device driver structure containing the received packet - * - * Returned Value: - * OK The packet has been processed and can be deleted - * -EAGAIN There is a matching connection, but could not dispatch the packet - * yet. Useful when a packet arrives before a recv call is in - * place. - * - * Assumptions: - * This function can be called from an interrupt. - * - ****************************************************************************/ - -int can_input(FAR struct net_driver_s *dev) -{ - FAR uint8_t *buf; - int ret; - -#ifdef CONFIG_NET_STATISTICS - g_netstats.can.recv++; -#endif - netdev_lock(dev); - - if (dev->d_iob != NULL) - { - buf = dev->d_buf; - - /* Set the device buffer to l2 */ - - dev->d_buf = NETLLBUF; - ret = can_in(dev); - - dev->d_buf = buf; - - netdev_unlock(dev); - return ret; - } - - ret = netdev_input(dev, can_in, false); - if (ret < 0) - { -#ifdef CONFIG_NET_STATISTICS - g_netstats.can.drop++; -#endif - } - - netdev_unlock(dev); - return ret; -} - -#endif /* CONFIG_NET && CONFIG_NET_CAN */
