From 379d588b0c348b516b334261f7ecdad2cb4a682b Mon Sep 17 00:00:00 2001 From: Aleksander Wasaznik Date: Wed, 24 Jan 2024 17:02:03 +0100 Subject: [PATCH 1/4] Bluetooth: Host: Remove overspecifying test for `bt_buf_get_evt` The next commit will change the behavior of `bt_buf_get_evt` to no longer use `bt_buf_get_cmd_complete`. We have to remove the test that would prevent this. Signed-off-by: Aleksander Wasaznik --- .../src/test_suite_hci_evt_cmd.c | 114 ------------------ 1 file changed, 114 deletions(-) diff --git a/tests/bluetooth/host/buf/bt_buf_get_evt/src/test_suite_hci_evt_cmd.c b/tests/bluetooth/host/buf/bt_buf_get_evt/src/test_suite_hci_evt_cmd.c index 8c4c1ca0b5eb8..2c4d2fed7aaa4 100644 --- a/tests/bluetooth/host/buf/bt_buf_get_evt/src/test_suite_hci_evt_cmd.c +++ b/tests/bluetooth/host/buf/bt_buf_get_evt/src/test_suite_hci_evt_cmd.c @@ -39,117 +39,3 @@ static struct net_buf_pool *get_memory_pool(void) return memory_pool; } - -ZTEST_SUITE(bt_buf_get_evt_cmd_type_returns_null, NULL, NULL, NULL, NULL, NULL); -ZTEST_SUITE(bt_buf_get_evt_cmd_type_returns_not_null, NULL, NULL, NULL, NULL, NULL); - -/* - * Return value from bt_buf_get_evt() should match the value returned - * from bt_buf_get_cmd_complete() which is NULL - * - * Constraints: - * - Event type 'BT_HCI_EVT_CMD_COMPLETE' or 'BT_HCI_EVT_CMD_STATUS' - * -'discardable' flag value doesn't care - * - bt_buf_get_cmd_complete() returns NULL - * - Timeout value is a positive non-zero value - * - * Expected behaviour: - * - net_buf_alloc() to be called with the correct memory allocation pool - * and the same timeout value passed to bt_buf_get_evt() - * - bt_buf_get_evt() returns NULL - */ -ZTEST(bt_buf_get_evt_cmd_type_returns_null, test_return_value_matches_bt_buf_get_cmd_complete_null) -{ - struct net_buf *returned_buf; - k_timeout_t timeout = Z_TIMEOUT_TICKS(1000); - struct testing_params const *params_vector; - uint8_t evt; - bool discardable; - - for (size_t i = 0; i < (ARRAY_SIZE(testing_params_lut)); i++) { - - /* Register resets */ - NET_BUF_FFF_FAKES_LIST(RESET_FAKE); - - params_vector = &testing_params_lut[i]; - evt = params_vector->evt; - discardable = params_vector->discardable; - - zassert_true((evt == BT_HCI_EVT_CMD_COMPLETE || evt == BT_HCI_EVT_CMD_STATUS), - "Invalid event type %u to this test", evt); - - bt_dev.sent_cmd = NULL; - net_buf_alloc_fixed_fake.return_val = NULL; - - returned_buf = bt_buf_get_evt(evt, discardable, timeout); - - expect_single_call_net_buf_alloc(get_memory_pool(), &timeout); - expect_not_called_net_buf_reserve(); - expect_not_called_net_buf_ref(); - - zassert_is_null(returned_buf, - "bt_buf_get_evt() returned non-NULL value while expecting NULL"); - } -} - -/* - * Return value from bt_buf_get_evt() should match the value returned - * from bt_buf_get_cmd_complete() which isn't NULL - * - * Constraints: - * - Event type 'BT_HCI_EVT_CMD_COMPLETE' or 'BT_HCI_EVT_CMD_STATUS' - * -'discardable' flag value doesn't care - * - bt_buf_get_cmd_complete() returns a valid reference - * - Timeout value is a positive non-zero value - * - * Expected behaviour: - * - net_buf_alloc() to be called with the correct memory allocation pool - * and the same timeout value passed to bt_buf_get_evt() - * - bt_buf_get_evt() returns the same reference returned by net_buf_alloc_fixed() - */ -ZTEST(bt_buf_get_evt_cmd_type_returns_not_null, - test_return_value_matches_bt_buf_get_cmd_complete_not_null) -{ - const size_t user_data_size = sizeof(struct bt_buf_data); - uint8_t *expected_buf_data[sizeof(struct net_buf) + user_data_size]; - struct net_buf *expected_buf = (struct net_buf *)expected_buf_data; - struct net_buf *returned_buf; - uint8_t returned_buffer_type; - k_timeout_t timeout = Z_TIMEOUT_TICKS(1000); - struct testing_params const *params_vector; - uint8_t evt; - bool discardable; - - expected_buf->user_data_size = user_data_size; - - for (size_t i = 0; i < (ARRAY_SIZE(testing_params_lut)); i++) { - - /* Register resets */ - NET_BUF_FFF_FAKES_LIST(RESET_FAKE); - - params_vector = &testing_params_lut[i]; - evt = params_vector->evt; - discardable = params_vector->discardable; - - zassert_true((evt == BT_HCI_EVT_CMD_COMPLETE || evt == BT_HCI_EVT_CMD_STATUS), - "Invalid event type %u to this test", evt); - - bt_dev.sent_cmd = NULL; - net_buf_alloc_fixed_fake.return_val = expected_buf; - - returned_buf = bt_buf_get_evt(evt, discardable, timeout); - - expect_single_call_net_buf_alloc(get_memory_pool(), &timeout); - expect_single_call_net_buf_reserve(expected_buf); - expect_not_called_net_buf_ref(); - - zassert_equal(returned_buf, expected_buf, - "bt_buf_get_evt() returned incorrect buffer pointer value"); - - returned_buffer_type = bt_buf_get_type(returned_buf); - zassert_equal( - returned_buffer_type, BT_BUF_EVT, - "bt_buf_get_evt() returned incorrect buffer type %u, expected %u (%s)", - returned_buffer_type, BT_BUF_EVT, STRINGIFY(BT_BUF_EVT)); - } -} From fa51f47160f4ff7218bbb2915364a92df397da14 Mon Sep 17 00:00:00 2001 From: Aleksander Wasaznik Date: Mon, 22 Jan 2024 15:51:24 +0100 Subject: [PATCH 2/4] Bluetooth: Host: Remove use of `bt_buf_get_cmd_complete` `bt_buf_get_cmd_complete` is broken due to https://github.com/zephyrproject-rtos/zephyr/issues/64158, and fixing it would require changing its signature and put even more complexity into the HCI drivers, as it would require the drivers to perform an even deeper peek into the event in order to observe the opcode. Instead of the above, this patch removes the use of `bt_buf_get_cmd_complete` and adds logic to allow the host to accept command complete events in normal event buffers. The above means performing a copy into the destination buffer, which is the original command buffer. This is a small inefficiency for now, but we should strive to redesign the host into a streaming architecture as much as possible and handle events immediately instead of retaining buffers. This fixes https://github.com/zephyrproject-rtos/zephyr/issues/64158: Like all command completed events, the completion event for `BT_HCI_OP_HOST_NUM_COMPLETED_PACKETS` is now placed in normal event buffers. The the logic where the host discards this event is already present. Since it's discarded, it will not interfere with the logic around `bt_dev.cmd_send`. Signed-off-by: Aleksander Wasaznik --- subsys/bluetooth/host/buf.c | 3 --- subsys/bluetooth/host/hci_core.c | 46 ++++++++++++++++++++++++-------- 2 files changed, 35 insertions(+), 14 deletions(-) diff --git a/subsys/bluetooth/host/buf.c b/subsys/bluetooth/host/buf.c index 296d2aa0b5ec2..2623871c502dc 100644 --- a/subsys/bluetooth/host/buf.c +++ b/subsys/bluetooth/host/buf.c @@ -120,9 +120,6 @@ struct net_buf *bt_buf_get_evt(uint8_t evt, bool discardable, return buf; } #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; diff --git a/subsys/bluetooth/host/hci_core.c b/subsys/bluetooth/host/hci_core.c index 2f54298b41be6..b751cb989f14b 100644 --- a/subsys/bluetooth/host/hci_core.c +++ b/subsys/bluetooth/host/hci_core.c @@ -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) { @@ -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) From 25b33c6d0e62832bd5e55ac42c7621e884320c15 Mon Sep 17 00:00:00 2001 From: Aleksander Wasaznik Date: Wed, 24 Jan 2024 17:15:20 +0100 Subject: [PATCH 3/4] Bluetooth: Host: Remove `bt_buf_get_cmd_complete` After the previous commit, this function no longer has any users. Signed-off-by: Aleksander Wasaznik --- include/zephyr/bluetooth/buf.h | 11 -- subsys/bluetooth/host/buf.c | 16 -- subsys/bluetooth/host/hci_raw.c | 5 - .../bt_buf_get_cmd_complete/CMakeLists.txt | 16 -- .../host/buf/bt_buf_get_cmd_complete/prj.conf | 5 - .../buf/bt_buf_get_cmd_complete/src/main.c | 164 ------------------ .../buf/bt_buf_get_cmd_complete/testcase.yaml | 21 --- .../host/buf/mocks/net_buf_expects.h | 1 - 8 files changed, 239 deletions(-) delete mode 100644 tests/bluetooth/host/buf/bt_buf_get_cmd_complete/CMakeLists.txt delete mode 100644 tests/bluetooth/host/buf/bt_buf_get_cmd_complete/prj.conf delete mode 100644 tests/bluetooth/host/buf/bt_buf_get_cmd_complete/src/main.c delete mode 100644 tests/bluetooth/host/buf/bt_buf_get_cmd_complete/testcase.yaml diff --git a/include/zephyr/bluetooth/buf.h b/include/zephyr/bluetooth/buf.h index b6c377a936933..b079695d61c97 100644 --- a/include/zephyr/bluetooth/buf.h +++ b/include/zephyr/bluetooth/buf.h @@ -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 diff --git a/subsys/bluetooth/host/buf.c b/subsys/bluetooth/host/buf.c index 2623871c502dc..b6c325f9b6ffa 100644 --- a/subsys/bluetooth/host/buf.c +++ b/subsys/bluetooth/host/buf.c @@ -86,22 +86,6 @@ 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) { diff --git a/subsys/bluetooth/host/hci_raw.c b/subsys/bluetooth/host/hci_raw.c index e40ae4cbddbfd..16113c8d25a15 100644 --- a/subsys/bluetooth/host/hci_raw.c +++ b/subsys/bluetooth/host/hci_raw.c @@ -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); diff --git a/tests/bluetooth/host/buf/bt_buf_get_cmd_complete/CMakeLists.txt b/tests/bluetooth/host/buf/bt_buf_get_cmd_complete/CMakeLists.txt deleted file mode 100644 index 59a793435988c..0000000000000 --- a/tests/bluetooth/host/buf/bt_buf_get_cmd_complete/CMakeLists.txt +++ /dev/null @@ -1,16 +0,0 @@ -# SPDX-License-Identifier: Apache-2.0 - -cmake_minimum_required(VERSION 3.20.0) - -project(bluetooth_bt_buf_get_cmd_complete) - -find_package(Zephyr COMPONENTS unittest REQUIRED HINTS $ENV{ZEPHYR_BASE}) - -add_subdirectory(${ZEPHYR_BASE}/tests/bluetooth/host host_mocks) -add_subdirectory(${ZEPHYR_BASE}/tests/bluetooth/host/buf mocks) - -target_link_libraries(testbinary PRIVATE mocks host_mocks) -target_sources(testbinary - PRIVATE - src/main.c -) diff --git a/tests/bluetooth/host/buf/bt_buf_get_cmd_complete/prj.conf b/tests/bluetooth/host/buf/bt_buf_get_cmd_complete/prj.conf deleted file mode 100644 index 652e7e5d1690e..0000000000000 --- a/tests/bluetooth/host/buf/bt_buf_get_cmd_complete/prj.conf +++ /dev/null @@ -1,5 +0,0 @@ -CONFIG_ZTEST=y -CONFIG_BT=y -CONFIG_ASSERT=y -CONFIG_ASSERT_LEVEL=2 -CONFIG_ASSERT_VERBOSE=y diff --git a/tests/bluetooth/host/buf/bt_buf_get_cmd_complete/src/main.c b/tests/bluetooth/host/buf/bt_buf_get_cmd_complete/src/main.c deleted file mode 100644 index bd9d82f63162b..0000000000000 --- a/tests/bluetooth/host/buf/bt_buf_get_cmd_complete/src/main.c +++ /dev/null @@ -1,164 +0,0 @@ -/* - * Copyright (c) 2022 Nordic Semiconductor ASA - * - * SPDX-License-Identifier: Apache-2.0 - */ - -#include -#include -#include -#include -#include -#include "mocks/net_buf.h" -#include "mocks/net_buf_expects.h" -#include "mocks/buf_help_utils.h" - -DEFINE_FFF_GLOBALS; - -static void tc_setup(void *f) -{ - /* Register resets */ - NET_BUF_FFF_FAKES_LIST(RESET_FAKE); -} - -ZTEST_SUITE(bt_buf_get_cmd_complete_returns_not_null, NULL, NULL, tc_setup, NULL, NULL); -ZTEST_SUITE(bt_buf_get_cmd_complete_returns_null, NULL, NULL, tc_setup, NULL, NULL); - -/* - * Return value from bt_buf_get_cmd_complete() should be NULL - * - * This is to test the behaviour when memory allocation request fails - * - * Constraints: - * - bt_dev.sent_cmd value is NULL - * - Timeout value is a positive non-zero value - * - net_buf_alloc() returns a NULL value - * - * Expected behaviour: - * - net_buf_alloc() to be called with the correct memory allocation pool - * and the same timeout value passed to bt_buf_get_cmd_complete() - * - bt_dev.sent_cmd value is cleared after calling bt_buf_get_cmd_complete() - * - bt_buf_get_cmd_complete() returns NULL - */ -ZTEST(bt_buf_get_cmd_complete_returns_null, test_returns_null_sent_cmd_is_null) -{ - struct net_buf *returned_buf; - k_timeout_t timeout = Z_TIMEOUT_TICKS(1000); - - bt_dev.sent_cmd = NULL; - - struct net_buf_pool *memory_pool; - - if ((IS_ENABLED(CONFIG_BT_HCI_ACL_FLOW_CONTROL))) { - memory_pool = bt_buf_get_evt_pool(); - } else { - memory_pool = bt_buf_get_hci_rx_pool(); - } - - net_buf_alloc_fixed_fake.return_val = NULL; - - returned_buf = bt_buf_get_cmd_complete(timeout); - - expect_single_call_net_buf_alloc(memory_pool, &timeout); - expect_not_called_net_buf_reserve(); - expect_not_called_net_buf_ref(); - - zassert_is_null(returned_buf, - "bt_buf_get_cmd_complete() returned non-NULL value while expecting NULL"); - - zassert_equal(bt_dev.sent_cmd, NULL, - "bt_buf_get_cmd_complete() didn't clear bt_dev.sent_cmd"); -} - -/* - * Return value from bt_buf_get_cmd_complete() shouldn't be NULL - * - * Constraints: - * - bt_dev.sent_cmd value is NULL - * - Timeout value is a positive non-zero value - * - net_buf_alloc() return a not NULL value - * - * Expected behaviour: - * - net_buf_alloc() to be called with the correct memory allocation pool - * and the same timeout value passed to bt_buf_get_cmd_complete() - * - bt_dev.sent_cmd value is cleared after calling bt_buf_get_cmd_complete() - * - bt_buf_get_cmd_complete() returns the same value returned by net_buf_alloc_fixed() - * - Return buffer type is set to BT_BUF_EVT - */ -ZTEST(bt_buf_get_cmd_complete_returns_not_null, test_returns_not_null_sent_cmd_is_null) -{ - static struct net_buf expected_buf; - struct net_buf *returned_buf; - uint8_t returned_buffer_type; - k_timeout_t timeout = Z_TIMEOUT_TICKS(1000); - - bt_dev.sent_cmd = NULL; - - struct net_buf_pool *memory_pool; - - if ((IS_ENABLED(CONFIG_BT_HCI_ACL_FLOW_CONTROL))) { - memory_pool = bt_buf_get_evt_pool(); - } else { - memory_pool = bt_buf_get_hci_rx_pool(); - } - - net_buf_alloc_fixed_fake.return_val = &expected_buf; - - returned_buf = bt_buf_get_cmd_complete(timeout); - - expect_single_call_net_buf_alloc(memory_pool, &timeout); - expect_single_call_net_buf_reserve(&expected_buf); - expect_not_called_net_buf_ref(); - - zassert_equal(returned_buf, &expected_buf, - "bt_buf_get_cmd_complete() returned incorrect buffer pointer value"); - - returned_buffer_type = bt_buf_get_type(returned_buf); - zassert_equal(returned_buffer_type, BT_BUF_EVT, - "bt_buf_get_cmd_complete() returned incorrect buffer type %u, expected %u (%s)", - returned_buffer_type, BT_BUF_EVT, STRINGIFY(BT_BUF_EVT)); - - zassert_equal(bt_dev.sent_cmd, NULL, - "bt_buf_get_cmd_complete() didn't clear bt_dev.sent_cmd"); -} - -/* - * Return value from bt_buf_get_cmd_complete() shouldn't be NULL - * - * Constraints: - * - bt_dev.sent_cmd value isn't NULL - * - Timeout value is a positive non-zero value - * - * Expected behaviour: - * - net_buf_alloc() isn't called - * - bt_dev.sent_cmd value is cleared after calling bt_buf_get_cmd_complete() - * - bt_buf_get_cmd_complete() returns the same value set to bt_dev.sent_cmd - * - Return buffer type is set to BT_BUF_EVT - */ -ZTEST(bt_buf_get_cmd_complete_returns_not_null, test_returns_not_null_sent_cmd_is_not_null) -{ - static struct net_buf expected_buf; - struct net_buf *returned_buf; - uint8_t returned_buffer_type; - k_timeout_t timeout = Z_TIMEOUT_TICKS(1000); - - bt_dev.sent_cmd = &expected_buf; - - net_buf_ref_fake.return_val = &expected_buf; - - returned_buf = bt_buf_get_cmd_complete(timeout); - - expect_single_call_net_buf_reserve(&expected_buf); - expect_not_called_net_buf_alloc(); - - zassert_equal(returned_buf, &expected_buf, - "bt_buf_get_cmd_complete() returned incorrect buffer pointer value"); - - returned_buffer_type = bt_buf_get_type(returned_buf); - zassert_equal(returned_buffer_type, BT_BUF_EVT, - "bt_buf_get_cmd_complete() returned incorrect buffer type %u, expected %u (%s)", - returned_buffer_type, BT_BUF_EVT, STRINGIFY(BT_BUF_EVT)); - - zassert_equal(bt_dev.sent_cmd, NULL, - "bt_buf_get_cmd_complete() didn't clear bt_dev.sent_cmd"); -} diff --git a/tests/bluetooth/host/buf/bt_buf_get_cmd_complete/testcase.yaml b/tests/bluetooth/host/buf/bt_buf_get_cmd_complete/testcase.yaml deleted file mode 100644 index bf8ab566d6c48..0000000000000 --- a/tests/bluetooth/host/buf/bt_buf_get_cmd_complete/testcase.yaml +++ /dev/null @@ -1,21 +0,0 @@ -common: - tags: - - bluetooth - - host -tests: - bluetooth.host.bt_buf_get_cmd_complete.default: - type: unit - bluetooth.host.bt_buf_get_cmd_complete.hci_acl_flow_control: - type: unit - extra_configs: - - CONFIG_BT_CENTRAL=y - - CONFIG_BT_HCI_ACL_FLOW_CONTROL=y - bluetooth.host.bt_buf_get_cmd_complete.iso_unicast: - type: unit - # enable CONFIG_BT_ISO_UNICAST - extra_configs: - - CONFIG_BT_ISO_CENTRAL=y - bluetooth.host.bt_buf_get_cmd_complete.iso_sync_receiver: - type: unit - extra_configs: - - CONFIG_BT_ISO_SYNC_RECEIVER=y diff --git a/tests/bluetooth/host/buf/mocks/net_buf_expects.h b/tests/bluetooth/host/buf/mocks/net_buf_expects.h index b50662202f384..99e84fd85be4f 100644 --- a/tests/bluetooth/host/buf/mocks/net_buf_expects.h +++ b/tests/bluetooth/host/buf/mocks/net_buf_expects.h @@ -12,7 +12,6 @@ * Expected behaviour: * - net_buf_alloc() to be called once with : * - correct memory allocation pool - * - same timeout value passed to bt_buf_get_cmd_complete() */ void expect_single_call_net_buf_alloc(struct net_buf_pool *pool, k_timeout_t *timeout); From b7d1ea2bc59faa112eb79e8cd9e362aebde7b5d1 Mon Sep 17 00:00:00 2001 From: Aleksander Wasaznik Date: Thu, 25 Jan 2024 11:27:30 +0100 Subject: [PATCH 4/4] Bluetooth: Host: Refactor `bt_buf_get_evt` This is purely a syntactical refactor. Signed-off-by: Aleksander Wasaznik --- subsys/bluetooth/host/buf.c | 32 ++++++++++++-------------------- 1 file changed, 12 insertions(+), 20 deletions(-) diff --git a/subsys/bluetooth/host/buf.c b/subsys/bluetooth/host/buf.c index b6c325f9b6ffa..9d87a6390e2cb 100644 --- a/subsys/bluetooth/host/buf.c +++ b/subsys/bluetooth/host/buf.c @@ -89,36 +89,28 @@ struct net_buf *bt_buf_get_rx(enum bt_buf_type type, k_timeout_t 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 */ 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