Zephyr Project API 4.4.99
A Scalable Open Source RTOS
Loading...
Searching...
No Matches
buf.h
Go to the documentation of this file.
1
4
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
22
23#include <stddef.h>
24#include <stdint.h>
25
26#include <zephyr/autoconf.h>
29#include <zephyr/net_buf.h>
30#include <zephyr/sys/util.h>
32#include <zephyr/sys/clock.h>
33#include <zephyr/toolchain.h>
34
35#ifdef __cplusplus
36extern "C" {
37#endif
38
56
64
70static inline uint8_t bt_buf_type_to_h4(enum bt_buf_type type)
71{
72 switch (type) {
73 case BT_BUF_CMD:
74 return BT_HCI_H4_CMD;
75 case BT_BUF_ACL_IN:
76 case BT_BUF_ACL_OUT:
77 return BT_HCI_H4_ACL;
78 case BT_BUF_ISO_IN:
79 case BT_BUF_ISO_OUT:
80 return BT_HCI_H4_ISO;
81 case BT_BUF_EVT:
82 return BT_HCI_H4_EVT;
83 default:
84 __ASSERT_NO_MSG(false);
85 return 0;
86 }
87}
88
95static inline enum bt_buf_type bt_buf_type_from_h4(uint8_t h4_type, enum bt_buf_dir dir)
96{
97 switch (h4_type) {
98 case BT_HCI_H4_CMD:
99 return BT_BUF_CMD;
100 case BT_HCI_H4_ACL:
101 return dir == BT_BUF_OUT ? BT_BUF_ACL_OUT : BT_BUF_ACL_IN;
102 case BT_HCI_H4_EVT:
103 return BT_BUF_EVT;
104 case BT_HCI_H4_ISO:
105 return dir == BT_BUF_OUT ? BT_BUF_ISO_OUT : BT_BUF_ISO_IN;
106 default:
107 return BT_BUF_TYPE_NONE;
108 }
109}
110
111/* Headroom reserved in buffers, primarily for HCI transport encoding purposes */
112#define BT_BUF_RESERVE 1
113
115#define BT_BUF_SIZE(size) (BT_BUF_RESERVE + (size))
116
118#define BT_BUF_ACL_SIZE(size) BT_BUF_SIZE(BT_HCI_ACL_HDR_SIZE + (size))
119
121#define BT_BUF_EVT_SIZE(size) BT_BUF_SIZE(BT_HCI_EVT_HDR_SIZE + (size))
122
124#define BT_BUF_CMD_SIZE(size) BT_BUF_SIZE(BT_HCI_CMD_HDR_SIZE + (size))
125
127#define BT_BUF_ISO_SIZE(size) BT_BUF_SIZE(BT_HCI_ISO_HDR_SIZE + \
128 BT_HCI_ISO_SDU_TS_HDR_SIZE + \
129 (size))
130
132#define BT_BUF_ACL_RX_SIZE BT_BUF_ACL_SIZE(CONFIG_BT_BUF_ACL_RX_SIZE)
133
135#define BT_BUF_EVT_RX_SIZE BT_BUF_EVT_SIZE(CONFIG_BT_BUF_EVT_RX_SIZE)
136
137#if defined(CONFIG_BT_ISO)
138#define BT_BUF_ISO_RX_SIZE BT_BUF_ISO_SIZE(CONFIG_BT_ISO_RX_MTU)
139#define BT_BUF_ISO_RX_COUNT CONFIG_BT_ISO_RX_BUF_COUNT
140#else
141#define BT_BUF_ISO_RX_SIZE 0
142#define BT_BUF_ISO_RX_COUNT 0
143#endif /* CONFIG_BT_ISO */
144
145/* see Core Spec v6.0 vol.4 part E 7.4.5 */
146#define BT_BUF_ACL_RX_COUNT_MAX 65535
147
148#if defined(CONFIG_BT_CONN) && defined(CONFIG_BT_HCI_HOST)
149/* The host needs more ACL buffers than maximum ACL links. This is because of
150 * the way we re-assemble ACL packets into L2CAP PDUs.
151 *
152 * We keep around the first buffer (that comes from the driver) to do
153 * re-assembly into, and if all links are re-assembling, there will be no buffer
154 * available for the HCI driver to allocate from.
155 */
156#define BT_BUF_ACL_RX_COUNT_EXTRA CONFIG_BT_BUF_ACL_RX_COUNT_EXTRA
157#define BT_BUF_ACL_RX_COUNT (1 + BT_BUF_ACL_RX_COUNT_EXTRA)
158#else
159#define BT_BUF_ACL_RX_COUNT_EXTRA 0
160#define BT_BUF_ACL_RX_COUNT 0
161#endif /* CONFIG_BT_CONN && CONFIG_BT_HCI_HOST */
162
164 "Maximum number of ACL RX buffer is 65535, reduce CONFIG_BT_BUF_ACL_RX_COUNT_EXTRA");
165
167#define BT_BUF_RX_SIZE (MAX(MAX(BT_BUF_ACL_RX_SIZE, BT_BUF_EVT_RX_SIZE), \
168 BT_BUF_ISO_RX_SIZE))
169
170/* Controller can generate up to CONFIG_BT_BUF_ACL_TX_COUNT number of unique HCI Number of Completed
171 * Packets events.
172 */
173BUILD_ASSERT(CONFIG_BT_BUF_EVT_RX_COUNT > CONFIG_BT_BUF_ACL_TX_COUNT,
174 "Increase Event RX buffer count to be greater than ACL TX buffer count");
175
177#define BT_BUF_RX_COUNT (CONFIG_BT_BUF_EVT_RX_COUNT + \
178 MAX(BT_BUF_ACL_RX_COUNT, BT_BUF_ISO_RX_COUNT))
179
181#define BT_BUF_CMD_TX_SIZE BT_BUF_CMD_SIZE(CONFIG_BT_BUF_CMD_TX_SIZE)
182
193struct net_buf *bt_buf_get_rx(enum bt_buf_type type, k_timeout_t timeout);
194
210typedef void (*bt_buf_rx_freed_cb_t)(enum bt_buf_type type_mask);
211
222
234struct net_buf *bt_buf_get_tx(enum bt_buf_type type, k_timeout_t timeout,
235 const void *data, size_t size);
236
247struct net_buf *bt_buf_get_evt(uint8_t evt, bool discardable, k_timeout_t timeout);
248
252
253#ifdef __cplusplus
254}
255#endif
256
257#endif /* ZEPHYR_INCLUDE_BLUETOOTH_BUF_H_ */
System clock APIs.
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:95
#define BT_BUF_ACL_RX_COUNT
Definition buf.h:160
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:146
bt_buf_dir
Direction of HCI packets.
Definition buf.h:58
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:70
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:210
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:40
@ BT_BUF_OUT
Packet from the host to the controller.
Definition buf.h:62
@ BT_BUF_IN
Packet from the controller to the host.
Definition buf.h:60
@ BT_BUF_EVT
HCI event.
Definition buf.h:46
@ BT_BUF_TYPE_NONE
Unknown/invalid packet type, used for error checking.
Definition buf.h:42
@ BT_BUF_ISO_OUT
Outgoing ISO data.
Definition buf.h:52
@ BT_BUF_ACL_OUT
Outgoing ACL data.
Definition buf.h:48
@ BT_BUF_ISO_IN
Incoming ISO data.
Definition buf.h:54
@ BT_BUF_CMD
HCI command.
Definition buf.h:44
@ BT_BUF_ACL_IN
Incoming ACL data.
Definition buf.h:50
#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
#define BUILD_ASSERT(EXPR, MSG...)
Definition llvm.h:51
Buffer management.
__UINT8_TYPE__ uint8_t
Definition stdint.h:88
Kernel timeout type.
Definition clock.h:65
Network buffer representation.
Definition net_buf.h:1048
uint16_t size
Amount of data that this buffer can store.
Definition net_buf.h:1143
uint8_t * data
Pointer to the start of data in the buffer.
Definition net_buf.h:1137
Misc utilities.
Macros to abstract toolchain specific capabilities.
Macro utilities.