Add fu_usb_device_new_with_context() helpers

This means the context is set at construction time, which is much less
fragile than setting the context manually.
This commit is contained in:
Richard Hughes 2021-10-14 16:15:52 +01:00
parent ccc0f9b0c6
commit bd43647554
11 changed files with 89 additions and 31 deletions

View File

@ -1746,8 +1746,10 @@ fu_udev_device_get_siblings_with_subsystem(FuUdevDevice *self, const gchar *cons
/* if the sysfs path of self's parent is the same as that of the
* located device's parent, they are siblings */
if (g_strcmp0(udev_parent_path, enumerated_parent_path) == 0) {
g_ptr_array_add(out,
fu_udev_device_new(g_steal_pointer(&enumerated_device)));
FuUdevDevice *dev =
fu_udev_device_new_with_context(fu_device_get_context(FU_DEVICE(self)),
g_steal_pointer(&enumerated_device));
g_ptr_array_add(out, dev);
}
}
#endif
@ -1787,8 +1789,10 @@ fu_udev_device_get_children_with_subsystem(FuUdevDevice *self, const gchar *cons
/* enumerated device is a child of self if its parent is the
* same as self */
if (g_strcmp0(self_path, enumerated_parent_path) == 0) {
g_ptr_array_add(out,
fu_udev_device_new(g_steal_pointer(&enumerated_device)));
FuUdevDevice *dev =
fu_udev_device_new_with_context(fu_device_get_context(FU_DEVICE(self)),
g_steal_pointer(&enumerated_device));
g_ptr_array_add(out, dev);
}
}
#endif
@ -1925,6 +1929,34 @@ fu_udev_device_class_init(FuUdevDeviceClass *klass)
g_object_class_install_property(object_class, PROP_DEVICE_FILE, pspec);
}
/**
* fu_udev_device_new_with_context:
* @ctx: (nullable): a #FuContext
* @udev_device: a #GUdevDevice
*
* Creates a new #FuUdevDevice.
*
* Returns: (transfer full): a #FuUdevDevice
*
* Since: 1.7.1
**/
FuUdevDevice *
fu_udev_device_new_with_context(FuContext *ctx, GUdevDevice *udev_device)
{
#ifdef HAVE_GUDEV
/* create the correct object depending on the subsystem */
if (g_strcmp0(g_udev_device_get_subsystem(udev_device), "i2c-dev") == 0) {
return g_object_new(FU_TYPE_I2C_DEVICE,
"context",
ctx,
"udev-device",
udev_device,
NULL);
}
#endif
return g_object_new(FU_TYPE_UDEV_DEVICE, "context", ctx, "udev-device", udev_device, NULL);
}
/**
* fu_udev_device_new:
* @udev_device: a #GUdevDevice
@ -1938,12 +1970,5 @@ fu_udev_device_class_init(FuUdevDeviceClass *klass)
FuUdevDevice *
fu_udev_device_new(GUdevDevice *udev_device)
{
#ifdef HAVE_GUDEV
/* create the correct object depending on the subsystem */
if (g_strcmp0(g_udev_device_get_subsystem(udev_device), "i2c-dev") == 0) {
return FU_UDEV_DEVICE(
g_object_new(FU_TYPE_I2C_DEVICE, "udev-device", udev_device, NULL));
}
#endif
return FU_UDEV_DEVICE(g_object_new(FU_TYPE_UDEV_DEVICE, "udev-device", udev_device, NULL));
return fu_udev_device_new_with_context(NULL, udev_device);
}

View File

@ -48,7 +48,9 @@ typedef enum {
} FuUdevDeviceFlags;
FuUdevDevice *
fu_udev_device_new(GUdevDevice *udev_device);
fu_udev_device_new(GUdevDevice *udev_device) G_DEPRECATED_FOR(fu_udev_device_new_with_context);
FuUdevDevice *
fu_udev_device_new_with_context(FuContext *ctx, GUdevDevice *udev_device);
GUdevDevice *
fu_udev_device_get_dev(FuUdevDevice *self);
void

View File

