Skip to content

Commit cd76dde

Browse files
committed
Bluetooth: BAP: Add bt_bap_ep_get_conn
Add a new function, bt_bap_ep_get_conn, which returns the ACL connection for the endpoint. This works because endpoints are specific to an ACL in BAP. The function returns a pointer with a new reference similar to the lookup functions from conn.h The conn pointer was not added to the bt_bap_ep_info struct, as doing so would be more likely to cause reference leaks if the caller did not care about the conn pointer when using bt_bap_ep_get_info. It can be added later if that is requested. Signed-off-by: Emil Gydesen <[email protected]>
1 parent 8786061 commit cd76dde

File tree

7 files changed

+96
-0
lines changed

7 files changed

+96
-0
lines changed

include/zephyr/bluetooth/audio/bap.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -882,6 +882,21 @@ struct bt_bap_ep_info {
882882
*/
883883
int bt_bap_ep_get_info(const struct bt_bap_ep *ep, struct bt_bap_ep_info *info);
884884

885+
/**
886+
* @brief Get the pointer to the ACL connection of an endpoint
887+
*
888+
* The caller gets a new reference to the connection object, if not NULL, which must be
889+
* released with bt_conn_unref() once done using the object.
890+
*
891+
* @param ep The endpoint to get the ACL connection of
892+
*
893+
* @return The ACL connection pointer.
894+
* Will always be NULL for broadcast endpoints.
895+
* Will be NULL for Unicast Server endpoints if the endpoint is not configured by a client.
896+
* Will be NULL for Unicast Client endpoints if @p does not match a discovered endpoint.
897+
*/
898+
struct bt_conn *bt_bap_ep_get_conn(const struct bt_bap_ep *ep);
899+
885900
/**
886901
* @brief Basic Audio Profile stream structure.
887902
*

subsys/bluetooth/audio/ascs.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3263,4 +3263,17 @@ bool bt_ascs_has_ep(const struct bt_bap_ep *ep)
32633263
{
32643264
return PART_OF_ARRAY(ascs.ase_pool, ep);
32653265
}
3266+
3267+
struct bt_conn *bt_ascs_ep_get_conn(const struct bt_bap_ep *ep)
3268+
{
3269+
struct bt_ascs_ase *ase = CONTAINER_OF(ep, struct bt_ascs_ase, ep);
3270+
3271+
__ASSERT_NO_MSG(bt_ascs_has_ep(ep));
3272+
3273+
if (ase->conn == NULL) {
3274+
return NULL;
3275+
}
3276+
3277+
return bt_conn_ref(ase->conn);
3278+
}
32663279
#endif /* BT_BAP_UNICAST_SERVER */

subsys/bluetooth/audio/ascs_internal.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,5 +365,6 @@ void bt_ascs_foreach_ep(struct bt_conn *conn, bt_bap_ep_func_t func, void *user_
365365
int bt_ascs_register(uint8_t snk_cnt, uint8_t src_cnt);
366366
int bt_ascs_unregister(void);
367367
bool bt_ascs_has_ep(const struct bt_bap_ep *ep);
368+
struct bt_conn *bt_ascs_ep_get_conn(const struct bt_bap_ep *ep);
368369

369370
#endif /* BT_ASCS_INTERNAL_H */

subsys/bluetooth/audio/bap_internal.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,3 +147,5 @@ bool bt_bap_broadcast_sink_has_ep(const struct bt_bap_ep *ep);
147147
bool bt_bap_broadcast_source_has_ep(const struct bt_bap_ep *ep);
148148
bool bt_bap_unicast_client_has_ep(const struct bt_bap_ep *ep);
149149
bool bt_bap_unicast_server_has_ep(const struct bt_bap_ep *ep);
150+
struct bt_conn *bt_bap_unicast_client_ep_get_conn(const struct bt_bap_ep *ep);
151+
struct bt_conn *bt_bap_unicast_server_ep_get_conn(const struct bt_bap_ep *ep);

subsys/bluetooth/audio/bap_stream.c

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,31 @@ int bt_bap_ep_get_info(const struct bt_bap_ep *ep, struct bt_bap_ep_info *info)
173173
return 0;
174174
}
175175

176+
struct bt_conn *bt_bap_ep_get_conn(const struct bt_bap_ep *ep)
177+
{
178+
struct bt_conn *conn;
179+
180+
if (ep == NULL) {
181+
LOG_DBG("ep is NULL");
182+
183+
return NULL;
184+
}
185+
186+
if ((IS_ENABLED(CONFIG_BT_BAP_BROADCAST_SOURCE) && bt_bap_broadcast_source_has_ep(ep)) ||
187+
(IS_ENABLED(CONFIG_BT_BAP_BROADCAST_SINK) && bt_bap_broadcast_sink_has_ep(ep))) {
188+
conn = NULL;
189+
} else if (IS_ENABLED(CONFIG_BT_BAP_UNICAST_CLIENT) && bt_bap_unicast_client_has_ep(ep)) {
190+
conn = bt_bap_unicast_client_ep_get_conn(ep);
191+
} else if (IS_ENABLED(CONFIG_BT_BAP_UNICAST_SERVER) && bt_bap_unicast_server_has_ep(ep)) {
192+
conn = bt_bap_unicast_server_ep_get_conn(ep);
193+
} else {
194+
LOG_DBG("Invalid endpoint %p", ep);
195+
conn = NULL;
196+
}
197+
198+
return conn;
199+
}
200+
176201
enum bt_bap_ascs_reason bt_audio_verify_qos(const struct bt_bap_qos_cfg *qos)
177202
{
178203
if (qos->interval < BT_ISO_SDU_INTERVAL_MIN ||

subsys/bluetooth/audio/bap_unicast_client.c

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -460,6 +460,41 @@ bool bt_bap_unicast_client_has_ep(const struct bt_bap_ep *ep)
460460
return false;
461461
}
462462

463+
struct bt_conn *bt_bap_unicast_client_ep_get_conn(const struct bt_bap_ep *ep)
464+
{
465+
for (size_t i = 0U; i < ARRAY_SIZE(uni_cli_insts); i++) {
466+
#if CONFIG_BT_BAP_UNICAST_CLIENT_ASE_SNK_COUNT > 0
467+
if (PART_OF_ARRAY(uni_cli_insts[i].snks, ep)) {
468+
ARRAY_FOR_EACH_PTR(uni_cli_insts[i].snks, client_ep) {
469+
if (&client_ep->ep == ep) {
470+
if (client_ep->handle == BAP_HANDLE_UNUSED) {
471+
return NULL;
472+
}
473+
474+
return bt_conn_lookup_index((uint8_t)i);
475+
}
476+
}
477+
}
478+
#endif /* CONFIG_BT_BAP_UNICAST_CLIENT_ASE_SNK_COUNT > 0 */
479+
480+
#if CONFIG_BT_BAP_UNICAST_CLIENT_ASE_SRC_COUNT > 0
481+
if (PART_OF_ARRAY(uni_cli_insts[i].srcs, ep)) {
482+
ARRAY_FOR_EACH_PTR(uni_cli_insts[i].srcs, client_ep) {
483+
if (&client_ep->ep == ep) {
484+
if (client_ep->handle == BAP_HANDLE_UNUSED) {
485+
return NULL;
486+
}
487+
488+
return bt_conn_lookup_index((uint8_t)i);
489+
}
490+
}
491+
}
492+
#endif /* CONFIG_BT_BAP_UNICAST_CLIENT_ASE_SRC_COUNT > 0 */
493+
}
494+
495+
return NULL;
496+
}
497+
463498
static void unicast_client_ep_init(struct bt_bap_ep *ep, uint16_t handle, uint8_t dir)
464499
{
465500
struct bt_bap_unicast_client_ep *client_ep;

subsys/bluetooth/audio/bap_unicast_server.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,3 +227,8 @@ bool bt_bap_unicast_server_has_ep(const struct bt_bap_ep *ep)
227227
{
228228
return bt_ascs_has_ep(ep);
229229
}
230+
231+
struct bt_conn *bt_bap_unicast_server_ep_get_conn(const struct bt_bap_ep *ep)
232+
{
233+
return bt_ascs_ep_get_conn(ep);
234+
}

0 commit comments

Comments
 (0)