Skip to content

Commit 2f9be4a

Browse files
committed
samples: Bluetooth: CAP Handover
Add a CAP Handover sample that uses the CAP handover API as the CAP Initiator and CAP commander. Signed-off-by: Emil Gydesen <[email protected]>
1 parent 335e1c2 commit 2f9be4a

15 files changed

+1659
-0
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# SPDX-License-Identifier: Apache-2.0
2+
3+
cmake_minimum_required(VERSION 3.20.0)
4+
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
5+
project(cap_initiator)
6+
7+
target_sources(app PRIVATE
8+
src/main.c
9+
)
10+
11+
zephyr_library_include_directories(${ZEPHYR_BASE}/samples/bluetooth)
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Copyright (c) 2024 Nordic Semiconductor ASA
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
mainmenu "Bluetooth: Common Audio Profile initiator sample"
5+
6+
config SAMPLE_STATIC_BROADCAST_ID
7+
bool "Use static broadcast ID"
8+
default y
9+
help
10+
Enabling this option will make the application use static broadcast ID, as opposed to a
11+
randomly generated one.
12+
13+
config SAMPLE_BROADCAST_ID
14+
hex "The static broadcast ID to use"
15+
range 0x000000 0xFFFFFF
16+
depends on STATIC_BROADCAST_ID
17+
default 0x123456
18+
help
19+
This is the 3-octet broadcast ID advertised if static broadcast IDs are enabled.
20+
21+
source "Kconfig.zephyr"
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Copyright 2023 Nordic Semiconductor ASA
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
source "share/sysbuild/Kconfig"
5+
6+
config NET_CORE_BOARD
7+
string
8+
default "nrf5340dk/nrf5340/cpunet" if "$(BOARD)" = "nrf5340dk"
9+
default "nrf5340_audio_dk/nrf5340/cpunet" if "$(BOARD)" = "nrf5340_audio_dk"
10+
default "nrf5340bsim/nrf5340/cpunet" if $(BOARD_TARGET_STRING) = "NRF5340BSIM_NRF5340_CPUAPP"
11+
12+
config NET_CORE_IMAGE_HCI_IPC
13+
bool "HCI IPC image on network core"
14+
default y
15+
depends on NET_CORE_BOARD != ""
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
.. zephyr:code-sample:: bluetooth_cap_handover
2+
:name: Common Audio Profile (CAP) Handover
3+
:relevant-api: bluetooth bt_bap bt_cap bt_conn
4+
5+
Connects to a CAP acceptor and performs CAP handover procedures
6+
7+
Overview
8+
********
9+
10+
Application demonstrating the CAP handover functionality.
11+
Starts by scanning for a CAP Acceptor then sets up unicast audio, and then switches between unicast
12+
and broadcast using the CAP handover procedures.
13+
14+
This sample can be found under :zephyr_file:`samples/bluetooth/cap_handover` in the Zephyr tree.
15+
16+
Check the :zephyr:code-sample-category:`bluetooth` samples for general information.
17+
18+
Requirements
19+
************
20+
21+
* BlueZ running on the host, or
22+
* A board with Bluetooth Low Energy 5.2 support
23+
24+
Building and Running
25+
********************
26+
27+
When building targeting an nrf52 series board with the Zephyr Bluetooth Controller,
28+
use ``-DEXTRA_CONF_FILE=overlay-bt_ll_sw_split.conf`` to enable the required ISO
29+
feature support.
30+
31+
Building for an nrf5340dk
32+
-------------------------
33+
34+
You can build both the application core image and an appropriate controller image for the network
35+
core with:
36+
37+
.. zephyr-app-commands::
38+
:zephyr-app: samples/bluetooth/cap_handover/
39+
:board: nrf5340dk/nrf5340/cpuapp
40+
:goals: build
41+
:west-args: --sysbuild
42+
43+
If you prefer to only build the application core image, you can do so by doing instead:
44+
45+
.. zephyr-app-commands::
46+
:zephyr-app: samples/bluetooth/cap_handover/
47+
:board: nrf5340dk/nrf5340/cpuapp
48+
:goals: build
49+
50+
In that case you can pair this application core image with the
51+
:zephyr:code-sample:`bluetooth_hci_ipc` sample
52+
:zephyr_file:`samples/bluetooth/hci_ipc/nrf5340_cpunet_iso-bt_ll_sw_split.conf` configuration.
53+
54+
Building for a simulated nrf5340bsim
55+
------------------------------------
56+
57+
Similarly to how you would for real HW, you can do:
58+
59+
.. zephyr-app-commands::
60+
:zephyr-app: samples/bluetooth/cap_handover/
61+
:board: nrf5340bsim/nrf5340/cpuapp
62+
:goals: build
63+
:west-args: --sysbuild
64+
65+
Note this will produce a Linux executable in :file:`./build/zephyr/zephyr.exe`.
66+
For more information, check :ref:`this board documentation <nrf5340bsim>`.
67+
68+
Building for a simulated nrf52_bsim
69+
-----------------------------------
70+
71+
.. zephyr-app-commands::
72+
:zephyr-app: samples/bluetooth/cap_handover/
73+
:board: nrf52_bsim
74+
:goals: build
75+
:gen-args: -DEXTRA_CONF_FILE=overlay-bt_ll_sw_split.conf
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
CONFIG_BT_BUF_EVT_RX_SIZE=255
2+
CONFIG_BT_BUF_ACL_RX_SIZE=255
3+
CONFIG_BT_BUF_ACL_TX_SIZE=251
4+
CONFIG_BT_BUF_CMD_TX_SIZE=255
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
CONFIG_BT_BUF_EVT_RX_SIZE=255
2+
CONFIG_BT_BUF_ACL_RX_SIZE=255
3+
CONFIG_BT_BUF_ACL_TX_SIZE=251
4+
CONFIG_BT_BUF_CMD_TX_SIZE=255
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Zephyr Bluetooth Controller
2+
CONFIG_BT_LL_SW_SPLIT=y
3+
4+
# Zephyr Controller tested maximum advertising data that can be set in a single HCI command
5+
CONFIG_BT_CTLR_SCAN_DATA_LEN_MAX=191
6+
CONFIG_BT_CTLR_ADV_DATA_LEN_MAX=191
7+
8+
# Support the highest SDU size required by any BAP LC3 presets (155) + 8 bytes of HCI ISO Data
9+
# packet overhead (the Packet_Sequence_Number, ISO_SDU_Length, Packet_Status_Flag fields; and
10+
# the optional Time_Stamp field, if supplied)
11+
CONFIG_BT_CTLR_ISO_TX_BUFFER_SIZE=163
12+
CONFIG_BT_CTLR_ISO_TX_SDU_LEN_MAX=155
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
CONFIG_LOG=y
2+
3+
CONFIG_BT=y
4+
CONFIG_BT_SMP=y
5+
CONFIG_BT_AUDIO=y
6+
CONFIG_BT_EXT_ADV=y
7+
CONFIG_BT_PER_ADV=y
8+
CONFIG_BT_DEVICE_NAME="CAP Initiator"
9+
10+
# ISO configs
11+
CONFIG_BT_ISO_MAX_CHAN=2
12+
CONFIG_BT_ISO_UNICAST=y
13+
CONFIG_BT_ISO_BROADCASTER=y
14+
15+
# BAP configs
16+
CONFIG_BT_BAP_UNICAST_CLIENT=y
17+
CONFIG_BT_BAP_UNICAST_CLIENT_GROUP_STREAM_COUNT=2
18+
CONFIG_BT_BAP_UNICAST_CLIENT_ASE_SNK_COUNT=2
19+
CONFIG_BT_BAP_UNICAST_CLIENT_ASE_SRC_COUNT=2
20+
21+
CONFIG_BT_BAP_BROADCAST_SOURCE=y
22+
CONFIG_BT_BAP_BROADCAST_SRC_STREAM_COUNT=2
23+
CONFIG_BT_BAP_BROADCAST_SRC_SUBGROUP_COUNT=1
24+
25+
CONFIG_BT_BAP_ASSISTANT=y
26+
27+
# CAP configs
28+
CONFIG_BT_CAP_INITIATOR=y
29+
CONFIG_BT_CAP_COMMANDER=y
30+
CONFIG_BT_CAP_HANDOVER=y
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
sample:
2+
description: Bluetooth Low Energy Common Audio Profile Handover sample
3+
name: Bluetooth Low Energy Common Audio Profile Handover sample
4+
tests:
5+
sample.bluetooth.cap_handover:
6+
harness: bluetooth
7+
platform_allow:
8+
- qemu_cortex_m3
9+
- qemu_x86
10+
- nrf5340dk/nrf5340/cpuapp
11+
- nrf5340bsim/nrf5340/cpuapp
12+
integration_platforms:
13+
- qemu_x86
14+
- nrf5340dk/nrf5340/cpuapp
15+
tags: bluetooth
16+
sysbuild: true
17+
sample.bluetooth.cap_handover.bt_ll_sw_split:
18+
harness: bluetooth
19+
platform_allow:
20+
- nrf52_bsim
21+
- nrf52833dk/nrf52833
22+
- nrf52840dk/nrf52840
23+
- nrf52840dongle/nrf52840
24+
integration_platforms:
25+
- nrf52_bsim
26+
- nrf52833dk/nrf52833
27+
- nrf52840dk/nrf52840
28+
- nrf52840dongle/nrf52840
29+
extra_args: EXTRA_CONF_FILE=overlay-bt_ll_sw_split.conf
30+
tags: bluetooth
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
* Copyright (c) 2024 Nordic Semiconductor ASA
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
#include <stdint.h>
7+
8+
#include <zephyr/bluetooth/audio/bap.h>
9+
#include <zephyr/bluetooth/audio/cap.h>
10+
#include <zephyr/bluetooth/conn.h>
11+
12+
struct tx_stream {
13+
struct bt_cap_stream *stream;
14+
uint16_t seq_num;
15+
};
16+
17+
/**
18+
* @brief Run the application as a CAP Initiator for unicast
19+
*
20+
* This will start scanning for and connecting to a CAP acceptor, and then attempt to setup
21+
* unicast streams.
22+
*
23+
* @return 0 if success, errno on failure.
24+
*/
25+
int cap_initiator_unicast(void);
26+
27+
/**
28+
* @brief Run the application as a CAP Initiator for broadcast
29+
*
30+
* This will start advertising broadcast audio that CAP acceptors can synchronize to.
31+
*
32+
* @return 0 if success, errno on failure.
33+
*/
34+
int cap_initiator_broadcast(void);
35+
36+
/**
37+
* @brief Initialize TX
38+
*
39+
* This will initialize TX if not already initialized. This creates and starts a thread that
40+
* will attempt to send data on all streams registered with cap_initiator_tx_register_stream().
41+
*/
42+
void cap_initiator_tx_init(void);
43+
44+
/**
45+
* @brief Register a stream for TX
46+
*
47+
* This will add it to the list of streams the TX thread will attempt to send on.
48+
*
49+
* @retval 0 on success
50+
* @retval -EINVAL if @p cap_stream is NULL
51+
* @retval -ENOMEM if not more streams can be registered
52+
*/
53+
int cap_initiator_tx_register_stream(struct bt_cap_stream *cap_stream);
54+
55+
/**
56+
* @brief Unregister a stream for TX
57+
*
58+
* This will remove it to the list of streams the TX thread will attempt to send on.
59+
*
60+
* @retval 0 on success
61+
* @retval -EINVAL if @p cap_stream is NULL
62+
* @retval -EALREADY if the stream is currently not registered
63+
*/
64+
int cap_initiator_tx_unregister_stream(struct bt_cap_stream *cap_stream);

0 commit comments

Comments
 (0)