Skip to content

Commit 3e45d84

Browse files
committed
Pass A2DP configuration with NAME:CONFIG notation
In all places, the A2DP codec configuration is printed as NAME:CONFIG. Also, when selecting configuration with ALSA plug-in, the notation is just like that. However, when selecting codec with our CLI the config needs to be passed as a separate argument. This might be confusing to some. This commit unifies the notation of A2DP codec name with the configuration in all places. However, it slightly breaks command line syntax with previous releases of bluez-alsa.
1 parent 0201fdb commit 3e45d84

File tree

4 files changed

+33
-33
lines changed

4 files changed

+33
-33
lines changed

doc/bluealsa-cli.1.rst

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ bluealsa-cli
66
a simple command line interface for the BlueALSA D-Bus API
77
----------------------------------------------------------
88

9-
:Date: July 2023
9+
:Date: January 2024
1010
:Manual section: 1
1111
:Manual group: General Commands Manual
1212
:Version: $VERSION$
@@ -89,7 +89,7 @@ info *PCM_PATH*
8989
The list of available A2DP codecs requires BlueZ SEP support
9090
(BlueZ >= 5.52)
9191

92-
codec [--force] *PCM_PATH* [*CODEC* [*CONFIG*]]
92+
codec [--force] *PCM_PATH* [*CODEC*[:*CONFIG*]]
9393
Get or set the Bluetooth codec used by the given PCM.
9494

9595
If *CODEC* is given, change the codec to be used by the given PCM. This

src/asound/bluealsa-pcm.c

+16-21
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
* bluealsa-pcm.c
3-
* Copyright (c) 2016-2022 Arkadiusz Bokowy
3+
* Copyright (c) 2016-2024 Arkadiusz Bokowy
44
*
55
* This file is a part of bluez-alsa.
66
*
@@ -26,6 +26,7 @@
2626
#include <strings.h>
2727
#include <sys/eventfd.h>
2828
#include <sys/ioctl.h>
29+
#include <sys/param.h>
2930
#include <sys/time.h>
3031
#include <unistd.h>
3132

@@ -1260,42 +1261,36 @@ static int bluealsa_set_hw_constraint(struct bluealsa_pcm *pcm) {
12601261

12611262
static bool bluealsa_select_pcm_codec(struct bluealsa_pcm *pcm, const char *codec, DBusError *err) {
12621263

1263-
uint8_t config[64];
1264+
char name[32] = { 0 };
1265+
size_t name_len = sizeof(name) - 1;
1266+
uint8_t config[64] = { 0 };
12641267
ssize_t config_len = 0;
1265-
bool ret = false;
12661268

1267-
char *codec_name;
1268-
if ((codec_name = strdup(codec)) == NULL) {
1269-
dbus_set_error(err, DBUS_ERROR_NO_MEMORY, NULL);
1270-
return false;
1271-
}
1272-
1273-
char *config_hex;
1269+
const char *config_hex;
12741270
/* split the given string into name and configuration components */
1275-
if ((config_hex = strchr(codec_name, ':')) != NULL) {
1276-
*config_hex++ = '\0';
1271+
if ((config_hex = strchr(codec, ':')) != NULL) {
1272+
name_len = MIN(name_len, config_hex - codec);
1273+
config_hex++;
12771274

12781275
size_t config_hex_len;
12791276
if ((config_hex_len = strlen(config_hex)) > sizeof(config) * 2) {
12801277
dbus_set_error(err, DBUS_ERROR_FAILED, "Invalid codec configuration: %s", config_hex);
1281-
goto fail;
1278+
return false;
12821279
}
1280+
12831281
if ((config_len = hex2bin(config_hex, config, config_hex_len)) == -1) {
12841282
dbus_set_error(err, DBUS_ERROR_FAILED, "%s", strerror(errno));
1285-
goto fail;
1283+
return false;
12861284
}
12871285

12881286
}
12891287

1288+
strncpy(name, codec, name_len);
12901289
if (!ba_dbus_pcm_select_codec(&pcm->dbus_ctx, pcm->ba_pcm.pcm_path,
1291-
ba_dbus_pcm_codec_get_canonical_name(codec_name), config, config_len, 0, err))
1292-
goto fail;
1293-
1294-
ret = true;
1290+
ba_dbus_pcm_codec_get_canonical_name(name), config, config_len, 0, err))
1291+
return false;
12951292

1296-
fail:
1297-
free(codec_name);
1298-
return ret;
1293+
return true;
12991294
}
13001295

