@@ -36,6 +36,8 @@ extern "C" {
3636
3737/** Possible types of buffers passed around the Bluetooth stack in a form of bitmask. */
3838enum bt_buf_type {
39+ /** Invalid value, used for error checking */
40+ BT_BUF_TYPE_INVALID = 0 ,
3941 /** HCI command */
4042 BT_BUF_CMD = BIT (0 ),
4143 /** HCI event */
@@ -48,15 +50,60 @@ enum bt_buf_type {
4850 BT_BUF_ISO_OUT = BIT (4 ),
4951 /** Incoming ISO data */
5052 BT_BUF_ISO_IN = BIT (5 ),
51- /** H:4 data */
52- BT_BUF_H4 = BIT (6 ),
5353};
5454
55- /** @brief This is a base type for bt_buf user data. */
56- struct bt_buf_data {
57- uint8_t type ;
55+ /** Direction of HCI packets. Only used for mapping H:4 to BT_BUF_* values. */
56+ enum bt_buf_dir {
57+ BT_BUF_IN ,
58+ BT_BUF_OUT ,
5859};
5960
61+ /** Convert from bt_buf_type to H:4 type.
62+ *
63+ * @param type The bt_buf_type to convert
64+ * @return The H:4 type
65+ */
66+ static inline uint8_t bt_buf_type_to_h4 (enum bt_buf_type type )
67+ {
68+ switch (type ) {
69+ case BT_BUF_CMD :
70+ return BT_HCI_H4_CMD ;
71+ case BT_BUF_ACL_IN :
72+ case BT_BUF_ACL_OUT :
73+ return BT_HCI_H4_ACL ;
74+ case BT_BUF_ISO_IN :
75+ case BT_BUF_ISO_OUT :
76+ return BT_HCI_H4_ISO ;
77+ case BT_BUF_EVT :
78+ return BT_HCI_H4_EVT ;
79+ default :
80+ __ASSERT_NO_MSG (false);
81+ return 0 ;
82+ }
83+ }
84+
85+ /** Convert from H:4 type to bt_buf_type.
86+ *
87+ * @param h4_type The H:4 type to convert
88+ * @param dir The direction of the packet
89+ * @return The bt_buf_type
90+ */
91+ static inline enum bt_buf_type bt_buf_type_from_h4 (uint8_t h4_type , enum bt_buf_dir dir )
92+ {
93+ switch (h4_type ) {
94+ case BT_HCI_H4_CMD :
95+ return BT_BUF_CMD ;
96+ case BT_HCI_H4_ACL :
97+ return dir == BT_BUF_OUT ? BT_BUF_ACL_OUT : BT_BUF_ACL_IN ;
98+ case BT_HCI_H4_EVT :
99+ return BT_BUF_EVT ;
100+ case BT_HCI_H4_ISO :
101+ return dir == BT_BUF_OUT ? BT_BUF_ISO_OUT : BT_BUF_ISO_IN ;
102+ default :
103+ return BT_BUF_TYPE_INVALID ;
104+ }
105+ }
106+
60107/* Headroom reserved in buffers, primarily for HCI transport encoding purposes */
61108#define BT_BUF_RESERVE 1
62109
@@ -138,8 +185,7 @@ BUILD_ASSERT(CONFIG_BT_BUF_EVT_RX_COUNT > CONFIG_BT_BUF_ACL_TX_COUNT,
138185
139186/** Allocate a buffer for incoming data
140187 *
141- * This will set the buffer type so bt_buf_set_type() does not need to
142- * be explicitly called.
188+ * This will set the buffer type so it doesn't need to be explicitly encoded into the buffer.
143189 *
144190 * @param type Type of buffer. Only BT_BUF_EVT, BT_BUF_ACL_IN and BT_BUF_ISO_IN
145191 * are allowed.
@@ -172,14 +218,12 @@ void bt_buf_rx_freed_cb_set(bt_buf_rx_freed_cb_t cb);
172218
173219/** Allocate a buffer for outgoing data
174220 *
175- * This will set the buffer type so bt_buf_set_type() does not need to
176- * be explicitly called.
221+ * This will set the buffer type so it doesn't need to be explicitly encoded into the buffer.
177222 *
178- * @param type Type of buffer. Only BT_BUF_CMD, BT_BUF_ACL_OUT or
179- * BT_BUF_H4, when operating on H:4 mode, are allowed.
223+ * @param type Type of buffer. BT_BUF_CMD or BT_BUF_ACL_OUT.
180224 * @param timeout Non-negative waiting period to obtain a buffer or one of the
181225 * special values K_NO_WAIT and K_FOREVER.
182- * @param data Initial data to append to buffer.
226+ * @param data Initial data to append to buffer. This is optional and can be NULL.
183227 * @param size Initial data size.
184228 * @return A new buffer.
185229 */
@@ -188,8 +232,7 @@ struct net_buf *bt_buf_get_tx(enum bt_buf_type type, k_timeout_t timeout,
188232
189233/** Allocate a buffer for an HCI Event
190234 *
191- * This will set the buffer type so bt_buf_set_type() does not need to
192- * be explicitly called.
235+ * This will set the buffer type so it doesn't need to be explicitly encoded into the buffer.
193236 *
194237 * @param evt HCI event code
195238 * @param discardable Whether the driver considers the event discardable.
@@ -199,26 +242,33 @@ struct net_buf *bt_buf_get_tx(enum bt_buf_type type, k_timeout_t timeout,
199242 */
200243struct net_buf * bt_buf_get_evt (uint8_t evt , bool discardable , k_timeout_t timeout );
201244
202- /** Set the buffer type
245+ /** Set the buffer type. The type is encoded as an H:4 byte prefix as part of
246+ * the payload itself.
203247 *
204248 * @param buf Bluetooth buffer
205249 * @param type The BT_* type to set the buffer to
206250 */
207- static inline void bt_buf_set_type (struct net_buf * buf , enum bt_buf_type type )
251+ static inline void __deprecated bt_buf_set_type (struct net_buf * buf , enum bt_buf_type type )
208252{
209- ((struct bt_buf_data * )net_buf_user_data (buf ))-> type = type ;
253+ __ASSERT_NO_MSG (net_buf_headroom (buf ) >= 1 );
254+ net_buf_push_u8 (buf , bt_buf_type_to_h4 (type ));
210255}
211256
212- /** Get the buffer type
257+
258+ /** Get the buffer type. This pulls the H:4 byte prefix from the payload, which means
259+ * that the call can be done only once per buffer.
213260 *
214261 * @param buf Bluetooth buffer
215262 *
216263 * @return The BT_* type to of the buffer
217264 */
218- static inline enum bt_buf_type bt_buf_get_type (struct net_buf * buf )
265+ static inline enum bt_buf_type __deprecated bt_buf_get_type (struct net_buf * buf )
219266{
220- return (enum bt_buf_type )((struct bt_buf_data * )net_buf_user_data (buf ))
221- -> type ;
267+ /* We have to assume the direction since the H:4 type doesn't tell us
268+ * if the buffer is incoming or outgoing. The common use case of this API is for outgoing
269+ * buffers, so we assume that.
270+ */
271+ return bt_buf_type_from_h4 (net_buf_pull_u8 (buf ), BT_BUF_OUT );
222272}
223273
224274/**
0 commit comments