Skip to content

Commit

Permalink
Bluetooth: BAP: Unicast client Split start and connect
Browse files Browse the repository at this point in the history
Removes the CIS connection establishment from bt_bap_stream_start
and move the behavior to a new funciton bt_bap_stream_connect.

This has 2 advantages:
1) The behavior of bt_bap_stream_start is much more clear and more aligned
with the spec's behavior for the receiver start ready opcode.
2) It is possible to connect streams in both the enabling
and the QoS configured state with bt_bap_stream_connect as
per the spec. This allows us to pass additional PTS test cases.

To implement this new behavior, samples and tests have been updated.

The CAP Initiator implementation has also been updated
to accomodate for the change in BAP, but the CAP
initiator implementation should work the same for application, except
that it's now possible to do unicast start on ASEs in any order
(#72138).

Signed-off-by: Emil Gydesen <emil.gydesen@nordicsemi.no>
  • Loading branch information
Thalley committed May 27, 2024
1 parent 3122296 commit 6914177
Show file tree
Hide file tree
Showing 18 changed files with 925 additions and 205 deletions.
7 changes: 3 additions & 4 deletions doc/connectivity/bluetooth/api/audio/shell/bap.rst
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ Commands
config : <direction: sink, source> <index> [loc <loc_bits>]
[preset <preset_name>]
stream_qos : interval [framing] [latency] [pd] [sdu] [phy] [rtn]
stream_connect : Connect the CIS of the stream
qos : Send QoS configure for Unicast Group
enable : [context]
stop
Expand Down Expand Up @@ -62,10 +63,8 @@ Commands
[extended <meta>]
[vendor <meta>]]
send : Send to Audio Stream [data]
start_sine : Start sending a LC3 encoded sine wave [all]
stop_sine : Stop sending a LC3 encoded sine wave [all]
recv_stats : Sets or gets the receive statistics reporting interval
in # of packets
bap_stats : Sets or gets the statistics reporting interval in # of
packets
set_location : <direction: sink, source> <location bitmask>
set_context : <direction: sink, source><context bitmask> <type:
supported, available>
Expand Down
4 changes: 4 additions & 0 deletions doc/releases/migration-guide-3.7.rst
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,10 @@ Bluetooth Audio
This needs to be added to all instances of CAP discovery callback functions defined.
(:github:`72797`)

* :c:func:`bt_bap_stream_start` no longer connects the CIS. To connect the CIS,
the :c:func:`bt_bap_stream_connect` shall now be called before :c:func:`bt_bap_stream_start`.
(:github:`73032`)

Bluetooth Classic
=================

