blob: 660d55ef1f63bc2040c71e2d0725670aaed48fe6 [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 Hughes0eb123b2018-04-19 12:00:04 +010060 GHashTable *runtime_versions;
Richard Hughes1354ea92017-09-19 15:58:31 +010061 GPtrArray *supported_guids;
62 FuSmbios *smbios;
Richard Hughescff38bc2016-12-12 12:03:37 +000063 GHashTable *devices; /* platform_id:GObject */
Richard Hughesae3d65f2016-12-16 09:38:01 +000064 GHashTable *devices_delay; /* FuDevice:FuPluginHelper */
Richard Hughes80b79bb2018-01-11 21:11:06 +000065 GHashTable *report_metadata; /* key:value */
Richard Hughescff38bc2016-12-12 12:03:37 +000066 FuPluginData *data;
67} FuPluginPrivate;
68
69enum {
70 SIGNAL_DEVICE_ADDED,
71 SIGNAL_DEVICE_REMOVED,
Richard Hughese1fd34d2017-08-24 14:19:51 +010072 SIGNAL_DEVICE_REGISTER,
Richard Hughes362d6d72017-01-07 21:42:14 +000073 SIGNAL_RECOLDPLUG,
Richard Hughesb0829032017-01-10 09:27:08 +000074 SIGNAL_SET_COLDPLUG_DELAY,
Richard Hughescff38bc2016-12-12 12:03:37 +000075 SIGNAL_LAST
76};
77
78static guint signals[SIGNAL_LAST] = { 0 };
79
80G_DEFINE_TYPE_WITH_PRIVATE (FuPlugin, fu_plugin, G_TYPE_OBJECT)
81#define GET_PRIVATE(o) (fu_plugin_get_instance_private (o))
82
83typedef const gchar *(*FuPluginGetNameFunc) (void);
84typedef void (*FuPluginInitFunc) (FuPlugin *plugin);
85typedef gboolean (*FuPluginStartupFunc) (FuPlugin *plugin,
86 GError **error);
Richard Hughese1fd34d2017-08-24 14:19:51 +010087typedef void (*FuPluginDeviceRegisterFunc) (FuPlugin *plugin,
88 FuDevice *device);
Richard Hughescff38bc2016-12-12 12:03:37 +000089typedef gboolean (*FuPluginDeviceFunc) (FuPlugin *plugin,
90 FuDevice *device,
91 GError **error);
92typedef gboolean (*FuPluginVerifyFunc) (FuPlugin *plugin,
93 FuDevice *device,
94 FuPluginVerifyFlags flags,
95 GError **error);
96typedef gboolean (*FuPluginUpdateFunc) (FuPlugin *plugin,
97 FuDevice *device,
98 GBytes *blob_fw,
99 FwupdInstallFlags flags,
100 GError **error);
Richard Hughes104f6512017-11-24 11:44:57 +0000101typedef gboolean (*FuPluginUsbDeviceAddedFunc) (FuPlugin *plugin,
102 GUsbDevice *usb_device,
103 GError **error);
Richard Hughescff38bc2016-12-12 12:03:37 +0000104
Richard Hughes57d18222017-01-10 16:02:59 +0000105/**
106 * fu_plugin_get_name:
107 * @plugin: A #FuPlugin
108 *
109 * Gets the plugin name.
110 *
111 * Returns: a plugin name, or %NULL for unknown.
112 *
113 * Since: 0.8.0
114 **/
Richard Hughescff38bc2016-12-12 12:03:37 +0000115const gchar *
116fu_plugin_get_name (FuPlugin *plugin)
Richard Hughesd0905142016-03-13 09:46:49 +0000117{
Richard Hughescff38bc2016-12-12 12:03:37 +0000118 FuPluginPrivate *priv = GET_PRIVATE (plugin);
Richard Hughesccd78a92017-01-11 16:57:41 +0000119 g_return_val_if_fail (FU_IS_PLUGIN (plugin), NULL);
Richard Hughescff38bc2016-12-12 12:03:37 +0000120 return priv->name;
121}
Richard Hughesd0905142016-03-13 09:46:49 +0000122
Richard Hughes34834102017-11-21 21:55:00 +0000123void
124fu_plugin_set_name (FuPlugin *plugin, const gchar *name)
125{
126 FuPluginPrivate *priv = GET_PRIVATE (plugin);
127 g_return_if_fail (FU_IS_PLUGIN (plugin));
128 g_return_if_fail (name != NULL);
129 g_free (priv->name);
130 priv->name = g_strdup (name);
131}
132
Richard Hughes57d18222017-01-10 16:02:59 +0000133/**
134 * fu_plugin_cache_lookup:
135 * @plugin: A #FuPlugin
136 * @id: the key
137 *
138 * Finds an object in the per-plugin cache.
139 *
140 * Returns: (transfer none): a #GObject, or %NULL for unfound.
141 *
142 * Since: 0.8.0
143 **/
Richard Hughescff38bc2016-12-12 12:03:37 +0000144gpointer
145fu_plugin_cache_lookup (FuPlugin *plugin, const gchar *id)
146{
147 FuPluginPrivate *priv = GET_PRIVATE (plugin);
Richard Hughesccd78a92017-01-11 16:57:41 +0000148 g_return_val_if_fail (FU_IS_PLUGIN (plugin), NULL);
149 g_return_val_if_fail (id != NULL, NULL);
Richard Hughescff38bc2016-12-12 12:03:37 +0000150 return g_hash_table_lookup (priv->devices, id);
151}
Richard Hughesd0905142016-03-13 09:46:49 +0000152
Richard Hughes57d18222017-01-10 16:02:59 +0000153/**
154 * fu_plugin_cache_add:
155 * @plugin: A #FuPlugin
156 * @id: the key
157 * @dev: a #GObject, typically a #FuDevice
158 *
159 * Adds an object to the per-plugin cache.
160 *
161 * Since: 0.8.0
162 **/
Richard Hughescff38bc2016-12-12 12:03:37 +0000163void
164fu_plugin_cache_add (FuPlugin *plugin, const gchar *id, gpointer dev)
165{
166 FuPluginPrivate *priv = GET_PRIVATE (plugin);
Richard Hughesccd78a92017-01-11 16:57:41 +0000167 g_return_if_fail (FU_IS_PLUGIN (plugin));
168 g_return_if_fail (id != NULL);
Richard Hughescff38bc2016-12-12 12:03:37 +0000169 g_hash_table_insert (priv->devices, g_strdup (id), g_object_ref (dev));
170}
171
Richard Hughes57d18222017-01-10 16:02:59 +0000172/**
173 * fu_plugin_cache_remove:
174 * @plugin: A #FuPlugin
175 * @id: the key
176 *
177 * Removes an object from the per-plugin cache.
178 *
179 * Since: 0.8.0
180 **/
Richard Hughescff38bc2016-12-12 12:03:37 +0000181void
182fu_plugin_cache_remove (FuPlugin *plugin, const gchar *id)
183{
184 FuPluginPrivate *priv = GET_PRIVATE (plugin);
Richard Hughesccd78a92017-01-11 16:57:41 +0000185 g_return_if_fail (FU_IS_PLUGIN (plugin));
186 g_return_if_fail (id != NULL);
Richard Hughescff38bc2016-12-12 12:03:37 +0000187 g_hash_table_remove (priv->devices, id);
188}
189
Richard Hughes57d18222017-01-10 16:02:59 +0000190/**
191 * fu_plugin_get_data:
192 * @plugin: A #FuPlugin
193 *
Richard Hughes4eada342017-10-03 21:20:32 +0100194 * Gets the per-plugin allocated private data. This will return %NULL unless
195 * fu_plugin_alloc_data() has been called by the plugin.
Richard Hughes57d18222017-01-10 16:02:59 +0000196 *
Richard Hughes4eada342017-10-03 21:20:32 +0100197 * Returns: (transfer none): a pointer to a structure, or %NULL for unset.
Richard Hughes57d18222017-01-10 16:02:59 +0000198 *
199 * Since: 0.8.0
200 **/
Richard Hughescff38bc2016-12-12 12:03:37 +0000201FuPluginData *
202fu_plugin_get_data (FuPlugin *plugin)
203{
204 FuPluginPrivate *priv = GET_PRIVATE (plugin);
Richard Hughesccd78a92017-01-11 16:57:41 +0000205 g_return_val_if_fail (FU_IS_PLUGIN (plugin), NULL);
Richard Hughescff38bc2016-12-12 12:03:37 +0000206 return priv->data;
207}
208
Richard Hughes57d18222017-01-10 16:02:59 +0000209/**
210 * fu_plugin_alloc_data:
211 * @plugin: A #FuPlugin
212 * @data_sz: the size to allocate
213 *
214 * Allocates the per-plugin allocated private data.
215 *
216 * Returns: (transfer full): a pointer to a structure, or %NULL for unset.
217 *
218 * Since: 0.8.0
219 **/
Richard Hughescff38bc2016-12-12 12:03:37 +0000220FuPluginData *
221fu_plugin_alloc_data (FuPlugin *plugin, gsize data_sz)
222{
223 FuPluginPrivate *priv = GET_PRIVATE (plugin);
Richard Hughesccd78a92017-01-11 16:57:41 +0000224 g_return_val_if_fail (FU_IS_PLUGIN (plugin), NULL);
Richard Hughes44dee882017-01-11 08:31:10 +0000225 if (priv->data != NULL) {
226 g_critical ("fu_plugin_alloc_data() already used by plugin");
227 return priv->data;
228 }
Richard Hughescff38bc2016-12-12 12:03:37 +0000229 priv->data = g_malloc0 (data_sz);
230 return priv->data;
Richard Hughesd0905142016-03-13 09:46:49 +0000231}
232
Richard Hughes57d18222017-01-10 16:02:59 +0000233/**
234 * fu_plugin_get_usb_context:
235 * @plugin: A #FuPlugin
236 *
237 * Gets the shared USB context that all plugins can use.
238 *
239 * Returns: (transfer none): a #GUsbContext.
240 *
241 * Since: 0.8.0
242 **/
Richard Hughesbc93e4a2016-12-08 17:29:51 +0000243GUsbContext *
244fu_plugin_get_usb_context (FuPlugin *plugin)
245{
Richard Hughescff38bc2016-12-12 12:03:37 +0000246 FuPluginPrivate *priv = GET_PRIVATE (plugin);
Richard Hughesccd78a92017-01-11 16:57:41 +0000247 g_return_val_if_fail (FU_IS_PLUGIN (plugin), NULL);
Richard Hughescff38bc2016-12-12 12:03:37 +0000248 return priv->usb_ctx;
Richard Hughesbc93e4a2016-12-08 17:29:51 +0000249}
250
251void
252fu_plugin_set_usb_context (FuPlugin *plugin, GUsbContext *usb_ctx)
253{
Richard Hughescff38bc2016-12-12 12:03:37 +0000254 FuPluginPrivate *priv = GET_PRIVATE (plugin);
255 g_set_object (&priv->usb_ctx, usb_ctx);
256}
257
Richard Hughes57d18222017-01-10 16:02:59 +0000258/**
259 * fu_plugin_get_enabled:
260 * @plugin: A #FuPlugin
261 *
Richard Hughes4eada342017-10-03 21:20:32 +0100262 * Returns if the plugin is enabled. Plugins may self-disable using
263 * fu_plugin_set_enabled() or can be disabled by the daemon.
Richard Hughes57d18222017-01-10 16:02:59 +0000264 *
265 * Returns: %TRUE if the plugin is currently enabled.
266 *
267 * Since: 0.8.0
268 **/
Richard Hughescff38bc2016-12-12 12:03:37 +0000269gboolean
270fu_plugin_get_enabled (FuPlugin *plugin)
271{
272 FuPluginPrivate *priv = GET_PRIVATE (plugin);
Richard Hughesccd78a92017-01-11 16:57:41 +0000273 g_return_val_if_fail (FU_IS_PLUGIN (plugin), FALSE);
Richard Hughescff38bc2016-12-12 12:03:37 +0000274 return priv->enabled;
Richard Hughesbc93e4a2016-12-08 17:29:51 +0000275}
276
Richard Hughes57d18222017-01-10 16:02:59 +0000277/**
278 * fu_plugin_set_enabled:
279 * @plugin: A #FuPlugin
280 * @enabled: the enabled value
281 *
282 * Enables or disables a plugin. Plugins can self-disable at any point.
283 *
284 * Since: 0.8.0
285 **/
Richard Hughesd0905142016-03-13 09:46:49 +0000286void
Richard Hughescff38bc2016-12-12 12:03:37 +0000287fu_plugin_set_enabled (FuPlugin *plugin, gboolean enabled)
Richard Hughesd0905142016-03-13 09:46:49 +0000288{
Richard Hughescff38bc2016-12-12 12:03:37 +0000289 FuPluginPrivate *priv = GET_PRIVATE (plugin);
Richard Hughesccd78a92017-01-11 16:57:41 +0000290 g_return_if_fail (FU_IS_PLUGIN (plugin));
Richard Hughescff38bc2016-12-12 12:03:37 +0000291 priv->enabled = enabled;
292}
293
294gboolean
295fu_plugin_open (FuPlugin *plugin, const gchar *filename, GError **error)
296{
297 FuPluginPrivate *priv = GET_PRIVATE (plugin);
298 FuPluginInitFunc func = NULL;
299 gchar *str;
300
301 priv->module = g_module_open (filename, 0);
302 if (priv->module == NULL) {
303 g_set_error (error,
304 G_IO_ERROR,
305 G_IO_ERROR_FAILED,
306 "failed to open plugin: %s",
307 g_module_error ());
308 return FALSE;
309 }
310
311 /* set automatically */
312 str = g_strstr_len (filename, -1, "libfu_plugin_");
313 if (str != NULL) {
314 priv->name = g_strdup (str + 13);
315 g_strdelimit (priv->name, ".", '\0');
316 }
Richard Hughesd0905142016-03-13 09:46:49 +0000317
318 /* optional */
Richard Hughescff38bc2016-12-12 12:03:37 +0000319 g_module_symbol (priv->module, "fu_plugin_init", (gpointer *) &func);
320 if (func != NULL) {
321 g_debug ("performing init() on %s", filename);
Richard Hughesd0905142016-03-13 09:46:49 +0000322 func (plugin);
323 }
324
Richard Hughescff38bc2016-12-12 12:03:37 +0000325 return TRUE;
326}
327
Richard Hughes57d18222017-01-10 16:02:59 +0000328/**
329 * fu_plugin_device_add:
330 * @plugin: A #FuPlugin
331 * @device: A #FuDevice
332 *
333 * Asks the daemon to add a device to the exported list. If this device ID
334 * has already been added by a different plugin then this request will be
335 * ignored.
336 *
337 * Plugins should use fu_plugin_device_add_delay() if they are not capable of
338 * actually flashing an image to the hardware so that higher-priority plugins
339 * can add the device themselves.
340 *
341 * Since: 0.8.0
342 **/
Richard Hughescff38bc2016-12-12 12:03:37 +0000343void
344fu_plugin_device_add (FuPlugin *plugin, FuDevice *device)
345{
Richard Hughesccd78a92017-01-11 16:57:41 +0000346 g_return_if_fail (FU_IS_PLUGIN (plugin));
347 g_return_if_fail (FU_IS_DEVICE (device));
348
Richard Hughescff38bc2016-12-12 12:03:37 +0000349 g_debug ("emit added from %s: %s",
350 fu_plugin_get_name (plugin),
351 fu_device_get_id (device));
352 fu_device_set_created (device, (guint64) g_get_real_time () / G_USEC_PER_SEC);
353 fu_device_set_plugin (device, fu_plugin_get_name (plugin));
354 g_signal_emit (plugin, signals[SIGNAL_DEVICE_ADDED], 0, device);
355}
356
Richard Hughese1fd34d2017-08-24 14:19:51 +0100357/**
358 * fu_plugin_device_register:
359 * @plugin: A #FuPlugin
360 * @device: A #FuDevice
361 *
362 * Registers the device with other plugins so they can set metadata.
363 *
364 * Plugins do not have to call this manually as this is done automatically
365 * when using fu_plugin_device_add(). They may wish to use this manually
366 * if for intance the coldplug should be ignored based on the metadata
367 * set from other plugins.
368 *
369 * Since: 0.9.7
370 **/
371void
372fu_plugin_device_register (FuPlugin *plugin, FuDevice *device)
373{
374 g_return_if_fail (FU_IS_PLUGIN (plugin));
375 g_return_if_fail (FU_IS_DEVICE (device));
376
377 g_debug ("emit device-register from %s: %s",
378 fu_plugin_get_name (plugin),
379 fu_device_get_id (device));
380 g_signal_emit (plugin, signals[SIGNAL_DEVICE_REGISTER], 0, device);
381}
382
Richard Hughesae3d65f2016-12-16 09:38:01 +0000383typedef struct {
384 FuPlugin *plugin;
385 FuDevice *device;
386 guint timeout_id;
Richard Hughesd4184cf2016-12-21 16:05:17 +0000387 GHashTable *devices;
Richard Hughesae3d65f2016-12-16 09:38:01 +0000388} FuPluginHelper;
389
390static void
391fu_plugin_helper_free (FuPluginHelper *helper)
392{
393 g_object_unref (helper->plugin);
394 g_object_unref (helper->device);
Richard Hughesd4184cf2016-12-21 16:05:17 +0000395 g_hash_table_unref (helper->devices);
Richard Hughesae3d65f2016-12-16 09:38:01 +0000396 g_free (helper);
397}
398
399static gboolean
400fu_plugin_device_add_delay_cb (gpointer user_data)
401{
402 FuPluginHelper *helper = (FuPluginHelper *) user_data;
403 fu_plugin_device_add (helper->plugin, helper->device);
Richard Hughes0dec2742017-09-18 11:07:39 +0100404 g_hash_table_remove (helper->devices, helper->device);
Richard Hughesae3d65f2016-12-16 09:38:01 +0000405 fu_plugin_helper_free (helper);
406 return FALSE;
407}
408
Richard Hughes57d18222017-01-10 16:02:59 +0000409/**
Richard Hughesf0a799e2017-01-17 20:13:30 +0000410 * fu_plugin_has_device_delay:
411 * @plugin: A #FuPlugin
412 *
413 * Returns if the device has a pending device that is waiting to be added.
414 *
415 * Returns: %TRUE if a device is waiting to be added
416 *
417 * Since: 0.8.0
418 **/
419gboolean
420fu_plugin_has_device_delay (FuPlugin *plugin)
421{
422 FuPluginPrivate *priv = GET_PRIVATE (plugin);
423 return g_hash_table_size (priv->devices_delay) > 0;
424}
425
426/**
Richard Hughes57d18222017-01-10 16:02:59 +0000427 * fu_plugin_device_add_delay:
428 * @plugin: A #FuPlugin
429 * @device: A #FuDevice
430 *
431 * Asks the daemon to add a device to the exported list after a small delay.
432 *
433 * Since: 0.8.0
434 **/
Richard Hughesae3d65f2016-12-16 09:38:01 +0000435void
436fu_plugin_device_add_delay (FuPlugin *plugin, FuDevice *device)
437{
438 FuPluginPrivate *priv = GET_PRIVATE (plugin);
439 FuPluginHelper *helper;
Richard Hughesccd78a92017-01-11 16:57:41 +0000440
441 g_return_if_fail (FU_IS_PLUGIN (plugin));
442 g_return_if_fail (FU_IS_DEVICE (device));
443
Richard Hughes6ad951d2017-02-03 10:12:12 +0000444 /* already waiting for add */
445 helper = g_hash_table_lookup (priv->devices_delay, device);
446 if (helper != NULL) {
Richard Hughes3cca1c62017-07-21 13:38:20 +0100447 g_debug ("ignoring add-delay as device %s already pending",
448 fu_device_get_id (device));
Richard Hughes6ad951d2017-02-03 10:12:12 +0000449 return;
450 }
451
452 /* add after a small delay */
Richard Hughesae3d65f2016-12-16 09:38:01 +0000453 g_debug ("waiting a small time for other plugins");
454 helper = g_new0 (FuPluginHelper, 1);
455 helper->plugin = g_object_ref (plugin);
456 helper->device = g_object_ref (device);
457 helper->timeout_id = g_timeout_add (500, fu_plugin_device_add_delay_cb, helper);
Richard Hughesd4184cf2016-12-21 16:05:17 +0000458 helper->devices = g_hash_table_ref (priv->devices_delay);
459 g_hash_table_insert (helper->devices, device, helper);
Richard Hughesae3d65f2016-12-16 09:38:01 +0000460}
461
Richard Hughes57d18222017-01-10 16:02:59 +0000462/**
Richard Hughes4eada342017-10-03 21:20:32 +0100463 * fu_plugin_device_remove:
Richard Hughes57d18222017-01-10 16:02:59 +0000464 * @plugin: A #FuPlugin
465 * @device: A #FuDevice
466 *
467 * Asks the daemon to remove a device from the exported list.
468 *
469 * Since: 0.8.0
470 **/
Richard Hughescff38bc2016-12-12 12:03:37 +0000471void
472fu_plugin_device_remove (FuPlugin *plugin, FuDevice *device)
473{
Richard Hughesae3d65f2016-12-16 09:38:01 +0000474 FuPluginPrivate *priv = GET_PRIVATE (plugin);
475 FuPluginHelper *helper;
476
Richard Hughesccd78a92017-01-11 16:57:41 +0000477 g_return_if_fail (FU_IS_PLUGIN (plugin));
478 g_return_if_fail (FU_IS_DEVICE (device));
479
Richard Hughesae3d65f2016-12-16 09:38:01 +0000480 /* waiting for add */
481 helper = g_hash_table_lookup (priv->devices_delay, device);
482 if (helper != NULL) {
483 g_debug ("ignoring remove from delayed addition");
484 g_source_remove (helper->timeout_id);
Richard Hughesd4184cf2016-12-21 16:05:17 +0000485 g_hash_table_remove (priv->devices_delay, helper->device);
Richard Hughesae3d65f2016-12-16 09:38:01 +0000486 fu_plugin_helper_free (helper);
487 return;
488 }
489
Richard Hughescff38bc2016-12-12 12:03:37 +0000490 g_debug ("emit removed from %s: %s",
491 fu_plugin_get_name (plugin),
492 fu_device_get_id (device));
493 g_signal_emit (plugin, signals[SIGNAL_DEVICE_REMOVED], 0, device);
494}
495
Richard Hughes57d18222017-01-10 16:02:59 +0000496/**
Richard Hughes2de8f132018-01-17 09:12:02 +0000497 * fu_plugin_request_recoldplug:
Richard Hughes362d6d72017-01-07 21:42:14 +0000498 * @plugin: A #FuPlugin
499 *
500 * Ask all the plugins to coldplug all devices, which will include the prepare()
501 * and cleanup() phases. Duplicate devices added will be ignored.
502 *
503 * Since: 0.8.0
504 **/
505void
Richard Hughes2de8f132018-01-17 09:12:02 +0000506fu_plugin_request_recoldplug (FuPlugin *plugin)
Richard Hughes362d6d72017-01-07 21:42:14 +0000507{
Richard Hughesccd78a92017-01-11 16:57:41 +0000508 g_return_if_fail (FU_IS_PLUGIN (plugin));
Richard Hughes362d6d72017-01-07 21:42:14 +0000509 g_signal_emit (plugin, signals[SIGNAL_RECOLDPLUG], 0);
510}
511
Richard Hughesb0829032017-01-10 09:27:08 +0000512/**
Richard Hughesb8f8db22017-04-25 15:56:00 +0100513 * fu_plugin_check_hwid:
514 * @plugin: A #FuPlugin
Richard Hughes4eada342017-10-03 21:20:32 +0100515 * @hwid: A Hardware ID GUID, e.g. `6de5d951-d755-576b-bd09-c5cf66b27234`
Richard Hughesb8f8db22017-04-25 15:56:00 +0100516 *
Richard Hughesd7704d42017-08-08 20:29:09 +0100517 * Checks to see if a specific GUID exists. All hardware IDs on a
Richard Hughesb8f8db22017-04-25 15:56:00 +0100518 * specific system can be shown using the `fwupdmgr hwids` command.
519 *
Richard Hughes4eada342017-10-03 21:20:32 +0100520 * Returns: %TRUE if the HwId is found on the system.
521 *
Richard Hughesb8f8db22017-04-25 15:56:00 +0100522 * Since: 0.9.1
523 **/
524gboolean
525fu_plugin_check_hwid (FuPlugin *plugin, const gchar *hwid)
526{
527 FuPluginPrivate *priv = GET_PRIVATE (plugin);
528 if (priv->hwids == NULL)
529 return FALSE;
Richard Hughesd7704d42017-08-08 20:29:09 +0100530 return fu_hwids_has_guid (priv->hwids, hwid);
531}
532
533/**
Richard Hughes1354ea92017-09-19 15:58:31 +0100534 * fu_plugin_check_supported:
535 * @plugin: A #FuPlugin
Richard Hughes4eada342017-10-03 21:20:32 +0100536 * @guid: A Hardware ID GUID, e.g. `6de5d951-d755-576b-bd09-c5cf66b27234`
Richard Hughes1354ea92017-09-19 15:58:31 +0100537 *
538 * Checks to see if a specific device GUID is supported, i.e. available in the
539 * AppStream metadata.
540 *
Richard Hughes4eada342017-10-03 21:20:32 +0100541 * Returns: %TRUE if the device is supported.
542 *
Richard Hughes1354ea92017-09-19 15:58:31 +0100543 * Since: 1.0.0
544 **/
545gboolean
546fu_plugin_check_supported (FuPlugin *plugin, const gchar *guid)
547{
548 FuPluginPrivate *priv = GET_PRIVATE (plugin);
549 if (priv->supported_guids == NULL)
550 return FALSE;
551 for (guint i = 0; i < priv->supported_guids->len; i++) {
552 const gchar *guid_tmp = g_ptr_array_index (priv->supported_guids, i);
553 if (g_strcmp0 (guid, guid_tmp) == 0)
554 return TRUE;
555 }
556 return FALSE;
557}
558
559/**
Richard Hughesd7704d42017-08-08 20:29:09 +0100560 * fu_plugin_get_dmi_value:
561 * @plugin: A #FuPlugin
Richard Hughes4eada342017-10-03 21:20:32 +0100562 * @dmi_id: A DMI ID, e.g. `BiosVersion`
Richard Hughesd7704d42017-08-08 20:29:09 +0100563 *
564 * Gets a hardware DMI value.
565 *
Richard Hughes4eada342017-10-03 21:20:32 +0100566 * Returns: The string, or %NULL
567 *
Richard Hughesd7704d42017-08-08 20:29:09 +0100568 * Since: 0.9.7
569 **/
570const gchar *
571fu_plugin_get_dmi_value (FuPlugin *plugin, const gchar *dmi_id)
572{
573 FuPluginPrivate *priv = GET_PRIVATE (plugin);
574 if (priv->hwids == NULL)
Richard Hughes7ef96b82017-08-23 18:28:24 +0100575 return NULL;
Richard Hughesd7704d42017-08-08 20:29:09 +0100576 return fu_hwids_get_value (priv->hwids, dmi_id);
Richard Hughesb8f8db22017-04-25 15:56:00 +0100577}
578
Richard Hughes49e5e052017-09-03 12:15:41 +0100579/**
580 * fu_plugin_get_smbios_string:
581 * @plugin: A #FuPlugin
582 * @structure_type: A SMBIOS structure type, e.g. %FU_SMBIOS_STRUCTURE_TYPE_BIOS
583 * @offset: A SMBIOS offset
584 *
585 * Gets a hardware SMBIOS string.
586 *
587 * The @type and @offset can be referenced from the DMTF SMBIOS specification:
588 * https://www.dmtf.org/sites/default/files/standards/documents/DSP0134_3.1.1.pdf
589 *
Richard Hughes4eada342017-10-03 21:20:32 +0100590 * Returns: A string, or %NULL
591 *
Richard Hughes49e5e052017-09-03 12:15:41 +0100592 * Since: 0.9.8
593 **/
594const gchar *
595fu_plugin_get_smbios_string (FuPlugin *plugin, guint8 structure_type, guint8 offset)
596{
597 FuPluginPrivate *priv = GET_PRIVATE (plugin);
598 if (priv->smbios == NULL)
599 return NULL;
600 return fu_smbios_get_string (priv->smbios, structure_type, offset, NULL);
601}
602
603/**
604 * fu_plugin_get_smbios_data:
605 * @plugin: A #FuPlugin
606 * @structure_type: A SMBIOS structure type, e.g. %FU_SMBIOS_STRUCTURE_TYPE_BIOS
607 *
608 * Gets a hardware SMBIOS data.
609 *
Richard Hughes4eada342017-10-03 21:20:32 +0100610 * Returns: (transfer none): A #GBytes, or %NULL
611 *
Richard Hughes49e5e052017-09-03 12:15:41 +0100612 * Since: 0.9.8
613 **/
614GBytes *
615fu_plugin_get_smbios_data (FuPlugin *plugin, guint8 structure_type)
616{
617 FuPluginPrivate *priv = GET_PRIVATE (plugin);
618 if (priv->smbios == NULL)
619 return NULL;
620 return fu_smbios_get_data (priv->smbios, structure_type, NULL);
621}
622
Richard Hughesb8f8db22017-04-25 15:56:00 +0100623void
Richard Hughesd7704d42017-08-08 20:29:09 +0100624fu_plugin_set_hwids (FuPlugin *plugin, FuHwids *hwids)
Richard Hughesb8f8db22017-04-25 15:56:00 +0100625{
626 FuPluginPrivate *priv = GET_PRIVATE (plugin);
Richard Hughesd7704d42017-08-08 20:29:09 +0100627 g_set_object (&priv->hwids, hwids);
Richard Hughesb8f8db22017-04-25 15:56:00 +0100628}
629
Richard Hughes49e5e052017-09-03 12:15:41 +0100630void
Richard Hughes1354ea92017-09-19 15:58:31 +0100631fu_plugin_set_supported (FuPlugin *plugin, GPtrArray *supported_guids)
632{
633 FuPluginPrivate *priv = GET_PRIVATE (plugin);
634 if (priv->supported_guids != NULL)
635 g_ptr_array_unref (priv->supported_guids);
636 priv->supported_guids = g_ptr_array_ref (supported_guids);
637}
638
Richard Hughes9c028f02017-10-28 21:14:28 +0100639void
640fu_plugin_set_quirks (FuPlugin *plugin, FuQuirks *quirks)
641{
642 FuPluginPrivate *priv = GET_PRIVATE (plugin);
643 g_set_object (&priv->quirks, quirks);
644}
645
646/**
647 * fu_plugin_get_quirks:
648 * @plugin: A #FuPlugin
649 *
650 * Returns the hardware database object. This can be used to discover device
651 * quirks or other device-specific settings.
652 *
653 * Returns: (transfer none): a #FuQuirks, or %NULL if not set
654 *
655 * Since: 1.0.1
656 **/
657FuQuirks *
658fu_plugin_get_quirks (FuPlugin *plugin)
659{
660 FuPluginPrivate *priv = GET_PRIVATE (plugin);
661 return priv->quirks;
662}
663
Richard Hughes0eb123b2018-04-19 12:00:04 +0100664void
665fu_plugin_set_runtime_versions (FuPlugin *plugin, GHashTable *runtime_versions)
666{
667 FuPluginPrivate *priv = GET_PRIVATE (plugin);
668 priv->runtime_versions = g_hash_table_ref (runtime_versions);
669}
670
671/**
672 * fu_plugin_add_runtime_version:
673 * @plugin: A #FuPlugin
674 * @component_id: An AppStream component id, e.g. "org.gnome.Software"
675 * @version: A version string, e.g. "1.2.3"
676 *
677 * Sets a runtime version of a specific dependancy.
678 *
679 * Since: 1.0.7
680 **/
681void
682fu_plugin_add_runtime_version (FuPlugin *plugin,
683 const gchar *component_id,
684 const gchar *version)
685{
686 FuPluginPrivate *priv = GET_PRIVATE (plugin);
Richard Hughesb01b4862018-04-20 16:39:48 +0100687 if (priv->runtime_versions == NULL)
688 return;
Richard Hughes0eb123b2018-04-19 12:00:04 +0100689 g_hash_table_insert (priv->runtime_versions,
690 g_strdup (component_id),
691 g_strdup (version));
692}
693
Richard Hughes9c028f02017-10-28 21:14:28 +0100694/**
695 * fu_plugin_lookup_quirk_by_id:
696 * @plugin: A #FuPlugin
697 * @prefix: A string prefix that matches the quirks file basename, e.g. "dfu-quirks"
698 * @id: An ID to match the entry, e.g. "012345"
699 *
700 * Looks up an entry in the hardware database using a string value.
701 *
702 * Returns: (transfer none): values from the database, or %NULL if not found
703 *
704 * Since: 1.0.1
705 **/
706const gchar *
707fu_plugin_lookup_quirk_by_id (FuPlugin *plugin, const gchar *prefix, const gchar *id)
708{
709 FuPluginPrivate *priv = GET_PRIVATE (plugin);
710 g_return_val_if_fail (FU_IS_PLUGIN (plugin), NULL);
711
712 /* wildcard */
713 if (g_strstr_len (id, -1, "*") != NULL)
714 return fu_quirks_lookup_by_glob (priv->quirks, prefix, id);
715
716 /* exact ID */
717 return fu_quirks_lookup_by_id (priv->quirks, prefix, id);
718}
719
720/**
721 * fu_plugin_lookup_quirk_by_usb_device:
722 * @plugin: A #FuPlugin
723 * @prefix: A string prefix that matches the quirks file basename, e.g. "dfu-quirks"
724 * @dev: A #GUsbDevice
725 *
726 * Looks up an entry in the hardware database using various keys generated
727 * from @dev.
728 *
729 * Returns: (transfer none): values from the database, or %NULL if not found
730 *
731 * Since: 1.0.1
732 **/
733const gchar *
734fu_plugin_lookup_quirk_by_usb_device (FuPlugin *plugin, const gchar *prefix, GUsbDevice *dev)
735{
736 FuPluginPrivate *priv = GET_PRIVATE (plugin);
737 g_return_val_if_fail (FU_IS_PLUGIN (plugin), NULL);
738 return fu_quirks_lookup_by_usb_device (priv->quirks, prefix, dev);
739}
740
Richard Hughes1354ea92017-09-19 15:58:31 +0100741/**
742 * fu_plugin_get_supported:
743 * @plugin: A #FuPlugin
744 *
745 * Gets all the device GUIDs supported by the daemon.
746 *
Richard Hughes4eada342017-10-03 21:20:32 +0100747 * Returns: (element-type utf8) (transfer none): GUIDs
748 *
Richard Hughes1354ea92017-09-19 15:58:31 +0100749 * Since: 1.0.0
750 **/
751GPtrArray *
752fu_plugin_get_supported (FuPlugin *plugin)
753{
754 FuPluginPrivate *priv = GET_PRIVATE (plugin);
755 return priv->supported_guids;
756}
757
758void
Richard Hughes49e5e052017-09-03 12:15:41 +0100759fu_plugin_set_smbios (FuPlugin *plugin, FuSmbios *smbios)
760{
761 FuPluginPrivate *priv = GET_PRIVATE (plugin);
762 g_set_object (&priv->smbios, smbios);
763}
764
Richard Hughesb8f8db22017-04-25 15:56:00 +0100765/**
Richard Hughesb0829032017-01-10 09:27:08 +0000766 * fu_plugin_set_coldplug_delay:
767 * @plugin: A #FuPlugin
768 * @duration: A delay in milliseconds
769 *
770 * Set the minimum time that should be waited inbetween the call to
771 * fu_plugin_coldplug_prepare() and fu_plugin_coldplug(). This is usually going
772 * to be the minimum hardware initialisation time from a datasheet.
773 *
774 * It is better to use this function rather than using a sleep() in the plugin
775 * itself as then only one delay is done in the daemon rather than waiting for
776 * each coldplug prepare in a serial way.
777 *
778 * Additionally, very long delays should be avoided as the daemon will be
779 * blocked from processing requests whilst the coldplug delay is being
780 * performed.
781 *
782 * Since: 0.8.0
783 **/
784void
785fu_plugin_set_coldplug_delay (FuPlugin *plugin, guint duration)
786{
787 g_return_if_fail (FU_IS_PLUGIN (plugin));
788 g_return_if_fail (duration > 0);
789
790 /* check sanity */
791 if (duration > FU_PLUGIN_COLDPLUG_DELAY_MAXIMUM) {
792 g_warning ("duration of %ums is crazy, truncating to %ums",
793 duration,
794 FU_PLUGIN_COLDPLUG_DELAY_MAXIMUM);
795 duration = FU_PLUGIN_COLDPLUG_DELAY_MAXIMUM;
796 }
797
798 /* emit */
799 g_signal_emit (plugin, signals[SIGNAL_SET_COLDPLUG_DELAY], 0, duration);
800}
801
Richard Hughesd0905142016-03-13 09:46:49 +0000802gboolean
Richard Hughescff38bc2016-12-12 12:03:37 +0000803fu_plugin_runner_startup (FuPlugin *plugin, GError **error)
Richard Hughesd0905142016-03-13 09:46:49 +0000804{
Richard Hughescff38bc2016-12-12 12:03:37 +0000805 FuPluginPrivate *priv = GET_PRIVATE (plugin);
Richard Hughes7b8b2022016-12-12 16:15:03 +0000806 FuPluginStartupFunc func = NULL;
Richard Hughesd0905142016-03-13 09:46:49 +0000807
808 /* not enabled */
Richard Hughescff38bc2016-12-12 12:03:37 +0000809 if (!priv->enabled)
Richard Hughesd0905142016-03-13 09:46:49 +0000810 return TRUE;
811
Richard Hughes639da472018-01-06 22:35:04 +0000812 /* no object loaded */
813 if (priv->module == NULL)
814 return TRUE;
815
Richard Hughesd0905142016-03-13 09:46:49 +0000816 /* optional */
Richard Hughescff38bc2016-12-12 12:03:37 +0000817 g_module_symbol (priv->module, "fu_plugin_startup", (gpointer *) &func);
818 if (func == NULL)
Richard Hughesd0905142016-03-13 09:46:49 +0000819 return TRUE;
Richard Hughescff38bc2016-12-12 12:03:37 +0000820 g_debug ("performing startup() on %s", priv->name);
Richard Hughesd0905142016-03-13 09:46:49 +0000821 if (!func (plugin, error)) {
Richard Hughescff38bc2016-12-12 12:03:37 +0000822 g_prefix_error (error, "failed to startup %s: ", priv->name);
823 return FALSE;
824 }
825 return TRUE;
826}
827
828static gboolean
829fu_plugin_runner_offline_invalidate (GError **error)
830{
831 g_autoptr(GError) error_local = NULL;
832 g_autoptr(GFile) file1 = NULL;
833
834 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
835
836 file1 = g_file_new_for_path (FU_OFFLINE_TRIGGER_FILENAME);
837 if (!g_file_query_exists (file1, NULL))
838 return TRUE;
839 if (!g_file_delete (file1, NULL, &error_local)) {
840 g_set_error (error,
841 FWUPD_ERROR,
842 FWUPD_ERROR_INTERNAL,
843 "Cannot delete %s: %s",
844 FU_OFFLINE_TRIGGER_FILENAME,
845 error_local->message);
846 return FALSE;
847 }
848 return TRUE;
849}
850
851static gboolean
852fu_plugin_runner_offline_setup (GError **error)
853{
854 gint rc;
855
856 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
857
858 /* create symlink for the systemd-system-update-generator */
859 rc = symlink ("/var/lib/fwupd", FU_OFFLINE_TRIGGER_FILENAME);
860 if (rc < 0) {
861 g_set_error (error,
862 FWUPD_ERROR,
863 FWUPD_ERROR_INTERNAL,
864 "Failed to create symlink %s to %s: %s",
865 FU_OFFLINE_TRIGGER_FILENAME,
866 "/var/lib", strerror (errno));
Richard Hughesd0905142016-03-13 09:46:49 +0000867 return FALSE;
868 }
869 return TRUE;
870}
871
Richard Hughes0d7fdb32017-11-11 20:23:14 +0000872static gboolean
873fu_plugin_runner_device_generic (FuPlugin *plugin, FuDevice *device,
874 const gchar *symbol_name, GError **error)
875{
876 FuPluginPrivate *priv = GET_PRIVATE (plugin);
877 FuPluginDeviceFunc func = NULL;
878
879 /* not enabled */
880 if (!priv->enabled)
881 return TRUE;
882
Richard Hughesd3d96cc2017-11-14 11:34:33 +0000883 /* no object loaded */
884 if (priv->module == NULL)
885 return TRUE;
886
Richard Hughes0d7fdb32017-11-11 20:23:14 +0000887 /* optional */
888 g_module_symbol (priv->module, symbol_name, (gpointer *) &func);
889 if (func == NULL)
890 return TRUE;
891 g_debug ("performing %s() on %s", symbol_name + 10, priv->name);
892 if (!func (plugin, device, error)) {
Richard Hughes83e54e42018-01-31 23:27:30 +0000893 g_prefix_error (error, "failed to run %s() on %s: ",
Richard Hughes0d7fdb32017-11-11 20:23:14 +0000894 symbol_name + 10,
Richard Hughes0d7fdb32017-11-11 20:23:14 +0000895 priv->name);
896 return FALSE;
897 }
898 return TRUE;
899}
900
Richard Hughesd0905142016-03-13 09:46:49 +0000901gboolean
Richard Hughescff38bc2016-12-12 12:03:37 +0000902fu_plugin_runner_coldplug (FuPlugin *plugin, GError **error)
Richard Hughesd0905142016-03-13 09:46:49 +0000903{
Richard Hughescff38bc2016-12-12 12:03:37 +0000904 FuPluginPrivate *priv = GET_PRIVATE (plugin);
Richard Hughes7b8b2022016-12-12 16:15:03 +0000905 FuPluginStartupFunc func = NULL;
Richard Hughesd0905142016-03-13 09:46:49 +0000906
907 /* not enabled */
Richard Hughescff38bc2016-12-12 12:03:37 +0000908 if (!priv->enabled)
Richard Hughesd0905142016-03-13 09:46:49 +0000909 return TRUE;
910
Richard Hughes639da472018-01-06 22:35:04 +0000911 /* no object loaded */
912 if (priv->module == NULL)
913 return TRUE;
914
Richard Hughesd0905142016-03-13 09:46:49 +0000915 /* optional */
Richard Hughescff38bc2016-12-12 12:03:37 +0000916 g_module_symbol (priv->module, "fu_plugin_coldplug", (gpointer *) &func);
917 if (func == NULL)
Richard Hughesd0905142016-03-13 09:46:49 +0000918 return TRUE;
Richard Hughescff38bc2016-12-12 12:03:37 +0000919 g_debug ("performing coldplug() on %s", priv->name);
920 if (!func (plugin, error)) {
921 g_prefix_error (error, "failed to coldplug %s: ", priv->name);
922 return FALSE;
923 }
924 return TRUE;
925}
926
Richard Hughes7b8b2022016-12-12 16:15:03 +0000927gboolean
Richard Hughes2de8f132018-01-17 09:12:02 +0000928fu_plugin_runner_recoldplug (FuPlugin *plugin, GError **error)
929{
930 FuPluginPrivate *priv = GET_PRIVATE (plugin);
931 FuPluginStartupFunc func = NULL;
932
933 /* not enabled */
934 if (!priv->enabled)
935 return TRUE;
936
937 /* no object loaded */
938 if (priv->module == NULL)
939 return TRUE;
940
941 /* optional */
942 g_module_symbol (priv->module, "fu_plugin_recoldplug", (gpointer *) &func);
943 if (func == NULL)
944 return TRUE;
945 g_debug ("performing recoldplug() on %s", priv->name);
946 if (!func (plugin, error)) {
947 g_prefix_error (error, "failed to recoldplug %s: ", priv->name);
948 return FALSE;
949 }
950 return TRUE;
951}
952
953gboolean
Richard Hughes46487c92017-01-07 21:26:34 +0000954fu_plugin_runner_coldplug_prepare (FuPlugin *plugin, GError **error)
955{
956 FuPluginPrivate *priv = GET_PRIVATE (plugin);
957 FuPluginStartupFunc func = NULL;
958
959 /* not enabled */
960 if (!priv->enabled)
961 return TRUE;
962
Richard Hughes639da472018-01-06 22:35:04 +0000963 /* no object loaded */
964 if (priv->module == NULL)
965 return TRUE;
966
Richard Hughes46487c92017-01-07 21:26:34 +0000967 /* optional */
968 g_module_symbol (priv->module, "fu_plugin_coldplug_prepare", (gpointer *) &func);
969 if (func == NULL)
970 return TRUE;
971 g_debug ("performing coldplug_prepare() on %s", priv->name);
972 if (!func (plugin, error)) {
973 g_prefix_error (error, "failed to prepare for coldplug %s: ", priv->name);
974 return FALSE;
975 }
976 return TRUE;
977}
978
979gboolean
980fu_plugin_runner_coldplug_cleanup (FuPlugin *plugin, GError **error)
981{
982 FuPluginPrivate *priv = GET_PRIVATE (plugin);
983 FuPluginStartupFunc func = NULL;
984
985 /* not enabled */
986 if (!priv->enabled)
987 return TRUE;
988
Richard Hughes639da472018-01-06 22:35:04 +0000989 /* no object loaded */
990 if (priv->module == NULL)
991 return TRUE;
992
Richard Hughes46487c92017-01-07 21:26:34 +0000993 /* optional */
994 g_module_symbol (priv->module, "fu_plugin_coldplug_cleanup", (gpointer *) &func);
995 if (func == NULL)
996 return TRUE;
997 g_debug ("performing coldplug_cleanup() on %s", priv->name);
998 if (!func (plugin, error)) {
999 g_prefix_error (error, "failed to cleanup coldplug %s: ", priv->name);
1000 return FALSE;
1001 }
1002 return TRUE;
1003}
1004
1005gboolean
Richard Hughes7b8b2022016-12-12 16:15:03 +00001006fu_plugin_runner_update_prepare (FuPlugin *plugin, FuDevice *device, GError **error)
1007{
Richard Hughes0d7fdb32017-11-11 20:23:14 +00001008 return fu_plugin_runner_device_generic (plugin, device,
1009 "fu_plugin_update_prepare", error);
Richard Hughes7b8b2022016-12-12 16:15:03 +00001010}
1011
1012gboolean
1013fu_plugin_runner_update_cleanup (FuPlugin *plugin, FuDevice *device, GError **error)
1014{
Richard Hughes0d7fdb32017-11-11 20:23:14 +00001015 return fu_plugin_runner_device_generic (plugin, device,
1016 "fu_plugin_update_cleanup", error);
1017}
Richard Hughes7b8b2022016-12-12 16:15:03 +00001018
Richard Hughes0d7fdb32017-11-11 20:23:14 +00001019gboolean
1020fu_plugin_runner_update_attach (FuPlugin *plugin, FuDevice *device, GError **error)
1021{
1022 return fu_plugin_runner_device_generic (plugin, device,
1023 "fu_plugin_update_attach", error);
1024}
Richard Hughes7b8b2022016-12-12 16:15:03 +00001025
Richard Hughes0d7fdb32017-11-11 20:23:14 +00001026gboolean
1027fu_plugin_runner_update_detach (FuPlugin *plugin, FuDevice *device, GError **error)
1028{
1029 return fu_plugin_runner_device_generic (plugin, device,
1030 "fu_plugin_update_detach", error);
1031}
1032
1033gboolean
1034fu_plugin_runner_update_reload (FuPlugin *plugin, FuDevice *device, GError **error)
1035{
1036 return fu_plugin_runner_device_generic (plugin, device,
1037 "fu_plugin_update_reload", error);
Richard Hughes7b8b2022016-12-12 16:15:03 +00001038}
1039
Richard Hughes104f6512017-11-24 11:44:57 +00001040gboolean
1041fu_plugin_runner_usb_device_added (FuPlugin *plugin, GUsbDevice *usb_device, GError **error)
1042{
1043 FuPluginPrivate *priv = GET_PRIVATE (plugin);
1044 FuPluginUsbDeviceAddedFunc func = NULL;
1045
1046 /* not enabled */
1047 if (!priv->enabled)
1048 return TRUE;
Richard Hughes639da472018-01-06 22:35:04 +00001049
1050 /* no object loaded */
Richard Hughes104f6512017-11-24 11:44:57 +00001051 if (priv->module == NULL)
1052 return TRUE;
1053
1054 /* optional */
1055 g_module_symbol (priv->module, "fu_plugin_usb_device_added", (gpointer *) &func);
1056 if (func != NULL) {
1057 g_debug ("performing usb_device_added() on %s", priv->name);
1058 return func (plugin, usb_device, error);
1059 }
1060 return TRUE;
1061}
1062
Richard Hughese1fd34d2017-08-24 14:19:51 +01001063void
1064fu_plugin_runner_device_register (FuPlugin *plugin, FuDevice *device)
1065{
1066 FuPluginPrivate *priv = GET_PRIVATE (plugin);
1067 FuPluginDeviceRegisterFunc func = NULL;
1068
1069 /* not enabled */
1070 if (!priv->enabled)
1071 return;
Richard Hughes34834102017-11-21 21:55:00 +00001072 if (priv->module == NULL)
1073 return;
Richard Hughese1fd34d2017-08-24 14:19:51 +01001074
1075 /* optional */
1076 g_module_symbol (priv->module, "fu_plugin_device_registered", (gpointer *) &func);
1077 if (func != NULL) {
1078 g_debug ("performing device_added() on %s", priv->name);
1079 func (plugin, device);
1080 }
1081}
1082
Richard Hughescff38bc2016-12-12 12:03:37 +00001083static gboolean
1084fu_plugin_runner_schedule_update (FuPlugin *plugin,
1085 FuDevice *device,
1086 GBytes *blob_cab,
1087 GError **error)
1088{
Richard Hughes38fb56c2018-02-01 22:07:52 +00001089 FwupdRelease *release;
Richard Hughescff38bc2016-12-12 12:03:37 +00001090 gchar tmpname[] = {"XXXXXX.cap"};
1091 g_autofree gchar *dirname = NULL;
1092 g_autofree gchar *filename = NULL;
Richard Hughes68982c62017-09-13 15:40:14 +01001093 g_autoptr(FuDevice) res_tmp = NULL;
Richard Hughes780ef3f2018-01-12 16:20:31 +00001094 g_autoptr(FuHistory) history = NULL;
Richard Hughes38fb56c2018-02-01 22:07:52 +00001095 g_autoptr(FwupdRelease) release_tmp = fwupd_release_new ();
Richard Hughescff38bc2016-12-12 12:03:37 +00001096 g_autoptr(GFile) file = NULL;
1097
1098 /* id already exists */
Richard Hughes780ef3f2018-01-12 16:20:31 +00001099 history = fu_history_new ();
Richard Hughes0b9d9962018-01-12 16:31:28 +00001100 res_tmp = fu_history_get_device_by_id (history, fu_device_get_id (device), NULL);
Richard Hughescff38bc2016-12-12 12:03:37 +00001101 if (res_tmp != NULL) {
1102 g_set_error (error,
1103 FWUPD_ERROR,
1104 FWUPD_ERROR_ALREADY_PENDING,
1105 "%s is already scheduled to be updated",
1106 fu_device_get_id (device));
1107 return FALSE;
1108 }
1109
1110 /* create directory */
1111 dirname = g_build_filename (LOCALSTATEDIR, "lib", "fwupd", NULL);
1112 file = g_file_new_for_path (dirname);
1113 if (!g_file_query_exists (file, NULL)) {
1114 if (!g_file_make_directory_with_parents (file, NULL, error))
1115 return FALSE;
1116 }
1117
1118 /* get a random filename */
1119 for (guint i = 0; i < 6; i++)
1120 tmpname[i] = (gchar) g_random_int_range ('A', 'Z');
1121 filename = g_build_filename (dirname, tmpname, NULL);
1122
1123 /* just copy to the temp file */
Richard Hughes23135eb2017-11-30 21:01:25 +00001124 fu_device_set_status (device, FWUPD_STATUS_SCHEDULING);
Richard Hughescff38bc2016-12-12 12:03:37 +00001125 if (!g_file_set_contents (filename,
1126 g_bytes_get_data (blob_cab, NULL),
1127 (gssize) g_bytes_get_size (blob_cab),
1128 error))
1129 return FALSE;
1130
1131 /* schedule for next boot */
1132 g_debug ("schedule %s to be installed to %s on next boot",
1133 filename, fu_device_get_id (device));
Richard Hughes38fb56c2018-02-01 22:07:52 +00001134 release = fu_device_get_release_default (device);
1135 fwupd_release_set_version (release_tmp, fwupd_release_get_version (release));
1136 fwupd_release_set_filename (release_tmp, filename);
Richard Hughescff38bc2016-12-12 12:03:37 +00001137
1138 /* add to database */
Richard Hughes3e90a582018-01-06 22:38:09 +00001139 fu_device_set_update_state (device, FWUPD_UPDATE_STATE_PENDING);
Richard Hughes38fb56c2018-02-01 22:07:52 +00001140 if (!fu_history_add_device (history, device, release_tmp, error))
Richard Hughescff38bc2016-12-12 12:03:37 +00001141 return FALSE;
1142
1143 /* next boot we run offline */
1144 return fu_plugin_runner_offline_setup (error);
1145}
1146
1147gboolean
1148fu_plugin_runner_verify (FuPlugin *plugin,
1149 FuDevice *device,
1150 FuPluginVerifyFlags flags,
1151 GError **error)
1152{
1153 FuPluginPrivate *priv = GET_PRIVATE (plugin);
Richard Hughes7b8b2022016-12-12 16:15:03 +00001154 FuPluginVerifyFunc func = NULL;
Richard Hughesababbb72017-06-15 20:18:36 +01001155 GPtrArray *checksums;
Richard Hughescff38bc2016-12-12 12:03:37 +00001156
1157 /* not enabled */
1158 if (!priv->enabled)
1159 return TRUE;
1160
Richard Hughes639da472018-01-06 22:35:04 +00001161 /* no object loaded */
1162 if (priv->module == NULL)
1163 return TRUE;
1164
Richard Hughesababbb72017-06-15 20:18:36 +01001165 /* clear any existing verification checksums */
1166 checksums = fu_device_get_checksums (device);
1167 g_ptr_array_set_size (checksums, 0);
1168
Richard Hughescff38bc2016-12-12 12:03:37 +00001169 /* optional */
1170 g_module_symbol (priv->module, "fu_plugin_verify", (gpointer *) &func);
1171 if (func == NULL)
1172 return TRUE;
1173 g_debug ("performing verify() on %s", priv->name);
1174 if (!func (plugin, device, flags, error)) {
1175 g_prefix_error (error, "failed to verify %s: ", priv->name);
Richard Hughesd0905142016-03-13 09:46:49 +00001176 return FALSE;
1177 }
1178 return TRUE;
1179}
1180
Richard Hughesd0905142016-03-13 09:46:49 +00001181gboolean
Richard Hughescff38bc2016-12-12 12:03:37 +00001182fu_plugin_runner_unlock (FuPlugin *plugin, FuDevice *device, GError **error)
Richard Hughesd0905142016-03-13 09:46:49 +00001183{
Richard Hughescff38bc2016-12-12 12:03:37 +00001184 guint64 flags;
Richard Hughescff38bc2016-12-12 12:03:37 +00001185
1186 /* final check */
1187 flags = fu_device_get_flags (device);
1188 if ((flags & FWUPD_DEVICE_FLAG_LOCKED) == 0) {
1189 g_set_error (error,
1190 FWUPD_ERROR,
1191 FWUPD_ERROR_NOT_SUPPORTED,
1192 "Device %s is not locked",
1193 fu_device_get_id (device));
1194 return FALSE;
1195 }
1196
Richard Hughes9c4b5312017-11-14 11:34:53 +00001197 /* run vfunc */
1198 if (!fu_plugin_runner_device_generic (plugin, device,
1199 "fu_plugin_unlock", error))
1200 return FALSE;
Richard Hughescff38bc2016-12-12 12:03:37 +00001201
1202 /* update with correct flags */
1203 flags = fu_device_get_flags (device);
1204 fu_device_set_flags (device, flags &= ~FWUPD_DEVICE_FLAG_LOCKED);
1205 fu_device_set_modified (device, (guint64) g_get_real_time () / G_USEC_PER_SEC);
1206 return TRUE;
1207}
1208
1209gboolean
1210fu_plugin_runner_update (FuPlugin *plugin,
Richard Hughesa785a1c2017-08-25 16:00:58 +01001211 FuDevice *device,
1212 GBytes *blob_cab,
1213 GBytes *blob_fw,
1214 FwupdInstallFlags flags,
1215 GError **error)
Richard Hughescff38bc2016-12-12 12:03:37 +00001216{
1217 FuPluginPrivate *priv = GET_PRIVATE (plugin);
Richard Hughesa785a1c2017-08-25 16:00:58 +01001218 FuPluginUpdateFunc update_func;
Richard Hughes780ef3f2018-01-12 16:20:31 +00001219 g_autoptr(FuHistory) history = NULL;
Richard Hughes68982c62017-09-13 15:40:14 +01001220 g_autoptr(FuDevice) device_pending = NULL;
Richard Hughescff38bc2016-12-12 12:03:37 +00001221 GError *error_update = NULL;
Richard Hughesf556d372017-06-15 19:49:18 +01001222 GPtrArray *checksums;
Richard Hughescff38bc2016-12-12 12:03:37 +00001223
1224 /* not enabled */
Richard Hughes41c15482018-02-01 22:07:21 +00001225 if (!priv->enabled) {
1226 g_debug ("plugin not enabled, skipping");
Richard Hughesd0905142016-03-13 09:46:49 +00001227 return TRUE;
Richard Hughes41c15482018-02-01 22:07:21 +00001228 }
Richard Hughesd0905142016-03-13 09:46:49 +00001229
Richard Hughes639da472018-01-06 22:35:04 +00001230 /* no object loaded */
Richard Hughes41c15482018-02-01 22:07:21 +00001231 if (priv->module == NULL) {
1232 g_debug ("module not enabled, skipping");
Richard Hughes639da472018-01-06 22:35:04 +00001233 return TRUE;
Richard Hughes41c15482018-02-01 22:07:21 +00001234 }
Richard Hughes639da472018-01-06 22:35:04 +00001235
Richard Hughesd0905142016-03-13 09:46:49 +00001236 /* optional */
Richard Hughesa785a1c2017-08-25 16:00:58 +01001237 g_module_symbol (priv->module, "fu_plugin_update", (gpointer *) &update_func);
1238 if (update_func == NULL) {
1239 g_set_error_literal (error,
1240 FWUPD_ERROR,
1241 FWUPD_ERROR_NOT_SUPPORTED,
1242 "No update possible");
1243 return FALSE;
1244 }
Richard Hughesd0905142016-03-13 09:46:49 +00001245
Richard Hughesa785a1c2017-08-25 16:00:58 +01001246 /* just schedule this for the next reboot */
Richard Hughescff38bc2016-12-12 12:03:37 +00001247 if (flags & FWUPD_INSTALL_FLAG_OFFLINE) {
Richard Hughesa785a1c2017-08-25 16:00:58 +01001248 return fu_plugin_runner_schedule_update (plugin,
1249 device,
1250 blob_cab,
1251 error);
Richard Hughescff38bc2016-12-12 12:03:37 +00001252 }
1253
1254 /* cancel the pending action */
1255 if (!fu_plugin_runner_offline_invalidate (error))
1256 return FALSE;
1257
1258 /* online */
Richard Hughes780ef3f2018-01-12 16:20:31 +00001259 history = fu_history_new ();
Richard Hughes0b9d9962018-01-12 16:31:28 +00001260 device_pending = fu_history_get_device_by_id (history, fu_device_get_id (device), NULL);
Richard Hughesa785a1c2017-08-25 16:00:58 +01001261 if (!update_func (plugin, device, blob_fw, flags, &error_update)) {
Richard Hughesc0cd0232018-01-31 15:02:00 +00001262 fu_device_set_update_error (device, error_update->message);
Richard Hughescff38bc2016-12-12 12:03:37 +00001263 g_propagate_error (error, error_update);
1264 return FALSE;
1265 }
1266
Richard Hughesf556d372017-06-15 19:49:18 +01001267 /* no longer valid */
1268 checksums = fu_device_get_checksums (device);
1269 g_ptr_array_set_size (checksums, 0);
1270
Richard Hughescff38bc2016-12-12 12:03:37 +00001271 /* cleanup */
Richard Hughes68982c62017-09-13 15:40:14 +01001272 if (device_pending != NULL) {
Richard Hughescff38bc2016-12-12 12:03:37 +00001273 const gchar *tmp;
Richard Hughesbc3a4e12018-01-06 22:41:47 +00001274 FwupdRelease *release;
Richard Hughescff38bc2016-12-12 12:03:37 +00001275
Richard Hughes780ef3f2018-01-12 16:20:31 +00001276 /* update history database */
Richard Hughesc0cd0232018-01-31 15:02:00 +00001277 fu_device_set_update_state (device, FWUPD_UPDATE_STATE_SUCCESS);
1278 if (!fu_history_modify_device (history, device,
1279 FU_HISTORY_FLAGS_MATCH_NEW_VERSION,
1280 error))
Richard Hughes0b9d9962018-01-12 16:31:28 +00001281 return FALSE;
Richard Hughescff38bc2016-12-12 12:03:37 +00001282
1283 /* delete cab file */
Richard Hughesbc3a4e12018-01-06 22:41:47 +00001284 release = fu_device_get_release_default (device_pending);
1285 tmp = fwupd_release_get_filename (release);
Richard Hughescff38bc2016-12-12 12:03:37 +00001286 if (tmp != NULL && g_str_has_prefix (tmp, LIBEXECDIR)) {
1287 g_autoptr(GError) error_local = NULL;
1288 g_autoptr(GFile) file = NULL;
1289 file = g_file_new_for_path (tmp);
1290 if (!g_file_delete (file, NULL, &error_local)) {
1291 g_set_error (error,
1292 FWUPD_ERROR,
1293 FWUPD_ERROR_INVALID_FILE,
1294 "Failed to delete %s: %s",
1295 tmp, error_local->message);
1296 return FALSE;
1297 }
1298 }
1299 }
Richard Hughesd0905142016-03-13 09:46:49 +00001300 return TRUE;
1301}
Richard Hughescff38bc2016-12-12 12:03:37 +00001302
1303gboolean
1304fu_plugin_runner_clear_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 */
1318 g_module_symbol (priv->module, "fu_plugin_get_results", (gpointer *) &func);
1319 if (func == NULL)
Richard Hughescff38bc2016-12-12 12:03:37 +00001320 return TRUE;
Richard Hughes65e44ca2018-01-30 17:26:30 +00001321 g_debug ("performing clear_result() on %s", priv->name);
1322 if (!func (plugin, device, error)) {
1323 g_prefix_error (error, "failed to clear_result %s: ", priv->name);
Richard Hughescff38bc2016-12-12 12:03:37 +00001324 return FALSE;
1325 }
Richard Hughes65e44ca2018-01-30 17:26:30 +00001326 return TRUE;
Richard Hughescff38bc2016-12-12 12:03:37 +00001327}
1328
1329gboolean
1330fu_plugin_runner_get_results (FuPlugin *plugin, FuDevice *device, GError **error)
1331{
1332 FuPluginPrivate *priv = GET_PRIVATE (plugin);
Richard Hughes7b8b2022016-12-12 16:15:03 +00001333 FuPluginDeviceFunc func = NULL;
Richard Hughescff38bc2016-12-12 12:03:37 +00001334
1335 /* not enabled */
1336 if (!priv->enabled)
1337 return TRUE;
1338
Richard Hughes639da472018-01-06 22:35:04 +00001339 /* no object loaded */
1340 if (priv->module == NULL)
1341 return TRUE;
1342
Richard Hughes65e44ca2018-01-30 17:26:30 +00001343 /* optional */
Richard Hughescff38bc2016-12-12 12:03:37 +00001344 g_module_symbol (priv->module, "fu_plugin_get_results", (gpointer *) &func);
Richard Hughes65e44ca2018-01-30 17:26:30 +00001345 if (func == NULL)
Richard Hughescff38bc2016-12-12 12:03:37 +00001346 return TRUE;
Richard Hughes65e44ca2018-01-30 17:26:30 +00001347 g_debug ("performing get_results() on %s", priv->name);
1348 if (!func (plugin, device, error)) {
1349 g_prefix_error (error, "failed to get_results %s: ", priv->name);
Richard Hughescff38bc2016-12-12 12:03:37 +00001350 return FALSE;
1351 }
Richard Hughescff38bc2016-12-12 12:03:37 +00001352 return TRUE;
1353}
1354
Richard Hughes08a37992017-09-12 12:57:43 +01001355/**
1356 * fu_plugin_get_order:
1357 * @plugin: a #FuPlugin
1358 *
1359 * Gets the plugin order, where higher numbers are run after lower
1360 * numbers.
1361 *
1362 * Returns: the integer value
1363 **/
1364guint
1365fu_plugin_get_order (FuPlugin *plugin)
1366{
1367 FuPluginPrivate *priv = fu_plugin_get_instance_private (plugin);
1368 return priv->order;
1369}
1370
1371/**
1372 * fu_plugin_set_order:
1373 * @plugin: a #FuPlugin
1374 * @order: a integer value
1375 *
1376 * Sets the plugin order, where higher numbers are run after lower
1377 * numbers.
1378 **/
1379void
1380fu_plugin_set_order (FuPlugin *plugin, guint order)
1381{
1382 FuPluginPrivate *priv = fu_plugin_get_instance_private (plugin);
1383 priv->order = order;
1384}
1385
1386/**
1387 * fu_plugin_add_rule:
1388 * @plugin: a #FuPlugin
1389 * @rule: a #FuPluginRule, e.g. %FU_PLUGIN_RULE_CONFLICTS
Richard Hughes4eada342017-10-03 21:20:32 +01001390 * @name: a plugin name, e.g. `upower`
Richard Hughes08a37992017-09-12 12:57:43 +01001391 *
1392 * If the plugin name is found, the rule will be used to sort the plugin list,
1393 * for example the plugin specified by @name will be ordered after this plugin
1394 * when %FU_PLUGIN_RULE_RUN_AFTER is used.
1395 *
1396 * NOTE: The depsolver is iterative and may not solve overly-complicated rules;
1397 * If depsolving fails then fwupd will not start.
1398 **/
1399void
1400fu_plugin_add_rule (FuPlugin *plugin, FuPluginRule rule, const gchar *name)
1401{
1402 FuPluginPrivate *priv = fu_plugin_get_instance_private (plugin);
1403 g_ptr_array_add (priv->rules[rule], g_strdup (name));
1404}
1405
1406/**
1407 * fu_plugin_get_rules:
1408 * @plugin: a #FuPlugin
1409 * @rule: a #FuPluginRule, e.g. %FU_PLUGIN_RULE_CONFLICTS
1410 *
1411 * Gets the plugin IDs that should be run after this plugin.
1412 *
1413 * Returns: (element-type utf8) (transfer none): the list of plugin names, e.g. ['appstream']
1414 **/
1415GPtrArray *
1416fu_plugin_get_rules (FuPlugin *plugin, FuPluginRule rule)
1417{
1418 FuPluginPrivate *priv = fu_plugin_get_instance_private (plugin);
1419 return priv->rules[rule];
1420}
1421
Richard Hughes80b79bb2018-01-11 21:11:06 +00001422/**
1423 * fu_plugin_add_report_metadata:
1424 * @plugin: a #FuPlugin
1425 * @key: a string, e.g. `FwupdateVersion`
1426 * @value: a string, e.g. `10`
1427 *
1428 * Sets any additional metadata to be included in the firmware report to aid
1429 * debugging problems.
1430 *
1431 * Any data included here will be sent to the metadata server after user
1432 * confirmation.
1433 **/
1434void
1435fu_plugin_add_report_metadata (FuPlugin *plugin, const gchar *key, const gchar *value)
1436{
1437 FuPluginPrivate *priv = fu_plugin_get_instance_private (plugin);
1438 g_hash_table_insert (priv->report_metadata, g_strdup (key), g_strdup (value));
1439}
1440
1441/**
1442 * fu_plugin_get_report_metadata:
1443 * @plugin: a #FuPlugin
1444 *
1445 * Returns the list of additional metadata to be added when filing a report.
1446 *
1447 * Returns: (transfer none): the map of report metadata
1448 **/
1449GHashTable *
1450fu_plugin_get_report_metadata (FuPlugin *plugin)
1451{
1452 FuPluginPrivate *priv = fu_plugin_get_instance_private (plugin);
1453 return priv->report_metadata;
1454}
1455
Mario Limonciello963dc422018-02-27 14:26:58 -06001456/**
1457 * fu_plugin_get_config_value:
1458 * @plugin: a #FuPlugin
1459 * @key: A settings key
1460 *
1461 * Return the value of a key if it's been configured
1462 *
1463 * Since: 1.0.6
1464 **/
1465gchar *
1466fu_plugin_get_config_value (FuPlugin *plugin, const gchar *key)
1467{
1468 g_autofree gchar *conf_file = NULL;
1469 g_autofree gchar *conf_path = NULL;
1470 g_autoptr(GKeyFile) keyfile = NULL;
1471 const gchar *plugin_name;
1472
1473 plugin_name = fu_plugin_get_name (plugin);
1474 conf_file = g_strdup_printf ("%s.conf", plugin_name);
1475 conf_path = g_build_filename (FWUPDCONFIGDIR, conf_file, NULL);
1476 if (!g_file_test (conf_path, G_FILE_TEST_IS_REGULAR))
1477 return NULL;
1478 keyfile = g_key_file_new ();
1479 if (!g_key_file_load_from_file (keyfile, conf_path,
1480 G_KEY_FILE_NONE, NULL))
1481 return NULL;
1482 return g_key_file_get_string (keyfile, plugin_name, key, NULL);
1483}
1484
Richard Hughescff38bc2016-12-12 12:03:37 +00001485static void
1486fu_plugin_class_init (FuPluginClass *klass)
1487{
1488 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1489 object_class->finalize = fu_plugin_finalize;
1490 signals[SIGNAL_DEVICE_ADDED] =
1491 g_signal_new ("device-added",
1492 G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST,
1493 G_STRUCT_OFFSET (FuPluginClass, device_added),
1494 NULL, NULL, g_cclosure_marshal_VOID__OBJECT,
1495 G_TYPE_NONE, 1, FU_TYPE_DEVICE);
1496 signals[SIGNAL_DEVICE_REMOVED] =
1497 g_signal_new ("device-removed",
1498 G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST,
1499 G_STRUCT_OFFSET (FuPluginClass, device_removed),
1500 NULL, NULL, g_cclosure_marshal_VOID__OBJECT,
1501 G_TYPE_NONE, 1, FU_TYPE_DEVICE);
Richard Hughese1fd34d2017-08-24 14:19:51 +01001502 signals[SIGNAL_DEVICE_REGISTER] =
1503 g_signal_new ("device-register",
1504 G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST,
1505 G_STRUCT_OFFSET (FuPluginClass, device_register),
1506 NULL, NULL, g_cclosure_marshal_VOID__OBJECT,
1507 G_TYPE_NONE, 1, FU_TYPE_DEVICE);
Richard Hughes362d6d72017-01-07 21:42:14 +00001508 signals[SIGNAL_RECOLDPLUG] =
1509 g_signal_new ("recoldplug",
1510 G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST,
1511 G_STRUCT_OFFSET (FuPluginClass, recoldplug),
1512 NULL, NULL, g_cclosure_marshal_VOID__VOID,
1513 G_TYPE_NONE, 0);
Richard Hughesb0829032017-01-10 09:27:08 +00001514 signals[SIGNAL_SET_COLDPLUG_DELAY] =
1515 g_signal_new ("set-coldplug-delay",
1516 G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST,
1517 G_STRUCT_OFFSET (FuPluginClass, set_coldplug_delay),
1518 NULL, NULL, g_cclosure_marshal_VOID__UINT,
1519 G_TYPE_NONE, 1, G_TYPE_UINT);
Richard Hughescff38bc2016-12-12 12:03:37 +00001520}
1521
1522static void
1523fu_plugin_init (FuPlugin *plugin)
1524{
1525 FuPluginPrivate *priv = GET_PRIVATE (plugin);
1526 priv->enabled = TRUE;
1527 priv->devices = g_hash_table_new_full (g_str_hash, g_str_equal,
1528 g_free, (GDestroyNotify) g_object_unref);
Richard Hughese8b5db62017-07-17 14:18:22 +01001529 priv->devices_delay = g_hash_table_new (g_direct_hash, g_direct_equal);
Richard Hughes80b79bb2018-01-11 21:11:06 +00001530 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 +01001531 for (guint i = 0; i < FU_PLUGIN_RULE_LAST; i++)
1532 priv->rules[i] = g_ptr_array_new_with_free_func (g_free);
Richard Hughescff38bc2016-12-12 12:03:37 +00001533}
1534
1535static void
1536fu_plugin_finalize (GObject *object)
1537{
1538 FuPlugin *plugin = FU_PLUGIN (object);
1539 FuPluginPrivate *priv = GET_PRIVATE (plugin);
1540 FuPluginInitFunc func = NULL;
1541
1542 /* optional */
1543 if (priv->module != NULL) {
1544 g_module_symbol (priv->module, "fu_plugin_destroy", (gpointer *) &func);
1545 if (func != NULL) {
1546 g_debug ("performing destroy() on %s", priv->name);
1547 func (plugin);
1548 }
1549 }
1550
Richard Hughes08a37992017-09-12 12:57:43 +01001551 for (guint i = 0; i < FU_PLUGIN_RULE_LAST; i++)
1552 g_ptr_array_unref (priv->rules[i]);
1553
Richard Hughescff38bc2016-12-12 12:03:37 +00001554 if (priv->usb_ctx != NULL)
1555 g_object_unref (priv->usb_ctx);
Richard Hughesb8f8db22017-04-25 15:56:00 +01001556 if (priv->hwids != NULL)
Richard Hughesd7704d42017-08-08 20:29:09 +01001557 g_object_unref (priv->hwids);
Richard Hughes9c028f02017-10-28 21:14:28 +01001558 if (priv->quirks != NULL)
1559 g_object_unref (priv->quirks);
Richard Hughes1354ea92017-09-19 15:58:31 +01001560 if (priv->supported_guids != NULL)
1561 g_ptr_array_unref (priv->supported_guids);
Richard Hughes49e5e052017-09-03 12:15:41 +01001562 if (priv->smbios != NULL)
1563 g_object_unref (priv->smbios);
Richard Hughes275d3b42018-04-20 16:40:37 +01001564 if (priv->runtime_versions != NULL)
1565 g_hash_table_unref (priv->runtime_versions);
Richard Hughes576c0122017-02-24 09:47:00 +00001566#ifndef RUNNING_ON_VALGRIND
Richard Hughescff38bc2016-12-12 12:03:37 +00001567 if (priv->module != NULL)
1568 g_module_close (priv->module);
Richard Hughes576c0122017-02-24 09:47:00 +00001569#endif
Richard Hughescff38bc2016-12-12 12:03:37 +00001570 g_hash_table_unref (priv->devices);
Richard Hughesae3d65f2016-12-16 09:38:01 +00001571 g_hash_table_unref (priv->devices_delay);
Richard Hughes80b79bb2018-01-11 21:11:06 +00001572 g_hash_table_unref (priv->report_metadata);
Richard Hughescff38bc2016-12-12 12:03:37 +00001573 g_free (priv->name);
1574 g_free (priv->data);
1575
1576 G_OBJECT_CLASS (fu_plugin_parent_class)->finalize (object);
1577}
1578
1579FuPlugin *
1580fu_plugin_new (void)
1581{
1582 FuPlugin *plugin;
1583 plugin = g_object_new (FU_TYPE_PLUGIN, NULL);
1584 return plugin;
1585}