From 43ffe3cad9157ca8cefd62a230df5792d1319df7 Mon Sep 17 00:00:00 2001 From: Mariusz Skamra Date: Tue, 18 Oct 2022 22:37:32 +0200 Subject: [PATCH] Bluetooth: audio: pacs: Refactor PAC read handler This makes use of bt_audio_foreach_capability fuction to simplify building the PAC attribute value. Signed-off-by: Mariusz Skamra --- subsys/bluetooth/audio/pacs.c | 107 +++++++++++++--------------------- 1 file changed, 42 insertions(+), 65 deletions(-) diff --git a/subsys/bluetooth/audio/pacs.c b/subsys/bluetooth/audio/pacs.c index 2b4877bce93..2c69bc65946 100644 --- a/subsys/bluetooth/audio/pacs.c +++ b/subsys/bluetooth/audio/pacs.c @@ -58,87 +58,64 @@ static void pac_data_add(struct net_buf_simple *buf, uint8_t num, } } -static int publish_capability(struct bt_conn *conn, uint8_t dir, - uint8_t index, struct bt_codec *codec) +struct pac_records_build_data { + struct bt_pacs_read_rsp *rsp; + struct net_buf_simple *buf; +}; + +static bool build_pac_records(const struct bt_audio_capability *capability, void *user_data) { - struct bt_audio_capability *cap; - sys_slist_t *lst; - uint8_t i; + struct pac_records_build_data *data = user_data; + struct net_buf_simple *buf = data->buf; + struct bt_pac_meta *meta; + struct bt_codec *codec; + struct bt_pac *pac; - if (dir == BT_AUDIO_DIR_SINK) { - lst = &snks; - } else if (dir == BT_AUDIO_DIR_SOURCE) { - lst = &srcs; - } else { - BT_ERR("Invalid endpoint dir: %u", dir); - return -EINVAL; - } + codec = capability->codec; - i = 0; - SYS_SLIST_FOR_EACH_CONTAINER(lst, cap, _node) { - if (i != index) { - i++; - continue; - } + pac = net_buf_simple_add(buf, sizeof(*pac)); + pac->codec.id = codec->id; + pac->codec.cid = sys_cpu_to_le16(codec->cid); + pac->codec.vid = sys_cpu_to_le16(codec->vid); + pac->cc_len = buf->len; - (void)memcpy(codec, cap->codec, sizeof(*codec)); + BT_DBG("Parsing codec config data"); + pac_data_add(buf, codec->data_count, codec->data); - return 0; - } + /* Buffer size shall never be below PAC len since we are just + * append data. + */ + __ASSERT_NO_MSG(buf->len >= pac->cc_len); - return -ENOENT; + pac->cc_len = buf->len - pac->cc_len; + + meta = net_buf_simple_add(buf, sizeof(*meta)); + meta->len = buf->len; + BT_DBG("Parsing metadata"); + pac_data_add(buf, codec->meta_count, codec->meta); + meta->len = buf->len - meta->len; + + BT_DBG("pac #%u: codec capability len %u metadata len %u", + data->rsp->num_pac, pac->cc_len, meta->len); + + data->rsp->num_pac++; + + return true; } static void get_pac_records(struct bt_conn *conn, enum bt_audio_dir dir, struct net_buf_simple *buf) { - struct bt_pacs_read_rsp *rsp; + struct pac_records_build_data data; /* Reset if buffer before using */ net_buf_simple_reset(buf); - rsp = net_buf_simple_add(buf, sizeof(*rsp)); - rsp->num_pac = 0; + data.rsp = net_buf_simple_add(buf, sizeof(*data.rsp)); + data.rsp->num_pac = 0; + data.buf = buf; - while (true) { - struct bt_pac_meta *meta; - struct bt_codec codec; - struct bt_pac *pac; - int err; - - err = publish_capability(conn, dir, rsp->num_pac, &codec); - if (err != 0) { - break; - } - - pac = net_buf_simple_add(buf, sizeof(*pac)); - - pac->codec.id = codec.id; - pac->codec.cid = sys_cpu_to_le16(codec.cid); - pac->codec.vid = sys_cpu_to_le16(codec.vid); - pac->cc_len = buf->len; - - BT_DBG("Parsing codec config data"); - pac_data_add(buf, codec.data_count, codec.data); - - /* Buffer size shall never be below PAC len since we are just - * append data. - */ - __ASSERT_NO_MSG(buf->len >= pac->cc_len); - - pac->cc_len = buf->len - pac->cc_len; - - meta = net_buf_simple_add(buf, sizeof(*meta)); - meta->len = buf->len; - BT_DBG("Parsing metadata"); - pac_data_add(buf, codec.meta_count, codec.meta); - meta->len = buf->len - meta->len; - - BT_DBG("pac #%u: codec capability len %u metadata len %u", - rsp->num_pac, pac->cc_len, meta->len); - - rsp->num_pac++; - } + bt_audio_foreach_capability(dir, build_pac_records, &data); } static void available_context_cfg_changed(const struct bt_gatt_attr *attr, uint16_t value)