blob: ca965f1d9f690d5bfbf69412a6fb29b08eb299c8 [file] [log] [blame]
Richard Hughes02c90d82018-08-09 12:13:03 +01001/*
Richard Hughes2de8f132018-01-17 09:12:02 +00002 * Copyright (C) 2016-2018 Richard Hughes <richard@hughsie.com>
Richard Hughesd0905142016-03-13 09:46:49 +00003 *
Mario Limonciello51308e62018-05-28 20:05:46 -05004 * SPDX-License-Identifier: LGPL-2.1+
Richard Hughesd0905142016-03-13 09:46:49 +00005 */
6
Richard Hughesb08e7bc2018-09-11 10:51:13 +01007#define G_LOG_DOMAIN "FuPlugin"
8
Richard Hughesd0905142016-03-13 09:46:49 +00009#include "config.h"
10
Richard Hughescff38bc2016-12-12 12:03:37 +000011#include <fwupd.h>
12#include <gmodule.h>
Richard Hughescff38bc2016-12-12 12:03:37 +000013#include <errno.h>
14#include <string.h>
Richard Hughes68f12dd2018-08-09 14:43:31 +010015#include <unistd.h>
Richard Hughescff38bc2016-12-12 12:03:37 +000016#include <gio/gunixinputstream.h>
Mario Limonciello6d0aa3d2017-02-28 08:22:27 -060017#ifdef HAVE_VALGRIND
Richard Hughes576c0122017-02-24 09:47:00 +000018#include <valgrind.h>
Mario Limonciello6d0aa3d2017-02-28 08:22:27 -060019#endif /* HAVE_VALGRIND */
Richard Hughesd0905142016-03-13 09:46:49 +000020
Richard Hughes9dde04f2017-09-13 12:07:15 +010021#include "fu-device-private.h"
Richard Hughescff38bc2016-12-12 12:03:37 +000022#include "fu-plugin-private.h"
Richard Hughesbc3a4e12018-01-06 22:41:47 +000023#include "fu-history.h"
Richard Hughes37d09432018-09-09 10:39:45 +010024#include "fu-mutex.h"
Richard Hughesd0905142016-03-13 09:46:49 +000025
Richard Hughes4eada342017-10-03 21:20:32 +010026/**
27 * SECTION:fu-plugin
28 * @short_description: a daemon plugin
29 *
30 * An object that represents a plugin run by the daemon.
31 *
32 * See also: #FuDevice
33 */
34
Richard Hughesb0829032017-01-10 09:27:08 +000035#define FU_PLUGIN_COLDPLUG_DELAY_MAXIMUM 3000u /* ms */
36
Richard Hughescff38bc2016-12-12 12:03:37 +000037static void fu_plugin_finalize (GObject *object);
38
39typedef struct {
40 GModule *module;
41 GUsbContext *usb_ctx;
42 gboolean enabled;
Richard Hughes08a37992017-09-12 12:57:43 +010043 guint order;
Richard Hughes81c427c2018-08-06 15:20:17 +010044 guint priority;
Richard Hughes08a37992017-09-12 12:57:43 +010045 GPtrArray *rules[FU_PLUGIN_RULE_LAST];
Richard Hughescff38bc2016-12-12 12:03:37 +000046 gchar *name;
Richard Hughesd7704d42017-08-08 20:29:09 +010047 FuHwids *hwids;
Richard Hughes9c028f02017-10-28 21:14:28 +010048 FuQuirks *quirks;
Richard Hughes0eb123b2018-04-19 12:00:04 +010049 GHashTable *runtime_versions;
Richard Hughes34e0dab2018-04-20 16:43:00 +010050 GHashTable *compile_versions;
Richard Hughes9d6e0e72018-08-24 20:20:17 +010051 GPtrArray *udev_subsystems;
Richard Hughes1354ea92017-09-19 15:58:31 +010052 FuSmbios *smbios;
Richard Hughescff38bc2016-12-12 12:03:37 +000053 GHashTable *devices; /* platform_id:GObject */
Richard Hughes37d09432018-09-09 10:39:45 +010054 FuMutex *devices_mutex;
Richard Hughes80b79bb2018-01-11 21:11:06 +000055 GHashTable *report_metadata; /* key:value */
Richard Hughescff38bc2016-12-12 12:03:37 +000056 FuPluginData *data;
57} FuPluginPrivate;
58
59enum {
60 SIGNAL_DEVICE_ADDED,
61 SIGNAL_DEVICE_REMOVED,
Richard Hughese1fd34d2017-08-24 14:19:51 +010062 SIGNAL_DEVICE_REGISTER,
Richard Hughes362d6d72017-01-07 21:42:14 +000063 SIGNAL_RECOLDPLUG,
Richard Hughesb0829032017-01-10 09:27:08 +000064 SIGNAL_SET_COLDPLUG_DELAY,
Richard Hughesaabdc372018-11-14 10:11:08 +000065 SIGNAL_CHECK_SUPPORTED,
Richard Hughescff38bc2016-12-12 12:03:37 +000066 SIGNAL_LAST
67};
68
69static guint signals[SIGNAL_LAST] = { 0 };
70
71G_DEFINE_TYPE_WITH_PRIVATE (FuPlugin, fu_plugin, G_TYPE_OBJECT)
72#define GET_PRIVATE(o) (fu_plugin_get_instance_private (o))
73
74typedef const gchar *(*FuPluginGetNameFunc) (void);
Richard Hughes12724852018-09-04 13:53:44 +010075typedef void (*FuPluginInitFunc) (FuPlugin *self);
76typedef gboolean (*FuPluginStartupFunc) (FuPlugin *self,
Richard Hughescff38bc2016-12-12 12:03:37 +000077 GError **error);
Richard Hughes12724852018-09-04 13:53:44 +010078typedef void (*FuPluginDeviceRegisterFunc) (FuPlugin *self,
Richard Hughese1fd34d2017-08-24 14:19:51 +010079 FuDevice *device);
Richard Hughes12724852018-09-04 13:53:44 +010080typedef gboolean (*FuPluginDeviceFunc) (FuPlugin *self,
Richard Hughescff38bc2016-12-12 12:03:37 +000081 FuDevice *device,
82 GError **error);
Richard Hughes12724852018-09-04 13:53:44 +010083typedef gboolean (*FuPluginFlaggedDeviceFunc) (FuPlugin *self,
Mario Limoncielloe3b1a3f2018-08-21 13:01:45 -050084 FwupdInstallFlags flags,
85 FuDevice *device,
86 GError **error);
Richard Hughes12724852018-09-04 13:53:44 +010087typedef gboolean (*FuPluginDeviceArrayFunc) (FuPlugin *self,
Richard Hughesdbd8c762018-06-15 20:31:40 +010088 GPtrArray *devices,
89 GError **error);
Richard Hughes12724852018-09-04 13:53:44 +010090typedef gboolean (*FuPluginVerifyFunc) (FuPlugin *self,
Richard Hughescff38bc2016-12-12 12:03:37 +000091 FuDevice *device,
92 FuPluginVerifyFlags flags,
93 GError **error);
Richard Hughes12724852018-09-04 13:53:44 +010094typedef gboolean (*FuPluginUpdateFunc) (FuPlugin *self,
Richard Hughescff38bc2016-12-12 12:03:37 +000095 FuDevice *device,
96 GBytes *blob_fw,
97 FwupdInstallFlags flags,
98 GError **error);
Richard Hughes12724852018-09-04 13:53:44 +010099typedef gboolean (*FuPluginUsbDeviceAddedFunc) (FuPlugin *self,
Richard Hughesff704412018-09-04 11:28:32 +0100100 FuUsbDevice *device,
Richard Hughes104f6512017-11-24 11:44:57 +0000101 GError **error);
Richard Hughes12724852018-09-04 13:53:44 +0100102typedef gboolean (*FuPluginUdevDeviceAddedFunc) (FuPlugin *self,
Richard Hughesff704412018-09-04 11:28:32 +0100103 FuUdevDevice *device,
Richard Hughes9d6e0e72018-08-24 20:20:17 +0100104 GError **error);
Richard Hughescff38bc2016-12-12 12:03:37 +0000105
Richard Hughes57d18222017-01-10 16:02:59 +0000106/**
107 * fu_plugin_get_name:
Richard Hughes2c0635a2018-09-04 14:52:46 +0100108 * @self: A #FuPlugin
Richard Hughes57d18222017-01-10 16:02:59 +0000109 *
110 * Gets the plugin name.
111 *
112 * Returns: a plugin name, or %NULL for unknown.
113 *
114 * Since: 0.8.0
115 **/
Richard Hughescff38bc2016-12-12 12:03:37 +0000116const gchar *
Richard Hughes12724852018-09-04 13:53:44 +0100117fu_plugin_get_name (FuPlugin *self)
Richard Hughesd0905142016-03-13 09:46:49 +0000118{
Richard Hughes12724852018-09-04 13:53:44 +0100119 FuPluginPrivate *priv = GET_PRIVATE (self);
120 g_return_val_if_fail (FU_IS_PLUGIN (self), NULL);
Richard Hughescff38bc2016-12-12 12:03:37 +0000121 return priv->name;
122}
Richard Hughesd0905142016-03-13 09:46:49 +0000123
Richard Hughes34834102017-11-21 21:55:00 +0000124void
Richard Hughes12724852018-09-04 13:53:44 +0100125fu_plugin_set_name (FuPlugin *self, const gchar *name)
Richard Hughes34834102017-11-21 21:55:00 +0000126{
Richard Hughes12724852018-09-04 13:53:44 +0100127 FuPluginPrivate *priv = GET_PRIVATE (self);
128 g_return_if_fail (FU_IS_PLUGIN (self));
Richard Hughes34834102017-11-21 21:55:00 +0000129 g_return_if_fail (name != NULL);
130 g_free (priv->name);
131 priv->name = g_strdup (name);
132}
133
Richard Hughes57d18222017-01-10 16:02:59 +0000134/**
135 * fu_plugin_cache_lookup:
Richard Hughes2c0635a2018-09-04 14:52:46 +0100136 * @self: A #FuPlugin
Richard Hughes57d18222017-01-10 16:02:59 +0000137 * @id: the key
138 *
139 * Finds an object in the per-plugin cache.
140 *
141 * Returns: (transfer none): a #GObject, or %NULL for unfound.
142 *
143 * Since: 0.8.0
144 **/
Richard Hughescff38bc2016-12-12 12:03:37 +0000145gpointer
Richard Hughes12724852018-09-04 13:53:44 +0100146fu_plugin_cache_lookup (FuPlugin *self, const gchar *id)
Richard Hughescff38bc2016-12-12 12:03:37 +0000147{
Richard Hughes12724852018-09-04 13:53:44 +0100148 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughes37d09432018-09-09 10:39:45 +0100149 g_autoptr(FuMutexLocker) locker = fu_mutex_read_locker_new (priv->devices_mutex);
Richard Hughes12724852018-09-04 13:53:44 +0100150 g_return_val_if_fail (FU_IS_PLUGIN (self), NULL);
Richard Hughesccd78a92017-01-11 16:57:41 +0000151 g_return_val_if_fail (id != NULL, NULL);
Richard Hughes37d09432018-09-09 10:39:45 +0100152 g_return_val_if_fail (locker != NULL, NULL);
Richard Hughescff38bc2016-12-12 12:03:37 +0000153 return g_hash_table_lookup (priv->devices, id);
154}
Richard Hughesd0905142016-03-13 09:46:49 +0000155
Richard Hughes57d18222017-01-10 16:02:59 +0000156/**
157 * fu_plugin_cache_add:
Richard Hughes2c0635a2018-09-04 14:52:46 +0100158 * @self: A #FuPlugin
Richard Hughes57d18222017-01-10 16:02:59 +0000159 * @id: the key
160 * @dev: a #GObject, typically a #FuDevice
161 *
162 * Adds an object to the per-plugin cache.
163 *
164 * Since: 0.8.0
165 **/
Richard Hughescff38bc2016-12-12 12:03:37 +0000166void
Richard Hughes12724852018-09-04 13:53:44 +0100167fu_plugin_cache_add (FuPlugin *self, const gchar *id, gpointer dev)
Richard Hughescff38bc2016-12-12 12:03:37 +0000168{
Richard Hughes12724852018-09-04 13:53:44 +0100169 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughes37d09432018-09-09 10:39:45 +0100170 g_autoptr(FuMutexLocker) locker = fu_mutex_write_locker_new (priv->devices_mutex);
Richard Hughes12724852018-09-04 13:53:44 +0100171 g_return_if_fail (FU_IS_PLUGIN (self));
Richard Hughesccd78a92017-01-11 16:57:41 +0000172 g_return_if_fail (id != NULL);
Richard Hughes37d09432018-09-09 10:39:45 +0100173 g_return_if_fail (locker != NULL);
Richard Hughescff38bc2016-12-12 12:03:37 +0000174 g_hash_table_insert (priv->devices, g_strdup (id), g_object_ref (dev));
175}
176
Richard Hughes57d18222017-01-10 16:02:59 +0000177/**
178 * fu_plugin_cache_remove:
Richard Hughes2c0635a2018-09-04 14:52:46 +0100179 * @self: A #FuPlugin
Richard Hughes57d18222017-01-10 16:02:59 +0000180 * @id: the key
181 *
182 * Removes an object from the per-plugin cache.
183 *
184 * Since: 0.8.0
185 **/
Richard Hughescff38bc2016-12-12 12:03:37 +0000186void
Richard Hughes12724852018-09-04 13:53:44 +0100187fu_plugin_cache_remove (FuPlugin *self, const gchar *id)
Richard Hughescff38bc2016-12-12 12:03:37 +0000188{
Richard Hughes12724852018-09-04 13:53:44 +0100189 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughes37d09432018-09-09 10:39:45 +0100190 g_autoptr(FuMutexLocker) locker = fu_mutex_write_locker_new (priv->devices_mutex);
Richard Hughes12724852018-09-04 13:53:44 +0100191 g_return_if_fail (FU_IS_PLUGIN (self));
Richard Hughesccd78a92017-01-11 16:57:41 +0000192 g_return_if_fail (id != NULL);
Richard Hughes37d09432018-09-09 10:39:45 +0100193 g_return_if_fail (locker != NULL);
Richard Hughescff38bc2016-12-12 12:03:37 +0000194 g_hash_table_remove (priv->devices, id);
195}
196
Richard Hughes57d18222017-01-10 16:02:59 +0000197/**
198 * fu_plugin_get_data:
Richard Hughes2c0635a2018-09-04 14:52:46 +0100199 * @self: A #FuPlugin
Richard Hughes57d18222017-01-10 16:02:59 +0000200 *
Richard Hughes4eada342017-10-03 21:20:32 +0100201 * Gets the per-plugin allocated private data. This will return %NULL unless
202 * fu_plugin_alloc_data() has been called by the plugin.
Richard Hughes57d18222017-01-10 16:02:59 +0000203 *
Richard Hughes4eada342017-10-03 21:20:32 +0100204 * Returns: (transfer none): a pointer to a structure, or %NULL for unset.
Richard Hughes57d18222017-01-10 16:02:59 +0000205 *
206 * Since: 0.8.0
207 **/
Richard Hughescff38bc2016-12-12 12:03:37 +0000208FuPluginData *
Richard Hughes12724852018-09-04 13:53:44 +0100209fu_plugin_get_data (FuPlugin *self)
Richard Hughescff38bc2016-12-12 12:03:37 +0000210{
Richard Hughes12724852018-09-04 13:53:44 +0100211 FuPluginPrivate *priv = GET_PRIVATE (self);
212 g_return_val_if_fail (FU_IS_PLUGIN (self), NULL);
Richard Hughescff38bc2016-12-12 12:03:37 +0000213 return priv->data;
214}
215
Richard Hughes57d18222017-01-10 16:02:59 +0000216/**
217 * fu_plugin_alloc_data:
Richard Hughes2c0635a2018-09-04 14:52:46 +0100218 * @self: A #FuPlugin
Richard Hughes57d18222017-01-10 16:02:59 +0000219 * @data_sz: the size to allocate
220 *
221 * Allocates the per-plugin allocated private data.
222 *
223 * Returns: (transfer full): a pointer to a structure, or %NULL for unset.
224 *
225 * Since: 0.8.0
226 **/
Richard Hughescff38bc2016-12-12 12:03:37 +0000227FuPluginData *
Richard Hughes12724852018-09-04 13:53:44 +0100228fu_plugin_alloc_data (FuPlugin *self, gsize data_sz)
Richard Hughescff38bc2016-12-12 12:03:37 +0000229{
Richard Hughes12724852018-09-04 13:53:44 +0100230 FuPluginPrivate *priv = GET_PRIVATE (self);
231 g_return_val_if_fail (FU_IS_PLUGIN (self), NULL);
Richard Hughes44dee882017-01-11 08:31:10 +0000232 if (priv->data != NULL) {
233 g_critical ("fu_plugin_alloc_data() already used by plugin");
234 return priv->data;
235 }
Richard Hughescff38bc2016-12-12 12:03:37 +0000236 priv->data = g_malloc0 (data_sz);
237 return priv->data;
Richard Hughesd0905142016-03-13 09:46:49 +0000238}
239
Richard Hughes57d18222017-01-10 16:02:59 +0000240/**
241 * fu_plugin_get_usb_context:
Richard Hughes2c0635a2018-09-04 14:52:46 +0100242 * @self: A #FuPlugin
Richard Hughes57d18222017-01-10 16:02:59 +0000243 *
244 * Gets the shared USB context that all plugins can use.
245 *
246 * Returns: (transfer none): a #GUsbContext.
247 *
248 * Since: 0.8.0
249 **/
Richard Hughesbc93e4a2016-12-08 17:29:51 +0000250GUsbContext *
Richard Hughes12724852018-09-04 13:53:44 +0100251fu_plugin_get_usb_context (FuPlugin *self)
Richard Hughesbc93e4a2016-12-08 17:29:51 +0000252{
Richard Hughes12724852018-09-04 13:53:44 +0100253 FuPluginPrivate *priv = GET_PRIVATE (self);
254 g_return_val_if_fail (FU_IS_PLUGIN (self), NULL);
Richard Hughescff38bc2016-12-12 12:03:37 +0000255 return priv->usb_ctx;
Richard Hughesbc93e4a2016-12-08 17:29:51 +0000256}
257
258void
Richard Hughes12724852018-09-04 13:53:44 +0100259fu_plugin_set_usb_context (FuPlugin *self, GUsbContext *usb_ctx)
Richard Hughesbc93e4a2016-12-08 17:29:51 +0000260{
Richard Hughes12724852018-09-04 13:53:44 +0100261 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughescff38bc2016-12-12 12:03:37 +0000262 g_set_object (&priv->usb_ctx, usb_ctx);
263}
264
Richard Hughes57d18222017-01-10 16:02:59 +0000265/**
266 * fu_plugin_get_enabled:
Richard Hughes2c0635a2018-09-04 14:52:46 +0100267 * @self: A #FuPlugin
Richard Hughes57d18222017-01-10 16:02:59 +0000268 *
Richard Hughes4eada342017-10-03 21:20:32 +0100269 * Returns if the plugin is enabled. Plugins may self-disable using
270 * fu_plugin_set_enabled() or can be disabled by the daemon.
Richard Hughes57d18222017-01-10 16:02:59 +0000271 *
272 * Returns: %TRUE if the plugin is currently enabled.
273 *
274 * Since: 0.8.0
275 **/
Richard Hughescff38bc2016-12-12 12:03:37 +0000276gboolean
Richard Hughes12724852018-09-04 13:53:44 +0100277fu_plugin_get_enabled (FuPlugin *self)
Richard Hughescff38bc2016-12-12 12:03:37 +0000278{
Richard Hughes12724852018-09-04 13:53:44 +0100279 FuPluginPrivate *priv = GET_PRIVATE (self);
280 g_return_val_if_fail (FU_IS_PLUGIN (self), FALSE);
Richard Hughescff38bc2016-12-12 12:03:37 +0000281 return priv->enabled;
Richard Hughesbc93e4a2016-12-08 17:29:51 +0000282}
283
Richard Hughes57d18222017-01-10 16:02:59 +0000284/**
285 * fu_plugin_set_enabled:
Richard Hughes2c0635a2018-09-04 14:52:46 +0100286 * @self: A #FuPlugin
Richard Hughes57d18222017-01-10 16:02:59 +0000287 * @enabled: the enabled value
288 *
289 * Enables or disables a plugin. Plugins can self-disable at any point.
290 *
291 * Since: 0.8.0
292 **/
Richard Hughesd0905142016-03-13 09:46:49 +0000293void
Richard Hughes12724852018-09-04 13:53:44 +0100294fu_plugin_set_enabled (FuPlugin *self, gboolean enabled)
Richard Hughesd0905142016-03-13 09:46:49 +0000295{
Richard Hughes12724852018-09-04 13:53:44 +0100296 FuPluginPrivate *priv = GET_PRIVATE (self);
297 g_return_if_fail (FU_IS_PLUGIN (self));
Richard Hughescff38bc2016-12-12 12:03:37 +0000298 priv->enabled = enabled;
299}
300
Richard Hughes1e456bc2018-05-10 20:16:16 +0100301gchar *
302fu_plugin_guess_name_from_fn (const gchar *filename)
303{
304 const gchar *prefix = "libfu_plugin_";
305 gchar *name;
306 gchar *str = g_strstr_len (filename, -1, prefix);
307 if (str == NULL)
308 return NULL;
309 name = g_strdup (str + strlen (prefix));
310 g_strdelimit (name, ".", '\0');
311 return name;
312}
313
Richard Hughescff38bc2016-12-12 12:03:37 +0000314gboolean
Richard Hughes12724852018-09-04 13:53:44 +0100315fu_plugin_open (FuPlugin *self, const gchar *filename, GError **error)
Richard Hughescff38bc2016-12-12 12:03:37 +0000316{
Richard Hughes12724852018-09-04 13:53:44 +0100317 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughescff38bc2016-12-12 12:03:37 +0000318 FuPluginInitFunc func = NULL;
Richard Hughescff38bc2016-12-12 12:03:37 +0000319
320 priv->module = g_module_open (filename, 0);
321 if (priv->module == NULL) {
322 g_set_error (error,
323 G_IO_ERROR,
324 G_IO_ERROR_FAILED,
325 "failed to open plugin: %s",
326 g_module_error ());
327 return FALSE;
328 }
329
330 /* set automatically */
Richard Hughes1e456bc2018-05-10 20:16:16 +0100331 if (priv->name == NULL)
332 priv->name = fu_plugin_guess_name_from_fn (filename);
Richard Hughesd0905142016-03-13 09:46:49 +0000333
334 /* optional */
Richard Hughescff38bc2016-12-12 12:03:37 +0000335 g_module_symbol (priv->module, "fu_plugin_init", (gpointer *) &func);
336 if (func != NULL) {
337 g_debug ("performing init() on %s", filename);
Richard Hughes12724852018-09-04 13:53:44 +0100338 func (self);
Richard Hughesd0905142016-03-13 09:46:49 +0000339 }
340
Richard Hughescff38bc2016-12-12 12:03:37 +0000341 return TRUE;
342}
343
Richard Hughes57d18222017-01-10 16:02:59 +0000344/**
345 * fu_plugin_device_add:
Richard Hughes2c0635a2018-09-04 14:52:46 +0100346 * @self: A #FuPlugin
Richard Hughes57d18222017-01-10 16:02:59 +0000347 * @device: A #FuDevice
348 *
349 * Asks the daemon to add a device to the exported list. If this device ID
350 * has already been added by a different plugin then this request will be
351 * ignored.
352 *
353 * Plugins should use fu_plugin_device_add_delay() if they are not capable of
354 * actually flashing an image to the hardware so that higher-priority plugins
355 * can add the device themselves.
356 *
357 * Since: 0.8.0
358 **/
Richard Hughescff38bc2016-12-12 12:03:37 +0000359void
Richard Hughes12724852018-09-04 13:53:44 +0100360fu_plugin_device_add (FuPlugin *self, FuDevice *device)
Richard Hughescff38bc2016-12-12 12:03:37 +0000361{
Richard Hughes5e447292018-04-27 14:25:54 +0100362 GPtrArray *children;
Richard Hughesc125ec02018-09-05 19:35:17 +0100363 g_autoptr(GError) error = NULL;
Richard Hughes5e447292018-04-27 14:25:54 +0100364
Richard Hughes12724852018-09-04 13:53:44 +0100365 g_return_if_fail (FU_IS_PLUGIN (self));
Richard Hughesccd78a92017-01-11 16:57:41 +0000366 g_return_if_fail (FU_IS_DEVICE (device));
367
Richard Hughesc125ec02018-09-05 19:35:17 +0100368 /* ensure the device ID is set from the physical and logical IDs */
369 if (!fu_device_ensure_id (device, &error)) {
370 g_warning ("ignoring add: %s", error->message);
371 return;
372 }
373
Richard Hughescff38bc2016-12-12 12:03:37 +0000374 g_debug ("emit added from %s: %s",
Richard Hughes12724852018-09-04 13:53:44 +0100375 fu_plugin_get_name (self),
Richard Hughescff38bc2016-12-12 12:03:37 +0000376 fu_device_get_id (device));
377 fu_device_set_created (device, (guint64) g_get_real_time () / G_USEC_PER_SEC);
Richard Hughes12724852018-09-04 13:53:44 +0100378 fu_device_set_plugin (device, fu_plugin_get_name (self));
379 g_signal_emit (self, signals[SIGNAL_DEVICE_ADDED], 0, device);
Richard Hughes5e447292018-04-27 14:25:54 +0100380
Richard Hughes128c0162018-08-10 11:00:29 +0100381 /* add children if they have not already been added */
Richard Hughes5e447292018-04-27 14:25:54 +0100382 children = fu_device_get_children (device);
383 for (guint i = 0; i < children->len; i++) {
384 FuDevice *child = g_ptr_array_index (children, i);
Richard Hughes128c0162018-08-10 11:00:29 +0100385 if (fu_device_get_created (child) == 0)
Richard Hughes12724852018-09-04 13:53:44 +0100386 fu_plugin_device_add (self, child);
Richard Hughes5e447292018-04-27 14:25:54 +0100387 }
Richard Hughescff38bc2016-12-12 12:03:37 +0000388}
389
Richard Hughese1fd34d2017-08-24 14:19:51 +0100390/**
391 * fu_plugin_device_register:
Richard Hughes2c0635a2018-09-04 14:52:46 +0100392 * @self: A #FuPlugin
Richard Hughese1fd34d2017-08-24 14:19:51 +0100393 * @device: A #FuDevice
394 *
395 * Registers the device with other plugins so they can set metadata.
396 *
397 * Plugins do not have to call this manually as this is done automatically
398 * when using fu_plugin_device_add(). They may wish to use this manually
399 * if for intance the coldplug should be ignored based on the metadata
400 * set from other plugins.
401 *
402 * Since: 0.9.7
403 **/
404void
Richard Hughes12724852018-09-04 13:53:44 +0100405fu_plugin_device_register (FuPlugin *self, FuDevice *device)
Richard Hughese1fd34d2017-08-24 14:19:51 +0100406{
Richard Hughesc125ec02018-09-05 19:35:17 +0100407 g_autoptr(GError) error = NULL;
408
Richard Hughes12724852018-09-04 13:53:44 +0100409 g_return_if_fail (FU_IS_PLUGIN (self));
Richard Hughese1fd34d2017-08-24 14:19:51 +0100410 g_return_if_fail (FU_IS_DEVICE (device));
411
Richard Hughesc125ec02018-09-05 19:35:17 +0100412 /* ensure the device ID is set from the physical and logical IDs */
413 if (!fu_device_ensure_id (device, &error)) {
414 g_warning ("ignoring registration: %s", error->message);
415 return;
416 }
417
Richard Hughese1fd34d2017-08-24 14:19:51 +0100418 g_debug ("emit device-register from %s: %s",
Richard Hughes12724852018-09-04 13:53:44 +0100419 fu_plugin_get_name (self),
Richard Hughese1fd34d2017-08-24 14:19:51 +0100420 fu_device_get_id (device));
Richard Hughes12724852018-09-04 13:53:44 +0100421 g_signal_emit (self, signals[SIGNAL_DEVICE_REGISTER], 0, device);
Richard Hughese1fd34d2017-08-24 14:19:51 +0100422}
423
Richard Hughes57d18222017-01-10 16:02:59 +0000424/**
Richard Hughes4eada342017-10-03 21:20:32 +0100425 * fu_plugin_device_remove:
Richard Hughes2c0635a2018-09-04 14:52:46 +0100426 * @self: A #FuPlugin
Richard Hughes57d18222017-01-10 16:02:59 +0000427 * @device: A #FuDevice
428 *
429 * Asks the daemon to remove a device from the exported list.
430 *
431 * Since: 0.8.0
432 **/
Richard Hughescff38bc2016-12-12 12:03:37 +0000433void
Richard Hughes12724852018-09-04 13:53:44 +0100434fu_plugin_device_remove (FuPlugin *self, FuDevice *device)
Richard Hughescff38bc2016-12-12 12:03:37 +0000435{
Richard Hughes12724852018-09-04 13:53:44 +0100436 g_return_if_fail (FU_IS_PLUGIN (self));
Richard Hughesccd78a92017-01-11 16:57:41 +0000437 g_return_if_fail (FU_IS_DEVICE (device));
438
Richard Hughescff38bc2016-12-12 12:03:37 +0000439 g_debug ("emit removed from %s: %s",
Richard Hughes12724852018-09-04 13:53:44 +0100440 fu_plugin_get_name (self),
Richard Hughescff38bc2016-12-12 12:03:37 +0000441 fu_device_get_id (device));
Richard Hughes12724852018-09-04 13:53:44 +0100442 g_signal_emit (self, signals[SIGNAL_DEVICE_REMOVED], 0, device);
Richard Hughescff38bc2016-12-12 12:03:37 +0000443}
444
Richard Hughes57d18222017-01-10 16:02:59 +0000445/**
Richard Hughes2de8f132018-01-17 09:12:02 +0000446 * fu_plugin_request_recoldplug:
Richard Hughes2c0635a2018-09-04 14:52:46 +0100447 * @self: A #FuPlugin
Richard Hughes362d6d72017-01-07 21:42:14 +0000448 *
449 * Ask all the plugins to coldplug all devices, which will include the prepare()
450 * and cleanup() phases. Duplicate devices added will be ignored.
451 *
452 * Since: 0.8.0
453 **/
454void
Richard Hughes12724852018-09-04 13:53:44 +0100455fu_plugin_request_recoldplug (FuPlugin *self)
Richard Hughes362d6d72017-01-07 21:42:14 +0000456{
Richard Hughes12724852018-09-04 13:53:44 +0100457 g_return_if_fail (FU_IS_PLUGIN (self));
458 g_signal_emit (self, signals[SIGNAL_RECOLDPLUG], 0);
Richard Hughes362d6d72017-01-07 21:42:14 +0000459}
460
Richard Hughesb0829032017-01-10 09:27:08 +0000461/**
Richard Hughesb8f8db22017-04-25 15:56:00 +0100462 * fu_plugin_check_hwid:
Richard Hughes2c0635a2018-09-04 14:52:46 +0100463 * @self: A #FuPlugin
Richard Hughes4eada342017-10-03 21:20:32 +0100464 * @hwid: A Hardware ID GUID, e.g. `6de5d951-d755-576b-bd09-c5cf66b27234`
Richard Hughesb8f8db22017-04-25 15:56:00 +0100465 *
Richard Hughesd7704d42017-08-08 20:29:09 +0100466 * Checks to see if a specific GUID exists. All hardware IDs on a
Richard Hughesb8f8db22017-04-25 15:56:00 +0100467 * specific system can be shown using the `fwupdmgr hwids` command.
468 *
Richard Hughes4eada342017-10-03 21:20:32 +0100469 * Returns: %TRUE if the HwId is found on the system.
470 *
Richard Hughesb8f8db22017-04-25 15:56:00 +0100471 * Since: 0.9.1
472 **/
473gboolean
Richard Hughes12724852018-09-04 13:53:44 +0100474fu_plugin_check_hwid (FuPlugin *self, const gchar *hwid)
Richard Hughesb8f8db22017-04-25 15:56:00 +0100475{
Richard Hughes12724852018-09-04 13:53:44 +0100476 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughesb8f8db22017-04-25 15:56:00 +0100477 if (priv->hwids == NULL)
478 return FALSE;
Richard Hughesd7704d42017-08-08 20:29:09 +0100479 return fu_hwids_has_guid (priv->hwids, hwid);
480}
481
482/**
Richard Hughes69a5f352018-08-08 11:58:15 +0100483 * fu_plugin_get_hwids:
Richard Hughes2c0635a2018-09-04 14:52:46 +0100484 * @self: A #FuPlugin
Richard Hughes69a5f352018-08-08 11:58:15 +0100485 *
486 * Returns all the HWIDs defined in the system. All hardware IDs on a
487 * specific system can be shown using the `fwupdmgr hwids` command.
488 *
489 * Returns: (transfer none) (element-type utf-8): An array of GUIDs
490 *
491 * Since: 1.1.1
492 **/
493GPtrArray *
Richard Hughes12724852018-09-04 13:53:44 +0100494fu_plugin_get_hwids (FuPlugin *self)
Richard Hughes69a5f352018-08-08 11:58:15 +0100495{
Richard Hughes12724852018-09-04 13:53:44 +0100496 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughes69a5f352018-08-08 11:58:15 +0100497 if (priv->hwids == NULL)
498 return NULL;
499 return fu_hwids_get_guids (priv->hwids);
500}
501
502/**
Richard Hughes1354ea92017-09-19 15:58:31 +0100503 * fu_plugin_check_supported:
Richard Hughes2c0635a2018-09-04 14:52:46 +0100504 * @self: A #FuPlugin
Richard Hughes4eada342017-10-03 21:20:32 +0100505 * @guid: A Hardware ID GUID, e.g. `6de5d951-d755-576b-bd09-c5cf66b27234`
Richard Hughes1354ea92017-09-19 15:58:31 +0100506 *
507 * Checks to see if a specific device GUID is supported, i.e. available in the
508 * AppStream metadata.
509 *
Richard Hughes4eada342017-10-03 21:20:32 +0100510 * Returns: %TRUE if the device is supported.
511 *
Richard Hughes1354ea92017-09-19 15:58:31 +0100512 * Since: 1.0.0
513 **/
514gboolean
Richard Hughes12724852018-09-04 13:53:44 +0100515fu_plugin_check_supported (FuPlugin *self, const gchar *guid)
Richard Hughes1354ea92017-09-19 15:58:31 +0100516{
Richard Hughesaabdc372018-11-14 10:11:08 +0000517 gboolean retval = FALSE;
518 g_signal_emit (self, signals[SIGNAL_CHECK_SUPPORTED], 0, guid, &retval);
519 return retval;
Richard Hughes1354ea92017-09-19 15:58:31 +0100520}
521
522/**
Richard Hughesd7704d42017-08-08 20:29:09 +0100523 * fu_plugin_get_dmi_value:
Richard Hughes2c0635a2018-09-04 14:52:46 +0100524 * @self: A #FuPlugin
Richard Hughes4eada342017-10-03 21:20:32 +0100525 * @dmi_id: A DMI ID, e.g. `BiosVersion`
Richard Hughesd7704d42017-08-08 20:29:09 +0100526 *
527 * Gets a hardware DMI value.
528 *
Richard Hughes4eada342017-10-03 21:20:32 +0100529 * Returns: The string, or %NULL
530 *
Richard Hughesd7704d42017-08-08 20:29:09 +0100531 * Since: 0.9.7
532 **/
533const gchar *
Richard Hughes12724852018-09-04 13:53:44 +0100534fu_plugin_get_dmi_value (FuPlugin *self, const gchar *dmi_id)
Richard Hughesd7704d42017-08-08 20:29:09 +0100535{
Richard Hughes12724852018-09-04 13:53:44 +0100536 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughesd7704d42017-08-08 20:29:09 +0100537 if (priv->hwids == NULL)
Richard Hughes7ef96b82017-08-23 18:28:24 +0100538 return NULL;
Richard Hughesd7704d42017-08-08 20:29:09 +0100539 return fu_hwids_get_value (priv->hwids, dmi_id);
Richard Hughesb8f8db22017-04-25 15:56:00 +0100540}
541
Richard Hughes49e5e052017-09-03 12:15:41 +0100542/**
543 * fu_plugin_get_smbios_string:
Richard Hughes2c0635a2018-09-04 14:52:46 +0100544 * @self: A #FuPlugin
Richard Hughes49e5e052017-09-03 12:15:41 +0100545 * @structure_type: A SMBIOS structure type, e.g. %FU_SMBIOS_STRUCTURE_TYPE_BIOS
546 * @offset: A SMBIOS offset
547 *
548 * Gets a hardware SMBIOS string.
549 *
550 * The @type and @offset can be referenced from the DMTF SMBIOS specification:
551 * https://www.dmtf.org/sites/default/files/standards/documents/DSP0134_3.1.1.pdf
552 *
Richard Hughes4eada342017-10-03 21:20:32 +0100553 * Returns: A string, or %NULL
554 *
Richard Hughes49e5e052017-09-03 12:15:41 +0100555 * Since: 0.9.8
556 **/
557const gchar *
Richard Hughes12724852018-09-04 13:53:44 +0100558fu_plugin_get_smbios_string (FuPlugin *self, guint8 structure_type, guint8 offset)
Richard Hughes49e5e052017-09-03 12:15:41 +0100559{
Richard Hughes12724852018-09-04 13:53:44 +0100560 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughes49e5e052017-09-03 12:15:41 +0100561 if (priv->smbios == NULL)
562 return NULL;
563 return fu_smbios_get_string (priv->smbios, structure_type, offset, NULL);
564}
565
566/**
567 * fu_plugin_get_smbios_data:
Richard Hughes2c0635a2018-09-04 14:52:46 +0100568 * @self: A #FuPlugin
Richard Hughes49e5e052017-09-03 12:15:41 +0100569 * @structure_type: A SMBIOS structure type, e.g. %FU_SMBIOS_STRUCTURE_TYPE_BIOS
570 *
571 * Gets a hardware SMBIOS data.
572 *
Richard Hughes4eada342017-10-03 21:20:32 +0100573 * Returns: (transfer none): A #GBytes, or %NULL
574 *
Richard Hughes49e5e052017-09-03 12:15:41 +0100575 * Since: 0.9.8
576 **/
577GBytes *
Richard Hughes12724852018-09-04 13:53:44 +0100578fu_plugin_get_smbios_data (FuPlugin *self, guint8 structure_type)
Richard Hughes49e5e052017-09-03 12:15:41 +0100579{
Richard Hughes12724852018-09-04 13:53:44 +0100580 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughes49e5e052017-09-03 12:15:41 +0100581 if (priv->smbios == NULL)
582 return NULL;
583 return fu_smbios_get_data (priv->smbios, structure_type, NULL);
584}
585
Richard Hughesb8f8db22017-04-25 15:56:00 +0100586void
Richard Hughes12724852018-09-04 13:53:44 +0100587fu_plugin_set_hwids (FuPlugin *self, FuHwids *hwids)
Richard Hughesb8f8db22017-04-25 15:56:00 +0100588{
Richard Hughes12724852018-09-04 13:53:44 +0100589 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughesd7704d42017-08-08 20:29:09 +0100590 g_set_object (&priv->hwids, hwids);
Richard Hughesb8f8db22017-04-25 15:56:00 +0100591}
592
Richard Hughes49e5e052017-09-03 12:15:41 +0100593void
Richard Hughes12724852018-09-04 13:53:44 +0100594fu_plugin_set_udev_subsystems (FuPlugin *self, GPtrArray *udev_subsystems)
Richard Hughes9d6e0e72018-08-24 20:20:17 +0100595{
Richard Hughes12724852018-09-04 13:53:44 +0100596 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughes9d6e0e72018-08-24 20:20:17 +0100597 if (priv->udev_subsystems != NULL)
598 g_ptr_array_unref (priv->udev_subsystems);
599 priv->udev_subsystems = g_ptr_array_ref (udev_subsystems);
600}
601
602void
Richard Hughes12724852018-09-04 13:53:44 +0100603fu_plugin_set_quirks (FuPlugin *self, FuQuirks *quirks)
Richard Hughes9c028f02017-10-28 21:14:28 +0100604{
Richard Hughes12724852018-09-04 13:53:44 +0100605 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughes9c028f02017-10-28 21:14:28 +0100606 g_set_object (&priv->quirks, quirks);
607}
608
609/**
610 * fu_plugin_get_quirks:
Richard Hughes2c0635a2018-09-04 14:52:46 +0100611 * @self: A #FuPlugin
Richard Hughes9c028f02017-10-28 21:14:28 +0100612 *
613 * Returns the hardware database object. This can be used to discover device
614 * quirks or other device-specific settings.
615 *
616 * Returns: (transfer none): a #FuQuirks, or %NULL if not set
617 *
618 * Since: 1.0.1
619 **/
620FuQuirks *
Richard Hughes12724852018-09-04 13:53:44 +0100621fu_plugin_get_quirks (FuPlugin *self)
Richard Hughes9c028f02017-10-28 21:14:28 +0100622{
Richard Hughes12724852018-09-04 13:53:44 +0100623 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughes9c028f02017-10-28 21:14:28 +0100624 return priv->quirks;
625}
626
Richard Hughes0eb123b2018-04-19 12:00:04 +0100627void
Richard Hughes12724852018-09-04 13:53:44 +0100628fu_plugin_set_runtime_versions (FuPlugin *self, GHashTable *runtime_versions)
Richard Hughes0eb123b2018-04-19 12:00:04 +0100629{
Richard Hughes12724852018-09-04 13:53:44 +0100630 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughes0eb123b2018-04-19 12:00:04 +0100631 priv->runtime_versions = g_hash_table_ref (runtime_versions);
632}
633
634/**
635 * fu_plugin_add_runtime_version:
Richard Hughes2c0635a2018-09-04 14:52:46 +0100636 * @self: A #FuPlugin
Richard Hughes0eb123b2018-04-19 12:00:04 +0100637 * @component_id: An AppStream component id, e.g. "org.gnome.Software"
638 * @version: A version string, e.g. "1.2.3"
639 *
640 * Sets a runtime version of a specific dependancy.
641 *
642 * Since: 1.0.7
643 **/
644void
Richard Hughes12724852018-09-04 13:53:44 +0100645fu_plugin_add_runtime_version (FuPlugin *self,
Richard Hughes0eb123b2018-04-19 12:00:04 +0100646 const gchar *component_id,
647 const gchar *version)
648{
Richard Hughes12724852018-09-04 13:53:44 +0100649 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughesb01b4862018-04-20 16:39:48 +0100650 if (priv->runtime_versions == NULL)
651 return;
Richard Hughes0eb123b2018-04-19 12:00:04 +0100652 g_hash_table_insert (priv->runtime_versions,
653 g_strdup (component_id),
654 g_strdup (version));
655}
656
Richard Hughes34e0dab2018-04-20 16:43:00 +0100657void
Richard Hughes12724852018-09-04 13:53:44 +0100658fu_plugin_set_compile_versions (FuPlugin *self, GHashTable *compile_versions)
Richard Hughes34e0dab2018-04-20 16:43:00 +0100659{
Richard Hughes12724852018-09-04 13:53:44 +0100660 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughes34e0dab2018-04-20 16:43:00 +0100661 priv->compile_versions = g_hash_table_ref (compile_versions);
662}
663
664/**
665 * fu_plugin_add_compile_version:
Richard Hughes2c0635a2018-09-04 14:52:46 +0100666 * @self: A #FuPlugin
Richard Hughes34e0dab2018-04-20 16:43:00 +0100667 * @component_id: An AppStream component id, e.g. "org.gnome.Software"
668 * @version: A version string, e.g. "1.2.3"
669 *
670 * Sets a compile-time version of a specific dependancy.
671 *
672 * Since: 1.0.7
673 **/
674void
Richard Hughes12724852018-09-04 13:53:44 +0100675fu_plugin_add_compile_version (FuPlugin *self,
Richard Hughes34e0dab2018-04-20 16:43:00 +0100676 const gchar *component_id,
677 const gchar *version)
678{
Richard Hughes12724852018-09-04 13:53:44 +0100679 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughes34e0dab2018-04-20 16:43:00 +0100680 if (priv->compile_versions == NULL)
681 return;
682 g_hash_table_insert (priv->compile_versions,
683 g_strdup (component_id),
684 g_strdup (version));
685}
686
Richard Hughes9c028f02017-10-28 21:14:28 +0100687/**
688 * fu_plugin_lookup_quirk_by_id:
Richard Hughes2c0635a2018-09-04 14:52:46 +0100689 * @self: A #FuPlugin
Richard Hughes87fb9ff2018-06-28 12:55:59 +0100690 * @group: A string, e.g. "DfuFlags"
691 * @key: An ID to match the entry, e.g. "Summary"
Richard Hughes9c028f02017-10-28 21:14:28 +0100692 *
693 * Looks up an entry in the hardware database using a string value.
694 *
695 * Returns: (transfer none): values from the database, or %NULL if not found
696 *
697 * Since: 1.0.1
698 **/
699const gchar *
Richard Hughes12724852018-09-04 13:53:44 +0100700fu_plugin_lookup_quirk_by_id (FuPlugin *self, const gchar *group, const gchar *key)
Richard Hughes9c028f02017-10-28 21:14:28 +0100701{
Richard Hughes12724852018-09-04 13:53:44 +0100702 FuPluginPrivate *priv = GET_PRIVATE (self);
703 g_return_val_if_fail (FU_IS_PLUGIN (self), NULL);
Richard Hughes9c028f02017-10-28 21:14:28 +0100704
Richard Hughes9c028f02017-10-28 21:14:28 +0100705 /* exact ID */
Richard Hughes87fb9ff2018-06-28 12:55:59 +0100706 return fu_quirks_lookup_by_id (priv->quirks, group, key);
Richard Hughes9c028f02017-10-28 21:14:28 +0100707}
708
709/**
Richard Hughes8fe7cdd2018-08-23 10:02:44 +0100710 * fu_plugin_lookup_quirk_by_id_as_uint64:
Richard Hughes2c0635a2018-09-04 14:52:46 +0100711 * @self: A #FuPlugin
Richard Hughes8fe7cdd2018-08-23 10:02:44 +0100712 * @group: A string, e.g. "DfuFlags"
713 * @key: An ID to match the entry, e.g. "Size"
714 *
715 * Looks up an entry in the hardware database using a string key, returning
716 * an integer value. Values are assumed base 10, unless prefixed with "0x"
717 * where they are parsed as base 16.
718 *
719 * Returns: (transfer none): value from the database, or 0 if not found
720 *
721 * Since: 1.1.2
722 **/
723guint64
Richard Hughes12724852018-09-04 13:53:44 +0100724fu_plugin_lookup_quirk_by_id_as_uint64 (FuPlugin *self, const gchar *group, const gchar *key)
Richard Hughes8fe7cdd2018-08-23 10:02:44 +0100725{
Richard Hughes12724852018-09-04 13:53:44 +0100726 return fu_common_strtoull (fu_plugin_lookup_quirk_by_id (self, group, key));
Richard Hughes8fe7cdd2018-08-23 10:02:44 +0100727}
728
Richard Hughes1354ea92017-09-19 15:58:31 +0100729void
Richard Hughes12724852018-09-04 13:53:44 +0100730fu_plugin_set_smbios (FuPlugin *self, FuSmbios *smbios)
Richard Hughes49e5e052017-09-03 12:15:41 +0100731{
Richard Hughes12724852018-09-04 13:53:44 +0100732 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughes49e5e052017-09-03 12:15:41 +0100733 g_set_object (&priv->smbios, smbios);
734}
735
Richard Hughesb8f8db22017-04-25 15:56:00 +0100736/**
Richard Hughesb0829032017-01-10 09:27:08 +0000737 * fu_plugin_set_coldplug_delay:
Richard Hughes2c0635a2018-09-04 14:52:46 +0100738 * @self: A #FuPlugin
Richard Hughesb0829032017-01-10 09:27:08 +0000739 * @duration: A delay in milliseconds
740 *
741 * Set the minimum time that should be waited inbetween the call to
742 * fu_plugin_coldplug_prepare() and fu_plugin_coldplug(). This is usually going
743 * to be the minimum hardware initialisation time from a datasheet.
744 *
745 * It is better to use this function rather than using a sleep() in the plugin
746 * itself as then only one delay is done in the daemon rather than waiting for
747 * each coldplug prepare in a serial way.
748 *
749 * Additionally, very long delays should be avoided as the daemon will be
750 * blocked from processing requests whilst the coldplug delay is being
751 * performed.
752 *
753 * Since: 0.8.0
754 **/
755void
Richard Hughes12724852018-09-04 13:53:44 +0100756fu_plugin_set_coldplug_delay (FuPlugin *self, guint duration)
Richard Hughesb0829032017-01-10 09:27:08 +0000757{
Richard Hughes12724852018-09-04 13:53:44 +0100758 g_return_if_fail (FU_IS_PLUGIN (self));
Richard Hughesb0829032017-01-10 09:27:08 +0000759 g_return_if_fail (duration > 0);
760
761 /* check sanity */
762 if (duration > FU_PLUGIN_COLDPLUG_DELAY_MAXIMUM) {
763 g_warning ("duration of %ums is crazy, truncating to %ums",
764 duration,
765 FU_PLUGIN_COLDPLUG_DELAY_MAXIMUM);
766 duration = FU_PLUGIN_COLDPLUG_DELAY_MAXIMUM;
767 }
768
769 /* emit */
Richard Hughes12724852018-09-04 13:53:44 +0100770 g_signal_emit (self, signals[SIGNAL_SET_COLDPLUG_DELAY], 0, duration);
Richard Hughesb0829032017-01-10 09:27:08 +0000771}
772
Richard Hughesd0905142016-03-13 09:46:49 +0000773gboolean
Richard Hughes12724852018-09-04 13:53:44 +0100774fu_plugin_runner_startup (FuPlugin *self, GError **error)
Richard Hughesd0905142016-03-13 09:46:49 +0000775{
Richard Hughes12724852018-09-04 13:53:44 +0100776 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughes7b8b2022016-12-12 16:15:03 +0000777 FuPluginStartupFunc func = NULL;
Richard Hughesd0905142016-03-13 09:46:49 +0000778
779 /* not enabled */
Richard Hughescff38bc2016-12-12 12:03:37 +0000780 if (!priv->enabled)
Richard Hughesd0905142016-03-13 09:46:49 +0000781 return TRUE;
782
Richard Hughes639da472018-01-06 22:35:04 +0000783 /* no object loaded */
784 if (priv->module == NULL)
785 return TRUE;
786
Richard Hughesd0905142016-03-13 09:46:49 +0000787 /* optional */
Richard Hughescff38bc2016-12-12 12:03:37 +0000788 g_module_symbol (priv->module, "fu_plugin_startup", (gpointer *) &func);
789 if (func == NULL)
Richard Hughesd0905142016-03-13 09:46:49 +0000790 return TRUE;
Richard Hughescff38bc2016-12-12 12:03:37 +0000791 g_debug ("performing startup() on %s", priv->name);
Richard Hughes12724852018-09-04 13:53:44 +0100792 if (!func (self, error)) {
Richard Hughescff38bc2016-12-12 12:03:37 +0000793 g_prefix_error (error, "failed to startup %s: ", priv->name);
794 return FALSE;
795 }
796 return TRUE;
797}
798
799static gboolean
800fu_plugin_runner_offline_invalidate (GError **error)
801{
802 g_autoptr(GError) error_local = NULL;
803 g_autoptr(GFile) file1 = NULL;
804
805 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
806
807 file1 = g_file_new_for_path (FU_OFFLINE_TRIGGER_FILENAME);
808 if (!g_file_query_exists (file1, NULL))
809 return TRUE;
810 if (!g_file_delete (file1, NULL, &error_local)) {
811 g_set_error (error,
812 FWUPD_ERROR,
813 FWUPD_ERROR_INTERNAL,
814 "Cannot delete %s: %s",
815 FU_OFFLINE_TRIGGER_FILENAME,
816 error_local->message);
817 return FALSE;
818 }
819 return TRUE;
820}
821
822static gboolean
823fu_plugin_runner_offline_setup (GError **error)
824{
825 gint rc;
826
827 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
828
829 /* create symlink for the systemd-system-update-generator */
830 rc = symlink ("/var/lib/fwupd", FU_OFFLINE_TRIGGER_FILENAME);
831 if (rc < 0) {
832 g_set_error (error,
833 FWUPD_ERROR,
834 FWUPD_ERROR_INTERNAL,
835 "Failed to create symlink %s to %s: %s",
836 FU_OFFLINE_TRIGGER_FILENAME,
837 "/var/lib", strerror (errno));
Richard Hughesd0905142016-03-13 09:46:49 +0000838 return FALSE;
839 }
840 return TRUE;
841}
842
Richard Hughes0d7fdb32017-11-11 20:23:14 +0000843static gboolean
Richard Hughes12724852018-09-04 13:53:44 +0100844fu_plugin_runner_device_generic (FuPlugin *self, FuDevice *device,
Richard Hughes0d7fdb32017-11-11 20:23:14 +0000845 const gchar *symbol_name, GError **error)
846{
Richard Hughes12724852018-09-04 13:53:44 +0100847 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughes0d7fdb32017-11-11 20:23:14 +0000848 FuPluginDeviceFunc func = NULL;
849
850 /* not enabled */
851 if (!priv->enabled)
852 return TRUE;
853
Richard Hughesd3d96cc2017-11-14 11:34:33 +0000854 /* no object loaded */
855 if (priv->module == NULL)
856 return TRUE;
857
Richard Hughes0d7fdb32017-11-11 20:23:14 +0000858 /* optional */
859 g_module_symbol (priv->module, symbol_name, (gpointer *) &func);
860 if (func == NULL)
861 return TRUE;
862 g_debug ("performing %s() on %s", symbol_name + 10, priv->name);
Richard Hughes12724852018-09-04 13:53:44 +0100863 if (!func (self, device, error)) {
Richard Hughes83e54e42018-01-31 23:27:30 +0000864 g_prefix_error (error, "failed to run %s() on %s: ",
Richard Hughes0d7fdb32017-11-11 20:23:14 +0000865 symbol_name + 10,
Richard Hughes0d7fdb32017-11-11 20:23:14 +0000866 priv->name);
867 return FALSE;
868 }
869 return TRUE;
870}
871
Richard Hughesdbd8c762018-06-15 20:31:40 +0100872static gboolean
Richard Hughes12724852018-09-04 13:53:44 +0100873fu_plugin_runner_flagged_device_generic (FuPlugin *self, FwupdInstallFlags flags,
Mario Limoncielloe3b1a3f2018-08-21 13:01:45 -0500874 FuDevice *device,
875 const gchar *symbol_name, GError **error)
876{
Richard Hughes12724852018-09-04 13:53:44 +0100877 FuPluginPrivate *priv = GET_PRIVATE (self);
Mario Limoncielloe3b1a3f2018-08-21 13:01:45 -0500878 FuPluginFlaggedDeviceFunc func = NULL;
879
880 /* not enabled */
881 if (!priv->enabled)
882 return TRUE;
883
884 /* no object loaded */
885 if (priv->module == NULL)
886 return TRUE;
887
888 /* optional */
889 g_module_symbol (priv->module, symbol_name, (gpointer *) &func);
890 if (func == NULL)
891 return TRUE;
892 g_debug ("performing %s() on %s", symbol_name + 10, priv->name);
Richard Hughes12724852018-09-04 13:53:44 +0100893 if (!func (self, flags, device, error)) {
Mario Limoncielloe3b1a3f2018-08-21 13:01:45 -0500894 g_prefix_error (error, "failed to run %s() on %s: ",
895 symbol_name + 10,
896 priv->name);
897 return FALSE;
898 }
899 return TRUE;
900
901}
902
903static gboolean
Richard Hughes12724852018-09-04 13:53:44 +0100904fu_plugin_runner_device_array_generic (FuPlugin *self, GPtrArray *devices,
Richard Hughesdbd8c762018-06-15 20:31:40 +0100905 const gchar *symbol_name, GError **error)
906{
Richard Hughes12724852018-09-04 13:53:44 +0100907 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughesdbd8c762018-06-15 20:31:40 +0100908 FuPluginDeviceArrayFunc func = NULL;
909
910 /* not enabled */
911 if (!priv->enabled)
912 return TRUE;
913
914 /* no object loaded */
915 if (priv->module == NULL)
916 return TRUE;
917
918 /* optional */
919 g_module_symbol (priv->module, symbol_name, (gpointer *) &func);
920 if (func == NULL)
921 return TRUE;
922 g_debug ("performing %s() on %s", symbol_name + 10, priv->name);
Richard Hughes12724852018-09-04 13:53:44 +0100923 if (!func (self, devices, error)) {
Richard Hughesdbd8c762018-06-15 20:31:40 +0100924 g_prefix_error (error, "failed to run %s() on %s: ",
925 symbol_name + 10,
926 priv->name);
927 return FALSE;
928 }
929 return TRUE;
930}
931
Richard Hughesd0905142016-03-13 09:46:49 +0000932gboolean
Richard Hughes12724852018-09-04 13:53:44 +0100933fu_plugin_runner_coldplug (FuPlugin *self, GError **error)
Richard Hughesd0905142016-03-13 09:46:49 +0000934{
Richard Hughes12724852018-09-04 13:53:44 +0100935 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughes7b8b2022016-12-12 16:15:03 +0000936 FuPluginStartupFunc func = NULL;
Richard Hughesd0905142016-03-13 09:46:49 +0000937
938 /* not enabled */
Richard Hughescff38bc2016-12-12 12:03:37 +0000939 if (!priv->enabled)
Richard Hughesd0905142016-03-13 09:46:49 +0000940 return TRUE;
941
Richard Hughes639da472018-01-06 22:35:04 +0000942 /* no object loaded */
943 if (priv->module == NULL)
944 return TRUE;
945
Richard Hughesd0905142016-03-13 09:46:49 +0000946 /* optional */
Richard Hughescff38bc2016-12-12 12:03:37 +0000947 g_module_symbol (priv->module, "fu_plugin_coldplug", (gpointer *) &func);
948 if (func == NULL)
Richard Hughesd0905142016-03-13 09:46:49 +0000949 return TRUE;
Richard Hughescff38bc2016-12-12 12:03:37 +0000950 g_debug ("performing coldplug() on %s", priv->name);
Richard Hughes12724852018-09-04 13:53:44 +0100951 if (!func (self, error)) {
Richard Hughescff38bc2016-12-12 12:03:37 +0000952 g_prefix_error (error, "failed to coldplug %s: ", priv->name);
953 return FALSE;
954 }
955 return TRUE;
956}
957
Richard Hughes7b8b2022016-12-12 16:15:03 +0000958gboolean
Richard Hughes12724852018-09-04 13:53:44 +0100959fu_plugin_runner_recoldplug (FuPlugin *self, GError **error)
Richard Hughes2de8f132018-01-17 09:12:02 +0000960{
Richard Hughes12724852018-09-04 13:53:44 +0100961 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughes2de8f132018-01-17 09:12:02 +0000962 FuPluginStartupFunc func = NULL;
963
964 /* not enabled */
965 if (!priv->enabled)
966 return TRUE;
967
968 /* no object loaded */
969 if (priv->module == NULL)
970 return TRUE;
971
972 /* optional */
973 g_module_symbol (priv->module, "fu_plugin_recoldplug", (gpointer *) &func);
974 if (func == NULL)
975 return TRUE;
976 g_debug ("performing recoldplug() on %s", priv->name);
Richard Hughes12724852018-09-04 13:53:44 +0100977 if (!func (self, error)) {
Richard Hughes2de8f132018-01-17 09:12:02 +0000978 g_prefix_error (error, "failed to recoldplug %s: ", priv->name);
979 return FALSE;
980 }
981 return TRUE;
982}
983
984gboolean
Richard Hughes12724852018-09-04 13:53:44 +0100985fu_plugin_runner_coldplug_prepare (FuPlugin *self, GError **error)
Richard Hughes46487c92017-01-07 21:26:34 +0000986{
Richard Hughes12724852018-09-04 13:53:44 +0100987 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughes46487c92017-01-07 21:26:34 +0000988 FuPluginStartupFunc func = NULL;
989
990 /* not enabled */
991 if (!priv->enabled)
992 return TRUE;
993
Richard Hughes639da472018-01-06 22:35:04 +0000994 /* no object loaded */
995 if (priv->module == NULL)
996 return TRUE;
997
Richard Hughes46487c92017-01-07 21:26:34 +0000998 /* optional */
999 g_module_symbol (priv->module, "fu_plugin_coldplug_prepare", (gpointer *) &func);
1000 if (func == NULL)
1001 return TRUE;
1002 g_debug ("performing coldplug_prepare() on %s", priv->name);
Richard Hughes12724852018-09-04 13:53:44 +01001003 if (!func (self, error)) {
Richard Hughes46487c92017-01-07 21:26:34 +00001004 g_prefix_error (error, "failed to prepare for coldplug %s: ", priv->name);
1005 return FALSE;
1006 }
1007 return TRUE;
1008}
1009
1010gboolean
Richard Hughes12724852018-09-04 13:53:44 +01001011fu_plugin_runner_coldplug_cleanup (FuPlugin *self, GError **error)
Richard Hughes46487c92017-01-07 21:26:34 +00001012{
Richard Hughes12724852018-09-04 13:53:44 +01001013 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughes46487c92017-01-07 21:26:34 +00001014 FuPluginStartupFunc func = NULL;
1015
1016 /* not enabled */
1017 if (!priv->enabled)
1018 return TRUE;
1019
Richard Hughes639da472018-01-06 22:35:04 +00001020 /* no object loaded */
1021 if (priv->module == NULL)
1022 return TRUE;
1023
Richard Hughes46487c92017-01-07 21:26:34 +00001024 /* optional */
1025 g_module_symbol (priv->module, "fu_plugin_coldplug_cleanup", (gpointer *) &func);
1026 if (func == NULL)
1027 return TRUE;
1028 g_debug ("performing coldplug_cleanup() on %s", priv->name);
Richard Hughes12724852018-09-04 13:53:44 +01001029 if (!func (self, error)) {
Richard Hughes46487c92017-01-07 21:26:34 +00001030 g_prefix_error (error, "failed to cleanup coldplug %s: ", priv->name);
1031 return FALSE;
1032 }
1033 return TRUE;
1034}
1035
1036gboolean
Richard Hughes12724852018-09-04 13:53:44 +01001037fu_plugin_runner_composite_prepare (FuPlugin *self, GPtrArray *devices, GError **error)
Richard Hughesdbd8c762018-06-15 20:31:40 +01001038{
Richard Hughes12724852018-09-04 13:53:44 +01001039 return fu_plugin_runner_device_array_generic (self, devices,
Richard Hughesdbd8c762018-06-15 20:31:40 +01001040 "fu_plugin_composite_prepare",
1041 error);
1042}
1043
1044gboolean
Richard Hughes12724852018-09-04 13:53:44 +01001045fu_plugin_runner_composite_cleanup (FuPlugin *self, GPtrArray *devices, GError **error)
Richard Hughesdbd8c762018-06-15 20:31:40 +01001046{
Richard Hughes12724852018-09-04 13:53:44 +01001047 return fu_plugin_runner_device_array_generic (self, devices,
Richard Hughesdbd8c762018-06-15 20:31:40 +01001048 "fu_plugin_composite_cleanup",
1049 error);
1050}
1051
1052gboolean
Richard Hughes12724852018-09-04 13:53:44 +01001053fu_plugin_runner_update_prepare (FuPlugin *self, FwupdInstallFlags flags, FuDevice *device,
Mario Limoncielloe3b1a3f2018-08-21 13:01:45 -05001054 GError **error)
Richard Hughes7b8b2022016-12-12 16:15:03 +00001055{
Richard Hughes12724852018-09-04 13:53:44 +01001056 return fu_plugin_runner_flagged_device_generic (self, flags, device,
Mario Limoncielloe3b1a3f2018-08-21 13:01:45 -05001057 "fu_plugin_update_prepare",
1058 error);
Richard Hughes7b8b2022016-12-12 16:15:03 +00001059}
1060
1061gboolean
Richard Hughes12724852018-09-04 13:53:44 +01001062fu_plugin_runner_update_cleanup (FuPlugin *self, FwupdInstallFlags flags, FuDevice *device,
Mario Limoncielloe3b1a3f2018-08-21 13:01:45 -05001063 GError **error)
Richard Hughes7b8b2022016-12-12 16:15:03 +00001064{
Richard Hughes12724852018-09-04 13:53:44 +01001065 return fu_plugin_runner_flagged_device_generic (self, flags, device,
Mario Limoncielloe3b1a3f2018-08-21 13:01:45 -05001066 "fu_plugin_update_cleanup",
1067 error);
Richard Hughes0d7fdb32017-11-11 20:23:14 +00001068}
Richard Hughes7b8b2022016-12-12 16:15:03 +00001069
Richard Hughes0d7fdb32017-11-11 20:23:14 +00001070gboolean
Richard Hughes12724852018-09-04 13:53:44 +01001071fu_plugin_runner_update_attach (FuPlugin *self, FuDevice *device, GError **error)
Richard Hughes0d7fdb32017-11-11 20:23:14 +00001072{
Richard Hughes12724852018-09-04 13:53:44 +01001073 return fu_plugin_runner_device_generic (self, device,
Richard Hughes0d7fdb32017-11-11 20:23:14 +00001074 "fu_plugin_update_attach", error);
1075}
Richard Hughes7b8b2022016-12-12 16:15:03 +00001076
Richard Hughes0d7fdb32017-11-11 20:23:14 +00001077gboolean
Richard Hughes12724852018-09-04 13:53:44 +01001078fu_plugin_runner_update_detach (FuPlugin *self, FuDevice *device, GError **error)
Richard Hughes0d7fdb32017-11-11 20:23:14 +00001079{
Richard Hughes12724852018-09-04 13:53:44 +01001080 return fu_plugin_runner_device_generic (self, device,
Richard Hughes0d7fdb32017-11-11 20:23:14 +00001081 "fu_plugin_update_detach", error);
1082}
1083
1084gboolean
Richard Hughes12724852018-09-04 13:53:44 +01001085fu_plugin_runner_update_reload (FuPlugin *self, FuDevice *device, GError **error)
Richard Hughes0d7fdb32017-11-11 20:23:14 +00001086{
Richard Hughes12724852018-09-04 13:53:44 +01001087 return fu_plugin_runner_device_generic (self, device,
Richard Hughes0d7fdb32017-11-11 20:23:14 +00001088 "fu_plugin_update_reload", error);
Richard Hughes7b8b2022016-12-12 16:15:03 +00001089}
1090
Richard Hughes9d6e0e72018-08-24 20:20:17 +01001091/**
1092 * fu_plugin_add_udev_subsystem:
Richard Hughes2c0635a2018-09-04 14:52:46 +01001093 * @self: a #FuPlugin
Richard Hughes9d6e0e72018-08-24 20:20:17 +01001094 * @subsystem: a subsystem name, e.g. `pciport`
1095 *
1096 * Registers the udev subsystem to be watched by the daemon.
1097 *
1098 * Plugins can use this method only in fu_plugin_init()
1099 **/
1100void
Richard Hughes12724852018-09-04 13:53:44 +01001101fu_plugin_add_udev_subsystem (FuPlugin *self, const gchar *subsystem)
Richard Hughes9d6e0e72018-08-24 20:20:17 +01001102{
Richard Hughes12724852018-09-04 13:53:44 +01001103 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughes9d6e0e72018-08-24 20:20:17 +01001104 for (guint i = 0; i < priv->udev_subsystems->len; i++) {
1105 const gchar *subsystem_tmp = g_ptr_array_index (priv->udev_subsystems, i);
1106 if (g_strcmp0 (subsystem_tmp, subsystem) == 0)
1107 return;
1108 }
1109 g_debug ("added udev subsystem watch of %s", subsystem);
1110 g_ptr_array_add (priv->udev_subsystems, g_strdup (subsystem));
1111}
1112
Richard Hughes104f6512017-11-24 11:44:57 +00001113gboolean
Richard Hughes12724852018-09-04 13:53:44 +01001114fu_plugin_runner_usb_device_added (FuPlugin *self, FuUsbDevice *device, GError **error)
Richard Hughes104f6512017-11-24 11:44:57 +00001115{
Richard Hughes12724852018-09-04 13:53:44 +01001116 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughes104f6512017-11-24 11:44:57 +00001117 FuPluginUsbDeviceAddedFunc func = NULL;
1118
1119 /* not enabled */
1120 if (!priv->enabled)
1121 return TRUE;
Richard Hughes639da472018-01-06 22:35:04 +00001122
1123 /* no object loaded */
Richard Hughes104f6512017-11-24 11:44:57 +00001124 if (priv->module == NULL)
1125 return TRUE;
1126
1127 /* optional */
1128 g_module_symbol (priv->module, "fu_plugin_usb_device_added", (gpointer *) &func);
1129 if (func != NULL) {
1130 g_debug ("performing usb_device_added() on %s", priv->name);
Richard Hughes12724852018-09-04 13:53:44 +01001131 return func (self, device, error);
Richard Hughes104f6512017-11-24 11:44:57 +00001132 }
1133 return TRUE;
1134}
1135
Richard Hughes9d6e0e72018-08-24 20:20:17 +01001136gboolean
Richard Hughes12724852018-09-04 13:53:44 +01001137fu_plugin_runner_udev_device_added (FuPlugin *self, FuUdevDevice *device, GError **error)
Richard Hughes9d6e0e72018-08-24 20:20:17 +01001138{
Richard Hughes12724852018-09-04 13:53:44 +01001139 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughes9d6e0e72018-08-24 20:20:17 +01001140 FuPluginUdevDeviceAddedFunc func = NULL;
1141
1142 /* not enabled */
1143 if (!priv->enabled)
1144 return TRUE;
1145
1146 /* no object loaded */
1147 if (priv->module == NULL)
1148 return TRUE;
1149
1150 /* optional */
1151 g_module_symbol (priv->module, "fu_plugin_udev_device_added", (gpointer *) &func);
1152 if (func != NULL) {
1153 g_debug ("performing udev_device_added() on %s", priv->name);
Richard Hughes12724852018-09-04 13:53:44 +01001154 return func (self, device, error);
Richard Hughes9d6e0e72018-08-24 20:20:17 +01001155 }
1156 return TRUE;
1157}
1158
Richard Hughese1fd34d2017-08-24 14:19:51 +01001159void
Richard Hughes12724852018-09-04 13:53:44 +01001160fu_plugin_runner_device_removed (FuPlugin *self, FuDevice *device)
Mario Limoncielloe260ead2018-09-01 09:19:24 -05001161{
1162 g_autoptr(GError) error_local= NULL;
1163
Richard Hughes12724852018-09-04 13:53:44 +01001164 if (!fu_plugin_runner_device_generic (self, device,
Mario Limoncielloe260ead2018-09-01 09:19:24 -05001165 "fu_plugin_device_removed",
1166 &error_local))
1167 g_warning ("%s", error_local->message);
1168}
1169
1170void
Richard Hughes12724852018-09-04 13:53:44 +01001171fu_plugin_runner_device_register (FuPlugin *self, FuDevice *device)
Richard Hughese1fd34d2017-08-24 14:19:51 +01001172{
Richard Hughes12724852018-09-04 13:53:44 +01001173 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughese1fd34d2017-08-24 14:19:51 +01001174 FuPluginDeviceRegisterFunc func = NULL;
1175
1176 /* not enabled */
1177 if (!priv->enabled)
1178 return;
Richard Hughes34834102017-11-21 21:55:00 +00001179 if (priv->module == NULL)
1180 return;
Richard Hughese1fd34d2017-08-24 14:19:51 +01001181
Mario Limonciello4910b242018-06-22 15:04:21 -05001182 /* don't notify plugins on their own devices */
Richard Hughes12724852018-09-04 13:53:44 +01001183 if (g_strcmp0 (fu_device_get_plugin (device), fu_plugin_get_name (self)) == 0)
Mario Limonciello4910b242018-06-22 15:04:21 -05001184 return;
1185
Richard Hughese1fd34d2017-08-24 14:19:51 +01001186 /* optional */
1187 g_module_symbol (priv->module, "fu_plugin_device_registered", (gpointer *) &func);
1188 if (func != NULL) {
Richard Hughes1bf7ff92018-08-24 20:21:35 +01001189 g_debug ("performing fu_plugin_device_registered() on %s", priv->name);
Richard Hughes12724852018-09-04 13:53:44 +01001190 func (self, device);
Richard Hughese1fd34d2017-08-24 14:19:51 +01001191 }
1192}
1193
Richard Hughescff38bc2016-12-12 12:03:37 +00001194static gboolean
Richard Hughes12724852018-09-04 13:53:44 +01001195fu_plugin_runner_schedule_update (FuPlugin *self,
Richard Hughescff38bc2016-12-12 12:03:37 +00001196 FuDevice *device,
1197 GBytes *blob_cab,
1198 GError **error)
1199{
Richard Hughes38fb56c2018-02-01 22:07:52 +00001200 FwupdRelease *release;
Richard Hughescff38bc2016-12-12 12:03:37 +00001201 gchar tmpname[] = {"XXXXXX.cap"};
1202 g_autofree gchar *dirname = NULL;
1203 g_autofree gchar *filename = NULL;
Richard Hughes68982c62017-09-13 15:40:14 +01001204 g_autoptr(FuDevice) res_tmp = NULL;
Richard Hughes780ef3f2018-01-12 16:20:31 +00001205 g_autoptr(FuHistory) history = NULL;
Richard Hughes38fb56c2018-02-01 22:07:52 +00001206 g_autoptr(FwupdRelease) release_tmp = fwupd_release_new ();
Richard Hughescff38bc2016-12-12 12:03:37 +00001207 g_autoptr(GFile) file = NULL;
1208
1209 /* id already exists */
Richard Hughes780ef3f2018-01-12 16:20:31 +00001210 history = fu_history_new ();
Richard Hughes0b9d9962018-01-12 16:31:28 +00001211 res_tmp = fu_history_get_device_by_id (history, fu_device_get_id (device), NULL);
Richard Hughescff38bc2016-12-12 12:03:37 +00001212 if (res_tmp != NULL) {
1213 g_set_error (error,
1214 FWUPD_ERROR,
1215 FWUPD_ERROR_ALREADY_PENDING,
1216 "%s is already scheduled to be updated",
1217 fu_device_get_id (device));
1218 return FALSE;
1219 }
1220
1221 /* create directory */
Richard Hughes4be17d12018-05-30 20:36:29 +01001222 dirname = fu_common_get_path (FU_PATH_KIND_LOCALSTATEDIR_PKG);
Richard Hughescff38bc2016-12-12 12:03:37 +00001223 file = g_file_new_for_path (dirname);
1224 if (!g_file_query_exists (file, NULL)) {
1225 if (!g_file_make_directory_with_parents (file, NULL, error))
1226 return FALSE;
1227 }
1228
1229 /* get a random filename */
1230 for (guint i = 0; i < 6; i++)
1231 tmpname[i] = (gchar) g_random_int_range ('A', 'Z');
1232 filename = g_build_filename (dirname, tmpname, NULL);
1233
1234 /* just copy to the temp file */
Richard Hughes23135eb2017-11-30 21:01:25 +00001235 fu_device_set_status (device, FWUPD_STATUS_SCHEDULING);
Richard Hughescff38bc2016-12-12 12:03:37 +00001236 if (!g_file_set_contents (filename,
1237 g_bytes_get_data (blob_cab, NULL),
1238 (gssize) g_bytes_get_size (blob_cab),
1239 error))
1240 return FALSE;
1241
1242 /* schedule for next boot */
1243 g_debug ("schedule %s to be installed to %s on next boot",
1244 filename, fu_device_get_id (device));
Richard Hughes38fb56c2018-02-01 22:07:52 +00001245 release = fu_device_get_release_default (device);
1246 fwupd_release_set_version (release_tmp, fwupd_release_get_version (release));
1247 fwupd_release_set_filename (release_tmp, filename);
Richard Hughescff38bc2016-12-12 12:03:37 +00001248
1249 /* add to database */
Richard Hughes3e90a582018-01-06 22:38:09 +00001250 fu_device_set_update_state (device, FWUPD_UPDATE_STATE_PENDING);
Richard Hughes38fb56c2018-02-01 22:07:52 +00001251 if (!fu_history_add_device (history, device, release_tmp, error))
Richard Hughescff38bc2016-12-12 12:03:37 +00001252 return FALSE;
1253
1254 /* next boot we run offline */
1255 return fu_plugin_runner_offline_setup (error);
1256}
1257
1258gboolean
Richard Hughes12724852018-09-04 13:53:44 +01001259fu_plugin_runner_verify (FuPlugin *self,
Richard Hughescff38bc2016-12-12 12:03:37 +00001260 FuDevice *device,
1261 FuPluginVerifyFlags flags,
1262 GError **error)
1263{
Richard Hughes12724852018-09-04 13:53:44 +01001264 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughes7b8b2022016-12-12 16:15:03 +00001265 FuPluginVerifyFunc func = NULL;
Richard Hughesababbb72017-06-15 20:18:36 +01001266 GPtrArray *checksums;
Richard Hughescff38bc2016-12-12 12:03:37 +00001267
1268 /* not enabled */
1269 if (!priv->enabled)
1270 return TRUE;
1271
Richard Hughes639da472018-01-06 22:35:04 +00001272 /* no object loaded */
1273 if (priv->module == NULL)
1274 return TRUE;
1275
Richard Hughesababbb72017-06-15 20:18:36 +01001276 /* clear any existing verification checksums */
1277 checksums = fu_device_get_checksums (device);
1278 g_ptr_array_set_size (checksums, 0);
1279
Richard Hughescff38bc2016-12-12 12:03:37 +00001280 /* optional */
1281 g_module_symbol (priv->module, "fu_plugin_verify", (gpointer *) &func);
1282 if (func == NULL)
1283 return TRUE;
1284 g_debug ("performing verify() on %s", priv->name);
Richard Hughes12724852018-09-04 13:53:44 +01001285 if (!func (self, device, flags, error)) {
Richard Hughescff38bc2016-12-12 12:03:37 +00001286 g_prefix_error (error, "failed to verify %s: ", priv->name);
Richard Hughesd0905142016-03-13 09:46:49 +00001287 return FALSE;
1288 }
1289 return TRUE;
1290}
1291
Richard Hughesd0905142016-03-13 09:46:49 +00001292gboolean
Richard Hughes12724852018-09-04 13:53:44 +01001293fu_plugin_runner_unlock (FuPlugin *self, FuDevice *device, GError **error)
Richard Hughesd0905142016-03-13 09:46:49 +00001294{
Richard Hughescff38bc2016-12-12 12:03:37 +00001295 guint64 flags;
Richard Hughescff38bc2016-12-12 12:03:37 +00001296
1297 /* final check */
1298 flags = fu_device_get_flags (device);
1299 if ((flags & FWUPD_DEVICE_FLAG_LOCKED) == 0) {
1300 g_set_error (error,
1301 FWUPD_ERROR,
1302 FWUPD_ERROR_NOT_SUPPORTED,
1303 "Device %s is not locked",
1304 fu_device_get_id (device));
1305 return FALSE;
1306 }
1307
Richard Hughes9c4b5312017-11-14 11:34:53 +00001308 /* run vfunc */
Richard Hughes12724852018-09-04 13:53:44 +01001309 if (!fu_plugin_runner_device_generic (self, device,
Richard Hughes9c4b5312017-11-14 11:34:53 +00001310 "fu_plugin_unlock", error))
1311 return FALSE;
Richard Hughescff38bc2016-12-12 12:03:37 +00001312
1313 /* update with correct flags */
1314 flags = fu_device_get_flags (device);
1315 fu_device_set_flags (device, flags &= ~FWUPD_DEVICE_FLAG_LOCKED);
1316 fu_device_set_modified (device, (guint64) g_get_real_time () / G_USEC_PER_SEC);
1317 return TRUE;
1318}
1319
1320gboolean
Richard Hughes12724852018-09-04 13:53:44 +01001321fu_plugin_runner_update (FuPlugin *self,
Richard Hughesa785a1c2017-08-25 16:00:58 +01001322 FuDevice *device,
1323 GBytes *blob_cab,
1324 GBytes *blob_fw,
1325 FwupdInstallFlags flags,
1326 GError **error)
Richard Hughescff38bc2016-12-12 12:03:37 +00001327{
Richard Hughes12724852018-09-04 13:53:44 +01001328 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughesa785a1c2017-08-25 16:00:58 +01001329 FuPluginUpdateFunc update_func;
Richard Hughes780ef3f2018-01-12 16:20:31 +00001330 g_autoptr(FuHistory) history = NULL;
Richard Hughes68982c62017-09-13 15:40:14 +01001331 g_autoptr(FuDevice) device_pending = NULL;
Richard Hughescff38bc2016-12-12 12:03:37 +00001332 GError *error_update = NULL;
Richard Hughesf556d372017-06-15 19:49:18 +01001333 GPtrArray *checksums;
Richard Hughescff38bc2016-12-12 12:03:37 +00001334
1335 /* not enabled */
Richard Hughes41c15482018-02-01 22:07:21 +00001336 if (!priv->enabled) {
1337 g_debug ("plugin not enabled, skipping");
Richard Hughesd0905142016-03-13 09:46:49 +00001338 return TRUE;
Richard Hughes41c15482018-02-01 22:07:21 +00001339 }
Richard Hughesd0905142016-03-13 09:46:49 +00001340
Richard Hughes639da472018-01-06 22:35:04 +00001341 /* no object loaded */
Richard Hughes41c15482018-02-01 22:07:21 +00001342 if (priv->module == NULL) {
1343 g_debug ("module not enabled, skipping");
Richard Hughes639da472018-01-06 22:35:04 +00001344 return TRUE;
Richard Hughes41c15482018-02-01 22:07:21 +00001345 }
Richard Hughes639da472018-01-06 22:35:04 +00001346
Richard Hughesd0905142016-03-13 09:46:49 +00001347 /* optional */
Richard Hughesa785a1c2017-08-25 16:00:58 +01001348 g_module_symbol (priv->module, "fu_plugin_update", (gpointer *) &update_func);
1349 if (update_func == NULL) {
1350 g_set_error_literal (error,
1351 FWUPD_ERROR,
1352 FWUPD_ERROR_NOT_SUPPORTED,
1353 "No update possible");
1354 return FALSE;
1355 }
Richard Hughesd0905142016-03-13 09:46:49 +00001356
Richard Hughesa785a1c2017-08-25 16:00:58 +01001357 /* just schedule this for the next reboot */
Richard Hughescff38bc2016-12-12 12:03:37 +00001358 if (flags & FWUPD_INSTALL_FLAG_OFFLINE) {
Richard Hughes026cdd82018-05-18 10:23:19 +01001359 if (blob_cab == NULL) {
1360 g_set_error_literal (error,
1361 FWUPD_ERROR,
1362 FWUPD_ERROR_NOT_SUPPORTED,
1363 "No cabinet archive to schedule");
1364 return FALSE;
1365 }
Richard Hughes12724852018-09-04 13:53:44 +01001366 return fu_plugin_runner_schedule_update (self,
Richard Hughesa785a1c2017-08-25 16:00:58 +01001367 device,
1368 blob_cab,
1369 error);
Richard Hughescff38bc2016-12-12 12:03:37 +00001370 }
1371
1372 /* cancel the pending action */
1373 if (!fu_plugin_runner_offline_invalidate (error))
1374 return FALSE;
1375
1376 /* online */
Richard Hughes780ef3f2018-01-12 16:20:31 +00001377 history = fu_history_new ();
Richard Hughes0b9d9962018-01-12 16:31:28 +00001378 device_pending = fu_history_get_device_by_id (history, fu_device_get_id (device), NULL);
Richard Hughes12724852018-09-04 13:53:44 +01001379 if (!update_func (self, device, blob_fw, flags, &error_update)) {
Richard Hughes3c8ada32018-10-12 10:08:58 +01001380 if (error_update == NULL) {
1381 g_critical ("plugin %s returned FALSE from UpdateFunc "
1382 "but did not set error!",
1383 fu_plugin_get_name (self));
1384 g_set_error_literal (&error_update,
1385 FWUPD_ERROR,
1386 FWUPD_ERROR_INTERNAL,
1387 "unspecified error");
1388 }
Richard Hughesc0cd0232018-01-31 15:02:00 +00001389 fu_device_set_update_error (device, error_update->message);
Richard Hughescff38bc2016-12-12 12:03:37 +00001390 g_propagate_error (error, error_update);
1391 return FALSE;
1392 }
1393
Richard Hughesf556d372017-06-15 19:49:18 +01001394 /* no longer valid */
1395 checksums = fu_device_get_checksums (device);
1396 g_ptr_array_set_size (checksums, 0);
1397
Richard Hughescff38bc2016-12-12 12:03:37 +00001398 /* cleanup */
Richard Hughes68982c62017-09-13 15:40:14 +01001399 if (device_pending != NULL) {
Richard Hughescff38bc2016-12-12 12:03:37 +00001400 const gchar *tmp;
Richard Hughesbc3a4e12018-01-06 22:41:47 +00001401 FwupdRelease *release;
Richard Hughescff38bc2016-12-12 12:03:37 +00001402
Richard Hughes780ef3f2018-01-12 16:20:31 +00001403 /* update history database */
Richard Hughesc0cd0232018-01-31 15:02:00 +00001404 fu_device_set_update_state (device, FWUPD_UPDATE_STATE_SUCCESS);
1405 if (!fu_history_modify_device (history, device,
1406 FU_HISTORY_FLAGS_MATCH_NEW_VERSION,
1407 error))
Richard Hughes0b9d9962018-01-12 16:31:28 +00001408 return FALSE;
Richard Hughescff38bc2016-12-12 12:03:37 +00001409
1410 /* delete cab file */
Richard Hughesbc3a4e12018-01-06 22:41:47 +00001411 release = fu_device_get_release_default (device_pending);
1412 tmp = fwupd_release_get_filename (release);
Richard Hughescff38bc2016-12-12 12:03:37 +00001413 if (tmp != NULL && g_str_has_prefix (tmp, LIBEXECDIR)) {
1414 g_autoptr(GError) error_local = NULL;
1415 g_autoptr(GFile) file = NULL;
1416 file = g_file_new_for_path (tmp);
1417 if (!g_file_delete (file, NULL, &error_local)) {
1418 g_set_error (error,
1419 FWUPD_ERROR,
1420 FWUPD_ERROR_INVALID_FILE,
1421 "Failed to delete %s: %s",
1422 tmp, error_local->message);
1423 return FALSE;
1424 }
1425 }
1426 }
Richard Hughesd0905142016-03-13 09:46:49 +00001427 return TRUE;
1428}
Richard Hughescff38bc2016-12-12 12:03:37 +00001429
1430gboolean
Richard Hughes12724852018-09-04 13:53:44 +01001431fu_plugin_runner_clear_results (FuPlugin *self, FuDevice *device, GError **error)
Richard Hughescff38bc2016-12-12 12:03:37 +00001432{
Richard Hughes12724852018-09-04 13:53:44 +01001433 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughes7b8b2022016-12-12 16:15:03 +00001434 FuPluginDeviceFunc func = NULL;
Richard Hughescff38bc2016-12-12 12:03:37 +00001435
1436 /* not enabled */
1437 if (!priv->enabled)
1438 return TRUE;
1439
Richard Hughes639da472018-01-06 22:35:04 +00001440 /* no object loaded */
1441 if (priv->module == NULL)
1442 return TRUE;
1443
Richard Hughes65e44ca2018-01-30 17:26:30 +00001444 /* optional */
1445 g_module_symbol (priv->module, "fu_plugin_get_results", (gpointer *) &func);
1446 if (func == NULL)
Richard Hughescff38bc2016-12-12 12:03:37 +00001447 return TRUE;
Richard Hughes65e44ca2018-01-30 17:26:30 +00001448 g_debug ("performing clear_result() on %s", priv->name);
Richard Hughes12724852018-09-04 13:53:44 +01001449 if (!func (self, device, error)) {
Richard Hughes65e44ca2018-01-30 17:26:30 +00001450 g_prefix_error (error, "failed to clear_result %s: ", priv->name);
Richard Hughescff38bc2016-12-12 12:03:37 +00001451 return FALSE;
1452 }
Richard Hughes65e44ca2018-01-30 17:26:30 +00001453 return TRUE;
Richard Hughescff38bc2016-12-12 12:03:37 +00001454}
1455
1456gboolean
Richard Hughes12724852018-09-04 13:53:44 +01001457fu_plugin_runner_get_results (FuPlugin *self, FuDevice *device, GError **error)
Richard Hughescff38bc2016-12-12 12:03:37 +00001458{
Richard Hughes12724852018-09-04 13:53:44 +01001459 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughes7b8b2022016-12-12 16:15:03 +00001460 FuPluginDeviceFunc func = NULL;
Richard Hughescff38bc2016-12-12 12:03:37 +00001461
1462 /* not enabled */
1463 if (!priv->enabled)
1464 return TRUE;
1465
Richard Hughes639da472018-01-06 22:35:04 +00001466 /* no object loaded */
1467 if (priv->module == NULL)
1468 return TRUE;
1469
Richard Hughes65e44ca2018-01-30 17:26:30 +00001470 /* optional */
Richard Hughescff38bc2016-12-12 12:03:37 +00001471 g_module_symbol (priv->module, "fu_plugin_get_results", (gpointer *) &func);
Richard Hughes65e44ca2018-01-30 17:26:30 +00001472 if (func == NULL)
Richard Hughescff38bc2016-12-12 12:03:37 +00001473 return TRUE;
Richard Hughes65e44ca2018-01-30 17:26:30 +00001474 g_debug ("performing get_results() on %s", priv->name);
Richard Hughes12724852018-09-04 13:53:44 +01001475 if (!func (self, device, error)) {
Richard Hughes65e44ca2018-01-30 17:26:30 +00001476 g_prefix_error (error, "failed to get_results %s: ", priv->name);
Richard Hughescff38bc2016-12-12 12:03:37 +00001477 return FALSE;
1478 }
Richard Hughescff38bc2016-12-12 12:03:37 +00001479 return TRUE;
1480}
1481
Richard Hughes08a37992017-09-12 12:57:43 +01001482/**
1483 * fu_plugin_get_order:
Richard Hughes2c0635a2018-09-04 14:52:46 +01001484 * @self: a #FuPlugin
Richard Hughes08a37992017-09-12 12:57:43 +01001485 *
1486 * Gets the plugin order, where higher numbers are run after lower
1487 * numbers.
1488 *
1489 * Returns: the integer value
1490 **/
1491guint
Richard Hughes12724852018-09-04 13:53:44 +01001492fu_plugin_get_order (FuPlugin *self)
Richard Hughes08a37992017-09-12 12:57:43 +01001493{
Richard Hughes12724852018-09-04 13:53:44 +01001494 FuPluginPrivate *priv = fu_plugin_get_instance_private (self);
Richard Hughes08a37992017-09-12 12:57:43 +01001495 return priv->order;
1496}
1497
1498/**
1499 * fu_plugin_set_order:
Richard Hughes2c0635a2018-09-04 14:52:46 +01001500 * @self: a #FuPlugin
Richard Hughes08a37992017-09-12 12:57:43 +01001501 * @order: a integer value
1502 *
1503 * Sets the plugin order, where higher numbers are run after lower
1504 * numbers.
1505 **/
1506void
Richard Hughes12724852018-09-04 13:53:44 +01001507fu_plugin_set_order (FuPlugin *self, guint order)
Richard Hughes08a37992017-09-12 12:57:43 +01001508{
Richard Hughes12724852018-09-04 13:53:44 +01001509 FuPluginPrivate *priv = fu_plugin_get_instance_private (self);
Richard Hughes08a37992017-09-12 12:57:43 +01001510 priv->order = order;
1511}
1512
1513/**
Richard Hughes81c427c2018-08-06 15:20:17 +01001514 * fu_plugin_get_priority:
Richard Hughes2c0635a2018-09-04 14:52:46 +01001515 * @self: a #FuPlugin
Richard Hughes81c427c2018-08-06 15:20:17 +01001516 *
1517 * Gets the plugin priority, where higher numbers are better.
1518 *
1519 * Returns: the integer value
1520 **/
1521guint
Richard Hughes12724852018-09-04 13:53:44 +01001522fu_plugin_get_priority (FuPlugin *self)
Richard Hughes81c427c2018-08-06 15:20:17 +01001523{
Richard Hughes12724852018-09-04 13:53:44 +01001524 FuPluginPrivate *priv = fu_plugin_get_instance_private (self);
Richard Hughes81c427c2018-08-06 15:20:17 +01001525 return priv->priority;
1526}
1527
1528/**
1529 * fu_plugin_set_priority:
Richard Hughes2c0635a2018-09-04 14:52:46 +01001530 * @self: a #FuPlugin
Richard Hughes81c427c2018-08-06 15:20:17 +01001531 * @priority: a integer value
1532 *
1533 * Sets the plugin priority, where higher numbers are better.
1534 **/
1535void
Richard Hughes12724852018-09-04 13:53:44 +01001536fu_plugin_set_priority (FuPlugin *self, guint priority)
Richard Hughes81c427c2018-08-06 15:20:17 +01001537{
Richard Hughes12724852018-09-04 13:53:44 +01001538 FuPluginPrivate *priv = fu_plugin_get_instance_private (self);
Richard Hughes81c427c2018-08-06 15:20:17 +01001539 priv->priority = priority;
1540}
1541
1542/**
Richard Hughes08a37992017-09-12 12:57:43 +01001543 * fu_plugin_add_rule:
Richard Hughes2c0635a2018-09-04 14:52:46 +01001544 * @self: a #FuPlugin
Richard Hughes08a37992017-09-12 12:57:43 +01001545 * @rule: a #FuPluginRule, e.g. %FU_PLUGIN_RULE_CONFLICTS
Richard Hughes4eada342017-10-03 21:20:32 +01001546 * @name: a plugin name, e.g. `upower`
Richard Hughes08a37992017-09-12 12:57:43 +01001547 *
1548 * If the plugin name is found, the rule will be used to sort the plugin list,
1549 * for example the plugin specified by @name will be ordered after this plugin
1550 * when %FU_PLUGIN_RULE_RUN_AFTER is used.
1551 *
1552 * NOTE: The depsolver is iterative and may not solve overly-complicated rules;
1553 * If depsolving fails then fwupd will not start.
1554 **/
1555void
Richard Hughes12724852018-09-04 13:53:44 +01001556fu_plugin_add_rule (FuPlugin *self, FuPluginRule rule, const gchar *name)
Richard Hughes08a37992017-09-12 12:57:43 +01001557{
Richard Hughes12724852018-09-04 13:53:44 +01001558 FuPluginPrivate *priv = fu_plugin_get_instance_private (self);
Richard Hughes08a37992017-09-12 12:57:43 +01001559 g_ptr_array_add (priv->rules[rule], g_strdup (name));
1560}
1561
1562/**
1563 * fu_plugin_get_rules:
Richard Hughes2c0635a2018-09-04 14:52:46 +01001564 * @self: a #FuPlugin
Richard Hughes08a37992017-09-12 12:57:43 +01001565 * @rule: a #FuPluginRule, e.g. %FU_PLUGIN_RULE_CONFLICTS
1566 *
1567 * Gets the plugin IDs that should be run after this plugin.
1568 *
1569 * Returns: (element-type utf8) (transfer none): the list of plugin names, e.g. ['appstream']
1570 **/
1571GPtrArray *
Richard Hughes12724852018-09-04 13:53:44 +01001572fu_plugin_get_rules (FuPlugin *self, FuPluginRule rule)
Richard Hughes08a37992017-09-12 12:57:43 +01001573{
Richard Hughes12724852018-09-04 13:53:44 +01001574 FuPluginPrivate *priv = fu_plugin_get_instance_private (self);
Richard Hughes08a37992017-09-12 12:57:43 +01001575 return priv->rules[rule];
1576}
1577
Richard Hughes80b79bb2018-01-11 21:11:06 +00001578/**
Richard Hughes5f3a56b2018-06-28 12:13:59 +01001579 * fu_plugin_has_rule:
Richard Hughes2c0635a2018-09-04 14:52:46 +01001580 * @self: a #FuPlugin
Richard Hughes5f3a56b2018-06-28 12:13:59 +01001581 * @rule: a #FuPluginRule, e.g. %FU_PLUGIN_RULE_CONFLICTS
1582 * @name: a plugin name, e.g. `upower`
1583 *
Richard Hughes87fb9ff2018-06-28 12:55:59 +01001584 * Gets the plugin IDs that should be run after this plugin.
Richard Hughes5f3a56b2018-06-28 12:13:59 +01001585 *
1586 * Returns: %TRUE if the name exists for the specific rule
1587 **/
1588gboolean
Richard Hughes12724852018-09-04 13:53:44 +01001589fu_plugin_has_rule (FuPlugin *self, FuPluginRule rule, const gchar *name)
Richard Hughes5f3a56b2018-06-28 12:13:59 +01001590{
Richard Hughes12724852018-09-04 13:53:44 +01001591 FuPluginPrivate *priv = fu_plugin_get_instance_private (self);
Richard Hughes5f3a56b2018-06-28 12:13:59 +01001592 for (guint i = 0; i < priv->rules[rule]->len; i++) {
1593 const gchar *tmp = g_ptr_array_index (priv->rules[rule], i);
1594 if (g_strcmp0 (tmp, name) == 0)
1595 return TRUE;
1596 }
1597 return FALSE;
1598}
1599
1600/**
Richard Hughes80b79bb2018-01-11 21:11:06 +00001601 * fu_plugin_add_report_metadata:
Richard Hughes2c0635a2018-09-04 14:52:46 +01001602 * @self: a #FuPlugin
Richard Hughes80b79bb2018-01-11 21:11:06 +00001603 * @key: a string, e.g. `FwupdateVersion`
1604 * @value: a string, e.g. `10`
1605 *
1606 * Sets any additional metadata to be included in the firmware report to aid
1607 * debugging problems.
1608 *
1609 * Any data included here will be sent to the metadata server after user
1610 * confirmation.
1611 **/
1612void
Richard Hughes12724852018-09-04 13:53:44 +01001613fu_plugin_add_report_metadata (FuPlugin *self, const gchar *key, const gchar *value)
Richard Hughes80b79bb2018-01-11 21:11:06 +00001614{
Richard Hughes12724852018-09-04 13:53:44 +01001615 FuPluginPrivate *priv = fu_plugin_get_instance_private (self);
Richard Hughes80b79bb2018-01-11 21:11:06 +00001616 g_hash_table_insert (priv->report_metadata, g_strdup (key), g_strdup (value));
1617}
1618
1619/**
1620 * fu_plugin_get_report_metadata:
Richard Hughes2c0635a2018-09-04 14:52:46 +01001621 * @self: a #FuPlugin
Richard Hughes80b79bb2018-01-11 21:11:06 +00001622 *
1623 * Returns the list of additional metadata to be added when filing a report.
1624 *
1625 * Returns: (transfer none): the map of report metadata
1626 **/
1627GHashTable *
Richard Hughes12724852018-09-04 13:53:44 +01001628fu_plugin_get_report_metadata (FuPlugin *self)
Richard Hughes80b79bb2018-01-11 21:11:06 +00001629{
Richard Hughes12724852018-09-04 13:53:44 +01001630 FuPluginPrivate *priv = fu_plugin_get_instance_private (self);
Richard Hughes80b79bb2018-01-11 21:11:06 +00001631 return priv->report_metadata;
1632}
1633
Mario Limonciello963dc422018-02-27 14:26:58 -06001634/**
1635 * fu_plugin_get_config_value:
Richard Hughes2c0635a2018-09-04 14:52:46 +01001636 * @self: a #FuPlugin
Mario Limonciello963dc422018-02-27 14:26:58 -06001637 * @key: A settings key
1638 *
1639 * Return the value of a key if it's been configured
1640 *
1641 * Since: 1.0.6
1642 **/
1643gchar *
Richard Hughes12724852018-09-04 13:53:44 +01001644fu_plugin_get_config_value (FuPlugin *self, const gchar *key)
Mario Limonciello963dc422018-02-27 14:26:58 -06001645{
Richard Hughes4be17d12018-05-30 20:36:29 +01001646 g_autofree gchar *conf_dir = NULL;
Mario Limonciello963dc422018-02-27 14:26:58 -06001647 g_autofree gchar *conf_file = NULL;
1648 g_autofree gchar *conf_path = NULL;
1649 g_autoptr(GKeyFile) keyfile = NULL;
1650 const gchar *plugin_name;
1651
Richard Hughes4be17d12018-05-30 20:36:29 +01001652 conf_dir = fu_common_get_path (FU_PATH_KIND_SYSCONFDIR_PKG);
Richard Hughes12724852018-09-04 13:53:44 +01001653 plugin_name = fu_plugin_get_name (self);
Mario Limonciello963dc422018-02-27 14:26:58 -06001654 conf_file = g_strdup_printf ("%s.conf", plugin_name);
Richard Hughes4be17d12018-05-30 20:36:29 +01001655 conf_path = g_build_filename (conf_dir, conf_file, NULL);
Mario Limonciello963dc422018-02-27 14:26:58 -06001656 if (!g_file_test (conf_path, G_FILE_TEST_IS_REGULAR))
1657 return NULL;
1658 keyfile = g_key_file_new ();
1659 if (!g_key_file_load_from_file (keyfile, conf_path,
1660 G_KEY_FILE_NONE, NULL))
1661 return NULL;
1662 return g_key_file_get_string (keyfile, plugin_name, key, NULL);
1663}
1664
Richard Hughes8c71a3f2018-05-22 19:19:52 +01001665/**
1666 * fu_plugin_name_compare:
1667 * @plugin1: first #FuPlugin to compare.
1668 * @plugin2: second #FuPlugin to compare.
1669 *
1670 * Compares two plugins by their names.
1671 *
1672 * Returns: 1, 0 or -1 if @plugin1 is greater, equal, or less than @plugin2.
1673 **/
1674gint
1675fu_plugin_name_compare (FuPlugin *plugin1, FuPlugin *plugin2)
1676{
1677 FuPluginPrivate *priv1 = fu_plugin_get_instance_private (plugin1);
1678 FuPluginPrivate *priv2 = fu_plugin_get_instance_private (plugin2);
1679 return g_strcmp0 (priv1->name, priv2->name);
1680}
1681
1682/**
1683 * fu_plugin_order_compare:
1684 * @plugin1: first #FuPlugin to compare.
1685 * @plugin2: second #FuPlugin to compare.
1686 *
1687 * Compares two plugins by their depsolved order.
1688 *
1689 * Returns: 1, 0 or -1 if @plugin1 is greater, equal, or less than @plugin2.
1690 **/
1691gint
1692fu_plugin_order_compare (FuPlugin *plugin1, FuPlugin *plugin2)
1693{
1694 FuPluginPrivate *priv1 = fu_plugin_get_instance_private (plugin1);
1695 FuPluginPrivate *priv2 = fu_plugin_get_instance_private (plugin2);
1696 if (priv1->order < priv2->order)
1697 return -1;
1698 if (priv1->order > priv2->order)
1699 return 1;
1700 return 0;
1701}
1702
Richard Hughescff38bc2016-12-12 12:03:37 +00001703static void
1704fu_plugin_class_init (FuPluginClass *klass)
1705{
1706 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1707 object_class->finalize = fu_plugin_finalize;
1708 signals[SIGNAL_DEVICE_ADDED] =
1709 g_signal_new ("device-added",
1710 G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST,
1711 G_STRUCT_OFFSET (FuPluginClass, device_added),
1712 NULL, NULL, g_cclosure_marshal_VOID__OBJECT,
1713 G_TYPE_NONE, 1, FU_TYPE_DEVICE);
1714 signals[SIGNAL_DEVICE_REMOVED] =
1715 g_signal_new ("device-removed",
1716 G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST,
1717 G_STRUCT_OFFSET (FuPluginClass, device_removed),
1718 NULL, NULL, g_cclosure_marshal_VOID__OBJECT,
1719 G_TYPE_NONE, 1, FU_TYPE_DEVICE);
Richard Hughese1fd34d2017-08-24 14:19:51 +01001720 signals[SIGNAL_DEVICE_REGISTER] =
1721 g_signal_new ("device-register",
1722 G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST,
1723 G_STRUCT_OFFSET (FuPluginClass, device_register),
1724 NULL, NULL, g_cclosure_marshal_VOID__OBJECT,
1725 G_TYPE_NONE, 1, FU_TYPE_DEVICE);
Richard Hughes362d6d72017-01-07 21:42:14 +00001726 signals[SIGNAL_RECOLDPLUG] =
1727 g_signal_new ("recoldplug",
1728 G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST,
1729 G_STRUCT_OFFSET (FuPluginClass, recoldplug),
1730 NULL, NULL, g_cclosure_marshal_VOID__VOID,
1731 G_TYPE_NONE, 0);
Richard Hughesb0829032017-01-10 09:27:08 +00001732 signals[SIGNAL_SET_COLDPLUG_DELAY] =
1733 g_signal_new ("set-coldplug-delay",
1734 G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST,
1735 G_STRUCT_OFFSET (FuPluginClass, set_coldplug_delay),
1736 NULL, NULL, g_cclosure_marshal_VOID__UINT,
1737 G_TYPE_NONE, 1, G_TYPE_UINT);
Richard Hughesaabdc372018-11-14 10:11:08 +00001738 signals[SIGNAL_CHECK_SUPPORTED] =
1739 g_signal_new ("check-supported",
1740 G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST,
1741 G_STRUCT_OFFSET (FuPluginClass, check_supported),
1742 NULL, NULL, g_cclosure_marshal_generic,
1743 G_TYPE_BOOLEAN, 1, G_TYPE_STRING);
Richard Hughescff38bc2016-12-12 12:03:37 +00001744}
1745
1746static void
Richard Hughes12724852018-09-04 13:53:44 +01001747fu_plugin_init (FuPlugin *self)
Richard Hughescff38bc2016-12-12 12:03:37 +00001748{
Richard Hughes12724852018-09-04 13:53:44 +01001749 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughescff38bc2016-12-12 12:03:37 +00001750 priv->enabled = TRUE;
1751 priv->devices = g_hash_table_new_full (g_str_hash, g_str_equal,
1752 g_free, (GDestroyNotify) g_object_unref);
Richard Hughes37d09432018-09-09 10:39:45 +01001753 priv->devices_mutex = fu_mutex_new (G_OBJECT_TYPE_NAME(self), "devices");
Richard Hughes80b79bb2018-01-11 21:11:06 +00001754 priv->report_metadata = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
Richard Hughes08a37992017-09-12 12:57:43 +01001755 for (guint i = 0; i < FU_PLUGIN_RULE_LAST; i++)
1756 priv->rules[i] = g_ptr_array_new_with_free_func (g_free);
Richard Hughescff38bc2016-12-12 12:03:37 +00001757}
1758
1759static void
1760fu_plugin_finalize (GObject *object)
1761{
Richard Hughes12724852018-09-04 13:53:44 +01001762 FuPlugin *self = FU_PLUGIN (object);
1763 FuPluginPrivate *priv = GET_PRIVATE (self);
Richard Hughescff38bc2016-12-12 12:03:37 +00001764 FuPluginInitFunc func = NULL;
1765
1766 /* optional */
1767 if (priv->module != NULL) {
1768 g_module_symbol (priv->module, "fu_plugin_destroy", (gpointer *) &func);
1769 if (func != NULL) {
1770 g_debug ("performing destroy() on %s", priv->name);
Richard Hughes12724852018-09-04 13:53:44 +01001771 func (self);
Richard Hughescff38bc2016-12-12 12:03:37 +00001772 }
1773 }
1774
Richard Hughes08a37992017-09-12 12:57:43 +01001775 for (guint i = 0; i < FU_PLUGIN_RULE_LAST; i++)
1776 g_ptr_array_unref (priv->rules[i]);
1777
Richard Hughescff38bc2016-12-12 12:03:37 +00001778 if (priv->usb_ctx != NULL)
1779 g_object_unref (priv->usb_ctx);
Richard Hughesb8f8db22017-04-25 15:56:00 +01001780 if (priv->hwids != NULL)
Richard Hughesd7704d42017-08-08 20:29:09 +01001781 g_object_unref (priv->hwids);
Richard Hughes9c028f02017-10-28 21:14:28 +01001782 if (priv->quirks != NULL)
1783 g_object_unref (priv->quirks);
Richard Hughes9d6e0e72018-08-24 20:20:17 +01001784 if (priv->udev_subsystems != NULL)
1785 g_ptr_array_unref (priv->udev_subsystems);
Richard Hughes49e5e052017-09-03 12:15:41 +01001786 if (priv->smbios != NULL)
1787 g_object_unref (priv->smbios);
Richard Hughes275d3b42018-04-20 16:40:37 +01001788 if (priv->runtime_versions != NULL)
1789 g_hash_table_unref (priv->runtime_versions);
Richard Hughes34e0dab2018-04-20 16:43:00 +01001790 if (priv->compile_versions != NULL)
1791 g_hash_table_unref (priv->compile_versions);
Richard Hughescff38bc2016-12-12 12:03:37 +00001792 g_hash_table_unref (priv->devices);
Richard Hughes80b79bb2018-01-11 21:11:06 +00001793 g_hash_table_unref (priv->report_metadata);
Richard Hughes37d09432018-09-09 10:39:45 +01001794 g_object_unref (priv->devices_mutex);
Richard Hughescff38bc2016-12-12 12:03:37 +00001795 g_free (priv->name);
1796 g_free (priv->data);
Mario Limonciello3f9a1c12018-06-06 14:06:40 -05001797 /* Must happen as the last step to avoid prematurely
1798 * freeing memory held by the plugin */
1799#ifndef RUNNING_ON_VALGRIND
1800 if (priv->module != NULL)
1801 g_module_close (priv->module);
1802#endif
Richard Hughescff38bc2016-12-12 12:03:37 +00001803
1804 G_OBJECT_CLASS (fu_plugin_parent_class)->finalize (object);
1805}
1806
1807FuPlugin *
1808fu_plugin_new (void)
1809{
Richard Hughes12724852018-09-04 13:53:44 +01001810 return FU_PLUGIN (g_object_new (FU_TYPE_PLUGIN, NULL));
Richard Hughescff38bc2016-12-12 12:03:37 +00001811}