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 48f2168ac3896ffc22425d59c3a5ad9f4b720614
Author: p-szafonimateusz <[email protected]>
AuthorDate: Wed Nov 19 12:39:32 2025 +0100

    drivers/note: add note over netdev
    
    Add a note driver which streams sched_note instrumentation data to a
    remote host as UDP packets through netpoll. The netdev interface,
    destination IP/MAC address and UDP port are configurable through
    CONFIG_DRIVERS_NOTENET_*.
    
    The driver is bound to the netdev named CONFIG_DRIVERS_NOTENET_INTF
    when the interface is brought up by the netdev upper-half driver.
    
    Signed-off-by: p-szafonimateusz <[email protected]>
---
 Documentation/os/networking/index.rst |   1 +
 Documentation/os/networking/note.rst  |  13 ++
 drivers/net/CMakeLists.txt            |   3 +
 drivers/net/Make.defs                 |   3 +
 drivers/net/netdev_drivers.c          |  74 +++++++
 drivers/net/netdev_drivers.h          |  52 +++++
 drivers/net/netdev_upperhalf.c        |  21 +-
 drivers/note/CMakeLists.txt           |   4 +
 drivers/note/Kconfig                  |  43 ++++
 drivers/note/Make.defs                |   4 +
 drivers/note/note_driver.c            |   6 +
 drivers/note/notenet_driver.c         | 390 ++++++++++++++++++++++++++++++++++
 include/nuttx/note/notenet_driver.h   |  59 +++++
 13 files changed, 672 insertions(+), 1 deletion(-)

diff --git a/Documentation/os/networking/index.rst 
b/Documentation/os/networking/index.rst
index 5e569eb8165..9f4a9668424 100644
--- a/Documentation/os/networking/index.rst
+++ b/Documentation/os/networking/index.rst
@@ -69,6 +69,7 @@ Network Support
   netdev.rst
   netdriver.rst
   netpoll.rst
+  note.rst
   mdio.rst
   netguardsize.rst
   netlink.rst
diff --git a/Documentation/os/networking/note.rst 
b/Documentation/os/networking/note.rst
new file mode 100644
index 00000000000..71e0b03b25f
--- /dev/null
+++ b/Documentation/os/networking/note.rst
@@ -0,0 +1,13 @@
+================
+Note over NETDEV
+================
+
+Note over NETDEV streams kernel tracing or instrumentation data (e.g., 
scheduler
+events, interrupts, context switches) to a remote host for analysis.
+It is one-way communication over UDP.
+
+The netdev driver selected by ``CONFIG_DRIVERS_NOTENET_INTF`` must be based on
+the :ref:`Netdev Lowerhalf <netdriver>` implementation, and 
``CONFIG_NET_NETPOLL``
+must be enabled. Notes are sent as raw note records to
+``CONFIG_DRIVERS_NOTENET_IPADDR`` / ``CONFIG_DRIVERS_NOTENET_MAC`` on UDP port
+``CONFIG_DRIVERS_NOTENET_PORT`` (broadcast by default).
diff --git a/drivers/net/CMakeLists.txt b/drivers/net/CMakeLists.txt
index fac681c8182..8a0d0d3105c 100644
--- a/drivers/net/CMakeLists.txt
+++ b/drivers/net/CMakeLists.txt
@@ -27,6 +27,9 @@ if(CONFIG_NET)
 
   if(CONFIG_MM_IOB)
     list(APPEND SRCS netdev_upperhalf.c)
+    if(CONFIG_DRIVERS_NOTENET)
+      list(APPEND SRCS netdev_drivers.c)
+    endif()
   endif()
 
   if(CONFIG_MDIO_BUS)
diff --git a/drivers/net/Make.defs b/drivers/net/Make.defs
index d92b34aee11..6fc1a0816be 100644
--- a/drivers/net/Make.defs
+++ b/drivers/net/Make.defs
@@ -28,6 +28,9 @@ ifeq ($(CONFIG_NET),y)
 
 ifeq ($(CONFIG_MM_IOB),y)
   CSRCS += netdev_upperhalf.c
+ifeq ($(CONFIG_DRIVERS_NOTENET),y)
+  CSRCS += netdev_drivers.c
+endif
 endif
 
 ifeq ($(CONFIG_MDIO_BUS),y)
