Skip to content

Commit 7055e52

Browse files
committed
Bluetooth: Deprecate bt_buf_get/set_type()
These are fairly pointless now and needlessly obscure the actual encoding or decoding of the packet type. Signed-off-by: Johan Hedberg <[email protected]>
1 parent 008b8bc commit 7055e52

File tree

27 files changed

+56
-83
lines changed

27 files changed

+56
-83
lines changed

doc/releases/migration-guide-4.2.rst

+2-1
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,8 @@ Bluetooth HCI
238238

239239
* The buffer types passing through the HCI driver interface are now indicated as H:4 encoded prefix
240240
bytes as part of the buffer payload itself. The bt_buf_set_type() and bt_buf_get_type() functions
241-
are still usable, except that the latter can only be called once per buffer.
241+
have been deprecated, but are still useable, with the exception that the latter can only be
242+
called once per buffer.
242243

243244
* As part of the HCI buffer encoding change, since packet type values are no longer independent
244245
bits, the callback used by :c:func:`bt_buf_rx_freed_cb_set` has been changed to take a bitmask

drivers/bluetooth/hci/hci_ifx_cyw208xx.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ static int cyw208xx_send(const struct device *dev, struct net_buf *buf)
265265

266266
k_sem_take(&hci_sem, K_FOREVER);
267267

268-
type = bt_buf_get_type(buf);
268+
type = net_buf_pull_u8(buf);
269269

270270
LOG_DBG("buf %p type %u len %u", buf, type, buf->len);
271271

drivers/bluetooth/hci/hci_ifx_psoc6_bless.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ static int psoc6_bless_send(const struct device *dev, struct net_buf *buf)
166166

167167
memset(&hci_tx_pkt, 0, sizeof(cy_stc_ble_hci_tx_packet_info_t));
168168

169-
hci_tx_pkt.packetType = bt_buf_get_type(buf);
169+
hci_tx_pkt.packetType = net_buf_pull_u8(buf);
170170
hci_tx_pkt.dataLength = buf->len;
171171
hci_tx_pkt.data = buf->data;
172172

