From e4d1e4d82cffe1afb521078526ddd943ad613091 Mon Sep 17 00:00:00 2001 From: Emil Gydesen Date: Fri, 5 May 2023 11:40:21 +0200 Subject: [PATCH] Bluetooth: Audio: Shell: Add preset copy functions Add common preset copy functions for unicast streams and broadcast sources. Signed-off-by: Emil Gydesen --- subsys/bluetooth/audio/shell/audio.h | 64 ++++++++++++++++++++ subsys/bluetooth/audio/shell/bap.c | 27 +-------- subsys/bluetooth/audio/shell/cap_initiator.c | 33 ++-------- 3 files changed, 72 insertions(+), 52 deletions(-) diff --git a/subsys/bluetooth/audio/shell/audio.h b/subsys/bluetooth/audio/shell/audio.h index 799ae991892d441..fa17e997daaf830 100644 --- a/subsys/bluetooth/audio/shell/audio.h +++ b/subsys/bluetooth/audio/shell/audio.h @@ -17,10 +17,13 @@ #include #include +#include #include #include #include +#include "shell/bt.h" + extern struct bt_csip_set_member_svc_inst *svc_inst; ssize_t audio_ad_data_add(struct bt_data *data, const size_t data_size, const bool discoverable, @@ -185,6 +188,67 @@ static inline void print_base(const struct shell *sh, const struct bt_bap_base * shell_print(sh, "Possible indexes: %s", bis_indexes_str); } #endif /* BROADCAST_SNK_SUBGROUP_CNT > 0 */ + +static inline void copy_unicast_stream_preset(struct unicast_stream *stream, + const struct named_lc3_preset *named_preset) +{ + memcpy(&stream->qos, &named_preset->preset.qos, sizeof(stream->qos)); + memcpy(&stream->codec, &named_preset->preset.codec, sizeof(stream->codec)); + +#if CONFIG_BT_CODEC_MAX_DATA_COUNT > 0 && CONFIG_BT_CODEC_MAX_DATA_LEN > 0 + /* Need to update the `bt_data.data` pointer to the new value after copying the codec */ + for (size_t i = 0U; i < ARRAY_SIZE(stream->codec.data); i++) { + const struct bt_codec_data *preset_data = &named_preset->preset.codec.data[i]; + struct bt_codec_data *data = &stream->codec.data[i]; + + data->data.data = data->value; + data->data.data_len = preset_data->data.data_len; + memcpy(data->value, preset_data->data.data, preset_data->data.data_len); + } +#endif /* CONFIG_BT_CODEC_MAX_METADATA_COUNT > 0 && CONFIG_BT_CODEC_MAX_DATA_LEN > 0 */ + +#if CONFIG_BT_CODEC_MAX_METADATA_COUNT > 0 && CONFIG_BT_CODEC_MAX_DATA_LEN > 0 + for (size_t i = 0U; i < ARRAY_SIZE(stream->codec.meta); i++) { + const struct bt_codec_data *preset_data = &named_preset->preset.codec.meta[i]; + struct bt_codec_data *data = &stream->codec.meta[i]; + + data->data.data = data->value; + data->data.data_len = preset_data->data.data_len; + memcpy(data->value, preset_data->data.data, preset_data->data.data_len); + } +#endif /* CONFIG_BT_CODEC_MAX_METADATA_COUNT > 0 && CONFIG_BT_CODEC_MAX_DATA_LEN > 0 */ +} + +static inline void copy_broadcast_source_preset(struct broadcast_source *source, + const struct named_lc3_preset *named_preset) +{ + memcpy(&source->qos, &named_preset->preset.qos, sizeof(source->qos)); + memcpy(&source->codec, &named_preset->preset.codec, sizeof(source->codec)); + +#if CONFIG_BT_CODEC_MAX_DATA_COUNT > 0 && CONFIG_BT_CODEC_MAX_DATA_LEN > 0 + /* Need to update the `bt_data.data` pointer to the new value after copying the codec */ + for (size_t i = 0U; i < ARRAY_SIZE(source->codec.data); i++) { + const struct bt_codec_data *preset_data = &named_preset->preset.codec.data[i]; + struct bt_codec_data *data = &source->codec.data[i]; + + data->data.data = data->value; + data->data.data_len = preset_data->data.data_len; + memcpy(data->value, preset_data->data.data, preset_data->data.data_len); + } +#endif /* CONFIG_BT_CODEC_MAX_METADATA_COUNT > 0 && CONFIG_BT_CODEC_MAX_DATA_LEN > 0 */ + +#if CONFIG_BT_CODEC_MAX_METADATA_COUNT > 0 && CONFIG_BT_CODEC_MAX_DATA_LEN > 0 + for (size_t i = 0U; i < ARRAY_SIZE(source->codec.meta); i++) { + const struct bt_codec_data *preset_data = &named_preset->preset.codec.meta[i]; + struct bt_codec_data *data = &source->codec.meta[i]; + + data->data.data = data->value; + data->data.data_len = preset_data->data.data_len; + memcpy(data->value, preset_data->data.data, preset_data->data.data_len); + } +#endif /* CONFIG_BT_CODEC_MAX_METADATA_COUNT > 0 && CONFIG_BT_CODEC_MAX_DATA_LEN > 0 */ +} + #endif /* CONFIG_BT_AUDIO */ #endif /* __AUDIO_H */ diff --git a/subsys/bluetooth/audio/shell/bap.c b/subsys/bluetooth/audio/shell/bap.c index aabb7218ad9c9fe..4f292e4b78a64b4 100644 --- a/subsys/bluetooth/audio/shell/bap.c +++ b/subsys/bluetooth/audio/shell/bap.c @@ -1040,29 +1040,7 @@ static int cmd_config(const struct shell *sh, size_t argc, char *argv[]) } uni_stream = CONTAINER_OF(default_stream, struct unicast_stream, stream); - memcpy(&uni_stream->qos, &named_preset->preset.qos, sizeof(uni_stream->qos)); - memcpy(&uni_stream->codec, &named_preset->preset.codec, sizeof(uni_stream->codec)); - /* Need to update the `bt_data.data` pointer to the new value after copying the codec */ - /* Need to copy from the data pointer, as the preset->value is empty, as they are defined as - * compound literals - */ - for (size_t i = 0U; i < ARRAY_SIZE(uni_stream->codec.data); i++) { - const struct bt_codec_data *preset_data = &named_preset->preset.codec.data[i]; - struct bt_codec_data *data = &uni_stream->codec.data[i]; - - data->data.data = data->value; - data->data.data_len = preset_data->data.data_len; - memcpy(data->value, preset_data->data.data, preset_data->data.data_len); - } - - for (size_t i = 0U; i < ARRAY_SIZE(uni_stream->codec.meta); i++) { - const struct bt_codec_data *preset_meta = &named_preset->preset.codec.meta[i]; - struct bt_codec_data *meta = &uni_stream->codec.meta[i]; - - meta->data.data = meta->value; - meta->data.data_len = preset_meta->data.data_len; - memcpy(meta->value, preset_meta->data.data, preset_meta->data.data_len); - } + copy_unicast_stream_preset(uni_stream, named_preset); /* If location has been modifed, we update the location in the codec configuration */ if (location != BT_AUDIO_LOCATION_PROHIBITED) { @@ -1930,8 +1908,7 @@ static int cmd_create_broadcast(const struct shell *sh, size_t argc, } } - memcpy(&default_source.codec, &named_preset->preset.codec, sizeof(default_source.codec)); - memcpy(&default_source.qos, &named_preset->preset.qos, sizeof(default_source.qos)); + copy_broadcast_source_preset(&default_source, named_preset); (void)memset(stream_params, 0, sizeof(stream_params)); for (size_t i = 0; i < ARRAY_SIZE(stream_params); i++) { diff --git a/subsys/bluetooth/audio/shell/cap_initiator.c b/subsys/bluetooth/audio/shell/cap_initiator.c index bbfdfb79e3ca683..3a2a36459e2aee5 100644 --- a/subsys/bluetooth/audio/shell/cap_initiator.c +++ b/subsys/bluetooth/audio/shell/cap_initiator.c @@ -128,27 +128,6 @@ static void populate_connected_conns(struct bt_conn *conn, void *data) } } -static void cap_copy_preset(struct unicast_stream *uni_stream, - const struct named_lc3_preset *name_preset) -{ - memcpy(&uni_stream->qos, &name_preset->preset.qos, sizeof(uni_stream->qos)); - memcpy(&uni_stream->codec, &name_preset->preset.codec, - sizeof(uni_stream->codec)); - - /* Need to update the `bt_data.data` pointer to the new value after copying the codec */ - for (size_t i = 0U; i < ARRAY_SIZE(uni_stream->codec.data); i++) { - struct bt_codec_data *data = &uni_stream->codec.data[i]; - - data->data.data = data->value; - } - - for (size_t i = 0U; i < ARRAY_SIZE(uni_stream->codec.meta); i++) { - struct bt_codec_data *data = &uni_stream->codec.meta[i]; - - data->data.data = data->value; - } -} - static int cmd_cap_initiator_unicast_start(const struct shell *sh, size_t argc, char *argv[]) { @@ -264,7 +243,7 @@ static int cmd_cap_initiator_unicast_start(const struct shell *sh, size_t argc, stream_param[start_param.count].member.member = conn; stream_param[start_param.count].stream = stream; stream_param[start_param.count].ep = snk_ep; - cap_copy_preset(uni_stream, default_sink_preset); + copy_unicast_stream_preset(uni_stream, default_sink_preset); stream_param[start_param.count].codec = &uni_stream->codec; stream_param[start_param.count].qos = &uni_stream->qos; @@ -298,7 +277,7 @@ static int cmd_cap_initiator_unicast_start(const struct shell *sh, size_t argc, stream_param[start_param.count].member.member = conn; stream_param[start_param.count].stream = stream; stream_param[start_param.count].ep = src_ep; - cap_copy_preset(uni_stream, default_source_preset); + copy_unicast_stream_preset(uni_stream, default_source_preset); stream_param[start_param.count].codec = &uni_stream->codec; stream_param[start_param.count].qos = &uni_stream->qos; @@ -399,9 +378,9 @@ static int cmd_cap_initiator_unicast_update(const struct shell *sh, size_t argc, if (ep_info.dir == BT_AUDIO_DIR_SINK) { - cap_copy_preset(uni_stream, default_sink_preset); + copy_unicast_stream_preset(uni_stream, default_sink_preset); } else { - cap_copy_preset(uni_stream, default_source_preset); + copy_unicast_stream_preset(uni_stream, default_source_preset); } params[count].meta = uni_stream->codec.meta; @@ -441,9 +420,9 @@ static int cmd_cap_initiator_unicast_update(const struct shell *sh, size_t argc, params[count].stream = stream; if (ep_info.dir == BT_AUDIO_DIR_SINK) { - cap_copy_preset(uni_stream, default_sink_preset); + copy_unicast_stream_preset(uni_stream, default_sink_preset); } else { - cap_copy_preset(uni_stream, default_source_preset); + copy_unicast_stream_preset(uni_stream, default_source_preset); } params[count].meta = uni_stream->codec.meta;