Bluetooth: Audio: Move some unicast shell commands to also support server

Some of the unicast audio shell commands were guarded by
CONFIG_BT_AUDIO_UNICAST_CLIENT and only usable by the client.

This moves some of the commands to a CONFIG_BT_AUDIO_UNICAST
guard instead, so that they are usable for builds with just
the server.

Signed-off-by: Emil Gydesen <emil.gydesen@nordicsemi.no>
This commit is contained in:
Emil Gydesen 2022-10-05 14:20:26 +02:00 committed by Carles Cufí
parent 06fde72092
commit 34715022ef

View file

@ -47,8 +47,6 @@ static struct bt_audio_unicast_group *default_unicast_group;
static struct bt_codec *rcodecs[2][CONFIG_BT_AUDIO_UNICAST_CLIENT_PAC_COUNT];
static struct bt_audio_ep *snks[CONFIG_BT_AUDIO_UNICAST_CLIENT_ASE_SNK_COUNT];
static struct bt_audio_ep *srcs[CONFIG_BT_AUDIO_UNICAST_CLIENT_ASE_SRC_COUNT];
static uint8_t stream_dir(const struct bt_audio_stream *stream);
#endif /* CONFIG_BT_AUDIO_UNICAST_CLIENT */
#endif /* CONFIG_BT_AUDIO_UNICAST */
@ -691,6 +689,75 @@ static struct bt_pacs_cap cap_sink = {
static struct bt_pacs_cap cap_source = {
.codec = &lc3_codec,
};
#if defined(CONFIG_BT_AUDIO_UNICAST)
static uint16_t strmeta(const char *name)
{
if (strcmp(name, "Unspecified") == 0) {
return BT_AUDIO_CONTEXT_TYPE_UNSPECIFIED;
} else if (strcmp(name, "Conversational") == 0) {
return BT_AUDIO_CONTEXT_TYPE_CONVERSATIONAL;
} else if (strcmp(name, "Media") == 0) {
return BT_AUDIO_CONTEXT_TYPE_MEDIA;
} else if (strcmp(name, "Game") == 0) {
return BT_AUDIO_CONTEXT_TYPE_GAME;
} else if (strcmp(name, "Instructional") == 0) {
return BT_AUDIO_CONTEXT_TYPE_INSTRUCTIONAL;
} else if (strcmp(name, "VoiceAssistants") == 0) {
return BT_AUDIO_CONTEXT_TYPE_VOICE_ASSISTANTS;
} else if (strcmp(name, "Live") == 0) {
return BT_AUDIO_CONTEXT_TYPE_LIVE;
} else if (strcmp(name, "SoundEffects") == 0) {
return BT_AUDIO_CONTEXT_TYPE_SOUND_EFFECTS;
} else if (strcmp(name, "Notifications") == 0) {
return BT_AUDIO_CONTEXT_TYPE_NOTIFICATIONS;
} else if (strcmp(name, "Ringtone") == 0) {
return BT_AUDIO_CONTEXT_TYPE_RINGTONE;
} else if (strcmp(name, "Alerts") == 0) {
return BT_AUDIO_CONTEXT_TYPE_ALERTS;
} else if (strcmp(name, "EmergencyAlarm") == 0) {
return BT_AUDIO_CONTEXT_TYPE_EMERGENCY_ALARM;
}
return 0u;
}
static int handle_metadata_update(const char *meta_str,
struct bt_codec_data *meta_out[],
size_t *meta_count_out)
{
static struct bt_codec_data meta[CONFIG_BT_CODEC_MAX_METADATA_COUNT];
size_t meta_count;
/* We create a copy of the preset meta, as the presets cannot be modified */
meta_count = default_preset->preset.codec.meta_count;
(void)memset(meta, 0, sizeof(meta));
for (size_t i = 0U; i < meta_count; i++) {
(void)memcpy(meta[i].value,
default_preset->preset.codec.meta[i].data.data,
default_preset->preset.codec.meta[i].data.data_len);
meta[i].data.data_len = default_preset->preset.codec.meta[i].data.data_len;
meta[i].data.data = meta[i].value;
}
if (meta_str != NULL) {
uint16_t context;
context = strmeta(meta_str);
if (context == 0) {
return -ENOEXEC;
}
/* TODO: Check the type and only overwrite the streaming context */
sys_put_le16(context, meta[0].value);
}
*meta_count_out = meta_count;
*meta_out = meta;
return 0;
}
#if defined(CONFIG_BT_AUDIO_UNICAST_CLIENT)
static uint8_t stream_dir(const struct bt_audio_stream *stream)
@ -866,29 +933,6 @@ static int cmd_discover(const struct shell *sh, size_t argc, char *argv[])
return bt_audio_discover(default_conn, &params);
}
static int cmd_preset(const struct shell *sh, size_t argc, char *argv[])
{
struct named_lc3_preset *named_preset;
named_preset = default_preset;
if (argc > 1) {
named_preset = set_preset(true, argc - 1, argv + 1);
if (named_preset == NULL) {
shell_error(sh, "Unable to parse named_preset %s",
argv[1]);
return -ENOEXEC;
}
}
shell_print(sh, "%s", named_preset->name);
print_codec(&named_preset->preset.codec);
print_qos(&named_preset->preset.qos);
return 0;
}
static int cmd_config(const struct shell *sh, size_t argc, char *argv[])
{
int32_t index, dir;
@ -964,24 +1008,6 @@ static int cmd_config(const struct shell *sh, size_t argc, char *argv[])
return 0;
}
static int cmd_release(const struct shell *sh, size_t argc, char *argv[])
{
int err;
if (default_stream == NULL) {
shell_print(sh, "Not connected");
return -ENOEXEC;
}
err = bt_audio_stream_release(default_stream);
if (err) {
shell_error(sh, "Unable to release Channel");
return -ENOEXEC;
}
return 0;
}
static int cmd_qos(const struct shell *sh, size_t argc, char *argv[])
{
int err;
@ -1028,73 +1054,6 @@ static int cmd_qos(const struct shell *sh, size_t argc, char *argv[])
return 0;
}
static uint16_t strmeta(const char *name)
{
if (strcmp(name, "Unspecified") == 0) {
return BT_AUDIO_CONTEXT_TYPE_UNSPECIFIED;
} else if (strcmp(name, "Conversational") == 0) {
return BT_AUDIO_CONTEXT_TYPE_CONVERSATIONAL;
} else if (strcmp(name, "Media") == 0) {
return BT_AUDIO_CONTEXT_TYPE_MEDIA;
} else if (strcmp(name, "Game") == 0) {
return BT_AUDIO_CONTEXT_TYPE_GAME;
} else if (strcmp(name, "Instructional") == 0) {
return BT_AUDIO_CONTEXT_TYPE_INSTRUCTIONAL;
} else if (strcmp(name, "VoiceAssistants") == 0) {
return BT_AUDIO_CONTEXT_TYPE_VOICE_ASSISTANTS;
} else if (strcmp(name, "Live") == 0) {
return BT_AUDIO_CONTEXT_TYPE_LIVE;
} else if (strcmp(name, "SoundEffects") == 0) {
return BT_AUDIO_CONTEXT_TYPE_SOUND_EFFECTS;
} else if (strcmp(name, "Notifications") == 0) {
return BT_AUDIO_CONTEXT_TYPE_NOTIFICATIONS;
} else if (strcmp(name, "Ringtone") == 0) {
return BT_AUDIO_CONTEXT_TYPE_RINGTONE;
} else if (strcmp(name, "Alerts") == 0) {
return BT_AUDIO_CONTEXT_TYPE_ALERTS;
} else if (strcmp(name, "EmergencyAlarm") == 0) {
return BT_AUDIO_CONTEXT_TYPE_EMERGENCY_ALARM;
}
return 0u;
}
static int handle_metadata_update(const char *meta_str,
struct bt_codec_data *meta_out[],
size_t *meta_count_out)
{
static struct bt_codec_data meta[CONFIG_BT_CODEC_MAX_METADATA_COUNT];
size_t meta_count;
/* We create a copy of the preset meta, as the presets cannot be modified */
meta_count = default_preset->preset.codec.meta_count;
(void)memset(meta, 0, sizeof(meta));
for (size_t i = 0U; i < meta_count; i++) {
(void)memcpy(meta[i].value,
default_preset->preset.codec.meta[i].data.data,
default_preset->preset.codec.meta[i].data.data_len);
meta[i].data.data_len = default_preset->preset.codec.meta[i].data.data_len;
meta[i].data.data = meta[i].value;
}
if (meta_str != NULL) {
uint16_t context;
context = strmeta(meta_str);
if (context == 0) {
return -ENOEXEC;
}
/* TODO: Check the type and only overwrite the streaming context */
sys_put_le16(context, meta[0].value);
}
*meta_count_out = meta_count;
*meta_out = meta;
return 0;
}
static int cmd_enable(const struct shell *sh, size_t argc, char *argv[])
{
struct bt_codec_data *meta;
@ -1126,6 +1085,62 @@ static int cmd_enable(const struct shell *sh, size_t argc, char *argv[])
return 0;
}
static int cmd_stop(const struct shell *sh, size_t argc, char *argv[])
{
int err;
if (default_stream == NULL) {
shell_error(sh, "Not connected");
return -ENOEXEC;
}
err = bt_audio_stream_stop(default_stream);
if (err) {
shell_error(sh, "Unable to start Channel");
return -ENOEXEC;
}
return 0;
}
static int cmd_connect(const struct shell *sh, size_t argc, char *argv[])
{
int err;
err = cmd_config(sh, argc, argv);
if (err) {
return err;
}
connecting = true;
return 0;
}
#endif /* CONFIG_BT_AUDIO_UNICAST_CLIENT */
static int cmd_preset(const struct shell *sh, size_t argc, char *argv[])
{
struct named_lc3_preset *named_preset;
named_preset = default_preset;
if (argc > 1) {
named_preset = set_preset(true, argc - 1, argv + 1);
if (named_preset == NULL) {
shell_error(sh, "Unable to parse named_preset %s",
argv[1]);
return -ENOEXEC;
}
}
shell_print(sh, "%s", named_preset->name);
print_codec(&named_preset->preset.codec);
print_qos(&named_preset->preset.qos);
return 0;
}
#define MAX_META_DATA \
(CONFIG_BT_CODEC_MAX_METADATA_COUNT * sizeof(struct bt_codec_data))
@ -1198,24 +1213,6 @@ static int cmd_disable(const struct shell *sh, size_t argc, char *argv[])
return 0;
}
static int cmd_stop(const struct shell *sh, size_t argc, char *argv[])
{
int err;
if (default_stream == NULL) {
shell_error(sh, "Not connected");
return -ENOEXEC;
}
err = bt_audio_stream_stop(default_stream);
if (err) {
shell_error(sh, "Unable to start Channel");
return -ENOEXEC;
}
return 0;
}
static int cmd_list(const struct shell *sh, size_t argc, char *argv[])
{
int i;
@ -1226,12 +1223,13 @@ static int cmd_list(const struct shell *sh, size_t argc, char *argv[])
struct bt_audio_stream *stream = &streams[i];
if (stream->conn) {
shell_print(sh, " %s#%u: stream %p dir 0x%02x group %p",
stream == default_stream ? "*" : " ", i, stream,
stream_dir(stream), stream->group);
shell_print(sh, " %s#%u: stream %p ep %p group %p",
stream == default_stream ? "*" : " ", i,
stream, stream->ep, stream->group);
}
}
#if defined(CONFIG_BT_AUDIO_UNICAST_CLIENT)
shell_print(sh, "Sinks:");
for (i = 0; i < ARRAY_SIZE(snks); i++) {
@ -1251,24 +1249,29 @@ static int cmd_list(const struct shell *sh, size_t argc, char *argv[])
shell_print(sh, " #%u: ep %p", i, ep);
}
}
#endif /* CONFIG_BT_AUDIO_UNICAST_CLIENT */
return 0;
}
static int cmd_connect(const struct shell *sh, size_t argc, char *argv[])
static int cmd_release(const struct shell *sh, size_t argc, char *argv[])
{
int err;
err = cmd_config(sh, argc, argv);
if (err) {
return err;
if (default_stream == NULL) {
shell_print(sh, "Not connected");
return -ENOEXEC;
}
connecting = true;
err = bt_audio_stream_release(default_stream);
if (err) {
shell_error(sh, "Unable to release Channel");
return -ENOEXEC;
}
return 0;
}
#endif /* CONFIG_BT_AUDIO_UNICAST_CLIENT */
#endif /* CONFIG_BT_AUDIO_UNICAST */
#if defined(CONFIG_BT_AUDIO_BROADCAST_SINK)
static uint32_t accepted_broadcast_id;
@ -1940,10 +1943,10 @@ SHELL_STATIC_SUBCMD_SET_CREATE(audio_cmds,
SHELL_CMD_ARG(term_broadcast_sink, NULL, "",
cmd_term_broadcast_sink, 1, 0),
#endif /* CONFIG_BT_AUDIO_BROADCAST_SINK */
#if defined(CONFIG_BT_AUDIO_UNICAST)
#if defined(CONFIG_BT_AUDIO_UNICAST_CLIENT)
SHELL_CMD_ARG(discover, NULL, "[dir: sink, source]",
cmd_discover, 1, 1),
SHELL_CMD_ARG(preset, NULL, "[preset]", cmd_preset, 1, 1),
SHELL_CMD_ARG(config, NULL,
"<direction: sink, source> <index> [codec] [preset]",
cmd_config, 3, 2),
@ -1951,18 +1954,20 @@ SHELL_STATIC_SUBCMD_SET_CREATE(audio_cmds,
"[preset] [interval] [framing] [latency] [pd] [sdu] [phy]"
" [rtn]", cmd_qos, 1, 8),
SHELL_CMD_ARG(enable, NULL, NULL, cmd_enable, 1, 1),
SHELL_CMD_ARG(metadata, NULL, "[context]", cmd_metadata, 1, 1),
SHELL_CMD_ARG(start, NULL, NULL, cmd_start, 1, 0),
SHELL_CMD_ARG(disable, NULL, NULL, cmd_disable, 1, 0),
SHELL_CMD_ARG(stop, NULL, NULL, cmd_stop, 1, 0),
SHELL_CMD_ARG(release, NULL, NULL, cmd_release, 1, 0),
SHELL_CMD_ARG(list, NULL, NULL, cmd_list, 1, 0),
SHELL_CMD_ARG(connect, NULL,
"<direction: sink, source> <index> [codec] [preset]",
cmd_connect, 3, 2),
#endif /* CONFIG_BT_AUDIO_UNICAST_CLIENT */
SHELL_COND_CMD_ARG(CONFIG_BT_AUDIO_UNICAST, select_unicast, NULL,
"<stream>", cmd_select_unicast, 2, 0),
SHELL_CMD_ARG(preset, NULL, "[preset]", cmd_preset, 1, 1),
SHELL_CMD_ARG(metadata, NULL, "[context]", cmd_metadata, 1, 1),
SHELL_CMD_ARG(start, NULL, NULL, cmd_start, 1, 0),
SHELL_CMD_ARG(disable, NULL, NULL, cmd_disable, 1, 0),
SHELL_CMD_ARG(release, NULL, NULL, cmd_release, 1, 0),
SHELL_CMD_ARG(list, NULL, NULL, cmd_list, 1, 0),
SHELL_CMD_ARG(select_unicast, NULL, "<stream>",
cmd_select_unicast, 2, 0),
#endif /* CONFIG_BT_AUDIO_UNICAST */
SHELL_CMD_ARG(send, NULL, "Send to Audio Stream [data]",
cmd_send, 1, 1),
#if defined(CONFIG_LIBLC3)