Bluetooth: Move buffer handling into a dedicated file

The buffer handling code is quite large and will continue to grow. As
it's not strictly HCI core material it's better to just manage it in a
separate file. This patch moves the code to include/bluetooth/buf.h
and net/bluetooth/buf.c.

Change-Id: Ie1ff79ac2cfa132359ce9f7674ff812d34b228aa
Signed-off-by: Johan Hedberg <johan.hedberg@intel.com>
This commit is contained in:
Johan Hedberg 2015-04-28 10:59:35 +03:00 committed by Anas Nashif
commit 429240d376
4 changed files with 214 additions and 136 deletions

View file

@ -33,6 +33,7 @@
#define __BT_BLUETOOTH_H
#include <misc/printk.h>
#include <bluetooth/buf.h>
/* Bluetooth subsystem logging helpers */
@ -40,67 +41,6 @@
#define BT_ERR(fmt, ...) printk("bt: %s: " fmt, __func__, ##__VA_ARGS__)
#define BT_INFO(fmt, ...) printk("bt: " fmt, ##__VA_ARGS__)
/* Data buffer API - used for all data to/from HCI */
/* The biggest foreseeable buffer size requirement right now comes from
* the Bluetooth 4.2 SMP MTU which is 65. This then become 65 + 4 (L2CAP
* header) + 4 (ACL header) + 1 (H4 header) = 74. This also covers the
* biggest HCI commands and events which are a bit under the 70 byte
* mark.
*/
#define BT_BUF_MAX_DATA 74
/* Type of data contained in this buffer */
enum bt_buf_type {
BT_CMD,
BT_EVT,
BT_ACL,
};
struct bt_buf {
/* FIFO uses first 4 bytes itself, reserve space */
int __unused;
/* HCI command specific info */
struct nano_sem *sync;
uint16_t opcode;
/* Type of data contained in the buffer */
uint8_t type;
/* Buffer data variables */
uint8_t len;
uint8_t *data;
uint8_t buf[BT_BUF_MAX_DATA];
};
/* Get buffer from the available buffers pool */
struct bt_buf *bt_buf_get(void);
/* Same as bt_buf_get, but also reserve headroom for potential headers */
struct bt_buf *bt_buf_get_reserve(size_t reserve_head);
/* Place buffer back into the available buffers pool */
void bt_buf_put(struct bt_buf *buf);
/* Prepare data to be added at the end of the buffer */
uint8_t *bt_buf_add(struct bt_buf *buf, size_t len);
/* Push data to the beginning of the buffer */
uint8_t *bt_buf_push(struct bt_buf *buf, size_t len);
/* Remove data from the beginning of the buffer */
uint8_t *bt_buf_pull(struct bt_buf *buf, size_t len);
/* Returns how much free space there is at the end of the buffer */
size_t bt_buf_tailroom(struct bt_buf *buf);
/* Returns how much free space there is at the beginning of the buffer */
size_t bt_buf_headroom(struct bt_buf *buf);
/* Return pointer to the end of the data in the buffer */
#define bt_buf_tail(buf) ((buf)->data + (buf)->len)
/* HCI control APIs */
/* Reset the state of the controller (i.e. perform full HCI init */

97
include/bluetooth/buf.h Normal file
View file

@ -0,0 +1,97 @@
/* buf.h - Bluetooth buffer management */
/*
* Copyright (c) 2015 Intel Corporation
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1) Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2) Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* 3) Neither the name of Intel Corporation nor the names of its contributors
* may be used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef __BT_BUF_H
#define __BT_BUF_H
/* The biggest foreseeable buffer size requirement right now comes from
* the Bluetooth 4.2 SMP MTU which is 65. This then become 65 + 4 (L2CAP
* header) + 4 (ACL header) + 1 (H4 header) = 74. This also covers the
* biggest HCI commands and events which are a bit under the 70 byte
* mark.
*/
#define BT_BUF_MAX_DATA 74
/* Type of data contained in this buffer */
enum bt_buf_type {
BT_CMD,
BT_EVT,
BT_ACL,
};
struct bt_buf {
/* FIFO uses first 4 bytes itself, reserve space */
int __unused;
/* HCI command specific info */
struct nano_sem *sync;
uint16_t opcode;
/* Type of data contained in the buffer */
uint8_t type;
/* Buffer data variables */
uint8_t len;
uint8_t *data;
uint8_t buf[BT_BUF_MAX_DATA];
};
/* Get buffer from the available buffers pool */
struct bt_buf *bt_buf_get(void);
/* Same as bt_buf_get, but also reserve headroom for potential headers */
struct bt_buf *bt_buf_get_reserve(size_t reserve_head);
/* Place buffer back into the available buffers pool */
void bt_buf_put(struct bt_buf *buf);
/* Prepare data to be added at the end of the buffer */
uint8_t *bt_buf_add(struct bt_buf *buf, size_t len);
/* Push data to the beginning of the buffer */
uint8_t *bt_buf_push(struct bt_buf *buf, size_t len);
/* Remove data from the beginning of the buffer */
uint8_t *bt_buf_pull(struct bt_buf *buf, size_t len);
/* Returns how much free space there is at the end of the buffer */
size_t bt_buf_tailroom(struct bt_buf *buf);
/* Returns how much free space there is at the beginning of the buffer */
size_t bt_buf_headroom(struct bt_buf *buf);
/* Return pointer to the end of the data in the buffer */
#define bt_buf_tail(buf) ((buf)->data + (buf)->len)
/* Initialize the buffers */
void bt_buf_init(void);
#endif /* __BT_BUF_H */

