summaryrefslogtreecommitdiff
path: root/samples/bluetooth
diff options
context:
space:
mode:
authorJohan Hedberg <johan.hedberg@intel.com>2016-10-18 23:24:51 +0300
committerAnas Nashif <nashif@linux.intel.com>2016-12-13 21:41:53 +0000
commitc3e08c8feaf928706675cd3c52bae67667ed9d13 (patch)
treebdffe5ee2de072a35e617c92fc9e822861715b1b /samples/bluetooth
parent1e2e05c784cb98509e8d95c5f976ad89a9a2cff4 (diff)
net: buf: Redesigned pool & buffer allocation API
Until now it has been necessary to separately define a k_fifo and an array of buffers when creating net_buf pools. This has been a bit of an inconvenience as well as blurred the line of what exactly constitutes the "pool". This patch removes the NET_BUF_POOL() macro and replaces it with a NET_BUF_POOL_DEFINE() macro that internally expands into the buffer array and new net_buf_pool struct with a given name: NET_BUF_POOL_DEFINE(pool_name, ...); Having a dedicated context struct for the pool has the added benefit that we can start moving there net_buf members that have the same value for all buffers from the same pool. The first such member that gets moved is the destroy callback, thus shrinking net_buf by four bytes. Another potential candidate is the user_data_size, however right not that's left out since it would just leave 2 bytes of padding in net_buf (i.e. not influence its size). Another common value is buf->size, however that one is also used by net_buf_simple and can therefore not be moved. This patch also splits getting buffers from a FIFO and allocating a new buffer from a pool into two separate APIs: net_buf_get and net_buf_alloc, thus simplifying the APIs and their usage. There is no separate 'reserve_head' parameter anymore when allocating, rather the user is expected to call net_buf_reserve() afterwards if something else than 0 headroom is desired. Change-Id: Id91b1e5c2be2deb1274dde47f5edebfe29af383a Signed-off-by: Johan Hedberg <johan.hedberg@intel.com>
Diffstat (limited to 'samples/bluetooth')
-rw-r--r--samples/bluetooth/hci_uart/src/main.c23
-rw-r--r--samples/bluetooth/hci_usb/src/main.c19
2 files changed, 18 insertions, 24 deletions
diff --git a/samples/bluetooth/hci_uart/src/main.c b/samples/bluetooth/hci_uart/src/main.c
index bf078e5f9..a9401719a 100644
--- a/samples/bluetooth/hci_uart/src/main.c
+++ b/samples/bluetooth/hci_uart/src/main.c
@@ -45,9 +45,8 @@ static BT_STACK_NOINIT(tx_thread_stack, CONFIG_BLUETOOTH_HCI_SEND_STACK);
sizeof(struct bt_hci_cmd_hdr) + \
CONFIG_BLUETOOTH_MAX_CMD_LEN)
-static struct k_fifo avail_cmd_tx;
-static NET_BUF_POOL(cmd_tx_pool, CONFIG_BLUETOOTH_HCI_CMD_COUNT, CMD_BUF_SIZE,
- &avail_cmd_tx, NULL, BT_BUF_USER_DATA_MIN);
+NET_BUF_POOL_DEFINE(cmd_tx_pool, CONFIG_BLUETOOTH_HCI_CMD_COUNT, CMD_BUF_SIZE,
+ BT_BUF_USER_DATA_MIN, NULL);
#define BT_L2CAP_MTU 64
/** Data size needed for ACL buffers */
@@ -62,10 +61,8 @@ static NET_BUF_POOL(cmd_tx_pool, CONFIG_BLUETOOTH_HCI_CMD_COUNT, CMD_BUF_SIZE,
#define TX_BUF_COUNT 6
#endif
-static struct k_fifo avail_acl_tx;
-
-static NET_BUF_POOL(acl_tx_pool, TX_BUF_COUNT, BT_BUF_ACL_SIZE,
- &avail_acl_tx, NULL, BT_BUF_USER_DATA_MIN);
+NET_BUF_POOL_DEFINE(acl_tx_pool, TX_BUF_COUNT, BT_BUF_ACL_SIZE,
+ BT_BUF_USER_DATA_MIN, NULL);
static struct k_fifo tx_queue;
@@ -126,7 +123,7 @@ static struct net_buf *h4_cmd_recv(int *remaining)
*remaining = hdr.param_len;
- buf = net_buf_get(&avail_cmd_tx, 0);
+ buf = net_buf_alloc(&cmd_tx_pool, K_NO_WAIT);
if (buf) {
bt_buf_set_type(buf, BT_BUF_CMD);
memcpy(net_buf_add(buf, sizeof(hdr)), &hdr, sizeof(hdr));
@@ -147,7 +144,7 @@ static struct net_buf *h4_acl_recv(int *remaining)
/* We can ignore the return value since we pass len == min */
h4_read(hci_uart_dev, (void *)&hdr, sizeof(hdr), sizeof(hdr));
- buf = net_buf_get(&avail_acl_tx, 0);
+ buf = net_buf_alloc(&acl_tx_pool, K_NO_WAIT);
if (buf) {
bt_buf_set_type(buf, BT_BUF_ACL_OUT);
memcpy(net_buf_add(buf, sizeof(hdr)), &hdr, sizeof(hdr));
@@ -245,7 +242,7 @@ static void tx_thread(void *p1, void *p2, void *p3)
struct net_buf *buf;
/* Wait until a buffer is available */
- buf = net_buf_get_timeout(&tx_queue, 0, K_FOREVER);
+ buf = net_buf_get(&tx_queue, K_FOREVER);
/* Pass buffer to the stack */
bt_send(buf);
@@ -362,8 +359,8 @@ void main(void)
SYS_LOG_DBG("Start");
/* Initialize the buffer pools */
- net_buf_pool_init(cmd_tx_pool);
- net_buf_pool_init(acl_tx_pool);
+ net_buf_pool_init(&cmd_tx_pool);
+ net_buf_pool_init(&acl_tx_pool);
/* Initialize the FIFOs */
k_fifo_init(&tx_queue);
k_fifo_init(&rx_queue);
@@ -379,7 +376,7 @@ void main(void)
while (1) {
struct net_buf *buf;
- buf = net_buf_get_timeout(&rx_queue, 0, K_FOREVER);
+ buf = net_buf_get(&rx_queue, K_FOREVER);
err = h4_send(buf);
if (err) {
SYS_LOG_ERR("Failed to send");
diff --git a/samples/bluetooth/hci_usb/src/main.c b/samples/bluetooth/hci_usb/src/main.c
index 675f0923f..b6aca2f21 100644
--- a/samples/bluetooth/hci_usb/src/main.c
+++ b/samples/bluetooth/hci_usb/src/main.c
@@ -88,9 +88,8 @@ static struct k_fifo rx_queue;
sizeof(struct bt_hci_cmd_hdr) + \
CONFIG_BLUETOOTH_MAX_CMD_LEN)
-static struct k_fifo avail_tx;
-static NET_BUF_POOL(tx_pool, CONFIG_BLUETOOTH_HCI_CMD_COUNT, CMD_BUF_SIZE,
- &avail_tx, NULL, sizeof(uint8_t));
+NET_BUF_POOL_DEFINE(tx_pool, CONFIG_BLUETOOTH_HCI_CMD_COUNT, CMD_BUF_SIZE,
+ sizeof(uint8_t), NULL);
#define BT_L2CAP_MTU 64
/** Data size needed for ACL buffers */
@@ -99,9 +98,7 @@ static NET_BUF_POOL(tx_pool, CONFIG_BLUETOOTH_HCI_CMD_COUNT, CMD_BUF_SIZE,
4 /* L2CAP header size */ + \
BT_L2CAP_MTU)
-static struct k_fifo avail_acl_tx;
-static NET_BUF_POOL(acl_tx_pool, 2, BT_BUF_ACL_SIZE, &avail_acl_tx, NULL,
- sizeof(uint8_t));
+NET_BUF_POOL_DEFINE(acl_tx_pool, 2, BT_BUF_ACL_SIZE, sizeof(uint8_t), NULL);
/* Device data structure */
struct btusb_dev_data_t {
@@ -447,7 +444,7 @@ static void btusb_bulk_out(uint8_t ep, enum usb_dc_ep_cb_status_code ep_status)
return;
}
- buf = net_buf_get(&avail_acl_tx, 0);
+ buf = net_buf_alloc(&acl_tx_pool, K_NO_WAIT);
if (!buf) {
SYS_LOG_ERR("Cannot get free buffer\n");
return;
@@ -616,7 +613,7 @@ static int btusb_class_handler(struct usb_setup_packet *setup,
hexdump(">", *data, *len);
- buf = net_buf_get(&avail_tx, 0);
+ buf = net_buf_alloc(&tx_pool, K_NO_WAIT);
if (!buf) {
SYS_LOG_ERR("Cannot get free buffer\n");
return -ENOMEM;
@@ -695,8 +692,8 @@ void main(void)
SYS_LOG_DBG("Start");
/* Initialize the buffer pools */
- net_buf_pool_init(tx_pool);
- net_buf_pool_init(acl_tx_pool);
+ net_buf_pool_init(&tx_pool);
+ net_buf_pool_init(&acl_tx_pool);
k_fifo_init(&rx_queue);
bt_enable_raw(&rx_queue);
@@ -704,7 +701,7 @@ void main(void)
while (1) {
struct net_buf *buf;
- buf = net_buf_get_timeout(&rx_queue, 0, K_FOREVER);
+ buf = net_buf_get(&rx_queue, K_FOREVER);
hexdump("<", buf->data, buf->len);