Skip to content

Commit

Permalink
bluetooth: hci: refactored bluetooth hci packet type indicators
Browse files Browse the repository at this point in the history
Introduced a unified definition for HCI packet type indicators in
'bluetooth/hci_types.h. This change streamlines the code in
'drivers/bluetooth/hci/', reducing redundancy.
Enhances maintainability and consistency across all HCI drivers.

Signed-off-by: Pisit Sawangvonganan <[email protected]>
  • Loading branch information
ndrs-pst authored and aescolar committed May 1, 2024
1 parent 6205f82 commit 3d39926
Show file tree
Hide file tree
Showing 12 changed files with 95 additions and 145 deletions.
45 changes: 19 additions & 26 deletions drivers/bluetooth/hci/h4.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,6 @@ LOG_MODULE_REGISTER(bt_driver);

#include "../util.h"

#define H4_NONE 0x00
#define H4_CMD 0x01
#define H4_ACL 0x02
#define H4_SCO 0x03
#define H4_EVT 0x04
#define H4_ISO 0x05

static K_KERNEL_STACK_DEFINE(rx_thread_stack, CONFIG_BT_DRV_RX_STACK_SIZE);
static struct k_thread rx_thread_data;

Expand Down Expand Up @@ -78,20 +71,20 @@ static inline void h4_get_type(void)
/* Get packet type */
if (uart_fifo_read(h4_dev, &rx.type, 1) != 1) {
LOG_WRN("Unable to read H:4 packet type");
rx.type = H4_NONE;
rx.type = BT_HCI_H4_NONE;
return;
}

switch (rx.type) {
case H4_EVT:
case BT_HCI_H4_EVT:
rx.remaining = sizeof(rx.evt);
rx.hdr_len = rx.remaining;
break;
case H4_ACL:
case BT_HCI_H4_ACL:
rx.remaining = sizeof(rx.acl);
rx.hdr_len = rx.remaining;
break;
case H4_ISO:
case BT_HCI_H4_ISO:
if (IS_ENABLED(CONFIG_BT_ISO)) {
rx.remaining = sizeof(rx.iso);
rx.hdr_len = rx.remaining;
Expand All @@ -100,7 +93,7 @@ static inline void h4_get_type(void)
__fallthrough;
default:
LOG_ERR("Unknown H:4 type 0x%02x", rx.type);
rx.type = H4_NONE;
rx.type = BT_HCI_H4_NONE;
}
}

Expand Down Expand Up @@ -185,7 +178,7 @@ static inline void copy_hdr(struct net_buf *buf)

