| 1 |
/* |
| 2 |
* BlueALSA - cmd-monitor.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 |
#include <strings.h> |
| 17 |
|
| 18 |
#include <dbus/dbus.h> |
| 19 |
|
| 20 |
#include "cli.h" |
| 21 |
#include "shared/dbus-client.h" |
| 22 |
#include "shared/dbus-client-pcm.h" |
| 23 |
#include "shared/defs.h" |
| 24 |
#include "shared/log.h" |
| 25 |
|
| 26 |
enum { |
| 27 |
PROPERTY_CODEC, |
| 28 |
PROPERTY_RUNNING, |
| 29 |
PROPERTY_SOFTVOL, |
| 30 |
PROPERTY_VOLUME, |
| 31 |
}; |
| 32 |
|
| 33 |
struct property { |
| 34 |
const char *name; |
| 35 |
bool enabled; |
| 36 |
}; |
| 37 |
|
| 38 |
static bool monitor_properties = false; |
| 39 |
static struct property monitor_properties_set[] = { |
| 40 |
[PROPERTY_CODEC] = { "Codec", false }, |
| 41 |
[PROPERTY_RUNNING] = { "Running", false }, |
| 42 |
[PROPERTY_SOFTVOL] = { "SoftVolume", false }, |
| 43 |
[PROPERTY_VOLUME] = { "Volume", false }, |
| 44 |
}; |
| 45 |
|
| 46 |
static bool test_bluealsa_service(const char *name, void *data) { |
| 47 |
bool *result = data; |
| 48 |
if (strcmp(name, BLUEALSA_SERVICE) == 0) { |
| 49 |
*result = true; |
| 50 |
return false; |
| 51 |
} |
| 52 |
*result = false; |
| 53 |
return true; |
| 54 |
} |
| 55 |
|
| 56 |
static dbus_bool_t monitor_dbus_message_iter_get_pcm_props_cb(const char *key, |
| 57 |
DBusMessageIter *value, void *userdata, DBusError *error) { |
| 58 |
const char *path = userdata; |
| 59 |
|
| 60 |
char type; |
| 61 |
if ((type = dbus_message_iter_get_arg_type(value)) != DBUS_TYPE_VARIANT) { |
| 62 |
dbus_set_error(error, DBUS_ERROR_INVALID_SIGNATURE, |
| 63 |
"Incorrect property value type: %c != %c", type, DBUS_TYPE_VARIANT); |
| 64 |
return FALSE; |
| 65 |
} |
| 66 |
|
| 67 |
DBusMessageIter variant; |
| 68 |
dbus_message_iter_recurse(value, &variant); |
| 69 |
type = dbus_message_iter_get_arg_type(&variant); |
| 70 |
char type_expected; |
| 71 |
|
| 72 |
if (monitor_properties_set[PROPERTY_CODEC].enabled && |
| 73 |
strcmp(key, monitor_properties_set[PROPERTY_CODEC].name) == 0) { |
| 74 |
if (type != (type_expected = DBUS_TYPE_STRING)) |
| 75 |
goto fail; |
| 76 |
const char *codec; |
| 77 |
dbus_message_iter_get_basic(&variant, &codec); |
| 78 |
printf("PropertyChanged %s Codec %s\n", path, codec); |
| 79 |
} |
| 80 |
else if (monitor_properties_set[PROPERTY_RUNNING].enabled && |
| 81 |
strcmp(key, monitor_properties_set[PROPERTY_RUNNING].name) == 0) { |
| 82 |
if (type != (type_expected = DBUS_TYPE_BOOLEAN)) |
| 83 |
goto fail; |
| 84 |
dbus_bool_t running; |
| 85 |
dbus_message_iter_get_basic(&variant, &running); |
| 86 |
printf("PropertyChanged %s Running %s\n", path, running ? "true" : "false"); |
| 87 |
} |
| 88 |
else if (monitor_properties_set[PROPERTY_SOFTVOL].enabled && |
| 89 |
strcmp(key, monitor_properties_set[PROPERTY_SOFTVOL].name) == 0) { |
| 90 |
if (type != (type_expected = DBUS_TYPE_BOOLEAN)) |
| 91 |
goto fail; |
| 92 |
dbus_bool_t softvol; |
| 93 |
dbus_message_iter_get_basic(&variant, &softvol); |
| 94 |
printf("PropertyChanged %s SoftVolume %s\n", path, softvol ? "true" : "false"); |
| 95 |
} |
| 96 |
else if (monitor_properties_set[PROPERTY_VOLUME].enabled && |
| 97 |
strcmp(key, monitor_properties_set[PROPERTY_VOLUME].name) == 0) { |
| 98 |
if (type != (type_expected = DBUS_TYPE_UINT16)) |
| 99 |
goto fail; |
| 100 |
dbus_uint16_t volume; |
| 101 |
dbus_message_iter_get_basic(&variant, &volume); |
| 102 |
printf("PropertyChanged %s Volume 0x%.4X\n", path, volume); |
| 103 |
} |
| 104 |
|
| 105 |
return TRUE; |
| 106 |
|
| 107 |
fail: |
| 108 |
dbus_set_error(error, DBUS_ERROR_INVALID_SIGNATURE, |
| 109 |
"Incorrect variant for '%s': %c != %c", key, type, type_expected); |
| 110 |
return FALSE; |
| 111 |
} |
| 112 |
|
| 113 |
static DBusHandlerResult dbus_signal_handler(DBusConnection *conn, DBusMessage *message, void *data) { |
| 114 |
(void)conn; |
| 115 |
(void)data; |
| 116 |
|
| 117 |
if (dbus_message_get_type(message) != DBUS_MESSAGE_TYPE_SIGNAL) |
| 118 |
return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; |
| 119 |
|
| 120 |
const char *interface = dbus_message_get_interface(message); |
| 121 |
const char *signal = dbus_message_get_member(message); |
| 122 |
|
| 123 |
DBusMessageIter iter; |
| 124 |
if (!dbus_message_iter_init(message, &iter)) |
| 125 |
goto fail; |
| 126 |
|
| 127 |
if (strcmp(interface, DBUS_INTERFACE_OBJECT_MANAGER) == 0) { |
| 128 |
|
| 129 |
const char *path; |
| 130 |
if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_OBJECT_PATH) |
| 131 |
goto fail; |
| 132 |
dbus_message_iter_get_basic(&iter, &path); |
| 133 |
|
| 134 |
if (!dbus_message_iter_next(&iter)) |
| 135 |
goto fail; |
| 136 |
|
| 137 |
if (strcmp(signal, "InterfacesAdded") == 0) { |
| 138 |
|
| 139 |
DBusMessageIter iter_ifaces; |
| 140 |
for (dbus_message_iter_recurse(&iter, &iter_ifaces); |
| 141 |
dbus_message_iter_get_arg_type(&iter_ifaces) != DBUS_TYPE_INVALID; |
| 142 |
dbus_message_iter_next(&iter_ifaces)) { |
| 143 |
|
| 144 |
DBusMessageIter iter_iface_entry; |
| 145 |
if (dbus_message_iter_get_arg_type(&iter_ifaces) != DBUS_TYPE_DICT_ENTRY) |
| 146 |
goto fail; |
| 147 |
dbus_message_iter_recurse(&iter_ifaces, &iter_iface_entry); |
| 148 |
|
| 149 |
const char *iface; |
| 150 |
if (dbus_message_iter_get_arg_type(&iter_iface_entry) != DBUS_TYPE_STRING) |
| 151 |
goto fail; |
| 152 |
dbus_message_iter_get_basic(&iter_iface_entry, &iface); |
| 153 |
|
| 154 |
if (strcmp(iface, BLUEALSA_INTERFACE_PCM) == 0) { |
| 155 |
|
| 156 |
printf("PCMAdded %s\n", path); |
| 157 |
|
| 158 |
if (config.verbose) { |
| 159 |
|
| 160 |
DBusMessageIter iter2; |
| 161 |
if (!dbus_message_iter_init(message, &iter2)) |
| 162 |
goto fail; |
| 163 |
|
| 164 |
struct ba_pcm pcm; |
| 165 |
DBusError err = DBUS_ERROR_INIT; |
| 166 |
if (!dbus_message_iter_get_ba_pcm(&iter2, &err, &pcm)) { |
| 167 |
error("Couldn't read PCM properties: %s", err.message); |
| 168 |
dbus_error_free(&err); |
| 169 |
goto fail; |
| 170 |
} |
| 171 |
|
| 172 |
cli_print_pcm_properties(&pcm, &err); |
| 173 |
printf("\n"); |
| 174 |
|
| 175 |
} |
| 176 |
|
| 177 |
} |
| 178 |
else if (strcmp(iface, BLUEALSA_INTERFACE_RFCOMM) == 0) { |
| 179 |
printf("RFCOMMAdded %s\n", path); |
| 180 |
} |
| 181 |
|
| 182 |
} |
| 183 |
|
| 184 |
return DBUS_HANDLER_RESULT_HANDLED; |
| 185 |
} |
| 186 |
else if (strcmp(signal, "InterfacesRemoved") == 0) { |
| 187 |
|
| 188 |
DBusMessageIter iter_ifaces; |
| 189 |
for (dbus_message_iter_recurse(&iter, &iter_ifaces); |
| 190 |
dbus_message_iter_get_arg_type(&iter_ifaces) != DBUS_TYPE_INVALID; |
| 191 |
dbus_message_iter_next(&iter_ifaces)) { |
| 192 |
|
| 193 |
const char *iface; |
| 194 |
if (dbus_message_iter_get_arg_type(&iter_ifaces) != DBUS_TYPE_STRING) |
| 195 |
goto fail; |
| 196 |
dbus_message_iter_get_basic(&iter_ifaces, &iface); |
| 197 |
|
| 198 |
if (strcmp(iface, BLUEALSA_INTERFACE_PCM) == 0) |
| 199 |
printf("PCMRemoved %s\n", path); |
| 200 |
else if (strcmp(iface, BLUEALSA_INTERFACE_RFCOMM) == 0) |
| 201 |
printf("RFCOMMRemoved %s\n", path); |
| 202 |
|
| 203 |
} |
| 204 |
|
| 205 |
return DBUS_HANDLER_RESULT_HANDLED; |
| 206 |
} |
| 207 |
|
| 208 |
} |
| 209 |
else if (strcmp(interface, DBUS_INTERFACE_DBUS) == 0) { |
| 210 |
if (strcmp(signal, "NameOwnerChanged") == 0) { |
| 211 |
|
| 212 |
const char *arg0 = NULL, *arg1 = NULL, *arg2 = NULL; |
| 213 |
if (dbus_message_iter_init(message, &iter) && |
| 214 |
dbus_message_iter_get_arg_type(&iter) == DBUS_TYPE_STRING) |
| 215 |
dbus_message_iter_get_basic(&iter, &arg0); |
| 216 |
else |
| 217 |
goto fail; |
| 218 |
if (dbus_message_iter_next(&iter) && |
| 219 |
dbus_message_iter_get_arg_type(&iter) == DBUS_TYPE_STRING) |
| 220 |
dbus_message_iter_get_basic(&iter, &arg1); |
| 221 |
else |
| 222 |
goto fail; |
| 223 |
if (dbus_message_iter_next(&iter) && |
| 224 |
dbus_message_iter_get_arg_type(&iter) == DBUS_TYPE_STRING) |
| 225 |
dbus_message_iter_get_basic(&iter, &arg2); |
| 226 |
else |
| 227 |
goto fail; |
| 228 |
|
| 229 |
if (strcmp(arg0, config.dbus.ba_service)) |
| 230 |
goto fail; |
| 231 |
|
| 232 |
if (strlen(arg1) == 0) |
| 233 |
printf("ServiceRunning %s\n", config.dbus.ba_service); |
| 234 |
else if (strlen(arg2) == 0) |
| 235 |
printf("ServiceStopped %s\n", config.dbus.ba_service); |
| 236 |
else |
| 237 |
goto fail; |
| 238 |
|
| 239 |
return DBUS_HANDLER_RESULT_HANDLED; |
| 240 |
} |
| 241 |
} |
| 242 |
else if (strcmp(interface, DBUS_INTERFACE_PROPERTIES) == 0 && |
| 243 |
strcmp(signal, "PropertiesChanged") == 0) { |
| 244 |
|
| 245 |
const char *updated_interface; |
| 246 |
dbus_message_iter_get_basic(&iter, &updated_interface); |
| 247 |
dbus_message_iter_next(&iter); |
| 248 |
|
| 249 |
if (strcmp(updated_interface, BLUEALSA_INTERFACE_PCM) == 0) { |
| 250 |
|
| 251 |
DBusError err = DBUS_ERROR_INIT; |
| 252 |
const char *path = dbus_message_get_path(message); |
| 253 |
if (!dbus_message_iter_dict(&iter, &err, |
| 254 |
monitor_dbus_message_iter_get_pcm_props_cb, (void *)path)) { |
| 255 |
error("Unexpected D-Bus signal: %s", err.message); |
| 256 |
dbus_error_free(&err); |
| 257 |
goto fail; |
| 258 |
} |
| 259 |
|
| 260 |
return DBUS_HANDLER_RESULT_HANDLED; |
| 261 |
} |
| 262 |
} |
| 263 |
|
| 264 |
fail: |
| 265 |
return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; |
| 266 |
} |
| 267 |
|
| 268 |
static bool parse_property_list(char *argv[], char *props) { |
| 269 |
|
| 270 |
if (props == NULL) { |
| 271 |
/* monitor all properties */ |
| 272 |
for (size_t i = 0; i < ARRAYSIZE(monitor_properties_set); i++) |
| 273 |
monitor_properties_set[i].enabled = true; |
| 274 |
return true; |
| 275 |
} |
| 276 |
|
| 277 |
char *prop = strtok(props, ","); |
| 278 |
for (; prop; prop = strtok(NULL, ",")) { |
| 279 |
|
| 280 |
bool found = false; |
| 281 |
for (size_t i = 0; i < ARRAYSIZE(monitor_properties_set); i++) { |
| 282 |
if (strcasecmp(prop, monitor_properties_set[i].name) == 0) { |
| 283 |
monitor_properties_set[i].enabled = true; |
| 284 |
found = true; |
| 285 |
break; |
| 286 |
} |
| 287 |
} |
| 288 |
|
| 289 |
if (!found) { |
| 290 |
cmd_print_error("Unknown property '%s'", prop); |
| 291 |
return false; |
| 292 |
} |
| 293 |
|
| 294 |
} |
| 295 |
|
| 296 |
return true; |
| 297 |
} |
| 298 |
|
| 299 |
static void usage(const char *command) { |
| 300 |
printf("Display D-Bus signals.\n\n"); |
| 301 |
cli_print_usage("%s [OPTION]...", command); |
| 302 |
printf("\nOptions:\n" |
| 303 |
" -h, --help\t\t\tShow this message and exit\n" |
| 304 |
" -p, --properties[=PROPS]\tShow PCM property changes\n" |
| 305 |
); |
| 306 |
} |
| 307 |
|
| 308 |
static int cmd_monitor_func(int argc, char *argv[]) { |
| 309 |
|
| 310 |
int opt; |
| 311 |
const char *opts = "hqvp::"; |
| 312 |
const struct option longopts[] = { |
| 313 |
{ "help", no_argument, NULL, 'h' }, |
| 314 |
{ "quiet", no_argument, NULL, 'q' }, |
| 315 |
{ "verbose", no_argument, NULL, 'v' }, |
| 316 |
{ "properties", optional_argument, NULL, 'p' }, |
| 317 |
{ 0 }, |
| 318 |
}; |
| 319 |
|
| 320 |
opterr = 0; |
| 321 |
while ((opt = getopt_long(argc, argv, opts, longopts, NULL)) != -1) { |
| 322 |
if (cli_parse_common_options(opt)) |
| 323 |
continue; |
| 324 |
switch (opt) { |
| 325 |
case 'h' /* --help */ : |
| 326 |
usage(argv[0]); |
| 327 |
return EXIT_SUCCESS; |
| 328 |
case 'p' /* --properties[=PROPS] */ : |
| 329 |
monitor_properties = true; |
| 330 |
if (!parse_property_list(argv, optarg)) |
| 331 |
return EXIT_FAILURE; |
| 332 |
break; |
| 333 |
default: |
| 334 |
cmd_print_error("Invalid argument '%s'", argv[optind - 1]); |
| 335 |
return EXIT_FAILURE; |
| 336 |
} |
| 337 |
} |
| 338 |
|
| 339 |
if (argc != optind) { |
| 340 |
cmd_print_error("Invalid number of arguments"); |
| 341 |
return EXIT_FAILURE; |
| 342 |
} |
| 343 |
|
| 344 |
/* Force line buffered output to be sure each event will be flushed |
| 345 |
* immediately, as this command will most likely be used to write to |
| 346 |
* a pipe. */ |
| 347 |
setvbuf(stdout, NULL, _IOLBF, 0); |
| 348 |
|
| 349 |
ba_dbus_connection_signal_match_add(&config.dbus, |
| 350 |
config.dbus.ba_service, NULL, DBUS_INTERFACE_OBJECT_MANAGER, "InterfacesAdded", |
| 351 |
"path_namespace='/org/bluealsa'"); |
| 352 |
ba_dbus_connection_signal_match_add(&config.dbus, |
| 353 |
config.dbus.ba_service, NULL, DBUS_INTERFACE_OBJECT_MANAGER, "InterfacesRemoved", |
| 354 |
"path_namespace='/org/bluealsa'"); |
| 355 |
|
| 356 |
char dbus_args[50]; |
| 357 |
snprintf(dbus_args, sizeof(dbus_args), "arg0='%s',arg2=''", config.dbus.ba_service); |
| 358 |
ba_dbus_connection_signal_match_add(&config.dbus, |
| 359 |
DBUS_SERVICE_DBUS, NULL, DBUS_INTERFACE_DBUS, "NameOwnerChanged", dbus_args); |
| 360 |
snprintf(dbus_args, sizeof(dbus_args), "arg0='%s',arg1=''", config.dbus.ba_service); |
| 361 |
ba_dbus_connection_signal_match_add(&config.dbus, |
| 362 |
DBUS_SERVICE_DBUS, NULL, DBUS_INTERFACE_DBUS, "NameOwnerChanged", dbus_args); |
| 363 |
|
| 364 |
if (monitor_properties) |
| 365 |
ba_dbus_connection_signal_match_add(&config.dbus, config.dbus.ba_service, NULL, |
| 366 |
DBUS_INTERFACE_PROPERTIES, "PropertiesChanged", "arg0='"BLUEALSA_INTERFACE_PCM"'"); |
| 367 |
|
| 368 |
if (!dbus_connection_add_filter(config.dbus.conn, dbus_signal_handler, NULL, NULL)) { |
| 369 |
cmd_print_error("Couldn't add D-Bus filter"); |
| 370 |
return EXIT_FAILURE; |
| 371 |
} |
| 372 |
|
| 373 |
bool running = false; |
| 374 |
DBusError err = DBUS_ERROR_INIT; |
| 375 |
cli_get_ba_services(test_bluealsa_service, &running, &err); |
| 376 |
if (dbus_error_is_set(&err)) { |
| 377 |
cmd_print_error("D-Bus error: %s", err.message); |
| 378 |
return EXIT_FAILURE; |
| 379 |
} |
| 380 |
|
| 381 |
if (running) |
| 382 |
printf("ServiceRunning %s\n", config.dbus.ba_service); |
| 383 |
else |
| 384 |
printf("ServiceStopped %s\n", config.dbus.ba_service); |
| 385 |
|
| 386 |
while (dbus_connection_read_write_dispatch(config.dbus.conn, -1)) |
| 387 |
continue; |
| 388 |
|
| 389 |
return EXIT_SUCCESS; |
| 390 |
} |
| 391 |
|
| 392 |
const struct cli_command cmd_monitor = { |
| 393 |
"monitor", |
| 394 |
"Display D-Bus signals", |
| 395 |
cmd_monitor_func, |
| 396 |
}; |