From 52dc3b6f96b597f1560fa930db335ba7a018166a Mon Sep 17 00:00:00 2001 From: Yago Fontoura do Rosario Date: Wed, 8 Jan 2025 15:05:44 +0100 Subject: [PATCH 01/49] [nrf fromtree] Bluetooth: Host: Ensure conn_ready access is thread safe * The `bt_dev.le.conn_ready` list is accessed by the tx_processor which runs in a workqueue, but `bt_conn_data_ready` can be called from different threads so we need to make sure that nothing will trigger a context switch while we are manipulating the list since sys_slist_*() functions are not thread safe. * This only happens if call to `bt_conn_data_ready` is performed from a preemptive task which can happen depending on the application. Signed-off-by: Yago Fontoura do Rosario (cherry picked from commit 9f7736259fe3fa12aa50c6c72d9689e1af337839) Signed-off-by: alperen sener --- subsys/bluetooth/host/conn.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/subsys/bluetooth/host/conn.c b/subsys/bluetooth/host/conn.c index 740b82d8cc32..823de6052656 100644 --- a/subsys/bluetooth/host/conn.c +++ b/subsys/bluetooth/host/conn.c @@ -864,10 +864,18 @@ void bt_conn_data_ready(struct bt_conn *conn) * * This reference will be consumed when the conn is popped off * the list (in `get_conn_ready`). + * + * The `bt_dev.le.conn_ready` list is accessed by the tx_processor + * which runs in a workqueue, but `bt_conn_data_ready` can be called + * from different threads so we need to make sure that nothing will + * trigger a thread switch while we are manipulating the list since + * sys_slist_*() functions are not thread safe. */ bt_conn_ref(conn); + k_sched_lock(); sys_slist_append(&bt_dev.le.conn_ready, &conn->_conn_ready); + k_sched_unlock(); LOG_DBG("raised"); } else { LOG_DBG("already in list"); From 91d93bbf76fceb9e7d95057a11107c6374f38fb5 Mon Sep 17 00:00:00 2001 From: Yago Fontoura do Rosario Date: Mon, 13 Jan 2025 16:30:51 +0100 Subject: [PATCH 02/49] [nrf fromtree] tests: bluetooth: Add missing mocks after fix on host * Two mocks were missing in kernel mocks Signed-off-by: Yago Fontoura do Rosario (cherry picked from commit ee326138cad076e454f6d23a5f52d0058925be6a) Signed-off-by: alperen sener --- tests/bluetooth/host/conn/mocks/kernel.c | 2 ++ tests/bluetooth/host/conn/mocks/kernel.h | 6 +++++- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/tests/bluetooth/host/conn/mocks/kernel.c b/tests/bluetooth/host/conn/mocks/kernel.c index ef56b52ec883..5f12f5b246ce 100644 --- a/tests/bluetooth/host/conn/mocks/kernel.c +++ b/tests/bluetooth/host/conn/mocks/kernel.c @@ -29,5 +29,7 @@ DEFINE_FAKE_VALUE_FUNC(void *, k_queue_get, struct k_queue *, k_timeout_t); DEFINE_FAKE_VOID_FUNC(k_queue_prepend, struct k_queue *, void *); DEFINE_FAKE_VALUE_FUNC(void *, k_heap_alloc, struct k_heap *, size_t, k_timeout_t); DEFINE_FAKE_VOID_FUNC(k_heap_free, struct k_heap *, void *); +DEFINE_FAKE_VOID_FUNC(k_sched_lock); +DEFINE_FAKE_VOID_FUNC(k_sched_unlock); struct k_work_q k_sys_work_q; diff --git a/tests/bluetooth/host/conn/mocks/kernel.h b/tests/bluetooth/host/conn/mocks/kernel.h index d649c527d28f..ee6edca355d1 100644 --- a/tests/bluetooth/host/conn/mocks/kernel.h +++ b/tests/bluetooth/host/conn/mocks/kernel.h @@ -29,7 +29,9 @@ FAKE(k_queue_get) \ FAKE(k_queue_prepend) \ FAKE(k_heap_alloc) \ - FAKE(k_heap_free) + FAKE(k_heap_free) \ + FAKE(k_sched_lock) \ + FAKE(k_sched_unlock) \ DECLARE_FAKE_VALUE_FUNC(bool, k_is_in_isr); DECLARE_FAKE_VALUE_FUNC(int, k_poll_signal_raise, struct k_poll_signal *, int); @@ -52,3 +54,5 @@ DECLARE_FAKE_VALUE_FUNC(void *, k_queue_get, struct k_queue *, k_timeout_t); DECLARE_FAKE_VOID_FUNC(k_queue_prepend, struct k_queue *, void *); DECLARE_FAKE_VALUE_FUNC(void *, k_heap_alloc, struct k_heap *, size_t, k_timeout_t); DECLARE_FAKE_VOID_FUNC(k_heap_free, struct k_heap *, void *); +DECLARE_FAKE_VOID_FUNC(k_sched_lock); +DECLARE_FAKE_VOID_FUNC(k_sched_unlock); From 1d7a4cac6122111f126c937ada5a878638b167b9 Mon Sep 17 00:00:00 2001 From: alperen sener Date: Fri, 13 Dec 2024 17:18:12 +0100 Subject: [PATCH 03/49] [nrf fromtree] bluetooth: mesh: fix mesh pb gatt cli uuid usage uuid needs to be keept in pb_gatt_cli server context until the client is connected to server, otherwise, since we only kept the pointer from the net_buffer, any incoming unprovisioned beacon before the connection is established may overwrite uuid. Signed-off-by: alperen sener (cherry picked from commit 37cdfe818b8bcae33d024d1c0e777b57d339913e) Signed-off-by: alperen sener --- subsys/bluetooth/mesh/pb_gatt_cli.c | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/subsys/bluetooth/mesh/pb_gatt_cli.c b/subsys/bluetooth/mesh/pb_gatt_cli.c index 07d77483cafd..9138cf55f77d 100644 --- a/subsys/bluetooth/mesh/pb_gatt_cli.c +++ b/subsys/bluetooth/mesh/pb_gatt_cli.c @@ -33,7 +33,8 @@ LOG_MODULE_REGISTER(bt_mesh_pb_gatt_client); static struct { - const uint8_t *target; + bool target_set; + uint8_t target_uuid[16]; struct bt_mesh_proxy_role *srv; } server; @@ -55,7 +56,7 @@ static void pb_gatt_connected(struct bt_conn *conn, void *user_data) server.srv = bt_mesh_proxy_role_setup(conn, bt_mesh_gatt_send, pb_gatt_msg_recv); - server.target = NULL; + server.target_set = false; bt_mesh_pb_gatt_cli_start(conn); } @@ -91,8 +92,8 @@ int bt_mesh_pb_gatt_cli_setup(const uint8_t uuid[16]) return -EBUSY; } - server.target = uuid; - + memcpy(server.target_uuid, uuid, 16); + server.target_set = true; return 0; } @@ -112,8 +113,8 @@ void bt_mesh_pb_gatt_cli_adv_recv(const struct bt_le_scan_recv_info *info, uuid = net_buf_simple_pull_mem(buf, 16); - if (server.target && - !memcmp(server.target, uuid, 16)) { + if (server.target_set && + !memcmp(server.target_uuid, uuid, 16)) { (void)bt_mesh_gatt_cli_connect(info->addr, &pbgatt, NULL); return; } From b395df827ee77641b2df2cfa6d9b915c3466c13e Mon Sep 17 00:00:00 2001 From: alperen sener Date: Thu, 12 Dec 2024 00:19:47 +0100 Subject: [PATCH 04/49] [nrf fromtree] tests: bluetooth: mesh: Add PB-GATT provisioning test cases Extending existing provisioning test code to use PB-GATT and adding test cases for provisioning over PB-GATT. Test names are changed to represent both provisioning brearers. Enabled required kconfigs for PB-GATT and GATT proxy features in overlay_gatt.conf. Test argument prov-bearer is added to provisioning tests to select bearer. Following tests are renamed and extended to use PB-GATT - pb_adv_multi - pb_adv_no_oob - pb_adv_oob_auth_ib_pk - pb_adv_oob_auth_ignore_oob_pk - pb_adv_oob_auth_oob_pk - pb_adv_reprovision PB-GATT test cases will run only with PSA encryption since tinycrypt is to be deprecated. Signed-off-by: alperen sener (cherry picked from commit 917683a46b3a66b7f4af9f2e5b39b49f0163b281) Signed-off-by: alperen sener --- tests/bsim/bluetooth/mesh/CMakeLists.txt | 1 + tests/bsim/bluetooth/mesh/overlay_gatt.conf | 7 +- tests/bsim/bluetooth/mesh/src/main.c | 3 +- .../bsim/bluetooth/mesh/src/test_provision.c | 146 +++++++++++------- .../tests_scripts/provision/pb_adv_multi.sh | 21 --- .../tests_scripts/provision/pb_adv_no_oob.sh | 14 -- .../provision/pb_adv_oob_auth_oob_pk.sh | 13 -- .../provision/pb_adv_reprovision.sh | 14 -- .../provision/pb_remote_parallel.sh | 10 +- .../provision/pb_remote_reprovision.sh | 6 +- .../provision/pb_remote_timeout.sh | 4 +- .../tests_scripts/provision/prov_multi.sh | 31 ++++ .../tests_scripts/provision/prov_no_oob.sh | 22 +++ ...b_auth_ib_pk.sh => prov_oob_auth_ib_pk.sh} | 11 +- ...b_pk.sh => prov_oob_auth_ignore_oob_pk.sh} | 11 +- .../provision/prov_oob_auth_oob_pk.sh | 20 +++ .../provision/prov_reprovision.sh | 22 +++ 17 files changed, 220 insertions(+), 136 deletions(-) delete mode 100755 tests/bsim/bluetooth/mesh/tests_scripts/provision/pb_adv_multi.sh delete mode 100755 tests/bsim/bluetooth/mesh/tests_scripts/provision/pb_adv_no_oob.sh delete mode 100755 tests/bsim/bluetooth/mesh/tests_scripts/provision/pb_adv_oob_auth_oob_pk.sh delete mode 100755 tests/bsim/bluetooth/mesh/tests_scripts/provision/pb_adv_reprovision.sh create mode 100755 tests/bsim/bluetooth/mesh/tests_scripts/provision/prov_multi.sh create mode 100755 tests/bsim/bluetooth/mesh/tests_scripts/provision/prov_no_oob.sh rename tests/bsim/bluetooth/mesh/tests_scripts/provision/{pb_adv_oob_auth_ib_pk.sh => prov_oob_auth_ib_pk.sh} (50%) rename tests/bsim/bluetooth/mesh/tests_scripts/provision/{pb_adv_oob_auth_ignore_oob_pk.sh => prov_oob_auth_ignore_oob_pk.sh} (50%) create mode 100755 tests/bsim/bluetooth/mesh/tests_scripts/provision/prov_oob_auth_oob_pk.sh create mode 100755 tests/bsim/bluetooth/mesh/tests_scripts/provision/prov_reprovision.sh diff --git a/tests/bsim/bluetooth/mesh/CMakeLists.txt b/tests/bsim/bluetooth/mesh/CMakeLists.txt index c907e8c036be..9897e5074a23 100644 --- a/tests/bsim/bluetooth/mesh/CMakeLists.txt +++ b/tests/bsim/bluetooth/mesh/CMakeLists.txt @@ -45,6 +45,7 @@ elseif(CONFIG_BT_MESH_GATT_PROXY) src/test_advertiser.c src/test_suspend.c src/test_beacon.c + src/test_provision.c ) elseif(CONFIG_BT_CTLR_LOW_LAT) diff --git a/tests/bsim/bluetooth/mesh/overlay_gatt.conf b/tests/bsim/bluetooth/mesh/overlay_gatt.conf index a8ce71afaef1..718e0eae4b40 100644 --- a/tests/bsim/bluetooth/mesh/overlay_gatt.conf +++ b/tests/bsim/bluetooth/mesh/overlay_gatt.conf @@ -1,7 +1,8 @@ +CONFIG_BT_CENTRAL=y CONFIG_BT_PERIPHERAL=y -CONFIG_BT_MESH_GATT_PROXY=y -CONFIG_BT_MESH_PB_GATT=y -CONFIG_BT_CENTRAL=y +CONFIG_BT_MESH_PB_GATT=y +CONFIG_BT_MESH_PB_GATT_CLIENT=y +CONFIG_BT_MESH_GATT_PROXY=y CONFIG_BT_MESH_PROXY_CLIENT=y CONFIG_BT_MESH_PROXY_SOLICITATION=y diff --git a/tests/bsim/bluetooth/mesh/src/main.c b/tests/bsim/bluetooth/mesh/src/main.c index bae3146fb210..7ce3a990d462 100644 --- a/tests/bsim/bluetooth/mesh/src/main.c +++ b/tests/bsim/bluetooth/mesh/src/main.c @@ -23,6 +23,7 @@ extern struct bst_test_list *test_proxy_sol_install(struct bst_test_list *test); extern struct bst_test_list *test_adv_install(struct bst_test_list *test); extern struct bst_test_list *test_suspend_install(struct bst_test_list *test); extern struct bst_test_list *test_beacon_install(struct bst_test_list *tests); +extern struct bst_test_list *test_provision_install(struct bst_test_list *tests); #elif defined(CONFIG_BT_CTLR_LOW_LAT) extern struct bst_test_list *test_transport_install(struct bst_test_list *tests); extern struct bst_test_list *test_friendship_install(struct bst_test_list *tests); @@ -60,7 +61,7 @@ bst_test_install_t test_installers[] = { test_proxy_sol_install, #endif #elif defined(CONFIG_BT_MESH_GATT_PROXY) - test_adv_install, test_suspend_install, test_beacon_install, + test_adv_install, test_suspend_install, test_beacon_install, test_provision_install, #elif defined(CONFIG_BT_CTLR_LOW_LAT) test_transport_install, test_friendship_install, test_suspend_install, test_adv_install, #else diff --git a/tests/bsim/bluetooth/mesh/src/test_provision.c b/tests/bsim/bluetooth/mesh/src/test_provision.c index f4057ebc7b74..126b63f6b811 100644 --- a/tests/bsim/bluetooth/mesh/src/test_provision.c +++ b/tests/bsim/bluetooth/mesh/src/test_provision.c @@ -95,6 +95,24 @@ static uint8_t *uuid_to_provision; static struct k_sem reprov_sem; static uint32_t link_close_timestamp; +/* Set prov_bearer to non-zero invalid value. */ +static bt_mesh_prov_bearer_t prov_bearer = 0xF8; + +static void test_args_parse(int argc, char *argv[]) +{ + bs_args_struct_t args_struct[] = { + { + .dest = &prov_bearer, + .type = 'i', + .name = "{invalid, PB-ADV, PB-GATT}", + .option = "prov-brearer", + .descript = "Provisioning bearer that is to be used." + }, + }; + + bs_args_parse_all_cmd_line(argc, argv, args_struct); +} + #if IS_RPR_PRESENT static struct k_sem pdu_send_sem; static struct k_sem scan_sem; @@ -268,10 +286,22 @@ static void unprovisioned_beacon(uint8_t uuid[16], if (uuid_to_provision && memcmp(uuid, uuid_to_provision, 16)) { return; } - bt_mesh_provision_adv(uuid, 0, prov_addr, 0); } +static void unprovisioned_beacon_gatt(uint8_t uuid[16], bt_mesh_prov_oob_info_t oob_info) +{ + if (!atomic_test_bit(test_flags, IS_PROVISIONER)) { + return; + } + + if (uuid_to_provision && memcmp(uuid, uuid_to_provision, 16)) { + return; + } + + bt_mesh_provision_gatt(uuid, 0, prov_addr, 0); +} + static void prov_complete(uint16_t net_idx, uint16_t addr) { if (!atomic_test_bit(test_flags, IS_PROVISIONER)) { @@ -305,7 +335,7 @@ static void prov_reprovisioned(uint16_t addr) static void prov_reset(void) { - ASSERT_OK(bt_mesh_prov_enable(BT_MESH_PROV_ADV)); + ASSERT_OK(bt_mesh_prov_enable(prov_bearer)); } static bt_mesh_input_action_t gact; @@ -363,6 +393,7 @@ static void capabilities(const struct bt_mesh_dev_capabilities *cap); static struct bt_mesh_prov prov = { .uuid = dev_uuid, .unprovisioned_beacon = unprovisioned_beacon, + .unprovisioned_beacon_gatt = unprovisioned_beacon_gatt, .complete = prov_complete, .link_open = prov_link_open, .link_close = prov_link_close, @@ -483,7 +514,7 @@ static void oob_device(bool use_oob_pk) for (int i = 0; i < ARRAY_SIZE(oob_auth_test_vector); i++) { oob_auth_set(i); - ASSERT_OK(bt_mesh_prov_enable(BT_MESH_PROV_ADV)); + ASSERT_OK(bt_mesh_prov_enable(prov_bearer)); /* Keep a long timeout so the prov multi case has time to finish: */ ASSERT_OK(k_sem_take(&prov_sem, K_SECONDS(40))); @@ -606,13 +637,12 @@ static void node_configure_and_reset(void) /** @brief Verify that this device pb-adv provision. */ -static void test_device_pb_adv_no_oob(void) +static void test_device_no_oob(void) { k_sem_init(&prov_sem, 0, 1); bt_mesh_device_setup(&prov, &comp); - - ASSERT_OK(bt_mesh_prov_enable(BT_MESH_PROV_ADV)); + ASSERT_OK(bt_mesh_prov_enable(prov_bearer)); LOG_INF("Mesh initialized\n"); @@ -624,13 +654,13 @@ static void test_device_pb_adv_no_oob(void) /** @brief Verify that this device can be reprovisioned after resets */ -static void test_device_pb_adv_reprovision(void) +static void test_device_reprovision(void) { k_sem_init(&prov_sem, 0, 1); bt_mesh_device_setup(&prov, &comp); - ASSERT_OK(bt_mesh_prov_enable(BT_MESH_PROV_ADV)); + ASSERT_OK(bt_mesh_prov_enable(prov_bearer)); LOG_INF("Mesh initialized\n"); @@ -645,7 +675,7 @@ static void test_device_pb_adv_reprovision(void) /** @brief Verify that this provisioner pb-adv provision. */ -static void test_provisioner_pb_adv_no_oob(void) +static void test_provisioner_no_oob(void) { k_sem_init(&prov_sem, 0, 1); @@ -660,14 +690,14 @@ static void test_provisioner_pb_adv_no_oob(void) PASS(); } -static void test_device_pb_adv_oob_auth(void) +static void test_device_oob_auth(void) { oob_device(false); PASS(); } -static void test_provisioner_pb_adv_oob_auth(void) +static void test_provisioner_oob_auth(void) { oob_provisioner(false, false); @@ -683,21 +713,21 @@ static void test_back_channel_pre_init(void) } } -static void test_device_pb_adv_oob_public_key(void) +static void test_device_oob_public_key(void) { oob_device(true); PASS(); } -static void test_provisioner_pb_adv_oob_public_key(void) +static void test_provisioner_oob_public_key(void) { oob_provisioner(true, true); PASS(); } -static void test_provisioner_pb_adv_oob_auth_no_oob_public_key(void) +static void test_provisioner_oob_auth_no_oob_public_key(void) { oob_provisioner(true, false); @@ -706,7 +736,7 @@ static void test_provisioner_pb_adv_oob_auth_no_oob_public_key(void) /** @brief Verify that the provisioner can provision multiple devices in a row */ -static void test_provisioner_pb_adv_multi(void) +static void test_provisioner_multi(void) { k_sem_init(&prov_sem, 0, 1); @@ -767,7 +797,7 @@ static void test_provisioner_iv_update_flag_one(void) /** @brief Verify that the provisioner can provision a device multiple times after resets */ -static void test_provisioner_pb_adv_reprovision(void) +static void test_provisioner_reprovision(void) { k_sem_init(&prov_sem, 0, 1); @@ -798,7 +828,7 @@ static void test_device_unresponsive(void) k_sem_init(&prov_sem, 0, 1); - ASSERT_OK(bt_mesh_prov_enable(BT_MESH_PROV_ADV)); + ASSERT_OK(bt_mesh_prov_enable(prov_bearer)); /* stop responding for 30s to timeout PB-ADV link establishment. */ bt_mesh_scan_disable(); @@ -1739,35 +1769,35 @@ static void test_device_pb_remote_server_ncrp_second_time(void) } #endif /* IS_RPR_PRESENT */ -#define TEST_CASE(role, name, description) \ - { \ - .test_id = "prov_" #role "_" #name, .test_descr = description, \ - .test_post_init_f = test_##role##_init, \ - .test_tick_f = bt_mesh_test_timeout, \ - .test_main_f = test_##role##_##name, \ - .test_delete_f = test_terminate \ - } -#define TEST_CASE_WBACKCHANNEL(role, name, description) \ - { \ - .test_id = "prov_" #role "_" #name, .test_descr = description, \ - .test_post_init_f = test_##role##_init, \ - .test_pre_init_f = test_back_channel_pre_init, \ - .test_tick_f = bt_mesh_test_timeout, \ - .test_main_f = test_##role##_##name, \ - .test_delete_f = test_terminate \ - } +/* Test cases by default will run over PB_ADV*/ +#define TEST_CASE(role, name, description) \ + {.test_id = "prov_" #role "_" #name, \ + .test_descr = description, \ + .test_args_f = test_args_parse, \ + .test_post_init_f = test_##role##_init, \ + .test_tick_f = bt_mesh_test_timeout, \ + .test_main_f = test_##role##_##name, \ + .test_delete_f = test_terminate} +/* Test cases that will run over either PB_ADV or PB_GATT */ +#define TEST_CASE_WBACKCHANNEL(role, name, description) \ + {.test_id = "prov_" #role "_" #name, \ + .test_descr = description, \ + .test_args_f = test_args_parse, \ + .test_post_init_f = test_##role##_init, \ + .test_pre_init_f = test_back_channel_pre_init, \ + .test_tick_f = bt_mesh_test_timeout, \ + .test_main_f = test_##role##_##name, \ + .test_delete_f = test_terminate} static const struct bst_test_instance test_connect[] = { - TEST_CASE(device, pb_adv_no_oob, - "Device: pb-adv provisioning use no-oob method"), - TEST_CASE_WBACKCHANNEL(device, pb_adv_oob_auth, - "Device: pb-adv provisioning use oob authentication"), - TEST_CASE_WBACKCHANNEL(device, pb_adv_oob_public_key, - "Device: pb-adv provisioning use oob public key"), - TEST_CASE(device, pb_adv_reprovision, - "Device: pb-adv provisioning, reprovision"), TEST_CASE(device, unresponsive, - "Device: pb-adv provisioning, stops and resumes responding to provisioning"), + "Device: provisioning, stops and resumes responding to provisioning"), + TEST_CASE(device, no_oob, "Device: provisioning use no-oob method"), + TEST_CASE_WBACKCHANNEL(device, oob_auth, + "Device: provisioning use oob authentication"), + TEST_CASE_WBACKCHANNEL(device, oob_public_key, + "Device: provisioning use oob public key"), + TEST_CASE(device, reprovision, "Device: provisioning, reprovision"), #if IS_RPR_PRESENT TEST_CASE(device, pb_remote_server_unproved, "Device: used for remote provisioning, starts unprovisioned"), @@ -1780,23 +1810,24 @@ static const struct bst_test_instance test_connect[] = { TEST_CASE(device, pb_remote_server_same_dev, "Device: used for remote reprovisioning, with both client and server"), #endif - - TEST_CASE(provisioner, pb_adv_no_oob, - "Provisioner: pb-adv provisioning use no-oob method"), - TEST_CASE(provisioner, pb_adv_multi, - "Provisioner: pb-adv provisioning multiple devices"), TEST_CASE(provisioner, iv_update_flag_zero, "Provisioner: effect on ivu_duration when IV Update flag is set to zero"), TEST_CASE(provisioner, iv_update_flag_one, "Provisioner: effect on ivu_duration when IV Update flag is set to one"), - TEST_CASE_WBACKCHANNEL(provisioner, pb_adv_oob_auth, - "Provisioner: pb-adv provisioning use oob authentication"), - TEST_CASE_WBACKCHANNEL(provisioner, pb_adv_oob_public_key, - "Provisioner: pb-adv provisioning use oob public key"), - TEST_CASE_WBACKCHANNEL(provisioner, pb_adv_oob_auth_no_oob_public_key, - "Provisioner: pb-adv provisioning use oob authentication, ignore oob public key"), - TEST_CASE(provisioner, pb_adv_reprovision, - "Provisioner: pb-adv provisioning, resetting and reprovisioning multiple times."), + TEST_CASE(provisioner, no_oob, + "Provisioner: provisioning use no-oob method"), + TEST_CASE(provisioner, multi, + "Provisioner: provisioning multiple devices"), + TEST_CASE_WBACKCHANNEL(provisioner, oob_auth, + "Provisioner: provisioning use oob authentication"), + TEST_CASE_WBACKCHANNEL(provisioner, oob_public_key, + "Provisioner: provisioning use oob public key"), + TEST_CASE_WBACKCHANNEL( + provisioner, oob_auth_no_oob_public_key, + "Provisioner: provisioning use oob authentication, ignore oob public key"), + TEST_CASE( + provisioner, reprovision, + "Provisioner: provisioning, resetting and reprovisioning multiple times."), #if IS_RPR_PRESENT TEST_CASE(provisioner, pb_remote_client_reprovision, "Provisioner: pb-remote provisioning, resetting and reprov-ing multiple times."), @@ -1808,8 +1839,7 @@ static const struct bst_test_instance test_connect[] = { "Provisioner: provisioning test, devices stop responding"), #endif - BSTEST_END_MARKER -}; + BSTEST_END_MARKER}; struct bst_test_list *test_provision_install(struct bst_test_list *tests) { diff --git a/tests/bsim/bluetooth/mesh/tests_scripts/provision/pb_adv_multi.sh b/tests/bsim/bluetooth/mesh/tests_scripts/provision/pb_adv_multi.sh deleted file mode 100755 index 61bbbcc1a27c..000000000000 --- a/tests/bsim/bluetooth/mesh/tests_scripts/provision/pb_adv_multi.sh +++ /dev/null @@ -1,21 +0,0 @@ -#!/usr/bin/env bash -# Copyright 2021 Nordic Semiconductor -# SPDX-License-Identifier: Apache-2.0 - -source $(dirname "${BASH_SOURCE[0]}")/../../_mesh_test.sh - -# Provision 3 devices in succession: -# Note that the number of devices must match the -# PROV_MULTI_COUNT define in test_provision.c -RunTest mesh_prov_pb_adv_multi \ - prov_provisioner_pb_adv_multi \ - prov_device_pb_adv_no_oob \ - prov_device_pb_adv_no_oob \ - prov_device_pb_adv_no_oob - -overlay=overlay_psa_conf -RunTest mesh_prov_pb_adv_multi_psa \ - prov_provisioner_pb_adv_multi \ - prov_device_pb_adv_no_oob \ - prov_device_pb_adv_no_oob \ - prov_device_pb_adv_no_oob diff --git a/tests/bsim/bluetooth/mesh/tests_scripts/provision/pb_adv_no_oob.sh b/tests/bsim/bluetooth/mesh/tests_scripts/provision/pb_adv_no_oob.sh deleted file mode 100755 index 91bde356f2b8..000000000000 --- a/tests/bsim/bluetooth/mesh/tests_scripts/provision/pb_adv_no_oob.sh +++ /dev/null @@ -1,14 +0,0 @@ -#!/usr/bin/env bash -# Copyright 2021 Lingao Meng -# SPDX-License-Identifier: Apache-2.0 - -source $(dirname "${BASH_SOURCE[0]}")/../../_mesh_test.sh - -RunTest mesh_prov_pb_adv_on_oob \ - prov_device_pb_adv_no_oob \ - prov_provisioner_pb_adv_no_oob - -overlay=overlay_psa_conf -RunTest mesh_prov_pb_adv_on_oob_psa \ - prov_device_pb_adv_no_oob \ - prov_provisioner_pb_adv_no_oob diff --git a/tests/bsim/bluetooth/mesh/tests_scripts/provision/pb_adv_oob_auth_oob_pk.sh b/tests/bsim/bluetooth/mesh/tests_scripts/provision/pb_adv_oob_auth_oob_pk.sh deleted file mode 100755 index d24e5afccd4a..000000000000 --- a/tests/bsim/bluetooth/mesh/tests_scripts/provision/pb_adv_oob_auth_oob_pk.sh +++ /dev/null @@ -1,13 +0,0 @@ -#!/usr/bin/env bash -# Copyright 2021 Nordic Semiconductor -# SPDX-License-Identifier: Apache-2.0 - -source $(dirname "${BASH_SOURCE[0]}")/../../_mesh_test.sh - -# Test provisioning with OOB authentication and with OOB public key -RunTest mesh_prov_pb_adv_oob_public_key \ - prov_device_pb_adv_oob_public_key prov_provisioner_pb_adv_oob_public_key - -overlay=overlay_psa_conf -RunTest mesh_prov_pb_adv_oob_public_key_psa \ - prov_device_pb_adv_oob_public_key prov_provisioner_pb_adv_oob_public_key diff --git a/tests/bsim/bluetooth/mesh/tests_scripts/provision/pb_adv_reprovision.sh b/tests/bsim/bluetooth/mesh/tests_scripts/provision/pb_adv_reprovision.sh deleted file mode 100755 index 0e3b84e3138b..000000000000 --- a/tests/bsim/bluetooth/mesh/tests_scripts/provision/pb_adv_reprovision.sh +++ /dev/null @@ -1,14 +0,0 @@ -#!/usr/bin/env bash -# Copyright 2021 Nordic Semiconductor -# SPDX-License-Identifier: Apache-2.0 - -source $(dirname "${BASH_SOURCE[0]}")/../../_mesh_test.sh - -RunTest mesh_prov_pb_adv_repr \ - prov_device_pb_adv_reprovision \ - prov_provisioner_pb_adv_reprovision - -overlay=overlay_psa_conf -RunTest mesh_prov_pb_adv_repr_psa \ - prov_device_pb_adv_reprovision \ - prov_provisioner_pb_adv_reprovision diff --git a/tests/bsim/bluetooth/mesh/tests_scripts/provision/pb_remote_parallel.sh b/tests/bsim/bluetooth/mesh/tests_scripts/provision/pb_remote_parallel.sh index 6ecb32921939..815df506d5fb 100755 --- a/tests/bsim/bluetooth/mesh/tests_scripts/provision/pb_remote_parallel.sh +++ b/tests/bsim/bluetooth/mesh/tests_scripts/provision/pb_remote_parallel.sh @@ -14,12 +14,14 @@ source $(dirname "${BASH_SOURCE[0]}")/../../_mesh_test.sh RunTest mesh_prov_pb_remote_parallel \ prov_provisioner_pb_remote_client_parallel \ prov_device_pb_remote_server_unproved \ - prov_device_pb_adv_no_oob \ - prov_device_pb_adv_no_oob + prov_device_no_oob \ + prov_device_no_oob \ + -- -argstest prov-brearer=1 overlay=overlay_psa_conf RunTest mesh_prov_pb_remote_parallel_psa \ prov_provisioner_pb_remote_client_parallel \ prov_device_pb_remote_server_unproved \ - prov_device_pb_adv_no_oob \ - prov_device_pb_adv_no_oob + prov_device_no_oob \ + prov_device_no_oob \ + -- -argstest prov-brearer=1 diff --git a/tests/bsim/bluetooth/mesh/tests_scripts/provision/pb_remote_reprovision.sh b/tests/bsim/bluetooth/mesh/tests_scripts/provision/pb_remote_reprovision.sh index 1a2ae89d655f..1e5da310bd3f 100755 --- a/tests/bsim/bluetooth/mesh/tests_scripts/provision/pb_remote_reprovision.sh +++ b/tests/bsim/bluetooth/mesh/tests_scripts/provision/pb_remote_reprovision.sh @@ -15,10 +15,12 @@ source $(dirname "${BASH_SOURCE[0]}")/../../_mesh_test.sh RunTest mesh_prov_pb_remote_reprovision \ prov_provisioner_pb_remote_client_reprovision \ prov_device_pb_remote_server_unproved \ - prov_device_pb_adv_reprovision + prov_device_reprovision \ + -- -argstest prov-brearer=1 overlay=overlay_psa_conf RunTest mesh_prov_pb_remote_reprovision_psa \ prov_provisioner_pb_remote_client_reprovision \ prov_device_pb_remote_server_unproved \ - prov_device_pb_adv_reprovision + prov_device_reprovision \ + -- -argstest prov-brearer=1 diff --git a/tests/bsim/bluetooth/mesh/tests_scripts/provision/pb_remote_timeout.sh b/tests/bsim/bluetooth/mesh/tests_scripts/provision/pb_remote_timeout.sh index 7feba542c44b..b332b1987adf 100755 --- a/tests/bsim/bluetooth/mesh/tests_scripts/provision/pb_remote_timeout.sh +++ b/tests/bsim/bluetooth/mesh/tests_scripts/provision/pb_remote_timeout.sh @@ -27,10 +27,10 @@ source $(dirname "${BASH_SOURCE[0]}")/../../_mesh_test.sh RunTest mesh_prov_pb_remote_provisioning_timeout \ prov_provisioner_pb_remote_client_provision_timeout \ prov_device_pb_remote_server_unproved_unresponsive \ - prov_device_unresponsive + prov_device_unresponsive -- -argstest prov-brearer=1 overlay=overlay_psa_conf RunTest mesh_prov_pb_remote_provisioning_timeout_psa \ prov_provisioner_pb_remote_client_provision_timeout \ prov_device_pb_remote_server_unproved_unresponsive \ - prov_device_unresponsive + prov_device_unresponsive -- -argstest prov-brearer=1 diff --git a/tests/bsim/bluetooth/mesh/tests_scripts/provision/prov_multi.sh b/tests/bsim/bluetooth/mesh/tests_scripts/provision/prov_multi.sh new file mode 100755 index 000000000000..bed577add241 --- /dev/null +++ b/tests/bsim/bluetooth/mesh/tests_scripts/provision/prov_multi.sh @@ -0,0 +1,31 @@ +#!/usr/bin/env bash +# Copyright 2021 Nordic Semiconductor +# SPDX-License-Identifier: Apache-2.0 + +source $(dirname "${BASH_SOURCE[0]}")/../../_mesh_test.sh + +# Provision 3 devices in succession: +# Note that the number of devices must match the +# PROV_MULTI_COUNT define in test_provision.c +RunTest mesh_prov_pb_adv_multi \ + prov_provisioner_multi \ + prov_device_no_oob \ + prov_device_no_oob \ + prov_device_no_oob \ + -- -argstest prov-brearer=1 + +overlay=overlay_gatt_conf_overlay_psa_conf +RunTest mesh_prov_pb_gatt_multi \ + prov_provisioner_multi \ + prov_device_no_oob \ + prov_device_no_oob \ + prov_device_no_oob \ + -- -argstest prov-brearer=2 + +overlay=overlay_psa_conf +RunTest mesh_prov_pb_adv_multi_psa \ + prov_provisioner_multi \ + prov_device_no_oob \ + prov_device_no_oob \ + prov_device_no_oob \ + -- -argstest prov-brearer=1 diff --git a/tests/bsim/bluetooth/mesh/tests_scripts/provision/prov_no_oob.sh b/tests/bsim/bluetooth/mesh/tests_scripts/provision/prov_no_oob.sh new file mode 100755 index 000000000000..4f633b91c2c0 --- /dev/null +++ b/tests/bsim/bluetooth/mesh/tests_scripts/provision/prov_no_oob.sh @@ -0,0 +1,22 @@ +#!/usr/bin/env bash +# Copyright 2021 Lingao Meng +# SPDX-License-Identifier: Apache-2.0 + +source $(dirname "${BASH_SOURCE[0]}")/../../_mesh_test.sh + +RunTest mesh_prov_pb_adv_on_oob \ + prov_device_no_oob \ + prov_provisioner_no_oob \ + -- -argstest prov-brearer=1 + +overlay=overlay_gatt_conf_overlay_psa_conf +RunTest mesh_prov_pb_gatt_on_oob \ + prov_device_no_oob \ + prov_provisioner_no_oob \ + -- -argstest prov-brearer=2 + +overlay=overlay_psa_conf +RunTest mesh_prov_pb_adv_on_oob_psa \ + prov_device_no_oob \ + prov_provisioner_no_oob \ + -- -argstest prov-brearer=1 diff --git a/tests/bsim/bluetooth/mesh/tests_scripts/provision/pb_adv_oob_auth_ib_pk.sh b/tests/bsim/bluetooth/mesh/tests_scripts/provision/prov_oob_auth_ib_pk.sh similarity index 50% rename from tests/bsim/bluetooth/mesh/tests_scripts/provision/pb_adv_oob_auth_ib_pk.sh rename to tests/bsim/bluetooth/mesh/tests_scripts/provision/prov_oob_auth_ib_pk.sh index ef8e07f7c80a..e53c6b5f2546 100755 --- a/tests/bsim/bluetooth/mesh/tests_scripts/provision/pb_adv_oob_auth_ib_pk.sh +++ b/tests/bsim/bluetooth/mesh/tests_scripts/provision/prov_oob_auth_ib_pk.sh @@ -6,8 +6,15 @@ source $(dirname "${BASH_SOURCE[0]}")/../../_mesh_test.sh # Test provisioning with OOB authentication and with inband public key RunTest mesh_prov_pb_adv_oob_auth \ - prov_device_pb_adv_oob_auth prov_provisioner_pb_adv_oob_auth + prov_device_oob_auth prov_provisioner_oob_auth \ + -- -argstest prov-brearer=1 + +overlay=overlay_gatt_conf_overlay_psa_conf +RunTest mesh_prov_pb_gatt_oob_auth \ + prov_device_oob_auth prov_provisioner_oob_auth \ + -- -argstest prov-brearer=2 overlay=overlay_psa_conf RunTest mesh_prov_pb_adv_oob_auth_psa \ - prov_device_pb_adv_oob_auth prov_provisioner_pb_adv_oob_auth + prov_device_oob_auth prov_provisioner_oob_auth \ + -- -argstest prov-brearer=1 diff --git a/tests/bsim/bluetooth/mesh/tests_scripts/provision/pb_adv_oob_auth_ignore_oob_pk.sh b/tests/bsim/bluetooth/mesh/tests_scripts/provision/prov_oob_auth_ignore_oob_pk.sh similarity index 50% rename from tests/bsim/bluetooth/mesh/tests_scripts/provision/pb_adv_oob_auth_ignore_oob_pk.sh rename to tests/bsim/bluetooth/mesh/tests_scripts/provision/prov_oob_auth_ignore_oob_pk.sh index 8df3c6a4bf6b..7ac6b5f4d758 100755 --- a/tests/bsim/bluetooth/mesh/tests_scripts/provision/pb_adv_oob_auth_ignore_oob_pk.sh +++ b/tests/bsim/bluetooth/mesh/tests_scripts/provision/prov_oob_auth_ignore_oob_pk.sh @@ -7,8 +7,15 @@ source $(dirname "${BASH_SOURCE[0]}")/../../_mesh_test.sh # Test provisioning with OOB authentication, with device OOB public key # but provisioner doesn't have OOB public key RunTest mesh_prov_pb_adv_device_w_oob_pk_prvnr_wt_pk \ - prov_device_pb_adv_oob_public_key prov_provisioner_pb_adv_oob_auth_no_oob_public_key + prov_device_oob_public_key prov_provisioner_oob_auth_no_oob_public_key \ + -- -argstest prov-brearer=1 + +overlay=overlay_gatt_conf_overlay_psa_conf +RunTest mesh_prov_pb_gatt_device_w_oob_pk_prvnr_wt_pk \ + prov_device_oob_public_key prov_provisioner_oob_auth_no_oob_public_key \ + -- -argstest prov-brearer=2 overlay=overlay_psa_conf RunTest mesh_prov_pb_adv_device_w_oob_pk_prvnr_wt_pk_psa \ - prov_device_pb_adv_oob_public_key prov_provisioner_pb_adv_oob_auth_no_oob_public_key + prov_device_oob_public_key prov_provisioner_oob_auth_no_oob_public_key \ + -- -argstest prov-brearer=1 diff --git a/tests/bsim/bluetooth/mesh/tests_scripts/provision/prov_oob_auth_oob_pk.sh b/tests/bsim/bluetooth/mesh/tests_scripts/provision/prov_oob_auth_oob_pk.sh new file mode 100755 index 000000000000..90375f81a504 --- /dev/null +++ b/tests/bsim/bluetooth/mesh/tests_scripts/provision/prov_oob_auth_oob_pk.sh @@ -0,0 +1,20 @@ +#!/usr/bin/env bash +# Copyright 2021 Nordic Semiconductor +# SPDX-License-Identifier: Apache-2.0 + +source $(dirname "${BASH_SOURCE[0]}")/../../_mesh_test.sh + +# Test provisioning with OOB authentication and with OOB public key +RunTest mesh_prov_pb_adv_oob_public_key \ + prov_device_oob_public_key prov_provisioner_oob_public_key \ + -- -argstest prov-brearer=1 + +overlay=overlay_gatt_conf_overlay_psa_conf +RunTest mesh_prov_pb_gatt_oob_public_key \ + prov_device_oob_public_key prov_provisioner_oob_public_key \ + -- -argstest prov-brearer=2 + +overlay=overlay_psa_conf +RunTest mesh_prov_pb_adv_oob_public_key_psa \ + prov_device_oob_public_key prov_provisioner_oob_public_key \ + -- -argstest prov-brearer=1 diff --git a/tests/bsim/bluetooth/mesh/tests_scripts/provision/prov_reprovision.sh b/tests/bsim/bluetooth/mesh/tests_scripts/provision/prov_reprovision.sh new file mode 100755 index 000000000000..0143ef20c456 --- /dev/null +++ b/tests/bsim/bluetooth/mesh/tests_scripts/provision/prov_reprovision.sh @@ -0,0 +1,22 @@ +#!/usr/bin/env bash +# Copyright 2021 Nordic Semiconductor +# SPDX-License-Identifier: Apache-2.0 + +source $(dirname "${BASH_SOURCE[0]}")/../../_mesh_test.sh + +RunTest mesh_prov_pb_adv_repr \ + prov_device_reprovision \ + prov_provisioner_reprovision \ + -- -argstest prov-brearer=1 + +overlay=overlay_gatt_conf_overlay_psa_conf +RunTest mesh_prov_pb_gatt_repr \ + prov_device_reprovision \ + prov_provisioner_reprovision \ + -- -argstest prov-brearer=2 + +overlay=overlay_psa_conf +RunTest mesh_prov_pb_adv_repr_psa \ + prov_device_reprovision \ + prov_provisioner_reprovision \ + -- -argstest prov-brearer=1 From 4c7ffbdfe81c25cbea833c60013ef7566401ae1e Mon Sep 17 00:00:00 2001 From: Kyra Lengfeld Date: Tue, 17 Dec 2024 18:19:32 +0100 Subject: [PATCH 05/49] [nrf fromtree] Bluetooth: Mesh: use settings priority feature By using `SETTINGS_STATIC_HANDLER_DEFINE_WITH_CPRIO` we can ensure that BT subsystems always get loaded after BT, and BT Mesh after both of them. This solves the host having to register a GATT service in a delayed manner, as we are sure to now register the GATT service after sc_commit sets `SC_LOAD`. Signed-off-by: Kyra Lengfeld (cherry picked from commit 64d87136838a70d847642979b26090fa83e451f9) Signed-off-by: alperen sener --- subsys/bluetooth/common/bt_settings_commit.h | 19 ++++++++ subsys/bluetooth/host/settings.c | 4 +- subsys/bluetooth/host/settings.h | 5 ++- subsys/bluetooth/mesh/proxy_srv.c | 46 +++++--------------- subsys/bluetooth/mesh/settings.c | 5 ++- tests/bsim/bluetooth/mesh/overlay_gatt.conf | 2 + 6 files changed, 42 insertions(+), 39 deletions(-) create mode 100644 subsys/bluetooth/common/bt_settings_commit.h diff --git a/subsys/bluetooth/common/bt_settings_commit.h b/subsys/bluetooth/common/bt_settings_commit.h new file mode 100644 index 000000000000..921e62af50cf --- /dev/null +++ b/subsys/bluetooth/common/bt_settings_commit.h @@ -0,0 +1,19 @@ +/* Copyright (c) 2024 Nordic Semiconductor ASA + * SPDX-License-Identifier: Apache-2.0 + */ +#ifndef BT_SETTINGS_COMMIT_H_ +#define BT_SETTINGS_COMMIT_H_ + +/** + * @brief Bluetooth Settings Commit Priorities + * + * Enum of commit priorities for Bluetooth settings handlers. + * Lower values indicate higher priority. + */ +enum bt_settings_commit_priority { + BT_SETTINGS_CPRIO_0, + BT_SETTINGS_CPRIO_1, + BT_SETTINGS_CPRIO_2, +}; + +#endif /* BT_SETTINGS_COMMIT_H_ */ diff --git a/subsys/bluetooth/host/settings.c b/subsys/bluetooth/host/settings.c index 4cc2b56f6a09..1718bc653d40 100644 --- a/subsys/bluetooth/host/settings.c +++ b/subsys/bluetooth/host/settings.c @@ -8,6 +8,7 @@ #include #include +#include #include #include @@ -281,7 +282,8 @@ static int commit_settings(void) return 0; } -SETTINGS_STATIC_HANDLER_DEFINE(bt, "bt", NULL, set_setting, commit_settings, NULL); +SETTINGS_STATIC_HANDLER_DEFINE_WITH_CPRIO(bt, "bt", NULL, set_setting, commit_settings, NULL, + BT_SETTINGS_CPRIO_0); int bt_settings_init(void) { diff --git a/subsys/bluetooth/host/settings.h b/subsys/bluetooth/host/settings.h index 1aa3ab0a87fe..cf5618b378db 100644 --- a/subsys/bluetooth/host/settings.h +++ b/subsys/bluetooth/host/settings.h @@ -10,6 +10,7 @@ #include #include +#include /* Max settings key length (with all components) */ #define BT_SETTINGS_KEY_MAX 36 @@ -18,7 +19,9 @@ #define BT_SETTINGS_SIZE(in_size) ((((((in_size) - 1) / 3) * 4) + 4) + 1) #define BT_SETTINGS_DEFINE(_hname, _subtree, _set, _commit) \ - SETTINGS_STATIC_HANDLER_DEFINE(bt_##_hname, "bt/" _subtree, NULL, _set, _commit, NULL) + SETTINGS_STATIC_HANDLER_DEFINE_WITH_CPRIO(bt_##_hname, "bt/" _subtree, NULL, _set, _commit,\ + NULL, BT_SETTINGS_CPRIO_1) + #define ID_DATA_LEN(array) (bt_dev.id_count * sizeof(array[0])) diff --git a/subsys/bluetooth/mesh/proxy_srv.c b/subsys/bluetooth/mesh/proxy_srv.c index aab4afe47df5..8863a5ce52f9 100644 --- a/subsys/bluetooth/mesh/proxy_srv.c +++ b/subsys/bluetooth/mesh/proxy_srv.c @@ -36,9 +36,6 @@ #include LOG_MODULE_REGISTER(bt_mesh_gatt); -#define PROXY_SVC_INIT_TIMEOUT K_MSEC(10) -#define PROXY_SVC_REG_ATTEMPTS 5 - /* Interval to update random value in (10 minutes). * * Defined in the Bluetooth Mesh Specification v1.1, Section 7.2.2.2.4. @@ -957,34 +954,6 @@ static struct bt_gatt_attr proxy_attrs[] = { }; static struct bt_gatt_service proxy_svc = BT_GATT_SERVICE(proxy_attrs); -static void svc_reg_work_handler(struct k_work *work); -static struct k_work_delayable svc_reg_work = Z_WORK_DELAYABLE_INITIALIZER(svc_reg_work_handler); -static uint32_t svc_reg_attempts; - -static void svc_reg_work_handler(struct k_work *work) -{ - int err; - - err = bt_gatt_service_register(&proxy_svc); - if ((err == -EINVAL) && ((--svc_reg_attempts) > 0)) { - /* settings_load() didn't finish yet. Try again. */ - (void)k_work_schedule(&svc_reg_work, PROXY_SVC_INIT_TIMEOUT); - return; - } else if (err) { - LOG_ERR("Unable to register Mesh Proxy Service (err %d)", err); - return; - } - - service_registered = true; - - for (int i = 0; i < ARRAY_SIZE(clients); i++) { - if (clients[i].cli) { - clients[i].filter_type = ACCEPT; - } - } - - bt_mesh_adv_gatt_update(); -} int bt_mesh_proxy_gatt_enable(void) { @@ -1000,13 +969,20 @@ int bt_mesh_proxy_gatt_enable(void) return -EBUSY; } - svc_reg_attempts = PROXY_SVC_REG_ATTEMPTS; - err = k_work_schedule(&svc_reg_work, PROXY_SVC_INIT_TIMEOUT); - if (err < 0) { - LOG_ERR("Enabling GATT proxy failed (err %d)", err); + err = bt_gatt_service_register(&proxy_svc); + if (err) { + LOG_ERR("Unable to register Mesh Proxy Service (err %d)", err); return err; } + service_registered = true; + + for (int i = 0; i < ARRAY_SIZE(clients); i++) { + if (clients[i].cli) { + clients[i].filter_type = ACCEPT; + } + } + return 0; } diff --git a/subsys/bluetooth/mesh/settings.c b/subsys/bluetooth/mesh/settings.c index 6bd651c74939..f02672e6d668 100644 --- a/subsys/bluetooth/mesh/settings.c +++ b/subsys/bluetooth/mesh/settings.c @@ -12,6 +12,7 @@ #include #include +#include #include #include "host/hci_core.h" @@ -117,8 +118,8 @@ static int mesh_commit(void) return 0; } -SETTINGS_STATIC_HANDLER_DEFINE(bt_mesh, "bt/mesh", NULL, NULL, mesh_commit, - NULL); +SETTINGS_STATIC_HANDLER_DEFINE_WITH_CPRIO(bt_mesh, "bt/mesh", NULL, NULL, mesh_commit, NULL, + BT_SETTINGS_CPRIO_2); /* Pending flags that use K_NO_WAIT as the storage timeout */ #define NO_WAIT_PENDING_BITS (BIT(BT_MESH_SETTINGS_NET_PENDING) | \ diff --git a/tests/bsim/bluetooth/mesh/overlay_gatt.conf b/tests/bsim/bluetooth/mesh/overlay_gatt.conf index 718e0eae4b40..0438205696e3 100644 --- a/tests/bsim/bluetooth/mesh/overlay_gatt.conf +++ b/tests/bsim/bluetooth/mesh/overlay_gatt.conf @@ -6,3 +6,5 @@ CONFIG_BT_MESH_PB_GATT_CLIENT=y CONFIG_BT_MESH_GATT_PROXY=y CONFIG_BT_MESH_PROXY_CLIENT=y CONFIG_BT_MESH_PROXY_SOLICITATION=y + +CONFIG_BT_BUF_CMD_TX_COUNT=3 From ecea727adbc71f8a43d781834b57b393016e8071 Mon Sep 17 00:00:00 2001 From: Aleksandr Khromykh Date: Thu, 28 Nov 2024 15:30:11 +0100 Subject: [PATCH 06/49] [nrf fromtree] Bluetooth: Mesh: use secure storage in ble mesh Commit: - adds dependency of the mbedtls psa usage on secure storage - removes PSA ITS emulator and enables usage of the secure storage in ble mesh bsim tests - enables secure storage in all ble mesh and related samples Signed-off-by: Aleksandr Khromykh (cherry picked from commit 967b096ad98a3c6f3d7b0a7aabf4dcb3ebf5d35f) Signed-off-by: alperen sener --- modules/mbedtls/configs/config-tls-generic.h | 6 - .../boards/nrf5340dk_nrf5340_cpuapp_ns.conf | 1 + samples/bluetooth/mesh/prj.conf | 1 + .../boards/nrf5340dk_nrf5340_cpuapp_ns.conf | 1 + samples/bluetooth/mesh_demo/prj.conf | 1 + .../boards/nrf5340dk_nrf5340_cpuapp_ns.conf | 1 + samples/bluetooth/mesh_provisioner/prj.conf | 1 + samples/boards/nordic/mesh/onoff-app/prj.conf | 2 + .../onoff_level_lighting_vnd_app/prj.conf | 1 + .../phytec/reel_board/mesh_badge/prj.conf | 1 + subsys/bluetooth/mesh/Kconfig | 5 + tests/bluetooth/mesh_shell/prj.conf | 1 + tests/bsim/bluetooth/mesh/CMakeLists.txt | 13 -- tests/bsim/bluetooth/mesh/overlay_pst.conf | 1 + tests/bsim/bluetooth/mesh/src/psa_its_emul.c | 209 ------------------ 15 files changed, 17 insertions(+), 228 deletions(-) create mode 100644 samples/bluetooth/mesh/boards/nrf5340dk_nrf5340_cpuapp_ns.conf create mode 100644 samples/bluetooth/mesh_demo/boards/nrf5340dk_nrf5340_cpuapp_ns.conf create mode 100644 samples/bluetooth/mesh_provisioner/boards/nrf5340dk_nrf5340_cpuapp_ns.conf delete mode 100644 tests/bsim/bluetooth/mesh/src/psa_its_emul.c diff --git a/modules/mbedtls/configs/config-tls-generic.h b/modules/mbedtls/configs/config-tls-generic.h index f3b0e96afcb3..eb3f943a4587 100644 --- a/modules/mbedtls/configs/config-tls-generic.h +++ b/modules/mbedtls/configs/config-tls-generic.h @@ -482,12 +482,6 @@ #define MBEDTLS_PSA_P256M_DRIVER_ENABLED #endif -#if defined(CONFIG_ARCH_POSIX) && !defined(CONFIG_PICOLIBC) && !defined(CONFIG_SECURE_STORAGE) -#define MBEDTLS_PSA_ITS_FILE_C -#define MBEDTLS_PSA_CRYPTO_STORAGE_C -#define MBEDTLS_FS_IO -#endif - #if defined(CONFIG_SECURE_STORAGE) #define MBEDTLS_PSA_CRYPTO_STORAGE_C #endif diff --git a/samples/bluetooth/mesh/boards/nrf5340dk_nrf5340_cpuapp_ns.conf b/samples/bluetooth/mesh/boards/nrf5340dk_nrf5340_cpuapp_ns.conf new file mode 100644 index 000000000000..2c00cdba0819 --- /dev/null +++ b/samples/bluetooth/mesh/boards/nrf5340dk_nrf5340_cpuapp_ns.conf @@ -0,0 +1 @@ +CONFIG_SECURE_STORAGE=n diff --git a/samples/bluetooth/mesh/prj.conf b/samples/bluetooth/mesh/prj.conf index 9c8daad91316..14b19316a866 100644 --- a/samples/bluetooth/mesh/prj.conf +++ b/samples/bluetooth/mesh/prj.conf @@ -5,6 +5,7 @@ CONFIG_FLASH=y CONFIG_FLASH_MAP=y CONFIG_NVS=y CONFIG_SETTINGS=y +CONFIG_SECURE_STORAGE=y CONFIG_HWINFO=y CONFIG_BT=y diff --git a/samples/bluetooth/mesh_demo/boards/nrf5340dk_nrf5340_cpuapp_ns.conf b/samples/bluetooth/mesh_demo/boards/nrf5340dk_nrf5340_cpuapp_ns.conf new file mode 100644 index 000000000000..2c00cdba0819 --- /dev/null +++ b/samples/bluetooth/mesh_demo/boards/nrf5340dk_nrf5340_cpuapp_ns.conf @@ -0,0 +1 @@ +CONFIG_SECURE_STORAGE=n diff --git a/samples/bluetooth/mesh_demo/prj.conf b/samples/bluetooth/mesh_demo/prj.conf index c8c52aaffce3..bcb738ae5bd1 100644 --- a/samples/bluetooth/mesh_demo/prj.conf +++ b/samples/bluetooth/mesh_demo/prj.conf @@ -31,6 +31,7 @@ CONFIG_FLASH_MAP=y CONFIG_NVS=y CONFIG_SETTINGS=y CONFIG_BT_MESH_RPL_STORE_TIMEOUT=600 +CONFIG_SECURE_STORAGE=y # Limit the number of key slots in PSA Crypto core to reduce # RAM footprint diff --git a/samples/bluetooth/mesh_provisioner/boards/nrf5340dk_nrf5340_cpuapp_ns.conf b/samples/bluetooth/mesh_provisioner/boards/nrf5340dk_nrf5340_cpuapp_ns.conf new file mode 100644 index 000000000000..2c00cdba0819 --- /dev/null +++ b/samples/bluetooth/mesh_provisioner/boards/nrf5340dk_nrf5340_cpuapp_ns.conf @@ -0,0 +1 @@ +CONFIG_SECURE_STORAGE=n diff --git a/samples/bluetooth/mesh_provisioner/prj.conf b/samples/bluetooth/mesh_provisioner/prj.conf index 8055e388df14..10949c5480db 100644 --- a/samples/bluetooth/mesh_provisioner/prj.conf +++ b/samples/bluetooth/mesh_provisioner/prj.conf @@ -45,6 +45,7 @@ CONFIG_FLASH_MAP=y CONFIG_NVS=y CONFIG_SETTINGS=y CONFIG_BT_MESH_RPL_STORE_TIMEOUT=600 +CONFIG_SECURE_STORAGE=y #CONFIG_BT_MESH_LOG_LEVEL_DBG=y #CONFIG_BT_MESH_SETTINGS_LOG_LEVEL_DBG=y diff --git a/samples/boards/nordic/mesh/onoff-app/prj.conf b/samples/boards/nordic/mesh/onoff-app/prj.conf index 0ad1ed47fd8d..0e67042b2653 100644 --- a/samples/boards/nordic/mesh/onoff-app/prj.conf +++ b/samples/boards/nordic/mesh/onoff-app/prj.conf @@ -9,6 +9,8 @@ CONFIG_FLASH=y CONFIG_FLASH_MAP=y CONFIG_NVS=y CONFIG_SETTINGS=y +CONFIG_SECURE_STORAGE=y + CONFIG_BT_MESH_RPL_STORE_TIMEOUT=600 CONFIG_BOOT_BANNER=y diff --git a/samples/boards/nordic/mesh/onoff_level_lighting_vnd_app/prj.conf b/samples/boards/nordic/mesh/onoff_level_lighting_vnd_app/prj.conf index 9e1fb4798752..3bb984208c70 100644 --- a/samples/boards/nordic/mesh/onoff_level_lighting_vnd_app/prj.conf +++ b/samples/boards/nordic/mesh/onoff_level_lighting_vnd_app/prj.conf @@ -7,6 +7,7 @@ CONFIG_FLASH=y CONFIG_FLASH_MAP=y CONFIG_NVS=y CONFIG_SETTINGS=y +CONFIG_SECURE_STORAGE=y CONFIG_BT_OBSERVER=y CONFIG_BT_BROADCASTER=y diff --git a/samples/boards/phytec/reel_board/mesh_badge/prj.conf b/samples/boards/phytec/reel_board/mesh_badge/prj.conf index ad6f656f3015..5367f4d1e9a6 100644 --- a/samples/boards/phytec/reel_board/mesh_badge/prj.conf +++ b/samples/boards/phytec/reel_board/mesh_badge/prj.conf @@ -66,4 +66,5 @@ CONFIG_FLASH=y CONFIG_FLASH_MAP=y CONFIG_NVS=y CONFIG_SETTINGS=y +CONFIG_SECURE_STORAGE=y CONFIG_CBPRINTF_FP_SUPPORT=y diff --git a/subsys/bluetooth/mesh/Kconfig b/subsys/bluetooth/mesh/Kconfig index a33b20f663da..193c75dea12e 100644 --- a/subsys/bluetooth/mesh/Kconfig +++ b/subsys/bluetooth/mesh/Kconfig @@ -1467,6 +1467,10 @@ config BT_MESH_SOL_ADV_XMIT endmenu # Proxy +config BT_MESH_SECURE_STORAGE + bool + depends on SECURE_STORAGE + choice BT_MESH_CRYPTO_LIB prompt "Crypto library:" default BT_MESH_USES_TFM_PSA if BUILD_WITH_TFM @@ -1506,6 +1510,7 @@ config BT_MESH_USES_MBEDTLS_PSA select PSA_WANT_ALG_SHA_256 select PSA_WANT_ALG_ECDH select PSA_WANT_ECC_SECP_R1_256 + select BT_MESH_SECURE_STORAGE if BT_SETTINGS help Use Mbed TLS as PSA Crypto API provider. This is useful on platforms that do not support TF-M. diff --git a/tests/bluetooth/mesh_shell/prj.conf b/tests/bluetooth/mesh_shell/prj.conf index 043428e06a24..2af600295680 100644 --- a/tests/bluetooth/mesh_shell/prj.conf +++ b/tests/bluetooth/mesh_shell/prj.conf @@ -14,6 +14,7 @@ CONFIG_FLASH=y CONFIG_FLASH_MAP=y CONFIG_NVS=y CONFIG_SETTINGS=y +CONFIG_SECURE_STORAGE=y CONFIG_BT=y CONFIG_BT_OBSERVER=y diff --git a/tests/bsim/bluetooth/mesh/CMakeLists.txt b/tests/bsim/bluetooth/mesh/CMakeLists.txt index 9897e5074a23..7271af88810f 100644 --- a/tests/bsim/bluetooth/mesh/CMakeLists.txt +++ b/tests/bsim/bluetooth/mesh/CMakeLists.txt @@ -29,7 +29,6 @@ if(CONFIG_SETTINGS) if(CONFIG_BT_MESH_USES_MBEDTLS_PSA) target_sources(app PRIVATE src/distribute_keyid.c - src/psa_its_emul.c ) endif() @@ -83,15 +82,3 @@ zephyr_include_directories( ${BSIM_COMPONENTS_PATH}/libUtilv1/src/ ${BSIM_COMPONENTS_PATH}/libPhyComv1/src/ ) - -# The mbedTLS PSA ITS is not thread safe. -# The issue: https://github.com/zephyrproject-rtos/zephyr/issues/59362 -# Also, it isn't possible to use "native" ITS implementation since -# mbedTLS includes headers that do not exist. -# This linker option allows linking custom ITS implementation instead of -# precompiled objects from the mbedTLS library to run it in parallel. -if(CONFIG_BT_MESH_USES_MBEDTLS_PSA) - zephyr_ld_options( - ${LINKERFLAGPREFIX},--allow-multiple-definition - ) -endif() diff --git a/tests/bsim/bluetooth/mesh/overlay_pst.conf b/tests/bsim/bluetooth/mesh/overlay_pst.conf index 37b9f2b99692..52856866819d 100644 --- a/tests/bsim/bluetooth/mesh/overlay_pst.conf +++ b/tests/bsim/bluetooth/mesh/overlay_pst.conf @@ -3,6 +3,7 @@ CONFIG_BT_SETTINGS=y CONFIG_FLASH=y CONFIG_FLASH_MAP=y CONFIG_NVS=y +CONFIG_SECURE_STORAGE=y CONFIG_BT_PERIPHERAL=y CONFIG_BT_MESH_GATT_PROXY=y diff --git a/tests/bsim/bluetooth/mesh/src/psa_its_emul.c b/tests/bsim/bluetooth/mesh/src/psa_its_emul.c deleted file mode 100644 index 0598b40a3892..000000000000 --- a/tests/bsim/bluetooth/mesh/src/psa_its_emul.c +++ /dev/null @@ -1,209 +0,0 @@ -/* - * Copyright (c) 2023 Nordic Semiconductor ASA - * - * SPDX-License-Identifier: Apache-2.0 - */ -/* - * PSA ITS emulator over settings. - */ - -#include - -#include -#include <../library/psa_crypto_its.h> - -#define LOG_MODULE_NAME pts_its_emu - -#include -#include "mesh/net.h" -#include "mesh/settings.h" - -LOG_MODULE_REGISTER(LOG_MODULE_NAME, LOG_LEVEL_INF); - -/* The value of 52 bytes was measured practically in the mbedTLS psa security storage. */ -#define MAX_ITEM_LENGTH 52 -#define MAX_ITEM_NUMBER MBEDTLS_PSA_KEY_SLOT_COUNT - -typedef struct { - uint32_t size; - psa_storage_create_flags_t flags; - uint8_t data[MAX_ITEM_LENGTH]; -} psa_its_pst_item_t; - -typedef struct { - psa_storage_uid_t uid; - psa_its_pst_item_t pst_item; -} psa_its_item_t; - -static psa_its_item_t item[MAX_ITEM_NUMBER]; - -static psa_its_item_t *get_item_by_uid(psa_storage_uid_t uid) -{ - for (int i = 0; i < MAX_ITEM_NUMBER; i++) { - if (uid == item[i].uid) { - return &item[i]; - } - } - - return NULL; -} - -static int itsemul_set(const char *name, size_t len_rd, settings_read_cb read_cb, void *cb_arg) -{ - ssize_t len; - uint64_t uid; - psa_its_item_t *p_item; - - LOG_DBG("read out uid: %s", name); - - if (!name) { - LOG_ERR("Insufficient number of arguments"); - return -ENOENT; - } - - uid = strtoull(name, NULL, 10); - if (uid == ULLONG_MAX) { - LOG_ERR("Invalid format for uid"); - return -EINVAL; - } - - p_item = get_item_by_uid(uid); - if (p_item == NULL) { - p_item = get_item_by_uid(0ull); - } - - if (p_item == NULL) { - LOG_ERR("Insufficient sources for %llu", uid); - return -EINVAL; - } - - p_item->uid = uid; - - len = read_cb(cb_arg, &p_item->pst_item, len_rd); - if (len < 0) { - LOG_ERR("Failed to read value (err %zd)", len); - return -EINVAL; - } - - LOG_HEXDUMP_DBG(&p_item->pst_item, len, "pst_item:"); - - if (len != len_rd) { - LOG_ERR("Unexpected length (%zd != %zu)", len, len_rd); - return -EINVAL; - } - - return 0; -} - -SETTINGS_STATIC_HANDLER_DEFINE(psa_its_emu, "itsemul", NULL, itsemul_set, NULL, NULL); - -psa_status_t psa_its_get_info(psa_storage_uid_t uid, struct psa_storage_info_t *p_info) -{ - psa_its_item_t *p_item; - - LOG_DBG("get info uid: %llu", uid); - - p_item = get_item_by_uid(uid); - if (p_item == NULL) { - return PSA_ERROR_DOES_NOT_EXIST; - } - - p_info->flags = p_item->pst_item.flags; - p_info->size = p_item->pst_item.size; - - LOG_DBG("flags: %lu, size: %lu", p_info->flags, p_info->size); - - return PSA_SUCCESS; -} - -psa_status_t psa_its_get(psa_storage_uid_t uid, uint32_t data_offset, uint32_t data_length, - void *p_data, size_t *p_data_length) -{ - psa_its_item_t *p_item; - psa_its_pst_item_t *p_pst_item; - - LOG_DBG("get uid: %llu", uid); - - p_item = get_item_by_uid(uid); - if (p_item == NULL) { - return PSA_ERROR_DOES_NOT_EXIST; - } - - p_pst_item = &p_item->pst_item; - - if (data_offset > p_pst_item->size) { - return PSA_ERROR_DATA_CORRUPT; - } - - *p_data_length = MIN(p_pst_item->size - data_offset, data_length); - memcpy(p_data, p_pst_item->data + data_offset, *p_data_length); - - return PSA_SUCCESS; -} - -psa_status_t psa_its_set(psa_storage_uid_t uid, uint32_t data_length, const void *p_data, - psa_storage_create_flags_t create_flags) -{ - char path[40]; - psa_its_item_t *p_item; - psa_its_pst_item_t *p_pst_item; - psa_status_t status = PSA_SUCCESS; - - LOG_DBG("Set uid: %llu, len: %lu", uid, data_length); - - if (data_length > MAX_ITEM_LENGTH) { - LOG_ERR("Too long item data: %lu > " STRINGIFY(MAX_ITEM_LENGTH), data_length); - } - - p_item = get_item_by_uid(uid); - if (p_item == NULL) { - p_item = get_item_by_uid(0ull); - } - - if (p_item == NULL) { - return PSA_ERROR_STORAGE_FAILURE; - } - - snprintk(path, sizeof(path), "itsemul/%llu", uid); - - p_item->uid = uid; - p_pst_item = &p_item->pst_item; - p_pst_item->size = data_length; - p_pst_item->flags = create_flags; - memcpy(p_pst_item->data, p_data, data_length); - - if (settings_save_one(path, p_pst_item, sizeof(psa_its_pst_item_t))) { - LOG_ERR("Failed to store its item: %s", path); - status = PSA_ERROR_STORAGE_FAILURE; - } else { - LOG_DBG("Stored its item: %s", path); - } - - return status; -} - -psa_status_t psa_its_remove(psa_storage_uid_t uid) -{ - char path[40]; - psa_status_t status = PSA_SUCCESS; - psa_its_item_t *p_item; - - LOG_DBG("remove uid: %llu", uid); - - p_item = get_item_by_uid(uid); - if (p_item == NULL) { - return status; - } - memset(p_item, 0, sizeof(psa_its_item_t)); - - snprintk(path, sizeof(path), "itsemul/%llu", uid); - - if (settings_delete(path)) { - LOG_ERR("Failed to remove its item: %s", path); - status = PSA_ERROR_STORAGE_FAILURE; - } else { - LOG_DBG("Removed its item: %s", path); - } - - return status; -} From 6edb3af04bd8302852cbbb652b27ff87aeb37cd6 Mon Sep 17 00:00:00 2001 From: alperen sener Date: Fri, 10 Jan 2025 13:20:25 +0100 Subject: [PATCH 07/49] [nrf fromtree] tests: bluetooth: tester: Increase number of PSA key slots for mesh MESH PTS tests require different number of keys, as an example; only one netkey requires 8 key slots and there are test cases that use at least two netkeys which consume the default 16 slots, so it is better to have enough slots all times. Setting slot count to 32. Signed-off-by: alperen sener (cherry picked from commit 35aea4909658dd5294889c5f268aa2d6873a69c0) Signed-off-by: alperen sener --- tests/bluetooth/tester/overlay-mesh.conf | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/tests/bluetooth/tester/overlay-mesh.conf b/tests/bluetooth/tester/overlay-mesh.conf index db30d4235778..815f49524971 100644 --- a/tests/bluetooth/tester/overlay-mesh.conf +++ b/tests/bluetooth/tester/overlay-mesh.conf @@ -1,3 +1,8 @@ +# Different test cases require different number of +# PSA key slots; in order to make sure there is always +# enough key slots allocating 32 slots. +CONFIG_MBEDTLS_PSA_KEY_SLOT_COUNT=32 + CONFIG_BT_MESH=y CONFIG_BT_MESH_RELAY=y CONFIG_BT_MESH_PB_ADV=y From bedb272a865d5661eaf94395d0d6a8c82905cd58 Mon Sep 17 00:00:00 2001 From: Pavel Vasilyev Date: Tue, 14 Jan 2025 09:52:53 +0100 Subject: [PATCH 08/49] [nrf fromtree] bluetooth: mesh: Remove assertion for Received List message PDU size According to MshDFUv1.0, section 6.2.3.5, the Firmware Distribution Receivers List message is not limited by the size of the receivers list: ``` The number of selected entries shall be limited by the following: - The number of entries shall not exceed the value of the Entries Limit field from the Firmware Distribution Receivers Get message. - The number of entries shall not cause the message payload to exceed the maximum Access PDU size. ``` Thus, this assertion is incorrect as it doesn't allow to have the receivers list bigger than number of maximal Access PDU size. Signed-off-by: Pavel Vasilyev (cherry picked from commit 6463d15a6653a8e8ecad3e16f917f60a3f5f9c5e) Signed-off-by: alperen sener --- subsys/bluetooth/mesh/dfd_srv.c | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/subsys/bluetooth/mesh/dfd_srv.c b/subsys/bluetooth/mesh/dfd_srv.c index 1b640739e50a..1aebb51ba3d7 100644 --- a/subsys/bluetooth/mesh/dfd_srv.c +++ b/subsys/bluetooth/mesh/dfd_srv.c @@ -35,12 +35,8 @@ BUILD_ASSERT((DFD_UPLOAD_START_MSG_MAXLEN + BT_MESH_MODEL_OP_LEN(BT_MESH_DFD_OP_ "The Firmware Distribution Upload Start message does not fit into the maximum " "incoming SDU size."); -#define DFD_RECEIVERS_LIST_MSG_MAXLEN (4 + CONFIG_BT_MESH_DFD_SRV_TARGETS_MAX * 5) - -BUILD_ASSERT((DFD_RECEIVERS_LIST_MSG_MAXLEN + BT_MESH_MODEL_OP_LEN(BT_MESH_DFD_OP_RECEIVERS_LIST) + - BT_MESH_MIC_SHORT) <= BT_MESH_TX_SDU_MAX, - "The Firmware Distribution Receivers List message does not fit into the maximum " - "outgoing SDU size."); +#define DFD_RECEIVERS_LIST_MSG_MAXLEN (BT_MESH_TX_SDU_MAX - BT_MESH_MIC_SHORT - \ + BT_MESH_MODEL_OP_LEN(BT_MESH_DFD_OP_RECEIVERS_LIST)) #define DFD_RECEIVERS_ADD_MSG_MAXLEN (CONFIG_BT_MESH_DFD_SRV_TARGETS_MAX * 3) From 1c57b77e201da878b82e7724fa0ea80082157277 Mon Sep 17 00:00:00 2001 From: Stine Akredalen Date: Tue, 14 Jan 2025 21:10:28 +0100 Subject: [PATCH 09/49] [nrf fromtree] Bluetooth: host: Add overlays for bsim tests Removed similar prj.conf files and added overlays instead to avoid duplicate code. Renamed some test files for consistency. Signed-off-by: Stine Akredalen (cherry picked from commit 724d32cd7ccb5e1863ac1218f33cc2bab110c22a) Signed-off-by: alperen sener --- .../ccc_store/overlay-no_store_on_write.conf | 2 + .../bluetooth/host/gatt/ccc_store/prj_2.conf | 24 -------- ...cc_store_2.sh => ccc_no_store_on_write.sh} | 4 +- .../host/gatt/ccc_store/testcase.yaml | 4 +- .../{prj_2.conf => overlay-privacy.conf} | 0 ...ings_2.sh => run_gatt_settings_privacy.sh} | 40 ++++++------- .../host/gatt/settings/testcase.yaml | 6 +- .../host/l2cap/credits/overlay-ecred.conf | 1 + .../host/l2cap/credits/prj_ecred.conf | 21 ------- .../host/l2cap/credits/testcase.yaml | 5 +- .../tests_scripts/l2cap_credits_ecred.sh | 2 +- .../l2cap/credits_seg_recv/overlay-ecred.conf | 1 + .../l2cap/credits_seg_recv/prj_ecred.conf | 23 -------- .../host/l2cap/credits_seg_recv/testcase.yaml | 5 +- .../l2cap_credits_seg_recv_ecred.sh | 2 +- .../l2cap/send_on_connect/overlay-ecred.conf | 1 + .../host/l2cap/send_on_connect/prj_ecred.conf | 10 ---- .../host/l2cap/send_on_connect/testcase.yaml | 4 +- .../send_on_connect/tests_scripts/l2cap.sh | 6 +- .../host/l2cap/stress/overlay-nofrag.conf | 5 ++ .../host/l2cap/stress/overlay-syswq.conf | 1 + .../host/l2cap/stress/prj_nofrag.conf | 47 --------------- .../host/l2cap/stress/prj_syswq.conf | 57 ------------------- .../bluetooth/host/l2cap/stress/testcase.yaml | 10 ++-- .../stress/tests_scripts/l2cap_nofrag.sh | 2 +- .../l2cap/stress/tests_scripts/l2cap_syswq.sh | 2 +- .../{prj_2.conf => overlay-no_lazy_load.conf} | 0 ...update_2.sh => ccc_update_no_lazy_load.sh} | 2 +- .../host/security/ccc_update/testcase.yaml | 4 +- 29 files changed, 59 insertions(+), 232 deletions(-) create mode 100644 tests/bsim/bluetooth/host/gatt/ccc_store/overlay-no_store_on_write.conf delete mode 100644 tests/bsim/bluetooth/host/gatt/ccc_store/prj_2.conf rename tests/bsim/bluetooth/host/gatt/ccc_store/test_scripts/{ccc_store_2.sh => ccc_no_store_on_write.sh} (91%) rename tests/bsim/bluetooth/host/gatt/settings/{prj_2.conf => overlay-privacy.conf} (100%) rename tests/bsim/bluetooth/host/gatt/settings/test_scripts/{run_gatt_settings_2.sh => run_gatt_settings_privacy.sh} (62%) create mode 100644 tests/bsim/bluetooth/host/l2cap/credits/overlay-ecred.conf delete mode 100644 tests/bsim/bluetooth/host/l2cap/credits/prj_ecred.conf create mode 100644 tests/bsim/bluetooth/host/l2cap/credits_seg_recv/overlay-ecred.conf delete mode 100644 tests/bsim/bluetooth/host/l2cap/credits_seg_recv/prj_ecred.conf create mode 100644 tests/bsim/bluetooth/host/l2cap/send_on_connect/overlay-ecred.conf delete mode 100644 tests/bsim/bluetooth/host/l2cap/send_on_connect/prj_ecred.conf create mode 100644 tests/bsim/bluetooth/host/l2cap/stress/overlay-nofrag.conf create mode 100644 tests/bsim/bluetooth/host/l2cap/stress/overlay-syswq.conf delete mode 100644 tests/bsim/bluetooth/host/l2cap/stress/prj_nofrag.conf delete mode 100644 tests/bsim/bluetooth/host/l2cap/stress/prj_syswq.conf rename tests/bsim/bluetooth/host/security/ccc_update/{prj_2.conf => overlay-no_lazy_load.conf} (100%) rename tests/bsim/bluetooth/host/security/ccc_update/test_scripts/{ccc_update_2.sh => ccc_update_no_lazy_load.sh} (97%) diff --git a/tests/bsim/bluetooth/host/gatt/ccc_store/overlay-no_store_on_write.conf b/tests/bsim/bluetooth/host/gatt/ccc_store/overlay-no_store_on_write.conf new file mode 100644 index 000000000000..dd3dedec6ac2 --- /dev/null +++ b/tests/bsim/bluetooth/host/gatt/ccc_store/overlay-no_store_on_write.conf @@ -0,0 +1,2 @@ +# Test that CCC is stored even if BT_SETTINGS_DELAYED_STORE is enabled +CONFIG_BT_SETTINGS_CCC_STORE_ON_WRITE=n diff --git a/tests/bsim/bluetooth/host/gatt/ccc_store/prj_2.conf b/tests/bsim/bluetooth/host/gatt/ccc_store/prj_2.conf deleted file mode 100644 index 82fa0d0c1557..000000000000 --- a/tests/bsim/bluetooth/host/gatt/ccc_store/prj_2.conf +++ /dev/null @@ -1,24 +0,0 @@ -CONFIG_BT=y -CONFIG_BT_CENTRAL=y -CONFIG_BT_PERIPHERAL=y -CONFIG_BT_DEVICE_NAME="CCC Store Test" - -CONFIG_LOG=y - -CONFIG_BT_EXT_ADV=y -CONFIG_BT_MAX_CONN=3 -CONFIG_BT_GATT_CLIENT=y - -CONFIG_BT_SMP=y - -CONFIG_SETTINGS=y -CONFIG_BT_SETTINGS=y -CONFIG_FLASH=y -CONFIG_NVS=y -CONFIG_FLASH_MAP=y -CONFIG_SETTINGS_NVS=y - -CONFIG_ASSERT=y - -# Test that CCC is stored even if BT_SETTINGS_DELAYED_STORE is enabled -CONFIG_BT_SETTINGS_CCC_STORE_ON_WRITE=n diff --git a/tests/bsim/bluetooth/host/gatt/ccc_store/test_scripts/ccc_store_2.sh b/tests/bsim/bluetooth/host/gatt/ccc_store/test_scripts/ccc_no_store_on_write.sh similarity index 91% rename from tests/bsim/bluetooth/host/gatt/ccc_store/test_scripts/ccc_store_2.sh rename to tests/bsim/bluetooth/host/gatt/ccc_store/test_scripts/ccc_no_store_on_write.sh index 29befb866026..97b6631b1a4c 100755 --- a/tests/bsim/bluetooth/host/gatt/ccc_store/test_scripts/ccc_store_2.sh +++ b/tests/bsim/bluetooth/host/gatt/ccc_store/test_scripts/ccc_no_store_on_write.sh @@ -4,8 +4,8 @@ source ${ZEPHYR_BASE}/tests/bsim/sh_common.source -test_exe="bs_${BOARD_TS}_$(guess_test_long_name)_prj_2_conf" -simulation_id="ccc_store_2" +test_exe="bs_${BOARD_TS}_$(guess_test_long_name)_overlay-no_store_on_write_conf" +simulation_id="ccc_no_store_on_write" verbosity_level=2 EXECUTE_TIMEOUT=60 diff --git a/tests/bsim/bluetooth/host/gatt/ccc_store/testcase.yaml b/tests/bsim/bluetooth/host/gatt/ccc_store/testcase.yaml index f285f4c014c0..12ab2fc7036b 100644 --- a/tests/bsim/bluetooth/host/gatt/ccc_store/testcase.yaml +++ b/tests/bsim/bluetooth/host/gatt/ccc_store/testcase.yaml @@ -12,6 +12,6 @@ tests: bsim_exe_name: tests_bsim_bluetooth_host_gatt_ccc_store_prj_conf bluetooth.host.gatt.ccc_store_2: harness_config: - bsim_exe_name: tests_bsim_bluetooth_host_gatt_ccc_store_prj_2_conf + bsim_exe_name: tests_bsim_bluetooth_host_gatt_ccc_store_overlay-no_store_on_write_conf extra_args: - EXTRA_CONF_FILE=prj_2.conf + EXTRA_CONF_FILE=overlay-no_store_on_write.conf diff --git a/tests/bsim/bluetooth/host/gatt/settings/prj_2.conf b/tests/bsim/bluetooth/host/gatt/settings/overlay-privacy.conf similarity index 100% rename from tests/bsim/bluetooth/host/gatt/settings/prj_2.conf rename to tests/bsim/bluetooth/host/gatt/settings/overlay-privacy.conf diff --git a/tests/bsim/bluetooth/host/gatt/settings/test_scripts/run_gatt_settings_2.sh b/tests/bsim/bluetooth/host/gatt/settings/test_scripts/run_gatt_settings_privacy.sh similarity index 62% rename from tests/bsim/bluetooth/host/gatt/settings/test_scripts/run_gatt_settings_2.sh rename to tests/bsim/bluetooth/host/gatt/settings/test_scripts/run_gatt_settings_privacy.sh index b4d3d16b01ff..94e660dfecd5 100755 --- a/tests/bsim/bluetooth/host/gatt/settings/test_scripts/run_gatt_settings_2.sh +++ b/tests/bsim/bluetooth/host/gatt/settings/test_scripts/run_gatt_settings_privacy.sh @@ -5,15 +5,15 @@ set -eu source ${ZEPHYR_BASE}/tests/bsim/sh_common.source -simulation_id="settings_2" +simulation_id="settings_privacy" verbosity_level=2 EXECUTE_TIMEOUT=120 -test_2_exe="./bs_${BOARD_TS}_$(guess_test_long_name)_prj_2_conf" +test_priv_exe="./bs_${BOARD_TS}_$(guess_test_long_name)_overlay-privacy_conf" cd ${BSIM_OUT_PATH}/bin # Remove the files used by the custom SETTINGS backend -TO_DELETE="${simulation_id}_server_2.log ${simulation_id}_client_2.log" +TO_DELETE="${simulation_id}_server_priv.log ${simulation_id}_client_priv.log" echo "remove settings files ${TO_DELETE}" rm ${TO_DELETE} || true @@ -26,22 +26,22 @@ Execute ./bs_2G4_phy_v1 -v=${verbosity_level} -s="${simulation_id}" -D=8 -sim_le # # Each device will wait until the previous instance (called 'test round') has # finished executing before starting up. -Execute "$test_2_exe" -v=${verbosity_level} \ - -s="${simulation_id}" -d=0 -testid=server -RealEncryption=1 -argstest 0 6 "server_2" -Execute "$test_2_exe" -v=${verbosity_level} \ - -s="${simulation_id}" -d=1 -testid=server -RealEncryption=1 -argstest 1 6 "server_2" -Execute "$test_2_exe" -v=${verbosity_level} \ - -s="${simulation_id}" -d=2 -testid=server -RealEncryption=1 -argstest 2 6 "server_2" -Execute "$test_2_exe" -v=${verbosity_level} \ - -s="${simulation_id}" -d=3 -testid=server -RealEncryption=1 -argstest 3 6 "server_2" -Execute "$test_2_exe" -v=${verbosity_level} \ - -s="${simulation_id}" -d=4 -testid=server -RealEncryption=1 -argstest 4 6 "server_2" -Execute "$test_2_exe" -v=${verbosity_level} \ - -s="${simulation_id}" -d=5 -testid=server -RealEncryption=1 -argstest 5 6 "server_2" -Execute "$test_2_exe" -v=${verbosity_level} \ - -s="${simulation_id}" -d=6 -testid=server -RealEncryption=1 -argstest 6 6 "server_2" - -Execute "$test_2_exe" -v=${verbosity_level} \ - -s="${simulation_id}" -d=7 -testid=client -RealEncryption=1 -argstest 0 0 "client_2" +Execute "$test_priv_exe" -v=${verbosity_level} \ + -s="${simulation_id}" -d=0 -testid=server -RealEncryption=1 -argstest 0 6 "server_priv" +Execute "$test_priv_exe" -v=${verbosity_level} \ + -s="${simulation_id}" -d=1 -testid=server -RealEncryption=1 -argstest 1 6 "server_priv" +Execute "$test_priv_exe" -v=${verbosity_level} \ + -s="${simulation_id}" -d=2 -testid=server -RealEncryption=1 -argstest 2 6 "server_priv" +Execute "$test_priv_exe" -v=${verbosity_level} \ + -s="${simulation_id}" -d=3 -testid=server -RealEncryption=1 -argstest 3 6 "server_priv" +Execute "$test_priv_exe" -v=${verbosity_level} \ + -s="${simulation_id}" -d=4 -testid=server -RealEncryption=1 -argstest 4 6 "server_priv" +Execute "$test_priv_exe" -v=${verbosity_level} \ + -s="${simulation_id}" -d=5 -testid=server -RealEncryption=1 -argstest 5 6 "server_priv" +Execute "$test_priv_exe" -v=${verbosity_level} \ + -s="${simulation_id}" -d=6 -testid=server -RealEncryption=1 -argstest 6 6 "server_priv" + +Execute "$test_priv_exe" -v=${verbosity_level} \ + -s="${simulation_id}" -d=7 -testid=client -RealEncryption=1 -argstest 0 0 "client_priv" wait_for_background_jobs diff --git a/tests/bsim/bluetooth/host/gatt/settings/testcase.yaml b/tests/bsim/bluetooth/host/gatt/settings/testcase.yaml index d09d5f9094c3..8d8575b2c49e 100644 --- a/tests/bsim/bluetooth/host/gatt/settings/testcase.yaml +++ b/tests/bsim/bluetooth/host/gatt/settings/testcase.yaml @@ -10,8 +10,8 @@ tests: bluetooth.host.gatt.settings: harness_config: bsim_exe_name: tests_bsim_bluetooth_host_gatt_settings_prj_conf - bluetooth.host.gatt.settings_2: + bluetooth.host.gatt.settings_priv: harness_config: - bsim_exe_name: tests_bsim_bluetooth_host_gatt_settings_prj_2_conf + bsim_exe_name: tests_bsim_bluetooth_host_gatt_settings_overlay-privacy_conf extra_args: - EXTRA_CONF_FILE=prj_2.conf + EXTRA_CONF_FILE=overlay-privacy.conf diff --git a/tests/bsim/bluetooth/host/l2cap/credits/overlay-ecred.conf b/tests/bsim/bluetooth/host/l2cap/credits/overlay-ecred.conf new file mode 100644 index 000000000000..df8a8081991a --- /dev/null +++ b/tests/bsim/bluetooth/host/l2cap/credits/overlay-ecred.conf @@ -0,0 +1 @@ +CONFIG_BT_L2CAP_ECRED=y diff --git a/tests/bsim/bluetooth/host/l2cap/credits/prj_ecred.conf b/tests/bsim/bluetooth/host/l2cap/credits/prj_ecred.conf deleted file mode 100644 index 3c0d340201be..000000000000 --- a/tests/bsim/bluetooth/host/l2cap/credits/prj_ecred.conf +++ /dev/null @@ -1,21 +0,0 @@ -CONFIG_BT=y -CONFIG_BT_CENTRAL=y -CONFIG_BT_PERIPHERAL=y -CONFIG_BT_DEVICE_NAME="L2CAP credits test" - -CONFIG_BT_L2CAP_ECRED=y - -CONFIG_BT_SMP=y # Next config depends on it -CONFIG_BT_L2CAP_DYNAMIC_CHANNEL=y - -# Disable auto-initiated procedures so they don't -# mess with the test's execution. -CONFIG_BT_AUTO_PHY_UPDATE=n -CONFIG_BT_AUTO_DATA_LEN_UPDATE=n -CONFIG_BT_GAP_AUTO_UPDATE_CONN_PARAMS=n - -CONFIG_LOG=y -CONFIG_ASSERT=y - -# Enable when the test fails -# CONFIG_BT_L2CAP_LOG_LEVEL_DBG=y diff --git a/tests/bsim/bluetooth/host/l2cap/credits/testcase.yaml b/tests/bsim/bluetooth/host/l2cap/credits/testcase.yaml index 1e59e225b811..4b7ef8127c58 100644 --- a/tests/bsim/bluetooth/host/l2cap/credits/testcase.yaml +++ b/tests/bsim/bluetooth/host/l2cap/credits/testcase.yaml @@ -12,6 +12,5 @@ tests: bsim_exe_name: tests_bsim_bluetooth_host_l2cap_credits_prj_conf bluetooth.host.l2cap.credits_ecred: harness_config: - bsim_exe_name: tests_bsim_bluetooth_host_l2cap_credits_prj_ecred_conf - extra_args: - CONF_FILE=prj_ecred.conf + bsim_exe_name: tests_bsim_bluetooth_host_l2cap_credits_prj_conf_overlay-ecred_conf + extra_args: EXTRA_CONF_FILE="overlay-ecred.conf" diff --git a/tests/bsim/bluetooth/host/l2cap/credits/tests_scripts/l2cap_credits_ecred.sh b/tests/bsim/bluetooth/host/l2cap/credits/tests_scripts/l2cap_credits_ecred.sh index 46ba8a270334..b58c32f6aab7 100755 --- a/tests/bsim/bluetooth/host/l2cap/credits/tests_scripts/l2cap_credits_ecred.sh +++ b/tests/bsim/bluetooth/host/l2cap/credits/tests_scripts/l2cap_credits_ecred.sh @@ -6,7 +6,7 @@ source ${ZEPHYR_BASE}/tests/bsim/sh_common.source verbosity_level=2 simulation_id=$(guess_test_long_name)_ecred -bsim_exe=./bs_${BOARD_TS}_$(guess_test_long_name)_prj_ecred_conf +bsim_exe=./bs_${BOARD_TS}_$(guess_test_long_name)_prj_conf_overlay-ecred_conf cd ${BSIM_OUT_PATH}/bin diff --git a/tests/bsim/bluetooth/host/l2cap/credits_seg_recv/overlay-ecred.conf b/tests/bsim/bluetooth/host/l2cap/credits_seg_recv/overlay-ecred.conf new file mode 100644 index 000000000000..df8a8081991a --- /dev/null +++ b/tests/bsim/bluetooth/host/l2cap/credits_seg_recv/overlay-ecred.conf @@ -0,0 +1 @@ +CONFIG_BT_L2CAP_ECRED=y diff --git a/tests/bsim/bluetooth/host/l2cap/credits_seg_recv/prj_ecred.conf b/tests/bsim/bluetooth/host/l2cap/credits_seg_recv/prj_ecred.conf deleted file mode 100644 index 27dd9b509123..000000000000 --- a/tests/bsim/bluetooth/host/l2cap/credits_seg_recv/prj_ecred.conf +++ /dev/null @@ -1,23 +0,0 @@ -CONFIG_BT=y -CONFIG_BT_CENTRAL=y -CONFIG_BT_PERIPHERAL=y -CONFIG_BT_DEVICE_NAME="L2CAP credits test" - -CONFIG_BT_L2CAP_ECRED=y - -CONFIG_BT_SMP=y # Next config depends on it -CONFIG_BT_L2CAP_DYNAMIC_CHANNEL=y - -# Disable auto-initiated procedures so they don't -# mess with the test's execution. -CONFIG_BT_AUTO_PHY_UPDATE=n -CONFIG_BT_AUTO_DATA_LEN_UPDATE=n -CONFIG_BT_GAP_AUTO_UPDATE_CONN_PARAMS=n - -CONFIG_LOG=y -CONFIG_ASSERT=y - -CONFIG_BT_L2CAP_SEG_RECV=y - -# Enable when the test fails -# CONFIG_BT_L2CAP_LOG_LEVEL_DBG=y diff --git a/tests/bsim/bluetooth/host/l2cap/credits_seg_recv/testcase.yaml b/tests/bsim/bluetooth/host/l2cap/credits_seg_recv/testcase.yaml index 49b754094c8e..47ecd4484ab2 100644 --- a/tests/bsim/bluetooth/host/l2cap/credits_seg_recv/testcase.yaml +++ b/tests/bsim/bluetooth/host/l2cap/credits_seg_recv/testcase.yaml @@ -12,6 +12,5 @@ tests: bsim_exe_name: tests_bsim_bluetooth_host_l2cap_credits_seg_recv_prj_conf bluetooth.host.l2cap.credits_seg_recv_ecred: harness_config: - bsim_exe_name: tests_bsim_bluetooth_host_l2cap_credits_seg_recv_prj_ecred_conf - extra_args: - CONF_FILE=prj_ecred.conf + bsim_exe_name: tests_bsim_bluetooth_host_l2cap_credits_seg_recv_prj_conf_overlay-ecred_conf + extra_args: EXTRA_CONF_FILE="overlay-ecred.conf" diff --git a/tests/bsim/bluetooth/host/l2cap/credits_seg_recv/tests_scripts/l2cap_credits_seg_recv_ecred.sh b/tests/bsim/bluetooth/host/l2cap/credits_seg_recv/tests_scripts/l2cap_credits_seg_recv_ecred.sh index 46ba8a270334..b58c32f6aab7 100755 --- a/tests/bsim/bluetooth/host/l2cap/credits_seg_recv/tests_scripts/l2cap_credits_seg_recv_ecred.sh +++ b/tests/bsim/bluetooth/host/l2cap/credits_seg_recv/tests_scripts/l2cap_credits_seg_recv_ecred.sh @@ -6,7 +6,7 @@ source ${ZEPHYR_BASE}/tests/bsim/sh_common.source verbosity_level=2 simulation_id=$(guess_test_long_name)_ecred -bsim_exe=./bs_${BOARD_TS}_$(guess_test_long_name)_prj_ecred_conf +bsim_exe=./bs_${BOARD_TS}_$(guess_test_long_name)_prj_conf_overlay-ecred_conf cd ${BSIM_OUT_PATH}/bin diff --git a/tests/bsim/bluetooth/host/l2cap/send_on_connect/overlay-ecred.conf b/tests/bsim/bluetooth/host/l2cap/send_on_connect/overlay-ecred.conf new file mode 100644 index 000000000000..df8a8081991a --- /dev/null +++ b/tests/bsim/bluetooth/host/l2cap/send_on_connect/overlay-ecred.conf @@ -0,0 +1 @@ +CONFIG_BT_L2CAP_ECRED=y diff --git a/tests/bsim/bluetooth/host/l2cap/send_on_connect/prj_ecred.conf b/tests/bsim/bluetooth/host/l2cap/send_on_connect/prj_ecred.conf deleted file mode 100644 index 6d4180031747..000000000000 --- a/tests/bsim/bluetooth/host/l2cap/send_on_connect/prj_ecred.conf +++ /dev/null @@ -1,10 +0,0 @@ -CONFIG_BT=y -CONFIG_BT_CENTRAL=y -CONFIG_BT_PERIPHERAL=y -CONFIG_BT_SMP=y -CONFIG_BT_L2CAP_DYNAMIC_CHANNEL=y -CONFIG_BT_L2CAP_ECRED=y - -CONFIG_BT_BUF_ACL_RX_SIZE=75 - -CONFIG_ASSERT=y diff --git a/tests/bsim/bluetooth/host/l2cap/send_on_connect/testcase.yaml b/tests/bsim/bluetooth/host/l2cap/send_on_connect/testcase.yaml index 6604121a3bec..36ee2712a43d 100644 --- a/tests/bsim/bluetooth/host/l2cap/send_on_connect/testcase.yaml +++ b/tests/bsim/bluetooth/host/l2cap/send_on_connect/testcase.yaml @@ -13,6 +13,6 @@ tests: bsim_exe_name: tests_bsim_bluetooth_host_l2cap_send_on_connect_prj_conf bluetooth.host.l2cap.send_on_connect_ecred: harness_config: - bsim_exe_name: tests_bsim_bluetooth_host_l2cap_send_on_connect_prj_ecred_conf + bsim_exe_name: tests_bsim_bluetooth_host_l2cap_send_on_connect_prj_conf_overlay-ecred_conf extra_args: - CONF_FILE=prj_ecred.conf + EXTRA_CONF_FILE=overlay-ecred.conf diff --git a/tests/bsim/bluetooth/host/l2cap/send_on_connect/tests_scripts/l2cap.sh b/tests/bsim/bluetooth/host/l2cap/send_on_connect/tests_scripts/l2cap.sh index 32c5eebda06d..d7b8abd67ca9 100755 --- a/tests/bsim/bluetooth/host/l2cap/send_on_connect/tests_scripts/l2cap.sh +++ b/tests/bsim/bluetooth/host/l2cap/send_on_connect/tests_scripts/l2cap.sh @@ -25,10 +25,12 @@ simulation_id="l2cap_send_on_connect_ecred" cd ${BSIM_OUT_PATH}/bin -Execute ./bs_${BOARD_TS}_tests_bsim_bluetooth_host_l2cap_send_on_connect_prj_ecred_conf \ +Execute \ + ./bs_${BOARD_TS}_tests_bsim_bluetooth_host_l2cap_send_on_connect_prj_conf_overlay-ecred_conf \ -v=${verbosity_level} -s=${simulation_id} -d=0 -testid=central -Execute ./bs_${BOARD_TS}_tests_bsim_bluetooth_host_l2cap_send_on_connect_prj_ecred_conf \ +Execute \ + ./bs_${BOARD_TS}_tests_bsim_bluetooth_host_l2cap_send_on_connect_prj_conf_overlay-ecred_conf \ -v=${verbosity_level} -s=${simulation_id} -d=1 -testid=peripheral Execute ./bs_2G4_phy_v1 -v=${verbosity_level} -s=${simulation_id} \ diff --git a/tests/bsim/bluetooth/host/l2cap/stress/overlay-nofrag.conf b/tests/bsim/bluetooth/host/l2cap/stress/overlay-nofrag.conf new file mode 100644 index 000000000000..52e940daeff0 --- /dev/null +++ b/tests/bsim/bluetooth/host/l2cap/stress/overlay-nofrag.conf @@ -0,0 +1,5 @@ +# Send L2CAP PDUs without any fragmentation. +CONFIG_BT_BUF_ACL_TX_SIZE=81 + +CONFIG_BT_L2CAP_TX_BUF_COUNT=100 +CONFIG_BT_CTLR_DATA_LENGTH_MAX=81 diff --git a/tests/bsim/bluetooth/host/l2cap/stress/overlay-syswq.conf b/tests/bsim/bluetooth/host/l2cap/stress/overlay-syswq.conf new file mode 100644 index 000000000000..2029d6edf955 --- /dev/null +++ b/tests/bsim/bluetooth/host/l2cap/stress/overlay-syswq.conf @@ -0,0 +1 @@ +CONFIG_BT_RECV_WORKQ_SYS=y diff --git a/tests/bsim/bluetooth/host/l2cap/stress/prj_nofrag.conf b/tests/bsim/bluetooth/host/l2cap/stress/prj_nofrag.conf deleted file mode 100644 index ea9cda13529d..000000000000 --- a/tests/bsim/bluetooth/host/l2cap/stress/prj_nofrag.conf +++ /dev/null @@ -1,47 +0,0 @@ -CONFIG_BT=y -CONFIG_BT_CENTRAL=y -CONFIG_BT_PERIPHERAL=y -CONFIG_BT_DEVICE_NAME="L2CAP stress test" - -CONFIG_BT_EATT=n -CONFIG_BT_L2CAP_ECRED=n - -CONFIG_BT_SMP=y # Next config depends on it -CONFIG_BT_L2CAP_DYNAMIC_CHANNEL=y - -# Disable auto-initiated procedures so they don't -# mess with the test's execution. -CONFIG_BT_AUTO_PHY_UPDATE=n -CONFIG_BT_AUTO_DATA_LEN_UPDATE=n -CONFIG_BT_GAP_AUTO_UPDATE_CONN_PARAMS=n - -# L2CAP MPS -# 23+27+27=77 makes exactly three full packets -CONFIG_BT_L2CAP_TX_MTU=77 - -# Send L2CAP PDUs without any fragmentation. -CONFIG_BT_BUF_ACL_TX_SIZE=81 - -CONFIG_BT_BUF_ACL_TX_COUNT=4 - -# The minimum value for this is -# L2AP MPS + L2CAP header (4) -CONFIG_BT_BUF_ACL_RX_SIZE=81 - -CONFIG_BT_L2CAP_TX_BUF_COUNT=100 - -CONFIG_BT_CTLR_DATA_LENGTH_MAX=81 -CONFIG_BT_CTLR_RX_BUFFERS=10 - -CONFIG_BT_MAX_CONN=10 - -CONFIG_LOG=y -CONFIG_ASSERT=y -CONFIG_NET_BUF_POOL_USAGE=y - -# CONFIG_BT_L2CAP_LOG_LEVEL_DBG=y -# CONFIG_BT_CONN_LOG_LEVEL_DBG=y -CONFIG_LOG_THREAD_ID_PREFIX=y -CONFIG_THREAD_NAME=y - -CONFIG_ARCH_POSIX_TRAP_ON_FATAL=y diff --git a/tests/bsim/bluetooth/host/l2cap/stress/prj_syswq.conf b/tests/bsim/bluetooth/host/l2cap/stress/prj_syswq.conf deleted file mode 100644 index 3cddbe332cde..000000000000 --- a/tests/bsim/bluetooth/host/l2cap/stress/prj_syswq.conf +++ /dev/null @@ -1,57 +0,0 @@ -CONFIG_BT=y -CONFIG_BT_CENTRAL=y -CONFIG_BT_PERIPHERAL=y -CONFIG_BT_DEVICE_NAME="L2CAP stress test" - -CONFIG_BT_EATT=n -CONFIG_BT_L2CAP_ECRED=n - -CONFIG_BT_SMP=y # Next config depends on it -CONFIG_BT_L2CAP_DYNAMIC_CHANNEL=y - -# Disable auto-initiated procedures so they don't -# mess with the test's execution. -CONFIG_BT_AUTO_PHY_UPDATE=n -CONFIG_BT_AUTO_DATA_LEN_UPDATE=n -CONFIG_BT_GAP_AUTO_UPDATE_CONN_PARAMS=n - -# L2CAP MPS -# 23+27+27=77 makes exactly three full packets -CONFIG_BT_L2CAP_TX_MTU=77 - -# Use this to send L2CAP PDUs without any fragmentation. -# In this particular case, we prefer fragmenting to test that code path. -# CONFIG_BT_BUF_ACL_TX_SIZE=81 - -# L2CAP PDUs will be fragmented in 3 ACL packets. -CONFIG_BT_BUF_ACL_TX_SIZE=27 - -CONFIG_BT_BUF_ACL_TX_COUNT=4 - -# The minimum value for this is -# L2AP MPS + L2CAP header (4) -CONFIG_BT_BUF_ACL_RX_SIZE=81 - -# Governs BT_CONN_TX_MAX, and so must be >= than the max number of -# peers, since we attempt to send one SDU per peer. The test execution -# is a bit slowed down by having this at the very minimum, but we want -# to keep it that way as to stress the stack as much as possible. -CONFIG_BT_L2CAP_TX_BUF_COUNT=6 - -CONFIG_BT_CTLR_DATA_LENGTH_MAX=27 -CONFIG_BT_CTLR_RX_BUFFERS=10 - -CONFIG_BT_MAX_CONN=10 - -CONFIG_LOG=y -CONFIG_ASSERT=y -CONFIG_NET_BUF_POOL_USAGE=y - -# CONFIG_BT_L2CAP_LOG_LEVEL_DBG=y -# CONFIG_BT_CONN_LOG_LEVEL_DBG=y -CONFIG_LOG_THREAD_ID_PREFIX=y -CONFIG_THREAD_NAME=y - -CONFIG_ARCH_POSIX_TRAP_ON_FATAL=y - -CONFIG_BT_RECV_WORKQ_SYS=y diff --git a/tests/bsim/bluetooth/host/l2cap/stress/testcase.yaml b/tests/bsim/bluetooth/host/l2cap/stress/testcase.yaml index 087f2f3ba505..f872f5873dae 100644 --- a/tests/bsim/bluetooth/host/l2cap/stress/testcase.yaml +++ b/tests/bsim/bluetooth/host/l2cap/stress/testcase.yaml @@ -12,11 +12,9 @@ tests: bsim_exe_name: tests_bsim_bluetooth_host_l2cap_stress_prj_conf bluetooth.host.l2cap.stress_nofrag: harness_config: - bsim_exe_name: tests_bsim_bluetooth_host_l2cap_stress_prj_nofrag_conf - extra_args: - CONF_FILE=prj_nofrag.conf + bsim_exe_name: tests_bsim_bluetooth_host_l2cap_stress_prj_conf_overlay-nofrag_conf + extra_args: EXTRA_CONF_FILE="overlay-nofrag.conf" bluetooth.host.l2cap.stress_syswq: harness_config: - bsim_exe_name: tests_bsim_bluetooth_host_l2cap_stress_prj_syswq_conf - extra_args: - CONF_FILE=prj_syswq.conf + bsim_exe_name: tests_bsim_bluetooth_host_l2cap_stress_prj_conf_overlay-syswq_conf + extra_args: EXTRA_CONF_FILE="overlay-syswq.conf" diff --git a/tests/bsim/bluetooth/host/l2cap/stress/tests_scripts/l2cap_nofrag.sh b/tests/bsim/bluetooth/host/l2cap/stress/tests_scripts/l2cap_nofrag.sh index 4d4bb7f9152f..7183d4b01631 100755 --- a/tests/bsim/bluetooth/host/l2cap/stress/tests_scripts/l2cap_nofrag.sh +++ b/tests/bsim/bluetooth/host/l2cap/stress/tests_scripts/l2cap_nofrag.sh @@ -9,7 +9,7 @@ simulation_id="l2cap_stress_nofrag" verbosity_level=2 EXECUTE_TIMEOUT=240 -bsim_exe=./bs_${BOARD_TS}_tests_bsim_bluetooth_host_l2cap_stress_prj_nofrag_conf +bsim_exe=./bs_${BOARD_TS}_tests_bsim_bluetooth_host_l2cap_stress_prj_conf_overlay-nofrag_conf cd ${BSIM_OUT_PATH}/bin diff --git a/tests/bsim/bluetooth/host/l2cap/stress/tests_scripts/l2cap_syswq.sh b/tests/bsim/bluetooth/host/l2cap/stress/tests_scripts/l2cap_syswq.sh index b6e24b067801..57fcf1747b68 100755 --- a/tests/bsim/bluetooth/host/l2cap/stress/tests_scripts/l2cap_syswq.sh +++ b/tests/bsim/bluetooth/host/l2cap/stress/tests_scripts/l2cap_syswq.sh @@ -9,7 +9,7 @@ simulation_id="l2cap_stress_syswq" verbosity_level=2 EXECUTE_TIMEOUT=240 -bsim_exe=./bs_${BOARD_TS}_tests_bsim_bluetooth_host_l2cap_stress_prj_syswq_conf +bsim_exe=./bs_${BOARD_TS}_tests_bsim_bluetooth_host_l2cap_stress_prj_conf_overlay-syswq_conf cd ${BSIM_OUT_PATH}/bin diff --git a/tests/bsim/bluetooth/host/security/ccc_update/prj_2.conf b/tests/bsim/bluetooth/host/security/ccc_update/overlay-no_lazy_load.conf similarity index 100% rename from tests/bsim/bluetooth/host/security/ccc_update/prj_2.conf rename to tests/bsim/bluetooth/host/security/ccc_update/overlay-no_lazy_load.conf diff --git a/tests/bsim/bluetooth/host/security/ccc_update/test_scripts/ccc_update_2.sh b/tests/bsim/bluetooth/host/security/ccc_update/test_scripts/ccc_update_no_lazy_load.sh similarity index 97% rename from tests/bsim/bluetooth/host/security/ccc_update/test_scripts/ccc_update_2.sh rename to tests/bsim/bluetooth/host/security/ccc_update/test_scripts/ccc_update_no_lazy_load.sh index eb165fa85f05..90a089f34a5c 100755 --- a/tests/bsim/bluetooth/host/security/ccc_update/test_scripts/ccc_update_2.sh +++ b/tests/bsim/bluetooth/host/security/ccc_update/test_scripts/ccc_update_no_lazy_load.sh @@ -5,7 +5,7 @@ source ${ZEPHYR_BASE}/tests/bsim/sh_common.source test_name='ccc_update' -test_exe="bs_${BOARD_TS}_tests_bsim_bluetooth_host_security_${test_name}_prj_2_conf" +test_exe="bs_${BOARD_TS}_tests_bsim_bluetooth_host_security_${test_name}_overlay-no_lazy_load_conf" simulation_id="${test_name}_2" verbosity_level=2 diff --git a/tests/bsim/bluetooth/host/security/ccc_update/testcase.yaml b/tests/bsim/bluetooth/host/security/ccc_update/testcase.yaml index 1c3176d268e7..909439ab9ab1 100644 --- a/tests/bsim/bluetooth/host/security/ccc_update/testcase.yaml +++ b/tests/bsim/bluetooth/host/security/ccc_update/testcase.yaml @@ -12,6 +12,6 @@ tests: bsim_exe_name: tests_bsim_bluetooth_host_security_ccc_update_prj_conf bluetooth.host.security.ccc_update_2: harness_config: - bsim_exe_name: tests_bsim_bluetooth_host_security_ccc_update_prj_2_conf + bsim_exe_name: tests_bsim_bluetooth_host_security_ccc_update_overlay-no_lazy_load_conf extra_args: - EXTRA_CONF_FILE=prj_2.conf + EXTRA_CONF_FILE=overlay-no_lazy_load.conf From 4db339d69047764f67b6fa38aa2a825bc56050a4 Mon Sep 17 00:00:00 2001 From: Ahmed Ahmed Date: Fri, 10 Jan 2025 13:46:42 +0100 Subject: [PATCH 10/49] [nrf fromtree] bluetooth: host: iterate over connections in TX list Iterate over the list of connections to find a connection that is able to send data, instead of returning NULL if the first connection can't send data. This solves the problem with starvation of other connections. Signed-off-by: Ahmed Ahmed (cherry picked from commit 099871622af37034cf9c144bfc3ae1e2ab94d76e) Signed-off-by: alperen sener --- subsys/bluetooth/host/conn.c | 90 ++++++++++++++++++------------------ 1 file changed, 45 insertions(+), 45 deletions(-) diff --git a/subsys/bluetooth/host/conn.c b/subsys/bluetooth/host/conn.c index 823de6052656..e361cf8ce88d 100644 --- a/subsys/bluetooth/host/conn.c +++ b/subsys/bluetooth/host/conn.c @@ -918,67 +918,67 @@ __maybe_unused static bool dont_have_methods(struct bt_conn *conn) struct bt_conn *get_conn_ready(void) { - /* Here we only peek: we pop the conn (and insert it at the back if it - * still has data) after the QoS function returns false. - */ - sys_snode_t *node = sys_slist_peek_head(&bt_dev.le.conn_ready); - - if (node == NULL) { - return NULL; - } - - /* `conn` borrows from the list node. That node is _not_ popped yet. - * - * If we end up not popping that conn off the list, we have to make sure - * to increase the refcount before returning a pointer to that - * connection out of this function. - */ - struct bt_conn *conn = CONTAINER_OF(node, struct bt_conn, _conn_ready); + struct bt_conn *conn, *tmp; + sys_snode_t *prev = NULL; if (dont_have_viewbufs()) { - /* We will get scheduled again when the (view) buffers are freed. If you - * hit this a lot, try increasing `CONFIG_BT_CONN_FRAG_COUNT` + /* We will get scheduled again when the (view) buffers are freed. If + * you hit this a lot, try increasing `CONFIG_BT_CONN_FRAG_COUNT` */ LOG_DBG("no view bufs"); return NULL; } - if (cannot_send_to_controller(conn)) { - /* We will get scheduled again when the buffers are freed. */ - LOG_DBG("no LL bufs for %p", conn); - return NULL; - } - - if (dont_have_tx_context(conn)) { - /* We will get scheduled again when TX contexts are available. */ - LOG_DBG("no TX contexts"); - return NULL; - } + SYS_SLIST_FOR_EACH_CONTAINER_SAFE(&bt_dev.le.conn_ready, conn, tmp, + _conn_ready) { + /* Iterate over the list of connections that have data to send + * and return the first one that can be sent. + */ - CHECKIF(dont_have_methods(conn)) { - LOG_DBG("conn %p (type %d) is missing mandatory methods", - conn, conn->type); + if (cannot_send_to_controller(conn)) { + /* When buffers are full, try next connection. + */ + LOG_DBG("no LL bufs for %p", conn); + prev = &conn->_conn_ready; + continue; + } - return NULL; - } + if (dont_have_tx_context(conn)) { + /* When TX contexts are not available, try next connection. + */ + LOG_DBG("no TX contexts for %p", conn); + prev = &conn->_conn_ready; + continue; + } - if (should_stop_tx(conn)) { - /* Move reference off the list and into the `conn` variable. */ - __maybe_unused sys_snode_t *s = sys_slist_get(&bt_dev.le.conn_ready); + CHECKIF(dont_have_methods(conn)) { + /* When a connection is missing mandatory methods, try next + * connection. + */ + LOG_DBG("conn %p (type %d) is missing mandatory methods", + conn, conn->type); + prev = &conn->_conn_ready; + continue; + } - __ASSERT_NO_MSG(s == node); - (void)atomic_set(&conn->_conn_ready_lock, 0); + if (should_stop_tx(conn)) { + /* Move reference off the list */ + sys_slist_remove(&bt_dev.le.conn_ready, prev, &conn->_conn_ready); + (void)atomic_set(&conn->_conn_ready_lock, 0); + bt_conn_unref(conn); - /* Append connection to list if it still has data */ - if (conn->has_data(conn)) { - LOG_DBG("appending %p to back of TX queue", conn); - bt_conn_data_ready(conn); + /* Append connection to list if it still has data */ + if (conn->has_data(conn)) { + LOG_DBG("appending %p to back of TX queue", conn); + bt_conn_data_ready(conn); + } } - return conn; + return bt_conn_ref(conn); } - return bt_conn_ref(conn); + /* No connection has data to send */ + return NULL; } /* Crazy that this file is compiled even if this is not true, but here we are. */ From 410a315b8c4d1bbcc71d193f8f886d431015c144 Mon Sep 17 00:00:00 2001 From: Ahmed Ahmed Date: Thu, 16 Jan 2025 13:49:00 +0100 Subject: [PATCH 11/49] [nrf fromtree] bluetooth: host: add asserts to foreach in get_conn_ready Assert that the previous connection in the list and the tmp element in the loop are different to the current connection. Otherwise, if the same connection was somehow added twice it could result in an infinite loop. Signed-off-by: Ahmed Ahmed (cherry picked from commit 2edddbddb54e2397f227d7fb43138c815a9fb637) Signed-off-by: alperen sener --- subsys/bluetooth/host/conn.c | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/subsys/bluetooth/host/conn.c b/subsys/bluetooth/host/conn.c index e361cf8ce88d..9bb33e327ac2 100644 --- a/subsys/bluetooth/host/conn.c +++ b/subsys/bluetooth/host/conn.c @@ -922,47 +922,44 @@ struct bt_conn *get_conn_ready(void) sys_snode_t *prev = NULL; if (dont_have_viewbufs()) { - /* We will get scheduled again when the (view) buffers are freed. If - * you hit this a lot, try increasing `CONFIG_BT_CONN_FRAG_COUNT` + /* We will get scheduled again when the (view) buffers are freed. If you + * hit this a lot, try increasing `CONFIG_BT_CONN_FRAG_COUNT` */ LOG_DBG("no view bufs"); return NULL; } - SYS_SLIST_FOR_EACH_CONTAINER_SAFE(&bt_dev.le.conn_ready, conn, tmp, - _conn_ready) { + SYS_SLIST_FOR_EACH_CONTAINER_SAFE(&bt_dev.le.conn_ready, conn, tmp, _conn_ready) { + __ASSERT_NO_MSG(tmp != conn); + /* Iterate over the list of connections that have data to send * and return the first one that can be sent. */ if (cannot_send_to_controller(conn)) { - /* When buffers are full, try next connection. - */ + /* When buffers are full, try next connection. */ LOG_DBG("no LL bufs for %p", conn); prev = &conn->_conn_ready; continue; } if (dont_have_tx_context(conn)) { - /* When TX contexts are not available, try next connection. - */ + /* When TX contexts are not available, try next connection. */ LOG_DBG("no TX contexts for %p", conn); prev = &conn->_conn_ready; continue; } CHECKIF(dont_have_methods(conn)) { - /* When a connection is missing mandatory methods, try next - * connection. - */ - LOG_DBG("conn %p (type %d) is missing mandatory methods", - conn, conn->type); + /* When a connection is missing mandatory methods, try next connection. */ + LOG_DBG("conn %p (type %d) is missing mandatory methods", conn, conn->type); prev = &conn->_conn_ready; continue; } if (should_stop_tx(conn)) { /* Move reference off the list */ + __ASSERT_NO_MSG(prev != &conn->_conn_ready); sys_slist_remove(&bt_dev.le.conn_ready, prev, &conn->_conn_ready); (void)atomic_set(&conn->_conn_ready_lock, 0); bt_conn_unref(conn); From 1ef1960cc9206954c1f39c5955f4472b8496216d Mon Sep 17 00:00:00 2001 From: Ahmed Ahmed Date: Thu, 16 Jan 2025 13:53:52 +0100 Subject: [PATCH 12/49] [nrf fromtree] bluetooth: host: fix connection reference before returning As the connection was removed from the list, we now own its reference. Keep this reference and return it to the TX processor. Signed-off-by: Ahmed Ahmed (cherry picked from commit 6a45e17b4ba73ceeeab4478d2def0b82d9b8cd5d) Signed-off-by: alperen sener --- subsys/bluetooth/host/conn.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/subsys/bluetooth/host/conn.c b/subsys/bluetooth/host/conn.c index 9bb33e327ac2..40d91c3e8d64 100644 --- a/subsys/bluetooth/host/conn.c +++ b/subsys/bluetooth/host/conn.c @@ -962,13 +962,14 @@ struct bt_conn *get_conn_ready(void) __ASSERT_NO_MSG(prev != &conn->_conn_ready); sys_slist_remove(&bt_dev.le.conn_ready, prev, &conn->_conn_ready); (void)atomic_set(&conn->_conn_ready_lock, 0); - bt_conn_unref(conn); /* Append connection to list if it still has data */ if (conn->has_data(conn)) { LOG_DBG("appending %p to back of TX queue", conn); bt_conn_data_ready(conn); } + + return conn; } return bt_conn_ref(conn); From 2db3b810c7e1d1e7c2bcc81429a3b1f7a939e375 Mon Sep 17 00:00:00 2001 From: Lingao Meng Date: Tue, 14 Jan 2025 09:54:13 +0800 Subject: [PATCH 13/49] [nrf fromtree] Bluetooth: Mesh: Fix Assert in bt_mesh_adv_unref when messages to a proxy Fixes:https://github.com/zephyrproject-rtos/zephyr/issues/83904 This solution fix is to define a separate variable for the each proxy FIFO. Signed-off-by: Lingao Meng (cherry picked from commit 6371080406eb0b621f7327be77d880d9d707800b) Signed-off-by: alperen sener --- subsys/bluetooth/mesh/adv.h | 6 +++++- subsys/bluetooth/mesh/proxy_msg.c | 24 +++++++++++++++++++++--- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/subsys/bluetooth/mesh/adv.h b/subsys/bluetooth/mesh/adv.h index 563a2118b711..2db83d592116 100644 --- a/subsys/bluetooth/mesh/adv.h +++ b/subsys/bluetooth/mesh/adv.h @@ -58,7 +58,11 @@ struct bt_mesh_adv_ctx { }; struct bt_mesh_adv { - sys_snode_t node; + void *adv_bearer; + +#if defined(CONFIG_BT_MESH_GATT) + void *gatt_bearer[CONFIG_BT_MAX_CONN]; +#endif struct bt_mesh_adv_ctx ctx; diff --git a/subsys/bluetooth/mesh/proxy_msg.c b/subsys/bluetooth/mesh/proxy_msg.c index edee3ad384cd..a4d8c8c14b2e 100644 --- a/subsys/bluetooth/mesh/proxy_msg.c +++ b/subsys/bluetooth/mesh/proxy_msg.c @@ -56,6 +56,24 @@ static struct bt_mesh_proxy_role roles[CONFIG_BT_MAX_CONN]; static int conn_count; +static void proxy_queue_put(struct bt_mesh_proxy_role *role, struct bt_mesh_adv *adv) +{ + k_fifo_put(&role->pending, &(adv->gatt_bearer[bt_conn_index(role->conn)])); +} + +static struct bt_mesh_adv *proxy_queue_get(struct bt_mesh_proxy_role *role) +{ + void *gatt_bearer; + + gatt_bearer = k_fifo_get(&role->pending, K_NO_WAIT); + if (!gatt_bearer) { + return NULL; + } + + return CONTAINER_OF(gatt_bearer, struct bt_mesh_adv, + gatt_bearer[bt_conn_index(role->conn)]); +} + static void proxy_sar_timeout(struct k_work *work) { struct bt_mesh_proxy_role *role; @@ -66,7 +84,7 @@ static void proxy_sar_timeout(struct k_work *work) role = CONTAINER_OF(dwork, struct bt_mesh_proxy_role, sar_timer); while (!k_fifo_is_empty(&role->pending)) { - struct bt_mesh_adv *adv = k_fifo_get(&role->pending, K_NO_WAIT); + struct bt_mesh_adv *adv = proxy_queue_get(role); __ASSERT_NO_MSG(adv); @@ -243,7 +261,7 @@ int bt_mesh_proxy_relay_send(struct bt_conn *conn, struct bt_mesh_adv *adv) { struct bt_mesh_proxy_role *role = &roles[bt_conn_index(conn)]; - k_fifo_put(&role->pending, bt_mesh_adv_ref(adv)); + proxy_queue_put(role, bt_mesh_adv_ref(adv)); bt_mesh_wq_submit(&role->work); @@ -259,7 +277,7 @@ static void proxy_msg_send_pending(struct k_work *work) return; } - adv = k_fifo_get(&role->pending, K_NO_WAIT); + adv = proxy_queue_get(role); if (!adv) { return; } From 389b307c488cd71d32fbee9e39a18ee9ee7aeeac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A5vard=20Reierstad?= Date: Wed, 15 Jan 2025 12:30:27 +0100 Subject: [PATCH 14/49] [nrf fromtree] Bluetooth: Host: bsim: Refactor common files MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This commit deals mainly with bsim tests which have `common.c/h` files. It does the following changes: * Use functionality from the `babbelkit` library for common functions related to flags, test progression (failing, passing etc.) and synchronization between two devices. Locally defined equivalents are removed. * Remove the `common.c/h` containing only functionality that is provided by the `babblekit` library. * Remove the `test_pre_init_f` and `test_tick_f` functions (commonly implemented as `test_init` and `test_tick`) from the modified tests. These functions are not needed as they were only used to fail the test if a device didn't complete the test within a certain time frame. This is already handled by the `sim_length` argument used in the test scripts. * Changes use of `atol` to `strtol` as the prior is against Zephyr coding guidelines. Signed-off-by: HÃ¥vard Reierstad (cherry picked from commit fe923670a9ee60487cb921c69e08e2fcaec2befa) Signed-off-by: alperen sener --- .../encrypted/css_sample_data/CMakeLists.txt | 3 + .../encrypted/css_sample_data/src/central.c | 21 ++-- .../encrypted/css_sample_data/src/common.h | 23 ---- .../adv/encrypted/css_sample_data/src/main.c | 26 ----- .../css_sample_data/src/peripheral.c | 23 ++-- .../css_sample_data/test_scripts/run_tests.sh | 4 +- .../host/adv/extended/CMakeLists.txt | 6 +- .../bluetooth/host/adv/extended/src/common.c | 24 ---- .../bluetooth/host/adv/extended/src/common.h | 53 --------- .../adv/extended/src/ext_adv_advertiser.c | 32 ++---- .../host/adv/extended/src/ext_adv_scanner.c | 39 +++---- .../host/adv/periodic/CMakeLists.txt | 3 + .../bluetooth/host/adv/periodic/src/common.c | 15 --- .../bluetooth/host/adv/periodic/src/common.h | 45 +------- .../adv/periodic/src/per_adv_advertiser.c | 44 +++----- .../host/adv/periodic/src/per_adv_sync.c | 72 ++++++------ .../adv/periodic/tests_scripts/per_adv.sh | 2 +- .../tests_scripts/per_adv_app_not_scanning.sh | 2 +- .../per_adv_app_not_scanning_coded.sh | 2 +- .../periodic/tests_scripts/per_adv_conn.sh | 2 +- .../tests_scripts/per_adv_conn_privacy.sh | 2 +- .../tests_scripts/per_adv_long_data.sh | 2 +- .../bluetooth/host/att/eatt/CMakeLists.txt | 2 + .../bsim/bluetooth/host/att/eatt/src/common.c | 79 ++----------- .../bsim/bluetooth/host/att/eatt/src/common.h | 31 ------ .../host/att/eatt/src/main_autoconnect.c | 9 +- .../host/att/eatt/src/main_collision.c | 26 ++--- .../bluetooth/host/att/eatt/src/main_lowres.c | 9 +- .../host/att/eatt/src/main_reconfigure.c | 35 +++--- .../att/eatt/tests_scripts/autoconnect.sh | 2 +- .../host/att/eatt/tests_scripts/collision.sh | 2 +- .../host/att/eatt/tests_scripts/lowres.sh | 2 +- .../att/eatt/tests_scripts/multiple_conn.sh | 2 +- .../host/att/eatt_notif/CMakeLists.txt | 3 + .../host/att/eatt_notif/src/client_test.c | 47 ++++---- .../host/att/eatt_notif/src/common.c | 67 ----------- .../host/att/eatt_notif/src/common.h | 53 --------- .../host/att/eatt_notif/src/server_test.c | 42 ++++--- .../att/eatt_notif/test_scripts/eatt_notif.sh | 2 +- .../host/gatt/authorization/CMakeLists.txt | 3 + .../host/gatt/authorization/src/common.c | 20 ---- .../host/gatt/authorization/src/common.h | 44 -------- .../gatt/authorization/src/gatt_client_test.c | 54 +++++---- .../gatt/authorization/src/gatt_server_test.c | 33 ++++-- .../gatt/authorization/test_scripts/gatt.sh | 2 +- .../host/gatt/caching/CMakeLists.txt | 3 + .../bluetooth/host/gatt/caching/src/common.c | 66 ----------- .../bluetooth/host/gatt/caching/src/common.h | 50 --------- .../host/gatt/caching/src/gatt_client_test.c | 105 +++++++++--------- .../host/gatt/caching/src/gatt_server_test.c | 43 ++++--- .../host/gatt/ccc_store/CMakeLists.txt | 4 +- .../host/gatt/ccc_store/src/central.c | 73 +++++------- .../host/gatt/ccc_store/src/common.c | 38 ------- .../host/gatt/ccc_store/src/common.h | 39 ------- .../bluetooth/host/gatt/ccc_store/src/main.c | 41 +------ .../host/gatt/ccc_store/src/peripheral.c | 63 ++++------- .../host/gatt/general/CMakeLists.txt | 3 + .../bluetooth/host/gatt/general/src/common.c | 20 ---- .../bluetooth/host/gatt/general/src/common.h | 44 -------- .../host/gatt/general/src/gatt_client_test.c | 68 +++++++----- .../host/gatt/general/src/gatt_server_test.c | 28 +++-- .../host/gatt/general/test_scripts/gatt.sh | 2 +- .../bluetooth/host/gatt/notify/CMakeLists.txt | 3 + .../bluetooth/host/gatt/notify/src/common.c | 20 ---- .../bluetooth/host/gatt/notify/src/common.h | 44 +------- .../host/gatt/notify/src/gatt_client_test.c | 74 ++++++------ .../host/gatt/notify/src/gatt_server_test.c | 46 ++++---- .../bluetooth/host/gatt/settings/src/main.c | 5 +- .../bluetooth/host/iso/bis/CMakeLists.txt | 1 - .../host/iso/bis/src/bis_broadcaster.c | 7 -- .../bluetooth/host/iso/bis/src/bis_receiver.c | 3 - .../bsim/bluetooth/host/iso/bis/src/common.c | 31 ------ .../bsim/bluetooth/host/iso/bis/src/common.h | 16 --- .../bluetooth/host/iso/cis/CMakeLists.txt | 3 + .../bluetooth/host/iso/cis/src/cis_central.c | 59 +++++----- .../host/iso/cis/src/cis_peripheral.c | 20 ++-- .../bsim/bluetooth/host/iso/cis/src/common.c | 17 +-- .../bsim/bluetooth/host/iso/cis/src/common.h | 34 ------ .../host/l2cap/credits/CMakeLists.txt | 3 + .../bluetooth/host/l2cap/credits/src/common.c | 22 ---- .../bluetooth/host/l2cap/credits/src/common.h | 57 ---------- .../bluetooth/host/l2cap/credits/src/main.c | 64 ++++++----- .../l2cap/credits_seg_recv/CMakeLists.txt | 4 +- .../host/l2cap/credits_seg_recv/src/common.c | 22 ---- .../host/l2cap/credits_seg_recv/src/common.h | 57 ---------- .../host/l2cap/credits_seg_recv/src/main.c | 67 ++++++----- .../host/l2cap/general/CMakeLists.txt | 3 + .../bluetooth/host/l2cap/general/src/common.c | 68 ------------ .../bluetooth/host/l2cap/general/src/common.h | 59 ---------- .../host/l2cap/general/src/main_l2cap_ecred.c | 89 ++++++++------- .../host/l2cap/many_conns/CMakeLists.txt | 3 + .../host/l2cap/many_conns/src/common.c | 22 ---- .../host/l2cap/many_conns/src/common.h | 57 ---------- .../host/l2cap/many_conns/src/main.c | 67 +++++------ .../host/l2cap/send_on_connect/CMakeLists.txt | 4 +- .../host/l2cap/send_on_connect/src/common.c | 22 ---- .../host/l2cap/send_on_connect/src/common.h | 44 -------- .../src/main_l2cap_send_on_connect.c | 62 +++++------ .../host/l2cap/stress/CMakeLists.txt | 3 + .../bluetooth/host/l2cap/stress/src/common.c | 22 ---- .../bluetooth/host/l2cap/stress/src/common.h | 57 ---------- .../bluetooth/host/l2cap/stress/src/main.c | 62 ++++++----- .../host/l2cap/userdata/CMakeLists.txt | 3 + .../host/l2cap/userdata/src/common.c | 22 ---- .../host/l2cap/userdata/src/common.h | 44 -------- .../l2cap/userdata/src/main_l2cap_userdata.c | 61 +++++----- .../host/misc/conn_stress/central/src/main.c | 4 +- .../misc/conn_stress/peripheral/src/main.c | 2 +- .../host/misc/disable/CMakeLists.txt | 2 + .../bluetooth/host/misc/disable/src/common.c | 20 ---- .../bluetooth/host/misc/disable/src/common.h | 48 -------- .../host/misc/disable/src/gatt_client_test.c | 61 +++++----- .../host/misc/disable/src/gatt_server_test.c | 28 +++-- .../host/misc/disable/src/main_disable.c | 30 ++--- .../misc/unregister_conn_cb/CMakeLists.txt | 3 + .../host/misc/unregister_conn_cb/src/common.c | 20 ---- .../host/misc/unregister_conn_cb/src/common.h | 55 --------- .../host/misc/unregister_conn_cb/src/main.c | 47 ++++---- .../host/security/ccc_update/CMakeLists.txt | 3 + .../host/security/ccc_update/src/central.c | 48 ++++---- .../host/security/ccc_update/src/common.h | 24 ---- .../host/security/ccc_update/src/main.c | 41 ------- .../host/security/ccc_update/src/peripheral.c | 40 +++---- 123 files changed, 1013 insertions(+), 2620 deletions(-) delete mode 100644 tests/bsim/bluetooth/host/adv/extended/src/common.c delete mode 100644 tests/bsim/bluetooth/host/adv/extended/src/common.h delete mode 100644 tests/bsim/bluetooth/host/att/eatt_notif/src/common.c delete mode 100644 tests/bsim/bluetooth/host/gatt/authorization/src/common.c delete mode 100644 tests/bsim/bluetooth/host/gatt/caching/src/common.c delete mode 100644 tests/bsim/bluetooth/host/gatt/ccc_store/src/common.c delete mode 100644 tests/bsim/bluetooth/host/gatt/general/src/common.c delete mode 100644 tests/bsim/bluetooth/host/gatt/notify/src/common.c delete mode 100644 tests/bsim/bluetooth/host/iso/bis/src/common.c delete mode 100644 tests/bsim/bluetooth/host/iso/bis/src/common.h delete mode 100644 tests/bsim/bluetooth/host/l2cap/credits/src/common.c delete mode 100644 tests/bsim/bluetooth/host/l2cap/credits/src/common.h delete mode 100644 tests/bsim/bluetooth/host/l2cap/credits_seg_recv/src/common.c delete mode 100644 tests/bsim/bluetooth/host/l2cap/credits_seg_recv/src/common.h delete mode 100644 tests/bsim/bluetooth/host/l2cap/general/src/common.c delete mode 100644 tests/bsim/bluetooth/host/l2cap/general/src/common.h delete mode 100644 tests/bsim/bluetooth/host/l2cap/many_conns/src/common.c delete mode 100644 tests/bsim/bluetooth/host/l2cap/many_conns/src/common.h delete mode 100644 tests/bsim/bluetooth/host/l2cap/send_on_connect/src/common.c delete mode 100644 tests/bsim/bluetooth/host/l2cap/send_on_connect/src/common.h delete mode 100644 tests/bsim/bluetooth/host/l2cap/stress/src/common.c delete mode 100644 tests/bsim/bluetooth/host/l2cap/stress/src/common.h delete mode 100644 tests/bsim/bluetooth/host/l2cap/userdata/src/common.c delete mode 100644 tests/bsim/bluetooth/host/l2cap/userdata/src/common.h delete mode 100644 tests/bsim/bluetooth/host/misc/disable/src/common.c delete mode 100644 tests/bsim/bluetooth/host/misc/unregister_conn_cb/src/common.c delete mode 100644 tests/bsim/bluetooth/host/misc/unregister_conn_cb/src/common.h diff --git a/tests/bsim/bluetooth/host/adv/encrypted/css_sample_data/CMakeLists.txt b/tests/bsim/bluetooth/host/adv/encrypted/css_sample_data/CMakeLists.txt index 5a644c12fe19..c60770f98111 100644 --- a/tests/bsim/bluetooth/host/adv/encrypted/css_sample_data/CMakeLists.txt +++ b/tests/bsim/bluetooth/host/adv/encrypted/css_sample_data/CMakeLists.txt @@ -5,6 +5,9 @@ cmake_minimum_required(VERSION 3.20.0) find_package(Zephyr HINTS $ENV{ZEPHYR_BASE}) project(bsim_test_ead_css_sample_data) +add_subdirectory(${ZEPHYR_BASE}/tests/bsim/babblekit babblekit) +target_link_libraries(app PRIVATE babblekit) + target_sources(app PRIVATE src/main.c src/common.c diff --git a/tests/bsim/bluetooth/host/adv/encrypted/css_sample_data/src/central.c b/tests/bsim/bluetooth/host/adv/encrypted/css_sample_data/src/central.c index ee2fa5d3da80..9a5d699806bf 100644 --- a/tests/bsim/bluetooth/host/adv/encrypted/css_sample_data/src/central.c +++ b/tests/bsim/bluetooth/host/adv/encrypted/css_sample_data/src/central.c @@ -2,6 +2,7 @@ * SPDX-License-Identifier: Apache-2.0 */ +#include "babblekit/testcase.h" #include "common.h" extern const struct test_sample_data *sample_data; @@ -19,9 +20,8 @@ static bool data_parse_cb(struct bt_data *data, void *user_data) if (decrypted_data_size != sample_data->size_ad_data) { LOG_ERR("Size of decrypted data: %d", decrypted_data_size); LOG_ERR("Size of sample data: %d", sample_data->size_ad_data); - FAIL("Computed size of data does not match the size of the data from the " - "sample. (data set %d)\n", - data_set); + TEST_FAIL("Computed size of data does not match the size of the data from " + "the sample. (data set %d)", data_set); } if (memcmp(sample_data->randomizer_little_endian, data->data, @@ -31,7 +31,7 @@ static bool data_parse_cb(struct bt_data *data, void *user_data) LOG_ERR("Expected Randomizer from sample: %s", bt_hex(sample_data->randomizer_little_endian, BT_EAD_RANDOMIZER_SIZE)); - FAIL("Received Randomizer does not match the expected one.\n"); + TEST_FAIL("Received Randomizer does not match the expected one."); } net_buf_simple_init_with_data(&decrypted_buf, decrypted_payload, @@ -40,22 +40,21 @@ static bool data_parse_cb(struct bt_data *data, void *user_data) err = bt_ead_decrypt(sample_data->session_key, sample_data->iv, data->data, data->data_len, decrypted_buf.data); if (err != 0) { - FAIL("Error during decryption.\n"); + TEST_FAIL("Error during decryption."); } else if (memcmp(decrypted_buf.data, sample_data->ad_data, decrypted_data_size)) { LOG_HEXDUMP_ERR(decrypted_buf.data, decrypted_data_size, "Decrypted data from bt_ead_decrypt:"); LOG_HEXDUMP_ERR(sample_data->ad_data, sample_data->size_ad_data, "Expected data from sample:"); - FAIL("Decrypted AD data does not match expected sample data. (data set " - "%d)\n", - data_set); + TEST_FAIL("Decrypted AD data does not match expected sample data. (data set" + " %d)", data_set); } LOG_HEXDUMP_DBG(decrypted_buf.data, decrypted_data_size, "Raw decrypted data: "); bt_data_parse(&decrypted_buf, &data_parse_cb, NULL); - PASS("Central test passed. (data set %d)\n", data_set); + TEST_PASS("Central test passed. (data set %d)", data_set); return false; } @@ -86,7 +85,7 @@ static void start_scan(void) err = bt_le_scan_start(BT_LE_SCAN_PASSIVE, device_found); if (err) { - FAIL("Scanning failed to start (err %d)\n", err); + TEST_FAIL("Scanning failed to start (err %d)", err); } LOG_DBG("Scanning successfully started"); @@ -100,7 +99,7 @@ void test_central(void) err = bt_enable(NULL); if (err) { - FAIL("Bluetooth init failed (err %d)\n", err); + TEST_FAIL("Bluetooth init failed (err %d)", err); } LOG_DBG("Bluetooth initialized"); diff --git a/tests/bsim/bluetooth/host/adv/encrypted/css_sample_data/src/common.h b/tests/bsim/bluetooth/host/adv/encrypted/css_sample_data/src/common.h index 91a5ff325bb6..96769b64b575 100644 --- a/tests/bsim/bluetooth/host/adv/encrypted/css_sample_data/src/common.h +++ b/tests/bsim/bluetooth/host/adv/encrypted/css_sample_data/src/common.h @@ -46,29 +46,6 @@ int bt_test_ead_encrypt(const uint8_t session_key[BT_EAD_KEY_SIZE], const uint8_t randomizer[BT_EAD_RANDOMIZER_SIZE], const uint8_t *payload, size_t payload_size, uint8_t *encrypted_payload); -#define FAIL(...) \ - do { \ - bst_result = Failed; \ - bs_trace_error_time_line(__VA_ARGS__); \ - } while (0) - -#define PASS(...) \ - do { \ - bst_result = Passed; \ - bs_trace_info_time(1, __VA_ARGS__); \ - } while (0) - -extern enum bst_result_t bst_result; - -#define CREATE_FLAG(flag) static atomic_t flag = (atomic_t) false -#define SET_FLAG(flag) (void)atomic_set(&flag, (atomic_t) true) -#define GET_FLAG(flag) (bool)atomic_get(&flag) -#define UNSET_FLAG(flag) (void)atomic_set(&flag, (atomic_t) false) -#define WAIT_FOR_FLAG(flag) \ - while (!(bool)atomic_get(&flag)) { \ - (void)k_sleep(K_MSEC(1)); \ - } - LOG_MODULE_DECLARE(bt_bsim_ead_sample_data, CONFIG_BT_EAD_LOG_LEVEL); struct test_sample_data { diff --git a/tests/bsim/bluetooth/host/adv/encrypted/css_sample_data/src/main.c b/tests/bsim/bluetooth/host/adv/encrypted/css_sample_data/src/main.c index cb9925179d64..9175d9d82b30 100644 --- a/tests/bsim/bluetooth/host/adv/encrypted/css_sample_data/src/main.c +++ b/tests/bsim/bluetooth/host/adv/encrypted/css_sample_data/src/main.c @@ -2,51 +2,25 @@ * SPDX-License-Identifier: Apache-2.0 */ -#include "bs_types.h" -#include "bs_tracing.h" #include "bstests.h" #include LOG_MODULE_REGISTER(bt_bsim_ead_sample_data, CONFIG_BT_EAD_LOG_LEVEL); -extern enum bst_result_t bst_result; - -#define WAIT_TIME_S 20 -#define WAIT_TIME (WAIT_TIME_S * 1e6) - extern void test_central(void); extern void test_peripheral(void); extern void test_args_parse(int argc, char *argv[]); -void test_tick(bs_time_t HW_device_time) -{ - if (bst_result != Passed) { - bst_result = Failed; - bs_trace_error_time_line("Test failed (not passed after %d seconds)\n", - WAIT_TIME_S); - } -} - -static void test_ead_sample_data_init(void) -{ - bst_ticker_set_next_tick_absolute(WAIT_TIME); - bst_result = In_progress; -} - static const struct bst_test_instance test_def[] = { { .test_id = "central", .test_descr = "Central device", - .test_pre_init_f = test_ead_sample_data_init, - .test_tick_f = test_tick, .test_main_f = test_central, .test_args_f = test_args_parse, }, { .test_id = "peripheral", .test_descr = "Peripheral device", - .test_pre_init_f = test_ead_sample_data_init, - .test_tick_f = test_tick, .test_main_f = test_peripheral, .test_args_f = test_args_parse, }, diff --git a/tests/bsim/bluetooth/host/adv/encrypted/css_sample_data/src/peripheral.c b/tests/bsim/bluetooth/host/adv/encrypted/css_sample_data/src/peripheral.c index 41362a547892..f6dc1418bce7 100644 --- a/tests/bsim/bluetooth/host/adv/encrypted/css_sample_data/src/peripheral.c +++ b/tests/bsim/bluetooth/host/adv/encrypted/css_sample_data/src/peripheral.c @@ -2,6 +2,7 @@ * SPDX-License-Identifier: Apache-2.0 */ +#include "babblekit/testcase.h" #include "common.h" extern const struct test_sample_data *sample_data; @@ -25,7 +26,7 @@ static void create_adv(struct bt_le_ext_adv **adv) err = bt_le_ext_adv_create(¶ms, NULL, adv); if (err) { - FAIL("Failed to create advertiser (%d)\n", err); + TEST_FAIL("Failed to create advertiser (%d)", err); } } @@ -42,7 +43,7 @@ static void start_adv(struct bt_le_ext_adv *adv) err = bt_le_ext_adv_start(adv, &start_params); if (err) { - FAIL("Failed to start advertiser (%d)\n", err); + TEST_FAIL("Failed to start advertiser (%d)", err); } LOG_DBG("Advertiser started"); @@ -60,23 +61,21 @@ static void set_ad_data(struct bt_le_ext_adv *adv) if (size_ead != sample_data->size_ead) { LOG_ERR("Size of ead: %zu\n", size_ead); LOG_ERR("Size of sample_ead: %zu", sample_data->size_ead); - FAIL("Computed size of encrypted data does not match the size of the encrypted " - "data from the sample. (data set %d)\n", - data_set); + TEST_FAIL("Computed size of encrypted data does not match the size of the encrypted" + " data from the sample. (data set %d)", data_set); } err = bt_test_ead_encrypt(sample_data->session_key, sample_data->iv, sample_data->randomizer_little_endian, sample_data->ad_data, size_ad_data, ead); if (err != 0) { - FAIL("Error during encryption.\n"); + TEST_FAIL("Error during encryption."); } else if (memcmp(ead, sample_data->ead, sample_data->size_ead) != 0) { LOG_HEXDUMP_ERR(ead, size_ead, "Encrypted data from bt_ead_encrypt:"); LOG_HEXDUMP_ERR(sample_data->ead, sample_data->size_ead, "Encrypted data from sample:"); - FAIL("Encrypted AD data does not match the ones provided in the sample. (data set " - "%d)\n", - data_set); + TEST_FAIL("Encrypted AD data does not match the ones provided in the sample. (data" + " set %d)", data_set); } LOG_HEXDUMP_DBG(ead, size_ead, "Encrypted data:"); @@ -87,10 +86,10 @@ static void set_ad_data(struct bt_le_ext_adv *adv) err = bt_le_ext_adv_set_data(adv, &ead_struct, 1, NULL, 0); if (err) { - FAIL("Failed to set advertising data (%d)\n", err); + TEST_FAIL("Failed to set advertising data (%d)", err); } - PASS("Peripheral test passed. (data set %d)\n", data_set); + TEST_PASS("Peripheral test passed. (data set %d)", data_set); } void test_peripheral(void) @@ -102,7 +101,7 @@ void test_peripheral(void) err = bt_enable(NULL); if (err) { - FAIL("Bluetooth init failed (err %d)\n", err); + TEST_FAIL("Bluetooth init failed (err %d)", err); } LOG_DBG("Bluetooth initialized"); diff --git a/tests/bsim/bluetooth/host/adv/encrypted/css_sample_data/test_scripts/run_tests.sh b/tests/bsim/bluetooth/host/adv/encrypted/css_sample_data/test_scripts/run_tests.sh index 0ac5936aaa30..4bde3f3f7405 100755 --- a/tests/bsim/bluetooth/host/adv/encrypted/css_sample_data/test_scripts/run_tests.sh +++ b/tests/bsim/bluetooth/host/adv/encrypted/css_sample_data/test_scripts/run_tests.sh @@ -21,7 +21,7 @@ Execute ./"$test_exe" \ -RealEncryption=1 -argstest data-set="${data_set}" Execute ./bs_2G4_phy_v1 -v=${verbosity_level} -s="${simulation_id}_${data_set}" \ - -D=2 -sim_length=60e6 $@ + -D=2 -sim_length=20e6 $@ wait_for_background_jobs @@ -36,6 +36,6 @@ Execute ./"$test_exe" \ -RealEncryption=1 -argstest data-set="${data_set}" Execute ./bs_2G4_phy_v1 -v=${verbosity_level} -s="${simulation_id}_${data_set}" \ - -D=2 -sim_length=60e6 $@ + -D=2 -sim_length=20e6 $@ wait_for_background_jobs diff --git a/tests/bsim/bluetooth/host/adv/extended/CMakeLists.txt b/tests/bsim/bluetooth/host/adv/extended/CMakeLists.txt index 1eeaba1207e4..0ad660d99580 100644 --- a/tests/bsim/bluetooth/host/adv/extended/CMakeLists.txt +++ b/tests/bsim/bluetooth/host/adv/extended/CMakeLists.txt @@ -6,10 +6,8 @@ cmake_minimum_required(VERSION 3.20.0) find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) project(bsim_test_ext_adv) -target_sources(app PRIVATE - src/common.c -) - +add_subdirectory(${ZEPHYR_BASE}/tests/bsim/babblekit babblekit) +target_link_libraries(app PRIVATE babblekit) target_sources_ifdef(CONFIG_BT_CENTRAL app PRIVATE src/ext_adv_scanner.c diff --git a/tests/bsim/bluetooth/host/adv/extended/src/common.c b/tests/bsim/bluetooth/host/adv/extended/src/common.c deleted file mode 100644 index 6de9aee1de9b..000000000000 --- a/tests/bsim/bluetooth/host/adv/extended/src/common.c +++ /dev/null @@ -1,24 +0,0 @@ -/** - * Copyright (c) 2024 Croxel, Inc. - * - * SPDX-License-Identifier: Apache-2.0 - */ - -#include -#include "bstests.h" -#include "common.h" - -extern enum bst_result_t bst_result; - -void test_tick(bs_time_t HW_device_time) -{ - if (bst_result != Passed) { - FAIL("test failed (not passed after %i seconds)\n", WAIT_SECONDS); - } -} - -void test_init(void) -{ - bst_ticker_set_next_tick_absolute(WAIT_TIME); - bst_result = In_progress; -} diff --git a/tests/bsim/bluetooth/host/adv/extended/src/common.h b/tests/bsim/bluetooth/host/adv/extended/src/common.h deleted file mode 100644 index 658613892e11..000000000000 --- a/tests/bsim/bluetooth/host/adv/extended/src/common.h +++ /dev/null @@ -1,53 +0,0 @@ -/** - * Copyright (c) 2024 Croxel, Inc. - * - * SPDX-License-Identifier: Apache-2.0 - */ - -#ifndef ZEPHYR_TEST_BSIM_BT_EXT_ADV_TEST_ -#define ZEPHYR_TEST_BSIM_BT_EXT_ADV_TEST_ - -#include - -#include "bs_types.h" -#include "bs_tracing.h" -#include "time_machine.h" -#include "bstests.h" - -#define WAIT_SECONDS 30 /* seconds */ -#define WAIT_TIME (WAIT_SECONDS * USEC_PER_SEC) /* microseconds*/ - -#define CREATE_FLAG(flag) static atomic_t flag = (atomic_t)false -#define SET_FLAG(flag) (void)atomic_set(&flag, (atomic_t) true) -#define TEST_FLAG(flag) (atomic_get(&flag) == (atomic_t)true) -#define UNSET_FLAG(flag) (void)atomic_set(&flag, (atomic_t) false) -#define WAIT_FOR_FLAG(flag) \ - while (!(bool)atomic_get(&flag)) { \ - (void)k_sleep(K_MSEC(1)); \ - } -#define WAIT_FOR_FLAG_UNSET(flag) \ - while ((bool)atomic_get(&flag)) { \ - (void)k_sleep(K_MSEC(1)); \ - } -#define TAKE_FLAG(flag) \ - while (!(bool)atomic_cas(&flag, true, false)) { \ - (void)k_sleep(K_MSEC(1)); \ - } - - -#define FAIL(...) \ - do { \ - bst_result = Failed; \ - bs_trace_error_time_line(__VA_ARGS__); \ - } while (0) - -#define PASS(...) \ - do { \ - bst_result = Passed; \ - bs_trace_info_time(1, "PASSED: " __VA_ARGS__); \ - } while (0) - -void test_tick(bs_time_t HW_device_time); -void test_init(void); - -#endif /* ZEPHYR_TEST_BSIM_BT_EXT_ADV_TEST_ */ diff --git a/tests/bsim/bluetooth/host/adv/extended/src/ext_adv_advertiser.c b/tests/bsim/bluetooth/host/adv/extended/src/ext_adv_advertiser.c index a146107efa8f..492b26b106ed 100644 --- a/tests/bsim/bluetooth/host/adv/extended/src/ext_adv_advertiser.c +++ b/tests/bsim/bluetooth/host/adv/extended/src/ext_adv_advertiser.c @@ -5,12 +5,8 @@ */ #include -#include "common.h" - -#include "bs_types.h" -#include "bs_tracing.h" -#include "time_machine.h" -#include "bstests.h" +#include "babblekit/testcase.h" +#include "babblekit/flags.h" #include #include @@ -22,8 +18,8 @@ extern enum bst_result_t bst_result; static struct bt_conn *g_conn; -CREATE_FLAG(flag_connected); -CREATE_FLAG(flag_conn_recycled); +static DEFINE_FLAG(flag_connected); +static DEFINE_FLAG(flag_conn_recycled); static void common_init(void) { @@ -32,7 +28,7 @@ static void common_init(void) err = bt_enable(NULL); if (err) { - FAIL("Bluetooth init failed: %d\n", err); + TEST_FAIL("Bluetooth init failed: %d", err); return; } printk("Bluetooth initialized\n"); @@ -103,7 +99,7 @@ static void disconnect_from_target(void) err = bt_conn_disconnect(g_conn, BT_HCI_ERR_REMOTE_USER_TERM_CONN); if (err) { - FAIL("BT Disconnect failed: %d\n", err); + TEST_FAIL("BT Disconnect failed: %d", err); return; } } @@ -115,13 +111,13 @@ static void connected(struct bt_conn *conn, uint8_t err) bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr)); if (err != BT_HCI_ERR_SUCCESS) { - FAIL("Failed to connect to %s: %u\n", addr, err); + TEST_FAIL("Failed to connect to %s: %u", addr, err); return; } printk("Connected to %s\n", addr); if (g_conn != NULL) { - FAIL("Attempt to override connection object without clean-up\n"); + TEST_FAIL("Attempt to override connection object without clean-up"); return; } g_conn = bt_conn_ref(conn); @@ -182,7 +178,7 @@ static void main_ext_adv_advertiser(void) ext_adv = NULL; - PASS("Extended advertiser passed\n"); + TEST_PASS("Extended advertiser passed"); } static void adv_connect_and_disconnect_cycle(void) @@ -228,7 +224,7 @@ static void main_ext_conn_adv_advertiser(void) ext_adv = NULL; - PASS("Extended advertiser passed\n"); + TEST_PASS("Extended advertiser passed"); } static void main_ext_conn_adv_advertiser_x5(void) @@ -252,7 +248,7 @@ static void main_ext_conn_adv_advertiser_x5(void) delete_adv_set(ext_adv); ext_adv = NULL; - PASS("Extended advertiser passed\n"); + TEST_PASS("Extended advertiser passed"); } static const struct bst_test_instance ext_adv_advertiser[] = { @@ -260,16 +256,12 @@ static const struct bst_test_instance ext_adv_advertiser[] = { .test_id = "ext_adv_advertiser", .test_descr = "Basic extended advertising test. " "Will just start extended advertising.", - .test_pre_init_f = test_init, - .test_tick_f = test_tick, .test_main_f = main_ext_adv_advertiser }, { .test_id = "ext_adv_conn_advertiser", .test_descr = "Basic connectable extended advertising test. " "Starts extended advertising, and restarts it after disconnecting", - .test_pre_init_f = test_init, - .test_tick_f = test_tick, .test_main_f = main_ext_conn_adv_advertiser }, { @@ -277,8 +269,6 @@ static const struct bst_test_instance ext_adv_advertiser[] = { .test_descr = "Basic connectable extended advertising test. " "Starts extended advertising, and restarts it after disconnecting, " "repeated over 5 times", - .test_pre_init_f = test_init, - .test_tick_f = test_tick, .test_main_f = main_ext_conn_adv_advertiser_x5 }, BSTEST_END_MARKER diff --git a/tests/bsim/bluetooth/host/adv/extended/src/ext_adv_scanner.c b/tests/bsim/bluetooth/host/adv/extended/src/ext_adv_scanner.c index b4cd0691afe7..231b344ca0f4 100644 --- a/tests/bsim/bluetooth/host/adv/extended/src/ext_adv_scanner.c +++ b/tests/bsim/bluetooth/host/adv/extended/src/ext_adv_scanner.c @@ -5,12 +5,8 @@ */ #include -#include "common.h" - -#include "bs_types.h" -#include "bs_tracing.h" -#include "time_machine.h" -#include "bstests.h" +#include "babblekit/testcase.h" +#include "babblekit/flags.h" #include #include @@ -22,9 +18,9 @@ extern enum bst_result_t bst_result; static struct bt_conn *g_conn; -CREATE_FLAG(flag_ext_adv_seen); -CREATE_FLAG(flag_connected); -CREATE_FLAG(flag_conn_recycled); +static DEFINE_FLAG(flag_ext_adv_seen); +static DEFINE_FLAG(flag_connected); +static DEFINE_FLAG(flag_conn_recycled); static void connected(struct bt_conn *conn, uint8_t err) { @@ -33,7 +29,7 @@ static void connected(struct bt_conn *conn, uint8_t err) bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr)); if (err != BT_HCI_ERR_SUCCESS) { - FAIL("Failed to connect to %s: %u\n", addr, err); + TEST_FAIL("Failed to connect to %s: %u", addr, err); bt_conn_unref(g_conn); g_conn = NULL; return; @@ -93,21 +89,20 @@ static void scan_recv(const struct bt_le_scan_recv_info *info, SET_FLAG(flag_ext_adv_seen); } - if (!TEST_FLAG(flag_connected) && - info->adv_props & BT_GAP_ADV_PROP_CONNECTABLE) { + if (!IS_FLAG_SET(flag_connected) && info->adv_props & BT_GAP_ADV_PROP_CONNECTABLE) { int err; printk("Stopping scan\n"); err = bt_le_scan_stop(); if (err) { - FAIL("Failed to stop scan: %d", err); + TEST_FAIL("Failed to stop scan: %d", err); return; } err = bt_conn_le_create(info->addr, BT_CONN_LE_CREATE_CONN, BT_LE_CONN_PARAM_DEFAULT, &g_conn); if (err) { - FAIL("Could not connect to peer: %d", err); + TEST_FAIL("Could not connect to peer: %d", err); return; } } @@ -124,7 +119,7 @@ static void common_init(void) err = bt_enable(NULL); if (err) { - FAIL("Bluetooth init failed: %d\n", err); + TEST_FAIL("Bluetooth init failed: %d", err); return; } @@ -141,7 +136,7 @@ static void start_scan(void) printk("Start scanning..."); err = bt_le_scan_start(BT_LE_SCAN_PASSIVE, NULL); if (err) { - FAIL("Failed to start scan: %d\n", err); + TEST_FAIL("Failed to start scan: %d", err); return; } printk("done.\n"); @@ -156,7 +151,7 @@ static void main_ext_adv_scanner(void) WAIT_FOR_FLAG(flag_ext_adv_seen); - PASS("Extended adv scanner passed\n"); + TEST_PASS("Extended adv scanner passed"); } static void scan_connect_and_disconnect_cycle(void) @@ -191,7 +186,7 @@ static void main_ext_adv_conn_scanner(void) printk("Waiting to extended advertisements (again)...\n"); WAIT_FOR_FLAG(flag_ext_adv_seen); - PASS("Extended adv scanner passed\n"); + TEST_PASS("Extended adv scanner passed"); } static void main_ext_adv_conn_scanner_x5(void) @@ -207,7 +202,7 @@ static void main_ext_adv_conn_scanner_x5(void) printk("Waiting to extended advertisements (again)...\n"); WAIT_FOR_FLAG(flag_ext_adv_seen); - PASS("Extended adv scanner x5 passed\n"); + TEST_PASS("Extended adv scanner x5 passed"); } static const struct bst_test_instance ext_adv_scanner[] = { @@ -215,8 +210,6 @@ static const struct bst_test_instance ext_adv_scanner[] = { .test_id = "ext_adv_scanner", .test_descr = "Basic extended advertising scanning test. " "Will just scan an extended advertiser.", - .test_pre_init_f = test_init, - .test_tick_f = test_tick, .test_main_f = main_ext_adv_scanner }, { @@ -224,8 +217,6 @@ static const struct bst_test_instance ext_adv_scanner[] = { .test_descr = "Basic extended advertising scanning test. " "Will scan an extended advertiser, connect " "and verify it's detected after disconnection", - .test_pre_init_f = test_init, - .test_tick_f = test_tick, .test_main_f = main_ext_adv_conn_scanner }, { @@ -234,8 +225,6 @@ static const struct bst_test_instance ext_adv_scanner[] = { "Will scan an extended advertiser, connect " "and verify it's detected after disconnection," "repeated over 5 times", - .test_pre_init_f = test_init, - .test_tick_f = test_tick, .test_main_f = main_ext_adv_conn_scanner_x5 }, BSTEST_END_MARKER diff --git a/tests/bsim/bluetooth/host/adv/periodic/CMakeLists.txt b/tests/bsim/bluetooth/host/adv/periodic/CMakeLists.txt index 85b2a61c9db5..525996ebd945 100644 --- a/tests/bsim/bluetooth/host/adv/periodic/CMakeLists.txt +++ b/tests/bsim/bluetooth/host/adv/periodic/CMakeLists.txt @@ -5,6 +5,9 @@ cmake_minimum_required(VERSION 3.20.0) find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) project(bsim_test_audio) +add_subdirectory(${ZEPHYR_BASE}/tests/bsim/babblekit babblekit) +target_link_libraries(app PRIVATE babblekit) + target_sources(app PRIVATE src/common.c src/main.c diff --git a/tests/bsim/bluetooth/host/adv/periodic/src/common.c b/tests/bsim/bluetooth/host/adv/periodic/src/common.c index b4e4151c49a4..105dd4b83827 100644 --- a/tests/bsim/bluetooth/host/adv/periodic/src/common.c +++ b/tests/bsim/bluetooth/host/adv/periodic/src/common.c @@ -6,21 +6,6 @@ #include "common.h" -extern enum bst_result_t bst_result; - -void test_tick(bs_time_t HW_device_time) -{ - if (bst_result != Passed) { - FAIL("test failed (not passed after %i seconds)\n", WAIT_SECONDS); - } -} - -void test_init(void) -{ - bst_ticker_set_next_tick_absolute(WAIT_TIME); - bst_result = In_progress; -} - uint8_t mfg_data[254] = { 0xFF, 0xFF, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, diff --git a/tests/bsim/bluetooth/host/adv/periodic/src/common.h b/tests/bsim/bluetooth/host/adv/periodic/src/common.h index 02a8fea70e81..03a1f0c5bbb2 100644 --- a/tests/bsim/bluetooth/host/adv/periodic/src/common.h +++ b/tests/bsim/bluetooth/host/adv/periodic/src/common.h @@ -9,50 +9,7 @@ #ifndef ZEPHYR_TEST_BSIM_BT_AUDIO_TEST_ #define ZEPHYR_TEST_BSIM_BT_AUDIO_TEST_ -#include - -#include "bs_types.h" -#include "bs_tracing.h" -#include "time_machine.h" -#include "bstests.h" - -#include -#include -#include -#include - -#include -#include -#include -#include -#include - -#define WAIT_SECONDS 15 /* seconds */ -#define WAIT_TIME (WAIT_SECONDS * USEC_PER_SEC) /* microseconds*/ - -#define CREATE_FLAG(flag) static atomic_t flag = (atomic_t)false -#define SET_FLAG(flag) (void)atomic_set(&flag, (atomic_t)true) -#define UNSET_FLAG(flag) (void)atomic_set(&flag, (atomic_t)false) -#define TEST_FLAG(flag) (atomic_get(&flag) == (atomic_t)true) -#define WAIT_FOR_FLAG(flag) \ - while (!(bool)atomic_get(&flag)) { \ - (void)k_sleep(K_MSEC(1)); \ - } - -#define FAIL(...) \ - do { \ - bst_result = Failed; \ - bs_trace_error_time_line(__VA_ARGS__); \ - } while (0) - -#define PASS(...) \ - do { \ - bst_result = Passed; \ - bs_trace_info_time(1, "PASSED: " __VA_ARGS__); \ - } while (0) - -void test_tick(bs_time_t HW_device_time); -void test_init(void); +#include extern uint8_t mfg_data[254]; diff --git a/tests/bsim/bluetooth/host/adv/periodic/src/per_adv_advertiser.c b/tests/bsim/bluetooth/host/adv/periodic/src/per_adv_advertiser.c index aa01d46db3c6..3085bda474a9 100644 --- a/tests/bsim/bluetooth/host/adv/periodic/src/per_adv_advertiser.c +++ b/tests/bsim/bluetooth/host/adv/periodic/src/per_adv_advertiser.c @@ -3,26 +3,28 @@ * * SPDX-License-Identifier: Apache-2.0 */ -#include - -#include "bs_types.h" -#include "bs_tracing.h" -#include "time_machine.h" -#include "bstests.h" +#include +#include #include +#include #include - #include +#include +#include +#include +#include +#include "babblekit/testcase.h" +#include "babblekit/flags.h" #include "common.h" extern enum bst_result_t bst_result; static struct bt_conn *g_conn; -CREATE_FLAG(flag_connected); -CREATE_FLAG(flag_bonded); +static DEFINE_FLAG(flag_connected); +static DEFINE_FLAG(flag_bonded); static void connected(struct bt_conn *conn, uint8_t err) { @@ -31,7 +33,7 @@ static void connected(struct bt_conn *conn, uint8_t err) bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr)); if (err != BT_HCI_ERR_SUCCESS) { - FAIL("Failed to connect to %s: %u\n", addr, err); + TEST_FAIL("Failed to connect to %s: %u", addr, err); return; } @@ -75,7 +77,7 @@ static void common_init(void) err = bt_enable(NULL); if (err) { - FAIL("Bluetooth init failed: %d\n", err); + TEST_FAIL("Bluetooth init failed: %d", err); return; } printk("Bluetooth initialized\n"); @@ -249,7 +251,7 @@ static void main_per_adv_advertiser(void) delete_adv_set(per_adv); per_adv = NULL; - PASS("Periodic advertiser passed\n"); + TEST_PASS("Periodic advertiser passed"); } #if defined(CONFIG_BT_CTLR_PHY_CODED) @@ -273,7 +275,7 @@ static void main_per_adv_advertiser_coded(void) delete_adv_set(per_adv); per_adv = NULL; - PASS("Periodic advertiser coded PHY passed\n"); + TEST_PASS("Periodic advertiser coded PHY passed"); } #endif /* CONFIG_BT_CTLR_PHY_CODED */ @@ -305,7 +307,7 @@ static void main_per_adv_conn_advertiser(void) delete_adv_set(conn_adv); conn_adv = NULL; - PASS("Periodic advertiser passed\n"); + TEST_PASS("Periodic advertiser passed"); } static void main_per_adv_conn_privacy_advertiser(void) @@ -341,7 +343,7 @@ static void main_per_adv_conn_privacy_advertiser(void) delete_adv_set(conn_adv); conn_adv = NULL; - PASS("Periodic advertiser passed\n"); + TEST_PASS("Periodic advertiser passed"); } static void main_per_adv_long_data_advertiser(void) @@ -366,7 +368,7 @@ static void main_per_adv_long_data_advertiser(void) delete_adv_set(per_adv); per_adv = NULL; #endif - PASS("Periodic long data advertiser passed\n"); + TEST_PASS("Periodic long data advertiser passed"); } static const struct bst_test_instance per_adv_advertiser[] = { @@ -374,8 +376,6 @@ static const struct bst_test_instance per_adv_advertiser[] = { .test_id = "per_adv_advertiser", .test_descr = "Basic periodic advertising test. " "Will just start periodic advertising.", - .test_pre_init_f = test_init, - .test_tick_f = test_tick, .test_main_f = main_per_adv_advertiser }, #if defined(CONFIG_BT_CTLR_PHY_CODED) @@ -383,8 +383,6 @@ static const struct bst_test_instance per_adv_advertiser[] = { .test_id = "per_adv_advertiser_coded_phy", .test_descr = "Basic periodic advertising test on Coded PHY. " "Advertiser and periodic advertiser uses Coded PHY", - .test_pre_init_f = test_init, - .test_tick_f = test_tick, .test_main_f = main_per_adv_advertiser_coded }, #endif /* CONFIG_BT_CTLR_PHY_CODED */ @@ -392,24 +390,18 @@ static const struct bst_test_instance per_adv_advertiser[] = { .test_id = "per_adv_conn_advertiser", .test_descr = "Periodic advertising test with concurrent ACL " "and PA sync.", - .test_pre_init_f = test_init, - .test_tick_f = test_tick, .test_main_f = main_per_adv_conn_advertiser }, { .test_id = "per_adv_conn_privacy_advertiser", .test_descr = "Periodic advertising test with concurrent ACL " "with bonding and PA sync.", - .test_pre_init_f = test_init, - .test_tick_f = test_tick, .test_main_f = main_per_adv_conn_privacy_advertiser }, { .test_id = "per_adv_long_data_advertiser", .test_descr = "Periodic advertising test with a longer data length. " "To test the reassembly of large data packets", - .test_pre_init_f = test_init, - .test_tick_f = test_tick, .test_main_f = main_per_adv_long_data_advertiser }, BSTEST_END_MARKER diff --git a/tests/bsim/bluetooth/host/adv/periodic/src/per_adv_sync.c b/tests/bsim/bluetooth/host/adv/periodic/src/per_adv_sync.c index 08c3d125242b..0b7a8969f9a0 100644 --- a/tests/bsim/bluetooth/host/adv/periodic/src/per_adv_sync.c +++ b/tests/bsim/bluetooth/host/adv/periodic/src/per_adv_sync.c @@ -3,18 +3,20 @@ * * SPDX-License-Identifier: Apache-2.0 */ -#include - -#include "bs_types.h" -#include "bs_tracing.h" -#include "time_machine.h" -#include "bstests.h" +#include +#include #include +#include #include - #include +#include +#include +#include +#include +#include "babblekit/testcase.h" +#include "babblekit/flags.h" #include "common.h" extern enum bst_result_t bst_result; @@ -23,12 +25,12 @@ static struct bt_conn *g_conn; static bt_addr_le_t per_addr; static uint8_t per_sid; -CREATE_FLAG(flag_connected); -CREATE_FLAG(flag_bonded); -CREATE_FLAG(flag_per_adv); -CREATE_FLAG(flag_per_adv_sync); -CREATE_FLAG(flag_per_adv_sync_lost); -CREATE_FLAG(flag_per_adv_recv); +static DEFINE_FLAG(flag_connected); +static DEFINE_FLAG(flag_bonded); +static DEFINE_FLAG(flag_per_adv); +static DEFINE_FLAG(flag_per_adv_sync); +static DEFINE_FLAG(flag_per_adv_sync_lost); +static DEFINE_FLAG(flag_per_adv_recv); static void connected(struct bt_conn *conn, uint8_t err) { @@ -37,7 +39,7 @@ static void connected(struct bt_conn *conn, uint8_t err) bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr)); if (err != BT_HCI_ERR_SUCCESS) { - FAIL("Failed to connect to %s: %u\n", addr, err); + TEST_FAIL("Failed to connect to %s: %u", addr, err); return; } @@ -77,24 +79,24 @@ static struct bt_conn_auth_info_cb auto_info_cbs = { static void scan_recv(const struct bt_le_scan_recv_info *info, struct net_buf_simple *buf) { - if (!TEST_FLAG(flag_connected) && + if (!IS_FLAG_SET(flag_connected) && info->adv_props & BT_GAP_ADV_PROP_CONNECTABLE) { int err; printk("Stopping scan\n"); err = bt_le_scan_stop(); if (err != 0) { - FAIL("Failed to stop scan: %d", err); + TEST_FAIL("Failed to stop scan: %d", err); return; } err = bt_conn_le_create(info->addr, BT_CONN_LE_CREATE_CONN, BT_LE_CONN_PARAM_DEFAULT, &g_conn); if (err != 0) { - FAIL("Could not connect to peer: %d", err); + TEST_FAIL("Could not connect to peer: %d", err); return; } - } else if (!TEST_FLAG(flag_per_adv) && info->interval != 0U) { + } else if (!IS_FLAG_SET(flag_per_adv) && info->interval != 0U) { per_sid = info->sid; bt_addr_le_copy(&per_addr, info->addr); @@ -142,7 +144,7 @@ static void recv_cb(struct bt_le_per_adv_sync *recv_sync, char le_addr[BT_ADDR_LE_STR_LEN]; uint8_t buf_data_len; - if (TEST_FLAG(flag_per_adv_recv)) { + if (IS_FLAG_SET(flag_per_adv_recv)) { return; } @@ -154,7 +156,7 @@ static void recv_cb(struct bt_le_per_adv_sync *recv_sync, buf_data_len = (uint8_t)net_buf_simple_pull_le16(buf); if (buf->data[0] - 1 != sizeof(mfg_data) || memcmp(buf->data, mfg_data, sizeof(mfg_data))) { - FAIL("Unexpected adv data received\n"); + TEST_FAIL("Unexpected adv data received"); } net_buf_simple_pull(buf, ARRAY_SIZE(mfg_data)); } @@ -175,7 +177,7 @@ static void common_init(void) err = bt_enable(NULL); if (err) { - FAIL("Bluetooth init failed: %d\n", err); + TEST_FAIL("Bluetooth init failed: %d", err); return; } @@ -195,7 +197,7 @@ static void start_scan(void) BT_LE_SCAN_CODED_ACTIVE : BT_LE_SCAN_ACTIVE, NULL); if (err) { - FAIL("Failed to start scan: %d\n", err); + TEST_FAIL("Failed to start scan: %d", err); return; } printk("done.\n"); @@ -214,7 +216,7 @@ static void create_pa_sync(struct bt_le_per_adv_sync **sync) sync_create_param.timeout = 0x0a; err = bt_le_per_adv_sync_create(&sync_create_param, sync); if (err) { - FAIL("Failed to create periodic advertising sync: %d\n", err); + TEST_FAIL("Failed to create periodic advertising sync: %d", err); return; } printk("done.\n"); @@ -231,7 +233,7 @@ static void start_bonding(void) printk("Setting security..."); err = bt_conn_set_security(g_conn, BT_SECURITY_L2); if (err) { - FAIL("Failed to set security: %d\n", err); + TEST_FAIL("Failed to set security: %d", err); return; } printk("done.\n"); @@ -253,7 +255,7 @@ static void main_per_adv_sync(void) printk("Waiting for periodic sync lost...\n"); WAIT_FOR_FLAG(flag_per_adv_sync_lost); - PASS("Periodic advertising sync passed\n"); + TEST_PASS("Periodic advertising sync passed"); } static void main_per_adv_sync_app_not_scanning(void) @@ -271,7 +273,7 @@ static void main_per_adv_sync_app_not_scanning(void) printk("Stopping scan\n"); err = bt_le_scan_stop(); if (err != 0) { - FAIL("Failed to stop scan: %d", err); + TEST_FAIL("Failed to stop scan: %d", err); return; } @@ -280,7 +282,7 @@ static void main_per_adv_sync_app_not_scanning(void) printk("Waiting for periodic sync lost...\n"); WAIT_FOR_FLAG(flag_per_adv_sync_lost); - PASS("Periodic advertising sync passed\n"); + TEST_PASS("Periodic advertising sync passed"); } static void main_per_adv_conn_sync(void) @@ -305,7 +307,7 @@ static void main_per_adv_conn_sync(void) printk("Waiting for periodic sync lost...\n"); WAIT_FOR_FLAG(flag_per_adv_sync_lost); - PASS("Periodic advertising sync passed\n"); + TEST_PASS("Periodic advertising sync passed"); } static void main_per_adv_conn_privacy_sync(void) @@ -336,7 +338,7 @@ static void main_per_adv_conn_privacy_sync(void) printk("Waiting for periodic sync lost...\n"); WAIT_FOR_FLAG(flag_per_adv_sync_lost); - PASS("Periodic advertising sync passed\n"); + TEST_PASS("Periodic advertising sync passed"); } static void main_per_adv_long_data_sync(void) @@ -359,7 +361,7 @@ static void main_per_adv_long_data_sync(void) printk("Waiting for periodic sync lost...\n"); WAIT_FOR_FLAG(flag_per_adv_sync_lost); #endif - PASS("Periodic advertising long data sync passed\n"); + TEST_PASS("Periodic advertising long data sync passed"); } static const struct bst_test_instance per_adv_sync[] = { @@ -367,8 +369,6 @@ static const struct bst_test_instance per_adv_sync[] = { .test_id = "per_adv_sync", .test_descr = "Basic periodic advertising sync test. " "Will just sync to a periodic advertiser.", - .test_pre_init_f = test_init, - .test_tick_f = test_tick, .test_main_f = main_per_adv_sync }, { @@ -376,8 +376,6 @@ static const struct bst_test_instance per_adv_sync[] = { .test_descr = "Basic periodic advertising sync test but where " "the app stopped scanning before creating sync." "Expect the host to start scanning automatically.", - .test_pre_init_f = test_init, - .test_tick_f = test_tick, .test_main_f = main_per_adv_sync_app_not_scanning }, { @@ -385,8 +383,6 @@ static const struct bst_test_instance per_adv_sync[] = { .test_descr = "Periodic advertising sync test, but where there " "is a connection between the advertiser and the " "synchronized device.", - .test_pre_init_f = test_init, - .test_tick_f = test_tick, .test_main_f = main_per_adv_conn_sync }, { @@ -394,8 +390,6 @@ static const struct bst_test_instance per_adv_sync[] = { .test_descr = "Periodic advertising sync test, but where " "advertiser and synchronized device are bonded and using " "privacy", - .test_pre_init_f = test_init, - .test_tick_f = test_tick, .test_main_f = main_per_adv_conn_privacy_sync }, { @@ -403,8 +397,6 @@ static const struct bst_test_instance per_adv_sync[] = { .test_descr = "Periodic advertising sync test with larger " "data length. Test is used to verify that " "reassembly of long data is handeled correctly.", - .test_pre_init_f = test_init, - .test_tick_f = test_tick, .test_main_f = main_per_adv_long_data_sync }, BSTEST_END_MARKER diff --git a/tests/bsim/bluetooth/host/adv/periodic/tests_scripts/per_adv.sh b/tests/bsim/bluetooth/host/adv/periodic/tests_scripts/per_adv.sh index a7b8d99b6548..a27b9fedf4ee 100755 --- a/tests/bsim/bluetooth/host/adv/periodic/tests_scripts/per_adv.sh +++ b/tests/bsim/bluetooth/host/adv/periodic/tests_scripts/per_adv.sh @@ -21,6 +21,6 @@ Execute ./bs_${BOARD_TS}_tests_bsim_bluetooth_host_adv_periodic_prj_conf \ -testid=per_adv_sync -rs=6 Execute ./bs_2G4_phy_v1 -v=${verbosity_level} -s=${simulation_id} \ - -D=2 -sim_length=20e6 $@ + -D=2 -sim_length=15e6 $@ wait_for_background_jobs diff --git a/tests/bsim/bluetooth/host/adv/periodic/tests_scripts/per_adv_app_not_scanning.sh b/tests/bsim/bluetooth/host/adv/periodic/tests_scripts/per_adv_app_not_scanning.sh index 7176929cb2d0..33c61af5fb9c 100755 --- a/tests/bsim/bluetooth/host/adv/periodic/tests_scripts/per_adv_app_not_scanning.sh +++ b/tests/bsim/bluetooth/host/adv/periodic/tests_scripts/per_adv_app_not_scanning.sh @@ -21,6 +21,6 @@ Execute ./bs_${BOARD_TS}_tests_bsim_bluetooth_host_adv_periodic_prj_conf \ -testid=per_adv_sync_app_not_scanning -rs=6 Execute ./bs_2G4_phy_v1 -v=${verbosity_level} -s=${simulation_id} \ - -D=2 -sim_length=20e6 $@ + -D=2 -sim_length=15e6 $@ wait_for_background_jobs diff --git a/tests/bsim/bluetooth/host/adv/periodic/tests_scripts/per_adv_app_not_scanning_coded.sh b/tests/bsim/bluetooth/host/adv/periodic/tests_scripts/per_adv_app_not_scanning_coded.sh index 31fd0195dddf..c64dbc4d20bc 100755 --- a/tests/bsim/bluetooth/host/adv/periodic/tests_scripts/per_adv_app_not_scanning_coded.sh +++ b/tests/bsim/bluetooth/host/adv/periodic/tests_scripts/per_adv_app_not_scanning_coded.sh @@ -22,6 +22,6 @@ Execute ./bs_${BOARD_TS}_tests_bsim_bluetooth_host_adv_periodic_prj_coded_conf \ -testid=per_adv_sync_app_not_scanning -rs=6 Execute ./bs_2G4_phy_v1 -v=${verbosity_level} -s=${simulation_id} \ - -D=2 -sim_length=20e6 $@ + -D=2 -sim_length=15e6 $@ wait_for_background_jobs diff --git a/tests/bsim/bluetooth/host/adv/periodic/tests_scripts/per_adv_conn.sh b/tests/bsim/bluetooth/host/adv/periodic/tests_scripts/per_adv_conn.sh index a459c91e450a..dce1400647f2 100755 --- a/tests/bsim/bluetooth/host/adv/periodic/tests_scripts/per_adv_conn.sh +++ b/tests/bsim/bluetooth/host/adv/periodic/tests_scripts/per_adv_conn.sh @@ -21,6 +21,6 @@ Execute ./bs_${BOARD_TS}_tests_bsim_bluetooth_host_adv_periodic_prj_conf \ -testid=per_adv_conn_sync -rs=6 Execute ./bs_2G4_phy_v1 -v=${verbosity_level} -s=${simulation_id} \ - -D=2 -sim_length=20e6 $@ + -D=2 -sim_length=15e6 $@ wait_for_background_jobs diff --git a/tests/bsim/bluetooth/host/adv/periodic/tests_scripts/per_adv_conn_privacy.sh b/tests/bsim/bluetooth/host/adv/periodic/tests_scripts/per_adv_conn_privacy.sh index c3c4316ef5a6..018887a2255c 100755 --- a/tests/bsim/bluetooth/host/adv/periodic/tests_scripts/per_adv_conn_privacy.sh +++ b/tests/bsim/bluetooth/host/adv/periodic/tests_scripts/per_adv_conn_privacy.sh @@ -22,6 +22,6 @@ Execute ./bs_${BOARD_TS}_tests_bsim_bluetooth_host_adv_periodic_prj_conf \ -testid=per_adv_conn_privacy_sync -rs=6 Execute ./bs_2G4_phy_v1 -v=${verbosity_level} -s=${simulation_id} \ - -D=2 -sim_length=20e6 $@ + -D=2 -sim_length=15e6 $@ wait_for_background_jobs diff --git a/tests/bsim/bluetooth/host/adv/periodic/tests_scripts/per_adv_long_data.sh b/tests/bsim/bluetooth/host/adv/periodic/tests_scripts/per_adv_long_data.sh index 732e8aa13dd8..48c0934fb88d 100755 --- a/tests/bsim/bluetooth/host/adv/periodic/tests_scripts/per_adv_long_data.sh +++ b/tests/bsim/bluetooth/host/adv/periodic/tests_scripts/per_adv_long_data.sh @@ -22,6 +22,6 @@ Execute ./bs_${BOARD_TS}_tests_bsim_bluetooth_host_adv_periodic_prj_long_data_co -testid=per_adv_long_data_sync -rs=6 Execute ./bs_2G4_phy_v1 -v=${verbosity_level} -s=${simulation_id} \ - -D=2 -sim_length=20e6 $@ + -D=2 -sim_length=15e6 $@ wait_for_background_jobs diff --git a/tests/bsim/bluetooth/host/att/eatt/CMakeLists.txt b/tests/bsim/bluetooth/host/att/eatt/CMakeLists.txt index 9f42ea3eb565..6bea3ce925d5 100644 --- a/tests/bsim/bluetooth/host/att/eatt/CMakeLists.txt +++ b/tests/bsim/bluetooth/host/att/eatt/CMakeLists.txt @@ -5,6 +5,8 @@ cmake_minimum_required(VERSION 3.20.0) find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) project(bsim_test_host) +add_subdirectory(${ZEPHYR_BASE}/tests/bsim/babblekit babblekit) +target_link_libraries(app PRIVATE babblekit) target_sources(app PRIVATE src/common.c diff --git a/tests/bsim/bluetooth/host/att/eatt/src/common.c b/tests/bsim/bluetooth/host/att/eatt/src/common.c index 271d759dcd6e..b0cec2ab3c5b 100644 --- a/tests/bsim/bluetooth/host/att/eatt/src/common.c +++ b/tests/bsim/bluetooth/host/att/eatt/src/common.c @@ -6,6 +6,7 @@ * SPDX-License-Identifier: Apache-2.0 */ +#include "babblekit/testcase.h" #include "common.h" #include "argparse.h" @@ -30,7 +31,7 @@ static void connected(struct bt_conn *conn, uint8_t conn_err) default_conn = NULL; } - FAIL("Failed to connect to %s (%u)\n", addr, conn_err); + TEST_FAIL("Failed to connect to %s (%u)", addr, conn_err); return; } @@ -77,44 +78,30 @@ static void device_found(const bt_addr_le_t *addr, int8_t rssi, uint8_t type, err = bt_le_scan_stop(); if (err) { - FAIL("Stop LE scan failed (err %d)\n", err); + TEST_FAIL("Stop LE scan failed (err %d)", err); } err = bt_conn_le_create(addr, BT_CONN_LE_CREATE_CONN, BT_LE_CONN_PARAM_DEFAULT, &default_conn); if (err) { - FAIL("Create conn failed (err %d)\n", err); + TEST_FAIL("Create conn failed (err %d)", err); } printk("Device connected\n"); } -void test_init(void) -{ - bst_ticker_set_next_tick_absolute(60e6); /* 60 seconds */ - bst_result = In_progress; -} - -void test_tick(bs_time_t HW_device_time) -{ - if (bst_result != Passed) { - bst_result = Failed; - bs_trace_error_time_line("Test eatt finished.\n"); - } -} - void central_setup_and_connect(void) { int err; err = bt_enable(NULL); if (err) { - FAIL("Can't enable Bluetooth (err %d)\n", err); + TEST_FAIL("Can't enable Bluetooth (err %d)", err); } err = bt_le_scan_start(BT_LE_SCAN_ACTIVE, device_found); if (err) { - FAIL("Scanning failed to start (err %d)\n", err); + TEST_FAIL("Scanning failed to start (err %d)", err); } while (!is_connected) { @@ -123,7 +110,7 @@ void central_setup_and_connect(void) err = bt_conn_set_security(default_conn, BT_SECURITY_L2); if (err) { - FAIL("Failed to start encryption procedure\n"); + TEST_FAIL("Failed to start encryption procedure"); } while (!is_encrypted) { @@ -137,12 +124,12 @@ void peripheral_setup_and_connect(void) err = bt_enable(NULL); if (err) { - FAIL("Can't enable Bluetooth (err %d)\n", err); + TEST_FAIL("Can't enable Bluetooth (err %d)", err); } err = bt_le_adv_start(BT_LE_ADV_CONN_FAST_1, ad, ARRAY_SIZE(ad), NULL, 0); if (err) { - FAIL("Advertising failed to start (err %d)\n", err); + TEST_FAIL("Advertising failed to start (err %d)", err); } while (!is_connected) { @@ -168,56 +155,10 @@ void disconnect(void) err = bt_conn_disconnect(default_conn, BT_HCI_ERR_REMOTE_USER_TERM_CONN); if (err) { - FAIL("Disconnection failed (err %d)\n", err); + TEST_FAIL("Disconnection failed (err %d)", err); } while (is_connected) { k_sleep(K_MSEC(100)); } } - - -#define CHANNEL_ID 0 -#define MSG_SIZE 1 - -void backchannel_init(void) -{ - uint device_number = get_device_nbr(); - uint peer_number = device_number ^ 1; - uint device_numbers[] = { peer_number }; - uint channel_numbers[] = { CHANNEL_ID }; - uint *ch; - - ch = bs_open_back_channel(device_number, device_numbers, channel_numbers, - ARRAY_SIZE(channel_numbers)); - if (!ch) { - FAIL("Unable to open backchannel\n"); - } -} - -void backchannel_sync_send(void) -{ - uint8_t sync_msg[MSG_SIZE] = { get_device_nbr() }; - - printk("Sending sync\n"); - bs_bc_send_msg(CHANNEL_ID, sync_msg, ARRAY_SIZE(sync_msg)); -} - -void backchannel_sync_wait(void) -{ - uint8_t sync_msg[MSG_SIZE]; - - while (true) { - if (bs_bc_is_msg_received(CHANNEL_ID) > 0) { - bs_bc_receive_msg(CHANNEL_ID, sync_msg, ARRAY_SIZE(sync_msg)); - if (sync_msg[0] != get_device_nbr()) { - /* Received a message from another device, exit */ - break; - } - } - - k_sleep(K_MSEC(1)); - } - - printk("Sync received\n"); -} diff --git a/tests/bsim/bluetooth/host/att/eatt/src/common.h b/tests/bsim/bluetooth/host/att/eatt/src/common.h index 49bc88f03fda..6dd9519f8d7f 100644 --- a/tests/bsim/bluetooth/host/att/eatt/src/common.h +++ b/tests/bsim/bluetooth/host/att/eatt/src/common.h @@ -17,32 +17,6 @@ #include #include -#include "bs_types.h" -#include "bs_tracing.h" -#include "bstests.h" -#include "bs_pc_backchannel.h" - -extern enum bst_result_t bst_result; - -#define CREATE_FLAG(flag) static atomic_t flag = (atomic_t)false -#define SET_FLAG(flag) (void)atomic_set(&flag, (atomic_t)true) -#define WAIT_FOR_FLAG(flag) \ - while (!(bool)atomic_get(&flag)) { \ - (void)k_sleep(K_MSEC(1)); \ - } - -#define FAIL(...) \ - do { \ - bst_result = Failed; \ - bs_trace_error_time_line(__VA_ARGS__); \ - } while (0) - -#define PASS(...) \ - do { \ - bst_result = Passed; \ - bs_trace_info_time(1, __VA_ARGS__); \ - } while (0) - extern volatile int num_eatt_channels; extern struct bt_conn *default_conn; @@ -50,8 +24,3 @@ void central_setup_and_connect(void); void peripheral_setup_and_connect(void); void wait_for_disconnect(void); void disconnect(void); -void test_init(void); -void test_tick(bs_time_t HW_device_time); -void backchannel_init(void); -void backchannel_sync_send(void); -void backchannel_sync_wait(void); diff --git a/tests/bsim/bluetooth/host/att/eatt/src/main_autoconnect.c b/tests/bsim/bluetooth/host/att/eatt/src/main_autoconnect.c index 89b46b30f252..ba19c66db4e4 100644 --- a/tests/bsim/bluetooth/host/att/eatt/src/main_autoconnect.c +++ b/tests/bsim/bluetooth/host/att/eatt/src/main_autoconnect.c @@ -6,6 +6,7 @@ * SPDX-License-Identifier: Apache-2.0 */ +#include "babblekit/testcase.h" #include "common.h" static void test_peripheral_main(void) @@ -21,7 +22,7 @@ static void test_peripheral_main(void) disconnect(); - PASS("EATT Peripheral tests Passed\n"); + TEST_PASS("EATT Peripheral tests Passed"); } static void test_central_main(void) @@ -34,22 +35,18 @@ static void test_central_main(void) wait_for_disconnect(); - PASS("EATT Central tests Passed\n"); + TEST_PASS("EATT Central tests Passed"); } static const struct bst_test_instance test_def[] = { { .test_id = "peripheral_autoconnect", .test_descr = "Peripheral autoconnect", - .test_pre_init_f = test_init, - .test_tick_f = test_tick, .test_main_f = test_peripheral_main, }, { .test_id = "central_autoconnect", .test_descr = "Central autoconnect", - .test_pre_init_f = test_init, - .test_tick_f = test_tick, .test_main_f = test_central_main, }, BSTEST_END_MARKER, diff --git a/tests/bsim/bluetooth/host/att/eatt/src/main_collision.c b/tests/bsim/bluetooth/host/att/eatt/src/main_collision.c index d4bd2f8abb24..f280ab28ea38 100644 --- a/tests/bsim/bluetooth/host/att/eatt/src/main_collision.c +++ b/tests/bsim/bluetooth/host/att/eatt/src/main_collision.c @@ -6,25 +6,27 @@ * SPDX-License-Identifier: Apache-2.0 */ +#include "babblekit/testcase.h" +#include "babblekit/sync.h" #include "common.h" static void test_peripheral_main(void) { int err; - backchannel_init(); + TEST_ASSERT(bk_sync_init() == 0, "Failed to open backchannel"); peripheral_setup_and_connect(); /* * we need to sync with peer to ensure that we get collisions */ - backchannel_sync_send(); - backchannel_sync_wait(); + bk_sync_send(); + bk_sync_wait(); err = bt_eatt_connect(default_conn, CONFIG_BT_EATT_MAX); if (err) { - FAIL("Sending credit based connection request failed (err %d)\n", err); + TEST_FAIL("Sending credit based connection request failed (err %d)", err); } while (bt_eatt_count(default_conn) < CONFIG_BT_EATT_MAX) { @@ -36,23 +38,23 @@ static void test_peripheral_main(void) disconnect(); - PASS("EATT Peripheral tests Passed\n"); + TEST_PASS("EATT Peripheral tests Passed"); } static void test_central_main(void) { int err; - backchannel_init(); + TEST_ASSERT(bk_sync_init() == 0, "Failed to open backchannel"); central_setup_and_connect(); - backchannel_sync_wait(); - backchannel_sync_send(); + bk_sync_wait(); + bk_sync_send(); err = bt_eatt_connect(default_conn, CONFIG_BT_EATT_MAX); if (err) { - FAIL("Sending credit based connection request failed (err %d)\n", err); + TEST_FAIL("Sending credit based connection request failed (err %d)", err); } while (bt_eatt_count(default_conn) < CONFIG_BT_EATT_MAX) { @@ -61,22 +63,18 @@ static void test_central_main(void) wait_for_disconnect(); - PASS("EATT Central tests Passed\n"); + TEST_PASS("EATT Central tests Passed"); } static const struct bst_test_instance test_def[] = { { .test_id = "peripheral", .test_descr = "Peripheral Collision", - .test_pre_init_f = test_init, - .test_tick_f = test_tick, .test_main_f = test_peripheral_main, }, { .test_id = "central", .test_descr = "Central Collision", - .test_pre_init_f = test_init, - .test_tick_f = test_tick, .test_main_f = test_central_main, }, BSTEST_END_MARKER, diff --git a/tests/bsim/bluetooth/host/att/eatt/src/main_lowres.c b/tests/bsim/bluetooth/host/att/eatt/src/main_lowres.c index fb93cbdba0e0..c6b3752ad24a 100644 --- a/tests/bsim/bluetooth/host/att/eatt/src/main_lowres.c +++ b/tests/bsim/bluetooth/host/att/eatt/src/main_lowres.c @@ -6,6 +6,7 @@ * SPDX-License-Identifier: Apache-2.0 */ +#include "babblekit/testcase.h" #include "common.h" static void test_peripheral_main(void) @@ -18,7 +19,7 @@ static void test_peripheral_main(void) disconnect(); - PASS("EATT Peripheral tests Passed\n"); + TEST_PASS("EATT Peripheral tests Passed"); } static void test_central_main(void) @@ -35,22 +36,18 @@ static void test_central_main(void) wait_for_disconnect(); - PASS("EATT Central tests Passed\n"); + TEST_PASS("EATT Central tests Passed"); } static const struct bst_test_instance test_def[] = { { .test_id = "peripheral_lowres", .test_descr = "Peripheral lowres", - .test_pre_init_f = test_init, - .test_tick_f = test_tick, .test_main_f = test_peripheral_main, }, { .test_id = "central_lowres", .test_descr = "Central lowres", - .test_pre_init_f = test_init, - .test_tick_f = test_tick, .test_main_f = test_central_main, }, BSTEST_END_MARKER, diff --git a/tests/bsim/bluetooth/host/att/eatt/src/main_reconfigure.c b/tests/bsim/bluetooth/host/att/eatt/src/main_reconfigure.c index c208fc094847..138583f4ce26 100644 --- a/tests/bsim/bluetooth/host/att/eatt/src/main_reconfigure.c +++ b/tests/bsim/bluetooth/host/att/eatt/src/main_reconfigure.c @@ -6,13 +6,16 @@ * SPDX-License-Identifier: Apache-2.0 */ +#include "babblekit/testcase.h" +#include "babblekit/flags.h" +#include "babblekit/sync.h" #include "common.h" #include #define NEW_MTU 100 -CREATE_FLAG(flag_reconfigured); +static DEFINE_FLAG(flag_reconfigured); void att_mtu_updated(struct bt_conn *conn, uint16_t tx, uint16_t rx) { @@ -29,7 +32,7 @@ static struct bt_gatt_cb cb = { static void test_peripheral_main(void) { - backchannel_init(); + TEST_ASSERT(bk_sync_init() == 0, "Failed to open backchannel"); peripheral_setup_and_connect(); @@ -39,25 +42,25 @@ static void test_peripheral_main(void) while (bt_eatt_count(default_conn) < CONFIG_BT_EATT_MAX) { k_sleep(K_MSEC(10)); } - backchannel_sync_send(); - backchannel_sync_wait(); + bk_sync_send(); + bk_sync_wait(); WAIT_FOR_FLAG(flag_reconfigured); - backchannel_sync_send(); + bk_sync_send(); /* Wait for the reconfigured flag on the other end */ - backchannel_sync_wait(); + bk_sync_wait(); disconnect(); - PASS("EATT Peripheral tests Passed\n"); + TEST_PASS("EATT Peripheral tests Passed"); } static void test_central_main(void) { int err; - backchannel_init(); + TEST_ASSERT(bk_sync_init() == 0, "Failed to open backchannel"); central_setup_and_connect(); @@ -69,35 +72,31 @@ static void test_central_main(void) err = bt_eatt_reconfigure(default_conn, NEW_MTU); if (err < 0) { - FAIL("Reconfigure failed (%d)\n", err); + TEST_FAIL("Reconfigure failed (%d)", err); } - backchannel_sync_send(); - backchannel_sync_wait(); + bk_sync_send(); + bk_sync_wait(); WAIT_FOR_FLAG(flag_reconfigured); - backchannel_sync_send(); + bk_sync_send(); /* Wait for the reconfigured flag on the other end */ - backchannel_sync_wait(); + bk_sync_wait(); wait_for_disconnect(); - PASS("EATT Central tests Passed\n"); + TEST_PASS("EATT Central tests Passed"); } static const struct bst_test_instance test_def[] = { { .test_id = "peripheral_reconfigure", .test_descr = "Peripheral reconfigure", - .test_pre_init_f = test_init, - .test_tick_f = test_tick, .test_main_f = test_peripheral_main, }, { .test_id = "central_reconfigure", .test_descr = "Central reconfigure", - .test_pre_init_f = test_init, - .test_tick_f = test_tick, .test_main_f = test_central_main, }, BSTEST_END_MARKER, diff --git a/tests/bsim/bluetooth/host/att/eatt/tests_scripts/autoconnect.sh b/tests/bsim/bluetooth/host/att/eatt/tests_scripts/autoconnect.sh index 057e76f18131..47fc5f0e2a0e 100755 --- a/tests/bsim/bluetooth/host/att/eatt/tests_scripts/autoconnect.sh +++ b/tests/bsim/bluetooth/host/att/eatt/tests_scripts/autoconnect.sh @@ -17,6 +17,6 @@ Execute ./bs_${BOARD_TS}_tests_bsim_bluetooth_host_att_eatt_prj_autoconnect_conf -v=${verbosity_level} -s=${simulation_id} -d=1 -testid=peripheral_autoconnect -RealEncryption=1 Execute ./bs_2G4_phy_v1 -v=${verbosity_level} -s=${simulation_id} \ - -D=2 -sim_length=200e6 $@ + -D=2 -sim_length=60e6 $@ wait_for_background_jobs diff --git a/tests/bsim/bluetooth/host/att/eatt/tests_scripts/collision.sh b/tests/bsim/bluetooth/host/att/eatt/tests_scripts/collision.sh index c89bab275481..010ac1600cbd 100755 --- a/tests/bsim/bluetooth/host/att/eatt/tests_scripts/collision.sh +++ b/tests/bsim/bluetooth/host/att/eatt/tests_scripts/collision.sh @@ -17,6 +17,6 @@ Execute ./bs_${BOARD_TS}_tests_bsim_bluetooth_host_att_eatt_prj_collision_conf \ -v=${verbosity_level} -s=${simulation_id} -d=1 -testid=peripheral -RealEncryption=1 Execute ./bs_2G4_phy_v1 -v=${verbosity_level} -s=${simulation_id} \ - -D=2 -sim_length=200e6 $@ + -D=2 -sim_length=60e6 $@ wait_for_background_jobs diff --git a/tests/bsim/bluetooth/host/att/eatt/tests_scripts/lowres.sh b/tests/bsim/bluetooth/host/att/eatt/tests_scripts/lowres.sh index 0d988761adfb..b12d045edd9a 100755 --- a/tests/bsim/bluetooth/host/att/eatt/tests_scripts/lowres.sh +++ b/tests/bsim/bluetooth/host/att/eatt/tests_scripts/lowres.sh @@ -16,6 +16,6 @@ Execute ./bs_${BOARD_TS}_tests_bsim_bluetooth_host_att_eatt_prj_lowres_conf \ -v=${verbosity_level} -s=${simulation_id} -d=1 -testid=peripheral_lowres -RealEncryption=1 Execute ./bs_2G4_phy_v1 -v=${verbosity_level} -s=${simulation_id} \ - -D=2 -sim_length=200e6 $@ + -D=2 -sim_length=60e6 $@ wait_for_background_jobs diff --git a/tests/bsim/bluetooth/host/att/eatt/tests_scripts/multiple_conn.sh b/tests/bsim/bluetooth/host/att/eatt/tests_scripts/multiple_conn.sh index e3919eb4a265..9bdf58693dc3 100755 --- a/tests/bsim/bluetooth/host/att/eatt/tests_scripts/multiple_conn.sh +++ b/tests/bsim/bluetooth/host/att/eatt/tests_scripts/multiple_conn.sh @@ -17,6 +17,6 @@ Execute ./bs_${BOARD_TS}_tests_bsim_bluetooth_host_att_eatt_prj_multiple_conn_co -v=${verbosity_level} -s=${simulation_id} -d=1 -testid=peripheral -RealEncryption=1 Execute ./bs_2G4_phy_v1 -v=${verbosity_level} -s=${simulation_id} \ - -D=2 -sim_length=200e6 $@ + -D=2 -sim_length=60e6 $@ wait_for_background_jobs diff --git a/tests/bsim/bluetooth/host/att/eatt_notif/CMakeLists.txt b/tests/bsim/bluetooth/host/att/eatt_notif/CMakeLists.txt index 202f757b3b72..bb9f7adb430b 100644 --- a/tests/bsim/bluetooth/host/att/eatt_notif/CMakeLists.txt +++ b/tests/bsim/bluetooth/host/att/eatt_notif/CMakeLists.txt @@ -5,6 +5,9 @@ cmake_minimum_required(VERSION 3.20.0) find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) project(bsim_test_eatt_notif) +add_subdirectory(${ZEPHYR_BASE}/tests/bsim/babblekit babblekit) +target_link_libraries(app PRIVATE babblekit) + FILE(GLOB app_sources src/*.c) target_sources(app PRIVATE ${app_sources} ) diff --git a/tests/bsim/bluetooth/host/att/eatt_notif/src/client_test.c b/tests/bsim/bluetooth/host/att/eatt_notif/src/client_test.c index 5f403304fcb6..e846b1d05b94 100644 --- a/tests/bsim/bluetooth/host/att/eatt_notif/src/client_test.c +++ b/tests/bsim/bluetooth/host/att/eatt_notif/src/client_test.c @@ -14,16 +14,25 @@ * on EATT channels. */ +#include +#include +#include +#include #include -#include +#include #include +#include +#include #include +#include "babblekit/testcase.h" +#include "babblekit/flags.h" +#include "babblekit/sync.h" #include "common.h" -CREATE_FLAG(flag_is_connected); -CREATE_FLAG(flag_discover_complete); -CREATE_FLAG(flag_is_encrypted); +static DEFINE_FLAG(flag_is_connected); +static DEFINE_FLAG(flag_discover_complete); +static DEFINE_FLAG(flag_is_encrypted); static struct bt_conn *g_conn; static const struct bt_gatt_attr *local_attr; @@ -42,7 +51,7 @@ static void connected(struct bt_conn *conn, uint8_t err) bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr)); if (err != 0) { - FAIL("Failed to connect to %s (%u)\n", addr, err); + TEST_FAIL("Failed to connect to %s (%u)", addr, err); return; } @@ -103,14 +112,14 @@ void device_found(const bt_addr_le_t *addr, int8_t rssi, uint8_t type, printk("Stopping scan\n"); err = bt_le_scan_stop(); if (err != 0) { - FAIL("Could not stop scan: %d"); + TEST_FAIL("Could not stop scan: %d"); return; } err = bt_conn_le_create(addr, BT_CONN_LE_CREATE_CONN, BT_LE_CONN_PARAM_DEFAULT, &g_conn); if (err != 0) { - FAIL("Could not connect to peer: %d", err); + TEST_FAIL("Could not connect to peer: %d", err); } } @@ -157,7 +166,7 @@ static void gatt_discover(void) err = bt_gatt_discover(g_conn, &discover_params); if (err != 0) { - FAIL("Discover failed(err %d)\n", err); + TEST_FAIL("Discover failed(err %d)", err); } } @@ -172,16 +181,16 @@ static void test_main(void) { int err; - device_sync_init(PERIPHERAL_ID); + TEST_ASSERT(bk_sync_init() == 0, "Failed to open backchannel"); err = bt_enable(NULL); if (err != 0) { - FAIL("Bluetooth enable failed (err %d)\n", err); + TEST_FAIL("Bluetooth enable failed (err %d)", err); } err = bt_le_scan_start(BT_LE_SCAN_PASSIVE, device_found); if (err != 0) { - FAIL("Scanning failed to start (err %d)\n", err); + TEST_FAIL("Scanning failed to start (err %d)", err); } printk("Scanning successfully started\n"); @@ -190,14 +199,14 @@ static void test_main(void) err = bt_conn_set_security(g_conn, BT_SECURITY_L2); if (err) { - FAIL("Failed to start encryption procedure\n"); + TEST_FAIL("Failed to start encryption procedure"); } WAIT_FOR_FLAG(flag_is_encrypted); err = bt_eatt_connect(g_conn, CONFIG_BT_EATT_MAX); if (err) { - FAIL("Sending credit based connection request failed (err %d)\n", err); + TEST_FAIL("Sending credit based connection request failed (err %d)", err); } /* Wait for the channels to be connected */ @@ -206,7 +215,7 @@ static void test_main(void) } printk("Waiting for sync\n"); - device_sync_wait(); + bk_sync_wait(); local_attr = &g_svc.attrs[1]; @@ -227,7 +236,7 @@ static void test_main(void) printk("Connecting %d bearers\n", EATT_BEARERS_TEST); err = bt_eatt_connect(g_conn, EATT_BEARERS_TEST); if (err) { - FAIL("Sending credit based connection request failed (err %d)\n", err); + TEST_FAIL("Sending credit based connection request failed (err %d)", err); } /* Wait for the channels to be connected */ @@ -237,22 +246,20 @@ static void test_main(void) printk("############# Send notifications during discovery request\n"); gatt_discover(); - while (!TEST_FLAG(flag_discover_complete)) { + while (!IS_FLAG_SET(flag_discover_complete)) { printk("Notifying...\n"); send_notification(); } printk("Sending final sync\n"); - device_sync_send(); + bk_sync_send(); - PASS("Client Passed\n"); + TEST_PASS("Client Passed"); } static const struct bst_test_instance test_vcs[] = { { .test_id = "client", - .test_pre_init_f = test_init, - .test_tick_f = test_tick, .test_main_f = test_main }, BSTEST_END_MARKER diff --git a/tests/bsim/bluetooth/host/att/eatt_notif/src/common.c b/tests/bsim/bluetooth/host/att/eatt_notif/src/common.c deleted file mode 100644 index 1df3895fc3e9..000000000000 --- a/tests/bsim/bluetooth/host/att/eatt_notif/src/common.c +++ /dev/null @@ -1,67 +0,0 @@ -/* - * Copyright (c) 2022 Nordic Semiconductor ASA - * - * SPDX-License-Identifier: Apache-2.0 - */ - -#include "common.h" -#include - -#define LOG_MODULE_NAME common - -LOG_MODULE_REGISTER(LOG_MODULE_NAME, LOG_LEVEL_DBG); - -void test_tick(bs_time_t HW_device_time) -{ - if (bst_result != Passed) { - FAIL("test failed (not passed after %i seconds)\n", WAIT_TIME); - } -} - -void test_init(void) -{ - bst_ticker_set_next_tick_absolute(WAIT_TIME); - bst_result = In_progress; -} - -/* Call in init functions*/ -void device_sync_init(uint device_nbr) -{ - uint peer; - - if (device_nbr == CENTRAL_ID) { - peer = PERIPHERAL_ID; - } else { - peer = CENTRAL_ID; - } - - uint dev_nbrs[BACK_CHANNELS] = { peer }; - uint channel_nbrs[BACK_CHANNELS] = { 0 }; - const uint *ch = bs_open_back_channel(device_nbr, dev_nbrs, channel_nbrs, BACK_CHANNELS); - - if (!ch) { - LOG_ERR("bs_open_back_channel failed!"); - } -} - -/* Call it to make peer to proceed.*/ -void device_sync_send(void) -{ - uint8_t msg[1] = "S"; - - bs_bc_send_msg(0, msg, sizeof(msg)); -} - -/* Wait until peer send sync*/ -void device_sync_wait(void) -{ - int size_msg_received = 0; - uint8_t msg; - - while (!size_msg_received) { - size_msg_received = bs_bc_is_msg_received(0); - k_sleep(K_MSEC(1)); - } - - bs_bc_receive_msg(0, &msg, size_msg_received); -} diff --git a/tests/bsim/bluetooth/host/att/eatt_notif/src/common.h b/tests/bsim/bluetooth/host/att/eatt_notif/src/common.h index aecbda9cbe6c..ec2c039acb95 100644 --- a/tests/bsim/bluetooth/host/att/eatt_notif/src/common.h +++ b/tests/bsim/bluetooth/host/att/eatt_notif/src/common.h @@ -6,49 +6,6 @@ * SPDX-License-Identifier: Apache-2.0 */ -#include - -#include "bs_types.h" -#include "bs_tracing.h" -#include "time_machine.h" -#include "bstests.h" - -#include -#include -#include - -#include -#include -#include -#include -#include -#include "bs_pc_backchannel.h" - -extern enum bst_result_t bst_result; - -#define WAIT_TIME (30 * 1e6) /*seconds*/ - -#define CREATE_FLAG(flag) static atomic_t flag = (atomic_t)false -#define SET_FLAG(flag) (void)atomic_set(&flag, (atomic_t)true) -#define UNSET_FLAG(flag) (void)atomic_set(&flag, (atomic_t)false) -#define TEST_FLAG(flag) (atomic_get(&flag) == (atomic_t)true) -#define WAIT_FOR_FLAG(flag) \ - while (!(bool)atomic_get(&flag)) { \ - (void)k_sleep(K_MSEC(1)); \ - } - -#define FAIL(...) \ - do { \ - bst_result = Failed; \ - bs_trace_error_time_line(__VA_ARGS__); \ - } while (0) - -#define PASS(...) \ - do { \ - bst_result = Passed; \ - bs_trace_info_time(1, __VA_ARGS__); \ - } while (0) - #define CHRC_SIZE 10 #define LONG_CHRC_SIZE 40 @@ -63,13 +20,3 @@ extern enum bst_result_t bst_result; #define TEST_LONG_CHRC_UUID \ BT_UUID_DECLARE_128(0x01, 0x23, 0x45, 0x67, 0x89, 0x01, 0x02, 0x03, \ 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0xFF, 0x11) - -#define CENTRAL_ID 0 -#define PERIPHERAL_ID 1 -#define BACK_CHANNELS 1 - -void test_tick(bs_time_t HW_device_time); -void test_init(void); -void device_sync_init(uint device_nbr); -void device_sync_send(void); -void device_sync_wait(void); diff --git a/tests/bsim/bluetooth/host/att/eatt_notif/src/server_test.c b/tests/bsim/bluetooth/host/att/eatt_notif/src/server_test.c index 474fb1011139..94cf4ab699b1 100644 --- a/tests/bsim/bluetooth/host/att/eatt_notif/src/server_test.c +++ b/tests/bsim/bluetooth/host/att/eatt_notif/src/server_test.c @@ -4,15 +4,27 @@ * SPDX-License-Identifier: Apache-2.0 */ -#include "common.h" +#include +#include +#include +#include +#include +#include #include +#include +#include #include -CREATE_FLAG(flag_discover_complete); +#include "babblekit/testcase.h" +#include "babblekit/flags.h" +#include "babblekit/sync.h" +#include "common.h" + +static DEFINE_FLAG(flag_discover_complete); extern enum bst_result_t bst_result; -CREATE_FLAG(flag_is_connected); +static DEFINE_FLAG(flag_is_connected); static struct bt_conn *g_conn; @@ -23,7 +35,7 @@ static void connected(struct bt_conn *conn, uint8_t err) bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr)); if (err != 0) { - FAIL("Failed to connect to %s (%u)\n", addr, err); + TEST_FAIL("Failed to connect to %s (%u)", addr, err); return; } @@ -65,7 +77,7 @@ static uint8_t discover_func(struct bt_conn *conn, if (attr == NULL) { if (chrc_handle == 0) { - FAIL("Did not discover chrc (%x)", chrc_handle); + TEST_FAIL("Did not discover chrc (%x)", chrc_handle); } (void)memset(params, 0, sizeof(*params)); @@ -86,7 +98,7 @@ static uint8_t discover_func(struct bt_conn *conn, err = bt_gatt_discover(conn, params); if (err != 0) { - FAIL("Discover failed (err %d)\n", err); + TEST_FAIL("Discover failed (err %d)", err); } return BT_GATT_ITER_STOP; @@ -119,7 +131,7 @@ static void gatt_discover(void) err = bt_gatt_discover(g_conn, &discover_params); if (err != 0) { - FAIL("Discover failed(err %d)\n", err); + TEST_FAIL("Discover failed(err %d)", err); } printk("Discovery complete\n"); @@ -146,7 +158,7 @@ void subscribed_cb(struct bt_conn *conn, uint8_t err, params->ccc_handle); printk("Sending sync to peer\n"); - device_sync_send(); + bk_sync_send(); } static struct bt_gatt_discover_params disc_params; @@ -168,7 +180,7 @@ static void gatt_subscribe(void) printk("Subscribing: val %x\n", chrc_handle); err = bt_gatt_subscribe(g_conn, &subscribe_params); if (err != 0) { - FAIL("Subscription failed(err %d)\n", err); + TEST_FAIL("Subscription failed(err %d)", err); } } @@ -179,11 +191,11 @@ static void test_main(void) BT_DATA_BYTES(BT_DATA_FLAGS, (BT_LE_AD_GENERAL | BT_LE_AD_NO_BREDR)) }; - device_sync_init(CENTRAL_ID); + TEST_ASSERT(bk_sync_init() == 0, "Failed to open backchannel"); err = bt_enable(NULL); if (err != 0) { - FAIL("Bluetooth init failed (err %d)\n", err); + TEST_FAIL("Bluetooth init failed (err %d)", err); return; } @@ -191,7 +203,7 @@ static void test_main(void) err = bt_le_adv_start(BT_LE_ADV_CONN_FAST_1, ad, ARRAY_SIZE(ad), NULL, 0); if (err != 0) { - FAIL("Advertising failed to start (err %d)\n", err); + TEST_FAIL("Advertising failed to start (err %d)", err); return; } @@ -209,16 +221,14 @@ static void test_main(void) gatt_subscribe(); printk("Waiting for final sync\n"); - device_sync_wait(); + bk_sync_wait(); - PASS("Server Passed\n"); + TEST_PASS("Server Passed"); } static const struct bst_test_instance test_server[] = { { .test_id = "server", - .test_pre_init_f = test_init, - .test_tick_f = test_tick, .test_main_f = test_main }, BSTEST_END_MARKER diff --git a/tests/bsim/bluetooth/host/att/eatt_notif/test_scripts/eatt_notif.sh b/tests/bsim/bluetooth/host/att/eatt_notif/test_scripts/eatt_notif.sh index 39000efbc742..39d442e38b2e 100755 --- a/tests/bsim/bluetooth/host/att/eatt_notif/test_scripts/eatt_notif.sh +++ b/tests/bsim/bluetooth/host/att/eatt_notif/test_scripts/eatt_notif.sh @@ -19,6 +19,6 @@ Execute ./bs_${BOARD_TS}_tests_bsim_bluetooth_host_att_eatt_notif_prj_conf \ -v=${verbosity_level} -s=${simulation_id} -d=1 -testid=server -RealEncryption=1 Execute ./bs_2G4_phy_v1 -v=${verbosity_level} -s=${simulation_id} \ - -D=2 -sim_length=60e6 $@ + -D=2 -sim_length=30e6 $@ wait_for_background_jobs diff --git a/tests/bsim/bluetooth/host/gatt/authorization/CMakeLists.txt b/tests/bsim/bluetooth/host/gatt/authorization/CMakeLists.txt index acb0dd45947b..6746842d40b4 100644 --- a/tests/bsim/bluetooth/host/gatt/authorization/CMakeLists.txt +++ b/tests/bsim/bluetooth/host/gatt/authorization/CMakeLists.txt @@ -5,6 +5,9 @@ cmake_minimum_required(VERSION 3.20.0) find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) project(bsim_test_gatt_authorization) +add_subdirectory(${ZEPHYR_BASE}/tests/bsim/babblekit babblekit) +target_link_libraries(app PRIVATE babblekit) + FILE(GLOB app_sources src/*.c) target_sources(app PRIVATE ${app_sources} ) diff --git a/tests/bsim/bluetooth/host/gatt/authorization/src/common.c b/tests/bsim/bluetooth/host/gatt/authorization/src/common.c deleted file mode 100644 index adff2dd05ef6..000000000000 --- a/tests/bsim/bluetooth/host/gatt/authorization/src/common.c +++ /dev/null @@ -1,20 +0,0 @@ -/* - * Copyright (c) 2024 Nordic Semiconductor ASA - * - * SPDX-License-Identifier: Apache-2.0 - */ - -#include "common.h" - -void test_tick(bs_time_t HW_device_time) -{ - if (bst_result != Passed) { - FAIL("test failed (not passed after %i seconds)\n", WAIT_TIME); - } -} - -void test_init(void) -{ - bst_ticker_set_next_tick_absolute(WAIT_TIME); - bst_result = In_progress; -} diff --git a/tests/bsim/bluetooth/host/gatt/authorization/src/common.h b/tests/bsim/bluetooth/host/gatt/authorization/src/common.h index 339919dfe884..358b8ec9de56 100644 --- a/tests/bsim/bluetooth/host/gatt/authorization/src/common.h +++ b/tests/bsim/bluetooth/host/gatt/authorization/src/common.h @@ -6,47 +6,6 @@ * SPDX-License-Identifier: Apache-2.0 */ -#include - -#include "bs_types.h" -#include "bs_tracing.h" -#include "time_machine.h" -#include "bstests.h" - -#include -#include -#include - -#include -#include -#include -#include -#include - -extern enum bst_result_t bst_result; - -#define WAIT_TIME (30 * 1e6) /*seconds*/ - -#define CREATE_FLAG(flag) static atomic_t flag = (atomic_t)false -#define SET_FLAG(flag) (void)atomic_set(&flag, (atomic_t)true) -#define UNSET_FLAG(flag) (void)atomic_set(&flag, (atomic_t)false) -#define WAIT_FOR_FLAG(flag) \ - while (!(bool)atomic_get(&flag)) { \ - (void)k_sleep(K_MSEC(1)); \ - } - -#define FAIL(...) \ - do { \ - bst_result = Failed; \ - bs_trace_error_time_line(__VA_ARGS__); \ - } while (0) - -#define PASS(...) \ - do { \ - bst_result = Passed; \ - bs_trace_info_time(1, __VA_ARGS__); \ - } while (0) - #define CHRC_SIZE 10 #define TEST_SERVICE_UUID \ @@ -68,6 +27,3 @@ extern enum bst_result_t bst_result; #define TEST_CP_CHRC_UUID \ BT_UUID_DECLARE_128(0x01, 0x23, 0x45, 0x67, 0x89, 0x01, 0x02, 0x03, \ 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0xF0, 0x00) - -void test_tick(bs_time_t HW_device_time); -void test_init(void); diff --git a/tests/bsim/bluetooth/host/gatt/authorization/src/gatt_client_test.c b/tests/bsim/bluetooth/host/gatt/authorization/src/gatt_client_test.c index fb8d7e711338..3e162d50297f 100644 --- a/tests/bsim/bluetooth/host/gatt/authorization/src/gatt_client_test.c +++ b/tests/bsim/bluetooth/host/gatt/authorization/src/gatt_client_test.c @@ -4,15 +4,25 @@ * SPDX-License-Identifier: Apache-2.0 */ +#include +#include +#include +#include + #include +#include +#include +#include #include +#include "babblekit/testcase.h" +#include "babblekit/flags.h" #include "common.h" -CREATE_FLAG(flag_is_connected); -CREATE_FLAG(flag_discover_complete); -CREATE_FLAG(flag_write_complete); -CREATE_FLAG(flag_read_complete); +static DEFINE_FLAG(flag_is_connected); +static DEFINE_FLAG(flag_discover_complete); +static DEFINE_FLAG(flag_write_complete); +static DEFINE_FLAG(flag_read_complete); static struct bt_conn *g_conn; static uint16_t unhandled_chrc_handle; @@ -31,7 +41,7 @@ static void connected(struct bt_conn *conn, uint8_t err) bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr)); if (err != 0) { - FAIL("Failed to connect to %s (%u)\n", addr, err); + TEST_FAIL("Failed to connect to %s (%u)", addr, err); return; } @@ -86,14 +96,14 @@ void device_found(const bt_addr_le_t *addr, int8_t rssi, uint8_t type, printk("Stopping scan\n"); err = bt_le_scan_stop(); if (err != 0) { - FAIL("Could not stop scan: %d"); + TEST_FAIL("Could not stop scan: %d"); return; } err = bt_conn_le_create(addr, BT_CONN_LE_CREATE_CONN, BT_LE_CONN_PARAM_DEFAULT, &g_conn); if (err != 0) { - FAIL("Could not connect to peer: %d", err); + TEST_FAIL("Could not connect to peer: %d", err); } } @@ -107,7 +117,7 @@ static uint8_t discover_func(struct bt_conn *conn, if (unhandled_chrc_handle == 0 || unauthorized_chrc_handle == 0 || authorized_chrc_handle == 0) { - FAIL("Did not discover required characterstics"); + TEST_FAIL("Did not discover required characterstics"); } (void)memset(params, 0, sizeof(*params)); @@ -128,7 +138,7 @@ static uint8_t discover_func(struct bt_conn *conn, err = bt_gatt_discover(conn, params); if (err != 0) { - FAIL("Discover failed (err %d)\n", err); + TEST_FAIL("Discover failed (err %d)", err); } return BT_GATT_ITER_STOP; @@ -168,7 +178,7 @@ static void gatt_discover(void) err = bt_gatt_discover(g_conn, &discover_params); if (err != 0) { - FAIL("Discover failed(err %d)\n", err); + TEST_FAIL("Discover failed(err %d)", err); } WAIT_FOR_FLAG(flag_discover_complete); @@ -179,11 +189,11 @@ static void gatt_write_cb(struct bt_conn *conn, uint8_t err, struct bt_gatt_write_params *params) { if ((err != BT_ATT_ERR_SUCCESS) && (params->handle != unauthorized_chrc_handle)) { - FAIL("Write failed on authorized characteristics: 0x%02X\n", err); + TEST_FAIL("Write failed on authorized characteristics: 0x%02X", err); } if ((err != BT_ATT_ERR_AUTHORIZATION) && (params->handle == unauthorized_chrc_handle)) { - FAIL("Write failed on unauthorized characteristics: 0x%02X\n", err); + TEST_FAIL("Write failed on unauthorized characteristics: 0x%02X", err); } (void)memset(params, 0, sizeof(*params)); @@ -207,7 +217,7 @@ static void gatt_write(uint16_t handle) err = bt_gatt_write(g_conn, &write_params); if (err != 0) { - FAIL("bt_gatt_write failed: %d\n", err); + TEST_FAIL("bt_gatt_write failed: %d", err); } WAIT_FOR_FLAG(flag_write_complete); @@ -231,7 +241,7 @@ static void gatt_cp_write(void) err = bt_gatt_write(g_conn, &write_params); if (err != 0) { - FAIL("bt_gatt_write failed: %d\n", err); + TEST_FAIL("bt_gatt_write failed: %d", err); } WAIT_FOR_FLAG(flag_write_complete); @@ -244,16 +254,16 @@ static uint8_t gatt_read_cb(struct bt_conn *conn, uint8_t err, { if ((err != BT_ATT_ERR_SUCCESS) && (params->single.handle != unauthorized_chrc_handle)) { - FAIL("Read failed on authorized characteristics: 0x%02X\n", err); + TEST_FAIL("Read failed on authorized characteristics: 0x%02X", err); if ((length != CHRC_SIZE) || (memcmp(data, chrc_data, length) != 0)) { - FAIL("chrc data different than expected", err); + TEST_FAIL("chrc data different than expected: 0x%02X", err); } } if ((err != BT_ATT_ERR_AUTHORIZATION) && (params->single.handle == unauthorized_chrc_handle)) { - FAIL("Read failed on unauthorized characteristics: 0x%02X\n", err); + TEST_FAIL("Read failed on unauthorized characteristics: 0x%02X", err); } (void)memset(params, 0, sizeof(*params)); @@ -279,7 +289,7 @@ static void gatt_read(uint16_t handle) err = bt_gatt_read(g_conn, &read_params); if (err != 0) { - FAIL("bt_gatt_read failed: %d\n", err); + TEST_FAIL("bt_gatt_read failed: %d", err); } WAIT_FOR_FLAG(flag_read_complete); @@ -301,12 +311,12 @@ static void test_main(void) err = bt_enable(NULL); if (err != 0) { - FAIL("Bluetooth discover failed (err %d)\n", err); + TEST_FAIL("Bluetooth discover failed (err %d)", err); } err = bt_le_scan_start(BT_LE_SCAN_PASSIVE, device_found); if (err != 0) { - FAIL("Scanning failed to start (err %d)\n", err); + TEST_FAIL("Scanning failed to start (err %d)", err); } printk("Scanning successfully started\n"); @@ -324,14 +334,12 @@ static void test_main(void) printk("Interacting with the authorized characteristic\n"); gatt_interact(authorized_chrc_handle); - PASS("GATT client Passed\n"); + TEST_PASS("GATT client Passed"); } static const struct bst_test_instance test_vcs[] = { { .test_id = "gatt_client", - .test_pre_init_f = test_init, - .test_tick_f = test_tick, .test_main_f = test_main }, BSTEST_END_MARKER diff --git a/tests/bsim/bluetooth/host/gatt/authorization/src/gatt_server_test.c b/tests/bsim/bluetooth/host/gatt/authorization/src/gatt_server_test.c index c8a3303a8d9b..4c206f7cd27d 100644 --- a/tests/bsim/bluetooth/host/gatt/authorization/src/gatt_server_test.c +++ b/tests/bsim/bluetooth/host/gatt/authorization/src/gatt_server_test.c @@ -4,11 +4,24 @@ * SPDX-License-Identifier: Apache-2.0 */ +#include +#include +#include +#include + +#include +#include +#include +#include +#include + +#include "babblekit/testcase.h" +#include "babblekit/flags.h" #include "common.h" extern enum bst_result_t bst_result; -CREATE_FLAG(flag_is_chrc_ctx_validated); +static DEFINE_FLAG(flag_is_chrc_ctx_validated); static struct bt_conn *g_conn; @@ -19,7 +32,7 @@ static void connected(struct bt_conn *conn, uint8_t err) bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr)); if (err != 0) { - FAIL("Failed to connect to %s (%u)\n", addr, err); + TEST_FAIL("Failed to connect to %s (%u)", addr, err); return; } @@ -87,7 +100,7 @@ static ssize_t write_test_chrc(struct test_chrc_ctx *chrc_ctx, } if (flags != 0) { - FAIL("Invalid flags %u\n", flags); + TEST_FAIL("Invalid flags %u", flags); return BT_GATT_ERR(BT_ATT_ERR_UNLIKELY); } @@ -240,14 +253,14 @@ static ssize_t write_cp_chrc(struct bt_conn *conn, pass = authorized_chrc_operation_validate(); log_str = "authorized"; } else { - FAIL("Invalid value of CP write counter %u\n", cp_write_cnt); + TEST_FAIL("Invalid value of CP write counter %u", cp_write_cnt); return BT_GATT_ERR(BT_ATT_ERR_UNLIKELY); } if (pass) { printk("Correct context for %s chrc\n", log_str); } else { - FAIL("Invalid context for %s chrc\n", log_str); + TEST_FAIL("Invalid context for %s chrc", log_str); return BT_GATT_ERR(BT_ATT_ERR_UNLIKELY); } @@ -327,7 +340,7 @@ static void test_main(void) err = bt_gatt_authorization_cb_register(&gatt_authorization_callbacks); if (err) { - FAIL("Registering GATT authorization callbacks failed (err %d)\n", err); + TEST_FAIL("Registering GATT authorization callbacks failed (err %d)", err); return; } @@ -335,7 +348,7 @@ static void test_main(void) err = bt_enable(NULL); if (err != 0) { - FAIL("Bluetooth init failed (err %d)\n", err); + TEST_FAIL("Bluetooth init failed (err %d)", err); return; } @@ -343,7 +356,7 @@ static void test_main(void) err = bt_le_adv_start(BT_LE_ADV_CONN_FAST_1, ad, ARRAY_SIZE(ad), NULL, 0); if (err != 0) { - FAIL("Advertising failed to start (err %d)\n", err); + TEST_FAIL("Advertising failed to start (err %d)", err); return; } @@ -351,14 +364,12 @@ static void test_main(void) WAIT_FOR_FLAG(flag_is_chrc_ctx_validated); - PASS("GATT server passed\n"); + TEST_PASS("GATT server passed"); } static const struct bst_test_instance test_gatt_server[] = { { .test_id = "gatt_server", - .test_pre_init_f = test_init, - .test_tick_f = test_tick, .test_main_f = test_main }, BSTEST_END_MARKER diff --git a/tests/bsim/bluetooth/host/gatt/authorization/test_scripts/gatt.sh b/tests/bsim/bluetooth/host/gatt/authorization/test_scripts/gatt.sh index 359247e08d98..c318928fbf8e 100755 --- a/tests/bsim/bluetooth/host/gatt/authorization/test_scripts/gatt.sh +++ b/tests/bsim/bluetooth/host/gatt/authorization/test_scripts/gatt.sh @@ -17,6 +17,6 @@ Execute ./bs_${BOARD_TS}_tests_bsim_bluetooth_host_gatt_authorization_prj_conf \ -v=${verbosity_level} -s=${simulation_id} -d=1 -testid=gatt_server Execute ./bs_2G4_phy_v1 -v=${verbosity_level} -s=${simulation_id} \ - -D=2 -sim_length=60e6 $@ + -D=2 -sim_length=30e6 $@ wait_for_background_jobs diff --git a/tests/bsim/bluetooth/host/gatt/caching/CMakeLists.txt b/tests/bsim/bluetooth/host/gatt/caching/CMakeLists.txt index 7869a21840fe..b279e8c14396 100644 --- a/tests/bsim/bluetooth/host/gatt/caching/CMakeLists.txt +++ b/tests/bsim/bluetooth/host/gatt/caching/CMakeLists.txt @@ -5,6 +5,9 @@ cmake_minimum_required(VERSION 3.20.0) find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) project(bsim_test_gatt) +add_subdirectory(${ZEPHYR_BASE}/tests/bsim/babblekit babblekit) +target_link_libraries(app PRIVATE babblekit) + FILE(GLOB app_sources src/*.c) target_sources(app PRIVATE ${app_sources} ) diff --git a/tests/bsim/bluetooth/host/gatt/caching/src/common.c b/tests/bsim/bluetooth/host/gatt/caching/src/common.c deleted file mode 100644 index a70839219052..000000000000 --- a/tests/bsim/bluetooth/host/gatt/caching/src/common.c +++ /dev/null @@ -1,66 +0,0 @@ -/* - * Copyright (c) 2022 Nordic Semiconductor ASA - * - * SPDX-License-Identifier: Apache-2.0 - */ - -#include "common.h" -#include "argparse.h" - -void test_tick(bs_time_t HW_device_time) -{ - if (bst_result != Passed) { - FAIL("test failed (not passed after %i seconds)\n", WAIT_TIME); - } -} - -void test_init(void) -{ - bst_ticker_set_next_tick_absolute(WAIT_TIME); - bst_result = In_progress; -} - -#define CHANNEL_ID 0 -#define MSG_SIZE 1 - -void backchannel_init(void) -{ - uint device_number = get_device_nbr(); - uint peer_number = device_number ^ 1; - uint device_numbers[] = { peer_number }; - uint channel_numbers[] = { CHANNEL_ID }; - uint *ch; - - ch = bs_open_back_channel(device_number, device_numbers, channel_numbers, - ARRAY_SIZE(channel_numbers)); - if (!ch) { - FAIL("Unable to open backchannel\n"); - } -} - -void backchannel_sync_send(void) -{ - uint8_t sync_msg[MSG_SIZE] = { get_device_nbr() }; - - printk("Sending sync\n"); - bs_bc_send_msg(CHANNEL_ID, sync_msg, ARRAY_SIZE(sync_msg)); -} - -void backchannel_sync_wait(void) -{ - uint8_t sync_msg[MSG_SIZE]; - - while (true) { - if (bs_bc_is_msg_received(CHANNEL_ID) > 0) { - bs_bc_receive_msg(CHANNEL_ID, sync_msg, ARRAY_SIZE(sync_msg)); - if (sync_msg[0] != get_device_nbr()) { - /* Received a message from another device, exit */ - break; - } - } - - k_sleep(K_MSEC(1)); - } - - printk("Sync received\n"); -} diff --git a/tests/bsim/bluetooth/host/gatt/caching/src/common.h b/tests/bsim/bluetooth/host/gatt/caching/src/common.h index d01282ca0354..adf5d4754a9e 100644 --- a/tests/bsim/bluetooth/host/gatt/caching/src/common.h +++ b/tests/bsim/bluetooth/host/gatt/caching/src/common.h @@ -6,50 +6,6 @@ * SPDX-License-Identifier: Apache-2.0 */ -#include - -#include "bs_types.h" -#include "bs_tracing.h" -#include "time_machine.h" -#include "bstests.h" -#include "bs_pc_backchannel.h" - -#include -#include -#include - -#include -#include -#include -#include -#include - -extern enum bst_result_t bst_result; - -#define WAIT_TIME (60 * 1e6) /*seconds*/ - -#define CREATE_FLAG(flag) static atomic_t flag = (atomic_t)false -#define SET_FLAG(flag) (void)atomic_set(&flag, (atomic_t)true) -#define UNSET_FLAG(flag) (void)atomic_set(&flag, (atomic_t)false) -#define WAIT_FOR_FLAG(flag) \ - while (!(bool)atomic_get(&flag)) { \ - (void)k_sleep(K_MSEC(1)); \ - } - -#define FAIL(...) \ - do { \ - bst_result = Failed; \ - bs_trace_error_time_line(__VA_ARGS__); \ - } while (0) - -#define PASS(...) \ - do { \ - bst_result = Passed; \ - bs_trace_info_time(1, __VA_ARGS__); \ - } while (0) - -#define CHRC_SIZE 10 - #define TEST_SERVICE_UUID \ BT_UUID_DECLARE_128(0x01, 0x23, 0x45, 0x67, 0x89, 0x01, 0x02, 0x03, \ 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x00, 0x00) @@ -61,9 +17,3 @@ extern enum bst_result_t bst_result; #define TEST_ADDITIONAL_CHRC_UUID \ BT_UUID_DECLARE_128(0x01, 0x23, 0x45, 0x67, 0x89, 0x01, 0x02, 0x03, \ 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0xFF, 0x11) - -void test_tick(bs_time_t HW_device_time); -void test_init(void); -void backchannel_init(void); -void backchannel_sync_send(void); -void backchannel_sync_wait(void); diff --git a/tests/bsim/bluetooth/host/gatt/caching/src/gatt_client_test.c b/tests/bsim/bluetooth/host/gatt/caching/src/gatt_client_test.c index 3699f80e09bf..65d9b0213c02 100644 --- a/tests/bsim/bluetooth/host/gatt/caching/src/gatt_client_test.c +++ b/tests/bsim/bluetooth/host/gatt/caching/src/gatt_client_test.c @@ -4,18 +4,29 @@ * SPDX-License-Identifier: Apache-2.0 */ +#include +#include +#include +#include + #include +#include +#include +#include #include +#include "babblekit/testcase.h" +#include "babblekit/flags.h" +#include "babblekit/sync.h" #include "common.h" -CREATE_FLAG(flag_is_connected); -CREATE_FLAG(flag_discover_complete); -CREATE_FLAG(flag_write_complete); -CREATE_FLAG(flag_chan_1_read); -CREATE_FLAG(flag_chan_2_read); -CREATE_FLAG(flag_db_hash_read); -CREATE_FLAG(flag_encrypted); +static DEFINE_FLAG(flag_is_connected); +static DEFINE_FLAG(flag_discover_complete); +static DEFINE_FLAG(flag_write_complete); +static DEFINE_FLAG(flag_chan_1_read); +static DEFINE_FLAG(flag_chan_2_read); +static DEFINE_FLAG(flag_db_hash_read); +static DEFINE_FLAG(flag_encrypted); static struct bt_conn *g_conn; static uint16_t chrc_handle; @@ -29,7 +40,7 @@ static void connected(struct bt_conn *conn, uint8_t err) bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr)); if (err != 0) { - FAIL("Failed to connect to %s (%u)\n", addr, err); + TEST_FAIL("Failed to connect to %s (%u)", addr, err); return; } @@ -60,9 +71,9 @@ static void disconnected(struct bt_conn *conn, uint8_t reason) void security_changed(struct bt_conn *conn, bt_security_t level, enum bt_security_err err) { if (err != BT_SECURITY_ERR_SUCCESS) { - FAIL("Encryption failed\n"); + TEST_FAIL("Encryption failed"); } else if (level < BT_SECURITY_L2) { - FAIL("Insufficient security\n"); + TEST_FAIL("Insufficient security"); } else { SET_FLAG(flag_encrypted); } @@ -94,14 +105,14 @@ void device_found(const bt_addr_le_t *addr, int8_t rssi, uint8_t type, struct ne printk("Stopping scan\n"); err = bt_le_scan_stop(); if (err != 0) { - FAIL("Could not stop scan (err %d)\n"); + TEST_FAIL("Could not stop scan (err %d)"); return; } err = bt_conn_le_create(addr, BT_CONN_LE_CREATE_CONN, BT_LE_CONN_PARAM_DEFAULT, &g_conn); if (err != 0) { - FAIL("Could not connect to peer (err %d)", err); + TEST_FAIL("Could not connect to peer (err %d)", err); } } @@ -112,7 +123,7 @@ static uint8_t discover_func(struct bt_conn *conn, const struct bt_gatt_attr *at if (attr == NULL) { if (chrc_handle == 0) { - FAIL("Did not discover chrc (%x)\n", chrc_handle); + TEST_FAIL("Did not discover chrc (%x)", chrc_handle); } (void)memset(params, 0, sizeof(*params)); @@ -133,7 +144,7 @@ static uint8_t discover_func(struct bt_conn *conn, const struct bt_gatt_attr *at err = bt_gatt_discover(conn, params); if (err != 0) { - FAIL("Discover failed (err %d)\n", err); + TEST_FAIL("Discover failed (err %d)", err); } return BT_GATT_ITER_STOP; @@ -171,7 +182,7 @@ static void gatt_discover(const struct bt_uuid *uuid, uint8_t type) err = bt_gatt_discover(g_conn, &discover_params); if (err != 0) { - FAIL("Discover failed(err %d)\n", err); + TEST_FAIL("Discover failed (err %d)", err); } WAIT_FOR_FLAG(flag_discover_complete); @@ -203,10 +214,10 @@ static struct bt_gatt_read_params db_hash_read = { .chan_opt = BT_ATT_CHAN_OPT_NONE, }; -void expect_status(uint8_t err, uint8_t status) +static void expect_status(uint8_t err, uint8_t status) { if (err != status) { - FAIL("Unexpected status from read: 0x%02X, expected 0x%02X\n", err, status); + TEST_FAIL("Unexpected status from read: 0x%02X, expected 0x%02X", err, status); } } @@ -224,7 +235,7 @@ static uint8_t gatt_read_expect_success_cb(struct bt_conn *conn, uint8_t err, } else if (params == &chan_2_read) { SET_FLAG(flag_chan_2_read); } else { - FAIL("Unexpected params\n"); + TEST_FAIL("Unexpected params"); } return 0; @@ -242,7 +253,7 @@ static uint8_t gatt_read_expect_err_unlikely_cb(struct bt_conn *conn, uint8_t er } else if (params == &chan_2_read) { SET_FLAG(flag_chan_2_read); } else { - FAIL("Unexpected params\n"); + TEST_FAIL("Unexpected params"); } return 0; @@ -260,7 +271,7 @@ static uint8_t gatt_read_expect_err_out_of_sync_cb(struct bt_conn *conn, uint8_t } else if (params == &chan_2_read) { SET_FLAG(flag_chan_2_read); } else { - FAIL("Unexpected params\n"); + TEST_FAIL("Unexpected params"); } return 0; @@ -274,14 +285,14 @@ static void gatt_read(struct bt_gatt_read_params *read_params) err = bt_gatt_read(g_conn, read_params); if (err != 0) { - FAIL("bt_gatt_read failed: %d\n", err); + TEST_FAIL("bt_gatt_read failed: %d", err); } } static void write_cb(struct bt_conn *conn, uint8_t err, struct bt_gatt_write_params *params) { if (err != BT_ATT_ERR_SUCCESS) { - FAIL("Write failed: 0x%02X\n", err); + TEST_FAIL("Write failed: 0x%02X", err); } SET_FLAG(flag_write_complete); @@ -310,7 +321,7 @@ static void enable_robust_caching(void) err = bt_gatt_write(g_conn, &write_params); if (err) { - FAIL("bt_gatt_write failed (err %d)\n", err); + TEST_FAIL("bt_gatt_write failed (err %d)", err); } WAIT_FOR_FLAG(flag_write_complete); @@ -321,16 +332,16 @@ static void test_main_common(bool connect_eatt) { int err; - backchannel_init(); + TEST_ASSERT(bk_sync_init() == 0, "Failed to open backchannel"); err = bt_enable(NULL); if (err != 0) { - FAIL("Bluetooth discover failed (err %d)\n", err); + TEST_FAIL("Bluetooth discover failed (err %d)", err); } err = bt_le_scan_start(BT_LE_SCAN_PASSIVE, device_found); if (err != 0) { - FAIL("Scanning failed to start (err %d)\n", err); + TEST_FAIL("Scanning failed to start (err %d)", err); } printk("Scanning successfully started\n"); @@ -339,7 +350,7 @@ static void test_main_common(bool connect_eatt) err = bt_conn_set_security(g_conn, BT_SECURITY_L2); if (err) { - FAIL("Failed to start encryption procedure\n"); + TEST_FAIL("Failed to start encryption procedure"); } WAIT_FOR_FLAG(flag_encrypted); @@ -357,10 +368,10 @@ static void test_main_common(bool connect_eatt) } /* Tell the server to register additional service */ - backchannel_sync_send(); + bk_sync_send(); /* Wait for new service to be added by server */ - backchannel_sync_wait(); + bk_sync_wait(); chan_1_read.single.handle = chrc_handle; chan_2_read.single.handle = chrc_handle; @@ -386,9 +397,9 @@ static void test_main_db_hash_read_eatt(void) WAIT_FOR_FLAG(flag_chan_2_read); /* Signal to server that reads are done */ - backchannel_sync_send(); + bk_sync_send(); - PASS("GATT client Passed\n"); + TEST_PASS("GATT client Passed"); } static void test_main_out_of_sync_eatt(void) @@ -423,9 +434,9 @@ static void test_main_out_of_sync_eatt(void) WAIT_FOR_FLAG(flag_chan_2_read); /* Signal to server that reads are done */ - backchannel_sync_send(); + bk_sync_send(); - PASS("GATT client Passed\n"); + TEST_PASS("GATT client Passed"); } static void test_main_retry_reads_eatt(void) @@ -455,9 +466,9 @@ static void test_main_retry_reads_eatt(void) WAIT_FOR_FLAG(flag_chan_2_read); /* Signal to server that reads are done */ - backchannel_sync_send(); + bk_sync_send(); - PASS("GATT client Passed\n"); + TEST_PASS("GATT client Passed"); } static void test_main_db_hash_read_no_eatt(void) @@ -476,9 +487,9 @@ static void test_main_db_hash_read_no_eatt(void) WAIT_FOR_FLAG(flag_chan_1_read); /* Signal to server that reads are done */ - backchannel_sync_send(); + bk_sync_send(); - PASS("GATT client Passed\n"); + TEST_PASS("GATT client Passed"); } static void test_main_out_of_sync_no_eatt(void) @@ -501,9 +512,9 @@ static void test_main_out_of_sync_no_eatt(void) WAIT_FOR_FLAG(flag_chan_1_read); /* Signal to server that reads are done */ - backchannel_sync_send(); + bk_sync_send(); - PASS("GATT client Passed\n"); + TEST_PASS("GATT client Passed"); } static void test_main_retry_reads_no_eatt(void) @@ -521,46 +532,34 @@ static void test_main_retry_reads_no_eatt(void) WAIT_FOR_FLAG(flag_chan_1_read); /* Signal to server that reads are done */ - backchannel_sync_send(); + bk_sync_send(); - PASS("GATT client Passed\n"); + TEST_PASS("GATT client Passed"); } static const struct bst_test_instance test_vcs[] = { { .test_id = "gatt_client_db_hash_read_eatt", - .test_pre_init_f = test_init, - .test_tick_f = test_tick, .test_main_f = test_main_db_hash_read_eatt, }, { .test_id = "gatt_client_out_of_sync_eatt", - .test_pre_init_f = test_init, - .test_tick_f = test_tick, .test_main_f = test_main_out_of_sync_eatt, }, { .test_id = "gatt_client_retry_reads_eatt", - .test_pre_init_f = test_init, - .test_tick_f = test_tick, .test_main_f = test_main_retry_reads_eatt, }, { .test_id = "gatt_client_db_hash_read_no_eatt", - .test_pre_init_f = test_init, - .test_tick_f = test_tick, .test_main_f = test_main_db_hash_read_no_eatt, }, { .test_id = "gatt_client_out_of_sync_no_eatt", - .test_pre_init_f = test_init, - .test_tick_f = test_tick, .test_main_f = test_main_out_of_sync_no_eatt, }, { .test_id = "gatt_client_retry_reads_no_eatt", - .test_pre_init_f = test_init, - .test_tick_f = test_tick, .test_main_f = test_main_retry_reads_no_eatt, }, BSTEST_END_MARKER, diff --git a/tests/bsim/bluetooth/host/gatt/caching/src/gatt_server_test.c b/tests/bsim/bluetooth/host/gatt/caching/src/gatt_server_test.c index 637b7037e2fc..1cb88b489fb8 100644 --- a/tests/bsim/bluetooth/host/gatt/caching/src/gatt_server_test.c +++ b/tests/bsim/bluetooth/host/gatt/caching/src/gatt_server_test.c @@ -4,12 +4,26 @@ * SPDX-License-Identifier: Apache-2.0 */ +#include +#include +#include +#include + +#include +#include +#include +#include +#include + +#include "babblekit/testcase.h" +#include "babblekit/flags.h" +#include "babblekit/sync.h" #include "common.h" extern enum bst_result_t bst_result; -CREATE_FLAG(flag_is_connected); -CREATE_FLAG(flag_is_encrypted); +static DEFINE_FLAG(flag_is_connected); +static DEFINE_FLAG(flag_is_encrypted); static struct bt_conn *g_conn; @@ -20,7 +34,7 @@ static void connected(struct bt_conn *conn, uint8_t err) bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr)); if (err != 0) { - FAIL("Failed to connect to %s (%u)\n", addr, err); + TEST_FAIL("Failed to connect to %s (%u)", addr, err); return; } @@ -63,6 +77,7 @@ BT_CONN_CB_DEFINE(conn_callbacks) = { .security_changed = security_changed, }; +#define CHRC_SIZE 10 #define ARRAY_ITEM(i, _) i static const uint8_t chrc_data[] = { LISTIFY(CHRC_SIZE, ARRAY_ITEM, (,)) }; /* 1, 2, 3 ... */ @@ -90,11 +105,11 @@ static void test_main_common(bool connect_eatt) const struct bt_data ad[] = { BT_DATA_BYTES(BT_DATA_FLAGS, (BT_LE_AD_GENERAL | BT_LE_AD_NO_BREDR)) }; - backchannel_init(); + TEST_ASSERT(bk_sync_init() == 0, "Failed to open backchannel"); err = bt_enable(NULL); if (err != 0) { - FAIL("Bluetooth init failed (err %d)\n", err); + TEST_FAIL("Bluetooth init failed (err %d)", err); return; } @@ -103,7 +118,7 @@ static void test_main_common(bool connect_eatt) err = bt_le_adv_start(BT_LE_ADV_CONN_FAST_1, ad, ARRAY_SIZE(ad), NULL, 0); if (err != 0) { - FAIL("Advertising failed to start (err %d)\n", err); + TEST_FAIL("Advertising failed to start (err %d)", err); return; } @@ -117,28 +132,28 @@ static void test_main_common(bool connect_eatt) err = bt_eatt_connect(g_conn, CONFIG_BT_EATT_MAX); if (err) { - FAIL("Failed to connect EATT channels (err %d)\n", err); + TEST_FAIL("Failed to connect EATT channels (err %d)", err); return; } } /* Wait for client to do discovery and configuration */ - backchannel_sync_wait(); + bk_sync_wait(); printk("Registering additional service\n"); err = bt_gatt_service_register(&additional_gatt_service); if (err < 0) { - FAIL("Registering additional service failed (err %d)\n", err); + TEST_FAIL("Registering additional service failed (err %d)", err); } /* Signal to client that additional service is registered */ - backchannel_sync_send(); + bk_sync_send(); /* Wait for client to be done reading */ - backchannel_sync_wait(); + bk_sync_wait(); - PASS("GATT server passed\n"); + TEST_PASS("GATT server passed"); } static void test_main_eatt(void) @@ -154,14 +169,10 @@ static void test_main_no_eatt(void) static const struct bst_test_instance test_gatt_server[] = { { .test_id = "gatt_server_eatt", - .test_pre_init_f = test_init, - .test_tick_f = test_tick, .test_main_f = test_main_eatt, }, { .test_id = "gatt_server_no_eatt", - .test_pre_init_f = test_init, - .test_tick_f = test_tick, .test_main_f = test_main_no_eatt, }, BSTEST_END_MARKER, diff --git a/tests/bsim/bluetooth/host/gatt/ccc_store/CMakeLists.txt b/tests/bsim/bluetooth/host/gatt/ccc_store/CMakeLists.txt index 832d770fbd97..35f8186b3ecb 100644 --- a/tests/bsim/bluetooth/host/gatt/ccc_store/CMakeLists.txt +++ b/tests/bsim/bluetooth/host/gatt/ccc_store/CMakeLists.txt @@ -12,9 +12,11 @@ endif() find_package(Zephyr HINTS $ENV{ZEPHYR_BASE}) project(bsim_test_ccc_store) +add_subdirectory(${ZEPHYR_BASE}/tests/bsim/babblekit babblekit) +target_link_libraries(app PRIVATE babblekit) + target_sources(app PRIVATE src/main.c - src/common.c src/central.c src/peripheral.c diff --git a/tests/bsim/bluetooth/host/gatt/ccc_store/src/central.c b/tests/bsim/bluetooth/host/gatt/ccc_store/src/central.c index 0784a5e587df..a4f056dd4083 100644 --- a/tests/bsim/bluetooth/host/gatt/ccc_store/src/central.c +++ b/tests/bsim/bluetooth/host/gatt/ccc_store/src/central.c @@ -22,14 +22,17 @@ #include "settings.h" #include "argparse.h" -#include "bs_pc_backchannel.h" + +#include "babblekit/testcase.h" +#include "babblekit/flags.h" +#include "babblekit/sync.h" #define SERVER_CHAN 0 -CREATE_FLAG(connected_flag); -CREATE_FLAG(disconnected_flag); -CREATE_FLAG(security_updated_flag); -CREATE_FLAG(notification_received_flag); +static DEFINE_FLAG(connected_flag); +static DEFINE_FLAG(disconnected_flag); +static DEFINE_FLAG(security_updated_flag); +static DEFINE_FLAG(notification_received_flag); #define BT_UUID_DUMMY_SERVICE BT_UUID_DECLARE_128(DUMMY_SERVICE_TYPE) #define BT_UUID_DUMMY_SERVICE_NOTIFY BT_UUID_DECLARE_128(DUMMY_SERVICE_NOTIFY_TYPE) @@ -38,7 +41,7 @@ static struct bt_conn *default_conn; static struct bt_conn_cb central_cb; -CREATE_FLAG(gatt_subscribed_flag); +static DEFINE_FLAG(gatt_subscribed_flag); static uint8_t notify_cb(struct bt_conn *conn, struct bt_gatt_subscribe_params *params, const void *data, uint16_t length) @@ -88,7 +91,7 @@ static void ccc_subscribe(void) err = bt_gatt_subscribe(default_conn, &subscribe_params); if (err) { - FAIL("Failed to subscribe (att err %d)", err); + TEST_FAIL("Failed to subscribe (att err %d)", err); } WAIT_FOR_FLAG(gatt_subscribed_flag); @@ -106,13 +109,13 @@ static void device_found(const bt_addr_le_t *addr, int8_t rssi, uint8_t type, err = bt_le_scan_stop(); if (err) { - FAIL("Failed to stop scanner (err %d)\n", err); + TEST_FAIL("Failed to stop scanner (err %d)", err); } err = bt_conn_le_create(addr, BT_CONN_LE_CREATE_CONN, BT_LE_CONN_PARAM_DEFAULT, &default_conn); if (err) { - FAIL("Could not connect to peer: %s (err %d)\n", addr_str, err); + TEST_FAIL("Could not connect to peer: %s (err %d)", addr_str, err); } } @@ -125,7 +128,7 @@ static void connected(struct bt_conn *conn, uint8_t err) bt_addr_le_to_str(addr, addr_str, sizeof(addr_str)); if (err) { - FAIL("Failed to connect to %s (err %d)\n", addr_str, err); + TEST_FAIL("Failed to connect to %s (err %d)", addr_str, err); } LOG_DBG("Connected: %s", addr_str); @@ -173,7 +176,7 @@ static void start_scan(void) err = bt_le_scan_start(BT_LE_SCAN_PASSIVE, device_found); if (err) { - FAIL("Scanning failed to start (err %d)\n", err); + TEST_FAIL("Scanning failed to start (err %d)", err); } LOG_DBG("Scanning successfully started"); @@ -185,7 +188,7 @@ static void disconnect(void) err = bt_conn_disconnect(default_conn, BT_HCI_ERR_REMOTE_USER_TERM_CONN); if (err) { - FAIL("Disconnection failed (err %d)\n", err); + TEST_FAIL("Disconnection failed (err %d)", err); } WAIT_FOR_FLAG(disconnected_flag); @@ -205,7 +208,7 @@ static void connect_pair_subscribe(void) err = bt_conn_set_security(default_conn, BT_SECURITY_L2); if (err != 0) { - FAIL("Failed to set security (err %d)\n", err); + TEST_FAIL("Failed to set security (err %d)", err); } WAIT_FOR_FLAG(security_updated_flag); @@ -215,9 +218,9 @@ static void connect_pair_subscribe(void) ccc_subscribe(); /* confirm to server that we subscribed */ - backchannel_sync_send(SERVER_CHAN, SERVER_ID); + bk_sync_send(); /* wait for server to check that the subscribtion is well registered */ - backchannel_sync_wait(SERVER_CHAN, SERVER_ID); + bk_sync_wait(); WAIT_FOR_FLAG(notification_received_flag); UNSET_FLAG(notification_received_flag); @@ -234,21 +237,21 @@ static void connect_restore_sec(void) err = bt_conn_set_security(default_conn, BT_SECURITY_L2); if (err != 0) { - FAIL("Failed to set security (err %d)\n", err); + TEST_FAIL("Failed to set security (err %d)", err); } WAIT_FOR_FLAG(security_updated_flag); UNSET_FLAG(security_updated_flag); /* check local subscription state */ - if (GET_FLAG(gatt_subscribed_flag) == false) { - FAIL("Not subscribed\n"); + if (!IS_FLAG_SET(gatt_subscribed_flag)) { + TEST_FAIL("Not subscribed"); } /* notify the end of security update to server */ - backchannel_sync_send(SERVER_CHAN, SERVER_ID); + bk_sync_send(); /* wait for server to check that the subscribtion has been restored */ - backchannel_sync_wait(SERVER_CHAN, SERVER_ID); + bk_sync_wait(); WAIT_FOR_FLAG(notification_received_flag); UNSET_FLAG(notification_received_flag); @@ -256,26 +259,6 @@ static void connect_restore_sec(void) /* Util functions */ -void central_backchannel_init(void) -{ - uint device_number = get_device_nbr(); - uint channel_numbers[1] = { - 0, - }; - uint device_numbers[1]; - uint num_ch = 1; - uint *ch; - - device_numbers[0] = SERVER_ID; - - LOG_DBG("Opening back channels for device %d", device_number); - ch = bs_open_back_channel(device_number, device_numbers, channel_numbers, num_ch); - if (!ch) { - FAIL("Unable to open backchannel\n"); - } - LOG_DBG("Back channels for device %d opened", device_number); -} - static void set_public_addr(void) { bt_addr_le_t addr = {BT_ADDR_LE_RANDOM, {{0x0A, 0x89, 0x67, 0x45, 0x23, 0xC1}}}; @@ -293,12 +276,12 @@ void run_central(int times) central_cb.disconnected = disconnected; central_cb.security_changed = security_changed; - central_backchannel_init(); + TEST_ASSERT(bk_sync_init() == 0); set_public_addr(); err = bt_enable(NULL); if (err) { - FAIL("Bluetooth init failed (err %d)\n", err); + TEST_FAIL("Bluetooth init failed (err %d)", err); } LOG_DBG("Bluetooth initialized"); @@ -307,12 +290,12 @@ void run_central(int times) err = settings_load(); if (err) { - FAIL("Settings load failed (err %d)\n", err); + TEST_FAIL("Settings load failed (err %d)", err); } err = bt_unpair(BT_ID_DEFAULT, BT_ADDR_LE_ANY); if (err) { - FAIL("Unpairing failed (err %d)\n", err); + TEST_FAIL("Unpairing failed (err %d)", err); } connect_pair_subscribe(); @@ -323,5 +306,5 @@ void run_central(int times) disconnect(); } - PASS("Central test passed\n"); + TEST_PASS("Central test passed"); } diff --git a/tests/bsim/bluetooth/host/gatt/ccc_store/src/common.c b/tests/bsim/bluetooth/host/gatt/ccc_store/src/common.c deleted file mode 100644 index 7c9c898bc551..000000000000 --- a/tests/bsim/bluetooth/host/gatt/ccc_store/src/common.c +++ /dev/null @@ -1,38 +0,0 @@ -/* Copyright (c) 2023 Nordic Semiconductor ASA - * SPDX-License-Identifier: Apache-2.0 - */ - -#include - -#include "common.h" - -#include "argparse.h" -#include "bs_pc_backchannel.h" - -void backchannel_sync_send(uint channel, uint device_nbr) -{ - uint8_t sync_msg[BC_MSG_SIZE] = {get_device_nbr(), device_nbr}; /* src, dst */ - - bs_bc_send_msg(channel, sync_msg, ARRAY_SIZE(sync_msg)); -} - -void backchannel_sync_wait(uint channel, uint device_nbr) -{ - uint8_t sync_msg[BC_MSG_SIZE]; - - LOG_DBG("Wait for %d on channel %d", device_nbr, channel); - - while (true) { - if (bs_bc_is_msg_received(channel) > 0) { - bs_bc_receive_msg(channel, sync_msg, ARRAY_SIZE(sync_msg)); - - if (sync_msg[0] == device_nbr && sync_msg[1] == get_device_nbr()) { - break; - } - } - - k_msleep(1); - } - - LOG_DBG("Sync received"); -} diff --git a/tests/bsim/bluetooth/host/gatt/ccc_store/src/common.h b/tests/bsim/bluetooth/host/gatt/ccc_store/src/common.h index c4c8f9c4ff0c..8f3dcfb29f27 100644 --- a/tests/bsim/bluetooth/host/gatt/ccc_store/src/common.h +++ b/tests/bsim/bluetooth/host/gatt/ccc_store/src/common.h @@ -2,39 +2,8 @@ * SPDX-License-Identifier: Apache-2.0 */ -#include - -#include -#include - #include -#include "bs_tracing.h" -#include "bstests.h" - -#define FAIL(...) \ - do { \ - bst_result = Failed; \ - bs_trace_error_time_line(__VA_ARGS__); \ - } while (0) - -#define PASS(...) \ - do { \ - bst_result = Passed; \ - bs_trace_info_time(1, __VA_ARGS__); \ - } while (0) - -extern enum bst_result_t bst_result; - -#define CREATE_FLAG(flag) static atomic_t flag = (atomic_t) false -#define SET_FLAG(flag) (void)atomic_set(&flag, (atomic_t) true) -#define GET_FLAG(flag) (bool)atomic_get(&flag) -#define UNSET_FLAG(flag) (void)atomic_set(&flag, (atomic_t) false) -#define WAIT_FOR_FLAG(flag) \ - while (!(bool)atomic_get(&flag)) { \ - (void)k_sleep(K_MSEC(1)); \ - } - LOG_MODULE_DECLARE(bt_bsim_ccc_store, LOG_LEVEL_DBG); #define DUMMY_SERVICE_TYPE BT_UUID_128_ENCODE(0x2e2b8dc3, 0x06e0, 0x4f93, 0x9bb2, 0x734091c356f0) @@ -46,11 +15,3 @@ LOG_MODULE_DECLARE(bt_bsim_ccc_store, LOG_LEVEL_DBG); #define VAL_HANDLE 18 #define CCC_HANDLE 19 - -#define BC_MSG_SIZE 2 - -#define CLIENT_ID 0 -#define SERVER_ID 1 - -void backchannel_sync_send(uint channel, uint device_nbr); -void backchannel_sync_wait(uint channel, uint device_nbr); diff --git a/tests/bsim/bluetooth/host/gatt/ccc_store/src/main.c b/tests/bsim/bluetooth/host/gatt/ccc_store/src/main.c index fb47ef4c0680..d37c1314e26e 100644 --- a/tests/bsim/bluetooth/host/gatt/ccc_store/src/main.c +++ b/tests/bsim/bluetooth/host/gatt/ccc_store/src/main.c @@ -4,32 +4,13 @@ #include -#include "bs_types.h" -#include "bs_tracing.h" #include "bstests.h" -#include +#include #include LOG_MODULE_REGISTER(bt_bsim_ccc_store, LOG_LEVEL_DBG); -#define FAIL(...) \ - do { \ - bst_result = Failed; \ - bs_trace_error_time_line(__VA_ARGS__); \ - } while (0) - -#define PASS(...) \ - do { \ - bst_result = Passed; \ - bs_trace_info_time(1, __VA_ARGS__); \ - } while (0) - -extern enum bst_result_t bst_result; - -#define WAIT_TIME_S 60 -#define WAIT_TIME (WAIT_TIME_S * 1e6) - static int n_times; extern void run_peripheral(int times); @@ -45,41 +26,23 @@ static void peripheral_main(void) run_peripheral(n_times); } -void test_tick(bs_time_t HW_device_time) -{ - if (bst_result != Passed) { - bst_result = Failed; - bs_trace_error_time_line("Test failed (not passed after %d seconds)\n", - WAIT_TIME_S); - } -} - static void test_args(int argc, char **argv) { __ASSERT(argc == 1, "Please specify only 1 test argument\n"); - n_times = atol(argv[0]); -} - -static void test_ccc_store_init(void) -{ - bst_ticker_set_next_tick_absolute(WAIT_TIME); + n_times = strtol(argv[0], NULL, 10); } static const struct bst_test_instance test_def[] = { { .test_id = "central", .test_descr = "Central device", - .test_pre_init_f = test_ccc_store_init, - .test_tick_f = test_tick, .test_main_f = central_main, .test_args_f = test_args, }, { .test_id = "peripheral", .test_descr = "Peripheral device", - .test_pre_init_f = test_ccc_store_init, - .test_tick_f = test_tick, .test_main_f = peripheral_main, .test_args_f = test_args, }, diff --git a/tests/bsim/bluetooth/host/gatt/ccc_store/src/peripheral.c b/tests/bsim/bluetooth/host/gatt/ccc_store/src/peripheral.c index 873685ecd4fa..6205306306b6 100644 --- a/tests/bsim/bluetooth/host/gatt/ccc_store/src/peripheral.c +++ b/tests/bsim/bluetooth/host/gatt/ccc_store/src/peripheral.c @@ -20,15 +20,17 @@ #include "settings.h" #include "argparse.h" -#include "bs_pc_backchannel.h" +#include "babblekit/testcase.h" +#include "babblekit/flags.h" +#include "babblekit/sync.h" #define CLIENT_CHAN 0 -CREATE_FLAG(connected_flag); -CREATE_FLAG(disconnected_flag); -CREATE_FLAG(security_updated_flag); +static DEFINE_FLAG(connected_flag); +static DEFINE_FLAG(disconnected_flag); +static DEFINE_FLAG(security_updated_flag); -CREATE_FLAG(ccc_cfg_changed_flag); +static DEFINE_FLAG(ccc_cfg_changed_flag); static const struct bt_uuid_128 dummy_service = BT_UUID_INIT_128(DUMMY_SERVICE_TYPE); @@ -61,7 +63,7 @@ static void create_adv(struct bt_le_ext_adv **adv) err = bt_le_ext_adv_create(BT_LE_ADV_CONN_FAST_1, NULL, adv); if (err) { - FAIL("Failed to create advertiser (%d)\n", err); + TEST_FAIL("Failed to create advertiser (%d)", err); } } @@ -78,7 +80,7 @@ static void start_adv(struct bt_le_ext_adv *adv) err = bt_le_ext_adv_start(adv, &start_params); if (err) { - FAIL("Failed to start advertiser (err %d)\n", err); + TEST_FAIL("Failed to start advertiser (err %d)", err); } LOG_DBG("Advertiser started"); @@ -90,7 +92,7 @@ static void stop_adv(struct bt_le_ext_adv *adv) err = bt_le_ext_adv_stop(adv); if (err) { - FAIL("Failed to stop advertiser (err %d)\n", err); + TEST_FAIL("Failed to stop advertiser (err %d)", err); } } @@ -101,7 +103,7 @@ static void connected(struct bt_conn *conn, uint8_t err) bt_addr_le_to_str(bt_conn_get_dst(conn), addr_str, sizeof(addr_str)); if (err) { - FAIL("Failed to connect to %s (err %d)\n", addr_str, err); + TEST_FAIL("Failed to connect to %s (err %d)", addr_str, err); } LOG_DBG("Connected: %s", addr_str); @@ -160,7 +162,7 @@ static void send_value_notification(void) err = bt_gatt_notify(default_conn, attr, &value, sizeof(value)); if (err != 0) { - FAIL("Failed to send notification (err %d)\n", err); + TEST_FAIL("Failed to send notification (err %d)", err); } value++; @@ -176,17 +178,17 @@ static void connect_pair_check_subscribtion(struct bt_le_ext_adv *adv) UNSET_FLAG(security_updated_flag); /* wait for confirmation of subscribtion from good client */ - backchannel_sync_wait(CLIENT_CHAN, CLIENT_ID); + bk_sync_wait(); /* check that subscribtion request did not fail */ if (!is_peer_subscribed(default_conn)) { - FAIL("Client did not subscribed\n"); + TEST_FAIL("Client did not subscribed"); } stop_adv(adv); /* confirm to client that the subscribtion has been well registered */ - backchannel_sync_send(CLIENT_CHAN, CLIENT_ID); + bk_sync_send(); send_value_notification(); } @@ -201,42 +203,23 @@ static void connect_restore_sec_check_subscribtion(struct bt_le_ext_adv *adv) UNSET_FLAG(security_updated_flag); /* wait for client end of security update */ - backchannel_sync_wait(CLIENT_CHAN, CLIENT_ID); + bk_sync_wait(); /* check that subscribtion has been restored */ if (!is_peer_subscribed(default_conn)) { - FAIL("Client is not subscribed\n"); + TEST_FAIL("Client is not subscribed"); } else { LOG_DBG("Client is subscribed"); } /* confirm to good client that the subscribtion has been well restored */ - backchannel_sync_send(CLIENT_CHAN, CLIENT_ID); + bk_sync_send(); send_value_notification(); } /* Util functions */ -void peripheral_backchannel_init(void) -{ - uint device_number = get_device_nbr(); - uint channel_numbers[1] = { - 0, - }; - uint device_numbers[1] = { - CLIENT_ID, - }; - uint num_ch = 1; - uint *ch; - - LOG_DBG("Opening back channels for device %d", device_number); - ch = bs_open_back_channel(device_number, device_numbers, channel_numbers, num_ch); - if (!ch) { - FAIL("Unable to open backchannel\n"); - } -} - static void check_ccc_handle(void) { struct bt_gatt_attr *service_notify_attr = @@ -269,11 +252,11 @@ void run_peripheral(int times) peripheral_cb.disconnected = disconnected; peripheral_cb.security_changed = security_changed; - peripheral_backchannel_init(); + TEST_ASSERT(bk_sync_init() == 0); err = bt_enable(NULL); if (err) { - FAIL("Bluetooth init failed (err %d)\n", err); + TEST_FAIL("Bluetooth init failed (err %d)", err); } LOG_DBG("Bluetooth initialized"); @@ -284,12 +267,12 @@ void run_peripheral(int times) err = settings_load(); if (err) { - FAIL("Settings load failed (err %d)\n", err); + TEST_FAIL("Settings load failed (err %d)", err); } err = bt_unpair(BT_ID_DEFAULT, BT_ADDR_LE_ANY); if (err) { - FAIL("Unpairing failed (err %d)\n", err); + TEST_FAIL("Unpairing failed (err %d)", err); } create_adv(&adv); @@ -302,5 +285,5 @@ void run_peripheral(int times) WAIT_FOR_FLAG(disconnected_flag); } - PASS("Peripheral test passed\n"); + TEST_PASS("Peripheral test passed"); } diff --git a/tests/bsim/bluetooth/host/gatt/general/CMakeLists.txt b/tests/bsim/bluetooth/host/gatt/general/CMakeLists.txt index 7869a21840fe..b279e8c14396 100644 --- a/tests/bsim/bluetooth/host/gatt/general/CMakeLists.txt +++ b/tests/bsim/bluetooth/host/gatt/general/CMakeLists.txt @@ -5,6 +5,9 @@ cmake_minimum_required(VERSION 3.20.0) find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) project(bsim_test_gatt) +add_subdirectory(${ZEPHYR_BASE}/tests/bsim/babblekit babblekit) +target_link_libraries(app PRIVATE babblekit) + FILE(GLOB app_sources src/*.c) target_sources(app PRIVATE ${app_sources} ) diff --git a/tests/bsim/bluetooth/host/gatt/general/src/common.c b/tests/bsim/bluetooth/host/gatt/general/src/common.c deleted file mode 100644 index df438607c5fa..000000000000 --- a/tests/bsim/bluetooth/host/gatt/general/src/common.c +++ /dev/null @@ -1,20 +0,0 @@ -/* - * Copyright (c) 2022 Nordic Semiconductor ASA - * - * SPDX-License-Identifier: Apache-2.0 - */ - -#include "common.h" - -void test_tick(bs_time_t HW_device_time) -{ - if (bst_result != Passed) { - FAIL("test failed (not passed after %i seconds)\n", WAIT_TIME); - } -} - -void test_init(void) -{ - bst_ticker_set_next_tick_absolute(WAIT_TIME); - bst_result = In_progress; -} diff --git a/tests/bsim/bluetooth/host/gatt/general/src/common.h b/tests/bsim/bluetooth/host/gatt/general/src/common.h index b37e7aec46cb..eb8fa59eb639 100644 --- a/tests/bsim/bluetooth/host/gatt/general/src/common.h +++ b/tests/bsim/bluetooth/host/gatt/general/src/common.h @@ -6,47 +6,6 @@ * SPDX-License-Identifier: Apache-2.0 */ -#include - -#include "bs_types.h" -#include "bs_tracing.h" -#include "time_machine.h" -#include "bstests.h" - -#include -#include -#include - -#include -#include -#include -#include -#include - -extern enum bst_result_t bst_result; - -#define WAIT_TIME (30 * 1e6) /*seconds*/ - -#define CREATE_FLAG(flag) static atomic_t flag = (atomic_t)false -#define SET_FLAG(flag) (void)atomic_set(&flag, (atomic_t)true) -#define UNSET_FLAG(flag) (void)atomic_set(&flag, (atomic_t)false) -#define WAIT_FOR_FLAG(flag) \ - while (!(bool)atomic_get(&flag)) { \ - (void)k_sleep(K_MSEC(1)); \ - } - -#define FAIL(...) \ - do { \ - bst_result = Failed; \ - bs_trace_error_time_line(__VA_ARGS__); \ - } while (0) - -#define PASS(...) \ - do { \ - bst_result = Passed; \ - bs_trace_info_time(1, __VA_ARGS__); \ - } while (0) - #define CHRC_SIZE 10 #define LONG_CHRC_SIZE 40 @@ -69,6 +28,3 @@ extern enum bst_result_t bst_result; #define TEST_LESC_CHRC_UUID \ BT_UUID_DECLARE_128(0x01, 0x23, 0x45, 0x67, 0x89, 0x01, 0x02, 0x03, \ 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0xFF, 0x33) - -void test_tick(bs_time_t HW_device_time); -void test_init(void); diff --git a/tests/bsim/bluetooth/host/gatt/general/src/gatt_client_test.c b/tests/bsim/bluetooth/host/gatt/general/src/gatt_client_test.c index a0fe2704f8e0..b78b5d2a619a 100644 --- a/tests/bsim/bluetooth/host/gatt/general/src/gatt_client_test.c +++ b/tests/bsim/bluetooth/host/gatt/general/src/gatt_client_test.c @@ -4,16 +4,27 @@ * SPDX-License-Identifier: Apache-2.0 */ +#include + +#include +#include +#include + #include +#include +#include +#include #include +#include "babblekit/testcase.h" +#include "babblekit/flags.h" #include "common.h" -CREATE_FLAG(flag_is_connected); -CREATE_FLAG(flag_discover_complete); -CREATE_FLAG(flag_security_changed); -CREATE_FLAG(flag_write_complete); -CREATE_FLAG(flag_read_complete); +static DEFINE_FLAG(flag_is_connected); +static DEFINE_FLAG(flag_discover_complete); +static DEFINE_FLAG(flag_security_changed); +static DEFINE_FLAG(flag_write_complete); +static DEFINE_FLAG(flag_read_complete); static struct bt_conn *g_conn; static uint16_t chrc_handle; @@ -36,7 +47,7 @@ static void connected(struct bt_conn *conn, uint8_t err) bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr)); if (err != 0) { - FAIL("Failed to connect to %s (%u)\n", addr, err); + TEST_FAIL("Failed to connect to %s (%u)", addr, err); return; } @@ -68,7 +79,7 @@ static void disconnected(struct bt_conn *conn, uint8_t reason) static void security_changed(struct bt_conn *conn, bt_security_t level, enum bt_security_err err) { if (err != BT_SECURITY_ERR_SUCCESS) { - FAIL("Security failed (err %d)\n", err); + TEST_FAIL("Security failed (err %d)", err); } else { SET_FLAG(flag_security_changed); } @@ -101,14 +112,14 @@ void device_found(const bt_addr_le_t *addr, int8_t rssi, uint8_t type, printk("Stopping scan\n"); err = bt_le_scan_stop(); if (err != 0) { - FAIL("Could not stop scan: %d\n"); + TEST_FAIL("Could not stop scan: %d"); return; } err = bt_conn_le_create(addr, BT_CONN_LE_CREATE_CONN, BT_LE_CONN_PARAM_DEFAULT, &g_conn); if (err != 0) { - FAIL("Could not connect to peer: %d\n", err); + TEST_FAIL("Could not connect to peer: %d", err); } } @@ -120,7 +131,7 @@ static uint8_t discover_func(struct bt_conn *conn, if (attr == NULL) { if (chrc_handle == 0 || long_chrc_handle == 0) { - FAIL("Did not discover chrc (%x) or long_chrc (%x)\n", chrc_handle, + TEST_FAIL("Did not discover chrc (%x) or long_chrc (%x)", chrc_handle, long_chrc_handle); } @@ -142,7 +153,7 @@ static uint8_t discover_func(struct bt_conn *conn, err = bt_gatt_discover(conn, params); if (err != 0) { - FAIL("Discover failed (err %d)\n", err); + TEST_FAIL("Discover failed (err %d)", err); } return BT_GATT_ITER_STOP; @@ -182,7 +193,7 @@ static void gatt_discover(void) err = bt_gatt_discover(g_conn, &discover_params); if (err != 0) { - FAIL("Discover failed(err %d)\n", err); + TEST_FAIL("Discover failed(err %d)", err); } WAIT_FOR_FLAG(flag_discover_complete); @@ -196,7 +207,7 @@ static void update_security(void) printk("Updating security\n"); err = bt_conn_set_security(g_conn, BT_SECURITY_L2); if (err != 0) { - FAIL("Set security failed (err %d)\n", err); + TEST_FAIL("Set security failed (err %d)", err); } WAIT_FOR_FLAG(flag_security_changed); @@ -241,13 +252,13 @@ static void gatt_write(uint16_t handle, uint8_t expect_att_err) err = bt_gatt_write(g_conn, &write_params); if (err != 0) { - FAIL("bt_gatt_write failed: %d\n", err); + TEST_FAIL("bt_gatt_write failed: %d", err); } WAIT_FOR_FLAG(flag_write_complete); if (att_err != expect_att_err) { - FAIL("Write failed: 0x%02X\n", att_err); + TEST_FAIL("Write failed: 0x%02X", att_err); } printk("success\n"); @@ -270,7 +281,8 @@ static uint8_t gatt_read_cb(struct bt_conn *conn, uint8_t err, if (data != NULL) { if (data_received_size + length > sizeof(data_received)) { - FAIL("Invalid amount of data received: %u\n", data_received_size + length); + TEST_FAIL("Invalid amount of data received: %u", + data_received_size + length); } else { memcpy(&data_received[data_received_size], data, length); data_received_size += length; @@ -282,23 +294,25 @@ static uint8_t gatt_read_cb(struct bt_conn *conn, uint8_t err, if (params->single.handle == chrc_handle) { if (data_received_size != CHRC_SIZE || memcmp(data_received, chrc_data, data_received_size) != 0) { - FAIL("chrc data different than expected (%u %u)\n", length, CHRC_SIZE); + TEST_FAIL("chrc data different than expected (%u %u)", length, CHRC_SIZE); } } else if (params->single.handle == long_chrc_handle) { if (data_received_size != LONG_CHRC_SIZE || memcmp(data_received, long_chrc_data, data_received_size) != 0) { - FAIL("long_chrc data different than expected (%u %u)\n", length, - LONG_CHRC_SIZE); + TEST_FAIL("long_chrc data different than expected (%u %u)", length, + LONG_CHRC_SIZE); } } else if (params->single.handle == enc_chrc_handle) { if (data_received_size != CHRC_SIZE || memcmp(data_received, chrc_data, data_received_size) != 0) { - FAIL("enc_chrc data different than expected (%u %u)\n", length, CHRC_SIZE); + TEST_FAIL("enc_chrc data different than expected (%u %u)", length, + CHRC_SIZE); } } else if (params->single.handle == lesc_chrc_handle) { if (data_received_size != CHRC_SIZE || memcmp(data_received, chrc_data, data_received_size) != 0) { - FAIL("lesc_chrc data different than expected (%u %u)\n", length, CHRC_SIZE); + TEST_FAIL("lesc_chrc data different than expected (%u %u)", length, + CHRC_SIZE); } } @@ -335,13 +349,13 @@ static void gatt_read(uint16_t handle, uint8_t expect_att_err) err = bt_gatt_read(g_conn, &read_params); if (err != 0) { - FAIL("bt_gatt_read failed: %d\n", err); + TEST_FAIL("bt_gatt_read failed: %d", err); } WAIT_FOR_FLAG(flag_read_complete); if (att_err != expect_att_err) { - FAIL("Read failed: 0x%02X\n", att_err); + TEST_FAIL("Read failed: 0x%02X", att_err); } printk("success\n"); @@ -355,12 +369,12 @@ static void test_main(void) err = bt_enable(NULL); if (err != 0) { - FAIL("Bluetooth discover failed (err %d)\n", err); + TEST_FAIL("Bluetooth discover failed (err %d)", err); } err = bt_le_scan_start(BT_LE_SCAN_PASSIVE, device_found); if (err != 0) { - FAIL("Scanning failed to start (err %d)\n", err); + TEST_FAIL("Scanning failed to start (err %d)", err); } printk("Scanning successfully started\n"); @@ -389,14 +403,12 @@ static void test_main(void) gatt_write(lesc_chrc_handle, BT_ATT_ERR_SUCCESS); gatt_read(lesc_chrc_handle, BT_ATT_ERR_SUCCESS); - PASS("GATT client Passed\n"); + TEST_PASS("GATT client Passed"); } static const struct bst_test_instance test_vcs[] = { { .test_id = "gatt_client", - .test_pre_init_f = test_init, - .test_tick_f = test_tick, .test_main_f = test_main }, BSTEST_END_MARKER diff --git a/tests/bsim/bluetooth/host/gatt/general/src/gatt_server_test.c b/tests/bsim/bluetooth/host/gatt/general/src/gatt_server_test.c index 476f5bba0ef4..6f3225c4d293 100644 --- a/tests/bsim/bluetooth/host/gatt/general/src/gatt_server_test.c +++ b/tests/bsim/bluetooth/host/gatt/general/src/gatt_server_test.c @@ -4,11 +4,25 @@ * SPDX-License-Identifier: Apache-2.0 */ +#include + +#include +#include +#include + +#include +#include +#include +#include +#include + +#include "babblekit/testcase.h" +#include "babblekit/flags.h" #include "common.h" extern enum bst_result_t bst_result; -CREATE_FLAG(flag_is_connected); +static DEFINE_FLAG(flag_is_connected); static struct bt_conn *g_conn; @@ -19,7 +33,7 @@ static void connected(struct bt_conn *conn, uint8_t err) bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr)); if (err != 0) { - FAIL("Failed to connect to %s (%u)\n", addr, err); + TEST_FAIL("Failed to connect to %s (%u)", addr, err); return; } @@ -79,7 +93,7 @@ static ssize_t write_test_chrc(struct bt_conn *conn, } if (flags != 0) { - FAIL("Invalid flags %u\n", flags); + TEST_FAIL("Invalid flags %u", flags); return BT_GATT_ERR(BT_ATT_ERR_UNLIKELY); } @@ -155,7 +169,7 @@ static void test_main(void) err = bt_enable(NULL); if (err != 0) { - FAIL("Bluetooth init failed (err %d)\n", err); + TEST_FAIL("Bluetooth init failed (err %d)", err); return; } @@ -163,7 +177,7 @@ static void test_main(void) err = bt_le_adv_start(BT_LE_ADV_CONN_FAST_1, ad, ARRAY_SIZE(ad), NULL, 0); if (err != 0) { - FAIL("Advertising failed to start (err %d)\n", err); + TEST_FAIL("Advertising failed to start (err %d)", err); return; } @@ -171,14 +185,12 @@ static void test_main(void) WAIT_FOR_FLAG(flag_is_connected); - PASS("GATT server passed\n"); + TEST_PASS("GATT server passed"); } static const struct bst_test_instance test_gatt_server[] = { { .test_id = "gatt_server", - .test_pre_init_f = test_init, - .test_tick_f = test_tick, .test_main_f = test_main }, BSTEST_END_MARKER diff --git a/tests/bsim/bluetooth/host/gatt/general/test_scripts/gatt.sh b/tests/bsim/bluetooth/host/gatt/general/test_scripts/gatt.sh index df41707e832a..dbcf00784033 100755 --- a/tests/bsim/bluetooth/host/gatt/general/test_scripts/gatt.sh +++ b/tests/bsim/bluetooth/host/gatt/general/test_scripts/gatt.sh @@ -21,6 +21,6 @@ Execute ./bs_${BOARD_TS}_tests_bsim_bluetooth_host_gatt_general_prj_conf \ -v=${verbosity_level} -s=${simulation_id} -d=1 -testid=gatt_server -RealEncryption=1 Execute ./bs_2G4_phy_v1 -v=${verbosity_level} -s=${simulation_id} \ - -D=2 -sim_length=60e6 $@ + -D=2 -sim_length=30e6 $@ wait_for_background_jobs diff --git a/tests/bsim/bluetooth/host/gatt/notify/CMakeLists.txt b/tests/bsim/bluetooth/host/gatt/notify/CMakeLists.txt index 7869a21840fe..b279e8c14396 100644 --- a/tests/bsim/bluetooth/host/gatt/notify/CMakeLists.txt +++ b/tests/bsim/bluetooth/host/gatt/notify/CMakeLists.txt @@ -5,6 +5,9 @@ cmake_minimum_required(VERSION 3.20.0) find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) project(bsim_test_gatt) +add_subdirectory(${ZEPHYR_BASE}/tests/bsim/babblekit babblekit) +target_link_libraries(app PRIVATE babblekit) + FILE(GLOB app_sources src/*.c) target_sources(app PRIVATE ${app_sources} ) diff --git a/tests/bsim/bluetooth/host/gatt/notify/src/common.c b/tests/bsim/bluetooth/host/gatt/notify/src/common.c deleted file mode 100644 index df438607c5fa..000000000000 --- a/tests/bsim/bluetooth/host/gatt/notify/src/common.c +++ /dev/null @@ -1,20 +0,0 @@ -/* - * Copyright (c) 2022 Nordic Semiconductor ASA - * - * SPDX-License-Identifier: Apache-2.0 - */ - -#include "common.h" - -void test_tick(bs_time_t HW_device_time) -{ - if (bst_result != Passed) { - FAIL("test failed (not passed after %i seconds)\n", WAIT_TIME); - } -} - -void test_init(void) -{ - bst_ticker_set_next_tick_absolute(WAIT_TIME); - bst_result = In_progress; -} diff --git a/tests/bsim/bluetooth/host/gatt/notify/src/common.h b/tests/bsim/bluetooth/host/gatt/notify/src/common.h index 67486b175e41..1058eb86d37b 100644 --- a/tests/bsim/bluetooth/host/gatt/notify/src/common.h +++ b/tests/bsim/bluetooth/host/gatt/notify/src/common.h @@ -6,46 +6,7 @@ * SPDX-License-Identifier: Apache-2.0 */ -#include - -#include "bs_types.h" -#include "bs_tracing.h" -#include "time_machine.h" -#include "bstests.h" - -#include -#include -#include - -#include -#include -#include -#include -#include - -extern enum bst_result_t bst_result; - -#define WAIT_TIME (60 * 1e6) /*seconds*/ - -#define CREATE_FLAG(flag) static atomic_t flag = (atomic_t)false -#define SET_FLAG(flag) (void)atomic_set(&flag, (atomic_t)true) -#define UNSET_FLAG(flag) (void)atomic_set(&flag, (atomic_t)false) -#define WAIT_FOR_FLAG(flag) \ - while (!(bool)atomic_get(&flag)) { \ - (void)k_sleep(K_MSEC(1)); \ - } - -#define FAIL(...) \ - do { \ - bst_result = Failed; \ - bs_trace_error_time_line(__VA_ARGS__); \ - } while (0) - -#define PASS(...) \ - do { \ - bst_result = Passed; \ - bs_trace_info_time(1, __VA_ARGS__); \ - } while (0) +#include #define CHRC_SIZE 10 #define LONG_CHRC_SIZE 40 @@ -62,8 +23,5 @@ extern enum bst_result_t bst_result; BT_UUID_DECLARE_128(0x01, 0x23, 0x45, 0x67, 0x89, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, \ 0x07, 0x08, 0x09, 0xFF, 0x11) -void test_tick(bs_time_t HW_device_time); -void test_init(void); - #define NOTIFICATION_COUNT 10 BUILD_ASSERT(NOTIFICATION_COUNT % 2 == 0); diff --git a/tests/bsim/bluetooth/host/gatt/notify/src/gatt_client_test.c b/tests/bsim/bluetooth/host/gatt/notify/src/gatt_client_test.c index 125029653a09..273ddf51aa71 100644 --- a/tests/bsim/bluetooth/host/gatt/notify/src/gatt_client_test.c +++ b/tests/bsim/bluetooth/host/gatt/notify/src/gatt_client_test.c @@ -4,16 +4,26 @@ * SPDX-License-Identifier: Apache-2.0 */ +#include +#include + +#include +#include #include +#include +#include +#include #include +#include "babblekit/testcase.h" +#include "babblekit/flags.h" #include "common.h" -CREATE_FLAG(flag_is_connected); -CREATE_FLAG(flag_is_encrypted); -CREATE_FLAG(flag_discover_complete); -CREATE_FLAG(flag_short_subscribed); -CREATE_FLAG(flag_long_subscribed); +static DEFINE_FLAG(flag_is_connected); +static DEFINE_FLAG(flag_is_encrypted); +static DEFINE_FLAG(flag_discover_complete); +static DEFINE_FLAG(flag_short_subscribed); +static DEFINE_FLAG(flag_long_subscribed); static struct bt_conn *g_conn; static uint16_t chrc_handle; @@ -27,7 +37,7 @@ static void connected(struct bt_conn *conn, uint8_t err) bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr)); if (err != 0) { - FAIL("Failed to connect to %s (%u)\n", addr, err); + TEST_FAIL("Failed to connect to %s (%u)", addr, err); return; } @@ -57,9 +67,9 @@ static void disconnected(struct bt_conn *conn, uint8_t reason) void security_changed(struct bt_conn *conn, bt_security_t level, enum bt_security_err err) { if (err) { - FAIL("Encryption failer (%d)\n", err); + TEST_FAIL("Encryption failure (%d)", err); } else if (level < BT_SECURITY_L2) { - FAIL("Insufficient sec level (%d)\n", level); + TEST_FAIL("Insufficient sec level (%d)", level); } else { SET_FLAG(flag_is_encrypted); } @@ -91,13 +101,13 @@ void device_found(const bt_addr_le_t *addr, int8_t rssi, uint8_t type, struct ne printk("Stopping scan\n"); err = bt_le_scan_stop(); if (err != 0) { - FAIL("Could not stop scan: %d"); + TEST_FAIL("Could not stop scan: %d"); return; } err = bt_conn_le_create(addr, BT_CONN_LE_CREATE_CONN, BT_LE_CONN_PARAM_DEFAULT, &g_conn); if (err != 0) { - FAIL("Could not connect to peer: %d", err); + TEST_FAIL("Could not connect to peer: %d", err); } } @@ -108,7 +118,7 @@ static uint8_t discover_func(struct bt_conn *conn, const struct bt_gatt_attr *at if (attr == NULL) { if (chrc_handle == 0 || long_chrc_handle == 0) { - FAIL("Did not discover chrc (%x) or long_chrc (%x)", chrc_handle, + TEST_FAIL("Did not discover chrc (%x) or long_chrc (%x)", chrc_handle, long_chrc_handle); } @@ -130,7 +140,7 @@ static uint8_t discover_func(struct bt_conn *conn, const struct bt_gatt_attr *at err = bt_gatt_discover(conn, params); if (err != 0) { - FAIL("Discover failed (err %d)\n", err); + TEST_FAIL("Discover failed (err %d)", err); } return BT_GATT_ITER_STOP; @@ -165,7 +175,7 @@ static void gatt_discover(enum bt_att_chan_opt opt) err = bt_gatt_discover(g_conn, &discover_params); if (err != 0) { - FAIL("Discover failed(err %d)\n", err); + TEST_FAIL("Discover failed(err %d)", err); } WAIT_FOR_FLAG(flag_discover_complete); @@ -176,7 +186,7 @@ static void test_short_subscribed(struct bt_conn *conn, uint8_t err, struct bt_gatt_subscribe_params *params) { if (err) { - FAIL("Subscribe failed (err %d)\n", err); + TEST_FAIL("Subscribe failed (err %d)", err); } SET_FLAG(flag_short_subscribed); @@ -189,7 +199,7 @@ static void test_short_subscribed(struct bt_conn *conn, uint8_t err, if (params->value_handle == chrc_handle) { printk("Subscribed to short characteristic\n"); } else { - FAIL("Unknown handle %d\n", params->value_handle); + TEST_FAIL("Unknown handle %d", params->value_handle); } } @@ -197,7 +207,7 @@ static void test_long_subscribed(struct bt_conn *conn, uint8_t err, struct bt_gatt_subscribe_params *params) { if (err) { - FAIL("Subscribe failed (err %d)\n", err); + TEST_FAIL("Subscribe failed (err %d)", err); } SET_FLAG(flag_long_subscribed); @@ -210,7 +220,7 @@ static void test_long_subscribed(struct bt_conn *conn, uint8_t err, if (params->value_handle == long_chrc_handle) { printk("Subscribed to long characteristic\n"); } else { - FAIL("Unknown handle %d\n", params->value_handle); + TEST_FAIL("Unknown handle %d", params->value_handle); } } @@ -250,7 +260,7 @@ static void gatt_subscribe_short(enum bt_att_chan_opt opt) sub_params_short.chan_opt = opt; err = bt_gatt_subscribe(g_conn, &sub_params_short); if (err < 0) { - FAIL("Failed to subscribe\n"); + TEST_FAIL("Failed to subscribe"); } else { printk("Subscribe request sent\n"); } @@ -264,7 +274,7 @@ static void gatt_unsubscribe_short(enum bt_att_chan_opt opt) sub_params_short.chan_opt = opt; err = bt_gatt_unsubscribe(g_conn, &sub_params_short); if (err < 0) { - FAIL("Failed to unsubscribe\n"); + TEST_FAIL("Failed to unsubscribe"); } else { printk("Unsubscribe request sent\n"); } @@ -279,7 +289,7 @@ static void gatt_subscribe_long(enum bt_att_chan_opt opt) sub_params_long.chan_opt = opt; err = bt_gatt_subscribe(g_conn, &sub_params_long); if (err < 0) { - FAIL("Failed to subscribe\n"); + TEST_FAIL("Failed to subscribe"); } else { printk("Subscribe request sent\n"); } @@ -294,7 +304,7 @@ static void gatt_unsubscribe_long(enum bt_att_chan_opt opt) sub_params_long.chan_opt = opt; err = bt_gatt_unsubscribe(g_conn, &sub_params_long); if (err < 0) { - FAIL("Failed to unsubscribe\n"); + TEST_FAIL("Failed to unsubscribe"); } else { printk("Unsubscribe request sent\n"); } @@ -306,12 +316,12 @@ static void setup(void) err = bt_enable(NULL); if (err != 0) { - FAIL("Bluetooth discover failed (err %d)\n", err); + TEST_FAIL("Bluetooth discover failed (err %d)", err); } err = bt_le_scan_start(BT_LE_SCAN_PASSIVE, device_found); if (err != 0) { - FAIL("Scanning failed to start (err %d)\n", err); + TEST_FAIL("Scanning failed to start (err %d)", err); } printk("Scanning successfully started\n"); @@ -320,7 +330,7 @@ static void setup(void) err = bt_conn_set_security(g_conn, BT_SECURITY_L2); if (err) { - FAIL("Starting encryption procedure failed (%d)\n", err); + TEST_FAIL("Starting encryption procedure failed (%d)", err); } WAIT_FOR_FLAG(flag_is_encrypted); @@ -354,7 +364,7 @@ static void test_main_none(void) printk("Unsubscribed\n"); - PASS("GATT client Passed\n"); + TEST_PASS("GATT client Passed"); } static void test_main_unenhanced(void) @@ -380,7 +390,7 @@ static void test_main_unenhanced(void) printk("Unsubscribed\n"); - PASS("GATT client Passed\n"); + TEST_PASS("GATT client Passed"); } static void test_main_enhanced(void) @@ -406,7 +416,7 @@ static void test_main_enhanced(void) printk("Unsubscribed\n"); - PASS("GATT client Passed\n"); + TEST_PASS("GATT client Passed"); } static void test_main_mixed(void) @@ -432,32 +442,24 @@ static void test_main_mixed(void) printk("Unsubscribed\n"); - PASS("GATT client Passed\n"); + TEST_PASS("GATT client Passed"); } static const struct bst_test_instance test_vcs[] = { { .test_id = "gatt_client_none", - .test_pre_init_f = test_init, - .test_tick_f = test_tick, .test_main_f = test_main_none, }, { .test_id = "gatt_client_unenhanced", - .test_pre_init_f = test_init, - .test_tick_f = test_tick, .test_main_f = test_main_unenhanced, }, { .test_id = "gatt_client_enhanced", - .test_pre_init_f = test_init, - .test_tick_f = test_tick, .test_main_f = test_main_enhanced, }, { .test_id = "gatt_client_mixed", - .test_pre_init_f = test_init, - .test_tick_f = test_tick, .test_main_f = test_main_mixed, }, BSTEST_END_MARKER, diff --git a/tests/bsim/bluetooth/host/gatt/notify/src/gatt_server_test.c b/tests/bsim/bluetooth/host/gatt/notify/src/gatt_server_test.c index 6b4cc13a7c25..d293ad9ac19d 100644 --- a/tests/bsim/bluetooth/host/gatt/notify/src/gatt_server_test.c +++ b/tests/bsim/bluetooth/host/gatt/notify/src/gatt_server_test.c @@ -3,14 +3,26 @@ * * SPDX-License-Identifier: Apache-2.0 */ - +#include +#include + +#include +#include +#include +#include +#include +#include +#include + +#include "babblekit/testcase.h" +#include "babblekit/flags.h" #include "common.h" extern enum bst_result_t bst_result; -CREATE_FLAG(flag_is_connected); -CREATE_FLAG(flag_short_subscribe); -CREATE_FLAG(flag_long_subscribe); +static DEFINE_FLAG(flag_is_connected); +static DEFINE_FLAG(flag_short_subscribe); +static DEFINE_FLAG(flag_long_subscribe); static struct bt_conn *g_conn; @@ -25,7 +37,7 @@ static void connected(struct bt_conn *conn, uint8_t err) bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr)); if (err != 0) { - FAIL("Failed to connect to %s (%u)\n", addr, err); + TEST_FAIL("Failed to connect to %s (%u)", addr, err); return; } @@ -137,7 +149,7 @@ static void short_notify(enum bt_att_chan_opt opt) if (err == -ENOMEM) { k_sleep(K_MSEC(10)); } else if (err) { - FAIL("Short notify failed (err %d)\n", err); + TEST_FAIL("Short notify failed (err %d)", err); } } while (err); } @@ -163,7 +175,7 @@ static void long_notify(enum bt_att_chan_opt opt) if (err == -ENOMEM) { k_sleep(K_MSEC(10)); } else if (err) { - FAIL("Long notify failed (err %d)\n", err); + TEST_FAIL("Long notify failed (err %d)", err); } } while (err); } @@ -177,7 +189,7 @@ static void setup(void) err = bt_enable(NULL); if (err != 0) { - FAIL("Bluetooth init failed (err %d)\n", err); + TEST_FAIL("Bluetooth init failed (err %d)", err); return; } @@ -185,7 +197,7 @@ static void setup(void) err = bt_le_adv_start(BT_LE_ADV_CONN_FAST_1, ad, ARRAY_SIZE(ad), NULL, 0); if (err != 0) { - FAIL("Advertising failed to start (err %d)\n", err); + TEST_FAIL("Advertising failed to start (err %d)", err); return; } @@ -215,7 +227,7 @@ static void test_main_none(void) k_sleep(K_MSEC(100)); } - PASS("GATT server passed\n"); + TEST_PASS("GATT server passed"); } static void test_main_enhanced(void) @@ -231,7 +243,7 @@ static void test_main_enhanced(void) k_sleep(K_MSEC(100)); } - PASS("GATT server passed\n"); + TEST_PASS("GATT server passed"); } static void test_main_unenhanced(void) @@ -247,7 +259,7 @@ static void test_main_unenhanced(void) k_sleep(K_MSEC(100)); } - PASS("GATT server passed\n"); + TEST_PASS("GATT server passed"); } static void test_main_mixed(void) @@ -263,32 +275,24 @@ static void test_main_mixed(void) k_sleep(K_MSEC(100)); } - PASS("GATT server passed\n"); + TEST_PASS("GATT server passed"); } static const struct bst_test_instance test_gatt_server[] = { { .test_id = "gatt_server_none", - .test_pre_init_f = test_init, - .test_tick_f = test_tick, .test_main_f = test_main_none, }, { .test_id = "gatt_server_unenhanced", - .test_pre_init_f = test_init, - .test_tick_f = test_tick, .test_main_f = test_main_unenhanced, }, { .test_id = "gatt_server_enhanced", - .test_pre_init_f = test_init, - .test_tick_f = test_tick, .test_main_f = test_main_enhanced, }, { .test_id = "gatt_server_mixed", - .test_pre_init_f = test_init, - .test_tick_f = test_tick, .test_main_f = test_main_mixed, }, BSTEST_END_MARKER, diff --git a/tests/bsim/bluetooth/host/gatt/settings/src/main.c b/tests/bsim/bluetooth/host/gatt/settings/src/main.c index 6fd131ca3ed8..12fbed9e8f89 100644 --- a/tests/bsim/bluetooth/host/gatt/settings/src/main.c +++ b/tests/bsim/bluetooth/host/gatt/settings/src/main.c @@ -4,6 +4,7 @@ * SPDX-License-Identifier: Apache-2.0 */ +#include #include "utils.h" #include "main.h" #include "argparse.h" @@ -41,8 +42,8 @@ static void test_args(int argc, char **argv) { __ASSERT(argc == 3, "Please specify only 3 test arguments\n"); - test_round = atol(argv[0]); - final_round = atol(argv[1]); + test_round = strtol(argv[0], NULL, 10); + final_round = strtol(argv[1], NULL, 10); settings_file = argv[2]; bs_trace_raw(0, "Test round %u\n", test_round); diff --git a/tests/bsim/bluetooth/host/iso/bis/CMakeLists.txt b/tests/bsim/bluetooth/host/iso/bis/CMakeLists.txt index 89a3d5cf313d..c9ecd0997a3f 100644 --- a/tests/bsim/bluetooth/host/iso/bis/CMakeLists.txt +++ b/tests/bsim/bluetooth/host/iso/bis/CMakeLists.txt @@ -10,7 +10,6 @@ add_subdirectory(${ZEPHYR_BASE}/tests/bsim/babblekit babblekit) target_link_libraries(app PRIVATE babblekit) target_sources(app PRIVATE - src/common.c src/bis_broadcaster.c src/bis_receiver.c src/main.c diff --git a/tests/bsim/bluetooth/host/iso/bis/src/bis_broadcaster.c b/tests/bsim/bluetooth/host/iso/bis/src/bis_broadcaster.c index 4afc0bb32472..cef7d417ff30 100644 --- a/tests/bsim/bluetooth/host/iso/bis/src/bis_broadcaster.c +++ b/tests/bsim/bluetooth/host/iso/bis/src/bis_broadcaster.c @@ -3,9 +3,6 @@ * * SPDX-License-Identifier: Apache-2.0 */ - -#include "common.h" - #include #include #include @@ -308,15 +305,11 @@ static const struct bst_test_instance test_def[] = { { .test_id = "broadcaster", .test_descr = "Minimal BIS broadcaster that broadcast ISO data", - .test_pre_init_f = test_init, - .test_tick_f = test_tick, .test_main_f = test_main, }, { .test_id = "broadcaster_disable", .test_descr = "BIS broadcaster that tests bt_disable for ISO", - .test_pre_init_f = test_init, - .test_tick_f = test_tick, .test_main_f = test_main_disable, }, BSTEST_END_MARKER, diff --git a/tests/bsim/bluetooth/host/iso/bis/src/bis_receiver.c b/tests/bsim/bluetooth/host/iso/bis/src/bis_receiver.c index ac72cd26694c..d4a03284c17c 100644 --- a/tests/bsim/bluetooth/host/iso/bis/src/bis_receiver.c +++ b/tests/bsim/bluetooth/host/iso/bis/src/bis_receiver.c @@ -5,7 +5,6 @@ */ #include -#include "common.h" #include #include @@ -270,8 +269,6 @@ static const struct bst_test_instance test_def[] = { { .test_id = "receiver", .test_descr = "receiver", - .test_pre_init_f = test_init, - .test_tick_f = test_tick, .test_main_f = test_main, }, BSTEST_END_MARKER, diff --git a/tests/bsim/bluetooth/host/iso/bis/src/common.c b/tests/bsim/bluetooth/host/iso/bis/src/common.c deleted file mode 100644 index f644f9181c2b..000000000000 --- a/tests/bsim/bluetooth/host/iso/bis/src/common.c +++ /dev/null @@ -1,31 +0,0 @@ -/* - * Copyright (c) 2024 Nordic Semiconductor ASA - * - * SPDX-License-Identifier: Apache-2.0 - */ -#include - -#include -#include -#include -#include - -#include "babblekit/flags.h" -#include "babblekit/testcase.h" - -#include "common.h" - -extern enum bst_result_t bst_result; - -void test_init(void) -{ - bst_result = In_progress; - bst_ticker_set_next_tick_absolute(WAIT_TIME); -} - -void test_tick(bs_time_t HW_device_time) -{ - if (bst_result != Passed) { - TEST_FAIL("Test failed (not passed after %" PRIu64 " us)", WAIT_TIME); - } -} diff --git a/tests/bsim/bluetooth/host/iso/bis/src/common.h b/tests/bsim/bluetooth/host/iso/bis/src/common.h deleted file mode 100644 index 60a191e38838..000000000000 --- a/tests/bsim/bluetooth/host/iso/bis/src/common.h +++ /dev/null @@ -1,16 +0,0 @@ -/* - * Common functions and helpers for ISO broadcast tests - * - * Copyright (c) 2024 Nordic Semiconductor ASA - * - * SPDX-License-Identifier: Apache-2.0 - */ - -#include "bs_types.h" -#include "bs_tracing.h" -#include "bstests.h" - -#define WAIT_TIME (30e6) /* 30 seconds*/ - -void test_init(void); -void test_tick(bs_time_t HW_device_time); diff --git a/tests/bsim/bluetooth/host/iso/cis/CMakeLists.txt b/tests/bsim/bluetooth/host/iso/cis/CMakeLists.txt index 2e8e50c8f7fd..a0328bc68707 100644 --- a/tests/bsim/bluetooth/host/iso/cis/CMakeLists.txt +++ b/tests/bsim/bluetooth/host/iso/cis/CMakeLists.txt @@ -18,6 +18,9 @@ zephyr_include_directories( ) add_subdirectory(${ZEPHYR_BASE}/tests/bluetooth/common/testlib testlib) +add_subdirectory(${ZEPHYR_BASE}/tests/bsim/babblekit babblekit) + target_link_libraries(app PRIVATE testlib + babblekit ) diff --git a/tests/bsim/bluetooth/host/iso/cis/src/cis_central.c b/tests/bsim/bluetooth/host/iso/cis/src/cis_central.c index 80fff68d4ef3..75c92a9e181e 100644 --- a/tests/bsim/bluetooth/host/iso/cis/src/cis_central.c +++ b/tests/bsim/bluetooth/host/iso/cis/src/cis_central.c @@ -4,6 +4,8 @@ * SPDX-License-Identifier: Apache-2.0 */ +#include "babblekit/testcase.h" +#include "babblekit/flags.h" #include "common.h" #include @@ -25,7 +27,7 @@ NET_BUF_POOL_FIXED_DEFINE(tx_pool, ENQUEUE_COUNT, BT_ISO_SDU_BUF_SIZE(CONFIG_BT_ BUILD_ASSERT(CONFIG_BT_ISO_MAX_CHAN > 1, "CONFIG_BT_ISO_MAX_CHAN shall be at least 2"); -CREATE_FLAG(flag_iso_connected); +static DEFINE_FLAG(flag_iso_connected); static void send_data_cb(struct k_work *work) { @@ -35,7 +37,7 @@ static void send_data_cb(struct k_work *work) struct net_buf *buf; int ret; - if (!TEST_FLAG(flag_iso_connected)) { + if (!IS_FLAG_SET(flag_iso_connected)) { /* TX has been aborted */ return; } @@ -86,7 +88,7 @@ static void device_found(const bt_addr_le_t *addr, int8_t rssi, uint8_t type, err = bt_le_scan_stop(); if (err) { - FAIL("Failed to stop scanning (err %d)\n", err); + TEST_FAIL("Failed to stop scanning (err %d)", err); return; } @@ -94,7 +96,7 @@ static void device_found(const bt_addr_le_t *addr, int8_t rssi, uint8_t type, err = bt_conn_le_create(addr, BT_CONN_LE_CREATE_CONN, BT_LE_CONN_PARAM_DEFAULT, &default_conn); if (err) { - FAIL("Failed to create connection (err %d)\n", err); + TEST_FAIL("Failed to create connection (err %d)", err); return; } @@ -132,14 +134,14 @@ static void sdu_sent_cb(struct bt_iso_chan *chan) enqueue_cnt++; - if (!TEST_FLAG(flag_iso_connected)) { + if (!IS_FLAG_SET(flag_iso_connected)) { /* TX has been aborted */ return; } err = k_work_schedule(&iso_send_work, K_NO_WAIT); if (err < 0) { - FAIL("Failed to schedule TX for chan %p: %d\n", chan, err); + TEST_FAIL("Failed to schedule TX for chan %p: %d", chan, err); } } @@ -164,7 +166,7 @@ static void init(void) err = bt_enable(NULL); if (err != 0) { - FAIL("Bluetooth enable failed (err %d)\n", err); + TEST_FAIL("Bluetooth enable failed (err %d)", err); return; } @@ -208,7 +210,7 @@ static void create_cig(size_t iso_channels) err = bt_iso_cig_create(¶m, &cig); if (err != 0) { - FAIL("Failed to create CIG (%d)\n", err); + TEST_FAIL("Failed to create CIG (%d)", err); return; } @@ -224,14 +226,14 @@ static int reconfigure_cig_interval(struct bt_iso_cig_param *param) param->p_to_c_interval = param->c_to_p_interval; err = bt_iso_cig_reconfigure(cig, param); if (err != 0) { - FAIL("Failed to reconfigure CIG to new interval (%d)\n", err); + TEST_FAIL("Failed to reconfigure CIG to new interval (%d)", err); return err; } err = bt_iso_cig_reconfigure(cig, param); if (err != 0) { - FAIL("Failed to reconfigure CIG to same interval (%d)\n", err); + TEST_FAIL("Failed to reconfigure CIG to same interval (%d)", err); return err; } @@ -241,7 +243,7 @@ static int reconfigure_cig_interval(struct bt_iso_cig_param *param) param->p_to_c_interval = 2500; /* us */ err = bt_iso_cig_reconfigure(cig, param); if (err != 0) { - FAIL("Failed to reconfigure CIG to new interval (%d)\n", err); + TEST_FAIL("Failed to reconfigure CIG to new interval (%d)", err); return err; } @@ -259,7 +261,7 @@ static int reconfigure_cig_latency(struct bt_iso_cig_param *param) param->p_to_c_latency = param->c_to_p_latency; err = bt_iso_cig_reconfigure(cig, param); if (err != 0) { - FAIL("Failed to reconfigure CIG latency (%d)\n", err); + TEST_FAIL("Failed to reconfigure CIG latency (%d)", err); return err; } @@ -268,7 +270,7 @@ static int reconfigure_cig_latency(struct bt_iso_cig_param *param) param->p_to_c_latency = 40; /* ms */ err = bt_iso_cig_reconfigure(cig, param); if (err != 0) { - FAIL("Failed to reconfigure CIG for different latencies (%d)\n", err); + TEST_FAIL("Failed to reconfigure CIG for different latencies (%d)", err); return err; } @@ -293,7 +295,7 @@ static void reconfigure_cig(void) err = bt_iso_cig_reconfigure(cig, ¶m); if (err != 0) { - FAIL("Failed to reconfigure CIS to new RTN (%d)\n", err); + TEST_FAIL("Failed to reconfigure CIS to new RTN (%d)", err); return; } @@ -316,7 +318,8 @@ static void reconfigure_cig(void) err = bt_iso_cig_reconfigure(cig, ¶m); if (err != 0) { - FAIL("Failed to reconfigure CIG with new CIS and original parameters (%d)\n", err); + TEST_FAIL("Failed to reconfigure CIG with new CIS and original parameters (%d)", + err); return; } @@ -328,12 +331,12 @@ static void connect_acl(void) err = bt_le_scan_start(BT_LE_SCAN_PASSIVE, device_found); if (err != 0) { - FAIL("Scanning failed to start (err %d)\n", err); + TEST_FAIL("Scanning failed to start (err %d)", err); return; } - WAIT_FOR_FLAG_SET(flag_connected); + WAIT_FOR_FLAG(flag_connected); } static void connect_cis(void) @@ -346,12 +349,12 @@ static void connect_cis(void) err = bt_iso_chan_connect(&connect_param, 1); if (err) { - FAIL("Failed to connect ISO (%d)\n", err); + TEST_FAIL("Failed to connect ISO (%d)", err); return; } - WAIT_FOR_FLAG_SET(flag_iso_connected); + WAIT_FOR_FLAG(flag_iso_connected); } static void disconnect_cis(void) @@ -360,7 +363,7 @@ static void disconnect_cis(void) err = bt_iso_chan_disconnect(default_chan); if (err) { - FAIL("Failed to disconnect ISO (err %d)\n", err); + TEST_FAIL("Failed to disconnect ISO (err %d)", err); return; } @@ -374,7 +377,7 @@ static void disconnect_acl(void) err = bt_conn_disconnect(default_conn, BT_HCI_ERR_REMOTE_USER_TERM_CONN); if (err) { - FAIL("Failed to disconnect ACL (err %d)\n", err); + TEST_FAIL("Failed to disconnect ACL (err %d)", err); return; } @@ -388,7 +391,7 @@ static void terminate_cig(void) err = bt_iso_cig_terminate(cig); if (err != 0) { - FAIL("Failed to terminate CIG (%d)\n", err); + TEST_FAIL("Failed to terminate CIG (%d)", err); return; } @@ -404,7 +407,7 @@ static void reset_bluetooth(void) err = bt_disable(); if (err != 0) { - FAIL("Failed to disable (%d)\n", err); + TEST_FAIL("Failed to disable (%d)", err); return; } @@ -414,7 +417,7 @@ static void reset_bluetooth(void) err = bt_enable(NULL); if (err != 0) { - FAIL("Failed to re-enable (%d)\n", err); + TEST_FAIL("Failed to re-enable (%d)", err); return; } @@ -436,7 +439,7 @@ static void test_main(void) disconnect_acl(); terminate_cig(); - PASS("Test passed\n"); + TEST_PASS("Test passed"); } static void test_main_disable(void) @@ -464,22 +467,18 @@ static void test_main_disable(void) disconnect_acl(); terminate_cig(); - PASS("Disable test passed\n"); + TEST_PASS("Disable test passed"); } static const struct bst_test_instance test_def[] = { { .test_id = "central", .test_descr = "Central", - .test_pre_init_f = test_init, - .test_tick_f = test_tick, .test_main_f = test_main, }, { .test_id = "central_disable", .test_descr = "CIS central that tests bt_disable for ISO", - .test_pre_init_f = test_init, - .test_tick_f = test_tick, .test_main_f = test_main_disable, }, BSTEST_END_MARKER, diff --git a/tests/bsim/bluetooth/host/iso/cis/src/cis_peripheral.c b/tests/bsim/bluetooth/host/iso/cis/src/cis_peripheral.c index 56fe4070ad50..abd7bba1c5a2 100644 --- a/tests/bsim/bluetooth/host/iso/cis/src/cis_peripheral.c +++ b/tests/bsim/bluetooth/host/iso/cis/src/cis_peripheral.c @@ -6,6 +6,8 @@ #include +#include "babblekit/testcase.h" +#include "babblekit/flags.h" #include "common.h" #include @@ -16,7 +18,7 @@ extern enum bst_result_t bst_result; -CREATE_FLAG(flag_data_received); +static DEFINE_FLAG(flag_data_received); static const struct bt_data ad[] = { BT_DATA_BYTES(BT_DATA_FLAGS, (BT_LE_AD_GENERAL | BT_LE_AD_NO_BREDR)), @@ -85,7 +87,7 @@ static int iso_accept(const struct bt_iso_accept_info *info, struct bt_iso_chan printk("Incoming request from %p\n", (void *)info->acl); if (iso_chan.iso) { - FAIL("No channels available\n"); + TEST_FAIL("No channels available"); return -ENOMEM; } @@ -120,7 +122,7 @@ static void init(void) err = bt_enable(NULL); if (err) { - FAIL("Bluetooth enable failed (err %d)\n", err); + TEST_FAIL("Bluetooth enable failed (err %d)", err); return; } @@ -133,7 +135,7 @@ static void init(void) err = bt_iso_server_register(&iso_server); if (err) { - FAIL("Unable to register ISO server (err %d)\n", err); + TEST_FAIL("Unable to register ISO server (err %d)", err); return; } @@ -145,14 +147,14 @@ static void adv_connect(void) err = bt_le_adv_start(BT_LE_ADV_CONN_FAST_1, ad, ARRAY_SIZE(ad), NULL, 0); if (err) { - FAIL("Advertising failed to start (err %d)\n", err); + TEST_FAIL("Advertising failed to start (err %d)", err); return; } printk("Advertising successfully started\n"); - WAIT_FOR_FLAG_SET(flag_connected); + WAIT_FOR_FLAG(flag_connected); } static void test_main(void) @@ -163,8 +165,8 @@ static void test_main(void) adv_connect(); bt_testlib_conn_wait_free(); - if (TEST_FLAG(flag_data_received)) { - PASS("Test passed\n"); + if (IS_FLAG_SET(flag_data_received)) { + TEST_PASS("Test passed"); } } } @@ -173,8 +175,6 @@ static const struct bst_test_instance test_def[] = { { .test_id = "peripheral", .test_descr = "Peripheral", - .test_pre_init_f = test_init, - .test_tick_f = test_tick, .test_main_f = test_main, }, BSTEST_END_MARKER, diff --git a/tests/bsim/bluetooth/host/iso/cis/src/common.c b/tests/bsim/bluetooth/host/iso/cis/src/common.c index ef1a4e9f9dd5..0bd848897b22 100644 --- a/tests/bsim/bluetooth/host/iso/cis/src/common.c +++ b/tests/bsim/bluetooth/host/iso/cis/src/common.c @@ -10,6 +10,8 @@ #include #include +#include "babblekit/testcase.h" +#include "babblekit/flags.h" #include "common.h" extern enum bst_result_t bst_result; @@ -31,7 +33,7 @@ static void connected(struct bt_conn *conn, uint8_t err) bt_conn_unref(default_conn); default_conn = NULL; - FAIL("Failed to connect to %s (0x%02x)\n", addr, err); + TEST_FAIL("Failed to connect to %s (0x%02x)", addr, err); return; } @@ -72,16 +74,3 @@ BT_CONN_CB_DEFINE(conn_callbacks) = { .disconnected = disconnected, .le_param_updated = conn_param_updated_cb, }; - -void test_init(void) -{ - bst_result = In_progress; - bst_ticker_set_next_tick_absolute(WAIT_TIME); -} - -void test_tick(bs_time_t HW_device_time) -{ - if (bst_result != Passed) { - FAIL("Test failed (not passed after %i us)\n", WAIT_TIME); - } -} diff --git a/tests/bsim/bluetooth/host/iso/cis/src/common.h b/tests/bsim/bluetooth/host/iso/cis/src/common.h index 0b41515d2237..53f8d4ed1fba 100644 --- a/tests/bsim/bluetooth/host/iso/cis/src/common.h +++ b/tests/bsim/bluetooth/host/iso/cis/src/common.h @@ -10,40 +10,6 @@ #include -#include "bs_types.h" -#include "bs_tracing.h" -#include "bstests.h" - -#define CREATE_FLAG(flag) static atomic_t flag = (atomic_t) false -#define SET_FLAG(flag) (void)atomic_set(&flag, (atomic_t) true) -#define UNSET_FLAG(flag) (void)atomic_set(&flag, (atomic_t) false) -#define TEST_FLAG(flag) (atomic_get(&flag) == (atomic_t) true) -#define WAIT_FOR_FLAG_SET(flag) \ - while (!(bool)atomic_get(&flag)) { \ - (void)k_sleep(K_MSEC(1)); \ - } -#define WAIT_FOR_FLAG_UNSET(flag) \ - while ((bool)atomic_get(&flag)) { \ - (void)k_sleep(K_MSEC(1)); \ - } - -#define WAIT_TIME (30e6) /* 30 seconds*/ - -#define FAIL(...) \ - do { \ - bst_result = Failed; \ - bs_trace_error_time_line(__VA_ARGS__); \ - } while (0) - -#define PASS(...) \ - do { \ - bst_result = Passed; \ - bs_trace_info_time(1, __VA_ARGS__); \ - } while (0) - extern struct bt_conn *default_conn; extern atomic_t flag_connected; extern atomic_t flag_conn_updated; - -void test_init(void); -void test_tick(bs_time_t HW_device_time); diff --git a/tests/bsim/bluetooth/host/l2cap/credits/CMakeLists.txt b/tests/bsim/bluetooth/host/l2cap/credits/CMakeLists.txt index c28d99b9d678..1fca27583906 100644 --- a/tests/bsim/bluetooth/host/l2cap/credits/CMakeLists.txt +++ b/tests/bsim/bluetooth/host/l2cap/credits/CMakeLists.txt @@ -5,6 +5,9 @@ cmake_minimum_required(VERSION 3.20.0) find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) project(bsim_test_l2cap_credits) +add_subdirectory(${ZEPHYR_BASE}/tests/bsim/babblekit babblekit) +target_link_libraries(app PRIVATE babblekit) + FILE(GLOB app_sources src/*.c) target_sources(app PRIVATE ${app_sources} ) diff --git a/tests/bsim/bluetooth/host/l2cap/credits/src/common.c b/tests/bsim/bluetooth/host/l2cap/credits/src/common.c deleted file mode 100644 index 5b344c60571f..000000000000 --- a/tests/bsim/bluetooth/host/l2cap/credits/src/common.c +++ /dev/null @@ -1,22 +0,0 @@ -/* - * Copyright (c) 2022 Nordic Semiconductor ASA - * - * SPDX-License-Identifier: Apache-2.0 - */ - -#include "common.h" - -extern enum bst_result_t bst_result; - -void test_init(void) -{ - bst_ticker_set_next_tick_absolute(WAIT_TIME); - bst_result = In_progress; -} - -void test_tick(bs_time_t HW_device_time) -{ - if (bst_result != Passed) { - FAIL("test failed (not passed after %i seconds)\n", WAIT_SECONDS); - } -} diff --git a/tests/bsim/bluetooth/host/l2cap/credits/src/common.h b/tests/bsim/bluetooth/host/l2cap/credits/src/common.h deleted file mode 100644 index a778b7d5e1cd..000000000000 --- a/tests/bsim/bluetooth/host/l2cap/credits/src/common.h +++ /dev/null @@ -1,57 +0,0 @@ -/* - * Common functions and helpers for L2CAP tests - * - * Copyright (c) 2022 Nordic Semiconductor ASA - * - * SPDX-License-Identifier: Apache-2.0 - */ - -#include - -#include -#include -#include - -#include -#include -#include -#include "bs_types.h" -#include "bs_tracing.h" -#include "bstests.h" -#include "bs_pc_backchannel.h" - -extern enum bst_result_t bst_result; - -#define CREATE_FLAG(flag) static atomic_t flag = (atomic_t)false -#define SET_FLAG(flag) (void)atomic_set(&flag, (atomic_t)true) -#define UNSET_FLAG(flag) (void)atomic_set(&flag, (atomic_t)false) -#define TEST_FLAG(flag) (atomic_get(&flag) == (atomic_t)true) -#define WAIT_FOR_FLAG_SET(flag) \ - while (!(bool)atomic_get(&flag)) { \ - (void)k_sleep(K_MSEC(1)); \ - } -#define WAIT_FOR_FLAG_UNSET(flag) \ - while ((bool)atomic_get(&flag)) { \ - (void)k_sleep(K_MSEC(1)); \ - } - - -#define WAIT_SECONDS 30 /* seconds */ -#define WAIT_TIME (WAIT_SECONDS * USEC_PER_SEC) /* microseconds*/ - -#define FAIL(...) \ - do { \ - bst_result = Failed; \ - bs_trace_error_time_line(__VA_ARGS__); \ - } while (0) - -#define PASS(...) \ - do { \ - bst_result = Passed; \ - bs_trace_info_time(1, __VA_ARGS__); \ - } while (0) - -#define ASSERT(expr, ...) if (!(expr)) {FAIL(__VA_ARGS__); } - -void test_init(void); -void test_tick(bs_time_t HW_device_time); diff --git a/tests/bsim/bluetooth/host/l2cap/credits/src/main.c b/tests/bsim/bluetooth/host/l2cap/credits/src/main.c index f35abf350ac2..8e7df0f5ade5 100644 --- a/tests/bsim/bluetooth/host/l2cap/credits/src/main.c +++ b/tests/bsim/bluetooth/host/l2cap/credits/src/main.c @@ -6,15 +6,25 @@ * SPDX-License-Identifier: Apache-2.0 */ -#include "bstests.h" -#include "common.h" +#include + +#include +#include +#include + +#include +#include +#include + +#include "babblekit/testcase.h" +#include "babblekit/flags.h" #define LOG_MODULE_NAME main #include LOG_MODULE_REGISTER(LOG_MODULE_NAME, LOG_LEVEL_DBG); -CREATE_FLAG(is_connected); -CREATE_FLAG(flag_l2cap_connected); +static DEFINE_FLAG(is_connected); +static DEFINE_FLAG(flag_l2cap_connected); #define L2CAP_MPS CONFIG_BT_L2CAP_TX_MTU #define SDU_NUM 3 @@ -44,7 +54,7 @@ int l2cap_chan_send(struct bt_l2cap_chan *chan, uint8_t *data, size_t len) struct net_buf *buf = net_buf_alloc(&sdu_pool, K_NO_WAIT); if (buf == NULL) { - FAIL("No more memory\n"); + TEST_FAIL("No more memory"); return -ENOMEM; } @@ -53,7 +63,7 @@ int l2cap_chan_send(struct bt_l2cap_chan *chan, uint8_t *data, size_t len) int ret = bt_l2cap_chan_send(chan, buf); - ASSERT(ret >= 0, "Failed sending: err %d", ret); + TEST_ASSERT(ret >= 0, "Failed sending: err %d", ret); LOG_DBG("sent %d len %d", ret, len); return ret; @@ -94,7 +104,7 @@ int recv_cb(struct bt_l2cap_chan *chan, struct net_buf *buf) rx_cnt++; /* Verify SDU data matches TX'd data. */ - ASSERT(memcmp(buf->data, tx_data, buf->len) == 0, "RX data doesn't match TX"); + TEST_ASSERT(memcmp(buf->data, tx_data, buf->len) == 0, "RX data doesn't match TX"); /* Keep a ref for a few seconds: this will make the allocation fail, as * there is only 1 buffer in the pool. @@ -160,7 +170,7 @@ static int l2cap_server_register(bt_security_t sec_level) int err = bt_l2cap_server_register(&test_l2cap_server); - ASSERT(err == 0, "Failed to register l2cap server."); + TEST_ASSERT(err == 0, "Failed to register l2cap server."); return test_l2cap_server.psm; } @@ -172,7 +182,7 @@ static void connected(struct bt_conn *conn, uint8_t conn_err) bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr)); if (conn_err) { - FAIL("Failed to connect to %s (%u)", addr, conn_err); + TEST_FAIL("Failed to connect to %s (%u)", addr, conn_err); return; } @@ -204,7 +214,7 @@ static void disconnect_device(struct bt_conn *conn, void *data) SET_FLAG(is_connected); err = bt_conn_disconnect(conn, BT_HCI_ERR_REMOTE_USER_TERM_CONN); - ASSERT(!err, "Failed to initate disconnect (err %d)", err); + TEST_ASSERT(!err, "Failed to initate disconnect (err %d)", err); LOG_DBG("Waiting for disconnection..."); WAIT_FOR_FLAG_UNSET(is_connected); @@ -222,7 +232,7 @@ static void test_peripheral_main(void) err = bt_enable(NULL); if (err) { - FAIL("Can't enable Bluetooth (err %d)", err); + TEST_FAIL("Can't enable Bluetooth (err %d)", err); return; } @@ -230,13 +240,13 @@ static void test_peripheral_main(void) LOG_DBG("Connectable advertising..."); err = bt_le_adv_start(BT_LE_ADV_CONN_FAST_1, NULL, 0, NULL, 0); if (err) { - FAIL("Advertising failed to start (err %d)", err); + TEST_FAIL("Advertising failed to start (err %d)", err); return; } LOG_DBG("Advertising started."); LOG_DBG("Peripheral waiting for connection..."); - WAIT_FOR_FLAG_SET(is_connected); + WAIT_FOR_FLAG(is_connected); LOG_DBG("Peripheral Connected."); int psm = l2cap_server_register(BT_SECURITY_L1); @@ -261,9 +271,9 @@ static void test_peripheral_main(void) bt_conn_foreach(BT_CONN_TYPE_LE, disconnect_device, NULL); LOG_INF("Total received: %d", rx_cnt); - ASSERT(rx_cnt == SDU_NUM, "Did not receive expected no of SDUs\n"); + TEST_ASSERT(rx_cnt == SDU_NUM, "Did not receive expected no of SDUs\n"); - PASS("L2CAP CREDITS Peripheral passed\n"); + TEST_PASS("L2CAP CREDITS Peripheral passed"); } static void device_found(const bt_addr_le_t *addr, int8_t rssi, uint8_t type, @@ -275,7 +285,7 @@ static void device_found(const bt_addr_le_t *addr, int8_t rssi, uint8_t type, err = bt_le_scan_stop(); if (err) { - FAIL("Stop LE scan failed (err %d)", err); + TEST_FAIL("Stop LE scan failed (err %d)", err); return; } @@ -288,7 +298,7 @@ static void device_found(const bt_addr_le_t *addr, int8_t rssi, uint8_t type, param = BT_LE_CONN_PARAM_DEFAULT; err = bt_conn_le_create(addr, BT_CONN_LE_CREATE_CONN, param, &conn); if (err) { - FAIL("Create conn failed (err %d)", err); + TEST_FAIL("Create conn failed (err %d)", err); return; } } @@ -306,10 +316,10 @@ static void connect_peripheral(void) int err = bt_le_scan_start(&scan_param, device_found); - ASSERT(!err, "Scanning failed to start (err %d)\n", err); + TEST_ASSERT(!err, "Scanning failed to start (err %d)\n", err); LOG_DBG("Central initiating connection..."); - WAIT_FOR_FLAG_SET(is_connected); + WAIT_FOR_FLAG(is_connected); } static void connect_l2cap_channel(struct bt_conn *conn, void *data) @@ -323,9 +333,9 @@ static void connect_l2cap_channel(struct bt_conn *conn, void *data) UNSET_FLAG(flag_l2cap_connected); err = bt_l2cap_chan_connect(conn, &le_chan->chan, 0x0080); - ASSERT(!err, "Error connecting l2cap channel (err %d)\n", err); + TEST_ASSERT(!err, "Error connecting l2cap channel (err %d)\n", err); - WAIT_FOR_FLAG_SET(flag_l2cap_connected); + WAIT_FOR_FLAG(flag_l2cap_connected); } static void connect_l2cap_ecred_channel(struct bt_conn *conn, void *data) @@ -340,9 +350,9 @@ static void connect_l2cap_ecred_channel(struct bt_conn *conn, void *data) UNSET_FLAG(flag_l2cap_connected); err = bt_l2cap_ecred_chan_connect(conn, chan_list, 0x0080); - ASSERT(!err, "Error connecting l2cap channel (err %d)\n", err); + TEST_ASSERT(!err, "Error connecting l2cap channel (err %d)\n", err); - WAIT_FOR_FLAG_SET(flag_l2cap_connected); + WAIT_FOR_FLAG(flag_l2cap_connected); } static void test_central_main(void) @@ -356,7 +366,7 @@ static void test_central_main(void) } err = bt_enable(NULL); - ASSERT(err == 0, "Can't enable Bluetooth (err %d)\n", err); + TEST_ASSERT(err == 0, "Can't enable Bluetooth (err %d)\n", err); LOG_DBG("Central Bluetooth initialized."); connect_peripheral(); @@ -380,22 +390,18 @@ static void test_central_main(void) WAIT_FOR_FLAG_UNSET(is_connected); LOG_DBG("Peripheral disconnected."); - PASS("L2CAP CREDITS Central passed\n"); + TEST_PASS("L2CAP CREDITS Central passed"); } static const struct bst_test_instance test_def[] = { { .test_id = "peripheral", .test_descr = "Peripheral L2CAP CREDITS", - .test_pre_init_f = test_init, - .test_tick_f = test_tick, .test_main_f = test_peripheral_main }, { .test_id = "central", .test_descr = "Central L2CAP CREDITS", - .test_pre_init_f = test_init, - .test_tick_f = test_tick, .test_main_f = test_central_main }, BSTEST_END_MARKER diff --git a/tests/bsim/bluetooth/host/l2cap/credits_seg_recv/CMakeLists.txt b/tests/bsim/bluetooth/host/l2cap/credits_seg_recv/CMakeLists.txt index cf303ae2ef6e..40e8bd1e3679 100644 --- a/tests/bsim/bluetooth/host/l2cap/credits_seg_recv/CMakeLists.txt +++ b/tests/bsim/bluetooth/host/l2cap/credits_seg_recv/CMakeLists.txt @@ -5,8 +5,10 @@ cmake_minimum_required(VERSION 3.20.0) find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) project(bsim_test_l2cap_credits_seg_recv) +add_subdirectory(${ZEPHYR_BASE}/tests/bsim/babblekit babblekit) +target_link_libraries(app PRIVATE babblekit) + target_sources(app PRIVATE - src/common.c src/main.c ) diff --git a/tests/bsim/bluetooth/host/l2cap/credits_seg_recv/src/common.c b/tests/bsim/bluetooth/host/l2cap/credits_seg_recv/src/common.c deleted file mode 100644 index 6b607482012c..000000000000 --- a/tests/bsim/bluetooth/host/l2cap/credits_seg_recv/src/common.c +++ /dev/null @@ -1,22 +0,0 @@ -/* - * Copyright (c) 2023 Nordic Semiconductor ASA - * - * SPDX-License-Identifier: Apache-2.0 - */ - -#include "common.h" - -extern enum bst_result_t bst_result; - -void test_init(void) -{ - bst_ticker_set_next_tick_absolute(WAIT_TIME); - bst_result = In_progress; -} - -void test_tick(bs_time_t HW_device_time) -{ - if (bst_result != Passed) { - FAIL("test failed (not passed after %i seconds)\n", WAIT_SECONDS); - } -} diff --git a/tests/bsim/bluetooth/host/l2cap/credits_seg_recv/src/common.h b/tests/bsim/bluetooth/host/l2cap/credits_seg_recv/src/common.h deleted file mode 100644 index c0b7ae353b4c..000000000000 --- a/tests/bsim/bluetooth/host/l2cap/credits_seg_recv/src/common.h +++ /dev/null @@ -1,57 +0,0 @@ -/* - * Common functions and helpers for L2CAP tests - * - * Copyright (c) 2023 Nordic Semiconductor ASA - * - * SPDX-License-Identifier: Apache-2.0 - */ - -#include - -#include -#include -#include - -#include -#include -#include -#include "bs_types.h" -#include "bs_tracing.h" -#include "bstests.h" -#include "bs_pc_backchannel.h" - -extern enum bst_result_t bst_result; - -#define CREATE_FLAG(flag) static atomic_t flag = (atomic_t)false -#define SET_FLAG(flag) (void)atomic_set(&flag, (atomic_t)true) -#define UNSET_FLAG(flag) (void)atomic_set(&flag, (atomic_t)false) -#define TEST_FLAG(flag) (atomic_get(&flag) == (atomic_t)true) -#define WAIT_FOR_FLAG_SET(flag) \ - while (!(bool)atomic_get(&flag)) { \ - (void)k_sleep(K_MSEC(1)); \ - } -#define WAIT_FOR_FLAG_UNSET(flag) \ - while ((bool)atomic_get(&flag)) { \ - (void)k_sleep(K_MSEC(1)); \ - } - - -#define WAIT_SECONDS 30 /* seconds */ -#define WAIT_TIME (WAIT_SECONDS * USEC_PER_SEC) /* microseconds*/ - -#define FAIL(...) \ - do { \ - bst_result = Failed; \ - bs_trace_error_time_line(__VA_ARGS__); \ - } while (0) - -#define PASS(...) \ - do { \ - bst_result = Passed; \ - bs_trace_info_time(1, __VA_ARGS__); \ - } while (0) - -#define ASSERT(expr, ...) if (!(expr)) {FAIL(__VA_ARGS__); } - -void test_init(void); -void test_tick(bs_time_t HW_device_time); diff --git a/tests/bsim/bluetooth/host/l2cap/credits_seg_recv/src/main.c b/tests/bsim/bluetooth/host/l2cap/credits_seg_recv/src/main.c index 1ee69d76585b..d06188510c41 100644 --- a/tests/bsim/bluetooth/host/l2cap/credits_seg_recv/src/main.c +++ b/tests/bsim/bluetooth/host/l2cap/credits_seg_recv/src/main.c @@ -4,15 +4,25 @@ * SPDX-License-Identifier: Apache-2.0 */ -#include "bstests.h" -#include "common.h" +#include + +#include +#include +#include + +#include +#include +#include + +#include "babblekit/testcase.h" +#include "babblekit/flags.h" #define LOG_MODULE_NAME main #include LOG_MODULE_REGISTER(LOG_MODULE_NAME, LOG_LEVEL_DBG); -CREATE_FLAG(is_connected); -CREATE_FLAG(flag_l2cap_connected); +static DEFINE_FLAG(is_connected); +static DEFINE_FLAG(flag_l2cap_connected); #define L2CAP_MPS CONFIG_BT_L2CAP_TX_MTU #define SDU_NUM 3 @@ -44,7 +54,7 @@ int l2cap_chan_send(struct bt_l2cap_chan *chan, uint8_t *data, size_t len) buf = net_buf_alloc(&sdu_pool, K_NO_WAIT); if (buf == NULL) { - FAIL("No more memory\n"); + TEST_FAIL("No more memory"); return -ENOMEM; } @@ -53,7 +63,7 @@ int l2cap_chan_send(struct bt_l2cap_chan *chan, uint8_t *data, size_t len) ret = bt_l2cap_chan_send(chan, buf); - ASSERT(ret >= 0, "Failed sending: err %d", ret); + TEST_ASSERT(ret >= 0, "Failed sending: err %d", ret); LOG_DBG("sent %d len %d", ret, len); return ret; @@ -88,10 +98,11 @@ void recv_cb(struct bt_l2cap_chan *l2cap_chan, size_t sdu_len, off_t seg_offset, { LOG_DBG("sdu len %u frag offset %u frag len %u", sdu_len, seg_offset, seg->len); - ASSERT(sdu_len == sizeof(tx_data), "Recv SDU length does not match send length."); + TEST_ASSERT(sdu_len == sizeof(tx_data), "Recv SDU length does not match send length."); /* Verify SDU data matches TX'd data. */ - ASSERT(memcmp(seg->data, &tx_data[seg_offset], seg->len) == 0, "RX data doesn't match TX"); + TEST_ASSERT(memcmp(seg->data, &tx_data[seg_offset], seg->len) == 0, + "RX data doesn't match TX"); if (seg_offset + seg->len == sdu_len) { /* Don't give credits right away. The taker of this @@ -171,7 +182,7 @@ static int l2cap_server_register(bt_security_t sec_level) err = bt_l2cap_server_register(&test_l2cap_server); - ASSERT(err == 0, "Failed to register l2cap server."); + TEST_ASSERT(err == 0, "Failed to register l2cap server."); return test_l2cap_server.psm; } @@ -183,7 +194,7 @@ static void connected(struct bt_conn *conn, uint8_t conn_err) bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr)); if (conn_err) { - FAIL("Failed to connect to %s (%u)", addr, conn_err); + TEST_FAIL("Failed to connect to %s (%u)", addr, conn_err); return; } @@ -215,7 +226,7 @@ static void disconnect_device(struct bt_conn *conn, void *data) SET_FLAG(is_connected); err = bt_conn_disconnect(conn, BT_HCI_ERR_REMOTE_USER_TERM_CONN); - ASSERT(!err, "Failed to initate disconnect (err %d)", err); + TEST_ASSERT(!err, "Failed to initate disconnect (err %d)", err); LOG_DBG("Waiting for disconnection..."); WAIT_FOR_FLAG_UNSET(is_connected); @@ -235,7 +246,7 @@ static void test_peripheral_main(void) err = bt_enable(NULL); if (err) { - FAIL("Can't enable Bluetooth (err %d)", err); + TEST_FAIL("Can't enable Bluetooth (err %d)", err); return; } @@ -243,13 +254,13 @@ static void test_peripheral_main(void) LOG_DBG("Connectable advertising..."); err = bt_le_adv_start(BT_LE_ADV_CONN_FAST_1, NULL, 0, NULL, 0); if (err) { - FAIL("Advertising failed to start (err %d)", err); + TEST_FAIL("Advertising failed to start (err %d)", err); return; } LOG_DBG("Advertising started."); LOG_DBG("Peripheral waiting for connection..."); - WAIT_FOR_FLAG_SET(is_connected); + WAIT_FOR_FLAG(is_connected); LOG_DBG("Peripheral Connected."); psm = l2cap_server_register(BT_SECURITY_L1); @@ -274,9 +285,9 @@ static void test_peripheral_main(void) bt_conn_foreach(BT_CONN_TYPE_LE, disconnect_device, NULL); LOG_INF("Total received: %d", rx_cnt); - ASSERT(rx_cnt == SDU_NUM, "Did not receive expected no of SDUs\n"); + TEST_ASSERT(rx_cnt == SDU_NUM, "Did not receive expected no of SDUs"); - PASS("L2CAP CREDITS Peripheral passed\n"); + TEST_PASS("L2CAP CREDITS Peripheral passed"); } static void device_found(const bt_addr_le_t *addr, int8_t rssi, uint8_t type, @@ -289,7 +300,7 @@ static void device_found(const bt_addr_le_t *addr, int8_t rssi, uint8_t type, err = bt_le_scan_stop(); if (err) { - FAIL("Stop LE scan failed (err %d)", err); + TEST_FAIL("Stop LE scan failed (err %d)", err); return; } @@ -300,7 +311,7 @@ static void device_found(const bt_addr_le_t *addr, int8_t rssi, uint8_t type, param = BT_LE_CONN_PARAM_DEFAULT; err = bt_conn_le_create(addr, BT_CONN_LE_CREATE_CONN, param, &conn); if (err) { - FAIL("Create conn failed (err %d)", err); + TEST_FAIL("Create conn failed (err %d)", err); return; } } @@ -319,10 +330,10 @@ static void connect_peripheral(void) err = bt_le_scan_start(&scan_param, device_found); - ASSERT(!err, "Scanning failed to start (err %d)\n", err); + TEST_ASSERT(!err, "Scanning failed to start (err %d)", err); LOG_DBG("Central initiating connection..."); - WAIT_FOR_FLAG_SET(is_connected); + WAIT_FOR_FLAG(is_connected); } static void connect_l2cap_channel(struct bt_conn *conn, void *data) @@ -346,9 +357,9 @@ static void connect_l2cap_channel(struct bt_conn *conn, void *data) bt_l2cap_chan_give_credits(&le_chan->chan, 1); err = bt_l2cap_chan_connect(conn, &le_chan->chan, 0x0080); - ASSERT(!err, "Error connecting l2cap channel (err %d)\n", err); + TEST_ASSERT(!err, "Error connecting l2cap channel (err %d)", err); - WAIT_FOR_FLAG_SET(flag_l2cap_connected); + WAIT_FOR_FLAG(flag_l2cap_connected); } static void connect_l2cap_ecred_channel(struct bt_conn *conn, void *data) @@ -373,9 +384,9 @@ static void connect_l2cap_ecred_channel(struct bt_conn *conn, void *data) bt_l2cap_chan_give_credits(&le_chan->chan, 1); err = bt_l2cap_ecred_chan_connect(conn, chan_list, 0x0080); - ASSERT(!err, "Error connecting l2cap channel (err %d)\n", err); + TEST_ASSERT(!err, "Error connecting l2cap channel (err %d)", err); - WAIT_FOR_FLAG_SET(flag_l2cap_connected); + WAIT_FOR_FLAG(flag_l2cap_connected); } static void test_central_main(void) @@ -390,7 +401,7 @@ static void test_central_main(void) } err = bt_enable(NULL); - ASSERT(err == 0, "Can't enable Bluetooth (err %d)\n", err); + TEST_ASSERT(err == 0, "Can't enable Bluetooth (err %d)", err); LOG_DBG("Central Bluetooth initialized."); connect_peripheral(); @@ -414,19 +425,15 @@ static void test_central_main(void) WAIT_FOR_FLAG_UNSET(is_connected); LOG_DBG("Peripheral disconnected."); - PASS("L2CAP CREDITS Central passed\n"); + TEST_PASS("L2CAP CREDITS Central passed"); } static const struct bst_test_instance test_def[] = { {.test_id = "peripheral", .test_descr = "Peripheral L2CAP CREDITS", - .test_pre_init_f = test_init, - .test_tick_f = test_tick, .test_main_f = test_peripheral_main}, {.test_id = "central", .test_descr = "Central L2CAP CREDITS", - .test_pre_init_f = test_init, - .test_tick_f = test_tick, .test_main_f = test_central_main}, BSTEST_END_MARKER, }; diff --git a/tests/bsim/bluetooth/host/l2cap/general/CMakeLists.txt b/tests/bsim/bluetooth/host/l2cap/general/CMakeLists.txt index 6adc381f4f51..5ac53e80ae1a 100644 --- a/tests/bsim/bluetooth/host/l2cap/general/CMakeLists.txt +++ b/tests/bsim/bluetooth/host/l2cap/general/CMakeLists.txt @@ -5,6 +5,9 @@ cmake_minimum_required(VERSION 3.20.0) find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) project(bsim_test_l2cap) +add_subdirectory(${ZEPHYR_BASE}/tests/bsim/babblekit babblekit) +target_link_libraries(app PRIVATE babblekit) + FILE(GLOB app_sources src/*.c) target_sources(app PRIVATE ${app_sources} ) diff --git a/tests/bsim/bluetooth/host/l2cap/general/src/common.c b/tests/bsim/bluetooth/host/l2cap/general/src/common.c deleted file mode 100644 index 648b58f271b6..000000000000 --- a/tests/bsim/bluetooth/host/l2cap/general/src/common.c +++ /dev/null @@ -1,68 +0,0 @@ -/* - * Copyright (c) 2022 Nordic Semiconductor ASA - * - * SPDX-License-Identifier: Apache-2.0 - */ - -#include "common.h" - -#define LOG_MODULE_NAME common - -#include - -LOG_MODULE_REGISTER(LOG_MODULE_NAME, LOG_LEVEL_DBG); - -extern enum bst_result_t bst_result; - -void test_init(void) -{ - bst_result = In_progress; - bst_ticker_set_next_tick_absolute(WAIT_TIME); -} - -void test_tick(bs_time_t HW_device_time) -{ - if (bst_result != Passed) { - FAIL("test failed (not passed after %i us)\n", WAIT_TIME); - } -} - -/* Call in init functions*/ -void device_sync_init(uint device_nbr) -{ - uint peer = CENTRAL_ID; - - if (device_nbr == CENTRAL_ID) { - peer = PERIPHERAL_ID; - } - - uint dev_nbrs[BACK_CHANNELS] = {peer}; - uint channel_nbrs[BACK_CHANNELS] = {0}; - uint *ch = bs_open_back_channel(device_nbr, dev_nbrs, channel_nbrs, BACK_CHANNELS); - - if (!ch) { - LOG_ERR("bs_open_back_channel failed!"); - } -} - -/* Call it to make peer to proceed.*/ -void device_sync_send(void) -{ - uint8_t msg[1] = "S"; - - bs_bc_send_msg(0, msg, sizeof(msg)); -} - -/* Wait until peer send sync*/ -void device_sync_wait(void) -{ - int size_msg_received = 0; - uint8_t msg; - - while (!size_msg_received) { - size_msg_received = bs_bc_is_msg_received(0); - k_sleep(K_MSEC(1)); - } - - bs_bc_receive_msg(0, &msg, size_msg_received); -} diff --git a/tests/bsim/bluetooth/host/l2cap/general/src/common.h b/tests/bsim/bluetooth/host/l2cap/general/src/common.h deleted file mode 100644 index a399786bfd21..000000000000 --- a/tests/bsim/bluetooth/host/l2cap/general/src/common.h +++ /dev/null @@ -1,59 +0,0 @@ -/* - * Common functions and helpers for L2CAP tests - * - * Copyright (c) 2022 Nordic Semiconductor ASA - * - * SPDX-License-Identifier: Apache-2.0 - */ - -#include - -#include -#include -#include - -#include -#include -#include -#include "bs_types.h" -#include "bs_tracing.h" -#include "bstests.h" -#include "bs_pc_backchannel.h" - -#define CREATE_FLAG(flag) static atomic_t flag = (atomic_t)false -#define SET_FLAG(flag) (void)atomic_set(&flag, (atomic_t)true) -#define UNSET_FLAG(flag) (void)atomic_set(&flag, (atomic_t)false) -#define TEST_FLAG(flag) (atomic_get(&flag) == (atomic_t)true) -#define WAIT_FOR_FLAG_SET(flag) \ - while (!(bool)atomic_get(&flag)) { \ - (void)k_sleep(K_MSEC(1)); \ - } -#define WAIT_FOR_FLAG_UNSET(flag) \ - while ((bool)atomic_get(&flag)) { \ - (void)k_sleep(K_MSEC(1)); \ - } - - -#define WAIT_TIME (60e6) /* 60 seconds*/ - -#define FAIL(...) \ - do { \ - bst_result = Failed; \ - bs_trace_error_time_line(__VA_ARGS__); \ - } while (0) - -#define PASS(...) \ - do { \ - bst_result = Passed; \ - bs_trace_info_time(1, __VA_ARGS__); \ - } while (0) - -#define CENTRAL_ID 0 -#define PERIPHERAL_ID 1 -#define BACK_CHANNELS 1 - -void test_init(void); -void test_tick(bs_time_t HW_device_time); -void device_sync_init(uint device_nbr); -void device_sync_send(void); -void device_sync_wait(void); diff --git a/tests/bsim/bluetooth/host/l2cap/general/src/main_l2cap_ecred.c b/tests/bsim/bluetooth/host/l2cap/general/src/main_l2cap_ecred.c index 06135e030a52..4cd2f619ff7b 100644 --- a/tests/bsim/bluetooth/host/l2cap/general/src/main_l2cap_ecred.c +++ b/tests/bsim/bluetooth/host/l2cap/general/src/main_l2cap_ecred.c @@ -6,7 +6,19 @@ * SPDX-License-Identifier: Apache-2.0 */ -#include "common.h" +#include + +#include +#include +#include + +#include +#include +#include + +#include "babblekit/testcase.h" +#include "babblekit/flags.h" +#include "babblekit/sync.h" #define LOG_MODULE_NAME main_l2cap_ecred #include @@ -54,8 +66,8 @@ struct channel { }; static struct channel channels[L2CAP_CHANNELS]; -CREATE_FLAG(is_connected); -CREATE_FLAG(unsequenced_data); +static DEFINE_FLAG(is_connected); +static DEFINE_FLAG(unsequenced_data); #define T_STACK_SIZE 512 #define T_PRIORITY 5 @@ -90,22 +102,23 @@ static int chan_recv_cb(struct bt_l2cap_chan *l2cap_chan, struct net_buf *buf) LOG_DBG("received_iterration %i sdus_received %i, chan_id: %d, data_length: %d", received_iterration, chan->sdus_received, chan->chan_id, buf->len); - if (!TEST_FLAG(unsequenced_data) && received_iterration != chan->sdus_received) { - FAIL("Received out of sequence data."); + if (!IS_FLAG_SET(unsequenced_data) && received_iterration != chan->sdus_received) { + TEST_FAIL("Received out of sequence data."); } const int retval = memcmp(buf->data + sizeof(received_iterration), chan->payload + sizeof(received_iterration), buf->len - sizeof(received_iterration)); if (retval) { - FAIL("Payload received didn't match expected value memcmp returned %i", retval); + TEST_FAIL("Payload received didn't match expected value memcmp returned %i", + retval); } /*By the time we rx on long msg channel we should have already rx on short msg channel*/ if (chan->chan_id == 0) { if (channels[SHORT_MSG_CHAN_IDX].sdus_received != (channels[LONG_MSG_CHAN_IDX].sdus_received + 1)) { - FAIL("Didn't receive on short msg channel first"); + TEST_FAIL("Didn't receive on short msg channel first"); } } @@ -232,7 +245,7 @@ static void connect_num_channels(uint8_t num_l2cap_channels) struct channel *chan = get_free_channel(); if (!chan) { - FAIL("failed, chan not free"); + TEST_FAIL("failed, chan not free"); return; } @@ -243,7 +256,7 @@ static void connect_num_channels(uint8_t num_l2cap_channels) servers[0].psm); if (err) { - FAIL("can't connect ecred %d ", err); + TEST_FAIL("can't connect ecred %d ", err); } } @@ -297,7 +310,7 @@ static void register_l2cap_server(void) server = get_free_server(); if (!server) { - FAIL("Failed to get free server"); + TEST_FAIL("Failed to get free server"); return; } @@ -305,7 +318,7 @@ static void register_l2cap_server(void) server->psm = 0; if (bt_l2cap_server_register(server) < 0) { - FAIL("Failed to get free server"); + TEST_FAIL("Failed to get free server"); return; } @@ -319,7 +332,7 @@ static void connected(struct bt_conn *conn, uint8_t conn_err) bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr)); if (conn_err) { - FAIL("Failed to connect to %s (%u)", addr, conn_err); + TEST_FAIL("Failed to connect to %s (%u)", addr, conn_err); bt_conn_unref(default_conn); default_conn = NULL; return; @@ -340,7 +353,7 @@ static void disconnected(struct bt_conn *conn, uint8_t reason) LOG_DBG("%s (reason 0x%02x)", addr, reason); if (default_conn != conn) { - FAIL("Conn mismatch disconnect %s %s)", default_conn, conn); + TEST_FAIL("Conn mismatch disconnect %s %s)", default_conn, conn); return; } @@ -363,7 +376,7 @@ static void send_sdu(int iteration, int chan_idx, int bytes) sys_put_le32(iteration, channels[chan_idx].payload); if (channels[chan_idx].buf != 0) { - FAIL("Buf should have been deallocated by now"); + TEST_FAIL("Buf should have been deallocated by now"); return; } @@ -374,7 +387,7 @@ static void send_sdu(int iteration, int chan_idx, int bytes) } if (buf == NULL) { - FAIL("Failed to get buff on ch %i, iteration %i should never happen", chan_idx, + TEST_FAIL("Failed to get buff on ch %i, iteration %i should never happen", chan_idx, chan_idx); } @@ -388,7 +401,7 @@ static void send_sdu(int iteration, int chan_idx, int bytes) LOG_DBG("bt_l2cap_chan_send returned: %i", ret); if (ret < 0) { - FAIL("Error: send failed error: %i", ret); + TEST_FAIL("Error: send failed error: %i", ret); channels[chan_idx].buf = 0; net_buf_unref(buf); } @@ -412,7 +425,7 @@ static void send_sdu_concurrently(void) &channels[k].work); if (err < 0) { - FAIL("Failed to submit work to the queue, error: %d", err); + TEST_FAIL("Failed to submit work to the queue, error: %d", err); } } @@ -436,14 +449,14 @@ static int change_mtu_on_channels(int num_channels, int new_mtu) static void test_peripheral_main(void) { - device_sync_init(PERIPHERAL_ID); + TEST_ASSERT(bk_sync_init() == 0, "Failed to open backchannel"); LOG_DBG("*L2CAP ECRED Peripheral started*"); init_workqs(); int err; err = bt_enable(NULL); if (err) { - FAIL("Can't enable Bluetooth (err %d)", err); + TEST_FAIL("Can't enable Bluetooth (err %d)", err); return; } @@ -451,13 +464,13 @@ static void test_peripheral_main(void) LOG_DBG("Connectable advertising..."); err = bt_le_adv_start(BT_LE_ADV_CONN_FAST_1, ad, ARRAY_SIZE(ad), NULL, 0); if (err) { - FAIL("Advertising failed to start (err %d)", err); + TEST_FAIL("Advertising failed to start (err %d)", err); return; } LOG_DBG("Advertising started."); LOG_DBG("Peripheral waiting for connection..."); - WAIT_FOR_FLAG_SET(is_connected); + WAIT_FOR_FLAG(is_connected); LOG_DBG("Peripheral Connected."); register_l2cap_server(); connect_num_channels(L2CAP_CHANNELS); @@ -472,7 +485,7 @@ static void test_peripheral_main(void) k_sem_take(&all_chan_conn_sem, K_FOREVER); LOG_DBG("Send sync after reconnection"); - device_sync_send(); + bk_sync_send(); /* Send bytes on both channels and expect ch 1 to receive all of them before ch 0 *********/ LOG_DBG("############# Send bytes on both channels concurrently"); @@ -483,20 +496,20 @@ static void test_peripheral_main(void) err = change_mtu_on_channels(L2CAP_CHANNELS, CONFIG_BT_L2CAP_TX_MTU + 10); if (err) { - FAIL("MTU change failed (err %d)\n", err); + TEST_FAIL("MTU change failed (err %d)", err); } /* Read from both devices (Central and Peripheral) at the same time **********************/ LOG_DBG("############# Read from both devices (Central and Peripheral) at the same time"); LOG_DBG("Wait for sync before sending the msg"); - device_sync_wait(); + bk_sync_wait(); LOG_DBG("Received sync"); send_sdu(0, 1, 10); k_sem_take(&sent_sem, K_FOREVER); disconnect_all_channels(); WAIT_FOR_FLAG_UNSET(is_connected); - PASS("L2CAP ECRED Peripheral tests Passed"); + TEST_PASS("L2CAP ECRED Peripheral tests Passed"); bs_trace_silent_exit(0); } @@ -508,14 +521,14 @@ static void device_found(const bt_addr_le_t *addr, int8_t rssi, uint8_t type, err = bt_le_scan_stop(); if (err) { - FAIL("Stop LE scan failed (err %d)", err); + TEST_FAIL("Stop LE scan failed (err %d)", err); return; } param = BT_LE_CONN_PARAM_DEFAULT; err = bt_conn_le_create(addr, BT_CONN_LE_CREATE_CONN, param, &default_conn); if (err) { - FAIL("Create conn failed (err %d)", err); + TEST_FAIL("Create conn failed (err %d)", err); return; } } @@ -529,40 +542,40 @@ static void test_central_main(void) .window = BT_GAP_SCAN_FAST_WINDOW, }; - device_sync_init(CENTRAL_ID); + TEST_ASSERT(bk_sync_init() == 0, "Failed to open backchannel"); LOG_DBG("*L2CAP ECRED Central started*"); int err; err = bt_enable(NULL); if (err) { - FAIL("Can't enable Bluetooth (err %d)\n", err); + TEST_FAIL("Can't enable Bluetooth (err %d)", err); return; } LOG_DBG("Central Bluetooth initialized.\n"); err = bt_le_scan_start(&scan_param, device_found); if (err) { - FAIL("Scanning failed to start (err %d)\n", err); + TEST_FAIL("Scanning failed to start (err %d)", err); return; } LOG_DBG("Scanning successfully started\n"); LOG_DBG("Central waiting for connection...\n"); - WAIT_FOR_FLAG_SET(is_connected); + WAIT_FOR_FLAG(is_connected); LOG_DBG("Central Connected.\n"); register_l2cap_server(); LOG_DBG("Wait for sync after reconnection"); - device_sync_wait(); + bk_sync_wait(); LOG_DBG("Received sync"); /* Read from both devices (Central and Peripheral) at the same time **********************/ LOG_DBG("############# Read from both devices (Central and Peripheral) at the same time"); LOG_DBG("Send sync for SDU send"); SET_FLAG(unsequenced_data); - device_sync_send(); + bk_sync_send(); send_sdu(0, 1, 10); /* Wait until all of the channels are disconnected */ @@ -577,7 +590,7 @@ static void test_central_main(void) if (channels[LONG_MSG_CHAN_IDX].sdus_received < SDU_SEND_COUNT || channels[SHORT_MSG_CHAN_IDX].sdus_received < SDU_SEND_COUNT) { - FAIL("received less than %i", SDU_SEND_COUNT); + TEST_FAIL("received less than %i", SDU_SEND_COUNT); } /* Disconnect */ @@ -587,28 +600,24 @@ static void test_central_main(void) LOG_DBG("Central tried to disconnect"); if (err) { - FAIL("Disconnection failed (err %d)", err); + TEST_FAIL("Disconnection failed (err %d)", err); return; } LOG_DBG("Central Disconnected."); - PASS("L2CAP ECRED Central tests Passed\n"); + TEST_PASS("L2CAP ECRED Central tests Passed"); } static const struct bst_test_instance test_def[] = { { .test_id = "peripheral", .test_descr = "Peripheral L2CAP ECRED", - .test_pre_init_f = test_init, - .test_tick_f = test_tick, .test_main_f = test_peripheral_main }, { .test_id = "central", .test_descr = "Central L2CAP ECRED", - .test_pre_init_f = test_init, - .test_tick_f = test_tick, .test_main_f = test_central_main }, BSTEST_END_MARKER diff --git a/tests/bsim/bluetooth/host/l2cap/many_conns/CMakeLists.txt b/tests/bsim/bluetooth/host/l2cap/many_conns/CMakeLists.txt index 88c326dddf3d..383f54c095cf 100644 --- a/tests/bsim/bluetooth/host/l2cap/many_conns/CMakeLists.txt +++ b/tests/bsim/bluetooth/host/l2cap/many_conns/CMakeLists.txt @@ -5,6 +5,9 @@ cmake_minimum_required(VERSION 3.20.0) find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) project(bsim_test_l2cap_stress) +add_subdirectory(${ZEPHYR_BASE}/tests/bsim/babblekit babblekit) +target_link_libraries(app PRIVATE babblekit) + FILE(GLOB app_sources src/*.c) target_sources(app PRIVATE ${app_sources} ) diff --git a/tests/bsim/bluetooth/host/l2cap/many_conns/src/common.c b/tests/bsim/bluetooth/host/l2cap/many_conns/src/common.c deleted file mode 100644 index c6679d83494e..000000000000 --- a/tests/bsim/bluetooth/host/l2cap/many_conns/src/common.c +++ /dev/null @@ -1,22 +0,0 @@ -/* - * Copyright (c) 2024 Nordic Semiconductor ASA - * - * SPDX-License-Identifier: Apache-2.0 - */ - -#include "common.h" - -extern enum bst_result_t bst_result; - -void test_init(void) -{ - bst_ticker_set_next_tick_absolute(WAIT_TIME); - bst_result = In_progress; -} - -void test_tick(bs_time_t HW_device_time) -{ - if (bst_result != Passed) { - FAIL("test failed (not passed after %i seconds)\n", WAIT_SECONDS); - } -} diff --git a/tests/bsim/bluetooth/host/l2cap/many_conns/src/common.h b/tests/bsim/bluetooth/host/l2cap/many_conns/src/common.h deleted file mode 100644 index 2ce0288975d3..000000000000 --- a/tests/bsim/bluetooth/host/l2cap/many_conns/src/common.h +++ /dev/null @@ -1,57 +0,0 @@ -/* - * Common functions and helpers for L2CAP tests - * - * Copyright (c) 2024 Nordic Semiconductor ASA - * - * SPDX-License-Identifier: Apache-2.0 - */ - -#include - -#include -#include -#include - -#include -#include -#include -#include "bs_types.h" -#include "bs_tracing.h" -#include "bstests.h" -#include "bs_pc_backchannel.h" - -extern enum bst_result_t bst_result; - -#define CREATE_FLAG(flag) static atomic_t flag = (atomic_t)false -#define SET_FLAG(flag) (void)atomic_set(&flag, (atomic_t)true) -#define UNSET_FLAG(flag) (void)atomic_set(&flag, (atomic_t)false) -#define TEST_FLAG(flag) (atomic_get(&flag) == (atomic_t)true) -#define WAIT_FOR_FLAG_SET(flag) \ - while (!(bool)atomic_get(&flag)) { \ - (void)k_sleep(K_MSEC(1)); \ - } -#define WAIT_FOR_FLAG_UNSET(flag) \ - while ((bool)atomic_get(&flag)) { \ - (void)k_sleep(K_MSEC(1)); \ - } - - -#define WAIT_SECONDS 400 /* seconds */ -#define WAIT_TIME (WAIT_SECONDS * USEC_PER_SEC) /* microseconds*/ - -#define FAIL(...) \ - do { \ - bst_result = Failed; \ - bs_trace_error_time_line(__VA_ARGS__); \ - } while (0) - -#define PASS(...) \ - do { \ - bst_result = Passed; \ - bs_trace_info_time(1, __VA_ARGS__); \ - } while (0) - -#define ASSERT(expr, ...) if (!(expr)) {FAIL(__VA_ARGS__); } - -void test_init(void); -void test_tick(bs_time_t HW_device_time); diff --git a/tests/bsim/bluetooth/host/l2cap/many_conns/src/main.c b/tests/bsim/bluetooth/host/l2cap/many_conns/src/main.c index 04825306285a..174d3739086b 100644 --- a/tests/bsim/bluetooth/host/l2cap/many_conns/src/main.c +++ b/tests/bsim/bluetooth/host/l2cap/many_conns/src/main.c @@ -6,15 +6,25 @@ * SPDX-License-Identifier: Apache-2.0 */ -#include "bstests.h" -#include "common.h" +#include + +#include +#include +#include + +#include +#include +#include + +#include "babblekit/testcase.h" +#include "babblekit/flags.h" #define LOG_MODULE_NAME main #include LOG_MODULE_REGISTER(LOG_MODULE_NAME, LOG_LEVEL_INF); -CREATE_FLAG(is_connected); -CREATE_FLAG(flag_l2cap_connected); +static DEFINE_FLAG(is_connected); +static DEFINE_FLAG(flag_l2cap_connected); #define NUM_PERIPHERALS CONFIG_BT_MAX_CONN #define L2CAP_CHANS NUM_PERIPHERALS @@ -47,7 +57,7 @@ struct test_ctx *get_ctx(struct bt_l2cap_chan *chan) struct bt_l2cap_le_chan *le_chan = CONTAINER_OF(chan, struct bt_l2cap_le_chan, chan); struct test_ctx *ctx = CONTAINER_OF(le_chan, struct test_ctx, le_chan); - ASSERT(ctx >= &contexts[0] && + TEST_ASSERT(ctx >= &contexts[0] && ctx <= &contexts[L2CAP_CHANS], "memory corruption"); return ctx; @@ -59,14 +69,14 @@ int l2cap_chan_send(struct bt_l2cap_chan *chan, uint8_t *data, size_t len) struct net_buf *buf = net_buf_alloc(&sdu_tx_pool, K_NO_WAIT); - ASSERT(buf, "No more memory\n"); + TEST_ASSERT(buf, "No more memory"); net_buf_reserve(buf, BT_L2CAP_SDU_CHAN_SEND_RESERVE); net_buf_add_mem(buf, data, len); int ret = bt_l2cap_chan_send(chan, buf); - ASSERT(ret >= 0, "Failed sending: err %d", ret); + TEST_ASSERT(ret >= 0, "Failed sending: err %d", ret); LOG_DBG("sent %d len %d", ret, len); return ret; @@ -187,7 +197,7 @@ static int l2cap_server_register(bt_security_t sec_level) int err = bt_l2cap_server_register(&test_l2cap_server); - ASSERT(err == 0, "Failed to register l2cap server."); + TEST_ASSERT(err == 0, "Failed to register l2cap server."); return test_l2cap_server.psm; } @@ -198,7 +208,7 @@ static void connected(struct bt_conn *conn, uint8_t conn_err) bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr)); - ASSERT(!conn_err, "Failed to connect to %s (%u)", addr, conn_err); + TEST_ASSERT(!conn_err, "Failed to connect to %s (%u)", addr, conn_err); LOG_DBG("%s", addr); @@ -229,7 +239,7 @@ static void disconnect_device(struct bt_conn *conn, void *data) SET_FLAG(is_connected); err = bt_conn_disconnect(conn, BT_HCI_ERR_REMOTE_USER_TERM_CONN); - ASSERT(!err, "Failed to initate disconnect (err %d)", err); + TEST_ASSERT(!err, "Failed to initate disconnect (err %d)", err); LOG_DBG("Waiting for disconnection..."); WAIT_FOR_FLAG_UNSET(is_connected); @@ -246,16 +256,16 @@ static void test_peripheral_main(void) } err = bt_enable(NULL); - ASSERT(!err, "Can't enable Bluetooth (err %d)", err); + TEST_ASSERT(!err, "Can't enable Bluetooth (err %d)", err); LOG_DBG("Peripheral Bluetooth initialized."); LOG_DBG("Connectable advertising..."); err = bt_le_adv_start(BT_LE_ADV_CONN_FAST_1, NULL, 0, NULL, 0); - ASSERT(!err, "Advertising failed to start (err %d)", err); + TEST_ASSERT(!err, "Advertising failed to start (err %d)", err); LOG_DBG("Advertising started."); LOG_DBG("Peripheral waiting for connection..."); - WAIT_FOR_FLAG_SET(is_connected); + WAIT_FOR_FLAG(is_connected); LOG_DBG("Peripheral Connected."); int psm = l2cap_server_register(BT_SECURITY_L1); @@ -271,9 +281,9 @@ static void test_peripheral_main(void) WAIT_FOR_FLAG_UNSET(is_connected); LOG_INF("Total received: %d", rx_cnt); - ASSERT(rx_cnt == SDU_NUM, "Did not receive expected no of SDUs\n"); + TEST_ASSERT(rx_cnt == SDU_NUM, "Did not receive expected no of SDUs"); - PASS("L2CAP LATENCY Peripheral passed\n"); + TEST_PASS("L2CAP LATENCY Peripheral passed"); } static void device_found(const bt_addr_le_t *addr, int8_t rssi, uint8_t type, @@ -284,7 +294,7 @@ static void device_found(const bt_addr_le_t *addr, int8_t rssi, uint8_t type, int err; err = bt_le_scan_stop(); - ASSERT(!err, "Stop LE scan failed (err %d)", err); + TEST_ASSERT(!err, "Stop LE scan failed (err %d)", err); char str[BT_ADDR_LE_STR_LEN]; @@ -294,7 +304,7 @@ static void device_found(const bt_addr_le_t *addr, int8_t rssi, uint8_t type, param = BT_LE_CONN_PARAM_DEFAULT; err = bt_conn_le_create(addr, BT_CONN_LE_CREATE_CONN, param, &conn); - ASSERT(!err, "Create conn failed (err %d)", err); + TEST_ASSERT(!err, "Create conn failed (err %d)", err); } static void connect_peripheral(void) @@ -310,10 +320,10 @@ static void connect_peripheral(void) int err = bt_le_scan_start(&scan_param, device_found); - ASSERT(!err, "Scanning failed to start (err %d)\n", err); + TEST_ASSERT(!err, "Scanning failed to start (err %d)", err); LOG_DBG("Central initiating connection..."); - WAIT_FOR_FLAG_SET(is_connected); + WAIT_FOR_FLAG(is_connected); } static void connect_l2cap_channel(struct bt_conn *conn, void *data) @@ -321,7 +331,7 @@ static void connect_l2cap_channel(struct bt_conn *conn, void *data) int err; struct test_ctx *ctx = alloc_test_context(); - ASSERT(ctx, "No more available test contexts\n"); + TEST_ASSERT(ctx, "No more available test contexts"); struct bt_l2cap_le_chan *le_chan = &ctx->le_chan; @@ -330,9 +340,9 @@ static void connect_l2cap_channel(struct bt_conn *conn, void *data) UNSET_FLAG(flag_l2cap_connected); err = bt_l2cap_chan_connect(conn, &le_chan->chan, 0x0080); - ASSERT(!err, "Error connecting l2cap channel (err %d)\n", err); + TEST_ASSERT(!err, "Error connecting l2cap channel (err %d)", err); - WAIT_FOR_FLAG_SET(flag_l2cap_connected); + WAIT_FOR_FLAG(flag_l2cap_connected); } #define L2CAP_LE_CID_DYN_START 0x0040 @@ -363,8 +373,7 @@ void bt_test_l2cap_data_pull_spy(struct bt_conn *conn, return; } - ASSERT(uptime == last_pull_time, - "Too much delay servicing ready channels\n"); + TEST_ASSERT(uptime == last_pull_time, "Too much delay servicing ready channels"); } static void test_central_main(void) @@ -378,7 +387,7 @@ static void test_central_main(void) } err = bt_enable(NULL); - ASSERT(err == 0, "Can't enable Bluetooth (err %d)\n", err); + TEST_ASSERT(err == 0, "Can't enable Bluetooth (err %d)", err); LOG_DBG("Central Bluetooth initialized."); /* Connect all peripherals */ @@ -407,7 +416,7 @@ static void test_central_main(void) LOG_DBG("Wait until all transfers are completed."); int remaining_tx_total; - /* Assertion that the `pull` callback gets serviced for all connections + /* TEST_Assertion that the `pull` callback gets serviced for all connections * at the same time is handled in bt_l2cap_data_pull_spy(). */ @@ -426,22 +435,18 @@ static void test_central_main(void) } LOG_DBG("All peripherals disconnected."); - PASS("L2CAP LATENCY Central passed\n"); + TEST_PASS("L2CAP LATENCY Central passed"); } static const struct bst_test_instance test_def[] = { { .test_id = "peripheral", .test_descr = "Peripheral L2CAP LATENCY", - .test_pre_init_f = test_init, - .test_tick_f = test_tick, .test_main_f = test_peripheral_main }, { .test_id = "central", .test_descr = "Central L2CAP LATENCY", - .test_pre_init_f = test_init, - .test_tick_f = test_tick, .test_main_f = test_central_main }, BSTEST_END_MARKER diff --git a/tests/bsim/bluetooth/host/l2cap/send_on_connect/CMakeLists.txt b/tests/bsim/bluetooth/host/l2cap/send_on_connect/CMakeLists.txt index f6db9d424e24..5a5dd7ab174e 100644 --- a/tests/bsim/bluetooth/host/l2cap/send_on_connect/CMakeLists.txt +++ b/tests/bsim/bluetooth/host/l2cap/send_on_connect/CMakeLists.txt @@ -5,8 +5,10 @@ cmake_minimum_required(VERSION 3.20.0) find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) project(bsim_test_l2cap_send_on_connect) +add_subdirectory(${ZEPHYR_BASE}/tests/bsim/babblekit babblekit) +target_link_libraries(app PRIVATE babblekit) + target_sources(app PRIVATE - src/common.c src/main_l2cap_send_on_connect.c src/main.c ) diff --git a/tests/bsim/bluetooth/host/l2cap/send_on_connect/src/common.c b/tests/bsim/bluetooth/host/l2cap/send_on_connect/src/common.c deleted file mode 100644 index f725ac6129c3..000000000000 --- a/tests/bsim/bluetooth/host/l2cap/send_on_connect/src/common.c +++ /dev/null @@ -1,22 +0,0 @@ -/* - * Copyright (c) 2022 Nordic Semiconductor ASA - * - * SPDX-License-Identifier: Apache-2.0 - */ - -#include "common.h" - -extern enum bst_result_t bst_result; - -void test_init(void) -{ - bst_result = In_progress; - bst_ticker_set_next_tick_absolute(WAIT_TIME); -} - -void test_tick(bs_time_t HW_device_time) -{ - if (bst_result != Passed) { - FAIL("test failed (not passed after %i us)\n", WAIT_TIME); - } -} diff --git a/tests/bsim/bluetooth/host/l2cap/send_on_connect/src/common.h b/tests/bsim/bluetooth/host/l2cap/send_on_connect/src/common.h deleted file mode 100644 index 3b0d09c53b6c..000000000000 --- a/tests/bsim/bluetooth/host/l2cap/send_on_connect/src/common.h +++ /dev/null @@ -1,44 +0,0 @@ -/* - * Common functions and helpers for L2CAP tests - * - * Copyright (c) 2022 Nordic Semiconductor ASA - * - * SPDX-License-Identifier: Apache-2.0 - */ - -#include - -#include "bs_types.h" -#include "bs_tracing.h" -#include "bstests.h" - -#define CREATE_FLAG(flag) static atomic_t flag = (atomic_t)false -#define SET_FLAG(flag) (void)atomic_set(&flag, (atomic_t)true) -#define UNSET_FLAG(flag) (void)atomic_set(&flag, (atomic_t)false) -#define WAIT_FOR_FLAG_SET(flag) \ - while (!(bool)atomic_get(&flag)) { \ - (void)k_sleep(K_MSEC(1)); \ - } -#define WAIT_FOR_FLAG_UNSET(flag) \ - while ((bool)atomic_get(&flag)) { \ - (void)k_sleep(K_MSEC(1)); \ - } - - -#define WAIT_TIME (30e6) /* 30 seconds*/ - -#define FAIL(...) \ - do { \ - bst_result = Failed; \ - bs_trace_error_time_line(__VA_ARGS__); \ - } while (0) - -#define PASS(...) \ - do { \ - bst_result = Passed; \ - bs_trace_info_time(1, __VA_ARGS__); \ - } while (0) - - -void test_init(void); -void test_tick(bs_time_t HW_device_time); diff --git a/tests/bsim/bluetooth/host/l2cap/send_on_connect/src/main_l2cap_send_on_connect.c b/tests/bsim/bluetooth/host/l2cap/send_on_connect/src/main_l2cap_send_on_connect.c index 9fd1c4bfcb86..825a8a74a2e3 100644 --- a/tests/bsim/bluetooth/host/l2cap/send_on_connect/src/main_l2cap_send_on_connect.c +++ b/tests/bsim/bluetooth/host/l2cap/send_on_connect/src/main_l2cap_send_on_connect.c @@ -4,20 +4,22 @@ * SPDX-License-Identifier: Apache-2.0 */ -#include "common.h" - +#include #include #include +#include "babblekit/testcase.h" +#include "babblekit/flags.h" + extern enum bst_result_t bst_result; static struct bt_conn *default_conn; #define PSM 0x80 -CREATE_FLAG(is_connected); -CREATE_FLAG(chan_connected); -CREATE_FLAG(data_received); +static DEFINE_FLAG(is_connected); +static DEFINE_FLAG(chan_connected); +static DEFINE_FLAG(data_received); #define DATA_BYTE_VAL 0xBB @@ -32,7 +34,7 @@ static void chan_connected_cb(struct bt_l2cap_chan *l2cap_chan) /* Send data immediately on L2CAP connection */ buf = net_buf_alloc(&buf_pool, K_NO_WAIT); if (!buf) { - FAIL("Buffer allocation failed\n"); + TEST_FAIL("Buffer allocation failed"); } (void)net_buf_reserve(buf, BT_L2CAP_SDU_CHAN_SEND_RESERVE); @@ -41,7 +43,7 @@ static void chan_connected_cb(struct bt_l2cap_chan *l2cap_chan) /* Try to send data */ err = bt_l2cap_chan_send(l2cap_chan, buf); if (err < 0) { - FAIL("Could not send data, error %d\n", err); + TEST_FAIL("Could not send data, error %d", err); } SET_FLAG(chan_connected); @@ -59,7 +61,7 @@ static int chan_recv_cb(struct bt_l2cap_chan *chan, struct net_buf *buf) (void)chan; if ((buf->len != 1) || (buf->data[0] != DATA_BYTE_VAL)) { - FAIL("Unexpected data received"); + TEST_FAIL("Unexpected data received"); } SET_FLAG(data_received); @@ -100,12 +102,12 @@ static void connect_l2cap_channel(void) if (IS_ENABLED(CONFIG_BT_L2CAP_ECRED)) { err = bt_l2cap_ecred_chan_connect(default_conn, chans, server.psm); if (err) { - FAIL("Failed to send ecred connection request (err %d)\n", err); + TEST_FAIL("Failed to send ecred connection request (err %d)", err); } } else { err = bt_l2cap_chan_connect(default_conn, &channel.chan, server.psm); if (err) { - FAIL("Failed to send connection request (err %d)\n", err); + TEST_FAIL("Failed to send connection request (err %d)", err); } } } @@ -116,7 +118,7 @@ static void register_l2cap_server(void) err = bt_l2cap_server_register(&server); if (err < 0) { - FAIL("Failed to get free server (err %d)\n"); + TEST_FAIL("Failed to get free server (err %d)", err); return; } } @@ -124,7 +126,7 @@ static void register_l2cap_server(void) static void connected(struct bt_conn *conn, uint8_t err) { if (err) { - FAIL("Failed to connect (err %d)\n", err); + TEST_FAIL("Failed to connect (err %d)", err); bt_conn_unref(default_conn); default_conn = NULL; return; @@ -138,7 +140,7 @@ static void connected(struct bt_conn *conn, uint8_t err) static void disconnected(struct bt_conn *conn, uint8_t reason) { if (default_conn != conn) { - FAIL("Connection mismatch %p %p)\n", default_conn, conn); + TEST_FAIL("Connection mismatch %p %p)", default_conn, conn); return; } @@ -160,14 +162,14 @@ static void device_found(const bt_addr_le_t *addr, int8_t rssi, uint8_t type, err = bt_le_scan_stop(); if (err) { - FAIL("Failed to stop scanning (err %d)\n", err); + TEST_FAIL("Failed to stop scanning (err %d)", err); return; } param = BT_LE_CONN_PARAM_DEFAULT; err = bt_conn_le_create(addr, BT_CONN_LE_CREATE_CONN, param, &default_conn); if (err) { - FAIL("Failed to create connection (err %d)\n", err); + TEST_FAIL("Failed to create connection (err %d)", err); return; } } @@ -181,7 +183,7 @@ static void test_peripheral_main(void) err = bt_enable(NULL); if (err != 0) { - FAIL("Bluetooth init failed (err %d)\n", err); + TEST_FAIL("Bluetooth init failed (err %d)", err); return; } @@ -189,19 +191,19 @@ static void test_peripheral_main(void) err = bt_le_adv_start(BT_LE_ADV_CONN_FAST_1, ad, ARRAY_SIZE(ad), NULL, 0); if (err != 0) { - FAIL("Advertising failed to start (err %d)\n", err); + TEST_FAIL("Advertising failed to start (err %d)", err); return; } - WAIT_FOR_FLAG_SET(is_connected); + WAIT_FOR_FLAG(is_connected); - WAIT_FOR_FLAG_SET(chan_connected); + WAIT_FOR_FLAG(chan_connected); - WAIT_FOR_FLAG_SET(data_received); + WAIT_FOR_FLAG(data_received); WAIT_FOR_FLAG_UNSET(is_connected); - PASS("Test passed\n"); +TEST_PASS("Test passed"); } static void test_central_main(void) @@ -210,45 +212,41 @@ static void test_central_main(void) err = bt_enable(NULL); if (err != 0) { - FAIL("Bluetooth init failed (err %d)\n", err); + TEST_FAIL("Bluetooth init failed (err %d)", err); } err = bt_le_scan_start(BT_LE_SCAN_PASSIVE, device_found); if (err != 0) { - FAIL("Scanning failed to start (err %d)\n", err); + TEST_FAIL("Scanning failed to start (err %d)", err); } - WAIT_FOR_FLAG_SET(is_connected); + WAIT_FOR_FLAG(is_connected); connect_l2cap_channel(); - WAIT_FOR_FLAG_SET(chan_connected); + WAIT_FOR_FLAG(chan_connected); - WAIT_FOR_FLAG_SET(data_received); + WAIT_FOR_FLAG(data_received); err = bt_conn_disconnect(default_conn, BT_HCI_ERR_REMOTE_USER_TERM_CONN); if (err) { - FAIL("Failed to disconnect (err %d)\n", err); + TEST_FAIL("Failed to disconnect (err %d)", err); return; } WAIT_FOR_FLAG_UNSET(is_connected); - PASS("Test passed\n"); + TEST_PASS("Test passed"); } static const struct bst_test_instance test_def[] = { { .test_id = "peripheral", .test_descr = "Peripheral", - .test_pre_init_f = test_init, - .test_tick_f = test_tick, .test_main_f = test_peripheral_main, }, { .test_id = "central", .test_descr = "Central", - .test_pre_init_f = test_init, - .test_tick_f = test_tick, .test_main_f = test_central_main, }, BSTEST_END_MARKER, diff --git a/tests/bsim/bluetooth/host/l2cap/stress/CMakeLists.txt b/tests/bsim/bluetooth/host/l2cap/stress/CMakeLists.txt index 88c326dddf3d..383f54c095cf 100644 --- a/tests/bsim/bluetooth/host/l2cap/stress/CMakeLists.txt +++ b/tests/bsim/bluetooth/host/l2cap/stress/CMakeLists.txt @@ -5,6 +5,9 @@ cmake_minimum_required(VERSION 3.20.0) find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) project(bsim_test_l2cap_stress) +add_subdirectory(${ZEPHYR_BASE}/tests/bsim/babblekit babblekit) +target_link_libraries(app PRIVATE babblekit) + FILE(GLOB app_sources src/*.c) target_sources(app PRIVATE ${app_sources} ) diff --git a/tests/bsim/bluetooth/host/l2cap/stress/src/common.c b/tests/bsim/bluetooth/host/l2cap/stress/src/common.c deleted file mode 100644 index 5b344c60571f..000000000000 --- a/tests/bsim/bluetooth/host/l2cap/stress/src/common.c +++ /dev/null @@ -1,22 +0,0 @@ -/* - * Copyright (c) 2022 Nordic Semiconductor ASA - * - * SPDX-License-Identifier: Apache-2.0 - */ - -#include "common.h" - -extern enum bst_result_t bst_result; - -void test_init(void) -{ - bst_ticker_set_next_tick_absolute(WAIT_TIME); - bst_result = In_progress; -} - -void test_tick(bs_time_t HW_device_time) -{ - if (bst_result != Passed) { - FAIL("test failed (not passed after %i seconds)\n", WAIT_SECONDS); - } -} diff --git a/tests/bsim/bluetooth/host/l2cap/stress/src/common.h b/tests/bsim/bluetooth/host/l2cap/stress/src/common.h deleted file mode 100644 index 88b7950899a4..000000000000 --- a/tests/bsim/bluetooth/host/l2cap/stress/src/common.h +++ /dev/null @@ -1,57 +0,0 @@ -/* - * Common functions and helpers for L2CAP tests - * - * Copyright (c) 2022 Nordic Semiconductor ASA - * - * SPDX-License-Identifier: Apache-2.0 - */ - -#include - -#include -#include -#include - -#include -#include -#include -#include "bs_types.h" -#include "bs_tracing.h" -#include "bstests.h" -#include "bs_pc_backchannel.h" - -extern enum bst_result_t bst_result; - -#define CREATE_FLAG(flag) static atomic_t flag = (atomic_t)false -#define SET_FLAG(flag) (void)atomic_set(&flag, (atomic_t)true) -#define UNSET_FLAG(flag) (void)atomic_set(&flag, (atomic_t)false) -#define TEST_FLAG(flag) (atomic_get(&flag) == (atomic_t)true) -#define WAIT_FOR_FLAG_SET(flag) \ - while (!(bool)atomic_get(&flag)) { \ - (void)k_sleep(K_MSEC(1)); \ - } -#define WAIT_FOR_FLAG_UNSET(flag) \ - while ((bool)atomic_get(&flag)) { \ - (void)k_sleep(K_MSEC(1)); \ - } - - -#define WAIT_SECONDS 400 /* seconds */ -#define WAIT_TIME (WAIT_SECONDS * USEC_PER_SEC) /* microseconds*/ - -#define FAIL(...) \ - do { \ - bst_result = Failed; \ - bs_trace_error_time_line(__VA_ARGS__); \ - } while (0) - -#define PASS(...) \ - do { \ - bst_result = Passed; \ - bs_trace_info_time(1, __VA_ARGS__); \ - } while (0) - -#define ASSERT(expr, ...) if (!(expr)) {FAIL(__VA_ARGS__); } - -void test_init(void); -void test_tick(bs_time_t HW_device_time); diff --git a/tests/bsim/bluetooth/host/l2cap/stress/src/main.c b/tests/bsim/bluetooth/host/l2cap/stress/src/main.c index 0a9023ecbd31..e104fcfae810 100644 --- a/tests/bsim/bluetooth/host/l2cap/stress/src/main.c +++ b/tests/bsim/bluetooth/host/l2cap/stress/src/main.c @@ -6,15 +6,24 @@ * SPDX-License-Identifier: Apache-2.0 */ -#include "bstests.h" -#include "common.h" +#include + +#include +#include +#include + +#include +#include +#include +#include "babblekit/testcase.h" +#include "babblekit/flags.h" #define LOG_MODULE_NAME main #include LOG_MODULE_REGISTER(LOG_MODULE_NAME, LOG_LEVEL_INF); -CREATE_FLAG(is_connected); -CREATE_FLAG(flag_l2cap_connected); +static DEFINE_FLAG(is_connected); +static DEFINE_FLAG(flag_l2cap_connected); #define NUM_PERIPHERALS 6 #define L2CAP_CHANS NUM_PERIPHERALS @@ -63,8 +72,7 @@ struct test_ctx *get_ctx(struct bt_l2cap_chan *chan) struct bt_l2cap_le_chan *le_chan = CONTAINER_OF(chan, struct bt_l2cap_le_chan, chan); struct test_ctx *ctx = CONTAINER_OF(le_chan, struct test_ctx, le_chan); - ASSERT(ctx >= &contexts[0] && - ctx <= &contexts[L2CAP_CHANS], "memory corruption"); + TEST_ASSERT(ctx >= &contexts[0] && ctx <= &contexts[L2CAP_CHANS], "memory corruption"); return ctx; } @@ -76,7 +84,7 @@ int l2cap_chan_send(struct bt_l2cap_chan *chan, uint8_t *data, size_t len) struct net_buf *buf = net_buf_alloc(&sdu_tx_pool, K_NO_WAIT); if (buf == NULL) { - FAIL("No more memory\n"); + TEST_FAIL("No more memory"); return -ENOMEM; } @@ -93,7 +101,7 @@ int l2cap_chan_send(struct bt_l2cap_chan *chan, uint8_t *data, size_t len) return ret; } - ASSERT(ret >= 0, "Failed sending: err %d", ret); + TEST_ASSERT(ret >= 0, "Failed sending: err %d", ret); LOG_DBG("sent %d len %d", ret, len); return ret; @@ -242,7 +250,7 @@ static int l2cap_server_register(bt_security_t sec_level) int err = bt_l2cap_server_register(&test_l2cap_server); - ASSERT(err == 0, "Failed to register l2cap server."); + TEST_ASSERT(err == 0, "Failed to register l2cap server."); return test_l2cap_server.psm; } @@ -254,7 +262,7 @@ static void connected(struct bt_conn *conn, uint8_t conn_err) bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr)); if (conn_err) { - FAIL("Failed to connect to %s (%u)", addr, conn_err); + TEST_FAIL("Failed to connect to %s (%u)", addr, conn_err); return; } @@ -287,7 +295,7 @@ static void disconnect_device(struct bt_conn *conn, void *data) SET_FLAG(is_connected); err = bt_conn_disconnect(conn, BT_HCI_ERR_REMOTE_USER_TERM_CONN); - ASSERT(!err, "Failed to initate disconnect (err %d)", err); + TEST_ASSERT(!err, "Failed to initate disconnect (err %d)", err); LOG_DBG("Waiting for disconnection..."); WAIT_FOR_FLAG_UNSET(is_connected); @@ -305,7 +313,7 @@ static void test_peripheral_main(void) err = bt_enable(NULL); if (err) { - FAIL("Can't enable Bluetooth (err %d)", err); + TEST_FAIL("Can't enable Bluetooth (err %d)", err); return; } @@ -313,13 +321,13 @@ static void test_peripheral_main(void) LOG_DBG("Connectable advertising..."); err = bt_le_adv_start(BT_LE_ADV_CONN_FAST_1, NULL, 0, NULL, 0); if (err) { - FAIL("Advertising failed to start (err %d)", err); + TEST_FAIL("Advertising failed to start (err %d)", err); return; } LOG_DBG("Advertising started."); LOG_DBG("Peripheral waiting for connection..."); - WAIT_FOR_FLAG_SET(is_connected); + WAIT_FOR_FLAG(is_connected); LOG_DBG("Peripheral Connected."); int psm = l2cap_server_register(BT_SECURITY_L1); @@ -335,9 +343,9 @@ static void test_peripheral_main(void) WAIT_FOR_FLAG_UNSET(is_connected); LOG_INF("Total received: %d", rx_cnt); - ASSERT(rx_cnt == SDU_NUM, "Did not receive expected no of SDUs\n"); + TEST_ASSERT(rx_cnt == SDU_NUM, "Did not receive expected no of SDUs"); - PASS("L2CAP STRESS Peripheral passed\n"); + TEST_PASS("L2CAP STRESS Peripheral passed"); } static void device_found(const bt_addr_le_t *addr, int8_t rssi, uint8_t type, @@ -349,7 +357,7 @@ static void device_found(const bt_addr_le_t *addr, int8_t rssi, uint8_t type, err = bt_le_scan_stop(); if (err) { - FAIL("Stop LE scan failed (err %d)", err); + TEST_FAIL("Stop LE scan failed (err %d)", err); return; } @@ -362,7 +370,7 @@ static void device_found(const bt_addr_le_t *addr, int8_t rssi, uint8_t type, param = BT_LE_CONN_PARAM_DEFAULT; err = bt_conn_le_create(addr, BT_CONN_LE_CREATE_CONN, param, &conn); if (err) { - FAIL("Create conn failed (err %d)", err); + TEST_FAIL("Create conn failed (err %d)", err); return; } } @@ -380,10 +388,10 @@ static void connect_peripheral(void) int err = bt_le_scan_start(&scan_param, device_found); - ASSERT(!err, "Scanning failed to start (err %d)\n", err); + TEST_ASSERT(!err, "Scanning failed to start (err %d)", err); LOG_DBG("Central initiating connection..."); - WAIT_FOR_FLAG_SET(is_connected); + WAIT_FOR_FLAG(is_connected); } static void connect_l2cap_channel(struct bt_conn *conn, void *data) @@ -391,7 +399,7 @@ static void connect_l2cap_channel(struct bt_conn *conn, void *data) int err; struct test_ctx *ctx = alloc_test_context(); - ASSERT(ctx, "No more available test contexts\n"); + TEST_ASSERT(ctx, "No more available test contexts"); struct bt_l2cap_le_chan *le_chan = &ctx->le_chan; @@ -401,9 +409,9 @@ static void connect_l2cap_channel(struct bt_conn *conn, void *data) UNSET_FLAG(flag_l2cap_connected); err = bt_l2cap_chan_connect(conn, &le_chan->chan, 0x0080); - ASSERT(!err, "Error connecting l2cap channel (err %d)\n", err); + TEST_ASSERT(!err, "Error connecting l2cap channel (err %d)", err); - WAIT_FOR_FLAG_SET(flag_l2cap_connected); + WAIT_FOR_FLAG(flag_l2cap_connected); } static void test_central_main(void) @@ -417,7 +425,7 @@ static void test_central_main(void) } err = bt_enable(NULL); - ASSERT(err == 0, "Can't enable Bluetooth (err %d)\n", err); + TEST_ASSERT(err == 0, "Can't enable Bluetooth (err %d)", err); LOG_DBG("Central Bluetooth initialized."); /* Connect all peripherals */ @@ -453,22 +461,18 @@ static void test_central_main(void) } LOG_DBG("All peripherals disconnected."); - PASS("L2CAP STRESS Central passed\n"); + TEST_PASS("L2CAP STRESS Central passed"); } static const struct bst_test_instance test_def[] = { { .test_id = "peripheral", .test_descr = "Peripheral L2CAP STRESS", - .test_pre_init_f = test_init, - .test_tick_f = test_tick, .test_main_f = test_peripheral_main }, { .test_id = "central", .test_descr = "Central L2CAP STRESS", - .test_pre_init_f = test_init, - .test_tick_f = test_tick, .test_main_f = test_central_main }, BSTEST_END_MARKER diff --git a/tests/bsim/bluetooth/host/l2cap/userdata/CMakeLists.txt b/tests/bsim/bluetooth/host/l2cap/userdata/CMakeLists.txt index 298213989f5f..2355ef86c05e 100644 --- a/tests/bsim/bluetooth/host/l2cap/userdata/CMakeLists.txt +++ b/tests/bsim/bluetooth/host/l2cap/userdata/CMakeLists.txt @@ -5,6 +5,9 @@ cmake_minimum_required(VERSION 3.20.0) find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) project(bsim_test_l2cap_userdata) +add_subdirectory(${ZEPHYR_BASE}/tests/bsim/babblekit babblekit) +target_link_libraries(app PRIVATE babblekit) + FILE(GLOB app_sources src/*.c) target_sources(app PRIVATE ${app_sources} ) diff --git a/tests/bsim/bluetooth/host/l2cap/userdata/src/common.c b/tests/bsim/bluetooth/host/l2cap/userdata/src/common.c deleted file mode 100644 index f725ac6129c3..000000000000 --- a/tests/bsim/bluetooth/host/l2cap/userdata/src/common.c +++ /dev/null @@ -1,22 +0,0 @@ -/* - * Copyright (c) 2022 Nordic Semiconductor ASA - * - * SPDX-License-Identifier: Apache-2.0 - */ - -#include "common.h" - -extern enum bst_result_t bst_result; - -void test_init(void) -{ - bst_result = In_progress; - bst_ticker_set_next_tick_absolute(WAIT_TIME); -} - -void test_tick(bs_time_t HW_device_time) -{ - if (bst_result != Passed) { - FAIL("test failed (not passed after %i us)\n", WAIT_TIME); - } -} diff --git a/tests/bsim/bluetooth/host/l2cap/userdata/src/common.h b/tests/bsim/bluetooth/host/l2cap/userdata/src/common.h deleted file mode 100644 index 3b0d09c53b6c..000000000000 --- a/tests/bsim/bluetooth/host/l2cap/userdata/src/common.h +++ /dev/null @@ -1,44 +0,0 @@ -/* - * Common functions and helpers for L2CAP tests - * - * Copyright (c) 2022 Nordic Semiconductor ASA - * - * SPDX-License-Identifier: Apache-2.0 - */ - -#include - -#include "bs_types.h" -#include "bs_tracing.h" -#include "bstests.h" - -#define CREATE_FLAG(flag) static atomic_t flag = (atomic_t)false -#define SET_FLAG(flag) (void)atomic_set(&flag, (atomic_t)true) -#define UNSET_FLAG(flag) (void)atomic_set(&flag, (atomic_t)false) -#define WAIT_FOR_FLAG_SET(flag) \ - while (!(bool)atomic_get(&flag)) { \ - (void)k_sleep(K_MSEC(1)); \ - } -#define WAIT_FOR_FLAG_UNSET(flag) \ - while ((bool)atomic_get(&flag)) { \ - (void)k_sleep(K_MSEC(1)); \ - } - - -#define WAIT_TIME (30e6) /* 30 seconds*/ - -#define FAIL(...) \ - do { \ - bst_result = Failed; \ - bs_trace_error_time_line(__VA_ARGS__); \ - } while (0) - -#define PASS(...) \ - do { \ - bst_result = Passed; \ - bs_trace_info_time(1, __VA_ARGS__); \ - } while (0) - - -void test_init(void); -void test_tick(bs_time_t HW_device_time); diff --git a/tests/bsim/bluetooth/host/l2cap/userdata/src/main_l2cap_userdata.c b/tests/bsim/bluetooth/host/l2cap/userdata/src/main_l2cap_userdata.c index 57e70ae88210..e492036736f1 100644 --- a/tests/bsim/bluetooth/host/l2cap/userdata/src/main_l2cap_userdata.c +++ b/tests/bsim/bluetooth/host/l2cap/userdata/src/main_l2cap_userdata.c @@ -4,11 +4,12 @@ * SPDX-License-Identifier: Apache-2.0 */ -#include "common.h" - #include #include +#include "babblekit/testcase.h" +#include "babblekit/flags.h" + extern enum bst_result_t bst_result; static struct bt_conn *default_conn; @@ -20,10 +21,10 @@ static struct bt_conn *default_conn; /* Pool to allocate a buffer that is too large to send */ NET_BUF_POOL_DEFINE(buf_pool, 1, BT_L2CAP_SDU_BUF_SIZE(DATA_SIZE), USER_DATA_SIZE, NULL); -CREATE_FLAG(is_connected); -CREATE_FLAG(is_sent); -CREATE_FLAG(has_received); -CREATE_FLAG(chan_connected); +static DEFINE_FLAG(is_connected); +static DEFINE_FLAG(is_sent); +static DEFINE_FLAG(has_received); +static DEFINE_FLAG(chan_connected); static void chan_connected_cb(struct bt_l2cap_chan *l2cap_chan) { @@ -96,7 +97,7 @@ static void connect_l2cap_channel(void) err = bt_l2cap_ecred_chan_connect(default_conn, chans, server.psm); if (err) { - FAIL("Failed to send ecred connection request (err %d)\n", err); + TEST_FAIL("Failed to send ecred connection request (err %d)", err); } } @@ -106,7 +107,7 @@ static void register_l2cap_server(void) err = bt_l2cap_server_register(&server); if (err < 0) { - FAIL("Failed to get free server (err %d)\n"); + TEST_FAIL("Failed to get free server (err %d)"); return; } } @@ -114,7 +115,7 @@ static void register_l2cap_server(void) static void connected(struct bt_conn *conn, uint8_t err) { if (err) { - FAIL("Failed to connect (err %d)\n", err); + TEST_FAIL("Failed to connect (err %d)", err); bt_conn_unref(default_conn); default_conn = NULL; return; @@ -128,7 +129,7 @@ static void connected(struct bt_conn *conn, uint8_t err) static void disconnected(struct bt_conn *conn, uint8_t reason) { if (default_conn != conn) { - FAIL("Connection mismatch %p %p)\n", default_conn, conn); + TEST_FAIL("Connection mismatch %p %p)", default_conn, conn); return; } @@ -150,14 +151,14 @@ static void device_found(const bt_addr_le_t *addr, int8_t rssi, uint8_t type, err = bt_le_scan_stop(); if (err) { - FAIL("Failed to stop scanning (err %d)\n", err); + TEST_FAIL("Failed to stop scanning (err %d)", err); return; } param = BT_LE_CONN_PARAM_DEFAULT; err = bt_conn_le_create(addr, BT_CONN_LE_CREATE_CONN, param, &default_conn); if (err) { - FAIL("Failed to create connection (err %d)\n", err); + TEST_FAIL("Failed to create connection (err %d)", err); return; } } @@ -171,37 +172,37 @@ static void test_peripheral_main(void) err = bt_enable(NULL); if (err != 0) { - FAIL("Bluetooth init failed (err %d)\n", err); + TEST_FAIL("Bluetooth init failed (err %d)", err); return; } err = bt_le_adv_start(BT_LE_ADV_CONN_FAST_1, ad, ARRAY_SIZE(ad), NULL, 0); if (err != 0) { - FAIL("Advertising failed to start (err %d)\n", err); + TEST_FAIL("Advertising failed to start (err %d)", err); return; } - WAIT_FOR_FLAG_SET(is_connected); + WAIT_FOR_FLAG(is_connected); register_l2cap_server(); if (!IS_ENABLED(CONFIG_NO_RUNTIME_CHECKS)) { - PASS("Peripheral done\n"); + TEST_PASS("Peripheral done"); return; } - WAIT_FOR_FLAG_SET(has_received); + WAIT_FOR_FLAG(has_received); /* /\* Wait until we get the credits *\/ */ /* k_sleep(K_MSEC(100)); */ err = bt_conn_disconnect(default_conn, BT_HCI_ERR_REMOTE_USER_TERM_CONN); if (err) { - FAIL("Failed to disconnect (err %d)\n", err); + TEST_FAIL("Failed to disconnect (err %d)", err); return; } - PASS("Test passed\n"); + TEST_PASS("Test passed"); } #define FILL 0xAA @@ -226,22 +227,22 @@ static void test_central_main(void) err = bt_enable(NULL); if (err != 0) { - FAIL("Bluetooth discover failed (err %d)\n", err); + TEST_FAIL("Bluetooth discover failed (err %d)", err); } err = bt_le_scan_start(BT_LE_SCAN_PASSIVE, device_found); if (err != 0) { - FAIL("Scanning failed to start (err %d)\n", err); + TEST_FAIL("Scanning failed to start (err %d)", err); } - WAIT_FOR_FLAG_SET(is_connected); + WAIT_FOR_FLAG(is_connected); connect_l2cap_channel(); - WAIT_FOR_FLAG_SET(chan_connected); + WAIT_FOR_FLAG(chan_connected); buf = net_buf_alloc(&buf_pool, K_NO_WAIT); if (!buf) { - FAIL("Buffer allcation failed\n"); + TEST_FAIL("Buffer allcation failed"); } net_buf_reserve(buf, BT_L2CAP_SDU_CHAN_SEND_RESERVE); @@ -264,10 +265,10 @@ static void test_central_main(void) */ if (err != 0) { - FAIL("Got error %d\n", err); + TEST_FAIL("Got error %d", err); } - WAIT_FOR_FLAG_SET(is_sent); + WAIT_FOR_FLAG(is_sent); printk("Buffer user_data after (should've been cleared)\n"); print_user_data(buf); @@ -277,26 +278,22 @@ static void test_central_main(void) /* Validate that the user data has changed */ for (int i = 0; i < USER_DATA_SIZE; i++) { if (buf->user_data[i] == FILL) { - FAIL("Buffer user data should be reset by stack.\n"); + TEST_FAIL("Buffer user data should be reset by stack."); } } - PASS("(Disabled-checks) Test passed\n"); + TEST_PASS("(Disabled-checks) Test passed"); } static const struct bst_test_instance test_def[] = { { .test_id = "peripheral", .test_descr = "Peripheral", - .test_pre_init_f = test_init, - .test_tick_f = test_tick, .test_main_f = test_peripheral_main, }, { .test_id = "central", .test_descr = "Central", - .test_pre_init_f = test_init, - .test_tick_f = test_tick, .test_main_f = test_central_main, }, BSTEST_END_MARKER, diff --git a/tests/bsim/bluetooth/host/misc/conn_stress/central/src/main.c b/tests/bsim/bluetooth/host/misc/conn_stress/central/src/main.c index 9b8a38e8cb61..7ce3dbfe489d 100644 --- a/tests/bsim/bluetooth/host/misc/conn_stress/central/src/main.c +++ b/tests/bsim/bluetooth/host/misc/conn_stress/central/src/main.c @@ -720,7 +720,7 @@ static void test_args(int argc, char **argv) ptr = strstr(argv[0], "notify_size="); if (ptr != NULL) { ptr += strlen("notify_size="); - notification_size = atol(ptr); + notification_size = strtol(ptr, NULL, 10); notification_size = MIN(NOTIFICATION_DATA_LEN, notification_size); } } @@ -731,7 +731,7 @@ static void test_args(int argc, char **argv) ptr = strstr(argv[1], "conn_interval="); if (ptr != NULL) { ptr += strlen("conn_interval="); - conn_interval_max = atol(ptr); + conn_interval_max = strtol(ptr, NULL, 10); } } diff --git a/tests/bsim/bluetooth/host/misc/conn_stress/peripheral/src/main.c b/tests/bsim/bluetooth/host/misc/conn_stress/peripheral/src/main.c index be11ade9baf8..b46d52b60d79 100644 --- a/tests/bsim/bluetooth/host/misc/conn_stress/peripheral/src/main.c +++ b/tests/bsim/bluetooth/host/misc/conn_stress/peripheral/src/main.c @@ -473,7 +473,7 @@ static void test_args(int argc, char **argv) ptr = strstr(argv[0], "notify_size="); if (ptr != NULL) { ptr += strlen("notify_size="); - notification_size = atol(ptr); + notification_size = strtol(ptr, NULL, 10); notification_size = MIN(NOTIFICATION_DATA_LEN, notification_size); } } diff --git a/tests/bsim/bluetooth/host/misc/disable/CMakeLists.txt b/tests/bsim/bluetooth/host/misc/disable/CMakeLists.txt index af4a62820e71..199232976e14 100644 --- a/tests/bsim/bluetooth/host/misc/disable/CMakeLists.txt +++ b/tests/bsim/bluetooth/host/misc/disable/CMakeLists.txt @@ -14,6 +14,8 @@ zephyr_include_directories( ) add_subdirectory(${ZEPHYR_BASE}/tests/bluetooth/common/testlib testlib) +add_subdirectory(${ZEPHYR_BASE}/tests/bsim/babblekit babblekit) target_link_libraries(app PRIVATE testlib + babblekit ) diff --git a/tests/bsim/bluetooth/host/misc/disable/src/common.c b/tests/bsim/bluetooth/host/misc/disable/src/common.c deleted file mode 100644 index df438607c5fa..000000000000 --- a/tests/bsim/bluetooth/host/misc/disable/src/common.c +++ /dev/null @@ -1,20 +0,0 @@ -/* - * Copyright (c) 2022 Nordic Semiconductor ASA - * - * SPDX-License-Identifier: Apache-2.0 - */ - -#include "common.h" - -void test_tick(bs_time_t HW_device_time) -{ - if (bst_result != Passed) { - FAIL("test failed (not passed after %i seconds)\n", WAIT_TIME); - } -} - -void test_init(void) -{ - bst_ticker_set_next_tick_absolute(WAIT_TIME); - bst_result = In_progress; -} diff --git a/tests/bsim/bluetooth/host/misc/disable/src/common.h b/tests/bsim/bluetooth/host/misc/disable/src/common.h index cb93d7ff71af..46c3dc38232a 100644 --- a/tests/bsim/bluetooth/host/misc/disable/src/common.h +++ b/tests/bsim/bluetooth/host/misc/disable/src/common.h @@ -6,51 +6,6 @@ * SPDX-License-Identifier: Apache-2.0 */ -#include - -#include "bs_types.h" -#include "bs_tracing.h" -#include "time_machine.h" -#include "bstests.h" - -#include -#include -#include - -#include -#include -#include -#include -#include - -extern enum bst_result_t bst_result; - -#define WAIT_TIME (600 * 1e6) /*seconds*/ - -#define CREATE_FLAG(flag) static atomic_t flag = (atomic_t)false -#define SET_FLAG(flag) (void)atomic_set(&flag, (atomic_t)true) -#define UNSET_FLAG(flag) (void)atomic_set(&flag, (atomic_t)false) -#define WAIT_FOR_FLAG(flag) \ - while (!(bool)atomic_get(&flag)) { \ - (void)k_sleep(K_MSEC(1)); \ - } -#define WAIT_FOR_FLAG_UNSET(flag) \ - while ((bool)atomic_get(&flag)) { \ - (void)k_sleep(K_MSEC(1)); \ - } - -#define FAIL(...) \ - do { \ - bst_result = Failed; \ - bs_trace_error_time_line(__VA_ARGS__); \ - } while (0) - -#define PASS(...) \ - do { \ - bst_result = Passed; \ - bs_trace_info_time(1, __VA_ARGS__); \ - } while (0) - #define CHRC_SIZE 10 #define LONG_CHRC_SIZE 40 @@ -65,6 +20,3 @@ extern enum bst_result_t bst_result; #define TEST_LONG_CHRC_UUID \ BT_UUID_DECLARE_128(0x01, 0x23, 0x45, 0x67, 0x89, 0x01, 0x02, 0x03, \ 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0xFF, 0x11) - -void test_tick(bs_time_t HW_device_time); -void test_init(void); diff --git a/tests/bsim/bluetooth/host/misc/disable/src/gatt_client_test.c b/tests/bsim/bluetooth/host/misc/disable/src/gatt_client_test.c index 1c7df533bbd8..4d53d60a7ec5 100644 --- a/tests/bsim/bluetooth/host/misc/disable/src/gatt_client_test.c +++ b/tests/bsim/bluetooth/host/misc/disable/src/gatt_client_test.c @@ -4,15 +4,24 @@ * SPDX-License-Identifier: Apache-2.0 */ +#include +#include +#include +#include #include +#include +#include +#include #include +#include "babblekit/testcase.h" +#include "babblekit/flags.h" #include "common.h" -CREATE_FLAG(flag_is_connected); -CREATE_FLAG(flag_discover_complete); -CREATE_FLAG(flag_write_complete); -CREATE_FLAG(flag_read_complete); +static DEFINE_FLAG(flag_is_connected); +static DEFINE_FLAG(flag_discover_complete); +static DEFINE_FLAG(flag_write_complete); +static DEFINE_FLAG(flag_read_complete); static struct bt_conn *g_conn; static uint16_t chrc_handle; @@ -32,7 +41,7 @@ static void connected(struct bt_conn *conn, uint8_t err) bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr)); if (err != 0) { - FAIL("Failed to connect to %s (%u)\n", addr, err); + TEST_FAIL("Failed to connect to %s (%u)", addr, err); return; } @@ -86,14 +95,14 @@ void device_found(const bt_addr_le_t *addr, int8_t rssi, uint8_t type, printk("Stopping scan\n"); err = bt_le_scan_stop(); if (err != 0) { - FAIL("Could not stop scan: %d"); + TEST_FAIL("Could not stop scan: %d"); return; } err = bt_conn_le_create(addr, BT_CONN_LE_CREATE_CONN, BT_LE_CONN_PARAM_DEFAULT, &g_conn); if (err != 0) { - FAIL("Could not connect to peer: %d", err); + TEST_FAIL("Could not connect to peer: %d", err); } } @@ -105,8 +114,8 @@ static uint8_t discover_func(struct bt_conn *conn, if (attr == NULL) { if (chrc_handle == 0 || long_chrc_handle == 0) { - FAIL("Did not discover chrc (%x) or long_chrc (%x)", - chrc_handle, long_chrc_handle); + TEST_FAIL("Did not discover chrc (%x) or long_chrc (%x)", chrc_handle, + long_chrc_handle); } (void)memset(params, 0, sizeof(*params)); @@ -127,7 +136,7 @@ static uint8_t discover_func(struct bt_conn *conn, err = bt_gatt_discover(conn, params); if (err != 0) { - FAIL("Discover failed (err %d)\n", err); + TEST_FAIL("Discover failed (err %d)", err); } return BT_GATT_ITER_STOP; @@ -161,7 +170,7 @@ static void gatt_discover(void) err = bt_gatt_discover(g_conn, &discover_params); if (err != 0) { - FAIL("Discover failed(err %d)\n", err); + TEST_FAIL("Discover failed(err %d)", err); } WAIT_FOR_FLAG(flag_discover_complete); @@ -172,7 +181,7 @@ static void gatt_write_cb(struct bt_conn *conn, uint8_t err, struct bt_gatt_write_params *params) { if (err != BT_ATT_ERR_SUCCESS) { - FAIL("Write failed: 0x%02X\n", err); + TEST_FAIL("Write failed: 0x%02X\n", err); } (void)memset(params, 0, sizeof(*params)); @@ -202,7 +211,7 @@ static void gatt_write(uint16_t handle) err = bt_gatt_write(g_conn, &write_params); if (err != 0) { - FAIL("bt_gatt_write failed: %d\n", err); + TEST_FAIL("bt_gatt_write failed: %d", err); } WAIT_FOR_FLAG(flag_write_complete); @@ -214,18 +223,16 @@ static uint8_t gatt_read_cb(struct bt_conn *conn, uint8_t err, const void *data, uint16_t length) { if (err != BT_ATT_ERR_SUCCESS) { - FAIL("Read failed: 0x%02X\n", err); + TEST_FAIL("Read failed: 0x%02X\n", err); } if (params->single.handle == chrc_handle) { - if (length != CHRC_SIZE || - memcmp(data, chrc_data, length) != 0) { - FAIL("chrc data different than expected", err); + if (length != CHRC_SIZE || memcmp(data, chrc_data, length) != 0) { + TEST_FAIL("chrc data different than expected", err); } } else if (params->single.handle == chrc_handle) { - if (length != LONG_CHRC_SIZE || - memcmp(data, long_chrc_data, length) != 0) { - FAIL("long_chrc data different than expected", err); + if (length != LONG_CHRC_SIZE || memcmp(data, long_chrc_data, length) != 0) { + TEST_FAIL("long_chrc data different than expected", err); } } @@ -252,7 +259,7 @@ static void gatt_read(uint16_t handle) err = bt_gatt_read(g_conn, &read_params); if (err != 0) { - FAIL("bt_gatt_read failed: %d\n", err); + TEST_FAIL("bt_gatt_read failed: %d", err); } WAIT_FOR_FLAG(flag_read_complete); @@ -269,13 +276,13 @@ static void test_main(void) err = bt_enable(NULL); if (err != 0) { - FAIL("Bluetooth discover failed (err %d)\n", err); + TEST_FAIL("Bluetooth discover failed (err %d)", err); } printk("Bluetooth initialized\n"); err = bt_le_scan_start(BT_LE_SCAN_PASSIVE, device_found); if (err != 0) { - FAIL("Scanning failed to start (err %d)\n", err); + TEST_FAIL("Scanning failed to start (err %d)", err); } printk("Scanning successfully started\n"); @@ -294,7 +301,7 @@ static void test_main(void) err = bt_conn_disconnect(g_conn, 0x13); if (err != 0) { - FAIL("Disconnect failed (err %d)\n", err); + TEST_FAIL("Disconnect failed (err %d)", err); return; } @@ -302,19 +309,17 @@ static void test_main(void) err = bt_disable(); if (err != 0) { - FAIL("Bluetooth disable failed (err %d)\n", err); + TEST_FAIL("Bluetooth disable failed (err %d)", err); } printk("Bluetooth successfully disabled\n"); } - PASS("GATT client Passed\n"); + TEST_PASS("GATT client Passed\n"); } static const struct bst_test_instance test_vcs[] = { { .test_id = "gatt_client", - .test_pre_init_f = test_init, - .test_tick_f = test_tick, .test_main_f = test_main }, BSTEST_END_MARKER diff --git a/tests/bsim/bluetooth/host/misc/disable/src/gatt_server_test.c b/tests/bsim/bluetooth/host/misc/disable/src/gatt_server_test.c index 78cee69024d2..613f76d5c22b 100644 --- a/tests/bsim/bluetooth/host/misc/disable/src/gatt_server_test.c +++ b/tests/bsim/bluetooth/host/misc/disable/src/gatt_server_test.c @@ -4,11 +4,23 @@ * SPDX-License-Identifier: Apache-2.0 */ +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "babblekit/testcase.h" +#include "babblekit/flags.h" #include "common.h" extern enum bst_result_t bst_result; -CREATE_FLAG(flag_is_connected); +static DEFINE_FLAG(flag_is_connected); #define NUM_ITERATIONS 10 @@ -19,7 +31,7 @@ static void connected(struct bt_conn *conn, uint8_t err) bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr)); if (err != 0) { - FAIL("Failed to connect to %s (%u)\n", addr, err); + TEST_FAIL("Failed to connect to %s (%u)", addr, err); return; } @@ -71,7 +83,7 @@ static ssize_t write_test_chrc(struct bt_conn *conn, } if (flags != 0) { - FAIL("Invalid flags %u\n", flags); + TEST_FAIL("Invalid flags %u", flags); return BT_GATT_ERR(BT_ATT_ERR_UNLIKELY); } @@ -138,7 +150,7 @@ static void test_main(void) for (int i = 0; i < NUM_ITERATIONS; i++) { err = bt_enable(NULL); if (err != 0) { - FAIL("Bluetooth init failed (err %d)\n", err); + TEST_FAIL("Bluetooth init failed (err %d)", err); return; } @@ -146,7 +158,7 @@ static void test_main(void) err = bt_le_adv_start(BT_LE_ADV_CONN_FAST_1, ad, ARRAY_SIZE(ad), NULL, 0); if (err != 0) { - FAIL("Advertising failed to start (err %d)\n", err); + TEST_FAIL("Advertising failed to start (err %d)", err); return; } @@ -158,21 +170,19 @@ static void test_main(void) err = bt_disable(); if (err != 0) { - FAIL("Bluetooth disable failed (err %d)\n", err); + TEST_FAIL("Bluetooth disable failed (err %d)", err); return; } printk("Bluetooth disabled\n"); } - PASS("GATT server passed\n"); + TEST_PASS("GATT server passed"); } static const struct bst_test_instance test_gatt_server[] = { { .test_id = "gatt_server", - .test_pre_init_f = test_init, - .test_tick_f = test_tick, .test_main_f = test_main }, BSTEST_END_MARKER diff --git a/tests/bsim/bluetooth/host/misc/disable/src/main_disable.c b/tests/bsim/bluetooth/host/misc/disable/src/main_disable.c index f7f2c3c495ad..e100e50456dd 100644 --- a/tests/bsim/bluetooth/host/misc/disable/src/main_disable.c +++ b/tests/bsim/bluetooth/host/misc/disable/src/main_disable.c @@ -6,10 +6,14 @@ * SPDX-License-Identifier: Apache-2.0 */ +#include + #define LOG_MODULE_NAME main_disable #include LOG_MODULE_REGISTER(LOG_MODULE_NAME, LOG_LEVEL_DBG); +#include "babblekit/testcase.h" +#include "babblekit/flags.h" #include "common.h" extern enum bst_result_t bst_result; @@ -23,16 +27,16 @@ static void test_disable_main(void) err = bt_enable(NULL); if (err != 0) { - FAIL("Enable failed (err %d)\n", err); + TEST_FAIL("Enable failed (err %d)", err); } err = bt_disable(); if (err) { - FAIL("Enable failed (err %d)\n", err); + TEST_FAIL("Enable failed (err %d)", err); } } - PASS("Disable test passed\n"); + TEST_PASS("Disable test passed"); } static void test_disable_set_default_id(void) @@ -53,23 +57,23 @@ static void test_disable_set_default_id(void) err = bt_id_create(&addr, NULL); if (err != 0) { - FAIL("Creating ID failed (err %d)\n", err); + TEST_FAIL("Creating ID failed (err %d)", err); } err = bt_enable(NULL); if (err != 0) { - FAIL("Enable failed (err %d)\n", err); + TEST_FAIL("Enable failed (err %d)", err); } bt_id_get(NULL, &id_count); if (id_count != 1) { - FAIL("Expected only one ID, but got: %u\n", id_count); + TEST_FAIL("Expected only one ID, but got: %u", id_count); } id_count = 1; bt_id_get(&stored_id, &id_count); if (id_count != 1) { - FAIL("Expected only one ID, but got: %u\n", id_count); + TEST_FAIL("Expected only one ID, but got: %u", id_count); } if (!bt_addr_le_eq(&stored_id, &addr)) { @@ -78,32 +82,28 @@ static void test_disable_set_default_id(void) bt_addr_le_to_str(&addr, addr_set_str, BT_ADDR_LE_STR_LEN); bt_addr_le_to_str(&stored_id, addr_stored_str, BT_ADDR_LE_STR_LEN); - FAIL("Expected stored ID to be equal to set ID: %s, %s\n", - addr_set_str, addr_stored_str); + TEST_FAIL("Expected stored ID to be equal to set ID: %s, %s", addr_set_str, + addr_stored_str); } err = bt_disable(); if (err) { - FAIL("Enable failed (err %d)\n", err); + TEST_FAIL("Enable failed (err %d)", err); } } - PASS("Disable set default ID test passed\n"); + TEST_PASS("Disable set default ID test passed"); } static const struct bst_test_instance test_def[] = { { .test_id = "disable", .test_descr = "disable_test", - .test_pre_init_f = test_init, - .test_tick_f = test_tick, .test_main_f = test_disable_main }, { .test_id = "disable_set_default_id", .test_descr = "disable_test where each iteration sets the default ID", - .test_pre_init_f = test_init, - .test_tick_f = test_tick, .test_main_f = test_disable_set_default_id }, BSTEST_END_MARKER diff --git a/tests/bsim/bluetooth/host/misc/unregister_conn_cb/CMakeLists.txt b/tests/bsim/bluetooth/host/misc/unregister_conn_cb/CMakeLists.txt index 81042fb740b8..57928c2eb04f 100644 --- a/tests/bsim/bluetooth/host/misc/unregister_conn_cb/CMakeLists.txt +++ b/tests/bsim/bluetooth/host/misc/unregister_conn_cb/CMakeLists.txt @@ -5,6 +5,9 @@ cmake_minimum_required(VERSION 3.20.0) find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) project(bsim_test_unregister_conn_cb) +add_subdirectory(${ZEPHYR_BASE}/tests/bsim/babblekit babblekit) +target_link_libraries(app PRIVATE babblekit) + FILE(GLOB app_sources src/*.c) target_sources(app PRIVATE ${app_sources} ) diff --git a/tests/bsim/bluetooth/host/misc/unregister_conn_cb/src/common.c b/tests/bsim/bluetooth/host/misc/unregister_conn_cb/src/common.c deleted file mode 100644 index df438607c5fa..000000000000 --- a/tests/bsim/bluetooth/host/misc/unregister_conn_cb/src/common.c +++ /dev/null @@ -1,20 +0,0 @@ -/* - * Copyright (c) 2022 Nordic Semiconductor ASA - * - * SPDX-License-Identifier: Apache-2.0 - */ - -#include "common.h" - -void test_tick(bs_time_t HW_device_time) -{ - if (bst_result != Passed) { - FAIL("test failed (not passed after %i seconds)\n", WAIT_TIME); - } -} - -void test_init(void) -{ - bst_ticker_set_next_tick_absolute(WAIT_TIME); - bst_result = In_progress; -} diff --git a/tests/bsim/bluetooth/host/misc/unregister_conn_cb/src/common.h b/tests/bsim/bluetooth/host/misc/unregister_conn_cb/src/common.h deleted file mode 100644 index 159f27a8a21a..000000000000 --- a/tests/bsim/bluetooth/host/misc/unregister_conn_cb/src/common.h +++ /dev/null @@ -1,55 +0,0 @@ -/** - * Common functions and helpers for unregister connection callback tests - * - * Copyright (c) 2024 NXP - * Copyright (c) 2022 Nordic Semiconductor ASA - * - * SPDX-License-Identifier: Apache-2.0 - */ - -#include - -#include "bs_types.h" -#include "bs_tracing.h" -#include "time_machine.h" -#include "bstests.h" - -#include -#include -#include - -#include -#include -#include - -extern enum bst_result_t bst_result; - -#define WAIT_SECONDS (30) /*seconds*/ -#define WAIT_TIME (WAIT_SECONDS * USEC_PER_SEC) /*microseconds*/ - -#define CREATE_FLAG(flag) static atomic_t flag = (atomic_t) false -#define SET_FLAG(flag) (void)atomic_set(&flag, (atomic_t) true) -#define UNSET_FLAG(flag) (void)atomic_set(&flag, (atomic_t) false) -#define WAIT_FOR_FLAG(flag) \ - while (!(bool)atomic_get(&flag)) { \ - (void)k_sleep(K_MSEC(1)); \ - } -#define WAIT_FOR_FLAG_UNSET(flag) \ - while ((bool)atomic_get(&flag)) { \ - (void)k_sleep(K_MSEC(1)); \ - } - -#define FAIL(...) \ - do { \ - bst_result = Failed; \ - bs_trace_error_time_line(__VA_ARGS__); \ - } while (0) - -#define PASS(...) \ - do { \ - bst_result = Passed; \ - bs_trace_info_time(1, __VA_ARGS__); \ - } while (0) - -void test_tick(bs_time_t HW_device_time); -void test_init(void); diff --git a/tests/bsim/bluetooth/host/misc/unregister_conn_cb/src/main.c b/tests/bsim/bluetooth/host/misc/unregister_conn_cb/src/main.c index ac2d919afab4..a2c6cd87ce4e 100644 --- a/tests/bsim/bluetooth/host/misc/unregister_conn_cb/src/main.c +++ b/tests/bsim/bluetooth/host/misc/unregister_conn_cb/src/main.c @@ -7,14 +7,19 @@ * SPDX-License-Identifier: Apache-2.0 */ -#include "bstests.h" +#include +#include +#include +#include #include +#include +#include #include +#include "babblekit/testcase.h" +#include "babblekit/flags.h" -#include "common.h" - -CREATE_FLAG(flag_is_connected); +static DEFINE_FLAG(flag_is_connected); static struct bt_conn *g_conn; @@ -25,7 +30,7 @@ static void connected(struct bt_conn *conn, uint8_t err) bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr)); if (err != 0) { - FAIL("Failed to connect to %s (%u)\n", addr, err); + TEST_FAIL("Failed to connect to %s (%u)", addr, err); return; } @@ -82,13 +87,13 @@ void device_found(const bt_addr_le_t *addr, int8_t rssi, uint8_t type, struct ne printk("Stopping scan\n"); err = bt_le_scan_stop(); if (err != 0) { - FAIL("Could not stop scan: %d"); + TEST_FAIL("Could not stop scan: %d"); return; } err = bt_conn_le_create(addr, BT_CONN_LE_CREATE_CONN, BT_LE_CONN_PARAM_DEFAULT, &conn); if (err != 0) { - FAIL("Could not connect to peer: %d", err); + TEST_FAIL("Could not connect to peer: %d", err); } printk("%s: connected to found device\n", __func__); @@ -125,7 +130,7 @@ static void start_adv(void) err = bt_le_adv_start(BT_LE_ADV_CONN_FAST_1, ad, ARRAY_SIZE(ad), NULL, 0); if (err != 0) { - FAIL("Advertising failed to start (err %d)\n", err); + TEST_FAIL("Advertising failed to start (err %d)", err); return; } @@ -138,7 +143,7 @@ static void test_peripheral_main(void) err = bt_enable(NULL); if (err != 0) { - FAIL("Bluetooth init failed (err %d)\n", err); + TEST_FAIL("Bluetooth init failed (err %d)", err); return; } @@ -161,13 +166,13 @@ static void test_peripheral_main(void) err = bt_disable(); if (err != 0) { - FAIL("Bluetooth disable failed (err %d)\n", err); + TEST_FAIL("Bluetooth disable failed (err %d)", err); return; } printk("Bluetooth successfully disabled\n"); - PASS("Peripheral device passed\n"); + TEST_PASS("Peripheral device passed"); } static void test_central_main(void) @@ -177,14 +182,14 @@ static void test_central_main(void) err = bt_enable(NULL); if (err != 0) { - FAIL("Bluetooth discover failed (err %d)\n", err); + TEST_FAIL("Bluetooth discover failed (err %d)", err); } printk("Bluetooth initialized\n"); bt_conn_cb_register(&conn_callbacks); /* Connect to peer device after conn_callbacks registered*/ err = bt_le_scan_start(BT_LE_SCAN_PASSIVE, device_found); if (err != 0) { - FAIL("Scanning failed to start (err %d)\n", err); + TEST_FAIL("Scanning failed to start (err %d)", err); } printk("Scanning successfully started\n"); @@ -194,7 +199,7 @@ static void test_central_main(void) err = bt_conn_disconnect(g_conn, 0x13); if (err != 0) { - FAIL("Disconnect failed (err %d)\n", err); + TEST_FAIL("Disconnect failed (err %d)", err); return; } @@ -203,40 +208,36 @@ static void test_central_main(void) /* Reconnect to the device after conn_callbacks unregistered */ err = bt_le_scan_start(BT_LE_SCAN_PASSIVE, device_found); if (err != 0) { - FAIL("Scanning failed to start (err %d)\n", err); + TEST_FAIL("Scanning failed to start (err %d)", err); } printk("Scanning successfully started\n"); k_sleep(K_SECONDS(1)); bt_conn_foreach(BT_CONN_TYPE_LE, connection_info, &conn_count); if (!conn_count) { - FAIL("Reconnect to peer device failed!"); + TEST_FAIL("Reconnect to peer device failed!"); } /* flag_is_connected not set means no conn_callbacks being called */ if (flag_is_connected) { - FAIL("Unregister conn_callback didn't work"); + TEST_FAIL("Unregister conn_callback didn't work"); } printk("Unregister connection callbacks succeed!\n"); err = bt_disable(); if (err != 0) { - FAIL("Bluetooth disable failed (err %d)\n", err); + TEST_FAIL("Bluetooth disable failed (err %d)", err); } printk("Bluetooth successfully disabled\n"); - PASS("Central device passed\n"); + TEST_PASS("Central device passed"); } static const struct bst_test_instance test_def[] = {{.test_id = "peripheral", .test_descr = "Peripheral device", - .test_pre_init_f = test_init, - .test_tick_f = test_tick, .test_main_f = test_peripheral_main}, {.test_id = "central", .test_descr = "Central device", - .test_pre_init_f = test_init, - .test_tick_f = test_tick, .test_main_f = test_central_main}, BSTEST_END_MARKER}; diff --git a/tests/bsim/bluetooth/host/security/ccc_update/CMakeLists.txt b/tests/bsim/bluetooth/host/security/ccc_update/CMakeLists.txt index fb2b3228eccd..be4416ced577 100644 --- a/tests/bsim/bluetooth/host/security/ccc_update/CMakeLists.txt +++ b/tests/bsim/bluetooth/host/security/ccc_update/CMakeLists.txt @@ -12,6 +12,9 @@ endif() find_package(Zephyr HINTS $ENV{ZEPHYR_BASE}) project(bsim_test_ccc_update) +add_subdirectory(${ZEPHYR_BASE}/tests/bsim/babblekit babblekit) +target_link_libraries(app PRIVATE babblekit) + target_sources(app PRIVATE src/main.c src/common.c diff --git a/tests/bsim/bluetooth/host/security/ccc_update/src/central.c b/tests/bsim/bluetooth/host/security/ccc_update/src/central.c index 8f2d2c17dd46..40adf5efb298 100644 --- a/tests/bsim/bluetooth/host/security/ccc_update/src/central.c +++ b/tests/bsim/bluetooth/host/security/ccc_update/src/central.c @@ -18,6 +18,8 @@ #include +#include "babblekit/testcase.h" +#include "babblekit/flags.h" #include "common.h" #include "settings.h" @@ -27,9 +29,9 @@ #define CLIENT_CLIENT_CHAN 0 #define SERVER_CLIENT_CHAN 1 -CREATE_FLAG(connected_flag); -CREATE_FLAG(disconnected_flag); -CREATE_FLAG(security_updated_flag); +static DEFINE_FLAG(connected_flag); +static DEFINE_FLAG(disconnected_flag); +static DEFINE_FLAG(security_updated_flag); #define BT_UUID_DUMMY_SERVICE BT_UUID_DECLARE_128(DUMMY_SERVICE_TYPE) #define BT_UUID_DUMMY_SERVICE_NOTIFY BT_UUID_DECLARE_128(DUMMY_SERVICE_NOTIFY_TYPE) @@ -38,7 +40,7 @@ static struct bt_conn *default_conn; static struct bt_conn_cb central_cb; -CREATE_FLAG(gatt_write_flag); +static DEFINE_FLAG(gatt_write_flag); static uint8_t gatt_write_att_err; static void gatt_write_cb(struct bt_conn *conn, uint8_t att_err, @@ -47,7 +49,7 @@ static void gatt_write_cb(struct bt_conn *conn, uint8_t att_err, gatt_write_att_err = att_err; if (att_err) { - FAIL("GATT write ATT error (err %d)\n", att_err); + TEST_FAIL("GATT write ATT error (err %d)", att_err); } SET_FLAG(gatt_write_flag); @@ -72,7 +74,7 @@ static int gatt_write(struct bt_conn *conn, uint16_t handle, const uint8_t *writ */ err = bt_gatt_write(conn, ¶ms); if (err) { - FAIL("GATT write failed (err %d)", err); + TEST_FAIL("GATT write failed (err %d)", err); } WAIT_FOR_FLAG(gatt_write_flag); @@ -87,7 +89,7 @@ static void ccc_subscribe(void) err = gatt_write(default_conn, CCC_HANDLE, &buf, sizeof(buf)); if (err) { - FAIL("Failed to subscribe (att err %d)", err); + TEST_FAIL("Failed to subscribe (att err %d)", err); } } @@ -98,7 +100,7 @@ static void ccc_unsubscribe(void) err = gatt_write(default_conn, CCC_HANDLE, &buf, sizeof(buf)); if (err) { - FAIL("Failed to unsubscribe (att err %d)", err); + TEST_FAIL("Failed to unsubscribe (att err %d)", err); } } @@ -114,13 +116,13 @@ static void device_found(const bt_addr_le_t *addr, int8_t rssi, uint8_t type, err = bt_le_scan_stop(); if (err) { - FAIL("Failed to stop scanner (err %d)\n", err); + TEST_FAIL("Failed to stop scanner (err %d)", err); } err = bt_conn_le_create(addr, BT_CONN_LE_CREATE_CONN, BT_LE_CONN_PARAM_DEFAULT, &default_conn); if (err) { - FAIL("Could not connect to peer: %s (err %d)\n", addr_str, err); + TEST_FAIL("Could not connect to peer: %s (err %d)", addr_str, err); } } @@ -133,7 +135,7 @@ static void connected(struct bt_conn *conn, uint8_t err) bt_addr_le_to_str(addr, addr_str, sizeof(addr_str)); if (err) { - FAIL("Failed to connect to %s (err %d)\n", addr_str, err); + TEST_FAIL("Failed to connect to %s (err %d)", addr_str, err); } LOG_DBG("Connected: %s", addr_str); @@ -181,7 +183,7 @@ static void start_scan(void) err = bt_le_scan_start(BT_LE_SCAN_PASSIVE, device_found); if (err) { - FAIL("Scanning failed to start (err %d)\n", err); + TEST_FAIL("Scanning failed to start (err %d)", err); } LOG_DBG("Scanning successfully started"); @@ -193,7 +195,7 @@ static void disconnect(void) err = bt_conn_disconnect(default_conn, BT_HCI_ERR_REMOTE_USER_TERM_CONN); if (err) { - FAIL("Disconnection failed (err %d)\n", err); + TEST_FAIL("Disconnection failed (err %d)", err); } WAIT_FOR_FLAG(disconnected_flag); @@ -213,7 +215,7 @@ static void connect_pair_subscribe(void) err = bt_conn_set_security(default_conn, BT_SECURITY_L2); if (err != 0) { - FAIL("Failed to set security (err %d)\n", err); + TEST_FAIL("Failed to set security (err %d)", err); } WAIT_FOR_FLAG(security_updated_flag); @@ -259,7 +261,7 @@ static void connect_restore_sec(void) err = bt_conn_set_security(default_conn, BT_SECURITY_L2); if (err != 0) { - FAIL("Failed to set security (err %d)\n", err); + TEST_FAIL("Failed to set security (err %d)", err); } WAIT_FOR_FLAG(security_updated_flag); @@ -293,7 +295,7 @@ void central_backchannel_init(void) LOG_DBG("Opening back channels for device %d", device_number); ch = bs_open_back_channel(device_number, device_numbers, channel_numbers, num_ch); if (!ch) { - FAIL("Unable to open backchannel\n"); + TEST_FAIL("Unable to open backchannel"); } LOG_DBG("Back channels for device %d opened", device_number); } @@ -320,7 +322,7 @@ void run_central(void) err = bt_enable(NULL); if (err) { - FAIL("Bluetooth init failed (err %d)\n", err); + TEST_FAIL("Bluetooth init failed (err %d)", err); } LOG_DBG("Bluetooth initialized"); @@ -329,12 +331,12 @@ void run_central(void) err = settings_load(); if (err) { - FAIL("Settings load failed (err %d)\n", err); + TEST_FAIL("Settings load failed (err %d)", err); } err = bt_unpair(BT_ID_DEFAULT, BT_ADDR_LE_ANY); if (err) { - FAIL("Unpairing failed (err %d)\n", err); + TEST_FAIL("Unpairing failed (err %d)", err); } connect_pair_subscribe(); @@ -347,7 +349,7 @@ void run_central(void) connect_restore_sec(); disconnect(); - PASS("Central test passed\n"); + TEST_PASS("Central test passed"); } void run_bad_central(void) @@ -366,7 +368,7 @@ void run_bad_central(void) err = bt_enable(NULL); if (err) { - FAIL("Bluetooth init failed (err %d)\n"); + TEST_FAIL("Bluetooth init failed (err %d)"); } LOG_DBG("Bluetooth initialized"); @@ -375,13 +377,13 @@ void run_bad_central(void) err = settings_load(); if (err) { - FAIL("Settings load failed (err %d)\n"); + TEST_FAIL("Settings load failed (err %d)"); } connect_unsubscribe(); disconnect(); - PASS("Bad Central test passed\n"); + TEST_PASS("Bad Central test passed"); /* tell the good client that we disconnected from the server */ backchannel_sync_send(CLIENT_CLIENT_CHAN, GOOD_CLIENT_ID); diff --git a/tests/bsim/bluetooth/host/security/ccc_update/src/common.h b/tests/bsim/bluetooth/host/security/ccc_update/src/common.h index 4166fa2b6aa0..2a004e40ec10 100644 --- a/tests/bsim/bluetooth/host/security/ccc_update/src/common.h +++ b/tests/bsim/bluetooth/host/security/ccc_update/src/common.h @@ -10,30 +10,6 @@ #include #include "bs_tracing.h" -#include "bstests.h" - -#define FAIL(...) \ - do { \ - bst_result = Failed; \ - bs_trace_error_time_line(__VA_ARGS__); \ - } while (0) - -#define PASS(...) \ - do { \ - bst_result = Passed; \ - bs_trace_info_time(1, __VA_ARGS__); \ - } while (0) - -extern enum bst_result_t bst_result; - -#define CREATE_FLAG(flag) static atomic_t flag = (atomic_t) false -#define SET_FLAG(flag) (void)atomic_set(&flag, (atomic_t) true) -#define GET_FLAG(flag) (bool)atomic_get(&flag) -#define UNSET_FLAG(flag) (void)atomic_set(&flag, (atomic_t) false) -#define WAIT_FOR_FLAG(flag) \ - while (!(bool)atomic_get(&flag)) { \ - (void)k_sleep(K_MSEC(1)); \ - } LOG_MODULE_DECLARE(bt_bsim_ccc_update, LOG_LEVEL_DBG); diff --git a/tests/bsim/bluetooth/host/security/ccc_update/src/main.c b/tests/bsim/bluetooth/host/security/ccc_update/src/main.c index 4455e09d9f2a..2ba29e07b681 100644 --- a/tests/bsim/bluetooth/host/security/ccc_update/src/main.c +++ b/tests/bsim/bluetooth/host/security/ccc_update/src/main.c @@ -3,33 +3,12 @@ */ #include - -#include "bs_types.h" -#include "bs_tracing.h" #include "bstests.h" -#include #include LOG_MODULE_REGISTER(bt_bsim_ccc_update, LOG_LEVEL_DBG); -#define FAIL(...) \ - do { \ - bst_result = Failed; \ - bs_trace_error_time_line(__VA_ARGS__); \ - } while (0) - -#define PASS(...) \ - do { \ - bst_result = Passed; \ - bs_trace_info_time(1, __VA_ARGS__); \ - } while (0) - -extern enum bst_result_t bst_result; - -#define WAIT_TIME_S 60 -#define WAIT_TIME (WAIT_TIME_S * 1e6) - extern void run_peripheral(void); extern void run_central(void); extern void run_bad_central(void); @@ -49,40 +28,20 @@ static void peripheral_main(void) run_peripheral(); } -void test_tick(bs_time_t HW_device_time) -{ - if (bst_result != Passed) { - bst_result = Failed; - bs_trace_error_time_line("Test failed (not passed after %d seconds)\n", - WAIT_TIME_S); - } -} - -static void test_ccc_update_init(void) -{ - bst_ticker_set_next_tick_absolute(WAIT_TIME); -} - static const struct bst_test_instance test_def[] = { { .test_id = "central", .test_descr = "Central device", - .test_pre_init_f = test_ccc_update_init, - .test_tick_f = test_tick, .test_main_f = central_main, }, { .test_id = "bad_central", .test_descr = "Bad Central device", - .test_pre_init_f = test_ccc_update_init, - .test_tick_f = test_tick, .test_main_f = bad_central_main, }, { .test_id = "peripheral", .test_descr = "Peripheral device", - .test_pre_init_f = test_ccc_update_init, - .test_tick_f = test_tick, .test_main_f = peripheral_main, }, BSTEST_END_MARKER}; diff --git a/tests/bsim/bluetooth/host/security/ccc_update/src/peripheral.c b/tests/bsim/bluetooth/host/security/ccc_update/src/peripheral.c index 0cae51ba2d96..dae03f846a28 100644 --- a/tests/bsim/bluetooth/host/security/ccc_update/src/peripheral.c +++ b/tests/bsim/bluetooth/host/security/ccc_update/src/peripheral.c @@ -16,6 +16,8 @@ #include +#include "babblekit/testcase.h" +#include "babblekit/flags.h" #include "common.h" #include "settings.h" @@ -25,11 +27,11 @@ #define GOOD_CLIENT_CHAN 0 #define BAD_CLIENT_CHAN 1 -CREATE_FLAG(connected_flag); -CREATE_FLAG(disconnected_flag); -CREATE_FLAG(security_updated_flag); +static DEFINE_FLAG(connected_flag); +static DEFINE_FLAG(disconnected_flag); +static DEFINE_FLAG(security_updated_flag); -CREATE_FLAG(ccc_cfg_changed_flag); +static DEFINE_FLAG(ccc_cfg_changed_flag); static const struct bt_uuid_128 dummy_service = BT_UUID_INIT_128(DUMMY_SERVICE_TYPE); @@ -72,7 +74,7 @@ static void create_adv(struct bt_le_ext_adv **adv) err = bt_le_ext_adv_create(¶ms, NULL, adv); if (err) { - FAIL("Failed to create advertiser (%d)\n", err); + TEST_FAIL("Failed to create advertiser (%d)", err); } } @@ -89,7 +91,7 @@ static void start_adv(struct bt_le_ext_adv *adv) err = bt_le_ext_adv_start(adv, &start_params); if (err) { - FAIL("Failed to start advertiser (err %d)\n", err); + TEST_FAIL("Failed to start advertiser (err %d)", err); } LOG_DBG("Advertiser started"); @@ -101,7 +103,7 @@ static void stop_adv(struct bt_le_ext_adv *adv) err = bt_le_ext_adv_stop(adv); if (err) { - FAIL("Failed to stop advertiser (err %d)\n", err); + TEST_FAIL("Failed to stop advertiser (err %d)", err); } } @@ -112,7 +114,7 @@ static void connected(struct bt_conn *conn, uint8_t err) bt_addr_le_to_str(bt_conn_get_dst(conn), addr_str, sizeof(addr_str)); if (err) { - FAIL("Failed to connect to %s (err %d)\n", addr_str, err); + TEST_FAIL("Failed to connect to %s (err %d)", addr_str, err); } LOG_DBG("Connected: %s", addr_str); @@ -176,7 +178,7 @@ static void connect_pair_check_subscribtion(struct bt_le_ext_adv *adv) /* check that subscribtion request did not fail */ if (!is_peer_subscribed(default_conn)) { - FAIL("Good client did not subscribed\n"); + TEST_FAIL("Good client did not subscribed"); } stop_adv(adv); @@ -197,7 +199,7 @@ static void connect_wait_unsubscribtion(struct bt_le_ext_adv *adv) /* check that subscribtion is restored for bad client */ if (!is_peer_subscribed(default_conn)) { - FAIL("Subscribtion has not been restored for bad client\n"); + TEST_FAIL("Subscribtion has not been restored for bad client"); } /* confirm to bad client that the subscribtion had not been restored */ @@ -206,8 +208,8 @@ static void connect_wait_unsubscribtion(struct bt_le_ext_adv *adv) backchannel_sync_wait(BAD_CLIENT_CHAN, BAD_CLIENT_ID); /* check that unsubscribtion request didn't fail */ - if (!GET_FLAG(ccc_cfg_changed_flag)) { - FAIL("Bad client didn't manage to update CCC config\n"); + if (!IS_FLAG_SET(ccc_cfg_changed_flag)) { + TEST_FAIL("Bad client didn't manage to update CCC config"); } /* confirm to bad client that unsubscribtion request has been well registered */ @@ -228,7 +230,7 @@ static void connect_restore_sec_check_subscribtion(struct bt_le_ext_adv *adv) /* check that subscribtion hasn't been restored */ if (is_peer_subscribed(default_conn)) { - FAIL("Good client is subscribed\n"); + TEST_FAIL("Good client is subscribed"); } /* confirm to good client that the subscribtion has been well restored */ @@ -238,7 +240,7 @@ static void connect_restore_sec_check_subscribtion(struct bt_le_ext_adv *adv) /* check that unsubscribtion request from good client has been registered */ if (is_peer_subscribed(default_conn)) { - FAIL("Good client did not unsubscribe\n"); + TEST_FAIL("Good client did not unsubscribe"); } } @@ -261,7 +263,7 @@ void peripheral_backchannel_init(void) LOG_DBG("Opening back channels for device %d", device_number); ch = bs_open_back_channel(device_number, device_numbers, channel_numbers, num_ch); if (!ch) { - FAIL("Unable to open backchannel\n"); + TEST_FAIL("Unable to open backchannel"); } } @@ -305,7 +307,7 @@ void run_peripheral(void) err = bt_enable(NULL); if (err) { - FAIL("Bluetooth init failed (err %d)\n", err); + TEST_FAIL("Bluetooth init failed (err %d)", err); } LOG_DBG("Bluetooth initialized"); @@ -316,12 +318,12 @@ void run_peripheral(void) err = settings_load(); if (err) { - FAIL("Settings load failed (err %d)\n", err); + TEST_FAIL("Settings load failed (err %d)", err); } err = bt_unpair(BT_ID_DEFAULT, BT_ADDR_LE_ANY); if (err) { - FAIL("Unpairing failed (err %d)\n", err); + TEST_FAIL("Unpairing failed (err %d)", err); } create_adv(&adv); @@ -335,5 +337,5 @@ void run_peripheral(void) connect_restore_sec_check_subscribtion(adv); WAIT_FOR_FLAG(disconnected_flag); - PASS("Peripheral test passed\n"); + TEST_PASS("Peripheral test passed"); } From 9c4c86c489fd7837ad2a7fb8852339fe09a7c3bb Mon Sep 17 00:00:00 2001 From: Emil Gydesen Date: Mon, 20 Jan 2025 20:46:36 +0100 Subject: [PATCH 15/49] [nrf fromtree] tests: Bluetooth: Tester: Reorder btp_buf to fix variable array The `btp_hdr` needs to be last in the struct, as it has a variable length array (data[]). Signed-off-by: Emil Gydesen (cherry picked from commit 176676d81fdc44a62fe6f5ffe857d0f521ee1f24) Signed-off-by: alperen sener --- tests/bluetooth/tester/src/btp.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/bluetooth/tester/src/btp.c b/tests/bluetooth/tester/src/btp.c index 6bc7a4d5e3af..ca95e18a4750 100644 --- a/tests/bluetooth/tester/src/btp.c +++ b/tests/bluetooth/tester/src/btp.c @@ -31,11 +31,11 @@ static struct k_thread cmd_thread; #define CMD_QUEUED 2 struct btp_buf { intptr_t _reserved; + uint8_t rsp[BTP_MTU]; union { uint8_t data[BTP_MTU]; struct btp_hdr hdr; }; - uint8_t rsp[BTP_MTU]; }; static struct btp_buf cmd_buf[CMD_QUEUED]; From eb38d18eb505a9c1ccb5e4ba4da65e47243776e3 Mon Sep 17 00:00:00 2001 From: Aleksandr Khromykh Date: Mon, 20 Jan 2025 16:17:43 +0100 Subject: [PATCH 16/49] [nrf fromtree] Bluetooth: Mesh: remove experimental flag from mbedtls support Commit removes experimental flag from mbedtls psa crypto library support. Signed-off-by: Aleksandr Khromykh (cherry picked from commit a8e540c371e077fbbff53d5de7da09a0e99cca30) Signed-off-by: alperen sener --- subsys/bluetooth/mesh/Kconfig | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/subsys/bluetooth/mesh/Kconfig b/subsys/bluetooth/mesh/Kconfig index 193c75dea12e..0b69688a9eeb 100644 --- a/subsys/bluetooth/mesh/Kconfig +++ b/subsys/bluetooth/mesh/Kconfig @@ -1492,8 +1492,7 @@ config BT_MESH_USES_TINYCRYPT Use TinyCrypt library to perform crypto operations. config BT_MESH_USES_MBEDTLS_PSA - bool "mbed TLS PSA [EXPERIMENTAL]" - select EXPERIMENTAL + bool "mbed TLS PSA" select MBEDTLS select MBEDTLS_PSA_CRYPTO_C select PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_IMPORT @@ -1503,7 +1502,6 @@ config BT_MESH_USES_MBEDTLS_PSA select PSA_WANT_ALG_CMAC select PSA_WANT_ALG_ECB_NO_PADDING select PSA_WANT_KEY_TYPE_AES - imply MBEDTLS_AES_ROM_TABLES select PSA_WANT_ALG_CCM select PSA_WANT_KEY_TYPE_HMAC select PSA_WANT_ALG_HMAC @@ -1511,10 +1509,9 @@ config BT_MESH_USES_MBEDTLS_PSA select PSA_WANT_ALG_ECDH select PSA_WANT_ECC_SECP_R1_256 select BT_MESH_SECURE_STORAGE if BT_SETTINGS + imply MBEDTLS_AES_ROM_TABLES help - Use Mbed TLS as PSA Crypto API provider. This is useful on platforms - that do not support TF-M. - This feature is experimental and only BabbleSim tests were run. + Use Mbed TLS as PSA Crypto API provider. config BT_MESH_USES_TFM_PSA bool "Use TF-M PSA [EXPERIMENTAL]" From a7732ac0b03ec6a4827245173b399922cc042e73 Mon Sep 17 00:00:00 2001 From: Aleksandr Khromykh Date: Mon, 20 Jan 2025 16:36:38 +0100 Subject: [PATCH 17/49] =?UTF-8?q?[nrf=20fromtree]=C2=A0doc:=20update=20mig?= =?UTF-8?q?ration=20guide=20with=20mesh=20dependency?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Commit adds description of the mesh dependency on the secure storage subsytem. Signed-off-by: Aleksandr Khromykh (cherry picked from commit f9830452e58ca30414a542ecf5ec7b73d9e7f6d8) Signed-off-by: alperen sener --- doc/releases/migration-guide-4.1.rst | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/doc/releases/migration-guide-4.1.rst b/doc/releases/migration-guide-4.1.rst index b5a5c6a58cbe..70a168c6de5a 100644 --- a/doc/releases/migration-guide-4.1.rst +++ b/doc/releases/migration-guide-4.1.rst @@ -305,6 +305,11 @@ Bluetooth Mesh set as deprecated. Default option for platforms that do not support TF-M is :kconfig:option:`CONFIG_BT_MESH_USES_MBEDTLS_PSA`. +* Mesh explicitly depends on the Secure Storage subsystem if storing into + non-volatile memory (:kconfig:option:`CONFIG_BT_SETTINGS`) is enabled and + Mbed TLS library (:kconfig:option:`CONFIG_BT_MESH_USES_MBEDTLS_PSA`) is used. + Applications should be built with :kconfig:option:`CONFIG_SECURE_STORAGE` enabled. + Bluetooth Audio =============== From 2ec2f0f00d3ff89313aa556517956f9ed22defe1 Mon Sep 17 00:00:00 2001 From: Dominik Ermel Date: Sun, 1 Dec 2024 21:16:46 +0100 Subject: [PATCH 18/49] [nrf fromtree] Bluetooth: Mesh: Improve logic for serving devices erase capabilities The commit fixes issue where flash_area_flatten has been used where code was only supposed to erase devices by hardware requirement prior to write, by replacing the call with flash_area_erase and supporting logic to select proper path. There have been following Kconfig options added: - CONFIG_BT_MESH_BLOB_IO_FLASH_WITHOUT_ERASE - CONFIG_BT_MESH_BLOB_IO_FLASH_WITH_ERASE that are available for user depending on devices in the system and allow to turn off paths that are not used by BLOB IO; for example if user never writes to device with erase CONFIG_BT_MESH_BLOB_IO_FLASH_WITH_ERASE will disable the path. Both Kconfig options are y by default and enable all paths. Signed-off-by: Dominik Ermel (cherry picked from commit 49f5598835f06304f958885028ffc239d6db3ecd) Signed-off-by: alperen sener --- subsys/bluetooth/mesh/Kconfig | 24 ++++++++++ subsys/bluetooth/mesh/blob_io_flash.c | 63 +++++++++++++++++---------- 2 files changed, 63 insertions(+), 24 deletions(-) diff --git a/subsys/bluetooth/mesh/Kconfig b/subsys/bluetooth/mesh/Kconfig index 0b69688a9eeb..13d892aa4379 100644 --- a/subsys/bluetooth/mesh/Kconfig +++ b/subsys/bluetooth/mesh/Kconfig @@ -1055,6 +1055,30 @@ config BT_MESH_BLOB_IO_FLASH Enable the BLOB flash stream for reading and writing BLOBs directly to and from flash. +if BT_MESH_BLOB_IO_FLASH + +config BT_MESH_BLOB_IO_FLASH_WITHOUT_ERASE + bool "BLOB flash support for devices without erase" + default y if FLASH_HAS_NO_EXPLICIT_ERASE + depends on FLASH_HAS_NO_EXPLICIT_ERASE + help + Enable path supporting devices without erase. This option appears only + if there are devices without explicit erase requirements in the system + and may be disabled to reduce code size in case when no operations + are intended on such type of devices. + +config BT_MESH_BLOB_IO_FLASH_WITH_ERASE + bool "BLOB flash support for devices with erase" + default y if FLASH_HAS_EXPLICIT_ERASE + depends on FLASH_HAS_EXPLICIT_ERASE + help + Enable path supporting devices with erase. This option appears only + if there are devices requiring erase, before write, in the system + and may be disabled to reduce code size in case when no operations + are intended on such type of devices. + +endif # BT_MESH_BLOB_IO_FLASH + config BT_MESH_DFU_SRV bool "Support for Firmware Update Server model" depends on BT_MESH_MODEL_EXTENSIONS diff --git a/subsys/bluetooth/mesh/blob_io_flash.c b/subsys/bluetooth/mesh/blob_io_flash.c index eb0b3342d3ed..8d989a6feca5 100644 --- a/subsys/bluetooth/mesh/blob_io_flash.c +++ b/subsys/bluetooth/mesh/blob_io_flash.c @@ -56,40 +56,55 @@ static void io_close(const struct bt_mesh_blob_io *io, flash_area_close(flash->area); } +static inline int erase_device_block(const struct flash_area *fa, off_t start, size_t size) +{ + /* If there are no devices requiring erase, then there is nothing to do */ + if (IS_ENABLED(CONFIG_BT_MESH_BLOB_IO_FLASH_WITH_ERASE)) { + const struct device *fdev = flash_area_get_device(fa); + + if (!fdev) { + return -ENODEV; + } + + /* We have a mix of devices in system */ + if (IS_ENABLED(CONFIG_BT_MESH_BLOB_IO_FLASH_WITHOUT_ERASE)) { + const struct flash_parameters *fparam = flash_get_parameters(fdev); + + /* If device has no erase requirement then do nothing */ + if (!(flash_params_get_erase_cap(fparam) & FLASH_ERASE_C_EXPLICIT)) { + return 0; + } + } + + if (IS_ENABLED(CONFIG_FLASH_PAGE_LAYOUT)) { + struct flash_pages_info page; + int err; + + err = flash_get_page_info_by_offs(fdev, start, &page); + if (err) { + return err; + } + + size = page.size * DIV_ROUND_UP(size, page.size); + start = page.start_offset; + } + return flash_area_erase(fa, start, size); + } + + return 0; +} + static int block_start(const struct bt_mesh_blob_io *io, const struct bt_mesh_blob_xfer *xfer, const struct bt_mesh_blob_block *block) { struct bt_mesh_blob_io_flash *flash = FLASH_IO(io); - size_t erase_size; if (flash->mode == BT_MESH_BLOB_READ) { return 0; } -#if defined(CONFIG_FLASH_PAGE_LAYOUT) - struct flash_pages_info page; - const struct device *flash_dev; - int err; - - flash_dev = flash_area_get_device(flash->area); - if (!flash_dev) { - return -ENODEV; - } - - err = flash_get_page_info_by_offs(flash_dev, - flash->offset + block->offset, &page); - if (err) { - return err; - } - - erase_size = page.size * DIV_ROUND_UP(block->size, page.size); -#else - erase_size = block->size; -#endif - - return flash_area_flatten(flash->area, flash->offset + block->offset, - erase_size); + return erase_device_block(flash->area, flash->offset + block->offset, block->size); } static int rd_chunk(const struct bt_mesh_blob_io *io, From 3a23e86984f50a8bc797562605976a1005909ded Mon Sep 17 00:00:00 2001 From: Johan Hedberg Date: Mon, 27 Jan 2025 08:08:28 +0200 Subject: [PATCH 19/49] [nrf fromtree] Bluetooth: Host: Remove leftover HCI ECC command checks The code shouldn't be checking for HCI command support anymore, rather in the case of debug keys we can just start immediately using them. Fixes commit 09e86f3b69b3112b96d6f89bf388d1ced982aef9. Signed-off-by: Johan Hedberg (cherry picked from commit 3ae8a9cfe11cedafe0dc6f0a9d321edd5fba58c0) Signed-off-by: alperen sener --- subsys/bluetooth/host/ecc.c | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/subsys/bluetooth/host/ecc.c b/subsys/bluetooth/host/ecc.c index 749ffc1d8aa0..a7354c20e37a 100644 --- a/subsys/bluetooth/host/ecc.c +++ b/subsys/bluetooth/host/ecc.c @@ -252,14 +252,10 @@ int bt_pub_key_gen(struct bt_pub_key_cb *new_cb) struct bt_pub_key_cb *cb; if (IS_ENABLED(CONFIG_BT_USE_DEBUG_KEYS)) { - if (!BT_CMD_TEST(bt_dev.supported_commands, 41, 2)) { - LOG_WRN("ECC Debug keys HCI command not available"); - } else { - atomic_set_bit(bt_dev.flags, BT_DEV_HAS_PUB_KEY); - __ASSERT_NO_MSG(new_cb->func != NULL); - new_cb->func(debug_public_key); - return 0; - } + atomic_set_bit(bt_dev.flags, BT_DEV_HAS_PUB_KEY); + __ASSERT_NO_MSG(new_cb->func != NULL); + new_cb->func(debug_public_key); + return 0; } if (!new_cb) { @@ -312,8 +308,7 @@ void bt_pub_key_hci_disrupted(void) const uint8_t *bt_pub_key_get(void) { - if (IS_ENABLED(CONFIG_BT_USE_DEBUG_KEYS) && - BT_CMD_TEST(bt_dev.supported_commands, 41, 2)) { + if (IS_ENABLED(CONFIG_BT_USE_DEBUG_KEYS)) { return debug_public_key; } From a148e59ac94ce33858ad5d4b9a65f6f739917086 Mon Sep 17 00:00:00 2001 From: Szymon Janc Date: Tue, 21 Jan 2025 10:42:10 +0100 Subject: [PATCH 20/49] [nrf fromtree] tests: Bluetooth: Tester: Avoid union with packed structure This may cause some compiler warnings due to flexible array being used in btp_hdr. Simply avoid it and do explicit pointer type cast where needed. Signed-off-by: Szymon Janc (cherry picked from commit aff2875d214dfcac3aa657904548369124f73781) Signed-off-by: alperen sener --- tests/bluetooth/tester/src/btp.c | 29 ++++++++++++++--------------- 1 file changed, 14 insertions(+), 15 deletions(-) diff --git a/tests/bluetooth/tester/src/btp.c b/tests/bluetooth/tester/src/btp.c index ca95e18a4750..0ce11e3fb83c 100644 --- a/tests/bluetooth/tester/src/btp.c +++ b/tests/bluetooth/tester/src/btp.c @@ -31,11 +31,8 @@ static struct k_thread cmd_thread; #define CMD_QUEUED 2 struct btp_buf { intptr_t _reserved; - uint8_t rsp[BTP_MTU]; - union { - uint8_t data[BTP_MTU]; - struct btp_hdr hdr; - }; + uint8_t data[BTP_MTU]; /* includes btp header */ + uint8_t rsp[BTP_DATA_MAX_SIZE]; }; static struct btp_buf cmd_buf[CMD_QUEUED]; @@ -90,25 +87,27 @@ static void cmd_handler(void *p1, void *p2, void *p3) while (1) { const struct btp_handler *btp; struct btp_buf *cmd; + struct btp_hdr *hdr; uint8_t status; uint16_t rsp_len = 0; uint16_t len; cmd = k_fifo_get(&cmds_queue, K_FOREVER); + hdr = (struct btp_hdr *)cmd->data; - LOG_DBG("cmd service 0x%02x opcode 0x%02x index 0x%02x", cmd->hdr.service, - cmd->hdr.opcode, cmd->hdr.index); + LOG_DBG("cmd service 0x%02x opcode 0x%02x index 0x%02x", + hdr->service, hdr->opcode, hdr->index); - len = sys_le16_to_cpu(cmd->hdr.len); + len = sys_le16_to_cpu(hdr->len); - btp = find_btp_handler(cmd->hdr.service, cmd->hdr.opcode); + btp = find_btp_handler(hdr->service, hdr->opcode); if (btp) { - if (btp->index != cmd->hdr.index) { + if (btp->index != hdr->index) { status = BTP_STATUS_FAILED; } else if ((btp->expect_len >= 0) && (btp->expect_len != len)) { status = BTP_STATUS_FAILED; } else { - status = btp->func(cmd->hdr.data, len, + status = btp->func(hdr->data, len, cmd->rsp, &rsp_len); } @@ -127,11 +126,11 @@ static void cmd_handler(void *p1, void *p2, void *p3) } if ((status == BTP_STATUS_SUCCESS) && rsp_len > 0) { - tester_send_with_index(cmd->hdr.service, cmd->hdr.opcode, - cmd->hdr.index, cmd->rsp, rsp_len); + tester_send_with_index(hdr->service, hdr->opcode, + hdr->index, cmd->rsp, rsp_len); } else { - tester_rsp_with_index(cmd->hdr.service, cmd->hdr.opcode, - cmd->hdr.index, status); + tester_rsp_with_index(hdr->service, hdr->opcode, + hdr->index, status); } (void)memset(cmd, 0, sizeof(*cmd)); From 02fb2eed243a8661b3ea8e65597d18d61ac887b1 Mon Sep 17 00:00:00 2001 From: Szymon Janc Date: Fri, 24 Jan 2025 14:05:12 +0100 Subject: [PATCH 21/49] =?UTF-8?q?[nrf=20fromtree]=C2=A0tests:=20Bluetooth:?= =?UTF-8?q?=20Tester:=20Improve=20BTP=20MTU=20validation?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Make sure MTU is validated to avoid reading pass command buffer. Also make more explicit check in response length validation. Signed-off-by: Szymon Janc (cherry picked from commit 24abac1207c4b8edecb029d909bf56640e373549) Signed-off-by: alperen sener --- tests/bluetooth/tester/src/btp.c | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/tests/bluetooth/tester/src/btp.c b/tests/bluetooth/tester/src/btp.c index 0ce11e3fb83c..2ba92bf82148 100644 --- a/tests/bluetooth/tester/src/btp.c +++ b/tests/bluetooth/tester/src/btp.c @@ -90,7 +90,6 @@ static void cmd_handler(void *p1, void *p2, void *p3) struct btp_hdr *hdr; uint8_t status; uint16_t rsp_len = 0; - uint16_t len; cmd = k_fifo_get(&cmds_queue, K_FOREVER); hdr = (struct btp_hdr *)cmd->data; @@ -98,11 +97,13 @@ static void cmd_handler(void *p1, void *p2, void *p3) LOG_DBG("cmd service 0x%02x opcode 0x%02x index 0x%02x", hdr->service, hdr->opcode, hdr->index); - len = sys_le16_to_cpu(hdr->len); - btp = find_btp_handler(hdr->service, hdr->opcode); if (btp) { - if (btp->index != hdr->index) { + uint16_t len = sys_le16_to_cpu(hdr->len); + + if (len > BTP_DATA_MAX_SIZE) { + status = BTP_STATUS_FAILED; + } else if (btp->index != hdr->index) { status = BTP_STATUS_FAILED; } else if ((btp->expect_len >= 0) && (btp->expect_len != len)) { status = BTP_STATUS_FAILED; @@ -111,7 +112,8 @@ static void cmd_handler(void *p1, void *p2, void *p3) cmd->rsp, &rsp_len); } - __ASSERT_NO_MSG((rsp_len + sizeof(struct btp_hdr)) <= BTP_MTU); + /* This means that caller likely overwrote rsp buffer */ + __ASSERT_NO_MSG(rsp_len <= BTP_DATA_MAX_SIZE); } else { status = BTP_STATUS_UNKNOWN_CMD; } From 1c6e459e31fcff33ec32c58294a2516cd124ceca Mon Sep 17 00:00:00 2001 From: Johan Hedberg Date: Mon, 27 Jan 2025 16:32:27 +0200 Subject: [PATCH 22/49] [nrf fromtree] Bluetooth: Host: Fix overwriting ECC error value Jump straight to the exit portion of the function in the case that psa_destroy_key() failed and we set err to a non-zero value. This also fixes Coverity CID 487701 "Code maintainability issues (UNUSED_VALUE)". Signed-off-by: Johan Hedberg (cherry picked from commit 8b356fd9fbc5928d7cb54d788ee516ca0c1eed05) Signed-off-by: alperen sener --- subsys/bluetooth/host/ecc.c | 1 + 1 file changed, 1 insertion(+) diff --git a/subsys/bluetooth/host/ecc.c b/subsys/bluetooth/host/ecc.c index a7354c20e37a..6efc4f2baa9e 100644 --- a/subsys/bluetooth/host/ecc.c +++ b/subsys/bluetooth/host/ecc.c @@ -220,6 +220,7 @@ static void generate_dh_key(struct k_work *work) if (ret != PSA_SUCCESS) { LOG_ERR("Failed to destroy the key %d", ret); err = -EIO; + goto exit; } err = 0; From 82b05ee90e2fdf152d875081014b9cf9f2dde08c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9o=20Battrel?= Date: Fri, 10 Jan 2025 12:02:40 +0100 Subject: [PATCH 23/49] =?UTF-8?q?[nrf=20fromtree]=C2=A0Bluetooth:=20Host:?= =?UTF-8?q?=20Specify=20parameter=20needs=20of=20`bt=5Fset=5Fname`?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit State that the name given to `bt_set_name` must be null terminated. If not, the behavior of the function may be wrong as it's using `strlen` to get the length of the given name and `strcmp` to compare it to the current device name. Both of those functions expect null terminated string. Signed-off-by: Théo Battrel (cherry picked from commit 7884b929fe7635dcde8f68bb60ae2018a572f1fe) Signed-off-by: alperen sener --- include/zephyr/bluetooth/bluetooth.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/zephyr/bluetooth/bluetooth.h b/include/zephyr/bluetooth/bluetooth.h index 1b5576d94669..d1618f98a3a2 100644 --- a/include/zephyr/bluetooth/bluetooth.h +++ b/include/zephyr/bluetooth/bluetooth.h @@ -268,7 +268,7 @@ bool bt_is_ready(void); * * @sa @kconfig{CONFIG_BT_DEVICE_NAME_MAX}. * - * @param name New name + * @param name New name, must be null terminated * * @return Zero on success or (negative) error code otherwise. */ From b48d4e9a843c4a3a8f51ae22e017c705d15fb127 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9o=20Battrel?= Date: Wed, 15 Jan 2025 08:50:23 +0100 Subject: [PATCH 24/49] [nrf fromtree] Bluetooth: Host: GAP Device Name write now add null char at the end MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The function used to write the value of the GAP Device Name characteristic now ensure that the string passed to `bt_set_name` is null terminated. Also fix a wrong offset calculation. The function used to write the value of the GAP Device Name characteristic was returning an error when the offset + the length of data to write was superior **or equal** to the maximum size of the device name. This caused the actual maximum device name size to be reduced by 1 byte. Signed-off-by: Théo Battrel (cherry picked from commit 972d4c7369bc9c2a4af3274768b4d6e74bb4141f) Signed-off-by: alperen sener --- subsys/bluetooth/host/gatt.c | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/subsys/bluetooth/host/gatt.c b/subsys/bluetooth/host/gatt.c index c9a056bb2dfa..7ea10083d0fe 100644 --- a/subsys/bluetooth/host/gatt.c +++ b/subsys/bluetooth/host/gatt.c @@ -101,22 +101,24 @@ static ssize_t read_name(struct bt_conn *conn, const struct bt_gatt_attr *attr, #if defined(CONFIG_BT_DEVICE_NAME_GATT_WRITABLE) -static ssize_t write_name(struct bt_conn *conn, const struct bt_gatt_attr *attr, - const void *buf, uint16_t len, uint16_t offset, - uint8_t flags) +static ssize_t write_name(struct bt_conn *conn, const struct bt_gatt_attr *attr, const void *buf, + uint16_t len, uint16_t offset, uint8_t flags) { - char value[CONFIG_BT_DEVICE_NAME_MAX] = {}; + /* adding one to fit the terminating null character */ + char value[CONFIG_BT_DEVICE_NAME_MAX + 1] = {}; - if (offset >= sizeof(value)) { + if (offset >= CONFIG_BT_DEVICE_NAME_MAX) { return BT_GATT_ERR(BT_ATT_ERR_INVALID_OFFSET); } - if (offset + len >= sizeof(value)) { + if (offset + len > CONFIG_BT_DEVICE_NAME_MAX) { return BT_GATT_ERR(BT_ATT_ERR_INVALID_ATTRIBUTE_LEN); } memcpy(value, buf, len); + value[len] = '\0'; + bt_set_name(value); return len; From 487fe6df7d21be1aefc0a8aefc4c858d3e79e4cb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9o=20Battrel?= Date: Wed, 15 Jan 2025 12:41:47 +0100 Subject: [PATCH 25/49] =?UTF-8?q?[nrf=20fromtree]=C2=A0Tests:=20Bluetooth:?= =?UTF-8?q?=20Add=20MTU=20exchange=20procedure=20in=20testlib?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add `bt_testlib_att_exchange_mtu` function to simplify the procedure. Signed-off-by: Théo Battrel (cherry picked from commit e3cf59bfd31f36533fe397ddc55fae5d57f28fec) Signed-off-by: alperen sener --- tests/bluetooth/common/testlib/CMakeLists.txt | 1 + .../common/testlib/include/testlib/att.h | 15 ++++ tests/bluetooth/common/testlib/src/att.c | 69 +++++++++++++++++++ 3 files changed, 85 insertions(+) create mode 100644 tests/bluetooth/common/testlib/include/testlib/att.h create mode 100644 tests/bluetooth/common/testlib/src/att.c diff --git a/tests/bluetooth/common/testlib/CMakeLists.txt b/tests/bluetooth/common/testlib/CMakeLists.txt index fa84121285d7..5f71212adf1a 100644 --- a/tests/bluetooth/common/testlib/CMakeLists.txt +++ b/tests/bluetooth/common/testlib/CMakeLists.txt @@ -15,6 +15,7 @@ target_include_directories(testlib PUBLIC target_sources(testlib PRIVATE src/adv.c + src/att.c src/att_read.c src/att_write.c src/conn_ref.c diff --git a/tests/bluetooth/common/testlib/include/testlib/att.h b/tests/bluetooth/common/testlib/include/testlib/att.h new file mode 100644 index 000000000000..f83b8b20e119 --- /dev/null +++ b/tests/bluetooth/common/testlib/include/testlib/att.h @@ -0,0 +1,15 @@ +/* + * Copyright (c) 2025 Nordic Semiconductor ASA + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#ifndef ZEPHYR_TESTS_BLUETOOTH_COMMON_TESTLIB_INCLUDE_TESTLIB_ATT_H_ +#define ZEPHYR_TESTS_BLUETOOTH_COMMON_TESTLIB_INCLUDE_TESTLIB_ATT_H_ + +#include +#include + +int bt_testlib_att_exchange_mtu(struct bt_conn *conn); + +#endif /* ZEPHYR_TESTS_BLUETOOTH_COMMON_TESTLIB_INCLUDE_TESTLIB_ATT_H_ */ diff --git a/tests/bluetooth/common/testlib/src/att.c b/tests/bluetooth/common/testlib/src/att.c new file mode 100644 index 000000000000..01c9a5b774c8 --- /dev/null +++ b/tests/bluetooth/common/testlib/src/att.c @@ -0,0 +1,69 @@ +/* + * Copyright (c) 2025 Nordic Semiconductor ASA + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include + +#include +#include +#include +#include + +#include + +LOG_MODULE_REGISTER(bt_testlib_att, LOG_LEVEL_DBG); + +static uint8_t exchange_mtu_err; +static K_MUTEX_DEFINE(exchange_mtu_lock); +static K_CONDVAR_DEFINE(exchange_mtu_done); + +static void bt_testlib_att_exchange_mtu_cb(struct bt_conn *conn, uint8_t err, + struct bt_gatt_exchange_params *params) +{ + char addr[BT_ADDR_LE_STR_LEN]; + + k_mutex_lock(&exchange_mtu_lock, K_FOREVER); + + bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr)); + + LOG_DBG("MTU exchange %s (%s)", err == 0U ? "successful" : "failed", addr); + + exchange_mtu_err = err; + + k_condvar_signal(&exchange_mtu_done); + + k_mutex_unlock(&exchange_mtu_lock); +} + +int bt_testlib_att_exchange_mtu(struct bt_conn *conn) +{ + int err; + struct bt_gatt_exchange_params exchange_mtu_params; + + __ASSERT_NO_MSG(conn != NULL); + + k_mutex_lock(&exchange_mtu_lock, K_FOREVER); + + exchange_mtu_err = 0; + + exchange_mtu_params.func = bt_testlib_att_exchange_mtu_cb; + + err = bt_gatt_exchange_mtu(conn, &exchange_mtu_params); + if (err != 0) { + LOG_ERR("Failed to exchange MTU (err %d)", err); + + k_mutex_unlock(&exchange_mtu_lock); + + return err; + } + + k_condvar_wait(&exchange_mtu_done, &exchange_mtu_lock, K_FOREVER); + + err = exchange_mtu_err; + + k_mutex_unlock(&exchange_mtu_lock); + + return err; +} From 78f87bea70f353721e317cac9ea92bb384d4f97b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9o=20Battrel?= Date: Fri, 10 Jan 2025 12:27:31 +0100 Subject: [PATCH 26/49] [nrf fromtree] Tests: Bluetooth: Add 'device_name' GATT test MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The goal of this test is to ensure that setting the device name by writing to the GAP 'Device Name' characteristic work as expected. Signed-off-by: Théo Battrel (cherry picked from commit 161222050cd02c92d2ff469f08208bd0521aeac0) Signed-off-by: alperen sener --- .../host/gatt/device_name/CMakeLists.txt | 24 +++ .../bluetooth/host/gatt/device_name/prj.conf | 35 ++++ .../host/gatt/device_name/src/client.c | 126 ++++++++++++++ .../host/gatt/device_name/src/common.h | 24 +++ .../host/gatt/device_name/src/main.c | 51 ++++++ .../host/gatt/device_name/src/server.c | 161 ++++++++++++++++++ .../test_scripts/run_device_name.sh | 28 +++ .../host/gatt/device_name/testcase.yaml | 10 ++ 8 files changed, 459 insertions(+) create mode 100644 tests/bsim/bluetooth/host/gatt/device_name/CMakeLists.txt create mode 100644 tests/bsim/bluetooth/host/gatt/device_name/prj.conf create mode 100644 tests/bsim/bluetooth/host/gatt/device_name/src/client.c create mode 100644 tests/bsim/bluetooth/host/gatt/device_name/src/common.h create mode 100644 tests/bsim/bluetooth/host/gatt/device_name/src/main.c create mode 100644 tests/bsim/bluetooth/host/gatt/device_name/src/server.c create mode 100755 tests/bsim/bluetooth/host/gatt/device_name/test_scripts/run_device_name.sh create mode 100644 tests/bsim/bluetooth/host/gatt/device_name/testcase.yaml diff --git a/tests/bsim/bluetooth/host/gatt/device_name/CMakeLists.txt b/tests/bsim/bluetooth/host/gatt/device_name/CMakeLists.txt new file mode 100644 index 000000000000..f8422e0256c3 --- /dev/null +++ b/tests/bsim/bluetooth/host/gatt/device_name/CMakeLists.txt @@ -0,0 +1,24 @@ +# SPDX-License-Identifier: Apache-2.0 + +cmake_minimum_required(VERSION 3.20.0) + +find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) + +project(device_name) + +add_subdirectory(${ZEPHYR_BASE}/tests/bluetooth/common/testlib testlib) +target_link_libraries(app PRIVATE testlib) + +add_subdirectory(${ZEPHYR_BASE}/tests/bsim/babblekit babblekit) +target_link_libraries(app PRIVATE babblekit) + +zephyr_include_directories( + ${BSIM_COMPONENTS_PATH}/libUtilv1/src/ + ${BSIM_COMPONENTS_PATH}/libPhyComv1/src/ +) + +target_sources(app PRIVATE + src/main.c + src/server.c + src/client.c +) diff --git a/tests/bsim/bluetooth/host/gatt/device_name/prj.conf b/tests/bsim/bluetooth/host/gatt/device_name/prj.conf new file mode 100644 index 000000000000..364cccfca48b --- /dev/null +++ b/tests/bsim/bluetooth/host/gatt/device_name/prj.conf @@ -0,0 +1,35 @@ +CONFIG_BT_TESTING=y + +CONFIG_BT=y +CONFIG_BT_CENTRAL=y +CONFIG_BT_PERIPHERAL=y + +CONFIG_BT_DEVICE_NAME_DYNAMIC=y +CONFIG_BT_DEVICE_NAME_GATT_WRITABLE=y + +# Dependency of testlib/adv and testlib/scan. +CONFIG_BT_EXT_ADV=y + +CONFIG_BT_AUTO_PHY_UPDATE=n +CONFIG_BT_GATT_AUTO_UPDATE_MTU=n +CONFIG_BT_AUTO_DATA_LEN_UPDATE=n +CONFIG_BT_GAP_AUTO_UPDATE_CONN_PARAMS=n + +CONFIG_BT_GATT_AUTO_DISCOVER_CCC=y +CONFIG_BT_GATT_AUTO_RESUBSCRIBE=n + +CONFIG_BT_SMP=y +CONFIG_BT_GATT_CLIENT=y +CONFIG_BT_GATT_DYNAMIC_DB=y +CONFIG_BT_GATT_CACHING=y + +CONFIG_LOG=y +CONFIG_ASSERT=y + +CONFIG_THREAD_NAME=y +CONFIG_LOG_THREAD_ID_PREFIX=y + +CONFIG_ARCH_POSIX_TRAP_ON_FATAL=y + +CONFIG_BT_L2CAP_TX_MTU=512 +CONFIG_BT_BUF_ACL_RX_SIZE=518 diff --git a/tests/bsim/bluetooth/host/gatt/device_name/src/client.c b/tests/bsim/bluetooth/host/gatt/device_name/src/client.c new file mode 100644 index 000000000000..b564692b8ce0 --- /dev/null +++ b/tests/bsim/bluetooth/host/gatt/device_name/src/client.c @@ -0,0 +1,126 @@ +/* + * Copyright (c) 2025 Nordic Semiconductor ASA + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include + +#include +#include +#include +#include + +#include + +#include + +#include "testlib/adv.h" +#include "testlib/att.h" +#include "testlib/att_read.h" +#include "testlib/att_write.h" +#include "testlib/conn.h" + +#include "babblekit/flags.h" +#include "babblekit/sync.h" +#include "babblekit/testcase.h" + +#include "common.h" + +LOG_MODULE_REGISTER(client, LOG_LEVEL_DBG); + +static DEFINE_FLAG(client_security_changed_flag); + +static struct bt_conn_cb client_conn_cb; + +static void security_changed(struct bt_conn *conn, bt_security_t level, enum bt_security_err err) +{ + char addr_str[BT_ADDR_LE_STR_LEN]; + + bt_addr_le_to_str(bt_conn_get_dst(conn), addr_str, sizeof(addr_str)); + + TEST_ASSERT(err == 0, "Security update failed: %s level %u err %d", addr_str, level, err); + + LOG_DBG("Security changed: %s level %u", addr_str, level); + SET_FLAG(client_security_changed_flag); +} + +static void init_client_conn_callbacks(void) +{ + int err; + + client_conn_cb.connected = NULL; + client_conn_cb.disconnected = NULL; + client_conn_cb.security_changed = security_changed; + + err = bt_conn_cb_register(&client_conn_cb); + TEST_ASSERT(err == 0, "Failed to set client conn callbacks (err %d)", err); +} + +void find_characteristic(struct bt_conn *conn, const struct bt_uuid *svc, + const struct bt_uuid *chrc, uint16_t *chrc_value_handle) +{ + int err; + uint16_t svc_handle; + uint16_t svc_end_handle; + uint16_t chrc_end_handle; + + err = bt_testlib_gatt_discover_primary(&svc_handle, &svc_end_handle, conn, svc, + BT_ATT_FIRST_ATTRIBUTE_HANDLE, + BT_ATT_LAST_ATTRIBUTE_HANDLE); + TEST_ASSERT(err == 0, "Failed to discover service: %d", err); + + LOG_DBG("svc_handle: %u, svc_end_handle: %u", svc_handle, svc_end_handle); + + err = bt_testlib_gatt_discover_characteristic(chrc_value_handle, &chrc_end_handle, NULL, + conn, chrc, (svc_handle + 1), svc_end_handle); + TEST_ASSERT(err == 0, "Failed to get value handle: %d", err); + + LOG_DBG("chrc_value_handle: %u, chrc_end_handle: %u", *chrc_value_handle, chrc_end_handle); +} + +void client_procedure(void) +{ + int err; + struct bt_conn *conn; + uint16_t handle; + + char server_new_name[CONFIG_BT_DEVICE_NAME_MAX]; + + NET_BUF_SIMPLE_DEFINE(attr_value_buf, BT_ATT_MAX_ATTRIBUTE_LEN); + + generate_name(server_new_name, CONFIG_BT_DEVICE_NAME_MAX); + + TEST_START("client"); + + bk_sync_init(); + + err = bt_enable(NULL); + TEST_ASSERT(err == 0, "Cannot enable Bluetooth (err %d)", err); + + LOG_DBG("Bluetooth initialized"); + + init_client_conn_callbacks(); + + err = bt_testlib_adv_conn(&conn, BT_ID_DEFAULT, ADVERTISER_NAME); + TEST_ASSERT(err == 0, "Failed to start connectable advertising (err %d)", err); + + err = bt_testlib_att_exchange_mtu(conn); + TEST_ASSERT(err == 0, "Failed to update MTU (err %d)", err); + + find_characteristic(conn, BT_UUID_GAP, BT_UUID_GAP_DEVICE_NAME, &handle); + + err = bt_testlib_att_read_by_handle_sync(&attr_value_buf, NULL, NULL, conn, + BT_ATT_CHAN_OPT_UNENHANCED_ONLY, handle, 0); + TEST_ASSERT(err == 0, "Failed to read characteristic (err %d)", err); + + LOG_DBG("Device Name of the server: %.*s", attr_value_buf.len, attr_value_buf.data); + + err = bt_testlib_att_write(conn, BT_ATT_CHAN_OPT_UNENHANCED_ONLY, handle, server_new_name, + sizeof(server_new_name)); + TEST_ASSERT(err == BT_ATT_ERR_SUCCESS, "Got ATT error: %d", err); + + bk_sync_send(); + + TEST_PASS("client"); +} diff --git a/tests/bsim/bluetooth/host/gatt/device_name/src/common.h b/tests/bsim/bluetooth/host/gatt/device_name/src/common.h new file mode 100644 index 000000000000..e04fc38fce2d --- /dev/null +++ b/tests/bsim/bluetooth/host/gatt/device_name/src/common.h @@ -0,0 +1,24 @@ +/* + * Copyright (c) 2025 Nordic Semiconductor ASA + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#ifndef ZEPHYR_TESTS_BSIM_BLUETOOTH_HOST_GATT_DEVICE_NAME_SRC_COMMON_H_ +#define ZEPHYR_TESTS_BSIM_BLUETOOTH_HOST_GATT_DEVICE_NAME_SRC_COMMON_H_ + +#include +#include + +#include + +#define ADVERTISER_NAME "Advertiser Pro II" + +static void generate_name(uint8_t *name, size_t length) +{ + for (size_t i = 0; i < length; i++) { + name[i] = (i % 26) + 97; + } +} + +#endif /* ZEPHYR_TESTS_BSIM_BLUETOOTH_HOST_DEVICE_NAME_CLEAR_SRC_COMMON_H_ */ diff --git a/tests/bsim/bluetooth/host/gatt/device_name/src/main.c b/tests/bsim/bluetooth/host/gatt/device_name/src/main.c new file mode 100644 index 000000000000..e6f5ad5e26ba --- /dev/null +++ b/tests/bsim/bluetooth/host/gatt/device_name/src/main.c @@ -0,0 +1,51 @@ +/* + * Copyright (c) 2025 Nordic Semiconductor ASA + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include + +#include "bs_tracing.h" +#include "bstests.h" +#include "babblekit/testcase.h" +#include "testlib/log_utils.h" + +extern void server_procedure(void); +extern void client_procedure(void); +extern enum bst_result_t bst_result; + +static void test_end_cb(void) +{ + if (bst_result != Passed) { + TEST_PRINT("Test has not passed."); + } +} + +static const struct bst_test_instance entrypoints[] = { + { + .test_id = "server", + .test_delete_f = test_end_cb, + .test_main_f = server_procedure, + }, + { + .test_id = "client", + .test_delete_f = test_end_cb, + .test_main_f = client_procedure, + }, + BSTEST_END_MARKER, +}; + +static struct bst_test_list *install(struct bst_test_list *tests) +{ + return bst_add_tests(tests, entrypoints); +}; + +bst_test_install_t test_installers[] = {install, NULL}; + +int main(void) +{ + bst_main(); + + return 0; +} diff --git a/tests/bsim/bluetooth/host/gatt/device_name/src/server.c b/tests/bsim/bluetooth/host/gatt/device_name/src/server.c new file mode 100644 index 000000000000..3d3d7edeeac0 --- /dev/null +++ b/tests/bsim/bluetooth/host/gatt/device_name/src/server.c @@ -0,0 +1,161 @@ +/* + * Copyright (c) 2025 Nordic Semiconductor ASA + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include +#include + +#include + +#include +#include +#include +#include + +#include + +#include + +#include + +#include "testlib/conn.h" +#include "testlib/scan.h" + +#include "babblekit/flags.h" +#include "babblekit/sync.h" +#include "babblekit/testcase.h" + +#include "common.h" + +LOG_MODULE_REGISTER(server, LOG_LEVEL_DBG); + +static DEFINE_FLAG(security_changed_flag); + +static struct bt_conn_cb server_conn_cb; + +static inline bool memeq(const void *m1, size_t len1, const void *m2, size_t len2) +{ + int ret; + + if (len1 != len2) { + return false; + } + + ret = memcmp(m1, m2, len1); + + return ret == 0; +} + +static void security_changed(struct bt_conn *conn, bt_security_t level, enum bt_security_err err) +{ + char addr_str[BT_ADDR_LE_STR_LEN]; + + bt_addr_le_to_str(bt_conn_get_dst(conn), addr_str, sizeof(addr_str)); + + TEST_ASSERT(err == 0, "Security update failed: %s level %u err %d", addr_str, level, err); + + LOG_DBG("Security changed: %s level %u", addr_str, level); + SET_FLAG(security_changed_flag); +} + +static void init_server_conn_callbacks(void) +{ + int err; + + server_conn_cb.connected = NULL; + server_conn_cb.disconnected = NULL; + server_conn_cb.security_changed = security_changed; + server_conn_cb.identity_resolved = NULL; + + err = bt_conn_cb_register(&server_conn_cb); + TEST_ASSERT(err == 0, "Failed to set server conn callbacks (err %d)", err); +} + +static void connect_and_set_security(struct bt_conn **conn) +{ + int err; + bt_addr_le_t client = {}; + + err = bt_testlib_scan_find_name(&client, ADVERTISER_NAME); + TEST_ASSERT(err == 0, "Failed to start scan (err %d)", err); + + err = bt_testlib_connect(&client, conn); + TEST_ASSERT(err == 0, "Failed to initiate connection (err %d)", err); + + err = bt_conn_set_security(*conn, BT_SECURITY_L2); + TEST_ASSERT(err == 0, "Failed to set security (err %d)", err); + + WAIT_FOR_FLAG(security_changed_flag); +} + +void server_procedure(void) +{ + /* Test purpose: + * + * Verifies that writing to the GAP Device Name characteristic correctly + * update the device name. + * + * Two devices: + * - `server`: GATT server, connect and elevate security + * - `client`: GATT client, when connected will look for the GAP Device + * Name characteristic handle and then will send a GATT write with a + * new name + * + * [verdict] + * - the server device name has been updated by the client + */ + + int err; + struct bt_conn *conn = NULL; + + bool names_are_matching; + + uint8_t expected_name[CONFIG_BT_DEVICE_NAME_MAX]; + + /* add one for the null character */ + char original_name[CONFIG_BT_DEVICE_NAME_MAX + 1]; + char new_name[CONFIG_BT_DEVICE_NAME_MAX + 1]; + + const char *name; + + generate_name(expected_name, CONFIG_BT_DEVICE_NAME_MAX); + + TEST_START("server"); + + bk_sync_init(); + + err = bt_enable(NULL); + TEST_ASSERT(err == 0, "Cannot enable Bluetooth (err %d)", err); + + LOG_DBG("Bluetooth initialized"); + + err = bt_set_name("Server Super Name"); + TEST_ASSERT(err == 0, "Failed to set the name (err %d)", err); + + name = bt_get_name(); + memcpy(original_name, name, strlen(name) + 1); + + init_server_conn_callbacks(); + + connect_and_set_security(&conn); + + /* wait for client to do gatt write */ + bk_sync_wait(); + + name = bt_get_name(); + memcpy(new_name, name, strlen(name) + 1); + + LOG_DBG("Original Device Name: %s", original_name); + LOG_DBG("New Device Name: %s", new_name); + + names_are_matching = + memeq(expected_name, sizeof(expected_name), new_name, strlen(new_name)); + TEST_ASSERT(names_are_matching, + "The name of the server doesn't match the one set by the client (server name: " + "`%s`, expected name: `%.*s`)", + new_name, sizeof(expected_name), expected_name); + + TEST_PASS("server"); +} diff --git a/tests/bsim/bluetooth/host/gatt/device_name/test_scripts/run_device_name.sh b/tests/bsim/bluetooth/host/gatt/device_name/test_scripts/run_device_name.sh new file mode 100755 index 000000000000..944de3a87fe1 --- /dev/null +++ b/tests/bsim/bluetooth/host/gatt/device_name/test_scripts/run_device_name.sh @@ -0,0 +1,28 @@ +#!/usr/bin/env bash +# Copyright 2025 Nordic Semiconductor ASA +# SPDX-License-Identifier: Apache-2.0 +set -eu + +source ${ZEPHYR_BASE}/tests/bsim/sh_common.source + +test_name="$(guess_test_long_name)" +simulation_id=${test_name} + +verbosity_level=2 + +EXECUTE_TIMEOUT=120 + +SIM_LEN_US=$((2 * 1000 * 1000)) + +test_exe="${BSIM_OUT_PATH}/bin/bs_${BOARD_TS}_${test_name}_prj_conf" + +cd ${BSIM_OUT_PATH}/bin + +Execute ./bs_2G4_phy_v1 -v=${verbosity_level} -s=${simulation_id} -D=2 -sim_length=${SIM_LEN_US} $@ + +Execute "${test_exe}" -v=${verbosity_level} -s=${simulation_id} -d=0 -rs=420 -testid=server \ + -RealEncryption=1 +Execute "${test_exe}" -v=${verbosity_level} -s=${simulation_id} -d=1 -rs=69 -testid=client \ + -RealEncryption=1 + +wait_for_background_jobs diff --git a/tests/bsim/bluetooth/host/gatt/device_name/testcase.yaml b/tests/bsim/bluetooth/host/gatt/device_name/testcase.yaml new file mode 100644 index 000000000000..4bd3a1b7c834 --- /dev/null +++ b/tests/bsim/bluetooth/host/gatt/device_name/testcase.yaml @@ -0,0 +1,10 @@ +tests: + bluetooth.host.gatt.device_name: + build_only: true + tags: + - bluetooth + platform_allow: + - nrf52_bsim/native + harness: bsim + harness_config: + bsim_exe_name: tests_bsim_bluetooth_host_gatt_device_name_prj_conf From 58db9052f804ff6f119b0b02883aad3586038b27 Mon Sep 17 00:00:00 2001 From: Ludvig Jordet Date: Tue, 28 Jan 2025 16:41:13 +0100 Subject: [PATCH 27/49] =?UTF-8?q?[nrf=20fromtree]=C2=A0Tests:=20Bluetooth:?= =?UTF-8?q?=20Add=20hci=5Fipc=20config=20to=20Twister=20integration?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This adds the configuration used when running PTS tests with hci_ipc to the testcase.yaml, so they are built on integration by CI, avoiding regression. Signed-off-by: Ludvig Jordet (cherry picked from commit ce4bab5662764471f4233a09ade3e2e712fd013f) Signed-off-by: alperen sener --- tests/bluetooth/tester/testcase.yaml | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/tests/bluetooth/tester/testcase.yaml b/tests/bluetooth/tester/testcase.yaml index 7a389a122f5e..b9946b9d610b 100644 --- a/tests/bluetooth/tester/testcase.yaml +++ b/tests/bluetooth/tester/testcase.yaml @@ -16,6 +16,20 @@ tests: - frdm_rw612 extra_configs: - CONFIG_NXP_MONOLITHIC_NBU=n + bluetooth.general.tester_hci_ipc: + build_only: true + harness: bluetooth + platform_allow: + - qemu_x86 + - native_sim + - nrf5340dk/nrf5340/cpuapp + integration_platforms: + - nrf5340dk/nrf5340/cpuapp + extra_args: + # Build config used by AutoPTS for nRF53 + - EXTRA_CONF_FILE="hci_ipc.conf" + tags: bluetooth + sysbuild: true bluetooth.general.tester_le_audio: build_only: true platform_allow: From 9f2dc4786dfed8b39715725964b43f19aa33b6c9 Mon Sep 17 00:00:00 2001 From: Ludvig Jordet Date: Tue, 17 Dec 2024 17:07:01 +0100 Subject: [PATCH 28/49] [nrf fromtree] Bluetooth: Mesh: add missing device key candidate PSA support Commit adds PSA key support for device key candidate. Signed-off-by: Ludvig Jordet (cherry picked from commit 5facb50fe909b7c56b4bf6a1b548e1bd4769cea1) Signed-off-by: alperen sener --- subsys/bluetooth/mesh/net.c | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/subsys/bluetooth/mesh/net.c b/subsys/bluetooth/mesh/net.c index cf95884fd5ac..276728bd9a45 100644 --- a/subsys/bluetooth/mesh/net.c +++ b/subsys/bluetooth/mesh/net.c @@ -1109,17 +1109,23 @@ BT_MESH_SETTINGS_DEFINE(seq, "Seq", seq_set); #if defined(CONFIG_BT_MESH_RPR_SRV) static int dev_key_cand_set(const char *name, size_t len_rd, settings_read_cb read_cb, void *cb_arg) -{ int err; +{ + int err; + struct bt_mesh_key key; - if (len_rd < 16) { - return -EINVAL; + if (len_rd == 0) { + LOG_DBG("val (null)"); + + bt_mesh_key_destroy(&bt_mesh.dev_key_cand); + memset(&bt_mesh.dev_key_cand, 0, sizeof(struct bt_mesh_key)); + return 0; } - err = bt_mesh_settings_set(read_cb, cb_arg, &bt_mesh.dev_key_cand, - sizeof(struct bt_mesh_key)); + err = bt_mesh_settings_set(read_cb, cb_arg, &key, sizeof(struct bt_mesh_key)); if (!err) { LOG_DBG("DevKey candidate recovered from storage"); atomic_set_bit(bt_mesh.flags, BT_MESH_DEVKEY_CAND); + bt_mesh_key_assign(&bt_mesh.dev_key_cand, &key); } return err; From f4d7ef5dbc68efd368f78f1c20108c4f2cf9e704 Mon Sep 17 00:00:00 2001 From: Aleksandr Khromykh Date: Wed, 29 Jan 2025 15:21:25 +0100 Subject: [PATCH 29/49] [nrf fromtree] Bluetooth: Mesh: remove deprecated field in cdb subnet Commit removes deprecated filed in cdb subnetwork. Signed-off-by: Aleksandr Khromykh (cherry picked from commit a09fa5f7f3caed6364ba9cc4fc0d48999b146b4d) Signed-off-by: alperen sener --- subsys/bluetooth/mesh/cdb.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/subsys/bluetooth/mesh/cdb.c b/subsys/bluetooth/mesh/cdb.c index 33c9601807c2..310108d7b756 100644 --- a/subsys/bluetooth/mesh/cdb.c +++ b/subsys/bluetooth/mesh/cdb.c @@ -55,7 +55,7 @@ struct node_val { /* NetKey storage information */ struct net_key_val { - uint8_t kr_flag:1, + uint8_t unused:1, kr_phase:7; struct bt_mesh_key val[2]; } __packed; @@ -491,7 +491,7 @@ static void store_cdb_subnet(const struct bt_mesh_cdb_subnet *sub) memcpy(&key.val[0], &sub->keys[0].net_key, sizeof(struct bt_mesh_key)); memcpy(&key.val[1], &sub->keys[1].net_key, sizeof(struct bt_mesh_key)); - key.kr_flag = 0U; /* Deprecated */ + key.unused = 0U; key.kr_phase = sub->kr_phase; snprintk(path, sizeof(path), "bt/mesh/cdb/Subnet/%x", sub->net_idx); From 915123554e642916ce18ef404d65dbda09b99072 Mon Sep 17 00:00:00 2001 From: Aleksandr Khromykh Date: Wed, 29 Jan 2025 16:11:34 +0100 Subject: [PATCH 30/49] [nrf fromtree] doc: update migration guide with mesh versions incompatibility Commit adds description of key representation incompatibility for mesh images built with different crypto libraries. Signed-off-by: Aleksandr Khromykh (cherry picked from commit 5e235bbf88fa26597ae648428eff23a76b761348) Signed-off-by: alperen sener --- doc/connectivity/bluetooth/api/mesh/dfu.rst | 5 +++-- doc/releases/migration-guide-4.1.rst | 10 ++++++++++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/doc/connectivity/bluetooth/api/mesh/dfu.rst b/doc/connectivity/bluetooth/api/mesh/dfu.rst index 926754ab0f02..959872ae9b9f 100644 --- a/doc/connectivity/bluetooth/api/mesh/dfu.rst +++ b/doc/connectivity/bluetooth/api/mesh/dfu.rst @@ -212,9 +212,10 @@ re-provisioned. The complete list of available options is defined in :c:enum:`bt provisioning. In this case, the device stays provisioned and the new composition data takes place after re-provisioning using the Remote Provisioning models. :c:enumerator:`BT_MESH_DFU_EFFECT_UNPROV` - This effect is chosen if the composition data in the new firmware changes, the device doesn't + This effect is chosen if the composition data in the new firmware changes, the device does not support the remote provisioning, and the new composition data takes effect after applying the - firmware. + firmware. The effect can also be chosen, if it is necessary to unprovision the device for + application-specific reasons. When the Target node receives the Firmware Update Firmware Metadata Check message, the Firmware Update Server model calls the :c:member:`bt_mesh_dfu_srv_cb.check` callback, the application can diff --git a/doc/releases/migration-guide-4.1.rst b/doc/releases/migration-guide-4.1.rst index 70a168c6de5a..0d4f16db084d 100644 --- a/doc/releases/migration-guide-4.1.rst +++ b/doc/releases/migration-guide-4.1.rst @@ -305,6 +305,16 @@ Bluetooth Mesh set as deprecated. Default option for platforms that do not support TF-M is :kconfig:option:`CONFIG_BT_MESH_USES_MBEDTLS_PSA`. +* Mesh key representations are not backward compatible if images are built with TinyCrypt and + crypto libraries based on the PSA API. Mesh no longer stores the key values for those crypto + libraries. The crypto library stores the keys in the internal trusted storage. + If a provisioned device is going to update its image that was built with + the :kconfig:option:`CONFIG_BT_MESH_USES_TINYCRYPT` Kconfig option set on an image + that was built with :kconfig:option:`CONFIG_BT_MESH_USES_MBEDTLS_PSA` or + :kconfig:option:`CONFIG_BT_MESH_USES_TFM_PSA` without erasing the persistent area, + it should be unprovisioned first and reprovisioned after update again. + If the image is changed over Mesh DFU, use :c:enumerator:`BT_MESH_DFU_EFFECT_UNPROV`. + * Mesh explicitly depends on the Secure Storage subsystem if storing into non-volatile memory (:kconfig:option:`CONFIG_BT_SETTINGS`) is enabled and Mbed TLS library (:kconfig:option:`CONFIG_BT_MESH_USES_MBEDTLS_PSA`) is used. From 8013e61eb151707eec111b83bb4b35a198aa5286 Mon Sep 17 00:00:00 2001 From: Ingar Kulbrandstad Date: Wed, 5 Feb 2025 14:06:42 +0100 Subject: [PATCH 31/49] [nrf fromtree] Bluetooth: Host: Updated Kconfig description Added warning to the BT_RECV_WORKQ_SYS description to explain the dangers by using this option. Signed-off-by: Ingar Kulbrandstad (cherry picked from commit 0330ff1ac03aa84f271e103ea3fbaebb5d09732c) Signed-off-by: alperen sener --- subsys/bluetooth/host/Kconfig | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/subsys/bluetooth/host/Kconfig b/subsys/bluetooth/host/Kconfig index 36cbac984acd..ee1b50d3e62f 100644 --- a/subsys/bluetooth/host/Kconfig +++ b/subsys/bluetooth/host/Kconfig @@ -90,8 +90,12 @@ config BT_RECV_WORKQ_SYS High priority HCI packets will processed in the context of the caller of bt_recv(). The application needs to ensure the system workqueue stack size (SYSTEM_WORKQUEUE_STACK_SIZE) is large enough, refer to BT_RX_STACK_SIZE for the recommended minimum. - Note: When this option is used, other users of the system work queue will influence the - latency of incoming Bluetooth events. + Warning: Enabling this option will cause the latency of incoming Bluetooth events to be + affected by other tasks using the system work queue. When this option is active, the Host + will process Bluetooth events in a blocking manner. This can lead to deadlocks if the + application waits for the system work queue while handling Bluetooth events. This feature + is intended for advanced users to allow aggressive memory optimization for devices with + very limited memory. It is strongly advised not to use this option. config BT_RECV_WORKQ_BT bool "Process low priority HCI packets in the bluetooth-specific work queue" From a1bbbd57e69de279afca8cb69c411d636f5e8173 Mon Sep 17 00:00:00 2001 From: Jordan Yates Date: Thu, 6 Feb 2025 09:35:56 +1000 Subject: [PATCH 32/49] [nrf fromtree] bluetooth: host: hci_core: add missing `NULL` check Add check that the command buffer claimed in `bt_le_create_conn_cancel` is not `NULL`. Fixes a fault caused by providing the `NULL` buffer to `bt_hci_cmd_state_set_init`. Signed-off-by: Jordan Yates (cherry picked from commit 7365bcf13350f7027811ffba4b363dc432e9ff98) Signed-off-by: alperen sener --- subsys/bluetooth/host/hci_core.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/subsys/bluetooth/host/hci_core.c b/subsys/bluetooth/host/hci_core.c index 7c1d1376d065..a95a543b0e77 100644 --- a/subsys/bluetooth/host/hci_core.c +++ b/subsys/bluetooth/host/hci_core.c @@ -884,6 +884,9 @@ int bt_le_create_conn_cancel(void) struct bt_hci_cmd_state_set state; buf = bt_hci_cmd_create(BT_HCI_OP_LE_CREATE_CONN_CANCEL, 0); + if (!buf) { + return -ENOBUFS; + } bt_hci_cmd_state_set_init(buf, &state, bt_dev.flags, BT_DEV_INITIATING, false); From 9feb592057ce00944643081af85b867336d16f31 Mon Sep 17 00:00:00 2001 From: alperen sener Date: Thu, 6 Feb 2025 13:30:48 +0100 Subject: [PATCH 33/49] [nrf fromtree] bluetooth: host/crypto: fix the psa crypto init for host psa_crypto_init was bounded to CONFIG_BT_HOST_CRYPTO_PRNG and used to be called under prng_init. Updating the ifdef condition and appropriating the function name for crypto init. Also it is better to make sure psa_crypto_init called by host. Signed-off-by: alperen sener (cherry picked from commit 22de0b3732476c8b16a4c11a7c1724f57918e790) Signed-off-by: alperen sener --- subsys/bluetooth/host/crypto.h | 2 +- subsys/bluetooth/host/crypto_psa.c | 2 +- subsys/bluetooth/host/hci_core.c | 8 +++----- 3 files changed, 5 insertions(+), 7 deletions(-) diff --git a/subsys/bluetooth/host/crypto.h b/subsys/bluetooth/host/crypto.h index 7b3aa0f7df37..1a349591d4ba 100644 --- a/subsys/bluetooth/host/crypto.h +++ b/subsys/bluetooth/host/crypto.h @@ -5,4 +5,4 @@ * SPDX-License-Identifier: Apache-2.0 */ -int prng_init(void); +int bt_crypto_init(void); diff --git a/subsys/bluetooth/host/crypto_psa.c b/subsys/bluetooth/host/crypto_psa.c index c15a52f2fbd6..f71584c649cf 100644 --- a/subsys/bluetooth/host/crypto_psa.c +++ b/subsys/bluetooth/host/crypto_psa.c @@ -27,7 +27,7 @@ #include LOG_MODULE_REGISTER(bt_host_crypto); -int prng_init(void) +int bt_crypto_init(void) { psa_status_t status = psa_crypto_init(); diff --git a/subsys/bluetooth/host/hci_core.c b/subsys/bluetooth/host/hci_core.c index a95a543b0e77..b01c49d3243b 100644 --- a/subsys/bluetooth/host/hci_core.c +++ b/subsys/bluetooth/host/hci_core.c @@ -3287,11 +3287,9 @@ static int common_init(void) read_supported_commands_complete(rsp); net_buf_unref(rsp); - if (IS_ENABLED(CONFIG_BT_HOST_CRYPTO_PRNG)) { - /* Initialize the PRNG so that it is safe to use it later - * on in the initialization process. - */ - err = prng_init(); + if (IS_ENABLED(CONFIG_BT_HOST_CRYPTO)) { + /* Initialize crypto for host */ + err = bt_crypto_init(); if (err) { return err; } From 16208dcdebb571831b4357b7825952653bf3cce2 Mon Sep 17 00:00:00 2001 From: Pavel Vasilyev Date: Thu, 6 Feb 2025 13:11:44 +0100 Subject: [PATCH 34/49] [nrf fromtree] bluetooth: mesh: pb_adv: ensure that bitwise NOT doesn't result in 0 Ensure that ~(link.rx.seg) & SEG_NVAL doesn't result in 0. Fixes #84804 Coverity-CID: 393090 Signed-off-by: Pavel Vasilyev (cherry picked from commit fcf09dd0ad61e932dd0fee39ee0b4ae5adc2c88e) Signed-off-by: alperen sener --- subsys/bluetooth/mesh/pb_adv.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/subsys/bluetooth/mesh/pb_adv.c b/subsys/bluetooth/mesh/pb_adv.c index 8e363c0dae94..86f20497704f 100644 --- a/subsys/bluetooth/mesh/pb_adv.c +++ b/subsys/bluetooth/mesh/pb_adv.c @@ -637,7 +637,14 @@ static void gen_prov_start(struct prov_rx *rx, struct net_buf_simple *buf) link.rx.last_seg = START_LAST_SEG(rx->gpc); + /* This (BIT(0) is set) can happen if we received a Transaction Continuation PDU, before + * receiving a Transaction Start PDU (see `gen_prov_cont`). Now we received the Transaction + * Start PDU and we can extract the last segment number. Knowing this, we check if + * previously received segment exceeds the last segment number. If so, we reject the + * Transaction Start PDU. + */ if ((link.rx.seg & BIT(0)) && + ((link.rx.seg & SEG_NVAL) != SEG_NVAL) && (find_msb_set((~link.rx.seg) & SEG_NVAL) - 1 > link.rx.last_seg)) { LOG_ERR("Invalid segment index %u", seg); prov_failed(PROV_ERR_NVAL_FMT); From 87cbc6647c509b8716da94dcf3c940f9e7b8cef5 Mon Sep 17 00:00:00 2001 From: Pavel Vasilyev Date: Thu, 6 Feb 2025 14:27:08 +0100 Subject: [PATCH 35/49] [nrf fromtree] bluetooth: mesh: access: cast to uint8_t to avoid potential overflow Explicitly cast `BT_MESH_TRANSMIT` output to avoid potential overflow. Fixes #84759 Fixes #84749 Fixes #84702 Coverity-CID: 487624 Coverity-CID: 487648 Coverity-CID: 487722 Signed-off-by: Pavel Vasilyev (cherry picked from commit f82c0e85b09d8b8716a37d50225cea62926ebf49) Signed-off-by: alperen sener --- include/zephyr/bluetooth/mesh/access.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/zephyr/bluetooth/mesh/access.h b/include/zephyr/bluetooth/mesh/access.h index db477ee6206b..71aa89db0ef3 100644 --- a/include/zephyr/bluetooth/mesh/access.h +++ b/include/zephyr/bluetooth/mesh/access.h @@ -628,7 +628,7 @@ struct bt_mesh_model_op { * @return Mesh transmit value that can be used e.g. for the default * values of the configuration model data. */ -#define BT_MESH_TRANSMIT(count, int_ms) ((count) | (((int_ms / 10) - 1) << 3)) +#define BT_MESH_TRANSMIT(count, int_ms) ((uint8_t)((count) | (((int_ms / 10) - 1) << 3))) /** * @brief Decode transmit count from a transmit value. From 5fb90093688fab1aa55c663b3b0920faf78503c1 Mon Sep 17 00:00:00 2001 From: Pavel Vasilyev Date: Thu, 6 Feb 2025 21:17:49 +0100 Subject: [PATCH 36/49] [nrf fromtree] bluetooth: mesh: proxy_msg: check that att mtu is big enough This commit checks that ATT MTU value returned by `bt_gatt_get_mtu` is greater or equal to 3 to prevent integer overflow. Fixes #84693 Coverity-CID: 487743 Signed-off-by: Pavel Vasilyev (cherry picked from commit 038173acdadd507049579f2996f49e9f141a5b4b) Signed-off-by: alperen sener --- subsys/bluetooth/mesh/proxy_msg.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/subsys/bluetooth/mesh/proxy_msg.c b/subsys/bluetooth/mesh/proxy_msg.c index a4d8c8c14b2e..82cd56457fce 100644 --- a/subsys/bluetooth/mesh/proxy_msg.c +++ b/subsys/bluetooth/mesh/proxy_msg.c @@ -176,6 +176,7 @@ int bt_mesh_proxy_msg_send(struct bt_conn *conn, uint8_t type, bt_gatt_complete_func_t end, void *user_data) { int err; + uint16_t att_mtu = bt_gatt_get_mtu(conn); uint16_t mtu; struct bt_mesh_proxy_role *role = &roles[bt_conn_index(conn)]; @@ -183,7 +184,12 @@ int bt_mesh_proxy_msg_send(struct bt_conn *conn, uint8_t type, bt_hex(msg->data, msg->len)); /* ATT_MTU - OpCode (1 byte) - Handle (2 bytes) */ - mtu = bt_gatt_get_mtu(conn) - 3; + if (att_mtu < 3) { + LOG_WRN("Invalid ATT MTU: %d", att_mtu); + return -EINVAL; + } + + mtu = att_mtu - 3; if (mtu > msg->len) { net_buf_simple_push_u8(msg, PDU_HDR(SAR_COMPLETE, type)); return role->cb.send(conn, msg->data, msg->len, end, user_data); From f16fdf0c26df75745dd2ffe4df77a8dbe45a7474 Mon Sep 17 00:00:00 2001 From: Pavel Vasilyev Date: Thu, 6 Feb 2025 21:40:37 +0100 Subject: [PATCH 37/49] [nrf fromtree] bluetooth: mesh: delayable_msg: cast to avoid truncation issue Explicitly cast Kconfig option value to avoid truncation issue reported by Coverity. Fixes #84697 Coverity-CID: 487737 Signed-off-by: Pavel Vasilyev (cherry picked from commit b7f16f10414c0df1e75937e2f55de7edc79f57d0) Signed-off-by: alperen sener --- subsys/bluetooth/mesh/delayable_msg.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/subsys/bluetooth/mesh/delayable_msg.c b/subsys/bluetooth/mesh/delayable_msg.c index 5b2d1a241ab7..a591ba68c954 100644 --- a/subsys/bluetooth/mesh/delayable_msg.c +++ b/subsys/bluetooth/mesh/delayable_msg.c @@ -179,7 +179,7 @@ static bool push_msg_from_delayable_msgs(void) NET_BUF_SIMPLE_DEFINE(buf, BT_MESH_TX_SDU_MAX); SYS_SLIST_FOR_EACH_NODE(&msg->chunks, node) { - uint16_t tmp = MIN(CONFIG_BT_MESH_ACCESS_DELAYABLE_MSG_CHUNK_SIZE, len); + uint16_t tmp = MIN((uint16_t)CONFIG_BT_MESH_ACCESS_DELAYABLE_MSG_CHUNK_SIZE, len); chunk = CONTAINER_OF(node, struct delayable_msg_chunk, node); memcpy(net_buf_simple_add(&buf, tmp), chunk->data, tmp); From 22804c74969dad11118ae59f27360d0ca3addc1c Mon Sep 17 00:00:00 2001 From: Lingao Meng Date: Wed, 13 Nov 2024 16:23:04 +0800 Subject: [PATCH 38/49] [nrf fromtree] Bluetooth: Mesh: Remove ADV_FLAG_PROXY_START, due to meanless The first why introduce ADV_FLAG_PROXY_START see here(https://github.com/zephyrproject-rtos/zephyr/pull/58826) The main core change is to mark ADV_FLAG_PROXY as moving to the last enabled proxy instead of before. But introducing the ADV_FLAG_PROXY_START flag seems meaningless. Signed-off-by: Lingao Meng (cherry picked from commit bcac572ab477b4421063bfaf9d8a5cf833dc5100) Signed-off-by: alperen sener --- subsys/bluetooth/mesh/adv_ext.c | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/subsys/bluetooth/mesh/adv_ext.c b/subsys/bluetooth/mesh/adv_ext.c index 63ece26889e5..344cafc064eb 100644 --- a/subsys/bluetooth/mesh/adv_ext.c +++ b/subsys/bluetooth/mesh/adv_ext.c @@ -51,8 +51,6 @@ enum { ADV_FLAG_SENT, /** Currently performing proxy advertising */ ADV_FLAG_PROXY, - /** The proxy has been start, but maybe pending. */ - ADV_FLAG_PROXY_START, /** The send-call has been pending. */ ADV_FLAG_SCHEDULE_PENDING, /** Custom adv params have been set, we need to update the parameters on @@ -286,7 +284,6 @@ static const char * const adv_tag_to_str[] = { static bool schedule_send_with_mask(struct bt_mesh_ext_adv *ext_adv, int ignore_mask) { if (atomic_test_and_clear_bit(ext_adv->flags, ADV_FLAG_PROXY)) { - atomic_clear_bit(ext_adv->flags, ADV_FLAG_PROXY_START); (void)bt_le_ext_adv_stop(ext_adv->instance); atomic_clear_bit(ext_adv->flags, ADV_FLAG_ACTIVE); @@ -326,7 +323,6 @@ static void send_pending_adv(struct k_work *work) atomic_clear_bit(ext_adv->flags, ADV_FLAG_ACTIVE); atomic_clear_bit(ext_adv->flags, ADV_FLAG_PROXY); - atomic_clear_bit(ext_adv->flags, ADV_FLAG_PROXY_START); if (ext_adv->adv) { struct bt_mesh_adv_ctx ctx = ext_adv->adv->ctx; @@ -371,8 +367,6 @@ static void send_pending_adv(struct k_work *work) return; } - atomic_set_bit(ext_adv->flags, ADV_FLAG_PROXY_START); - if (!bt_mesh_adv_gatt_send()) { atomic_set_bit(ext_adv->flags, ADV_FLAG_PROXY); } @@ -528,7 +522,7 @@ static void connected(struct bt_le_ext_adv *instance, { struct bt_mesh_ext_adv *ext_adv = gatt_adv_get(); - if (atomic_test_and_clear_bit(ext_adv->flags, ADV_FLAG_PROXY_START)) { + if (atomic_test_and_clear_bit(ext_adv->flags, ADV_FLAG_PROXY)) { atomic_clear_bit(ext_adv->flags, ADV_FLAG_ACTIVE); (void)schedule_send(ext_adv); } From 7897388706f8928a901ac75625288e1bfdc5b2b6 Mon Sep 17 00:00:00 2001 From: Lingao Meng Date: Wed, 13 Nov 2024 16:47:04 +0800 Subject: [PATCH 39/49] [nrf fromtree] Bluetooth: Mesh: Remove START_PENDING flags Remove START_PENDING flag, and replace with bt_mesh_adv_is_empty_by_tag not only make adv_ext.c more readable, but also enhancement In previous implementation, after proxy started, will check this flag, and when the flag is true, will direct stop proxy and re-enter schedule, but has one problem, this flag set true, but buf will be empty, such as when sending mesh relay messages will for-each every sets, until find not-active, but also set previous set START_PENDING flags, so will cause proxy advertising started->stop->started. Signed-off-by: Lingao Meng (cherry picked from commit 2ebdcfa963a56cded34fa77b5681345ced682050) Signed-off-by: alperen sener --- subsys/bluetooth/mesh/adv_ext.c | 137 +++++++++++++++++--------------- 1 file changed, 72 insertions(+), 65 deletions(-) diff --git a/subsys/bluetooth/mesh/adv_ext.c b/subsys/bluetooth/mesh/adv_ext.c index 344cafc064eb..4650541af845 100644 --- a/subsys/bluetooth/mesh/adv_ext.c +++ b/subsys/bluetooth/mesh/adv_ext.c @@ -51,8 +51,6 @@ enum { ADV_FLAG_SENT, /** Currently performing proxy advertising */ ADV_FLAG_PROXY, - /** The send-call has been pending. */ - ADV_FLAG_SCHEDULE_PENDING, /** Custom adv params have been set, we need to update the parameters on * the next send. */ @@ -273,40 +271,72 @@ static int adv_send(struct bt_mesh_ext_adv *ext_adv, struct bt_mesh_adv *adv) return err; } -static const char * const adv_tag_to_str[] = { - [BT_MESH_ADV_TAG_LOCAL] = "local adv", - [BT_MESH_ADV_TAG_RELAY] = "relay adv", - [BT_MESH_ADV_TAG_PROXY] = "proxy adv", - [BT_MESH_ADV_TAG_FRIEND] = "friend adv", - [BT_MESH_ADV_TAG_PROV] = "prov adv", -}; - -static bool schedule_send_with_mask(struct bt_mesh_ext_adv *ext_adv, int ignore_mask) +static bool stop_proxy_adv(struct bt_mesh_ext_adv *ext_adv) { if (atomic_test_and_clear_bit(ext_adv->flags, ADV_FLAG_PROXY)) { - (void)bt_le_ext_adv_stop(ext_adv->instance); + int err = bt_le_ext_adv_stop(ext_adv->instance); + + __ASSERT_NO_MSG(err == 0); atomic_clear_bit(ext_adv->flags, ADV_FLAG_ACTIVE); } + return true; +} + +static bool schedule_send_with_mask(struct bt_mesh_ext_adv *ext_adv, int ignore_mask) +{ + (void)stop_proxy_adv(ext_adv); + if (atomic_test_bit(ext_adv->flags, ADV_FLAG_ACTIVE)) { - atomic_set_bit(ext_adv->flags, ADV_FLAG_SCHEDULE_PENDING); return false; } else if ((~ignore_mask) & k_work_busy_get(&ext_adv->work)) { return false; } - atomic_clear_bit(ext_adv->flags, ADV_FLAG_SCHEDULE_PENDING); bt_mesh_wq_submit(&ext_adv->work); return true; } +static bool adv_send_process(struct bt_mesh_adv *adv, struct bt_mesh_ext_adv *ext_adv) +{ + int err; + + while (adv || (adv = bt_mesh_adv_get_by_tag(ext_adv->tags, K_NO_WAIT))) { + /* busy == 0 means this was canceled */ + if (!adv->ctx.busy) { + bt_mesh_adv_unref(adv); + adv = NULL; + continue; + } + + adv->ctx.busy = 0U; + err = adv_send(ext_adv, adv); + + bt_mesh_adv_unref(adv); + adv = NULL; + + if (!err) { + return true; + } + } + + return false; +} + static void send_pending_adv(struct k_work *work) { + static const char * const adv_tag_to_str[] = { + [BT_MESH_ADV_TAG_LOCAL] = "local", + [BT_MESH_ADV_TAG_RELAY] = "relay", + [BT_MESH_ADV_TAG_PROXY] = "proxy", + [BT_MESH_ADV_TAG_FRIEND] = "friend", + [BT_MESH_ADV_TAG_PROV] = "prov", + }; struct bt_mesh_ext_adv *ext_adv; - struct bt_mesh_adv *adv; - int err; + struct bt_mesh_adv *adv = NULL; + bool sent; ext_adv = CONTAINER_OF(work, struct bt_mesh_ext_adv, work); @@ -316,7 +346,7 @@ static void send_pending_adv(struct k_work *work) } if (atomic_test_and_clear_bit(ext_adv->flags, ADV_FLAG_SENT)) { - LOG_DBG("Advertising stopped after %u ms for %s", + LOG_DBG("Advertising stopped after %u ms for %s adv", k_uptime_get_32() - ext_adv->timestamp, ext_adv->adv ? adv_tag_to_str[ext_adv->adv->ctx.tag] : adv_tag_to_str[BT_MESH_ADV_TAG_PROXY]); @@ -335,45 +365,37 @@ static void send_pending_adv(struct k_work *work) } } - while ((adv = bt_mesh_adv_get_by_tag(ext_adv->tags, K_NO_WAIT))) { - /* busy == 0 means this was canceled */ - if (!adv->ctx.busy) { - bt_mesh_adv_unref(adv); - continue; + do { + sent = adv_send_process(adv, ext_adv); + if (sent) { + return; } - adv->ctx.busy = 0U; - err = adv_send(ext_adv, adv); - - bt_mesh_adv_unref(adv); - - if (!err) { - return; /* Wait for advertising to finish */ + if (ext_adv->instance == NULL) { + LOG_DBG("Advertiser is suspended or deleted"); + return; } - } - - if (ext_adv->instance == NULL) { - LOG_DBG("Advertiser is suspended or deleted"); - return; - } - if (IS_ENABLED(CONFIG_BT_MESH_PROXY_SOLICITATION) && - !bt_mesh_sol_send()) { - return; - } + if (IS_ENABLED(CONFIG_BT_MESH_PROXY_SOLICITATION) && + !bt_mesh_sol_send()) { + return; + } - if (!IS_ENABLED(CONFIG_BT_MESH_GATT_SERVER) || - !(ext_adv->tags & BT_MESH_ADV_TAG_BIT_PROXY)) { - return; - } + if (!IS_ENABLED(CONFIG_BT_MESH_GATT_SERVER) || + !(ext_adv->tags & BT_MESH_ADV_TAG_BIT_PROXY)) { + return; + } - if (!bt_mesh_adv_gatt_send()) { - atomic_set_bit(ext_adv->flags, ADV_FLAG_PROXY); - } + if (!bt_mesh_adv_gatt_send()) { + atomic_set_bit(ext_adv->flags, ADV_FLAG_PROXY); + } - if (atomic_test_and_clear_bit(ext_adv->flags, ADV_FLAG_SCHEDULE_PENDING)) { - schedule_send_with_mask(ext_adv, K_WORK_RUNNING); - } + /* If there is a pending adv during the process of starting the + * proxy advertising, the proxy advertising needs to be stop and + * sent pending advertising immediately. + */ + } while ((adv = bt_mesh_adv_get_by_tag(ext_adv->tags, K_NO_WAIT)) && + stop_proxy_adv(ext_adv)); } static bool schedule_send(struct bt_mesh_ext_adv *ext_adv) @@ -508,6 +530,7 @@ static void adv_sent(struct bt_le_ext_adv *instance, } if (!atomic_test_bit(ext_adv->flags, ADV_FLAG_ACTIVE)) { + LOG_WRN("Advertiser %p ADV_FLAG_ACTIVE not set", ext_adv); return; } @@ -516,28 +539,12 @@ static void adv_sent(struct bt_le_ext_adv *instance, bt_mesh_wq_submit(&ext_adv->work); } -#if defined(CONFIG_BT_MESH_GATT_SERVER) -static void connected(struct bt_le_ext_adv *instance, - struct bt_le_ext_adv_connected_info *info) -{ - struct bt_mesh_ext_adv *ext_adv = gatt_adv_get(); - - if (atomic_test_and_clear_bit(ext_adv->flags, ADV_FLAG_PROXY)) { - atomic_clear_bit(ext_adv->flags, ADV_FLAG_ACTIVE); - (void)schedule_send(ext_adv); - } -} -#endif /* CONFIG_BT_MESH_GATT_SERVER */ - int bt_mesh_adv_enable(void) { int err; static const struct bt_le_ext_adv_cb adv_cb = { .sent = adv_sent, -#if defined(CONFIG_BT_MESH_GATT_SERVER) - .connected = connected, -#endif /* CONFIG_BT_MESH_GATT_SERVER */ }; if (advs[0].instance) { From b532acc115f4639ca809324a583d448925198cb2 Mon Sep 17 00:00:00 2001 From: Lingao Meng Date: Thu, 5 Dec 2024 10:00:02 +0800 Subject: [PATCH 40/49] [nrf fromtree] tests: bsim: Add testcase for bluetooth mesh extended advertiser Add some configuration for test adv_ext.c Signed-off-by: Lingao Meng Signed-off-by: Lingao Meng (cherry picked from commit f5bd2170d1e4bbc3367c56081bf8d684dfa140f1) Signed-off-by: alperen sener --- subsys/bluetooth/mesh/adv_ext.c | 137 ++++++------ tests/bsim/bluetooth/mesh/compile.sh | 4 + .../bluetooth/mesh/overlay_gatt_separate.conf | 12 ++ .../mesh/overlay_multi_adv_sets.conf | 5 + .../bsim/bluetooth/mesh/src/test_advertiser.c | 200 ++++++++++++++++++ .../mesh/tests_scripts/advertiser/cancel.sh | 17 ++ .../tests_scripts/advertiser/proxy_mixin.sh | 6 + .../tests_scripts/advertiser/random_order.sh | 3 + .../mesh/tests_scripts/advertiser/relay.sh | 18 ++ .../tests_scripts/advertiser/reverse_order.sh | 3 + .../tests_scripts/advertiser/send_order.sh | 3 + .../tests_scripts/advertiser/terminate.sh | 15 ++ .../tests_scripts/advertiser/tx_cb_multi.sh | 3 + .../tests_scripts/advertiser/tx_cb_single.sh | 3 + 14 files changed, 368 insertions(+), 61 deletions(-) create mode 100644 tests/bsim/bluetooth/mesh/overlay_gatt_separate.conf create mode 100644 tests/bsim/bluetooth/mesh/overlay_multi_adv_sets.conf create mode 100755 tests/bsim/bluetooth/mesh/tests_scripts/advertiser/cancel.sh create mode 100755 tests/bsim/bluetooth/mesh/tests_scripts/advertiser/relay.sh create mode 100755 tests/bsim/bluetooth/mesh/tests_scripts/advertiser/terminate.sh diff --git a/subsys/bluetooth/mesh/adv_ext.c b/subsys/bluetooth/mesh/adv_ext.c index 4650541af845..05593a3b60e0 100644 --- a/subsys/bluetooth/mesh/adv_ext.c +++ b/subsys/bluetooth/mesh/adv_ext.c @@ -271,43 +271,40 @@ static int adv_send(struct bt_mesh_ext_adv *ext_adv, struct bt_mesh_adv *adv) return err; } -static bool stop_proxy_adv(struct bt_mesh_ext_adv *ext_adv) +static int stop_proxy_adv(struct bt_mesh_ext_adv *ext_adv) { - if (atomic_test_and_clear_bit(ext_adv->flags, ADV_FLAG_PROXY)) { - int err = bt_le_ext_adv_stop(ext_adv->instance); + int err; - __ASSERT_NO_MSG(err == 0); + if (atomic_test_bit(ext_adv->flags, ADV_FLAG_PROXY)) { + err = bt_le_ext_adv_stop(ext_adv->instance); + if (err) { + LOG_ERR("Failed to stop proxy advertising: %d", err); + return err; + } + atomic_clear_bit(ext_adv->flags, ADV_FLAG_PROXY); atomic_clear_bit(ext_adv->flags, ADV_FLAG_ACTIVE); } - return true; -} - -static bool schedule_send_with_mask(struct bt_mesh_ext_adv *ext_adv, int ignore_mask) -{ - (void)stop_proxy_adv(ext_adv); - - if (atomic_test_bit(ext_adv->flags, ADV_FLAG_ACTIVE)) { - return false; - } else if ((~ignore_mask) & k_work_busy_get(&ext_adv->work)) { - return false; - } - - bt_mesh_wq_submit(&ext_adv->work); - - return true; + return 0; } -static bool adv_send_process(struct bt_mesh_adv *adv, struct bt_mesh_ext_adv *ext_adv) +static int adv_queue_send_process(struct bt_mesh_ext_adv *ext_adv) { - int err; + struct bt_mesh_adv *adv; + int err = -ENOENT; - while (adv || (adv = bt_mesh_adv_get_by_tag(ext_adv->tags, K_NO_WAIT))) { + while ((adv = bt_mesh_adv_get_by_tag(ext_adv->tags, K_NO_WAIT))) { /* busy == 0 means this was canceled */ if (!adv->ctx.busy) { bt_mesh_adv_unref(adv); - adv = NULL; + continue; + } + + if (stop_proxy_adv(ext_adv)) { + LOG_WRN("Advertising %p canceled due to proxy adv failed to stop", adv); + bt_mesh_adv_send_start(0, -ECANCELED, &adv->ctx); + bt_mesh_adv_unref(adv); continue; } @@ -315,14 +312,43 @@ static bool adv_send_process(struct bt_mesh_adv *adv, struct bt_mesh_ext_adv *ex err = adv_send(ext_adv, adv); bt_mesh_adv_unref(adv); - adv = NULL; if (!err) { - return true; + return 0; /* Wait for advertising to finish */ + } + } + + return err; +} + +static void start_proxy_sol_or_proxy_adv(struct bt_mesh_ext_adv *ext_adv) +{ + if (ext_adv->instance == NULL) { + LOG_DBG("Advertiser is suspended or deleted"); + return; + } + + if (!(ext_adv->tags & BT_MESH_ADV_TAG_BIT_PROXY)) { + return; + } + + if (IS_ENABLED(CONFIG_BT_MESH_PROXY_SOLICITATION)) { + if (stop_proxy_adv(ext_adv)) { + return; + } + + if (!bt_mesh_sol_send()) { + return; } } - return false; + if (IS_ENABLED(CONFIG_BT_MESH_GATT_SERVER) && + !atomic_test_and_set_bit(ext_adv->flags, ADV_FLAG_PROXY)) { + if (bt_mesh_adv_gatt_send()) { + atomic_clear_bit(ext_adv->flags, ADV_FLAG_PROXY); + return; + } + } } static void send_pending_adv(struct k_work *work) @@ -335,8 +361,7 @@ static void send_pending_adv(struct k_work *work) [BT_MESH_ADV_TAG_PROV] = "prov", }; struct bt_mesh_ext_adv *ext_adv; - struct bt_mesh_adv *adv = NULL; - bool sent; + int err; ext_adv = CONTAINER_OF(work, struct bt_mesh_ext_adv, work); @@ -365,42 +390,32 @@ static void send_pending_adv(struct k_work *work) } } - do { - sent = adv_send_process(adv, ext_adv); - if (sent) { - return; - } - - if (ext_adv->instance == NULL) { - LOG_DBG("Advertiser is suspended or deleted"); - return; - } - - if (IS_ENABLED(CONFIG_BT_MESH_PROXY_SOLICITATION) && - !bt_mesh_sol_send()) { - return; - } - - if (!IS_ENABLED(CONFIG_BT_MESH_GATT_SERVER) || - !(ext_adv->tags & BT_MESH_ADV_TAG_BIT_PROXY)) { - return; - } - - if (!bt_mesh_adv_gatt_send()) { - atomic_set_bit(ext_adv->flags, ADV_FLAG_PROXY); - } + err = adv_queue_send_process(ext_adv); + if (!err) { + return; + } - /* If there is a pending adv during the process of starting the - * proxy advertising, the proxy advertising needs to be stop and - * sent pending advertising immediately. - */ - } while ((adv = bt_mesh_adv_get_by_tag(ext_adv->tags, K_NO_WAIT)) && - stop_proxy_adv(ext_adv)); + start_proxy_sol_or_proxy_adv(ext_adv); } static bool schedule_send(struct bt_mesh_ext_adv *ext_adv) { - return schedule_send_with_mask(ext_adv, 0); + if (atomic_test_bit(ext_adv->flags, ADV_FLAG_ACTIVE)) { + /* We don't need to resubmit the `send_pending_adv` work if the mesh advertiser + * is sending a mesh packet. The `send_pending_adv` work is resubmitted when the + * current advertising is finished, which is done through the `adv_sent` callback. + * + * The proxy advertisement in turns doesn't timeout or stop quickly and has less + * priority than regular mesh messages, thus needs to be stopped immeditaly. + */ + if (!atomic_test_bit(ext_adv->flags, ADV_FLAG_PROXY)) { + return false; + } + } + + bt_mesh_wq_submit(&ext_adv->work); + + return true; } void bt_mesh_adv_gatt_update(void) @@ -530,7 +545,7 @@ static void adv_sent(struct bt_le_ext_adv *instance, } if (!atomic_test_bit(ext_adv->flags, ADV_FLAG_ACTIVE)) { - LOG_WRN("Advertiser %p ADV_FLAG_ACTIVE not set", ext_adv); + LOG_DBG("Advertiser %p ADV_FLAG_ACTIVE not set", ext_adv); return; } diff --git a/tests/bsim/bluetooth/mesh/compile.sh b/tests/bsim/bluetooth/mesh/compile.sh index 05c51f7bfe1e..15a809378791 100755 --- a/tests/bsim/bluetooth/mesh/compile.sh +++ b/tests/bsim/bluetooth/mesh/compile.sh @@ -13,15 +13,19 @@ source ${ZEPHYR_BASE}/tests/bsim/compile.source app=tests/bsim/bluetooth/mesh compile app=tests/bsim/bluetooth/mesh conf_overlay=overlay_pst.conf compile app=tests/bsim/bluetooth/mesh conf_overlay=overlay_gatt.conf compile +app=tests/bsim/bluetooth/mesh conf_overlay=overlay_gatt_separate.conf compile app=tests/bsim/bluetooth/mesh conf_overlay=overlay_low_lat.conf compile app=tests/bsim/bluetooth/mesh conf_overlay=overlay_psa.conf compile app=tests/bsim/bluetooth/mesh conf_overlay=overlay_workq_sys.conf compile +app=tests/bsim/bluetooth/mesh conf_overlay=overlay_multi_adv_sets.conf compile app=tests/bsim/bluetooth/mesh conf_overlay="overlay_pst.conf;overlay_psa.conf" compile app=tests/bsim/bluetooth/mesh conf_overlay="overlay_gatt.conf;overlay_psa.conf" compile app=tests/bsim/bluetooth/mesh conf_overlay="overlay_gatt.conf;overlay_workq_sys.conf" compile app=tests/bsim/bluetooth/mesh conf_overlay="overlay_low_lat.conf;overlay_psa.conf" compile app=tests/bsim/bluetooth/mesh conf_overlay="overlay_gatt.conf;overlay_low_lat.conf" compile app=tests/bsim/bluetooth/mesh conf_overlay="overlay_pst.conf;overlay_gatt.conf" compile +app=tests/bsim/bluetooth/mesh \ + conf_overlay="overlay_gatt.conf;overlay_multi_adv_sets.conf" compile app=tests/bsim/bluetooth/mesh \ conf_overlay="overlay_pst.conf;overlay_gatt.conf;overlay_psa.conf" compile app=tests/bsim/bluetooth/mesh \ diff --git a/tests/bsim/bluetooth/mesh/overlay_gatt_separate.conf b/tests/bsim/bluetooth/mesh/overlay_gatt_separate.conf new file mode 100644 index 000000000000..68575635b62b --- /dev/null +++ b/tests/bsim/bluetooth/mesh/overlay_gatt_separate.conf @@ -0,0 +1,12 @@ +CONFIG_BT_CTLR_ADV_SET=4 + +CONFIG_BT_CENTRAL=y +CONFIG_BT_PERIPHERAL=y + +CONFIG_BT_EXT_ADV_MAX_ADV_SET=4 + +CONFIG_BT_MESH_PB_GATT=y +CONFIG_BT_MESH_GATT_PROXY=y +CONFIG_BT_MESH_ADV_EXT_GATT_SEPARATE=y +CONFIG_BT_MESH_PROXY_CLIENT=y +CONFIG_BT_MESH_PROXY_SOLICITATION=y diff --git a/tests/bsim/bluetooth/mesh/overlay_multi_adv_sets.conf b/tests/bsim/bluetooth/mesh/overlay_multi_adv_sets.conf new file mode 100644 index 000000000000..51ac121caeae --- /dev/null +++ b/tests/bsim/bluetooth/mesh/overlay_multi_adv_sets.conf @@ -0,0 +1,5 @@ +CONFIG_BT_CTLR_ADV_SET=4 +CONFIG_BT_EXT_ADV_MAX_ADV_SET=4 + +CONFIG_BT_MESH_RELAY_ADV_SETS=2 +CONFIG_BT_MESH_ADV_EXT_RELAY_USING_MAIN_ADV_SET=y diff --git a/tests/bsim/bluetooth/mesh/src/test_advertiser.c b/tests/bsim/bluetooth/mesh/src/test_advertiser.c index 411533eb1766..97b3c9c8aebb 100644 --- a/tests/bsim/bluetooth/mesh/src/test_advertiser.c +++ b/tests/bsim/bluetooth/mesh/src/test_advertiser.c @@ -37,6 +37,7 @@ static int seq_checker; static struct bt_mesh_test_gatt gatt_param; static int num_adv_sent; static uint8_t previous_checker = 0xff; +static bool local_sent, relay_sent; static K_SEM_DEFINE(observer_sem, 0, 1); @@ -501,6 +502,202 @@ static void test_tx_send_order(void) PASS(); } +static void cancel_adv_send_start(uint16_t duration, int err, void *cb_data) +{ + if (cb_data != NULL) { + struct bt_mesh_adv *adv_cancel = (struct bt_mesh_adv *)cb_data; + + adv_cancel->ctx.busy = 0; + + bt_mesh_adv_unref(adv_cancel); + + return; + } + + ASSERT_FALSE_MSG(true, "The adv should be canceled.\n"); +} + +static void cancel_adv_send_end(int err, void *cb_data) +{ + k_sem_give(&observer_sem); +} + +static void test_tx_send_cancel(void) +{ + static const struct bt_mesh_send_cb local_send_cb = { + .start = cancel_adv_send_start, + .end = cancel_adv_send_end, + }; + struct bt_mesh_adv *adv_cancel; + uint8_t xmit = BT_MESH_TRANSMIT(2, 20); + struct bt_mesh_adv *local; + + bt_init(); + adv_init(); + + local = bt_mesh_adv_create(BT_MESH_ADV_DATA, BT_MESH_ADV_TAG_LOCAL, + xmit, K_NO_WAIT); + ASSERT_FALSE_MSG(!local, "Out of local advs\n"); + + adv_cancel = bt_mesh_adv_create(BT_MESH_ADV_DATA, BT_MESH_ADV_TAG_LOCAL, + xmit, K_NO_WAIT); + ASSERT_FALSE_MSG(!adv_cancel, "Out of local advs\n"); + + net_buf_simple_add_u8(&local->b, 0x00); + net_buf_simple_add_u8(&adv_cancel->b, 0x01); + + bt_mesh_adv_send(local, &local_send_cb, bt_mesh_adv_ref(adv_cancel)); + bt_mesh_adv_send(adv_cancel, &local_send_cb, NULL); + + bt_mesh_adv_unref(local); + bt_mesh_adv_unref(adv_cancel); + + /* Make relay advs sent out. */ + k_sleep(K_SECONDS(1)); + + ASSERT_OK_MSG(k_sem_take(&observer_sem, K_SECONDS(10)), + "Didn't call the last end tx cb."); + + PASS(); +} + +static void terminate_adv_send_start(uint16_t duration, int err, void *cb_data) +{ + if (cb_data == NULL) { + k_sem_give(&observer_sem); + return; + } +} + +static void terminate_adv_send_end(int err, void *cb_data) +{ + ASSERT_FALSE_MSG(true, "The adv should be terminated.\n"); +} + +static void test_tx_send_terminate(void) +{ + static const struct bt_mesh_send_cb local_send_cb = { + .start = terminate_adv_send_start, + .end = terminate_adv_send_end, + }; + uint8_t xmit = BT_MESH_TRANSMIT(2, 20); + struct bt_mesh_adv *local; + + bt_init(); + adv_init(); + + local = bt_mesh_adv_create(BT_MESH_ADV_DATA, BT_MESH_ADV_TAG_LOCAL, + xmit, K_NO_WAIT); + ASSERT_FALSE_MSG(!local, "Out of local advs\n"); + + net_buf_simple_add_u8(&local->b, 0x00); + + bt_mesh_adv_send(local, &local_send_cb, NULL); + + bt_mesh_adv_unref(local); + + ASSERT_OK_MSG(k_sem_take(&observer_sem, K_SECONDS(10)), + "Didn't call the last start tx cb."); + + bt_mesh_adv_terminate(local); + + /* Make relay advs sent out. */ + k_sleep(K_SECONDS(1)); + + PASS(); +} + +static void local_adv_send_end(int err, void *cb_data) +{ + local_sent = true; +} + +static void relay_adv_send_start(uint16_t duration, int err, void *cb_data) +{ +#if defined(CONFIG_BT_MESH_RELAY_ADV_SETS) && CONFIG_BT_MESH_RELAY_ADV_SETS > 0 + ASSERT_FALSE_MSG(local_sent, + "The relay adv should sending with the local adv in parallel.\n"); +#else + ASSERT_FALSE_MSG(!local_sent, + "The relay adv should start to sending after sent local adv.\n"); +#endif +} + +static void relay_adv_send_end(int err, void *cb_data) +{ + relay_sent = true; +} + +static void second_relay_adv_send_start(uint16_t duration, int err, void *cb_data) +{ +#if defined(CONFIG_BT_MESH_RELAY_ADV_SETS) && CONFIG_BT_MESH_RELAY_ADV_SETS > 0 + ASSERT_FALSE_MSG(relay_sent, + "The second relay adv should sending with the first relay" + " adv in parallel\n"); +#else + ASSERT_FALSE_MSG(!relay_sent, + "The second relay adv should start to sending after sent" + " first relay adv\n"); +#endif +} + +static void second_relay_adv_send_end(int err, void *cb_data) +{ + k_sem_give(&observer_sem); +} + +static void test_tx_send_relay(void) +{ + static const struct bt_mesh_send_cb local_send_cb = { + .end = local_adv_send_end, + }; + static const struct bt_mesh_send_cb relay_first_send_cb = { + .start = relay_adv_send_start, + .end = relay_adv_send_end, + }; + static const struct bt_mesh_send_cb relay_second_send_cb = { + .start = second_relay_adv_send_start, + .end = second_relay_adv_send_end, + }; + struct bt_mesh_adv *local, *relay_first, *relay_second; + uint8_t xmit = BT_MESH_TRANSMIT(2, 20); + + bt_init(); + adv_init(); + + local = bt_mesh_adv_create(BT_MESH_ADV_DATA, BT_MESH_ADV_TAG_LOCAL, + xmit, K_NO_WAIT); + ASSERT_FALSE_MSG(!local, "Out of local advs\n"); + + relay_first = bt_mesh_adv_create(BT_MESH_ADV_DATA, BT_MESH_ADV_TAG_RELAY, + xmit, K_NO_WAIT); + ASSERT_FALSE_MSG(!relay_first, "Out of relay advs\n"); + + relay_second = bt_mesh_adv_create(BT_MESH_ADV_DATA, BT_MESH_ADV_TAG_RELAY, + xmit, K_NO_WAIT); + ASSERT_FALSE_MSG(!relay_second, "Out of relay advs\n"); + + net_buf_simple_add_u8(&local->b, 0x00); + net_buf_simple_add_u8(&relay_first->b, 0x01); + net_buf_simple_add_u8(&relay_second->b, 0x02); + + bt_mesh_adv_send(local, &local_send_cb, NULL); + bt_mesh_adv_send(relay_first, &relay_first_send_cb, NULL); + bt_mesh_adv_send(relay_second, &relay_second_send_cb, NULL); + + bt_mesh_adv_unref(local); + bt_mesh_adv_unref(relay_first); + bt_mesh_adv_unref(relay_second); + + /* Make relay advs sent out. */ + k_sleep(K_SECONDS(1)); + + ASSERT_OK_MSG(k_sem_take(&observer_sem, K_SECONDS(10)), + "Didn't call the last end tx cb."); + + PASS(); +} + static void test_tx_reverse_order(void) { struct bt_mesh_adv *adv[CONFIG_BT_MESH_ADV_BUF_COUNT]; @@ -765,7 +962,10 @@ static const struct bst_test_instance test_adv[] = { TEST_CASE(tx, cb_single, "ADV: tx cb parameter checker"), TEST_CASE(tx, cb_multi, "ADV: tx cb sequence checker"), TEST_CASE(tx, proxy_mixin, "ADV: proxy mix-in gatt adv"), + TEST_CASE(tx, send_cancel, "ADV: tx send cancel"), + TEST_CASE(tx, send_terminate, "ADV: tx send terminate"), TEST_CASE(tx, send_order, "ADV: tx send order"), + TEST_CASE(tx, send_relay, "ADV: tx relay sent order"), TEST_CASE(tx, reverse_order, "ADV: tx reversed order"), TEST_CASE(tx, random_order, "ADV: tx random order"), TEST_CASE(tx, disable, "ADV: test suspending/resuming advertiser"), diff --git a/tests/bsim/bluetooth/mesh/tests_scripts/advertiser/cancel.sh b/tests/bsim/bluetooth/mesh/tests_scripts/advertiser/cancel.sh new file mode 100755 index 000000000000..d1e594538331 --- /dev/null +++ b/tests/bsim/bluetooth/mesh/tests_scripts/advertiser/cancel.sh @@ -0,0 +1,17 @@ +#!/usr/bin/env bash +# Copyright 2025 Xiaomi Corporation +# SPDX-License-Identifier: Apache-2.0 + +source $(dirname "${BASH_SOURCE[0]}")/../../_mesh_test.sh + +# Test scenario: +# Allocate an advertiser buffer 1 using bt_mesh_adv_buf_create(). +# Allocate an advertiser buffer 2 using bt_mesh_adv_buf_create(). +# Submit the advertiser buffer 1 and 2 using bt_mesh_adv_send(). +# In the first buffer cb->send_start cancel buffer 2. +# Expect the second buffer cb->send_start not be called. + +RunTest mesh_adv_tx_send_cancel adv_tx_send_cancel + +overlay="overlay_multi_adv_sets_conf" +RunTest mesh_adv_tx_send_cancel_multi_adv_sets adv_tx_send_cancel diff --git a/tests/bsim/bluetooth/mesh/tests_scripts/advertiser/proxy_mixin.sh b/tests/bsim/bluetooth/mesh/tests_scripts/advertiser/proxy_mixin.sh index fb6af48f6518..a130701611d8 100755 --- a/tests/bsim/bluetooth/mesh/tests_scripts/advertiser/proxy_mixin.sh +++ b/tests/bsim/bluetooth/mesh/tests_scripts/advertiser/proxy_mixin.sh @@ -21,8 +21,14 @@ source $(dirname "${BASH_SOURCE[0]}")/../../_mesh_test.sh overlay=overlay_gatt_conf RunTest mesh_adv_proxy_mixin adv_tx_proxy_mixin adv_rx_proxy_mixin +overlay=overlay_gatt_separate_conf +RunTest mesh_adv_proxy_mixin_separate adv_tx_proxy_mixin adv_rx_proxy_mixin + overlay=overlay_gatt_conf_overlay_workq_sys_conf RunTest mesh_adv_proxy_mixin_workq adv_tx_proxy_mixin adv_rx_proxy_mixin +overlay="overlay_gatt_conf_overlay_multi_adv_sets_conf" +RunTest mesh_adv_proxy_mixin_multi_adv_sets adv_tx_proxy_mixin adv_rx_proxy_mixin + overlay="overlay_gatt_conf_overlay_psa_conf" RunTest mesh_adv_proxy_mixin_psa adv_tx_proxy_mixin adv_rx_proxy_mixin diff --git a/tests/bsim/bluetooth/mesh/tests_scripts/advertiser/random_order.sh b/tests/bsim/bluetooth/mesh/tests_scripts/advertiser/random_order.sh index 5808d2cd5086..16c6fd5ee628 100755 --- a/tests/bsim/bluetooth/mesh/tests_scripts/advertiser/random_order.sh +++ b/tests/bsim/bluetooth/mesh/tests_scripts/advertiser/random_order.sh @@ -10,5 +10,8 @@ RunTest mesh_adv_random_order adv_tx_random_order adv_rx_random_order overlay=overlay_workq_sys_conf RunTest mesh_adv_random_order_workq adv_tx_random_order adv_rx_random_order +overlay="overlay_multi_adv_sets_conf" +RunTest mesh_adv_random_order_multi_adv_sets adv_tx_random_order adv_rx_random_order + overlay=overlay_psa_conf RunTest mesh_adv_random_order_psa adv_tx_random_order adv_rx_random_order diff --git a/tests/bsim/bluetooth/mesh/tests_scripts/advertiser/relay.sh b/tests/bsim/bluetooth/mesh/tests_scripts/advertiser/relay.sh new file mode 100755 index 000000000000..453e8d14514c --- /dev/null +++ b/tests/bsim/bluetooth/mesh/tests_scripts/advertiser/relay.sh @@ -0,0 +1,18 @@ +#!/usr/bin/env bash +# Copyright 2025 Xiaomi Corporation +# SPDX-License-Identifier: Apache-2.0 + +source $(dirname "${BASH_SOURCE[0]}")/../../_mesh_test.sh + +# Test scenario: +# Allocate an advertiser buffer 1 using bt_mesh_adv_buf_create() with tag BT_MESH_ADV_TAG_LOCAL. +# Allocate an advertiser buffer 2 using bt_mesh_adv_buf_create() with tag BT_MESH_ADV_TAG_RELAY. +# Allocate an advertiser buffer 3 using bt_mesh_adv_buf_create() with tag BT_MESH_ADV_TAG_RELAY. +# Submit the advertiser buffer 1,2 and 3 using bt_mesh_adv_send(). +# For not enable multiple advertising sets, expect the adv 1,2,3 are be sent by fifo order. +# For enable multiple advertising sets, expect the adv 1,2,3 are be sent by same time. + +RunTest mesh_adv_tx_send_relay adv_tx_send_relay + +overlay="overlay_multi_adv_sets_conf" +RunTest mesh_adv_tx_send_relay_multi_adv_sets adv_tx_send_relay diff --git a/tests/bsim/bluetooth/mesh/tests_scripts/advertiser/reverse_order.sh b/tests/bsim/bluetooth/mesh/tests_scripts/advertiser/reverse_order.sh index 1428833a35b0..7c63dd9f7cf4 100755 --- a/tests/bsim/bluetooth/mesh/tests_scripts/advertiser/reverse_order.sh +++ b/tests/bsim/bluetooth/mesh/tests_scripts/advertiser/reverse_order.sh @@ -10,5 +10,8 @@ RunTest mesh_adv_reverse_order adv_tx_reverse_order adv_rx_receive_order overlay=overlay_workq_sys_conf RunTest mesh_adv_reverse_order_workq adv_tx_reverse_order adv_rx_receive_order +overlay="overlay_multi_adv_sets_conf" +RunTest mesh_adv_reverse_order_multi_adv_sets adv_tx_reverse_order adv_rx_receive_order + overlay=overlay_psa_conf RunTest mesh_adv_reverse_order_psa adv_tx_reverse_order adv_rx_receive_order diff --git a/tests/bsim/bluetooth/mesh/tests_scripts/advertiser/send_order.sh b/tests/bsim/bluetooth/mesh/tests_scripts/advertiser/send_order.sh index 5122d7fdf053..90b43ac0e2a0 100755 --- a/tests/bsim/bluetooth/mesh/tests_scripts/advertiser/send_order.sh +++ b/tests/bsim/bluetooth/mesh/tests_scripts/advertiser/send_order.sh @@ -10,5 +10,8 @@ RunTest mesh_adv_send_order adv_tx_send_order adv_rx_receive_order overlay=overlay_workq_sys_conf RunTest mesh_adv_send_order_workq adv_tx_send_order adv_rx_receive_order +overlay="overlay_multi_adv_sets_conf" +RunTest mesh_adv_send_order_multi_adv_sets adv_tx_send_order adv_rx_receive_order + overlay=overlay_psa_conf RunTest mesh_adv_send_order_psa adv_tx_send_order adv_rx_receive_order diff --git a/tests/bsim/bluetooth/mesh/tests_scripts/advertiser/terminate.sh b/tests/bsim/bluetooth/mesh/tests_scripts/advertiser/terminate.sh new file mode 100755 index 000000000000..f2c7e2e687eb --- /dev/null +++ b/tests/bsim/bluetooth/mesh/tests_scripts/advertiser/terminate.sh @@ -0,0 +1,15 @@ +#!/usr/bin/env bash +# Copyright 2025 Xiaomi Corporation +# SPDX-License-Identifier: Apache-2.0 + +source $(dirname "${BASH_SOURCE[0]}")/../../_mesh_test.sh + +# Test scenario: +# Allocate an advertiser buffer using bt_mesh_adv_buf_create(). +# Submit the advertiser buffer using bt_mesh_adv_send(). +# call `bt_mesh_adv_terminate` to terminate the ongoing transmission. +# expect the advertiser terminated and cd->end not be called. +RunTest mesh_adv_tx_send_terminate adv_tx_send_terminate + +overlay="overlay_multi_adv_sets_conf" +RunTest mesh_adv_tx_send_terminate_multi_adv_sets adv_tx_send_terminate diff --git a/tests/bsim/bluetooth/mesh/tests_scripts/advertiser/tx_cb_multi.sh b/tests/bsim/bluetooth/mesh/tests_scripts/advertiser/tx_cb_multi.sh index a7e3ec954371..e5d063b1cbe9 100755 --- a/tests/bsim/bluetooth/mesh/tests_scripts/advertiser/tx_cb_multi.sh +++ b/tests/bsim/bluetooth/mesh/tests_scripts/advertiser/tx_cb_multi.sh @@ -10,5 +10,8 @@ RunTest mesh_adv_tx_cb_multi adv_tx_cb_multi overlay=overlay_workq_sys_conf RunTest mesh_adv_tx_cb_multi_workq adv_tx_cb_multi +overlay="overlay_multi_adv_sets_conf" +RunTest mesh_adv_tx_cb_multi_multi_adv_sets adv_tx_cb_multi + overlay=overlay_psa_conf RunTest mesh_adv_tx_cb_multi_psa adv_tx_cb_multi diff --git a/tests/bsim/bluetooth/mesh/tests_scripts/advertiser/tx_cb_single.sh b/tests/bsim/bluetooth/mesh/tests_scripts/advertiser/tx_cb_single.sh index 48d81a601364..ccc8ad8da410 100755 --- a/tests/bsim/bluetooth/mesh/tests_scripts/advertiser/tx_cb_single.sh +++ b/tests/bsim/bluetooth/mesh/tests_scripts/advertiser/tx_cb_single.sh @@ -10,5 +10,8 @@ RunTest mesh_adv_tx_cb_single adv_tx_cb_single adv_rx_xmit overlay=overlay_workq_sys_conf RunTest mesh_adv_tx_cb_single_workq adv_tx_cb_single adv_rx_xmit +overlay="overlay_multi_adv_sets_conf" +RunTest mesh_adv_tx_cb_single_psa_multi_adv_sets adv_tx_cb_single adv_rx_xmit + overlay=overlay_psa_conf RunTest mesh_adv_tx_cb_single_psa adv_tx_cb_single adv_rx_xmit From ef4d2fb8be517643fa4bc7965dc6d3f823ce309d Mon Sep 17 00:00:00 2001 From: Aleksandr Khromykh Date: Fri, 7 Feb 2025 16:49:03 +0100 Subject: [PATCH 41/49] [nrf fromtree] Bluetooth: Mesh: remove double cdb node storing Commit removes the double cdb node entry storing into the settings if device was reprovisioned over nppi interface. Signed-off-by: Aleksandr Khromykh (cherry picked from commit 4bb595718da6dbb52c65c14243da510d146565b4) Signed-off-by: alperen sener --- subsys/bluetooth/mesh/provisioner.c | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/subsys/bluetooth/mesh/provisioner.c b/subsys/bluetooth/mesh/provisioner.c index 4f2232765bf1..b1df6431d068 100644 --- a/subsys/bluetooth/mesh/provisioner.c +++ b/subsys/bluetooth/mesh/provisioner.c @@ -589,18 +589,16 @@ static void prov_node_add(void) struct bt_mesh_cdb_node *node = provisionee.node; int err; - if (atomic_test_bit(bt_mesh_prov_link.flags, REPROVISION)) { - bt_mesh_cdb_node_update(node, bt_mesh_prov_link.addr, - provisionee.elem_count); - } - err = bt_mesh_cdb_node_key_import(node, provisionee.new_dev_key); if (err) { LOG_ERR("Failed to import node device key"); return; } - if (IS_ENABLED(CONFIG_BT_SETTINGS)) { + if (atomic_test_bit(bt_mesh_prov_link.flags, REPROVISION)) { + bt_mesh_cdb_node_update(node, bt_mesh_prov_link.addr, + provisionee.elem_count); + } else if (IS_ENABLED(CONFIG_BT_SETTINGS)) { bt_mesh_cdb_node_store(node); } From 5fd773145e3c8c9e8726d95acb9089e31683c866 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A5vard=20Reierstad?= Date: Mon, 10 Feb 2025 08:52:25 +0100 Subject: [PATCH 42/49] [nrf fromtree] Bluetooth: testlib: Add addr string macro MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Adds a macro returning a string representation of a Bluetooth address. This allows you to print multiple addresses in a single printk or LOG... call. Signed-off-by: HÃ¥vard Reierstad (cherry picked from commit 997814cf8c7fcfdb0cb912d6b48efe08486c6583) Signed-off-by: alperen sener --- .../common/testlib/include/testlib/addr.h | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/tests/bluetooth/common/testlib/include/testlib/addr.h b/tests/bluetooth/common/testlib/include/testlib/addr.h index 2f9e49488b7a..90802e50fb5d 100644 --- a/tests/bluetooth/common/testlib/include/testlib/addr.h +++ b/tests/bluetooth/common/testlib/include/testlib/addr.h @@ -17,4 +17,18 @@ .a = {{last, 0x00, 0x00, 0x00, 0x00, 0xc0}}, \ }) +static inline char *bt_testlib_get_addr_str(const bt_addr_le_t *addr, + char addr_str[BT_ADDR_LE_STR_LEN]) +{ + bt_addr_le_to_str(addr, addr_str, BT_ADDR_LE_STR_LEN); + + return addr_str; +} + +/** Macro to simplify printing of addresses. The returned pointer is valid until the end of the + * scope this was called from, due to the use of a compound literal. + */ +#define bt_testlib_addr_to_str(addr) \ + bt_testlib_get_addr_str((addr), (char[BT_ADDR_LE_STR_LEN]) {0}) + #endif /* ZEPHYR_TESTS_BLUETOOTH_COMMON_TESTLIB_INCLUDE_TESTLIB_ADDR_H_ */ From aea99996249b13fbc4ed59bd6fb6a022b24196b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A5vard=20Reierstad?= Date: Mon, 10 Feb 2025 09:20:54 +0100 Subject: [PATCH 43/49] [nrf fromtree] Bluetooth: tests: Add macros to babblekit MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Adds two macros to the babblekit library: * DEFINE_FLAG_STATIC * TEST_ASSERT_NO_MSG Signed-off-by: HÃ¥vard Reierstad (cherry picked from commit c2c4f2e0d110edaeb12c47a4a81f69a895d28a7d) Signed-off-by: alperen sener --- tests/bsim/babblekit/include/babblekit/flags.h | 8 +++++++- tests/bsim/babblekit/include/babblekit/testcase.h | 11 +++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/tests/bsim/babblekit/include/babblekit/flags.h b/tests/bsim/babblekit/include/babblekit/flags.h index 89c8b45ca3e9..a614e811309d 100644 --- a/tests/bsim/babblekit/include/babblekit/flags.h +++ b/tests/bsim/babblekit/include/babblekit/flags.h @@ -19,12 +19,18 @@ #define DECLARE_FLAG(flag) extern atomic_t flag /* Define a new binary flag. - * Declare them static if defining flags with the same name in multiple files. + * Use @ref DEFINE_FLAG_STATIC if defining flags with the same name in multiple files. * * @param flag Name of the flag */ #define DEFINE_FLAG(flag) atomic_t flag = (atomic_t) false +/* Define a new, static binary flag. + * + * @param flag Name of the flag + */ +#define DEFINE_FLAG_STATIC(flag) static atomic_t flag = (atomic_t) false + #define SET_FLAG(flag) (void)atomic_set(&flag, (atomic_t) true) #define UNSET_FLAG(flag) (void)atomic_set(&flag, (atomic_t) false) diff --git a/tests/bsim/babblekit/include/babblekit/testcase.h b/tests/bsim/babblekit/include/babblekit/testcase.h index 9d13a1a00300..0cbbb7562fff 100644 --- a/tests/bsim/babblekit/include/babblekit/testcase.h +++ b/tests/bsim/babblekit/include/babblekit/testcase.h @@ -95,6 +95,17 @@ extern enum bst_result_t bst_result; } \ } while (0) +/* + * @brief Assert `expr` is true + * + * Assert that `expr` is true. If assertion is false, fail the test. I.e. return non-zero. + * + * @note This is different than `sys/__assert.h`. + * + */ +#define TEST_ASSERT_NO_MSG(expr) \ + TEST_ASSERT(expr, "") + /* * @brief Print a value. Lower-level than `printk` or `LOG_xx`. * From eeb2e385876785c945409c337782f681f4c350db Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A5vard=20Reierstad?= Date: Thu, 6 Feb 2025 15:08:48 +0100 Subject: [PATCH 44/49] [nrf fromtree] Bluetooth: Host: More bsim refactoring MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Commit does the following changes: * Use functionality from the `babbelkit` library for common functions related to flags, test progression (failing, passing etc.) and synchronization between two devices. Locally defined equivalents are removed. * Removes the files containing only functionality that is provided by the `babblekit` library. * Remove the `test_pre_init_f` and `test_tick_f` functions (commonly implemented as `test_init` and `test_tick`) from the modified tests. These functions are not needed as they were only used to fail the test if a device didn't complete the test within a certain time frame. This is already handled by the `sim_length` argument used in the test scripts. Signed-off-by: HÃ¥vard Reierstad (cherry picked from commit 7737483c0612d6d692c70d43f8dc1c6791f4922b) Signed-off-by: alperen sener --- .../bluetooth/host/adv/chain/CMakeLists.txt | 3 + .../bsim/bluetooth/host/adv/chain/src/main.c | 49 ++--------- .../adv/extended/src/ext_adv_advertiser.c | 4 +- .../host/adv/extended/src/ext_adv_scanner.c | 6 +- .../host/adv/long_ad/src/advertiser.c | 6 +- .../bluetooth/host/adv/long_ad/src/scanner.c | 2 +- .../adv/periodic/src/per_adv_advertiser.c | 4 +- .../host/adv/periodic/src/per_adv_sync.c | 12 +-- .../host/att/eatt/src/main_reconfigure.c | 2 +- .../host/att/eatt_notif/src/client_test.c | 6 +- .../host/att/eatt_notif/src/server_test.c | 4 +- .../host/att/mtu_update/CMakeLists.txt | 3 + .../host/att/mtu_update/src/common.h | 37 -------- .../host/att/mtu_update/src/main_central.c | 25 ++---- .../host/att/mtu_update/src/main_peripheral.c | 22 +---- .../att/mtu_update/test_scripts/run_test.sh | 2 +- .../host/att/pipeline/common/utils.h | 62 -------------- .../host/att/pipeline/dut/CMakeLists.txt | 8 +- .../host/att/pipeline/dut/src/main.c | 79 +++++++---------- .../host/att/pipeline/tester/CMakeLists.txt | 4 +- .../host/att/pipeline/tester/src/main.c | 84 +++++++------------ .../host/att/read_fill_buf/bs_macro.h | 13 --- .../host/att/read_fill_buf/bs_main.c | 53 ------------ .../att/read_fill_buf/client/CMakeLists.txt | 8 +- .../host/att/read_fill_buf/client/main.c | 28 ++++++- .../att/read_fill_buf/server/CMakeLists.txt | 8 +- .../host/att/read_fill_buf/server/main.c | 28 ++++++- .../read_fill_buf/test_scripts/run_tests.sh | 8 +- .../retry_on_sec_err/client/CMakeLists.txt | 3 +- .../host/att/retry_on_sec_err/client/main.c | 14 ++-- .../retry_on_sec_err/server/CMakeLists.txt | 3 +- .../host/att/retry_on_sec_err/server/main.c | 12 ++- .../host/att/retry_on_sec_err/test_utils.c | 21 ----- .../host/att/retry_on_sec_err/test_utils.h | 29 ------- .../host/att/sequential/common/common_defs.h | 9 ++ .../host/att/sequential/common/utils.h | 59 ------------- .../host/att/sequential/dut/CMakeLists.txt | 3 + .../host/att/sequential/dut/src/main.c | 57 +++++-------- .../host/att/sequential/tester/CMakeLists.txt | 3 + .../host/att/sequential/tester/src/main.c | 64 ++++++-------- .../host/central/src/dummy_peripheral.c | 1 - tests/bsim/bluetooth/host/central/src/main.c | 3 - .../gatt/authorization/src/gatt_client_test.c | 8 +- .../gatt/authorization/src/gatt_server_test.c | 2 +- .../host/gatt/caching/src/gatt_client_test.c | 14 ++-- .../host/gatt/caching/src/gatt_server_test.c | 4 +- .../host/gatt/ccc_store/src/central.c | 10 +-- .../host/gatt/ccc_store/src/peripheral.c | 8 +- .../host/gatt/device_name/src/client.c | 2 +- .../host/gatt/device_name/src/server.c | 2 +- .../host/gatt/general/src/gatt_client_test.c | 10 +-- .../host/gatt/general/src/gatt_server_test.c | 2 +- .../host/gatt/notify/src/gatt_client_test.c | 10 +-- .../host/gatt/notify/src/gatt_server_test.c | 6 +- .../host/gatt/notify_multiple/CMakeLists.txt | 3 + .../host/gatt/notify_multiple/src/common.c | 20 ----- .../host/gatt/notify_multiple/src/common.h | 44 ---------- .../notify_multiple/src/gatt_client_test.c | 61 ++++++++------ .../notify_multiple/src/gatt_server_test.c | 36 +++++--- .../host/gatt/sc_indicate/CMakeLists.txt | 3 + .../host/gatt/sc_indicate/src/bs_bt_utils.c | 22 ++--- .../host/gatt/sc_indicate/src/bs_bt_utils.h | 40 +-------- .../host/gatt/sc_indicate/src/central.c | 25 +++--- .../host/gatt/sc_indicate/src/main.c | 22 ----- .../host/gatt/sc_indicate/src/peripheral.c | 10 ++- .../host/gatt/settings/CMakeLists.txt | 3 + .../bluetooth/host/gatt/settings/src/client.c | 4 +- .../host/gatt/settings/src/gatt_utils.c | 14 ++-- .../bluetooth/host/gatt/settings/src/main.c | 28 +------ .../bluetooth/host/gatt/settings/src/server.c | 4 +- .../bluetooth/host/gatt/settings/src/utils.c | 10 +-- .../bluetooth/host/gatt/settings/src/utils.h | 47 ----------- .../host/gatt/settings_clear/src/client.c | 4 +- .../host/gatt/settings_clear/src/server.c | 6 +- .../bluetooth/host/id/settings/CMakeLists.txt | 3 + .../bluetooth/host/id/settings/src/common.h | 20 ----- .../bsim/bluetooth/host/id/settings/src/dut.c | 18 ++-- .../bluetooth/host/id/settings/src/main.c | 23 ----- .../host/iso/bis/src/bis_broadcaster.c | 2 +- .../bluetooth/host/iso/bis/src/bis_receiver.c | 10 +-- .../bluetooth/host/iso/cis/src/cis_central.c | 2 +- .../host/iso/cis/src/cis_peripheral.c | 2 +- .../bluetooth/host/iso/frag/src/broadcaster.c | 4 +- .../host/iso/frag_2/src/broadcaster.c | 6 +- .../bluetooth/host/l2cap/credits/src/main.c | 4 +- .../host/l2cap/credits_seg_recv/src/main.c | 4 +- .../host/l2cap/general/src/main_l2cap_ecred.c | 4 +- .../host/l2cap/many_conns/src/main.c | 4 +- .../host/l2cap/multilink_peripheral/src/dut.c | 2 +- .../host/l2cap/reassembly/dut/src/dut.c | 2 +- .../host/l2cap/reassembly/peer/src/peer.c | 6 +- .../src/main_l2cap_send_on_connect.c | 6 +- .../bluetooth/host/l2cap/split/common/utils.h | 51 ----------- .../host/l2cap/split/dut/CMakeLists.txt | 3 + .../bluetooth/host/l2cap/split/dut/src/main.c | 46 ++++------ .../host/l2cap/split/tester/CMakeLists.txt | 3 + .../host/l2cap/split/tester/src/main.c | 36 +++----- .../bluetooth/host/l2cap/stress/src/main.c | 4 +- .../l2cap/userdata/src/main_l2cap_userdata.c | 8 +- .../bluetooth/host/misc/acl_tx_frag/src/dut.c | 10 +-- .../host/misc/acl_tx_frag/src/peer.c | 4 +- .../host/misc/disable/src/gatt_client_test.c | 8 +- .../host/misc/disable/src/gatt_server_test.c | 2 +- .../host/misc/disconnect/common/common.h | 9 ++ .../host/misc/disconnect/common/sync.c | 65 -------------- .../host/misc/disconnect/common/sync.h | 9 -- .../host/misc/disconnect/common/utils.h | 63 -------------- .../host/misc/disconnect/dut/CMakeLists.txt | 4 +- .../host/misc/disconnect/dut/src/main.c | 63 ++++++-------- .../misc/disconnect/tester/CMakeLists.txt | 4 +- .../host/misc/disconnect/tester/src/main.c | 70 ++++++---------- .../bluetooth/host/misc/hfc/CMakeLists.txt | 2 + tests/bsim/bluetooth/host/misc/hfc/src/main.c | 59 +++++-------- .../bsim/bluetooth/host/misc/hfc/src/utils.h | 55 ------------ .../bluetooth/host/misc/sample_test/src/dut.c | 2 +- .../host/misc/sample_test/src/peer.c | 6 +- .../host/misc/unregister_conn_cb/src/main.c | 2 +- .../host/privacy/central/CMakeLists.txt | 1 - .../host/privacy/central/src/bs_bt_utils.c | 75 ----------------- .../host/privacy/central/src/bs_bt_utils.h | 72 ---------------- .../bluetooth/host/privacy/central/src/dut.c | 17 ++-- .../bluetooth/host/privacy/central/src/main.c | 11 --- .../host/privacy/central/src/tester.c | 34 ++++---- .../host/privacy/device/CMakeLists.txt | 3 + .../device/src/test_undirected_central.c | 57 ++++--------- .../privacy/device/src/test_undirected_main.c | 26 ------ .../device/src/test_undirected_peripheral.c | 46 +++------- .../privacy/device/test_scripts/run_tests.sh | 3 +- .../host/privacy/legacy/CMakeLists.txt | 8 +- .../host/privacy/legacy/src/bs_bt_utils.c | 73 ---------------- .../host/privacy/legacy/src/bs_bt_utils.h | 54 ------------ .../bluetooth/host/privacy/legacy/src/dut.c | 31 ++++--- .../bluetooth/host/privacy/legacy/src/main.c | 5 -- .../host/privacy/legacy/src/tester.c | 56 ++++++------- .../host/privacy/peripheral/CMakeLists.txt | 8 +- .../host/privacy/peripheral/src/bs_bt_utils.c | 35 -------- .../host/privacy/peripheral/src/bs_bt_utils.h | 52 ------------ .../privacy/peripheral/src/dut_rpa_expired.c | 29 ++++--- .../privacy/peripheral/src/dut_rpa_rotation.c | 28 ++++--- .../host/privacy/peripheral/src/main.c | 1 - .../privacy/peripheral/src/main_rpa_expired.c | 5 -- .../peripheral/src/main_rpa_rotation.c | 5 -- .../peripheral/src/tester_rpa_expired.c | 41 +++++---- .../peripheral/src/tester_rpa_rotation.c | 42 ++++++---- .../bluetooth/host/scan/start_stop/src/main.c | 41 +++------ .../bond_overwrite_allowed/CMakeLists.txt | 3 + .../bond_overwrite_allowed/src/bs_bt_utils.c | 42 +++------- .../bond_overwrite_allowed/src/bs_bt_utils.h | 42 +--------- .../bond_overwrite_allowed/src/central.c | 9 +- .../bond_overwrite_allowed/src/main.c | 4 - .../bond_overwrite_allowed/src/peripheral.c | 13 +-- .../bond_overwrite_denied/CMakeLists.txt | 3 + .../bond_overwrite_denied/src/bs_bt_utils.c | 42 +++------- .../bond_overwrite_denied/src/bs_bt_utils.h | 42 +--------- .../bond_overwrite_denied/src/central.c | 5 +- .../security/bond_overwrite_denied/src/main.c | 4 - .../bond_overwrite_denied/src/peripheral.c | 9 +- .../bond_per_connection/CMakeLists.txt | 3 + .../bond_per_connection/src/bs_bt_utils.c | 48 ++++------- .../bond_per_connection/src/bs_bt_utils.h | 44 +--------- .../bond_per_connection/src/central.c | 5 +- .../security/bond_per_connection/src/main.c | 4 - .../bond_per_connection/src/peripheral.c | 9 +- .../host/security/ccc_update/src/central.c | 8 +- .../host/security/ccc_update/src/peripheral.c | 9 +- .../id_addr_update/central/CMakeLists.txt | 3 + .../id_addr_update/central/src/central.c | 6 +- .../id_addr_update/central/src/utils.c | 45 ++++------ .../id_addr_update/central/src/utils.h | 3 - .../id_addr_update/common/bs_bt_utils.h | 50 ----------- .../id_addr_update/peripheral/CMakeLists.txt | 3 + .../peripheral/src/peripheral.c | 16 ++-- .../id_addr_update/peripheral/src/utils.c | 39 +++------ .../id_addr_update/peripheral/src/utils.h | 3 - .../security_changed_callback/CMakeLists.txt | 3 + .../src/bs_bt_utils.c | 41 +++------ .../src/bs_bt_utils.h | 47 +---------- .../security_changed_callback/src/central.c | 4 +- .../security_changed_callback/src/main.c | 6 -- .../src/peripheral.c | 13 +-- .../battery_service/src/central_test.c | 6 +- 181 files changed, 996 insertions(+), 2544 deletions(-) delete mode 100644 tests/bsim/bluetooth/host/att/mtu_update/src/common.h delete mode 100644 tests/bsim/bluetooth/host/att/pipeline/common/utils.h delete mode 100644 tests/bsim/bluetooth/host/att/read_fill_buf/bs_macro.h delete mode 100644 tests/bsim/bluetooth/host/att/read_fill_buf/bs_main.c delete mode 100644 tests/bsim/bluetooth/host/att/retry_on_sec_err/test_utils.c delete mode 100644 tests/bsim/bluetooth/host/att/retry_on_sec_err/test_utils.h create mode 100644 tests/bsim/bluetooth/host/att/sequential/common/common_defs.h delete mode 100644 tests/bsim/bluetooth/host/att/sequential/common/utils.h delete mode 100644 tests/bsim/bluetooth/host/gatt/notify_multiple/src/common.c delete mode 100644 tests/bsim/bluetooth/host/id/settings/src/common.h delete mode 100644 tests/bsim/bluetooth/host/l2cap/split/common/utils.h create mode 100644 tests/bsim/bluetooth/host/misc/disconnect/common/common.h delete mode 100644 tests/bsim/bluetooth/host/misc/disconnect/common/sync.c delete mode 100644 tests/bsim/bluetooth/host/misc/disconnect/common/sync.h delete mode 100644 tests/bsim/bluetooth/host/misc/disconnect/common/utils.h delete mode 100644 tests/bsim/bluetooth/host/misc/hfc/src/utils.h delete mode 100644 tests/bsim/bluetooth/host/privacy/central/src/bs_bt_utils.c delete mode 100644 tests/bsim/bluetooth/host/privacy/central/src/bs_bt_utils.h delete mode 100644 tests/bsim/bluetooth/host/privacy/legacy/src/bs_bt_utils.c delete mode 100644 tests/bsim/bluetooth/host/privacy/legacy/src/bs_bt_utils.h delete mode 100644 tests/bsim/bluetooth/host/privacy/peripheral/src/bs_bt_utils.c delete mode 100644 tests/bsim/bluetooth/host/privacy/peripheral/src/bs_bt_utils.h diff --git a/tests/bsim/bluetooth/host/adv/chain/CMakeLists.txt b/tests/bsim/bluetooth/host/adv/chain/CMakeLists.txt index 032cd1d68bc0..9aaea1738643 100644 --- a/tests/bsim/bluetooth/host/adv/chain/CMakeLists.txt +++ b/tests/bsim/bluetooth/host/adv/chain/CMakeLists.txt @@ -5,6 +5,9 @@ cmake_minimum_required(VERSION 3.20.0) find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) project(bsim_test_adv_chain) +add_subdirectory(${ZEPHYR_BASE}/tests/bsim/babblekit babblekit) +target_link_libraries(app PRIVATE babblekit) + target_sources(app PRIVATE src/main.c ${ZEPHYR_BASE}/samples/bluetooth/broadcaster_multiple/src/broadcaster_multiple.c diff --git a/tests/bsim/bluetooth/host/adv/chain/src/main.c b/tests/bsim/bluetooth/host/adv/chain/src/main.c index ad7181253186..4feb20b87c1f 100644 --- a/tests/bsim/bluetooth/host/adv/chain/src/main.c +++ b/tests/bsim/bluetooth/host/adv/chain/src/main.c @@ -14,22 +14,7 @@ #include #include -#include "bs_types.h" -#include "bs_tracing.h" -#include "time_machine.h" -#include "bstests.h" - -#define FAIL(...) \ - do { \ - bst_result = Failed; \ - bs_trace_error_time_line(__VA_ARGS__); \ - } while (0) - -#define PASS(...) \ - do { \ - bst_result = Passed; \ - bs_trace_info_time(1, __VA_ARGS__); \ - } while (0) +#include "babblekit/testcase.h" #define NAME_LEN 30 #define BT_AD_DATA_NAME_SIZE (sizeof(CONFIG_BT_DEVICE_NAME) - 1U + 2U) @@ -44,8 +29,6 @@ static K_SEM_DEFINE(sem_recv, 0, 1); -extern enum bst_result_t bst_result; - static void test_adv_main(void) { extern int broadcaster_multiple(void); @@ -53,7 +36,7 @@ static void test_adv_main(void) err = bt_enable(NULL); if (err) { - FAIL("Bluetooth init failed\n"); + TEST_FAIL("Bluetooth init failed"); bs_trace_silent_exit(err); return; @@ -61,13 +44,13 @@ static void test_adv_main(void) err = broadcaster_multiple(); if (err) { - FAIL("Adv tests failed\n"); + TEST_FAIL("Adv tests failed"); bs_trace_silent_exit(err); return; } /* Successfully started advertising multiple sets */ - PASS("Adv tests passed\n"); + TEST_PASS("Adv tests passed"); /* Let the scanner receive the reports */ k_sleep(K_SECONDS(10)); @@ -146,7 +129,7 @@ static void test_scan_main(void) err = bt_enable(NULL); if (err) { - FAIL("Bluetooth init failed\n"); + TEST_FAIL("Bluetooth init failed"); bs_trace_silent_exit(err); return; @@ -156,7 +139,7 @@ static void test_scan_main(void) err = observer_start(); if (err) { - FAIL("Observer start failed\n"); + TEST_FAIL("Observer start failed"); bs_trace_silent_exit(err); return; @@ -167,42 +150,26 @@ static void test_scan_main(void) err = k_sem_take(&sem_recv, K_NO_WAIT); if (err) { - FAIL("Scan receive failed\n"); + TEST_FAIL("Scan receive failed"); bs_trace_silent_exit(err); return; } - PASS("Scan tests passed\n"); + TEST_PASS("Scan tests passed"); bs_trace_silent_exit(0); } -static void test_adv_chain_init(void) -{ - bst_ticker_set_next_tick_absolute(60e6); - bst_result = In_progress; -} - -static void test_adv_chain_tick(bs_time_t HW_device_time) -{ - bst_result = Failed; - bs_trace_error_line("Test GATT Write finished.\n"); -} - static const struct bst_test_instance test_def[] = { { .test_id = "adv", .test_descr = "Central GATT Write", - .test_pre_init_f = test_adv_chain_init, - .test_tick_f = test_adv_chain_tick, .test_main_f = test_adv_main }, { .test_id = "scan", .test_descr = "Peripheral GATT Write", - .test_pre_init_f = test_adv_chain_init, - .test_tick_f = test_adv_chain_tick, .test_main_f = test_scan_main }, BSTEST_END_MARKER diff --git a/tests/bsim/bluetooth/host/adv/extended/src/ext_adv_advertiser.c b/tests/bsim/bluetooth/host/adv/extended/src/ext_adv_advertiser.c index 492b26b106ed..186d77f6f674 100644 --- a/tests/bsim/bluetooth/host/adv/extended/src/ext_adv_advertiser.c +++ b/tests/bsim/bluetooth/host/adv/extended/src/ext_adv_advertiser.c @@ -18,8 +18,8 @@ extern enum bst_result_t bst_result; static struct bt_conn *g_conn; -static DEFINE_FLAG(flag_connected); -static DEFINE_FLAG(flag_conn_recycled); +DEFINE_FLAG_STATIC(flag_connected); +DEFINE_FLAG_STATIC(flag_conn_recycled); static void common_init(void) { diff --git a/tests/bsim/bluetooth/host/adv/extended/src/ext_adv_scanner.c b/tests/bsim/bluetooth/host/adv/extended/src/ext_adv_scanner.c index 231b344ca0f4..f9299d1b4994 100644 --- a/tests/bsim/bluetooth/host/adv/extended/src/ext_adv_scanner.c +++ b/tests/bsim/bluetooth/host/adv/extended/src/ext_adv_scanner.c @@ -18,9 +18,9 @@ extern enum bst_result_t bst_result; static struct bt_conn *g_conn; -static DEFINE_FLAG(flag_ext_adv_seen); -static DEFINE_FLAG(flag_connected); -static DEFINE_FLAG(flag_conn_recycled); +DEFINE_FLAG_STATIC(flag_ext_adv_seen); +DEFINE_FLAG_STATIC(flag_connected); +DEFINE_FLAG_STATIC(flag_conn_recycled); static void connected(struct bt_conn *conn, uint8_t err) { diff --git a/tests/bsim/bluetooth/host/adv/long_ad/src/advertiser.c b/tests/bsim/bluetooth/host/adv/long_ad/src/advertiser.c index b4fbc02b0d2a..308d96eee24c 100644 --- a/tests/bsim/bluetooth/host/adv/long_ad/src/advertiser.c +++ b/tests/bsim/bluetooth/host/adv/long_ad/src/advertiser.c @@ -32,7 +32,7 @@ static void create_adv(struct bt_le_ext_adv **adv) err = bt_le_ext_adv_create(¶ms, NULL, adv); if (err) { - TEST_FAIL("Failed to create advertiser (%d)\n", err); + TEST_FAIL("Failed to create advertiser (%d)", err); } } @@ -49,7 +49,7 @@ static void start_adv(struct bt_le_ext_adv *adv) err = bt_le_ext_adv_start(adv, &start_params); if (err) { - TEST_FAIL("Failed to start advertiser (%d)\n", err); + TEST_FAIL("Failed to start advertiser (%d)", err); } } @@ -99,7 +99,7 @@ static int set_ad_data(struct bt_le_ext_adv *adv, const uint8_t *serialized_ad, err = bt_le_ext_adv_set_data(adv, ad, ad_len, NULL, 0); if (err != 0 && err != -EDOM) { - TEST_FAIL("Failed to set advertising data (%d)\n", err); + TEST_FAIL("Failed to set advertising data (%d)", err); } return err; diff --git a/tests/bsim/bluetooth/host/adv/long_ad/src/scanner.c b/tests/bsim/bluetooth/host/adv/long_ad/src/scanner.c index b1d17877b741..e2ca5b60816e 100644 --- a/tests/bsim/bluetooth/host/adv/long_ad/src/scanner.c +++ b/tests/bsim/bluetooth/host/adv/long_ad/src/scanner.c @@ -62,7 +62,7 @@ static void start_scan(void) err = bt_le_scan_start(BT_LE_SCAN_PASSIVE, device_found); if (err) { - TEST_FAIL("Scanning failed to start (err %d)\n", err); + TEST_FAIL("Scanning failed to start (err %d)", err); } LOG_DBG("Scanning successfully started"); diff --git a/tests/bsim/bluetooth/host/adv/periodic/src/per_adv_advertiser.c b/tests/bsim/bluetooth/host/adv/periodic/src/per_adv_advertiser.c index 3085bda474a9..62385d3ce2d6 100644 --- a/tests/bsim/bluetooth/host/adv/periodic/src/per_adv_advertiser.c +++ b/tests/bsim/bluetooth/host/adv/periodic/src/per_adv_advertiser.c @@ -23,8 +23,8 @@ extern enum bst_result_t bst_result; static struct bt_conn *g_conn; -static DEFINE_FLAG(flag_connected); -static DEFINE_FLAG(flag_bonded); +DEFINE_FLAG_STATIC(flag_connected); +DEFINE_FLAG_STATIC(flag_bonded); static void connected(struct bt_conn *conn, uint8_t err) { diff --git a/tests/bsim/bluetooth/host/adv/periodic/src/per_adv_sync.c b/tests/bsim/bluetooth/host/adv/periodic/src/per_adv_sync.c index 0b7a8969f9a0..2938bf9846f2 100644 --- a/tests/bsim/bluetooth/host/adv/periodic/src/per_adv_sync.c +++ b/tests/bsim/bluetooth/host/adv/periodic/src/per_adv_sync.c @@ -25,12 +25,12 @@ static struct bt_conn *g_conn; static bt_addr_le_t per_addr; static uint8_t per_sid; -static DEFINE_FLAG(flag_connected); -static DEFINE_FLAG(flag_bonded); -static DEFINE_FLAG(flag_per_adv); -static DEFINE_FLAG(flag_per_adv_sync); -static DEFINE_FLAG(flag_per_adv_sync_lost); -static DEFINE_FLAG(flag_per_adv_recv); +DEFINE_FLAG_STATIC(flag_connected); +DEFINE_FLAG_STATIC(flag_bonded); +DEFINE_FLAG_STATIC(flag_per_adv); +DEFINE_FLAG_STATIC(flag_per_adv_sync); +DEFINE_FLAG_STATIC(flag_per_adv_sync_lost); +DEFINE_FLAG_STATIC(flag_per_adv_recv); static void connected(struct bt_conn *conn, uint8_t err) { diff --git a/tests/bsim/bluetooth/host/att/eatt/src/main_reconfigure.c b/tests/bsim/bluetooth/host/att/eatt/src/main_reconfigure.c index 138583f4ce26..bab8e25264e1 100644 --- a/tests/bsim/bluetooth/host/att/eatt/src/main_reconfigure.c +++ b/tests/bsim/bluetooth/host/att/eatt/src/main_reconfigure.c @@ -15,7 +15,7 @@ #define NEW_MTU 100 -static DEFINE_FLAG(flag_reconfigured); +DEFINE_FLAG_STATIC(flag_reconfigured); void att_mtu_updated(struct bt_conn *conn, uint16_t tx, uint16_t rx) { diff --git a/tests/bsim/bluetooth/host/att/eatt_notif/src/client_test.c b/tests/bsim/bluetooth/host/att/eatt_notif/src/client_test.c index e846b1d05b94..036478bb4104 100644 --- a/tests/bsim/bluetooth/host/att/eatt_notif/src/client_test.c +++ b/tests/bsim/bluetooth/host/att/eatt_notif/src/client_test.c @@ -30,9 +30,9 @@ #include "babblekit/sync.h" #include "common.h" -static DEFINE_FLAG(flag_is_connected); -static DEFINE_FLAG(flag_discover_complete); -static DEFINE_FLAG(flag_is_encrypted); +DEFINE_FLAG_STATIC(flag_is_connected); +DEFINE_FLAG_STATIC(flag_discover_complete); +DEFINE_FLAG_STATIC(flag_is_encrypted); static struct bt_conn *g_conn; static const struct bt_gatt_attr *local_attr; diff --git a/tests/bsim/bluetooth/host/att/eatt_notif/src/server_test.c b/tests/bsim/bluetooth/host/att/eatt_notif/src/server_test.c index 94cf4ab699b1..6ac5a6662ace 100644 --- a/tests/bsim/bluetooth/host/att/eatt_notif/src/server_test.c +++ b/tests/bsim/bluetooth/host/att/eatt_notif/src/server_test.c @@ -20,11 +20,11 @@ #include "babblekit/sync.h" #include "common.h" -static DEFINE_FLAG(flag_discover_complete); +DEFINE_FLAG_STATIC(flag_discover_complete); extern enum bst_result_t bst_result; -static DEFINE_FLAG(flag_is_connected); +DEFINE_FLAG_STATIC(flag_is_connected); static struct bt_conn *g_conn; diff --git a/tests/bsim/bluetooth/host/att/mtu_update/CMakeLists.txt b/tests/bsim/bluetooth/host/att/mtu_update/CMakeLists.txt index 737bd9b5d85b..adb53fbca4c7 100644 --- a/tests/bsim/bluetooth/host/att/mtu_update/CMakeLists.txt +++ b/tests/bsim/bluetooth/host/att/mtu_update/CMakeLists.txt @@ -5,6 +5,9 @@ cmake_minimum_required(VERSION 3.20.0) find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) project(bsim_test_mtu_update) +add_subdirectory(${ZEPHYR_BASE}/tests/bsim/babblekit babblekit) +target_link_libraries(app PRIVATE babblekit) + target_include_directories(app PRIVATE src) target_sources_ifdef(CONFIG_BT_PERIPHERAL app PRIVATE diff --git a/tests/bsim/bluetooth/host/att/mtu_update/src/common.h b/tests/bsim/bluetooth/host/att/mtu_update/src/common.h deleted file mode 100644 index 426bef3200fb..000000000000 --- a/tests/bsim/bluetooth/host/att/mtu_update/src/common.h +++ /dev/null @@ -1,37 +0,0 @@ -/* - * Common functions and helpers for MTU Update test - * - * Copyright (c) 2022 Nordic Semiconductor ASA - * - * SPDX-License-Identifier: Apache-2.0 - */ - -#include - -#include "bs_types.h" -#include "bs_tracing.h" -#include "bstests.h" - -#define FAIL(...) \ - do { \ - bst_result = Failed; \ - bs_trace_error_time_line(__VA_ARGS__); \ - } while (0) - -#define PASS(...) \ - do { \ - bst_result = Passed; \ - bs_trace_info_time(1, __VA_ARGS__); \ - } while (0) - -extern enum bst_result_t bst_result; - -#define CREATE_FLAG(flag) static atomic_t flag = (atomic_t)false -#define SET_FLAG(flag) (void)atomic_set(&flag, (atomic_t)true) -#define WAIT_FOR_FLAG(flag) \ - while (!(bool)atomic_get(&flag)) { \ - (void)k_sleep(K_MSEC(1)); \ - } - -#define WAIT_TIME (20e6) /* 20 seconds */ -#define PERIPHERAL_NOTIFY_TIME ((WAIT_TIME - 10e6) / 1e6) diff --git a/tests/bsim/bluetooth/host/att/mtu_update/src/main_central.c b/tests/bsim/bluetooth/host/att/mtu_update/src/main_central.c index d41cf8464778..1f4854a7b81a 100644 --- a/tests/bsim/bluetooth/host/att/mtu_update/src/main_central.c +++ b/tests/bsim/bluetooth/host/att/mtu_update/src/main_central.c @@ -14,15 +14,15 @@ #include -#include "common.h" -#include "time_machine.h" +#include "babblekit/testcase.h" +#include "babblekit/flags.h" #include LOG_MODULE_REGISTER(bt_bsim_mtu_update, LOG_LEVEL_DBG); extern void run_central_sample(void *cb); -CREATE_FLAG(flag_notification_received); +DEFINE_FLAG_STATIC(flag_notification_received); uint8_t notify_data[100] = {}; uint8_t is_data_equal; @@ -51,31 +51,16 @@ static void test_central_main(void) WAIT_FOR_FLAG(flag_notification_received); if (is_data_equal) { - PASS("MTU Update test passed\n"); + TEST_PASS("MTU Update test passed"); } else { - FAIL("MTU Update test failed\n"); + TEST_FAIL("MTU Update test failed"); } } -void test_tick(bs_time_t HW_device_time) -{ - if (bst_result != Passed) { - FAIL("Test failed (not passed after %i seconds)\n", WAIT_TIME); - } -} - -static void test_mtu_update_init(void) -{ - bst_ticker_set_next_tick_absolute(WAIT_TIME); - bst_result = In_progress; -} - static const struct bst_test_instance test_def[] = { { .test_id = "central", .test_descr = "Central GATT MTU Update", - .test_pre_init_f = test_mtu_update_init, - .test_tick_f = test_tick, .test_main_f = test_central_main }, BSTEST_END_MARKER diff --git a/tests/bsim/bluetooth/host/att/mtu_update/src/main_peripheral.c b/tests/bsim/bluetooth/host/att/mtu_update/src/main_peripheral.c index 9124965b67ba..8f76d2d6d3c4 100644 --- a/tests/bsim/bluetooth/host/att/mtu_update/src/main_peripheral.c +++ b/tests/bsim/bluetooth/host/att/mtu_update/src/main_peripheral.c @@ -12,12 +12,13 @@ #include -#include "common.h" -#include "time_machine.h" +#include "babblekit/testcase.h" #include LOG_MODULE_REGISTER(bt_bsim_mtu_update, LOG_LEVEL_DBG); +#define PERIPHERAL_NOTIFY_TIME 10 /* seconds */ + extern void run_peripheral_sample(uint8_t *notify_data, size_t notify_data_size, uint16_t seconds); uint8_t notify_data[100] = {}; @@ -29,28 +30,13 @@ static void test_peripheral_main(void) run_peripheral_sample(notify_data, sizeof(notify_data), PERIPHERAL_NOTIFY_TIME); - PASS("MTU Update test passed\n"); -} - -void test_tick(bs_time_t HW_device_time) -{ - if (bst_result != Passed) { - FAIL("Test failed (not passed after %i seconds)\n", WAIT_TIME); - } -} - -static void test_mtu_update_init(void) -{ - bst_ticker_set_next_tick_absolute(WAIT_TIME); - bst_result = In_progress; + TEST_PASS("MTU Update test passed"); } static const struct bst_test_instance test_def[] = { { .test_id = "peripheral", .test_descr = "Peripheral GATT MTU Update", - .test_pre_init_f = test_mtu_update_init, - .test_tick_f = test_tick, .test_main_f = test_peripheral_main }, BSTEST_END_MARKER diff --git a/tests/bsim/bluetooth/host/att/mtu_update/test_scripts/run_test.sh b/tests/bsim/bluetooth/host/att/mtu_update/test_scripts/run_test.sh index f2947d62b124..b8cae7776531 100755 --- a/tests/bsim/bluetooth/host/att/mtu_update/test_scripts/run_test.sh +++ b/tests/bsim/bluetooth/host/att/mtu_update/test_scripts/run_test.sh @@ -20,6 +20,6 @@ Execute "$peripheral_exe" \ -v=${verbosity_level} -s=${simulation_id} -d=1 -testid=peripheral -RealEncryption=1 Execute ./bs_2G4_phy_v1 -v=${verbosity_level} -s=${simulation_id} \ - -D=2 -sim_length=60e6 $@ -argschannel -at=40 + -D=2 -sim_length=20e6 $@ -argschannel -at=40 wait_for_background_jobs diff --git a/tests/bsim/bluetooth/host/att/pipeline/common/utils.h b/tests/bsim/bluetooth/host/att/pipeline/common/utils.h deleted file mode 100644 index 60f2396fbb9a..000000000000 --- a/tests/bsim/bluetooth/host/att/pipeline/common/utils.h +++ /dev/null @@ -1,62 +0,0 @@ -/* - * Copyright (c) 2023 Nordic Semiconductor ASA - * - * SPDX-License-Identifier: Apache-2.0 - */ - -#include "bs_tracing.h" -#include "bs_types.h" -#include "bstests.h" - -#define BS_SECONDS(dur_sec) ((bs_time_t)dur_sec * USEC_PER_SEC) - -extern enum bst_result_t bst_result; - -#define DECLARE_FLAG(flag) extern atomic_t flag -#define DEFINE_FLAG(flag) atomic_t flag = (atomic_t) false -#define TEST_FLAG(flag) (bool)atomic_get(&flag) -#define SET_FLAG(flag) (void)atomic_set(&flag, (atomic_t) true) -#define UNSET_FLAG(flag) (void)atomic_set(&flag, (atomic_t) false) - -#define WAIT_FOR_VAL(var, val) \ - while (atomic_get(&var) != val) { \ - (void)k_sleep(K_MSEC(1)); \ - } -#define WAIT_FOR_FLAG(flag) \ - while (!(bool)atomic_get(&flag)) { \ - (void)k_sleep(K_MSEC(1)); \ - } -#define WAIT_FOR_FLAG_UNSET(flag) \ - while ((bool)atomic_get(&flag)) { \ - (void)k_sleep(K_MSEC(1)); \ - } -#define TAKE_FLAG(flag) \ - while (!(bool)atomic_cas(&flag, true, false)) { \ - (void)k_sleep(K_MSEC(1)); \ - } - -#define ASSERT(expr, ...) \ - do { \ - if (!(expr)) { \ - FAIL(__VA_ARGS__); \ - } \ - } while (0) - -#define FAIL(...) \ - do { \ - bst_result = Failed; \ - bs_trace_error_time_line(__VA_ARGS__); \ - } while (0) - -#define PASS(...) \ - do { \ - bst_result = Passed; \ - bs_trace_info_time(1, __VA_ARGS__); \ - } while (0) - -#define TEST_TIMEOUT_SIMULATED BS_SECONDS(100) -#define HVX_HANDLE 0x0012 -#define DUT_DEVICE_NBR 0 - -/* Run for more than ATT_TIMEOUT */ -#define PROCEDURE_1_TIMEOUT_MS (1000 * 70) diff --git a/tests/bsim/bluetooth/host/att/pipeline/dut/CMakeLists.txt b/tests/bsim/bluetooth/host/att/pipeline/dut/CMakeLists.txt index 2fa987356808..20d7105045e3 100644 --- a/tests/bsim/bluetooth/host/att/pipeline/dut/CMakeLists.txt +++ b/tests/bsim/bluetooth/host/att/pipeline/dut/CMakeLists.txt @@ -5,6 +5,7 @@ cmake_minimum_required(VERSION 3.20.0) find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) project(bsim_test_pipeline_dut) +add_subdirectory(${ZEPHYR_BASE}/tests/bsim/babblekit babblekit) add_subdirectory(${ZEPHYR_BASE}/tests/bluetooth/common/testlib testlib) target_sources(app PRIVATE @@ -12,9 +13,12 @@ target_sources(app PRIVATE ) zephyr_include_directories( - ../common/ ${BSIM_COMPONENTS_PATH}/libUtilv1/src/ ${BSIM_COMPONENTS_PATH}/libPhyComv1/src/ ) -target_link_libraries(app PRIVATE testlib) +target_link_libraries(app +PRIVATE + testlib + babblekit +) diff --git a/tests/bsim/bluetooth/host/att/pipeline/dut/src/main.c b/tests/bsim/bluetooth/host/att/pipeline/dut/src/main.c index a380ff615e26..46b4722ec65e 100644 --- a/tests/bsim/bluetooth/host/att/pipeline/dut/src/main.c +++ b/tests/bsim/bluetooth/host/att/pipeline/dut/src/main.c @@ -18,20 +18,18 @@ #include "testlib/att_read.h" -#include /* For get_device_nbr() */ -#include "utils.h" -#include "bstests.h" - -#define strucc struct +#include "bsim_args_runner.h" +#include "babblekit/testcase.h" +#include "babblekit/flags.h" #include LOG_MODULE_REGISTER(main, LOG_LEVEL_INF); -DEFINE_FLAG(is_connected); -DEFINE_FLAG(is_subscribed); -DEFINE_FLAG(one_indication); -DEFINE_FLAG(two_notifications); -DEFINE_FLAG(flag_data_length_updated); +/* Run for more than ATT_TIMEOUT */ +#define PROCEDURE_1_TIMEOUT_MS (1000 * 70) + +DEFINE_FLAG_STATIC(is_connected); +DEFINE_FLAG_STATIC(flag_data_length_updated); static struct bt_conn *dconn; @@ -45,7 +43,7 @@ static void connected(struct bt_conn *conn, uint8_t conn_err) bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr)); if (conn_err) { - FAIL("Failed to connect to %s (%u)", addr, conn_err); + TEST_FAIL("Failed to connect to %s (%u)", addr, conn_err); return; } @@ -85,7 +83,7 @@ static void do_dlu(struct bt_conn *conn) LOG_INF("update DL"); err = bt_conn_le_data_len_update(conn, ¶m); - ASSERT(err == 0, "Can't update data length (err %d)\n", err); + TEST_ASSERT(err == 0, "Can't update data length (err %d)", err); WAIT_FOR_FLAG(flag_data_length_updated); } @@ -106,7 +104,7 @@ static void device_found(const bt_addr_le_t *addr, int8_t rssi, uint8_t type, err = bt_le_scan_stop(); if (err) { - FAIL("Stop LE scan failed (err %d)", err); + TEST_FAIL("Stop LE scan failed (err %d)", err); return; } @@ -116,12 +114,12 @@ static void device_found(const bt_addr_le_t *addr, int8_t rssi, uint8_t type, param = BT_LE_CONN_PARAM_DEFAULT; err = bt_conn_le_create(addr, BT_CONN_LE_CREATE_CONN, param, &conn); if (err) { - FAIL("Create conn failed (err %d)", err); + TEST_FAIL("Create conn failed (err %d)", err); return; } } -static strucc bt_conn *connecc(void) +static struct bt_conn *connecc(void) { int err; struct bt_conn *conn; @@ -129,12 +127,12 @@ static strucc bt_conn *connecc(void) UNSET_FLAG(is_connected); err = bt_le_adv_start(BT_LE_ADV_CONN_FAST_1, NULL, 0, NULL, 0); - ASSERT(!err, "Adving failed to start (err %d)\n", err); + TEST_ASSERT(!err, "Adving failed to start (err %d)", err); LOG_DBG(" wait connecc..."); WAIT_FOR_FLAG(is_connected); - LOG_INF("conecd"); + LOG_INF("Connected"); conn = dconn; dconn = NULL; @@ -150,7 +148,7 @@ static struct bt_conn *connect(void) UNSET_FLAG(is_connected); err = bt_le_scan_start(BT_LE_SCAN_ACTIVE_CONTINUOUS, device_found); - ASSERT(!err, "Scanning failed to start (err %d)\n", err); + TEST_ASSERT(!err, "Scanning failed to start (err %d)", err); LOG_DBG("Central initiating connection..."); WAIT_FOR_FLAG(is_connected); @@ -220,7 +218,7 @@ static void send_write_handle(struct bt_conn *conn) sys_put_le16(handle, data); err = bt_gatt_notify(conn, attr, data, sizeof(data)); - ASSERT(!err, "Failed to transmit handle for write (err %d)\n", err); + TEST_ASSERT(!err, "Failed to transmit handle for write (err %d)", err); } static void gatt_read(struct bt_conn *conn, uint16_t handle) @@ -232,11 +230,11 @@ static void gatt_read(struct bt_conn *conn, uint16_t handle) NET_BUF_SIMPLE_DEFINE(buf, BT_ATT_MAX_ATTRIBUTE_LEN); err = bt_testlib_att_read_by_handle_sync(&buf, NULL, NULL, conn, 0, handle, 0); - ASSERT(!err, "Failed read: err %d", err); + TEST_ASSERT(!err, "Failed read: err %d", err); value = net_buf_simple_pull_le16(&buf); - ASSERT(prev_val == value, "Something's up: expected %d got %d", prev_val, value); + TEST_ASSERT(prev_val == value, "Something's up: expected %d got %d", prev_val, value); prev_val++; LOG_INF("Read by handle: handle %x val %d err %d", handle, value, err); @@ -251,14 +249,14 @@ static void find_the_chrc(struct bt_conn *conn, const struct bt_uuid *svc, int err; err = bt_testlib_gatt_discover_primary(&svc_handle, &svc_end_handle, conn, svc, 1, 0xffff); - ASSERT(!err, "Failed to discover service %d"); + TEST_ASSERT(!err, "Failed to discover service %d"); LOG_DBG("svc_handle: %u, svc_end_handle: %u", svc_handle, svc_end_handle); err = bt_testlib_gatt_discover_characteristic(chrc_value_handle, &chrc_end_handle, NULL, conn, chrc, (svc_handle + 1), svc_end_handle); - ASSERT(!err, "Failed to get value handle %d"); + TEST_ASSERT(!err, "Failed to get value handle %d"); LOG_DBG("chrc_value_handle: %u, chrc_end_handle: %u", *chrc_value_handle, chrc_end_handle); } @@ -271,7 +269,7 @@ void good_peer_procedure(void) struct bt_conn *conn; err = bt_enable(NULL); - ASSERT(err == 0, "Can't enable Bluetooth (err %d)\n", err); + TEST_ASSERT(err == 0, "Can't enable Bluetooth (err %d)", err); LOG_DBG("Central: Bluetooth initialized."); conn = connecc(); @@ -285,7 +283,7 @@ void good_peer_procedure(void) gatt_read(conn, handle); } - PASS("Good peer done\n"); + TEST_PASS("Good peer done"); } void dut_procedure(void) @@ -296,7 +294,7 @@ void dut_procedure(void) struct bt_conn *good, *bad; err = bt_enable(NULL); - ASSERT(err == 0, "Can't enable Bluetooth (err %d)\n", err); + TEST_ASSERT(err == 0, "Can't enable Bluetooth (err %d)", err); LOG_DBG("Central: Bluetooth initialized."); LOG_DBG("Central: Connect to good peer"); @@ -311,7 +309,7 @@ void dut_procedure(void) send_write_handle(bad); /* Pass unless some assert in callbacks fails. */ - PASS("DUT done\n"); + TEST_PASS("DUT done"); } void test_procedure_0(void) @@ -346,7 +344,7 @@ void test_procedure_0(void) * - no buffer allocation failures for responding to the good peer, * timeouts or stalls. */ - bool dut = (get_device_nbr() == DUT_DEVICE_NBR); + bool dut = (bsim_args_get_global_device_nbr() == 0); /* We use the same image for both to lighten build load. */ if (dut) { @@ -376,7 +374,7 @@ static void gatt_write(struct bt_conn *conn, struct bt_gatt_write_params *params LOG_INF("Queue GATT write"); err = bt_gatt_write(conn, params); - ASSERT(!err, "Failed write: err %d", err); + TEST_ASSERT(!err, "Failed write: err %d", err); } void test_procedure_1(void) @@ -398,7 +396,7 @@ void test_procedure_1(void) int err; err = bt_enable(NULL); - ASSERT(err == 0, "Can't enable Bluetooth (err %d)\n", err); + TEST_ASSERT(err == 0, "Can't enable Bluetooth (err %d)", err); LOG_DBG("Central: Bluetooth initialized."); struct bt_conn *tester = connect(); @@ -415,35 +413,16 @@ void test_procedure_1(void) bt_conn_unref(tester); /* Pass unless some assert in callbacks fails. */ - PASS("DUT done\n"); -} - -void test_tick(bs_time_t HW_device_time) -{ - bs_trace_debug_time(0, "Simulation ends now.\n"); - if (bst_result != Passed) { - bst_result = Failed; - bs_trace_error("Test did not pass before simulation ended.\n"); - } -} - -void test_init(void) -{ - bst_ticker_set_next_tick_absolute(TEST_TIMEOUT_SIMULATED); - bst_result = In_progress; + TEST_PASS("DUT done"); } static const struct bst_test_instance test_to_add[] = { { .test_id = "dut", - .test_pre_init_f = test_init, - .test_tick_f = test_tick, .test_main_f = test_procedure_0, }, { .test_id = "dut_1", - .test_pre_init_f = test_init, - .test_tick_f = test_tick, .test_main_f = test_procedure_1, }, BSTEST_END_MARKER, diff --git a/tests/bsim/bluetooth/host/att/pipeline/tester/CMakeLists.txt b/tests/bsim/bluetooth/host/att/pipeline/tester/CMakeLists.txt index 440ae5e49c00..fb32c26de705 100644 --- a/tests/bsim/bluetooth/host/att/pipeline/tester/CMakeLists.txt +++ b/tests/bsim/bluetooth/host/att/pipeline/tester/CMakeLists.txt @@ -5,12 +5,14 @@ cmake_minimum_required(VERSION 3.20.0) find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) project(bsim_test_pipeline_tester) +add_subdirectory(${ZEPHYR_BASE}/tests/bsim/babblekit babblekit) +target_link_libraries(app PRIVATE babblekit) + target_sources(app PRIVATE src/main.c ) zephyr_include_directories( - ../common/ ${ZEPHYR_BASE}/subsys/bluetooth/common/ ${BSIM_COMPONENTS_PATH}/libUtilv1/src/ ${BSIM_COMPONENTS_PATH}/libPhyComv1/src/ diff --git a/tests/bsim/bluetooth/host/att/pipeline/tester/src/main.c b/tests/bsim/bluetooth/host/att/pipeline/tester/src/main.c index dcc20abcfbd4..3f49e7e43cd3 100644 --- a/tests/bsim/bluetooth/host/att/pipeline/tester/src/main.c +++ b/tests/bsim/bluetooth/host/att/pipeline/tester/src/main.c @@ -21,8 +21,8 @@ #include "host/conn_internal.h" #include "host/l2cap_internal.h" -#include "utils.h" -#include "bstests.h" +#include "babblekit/testcase.h" +#include "babblekit/flags.h" #include LOG_MODULE_REGISTER(bt_tinyhost, LOG_LEVEL_INF); @@ -38,12 +38,14 @@ LOG_MODULE_REGISTER(bt_tinyhost, LOG_LEVEL_INF); #define BT_L2CAP_CID_ATT 0x0004 #define LAST_SUPPORTED_ATT_OPCODE 0x20 -DEFINE_FLAG(is_connected); -DEFINE_FLAG(flag_data_length_updated); -DEFINE_FLAG(flag_handle); -DEFINE_FLAG(flag_notified); -DEFINE_FLAG(flag_write_ack); -DEFINE_FLAG(flag_req_in_progress); +/* Run for more than ATT_TIMEOUT */ +#define PROCEDURE_1_TIMEOUT_MS (1000 * 70) + +DEFINE_FLAG_STATIC(is_connected); +DEFINE_FLAG_STATIC(flag_data_length_updated); +DEFINE_FLAG_STATIC(flag_handle); +DEFINE_FLAG_STATIC(flag_write_ack); +DEFINE_FLAG_STATIC(flag_req_in_progress); static uint16_t server_write_handle; @@ -71,7 +73,7 @@ struct net_buf *bt_hci_cmd_create(uint16_t opcode, uint8_t param_len) LOG_DBG("opcode 0x%04x param_len %u", opcode, param_len); buf = net_buf_alloc(&hci_cmd_pool, K_FOREVER); - ASSERT(buf, "failed allocation"); + TEST_ASSERT(buf, "failed allocation"); LOG_DBG("buf %p", buf); @@ -114,14 +116,14 @@ static void handle_cmd_complete(struct net_buf *buf) opcode = sys_le16_to_cpu(evt->opcode); } else { - FAIL("unhandled event 0x%x", hdr->evt); + TEST_FAIL("unhandled event 0x%x", hdr->evt); } LOG_DBG("opcode 0x%04x status %x", opcode, status); - ASSERT(status == 0x00, "cmd status: %x", status); + TEST_ASSERT(status == 0x00, "cmd status: %x", status); - ASSERT(active_opcode == opcode, "unexpected opcode %x != %x", active_opcode, opcode); + TEST_ASSERT(active_opcode == opcode, "unexpected opcode %x != %x", active_opcode, opcode); if (active_opcode) { active_opcode = 0xFFFF; @@ -208,8 +210,8 @@ static void handle_att_write_0(struct net_buf *buf) LOG_INF("Got write for 0x%04x len %d", handle, buf->len); LOG_HEXDUMP_DBG(buf->data, buf->len, "payload"); - ASSERT(!TEST_FLAG(flag_req_in_progress), - "Peer is pipelining REQs. This inSIGdent will be reported.\n"); + TEST_ASSERT(!IS_FLAG_SET(flag_req_in_progress), + "Peer is pipelining REQs. This inSIGdent will be reported."); SET_FLAG(flag_req_in_progress); send_write_rsp(); @@ -222,8 +224,8 @@ static void handle_att_write_1(struct net_buf *buf) LOG_INF("Got write for 0x%04x len %d", handle, buf->len); LOG_HEXDUMP_DBG(buf->data, buf->len, "payload"); - ASSERT(!TEST_FLAG(flag_req_in_progress), - "Peer is pipelining REQs. This inSIGdent will be reported.\n"); + TEST_ASSERT(!IS_FLAG_SET(flag_req_in_progress), + "Peer is pipelining REQs. This inSIGdent will be reported."); SET_FLAG(flag_req_in_progress); @@ -253,7 +255,7 @@ static void handle_att(struct net_buf *buf) return; default: LOG_HEXDUMP_ERR(buf->data, buf->len, "payload"); - FAIL("unhandled opcode %x\n", op); + TEST_FAIL("unhandled opcode %x", op); return; } } @@ -270,10 +272,9 @@ static void handle_l2cap(struct net_buf *buf) LOG_HEXDUMP_DBG(buf->data, buf->len, "l2cap"); /* Make sure we don't have to recombine packets */ - ASSERT(buf->len == hdr->len, "buflen = %d != hdrlen %d", - buf->len, hdr->len); + TEST_ASSERT(buf->len == hdr->len, "buflen = %d != hdrlen %d", buf->len, hdr->len); - ASSERT(cid == BT_L2CAP_CID_ATT, "We only support (U)ATT"); + TEST_ASSERT(cid == BT_L2CAP_CID_ATT, "We only support (U)ATT"); /* (U)ATT PDU */ handle_att(buf); @@ -292,8 +293,7 @@ static void handle_acl(struct net_buf *buf) flags = bt_acl_flags(handle); handle = bt_acl_handle(handle); - ASSERT(flags == BT_ACL_START, - "Fragmentation not supported"); + TEST_ASSERT(flags == BT_ACL_START, "Fragmentation not supported"); LOG_DBG("ACL: conn %d len %d flags %d", handle, len, flags); LOG_HEXDUMP_DBG(buf->data, buf->len, "HCI ACL"); @@ -353,7 +353,7 @@ static void send_cmd(uint16_t opcode, struct net_buf *cmd, struct net_buf **rsp) } k_sem_take(&cmd_sem, K_FOREVER); - ASSERT(active_opcode == 0xFFFF, ""); + TEST_ASSERT_NO_MSG(active_opcode == 0xFFFF); active_opcode = opcode; @@ -422,7 +422,7 @@ static void write_default_data_len(uint16_t tx_octets, uint16_t tx_time) struct bt_hci_cp_le_write_default_data_len *cp; struct net_buf *buf = bt_hci_cmd_create(BT_HCI_OP_LE_WRITE_DEFAULT_DATA_LEN, sizeof(*cp)); - ASSERT(buf, ""); + TEST_ASSERT_NO_MSG(buf); cp = net_buf_add(buf, sizeof(*cp)); cp->max_tx_octets = sys_cpu_to_le16(tx_octets); @@ -447,7 +447,7 @@ static void set_event_mask(uint16_t opcode) /* The two commands have the same length/params */ buf = bt_hci_cmd_create(opcode, sizeof(*cp_mask)); - ASSERT(buf, ""); + TEST_ASSERT_NO_MSG(buf); /* Forward all events */ cp_mask = net_buf_add(buf, sizeof(*cp_mask)); @@ -465,7 +465,7 @@ static void set_random_address(void) LOG_DBG("%s", bt_addr_str(&addr.a)); buf = bt_hci_cmd_create(BT_HCI_OP_LE_SET_RANDOM_ADDRESS, sizeof(addr.a)); - ASSERT(buf, ""); + TEST_ASSERT_NO_MSG(buf); net_buf_add_mem(buf, &addr.a, sizeof(addr.a)); send_cmd(BT_HCI_OP_LE_SET_RANDOM_ADDRESS, buf, NULL); @@ -505,7 +505,7 @@ struct net_buf *alloc_l2cap_pdu(void) uint16_t reserve; buf = net_buf_alloc(&acl_tx_pool, K_FOREVER); - ASSERT(buf, "failed ACL allocation"); + TEST_ASSERT(buf, "failed ACL allocation"); reserve = sizeof(struct bt_l2cap_hdr); reserve += sizeof(struct bt_hci_acl_hdr) + BT_BUF_RESERVE; @@ -540,8 +540,7 @@ static void send_l2cap_packet(struct net_buf *buf, uint16_t cid) hdr->cid = sys_cpu_to_le16(cid); /* Always entire packets, no HCI fragmentation */ - ASSERT(buf->len <= CONFIG_BT_BUF_ACL_TX_SIZE, - "Fragmentation not supported"); + TEST_ASSERT(buf->len <= CONFIG_BT_BUF_ACL_TX_SIZE, "Fragmentation not supported"); send_acl(buf); } @@ -626,7 +625,7 @@ void test_procedure_0(void) /* Verify we get at least one write */ WAIT_FOR_FLAG(flag_write_ack); - PASS("Tester done\n"); + TEST_PASS("Tester done"); } void test_procedure_1(void) @@ -649,43 +648,24 @@ void test_procedure_1(void) /* In this testcase, DUT is the aggressor. * Tester verifies no spec violation happens. */ - while (TEST_FLAG(is_connected)) { + while (IS_FLAG_SET(is_connected)) { /* Should be enough to allow DUT's app to batch a few requests. */ k_msleep(1000); - if (TEST_FLAG(flag_req_in_progress)) { + if (IS_FLAG_SET(flag_req_in_progress)) { send_write_rsp(); } } - PASS("Tester done\n"); -} - -void test_tick(bs_time_t HW_device_time) -{ - bs_trace_debug_time(0, "Simulation ends now.\n"); - if (bst_result != Passed) { - bst_result = Failed; - bs_trace_error("Test did not pass before simulation ended.\n"); - } -} - -void test_init(void) -{ - bst_ticker_set_next_tick_absolute(TEST_TIMEOUT_SIMULATED); - bst_result = In_progress; + TEST_PASS("Tester done"); } static const struct bst_test_instance test_to_add[] = { { .test_id = "tester", - .test_pre_init_f = test_init, - .test_tick_f = test_tick, .test_main_f = test_procedure_0, }, { .test_id = "tester_1", - .test_pre_init_f = test_init, - .test_tick_f = test_tick, .test_main_f = test_procedure_1, }, BSTEST_END_MARKER, diff --git a/tests/bsim/bluetooth/host/att/read_fill_buf/bs_macro.h b/tests/bsim/bluetooth/host/att/read_fill_buf/bs_macro.h deleted file mode 100644 index abe3e07e6744..000000000000 --- a/tests/bsim/bluetooth/host/att/read_fill_buf/bs_macro.h +++ /dev/null @@ -1,13 +0,0 @@ -/* Copyright (c) 2023 Nordic Semiconductor ASA - * SPDX-License-Identifier: Apache-2.0 - */ - -#include -#include - -#define PASS(...) \ - do { \ - extern enum bst_result_t bst_result; \ - bst_result = Passed; \ - bs_trace_info_time(1, __VA_ARGS__); \ - } while (0) diff --git a/tests/bsim/bluetooth/host/att/read_fill_buf/bs_main.c b/tests/bsim/bluetooth/host/att/read_fill_buf/bs_main.c deleted file mode 100644 index dc12c3fa576b..000000000000 --- a/tests/bsim/bluetooth/host/att/read_fill_buf/bs_main.c +++ /dev/null @@ -1,53 +0,0 @@ -/* Copyright (c) 2023 Nordic Semiconductor ASA - * SPDX-License-Identifier: Apache-2.0 - */ - -#include -#include -#include - -#define BS_SECONDS(dur_sec) ((bs_time_t)dur_sec * 1000000) -#define SIMULATED_TEST_TIMEOUT BS_SECONDS(60) - -void the_test(void); -extern enum bst_result_t bst_result; - -void test_init(void) -{ - bst_result = In_progress; - bst_ticker_set_next_tick_absolute(SIMULATED_TEST_TIMEOUT); -} - -void test_tick(bs_time_t HW_device_time) -{ - bs_trace_debug_time(0, "Simulation ends now.\n"); - if (bst_result == In_progress) { - bst_result = Failed; - bs_trace_error("Test did not pass before simulation ended. Consider increasing " - "simulation length.\n"); - } -} - -static const struct bst_test_instance test_to_add[] = { - { - .test_id = "the_test", - .test_pre_init_f = test_init, - .test_tick_f = test_tick, - .test_main_f = the_test, - }, - BSTEST_END_MARKER, -}; - -static struct bst_test_list *install(struct bst_test_list *tests) -{ - return bst_add_tests(tests, test_to_add); -}; - -bst_test_install_t test_installers[] = {install, NULL}; - -int main(void) -{ - bst_main(); - - return 0; -} diff --git a/tests/bsim/bluetooth/host/att/read_fill_buf/client/CMakeLists.txt b/tests/bsim/bluetooth/host/att/read_fill_buf/client/CMakeLists.txt index a384dcbed860..dabb2daff7f1 100644 --- a/tests/bsim/bluetooth/host/att/read_fill_buf/client/CMakeLists.txt +++ b/tests/bsim/bluetooth/host/att/read_fill_buf/client/CMakeLists.txt @@ -6,7 +6,6 @@ find_package(Zephyr HINTS $ENV{ZEPHYR_BASE}) project(app) target_sources(app PRIVATE - ../bs_main.c main.c ) @@ -15,5 +14,10 @@ zephyr_include_directories( ${BSIM_COMPONENTS_PATH}/libUtilv1/src/ ) +add_subdirectory(${ZEPHYR_BASE}/tests/bsim/babblekit babblekit) add_subdirectory(${ZEPHYR_BASE}/tests/bluetooth/common/testlib testlib) -target_link_libraries(app PRIVATE testlib) +target_link_libraries(app + PRIVATE + babblekit + testlib +) diff --git a/tests/bsim/bluetooth/host/att/read_fill_buf/client/main.c b/tests/bsim/bluetooth/host/att/read_fill_buf/client/main.c index 159ce5cb27dd..5e77c7c8993a 100644 --- a/tests/bsim/bluetooth/host/att/read_fill_buf/client/main.c +++ b/tests/bsim/bluetooth/host/att/read_fill_buf/client/main.c @@ -15,7 +15,7 @@ #include #include -#include "../bs_macro.h" +#include "babblekit/testcase.h" #include "../common_defs.h" LOG_MODULE_REGISTER(client, LOG_LEVEL_DBG); @@ -69,7 +69,7 @@ void test_long_read(struct bt_conn *conn, enum bt_att_chan_opt bearer) } } -void the_test(void) +static void test_cli_main(void) { bt_addr_le_t scan_result; int err; @@ -103,5 +103,27 @@ void the_test(void) bt_conn_unref(conn); conn = NULL; - PASS("PASS\n"); + TEST_PASS("PASS"); +} + +static const struct bst_test_instance test_def[] = { + { + .test_id = "cli", + .test_main_f = test_cli_main, + }, + BSTEST_END_MARKER, +}; + +static struct bst_test_list *install(struct bst_test_list *tests) +{ + return bst_add_tests(tests, test_def); +}; + +bst_test_install_t test_installers[] = {install, NULL}; + +int main(void) +{ + bst_main(); + + return 0; } diff --git a/tests/bsim/bluetooth/host/att/read_fill_buf/server/CMakeLists.txt b/tests/bsim/bluetooth/host/att/read_fill_buf/server/CMakeLists.txt index a384dcbed860..dabb2daff7f1 100644 --- a/tests/bsim/bluetooth/host/att/read_fill_buf/server/CMakeLists.txt +++ b/tests/bsim/bluetooth/host/att/read_fill_buf/server/CMakeLists.txt @@ -6,7 +6,6 @@ find_package(Zephyr HINTS $ENV{ZEPHYR_BASE}) project(app) target_sources(app PRIVATE - ../bs_main.c main.c ) @@ -15,5 +14,10 @@ zephyr_include_directories( ${BSIM_COMPONENTS_PATH}/libUtilv1/src/ ) +add_subdirectory(${ZEPHYR_BASE}/tests/bsim/babblekit babblekit) add_subdirectory(${ZEPHYR_BASE}/tests/bluetooth/common/testlib testlib) -target_link_libraries(app PRIVATE testlib) +target_link_libraries(app + PRIVATE + babblekit + testlib +) diff --git a/tests/bsim/bluetooth/host/att/read_fill_buf/server/main.c b/tests/bsim/bluetooth/host/att/read_fill_buf/server/main.c index 244eb6e7a662..0ca266257dbb 100644 --- a/tests/bsim/bluetooth/host/att/read_fill_buf/server/main.c +++ b/tests/bsim/bluetooth/host/att/read_fill_buf/server/main.c @@ -10,7 +10,7 @@ #include -#include "../bs_macro.h" +#include "babblekit/testcase.h" #include "../common_defs.h" LOG_MODULE_REGISTER(server, LOG_LEVEL_DBG); @@ -34,7 +34,7 @@ BT_GATT_SERVICE_DEFINE(long_attr_svc, BT_GATT_PRIMARY_SERVICE(MTU_VALIDATION_SVC BT_GATT_PERM_READ, read_mtu_validation_chrc, NULL, NULL)); -void the_test(void) +static void test_srv_main(void) { int err; @@ -48,5 +48,27 @@ void the_test(void) err = bt_testlib_adv_conn(NULL, BT_ID_DEFAULT, bt_get_name()); __ASSERT_NO_MSG(!err); - PASS("PASS\n"); + TEST_PASS("PASS"); +} + +static const struct bst_test_instance test_def[] = { + { + .test_id = "srv", + .test_main_f = test_srv_main, + }, + BSTEST_END_MARKER, +}; + +static struct bst_test_list *install(struct bst_test_list *tests) +{ + return bst_add_tests(tests, test_def); +}; + +bst_test_install_t test_installers[] = {install, NULL}; + +int main(void) +{ + bst_main(); + + return 0; } diff --git a/tests/bsim/bluetooth/host/att/read_fill_buf/test_scripts/run_tests.sh b/tests/bsim/bluetooth/host/att/read_fill_buf/test_scripts/run_tests.sh index 2c51d5ff3f5b..a35e1f0d2ad0 100755 --- a/tests/bsim/bluetooth/host/att/read_fill_buf/test_scripts/run_tests.sh +++ b/tests/bsim/bluetooth/host/att/read_fill_buf/test_scripts/run_tests.sh @@ -8,17 +8,17 @@ source ${ZEPHYR_BASE}/tests/bsim/sh_common.source verbosity_level=2 simulation_id="read_fill_buf" -test_exe_d0="${BSIM_OUT_PATH}/bin/bs_${BOARD_TS}_$(guess_test_long_name)_client_prj_conf" -test_exe_d1="${BSIM_OUT_PATH}/bin/bs_${BOARD_TS}_$(guess_test_long_name)_server_prj_conf" +test_exe_d0="./bs_${BOARD_TS}_$(guess_test_long_name)_client_prj_conf" +test_exe_d1="./bs_${BOARD_TS}_$(guess_test_long_name)_server_prj_conf" cd ${BSIM_OUT_PATH}/bin Execute "$test_exe_d0" \ - -v=${verbosity_level} -s="${simulation_id}" -d=0 -testid=the_test \ + -v=${verbosity_level} -s="${simulation_id}" -d=0 -testid=cli \ -RealEncryption=1 Execute "$test_exe_d1" \ - -v=${verbosity_level} -s="${simulation_id}" -d=1 -testid=the_test \ + -v=${verbosity_level} -s="${simulation_id}" -d=1 -testid=srv \ -RealEncryption=1 Execute ./bs_2G4_phy_v1 -v=${verbosity_level} -s="${simulation_id}" \ diff --git a/tests/bsim/bluetooth/host/att/retry_on_sec_err/client/CMakeLists.txt b/tests/bsim/bluetooth/host/att/retry_on_sec_err/client/CMakeLists.txt index b244078e7610..9a2c1c1f4ee9 100644 --- a/tests/bsim/bluetooth/host/att/retry_on_sec_err/client/CMakeLists.txt +++ b/tests/bsim/bluetooth/host/att/retry_on_sec_err/client/CMakeLists.txt @@ -5,10 +5,10 @@ cmake_minimum_required(VERSION 3.20.0) find_package(Zephyr HINTS $ENV{ZEPHYR_BASE}) project(app) +add_subdirectory(${ZEPHYR_BASE}/tests/bsim/babblekit babblekit) add_subdirectory(${ZEPHYR_BASE}/tests/bluetooth/common/testlib testlib) target_sources(app PRIVATE - ../test_utils.c main.c ) @@ -19,4 +19,5 @@ zephyr_include_directories( target_link_libraries(app PRIVATE testlib + babblekit ) diff --git a/tests/bsim/bluetooth/host/att/retry_on_sec_err/client/main.c b/tests/bsim/bluetooth/host/att/retry_on_sec_err/client/main.c index e48473fb4be6..870f9642b4d7 100644 --- a/tests/bsim/bluetooth/host/att/retry_on_sec_err/client/main.c +++ b/tests/bsim/bluetooth/host/att/retry_on_sec_err/client/main.c @@ -11,15 +11,17 @@ #include #include "../common_defs.h" -#include "../test_utils.h" #include #include "testlib/scan.h" #include "testlib/security.h" +#include "babblekit/testcase.h" +#include "babblekit/flags.h" + LOG_MODULE_REGISTER(client, LOG_LEVEL_DBG); -DEFINE_FLAG(flag_attr_read_success); +DEFINE_FLAG_STATIC(flag_attr_read_success); static uint8_t gatt_attr_read_cb(struct bt_conn *conn, uint8_t att_err, struct bt_gatt_read_params *params, const void *data, uint16_t len) @@ -96,7 +98,7 @@ static void test_client(void) bt_conn_unref(conn); conn = NULL; - PASS("PASS\n"); + TEST_PASS("PASS"); } DEFINE_FLAG(flag_pairing_in_progress); @@ -158,20 +160,16 @@ static void test_client_security_request(void) bt_conn_unref(conn); conn = NULL; - PASS("PASS\n"); + TEST_PASS("PASS"); } static const struct bst_test_instance client_tests[] = { { .test_id = "test_client", - .test_pre_init_f = test_init, - .test_tick_f = test_tick, .test_main_f = test_client, }, { .test_id = "test_client_security_request", - .test_pre_init_f = test_init, - .test_tick_f = test_tick, .test_main_f = test_client_security_request, }, BSTEST_END_MARKER, diff --git a/tests/bsim/bluetooth/host/att/retry_on_sec_err/server/CMakeLists.txt b/tests/bsim/bluetooth/host/att/retry_on_sec_err/server/CMakeLists.txt index b244078e7610..9a2c1c1f4ee9 100644 --- a/tests/bsim/bluetooth/host/att/retry_on_sec_err/server/CMakeLists.txt +++ b/tests/bsim/bluetooth/host/att/retry_on_sec_err/server/CMakeLists.txt @@ -5,10 +5,10 @@ cmake_minimum_required(VERSION 3.20.0) find_package(Zephyr HINTS $ENV{ZEPHYR_BASE}) project(app) +add_subdirectory(${ZEPHYR_BASE}/tests/bsim/babblekit babblekit) add_subdirectory(${ZEPHYR_BASE}/tests/bluetooth/common/testlib testlib) target_sources(app PRIVATE - ../test_utils.c main.c ) @@ -19,4 +19,5 @@ zephyr_include_directories( target_link_libraries(app PRIVATE testlib + babblekit ) diff --git a/tests/bsim/bluetooth/host/att/retry_on_sec_err/server/main.c b/tests/bsim/bluetooth/host/att/retry_on_sec_err/server/main.c index b2abffdc4e6a..3f3150c2cb24 100644 --- a/tests/bsim/bluetooth/host/att/retry_on_sec_err/server/main.c +++ b/tests/bsim/bluetooth/host/att/retry_on_sec_err/server/main.c @@ -12,8 +12,10 @@ #include "testlib/adv.h" #include "testlib/security.h" +#include "babblekit/testcase.h" + #include "../common_defs.h" -#include "../test_utils.h" + LOG_MODULE_REGISTER(server, LOG_LEVEL_DBG); @@ -47,7 +49,7 @@ static void test_server(void) { test_common(NULL); - PASS("PASS\n"); + TEST_PASS("PASS"); } static void test_server_security_request(void) @@ -60,20 +62,16 @@ static void test_server_security_request(void) err = bt_testlib_secure(conn, BT_SECURITY_L2); __ASSERT(!err, "err %d", err); - PASS("PASS\n"); + TEST_PASS("PASS"); } static const struct bst_test_instance server_tests[] = { { .test_id = "test_server", - .test_pre_init_f = test_init, - .test_tick_f = test_tick, .test_main_f = test_server, }, { .test_id = "test_server_security_request", - .test_pre_init_f = test_init, - .test_tick_f = test_tick, .test_main_f = test_server_security_request, }, BSTEST_END_MARKER, diff --git a/tests/bsim/bluetooth/host/att/retry_on_sec_err/test_utils.c b/tests/bsim/bluetooth/host/att/retry_on_sec_err/test_utils.c deleted file mode 100644 index cacb0fa9e539..000000000000 --- a/tests/bsim/bluetooth/host/att/retry_on_sec_err/test_utils.c +++ /dev/null @@ -1,21 +0,0 @@ -/* Copyright (c) 2023 Codecoup - * SPDX-License-Identifier: Apache-2.0 - */ - -#include "test_utils.h" - -void test_init(void) -{ - bst_result = In_progress; - bst_ticker_set_next_tick_absolute(SIMULATED_TEST_TIMEOUT); -} - -void test_tick(bs_time_t HW_device_time) -{ - bs_trace_debug_time(0, "Simulation ends now.\n"); - if (bst_result == In_progress) { - bst_result = Failed; - bs_trace_error("Test did not pass before simulation ended. Consider increasing " - "simulation length.\n"); - } -} diff --git a/tests/bsim/bluetooth/host/att/retry_on_sec_err/test_utils.h b/tests/bsim/bluetooth/host/att/retry_on_sec_err/test_utils.h deleted file mode 100644 index 0d5113f00f60..000000000000 --- a/tests/bsim/bluetooth/host/att/retry_on_sec_err/test_utils.h +++ /dev/null @@ -1,29 +0,0 @@ -/* Copyright (c) 2023 Codecoup - * SPDX-License-Identifier: Apache-2.0 - */ - -#include -#include - -#define DECLARE_FLAG(flag) extern atomic_t flag -#define DEFINE_FLAG(flag) atomic_t flag = (atomic_t) false -#define SET_FLAG(flag) (void)atomic_set(&flag, (atomic_t) true) -#define UNSET_FLAG(flag) (void)atomic_set(&flag, (atomic_t) false) -#define WAIT_FOR_FLAG(flag) \ - while (!(bool)atomic_get(&flag)) { \ - (void)k_sleep(K_MSEC(1)); \ - } - -#define PASS(...) \ - do { \ - bst_result = Passed; \ - bs_trace_info_time(1, __VA_ARGS__); \ - } while (0) - -#define BS_SECONDS(dur_sec) ((bs_time_t)dur_sec * 1000000) -#define SIMULATED_TEST_TIMEOUT BS_SECONDS(60) - -extern enum bst_result_t bst_result; - -void test_init(void); -void test_tick(bs_time_t HW_device_time); diff --git a/tests/bsim/bluetooth/host/att/sequential/common/common_defs.h b/tests/bsim/bluetooth/host/att/sequential/common/common_defs.h new file mode 100644 index 000000000000..122d2f7830b2 --- /dev/null +++ b/tests/bsim/bluetooth/host/att/sequential/common/common_defs.h @@ -0,0 +1,9 @@ +/* + * Copyright (c) 2025 Nordic Semiconductor ASA + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#define HVX_HANDLE 0x0012 +#define INDICATION_PAYLOAD "indication" +#define NOTIFICATION_PAYLOAD "notification" diff --git a/tests/bsim/bluetooth/host/att/sequential/common/utils.h b/tests/bsim/bluetooth/host/att/sequential/common/utils.h deleted file mode 100644 index b4892d5a0871..000000000000 --- a/tests/bsim/bluetooth/host/att/sequential/common/utils.h +++ /dev/null @@ -1,59 +0,0 @@ -/* - * Copyright (c) 2023 Nordic Semiconductor ASA - * - * SPDX-License-Identifier: Apache-2.0 - */ - -#include "bs_tracing.h" -#include "bs_types.h" -#include "bstests.h" - -#define BS_SECONDS(dur_sec) ((bs_time_t)dur_sec * USEC_PER_SEC) -#define TEST_TIMEOUT_SIMULATED BS_SECONDS(10) - -extern enum bst_result_t bst_result; - -#define DECLARE_FLAG(flag) extern atomic_t flag -#define DEFINE_FLAG(flag) atomic_t flag = (atomic_t) false -#define SET_FLAG(flag) (void)atomic_set(&flag, (atomic_t) true) -#define UNSET_FLAG(flag) (void)atomic_set(&flag, (atomic_t) false) - -#define WAIT_FOR_VAL(var, val) \ - while (atomic_get(&var) != val) { \ - (void)k_sleep(K_MSEC(1)); \ - } -#define WAIT_FOR_FLAG(flag) \ - while (!(bool)atomic_get(&flag)) { \ - (void)k_sleep(K_MSEC(1)); \ - } -#define WAIT_FOR_FLAG_UNSET(flag) \ - while ((bool)atomic_get(&flag)) { \ - (void)k_sleep(K_MSEC(1)); \ - } -#define TAKE_FLAG(flag) \ - while (!(bool)atomic_cas(&flag, true, false)) { \ - (void)k_sleep(K_MSEC(1)); \ - } - -#define ASSERT(expr, ...) \ - do { \ - if (!(expr)) { \ - FAIL(__VA_ARGS__); \ - } \ - } while (0) - -#define FAIL(...) \ - do { \ - bst_result = Failed; \ - bs_trace_error_time_line(__VA_ARGS__); \ - } while (0) - -#define PASS(...) \ - do { \ - bst_result = Passed; \ - bs_trace_info_time(1, __VA_ARGS__); \ - } while (0) - -#define HVX_HANDLE 0x0012 -#define INDICATION_PAYLOAD "indication" -#define NOTIFICATION_PAYLOAD "notification" diff --git a/tests/bsim/bluetooth/host/att/sequential/dut/CMakeLists.txt b/tests/bsim/bluetooth/host/att/sequential/dut/CMakeLists.txt index b6c34df4916a..89ebab87ef34 100644 --- a/tests/bsim/bluetooth/host/att/sequential/dut/CMakeLists.txt +++ b/tests/bsim/bluetooth/host/att/sequential/dut/CMakeLists.txt @@ -5,6 +5,9 @@ cmake_minimum_required(VERSION 3.20.0) find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) project(bsim_test_sequential_dut) +add_subdirectory(${ZEPHYR_BASE}/tests/bsim/babblekit babblekit) +target_link_libraries(app PRIVATE babblekit) + target_sources(app PRIVATE src/main.c ) diff --git a/tests/bsim/bluetooth/host/att/sequential/dut/src/main.c b/tests/bsim/bluetooth/host/att/sequential/dut/src/main.c index 3796c83db3cf..a110689d1310 100644 --- a/tests/bsim/bluetooth/host/att/sequential/dut/src/main.c +++ b/tests/bsim/bluetooth/host/att/sequential/dut/src/main.c @@ -16,17 +16,17 @@ #include #include -#include "utils.h" -#include "bstests.h" +#include "babblekit/testcase.h" +#include "babblekit/flags.h" + +#include "common_defs.h" #include LOG_MODULE_REGISTER(dut, LOG_LEVEL_INF); -DEFINE_FLAG(is_connected); -DEFINE_FLAG(is_subscribed); -DEFINE_FLAG(one_indication); -DEFINE_FLAG(two_notifications); -DEFINE_FLAG(flag_data_length_updated); +DEFINE_FLAG_STATIC(is_connected); +DEFINE_FLAG_STATIC(is_subscribed); +DEFINE_FLAG_STATIC(flag_data_length_updated); static atomic_t nwrites; static atomic_t indications; @@ -44,7 +44,7 @@ static void connected(struct bt_conn *conn, uint8_t conn_err) bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr)); if (conn_err) { - FAIL("Failed to connect to %s (%u)", addr, conn_err); + TEST_FAIL("Failed to connect to %s (%u)", addr, conn_err); return; } @@ -84,7 +84,7 @@ static void do_dlu(void) param.tx_max_time = 2500; err = bt_conn_le_data_len_update(dconn, ¶m); - ASSERT(err == 0, "Can't update data length (err %d)\n", err); + TEST_ASSERT(err == 0, "Can't update data length (err %d)", err); WAIT_FOR_FLAG(flag_data_length_updated); } @@ -105,7 +105,7 @@ static void device_found(const bt_addr_le_t *addr, int8_t rssi, uint8_t type, err = bt_le_scan_stop(); if (err) { - FAIL("Stop LE scan failed (err %d)", err); + TEST_FAIL("Stop LE scan failed (err %d)", err); return; } @@ -115,7 +115,7 @@ static void device_found(const bt_addr_le_t *addr, int8_t rssi, uint8_t type, param = BT_LE_CONN_PARAM_DEFAULT; err = bt_conn_le_create(addr, BT_CONN_LE_CREATE_CONN, param, &conn); if (err) { - FAIL("Create conn failed (err %d)", err); + TEST_FAIL("Create conn failed (err %d)", err); return; } } @@ -133,7 +133,7 @@ static void connect(void) UNSET_FLAG(is_connected); err = bt_le_scan_start(&scan_param, device_found); - ASSERT(!err, "Scanning failed to start (err %d)\n", err); + TEST_ASSERT(!err, "Scanning failed to start (err %d)", err); LOG_DBG("Central initiating connection..."); WAIT_FOR_FLAG(is_connected); @@ -187,8 +187,8 @@ static uint8_t notified(struct bt_conn *conn, struct bt_gatt_subscribe_params *p static uint8_t indication[] = INDICATION_PAYLOAD; bool is_nfy; - ASSERT(length >= sizeof(indication), "Unexpected data"); - ASSERT(length <= sizeof(notification), "Unexpected data"); + TEST_ASSERT(length >= sizeof(indication), "Unexpected data"); + TEST_ASSERT(length <= sizeof(notification), "Unexpected data"); LOG_HEXDUMP_DBG(data, length, "HVx data"); @@ -210,9 +210,9 @@ static void subscribed(struct bt_conn *conn, uint8_t err, struct bt_gatt_subscribe_params *params) { - ASSERT(!err, "Subscribe failed (err %d)\n", err); + TEST_ASSERT(!err, "Subscribe failed (err %d)", err); - ASSERT(params, "params is NULL\n"); + TEST_ASSERT(params, "params is NULL"); SET_FLAG(is_subscribed); /* spoiler: tester doesn't really have attributes */ @@ -233,7 +233,7 @@ void subscribe(void) }; err = bt_gatt_subscribe(dconn, ¶ms); - ASSERT(!err, "Subscribe failed (err %d)\n", err); + TEST_ASSERT(!err, "Subscribe failed (err %d)", err); WAIT_FOR_FLAG(is_subscribed); } @@ -250,7 +250,7 @@ static void send_write_handle(void) sys_put_le16(handle, data); err = bt_gatt_notify(dconn, attr, data, sizeof(data)); - ASSERT(!err, "Failed to transmit handle for write (err %d)\n", err); + TEST_ASSERT(!err, "Failed to transmit handle for write (err %d)", err); } void test_procedure_0(void) @@ -259,7 +259,7 @@ void test_procedure_0(void) int err; err = bt_enable(NULL); - ASSERT(err == 0, "Can't enable Bluetooth (err %d)\n", err); + TEST_ASSERT(err == 0, "Can't enable Bluetooth (err %d)", err); LOG_DBG("Central: Bluetooth initialized."); /* Test purpose: @@ -313,29 +313,12 @@ void test_procedure_0(void) /* Send RSP to LL */ bt_conn_suspend_tx(false); - PASS("DUT done\n"); -} - -void test_tick(bs_time_t HW_device_time) -{ - bs_trace_debug_time(0, "Simulation ends now.\n"); - if (bst_result != Passed) { - bst_result = Failed; - bs_trace_error("Test did not pass before simulation ended.\n"); - } -} - -void test_init(void) -{ - bst_ticker_set_next_tick_absolute(TEST_TIMEOUT_SIMULATED); - bst_result = In_progress; + TEST_PASS("DUT done"); } static const struct bst_test_instance test_to_add[] = { { .test_id = "dut", - .test_pre_init_f = test_init, - .test_tick_f = test_tick, .test_main_f = test_procedure_0, }, BSTEST_END_MARKER, diff --git a/tests/bsim/bluetooth/host/att/sequential/tester/CMakeLists.txt b/tests/bsim/bluetooth/host/att/sequential/tester/CMakeLists.txt index c4410ee0b9e7..8227245fdd9b 100644 --- a/tests/bsim/bluetooth/host/att/sequential/tester/CMakeLists.txt +++ b/tests/bsim/bluetooth/host/att/sequential/tester/CMakeLists.txt @@ -5,6 +5,9 @@ cmake_minimum_required(VERSION 3.20.0) find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) project(bsim_test_sequential_tester) +add_subdirectory(${ZEPHYR_BASE}/tests/bsim/babblekit babblekit) +target_link_libraries(app PRIVATE babblekit) + target_sources(app PRIVATE src/main.c ) diff --git a/tests/bsim/bluetooth/host/att/sequential/tester/src/main.c b/tests/bsim/bluetooth/host/att/sequential/tester/src/main.c index 511323dacb3d..4cd981d615b0 100644 --- a/tests/bsim/bluetooth/host/att/sequential/tester/src/main.c +++ b/tests/bsim/bluetooth/host/att/sequential/tester/src/main.c @@ -21,8 +21,10 @@ #include "host/conn_internal.h" #include "host/l2cap_internal.h" -#include "utils.h" -#include "bstests.h" +#include "babblekit/testcase.h" +#include "babblekit/flags.h" + +#include "common_defs.h" #include LOG_MODULE_REGISTER(bt_tinyhost, LOG_LEVEL_INF); @@ -37,12 +39,11 @@ LOG_MODULE_REGISTER(bt_tinyhost, LOG_LEVEL_INF); #define BT_ATT_OP_WRITE_CMD 0x52 #define BT_L2CAP_CID_ATT 0x0004 -DEFINE_FLAG(is_connected); -DEFINE_FLAG(flag_data_length_updated); -DEFINE_FLAG(flag_handle); -DEFINE_FLAG(flag_notified); -DEFINE_FLAG(flag_write_ack); -DEFINE_FLAG(flag_indication_ack); +DEFINE_FLAG_STATIC(is_connected); +DEFINE_FLAG_STATIC(flag_data_length_updated); +DEFINE_FLAG_STATIC(flag_handle); +DEFINE_FLAG_STATIC(flag_write_ack); +DEFINE_FLAG_STATIC(flag_indication_ack); static uint16_t server_write_handle; @@ -67,7 +68,7 @@ struct net_buf *bt_hci_cmd_create(uint16_t opcode, uint8_t param_len) LOG_DBG("opcode 0x%04x param_len %u", opcode, param_len); buf = net_buf_alloc(&hci_cmd_pool, K_FOREVER); - ASSERT(buf, "failed allocation"); + TEST_ASSERT(buf, "failed allocation"); LOG_DBG("buf %p", buf); @@ -110,14 +111,14 @@ static void handle_cmd_complete(struct net_buf *buf) opcode = sys_le16_to_cpu(evt->opcode); } else { - FAIL("unhandled event 0x%x", hdr->evt); + TEST_FAIL("unhandled event 0x%x", hdr->evt); } LOG_DBG("opcode 0x%04x status %x", opcode, status); - ASSERT(status == 0x00, "cmd status: %x", status); + TEST_ASSERT(status == 0x00, "cmd status: %x", status); - ASSERT(active_opcode == opcode, "unexpected opcode %x != %x", active_opcode, opcode); + TEST_ASSERT(active_opcode == opcode, "unexpected opcode %x != %x", active_opcode, opcode); if (active_opcode) { active_opcode = 0xFFFF; @@ -229,7 +230,7 @@ static void handle_att(struct net_buf *buf) return; default: LOG_HEXDUMP_ERR(buf->data, buf->len, "payload"); - FAIL("unhandled opcode %x\n", op); + TEST_FAIL("unhandled opcode %x", op); return; } } @@ -246,10 +247,10 @@ static void handle_l2cap(struct net_buf *buf) LOG_HEXDUMP_DBG(buf->data, buf->len, "l2cap"); /* Make sure we don't have to recombine packets */ - ASSERT(buf->len == hdr->len, "buflen = %d != hdrlen %d", + TEST_ASSERT(buf->len == hdr->len, "buflen = %d != hdrlen %d", buf->len, hdr->len); - ASSERT(cid == BT_L2CAP_CID_ATT, "We only support (U)ATT"); + TEST_ASSERT(cid == BT_L2CAP_CID_ATT, "We only support (U)ATT"); /* (U)ATT PDU */ handle_att(buf); @@ -268,7 +269,7 @@ static void handle_acl(struct net_buf *buf) flags = bt_acl_flags(handle); handle = bt_acl_handle(handle); - ASSERT(flags == BT_ACL_START, + TEST_ASSERT(flags == BT_ACL_START, "Fragmentation not supported"); LOG_DBG("ACL: conn %d len %d flags %d", handle, len, flags); @@ -329,7 +330,7 @@ static void send_cmd(uint16_t opcode, struct net_buf *cmd, struct net_buf **rsp) } k_sem_take(&cmd_sem, K_FOREVER); - ASSERT(active_opcode == 0xFFFF, ""); + TEST_ASSERT_NO_MSG(active_opcode == 0xFFFF); active_opcode = opcode; @@ -398,7 +399,7 @@ static void write_default_data_len(uint16_t tx_octets, uint16_t tx_time) struct bt_hci_cp_le_write_default_data_len *cp; struct net_buf *buf = bt_hci_cmd_create(BT_HCI_OP_LE_WRITE_DEFAULT_DATA_LEN, sizeof(*cp)); - ASSERT(buf, ""); + TEST_ASSERT_NO_MSG(buf); cp = net_buf_add(buf, sizeof(*cp)); cp->max_tx_octets = sys_cpu_to_le16(tx_octets); @@ -423,7 +424,7 @@ static void set_event_mask(uint16_t opcode) /* The two commands have the same length/params */ buf = bt_hci_cmd_create(opcode, sizeof(*cp_mask)); - ASSERT(buf, ""); + TEST_ASSERT_NO_MSG(buf); /* Forward all events */ cp_mask = net_buf_add(buf, sizeof(*cp_mask)); @@ -441,7 +442,7 @@ static void set_random_address(void) LOG_DBG("%s", bt_addr_str(&addr.a)); buf = bt_hci_cmd_create(BT_HCI_OP_LE_SET_RANDOM_ADDRESS, sizeof(addr.a)); - ASSERT(buf, ""); + TEST_ASSERT_NO_MSG(buf); net_buf_add_mem(buf, &addr.a, sizeof(addr.a)); send_cmd(BT_HCI_OP_LE_SET_RANDOM_ADDRESS, buf, NULL); @@ -481,7 +482,7 @@ struct net_buf *alloc_l2cap_pdu(void) uint16_t reserve; buf = net_buf_alloc(&acl_tx_pool, K_FOREVER); - ASSERT(buf, "failed ACL allocation"); + TEST_ASSERT(buf, "failed ACL allocation"); reserve = sizeof(struct bt_l2cap_hdr); reserve += sizeof(struct bt_hci_acl_hdr) + BT_BUF_RESERVE; @@ -516,7 +517,7 @@ static void send_l2cap_packet(struct net_buf *buf, uint16_t cid) hdr->cid = sys_cpu_to_le16(cid); /* Always entire packets, no HCI fragmentation */ - ASSERT(buf->len <= CONFIG_BT_BUF_ACL_TX_SIZE, + TEST_ASSERT(buf->len <= CONFIG_BT_BUF_ACL_TX_SIZE, "Fragmentation not supported"); send_acl(buf); @@ -625,29 +626,12 @@ void test_procedure_0(void) WAIT_FOR_FLAG(flag_write_ack); WAIT_FOR_FLAG(flag_indication_ack); - PASS("Tester done\n"); -} - -void test_tick(bs_time_t HW_device_time) -{ - bs_trace_debug_time(0, "Simulation ends now.\n"); - if (bst_result != Passed) { - bst_result = Failed; - bs_trace_error("Test did not pass before simulation ended.\n"); - } -} - -void test_init(void) -{ - bst_ticker_set_next_tick_absolute(TEST_TIMEOUT_SIMULATED); - bst_result = In_progress; + TEST_PASS("Tester done"); } static const struct bst_test_instance test_to_add[] = { { .test_id = "tester", - .test_pre_init_f = test_init, - .test_tick_f = test_tick, .test_main_f = test_procedure_0, }, BSTEST_END_MARKER, diff --git a/tests/bsim/bluetooth/host/central/src/dummy_peripheral.c b/tests/bsim/bluetooth/host/central/src/dummy_peripheral.c index 6f6cb7ae5432..6f7e5a1cc126 100644 --- a/tests/bsim/bluetooth/host/central/src/dummy_peripheral.c +++ b/tests/bsim/bluetooth/host/central/src/dummy_peripheral.c @@ -54,7 +54,6 @@ static const struct bst_test_instance test_def[] = { { .test_id = "peripheral_dummy", .test_descr = "Connectable peripheral", - .test_tick_f = bst_tick, .test_main_f = test_peripheral_dummy, }, BSTEST_END_MARKER, diff --git a/tests/bsim/bluetooth/host/central/src/main.c b/tests/bsim/bluetooth/host/central/src/main.c index 2068ecefc8d8..2ee7b39d3fda 100644 --- a/tests/bsim/bluetooth/host/central/src/main.c +++ b/tests/bsim/bluetooth/host/central/src/main.c @@ -195,14 +195,12 @@ static const struct bst_test_instance test_def[] = { { .test_id = "central_connect_timeout", .test_descr = "Verifies that the default connection timeout is used correctly", - .test_tick_f = bst_tick, .test_main_f = test_central_connect_timeout, }, { .test_id = "central_connect_when_connecting", .test_descr = "Verifies that the stack returns an error code when trying to connect" " while already connecting", - .test_tick_f = bst_tick, .test_main_f = test_central_connect_when_connecting, }, { @@ -210,7 +208,6 @@ static const struct bst_test_instance test_def[] = { .test_descr = "Verifies that the stack returns an error code when trying to connect" " to an existing device and does not unref the existing connection object.", - .test_tick_f = bst_tick, .test_main_f = test_central_connect_to_existing, }, BSTEST_END_MARKER, diff --git a/tests/bsim/bluetooth/host/gatt/authorization/src/gatt_client_test.c b/tests/bsim/bluetooth/host/gatt/authorization/src/gatt_client_test.c index 3e162d50297f..7505230ed8a4 100644 --- a/tests/bsim/bluetooth/host/gatt/authorization/src/gatt_client_test.c +++ b/tests/bsim/bluetooth/host/gatt/authorization/src/gatt_client_test.c @@ -19,10 +19,10 @@ #include "babblekit/flags.h" #include "common.h" -static DEFINE_FLAG(flag_is_connected); -static DEFINE_FLAG(flag_discover_complete); -static DEFINE_FLAG(flag_write_complete); -static DEFINE_FLAG(flag_read_complete); +DEFINE_FLAG_STATIC(flag_is_connected); +DEFINE_FLAG_STATIC(flag_discover_complete); +DEFINE_FLAG_STATIC(flag_write_complete); +DEFINE_FLAG_STATIC(flag_read_complete); static struct bt_conn *g_conn; static uint16_t unhandled_chrc_handle; diff --git a/tests/bsim/bluetooth/host/gatt/authorization/src/gatt_server_test.c b/tests/bsim/bluetooth/host/gatt/authorization/src/gatt_server_test.c index 4c206f7cd27d..88962b0d1693 100644 --- a/tests/bsim/bluetooth/host/gatt/authorization/src/gatt_server_test.c +++ b/tests/bsim/bluetooth/host/gatt/authorization/src/gatt_server_test.c @@ -21,7 +21,7 @@ extern enum bst_result_t bst_result; -static DEFINE_FLAG(flag_is_chrc_ctx_validated); +DEFINE_FLAG_STATIC(flag_is_chrc_ctx_validated); static struct bt_conn *g_conn; diff --git a/tests/bsim/bluetooth/host/gatt/caching/src/gatt_client_test.c b/tests/bsim/bluetooth/host/gatt/caching/src/gatt_client_test.c index 65d9b0213c02..1cc55dadf9ae 100644 --- a/tests/bsim/bluetooth/host/gatt/caching/src/gatt_client_test.c +++ b/tests/bsim/bluetooth/host/gatt/caching/src/gatt_client_test.c @@ -20,13 +20,13 @@ #include "babblekit/sync.h" #include "common.h" -static DEFINE_FLAG(flag_is_connected); -static DEFINE_FLAG(flag_discover_complete); -static DEFINE_FLAG(flag_write_complete); -static DEFINE_FLAG(flag_chan_1_read); -static DEFINE_FLAG(flag_chan_2_read); -static DEFINE_FLAG(flag_db_hash_read); -static DEFINE_FLAG(flag_encrypted); +DEFINE_FLAG_STATIC(flag_is_connected); +DEFINE_FLAG_STATIC(flag_discover_complete); +DEFINE_FLAG_STATIC(flag_write_complete); +DEFINE_FLAG_STATIC(flag_chan_1_read); +DEFINE_FLAG_STATIC(flag_chan_2_read); +DEFINE_FLAG_STATIC(flag_db_hash_read); +DEFINE_FLAG_STATIC(flag_encrypted); static struct bt_conn *g_conn; static uint16_t chrc_handle; diff --git a/tests/bsim/bluetooth/host/gatt/caching/src/gatt_server_test.c b/tests/bsim/bluetooth/host/gatt/caching/src/gatt_server_test.c index 1cb88b489fb8..de58beff8b97 100644 --- a/tests/bsim/bluetooth/host/gatt/caching/src/gatt_server_test.c +++ b/tests/bsim/bluetooth/host/gatt/caching/src/gatt_server_test.c @@ -22,8 +22,8 @@ extern enum bst_result_t bst_result; -static DEFINE_FLAG(flag_is_connected); -static DEFINE_FLAG(flag_is_encrypted); +DEFINE_FLAG_STATIC(flag_is_connected); +DEFINE_FLAG_STATIC(flag_is_encrypted); static struct bt_conn *g_conn; diff --git a/tests/bsim/bluetooth/host/gatt/ccc_store/src/central.c b/tests/bsim/bluetooth/host/gatt/ccc_store/src/central.c index a4f056dd4083..664a65ca30db 100644 --- a/tests/bsim/bluetooth/host/gatt/ccc_store/src/central.c +++ b/tests/bsim/bluetooth/host/gatt/ccc_store/src/central.c @@ -29,10 +29,10 @@ #define SERVER_CHAN 0 -static DEFINE_FLAG(connected_flag); -static DEFINE_FLAG(disconnected_flag); -static DEFINE_FLAG(security_updated_flag); -static DEFINE_FLAG(notification_received_flag); +DEFINE_FLAG_STATIC(connected_flag); +DEFINE_FLAG_STATIC(disconnected_flag); +DEFINE_FLAG_STATIC(security_updated_flag); +DEFINE_FLAG_STATIC(notification_received_flag); #define BT_UUID_DUMMY_SERVICE BT_UUID_DECLARE_128(DUMMY_SERVICE_TYPE) #define BT_UUID_DUMMY_SERVICE_NOTIFY BT_UUID_DECLARE_128(DUMMY_SERVICE_NOTIFY_TYPE) @@ -41,7 +41,7 @@ static struct bt_conn *default_conn; static struct bt_conn_cb central_cb; -static DEFINE_FLAG(gatt_subscribed_flag); +DEFINE_FLAG_STATIC(gatt_subscribed_flag); static uint8_t notify_cb(struct bt_conn *conn, struct bt_gatt_subscribe_params *params, const void *data, uint16_t length) diff --git a/tests/bsim/bluetooth/host/gatt/ccc_store/src/peripheral.c b/tests/bsim/bluetooth/host/gatt/ccc_store/src/peripheral.c index 6205306306b6..0df6d2b7f9f5 100644 --- a/tests/bsim/bluetooth/host/gatt/ccc_store/src/peripheral.c +++ b/tests/bsim/bluetooth/host/gatt/ccc_store/src/peripheral.c @@ -26,11 +26,11 @@ #define CLIENT_CHAN 0 -static DEFINE_FLAG(connected_flag); -static DEFINE_FLAG(disconnected_flag); -static DEFINE_FLAG(security_updated_flag); +DEFINE_FLAG_STATIC(connected_flag); +DEFINE_FLAG_STATIC(disconnected_flag); +DEFINE_FLAG_STATIC(security_updated_flag); -static DEFINE_FLAG(ccc_cfg_changed_flag); +DEFINE_FLAG_STATIC(ccc_cfg_changed_flag); static const struct bt_uuid_128 dummy_service = BT_UUID_INIT_128(DUMMY_SERVICE_TYPE); diff --git a/tests/bsim/bluetooth/host/gatt/device_name/src/client.c b/tests/bsim/bluetooth/host/gatt/device_name/src/client.c index b564692b8ce0..2225b23d9e6f 100644 --- a/tests/bsim/bluetooth/host/gatt/device_name/src/client.c +++ b/tests/bsim/bluetooth/host/gatt/device_name/src/client.c @@ -29,7 +29,7 @@ LOG_MODULE_REGISTER(client, LOG_LEVEL_DBG); -static DEFINE_FLAG(client_security_changed_flag); +DEFINE_FLAG_STATIC(client_security_changed_flag); static struct bt_conn_cb client_conn_cb; diff --git a/tests/bsim/bluetooth/host/gatt/device_name/src/server.c b/tests/bsim/bluetooth/host/gatt/device_name/src/server.c index 3d3d7edeeac0..feb8ce34fe89 100644 --- a/tests/bsim/bluetooth/host/gatt/device_name/src/server.c +++ b/tests/bsim/bluetooth/host/gatt/device_name/src/server.c @@ -31,7 +31,7 @@ LOG_MODULE_REGISTER(server, LOG_LEVEL_DBG); -static DEFINE_FLAG(security_changed_flag); +DEFINE_FLAG_STATIC(security_changed_flag); static struct bt_conn_cb server_conn_cb; diff --git a/tests/bsim/bluetooth/host/gatt/general/src/gatt_client_test.c b/tests/bsim/bluetooth/host/gatt/general/src/gatt_client_test.c index b78b5d2a619a..a754f08649fc 100644 --- a/tests/bsim/bluetooth/host/gatt/general/src/gatt_client_test.c +++ b/tests/bsim/bluetooth/host/gatt/general/src/gatt_client_test.c @@ -20,11 +20,11 @@ #include "babblekit/flags.h" #include "common.h" -static DEFINE_FLAG(flag_is_connected); -static DEFINE_FLAG(flag_discover_complete); -static DEFINE_FLAG(flag_security_changed); -static DEFINE_FLAG(flag_write_complete); -static DEFINE_FLAG(flag_read_complete); +DEFINE_FLAG_STATIC(flag_is_connected); +DEFINE_FLAG_STATIC(flag_discover_complete); +DEFINE_FLAG_STATIC(flag_security_changed); +DEFINE_FLAG_STATIC(flag_write_complete); +DEFINE_FLAG_STATIC(flag_read_complete); static struct bt_conn *g_conn; static uint16_t chrc_handle; diff --git a/tests/bsim/bluetooth/host/gatt/general/src/gatt_server_test.c b/tests/bsim/bluetooth/host/gatt/general/src/gatt_server_test.c index 6f3225c4d293..274773fe7762 100644 --- a/tests/bsim/bluetooth/host/gatt/general/src/gatt_server_test.c +++ b/tests/bsim/bluetooth/host/gatt/general/src/gatt_server_test.c @@ -22,7 +22,7 @@ extern enum bst_result_t bst_result; -static DEFINE_FLAG(flag_is_connected); +DEFINE_FLAG_STATIC(flag_is_connected); static struct bt_conn *g_conn; diff --git a/tests/bsim/bluetooth/host/gatt/notify/src/gatt_client_test.c b/tests/bsim/bluetooth/host/gatt/notify/src/gatt_client_test.c index 273ddf51aa71..7baecfe4fb1c 100644 --- a/tests/bsim/bluetooth/host/gatt/notify/src/gatt_client_test.c +++ b/tests/bsim/bluetooth/host/gatt/notify/src/gatt_client_test.c @@ -19,11 +19,11 @@ #include "babblekit/flags.h" #include "common.h" -static DEFINE_FLAG(flag_is_connected); -static DEFINE_FLAG(flag_is_encrypted); -static DEFINE_FLAG(flag_discover_complete); -static DEFINE_FLAG(flag_short_subscribed); -static DEFINE_FLAG(flag_long_subscribed); +DEFINE_FLAG_STATIC(flag_is_connected); +DEFINE_FLAG_STATIC(flag_is_encrypted); +DEFINE_FLAG_STATIC(flag_discover_complete); +DEFINE_FLAG_STATIC(flag_short_subscribed); +DEFINE_FLAG_STATIC(flag_long_subscribed); static struct bt_conn *g_conn; static uint16_t chrc_handle; diff --git a/tests/bsim/bluetooth/host/gatt/notify/src/gatt_server_test.c b/tests/bsim/bluetooth/host/gatt/notify/src/gatt_server_test.c index d293ad9ac19d..e5702d245233 100644 --- a/tests/bsim/bluetooth/host/gatt/notify/src/gatt_server_test.c +++ b/tests/bsim/bluetooth/host/gatt/notify/src/gatt_server_test.c @@ -20,9 +20,9 @@ extern enum bst_result_t bst_result; -static DEFINE_FLAG(flag_is_connected); -static DEFINE_FLAG(flag_short_subscribe); -static DEFINE_FLAG(flag_long_subscribe); +DEFINE_FLAG_STATIC(flag_is_connected); +DEFINE_FLAG_STATIC(flag_short_subscribe); +DEFINE_FLAG_STATIC(flag_long_subscribe); static struct bt_conn *g_conn; diff --git a/tests/bsim/bluetooth/host/gatt/notify_multiple/CMakeLists.txt b/tests/bsim/bluetooth/host/gatt/notify_multiple/CMakeLists.txt index 7869a21840fe..b279e8c14396 100644 --- a/tests/bsim/bluetooth/host/gatt/notify_multiple/CMakeLists.txt +++ b/tests/bsim/bluetooth/host/gatt/notify_multiple/CMakeLists.txt @@ -5,6 +5,9 @@ cmake_minimum_required(VERSION 3.20.0) find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) project(bsim_test_gatt) +add_subdirectory(${ZEPHYR_BASE}/tests/bsim/babblekit babblekit) +target_link_libraries(app PRIVATE babblekit) + FILE(GLOB app_sources src/*.c) target_sources(app PRIVATE ${app_sources} ) diff --git a/tests/bsim/bluetooth/host/gatt/notify_multiple/src/common.c b/tests/bsim/bluetooth/host/gatt/notify_multiple/src/common.c deleted file mode 100644 index df438607c5fa..000000000000 --- a/tests/bsim/bluetooth/host/gatt/notify_multiple/src/common.c +++ /dev/null @@ -1,20 +0,0 @@ -/* - * Copyright (c) 2022 Nordic Semiconductor ASA - * - * SPDX-License-Identifier: Apache-2.0 - */ - -#include "common.h" - -void test_tick(bs_time_t HW_device_time) -{ - if (bst_result != Passed) { - FAIL("test failed (not passed after %i seconds)\n", WAIT_TIME); - } -} - -void test_init(void) -{ - bst_ticker_set_next_tick_absolute(WAIT_TIME); - bst_result = In_progress; -} diff --git a/tests/bsim/bluetooth/host/gatt/notify_multiple/src/common.h b/tests/bsim/bluetooth/host/gatt/notify_multiple/src/common.h index 56ca219497b5..ea8d590b182a 100644 --- a/tests/bsim/bluetooth/host/gatt/notify_multiple/src/common.h +++ b/tests/bsim/bluetooth/host/gatt/notify_multiple/src/common.h @@ -7,50 +7,9 @@ */ #include - -#include "bs_types.h" -#include "bs_tracing.h" -#include "time_machine.h" -#include "bstests.h" - -#include -#include -#include - -#include -#include -#include #include -#include - -extern enum bst_result_t bst_result; -#define WAIT_TIME (60 * 1e6) /*seconds*/ - -#define CREATE_FLAG(flag) static atomic_t flag = (atomic_t)false #define FORCE_FLAG(flag, val) (void)atomic_set(&flag, (atomic_t)val) -#define SET_FLAG(flag) (void)atomic_set(&flag, (atomic_t)true) -#define UNSET_FLAG(flag) (void)atomic_set(&flag, (atomic_t)false) -#define WAIT_FOR_FLAG(flag) \ - while (!(bool)atomic_get(&flag)) { \ - (void)k_sleep(K_MSEC(1)); \ - } -#define WAIT_FOR_FLAG_UNSET(flag) \ - while ((bool)atomic_get(&flag)) { \ - (void)k_sleep(K_MSEC(1)); \ - } - -#define FAIL(...) \ - do { \ - bst_result = Failed; \ - bs_trace_error_time_line(__VA_ARGS__); \ - } while (0) - -#define PASS(...) \ - do { \ - bst_result = Passed; \ - bs_trace_info_time(1, __VA_ARGS__); \ - } while (0) #define CHRC_SIZE 10 #define LONG_CHRC_SIZE 40 @@ -67,8 +26,5 @@ extern enum bst_result_t bst_result; BT_UUID_DECLARE_128(0x01, 0x23, 0x45, 0x67, 0x89, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, \ 0x07, 0x08, 0x09, 0xFF, 0x11) -void test_tick(bs_time_t HW_device_time); -void test_init(void); - #define NOTIFICATION_COUNT 10 BUILD_ASSERT(NOTIFICATION_COUNT % 2 == 0); diff --git a/tests/bsim/bluetooth/host/gatt/notify_multiple/src/gatt_client_test.c b/tests/bsim/bluetooth/host/gatt/notify_multiple/src/gatt_client_test.c index d46af04f9ec9..c295076afeb1 100644 --- a/tests/bsim/bluetooth/host/gatt/notify_multiple/src/gatt_client_test.c +++ b/tests/bsim/bluetooth/host/gatt/notify_multiple/src/gatt_client_test.c @@ -4,17 +4,28 @@ * SPDX-License-Identifier: Apache-2.0 */ +#include + +#include +#include +#include + #include +#include +#include #include +#include "babblekit/testcase.h" +#include "babblekit/flags.h" + #include "common.h" -CREATE_FLAG(flag_is_connected); -CREATE_FLAG(flag_is_encrypted); -CREATE_FLAG(flag_discover_complete); -CREATE_FLAG(flag_write_complete); -CREATE_FLAG(flag_subscribed_short); -CREATE_FLAG(flag_subscribed_long); +DEFINE_FLAG_STATIC(flag_is_connected); +DEFINE_FLAG_STATIC(flag_is_encrypted); +DEFINE_FLAG_STATIC(flag_discover_complete); +DEFINE_FLAG_STATIC(flag_write_complete); +DEFINE_FLAG_STATIC(flag_subscribed_short); +DEFINE_FLAG_STATIC(flag_subscribed_long); static struct bt_conn *g_conn; static uint16_t chrc_handle; @@ -39,7 +50,7 @@ static void connected(struct bt_conn *conn, uint8_t err) bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr)); if (err != 0) { - FAIL("Failed to connect to %s (%u)\n", addr, err); + TEST_FAIL("Failed to connect to %s (%u)", addr, err); return; } @@ -75,9 +86,9 @@ static void disconnected(struct bt_conn *conn, uint8_t reason) void security_changed(struct bt_conn *conn, bt_security_t level, enum bt_security_err err) { if (err) { - FAIL("Encryption failer (%d)\n", err); + TEST_FAIL("Encryption failed (%d)", err); } else if (level < BT_SECURITY_L2) { - FAIL("Insufficient sec level (%d)\n", level); + TEST_FAIL("Insufficient sec level (%d)", level); } else { SET_FLAG(flag_is_encrypted); } @@ -109,13 +120,13 @@ void device_found(const bt_addr_le_t *addr, int8_t rssi, uint8_t type, struct ne printk("Stopping scan\n"); err = bt_le_scan_stop(); if (err != 0) { - FAIL("Could not stop scan: %d"); + TEST_FAIL("Could not stop scan: %d"); return; } err = bt_conn_le_create(addr, BT_CONN_LE_CREATE_CONN, BT_LE_CONN_PARAM_DEFAULT, &g_conn); if (err != 0) { - FAIL("Could not connect to peer: %d", err); + TEST_FAIL("Could not connect to peer: %d", err); } } @@ -126,7 +137,7 @@ static uint8_t discover_func(struct bt_conn *conn, const struct bt_gatt_attr *at if (attr == NULL) { if (chrc_handle == 0 || long_chrc_handle == 0) { - FAIL("Did not discover chrc (%x) or long_chrc (%x)", chrc_handle, + TEST_FAIL("Did not discover chrc (%x) or long_chrc (%x)", chrc_handle, long_chrc_handle); } @@ -148,7 +159,7 @@ static uint8_t discover_func(struct bt_conn *conn, const struct bt_gatt_attr *at err = bt_gatt_discover(conn, params); if (err != 0) { - FAIL("Discover failed (err %d)\n", err); + TEST_FAIL("Discover failed (err %d)", err); } return BT_GATT_ITER_STOP; @@ -187,7 +198,7 @@ static void gatt_discover(const struct bt_uuid *uuid, uint8_t type) err = bt_gatt_discover(g_conn, &discover_params); if (err != 0) { - FAIL("Discover failed(err %d)\n", err); + TEST_FAIL("Discover failed(err %d)", err); } WAIT_FOR_FLAG(flag_discover_complete); @@ -199,11 +210,11 @@ static void test_subscribed(struct bt_conn *conn, struct bt_gatt_subscribe_params *params) { if (err) { - FAIL("Subscribe failed (err %d)\n", err); + TEST_FAIL("Subscribe failed (err %d)", err); } if (!params) { - FAIL("params NULL\n"); + TEST_FAIL("params NULL"); } if (params->value_handle == chrc_handle) { @@ -213,7 +224,7 @@ static void test_subscribed(struct bt_conn *conn, FORCE_FLAG(flag_subscribed_long, (bool)params->value); printk("Subscribed to long characteristic\n"); } else { - FAIL("Unknown handle %d\n", params->value_handle); + TEST_FAIL("Unknown handle %d", params->value_handle); } } @@ -248,7 +259,7 @@ static struct bt_gatt_subscribe_params sub_params_long = { static void write_cb(struct bt_conn *conn, uint8_t err, struct bt_gatt_write_params *params) { if (err != BT_ATT_ERR_SUCCESS) { - FAIL("Write failed: 0x%02X\n", err); + TEST_FAIL("Write failed: 0x%02X", err); } SET_FLAG(flag_write_complete); @@ -277,7 +288,7 @@ static void write_csf(void) err = bt_gatt_write(g_conn, &write_params); if (err) { - FAIL("bt_gatt_write failed (err %d)\n", err); + TEST_FAIL("bt_gatt_write failed (err %d)", err); } WAIT_FOR_FLAG(flag_write_complete); @@ -295,7 +306,7 @@ static void subscribe(struct bt_gatt_subscribe_params *params, bool subscribe) } if (err < 0) { - FAIL("Failed to %ssubscribe (err %d)\n", subscribe ? "un":"", err); + TEST_FAIL("Failed to %ssubscribe (err %d)", subscribe ? "un":"", err); } else { printk("%ssubscribe request sent\n", subscribe ? "un":""); } @@ -308,12 +319,12 @@ static void test_main(void) err = bt_enable(NULL); if (err != 0) { - FAIL("Bluetooth discover failed (err %d)\n", err); + TEST_FAIL("Bluetooth discover failed (err %d)", err); } err = bt_le_scan_start(BT_LE_SCAN_PASSIVE, device_found); if (err != 0) { - FAIL("Scanning failed to start (err %d)\n", err); + TEST_FAIL("Scanning failed to start (err %d)", err); } printk("Scanning successfully started\n"); @@ -322,7 +333,7 @@ static void test_main(void) err = bt_conn_set_security(g_conn, BT_SECURITY_L2); if (err) { - FAIL("Starting encryption procedure failed (%d)\n", err); + TEST_FAIL("Starting encryption procedure failed (%d)", err); } WAIT_FOR_FLAG(flag_is_encrypted); @@ -359,14 +370,12 @@ static void test_main(void) printk("Unsubscribed\n"); - PASS("GATT client Passed\n"); + TEST_PASS("GATT client Passed"); } static const struct bst_test_instance test_vcs[] = { { .test_id = "gatt_client", - .test_pre_init_f = test_init, - .test_tick_f = test_tick, .test_main_f = test_main, }, BSTEST_END_MARKER, diff --git a/tests/bsim/bluetooth/host/gatt/notify_multiple/src/gatt_server_test.c b/tests/bsim/bluetooth/host/gatt/notify_multiple/src/gatt_server_test.c index 1a9a95262d63..7dbdfdf4cc51 100644 --- a/tests/bsim/bluetooth/host/gatt/notify_multiple/src/gatt_server_test.c +++ b/tests/bsim/bluetooth/host/gatt/notify_multiple/src/gatt_server_test.c @@ -4,13 +4,25 @@ * SPDX-License-Identifier: Apache-2.0 */ -#include "common.h" +#include + +#include +#include +#include + +#include +#include +#include +#include -extern enum bst_result_t bst_result; +#include "babblekit/testcase.h" +#include "babblekit/flags.h" + +#include "common.h" -CREATE_FLAG(flag_is_connected); -CREATE_FLAG(flag_short_subscribe); -CREATE_FLAG(flag_long_subscribe); +DEFINE_FLAG_STATIC(flag_is_connected); +DEFINE_FLAG_STATIC(flag_short_subscribe); +DEFINE_FLAG_STATIC(flag_long_subscribe); static struct bt_conn *g_conn; @@ -25,7 +37,7 @@ static void connected(struct bt_conn *conn, uint8_t err) bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr)); if (err != 0) { - FAIL("Failed to connect to %s (%u)\n", addr, err); + TEST_FAIL("Failed to connect to %s (%u)", addr, err); return; } @@ -124,7 +136,7 @@ static inline void multiple_notify(const struct bt_gatt_attr *attrs[2]) if (err == -ENOMEM) { k_sleep(K_MSEC(10)); } else if (err) { - FAIL("multiple notify failed (err %d)\n", err); + TEST_FAIL("multiple notify failed (err %d)", err); } } while (err); } @@ -139,7 +151,7 @@ static void test_main(void) err = bt_enable(NULL); if (err != 0) { - FAIL("Bluetooth init failed (err %d)\n", err); + TEST_FAIL("Bluetooth init failed (err %d)", err); return; } @@ -147,7 +159,7 @@ static void test_main(void) err = bt_le_adv_start(BT_LE_ADV_CONN_FAST_1, ad, ARRAY_SIZE(ad), NULL, 0); if (err != 0) { - FAIL("Advertising failed to start (err %d)\n", err); + TEST_FAIL("Advertising failed to start (err %d)", err); return; } @@ -173,17 +185,15 @@ static void test_main(void) k_sleep(K_MSEC(1000)); if (num_notifications_sent != NOTIFICATION_COUNT) { - FAIL("Unexpected notification callback value\n"); + TEST_FAIL("Unexpected notification callback value"); } - PASS("GATT server passed\n"); + TEST_PASS("GATT server passed"); } static const struct bst_test_instance test_gatt_server[] = { { .test_id = "gatt_server", - .test_pre_init_f = test_init, - .test_tick_f = test_tick, .test_main_f = test_main, }, BSTEST_END_MARKER, diff --git a/tests/bsim/bluetooth/host/gatt/sc_indicate/CMakeLists.txt b/tests/bsim/bluetooth/host/gatt/sc_indicate/CMakeLists.txt index d734b3c054e6..7a5f7ed51a4f 100644 --- a/tests/bsim/bluetooth/host/gatt/sc_indicate/CMakeLists.txt +++ b/tests/bsim/bluetooth/host/gatt/sc_indicate/CMakeLists.txt @@ -12,6 +12,9 @@ endif() find_package(Zephyr HINTS $ENV{ZEPHYR_BASE}) project(bsim_test_auto_seq_req) +add_subdirectory(${ZEPHYR_BASE}/tests/bsim/babblekit babblekit) +target_link_libraries(app PRIVATE babblekit) + target_sources(app PRIVATE src/main.c src/central.c diff --git a/tests/bsim/bluetooth/host/gatt/sc_indicate/src/bs_bt_utils.c b/tests/bsim/bluetooth/host/gatt/sc_indicate/src/bs_bt_utils.c index 91391b404c7a..df054cc33216 100644 --- a/tests/bsim/bluetooth/host/gatt/sc_indicate/src/bs_bt_utils.c +++ b/tests/bsim/bluetooth/host/gatt/sc_indicate/src/bs_bt_utils.c @@ -52,13 +52,13 @@ void clear_g_conn(void) conn = g_conn; g_conn = NULL; - BSIM_ASSERT(conn, "Test error: no g_conn!\n"); + TEST_ASSERT(conn, "Test error: no g_conn!"); bt_conn_unref(conn); } static void connected(struct bt_conn *conn, uint8_t err) { - BSIM_ASSERT((!g_conn || (conn == g_conn)), "Unexpected new connection."); + TEST_ASSERT((!g_conn || (conn == g_conn)), "Unexpected new connection."); if (!g_conn) { g_conn = bt_conn_ref(conn); @@ -94,10 +94,10 @@ static void stop_scan_and_connect(const bt_addr_le_t *addr, printk("Got scan result, connecting.. dst %s, RSSI %d\n", addr_str, rssi); err = bt_le_scan_stop(); - BSIM_ASSERT(!err, "Err bt_le_scan_stop %d", err); + TEST_ASSERT(!err, "Err bt_le_scan_stop %d", err); err = bt_conn_le_create(addr, BT_CONN_LE_CREATE_CONN, BT_LE_CONN_PARAM_DEFAULT, &g_conn); - BSIM_ASSERT(!err, "Err bt_conn_le_create %d", err); + TEST_ASSERT(!err, "Err bt_conn_le_create %d", err); } void scan_connect_to_first_result(void) @@ -105,7 +105,7 @@ void scan_connect_to_first_result(void) int err; err = bt_le_scan_start(BT_LE_SCAN_PASSIVE, stop_scan_and_connect); - BSIM_ASSERT(!err, "Err bt_le_scan_start %d", err); + TEST_ASSERT(!err, "Err bt_le_scan_start %d", err); } void disconnect(void) @@ -113,7 +113,7 @@ void disconnect(void) int err; err = bt_conn_disconnect(g_conn, BT_HCI_ERR_REMOTE_USER_TERM_CONN); - BSIM_ASSERT(!err, "bt_conn_disconnect failed (%d)\n", err); + TEST_ASSERT(!err, "bt_conn_disconnect failed (%d)", err); } void set_security(bt_security_t sec) @@ -121,7 +121,7 @@ void set_security(bt_security_t sec) int err; err = bt_conn_set_security(g_conn, sec); - BSIM_ASSERT(!err, "Err bt_conn_set_security %d", err); + TEST_ASSERT(!err, "Err bt_conn_set_security %d", err); } void create_adv(struct bt_le_ext_adv **adv) @@ -138,7 +138,7 @@ void create_adv(struct bt_le_ext_adv **adv) params.interval_max = BT_GAP_ADV_FAST_INT_MAX_2; err = bt_le_ext_adv_create(¶ms, NULL, adv); - BSIM_ASSERT(!err, "bt_le_ext_adv_create failed (%d)\n", err); + TEST_ASSERT(!err, "bt_le_ext_adv_create failed (%d)", err); } void start_adv(struct bt_le_ext_adv *adv) @@ -146,7 +146,7 @@ void start_adv(struct bt_le_ext_adv *adv) int err; err = bt_le_ext_adv_start(adv, BT_LE_EXT_ADV_START_DEFAULT); - BSIM_ASSERT(!err, "bt_le_ext_adv_start failed (%d)\n", err); + TEST_ASSERT(!err, "bt_le_ext_adv_start failed (%d)", err); } void stop_adv(struct bt_le_ext_adv *adv) @@ -154,7 +154,7 @@ void stop_adv(struct bt_le_ext_adv *adv) int err; err = bt_le_ext_adv_stop(adv); - BSIM_ASSERT(!err, "bt_le_ext_adv_stop failed (%d)\n", err); + TEST_ASSERT(!err, "bt_le_ext_adv_stop failed (%d)", err); } /* The following flags are raised by events and lowered by test code. */ @@ -178,5 +178,5 @@ void pairing_complete(struct bt_conn *conn, bool bonded) void pairing_failed(struct bt_conn *conn, enum bt_security_err err) { - FAIL("Pairing failed\n"); + TEST_FAIL("Pairing failed"); } diff --git a/tests/bsim/bluetooth/host/gatt/sc_indicate/src/bs_bt_utils.h b/tests/bsim/bluetooth/host/gatt/sc_indicate/src/bs_bt_utils.h index 5ec34005e9f7..9f86eb389297 100644 --- a/tests/bsim/bluetooth/host/gatt/sc_indicate/src/bs_bt_utils.h +++ b/tests/bsim/bluetooth/host/gatt/sc_indicate/src/bs_bt_utils.h @@ -10,44 +10,8 @@ #include #include -extern enum bst_result_t bst_result; - -#define DECLARE_FLAG(flag) extern atomic_t flag -#define DEFINE_FLAG(flag) atomic_t flag = (atomic_t) false -#define SET_FLAG(flag) (void)atomic_set(&flag, (atomic_t) true) -#define UNSET_FLAG(flag) (void)atomic_set(&flag, (atomic_t) false) -#define WAIT_FOR_FLAG(flag) \ - while (!(bool)atomic_get(&flag)) { \ - (void)k_sleep(K_MSEC(1)); \ - } -#define WAIT_FOR_FLAG_UNSET(flag) \ - while ((bool)atomic_get(&flag)) { \ - (void)k_sleep(K_MSEC(1)); \ - } -#define TAKE_FLAG(flag) \ - while (!(bool)atomic_cas(&flag, true, false)) { \ - (void)k_sleep(K_MSEC(1)); \ - } -#define GET_FLAG(flag) (bool)atomic_get(&flag) - -#define BSIM_ASSERT(expr, ...) \ - do { \ - if (!(expr)) { \ - FAIL(__VA_ARGS__); \ - } \ - } while (0) - -#define FAIL(...) \ - do { \ - bst_result = Failed; \ - bs_trace_error_time_line(__VA_ARGS__); \ - } while (0) - -#define PASS(...) \ - do { \ - bst_result = Passed; \ - bs_trace_info_time(1, __VA_ARGS__); \ - } while (0) +#include "babblekit/testcase.h" +#include "babblekit/flags.h" DECLARE_FLAG(flag_pairing_complete); DECLARE_FLAG(flag_bonded); diff --git a/tests/bsim/bluetooth/host/gatt/sc_indicate/src/central.c b/tests/bsim/bluetooth/host/gatt/sc_indicate/src/central.c index ca45874e9cbc..f9bf40965984 100644 --- a/tests/bsim/bluetooth/host/gatt/sc_indicate/src/central.c +++ b/tests/bsim/bluetooth/host/gatt/sc_indicate/src/central.c @@ -16,14 +16,17 @@ #include #include +#include "babblekit/testcase.h" +#include "babblekit/flags.h" + #include LOG_MODULE_REGISTER(test_central, LOG_LEVEL_DBG); #include "bs_bt_utils.h" -DEFINE_FLAG(flag_discovered); -DEFINE_FLAG(flag_subscribed); -DEFINE_FLAG(flag_indicated); +DEFINE_FLAG_STATIC(flag_discovered); +DEFINE_FLAG_STATIC(flag_subscribed); +DEFINE_FLAG_STATIC(flag_indicated); enum GATT_HANDLES { SC, @@ -66,7 +69,7 @@ static void subscribe(void) subscribe_params.notify = sc_indicated; err = bt_gatt_subscribe(get_g_conn(), &subscribe_params); - BSIM_ASSERT(!err, "bt_gatt_subscribe failed (%d)\n", err); + TEST_ASSERT(!err, "bt_gatt_subscribe failed (%d)", err); WAIT_FOR_FLAG(flag_subscribed); } @@ -78,7 +81,7 @@ static uint8_t discover_func(struct bt_conn *conn, if (attr == NULL) { for (size_t i = 0U; i < ARRAY_SIZE(gatt_handles); i++) { LOG_DBG("handle[%d] = 0x%x", i, gatt_handles[i]); - BSIM_ASSERT(gatt_handles[i] != 0, "did not find all handles\n"); + TEST_ASSERT(gatt_handles[i] != 0, "did not find all handles"); } (void)memset(params, 0, sizeof(*params)); @@ -102,7 +105,7 @@ static uint8_t discover_func(struct bt_conn *conn, params->type = BT_GATT_DISCOVER_DESCRIPTOR; err = bt_gatt_discover(conn, params); - BSIM_ASSERT(!err, "bt_gatt_discover failed (%d)\n", err); + TEST_ASSERT(!err, "bt_gatt_discover failed (%d)", err); return BT_GATT_ITER_STOP; } @@ -131,7 +134,7 @@ static void gatt_discover(void) discover_params.type = BT_GATT_DISCOVER_CHARACTERISTIC; err = bt_gatt_discover(get_g_conn(), &discover_params); - BSIM_ASSERT(!err, "bt_gatt_discover failed (%d)\n", err); + TEST_ASSERT(!err, "bt_gatt_discover failed (%d)", err); WAIT_FOR_FLAG(flag_discovered); @@ -161,13 +164,13 @@ void central(void) }; err = bt_enable(NULL); - BSIM_ASSERT(!err, "bt_enable failed (%d)\n", err); + TEST_ASSERT(!err, "bt_enable failed (%d)", err); err = bt_conn_auth_info_cb_register(&bt_conn_auth_info_cb); - BSIM_ASSERT(!err, "bt_conn_auth_info_cb_register failed.\n"); + TEST_ASSERT(!err, "bt_conn_auth_info_cb_register failed."); err = settings_load(); - BSIM_ASSERT(!err, "settings_load failed (%d)\n", err); + TEST_ASSERT(!err, "settings_load failed (%d)", err); scan_connect_to_first_result(); wait_connected(); @@ -191,5 +194,5 @@ void central(void) /* wait for service change indication */ WAIT_FOR_FLAG(flag_indicated); - PASS("PASS\n"); + TEST_PASS("PASS"); } diff --git a/tests/bsim/bluetooth/host/gatt/sc_indicate/src/main.c b/tests/bsim/bluetooth/host/gatt/sc_indicate/src/main.c index 97c0a48e20a8..4be6925f2f81 100644 --- a/tests/bsim/bluetooth/host/gatt/sc_indicate/src/main.c +++ b/tests/bsim/bluetooth/host/gatt/sc_indicate/src/main.c @@ -7,38 +7,16 @@ #include "bstests.h" #include "bs_bt_utils.h" -#define BS_SECONDS_TO_US(dur_sec) ((bs_time_t)dur_sec * USEC_PER_SEC) -#define TEST_TIMEOUT_SIMULATED BS_SECONDS_TO_US(60) - extern void central(void); extern void peripheral(void); -static void test_tick(bs_time_t HW_device_time) -{ - bs_trace_debug_time(0, "Simulation ends now.\n"); - if (bst_result != Passed) { - bst_result = Failed; - bs_trace_error("Test did not pass before simulation ended.\n"); - } -} - -static void test_init(void) -{ - bst_ticker_set_next_tick_absolute(TEST_TIMEOUT_SIMULATED); - bst_result = In_progress; -} - static const struct bst_test_instance test_to_add[] = { { .test_id = "central", - .test_pre_init_f = test_init, - .test_tick_f = test_tick, .test_main_f = central, }, { .test_id = "peripheral", - .test_pre_init_f = test_init, - .test_tick_f = test_tick, .test_main_f = peripheral, }, BSTEST_END_MARKER, diff --git a/tests/bsim/bluetooth/host/gatt/sc_indicate/src/peripheral.c b/tests/bsim/bluetooth/host/gatt/sc_indicate/src/peripheral.c index e15c00b60d72..7d1877b64142 100644 --- a/tests/bsim/bluetooth/host/gatt/sc_indicate/src/peripheral.c +++ b/tests/bsim/bluetooth/host/gatt/sc_indicate/src/peripheral.c @@ -14,6 +14,8 @@ #include #include +#include "babblekit/testcase.h" + #include LOG_MODULE_REGISTER(test_peripheral, LOG_LEVEL_DBG); @@ -60,10 +62,10 @@ void peripheral(void) struct bt_le_ext_adv *adv = NULL; err = bt_enable(NULL); - BSIM_ASSERT(!err, "bt_enable failed (%d)\n", err); + TEST_ASSERT(!err, "bt_enable failed (%d)", err); err = settings_load(); - BSIM_ASSERT(!err, "settings_load failed (%d)\n", err); + TEST_ASSERT(!err, "settings_load failed (%d)", err); create_adv(&adv); start_adv(adv); @@ -76,11 +78,11 @@ void peripheral(void) /* add a new service to trigger the service changed indication */ err = bt_gatt_service_register(&svc); - BSIM_ASSERT(!err, "bt_gatt_service_register failed (%d)\n", err); + TEST_ASSERT(!err, "bt_gatt_service_register failed (%d)", err); LOG_DBG("New service added"); start_adv(adv); wait_connected(); - PASS("Done\n"); + TEST_PASS("Done"); } diff --git a/tests/bsim/bluetooth/host/gatt/settings/CMakeLists.txt b/tests/bsim/bluetooth/host/gatt/settings/CMakeLists.txt index 32d9c0c6af46..bd6907e01fb5 100644 --- a/tests/bsim/bluetooth/host/gatt/settings/CMakeLists.txt +++ b/tests/bsim/bluetooth/host/gatt/settings/CMakeLists.txt @@ -5,6 +5,9 @@ cmake_minimum_required(VERSION 3.20.0) find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) project(bsim_test_gatt_settings) +add_subdirectory(${ZEPHYR_BASE}/tests/bsim/babblekit babblekit) +target_link_libraries(app PRIVATE babblekit) + target_sources(app PRIVATE src/server.c src/client.c diff --git a/tests/bsim/bluetooth/host/gatt/settings/src/client.c b/tests/bsim/bluetooth/host/gatt/settings/src/client.c index fc33f17bd4b7..23e947fbabd8 100644 --- a/tests/bsim/bluetooth/host/gatt/settings/src/client.c +++ b/tests/bsim/bluetooth/host/gatt/settings/src/client.c @@ -16,6 +16,8 @@ #include #include +#include "babblekit/testcase.h" + void client_round_0(void) { struct bt_conn *conn; @@ -168,5 +170,5 @@ void client_procedure(void) client_round_5(); client_round_6(); - PASS("PASS\n"); + TEST_PASS("PASS"); } diff --git a/tests/bsim/bluetooth/host/gatt/settings/src/gatt_utils.c b/tests/bsim/bluetooth/host/gatt/settings/src/gatt_utils.c index 3345f61e00ce..6368cb75a101 100644 --- a/tests/bsim/bluetooth/host/gatt/settings/src/gatt_utils.c +++ b/tests/bsim/bluetooth/host/gatt/settings/src/gatt_utils.c @@ -6,13 +6,15 @@ #include "utils.h" #include "argparse.h" -#include "bs_pc_backchannel.h" #include #include #include #include +#include "babblekit/testcase.h" +#include "babblekit/flags.h" + /* Custom Service Variables */ static const struct bt_uuid_128 test_svc_uuid = BT_UUID_INIT_128( 0xf0, 0xde, 0xbc, 0x9a, 0x78, 0x56, 0x34, 0x12, @@ -28,7 +30,7 @@ static const struct bt_uuid_128 test_chrc_uuid = BT_UUID_INIT_128( static uint8_t test_value[] = { 'T', 'e', 's', 't', '\0' }; -DEFINE_FLAG(flag_client_read); +DEFINE_FLAG_STATIC(flag_client_read); static ssize_t read_test(struct bt_conn *conn, const struct bt_gatt_attr *attr, void *buf, uint16_t len, uint16_t offset) @@ -130,7 +132,7 @@ static uint8_t discover_func(struct bt_conn *conn, const struct bt_gatt_attr *at printk("handle[%d] = 0x%x\n", i, gatt_handles[i]); if (gatt_handles[i] == 0) { - FAIL("Did not discover all characteristics\n"); + TEST_FAIL("Did not discover all characteristics"); } } @@ -222,7 +224,7 @@ void gatt_subscribe_to_service_changed(bool subscribe) } if (err != 0) { - FAIL("Subscription failed(err %d)\n", err); + TEST_FAIL("Subscription failed(err %d)", err); } else { printk("%subscribed %s SC indications\n", subscribe ? "S" : "Uns", @@ -246,7 +248,7 @@ void gatt_discover(void) err = bt_gatt_discover(get_conn(), &discover_params); if (err != 0) { - FAIL("Discover failed(err %d)\n", err); + TEST_FAIL("Discover failed(err %d)", err); } WAIT_FOR_FLAG(flag_discovered); @@ -258,7 +260,7 @@ DEFINE_FLAG(flag_written); static void write_cb(struct bt_conn *conn, uint8_t err, struct bt_gatt_write_params *params) { if (err != BT_ATT_ERR_SUCCESS) { - FAIL("Write failed: 0x%02X\n", err); + TEST_FAIL("Write failed: 0x%02X", err); } SET_FLAG(flag_written); diff --git a/tests/bsim/bluetooth/host/gatt/settings/src/main.c b/tests/bsim/bluetooth/host/gatt/settings/src/main.c index 12fbed9e8f89..bffb5045b8dc 100644 --- a/tests/bsim/bluetooth/host/gatt/settings/src/main.c +++ b/tests/bsim/bluetooth/host/gatt/settings/src/main.c @@ -13,12 +13,11 @@ #include +#include "babblekit/testcase.h" + void server_procedure(void); void client_procedure(void); -#define BS_SECONDS(dur_sec) ((bs_time_t)dur_sec * USEC_PER_SEC) -#define TEST_TIMEOUT_SIMULATED BS_SECONDS(30) - static int test_round; static int final_round; static char *settings_file; @@ -50,33 +49,14 @@ static void test_args(int argc, char **argv) bs_trace_raw(0, "Final round %u\n", final_round); } -void test_tick(bs_time_t HW_device_time) -{ - bs_trace_debug_time(0, "Simulation ends now.\n"); - if (bst_result != Passed) { - bst_result = Failed; - bs_trace_error("Test did not pass before simulation ended.\n"); - } -} - -void test_init(void) -{ - bst_ticker_set_next_tick_absolute(TEST_TIMEOUT_SIMULATED); - bst_result = In_progress; -} - static const struct bst_test_instance test_to_add[] = { { .test_id = "server", - .test_pre_init_f = test_init, - .test_tick_f = test_tick, .test_main_f = server_procedure, .test_args_f = test_args, }, { .test_id = "client", - .test_pre_init_f = test_init, - .test_tick_f = test_tick, .test_main_f = client_procedure, .test_args_f = test_args, }, @@ -141,7 +121,7 @@ void backchannel_init(void) ch = bs_open_back_channel(device_number, device_numbers, channel_numbers, num_ch); if (!ch) { - FAIL("Unable to open backchannel\n"); + TEST_FAIL("Unable to open backchannel"); } } @@ -197,7 +177,7 @@ void signal_next_test_round(void) backchannel_sync_send(0); } - PASS("round %d over\n", get_test_round()); + TEST_PASS("round %d over", get_test_round()); stop_all_threads(); } diff --git a/tests/bsim/bluetooth/host/gatt/settings/src/server.c b/tests/bsim/bluetooth/host/gatt/settings/src/server.c index 7fff0f72c154..a70550dcd6a6 100644 --- a/tests/bsim/bluetooth/host/gatt/settings/src/server.c +++ b/tests/bsim/bluetooth/host/gatt/settings/src/server.c @@ -17,6 +17,8 @@ #include #include +#include "babblekit/testcase.h" + void set_public_addr(void) { bt_addr_le_t addr = {BT_ADDR_LE_RANDOM, @@ -200,7 +202,7 @@ void server_procedure(void) server_round_6(); break; default: - FAIL("Round %d doesn't exist\n", round); + TEST_FAIL("Round %d doesn't exist", round); break; } diff --git a/tests/bsim/bluetooth/host/gatt/settings/src/utils.c b/tests/bsim/bluetooth/host/gatt/settings/src/utils.c index c7cf895d8a2c..cad71a914220 100644 --- a/tests/bsim/bluetooth/host/gatt/settings/src/utils.c +++ b/tests/bsim/bluetooth/host/gatt/settings/src/utils.c @@ -8,18 +8,18 @@ #include "gatt_utils.h" #include #include +#include "babblekit/testcase.h" +#include "babblekit/flags.h" static struct bt_conn *default_conn; -DEFINE_FLAG(flag_is_connected); -DEFINE_FLAG(flag_test_end); +DEFINE_FLAG_STATIC(flag_is_connected); void wait_connected(void) { UNSET_FLAG(flag_is_connected); WAIT_FOR_FLAG(flag_is_connected); printk("connected\n"); - } void wait_disconnected(void) @@ -87,7 +87,7 @@ static void scan_connect_to_first_result_device_found(const bt_addr_le_t *addr, /* We're only interested in connectable events */ if (type != BT_HCI_ADV_IND && type != BT_HCI_ADV_DIRECT_IND) { - FAIL("Unexpected advertisement type."); + TEST_FAIL("Unexpected advertisement type."); } bt_addr_le_to_str(addr, addr_str, sizeof(addr_str)); @@ -147,7 +147,7 @@ DEFINE_FLAG(flag_pairing_complete); static void pairing_failed(struct bt_conn *conn, enum bt_security_err reason) { - FAIL("Pairing failed (unexpected): reason %u", reason); + TEST_FAIL("Pairing failed (unexpected): reason %u", reason); } static void pairing_complete(struct bt_conn *conn, bool bonded) diff --git a/tests/bsim/bluetooth/host/gatt/settings/src/utils.h b/tests/bsim/bluetooth/host/gatt/settings/src/utils.h index a721edc1d9c2..e5e7abf1e734 100644 --- a/tests/bsim/bluetooth/host/gatt/settings/src/utils.h +++ b/tests/bsim/bluetooth/host/gatt/settings/src/utils.h @@ -4,57 +4,10 @@ * SPDX-License-Identifier: Apache-2.0 */ -#include "bs_tracing.h" -#include "bs_types.h" -#include "bstests.h" -#include "time_machine.h" - #include #include -extern enum bst_result_t bst_result; - -#define DECLARE_FLAG(flag) extern atomic_t flag -#define DEFINE_FLAG(flag) atomic_t flag = (atomic_t) false -#define SET_FLAG(flag) (void)atomic_set(&flag, (atomic_t) true) -#define UNSET_FLAG(flag) (void)atomic_set(&flag, (atomic_t) false) -#define WAIT_FOR_FLAG(flag) \ - while (!(bool)atomic_get(&flag)) { \ - (void)k_sleep(K_MSEC(1)); \ - } -#define WAIT_FOR_FLAG_UNSET(flag) \ - while ((bool)atomic_get(&flag)) { \ - (void)k_sleep(K_MSEC(1)); \ - } -#define TAKE_FLAG(flag) \ - while (!(bool)atomic_cas(&flag, true, false)) { \ - (void)k_sleep(K_MSEC(1)); \ - } - -#define ASSERT(expr, ...) \ - do { \ - if (!(expr)) { \ - FAIL(__VA_ARGS__); \ - } \ - } while (0) - -DECLARE_FLAG(flag_test_end); - -#define FAIL(...) \ - SET_FLAG(flag_test_end); \ - do { \ - bst_result = Failed; \ - bs_trace_error_time_line(__VA_ARGS__); \ - } while (0) - -#define PASS(...) \ - SET_FLAG(flag_test_end); \ - do { \ - bst_result = Passed; \ - bs_trace_info_time(1, __VA_ARGS__); \ - } while (0) - void disconnect(struct bt_conn *conn); void wait_disconnected(void); struct bt_conn *get_conn(void); diff --git a/tests/bsim/bluetooth/host/gatt/settings_clear/src/client.c b/tests/bsim/bluetooth/host/gatt/settings_clear/src/client.c index 74501617f334..94163a9aed44 100644 --- a/tests/bsim/bluetooth/host/gatt/settings_clear/src/client.c +++ b/tests/bsim/bluetooth/host/gatt/settings_clear/src/client.c @@ -27,8 +27,8 @@ LOG_MODULE_REGISTER(client, LOG_LEVEL_DBG); -static DEFINE_FLAG(client_security_changed_flag); -static DEFINE_FLAG(client_is_subscribed_flag); +DEFINE_FLAG_STATIC(client_security_changed_flag); +DEFINE_FLAG_STATIC(client_is_subscribed_flag); /* Subscription parameters have the same lifetime as a subscription. * That is the backing struct should stay valid until a call to diff --git a/tests/bsim/bluetooth/host/gatt/settings_clear/src/server.c b/tests/bsim/bluetooth/host/gatt/settings_clear/src/server.c index 50cb684c3f1a..c450a2769d36 100644 --- a/tests/bsim/bluetooth/host/gatt/settings_clear/src/server.c +++ b/tests/bsim/bluetooth/host/gatt/settings_clear/src/server.c @@ -27,9 +27,9 @@ LOG_MODULE_REGISTER(server, LOG_LEVEL_DBG); -static DEFINE_FLAG(ccc_cfg_changed_flag); -static DEFINE_FLAG(disconnected_flag); -static DEFINE_FLAG(security_changed_flag); +DEFINE_FLAG_STATIC(ccc_cfg_changed_flag); +DEFINE_FLAG_STATIC(disconnected_flag); +DEFINE_FLAG_STATIC(security_changed_flag); static struct bt_conn_cb server_conn_cb; diff --git a/tests/bsim/bluetooth/host/id/settings/CMakeLists.txt b/tests/bsim/bluetooth/host/id/settings/CMakeLists.txt index 659359fc027e..0ebece0d4d04 100644 --- a/tests/bsim/bluetooth/host/id/settings/CMakeLists.txt +++ b/tests/bsim/bluetooth/host/id/settings/CMakeLists.txt @@ -5,6 +5,9 @@ cmake_minimum_required(VERSION 3.20.0) find_package(Zephyr HINTS $ENV{ZEPHYR_BASE}) project(bsim_test_id_settings) +add_subdirectory(${ZEPHYR_BASE}/tests/bsim/babblekit babblekit) +target_link_libraries(app PRIVATE babblekit) + target_sources(app PRIVATE src/main.c src/dut.c diff --git a/tests/bsim/bluetooth/host/id/settings/src/common.h b/tests/bsim/bluetooth/host/id/settings/src/common.h deleted file mode 100644 index 41d8ba0346e8..000000000000 --- a/tests/bsim/bluetooth/host/id/settings/src/common.h +++ /dev/null @@ -1,20 +0,0 @@ -/* Copyright (c) 2023 Nordic Semiconductor ASA - * SPDX-License-Identifier: Apache-2.0 - */ - -#include "bs_tracing.h" -#include "bstests.h" - -#define FAIL(...) \ - do { \ - bst_result = Failed; \ - bs_trace_error_time_line(__VA_ARGS__); \ - } while (0) - -#define PASS(...) \ - do { \ - bst_result = Passed; \ - bs_trace_info_time(1, __VA_ARGS__); \ - } while (0) - -extern enum bst_result_t bst_result; diff --git a/tests/bsim/bluetooth/host/id/settings/src/dut.c b/tests/bsim/bluetooth/host/id/settings/src/dut.c index 572ceaec1885..4e5244f995e5 100644 --- a/tests/bsim/bluetooth/host/id/settings/src/dut.c +++ b/tests/bsim/bluetooth/host/id/settings/src/dut.c @@ -11,7 +11,7 @@ #include #include -#include "common.h" +#include "babblekit/testcase.h" LOG_MODULE_DECLARE(bt_bsim_id_settings, LOG_LEVEL_DBG); @@ -24,14 +24,14 @@ void run_dut1(void) err = bt_enable(NULL); if (err) { - FAIL("Bluetooth init failed (err %d)\n", err); + TEST_FAIL("Bluetooth init failed (err %d)", err); } LOG_DBG("Bluetooth initialised"); err = settings_load(); if (err) { - FAIL("Failed to load settings (err %d)\n", err); + TEST_FAIL("Failed to load settings (err %d)", err); } bt_id_get(NULL, &bt_id_count); @@ -39,7 +39,7 @@ void run_dut1(void) err = bt_id_create(NULL, NULL); if (err < 0) { - FAIL("Failed to create a new identity (err %d)\n", err); + TEST_FAIL("Failed to create a new identity (err %d)", err); } bt_id_get(NULL, &bt_id_count); @@ -48,7 +48,7 @@ void run_dut1(void) /* Wait for the workqueue to complete before switching device */ k_msleep(100); - PASS("Test passed (DUT 1)\n"); + TEST_PASS("Test passed (DUT 1)"); } void run_dut2(void) @@ -61,22 +61,22 @@ void run_dut2(void) err = bt_enable(NULL); if (err) { - FAIL("Bluetooth init failed (err %d)\n", err); + TEST_FAIL("Bluetooth init failed (err %d)", err); } LOG_DBG("Bluetooth initialised"); err = settings_load(); if (err) { - FAIL("Failed to load settings (err %d)\n", err); + TEST_FAIL("Failed to load settings (err %d)", err); } LOG_DBG("Settings loaded"); bt_id_get(NULL, &bt_id_count); if (bt_id_count != expected_id_count) { - FAIL("Wrong ID count (got %d; expected %d)\n", bt_id_count, expected_id_count); + TEST_FAIL("Wrong ID count (got %d; expected %d)", bt_id_count, expected_id_count); } - PASS("Test passed (DUT 2)\n"); + TEST_PASS("Test passed (DUT 2)"); } diff --git a/tests/bsim/bluetooth/host/id/settings/src/main.c b/tests/bsim/bluetooth/host/id/settings/src/main.c index f385270cf974..bd965fc093d5 100644 --- a/tests/bsim/bluetooth/host/id/settings/src/main.c +++ b/tests/bsim/bluetooth/host/id/settings/src/main.c @@ -4,48 +4,25 @@ #include -#include "bs_types.h" -#include "bs_tracing.h" #include "bstests.h" #include #include -#include "common.h" - -#define WAIT_TIME_S 60 -#define WAIT_TIME (WAIT_TIME_S * 1e6) - LOG_MODULE_REGISTER(bt_bsim_id_settings, LOG_LEVEL_DBG); extern void run_dut1(void); extern void run_dut2(void); -void test_tick(bs_time_t HW_device_time) -{ - if (bst_result != Passed) { - FAIL("Test failed (not passed after %d seconds)\n", WAIT_TIME_S); - } -} - -static void test_id_settings_init(void) -{ - bst_ticker_set_next_tick_absolute(WAIT_TIME); -} - static const struct bst_test_instance test_def[] = { { .test_id = "dut1", .test_descr = "DUT 1", - .test_pre_init_f = test_id_settings_init, - .test_tick_f = test_tick, .test_main_f = run_dut1, }, { .test_id = "dut2", .test_descr = "DUT 2", - .test_pre_init_f = test_id_settings_init, - .test_tick_f = test_tick, .test_main_f = run_dut2, }, BSTEST_END_MARKER}; diff --git a/tests/bsim/bluetooth/host/iso/bis/src/bis_broadcaster.c b/tests/bsim/bluetooth/host/iso/bis/src/bis_broadcaster.c index cef7d417ff30..d18bc3050531 100644 --- a/tests/bsim/bluetooth/host/iso/bis/src/bis_broadcaster.c +++ b/tests/bsim/bluetooth/host/iso/bis/src/bis_broadcaster.c @@ -24,7 +24,7 @@ NET_BUF_POOL_FIXED_DEFINE(tx_pool, CONFIG_BT_ISO_TX_BUF_COUNT, BT_ISO_SDU_BUF_SIZE(CONFIG_BT_ISO_TX_MTU), CONFIG_BT_CONN_TX_USER_DATA_SIZE, NULL); -static DEFINE_FLAG(flag_iso_connected); +DEFINE_FLAG_STATIC(flag_iso_connected); static void send_data_cb(struct k_work *work); K_WORK_DELAYABLE_DEFINE(iso_send_work, send_data_cb); diff --git a/tests/bsim/bluetooth/host/iso/bis/src/bis_receiver.c b/tests/bsim/bluetooth/host/iso/bis/src/bis_receiver.c index d4a03284c17c..5f80abdd1c1d 100644 --- a/tests/bsim/bluetooth/host/iso/bis/src/bis_receiver.c +++ b/tests/bsim/bluetooth/host/iso/bis/src/bis_receiver.c @@ -22,11 +22,11 @@ LOG_MODULE_REGISTER(bis_receiver, LOG_LEVEL_INF); extern enum bst_result_t bst_result; -static DEFINE_FLAG(flag_broadcaster_found); -static DEFINE_FLAG(flag_iso_connected); -static DEFINE_FLAG(flag_data_received); -static DEFINE_FLAG(flag_pa_synced); -static DEFINE_FLAG(flag_biginfo); +DEFINE_FLAG_STATIC(flag_broadcaster_found); +DEFINE_FLAG_STATIC(flag_iso_connected); +DEFINE_FLAG_STATIC(flag_data_received); +DEFINE_FLAG_STATIC(flag_pa_synced); +DEFINE_FLAG_STATIC(flag_biginfo); static struct bt_iso_chan iso_chans[CONFIG_BT_ISO_MAX_CHAN]; static struct bt_le_scan_recv_info broadcaster_info; diff --git a/tests/bsim/bluetooth/host/iso/cis/src/cis_central.c b/tests/bsim/bluetooth/host/iso/cis/src/cis_central.c index 75c92a9e181e..f67afe3d3994 100644 --- a/tests/bsim/bluetooth/host/iso/cis/src/cis_central.c +++ b/tests/bsim/bluetooth/host/iso/cis/src/cis_central.c @@ -27,7 +27,7 @@ NET_BUF_POOL_FIXED_DEFINE(tx_pool, ENQUEUE_COUNT, BT_ISO_SDU_BUF_SIZE(CONFIG_BT_ BUILD_ASSERT(CONFIG_BT_ISO_MAX_CHAN > 1, "CONFIG_BT_ISO_MAX_CHAN shall be at least 2"); -static DEFINE_FLAG(flag_iso_connected); +DEFINE_FLAG_STATIC(flag_iso_connected); static void send_data_cb(struct k_work *work) { diff --git a/tests/bsim/bluetooth/host/iso/cis/src/cis_peripheral.c b/tests/bsim/bluetooth/host/iso/cis/src/cis_peripheral.c index abd7bba1c5a2..ef0ede7b0817 100644 --- a/tests/bsim/bluetooth/host/iso/cis/src/cis_peripheral.c +++ b/tests/bsim/bluetooth/host/iso/cis/src/cis_peripheral.c @@ -18,7 +18,7 @@ extern enum bst_result_t bst_result; -static DEFINE_FLAG(flag_data_received); +DEFINE_FLAG_STATIC(flag_data_received); static const struct bt_data ad[] = { BT_DATA_BYTES(BT_DATA_FLAGS, (BT_LE_AD_GENERAL | BT_LE_AD_NO_BREDR)), diff --git a/tests/bsim/bluetooth/host/iso/frag/src/broadcaster.c b/tests/bsim/bluetooth/host/iso/frag/src/broadcaster.c index 87dfd08f7ceb..cdc9d68aceee 100644 --- a/tests/bsim/bluetooth/host/iso/frag/src/broadcaster.c +++ b/tests/bsim/bluetooth/host/iso/frag/src/broadcaster.c @@ -21,8 +21,8 @@ NET_BUF_POOL_FIXED_DEFINE(tx_pool, CONFIG_BT_ISO_TX_BUF_COUNT, BT_ISO_SDU_BUF_SIZE(CONFIG_BT_ISO_TX_MTU), CONFIG_BT_CONN_TX_USER_DATA_SIZE, NULL); -static DEFINE_FLAG(iso_connected); -static DEFINE_FLAG(sdu_sent); +DEFINE_FLAG_STATIC(iso_connected); +DEFINE_FLAG_STATIC(sdu_sent); static int send_data(struct bt_iso_chan *chan, bool ts) { diff --git a/tests/bsim/bluetooth/host/iso/frag_2/src/broadcaster.c b/tests/bsim/bluetooth/host/iso/frag_2/src/broadcaster.c index 6b70c3fd0564..c5491a5520fb 100644 --- a/tests/bsim/bluetooth/host/iso/frag_2/src/broadcaster.c +++ b/tests/bsim/bluetooth/host/iso/frag_2/src/broadcaster.c @@ -22,9 +22,9 @@ NET_BUF_POOL_FIXED_DEFINE(tx_pool, CONFIG_BT_ISO_TX_BUF_COUNT, BT_ISO_SDU_BUF_SIZE(CONFIG_BT_ISO_TX_MTU), CONFIG_BT_CONN_TX_USER_DATA_SIZE, NULL); -static DEFINE_FLAG(iso_connected); -static DEFINE_FLAG(first_frag); -static DEFINE_FLAG(sdu_sent); +DEFINE_FLAG_STATIC(iso_connected); +DEFINE_FLAG_STATIC(first_frag); +DEFINE_FLAG_STATIC(sdu_sent); extern void bt_conn_suspend_tx(bool suspend); extern void bt_testing_set_iso_mtu(uint16_t mtu); diff --git a/tests/bsim/bluetooth/host/l2cap/credits/src/main.c b/tests/bsim/bluetooth/host/l2cap/credits/src/main.c index 8e7df0f5ade5..a667a7601a69 100644 --- a/tests/bsim/bluetooth/host/l2cap/credits/src/main.c +++ b/tests/bsim/bluetooth/host/l2cap/credits/src/main.c @@ -23,8 +23,8 @@ #include LOG_MODULE_REGISTER(LOG_MODULE_NAME, LOG_LEVEL_DBG); -static DEFINE_FLAG(is_connected); -static DEFINE_FLAG(flag_l2cap_connected); +DEFINE_FLAG_STATIC(is_connected); +DEFINE_FLAG_STATIC(flag_l2cap_connected); #define L2CAP_MPS CONFIG_BT_L2CAP_TX_MTU #define SDU_NUM 3 diff --git a/tests/bsim/bluetooth/host/l2cap/credits_seg_recv/src/main.c b/tests/bsim/bluetooth/host/l2cap/credits_seg_recv/src/main.c index d06188510c41..07ee76eed261 100644 --- a/tests/bsim/bluetooth/host/l2cap/credits_seg_recv/src/main.c +++ b/tests/bsim/bluetooth/host/l2cap/credits_seg_recv/src/main.c @@ -21,8 +21,8 @@ #include LOG_MODULE_REGISTER(LOG_MODULE_NAME, LOG_LEVEL_DBG); -static DEFINE_FLAG(is_connected); -static DEFINE_FLAG(flag_l2cap_connected); +DEFINE_FLAG_STATIC(is_connected); +DEFINE_FLAG_STATIC(flag_l2cap_connected); #define L2CAP_MPS CONFIG_BT_L2CAP_TX_MTU #define SDU_NUM 3 diff --git a/tests/bsim/bluetooth/host/l2cap/general/src/main_l2cap_ecred.c b/tests/bsim/bluetooth/host/l2cap/general/src/main_l2cap_ecred.c index 4cd2f619ff7b..87fd1f169cbb 100644 --- a/tests/bsim/bluetooth/host/l2cap/general/src/main_l2cap_ecred.c +++ b/tests/bsim/bluetooth/host/l2cap/general/src/main_l2cap_ecred.c @@ -66,8 +66,8 @@ struct channel { }; static struct channel channels[L2CAP_CHANNELS]; -static DEFINE_FLAG(is_connected); -static DEFINE_FLAG(unsequenced_data); +DEFINE_FLAG_STATIC(is_connected); +DEFINE_FLAG_STATIC(unsequenced_data); #define T_STACK_SIZE 512 #define T_PRIORITY 5 diff --git a/tests/bsim/bluetooth/host/l2cap/many_conns/src/main.c b/tests/bsim/bluetooth/host/l2cap/many_conns/src/main.c index 174d3739086b..ff9f3030d1eb 100644 --- a/tests/bsim/bluetooth/host/l2cap/many_conns/src/main.c +++ b/tests/bsim/bluetooth/host/l2cap/many_conns/src/main.c @@ -23,8 +23,8 @@ #include LOG_MODULE_REGISTER(LOG_MODULE_NAME, LOG_LEVEL_INF); -static DEFINE_FLAG(is_connected); -static DEFINE_FLAG(flag_l2cap_connected); +DEFINE_FLAG_STATIC(is_connected); +DEFINE_FLAG_STATIC(flag_l2cap_connected); #define NUM_PERIPHERALS CONFIG_BT_MAX_CONN #define L2CAP_CHANS NUM_PERIPHERALS diff --git a/tests/bsim/bluetooth/host/l2cap/multilink_peripheral/src/dut.c b/tests/bsim/bluetooth/host/l2cap/multilink_peripheral/src/dut.c index e62eb7f67721..9fa65ce43362 100644 --- a/tests/bsim/bluetooth/host/l2cap/multilink_peripheral/src/dut.c +++ b/tests/bsim/bluetooth/host/l2cap/multilink_peripheral/src/dut.c @@ -18,7 +18,7 @@ LOG_MODULE_REGISTER(dut, CONFIG_APP_LOG_LEVEL); -static DEFINE_FLAG(ADVERTISING); +DEFINE_FLAG_STATIC(ADVERTISING); static void sdu_destroy(struct net_buf *buf) { diff --git a/tests/bsim/bluetooth/host/l2cap/reassembly/dut/src/dut.c b/tests/bsim/bluetooth/host/l2cap/reassembly/dut/src/dut.c index 405fd47beb9c..5693f7deccc4 100644 --- a/tests/bsim/bluetooth/host/l2cap/reassembly/dut/src/dut.c +++ b/tests/bsim/bluetooth/host/l2cap/reassembly/dut/src/dut.c @@ -26,7 +26,7 @@ LOG_MODULE_REGISTER(dut, LOG_LEVEL_DBG); extern unsigned long runtime_log_level; -static DEFINE_FLAG(got_notification); +DEFINE_FLAG_STATIC(got_notification); static uint8_t received_notification(struct bt_conn *conn, struct bt_gatt_subscribe_params *params, diff --git a/tests/bsim/bluetooth/host/l2cap/reassembly/peer/src/peer.c b/tests/bsim/bluetooth/host/l2cap/reassembly/peer/src/peer.c index b17246bb8b0f..dd9d0e77fb88 100644 --- a/tests/bsim/bluetooth/host/l2cap/reassembly/peer/src/peer.c +++ b/tests/bsim/bluetooth/host/l2cap/reassembly/peer/src/peer.c @@ -283,7 +283,7 @@ static void send_cmd(uint16_t opcode, struct net_buf *cmd, struct net_buf **rsp) } k_sem_take(&cmd_sem, K_FOREVER); - TEST_ASSERT(active_opcode == 0xFFFF, ""); + TEST_ASSERT_NO_MSG(active_opcode == 0xFFFF); __ASSERT_NO_MSG(opcode); active_opcode = opcode; @@ -353,7 +353,7 @@ static void set_event_mask(uint16_t opcode) /* The two commands have the same length/params */ buf = bt_hci_cmd_create(opcode, sizeof(*cp_mask)); - TEST_ASSERT(buf, ""); + TEST_ASSERT_NO_MSG(buf); /* Forward all events */ cp_mask = net_buf_add(buf, sizeof(*cp_mask)); @@ -371,7 +371,7 @@ static void set_random_address(void) LOG_DBG("%s", bt_addr_str(&addr.a)); buf = bt_hci_cmd_create(BT_HCI_OP_LE_SET_RANDOM_ADDRESS, sizeof(addr.a)); - TEST_ASSERT(buf, ""); + TEST_ASSERT_NO_MSG(buf); net_buf_add_mem(buf, &addr.a, sizeof(addr.a)); send_cmd(BT_HCI_OP_LE_SET_RANDOM_ADDRESS, buf, NULL); diff --git a/tests/bsim/bluetooth/host/l2cap/send_on_connect/src/main_l2cap_send_on_connect.c b/tests/bsim/bluetooth/host/l2cap/send_on_connect/src/main_l2cap_send_on_connect.c index 825a8a74a2e3..3031fcb7b28a 100644 --- a/tests/bsim/bluetooth/host/l2cap/send_on_connect/src/main_l2cap_send_on_connect.c +++ b/tests/bsim/bluetooth/host/l2cap/send_on_connect/src/main_l2cap_send_on_connect.c @@ -17,9 +17,9 @@ static struct bt_conn *default_conn; #define PSM 0x80 -static DEFINE_FLAG(is_connected); -static DEFINE_FLAG(chan_connected); -static DEFINE_FLAG(data_received); +DEFINE_FLAG_STATIC(is_connected); +DEFINE_FLAG_STATIC(chan_connected); +DEFINE_FLAG_STATIC(data_received); #define DATA_BYTE_VAL 0xBB diff --git a/tests/bsim/bluetooth/host/l2cap/split/common/utils.h b/tests/bsim/bluetooth/host/l2cap/split/common/utils.h deleted file mode 100644 index a21a3c324fef..000000000000 --- a/tests/bsim/bluetooth/host/l2cap/split/common/utils.h +++ /dev/null @@ -1,51 +0,0 @@ -/* - * Copyright (c) 2023 Nordic Semiconductor ASA - * - * SPDX-License-Identifier: Apache-2.0 - */ - -#include "bs_tracing.h" -#include "bs_types.h" -#include "bstests.h" - -#define BS_SECONDS(dur_sec) ((bs_time_t)dur_sec * USEC_PER_SEC) -#define TEST_TIMEOUT_SIMULATED BS_SECONDS(30) - -extern enum bst_result_t bst_result; - -#define DECLARE_FLAG(flag) extern atomic_t flag -#define DEFINE_FLAG(flag) atomic_t flag = (atomic_t) false -#define SET_FLAG(flag) (void)atomic_set(&flag, (atomic_t) true) -#define UNSET_FLAG(flag) (void)atomic_set(&flag, (atomic_t) false) - -#define WAIT_FOR_FLAG(flag) \ - while (!(bool)atomic_get(&flag)) { \ - (void)k_sleep(K_MSEC(1)); \ - } -#define WAIT_FOR_FLAG_UNSET(flag) \ - while ((bool)atomic_get(&flag)) { \ - (void)k_sleep(K_MSEC(1)); \ - } -#define TAKE_FLAG(flag) \ - while (!(bool)atomic_cas(&flag, true, false)) { \ - (void)k_sleep(K_MSEC(1)); \ - } - -#define ASSERT(expr, ...) \ - do { \ - if (!(expr)) { \ - FAIL(__VA_ARGS__); \ - } \ - } while (0) - -#define FAIL(...) \ - do { \ - bst_result = Failed; \ - bs_trace_error_time_line(__VA_ARGS__); \ - } while (0) - -#define PASS(...) \ - do { \ - bst_result = Passed; \ - bs_trace_info_time(1, __VA_ARGS__); \ - } while (0) diff --git a/tests/bsim/bluetooth/host/l2cap/split/dut/CMakeLists.txt b/tests/bsim/bluetooth/host/l2cap/split/dut/CMakeLists.txt index e5120c13de06..bca0f9c5d8f2 100644 --- a/tests/bsim/bluetooth/host/l2cap/split/dut/CMakeLists.txt +++ b/tests/bsim/bluetooth/host/l2cap/split/dut/CMakeLists.txt @@ -5,6 +5,9 @@ cmake_minimum_required(VERSION 3.20.0) find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) project(bsim_test_l2cap_split_dut) +add_subdirectory(${ZEPHYR_BASE}/tests/bsim/babblekit babblekit) +target_link_libraries(app PRIVATE babblekit) + target_sources(app PRIVATE src/main.c ) diff --git a/tests/bsim/bluetooth/host/l2cap/split/dut/src/main.c b/tests/bsim/bluetooth/host/l2cap/split/dut/src/main.c index 72818bee5c26..fad4b90d6147 100644 --- a/tests/bsim/bluetooth/host/l2cap/split/dut/src/main.c +++ b/tests/bsim/bluetooth/host/l2cap/split/dut/src/main.c @@ -11,16 +11,17 @@ #include "host/hci_core.h" #include "common.h" -#include "utils.h" -#include "bstests.h" + +#include "babblekit/testcase.h" +#include "babblekit/flags.h" #define LOG_MODULE_NAME main #include LOG_MODULE_REGISTER(LOG_MODULE_NAME, LOG_LEVEL_INF); -DEFINE_FLAG(is_connected); -DEFINE_FLAG(flag_l2cap_connected); -DEFINE_FLAG(flag_l2cap_rx_ok); +DEFINE_FLAG_STATIC(is_connected); +DEFINE_FLAG_STATIC(flag_l2cap_connected); +DEFINE_FLAG_STATIC(flag_l2cap_rx_ok); static struct bt_l2cap_le_chan test_chan; @@ -115,7 +116,7 @@ static int l2cap_server_register(bt_security_t sec_level) int err = bt_l2cap_server_register(&test_l2cap_server); - ASSERT(err == 0, "Failed to register l2cap server."); + TEST_ASSERT(err == 0, "Failed to register l2cap server."); return test_l2cap_server.psm; } @@ -127,7 +128,7 @@ static void connected(struct bt_conn *conn, uint8_t conn_err) bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr)); if (conn_err) { - FAIL("Failed to connect to %s (%u)", addr, conn_err); + TEST_FAIL("Failed to connect to %s (%u)", addr, conn_err); return; } @@ -161,7 +162,7 @@ static void device_found(const bt_addr_le_t *addr, int8_t rssi, uint8_t type, err = bt_le_scan_stop(); if (err) { - FAIL("Stop LE scan failed (err %d)", err); + TEST_FAIL("Stop LE scan failed (err %d)", err); return; } @@ -174,7 +175,7 @@ static void device_found(const bt_addr_le_t *addr, int8_t rssi, uint8_t type, param = BT_LE_CONN_PARAM_DEFAULT; err = bt_conn_le_create(addr, BT_CONN_LE_CREATE_CONN, param, &conn); if (err) { - FAIL("Create conn failed (err %d)", err); + TEST_FAIL("Create conn failed (err %d)", err); return; } } @@ -192,7 +193,7 @@ static void connect(void) int err = bt_le_scan_start(&scan_param, device_found); - ASSERT(!err, "Scanning failed to start (err %d)\n", err); + TEST_ASSERT(!err, "Scanning failed to start (err %d)", err); LOG_DBG("Central initiating connection..."); WAIT_FOR_FLAG(is_connected); @@ -205,7 +206,7 @@ static void disconnect_device(struct bt_conn *conn, void *data) SET_FLAG(is_connected); err = bt_conn_disconnect(conn, BT_HCI_ERR_REMOTE_USER_TERM_CONN); - ASSERT(!err, "Failed to initate disconnect (err %d)", err); + TEST_ASSERT(!err, "Failed to initate disconnect (err %d)", err); LOG_DBG("Waiting for disconnection..."); WAIT_FOR_FLAG_UNSET(is_connected); @@ -221,7 +222,7 @@ static void do_dlu(struct bt_conn *conn, void *data) param.tx_max_time = 1712; err = bt_conn_le_data_len_update(conn, ¶m); - ASSERT(err == 0, "Can't update data length (err %d)\n", err); + TEST_ASSERT(err == 0, "Can't update data length (err %d)", err); } void test_procedure_0(void) @@ -230,7 +231,7 @@ void test_procedure_0(void) int err; err = bt_enable(NULL); - ASSERT(err == 0, "Can't enable Bluetooth (err %d)\n", err); + TEST_ASSERT(err == 0, "Can't enable Bluetooth (err %d)", err); LOG_DBG("Central Bluetooth initialized."); int psm = l2cap_server_register(BT_SECURITY_L1); @@ -246,29 +247,12 @@ void test_procedure_0(void) bt_conn_foreach(BT_CONN_TYPE_LE, disconnect_device, NULL); - PASS("DUT done\n"); -} - -void test_tick(bs_time_t HW_device_time) -{ - bs_trace_debug_time(0, "Simulation ends now.\n"); - if (bst_result != Passed) { - bst_result = Failed; - bs_trace_error("Test did not pass before simulation ended.\n"); - } -} - -void test_init(void) -{ - bst_ticker_set_next_tick_absolute(TEST_TIMEOUT_SIMULATED); - bst_result = In_progress; + TEST_PASS("DUT done"); } static const struct bst_test_instance test_to_add[] = { { .test_id = "test_0", - .test_pre_init_f = test_init, - .test_tick_f = test_tick, .test_main_f = test_procedure_0, }, BSTEST_END_MARKER, diff --git a/tests/bsim/bluetooth/host/l2cap/split/tester/CMakeLists.txt b/tests/bsim/bluetooth/host/l2cap/split/tester/CMakeLists.txt index d294025336c7..6fab142893a4 100644 --- a/tests/bsim/bluetooth/host/l2cap/split/tester/CMakeLists.txt +++ b/tests/bsim/bluetooth/host/l2cap/split/tester/CMakeLists.txt @@ -5,6 +5,9 @@ cmake_minimum_required(VERSION 3.20.0) find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) project(bsim_test_l2cap_split_tester) +add_subdirectory(${ZEPHYR_BASE}/tests/bsim/babblekit babblekit) +target_link_libraries(app PRIVATE babblekit) + target_sources(app PRIVATE src/main.c) zephyr_include_directories( diff --git a/tests/bsim/bluetooth/host/l2cap/split/tester/src/main.c b/tests/bsim/bluetooth/host/l2cap/split/tester/src/main.c index 8e5d6522548c..af3467948bcf 100644 --- a/tests/bsim/bluetooth/host/l2cap/split/tester/src/main.c +++ b/tests/bsim/bluetooth/host/l2cap/split/tester/src/main.c @@ -21,16 +21,17 @@ #include "host/conn_internal.h" #include "host/l2cap_internal.h" -#include "utils.h" #include "common.h" -#include "bstests.h" + +#include "babblekit/testcase.h" +#include "babblekit/flags.h" #include LOG_MODULE_REGISTER(bt_tinyhost, 3); -DEFINE_FLAG(is_connected); -DEFINE_FLAG(flag_l2cap_connected); -DEFINE_FLAG(flag_data_length_updated); +DEFINE_FLAG_STATIC(is_connected); +DEFINE_FLAG_STATIC(flag_l2cap_connected); +DEFINE_FLAG_STATIC(flag_data_length_updated); static K_FIFO_DEFINE(rx_queue); @@ -203,10 +204,10 @@ static void handle_sig(struct net_buf *buf) handle_l2cap_credits(buf); return; case BT_L2CAP_DISCONN_REQ: - FAIL("channel disconnected\n"); + TEST_FAIL("channel disconnected"); return; default: - FAIL("unhandled opcode %x\n", hdr->code); + TEST_FAIL("unhandled opcode %x", hdr->code); return; } } @@ -231,7 +232,7 @@ static void handle_l2cap(struct net_buf *buf) /* CoC PDU */ if (cid == 0x0040) { - FAIL("unexpected data rx"); + TEST_FAIL("unexpected data rx"); } } @@ -642,29 +643,12 @@ void test_procedure_0(void) WAIT_FOR_FLAG_UNSET(is_connected); LOG_DBG("disconnected"); - PASS("Tester done\n"); -} - -void test_tick(bs_time_t HW_device_time) -{ - bs_trace_debug_time(0, "Simulation ends now.\n"); - if (bst_result != Passed) { - bst_result = Failed; - bs_trace_error("Test did not pass before simulation ended.\n"); - } -} - -void test_init(void) -{ - bst_ticker_set_next_tick_absolute(TEST_TIMEOUT_SIMULATED); - bst_result = In_progress; + TEST_PASS("Tester done"); } static const struct bst_test_instance test_to_add[] = { { .test_id = "test_0", - .test_pre_init_f = test_init, - .test_tick_f = test_tick, .test_main_f = test_procedure_0, }, BSTEST_END_MARKER, diff --git a/tests/bsim/bluetooth/host/l2cap/stress/src/main.c b/tests/bsim/bluetooth/host/l2cap/stress/src/main.c index e104fcfae810..d05f4e4f0a74 100644 --- a/tests/bsim/bluetooth/host/l2cap/stress/src/main.c +++ b/tests/bsim/bluetooth/host/l2cap/stress/src/main.c @@ -22,8 +22,8 @@ #include LOG_MODULE_REGISTER(LOG_MODULE_NAME, LOG_LEVEL_INF); -static DEFINE_FLAG(is_connected); -static DEFINE_FLAG(flag_l2cap_connected); +DEFINE_FLAG_STATIC(is_connected); +DEFINE_FLAG_STATIC(flag_l2cap_connected); #define NUM_PERIPHERALS 6 #define L2CAP_CHANS NUM_PERIPHERALS diff --git a/tests/bsim/bluetooth/host/l2cap/userdata/src/main_l2cap_userdata.c b/tests/bsim/bluetooth/host/l2cap/userdata/src/main_l2cap_userdata.c index e492036736f1..904bb073c3dc 100644 --- a/tests/bsim/bluetooth/host/l2cap/userdata/src/main_l2cap_userdata.c +++ b/tests/bsim/bluetooth/host/l2cap/userdata/src/main_l2cap_userdata.c @@ -21,10 +21,10 @@ static struct bt_conn *default_conn; /* Pool to allocate a buffer that is too large to send */ NET_BUF_POOL_DEFINE(buf_pool, 1, BT_L2CAP_SDU_BUF_SIZE(DATA_SIZE), USER_DATA_SIZE, NULL); -static DEFINE_FLAG(is_connected); -static DEFINE_FLAG(is_sent); -static DEFINE_FLAG(has_received); -static DEFINE_FLAG(chan_connected); +DEFINE_FLAG_STATIC(is_connected); +DEFINE_FLAG_STATIC(is_sent); +DEFINE_FLAG_STATIC(has_received); +DEFINE_FLAG_STATIC(chan_connected); static void chan_connected_cb(struct bt_l2cap_chan *l2cap_chan) { diff --git a/tests/bsim/bluetooth/host/misc/acl_tx_frag/src/dut.c b/tests/bsim/bluetooth/host/misc/acl_tx_frag/src/dut.c index 6fe11230201e..b09d192b14ef 100644 --- a/tests/bsim/bluetooth/host/misc/acl_tx_frag/src/dut.c +++ b/tests/bsim/bluetooth/host/misc/acl_tx_frag/src/dut.c @@ -27,11 +27,11 @@ LOG_MODULE_REGISTER(dut, LOG_LEVEL_DBG); -static DEFINE_FLAG(is_subscribed); -static DEFINE_FLAG(mtu_has_been_exchanged); -static DEFINE_FLAG(conn_recycled); -static DEFINE_FLAG(conn_param_updated); -static DEFINE_FLAG(indicated); +DEFINE_FLAG_STATIC(is_subscribed); +DEFINE_FLAG_STATIC(mtu_has_been_exchanged); +DEFINE_FLAG_STATIC(conn_recycled); +DEFINE_FLAG_STATIC(conn_param_updated); +DEFINE_FLAG_STATIC(indicated); extern unsigned long runtime_log_level; diff --git a/tests/bsim/bluetooth/host/misc/acl_tx_frag/src/peer.c b/tests/bsim/bluetooth/host/misc/acl_tx_frag/src/peer.c index af48857808aa..da130c1dc707 100644 --- a/tests/bsim/bluetooth/host/misc/acl_tx_frag/src/peer.c +++ b/tests/bsim/bluetooth/host/misc/acl_tx_frag/src/peer.c @@ -25,8 +25,8 @@ LOG_MODULE_REGISTER(peer, LOG_LEVEL_DBG); -static DEFINE_FLAG(is_subscribed); -static DEFINE_FLAG(got_notification_1); +DEFINE_FLAG_STATIC(is_subscribed); +DEFINE_FLAG_STATIC(got_notification_1); extern unsigned long runtime_log_level; diff --git a/tests/bsim/bluetooth/host/misc/disable/src/gatt_client_test.c b/tests/bsim/bluetooth/host/misc/disable/src/gatt_client_test.c index 4d53d60a7ec5..7608641f1784 100644 --- a/tests/bsim/bluetooth/host/misc/disable/src/gatt_client_test.c +++ b/tests/bsim/bluetooth/host/misc/disable/src/gatt_client_test.c @@ -18,10 +18,10 @@ #include "babblekit/flags.h" #include "common.h" -static DEFINE_FLAG(flag_is_connected); -static DEFINE_FLAG(flag_discover_complete); -static DEFINE_FLAG(flag_write_complete); -static DEFINE_FLAG(flag_read_complete); +DEFINE_FLAG_STATIC(flag_is_connected); +DEFINE_FLAG_STATIC(flag_discover_complete); +DEFINE_FLAG_STATIC(flag_write_complete); +DEFINE_FLAG_STATIC(flag_read_complete); static struct bt_conn *g_conn; static uint16_t chrc_handle; diff --git a/tests/bsim/bluetooth/host/misc/disable/src/gatt_server_test.c b/tests/bsim/bluetooth/host/misc/disable/src/gatt_server_test.c index 613f76d5c22b..0c8b4b9b3308 100644 --- a/tests/bsim/bluetooth/host/misc/disable/src/gatt_server_test.c +++ b/tests/bsim/bluetooth/host/misc/disable/src/gatt_server_test.c @@ -20,7 +20,7 @@ extern enum bst_result_t bst_result; -static DEFINE_FLAG(flag_is_connected); +DEFINE_FLAG_STATIC(flag_is_connected); #define NUM_ITERATIONS 10 diff --git a/tests/bsim/bluetooth/host/misc/disconnect/common/common.h b/tests/bsim/bluetooth/host/misc/disconnect/common/common.h new file mode 100644 index 000000000000..732b2afc4b56 --- /dev/null +++ b/tests/bsim/bluetooth/host/misc/disconnect/common/common.h @@ -0,0 +1,9 @@ +/* + * Copyright (c) 2025 Nordic Semiconductor ASA + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#define HVX_HANDLE 0x0012 +#define INDICATION_PAYLOAD "indication" +#define NOTIFICATION_PAYLOAD "notification" diff --git a/tests/bsim/bluetooth/host/misc/disconnect/common/sync.c b/tests/bsim/bluetooth/host/misc/disconnect/common/sync.c deleted file mode 100644 index d66a01832b3b..000000000000 --- a/tests/bsim/bluetooth/host/misc/disconnect/common/sync.c +++ /dev/null @@ -1,65 +0,0 @@ -/* - * Copyright (c) 2023 Nordic Semiconductor ASA - * - * SPDX-License-Identifier: Apache-2.0 - */ - -#include -#include -#include "argparse.h" -#include "bs_types.h" -#include "bs_tracing.h" -#include "time_machine.h" -#include "bstests.h" -#include "bs_pc_backchannel.h" - -#include -LOG_MODULE_REGISTER(sync, LOG_LEVEL_INF); - -#define CHANNEL_ID 0 -#define MSG_SIZE 1 - -int backchannel_init(void) -{ - uint device_number = get_device_nbr(); - uint peer_number = device_number ^ 1; - uint device_numbers[] = { peer_number }; - uint channel_numbers[] = { CHANNEL_ID }; - uint *ch; - - ch = bs_open_back_channel(device_number, device_numbers, channel_numbers, - ARRAY_SIZE(channel_numbers)); - - if (!ch) { - return -1; - } - - return 0; -} - -void backchannel_sync_send(void) -{ - uint8_t sync_msg[MSG_SIZE] = { get_device_nbr() }; - - LOG_INF("Sending sync"); - bs_bc_send_msg(CHANNEL_ID, sync_msg, ARRAY_SIZE(sync_msg)); -} - -void backchannel_sync_wait(void) -{ - uint8_t sync_msg[MSG_SIZE]; - - while (true) { - if (bs_bc_is_msg_received(CHANNEL_ID) > 0) { - bs_bc_receive_msg(CHANNEL_ID, sync_msg, ARRAY_SIZE(sync_msg)); - if (sync_msg[0] != get_device_nbr()) { - /* Received a message from another device, exit */ - break; - } - } - - k_sleep(K_MSEC(1)); - } - - LOG_INF("Sync received"); -} diff --git a/tests/bsim/bluetooth/host/misc/disconnect/common/sync.h b/tests/bsim/bluetooth/host/misc/disconnect/common/sync.h deleted file mode 100644 index aa3ef4e48ce2..000000000000 --- a/tests/bsim/bluetooth/host/misc/disconnect/common/sync.h +++ /dev/null @@ -1,9 +0,0 @@ -/* - * Copyright (c) 2023 Nordic Semiconductor ASA - * - * SPDX-License-Identifier: Apache-2.0 - */ - -int backchannel_init(void); -void backchannel_sync_send(void); -void backchannel_sync_wait(void); diff --git a/tests/bsim/bluetooth/host/misc/disconnect/common/utils.h b/tests/bsim/bluetooth/host/misc/disconnect/common/utils.h deleted file mode 100644 index d5be7e04bb3b..000000000000 --- a/tests/bsim/bluetooth/host/misc/disconnect/common/utils.h +++ /dev/null @@ -1,63 +0,0 @@ -/* - * Copyright (c) 2023 Nordic Semiconductor ASA - * - * SPDX-License-Identifier: Apache-2.0 - */ - -#include "bs_tracing.h" -#include "bs_types.h" -#include "bstests.h" - -#define BS_SECONDS(dur_sec) ((bs_time_t)dur_sec * USEC_PER_SEC) -#define TEST_TIMEOUT_SIMULATED BS_SECONDS(10) - -extern enum bst_result_t bst_result; - -#define DECLARE_FLAG(flag) extern atomic_t flag -#define DEFINE_FLAG(flag) atomic_t flag = (atomic_t) false -#define SET_FLAG(flag) (void)atomic_set(&flag, (atomic_t) true) -#define UNSET_FLAG(flag) (void)atomic_set(&flag, (atomic_t) false) - -#define WAIT_FOR_EXPR(var, expr) \ - while (atomic_get(&var) expr) { \ - (void)k_sleep(K_MSEC(1)); \ - } -#define WAIT_FOR_VAL(var, val) \ - while (atomic_get(&var) != val) { \ - (void)k_sleep(K_MSEC(1)); \ - } -#define WAIT_FOR_FLAG(flag) \ - while (!(bool)atomic_get(&flag)) { \ - (void)k_sleep(K_MSEC(1)); \ - } -#define WAIT_FOR_FLAG_UNSET(flag) \ - while ((bool)atomic_get(&flag)) { \ - (void)k_sleep(K_MSEC(1)); \ - } -#define TAKE_FLAG(flag) \ - while (!(bool)atomic_cas(&flag, true, false)) { \ - (void)k_sleep(K_MSEC(1)); \ - } - -#define ASSERT(expr, ...) \ - do { \ - if (!(expr)) { \ - FAIL(__VA_ARGS__); \ - } \ - } while (0) - -#define FAIL(...) \ - do { \ - bst_result = Failed; \ - bs_trace_error_time_line(__VA_ARGS__); \ - } while (0) - -#define PASS(...) \ - do { \ - bst_result = Passed; \ - bs_trace_info_time(1, __VA_ARGS__); \ - } while (0) - -#define HVX_HANDLE 0x0012 -#define INDICATION_PAYLOAD "indication" -#define NOTIFICATION_PAYLOAD "notification" diff --git a/tests/bsim/bluetooth/host/misc/disconnect/dut/CMakeLists.txt b/tests/bsim/bluetooth/host/misc/disconnect/dut/CMakeLists.txt index 9546e14eeb2a..00e3d30dab88 100644 --- a/tests/bsim/bluetooth/host/misc/disconnect/dut/CMakeLists.txt +++ b/tests/bsim/bluetooth/host/misc/disconnect/dut/CMakeLists.txt @@ -5,9 +5,11 @@ cmake_minimum_required(VERSION 3.20.0) find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) project(bsim_test_disconnect_dut) +add_subdirectory(${ZEPHYR_BASE}/tests/bsim/babblekit babblekit) +target_link_libraries(app PRIVATE babblekit) + target_sources(app PRIVATE src/main.c - ../common/sync.c ) zephyr_include_directories( diff --git a/tests/bsim/bluetooth/host/misc/disconnect/dut/src/main.c b/tests/bsim/bluetooth/host/misc/disconnect/dut/src/main.c index 90a70c30c529..6e63aa8a604e 100644 --- a/tests/bsim/bluetooth/host/misc/disconnect/dut/src/main.c +++ b/tests/bsim/bluetooth/host/misc/disconnect/dut/src/main.c @@ -16,16 +16,18 @@ #include #include -#include "utils.h" -#include "sync.h" -#include "bstests.h" +#include "babblekit/testcase.h" +#include "babblekit/flags.h" +#include "babblekit/sync.h" + +#include "common.h" #include LOG_MODULE_REGISTER(dut, LOG_LEVEL_INF); -DEFINE_FLAG(is_connected); -DEFINE_FLAG(is_subscribed); -DEFINE_FLAG(flag_data_length_updated); +DEFINE_FLAG_STATIC(is_connected); +DEFINE_FLAG_STATIC(is_subscribed); +DEFINE_FLAG_STATIC(flag_data_length_updated); static atomic_t notifications; @@ -41,7 +43,7 @@ static void connected(struct bt_conn *conn, uint8_t conn_err) bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr)); if (conn_err) { - FAIL("Failed to connect to %s (%u)", addr, conn_err); + TEST_FAIL("Failed to connect to %s (%u)", addr, conn_err); return; } @@ -81,7 +83,7 @@ static void do_dlu(void) param.tx_max_time = 2500; err = bt_conn_le_data_len_update(dconn, ¶m); - ASSERT(err == 0, "Can't update data length (err %d)\n", err); + TEST_ASSERT(err == 0, "Can't update data length (err %d)", err); WAIT_FOR_FLAG(flag_data_length_updated); } @@ -102,7 +104,7 @@ static void device_found(const bt_addr_le_t *addr, int8_t rssi, uint8_t type, err = bt_le_scan_stop(); if (err) { - FAIL("Stop LE scan failed (err %d)", err); + TEST_FAIL("Stop LE scan failed (err %d)", err); return; } @@ -112,7 +114,7 @@ static void device_found(const bt_addr_le_t *addr, int8_t rssi, uint8_t type, param = BT_LE_CONN_PARAM_DEFAULT; err = bt_conn_le_create(addr, BT_CONN_LE_CREATE_CONN, param, &conn); if (err) { - FAIL("Create conn failed (err %d)", err); + TEST_FAIL("Create conn failed (err %d)", err); return; } } @@ -130,7 +132,7 @@ static void connect(void) UNSET_FLAG(is_connected); err = bt_le_scan_start(&scan_param, device_found); - ASSERT(!err, "Scanning failed to start (err %d)\n", err); + TEST_ASSERT(!err, "Scanning failed to start (err %d)", err); LOG_DBG("Central initiating connection..."); WAIT_FOR_FLAG(is_connected); @@ -156,21 +158,21 @@ static uint8_t notified(struct bt_conn *conn, struct bt_gatt_subscribe_params *p return BT_GATT_ITER_CONTINUE; } - ASSERT(length >= sizeof(indication), "Unexpected data\n"); - ASSERT(length <= sizeof(notification), "Unexpected data\n"); + TEST_ASSERT(length >= sizeof(indication), "Unexpected data"); + TEST_ASSERT(length <= sizeof(notification), "Unexpected data"); is_nfy = memcmp(data, notification, length) == 0; LOG_INF("%s from 0x%x", is_nfy ? "notified" : "indicated", params->value_handle); - ASSERT(is_nfy, "Unexpected indication\n"); + TEST_ASSERT(is_nfy, "Unexpected indication"); atomic_inc(¬ifications); if (atomic_get(¬ifications) == 3) { LOG_INF("##################### BRB.."); - backchannel_sync_send(); + bk_sync_send(); /* Make scheduler rotate us in and out multiple times */ for (int i = 0; i < 10; i++) { @@ -189,9 +191,9 @@ static void subscribed(struct bt_conn *conn, uint8_t err, struct bt_gatt_subscribe_params *params) { - ASSERT(!err, "Subscribe failed (err %d)\n", err); + TEST_ASSERT(!err, "Subscribe failed (err %d)", err); - ASSERT(params, "params is NULL\n"); + TEST_ASSERT(params, "params is NULL"); SET_FLAG(is_subscribed); /* spoiler: tester doesn't really have attributes */ @@ -212,20 +214,20 @@ void subscribe(void) }; err = bt_gatt_subscribe(dconn, ¶ms); - ASSERT(!err, "Subscribe failed (err %d)\n", err); + TEST_ASSERT(!err, "Subscribe failed (err %d)", err); WAIT_FOR_FLAG(is_subscribed); } void test_procedure_0(void) { - ASSERT(backchannel_init() == 0, "Failed to open backchannel\n"); + TEST_ASSERT(bk_sync_init() == 0, "Failed to open backchannel"); LOG_DBG("Test start: ATT disconnect protocol"); int err; err = bt_enable(NULL); - ASSERT(err == 0, "Can't enable Bluetooth (err %d)\n", err); + TEST_ASSERT(err == 0, "Can't enable Bluetooth (err %d)", err); LOG_DBG("Central: Bluetooth initialized."); /* Test purpose: @@ -255,35 +257,18 @@ void test_procedure_0(void) do_dlu(); - WAIT_FOR_EXPR(notifications, < 4); + WAIT_FOR(atomic_get(¬ifications) < 4, 10000, k_msleep(1)); WAIT_FOR_FLAG_UNSET(is_connected); LOG_INF("##################### END TEST #####################"); - PASS("DUT exit\n"); -} - -void test_tick(bs_time_t HW_device_time) -{ - bs_trace_debug_time(0, "Simulation ends now.\n"); - if (bst_result != Passed) { - bst_result = Failed; - bs_trace_error("Test did not pass before simulation ended.\n"); - } -} - -void test_init(void) -{ - bst_ticker_set_next_tick_absolute(TEST_TIMEOUT_SIMULATED); - bst_result = In_progress; + TEST_PASS("DUT exit"); } static const struct bst_test_instance test_to_add[] = { { .test_id = "dut", - .test_pre_init_f = test_init, - .test_tick_f = test_tick, .test_main_f = test_procedure_0, }, BSTEST_END_MARKER, diff --git a/tests/bsim/bluetooth/host/misc/disconnect/tester/CMakeLists.txt b/tests/bsim/bluetooth/host/misc/disconnect/tester/CMakeLists.txt index e3a567377ef7..c734daa56039 100644 --- a/tests/bsim/bluetooth/host/misc/disconnect/tester/CMakeLists.txt +++ b/tests/bsim/bluetooth/host/misc/disconnect/tester/CMakeLists.txt @@ -5,9 +5,11 @@ cmake_minimum_required(VERSION 3.20.0) find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) project(bsim_test_disconnect_tester) +add_subdirectory(${ZEPHYR_BASE}/tests/bsim/babblekit babblekit) +target_link_libraries(app PRIVATE babblekit) + target_sources(app PRIVATE src/main.c - ../common/sync.c ) zephyr_include_directories( diff --git a/tests/bsim/bluetooth/host/misc/disconnect/tester/src/main.c b/tests/bsim/bluetooth/host/misc/disconnect/tester/src/main.c index 6f5dff3bafec..48923faa9805 100644 --- a/tests/bsim/bluetooth/host/misc/disconnect/tester/src/main.c +++ b/tests/bsim/bluetooth/host/misc/disconnect/tester/src/main.c @@ -21,11 +21,13 @@ #include "host/conn_internal.h" #include "host/l2cap_internal.h" -#include "utils.h" -#include "sync.h" -#include "bstests.h" +#include "babblekit/testcase.h" +#include "babblekit/flags.h" +#include "babblekit/sync.h" #include "NRF_HWLowL.h" /* for hwll_disconnect_phy(); */ +#include "common.h" + #include LOG_MODULE_REGISTER(bt_tinyhost, LOG_LEVEL_INF); @@ -39,8 +41,8 @@ LOG_MODULE_REGISTER(bt_tinyhost, LOG_LEVEL_INF); #define BT_ATT_OP_WRITE_CMD 0x52 #define BT_L2CAP_CID_ATT 0x0004 -DEFINE_FLAG(is_connected); -DEFINE_FLAG(flag_data_length_updated); +DEFINE_FLAG_STATIC(is_connected); +DEFINE_FLAG_STATIC(flag_data_length_updated); static K_FIFO_DEFINE(rx_queue); @@ -63,7 +65,7 @@ struct net_buf *bt_hci_cmd_create(uint16_t opcode, uint8_t param_len) LOG_DBG("opcode 0x%04x param_len %u", opcode, param_len); buf = net_buf_alloc(&hci_cmd_pool, K_FOREVER); - ASSERT(buf, "failed allocation"); + TEST_ASSERT(buf, "failed allocation"); LOG_DBG("buf %p", buf); @@ -106,14 +108,14 @@ static void handle_cmd_complete(struct net_buf *buf) opcode = sys_le16_to_cpu(evt->opcode); } else { - FAIL("unhandled event 0x%x", hdr->evt); + TEST_FAIL("unhandled event 0x%x", hdr->evt); } LOG_DBG("opcode 0x%04x status %x", opcode, status); - ASSERT(status == 0x00, "cmd status: %x", status); + TEST_ASSERT(status == 0x00, "cmd status: %x", status); - ASSERT(active_opcode == opcode, "unexpected opcode %x != %x", active_opcode, opcode); + TEST_ASSERT(active_opcode == opcode, "unexpected opcode %x != %x", active_opcode, opcode); if (active_opcode) { active_opcode = 0xFFFF; @@ -188,8 +190,8 @@ static void handle_att_write(struct net_buf *buf) static uint8_t ccc_write[2] = {0x03, 0x00}; - ASSERT(buf->len == 2, "unexpected write length: %d\n", buf->len); - ASSERT(memcmp(buf->data, ccc_write, sizeof(ccc_write)) == 0, "bad data\n"); + TEST_ASSERT(buf->len == 2, "unexpected write length: %d", buf->len); + TEST_ASSERT(memcmp(buf->data, ccc_write, sizeof(ccc_write)) == 0, "bad data"); send_write_rsp(); } @@ -207,7 +209,7 @@ static void handle_att(struct net_buf *buf) return; default: LOG_HEXDUMP_ERR(buf->data, buf->len, "payload"); - FAIL("unhandled opcode %x\n", op); + TEST_FAIL("unhandled opcode %x", op); return; } } @@ -224,10 +226,9 @@ static void handle_l2cap(struct net_buf *buf) LOG_HEXDUMP_DBG(buf->data, buf->len, "l2cap"); /* Make sure we don't have to recombine packets */ - ASSERT(buf->len == hdr->len, "buflen = %d != hdrlen %d", - buf->len, hdr->len); + TEST_ASSERT(buf->len == hdr->len, "buflen = %d != hdrlen %d", buf->len, hdr->len); - ASSERT(cid == BT_L2CAP_CID_ATT, "We only support (U)ATT"); + TEST_ASSERT(cid == BT_L2CAP_CID_ATT, "We only support (U)ATT"); /* (U)ATT PDU */ handle_att(buf); @@ -246,8 +247,7 @@ static void handle_acl(struct net_buf *buf) flags = bt_acl_flags(handle); handle = bt_acl_handle(handle); - ASSERT(flags == BT_ACL_START, - "Fragmentation not supported"); + TEST_ASSERT(flags == BT_ACL_START, "Fragmentation not supported"); LOG_DBG("ACL: conn %d len %d flags %d", handle, len, flags); LOG_HEXDUMP_DBG(buf->data, buf->len, "HCI ACL"); @@ -307,7 +307,7 @@ static void send_cmd(uint16_t opcode, struct net_buf *cmd, struct net_buf **rsp) } k_sem_take(&cmd_sem, K_FOREVER); - ASSERT(active_opcode == 0xFFFF, ""); + TEST_ASSERT_NO_MSG(active_opcode == 0xFFFF); active_opcode = opcode; @@ -376,7 +376,7 @@ static void write_default_data_len(uint16_t tx_octets, uint16_t tx_time) struct bt_hci_cp_le_write_default_data_len *cp; struct net_buf *buf = bt_hci_cmd_create(BT_HCI_OP_LE_WRITE_DEFAULT_DATA_LEN, sizeof(*cp)); - ASSERT(buf, ""); + TEST_ASSERT_NO_MSG(buf); cp = net_buf_add(buf, sizeof(*cp)); cp->max_tx_octets = sys_cpu_to_le16(tx_octets); @@ -401,7 +401,7 @@ static void set_event_mask(uint16_t opcode) /* The two commands have the same length/params */ buf = bt_hci_cmd_create(opcode, sizeof(*cp_mask)); - ASSERT(buf, ""); + TEST_ASSERT_NO_MSG(buf); /* Forward all events */ cp_mask = net_buf_add(buf, sizeof(*cp_mask)); @@ -419,7 +419,7 @@ static void set_random_address(void) LOG_DBG("%s", bt_addr_str(&addr.a)); buf = bt_hci_cmd_create(BT_HCI_OP_LE_SET_RANDOM_ADDRESS, sizeof(addr.a)); - ASSERT(buf, ""); + TEST_ASSERT_NO_MSG(buf); net_buf_add_mem(buf, &addr.a, sizeof(addr.a)); send_cmd(BT_HCI_OP_LE_SET_RANDOM_ADDRESS, buf, NULL); @@ -459,7 +459,7 @@ struct net_buf *alloc_l2cap_pdu(void) uint16_t reserve; buf = net_buf_alloc(&acl_tx_pool, K_FOREVER); - ASSERT(buf, "failed ACL allocation"); + TEST_ASSERT(buf, "failed ACL allocation"); reserve = sizeof(struct bt_l2cap_hdr); reserve += sizeof(struct bt_hci_acl_hdr) + BT_BUF_RESERVE; @@ -494,8 +494,7 @@ static void send_l2cap_packet(struct net_buf *buf, uint16_t cid) hdr->cid = sys_cpu_to_le16(cid); /* Always entire packets, no HCI fragmentation */ - ASSERT(buf->len <= CONFIG_BT_BUF_ACL_TX_SIZE, - "Fragmentation not supported"); + TEST_ASSERT(buf->len <= CONFIG_BT_BUF_ACL_TX_SIZE, "Fragmentation not supported"); send_acl(buf); } @@ -546,7 +545,7 @@ static void init_tinyhost(void) void test_procedure_0(void) { - ASSERT(backchannel_init() == 0, "Failed to open backchannel\n"); + TEST_ASSERT(bk_sync_init() == 0, "Failed to open backchannel"); init_tinyhost(); @@ -565,7 +564,7 @@ void test_procedure_0(void) } /* Wait until DUT starts sleeping */ - backchannel_sync_wait(); + bk_sync_wait(); /* Send some more, so DUT has some more data to process before having to * handle the disconnect. @@ -583,30 +582,13 @@ void test_procedure_0(void) hwll_disconnect_phy(); /* Pass has to be before the exit() for process to not error out */ - PASS("Tester exit\n"); + TEST_PASS("Tester exit"); bs_trace_silent_exit(0); } -void test_tick(bs_time_t HW_device_time) -{ - bs_trace_debug_time(0, "Simulation ends now.\n"); - if (bst_result != Passed) { - bst_result = Failed; - bs_trace_error("Test did not pass before simulation ended.\n"); - } -} - -void test_init(void) -{ - bst_ticker_set_next_tick_absolute(TEST_TIMEOUT_SIMULATED); - bst_result = In_progress; -} - static const struct bst_test_instance test_to_add[] = { { .test_id = "tester", - .test_pre_init_f = test_init, - .test_tick_f = test_tick, .test_main_f = test_procedure_0, }, BSTEST_END_MARKER, diff --git a/tests/bsim/bluetooth/host/misc/hfc/CMakeLists.txt b/tests/bsim/bluetooth/host/misc/hfc/CMakeLists.txt index 3a65df765124..411978a885b0 100644 --- a/tests/bsim/bluetooth/host/misc/hfc/CMakeLists.txt +++ b/tests/bsim/bluetooth/host/misc/hfc/CMakeLists.txt @@ -6,6 +6,7 @@ find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) project(bsim_test_hfc) +add_subdirectory(${ZEPHYR_BASE}/tests/bsim/babblekit babblekit) add_subdirectory(${ZEPHYR_BASE}/tests/bluetooth/common/testlib testlib) target_sources(app PRIVATE @@ -18,4 +19,5 @@ zephyr_include_directories( target_link_libraries(app PRIVATE testlib + babblekit ) diff --git a/tests/bsim/bluetooth/host/misc/hfc/src/main.c b/tests/bsim/bluetooth/host/misc/hfc/src/main.c index 7c70511c32f0..1cb5f32091b4 100644 --- a/tests/bsim/bluetooth/host/misc/hfc/src/main.c +++ b/tests/bsim/bluetooth/host/misc/hfc/src/main.c @@ -19,14 +19,14 @@ #include "testlib/att_read.h" #include /* For get_device_nbr() */ -#include "utils.h" -#include "bstests.h" +#include "babblekit/testcase.h" +#include "babblekit/flags.h" #include LOG_MODULE_REGISTER(main, LOG_LEVEL_INF); -DEFINE_FLAG(is_connected); -DEFINE_FLAG(is_subscribed); +DEFINE_FLAG_STATIC(is_connected); +DEFINE_FLAG_STATIC(is_subscribed); /* Default connection */ static struct bt_conn *dconn; @@ -47,7 +47,7 @@ static void connected(struct bt_conn *conn, uint8_t conn_err) bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr)); if (conn_err) { - FAIL("Failed to connect to %s (%u)", addr, conn_err); + TEST_FAIL("Failed to connect to %s (%u)", addr, conn_err); return; } @@ -90,7 +90,7 @@ static void device_found(const bt_addr_le_t *addr, int8_t rssi, uint8_t type, err = bt_le_scan_stop(); if (err) { - FAIL("Stop LE scan failed (err %d)", err); + TEST_FAIL("Stop LE scan failed (err %d)", err); return; } @@ -101,7 +101,7 @@ static void device_found(const bt_addr_le_t *addr, int8_t rssi, uint8_t type, err = bt_conn_le_create(addr, BT_CONN_LE_CREATE_CONN, param, &conn); if (err) { k_oops(); - FAIL("Create conn failed (err %d)", err); + TEST_FAIL("Create conn failed (err %d)", err); return; } } @@ -114,7 +114,7 @@ static struct bt_conn *connect_as_peripheral(void) UNSET_FLAG(is_connected); err = bt_le_adv_start(BT_LE_ADV_CONN_FAST_1, NULL, 0, NULL, 0); - ASSERT(!err, "Adving failed to start (err %d)\n", err); + TEST_ASSERT(!err, "Adving failed to start (err %d)", err); LOG_DBG("advertising"); WAIT_FOR_FLAG(is_connected); @@ -140,7 +140,7 @@ static struct bt_conn *connect_as_central(void) UNSET_FLAG(is_connected); err = bt_le_scan_start(&scan_param, device_found); - ASSERT(!err, "Scanning failed to start (err %d)\n", err); + TEST_ASSERT(!err, "Scanning failed to start (err %d)", err); LOG_DBG("Central initiating connection..."); WAIT_FOR_FLAG(is_connected); @@ -163,14 +163,14 @@ static void find_the_chrc(struct bt_conn *conn, int err; err = bt_testlib_gatt_discover_primary(&svc_handle, &svc_end_handle, conn, svc, 1, 0xffff); - ASSERT(!err, "Failed to discover service %d"); + TEST_ASSERT(!err, "Failed to discover service %d"); LOG_DBG("svc_handle: %u, svc_end_handle: %u", svc_handle, svc_end_handle); err = bt_testlib_gatt_discover_characteristic(chrc_value_handle, &chrc_end_handle, NULL, conn, chrc, (svc_handle + 1), svc_end_handle); - ASSERT(!err, "Failed to get value handle %d"); + TEST_ASSERT(!err, "Failed to get value handle %d"); LOG_DBG("chrc_value_handle: %u, chrc_end_handle: %u", *chrc_value_handle, chrc_end_handle); } @@ -199,9 +199,9 @@ static void subscribed(struct bt_conn *conn, uint8_t err, struct bt_gatt_subscribe_params *params) { - ASSERT(!err, "Subscribe failed (err %d)\n", err); + TEST_ASSERT(!err, "Subscribe failed (err %d)", err); - ASSERT(params, "params is NULL\n"); + TEST_ASSERT(params, "params is NULL"); SET_FLAG(is_subscribed); @@ -222,7 +222,7 @@ static void subscribe(struct bt_conn *conn, params.ccc_handle = handle + 1; err = bt_gatt_subscribe(conn, ¶ms); - ASSERT(!err, "Subscribe failed (err %d)\n", err); + TEST_ASSERT(!err, "Subscribe failed (err %d)", err); WAIT_FOR_FLAG(is_subscribed); } @@ -269,7 +269,7 @@ static bool is_disconnected(struct bt_conn *conn) struct bt_conn_info info; err = bt_conn_get_info(conn, &info); - ASSERT(err == 0, "Failed to get info for %p\n", conn); + TEST_ASSERT(err == 0, "Failed to get info for %p", conn); /* Return if fully disconnected */ return info.state == BT_CONN_STATE_DISCONNECTED; @@ -322,7 +322,7 @@ static void entrypoint_dut(void) s->rx = 0; err = bt_enable(NULL); - ASSERT(err == 0, "Can't enable Bluetooth (err %d)\n", err); + TEST_ASSERT(err == 0, "Can't enable Bluetooth (err %d)", err); LOG_DBG("Central: Bluetooth initialized."); s->conn = connect_and_subscribe(); @@ -350,7 +350,7 @@ static void entrypoint_dut(void) /* linux will "unref" the conn :p */ disconnect(s->conn); - PASS("DUT done\n"); + TEST_PASS("DUT done"); } static void entrypoint_peer(void) @@ -364,7 +364,7 @@ static void entrypoint_peer(void) LOG_DBG("Test start: peer 0"); err = bt_enable(NULL); - ASSERT(err == 0, "Can't enable Bluetooth (err %d)\n", err); + TEST_ASSERT(err == 0, "Can't enable Bluetooth (err %d)", err); LOG_DBG("Bluetooth initialized."); /* prepare data for notifications */ @@ -372,7 +372,7 @@ static void entrypoint_peer(void) memset(data, 0xfe, sizeof(data)); /* Pass unless something else errors out later */ - PASS("peer 0 done\n"); + TEST_PASS("peer 0 done"); tx = 0; while (true) { @@ -396,38 +396,19 @@ static void entrypoint_peer(void) LOG_INF("disconnect"); err = disconnect(conn); - ASSERT(!err, "Failed to initate disconnect (err %d)", err); + TEST_ASSERT(!err, "Failed to initate disconnect (err %d)", err); bt_conn_unref(conn); conn = NULL; } } -static void test_tick(bs_time_t HW_device_time) -{ - bs_trace_debug_time(0, "Simulation ends now.\n"); - if (bst_result != Passed) { - bst_result = Failed; - bs_trace_error("Test did not pass before simulation ended.\n"); - } -} - -static void test_init(void) -{ - bst_ticker_set_next_tick_absolute(TEST_TIMEOUT_SIMULATED); - bst_result = In_progress; -} - static const struct bst_test_instance test_to_add[] = { { .test_id = "dut", - .test_pre_init_f = test_init, - .test_tick_f = test_tick, .test_main_f = entrypoint_dut, }, { .test_id = "peer", - .test_pre_init_f = test_init, - .test_tick_f = test_tick, .test_main_f = entrypoint_peer, }, BSTEST_END_MARKER, diff --git a/tests/bsim/bluetooth/host/misc/hfc/src/utils.h b/tests/bsim/bluetooth/host/misc/hfc/src/utils.h deleted file mode 100644 index baf0750d267a..000000000000 --- a/tests/bsim/bluetooth/host/misc/hfc/src/utils.h +++ /dev/null @@ -1,55 +0,0 @@ -/* - * Copyright (c) 2023 Nordic Semiconductor ASA - * - * SPDX-License-Identifier: Apache-2.0 - */ - -#include "bs_tracing.h" -#include "bs_types.h" -#include "bstests.h" - -#define BS_SECONDS(dur_sec) ((bs_time_t)dur_sec * USEC_PER_SEC) -#define TEST_TIMEOUT_SIMULATED BS_SECONDS(60) - -extern enum bst_result_t bst_result; - -#define DECLARE_FLAG(flag) extern atomic_t flag -#define DEFINE_FLAG(flag) atomic_t flag = (atomic_t)false -#define SET_FLAG(flag) (void)atomic_set(&flag, (atomic_t)true) -#define UNSET_FLAG(flag) (void)atomic_set(&flag, (atomic_t)false) - -#define WAIT_FOR_VAL(var, val) \ - while (atomic_get(&var) != val) { \ - (void)k_sleep(K_MSEC(1)); \ - } -#define WAIT_FOR_FLAG(flag) \ - while (!(bool)atomic_get(&flag)) { \ - (void)k_sleep(K_MSEC(1)); \ - } -#define WAIT_FOR_FLAG_UNSET(flag) \ - while ((bool)atomic_get(&flag)) { \ - (void)k_sleep(K_MSEC(1)); \ - } -#define TAKE_FLAG(flag) \ - while (!(bool)atomic_cas(&flag, true, false)) { \ - (void)k_sleep(K_MSEC(1)); \ - } - -#define ASSERT(expr, ...) \ - do { \ - if (!(expr)) { \ - FAIL(__VA_ARGS__); \ - } \ - } while (0) - -#define FAIL(...) \ - do { \ - bst_result = Failed; \ - bs_trace_error_time_line(__VA_ARGS__); \ - } while (0) - -#define PASS(...) \ - do { \ - bst_result = Passed; \ - bs_trace_info_time(1, __VA_ARGS__); \ - } while (0) diff --git a/tests/bsim/bluetooth/host/misc/sample_test/src/dut.c b/tests/bsim/bluetooth/host/misc/sample_test/src/dut.c index 535eb31cf236..3e9b2fd60b2e 100644 --- a/tests/bsim/bluetooth/host/misc/sample_test/src/dut.c +++ b/tests/bsim/bluetooth/host/misc/sample_test/src/dut.c @@ -23,7 +23,7 @@ LOG_MODULE_REGISTER(dut, LOG_LEVEL_DBG); -static DEFINE_FLAG(is_subscribed); +DEFINE_FLAG_STATIC(is_subscribed); extern unsigned long runtime_log_level; diff --git a/tests/bsim/bluetooth/host/misc/sample_test/src/peer.c b/tests/bsim/bluetooth/host/misc/sample_test/src/peer.c index a5a4dfd3733a..d6b79254eb5d 100644 --- a/tests/bsim/bluetooth/host/misc/sample_test/src/peer.c +++ b/tests/bsim/bluetooth/host/misc/sample_test/src/peer.c @@ -26,9 +26,9 @@ LOG_MODULE_REGISTER(peer, LOG_LEVEL_DBG); -static DEFINE_FLAG(is_subscribed); -static DEFINE_FLAG(got_notification_1); -static DEFINE_FLAG(got_notification_2); +DEFINE_FLAG_STATIC(is_subscribed); +DEFINE_FLAG_STATIC(got_notification_1); +DEFINE_FLAG_STATIC(got_notification_2); extern unsigned long runtime_log_level; diff --git a/tests/bsim/bluetooth/host/misc/unregister_conn_cb/src/main.c b/tests/bsim/bluetooth/host/misc/unregister_conn_cb/src/main.c index a2c6cd87ce4e..b1a1042d81e8 100644 --- a/tests/bsim/bluetooth/host/misc/unregister_conn_cb/src/main.c +++ b/tests/bsim/bluetooth/host/misc/unregister_conn_cb/src/main.c @@ -19,7 +19,7 @@ #include "babblekit/testcase.h" #include "babblekit/flags.h" -static DEFINE_FLAG(flag_is_connected); +DEFINE_FLAG_STATIC(flag_is_connected); static struct bt_conn *g_conn; diff --git a/tests/bsim/bluetooth/host/privacy/central/CMakeLists.txt b/tests/bsim/bluetooth/host/privacy/central/CMakeLists.txt index 7ec3436c8f84..b4c38e404209 100644 --- a/tests/bsim/bluetooth/host/privacy/central/CMakeLists.txt +++ b/tests/bsim/bluetooth/host/privacy/central/CMakeLists.txt @@ -12,7 +12,6 @@ add_subdirectory(${ZEPHYR_BASE}/tests/bsim/babblekit babblekit) target_link_libraries(app PRIVATE babblekit) target_sources(app PRIVATE - src/bs_bt_utils.c src/tester.c src/main.c src/dut.c diff --git a/tests/bsim/bluetooth/host/privacy/central/src/bs_bt_utils.c b/tests/bsim/bluetooth/host/privacy/central/src/bs_bt_utils.c deleted file mode 100644 index 6011b5d300cd..000000000000 --- a/tests/bsim/bluetooth/host/privacy/central/src/bs_bt_utils.c +++ /dev/null @@ -1,75 +0,0 @@ -/* - * Copyright (c) 2023 Nordic Semiconductor ASA - * - * SPDX-License-Identifier: Apache-2.0 - */ - -#include "bs_bt_utils.h" -#include "bs_pc_backchannel.h" -#include "argparse.h" - -#define BS_SECONDS(dur_sec) ((bs_time_t)dur_sec * 1000000) -#define TEST_TIMEOUT_SIMULATED BS_SECONDS(60) -#define CHANNEL_ID 0 -#define MSG_SIZE 1 - -void test_tick(bs_time_t HW_device_time) -{ - bs_trace_debug_time(0, "Simulation ends now.\n"); - if (bst_result != Passed) { - bst_result = Failed; - bs_trace_error("Test did not pass before simulation ended.\n"); - } -} - -void test_init(void) -{ - bst_ticker_set_next_tick_absolute(TEST_TIMEOUT_SIMULATED); - bst_result = In_progress; -} - -void backchannel_init(uint peer) -{ - uint device_number = get_device_nbr(); - uint device_numbers[] = {peer}; - uint channel_numbers[] = {CHANNEL_ID}; - uint *ch; - - ch = bs_open_back_channel(device_number, device_numbers, channel_numbers, - ARRAY_SIZE(channel_numbers)); - if (!ch) { - FAIL("Unable to open backchannel\n"); - } -} - -void backchannel_sync_send(void) -{ - uint8_t sync_msg[MSG_SIZE] = {get_device_nbr()}; - - bs_bc_send_msg(CHANNEL_ID, sync_msg, ARRAY_SIZE(sync_msg)); -} - -void backchannel_sync_wait(void) -{ - uint8_t sync_msg[MSG_SIZE]; - - while (true) { - if (bs_bc_is_msg_received(CHANNEL_ID) > 0) { - bs_bc_receive_msg(CHANNEL_ID, sync_msg, ARRAY_SIZE(sync_msg)); - if (sync_msg[0] != get_device_nbr()) { - /* Received a message from another device, exit */ - break; - } - } - - k_sleep(K_MSEC(1)); - } -} - -void print_address(bt_addr_le_t *addr) -{ - char array[BT_ADDR_LE_STR_LEN]; - - bt_addr_le_to_str(addr, array, sizeof(array)); - printk("Address : %s\n", array); -} diff --git a/tests/bsim/bluetooth/host/privacy/central/src/bs_bt_utils.h b/tests/bsim/bluetooth/host/privacy/central/src/bs_bt_utils.h deleted file mode 100644 index 84fd44993bce..000000000000 --- a/tests/bsim/bluetooth/host/privacy/central/src/bs_bt_utils.h +++ /dev/null @@ -1,72 +0,0 @@ -/** - * Common functions and helpers for BSIM ADV tests - * - * Copyright (c) 2023 Nordic Semiconductor ASA - * - * SPDX-License-Identifier: Apache-2.0 - */ - -#include "bs_tracing.h" -#include "bs_types.h" -#include "bstests.h" -#include "time_machine.h" - -#include - -#include -#include -#include -#include -#include -#include -#include -#include - -extern enum bst_result_t bst_result; - -#define DECLARE_FLAG(flag) extern atomic_t flag -#define DEFINE_FLAG(flag) atomic_t flag = (atomic_t) false -#define SET_FLAG(flag) (void)atomic_set(&flag, (atomic_t) true) -#define UNSET_FLAG(flag) (void)atomic_set(&flag, (atomic_t) false) -#define WAIT_FOR_FLAG(flag) \ - while (!(bool)atomic_get(&flag)) { \ - (void)k_sleep(K_MSEC(1)); \ - } -#define WAIT_FOR_FLAG_UNSET(flag) \ - while ((bool)atomic_get(&flag)) { \ - (void)k_sleep(K_MSEC(1)); \ - } -#define TAKE_FLAG(flag) \ - while (!(bool)atomic_cas(&flag, true, false)) { \ - (void)k_sleep(K_MSEC(1)); \ - } - -#define ASSERT(expr, ...) \ - do { \ - if (!(expr)) { \ - FAIL(__VA_ARGS__); \ - } \ - } while (0) - -#define FAIL(...) \ - do { \ - bst_result = Failed; \ - bs_trace_error_time_line(__VA_ARGS__); \ - } while (0) - -#define PASS(...) \ - do { \ - bst_result = Passed; \ - bs_trace_info_time(1, __VA_ARGS__); \ - } while (0) - -#define DUT_CENTRAL_ID 0 -#define TESTER_PERIPHERAL_ID 1 - -void test_tick(bs_time_t HW_device_time); -void test_init(void); -void backchannel_init(uint peer); -void backchannel_sync_send(void); -void backchannel_sync_wait(void); - -void print_address(bt_addr_le_t *addr); diff --git a/tests/bsim/bluetooth/host/privacy/central/src/dut.c b/tests/bsim/bluetooth/host/privacy/central/src/dut.c index 5a49b6e5097b..669217f4d757 100644 --- a/tests/bsim/bluetooth/host/privacy/central/src/dut.c +++ b/tests/bsim/bluetooth/host/privacy/central/src/dut.c @@ -4,8 +4,6 @@ * SPDX-License-Identifier: Apache-2.0 */ -#include "bs_bt_utils.h" - #include #include @@ -13,6 +11,7 @@ #include #include +#include #include #include @@ -24,7 +23,7 @@ void start_scanning(void) /* Enable bluetooth */ err = bt_enable(NULL); if (err) { - FAIL("Failed to enable bluetooth (err %d\n)", err); + TEST_FAIL("Failed to enable bluetooth (err %d)", err); } /* Start active scanning */ @@ -38,7 +37,7 @@ void start_scanning(void) err = bt_le_scan_start(¶m, NULL); if (err) { - FAIL("Failed to start scanning"); + TEST_FAIL("Failed to start scanning"); } } @@ -48,12 +47,12 @@ void dut_procedure(void) /* Nothing to do */ - PASS("PASS\n"); + TEST_PASS("PASS"); } void dut_procedure_connect_short_rpa_timeout(void) { - backchannel_init(1); + TEST_ASSERT(bk_sync_init() == 0); const uint16_t rpa_timeout_s = 1; @@ -73,13 +72,13 @@ void dut_procedure_connect_short_rpa_timeout(void) TEST_ASSERT(!err, "Failed to start scan (err %d)", err); /* Indicate to the peer device that we have found the advertiser. */ - backchannel_sync_send(); + bk_sync_send(); /* Create a connection using that address */ err = bt_testlib_connect(&peer, &conn); TEST_ASSERT(!err, "Failed to initiate connection (err %d)", err); - PASS("PASS\n"); + TEST_PASS("PASS"); } void dut_procedure_connect_timeout(void) @@ -114,5 +113,5 @@ void dut_procedure_connect_timeout(void) TEST_ASSERT(diff_to_expected_ms < 0.1 * expected_conn_timeout_ms, "Connection timeout not within 10 \%% of expected timeout"); - PASS("PASS\n"); + TEST_PASS("PASS"); } diff --git a/tests/bsim/bluetooth/host/privacy/central/src/main.c b/tests/bsim/bluetooth/host/privacy/central/src/main.c index 0aa42d16c253..2e6f560c0d3c 100644 --- a/tests/bsim/bluetooth/host/privacy/central/src/main.c +++ b/tests/bsim/bluetooth/host/privacy/central/src/main.c @@ -4,7 +4,6 @@ * SPDX-License-Identifier: Apache-2.0 */ -#include "bs_bt_utils.h" #include "bstests.h" void tester_procedure(void); @@ -17,31 +16,23 @@ static const struct bst_test_instance test_to_add[] = { { .test_id = "central", .test_descr = "Central performs active scanning using RPA", - .test_pre_init_f = test_init, - .test_tick_f = test_tick, .test_main_f = dut_procedure, }, { .test_id = "central_connect_short_rpa_timeout", .test_descr = "Central connects to a peripheral using a short RPA timeout", - .test_pre_init_f = test_init, - .test_tick_f = test_tick, .test_main_f = dut_procedure_connect_short_rpa_timeout, }, { .test_id = "central_connect_fails_with_short_rpa_timeout", .test_descr = "Central connects to a peripheral using a short RPA timeout" " but expects connection establishment to time out.", - .test_pre_init_f = test_init, - .test_tick_f = test_tick, .test_main_f = dut_procedure_connect_timeout, }, { .test_id = "peripheral", .test_descr = "Performs scannable advertising, validates that the scanner" " RPA address refreshes", - .test_pre_init_f = test_init, - .test_tick_f = test_tick, .test_main_f = tester_procedure, }, { @@ -49,8 +40,6 @@ static const struct bst_test_instance test_to_add[] = { .test_descr = "Performs connectable advertising. " "The advertiser is stopped for 10 seconds when instructed by the DUT" " to allow it to run the initiator for longer than its RPA timeout.", - .test_post_init_f = test_init, - .test_tick_f = test_tick, .test_main_f = tester_procedure_periph_delayed_start_of_conn_adv, }, BSTEST_END_MARKER, diff --git a/tests/bsim/bluetooth/host/privacy/central/src/tester.c b/tests/bsim/bluetooth/host/privacy/central/src/tester.c index 119802c4a0d2..adf3e67d7bdb 100644 --- a/tests/bsim/bluetooth/host/privacy/central/src/tester.c +++ b/tests/bsim/bluetooth/host/privacy/central/src/tester.c @@ -4,8 +4,6 @@ * SPDX-License-Identifier: Apache-2.0 */ -#include "bs_bt_utils.h" - #include #include @@ -14,10 +12,12 @@ #include #include -#include +#include "babblekit/testcase.h" +#include "babblekit/flags.h" +#include "babblekit/sync.h" -DEFINE_FLAG(flag_new_address); -DEFINE_FLAG(flag_connected); +DEFINE_FLAG_STATIC(flag_new_address); +DEFINE_FLAG_STATIC(flag_connected); void scanned_cb(struct bt_le_ext_adv *adv, struct bt_le_ext_adv_scanned_info *info) { @@ -41,9 +41,10 @@ void scanned_cb(struct bt_le_ext_adv *adv, struct bt_le_ext_adv_scanned_info *in /* Check if the scan request comes from a new address */ if (bt_addr_le_cmp(&old_addr, &new_addr)) { int64_t new_time, diff, time_diff_ms, rpa_timeout_ms; + char addr_str[BT_ADDR_LE_STR_LEN]; - printk("Scanned request from new "); - print_address(info->addr); + bt_addr_le_to_str(info->addr, addr_str, sizeof(addr_str)); + printk("Scanned request from new address : %s\n", addr_str); /* Ensure the RPA rotation occurs within +-10% of CONFIG_BT_RPA_TIMEOUT */ new_time = k_uptime_get(); @@ -57,7 +58,8 @@ void scanned_cb(struct bt_le_ext_adv *adv, struct bt_le_ext_adv_scanned_info *in } if (diff > rpa_timeout_ms * 0.10) { - FAIL("RPA rotation did not occur within +-10%% of CONFIG_BT_RPA_TIMEOUT"); + TEST_FAIL("RPA rotation did not occur within +-10%% of " + "CONFIG_BT_RPA_TIMEOUT"); } old_time = new_time; @@ -89,7 +91,7 @@ void start_advertising(void) /* Enable bluetooth */ err = bt_enable(NULL); if (err) { - FAIL("Failed to enable bluetooth (err %d\n)", err); + TEST_FAIL("Failed to enable bluetooth (err %d)", err); } /* Create advertising set */ @@ -104,13 +106,13 @@ void start_advertising(void) err = bt_le_ext_adv_create(¶ms, &adv_callbacks, &adv); if (err) { - FAIL("Failed to create advertising set (err %d)\n", err); + TEST_FAIL("Failed to create advertising set (err %d)", err); } /* Set scan data */ err = bt_le_ext_adv_set_data(adv, NULL, 0, sd, ARRAY_SIZE(sd)); if (err) { - FAIL("Failed to set advertising data (err %d)", err); + TEST_FAIL("Failed to set advertising data (err %d)", err); } /* Start advertising */ @@ -119,7 +121,7 @@ void start_advertising(void) err = bt_le_ext_adv_start(adv, &start_params); if (err) { - FAIL("Failed to start advertising (err %d)\n", err); + TEST_FAIL("Failed to start advertising (err %d)", err); } } @@ -132,12 +134,12 @@ void tester_procedure(void) UNSET_FLAG(flag_new_address); } - PASS("PASS\n"); + TEST_PASS("PASS"); } void tester_procedure_periph_delayed_start_of_conn_adv(void) { - backchannel_init(0); + TEST_ASSERT(bk_sync_init() == 0); int err; struct bt_le_adv_param params = @@ -167,7 +169,7 @@ void tester_procedure_periph_delayed_start_of_conn_adv(void) err = bt_le_ext_adv_start(adv, BT_LE_EXT_ADV_START_DEFAULT); TEST_ASSERT(!err, "Failed to start advertiser (err %d)", err); - backchannel_sync_wait(); + bk_sync_wait(); err = bt_le_ext_adv_stop(adv); TEST_ASSERT(!err, "Failed to stop advertiser (err %d)", err); @@ -182,5 +184,5 @@ void tester_procedure_periph_delayed_start_of_conn_adv(void) WAIT_FOR_FLAG(flag_connected); - PASS("PASS\n"); + TEST_PASS("PASS"); } diff --git a/tests/bsim/bluetooth/host/privacy/device/CMakeLists.txt b/tests/bsim/bluetooth/host/privacy/device/CMakeLists.txt index 695ed8b17c08..e4fe1e4c90ed 100644 --- a/tests/bsim/bluetooth/host/privacy/device/CMakeLists.txt +++ b/tests/bsim/bluetooth/host/privacy/device/CMakeLists.txt @@ -5,6 +5,9 @@ cmake_minimum_required(VERSION 3.20.0) find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) project(bsim_test_privacy) +add_subdirectory(${ZEPHYR_BASE}/tests/bsim/babblekit babblekit) +target_link_libraries(app PRIVATE babblekit) + target_sources(app PRIVATE src/test_undirected_main.c src/test_undirected_central.c diff --git a/tests/bsim/bluetooth/host/privacy/device/src/test_undirected_central.c b/tests/bsim/bluetooth/host/privacy/device/src/test_undirected_central.c index c245c2131977..80b7a94c7759 100644 --- a/tests/bsim/bluetooth/host/privacy/device/src/test_undirected_central.c +++ b/tests/bsim/bluetooth/host/privacy/device/src/test_undirected_central.c @@ -18,37 +18,13 @@ #include LOG_MODULE_DECLARE(bt_bsim_privacy, LOG_LEVEL_INF); -#include "bs_types.h" -#include "bs_tracing.h" -#include "bstests.h" +#include "babblekit/testcase.h" +#include "babblekit/flags.h" #include "bs_cmd_line.h" -#define CREATE_FLAG(flag) static atomic_t flag = (atomic_t) false -#define SET_FLAG(flag) (void)atomic_set(&flag, (atomic_t) true) -#define GET_FLAG(flag) (bool)atomic_get(&flag) -#define UNSET_FLAG(flag) (void)atomic_set(&flag, (atomic_t) false) -#define WAIT_FOR_FLAG(flag) \ - while (!(bool)atomic_get(&flag)) { \ - (void)k_sleep(K_MSEC(1)); \ - } - -#define FAIL(...) \ - do { \ - bst_result = Failed; \ - bs_trace_error_time_line(__VA_ARGS__); \ - } while (0) - -#define PASS(...) \ - do { \ - bst_result = Passed; \ - bs_trace_info_time(1, __VA_ARGS__); \ - } while (0) - -extern enum bst_result_t bst_result; - -CREATE_FLAG(paired); -CREATE_FLAG(rpa_tested); -CREATE_FLAG(identity_tested); +DEFINE_FLAG_STATIC(paired); +DEFINE_FLAG_STATIC(rpa_tested); +DEFINE_FLAG_STATIC(identity_tested); static void start_scan(void); @@ -135,20 +111,19 @@ static void check_addresses(const bt_addr_le_t *peer_addr) SET_FLAG(identity_tested); addr_equal = bt_addr_le_eq(&peer_identity, peer_addr); if (!addr_equal) { - FAIL("The peer address is not the same as the peer previously paired.\n"); + TEST_FAIL( + "The peer address is not the same as the peer previously paired."); } } else if (test_addr_type == RPA) { SET_FLAG(rpa_tested); addr_equal = bt_addr_le_eq(&peer_identity, peer_addr); if (!addr_equal) { - FAIL("The resolved address is not the same as the peer previously " - "paired.\n"); + TEST_FAIL("The resolved address is not the same as the peer previously " + "paired."); } } } - - static void scan_recv(const struct bt_le_scan_recv_info *info, struct net_buf_simple *ad) { @@ -174,23 +149,23 @@ static void scan_recv(const struct bt_le_scan_recv_info *info, * the address resolving keys, then this check should only apply after * the pairing is done. */ - if (GET_FLAG(paired) && + if (IS_FLAG_SET(paired) && info->adv_props == (BT_GAP_ADV_PROP_EXT_ADV | BT_GAP_ADV_PROP_SCANNABLE)) { LOG_DBG("skipping AUX_ADV_IND report, waiting for AUX_SCAN_REQ " "(props: 0x%x)", info->adv_props); return; } - if (GET_FLAG(paired)) { + if (IS_FLAG_SET(paired)) { check_addresses(info->addr); } - if (connection_test || !GET_FLAG(paired)) { + if (connection_test || !IS_FLAG_SET(paired)) { if (bt_le_scan_stop()) { LOG_DBG("Failed to stop scanner"); return; } - LOG_DBG("Scanner stopped: conn %d paired %d", connection_test, GET_FLAG(paired)); + LOG_DBG("Scanner stopped: conn %d paired %d", connection_test, IS_FLAG_SET(paired)); err = bt_conn_le_create(info->addr, BT_CONN_LE_CREATE_CONN, @@ -217,7 +192,7 @@ static void start_scan(void) NULL); if (err) { - FAIL("Scanning failed to start (err %d)\n", err); + TEST_FAIL("Scanning failed to start (err %d)", err); } LOG_DBG("Scanning successfully started"); @@ -284,7 +259,7 @@ void test_central(void) err = bt_enable(NULL); if (err) { - FAIL("Bluetooth init failed (err %d)\n", err); + TEST_FAIL("Bluetooth init failed (err %d)", err); } UNSET_FLAG(identity_tested); @@ -311,5 +286,5 @@ void test_central_main(void) test_central(); - PASS("passed\n"); + TEST_PASS("passed"); } diff --git a/tests/bsim/bluetooth/host/privacy/device/src/test_undirected_main.c b/tests/bsim/bluetooth/host/privacy/device/src/test_undirected_main.c index 68c7ece462a8..f6053ca4099d 100644 --- a/tests/bsim/bluetooth/host/privacy/device/src/test_undirected_main.c +++ b/tests/bsim/bluetooth/host/privacy/device/src/test_undirected_main.c @@ -12,53 +12,27 @@ #include -#include "bs_types.h" -#include "bs_tracing.h" #include "bstests.h" #include LOG_MODULE_REGISTER(bt_bsim_privacy, LOG_LEVEL_INF); -extern enum bst_result_t bst_result; - -#define WAIT_TIME_S 20 -#define WAIT_TIME (WAIT_TIME_S * 1e6) /* 20 seconds */ - extern void central_test_args_parse(int argc, char *argv[]); extern void peripheral_test_args_parse(int argc, char *argv[]); extern void test_central_main(void); extern void test_peripheral(void); -void sim_timeout(bs_time_t HW_device_time) -{ - if (bst_result != Passed) { - bst_result = Failed; - bs_trace_error_time_line("Test failed (not passed after %d seconds)\n", - WAIT_TIME_S); - } -} - -static void test_privacy_init(void) -{ - bst_ticker_set_next_tick_absolute(WAIT_TIME); - bst_result = In_progress; -} - static const struct bst_test_instance test_def[] = { { .test_id = "central", .test_descr = "Central device", - .test_pre_init_f = test_privacy_init, - .test_tick_f = sim_timeout, .test_main_f = test_central_main, .test_args_f = central_test_args_parse, }, { .test_id = "peripheral", .test_descr = "Peripheral device", - .test_pre_init_f = test_privacy_init, - .test_tick_f = sim_timeout, .test_main_f = test_peripheral, .test_args_f = peripheral_test_args_parse, }, diff --git a/tests/bsim/bluetooth/host/privacy/device/src/test_undirected_peripheral.c b/tests/bsim/bluetooth/host/privacy/device/src/test_undirected_peripheral.c index fbfc45536add..38349e02682b 100644 --- a/tests/bsim/bluetooth/host/privacy/device/src/test_undirected_peripheral.c +++ b/tests/bsim/bluetooth/host/privacy/device/src/test_undirected_peripheral.c @@ -20,38 +20,14 @@ #include LOG_MODULE_DECLARE(bt_bsim_privacy, LOG_LEVEL_INF); -#include "bs_types.h" -#include "bs_tracing.h" -#include "bstests.h" +#include "babblekit/testcase.h" +#include "babblekit/flags.h" #include "bs_cmd_line.h" -#define FAIL(...) \ - do { \ - bst_result = Failed; \ - bs_trace_error_time_line(__VA_ARGS__); \ - } while (0) - -#define PASS(...) \ - do { \ - bst_result = Passed; \ - bs_trace_info_time(1, __VA_ARGS__); \ - } while (0) - -extern enum bst_result_t bst_result; - -#define CREATE_FLAG(flag) static atomic_t flag = (atomic_t) false -#define SET_FLAG(flag) (void)atomic_set(&flag, (atomic_t) true) -#define GET_FLAG(flag) (bool)atomic_get(&flag) -#define UNSET_FLAG(flag) (void)atomic_set(&flag, (atomic_t) false) -#define WAIT_FOR_FLAG(flag) \ - while (!(bool)atomic_get(&flag)) { \ - (void)k_sleep(K_MSEC(1)); \ - } - -CREATE_FLAG(paired_flag); -CREATE_FLAG(connected_flag); -CREATE_FLAG(wait_disconnection); -CREATE_FLAG(wait_scanned); +DEFINE_FLAG_STATIC(paired_flag); +DEFINE_FLAG_STATIC(connected_flag); +DEFINE_FLAG_STATIC(wait_disconnection); +DEFINE_FLAG_STATIC(wait_scanned); static struct bt_conn *default_conn; @@ -288,7 +264,7 @@ static void disconnect(void) err = bt_conn_disconnect(default_conn, BT_HCI_ERR_REMOTE_USER_TERM_CONN); if (err) { - FAIL("Disconnection failed (err %d)\n", err); + TEST_FAIL("Disconnection failed (err %d)", err); } WAIT_FOR_FLAG(wait_disconnection); @@ -318,9 +294,9 @@ static void connected(struct bt_conn *conn, uint8_t err) default_conn = bt_conn_ref(conn); - if (!GET_FLAG(paired_flag)) { + if (!IS_FLAG_SET(paired_flag)) { if (bt_conn_set_security(conn, BT_SECURITY_L2)) { - FAIL("Failed to set security\n"); + TEST_FAIL("Failed to set security"); } } else { SET_FLAG(connected_flag); @@ -406,7 +382,7 @@ static void test_peripheral_main(void) err = bt_enable(NULL); if (err) { - FAIL("Bluetooth init failed (err %d)\n", err); + TEST_FAIL("Bluetooth init failed (err %d)", err); } LOG_DBG("Bluetooth initialized"); @@ -460,5 +436,5 @@ void test_peripheral(void) test_peripheral_main(); - PASS("passed\n"); + TEST_PASS("passed"); } diff --git a/tests/bsim/bluetooth/host/privacy/device/test_scripts/run_tests.sh b/tests/bsim/bluetooth/host/privacy/device/test_scripts/run_tests.sh index 87b97841b157..171957b7163b 100755 --- a/tests/bsim/bluetooth/host/privacy/device/test_scripts/run_tests.sh +++ b/tests/bsim/bluetooth/host/privacy/device/test_scripts/run_tests.sh @@ -59,11 +59,10 @@ for args in ${TEST_ARGS[@]}; do use-ext-adv=${use_ext_adv} scannable=${scannable} connectable=${connectable} Execute ./bs_2G4_phy_v1 -v=${verbosity_level} -s="${simulation_id}_${sim_id_count}" \ - -D=2 -sim_length=60e6 $@ + -D=2 -sim_length=20e6 $@ wait_for_background_jobs sim_id_count=$(( sim_id_count + 1 )) done done - diff --git a/tests/bsim/bluetooth/host/privacy/legacy/CMakeLists.txt b/tests/bsim/bluetooth/host/privacy/legacy/CMakeLists.txt index 0f313743af07..b080f45fbe46 100644 --- a/tests/bsim/bluetooth/host/privacy/legacy/CMakeLists.txt +++ b/tests/bsim/bluetooth/host/privacy/legacy/CMakeLists.txt @@ -5,8 +5,14 @@ cmake_minimum_required(VERSION 3.20.0) find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) project(bsim_test_privacy_legacy) +add_subdirectory(${ZEPHYR_BASE}/tests/bsim/babblekit babblekit) +add_subdirectory(${ZEPHYR_BASE}/tests/bluetooth/common/testlib testlib) +target_link_libraries(app PRIVATE + babblekit + testlib +) + target_sources(app PRIVATE - src/bs_bt_utils.c src/tester.c src/main.c src/dut.c diff --git a/tests/bsim/bluetooth/host/privacy/legacy/src/bs_bt_utils.c b/tests/bsim/bluetooth/host/privacy/legacy/src/bs_bt_utils.c deleted file mode 100644 index b367c0dfb296..000000000000 --- a/tests/bsim/bluetooth/host/privacy/legacy/src/bs_bt_utils.c +++ /dev/null @@ -1,73 +0,0 @@ -/* - * Copyright (c) 2023 Nordic Semiconductor ASA - * - * SPDX-License-Identifier: Apache-2.0 - */ - -#include "bs_bt_utils.h" -#include "argparse.h" -#include "bs_pc_backchannel.h" - -#define BS_SECONDS(dur_sec) ((bs_time_t)dur_sec * 1000000) -#define TEST_TIMEOUT_SIMULATED BS_SECONDS(70) - -void test_tick(bs_time_t HW_device_time) -{ - bs_trace_debug_time(0, "Simulation ends now.\n"); - if (bst_result != Passed) { - bst_result = Failed; - - bs_trace_error("Test did not pass before simulation ended.\n"); - } -} - -void test_init(void) -{ - bst_ticker_set_next_tick_absolute(TEST_TIMEOUT_SIMULATED); - bst_result = In_progress; -} - -#define CHANNEL_ID 0 -#define MSG_SIZE 1 - -void backchannel_init(uint peer) -{ - uint device_number = get_device_nbr(); - uint device_numbers[] = { peer }; - uint channel_numbers[] = { CHANNEL_ID }; - uint *ch; - - ch = bs_open_back_channel(device_number, device_numbers, - channel_numbers, ARRAY_SIZE(channel_numbers)); - if (!ch) { - FAIL("Unable to open backchannel\n"); - } -} - -void backchannel_sync_send(void) -{ - uint8_t sync_msg[MSG_SIZE] = { get_device_nbr() }; - - printk("Sending sync\n"); - bs_bc_send_msg(CHANNEL_ID, sync_msg, ARRAY_SIZE(sync_msg)); -} - -void backchannel_sync_wait(void) -{ - uint8_t sync_msg[MSG_SIZE]; - - while (true) { - if (bs_bc_is_msg_received(CHANNEL_ID) > 0) { - bs_bc_receive_msg(CHANNEL_ID, sync_msg, - ARRAY_SIZE(sync_msg)); - if (sync_msg[0] != get_device_nbr()) { - /* Received a message from another device, exit */ - break; - } - } - - k_sleep(K_MSEC(1)); - } - - printk("Sync received\n"); -} diff --git a/tests/bsim/bluetooth/host/privacy/legacy/src/bs_bt_utils.h b/tests/bsim/bluetooth/host/privacy/legacy/src/bs_bt_utils.h deleted file mode 100644 index 825804728812..000000000000 --- a/tests/bsim/bluetooth/host/privacy/legacy/src/bs_bt_utils.h +++ /dev/null @@ -1,54 +0,0 @@ -/** - * Common functions and helpers for BSIM ADV tests - * - * Copyright (c) 2023 Nordic Semiconductor ASA - * - * SPDX-License-Identifier: Apache-2.0 - */ - -#include "bs_tracing.h" -#include "bs_types.h" -#include "bstests.h" -#include "time_machine.h" - -#include - -#include -#include -#include -#include -#include -#include -#include -#include - -extern enum bst_result_t bst_result; - -#define ASSERT(expr, ...) \ - do { \ - if (!(expr)) { \ - FAIL(__VA_ARGS__); \ - } \ - } while (0) - -#define FAIL(...) \ - do { \ - bst_result = Failed; \ - bs_trace_error_time_line(__VA_ARGS__); \ - } while (0) - -#define PASS(...) \ - do { \ - bst_result = Passed; \ - bs_trace_info_time(1, __VA_ARGS__); \ - } while (0) - -#define CENTRAL_SIM_ID 0 -#define PERIPHERAL_SIM_ID 1 - -void test_tick(bs_time_t HW_device_time); -void test_init(void); - -void backchannel_init(uint peer); -void backchannel_sync_send(void); -void backchannel_sync_wait(void); diff --git a/tests/bsim/bluetooth/host/privacy/legacy/src/dut.c b/tests/bsim/bluetooth/host/privacy/legacy/src/dut.c index f9530d46ac0b..71d328d03b78 100644 --- a/tests/bsim/bluetooth/host/privacy/legacy/src/dut.c +++ b/tests/bsim/bluetooth/host/privacy/legacy/src/dut.c @@ -4,14 +4,23 @@ * SPDX-License-Identifier: Apache-2.0 */ -#include "bs_bt_utils.h" - #include #include +#include + +#include #include #include #include +#include +#include +#include +#include +#include + +#include "babblekit/testcase.h" +#include "babblekit/sync.h" #include LOG_MODULE_REGISTER(dut, 4); @@ -34,11 +43,11 @@ static void set_public_addr(void) err = bt_id_create(&dut_addr, irk); if (err) { - FAIL("Failed to override addr %d\n", err); + TEST_FAIL("Failed to override addr %d", err); } } -void start_advertising(uint32_t options) +static void start_advertising(uint32_t options) { int err; @@ -48,11 +57,11 @@ void start_advertising(uint32_t options) err = bt_le_adv_start(¶m, ad, ARRAY_SIZE(ad), NULL, 0); if (err) { - FAIL("Failed to start advertising (err %d)\n", err); + TEST_FAIL("Failed to start advertising (err %d)", err); } } -void generate_new_rpa(void) +static void generate_new_rpa(void) { /* This will generate a new RPA and mark it valid */ struct bt_le_oob oob_local = { 0 }; @@ -65,7 +74,7 @@ void dut_procedure(void) int err; /* open a backchannel to the peer */ - backchannel_init(CENTRAL_SIM_ID); + TEST_ASSERT(bk_sync_init() == 0); /* override public address so the scanner can test if we're using it or not */ set_public_addr(); @@ -73,7 +82,7 @@ void dut_procedure(void) LOG_DBG("enable bt"); err = bt_enable(NULL); if (err) { - FAIL("Failed to enable bluetooth (err %d\n)", err); + TEST_FAIL("Failed to enable bluetooth (err %d)", err); } LOG_DBG("generate new RPA"); @@ -84,16 +93,16 @@ void dut_procedure(void) /* wait for the tester to validate we're using our identity address */ LOG_DBG("wait for validation by tester"); - backchannel_sync_wait(); + bk_sync_wait(); LOG_DBG("wait for validation by tester"); err = bt_le_adv_stop(); if (err) { - FAIL("Failed to stop advertising (err %d\n)", err); + TEST_FAIL("Failed to stop advertising (err %d)", err); } LOG_DBG("start adv with RPA"); start_advertising(BT_LE_ADV_OPT_CONN); /* Test pass verdict is decided by the tester */ - PASS("DUT done\n"); + TEST_PASS("DUT done"); } diff --git a/tests/bsim/bluetooth/host/privacy/legacy/src/main.c b/tests/bsim/bluetooth/host/privacy/legacy/src/main.c index edcd8e81bfd0..1f976a3b0751 100644 --- a/tests/bsim/bluetooth/host/privacy/legacy/src/main.c +++ b/tests/bsim/bluetooth/host/privacy/legacy/src/main.c @@ -4,7 +4,6 @@ * SPDX-License-Identifier: Apache-2.0 */ -#include "bs_bt_utils.h" #include "bstests.h" void tester_procedure(void); @@ -13,14 +12,10 @@ void dut_procedure(void); static const struct bst_test_instance test_to_add[] = { { .test_id = "central", - .test_pre_init_f = test_init, - .test_tick_f = test_tick, .test_main_f = tester_procedure, }, { .test_id = "peripheral", - .test_pre_init_f = test_init, - .test_tick_f = test_tick, .test_main_f = dut_procedure, }, BSTEST_END_MARKER, diff --git a/tests/bsim/bluetooth/host/privacy/legacy/src/tester.c b/tests/bsim/bluetooth/host/privacy/legacy/src/tester.c index eb2e1a551576..160f0f90e5cb 100644 --- a/tests/bsim/bluetooth/host/privacy/legacy/src/tester.c +++ b/tests/bsim/bluetooth/host/privacy/legacy/src/tester.c @@ -4,11 +4,22 @@ * SPDX-License-Identifier: Apache-2.0 */ -#include "bs_bt_utils.h" -#include -#include #include +#include + +#include +#include #include +#include +#include +#include +#include +#include +#include + +#include "babblekit/testcase.h" +#include "babblekit/sync.h" +#include "testlib/addr.h" #include "common/bt_str.h" @@ -27,21 +38,13 @@ struct test_data_t { extern bt_addr_le_t dut_addr; -void print_address(const bt_addr_le_t *addr) -{ - char array[BT_ADDR_LE_STR_LEN]; - - bt_addr_le_to_str(addr, array, sizeof(array)); - LOG_DBG("Address : %s", array); -} - static void cb_expect_rpa(const bt_addr_le_t *addr, int8_t rssi, uint8_t type, struct net_buf_simple *ad) { int64_t diff_ms, rpa_timeout_ms; if (bt_addr_le_eq(addr, &dut_addr)) { - FAIL("DUT used identity addr instead of RPA\n"); + TEST_FAIL("DUT used identity addr instead of RPA"); } /* Only save the address + time if this is the first scan */ @@ -56,10 +59,8 @@ static void cb_expect_rpa(const bt_addr_le_t *addr, int8_t rssi, uint8_t type, return; } test_data.addr_set = true; - LOG_DBG("Old "); - print_address(&test_data.old_addr); - LOG_DBG("New "); - print_address(addr); + LOG_DBG("Old addr: %s, New addr: %s", bt_testlib_addr_to_str(&test_data.old_addr), + bt_testlib_addr_to_str(addr)); test_data.rpa_rotations++; @@ -68,32 +69,29 @@ static void cb_expect_rpa(const bt_addr_le_t *addr, int8_t rssi, uint8_t type, rpa_timeout_ms = CONFIG_BT_RPA_TIMEOUT * MSEC_PER_SEC; if (abs(diff_ms - rpa_timeout_ms) > (rpa_timeout_ms / 10)) { - FAIL("RPA rotation did not occur within +-10% of CONFIG_BT_RPA_TIMEOUT\n"); + TEST_FAIL("RPA rotation did not occur within +-10%% of CONFIG_BT_RPA_TIMEOUT"); } bt_addr_le_copy(&test_data.old_addr, addr); test_data.old_time = k_uptime_get(); if (test_data.rpa_rotations > EXPECTED_NUM_ROTATIONS) { - PASS("PASS\n"); + TEST_PASS("PASS"); } } static void cb_expect_id(const bt_addr_le_t *addr, int8_t rssi, uint8_t type, struct net_buf_simple *ad) { - LOG_DBG("expecting addr:"); - print_address(&dut_addr); - LOG_DBG("got addr:"); - print_address(addr); + LOG_DBG("Expecting addr: %s, Got addr: %s", bt_testlib_addr_to_str(&test_data.old_addr), + bt_testlib_addr_to_str(addr)); if (addr->type != BT_ADDR_LE_RANDOM) { - FAIL("Expected public address (0x%x) got 0x%x\n", - BT_ADDR_LE_RANDOM, addr->type); + TEST_FAIL("Expected public address (0x%x) got 0x%x", BT_ADDR_LE_RANDOM, addr->type); } if (!bt_addr_le_eq(&dut_addr, addr)) { - FAIL("DUT not using identity address\n"); + TEST_FAIL("DUT not using identity address"); } } @@ -109,7 +107,7 @@ static void scan_cb(const bt_addr_le_t *addr, int8_t rssi, uint8_t type, struct cb_expect_id(addr, rssi, type, ad); /* Tell DUT to switch to RPA */ - backchannel_sync_send(); + bk_sync_send(); test_data.id_addr_ok = true; } else { cb_expect_rpa(addr, rssi, type, ad); @@ -121,16 +119,16 @@ void tester_procedure(void) int err; /* open a backchannel to the peer */ - backchannel_init(PERIPHERAL_SIM_ID); + TEST_ASSERT(bk_sync_init() == 0); err = bt_enable(NULL); if (err) { - FAIL("Failed to enable bluetooth (err %d\n)", err); + TEST_FAIL("Failed to enable bluetooth (err %d)", err); } err = bt_le_scan_start(BT_LE_SCAN_PASSIVE_CONTINUOUS, scan_cb); if (err) { - FAIL("Failed to start scanning\n"); + TEST_FAIL("Failed to start scanning"); } } diff --git a/tests/bsim/bluetooth/host/privacy/peripheral/CMakeLists.txt b/tests/bsim/bluetooth/host/privacy/peripheral/CMakeLists.txt index 8b6bee0ccf0c..32877debd2f8 100644 --- a/tests/bsim/bluetooth/host/privacy/peripheral/CMakeLists.txt +++ b/tests/bsim/bluetooth/host/privacy/peripheral/CMakeLists.txt @@ -5,8 +5,14 @@ cmake_minimum_required(VERSION 3.20.0) find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) project(bsim_test_rpa_peripheral) +add_subdirectory(${ZEPHYR_BASE}/tests/bsim/babblekit babblekit) +add_subdirectory(${ZEPHYR_BASE}/tests/bluetooth/common/testlib testlib) +target_link_libraries(app PRIVATE + babblekit + testlib +) + target_sources(app PRIVATE - src/bs_bt_utils.c src/tester_rpa_rotation.c src/tester_rpa_expired.c src/main.c diff --git a/tests/bsim/bluetooth/host/privacy/peripheral/src/bs_bt_utils.c b/tests/bsim/bluetooth/host/privacy/peripheral/src/bs_bt_utils.c deleted file mode 100644 index 795fef110144..000000000000 --- a/tests/bsim/bluetooth/host/privacy/peripheral/src/bs_bt_utils.c +++ /dev/null @@ -1,35 +0,0 @@ -/* - * Copyright (c) 2023 Nordic Semiconductor ASA - * - * SPDX-License-Identifier: Apache-2.0 - */ - -#include "bs_bt_utils.h" -#include "argparse.h" - -#define BS_SECONDS(dur_sec) ((bs_time_t)dur_sec * 1000000) -#define TEST_TIMEOUT_SIMULATED BS_SECONDS(70) - -void test_tick(bs_time_t HW_device_time) -{ - bs_trace_debug_time(0, "Simulation ends now.\n"); - if (bst_result != Passed) { - bst_result = Failed; - - bs_trace_error("Test did not pass before simulation ended.\n"); - } -} - -void test_init(void) -{ - bst_ticker_set_next_tick_absolute(TEST_TIMEOUT_SIMULATED); - bst_result = In_progress; -} - -void print_address(bt_addr_le_t *addr) -{ - char array[BT_ADDR_LE_STR_LEN]; - - bt_addr_le_to_str(addr, array, sizeof(array)); - printk("Address : %s\n", array); -} diff --git a/tests/bsim/bluetooth/host/privacy/peripheral/src/bs_bt_utils.h b/tests/bsim/bluetooth/host/privacy/peripheral/src/bs_bt_utils.h deleted file mode 100644 index bb28c81edbd6..000000000000 --- a/tests/bsim/bluetooth/host/privacy/peripheral/src/bs_bt_utils.h +++ /dev/null @@ -1,52 +0,0 @@ -/** - * Common functions and helpers for BSIM ADV tests - * - * Copyright (c) 2023 Nordic Semiconductor ASA - * - * SPDX-License-Identifier: Apache-2.0 - */ - -#include "bs_tracing.h" -#include "bs_types.h" -#include "bstests.h" -#include "time_machine.h" - -#include - -#include -#include -#include -#include -#include -#include -#include -#include - -extern enum bst_result_t bst_result; - -#define ASSERT(expr, ...) \ - do { \ - if (!(expr)) { \ - FAIL(__VA_ARGS__); \ - } \ - } while (0) - -#define FAIL(...) \ - do { \ - bst_result = Failed; \ - bs_trace_error_time_line(__VA_ARGS__); \ - } while (0) - -#define PASS(...) \ - do { \ - bst_result = Passed; \ - bs_trace_info_time(1, __VA_ARGS__); \ - } while (0) - -#define TESTER_CENTRAL_ID 0 -#define DUT_PERIPHERAL_ID 1 - -void test_tick(bs_time_t HW_device_time); -void test_init(void); - -void print_address(bt_addr_le_t *addr); diff --git a/tests/bsim/bluetooth/host/privacy/peripheral/src/dut_rpa_expired.c b/tests/bsim/bluetooth/host/privacy/peripheral/src/dut_rpa_expired.c index 124900fad5f5..4c0e2f561e51 100644 --- a/tests/bsim/bluetooth/host/privacy/peripheral/src/dut_rpa_expired.c +++ b/tests/bsim/bluetooth/host/privacy/peripheral/src/dut_rpa_expired.c @@ -4,17 +4,24 @@ * SPDX-License-Identifier: Apache-2.0 */ -#include "bs_bt_utils.h" - +#include #include #include +#include #include #include #include +#include +#include +#include +#include +#include #include #include +#include "babblekit/testcase.h" + #include "common/bt_str.h" #define ID_1 1 @@ -89,7 +96,7 @@ static void create_adv(struct bt_le_ext_adv **adv, int id) err = bt_le_ext_adv_create(¶ms, &cb_adv, adv); if (err) { - FAIL("Failed to create advertiser (%d)\n", err); + TEST_FAIL("Failed to create advertiser (%d)", err); } } @@ -101,12 +108,12 @@ void start_rpa_advertising(void) /* Enable bluetooth */ err = bt_enable(NULL); if (err) { - FAIL("Failed to enable bluetooth (err %d\n)", err); + TEST_FAIL("Failed to enable bluetooth (err %d)", err); } err = settings_load(); if (err) { - FAIL("Failed to enable settings (err %d\n)", err); + TEST_FAIL("Failed to enable settings (err %d)", err); } bt_id_get(NULL, &bt_id_count); @@ -119,12 +126,12 @@ void start_rpa_advertising(void) id_a = bt_id_create(NULL, NULL); if (id_a != ID_1) { - FAIL("bt_id_create id_a failed (err %d)\n", id_a); + TEST_FAIL("bt_id_create id_a failed (err %d)", id_a); } id_b = bt_id_create(NULL, NULL); if (id_b != ID_2) { - FAIL("bt_id_create id_b failed (err %d)\n", id_b); + TEST_FAIL("bt_id_create id_b failed (err %d)", id_b); } } else { printk("Extra identities loaded from settings\n"); @@ -132,7 +139,7 @@ void start_rpa_advertising(void) bt_id_get(NULL, &bt_id_count); if (bt_id_count != CONFIG_BT_ID_MAX) { - FAIL("bt_id_get returned incorrect number of identities %u\n", bt_id_count); + TEST_FAIL("bt_id_get returned incorrect number of identities %u", bt_id_count); } for (int i = 0; i < CONFIG_BT_EXT_ADV_MAX_ADV_SET; i++) { @@ -147,12 +154,12 @@ void start_rpa_advertising(void) /* Set extended advertising data */ err = bt_le_ext_adv_set_data(adv_set[i], &ad_id[i], 1, NULL, 0); if (err) { - FAIL("Failed to set advertising data for set %d (err %d)\n", i, err); + TEST_FAIL("Failed to set advertising data for set %d (err %d)", i, err); } err = bt_le_ext_adv_start(adv_set[i], BT_LE_EXT_ADV_START_DEFAULT); if (err) { - FAIL("Failed to start advertising (err %d)\n", err); + TEST_FAIL("Failed to start advertising (err %d)", err); } } } @@ -162,5 +169,5 @@ void dut_rpa_expired_procedure(void) start_rpa_advertising(); /* Nothing to do */ - PASS("PASS\n"); + TEST_PASS("PASS"); } diff --git a/tests/bsim/bluetooth/host/privacy/peripheral/src/dut_rpa_rotation.c b/tests/bsim/bluetooth/host/privacy/peripheral/src/dut_rpa_rotation.c index 9d72b5368b29..47f16a9b12b1 100644 --- a/tests/bsim/bluetooth/host/privacy/peripheral/src/dut_rpa_rotation.c +++ b/tests/bsim/bluetooth/host/privacy/peripheral/src/dut_rpa_rotation.c @@ -4,17 +4,25 @@ * SPDX-License-Identifier: Apache-2.0 */ -#include "bs_bt_utils.h" +#include #include #include +#include #include #include #include +#include +#include +#include +#include +#include #include #include +#include "babblekit/testcase.h" + #include "common/bt_str.h" #define ID_A_INDEX 1 @@ -63,7 +71,7 @@ static void create_adv(struct bt_le_ext_adv **adv, int id) err = bt_le_ext_adv_create(¶ms, &cb_adv, adv); if (err) { - FAIL("Failed to create advertiser (%d)\n", err); + TEST_FAIL("Failed to create advertiser (%d)", err); } } @@ -75,12 +83,12 @@ void start_advertising(void) /* Enable bluetooth */ err = bt_enable(NULL); if (err) { - FAIL("Failed to enable bluetooth (err %d\n)", err); + TEST_FAIL("Failed to enable bluetooth (err %d)", err); } err = settings_load(); if (err) { - FAIL("Failed to enable settings (err %d\n)", err); + TEST_FAIL("Failed to enable settings (err %d)", err); } bt_id_get(NULL, &bt_id_count); @@ -92,12 +100,12 @@ void start_advertising(void) id_a = bt_id_create(NULL, NULL); if (id_a != ID_A_INDEX) { - FAIL("bt_id_create id_a failed (err %d)\n", id_a); + TEST_FAIL("bt_id_create id_a failed (err %d)", id_a); } id_b = bt_id_create(NULL, NULL); if (id_b != ID_B_INDEX) { - FAIL("bt_id_create id_b failed (err %d)\n", id_b); + TEST_FAIL("bt_id_create id_b failed (err %d)", id_b); } } else { printk("Extra identities loaded from settings\n"); @@ -105,7 +113,7 @@ void start_advertising(void) bt_id_get(NULL, &bt_id_count); if (bt_id_count != CONFIG_BT_ID_MAX) { - FAIL("bt_id_get returned incorrect number of identities %u\n", bt_id_count); + TEST_FAIL("bt_id_get returned incorrect number of identities %u", bt_id_count); } for (int i = 0; i < CONFIG_BT_EXT_ADV_MAX_ADV_SET; i++) { @@ -121,12 +129,12 @@ void start_advertising(void) /* Set extended advertising data */ err = bt_le_ext_adv_set_data(adv_set[i], &ad_id[i], 1, NULL, 0); if (err) { - FAIL("Failed to set advertising data for set %d (err %d)\n", i, err); + TEST_FAIL("Failed to set advertising data for set %d (err %d)", i, err); } err = bt_le_ext_adv_start(adv_set[i], BT_LE_EXT_ADV_START_DEFAULT); if (err) { - FAIL("Failed to start advertising (err %d)\n", err); + TEST_FAIL("Failed to start advertising (err %d)", err); } } } @@ -136,5 +144,5 @@ void dut_procedure(void) start_advertising(); /* Nothing to do */ - PASS("PASS\n"); + TEST_PASS("PASS"); } diff --git a/tests/bsim/bluetooth/host/privacy/peripheral/src/main.c b/tests/bsim/bluetooth/host/privacy/peripheral/src/main.c index b84a17876362..c5cd5140ea75 100644 --- a/tests/bsim/bluetooth/host/privacy/peripheral/src/main.c +++ b/tests/bsim/bluetooth/host/privacy/peripheral/src/main.c @@ -4,7 +4,6 @@ * SPDX-License-Identifier: Apache-2.0 */ -#include "bs_bt_utils.h" #include "bstests.h" extern struct bst_test_list *test_main_rpa_rotation_install(struct bst_test_list *tests); diff --git a/tests/bsim/bluetooth/host/privacy/peripheral/src/main_rpa_expired.c b/tests/bsim/bluetooth/host/privacy/peripheral/src/main_rpa_expired.c index f5eb51a3e9d3..c7badb019229 100644 --- a/tests/bsim/bluetooth/host/privacy/peripheral/src/main_rpa_expired.c +++ b/tests/bsim/bluetooth/host/privacy/peripheral/src/main_rpa_expired.c @@ -4,7 +4,6 @@ * SPDX-License-Identifier: Apache-2.0 */ -#include "bs_bt_utils.h" #include "bstests.h" void tester_verify_rpa_procedure(void); @@ -13,14 +12,10 @@ void dut_rpa_expired_procedure(void); static const struct bst_test_instance test_to_add[] = { { .test_id = "central_rpa_check", - .test_pre_init_f = test_init, - .test_tick_f = test_tick, .test_main_f = tester_verify_rpa_procedure, }, { .test_id = "peripheral_rpa_expired", - .test_pre_init_f = test_init, - .test_tick_f = test_tick, .test_main_f = dut_rpa_expired_procedure, }, BSTEST_END_MARKER, diff --git a/tests/bsim/bluetooth/host/privacy/peripheral/src/main_rpa_rotation.c b/tests/bsim/bluetooth/host/privacy/peripheral/src/main_rpa_rotation.c index ac66700ab2f3..3eacd2efc29f 100644 --- a/tests/bsim/bluetooth/host/privacy/peripheral/src/main_rpa_rotation.c +++ b/tests/bsim/bluetooth/host/privacy/peripheral/src/main_rpa_rotation.c @@ -4,7 +4,6 @@ * SPDX-License-Identifier: Apache-2.0 */ -#include "bs_bt_utils.h" #include "bstests.h" void tester_procedure(void); @@ -13,14 +12,10 @@ void dut_procedure(void); static const struct bst_test_instance test_to_add[] = { { .test_id = "central", - .test_pre_init_f = test_init, - .test_tick_f = test_tick, .test_main_f = tester_procedure, }, { .test_id = "peripheral", - .test_pre_init_f = test_init, - .test_tick_f = test_tick, .test_main_f = dut_procedure, }, BSTEST_END_MARKER, diff --git a/tests/bsim/bluetooth/host/privacy/peripheral/src/tester_rpa_expired.c b/tests/bsim/bluetooth/host/privacy/peripheral/src/tester_rpa_expired.c index 09c9ffab9c4a..6862005a2521 100644 --- a/tests/bsim/bluetooth/host/privacy/peripheral/src/tester_rpa_expired.c +++ b/tests/bsim/bluetooth/host/privacy/peripheral/src/tester_rpa_expired.c @@ -4,13 +4,23 @@ * SPDX-License-Identifier: Apache-2.0 */ -#include "bs_bt_utils.h" -#include -#include +#include #include + +#include +#include #include +#include +#include +#include +#include +#include +#include #include +#include "babblekit/testcase.h" +#include "testlib/addr.h" + #define EXPECTED_NUM_ROTATIONS 5 struct adv_set_data_t { @@ -39,7 +49,7 @@ static void test_address(bt_addr_le_t *addr) static int64_t rpa_timeout_ms = CONFIG_BT_RPA_TIMEOUT * MSEC_PER_SEC; if (!BT_ADDR_IS_RPA(&addr->a)) { - FAIL("Bluetooth address is not RPA\n"); + TEST_FAIL("Bluetooth address is not RPA"); } /* Only save the address if this is the first scan */ @@ -55,10 +65,9 @@ static void test_address(bt_addr_le_t *addr) return; } - printk("Ad set %d Old ", adv_index); - print_address(&adv_set_data[adv_index].old_addr); - printk("Ad set %d New ", adv_index); - print_address(addr); + printk("Ad set %d: Old addr %s, new addr %s\n", adv_index, + bt_testlib_addr_to_str(&adv_set_data[adv_index].old_addr), + bt_testlib_addr_to_str(addr)); /* For the first 2 rpa rotations, either of the first 2 adv sets returns false. * Hence first 2 adv sets continue with old rpa in first 2 rpa rotations. @@ -69,28 +78,28 @@ static void test_address(bt_addr_le_t *addr) if (adv_index < 2) { if (!bt_addr_le_eq(addr, &adv_set_data[adv_index].old_addr)) { - FAIL("Adv sets should continue with old rpa\n"); + TEST_FAIL("Adv sets should continue with old rpa"); } } else { if (bt_addr_le_eq(addr, &adv_set_data[adv_index].old_addr)) { - FAIL("New RPA should have been generated\n"); + TEST_FAIL("New RPA should have been generated"); } } } else { if (adv_index < 2) { if (bt_addr_le_eq(addr, &adv_set_data[adv_index].old_addr)) { - FAIL("New RPA should have been generated\n"); + TEST_FAIL("New RPA should have been generated"); } } else { if (!bt_addr_le_eq(addr, &adv_set_data[adv_index].old_addr)) { - FAIL("Adv sets should continue with old rpa\n"); + TEST_FAIL("Adv sets should continue with old rpa"); } } } adv_set_data[adv_index].rpa_rotations++; if (adv_set_data[adv_index].rpa_rotations > EXPECTED_NUM_ROTATIONS) { - PASS("PASS\n"); + TEST_PASS("PASS"); } adv_set_data[adv_index].old_time = k_uptime_get(); @@ -117,7 +126,7 @@ void start_rpa_scanning(void) int err = bt_le_scan_start(&scan_param, cb_device_found); if (err) { - FAIL("Failed to start scanning"); + TEST_FAIL("Failed to start scanning"); } } @@ -127,12 +136,12 @@ void tester_verify_rpa_procedure(void) int err = bt_enable(NULL); if (err) { - FAIL("Failed to enable bluetooth (err %d\n)", err); + TEST_FAIL("Failed to enable bluetooth (err %d)", err); } err = settings_load(); if (err) { - FAIL("Failed to enable settings (err %d\n)", err); + TEST_FAIL("Failed to enable settings (err %d)", err); } start_rpa_scanning(); diff --git a/tests/bsim/bluetooth/host/privacy/peripheral/src/tester_rpa_rotation.c b/tests/bsim/bluetooth/host/privacy/peripheral/src/tester_rpa_rotation.c index f0e68785e32b..12b7279f76b1 100644 --- a/tests/bsim/bluetooth/host/privacy/peripheral/src/tester_rpa_rotation.c +++ b/tests/bsim/bluetooth/host/privacy/peripheral/src/tester_rpa_rotation.c @@ -4,13 +4,23 @@ * SPDX-License-Identifier: Apache-2.0 */ -#include "bs_bt_utils.h" +#include +#include + +#include #include #include -#include #include +#include +#include +#include +#include +#include #include +#include "babblekit/testcase.h" +#include "testlib/addr.h" + #define EXPECTED_NUM_ROTATIONS 5 struct adv_set_data_t { @@ -46,21 +56,21 @@ static void validate_rpa_addr_generated_for_adv_sets(void) * a different address even though they use the same Bluetooth ID. */ if (!IS_ENABLED(CONFIG_BT_RPA_SHARING)) { - FAIL("RPA same for adv sets with same id and RPA sharing disabled\n"); + TEST_FAIL("RPA same for adv sets with same id and RPA sharing disabled"); } } else { /* In the RPA sharing mode, the first two adv sets should have * the same address as they use the same Bluetooth ID. */ if (IS_ENABLED(CONFIG_BT_RPA_SHARING)) { - FAIL("RPA not same for adv sets with same id and RPA sharing enabled\n"); + TEST_FAIL("RPA not same for adv sets with same id and RPA sharing enabled"); } } if (bt_addr_le_eq(&adv_set_data[0].old_addr, &adv_set_data[3].old_addr)) { - FAIL("RPA same for adv sets with different id's\n"); + TEST_FAIL("RPA same for adv sets with different id's"); } if (bt_addr_le_eq(&adv_set_data[1].old_addr, &adv_set_data[3].old_addr)) { - FAIL("RPA same for adv sets with different id's\n"); + TEST_FAIL("RPA same for adv sets with different id's"); } adv_set_data[0].addr_set = false; adv_set_data[1].addr_set = false; @@ -72,7 +82,7 @@ static void test_address(bt_addr_le_t *addr) int64_t diff_ms, rpa_timeout_ms; if (!BT_ADDR_IS_RPA(&addr->a)) { - FAIL("Bluetooth address is not RPA\n"); + TEST_FAIL("Bluetooth address is not RPA"); } /* Only save the address + time if this is the first scan */ @@ -87,10 +97,10 @@ static void test_address(bt_addr_le_t *addr) return; } adv_set_data[adv_index].addr_set = true; - printk("Ad set %d Old ", adv_index); - print_address(&adv_set_data[adv_index].old_addr); - printk("Ad set %d New ", adv_index); - print_address(addr); + + printk("Ad set %d: Old addr %s, new addr %s\n", adv_index, + bt_testlib_addr_to_str(&adv_set_data[adv_index].old_addr), + bt_testlib_addr_to_str(addr)); adv_set_data[adv_index].rpa_rotations++; @@ -99,7 +109,7 @@ static void test_address(bt_addr_le_t *addr) rpa_timeout_ms = CONFIG_BT_RPA_TIMEOUT * MSEC_PER_SEC; if (abs(diff_ms - rpa_timeout_ms) > (rpa_timeout_ms / 10)) { - FAIL("RPA rotation did not occur within +-10%% of CONFIG_BT_RPA_TIMEOUT\n"); + TEST_FAIL("RPA rotation did not occur within +-10%% of CONFIG_BT_RPA_TIMEOUT"); } bt_addr_le_copy(&adv_set_data[adv_index].old_addr, addr); @@ -107,7 +117,7 @@ static void test_address(bt_addr_le_t *addr) validate_rpa_addr_generated_for_adv_sets(); if (adv_set_data[adv_index].rpa_rotations > EXPECTED_NUM_ROTATIONS) { - PASS("PASS\n"); + TEST_PASS("PASS"); } } @@ -131,7 +141,7 @@ void start_scanning(void) int err = bt_le_scan_start(&scan_param, cb_device_found); if (err) { - FAIL("Failed to start scanning"); + TEST_FAIL("Failed to start scanning"); } } @@ -141,12 +151,12 @@ void tester_procedure(void) int err = bt_enable(NULL); if (err) { - FAIL("Failed to enable bluetooth (err %d\n)", err); + TEST_FAIL("Failed to enable bluetooth (err %d)", err); } err = settings_load(); if (err) { - FAIL("Failed to enable settings (err %d\n)", err); + TEST_FAIL("Failed to enable settings (err %d)", err); } start_scanning(); diff --git a/tests/bsim/bluetooth/host/scan/start_stop/src/main.c b/tests/bsim/bluetooth/host/scan/start_stop/src/main.c index b266d8875079..966e23ad4887 100644 --- a/tests/bsim/bluetooth/host/scan/start_stop/src/main.c +++ b/tests/bsim/bluetooth/host/scan/start_stop/src/main.c @@ -18,25 +18,10 @@ LOG_MODULE_REGISTER(bt_bsim_scan_start_stop, LOG_LEVEL_DBG); -#define WAIT_TIME_S 60 -#define WAIT_TIME (WAIT_TIME_S * 1e6) - static atomic_t flag_adv_report_received; static atomic_t flag_periodic_sync_established; static bt_addr_le_t adv_addr; -static void test_tick(bs_time_t HW_device_time) -{ - if (bst_result != Passed) { - TEST_FAIL("Test failed (not passed after %d seconds)\n", WAIT_TIME_S); - } -} - -static void test_init(void) -{ - bst_ticker_set_next_tick_absolute(WAIT_TIME); -} - static void bt_sync_established_cb(struct bt_le_per_adv_sync *sync, struct bt_le_per_adv_sync_synced_info *info) { @@ -98,7 +83,7 @@ void run_dut(void) LOG_DBG("Starting DUT"); err = bt_enable(NULL); - TEST_ASSERT(!err, "Bluetooth init failed (err %d)\n", err); + TEST_ASSERT(!err, "Bluetooth init failed (err %d)", err); LOG_DBG("Bluetooth initialised"); @@ -118,7 +103,7 @@ void run_dut(void) bt_le_per_adv_sync_cb_register(&sync_callbacks); err = bt_le_per_adv_sync_create(&per_sync_param, &p_per_sync); - TEST_ASSERT(!err, "Periodic sync setup failed (err %d)\n", err); + TEST_ASSERT(!err, "Periodic sync setup failed (err %d)", err); LOG_DBG("Periodic sync started"); /* Start scanner. Check that we can start the scanner while it is already @@ -134,7 +119,7 @@ void run_dut(void) }; err = bt_le_scan_start(&scan_params, device_found); - TEST_ASSERT(!err, "Scanner setup failed (err %d)\n", err); + TEST_ASSERT(!err, "Scanner setup failed (err %d)", err); LOG_DBG("Explicit scanner started"); LOG_DBG("Wait for an advertising report"); @@ -142,12 +127,12 @@ void run_dut(void) /* Stop the scanner. That should not affect the periodic advertising sync. */ err = bt_le_scan_stop(); - TEST_ASSERT(!err, "Scanner stop failed (err %d)\n", err); + TEST_ASSERT(!err, "Scanner stop failed (err %d)", err); LOG_DBG("Explicit scanner stopped"); /* We should be able to stop the periodic advertising sync. */ err = bt_le_per_adv_sync_delete(p_per_sync); - TEST_ASSERT(!err, "Periodic sync stop failed (err %d)\n", err); + TEST_ASSERT(!err, "Periodic sync stop failed (err %d)", err); LOG_DBG("Periodic sync stopped"); /* Start the periodic advertising sync. This time, provide the address of the advertiser @@ -161,19 +146,19 @@ void run_dut(void) .timeout = BT_GAP_PER_ADV_MAX_TIMEOUT }; err = bt_le_per_adv_sync_create(&per_sync_param, &p_per_sync); - TEST_ASSERT(!err, "Periodic sync setup failed (err %d)\n", err); + TEST_ASSERT(!err, "Periodic sync setup failed (err %d)", err); LOG_DBG("Periodic sync started"); /* Start the explicit scanner */ err = bt_le_scan_start(&scan_params, device_found); - TEST_ASSERT(!err, "Scanner setup failed (err %d)\n", err); + TEST_ASSERT(!err, "Scanner setup failed (err %d)", err); LOG_DBG("Explicit scanner started"); /* Stop the explicit scanner. This should not stop scanner, since we still try to establish * a sync. */ err = bt_le_scan_stop(); - TEST_ASSERT(!err, "Scanner stop failed (err %d)\n", err); + TEST_ASSERT(!err, "Scanner stop failed (err %d)", err); LOG_DBG("Explicit scanner stopped"); /* Signal to the tester to start the periodic adv. */ @@ -186,7 +171,7 @@ void run_dut(void) /* Signal to the tester to end the test. */ bk_sync_send(); - TEST_PASS("Test passed (DUT)\n"); + TEST_PASS("Test passed (DUT)"); } void run_tester(void) @@ -199,7 +184,7 @@ void run_tester(void) LOG_DBG("Starting DUT"); err = bt_enable(NULL); - TEST_ASSERT(!err, "Bluetooth init failed (err %d)\n", err); + TEST_ASSERT(!err, "Bluetooth init failed (err %d)", err); LOG_DBG("Bluetooth initialised"); @@ -233,22 +218,18 @@ void run_tester(void) bt_le_per_adv_stop(per_adv); - TEST_PASS("Test passed (Tester)\n"); + TEST_PASS("Test passed (Tester)"); } static const struct bst_test_instance test_def[] = { { .test_id = "scanner", .test_descr = "SCANNER", - .test_post_init_f = test_init, - .test_tick_f = test_tick, .test_main_f = run_dut, }, { .test_id = "periodic_adv", .test_descr = "PER_ADV", - .test_post_init_f = test_init, - .test_tick_f = test_tick, .test_main_f = run_tester, }, BSTEST_END_MARKER diff --git a/tests/bsim/bluetooth/host/security/bond_overwrite_allowed/CMakeLists.txt b/tests/bsim/bluetooth/host/security/bond_overwrite_allowed/CMakeLists.txt index 6ff34818db7a..8da0030f3653 100644 --- a/tests/bsim/bluetooth/host/security/bond_overwrite_allowed/CMakeLists.txt +++ b/tests/bsim/bluetooth/host/security/bond_overwrite_allowed/CMakeLists.txt @@ -5,6 +5,9 @@ cmake_minimum_required(VERSION 3.20.0) find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) project(bsim_test_multi_id_bond_overwrite_deny) +add_subdirectory(${ZEPHYR_BASE}/tests/bsim/babblekit babblekit) +target_link_libraries(app PRIVATE babblekit) + target_sources(app PRIVATE src/bs_bt_utils.c src/central.c diff --git a/tests/bsim/bluetooth/host/security/bond_overwrite_allowed/src/bs_bt_utils.c b/tests/bsim/bluetooth/host/security/bond_overwrite_allowed/src/bs_bt_utils.c index d43fb32626a4..e96fdebdfee1 100644 --- a/tests/bsim/bluetooth/host/security/bond_overwrite_allowed/src/bs_bt_utils.c +++ b/tests/bsim/bluetooth/host/security/bond_overwrite_allowed/src/bs_bt_utils.c @@ -9,24 +9,6 @@ BUILD_ASSERT(CONFIG_BT_MAX_PAIRED >= 2, "CONFIG_BT_MAX_PAIRED is too small."); BUILD_ASSERT(CONFIG_BT_ID_MAX >= 3, "CONFIG_BT_ID_MAX is too small."); -#define BS_SECONDS(dur_sec) ((bs_time_t)dur_sec * 1000000) -#define TEST_TIMEOUT_SIMULATED BS_SECONDS(60) - -void test_tick(bs_time_t HW_device_time) -{ - bs_trace_debug_time(0, "Simulation ends now.\n"); - if (bst_result != Passed) { - bst_result = Failed; - bs_trace_error("Test did not pass before simulation ended.\n"); - } -} - -void test_init(void) -{ - bst_ticker_set_next_tick_absolute(TEST_TIMEOUT_SIMULATED); - bst_result = In_progress; -} - DEFINE_FLAG(flag_is_connected); struct bt_conn *g_conn; @@ -48,7 +30,7 @@ static void disconnected(struct bt_conn *conn, uint8_t reason) BUILD_ASSERT(CONFIG_BT_MAX_CONN == 1, "This test assumes a single link."); static void connected(struct bt_conn *conn, uint8_t err) { - ASSERT((!g_conn || (conn == g_conn)), "Unexpected new connection."); + TEST_ASSERT((!g_conn || (conn == g_conn)), "Unexpected new connection."); if (!g_conn) { g_conn = bt_conn_ref(conn); @@ -73,7 +55,7 @@ void clear_g_conn(void) conn = g_conn; g_conn = NULL; - ASSERT(conn, "Test error: No g_conn!\n"); + TEST_ASSERT(conn, "Test error: No g_conn!"); bt_conn_unref(conn); } @@ -101,9 +83,9 @@ void bs_bt_utils_setup(void) int err; err = bt_enable(NULL); - ASSERT(!err, "bt_enable failed.\n"); + TEST_ASSERT(!err, "bt_enable failed."); err = bt_conn_auth_info_cb_register(&bt_conn_auth_info_cb); - ASSERT(!err, "bt_conn_auth_info_cb_register failed.\n"); + TEST_ASSERT(!err, "bt_conn_auth_info_cb_register failed."); } static void scan_connect_to_first_result__device_found(const bt_addr_le_t *addr, int8_t rssi, @@ -118,17 +100,17 @@ static void scan_connect_to_first_result__device_found(const bt_addr_le_t *addr, /* We're only interested in connectable events */ if (type != BT_HCI_ADV_IND && type != BT_HCI_ADV_DIRECT_IND) { - FAIL("Unexpected advertisement type."); + TEST_FAIL("Unexpected advertisement type."); } bt_addr_le_to_str(addr, addr_str, sizeof(addr_str)); - printk("Got scan result, connecting.. dst %s, RSSI %d\n", addr_str, rssi); + printk("Got scan result, connecting.. dst %s, RSSI %d", addr_str, rssi); err = bt_le_scan_stop(); - ASSERT(!err, "Err bt_le_scan_stop %d", err); + TEST_ASSERT(!err, "Err bt_le_scan_stop %d", err); err = bt_conn_le_create(addr, BT_CONN_LE_CREATE_CONN, BT_LE_CONN_PARAM_DEFAULT, &g_conn); - ASSERT(!err, "Err bt_conn_le_create %d", err); + TEST_ASSERT(!err, "Err bt_conn_le_create %d", err); } void scan_connect_to_first_result(void) @@ -136,7 +118,7 @@ void scan_connect_to_first_result(void) int err; err = bt_le_scan_start(BT_LE_SCAN_PASSIVE, scan_connect_to_first_result__device_found); - ASSERT(!err, "Err bt_le_scan_start %d", err); + TEST_ASSERT(!err, "Err bt_le_scan_start %d", err); } void disconnect(void) @@ -144,7 +126,7 @@ void disconnect(void) int err; err = bt_conn_disconnect(g_conn, BT_HCI_ERR_REMOTE_USER_TERM_CONN); - ASSERT(!err, "Err bt_conn_disconnect %d", err); + TEST_ASSERT(!err, "Err bt_conn_disconnect %d", err); } void set_security(bt_security_t sec) @@ -152,7 +134,7 @@ void set_security(bt_security_t sec) int err; err = bt_conn_set_security(g_conn, sec); - ASSERT(!err, "Err bt_conn_set_security %d", err); + TEST_ASSERT(!err, "Err bt_conn_set_security %d", err); } void advertise_connectable(int id, bt_addr_le_t *directed_dst) @@ -171,5 +153,5 @@ void advertise_connectable(int id, bt_addr_le_t *directed_dst) } err = bt_le_adv_start(¶m, NULL, 0, NULL, 0); - ASSERT(err == 0, "Advertising failed to start (err %d)\n", err); + TEST_ASSERT(err == 0, "Advertising failed to start (err %d)", err); } diff --git a/tests/bsim/bluetooth/host/security/bond_overwrite_allowed/src/bs_bt_utils.h b/tests/bsim/bluetooth/host/security/bond_overwrite_allowed/src/bs_bt_utils.h index b3718eb769de..364ad44dc5d9 100644 --- a/tests/bsim/bluetooth/host/security/bond_overwrite_allowed/src/bs_bt_utils.h +++ b/tests/bsim/bluetooth/host/security/bond_overwrite_allowed/src/bs_bt_utils.h @@ -24,46 +24,8 @@ #include #include -extern enum bst_result_t bst_result; - -#define DECLARE_FLAG(flag) extern atomic_t flag -#define DEFINE_FLAG(flag) atomic_t flag = (atomic_t) false -#define SET_FLAG(flag) (void)atomic_set(&flag, (atomic_t) true) -#define UNSET_FLAG(flag) (void)atomic_set(&flag, (atomic_t) false) -#define WAIT_FOR_FLAG(flag) \ - while (!(bool)atomic_get(&flag)) { \ - (void)k_sleep(K_MSEC(1)); \ - } -#define WAIT_FOR_FLAG_UNSET(flag) \ - while ((bool)atomic_get(&flag)) { \ - (void)k_sleep(K_MSEC(1)); \ - } -#define TAKE_FLAG(flag) \ - while (!(bool)atomic_cas(&flag, true, false)) { \ - (void)k_sleep(K_MSEC(1)); \ - } - -#define ASSERT(expr, ...) \ - do { \ - if (!(expr)) { \ - FAIL(__VA_ARGS__); \ - } \ - } while (0) - -#define FAIL(...) \ - do { \ - bst_result = Failed; \ - bs_trace_error_time_line(__VA_ARGS__); \ - } while (0) - -#define PASS(...) \ - do { \ - bst_result = Passed; \ - bs_trace_info_time(1, __VA_ARGS__); \ - } while (0) - -void test_tick(bs_time_t HW_device_time); -void test_init(void); +#include "babblekit/testcase.h" +#include "babblekit/flags.h" DECLARE_FLAG(flag_pairing_complete); DECLARE_FLAG(flag_pairing_failed); diff --git a/tests/bsim/bluetooth/host/security/bond_overwrite_allowed/src/central.c b/tests/bsim/bluetooth/host/security/bond_overwrite_allowed/src/central.c index 164646fe79c4..c2e03e71dac4 100644 --- a/tests/bsim/bluetooth/host/security/bond_overwrite_allowed/src/central.c +++ b/tests/bsim/bluetooth/host/security/bond_overwrite_allowed/src/central.c @@ -12,6 +12,9 @@ #include +#include "babblekit/testcase.h" +#include "babblekit/flags.h" + void central(void) { bt_addr_le_t id_b; @@ -40,8 +43,8 @@ void central(void) printk("== Directed connect id b ==\n"); scan_connect_to_first_result(); wait_connected(); - ASSERT(bt_addr_le_eq(bt_conn_get_dst(g_conn), &id_b), - "Unexpected Peer. Did something resolve incorrectly?"); + TEST_ASSERT(bt_addr_le_eq(bt_conn_get_dst(g_conn), &id_b), + "Unexpected Peer. Did something resolve incorrectly?"); - PASS("PASS\n"); + TEST_PASS("PASS"); } diff --git a/tests/bsim/bluetooth/host/security/bond_overwrite_allowed/src/main.c b/tests/bsim/bluetooth/host/security/bond_overwrite_allowed/src/main.c index 0b3d857c0c47..9fa91e719ee3 100644 --- a/tests/bsim/bluetooth/host/security/bond_overwrite_allowed/src/main.c +++ b/tests/bsim/bluetooth/host/security/bond_overwrite_allowed/src/main.c @@ -13,14 +13,10 @@ void peripheral(void); static const struct bst_test_instance test_to_add[] = { { .test_id = "central", - .test_pre_init_f = test_init, - .test_tick_f = test_tick, .test_main_f = central, }, { .test_id = "peripheral", - .test_pre_init_f = test_init, - .test_tick_f = test_tick, .test_main_f = peripheral, }, BSTEST_END_MARKER, diff --git a/tests/bsim/bluetooth/host/security/bond_overwrite_allowed/src/peripheral.c b/tests/bsim/bluetooth/host/security/bond_overwrite_allowed/src/peripheral.c index 633768879d34..11d0fe51ff3c 100644 --- a/tests/bsim/bluetooth/host/security/bond_overwrite_allowed/src/peripheral.c +++ b/tests/bsim/bluetooth/host/security/bond_overwrite_allowed/src/peripheral.c @@ -13,6 +13,9 @@ #include #include +#include "babblekit/testcase.h" +#include "babblekit/flags.h" + void peripheral(void) { bs_bt_utils_setup(); @@ -22,10 +25,10 @@ void peripheral(void) bt_addr_le_t central; id_a = bt_id_create(NULL, NULL); - ASSERT(id_a >= 0, "bt_id_create id_a failed (err %d)\n", id_a); + TEST_ASSERT(id_a >= 0, "bt_id_create id_a failed (err %d)", id_a); id_b = bt_id_create(NULL, NULL); - ASSERT(id_b >= 0, "bt_id_create id_b failed (err %d)\n", id_b); + TEST_ASSERT(id_b >= 0, "bt_id_create id_b failed (err %d)", id_b); printk("== Bonding id a ==\n"); advertise_connectable(id_a, NULL); @@ -42,8 +45,8 @@ void peripheral(void) BUILD_ASSERT(IS_ENABLED(CONFIG_BT_SMP_ALLOW_UNAUTH_OVERWRITE), ""); BUILD_ASSERT(IS_ENABLED(CONFIG_BT_ID_UNPAIR_MATCHING_BONDS), ""); TAKE_FLAG(flag_pairing_complete); - ASSERT(bt_addr_le_eq(bt_conn_get_dst(g_conn), ¢ral), - "Test requires that central uses the same identity in both bonds."); + TEST_ASSERT(bt_addr_le_eq(bt_conn_get_dst(g_conn), ¢ral), + "Test requires that central uses the same identity in both bonds."); /* Central should disconnect here. */ wait_disconnected(); clear_g_conn(); @@ -53,5 +56,5 @@ void peripheral(void) wait_connected(); /* Central should verify that its bond with id_b works as expected. */ - PASS("PASS\n"); + TEST_PASS("PASS"); } diff --git a/tests/bsim/bluetooth/host/security/bond_overwrite_denied/CMakeLists.txt b/tests/bsim/bluetooth/host/security/bond_overwrite_denied/CMakeLists.txt index 6ff34818db7a..8da0030f3653 100644 --- a/tests/bsim/bluetooth/host/security/bond_overwrite_denied/CMakeLists.txt +++ b/tests/bsim/bluetooth/host/security/bond_overwrite_denied/CMakeLists.txt @@ -5,6 +5,9 @@ cmake_minimum_required(VERSION 3.20.0) find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) project(bsim_test_multi_id_bond_overwrite_deny) +add_subdirectory(${ZEPHYR_BASE}/tests/bsim/babblekit babblekit) +target_link_libraries(app PRIVATE babblekit) + target_sources(app PRIVATE src/bs_bt_utils.c src/central.c diff --git a/tests/bsim/bluetooth/host/security/bond_overwrite_denied/src/bs_bt_utils.c b/tests/bsim/bluetooth/host/security/bond_overwrite_denied/src/bs_bt_utils.c index d43fb32626a4..e96fdebdfee1 100644 --- a/tests/bsim/bluetooth/host/security/bond_overwrite_denied/src/bs_bt_utils.c +++ b/tests/bsim/bluetooth/host/security/bond_overwrite_denied/src/bs_bt_utils.c @@ -9,24 +9,6 @@ BUILD_ASSERT(CONFIG_BT_MAX_PAIRED >= 2, "CONFIG_BT_MAX_PAIRED is too small."); BUILD_ASSERT(CONFIG_BT_ID_MAX >= 3, "CONFIG_BT_ID_MAX is too small."); -#define BS_SECONDS(dur_sec) ((bs_time_t)dur_sec * 1000000) -#define TEST_TIMEOUT_SIMULATED BS_SECONDS(60) - -void test_tick(bs_time_t HW_device_time) -{ - bs_trace_debug_time(0, "Simulation ends now.\n"); - if (bst_result != Passed) { - bst_result = Failed; - bs_trace_error("Test did not pass before simulation ended.\n"); - } -} - -void test_init(void) -{ - bst_ticker_set_next_tick_absolute(TEST_TIMEOUT_SIMULATED); - bst_result = In_progress; -} - DEFINE_FLAG(flag_is_connected); struct bt_conn *g_conn; @@ -48,7 +30,7 @@ static void disconnected(struct bt_conn *conn, uint8_t reason) BUILD_ASSERT(CONFIG_BT_MAX_CONN == 1, "This test assumes a single link."); static void connected(struct bt_conn *conn, uint8_t err) { - ASSERT((!g_conn || (conn == g_conn)), "Unexpected new connection."); + TEST_ASSERT((!g_conn || (conn == g_conn)), "Unexpected new connection."); if (!g_conn) { g_conn = bt_conn_ref(conn); @@ -73,7 +55,7 @@ void clear_g_conn(void) conn = g_conn; g_conn = NULL; - ASSERT(conn, "Test error: No g_conn!\n"); + TEST_ASSERT(conn, "Test error: No g_conn!"); bt_conn_unref(conn); } @@ -101,9 +83,9 @@ void bs_bt_utils_setup(void) int err; err = bt_enable(NULL); - ASSERT(!err, "bt_enable failed.\n"); + TEST_ASSERT(!err, "bt_enable failed."); err = bt_conn_auth_info_cb_register(&bt_conn_auth_info_cb); - ASSERT(!err, "bt_conn_auth_info_cb_register failed.\n"); + TEST_ASSERT(!err, "bt_conn_auth_info_cb_register failed."); } static void scan_connect_to_first_result__device_found(const bt_addr_le_t *addr, int8_t rssi, @@ -118,17 +100,17 @@ static void scan_connect_to_first_result__device_found(const bt_addr_le_t *addr, /* We're only interested in connectable events */ if (type != BT_HCI_ADV_IND && type != BT_HCI_ADV_DIRECT_IND) { - FAIL("Unexpected advertisement type."); + TEST_FAIL("Unexpected advertisement type."); } bt_addr_le_to_str(addr, addr_str, sizeof(addr_str)); - printk("Got scan result, connecting.. dst %s, RSSI %d\n", addr_str, rssi); + printk("Got scan result, connecting.. dst %s, RSSI %d", addr_str, rssi); err = bt_le_scan_stop(); - ASSERT(!err, "Err bt_le_scan_stop %d", err); + TEST_ASSERT(!err, "Err bt_le_scan_stop %d", err); err = bt_conn_le_create(addr, BT_CONN_LE_CREATE_CONN, BT_LE_CONN_PARAM_DEFAULT, &g_conn); - ASSERT(!err, "Err bt_conn_le_create %d", err); + TEST_ASSERT(!err, "Err bt_conn_le_create %d", err); } void scan_connect_to_first_result(void) @@ -136,7 +118,7 @@ void scan_connect_to_first_result(void) int err; err = bt_le_scan_start(BT_LE_SCAN_PASSIVE, scan_connect_to_first_result__device_found); - ASSERT(!err, "Err bt_le_scan_start %d", err); + TEST_ASSERT(!err, "Err bt_le_scan_start %d", err); } void disconnect(void) @@ -144,7 +126,7 @@ void disconnect(void) int err; err = bt_conn_disconnect(g_conn, BT_HCI_ERR_REMOTE_USER_TERM_CONN); - ASSERT(!err, "Err bt_conn_disconnect %d", err); + TEST_ASSERT(!err, "Err bt_conn_disconnect %d", err); } void set_security(bt_security_t sec) @@ -152,7 +134,7 @@ void set_security(bt_security_t sec) int err; err = bt_conn_set_security(g_conn, sec); - ASSERT(!err, "Err bt_conn_set_security %d", err); + TEST_ASSERT(!err, "Err bt_conn_set_security %d", err); } void advertise_connectable(int id, bt_addr_le_t *directed_dst) @@ -171,5 +153,5 @@ void advertise_connectable(int id, bt_addr_le_t *directed_dst) } err = bt_le_adv_start(¶m, NULL, 0, NULL, 0); - ASSERT(err == 0, "Advertising failed to start (err %d)\n", err); + TEST_ASSERT(err == 0, "Advertising failed to start (err %d)", err); } diff --git a/tests/bsim/bluetooth/host/security/bond_overwrite_denied/src/bs_bt_utils.h b/tests/bsim/bluetooth/host/security/bond_overwrite_denied/src/bs_bt_utils.h index b3718eb769de..364ad44dc5d9 100644 --- a/tests/bsim/bluetooth/host/security/bond_overwrite_denied/src/bs_bt_utils.h +++ b/tests/bsim/bluetooth/host/security/bond_overwrite_denied/src/bs_bt_utils.h @@ -24,46 +24,8 @@ #include #include -extern enum bst_result_t bst_result; - -#define DECLARE_FLAG(flag) extern atomic_t flag -#define DEFINE_FLAG(flag) atomic_t flag = (atomic_t) false -#define SET_FLAG(flag) (void)atomic_set(&flag, (atomic_t) true) -#define UNSET_FLAG(flag) (void)atomic_set(&flag, (atomic_t) false) -#define WAIT_FOR_FLAG(flag) \ - while (!(bool)atomic_get(&flag)) { \ - (void)k_sleep(K_MSEC(1)); \ - } -#define WAIT_FOR_FLAG_UNSET(flag) \ - while ((bool)atomic_get(&flag)) { \ - (void)k_sleep(K_MSEC(1)); \ - } -#define TAKE_FLAG(flag) \ - while (!(bool)atomic_cas(&flag, true, false)) { \ - (void)k_sleep(K_MSEC(1)); \ - } - -#define ASSERT(expr, ...) \ - do { \ - if (!(expr)) { \ - FAIL(__VA_ARGS__); \ - } \ - } while (0) - -#define FAIL(...) \ - do { \ - bst_result = Failed; \ - bs_trace_error_time_line(__VA_ARGS__); \ - } while (0) - -#define PASS(...) \ - do { \ - bst_result = Passed; \ - bs_trace_info_time(1, __VA_ARGS__); \ - } while (0) - -void test_tick(bs_time_t HW_device_time); -void test_init(void); +#include "babblekit/testcase.h" +#include "babblekit/flags.h" DECLARE_FLAG(flag_pairing_complete); DECLARE_FLAG(flag_pairing_failed); diff --git a/tests/bsim/bluetooth/host/security/bond_overwrite_denied/src/central.c b/tests/bsim/bluetooth/host/security/bond_overwrite_denied/src/central.c index 26f3bee47105..6bf8d38bbf05 100644 --- a/tests/bsim/bluetooth/host/security/bond_overwrite_denied/src/central.c +++ b/tests/bsim/bluetooth/host/security/bond_overwrite_denied/src/central.c @@ -12,6 +12,9 @@ #include +#include "babblekit/testcase.h" +#include "babblekit/flags.h" + void central(void) { bs_bt_utils_setup(); @@ -30,5 +33,5 @@ void central(void) wait_connected(); set_security(BT_SECURITY_L2); wait_disconnected(); - PASS("PASS\n"); + TEST_PASS("PASS"); } diff --git a/tests/bsim/bluetooth/host/security/bond_overwrite_denied/src/main.c b/tests/bsim/bluetooth/host/security/bond_overwrite_denied/src/main.c index 0b3d857c0c47..9fa91e719ee3 100644 --- a/tests/bsim/bluetooth/host/security/bond_overwrite_denied/src/main.c +++ b/tests/bsim/bluetooth/host/security/bond_overwrite_denied/src/main.c @@ -13,14 +13,10 @@ void peripheral(void); static const struct bst_test_instance test_to_add[] = { { .test_id = "central", - .test_pre_init_f = test_init, - .test_tick_f = test_tick, .test_main_f = central, }, { .test_id = "peripheral", - .test_pre_init_f = test_init, - .test_tick_f = test_tick, .test_main_f = peripheral, }, BSTEST_END_MARKER, diff --git a/tests/bsim/bluetooth/host/security/bond_overwrite_denied/src/peripheral.c b/tests/bsim/bluetooth/host/security/bond_overwrite_denied/src/peripheral.c index 319779c94f1b..e9d0f484f30e 100644 --- a/tests/bsim/bluetooth/host/security/bond_overwrite_denied/src/peripheral.c +++ b/tests/bsim/bluetooth/host/security/bond_overwrite_denied/src/peripheral.c @@ -13,6 +13,9 @@ #include #include +#include "babblekit/testcase.h" +#include "babblekit/flags.h" + void peripheral(void) { bs_bt_utils_setup(); @@ -22,10 +25,10 @@ void peripheral(void) bt_addr_le_t central; id_a = bt_id_create(NULL, NULL); - ASSERT(id_a >= 0, "bt_id_create id_a failed (err %d)\n", id_a); + TEST_ASSERT(id_a >= 0, "bt_id_create id_a failed (err %d)", id_a); id_b = bt_id_create(NULL, NULL); - ASSERT(id_b >= 0, "bt_id_create id_b failed (err %d)\n", id_b); + TEST_ASSERT(id_b >= 0, "bt_id_create id_b failed (err %d)", id_b); printk("== Bonding id a ==\n"); advertise_connectable(id_a, NULL); @@ -41,5 +44,5 @@ void peripheral(void) /* Central should bond here. */ BUILD_ASSERT(!IS_ENABLED(CONFIG_BT_ID_UNPAIR_MATCHING_BONDS), ""); WAIT_FOR_FLAG(flag_pairing_failed); - PASS("PASS\n"); + TEST_PASS("PASS"); } diff --git a/tests/bsim/bluetooth/host/security/bond_per_connection/CMakeLists.txt b/tests/bsim/bluetooth/host/security/bond_per_connection/CMakeLists.txt index 2fa69bf185ff..c94b46437eca 100644 --- a/tests/bsim/bluetooth/host/security/bond_per_connection/CMakeLists.txt +++ b/tests/bsim/bluetooth/host/security/bond_per_connection/CMakeLists.txt @@ -5,6 +5,9 @@ cmake_minimum_required(VERSION 3.20.0) find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) project(bsim_test_multi_id_bond_per_connection) +add_subdirectory(${ZEPHYR_BASE}/tests/bsim/babblekit babblekit) +target_link_libraries(app PRIVATE babblekit) + target_sources(app PRIVATE src/bs_bt_utils.c src/central.c diff --git a/tests/bsim/bluetooth/host/security/bond_per_connection/src/bs_bt_utils.c b/tests/bsim/bluetooth/host/security/bond_per_connection/src/bs_bt_utils.c index b6ac6315ca9f..85dd13dedec9 100644 --- a/tests/bsim/bluetooth/host/security/bond_per_connection/src/bs_bt_utils.c +++ b/tests/bsim/bluetooth/host/security/bond_per_connection/src/bs_bt_utils.c @@ -9,23 +9,6 @@ BUILD_ASSERT(CONFIG_BT_MAX_PAIRED >= 2, "CONFIG_BT_MAX_PAIRED is too small."); BUILD_ASSERT(CONFIG_BT_ID_MAX >= 3, "CONFIG_BT_ID_MAX is too small."); -#define BS_SECONDS(dur_sec) ((bs_time_t)dur_sec * 1000000) -#define TEST_TIMEOUT_SIMULATED BS_SECONDS(60) - -void test_tick(bs_time_t HW_device_time) -{ - bs_trace_debug_time(0, "Simulation ends now.\n"); - if (bst_result != Passed) { - bst_result = Failed; - bs_trace_error("Test did not pass before simulation ended.\n"); - } -} - -void test_init(void) -{ - bst_ticker_set_next_tick_absolute(TEST_TIMEOUT_SIMULATED); - bst_result = In_progress; -} DEFINE_FLAG(flag_is_connected); struct bt_conn *g_conn; @@ -50,7 +33,7 @@ static void disconnected(struct bt_conn *conn, uint8_t reason) BUILD_ASSERT(CONFIG_BT_MAX_CONN == 1, "This test assumes a single link."); static void connected(struct bt_conn *conn, uint8_t err) { - ASSERT((!g_conn || (conn == g_conn)), "Unexpected new connection."); + TEST_ASSERT((!g_conn || (conn == g_conn)), "Unexpected new connection."); if (!g_conn) { g_conn = bt_conn_ref(conn); @@ -63,8 +46,9 @@ static void connected(struct bt_conn *conn, uint8_t err) SET_FLAG(flag_is_connected); - if (GET_FLAG(call_bt_conn_set_bondable) && bt_conn_set_bondable(conn, GET_FLAG(bondable))) { - ASSERT(0, "Fail during setting bondable flag for given connection."); + if (IS_FLAG_SET(call_bt_conn_set_bondable) && + bt_conn_set_bondable(conn, IS_FLAG_SET(bondable))) { + TEST_ASSERT(0, "Fail during setting bondable flag for given connection."); } } @@ -79,7 +63,7 @@ void clear_g_conn(void) conn = g_conn; g_conn = NULL; - ASSERT(conn, "Test error: No g_conn!\n"); + TEST_ASSERT(conn, "Test error: No g_conn!"); bt_conn_unref(conn); } @@ -90,7 +74,7 @@ DEFINE_FLAG(flag_not_bonded); static void pairing_failed(struct bt_conn *conn, enum bt_security_err reason) { - FAIL("Pairing failed (unexpected): reason %u\n", reason); + TEST_FAIL("Pairing failed (unexpected): reason %u", reason); } static void pairing_complete(struct bt_conn *conn, bool bonded) @@ -114,9 +98,9 @@ void bs_bt_utils_setup(void) int err; err = bt_enable(NULL); - ASSERT(!err, "bt_enable failed.\n"); + TEST_ASSERT(!err, "bt_enable failed."); err = bt_conn_auth_info_cb_register(&bt_conn_auth_info_cb); - ASSERT(!err, "bt_conn_auth_info_cb_register failed.\n"); + TEST_ASSERT(!err, "bt_conn_auth_info_cb_register failed."); } static void scan_connect_to_first_result__device_found(const bt_addr_le_t *addr, int8_t rssi, @@ -131,17 +115,17 @@ static void scan_connect_to_first_result__device_found(const bt_addr_le_t *addr, /* We're only interested in connectable events */ if (type != BT_HCI_ADV_IND && type != BT_HCI_ADV_DIRECT_IND) { - FAIL("Unexpected advertisement type."); + TEST_FAIL("Unexpected advertisement type."); } bt_addr_le_to_str(addr, addr_str, sizeof(addr_str)); printk("Got scan result, connecting.. dst %s, RSSI %d\n", addr_str, rssi); err = bt_le_scan_stop(); - ASSERT(!err, "Err bt_le_scan_stop %d", err); + TEST_ASSERT(!err, "Err bt_le_scan_stop %d", err); err = bt_conn_le_create(addr, BT_CONN_LE_CREATE_CONN, BT_LE_CONN_PARAM_DEFAULT, &g_conn); - ASSERT(!err, "Err bt_conn_le_create %d", err); + TEST_ASSERT(!err, "Err bt_conn_le_create %d", err); } void scan_connect_to_first_result(void) @@ -149,7 +133,7 @@ void scan_connect_to_first_result(void) int err; err = bt_le_scan_start(BT_LE_SCAN_PASSIVE, scan_connect_to_first_result__device_found); - ASSERT(!err, "Err bt_le_scan_start %d", err); + TEST_ASSERT(!err, "Err bt_le_scan_start %d", err); } void disconnect(void) @@ -157,7 +141,7 @@ void disconnect(void) int err; err = bt_conn_disconnect(g_conn, BT_HCI_ERR_REMOTE_USER_TERM_CONN); - ASSERT(!err, "Err bt_conn_disconnect %d", err); + TEST_ASSERT(!err, "Err bt_conn_disconnect %d", err); } void unpair(int id) @@ -165,7 +149,7 @@ void unpair(int id) int err; err = bt_unpair(id, BT_ADDR_LE_ANY); - ASSERT(!err, "Err bt_unpair %d", err); + TEST_ASSERT(!err, "Err bt_unpair %d", err); } void set_security(bt_security_t sec) @@ -173,7 +157,7 @@ void set_security(bt_security_t sec) int err; err = bt_conn_set_security(g_conn, sec); - ASSERT(!err, "Err bt_conn_set_security %d", err); + TEST_ASSERT(!err, "Err bt_conn_set_security %d", err); } void advertise_connectable(int id, bt_addr_le_t *directed_dst) @@ -192,7 +176,7 @@ void advertise_connectable(int id, bt_addr_le_t *directed_dst) } err = bt_le_adv_start(¶m, NULL, 0, NULL, 0); - ASSERT(err == 0, "Advertising failed to start (err %d)\n", err); + TEST_ASSERT(err == 0, "Advertising failed to start (err %d)", err); } void set_bondable(bool enable) diff --git a/tests/bsim/bluetooth/host/security/bond_per_connection/src/bs_bt_utils.h b/tests/bsim/bluetooth/host/security/bond_per_connection/src/bs_bt_utils.h index c16d9cb96769..44068911be5b 100644 --- a/tests/bsim/bluetooth/host/security/bond_per_connection/src/bs_bt_utils.h +++ b/tests/bsim/bluetooth/host/security/bond_per_connection/src/bs_bt_utils.h @@ -24,48 +24,8 @@ #include #include -extern enum bst_result_t bst_result; - -#define DECLARE_FLAG(flag) extern atomic_t flag -#define DEFINE_FLAG(flag) atomic_t flag = (atomic_t) false -#define SET_FLAG(flag) (void)atomic_set(&flag, (atomic_t) true) -#define UNSET_FLAG(flag) (void)atomic_set(&flag, (atomic_t) false) -#define WAIT_FOR_FLAG(flag) \ - while (!(bool)atomic_get(&flag)) { \ - (void)k_sleep(K_MSEC(1)); \ - } -#define WAIT_FOR_FLAG_UNSET(flag) \ - while ((bool)atomic_get(&flag)) { \ - (void)k_sleep(K_MSEC(1)); \ - } -#define TAKE_FLAG(flag) \ - while (!(bool)atomic_cas(&flag, true, false)) { \ - (void)k_sleep(K_MSEC(1)); \ - } -#define GET_FLAG(flag) \ - (bool)atomic_get(&flag) - -#define ASSERT(expr, ...) \ - do { \ - if (!(expr)) { \ - FAIL(__VA_ARGS__); \ - } \ - } while (0) - -#define FAIL(...) \ - do { \ - bst_result = Failed; \ - bs_trace_error_time_line(__VA_ARGS__); \ - } while (0) - -#define PASS(...) \ - do { \ - bst_result = Passed; \ - bs_trace_info_time(1, __VA_ARGS__); \ - } while (0) - -void test_tick(bs_time_t HW_device_time); -void test_init(void); +#include "babblekit/testcase.h" +#include "babblekit/flags.h" DECLARE_FLAG(flag_pairing_complete); DECLARE_FLAG(flag_bonded); diff --git a/tests/bsim/bluetooth/host/security/bond_per_connection/src/central.c b/tests/bsim/bluetooth/host/security/bond_per_connection/src/central.c index 4e0052310d3a..bfdb1508e7f7 100644 --- a/tests/bsim/bluetooth/host/security/bond_per_connection/src/central.c +++ b/tests/bsim/bluetooth/host/security/bond_per_connection/src/central.c @@ -12,6 +12,9 @@ #include +#include "babblekit/testcase.h" +#include "babblekit/flags.h" + void central(void) { bs_bt_utils_setup(); @@ -51,5 +54,5 @@ void central(void) wait_disconnected(); clear_g_conn(); - PASS("PASS\n"); + TEST_PASS("PASS"); } diff --git a/tests/bsim/bluetooth/host/security/bond_per_connection/src/main.c b/tests/bsim/bluetooth/host/security/bond_per_connection/src/main.c index 1b797f104c70..2a39920435bb 100644 --- a/tests/bsim/bluetooth/host/security/bond_per_connection/src/main.c +++ b/tests/bsim/bluetooth/host/security/bond_per_connection/src/main.c @@ -13,14 +13,10 @@ void peripheral(void); static const struct bst_test_instance test_to_add[] = { { .test_id = "central", - .test_pre_init_f = test_init, - .test_tick_f = test_tick, .test_main_f = central, }, { .test_id = "peripheral", - .test_pre_init_f = test_init, - .test_tick_f = test_tick, .test_main_f = peripheral, }, BSTEST_END_MARKER, diff --git a/tests/bsim/bluetooth/host/security/bond_per_connection/src/peripheral.c b/tests/bsim/bluetooth/host/security/bond_per_connection/src/peripheral.c index 04fc3cdd8f2d..81d0e9a7470a 100644 --- a/tests/bsim/bluetooth/host/security/bond_per_connection/src/peripheral.c +++ b/tests/bsim/bluetooth/host/security/bond_per_connection/src/peripheral.c @@ -13,6 +13,9 @@ #include #include +#include "babblekit/testcase.h" +#include "babblekit/flags.h" + void peripheral(void) { bs_bt_utils_setup(); @@ -21,10 +24,10 @@ void peripheral(void) int id_b; id_a = bt_id_create(NULL, NULL); - ASSERT(id_a >= 0, "bt_id_create id_a failed (err %d)\n", id_a); + TEST_ASSERT(id_a >= 0, "bt_id_create id_a failed (err %d)", id_a); id_b = bt_id_create(NULL, NULL); - ASSERT(id_b >= 0, "bt_id_create id_b failed (err %d)\n", id_b); + TEST_ASSERT(id_b >= 0, "bt_id_create id_b failed (err %d)", id_b); printk("== Bonding id a - global bondable mode ==\n"); BUILD_ASSERT(CONFIG_BT_BONDABLE, "CONFIG_BT_BONDABLE must be enabled by default."); @@ -58,5 +61,5 @@ void peripheral(void) TAKE_FLAG(flag_pairing_complete); TAKE_FLAG(flag_not_bonded); - PASS("PASS\n"); + TEST_PASS("PASS"); } diff --git a/tests/bsim/bluetooth/host/security/ccc_update/src/central.c b/tests/bsim/bluetooth/host/security/ccc_update/src/central.c index 40adf5efb298..8f067641ac3c 100644 --- a/tests/bsim/bluetooth/host/security/ccc_update/src/central.c +++ b/tests/bsim/bluetooth/host/security/ccc_update/src/central.c @@ -29,9 +29,9 @@ #define CLIENT_CLIENT_CHAN 0 #define SERVER_CLIENT_CHAN 1 -static DEFINE_FLAG(connected_flag); -static DEFINE_FLAG(disconnected_flag); -static DEFINE_FLAG(security_updated_flag); +DEFINE_FLAG_STATIC(connected_flag); +DEFINE_FLAG_STATIC(disconnected_flag); +DEFINE_FLAG_STATIC(security_updated_flag); #define BT_UUID_DUMMY_SERVICE BT_UUID_DECLARE_128(DUMMY_SERVICE_TYPE) #define BT_UUID_DUMMY_SERVICE_NOTIFY BT_UUID_DECLARE_128(DUMMY_SERVICE_NOTIFY_TYPE) @@ -40,7 +40,7 @@ static struct bt_conn *default_conn; static struct bt_conn_cb central_cb; -static DEFINE_FLAG(gatt_write_flag); +DEFINE_FLAG_STATIC(gatt_write_flag); static uint8_t gatt_write_att_err; static void gatt_write_cb(struct bt_conn *conn, uint8_t att_err, diff --git a/tests/bsim/bluetooth/host/security/ccc_update/src/peripheral.c b/tests/bsim/bluetooth/host/security/ccc_update/src/peripheral.c index dae03f846a28..10cb4210c222 100644 --- a/tests/bsim/bluetooth/host/security/ccc_update/src/peripheral.c +++ b/tests/bsim/bluetooth/host/security/ccc_update/src/peripheral.c @@ -27,11 +27,10 @@ #define GOOD_CLIENT_CHAN 0 #define BAD_CLIENT_CHAN 1 -static DEFINE_FLAG(connected_flag); -static DEFINE_FLAG(disconnected_flag); -static DEFINE_FLAG(security_updated_flag); - -static DEFINE_FLAG(ccc_cfg_changed_flag); +DEFINE_FLAG_STATIC(connected_flag); +DEFINE_FLAG_STATIC(disconnected_flag); +DEFINE_FLAG_STATIC(security_updated_flag); +DEFINE_FLAG_STATIC(ccc_cfg_changed_flag); static const struct bt_uuid_128 dummy_service = BT_UUID_INIT_128(DUMMY_SERVICE_TYPE); diff --git a/tests/bsim/bluetooth/host/security/id_addr_update/central/CMakeLists.txt b/tests/bsim/bluetooth/host/security/id_addr_update/central/CMakeLists.txt index 0ca6f4313bd6..eb720e642f4e 100644 --- a/tests/bsim/bluetooth/host/security/id_addr_update/central/CMakeLists.txt +++ b/tests/bsim/bluetooth/host/security/id_addr_update/central/CMakeLists.txt @@ -5,6 +5,9 @@ cmake_minimum_required(VERSION 3.20.0) find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) project(bsim_test_id_addr_update) +add_subdirectory(${ZEPHYR_BASE}/tests/bsim/babblekit babblekit) +target_link_libraries(app PRIVATE babblekit) + target_sources(app PRIVATE src/central.c src/utils.c) diff --git a/tests/bsim/bluetooth/host/security/id_addr_update/central/src/central.c b/tests/bsim/bluetooth/host/security/id_addr_update/central/src/central.c index 7ccaed1a65d4..e64196648a4e 100644 --- a/tests/bsim/bluetooth/host/security/id_addr_update/central/src/central.c +++ b/tests/bsim/bluetooth/host/security/id_addr_update/central/src/central.c @@ -13,6 +13,8 @@ #include +#include "babblekit/testcase.h" + #include LOG_MODULE_REGISTER(central, LOG_LEVEL_INF); @@ -60,14 +62,12 @@ void central(void) wait_disconnected(); clear_conn(conn_b); - PASS("PASS\n"); + TEST_PASS("PASS"); } static const struct bst_test_instance test_to_add[] = { { .test_id = "central", - .test_pre_init_f = test_init, - .test_tick_f = test_tick, .test_main_f = central, }, BSTEST_END_MARKER, diff --git a/tests/bsim/bluetooth/host/security/id_addr_update/central/src/utils.c b/tests/bsim/bluetooth/host/security/id_addr_update/central/src/utils.c index a1ccc4f6996c..ff4b104b584d 100644 --- a/tests/bsim/bluetooth/host/security/id_addr_update/central/src/utils.c +++ b/tests/bsim/bluetooth/host/security/id_addr_update/central/src/utils.c @@ -6,27 +6,14 @@ #include "bs_bt_utils.h" #include "utils.h" +#include "babblekit/testcase.h" +#include "babblekit/flags.h" BUILD_ASSERT(CONFIG_BT_MAX_PAIRED >= 2, "CONFIG_BT_MAX_PAIRED is too small."); BUILD_ASSERT(CONFIG_BT_ID_MAX >= 3, "CONFIG_BT_ID_MAX is too small."); BUILD_ASSERT(CONFIG_BT_MAX_CONN == 2, "CONFIG_BT_MAX_CONN should be equal to two."); BUILD_ASSERT(CONFIG_BT_GATT_CLIENT, "CONFIG_BT_GATT_CLIENT is disabled."); -void test_tick(bs_time_t HW_device_time) -{ - bs_trace_debug_time(0, "Simulation ends now.\n"); - if (bst_result != Passed) { - bst_result = Failed; - bs_trace_error("Test did not pass before simulation ended.\n"); - } -} - -void test_init(void) -{ - bst_ticker_set_next_tick_absolute(TEST_TIMEOUT_SIMULATED); - bst_result = In_progress; -} - DEFINE_FLAG(flag_has_new_conn); struct bt_conn *new_conn; @@ -38,7 +25,7 @@ void clear_conn(struct bt_conn *conn) new_conn = NULL; } - ASSERT(conn, "Test error: No new_conn!\n"); + TEST_ASSERT(conn, "Test error: No new_conn!"); bt_conn_unref(conn); } @@ -47,7 +34,7 @@ void wait_connected(struct bt_conn **conn) WAIT_FOR_FLAG(flag_has_new_conn); UNSET_FLAG(flag_has_new_conn); - ASSERT(new_conn, "connection unpopulated."); + TEST_ASSERT(new_conn, "connection unpopulated."); *conn = new_conn; new_conn = NULL; } @@ -65,7 +52,7 @@ static void print_conn_state_transition(const char *prefix, struct bt_conn *conn char addr_str[BT_ADDR_LE_STR_LEN]; err = bt_conn_get_info(conn, &info); - ASSERT(!err, "Unexpected conn info result."); + TEST_ASSERT(!err, "Unexpected conn info result."); bt_addr_le_to_str(info.le.dst, addr_str, sizeof(addr_str)); printk("%s: %s\n", prefix, addr_str); @@ -79,7 +66,7 @@ static void disconnected(struct bt_conn *conn, uint8_t reason) static void connected(struct bt_conn *conn, uint8_t err) { - ASSERT((!new_conn || (conn == new_conn)), "Unexpected new connection."); + TEST_ASSERT((!new_conn || (conn == new_conn)), "Unexpected new connection."); if (!new_conn) { new_conn = bt_conn_ref(conn); @@ -116,7 +103,7 @@ void set_security(struct bt_conn *conn, bt_security_t sec) int err; err = bt_conn_set_security(conn, sec); - ASSERT(!err, "Err bt_conn_set_security %d", err); + TEST_ASSERT(!err, "Err bt_conn_set_security %d", err); } void wait_pairing_completed(void) @@ -130,12 +117,12 @@ void bs_bt_utils_setup(void) int err; err = bt_enable(NULL); - ASSERT(!err, "bt_enable failed.\n"); + TEST_ASSERT(!err, "bt_enable failed."); err = bt_conn_auth_info_cb_register(&bt_conn_auth_info_cb); - ASSERT(!err, "bt_conn_auth_info_cb_register failed.\n"); + TEST_ASSERT(!err, "bt_conn_auth_info_cb_register failed."); err = settings_load(); - ASSERT(!err, "settings_load failed.\n"); + TEST_ASSERT(!err, "settings_load failed."); } static void scan_connect_to_first_result__device_found(const bt_addr_le_t *addr, int8_t rssi, @@ -146,17 +133,17 @@ static void scan_connect_to_first_result__device_found(const bt_addr_le_t *addr, /* We're only interested in connectable events */ if (type != BT_HCI_ADV_IND && type != BT_HCI_ADV_DIRECT_IND) { - FAIL("Unexpected advertisement type."); + TEST_FAIL("Unexpected advertisement type."); } bt_addr_le_to_str(addr, addr_str, sizeof(addr_str)); printk("Got scan result, connecting.. dst %s, RSSI %d\n", addr_str, rssi); err = bt_le_scan_stop(); - ASSERT(!err, "Err bt_le_scan_stop %d", err); + TEST_ASSERT(!err, "Err bt_le_scan_stop %d", err); err = bt_conn_le_create(addr, BT_CONN_LE_CREATE_CONN, BT_LE_CONN_PARAM_DEFAULT, &new_conn); - ASSERT(!err, "Err bt_conn_le_create %d", err); + TEST_ASSERT(!err, "Err bt_conn_le_create %d", err); } void scan_connect_to_first_result(void) @@ -164,7 +151,7 @@ void scan_connect_to_first_result(void) int err; err = bt_le_scan_start(BT_LE_SCAN_PASSIVE, scan_connect_to_first_result__device_found); - ASSERT(!err, "Err bt_le_scan_start %d", err); + TEST_ASSERT(!err, "Err bt_le_scan_start %d", err); } void disconnect(struct bt_conn *conn) @@ -172,7 +159,7 @@ void disconnect(struct bt_conn *conn) int err; err = bt_conn_disconnect(conn, BT_HCI_ERR_REMOTE_USER_TERM_CONN); - ASSERT(!err, "Err bt_conn_disconnect %d", err); + TEST_ASSERT(!err, "Err bt_conn_disconnect %d", err); } DEFINE_FLAG(flag_bas_has_notification); @@ -223,5 +210,5 @@ void bas_subscribe(struct bt_conn *conn) subscribe_params.notify = bas_notify_func; err = bt_gatt_subscribe(conn, &subscribe_params); - ASSERT(!err, "bt_gatt_subscribe failed (err %d)\n", err); + TEST_ASSERT(!err, "bt_gatt_subscribe failed (err %d)", err); } diff --git a/tests/bsim/bluetooth/host/security/id_addr_update/central/src/utils.h b/tests/bsim/bluetooth/host/security/id_addr_update/central/src/utils.h index af79f1658589..11b563b32195 100644 --- a/tests/bsim/bluetooth/host/security/id_addr_update/central/src/utils.h +++ b/tests/bsim/bluetooth/host/security/id_addr_update/central/src/utils.h @@ -1,9 +1,6 @@ #include #include -void test_tick(bs_time_t HW_device_time); -void test_init(void); - void bs_bt_utils_setup(void); void clear_conn(struct bt_conn *conn); diff --git a/tests/bsim/bluetooth/host/security/id_addr_update/common/bs_bt_utils.h b/tests/bsim/bluetooth/host/security/id_addr_update/common/bs_bt_utils.h index 2b4b4a489116..01f3edd13583 100644 --- a/tests/bsim/bluetooth/host/security/id_addr_update/common/bs_bt_utils.h +++ b/tests/bsim/bluetooth/host/security/id_addr_update/common/bs_bt_utils.h @@ -6,10 +6,6 @@ * SPDX-License-Identifier: Apache-2.0 */ -#include "bs_tracing.h" -#include "bs_types.h" -#include "bstests.h" -#include "time_machine.h" #include #include @@ -25,52 +21,6 @@ #include #include -extern enum bst_result_t bst_result; - -#define BS_SECONDS(dur_sec) ((bs_time_t)dur_sec * 1000000) -#define TEST_TIMEOUT_SIMULATED BS_SECONDS(60) - -#define DECLARE_FLAG(flag) extern atomic_t flag -#define DEFINE_FLAG(flag) atomic_t flag = (atomic_t) false -#define SET_FLAG(flag) (void)atomic_set(&flag, (atomic_t) true) -#define UNSET_FLAG(flag) (void)atomic_set(&flag, (atomic_t) false) -#define WAIT_FOR_FLAG(flag) \ - while (!(bool)atomic_get(&flag)) { \ - (void)k_sleep(K_MSEC(1)); \ - } -#define WAIT_FOR_FLAG_UNSET(flag) \ - while ((bool)atomic_get(&flag)) { \ - (void)k_sleep(K_MSEC(1)); \ - } -#define TAKE_FLAG(flag) \ - while (!(bool)atomic_cas(&flag, true, false)) { \ - (void)k_sleep(K_MSEC(1)); \ - } -#define GET_FLAG(flag) \ - (bool)atomic_get(&flag) - -#define ASSERT(expr, ...) \ - do { \ - if (!(expr)) { \ - FAIL(__VA_ARGS__); \ - } \ - } while (0) - -#define FAIL(...) \ - do { \ - bst_result = Failed; \ - bs_trace_error_time_line(__VA_ARGS__); \ - } while (0) - -#define PASS(...) \ - do { \ - bst_result = Passed; \ - bs_trace_info_time(1, __VA_ARGS__); \ - } while (0) - -void test_tick(bs_time_t HW_device_time); -void test_init(void); - void bs_bt_utils_setup(void); void clear_conn(struct bt_conn *conn); diff --git a/tests/bsim/bluetooth/host/security/id_addr_update/peripheral/CMakeLists.txt b/tests/bsim/bluetooth/host/security/id_addr_update/peripheral/CMakeLists.txt index 5ea3558fa642..1dcecd4459a6 100644 --- a/tests/bsim/bluetooth/host/security/id_addr_update/peripheral/CMakeLists.txt +++ b/tests/bsim/bluetooth/host/security/id_addr_update/peripheral/CMakeLists.txt @@ -5,6 +5,9 @@ cmake_minimum_required(VERSION 3.20.0) find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) project(bsim_test_id_addr_update) +add_subdirectory(${ZEPHYR_BASE}/tests/bsim/babblekit babblekit) +target_link_libraries(app PRIVATE babblekit) + target_sources(app PRIVATE src/peripheral.c src/utils.c diff --git a/tests/bsim/bluetooth/host/security/id_addr_update/peripheral/src/peripheral.c b/tests/bsim/bluetooth/host/security/id_addr_update/peripheral/src/peripheral.c index b481b1623c8c..368b8ebb74fc 100644 --- a/tests/bsim/bluetooth/host/security/id_addr_update/peripheral/src/peripheral.c +++ b/tests/bsim/bluetooth/host/security/id_addr_update/peripheral/src/peripheral.c @@ -14,6 +14,8 @@ #include #include +#include "babblekit/testcase.h" + #include LOG_MODULE_REGISTER(peripheral, LOG_LEVEL_INF); @@ -24,12 +26,12 @@ static void verify_equal_address(struct bt_conn *conn_a, struct bt_conn *conn_b) struct bt_conn_info info_b; err = bt_conn_get_info(conn_a, &info_a); - ASSERT(!err, "Unexpected info_a result."); + TEST_ASSERT(!err, "Unexpected info_a result."); err = bt_conn_get_info(conn_b, &info_b); - ASSERT(!err, "Unexpected info_b result."); + TEST_ASSERT(!err, "Unexpected info_b result."); - ASSERT(bt_addr_le_eq(info_a.le.dst, info_b.le.dst), + TEST_ASSERT(bt_addr_le_eq(info_a.le.dst, info_b.le.dst), "Conn A address is not equal with the conn B address"); } @@ -45,10 +47,10 @@ void peripheral(void) /* Create two identities that will simultaneously connect with the same central peer. */ id_a = bt_id_create(NULL, NULL); - ASSERT(id_a >= 0, "bt_id_create id_a failed (err %d)\n", id_a); + TEST_ASSERT(id_a >= 0, "bt_id_create id_a failed (err %d)", id_a); id_b = bt_id_create(NULL, NULL); - ASSERT(id_b >= 0, "bt_id_create id_b failed (err %d)\n", id_b); + TEST_ASSERT(id_b >= 0, "bt_id_create id_b failed (err %d)", id_b); /* Connect with the first identity. */ LOG_INF("adv"); @@ -82,14 +84,12 @@ void peripheral(void) wait_disconnected(); clear_conn(conn_b); - PASS("PASS\n"); + TEST_PASS("PASS"); } static const struct bst_test_instance test_to_add[] = { { .test_id = "peripheral", - .test_pre_init_f = test_init, - .test_tick_f = test_tick, .test_main_f = peripheral, }, BSTEST_END_MARKER, diff --git a/tests/bsim/bluetooth/host/security/id_addr_update/peripheral/src/utils.c b/tests/bsim/bluetooth/host/security/id_addr_update/peripheral/src/utils.c index 83c34361d369..fd513049d042 100644 --- a/tests/bsim/bluetooth/host/security/id_addr_update/peripheral/src/utils.c +++ b/tests/bsim/bluetooth/host/security/id_addr_update/peripheral/src/utils.c @@ -6,26 +6,13 @@ #include "bs_bt_utils.h" #include "utils.h" +#include "babblekit/testcase.h" +#include "babblekit/flags.h" BUILD_ASSERT(CONFIG_BT_MAX_PAIRED >= 2, "CONFIG_BT_MAX_PAIRED is too small."); BUILD_ASSERT(CONFIG_BT_ID_MAX >= 3, "CONFIG_BT_ID_MAX is too small."); BUILD_ASSERT(CONFIG_BT_MAX_CONN == 2, "CONFIG_BT_MAX_CONN should be equal to two."); -void test_tick(bs_time_t HW_device_time) -{ - bs_trace_debug_time(0, "Simulation ends now.\n"); - if (bst_result != Passed) { - bst_result = Failed; - bs_trace_error("Test did not pass before simulation ended.\n"); - } -} - -void test_init(void) -{ - bst_ticker_set_next_tick_absolute(TEST_TIMEOUT_SIMULATED); - bst_result = In_progress; -} - DEFINE_FLAG(flag_has_new_conn); struct bt_conn *new_conn; @@ -37,7 +24,7 @@ void clear_conn(struct bt_conn *conn) new_conn = NULL; } - ASSERT(conn, "Test error: No new_conn!\n"); + TEST_ASSERT(conn, "Test error: No new_conn!"); bt_conn_unref(conn); } @@ -46,7 +33,7 @@ void wait_connected(struct bt_conn **conn) WAIT_FOR_FLAG(flag_has_new_conn); UNSET_FLAG(flag_has_new_conn); - ASSERT(new_conn, "connection unpopulated."); + TEST_ASSERT(new_conn, "connection unpopulated."); *conn = new_conn; new_conn = NULL; } @@ -64,7 +51,7 @@ static void print_conn_state_transition(const char *prefix, struct bt_conn *conn char addr_str[BT_ADDR_LE_STR_LEN]; err = bt_conn_get_info(conn, &info); - ASSERT(!err, "Unexpected conn info result."); + TEST_ASSERT(!err, "Unexpected conn info result."); bt_addr_le_to_str(info.le.dst, addr_str, sizeof(addr_str)); printk("%s: %s\n", prefix, addr_str); @@ -78,7 +65,7 @@ static void disconnected(struct bt_conn *conn, uint8_t reason) static void connected(struct bt_conn *conn, uint8_t err) { - ASSERT((!new_conn || (conn == new_conn)), "Unexpected new connection."); + TEST_ASSERT((!new_conn || (conn == new_conn)), "Unexpected new connection."); if (!new_conn) { new_conn = bt_conn_ref(conn); @@ -115,7 +102,7 @@ void set_security(struct bt_conn *conn, bt_security_t sec) int err; err = bt_conn_set_security(conn, sec); - ASSERT(!err, "Err bt_conn_set_security %d", err); + TEST_ASSERT(!err, "Err bt_conn_set_security %d", err); } void wait_pairing_completed(void) @@ -129,12 +116,12 @@ void bs_bt_utils_setup(void) int err; err = bt_enable(NULL); - ASSERT(!err, "bt_enable failed.\n"); + TEST_ASSERT(!err, "bt_enable failed."); err = bt_conn_auth_info_cb_register(&bt_conn_auth_info_cb); - ASSERT(!err, "bt_conn_auth_info_cb_register failed.\n"); + TEST_ASSERT(!err, "bt_conn_auth_info_cb_register failed."); err = settings_load(); - ASSERT(!err, "settings_load failed.\n"); + TEST_ASSERT(!err, "settings_load failed."); } void disconnect(struct bt_conn *conn) @@ -142,7 +129,7 @@ void disconnect(struct bt_conn *conn) int err; err = bt_conn_disconnect(conn, BT_HCI_ERR_REMOTE_USER_TERM_CONN); - ASSERT(!err, "Err bt_conn_disconnect %d", err); + TEST_ASSERT(!err, "Err bt_conn_disconnect %d", err); } void advertise_connectable(int id) @@ -156,7 +143,7 @@ void advertise_connectable(int id) param.options |= BT_LE_ADV_OPT_CONN; err = bt_le_adv_start(¶m, NULL, 0, NULL, 0); - ASSERT(!err, "Advertising failed to start (err %d)\n", err); + TEST_ASSERT(!err, "Advertising failed to start (err %d)", err); } DEFINE_FLAG(flag_bas_ccc_subscribed); @@ -203,7 +190,7 @@ void bas_notify(struct bt_conn *conn) int err; err = bt_gatt_notify(conn, &bas.attrs[2], &bas_level, sizeof(bas_level)); - ASSERT(!err, "bt_gatt_notify failed (err %d)\n", err); + TEST_ASSERT(!err, "bt_gatt_notify failed (err %d)", err); } DEFINE_FLAG(flag_bas_has_notification); diff --git a/tests/bsim/bluetooth/host/security/id_addr_update/peripheral/src/utils.h b/tests/bsim/bluetooth/host/security/id_addr_update/peripheral/src/utils.h index 01690b2c82ce..975c3f16a481 100644 --- a/tests/bsim/bluetooth/host/security/id_addr_update/peripheral/src/utils.h +++ b/tests/bsim/bluetooth/host/security/id_addr_update/peripheral/src/utils.h @@ -1,9 +1,6 @@ #include #include -void test_tick(bs_time_t HW_device_time); -void test_init(void); - void bs_bt_utils_setup(void); void clear_conn(struct bt_conn *conn); diff --git a/tests/bsim/bluetooth/host/security/security_changed_callback/CMakeLists.txt b/tests/bsim/bluetooth/host/security/security_changed_callback/CMakeLists.txt index e6dcda9d49a7..e0f557ddbc4f 100644 --- a/tests/bsim/bluetooth/host/security/security_changed_callback/CMakeLists.txt +++ b/tests/bsim/bluetooth/host/security/security_changed_callback/CMakeLists.txt @@ -5,6 +5,9 @@ cmake_minimum_required(VERSION 3.20.0) find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) project(bsim_test_security_changed_callback) +add_subdirectory(${ZEPHYR_BASE}/tests/bsim/babblekit babblekit) +target_link_libraries(app PRIVATE babblekit) + target_sources(app PRIVATE src/bs_bt_utils.c src/central.c diff --git a/tests/bsim/bluetooth/host/security/security_changed_callback/src/bs_bt_utils.c b/tests/bsim/bluetooth/host/security/security_changed_callback/src/bs_bt_utils.c index 6f7f379853b1..241dc5b9bf07 100644 --- a/tests/bsim/bluetooth/host/security/security_changed_callback/src/bs_bt_utils.c +++ b/tests/bsim/bluetooth/host/security/security_changed_callback/src/bs_bt_utils.c @@ -9,25 +9,10 @@ #include #include -LOG_MODULE_REGISTER(bs_bt_utils, LOG_LEVEL_DBG); - -#define BS_SECONDS(dur_sec) ((bs_time_t)dur_sec * 1000000) -#define TEST_TIMEOUT_SIMULATED BS_SECONDS(60) +#include "babblekit/testcase.h" +#include "babblekit/flags.h" -void test_tick(bs_time_t HW_device_time) -{ - bs_trace_debug_time(0, "Simulation ends now.\n"); - if (bst_result != Passed) { - bst_result = Failed; - bs_trace_error("Test did not pass before simulation ended.\n"); - } -} - -void test_init(void) -{ - bst_ticker_set_next_tick_absolute(TEST_TIMEOUT_SIMULATED); - bst_result = In_progress; -} +LOG_MODULE_REGISTER(bs_bt_utils, LOG_LEVEL_DBG); DEFINE_FLAG(flag_is_connected); struct bt_conn *g_conn; @@ -55,7 +40,7 @@ static void disconnected(struct bt_conn *conn, uint8_t reason) static void connected(struct bt_conn *conn, uint8_t err) { - ASSERT((!g_conn || (conn == g_conn)), "Unexpected new connection."); + TEST_ASSERT((!g_conn || (conn == g_conn)), "Unexpected new connection."); if (!g_conn) { g_conn = bt_conn_ref(conn); @@ -81,7 +66,7 @@ void clear_g_conn(void) conn = g_conn; g_conn = NULL; - ASSERT(conn, "Test error: No g_conn!\n"); + TEST_ASSERT(conn, "Test error: No g_conn!\n"); bt_conn_unref(conn); } @@ -119,13 +104,13 @@ void bs_bt_utils_setup(void) int err; err = bt_enable(NULL); - ASSERT(!err, "bt_enable failed.\n"); + TEST_ASSERT(!err, "bt_enable failed."); err = bt_conn_auth_info_cb_register(&bt_conn_auth_info_cb); - ASSERT(!err, "bt_conn_auth_info_cb_register failed.\n"); + TEST_ASSERT(!err, "bt_conn_auth_info_cb_register failed."); err = settings_load(); if (err) { - FAIL("Settings load failed (err %d)\n", err); + TEST_FAIL("Settings load failed (err %d)", err); } } @@ -145,10 +130,10 @@ static void stop_scan_and_connect(const bt_addr_le_t *addr, printk("Got scan result, connecting.. dst %s, RSSI %d\n", addr_str, rssi); err = bt_le_scan_stop(); - ASSERT(!err, "Err bt_le_scan_stop %d", err); + TEST_ASSERT(!err, "Err bt_le_scan_stop %d", err); err = bt_conn_le_create(addr, BT_CONN_LE_CREATE_CONN, BT_LE_CONN_PARAM_DEFAULT, &g_conn); - ASSERT(!err, "Err bt_conn_le_create %d", err); + TEST_ASSERT(!err, "Err bt_conn_le_create %d", err); } void scan_connect_to_first_result(void) @@ -156,7 +141,7 @@ void scan_connect_to_first_result(void) int err; err = bt_le_scan_start(BT_LE_SCAN_PASSIVE, stop_scan_and_connect); - ASSERT(!err, "Err bt_le_scan_start %d", err); + TEST_ASSERT(!err, "Err bt_le_scan_start %d", err); } void set_security(bt_security_t sec) @@ -164,7 +149,7 @@ void set_security(bt_security_t sec) int err; err = bt_conn_set_security(g_conn, sec); - ASSERT(!err, "Err bt_conn_set_security %d", err); + TEST_ASSERT(!err, "Err bt_conn_set_security %d", err); } void advertise_connectable(int id, bt_addr_le_t *directed_dst) @@ -183,5 +168,5 @@ void advertise_connectable(int id, bt_addr_le_t *directed_dst) } err = bt_le_adv_start(¶m, NULL, 0, NULL, 0); - ASSERT(err == 0, "Advertising failed to start (err %d)\n", err); + TEST_ASSERT(err == 0, "Advertising failed to start (err %d)", err); } diff --git a/tests/bsim/bluetooth/host/security/security_changed_callback/src/bs_bt_utils.h b/tests/bsim/bluetooth/host/security/security_changed_callback/src/bs_bt_utils.h index ed6cea0d5ef9..3491493c9909 100644 --- a/tests/bsim/bluetooth/host/security/security_changed_callback/src/bs_bt_utils.h +++ b/tests/bsim/bluetooth/host/security/security_changed_callback/src/bs_bt_utils.h @@ -6,10 +6,6 @@ * SPDX-License-Identifier: Apache-2.0 */ -#include "bs_tracing.h" -#include "bs_types.h" -#include "bstests.h" - #include #include #include @@ -22,47 +18,8 @@ #include #include -extern enum bst_result_t bst_result; - -#define DECLARE_FLAG(flag) extern atomic_t flag -#define DEFINE_FLAG(flag) atomic_t flag = (atomic_t) false -#define SET_FLAG(flag) (void)atomic_set(&flag, (atomic_t) true) -#define UNSET_FLAG(flag) (void)atomic_set(&flag, (atomic_t) false) -#define WAIT_FOR_FLAG(flag) \ - while (!(bool)atomic_get(&flag)) { \ - (void)k_sleep(K_MSEC(1)); \ - } -#define WAIT_FOR_FLAG_UNSET(flag) \ - while ((bool)atomic_get(&flag)) { \ - (void)k_sleep(K_MSEC(1)); \ - } -#define TAKE_FLAG(flag) \ - while (!(bool)atomic_cas(&flag, true, false)) { \ - (void)k_sleep(K_MSEC(1)); \ - } -#define GET_FLAG(flag) (bool)atomic_get(&flag) - -#define ASSERT(expr, ...) \ - do { \ - if (!(expr)) { \ - FAIL(__VA_ARGS__); \ - } \ - } while (0) - -#define FAIL(...) \ - do { \ - bst_result = Failed; \ - bs_trace_error_time_line(__VA_ARGS__); \ - } while (0) - -#define PASS(...) \ - do { \ - bst_result = Passed; \ - bs_trace_info_time(1, __VA_ARGS__); \ - } while (0) - -void test_tick(bs_time_t HW_device_time); -void test_init(void); +#include "babblekit/testcase.h" +#include "babblekit/flags.h" DECLARE_FLAG(flag_pairing_complete); DECLARE_FLAG(flag_bonded); diff --git a/tests/bsim/bluetooth/host/security/security_changed_callback/src/central.c b/tests/bsim/bluetooth/host/security/security_changed_callback/src/central.c index abf896d86183..f03dca354f65 100644 --- a/tests/bsim/bluetooth/host/security/security_changed_callback/src/central.c +++ b/tests/bsim/bluetooth/host/security/security_changed_callback/src/central.c @@ -12,6 +12,8 @@ #include #include "bs_bt_utils.h" +#include "babblekit/testcase.h" +#include "babblekit/flags.h" LOG_MODULE_REGISTER(test_central, LOG_LEVEL_DBG); @@ -35,5 +37,5 @@ void central(void) clear_g_conn(); - PASS("PASS\n"); + TEST_PASS("PASS"); } diff --git a/tests/bsim/bluetooth/host/security/security_changed_callback/src/main.c b/tests/bsim/bluetooth/host/security/security_changed_callback/src/main.c index dcefa274297d..3a22ca41d2cf 100644 --- a/tests/bsim/bluetooth/host/security/security_changed_callback/src/main.c +++ b/tests/bsim/bluetooth/host/security/security_changed_callback/src/main.c @@ -14,20 +14,14 @@ void peripheral_disconnect_in_sec_cb(void); static const struct bst_test_instance test_to_add[] = { { .test_id = "central", - .test_pre_init_f = test_init, - .test_tick_f = test_tick, .test_main_f = central, }, { .test_id = "peripheral_unpair_in_sec_cb", - .test_pre_init_f = test_init, - .test_tick_f = test_tick, .test_main_f = peripheral_unpair_in_sec_cb, }, { .test_id = "peripheral_disconnect_in_sec_cb", - .test_pre_init_f = test_init, - .test_tick_f = test_tick, .test_main_f = peripheral_disconnect_in_sec_cb, }, BSTEST_END_MARKER, diff --git a/tests/bsim/bluetooth/host/security/security_changed_callback/src/peripheral.c b/tests/bsim/bluetooth/host/security/security_changed_callback/src/peripheral.c index 56c351efb05c..108f9d9fd574 100644 --- a/tests/bsim/bluetooth/host/security/security_changed_callback/src/peripheral.c +++ b/tests/bsim/bluetooth/host/security/security_changed_callback/src/peripheral.c @@ -13,6 +13,7 @@ #include #include "bs_bt_utils.h" +#include "babblekit/testcase.h" LOG_MODULE_REGISTER(test_peripheral, LOG_LEVEL_DBG); @@ -20,7 +21,7 @@ BUILD_ASSERT(CONFIG_BT_BONDABLE, "CONFIG_BT_BONDABLE must be enabled by default. static void pairing_complete_unpair(struct bt_conn *conn, bool bonded) { - FAIL("Pairing succeed\n"); + TEST_FAIL("Pairing succeed"); } static void peripheral_security_changed_unpair(struct bt_conn *conn, @@ -51,7 +52,7 @@ void peripheral_unpair_in_sec_cb(void) bt_conn_cb_register(&peripheral_cb); err = bt_conn_auth_info_cb_register(&peripheral_auth_info_cb); - ASSERT(!err, "bt_conn_auth_info_cb_register failed.\n"); + TEST_ASSERT(!err, "bt_conn_auth_info_cb_register failed."); advertise_connectable(BT_ID_DEFAULT, NULL); wait_connected(); @@ -60,12 +61,12 @@ void peripheral_unpair_in_sec_cb(void) clear_g_conn(); - PASS("PASS\n"); + TEST_PASS("PASS"); } static void pairing_failed_disconnect(struct bt_conn *conn, enum bt_security_err err) { - FAIL("Pairing failed\n"); + TEST_FAIL("Pairing failed"); } static void peripheral_security_changed_disconnect(struct bt_conn *conn, @@ -95,7 +96,7 @@ void peripheral_disconnect_in_sec_cb(void) bt_conn_cb_register(&peripheral_cb); err = bt_conn_auth_info_cb_register(&peripheral_auth_info_cb); - ASSERT(!err, "bt_conn_auth_info_cb_register failed.\n"); + TEST_ASSERT(!err, "bt_conn_auth_info_cb_register failed."); advertise_connectable(BT_ID_DEFAULT, NULL); wait_connected(); @@ -104,5 +105,5 @@ void peripheral_disconnect_in_sec_cb(void) clear_g_conn(); - PASS("PASS\n"); + TEST_PASS("PASS"); } diff --git a/tests/bsim/bluetooth/samples/battery_service/src/central_test.c b/tests/bsim/bluetooth/samples/battery_service/src/central_test.c index 958bbd99ba4f..832c6e4449ff 100644 --- a/tests/bsim/bluetooth/samples/battery_service/src/central_test.c +++ b/tests/bsim/bluetooth/samples/battery_service/src/central_test.c @@ -57,9 +57,9 @@ static struct bt_gatt_subscribe_params battery_critical_status_sub_params; #define BAS_BLS_IND_RECEIVED_COUNT 20 #define BAS_BLS_NTF_RECEIVED_COUNT 20 -static DEFINE_FLAG(notification_count_reached); -static DEFINE_FLAG(indication_count_reached); -static DEFINE_FLAG(bcs_char_read); +DEFINE_FLAG_STATIC(notification_count_reached); +DEFINE_FLAG_STATIC(indication_count_reached); +DEFINE_FLAG_STATIC(bcs_char_read); /* Callback for handling Battery Critical Status Read Response */ static uint8_t battery_critical_status_read_cb(struct bt_conn *conn, uint8_t err, From d0f05c3474a105fc84b615edc9760b51f4030f98 Mon Sep 17 00:00:00 2001 From: Pavel Vasilyev Date: Mon, 10 Feb 2025 15:16:50 +0100 Subject: [PATCH 45/49] [nrf fromtree] tests: bluetooth: tester: mesh: Increase CMD tx bufs Increase CONFIG_BT_BUF_CMD_TX_COUNT to avoid deadlock when running out of buffers. See #77241 for the reference. This fixes Mesh Provisioning Service tests (MESH/NODE/MPS). Signed-off-by: Pavel Vasilyev (cherry picked from commit 6c35d372dd201647fd7fc97e070da20421ad077b) Signed-off-by: alperen sener --- tests/bluetooth/tester/overlay-mesh.conf | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tests/bluetooth/tester/overlay-mesh.conf b/tests/bluetooth/tester/overlay-mesh.conf index 815f49524971..04e6c074e214 100644 --- a/tests/bluetooth/tester/overlay-mesh.conf +++ b/tests/bluetooth/tester/overlay-mesh.conf @@ -58,3 +58,6 @@ CONFIG_BT_MESH_COMP_PAGE_2=y CONFIG_BT_MESH_BRG_CFG_SRV=y CONFIG_BT_MESH_BRG_CFG_CLI=y CONFIG_SETTINGS=y + +# Increase the number of buffers to avoid deadlock when running out of buffers +CONFIG_BT_BUF_CMD_TX_COUNT=4 From e333a51319b5318c99bd98a787d899c1c6d89f67 Mon Sep 17 00:00:00 2001 From: alperen sener Date: Fri, 7 Feb 2025 09:57:37 +0100 Subject: [PATCH 46/49] [nrf fromtree] tests: bluetooth: tester: Make BTP CMD thread stack size kconfig Different test layers need different stack sizes for BTP CMD thread and also AutoPTS might need to overwrite this config based on test case. Increase the stack size for mesh tests. Signed-off-by: alperen sener (cherry picked from commit 3cab4fe0dc589a4babf3584021e7e5ee68f596e4) Signed-off-by: alperen sener --- tests/bluetooth/tester/Kconfig | 6 ++++++ tests/bluetooth/tester/overlay-mesh.conf | 1 + tests/bluetooth/tester/src/btp.c | 2 +- 3 files changed, 8 insertions(+), 1 deletion(-) diff --git a/tests/bluetooth/tester/Kconfig b/tests/bluetooth/tester/Kconfig index 5768a740441e..af0230b8dc80 100644 --- a/tests/bluetooth/tester/Kconfig +++ b/tests/bluetooth/tester/Kconfig @@ -9,6 +9,12 @@ module = BTTESTER module-str = bttester source "subsys/logging/Kconfig.template.log_config" +config BTTESTER_BTP_CMD_THREAD_STACK_SIZE + int "BTP CMD threads stack size." + default 2048 + help + Stack size in byte used by the BTP CMD thread. + endmenu source "Kconfig.zephyr" diff --git a/tests/bluetooth/tester/overlay-mesh.conf b/tests/bluetooth/tester/overlay-mesh.conf index 04e6c074e214..1377d93639a1 100644 --- a/tests/bluetooth/tester/overlay-mesh.conf +++ b/tests/bluetooth/tester/overlay-mesh.conf @@ -2,6 +2,7 @@ # PSA key slots; in order to make sure there is always # enough key slots allocating 32 slots. CONFIG_MBEDTLS_PSA_KEY_SLOT_COUNT=32 +CONFIG_BTTESTER_BTP_CMD_THREAD_STACK_SIZE=3072 CONFIG_BT_MESH=y CONFIG_BT_MESH_RELAY=y diff --git a/tests/bluetooth/tester/src/btp.c b/tests/bluetooth/tester/src/btp.c index 2ba92bf82148..3951ea0aee1b 100644 --- a/tests/bluetooth/tester/src/btp.c +++ b/tests/bluetooth/tester/src/btp.c @@ -24,7 +24,7 @@ LOG_MODULE_REGISTER(LOG_MODULE_NAME, CONFIG_BTTESTER_LOG_LEVEL); #include "btp/btp.h" -#define STACKSIZE 2048 +#define STACKSIZE CONFIG_BTTESTER_BTP_CMD_THREAD_STACK_SIZE static K_THREAD_STACK_DEFINE(stack, STACKSIZE); static struct k_thread cmd_thread; From 9bbff7d41f944549ede24b1c72955566217c0756 Mon Sep 17 00:00:00 2001 From: Ingar Kulbrandstad Date: Wed, 5 Feb 2025 15:53:35 +0100 Subject: [PATCH 47/49] [nrf fromtree] Bluetooth: Mesh: Remove experimental TF-M PSA Remove the experimental flag from BT_MESH_USES_TFM_PSA. Signed-off-by: Ingar Kulbrandstad (cherry picked from commit 45f233ba619079a0403a11deac802d62cceaf137) Signed-off-by: alperen sener --- subsys/bluetooth/mesh/Kconfig | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/subsys/bluetooth/mesh/Kconfig b/subsys/bluetooth/mesh/Kconfig index 13d892aa4379..8feef62d9473 100644 --- a/subsys/bluetooth/mesh/Kconfig +++ b/subsys/bluetooth/mesh/Kconfig @@ -1538,13 +1538,11 @@ config BT_MESH_USES_MBEDTLS_PSA Use Mbed TLS as PSA Crypto API provider. config BT_MESH_USES_TFM_PSA - bool "Use TF-M PSA [EXPERIMENTAL]" - select EXPERIMENTAL + bool "TF-M PSA" depends on BUILD_WITH_TFM help Use TF-M as PSA Crypto API provider. This is only possible on platforms that support TF-M. - This feature is experimental. endchoice From fc294e14ca40d9a064be812d419c9d28ce0dae05 Mon Sep 17 00:00:00 2001 From: alperen sener Date: Wed, 12 Feb 2025 15:51:49 +0100 Subject: [PATCH 48/49] [nrf fromtree] bluetooth: mesh: Correct callback check mesh blob client xfer_progress_complete should be checked instead of end callback Signed-off-by: alperen sener (cherry picked from commit 3fdb81cd1fee384939c41726484b70b0e62ebcec) Signed-off-by: alperen sener --- subsys/bluetooth/mesh/blob_cli.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/subsys/bluetooth/mesh/blob_cli.c b/subsys/bluetooth/mesh/blob_cli.c index 40d3d37a6c00..f5aae195476f 100644 --- a/subsys/bluetooth/mesh/blob_cli.c +++ b/subsys/bluetooth/mesh/blob_cli.c @@ -1086,7 +1086,7 @@ static void progress_checked(struct bt_mesh_blob_cli *cli) cli->state = BT_MESH_BLOB_CLI_STATE_NONE; - if (cli->cb && cli->cb->end) { + if (cli->cb && cli->cb->xfer_progress_complete) { cli->cb->xfer_progress_complete(cli); } } From 615d9497e4b5eb2e4437754e400ba6d1d1520dae Mon Sep 17 00:00:00 2001 From: alperen sener Date: Wed, 19 Feb 2025 13:35:12 +0100 Subject: [PATCH 49/49] [nrf noup] samples: bluetooth: mesh: Disable secure storage for real targets. Mesh currently works with trusted storage on real targets. Until secure storage is supported by default disable it. Signed-off-by: alperen sener --- samples/bluetooth/mesh/boards/qemu_x86.conf | 6 ++++++ samples/bluetooth/mesh/prj.conf | 2 +- samples/bluetooth/mesh_demo/boards/qemu_x86.conf | 6 ++++++ samples/bluetooth/mesh_demo/prj.conf | 2 +- samples/bluetooth/mesh_provisioner/boards/qemu_x86.conf | 6 ++++++ samples/bluetooth/mesh_provisioner/prj.conf | 2 +- samples/boards/nordic/mesh/onoff-app/prj.conf | 2 +- .../nordic/mesh/onoff_level_lighting_vnd_app/prj.conf | 2 +- 8 files changed, 23 insertions(+), 5 deletions(-) create mode 100644 samples/bluetooth/mesh/boards/qemu_x86.conf create mode 100644 samples/bluetooth/mesh_demo/boards/qemu_x86.conf create mode 100644 samples/bluetooth/mesh_provisioner/boards/qemu_x86.conf diff --git a/samples/bluetooth/mesh/boards/qemu_x86.conf b/samples/bluetooth/mesh/boards/qemu_x86.conf new file mode 100644 index 000000000000..bfb57193b782 --- /dev/null +++ b/samples/bluetooth/mesh/boards/qemu_x86.conf @@ -0,0 +1,6 @@ +# nrf_security only supports Cortex-M via PSA crypto libraries. +# Enforcing usage of built-in Mbed TLS for native simulator. +CONFIG_NRF_SECURITY=n +CONFIG_MBEDTLS_ENABLE_HEAP=n +CONFIG_TRUSTED_STORAGE=n +CONFIG_SECURE_STORAGE=y diff --git a/samples/bluetooth/mesh/prj.conf b/samples/bluetooth/mesh/prj.conf index 14b19316a866..cd7d6532b614 100644 --- a/samples/bluetooth/mesh/prj.conf +++ b/samples/bluetooth/mesh/prj.conf @@ -5,7 +5,7 @@ CONFIG_FLASH=y CONFIG_FLASH_MAP=y CONFIG_NVS=y CONFIG_SETTINGS=y -CONFIG_SECURE_STORAGE=y +# CONFIG_SECURE_STORAGE=y CONFIG_HWINFO=y CONFIG_BT=y diff --git a/samples/bluetooth/mesh_demo/boards/qemu_x86.conf b/samples/bluetooth/mesh_demo/boards/qemu_x86.conf new file mode 100644 index 000000000000..bfb57193b782 --- /dev/null +++ b/samples/bluetooth/mesh_demo/boards/qemu_x86.conf @@ -0,0 +1,6 @@ +# nrf_security only supports Cortex-M via PSA crypto libraries. +# Enforcing usage of built-in Mbed TLS for native simulator. +CONFIG_NRF_SECURITY=n +CONFIG_MBEDTLS_ENABLE_HEAP=n +CONFIG_TRUSTED_STORAGE=n +CONFIG_SECURE_STORAGE=y diff --git a/samples/bluetooth/mesh_demo/prj.conf b/samples/bluetooth/mesh_demo/prj.conf index bcb738ae5bd1..b7016b02c65b 100644 --- a/samples/bluetooth/mesh_demo/prj.conf +++ b/samples/bluetooth/mesh_demo/prj.conf @@ -31,7 +31,7 @@ CONFIG_FLASH_MAP=y CONFIG_NVS=y CONFIG_SETTINGS=y CONFIG_BT_MESH_RPL_STORE_TIMEOUT=600 -CONFIG_SECURE_STORAGE=y +# CONFIG_SECURE_STORAGE=y # Limit the number of key slots in PSA Crypto core to reduce # RAM footprint diff --git a/samples/bluetooth/mesh_provisioner/boards/qemu_x86.conf b/samples/bluetooth/mesh_provisioner/boards/qemu_x86.conf new file mode 100644 index 000000000000..bfb57193b782 --- /dev/null +++ b/samples/bluetooth/mesh_provisioner/boards/qemu_x86.conf @@ -0,0 +1,6 @@ +# nrf_security only supports Cortex-M via PSA crypto libraries. +# Enforcing usage of built-in Mbed TLS for native simulator. +CONFIG_NRF_SECURITY=n +CONFIG_MBEDTLS_ENABLE_HEAP=n +CONFIG_TRUSTED_STORAGE=n +CONFIG_SECURE_STORAGE=y diff --git a/samples/bluetooth/mesh_provisioner/prj.conf b/samples/bluetooth/mesh_provisioner/prj.conf index 10949c5480db..241767805164 100644 --- a/samples/bluetooth/mesh_provisioner/prj.conf +++ b/samples/bluetooth/mesh_provisioner/prj.conf @@ -45,7 +45,7 @@ CONFIG_FLASH_MAP=y CONFIG_NVS=y CONFIG_SETTINGS=y CONFIG_BT_MESH_RPL_STORE_TIMEOUT=600 -CONFIG_SECURE_STORAGE=y +# CONFIG_SECURE_STORAGE=y #CONFIG_BT_MESH_LOG_LEVEL_DBG=y #CONFIG_BT_MESH_SETTINGS_LOG_LEVEL_DBG=y diff --git a/samples/boards/nordic/mesh/onoff-app/prj.conf b/samples/boards/nordic/mesh/onoff-app/prj.conf index 0e67042b2653..0783579e795e 100644 --- a/samples/boards/nordic/mesh/onoff-app/prj.conf +++ b/samples/boards/nordic/mesh/onoff-app/prj.conf @@ -9,7 +9,7 @@ CONFIG_FLASH=y CONFIG_FLASH_MAP=y CONFIG_NVS=y CONFIG_SETTINGS=y -CONFIG_SECURE_STORAGE=y +# CONFIG_SECURE_STORAGE=y CONFIG_BT_MESH_RPL_STORE_TIMEOUT=600 diff --git a/samples/boards/nordic/mesh/onoff_level_lighting_vnd_app/prj.conf b/samples/boards/nordic/mesh/onoff_level_lighting_vnd_app/prj.conf index 3bb984208c70..96b5466b4a16 100644 --- a/samples/boards/nordic/mesh/onoff_level_lighting_vnd_app/prj.conf +++ b/samples/boards/nordic/mesh/onoff_level_lighting_vnd_app/prj.conf @@ -7,7 +7,7 @@ CONFIG_FLASH=y CONFIG_FLASH_MAP=y CONFIG_NVS=y CONFIG_SETTINGS=y -CONFIG_SECURE_STORAGE=y +# CONFIG_SECURE_STORAGE=y CONFIG_BT_OBSERVER=y CONFIG_BT_BROADCASTER=y