Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/releases/release-notes-4.3.rst
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ New APIs and options
* :c:struct:`bt_bap_stream` now contains an ``iso`` field as a reference to the ISO channel
* :c:func:`bt_bap_unicast_group_get_info`
* :c:func:`bt_cap_unicast_group_get_info`
* :c:func:`bt_bap_ep_get_conn`

* Host

Expand Down
30 changes: 26 additions & 4 deletions include/zephyr/bluetooth/audio/bap.h
Original file line number Diff line number Diff line change
Expand Up @@ -882,6 +882,21 @@ struct bt_bap_ep_info {
*/
int bt_bap_ep_get_info(const struct bt_bap_ep *ep, struct bt_bap_ep_info *info);

/**
* @brief Get the pointer to the ACL connection of an endpoint
*
* The caller gets a new reference to the connection object, if not NULL, which must be
* released with bt_conn_unref() once done using the object.
*
* @param ep The endpoint to get the ACL connection of
*
* @return The ACL connection pointer.
* Will always be NULL for broadcast endpoints.
* Will be NULL for Unicast Server endpoints if the endpoint is not configured by a client.
* Will be NULL for Unicast Client endpoints if @p does not match a discovered endpoint.
*/
struct bt_conn *bt_bap_ep_get_conn(const struct bt_bap_ep *ep);

/**
* @brief Basic Audio Profile stream structure.
*
Expand Down Expand Up @@ -1967,17 +1982,24 @@ struct bt_bap_unicast_client_cb {
/**
* @brief Register unicast client callbacks.
*
* Only one callback structure can be registered, and attempting to
* registering more than one will result in an error.
*
* @param cb Unicast client callback structure.
* @param cb Unicast client callback structure to register.
*
* @retval 0 Success
* @retval -EINVAL @p cb is NULL.
* @retval -EEXIST @p cb is already registered.
*/
int bt_bap_unicast_client_register_cb(struct bt_bap_unicast_client_cb *cb);

/**
* @brief Unregister unicast client callbacks.
*
* @param cb Unicast client callback structure to unregister.
*
* @retval 0 Success
* @retval -EINVAL @p cb is NULL or @p cb was not registered
*/
int bt_bap_unicast_client_unregister_cb(struct bt_bap_unicast_client_cb *cb);

/**
* @brief Discover remote capabilities and endpoints
*
Expand Down
12 changes: 12 additions & 0 deletions subsys/bluetooth/audio/ascs.c
Original file line number Diff line number Diff line change
Expand Up @@ -3259,4 +3259,16 @@ int bt_ascs_unregister(void)
return err;
}

struct bt_conn *bt_ascs_ep_get_conn(const struct bt_bap_ep *ep)
{
struct bt_ascs_ase *ase = CONTAINER_OF(ep, struct bt_ascs_ase, ep);

__ASSERT_NO_MSG(bt_ascs_has_ep(ep));

if (ase->conn == NULL) {
return NULL;
}

return bt_conn_ref(ase->conn);
}
#endif /* BT_BAP_UNICAST_SERVER */
1 change: 1 addition & 0 deletions subsys/bluetooth/audio/ascs_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -364,5 +364,6 @@ void bt_ascs_foreach_ep(struct bt_conn *conn, bt_bap_ep_func_t func, void *user_

int bt_ascs_register(uint8_t snk_cnt, uint8_t src_cnt);
int bt_ascs_unregister(void);
struct bt_conn *bt_ascs_ep_get_conn(const struct bt_bap_ep *ep);

#endif /* BT_ASCS_INTERNAL_H */
2 changes: 2 additions & 0 deletions subsys/bluetooth/audio/bap_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -147,3 +147,5 @@ bool bt_bap_broadcast_sink_has_ep(const struct bt_bap_ep *ep);
bool bt_bap_broadcast_source_has_ep(const struct bt_bap_ep *ep);
bool bt_bap_unicast_client_has_ep(const struct bt_bap_ep *ep);
bool bt_bap_unicast_server_has_ep(const struct bt_bap_ep *ep);
struct bt_conn *bt_bap_unicast_client_ep_get_conn(const struct bt_bap_ep *ep);
struct bt_conn *bt_bap_unicast_server_ep_get_conn(const struct bt_bap_ep *ep);
25 changes: 25 additions & 0 deletions subsys/bluetooth/audio/bap_stream.c
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,31 @@ int bt_bap_ep_get_info(const struct bt_bap_ep *ep, struct bt_bap_ep_info *info)
return 0;
}

struct bt_conn *bt_bap_ep_get_conn(const struct bt_bap_ep *ep)
{
struct bt_conn *conn;

if (ep == NULL) {
LOG_DBG("ep is NULL");

return NULL;
}

if ((IS_ENABLED(CONFIG_BT_BAP_BROADCAST_SOURCE) && bt_bap_broadcast_source_has_ep(ep)) ||
(IS_ENABLED(CONFIG_BT_BAP_BROADCAST_SINK) && bt_bap_broadcast_sink_has_ep(ep))) {
conn = NULL;
} else if (IS_ENABLED(CONFIG_BT_BAP_UNICAST_CLIENT) && bt_bap_unicast_client_has_ep(ep)) {
conn = bt_bap_unicast_client_ep_get_conn(ep);
} else if (IS_ENABLED(CONFIG_BT_BAP_UNICAST_SERVER) && bt_bap_unicast_server_has_ep(ep)) {
conn = bt_bap_unicast_server_ep_get_conn(ep);
} else {
LOG_DBG("Invalid endpoint %p", ep);
conn = NULL;
}

return conn;
}

enum bt_bap_ascs_reason bt_audio_verify_qos(const struct bt_bap_qos_cfg *qos)
{
if (qos->interval < BT_ISO_SDU_INTERVAL_MIN ||
Expand Down
50 changes: 50 additions & 0 deletions subsys/bluetooth/audio/bap_unicast_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,41 @@ bool bt_bap_unicast_client_has_ep(const struct bt_bap_ep *ep)
return false;
}

struct bt_conn *bt_bap_unicast_client_ep_get_conn(const struct bt_bap_ep *ep)
{
for (size_t i = 0U; i < ARRAY_SIZE(uni_cli_insts); i++) {
#if CONFIG_BT_BAP_UNICAST_CLIENT_ASE_SNK_COUNT > 0
if (PART_OF_ARRAY(uni_cli_insts[i].snks, ep)) {
ARRAY_FOR_EACH_PTR(uni_cli_insts[i].snks, client_ep) {
if (&client_ep->ep == ep) {
if (client_ep->handle == BAP_HANDLE_UNUSED) {
return NULL;
}

return bt_conn_lookup_index((uint8_t)i);
}
}
}
#endif /* CONFIG_BT_BAP_UNICAST_CLIENT_ASE_SNK_COUNT > 0 */

#if CONFIG_BT_BAP_UNICAST_CLIENT_ASE_SRC_COUNT > 0
if (PART_OF_ARRAY(uni_cli_insts[i].srcs, ep)) {
ARRAY_FOR_EACH_PTR(uni_cli_insts[i].srcs, client_ep) {
if (&client_ep->ep == ep) {
if (client_ep->handle == BAP_HANDLE_UNUSED) {
return NULL;
}

return bt_conn_lookup_index((uint8_t)i);
}
}
}
#endif /* CONFIG_BT_BAP_UNICAST_CLIENT_ASE_SRC_COUNT > 0 */
}

return NULL;
}

static void unicast_client_ep_init(struct bt_bap_ep *ep, uint16_t handle, uint8_t dir)
{
struct bt_bap_unicast_client_ep *client_ep;
Expand Down Expand Up @@ -4684,3 +4719,18 @@ int bt_bap_unicast_client_register_cb(struct bt_bap_unicast_client_cb *cb)

return 0;
}

int bt_bap_unicast_client_unregister_cb(struct bt_bap_unicast_client_cb *cb)
{
if (cb == NULL) {
LOG_DBG("cb was NULL");
return -EINVAL;
}

if (!sys_slist_find_and_remove(&unicast_client_cbs, &cb->_node)) {
LOG_DBG("cb was not registered");
return -EALREADY;
}

return 0;
}
5 changes: 5 additions & 0 deletions subsys/bluetooth/audio/bap_unicast_server.c
Original file line number Diff line number Diff line change
Expand Up @@ -227,3 +227,8 @@ bool bt_bap_unicast_server_has_ep(const struct bt_bap_ep *ep)
{
return bt_ascs_has_ep(ep);
}

struct bt_conn *bt_bap_unicast_server_ep_get_conn(const struct bt_bap_ep *ep)
{
return bt_ascs_ep_get_conn(ep);
}
19 changes: 18 additions & 1 deletion subsys/bluetooth/audio/cap_initiator.c
Original file line number Diff line number Diff line change
Expand Up @@ -1158,9 +1158,11 @@ bool bt_cap_initiator_valid_unicast_audio_start_param(
const union bt_cap_set_member *member = &stream_param->member;
const struct bt_cap_stream *cap_stream = stream_param->stream;
const struct bt_audio_codec_cfg *codec_cfg = stream_param->codec_cfg;
const struct bt_bap_ep *ep = stream_param->ep;
const struct bt_bap_stream *bap_stream;
const struct bt_conn *member_conn =
bt_cap_common_get_member_conn(param->type, member);
struct bt_conn *ep_conn;

if (member == NULL) {
LOG_DBG("param->members[%zu] is NULL", i);
Expand All @@ -1182,11 +1184,26 @@ bool bt_cap_initiator_valid_unicast_audio_start_param(
return false;
}

CHECKIF(stream_param->ep == NULL) {
if (ep == NULL) {
LOG_DBG("param->stream_params[%zu].ep is NULL", i);
return false;
}

ep_conn = bt_bap_ep_get_conn(ep);
if (ep_conn == NULL) {
LOG_DBG("param->stream_params[%zu].ep is invalid", i);
return false;
}
if (ep_conn != member_conn) {
LOG_DBG("param->stream_params[%zu].ep conn %p does not match "
"param->members[%zu] %p",
i, ep_conn, i, member_conn);
bt_conn_unref(ep_conn);

return false;
}
bt_conn_unref(ep_conn);

CHECKIF(member == NULL) {
LOG_DBG("param->stream_params[%zu].member is NULL", i);
return false;
Expand Down
5 changes: 3 additions & 2 deletions tests/bluetooth/audio/cap_initiator/include/test_common.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
/* test_common.h */

/*
* Copyright (c) 2024 Nordic Semiconductor ASA
* Copyright (c) 2024-2025 Nordic Semiconductor ASA
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <stdint.h>

#include <zephyr/bluetooth/audio/bap.h>
#include <zephyr/bluetooth/audio/bap_lc3_preset.h>
Expand All @@ -14,7 +15,7 @@
void test_mocks_init(void);
void test_mocks_cleanup(void);

void test_conn_init(struct bt_conn *conn);
void test_conn_init(struct bt_conn *conn, uint8_t index);

void test_unicast_set_state(struct bt_cap_stream *cap_stream, struct bt_conn *conn,
struct bt_bap_ep *ep, struct bt_bap_lc3_preset *preset,
Expand Down
2 changes: 1 addition & 1 deletion tests/bluetooth/audio/cap_initiator/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ struct cap_initiator_test_suite_fixture {
static void cap_initiator_test_suite_fixture_init(struct cap_initiator_test_suite_fixture *fixture)
{
for (size_t i = 0; i < ARRAY_SIZE(fixture->conns); i++) {
test_conn_init(&fixture->conns[i]);
test_conn_init(&fixture->conns[i], i);
}
}

Expand Down
8 changes: 5 additions & 3 deletions tests/bluetooth/audio/cap_initiator/src/test_common.c
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
/* test_common.c - common procedures for unit test of CAP initiator */

/*
* Copyright (c) 2024 Nordic Semiconductor ASA
* Copyright (c) 2024-2025 Nordic Semiconductor ASA
*
* SPDX-License-Identifier: Apache-2.0
*/

#include <stdint.h>

#include <zephyr/bluetooth/audio/bap.h>
#include <zephyr/bluetooth/audio/bap_lc3_preset.h>
#include <zephyr/bluetooth/audio/cap.h>
Expand All @@ -32,9 +34,9 @@ void test_mocks_cleanup(void)
mock_cap_initiator_cleanup();
}

void test_conn_init(struct bt_conn *conn)
void test_conn_init(struct bt_conn *conn, uint8_t index)
{
conn->index = 0;
conn->index = index;
conn->info.type = BT_CONN_TYPE_LE;
conn->info.role = BT_CONN_ROLE_CENTRAL;
conn->info.state = BT_CONN_STATE_CONNECTED;
Expand Down
Loading
Loading