This is an automated email from the ASF dual-hosted git repository.

GUIDINGLI pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx-apps.git

commit b263efad10cad3f77fdb88612b4d3089e18234b4
Author: Kai Cheng <[email protected]>
AuthorDate: Thu Nov 5 14:14:47 2026 +0800

    wireless/bluetooth/zblue: Add HCI driver on /dev/ttyHCIx
    
    Provide the Zephyr HCI driver (the devicetree node chosen as
    zephyr,bt-hci) on a NuttX H4 character device, CONFIG_UART_BTH4, so
    that any controller registered with bt_driver_register() can be used:
    the simulator HCI socket, an on-chip controller or a UART controller.
    
    A receive thread reads the H4 packet type, header and payload into
    host buffers and passes them to bt_hci_recv().
    
    send() queues the outgoing packets, with their H4 type, for a work
    queue of the driver that writes them.  The host sends from any thread,
    its work queues among them; in the flat build these are kernel threads
    and do not share the file descriptors of the application.  So the
    driver work queue opens the device for writing itself, and all the
    writes are done there, one after the other.
    
    Signed-off-by: Kai Cheng <[email protected]>
---
 wireless/bluetooth/zblue/port/src/hci.c | 396 ++++++++++++++++++++++++++++++++
 1 file changed, 396 insertions(+)