115
net/bluetooth/buf.c Normal file
View file

@ -0,0 +1,115 @@
/* buf.c - Bluetooth buffer management */
/*
* Copyright (c) 2015 Intel Corporation
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1) Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2) Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* 3) Neither the name of Intel Corporation nor the names of its contributors
* may be used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include <nanokernel.h>
#include <toolchain.h>
#include <stddef.h>
#include <bluetooth/hci.h>
#include <bluetooth/bluetooth.h>
#include <bluetooth/buf.h>
#include "hci_core.h"
/* Available (free) buffers queue */
#define NUM_BUFS 5
static struct bt_buf buffers[NUM_BUFS];
static struct nano_fifo free_bufs;
struct bt_buf *bt_buf_get_reserve(size_t reserve_head)
{
struct bt_buf *buf;
buf = nano_fifo_get(&free_bufs);
if (!buf) {
BT_ERR("Failed to get free buffer\n");
return NULL;
}
buf->data = buf->buf + reserve_head;
buf->len = 0;
buf->sync = NULL;
BT_DBG("buf %p reserve %u\n", buf, reserve_head);
return buf;
}
struct bt_buf *bt_buf_get(void)
{
return bt_buf_get_reserve(0);
}
void bt_buf_put(struct bt_buf *buf)
{
BT_DBG("buf %p\n", buf);
nano_fifo_put(&free_bufs, buf);
}
uint8_t *bt_buf_add(struct bt_buf *buf, size_t len)
{
uint8_t *tail = buf->data + buf->len;
buf->len += len;
return tail;
}
uint8_t *bt_buf_push(struct bt_buf *buf, size_t len)
{
buf->data -= len;
buf->len += len;
return buf->data;
}
uint8_t *bt_buf_pull(struct bt_buf *buf, size_t len)
{
buf->len -= len;
return buf->data += len;
}
size_t bt_buf_headroom(struct bt_buf *buf)
{
return buf->data - buf->buf;
}
size_t bt_buf_tailroom(struct bt_buf *buf)
{
return BT_BUF_MAX_DATA - bt_buf_headroom(buf) - buf->len;
}
void bt_buf_init(void)
{
nano_fifo_init(&free_bufs);
for (int i = 0; i < NUM_BUFS; i++)
nano_fifo_put(&free_bufs, &buffers[i]);
}

View file

@ -48,74 +48,8 @@
static char rx_fiber_stack[RX_STACK_SIZE];
static char cmd_fiber_stack[CMD_STACK_SIZE];
/* Available (free) buffers queue */
#define NUM_BUFS 5
static struct bt_buf buffers[NUM_BUFS];
static struct nano_fifo free_bufs;
static struct bt_dev dev;
struct bt_buf *bt_buf_get_reserve(size_t reserve_head)
{
struct bt_buf *buf;
buf = nano_fifo_get(&free_bufs);
if (!buf) {
BT_ERR("Failed to get free buffer\n");
return NULL;
}
buf->data = buf->buf + reserve_head;
buf->len = 0;
buf->sync = NULL;
BT_DBG("buf %p reserve %u\n", buf, reserve_head);
return buf;
}
struct bt_buf *bt_buf_get(void)
{
return bt_buf_get_reserve(0);
}
void bt_buf_put(struct bt_buf *buf)
{
BT_DBG("buf %p\n", buf);
nano_fifo_put(&free_bufs, buf);
}
uint8_t *bt_buf_add(struct bt_buf *buf, size_t len)
{
uint8_t *tail = buf->data + buf->len;
buf->len += len;
return tail;
}
uint8_t *bt_buf_push(struct bt_buf *buf, size_t len)
{
buf->data -= len;
buf->len += len;
return buf->data;
}
uint8_t *bt_buf_pull(struct bt_buf *buf, size_t len)
{
buf->len -= len;
return buf->data += len;
}
size_t bt_buf_headroom(struct bt_buf *buf)
{
return buf->data - buf->buf;
}
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;
@ -687,14 +621,6 @@ static void rx_queue_init(void)
(nano_fiber_entry_t) hci_rx_fiber, 0, 0, 7, 0);
}
static void free_queue_init(void)
{
nano_fifo_init(&free_bufs);
for (int i = 0; i < NUM_BUFS; i++)
nano_fifo_put(&free_bufs, &buffers[i]);
}
int bt_init(void)
{
struct bt_driver *drv = dev.drv;
@ -703,7 +629,7 @@ int bt_init(void)
if (!drv)
return -ENODEV;
free_queue_init();
bt_buf_init();
cmd_queue_init();
rx_queue_init();