diff --git a/libfwupdplugin/fu-plugin.c b/libfwupdplugin/fu-plugin.c index b11d9c519..0ab922c7d 100644 --- a/libfwupdplugin/fu-plugin.c +++ b/libfwupdplugin/fu-plugin.c @@ -40,6 +40,7 @@ typedef struct { guint order; guint priority; GPtrArray *rules[FU_PLUGIN_RULE_LAST]; + GPtrArray *devices; /* (nullable) (element-type FuDevice) */ gchar *build_hash; FuHwids *hwids; FuQuirks *quirks; @@ -477,6 +478,15 @@ fu_plugin_build_device_update_error (FuPlugin *self) return NULL; } +static void +fu_plugin_ensure_devices (FuPlugin *self) +{ + FuPluginPrivate *priv = GET_PRIVATE (self); + if (priv->devices != NULL) + return; + priv->devices = g_ptr_array_new_with_free_func ((GDestroyNotify) g_object_unref); +} + /** * fu_plugin_device_add: * @self: A #FuPlugin @@ -495,6 +505,7 @@ fu_plugin_build_device_update_error (FuPlugin *self) void fu_plugin_device_add (FuPlugin *self, FuDevice *device) { + FuPluginPrivate *priv = GET_PRIVATE (self); GPtrArray *children; g_autoptr(GError) error = NULL; @@ -507,6 +518,10 @@ fu_plugin_device_add (FuPlugin *self, FuDevice *device) return; } + /* add to array */ + fu_plugin_ensure_devices (self); + g_ptr_array_add (priv->devices, g_object_ref (device)); + /* proxy to device where required */ if (fu_plugin_has_flag (self, FWUPD_PLUGIN_FLAG_CLEAR_UPDATABLE)) { g_debug ("plugin %s has _CLEAR_UPDATABLE, so removing from %s", @@ -538,6 +553,26 @@ fu_plugin_device_add (FuPlugin *self, FuDevice *device) } } +/** + * fu_plugin_get_devices: + * @self: A #FuPlugin + * + * Returns all devices added by the plugin using fu_plugin_device_add() and + * not yet removed with fu_plugin_device_remove(). + * + * Returns: (transfer none) (element-type FuDevice): devices + * + * Since: 1.5.6 + **/ +GPtrArray * +fu_plugin_get_devices (FuPlugin *self) +{ + FuPluginPrivate *priv = GET_PRIVATE (self); + g_return_val_if_fail (FU_IS_PLUGIN (self), NULL); + fu_plugin_ensure_devices (self); + return priv->devices; +} + /** * fu_plugin_device_register: * @self: A #FuPlugin @@ -584,9 +619,15 @@ fu_plugin_device_register (FuPlugin *self, FuDevice *device) void fu_plugin_device_remove (FuPlugin *self, FuDevice *device) { + FuPluginPrivate *priv = GET_PRIVATE (self); + g_return_if_fail (FU_IS_PLUGIN (self)); g_return_if_fail (FU_IS_DEVICE (device)); + /* remove from array */ + if (priv->devices != NULL) + g_ptr_array_remove (priv->devices, device); + g_debug ("emit removed from %s: %s", fu_plugin_get_name (self), fu_device_get_id (device)); @@ -2906,6 +2947,8 @@ fu_plugin_finalize (GObject *object) if (priv->rules[i] != NULL) g_ptr_array_unref (priv->rules[i]); } + if (priv->devices != NULL) + g_ptr_array_unref (priv->devices); if (priv->usb_ctx != NULL) g_object_unref (priv->usb_ctx); if (priv->hwids != NULL) diff --git a/libfwupdplugin/fu-plugin.h b/libfwupdplugin/fu-plugin.h index b0fe44c73..846e7c873 100644 --- a/libfwupdplugin/fu-plugin.h +++ b/libfwupdplugin/fu-plugin.h @@ -140,6 +140,7 @@ gchar *fu_plugin_get_hwid_replace_value (FuPlugin *self, const gchar *keys, GError **error) G_GNUC_WARN_UNUSED_RESULT; +GPtrArray *fu_plugin_get_devices (FuPlugin *self); GPtrArray *fu_plugin_get_hwids (FuPlugin *self); const gchar *fu_plugin_get_dmi_value (FuPlugin *self, const gchar *dmi_id); diff --git a/libfwupdplugin/fu-self-test.c b/libfwupdplugin/fu-self-test.c index df0a21bbf..f1b9d30a1 100644 --- a/libfwupdplugin/fu-self-test.c +++ b/libfwupdplugin/fu-self-test.c @@ -423,6 +423,24 @@ _plugin_device_added_cb (FuPlugin *plugin, FuDevice *device, gpointer user_data) fu_test_loop_quit (); } +static void +fu_plugin_devices_func (void) +{ + g_autoptr(FuDevice) device = fu_device_new (); + g_autoptr(FuPlugin) plugin = fu_plugin_new (); + GPtrArray *devices; + + devices = fu_plugin_get_devices (plugin); + g_assert_nonnull (devices); + g_assert_cmpint (devices->len, ==, 0); + + fu_device_set_id (device, "testdev"); + fu_plugin_device_add (plugin, device); + g_assert_cmpint (devices->len, ==, 1); + fu_plugin_device_remove (plugin, device); + g_assert_cmpint (devices->len, ==, 0); +} + static void fu_plugin_delay_func (void) { @@ -2129,6 +2147,7 @@ main (int argc, char **argv) g_setenv ("FWUPD_LOCALSTATEDIR", "/tmp/fwupd-self-test/var", TRUE); g_test_add_func ("/fwupd/security-attrs{hsi}", fu_security_attrs_hsi_func); + g_test_add_func ("/fwupd/plugin{devices}", fu_plugin_devices_func); g_test_add_func ("/fwupd/plugin{delay}", fu_plugin_delay_func); g_test_add_func ("/fwupd/plugin{quirks}", fu_plugin_quirks_func); g_test_add_func ("/fwupd/plugin{quirks-performance}", fu_plugin_quirks_performance_func); diff --git a/libfwupdplugin/fwupdplugin.map b/libfwupdplugin/fwupdplugin.map index 9043bd243..f109bdd4c 100644 --- a/libfwupdplugin/fwupdplugin.map +++ b/libfwupdplugin/fwupdplugin.map @@ -721,3 +721,9 @@ LIBFWUPDPLUGIN_1.5.5 { fu_firmware_image_get_checksum; local: *; } LIBFWUPDPLUGIN_1.5.4; + +LIBFWUPDPLUGIN_1.5.6 { + global: + fu_plugin_get_devices; + local: *; +} LIBFWUPDPLUGIN_1.5.5;