| 1 |
/* |
| 2 |
* BlueALSA - cmd-codec.c |
| 3 |
* Copyright (c) 2016-2024 Arkadiusz Bokowy |
| 4 |
* |
| 5 |
* This file is a part of bluez-alsa. |
| 6 |
* |
| 7 |
* This project is licensed under the terms of the MIT license. |
| 8 |
* |
| 9 |
*/ |
| 10 |
|
| 11 |
#include <stdbool.h> |
| 12 |
#include <errno.h> |
| 13 |
#include <getopt.h> |
| 14 |
#include <stdint.h> |
| 15 |
#include <stdio.h> |
| 16 |
#include <stdlib.h> |
| 17 |
#include <string.h> |
| 18 |
|
| 19 |
#include <dbus/dbus.h> |
| 20 |
|
| 21 |
#include "cli.h" |
| 22 |
#include "shared/dbus-client-pcm.h" |
| 23 |
#include "shared/hex.h" |
| 24 |
|
| 25 |
static void usage(const char *command) { |
| 26 |
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); |
| 28 |
printf("\nOptions:\n" |
| 29 |
" -h, --help\t\tShow this message and exit\n" |
| 30 |
" -c, --channels=NUM\tSelect configuration with NUM channels\n" |
| 31 |
" -s, --sampling=NUM\tSelect configuration with NUM sampling frequency\n" |
| 32 |
" -f, --force\t\tForce codec configuration (skip conformance check)\n" |
| 33 |
"\nPositional arguments:\n" |
| 34 |
" PCM-PATH\tBlueALSA PCM D-Bus object path\n" |
| 35 |
" CODEC\t\tCodec identifier for setting new codec\n" |
| 36 |
" CONFIG\tOptional configuration for new codec\n" |
| 37 |
"\nNote:\n" |
| 38 |
" This command requires BlueZ version >= 5.52 for SEP support.\n" |
| 39 |
); |
| 40 |
} |
| 41 |
|
| 42 |
static int cmd_codec_func(int argc, char *argv[]) { |
| 43 |
|
| 44 |
int opt; |
| 45 |
const char *opts = "hqvc:s:f"; |
| 46 |
const struct option longopts[] = { |
| 47 |
{ "help", no_argument, NULL, 'h' }, |
| 48 |
{ "quiet", no_argument, NULL, 'q' }, |
| 49 |
{ "verbose", no_argument, NULL, 'v' }, |
| 50 |
{ "channels", required_argument, NULL, 'c' }, |
| 51 |
{ "sampling", required_argument, NULL, 's' }, |
| 52 |
{ "force", no_argument, NULL, 'f' }, |
| 53 |
{ 0 }, |
| 54 |
}; |
| 55 |
|
| 56 |
unsigned int channels = 0; |
| 57 |
unsigned int sampling = 0; |
| 58 |
bool force = false; |
| 59 |
|
| 60 |
opterr = 0; |
| 61 |
while ((opt = getopt_long(argc, argv, opts, longopts, NULL)) != -1) { |
| 62 |
if (cli_parse_common_options(opt)) |
| 63 |
continue; |
| 64 |
switch (opt) { |
| 65 |
case 'h' /* --help */ : |
| 66 |
usage(argv[0]); |
| 67 |
return EXIT_SUCCESS; |
| 68 |
case 'c' /* --channels */ : |
| 69 |
channels = atoi(optarg); |
| 70 |
break; |
| 71 |
case 's' /* --sampling */ : |
| 72 |
sampling = atoi(optarg); |
| 73 |
break; |
| 74 |
case 'f' /* --force */ : |
| 75 |
force = true; |
| 76 |
break; |
| 77 |
default: |
| 78 |
cmd_print_error("Invalid argument '%s'", argv[optind - 1]); |
| 79 |
return EXIT_FAILURE; |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
if (argc - optind < 1) { |
| 84 |
cmd_print_error("Missing BlueALSA PCM path argument"); |
| 85 |
return EXIT_FAILURE; |
| 86 |
} |
| 87 |
if (argc - optind > 2) { |
| 88 |
cmd_print_error("Invalid number of arguments"); |
| 89 |
return EXIT_FAILURE; |
| 90 |
} |
| 91 |
|
| 92 |
DBusError err = DBUS_ERROR_INIT; |
| 93 |
const char *path = argv[optind]; |
| 94 |
|
| 95 |
struct ba_pcm pcm; |
| 96 |
if (!cli_get_ba_pcm(path, &pcm, &err)) { |
| 97 |
cmd_print_error("Couldn't get BlueALSA PCM: %s", err.message); |
| 98 |
return EXIT_FAILURE; |
| 99 |
} |
| 100 |
|
| 101 |
if (argc - optind == 1) { |
| 102 |
cli_print_pcm_available_codecs(&pcm, NULL); |
| 103 |
cli_print_pcm_selected_codec(&pcm); |
| 104 |
return EXIT_SUCCESS; |
| 105 |
} |
| 106 |
|
| 107 |
char *codec = argv[optind + 1]; |
| 108 |
uint8_t codec_config[64] = { 0 }; |
| 109 |
ssize_t codec_config_len = 0; |
| 110 |
unsigned int flags = BA_PCM_SELECT_CODEC_FLAG_NONE; |
| 111 |
int result = EXIT_FAILURE; |
| 112 |
|
| 113 |
char *codec_config_hex; |
| 114 |
/* split the given string into name and configuration components */ |
| 115 |
if ((codec_config_hex = strchr(codec, ':')) != NULL) { |
| 116 |
*codec_config_hex++ = '\0'; |
| 117 |
|
| 118 |
size_t codec_config_hex_len; |
| 119 |
if ((codec_config_hex_len = strlen(codec_config_hex)) > sizeof(codec_config) * 2) { |
| 120 |
dbus_set_error(&err, DBUS_ERROR_FAILED, "Invalid codec configuration: %s", codec_config_hex); |
| 121 |
goto fail; |
| 122 |
} |
| 123 |
|
| 124 |
if ((codec_config_len = hex2bin(codec_config_hex, codec_config, codec_config_hex_len)) == -1) { |
| 125 |
dbus_set_error(&err, DBUS_ERROR_FAILED, "%s", strerror(errno)); |
| 126 |
goto fail; |
| 127 |
} |
| 128 |
|
| 129 |
} |
| 130 |
|
| 131 |
if (force) |
| 132 |
flags |= BA_PCM_SELECT_CODEC_FLAG_NON_CONFORMANT; |
| 133 |
|
| 134 |
if (!ba_dbus_pcm_select_codec(&config.dbus, path, ba_dbus_pcm_codec_get_canonical_name(codec), |
| 135 |
codec_config, codec_config_len, channels, sampling, flags, &err)) |
| 136 |
goto fail; |
| 137 |
|
| 138 |
result = EXIT_SUCCESS; |
| 139 |
|
| 140 |
fail: |
| 141 |
if (dbus_error_is_set(&err)) |
| 142 |
cmd_print_error("Couldn't select BlueALSA PCM Codec: %s", err.message); |
| 143 |
return result; |
| 144 |
} |
| 145 |
|
| 146 |
const struct cli_command cmd_codec = { |
| 147 |
"codec", |
| 148 |
"Get or set PCM Bluetooth codec", |
| 149 |
cmd_codec_func, |
| 150 |
}; |