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 <mariusz.skamra@codecoup.pl>
This commit is contained in:
parent
7c1f6d93dc
commit
43ffe3cad9
1 changed files with 42 additions and 65 deletions
|
@ -58,68 +58,29 @@ 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 bt_audio_capability *cap;
|
||||
sys_slist_t *lst;
|
||||
uint8_t i;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
i = 0;
|
||||
SYS_SLIST_FOR_EACH_CONTAINER(lst, cap, _node) {
|
||||
if (i != index) {
|
||||
i++;
|
||||
continue;
|
||||
}
|
||||
|
||||
(void)memcpy(codec, cap->codec, sizeof(*codec));
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
return -ENOENT;
|
||||
}
|
||||
|
||||
static void get_pac_records(struct bt_conn *conn, enum bt_audio_dir dir,
|
||||
struct net_buf_simple *buf)
|
||||
{
|
||||
struct pac_records_build_data {
|
||||
struct bt_pacs_read_rsp *rsp;
|
||||
struct net_buf_simple *buf;
|
||||
};
|
||||
|
||||
/* Reset if buffer before using */
|
||||
net_buf_simple_reset(buf);
|
||||
|
||||
rsp = net_buf_simple_add(buf, sizeof(*rsp));
|
||||
rsp->num_pac = 0;
|
||||
|
||||
while (true) {
|
||||
static bool build_pac_records(const struct bt_audio_capability *capability, void *user_data)
|
||||
{
|
||||
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_codec *codec;
|
||||
struct bt_pac *pac;
|
||||
int err;
|
||||
|
||||
err = publish_capability(conn, dir, rsp->num_pac, &codec);
|
||||
if (err != 0) {
|
||||
break;
|
||||
}
|
||||
codec = capability->codec;
|
||||
|
||||
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->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);
|
||||
pac_data_add(buf, codec->data_count, codec->data);
|
||||
|
||||
/* Buffer size shall never be below PAC len since we are just
|
||||
* append data.
|
||||
|
@ -131,14 +92,30 @@ static void get_pac_records(struct bt_conn *conn, enum bt_audio_dir dir,
|
|||
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);
|
||||
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);
|
||||
data->rsp->num_pac, pac->cc_len, meta->len);
|
||||
|
||||
rsp->num_pac++;
|
||||
}
|
||||
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 pac_records_build_data data;
|
||||
|
||||
/* Reset if buffer before using */
|
||||
net_buf_simple_reset(buf);
|
||||
|
||||
data.rsp = net_buf_simple_add(buf, sizeof(*data.rsp));
|
||||
data.rsp->num_pac = 0;
|
||||
data.buf = buf;
|
||||
|
||||
bt_audio_foreach_capability(dir, build_pac_records, &data);
|
||||
}
|
||||
|
||||
static void available_context_cfg_changed(const struct bt_gatt_attr *attr, uint16_t value)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue