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 */