Expand Down
41 changes: 32 additions & 9 deletions include/zephyr/bluetooth/audio/bap.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* @brief Header for Bluetooth BAP.
*
* Copyright (c) 2020 Bose Corporation
* Copyright (c) 2021-2023 Nordic Semiconductor ASA
* Copyright (c) 2021-2024 Nordic Semiconductor ASA
*
* SPDX-License-Identifier: Apache-2.0
*/
Expand Down Expand Up @@ -715,20 +715,43 @@ int bt_bap_stream_metadata(struct bt_bap_stream *stream, const uint8_t meta[], s
*/
int bt_bap_stream_disable(struct bt_bap_stream *stream);

/**
* @brief Connect unicast audio stream
*
* This procedure is used by a unicast client to connect the connected isochronous stream (CIS)
* associated with the audio stream. If two audio streams share a CIS, then this only needs to be
* done once for those streams. This can only be done for streams in the QoS configured or enabled
* states.
*
* The bt_bap_stream_ops.connected() callback will be called on the streams once this has finished.
*
* This shall only be called for unicast streams, and only as the unicast client
* (@kconfig{CONFIG_BT_BAP_UNICAST_CLIENT}).
*
* @param stream Stream object
*
* @retval 0 in case of success
* @retval -EINVAL if the stream, endpoint, ISO channel or connection is NULL
* @retval -EBADMSG if the stream or ISO channel is in an invalid state for connection
* @retval -EOPNOTSUPP if the role of the stream is not @ref BT_HCI_ROLE_CENTRAL
* @retval -EALREADY if the ISO channel is already connecting or connected
* @retval -EBUSY if another ISO channel is connecting
* @retval -ENOEXEC if otherwise rejected by the ISO layer
*/
int bt_bap_stream_connect(struct bt_bap_stream *stream);

/**
* @brief Start Audio Stream
*
* This procedure is used by a unicast client or unicast server to make a stream start streaming.
*
* For the unicast client, this will connect the CIS for the stream before
* sending the start command.
* For the unicast client, this will send the receiver start ready command to the unicast server for
* @ref BT_AUDIO_DIR_SOURCE ASEs. The CIS is required to be connected first by
* bt_bap_stream_connect() before the command can be sent.
*
* For the unicast server, this will put a @ref BT_AUDIO_DIR_SINK stream into the streaming state if
* the CIS is connected (initialized by the unicast client). If the CIS is not connected yet, the
* stream will go into the streaming state as soon as the CIS is connected.
* @ref BT_AUDIO_DIR_SOURCE streams will go into the streaming state when the unicast client sends
* the Receiver Start Ready operation, which will trigger the @ref bt_bap_unicast_server_cb.start()
* callback.
* For the unicast server, this will execute the receiver start ready command on the unicast server
* for @ref BT_AUDIO_DIR_SINK ASEs. If the CIS is not connected yet, the stream will go into the
* streaming state as soon as the CIS is connected.
*
* This shall only be called for unicast streams.
*
Expand Down
19 changes: 16 additions & 3 deletions samples/bluetooth/tmap_peripheral/src/bap_unicast_sr.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/** @file
* @brief Bluetooth Basic Audio Profile (BAP) Unicast Server role.
*
* Copyright (c) 2021-2023 Nordic Semiconductor ASA
* Copyright (c) 2021-2024 Nordic Semiconductor ASA
* Copyright (c) 2022 Codecoup
* Copyright (c) 2023 NXP
*
Expand Down Expand Up @@ -293,10 +293,23 @@ static void stream_recv(struct bt_bap_stream *stream, const struct bt_iso_recv_i

static void stream_enabled(struct bt_bap_stream *stream)
{
const int err = bt_bap_stream_start(stream);
struct bt_bap_ep_info ep_info;
int err;

err = bt_bap_ep_get_info(stream->ep, &ep_info);
if (err != 0) {
printk("Failed to start stream %p: %d", stream, err);
printk("Failed to get ep info: %d\n", err);
return;
}

/* The unicast server is responsible for starting the sink streams */
if (ep_info.dir == BT_AUDIO_DIR_SINK) {
/* Automatically do the receiver start ready operation */
err = bt_bap_stream_start(stream);

if (err != 0) {
printk("Failed to start stream %p: %d", stream, err);
}
}
}

Expand Down
76 changes: 69 additions & 7 deletions samples/bluetooth/unicast_audio_client/src/main.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2021-2023 Nordic Semiconductor ASA
* Copyright (c) 2021-2024 Nordic Semiconductor ASA
*
* SPDX-License-Identifier: Apache-2.0
*/
Expand Down Expand Up @@ -59,6 +59,7 @@ static K_SEM_DEFINE(sem_stream_configured, 0, 1);
static K_SEM_DEFINE(sem_stream_qos, 0, ARRAY_SIZE(sinks) + ARRAY_SIZE(sources));
static K_SEM_DEFINE(sem_stream_enabled, 0, 1);
static K_SEM_DEFINE(sem_stream_started, 0, 1);
static K_SEM_DEFINE(sem_stream_connected, 0, 1);

#define AUDIO_DATA_TIMEOUT_US 1000000UL /* Send data every 1 second */

Expand Down Expand Up @@ -518,9 +519,9 @@ static void stream_enabled(struct bt_bap_stream *stream)
k_sem_give(&sem_stream_enabled);
}

static void stream_started(struct bt_bap_stream *stream)
static void stream_connected_cb(struct bt_bap_stream *stream)
{
printk("Audio Stream %p started\n", stream);
printk("Audio Stream %p connected\n", stream);

/* Reset sequence number for sinks */
for (size_t i = 0U; i < configured_sink_stream_count; i++) {
Expand All @@ -530,6 +531,13 @@ static void stream_started(struct bt_bap_stream *stream)
}
}

k_sem_give(&sem_stream_connected);
}

static void stream_started(struct bt_bap_stream *stream)
{
printk("Audio Stream %p started\n", stream);

k_sem_give(&sem_stream_started);
}

Expand Down Expand Up @@ -576,7 +584,8 @@ static struct bt_bap_stream_ops stream_ops = {
.disabled = stream_disabled,
.stopped = stream_stopped,
.released = stream_released,
.recv = stream_recv
.recv = stream_recv,
.connected = stream_connected_cb,
};

static void add_remote_source(struct bt_bap_ep *ep)
Expand Down Expand Up @@ -1018,17 +1027,62 @@ static int enable_streams(void)
return 0;
}

