Skip to content

Commit 6113230

Browse files
jhedbergkartben
authored andcommitted
Bluetooth: drivers: Update to use new H:4 buffer encoding
Update all HCI drivers to use the new H:4 encoding for buffers passing to/from drivers. One behavioral change that's done in favor of simplicity, is that where there's previously been switch statements that could return an error for unsupported packet types now simply pass any received packet unchanged to lower layers of the controller (or the HCI transport). Handling this is now the responsibility of the lower layers, however in practice hitting such scenarios means that there's a mismatch between configured host and controller features. Signed-off-by: Johan Hedberg <[email protected]>
1 parent 26d9716 commit 6113230

File tree

19 files changed

+49
-296
lines changed

19 files changed

+49
-296
lines changed

drivers/bluetooth/hci/h4.c

Lines changed: 1 addition & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -348,12 +348,6 @@ static inline void read_payload(const struct device *dev)
348348
buf = h4->rx.buf;
349349
h4->rx.buf = NULL;
350350

351-
if (h4->rx.type == BT_HCI_H4_EVT) {
352-
bt_buf_set_type(buf, BT_BUF_EVT);
353-
} else {
354-
bt_buf_set_type(buf, BT_BUF_ACL_IN);
355-
}
356-
357351
reset_rx(h4);
358352

359353
LOG_DBG("Putting buf %p to rx fifo", buf);
@@ -411,33 +405,6 @@ static inline void process_tx(const struct device *dev)
411405
}
412406
}
413407

414-
if (!h4->tx.type) {
415-
switch (bt_buf_get_type(h4->tx.buf)) {
416-
case BT_BUF_ACL_OUT:
417-
h4->tx.type = BT_HCI_H4_ACL;
418-
break;
419-
case BT_BUF_CMD:
420-
h4->tx.type = BT_HCI_H4_CMD;
421-
break;
422-
case BT_BUF_ISO_OUT:
423-
if (IS_ENABLED(CONFIG_BT_ISO)) {
424-
h4->tx.type = BT_HCI_H4_ISO;
425-
break;
426-
}
427-
__fallthrough;
428-
default:
429-
LOG_ERR("Unknown buffer type");
430-
goto done;
431-
}
432-
433-
bytes = uart_fifo_fill(cfg->uart, &h4->tx.type, 1);
434-
if (bytes != 1) {
435-
LOG_WRN("Unable to send H:4 type");
436-
h4->tx.type = BT_HCI_H4_NONE;
437-
return;
438-
}
439-
}
440-
441408
bytes = uart_fifo_fill(cfg->uart, h4->tx.buf->data, h4->tx.buf->len);
442409
if (unlikely(bytes < 0)) {
443410
LOG_ERR("Unable to write to UART (err %d)", bytes);
@@ -449,7 +416,6 @@ static inline void process_tx(const struct device *dev)
449416
return;
450417
}
451418

452-
done:
453419
h4->tx.type = BT_HCI_H4_NONE;
454420
net_buf_unref(h4->tx.buf);
455421
h4->tx.buf = k_fifo_get(&h4->tx.fifo, K_NO_WAIT);
@@ -499,7 +465,7 @@ static int h4_send(const struct device *dev, struct net_buf *buf)
499465
const struct h4_config *cfg = dev->config;
500466
struct h4_data *h4 = dev->data;
501467

502-
LOG_DBG("buf %p type %u len %u", buf, bt_buf_get_type(buf), buf->len);
468+
LOG_DBG("buf %p type %u len %u", buf, buf->data[0], buf->len);
503469

504470
k_fifo_put(&h4->tx.fifo, buf);
505471
uart_irq_tx_enable(cfg->uart);