diff --git a/drivers/net/netdev_drivers.c b/drivers/net/netdev_drivers.c
new file mode 100644
index 00000000000..084843b3203
--- /dev/null
+++ b/drivers/net/netdev_drivers.c
@@ -0,0 +1,74 @@
+/****************************************************************************
+ * drivers/net/netdev_drivers.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>
+
+#include <debug.h>
+#include <string.h>
+
+#ifdef CONFIG_DRIVERS_NOTENET
+#  include <nuttx/note/notenet_driver.h>
+#endif
+
+#include "netdev_drivers.h"
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: netdev_drivers_init
+ *
+ * Description:
+ *   Register all enabled NETDEV drivers.
+ *
+ * Input Parameters:
+ *   dev    - The lower half device driver structure
+ *
+ * Returned Value:
+ *   0:Success; negated errno on failure
+ *
+ ****************************************************************************/
+
+int netdev_drivers_init(FAR struct netdev_lowerhalf_s *dev)
+{
+  int ret = OK;
+
+#ifdef CONFIG_DRIVERS_NOTENET
+  if (strcmp(dev->netdev.d_ifname, CONFIG_DRIVERS_NOTENET_INTF) == 0)
+    {
+      ninfo("bind NOTE with netdev = %p\n", dev);
+      ret = notenet_init(dev);
+      if (ret < 0)
+        {
+          nerr("ERROR: netdev_note_init failed: %d\n", ret);
+          return ret;
+        }
+    }
+#endif
+
+  return ret;
+}
diff --git a/drivers/net/netdev_drivers.h b/drivers/net/netdev_drivers.h
new file mode 100644
index 00000000000..022bb7f91cd
--- /dev/null
+++ b/drivers/net/netdev_drivers.h
@@ -0,0 +1,52 @@
+/****************************************************************************
+ * drivers/net/netdev_drivers.h
+ *
+ * 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.
+ *
+ ****************************************************************************/
+
+#ifndef __DRIVERS_NET_NETDEV_DRIVERS_H
+#define __DRIVERS_NET_NETDEV_DRIVERS_H
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/net/netdev_lowerhalf.h>
+
+/****************************************************************************
+ * Public Function Prototypes
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: netdev_drivers_init
+ *
+ * Description:
+ *   Register all enabled NETDEV drivers.
+ *
+ * Input Parameters:
+ *   dev    - The lower half device driver structure
+ *
+ * Returned Value:
+ *   0:Success; negated errno on failure
+ *
+ ****************************************************************************/
+
+int netdev_drivers_init(FAR struct netdev_lowerhalf_s *dev);
+
+#endif  /* __DRIVERS_NET_NETDEV_DRIVERS_H */
diff --git a/drivers/net/netdev_upperhalf.c b/drivers/net/netdev_upperhalf.c
index 31939590c1d..7e4fced2be2 100644
--- a/drivers/net/netdev_upperhalf.c
+++ b/drivers/net/netdev_upperhalf.c
@@ -44,6 +44,10 @@
 #include <nuttx/semaphore.h>
 #include <nuttx/spinlock.h>
 
+#ifdef CONFIG_DRIVERS_NOTENET
+#  include "netdev_drivers.h"
+#endif
+
 /****************************************************************************
  * Pre-processor Definitions
  ****************************************************************************/
