From e21ed2b6f452b0b87fb89181a9b1872a654883dd Mon Sep 17 00:00:00 2001 From: Bastien Nocera Date: Fri, 2 Sep 2022 15:51:08 +0200 Subject: lib: Don't just get BlueZ battery info from upower Handle the battery information for a device even if it is available from a source other than bluetoothd in the upower output. This means getting battery information from the more authoritative kernel-supported protocol on Logitech devices, as well as being able to have battery information for Bluetooth classic devices like Apple input devices. See https://gitlab.freedesktop.org/upower/upower/-/merge_requests/166 --- lib/bluetooth-client.c | 32 ++++++++++++++++++++++++++------ lib/bluetooth-utils.h | 2 ++ 2 files changed, 28 insertions(+), 6 deletions(-) diff --git a/lib/bluetooth-client.c b/lib/bluetooth-client.c index e005f2d0..7346a8c0 100644 --- a/lib/bluetooth-client.c +++ b/lib/bluetooth-client.c @@ -114,6 +114,26 @@ get_device_for_path (BluetoothClient *client, return NULL; } +static BluetoothDevice * +get_device_for_bdaddr (BluetoothClient *client, + const char *bdaddr) +{ + guint n_items, i; + + n_items = g_list_model_get_n_items (G_LIST_MODEL (client->list_store)); + for (i = 0; i < n_items; i++) { + g_autoptr(BluetoothDevice) d = NULL; + g_autofree char *s = NULL; + + d = g_list_model_get_item (G_LIST_MODEL (client->list_store), i); + g_object_get (G_OBJECT (d), "address", &s, NULL); + if (g_ascii_strncasecmp (bdaddr, s, BDADDR_STR_LEN) == 0) { + return g_steal_pointer (&d); + } + } + return NULL; +} + static char ** device_list_uuids (const gchar * const *uuids) { @@ -983,7 +1003,7 @@ up_device_added_cb (UpClient *up_client, gpointer user_data) { BluetoothClient *client = user_data; - g_autofree char *native_path = NULL; + g_autofree char *serial = NULL; g_autoptr(BluetoothDevice) device = NULL; UpDeviceLevel battery_level; double percentage; @@ -992,16 +1012,16 @@ up_device_added_cb (UpClient *up_client, g_debug ("Considering UPower device %s", up_device_get_object_path (up_device)); g_object_get (up_device, - "native-path", &native_path, + "serial", &serial, "battery-level", &battery_level, "percentage", &percentage, NULL); - if (!native_path || !g_str_has_prefix (native_path, "/org/bluez/")) + if (!serial || !bluetooth_verify_address(serial)) return; - device = get_device_for_path (client, native_path); + device = get_device_for_bdaddr (client, serial); if (!device) { - g_debug ("Could not find bluez device for upower device %s", native_path); + g_debug ("Could not find bluez device for upower device with serial %s", serial); return; } g_signal_connect (G_OBJECT (up_device), "notify::battery-level", @@ -1013,7 +1033,7 @@ up_device_added_cb (UpClient *up_client, battery_type = BLUETOOTH_BATTERY_TYPE_PERCENTAGE; else battery_type = BLUETOOTH_BATTERY_TYPE_COARSE; - g_debug ("Applying battery information for %s", native_path); + g_debug ("Applying battery information for %s", serial); g_object_set (device, "battery-type", battery_type, "battery-level", battery_level, diff --git a/lib/bluetooth-utils.h b/lib/bluetooth-utils.h index efe091bf..14499132 100644 --- a/lib/bluetooth-utils.h +++ b/lib/bluetooth-utils.h @@ -27,6 +27,8 @@ #include #include +#define BDADDR_STR_LEN 17 + BluetoothType bluetooth_class_to_type (guint32 class); BluetoothType bluetooth_appearance_to_type (guint16 appearance); const gchar *bluetooth_type_to_string (guint type); -- cgit v1.2.1