13011296
static bool bluealsa_update_pcm_volume(struct bluealsa_pcm *pcm,

test/test-utils-cli.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ CK_START_TEST(test_codec) {
240240
/* check selecting A2DP codec without SEP support (with our mock BlueZ) */
241241
ck_assert_int_eq(run_bluealsa_cli(output, sizeof(output),
242242
"codec", "-vf", "/org/bluealsa/hci0/dev_12_34_56_78_9A_BC/a2dpsrc/sink",
243-
"SBC", "11150255",
243+
"SBC:11150255",
244244
NULL), EXIT_FAILURE);
245245

246246
spawn_terminate(&sp_ba_mock, 0);

utils/cli/cmd-codec.c

+14-9
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424

2525
static void usage(const char *command) {
2626
printf("Get or set the Bluetooth codec used by the given PCM.\n\n");
27-
cli_print_usage("%s [OPTION]... PCM-PATH [CODEC [CONFIG]]", command);
27+
cli_print_usage("%s [OPTION]... PCM-PATH [CODEC[:CONFIG]]", command);
2828
printf("\nOptions:\n"
2929
" -h, --help\t\tShow this message and exit\n"
3030
" -f, --force\t\tForce codec configuration (skip conformance check)\n"
@@ -72,7 +72,7 @@ static int cmd_codec_func(int argc, char *argv[]) {
7272
cmd_print_error("Missing BlueALSA PCM path argument");
7373
return EXIT_FAILURE;
7474
}
75-
if (argc - optind > 3) {
75+
if (argc - optind > 2) {
7676
cmd_print_error("Invalid number of arguments");
7777
return EXIT_FAILURE;
7878
}
@@ -92,30 +92,35 @@ static int cmd_codec_func(int argc, char *argv[]) {
9292
return EXIT_SUCCESS;
9393
}
9494

95-
const char *codec = ba_dbus_pcm_codec_get_canonical_name(argv[optind + 1]);
95+
char *codec = argv[optind + 1];
96+
uint8_t codec_config[64] = { 0 };
97+
ssize_t codec_config_len = 0;
98+
unsigned int flags = BA_PCM_SELECT_CODEC_FLAG_NONE;
9699
int result = EXIT_FAILURE;
97100

98-
unsigned int flags = BA_PCM_SELECT_CODEC_FLAG_NONE;
99-
uint8_t codec_config[64];
100-
ssize_t codec_config_len = 0;
101+
char *codec_config_hex;
102+
/* split the given string into name and configuration components */
103+
if ((codec_config_hex = strchr(codec, ':')) != NULL) {
104+
*codec_config_hex++ = '\0';
101105

102-
if (argc - optind == 3) {
103106
size_t codec_config_hex_len;
104-
const char *codec_config_hex = argv[optind + 2];
105107
if ((codec_config_hex_len = strlen(codec_config_hex)) > sizeof(codec_config) * 2) {
106108
dbus_set_error(&err, DBUS_ERROR_FAILED, "Invalid codec configuration: %s", codec_config_hex);
107109
goto fail;
108110
}
111+
109112
if ((codec_config_len = hex2bin(codec_config_hex, codec_config, codec_config_hex_len)) == -1) {
110113
dbus_set_error(&err, DBUS_ERROR_FAILED, "%s", strerror(errno));
111114
goto fail;
112115
}
116+
113117
}
114118

115119
if (force)
116120
flags |= BA_PCM_SELECT_CODEC_FLAG_NON_CONFORMANT;
117121

118-
if (!ba_dbus_pcm_select_codec(&config.dbus, path, codec,
122+
if (!ba_dbus_pcm_select_codec(&config.dbus, path,
123+
ba_dbus_pcm_codec_get_canonical_name(codec),
119124
codec_config, codec_config_len, flags, &err))
120125
goto fail;
121126

0 commit comments

Comments
 (0)