Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bluetooth: Host: More bsim refactoring #85319

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions tests/bluetooth/common/testlib/include/testlib/addr.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,18 @@
.a = {{last, 0x00, 0x00, 0x00, 0x00, 0xc0}}, \
})

static inline char *bt_testlib_get_addr_str(const bt_addr_le_t *addr,
char addr_str[BT_ADDR_LE_STR_LEN])
{
bt_addr_le_to_str(addr, addr_str, BT_ADDR_LE_STR_LEN);

return addr_str;
}

/** Macro to simplify printing of addresses. The returned pointer is valid until the end of the
* scope this was called from, due to the use of a compound literal.
*/
#define bt_testlib_addr_to_str(addr) \
bt_testlib_get_addr_str((addr), (char[BT_ADDR_LE_STR_LEN]) {0})

#endif /* ZEPHYR_TESTS_BLUETOOTH_COMMON_TESTLIB_INCLUDE_TESTLIB_ADDR_H_ */
8 changes: 7 additions & 1 deletion tests/bsim/babblekit/include/babblekit/flags.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,18 @@
#define DECLARE_FLAG(flag) extern atomic_t flag

/* Define a new binary flag.
* Declare them static if defining flags with the same name in multiple files.
* Use @ref DEFINE_FLAG_STATIC if defining flags with the same name in multiple files.
*
* @param flag Name of the flag
*/
#define DEFINE_FLAG(flag) atomic_t flag = (atomic_t) false

/* Define a new, static binary flag.
*
* @param flag Name of the flag
*/
#define DEFINE_FLAG_STATIC(flag) static atomic_t flag = (atomic_t) false

#define SET_FLAG(flag) (void)atomic_set(&flag, (atomic_t) true)
#define UNSET_FLAG(flag) (void)atomic_set(&flag, (atomic_t) false)

Expand Down
11 changes: 11 additions & 0 deletions tests/bsim/babblekit/include/babblekit/testcase.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,17 @@ extern enum bst_result_t bst_result;
} \
} while (0)

/*
* @brief Assert `expr` is true
*
* Assert that `expr` is true. If assertion is false, fail the test. I.e. return non-zero.
*
* @note This is different than `sys/__assert.h`.
*
*/
#define TEST_ASSERT_NO_MSG(expr) \
TEST_ASSERT(expr, "")

/*
* @brief Print a value. Lower-level than `printk` or `LOG_xx`.
*
Expand Down
3 changes: 3 additions & 0 deletions tests/bsim/bluetooth/host/adv/chain/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ cmake_minimum_required(VERSION 3.20.0)
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
project(bsim_test_adv_chain)

add_subdirectory(${ZEPHYR_BASE}/tests/bsim/babblekit babblekit)
target_link_libraries(app PRIVATE babblekit)

target_sources(app PRIVATE
src/main.c
${ZEPHYR_BASE}/samples/bluetooth/broadcaster_multiple/src/broadcaster_multiple.c
Expand Down
49 changes: 8 additions & 41 deletions tests/bsim/bluetooth/host/adv/chain/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,7 @@
#include <zephyr/bluetooth/bluetooth.h>
#include <zephyr/bluetooth/hci.h>

#include "bs_types.h"
#include "bs_tracing.h"
#include "time_machine.h"
#include "bstests.h"

#define FAIL(...) \
do { \
bst_result = Failed; \
bs_trace_error_time_line(__VA_ARGS__); \
} while (0)

#define PASS(...) \
do { \
bst_result = Passed; \
bs_trace_info_time(1, __VA_ARGS__); \
} while (0)
#include "babblekit/testcase.h"

#define NAME_LEN 30
#define BT_AD_DATA_NAME_SIZE (sizeof(CONFIG_BT_DEVICE_NAME) - 1U + 2U)
Expand All @@ -44,30 +29,28 @@

static K_SEM_DEFINE(sem_recv, 0, 1);

extern enum bst_result_t bst_result;

static void test_adv_main(void)
{
extern int broadcaster_multiple(void);
int err;

err = bt_enable(NULL);
if (err) {
FAIL("Bluetooth init failed\n");
TEST_FAIL("Bluetooth init failed");

bs_trace_silent_exit(err);
return;
}

err = broadcaster_multiple();
if (err) {
FAIL("Adv tests failed\n");
TEST_FAIL("Adv tests failed");
bs_trace_silent_exit(err);
return;
}

/* Successfully started advertising multiple sets */
PASS("Adv tests passed\n");
TEST_PASS("Adv tests passed");