diff --git a/wireless/bluetooth/zblue/port/src/hci.c 
b/wireless/bluetooth/zblue/port/src/hci.c
new file mode 100644
index 000000000..444b52e25
--- /dev/null
+++ b/wireless/bluetooth/zblue/port/src/hci.c
@@ -0,0 +1,396 @@
+/****************************************************************************
+ * apps/wireless/bluetooth/zblue/port/src/hci.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.
+ *
+ ****************************************************************************/
+
+/* The HCI device of the host: H4 packets over a NuttX /dev/ttyHCIx
+ * character device (drivers/serial/uart_bth4.c).  It is the zephyr,bt-hci
+ * chosen node of port/include/zephyr/devicetree_generated.h.
+ *
+ * The device is opened twice: for reading by the application, which the
+ * RX thread shares, and for writing on the TX work queue.
+ */
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <fcntl.h>
+#include <unistd.h>
+
+#include <zephyr/kernel.h>
+#include <zephyr/device.h>
+#include <zephyr/drivers/bluetooth.h>
+#include <zephyr/bluetooth/buf.h>
+#include <zephyr/bluetooth/hci_types.h>
+#include <zephyr/sys/byteorder.h>
+#include <zephyr/logging/log.h>
+
+#include "port.h"
+
+LOG_MODULE_REGISTER(zblue_hci, CONFIG_LOG_DEFAULT_LEVEL);
+
+/****************************************************************************
+ * Private Types
+ ****************************************************************************/
+
+struct hci_data
+{
+  struct bt_hci_driver_data common; /* Must be first */
+  int fd;                           /* RX, opened by the application */
+  int txfd;                         /* TX, opened on the TX work queue */
+};
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static struct hci_data g_hci_data =
+{
+  .fd = -1,
+  .txfd = -1,
+};
+
+static K_KERNEL_STACK_DEFINE(g_rx_stack, CONFIG_ZBLUE_HCI_RX_STACKSIZE);
+static struct k_thread g_rx_thread;
+static uint8_t g_rx_frame[BT_BUF_RX_SIZE];
+
+static K_KERNEL_STACK_DEFINE(g_tx_stack, CONFIG_BT_HCI_TX_STACK_SIZE);
+static struct k_work_q g_tx_queue;
+static struct k_work g_tx_open;
+static struct k_work g_tx_work;
+static K_FIFO_DEFINE(g_tx_fifo);
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+static int read_full(int fd, uint8_t *buf, size_t len)
+{
+  ssize_t ret;
+  int depth;
+  int err;
+
+  while (len > 0)
+    {
+      depth = z_zblue_block_begin();
+      ret = read(fd, buf, len);
+      err = errno;
+      z_zblue_block_end(depth);
+      if (ret < 0)
+        {
+          if (err == EINTR)
+            {
+              continue;
+            }
+
+          return -err;
+        }
+
+      buf += ret;
+      len -= ret;
+    }
+
+  return 0;
+}
+
+static bool evt_discardable(const uint8_t *hdr)
+{
+  const struct bt_hci_evt_hdr *evt = (const void *)hdr;
+  const struct bt_hci_evt_le_meta_event *meta;
+
+  if (IS_ENABLED(CONFIG_BT_CLASSIC) &&
+      (evt->evt == BT_HCI_EVT_INQUIRY_RESULT_WITH_RSSI ||
+       evt->evt == BT_HCI_EVT_EXTENDED_INQUIRY_RESULT))
+    {
+      return true;
+    }
+
+  if (evt->evt == BT_HCI_EVT_LE_META_EVENT)
+    {
+      meta = (const void *)evt->data;
+      return meta->subevent == BT_HCI_EVT_LE_ADVERTISING_REPORT;
+    }
+
+  return false;
+}
+
+static struct net_buf *rx_alloc(uint8_t type, const uint8_t *hdr)
+{
+  struct net_buf *buf = NULL;
+  bool discardable;
+
+  switch (type)
+    {
+      case BT_HCI_H4_EVT:
+        discardable = evt_discardable(hdr);
+        do
+          {
+            buf = bt_buf_get_evt(hdr[0], discardable,
+                                 discardable ? K_NO_WAIT : K_SECONDS(1));
+          }
+        while (buf == NULL && !discardable);
+        break;
+
+      case BT_HCI_H4_ACL:
+        buf = bt_buf_get_rx(BT_BUF_ACL_IN, K_FOREVER);
+        break;
+
+      case BT_HCI_H4_ISO:
+        if (IS_ENABLED(CONFIG_BT_ISO))
+          {
+            buf = bt_buf_get_rx(BT_BUF_ISO_IN, K_FOREVER);
+          }
+        break;
+    }
+
+  return buf;
+}
+
+static void rx_thread(void *p1, void *p2, void *p3)
+{
+  const struct device *dev = p1;
+  struct hci_data *hci = dev->data;
+  struct net_buf *buf;
+  uint8_t *frame = g_rx_frame;
+  size_t hdrlen;
+  size_t len;
+  int ret;
+
+  UNUSED(p2);
+  UNUSED(p3);
+
+  for (; ; )
+    {
+      ret = read_full(hci->fd, frame, 1);
+      if (ret < 0)
+        {
+          LOG_ERR("read failed: %d", ret);
+          return;
+        }
+
+      switch (frame[0])
+        {
+          case BT_HCI_H4_EVT:
+            hdrlen = BT_HCI_EVT_HDR_SIZE;
+            break;
+
+          case BT_HCI_H4_ACL:
+            hdrlen = BT_HCI_ACL_HDR_SIZE;
+            break;
+
+          case BT_HCI_H4_ISO:
+            hdrlen = BT_HCI_ISO_HDR_SIZE;
+            break;
+
+          default:
+            LOG_ERR("unknown H4 type 0x%02x", frame[0]);
+            continue;
+        }
+
+      ret = read_full(hci->fd, frame + 1, hdrlen);
+      if (ret < 0)
+        {
+          LOG_ERR("read failed: %d", ret);
+          return;
+        }
+
+      switch (frame[0])
+        {
+          case BT_HCI_H4_EVT:
+            len = frame[2];
+            break;
+
+          case BT_HCI_H4_ACL:
+            len = sys_get_le16(&frame[3]);
+            break;
+
+          default:
+            len = bt_iso_hdr_len(sys_get_le16(&frame[3]));
+            break;
+        }
+
+      if (1 + hdrlen + len > sizeof(g_rx_frame))
+        {
+          LOG_ERR("packet too long: %zu", len);
+          return;
+        }
+
+      ret = read_full(hci->fd, frame + 1 + hdrlen, len);
+      if (ret < 0)
+        {
+          LOG_ERR("read failed: %d", ret);
+          return;
+        }
+
+      buf = rx_alloc(frame[0], frame + 1);
+      if (buf == NULL)
+        {
+          LOG_WRN("dropped packet type %u", frame[0]);
+          continue;
+        }
+
+      if (net_buf_tailroom(buf) < hdrlen + len)
+        {
+          LOG_ERR("no room for %zu bytes", hdrlen + len);
+          net_buf_unref(buf);
+          continue;
+        }
+
+      net_buf_add_mem(buf, frame + 1, hdrlen + len);
+      bt_hci_recv(dev, buf);
+    }
+}
+
+/* The host sends from any thread, its work queues among them, which are
+ * kernel threads in the flat build and do not share the file descriptors
+ * of the application.  So the TX fd is opened on the TX work queue and
+ * all the writes are done there, one after the other.
+ */
+
+static void tx_open(struct k_work *work)
+{
+  UNUSED(work);
+
+  g_hci_data.txfd = open(CONFIG_ZBLUE_HCI_DEVICE, O_WRONLY | O_CLOEXEC);
+  if (g_hci_data.txfd < 0)
+    {
+      g_hci_data.txfd = -errno;
+    }
+}
+
+static void tx_work(struct k_work *work)
+{
+  struct hci_data *hci = &g_hci_data;
+  struct net_buf *buf;
+  const uint8_t *data;
+  size_t len;
+  ssize_t ret;
+  int depth;
+  int err;
+
+  UNUSED(work);
+
+  while ((buf = k_fifo_get(&g_tx_fifo, K_NO_WAIT)) != NULL)
+    {
+      data = buf->data;
+      len = buf->len;
+
+      while (len > 0)
+        {
+          depth = z_zblue_block_begin();
+          ret = write(hci->txfd, data, len);
+          err = errno;
+          z_zblue_block_end(depth);
+          if (ret < 0)
+            {
+              if (err == EINTR)
+                {
+                  continue;
+                }
+
+              LOG_ERR("write failed: %d", err);
+              break;
+            }
+
+          data += ret;
+          len -= ret;
+        }
+
+      net_buf_unref(buf);
+    }
+}
+
+static int hci_open(const struct device *dev)
+{
+  struct hci_data *hci = dev->data;
+  struct k_work_queue_config cfg =
+  {
+    .name = "zblue_hci_tx",
+  };
+
+  struct k_work_sync sync;
+
+  hci->fd = open(CONFIG_ZBLUE_HCI_DEVICE, O_RDONLY | O_CLOEXEC);
+  if (hci->fd < 0)
+    {
+      LOG_ERR("cannot open %s: %d", CONFIG_ZBLUE_HCI_DEVICE, errno);
+      return -errno;
+    }
+
+  k_work_queue_init(&g_tx_queue);
+  k_work_queue_start(&g_tx_queue, g_tx_stack,
+                     K_KERNEL_STACK_SIZEOF(g_tx_stack),
+                     K_PRIO_COOP(CONFIG_BT_HCI_TX_PRIO), &cfg);
+  k_work_init(&g_tx_open, tx_open);
+  k_work_init(&g_tx_work, tx_work);
+  k_work_submit_to_queue(&g_tx_queue, &g_tx_open);
+  k_work_flush(&g_tx_open, &sync);
+  if (hci->txfd < 0)
+    {
+      LOG_ERR("cannot open %s for TX: %d", CONFIG_ZBLUE_HCI_DEVICE,
+              hci->txfd);
+      close(hci->fd);
+      hci->fd = -1;
+      return hci->txfd;
+    }
+
+  k_thread_create(&g_rx_thread, g_rx_stack,
+                  K_KERNEL_STACK_SIZEOF(g_rx_stack), rx_thread,
+                  (void *)dev, NULL, NULL,
+                  K_PRIO_COOP(CONFIG_BT_DRIVER_RX_HIGH_PRIO), 0, K_NO_WAIT);
+  k_thread_name_set(&g_rx_thread, "zblue_hci_rx");
+  return 0;
+}
+
+/* buf starts with the H4 packet type, as the upstream drivers expect.
+ * It is written by the TX work queue, as upstream H4 queues it for the
+ * UART.
+ *
+ * There is no close(): the RX thread cannot be stopped while it waits in
+ * read(), so bt_disable() returns -ENOSYS, as for upstream drivers
+ * without close().
+ */
+
+static int hci_send(const struct device *dev, struct net_buf *buf)
+{
+  UNUSED(dev);
+
+  k_fifo_put(&g_tx_fifo, buf);
+  k_work_submit_to_queue(&g_tx_queue, &g_tx_work);
+  return 0;
+}
+
+static DEVICE_API(bt_hci, g_hci_api) =
+{
+  .open = hci_open,
+  .send = hci_send,
+};
+
+/****************************************************************************
+ * Public Data
+ ****************************************************************************/
+
+const struct device __device_dts_ord_1 =
+{
+  .name = "zblue_hci",
+  .api  = &g_hci_api,
+  .data = &g_hci_data,
+};

Reply via email to