drivers/bluetooth/hci/hci_stm32wb0.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,7 @@ static struct net_buf *get_rx(uint8_t *msg)
371371
static int bt_hci_stm32wb0_send(const struct device *dev, struct net_buf *buf)
372372
{
373373
int ret = 0;
374-
uint8_t type = bt_buf_get_type(buf);
374+
uint8_t type = net_buf_pull_u8(buf);
375375
uint8_t *hci_buffer = buf->data;
376376

377377
ARG_UNUSED(dev);

drivers/bluetooth/hci/ipm_stm32wb.c

+3-5
Original file line numberDiff line numberDiff line change
@@ -365,17 +365,15 @@ static int bt_ipm_send(const struct device *dev, struct net_buf *buf)
365365
case BT_BUF_ACL_OUT:
366366
LOG_DBG("ACL: buf %p type %u len %u", buf, buf->data[0], buf->len);
367367
k_sem_take(&acl_data_ack, K_FOREVER);
368-
memcpy((void *)
369-
&((TL_AclDataPacket_t *)HciAclDataBuffer)->AclDataSerial,
368+
memcpy((void *)&((TL_AclDataPacket_t *)HciAclDataBuffer)->AclDataSerial,
370369
buf->data, buf->len);
371370
TL_BLE_SendAclData(NULL, 0);
372371
break;
373372
case BT_BUF_CMD:
374373
LOG_DBG("CMD: buf %p type %u len %u", buf, buf->data[0], buf->len);
375-
ble_cmd_buff->cmdserial.type = bt_buf_get_type(buf);
374+
ble_cmd_buff->cmdserial.type = net_buf_pull_u8(buf);
376375
ble_cmd_buff->cmdserial.cmd.plen = buf->len;
377-
memcpy((void *)&ble_cmd_buff->cmdserial.cmd, buf->data,
378-
buf->len);
376+
memcpy((void *)&ble_cmd_buff->cmdserial.cmd, buf->data, buf->len);
379377
TL_BLE_SendCmd(NULL, 0);
380378
break;
381379
default:

include/zephyr/bluetooth/buf.h

+5-8
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,7 @@ BUILD_ASSERT(CONFIG_BT_BUF_EVT_RX_COUNT > CONFIG_BT_BUF_ACL_TX_COUNT,
131131

132132
/** Allocate a buffer for incoming data
133133
*
134-
* This will set the buffer type so bt_buf_set_type() does not need to
135-
* be explicitly called.
134+
* This will set the buffer type so it doesn't need to be explicitly encoded into the buffer.
136135
*
137136
* @param type Type of buffer. Only BT_BUF_EVT, BT_BUF_ACL_IN and BT_BUF_ISO_IN
138137
* are allowed.
@@ -165,8 +164,7 @@ void bt_buf_rx_freed_cb_set(bt_buf_rx_freed_cb_t cb);
165164

166165
/** Allocate a buffer for outgoing data
167166
*
168-
* This will set the buffer type so bt_buf_set_type() does not need to
169-
* be explicitly called.
167+
* This will set the buffer type so it doesn't need to be explicitly encoded into the buffer.
170168
*
171169
* @param type Type of buffer. BT_BUF_CMD or BT_BUF_ACL_OUT.
172170
* @param timeout Non-negative waiting period to obtain a buffer or one of the
@@ -180,8 +178,7 @@ struct net_buf *bt_buf_get_tx(enum bt_buf_type type, k_timeout_t timeout,
180178

181179
/** Allocate a buffer for an HCI Event
182180
*
183-
* This will set the buffer type so bt_buf_set_type() does not need to
184-
* be explicitly called.
181+
* This will set the buffer type so it doesn't need to be explicitly encoded into the buffer.
185182
*
186183
* @param evt HCI event code
187184
* @param discardable Whether the driver considers the event discardable.
@@ -197,7 +194,7 @@ struct net_buf *bt_buf_get_evt(uint8_t evt, bool discardable, k_timeout_t timeou
197194
* @param buf Bluetooth buffer
198195
* @param type The BT_* type to set the buffer to
199196
*/
200-
static inline void bt_buf_set_type(struct net_buf *buf, enum bt_buf_type type)
197+
static inline void __deprecated bt_buf_set_type(struct net_buf *buf, enum bt_buf_type type)
201198
{
202199
__ASSERT_NO_MSG(net_buf_headroom(buf) >= 1);
203200
net_buf_push_u8(buf, type);
@@ -210,7 +207,7 @@ static inline void bt_buf_set_type(struct net_buf *buf, enum bt_buf_type type)
210207
*
211208
* @return The BT_* type to of the buffer
212209
*/
213-
static inline enum bt_buf_type bt_buf_get_type(struct net_buf *buf)
210+
static inline enum bt_buf_type __deprecated bt_buf_get_type(struct net_buf *buf)
214211
{
215212
return net_buf_pull_u8(buf);
216213
}

include/zephyr/drivers/bluetooth.h

+3-5
Original file line numberDiff line numberDiff line change
@@ -114,9 +114,8 @@ __subsystem struct bt_hci_driver_api {
114114
*
115115
* @param dev HCI device
116116
* @param recv This is callback through which the HCI driver provides the
117-
* host with data from the controller. The buffer passed to
118-
* the callback will have its type set with bt_buf_set_type().
119-
* The callback is expected to be called from thread context.
117+
* host with data from the controller. The callback is expected
118+
* to be called from thread context.
120119
*
121120
* @return 0 on success or negative POSIX error number on failure.
122121
*/
@@ -151,8 +150,7 @@ static inline int bt_hci_close(const struct device *dev)
151150
/**
152151
* @brief Send HCI buffer to controller.
153152
*
154-
* Send an HCI packet to the controller. The packet type of the buffer
155-
* must be set using bt_buf_set_type(). The packet type is encoded as H:4,
153+
* Send an HCI packet to the controller. The packet type is encoded as H:4,
156154
* i.e. the UART transport encoding, as a prefix to the actual payload. This means
157155
* that HCI drivers that use H:4 as their native encoding don't need to do any
158156
* special handling of the packet type.

subsys/bluetooth/controller/hci/hci.c

+1-2
Original file line numberDiff line numberDiff line change
@@ -5237,8 +5237,7 @@ static struct net_buf *vs_err_evt_create(uint8_t subevt, uint8_t len)
52375237
struct bt_hci_evt_le_meta_event *me;
52385238
struct bt_hci_evt_hdr *hdr;
52395239

5240-
net_buf_reserve(buf, BT_BUF_RESERVE);
5241-
bt_buf_set_type(buf, BT_BUF_EVT);
5240+
net_buf_add_u8(buf, BT_BUF_EVT);
52425241

52435242
hdr = net_buf_add(buf, sizeof(*hdr));
52445243
hdr->evt = BT_HCI_EVT_VENDOR;

subsys/bluetooth/controller/hci/hci_driver.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ isoal_status_t sink_sdu_emit_hci(const struct isoal_sink *sink_ctx,
198198
hdr->handle = sys_cpu_to_le16(handle_packed);
199199
hdr->len = sys_cpu_to_le16(len);
200200

201-
bt_buf_set_type(buf, BT_BUF_ISO_IN);
201+
net_buf_push_u8(buf, BT_BUF_ISO_IN);
202202

203203
/* send fragment up the chain */
204204
data->recv(dev, buf);
@@ -966,7 +966,7 @@ static int hci_driver_send(const struct device *dev, struct net_buf *buf)
966966
return -EINVAL;
967967
}
968968

969-
type = bt_buf_get_type(buf);
969+
type = net_buf_pull_u8(buf);
970970
switch (type) {
971971
#if defined(CONFIG_BT_CONN)
972972
case BT_BUF_ACL_OUT:

subsys/bluetooth/host/buf.c

+2-5
Original file line numberDiff line numberDiff line change
@@ -128,10 +128,8 @@ struct net_buf *bt_buf_get_rx(enum bt_buf_type type, k_timeout_t timeout)
128128
#else
129129
buf = net_buf_alloc(&hci_rx_pool, timeout);
130130
#endif
131-
132131
if (buf) {
133-
net_buf_reserve(buf, BT_BUF_RESERVE);
134-
bt_buf_set_type(buf, type);
132+
net_buf_add_u8(buf, type);
135133
}
136134

137135
return buf;
@@ -172,8 +170,7 @@ struct net_buf *bt_buf_get_evt(uint8_t evt, bool discardable,
172170
}
173171

174172
if (buf) {
175-
net_buf_reserve(buf, BT_BUF_RESERVE);
176-
bt_buf_set_type(buf, BT_BUF_EVT);
173+
net_buf_add_u8(buf, BT_BUF_EVT);
177174
}
178175

179176
return buf;

subsys/bluetooth/host/conn.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -551,7 +551,7 @@ static int send_acl(struct bt_conn *conn, struct net_buf *buf, uint8_t flags)
551551
hdr->handle = sys_cpu_to_le16(bt_acl_handle_pack(conn->handle, flags));
552552
hdr->len = sys_cpu_to_le16(buf->len - sizeof(*hdr));
553553

554-
bt_buf_set_type(buf, BT_BUF_ACL_OUT);
554+
net_buf_push_u8(buf, BT_BUF_ACL_OUT);
555555

556556
return bt_send(buf);
557557
}
@@ -609,7 +609,7 @@ static int send_iso(struct bt_conn *conn, struct net_buf *buf, uint8_t flags)
609609
hdr->handle = sys_cpu_to_le16(bt_iso_handle_pack(conn->handle, flags, ts));
610610
hdr->len = sys_cpu_to_le16(buf->len - sizeof(*hdr));
611611

612-
bt_buf_set_type(buf, BT_BUF_ISO_OUT);
612+
net_buf_push_u8(buf, BT_BUF_ISO_OUT);
613613

614614
return bt_send(buf);
615615
}

subsys/bluetooth/host/hci_core.c

+2-3
Original file line numberDiff line numberDiff line change
@@ -331,8 +331,7 @@ struct net_buf *bt_hci_cmd_create(uint16_t opcode, uint8_t param_len)
331331

332332
LOG_DBG("buf %p", buf);
333333

334-
net_buf_reserve(buf, BT_BUF_RESERVE);
335-
bt_buf_set_type(buf, BT_BUF_CMD);
334+
net_buf_add_u8(buf, BT_BUF_CMD);
336335

337336
cmd(buf)->opcode = opcode;
338337
cmd(buf)->sync = NULL;
@@ -4239,7 +4238,7 @@ static void rx_work_handler(struct k_work *work)
42394238
return;
42404239
}
42414240

4242-
type = bt_buf_get_type(buf);
4241+
type = net_buf_pull_u8(buf);
42434242

42444243
LOG_DBG("buf %p type %u len %u", buf, type, buf->len);
42454244

subsys/bluetooth/host/hci_raw.c

+2-4
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,7 @@ struct net_buf *bt_buf_get_rx(enum bt_buf_type type, k_timeout_t timeout)
9696
return buf;
9797
}
9898

99-
net_buf_reserve(buf, BT_BUF_RESERVE);
100-
bt_buf_set_type(buf, type);
99+
net_buf_add_u8(buf, type);
101100

102101
return buf;
103102
}
@@ -135,8 +134,7 @@ struct net_buf *bt_buf_get_tx(enum bt_buf_type type, k_timeout_t timeout,
135134
return buf;
136135
}
137136

