blob: cfde9252306a09735f3c155bef4f7313468a633d [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{
1058 gchar tmpname[] = {"XXXXXX.cap"};
1059 g_autofree gchar *dirname = NULL;
1060 g_autofree gchar *filename = NULL;
Richard Hughes68982c62017-09-13 15:40:14 +01001061 g_autoptr(FuDevice) res_tmp = NULL;
Richard Hughes780ef3f2018-01-12 16:20:31 +00001062 g_autoptr(FuHistory) history = NULL;
Richard Hughesbc3a4e12018-01-06 22:41:47 +00001063 g_autoptr(FwupdRelease) release = fwupd_release_new ();
Richard Hughescff38bc2016-12-12 12:03:37 +00001064 g_autoptr(GFile) file = NULL;
1065
1066 /* id already exists */
Richard Hughes780ef3f2018-01-12 16:20:31 +00001067 history = fu_history_new ();
Richard Hughes0b9d9962018-01-12 16:31:28 +00001068 res_tmp = fu_history_get_device_by_id (history, fu_device_get_id (device), NULL);
Richard Hughescff38bc2016-12-12 12:03:37 +00001069 if (res_tmp != NULL) {
1070 g_set_error (error,
1071 FWUPD_ERROR,
1072 FWUPD_ERROR_ALREADY_PENDING,
1073 "%s is already scheduled to be updated",
1074 fu_device_get_id (device));
1075 return FALSE;
1076 }
1077
1078 /* create directory */
1079 dirname = g_build_filename (LOCALSTATEDIR, "lib", "fwupd", NULL);
1080 file = g_file_new_for_path (dirname);
1081 if (!g_file_query_exists (file, NULL)) {
1082 if (!g_file_make_directory_with_parents (file, NULL, error))
1083 return FALSE;
1084 }
1085
1086 /* get a random filename */
1087 for (guint i = 0; i < 6; i++)
1088 tmpname[i] = (gchar) g_random_int_range ('A', 'Z');
1089 filename = g_build_filename (dirname, tmpname, NULL);
1090
1091 /* just copy to the temp file */
Richard Hughes23135eb2017-11-30 21:01:25 +00001092 fu_device_set_status (device, FWUPD_STATUS_SCHEDULING);
Richard Hughescff38bc2016-12-12 12:03:37 +00001093 if (!g_file_set_contents (filename,
1094 g_bytes_get_data (blob_cab, NULL),
1095 (gssize) g_bytes_get_size (blob_cab),
1096 error))
1097 return FALSE;
1098
1099 /* schedule for next boot */
1100 g_debug ("schedule %s to be installed to %s on next boot",
1101 filename, fu_device_get_id (device));
Richard Hughesbc3a4e12018-01-06 22:41:47 +00001102 fwupd_release_set_filename (release, filename);
Richard Hughescff38bc2016-12-12 12:03:37 +00001103
1104 /* add to database */
Richard Hughes3e90a582018-01-06 22:38:09 +00001105 fu_device_set_update_state (device, FWUPD_UPDATE_STATE_PENDING);
Richard Hughes780ef3f2018-01-12 16:20:31 +00001106 if (!fu_history_add_device (history, device, release, error))
Richard Hughescff38bc2016-12-12 12:03:37 +00001107 return FALSE;
1108
1109 /* next boot we run offline */
1110 return fu_plugin_runner_offline_setup (error);
1111}
1112
1113gboolean
1114fu_plugin_runner_verify (FuPlugin *plugin,
1115 FuDevice *device,
1116 FuPluginVerifyFlags flags,
1117 GError **error)
1118{
1119 FuPluginPrivate *priv = GET_PRIVATE (plugin);
Richard Hughes7b8b2022016-12-12 16:15:03 +00001120 FuPluginVerifyFunc func = NULL;
Richard Hughesababbb72017-06-15 20:18:36 +01001121 GPtrArray *checksums;
Richard Hughescff38bc2016-12-12 12:03:37 +00001122
1123 /* not enabled */
1124 if (!priv->enabled)
1125 return TRUE;
1126
Richard Hughes639da472018-01-06 22:35:04 +00001127 /* no object loaded */
1128 if (priv->module == NULL)
1129 return TRUE;
1130
Richard Hughesababbb72017-06-15 20:18:36 +01001131 /* clear any existing verification checksums */
1132 checksums = fu_device_get_checksums (device);
1133 g_ptr_array_set_size (checksums, 0);
1134
Richard Hughescff38bc2016-12-12 12:03:37 +00001135 /* optional */
1136 g_module_symbol (priv->module, "fu_plugin_verify", (gpointer *) &func);
1137 if (func == NULL)
1138 return TRUE;
1139 g_debug ("performing verify() on %s", priv->name);
1140 if (!func (plugin, device, flags, error)) {
1141 g_prefix_error (error, "failed to verify %s: ", priv->name);
Richard Hughesd0905142016-03-13 09:46:49 +00001142 return FALSE;
1143 }
1144 return TRUE;
1145}
1146
Richard Hughesd0905142016-03-13 09:46:49 +00001147gboolean
Richard Hughescff38bc2016-12-12 12:03:37 +00001148fu_plugin_runner_unlock (FuPlugin *plugin, FuDevice *device, GError **error)
Richard Hughesd0905142016-03-13 09:46:49 +00001149{
Richard Hughescff38bc2016-12-12 12:03:37 +00001150 guint64 flags;
Richard Hughescff38bc2016-12-12 12:03:37 +00001151
1152 /* final check */
1153 flags = fu_device_get_flags (device);
1154 if ((flags & FWUPD_DEVICE_FLAG_LOCKED) == 0) {
1155 g_set_error (error,
1156 FWUPD_ERROR,
1157 FWUPD_ERROR_NOT_SUPPORTED,
1158 "Device %s is not locked",
1159 fu_device_get_id (device));
1160 return FALSE;
1161 }
1162
Richard Hughes9c4b5312017-11-14 11:34:53 +00001163 /* run vfunc */
1164 if (!fu_plugin_runner_device_generic (plugin, device,
1165 "fu_plugin_unlock", error))
1166 return FALSE;
Richard Hughescff38bc2016-12-12 12:03:37 +00001167
1168 /* update with correct flags */
1169 flags = fu_device_get_flags (device);
1170 fu_device_set_flags (device, flags &= ~FWUPD_DEVICE_FLAG_LOCKED);
1171 fu_device_set_modified (device, (guint64) g_get_real_time () / G_USEC_PER_SEC);
1172 return TRUE;
1173}
1174
1175gboolean
1176fu_plugin_runner_update (FuPlugin *plugin,
Richard Hughesa785a1c2017-08-25 16:00:58 +01001177 FuDevice *device,
1178 GBytes *blob_cab,
1179 GBytes *blob_fw,
1180 FwupdInstallFlags flags,
1181 GError **error)
Richard Hughescff38bc2016-12-12 12:03:37 +00001182{
1183 FuPluginPrivate *priv = GET_PRIVATE (plugin);
Richard Hughesa785a1c2017-08-25 16:00:58 +01001184 FuPluginUpdateFunc update_func;
Richard Hughes780ef3f2018-01-12 16:20:31 +00001185 g_autoptr(FuHistory) history = NULL;
Richard Hughes68982c62017-09-13 15:40:14 +01001186 g_autoptr(FuDevice) device_pending = NULL;
Richard Hughescff38bc2016-12-12 12:03:37 +00001187 GError *error_update = NULL;
Richard Hughesf556d372017-06-15 19:49:18 +01001188 GPtrArray *checksums;
Richard Hughescff38bc2016-12-12 12:03:37 +00001189
1190 /* not enabled */
Richard Hughes41c15482018-02-01 22:07:21 +00001191 if (!priv->enabled) {
1192 g_debug ("plugin not enabled, skipping");
Richard Hughesd0905142016-03-13 09:46:49 +00001193 return TRUE;
Richard Hughes41c15482018-02-01 22:07:21 +00001194 }
Richard Hughesd0905142016-03-13 09:46:49 +00001195
Richard Hughes639da472018-01-06 22:35:04 +00001196 /* no object loaded */
Richard Hughes41c15482018-02-01 22:07:21 +00001197 if (priv->module == NULL) {
1198 g_debug ("module not enabled, skipping");
Richard Hughes639da472018-01-06 22:35:04 +00001199 return TRUE;
Richard Hughes41c15482018-02-01 22:07:21 +00001200 }
Richard Hughes639da472018-01-06 22:35:04 +00001201
Richard Hughesd0905142016-03-13 09:46:49 +00001202 /* optional */
Richard Hughesa785a1c2017-08-25 16:00:58 +01001203 g_module_symbol (priv->module, "fu_plugin_update", (gpointer *) &update_func);
1204 if (update_func == NULL) {
1205 g_set_error_literal (error,
1206 FWUPD_ERROR,
1207 FWUPD_ERROR_NOT_SUPPORTED,
1208 "No update possible");
1209 return FALSE;
1210 }
Richard Hughesd0905142016-03-13 09:46:49 +00001211
Richard Hughesa785a1c2017-08-25 16:00:58 +01001212 /* just schedule this for the next reboot */
Richard Hughescff38bc2016-12-12 12:03:37 +00001213 if (flags & FWUPD_INSTALL_FLAG_OFFLINE) {
Richard Hughesa785a1c2017-08-25 16:00:58 +01001214 return fu_plugin_runner_schedule_update (plugin,
1215 device,
1216 blob_cab,
1217 error);
Richard Hughescff38bc2016-12-12 12:03:37 +00001218 }
1219
1220 /* cancel the pending action */
1221 if (!fu_plugin_runner_offline_invalidate (error))
1222 return FALSE;
1223
1224 /* online */
Richard Hughes780ef3f2018-01-12 16:20:31 +00001225 history = fu_history_new ();
Richard Hughes0b9d9962018-01-12 16:31:28 +00001226 device_pending = fu_history_get_device_by_id (history, fu_device_get_id (device), NULL);
Richard Hughesa785a1c2017-08-25 16:00:58 +01001227 if (!update_func (plugin, device, blob_fw, flags, &error_update)) {
Richard Hughescff38bc2016-12-12 12:03:37 +00001228 /* save the error to the database */
Richard Hughes68982c62017-09-13 15:40:14 +01001229 if (device_pending != NULL) {
Richard Hughes0b9d9962018-01-12 16:31:28 +00001230 fu_history_set_device_error (history,
Richard Hughesd949d962018-01-12 16:40:30 +00001231 fu_device_get_id (device),
Richard Hughes0b9d9962018-01-12 16:31:28 +00001232 error_update->message, NULL);
Richard Hughescff38bc2016-12-12 12:03:37 +00001233 }
1234 g_propagate_error (error, error_update);
1235 return FALSE;
1236 }
1237
Richard Hughesf556d372017-06-15 19:49:18 +01001238 /* no longer valid */
1239 checksums = fu_device_get_checksums (device);
1240 g_ptr_array_set_size (checksums, 0);
1241
Richard Hughescff38bc2016-12-12 12:03:37 +00001242 /* cleanup */
Richard Hughes68982c62017-09-13 15:40:14 +01001243 if (device_pending != NULL) {
Richard Hughescff38bc2016-12-12 12:03:37 +00001244 const gchar *tmp;
Richard Hughesbc3a4e12018-01-06 22:41:47 +00001245 FwupdRelease *release;
Richard Hughescff38bc2016-12-12 12:03:37 +00001246
Richard Hughes780ef3f2018-01-12 16:20:31 +00001247 /* update history database */
Richard Hughes0b9d9962018-01-12 16:31:28 +00001248 if (!fu_history_set_device_state (history,
Richard Hughesd949d962018-01-12 16:40:30 +00001249 fu_device_get_id (device),
Richard Hughes0b9d9962018-01-12 16:31:28 +00001250 FWUPD_UPDATE_STATE_SUCCESS,
1251 error))
1252 return FALSE;
Richard Hughescff38bc2016-12-12 12:03:37 +00001253
1254 /* delete cab file */
Richard Hughesbc3a4e12018-01-06 22:41:47 +00001255 release = fu_device_get_release_default (device_pending);
1256 tmp = fwupd_release_get_filename (release);
Richard Hughescff38bc2016-12-12 12:03:37 +00001257 if (tmp != NULL && g_str_has_prefix (tmp, LIBEXECDIR)) {
1258 g_autoptr(GError) error_local = NULL;
1259 g_autoptr(GFile) file = NULL;
1260 file = g_file_new_for_path (tmp);
1261 if (!g_file_delete (file, NULL, &error_local)) {
1262 g_set_error (error,
1263 FWUPD_ERROR,
1264 FWUPD_ERROR_INVALID_FILE,
1265 "Failed to delete %s: %s",
1266 tmp, error_local->message);
1267 return FALSE;
1268 }
1269 }
1270 }
Richard Hughesd0905142016-03-13 09:46:49 +00001271 return TRUE;
1272}
Richard Hughescff38bc2016-12-12 12:03:37 +00001273
1274gboolean
1275fu_plugin_runner_clear_results (FuPlugin *plugin, FuDevice *device, GError **error)
1276{
1277 FuPluginPrivate *priv = GET_PRIVATE (plugin);
Richard Hughes7b8b2022016-12-12 16:15:03 +00001278 FuPluginDeviceFunc func = NULL;
Richard Hughescff38bc2016-12-12 12:03:37 +00001279 g_autoptr(GError) error_local = NULL;
Richard Hughes68982c62017-09-13 15:40:14 +01001280 g_autoptr(FuDevice) device_pending = NULL;
Richard Hughes780ef3f2018-01-12 16:20:31 +00001281 g_autoptr(FuHistory) history = 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 Hughescff38bc2016-12-12 12:03:37 +00001291 /* use the plugin if the vfunc is provided */
1292 g_module_symbol (priv->module, "fu_plugin_clear_result", (gpointer *) &func);
1293 if (func != NULL) {
1294 g_debug ("performing clear_result() on %s", priv->name);
1295 if (!func (plugin, device, error)) {
1296 g_prefix_error (error, "failed to clear_result %s: ", priv->name);
1297 return FALSE;
1298 }
1299 return TRUE;
1300 }
1301
1302 /* handled using the database */
Richard Hughes780ef3f2018-01-12 16:20:31 +00001303 history = fu_history_new ();
Richard Hughes0b9d9962018-01-12 16:31:28 +00001304 device_pending = fu_history_get_device_by_id (history,
1305 fu_device_get_id (device),
1306 &error_local);
Richard Hughes68982c62017-09-13 15:40:14 +01001307 if (device_pending == NULL) {
Richard Hughescff38bc2016-12-12 12:03:37 +00001308 g_set_error (error,
1309 FWUPD_ERROR,
1310 FWUPD_ERROR_INVALID_FILE,
Richard Hughes780ef3f2018-01-12 16:20:31 +00001311 "Failed to find %s in history database: %s",
Richard Hughescff38bc2016-12-12 12:03:37 +00001312 fu_device_get_id (device),
1313 error_local->message);
1314 return FALSE;
1315 }
1316
Richard Hughes780ef3f2018-01-12 16:20:31 +00001317 /* remove from history database */
Richard Hughesd949d962018-01-12 16:40:30 +00001318 return fu_history_remove_device (history, fu_device_get_id (device), error);
Richard Hughescff38bc2016-12-12 12:03:37 +00001319}
1320
1321gboolean
1322fu_plugin_runner_get_results (FuPlugin *plugin, FuDevice *device, GError **error)
1323{
1324 FuPluginPrivate *priv = GET_PRIVATE (plugin);
Richard Hughes7b8b2022016-12-12 16:15:03 +00001325 FuPluginDeviceFunc func = NULL;
Richard Hughescff38bc2016-12-12 12:03:37 +00001326 FwupdUpdateState update_state;
1327 const gchar *tmp;
1328 g_autoptr(GError) error_local = NULL;
Richard Hughes68982c62017-09-13 15:40:14 +01001329 g_autoptr(FuDevice) device_pending = NULL;
Richard Hughes780ef3f2018-01-12 16:20:31 +00001330 g_autoptr(FuHistory) history = NULL;
Richard Hughesbc3a4e12018-01-06 22:41:47 +00001331 FwupdRelease *release;
1332 FwupdRelease *release_pending;
Richard Hughescff38bc2016-12-12 12:03:37 +00001333
1334 /* not enabled */
1335 if (!priv->enabled)
1336 return TRUE;
1337
Richard Hughes639da472018-01-06 22:35:04 +00001338 /* no object loaded */
1339 if (priv->module == NULL)
1340 return TRUE;
1341
Richard Hughescff38bc2016-12-12 12:03:37 +00001342 /* use the plugin if the vfunc is provided */
1343 g_module_symbol (priv->module, "fu_plugin_get_results", (gpointer *) &func);
1344 if (func != NULL) {
1345 g_debug ("performing get_results() on %s", priv->name);
1346 if (!func (plugin, device, error)) {
1347 g_prefix_error (error, "failed to get_results %s: ", priv->name);
1348 return FALSE;
1349 }
1350 return TRUE;
1351 }
1352
1353 /* handled using the database */
Richard Hughes780ef3f2018-01-12 16:20:31 +00001354 history = fu_history_new ();
Richard Hughes0b9d9962018-01-12 16:31:28 +00001355 device_pending = fu_history_get_device_by_id (history,
1356 fu_device_get_id (device),
1357 &error_local);
Richard Hughes68982c62017-09-13 15:40:14 +01001358 if (device_pending == NULL) {
Richard Hughescff38bc2016-12-12 12:03:37 +00001359 g_set_error (error,
1360 FWUPD_ERROR,
1361 FWUPD_ERROR_NOTHING_TO_DO,
Richard Hughes780ef3f2018-01-12 16:20:31 +00001362 "Failed to find %s in history database: %s",
Richard Hughescff38bc2016-12-12 12:03:37 +00001363 fu_device_get_id (device),
1364 error_local->message);
1365 return FALSE;
1366 }
1367
Richard Hughes780ef3f2018-01-12 16:20:31 +00001368 /* copy the important parts from the history device to the real one */
Richard Hughescc3de2e2017-09-13 19:28:17 +01001369 update_state = fu_device_get_update_state (device_pending);
Richard Hughescff38bc2016-12-12 12:03:37 +00001370 if (update_state == FWUPD_UPDATE_STATE_UNKNOWN ||
1371 update_state == FWUPD_UPDATE_STATE_PENDING) {
1372 g_set_error (error,
1373 FWUPD_ERROR,
1374 FWUPD_ERROR_NOTHING_TO_DO,
1375 "Device %s has not been updated offline yet",
1376 fu_device_get_id (device));
1377 return FALSE;
1378 }
1379
1380 /* copy */
1381 fu_device_set_update_state (device, update_state);
Richard Hughescc3de2e2017-09-13 19:28:17 +01001382 tmp = fu_device_get_update_error (device_pending);
Richard Hughescff38bc2016-12-12 12:03:37 +00001383 if (tmp != NULL)
1384 fu_device_set_update_error (device, tmp);
Richard Hughescc3de2e2017-09-13 19:28:17 +01001385 tmp = fu_device_get_version (device_pending);
Richard Hughescff38bc2016-12-12 12:03:37 +00001386 if (tmp != NULL)
1387 fu_device_set_version (device, tmp);
Richard Hughesbc3a4e12018-01-06 22:41:47 +00001388 release_pending = fu_device_get_release_default (device_pending);
1389 release = fu_device_get_release_default (device);
1390 tmp = fwupd_release_get_version (release_pending);
Richard Hughes6b0e5c42018-01-28 21:51:41 +00001391 if (tmp != NULL) {
1392 release = fu_device_get_release_default (device);
Richard Hughesbc3a4e12018-01-06 22:41:47 +00001393 fwupd_release_set_version (release, tmp);
Richard Hughes6b0e5c42018-01-28 21:51:41 +00001394 }
Richard Hughescff38bc2016-12-12 12:03:37 +00001395 return TRUE;
1396}
1397
Richard Hughes08a37992017-09-12 12:57:43 +01001398/**
1399 * fu_plugin_get_order:
1400 * @plugin: a #FuPlugin
1401 *
1402 * Gets the plugin order, where higher numbers are run after lower
1403 * numbers.
1404 *
1405 * Returns: the integer value
1406 **/
1407guint
1408fu_plugin_get_order (FuPlugin *plugin)
1409{
1410 FuPluginPrivate *priv = fu_plugin_get_instance_private (plugin);
1411 return priv->order;
1412}
1413
1414/**
1415 * fu_plugin_set_order:
1416 * @plugin: a #FuPlugin
1417 * @order: a integer value
1418 *
1419 * Sets the plugin order, where higher numbers are run after lower
1420 * numbers.
1421 **/
1422void
1423fu_plugin_set_order (FuPlugin *plugin, guint order)
1424{
1425 FuPluginPrivate *priv = fu_plugin_get_instance_private (plugin);
1426 priv->order = order;
1427}
1428
1429/**
1430 * fu_plugin_add_rule:
1431 * @plugin: a #FuPlugin
1432 * @rule: a #FuPluginRule, e.g. %FU_PLUGIN_RULE_CONFLICTS
Richard Hughes4eada342017-10-03 21:20:32 +01001433 * @name: a plugin name, e.g. `upower`
Richard Hughes08a37992017-09-12 12:57:43 +01001434 *
1435 * If the plugin name is found, the rule will be used to sort the plugin list,
1436 * for example the plugin specified by @name will be ordered after this plugin
1437 * when %FU_PLUGIN_RULE_RUN_AFTER is used.
1438 *
1439 * NOTE: The depsolver is iterative and may not solve overly-complicated rules;
1440 * If depsolving fails then fwupd will not start.
1441 **/
1442void
1443fu_plugin_add_rule (FuPlugin *plugin, FuPluginRule rule, const gchar *name)
1444{
1445 FuPluginPrivate *priv = fu_plugin_get_instance_private (plugin);
1446 g_ptr_array_add (priv->rules[rule], g_strdup (name));
1447}
1448
1449/**
1450 * fu_plugin_get_rules:
1451 * @plugin: a #FuPlugin
1452 * @rule: a #FuPluginRule, e.g. %FU_PLUGIN_RULE_CONFLICTS
1453 *
1454 * Gets the plugin IDs that should be run after this plugin.
1455 *
1456 * Returns: (element-type utf8) (transfer none): the list of plugin names, e.g. ['appstream']
1457 **/
1458GPtrArray *
1459fu_plugin_get_rules (FuPlugin *plugin, FuPluginRule rule)
1460{
1461 FuPluginPrivate *priv = fu_plugin_get_instance_private (plugin);
1462 return priv->rules[rule];
1463}
1464
Richard Hughes80b79bb2018-01-11 21:11:06 +00001465/**
1466 * fu_plugin_add_report_metadata:
1467 * @plugin: a #FuPlugin
1468 * @key: a string, e.g. `FwupdateVersion`
1469 * @value: a string, e.g. `10`
1470 *
1471 * Sets any additional metadata to be included in the firmware report to aid
1472 * debugging problems.
1473 *
1474 * Any data included here will be sent to the metadata server after user
1475 * confirmation.
1476 **/
1477void
1478fu_plugin_add_report_metadata (FuPlugin *plugin, const gchar *key, const gchar *value)
1479{
1480 FuPluginPrivate *priv = fu_plugin_get_instance_private (plugin);
1481 g_hash_table_insert (priv->report_metadata, g_strdup (key), g_strdup (value));
1482}
1483
1484/**
1485 * fu_plugin_get_report_metadata:
1486 * @plugin: a #FuPlugin
1487 *
1488 * Returns the list of additional metadata to be added when filing a report.
1489 *
1490 * Returns: (transfer none): the map of report metadata
1491 **/
1492GHashTable *
1493fu_plugin_get_report_metadata (FuPlugin *plugin)
1494{
1495 FuPluginPrivate *priv = fu_plugin_get_instance_private (plugin);
1496 return priv->report_metadata;
1497}
1498
Richard Hughescff38bc2016-12-12 12:03:37 +00001499static void
1500fu_plugin_class_init (FuPluginClass *klass)
1501{
1502 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1503 object_class->finalize = fu_plugin_finalize;
1504 signals[SIGNAL_DEVICE_ADDED] =
1505 g_signal_new ("device-added",
1506 G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST,
1507 G_STRUCT_OFFSET (FuPluginClass, device_added),
1508 NULL, NULL, g_cclosure_marshal_VOID__OBJECT,
1509 G_TYPE_NONE, 1, FU_TYPE_DEVICE);
1510 signals[SIGNAL_DEVICE_REMOVED] =
1511 g_signal_new ("device-removed",
1512 G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST,
1513 G_STRUCT_OFFSET (FuPluginClass, device_removed),
1514 NULL, NULL, g_cclosure_marshal_VOID__OBJECT,
1515 G_TYPE_NONE, 1, FU_TYPE_DEVICE);
Richard Hughese1fd34d2017-08-24 14:19:51 +01001516 signals[SIGNAL_DEVICE_REGISTER] =
1517 g_signal_new ("device-register",
1518 G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST,
1519 G_STRUCT_OFFSET (FuPluginClass, device_register),
1520 NULL, NULL, g_cclosure_marshal_VOID__OBJECT,
1521 G_TYPE_NONE, 1, FU_TYPE_DEVICE);
Richard Hughes362d6d72017-01-07 21:42:14 +00001522 signals[SIGNAL_RECOLDPLUG] =
1523 g_signal_new ("recoldplug",
1524 G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST,
1525 G_STRUCT_OFFSET (FuPluginClass, recoldplug),
1526 NULL, NULL, g_cclosure_marshal_VOID__VOID,
1527 G_TYPE_NONE, 0);
Richard Hughesb0829032017-01-10 09:27:08 +00001528 signals[SIGNAL_SET_COLDPLUG_DELAY] =
1529 g_signal_new ("set-coldplug-delay",
1530 G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST,
1531 G_STRUCT_OFFSET (FuPluginClass, set_coldplug_delay),
1532 NULL, NULL, g_cclosure_marshal_VOID__UINT,
1533 G_TYPE_NONE, 1, G_TYPE_UINT);
Richard Hughescff38bc2016-12-12 12:03:37 +00001534}
1535
1536static void
1537fu_plugin_init (FuPlugin *plugin)
1538{
1539 FuPluginPrivate *priv = GET_PRIVATE (plugin);
1540 priv->enabled = TRUE;
1541 priv->devices = g_hash_table_new_full (g_str_hash, g_str_equal,
1542 g_free, (GDestroyNotify) g_object_unref);
Richard Hughese8b5db62017-07-17 14:18:22 +01001543 priv->devices_delay = g_hash_table_new (g_direct_hash, g_direct_equal);
Richard Hughes80b79bb2018-01-11 21:11:06 +00001544 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 +01001545 for (guint i = 0; i < FU_PLUGIN_RULE_LAST; i++)
1546 priv->rules[i] = g_ptr_array_new_with_free_func (g_free);
Richard Hughescff38bc2016-12-12 12:03:37 +00001547}
1548
1549static void
1550fu_plugin_finalize (GObject *object)
1551{
1552 FuPlugin *plugin = FU_PLUGIN (object);
1553 FuPluginPrivate *priv = GET_PRIVATE (plugin);
1554 FuPluginInitFunc func = NULL;
1555
1556 /* optional */
1557 if (priv->module != NULL) {
1558 g_module_symbol (priv->module, "fu_plugin_destroy", (gpointer *) &func);
1559 if (func != NULL) {
1560 g_debug ("performing destroy() on %s", priv->name);
1561 func (plugin);
1562 }
1563 }
1564
Richard Hughes08a37992017-09-12 12:57:43 +01001565 for (guint i = 0; i < FU_PLUGIN_RULE_LAST; i++)
1566 g_ptr_array_unref (priv->rules[i]);
1567
Richard Hughescff38bc2016-12-12 12:03:37 +00001568 if (priv->usb_ctx != NULL)
1569 g_object_unref (priv->usb_ctx);
Richard Hughesb8f8db22017-04-25 15:56:00 +01001570 if (priv->hwids != NULL)
Richard Hughesd7704d42017-08-08 20:29:09 +01001571 g_object_unref (priv->hwids);
Richard Hughes9c028f02017-10-28 21:14:28 +01001572 if (priv->quirks != NULL)
1573 g_object_unref (priv->quirks);
Richard Hughes1354ea92017-09-19 15:58:31 +01001574 if (priv->supported_guids != NULL)
1575 g_ptr_array_unref (priv->supported_guids);
Richard Hughes49e5e052017-09-03 12:15:41 +01001576 if (priv->smbios != NULL)
1577 g_object_unref (priv->smbios);
Richard Hughes576c0122017-02-24 09:47:00 +00001578#ifndef RUNNING_ON_VALGRIND
Richard Hughescff38bc2016-12-12 12:03:37 +00001579 if (priv->module != NULL)
1580 g_module_close (priv->module);
Richard Hughes576c0122017-02-24 09:47:00 +00001581#endif
Richard Hughescff38bc2016-12-12 12:03:37 +00001582 g_hash_table_unref (priv->devices);
Richard Hughesae3d65f2016-12-16 09:38:01 +00001583 g_hash_table_unref (priv->devices_delay);
Richard Hughes80b79bb2018-01-11 21:11:06 +00001584 g_hash_table_unref (priv->report_metadata);
Richard Hughescff38bc2016-12-12 12:03:37 +00001585 g_free (priv->name);
1586 g_free (priv->data);
1587
1588 G_OBJECT_CLASS (fu_plugin_parent_class)->finalize (object);
1589}
1590
1591FuPlugin *
1592fu_plugin_new (void)
1593{
1594 FuPlugin *plugin;
1595 plugin = g_object_new (FU_TYPE_PLUGIN, NULL);
1596 return plugin;
1597}