@@ -1277,6 +1281,7 @@ static int netdev_upper_ifup(FAR struct net_driver_s *dev)
 {
   FAR struct netdev_upperhalf_s *upper = dev->d_private;
   int cpu = 1;
+  int ret;
 
   switch (upper->lower->rxtype)
     {
@@ -1318,7 +1323,21 @@ static int netdev_upper_ifup(FAR struct net_driver_s 
*dev)
 
   if (upper->lower->ops->ifup)
     {
-      return upper->lower->ops->ifup(upper->lower);
+      ret = upper->lower->ops->ifup(upper->lower);
+
+#ifdef CONFIG_DRIVERS_NOTENET
+      /* Bind the netdev drivers.  The interface is already up, so a
+       * failure here must not fail ifup.
+       */
+
+      if (ret >= 0 && netdev_drivers_init(upper->lower) < 0)
+        {
+          nwarn("WARNING: failed to bind netdev drivers to %s\n",
+                dev->d_ifname);
+        }
+#endif
+
+      return ret;
     }
 
   return -ENOSYS;
diff --git a/drivers/note/CMakeLists.txt b/drivers/note/CMakeLists.txt
index fa5a9a42e07..554dbd16362 100644
--- a/drivers/note/CMakeLists.txt
+++ b/drivers/note/CMakeLists.txt
@@ -50,5 +50,9 @@ if(CONFIG_DRIVERS_NOTERPMSG)
   list(APPEND SRCS noterpmsg_driver.c)
 endif()
 
+if(CONFIG_DRIVERS_NOTENET)
+  list(APPEND SRCS notenet_driver.c)
+endif()
+
 target_sources(drivers PRIVATE ${SRCS})
 target_include_directories(drivers PRIVATE ${NUTTX_DIR}/sched)
diff --git a/drivers/note/Kconfig b/drivers/note/Kconfig
index d9f8ebf3cb9..69173588205 100644
--- a/drivers/note/Kconfig
+++ b/drivers/note/Kconfig
@@ -170,4 +170,47 @@ config DRIVERS_NOTERPMSG_WORK_DELAY
 
 endif
 
+config DRIVERS_NOTENET
+       bool "Note to netdev"
+       depends on NETDEVICES && NET_NETPOLL
+       depends on SCHED_WORKQUEUE
+       default n
+       ---help---
+               Use the netdev as a Note output device. Notes are sent as UDP
+               packets through netpoll.
+
+if DRIVERS_NOTENET
+
+config DRIVERS_NOTENET_INTF
+       string "Note netdev interface"
+       default "eth0"
+       ---help---
+               Network interface used for netdev Note.
+
+config DRIVERS_NOTENET_IPADDR
+       hex "Note netdev destination IP address"
+       default 0xffffffff
+       ---help---
+               Address where netdev sends Note output.
+               Default: broadcast IP address.
+
+config DRIVERS_NOTENET_MAC
+       string "Note netdev destination MAC address"
+       default "ff:ff:ff:ff:ff:ff"
+       ---help---
+               MAC address where netdev sends Note output.
+               Default: broadcast MAC address.
+
+config DRIVERS_NOTENET_PORT
+       int "Note netdev port"
+       default 50000
+       ---help---
+               UDP port for Note output.
+
+config DRIVERS_NOTENET_WORK_DELAY
+       int "Note netdev work delay (ms)"
+       default 100
+
+endif
+
 endif # DRIVERS_NOTE
diff --git a/drivers/note/Make.defs b/drivers/note/Make.defs
index d27536bbc51..816fa185c84 100644
--- a/drivers/note/Make.defs
+++ b/drivers/note/Make.defs
@@ -59,5 +59,9 @@ ifneq ($(CONFIG_DRIVERS_NOTERAM_SECTION),"")
   CFLAGS += 
${DEFINE_PREFIX}DRIVERS_NOTERAM_SECTION=CONFIG_DRIVERS_NOTERAM_SECTION
 endif
 
+ifeq ($(CONFIG_DRIVERS_NOTENET),y)
+  CSRCS += notenet_driver.c
+endif
+
 DEPPATH += --dep-path note
 VPATH += :note
diff --git a/drivers/note/note_driver.c b/drivers/note/note_driver.c
index 1a52dc4c1c0..aca3c0d22c0 100644
--- a/drivers/note/note_driver.c
+++ b/drivers/note/note_driver.c
@@ -41,6 +41,9 @@
 #include <nuttx/note/noteram_driver.h>
 #include <nuttx/note/notelog_driver.h>
 #include <nuttx/note/notestream_driver.h>
+#ifdef CONFIG_DRIVERS_NOTENET
+#  include <nuttx/note/notenet_driver.h>
+#endif
 #include <nuttx/spinlock.h>
 #include <nuttx/sched_note.h>
 
@@ -162,6 +165,9 @@ FAR static struct note_driver_s *
 #endif
 #ifdef CONFIG_DRIVERS_NOTERPMSG
   (FAR struct note_driver_s *)&g_noterpmsg_driver,
+#endif
+#ifdef CONFIG_DRIVERS_NOTENET
+  (FAR struct note_driver_s *)&g_notenet,
 #endif
   NULL
 };
diff --git a/drivers/note/notenet_driver.c b/drivers/note/notenet_driver.c
new file mode 100644
index 00000000000..b8fd1c902e8
--- /dev/null
+++ b/drivers/note/notenet_driver.c
@@ -0,0 +1,390 @@
+/****************************************************************************
+ * drivers/note/notenet_driver.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>
+
+#include <nuttx/net/netpoll.h>
+#include <nuttx/note/note_driver.h>
+#include <nuttx/note/notenet_driver.h>
+#include <nuttx/panic_notifier.h>
+#include <nuttx/spinlock.h>
+#include <nuttx/wqueue.h>
+
+/****************************************************************************
+ * Pre-processor definitions
+ ****************************************************************************/
+
+#define NOTENET_WORK_DELAY MSEC2TICK(CONFIG_DRIVERS_NOTENET_WORK_DELAY)
+
+/****************************************************************************
+ * Private Types
+ ****************************************************************************/
+
+struct notenet_driver_s
+{
+  struct note_driver_s  driver;  /* Note driver part, must be first element */
+  spinlock_t            lock;    /* Protects pkt and next */
+  FAR netpkt_t         *pkt;     /* Packet being filled by note_netdev_add */
+  size_t                next;    /* The next index (where data is added) */
+  struct work_s         work;    /* Used for deferred send */
+  struct netpoll_s      np;      /* Netpoll */
+  struct notifier_block nb;      /* Panic notifier */
+  bool                  bound;   /* Netpoll is bound to a device */
+};
+
+/****************************************************************************
+ * Private Function Prototypes
+ ****************************************************************************/
+
+static void notenetdev_work(FAR void *arg);
+static void note_netdev_add(FAR struct note_driver_s *driver,
+                            FAR const void *note, size_t notelen);
+static int netdev_note_panic_callback(FAR struct notifier_block *nb,
+                                      unsigned long action,
+                                      FAR void *data);
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static const struct note_driver_ops_s g_notenetdev_ops =
+{
+  note_netdev_add
+};
+
+/****************************************************************************
+ * Public Data
+ ****************************************************************************/
+
+struct notenet_driver_s g_notenet =
+{
+  {
+#ifdef CONFIG_SCHED_INSTRUMENTATION_FILTER
+    "net",
+    {
+      {
+        CONFIG_SCHED_INSTRUMENTATION_FILTER_DEFAULT_MODE,
+#  ifdef CONFIG_SMP
+        CONFIG_SCHED_INSTRUMENTATION_CPUSET
+#  endif
+      },
+    },
+#endif
+    &g_notenetdev_ops
+  },
+  .lock = SP_UNLOCKED
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: notenetdev_take
+ *
+ * Description:
+ *   Detach the packet being filled, so it can be sent without holding
+ *   drv->lock.  Notes added before notenetdev_refill() provides a new
+ *   packet are dropped.
+ *
+ * Returned Value:
+ *   The packet to send, or NULL if there is nothing to send.
+ *
+ ****************************************************************************/
+
+static FAR netpkt_t *notenetdev_take(FAR struct notenet_driver_s *drv)
+{
+  FAR netpkt_t *pkt = NULL;
+  irqstate_t flags;
+
+  flags = spin_lock_irqsave_notrace(&drv->lock);
+
+  if (drv->pkt != NULL && drv->next > 0)
+    {
+      pkt       = drv->pkt;
+      drv->pkt  = NULL;
+      drv->next = 0;
+    }
+
+  spin_unlock_irqrestore_notrace(&drv->lock, flags);
+
+  return pkt;
+}
+
+/****************************************************************************
+ * Name: notenetdev_send
+ *
+ * Description:
+ *   Send one packet.  The packet is freed when the driver refuses it, for
+ *   example because the interface is down.
+ *
+ ****************************************************************************/
+
+static void notenetdev_send(FAR struct notenet_driver_s *drv,
+                            FAR netpkt_t *pkt)
+{
+  if (netpoll_udp_pkt_send(&drv->np, pkt) < 0)
+    {
+      netpkt_free(drv->np.lower, pkt, NETPKT_TX);
+    }
+}
+
+/****************************************************************************
+ * Name: notenetdev_refill
+ *
+ * Description:
+ *   Allocate a packet for note_netdev_add() if the previous allocation
+ *   failed.
+ *
+ ****************************************************************************/
+
+static void notenetdev_refill(FAR struct notenet_driver_s *drv)
+{
+  FAR netpkt_t *pkt;
+  irqstate_t flags;
+
+  if (drv->pkt != NULL)
+    {
+      return;
+    }
+
+  pkt = netpoll_udp_pkt_alloc(&drv->np);
+  if (pkt == NULL)
+    {
+      return;
+    }
+
+  flags = spin_lock_irqsave_notrace(&drv->lock);
+  if (drv->pkt == NULL)
+    {
+      drv->pkt  = pkt;
+      drv->next = 0;
+      pkt       = NULL;
+    }
+
+  spin_unlock_irqrestore_notrace(&drv->lock, flags);
+
+  if (pkt != NULL)
+    {
+      netpkt_free(drv->np.lower, pkt, NETPKT_TX);
+    }
+}
+
+/****************************************************************************
+ * Name: notenetdev_work
+ *
+ * Description:
+ *   Send the buffered notes.  The driver's transmit() is called with
+ *   netdev_lock() held, the same lock the upper half holds when it calls
+ *   transmit(), so the two never run in the driver at the same time.
+ *
+ ****************************************************************************/
+
+static void notenetdev_work(FAR void *arg)
+{
+  FAR struct notenet_driver_s *drv = arg;
+  FAR struct net_driver_s *dev = &drv->np.lower->netdev;
+  FAR netpkt_t *pkt;
+
+  netdev_lock(dev);
+
+  pkt = notenetdev_take(drv);
+  if (pkt != NULL)
+    {
+      notenetdev_send(drv, pkt);
+    }
+
+  notenetdev_refill(drv);
+
+  netdev_unlock(dev);
+}
+
+/****************************************************************************
+ * Name: note_netdev_add
+ *
+ * Description:
+ *   Add the variable length note to the packet being filled.  This can be
+ *   called from any context, also from an interrupt handler, so it never
+ *   sends: sending is deferred to the work queue.  When the note does not
+ *   fit, it is dropped.
+ *
+ * Input Parameters:
+ *   driver  - The note driver
+ *   note    - The note buffer
+ *   notelen - The buffer length
+ *
+ * Returned Value:
+ *   None
+ *
+ ****************************************************************************/
+
+static void note_netdev_add(FAR struct note_driver_s *driver,
+                            FAR const void *note, size_t notelen)
+{
+  FAR struct notenet_driver_s *drv =
+    (FAR struct notenet_driver_s *)driver;
+  clock_t      delay  = NOTENET_WORK_DELAY;
+  size_t       maxlen = netpoll_get_maxlen(&drv->np);
+  irqstate_t   flags;
+
+  flags = spin_lock_irqsave_notrace(&drv->lock);
+
+  if (!drv->bound)
+    {
+      spin_unlock_irqrestore_notrace(&drv->lock, flags);
+      return;
+    }
+
+  /* Drop the note if there is no packet or the packet is full, and let
+   * the work send what is buffered.
+   */
+
+  if (drv->pkt == NULL || drv->next + notelen > maxlen)
+    {
+      spin_unlock_irqrestore_notrace(&drv->lock, flags);
+      delay = 0;
+      goto kick;
+    }
+
+  if (netpoll_pkt_copy(&drv->np, drv->pkt, note, notelen, drv->next) < 0)
+    {
+      /* Out of IOBs: drop the note, and trim what was partly copied */
+
+      netpkt_setdatalen(drv->np.lower, drv->pkt,
+                        drv->np.hdrlen + drv->next);
+      spin_unlock_irqrestore_notrace(&drv->lock, flags);
+      delay = 0;
+      goto kick;
+    }
+
+  drv->next += notelen;
+
+  /* Start work immediately when the packet is more than 75% full */
+
+  if (maxlen - drv->next < maxlen / 4)
+    {
+      delay = 0;
+    }
+
+  spin_unlock_irqrestore_notrace(&drv->lock, flags);
+
+kick:
+  if (work_available(&drv->work))
+    {
+      work_queue(HPWORK, &drv->work, notenetdev_work, drv, delay);
+    }
+}
+
+/****************************************************************************
+ * Name: netdev_note_panic_callback
+ *
+ * Description:
+ *   Flush the buffered notes on panic.  Nothing else runs at this point,
+ *   so the packet is sent inline without netdev_lock().
+ *
+ ****************************************************************************/
+
+static int netdev_note_panic_callback(FAR struct notifier_block *nb,
+                                      unsigned long action,
+                                      FAR void *data)
+{
+  FAR struct notenet_driver_s *drv =
+    container_of(nb, struct notenet_driver_s, nb);
+  FAR netpkt_t *pkt;
+
+  if (action == PANIC_KERNEL_FINAL)
+    {
+      pkt = notenetdev_take(drv);
+      if (pkt != NULL)
+        {
+          notenetdev_send(drv, pkt);
+        }
+    }
+
+  return 0;
+}
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: notenet_init
+ *
+ * Description:
+ *   Register a netdev channel to note.
+ *
+ * Input Parameters:
+ *   dev - The lower half netdev driver structure to be bound.
+ *
+ * Returned Value:
+ *   Zero on success. A negated errno value is returned on a failure.
+ *
+ ****************************************************************************/
+
+int notenet_init(FAR struct netdev_lowerhalf_s *dev)
+{
+  FAR struct notenet_driver_s *note = &g_notenet;
+  FAR netpkt_t                *pkt;
+  irqstate_t                   flags;
+  int                          ret;
+
+  /* Bind only once: ifup runs again after every ifdown, and the packet
+   * and panic notifier set up the first time stay valid.
+   */
+
+  if (note->bound)
+    {
+      return note->np.lower == dev ? OK : -EBUSY;
+    }
+
+  /* Initialize netpoll */
+
+  ret = netpoll_udp_init(&note->np, dev,
+                         CONFIG_DRIVERS_NOTENET_PORT,
+                         CONFIG_DRIVERS_NOTENET_IPADDR,
+                         CONFIG_DRIVERS_NOTENET_PORT,
+                         CONFIG_DRIVERS_NOTENET_MAC);
+  if (ret < 0)
+    {
+      return ret;
+    }
+
+  pkt = netpoll_udp_pkt_alloc(&note->np);
+
+  flags = spin_lock_irqsave_notrace(&note->lock);
+  note->pkt   = pkt;
+  note->next  = 0;
+  note->bound = true;
+  spin_unlock_irqrestore_notrace(&note->lock, flags);
+
+  /* Register panic notifier */
+
+  note->nb.notifier_call = netdev_note_panic_callback;
+  panic_notifier_chain_register(&note->nb);
+
+  return OK;
+}
diff --git a/include/nuttx/note/notenet_driver.h 
b/include/nuttx/note/notenet_driver.h
new file mode 100644
index 00000000000..c4b7c8a78b3
--- /dev/null
+++ b/include/nuttx/note/notenet_driver.h
@@ -0,0 +1,59 @@
+/****************************************************************************
+ * include/nuttx/note/notenet_driver.h
+ *
+ * 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.
+ *
+ ****************************************************************************/
+
+#ifndef __INCLUDE_NUTTX_NOTENET_DRIVER_H
+#define __INCLUDE_NUTTX_NOTENET_DRIVER_H
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/net/netdev_lowerhalf.h>
+
+/****************************************************************************
+ * Public Data
+ ****************************************************************************/
+
+struct notenet_driver_s;
+extern struct notenet_driver_s g_notenet;
+
+/****************************************************************************
+ * Public Function Prototypes
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: notenet_init
+ *
+ * Description:
+ *   Register a netdev channel to note.
+ *
+ * Input Parameters:
+ *   dev - The lower half netdev driver structure to be bound.
+ *
+ * Returned Value:
+ *   Zero on success. A negated errno value is returned on a failure.
+ *
+ ****************************************************************************/
+
+int notenet_init(FAR struct netdev_lowerhalf_s *dev);
+
+#endif /* __INCLUDE_NUTTX_NOTENET_DRIVER_H */

Reply via email to