Skip to content

Commit 3d39926

Browse files
ndrs-pstaescolar
authored andcommitted
bluetooth: hci: refactored bluetooth hci packet type indicators
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]>
1 parent 6205f82 commit 3d39926

File tree

12 files changed

+95
-145
lines changed

12 files changed

+95
-145
lines changed

drivers/bluetooth/hci/h4.c

Lines changed: 19 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,6 @@ LOG_MODULE_REGISTER(bt_driver);
3030

3131
#include "../util.h"
3232

33-
#define H4_NONE 0x00
34-
#define H4_CMD 0x01
35-
#define H4_ACL 0x02
36-
#define H4_SCO 0x03
37-
#define H4_EVT 0x04
38-
#define H4_ISO 0x05
39-
4033
static K_KERNEL_STACK_DEFINE(rx_thread_stack, CONFIG_BT_DRV_RX_STACK_SIZE);
4134
static struct k_thread rx_thread_data;
4235

@@ -78,20 +71,20 @@ static inline void h4_get_type(void)
7871
/* Get packet type */
7972
if (uart_fifo_read(h4_dev, &rx.type, 1) != 1) {
8073
LOG_WRN("Unable to read H:4 packet type");
81-
rx.type = H4_NONE;
74+
rx.type = BT_HCI_H4_NONE;
8275
return;
8376
}
8477

8578
switch (rx.type) {
86-
case H4_EVT:
79+
case BT_HCI_H4_EVT:
8780
rx.remaining = sizeof(rx.evt);
8881
rx.hdr_len = rx.remaining;
8982
break;
90-
case H4_ACL:
83+
case BT_HCI_H4_ACL:
9184
rx.remaining = sizeof(rx.acl);
9285
rx.hdr_len = rx.remaining;
9386
break;
94-
case H4_ISO:
87+
case BT_HCI_H4_ISO:
9588
if (IS_ENABLED(CONFIG_BT_ISO)) {
9689
rx.remaining = sizeof(rx.iso);
9790
rx.hdr_len = rx.remaining;
@@ -100,7 +93,7 @@ static inline void h4_get_type(void)
10093
__fallthrough;
10194
default:
10295
LOG_ERR("Unknown H:4 type 0x%02x", rx.type);
103-
rx.type = H4_NONE;
96+
rx.type = BT_HCI_H4_NONE;
10497
}
10598
}
10699

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

