Debian Codegraph @

utils/cli/cmd-status.c

raw
1 /*
2 * BlueALSA - cmd-status.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 <getopt.h>
12 #include <stdio.h>
13 #include <stdlib.h>
14
15 #include <dbus/dbus.h>
16
17 #include "cli.h"
18 #include "shared/dbus-client.h"
19
20 static void usage(const char *command) {
21 printf("Show BlueALSA service runtime status.\n\n");
22 cli_print_usage("%s [OPTION]...", command);
23 printf("\nOptions:\n"
24 " -h, --help\t\tShow this message and exit\n"
25 );
26 }
27
28 static int cmd_status_func(int argc, char *argv[]) {
29
30 int opt;
31 const char *opts = "hqv";
32 const struct option longopts[] = {
33 { "help", no_argument, NULL, 'h' },
34 { "quiet", no_argument, NULL, 'q' },
35 { "verbose", no_argument, NULL, 'v' },
36 { 0 },
37 };
38
39 opterr = 0;
40 while ((opt = getopt_long(argc, argv, opts, longopts, NULL)) != -1) {
41 if (cli_parse_common_options(opt))
42 continue;
43 switch (opt) {
44 case 'h' /* --help */ :
45 usage(argv[0]);
46 return EXIT_SUCCESS;
47 default:
48 cmd_print_error("Invalid argument '%s'", argv[optind - 1]);
49 return EXIT_FAILURE;
50 }
51 }
52
53 if (argc != optind) {
54 cmd_print_error("Invalid number of arguments");
55 return EXIT_FAILURE;
56 }
57
58 struct ba_service_props props = { 0 };
59
60 DBusError err = DBUS_ERROR_INIT;
61 if (!ba_dbus_service_props_get(&config.dbus, &props, &err)) {
62 cmd_print_error("D-Bus error: %s", err.message);
63 ba_dbus_service_props_free(&props);
64 return EXIT_FAILURE;
65 }
66
67 printf("Service: %s\n", config.dbus.ba_service);
68 printf("Version: %s\n", props.version);
69 cli_print_adapters(&props);
70 cli_print_profiles_and_codecs(&props);
71
72 ba_dbus_service_props_free(&props);
73 return EXIT_SUCCESS;
74 }
75
76 const struct cli_command cmd_status = {
77 "status",
78 "Show BlueALSA service status",
79 cmd_status_func,
80 };