Debian Codegraph @

utils/cli/cli.c

raw
1 /*
2 * BlueALSA - cli.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 #if HAVE_CONFIG_H
12 # include <config.h>
13 #endif
14
15 #include <getopt.h>
16 #include <libgen.h>
17 #include <stdarg.h>
18 #include <stdbool.h>
19 #include <stdint.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <strings.h>
24 #include <sys/param.h>
25
26 #include <dbus/dbus.h>
27
28 #include "cli.h"
29 #include "shared/dbus-client.h"
30 #include "shared/defs.h"
31 #include "shared/log.h"
32
33 /* Initialize global configuration variable. */
34 struct cli_config config = {
35 .quiet = false,
36 .verbose = 0,
37 };
38
39 static const char *transport_code_to_string(int transport_code) {
40 switch (transport_code) {
41 case BA_PCM_TRANSPORT_A2DP_SOURCE:
42 return "A2DP-source";
43 case BA_PCM_TRANSPORT_A2DP_SINK:
44 return "A2DP-sink";
45 case BA_PCM_TRANSPORT_HFP_AG:
46 return "HFP-AG";
47 case BA_PCM_TRANSPORT_HFP_HF:
48 return "HFP-HF";
49 case BA_PCM_TRANSPORT_HSP_AG:
50 return "HSP-AG";
51 case BA_PCM_TRANSPORT_HSP_HS:
52 return "HSP-HS";
53 case BA_PCM_TRANSPORT_MASK_A2DP:
54 return "A2DP";
55 case BA_PCM_TRANSPORT_MASK_HFP:
56 return "HFP";
57 case BA_PCM_TRANSPORT_MASK_HSP:
58 return "HSP";
59 case BA_PCM_TRANSPORT_MASK_SCO:
60 return "SCO";
61 case BA_PCM_TRANSPORT_MASK_AG:
62 return "AG";
63 case BA_PCM_TRANSPORT_MASK_HF:
64 return "HF";
65 default:
66 return "Invalid";
67 }
68 }
69
70 static const char *pcm_mode_to_string(int pcm_mode) {
71 switch (pcm_mode) {
72 case BA_PCM_MODE_SINK:
73 return "sink";
74 case BA_PCM_MODE_SOURCE:
75 return "source";
76 default:
77 return "Invalid";
78 }
79 }
80
81 static const char *pcm_format_to_string(int pcm_format) {
82 switch (pcm_format) {
83 case 0x0108:
84 return "U8";
85 case 0x8210:
86 return "S16_LE";
87 case 0x8318:
88 return "S24_3LE";
89 case 0x8418:
90 return "S24_LE";
91 case 0x8420:
92 return "S32_LE";
93 default:
94 return "Invalid";
95 }
96 }
97
98 static void print_pcm_codec(const struct ba_pcm_codec *codec) {
99
100 printf("%s", codec->name);
101
102 if (config.verbose && codec->data_len > 0) {
103 printf(":");
104 for (size_t i = 0; i < codec->data_len; i++)
105 printf("%.2x", codec->data[i]);
106 }
107
108 if (config.verbose >= 2) {
109
110 printf(" [channels:");
111 for (size_t i = 0; i < ARRAYSIZE(codec->channels) && codec->channels[i] != 0; i++)
112 printf(" %u", codec->channels[i]);
113 printf("]");
114
115 printf(" [sampling:");
116 for (size_t i = 0; i < ARRAYSIZE(codec->sampling) && codec->sampling[i] != 0; i++)
117 printf(" %u", codec->sampling[i]);
118 printf("]");
119
120 }
121
122 }
123
124 void cli_get_ba_services(cli_get_ba_services_cb func, void *data, DBusError *err) {
125
126 DBusMessage *msg = NULL, *rep = NULL;
127
128 if ((msg = dbus_message_new_method_call(DBUS_SERVICE_DBUS,
129 DBUS_PATH_DBUS, DBUS_INTERFACE_DBUS, "ListNames")) == NULL) {
130 dbus_set_error_const(err, DBUS_ERROR_NO_MEMORY, NULL);
131 goto fail;
132 }
133
134 if ((rep = dbus_connection_send_with_reply_and_block(config.dbus.conn,
135 msg, DBUS_TIMEOUT_USE_DEFAULT, err)) == NULL) {
136 goto fail;
137 }
138
139 DBusMessageIter iter;
140 if (!dbus_message_iter_init(rep, &iter)) {
141 dbus_set_error(err, DBUS_ERROR_INVALID_SIGNATURE, "Empty response message");
142 goto fail;
143 }
144
145 DBusMessageIter iter_names;
146 for (dbus_message_iter_recurse(&iter, &iter_names);
147 dbus_message_iter_get_arg_type(&iter_names) != DBUS_TYPE_INVALID;
148 dbus_message_iter_next(&iter_names)) {
149
150 if (dbus_message_iter_get_arg_type(&iter_names) != DBUS_TYPE_STRING) {
151 char *signature = dbus_message_iter_get_signature(&iter);
152 dbus_set_error(err, DBUS_ERROR_INVALID_SIGNATURE,
153 "Incorrect signature: %s != as", signature);
154 dbus_free(signature);
155 goto fail;
156 }
157
158 const char *name;
159 dbus_message_iter_get_basic(&iter_names, &name);
160 if (!func(name, data)) {
161 break;
162 }
163
164 }
165
166 fail:
167 if (msg != NULL)
168 dbus_message_unref(msg);
169 if (rep != NULL)
170 dbus_message_unref(rep);
171 }
172
173 bool cli_get_ba_pcm(const char *path, struct ba_pcm *pcm, DBusError *err) {
174
175 struct ba_pcm *pcms = NULL;
176 size_t pcms_count = 0;
177
178 if (!dbus_validate_path(path, err))
179 return false;
180
181 if (!ba_dbus_pcm_get_all(&config.dbus, &pcms, &pcms_count, err))
182 return false;
183
184 bool found = false;
185 for (size_t i = 0; i < pcms_count; i++)
186 if (strcmp(pcms[i].pcm_path, path) == 0) {
187 memcpy(pcm, &pcms[i], sizeof(*pcm));
188 found = true;
189 break;
190 }
191
192 free(pcms);
193
194 if (!found)
195 dbus_set_error(err, DBUS_ERROR_UNKNOWN_OBJECT,
196 "Object path not found: '%s'", path);
197 return found;
198 }
199
200 bool cli_parse_common_options(int opt) {
201 switch (opt) {
202 case 'q' /* --quiet */ :
203 config.quiet = true;
204 return true;
205 case 'v' /* --verbose */ :
206 config.verbose++;
207 return true;
208 default:
209 return false;
210 }
211 }
212
213 bool cli_parse_value_on_off(const char *value, bool *out) {
214
215 static const char * const value_on[] = { "on", "yes", "true", "y", "1" };
216 static const char * const value_off[] = { "off", "no", "false", "n", "0" };
217
218 for (size_t i = 0; i < ARRAYSIZE(value_on); i++)
219 if (strcasecmp(value, value_on[i]) == 0) {
220 *out = true;
221 return true;
222 }
223
224 for (size_t i = 0; i < ARRAYSIZE(value_off); i++)
225 if (strcasecmp(value, value_off[i]) == 0) {
226 *out = false;
227 return true;
228 }
229
230 return false;
231 }
232
233 void cli_print_adapters(const struct ba_service_props *props) {
234 printf("Adapters:");
235 for (size_t i = 0; i < props->adapters_len; i++)
236 printf(" %s", props->adapters[i]);
237 printf("\n");
238 }
239
240 void cli_print_profiles_and_codecs(const struct ba_service_props *props) {
241 printf("Profiles:\n");
242 for (size_t i = 0; i < props->profiles_len; i++) {
243 printf(" %-11s :", props->profiles[i]);
244 size_t len = strlen(props->profiles[i]);
245 for (size_t ii = 0; ii < props->codecs_len; ii++)
246 if (strncmp(props->codecs[ii], props->profiles[i], len) == 0)
247 printf(" %s", &props->codecs[ii][len + 1]);
248 printf("\n");
249 }
250 }
251
252 void cli_print_pcm_available_codecs(const struct ba_pcm *pcm, DBusError *err) {
253
254 printf("Available codecs:");
255
256 struct ba_pcm_codecs codecs = { 0 };
257 if (!ba_dbus_pcm_codecs_get(&config.dbus, pcm->pcm_path, &codecs, err))
258 goto fail;
259
260 for (size_t i = 0; i < codecs.codecs_len; i++) {
261 printf("%s", config.verbose >= 2 ? "\n\t" : " ");
262 print_pcm_codec(&codecs.codecs[i]);
263 }
264
265 ba_dbus_pcm_codecs_free(&codecs);
266
267 fail:
268 if (codecs.codecs_len == 0)
269 printf(" [ Unknown ]");
270 printf("\n");
271 }
272
273 void cli_print_pcm_selected_codec(const struct ba_pcm *pcm) {
274 printf("Selected codec:%s", config.verbose >= 2 ? "\n\t" : " ");
275 print_pcm_codec(&pcm->codec);
276 printf("\n");
277 }
278
279 void cli_print_pcm_soft_volume(const struct ba_pcm *pcm) {
280 printf("SoftVolume: %s\n", pcm->soft_volume ? "true" : "false");
281 }
282
283 void cli_print_pcm_volume(const struct ba_pcm *pcm) {
284 if (pcm->channels == 2)
285 printf("Volume: L: %u R: %u\n", pcm->volume.ch1_volume, pcm->volume.ch2_volume);
286 else
287 printf("Volume: %u\n", pcm->volume.ch1_volume);
288 }
289
290 void cli_print_pcm_mute(const struct ba_pcm *pcm) {
291 if (pcm->channels == 2)
292 printf("Muted: L: %s R: %s\n", pcm->volume.ch1_muted ? "true" : "false",
293 pcm->volume.ch2_muted ? "true" : "false");
294 else
295 printf("Muted: %s\n", pcm->volume.ch1_muted ? "true" : "false");
296 }
297
298 void cli_print_pcm_properties(const struct ba_pcm *pcm, DBusError *err) {
299 printf("Device: %s\n", pcm->device_path);
300 printf("Sequence: %u\n", pcm->sequence);
301 printf("Transport: %s\n", transport_code_to_string(pcm->transport));
302 printf("Mode: %s\n", pcm_mode_to_string(pcm->mode));
303 printf("Running: %s\n", pcm->running ? "true" : "false");
304 printf("Format: %s\n", pcm_format_to_string(pcm->format));
305 printf("Channels: %d\n", pcm->channels);
306 printf("Sampling: %d Hz\n", pcm->sampling);
307 cli_print_pcm_available_codecs(pcm, err);
308 cli_print_pcm_selected_codec(pcm);
309 printf("Delay: %#.1f ms\n", (double)pcm->delay / 10);
310 printf("DelayAdjustment: %#.1f ms\n", (double)pcm->delay_adjustment / 10);
311 cli_print_pcm_soft_volume(pcm);
312 cli_print_pcm_volume(pcm);
313 cli_print_pcm_mute(pcm);
314 }
315
316 static const char *progname = NULL;
317 void cli_print_usage(const char *format, ...) {
318
319 char usage[256];
320 va_list va;
321
322 va_start(va, format);
323 vsnprintf(usage, sizeof(usage), format, va);
324 va_end(va);
325
326 printf("Usage:\n %s %s\n", progname, usage);
327 }
328
329 extern const struct cli_command cmd_list_services;
330 extern const struct cli_command cmd_list_pcms;
331 extern const struct cli_command cmd_status;
332 extern const struct cli_command cmd_info;
333 extern const struct cli_command cmd_codec;
334 extern const struct cli_command cmd_delay_adjustment;
335 extern const struct cli_command cmd_monitor;
336 extern const struct cli_command cmd_mute;
337 extern const struct cli_command cmd_open;
338 extern const struct cli_command cmd_softvol;
339 extern const struct cli_command cmd_volume;
340
341 static const struct cli_command *commands[] = {
342 &cmd_list_services,
343 &cmd_list_pcms,
344 &cmd_status,
345 &cmd_info,
346 &cmd_codec,
347 &cmd_delay_adjustment,
348 &cmd_volume,
349 &cmd_mute,
350 &cmd_softvol,
351 &cmd_monitor,
352 &cmd_open,
353 };
354
355 static void usage(const char *name) {
356
357 size_t command_name_max_len = 0;
358 for (size_t i = 0; i < ARRAYSIZE(commands); i++) {
359 size_t len = strlen(commands[i]->name);
360 command_name_max_len = MAX(command_name_max_len, len);
361 }
362
363 printf("%s - Utility to issue BlueALSA API commands\n\n", name);
364 cli_print_usage("[OPTION]... COMMAND [COMMAND-ARGS]");
365 printf("\nOptions:\n");
366 printf(" -h, --help Show this message and exit\n");
367 printf(" -V, --version Show version and exit\n");
368 printf(" -B, --dbus=NAME BlueALSA service name suffix\n");
369 printf(" -q, --quiet Do not print any error messages\n");
370 printf(" -v, --verbose Increase output verbosity\n");
371 printf("\nCommands:\n");
372 for (size_t i = 0; i < ARRAYSIZE(commands); i++)
373 printf(" %-*s %s\n", (int)(command_name_max_len),
374 commands[i]->name, commands[i]->description);
375
376 }
377
378 int main(int argc, char *argv[]) {
379
380 progname = argv[0];
381
382 int opt;
383 const char *opts = "+hqvB:V";
384 const struct option longopts[] = {
385 { "help", no_argument, NULL, 'h' },
386 { "quiet", no_argument, NULL, 'q' },
387 { "verbose", no_argument, NULL, 'v' },
388 { "dbus", required_argument, NULL, 'B' },
389 { "version", no_argument, NULL, 'V' },
390 { 0 },
391 };
392
393 char dbus_ba_service[32] = BLUEALSA_SERVICE;
394
395 while ((opt = getopt_long(argc, argv, opts, longopts, NULL)) != -1) {
396 if (cli_parse_common_options(opt))
397 continue;
398 switch (opt) {
399 case 'h' /* --help */ :
400 usage(argv[0]);
401 return EXIT_SUCCESS;
402 case 'V' /* --version */ :
403 printf("%s\n", PACKAGE_VERSION);
404 return EXIT_SUCCESS;
405 case 'B' /* --dbus=NAME */ :
406 snprintf(dbus_ba_service, sizeof(dbus_ba_service), BLUEALSA_SERVICE ".%s", optarg);
407 if (!dbus_validate_bus_name(dbus_ba_service, NULL)) {
408 error("Invalid BlueALSA D-Bus service name: %s", dbus_ba_service);
409 return EXIT_FAILURE;
410 }
411 break;
412 default:
413 fprintf(stderr, "Try '%s --help' for more information.\n", argv[0]);
414 return EXIT_FAILURE;
415 }
416 }
417
418 log_open(basename(argv[0]), false);
419 dbus_threads_init_default();
420
421 DBusError err = DBUS_ERROR_INIT;
422 if (!ba_dbus_connection_ctx_init(&config.dbus, dbus_ba_service, &err)) {
423 cli_print_error("Couldn't initialize D-Bus context: %s", err.message);
424 return EXIT_FAILURE;
425 }
426
427 argc -= optind;
428 argv += optind;
429 optind = 0;
430
431 if (argc == 0) {
432 /* show "status" information by default */
433 char *status_argv[] = { "status", NULL };
434 return cmd_status.func(1, status_argv);
435 }
436
437 for (size_t i = 0; i < ARRAYSIZE(commands); i++)
438 if (strcmp(argv[0], commands[i]->name) == 0)
439 return commands[i]->func(argc, argv);
440
441 cli_print_error("Invalid command: %s", argv[0]);
442 return EXIT_FAILURE;
443 }