Skip to content

Commit 33c253c

Browse files
Thalleycarlescufi
authored andcommitted
Tests: Bluetooth: CAP: Refactor some CAP commander functions
Refactor som CAP commander test functions to perform operations in parallel, rather than sequential. The CAP initiator functions are kept sequential so that we are testing both versions. Signed-off-by: Emil Gydesen <[email protected]>
1 parent 68ea1c4 commit 33c253c

File tree

2 files changed

+129
-60
lines changed

2 files changed

+129
-60
lines changed

tests/bsim/bluetooth/audio/prj.conf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,7 @@ CONFIG_BT_HAS_CLIENT_LOG_LEVEL_DBG=y
196196
CONFIG_BT_CAP_ACCEPTOR_LOG_LEVEL_DBG=y
197197
CONFIG_BT_CAP_INITIATOR_LOG_LEVEL_DBG=y
198198
CONFIG_BT_CAP_COMMANDER_LOG_LEVEL_DBG=y
199+
CONFIG_BT_CAP_COMMON_LOG_LEVEL_DBG=y
199200
CONFIG_BT_GMAP_LOG_LEVEL_DBG=y
200201

201202
# LOGGING

tests/bsim/bluetooth/audio/src/cap_commander_test.c

Lines changed: 128 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,22 @@
1212
#include <zephyr/bluetooth/audio/cap.h>
1313
#include <zephyr/bluetooth/audio/micp.h>
1414
#include <zephyr/bluetooth/audio/vcp.h>
15+
#include <zephyr/kernel.h>
1516
#include <zephyr/sys/byteorder.h>
1617
#include "common.h"
1718
#include "bap_common.h"
1819

20+
#define SEM_TIMEOUT K_SECONDS(5)
21+
1922
extern enum bst_result_t bst_result;
2023

2124
static struct bt_conn *connected_conns[CONFIG_BT_MAX_CONN];
2225
static volatile size_t connected_conn_cnt;
2326

24-
CREATE_FLAG(flag_cas_discovered);
25-
CREATE_FLAG(flag_vcs_discovered);
26-
CREATE_FLAG(flag_mics_discovered);
27+
static struct k_sem sem_disconnected;
28+
static struct k_sem sem_cas_discovered;
29+
static struct k_sem sem_vcs_discovered;
30+
static struct k_sem sem_mics_discovered;
2731
CREATE_FLAG(flag_mtu_exchanged);
2832
CREATE_FLAG(flag_volume_changed);
2933
CREATE_FLAG(flag_volume_mute_changed);
@@ -35,7 +39,7 @@ static void cap_discovery_complete_cb(struct bt_conn *conn, int err,
3539
const struct bt_csip_set_coordinator_csis_inst *csis_inst)
3640
{
3741
if (err != 0) {
38-
FAIL("Failed to discover CAS: %d\n", err);
42+
FAIL("Discover failed on %p: %d\n", (void *)conn, err);
3943

4044
return;
4145
}
@@ -47,12 +51,12 @@ static void cap_discovery_complete_cb(struct bt_conn *conn, int err,
4751
return;
4852
}
4953

50-
printk("Found CAS with CSIS %p\n", csis_inst);
54+
printk("Found CAS on %p with CSIS %p\n", (void *)conn, csis_inst);
5155
} else {
52-
printk("Found CAS\n");
56+
printk("Found CAS on %p\n", (void *)conn);
5357
}
5458

55-
SET_FLAG(flag_cas_discovered);
59+
k_sem_give(&sem_cas_discovered);
5660
}
5761

5862
#if defined(CONFIG_BT_VCP_VOL_CTLR)
@@ -140,7 +144,7 @@ static void cap_vcp_discover_cb(struct bt_vcp_vol_ctlr *vol_ctlr, int err, uint8
140144
}
141145

142146
printk("VCS for %p found with %u VOCS and %u AICS\n", vol_ctlr, vocs_count, aics_count);
143-
SET_FLAG(flag_vcs_discovered);
147+
k_sem_give(&sem_vcs_discovered);
144148
}
145149

146150
static void cap_vcp_state_cb(struct bt_vcp_vol_ctlr *vol_ctlr, int err, uint8_t volume,
@@ -168,7 +172,7 @@ static void cap_micp_discover_cb(struct bt_micp_mic_ctlr *mic_ctlr, int err, uin
168172
}
169173

170174
printk("MICS for %p found with %u AICS\n", mic_ctlr, aics_count);
171-
SET_FLAG(flag_mics_discovered);
175+
k_sem_give(&sem_mics_discovered);
172176
}
173177

