Skip to content

Commit

Permalink
Bluetooth: audio: ascs: Register ISO server on ASCS init
Browse files Browse the repository at this point in the history
This moves the ISO server registration to bt_ascs_init.
When the ASCS gets cleaned up (the callbacks are unregistered), the ISO
server gets unregistered.

Signed-off-by: Mariusz Skamra <mariusz.skamra@codecoup.pl>
  • Loading branch information
MariuszSkamra authored and carlescufi committed Mar 28, 2023
1 parent 08aedc6 commit 58de595
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 23 deletions.
46 changes: 25 additions & 21 deletions subsys/bluetooth/audio/ascs.c
Original file line number Diff line number Diff line change
Expand Up @@ -584,28 +584,10 @@ static int ascs_iso_accept(const struct bt_iso_accept_info *info,
static int ascs_iso_listen(struct bt_bap_stream *stream)
{
struct bt_bap_stream **free_stream = NULL;
static struct bt_iso_server iso_server = {
.sec_level = BT_SECURITY_L2,
.accept = ascs_iso_accept,
};
static bool server;
int err;

LOG_DBG("stream %p conn %p", stream, (void *)stream->conn);

if (server) {
goto done;
}

err = bt_iso_server_register(&iso_server);
if (err) {
LOG_ERR("bt_iso_server_register: %d", err);
return err;
}

server = true;
LOG_DBG("stream %p conn %p", stream, (void *)stream->conn);

done:
for (size_t i = 0U; i < ARRAY_SIZE(enabling); i++) {
if (enabling[i] == stream) {
return 0;
Expand Down Expand Up @@ -2707,9 +2689,28 @@ static int control_point_notify(struct bt_conn *conn, const void *data, uint16_t
return bt_gatt_notify_uuid(conn, BT_UUID_ASCS_ASE_CP, ascs_svc.attrs, data, len);
}

void bt_ascs_init(const struct bt_bap_unicast_server_cb *cb)
static struct bt_iso_server iso_server = {
.sec_level = BT_SECURITY_L2,
.accept = ascs_iso_accept,
};

int bt_ascs_init(const struct bt_bap_unicast_server_cb *cb)
{
int err;

if (unicast_server_cb != NULL) {
return -EALREADY;
}

err = bt_iso_server_register(&iso_server);
if (err) {
LOG_ERR("Failed to register ISO server %d", err);
return err;
}

unicast_server_cb = cb;

return 0;
}

static void ase_cleanup(struct bt_ascs_ase *ase)
Expand Down Expand Up @@ -2751,6 +2752,9 @@ void bt_ascs_cleanup(void)
session->conn = NULL;
}

unicast_server_cb = NULL;
if (unicast_server_cb != NULL) {
bt_iso_server_unregister(&iso_server);
unicast_server_cb = NULL;
}
}
#endif /* BT_BAP_UNICAST_SERVER */
2 changes: 1 addition & 1 deletion subsys/bluetooth/audio/ascs_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ static inline const char *bt_ascs_reason_str(uint8_t reason)
return "Unknown";
}

void bt_ascs_init(const struct bt_bap_unicast_server_cb *cb);
int bt_ascs_init(const struct bt_bap_unicast_server_cb *cb);
void bt_ascs_cleanup(void);

void ascs_ep_set_state(struct bt_bap_ep *ep, uint8_t state);
Expand Down
8 changes: 7 additions & 1 deletion subsys/bluetooth/audio/bap_unicast_server.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ static const struct bt_bap_unicast_server_cb *unicast_server_cb;

int bt_bap_unicast_server_register_cb(const struct bt_bap_unicast_server_cb *cb)
{
int err;

CHECKIF(cb == NULL) {
LOG_DBG("cb is NULL");
return -EINVAL;
Expand All @@ -33,8 +35,12 @@ int bt_bap_unicast_server_register_cb(const struct bt_bap_unicast_server_cb *cb)
return -EALREADY;
}

err = bt_ascs_init(cb);
if (err != 0) {
return err;
}

unicast_server_cb = cb;
bt_ascs_init(unicast_server_cb);

return 0;
}
Expand Down
2 changes: 2 additions & 0 deletions tests/bluetooth/audio/mocks/include/iso.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@
#define ISO_FFF_FAKES_LIST(FAKE) \
FAKE(bt_iso_chan_send) \
FAKE(bt_iso_server_register) \
FAKE(bt_iso_server_unregister) \
FAKE(bt_iso_chan_disconnect) \

DECLARE_FAKE_VALUE_FUNC(int, bt_iso_chan_send, struct bt_iso_chan *, struct net_buf *, uint16_t,
uint32_t);
DECLARE_FAKE_VALUE_FUNC(int, bt_iso_server_register, struct bt_iso_server *);
DECLARE_FAKE_VALUE_FUNC(int, bt_iso_server_unregister, struct bt_iso_server *);
DECLARE_FAKE_VALUE_FUNC(int, bt_iso_chan_disconnect, struct bt_iso_chan *);

#endif /* MOCKS_ISO_H_ */
1 change: 1 addition & 0 deletions tests/bluetooth/audio/mocks/src/iso.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@
DEFINE_FAKE_VALUE_FUNC(int, bt_iso_chan_send, struct bt_iso_chan *, struct net_buf *, uint16_t,
uint32_t);
DEFINE_FAKE_VALUE_FUNC(int, bt_iso_server_register, struct bt_iso_server *);
DEFINE_FAKE_VALUE_FUNC(int, bt_iso_server_unregister, struct bt_iso_server *);
DEFINE_FAKE_VALUE_FUNC(int, bt_iso_chan_disconnect, struct bt_iso_chan *);

0 comments on commit 58de595

Please sign in to comment.