@ -585,7 +585,7 @@ fu_udev_device_bind_driver(FuDevice *device,
dev = fu_usb_device_find_udev_device(self, error);
if (dev == NULL)
return FALSE;
udev_device = fu_udev_device_new(dev);
udev_device = fu_udev_device_new_with_context(fu_device_get_context(device), dev);
return fu_device_bind_driver(FU_DEVICE(udev_device), subsystem, driver, error);
}
@ -600,10 +600,27 @@ fu_udev_device_unbind_driver(FuDevice *device, GError **error)
dev = fu_usb_device_find_udev_device(self, error);
if (dev == NULL)
return FALSE;
udev_device = fu_udev_device_new(dev);
udev_device = fu_udev_device_new_with_context(fu_device_get_context(device), dev);
return fu_device_unbind_driver(FU_DEVICE(udev_device), error);
}
/**
* fu_usb_device_new_with_context:
* @ctx: (nullable): a #FuContext
* @usb_device: a USB device
*
* Creates a new #FuUsbDevice.
*
* Returns: (transfer full): a #FuUsbDevice
*
* Since: 1.7.1
**/
FuUsbDevice *
fu_usb_device_new_with_context(FuContext *ctx, GUsbDevice *usb_device)
{
return g_object_new(FU_TYPE_USB_DEVICE, "context", ctx, "usb-device", usb_device, NULL);
}
/**
* fu_usb_device_new:
* @usb_device: a USB device
@ -617,9 +634,7 @@ fu_udev_device_unbind_driver(FuDevice *device, GError **error)
FuUsbDevice *
fu_usb_device_new(GUsbDevice *usb_device)
{
FuUsbDevice *device = g_object_new(FU_TYPE_USB_DEVICE, NULL);
fu_usb_device_set_dev(device, usb_device);
return FU_USB_DEVICE(device);
return fu_usb_device_new_with_context(NULL, usb_device);
}
#ifdef HAVE_GUSB

View File

@ -29,7 +29,9 @@ struct _FuUsbDeviceClass {
};
FuUsbDevice *
fu_usb_device_new(GUsbDevice *usb_device);
fu_usb_device_new(GUsbDevice *usb_device) G_DEPRECATED_FOR(fu_usb_device_new_with_context);
FuUsbDevice *
fu_usb_device_new_with_context(FuContext *ctx, GUsbDevice *usb_device);
guint16
fu_usb_device_get_vid(FuUsbDevice *self);
guint16

View File

@ -930,5 +930,7 @@ LIBFWUPDPLUGIN_1.7.1 {
fu_common_check_full_disk_encryption;
fu_common_mkdir;
fu_device_add_string;
fu_udev_device_new_with_context;
fu_usb_device_new_with_context;
local: *;
} LIBFWUPDPLUGIN_1.7.0;

View File

@ -267,11 +267,12 @@ fu_plugin_dell_dock_func(gconstpointer user_data)
DOCK_INFO *dock_info;
g_autoptr(GError) error = NULL;
g_autoptr(GPtrArray) devices = NULL;
g_autoptr(FuUsbDevice) fake_usb_device = fu_usb_device_new(NULL);
g_autoptr(FuUsbDevice) fake_usb_device = NULL;
gulong added_id;
gulong register_id;
fu_device_set_context(FU_DEVICE(fake_usb_device), fu_plugin_get_context(self->plugin_dell));
fake_usb_device =
fu_usb_device_new_with_context(fu_plugin_get_context(self->plugin_dell), NULL);
devices = g_ptr_array_new_with_free_func((GDestroyNotify)g_object_unref);
added_id = g_signal_connect(self->plugin_uefi_capsule,
"device-added",

View File

@ -591,13 +591,13 @@ fu_parade_lspcon_device_reload(FuDevice *device, GError **error)
self->aux_device_name);
return FALSE;
}
aux_device = fu_udev_device_new(g_steal_pointer(&aux_devices->data));
aux_device = fu_udev_device_new_with_context(fu_device_get_context(device),
g_steal_pointer(&aux_devices->data));
g_debug("using aux dev %s", fu_udev_device_get_sysfs_path(aux_device));
/* the following open() requires the device have IDs set */
if (!fu_udev_device_set_physical_id(aux_device, "drm_dp_aux_dev", error))
return FALSE;
fu_device_set_context(FU_DEVICE(aux_device), fu_device_get_context(device));
/* open device to read version from DPCD */
if ((aux_device_locker = fu_device_locker_new(aux_device, error)) == NULL)

View File

