diff --git a/libfwupdplugin/fu-udev-device.c b/libfwupdplugin/fu-udev-device.c index 84ccfc360..9f9cf1c27 100644 --- a/libfwupdplugin/fu-udev-device.c +++ b/libfwupdplugin/fu-udev-device.c @@ -1781,6 +1781,48 @@ fu_udev_device_get_siblings_with_subsystem (FuUdevDevice *self, return g_steal_pointer(&out); } +/** + * fu_udev_device_get_children_with_subsystem + * @self: a #FuUdevDevice + * @subsystem: the name of a udev subsystem + * + * Get a list of devices that are children of self and have the + * provided subsystem. + * + * Returns (element-type FuUdevDevice) (transfer full): devices + * + * Since: 1.6.2 + */ +GPtrArray * +fu_udev_device_get_children_with_subsystem (FuUdevDevice *self, + const gchar *const subsystem) +{ + g_autoptr(GPtrArray) out = g_ptr_array_new_with_free_func (g_object_unref); + +#ifdef HAVE_GUDEV + const gchar *self_path = fu_udev_device_get_sysfs_path (self); + g_autoptr(GUdevClient) udev_client = g_udev_client_new (NULL); + + g_autoptr(GList) enumerated = g_udev_client_query_by_subsystem (udev_client, subsystem); + for (GList *element = enumerated; element != NULL; element = element->next) { + g_autoptr(GUdevDevice) enumerated_device = element->data; + g_autoptr(GUdevDevice) enumerated_parent = + g_udev_device_get_parent (enumerated_device); + const gchar *enumerated_parent_path = + g_udev_device_get_sysfs_path (enumerated_parent); + + /* 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))); + } + } +#endif + + return g_steal_pointer (&out); +} + static void fu_udev_device_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec) diff --git a/libfwupdplugin/fu-udev-device.h b/libfwupdplugin/fu-udev-device.h index 4f0368547..65ac8168b 100644 --- a/libfwupdplugin/fu-udev-device.h +++ b/libfwupdplugin/fu-udev-device.h @@ -122,3 +122,5 @@ gboolean fu_udev_device_write_sysfs (FuUdevDevice *self, const gchar *fu_udev_device_get_devtype (FuUdevDevice *self); GPtrArray *fu_udev_device_get_siblings_with_subsystem (FuUdevDevice *self, const gchar *subsystem); +GPtrArray *fu_udev_device_get_children_with_subsystem (FuUdevDevice *self, + const gchar *subsystem); diff --git a/libfwupdplugin/fwupdplugin.map b/libfwupdplugin/fwupdplugin.map index 56eb586f8..855567b1e 100644 --- a/libfwupdplugin/fwupdplugin.map +++ b/libfwupdplugin/fwupdplugin.map @@ -831,6 +831,7 @@ LIBFWUPDPLUGIN_1.6.2 { fu_i2c_device_read_full; fu_i2c_device_set_bus_number; fu_i2c_device_write_full; + fu_udev_device_get_children_with_subsystem; fu_udev_device_set_dev; local: *; } LIBFWUPDPLUGIN_1.6.1;