Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
138 changes: 38 additions & 100 deletions wireless/bluetooth/zblue/port/src/hci.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,6 @@
/* 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.
*/

/****************************************************************************
Expand Down Expand Up @@ -54,8 +51,7 @@ LOG_MODULE_REGISTER(zblue_hci, CONFIG_LOG_DEFAULT_LEVEL);
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 */
int fd;
};

/****************************************************************************
Expand All @@ -65,19 +61,12 @@ struct hci_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
****************************************************************************/
Expand Down Expand Up @@ -259,99 +248,17 @@ static void rx_thread(void *p1, void *p2, void *p3)
}
}

/* 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);
hci->fd = open(CONFIG_ZBLUE_HCI_DEVICE, O_RDWR | 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,
Expand All @@ -361,8 +268,10 @@ static int hci_open(const struct device *dev)
}

/* 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.
* The host sends from its work queues and, for the host flow control,
* from any thread that frees a buffer: all are threads of the application
* and share its file descriptors. uart_bth4 writes each packet whole
* under its send lock, so the writes of different threads do not mix.
*
* There is no close(): the RX thread cannot be stopped while it waits in
* read(), so bt_disable() returns -ENOSYS, as for upstream drivers
Expand All @@ -371,10 +280,39 @@ static int hci_open(const struct device *dev)

static int hci_send(const struct device *dev, struct net_buf *buf)
{
UNUSED(dev);
struct hci_data *hci = dev->data;
const uint8_t *data = buf->data;
size_t len = buf->len;
ssize_t ret;
int depth;
int err;

/* The controller driver may block in write(): let the other
* cooperative threads, the RX thread among them, run meanwhile.
*/

while (len > 0)
{
depth = z_zblue_block_begin();
ret = write(hci->fd, data, len);
err = errno;
z_zblue_block_end(depth);
if (ret < 0)
{
if (err == EINTR)
{
continue;
}

LOG_ERR("write failed: %d", err);
return -err;
}

data += ret;
len -= ret;
}

k_fifo_put(&g_tx_fifo, buf);
k_work_submit_to_queue(&g_tx_queue, &g_tx_work);
net_buf_unref(buf);
return 0;
}

Expand Down
10 changes: 6 additions & 4 deletions wireless/bluetooth/zblue/port/src/work.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@
*
****************************************************************************/

/* Zephyr work queues on NuttX work queues (work_queue_create()).
/* Zephyr work queues on NuttX work queues (work_queue_create_pthread()).
*
* Each k_work_q is a NuttX work queue with one thread. The NuttX work
* Each k_work_q is a NuttX work queue with one thread, a pthread of the
* application: the work runs with its file descriptors. The NuttX work
* item of a k_work only runs work_entry() on that thread when the item is
* due. The Zephyr state (QUEUED, DELAYED, RUNNING, CANCELING) is kept in
* the k_work flags, under one lock, and gives the Zephyr return values;
Expand Down Expand Up @@ -398,8 +399,9 @@ void k_work_queue_start(struct k_work_q *queue, k_thread_stack_t *stack,
queue->thread.coop = prio < 0;
queue->thread_id = &queue->thread;

queue->wq = work_queue_create(queue->thread.name, z_zblue_priority(prio),
NULL, stack_size + ZBLUE_STACK_EXTRA, 1);
queue->wq = work_queue_create_pthread(queue->thread.name,
z_zblue_priority(prio), NULL,
stack_size + ZBLUE_STACK_EXTRA, 1);
if (queue->wq == NULL ||
work_queue_wq(queue->wq, &queue->start, queue_start_entry,
queue, 0) < 0)
Expand Down
Loading