Skip to content

Commit 65c2f8e

Browse files
Thalleynashif
authored andcommitted
Bluetooth: BAP: Release stream on codec config if requested
An ASCS endpoint may go into either the idle or codec configured state when a stream is released. However since we have such a high coupling between audio streams and endpoints, we need to consider the endpoint as having been released (i.e. gone to the IDLE state). This is handled by a boolean state variable for now, but we may want to consider a more generic approach where streams and endpoints may be less coupled. Signed-off-by: Emil Gydesen <[email protected]>
1 parent f3f8380 commit 65c2f8e

File tree

1 file changed

+33
-17
lines changed

1 file changed

+33
-17
lines changed

subsys/bluetooth/audio/bap_unicast_client.c

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ struct bt_bap_unicast_client_ep {
5050

5151
/* Bool to help handle different order of CP and ASE notification when releasing */
5252
bool release_requested;
53+
bool cp_ntf_pending;
5354
};
5455

5556
static const struct bt_uuid *snk_uuid = BT_UUID_PACS_SNK;
@@ -562,15 +563,19 @@ static int unicast_client_ep_idle_state(struct bt_bap_ep *ep)
562563

563564
/* Notify upper layer */
564565
if (client_ep->release_requested) {
565-
/* In case that we get the idle state notification before the CP notification we
566-
* trigger the CP callback now, as after this we won't be able to find the stream
567-
* by the ASE ID
568-
*/
569566
client_ep->release_requested = false;
570567

571-
if (unicast_client_cbs != NULL && unicast_client_cbs->release != NULL) {
572-
unicast_client_cbs->release(stream, BT_BAP_ASCS_RSP_CODE_SUCCESS,
573-
BT_BAP_ASCS_REASON_NONE);
568+
if (client_ep->cp_ntf_pending) {
569+
/* In case that we get the idle state notification before the CP
570+
* notification we trigger the CP callback now, as after this we won't be
571+
* able to find the stream by the ASE ID
572+
*/
573+
client_ep->cp_ntf_pending = false;
574+
575+
if (unicast_client_cbs != NULL && unicast_client_cbs->release != NULL) {
576+
unicast_client_cbs->release(stream, BT_BAP_ASCS_RSP_CODE_SUCCESS,
577+
BT_BAP_ASCS_REASON_NONE);
578+
}
574579
}
575580
}
576581

@@ -1307,7 +1312,7 @@ static uint8_t unicast_client_cp_notify(struct bt_conn *conn,
13071312
}
13081313

13091314
for (uint8_t i = 0U; i < rsp->num_ase; i++) {
1310-
struct bt_bap_unicast_client_ep *client_ep;
1315+
struct bt_bap_unicast_client_ep *client_ep = NULL;
13111316
struct bt_ascs_cp_ase_rsp *ase_rsp;
13121317
struct bt_bap_stream *stream;
13131318

@@ -1331,6 +1336,9 @@ static uint8_t unicast_client_cp_notify(struct bt_conn *conn,
13311336
stream = audio_stream_by_ep_id(conn, ase_rsp->id);
13321337
if (stream == NULL) {
13331338
LOG_DBG("Could not find stream by id %u", ase_rsp->id);
1339+
} else {
1340+
client_ep = CONTAINER_OF(stream->ep, struct bt_bap_unicast_client_ep, ep);
1341+
client_ep->cp_ntf_pending = false;
13341342
}
13351343

13361344
switch (rsp->op) {
@@ -1371,18 +1379,20 @@ static uint8_t unicast_client_cp_notify(struct bt_conn *conn,
13711379
}
13721380
break;
13731381
case BT_ASCS_RELEASE_OP:
1374-
if (stream != NULL) {
1375-
client_ep = CONTAINER_OF(stream->ep,
1376-
struct bt_bap_unicast_client_ep, ep);
1377-
1378-
if (client_ep->release_requested) {
1379-
/* Set to false to only handle the callback here */
1382+
/* client_ep->release_requested is set to false if handled by the
1383+
* endpoint notification handler
1384+
*/
1385+
if (client_ep != NULL && client_ep->release_requested) {
1386+
/* If request was reject, do not expect endpoint notifications */
1387+
if (ase_rsp->code != BT_BAP_ASCS_RSP_CODE_SUCCESS) {
1388+
client_ep->cp_ntf_pending = false;
13801389
client_ep->release_requested = false;
13811390
}
1382-
}
13831391

1384-
if (unicast_client_cbs->release != NULL) {
1385-
unicast_client_cbs->release(stream, ase_rsp->code, ase_rsp->reason);
1392+
if (unicast_client_cbs->release != NULL) {
1393+
unicast_client_cbs->release(stream, ase_rsp->code,
1394+
ase_rsp->reason);
1395+
}
13861396
}
13871397
break;
13881398
default:
@@ -2012,6 +2022,10 @@ int bt_bap_unicast_client_ep_send(struct bt_conn *conn, struct bt_bap_ep *ep,
20122022
reset_att_buf(client);
20132023
}
20142024

2025+
if (err == 0) {
2026+
client_ep->cp_ntf_pending = true;
2027+
}
2028+
20152029
return err;
20162030
}
20172031

@@ -2041,6 +2055,8 @@ static void unicast_client_reset(struct bt_bap_ep *ep)
20412055
client_ep->cp_handle = 0U;
20422056
client_ep->handle = 0U;
20432057
(void)memset(&client_ep->discover, 0, sizeof(client_ep->discover));
2058+
client_ep->release_requested = false;
2059+
client_ep->cp_ntf_pending = false;
20442060
/* Need to keep the subscribe params intact for the callback */
20452061
}
20462062

0 commit comments

Comments
 (0)