Zephyr API Documentation 4.1.99
A Scalable Open Source RTOS
Loading...
Searching...
No Matches
buf.h
Go to the documentation of this file.
1
5/*
6 * Copyright (c) 2016 Intel Corporation
7 *
8 * SPDX-License-Identifier: Apache-2.0
9 */
10
11#ifndef ZEPHYR_INCLUDE_BLUETOOTH_BUF_H_
12#define ZEPHYR_INCLUDE_BLUETOOTH_BUF_H_
13
21#include <stddef.h>
22#include <stdint.h>
23
24#include <zephyr/autoconf.h>
27#include <zephyr/net_buf.h>
28#include <zephyr/sys/util.h>
30#include <zephyr/sys_clock.h>
31#include <zephyr/toolchain.h>
32
33#ifdef __cplusplus
34extern "C" {
35#endif
36
54
59
60/* Headroom reserved in buffers, primarily for HCI transport encoding purposes */
61#define BT_BUF_RESERVE 1
62
64#define BT_BUF_SIZE(size) (BT_BUF_RESERVE + (size))
65
67#define BT_BUF_ACL_SIZE(size) BT_BUF_SIZE(BT_HCI_ACL_HDR_SIZE + (size))
68
70#define BT_BUF_EVT_SIZE(size) BT_BUF_SIZE(BT_HCI_EVT_HDR_SIZE + (size))
71
73#define BT_BUF_CMD_SIZE(size) BT_BUF_SIZE(BT_HCI_CMD_HDR_SIZE + (size))
74
76#define BT_BUF_ISO_SIZE(size) BT_BUF_SIZE(BT_HCI_ISO_HDR_SIZE + \
77 BT_HCI_ISO_SDU_TS_HDR_SIZE + \
78 (size))
79
81#define BT_BUF_ACL_RX_SIZE BT_BUF_ACL_SIZE(CONFIG_BT_BUF_ACL_RX_SIZE)
82
84#define BT_BUF_EVT_RX_SIZE BT_BUF_EVT_SIZE(CONFIG_BT_BUF_EVT_RX_SIZE)
85
86#if defined(CONFIG_BT_ISO)
87#define BT_BUF_ISO_RX_SIZE BT_BUF_ISO_SIZE(CONFIG_BT_ISO_RX_MTU)
88#define BT_BUF_ISO_RX_COUNT CONFIG_BT_ISO_RX_BUF_COUNT
89#else
90#define BT_BUF_ISO_RX_SIZE 0
91#define BT_BUF_ISO_RX_COUNT 0
92#endif /* CONFIG_BT_ISO */
93
94/* see Core Spec v6.0 vol.4 part E 7.4.5 */
95#define BT_BUF_ACL_RX_COUNT_MAX 65535
96
97#if defined(CONFIG_BT_CONN) && defined(CONFIG_BT_HCI_HOST)
98 /* The host needs more ACL buffers than maximum ACL links. This is because of
99 * the way we re-assemble ACL packets into L2CAP PDUs.
100 *
101 * We keep around the first buffer (that comes from the driver) to do
102 * re-assembly into, and if all links are re-assembling, there will be no buffer
103 * available for the HCI driver to allocate from.
104 *
105 * TODO: When CONFIG_BT_BUF_ACL_RX_COUNT is removed,
106 * remove the MAX and only keep the 1.
107 */
108#define BT_BUF_ACL_RX_COUNT_EXTRA CONFIG_BT_BUF_ACL_RX_COUNT_EXTRA
109#define BT_BUF_ACL_RX_COUNT (MAX(CONFIG_BT_BUF_ACL_RX_COUNT, 1) + BT_BUF_ACL_RX_COUNT_EXTRA)
110#else
111#define BT_BUF_ACL_RX_COUNT_EXTRA 0
112#define BT_BUF_ACL_RX_COUNT 0
113#endif /* CONFIG_BT_CONN && CONFIG_BT_HCI_HOST */
114
115#if defined(CONFIG_BT_BUF_ACL_RX_COUNT) && CONFIG_BT_BUF_ACL_RX_COUNT > 0
116#warning "CONFIG_BT_BUF_ACL_RX_COUNT is deprecated, see Zephyr 4.1 migration guide"
117#endif /* CONFIG_BT_BUF_ACL_RX_COUNT && CONFIG_BT_BUF_ACL_RX_COUNT > 0 */
118
120 "Maximum number of ACL RX buffer is 65535, reduce CONFIG_BT_BUF_ACL_RX_COUNT_EXTRA");
121
123#define BT_BUF_RX_SIZE (MAX(MAX(BT_BUF_ACL_RX_SIZE, BT_BUF_EVT_RX_SIZE), \
124 BT_BUF_ISO_RX_SIZE))
125
126/* Controller can generate up to CONFIG_BT_BUF_ACL_TX_COUNT number of unique HCI Number of Completed
127 * Packets events.
128 */
129BUILD_ASSERT(CONFIG_BT_BUF_EVT_RX_COUNT > CONFIG_BT_BUF_ACL_TX_COUNT,
130 "Increase Event RX buffer count to be greater than ACL TX buffer count");
131
133#define BT_BUF_RX_COUNT (CONFIG_BT_BUF_EVT_RX_COUNT + \
134 MAX(BT_BUF_ACL_RX_COUNT, BT_BUF_ISO_RX_COUNT))
135
137#define BT_BUF_CMD_TX_SIZE BT_BUF_CMD_SIZE(CONFIG_BT_BUF_CMD_TX_SIZE)
138
150struct net_buf *bt_buf_get_rx(enum bt_buf_type type, k_timeout_t timeout);
151
164typedef void (*bt_buf_rx_freed_cb_t)(enum bt_buf_type type_mask);
165
172
186struct net_buf *bt_buf_get_tx(enum bt_buf_type type, k_timeout_t timeout,
187 const void *data, size_t size);
188
200struct net_buf *bt_buf_get_evt(uint8_t evt, bool discardable, k_timeout_t timeout);
201
207static inline void bt_buf_set_type(struct net_buf *buf, enum bt_buf_type type)
208{
209 ((struct bt_buf_data *)net_buf_user_data(buf))->type = type;
210}
211
218static inline enum bt_buf_type bt_buf_get_type(struct net_buf *buf)
219{
220 return (enum bt_buf_type)((struct bt_buf_data *)net_buf_user_data(buf))
221 ->type;
222}
223
228#ifdef __cplusplus
229}
230#endif
231
232#endif /* ZEPHYR_INCLUDE_BLUETOOTH_BUF_H_ */
#define BT_BUF_ACL_RX_COUNT
Definition buf.h:112
static enum bt_buf_type bt_buf_get_type(struct net_buf *buf)
Get the buffer type.
Definition buf.h:218
struct net_buf * bt_buf_get_rx(enum bt_buf_type type, k_timeout_t timeout)
Allocate a buffer for incoming data.
struct net_buf * bt_buf_get_tx(enum bt_buf_type type, k_timeout_t timeout, const void *data, size_t size)
Allocate a buffer for outgoing data.
struct net_buf * bt_buf_get_evt(uint8_t evt, bool discardable, k_timeout_t timeout)
Allocate a buffer for an HCI Event.
#define BT_BUF_ACL_RX_COUNT_MAX
Definition buf.h:95
static void bt_buf_set_type(struct net_buf *buf, enum bt_buf_type type)
Set the buffer type.
Definition buf.h:207
void(* bt_buf_rx_freed_cb_t)(enum bt_buf_type type_mask)
A callback to notify about freed buffer in the incoming data pool.
Definition buf.h:164
void bt_buf_rx_freed_cb_set(bt_buf_rx_freed_cb_t cb)
Set the callback to notify about freed buffer in the incoming data pool.
bt_buf_type
Possible types of buffers passed around the Bluetooth stack in a form of bitmask.
Definition buf.h:38
@ BT_BUF_EVT
HCI event.
Definition buf.h:42
@ BT_BUF_ISO_OUT
Outgoing ISO data.
Definition buf.h:48
@ BT_BUF_ACL_OUT
Outgoing ACL data.
Definition buf.h:44
@ BT_BUF_ISO_IN
Incoming ISO data.
Definition buf.h:50
@ BT_BUF_CMD
HCI command.
Definition buf.h:40
@ BT_BUF_H4
H:4 data.
Definition buf.h:52
@ BT_BUF_ACL_IN
Incoming ACL data.
Definition buf.h:46
static void * net_buf_user_data(const struct net_buf *buf)
Get a pointer to the user data of a buffer.
Definition net_buf.h:1532
#define BIT(n)
Unsigned integer with bit position n set (signed in assembly language).
Definition util_macro.h:44
Buffer management.
__UINT8_TYPE__ uint8_t
Definition stdint.h:88
This is a base type for bt_buf user data.
Definition buf.h:56
uint8_t type
Definition buf.h:57
Kernel timeout type.
Definition sys_clock.h:65
Network buffer representation.
Definition net_buf.h:1006
uint16_t size
Amount of data that this buffer can store.
Definition net_buf.h:1038
uint8_t * data
Pointer to the start of data in the buffer.
Definition net_buf.h:1032
Misc utilities.
Variables needed for system clock.
Macros to abstract toolchain specific capabilities.
Macro utilities.