Skip to content

Commit 4cc360a

Browse files
committed
tests: Bluetooth: CAP: Acceptor bsim test use ext_adv
The BSIM test for the CAP acceptor used legacy instead of extended advertising. This also adds the required advertising data and validation on the CAP initiator and CAP commander. Signed-off-by: Emil Gydesen <[email protected]>
1 parent 75cfe05 commit 4cc360a

File tree

3 files changed

+165
-89
lines changed

3 files changed

+165
-89
lines changed

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

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include <zephyr/bluetooth/audio/micp.h>
2424
#include <zephyr/bluetooth/audio/vcp.h>
2525
#include <zephyr/bluetooth/bluetooth.h>
26+
#include <zephyr/bluetooth/byteorder.h>
2627
#include <zephyr/bluetooth/gap.h>
2728
#include <zephyr/bluetooth/iso.h>
2829
#include <zephyr/bluetooth/uuid.h>
@@ -412,7 +413,22 @@ static struct bt_bap_scan_delegator_cb scan_delegator_cbs = {
412413
/* TODO: Expand with CAP service data */
413414
static const struct bt_data cap_acceptor_ad[] = {
414415
BT_DATA_BYTES(BT_DATA_FLAGS, (BT_LE_AD_GENERAL | BT_LE_AD_NO_BREDR)),
415-
BT_DATA_BYTES(BT_DATA_UUID16_ALL, BT_UUID_16_ENCODE(BT_UUID_CAS_VAL)),
416+
BT_DATA(BT_DATA_NAME_COMPLETE, CONFIG_BT_DEVICE_NAME, sizeof(CONFIG_BT_DEVICE_NAME) - 1),
417+
BT_DATA_BYTES(BT_DATA_UUID16_SOME, BT_UUID_16_ENCODE(BT_UUID_ASCS_VAL),
418+
BT_UUID_16_ENCODE(BT_UUID_CAS_VAL)),
419+
BT_DATA_BYTES(BT_DATA_SVC_DATA16, BT_UUID_16_ENCODE(BT_UUID_CAS_VAL),
420+
BT_AUDIO_UNICAST_ANNOUNCEMENT_TARGETED),
421+
IF_ENABLED(CONFIG_BT_BAP_UNICAST_SERVER,
422+
(BT_DATA_BYTES(BT_DATA_SVC_DATA16,
423+
BT_UUID_16_ENCODE(BT_UUID_ASCS_VAL),
424+
BT_AUDIO_UNICAST_ANNOUNCEMENT_TARGETED,
425+
BT_BYTES_LIST_LE16(SINK_CONTEXT),
426+
BT_BYTES_LIST_LE16(SOURCE_CONTEXT),
427+
0x00, /* Metadata length */),
428+
))
429+
IF_ENABLED(CONFIG_BT_BAP_SCAN_DELEGATOR,
430+
(BT_DATA_BYTES(BT_DATA_SVC_DATA16, BT_UUID_16_ENCODE(BT_UUID_BASS_VAL)),
431+
))
416432
};
417433

418434
static struct bt_csip_set_member_svc_inst *csip_set_member;

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

Lines changed: 74 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -507,8 +507,75 @@ static struct bt_bap_broadcast_assistant_cb ba_cbs = {
507507
.add_src = bap_broadcast_assistant_add_src_cb,
508508
};
509509

510+
static bool check_audio_support_and_connect_cb(struct bt_data *data, void *user_data)
511+
{
512+
char addr_str[BT_ADDR_LE_STR_LEN];
513+
bt_addr_le_t *addr = user_data;
514+
const struct bt_uuid *uuid;
515+
uint16_t uuid_val;
516+
int err;
517+
518+
printk("data->type %u\n", data->type);
519+
520+
if (data->type != BT_DATA_SVC_DATA16) {
521+
return true; /* Continue parsing to next AD data type */
522+
}
523+
524+
if (data->data_len < sizeof(uuid_val)) {
525+
return true; /* Continue parsing to next AD data type */
526+
}
527+
528+
/* We are looking for the CAS service data */
529+
uuid_val = sys_get_le16(data->data);
530+
uuid = BT_UUID_DECLARE_16(uuid_val);
531+
if (bt_uuid_cmp(uuid, BT_UUID_CAS) != 0) {
532+
return true; /* Continue parsing to next AD data type */
533+
}
534+
535+
bt_addr_le_to_str(addr, addr_str, sizeof(addr_str));
536+
printk("Device found: %s\n", addr_str);
537+
538+
printk("Stopping scan\n");
539+
if (bt_le_scan_stop()) {
540+
FAIL("Could not stop scan");
541+
return false;
542+
}
543+
544+
err = bt_conn_le_create(addr, BT_CONN_LE_CREATE_CONN,
545+
BT_LE_CONN_PARAM(BT_GAP_INIT_CONN_INT_MIN, BT_GAP_INIT_CONN_INT_MIN,
546+
0, BT_GAP_MS_TO_CONN_TIMEOUT(4000)),
547+
&connected_conns[connected_conn_cnt]);
548+
if (err != 0) {
549+
FAIL("Could not connect to peer: %d", err);
550+
}
551+
552+
return false; /* Stop parsing */
553+
}
554+
555+
static void scan_recv_cb(const struct bt_le_scan_recv_info *info, struct net_buf_simple *buf)
556+
{
557+
struct bt_conn *conn;
558+
559+
conn = bt_conn_lookup_addr_le(BT_ID_DEFAULT, info->addr);
560+
if (conn != NULL) {
561+
/* Already connected to this device */
562+
bt_conn_unref(conn);
563+
return;
564+
}
565+
566+
/* Check for connectable, extended advertising */
567+
if (((info->adv_props & BT_GAP_ADV_PROP_EXT_ADV) != 0) &&
568+
((info->adv_props & BT_GAP_ADV_PROP_CONNECTABLE)) != 0) {
569+
/* Check for TMAS support in advertising data */
570+
bt_data_parse(buf, check_audio_support_and_connect_cb, (void *)info->addr);
571+
}
572+
}
573+
510574
static void init(size_t acceptor_cnt)
511575
{
576+
static struct bt_le_scan_cb scan_callbacks = {
577+
.recv = scan_recv_cb,
578+
};
512579
static struct bt_conn_cb conn_cb = {
513580
.disconnected = cap_disconnected_cb,
514581
};
@@ -521,6 +588,12 @@ static void init(size_t acceptor_cnt)
521588
}
522589

523590
bt_gatt_cb_register(&gatt_callbacks);
591+
err = bt_le_scan_cb_register(&scan_callbacks);
592+
if (err != 0) {
593+
FAIL("Failed to register scan callbacks (err %d)\n", err);
594+
return;
595+
}
596+
524597
bt_conn_cb_register(&conn_cb);
525598

526599
err = bt_cap_commander_register_cb(&cap_cb);
@@ -573,56 +646,13 @@ static void init(size_t acceptor_cnt)
573646
UNSET_FLAG(flag_syncable);
574647
}
575648