static int start_streams(void)
static int connect_streams(void)
{
for (size_t i = 0U; i < configured_stream_count; i++) {
int err;

err = bt_bap_stream_start(&streams[i]);
if (err != 0) {
k_sem_reset(&sem_stream_connected);

err = bt_bap_stream_connect(&streams[i]);
if (err == -EALREADY) {
/* We have already connected a paired stream */
continue;
} else if (err != 0) {
printk("Unable to start stream: %d\n", err);
return err;
}

err = k_sem_take(&sem_stream_connected, K_FOREVER);
if (err != 0) {
printk("failed to take sem_stream_connected (err %d)\n", err);
return err;
}
}

return 0;
}

static enum bt_audio_dir stream_dir(const struct bt_bap_stream *stream)
{
struct bt_bap_ep_info ep_info;
int err;

err = bt_bap_ep_get_info(stream->ep, &ep_info);
if (err != 0) {
printk("Failed to get ep info for %p: %d\n", stream, err);
__ASSERT_NO_MSG(false);

return 0;
}

return ep_info.dir;
}

static int start_streams(void)
{
for (size_t i = 0U; i < configured_stream_count; i++) {
struct bt_bap_stream *stream = &streams[i];
int err;

if (stream_dir(stream) == BT_AUDIO_DIR_SOURCE) {
err = bt_bap_stream_start(&streams[i]);
if (err != 0) {
printk("Unable to start stream: %d\n", err);
return err;
}
} /* Sink streams are started by the unicast server */

err = k_sem_take(&sem_stream_started, K_FOREVER);
if (err != 0) {
printk("failed to take sem_stream_started (err %d)\n", err);
Expand All @@ -1051,6 +1105,7 @@ static void reset_data(void)
k_sem_reset(&sem_stream_qos);
k_sem_reset(&sem_stream_enabled);
k_sem_reset(&sem_stream_started);
k_sem_reset(&sem_stream_connected);

configured_sink_stream_count = 0;
configured_source_stream_count = 0;
Expand Down Expand Up @@ -1131,6 +1186,13 @@ int main(void)
}
printk("Streams enabled\n");

printk("Connecting streams\n");
err = connect_streams();
if (err != 0) {
return 0;
}
printk("Streams connected\n");

printk("Starting streams\n");
err = start_streams();
if (err != 0) {
Expand Down
38 changes: 37 additions & 1 deletion subsys/bluetooth/audio/bap_stream.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

/*
* Copyright (c) 2020 Intel Corporation
* Copyright (c) 2021-2023 Nordic Semiconductor ASA
* Copyright (c) 2021-2024 Nordic Semiconductor ASA
*
* SPDX-License-Identifier: Apache-2.0
*/
Expand Down Expand Up @@ -476,6 +476,8 @@ int bt_bap_stream_disconnect(struct bt_bap_stream *stream)

iso_chan = bt_bap_stream_iso_chan_get(stream);
if (iso_chan == NULL || iso_chan->iso == NULL) {
LOG_DBG("Not connected");

return -ENOTCONN;
}

Expand Down Expand Up @@ -720,6 +722,40 @@ int bt_bap_stream_reconfig(struct bt_bap_stream *stream,
return 0;
}

#if defined(CONFIG_BT_BAP_UNICAST_CLIENT)
int bt_bap_stream_connect(struct bt_bap_stream *stream)
{
uint8_t state;

LOG_DBG("stream %p ep %p", stream, stream == NULL ? NULL : stream->ep);

CHECKIF(stream == NULL || stream->ep == NULL || stream->conn == NULL) {
LOG_DBG("Invalid stream");
return -EINVAL;
}

/* Valid only after the CIS ID has been assigned in QoS configured state and while we are
* not streaming
*/
state = stream->ep->status.state;
switch (state) {
case BT_BAP_EP_STATE_QOS_CONFIGURED:
case BT_BAP_EP_STATE_ENABLING:
break;
default:
LOG_ERR("Invalid state: %s", bt_bap_ep_state_str(state));
return -EBADMSG;
}

/* Only a unicast client can connect a stream */
if (conn_get_role(stream->conn) == BT_HCI_ROLE_CENTRAL) {
return bt_bap_unicast_client_connect(stream);
} else {
return -EOPNOTSUPP;
}
}
#endif /* CONFIG_BT_BAP_UNICAST_CLIENT */

int bt_bap_stream_start(struct bt_bap_stream *stream)
{
uint8_t state;
Expand Down
Loading

0 comments on commit 6914177

Please sign in to comment.