blob: 7c3fdfc1b12022adf2afd13acca3052d8917dee9 [file] [log] [blame]
Richard Hughesd0905142016-03-13 09:46:49 +00001/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
2 *
Richard Hughes2de8f132018-01-17 09:12:02 +00003 * Copyright (C) 2016-2018 Richard Hughes <richard@hughsie.com>
Richard Hughesd0905142016-03-13 09:46:49 +00004 *
5 * Licensed under the GNU General Public License Version 2
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 */
21
22#include "config.h"
23
Richard Hughescff38bc2016-12-12 12:03:37 +000024#include <fwupd.h>
25#include <gmodule.h>
26#include <appstream-glib.h>
27#include <errno.h>
28#include <string.h>
29#include <gio/gunixinputstream.h>
Mario Limonciello6d0aa3d2017-02-28 08:22:27 -060030#ifdef HAVE_VALGRIND
Richard Hughes576c0122017-02-24 09:47:00 +000031#include <valgrind.h>
Mario Limonciello6d0aa3d2017-02-28 08:22:27 -060032#endif /* HAVE_VALGRIND */
Richard Hughesd0905142016-03-13 09:46:49 +000033
Richard Hughes9dde04f2017-09-13 12:07:15 +010034#include "fu-device-private.h"
Richard Hughescff38bc2016-12-12 12:03:37 +000035#include "fu-plugin-private.h"
Richard Hughesbc3a4e12018-01-06 22:41:47 +000036#include "fu-history.h"
Richard Hughesd0905142016-03-13 09:46:49 +000037
Richard Hughes4eada342017-10-03 21:20:32 +010038/**
39 * SECTION:fu-plugin
40 * @short_description: a daemon plugin
41 *
42 * An object that represents a plugin run by the daemon.
43 *
44 * See also: #FuDevice
45 */
46
Richard Hughesb0829032017-01-10 09:27:08 +000047#define FU_PLUGIN_COLDPLUG_DELAY_MAXIMUM 3000u /* ms */
48
Richard Hughescff38bc2016-12-12 12:03:37 +000049static void fu_plugin_finalize (GObject *object);
50
51typedef struct {
52 GModule *module;
53 GUsbContext *usb_ctx;
54 gboolean enabled;
Richard Hughes08a37992017-09-12 12:57:43 +010055 guint order;
56 GPtrArray *rules[FU_PLUGIN_RULE_LAST];
Richard Hughescff38bc2016-12-12 12:03:37 +000057 gchar *name;
Richard Hughesd7704d42017-08-08 20:29:09 +010058 FuHwids *hwids;
Richard Hughes9c028f02017-10-28 21:14:28 +010059 FuQuirks *quirks;
Richard Hughes1354ea92017-09-19 15:58:31 +010060 GPtrArray *supported_guids;
61 FuSmbios *smbios;
Richard Hughescff38bc2016-12-12 12:03:37 +000062 GHashTable *devices; /* platform_id:GObject */
Richard Hughesae3d65f2016-12-16 09:38:01 +000063 GHashTable *devices_delay; /* FuDevice:FuPluginHelper */
Richard Hughes80b79bb2018-01-11 21:11:06 +000064 GHashTable *report_metadata; /* key:value */
Richard Hughescff38bc2016-12-12 12:03:37 +000065 FuPluginData *data;
66} FuPluginPrivate;
67
68enum {
69 SIGNAL_DEVICE_ADDED,
70 SIGNAL_DEVICE_REMOVED,
Richard Hughese1fd34d2017-08-24 14:19:51 +010071 SIGNAL_DEVICE_REGISTER,
Richard Hughes362d6d72017-01-07 21:42:14 +000072 SIGNAL_RECOLDPLUG,
Richard Hughesb0829032017-01-10 09:27:08 +000073 SIGNAL_SET_COLDPLUG_DELAY,
Richard Hughescff38bc2016-12-12 12:03:37 +000074 SIGNAL_LAST
75};
76
77static guint signals[SIGNAL_LAST] = { 0 };
78
79G_DEFINE_TYPE_WITH_PRIVATE (FuPlugin, fu_plugin, G_TYPE_OBJECT)
80#define GET_PRIVATE(o) (fu_plugin_get_instance_private (o))
81
82typedef const gchar *(*FuPluginGetNameFunc) (void);
83typedef void (*FuPluginInitFunc) (FuPlugin *plugin);
84typedef gboolean (*FuPluginStartupFunc) (FuPlugin *plugin,
85 GError **error);
Richard Hughese1fd34d2017-08-24 14:19:51 +010086typedef void (*FuPluginDeviceRegisterFunc) (FuPlugin *plugin,
87 FuDevice *device);
Richard Hughescff38bc2016-12-12 12:03:37 +000088typedef gboolean (*FuPluginDeviceFunc) (FuPlugin *plugin,
89 FuDevice *device,
90 GError **error);
91typedef gboolean (*FuPluginVerifyFunc) (FuPlugin *plugin,
92 FuDevice *device,
93 FuPluginVerifyFlags flags,
94 GError **error);
95typedef gboolean (*FuPluginUpdateFunc) (FuPlugin *plugin,
96 FuDevice *device,
97 GBytes *blob_fw,
98 FwupdInstallFlags flags,
99 GError **error);
Richard Hughes104f6512017-11-24 11:44:57 +0000100typedef gboolean (*FuPluginUsbDeviceAddedFunc) (FuPlugin *plugin,
101 GUsbDevice *usb_device,
102 GError **error);
Richard Hughescff38bc2016-12-12 12:03:37 +0000103
Richard Hughes57d18222017-01-10 16:02:59 +0000104/**
105 * fu_plugin_get_name:
106 * @plugin: A #FuPlugin
107 *
108 * Gets the plugin name.
109 *
110 * Returns: a plugin name, or %NULL for unknown.
111 *
112 * Since: 0.8.0
113 **/
Richard Hughescff38bc2016-12-12 12:03:37 +0000114const gchar *
115fu_plugin_get_name (FuPlugin *plugin)
Richard Hughesd0905142016-03-13 09:46:49 +0000116{
Richard Hughescff38bc2016-12-12 12:03:37 +0000117 FuPluginPrivate *priv = GET_PRIVATE (plugin);
Richard Hughesccd78a92017-01-11 16:57:41 +0000118 g_return_val_if_fail (FU_IS_PLUGIN (plugin), NULL);
Richard Hughescff38bc2016-12-12 12:03:37 +0000119 return priv->name;
120}
Richard Hughesd0905142016-03-13 09:46:49 +0000121
Richard Hughes34834102017-11-21 21:55:00 +0000122void
123fu_plugin_set_name (FuPlugin *plugin, const gchar *name)
124{
125 FuPluginPrivate *priv = GET_PRIVATE (plugin);
126 g_return_if_fail (FU_IS_PLUGIN (plugin));
127 g_return_if_fail (name != NULL);
128 g_free (priv->name);
129 priv->name = g_strdup (name);
130}
131
Richard Hughes57d18222017-01-10 16:02:59 +0000132/**
133 * fu_plugin_cache_lookup:
134 * @plugin: A #FuPlugin
135 * @id: the key
136 *
137 * Finds an object in the per-plugin cache.
138 *
139 * Returns: (transfer none): a #GObject, or %NULL for unfound.
140 *
141 * Since: 0.8.0
142 **/
Richard Hughescff38bc2016-12-12 12:03:37 +0000143gpointer
144fu_plugin_cache_lookup (FuPlugin *plugin, const gchar *id)
145{
146 FuPluginPrivate *priv = GET_PRIVATE (plugin);
Richard Hughesccd78a92017-01-11 16:57:41 +0000147 g_return_val_if_fail (FU_IS_PLUGIN (plugin), NULL);
148 g_return_val_if_fail (id != NULL, NULL);
Richard Hughescff38bc2016-12-12 12:03:37 +0000149 return g_hash_table_lookup (priv->devices, id);
150}
Richard Hughesd0905142016-03-13 09:46:49 +0000151
Richard Hughes57d18222017-01-10 16:02:59 +0000152/**
153 * fu_plugin_cache_add:
154 * @plugin: A #FuPlugin
155 * @id: the key
156 * @dev: a #GObject, typically a #FuDevice
157 *
158 * Adds an object to the per-plugin cache.
159 *
160 * Since: 0.8.0
161 **/
Richard Hughescff38bc2016-12-12 12:03:37 +0000162void
163fu_plugin_cache_add (FuPlugin *plugin, const gchar *id, gpointer dev)
164{
165 FuPluginPrivate *priv = GET_PRIVATE (plugin);
Richard Hughesccd78a92017-01-11 16:57:41 +0000166 g_return_if_fail (FU_IS_PLUGIN (plugin));
167 g_return_if_fail (id != NULL);
Richard Hughescff38bc2016-12-12 12:03:37 +0000168 g_hash_table_insert (priv->devices, g_strdup (id), g_object_ref (dev));
169}
170
Richard Hughes57d18222017-01-10 16:02:59 +0000171/**
172 * fu_plugin_cache_remove:
173 * @plugin: A #FuPlugin
174 * @id: the key
175 *
176 * Removes an object from the per-plugin cache.
177 *
178 * Since: 0.8.0
179 **/
Richard Hughescff38bc2016-12-12 12:03:37 +0000180void
181fu_plugin_cache_remove (FuPlugin *plugin, const gchar *id)
182{
183 FuPluginPrivate *priv = GET_PRIVATE (plugin);
Richard Hughesccd78a92017-01-11 16:57:41 +0000184 g_return_if_fail (FU_IS_PLUGIN (plugin));
185 g_return_if_fail (id != NULL);
Richard Hughescff38bc2016-12-12 12:03:37 +0000186 g_hash_table_remove (priv->devices, id);
187}
188
Richard Hughes57d18222017-01-10 16:02:59 +0000189/**
190 * fu_plugin_get_data:
191 * @plugin: A #FuPlugin
192 *
Richard Hughes4eada342017-10-03 21:20:32 +0100193 * Gets the per-plugin allocated private data. This will return %NULL unless
194 * fu_plugin_alloc_data() has been called by the plugin.
Richard Hughes57d18222017-01-10 16:02:59 +0000195 *
Richard Hughes4eada342017-10-03 21:20:32 +0100196 * Returns: (transfer none): a pointer to a structure, or %NULL for unset.
Richard Hughes57d18222017-01-10 16:02:59 +0000197 *
198 * Since: 0.8.0
199 **/
Richard Hughescff38bc2016-12-12 12:03:37 +0000200FuPluginData *
201fu_plugin_get_data (FuPlugin *plugin)
202{
203 FuPluginPrivate *priv = GET_PRIVATE (plugin);
Richard Hughesccd78a92017-01-11 16:57:41 +0000204 g_return_val_if_fail (FU_IS_PLUGIN (plugin), NULL);
Richard Hughescff38bc2016-12-12 12:03:37 +0000205 return priv->data;
206}
207
Richard Hughes57d18222017-01-10 16:02:59 +0000208/**
209 * fu_plugin_alloc_data:
210 * @plugin: A #FuPlugin
211 * @data_sz: the size to allocate
212 *
213 * Allocates the per-plugin allocated private data.
214 *
215 * Returns: (transfer full): a pointer to a structure, or %NULL for unset.
216 *
217 * Since: 0.8.0
218 **/
Richard Hughescff38bc2016-12-12 12:03:37 +0000219FuPluginData *
220fu_plugin_alloc_data (FuPlugin *plugin, gsize data_sz)
221{
222 FuPluginPrivate *priv = GET_PRIVATE (plugin);
Richard Hughesccd78a92017-01-11 16:57:41 +0000223 g_return_val_if_fail (FU_IS_PLUGIN (plugin), NULL);
Richard Hughes44dee882017-01-11 08:31:10 +0000224 if (priv->data != NULL) {
225 g_critical ("fu_plugin_alloc_data() already used by plugin");
226 return priv->data;
227 }
Richard Hughescff38bc2016-12-12 12:03:37 +0000228 priv->data = g_malloc0 (data_sz);
229 return priv->data;
Richard Hughesd0905142016-03-13 09:46:49 +0000230}
231
Richard Hughes57d18222017-01-10 16:02:59 +0000232/**
233 * fu_plugin_get_usb_context:
234 * @plugin: A #FuPlugin
235 *
236 * Gets the shared USB context that all plugins can use.
237 *
238 * Returns: (transfer none): a #GUsbContext.
239 *
240 * Since: 0.8.0
241 **/
Richard Hughesbc93e4a2016-12-08 17:29:51 +0000242GUsbContext *
243fu_plugin_get_usb_context (FuPlugin *plugin)
244{
Richard Hughescff38bc2016-12-12 12:03:37 +0000245 FuPluginPrivate *priv = GET_PRIVATE (plugin);
Richard Hughesccd78a92017-01-11 16:57:41 +0000246 g_return_val_if_fail (FU_IS_PLUGIN (plugin), NULL);
Richard Hughescff38bc2016-12-12 12:03:37 +0000247 return priv->usb_ctx;
Richard Hughesbc93e4a2016-12-08 17:29:51 +0000248}
249
250void
251fu_plugin_set_usb_context (FuPlugin *plugin, GUsbContext *usb_ctx)
252{
Richard Hughescff38bc2016-12-12 12:03:37 +0000253 FuPluginPrivate *priv = GET_PRIVATE (plugin);
254 g_set_object (&priv->usb_ctx, usb_ctx);
255}
256
Richard Hughes57d18222017-01-10 16:02:59 +0000257/**
258 * fu_plugin_get_enabled:
259 * @plugin: A #FuPlugin
260 *
Richard Hughes4eada342017-10-03 21:20:32 +0100261 * Returns if the plugin is enabled. Plugins may self-disable using
262 * fu_plugin_set_enabled() or can be disabled by the daemon.
Richard Hughes57d18222017-01-10 16:02:59 +0000263 *
264 * Returns: %TRUE if the plugin is currently enabled.
265 *
266 * Since: 0.8.0
267 **/
Richard Hughescff38bc2016-12-12 12:03:37 +0000268gboolean
269fu_plugin_get_enabled (FuPlugin *plugin)
270{
271 FuPluginPrivate *priv = GET_PRIVATE (plugin);
Richard Hughesccd78a92017-01-11 16:57:41 +0000272 g_return_val_if_fail (FU_IS_PLUGIN (plugin), FALSE);
Richard Hughescff38bc2016-12-12 12:03:37 +0000273 return priv->enabled;
Richard Hughesbc93e4a2016-12-08 17:29:51 +0000274}
275
Richard Hughes57d18222017-01-10 16:02:59 +0000276/**
277 * fu_plugin_set_enabled:
278 * @plugin: A #FuPlugin
279 * @enabled: the enabled value
280 *
281 * Enables or disables a plugin. Plugins can self-disable at any point.
282 *
283 * Since: 0.8.0
284 **/
Richard Hughesd0905142016-03-13 09:46:49 +0000285void
Richard Hughescff38bc2016-12-12 12:03:37 +0000286fu_plugin_set_enabled (FuPlugin *plugin, gboolean enabled)
Richard Hughesd0905142016-03-13 09:46:49 +0000287{
Richard Hughescff38bc2016-12-12 12:03:37 +0000288 FuPluginPrivate *priv = GET_PRIVATE (plugin);
Richard Hughesccd78a92017-01-11 16:57:41 +0000289 g_return_if_fail (FU_IS_PLUGIN (plugin));
Richard Hughescff38bc2016-12-12 12:03:37 +0000290 priv->enabled = enabled;
291}
292
293gboolean
294fu_plugin_open (FuPlugin *plugin, const gchar *filename, GError **error)
295{
296 FuPluginPrivate *priv = GET_PRIVATE (plugin);
297 FuPluginInitFunc func = NULL;
298 gchar *str;
299
300 priv->module = g_module_open (filename, 0);
301 if (priv->module == NULL) {
302 g_set_error (error,
303 G_IO_ERROR,
304 G_IO_ERROR_FAILED,
305 "failed to open plugin: %s",
306 g_module_error ());
307 return FALSE;
308 }
309
310 /* set automatically */
311 str = g_strstr_len (filename, -1, "libfu_plugin_");
312 if (str != NULL) {
313 priv->name = g_strdup (str + 13);
314 g_strdelimit (priv->name, ".", '\0');
315 }
Richard Hughesd0905142016-03-13 09:46:49 +0000316
317 /* optional */
Richard Hughescff38bc2016-12-12 12:03:37 +0000318 g_module_symbol (priv->module, "fu_plugin_init", (gpointer *) &func);
319 if (func != NULL) {
320 g_debug ("performing init() on %s", filename);
Richard Hughesd0905142016-03-13 09:46:49 +0000321 func (plugin);
322 }
323
Richard Hughescff38bc2016-12-12 12:03:37 +0000324 return TRUE;
325}
326
Richard Hughes57d18222017-01-10 16:02:59 +0000327/**
328 * fu_plugin_device_add:
329 * @plugin: A #FuPlugin
330 * @device: A #FuDevice
331 *
332 * Asks the daemon to add a device to the exported list. If this device ID
333 * has already been added by a different plugin then this request will be
334 * ignored.
335 *
336 * Plugins should use fu_plugin_device_add_delay() if they are not capable of
337 * actually flashing an image to the hardware so that higher-priority plugins
338 * can add the device themselves.
339 *
340 * Since: 0.8.0
341 **/
Richard Hughescff38bc2016-12-12 12:03:37 +0000342void
343fu_plugin_device_add (FuPlugin *plugin, FuDevice *device)
344{
Richard Hughesccd78a92017-01-11 16:57:41 +0000345 g_return_if_fail (FU_IS_PLUGIN (plugin));
346 g_return_if_fail (FU_IS_DEVICE (device));
347
Richard Hughescff38bc2016-12-12 12:03:37 +0000348 g_debug ("emit added from %s: %s",
349 fu_plugin_get_name (plugin),
350 fu_device_get_id (device));
351 fu_device_set_created (device, (guint64) g_get_real_time () / G_USEC_PER_SEC);
352 fu_device_set_plugin (device, fu_plugin_get_name (plugin));
353 g_signal_emit (plugin, signals[SIGNAL_DEVICE_ADDED], 0, device);
354}
355
Richard Hughese1fd34d2017-08-24 14:19:51 +0100356/**
357 * fu_plugin_device_register:
358 * @plugin: A #FuPlugin
359 * @device: A #FuDevice
360 *
361 * Registers the device with other plugins so they can set metadata.
362 *
363 * Plugins do not have to call this manually as this is done automatically
364 * when using fu_plugin_device_add(). They may wish to use this manually
365 * if for intance the coldplug should be ignored based on the metadata
366 * set from other plugins.
367 *
368 * Since: 0.9.7
369 **/
370void
371fu_plugin_device_register (FuPlugin *plugin, FuDevice *device)
372{
373 g_return_if_fail (FU_IS_PLUGIN (plugin));
374 g_return_if_fail (FU_IS_DEVICE (device));
375
376 g_debug ("emit device-register from %s: %s",
377 fu_plugin_get_name (plugin),
378 fu_device_get_id (device));
379 g_signal_emit (plugin, signals[SIGNAL_DEVICE_REGISTER], 0, device);
380}
381
Richard Hughesae3d65f2016-12-16 09:38:01 +0000382typedef struct {
383 FuPlugin *plugin;
384 FuDevice *device;
385 guint timeout_id;
Richard Hughesd4184cf2016-12-21 16:05:17 +0000386 GHashTable *devices;
Richard Hughesae3d65f2016-12-16 09:38:01 +0000387} FuPluginHelper;
388
389static void
390fu_plugin_helper_free (FuPluginHelper *helper)
391{
392 g_object_unref (helper->plugin);
393 g_object_unref (helper->device);
Richard Hughesd4184cf2016-12-21 16:05:17 +0000394 g_hash_table_unref (helper->devices);
Richard Hughesae3d65f2016-12-16 09:38:01 +0000395 g_free (helper);
396}
397
398static gboolean
399fu_plugin_device_add_delay_cb (gpointer user_data)
400{
401 FuPluginHelper *helper = (FuPluginHelper *) user_data;
402 fu_plugin_device_add (helper->plugin, helper->device);
Richard Hughes0dec2742017-09-18 11:07:39 +0100403 g_hash_table_remove (helper->devices, helper->device);
Richard Hughesae3d65f2016-12-16 09:38:01 +0000404 fu_plugin_helper_free (helper);
405 return FALSE;
406}
407
Richard Hughes57d18222017-01-10 16:02:59 +0000408/**
Richard Hughesf0a799e2017-01-17 20:13:30 +0000409 * fu_plugin_has_device_delay:
410 * @plugin: A #FuPlugin
411 *
412 * Returns if the device has a pending device that is waiting to be added.
413 *
414 * Returns: %TRUE if a device is waiting to be added
415 *
416 * Since: 0.8.0
417 **/
418gboolean
419fu_plugin_has_device_delay (FuPlugin *plugin)
420{
421 FuPluginPrivate *priv = GET_PRIVATE (plugin);
422 return g_hash_table_size (priv->devices_delay) > 0;
423}
424
425/**
Richard Hughes57d18222017-01-10 16:02:59 +0000426 * fu_plugin_device_add_delay:
427 * @plugin: A #FuPlugin
428 * @device: A #FuDevice
429 *
430 * Asks the daemon to add a device to the exported list after a small delay.
431 *
432 * Since: 0.8.0
433 **/
Richard Hughesae3d65f2016-12-16 09:38:01 +0000434void
435fu_plugin_device_add_delay (FuPlugin *plugin, FuDevice *device)
436{
437 FuPluginPrivate *priv = GET_PRIVATE (plugin);
438 FuPluginHelper *helper;
Richard Hughesccd78a92017-01-11 16:57:41 +0000439
440 g_return_if_fail (FU_IS_PLUGIN (plugin));
441 g_return_if_fail (FU_IS_DEVICE (device));
442
Richard Hughes6ad951d2017-02-03 10:12:12 +0000443 /* already waiting for add */
444 helper = g_hash_table_lookup (priv->devices_delay, device);
445 if (helper != NULL) {
Richard Hughes3cca1c62017-07-21 13:38:20 +0100446 g_debug ("ignoring add-delay as device %s already pending",
447 fu_device_get_id (device));
Richard Hughes6ad951d2017-02-03 10:12:12 +0000448 return;
449 }
450
451 /* add after a small delay */
Richard Hughesae3d65f2016-12-16 09:38:01 +0000452 g_debug ("waiting a small time for other plugins");
453 helper = g_new0 (FuPluginHelper, 1);
454 helper->plugin = g_object_ref (plugin);
455 helper->device = g_object_ref (device);
456 helper->timeout_id = g_timeout_add (500, fu_plugin_device_add_delay_cb, helper);
Richard Hughesd4184cf2016-12-21 16:05:17 +0000457 helper->devices = g_hash_table_ref (priv->devices_delay);
458 g_hash_table_insert (helper->devices, device, helper);
Richard Hughesae3d65f2016-12-16 09:38:01 +0000459}
460
Richard Hughes57d18222017-01-10 16:02:59 +0000461/**
Richard Hughes4eada342017-10-03 21:20:32 +0100462 * fu_plugin_device_remove:
Richard Hughes57d18222017-01-10 16:02:59 +0000463 * @plugin: A #FuPlugin
464 * @device: A #FuDevice
465 *
466 * Asks the daemon to remove a device from the exported list.
467 *
468 * Since: 0.8.0
469 **/
Richard Hughescff38bc2016-12-12 12:03:37 +0000470void
471fu_plugin_device_remove (FuPlugin *plugin, FuDevice *device)
472{
Richard Hughesae3d65f2016-12-16 09:38:01 +0000473 FuPluginPrivate *priv = GET_PRIVATE (plugin);
474 FuPluginHelper *helper;
475
Richard Hughesccd78a92017-01-11 16:57:41 +0000476 g_return_if_fail (FU_IS_PLUGIN (plugin));
477 g_return_if_fail (FU_IS_DEVICE (device));
478
Richard Hughesae3d65f2016-12-16 09:38:01 +0000479 /* waiting for add */
480 helper = g_hash_table_lookup (priv->devices_delay, device);
481 if (helper != NULL) {
482 g_debug ("ignoring remove from delayed addition");
483 g_source_remove (helper->timeout_id);
Richard Hughesd4184cf2016-12-21 16:05:17 +0000484 g_hash_table_remove (priv->devices_delay, helper->device);
Richard Hughesae3d65f2016-12-16 09:38:01 +0000485 fu_plugin_helper_free (helper);
486 return;
487 }
488
Richard Hughescff38bc2016-12-12 12:03:37 +0000489 g_debug ("emit removed from %s: %s",
490 fu_plugin_get_name (plugin),
491 fu_device_get_id (device));
492 g_signal_emit (plugin, signals[SIGNAL_DEVICE_REMOVED], 0, device);
493}
494
Richard Hughes57d18222017-01-10 16:02:59 +0000495/**
Richard Hughes2de8f132018-01-17 09:12:02 +0000496 * fu_plugin_request_recoldplug:
Richard Hughes362d6d72017-01-07 21:42:14 +0000497 * @plugin: A #FuPlugin
498 *
499 * Ask all the plugins to coldplug all devices, which will include the prepare()
500 * and cleanup() phases. Duplicate devices added will be ignored.
501 *
502 * Since: 0.8.0
503 **/
504void
Richard Hughes2de8f132018-01-17 09:12:02 +0000505fu_plugin_request_recoldplug (FuPlugin *plugin)
Richard Hughes362d6d72017-01-07 21:42:14 +0000506{
Richard Hughesccd78a92017-01-11 16:57:41 +0000507 g_return_if_fail (FU_IS_PLUGIN (plugin));
Richard Hughes362d6d72017-01-07 21:42:14 +0000508 g_signal_emit (plugin, signals[SIGNAL_RECOLDPLUG], 0);
509}
510
Richard Hughesb0829032017-01-10 09:27:08 +0000511/**
Richard Hughesb8f8db22017-04-25 15:56:00 +0100512 * fu_plugin_check_hwid:
513 * @plugin: A #FuPlugin
Richard Hughes4eada342017-10-03 21:20:32 +0100514 * @hwid: A Hardware ID GUID, e.g. `6de5d951-d755-576b-bd09-c5cf66b27234`
Richard Hughesb8f8db22017-04-25 15:56:00 +0100515 *
Richard Hughesd7704d42017-08-08 20:29:09 +0100516 * Checks to see if a specific GUID exists. All hardware IDs on a
Richard Hughesb8f8db22017-04-25 15:56:00 +0100517 * specific system can be shown using the `fwupdmgr hwids` command.
518 *
Richard Hughes4eada342017-10-03 21:20:32 +0100519 * Returns: %TRUE if the HwId is found on the system.
520 *
Richard Hughesb8f8db22017-04-25 15:56:00 +0100521 * Since: 0.9.1
522 **/
523gboolean
524fu_plugin_check_hwid (FuPlugin *plugin, const gchar *hwid)
525{
526 FuPluginPrivate *priv = GET_PRIVATE (plugin);
527 if (priv->hwids == NULL)
528 return FALSE;
Richard Hughesd7704d42017-08-08 20:29:09 +0100529 return fu_hwids_has_guid (priv->hwids, hwid);
530}
531
532/**
Richard Hughes1354ea92017-09-19 15:58:31 +0100533 * fu_plugin_check_supported:
534 * @plugin: A #FuPlugin
Richard Hughes4eada342017-10-03 21:20:32 +0100535 * @guid: A Hardware ID GUID, e.g. `6de5d951-d755-576b-bd09-c5cf66b27234`
Richard Hughes1354ea92017-09-19 15:58:31 +0100536 *
537 * Checks to see if a specific device GUID is supported, i.e. available in the
538 * AppStream metadata.
539 *
Richard Hughes4eada342017-10-03 21:20:32 +0100540 * Returns: %TRUE if the device is supported.
541 *
Richard Hughes1354ea92017-09-19 15:58:31 +0100542 * Since: 1.0.0
543 **/
544gboolean
545fu_plugin_check_supported (FuPlugin *plugin, const gchar *guid)
546{
547 FuPluginPrivate *priv = GET_PRIVATE (plugin);
548 if (priv->supported_guids == NULL)
549 return FALSE;
550 for (guint i = 0; i < priv->supported_guids->len; i++) {
551 const gchar *guid_tmp = g_ptr_array_index (priv->supported_guids, i);
552 if (g_strcmp0 (guid, guid_tmp) == 0)
553 return TRUE;
554 }
555 return FALSE;
556}
557
558/**
Richard Hughesd7704d42017-08-08 20:29:09 +0100559 * fu_plugin_get_dmi_value:
560 * @plugin: A #FuPlugin
Richard Hughes4eada342017-10-03 21:20:32 +0100561 * @dmi_id: A DMI ID, e.g. `BiosVersion`
Richard Hughesd7704d42017-08-08 20:29:09 +0100562 *
563 * Gets a hardware DMI value.
564 *
Richard Hughes4eada342017-10-03 21:20:32 +0100565 * Returns: The string, or %NULL
566 *
Richard Hughesd7704d42017-08-08 20:29:09 +0100567 * Since: 0.9.7
568 **/
569const gchar *
570fu_plugin_get_dmi_value (FuPlugin *plugin, const gchar *dmi_id)
571{
572 FuPluginPrivate *priv = GET_PRIVATE (plugin);
573 if (priv->hwids == NULL)
Richard Hughes7ef96b82017-08-23 18:28:24 +0100574 return NULL;
Richard Hughesd7704d42017-08-08 20:29:09 +0100575 return fu_hwids_get_value (priv->hwids, dmi_id);
Richard Hughesb8f8db22017-04-25 15:56:00 +0100576}
577
Richard Hughes49e5e052017-09-03 12:15:41 +0100578/**
579 * fu_plugin_get_smbios_string:
580 * @plugin: A #FuPlugin
581 * @structure_type: A SMBIOS structure type, e.g. %FU_SMBIOS_STRUCTURE_TYPE_BIOS
582 * @offset: A SMBIOS offset
583 *
584 * Gets a hardware SMBIOS string.
585 *
586 * The @type and @offset can be referenced from the DMTF SMBIOS specification:
587 * https://www.dmtf.org/sites/default/files/standards/documents/DSP0134_3.1.1.pdf
588 *
Richard Hughes4eada342017-10-03 21:20:32 +0100589 * Returns: A string, or %NULL
590 *
Richard Hughes49e5e052017-09-03 12:15:41 +0100591 * Since: 0.9.8
592 **/
593const gchar *
594fu_plugin_get_smbios_string (FuPlugin *plugin, guint8 structure_type, guint8 offset)
595{
596 FuPluginPrivate *priv = GET_PRIVATE (plugin);
597 if (priv->smbios == NULL)
598 return NULL;
599 return fu_smbios_get_string (priv->smbios, structure_type, offset, NULL);
600}
601
602/**
603 * fu_plugin_get_smbios_data:
604 * @plugin: A #FuPlugin
605 * @structure_type: A SMBIOS structure type, e.g. %FU_SMBIOS_STRUCTURE_TYPE_BIOS
606 *
607 * Gets a hardware SMBIOS data.
608 *
Richard Hughes4eada342017-10-03 21:20:32 +0100609 * Returns: (transfer none): A #GBytes, or %NULL
610 *
Richard Hughes49e5e052017-09-03 12:15:41 +0100611 * Since: 0.9.8
612 **/
613GBytes *
614fu_plugin_get_smbios_data (FuPlugin *plugin, guint8 structure_type)
615{
616 FuPluginPrivate *priv = GET_PRIVATE (plugin);
617 if (priv->smbios == NULL)
618 return NULL;
619 return fu_smbios_get_data (priv->smbios, structure_type, NULL);
620}
621
Richard Hughesb8f8db22017-04-25 15:56:00 +0100622void
Richard Hughesd7704d42017-08-08 20:29:09 +0100623fu_plugin_set_hwids (FuPlugin *plugin, FuHwids *hwids)
Richard Hughesb8f8db22017-04-25 15:56:00 +0100624{
625 FuPluginPrivate *priv = GET_PRIVATE (plugin);
Richard Hughesd7704d42017-08-08 20:29:09 +0100626 g_set_object (&priv->hwids, hwids);
Richard Hughesb8f8db22017-04-25 15:56:00 +0100627}
628
Richard Hughes49e5e052017-09-03 12:15:41 +0100629void
Richard Hughes1354ea92017-09-19 15:58:31 +0100630fu_plugin_set_supported (FuPlugin *plugin, GPtrArray *supported_guids)
631{
632 FuPluginPrivate *priv = GET_PRIVATE (plugin);
633 if (priv->supported_guids != NULL)
634 g_ptr_array_unref (priv->supported_guids);
635 priv->supported_guids = g_ptr_array_ref (supported_guids);
636}
637
Richard Hughes9c028f02017-10-28 21:14:28 +0100638void
639fu_plugin_set_quirks (FuPlugin *plugin, FuQuirks *quirks)
640{
641 FuPluginPrivate *priv = GET_PRIVATE (plugin);
642 g_set_object (&priv->quirks, quirks);
643}
644
645/**
646 * fu_plugin_get_quirks:
647 * @plugin: A #FuPlugin
648 *
649 * Returns the hardware database object. This can be used to discover device
650 * quirks or other device-specific settings.
651 *
652 * Returns: (transfer none): a #FuQuirks, or %NULL if not set
653 *
654 * Since: 1.0.1
655 **/
656FuQuirks *
657fu_plugin_get_quirks (FuPlugin *plugin)
658{
659 FuPluginPrivate *priv = GET_PRIVATE (plugin);
660 return priv->quirks;
661}
662
663/**
664 * fu_plugin_lookup_quirk_by_id:
665 * @plugin: A #FuPlugin
666 * @prefix: A string prefix that matches the quirks file basename, e.g. "dfu-quirks"
667 * @id: An ID to match the entry, e.g. "012345"
668 *
669 * Looks up an entry in the hardware database using a string value.
670 *
671 * Returns: (transfer none): values from the database, or %NULL if not found
672 *
673 * Since: 1.0.1
674 **/
675const gchar *
676fu_plugin_lookup_quirk_by_id (FuPlugin *plugin, const gchar *prefix, const gchar *id)
677{
678 FuPluginPrivate *priv = GET_PRIVATE (plugin);
679 g_return_val_if_fail (FU_IS_PLUGIN (plugin), NULL);
680
681 /* wildcard */
682 if (g_strstr_len (id, -1, "*") != NULL)
683 return fu_quirks_lookup_by_glob (priv->quirks, prefix, id);
684
685 /* exact ID */
686 return fu_quirks_lookup_by_id (priv->quirks, prefix, id);
687}
688
689/**
690 * fu_plugin_lookup_quirk_by_usb_device:
691 * @plugin: A #FuPlugin
692 * @prefix: A string prefix that matches the quirks file basename, e.g. "dfu-quirks"
693 * @dev: A #GUsbDevice
694 *
695 * Looks up an entry in the hardware database using various keys generated
696 * from @dev.
697 *
698 * Returns: (transfer none): values from the database, or %NULL if not found
699 *
700 * Since: 1.0.1
701 **/
702const gchar *
703fu_plugin_lookup_quirk_by_usb_device (FuPlugin *plugin, const gchar *prefix, GUsbDevice *dev)
704{
705 FuPluginPrivate *priv = GET_PRIVATE (plugin);
706 g_return_val_if_fail (FU_IS_PLUGIN (plugin), NULL);
707 return fu_quirks_lookup_by_usb_device (priv->quirks, prefix, dev);
708}
709
Richard Hughes1354ea92017-09-19 15:58:31 +0100710/**
711 * fu_plugin_get_supported:
712 * @plugin: A #FuPlugin
713 *
714 * Gets all the device GUIDs supported by the daemon.
715 *
Richard Hughes4eada342017-10-03 21:20:32 +0100716 * Returns: (element-type utf8) (transfer none): GUIDs
717 *
Richard Hughes1354ea92017-09-19 15:58:31 +0100718 * Since: 1.0.0
719 **/
720GPtrArray *
721fu_plugin_get_supported (FuPlugin *plugin)
722{
723 FuPluginPrivate *priv = GET_PRIVATE (plugin);
724 return priv->supported_guids;
725}
726
727void
Richard Hughes49e5e052017-09-03 12:15:41 +0100728fu_plugin_set_smbios (FuPlugin *plugin, FuSmbios *smbios)
729{
730 FuPluginPrivate *priv = GET_PRIVATE (plugin);
731 g_set_object (&priv->smbios, smbios);
732}
733
Richard Hughesb8f8db22017-04-25 15:56:00 +0100734/**
Richard Hughesb0829032017-01-10 09:27:08 +0000735 * fu_plugin_set_coldplug_delay:
736 * @plugin: A #FuPlugin
737 * @duration: A delay in milliseconds
738 *
739 * Set the minimum time that should be waited inbetween the call to
740 * fu_plugin_coldplug_prepare() and fu_plugin_coldplug(). This is usually going
741 * to be the minimum hardware initialisation time from a datasheet.
742 *
743 * It is better to use this function rather than using a sleep() in the plugin
744 * itself as then only one delay is done in the daemon rather than waiting for
745 * each coldplug prepare in a serial way.
746 *
747 * Additionally, very long delays should be avoided as the daemon will be
748 * blocked from processing requests whilst the coldplug delay is being
749 * performed.
750 *
751 * Since: 0.8.0
752 **/
753void
754fu_plugin_set_coldplug_delay (FuPlugin *plugin, guint duration)
755{
756 g_return_if_fail (FU_IS_PLUGIN (plugin));
757 g_return_if_fail (duration > 0);
758
759 /* check sanity */
760 if (duration > FU_PLUGIN_COLDPLUG_DELAY_MAXIMUM) {
761 g_warning ("duration of %ums is crazy, truncating to %ums",
762 duration,
763 FU_PLUGIN_COLDPLUG_DELAY_MAXIMUM);
764 duration = FU_PLUGIN_COLDPLUG_DELAY_MAXIMUM;
765 }
766
767 /* emit */
768 g_signal_emit (plugin, signals[SIGNAL_SET_COLDPLUG_DELAY], 0, duration);
769}
770
Richard Hughesd0905142016-03-13 09:46:49 +0000771gboolean
Richard Hughescff38bc2016-12-12 12:03:37 +0000772fu_plugin_runner_startup (FuPlugin *plugin, GError **error)
Richard Hughesd0905142016-03-13 09:46:49 +0000773{
Richard Hughescff38bc2016-12-12 12:03:37 +0000774 FuPluginPrivate *priv = GET_PRIVATE (plugin);
Richard Hughes7b8b2022016-12-12 16:15:03 +0000775 FuPluginStartupFunc func = NULL;
Richard Hughesd0905142016-03-13 09:46:49 +0000776
777 /* not enabled */
Richard Hughescff38bc2016-12-12 12:03:37 +0000778 if (!priv->enabled)
Richard Hughesd0905142016-03-13 09:46:49 +0000779 return TRUE;
780
Richard Hughes639da472018-01-06 22:35:04 +0000781 /* no object loaded */
782 if (priv->module == NULL)
783 return TRUE;
784
Richard Hughesd0905142016-03-13 09:46:49 +0000785 /* optional */
Richard Hughescff38bc2016-12-12 12:03:37 +0000786 g_module_symbol (priv->module, "fu_plugin_startup", (gpointer *) &func);
787 if (func == NULL)
Richard Hughesd0905142016-03-13 09:46:49 +0000788 return TRUE;
Richard Hughescff38bc2016-12-12 12:03:37 +0000789 g_debug ("performing startup() on %s", priv->name);
Richard Hughesd0905142016-03-13 09:46:49 +0000790 if (!func (plugin, error)) {
Richard Hughescff38bc2016-12-12 12:03:37 +0000791 g_prefix_error (error, "failed to startup %s: ", priv->name);
792 return FALSE;
793 }
794 return TRUE;
795}
796
797static gboolean
798fu_plugin_runner_offline_invalidate (GError **error)
799{
800 g_autoptr(GError) error_local = NULL;
801 g_autoptr(GFile) file1 = NULL;
802
803 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
804
805 file1 = g_file_new_for_path (FU_OFFLINE_TRIGGER_FILENAME);
806 if (!g_file_query_exists (file1, NULL))
807 return TRUE;
808 if (!g_file_delete (file1, NULL, &error_local)) {
809 g_set_error (error,
810 FWUPD_ERROR,
811 FWUPD_ERROR_INTERNAL,
812 "Cannot delete %s: %s",
813 FU_OFFLINE_TRIGGER_FILENAME,
814 error_local->message);
815 return FALSE;
816 }
817 return TRUE;
818}
819
820static gboolean
821fu_plugin_runner_offline_setup (GError **error)
822{
823 gint rc;
824
825 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
826
827 /* create symlink for the systemd-system-update-generator */
828 rc = symlink ("/var/lib/fwupd", FU_OFFLINE_TRIGGER_FILENAME);
829 if (rc < 0) {
830 g_set_error (error,
831 FWUPD_ERROR,
832 FWUPD_ERROR_INTERNAL,
833 "Failed to create symlink %s to %s: %s",
834 FU_OFFLINE_TRIGGER_FILENAME,
835 "/var/lib", strerror (errno));
Richard Hughesd0905142016-03-13 09:46:49 +0000836 return FALSE;
837 }
838 return TRUE;
839}
840
Richard Hughes0d7fdb32017-11-11 20:23:14 +0000841static gboolean
842fu_plugin_runner_device_generic (FuPlugin *plugin, FuDevice *device,
843 const gchar *symbol_name, GError **error)
844{
845 FuPluginPrivate *priv = GET_PRIVATE (plugin);
846 FuPluginDeviceFunc func = NULL;
847
848 /* not enabled */
849 if (!priv->enabled)
850 return TRUE;
851
Richard Hughesd3d96cc2017-11-14 11:34:33 +0000852 /* no object loaded */
853 if (priv->module == NULL)
854 return TRUE;
855
Richard Hughes0d7fdb32017-11-11 20:23:14 +0000856 /* optional */
857 g_module_symbol (priv->module, symbol_name, (gpointer *) &func);
858 if (func == NULL)
859 return TRUE;
860 g_debug ("performing %s() on %s", symbol_name + 10, priv->name);
861 if (!func (plugin, device, error)) {
Richard Hughes83e54e42018-01-31 23:27:30 +0000862 g_prefix_error (error, "failed to run %s() on %s: ",
Richard Hughes0d7fdb32017-11-11 20:23:14 +0000863 symbol_name + 10,
Richard Hughes0d7fdb32017-11-11 20:23:14 +0000864 priv->name);
865 return FALSE;
866 }
867 return TRUE;
868}
869
Richard Hughesd0905142016-03-13 09:46:49 +0000870gboolean
Richard Hughescff38bc2016-12-12 12:03:37 +0000871fu_plugin_runner_coldplug (FuPlugin *plugin, GError **error)
Richard Hughesd0905142016-03-13 09:46:49 +0000872{
Richard Hughescff38bc2016-12-12 12:03:37 +0000873 FuPluginPrivate *priv = GET_PRIVATE (plugin);
Richard Hughes7b8b2022016-12-12 16:15:03 +0000874 FuPluginStartupFunc func = NULL;
Richard Hughesd0905142016-03-13 09:46:49 +0000875
876 /* not enabled */
Richard Hughescff38bc2016-12-12 12:03:37 +0000877 if (!priv->enabled)
Richard Hughesd0905142016-03-13 09:46:49 +0000878 return TRUE;
879
Richard Hughes639da472018-01-06 22:35:04 +0000880 /* no object loaded */
881 if (priv->module == NULL)
882 return TRUE;
883
Richard Hughesd0905142016-03-13 09:46:49 +0000884 /* optional */
Richard Hughescff38bc2016-12-12 12:03:37 +0000885 g_module_symbol (priv->module, "fu_plugin_coldplug", (gpointer *) &func);
886 if (func == NULL)
Richard Hughesd0905142016-03-13 09:46:49 +0000887 return TRUE;
Richard Hughescff38bc2016-12-12 12:03:37 +0000888 g_debug ("performing coldplug() on %s", priv->name);
889 if (!func (plugin, error)) {
890 g_prefix_error (error, "failed to coldplug %s: ", priv->name);
891 return FALSE;
892 }
893 return TRUE;
894}
895
Richard Hughes7b8b2022016-12-12 16:15:03 +0000896gboolean
Richard Hughes2de8f132018-01-17 09:12:02 +0000897fu_plugin_runner_recoldplug (FuPlugin *plugin, GError **error)
898{
899 FuPluginPrivate *priv = GET_PRIVATE (plugin);
900 FuPluginStartupFunc func = NULL;
901
902 /* not enabled */
903 if (!priv->enabled)
904 return TRUE;
905
906 /* no object loaded */
907 if (priv->module == NULL)
908 return TRUE;
909
910 /* optional */
911 g_module_symbol (priv->module, "fu_plugin_recoldplug", (gpointer *) &func);
912 if (func == NULL)
913 return TRUE;
914 g_debug ("performing recoldplug() on %s", priv->name);
915 if (!func (plugin, error)) {
916 g_prefix_error (error, "failed to recoldplug %s: ", priv->name);
917 return FALSE;
918 }
919 return TRUE;
920}
921
922gboolean
Richard Hughes46487c92017-01-07 21:26:34 +0000923fu_plugin_runner_coldplug_prepare (FuPlugin *plugin, GError **error)
924{
925 FuPluginPrivate *priv = GET_PRIVATE (plugin);
926 FuPluginStartupFunc func = NULL;
927
928 /* not enabled */
929 if (!priv->enabled)
930 return TRUE;
931
Richard Hughes639da472018-01-06 22:35:04 +0000932 /* no object loaded */
933 if (priv->module == NULL)
934 return TRUE;
935
Richard Hughes46487c92017-01-07 21:26:34 +0000936 /* optional */
937 g_module_symbol (priv->module, "fu_plugin_coldplug_prepare", (gpointer *) &func);
938 if (func == NULL)
939 return TRUE;
940 g_debug ("performing coldplug_prepare() on %s", priv->name);
941 if (!func (plugin, error)) {
942 g_prefix_error (error, "failed to prepare for coldplug %s: ", priv->name);
943 return FALSE;
944 }
945 return TRUE;
946}
947
948gboolean
949fu_plugin_runner_coldplug_cleanup (FuPlugin *plugin, GError **error)
950{
951 FuPluginPrivate *priv = GET_PRIVATE (plugin);
952 FuPluginStartupFunc func = NULL;
953
954 /* not enabled */
955 if (!priv->enabled)
956 return TRUE;
957
Richard Hughes639da472018-01-06 22:35:04 +0000958 /* no object loaded */
959 if (priv->module == NULL)
960 return TRUE;
961
Richard Hughes46487c92017-01-07 21:26:34 +0000962 /* optional */
963 g_module_symbol (priv->module, "fu_plugin_coldplug_cleanup", (gpointer *) &func);
964 if (func == NULL)
965 return TRUE;
966 g_debug ("performing coldplug_cleanup() on %s", priv->name);
967 if (!func (plugin, error)) {
968 g_prefix_error (error, "failed to cleanup coldplug %s: ", priv->name);
969 return FALSE;
970 }
971 return TRUE;
972}
973
974gboolean
Richard Hughes7b8b2022016-12-12 16:15:03 +0000975fu_plugin_runner_update_prepare (FuPlugin *plugin, FuDevice *device, GError **error)
976{
Richard Hughes0d7fdb32017-11-11 20:23:14 +0000977 return fu_plugin_runner_device_generic (plugin, device,
978 "fu_plugin_update_prepare", error);
Richard Hughes7b8b2022016-12-12 16:15:03 +0000979}
980
981gboolean
982fu_plugin_runner_update_cleanup (FuPlugin *plugin, FuDevice *device, GError **error)
983{
Richard Hughes0d7fdb32017-11-11 20:23:14 +0000984 return fu_plugin_runner_device_generic (plugin, device,
985 "fu_plugin_update_cleanup", error);
986}
Richard Hughes7b8b2022016-12-12 16:15:03 +0000987
Richard Hughes0d7fdb32017-11-11 20:23:14 +0000988gboolean
989fu_plugin_runner_update_attach (FuPlugin *plugin, FuDevice *device, GError **error)
990{
991 return fu_plugin_runner_device_generic (plugin, device,
992 "fu_plugin_update_attach", error);
993}
Richard Hughes7b8b2022016-12-12 16:15:03 +0000994
Richard Hughes0d7fdb32017-11-11 20:23:14 +0000995gboolean
996fu_plugin_runner_update_detach (FuPlugin *plugin, FuDevice *device, GError **error)
997{
998 return fu_plugin_runner_device_generic (plugin, device,
999 "fu_plugin_update_detach", error);
1000}
1001
1002gboolean
1003fu_plugin_runner_update_reload (FuPlugin *plugin, FuDevice *device, GError **error)
1004{
1005 return fu_plugin_runner_device_generic (plugin, device,
1006 "fu_plugin_update_reload", error);
Richard Hughes7b8b2022016-12-12 16:15:03 +00001007}
1008
Richard Hughes104f6512017-11-24 11:44:57 +00001009gboolean
1010fu_plugin_runner_usb_device_added (FuPlugin *plugin, GUsbDevice *usb_device, GError **error)
1011{
1012 FuPluginPrivate *priv = GET_PRIVATE (plugin);
1013 FuPluginUsbDeviceAddedFunc func = NULL;
1014
1015 /* not enabled */
1016 if (!priv->enabled)
1017 return TRUE;
Richard Hughes639da472018-01-06 22:35:04 +00001018
1019 /* no object loaded */
Richard Hughes104f6512017-11-24 11:44:57 +00001020 if (priv->module == NULL)
1021 return TRUE;
1022
1023 /* optional */
1024 g_module_symbol (priv->module, "fu_plugin_usb_device_added", (gpointer *) &func);
1025 if (func != NULL) {
1026 g_debug ("performing usb_device_added() on %s", priv->name);
1027 return func (plugin, usb_device, error);
1028 }
1029 return TRUE;
1030}
1031
Richard Hughese1fd34d2017-08-24 14:19:51 +01001032void
1033fu_plugin_runner_device_register (FuPlugin *plugin, FuDevice *device)
1034{
1035 FuPluginPrivate *priv = GET_PRIVATE (plugin);
1036 FuPluginDeviceRegisterFunc func = NULL;
1037
1038 /* not enabled */
1039 if (!priv->enabled)
1040 return;
Richard Hughes34834102017-11-21 21:55:00 +00001041 if (priv->module == NULL)
1042 return;
Richard Hughese1fd34d2017-08-24 14:19:51 +01001043
1044 /* optional */
1045 g_module_symbol (priv->module, "fu_plugin_device_registered", (gpointer *) &func);
1046 if (func != NULL) {
1047 g_debug ("performing device_added() on %s", priv->name);
1048 func (plugin, device);
1049 }
1050}
1051
Richard Hughescff38bc2016-12-12 12:03:37 +00001052static gboolean
1053fu_plugin_runner_schedule_update (FuPlugin *plugin,
1054 FuDevice *device,
1055 GBytes *blob_cab,
1056 GError **error)
1057{
Richard Hughes38fb56c2018-02-01 22:07:52 +00001058 FwupdRelease *release;
Richard Hughescff38bc2016-12-12 12:03:37 +00001059 gchar tmpname[] = {"XXXXXX.cap"};
1060 g_autofree gchar *dirname = NULL;
1061 g_autofree gchar *filename = NULL;
Richard Hughes68982c62017-09-13 15:40:14 +01001062 g_autoptr(FuDevice) res_tmp = NULL;
Richard Hughes780ef3f2018-01-12 16:20:31 +00001063 g_autoptr(FuHistory) history = NULL;
Richard Hughes38fb56c2018-02-01 22:07:52 +00001064 g_autoptr(FwupdRelease) release_tmp = fwupd_release_new ();
Richard Hughescff38bc2016-12-12 12:03:37 +00001065 g_autoptr(GFile) file = NULL;
1066
1067 /* id already exists */
Richard Hughes780ef3f2018-01-12 16:20:31 +00001068 history = fu_history_new ();
Richard Hughes0b9d9962018-01-12 16:31:28 +00001069 res_tmp = fu_history_get_device_by_id (history, fu_device_get_id (device), NULL);
Richard Hughescff38bc2016-12-12 12:03:37 +00001070 if (res_tmp != NULL) {
1071 g_set_error (error,
1072 FWUPD_ERROR,
1073 FWUPD_ERROR_ALREADY_PENDING,
1074 "%s is already scheduled to be updated",
1075 fu_device_get_id (device));
1076 return FALSE;
1077 }
1078
1079 /* create directory */
1080 dirname = g_build_filename (LOCALSTATEDIR, "lib", "fwupd", NULL);
1081 file = g_file_new_for_path (dirname);
1082 if (!g_file_query_exists (file, NULL)) {
1083 if (!g_file_make_directory_with_parents (file, NULL, error))
1084 return FALSE;
1085 }
1086
1087 /* get a random filename */
1088 for (guint i = 0; i < 6; i++)
1089 tmpname[i] = (gchar) g_random_int_range ('A', 'Z');
1090 filename = g_build_filename (dirname, tmpname, NULL);
1091
1092 /* just copy to the temp file */
Richard Hughes23135eb2017-11-30 21:01:25 +00001093 fu_device_set_status (device, FWUPD_STATUS_SCHEDULING);
Richard Hughescff38bc2016-12-12 12:03:37 +00001094 if (!g_file_set_contents (filename,
1095 g_bytes_get_data (blob_cab, NULL),
1096 (gssize) g_bytes_get_size (blob_cab),
1097 error))
1098 return FALSE;
1099
1100 /* schedule for next boot */
1101 g_debug ("schedule %s to be installed to %s on next boot",
1102 filename, fu_device_get_id (device));
Richard Hughes38fb56c2018-02-01 22:07:52 +00001103 release = fu_device_get_release_default (device);
1104 fwupd_release_set_version (release_tmp, fwupd_release_get_version (release));
1105 fwupd_release_set_filename (release_tmp, filename);
Richard Hughescff38bc2016-12-12 12:03:37 +00001106
1107 /* add to database */
Richard Hughes3e90a582018-01-06 22:38:09 +00001108 fu_device_set_update_state (device, FWUPD_UPDATE_STATE_PENDING);
Richard Hughes38fb56c2018-02-01 22:07:52 +00001109 if (!fu_history_add_device (history, device, release_tmp, error))
Richard Hughescff38bc2016-12-12 12:03:37 +00001110 return FALSE;
1111
1112 /* next boot we run offline */
1113 return fu_plugin_runner_offline_setup (error);
1114}
1115
1116gboolean
1117fu_plugin_runner_verify (FuPlugin *plugin,
1118 FuDevice *device,
1119 FuPluginVerifyFlags flags,
1120 GError **error)
1121{
1122 FuPluginPrivate *priv = GET_PRIVATE (plugin);
Richard Hughes7b8b2022016-12-12 16:15:03 +00001123 FuPluginVerifyFunc func = NULL;
Richard Hughesababbb72017-06-15 20:18:36 +01001124 GPtrArray *checksums;
Richard Hughescff38bc2016-12-12 12:03:37 +00001125
1126 /* not enabled */
1127 if (!priv->enabled)
1128 return TRUE;
1129
Richard Hughes639da472018-01-06 22:35:04 +00001130 /* no object loaded */
1131 if (priv->module == NULL)
1132 return TRUE;
1133
Richard Hughesababbb72017-06-15 20:18:36 +01001134 /* clear any existing verification checksums */
1135 checksums = fu_device_get_checksums (device);
1136 g_ptr_array_set_size (checksums, 0);
1137
Richard Hughescff38bc2016-12-12 12:03:37 +00001138 /* optional */
1139 g_module_symbol (priv->module, "fu_plugin_verify", (gpointer *) &func);
1140 if (func == NULL)
1141 return TRUE;
1142 g_debug ("performing verify() on %s", priv->name);
1143 if (!func (plugin, device, flags, error)) {
1144 g_prefix_error (error, "failed to verify %s: ", priv->name);
Richard Hughesd0905142016-03-13 09:46:49 +00001145 return FALSE;
1146 }
1147 return TRUE;
1148}
1149
Richard Hughesd0905142016-03-13 09:46:49 +00001150gboolean
Richard Hughescff38bc2016-12-12 12:03:37 +00001151fu_plugin_runner_unlock (FuPlugin *plugin, FuDevice *device, GError **error)
Richard Hughesd0905142016-03-13 09:46:49 +00001152{
Richard Hughescff38bc2016-12-12 12:03:37 +00001153 guint64 flags;
Richard Hughescff38bc2016-12-12 12:03:37 +00001154
1155 /* final check */
1156 flags = fu_device_get_flags (device);
1157 if ((flags & FWUPD_DEVICE_FLAG_LOCKED) == 0) {
1158 g_set_error (error,
1159 FWUPD_ERROR,
1160 FWUPD_ERROR_NOT_SUPPORTED,
1161 "Device %s is not locked",
1162 fu_device_get_id (device));
1163 return FALSE;
1164 }
1165
Richard Hughes9c4b5312017-11-14 11:34:53 +00001166 /* run vfunc */
1167 if (!fu_plugin_runner_device_generic (plugin, device,
1168 "fu_plugin_unlock", error))
1169 return FALSE;
Richard Hughescff38bc2016-12-12 12:03:37 +00001170
1171 /* update with correct flags */
1172 flags = fu_device_get_flags (device);
1173 fu_device_set_flags (device, flags &= ~FWUPD_DEVICE_FLAG_LOCKED);
1174 fu_device_set_modified (device, (guint64) g_get_real_time () / G_USEC_PER_SEC);
1175 return TRUE;
1176}
1177
1178gboolean
1179fu_plugin_runner_update (FuPlugin *plugin,
Richard Hughesa785a1c2017-08-25 16:00:58 +01001180 FuDevice *device,
1181 GBytes *blob_cab,
1182 GBytes *blob_fw,
1183 FwupdInstallFlags flags,
1184 GError **error)
Richard Hughescff38bc2016-12-12 12:03:37 +00001185{
1186 FuPluginPrivate *priv = GET_PRIVATE (plugin);
Richard Hughesa785a1c2017-08-25 16:00:58 +01001187 FuPluginUpdateFunc update_func;
Richard Hughes780ef3f2018-01-12 16:20:31 +00001188 g_autoptr(FuHistory) history = NULL;
Richard Hughes68982c62017-09-13 15:40:14 +01001189 g_autoptr(FuDevice) device_pending = NULL;
Richard Hughescff38bc2016-12-12 12:03:37 +00001190 GError *error_update = NULL;
Richard Hughesf556d372017-06-15 19:49:18 +01001191 GPtrArray *checksums;
Richard Hughescff38bc2016-12-12 12:03:37 +00001192
1193 /* not enabled */
Richard Hughes41c15482018-02-01 22:07:21 +00001194 if (!priv->enabled) {
1195 g_debug ("plugin not enabled, skipping");
Richard Hughesd0905142016-03-13 09:46:49 +00001196 return TRUE;
Richard Hughes41c15482018-02-01 22:07:21 +00001197 }
Richard Hughesd0905142016-03-13 09:46:49 +00001198
Richard Hughes639da472018-01-06 22:35:04 +00001199 /* no object loaded */
Richard Hughes41c15482018-02-01 22:07:21 +00001200 if (priv->module == NULL) {
1201 g_debug ("module not enabled, skipping");
Richard Hughes639da472018-01-06 22:35:04 +00001202 return TRUE;
Richard Hughes41c15482018-02-01 22:07:21 +00001203 }
Richard Hughes639da472018-01-06 22:35:04 +00001204
Richard Hughesd0905142016-03-13 09:46:49 +00001205 /* optional */
Richard Hughesa785a1c2017-08-25 16:00:58 +01001206 g_module_symbol (priv->module, "fu_plugin_update", (gpointer *) &update_func);
1207 if (update_func == NULL) {
1208 g_set_error_literal (error,
1209 FWUPD_ERROR,
1210 FWUPD_ERROR_NOT_SUPPORTED,
1211 "No update possible");
1212 return FALSE;
1213 }
Richard Hughesd0905142016-03-13 09:46:49 +00001214
Richard Hughesa785a1c2017-08-25 16:00:58 +01001215 /* just schedule this for the next reboot */
Richard Hughescff38bc2016-12-12 12:03:37 +00001216 if (flags & FWUPD_INSTALL_FLAG_OFFLINE) {
Richard Hughesa785a1c2017-08-25 16:00:58 +01001217 return fu_plugin_runner_schedule_update (plugin,
1218 device,
1219 blob_cab,
1220 error);
Richard Hughescff38bc2016-12-12 12:03:37 +00001221 }
1222
1223 /* cancel the pending action */
1224 if (!fu_plugin_runner_offline_invalidate (error))
1225 return FALSE;
1226
1227 /* online */
Richard Hughes780ef3f2018-01-12 16:20:31 +00001228 history = fu_history_new ();
Richard Hughes0b9d9962018-01-12 16:31:28 +00001229 device_pending = fu_history_get_device_by_id (history, fu_device_get_id (device), NULL);
Richard Hughesa785a1c2017-08-25 16:00:58 +01001230 if (!update_func (plugin, device, blob_fw, flags, &error_update)) {
Richard Hughescff38bc2016-12-12 12:03:37 +00001231 /* save the error to the database */
Richard Hughes68982c62017-09-13 15:40:14 +01001232 if (device_pending != NULL) {
Richard Hughes0b9d9962018-01-12 16:31:28 +00001233 fu_history_set_device_error (history,
Richard Hughesd949d962018-01-12 16:40:30 +00001234 fu_device_get_id (device),
Richard Hughes0b9d9962018-01-12 16:31:28 +00001235 error_update->message, NULL);
Richard Hughescff38bc2016-12-12 12:03:37 +00001236 }
1237 g_propagate_error (error, error_update);
1238 return FALSE;
1239 }
1240
Richard Hughesf556d372017-06-15 19:49:18 +01001241 /* no longer valid */
1242 checksums = fu_device_get_checksums (device);
1243 g_ptr_array_set_size (checksums, 0);
1244
Richard Hughescff38bc2016-12-12 12:03:37 +00001245 /* cleanup */
Richard Hughes68982c62017-09-13 15:40:14 +01001246 if (device_pending != NULL) {
Richard Hughescff38bc2016-12-12 12:03:37 +00001247 const gchar *tmp;
Richard Hughesbc3a4e12018-01-06 22:41:47 +00001248 FwupdRelease *release;
Richard Hughescff38bc2016-12-12 12:03:37 +00001249
Richard Hughes780ef3f2018-01-12 16:20:31 +00001250 /* update history database */
Richard Hughes0b9d9962018-01-12 16:31:28 +00001251 if (!fu_history_set_device_state (history,
Richard Hughesd949d962018-01-12 16:40:30 +00001252 fu_device_get_id (device),
Richard Hughes0b9d9962018-01-12 16:31:28 +00001253 FWUPD_UPDATE_STATE_SUCCESS,
1254 error))
1255 return FALSE;
Richard Hughescff38bc2016-12-12 12:03:37 +00001256
1257 /* delete cab file */
Richard Hughesbc3a4e12018-01-06 22:41:47 +00001258 release = fu_device_get_release_default (device_pending);
1259 tmp = fwupd_release_get_filename (release);
Richard Hughescff38bc2016-12-12 12:03:37 +00001260 if (tmp != NULL && g_str_has_prefix (tmp, LIBEXECDIR)) {
1261 g_autoptr(GError) error_local = NULL;
1262 g_autoptr(GFile) file = NULL;
1263 file = g_file_new_for_path (tmp);
1264 if (!g_file_delete (file, NULL, &error_local)) {
1265 g_set_error (error,
1266 FWUPD_ERROR,
1267 FWUPD_ERROR_INVALID_FILE,
1268 "Failed to delete %s: %s",
1269 tmp, error_local->message);
1270 return FALSE;
1271 }
1272 }
1273 }
Richard Hughesd0905142016-03-13 09:46:49 +00001274 return TRUE;
1275}
Richard Hughescff38bc2016-12-12 12:03:37 +00001276
1277gboolean
1278fu_plugin_runner_clear_results (FuPlugin *plugin, FuDevice *device, GError **error)
1279{
1280 FuPluginPrivate *priv = GET_PRIVATE (plugin);
Richard Hughes7b8b2022016-12-12 16:15:03 +00001281 FuPluginDeviceFunc func = NULL;
Richard Hughescff38bc2016-12-12 12:03:37 +00001282
1283 /* not enabled */
1284 if (!priv->enabled)
1285 return TRUE;
1286
Richard Hughes639da472018-01-06 22:35:04 +00001287 /* no object loaded */
1288 if (priv->module == NULL)
1289 return TRUE;
1290
Richard Hughes65e44ca2018-01-30 17:26:30 +00001291 /* optional */
1292 g_module_symbol (priv->module, "fu_plugin_get_results", (gpointer *) &func);
1293 if (func == NULL)
Richard Hughescff38bc2016-12-12 12:03:37 +00001294 return TRUE;
Richard Hughes65e44ca2018-01-30 17:26:30 +00001295 g_debug ("performing clear_result() on %s", priv->name);
1296 if (!func (plugin, device, error)) {
1297 g_prefix_error (error, "failed to clear_result %s: ", priv->name);
Richard Hughescff38bc2016-12-12 12:03:37 +00001298 return FALSE;
1299 }
Richard Hughes65e44ca2018-01-30 17:26:30 +00001300 return TRUE;
Richard Hughescff38bc2016-12-12 12:03:37 +00001301}
1302
1303gboolean
1304fu_plugin_runner_get_results (FuPlugin *plugin, FuDevice *device, GError **error)
1305{
1306 FuPluginPrivate *priv = GET_PRIVATE (plugin);
Richard Hughes7b8b2022016-12-12 16:15:03 +00001307 FuPluginDeviceFunc func = NULL;
Richard Hughescff38bc2016-12-12 12:03:37 +00001308
1309 /* not enabled */
1310 if (!priv->enabled)
1311 return TRUE;
1312
Richard Hughes639da472018-01-06 22:35:04 +00001313 /* no object loaded */
1314 if (priv->module == NULL)
1315 return TRUE;
1316
Richard Hughes65e44ca2018-01-30 17:26:30 +00001317 /* optional */
Richard Hughescff38bc2016-12-12 12:03:37 +00001318 g_module_symbol (priv->module, "fu_plugin_get_results", (gpointer *) &func);
Richard Hughes65e44ca2018-01-30 17:26:30 +00001319 if (func == NULL)
Richard Hughescff38bc2016-12-12 12:03:37 +00001320 return TRUE;
Richard Hughes65e44ca2018-01-30 17:26:30 +00001321 g_debug ("performing get_results() on %s", priv->name);
1322 if (!func (plugin, device, error)) {
1323 g_prefix_error (error, "failed to get_results %s: ", priv->name);
Richard Hughescff38bc2016-12-12 12:03:37 +00001324 return FALSE;
1325 }
Richard Hughescff38bc2016-12-12 12:03:37 +00001326 return TRUE;
1327}
1328
Richard Hughes08a37992017-09-12 12:57:43 +01001329/**
1330 * fu_plugin_get_order:
1331 * @plugin: a #FuPlugin
1332 *
1333 * Gets the plugin order, where higher numbers are run after lower
1334 * numbers.
1335 *
1336 * Returns: the integer value
1337 **/
1338guint
1339fu_plugin_get_order (FuPlugin *plugin)
1340{
1341 FuPluginPrivate *priv = fu_plugin_get_instance_private (plugin);
1342 return priv->order;
1343}
1344
1345/**
1346 * fu_plugin_set_order:
1347 * @plugin: a #FuPlugin
1348 * @order: a integer value
1349 *
1350 * Sets the plugin order, where higher numbers are run after lower
1351 * numbers.
1352 **/
1353void
1354fu_plugin_set_order (FuPlugin *plugin, guint order)
1355{
1356 FuPluginPrivate *priv = fu_plugin_get_instance_private (plugin);
1357 priv->order = order;
1358}
1359
1360/**
1361 * fu_plugin_add_rule:
1362 * @plugin: a #FuPlugin
1363 * @rule: a #FuPluginRule, e.g. %FU_PLUGIN_RULE_CONFLICTS
Richard Hughes4eada342017-10-03 21:20:32 +01001364 * @name: a plugin name, e.g. `upower`
Richard Hughes08a37992017-09-12 12:57:43 +01001365 *
1366 * If the plugin name is found, the rule will be used to sort the plugin list,
1367 * for example the plugin specified by @name will be ordered after this plugin
1368 * when %FU_PLUGIN_RULE_RUN_AFTER is used.
1369 *
1370 * NOTE: The depsolver is iterative and may not solve overly-complicated rules;
1371 * If depsolving fails then fwupd will not start.
1372 **/
1373void
1374fu_plugin_add_rule (FuPlugin *plugin, FuPluginRule rule, const gchar *name)
1375{
1376 FuPluginPrivate *priv = fu_plugin_get_instance_private (plugin);
1377 g_ptr_array_add (priv->rules[rule], g_strdup (name));
1378}
1379
1380/**
1381 * fu_plugin_get_rules:
1382 * @plugin: a #FuPlugin
1383 * @rule: a #FuPluginRule, e.g. %FU_PLUGIN_RULE_CONFLICTS
1384 *
1385 * Gets the plugin IDs that should be run after this plugin.
1386 *
1387 * Returns: (element-type utf8) (transfer none): the list of plugin names, e.g. ['appstream']
1388 **/
1389GPtrArray *
1390fu_plugin_get_rules (FuPlugin *plugin, FuPluginRule rule)
1391{
1392 FuPluginPrivate *priv = fu_plugin_get_instance_private (plugin);
1393 return priv->rules[rule];
1394}
1395
Richard Hughes80b79bb2018-01-11 21:11:06 +00001396/**
1397 * fu_plugin_add_report_metadata:
1398 * @plugin: a #FuPlugin
1399 * @key: a string, e.g. `FwupdateVersion`
1400 * @value: a string, e.g. `10`
1401 *
1402 * Sets any additional metadata to be included in the firmware report to aid
1403 * debugging problems.
1404 *
1405 * Any data included here will be sent to the metadata server after user
1406 * confirmation.
1407 **/
1408void
1409fu_plugin_add_report_metadata (FuPlugin *plugin, const gchar *key, const gchar *value)
1410{
1411 FuPluginPrivate *priv = fu_plugin_get_instance_private (plugin);
1412 g_hash_table_insert (priv->report_metadata, g_strdup (key), g_strdup (value));
1413}
1414
1415/**
1416 * fu_plugin_get_report_metadata:
1417 * @plugin: a #FuPlugin
1418 *
1419 * Returns the list of additional metadata to be added when filing a report.
1420 *
1421 * Returns: (transfer none): the map of report metadata
1422 **/
1423GHashTable *
1424fu_plugin_get_report_metadata (FuPlugin *plugin)
1425{
1426 FuPluginPrivate *priv = fu_plugin_get_instance_private (plugin);
1427 return priv->report_metadata;
1428}
1429
Richard Hughescff38bc2016-12-12 12:03:37 +00001430static void
1431fu_plugin_class_init (FuPluginClass *klass)
1432{
1433 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1434 object_class->finalize = fu_plugin_finalize;
1435 signals[SIGNAL_DEVICE_ADDED] =
1436 g_signal_new ("device-added",
1437 G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST,
1438 G_STRUCT_OFFSET (FuPluginClass, device_added),
1439 NULL, NULL, g_cclosure_marshal_VOID__OBJECT,
1440 G_TYPE_NONE, 1, FU_TYPE_DEVICE);
1441 signals[SIGNAL_DEVICE_REMOVED] =
1442 g_signal_new ("device-removed",
1443 G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST,
1444 G_STRUCT_OFFSET (FuPluginClass, device_removed),
1445 NULL, NULL, g_cclosure_marshal_VOID__OBJECT,
1446 G_TYPE_NONE, 1, FU_TYPE_DEVICE);
Richard Hughese1fd34d2017-08-24 14:19:51 +01001447 signals[SIGNAL_DEVICE_REGISTER] =
1448 g_signal_new ("device-register",
1449 G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST,
1450 G_STRUCT_OFFSET (FuPluginClass, device_register),
1451 NULL, NULL, g_cclosure_marshal_VOID__OBJECT,
1452 G_TYPE_NONE, 1, FU_TYPE_DEVICE);
Richard Hughes362d6d72017-01-07 21:42:14 +00001453 signals[SIGNAL_RECOLDPLUG] =
1454 g_signal_new ("recoldplug",
1455 G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST,
1456 G_STRUCT_OFFSET (FuPluginClass, recoldplug),
1457 NULL, NULL, g_cclosure_marshal_VOID__VOID,
1458 G_TYPE_NONE, 0);
Richard Hughesb0829032017-01-10 09:27:08 +00001459 signals[SIGNAL_SET_COLDPLUG_DELAY] =
1460 g_signal_new ("set-coldplug-delay",
1461 G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST,
1462 G_STRUCT_OFFSET (FuPluginClass, set_coldplug_delay),
1463 NULL, NULL, g_cclosure_marshal_VOID__UINT,
1464 G_TYPE_NONE, 1, G_TYPE_UINT);
Richard Hughescff38bc2016-12-12 12:03:37 +00001465}
1466
1467static void
1468fu_plugin_init (FuPlugin *plugin)
1469{
1470 FuPluginPrivate *priv = GET_PRIVATE (plugin);
1471 priv->enabled = TRUE;
1472 priv->devices = g_hash_table_new_full (g_str_hash, g_str_equal,
1473 g_free, (GDestroyNotify) g_object_unref);
Richard Hughese8b5db62017-07-17 14:18:22 +01001474 priv->devices_delay = g_hash_table_new (g_direct_hash, g_direct_equal);
Richard Hughes80b79bb2018-01-11 21:11:06 +00001475 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 +01001476 for (guint i = 0; i < FU_PLUGIN_RULE_LAST; i++)
1477 priv->rules[i] = g_ptr_array_new_with_free_func (g_free);
Richard Hughescff38bc2016-12-12 12:03:37 +00001478}
1479
1480static void
1481fu_plugin_finalize (GObject *object)
1482{
1483 FuPlugin *plugin = FU_PLUGIN (object);
1484 FuPluginPrivate *priv = GET_PRIVATE (plugin);
1485 FuPluginInitFunc func = NULL;
1486
1487 /* optional */
1488 if (priv->module != NULL) {
1489 g_module_symbol (priv->module, "fu_plugin_destroy", (gpointer *) &func);
1490 if (func != NULL) {
1491 g_debug ("performing destroy() on %s", priv->name);
1492 func (plugin);
1493 }
1494 }
1495
Richard Hughes08a37992017-09-12 12:57:43 +01001496 for (guint i = 0; i < FU_PLUGIN_RULE_LAST; i++)
1497 g_ptr_array_unref (priv->rules[i]);
1498
Richard Hughescff38bc2016-12-12 12:03:37 +00001499 if (priv->usb_ctx != NULL)
1500 g_object_unref (priv->usb_ctx);
Richard Hughesb8f8db22017-04-25 15:56:00 +01001501 if (priv->hwids != NULL)
Richard Hughesd7704d42017-08-08 20:29:09 +01001502 g_object_unref (priv->hwids);
Richard Hughes9c028f02017-10-28 21:14:28 +01001503 if (priv->quirks != NULL)
1504 g_object_unref (priv->quirks);
Richard Hughes1354ea92017-09-19 15:58:31 +01001505 if (priv->supported_guids != NULL)
1506 g_ptr_array_unref (priv->supported_guids);
Richard Hughes49e5e052017-09-03 12:15:41 +01001507 if (priv->smbios != NULL)
1508 g_object_unref (priv->smbios);
Richard Hughes576c0122017-02-24 09:47:00 +00001509#ifndef RUNNING_ON_VALGRIND
Richard Hughescff38bc2016-12-12 12:03:37 +00001510 if (priv->module != NULL)
1511 g_module_close (priv->module);
Richard Hughes576c0122017-02-24 09:47:00 +00001512#endif
Richard Hughescff38bc2016-12-12 12:03:37 +00001513 g_hash_table_unref (priv->devices);
Richard Hughesae3d65f2016-12-16 09:38:01 +00001514 g_hash_table_unref (priv->devices_delay);
Richard Hughes80b79bb2018-01-11 21:11:06 +00001515 g_hash_table_unref (priv->report_metadata);
Richard Hughescff38bc2016-12-12 12:03:37 +00001516 g_free (priv->name);
1517 g_free (priv->data);
1518
1519 G_OBJECT_CLASS (fu_plugin_parent_class)->finalize (object);
1520}
1521
1522FuPlugin *
1523fu_plugin_new (void)
1524{
1525 FuPlugin *plugin;
1526 plugin = g_object_new (FU_TYPE_PLUGIN, NULL);
1527 return plugin;
1528}