drivers/bluetooth/hci/h5.c

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -598,26 +598,8 @@ static uint8_t h5_get_type(struct net_buf *buf)
598598
static int h5_queue(const struct device *dev, struct net_buf *buf)
599599
{
600600
struct h5_data *h5 = dev->data;
601-
uint8_t type;
602601

603-
LOG_DBG("buf %p type %u len %u", buf, bt_buf_get_type(buf), buf->len);
604-
605-
switch (bt_buf_get_type(buf)) {
606-
case BT_BUF_CMD:
607-
type = HCI_COMMAND_PKT;
608-
break;
609-
case BT_BUF_ACL_OUT:
610-
type = HCI_ACLDATA_PKT;
611-
break;
612-
case BT_BUF_ISO_OUT:
613-
type = HCI_ISODATA_PKT;
614-
break;
615-
default:
616-
LOG_ERR("Unknown packet type %u", bt_buf_get_type(buf));
617-
return -1;
618-
}
619-
620-
memcpy(net_buf_push(buf, sizeof(type)), &type, sizeof(type));
602+
LOG_DBG("buf %p type %u len %u", buf, buf->data[0], buf->len);
621603

622604
k_fifo_put(&h5->tx_queue, buf);
623605

drivers/bluetooth/hci/hci_ambiq.c

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -344,27 +344,13 @@ static void bt_spi_rx_thread(void *p1, void *p2, void *p3)
344344

345345
static int bt_apollo_send(const struct device *dev, struct net_buf *buf)
346346
{
347-
int ret = 0;
347+
int ret;
348348

349-
/* Buffer needs an additional byte for type */
350-
if (buf->len >= SPI_MAX_TX_MSG_LEN) {
349+
if (buf->len > SPI_MAX_TX_MSG_LEN) {
351350
LOG_ERR("Message too long");
352351
return -EINVAL;
353352
}
354353

355-
switch (bt_buf_get_type(buf)) {
356-
case BT_BUF_ACL_OUT:
357-
net_buf_push_u8(buf, BT_HCI_H4_ACL);
358-
break;
359-
case BT_BUF_CMD:
360-
net_buf_push_u8(buf, BT_HCI_H4_CMD);
361-
break;
362-
default:
363-
LOG_ERR("Unsupported type");
364-
net_buf_unref(buf);
365-
return -EINVAL;
366-
}
367-
368354
/* Send the SPI packet */
369355
ret = spi_send_packet(buf->data, buf->len);
370356

drivers/bluetooth/hci/hci_da1469x.c

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -330,12 +330,6 @@ static inline void read_payload(void)
330330
buf = rx.buf;
331331
rx.buf = NULL;
332332

333-
if (rx.type == BT_HCI_H4_EVT) {
334-
bt_buf_set_type(buf, BT_BUF_EVT);
335-
} else {
336-
bt_buf_set_type(buf, BT_BUF_ACL_IN);
337-
}
338-
339333
reset_rx();
340334

341335
LOG_DBG("Putting buf %p to rx fifo", buf);
@@ -474,20 +468,6 @@ static int bt_da1469x_send(const struct device *dev, struct net_buf *buf)
474468
{
475469
ARG_UNUSED(dev);
476470

477-
switch (bt_buf_get_type(buf)) {
478-
case BT_BUF_ACL_OUT:
479-
LOG_DBG("ACL: buf %p type %u len %u", buf, bt_buf_get_type(buf), buf->len);
480-
net_buf_push_u8(buf, BT_HCI_H4_ACL);
481-
break;
482-
case BT_BUF_CMD:
483-
LOG_DBG("CMD: buf %p type %u len %u", buf, bt_buf_get_type(buf), buf->len);
484-
net_buf_push_u8(buf, BT_HCI_H4_CMD);
485-
break;
486-
default:
487-
LOG_ERR("Unsupported type");
488-
return -EINVAL;
489-
}
490-
491471
cmac_mbox_write(buf->data, buf->len);
492472

493473
net_buf_unref(buf);

drivers/bluetooth/hci/hci_esp32.c

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -237,25 +237,8 @@ static esp_vhci_host_callback_t vhci_host_cb = {
237237
static int bt_esp32_send(const struct device *dev, struct net_buf *buf)
238238
{
239239
int err = 0;
240-
uint8_t pkt_indicator;
241-
242-
LOG_DBG("buf %p type %u len %u", buf, bt_buf_get_type(buf), buf->len);
243240

244-
switch (bt_buf_get_type(buf)) {
245-
case BT_BUF_ACL_OUT:
246-
pkt_indicator = BT_HCI_H4_ACL;
247-
break;
248-
case BT_BUF_CMD:
249-
pkt_indicator = BT_HCI_H4_CMD;
250-
break;
251-
case BT_BUF_ISO_OUT:
252-
pkt_indicator = BT_HCI_H4_ISO;
253-
break;
254-
default:
255-
LOG_ERR("Unknown type %u", bt_buf_get_type(buf));
256-
goto done;
257-
}
258-
net_buf_push_u8(buf, pkt_indicator);
241+
LOG_DBG("buf %p type %u len %u", buf, buf->data[0], buf->len);
259242

260243
LOG_HEXDUMP_DBG(buf->data, buf->len, "Final HCI buffer:");
261244

@@ -270,7 +253,6 @@ static int bt_esp32_send(const struct device *dev, struct net_buf *buf)
270253
err = -ETIMEDOUT;
271254
}
272255

273-
done:
274256
net_buf_unref(buf);
275257
k_sem_give(&hci_send_sem);
276258

drivers/bluetooth/hci/hci_ifx_cyw208xx.c

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -257,32 +257,36 @@ static int cyw208xx_close(const struct device *dev)
257257

258258
static int cyw208xx_send(const struct device *dev, struct net_buf *buf)
259259
{
260+
uint8_t type;
261+
260262
ARG_UNUSED(dev);
261263

262264
int ret = 0;
263265

264266
k_sem_take(&hci_sem, K_FOREVER);
265267

266-
LOG_DBG("buf %p type %u len %u", buf, bt_buf_get_type(buf), buf->len);
268+
type = net_buf_pull_u8(buf);
269+
270+
LOG_DBG("buf %p type %u len %u", buf, type, buf->len);
267271

268-
switch (bt_buf_get_type(buf)) {
269-
case BT_BUF_ACL_OUT:
272+
switch (type) {
273+
case BT_HCI_H4_ACL:
270274
uint8_t *bt_msg = host_stack_get_acl_to_lower_buffer(BT_TRANSPORT_LE, buf->len);
271275

272276
memcpy(bt_msg, buf->data, buf->len);
273277
ret = host_stack_send_acl_to_lower(BT_TRANSPORT_LE, bt_msg, buf->len);
274278
break;
275279

276-
case BT_BUF_CMD:
280+
case BT_HCI_H4_CMD:
277281
ret = host_stack_send_cmd_to_lower(buf->data, buf->len);
278282
break;
279283

280-
case BT_BUF_ISO_OUT:
284+
case BT_HCI_H4_ISO:
281285
ret = host_stack_send_iso_to_lower(buf->data, buf->len);
282286
break;
283287

284288
default:
285-
LOG_ERR("Unknown type %u", bt_buf_get_type(buf));
289+
LOG_ERR("Unknown type %u", type);
286290
ret = EIO;
287291
goto done;
288292
}
@@ -376,7 +380,6 @@ void wiced_bt_process_hci(hci_packet_type_t pti, uint8_t *data, uint32_t length)
376380
LOG_ERR("Failed to allocate the buffer for RX: ACL ");
377381
return;
378382
}
379-
bt_buf_set_type(buf, BT_BUF_ACL_IN);
380383
break;
381384

382385
case HCI_PACKET_TYPE_SCO:

drivers/bluetooth/hci/hci_ifx_psoc6_bless.c

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -126,10 +126,8 @@ static void psoc6_bless_events_handler(uint32_t eventCode, void *eventParam)
126126
LOG_ERR("Failed to allocate the buffer for RX: ACL ");
127127
return;
128128
}
129-
bt_buf_set_type(buf, BT_BUF_ACL_IN);
130129

131130
break;
132-
133131
default:
134132
LOG_WRN("Unsupported HCI Packet Received");
135133
return;
@@ -168,21 +166,10 @@ static int psoc6_bless_send(const struct device *dev, struct net_buf *buf)
168166

169167
memset(&hci_tx_pkt, 0, sizeof(cy_stc_ble_hci_tx_packet_info_t));
170168

169+
hci_tx_pkt.packetType = net_buf_pull_u8(buf);
171170
hci_tx_pkt.dataLength = buf->len;
172171
hci_tx_pkt.data = buf->data;
173172

174-
switch (bt_buf_get_type(buf)) {
175-
case BT_BUF_ACL_OUT:
176-
hci_tx_pkt.packetType = BT_HCI_H4_ACL;
177-
break;
178-
case BT_BUF_CMD:
179-
hci_tx_pkt.packetType = BT_HCI_H4_CMD;
180-
break;
181-
default:
182-
net_buf_unref(buf);
183-
return -ENOTSUP;
184-
}
185-
186173
if (k_sem_take(&psoc6_bless_operation_sem, K_MSEC(BLE_LOCK_TMOUT_MS)) != 0) {
187174
LOG_ERR("Failed to acquire BLE DRV Semaphore");
188175
net_buf_unref(buf);

drivers/bluetooth/hci/hci_nxp.c

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -403,23 +403,8 @@ static void hci_rx_cb(uint8_t packetType, uint8_t *data, uint16_t len)
403403

404404
static int bt_nxp_send(const struct device *dev, struct net_buf *buf)
405405
{
406-
uint8_t packetType;
407-
408406
ARG_UNUSED(dev);
409407

410-
switch (bt_buf_get_type(buf)) {
411-
case BT_BUF_CMD:
412-
packetType = BT_HCI_H4_CMD;
413-
break;
414-
case BT_BUF_ACL_OUT:
415-
packetType = BT_HCI_H4_ACL;
416-
break;
417-
default:
418-
LOG_ERR("Not supported type");
419-
return -1;
420-
}
421-
422-
net_buf_push_u8(buf, packetType);
423408
#if defined(HCI_NXP_LOCK_STANDBY_BEFORE_SEND)
424409
/* Sending an HCI message requires to wake up the controller core if it's asleep.
425410
* Platform controllers may send reponses using non wakeable interrupts which can

drivers/bluetooth/hci/hci_silabs_efr32.c

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -195,28 +195,12 @@ uint32_t hci_common_transport_transmit(uint8_t *data, int16_t len)
195195

196196
static int slz_bt_send(const struct device *dev, struct net_buf *buf)
197197
{
198-
int rv = 0;
198+
int rv;
199199

200200
ARG_UNUSED(dev);
201201

202-
switch (bt_buf_get_type(buf)) {
203-
case BT_BUF_ACL_OUT:
204-
net_buf_push_u8(buf, BT_HCI_H4_ACL);
205-
break;
206-
case BT_BUF_CMD:
207-
net_buf_push_u8(buf, BT_HCI_H4_CMD);
208-
break;
209-
default:
210-
rv = -EINVAL;
211-
goto done;
212-
}
213-
214202
rv = hci_common_transport_receive(buf->data, buf->len, true);
215-
if (!rv) {
216-
goto done;
217-
}
218203

219-
done:
220204
net_buf_unref(buf);
221205
return rv;
222206
}

drivers/bluetooth/hci/hci_silabs_siwx91x.c

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -36,22 +36,8 @@ static int siwx91x_bt_send(const struct device *dev, struct net_buf *buf)
3636
{
3737
struct hci_data *hci = dev->data;
3838
int sc = -EOVERFLOW;
39-
uint8_t packet_type = BT_HCI_H4_NONE;
40-
41-
switch (bt_buf_get_type(buf)) {
42-
case BT_BUF_ACL_OUT:
43-
packet_type = BT_HCI_H4_ACL;
44-
break;
45-
case BT_BUF_CMD:
46-
packet_type = BT_HCI_H4_CMD;
47-
break;
48-
default:
49-
sc = -EINVAL;
50-
break;
51-
}
5239

53-
if ((packet_type != BT_HCI_H4_NONE) && (buf->len < sizeof(hci->rsi_data_packet.data))) {
54-
net_buf_push_u8(buf, packet_type);
40+
if (buf->len < sizeof(hci->rsi_data_packet.data)) {
5541
memcpy(&hci->rsi_data_packet, buf->data, buf->len);
5642
sc = rsi_bt_driver_send_cmd(RSI_BLE_REQ_HCI_RAW, &hci->rsi_data_packet, NULL);
5743
/* TODO SILABS ZEPHYR Convert to errno. A common function from rsi/sl_status should

0 commit comments

Comments
 (0)