Debian Codegraph @

utils/rfcomm/rfcomm.c

raw
1 /*
2 * BlueALSA - rfcomm.c
3 * Copyright (c) 2016-2022 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 <ctype.h>
16 #include <errno.h>
17 #include <getopt.h>
18 #include <libgen.h>
19 #include <poll.h>
20 #include <stdbool.h>
21 #include <stdint.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <unistd.h>
26
27 #include <bluetooth/bluetooth.h>
28 #include <dbus/dbus.h>
29 #include <readline/readline.h>
30 #include <readline/history.h>
31
32 #include "shared/dbus-client.h"
33 #include "shared/dbus-client-rfcomm.h"
34 #include "shared/log.h"
35
36 static int rfcomm_fd = -1;
37 static bool main_loop_on = true;
38 static bool input_is_tty = true;
39 static bool output_is_tty = true;
40 static bool properties = false;
41
42 static int path2hci(const char *path) {
43 int id;
44 if ((path = strstr(path, "/hci")) == NULL ||
45 sscanf(path, "/hci%d/", &id) != 1)
46 return -1;
47 return id;
48 }
49
50 static int path2ba(const char *path, bdaddr_t *ba) {
51
52 unsigned int x[6];
53 if ((path = strstr(path, "/dev_")) == NULL ||
54 sscanf(&path[5], "%x_%x_%x_%x_%x_%x",
55 &x[5], &x[4], &x[3], &x[2], &x[1], &x[0]) != 6)
56 return -1;
57
58 for (size_t i = 0; i < 6; i++)
59 ba->b[i] = x[i];
60
61 return 0;
62 }
63
64 static char *strtrim(char *str) {
65 while (isspace(*str))
66 str++;
67 if (*str == '\0')
68 return str;
69 char *end = &str[strlen(str) - 1];
70 while (end > str && isspace(*end))
71 end--;
72 end[1] = '\0';
73 return str;
74 }
75
76 static bool print_properties(struct ba_dbus_ctx *dbus_ctx, const char* path, DBusError *err) {
77
78 struct ba_rfcomm_props props = { 0 };
79 if (!ba_dbus_rfcomm_props_get(dbus_ctx, path, &props, err)) {
80 ba_dbus_rfcomm_props_free(&props);
81 return false;
82 }
83
84 printf("Transport: %s\n", props.transport);
85 printf("Features:");
86 for (size_t i = 0; i < props.features_len; i++)
87 printf(" %s", props.features[i]);
88 printf("\n");
89 printf("Battery: %d\n", props.battery);
90
91 ba_dbus_rfcomm_props_free(&props);
92
93 return true;
94 }
95
96 static char *build_rfcomm_command(char *buffer, size_t size, const char *cmd) {
97 bool at = strncmp(cmd, "AT", 2) == 0;
98 snprintf(buffer, size, "%s%s%s", at ? "" : "\r\n", cmd, at ? "\r" : "\r\n");
99 return buffer;
100 }
101
102 static void rl_callback_handler(char *line) {
103
104 if (line == NULL) {
105 rl_callback_handler_remove();
106 main_loop_on = false;
107 return;
108 }
109
110 line = strtrim(line);
111 if (strlen(line) == 0)
112 return;
113
114 char cmd[512];
115 build_rfcomm_command(cmd, sizeof(cmd), line);
116
117 if (write(rfcomm_fd, cmd, strlen(cmd)) == -1)
118 warn("Couldn't send RFCOMM command: %s", strerror(errno));
119
120 if (input_is_tty)
121 add_history(line);
122 }
123
124 int main(int argc, char *argv[]) {
125
126 int opt;
127 const char *opts = "hVB:p";
128 const struct option longopts[] = {
129 { "help", no_argument, NULL, 'h' },
130 { "version", no_argument, NULL, 'V' },
131 { "dbus", required_argument, NULL, 'B' },
132 { "properties", no_argument, NULL, 'p' },
133 { 0, 0, 0, 0 },
134 };
135
136 char dbus_ba_service[32] = BLUEALSA_SERVICE;
137
138 log_open(basename(argv[0]), false);
139
140 while ((opt = getopt_long(argc, argv, opts, longopts, NULL)) != -1)
141 switch (opt) {
142 case 'h' /* --help */ :
143 usage:
144 printf("Usage:\n"
145 " %s [OPTION]... <DEVICE-PATH>\n"
146 "\nOptions:\n"
147 " -h, --help\t\tprint this help and exit\n"
148 " -V, --version\t\tprint version and exit\n"
149 " -B, --dbus=NAME\tBlueALSA service name suffix\n"
150 " -p, --properties\tprint device properties and exit\n",
151 argv[0]);
152 return EXIT_SUCCESS;
153
154 case 'V' /* --version */ :
155 printf("%s\n", PACKAGE_VERSION);
156 return EXIT_SUCCESS;
157
158 case 'B' /* --dbus=NAME */ :
159 snprintf(dbus_ba_service, sizeof(dbus_ba_service), BLUEALSA_SERVICE ".%s", optarg);
160 if (!dbus_validate_bus_name(dbus_ba_service, NULL)) {
161 error("Invalid BlueALSA D-Bus service name: %s", dbus_ba_service);
162 return EXIT_FAILURE;
163 }
164 break;
165
166 case 'p' /* --properties */ :
167 properties = true;
168 break;
169
170 default:
171 fprintf(stderr, "Try '%s --help' for more information.\n", argv[0]);
172 return EXIT_FAILURE;
173 }
174
175 if (optind + 1 != argc)
176 goto usage;
177
178 int hci_dev_id;
179 bdaddr_t addr;
180
181 if ((hci_dev_id = path2hci(argv[optind])) == -1 ||
182 path2ba(argv[optind], &addr) == -1) {
183 error("Invalid BT device path: %s", argv[optind]);
184 return EXIT_FAILURE;
185 }
186
187 DBusError err = DBUS_ERROR_INIT;
188 struct ba_dbus_ctx dbus_ctx;
189
190 if (!ba_dbus_connection_ctx_init(&dbus_ctx, dbus_ba_service, &err)) {
191 error("Couldn't initialize D-Bus context: %s", err.message);
192 return EXIT_FAILURE;
193 }
194
195 char rfcomm_path[128];
196 sprintf(rfcomm_path, "/org/bluealsa/hci%d/dev_%.2X_%.2X_%.2X_%.2X_%.2X_%.2X/rfcomm",
197 hci_dev_id, addr.b[5], addr.b[4], addr.b[3], addr.b[2], addr.b[1], addr.b[0]);
198
199 if (properties) {
200 if (print_properties(&dbus_ctx, rfcomm_path, &err))
201 return EXIT_SUCCESS;
202 error("D-Bus error: %s", err.message);
203 return EXIT_FAILURE;
204 }
205
206 if (!ba_dbus_rfcomm_open(&dbus_ctx, rfcomm_path, &rfcomm_fd, &err)) {
207 error("Couldn't open RFCOMM: %s", err.message);
208 return EXIT_FAILURE;
209 }
210
211 input_is_tty = isatty(fileno(stdin));
212 output_is_tty = isatty(fileno(stdout));
213 const char *prefix = "";
214 char prompt[32] = "";
215
216 if (input_is_tty) {
217 sprintf(prompt, "%.2X:%.2X:%.2X:%.2X:%.2X:%.2X> ",
218 addr.b[5], addr.b[4], addr.b[3], addr.b[2], addr.b[1], addr.b[0]);
219 rl_bind_key('\t', rl_insert);
220 prefix = "> ";
221 }
222 else
223 /* libreadline echos its input to stdout which is a problem when
224 * stdin is not the terminal. The function rl_tty_set_echoing() is
225 * broken when used with the callback API, so as a workaround we
226 * redirect the (useless) output to /dev/null */
227 rl_outstream = fopen("/dev/null", "w");
228
229 if (!output_is_tty)
230 /* Force line buffered output to be sure each event will be flushed
231 * immediately. */
232 setvbuf(stdout, NULL, _IOLBF, 0);
233
234 rl_callback_handler_install(prompt, rl_callback_handler);
235
236 struct pollfd pfds[] = {
237 { fileno(stdin), POLLIN, 0 },
238 { rfcomm_fd, POLLIN, 0 },
239 };
240
241 while (main_loop_on) {
242
243 poll(pfds, 2, -1);
244
245 if (pfds[0].revents & POLLIN)
246 rl_callback_read_char();
247 else if (pfds[0].revents & POLLHUP)
248 break;
249
250 if (pfds[1].revents & POLLIN) {
251
252 char buffer[256];
253 ssize_t ret;
254
255 if ((ret = read(rfcomm_fd, buffer, sizeof(buffer) - 1)) <= 0) {
256 if (output_is_tty) {
257 rl_replace_line("disconnected", 0);
258 rl_message("");
259 rl_redisplay();
260 }
261 break;
262 }
263
264 buffer[ret] = '\0';
265
266 char *old_text = NULL;
267 int old_point = 0;
268
269 if (output_is_tty) {
270 old_point = rl_point;
271 old_text = rl_copy_text(0, rl_end);
272 rl_save_prompt();
273 rl_replace_line("", 0);
274 rl_redisplay();
275 }
276
277 fprintf(stdout, "%s%s\n", prefix, strtrim(buffer));
278
279 if (output_is_tty) {
280 rl_restore_prompt();
281 rl_replace_line(old_text, 0);
282 rl_point = old_point;
283 rl_redisplay();
284 free(old_text);
285 }
286
287 }
288
289 }
290
291 rl_callback_handler_remove();
292 fprintf(stdout, "\n");
293
294 return EXIT_SUCCESS;
295 }