186179
static void reset_rx(void)
187180
{
188-
rx.type = H4_NONE;
181+
rx.type = BT_HCI_H4_NONE;
189182
rx.remaining = 0U;
190183
rx.have_hdr = false;
191184
rx.hdr_len = 0U;
@@ -197,11 +190,11 @@ static struct net_buf *get_rx(k_timeout_t timeout)
197190
LOG_DBG("type 0x%02x, evt 0x%02x", rx.type, rx.evt.evt);
198191

199192
switch (rx.type) {
200-
case H4_EVT:
193+
case BT_HCI_H4_EVT:
201194
return bt_buf_get_evt(rx.evt.evt, rx.discardable, timeout);
202-
case H4_ACL:
195+
case BT_HCI_H4_ACL:
203196
return bt_buf_get_rx(BT_BUF_ACL_IN, timeout);
204-
case H4_ISO:
197+
case BT_HCI_H4_ISO:
205198
if (IS_ENABLED(CONFIG_BT_ISO)) {
206199
return bt_buf_get_rx(BT_BUF_ISO_IN, timeout);
207200
}
@@ -329,7 +322,7 @@ static inline void read_payload(void)
329322
buf = rx.buf;
330323
rx.buf = NULL;
331324

332-
if (rx.type == H4_EVT) {
325+
if (rx.type == BT_HCI_H4_EVT) {
333326
bt_buf_set_type(buf, BT_BUF_EVT);
334327
} else {
335328
bt_buf_set_type(buf, BT_BUF_ACL_IN);
@@ -344,16 +337,16 @@ static inline void read_payload(void)
344337
static inline void read_header(void)
345338
{
346339
switch (rx.type) {
347-
case H4_NONE:
340+
case BT_HCI_H4_NONE:
348341
h4_get_type();
349342
return;
350-
case H4_EVT:
343+
case BT_HCI_H4_EVT:
351344
get_evt_hdr();
352345
break;
353-
case H4_ACL:
346+
case BT_HCI_H4_ACL:
354347
get_acl_hdr();
355348
break;
356-
case H4_ISO:
349+
case BT_HCI_H4_ISO:
357350
if (IS_ENABLED(CONFIG_BT_ISO)) {
358351
get_iso_hdr();
359352
break;
@@ -391,14 +384,14 @@ static inline void process_tx(void)
391384
if (!tx.type) {
392385
switch (bt_buf_get_type(tx.buf)) {
393386
case BT_BUF_ACL_OUT:
394-
tx.type = H4_ACL;
387+
tx.type = BT_HCI_H4_ACL;
395388
break;
396389
case BT_BUF_CMD:
397-
tx.type = H4_CMD;
390+
tx.type = BT_HCI_H4_CMD;
398391
break;
399392
case BT_BUF_ISO_OUT:
400393
if (IS_ENABLED(CONFIG_BT_ISO)) {
401-
tx.type = H4_ISO;
394+
tx.type = BT_HCI_H4_ISO;
402395
break;
403396
}
404397
__fallthrough;
@@ -410,7 +403,7 @@ static inline void process_tx(void)
410403
bytes = uart_fifo_fill(h4_dev, &tx.type, 1);
411404
if (bytes != 1) {
412405
LOG_WRN("Unable to send H:4 type");
413-
tx.type = H4_NONE;
406+
tx.type = BT_HCI_H4_NONE;
414407
return;
415408
}
416409
}
@@ -427,7 +420,7 @@ static inline void process_tx(void)
427420
}
428421

429422
done:
430-
tx.type = H4_NONE;
423+
tx.type = BT_HCI_H4_NONE;
431424
net_buf_unref(tx.buf);
432425
tx.buf = net_buf_get(&tx.fifo, K_NO_WAIT);
433426
if (!tx.buf) {

drivers/bluetooth/hci/hci_ambiq.c

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,6 @@ LOG_MODULE_REGISTER(bt_hci_driver);
2525
#define HCI_SPI_NODE DT_COMPAT_GET_ANY_STATUS_OKAY(ambiq_bt_hci_spi)
2626
#define SPI_DEV_NODE DT_BUS(HCI_SPI_NODE)
2727

28-
#define HCI_CMD 0x01
29-
#define HCI_ACL 0x02
30-
#define HCI_SCO 0x03
31-
#define HCI_EVT 0x04
32-
3328
/* Offset of special item */
3429
#define PACKET_TYPE 0
3530
#define PACKET_TYPE_SIZE 1
@@ -272,11 +267,11 @@ static void bt_spi_rx_thread(void *p1, void *p2, void *p3)
272267
}
273268

274269
switch (rxmsg[PACKET_TYPE]) {
275-
case HCI_EVT:
270+
case BT_HCI_H4_EVT:
276271
buf = bt_hci_evt_recv(&rxmsg[PACKET_TYPE + PACKET_TYPE_SIZE],
277272
(len - PACKET_TYPE_SIZE));
278273
break;
279-
case HCI_ACL:
274+
case BT_HCI_H4_ACL:
280275
buf = bt_hci_acl_recv(&rxmsg[PACKET_TYPE + PACKET_TYPE_SIZE],
281276
(len - PACKET_TYPE_SIZE));
282277
break;
@@ -306,10 +301,10 @@ static int bt_hci_send(struct net_buf *buf)
306301

307302
switch (bt_buf_get_type(buf)) {
308303
case BT_BUF_ACL_OUT:
309-
net_buf_push_u8(buf, HCI_ACL);
304+
net_buf_push_u8(buf, BT_HCI_H4_ACL);
310305
break;
311306
case BT_BUF_CMD:
312-
net_buf_push_u8(buf, HCI_CMD);
307+
net_buf_push_u8(buf, BT_HCI_H4_CMD);
313308
break;
314309
default:
315310
LOG_ERR("Unsupported type");

drivers/bluetooth/hci/hci_b91.c

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,6 @@
1515
#include <zephyr/logging/log.h>
1616
LOG_MODULE_REGISTER(bt_hci_driver_b91);
1717

18-
#define HCI_CMD 0x01
19-
#define HCI_ACL 0x02
20-
#define HCI_EVT 0x04
21-
2218
#define HCI_BT_B91_TIMEOUT K_MSEC(2000)
2319

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

150146
switch (pkt_indicator) {
151-
case HCI_EVT:
147+
case BT_HCI_H4_EVT:
152148
buf = bt_b91_evt_recv(data, len);
153149
break;
154150

155-
case HCI_ACL:
151+
case BT_HCI_H4_ACL:
156152
buf = bt_b91_acl_recv(data, len);
157153
break;
158154

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

187183
switch (bt_buf_get_type(buf)) {
188184
case BT_BUF_ACL_OUT:
189-
type = HCI_ACL;
185+
type = BT_HCI_H4_ACL;
190186
break;
191187

192188
case BT_BUF_CMD:
193-
type = HCI_CMD;
189+
type = BT_HCI_H4_CMD;
194190
break;
195191

196192
default:

drivers/bluetooth/hci/hci_esp32.c

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,6 @@
1717
#include <zephyr/logging/log.h>
1818
LOG_MODULE_REGISTER(bt_hci_driver_esp32);
1919

20-
#define HCI_CMD 0x01
21-
#define HCI_ACL 0x02
22-
#define HCI_SCO 0x03
23-
#define HCI_EVT 0x04
24-
#define HCI_ISO 0x05
25-
2620
#define HCI_BT_ESP32_TIMEOUT K_MSEC(2000)
2721

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

198192
switch (pkt_indicator) {
199-
case HCI_EVT:
193+
case BT_HCI_H4_EVT:
200194
buf = bt_esp_evt_recv(data, remaining);
201195
break;
202196

203-
case HCI_ACL:
197+
case BT_HCI_H4_ACL:
204198
buf = bt_esp_acl_recv(data, remaining);
205199
break;
206200

207-
case HCI_SCO:
201+
case BT_HCI_H4_SCO:
208202
buf = bt_esp_iso_recv(data, remaining);
209203
break;
210204

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

242236
switch (bt_buf_get_type(buf)) {
243237
case BT_BUF_ACL_OUT:
244-
pkt_indicator = HCI_ACL;
238+
pkt_indicator = BT_HCI_H4_ACL;
245239
break;
246240
case BT_BUF_CMD:
247-
pkt_indicator = HCI_CMD;
241+
pkt_indicator = BT_HCI_H4_CMD;
248242
break;
249243
case BT_BUF_ISO_OUT:
250-
pkt_indicator = HCI_ISO;
244+
pkt_indicator = BT_HCI_H4_ISO;
251245
break;
252246
default:
253247
LOG_ERR("Unknown type %u", bt_buf_get_type(buf));

drivers/bluetooth/hci/hci_psoc6_bless.c

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,6 @@ LOG_MODULE_REGISTER(psoc6_bless);
3333

3434
#define DT_DRV_COMPAT infineon_cat1_bless_hci
3535

36-
#define PACKET_TYPE_HCI_COMMAND 0X1
37-
#define PACKET_TYPE_HCI_ACL_DATA 0x2
38-
#define PACKET_TYPE_HCI_SYNCHRONOUS 0X3
39-
#define PACKET_TYPE_HCI_EVENT 0X4
40-
4136
#define BLE_LOCK_TMOUT_MS (1000)
4237
#define BLE_THREAD_SEM_TMOUT_MS (1000)
4338

@@ -112,15 +107,15 @@ static void psoc6_bless_events_handler(uint32_t eventCode, void *eventParam)
112107
hci_rx = eventParam;
113108

114109
switch (hci_rx->packetType) {
115-
case PACKET_TYPE_HCI_EVENT:
110+
case BT_HCI_H4_EVT:
116111
buf = bt_buf_get_evt(hci_rx->data[0], 0, K_NO_WAIT);
117112
if (!buf) {
118113
LOG_ERR("Failed to allocate the buffer for RX: EVENT ");
119114
return;
120115
}
121116

122117
break;
123-
case PACKET_TYPE_HCI_ACL_DATA:
118+
case BT_HCI_H4_ACL:
124119
buf = bt_buf_get_rx(BT_BUF_ACL_IN, K_NO_WAIT);
125120
if (!buf) {
126121
LOG_ERR("Failed to allocate the buffer for RX: ACL ");
@@ -168,10 +163,10 @@ static int psoc6_bless_send(struct net_buf *buf)
168163

169164
switch (bt_buf_get_type(buf)) {
170165
case BT_BUF_ACL_OUT:
171-
hci_tx_pkt.packetType = PACKET_TYPE_HCI_ACL_DATA;
166+
hci_tx_pkt.packetType = BT_HCI_H4_ACL;
172167
break;
173168
case BT_BUF_CMD:
174-
hci_tx_pkt.packetType = PACKET_TYPE_HCI_COMMAND;
169+
hci_tx_pkt.packetType = BT_HCI_H4_CMD;
175170
break;
176171
default:
177172
net_buf_unref(buf);

drivers/bluetooth/hci/hci_spi_st.c

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,6 @@
2929
#include <zephyr/logging/log.h>
3030
LOG_MODULE_REGISTER(bt_driver);
3131

32-
#define HCI_CMD 0x01
33-
#define HCI_ACL 0x02
34-
#define HCI_SCO 0x03
35-
#define HCI_EVT 0x04
36-
#define HCI_ISO 0x05
3732
/* ST Proprietary extended event */
3833
#define HCI_EXT_EVT 0x82
3934

@@ -378,10 +373,10 @@ static int bt_spi_rx_buf_construct(uint8_t *msg, struct net_buf **bufp, uint16_t
378373
}
379374
/* Use memmove instead of memcpy due to buffer overlapping */
380375
memmove(msg + (1 + sizeof(*evt2)), msg + (1 + sizeof(*evt)), evt2->len);
381-
/* Manage event as regular HCI_EVT */
376+
/* Manage event as regular BT_HCI_H4_EVT */
382377
__fallthrough;
383378
#endif /* DT_HAS_COMPAT_STATUS_OKAY(st_hci_spi_v2) */
384-
case HCI_EVT:
379+
case BT_HCI_H4_EVT:
385380
switch (msg[EVT_HEADER_EVENT]) {
386381
case BT_HCI_EVT_VENDOR:
387382
/* Run event through interface handler */
@@ -419,7 +414,7 @@ static int bt_spi_rx_buf_construct(uint8_t *msg, struct net_buf **bufp, uint16_t
419414
}
420415
#endif /* DT_HAS_COMPAT_STATUS_OKAY(st_hci_spi_v1) */
421416
break;
422-
case HCI_ACL:
417+
case BT_HCI_H4_ACL:
423418
buf = bt_buf_get_rx(BT_BUF_ACL_IN, K_FOREVER);
424419
memcpy(&acl_hdr, &msg[1], sizeof(acl_hdr));
425420
len = sizeof(acl_hdr) + sys_le16_to_cpu(acl_hdr.len);
@@ -431,7 +426,7 @@ static int bt_spi_rx_buf_construct(uint8_t *msg, struct net_buf **bufp, uint16_t
431426
net_buf_add_mem(buf, &msg[1], len);
432427
break;
433428
#if defined(CONFIG_BT_ISO)
434-
case HCI_ISO:
429+
case BT_HCI_H4_ISO:
435430
struct bt_hci_iso_hdr iso_hdr;
436431

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

530525
switch (bt_buf_get_type(buf)) {
531526
case BT_BUF_ACL_OUT:
532-
net_buf_push_u8(buf, HCI_ACL);
527+
net_buf_push_u8(buf, BT_HCI_H4_ACL);
533528
break;
534529
case BT_BUF_CMD:
535-
net_buf_push_u8(buf, HCI_CMD);
530+
net_buf_push_u8(buf, BT_HCI_H4_CMD);
536531
break;
537532
#if defined(CONFIG_BT_ISO)
538533
case BT_BUF_ISO_OUT:
539-
net_buf_push_u8(buf, HCI_ISO);
534+
net_buf_push_u8(buf, BT_HCI_H4_ISO);
540535
break;
541536
#endif /* CONFIG_BT_ISO */
542537
default:

0 commit comments

Comments
 (0)