blob: 20c43b3258da0f3ac75d7c00f0a8963ffd3ab73e [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);
687 g_hash_table_insert (priv->runtime_versions,
688 g_strdup (component_id),
689 g_strdup (version));
690}
691
Richard Hughes9c028f02017-10-28 21:14:28 +0100692/**
693 * fu_plugin_lookup_quirk_by_id:
694 * @plugin: A #FuPlugin
695 * @prefix: A string prefix that matches the quirks file basename, e.g. "dfu-quirks"
696 * @id: An ID to match the entry, e.g. "012345"
697 *
698 * Looks up an entry in the hardware database using a string value.
699 *
700 * Returns: (transfer none): values from the database, or %NULL if not found
701 *
702 * Since: 1.0.1
703 **/
704const gchar *
705fu_plugin_lookup_quirk_by_id (FuPlugin *plugin, const gchar *prefix, const gchar *id)
706{
707 FuPluginPrivate *priv = GET_PRIVATE (plugin);
708 g_return_val_if_fail (FU_IS_PLUGIN (plugin), NULL);
709
710 /* wildcard */
711 if (g_strstr_len (id, -1, "*") != NULL)
712 return fu_quirks_lookup_by_glob (priv->quirks, prefix, id);
713
714 /* exact ID */
715 return fu_quirks_lookup_by_id (priv->quirks, prefix, id);
716}
717
718/**
719 * fu_plugin_lookup_quirk_by_usb_device:
720 * @plugin: A #FuPlugin
721 * @prefix: A string prefix that matches the quirks file basename, e.g. "dfu-quirks"
722 * @dev: A #GUsbDevice
723 *
724 * Looks up an entry in the hardware database using various keys generated
725 * from @dev.
726 *
727 * Returns: (transfer none): values from the database, or %NULL if not found
728 *
729 * Since: 1.0.1
730 **/
731const gchar *
732fu_plugin_lookup_quirk_by_usb_device (FuPlugin *plugin, const gchar *prefix, GUsbDevice *dev)
733{
734 FuPluginPrivate *priv = GET_PRIVATE (plugin);
735 g_return_val_if_fail (FU_IS_PLUGIN (plugin), NULL);
736 return fu_quirks_lookup_by_usb_device (priv->quirks, prefix, dev);
737}
738
Richard Hughes1354ea92017-09-19 15:58:31 +0100739/**
740 * fu_plugin_get_supported:
741 * @plugin: A #FuPlugin
742 *
743 * Gets all the device GUIDs supported by the daemon.
744 *
Richard Hughes4eada342017-10-03 21:20:32 +0100745 * Returns: (element-type utf8) (transfer none): GUIDs
746 *
Richard Hughes1354ea92017-09-19 15:58:31 +0100747 * Since: 1.0.0
748 **/
749GPtrArray *
750fu_plugin_get_supported (FuPlugin *plugin)
751{
752 FuPluginPrivate *priv = GET_PRIVATE (plugin);
753 return priv->supported_guids;
754}
755
756void
Richard Hughes49e5e052017-09-03 12:15:41 +0100757fu_plugin_set_smbios (FuPlugin *plugin, FuSmbios *smbios)
758{
759 FuPluginPrivate *priv = GET_PRIVATE (plugin);
760 g_set_object (&priv->smbios, smbios);
761}
762
Richard Hughesb8f8db22017-04-25 15:56:00 +0100763/**
Richard Hughesb0829032017-01-10 09:27:08 +0000764 * fu_plugin_set_coldplug_delay:
765 * @plugin: A #FuPlugin
766 * @duration: A delay in milliseconds
767 *
768 * Set the minimum time that should be waited inbetween the call to
769 * fu_plugin_coldplug_prepare() and fu_plugin_coldplug(). This is usually going
770 * to be the minimum hardware initialisation time from a datasheet.
771 *
772 * It is better to use this function rather than using a sleep() in the plugin
773 * itself as then only one delay is done in the daemon rather than waiting for
774 * each coldplug prepare in a serial way.
775 *
776 * Additionally, very long delays should be avoided as the daemon will be
777 * blocked from processing requests whilst the coldplug delay is being
778 * performed.
779 *
780 * Since: 0.8.0
781 **/
782void
783fu_plugin_set_coldplug_delay (FuPlugin *plugin, guint duration)
784{
785 g_return_if_fail (FU_IS_PLUGIN (plugin));
786 g_return_if_fail (duration > 0);
787
788 /* check sanity */
789 if (duration > FU_PLUGIN_COLDPLUG_DELAY_MAXIMUM) {
790 g_warning ("duration of %ums is crazy, truncating to %ums",
791 duration,
792 FU_PLUGIN_COLDPLUG_DELAY_MAXIMUM);
793 duration = FU_PLUGIN_COLDPLUG_DELAY_MAXIMUM;
794 }
795
796 /* emit */
797 g_signal_emit (plugin, signals[SIGNAL_SET_COLDPLUG_DELAY], 0, duration);
798}
799
Richard Hughesd0905142016-03-13 09:46:49 +0000800gboolean
Richard Hughescff38bc2016-12-12 12:03:37 +0000801fu_plugin_runner_startup (FuPlugin *plugin, GError **error)
Richard Hughesd0905142016-03-13 09:46:49 +0000802{
Richard Hughescff38bc2016-12-12 12:03:37 +0000803 FuPluginPrivate *priv = GET_PRIVATE (plugin);
Richard Hughes7b8b2022016-12-12 16:15:03 +0000804 FuPluginStartupFunc func = NULL;
Richard Hughesd0905142016-03-13 09:46:49 +0000805
806 /* not enabled */
Richard Hughescff38bc2016-12-12 12:03:37 +0000807 if (!priv->enabled)
Richard Hughesd0905142016-03-13 09:46:49 +0000808 return TRUE;
809
Richard Hughes639da472018-01-06 22:35:04 +0000810 /* no object loaded */
811 if (priv->module == NULL)
812 return TRUE;
813
Richard Hughesd0905142016-03-13 09:46:49 +0000814 /* optional */
Richard Hughescff38bc2016-12-12 12:03:37 +0000815 g_module_symbol (priv->module, "fu_plugin_startup", (gpointer *) &func);
816 if (func == NULL)
Richard Hughesd0905142016-03-13 09:46:49 +0000817 return TRUE;
Richard Hughescff38bc2016-12-12 12:03:37 +0000818 g_debug ("performing startup() on %s", priv->name);
Richard Hughesd0905142016-03-13 09:46:49 +0000819 if (!func (plugin, error)) {
Richard Hughescff38bc2016-12-12 12:03:37 +0000820 g_prefix_error (error, "failed to startup %s: ", priv->name);
821 return FALSE;
822 }
823 return TRUE;
824}
825
826static gboolean
827fu_plugin_runner_offline_invalidate (GError **error)
828{
829 g_autoptr(GError) error_local = NULL;
830 g_autoptr(GFile) file1 = NULL;
831
832 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
833
834 file1 = g_file_new_for_path (FU_OFFLINE_TRIGGER_FILENAME);
835 if (!g_file_query_exists (file1, NULL))
836 return TRUE;
837 if (!g_file_delete (file1, NULL, &error_local)) {
838 g_set_error (error,
839 FWUPD_ERROR,
840 FWUPD_ERROR_INTERNAL,
841 "Cannot delete %s: %s",
842 FU_OFFLINE_TRIGGER_FILENAME,
843 error_local->message);
844 return FALSE;
845 }
846 return TRUE;
847}
848
849static gboolean
850fu_plugin_runner_offline_setup (GError **error)
851{
852 gint rc;
853
854 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
855
856 /* create symlink for the systemd-system-update-generator */
857 rc = symlink ("/var/lib/fwupd", FU_OFFLINE_TRIGGER_FILENAME);
858 if (rc < 0) {
859 g_set_error (error,
860 FWUPD_ERROR,
861 FWUPD_ERROR_INTERNAL,
862 "Failed to create symlink %s to %s: %s",
863 FU_OFFLINE_TRIGGER_FILENAME,
864 "/var/lib", strerror (errno));
Richard Hughesd0905142016-03-13 09:46:49 +0000865 return FALSE;
866 }
867 return TRUE;
868}
869
Richard Hughes0d7fdb32017-11-11 20:23:14 +0000870static gboolean
871fu_plugin_runner_device_generic (FuPlugin *plugin, FuDevice *device,
872 const gchar *symbol_name, GError **error)
873{
874 FuPluginPrivate *priv = GET_PRIVATE (plugin);
875 FuPluginDeviceFunc func = NULL;
876
877 /* not enabled */
878 if (!priv->enabled)
879 return TRUE;
880
Richard Hughesd3d96cc2017-11-14 11:34:33 +0000881 /* no object loaded */
882 if (priv->module == NULL)
883 return TRUE;
884
Richard Hughes0d7fdb32017-11-11 20:23:14 +0000885 /* optional */
886 g_module_symbol (priv->module, symbol_name, (gpointer *) &func);
887 if (func == NULL)
888 return TRUE;
889 g_debug ("performing %s() on %s", symbol_name + 10, priv->name);
890 if (!func (plugin, device, error)) {
Richard Hughes83e54e42018-01-31 23:27:30 +0000891 g_prefix_error (error, "failed to run %s() on %s: ",
Richard Hughes0d7fdb32017-11-11 20:23:14 +0000892 symbol_name + 10,
Richard Hughes0d7fdb32017-11-11 20:23:14 +0000893 priv->name);
894 return FALSE;
895 }
896 return TRUE;
897}
898
Richard Hughesd0905142016-03-13 09:46:49 +0000899gboolean
Richard Hughescff38bc2016-12-12 12:03:37 +0000900fu_plugin_runner_coldplug (FuPlugin *plugin, GError **error)
Richard Hughesd0905142016-03-13 09:46:49 +0000901{
Richard Hughescff38bc2016-12-12 12:03:37 +0000902 FuPluginPrivate *priv = GET_PRIVATE (plugin);
Richard Hughes7b8b2022016-12-12 16:15:03 +0000903 FuPluginStartupFunc func = NULL;
Richard Hughesd0905142016-03-13 09:46:49 +0000904
905 /* not enabled */
Richard Hughescff38bc2016-12-12 12:03:37 +0000906 if (!priv->enabled)
Richard Hughesd0905142016-03-13 09:46:49 +0000907 return TRUE;
908
Richard Hughes639da472018-01-06 22:35:04 +0000909 /* no object loaded */
910 if (priv->module == NULL)
911 return TRUE;
912
Richard Hughesd0905142016-03-13 09:46:49 +0000913 /* optional */
Richard Hughescff38bc2016-12-12 12:03:37 +0000914 g_module_symbol (priv->module, "fu_plugin_coldplug", (gpointer *) &func);
915 if (func == NULL)
Richard Hughesd0905142016-03-13 09:46:49 +0000916 return TRUE;
Richard Hughescff38bc2016-12-12 12:03:37 +0000917 g_debug ("performing coldplug() on %s", priv->name);
918 if (!func (plugin, error)) {
919 g_prefix_error (error, "failed to coldplug %s: ", priv->name);
920 return FALSE;
921 }
922 return TRUE;
923}
924
Richard Hughes7b8b2022016-12-12 16:15:03 +0000925gboolean
Richard Hughes2de8f132018-01-17 09:12:02 +0000926fu_plugin_runner_recoldplug (FuPlugin *plugin, GError **error)
927{
928 FuPluginPrivate *priv = GET_PRIVATE (plugin);
929 FuPluginStartupFunc func = NULL;
930
931 /* not enabled */
932 if (!priv->enabled)
933 return TRUE;
934
935 /* no object loaded */
936 if (priv->module == NULL)
937 return TRUE;
938
939 /* optional */
940 g_module_symbol (priv->module, "fu_plugin_recoldplug", (gpointer *) &func);
941 if (func == NULL)
942 return TRUE;
943 g_debug ("performing recoldplug() on %s", priv->name);
944 if (!func (plugin, error)) {
945 g_prefix_error (error, "failed to recoldplug %s: ", priv->name);
946 return FALSE;
947 }
948 return TRUE;
949}
950
951gboolean
Richard Hughes46487c92017-01-07 21:26:34 +0000952fu_plugin_runner_coldplug_prepare (FuPlugin *plugin, GError **error)
953{
954 FuPluginPrivate *priv = GET_PRIVATE (plugin);
955 FuPluginStartupFunc func = NULL;
956
957 /* not enabled */
958 if (!priv->enabled)
959 return TRUE;
960
Richard Hughes639da472018-01-06 22:35:04 +0000961 /* no object loaded */
962 if (priv->module == NULL)
963 return TRUE;
964
Richard Hughes46487c92017-01-07 21:26:34 +0000965 /* optional */
966 g_module_symbol (priv->module, "fu_plugin_coldplug_prepare", (gpointer *) &func);
967 if (func == NULL)
968 return TRUE;
969 g_debug ("performing coldplug_prepare() on %s", priv->name);
970 if (!func (plugin, error)) {
971 g_prefix_error (error, "failed to prepare for coldplug %s: ", priv->name);
972 return FALSE;
973 }
974 return TRUE;
975}
976
977gboolean
978fu_plugin_runner_coldplug_cleanup (FuPlugin *plugin, GError **error)
979{
980 FuPluginPrivate *priv = GET_PRIVATE (plugin);
981 FuPluginStartupFunc func = NULL;
982
983 /* not enabled */
984 if (!priv->enabled)
985 return TRUE;
986
Richard Hughes639da472018-01-06 22:35:04 +0000987 /* no object loaded */
988 if (priv->module == NULL)
989 return TRUE;
990
Richard Hughes46487c92017-01-07 21:26:34 +0000991 /* optional */
992 g_module_symbol (priv->module, "fu_plugin_coldplug_cleanup", (gpointer *) &func);
993 if (func == NULL)
994 return TRUE;
995 g_debug ("performing coldplug_cleanup() on %s", priv->name);
996 if (!func (plugin, error)) {
997 g_prefix_error (error, "failed to cleanup coldplug %s: ", priv->name);
998 return FALSE;
999 }
1000 return TRUE;
1001}
1002
1003gboolean
Richard Hughes7b8b2022016-12-12 16:15:03 +00001004fu_plugin_runner_update_prepare (FuPlugin *plugin, FuDevice *device, GError **error)
1005{
Richard Hughes0d7fdb32017-11-11 20:23:14 +00001006 return fu_plugin_runner_device_generic (plugin, device,
1007 "fu_plugin_update_prepare", error);
Richard Hughes7b8b2022016-12-12 16:15:03 +00001008}
1009
1010gboolean
1011fu_plugin_runner_update_cleanup (FuPlugin *plugin, FuDevice *device, GError **error)
1012{
Richard Hughes0d7fdb32017-11-11 20:23:14 +00001013 return fu_plugin_runner_device_generic (plugin, device,
1014 "fu_plugin_update_cleanup", error);
1015}
Richard Hughes7b8b2022016-12-12 16:15:03 +00001016
Richard Hughes0d7fdb32017-11-11 20:23:14 +00001017gboolean
1018fu_plugin_runner_update_attach (FuPlugin *plugin, FuDevice *device, GError **error)
1019{
1020 return fu_plugin_runner_device_generic (plugin, device,
1021 "fu_plugin_update_attach", error);
1022}
Richard Hughes7b8b2022016-12-12 16:15:03 +00001023
Richard Hughes0d7fdb32017-11-11 20:23:14 +00001024gboolean
1025fu_plugin_runner_update_detach (FuPlugin *plugin, FuDevice *device, GError **error)
1026{
1027 return fu_plugin_runner_device_generic (plugin, device,
1028 "fu_plugin_update_detach", error);
1029}
1030
1031gboolean
1032fu_plugin_runner_update_reload (FuPlugin *plugin, FuDevice *device, GError **error)
1033{
1034 return fu_plugin_runner_device_generic (plugin, device,
1035 "fu_plugin_update_reload", error);
Richard Hughes7b8b2022016-12-12 16:15:03 +00001036}
1037
Richard Hughes104f6512017-11-24 11:44:57 +00001038gboolean
1039fu_plugin_runner_usb_device_added (FuPlugin *plugin, GUsbDevice *usb_device, GError **error)
1040{
1041 FuPluginPrivate *priv = GET_PRIVATE (plugin);
1042 FuPluginUsbDeviceAddedFunc func = NULL;
1043
1044 /* not enabled */
1045 if (!priv->enabled)
1046 return TRUE;
Richard Hughes639da472018-01-06 22:35:04 +00001047
1048 /* no object loaded */
Richard Hughes104f6512017-11-24 11:44:57 +00001049 if (priv->module == NULL)
1050 return TRUE;
1051
1052 /* optional */
1053 g_module_symbol (priv->module, "fu_plugin_usb_device_added", (gpointer *) &func);
1054 if (func != NULL) {
1055 g_debug ("performing usb_device_added() on %s", priv->name);
1056 return func (plugin, usb_device, error);
1057 }
1058 return TRUE;
1059}
1060
Richard Hughese1fd34d2017-08-24 14:19:51 +01001061void
1062fu_plugin_runner_device_register (FuPlugin *plugin, FuDevice *device)
1063{
1064 FuPluginPrivate *priv = GET_PRIVATE (plugin);
1065 FuPluginDeviceRegisterFunc func = NULL;
1066
1067 /* not enabled */
1068 if (!priv->enabled)
1069 return;
Richard Hughes34834102017-11-21 21:55:00 +00001070 if (priv->module == NULL)
1071 return;
Richard Hughese1fd34d2017-08-24 14:19:51 +01001072
1073 /* optional */
1074 g_module_symbol (priv->module, "fu_plugin_device_registered", (gpointer *) &func);
1075 if (func != NULL) {
1076 g_debug ("performing device_added() on %s", priv->name);
1077 func (plugin, device);
1078 }
1079}
1080
Richard Hughescff38bc2016-12-12 12:03:37 +00001081static gboolean
1082fu_plugin_runner_schedule_update (FuPlugin *plugin,
1083 FuDevice *device,
1084 GBytes *blob_cab,
1085 GError **error)
1086{
Richard Hughes38fb56c2018-02-01 22:07:52 +00001087 FwupdRelease *release;
Richard Hughescff38bc2016-12-12 12:03:37 +00001088 gchar tmpname[] = {"XXXXXX.cap"};
1089 g_autofree gchar *dirname = NULL;
1090 g_autofree gchar *filename = NULL;
Richard Hughes68982c62017-09-13 15:40:14 +01001091 g_autoptr(FuDevice) res_tmp = NULL;
Richard Hughes780ef3f2018-01-12 16:20:31 +00001092 g_autoptr(FuHistory) history = NULL;
Richard Hughes38fb56c2018-02-01 22:07:52 +00001093 g_autoptr(FwupdRelease) release_tmp = fwupd_release_new ();
Richard Hughescff38bc2016-12-12 12:03:37 +00001094 g_autoptr(GFile) file = NULL;
1095
1096 /* id already exists */
Richard Hughes780ef3f2018-01-12 16:20:31 +00001097 history = fu_history_new ();
Richard Hughes0b9d9962018-01-12 16:31:28 +00001098 res_tmp = fu_history_get_device_by_id (history, fu_device_get_id (device), NULL);
Richard Hughescff38bc2016-12-12 12:03:37 +00001099 if (res_tmp != NULL) {
1100 g_set_error (error,
1101 FWUPD_ERROR,
1102 FWUPD_ERROR_ALREADY_PENDING,
1103 "%s is already scheduled to be updated",
1104 fu_device_get_id (device));
1105 return FALSE;
1106 }
1107
1108 /* create directory */
1109 dirname = g_build_filename (LOCALSTATEDIR, "lib", "fwupd", NULL);
1110 file = g_file_new_for_path (dirname);
1111 if (!g_file_query_exists (file, NULL)) {
1112 if (!g_file_make_directory_with_parents (file, NULL, error))
1113 return FALSE;
1114 }
1115
1116 /* get a random filename */
1117 for (guint i = 0; i < 6; i++)
1118 tmpname[i] = (gchar) g_random_int_range ('A', 'Z');
1119 filename = g_build_filename (dirname, tmpname, NULL);
1120
1121 /* just copy to the temp file */
Richard Hughes23135eb2017-11-30 21:01:25 +00001122 fu_device_set_status (device, FWUPD_STATUS_SCHEDULING);
Richard Hughescff38bc2016-12-12 12:03:37 +00001123 if (!g_file_set_contents (filename,
1124 g_bytes_get_data (blob_cab, NULL),
1125 (gssize) g_bytes_get_size (blob_cab),
1126 error))
1127 return FALSE;
1128
1129 /* schedule for next boot */
1130 g_debug ("schedule %s to be installed to %s on next boot",
1131 filename, fu_device_get_id (device));
Richard Hughes38fb56c2018-02-01 22:07:52 +00001132 release = fu_device_get_release_default (device);
1133 fwupd_release_set_version (release_tmp, fwupd_release_get_version (release));
1134 fwupd_release_set_filename (release_tmp, filename);
Richard Hughescff38bc2016-12-12 12:03:37 +00001135
1136 /* add to database */
Richard Hughes3e90a582018-01-06 22:38:09 +00001137 fu_device_set_update_state (device, FWUPD_UPDATE_STATE_PENDING);
Richard Hughes38fb56c2018-02-01 22:07:52 +00001138 if (!fu_history_add_device (history, device, release_tmp, error))
Richard Hughescff38bc2016-12-12 12:03:37 +00001139 return FALSE;
1140
1141 /* next boot we run offline */
1142 return fu_plugin_runner_offline_setup (error);
1143}
1144
1145gboolean
1146fu_plugin_runner_verify (FuPlugin *plugin,
1147 FuDevice *device,
1148 FuPluginVerifyFlags flags,
1149 GError **error)
1150{
1151 FuPluginPrivate *priv = GET_PRIVATE (plugin);
Richard Hughes7b8b2022016-12-12 16:15:03 +00001152 FuPluginVerifyFunc func = NULL;
Richard Hughesababbb72017-06-15 20:18:36 +01001153 GPtrArray *checksums;
Richard Hughescff38bc2016-12-12 12:03:37 +00001154
1155 /* not enabled */
1156 if (!priv->enabled)
1157 return TRUE;
1158
Richard Hughes639da472018-01-06 22:35:04 +00001159 /* no object loaded */
1160 if (priv->module == NULL)
1161 return TRUE;
1162
Richard Hughesababbb72017-06-15 20:18:36 +01001163 /* clear any existing verification checksums */
1164 checksums = fu_device_get_checksums (device);
1165 g_ptr_array_set_size (checksums, 0);
1166
Richard Hughescff38bc2016-12-12 12:03:37 +00001167 /* optional */
1168 g_module_symbol (priv->module, "fu_plugin_verify", (gpointer *) &func);
1169 if (func == NULL)
1170 return TRUE;
1171 g_debug ("performing verify() on %s", priv->name);
1172 if (!func (plugin, device, flags, error)) {
1173 g_prefix_error (error, "failed to verify %s: ", priv->name);
Richard Hughesd0905142016-03-13 09:46:49 +00001174 return FALSE;
1175 }
1176 return TRUE;
1177}
1178
Richard Hughesd0905142016-03-13 09:46:49 +00001179gboolean
Richard Hughescff38bc2016-12-12 12:03:37 +00001180fu_plugin_runner_unlock (FuPlugin *plugin, FuDevice *device, GError **error)
Richard Hughesd0905142016-03-13 09:46:49 +00001181{
Richard Hughescff38bc2016-12-12 12:03:37 +00001182 guint64 flags;
Richard Hughescff38bc2016-12-12 12:03:37 +00001183
1184 /* final check */
1185 flags = fu_device_get_flags (device);
1186 if ((flags & FWUPD_DEVICE_FLAG_LOCKED) == 0) {
1187 g_set_error (error,
1188 FWUPD_ERROR,
1189 FWUPD_ERROR_NOT_SUPPORTED,
1190 "Device %s is not locked",
1191 fu_device_get_id (device));
1192 return FALSE;
1193 }
1194
Richard Hughes9c4b5312017-11-14 11:34:53 +00001195 /* run vfunc */
1196 if (!fu_plugin_runner_device_generic (plugin, device,
1197 "fu_plugin_unlock", error))
1198 return FALSE;
Richard Hughescff38bc2016-12-12 12:03:37 +00001199
1200 /* update with correct flags */
1201 flags = fu_device_get_flags (device);
1202 fu_device_set_flags (device, flags &= ~FWUPD_DEVICE_FLAG_LOCKED);
1203 fu_device_set_modified (device, (guint64) g_get_real_time () / G_USEC_PER_SEC);
1204 return TRUE;
1205}
1206
1207gboolean
1208fu_plugin_runner_update (FuPlugin *plugin,
Richard Hughesa785a1c2017-08-25 16:00:58 +01001209 FuDevice *device,
1210 GBytes *blob_cab,
1211 GBytes *blob_fw,
1212 FwupdInstallFlags flags,
1213 GError **error)
Richard Hughescff38bc2016-12-12 12:03:37 +00001214{
1215 FuPluginPrivate *priv = GET_PRIVATE (plugin);
Richard Hughesa785a1c2017-08-25 16:00:58 +01001216 FuPluginUpdateFunc update_func;
Richard Hughes780ef3f2018-01-12 16:20:31 +00001217 g_autoptr(FuHistory) history = NULL;
Richard Hughes68982c62017-09-13 15:40:14 +01001218 g_autoptr(FuDevice) device_pending = NULL;
Richard Hughescff38bc2016-12-12 12:03:37 +00001219 GError *error_update = NULL;
Richard Hughesf556d372017-06-15 19:49:18 +01001220 GPtrArray *checksums;
Richard Hughescff38bc2016-12-12 12:03:37 +00001221
1222 /* not enabled */
Richard Hughes41c15482018-02-01 22:07:21 +00001223 if (!priv->enabled) {
1224 g_debug ("plugin not enabled, skipping");
Richard Hughesd0905142016-03-13 09:46:49 +00001225 return TRUE;
Richard Hughes41c15482018-02-01 22:07:21 +00001226 }
Richard Hughesd0905142016-03-13 09:46:49 +00001227
Richard Hughes639da472018-01-06 22:35:04 +00001228 /* no object loaded */
Richard Hughes41c15482018-02-01 22:07:21 +00001229 if (priv->module == NULL) {
1230 g_debug ("module not enabled, skipping");
Richard Hughes639da472018-01-06 22:35:04 +00001231 return TRUE;
Richard Hughes41c15482018-02-01 22:07:21 +00001232 }
Richard Hughes639da472018-01-06 22:35:04 +00001233
Richard Hughesd0905142016-03-13 09:46:49 +00001234 /* optional */
Richard Hughesa785a1c2017-08-25 16:00:58 +01001235 g_module_symbol (priv->module, "fu_plugin_update", (gpointer *) &update_func);
1236 if (update_func == NULL) {
1237 g_set_error_literal (error,
1238 FWUPD_ERROR,
1239 FWUPD_ERROR_NOT_SUPPORTED,
1240 "No update possible");
1241 return FALSE;
1242 }
Richard Hughesd0905142016-03-13 09:46:49 +00001243
Richard Hughesa785a1c2017-08-25 16:00:58 +01001244 /* just schedule this for the next reboot */
Richard Hughescff38bc2016-12-12 12:03:37 +00001245 if (flags & FWUPD_INSTALL_FLAG_OFFLINE) {
Richard Hughesa785a1c2017-08-25 16:00:58 +01001246 return fu_plugin_runner_schedule_update (plugin,
1247 device,
1248 blob_cab,
1249 error);
Richard Hughescff38bc2016-12-12 12:03:37 +00001250 }
1251
1252 /* cancel the pending action */
1253 if (!fu_plugin_runner_offline_invalidate (error))
1254 return FALSE;
1255
1256 /* online */
Richard Hughes780ef3f2018-01-12 16:20:31 +00001257 history = fu_history_new ();
Richard Hughes0b9d9962018-01-12 16:31:28 +00001258 device_pending = fu_history_get_device_by_id (history, fu_device_get_id (device), NULL);
Richard Hughesa785a1c2017-08-25 16:00:58 +01001259 if (!update_func (plugin, device, blob_fw, flags, &error_update)) {
Richard Hughesc0cd0232018-01-31 15:02:00 +00001260 fu_device_set_update_error (device, error_update->message);
Richard Hughescff38bc2016-12-12 12:03:37 +00001261 g_propagate_error (error, error_update);
1262 return FALSE;
1263 }
1264
Richard Hughesf556d372017-06-15 19:49:18 +01001265 /* no longer valid */
1266 checksums = fu_device_get_checksums (device);
1267 g_ptr_array_set_size (checksums, 0);
1268
Richard Hughescff38bc2016-12-12 12:03:37 +00001269 /* cleanup */
Richard Hughes68982c62017-09-13 15:40:14 +01001270 if (device_pending != NULL) {
Richard Hughescff38bc2016-12-12 12:03:37 +00001271 const gchar *tmp;
Richard Hughesbc3a4e12018-01-06 22:41:47 +00001272 FwupdRelease *release;
Richard Hughescff38bc2016-12-12 12:03:37 +00001273
Richard Hughes780ef3f2018-01-12 16:20:31 +00001274 /* update history database */
Richard Hughesc0cd0232018-01-31 15:02:00 +00001275 fu_device_set_update_state (device, FWUPD_UPDATE_STATE_SUCCESS);
1276 if (!fu_history_modify_device (history, device,
1277 FU_HISTORY_FLAGS_MATCH_NEW_VERSION,
1278 error))
Richard Hughes0b9d9962018-01-12 16:31:28 +00001279 return FALSE;
Richard Hughescff38bc2016-12-12 12:03:37 +00001280
1281 /* delete cab file */
Richard Hughesbc3a4e12018-01-06 22:41:47 +00001282 release = fu_device_get_release_default (device_pending);
1283 tmp = fwupd_release_get_filename (release);
Richard Hughescff38bc2016-12-12 12:03:37 +00001284 if (tmp != NULL && g_str_has_prefix (tmp, LIBEXECDIR)) {
1285 g_autoptr(GError) error_local = NULL;
1286 g_autoptr(GFile) file = NULL;
1287 file = g_file_new_for_path (tmp);
1288 if (!g_file_delete (file, NULL, &error_local)) {
1289 g_set_error (error,
1290 FWUPD_ERROR,
1291 FWUPD_ERROR_INVALID_FILE,
1292 "Failed to delete %s: %s",
1293 tmp, error_local->message);
1294 return FALSE;
1295 }
1296 }
1297 }
Richard Hughesd0905142016-03-13 09:46:49 +00001298 return TRUE;
1299}
Richard Hughescff38bc2016-12-12 12:03:37 +00001300
1301gboolean
1302fu_plugin_runner_clear_results (FuPlugin *plugin, FuDevice *device, GError **error)
1303{
1304 FuPluginPrivate *priv = GET_PRIVATE (plugin);
Richard Hughes7b8b2022016-12-12 16:15:03 +00001305 FuPluginDeviceFunc func = NULL;
Richard Hughescff38bc2016-12-12 12:03:37 +00001306
1307 /* not enabled */
1308 if (!priv->enabled)
1309 return TRUE;
1310
Richard Hughes639da472018-01-06 22:35:04 +00001311 /* no object loaded */
1312 if (priv->module == NULL)
1313 return TRUE;
1314
Richard Hughes65e44ca2018-01-30 17:26:30 +00001315 /* optional */
1316 g_module_symbol (priv->module, "fu_plugin_get_results", (gpointer *) &func);
1317 if (func == NULL)
Richard Hughescff38bc2016-12-12 12:03:37 +00001318 return TRUE;
Richard Hughes65e44ca2018-01-30 17:26:30 +00001319 g_debug ("performing clear_result() on %s", priv->name);
1320 if (!func (plugin, device, error)) {
1321 g_prefix_error (error, "failed to clear_result %s: ", priv->name);
Richard Hughescff38bc2016-12-12 12:03:37 +00001322 return FALSE;
1323 }
Richard Hughes65e44ca2018-01-30 17:26:30 +00001324 return TRUE;
Richard Hughescff38bc2016-12-12 12:03:37 +00001325}
1326
1327gboolean
1328fu_plugin_runner_get_results (FuPlugin *plugin, FuDevice *device, GError **error)
1329{
1330 FuPluginPrivate *priv = GET_PRIVATE (plugin);
Richard Hughes7b8b2022016-12-12 16:15:03 +00001331 FuPluginDeviceFunc func = NULL;
Richard Hughescff38bc2016-12-12 12:03:37 +00001332
1333 /* not enabled */
1334 if (!priv->enabled)
1335 return TRUE;
1336
Richard Hughes639da472018-01-06 22:35:04 +00001337 /* no object loaded */
1338 if (priv->module == NULL)
1339 return TRUE;
1340
Richard Hughes65e44ca2018-01-30 17:26:30 +00001341 /* optional */
Richard Hughescff38bc2016-12-12 12:03:37 +00001342 g_module_symbol (priv->module, "fu_plugin_get_results", (gpointer *) &func);
Richard Hughes65e44ca2018-01-30 17:26:30 +00001343 if (func == NULL)
Richard Hughescff38bc2016-12-12 12:03:37 +00001344 return TRUE;
Richard Hughes65e44ca2018-01-30 17:26:30 +00001345 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);
Richard Hughescff38bc2016-12-12 12:03:37 +00001348 return FALSE;
1349 }
Richard Hughescff38bc2016-12-12 12:03:37 +00001350 return TRUE;
1351}
1352
Richard Hughes08a37992017-09-12 12:57:43 +01001353/**
1354 * fu_plugin_get_order:
1355 * @plugin: a #FuPlugin
1356 *
1357 * Gets the plugin order, where higher numbers are run after lower
1358 * numbers.
1359 *
1360 * Returns: the integer value
1361 **/
1362guint
1363fu_plugin_get_order (FuPlugin *plugin)
1364{
1365 FuPluginPrivate *priv = fu_plugin_get_instance_private (plugin);
1366 return priv->order;
1367}
1368
1369/**
1370 * fu_plugin_set_order:
1371 * @plugin: a #FuPlugin
1372 * @order: a integer value
1373 *
1374 * Sets the plugin order, where higher numbers are run after lower
1375 * numbers.
1376 **/
1377void
1378fu_plugin_set_order (FuPlugin *plugin, guint order)
1379{
1380 FuPluginPrivate *priv = fu_plugin_get_instance_private (plugin);
1381 priv->order = order;
1382}
1383
1384/**
1385 * fu_plugin_add_rule:
1386 * @plugin: a #FuPlugin
1387 * @rule: a #FuPluginRule, e.g. %FU_PLUGIN_RULE_CONFLICTS
Richard Hughes4eada342017-10-03 21:20:32 +01001388 * @name: a plugin name, e.g. `upower`
Richard Hughes08a37992017-09-12 12:57:43 +01001389 *
1390 * If the plugin name is found, the rule will be used to sort the plugin list,
1391 * for example the plugin specified by @name will be ordered after this plugin
1392 * when %FU_PLUGIN_RULE_RUN_AFTER is used.
1393 *
1394 * NOTE: The depsolver is iterative and may not solve overly-complicated rules;
1395 * If depsolving fails then fwupd will not start.
1396 **/
1397void
1398fu_plugin_add_rule (FuPlugin *plugin, FuPluginRule rule, const gchar *name)
1399{
1400 FuPluginPrivate *priv = fu_plugin_get_instance_private (plugin);
1401 g_ptr_array_add (priv->rules[rule], g_strdup (name));
1402}
1403
1404/**
1405 * fu_plugin_get_rules:
1406 * @plugin: a #FuPlugin
1407 * @rule: a #FuPluginRule, e.g. %FU_PLUGIN_RULE_CONFLICTS
1408 *
1409 * Gets the plugin IDs that should be run after this plugin.
1410 *
1411 * Returns: (element-type utf8) (transfer none): the list of plugin names, e.g. ['appstream']
1412 **/
1413GPtrArray *
1414fu_plugin_get_rules (FuPlugin *plugin, FuPluginRule rule)
1415{
1416 FuPluginPrivate *priv = fu_plugin_get_instance_private (plugin);
1417 return priv->rules[rule];
1418}
1419
Richard Hughes80b79bb2018-01-11 21:11:06 +00001420/**
1421 * fu_plugin_add_report_metadata:
1422 * @plugin: a #FuPlugin
1423 * @key: a string, e.g. `FwupdateVersion`
1424 * @value: a string, e.g. `10`
1425 *
1426 * Sets any additional metadata to be included in the firmware report to aid
1427 * debugging problems.
1428 *
1429 * Any data included here will be sent to the metadata server after user
1430 * confirmation.
1431 **/
1432void
1433fu_plugin_add_report_metadata (FuPlugin *plugin, const gchar *key, const gchar *value)
1434{
1435 FuPluginPrivate *priv = fu_plugin_get_instance_private (plugin);
1436 g_hash_table_insert (priv->report_metadata, g_strdup (key), g_strdup (value));
1437}
1438
1439/**
1440 * fu_plugin_get_report_metadata:
1441 * @plugin: a #FuPlugin
1442 *
1443 * Returns the list of additional metadata to be added when filing a report.
1444 *
1445 * Returns: (transfer none): the map of report metadata
1446 **/
1447GHashTable *
1448fu_plugin_get_report_metadata (FuPlugin *plugin)
1449{
1450 FuPluginPrivate *priv = fu_plugin_get_instance_private (plugin);
1451 return priv->report_metadata;
1452}
1453
Mario Limonciello963dc422018-02-27 14:26:58 -06001454/**
1455 * fu_plugin_get_config_value:
1456 * @plugin: a #FuPlugin
1457 * @key: A settings key
1458 *
1459 * Return the value of a key if it's been configured
1460 *
1461 * Since: 1.0.6
1462 **/
1463gchar *
1464fu_plugin_get_config_value (FuPlugin *plugin, const gchar *key)
1465{
1466 g_autofree gchar *conf_file = NULL;
1467 g_autofree gchar *conf_path = NULL;
1468 g_autoptr(GKeyFile) keyfile = NULL;
1469 const gchar *plugin_name;
1470
1471 plugin_name = fu_plugin_get_name (plugin);
1472 conf_file = g_strdup_printf ("%s.conf", plugin_name);
1473 conf_path = g_build_filename (FWUPDCONFIGDIR, conf_file, NULL);
1474 if (!g_file_test (conf_path, G_FILE_TEST_IS_REGULAR))
1475 return NULL;
1476 keyfile = g_key_file_new ();
1477 if (!g_key_file_load_from_file (keyfile, conf_path,
1478 G_KEY_FILE_NONE, NULL))
1479 return NULL;
1480 return g_key_file_get_string (keyfile, plugin_name, key, NULL);
1481}
1482
Richard Hughescff38bc2016-12-12 12:03:37 +00001483static void
1484fu_plugin_class_init (FuPluginClass *klass)
1485{
1486 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1487 object_class->finalize = fu_plugin_finalize;
1488 signals[SIGNAL_DEVICE_ADDED] =
1489 g_signal_new ("device-added",
1490 G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST,
1491 G_STRUCT_OFFSET (FuPluginClass, device_added),
1492 NULL, NULL, g_cclosure_marshal_VOID__OBJECT,
1493 G_TYPE_NONE, 1, FU_TYPE_DEVICE);
1494 signals[SIGNAL_DEVICE_REMOVED] =
1495 g_signal_new ("device-removed",
1496 G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST,
1497 G_STRUCT_OFFSET (FuPluginClass, device_removed),
1498 NULL, NULL, g_cclosure_marshal_VOID__OBJECT,
1499 G_TYPE_NONE, 1, FU_TYPE_DEVICE);
Richard Hughese1fd34d2017-08-24 14:19:51 +01001500 signals[SIGNAL_DEVICE_REGISTER] =
1501 g_signal_new ("device-register",
1502 G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST,
1503 G_STRUCT_OFFSET (FuPluginClass, device_register),
1504 NULL, NULL, g_cclosure_marshal_VOID__OBJECT,
1505 G_TYPE_NONE, 1, FU_TYPE_DEVICE);
Richard Hughes362d6d72017-01-07 21:42:14 +00001506 signals[SIGNAL_RECOLDPLUG] =
1507 g_signal_new ("recoldplug",
1508 G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST,
1509 G_STRUCT_OFFSET (FuPluginClass, recoldplug),
1510 NULL, NULL, g_cclosure_marshal_VOID__VOID,
1511 G_TYPE_NONE, 0);
Richard Hughesb0829032017-01-10 09:27:08 +00001512 signals[SIGNAL_SET_COLDPLUG_DELAY] =
1513 g_signal_new ("set-coldplug-delay",
1514 G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST,
1515 G_STRUCT_OFFSET (FuPluginClass, set_coldplug_delay),
1516 NULL, NULL, g_cclosure_marshal_VOID__UINT,
1517 G_TYPE_NONE, 1, G_TYPE_UINT);
Richard Hughescff38bc2016-12-12 12:03:37 +00001518}
1519
1520static void
1521fu_plugin_init (FuPlugin *plugin)
1522{
1523 FuPluginPrivate *priv = GET_PRIVATE (plugin);
1524 priv->enabled = TRUE;
1525 priv->devices = g_hash_table_new_full (g_str_hash, g_str_equal,
1526 g_free, (GDestroyNotify) g_object_unref);
Richard Hughese8b5db62017-07-17 14:18:22 +01001527 priv->devices_delay = g_hash_table_new (g_direct_hash, g_direct_equal);
Richard Hughes80b79bb2018-01-11 21:11:06 +00001528 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 +01001529 for (guint i = 0; i < FU_PLUGIN_RULE_LAST; i++)
1530 priv->rules[i] = g_ptr_array_new_with_free_func (g_free);
Richard Hughescff38bc2016-12-12 12:03:37 +00001531}
1532
1533static void
1534fu_plugin_finalize (GObject *object)
1535{
1536 FuPlugin *plugin = FU_PLUGIN (object);
1537 FuPluginPrivate *priv = GET_PRIVATE (plugin);
1538 FuPluginInitFunc func = NULL;
1539
1540 /* optional */
1541 if (priv->module != NULL) {
1542 g_module_symbol (priv->module, "fu_plugin_destroy", (gpointer *) &func);
1543 if (func != NULL) {
1544 g_debug ("performing destroy() on %s", priv->name);
1545 func (plugin);
1546 }
1547 }
1548
Richard Hughes08a37992017-09-12 12:57:43 +01001549 for (guint i = 0; i < FU_PLUGIN_RULE_LAST; i++)
1550 g_ptr_array_unref (priv->rules[i]);
1551
Richard Hughescff38bc2016-12-12 12:03:37 +00001552 if (priv->usb_ctx != NULL)
1553 g_object_unref (priv->usb_ctx);
Richard Hughesb8f8db22017-04-25 15:56:00 +01001554 if (priv->hwids != NULL)
Richard Hughesd7704d42017-08-08 20:29:09 +01001555 g_object_unref (priv->hwids);
Richard Hughes9c028f02017-10-28 21:14:28 +01001556 if (priv->quirks != NULL)
1557 g_object_unref (priv->quirks);
Richard Hughes1354ea92017-09-19 15:58:31 +01001558 if (priv->supported_guids != NULL)
1559 g_ptr_array_unref (priv->supported_guids);
Richard Hughes49e5e052017-09-03 12:15:41 +01001560 if (priv->smbios != NULL)
1561 g_object_unref (priv->smbios);
Richard Hughes275d3b42018-04-20 16:40:37 +01001562 if (priv->runtime_versions != NULL)
1563 g_hash_table_unref (priv->runtime_versions);
Richard Hughes576c0122017-02-24 09:47:00 +00001564#ifndef RUNNING_ON_VALGRIND
Richard Hughescff38bc2016-12-12 12:03:37 +00001565 if (priv->module != NULL)
1566 g_module_close (priv->module);
Richard Hughes576c0122017-02-24 09:47:00 +00001567#endif
Richard Hughescff38bc2016-12-12 12:03:37 +00001568 g_hash_table_unref (priv->devices);
Richard Hughesae3d65f2016-12-16 09:38:01 +00001569 g_hash_table_unref (priv->devices_delay);
Richard Hughes80b79bb2018-01-11 21:11:06 +00001570 g_hash_table_unref (priv->report_metadata);
Richard Hughescff38bc2016-12-12 12:03:37 +00001571 g_free (priv->name);
1572 g_free (priv->data);
1573
1574 G_OBJECT_CLASS (fu_plugin_parent_class)->finalize (object);
1575}
1576
1577FuPlugin *
1578fu_plugin_new (void)
1579{
1580 FuPlugin *plugin;
1581 plugin = g_object_new (FU_TYPE_PLUGIN, NULL);
1582 return plugin;
1583}