Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 0 additions & 11 deletions include/zephyr/bluetooth/buf.h
Original file line number Diff line number Diff line change
Expand Up @@ -129,17 +129,6 @@ struct net_buf *bt_buf_get_rx(enum bt_buf_type type, k_timeout_t timeout);
struct net_buf *bt_buf_get_tx(enum bt_buf_type type, k_timeout_t timeout,
const void *data, size_t size);

/** Allocate a buffer for an HCI Command Complete/Status Event
*
* This will set the buffer type so bt_buf_set_type() does not need to
* be explicitly called before bt_recv_prio().
*
* @param timeout Non-negative waiting period to obtain a buffer or one of the
* special values K_NO_WAIT and K_FOREVER.
* @return A new buffer.
*/
struct net_buf *bt_buf_get_cmd_complete(k_timeout_t timeout);

/** Allocate a buffer for an HCI Event
*
* This will set the buffer type so bt_buf_set_type() does not need to
Expand Down
51 changes: 12 additions & 39 deletions subsys/bluetooth/host/buf.c
Original file line number Diff line number Diff line change
Expand Up @@ -86,58 +86,31 @@ struct net_buf *bt_buf_get_rx(enum bt_buf_type type, k_timeout_t timeout)
return buf;
}

struct net_buf *bt_buf_get_cmd_complete(k_timeout_t timeout)
{
struct net_buf *buf;

buf = (struct net_buf *)atomic_ptr_clear((atomic_ptr_t *)&bt_dev.sent_cmd);
if (buf) {
bt_buf_set_type(buf, BT_BUF_EVT);
buf->len = 0U;
net_buf_reserve(buf, BT_BUF_RESERVE);

return buf;
}

return bt_buf_get_rx(BT_BUF_EVT, timeout);
}

struct net_buf *bt_buf_get_evt(uint8_t evt, bool discardable,
k_timeout_t timeout)
{
struct net_buf *buf;

switch (evt) {
#if defined(CONFIG_BT_CONN) || defined(CONFIG_BT_ISO)
case BT_HCI_EVT_NUM_COMPLETED_PACKETS:
{
struct net_buf *buf;

buf = net_buf_alloc(&num_complete_pool, timeout);
if (buf) {
net_buf_reserve(buf, BT_BUF_RESERVE);
bt_buf_set_type(buf, BT_BUF_EVT);
}

return buf;
}
buf = net_buf_alloc(&num_complete_pool, timeout);
break;
#endif /* CONFIG_BT_CONN || CONFIG_BT_ISO */
case BT_HCI_EVT_CMD_COMPLETE:
case BT_HCI_EVT_CMD_STATUS:
return bt_buf_get_cmd_complete(timeout);
default:
if (discardable) {
struct net_buf *buf;

buf = net_buf_alloc(&discardable_pool, timeout);
if (buf) {
net_buf_reserve(buf, BT_BUF_RESERVE);
bt_buf_set_type(buf, BT_BUF_EVT);
}

return buf;
} else {
return bt_buf_get_rx(BT_BUF_EVT, timeout);
}
}

return bt_buf_get_rx(BT_BUF_EVT, timeout);
if (buf) {
net_buf_reserve(buf, BT_BUF_RESERVE);
bt_buf_set_type(buf, BT_BUF_EVT);
}

return buf;
}

#ifdef ZTEST_UNITTEST
Expand Down
46 changes: 35 additions & 11 deletions subsys/bluetooth/host/hci_core.c
Original file line number Diff line number Diff line change
Expand Up @@ -2284,25 +2284,44 @@ static void hci_reset_complete(struct net_buf *buf)
atomic_set(bt_dev.flags, flags);
}

static void hci_cmd_done(uint16_t opcode, uint8_t status, struct net_buf *buf)
static void hci_cmd_done(uint16_t opcode, uint8_t status, struct net_buf *evt_buf)
{
LOG_DBG("opcode 0x%04x status 0x%02x buf %p", opcode, status, buf);
/* Original command buffer. */
struct net_buf *buf = NULL;

if (net_buf_pool_get(buf->pool_id) != &hci_cmd_pool) {
LOG_WRN("opcode 0x%04x pool id %u pool %p != &hci_cmd_pool %p", opcode,
buf->pool_id, net_buf_pool_get(buf->pool_id), &hci_cmd_pool);
return;
LOG_DBG("opcode 0x%04x status 0x%02x buf %p", opcode, status, evt_buf);

/* Unsolicited cmd complete. This does not complete a command.
* The controller can send these for effect of the `ncmd` field.
*/
if (opcode == 0) {
goto exit;
}

/* Take the original command buffer reference. */
buf = atomic_ptr_clear((atomic_ptr_t *)&bt_dev.sent_cmd);

if (!buf) {
LOG_ERR("No command sent for cmd complete 0x%04x", opcode);
goto exit;
}

if (cmd(buf)->opcode != opcode) {
LOG_WRN("OpCode 0x%04x completed instead of expected 0x%04x", opcode,
LOG_ERR("OpCode 0x%04x completed instead of expected 0x%04x", opcode,
cmd(buf)->opcode);
return;
buf = atomic_ptr_set((atomic_ptr_t *)&bt_dev.sent_cmd, buf);
__ASSERT_NO_MSG(!buf);
goto exit;
}

if (bt_dev.sent_cmd) {
net_buf_unref(bt_dev.sent_cmd);
bt_dev.sent_cmd = NULL;
/* Response data is to be delivered in the original command
* buffer.
*/
if (evt_buf != buf) {
net_buf_reset(buf);
bt_buf_set_type(buf, BT_BUF_EVT);
net_buf_reserve(buf, BT_BUF_RESERVE);
net_buf_add_mem(buf, evt_buf->data, evt_buf->len);
}

if (cmd(buf)->state && !status) {
Expand All @@ -2316,6 +2335,11 @@ static void hci_cmd_done(uint16_t opcode, uint8_t status, struct net_buf *buf)
cmd(buf)->status = status;
k_sem_give(cmd(buf)->sync);
}

exit:
if (buf) {
net_buf_unref(buf);
}
}

static void hci_cmd_complete(struct net_buf *buf)
Expand Down
5 changes: 0 additions & 5 deletions subsys/bluetooth/host/hci_raw.c
Original file line number Diff line number Diff line change
Expand Up @@ -177,11 +177,6 @@ struct net_buf *bt_buf_get_tx(enum bt_buf_type type, k_timeout_t timeout,
return buf;
}

struct net_buf *bt_buf_get_cmd_complete(k_timeout_t timeout)
{
return bt_buf_get_rx(BT_BUF_EVT, timeout);
}

struct net_buf *bt_buf_get_evt(uint8_t evt, bool discardable, k_timeout_t timeout)
{
return bt_buf_get_rx(BT_BUF_EVT, timeout);
Expand Down
16 changes: 0 additions & 16 deletions tests/bluetooth/host/buf/bt_buf_get_cmd_complete/CMakeLists.txt

This file was deleted.

5 changes: 0 additions & 5 deletions tests/bluetooth/host/buf/bt_buf_get_cmd_complete/prj.conf

This file was deleted.

164 changes: 0 additions & 164 deletions tests/bluetooth/host/buf/bt_buf_get_cmd_complete/src/main.c

This file was deleted.

21 changes: 0 additions & 21 deletions tests/bluetooth/host/buf/bt_buf_get_cmd_complete/testcase.yaml

This file was deleted.

Loading