Skip to content

Commit bb7463f

Browse files
ardo-nordicjhedberg
authored andcommitted
tests: bsim: bluetooth: host: enable conn_stress
Adding some minor modifications in order to be able to run host conn_stress test on CI. Signed-off-by: Artur Dobrynin <[email protected]>
1 parent f18e0ff commit bb7463f

File tree

7 files changed

+114
-83
lines changed

7 files changed

+114
-83
lines changed

tests/bsim/bluetooth/host/misc/conn_stress/central/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ cmake_minimum_required(VERSION 3.20.0)
55
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
66
project(conn_stress_central)
77

8+
add_subdirectory(${ZEPHYR_BASE}/tests/bsim/babblekit babblekit)
9+
target_link_libraries(app PRIVATE babblekit)
10+
811
target_sources(app PRIVATE
912
src/main.c
1013
)

tests/bsim/bluetooth/host/misc/conn_stress/central/prj.conf

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,8 @@ CONFIG_ASSERT_VERBOSE=y
2323
CONFIG_ASSERT_ON_ERRORS=y
2424

2525
# TODO: use default 3
26-
# we get an ATT timeout if 3
27-
# we get an SMP timeout if 10
28-
# CONFIG_BT_L2CAP_TX_BUF_COUNT=10
26+
# we get an ATT timeout if 3 or SMP timeout if 10
27+
CONFIG_BT_L2CAP_TX_BUF_COUNT=10
2928

3029
# deadlock on central with the default.
3130
# the problem is that `tx_free` is called from both syswq and btwq in that case.

tests/bsim/bluetooth/host/misc/conn_stress/central/src/main.c

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,16 @@
2222
#include <zephyr/types.h>
2323

2424
#include <zephyr/logging/log.h>
25-
LOG_MODULE_REGISTER(central, LOG_LEVEL_INF);
25+
LOG_MODULE_REGISTER(central, LOG_LEVEL_DBG);
2626

2727
#include "bstests.h"
2828
#include "bs_types.h"
2929
#include "bs_tracing.h"
3030
#include "bstests.h"
3131
#include "bs_pc_backchannel.h"
3232

33+
#include "babblekit/testcase.h"
34+
3335
#define DEFAULT_CONN_INTERVAL 20
3436
#define PERIPHERAL_DEVICE_NAME "Zephyr Peripheral"
3537
#define PERIPHERAL_DEVICE_NAME_LEN (sizeof(PERIPHERAL_DEVICE_NAME) - 1)
@@ -50,6 +52,8 @@ BUILD_ASSERT(NOTIFICATION_DATA_LEN <= CHARACTERISTIC_DATA_MAX_LEN);
5052
#define PERIPHERAL_SERVICE_UUID BT_UUID_DECLARE_128(PERIPHERAL_SERVICE_UUID_VAL)
5153
#define PERIPHERAL_CHARACTERISTIC_UUID BT_UUID_DECLARE_128(PERIPHERAL_CHARACTERISTIC_UUID_VAL)
5254

55+
#define EXPECTED_CONN_CYCLES 1
56+
5357
static struct bt_uuid_128 vnd_uuid =
5458
BT_UUID_INIT_128(BT_UUID_128_ENCODE(0x12345678, 0x1234, 0x5678, 0x1234, 0x56789abcdea0));
5559

@@ -89,6 +93,7 @@ struct conn_info {
8993
struct bt_gatt_discover_params discover_params;
9094
struct bt_gatt_subscribe_params subscribe_params;
9195
bt_addr_le_t addr;
96+
atomic_t conn_count;
9297
};
9398

