[gnome-bluetooth/wip/hadess/lib-changes: 11/24] lib: Add BluetoothDevice object
- From: Bastien Nocera <hadess src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnome-bluetooth/wip/hadess/lib-changes: 11/24] lib: Add BluetoothDevice object
- Date: Fri, 26 Nov 2021 15:29:55 +0000 (UTC)
commit 36d2d0b434a544c5e057168c7e4427c7e1143c5d
Author: Bastien Nocera <hadess hadess net>
Date: Fri Nov 19 13:47:59 2021 +0100
lib: Add BluetoothDevice object
lib/bluetooth-device.c | 255 ++++++++++++++++++++++++++++++++++++++++++
lib/bluetooth-device.h | 18 +++
lib/gnome-bluetooth.map | 4 +
lib/meson.build | 1 +
tests/meson.build | 12 ++
tests/test-bluetooth-device.c | 53 +++++++++
6 files changed, 343 insertions(+)
---
diff --git a/lib/bluetooth-device.c b/lib/bluetooth-device.c
new file mode 100644
index 00000000..570dfcc6
--- /dev/null
+++ b/lib/bluetooth-device.c
@@ -0,0 +1,255 @@
+/*
+ * Copyright (C) 2021 Bastien Nocera <hadess hadess net>
+ *
+ * SPDX-License-Identifier: LGPL-2.1-or-later
+ */
+
+#include "config.h"
+
+#include <glib-object.h>
+
+#include "bluetooth-device.h"
+#include "bluetooth-utils.h"
+#include "gnome-bluetooth-enum-types.h"
+
+enum {
+ PROP_PROXY = 1,
+ PROP_ADDRESS,
+ PROP_ALIAS,
+ PROP_NAME,
+ PROP_TYPE,
+ PROP_ICON,
+ PROP_PAIRED,
+ PROP_TRUSTED,
+ PROP_CONNECTED,
+ PROP_LEGACYPAIRING,
+ PROP_UUIDS,
+};
+
+struct _BluetoothDevice {
+ GObject parent;
+
+ GDBusProxy *proxy;
+ char *address;
+ char *alias;
+ char *name;
+ BluetoothType type;
+ char *icon;
+ gboolean paired;
+ gboolean trusted;
+ gboolean connected;
+ gboolean legacy_pairing;
+ char **uuids;
+};
+
+G_DEFINE_TYPE(BluetoothDevice, bluetooth_device, G_TYPE_OBJECT)
+
+static void
+bluetooth_device_get_property (GObject *object,
+ guint property_id,
+ GValue *value,
+ GParamSpec *pspec)
+{
+ BluetoothDevice *device = BLUETOOTH_DEVICE (object);
+
+ switch (property_id) {
+ case PROP_PROXY:
+ g_value_set_object (value, device->proxy);
+ break;
+ case PROP_ADDRESS:
+ g_value_set_string (value, device->address);
+ break;
+ case PROP_ALIAS:
+ g_value_set_string (value, device->alias);
+ break;
+ case PROP_NAME:
+ g_value_set_string (value, device->name);
+ break;
+ case PROP_TYPE:
+ g_value_set_flags (value, device->type);
+ break;
+ case PROP_ICON:
+ g_value_set_string (value, device->icon);
+ break;
+ case PROP_PAIRED:
+ g_value_set_boolean (value, device->paired);
+ break;
+ case PROP_TRUSTED:
+ g_value_set_boolean (value, device->trusted);
+ break;
+ case PROP_CONNECTED:
+ g_value_set_boolean (value, device->connected);
+ break;
+ case PROP_LEGACYPAIRING:
+ g_value_set_boolean (value, device->legacy_pairing);
+ break;
+ case PROP_UUIDS:
+ g_value_set_boxed (value, device->uuids);
+ break;
+ default:
+ G_OBJECT_WARN_INVALID_PROPERTY_ID(object, property_id, pspec);
+ break;
+ }
+}
+
+static void
+bluetooth_device_set_property (GObject *object,
+ guint property_id,
+ const GValue *value,
+ GParamSpec *pspec)
+{
+ BluetoothDevice *device = BLUETOOTH_DEVICE (object);
+
+ switch (property_id) {
+ case PROP_PROXY:
+ g_clear_object (&device->proxy);
+ device->proxy = g_value_get_object (value);
+ break;
+ case PROP_ADDRESS:
+ g_clear_pointer (&device->address, g_free);
+ device->address = g_value_dup_string (value);
+ break;
+ case PROP_ALIAS:
+ g_clear_pointer (&device->alias, g_free);
+ device->alias = g_value_dup_string (value);
+ break;
+ case PROP_NAME:
+ g_clear_pointer (&device->name, g_free);
+ device->name = g_value_dup_string (value);
+ break;
+ case PROP_TYPE:
+ device->type = g_value_get_flags (value);
+ break;
+ case PROP_ICON:
+ g_clear_pointer (&device->icon, g_free);
+ device->icon = g_value_dup_string (value);
+ break;
+ case PROP_PAIRED:
+ device->paired = g_value_get_boolean (value);
+ break;
+ case PROP_TRUSTED:
+ device->trusted = g_value_get_boolean (value);
+ break;
+ case PROP_CONNECTED:
+ device->connected = g_value_get_boolean (value);
+ break;
+ case PROP_LEGACYPAIRING:
+ device->legacy_pairing = g_value_get_boolean (value);
+ break;
+ case PROP_UUIDS:
+ g_clear_pointer (&device->uuids, g_strfreev);
+ device->uuids = g_value_dup_boxed (value);
+ break;
+ default:
+ G_OBJECT_WARN_INVALID_PROPERTY_ID(object, property_id, pspec);
+ break;
+ }
+}
+
+static void bluetooth_device_finalize(GObject *object)
+{
+ BluetoothDevice *device = BLUETOOTH_DEVICE (object);
+
+ g_clear_object (&device->proxy);
+ g_clear_pointer (&device->address, g_free);
+ g_clear_pointer (&device->alias, g_free);
+ g_clear_pointer (&device->name, g_free);
+ g_clear_pointer (&device->icon, g_free);
+ g_clear_pointer (&device->uuids, g_strfreev);
+
+ G_OBJECT_CLASS(bluetooth_device_parent_class)->finalize (object);
+}
+
+static void bluetooth_device_class_init(BluetoothDeviceClass *klass)
+{
+ GObjectClass *object_class = G_OBJECT_CLASS (klass);
+
+ object_class->finalize = bluetooth_device_finalize;
+ object_class->get_property = bluetooth_device_get_property;
+ object_class->set_property = bluetooth_device_set_property;
+
+ g_object_class_install_property (object_class, PROP_PROXY,
+ g_param_spec_object ("proxy", NULL, "Proxy",
+ G_TYPE_DBUS_PROXY, G_PARAM_READWRITE));
+ g_object_class_install_property (object_class, PROP_ADDRESS,
+ g_param_spec_string ("address", NULL, "Address",
+ NULL, G_PARAM_READWRITE));
+ g_object_class_install_property (object_class, PROP_ALIAS,
+ g_param_spec_string ("alias", NULL, "Alias",
+ NULL, G_PARAM_READWRITE));
+ g_object_class_install_property (object_class, PROP_NAME,
+ g_param_spec_string ("name", NULL, "Name",
+ NULL, G_PARAM_READWRITE));
+ g_object_class_install_property (object_class, PROP_TYPE,
+ g_param_spec_flags ("type", NULL, "Type",
+ BLUETOOTH_TYPE_TYPE, BLUETOOTH_TYPE_ANY,
G_PARAM_READWRITE));
+ g_object_class_install_property (object_class, PROP_ICON,
+ g_param_spec_string ("icon", NULL, "Icon",
+ NULL, G_PARAM_READWRITE));
+ g_object_class_install_property (object_class, PROP_PAIRED,
+ g_param_spec_boolean ("paired", NULL, "Paired",
+ FALSE, G_PARAM_READWRITE));
+ g_object_class_install_property (object_class, PROP_TRUSTED,
+ g_param_spec_boolean ("trusted", NULL, "Trusted",
+ FALSE, G_PARAM_READWRITE));
+ g_object_class_install_property (object_class, PROP_CONNECTED,
+ g_param_spec_boolean ("connected", NULL, "Connected",
+ FALSE, G_PARAM_READWRITE));
+ g_object_class_install_property (object_class, PROP_LEGACYPAIRING,
+ g_param_spec_boolean ("legacy-pairing", NULL, "Legacy Pairing",
+ FALSE, G_PARAM_READWRITE));
+ g_object_class_install_property (object_class, PROP_UUIDS,
+ g_param_spec_boxed ("uuids", NULL, "UUIDs",
+ G_TYPE_STRV, G_PARAM_READWRITE));
+}
+
+static void
+bluetooth_device_init (BluetoothDevice *device)
+{
+}
+
+const char *
+bluetooth_device_get_object_path (BluetoothDevice *device)
+{
+ g_return_val_if_fail (BLUETOOTH_IS_DEVICE (device), NULL);
+
+ if (device->proxy == NULL)
+ return NULL;
+ return g_dbus_proxy_get_object_path (device->proxy);
+}
+
+#define BOOL_STR(x) (x ? "True" : "False")
+
+char *
+bluetooth_device_to_string (BluetoothDevice *device)
+{
+ GString *str;
+
+ g_return_val_if_fail (BLUETOOTH_IS_DEVICE (device), NULL);
+
+ str = g_string_new (NULL);
+
+ g_string_append_printf (str, "Device: %s (%s)\n", device->alias, device->address);
+ g_string_append_printf (str, "\tD-Bus Path: %s\n", device->proxy ? g_dbus_proxy_get_object_path
(device->proxy) : "(none)");
+ g_string_append_printf (str, "\tType: %s Icon: %s\n", bluetooth_type_to_string (device->type),
device->icon);
+ g_string_append_printf (str, "\tPaired: %s Trusted: %s Connected: %s\n", BOOL_STR(device->paired),
BOOL_STR(device->trusted), BOOL_STR(device->connected));
+ if (device->uuids != NULL) {
+ guint i;
+ g_string_append_printf (str, "\tUUIDs: ");
+ for (i = 0; device->uuids[i] != NULL; i++)
+ g_string_append_printf (str, "%s ", device->uuids[i]);
+ }
+
+ return g_string_free (str, FALSE);
+}
+
+void
+bluetooth_device_dump (BluetoothDevice *device)
+{
+ g_autofree char *str = NULL;
+
+ g_return_if_fail (BLUETOOTH_IS_DEVICE (device));
+
+ str = bluetooth_device_to_string (device);
+ g_print ("%s\n", str);
+}
diff --git a/lib/bluetooth-device.h b/lib/bluetooth-device.h
new file mode 100644
index 00000000..bef2f39e
--- /dev/null
+++ b/lib/bluetooth-device.h
@@ -0,0 +1,18 @@
+/*
+ * Copyright (C) 2021 Bastien Nocera <hadess hadess net>
+ *
+ * SPDX-License-Identifier: LGPL-2.1-or-later
+ */
+
+#pragma once
+
+#include <glib-object.h>
+#include <gtk/gtk.h>
+#include <bluetooth-enums.h>
+
+#define BLUETOOTH_TYPE_DEVICE (bluetooth_device_get_type())
+G_DECLARE_FINAL_TYPE (BluetoothDevice, bluetooth_device, BLUETOOTH, DEVICE, GObject)
+
+const char *bluetooth_device_get_object_path (BluetoothDevice *device);
+void bluetooth_device_dump (BluetoothDevice *device);
+char *bluetooth_device_to_string (BluetoothDevice *device);
diff --git a/lib/gnome-bluetooth.map b/lib/gnome-bluetooth.map
index 9c448a62..e9fcb898 100644
--- a/lib/gnome-bluetooth.map
+++ b/lib/gnome-bluetooth.map
@@ -20,6 +20,10 @@ global:
bluetooth_column_get_type;
bluetooth_type_get_type;
bluetooth_status_get_type;
+ bluetooth_device_get_type;
+ bluetooth_device_dump;
+ bluetooth_device_get_object_path;
+ bluetooth_device_to_string;
bluetooth_agent_get_type;
bluetooth_agent_error_get_type;
bluetooth_agent_new;
diff --git a/lib/meson.build b/lib/meson.build
index 7ea744a8..4465e8c0 100644
--- a/lib/meson.build
+++ b/lib/meson.build
@@ -16,6 +16,7 @@ install_headers(
sources = files(
'bluetooth-agent.c',
'bluetooth-client.c',
+ 'bluetooth-device.c',
'bluetooth-pairing-dialog.c',
'bluetooth-settings-obexpush.c',
'bluetooth-settings-row.c',
diff --git a/tests/meson.build b/tests/meson.build
index a95cbb5d..817c9de0 100644
--- a/tests/meson.build
+++ b/tests/meson.build
@@ -14,3 +14,15 @@ if enable_gir
depends: test_deps
)
endif
+
+test_bluetooth_device = executable('test-bluetooth-device',
+ 'test-bluetooth-device.c',
+ include_directories: lib_inc,
+ dependencies: deps + private_deps,
+ c_args: cflags,
+ link_with: libgnome_bluetooth,
+)
+
+test('test-bluetooth-device-test',
+ test_bluetooth_device,
+)
diff --git a/tests/test-bluetooth-device.c b/tests/test-bluetooth-device.c
new file mode 100644
index 00000000..c47a2b68
--- /dev/null
+++ b/tests/test-bluetooth-device.c
@@ -0,0 +1,53 @@
+/*
+ * Copyright (C) 2021 Bastien Nocera <hadess hadess net>
+ *
+ * SPDX-License-Identifier: LGPL-2.1-or-later
+ */
+
+#include <glib.h>
+#include <glib/gi18n.h>
+
+#include "bluetooth-device.h"
+
+static void
+test_device (void)
+{
+ BluetoothDevice *device;
+ GDBusProxy *proxy;
+ const char *uuids[] = {
+ "OBEXFileTransfer",
+ NULL
+ };
+ const char *new_uuids[] = {
+ "AudioSource",
+ NULL
+ };
+
+ proxy = g_object_new (G_TYPE_DBUS_PROXY, NULL);
+ g_assert_nonnull (proxy);
+ device = g_object_new (BLUETOOTH_TYPE_DEVICE,
+ "proxy", proxy,
+ "name", "Fake Name",
+ "alias", "Changed Fake Name",
+ "address", "00:11:22:33:44",
+ "type", BLUETOOTH_TYPE_KEYBOARD,
+ "icon", "phone",
+ "paired", TRUE,
+ "trusted", TRUE,
+ "connected", FALSE,
+ "legacy-pairing", FALSE,
+ "uuids", uuids,
+ NULL);
+ g_object_set (G_OBJECT (device),
+ "uuids", new_uuids,
+ NULL);
+ g_object_unref (device);
+}
+
+int main (int argc, char **argv)
+{
+ g_test_init (&argc, &argv, NULL);
+ g_test_add_func ("/bluetooth/device", test_device);
+
+ return g_test_run ();
+}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]