Bluetooth: Add support for HCI data buffers

In order to manage incoming and outgoing HCI data (events, commands &
ACL data packets) we need to have some way of storing this into
buffers. This patch implements a 'pool' of buffers with the help of
the nano_fifo API. The pool is initially populated with all available
buffers. After this code can on demand request buffers from the pool
with the help of bt_buf_get() and return buffers back into the pool
with bt_buf_put().

Since we don't always know the execution context from where the API is
operated on, this patch also adds convenience fifo_get/fifo_put
wrappers that look up the exact context before calling the correct
nano_fifo API.

Change-Id: Ie7f6d450de865273171e21a000d5a14274d27d32
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:04:46 +03:00 committed by Anas Nashif
commit 64b117a9b1
2 changed files with 151 additions and 0 deletions

View file

@ -32,6 +32,75 @@
#ifndef __BT_BLUETOOTH_H
#define __BT_BLUETOOTH_H
#include <misc/printk.h>
/* Bluetooth subsystem logging helpers */
#define BT_DBG(fmt, ...) printk("bt: %s: " fmt, __func__, ##__VA_ARGS__)
#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)
/* Initialize Bluetooth. Must be the called before anything else. */
int bt_init(void);