Bluetooth: Add basic HCI command processing

This patch adds basic HCI command processing into the HCI core. The
commands are passed to the registered HCI driver with the help of a
FIFO and an associated fiber which wakes up whenever there's data in
the FIFO and the controller is ready to accept new commands.

The controller readiness (i.e. the num_cmds value returned in
cmd_status & cmd_complete events) is tracked with the help of a
semaphore. We only track whether the value is > 0 or not and never
send more than one command at a time. The reason for this is to keep
the tracking of pending command parameters simple in the form of a
single variable (dev.sent_cmd) rather than a list of pending buffers.

This patch also adds a very basic HCI initialization routine which
right now only consists of a single HCI_Reset command.

Change-Id: Ic68b298e4ea41334e49c3fe6bd4012a069ef6fdc
Co-authored-by: Andrei Emeltchenko <andrei.emeltchenko@intel.com>
Signed-off-by: Johan Hedberg <johan.hedberg@intel.com>
This commit is contained in:
Johan Hedberg 2015-04-14 15:41:55 +03:00 committed by Anas Nashif
commit 45554f5b72

View file

@ -36,11 +36,14 @@
#include <errno.h>
#include <misc/byteorder.h>
#include <bluetooth/hci.h>
#include <bluetooth/bluetooth.h>
/* Stacks for the fibers */
#define RX_STACK_SIZE 1024
#define CMD_STACK_SIZE 256
static char rx_fiber_stack[RX_STACK_SIZE];
static char cmd_fiber_stack[CMD_STACK_SIZE];
/* Available (free) buffers queue */
#define NUM_BUFS 5
@ -49,9 +52,19 @@ static struct nano_fifo free_bufs;
/* State tracking for the local Bluetooth controller */
static struct bt_dev {
/* Number of commands controller can accept */
uint8_t ncmd;
struct nano_sem ncmd_sem;
/* Last sent HCI command */
struct bt_buf *sent_cmd;
/* Queue for incoming HCI events & ACL data */
struct nano_fifo rx_queue;
/* Queue for outgoing HCI commands */
struct nano_fifo cmd_queue;
/* Registered HCI driver */
struct bt_driver *drv;
} dev;
@ -117,9 +130,187 @@ size_t bt_buf_tailroom(struct bt_buf *buf)
return BT_BUF_MAX_DATA - bt_buf_headroom(buf) - buf->len;
}
static struct bt_buf *bt_hci_cmd_create(uint16_t opcode, uint8_t param_len)
{
struct bt_hci_cmd_hdr *hdr;
struct bt_buf *buf;
BT_DBG("opcode %x param_len %u\n", opcode, param_len);
buf = bt_buf_get_reserve(dev.drv->head_reserve);
if (!buf) {
BT_ERR("Cannot get free buffer\n");
return NULL;
}
BT_DBG("buf %p\n", buf);
buf->type = BT_CMD;
buf->opcode = opcode;
buf->sync = NULL;
hdr = (void *)bt_buf_add(buf, sizeof(*hdr));
hdr->opcode = sys_cpu_to_le16(opcode);
hdr->param_len = param_len;
return buf;
}
static int bt_hci_cmd_send(uint16_t opcode, struct bt_buf *buf)
{
if (!buf) {
buf = bt_hci_cmd_create(opcode, 0);
if (!buf)
return -ENOBUFS;
}
BT_DBG("opcode %x len %u\n", opcode, buf->len);
nano_fifo_put(&dev.cmd_queue, buf);
return 0;
}
static void hci_acl(struct bt_buf *buf)
{
BT_DBG("\n");
}
/* HCI event processing */
static void hci_reset_complete(struct bt_buf *buf)
{
uint8_t status = buf->data[0];
BT_DBG("status %u\n", status);
if (status)
return;
}
static void hci_cmd_done(uint16_t opcode)
{
struct bt_buf *sent = dev.sent_cmd;
if (dev.sent_cmd->opcode != opcode) {
BT_ERR("Unexpected completion of opcode %x\n", opcode);
return;
}
dev.sent_cmd = NULL;
bt_buf_put(sent);
}
static void hci_cmd_complete(struct bt_buf *buf)
{
struct hci_evt_cmd_complete *evt = (void *)buf->data;
uint16_t opcode = sys_le16_to_cpu(evt->opcode);
BT_DBG("opcode %x\n", opcode);
bt_buf_pull(buf, sizeof(*evt));
switch (opcode) {
case BT_HCI_OP_RESET:
hci_reset_complete(buf);
break;
default:
BT_ERR("Unknown opcode %x\n", opcode);
break;
}
hci_cmd_done(opcode);
if (evt->ncmd && !dev.ncmd) {
/* Allow next command to be sent */
dev.ncmd = 1;
nano_fiber_sem_give(&dev.ncmd_sem);
}
}
static void hci_cmd_status(struct bt_buf *buf)
{
struct bt_hci_evt_cmd_status *evt = (void *)buf->data;
uint16_t opcode = sys_le16_to_cpu(evt->opcode);
BT_DBG("opcode %x\n", opcode);
bt_buf_pull(buf, sizeof(*evt));
switch (opcode) {
default:
BT_ERR("Unknown opcode %x", opcode);
break;
}
hci_cmd_done(opcode);
if (evt->ncmd && !dev.ncmd) {
/* Allow next command to be sent */
dev.ncmd = 1;
nano_fiber_sem_give(&dev.ncmd_sem);
}
}
static void hci_event(struct bt_buf *buf)
{
struct bt_hci_evt_hdr *hdr = (void *)buf->data;
BT_DBG("event %u\n", hdr->evt);
bt_buf_pull(buf, sizeof(*hdr));
switch (hdr->evt) {
case BT_HCI_EVT_CMD_COMPLETE:
hci_cmd_complete(buf);
break;
case BT_HCI_EVT_CMD_STATUS:
hci_cmd_status(buf);
break;
default:
BT_ERR("Unknown event %u\n", hdr->evt);
break;
}
}
static void hci_receive_packet(struct bt_buf *buf)
{
BT_DBG("buf %p type %u\n", buf, buf->type);
switch (buf->type) {
case BT_ACL:
hci_acl(buf);
break;
case BT_EVT:
hci_event(buf);
break;
default:
return;
}
}
static void hci_cmd_fiber(void)
{
struct bt_driver *drv = dev.drv;
BT_DBG("\n");
while (1) {
struct bt_buf *buf;
/* Wait until ncmd > 0 */
nano_fiber_sem_take_wait(&dev.ncmd_sem);
/* Get next command - wait if necessary */
buf = nano_fifo_get_wait(&dev.cmd_queue);
dev.ncmd = 0;
BT_DBG("Sending command (buf %p) to driver\n", buf);
drv->send(buf);
dev.sent_cmd = buf;
}
}
static void hci_rx_fiber(void)
@ -136,6 +327,14 @@ static void hci_rx_fiber(void)
}
}
static int hci_init(void)
{
/* Send HCI_RESET */
bt_hci_cmd_send(BT_HCI_OP_RESET, NULL);
return 0;
}
/* Interface to HCI driver layer */
void bt_recv(struct bt_buf *buf)
@ -163,6 +362,19 @@ void bt_driver_unregister(struct bt_driver *drv)
/* fibers, fifos and semaphores initialization */
static void cmd_queue_init(void)
{
nano_fifo_init(&dev.cmd_queue);
nano_sem_init(&dev.ncmd_sem);
/* Give cmd_sem allowing to send first HCI_Reset cmd */
dev.ncmd = 1;
nano_task_sem_give(&dev.ncmd_sem);
fiber_start(cmd_fiber_stack, CMD_STACK_SIZE,
(nano_fiber_entry_t) hci_cmd_fiber, 0, 0, 7, 0);
}
static void rx_queue_init(void)
{
nano_fifo_init(&dev.rx_queue);
@ -188,11 +400,12 @@ int bt_init(void)
return -ENODEV;
free_queue_init();
cmd_queue_init();
rx_queue_init();
err = drv->open();
if (err)
return err;
return 0;
return hci_init();
}