@ -181,9 +181,11 @@ fu_realtek_mst_device_use_aux_dev(FuRealtekMstDevice *self, GError **error)
* I2C bus that runs over DPDDC on the port represented by the
* drm_dp_aux_dev */
for (GList *element = matches; element != NULL; element = element->next) {
g_autoptr(FuUdevDevice) device = fu_udev_device_new(element->data);
g_autoptr(FuUdevDevice) device = NULL;
g_autoptr(GPtrArray) i2c_devices = NULL;
device = fu_udev_device_new_with_context(fu_device_get_context(FU_DEVICE(self)),
element->data);
if (bus_device != NULL) {
g_debug("Ignoring additional aux device %s",
fu_udev_device_get_sysfs_path(device));
@ -221,9 +223,11 @@ fu_realtek_mst_device_use_drm_card(FuRealtekMstDevice *self, GError **error)
g_udev_enumerator_add_match_name(enumerator, self->dp_card_kernel_name);
drm_devices = g_udev_enumerator_execute(enumerator);
for (GList *element = drm_devices; element != NULL; element = element->next) {
g_autoptr(FuUdevDevice) drm_device = fu_udev_device_new(element->data);
g_autoptr(FuUdevDevice) drm_device = NULL;
g_autoptr(GPtrArray) i2c_devices = NULL;
drm_device = fu_udev_device_new_with_context(fu_device_get_context(FU_DEVICE(self)),
element->data);
if (bus_device != NULL) {
g_debug("Ignoring additional drm device %s",
fu_udev_device_get_sysfs_path(drm_device));

View File

@ -841,6 +841,7 @@ typedef enum TestFlags {
typedef struct ThunderboltTest {
UMockdevTestbed *bed;
FuPlugin *plugin;
FuContext *ctx;
GUdevClient *udev_client;
/* if TestParam::initialize_tree */
@ -859,8 +860,10 @@ fu_thunderbolt_gudev_uevent_cb(GUdevClient *gudev_client,
ThunderboltTest *tt)
{
if (g_strcmp0(action, "add") == 0) {
g_autoptr(FuUdevDevice) device = fu_udev_device_new(udev_device);
g_autoptr(FuUdevDevice) device = NULL;
g_autoptr(GError) error_local = NULL;
device = fu_udev_device_new_with_context(tt->ctx, udev_device);
if (!fu_plugin_runner_backend_device_added(tt->plugin,
FU_DEVICE(device),
&error_local))
@ -888,10 +891,10 @@ test_set_up(ThunderboltTest *tt, gconstpointer params)
g_autofree gchar *pluginfn = NULL;
g_autofree gchar *sysfs = NULL;
g_autoptr(GError) error = NULL;
g_autoptr(FuContext) ctx = fu_context_new();
const gchar *udev_subsystems[] = {"thunderbolt", NULL};
ret = fu_context_load_quirks(ctx,
tt->ctx = fu_context_new();
ret = fu_context_load_quirks(tt->ctx,
FU_QUIRKS_LOAD_FLAG_NO_CACHE | FU_QUIRKS_LOAD_FLAG_NO_VERIFY,
&error);
g_assert_no_error(error);
@ -903,7 +906,7 @@ test_set_up(ThunderboltTest *tt, gconstpointer params)
sysfs = umockdev_testbed_get_sys_dir(tt->bed);
g_debug("mock sysfs at %s", sysfs);
tt->plugin = fu_plugin_new(ctx);
tt->plugin = fu_plugin_new(tt->ctx);
g_assert_nonnull(tt->plugin);
pluginfn =
@ -955,6 +958,7 @@ static void
test_tear_down(ThunderboltTest *tt, gconstpointer user_data)
{
g_object_unref(tt->plugin);
g_object_unref(tt->ctx);
g_object_unref(tt->bed);
g_object_unref(tt->udev_client);

View File

@ -25,9 +25,11 @@ G_DEFINE_TYPE(FuUdevBackend, fu_udev_backend, FU_TYPE_BACKEND)
static void
fu_udev_backend_device_add(FuUdevBackend *self, GUdevDevice *udev_device)
{
g_autoptr(FuUdevDevice) device = fu_udev_device_new(udev_device);
g_autoptr(FuUdevDevice) device = NULL;
/* success */
device =
fu_udev_device_new_with_context(fu_backend_get_context(FU_BACKEND(self)), udev_device);
fu_backend_device_added(FU_BACKEND(self), FU_DEVICE(device));
}

View File

@ -23,9 +23,10 @@ G_DEFINE_TYPE(FuUsbBackend, fu_usb_backend, FU_TYPE_BACKEND)
static void
fu_usb_backend_device_added_cb(GUsbContext *ctx, GUsbDevice *usb_device, FuBackend *backend)
{
g_autoptr(FuUsbDevice) device = fu_usb_device_new(usb_device);
g_autoptr(FuUsbDevice) device = NULL;
/* success */
device = fu_usb_device_new_with_context(fu_backend_get_context(backend), usb_device);
fu_backend_device_added(backend, FU_DEVICE(device));
}