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 32ff6765e4b709cd50cfcc2ddd6c329f0368907a
Author: wangjinjing1 <[email protected]>
AuthorDate: Mon Nov 16 14:47:18 2026 +0800

    net/can: add rx sync callback support
    
    Some users need to handle a received CAN frame synchronously in the
    context of can_input(), instead of waiting for a recv() on the socket.
    Queuing to the read-ahead buffer adds a copy and defers processing to
    the reader task, which is not acceptable for in-kernel consumers such as
    a forwarding/gateway path.
    
    Add an optional per-connection receive callback:
    
      typedef CODE int (*can_rx_cb_t)(FAR void *arg, FAR const void *frame);
      int can_set_rx_callback(FAR struct socket *psock,
                              can_rx_cb_t callback, FAR void *args);
    
    When a callback is registered, can_input() delivers each matching frame
    to it directly; a return of OK consumes the frame and it is not queued
    to the read-ahead path.  When no callback is registered the normal
    per-protocol conn->input() hook runs exactly as before, so existing
    users are unaffected.
    
    Ported onto the current SocketCAN architecture: the downstream change
    targeted net/can/can_input.c, which upstream has merged into
    net/can/can.c, so the hook sits in can_input_conn() just before the
    conn->input() dispatch.  can_set_rx_callback() is implemented in
    net/can/can_sockif.c and the rxcb/rxarg fields are added to
    struct can_conn_s.
    
    Signed-off-by: wangjinjing <[email protected]>
---
 include/nuttx/net/can.h | 23 +++++++++++++++++++++++
 net/can/can.c           | 18 +++++++++++++++++-
 net/can/can.h           |  7 +++++++
 net/can/can_sockif.c    | 35 +++++++++++++++++++++++++++++++++++
 4 files changed, 82 insertions(+), 1 deletion(-)

diff --git a/include/nuttx/net/can.h b/include/nuttx/net/can.h
index d48089f0160..60b282d0837 100644
--- a/include/nuttx/net/can.h
+++ b/include/nuttx/net/can.h
@@ -97,6 +97,29 @@ extern "C"
  * Public Function Prototypes
  ****************************************************************************/
 
+typedef CODE int (*can_rx_cb_t)(FAR void *arg, FAR const void *frame);
+
+/****************************************************************************
+ * Name: can_set_rx_callback
+ *
+ * Description:
+ *   register rx sync callback to the can connection structures.
+ *
+ * Input Parameters:
+ *   psock    - The socket structure of the CAN connection
+ *   callback - The callback function to be called when a frame is received
+ *   args     - The argument to be passed to the callback function
+ *
+ * Returned Value:
+ *   Zero (OK) is returned on success.  Otherwise, a negated errno value is
+ *   returned.
+ *
+ ****************************************************************************/
+
+struct socket; /* Forward reference */
+int can_set_rx_callback(FAR struct socket *psock, can_rx_cb_t callback,
+                        FAR void *args);
+
 /****************************************************************************
  * Name: can_input
  *
diff --git a/net/can/can.c b/net/can/can.c
index 551ab90fb2d..966c3536714 100644
--- a/net/can/can.c
+++ b/net/can/can.c
@@ -318,7 +318,23 @@ static int can_input_conn(FAR struct net_driver_s *dev,
 
   /* Perform the application callback */
 
-  flags = conn->input(conn, dev);
+  if (conn->rxcb != NULL)
+    {
+      /* Synchronous receive callback: deliver the frame directly.  A
+       * return of OK means the callback consumed the frame.
+       */
+
+      flags = CAN_NEWDATA;
+      ret = conn->rxcb(conn->rxarg, dev->d_buf);
+      if (ret == OK)
+        {
+          flags = 0;
+        }
+    }
+  else
+    {
+      flags = conn->input(conn, dev);
+    }
 
   /* If the operation was successful, the CAN_NEWDATA flag is removed
    * and thus the packet can be deleted (OK will be returned).
diff --git a/net/can/can.h b/net/can/can.h
index 952a1479dae..e788420f2cc 100644
--- a/net/can/can.h
+++ b/net/can/can.h
@@ -34,6 +34,7 @@
 
 #include <nuttx/semaphore.h>
 #include <nuttx/can.h>
+#include <nuttx/net/can.h>
 #include <nuttx/net/net.h>
 #include <nuttx/net/netdev.h>
 
@@ -128,11 +129,17 @@ struct can_conn_s
 
   /* Protocol hooks used by can_input():
    *
+   *   rxcb  - Optional synchronous receive callback.  When set, a received
+   *           frame is delivered to it instead of the normal input hook;
+   *           returning OK consumes the frame.
+   *   rxarg - User argument passed to rxcb.
    *   match - Return true if the received frame in dev is for conn.
    *   input - Deliver the received frame in dev to conn and return the
    *           remaining event flags.
    */
 
+  can_rx_cb_t rxcb;
+  FAR void   *rxarg;
   CODE bool (*match)(FAR struct can_conn_s *conn,
                      FAR struct net_driver_s *dev);
   CODE uint32_t (*input)(FAR struct can_conn_s *conn,
diff --git a/net/can/can_sockif.c b/net/can/can_sockif.c
index d7c28b78861..b3ae36725c5 100644
--- a/net/can/can_sockif.c
+++ b/net/can/can_sockif.c
@@ -68,4 +68,39 @@ can_sockif(sa_family_t family, int type, int protocol)
   return &g_can_raw_sockif;
 }
 
+/****************************************************************************
+ * Name: can_set_rx_callback
+ *
+ * Description:
+ *   register rx sync callback to the can connection structures.
+ *
+ * Input Parameters:
+ *   psock    - The socket structure of the CAN connection
+ *   callback - The callback function to be called when a frame is received
+ *   args     - The argument to be passed to the callback function
+ *
+ * Returned Value:
+ *   Zero (OK) is returned on success.  Otherwise, a negated errno value is
+ *   returned.
+ *
+ ****************************************************************************/
+
+int can_set_rx_callback(FAR struct socket *psock, can_rx_cb_t callback,
+                        FAR void *args)
+{
+  FAR struct can_conn_s *conn;
+
+  if (psock == NULL || psock->s_conn == NULL || callback == NULL)
+    {
+      return -EBADF;
+    }
+
+  conn = psock->s_conn;
+
+  conn->rxcb  = callback;
+  conn->rxarg = args;
+
+  return OK;
+}
+
 #endif /* CONFIG_NET_CAN */

Reply via email to