576-
static void cap_device_found(const bt_addr_le_t *addr, int8_t rssi, uint8_t type,
577-
struct net_buf_simple *ad)
578-
{
579-
char addr_str[BT_ADDR_LE_STR_LEN];
580-
struct bt_conn *conn;
581-
int err;
582-
583-
/* We're only interested in connectable events */
584-
if (type != BT_HCI_ADV_IND && type != BT_HCI_ADV_DIRECT_IND) {
585-
return;
586-
}
587-
588-
conn = bt_conn_lookup_addr_le(BT_ID_DEFAULT, addr);
589-
if (conn != NULL) {
590-
/* Already connected to this device */
591-
bt_conn_unref(conn);
592-
return;
593-
}
594-
595-
bt_addr_le_to_str(addr, addr_str, sizeof(addr_str));
596-
printk("Device found: %s (RSSI %d)\n", addr_str, rssi);
597-
598-
/* connect only to devices in close proximity */
599-
if (rssi < -70) {
600-
FAIL("RSSI too low");
601-
return;
602-
}
603-
604-
printk("Stopping scan\n");
605-
if (bt_le_scan_stop()) {
606-
FAIL("Could not stop scan");
607-
return;
608-
}
609-
610-
err = bt_conn_le_create(addr, BT_CONN_LE_CREATE_CONN,
611-
BT_LE_CONN_PARAM(BT_GAP_INIT_CONN_INT_MIN, BT_GAP_INIT_CONN_INT_MIN,
612-
0, BT_GAP_MS_TO_CONN_TIMEOUT(4000)),
613-
&connected_conns[connected_conn_cnt]);
614-
if (err) {
615-
FAIL("Could not connect to peer: %d", err);
616-
}
617-
}
618-
619649
static void scan_and_connect(void)
620650
{
621651
int err;
622652

623653
UNSET_FLAG(flag_connected);
624654

625-
err = bt_le_scan_start(BT_LE_SCAN_PASSIVE, cap_device_found);
655+
err = bt_le_scan_start(BT_LE_SCAN_PASSIVE, NULL);
626656
if (err != 0) {
627657
FAIL("Scanning failed to start (err %d)\n", err);
628658
return;

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

Lines changed: 74 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include <zephyr/bluetooth/gatt.h>
2525
#include <zephyr/bluetooth/hci_types.h>
2626
#include <zephyr/bluetooth/iso.h>
27+
#include <zephyr/bluetooth/uuid.h>
2728
#include <zephyr/kernel.h>
2829
#include <zephyr/net_buf.h>
2930
#include <zephyr/sys/atomic.h>
@@ -367,8 +368,75 @@ static struct bt_gatt_cb gatt_callbacks = {
367368
.att_mtu_updated = att_mtu_updated,
368369
};
369370

371+
static bool check_audio_support_and_connect_cb(struct bt_data *data, void *user_data)
372+
{
373+
char addr_str[BT_ADDR_LE_STR_LEN];
374+
bt_addr_le_t *addr = user_data;
375+
const struct bt_uuid *uuid;
376+
uint16_t uuid_val;
377+
int err;
378+
379+
printk("data->type %u\n", data->type);
380+
381+
if (data->type != BT_DATA_SVC_DATA16) {
382+
return true; /* Continue parsing to next AD data type */
383+
}
384+
385+
if (data->data_len < sizeof(uuid_val)) {
386+
return true; /* Continue parsing to next AD data type */
387+
}
388+
389+
/* We are looking for the CAS service data */
390+
uuid_val = sys_get_le16(data->data);
391+
uuid = BT_UUID_DECLARE_16(uuid_val);
392+
if (bt_uuid_cmp(uuid, BT_UUID_CAS) != 0) {
393+
return true; /* Continue parsing to next AD data type */
394+
}
395+
396+
bt_addr_le_to_str(addr, addr_str, sizeof(addr_str));
397+
printk("Device found: %s\n", addr_str);
398+
399+
printk("Stopping scan\n");
400+
if (bt_le_scan_stop()) {
401+
FAIL("Could not stop scan");
402+
return false;
403+
}
404+
405+
err = bt_conn_le_create(addr, BT_CONN_LE_CREATE_CONN,
406+
BT_LE_CONN_PARAM(BT_GAP_INIT_CONN_INT_MIN, BT_GAP_INIT_CONN_INT_MIN,
407+
0, BT_GAP_MS_TO_CONN_TIMEOUT(4000)),
408+
&connected_conns[connected_conn_cnt]);
409+
if (err != 0) {
410+
FAIL("Could not connect to peer: %d", err);
411+
}
412+
413+
return false; /* Stop parsing */
414+
}
415+
416+
static void scan_recv_cb(const struct bt_le_scan_recv_info *info, struct net_buf_simple *buf)
417+
{
418+
struct bt_conn *conn;
419+
420+
conn = bt_conn_lookup_addr_le(BT_ID_DEFAULT, info->addr);
421+
if (conn != NULL) {
422+
/* Already connected to this device */
423+
bt_conn_unref(conn);
424+
return;
425+
}
426+
427+
/* Check for connectable, extended advertising */
428+
if (((info->adv_props & BT_GAP_ADV_PROP_EXT_ADV) != 0) &&
429+
((info->adv_props & BT_GAP_ADV_PROP_CONNECTABLE)) != 0) {
430+
/* Check for TMAS support in advertising data */
431+
bt_data_parse(buf, check_audio_support_and_connect_cb, (void *)info->addr);
432+
}
433+
}
434+
370435
static void init(void)
371436
{
437+
static struct bt_le_scan_cb scan_callbacks = {
438+
.recv = scan_recv_cb,
439+
};
372440
int err;
373441

374442
err = bt_enable(NULL);
@@ -378,6 +446,11 @@ static void init(void)
378446
}
379447

380448
bt_gatt_cb_register(&gatt_callbacks);
449+
err = bt_le_scan_cb_register(&scan_callbacks);
450+
if (err != 0) {
451+
FAIL("Failed to register scan callbacks (err %d)\n", err);
452+
return;
453+
}
381454

382455
err = bt_bap_unicast_client_register_cb(&unicast_client_cbs);
383456
if (err != 0) {
@@ -404,56 +477,13 @@ static void init(void)
404477
}
405478
}
406479

407-
static void cap_device_found(const bt_addr_le_t *addr, int8_t rssi, uint8_t type,
408-
struct net_buf_simple *ad)
409-
{
410-
char addr_str[BT_ADDR_LE_STR_LEN];
411-
struct bt_conn *conn;
412-
int err;
413-
414-
/* We're only interested in connectable events */
415-
if (type != BT_HCI_ADV_IND && type != BT_HCI_ADV_DIRECT_IND) {
416-
return;
417-
}
418-
419-
conn = bt_conn_lookup_addr_le(BT_ID_DEFAULT, addr);
420-
if (conn != NULL) {
421-
/* Already connected to this device */
422-
bt_conn_unref(conn);
423-
return;
424-
}
425-
426-
bt_addr_le_to_str(addr, addr_str, sizeof(addr_str));
427-
printk("Device found: %s (RSSI %d)\n", addr_str, rssi);
428-
429-
/* connect only to devices in close proximity */
430-
if (rssi < -70) {
431-
FAIL("RSSI too low");
432-
return;
433-
}
434-
435-
printk("Stopping scan\n");
436-
if (bt_le_scan_stop()) {
437-
FAIL("Could not stop scan");
438-
return;
439-
}
440-
441-
err = bt_conn_le_create(addr, BT_CONN_LE_CREATE_CONN,
442-
BT_LE_CONN_PARAM(BT_GAP_INIT_CONN_INT_MIN, BT_GAP_INIT_CONN_INT_MIN,
443-
0, BT_GAP_MS_TO_CONN_TIMEOUT(4000)),
444-
&connected_conns[connected_conn_cnt]);
445-
if (err) {
446-
FAIL("Could not connect to peer: %d", err);
447-
}
448-
}
449-
450480
static void scan_and_connect(void)
451481
{
452482
int err;
453483

454484
UNSET_FLAG(flag_connected);
455485

456-
err = bt_le_scan_start(BT_LE_SCAN_PASSIVE, cap_device_found);
486+
err = bt_le_scan_start(BT_LE_SCAN_PASSIVE, NULL);
457487
if (err != 0) {
458488
FAIL("Scanning failed to start (err %d)\n", err);
459489
return;

0 commit comments

Comments
 (0)