From f2bf5b1b7919a8b1dc7a27c4a37557384cf6c77a Mon Sep 17 00:00:00 2001 From: Emil Gydesen Date: Thu, 26 Sep 2024 17:31:06 +0200 Subject: [PATCH] Bluetooth: BAP: Disallow bt_bap_stream_stop when CIS is not connected When the CIS for a BAP stream is not connected, we cannot truly be in the disabling state (the only state the stop operation can be performed by the unicast client). The reason for this is that if the CIS is disconnected, then the ASCS server shall transition to the QoS Configured state regardless of whether it has received a receiver stop ready command from the unicast client. Signed-off-by: Emil Gydesen --- include/zephyr/bluetooth/audio/bap.h | 10 ++++++++++ subsys/bluetooth/audio/bap_stream.c | 22 +++++++++++++++++++++ subsys/bluetooth/audio/bap_unicast_client.c | 13 +++++++++++- 3 files changed, 44 insertions(+), 1 deletion(-) diff --git a/include/zephyr/bluetooth/audio/bap.h b/include/zephyr/bluetooth/audio/bap.h index 661adabe262772..46cf7135c87b69 100644 --- a/include/zephyr/bluetooth/audio/bap.h +++ b/include/zephyr/bluetooth/audio/bap.h @@ -815,6 +815,16 @@ int bt_bap_stream_start(struct bt_bap_stream *stream); * * @param stream Stream object * + * @retval 0 Success + * @retval -EINVAL The @p stream does not have an endpoint or a connection, of the stream's + * connection's role is not @p BT_HCI_ROLE_CENTRAL + * @retval -EBADMSG The state of the @p stream endpoint is not @ref BT_BAP_EP_STATE_DISABLING + * @retval -EALREADY The CIS state of the @p is not in a connected state, and thus is already + * stopping + * @retval -EBUSY The @p stream is busy with another operation + * @retval -ENOTCONN The @p stream ACL connection is not connected + * @retval -ENOMEM No memory to send request + * @retval -ENOEXEC The request was rejected by GATT * @return 0 in case of success or negative value in case of error. */ int bt_bap_stream_stop(struct bt_bap_stream *stream); diff --git a/subsys/bluetooth/audio/bap_stream.c b/subsys/bluetooth/audio/bap_stream.c index 55ff17dd8e7e49..3f495081d06fd0 100644 --- a/subsys/bluetooth/audio/bap_stream.c +++ b/subsys/bluetooth/audio/bap_stream.c @@ -699,6 +699,7 @@ int bt_bap_stream_enable(struct bt_bap_stream *stream, const uint8_t meta[], siz int bt_bap_stream_stop(struct bt_bap_stream *stream) { + enum bt_iso_state iso_state; struct bt_bap_ep *ep; uint8_t role; int err; @@ -725,6 +726,27 @@ int bt_bap_stream_stop(struct bt_bap_stream *stream) return -EBADMSG; } + /* ASCS_v1.0 3.2 ASE state machine transitions + * + * If the server detects link loss of a CIS for an ASE in the Streaming state or the + * Disabling state, the server shall immediately transition that ASE to the QoS Configured + * state. + * + * This effectively means that if an ASE no longer has a connected CIS, the server shall + * bring it to the QoS Configured state. That means that we, as a unicast client, should not + * attempt to stop it + */ + if (ep->iso == NULL) { + LOG_DBG("Stream endpoint does not have a CIS, server will stop the ASE"); + return -EALREADY; + } + + iso_state = ep->iso->chan.state; + if (iso_state != BT_ISO_STATE_CONNECTED && iso_state != BT_ISO_STATE_CONNECTING) { + LOG_DBG("Stream endpoint CIS is not connected, server will stop the ASE"); + return -EALREADY; + } + err = bt_bap_unicast_client_stop(stream); if (err != 0) { LOG_DBG("Stopping stream failed: %d", err); diff --git a/subsys/bluetooth/audio/bap_unicast_client.c b/subsys/bluetooth/audio/bap_unicast_client.c index 3bc6effb21b182..691b242a08118f 100644 --- a/subsys/bluetooth/audio/bap_unicast_client.c +++ b/subsys/bluetooth/audio/bap_unicast_client.c @@ -3395,7 +3395,18 @@ int bt_bap_unicast_client_stop(struct bt_bap_stream *stream) } req->num_ases++; - return bt_bap_unicast_client_ep_send(stream->conn, ep, buf); + err = bt_bap_unicast_client_ep_send(stream->conn, ep, buf); + if (err != 0) { + /* Return expected error directly */ + if (err == -ENOTCONN || err == -ENOMEM) { + return err; + } + + LOG_DBG("bt_bap_unicast_client_ep_send failed with unexpected error %d", + err); + + return -ENOEXEC; + } } return 0;