138-
net_buf_reserve(buf, BT_BUF_RESERVE);
139-
bt_buf_set_type(buf, type);
137+
net_buf_add_u8(buf, type);
140138

141139
if (data && size) {
142140
if (net_buf_tailroom(buf) < size) {

subsys/bluetooth/host/iso.c

+1-2
Original file line numberDiff line numberDiff line change
@@ -616,8 +616,7 @@ struct net_buf *bt_iso_get_rx(k_timeout_t timeout)
616616
struct net_buf *buf = net_buf_alloc(&iso_rx_pool, timeout);
617617

618618
if (buf) {
619-
net_buf_reserve(buf, BT_BUF_RESERVE);
620-
bt_buf_set_type(buf, BT_BUF_ISO_IN);
619+
net_buf_add_u8(buf, BT_BUF_ISO_IN);
621620
}
622621

623622
return buf;

subsys/usb/device/class/bluetooth.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ static void hci_tx_thread(void *p1, void *p2, void *p3)
152152
}
153153
}
154154

155-
type = bt_buf_get_type(buf);
155+
type = net_buf_pull_u8(buf);
156156
switch (type) {
157157
case BT_BUF_EVT:
158158
usb_transfer_sync(

subsys/usb/device_next/class/bt_hci.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ static void bt_hci_tx_thread(void *p1, void *p2, void *p3)
207207
uint8_t ep;
208208

209209
bt_buf = k_fifo_get(&bt_hci_tx_queue, K_FOREVER);
210-
type = bt_buf_get_type(bt_buf);
210+
type = net_buf_pull_u8(bt_buf);
211211

212212
switch (type) {
213213
case BT_BUF_EVT:
@@ -279,7 +279,7 @@ static uint16_t hci_pkt_get_len(struct net_buf *const buf,
279279
size_t hdr_len = 0;
280280
uint16_t len = 0;
281281

282-
switch (bt_buf_get_type(buf)) {
282+
switch (net_buf_pull_u8(buf)) {
283283
case BT_BUF_CMD: {
284284
struct bt_hci_cmd_hdr *cmd_hdr;
285285

tests/bluetooth/hci_prop_evt/src/main.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ static int driver_open(const struct device *dev, bt_hci_recv_t recv)
227227
/* HCI driver send. */
228228
static int driver_send(const struct device *dev, struct net_buf *buf)
229229
{
230-
uint8_t type = bt_buf_get_type(buf);
230+
uint8_t type = net_buf_pull_u8(buf);
231231

232232
zassert_true(type == BT_BUF_CMD, "Expected command buffer, got %u", type);
233233

tests/bluetooth/host_long_adv_recv/src/main.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ static int driver_open(const struct device *dev, bt_hci_recv_t recv)
242242
/* HCI driver send. */
243243
static int driver_send(const struct device *dev, struct net_buf *buf)
244244
{
245-
uint8_t type = bt_buf_get_type(buf);
245+
uint8_t type = net_buf_pull_u8(buf);
246246

247247
zassert_true(type == BT_BUF_CMD, "Unexpected command buffer, got %u", type);
248248

tests/bsim/bluetooth/host/att/pipeline/tester/src/main.c

+3-4
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,7 @@ struct net_buf *bt_hci_cmd_create(uint16_t opcode, uint8_t param_len)
7878

7979
LOG_DBG("buf %p", buf);
8080

81-
net_buf_reserve(buf, BT_BUF_RESERVE);
82-
bt_buf_set_type(buf, BT_BUF_CMD);
81+
net_buf_add_u8(buf, BT_BUF_CMD);
8382

8483
hdr = net_buf_add(buf, sizeof(*hdr));
8584
hdr->opcode = sys_cpu_to_le16(opcode);
@@ -305,7 +304,7 @@ static void recv(struct net_buf *buf)
305304
{
306305
LOG_HEXDUMP_DBG(buf->data, buf->len, "HCI RX");
307306

308-
uint8_t type = bt_buf_get_type(buf);
307+
uint8_t type = net_buf_pull_u8(buf);
309308
uint8_t code = buf->data[0];
310309

311310
if (type == BT_BUF_EVT) {
@@ -525,7 +524,7 @@ static int send_acl(struct net_buf *buf)
525524
hdr->handle = sys_cpu_to_le16(bt_acl_handle_pack(conn_handle, flags));
526525
hdr->len = sys_cpu_to_le16(buf->len - sizeof(*hdr));
527526

528-
bt_buf_set_type(buf, BT_BUF_ACL_OUT);
527+
net_buf_push_u8(buf, BT_BUF_ACL_OUT);
529528

530529
k_sem_take(&acl_pkts, K_FOREVER);
531530

tests/bsim/bluetooth/host/att/sequential/tester/src/main.c

+3-4
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,7 @@ struct net_buf *bt_hci_cmd_create(uint16_t opcode, uint8_t param_len)
7373

7474
LOG_DBG("buf %p", buf);
7575

76-
net_buf_reserve(buf, BT_BUF_RESERVE);
77-
bt_buf_set_type(buf, BT_BUF_CMD);
76+
net_buf_add_u8(buf, BT_BUF_CMD);
7877

7978
hdr = net_buf_add(buf, sizeof(*hdr));
8079
hdr->opcode = sys_cpu_to_le16(opcode);
@@ -282,7 +281,7 @@ static void recv(struct net_buf *buf)
282281
{
283282
LOG_HEXDUMP_DBG(buf->data, buf->len, "HCI RX");
284283

285-
uint8_t type = bt_buf_get_type(buf);
284+
uint8_t type = net_buf_pull_u8(buf);
286285
uint8_t code = buf->data[0];
287286

288287
if (type == BT_BUF_EVT) {
@@ -502,7 +501,7 @@ static int send_acl(struct net_buf *buf)
502501
hdr->handle = sys_cpu_to_le16(bt_acl_handle_pack(conn_handle, flags));
503502
hdr->len = sys_cpu_to_le16(buf->len - sizeof(*hdr));
504503

505-
bt_buf_set_type(buf, BT_BUF_ACL_OUT);
504+
net_buf_push_u8(buf, BT_BUF_ACL_OUT);
506505

507506
k_sem_take(&acl_pkts, K_FOREVER);
508507

tests/bsim/bluetooth/host/iso/frag/src/broadcaster.c

+2-6
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ void entrypoint_broadcaster(void)
232232

233233
void validate_no_iso_frag(struct net_buf *buf)
234234
{
235-
struct bt_hci_iso_hdr *hci_hdr = (void *)buf->data;
235+
struct bt_hci_iso_hdr *hci_hdr = (void *)(buf->data + 1);
236236

237237
uint16_t handle = sys_le16_to_cpu(hci_hdr->handle);
238238
uint8_t flags = bt_iso_flags(handle);
@@ -245,15 +245,11 @@ int __real_bt_send(struct net_buf *buf);
245245

246246
int __wrap_bt_send(struct net_buf *buf)
247247
{
248-
uint8_t type = bt_buf_get_type(buf);
249-
250248
LOG_HEXDUMP_DBG(buf->data, buf->len, "h->c");
251249

252-
if (type == BT_BUF_ISO_OUT) {
250+
if (buf->data[0] == BT_BUF_ISO_OUT) {
253251
validate_no_iso_frag(buf);
254252
}
255253

256-
bt_buf_set_type(buf, type);
257-
258254
return __real_bt_send(buf);
259255
}

tests/bsim/bluetooth/host/iso/frag_2/src/broadcaster.c

+2-5
Original file line numberDiff line numberDiff line change
@@ -269,10 +269,9 @@ int __real_bt_send(struct net_buf *buf);
269269

270270
int __wrap_bt_send(struct net_buf *buf)
271271
{
272-
uint8_t type = bt_buf_get_type(buf);
273-
struct bt_hci_iso_hdr *hci_hdr = (void *)buf->data;
272+
if (buf->data[0] == BT_BUF_ISO_OUT) {
273+
struct bt_hci_iso_hdr *hci_hdr = (void *)(buf->data + 1);
274274

275-
if (type == BT_BUF_ISO_OUT) {
276275
uint16_t handle = sys_le16_to_cpu(hci_hdr->handle);
277276
uint8_t flags = bt_iso_flags(handle);
278277
uint8_t pb_flag = bt_iso_flags_pb(flags);
@@ -283,7 +282,5 @@ int __wrap_bt_send(struct net_buf *buf)
283282
}
284283
}
285284

286-
bt_buf_set_type(buf, type);
287-
288285
return __real_bt_send(buf);
289286
}

0 commit comments

Comments
 (0)