Skip to content
Open
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
4 changes: 4 additions & 0 deletions doc/releases/release-notes-4.4.rst
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ New APIs and options

* Bluetooth

* Audio

* :c:func:`bt_bap_ep_get_conn`

* Host

* :c:func:`bt_gatt_cb_unregister` Added an API to unregister GATT callback handlers.
Expand Down
15 changes: 15 additions & 0 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
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
35 changes: 35 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
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);
}
7 changes: 7 additions & 0 deletions tests/bsim/bluetooth/audio/src/bap_broadcast_sink_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -565,6 +565,7 @@ static void stream_started_cb(struct bt_bap_stream *stream)
{
struct audio_test_stream *test_stream = audio_test_stream_from_bap_stream(stream);
struct bt_bap_ep_info info;
struct bt_conn *ep_conn;
int err;

memset(&test_stream->last_info, 0, sizeof(test_stream->last_info));
Expand Down Expand Up @@ -603,6 +604,12 @@ static void stream_started_cb(struct bt_bap_stream *stream)
return;
}

ep_conn = bt_bap_ep_get_conn(stream->ep);
if (ep_conn != NULL) {
FAIL("Invalid conn from endpoint: %p", ep_conn);
return;
}

printk("Stream %p started\n", stream);
k_sem_give(&sem_stream_started);

Expand Down
7 changes: 7 additions & 0 deletions tests/bsim/bluetooth/audio/src/bap_broadcast_source_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ static void stream_started_cb(struct bt_bap_stream *stream)
{
struct audio_test_stream *test_stream = audio_test_stream_from_bap_stream(stream);
struct bt_bap_ep_info info;
struct bt_conn *ep_conn;
int err;

test_stream->seq_num = 0U;
Expand Down Expand Up @@ -214,6 +215,12 @@ static void stream_started_cb(struct bt_bap_stream *stream)
return;
}

ep_conn = bt_bap_ep_get_conn(stream->ep);
if (ep_conn != NULL) {
FAIL("Invalid conn from endpoint: %p", ep_conn);
return;
}

err = bap_stream_tx_register(stream);
if (err != 0) {
FAIL("Failed to register stream %p for TX: %d\n", stream, err);
Expand Down
9 changes: 9 additions & 0 deletions tests/bsim/bluetooth/audio/src/bap_unicast_client_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,21 @@ CREATE_FLAG(flag_operation_success);

static void stream_configured(struct bt_bap_stream *stream, const struct bt_bap_qos_cfg_pref *pref)
{
struct bt_conn *ep_conn;

printk("Configured stream %p\n", stream);

/* TODO: The preference should be used/taken into account when
* setting the QoS
*/

ep_conn = bt_bap_ep_get_conn(stream->ep);
if (ep_conn == NULL || stream->conn != ep_conn) {
FAIL("Invalid conn from endpoint: %p", ep_conn);
return;
}
bt_conn_unref(ep_conn);

SET_FLAG(flag_stream_codec_configured);
}

Expand Down
20 changes: 18 additions & 2 deletions tests/bsim/bluetooth/audio/src/bap_unicast_server_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,6 @@ static int lc3_config(struct bt_conn *conn, const struct bt_bap_ep *ep, enum bt_

bt_bap_unicast_server_foreach_ep(conn, print_ase_info, NULL);

SET_FLAG(flag_stream_configured);

*pref = qos_pref;

return 0;
Expand Down Expand Up @@ -222,6 +220,23 @@ static const struct bt_bap_unicast_server_cb unicast_server_cb = {
.release = lc3_release,
};

static void stream_configured_cb(struct bt_bap_stream *stream,
const struct bt_bap_qos_cfg_pref *pref)
{
struct bt_conn *ep_conn;

printk("Configured stream %p\n", stream);

ep_conn = bt_bap_ep_get_conn(stream->ep);
if (ep_conn == NULL || stream->conn != ep_conn) {
FAIL("Invalid conn from endpoint: %p", ep_conn);
return;
}
bt_conn_unref(ep_conn);

SET_FLAG(flag_stream_configured);
}

static void stream_enabled_cb(struct bt_bap_stream *stream)
{
struct bt_bap_ep_info ep_info;
Expand Down Expand Up @@ -279,6 +294,7 @@ static void stream_stopped_cb(struct bt_bap_stream *stream, uint8_t reason)
}

static struct bt_bap_stream_ops stream_ops = {
.configured = stream_configured_cb,
.enabled = stream_enabled_cb,
.started = stream_started_cb,
.stopped = stream_stopped_cb,
Expand Down
Loading