Zephyr Project API 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
62
68static inline uint8_t bt_buf_type_to_h4(enum bt_buf_type type)
69{
70 switch (type) {
71 case BT_BUF_CMD:
72 return BT_HCI_H4_CMD;
73 case BT_BUF_ACL_IN:
74 case BT_BUF_ACL_OUT:
75 return BT_HCI_H4_ACL;
76 case BT_BUF_ISO_IN:
77 case BT_BUF_ISO_OUT:
78 return BT_HCI_H4_ISO;
79 case BT_BUF_EVT:
80 return BT_HCI_H4_EVT;
81 default:
82 __ASSERT_NO_MSG(false);
83 return 0;
84 }
85}
86
93static inline enum bt_buf_type bt_buf_type_from_h4(uint8_t h4_type, enum bt_buf_dir dir)
94{
95 switch (h4_type) {
96 case BT_HCI_H4_CMD:
97 return BT_BUF_CMD;
98 case BT_HCI_H4_ACL:
99 return dir == BT_BUF_OUT ? BT_BUF_ACL_OUT : BT_BUF_ACL_IN;
100 case BT_HCI_H4_EVT:
101 return BT_BUF_EVT;
102 case BT_HCI_H4_ISO:
103 return dir == BT_BUF_OUT ? BT_BUF_ISO_OUT : BT_BUF_ISO_IN;
104 default:
105 return BT_BUF_TYPE_NONE;
106 }
107}
108
109/* Headroom reserved in buffers, primarily for HCI transport encoding purposes */
110#define BT_BUF_RESERVE 1
111
113#define BT_BUF_SIZE(size) (BT_BUF_RESERVE + (size))
114
116#define BT_BUF_ACL_SIZE(size) BT_BUF_SIZE(BT_HCI_ACL_HDR_SIZE + (size))
117
119#define BT_BUF_EVT_SIZE(size) BT_BUF_SIZE(BT_HCI_EVT_HDR_SIZE + (size))
120
122#define BT_BUF_CMD_SIZE(size) BT_BUF_SIZE(BT_HCI_CMD_HDR_SIZE + (size))
123
125#define BT_BUF_ISO_SIZE(size) BT_BUF_SIZE(BT_HCI_ISO_HDR_SIZE + \
126 BT_HCI_ISO_SDU_TS_HDR_SIZE + \
127 (size))
128
130#define BT_BUF_ACL_RX_SIZE BT_BUF_ACL_SIZE(CONFIG_BT_BUF_ACL_RX_SIZE)
131
133#define BT_BUF_EVT_RX_SIZE BT_BUF_EVT_SIZE(CONFIG_BT_BUF_EVT_RX_SIZE)
134
135#if defined(CONFIG_BT_ISO)
136#define BT_BUF_ISO_RX_SIZE BT_BUF_ISO_SIZE(CONFIG_BT_ISO_RX_MTU)
137#define BT_BUF_ISO_RX_COUNT CONFIG_BT_ISO_RX_BUF_COUNT
138#else
139#define BT_BUF_ISO_RX_SIZE 0
140#define BT_BUF_ISO_RX_COUNT 0
141#endif /* CONFIG_BT_ISO */
142
143/* see Core Spec v6.0 vol.4 part E 7.4.5 */
144#define BT_BUF_ACL_RX_COUNT_MAX 65535
145
146#if defined(CONFIG_BT_CONN) && defined(CONFIG_BT_HCI_HOST)
147 /* The host needs more ACL buffers than maximum ACL links. This is because of
148 * the way we re-assemble ACL packets into L2CAP PDUs.
149 *
150 * We keep around the first buffer (that comes from the driver) to do
151 * re-assembly into, and if all links are re-assembling, there will be no buffer
152 * available for the HCI driver to allocate from.
153 *
154 * TODO: When CONFIG_BT_BUF_ACL_RX_COUNT is removed,
155 * remove the MAX and only keep the 1.
156 */
157#define BT_BUF_ACL_RX_COUNT_EXTRA CONFIG_BT_BUF_ACL_RX_COUNT_EXTRA
158#define BT_BUF_ACL_RX_COUNT (MAX(CONFIG_BT_BUF_ACL_RX_COUNT, 1) + BT_BUF_ACL_RX_COUNT_EXTRA)
159#else
160#define BT_BUF_ACL_RX_COUNT_EXTRA 0
161#define BT_BUF_ACL_RX_COUNT 0
162#endif /* CONFIG_BT_CONN && CONFIG_BT_HCI_HOST */
163
164#if defined(CONFIG_BT_BUF_ACL_RX_COUNT) && CONFIG_BT_BUF_ACL_RX_COUNT > 0
165#warning "CONFIG_BT_BUF_ACL_RX_COUNT is deprecated, see Zephyr 4.1 migration guide"
166#endif /* CONFIG_BT_BUF_ACL_RX_COUNT && CONFIG_BT_BUF_ACL_RX_COUNT > 0 */
167
169 "Maximum number of ACL RX buffer is 65535, reduce CONFIG_BT_BUF_ACL_RX_COUNT_EXTRA");
170
172#define BT_BUF_RX_SIZE (MAX(MAX(BT_BUF_ACL_RX_SIZE, BT_BUF_EVT_RX_SIZE), \
173 BT_BUF_ISO_RX_SIZE))
174
175/* Controller can generate up to CONFIG_BT_BUF_ACL_TX_COUNT number of unique HCI Number of Completed
176 * Packets events.
177 */
178BUILD_ASSERT(CONFIG_BT_BUF_EVT_RX_COUNT > CONFIG_BT_BUF_ACL_TX_COUNT,
179 "Increase Event RX buffer count to be greater than ACL TX buffer count");
180
182#define BT_BUF_RX_COUNT (CONFIG_BT_BUF_EVT_RX_COUNT + \
183 MAX(BT_BUF_ACL_RX_COUNT, BT_BUF_ISO_RX_COUNT))
184
186#define BT_BUF_CMD_TX_SIZE BT_BUF_CMD_SIZE(CONFIG_BT_BUF_CMD_TX_SIZE)
187
198struct net_buf *bt_buf_get_rx(enum bt_buf_type type, k_timeout_t timeout);
199
212typedef void (*bt_buf_rx_freed_cb_t)(enum bt_buf_type type_mask);
213
220
232struct net_buf *bt_buf_get_tx(enum bt_buf_type type, k_timeout_t timeout,
233 const void *data, size_t size);
234
245struct net_buf *bt_buf_get_evt(uint8_t evt, bool discardable, k_timeout_t timeout);
246
253static inline void __deprecated bt_buf_set_type(struct net_buf *buf, enum bt_buf_type type)
254{
255 __ASSERT_NO_MSG(net_buf_headroom(buf) >= 1);
257}
258
259
267static inline enum bt_buf_type __deprecated bt_buf_get_type(struct net_buf *buf)
268{
269 /* We have to assume the direction since the H:4 type doesn't tell us
270 * if the buffer is incoming or outgoing. The common use case of this API is for outgoing
271 * buffers, so we assume that.
272 */
274}
275
280#ifdef __cplusplus
281}
282#endif
283
284#endif /* ZEPHYR_INCLUDE_BLUETOOTH_BUF_H_ */
static enum bt_buf_type bt_buf_type_from_h4(uint8_t h4_type, enum bt_buf_dir dir)
Convert from H:4 type to bt_buf_type.
Definition buf.h:93
#define BT_BUF_ACL_RX_COUNT
Definition buf.h:161
static enum bt_buf_type bt_buf_get_type(struct net_buf *buf)
Get the buffer type.
Definition buf.h:267
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:144
bt_buf_dir
Direction of HCI packets.
Definition buf.h:56
static uint8_t bt_buf_type_to_h4(enum bt_buf_type type)
Convert from bt_buf_type to H:4 type.
Definition buf.h:68
static void bt_buf_set_type(struct net_buf *buf, enum bt_buf_type type)
Set the buffer type.
Definition buf.h:253
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:212
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_OUT
Packet from the host to the controller.
Definition buf.h:60
@ BT_BUF_IN
Packet from the controller to the host.
Definition buf.h:58
@ BT_BUF_EVT
HCI event.
Definition buf.h:44
@ BT_BUF_TYPE_NONE
Unknown/invalid packet type, used for error checking.
Definition buf.h:40
@ BT_BUF_ISO_OUT
Outgoing ISO data.
Definition buf.h:50
@ BT_BUF_ACL_OUT
Outgoing ACL data.
Definition buf.h:46
@ BT_BUF_ISO_IN
Incoming ISO data.
Definition buf.h:52
@ BT_BUF_CMD
HCI command.
Definition buf.h:42
@ BT_BUF_ACL_IN
Incoming ACL data.
Definition buf.h:48
static uint8_t net_buf_pull_u8(struct net_buf *buf)
Remove a 8-bit value from the beginning of the buffer.
Definition net_buf.h:2258
static void net_buf_push_u8(struct net_buf *buf, uint8_t val)
Push 8-bit value to the beginning of the buffer.
Definition net_buf.h:2043
static size_t net_buf_headroom(const struct net_buf *buf)
Check buffer headroom.
Definition net_buf.h:2466
#define BIT(n)
Unsigned integer with bit position n set (signed in assembly language).
Definition util_macro.h:44
#define BT_HCI_H4_ACL
Definition hci_types.h:31
#define BT_HCI_H4_CMD
Definition hci_types.h:30
#define BT_HCI_H4_EVT
Definition hci_types.h:33
#define BT_HCI_H4_ISO
Definition hci_types.h:34
Buffer management.
__UINT8_TYPE__ uint8_t
Definition stdint.h:88
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.