diff --git a/drivers/bluetooth/hci/h4.c b/drivers/bluetooth/hci/h4.c index 4edd011b51d..859858e5bf1 100644 --- a/drivers/bluetooth/hci/h4.c +++ b/drivers/bluetooth/hci/h4.c @@ -167,7 +167,11 @@ static struct net_buf *get_rx(int timeout) return bt_buf_get_cmd_complete(timeout); } - return bt_buf_get_rx(timeout); + if (rx.type == H4_ACL) { + return bt_buf_get_rx(BT_BUF_ACL_IN, timeout); + } else { + return bt_buf_get_rx(BT_BUF_EVT, timeout); + } } static void rx_thread(void *p1, void *p2, void *p3) diff --git a/drivers/bluetooth/hci/h5.c b/drivers/bluetooth/hci/h5.c index 41c485825fe..4b8ee250045 100644 --- a/drivers/bluetooth/hci/h5.c +++ b/drivers/bluetooth/hci/h5.c @@ -412,12 +412,11 @@ static inline struct net_buf *get_evt_buf(u8_t evt) buf = bt_buf_get_cmd_complete(K_NO_WAIT); break; default: - buf = bt_buf_get_rx(K_NO_WAIT); + buf = bt_buf_get_rx(BT_BUF_EVT, K_NO_WAIT); break; } if (buf) { - bt_buf_set_type(h5.rx_buf, BT_BUF_EVT); net_buf_add_u8(h5.rx_buf, evt); } @@ -489,14 +488,14 @@ static void bt_uart_isr(struct device *unused) h5.rx_state = PAYLOAD; break; case HCI_ACLDATA_PKT: - h5.rx_buf = bt_buf_get_rx(K_NO_WAIT); + h5.rx_buf = bt_buf_get_rx(BT_BUF_ACL_IN, + K_NO_WAIT); if (!h5.rx_buf) { BT_WARN("No available data buffers"); h5_reset_rx(); continue; } - bt_buf_set_type(h5.rx_buf, BT_BUF_ACL_IN); h5.rx_state = PAYLOAD; break; case HCI_3WIRE_LINK_PKT: diff --git a/drivers/bluetooth/hci/spi.c b/drivers/bluetooth/hci/spi.c index ad49cb87fdd..85e43e768f5 100644 --- a/drivers/bluetooth/hci/spi.c +++ b/drivers/bluetooth/hci/spi.c @@ -181,17 +181,15 @@ static void bt_spi_rx_thread(void) buf = bt_buf_get_cmd_complete(K_FOREVER); break; default: - buf = bt_buf_get_rx(K_FOREVER); + buf = bt_buf_get_rx(BT_BUF_EVT, K_FOREVER); break; } - bt_buf_set_type(buf, BT_BUF_EVT); net_buf_add_mem(buf, &rxmsg[1], rxmsg[EVT_HEADER_SIZE] + 2); break; case HCI_ACL: - buf = bt_buf_get_rx(K_FOREVER); - bt_buf_set_type(buf, BT_BUF_ACL_IN); + buf = bt_buf_get_rx(BT_BUF_ACL_IN, K_FOREVER); memcpy(&acl_hdr, &rxmsg[1], sizeof(acl_hdr)); net_buf_add_mem(buf, &acl_hdr, sizeof(acl_hdr)); net_buf_add_mem(buf, &rxmsg[5], diff --git a/include/bluetooth/buf.h b/include/bluetooth/buf.h index c37e04c5228..b049eaeb134 100644 --- a/include/bluetooth/buf.h +++ b/include/bluetooth/buf.h @@ -43,14 +43,16 @@ enum bt_buf_type { /** Allocate a buffer for incoming data * - * This will not set the buffer type so bt_buf_set_type() needs to be called - * before bt_recv(). + * This will set the buffer type so bt_buf_set_type() does not need to + * be explicitly called before bt_recv_prio(). * + * @param type Type of buffer. Only BT_BUF_EVT and BT_BUF_ACL_IN are + * allowed. * @param timeout Timeout in milliseconds, or one of the special values * K_NO_WAIT and K_FOREVER. * @return A new buffer. */ -struct net_buf *bt_buf_get_rx(s32_t timeout); +struct net_buf *bt_buf_get_rx(enum bt_buf_type type, s32_t timeout); /** Allocate a buffer for an HCI Command Complete/Status Event * diff --git a/subsys/bluetooth/controller/hci/hci_driver.c b/subsys/bluetooth/controller/hci/hci_driver.c index 58224cabb14..42423af219a 100644 --- a/subsys/bluetooth/controller/hci/hci_driver.c +++ b/subsys/bluetooth/controller/hci/hci_driver.c @@ -72,8 +72,7 @@ static void prio_recv_thread(void *p1, void *p2, void *p3) #if defined(CONFIG_BLUETOOTH_CONN) struct net_buf *buf; - buf = bt_buf_get_rx(K_FOREVER); - bt_buf_set_type(buf, BT_BUF_EVT); + buf = bt_buf_get_rx(BT_BUF_EVT, K_FOREVER); hci_num_cmplt_encode(buf, handle, num_cmplt); BT_DBG("Num Complete: 0x%04x:%u", handle, num_cmplt); bt_recv_prio(buf); @@ -117,20 +116,18 @@ static inline struct net_buf *encode_node(struct radio_pdu_node_rx *node_rx, case HCI_CLASS_EVT_REQUIRED: case HCI_CLASS_EVT_CONNECTION: if (class == HCI_CLASS_EVT_DISCARDABLE) { - buf = bt_buf_get_rx(K_NO_WAIT); + buf = bt_buf_get_rx(BT_BUF_EVT, K_NO_WAIT); } else { - buf = bt_buf_get_rx(K_FOREVER); + buf = bt_buf_get_rx(BT_BUF_EVT, K_FOREVER); } if (buf) { - bt_buf_set_type(buf, BT_BUF_EVT); hci_evt_encode(node_rx, buf); } break; #if defined(CONFIG_BLUETOOTH_CONN) case HCI_CLASS_ACL_DATA: /* generate ACL data */ - buf = bt_buf_get_rx(K_FOREVER); - bt_buf_set_type(buf, BT_BUF_ACL_IN); + buf = bt_buf_get_rx(BT_BUF_ACL_IN, K_FOREVER); hci_acl_encode(node_rx, buf); break; #endif diff --git a/subsys/bluetooth/host/hci_core.c b/subsys/bluetooth/host/hci_core.c index fe0757afa7a..bc11afdbe73 100644 --- a/subsys/bluetooth/host/hci_core.c +++ b/subsys/bluetooth/host/hci_core.c @@ -4101,13 +4101,17 @@ int bt_le_scan_stop(void) return bt_le_scan_update(false); } -struct net_buf *bt_buf_get_rx(s32_t timeout) +struct net_buf *bt_buf_get_rx(enum bt_buf_type type, s32_t timeout) { struct net_buf *buf; + __ASSERT(type == BT_BUF_EVT || type == BT_BUF_ACL_IN, + "Invalid buffer type requested"); + buf = net_buf_alloc(&hci_rx_pool, timeout); if (buf) { net_buf_reserve(buf, CONFIG_BLUETOOTH_HCI_RESERVE); + bt_buf_set_type(buf, type); } return buf; @@ -4133,12 +4137,7 @@ struct net_buf *bt_buf_get_cmd_complete(s32_t timeout) return buf; } - buf = bt_buf_get_rx(timeout); - if (buf) { - bt_buf_set_type(buf, BT_BUF_EVT); - } - - return buf; + return bt_buf_get_rx(BT_BUF_EVT, timeout); } #if defined(CONFIG_BLUETOOTH_BREDR) diff --git a/subsys/bluetooth/host/hci_ecc.c b/subsys/bluetooth/host/hci_ecc.c index bdff65f7642..0a80b65ab45 100644 --- a/subsys/bluetooth/host/hci_ecc.c +++ b/subsys/bluetooth/host/hci_ecc.c @@ -137,8 +137,7 @@ static void emulate_le_p256_public_key_cmd(void) status = generate_keys(); - buf = bt_buf_get_rx(K_FOREVER); - bt_buf_set_type(buf, BT_BUF_EVT); + buf = bt_buf_get_rx(BT_BUF_EVT, K_FOREVER); hdr = net_buf_add(buf, sizeof(*hdr)); hdr->evt = BT_HCI_EVT_LE_META_EVENT; @@ -176,8 +175,7 @@ static void emulate_le_generate_dhkey(void) ret = ecdh_shared_secret(ecc.dhkey, &ecc.pk, ecc.private_key); } - buf = bt_buf_get_rx(K_FOREVER); - bt_buf_set_type(buf, BT_BUF_EVT); + buf = bt_buf_get_rx(BT_BUF_EVT, K_FOREVER); hdr = net_buf_add(buf, sizeof(*hdr)); hdr->evt = BT_HCI_EVT_LE_META_EVENT; diff --git a/subsys/bluetooth/host/hci_raw.c b/subsys/bluetooth/host/hci_raw.c index 2d9257caceb..c9a4906e72f 100644 --- a/subsys/bluetooth/host/hci_raw.c +++ b/subsys/bluetooth/host/hci_raw.c @@ -45,7 +45,7 @@ int bt_hci_driver_register(const struct bt_hci_driver *drv) return 0; } -struct net_buf *bt_buf_get_rx(s32_t timeout) +struct net_buf *bt_buf_get_rx(enum bt_buf_type type, s32_t timeout) { return net_buf_alloc(&hci_rx_pool, timeout); }