9499
static struct conn_info conn_infos[CONFIG_BT_MAX_CONN] = {0};
@@ -232,8 +237,6 @@ static uint8_t discover_func(struct bt_conn *conn, const struct bt_gatt_attr *at
232237
conn_info_ref = get_connected_conn_info_ref(conn);
233238
__ASSERT_NO_MSG(conn_info_ref);
234239

235-
atomic_clear_bit(conn_info_ref->flags, CONN_INFO_DISCOVER_PAUSED);
236-
237240
if (conn_info_ref->discover_params.type == BT_GATT_DISCOVER_PRIMARY) {
238241
LOG_DBG("Primary Service Found");
239242
memcpy(&conn_info_ref->uuid,
@@ -295,7 +298,7 @@ static uint8_t discover_func(struct bt_conn *conn, const struct bt_gatt_attr *at
295298
/* if we're out of buffers or metadata contexts, continue discovery
296299
* later.
297300
*/
298-
LOG_INF("out of memory/not connected, continuing sub later");
301+
LOG_INF("out of memory/not connected, continuing sub later (err %d)", err);
299302
atomic_set_bit(conn_info_ref->flags, CONN_INFO_DISCOVER_PAUSED);
300303

301304
return BT_GATT_ITER_STOP;
@@ -328,6 +331,17 @@ static bool check_if_peer_connected(const bt_addr_le_t *addr)
328331
return false;
329332
}
330333

334+
static bool check_if_completed(void)
335+
{
336+
for (size_t i = 0; i < ARRAY_SIZE(conn_infos); i++) {
337+
if (atomic_get(&conn_infos[i].conn_count) < EXPECTED_CONN_CYCLES) {
338+
return false;
339+
}
340+
}
341+
342+
return true;
343+
}
344+
331345
static bool parse_ad(struct bt_data *data, void *user_data)
332346
{
333347
bt_addr_le_t *addr = user_data;
@@ -451,6 +465,7 @@ static void disconnected(struct bt_conn *conn, uint8_t reason)
451465
bt_conn_unref(conn);
452466
clear_info(conn_info_ref);
453467
atomic_dec(&conn_count);
468+
atomic_inc(&conn_info_ref->conn_count);
454469
}
455470

456471
#if defined(CONFIG_BT_SMP)
@@ -602,6 +617,8 @@ static void subscribe_to_service(struct bt_conn *conn, void *data)
602617
if (err != -ENOMEM && err != -ENOTCONN) {
603618
__ASSERT(!err, "Subscribe failed (err %d)", err);
604619
}
620+
621+
atomic_clear_bit(conn_info_ref->flags, CONN_INFO_DISCOVER_PAUSED);
605622
}
606623
}
607624

@@ -669,7 +686,7 @@ void test_central_main(void)
669686

670687
start_scan();
671688

672-
while (true) {
689+
while (!check_if_completed()) {
673690
/* reconnect peripherals when they drop out */
674691
if (atomic_get(&conn_count) < CONFIG_BT_MAX_CONN &&
675692
!atomic_test_bit(status_flags, DEVICE_IS_SCANNING) &&
@@ -698,13 +715,16 @@ void test_central_main(void)
698715
}
699716
k_msleep(10);
700717
}
718+
719+
TEST_PASS("Central tests passed");
701720
}
702721

703722
void test_init(void)
704723
{
705724
extern enum bst_result_t bst_result;
706725

707726
LOG_INF("Initializing Test");
727+
bst_ticker_set_next_tick_absolute(600*1e6);
708728
/* The peripherals determines whether the test passed. */
709729
bst_result = Passed;
710730
}
@@ -739,12 +759,23 @@ static void test_args(int argc, char **argv)
739759
bs_trace_raw(0, "Notification data size : %d\n", notification_size);
740760
}
741761

