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 d13ed7e2ef440a88eb8334a15ca84bc3bcd61ca0
Author: p-szafonimateusz <[email protected]>
AuthorDate: Mon Nov 9 11:51:12 2026 +0100

    net/can: Add can_sockif() to select the CAN protocol.
    
    Add can_sockif(), which returns the socket interface of the CAN
    protocol requested in socket(), and call it from net_sockif() instead
    of using g_can_raw_sockif directly.  CAN_RAW is the only CAN protocol
    for now, so can_sockif() always returns g_can_raw_sockif and
    can_raw_setup() still validates the socket type and protocol.  Other
    CAN protocols such as ISO-TP can be added here.  No functional change.
    
    Assisted-by: Claude:claude-opus-5-5
    Signed-off-by: p-szafonimateusz <[email protected]>
---
 .github/CODEOWNERS                        |  1 +
 Documentation/os/networking/socketcan.rst |  1 +
 net/can/CMakeLists.txt                    |  2 +-
 net/can/Make.defs                         |  1 +
 net/can/can.h                             | 11 ++++++
 net/can/can_sockif.c                      | 63 +++++++++++++++++++++++++++++++
 net/socket/net_sockif.c                   |  2 +-
 7 files changed, 79 insertions(+), 2 deletions(-)

diff --git a/.github/CODEOWNERS b/.github/CODEOWNERS
index 1c83ccb642e..a5a79181c1e 100644
--- a/.github/CODEOWNERS
+++ b/.github/CODEOWNERS
@@ -15893,6 +15893,7 @@ 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_raw.c [email protected] [email protected] 
[email protected] [email protected] [email protected]
+net/can/can_sockif.c [email protected] [email protected] 
[email protected] [email protected] [email protected]
 net/devif/devif.h [email protected] [email protected] 
[email protected] [email protected]
 net/devif/devif_callback.c [email protected] [email protected] 
[email protected] [email protected]
 net/devif/devif_filesend.c [email protected] [email protected] 
[email protected] [email protected]
diff --git a/Documentation/os/networking/socketcan.rst 
b/Documentation/os/networking/socketcan.rst
index 4dea9ddef29..1ac2ce27732 100644
--- a/Documentation/os/networking/socketcan.rst
+++ b/Documentation/os/networking/socketcan.rst
@@ -95,6 +95,7 @@ Files supporting SocketCAN can be found in the following 
locations:
    - ``can.c`` - Connection management, input, callbacks and polling
    - ``can_raw.c`` - Socket interface, message transmission (with and without
      buffering) and reception
+   - ``can_sockif.c`` - Selection of the socket interface of a CAN protocol
 
 Configuration Options
 ---------------------
diff --git a/net/can/CMakeLists.txt b/net/can/CMakeLists.txt
index 39f2ac08f28..a9d53b86a6d 100644
--- a/net/can/CMakeLists.txt
+++ b/net/can/CMakeLists.txt
@@ -23,7 +23,7 @@ if(CONFIG_NET_CAN)
   set(SRCS)
 
   # Socket layer
-  list(APPEND SRCS can_raw.c)
+  list(APPEND SRCS can_sockif.c can_raw.c)
 
   if(CONFIG_NET_CAN_NBUFFERS GREATER 0)
     list(APPEND SRCS can_bufpool.c)
diff --git a/net/can/Make.defs b/net/can/Make.defs
index 58c33961a64..9b67a36dce3 100644
--- a/net/can/Make.defs
+++ b/net/can/Make.defs
@@ -26,6 +26,7 @@ ifeq ($(CONFIG_NET_CAN),y)
 
 # Socket layer
 
+SOCK_CSRCS += can_sockif.c
 SOCK_CSRCS += can_raw.c
 
 ifdef CONFIG_NET_CAN_NBUFFERS
diff --git a/net/can/can.h b/net/can/can.h
index 3ad7f49c426..959bbe9103c 100644
--- a/net/can/can.h
+++ b/net/can/can.h
@@ -150,6 +150,17 @@ extern const struct sock_intf_s g_can_raw_sockif;
  * Public Function Prototypes
  ****************************************************************************/
 
+/****************************************************************************
+ * Name: can_sockif
+ *
+ * Description:
+ *   Return the socket interface of the requested CAN protocol.
+ *
+ ****************************************************************************/
+
+FAR const struct sock_intf_s *
+can_sockif(sa_family_t family, int type, int protocol);
+
 /****************************************************************************
  * Name: can_alloc()
  *
diff --git a/net/can/can_sockif.c b/net/can/can_sockif.c
new file mode 100644
index 00000000000..9cf64ebe605
--- /dev/null
+++ b/net/can/can_sockif.c
@@ -0,0 +1,63 @@
+/****************************************************************************
+ * net/can/can_sockif.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>
+
+#ifdef CONFIG_NET_CAN
+
+#include <sys/socket.h>
+
+#include "can/can.h"
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: can_sockif
+ *
+ * Description:
+ *   Return the socket interface of the requested CAN protocol.  CAN_RAW
+ *   is the only CAN protocol for now; can_raw_setup() validates the socket
+ *   type and protocol.
+ *
+ * Input Parameters:
+ *   family   - Socket address family (always PF_CAN)
+ *   type     - Socket type
+ *   protocol - CAN protocol
+ *
+ * Returned Value:
+ *   The socket interface of the CAN protocol.
+ *
+ ****************************************************************************/
+
+FAR const struct sock_intf_s *
+can_sockif(sa_family_t family, int type, int protocol)
+{
+  return &g_can_raw_sockif;
+}
+
+#endif /* CONFIG_NET_CAN */
diff --git a/net/socket/net_sockif.c b/net/socket/net_sockif.c
index 84e04f3bf25..90c4a9589be 100644
--- a/net/socket/net_sockif.c
+++ b/net/socket/net_sockif.c
@@ -232,7 +232,7 @@ net_sockif(sa_family_t family, int type, int protocol)
 
 #ifdef CONFIG_NET_CAN
       case PF_CAN:
-        sockif = &g_can_raw_sockif;
+        sockif = can_sockif(family, type, protocol);
         break;
 #endif
 

Reply via email to