174178
static struct bt_micp_mic_ctlr_cb micp_cb = {
@@ -185,8 +189,16 @@ static struct bt_gatt_cb gatt_callbacks = {
185189
.att_mtu_updated = att_mtu_updated,
186190
};
187191

188-
static void init(void)
192+
static void cap_disconnected_cb(struct bt_conn *conn, uint8_t reason)
193+
{
194+
k_sem_give(&sem_disconnected);
195+
}
196+
197+
static void init(size_t acceptor_cnt)
189198
{
199+
static struct bt_conn_cb conn_cb = {
200+
.disconnected = cap_disconnected_cb,
201+
};
190202
int err;
191203

192204
err = bt_enable(NULL);
@@ -196,6 +208,7 @@ static void init(void)
196208
}
197209

198210
bt_gatt_cb_register(&gatt_callbacks);
211+
bt_conn_cb_register(&conn_cb);
199212

200213
err = bt_cap_commander_register_cb(&cap_cb);
201214
if (err != 0) {
@@ -214,6 +227,11 @@ static void init(void)
214227
FAIL("Failed to register MICP callbacks (err %d)\n", err);
215228
return;
216229
}
230+
231+
k_sem_init(&sem_disconnected, 0, acceptor_cnt);
232+
k_sem_init(&sem_cas_discovered, 0, acceptor_cnt);
233+
k_sem_init(&sem_vcs_discovered, 0, acceptor_cnt);
234+
k_sem_init(&sem_mics_discovered, 0, acceptor_cnt);
217235
}
218236

219237
static void cap_device_found(const bt_addr_le_t *addr, int8_t rssi, uint8_t type,
@@ -276,62 +294,117 @@ static void scan_and_connect(void)
276294
connected_conn_cnt++;
277295
}
278296

