[gnome-bluetooth/wip/hadess/more-bt-battery-info] lib: Don't just get BlueZ battery info from upower




commit d6e5edd32e84ef5895d189e0197d7ef78cd6509a
Author: Bastien Nocera <hadess hadess net>
Date:   Fri Sep 2 15:51:08 2022 +0200

    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 | 34 ++++++++++++++++++++++++++++------
 lib/bluetooth-utils.h  |  2 ++
 2 files changed, 30 insertions(+), 6 deletions(-)
---
diff --git a/lib/bluetooth-client.c b/lib/bluetooth-client.c
index e005f2d0..45e15ad6 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,25 +1003,27 @@ 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;
        BluetoothBatteryType battery_type;
+       gboolean is_present;
 
        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,
+                     "is-present", &is_present,
                      NULL);
 
-       if (!native_path || !g_str_has_prefix (native_path, "/org/bluez/"))
+       if (!is_present || !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 +1035,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 <gio/gio.h>
 #include <bluetooth-enums.h>
 
+#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);


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]