Skip to content

Commit 2560884

Browse files
committed
tests: Bluetooth: CAP: Handover u->b unittests
Adds unittests for the handover from unicast to broadcast APIs. Signed-off-by: Emil Gydesen <[email protected]>
1 parent 8d7c2e1 commit 2560884

File tree

19 files changed

+1679
-1
lines changed

19 files changed

+1679
-1
lines changed

subsys/bluetooth/audio/bap_broadcast_assistant.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
#include <zephyr/bluetooth/l2cap.h>
2929
#include <zephyr/bluetooth/buf.h>
3030
#include <zephyr/bluetooth/uuid.h>
31-
#include <zephyr/device.h>
3231
#include <zephyr/init.h>
3332
#include <zephyr/kernel.h>
3433
#include <zephyr/net_buf.h>
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# SPDX-License-Identifier: Apache-2.0
2+
3+
cmake_minimum_required(VERSION 3.20.0)
4+
5+
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
6+
project(bluetooth_cap_handover)
7+
8+
target_include_directories(app PRIVATE
9+
${ZEPHYR_BASE}/subsys/bluetooth/
10+
11+
${ZEPHYR_BASE}/tests/bluetooth/audio/cap_handover/include
12+
${ZEPHYR_BASE}/tests/bluetooth/audio/mocks/include
13+
)
14+
15+
target_sources(app PRIVATE
16+
# Test source files
17+
src/unicast_to_broadcast.c
18+
src/test_common.c
19+
20+
# UUT files
21+
uut/bap_broadcast_assistant.c
22+
uut/bap_unicast_client.c
23+
uut/cap_handover.c
24+
uut/cap_initiator.c
25+
uut/csip.c
26+
27+
# Stack source file
28+
${ZEPHYR_BASE}/subsys/bluetooth/audio/audio.c
29+
${ZEPHYR_BASE}/subsys/bluetooth/audio/codec.c
30+
${ZEPHYR_BASE}/subsys/bluetooth/audio/bap_broadcast_source.c
31+
${ZEPHYR_BASE}/subsys/bluetooth/audio/bap_iso.c
32+
${ZEPHYR_BASE}/subsys/bluetooth/audio/bap_stream.c
33+
${ZEPHYR_BASE}/subsys/bluetooth/audio/cap_commander.c
34+
${ZEPHYR_BASE}/subsys/bluetooth/audio/cap_handover.c
35+
${ZEPHYR_BASE}/subsys/bluetooth/audio/cap_initiator.c
36+
${ZEPHYR_BASE}/subsys/bluetooth/audio/cap_common.c
37+
${ZEPHYR_BASE}/subsys/bluetooth/audio/cap_stream.c
38+
${ZEPHYR_BASE}/subsys/bluetooth/audio/ccid.c
39+
${ZEPHYR_BASE}/subsys/bluetooth/common/addr.c
40+
${ZEPHYR_BASE}/subsys/bluetooth/common/bt_str.c
41+
${ZEPHYR_BASE}/subsys/bluetooth/host/uuid.c
42+
43+
# Mock files
44+
${ZEPHYR_BASE}/tests/bluetooth/audio/mocks/src/adv.c
45+
${ZEPHYR_BASE}/tests/bluetooth/audio/mocks/src/conn.c
46+
${ZEPHYR_BASE}/tests/bluetooth/audio/mocks/src/gatt.c
47+
${ZEPHYR_BASE}/tests/bluetooth/audio/mocks/src/iso.c
48+
)
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
# Copyright (c) 2025 Nordic Semiconductor ASA
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
config BT_ATT_PREPARE_COUNT
5+
int
6+
default 1
7+
8+
config BT_AUDIO
9+
bool
10+
default y
11+
12+
config BT_AUDIO_CODEC_CFG_MAX_METADATA_SIZE
13+
int
14+
default 20
15+
16+
config BT_BAP_BROADCAST_ASSISTANT
17+
bool
18+
default y
19+
20+
config BT_BAP_BROADCAST_SOURCE
21+
bool
22+
default y
23+
24+
config BT_BAP_BROADCAST_SRC_STREAM_COUNT
25+
int
26+
default 2
27+
28+
config BT_BAP_SCAN_DELEGATOR
29+
bool
30+
default y
31+
32+
config BT_BAP_STREAM
33+
bool
34+
default y
35+
36+
config BT_BAP_UNICAST_CLIENT
37+
bool
38+
default y
39+
40+
config BT_BAP_UNICAST_CLIENT_GROUP_COUNT
41+
int
42+
default 1
43+
44+
config BT_BAP_UNICAST_CLIENT_GROUP_STREAM_COUNT
45+
int
46+
default 4
47+
48+
config BT_BAP_UNICAST_CLIENT_ASE_SNK_COUNT
49+
int
50+
default 2
51+
52+
config BT_BONDABLE
53+
bool
54+
default y
55+
56+
config BT_BUF_ACL_RX_SIZE
57+
int
58+
default 69
59+
60+
config BT_BUF_ACL_TX_COUNT
61+
int
62+
default 5
63+
64+
config BT_BUF_EVT_RX_COUNT
65+
int
66+
default 10
67+
68+
config BT_CONN
69+
bool
70+
default y
71+
72+
config BT_CSIP_SET_COORDINATOR
73+
bool
74+
default y
75+
76+
config BT_ISO_MAX_BIG
77+
int
78+
default 1
79+
80+
config BT_ISO_MAX_CIG
81+
int
82+
default 1
83+
84+
config BT_ISO_MAX_CHAN
85+
int
86+
default 4
87+
88+
config BT_MAX_PAIRED
89+
int
90+
default 1
91+
92+
config BT_L2CAP_TX_MTU
93+
int
94+
default 65
95+
96+
config BT_LOG
97+
bool
98+
default y
99+
100+
config BT_MAX_CONN
101+
int
102+
default 2
103+
104+
config BT_SMP
105+
bool
106+
default y
107+
108+
# Include Zephyr's Kconfig.
109+
source "Kconfig"
110+
source "subsys/bluetooth/audio/Kconfig"
111+
source "subsys/bluetooth/Kconfig.logging"
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
* Copyright (c) 2025 Nordic Semiconductor ASA
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#ifndef MOCKS_CAP_HANDOVER_H_
8+
#define MOCKS_CAP_HANDOVER_H_
9+
10+
#include <zephyr/bluetooth/audio/cap.h>
11+
#include <zephyr/bluetooth/audio/csip.h>
12+
#include <zephyr/bluetooth/bluetooth.h>
13+
#include <zephyr/fff.h>
14+
15+
extern const struct bt_cap_handover_cb mock_cap_handover_cb;
16+
17+
void mock_cap_handover_init(void);
18+
19+
DECLARE_FAKE_VOID_FUNC(mock_unicast_to_broadcast_complete_cb, int, struct bt_conn *,
20+
struct bt_cap_unicast_group *, struct bt_cap_broadcast_source *);
21+
22+
#endif /* MOCKS_CAP_HANDOVER_H_ */
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
* Copyright (c) 2025 Nordic Semiconductor ASA
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#ifndef MOCKS_CAP_INITIATOR_H_
8+
#define MOCKS_CAP_INITIATOR_H_
9+
10+
#include <zephyr/bluetooth/audio/cap.h>
11+
#include <zephyr/bluetooth/audio/csip.h>
12+
#include <zephyr/bluetooth/bluetooth.h>
13+
#include <zephyr/fff.h>
14+
15+
extern const struct bt_cap_initiator_cb mock_cap_initiator_cb;
16+
17+
void mock_cap_initiator_init(void);
18+
19+
DECLARE_FAKE_VOID_FUNC(mock_unicast_start_complete_cb, int, struct bt_conn *);
20+
21+
#endif /* MOCKS_CAP_INITIATOR_H_ */
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/* test_common.h */
2+
3+
/*
4+
* Copyright (c) 2025 Nordic Semiconductor ASA
5+
*
6+
* SPDX-License-Identifier: Apache-2.0
7+
*/
8+
9+
#include <zephyr/bluetooth/audio/bap.h>
10+
#include <zephyr/bluetooth/audio/bap_lc3_preset.h>
11+
#include <zephyr/bluetooth/audio/cap.h>
12+
#include <zephyr/bluetooth/conn.h>
13+
14+
void test_mocks_init(void);
15+
void test_mocks_cleanup(void);
16+
void mock_bt_csip_cleanup(void);
17+
18+
void test_conn_init(struct bt_conn *conn);
19+
20+
void test_unicast_set_state(struct bt_cap_stream *cap_stream, struct bt_conn *conn,
21+
struct bt_bap_ep *ep, struct bt_bap_lc3_preset *preset,
22+
enum bt_bap_ep_state state);
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
CONFIG_ZTEST=y
2+
3+
CONFIG_NET_BUF=y
4+
5+
CONFIG_BT_CAP_INITIATOR=y
6+
CONFIG_BT_CAP_COMMANDER=y
7+
CONFIG_BT_CAP_HANDOVER=y
8+
9+
CONFIG_ASSERT=y
10+
CONFIG_ASSERT_LEVEL=2
11+
CONFIG_ASSERT_VERBOSE=y
12+
13+
CONFIG_LOG=y
14+
CONFIG_BT_BAP_STREAM_LOG_LEVEL_DBG=y
15+
CONFIG_BT_CAP_COMMON_LOG_LEVEL_DBG=y
16+
CONFIG_BT_CAP_INITIATOR_LOG_LEVEL_DBG=y
17+
CONFIG_BT_CAP_COMMANDER_LOG_LEVEL_DBG=y
18+
CONFIG_BT_BAP_BROADCAST_SOURCE_LOG_LEVEL_DBG=y
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/* test_common.c - common procedures for unit test of CAP handover */
2+
3+
/*
4+
* Copyright (c) 2025 Nordic Semiconductor ASA
5+
*
6+
* SPDX-License-Identifier: Apache-2.0
7+
*/
8+
9+
#include <zephyr/bluetooth/audio/bap.h>
10+
#include <zephyr/bluetooth/audio/bap_lc3_preset.h>
11+
#include <zephyr/bluetooth/audio/cap.h>
12+
#include <zephyr/bluetooth/conn.h>
13+
#include <zephyr/bluetooth/hci_types.h>
14+
#include <zephyr/fff.h>
15+
#include <zephyr/sys/printk.h>
16+
#include <zephyr/ztest_assert.h>
17+
18+
#include "audio/bap_endpoint.h"
19+
#include "cap_initiator.h"
20+
#include "cap_handover.h"
21+
#include "conn.h"
22+
#include "test_common.h"
23+
24+
DEFINE_FFF_GLOBALS;
25+
26+
void test_mocks_init(void)
27+
{
28+
mock_cap_initiator_init();
29+
mock_cap_handover_init();
30+
}
31+
32+
void test_mocks_cleanup(void)
33+
{
34+
mock_bt_csip_cleanup();
35+
}
36+
37+
void test_conn_init(struct bt_conn *conn)
38+
{
39+
conn->index = 0;
40+
conn->info.type = BT_CONN_TYPE_LE;
41+
conn->info.role = BT_CONN_ROLE_CENTRAL;
42+
conn->info.state = BT_CONN_STATE_CONNECTED;
43+
conn->info.security.level = BT_SECURITY_L2;
44+
conn->info.security.enc_key_size = BT_ENC_KEY_SIZE_MAX;
45+
conn->info.security.flags = BT_SECURITY_FLAG_OOB | BT_SECURITY_FLAG_SC;
46+
}
47+
48+
void test_unicast_set_state(struct bt_cap_stream *cap_stream, struct bt_conn *conn,
49+
struct bt_bap_ep *ep, struct bt_bap_lc3_preset *preset,
50+
enum bt_bap_ep_state state)
51+
{
52+
struct bt_bap_stream *bap_stream = &cap_stream->bap_stream;
53+
54+
printk("Setting stream %p to state %d\n", bap_stream, state);
55+
56+
if (state == BT_BAP_EP_STATE_IDLE) {
57+
return;
58+
}
59+
60+
zassert_not_null(cap_stream);
61+
zassert_not_null(conn);
62+
zassert_not_null(ep);
63+
zassert_not_null(preset);
64+
65+
bap_stream->conn = conn;
66+
bap_stream->ep = ep;
67+
bap_stream->qos = &preset->qos;
68+
bap_stream->codec_cfg = &preset->codec_cfg;
69+
bap_stream->ep->state = state;
70+
}

0 commit comments

Comments
 (0)