| 1 |
/* |
| 2 |
* BlueALSA - dbus.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 "dbus.h" |
| 12 |
|
| 13 |
#include <stdbool.h> |
| 14 |
#include <string.h> |
| 15 |
|
| 16 |
static bool dbus_message_iter_get_basic_boolean(DBusMessageIter *iter) { |
| 17 |
dbus_bool_t tmp; |
| 18 |
return dbus_message_iter_get_basic(iter, &tmp), tmp; |
| 19 |
} |
| 20 |
|
| 21 |
static const char *dbus_message_iter_get_basic_string(DBusMessageIter *iter) { |
| 22 |
static const char *tmp; |
| 23 |
return dbus_message_iter_get_basic(iter, &tmp), tmp; |
| 24 |
} |
| 25 |
|
| 26 |
DBusMessage *dbus_get_properties(DBusConnection *conn, |
| 27 |
const char *service, const char *path, const char *interface, |
| 28 |
const char *property, DBusError *error) { |
| 29 |
|
| 30 |
DBusMessage *msg; |
| 31 |
const char *method = property == NULL ? "GetAll" : "Get"; |
| 32 |
if ((msg = dbus_message_new_method_call(service, path, |
| 33 |
DBUS_INTERFACE_PROPERTIES, method)) == NULL) |
| 34 |
return NULL; |
| 35 |
|
| 36 |
DBusMessage *rep = NULL; |
| 37 |
|
| 38 |
DBusMessageIter iter; |
| 39 |
dbus_message_iter_init_append(msg, &iter); |
| 40 |
|
| 41 |
if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &interface)) |
| 42 |
goto fail; |
| 43 |
if (property != NULL && |
| 44 |
!dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &property)) |
| 45 |
goto fail; |
| 46 |
|
| 47 |
rep = dbus_connection_send_with_reply_and_block(conn, msg, |
| 48 |
DBUS_TIMEOUT_USE_DEFAULT, error); |
| 49 |
|
| 50 |
fail: |
| 51 |
dbus_message_unref(msg); |
| 52 |
return rep; |
| 53 |
} |
| 54 |
|
| 55 |
int dbus_bluez_get_device(DBusConnection *conn, const char *path, |
| 56 |
struct bluez_device *dev, DBusError *error) { |
| 57 |
|
| 58 |
char path_addr[sizeof("00:00:00:00:00:00")] = { 0 }; |
| 59 |
const char *tmp; |
| 60 |
|
| 61 |
memset(dev, 0, sizeof(*dev)); |
| 62 |
strncpy(dev->path, path, sizeof(dev->path) - 1); |
| 63 |
|
| 64 |
/* Try to extract BT MAC address from the D-Bus path. We will use it as |
| 65 |
* a fallback in case where BlueZ service is not available on the bus - |
| 66 |
* usage with bluealsa-mock server. */ |
| 67 |
if ((tmp = strstr(path, "/dev_")) != NULL) |
| 68 |
strncpy(path_addr, tmp + 5, sizeof(path_addr) - 1); |
| 69 |
for (size_t i = 0; i < sizeof(path_addr); i++) |
| 70 |
if (path_addr[i] == '_') |
| 71 |
path_addr[i] = ':'; |
| 72 |
str2ba(path_addr, &dev->bt_addr); |
| 73 |
|
| 74 |
DBusMessage *rep; |
| 75 |
if ((rep = dbus_get_properties(conn, "org.bluez", path, |
| 76 |
"org.bluez.Device1", NULL, error)) == NULL) |
| 77 |
return -1; |
| 78 |
|
| 79 |
DBusMessageIter iter; |
| 80 |
dbus_message_iter_init(rep, &iter); |
| 81 |
|
| 82 |
DBusMessageIter iter_dict; |
| 83 |
for (dbus_message_iter_recurse(&iter, &iter_dict); |
| 84 |
dbus_message_iter_get_arg_type(&iter_dict) != DBUS_TYPE_INVALID; |
| 85 |
dbus_message_iter_next(&iter_dict)) { |
| 86 |
|
| 87 |
DBusMessageIter iter_entry; |
| 88 |
DBusMessageIter iter_entry_val; |
| 89 |
const char *key; |
| 90 |
|
| 91 |
dbus_message_iter_recurse(&iter_dict, &iter_entry); |
| 92 |
dbus_message_iter_get_basic(&iter_entry, &key); |
| 93 |
dbus_message_iter_next(&iter_entry); |
| 94 |
dbus_message_iter_recurse(&iter_entry, &iter_entry_val); |
| 95 |
|
| 96 |
if (strcmp(key, "Adapter") == 0) { |
| 97 |
if ((tmp = strrchr(dbus_message_iter_get_basic_string(&iter_entry_val), '/')) != NULL) |
| 98 |
strncpy(dev->hci_name, tmp + 1, sizeof(dev->hci_name) - 1); |
| 99 |
} |
| 100 |
else if (strcmp(key, "Address") == 0) |
| 101 |
str2ba(dbus_message_iter_get_basic_string(&iter_entry_val), &dev->bt_addr); |
| 102 |
else if (strcmp(key, "Alias") == 0) |
| 103 |
strncpy(dev->name, dbus_message_iter_get_basic_string(&iter_entry_val), sizeof(dev->name) - 1); |
| 104 |
else if (strcmp(key, "Icon") == 0) |
| 105 |
strncpy(dev->icon, dbus_message_iter_get_basic_string(&iter_entry_val), sizeof(dev->icon) - 1); |
| 106 |
else if (strcmp(key, "Paired") == 0) |
| 107 |
dev->paired = dbus_message_iter_get_basic_boolean(&iter_entry_val); |
| 108 |
else if (strcmp(key, "Trusted") == 0) |
| 109 |
dev->trusted = dbus_message_iter_get_basic_boolean(&iter_entry_val); |
| 110 |
|
| 111 |
} |
| 112 |
|
| 113 |
dbus_message_unref(rep); |
| 114 |
return 0; |
| 115 |
} |