You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mynewt.apache.org by GitBox <gi...@apache.org> on 2020/05/20 05:52:49 UTC

[GitHub] [mynewt-nimble] rymanluk commented on a change in pull request #819: Add USB transport to NimBLE controller

rymanluk commented on a change in pull request #819:
URL: https://github.com/apache/mynewt-nimble/pull/819#discussion_r427747889



##########
File path: nimble/transport/usb/src/ble_hci_usb.c
##########
@@ -0,0 +1,420 @@
+/*
+ * 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.
+ */
+
+#include <assert.h>
+#include <stddef.h>
+#include "syscfg/syscfg.h"
+#include "sysinit/sysinit.h"
+#include "os/os.h"
+
+#include "nimble/ble.h"
+#include "nimble/ble_hci_trans.h"
+#include "nimble/hci_common.h"
+#include "transport/usb/ble_hci_usb.h"
+
+#include <class/bth/bth_device.h>
+
+/*
+ * The MBUF payload size must accommodate the HCI data header size plus the
+ * maximum ACL data packet length. The ACL block size is the size of the
+ * mbufs we will allocate.
+ */
+#define ACL_BLOCK_SIZE  OS_ALIGN(MYNEWT_VAL(BLE_ACL_BUF_SIZE) \
+                                 + BLE_MBUF_MEMBLOCK_OVERHEAD \
+                                 + BLE_HCI_DATA_HDR_SZ, OS_ALIGNMENT)
+
+struct usb_ble_hci_pool_cmd {
+    uint8_t cmd[BLE_HCI_TRANS_CMD_SZ];
+    bool allocated;
+};
+
+/* (Pseudo)pool for HCI commands */
+static struct usb_ble_hci_pool_cmd usb_ble_hci_pool_cmd;
+
+static ble_hci_trans_rx_cmd_fn *ble_hci_usb_rx_cmd_ll_cb;
+static void *ble_hci_usb_rx_cmd_ll_arg;
+
+static ble_hci_trans_rx_acl_fn *ble_hci_usb_rx_acl_ll_cb;
+static void *ble_hci_usb_rx_acl_ll_arg;
+
+static struct os_mempool ble_hci_usb_evt_hi_pool;
+static os_membuf_t ble_hci_usb_evt_hi_buf[
+    OS_MEMPOOL_SIZE(MYNEWT_VAL(BLE_HCI_EVT_HI_BUF_COUNT),
+                    MYNEWT_VAL(BLE_HCI_EVT_BUF_SIZE))
+];
+
+static struct os_mempool ble_hci_usb_evt_lo_pool;
+static os_membuf_t ble_hci_usb_evt_lo_buf[
+    OS_MEMPOOL_SIZE(MYNEWT_VAL(BLE_HCI_EVT_LO_BUF_COUNT),
+                    MYNEWT_VAL(BLE_HCI_EVT_BUF_SIZE))
+];
+
+static uint8_t ble_hci_pool_acl_mempool_buf[
+    OS_MEMPOOL_BYTES(MYNEWT_VAL(BLE_ACL_BUF_COUNT),
+                     ACL_BLOCK_SIZE)];
+static struct os_mempool ble_hci_pool_acl_mempool;
+static struct os_mbuf_pool ble_hci_pool_acl_mbuf_pool;
+
+static struct os_mbuf *incoming_acl_data;
+
+static struct os_mbuf *
+ble_hci_trans_acl_buf_alloc(void)
+{
+    struct os_mbuf *m;
+
+    m = os_mbuf_get_pkthdr(&ble_hci_pool_acl_mbuf_pool,
+                           sizeof(struct ble_mbuf_hdr));
+    return m;
+}
+
+void
+ble_hci_trans_cfg_ll(ble_hci_trans_rx_cmd_fn *cmd_cb,
+                     void *cmd_arg,
+                     ble_hci_trans_rx_acl_fn *acl_cb,
+                     void *acl_arg)
+{
+    ble_hci_usb_rx_cmd_ll_cb = cmd_cb;
+    ble_hci_usb_rx_cmd_ll_arg = cmd_arg;
+    ble_hci_usb_rx_acl_ll_cb = acl_cb;
+    ble_hci_usb_rx_acl_ll_arg = acl_arg;
+}
+
+int
+ble_hci_trans_hs_acl_tx(struct os_mbuf *om)
+{
+    int rc;
+
+    assert(ble_hci_usb_rx_acl_ll_cb != NULL);
+
+    rc = ble_hci_usb_rx_acl_ll_cb(om, ble_hci_usb_rx_acl_ll_arg);
+
+    return rc;
+}
+
+#define BLE_HCI_USB_EVT_COUNT  \
+    (MYNEWT_VAL(BLE_HCI_EVT_HI_BUF_COUNT) + MYNEWT_VAL(BLE_HCI_EVT_LO_BUF_COUNT))
+
+/**
+ * A packet to be sent over the USB.  This can be a command, an event, or ACL
+ * data.
+ */
+struct ble_hci_pkt {
+    STAILQ_ENTRY(ble_hci_pkt) next;
+    void *data;
+};
+
+static struct os_mempool ble_hci_pkt_pool;
+static os_membuf_t ble_hci_pkt_buf[
+    OS_MEMPOOL_SIZE(BLE_HCI_USB_EVT_COUNT + 1 +
+                    MYNEWT_VAL(BLE_HCI_ACL_OUT_COUNT),
+                    sizeof(struct ble_hci_pkt))];
+
+struct tx_queue {
+    STAILQ_HEAD(, ble_hci_pkt) queue;
+};
+
+static struct tx_queue ble_hci_tx_acl_queue = {STAILQ_HEAD_INITIALIZER(ble_hci_tx_acl_queue.queue)};
+static struct tx_queue ble_hci_tx_evt_queue = { STAILQ_HEAD_INITIALIZER(ble_hci_tx_evt_queue.queue) };
+
+/*
+ * TinyUSB callbacks.
+ */
+void
+tud_bt_acl_data_sent_cb(uint16_t sent_bytes)
+{
+    struct os_mbuf *om;
+    struct ble_hci_pkt *acl = STAILQ_FIRST(&ble_hci_tx_acl_queue.queue);
+
+    assert(acl != NULL);
+    om = acl->data;
+    assert(om != NULL && om->om_len >= sent_bytes);
+    os_mbuf_adj(om, sent_bytes);
+
+    while (om != NULL && om->om_len == 0) {
+        acl->data = SLIST_NEXT(om, om_next);
+        os_mbuf_free(om);

Review comment:
       did you consider to use  `os_mbuf_free_chain` instead?

##########
File path: nimble/transport/usb/src/ble_hci_usb.c
##########
@@ -0,0 +1,420 @@
+/*
+ * 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.
+ */
+
+#include <assert.h>
+#include <stddef.h>
+#include "syscfg/syscfg.h"
+#include "sysinit/sysinit.h"
+#include "os/os.h"
+
+#include "nimble/ble.h"
+#include "nimble/ble_hci_trans.h"
+#include "nimble/hci_common.h"
+#include "transport/usb/ble_hci_usb.h"
+
+#include <class/bth/bth_device.h>
+
+/*
+ * The MBUF payload size must accommodate the HCI data header size plus the
+ * maximum ACL data packet length. The ACL block size is the size of the
+ * mbufs we will allocate.
+ */
+#define ACL_BLOCK_SIZE  OS_ALIGN(MYNEWT_VAL(BLE_ACL_BUF_SIZE) \
+                                 + BLE_MBUF_MEMBLOCK_OVERHEAD \
+                                 + BLE_HCI_DATA_HDR_SZ, OS_ALIGNMENT)
+
+struct usb_ble_hci_pool_cmd {
+    uint8_t cmd[BLE_HCI_TRANS_CMD_SZ];
+    bool allocated;
+};
+
+/* (Pseudo)pool for HCI commands */
+static struct usb_ble_hci_pool_cmd usb_ble_hci_pool_cmd;
+
+static ble_hci_trans_rx_cmd_fn *ble_hci_usb_rx_cmd_ll_cb;
+static void *ble_hci_usb_rx_cmd_ll_arg;
+
+static ble_hci_trans_rx_acl_fn *ble_hci_usb_rx_acl_ll_cb;
+static void *ble_hci_usb_rx_acl_ll_arg;
+
+static struct os_mempool ble_hci_usb_evt_hi_pool;
+static os_membuf_t ble_hci_usb_evt_hi_buf[
+    OS_MEMPOOL_SIZE(MYNEWT_VAL(BLE_HCI_EVT_HI_BUF_COUNT),
+                    MYNEWT_VAL(BLE_HCI_EVT_BUF_SIZE))
+];
+
+static struct os_mempool ble_hci_usb_evt_lo_pool;
+static os_membuf_t ble_hci_usb_evt_lo_buf[
+    OS_MEMPOOL_SIZE(MYNEWT_VAL(BLE_HCI_EVT_LO_BUF_COUNT),
+                    MYNEWT_VAL(BLE_HCI_EVT_BUF_SIZE))
+];
+
+static uint8_t ble_hci_pool_acl_mempool_buf[
+    OS_MEMPOOL_BYTES(MYNEWT_VAL(BLE_ACL_BUF_COUNT),
+                     ACL_BLOCK_SIZE)];
+static struct os_mempool ble_hci_pool_acl_mempool;
+static struct os_mbuf_pool ble_hci_pool_acl_mbuf_pool;
+
+static struct os_mbuf *incoming_acl_data;
+
+static struct os_mbuf *
+ble_hci_trans_acl_buf_alloc(void)
+{
+    struct os_mbuf *m;
+
+    m = os_mbuf_get_pkthdr(&ble_hci_pool_acl_mbuf_pool,
+                           sizeof(struct ble_mbuf_hdr));
+    return m;
+}
+
+void
+ble_hci_trans_cfg_ll(ble_hci_trans_rx_cmd_fn *cmd_cb,
+                     void *cmd_arg,
+                     ble_hci_trans_rx_acl_fn *acl_cb,
+                     void *acl_arg)
+{
+    ble_hci_usb_rx_cmd_ll_cb = cmd_cb;
+    ble_hci_usb_rx_cmd_ll_arg = cmd_arg;
+    ble_hci_usb_rx_acl_ll_cb = acl_cb;
+    ble_hci_usb_rx_acl_ll_arg = acl_arg;
+}
+
+int
+ble_hci_trans_hs_acl_tx(struct os_mbuf *om)
+{
+    int rc;
+
+    assert(ble_hci_usb_rx_acl_ll_cb != NULL);
+
+    rc = ble_hci_usb_rx_acl_ll_cb(om, ble_hci_usb_rx_acl_ll_arg);
+
+    return rc;
+}
+
+#define BLE_HCI_USB_EVT_COUNT  \
+    (MYNEWT_VAL(BLE_HCI_EVT_HI_BUF_COUNT) + MYNEWT_VAL(BLE_HCI_EVT_LO_BUF_COUNT))
+
+/**
+ * A packet to be sent over the USB.  This can be a command, an event, or ACL
+ * data.
+ */
+struct ble_hci_pkt {
+    STAILQ_ENTRY(ble_hci_pkt) next;
+    void *data;
+};
+
+static struct os_mempool ble_hci_pkt_pool;
+static os_membuf_t ble_hci_pkt_buf[
+    OS_MEMPOOL_SIZE(BLE_HCI_USB_EVT_COUNT + 1 +
+                    MYNEWT_VAL(BLE_HCI_ACL_OUT_COUNT),
+                    sizeof(struct ble_hci_pkt))];
+
+struct tx_queue {
+    STAILQ_HEAD(, ble_hci_pkt) queue;
+};
+
+static struct tx_queue ble_hci_tx_acl_queue = {STAILQ_HEAD_INITIALIZER(ble_hci_tx_acl_queue.queue)};
+static struct tx_queue ble_hci_tx_evt_queue = { STAILQ_HEAD_INITIALIZER(ble_hci_tx_evt_queue.queue) };
+
+/*
+ * TinyUSB callbacks.
+ */
+void
+tud_bt_acl_data_sent_cb(uint16_t sent_bytes)
+{
+    struct os_mbuf *om;
+    struct ble_hci_pkt *acl = STAILQ_FIRST(&ble_hci_tx_acl_queue.queue);
+
+    assert(acl != NULL);
+    om = acl->data;
+    assert(om != NULL && om->om_len >= sent_bytes);
+    os_mbuf_adj(om, sent_bytes);
+
+    while (om != NULL && om->om_len == 0) {
+        acl->data = SLIST_NEXT(om, om_next);
+        os_mbuf_free(om);
+        om = acl->data;
+    }
+
+    if (om == NULL) {
+        STAILQ_REMOVE_HEAD(&ble_hci_tx_acl_queue.queue, next);
+        os_memblock_put(&ble_hci_pkt_pool, acl);
+        acl = STAILQ_FIRST(&ble_hci_tx_acl_queue.queue);
+        if (acl != NULL) {
+            om = acl->data;
+        }
+    }
+
+    if (om != NULL) {
+        tud_bt_acl_data_send(om->om_data, om->om_len);
+    }
+}
+
+void
+tud_bt_event_sent_cb(uint16_t sent_bytes)
+{
+    struct ble_hci_pkt *evt = STAILQ_FIRST(&ble_hci_tx_evt_queue.queue);
+    uint8_t *hci_ev;
+
+    assert(evt != NULL);
+    hci_ev = evt->data;
+
+    assert(hci_ev != NULL && hci_ev[1] + sizeof(struct ble_hci_ev) == sent_bytes);
+
+    ble_hci_trans_buf_free(hci_ev);
+
+    STAILQ_REMOVE_HEAD(&ble_hci_tx_evt_queue.queue, next);
+    os_memblock_put(&ble_hci_pkt_pool, evt);
+
+    evt = STAILQ_FIRST(&ble_hci_tx_evt_queue.queue);
+    if (evt != NULL) {
+        hci_ev = evt->data;
+        tud_bt_event_send(hci_ev, hci_ev[1] + sizeof(struct ble_hci_ev));
+    }
+}
+
+void
+tud_bt_acl_data_received_cb(void *acl_data, uint16_t data_len)
+{
+    uint8_t *data;
+    uint32_t len;
+    struct os_mbuf *om = incoming_acl_data;
+    int rc;
+
+    if (om == NULL) {
+        om = ble_hci_trans_acl_buf_alloc();
+        assert(om != 0);
+    }
+    assert(om->om_len + data_len <= MYNEWT_VAL(BLE_ACL_BUF_SIZE) + 4);
+
+    os_mbuf_append(om, acl_data, data_len);
+    incoming_acl_data = om;
+    if (om->om_len > BLE_HCI_DATA_HDR_SZ) {
+        data = incoming_acl_data->om_data;
+        len = data[2] + (data[3] << 8) + BLE_HCI_DATA_HDR_SZ;
+        if (len <= incoming_acl_data->om_len) {
+            incoming_acl_data = NULL;
+            rc = ble_hci_usb_rx_acl_ll_cb(om, ble_hci_usb_rx_acl_ll_arg);
+            (void)rc;
+        }
+    }
+}
+
+void
+tud_bt_hci_cmd_cb(void *hci_cmd, size_t cmd_len)
+{
+    uint8_t *buf;
+    int rc = -1;
+
+    buf = ble_hci_trans_buf_alloc(BLE_HCI_TRANS_BUF_CMD);
+    assert(buf != NULL);
+    memcpy(buf, hci_cmd, cmd_len);
+
+    assert(ble_hci_usb_rx_cmd_ll_cb);
+    if (ble_hci_usb_rx_cmd_ll_cb) {
+        rc = ble_hci_usb_rx_cmd_ll_cb(buf, ble_hci_usb_rx_cmd_ll_arg);
+    }
+
+    if (rc != 0) {
+        ble_hci_trans_buf_free(buf);
+    }
+}
+
+static int
+ble_hci_trans_ll_tx(struct tx_queue *queue, struct os_mbuf *om)
+{
+    struct ble_hci_pkt *pkt;
+    os_sr_t sr;
+
+    /* If this packet is zero length, just free it */
+    if (OS_MBUF_PKTLEN(om) == 0) {
+        os_mbuf_free_chain(om);
+        return 0;
+    }
+
+    pkt = os_memblock_get(&ble_hci_pkt_pool);
+    if (pkt == NULL) {
+        os_mbuf_free_chain(om);
+        return BLE_ERR_MEM_CAPACITY;
+    }
+
+    pkt->data = om;
+    OS_ENTER_CRITICAL(sr);
+    bool first = STAILQ_EMPTY(&queue->queue);
+    STAILQ_INSERT_TAIL(&queue->queue, pkt, next);
+    if (first) {
+        tud_bt_acl_data_send(om->om_data, om->om_len);
+    }
+    OS_EXIT_CRITICAL(sr);
+
+    return 0;
+}
+
+int
+ble_hci_trans_ll_acl_tx(struct os_mbuf *om)
+{
+    return ble_hci_trans_ll_tx(&ble_hci_tx_acl_queue, om);
+}
+
+int
+ble_hci_trans_ll_evt_tx(uint8_t *hci_ev)
+{
+    struct ble_hci_pkt *pkt;
+    os_sr_t sr;
+
+    assert(hci_ev != NULL);
+
+    pkt = os_memblock_get(&ble_hci_pkt_pool);
+    if (pkt == NULL) {
+        ble_hci_trans_buf_free(hci_ev);
+        return BLE_ERR_MEM_CAPACITY;
+    }
+
+    pkt->data = hci_ev;
+    OS_ENTER_CRITICAL(sr);
+    bool first = STAILQ_EMPTY(&ble_hci_tx_evt_queue.queue);
+    STAILQ_INSERT_TAIL(&ble_hci_tx_evt_queue.queue, pkt, next);
+    if (first) {
+        tud_bt_event_send(hci_ev, hci_ev[1] + sizeof(struct ble_hci_ev));
+    }
+    OS_EXIT_CRITICAL(sr);
+
+    return 0;
+}
+
+uint8_t *
+ble_hci_trans_buf_alloc(int type)
+{
+    uint8_t *buf;
+
+    switch (type) {
+    case BLE_HCI_TRANS_BUF_CMD:
+        assert(!usb_ble_hci_pool_cmd.allocated);
+        usb_ble_hci_pool_cmd.allocated = 1;
+        buf = usb_ble_hci_pool_cmd.cmd;
+        break;
+
+    case BLE_HCI_TRANS_BUF_EVT_HI:
+        buf = os_memblock_get(&ble_hci_usb_evt_hi_pool);
+        if (buf == NULL) {
+            /* If no high-priority event buffers remain, try to grab a
+             * low-priority one.
+             */
+            buf = ble_hci_trans_buf_alloc(BLE_HCI_TRANS_BUF_EVT_LO);
+        }
+        break;
+
+    case BLE_HCI_TRANS_BUF_EVT_LO:
+        buf = os_memblock_get(&ble_hci_usb_evt_lo_pool);
+        break;
+
+    default:
+        assert(0);
+        buf = NULL;
+    }
+
+    return buf;
+}
+
+void
+ble_hci_trans_buf_free(uint8_t *buf)
+{
+    int rc;
+
+    /* XXX: this may look a bit odd, but the controller uses the command
+     * buffer to send back the command complete/status as an immediate
+     * response to the command. This was done to insure that the controller
+     * could always send back one of these events when a command was received.
+     * Thus, we check to see which pool the buffer came from so we can free
+     * it to the appropriate pool
+     */
+    if (os_memblock_from(&ble_hci_usb_evt_hi_pool, buf)) {
+        rc = os_memblock_put(&ble_hci_usb_evt_hi_pool, buf);
+        assert(rc == 0);
+    } else if (os_memblock_from(&ble_hci_usb_evt_lo_pool, buf)) {
+        rc = os_memblock_put(&ble_hci_usb_evt_lo_pool, buf);
+        assert(rc == 0);
+    } else {
+        assert(usb_ble_hci_pool_cmd.allocated);
+        usb_ble_hci_pool_cmd.allocated = 0;
+    }
+}
+
+/**
+ * Unsupported; the USB transport does not have a dedicated ACL data packet
+ * pool.
+ */
+int
+ble_hci_trans_set_acl_free_cb(os_mempool_put_fn *cb, void *arg)
+{
+    return BLE_ERR_UNSUPPORTED;
+}
+
+int
+ble_hci_trans_reset(void)
+{
+    return 0;
+}
+
+void
+ble_hci_usb_init(void)
+{
+    int rc;
+
+    /* Ensure this function only gets called by sysinit. */
+    SYSINIT_ASSERT_ACTIVE();
+
+    rc = os_mempool_init(&ble_hci_pool_acl_mempool,

Review comment:
       these two calls `os_mempool_init` and `os_mbuf_pool_init` you chould replace with a single call `mem_init_mbuf_pool`

##########
File path: nimble/transport/usb/src/ble_hci_usb.c
##########
@@ -0,0 +1,420 @@
+/*
+ * 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.
+ */
+
+#include <assert.h>
+#include <stddef.h>
+#include "syscfg/syscfg.h"
+#include "sysinit/sysinit.h"
+#include "os/os.h"
+
+#include "nimble/ble.h"
+#include "nimble/ble_hci_trans.h"
+#include "nimble/hci_common.h"
+#include "transport/usb/ble_hci_usb.h"
+
+#include <class/bth/bth_device.h>
+
+/*
+ * The MBUF payload size must accommodate the HCI data header size plus the
+ * maximum ACL data packet length. The ACL block size is the size of the
+ * mbufs we will allocate.
+ */
+#define ACL_BLOCK_SIZE  OS_ALIGN(MYNEWT_VAL(BLE_ACL_BUF_SIZE) \
+                                 + BLE_MBUF_MEMBLOCK_OVERHEAD \
+                                 + BLE_HCI_DATA_HDR_SZ, OS_ALIGNMENT)
+
+struct usb_ble_hci_pool_cmd {
+    uint8_t cmd[BLE_HCI_TRANS_CMD_SZ];
+    bool allocated;
+};
+
+/* (Pseudo)pool for HCI commands */
+static struct usb_ble_hci_pool_cmd usb_ble_hci_pool_cmd;
+
+static ble_hci_trans_rx_cmd_fn *ble_hci_usb_rx_cmd_ll_cb;
+static void *ble_hci_usb_rx_cmd_ll_arg;
+
+static ble_hci_trans_rx_acl_fn *ble_hci_usb_rx_acl_ll_cb;
+static void *ble_hci_usb_rx_acl_ll_arg;
+
+static struct os_mempool ble_hci_usb_evt_hi_pool;
+static os_membuf_t ble_hci_usb_evt_hi_buf[
+    OS_MEMPOOL_SIZE(MYNEWT_VAL(BLE_HCI_EVT_HI_BUF_COUNT),
+                    MYNEWT_VAL(BLE_HCI_EVT_BUF_SIZE))
+];
+
+static struct os_mempool ble_hci_usb_evt_lo_pool;
+static os_membuf_t ble_hci_usb_evt_lo_buf[
+    OS_MEMPOOL_SIZE(MYNEWT_VAL(BLE_HCI_EVT_LO_BUF_COUNT),
+                    MYNEWT_VAL(BLE_HCI_EVT_BUF_SIZE))
+];
+
+static uint8_t ble_hci_pool_acl_mempool_buf[
+    OS_MEMPOOL_BYTES(MYNEWT_VAL(BLE_ACL_BUF_COUNT),
+                     ACL_BLOCK_SIZE)];
+static struct os_mempool ble_hci_pool_acl_mempool;
+static struct os_mbuf_pool ble_hci_pool_acl_mbuf_pool;
+
+static struct os_mbuf *incoming_acl_data;
+
+static struct os_mbuf *
+ble_hci_trans_acl_buf_alloc(void)
+{
+    struct os_mbuf *m;
+
+    m = os_mbuf_get_pkthdr(&ble_hci_pool_acl_mbuf_pool,
+                           sizeof(struct ble_mbuf_hdr));
+    return m;
+}
+
+void
+ble_hci_trans_cfg_ll(ble_hci_trans_rx_cmd_fn *cmd_cb,
+                     void *cmd_arg,
+                     ble_hci_trans_rx_acl_fn *acl_cb,
+                     void *acl_arg)
+{
+    ble_hci_usb_rx_cmd_ll_cb = cmd_cb;
+    ble_hci_usb_rx_cmd_ll_arg = cmd_arg;
+    ble_hci_usb_rx_acl_ll_cb = acl_cb;
+    ble_hci_usb_rx_acl_ll_arg = acl_arg;
+}
+
+int
+ble_hci_trans_hs_acl_tx(struct os_mbuf *om)
+{
+    int rc;
+
+    assert(ble_hci_usb_rx_acl_ll_cb != NULL);
+
+    rc = ble_hci_usb_rx_acl_ll_cb(om, ble_hci_usb_rx_acl_ll_arg);
+
+    return rc;
+}
+
+#define BLE_HCI_USB_EVT_COUNT  \
+    (MYNEWT_VAL(BLE_HCI_EVT_HI_BUF_COUNT) + MYNEWT_VAL(BLE_HCI_EVT_LO_BUF_COUNT))
+
+/**
+ * A packet to be sent over the USB.  This can be a command, an event, or ACL
+ * data.
+ */
+struct ble_hci_pkt {
+    STAILQ_ENTRY(ble_hci_pkt) next;
+    void *data;
+};
+
+static struct os_mempool ble_hci_pkt_pool;
+static os_membuf_t ble_hci_pkt_buf[
+    OS_MEMPOOL_SIZE(BLE_HCI_USB_EVT_COUNT + 1 +
+                    MYNEWT_VAL(BLE_HCI_ACL_OUT_COUNT),
+                    sizeof(struct ble_hci_pkt))];
+
+struct tx_queue {
+    STAILQ_HEAD(, ble_hci_pkt) queue;
+};
+
+static struct tx_queue ble_hci_tx_acl_queue = {STAILQ_HEAD_INITIALIZER(ble_hci_tx_acl_queue.queue)};
+static struct tx_queue ble_hci_tx_evt_queue = { STAILQ_HEAD_INITIALIZER(ble_hci_tx_evt_queue.queue) };
+
+/*
+ * TinyUSB callbacks.
+ */
+void
+tud_bt_acl_data_sent_cb(uint16_t sent_bytes)
+{
+    struct os_mbuf *om;
+    struct ble_hci_pkt *acl = STAILQ_FIRST(&ble_hci_tx_acl_queue.queue);
+
+    assert(acl != NULL);
+    om = acl->data;
+    assert(om != NULL && om->om_len >= sent_bytes);
+    os_mbuf_adj(om, sent_bytes);
+
+    while (om != NULL && om->om_len == 0) {
+        acl->data = SLIST_NEXT(om, om_next);
+        os_mbuf_free(om);
+        om = acl->data;
+    }
+
+    if (om == NULL) {
+        STAILQ_REMOVE_HEAD(&ble_hci_tx_acl_queue.queue, next);
+        os_memblock_put(&ble_hci_pkt_pool, acl);
+        acl = STAILQ_FIRST(&ble_hci_tx_acl_queue.queue);
+        if (acl != NULL) {
+            om = acl->data;
+        }
+    }
+
+    if (om != NULL) {
+        tud_bt_acl_data_send(om->om_data, om->om_len);

Review comment:
       It looks like mbuf chaining is not used. Do we make sure somewhere that memory is flat in that mbuf?

##########
File path: nimble/transport/usb/src/ble_hci_usb.c
##########
@@ -0,0 +1,420 @@
+/*
+ * 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.
+ */
+
+#include <assert.h>
+#include <stddef.h>
+#include "syscfg/syscfg.h"
+#include "sysinit/sysinit.h"
+#include "os/os.h"
+
+#include "nimble/ble.h"
+#include "nimble/ble_hci_trans.h"
+#include "nimble/hci_common.h"
+#include "transport/usb/ble_hci_usb.h"
+
+#include <class/bth/bth_device.h>
+
+/*
+ * The MBUF payload size must accommodate the HCI data header size plus the
+ * maximum ACL data packet length. The ACL block size is the size of the
+ * mbufs we will allocate.
+ */
+#define ACL_BLOCK_SIZE  OS_ALIGN(MYNEWT_VAL(BLE_ACL_BUF_SIZE) \
+                                 + BLE_MBUF_MEMBLOCK_OVERHEAD \
+                                 + BLE_HCI_DATA_HDR_SZ, OS_ALIGNMENT)
+
+struct usb_ble_hci_pool_cmd {
+    uint8_t cmd[BLE_HCI_TRANS_CMD_SZ];
+    bool allocated;
+};
+
+/* (Pseudo)pool for HCI commands */
+static struct usb_ble_hci_pool_cmd usb_ble_hci_pool_cmd;
+
+static ble_hci_trans_rx_cmd_fn *ble_hci_usb_rx_cmd_ll_cb;
+static void *ble_hci_usb_rx_cmd_ll_arg;
+
+static ble_hci_trans_rx_acl_fn *ble_hci_usb_rx_acl_ll_cb;
+static void *ble_hci_usb_rx_acl_ll_arg;
+
+static struct os_mempool ble_hci_usb_evt_hi_pool;
+static os_membuf_t ble_hci_usb_evt_hi_buf[
+    OS_MEMPOOL_SIZE(MYNEWT_VAL(BLE_HCI_EVT_HI_BUF_COUNT),
+                    MYNEWT_VAL(BLE_HCI_EVT_BUF_SIZE))
+];
+
+static struct os_mempool ble_hci_usb_evt_lo_pool;
+static os_membuf_t ble_hci_usb_evt_lo_buf[
+    OS_MEMPOOL_SIZE(MYNEWT_VAL(BLE_HCI_EVT_LO_BUF_COUNT),
+                    MYNEWT_VAL(BLE_HCI_EVT_BUF_SIZE))
+];
+
+static uint8_t ble_hci_pool_acl_mempool_buf[
+    OS_MEMPOOL_BYTES(MYNEWT_VAL(BLE_ACL_BUF_COUNT),
+                     ACL_BLOCK_SIZE)];
+static struct os_mempool ble_hci_pool_acl_mempool;
+static struct os_mbuf_pool ble_hci_pool_acl_mbuf_pool;
+
+static struct os_mbuf *incoming_acl_data;
+
+static struct os_mbuf *
+ble_hci_trans_acl_buf_alloc(void)
+{
+    struct os_mbuf *m;
+
+    m = os_mbuf_get_pkthdr(&ble_hci_pool_acl_mbuf_pool,
+                           sizeof(struct ble_mbuf_hdr));
+    return m;
+}
+
+void
+ble_hci_trans_cfg_ll(ble_hci_trans_rx_cmd_fn *cmd_cb,
+                     void *cmd_arg,
+                     ble_hci_trans_rx_acl_fn *acl_cb,
+                     void *acl_arg)
+{
+    ble_hci_usb_rx_cmd_ll_cb = cmd_cb;
+    ble_hci_usb_rx_cmd_ll_arg = cmd_arg;
+    ble_hci_usb_rx_acl_ll_cb = acl_cb;
+    ble_hci_usb_rx_acl_ll_arg = acl_arg;
+}
+
+int
+ble_hci_trans_hs_acl_tx(struct os_mbuf *om)
+{
+    int rc;
+
+    assert(ble_hci_usb_rx_acl_ll_cb != NULL);
+
+    rc = ble_hci_usb_rx_acl_ll_cb(om, ble_hci_usb_rx_acl_ll_arg);
+
+    return rc;
+}
+
+#define BLE_HCI_USB_EVT_COUNT  \
+    (MYNEWT_VAL(BLE_HCI_EVT_HI_BUF_COUNT) + MYNEWT_VAL(BLE_HCI_EVT_LO_BUF_COUNT))
+
+/**
+ * A packet to be sent over the USB.  This can be a command, an event, or ACL
+ * data.
+ */
+struct ble_hci_pkt {
+    STAILQ_ENTRY(ble_hci_pkt) next;
+    void *data;
+};
+
+static struct os_mempool ble_hci_pkt_pool;
+static os_membuf_t ble_hci_pkt_buf[
+    OS_MEMPOOL_SIZE(BLE_HCI_USB_EVT_COUNT + 1 +
+                    MYNEWT_VAL(BLE_HCI_ACL_OUT_COUNT),
+                    sizeof(struct ble_hci_pkt))];
+
+struct tx_queue {
+    STAILQ_HEAD(, ble_hci_pkt) queue;
+};
+
+static struct tx_queue ble_hci_tx_acl_queue = {STAILQ_HEAD_INITIALIZER(ble_hci_tx_acl_queue.queue)};
+static struct tx_queue ble_hci_tx_evt_queue = { STAILQ_HEAD_INITIALIZER(ble_hci_tx_evt_queue.queue) };
+
+/*
+ * TinyUSB callbacks.
+ */
+void
+tud_bt_acl_data_sent_cb(uint16_t sent_bytes)
+{
+    struct os_mbuf *om;
+    struct ble_hci_pkt *acl = STAILQ_FIRST(&ble_hci_tx_acl_queue.queue);
+
+    assert(acl != NULL);
+    om = acl->data;
+    assert(om != NULL && om->om_len >= sent_bytes);
+    os_mbuf_adj(om, sent_bytes);
+
+    while (om != NULL && om->om_len == 0) {
+        acl->data = SLIST_NEXT(om, om_next);
+        os_mbuf_free(om);
+        om = acl->data;
+    }
+
+    if (om == NULL) {
+        STAILQ_REMOVE_HEAD(&ble_hci_tx_acl_queue.queue, next);
+        os_memblock_put(&ble_hci_pkt_pool, acl);
+        acl = STAILQ_FIRST(&ble_hci_tx_acl_queue.queue);
+        if (acl != NULL) {
+            om = acl->data;
+        }
+    }
+
+    if (om != NULL) {
+        tud_bt_acl_data_send(om->om_data, om->om_len);
+    }
+}
+
+void
+tud_bt_event_sent_cb(uint16_t sent_bytes)
+{
+    struct ble_hci_pkt *evt = STAILQ_FIRST(&ble_hci_tx_evt_queue.queue);
+    uint8_t *hci_ev;
+
+    assert(evt != NULL);
+    hci_ev = evt->data;
+
+    assert(hci_ev != NULL && hci_ev[1] + sizeof(struct ble_hci_ev) == sent_bytes);
+
+    ble_hci_trans_buf_free(hci_ev);
+
+    STAILQ_REMOVE_HEAD(&ble_hci_tx_evt_queue.queue, next);
+    os_memblock_put(&ble_hci_pkt_pool, evt);
+
+    evt = STAILQ_FIRST(&ble_hci_tx_evt_queue.queue);
+    if (evt != NULL) {
+        hci_ev = evt->data;
+        tud_bt_event_send(hci_ev, hci_ev[1] + sizeof(struct ble_hci_ev));
+    }
+}
+
+void
+tud_bt_acl_data_received_cb(void *acl_data, uint16_t data_len)
+{
+    uint8_t *data;
+    uint32_t len;
+    struct os_mbuf *om = incoming_acl_data;
+    int rc;
+
+    if (om == NULL) {
+        om = ble_hci_trans_acl_buf_alloc();
+        assert(om != 0);
+    }
+    assert(om->om_len + data_len <= MYNEWT_VAL(BLE_ACL_BUF_SIZE) + 4);

Review comment:
       ah yes, here it is the check I was asking above.




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org