762+
static void test_tick(bs_time_t HW_device_time)
763+
{
764+
if (bst_result != Passed) {
765+
TEST_FAIL("Test timeout (not passed after %lu seconds)",
766+
(unsigned long)(HW_device_time / USEC_PER_SEC));
767+
}
768+
769+
bs_trace_silent_exit(0);
770+
}
771+
742772
static const struct bst_test_instance test_def[] = {
743773
{
744774
.test_id = "central",
745775
.test_descr = "Central Connection Stress",
746776
.test_args_f = test_args,
747777
.test_pre_init_f = test_init,
778+
.test_tick_f = test_tick,
748779
.test_main_f = test_central_main
749780
},
750781
BSTEST_END_MARKER

tests/bsim/bluetooth/host/misc/conn_stress/peripheral/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ cmake_minimum_required(VERSION 3.20.0)
55
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
66
project(conn_stress_peripheral)
77

8+
add_subdirectory(${ZEPHYR_BASE}/tests/bsim/babblekit babblekit)
9+
target_link_libraries(app PRIVATE babblekit)
10+
811
target_sources(app PRIVATE
912
src/main.c
1013
)

tests/bsim/bluetooth/host/misc/conn_stress/peripheral/src/main.c

Lines changed: 42 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
#include <zephyr/types.h>
2525

2626
#include <zephyr/logging/log.h>
27-
LOG_MODULE_REGISTER(peripheral, LOG_LEVEL_INF);
27+
LOG_MODULE_REGISTER(peripheral, LOG_LEVEL_DBG);
2828

2929
#include "bstests.h"
3030
#include "bs_types.h"
@@ -33,7 +33,8 @@ LOG_MODULE_REGISTER(peripheral, LOG_LEVEL_INF);
3333
#include "bs_pc_backchannel.h"
3434
#include "argparse.h"
3535

36-
#define TEST_ROUNDS 10
36+
#include "babblekit/testcase.h"
37+
3738
#define MIN_NOTIFICATIONS 50
3839

3940
#define NOTIFICATION_DATA_PREFIX "Counter:"
@@ -52,6 +53,8 @@ BUILD_ASSERT(NOTIFICATION_DATA_LEN <= CHARACTERISTIC_DATA_MAX_LEN);
5253
#define CENTRAL_SERVICE_UUID BT_UUID_DECLARE_128(CENTRAL_SERVICE_UUID_VAL)
5354
#define CENTRAL_CHARACTERISTIC_UUID BT_UUID_DECLARE_128(CENTRAL_CHARACTERISTIC_UUID_VAL)
5455

56+
#define EXPECTED_CONN_CYCLES 1
57+
5558
/* Custom Service Variables */
5659
static struct bt_uuid_128 vnd_uuid =
5760
BT_UUID_INIT_128(BT_UUID_128_ENCODE(0x12345678, 0x1234, 0x5678, 0x1234, 0x56789abcdef0));
@@ -164,7 +167,7 @@ static void disconnected(struct bt_conn *conn, uint8_t reason)
164167

165168
memset(&conn_info, 0x00, sizeof(struct active_conn_info));
166169

167-
if (rounds >= TEST_ROUNDS) {
170+
if (rounds >= EXPECTED_CONN_CYCLES) {
168171
LOG_INF("Number of conn/disconn cycles reached, stopping advertiser...");
169172
bt_le_adv_stop();
170173

@@ -400,18 +403,34 @@ void test_peripheral_main(void)
400403
sprintf(name, "per-%d", get_device_nbr());
401404
bt_set_name(name);
402405

403-
err = bt_le_adv_start(BT_LE_ADV_CONN_FAST_1, NULL, 0, NULL, 0);
406+
struct bt_le_adv_param adv_param = {
407+
.id = BT_ID_DEFAULT,
408+
.sid = 0,
409+
.secondary_max_skip = 0,
410+
.options = BT_LE_ADV_OPT_CONN | BT_LE_ADV_OPT_SCANNABLE,
411+
.interval_min = 0x0020, /* 20 ms */
412+
.interval_max = 0x0020, /* 20 ms */
413+
.peer = NULL,
414+
};
415+
416+
struct bt_data sd[1];
417+
418+
sd[0].type = BT_DATA_NAME_COMPLETE;
419+
sd[0].data_len = sizeof(CONFIG_BT_DEVICE_NAME) - 1;
420+
sd[0].data = CONFIG_BT_DEVICE_NAME;
421+
422+
err = bt_le_adv_start(&adv_param, sd, 1, sd, 1);
404423
if (err) {
405424
LOG_ERR("Advertising failed to start (err %d)", err);
406425
__ASSERT_NO_MSG(err);
407426
}
408-
LOG_INF("Started advertising");
427+
LOG_DBG("Started advertising");
409428

410429
bt_gatt_cb_register(&gatt_callbacks);
411430

412431
vnd_attr = bt_gatt_find_by_uuid(vnd_svc.attrs, vnd_svc.attr_count, &vnd_enc_uuid.uuid);
413432

414-
while (true) {
433+
for (size_t i = 0; i < EXPECTED_CONN_CYCLES; i++) {
415434
LOG_DBG("Waiting for connection from central..");
416435
while (!atomic_test_bit(conn_info.flags, CONN_INFO_CONNECTED)) {
417436
k_sleep(K_MSEC(10));
@@ -425,11 +444,12 @@ void test_peripheral_main(void)
425444
k_sleep(K_MSEC(10));
426445
}
427446

447+
LOG_DBG("Waiting until MTU exchange..");
428448
while (!atomic_test_bit(conn_info.flags, CONN_INFO_MTU_EXCHANGED)) {
429449
k_sleep(K_MSEC(10));
430450
}
431451

432-
LOG_INF("Begin sending notifications to central..");
452+
LOG_DBG("Begin sending notifications to central..");
433453
while (central_subscription &&
434454
atomic_test_bit(conn_info.flags, CONN_INFO_CONNECTED)) {
435455

@@ -448,18 +468,21 @@ void test_peripheral_main(void)
448468

449469
if (atomic_get(&conn_info.tx_notify_counter) > MIN_NOTIFICATIONS &&
450470
atomic_get(&conn_info.notify_counter) > MIN_NOTIFICATIONS) {
451-
LOG_INF("Disconnecting..");
471+
LOG_DBG("Disconnecting..");
452472
disconnect();
453473
}
454474
}
455475
}
476+
477+
TEST_PASS("Peripheral tests passed");
456478
}
457479

458480
void test_init(void)
459481
{
460482
extern enum bst_result_t bst_result;
461483

462484
LOG_INF("Initializing Test");
485+
bst_ticker_set_next_tick_absolute(600*1e6);
463486
bst_result = Failed;
464487
}
465488

@@ -481,12 +504,23 @@ static void test_args(int argc, char **argv)
481504
bs_trace_raw(0, "Notification data size : %d\n", notification_size);
482505
}
483506

507+
static void test_tick(bs_time_t HW_device_time)
508+
{
509+
if (bst_result != Passed) {
510+
TEST_FAIL("Test timeout (not passed after %lu seconds)",
511+
(unsigned long)(HW_device_time / USEC_PER_SEC));
512+
}
513+
514+
bs_trace_silent_exit(0);
515+
}
516+
484517
static const struct bst_test_instance test_def[] = {
485518
{
486519
.test_id = "peripheral",
487520
.test_descr = "Peripheral Connection Stress",
488521
.test_args_f = test_args,
489522
.test_pre_init_f = test_init,
523+
.test_tick_f = test_tick,
490524
.test_main_f = test_peripheral_main
491525
},
492526
BSTEST_END_MARKER

tests/bsim/bluetooth/host/misc/conn_stress/scripts/_conn_stress.sh

Lines changed: 0 additions & 67 deletions
This file was deleted.

0 commit comments

Comments
 (0)