Debian Codegraph @

utils/cli/cmd-open.c

raw
1 /*
2 * BlueALSA - cmd-open.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 <errno.h>
12 #include <getopt.h>
13 #include <stdbool.h>
14 #include <stdint.h>
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <string.h>
18 #include <unistd.h>
19
20 #include <dbus/dbus.h>
21
22 #include "cli.h"
23 #include "shared/dbus-client-pcm.h"
24 #include "shared/hex.h"
25
26 static void usage(const char *command) {
27 printf("Transfer raw PCM data via stdin or stdout.\n\n");
28 cli_print_usage("%s [OPTION]... PCM-PATH", command);
29 printf("\nOptions:\n"
30 " -h, --help\t\tShow this message and exit\n"
31 " -x, --hex\t\tTransfer data in hexadecimal format\n"
32 "\nPositional arguments:\n"
33 " PCM-PATH\tBlueALSA PCM D-Bus object path\n"
34 );
35 }
36
37 static int cmd_open_func(int argc, char *argv[]) {
38
39 int opt;
40 const char *opts = "hqvx";
41 const struct option longopts[] = {
42 { "help", no_argument, NULL, 'h' },
43 { "quiet", no_argument, NULL, 'q' },
44 { "verbose", no_argument, NULL, 'v' },
45 { "hex", no_argument, NULL, 'x' },
46 { 0 },
47 };
48
49 bool hex = false;
50
51 opterr = 0;
52 while ((opt = getopt_long(argc, argv, opts, longopts, NULL)) != -1) {
53 if (cli_parse_common_options(opt))
54 continue;
55 switch (opt) {
56 case 'h' /* --help */ :
57 usage(argv[0]);
58 return EXIT_SUCCESS;
59 case 'x' /* --hex */ :
60 hex = true;
61 break;
62 default:
63 cmd_print_error("Invalid argument '%s'", argv[optind - 1]);
64 return EXIT_FAILURE;
65 }
66 }
67
68 if (argc - optind < 1) {
69 cmd_print_error("Missing BlueALSA PCM path argument");
70 return EXIT_FAILURE;
71 }
72 if (argc - optind > 2) {
73 cmd_print_error("Invalid number of arguments");
74 return EXIT_FAILURE;
75 }
76
77 const char *path = argv[optind];
78 if (!dbus_validate_path(path, NULL)) {
79 cmd_print_error("Invalid PCM path: %s", path);
80 return EXIT_FAILURE;
81 }
82
83 int fd_pcm, fd_pcm_ctrl;
84 int fd_input, fd_output;
85 size_t len = strlen(path);
86
87 DBusError err = DBUS_ERROR_INIT;
88 if (!ba_dbus_pcm_open(&config.dbus, path, &fd_pcm, &fd_pcm_ctrl, &err)) {
89 cmd_print_error("Couldn't open PCM: %s", err.message);
90 return EXIT_FAILURE;
91 }
92
93 if (strcmp(path + len - strlen("source"), "source") == 0) {
94 fd_input = fd_pcm;
95 fd_output = STDOUT_FILENO;
96 }
97 else {
98 fd_input = STDIN_FILENO;
99 fd_output = fd_pcm;
100 }
101
102 uint8_t buffer[4096];
103 uint8_t buffer_hex[sizeof(buffer) * 2 + 1];
104 ssize_t count;
105
106 while ((count = read(fd_input, buffer, sizeof(buffer))) > 0) {
107
108 const uint8_t *pos = buffer;
109 ssize_t written = 0;
110
111 if (hex) {
112
113 if (fd_input == STDIN_FILENO) {
114 if ((count = hex2bin((const char *)buffer, buffer, count)) == -1) {
115 cmd_print_error("Couldn't decode hex string: %s", strerror(errno));
116 continue;
117 }
118 }
119
120 if (fd_output == STDOUT_FILENO) {
121 count = bin2hex(buffer, (char *)buffer_hex, count);
122 pos = buffer_hex;
123 }
124
125 }
126
127 while (written < count) {
128 ssize_t res = write(fd_output, pos, count - written);
129 if (res <= 0) {
130 /* Cannot write any more, so just terminate */
131 goto finish;
132 }
133 written += res;
134 pos += res;
135 }
136
137 }
138
139 if (fd_output == fd_pcm)
140 ba_dbus_pcm_ctrl_send_drain(fd_pcm_ctrl, &err);
141
142 finish:
143 close(fd_pcm);
144 close(fd_pcm_ctrl);
145 return EXIT_SUCCESS;
146 }
147
148 const struct cli_command cmd_open = {
149 "open",
150 "Transfer raw PCM via stdin or stdout",
151 cmd_open_func,
152 };