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 5a644c12fe1..c60770f9811 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 ee2fa5d3da8..9a5d699806b 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 91a5ff325bb..96769b64b57 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 cb9925179d6..9175d9d82b3 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 41362a54789..f6dc1418bce 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 0ac5936aaa3..4bde3f3f740 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 1eeaba1207e..0ad660d9958 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 6de9aee1de9..00000000000 --- 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 658613892e1..00000000000 --- 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 a146107efa8..492b26b106e 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 b4cd0691afe..231b344ca0f 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 85b2a61c9db..525996ebd94 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 b4e4151c49a..105dd4b8382 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 02a8fea70e8..03a1f0c5bbb 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 aa01d46db3c..3085bda474a 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 - -#include "bs_types.h" -#include "bs_tracing.h" -#include "time_machine.h" -#include "bstests.h" - #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 08c3d125242..0b7a8969f9a 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 - -#include "bs_types.h" -#include "bs_tracing.h" -#include "time_machine.h" -#include "bstests.h" - #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 a7b8d99b654..a27b9fedf4e 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 7176929cb2d..33c61af5fb9 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 31fd0195ddd..c64dbc4d20b 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 a459c91e450..dce1400647f 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 c3c4316ef5a..018887a2255 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 732e8aa13dd..48c0934fb88 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 9f42ea3eb56..6bea3ce925d 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 271d759dcd6..b0cec2ab3c5 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 49bc88f03fd..6dd9519f8d7 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 89b46b30f25..ba19c66db4e 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 d4bd2f8abb2..f280ab28ea3 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 fb93cbdba0e..c6b3752ad24 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 c208fc09484..138583f4ce2 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 057e76f1813..47fc5f0e2a0 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 c89bab27548..010ac1600cb 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 0d988761adf..b12d045edd9 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 e3919eb4a26..9bdf58693dc 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 202f757b3b7..bb9f7adb430 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 5f403304fcb..e846b1d05b9 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 1df3895fc3e..00000000000 --- 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 aecbda9cbe6..ec2c039acb9 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 474fb101113..94cf4ab699b 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 39000efbc74..39d442e38b2 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 acb0dd45947..6746842d40b 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 adff2dd05ef..00000000000 --- 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 339919dfe88..358b8ec9de5 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 fb8d7e71133..3e162d50297 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 c8a3303a8d9..4c206f7cd27 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 359247e08d9..c318928fbf8 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 7869a21840f..b279e8c1439 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 a7083921905..00000000000 --- 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 d01282ca035..adf5d4754a9 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 3699f80e09b..65d9b0213c0 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 637b7037e2f..1cb88b489fb 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 832d770fbd9..35f8186b3ec 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 0784a5e587d..a4f056dd408 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 7c9c898bc55..00000000000 --- 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 c4c8f9c4ff0..8f3dcfb29f2 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 fb47ef4c068..d37c1314e26 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 873685ecd4f..6205306306b 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 7869a21840f..b279e8c1439 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 df438607c5f..00000000000 --- 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 b37e7aec46c..eb8fa59eb63 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 a0fe2704f8e..b78b5d2a619 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 476f5bba0ef..6f3225c4d29 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 df41707e832..dbcf0078403 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 7869a21840f..b279e8c1439 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 df438607c5f..00000000000 --- 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 67486b175e4..1058eb86d37 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 125029653a0..273ddf51aa7 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 6b4cc13a7c2..d293ad9ac19 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 6fd131ca3ed..12fbed9e8f8 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 89a3d5cf313..c9ecd0997a3 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 4afc0bb3247..cef7d417ff3 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 ac72cd26694..d4a03284c17 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 f644f9181c2..00000000000 --- 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 60a191e3883..00000000000 --- 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 2e8e50c8f7f..a0328bc6870 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 80fff68d4ef..75c92a9e181 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 56fe4070ad5..abd7bba1c5a 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 ef1a4e9f9dd..0bd848897b2 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 0b41515d223..53f8d4ed1fb 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 c28d99b9d67..1fca2758390 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 5b344c60571..00000000000 --- 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 a778b7d5e1c..00000000000 --- 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 f35abf350ac..8e7df0f5ade 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 cf303ae2ef6..40e8bd1e367 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 6b607482012..00000000000 --- 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 c0b7ae353b4..00000000000 --- 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 1ee69d76585..d06188510c4 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 6adc381f4f5..5ac53e80ae1 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 648b58f271b..00000000000 --- 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 a399786bfd2..00000000000 --- 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 06135e030a5..4cd2f619ff7 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 88c326dddf3..383f54c095c 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 c6679d83494..00000000000 --- 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 2ce0288975d..00000000000 --- 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 04825306285..174d3739086 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 f6db9d424e2..5a5dd7ab174 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 f725ac6129c..00000000000 --- 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 3b0d09c53b6..00000000000 --- 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 9fd1c4bfcb8..825a8a74a2e 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 88c326dddf3..383f54c095c 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 5b344c60571..00000000000 --- 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 88b7950899a..00000000000 --- 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 0a9023ecbd3..e104fcfae81 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 298213989f5..2355ef86c05 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 f725ac6129c..00000000000 --- 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 3b0d09c53b6..00000000000 --- 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 57e70ae8821..e492036736f 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 9b8a38e8cb6..7ce3dbfe489 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 be11ade9baf..b46d52b60d7 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 af4a62820e7..199232976e1 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 df438607c5f..00000000000 --- 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 cb93d7ff71a..46c3dc38232 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 1c7df533bbd..4d53d60a7ec 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 78cee69024d..613f76d5c22 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 f7f2c3c495a..e100e50456d 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 81042fb740b..57928c2eb04 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 df438607c5f..00000000000 --- 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 159f27a8a21..00000000000 --- 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 ac2d919afab..a2c6cd87ce4 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 fb2b3228ecc..be4416ced57 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 8f2d2c17dd4..40adf5efb29 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 4166fa2b6aa..2a004e40ec1 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 4455e09d9f2..2ba29e07b68 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 0cae51ba2d9..dae03f846a2 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"); }