/* Let the scanner receive the reports */
k_sleep(K_SECONDS(10));
Expand Down Expand Up @@ -146,7 +129,7 @@ static void test_scan_main(void)

err = bt_enable(NULL);
if (err) {
FAIL("Bluetooth init failed\n");
TEST_FAIL("Bluetooth init failed");

bs_trace_silent_exit(err);
return;
Expand All @@ -156,7 +139,7 @@ static void test_scan_main(void)

err = observer_start();
if (err) {
FAIL("Observer start failed\n");
TEST_FAIL("Observer start failed");

bs_trace_silent_exit(err);
return;
Expand All @@ -167,42 +150,26 @@ static void test_scan_main(void)

err = k_sem_take(&sem_recv, K_NO_WAIT);
if (err) {
FAIL("Scan receive failed\n");
TEST_FAIL("Scan receive failed");

bs_trace_silent_exit(err);
return;
}

PASS("Scan tests passed\n");
TEST_PASS("Scan tests passed");

bs_trace_silent_exit(0);
}

static void test_adv_chain_init(void)
{
bst_ticker_set_next_tick_absolute(60e6);
bst_result = In_progress;
}

static void test_adv_chain_tick(bs_time_t HW_device_time)
{
bst_result = Failed;
bs_trace_error_line("Test GATT Write finished.\n");
}

static const struct bst_test_instance test_def[] = {
{
.test_id = "adv",
.test_descr = "Central GATT Write",
.test_pre_init_f = test_adv_chain_init,
.test_tick_f = test_adv_chain_tick,
.test_main_f = test_adv_main
},
{
.test_id = "scan",
.test_descr = "Peripheral GATT Write",
.test_pre_init_f = test_adv_chain_init,
.test_tick_f = test_adv_chain_tick,
.test_main_f = test_scan_main
},
BSTEST_END_MARKER
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ extern enum bst_result_t bst_result;

static struct bt_conn *g_conn;

static DEFINE_FLAG(flag_connected);
static DEFINE_FLAG(flag_conn_recycled);
DEFINE_FLAG_STATIC(flag_connected);
DEFINE_FLAG_STATIC(flag_conn_recycled);

static void common_init(void)
{
Expand Down
6 changes: 3 additions & 3 deletions tests/bsim/bluetooth/host/adv/extended/src/ext_adv_scanner.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ extern enum bst_result_t bst_result;

static struct bt_conn *g_conn;

static DEFINE_FLAG(flag_ext_adv_seen);
static DEFINE_FLAG(flag_connected);
static DEFINE_FLAG(flag_conn_recycled);
DEFINE_FLAG_STATIC(flag_ext_adv_seen);
DEFINE_FLAG_STATIC(flag_connected);
DEFINE_FLAG_STATIC(flag_conn_recycled);

static void connected(struct bt_conn *conn, uint8_t err)
{
Expand Down
6 changes: 3 additions & 3 deletions tests/bsim/bluetooth/host/adv/long_ad/src/advertiser.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ static void create_adv(struct bt_le_ext_adv **adv)

err = bt_le_ext_adv_create(&params, NULL, adv);
if (err) {
TEST_FAIL("Failed to create advertiser (%d)\n", err);
TEST_FAIL("Failed to create advertiser (%d)", err);
}
}

Expand All @@ -49,7 +49,7 @@ static void start_adv(struct bt_le_ext_adv *adv)

err = bt_le_ext_adv_start(adv, &start_params);
if (err) {
TEST_FAIL("Failed to start advertiser (%d)\n", err);
TEST_FAIL("Failed to start advertiser (%d)", err);
}
}

Expand Down Expand Up @@ -99,7 +99,7 @@ static int set_ad_data(struct bt_le_ext_adv *adv, const uint8_t *serialized_ad,

err = bt_le_ext_adv_set_data(adv, ad, ad_len, NULL, 0);
if (err != 0 && err != -EDOM) {
TEST_FAIL("Failed to set advertising data (%d)\n", err);
TEST_FAIL("Failed to set advertising data (%d)", err);
}

return err;
Expand Down
2 changes: 1 addition & 1 deletion tests/bsim/bluetooth/host/adv/long_ad/src/scanner.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ static void start_scan(void)

err = bt_le_scan_start(BT_LE_SCAN_PASSIVE, device_found);
if (err) {
TEST_FAIL("Scanning failed to start (err %d)\n", err);
TEST_FAIL("Scanning failed to start (err %d)", err);
}

LOG_DBG("Scanning successfully started");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ extern enum bst_result_t bst_result;

static struct bt_conn *g_conn;

static DEFINE_FLAG(flag_connected);
static DEFINE_FLAG(flag_bonded);
DEFINE_FLAG_STATIC(flag_connected);
DEFINE_FLAG_STATIC(flag_bonded);

static void connected(struct bt_conn *conn, uint8_t err)
{
Expand Down
12 changes: 6 additions & 6 deletions tests/bsim/bluetooth/host/adv/periodic/src/per_adv_sync.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ static struct bt_conn *g_conn;
static bt_addr_le_t per_addr;
static uint8_t per_sid;

static DEFINE_FLAG(flag_connected);
static DEFINE_FLAG(flag_bonded);
static DEFINE_FLAG(flag_per_adv);
static DEFINE_FLAG(flag_per_adv_sync);
static DEFINE_FLAG(flag_per_adv_sync_lost);
static DEFINE_FLAG(flag_per_adv_recv);
DEFINE_FLAG_STATIC(flag_connected);
DEFINE_FLAG_STATIC(flag_bonded);
DEFINE_FLAG_STATIC(flag_per_adv);
DEFINE_FLAG_STATIC(flag_per_adv_sync);
DEFINE_FLAG_STATIC(flag_per_adv_sync_lost);
DEFINE_FLAG_STATIC(flag_per_adv_recv);

static void connected(struct bt_conn *conn, uint8_t err)
{
Expand Down
2 changes: 1 addition & 1 deletion tests/bsim/bluetooth/host/att/eatt/src/main_reconfigure.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

#define NEW_MTU 100

static DEFINE_FLAG(flag_reconfigured);
DEFINE_FLAG_STATIC(flag_reconfigured);

void att_mtu_updated(struct bt_conn *conn, uint16_t tx, uint16_t rx)
{
Expand Down
6 changes: 3 additions & 3 deletions tests/bsim/bluetooth/host/att/eatt_notif/src/client_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@
#include "babblekit/sync.h"
#include "common.h"

static DEFINE_FLAG(flag_is_connected);
static DEFINE_FLAG(flag_discover_complete);
static DEFINE_FLAG(flag_is_encrypted);
DEFINE_FLAG_STATIC(flag_is_connected);
DEFINE_FLAG_STATIC(flag_discover_complete);
DEFINE_FLAG_STATIC(flag_is_encrypted);

static struct bt_conn *g_conn;
static const struct bt_gatt_attr *local_attr;
Expand Down
4 changes: 2 additions & 2 deletions tests/bsim/bluetooth/host/att/eatt_notif/src/server_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@
#include "babblekit/sync.h"
#include "common.h"

static DEFINE_FLAG(flag_discover_complete);
DEFINE_FLAG_STATIC(flag_discover_complete);

extern enum bst_result_t bst_result;

static DEFINE_FLAG(flag_is_connected);
DEFINE_FLAG_STATIC(flag_is_connected);

static struct bt_conn *g_conn;

Expand Down
3 changes: 3 additions & 0 deletions tests/bsim/bluetooth/host/att/mtu_update/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ cmake_minimum_required(VERSION 3.20.0)
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
project(bsim_test_mtu_update)

add_subdirectory(${ZEPHYR_BASE}/tests/bsim/babblekit babblekit)
target_link_libraries(app PRIVATE babblekit)

target_include_directories(app PRIVATE src)

target_sources_ifdef(CONFIG_BT_PERIPHERAL app PRIVATE
Expand Down
37 changes: 0 additions & 37 deletions tests/bsim/bluetooth/host/att/mtu_update/src/common.h

This file was deleted.

Loading
Loading