Skip to content

Commit 6f80c10

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 f89d1c6 commit 6f80c10

File tree

18 files changed

+1606
-1
lines changed

18 files changed

+1606
-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: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# SPDX-License-Identifier: Apache-2.0
2+
3+
cmake_minimum_required(VERSION 3.20.0)
4+
5+
find_package(Zephyr COMPONENTS unittest HINTS $ENV{ZEPHYR_BASE})
6+
7+
project(bluetooth_cap_handover)
8+
9+
add_subdirectory(${ZEPHYR_BASE}/tests/bluetooth/audio/cap_handover/uut uut)
10+
11+
target_link_libraries(testbinary PRIVATE uut)
12+
13+
target_include_directories(testbinary PRIVATE include)
14+
15+
target_sources(testbinary
16+
PRIVATE
17+
src/unicast_to_broadcast.c
18+
src/test_common.c
19+
)
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: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
CONFIG_ZTEST=y
2+
CONFIG_LOG=y
3+
4+
CONFIG_BT=y
5+
CONFIG_BT_SMP=y
6+
CONFIG_BT_EXT_ADV=y
7+
CONFIG_BT_PER_ADV_SYNC=y
8+
CONFIG_BT_CENTRAL=y
9+
CONFIG_BT_ISO_CENTRAL=y
10+
CONFIG_BT_ISO_BROADCASTER=y
11+
CONFIG_BT_ISO_SYNC_RECEIVER=y
12+
CONFIG_BT_GATT_CLIENT=y
13+
CONFIG_BT_GATT_AUTO_DISCOVER_CCC=y
14+
CONFIG_BT_GATT_AUTO_UPDATE_MTU=y
15+
CONFIG_BT_GATT_DYNAMIC_DB=y
16+
CONFIG_BT_AUDIO=y
17+
18+
# Dependencies for CAP initiator unicast
19+
CONFIG_BT_BAP_UNICAST_CLIENT=y
20+
CONFIG_BT_BAP_BROADCAST_SOURCE=y
21+
CONFIG_BT_BAP_BROADCAST_ASSISTANT=y
22+
CONFIG_BT_BAP_SCAN_DELEGATOR=y
23+
CONFIG_BT_CSIP_SET_COORDINATOR=y
24+
25+
CONFIG_BT_CAP_INITIATOR=y
26+
CONFIG_BT_CAP_COMMANDER=y
27+
CONFIG_BT_CAP_HANDOVER=y
28+
29+
# Support setting up a sink and source stream on 2 acceptors
30+
CONFIG_BT_MAX_CONN=2
31+
CONFIG_BT_ISO_MAX_CHAN=4
32+
CONFIG_BT_BAP_UNICAST_CLIENT_GROUP_STREAM_COUNT=4
33+
CONFIG_BT_BAP_UNICAST_CLIENT_ASE_SNK_COUNT=2
34+
CONFIG_BT_BAP_UNICAST_CLIENT_ASE_SRC_COUNT=2
35+
CONFIG_BT_BAP_BROADCAST_SRC_STREAM_COUNT=2
36+
37+
CONFIG_ASSERT=y
38+
CONFIG_ASSERT_LEVEL=2
39+
CONFIG_ASSERT_VERBOSE=y
40+
41+
CONFIG_BT_BAP_STREAM_LOG_LEVEL_DBG=y
42+
CONFIG_BT_CAP_COMMON_LOG_LEVEL_DBG=y
43+
CONFIG_BT_CAP_INITIATOR_LOG_LEVEL_DBG=y
44+
CONFIG_BT_CAP_COMMANDER_LOG_LEVEL_DBG=y
45+
CONFIG_BT_BAP_BROADCAST_SOURCE_LOG_LEVEL_DBG=y
46+
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+
17+
#include "bap_endpoint.h"
18+
#include "cap_initiator.h"
19+
#include "cap_handover.h"
20+
#include "conn.h"
21+
#include "test_common.h"
22+
#include "ztest_assert.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->status.state = state;
70+
}

0 commit comments

Comments
 (0)