279-
static void disconnect_acl(struct bt_conn *conn)
297+
static void disconnect_acl(size_t acceptor_cnt)
280298
{
281-
int err;
299+
k_sem_reset(&sem_disconnected);
282300

283-
err = bt_conn_disconnect(conn, BT_HCI_ERR_REMOTE_USER_TERM_CONN);
284-
if (err != 0) {
285-
FAIL("Failed to disconnect (err %d)\n", err);
286-
return;
301+
for (size_t i = 0U; i < acceptor_cnt; i++) {
302+
struct bt_conn *conn = connected_conns[i];
303+
int err;
304+
305+
printk("Disconnecting %p\n", (void *)conn);
306+
307+
err = bt_conn_disconnect(conn, BT_HCI_ERR_REMOTE_USER_TERM_CONN);
308+
if (err != 0) {
309+
FAIL("Failed to disconnect %p (err %d)\n", (void *)conn, err);
310+
return;
311+
}
312+
}
313+
314+
for (size_t i = 0U; i < acceptor_cnt; i++) {
315+
const int err = k_sem_take(&sem_disconnected, SEM_TIMEOUT);
316+
317+
if (err != 0) {
318+
const struct bt_conn *conn = connected_conns[i];
319+
320+
FAIL("Failed to take sem_disconnected for %p: %d", (void *)conn, err);
321+
}
287322
}
288323
}
289324

290-
static void discover_cas(struct bt_conn *conn)
325+
static void discover_cas(size_t acceptor_cnt)
291326
{
292-
int err;
327+
k_sem_reset(&sem_cas_discovered);
293328

294-
UNSET_FLAG(flag_cas_discovered);
329+
/* Do parallel discovery */
330+
for (size_t i = 0U; i < acceptor_cnt; i++) {
331+
struct bt_conn *conn = connected_conns[i];
332+
int err;
295333

296-
err = bt_cap_commander_discover(conn);
297-
if (err != 0) {
298-
printk("Failed to discover CAS: %d\n", err);
299-
return;
334+
printk("Discovering CAS on %p\n", (void *)conn);
335+
336+
err = bt_cap_commander_discover(conn);
337+
if (err != 0) {
338+
FAIL("Failed to discover CAS on %p: %d\n", (void *)conn, err);
339+
return;
340+
}
300341
}
301342

302-
WAIT_FOR_FLAG(flag_cas_discovered);
343+
for (size_t i = 0U; i < acceptor_cnt; i++) {
344+
const int err = k_sem_take(&sem_cas_discovered, SEM_TIMEOUT);
345+
346+
if (err != 0) {
347+
const struct bt_conn *conn = connected_conns[i];
348+
349+
FAIL("Failed to take sem_cas_discovered for %p: %d", (void *)conn, err);
350+
}
351+
}
303352
}
304353

305-
static void discover_vcs(struct bt_conn *conn)
354+
static void discover_vcs(size_t acceptor_cnt)
306355
{
307-
struct bt_vcp_vol_ctlr *vol_ctlr;
308-
int err;
356+
k_sem_reset(&sem_vcs_discovered);
309357

310-
UNSET_FLAG(flag_vcs_discovered);
358+
/* Do parallel discovery */
359+
for (size_t i = 0U; i < acceptor_cnt; i++) {
360+
struct bt_conn *conn = connected_conns[i];
361+
struct bt_vcp_vol_ctlr *vol_ctlr;
362+
int err;
311363

312-
err = bt_vcp_vol_ctlr_discover(conn, &vol_ctlr);
313-
if (err != 0) {
314-
FAIL("Failed to discover VCS: %d\n", err);
315-
return;
364+
printk("Discovering VCS on %p\n", (void *)conn);
365+
366+
err = bt_vcp_vol_ctlr_discover(conn, &vol_ctlr);
367+
if (err != 0) {
368+
FAIL("Failed to discover VCS on %p: %d\n", err);
369+
return;
370+
}
316371
}
317372

318-
WAIT_FOR_FLAG(flag_vcs_discovered);
373+
for (size_t i = 0U; i < acceptor_cnt; i++) {
374+
const int err = k_sem_take(&sem_vcs_discovered, SEM_TIMEOUT);
375+
376+
if (err != 0) {
377+
const struct bt_conn *conn = connected_conns[i];
378+
379+
FAIL("Failed to take sem_vcs_discovered for %p: %d", (void *)conn, err);
380+
}
381+
}
319382
}
320383

321-
static void discover_mics(struct bt_conn *conn)
384+
static void discover_mics(size_t acceptor_cnt)
322385
{
323-
struct bt_micp_mic_ctlr *mic_ctlr;
324-
int err;
386+
k_sem_reset(&sem_mics_discovered);
325387

326-
UNSET_FLAG(flag_mics_discovered);
388+
for (size_t i = 0U; i < acceptor_cnt; i++) {
389+
struct bt_micp_mic_ctlr *mic_ctlr;
390+
int err;
327391

328-
err = bt_micp_mic_ctlr_discover(conn, &mic_ctlr);
329-
if (err != 0) {
330-
FAIL("Failed to discover MICS: %d\n", err);
331-
return;
392+
err = bt_micp_mic_ctlr_discover(connected_conns[i], &mic_ctlr);
393+
if (err != 0) {
394+
FAIL("Failed to discover MICS: %d\n", err);
395+
return;
396+
}
332397
}
333398

334-
WAIT_FOR_FLAG(flag_mics_discovered);
399+
for (size_t i = 0U; i < acceptor_cnt; i++) {
400+
const int err = k_sem_take(&sem_mics_discovered, SEM_TIMEOUT);
401+
402+
if (err != 0) {
403+
const struct bt_conn *conn = connected_conns[i];
404+
405+
FAIL("Failed to take sem_mics_discovered for %p: %d", (void *)conn, err);
406+
}
407+
}
335408
}
336409

337410
static void test_change_volume(void)
@@ -477,29 +550,25 @@ static void test_change_microphone_gain(void)
477550

478551
static void test_main_cap_commander_capture_and_render(void)
479552
{
480-
init();
553+
const size_t acceptor_cnt = get_dev_cnt() - 1; /* Assume all other devices are acceptors
554+
*/
555+
init(acceptor_cnt);
481556

482557
/* Connect to and do discovery on all CAP acceptors */
483-
for (size_t i = 0U; i < get_dev_cnt() - 1; i++) {
558+
for (size_t i = 0U; i < acceptor_cnt; i++) {
484559
scan_and_connect();
485560

486561
WAIT_FOR_FLAG(flag_mtu_exchanged);
487-
488-
/* TODO: We should use CSIP to find set members */
489-
discover_cas(connected_conns[i]);
490-
discover_cas(connected_conns[i]); /* test that we can discover twice */
491-
492-
if (IS_ENABLED(CONFIG_BT_VCP_VOL_CTLR)) {
493-
discover_vcs(connected_conns[i]);
494-
}
495-
496-
if (IS_ENABLED(CONFIG_BT_MICP_MIC_CTLR)) {
497-
discover_mics(connected_conns[i]);
498-
}
499562
}
500563

564+
/* TODO: We should use CSIP to find set members */
565+
discover_cas(acceptor_cnt);
566+
discover_cas(acceptor_cnt); /* verify that we can discover twice */
567+
501568
if (IS_ENABLED(CONFIG_BT_CSIP_SET_COORDINATOR)) {
502569
if (IS_ENABLED(CONFIG_BT_VCP_VOL_CTLR)) {
570+
discover_vcs(acceptor_cnt);
571+
503572
test_change_volume();
504573

505574
test_change_volume_mute(true);
@@ -511,6 +580,8 @@ static void test_main_cap_commander_capture_and_render(void)
511580
}
512581

513582
if (IS_ENABLED(CONFIG_BT_MICP_MIC_CTLR)) {
583+
discover_mics(acceptor_cnt);
584+
514585
test_change_microphone_mute(true);
515586
test_change_microphone_mute(false);
516587

@@ -521,10 +592,7 @@ static void test_main_cap_commander_capture_and_render(void)
521592
}
522593

523594
/* Disconnect all CAP acceptors */
524-
for (size_t i = 0U; i < connected_conn_cnt; i++) {
525-
disconnect_acl(connected_conns[i]);
526-
}
527-
connected_conn_cnt = 0U;
595+
disconnect_acl(acceptor_cnt);
528596

529597
PASS("CAP commander capture and rendering passed\n");
530598
}

0 commit comments

Comments
 (0)