static void reset_rx(void)
{
rx.type = H4_NONE;
rx.type = BT_HCI_H4_NONE;
rx.remaining = 0U;
rx.have_hdr = false;
rx.hdr_len = 0U;
Expand All @@ -197,11 +190,11 @@ static struct net_buf *get_rx(k_timeout_t timeout)
LOG_DBG("type 0x%02x, evt 0x%02x", rx.type, rx.evt.evt);

switch (rx.type) {
case H4_EVT:
case BT_HCI_H4_EVT:
return bt_buf_get_evt(rx.evt.evt, rx.discardable, timeout);
case H4_ACL:
case BT_HCI_H4_ACL:
return bt_buf_get_rx(BT_BUF_ACL_IN, timeout);
case H4_ISO:
case BT_HCI_H4_ISO:
if (IS_ENABLED(CONFIG_BT_ISO)) {
return bt_buf_get_rx(BT_BUF_ISO_IN, timeout);
}
Expand Down Expand Up @@ -329,7 +322,7 @@ static inline void read_payload(void)
buf = rx.buf;
rx.buf = NULL;

if (rx.type == H4_EVT) {
if (rx.type == BT_HCI_H4_EVT) {
bt_buf_set_type(buf, BT_BUF_EVT);
} else {
bt_buf_set_type(buf, BT_BUF_ACL_IN);
Expand All @@ -344,16 +337,16 @@ static inline void read_payload(void)
static inline void read_header(void)
{
switch (rx.type) {
case H4_NONE:
case BT_HCI_H4_NONE:
h4_get_type();
return;
case H4_EVT:
case BT_HCI_H4_EVT:
get_evt_hdr();
break;
case H4_ACL:
case BT_HCI_H4_ACL:
get_acl_hdr();
break;
case H4_ISO:
case BT_HCI_H4_ISO:
if (IS_ENABLED(CONFIG_BT_ISO)) {
get_iso_hdr();
break;
Expand Down Expand Up @@ -391,14 +384,14 @@ static inline void process_tx(void)
if (!tx.type) {
switch (bt_buf_get_type(tx.buf)) {
case BT_BUF_ACL_OUT:
tx.type = H4_ACL;
tx.type = BT_HCI_H4_ACL;
break;
case BT_BUF_CMD:
tx.type = H4_CMD;
tx.type = BT_HCI_H4_CMD;
break;
case BT_BUF_ISO_OUT:
if (IS_ENABLED(CONFIG_BT_ISO)) {
tx.type = H4_ISO;
tx.type = BT_HCI_H4_ISO;
break;
}
__fallthrough;
Expand All @@ -410,7 +403,7 @@ static inline void process_tx(void)
bytes = uart_fifo_fill(h4_dev, &tx.type, 1);
if (bytes != 1) {
LOG_WRN("Unable to send H:4 type");
tx.type = H4_NONE;
tx.type = BT_HCI_H4_NONE;
return;
}
}
Expand All @@ -427,7 +420,7 @@ static inline void process_tx(void)
}

done:
tx.type = H4_NONE;
tx.type = BT_HCI_H4_NONE;
net_buf_unref(tx.buf);
tx.buf = net_buf_get(&tx.fifo, K_NO_WAIT);
if (!tx.buf) {
Expand Down
13 changes: 4 additions & 9 deletions drivers/bluetooth/hci/hci_ambiq.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,6 @@ LOG_MODULE_REGISTER(bt_hci_driver);
#define HCI_SPI_NODE DT_COMPAT_GET_ANY_STATUS_OKAY(ambiq_bt_hci_spi)
#define SPI_DEV_NODE DT_BUS(HCI_SPI_NODE)

#define HCI_CMD 0x01
#define HCI_ACL 0x02
#define HCI_SCO 0x03
#define HCI_EVT 0x04

/* Offset of special item */
#define PACKET_TYPE 0
#define PACKET_TYPE_SIZE 1
Expand Down Expand Up @@ -272,11 +267,11 @@ static void bt_spi_rx_thread(void *p1, void *p2, void *p3)
}

switch (rxmsg[PACKET_TYPE]) {
case HCI_EVT:
case BT_HCI_H4_EVT:
buf = bt_hci_evt_recv(&rxmsg[PACKET_TYPE + PACKET_TYPE_SIZE],
(len - PACKET_TYPE_SIZE));
break;
case HCI_ACL:
case BT_HCI_H4_ACL:
buf = bt_hci_acl_recv(&rxmsg[PACKET_TYPE + PACKET_TYPE_SIZE],
(len - PACKET_TYPE_SIZE));
break;
Expand Down Expand Up @@ -306,10 +301,10 @@ static int bt_hci_send(struct net_buf *buf)

switch (bt_buf_get_type(buf)) {
case BT_BUF_ACL_OUT:
net_buf_push_u8(buf, HCI_ACL);
net_buf_push_u8(buf, BT_HCI_H4_ACL);
break;
case BT_BUF_CMD:
net_buf_push_u8(buf, HCI_CMD);
net_buf_push_u8(buf, BT_HCI_H4_CMD);
break;
default:
LOG_ERR("Unsupported type");
Expand Down
12 changes: 4 additions & 8 deletions drivers/bluetooth/hci/hci_b91.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,6 @@
#include <zephyr/logging/log.h>
LOG_MODULE_REGISTER(bt_hci_driver_b91);

#define HCI_CMD 0x01
#define HCI_ACL 0x02
#define HCI_EVT 0x04

#define HCI_BT_B91_TIMEOUT K_MSEC(2000)

static K_SEM_DEFINE(hci_send_sem, 1, 1);
Expand Down Expand Up @@ -148,11 +144,11 @@ static void hci_b91_host_rcv_pkt(uint8_t *data, uint16_t len)
len -= sizeof(pkt_indicator);

switch (pkt_indicator) {
case HCI_EVT:
case BT_HCI_H4_EVT:
buf = bt_b91_evt_recv(data, len);
break;

case HCI_ACL:
case BT_HCI_H4_ACL:
buf = bt_b91_acl_recv(data, len);
break;

Expand Down Expand Up @@ -186,11 +182,11 @@ static int bt_b91_send(struct net_buf *buf)

switch (bt_buf_get_type(buf)) {
case BT_BUF_ACL_OUT:
type = HCI_ACL;
type = BT_HCI_H4_ACL;
break;

case BT_BUF_CMD:
type = HCI_CMD;
type = BT_HCI_H4_CMD;
break;

default:
Expand Down
18 changes: 6 additions & 12 deletions drivers/bluetooth/hci/hci_esp32.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,6 @@
#include <zephyr/logging/log.h>
LOG_MODULE_REGISTER(bt_hci_driver_esp32);

#define HCI_CMD 0x01
#define HCI_ACL 0x02
#define HCI_SCO 0x03
#define HCI_EVT 0x04
#define HCI_ISO 0x05

#define HCI_BT_ESP32_TIMEOUT K_MSEC(2000)

static K_SEM_DEFINE(hci_send_sem, 1, 1);
Expand Down Expand Up @@ -196,15 +190,15 @@ static int hci_esp_host_rcv_pkt(uint8_t *data, uint16_t len)
remaining -= sizeof(pkt_indicator);

switch (pkt_indicator) {
case HCI_EVT:
case BT_HCI_H4_EVT:
buf = bt_esp_evt_recv(data, remaining);
break;

case HCI_ACL:
case BT_HCI_H4_ACL:
buf = bt_esp_acl_recv(data, remaining);
break;

case HCI_SCO:
case BT_HCI_H4_SCO:
buf = bt_esp_iso_recv(data, remaining);
break;

Expand Down Expand Up @@ -241,13 +235,13 @@ static int bt_esp32_send(struct net_buf *buf)

switch (bt_buf_get_type(buf)) {
case BT_BUF_ACL_OUT:
pkt_indicator = HCI_ACL;
pkt_indicator = BT_HCI_H4_ACL;
break;
case BT_BUF_CMD:
pkt_indicator = HCI_CMD;
pkt_indicator = BT_HCI_H4_CMD;
break;
case BT_BUF_ISO_OUT:
pkt_indicator = HCI_ISO;
pkt_indicator = BT_HCI_H4_ISO;
break;
default:
LOG_ERR("Unknown type %u", bt_buf_get_type(buf));
Expand Down
13 changes: 4 additions & 9 deletions drivers/bluetooth/hci/hci_psoc6_bless.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,6 @@ LOG_MODULE_REGISTER(psoc6_bless);

#define DT_DRV_COMPAT infineon_cat1_bless_hci

#define PACKET_TYPE_HCI_COMMAND 0X1
#define PACKET_TYPE_HCI_ACL_DATA 0x2
#define PACKET_TYPE_HCI_SYNCHRONOUS 0X3
#define PACKET_TYPE_HCI_EVENT 0X4

#define BLE_LOCK_TMOUT_MS (1000)
#define BLE_THREAD_SEM_TMOUT_MS (1000)

Expand Down Expand Up @@ -112,15 +107,15 @@ static void psoc6_bless_events_handler(uint32_t eventCode, void *eventParam)
hci_rx = eventParam;

switch (hci_rx->packetType) {
case PACKET_TYPE_HCI_EVENT:
case BT_HCI_H4_EVT:
buf = bt_buf_get_evt(hci_rx->data[0], 0, K_NO_WAIT);
if (!buf) {
LOG_ERR("Failed to allocate the buffer for RX: EVENT ");
return;
}

break;
case PACKET_TYPE_HCI_ACL_DATA:
case BT_HCI_H4_ACL:
buf = bt_buf_get_rx(BT_BUF_ACL_IN, K_NO_WAIT);
if (!buf) {
LOG_ERR("Failed to allocate the buffer for RX: ACL ");
Expand Down Expand Up @@ -168,10 +163,10 @@ static int psoc6_bless_send(struct net_buf *buf)

switch (bt_buf_get_type(buf)) {
case BT_BUF_ACL_OUT:
hci_tx_pkt.packetType = PACKET_TYPE_HCI_ACL_DATA;
hci_tx_pkt.packetType = BT_HCI_H4_ACL;
break;
case BT_BUF_CMD:
hci_tx_pkt.packetType = PACKET_TYPE_HCI_COMMAND;
hci_tx_pkt.packetType = BT_HCI_H4_CMD;
break;
default:
net_buf_unref(buf);
Expand Down
19 changes: 7 additions & 12 deletions drivers/bluetooth/hci/hci_spi_st.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,6 @@
#include <zephyr/logging/log.h>
LOG_MODULE_REGISTER(bt_driver);

#define HCI_CMD 0x01
#define HCI_ACL 0x02
#define HCI_SCO 0x03
#define HCI_EVT 0x04
#define HCI_ISO 0x05
/* ST Proprietary extended event */
#define HCI_EXT_EVT 0x82

Expand Down Expand Up @@ -378,10 +373,10 @@ static int bt_spi_rx_buf_construct(uint8_t *msg, struct net_buf **bufp, uint16_t
}
/* Use memmove instead of memcpy due to buffer overlapping */
memmove(msg + (1 + sizeof(*evt2)), msg + (1 + sizeof(*evt)), evt2->len);
/* Manage event as regular HCI_EVT */
/* Manage event as regular BT_HCI_H4_EVT */
__fallthrough;
#endif /* DT_HAS_COMPAT_STATUS_OKAY(st_hci_spi_v2) */
case HCI_EVT:
case BT_HCI_H4_EVT:
switch (msg[EVT_HEADER_EVENT]) {
case BT_HCI_EVT_VENDOR:
/* Run event through interface handler */
Expand Down Expand Up @@ -419,7 +414,7 @@ static int bt_spi_rx_buf_construct(uint8_t *msg, struct net_buf **bufp, uint16_t
}
#endif /* DT_HAS_COMPAT_STATUS_OKAY(st_hci_spi_v1) */
break;
case HCI_ACL:
case BT_HCI_H4_ACL:
buf = bt_buf_get_rx(BT_BUF_ACL_IN, K_FOREVER);
memcpy(&acl_hdr, &msg[1], sizeof(acl_hdr));
len = sizeof(acl_hdr) + sys_le16_to_cpu(acl_hdr.len);
Expand All @@ -431,7 +426,7 @@ static int bt_spi_rx_buf_construct(uint8_t *msg, struct net_buf **bufp, uint16_t
net_buf_add_mem(buf, &msg[1], len);
break;
#if defined(CONFIG_BT_ISO)
case HCI_ISO:
case BT_HCI_H4_ISO:
struct bt_hci_iso_hdr iso_hdr;

buf = bt_buf_get_rx(BT_BUF_ISO_IN, timeout);
Expand Down Expand Up @@ -529,14 +524,14 @@ static int bt_spi_send(struct net_buf *buf)

switch (bt_buf_get_type(buf)) {
case BT_BUF_ACL_OUT:
net_buf_push_u8(buf, HCI_ACL);
net_buf_push_u8(buf, BT_HCI_H4_ACL);
break;
case BT_BUF_CMD:
net_buf_push_u8(buf, HCI_CMD);
net_buf_push_u8(buf, BT_HCI_H4_CMD);
break;
#if defined(CONFIG_BT_ISO)
case BT_BUF_ISO_OUT:
net_buf_push_u8(buf, HCI_ISO);
net_buf_push_u8(buf, BT_HCI_H4_ISO);
break;
#endif /* CONFIG_BT_ISO */
default:
Expand Down
Loading

0 comments on commit 3d39926

Please sign in to comment.