Skip to content

Commit f7cf252

Browse files
Thalleycarlescufi
authored andcommitted
Bluetooth: Audio: Add special handling of release cp notification
When releasing a stream as the unicast client, we may get either the ASE state notification with state idle or the control point notification first. If we get the ASE state notification first, we pretend we get the CP notification first, and call the release callback. Signed-off-by: Emil Gydesen <[email protected]>
1 parent db7bb2c commit f7cf252

File tree

1 file changed

+37
-2
lines changed

1 file changed

+37
-2
lines changed

subsys/bluetooth/audio/bap_unicast_client.c

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ struct bt_bap_unicast_client_ep {
4747
struct bt_gatt_subscribe_params subscribe;
4848
struct bt_gatt_discover_params discover;
4949
struct bt_bap_ep ep;
50+
51+
/* Bool to help handle different order of CP and ASE notification when releasing */
52+
bool release_requested;
5053
};
5154

5255
static const struct bt_uuid *snk_uuid = BT_UUID_PACS_SNK;
@@ -514,6 +517,8 @@ static struct bt_bap_ep *unicast_client_ep_get(struct bt_conn *conn, enum bt_aud
514517

515518
static void unicast_client_ep_idle_state(struct bt_bap_ep *ep)
516519
{
520+
struct bt_bap_unicast_client_ep *client_ep =
521+
CONTAINER_OF(ep, struct bt_bap_unicast_client_ep, ep);
517522
struct bt_bap_stream *stream = ep->stream;
518523
const struct bt_bap_stream_ops *ops;
519524

@@ -538,6 +543,19 @@ static void unicast_client_ep_idle_state(struct bt_bap_ep *ep)
538543
bt_bap_stream_reset(stream);
539544

540545
/* Notify upper layer */
546+
if (client_ep->release_requested) {
547+
/* In case that we get the idle state notification before the CP notification we
548+
* trigger the CP callback now, as after this we won't be able to find the stream
549+
* by the ASE ID
550+
*/
551+
client_ep->release_requested = false;
552+
553+
if (unicast_client_cbs->release != NULL) {
554+
unicast_client_cbs->release(stream, BT_BAP_ASCS_RSP_CODE_SUCCESS,
555+
BT_BAP_ASCS_REASON_NONE);
556+
}
557+
}
558+
541559
ops = stream->ops;
542560
if (ops != NULL && ops->released != NULL) {
543561
ops->released(stream);
@@ -1244,6 +1262,7 @@ static uint8_t unicast_client_cp_notify(struct bt_conn *conn,
12441262
rsp = net_buf_simple_pull_mem(&buf, sizeof(*rsp));
12451263

12461264
for (uint8_t i = 0U; i < rsp->num_ase; i++) {
1265+
struct bt_bap_unicast_client_ep *client_ep;
12471266
struct bt_ascs_cp_ase_rsp *ase_rsp;
12481267
struct bt_bap_stream *stream;
12491268

@@ -1270,6 +1289,8 @@ static uint8_t unicast_client_cp_notify(struct bt_conn *conn,
12701289
continue;
12711290
}
12721291

1292+
client_ep = CONTAINER_OF(stream->ep, struct bt_bap_unicast_client_ep, ep);
1293+
12731294
switch (rsp->op) {
12741295
case BT_ASCS_CONFIG_OP:
12751296
if (unicast_client_cbs->config != NULL) {
@@ -1308,7 +1329,12 @@ static uint8_t unicast_client_cp_notify(struct bt_conn *conn,
13081329
}
13091330
break;
13101331
case BT_ASCS_RELEASE_OP:
1311-
if (unicast_client_cbs->release != NULL) {
1332+
if (client_ep->release_requested) {
1333+
/* Set to false to only handle the callback here */
1334+
client_ep->release_requested = false;
1335+
}
1336+
1337+
if (unicast_client_cbs->config != NULL) {
13121338
unicast_client_cbs->release(stream, ase_rsp->code, ase_rsp->reason);
13131339
}
13141340
break;
@@ -2699,6 +2725,7 @@ int bt_bap_unicast_client_stop(struct bt_bap_stream *stream)
26992725

27002726
int bt_bap_unicast_client_release(struct bt_bap_stream *stream)
27012727
{
2728+
struct bt_bap_unicast_client_ep *client_ep;
27022729
struct bt_bap_ep *ep = stream->ep;
27032730
struct net_buf_simple *buf;
27042731
struct bt_ascs_disable_op *req;
@@ -2731,7 +2758,15 @@ int bt_bap_unicast_client_release(struct bt_bap_stream *stream)
27312758
return 0;
27322759
}
27332760

2734-
return bt_bap_unicast_client_ep_send(stream->conn, ep, buf);
2761+
err = bt_bap_unicast_client_ep_send(stream->conn, ep, buf);
2762+
if (err != 0) {
2763+
return err;
2764+
}
2765+
2766+
client_ep = CONTAINER_OF(ep, struct bt_bap_unicast_client_ep, ep);
2767+
client_ep->release_requested = true;
2768+
2769+
return 0;
27352770
}
27362771

27372772
static uint8_t unicast_client_cp_discover_func(struct bt_conn *conn,

0 commit comments

Comments
 (0)