Debian Codegraph @

utils/cli/cmd-list-services.c

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