From 34ffd2867592dd5e808da7d7b237ff15f2d035a6 Mon Sep 17 00:00:00 2001 From: Emil Gydesen Date: Fri, 13 Jun 2025 15:22:33 +0200 Subject: [PATCH] Bluetooth: BAP: BA: Refactor discover to not do read Refactor the bt_bap_broadcast_assistant_discover function to not read receives at the end of discovery. This makes the function more true to what it is supposed to do, and significantly reduces the complexity of the procedure and the read callback. Users will be required to, if wanted, to read the receive state themselves with the existing bt_bap_broadcast_assistant_read_recv_state. Signed-off-by: Emil Gydesen --- doc/releases/migration-guide-4.2.rst | 5 ++ .../bluetooth/audio/bap_broadcast_assistant.c | 66 ++++--------------- 2 files changed, 16 insertions(+), 55 deletions(-) diff --git a/doc/releases/migration-guide-4.2.rst b/doc/releases/migration-guide-4.2.rst index 28f67c1231f03..3b0dfba85d520 100644 --- a/doc/releases/migration-guide-4.2.rst +++ b/doc/releases/migration-guide-4.2.rst @@ -431,6 +431,11 @@ Bluetooth Audio * ``BT_AUDIO_CONTEXT_TYPE_PROHIBITED`` has been renamed to :c:enumerator:`BT_AUDIO_CONTEXT_TYPE_NONE`. (:github:`89506`) +* :c:func:`bt_bap_broadcast_assistant_discover` will now no longer perform reads of the remote BASS + receive states at the end of the procedure. Users will have to manually call + :c:func:`bt_bap_broadcast_assistant_read_recv_state` to read the existing receive states, if any, + prior to performing any operations. + Bluetooth HCI ============= diff --git a/subsys/bluetooth/audio/bap_broadcast_assistant.c b/subsys/bluetooth/audio/bap_broadcast_assistant.c index 9fcbc4017a4fb..5ae1b5a1fab11 100644 --- a/subsys/bluetooth/audio/bap_broadcast_assistant.c +++ b/subsys/bluetooth/audio/bap_broadcast_assistant.c @@ -2,7 +2,7 @@ /* * Copyright (c) 2019 Bose Corporation - * Copyright (c) 2022-2023 Nordic Semiconductor ASA + * Copyright (c) 2022-2025 Nordic Semiconductor ASA * Copyright (c) 2024 Demant A/S * * SPDX-License-Identifier: Apache-2.0 @@ -565,20 +565,15 @@ static uint8_t read_recv_state_cb(struct bt_conn *conn, uint8_t err, const void *data, uint16_t length) { struct bap_broadcast_assistant_instance *inst = inst_by_conn(conn); + bool active_recv_state = data != NULL && length != 0; + struct bt_bap_scan_delegator_recv_state recv_state; + uint16_t handle = params->single.handle; + int cb_err = err; if (inst == NULL) { return BT_GATT_ITER_STOP; } - uint16_t handle = params->single.handle; - uint8_t last_handle_index = inst->recv_state_cnt - 1; - uint16_t last_handle = inst->recv_state_handles[last_handle_index]; - struct bt_bap_scan_delegator_recv_state recv_state; - int cb_err = err; - bool active_recv_state = data != NULL && length != 0; - - /* TODO: Split discovery and receive state characteristic read */ - (void)memset(params, 0, sizeof(*params)); LOG_DBG("%s receive state", active_recv_state ? "Active " : "Inactive"); @@ -611,48 +606,12 @@ static uint8_t read_recv_state_cb(struct bt_conn *conn, uint8_t err, if (cb_err != 0) { LOG_DBG("err %d", cb_err); - - if (atomic_test_bit(inst->flags, BAP_BA_FLAG_DISCOVER_IN_PROGRESS)) { - bap_broadcast_assistant_discover_complete(conn, cb_err, 0); - } else { - atomic_clear_bit(inst->flags, BAP_BA_FLAG_BUSY); - bap_broadcast_assistant_recv_state_changed(conn, cb_err, NULL); - } - } else if (handle == last_handle) { - if (atomic_test_bit(inst->flags, BAP_BA_FLAG_DISCOVER_IN_PROGRESS)) { - const uint8_t recv_state_cnt = inst->recv_state_cnt; - - bap_broadcast_assistant_discover_complete(conn, cb_err, recv_state_cnt); - } else { - atomic_clear_bit(inst->flags, BAP_BA_FLAG_BUSY); - bap_broadcast_assistant_recv_state_changed(conn, cb_err, - active_recv_state ? - &recv_state : NULL); - } + atomic_clear_bit(inst->flags, BAP_BA_FLAG_BUSY); + bap_broadcast_assistant_recv_state_changed(conn, cb_err, NULL); } else { - for (uint8_t i = 0U; i < inst->recv_state_cnt; i++) { - if (handle != inst->recv_state_handles[i]) { - continue; - } - - if (i + 1 < ARRAY_SIZE(inst->recv_state_handles)) { - cb_err = read_recv_state(inst, i + 1); - if (cb_err != 0) { - LOG_DBG("Failed to read receive state: %d", cb_err); - - if (atomic_test_bit(inst->flags, - BAP_BA_FLAG_DISCOVER_IN_PROGRESS)) { - bap_broadcast_assistant_discover_complete( - conn, cb_err, 0); - } else { - atomic_clear_bit(inst->flags, BAP_BA_FLAG_BUSY); - bap_broadcast_assistant_recv_state_changed( - conn, cb_err, NULL); - } - } - } - break; - } + atomic_clear_bit(inst->flags, BAP_BA_FLAG_BUSY); + bap_broadcast_assistant_recv_state_changed(conn, cb_err, + active_recv_state ? &recv_state : NULL); } return BT_GATT_ITER_STOP; @@ -686,10 +645,7 @@ static uint8_t char_discover_func(struct bt_conn *conn, LOG_DBG("Found %u BASS receive states", inst->recv_state_cnt); (void)memset(params, 0, sizeof(*params)); - err = read_recv_state(inst, 0); - if (err != 0) { - bap_broadcast_assistant_discover_complete(conn, err, 0); - } + bap_broadcast_assistant_discover_complete(conn, 0, inst->recv_state_cnt); return BT_GATT_ITER_STOP; }