Richard Hughes | d090514 | 2016-03-13 09:46:49 +0000 | [diff] [blame] | 1 | /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- |
| 2 | * |
Richard Hughes | a785a1c | 2017-08-25 16:00:58 +0100 | [diff] [blame] | 3 | * Copyright (C) 2016-2017 Richard Hughes <richard@hughsie.com> |
Richard Hughes | d090514 | 2016-03-13 09:46:49 +0000 | [diff] [blame] | 4 | * |
| 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 Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 24 | #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 Limonciello | 6d0aa3d | 2017-02-28 08:22:27 -0600 | [diff] [blame] | 30 | #ifdef HAVE_VALGRIND |
Richard Hughes | 576c012 | 2017-02-24 09:47:00 +0000 | [diff] [blame] | 31 | #include <valgrind.h> |
Mario Limonciello | 6d0aa3d | 2017-02-28 08:22:27 -0600 | [diff] [blame] | 32 | #endif /* HAVE_VALGRIND */ |
Richard Hughes | d090514 | 2016-03-13 09:46:49 +0000 | [diff] [blame] | 33 | |
Richard Hughes | 9dde04f | 2017-09-13 12:07:15 +0100 | [diff] [blame] | 34 | #include "fu-device-private.h" |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 35 | #include "fu-plugin-private.h" |
| 36 | #include "fu-pending.h" |
Richard Hughes | d090514 | 2016-03-13 09:46:49 +0000 | [diff] [blame] | 37 | |
Richard Hughes | 4eada34 | 2017-10-03 21:20:32 +0100 | [diff] [blame] | 38 | /** |
| 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 Hughes | b082903 | 2017-01-10 09:27:08 +0000 | [diff] [blame] | 47 | #define FU_PLUGIN_COLDPLUG_DELAY_MAXIMUM 3000u /* ms */ |
| 48 | |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 49 | static void fu_plugin_finalize (GObject *object); |
| 50 | |
| 51 | typedef struct { |
| 52 | GModule *module; |
| 53 | GUsbContext *usb_ctx; |
| 54 | gboolean enabled; |
Richard Hughes | 08a3799 | 2017-09-12 12:57:43 +0100 | [diff] [blame] | 55 | guint order; |
| 56 | GPtrArray *rules[FU_PLUGIN_RULE_LAST]; |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 57 | gchar *name; |
Richard Hughes | d7704d4 | 2017-08-08 20:29:09 +0100 | [diff] [blame] | 58 | FuHwids *hwids; |
Richard Hughes | 9c028f0 | 2017-10-28 21:14:28 +0100 | [diff] [blame] | 59 | FuQuirks *quirks; |
Richard Hughes | 1354ea9 | 2017-09-19 15:58:31 +0100 | [diff] [blame] | 60 | GPtrArray *supported_guids; |
| 61 | FuSmbios *smbios; |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 62 | GHashTable *devices; /* platform_id:GObject */ |
Richard Hughes | ae3d65f | 2016-12-16 09:38:01 +0000 | [diff] [blame] | 63 | GHashTable *devices_delay; /* FuDevice:FuPluginHelper */ |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 64 | FuPluginData *data; |
| 65 | } FuPluginPrivate; |
| 66 | |
| 67 | enum { |
| 68 | SIGNAL_DEVICE_ADDED, |
| 69 | SIGNAL_DEVICE_REMOVED, |
Richard Hughes | e1fd34d | 2017-08-24 14:19:51 +0100 | [diff] [blame] | 70 | SIGNAL_DEVICE_REGISTER, |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 71 | SIGNAL_STATUS_CHANGED, |
| 72 | SIGNAL_PERCENTAGE_CHANGED, |
Richard Hughes | 362d6d7 | 2017-01-07 21:42:14 +0000 | [diff] [blame] | 73 | SIGNAL_RECOLDPLUG, |
Richard Hughes | b082903 | 2017-01-10 09:27:08 +0000 | [diff] [blame] | 74 | SIGNAL_SET_COLDPLUG_DELAY, |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 75 | SIGNAL_LAST |
| 76 | }; |
| 77 | |
| 78 | static guint signals[SIGNAL_LAST] = { 0 }; |
| 79 | |
| 80 | G_DEFINE_TYPE_WITH_PRIVATE (FuPlugin, fu_plugin, G_TYPE_OBJECT) |
| 81 | #define GET_PRIVATE(o) (fu_plugin_get_instance_private (o)) |
| 82 | |
| 83 | typedef const gchar *(*FuPluginGetNameFunc) (void); |
| 84 | typedef void (*FuPluginInitFunc) (FuPlugin *plugin); |
| 85 | typedef gboolean (*FuPluginStartupFunc) (FuPlugin *plugin, |
| 86 | GError **error); |
Richard Hughes | e1fd34d | 2017-08-24 14:19:51 +0100 | [diff] [blame] | 87 | typedef void (*FuPluginDeviceRegisterFunc) (FuPlugin *plugin, |
| 88 | FuDevice *device); |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 89 | typedef gboolean (*FuPluginDeviceFunc) (FuPlugin *plugin, |
| 90 | FuDevice *device, |
| 91 | GError **error); |
| 92 | typedef gboolean (*FuPluginVerifyFunc) (FuPlugin *plugin, |
| 93 | FuDevice *device, |
| 94 | FuPluginVerifyFlags flags, |
| 95 | GError **error); |
| 96 | typedef gboolean (*FuPluginUpdateFunc) (FuPlugin *plugin, |
| 97 | FuDevice *device, |
| 98 | GBytes *blob_fw, |
| 99 | FwupdInstallFlags flags, |
| 100 | GError **error); |
| 101 | |
Richard Hughes | 57d1822 | 2017-01-10 16:02:59 +0000 | [diff] [blame] | 102 | /** |
| 103 | * fu_plugin_get_name: |
| 104 | * @plugin: A #FuPlugin |
| 105 | * |
| 106 | * Gets the plugin name. |
| 107 | * |
| 108 | * Returns: a plugin name, or %NULL for unknown. |
| 109 | * |
| 110 | * Since: 0.8.0 |
| 111 | **/ |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 112 | const gchar * |
| 113 | fu_plugin_get_name (FuPlugin *plugin) |
Richard Hughes | d090514 | 2016-03-13 09:46:49 +0000 | [diff] [blame] | 114 | { |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 115 | FuPluginPrivate *priv = GET_PRIVATE (plugin); |
Richard Hughes | ccd78a9 | 2017-01-11 16:57:41 +0000 | [diff] [blame] | 116 | g_return_val_if_fail (FU_IS_PLUGIN (plugin), NULL); |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 117 | return priv->name; |
| 118 | } |
Richard Hughes | d090514 | 2016-03-13 09:46:49 +0000 | [diff] [blame] | 119 | |
Richard Hughes | 57d1822 | 2017-01-10 16:02:59 +0000 | [diff] [blame] | 120 | /** |
| 121 | * fu_plugin_cache_lookup: |
| 122 | * @plugin: A #FuPlugin |
| 123 | * @id: the key |
| 124 | * |
| 125 | * Finds an object in the per-plugin cache. |
| 126 | * |
| 127 | * Returns: (transfer none): a #GObject, or %NULL for unfound. |
| 128 | * |
| 129 | * Since: 0.8.0 |
| 130 | **/ |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 131 | gpointer |
| 132 | fu_plugin_cache_lookup (FuPlugin *plugin, const gchar *id) |
| 133 | { |
| 134 | FuPluginPrivate *priv = GET_PRIVATE (plugin); |
Richard Hughes | ccd78a9 | 2017-01-11 16:57:41 +0000 | [diff] [blame] | 135 | g_return_val_if_fail (FU_IS_PLUGIN (plugin), NULL); |
| 136 | g_return_val_if_fail (id != NULL, NULL); |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 137 | return g_hash_table_lookup (priv->devices, id); |
| 138 | } |
Richard Hughes | d090514 | 2016-03-13 09:46:49 +0000 | [diff] [blame] | 139 | |
Richard Hughes | 57d1822 | 2017-01-10 16:02:59 +0000 | [diff] [blame] | 140 | /** |
| 141 | * fu_plugin_cache_add: |
| 142 | * @plugin: A #FuPlugin |
| 143 | * @id: the key |
| 144 | * @dev: a #GObject, typically a #FuDevice |
| 145 | * |
| 146 | * Adds an object to the per-plugin cache. |
| 147 | * |
| 148 | * Since: 0.8.0 |
| 149 | **/ |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 150 | void |
| 151 | fu_plugin_cache_add (FuPlugin *plugin, const gchar *id, gpointer dev) |
| 152 | { |
| 153 | FuPluginPrivate *priv = GET_PRIVATE (plugin); |
Richard Hughes | ccd78a9 | 2017-01-11 16:57:41 +0000 | [diff] [blame] | 154 | g_return_if_fail (FU_IS_PLUGIN (plugin)); |
| 155 | g_return_if_fail (id != NULL); |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 156 | g_hash_table_insert (priv->devices, g_strdup (id), g_object_ref (dev)); |
| 157 | } |
| 158 | |
Richard Hughes | 57d1822 | 2017-01-10 16:02:59 +0000 | [diff] [blame] | 159 | /** |
| 160 | * fu_plugin_cache_remove: |
| 161 | * @plugin: A #FuPlugin |
| 162 | * @id: the key |
| 163 | * |
| 164 | * Removes an object from the per-plugin cache. |
| 165 | * |
| 166 | * Since: 0.8.0 |
| 167 | **/ |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 168 | void |
| 169 | fu_plugin_cache_remove (FuPlugin *plugin, const gchar *id) |
| 170 | { |
| 171 | FuPluginPrivate *priv = GET_PRIVATE (plugin); |
Richard Hughes | ccd78a9 | 2017-01-11 16:57:41 +0000 | [diff] [blame] | 172 | g_return_if_fail (FU_IS_PLUGIN (plugin)); |
| 173 | g_return_if_fail (id != NULL); |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 174 | g_hash_table_remove (priv->devices, id); |
| 175 | } |
| 176 | |
Richard Hughes | 57d1822 | 2017-01-10 16:02:59 +0000 | [diff] [blame] | 177 | /** |
| 178 | * fu_plugin_get_data: |
| 179 | * @plugin: A #FuPlugin |
| 180 | * |
Richard Hughes | 4eada34 | 2017-10-03 21:20:32 +0100 | [diff] [blame] | 181 | * Gets the per-plugin allocated private data. This will return %NULL unless |
| 182 | * fu_plugin_alloc_data() has been called by the plugin. |
Richard Hughes | 57d1822 | 2017-01-10 16:02:59 +0000 | [diff] [blame] | 183 | * |
Richard Hughes | 4eada34 | 2017-10-03 21:20:32 +0100 | [diff] [blame] | 184 | * Returns: (transfer none): a pointer to a structure, or %NULL for unset. |
Richard Hughes | 57d1822 | 2017-01-10 16:02:59 +0000 | [diff] [blame] | 185 | * |
| 186 | * Since: 0.8.0 |
| 187 | **/ |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 188 | FuPluginData * |
| 189 | fu_plugin_get_data (FuPlugin *plugin) |
| 190 | { |
| 191 | FuPluginPrivate *priv = GET_PRIVATE (plugin); |
Richard Hughes | ccd78a9 | 2017-01-11 16:57:41 +0000 | [diff] [blame] | 192 | g_return_val_if_fail (FU_IS_PLUGIN (plugin), NULL); |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 193 | return priv->data; |
| 194 | } |
| 195 | |
Richard Hughes | 57d1822 | 2017-01-10 16:02:59 +0000 | [diff] [blame] | 196 | /** |
| 197 | * fu_plugin_alloc_data: |
| 198 | * @plugin: A #FuPlugin |
| 199 | * @data_sz: the size to allocate |
| 200 | * |
| 201 | * Allocates the per-plugin allocated private data. |
| 202 | * |
| 203 | * Returns: (transfer full): a pointer to a structure, or %NULL for unset. |
| 204 | * |
| 205 | * Since: 0.8.0 |
| 206 | **/ |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 207 | FuPluginData * |
| 208 | fu_plugin_alloc_data (FuPlugin *plugin, gsize data_sz) |
| 209 | { |
| 210 | FuPluginPrivate *priv = GET_PRIVATE (plugin); |
Richard Hughes | ccd78a9 | 2017-01-11 16:57:41 +0000 | [diff] [blame] | 211 | g_return_val_if_fail (FU_IS_PLUGIN (plugin), NULL); |
Richard Hughes | 44dee88 | 2017-01-11 08:31:10 +0000 | [diff] [blame] | 212 | if (priv->data != NULL) { |
| 213 | g_critical ("fu_plugin_alloc_data() already used by plugin"); |
| 214 | return priv->data; |
| 215 | } |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 216 | priv->data = g_malloc0 (data_sz); |
| 217 | return priv->data; |
Richard Hughes | d090514 | 2016-03-13 09:46:49 +0000 | [diff] [blame] | 218 | } |
| 219 | |
Richard Hughes | 57d1822 | 2017-01-10 16:02:59 +0000 | [diff] [blame] | 220 | /** |
| 221 | * fu_plugin_get_usb_context: |
| 222 | * @plugin: A #FuPlugin |
| 223 | * |
| 224 | * Gets the shared USB context that all plugins can use. |
| 225 | * |
| 226 | * Returns: (transfer none): a #GUsbContext. |
| 227 | * |
| 228 | * Since: 0.8.0 |
| 229 | **/ |
Richard Hughes | bc93e4a | 2016-12-08 17:29:51 +0000 | [diff] [blame] | 230 | GUsbContext * |
| 231 | fu_plugin_get_usb_context (FuPlugin *plugin) |
| 232 | { |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 233 | FuPluginPrivate *priv = GET_PRIVATE (plugin); |
Richard Hughes | ccd78a9 | 2017-01-11 16:57:41 +0000 | [diff] [blame] | 234 | g_return_val_if_fail (FU_IS_PLUGIN (plugin), NULL); |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 235 | return priv->usb_ctx; |
Richard Hughes | bc93e4a | 2016-12-08 17:29:51 +0000 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | void |
| 239 | fu_plugin_set_usb_context (FuPlugin *plugin, GUsbContext *usb_ctx) |
| 240 | { |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 241 | FuPluginPrivate *priv = GET_PRIVATE (plugin); |
| 242 | g_set_object (&priv->usb_ctx, usb_ctx); |
| 243 | } |
| 244 | |
Richard Hughes | 57d1822 | 2017-01-10 16:02:59 +0000 | [diff] [blame] | 245 | /** |
| 246 | * fu_plugin_get_enabled: |
| 247 | * @plugin: A #FuPlugin |
| 248 | * |
Richard Hughes | 4eada34 | 2017-10-03 21:20:32 +0100 | [diff] [blame] | 249 | * Returns if the plugin is enabled. Plugins may self-disable using |
| 250 | * fu_plugin_set_enabled() or can be disabled by the daemon. |
Richard Hughes | 57d1822 | 2017-01-10 16:02:59 +0000 | [diff] [blame] | 251 | * |
| 252 | * Returns: %TRUE if the plugin is currently enabled. |
| 253 | * |
| 254 | * Since: 0.8.0 |
| 255 | **/ |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 256 | gboolean |
| 257 | fu_plugin_get_enabled (FuPlugin *plugin) |
| 258 | { |
| 259 | FuPluginPrivate *priv = GET_PRIVATE (plugin); |
Richard Hughes | ccd78a9 | 2017-01-11 16:57:41 +0000 | [diff] [blame] | 260 | g_return_val_if_fail (FU_IS_PLUGIN (plugin), FALSE); |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 261 | return priv->enabled; |
Richard Hughes | bc93e4a | 2016-12-08 17:29:51 +0000 | [diff] [blame] | 262 | } |
| 263 | |
Richard Hughes | 57d1822 | 2017-01-10 16:02:59 +0000 | [diff] [blame] | 264 | /** |
| 265 | * fu_plugin_set_enabled: |
| 266 | * @plugin: A #FuPlugin |
| 267 | * @enabled: the enabled value |
| 268 | * |
| 269 | * Enables or disables a plugin. Plugins can self-disable at any point. |
| 270 | * |
| 271 | * Since: 0.8.0 |
| 272 | **/ |
Richard Hughes | d090514 | 2016-03-13 09:46:49 +0000 | [diff] [blame] | 273 | void |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 274 | fu_plugin_set_enabled (FuPlugin *plugin, gboolean enabled) |
Richard Hughes | d090514 | 2016-03-13 09:46:49 +0000 | [diff] [blame] | 275 | { |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 276 | FuPluginPrivate *priv = GET_PRIVATE (plugin); |
Richard Hughes | ccd78a9 | 2017-01-11 16:57:41 +0000 | [diff] [blame] | 277 | g_return_if_fail (FU_IS_PLUGIN (plugin)); |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 278 | priv->enabled = enabled; |
| 279 | } |
| 280 | |
| 281 | gboolean |
| 282 | fu_plugin_open (FuPlugin *plugin, const gchar *filename, GError **error) |
| 283 | { |
| 284 | FuPluginPrivate *priv = GET_PRIVATE (plugin); |
| 285 | FuPluginInitFunc func = NULL; |
| 286 | gchar *str; |
| 287 | |
| 288 | priv->module = g_module_open (filename, 0); |
| 289 | if (priv->module == NULL) { |
| 290 | g_set_error (error, |
| 291 | G_IO_ERROR, |
| 292 | G_IO_ERROR_FAILED, |
| 293 | "failed to open plugin: %s", |
| 294 | g_module_error ()); |
| 295 | return FALSE; |
| 296 | } |
| 297 | |
| 298 | /* set automatically */ |
| 299 | str = g_strstr_len (filename, -1, "libfu_plugin_"); |
| 300 | if (str != NULL) { |
| 301 | priv->name = g_strdup (str + 13); |
| 302 | g_strdelimit (priv->name, ".", '\0'); |
| 303 | } |
Richard Hughes | d090514 | 2016-03-13 09:46:49 +0000 | [diff] [blame] | 304 | |
| 305 | /* optional */ |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 306 | g_module_symbol (priv->module, "fu_plugin_init", (gpointer *) &func); |
| 307 | if (func != NULL) { |
| 308 | g_debug ("performing init() on %s", filename); |
Richard Hughes | d090514 | 2016-03-13 09:46:49 +0000 | [diff] [blame] | 309 | func (plugin); |
| 310 | } |
| 311 | |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 312 | return TRUE; |
| 313 | } |
| 314 | |
Richard Hughes | 57d1822 | 2017-01-10 16:02:59 +0000 | [diff] [blame] | 315 | /** |
| 316 | * fu_plugin_device_add: |
| 317 | * @plugin: A #FuPlugin |
| 318 | * @device: A #FuDevice |
| 319 | * |
| 320 | * Asks the daemon to add a device to the exported list. If this device ID |
| 321 | * has already been added by a different plugin then this request will be |
| 322 | * ignored. |
| 323 | * |
| 324 | * Plugins should use fu_plugin_device_add_delay() if they are not capable of |
| 325 | * actually flashing an image to the hardware so that higher-priority plugins |
| 326 | * can add the device themselves. |
| 327 | * |
| 328 | * Since: 0.8.0 |
| 329 | **/ |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 330 | void |
| 331 | fu_plugin_device_add (FuPlugin *plugin, FuDevice *device) |
| 332 | { |
Richard Hughes | ccd78a9 | 2017-01-11 16:57:41 +0000 | [diff] [blame] | 333 | g_return_if_fail (FU_IS_PLUGIN (plugin)); |
| 334 | g_return_if_fail (FU_IS_DEVICE (device)); |
| 335 | |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 336 | g_debug ("emit added from %s: %s", |
| 337 | fu_plugin_get_name (plugin), |
| 338 | fu_device_get_id (device)); |
| 339 | fu_device_set_created (device, (guint64) g_get_real_time () / G_USEC_PER_SEC); |
| 340 | fu_device_set_plugin (device, fu_plugin_get_name (plugin)); |
| 341 | g_signal_emit (plugin, signals[SIGNAL_DEVICE_ADDED], 0, device); |
| 342 | } |
| 343 | |
Richard Hughes | e1fd34d | 2017-08-24 14:19:51 +0100 | [diff] [blame] | 344 | /** |
| 345 | * fu_plugin_device_register: |
| 346 | * @plugin: A #FuPlugin |
| 347 | * @device: A #FuDevice |
| 348 | * |
| 349 | * Registers the device with other plugins so they can set metadata. |
| 350 | * |
| 351 | * Plugins do not have to call this manually as this is done automatically |
| 352 | * when using fu_plugin_device_add(). They may wish to use this manually |
| 353 | * if for intance the coldplug should be ignored based on the metadata |
| 354 | * set from other plugins. |
| 355 | * |
| 356 | * Since: 0.9.7 |
| 357 | **/ |
| 358 | void |
| 359 | fu_plugin_device_register (FuPlugin *plugin, FuDevice *device) |
| 360 | { |
| 361 | g_return_if_fail (FU_IS_PLUGIN (plugin)); |
| 362 | g_return_if_fail (FU_IS_DEVICE (device)); |
| 363 | |
| 364 | g_debug ("emit device-register from %s: %s", |
| 365 | fu_plugin_get_name (plugin), |
| 366 | fu_device_get_id (device)); |
| 367 | g_signal_emit (plugin, signals[SIGNAL_DEVICE_REGISTER], 0, device); |
| 368 | } |
| 369 | |
Richard Hughes | ae3d65f | 2016-12-16 09:38:01 +0000 | [diff] [blame] | 370 | typedef struct { |
| 371 | FuPlugin *plugin; |
| 372 | FuDevice *device; |
| 373 | guint timeout_id; |
Richard Hughes | d4184cf | 2016-12-21 16:05:17 +0000 | [diff] [blame] | 374 | GHashTable *devices; |
Richard Hughes | ae3d65f | 2016-12-16 09:38:01 +0000 | [diff] [blame] | 375 | } FuPluginHelper; |
| 376 | |
| 377 | static void |
| 378 | fu_plugin_helper_free (FuPluginHelper *helper) |
| 379 | { |
| 380 | g_object_unref (helper->plugin); |
| 381 | g_object_unref (helper->device); |
Richard Hughes | d4184cf | 2016-12-21 16:05:17 +0000 | [diff] [blame] | 382 | g_hash_table_unref (helper->devices); |
Richard Hughes | ae3d65f | 2016-12-16 09:38:01 +0000 | [diff] [blame] | 383 | g_free (helper); |
| 384 | } |
| 385 | |
| 386 | static gboolean |
| 387 | fu_plugin_device_add_delay_cb (gpointer user_data) |
| 388 | { |
| 389 | FuPluginHelper *helper = (FuPluginHelper *) user_data; |
| 390 | fu_plugin_device_add (helper->plugin, helper->device); |
Richard Hughes | 0dec274 | 2017-09-18 11:07:39 +0100 | [diff] [blame] | 391 | g_hash_table_remove (helper->devices, helper->device); |
Richard Hughes | ae3d65f | 2016-12-16 09:38:01 +0000 | [diff] [blame] | 392 | fu_plugin_helper_free (helper); |
| 393 | return FALSE; |
| 394 | } |
| 395 | |
Richard Hughes | 57d1822 | 2017-01-10 16:02:59 +0000 | [diff] [blame] | 396 | /** |
Richard Hughes | f0a799e | 2017-01-17 20:13:30 +0000 | [diff] [blame] | 397 | * fu_plugin_has_device_delay: |
| 398 | * @plugin: A #FuPlugin |
| 399 | * |
| 400 | * Returns if the device has a pending device that is waiting to be added. |
| 401 | * |
| 402 | * Returns: %TRUE if a device is waiting to be added |
| 403 | * |
| 404 | * Since: 0.8.0 |
| 405 | **/ |
| 406 | gboolean |
| 407 | fu_plugin_has_device_delay (FuPlugin *plugin) |
| 408 | { |
| 409 | FuPluginPrivate *priv = GET_PRIVATE (plugin); |
| 410 | return g_hash_table_size (priv->devices_delay) > 0; |
| 411 | } |
| 412 | |
| 413 | /** |
Richard Hughes | 57d1822 | 2017-01-10 16:02:59 +0000 | [diff] [blame] | 414 | * fu_plugin_device_add_delay: |
| 415 | * @plugin: A #FuPlugin |
| 416 | * @device: A #FuDevice |
| 417 | * |
| 418 | * Asks the daemon to add a device to the exported list after a small delay. |
| 419 | * |
| 420 | * Since: 0.8.0 |
| 421 | **/ |
Richard Hughes | ae3d65f | 2016-12-16 09:38:01 +0000 | [diff] [blame] | 422 | void |
| 423 | fu_plugin_device_add_delay (FuPlugin *plugin, FuDevice *device) |
| 424 | { |
| 425 | FuPluginPrivate *priv = GET_PRIVATE (plugin); |
| 426 | FuPluginHelper *helper; |
Richard Hughes | ccd78a9 | 2017-01-11 16:57:41 +0000 | [diff] [blame] | 427 | |
| 428 | g_return_if_fail (FU_IS_PLUGIN (plugin)); |
| 429 | g_return_if_fail (FU_IS_DEVICE (device)); |
| 430 | |
Richard Hughes | 6ad951d | 2017-02-03 10:12:12 +0000 | [diff] [blame] | 431 | /* already waiting for add */ |
| 432 | helper = g_hash_table_lookup (priv->devices_delay, device); |
| 433 | if (helper != NULL) { |
Richard Hughes | 3cca1c6 | 2017-07-21 13:38:20 +0100 | [diff] [blame] | 434 | g_debug ("ignoring add-delay as device %s already pending", |
| 435 | fu_device_get_id (device)); |
Richard Hughes | 6ad951d | 2017-02-03 10:12:12 +0000 | [diff] [blame] | 436 | return; |
| 437 | } |
| 438 | |
| 439 | /* add after a small delay */ |
Richard Hughes | ae3d65f | 2016-12-16 09:38:01 +0000 | [diff] [blame] | 440 | g_debug ("waiting a small time for other plugins"); |
| 441 | helper = g_new0 (FuPluginHelper, 1); |
| 442 | helper->plugin = g_object_ref (plugin); |
| 443 | helper->device = g_object_ref (device); |
| 444 | helper->timeout_id = g_timeout_add (500, fu_plugin_device_add_delay_cb, helper); |
Richard Hughes | d4184cf | 2016-12-21 16:05:17 +0000 | [diff] [blame] | 445 | helper->devices = g_hash_table_ref (priv->devices_delay); |
| 446 | g_hash_table_insert (helper->devices, device, helper); |
Richard Hughes | ae3d65f | 2016-12-16 09:38:01 +0000 | [diff] [blame] | 447 | } |
| 448 | |
Richard Hughes | 57d1822 | 2017-01-10 16:02:59 +0000 | [diff] [blame] | 449 | /** |
Richard Hughes | 4eada34 | 2017-10-03 21:20:32 +0100 | [diff] [blame] | 450 | * fu_plugin_device_remove: |
Richard Hughes | 57d1822 | 2017-01-10 16:02:59 +0000 | [diff] [blame] | 451 | * @plugin: A #FuPlugin |
| 452 | * @device: A #FuDevice |
| 453 | * |
| 454 | * Asks the daemon to remove a device from the exported list. |
| 455 | * |
| 456 | * Since: 0.8.0 |
| 457 | **/ |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 458 | void |
| 459 | fu_plugin_device_remove (FuPlugin *plugin, FuDevice *device) |
| 460 | { |
Richard Hughes | ae3d65f | 2016-12-16 09:38:01 +0000 | [diff] [blame] | 461 | FuPluginPrivate *priv = GET_PRIVATE (plugin); |
| 462 | FuPluginHelper *helper; |
| 463 | |
Richard Hughes | ccd78a9 | 2017-01-11 16:57:41 +0000 | [diff] [blame] | 464 | g_return_if_fail (FU_IS_PLUGIN (plugin)); |
| 465 | g_return_if_fail (FU_IS_DEVICE (device)); |
| 466 | |
Richard Hughes | ae3d65f | 2016-12-16 09:38:01 +0000 | [diff] [blame] | 467 | /* waiting for add */ |
| 468 | helper = g_hash_table_lookup (priv->devices_delay, device); |
| 469 | if (helper != NULL) { |
| 470 | g_debug ("ignoring remove from delayed addition"); |
| 471 | g_source_remove (helper->timeout_id); |
Richard Hughes | d4184cf | 2016-12-21 16:05:17 +0000 | [diff] [blame] | 472 | g_hash_table_remove (priv->devices_delay, helper->device); |
Richard Hughes | ae3d65f | 2016-12-16 09:38:01 +0000 | [diff] [blame] | 473 | fu_plugin_helper_free (helper); |
| 474 | return; |
| 475 | } |
| 476 | |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 477 | g_debug ("emit removed from %s: %s", |
| 478 | fu_plugin_get_name (plugin), |
| 479 | fu_device_get_id (device)); |
| 480 | g_signal_emit (plugin, signals[SIGNAL_DEVICE_REMOVED], 0, device); |
| 481 | } |
| 482 | |
Richard Hughes | 57d1822 | 2017-01-10 16:02:59 +0000 | [diff] [blame] | 483 | /** |
| 484 | * fu_plugin_set_status: |
| 485 | * @plugin: A #FuPlugin |
| 486 | * @status: A #FwupdStatus, e.g. #FWUPD_STATUS_DECOMPRESSING |
| 487 | * |
| 488 | * Sets the global state of the daemon according to the current plugin action. |
| 489 | * |
| 490 | * Since: 0.8.0 |
| 491 | **/ |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 492 | void |
| 493 | fu_plugin_set_status (FuPlugin *plugin, FwupdStatus status) |
| 494 | { |
Richard Hughes | ccd78a9 | 2017-01-11 16:57:41 +0000 | [diff] [blame] | 495 | g_return_if_fail (FU_IS_PLUGIN (plugin)); |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 496 | g_signal_emit (plugin, signals[SIGNAL_STATUS_CHANGED], 0, status); |
| 497 | } |
| 498 | |
Richard Hughes | 57d1822 | 2017-01-10 16:02:59 +0000 | [diff] [blame] | 499 | /** |
| 500 | * fu_plugin_set_percentage: |
| 501 | * @plugin: A #FuPlugin |
| 502 | * @percentage: the percentage complete |
| 503 | * |
| 504 | * Sets the global completion of the daemon according to the current plugin |
| 505 | * action. |
| 506 | * |
| 507 | * Since: 0.8.0 |
| 508 | **/ |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 509 | void |
| 510 | fu_plugin_set_percentage (FuPlugin *plugin, guint percentage) |
| 511 | { |
Richard Hughes | ccd78a9 | 2017-01-11 16:57:41 +0000 | [diff] [blame] | 512 | g_return_if_fail (FU_IS_PLUGIN (plugin)); |
| 513 | g_return_if_fail (percentage <= 100); |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 514 | g_signal_emit (plugin, signals[SIGNAL_PERCENTAGE_CHANGED], 0, |
| 515 | percentage); |
Richard Hughes | d090514 | 2016-03-13 09:46:49 +0000 | [diff] [blame] | 516 | } |
| 517 | |
Richard Hughes | 362d6d7 | 2017-01-07 21:42:14 +0000 | [diff] [blame] | 518 | /** |
| 519 | * fu_plugin_recoldplug: |
| 520 | * @plugin: A #FuPlugin |
| 521 | * |
| 522 | * Ask all the plugins to coldplug all devices, which will include the prepare() |
| 523 | * and cleanup() phases. Duplicate devices added will be ignored. |
| 524 | * |
| 525 | * Since: 0.8.0 |
| 526 | **/ |
| 527 | void |
| 528 | fu_plugin_recoldplug (FuPlugin *plugin) |
| 529 | { |
Richard Hughes | ccd78a9 | 2017-01-11 16:57:41 +0000 | [diff] [blame] | 530 | g_return_if_fail (FU_IS_PLUGIN (plugin)); |
Richard Hughes | 362d6d7 | 2017-01-07 21:42:14 +0000 | [diff] [blame] | 531 | g_signal_emit (plugin, signals[SIGNAL_RECOLDPLUG], 0); |
| 532 | } |
| 533 | |
Richard Hughes | b082903 | 2017-01-10 09:27:08 +0000 | [diff] [blame] | 534 | /** |
Richard Hughes | b8f8db2 | 2017-04-25 15:56:00 +0100 | [diff] [blame] | 535 | * fu_plugin_check_hwid: |
| 536 | * @plugin: A #FuPlugin |
Richard Hughes | 4eada34 | 2017-10-03 21:20:32 +0100 | [diff] [blame] | 537 | * @hwid: A Hardware ID GUID, e.g. `6de5d951-d755-576b-bd09-c5cf66b27234` |
Richard Hughes | b8f8db2 | 2017-04-25 15:56:00 +0100 | [diff] [blame] | 538 | * |
Richard Hughes | d7704d4 | 2017-08-08 20:29:09 +0100 | [diff] [blame] | 539 | * Checks to see if a specific GUID exists. All hardware IDs on a |
Richard Hughes | b8f8db2 | 2017-04-25 15:56:00 +0100 | [diff] [blame] | 540 | * specific system can be shown using the `fwupdmgr hwids` command. |
| 541 | * |
Richard Hughes | 4eada34 | 2017-10-03 21:20:32 +0100 | [diff] [blame] | 542 | * Returns: %TRUE if the HwId is found on the system. |
| 543 | * |
Richard Hughes | b8f8db2 | 2017-04-25 15:56:00 +0100 | [diff] [blame] | 544 | * Since: 0.9.1 |
| 545 | **/ |
| 546 | gboolean |
| 547 | fu_plugin_check_hwid (FuPlugin *plugin, const gchar *hwid) |
| 548 | { |
| 549 | FuPluginPrivate *priv = GET_PRIVATE (plugin); |
| 550 | if (priv->hwids == NULL) |
| 551 | return FALSE; |
Richard Hughes | d7704d4 | 2017-08-08 20:29:09 +0100 | [diff] [blame] | 552 | return fu_hwids_has_guid (priv->hwids, hwid); |
| 553 | } |
| 554 | |
| 555 | /** |
Richard Hughes | 1354ea9 | 2017-09-19 15:58:31 +0100 | [diff] [blame] | 556 | * fu_plugin_check_supported: |
| 557 | * @plugin: A #FuPlugin |
Richard Hughes | 4eada34 | 2017-10-03 21:20:32 +0100 | [diff] [blame] | 558 | * @guid: A Hardware ID GUID, e.g. `6de5d951-d755-576b-bd09-c5cf66b27234` |
Richard Hughes | 1354ea9 | 2017-09-19 15:58:31 +0100 | [diff] [blame] | 559 | * |
| 560 | * Checks to see if a specific device GUID is supported, i.e. available in the |
| 561 | * AppStream metadata. |
| 562 | * |
Richard Hughes | 4eada34 | 2017-10-03 21:20:32 +0100 | [diff] [blame] | 563 | * Returns: %TRUE if the device is supported. |
| 564 | * |
Richard Hughes | 1354ea9 | 2017-09-19 15:58:31 +0100 | [diff] [blame] | 565 | * Since: 1.0.0 |
| 566 | **/ |
| 567 | gboolean |
| 568 | fu_plugin_check_supported (FuPlugin *plugin, const gchar *guid) |
| 569 | { |
| 570 | FuPluginPrivate *priv = GET_PRIVATE (plugin); |
| 571 | if (priv->supported_guids == NULL) |
| 572 | return FALSE; |
| 573 | for (guint i = 0; i < priv->supported_guids->len; i++) { |
| 574 | const gchar *guid_tmp = g_ptr_array_index (priv->supported_guids, i); |
| 575 | if (g_strcmp0 (guid, guid_tmp) == 0) |
| 576 | return TRUE; |
| 577 | } |
| 578 | return FALSE; |
| 579 | } |
| 580 | |
| 581 | /** |
Richard Hughes | d7704d4 | 2017-08-08 20:29:09 +0100 | [diff] [blame] | 582 | * fu_plugin_get_dmi_value: |
| 583 | * @plugin: A #FuPlugin |
Richard Hughes | 4eada34 | 2017-10-03 21:20:32 +0100 | [diff] [blame] | 584 | * @dmi_id: A DMI ID, e.g. `BiosVersion` |
Richard Hughes | d7704d4 | 2017-08-08 20:29:09 +0100 | [diff] [blame] | 585 | * |
| 586 | * Gets a hardware DMI value. |
| 587 | * |
Richard Hughes | 4eada34 | 2017-10-03 21:20:32 +0100 | [diff] [blame] | 588 | * Returns: The string, or %NULL |
| 589 | * |
Richard Hughes | d7704d4 | 2017-08-08 20:29:09 +0100 | [diff] [blame] | 590 | * Since: 0.9.7 |
| 591 | **/ |
| 592 | const gchar * |
| 593 | fu_plugin_get_dmi_value (FuPlugin *plugin, const gchar *dmi_id) |
| 594 | { |
| 595 | FuPluginPrivate *priv = GET_PRIVATE (plugin); |
| 596 | if (priv->hwids == NULL) |
Richard Hughes | 7ef96b8 | 2017-08-23 18:28:24 +0100 | [diff] [blame] | 597 | return NULL; |
Richard Hughes | d7704d4 | 2017-08-08 20:29:09 +0100 | [diff] [blame] | 598 | return fu_hwids_get_value (priv->hwids, dmi_id); |
Richard Hughes | b8f8db2 | 2017-04-25 15:56:00 +0100 | [diff] [blame] | 599 | } |
| 600 | |
Richard Hughes | 49e5e05 | 2017-09-03 12:15:41 +0100 | [diff] [blame] | 601 | /** |
| 602 | * fu_plugin_get_smbios_string: |
| 603 | * @plugin: A #FuPlugin |
| 604 | * @structure_type: A SMBIOS structure type, e.g. %FU_SMBIOS_STRUCTURE_TYPE_BIOS |
| 605 | * @offset: A SMBIOS offset |
| 606 | * |
| 607 | * Gets a hardware SMBIOS string. |
| 608 | * |
| 609 | * The @type and @offset can be referenced from the DMTF SMBIOS specification: |
| 610 | * https://www.dmtf.org/sites/default/files/standards/documents/DSP0134_3.1.1.pdf |
| 611 | * |
Richard Hughes | 4eada34 | 2017-10-03 21:20:32 +0100 | [diff] [blame] | 612 | * Returns: A string, or %NULL |
| 613 | * |
Richard Hughes | 49e5e05 | 2017-09-03 12:15:41 +0100 | [diff] [blame] | 614 | * Since: 0.9.8 |
| 615 | **/ |
| 616 | const gchar * |
| 617 | fu_plugin_get_smbios_string (FuPlugin *plugin, guint8 structure_type, guint8 offset) |
| 618 | { |
| 619 | FuPluginPrivate *priv = GET_PRIVATE (plugin); |
| 620 | if (priv->smbios == NULL) |
| 621 | return NULL; |
| 622 | return fu_smbios_get_string (priv->smbios, structure_type, offset, NULL); |
| 623 | } |
| 624 | |
| 625 | /** |
| 626 | * fu_plugin_get_smbios_data: |
| 627 | * @plugin: A #FuPlugin |
| 628 | * @structure_type: A SMBIOS structure type, e.g. %FU_SMBIOS_STRUCTURE_TYPE_BIOS |
| 629 | * |
| 630 | * Gets a hardware SMBIOS data. |
| 631 | * |
Richard Hughes | 4eada34 | 2017-10-03 21:20:32 +0100 | [diff] [blame] | 632 | * Returns: (transfer none): A #GBytes, or %NULL |
| 633 | * |
Richard Hughes | 49e5e05 | 2017-09-03 12:15:41 +0100 | [diff] [blame] | 634 | * Since: 0.9.8 |
| 635 | **/ |
| 636 | GBytes * |
| 637 | fu_plugin_get_smbios_data (FuPlugin *plugin, guint8 structure_type) |
| 638 | { |
| 639 | FuPluginPrivate *priv = GET_PRIVATE (plugin); |
| 640 | if (priv->smbios == NULL) |
| 641 | return NULL; |
| 642 | return fu_smbios_get_data (priv->smbios, structure_type, NULL); |
| 643 | } |
| 644 | |
Richard Hughes | b8f8db2 | 2017-04-25 15:56:00 +0100 | [diff] [blame] | 645 | void |
Richard Hughes | d7704d4 | 2017-08-08 20:29:09 +0100 | [diff] [blame] | 646 | fu_plugin_set_hwids (FuPlugin *plugin, FuHwids *hwids) |
Richard Hughes | b8f8db2 | 2017-04-25 15:56:00 +0100 | [diff] [blame] | 647 | { |
| 648 | FuPluginPrivate *priv = GET_PRIVATE (plugin); |
Richard Hughes | d7704d4 | 2017-08-08 20:29:09 +0100 | [diff] [blame] | 649 | g_set_object (&priv->hwids, hwids); |
Richard Hughes | b8f8db2 | 2017-04-25 15:56:00 +0100 | [diff] [blame] | 650 | } |
| 651 | |
Richard Hughes | 49e5e05 | 2017-09-03 12:15:41 +0100 | [diff] [blame] | 652 | void |
Richard Hughes | 1354ea9 | 2017-09-19 15:58:31 +0100 | [diff] [blame] | 653 | fu_plugin_set_supported (FuPlugin *plugin, GPtrArray *supported_guids) |
| 654 | { |
| 655 | FuPluginPrivate *priv = GET_PRIVATE (plugin); |
| 656 | if (priv->supported_guids != NULL) |
| 657 | g_ptr_array_unref (priv->supported_guids); |
| 658 | priv->supported_guids = g_ptr_array_ref (supported_guids); |
| 659 | } |
| 660 | |
Richard Hughes | 9c028f0 | 2017-10-28 21:14:28 +0100 | [diff] [blame] | 661 | void |
| 662 | fu_plugin_set_quirks (FuPlugin *plugin, FuQuirks *quirks) |
| 663 | { |
| 664 | FuPluginPrivate *priv = GET_PRIVATE (plugin); |
| 665 | g_set_object (&priv->quirks, quirks); |
| 666 | } |
| 667 | |
| 668 | /** |
| 669 | * fu_plugin_get_quirks: |
| 670 | * @plugin: A #FuPlugin |
| 671 | * |
| 672 | * Returns the hardware database object. This can be used to discover device |
| 673 | * quirks or other device-specific settings. |
| 674 | * |
| 675 | * Returns: (transfer none): a #FuQuirks, or %NULL if not set |
| 676 | * |
| 677 | * Since: 1.0.1 |
| 678 | **/ |
| 679 | FuQuirks * |
| 680 | fu_plugin_get_quirks (FuPlugin *plugin) |
| 681 | { |
| 682 | FuPluginPrivate *priv = GET_PRIVATE (plugin); |
| 683 | return priv->quirks; |
| 684 | } |
| 685 | |
| 686 | /** |
| 687 | * fu_plugin_lookup_quirk_by_id: |
| 688 | * @plugin: A #FuPlugin |
| 689 | * @prefix: A string prefix that matches the quirks file basename, e.g. "dfu-quirks" |
| 690 | * @id: An ID to match the entry, e.g. "012345" |
| 691 | * |
| 692 | * Looks up an entry in the hardware database using a string value. |
| 693 | * |
| 694 | * Returns: (transfer none): values from the database, or %NULL if not found |
| 695 | * |
| 696 | * Since: 1.0.1 |
| 697 | **/ |
| 698 | const gchar * |
| 699 | fu_plugin_lookup_quirk_by_id (FuPlugin *plugin, const gchar *prefix, const gchar *id) |
| 700 | { |
| 701 | FuPluginPrivate *priv = GET_PRIVATE (plugin); |
| 702 | g_return_val_if_fail (FU_IS_PLUGIN (plugin), NULL); |
| 703 | |
| 704 | /* wildcard */ |
| 705 | if (g_strstr_len (id, -1, "*") != NULL) |
| 706 | return fu_quirks_lookup_by_glob (priv->quirks, prefix, id); |
| 707 | |
| 708 | /* exact ID */ |
| 709 | return fu_quirks_lookup_by_id (priv->quirks, prefix, id); |
| 710 | } |
| 711 | |
| 712 | /** |
| 713 | * fu_plugin_lookup_quirk_by_usb_device: |
| 714 | * @plugin: A #FuPlugin |
| 715 | * @prefix: A string prefix that matches the quirks file basename, e.g. "dfu-quirks" |
| 716 | * @dev: A #GUsbDevice |
| 717 | * |
| 718 | * Looks up an entry in the hardware database using various keys generated |
| 719 | * from @dev. |
| 720 | * |
| 721 | * Returns: (transfer none): values from the database, or %NULL if not found |
| 722 | * |
| 723 | * Since: 1.0.1 |
| 724 | **/ |
| 725 | const gchar * |
| 726 | fu_plugin_lookup_quirk_by_usb_device (FuPlugin *plugin, const gchar *prefix, GUsbDevice *dev) |
| 727 | { |
| 728 | FuPluginPrivate *priv = GET_PRIVATE (plugin); |
| 729 | g_return_val_if_fail (FU_IS_PLUGIN (plugin), NULL); |
| 730 | return fu_quirks_lookup_by_usb_device (priv->quirks, prefix, dev); |
| 731 | } |
| 732 | |
Richard Hughes | 1354ea9 | 2017-09-19 15:58:31 +0100 | [diff] [blame] | 733 | /** |
| 734 | * fu_plugin_get_supported: |
| 735 | * @plugin: A #FuPlugin |
| 736 | * |
| 737 | * Gets all the device GUIDs supported by the daemon. |
| 738 | * |
Richard Hughes | 4eada34 | 2017-10-03 21:20:32 +0100 | [diff] [blame] | 739 | * Returns: (element-type utf8) (transfer none): GUIDs |
| 740 | * |
Richard Hughes | 1354ea9 | 2017-09-19 15:58:31 +0100 | [diff] [blame] | 741 | * Since: 1.0.0 |
| 742 | **/ |
| 743 | GPtrArray * |
| 744 | fu_plugin_get_supported (FuPlugin *plugin) |
| 745 | { |
| 746 | FuPluginPrivate *priv = GET_PRIVATE (plugin); |
| 747 | return priv->supported_guids; |
| 748 | } |
| 749 | |
| 750 | void |
Richard Hughes | 49e5e05 | 2017-09-03 12:15:41 +0100 | [diff] [blame] | 751 | fu_plugin_set_smbios (FuPlugin *plugin, FuSmbios *smbios) |
| 752 | { |
| 753 | FuPluginPrivate *priv = GET_PRIVATE (plugin); |
| 754 | g_set_object (&priv->smbios, smbios); |
| 755 | } |
| 756 | |
Richard Hughes | b8f8db2 | 2017-04-25 15:56:00 +0100 | [diff] [blame] | 757 | /** |
Richard Hughes | b082903 | 2017-01-10 09:27:08 +0000 | [diff] [blame] | 758 | * fu_plugin_set_coldplug_delay: |
| 759 | * @plugin: A #FuPlugin |
| 760 | * @duration: A delay in milliseconds |
| 761 | * |
| 762 | * Set the minimum time that should be waited inbetween the call to |
| 763 | * fu_plugin_coldplug_prepare() and fu_plugin_coldplug(). This is usually going |
| 764 | * to be the minimum hardware initialisation time from a datasheet. |
| 765 | * |
| 766 | * It is better to use this function rather than using a sleep() in the plugin |
| 767 | * itself as then only one delay is done in the daemon rather than waiting for |
| 768 | * each coldplug prepare in a serial way. |
| 769 | * |
| 770 | * Additionally, very long delays should be avoided as the daemon will be |
| 771 | * blocked from processing requests whilst the coldplug delay is being |
| 772 | * performed. |
| 773 | * |
| 774 | * Since: 0.8.0 |
| 775 | **/ |
| 776 | void |
| 777 | fu_plugin_set_coldplug_delay (FuPlugin *plugin, guint duration) |
| 778 | { |
| 779 | g_return_if_fail (FU_IS_PLUGIN (plugin)); |
| 780 | g_return_if_fail (duration > 0); |
| 781 | |
| 782 | /* check sanity */ |
| 783 | if (duration > FU_PLUGIN_COLDPLUG_DELAY_MAXIMUM) { |
| 784 | g_warning ("duration of %ums is crazy, truncating to %ums", |
| 785 | duration, |
| 786 | FU_PLUGIN_COLDPLUG_DELAY_MAXIMUM); |
| 787 | duration = FU_PLUGIN_COLDPLUG_DELAY_MAXIMUM; |
| 788 | } |
| 789 | |
| 790 | /* emit */ |
| 791 | g_signal_emit (plugin, signals[SIGNAL_SET_COLDPLUG_DELAY], 0, duration); |
| 792 | } |
| 793 | |
Richard Hughes | d090514 | 2016-03-13 09:46:49 +0000 | [diff] [blame] | 794 | gboolean |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 795 | fu_plugin_runner_startup (FuPlugin *plugin, GError **error) |
Richard Hughes | d090514 | 2016-03-13 09:46:49 +0000 | [diff] [blame] | 796 | { |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 797 | FuPluginPrivate *priv = GET_PRIVATE (plugin); |
Richard Hughes | 7b8b202 | 2016-12-12 16:15:03 +0000 | [diff] [blame] | 798 | FuPluginStartupFunc func = NULL; |
Richard Hughes | d090514 | 2016-03-13 09:46:49 +0000 | [diff] [blame] | 799 | |
| 800 | /* not enabled */ |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 801 | if (!priv->enabled) |
Richard Hughes | d090514 | 2016-03-13 09:46:49 +0000 | [diff] [blame] | 802 | return TRUE; |
| 803 | |
| 804 | /* optional */ |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 805 | g_module_symbol (priv->module, "fu_plugin_startup", (gpointer *) &func); |
| 806 | if (func == NULL) |
Richard Hughes | d090514 | 2016-03-13 09:46:49 +0000 | [diff] [blame] | 807 | return TRUE; |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 808 | g_debug ("performing startup() on %s", priv->name); |
Richard Hughes | d090514 | 2016-03-13 09:46:49 +0000 | [diff] [blame] | 809 | if (!func (plugin, error)) { |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 810 | g_prefix_error (error, "failed to startup %s: ", priv->name); |
| 811 | return FALSE; |
| 812 | } |
| 813 | return TRUE; |
| 814 | } |
| 815 | |
| 816 | static gboolean |
| 817 | fu_plugin_runner_offline_invalidate (GError **error) |
| 818 | { |
| 819 | g_autoptr(GError) error_local = NULL; |
| 820 | g_autoptr(GFile) file1 = NULL; |
| 821 | |
| 822 | g_return_val_if_fail (error == NULL || *error == NULL, FALSE); |
| 823 | |
| 824 | file1 = g_file_new_for_path (FU_OFFLINE_TRIGGER_FILENAME); |
| 825 | if (!g_file_query_exists (file1, NULL)) |
| 826 | return TRUE; |
| 827 | if (!g_file_delete (file1, NULL, &error_local)) { |
| 828 | g_set_error (error, |
| 829 | FWUPD_ERROR, |
| 830 | FWUPD_ERROR_INTERNAL, |
| 831 | "Cannot delete %s: %s", |
| 832 | FU_OFFLINE_TRIGGER_FILENAME, |
| 833 | error_local->message); |
| 834 | return FALSE; |
| 835 | } |
| 836 | return TRUE; |
| 837 | } |
| 838 | |
| 839 | static gboolean |
| 840 | fu_plugin_runner_offline_setup (GError **error) |
| 841 | { |
| 842 | gint rc; |
| 843 | |
| 844 | g_return_val_if_fail (error == NULL || *error == NULL, FALSE); |
| 845 | |
| 846 | /* create symlink for the systemd-system-update-generator */ |
| 847 | rc = symlink ("/var/lib/fwupd", FU_OFFLINE_TRIGGER_FILENAME); |
| 848 | if (rc < 0) { |
| 849 | g_set_error (error, |
| 850 | FWUPD_ERROR, |
| 851 | FWUPD_ERROR_INTERNAL, |
| 852 | "Failed to create symlink %s to %s: %s", |
| 853 | FU_OFFLINE_TRIGGER_FILENAME, |
| 854 | "/var/lib", strerror (errno)); |
Richard Hughes | d090514 | 2016-03-13 09:46:49 +0000 | [diff] [blame] | 855 | return FALSE; |
| 856 | } |
| 857 | return TRUE; |
| 858 | } |
| 859 | |
Richard Hughes | 0d7fdb3 | 2017-11-11 20:23:14 +0000 | [diff] [blame] | 860 | static gboolean |
| 861 | fu_plugin_runner_device_generic (FuPlugin *plugin, FuDevice *device, |
| 862 | const gchar *symbol_name, GError **error) |
| 863 | { |
| 864 | FuPluginPrivate *priv = GET_PRIVATE (plugin); |
| 865 | FuPluginDeviceFunc func = NULL; |
| 866 | |
| 867 | /* not enabled */ |
| 868 | if (!priv->enabled) |
| 869 | return TRUE; |
| 870 | |
Richard Hughes | d3d96cc | 2017-11-14 11:34:33 +0000 | [diff] [blame^] | 871 | /* no object loaded */ |
| 872 | if (priv->module == NULL) |
| 873 | return TRUE; |
| 874 | |
Richard Hughes | 0d7fdb3 | 2017-11-11 20:23:14 +0000 | [diff] [blame] | 875 | /* optional */ |
| 876 | g_module_symbol (priv->module, symbol_name, (gpointer *) &func); |
| 877 | if (func == NULL) |
| 878 | return TRUE; |
| 879 | g_debug ("performing %s() on %s", symbol_name + 10, priv->name); |
| 880 | if (!func (plugin, device, error)) { |
| 881 | g_prefix_error (error, "failed to run %s(%s) on %s: ", |
| 882 | symbol_name + 10, |
| 883 | fu_device_get_id (device), |
| 884 | priv->name); |
| 885 | return FALSE; |
| 886 | } |
| 887 | return TRUE; |
| 888 | } |
| 889 | |
Richard Hughes | d090514 | 2016-03-13 09:46:49 +0000 | [diff] [blame] | 890 | gboolean |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 891 | fu_plugin_runner_coldplug (FuPlugin *plugin, GError **error) |
Richard Hughes | d090514 | 2016-03-13 09:46:49 +0000 | [diff] [blame] | 892 | { |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 893 | FuPluginPrivate *priv = GET_PRIVATE (plugin); |
Richard Hughes | 7b8b202 | 2016-12-12 16:15:03 +0000 | [diff] [blame] | 894 | FuPluginStartupFunc func = NULL; |
Richard Hughes | d090514 | 2016-03-13 09:46:49 +0000 | [diff] [blame] | 895 | |
| 896 | /* not enabled */ |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 897 | if (!priv->enabled) |
Richard Hughes | d090514 | 2016-03-13 09:46:49 +0000 | [diff] [blame] | 898 | return TRUE; |
| 899 | |
| 900 | /* optional */ |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 901 | g_module_symbol (priv->module, "fu_plugin_coldplug", (gpointer *) &func); |
| 902 | if (func == NULL) |
Richard Hughes | d090514 | 2016-03-13 09:46:49 +0000 | [diff] [blame] | 903 | return TRUE; |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 904 | g_debug ("performing coldplug() on %s", priv->name); |
| 905 | if (!func (plugin, error)) { |
| 906 | g_prefix_error (error, "failed to coldplug %s: ", priv->name); |
| 907 | return FALSE; |
| 908 | } |
| 909 | return TRUE; |
| 910 | } |
| 911 | |
Richard Hughes | 7b8b202 | 2016-12-12 16:15:03 +0000 | [diff] [blame] | 912 | gboolean |
Richard Hughes | 46487c9 | 2017-01-07 21:26:34 +0000 | [diff] [blame] | 913 | fu_plugin_runner_coldplug_prepare (FuPlugin *plugin, GError **error) |
| 914 | { |
| 915 | FuPluginPrivate *priv = GET_PRIVATE (plugin); |
| 916 | FuPluginStartupFunc func = NULL; |
| 917 | |
| 918 | /* not enabled */ |
| 919 | if (!priv->enabled) |
| 920 | return TRUE; |
| 921 | |
| 922 | /* optional */ |
| 923 | g_module_symbol (priv->module, "fu_plugin_coldplug_prepare", (gpointer *) &func); |
| 924 | if (func == NULL) |
| 925 | return TRUE; |
| 926 | g_debug ("performing coldplug_prepare() on %s", priv->name); |
| 927 | if (!func (plugin, error)) { |
| 928 | g_prefix_error (error, "failed to prepare for coldplug %s: ", priv->name); |
| 929 | return FALSE; |
| 930 | } |
| 931 | return TRUE; |
| 932 | } |
| 933 | |
| 934 | gboolean |
| 935 | fu_plugin_runner_coldplug_cleanup (FuPlugin *plugin, GError **error) |
| 936 | { |
| 937 | FuPluginPrivate *priv = GET_PRIVATE (plugin); |
| 938 | FuPluginStartupFunc func = NULL; |
| 939 | |
| 940 | /* not enabled */ |
| 941 | if (!priv->enabled) |
| 942 | return TRUE; |
| 943 | |
| 944 | /* optional */ |
| 945 | g_module_symbol (priv->module, "fu_plugin_coldplug_cleanup", (gpointer *) &func); |
| 946 | if (func == NULL) |
| 947 | return TRUE; |
| 948 | g_debug ("performing coldplug_cleanup() on %s", priv->name); |
| 949 | if (!func (plugin, error)) { |
| 950 | g_prefix_error (error, "failed to cleanup coldplug %s: ", priv->name); |
| 951 | return FALSE; |
| 952 | } |
| 953 | return TRUE; |
| 954 | } |
| 955 | |
| 956 | gboolean |
Richard Hughes | 7b8b202 | 2016-12-12 16:15:03 +0000 | [diff] [blame] | 957 | fu_plugin_runner_update_prepare (FuPlugin *plugin, FuDevice *device, GError **error) |
| 958 | { |
Richard Hughes | 0d7fdb3 | 2017-11-11 20:23:14 +0000 | [diff] [blame] | 959 | return fu_plugin_runner_device_generic (plugin, device, |
| 960 | "fu_plugin_update_prepare", error); |
Richard Hughes | 7b8b202 | 2016-12-12 16:15:03 +0000 | [diff] [blame] | 961 | } |
| 962 | |
| 963 | gboolean |
| 964 | fu_plugin_runner_update_cleanup (FuPlugin *plugin, FuDevice *device, GError **error) |
| 965 | { |
Richard Hughes | 0d7fdb3 | 2017-11-11 20:23:14 +0000 | [diff] [blame] | 966 | return fu_plugin_runner_device_generic (plugin, device, |
| 967 | "fu_plugin_update_cleanup", error); |
| 968 | } |
Richard Hughes | 7b8b202 | 2016-12-12 16:15:03 +0000 | [diff] [blame] | 969 | |
Richard Hughes | 0d7fdb3 | 2017-11-11 20:23:14 +0000 | [diff] [blame] | 970 | gboolean |
| 971 | fu_plugin_runner_update_attach (FuPlugin *plugin, FuDevice *device, GError **error) |
| 972 | { |
| 973 | return fu_plugin_runner_device_generic (plugin, device, |
| 974 | "fu_plugin_update_attach", error); |
| 975 | } |
Richard Hughes | 7b8b202 | 2016-12-12 16:15:03 +0000 | [diff] [blame] | 976 | |
Richard Hughes | 0d7fdb3 | 2017-11-11 20:23:14 +0000 | [diff] [blame] | 977 | gboolean |
| 978 | fu_plugin_runner_update_detach (FuPlugin *plugin, FuDevice *device, GError **error) |
| 979 | { |
| 980 | return fu_plugin_runner_device_generic (plugin, device, |
| 981 | "fu_plugin_update_detach", error); |
| 982 | } |
| 983 | |
| 984 | gboolean |
| 985 | fu_plugin_runner_update_reload (FuPlugin *plugin, FuDevice *device, GError **error) |
| 986 | { |
| 987 | return fu_plugin_runner_device_generic (plugin, device, |
| 988 | "fu_plugin_update_reload", error); |
Richard Hughes | 7b8b202 | 2016-12-12 16:15:03 +0000 | [diff] [blame] | 989 | } |
| 990 | |
Richard Hughes | e1fd34d | 2017-08-24 14:19:51 +0100 | [diff] [blame] | 991 | void |
| 992 | fu_plugin_runner_device_register (FuPlugin *plugin, FuDevice *device) |
| 993 | { |
| 994 | FuPluginPrivate *priv = GET_PRIVATE (plugin); |
| 995 | FuPluginDeviceRegisterFunc func = NULL; |
| 996 | |
| 997 | /* not enabled */ |
| 998 | if (!priv->enabled) |
| 999 | return; |
| 1000 | |
| 1001 | /* optional */ |
| 1002 | g_module_symbol (priv->module, "fu_plugin_device_registered", (gpointer *) &func); |
| 1003 | if (func != NULL) { |
| 1004 | g_debug ("performing device_added() on %s", priv->name); |
| 1005 | func (plugin, device); |
| 1006 | } |
| 1007 | } |
| 1008 | |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 1009 | static gboolean |
| 1010 | fu_plugin_runner_schedule_update (FuPlugin *plugin, |
| 1011 | FuDevice *device, |
| 1012 | GBytes *blob_cab, |
| 1013 | GError **error) |
| 1014 | { |
| 1015 | gchar tmpname[] = {"XXXXXX.cap"}; |
| 1016 | g_autofree gchar *dirname = NULL; |
| 1017 | g_autofree gchar *filename = NULL; |
Richard Hughes | 68982c6 | 2017-09-13 15:40:14 +0100 | [diff] [blame] | 1018 | g_autoptr(FuDevice) res_tmp = NULL; |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 1019 | g_autoptr(FuPending) pending = NULL; |
| 1020 | g_autoptr(GFile) file = NULL; |
| 1021 | |
| 1022 | /* id already exists */ |
| 1023 | pending = fu_pending_new (); |
| 1024 | res_tmp = fu_pending_get_device (pending, fu_device_get_id (device), NULL); |
| 1025 | if (res_tmp != NULL) { |
| 1026 | g_set_error (error, |
| 1027 | FWUPD_ERROR, |
| 1028 | FWUPD_ERROR_ALREADY_PENDING, |
| 1029 | "%s is already scheduled to be updated", |
| 1030 | fu_device_get_id (device)); |
| 1031 | return FALSE; |
| 1032 | } |
| 1033 | |
| 1034 | /* create directory */ |
| 1035 | dirname = g_build_filename (LOCALSTATEDIR, "lib", "fwupd", NULL); |
| 1036 | file = g_file_new_for_path (dirname); |
| 1037 | if (!g_file_query_exists (file, NULL)) { |
| 1038 | if (!g_file_make_directory_with_parents (file, NULL, error)) |
| 1039 | return FALSE; |
| 1040 | } |
| 1041 | |
| 1042 | /* get a random filename */ |
| 1043 | for (guint i = 0; i < 6; i++) |
| 1044 | tmpname[i] = (gchar) g_random_int_range ('A', 'Z'); |
| 1045 | filename = g_build_filename (dirname, tmpname, NULL); |
| 1046 | |
| 1047 | /* just copy to the temp file */ |
| 1048 | fu_plugin_set_status (plugin, FWUPD_STATUS_SCHEDULING); |
| 1049 | if (!g_file_set_contents (filename, |
| 1050 | g_bytes_get_data (blob_cab, NULL), |
| 1051 | (gssize) g_bytes_get_size (blob_cab), |
| 1052 | error)) |
| 1053 | return FALSE; |
| 1054 | |
| 1055 | /* schedule for next boot */ |
| 1056 | g_debug ("schedule %s to be installed to %s on next boot", |
| 1057 | filename, fu_device_get_id (device)); |
Richard Hughes | cc3de2e | 2017-09-13 19:28:17 +0100 | [diff] [blame] | 1058 | fu_device_set_filename_pending (device, filename); |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 1059 | |
| 1060 | /* add to database */ |
Richard Hughes | 68982c6 | 2017-09-13 15:40:14 +0100 | [diff] [blame] | 1061 | if (!fu_pending_add_device (pending, device, error)) |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 1062 | return FALSE; |
| 1063 | |
| 1064 | /* next boot we run offline */ |
| 1065 | return fu_plugin_runner_offline_setup (error); |
| 1066 | } |
| 1067 | |
| 1068 | gboolean |
| 1069 | fu_plugin_runner_verify (FuPlugin *plugin, |
| 1070 | FuDevice *device, |
| 1071 | FuPluginVerifyFlags flags, |
| 1072 | GError **error) |
| 1073 | { |
| 1074 | FuPluginPrivate *priv = GET_PRIVATE (plugin); |
Richard Hughes | 7b8b202 | 2016-12-12 16:15:03 +0000 | [diff] [blame] | 1075 | FuPluginVerifyFunc func = NULL; |
Richard Hughes | ababbb7 | 2017-06-15 20:18:36 +0100 | [diff] [blame] | 1076 | GPtrArray *checksums; |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 1077 | |
| 1078 | /* not enabled */ |
| 1079 | if (!priv->enabled) |
| 1080 | return TRUE; |
| 1081 | |
Richard Hughes | ababbb7 | 2017-06-15 20:18:36 +0100 | [diff] [blame] | 1082 | /* clear any existing verification checksums */ |
| 1083 | checksums = fu_device_get_checksums (device); |
| 1084 | g_ptr_array_set_size (checksums, 0); |
| 1085 | |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 1086 | /* optional */ |
| 1087 | g_module_symbol (priv->module, "fu_plugin_verify", (gpointer *) &func); |
| 1088 | if (func == NULL) |
| 1089 | return TRUE; |
| 1090 | g_debug ("performing verify() on %s", priv->name); |
| 1091 | if (!func (plugin, device, flags, error)) { |
| 1092 | g_prefix_error (error, "failed to verify %s: ", priv->name); |
Richard Hughes | d090514 | 2016-03-13 09:46:49 +0000 | [diff] [blame] | 1093 | return FALSE; |
| 1094 | } |
| 1095 | return TRUE; |
| 1096 | } |
| 1097 | |
Richard Hughes | d090514 | 2016-03-13 09:46:49 +0000 | [diff] [blame] | 1098 | gboolean |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 1099 | fu_plugin_runner_unlock (FuPlugin *plugin, FuDevice *device, GError **error) |
Richard Hughes | d090514 | 2016-03-13 09:46:49 +0000 | [diff] [blame] | 1100 | { |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 1101 | guint64 flags; |
| 1102 | FuPluginPrivate *priv = GET_PRIVATE (plugin); |
Richard Hughes | 7b8b202 | 2016-12-12 16:15:03 +0000 | [diff] [blame] | 1103 | FuPluginDeviceFunc func = NULL; |
Richard Hughes | d090514 | 2016-03-13 09:46:49 +0000 | [diff] [blame] | 1104 | |
| 1105 | /* not enabled */ |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 1106 | if (!priv->enabled) |
| 1107 | return TRUE; |
| 1108 | |
| 1109 | /* final check */ |
| 1110 | flags = fu_device_get_flags (device); |
| 1111 | if ((flags & FWUPD_DEVICE_FLAG_LOCKED) == 0) { |
| 1112 | g_set_error (error, |
| 1113 | FWUPD_ERROR, |
| 1114 | FWUPD_ERROR_NOT_SUPPORTED, |
| 1115 | "Device %s is not locked", |
| 1116 | fu_device_get_id (device)); |
| 1117 | return FALSE; |
| 1118 | } |
| 1119 | |
| 1120 | /* optional */ |
| 1121 | g_module_symbol (priv->module, "fu_plugin_unlock", (gpointer *) &func); |
| 1122 | if (func != NULL) { |
| 1123 | g_debug ("performing unlock() on %s", priv->name); |
| 1124 | if (!func (plugin, device, error)) { |
Richard Hughes | 7b8b202 | 2016-12-12 16:15:03 +0000 | [diff] [blame] | 1125 | g_prefix_error (error, "failed to unlock %s: ", priv->name); |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 1126 | return FALSE; |
| 1127 | } |
| 1128 | } |
| 1129 | |
| 1130 | /* update with correct flags */ |
| 1131 | flags = fu_device_get_flags (device); |
| 1132 | fu_device_set_flags (device, flags &= ~FWUPD_DEVICE_FLAG_LOCKED); |
| 1133 | fu_device_set_modified (device, (guint64) g_get_real_time () / G_USEC_PER_SEC); |
| 1134 | return TRUE; |
| 1135 | } |
| 1136 | |
| 1137 | gboolean |
| 1138 | fu_plugin_runner_update (FuPlugin *plugin, |
Richard Hughes | a785a1c | 2017-08-25 16:00:58 +0100 | [diff] [blame] | 1139 | FuDevice *device, |
| 1140 | GBytes *blob_cab, |
| 1141 | GBytes *blob_fw, |
| 1142 | FwupdInstallFlags flags, |
| 1143 | GError **error) |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 1144 | { |
| 1145 | FuPluginPrivate *priv = GET_PRIVATE (plugin); |
Richard Hughes | a785a1c | 2017-08-25 16:00:58 +0100 | [diff] [blame] | 1146 | FuPluginUpdateFunc update_func; |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 1147 | g_autoptr(FuPending) pending = NULL; |
Richard Hughes | 68982c6 | 2017-09-13 15:40:14 +0100 | [diff] [blame] | 1148 | g_autoptr(FuDevice) device_pending = NULL; |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 1149 | GError *error_update = NULL; |
Richard Hughes | f556d37 | 2017-06-15 19:49:18 +0100 | [diff] [blame] | 1150 | GPtrArray *checksums; |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 1151 | |
| 1152 | /* not enabled */ |
| 1153 | if (!priv->enabled) |
Richard Hughes | d090514 | 2016-03-13 09:46:49 +0000 | [diff] [blame] | 1154 | return TRUE; |
| 1155 | |
| 1156 | /* optional */ |
Richard Hughes | a785a1c | 2017-08-25 16:00:58 +0100 | [diff] [blame] | 1157 | g_module_symbol (priv->module, "fu_plugin_update", (gpointer *) &update_func); |
| 1158 | if (update_func == NULL) { |
| 1159 | g_set_error_literal (error, |
| 1160 | FWUPD_ERROR, |
| 1161 | FWUPD_ERROR_NOT_SUPPORTED, |
| 1162 | "No update possible"); |
| 1163 | return FALSE; |
| 1164 | } |
Richard Hughes | d090514 | 2016-03-13 09:46:49 +0000 | [diff] [blame] | 1165 | |
Richard Hughes | a785a1c | 2017-08-25 16:00:58 +0100 | [diff] [blame] | 1166 | /* just schedule this for the next reboot */ |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 1167 | if (flags & FWUPD_INSTALL_FLAG_OFFLINE) { |
Richard Hughes | a785a1c | 2017-08-25 16:00:58 +0100 | [diff] [blame] | 1168 | return fu_plugin_runner_schedule_update (plugin, |
| 1169 | device, |
| 1170 | blob_cab, |
| 1171 | error); |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 1172 | } |
| 1173 | |
| 1174 | /* cancel the pending action */ |
| 1175 | if (!fu_plugin_runner_offline_invalidate (error)) |
| 1176 | return FALSE; |
| 1177 | |
| 1178 | /* online */ |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 1179 | pending = fu_pending_new (); |
Richard Hughes | 68982c6 | 2017-09-13 15:40:14 +0100 | [diff] [blame] | 1180 | device_pending = fu_pending_get_device (pending, fu_device_get_id (device), NULL); |
Richard Hughes | a785a1c | 2017-08-25 16:00:58 +0100 | [diff] [blame] | 1181 | if (!update_func (plugin, device, blob_fw, flags, &error_update)) { |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 1182 | /* save the error to the database */ |
Richard Hughes | 68982c6 | 2017-09-13 15:40:14 +0100 | [diff] [blame] | 1183 | if (device_pending != NULL) { |
| 1184 | fu_pending_set_error_msg (pending, device, |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 1185 | error_update->message, NULL); |
| 1186 | } |
| 1187 | g_propagate_error (error, error_update); |
| 1188 | return FALSE; |
| 1189 | } |
| 1190 | |
Richard Hughes | f556d37 | 2017-06-15 19:49:18 +0100 | [diff] [blame] | 1191 | /* no longer valid */ |
| 1192 | checksums = fu_device_get_checksums (device); |
| 1193 | g_ptr_array_set_size (checksums, 0); |
| 1194 | |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 1195 | /* cleanup */ |
Richard Hughes | 68982c6 | 2017-09-13 15:40:14 +0100 | [diff] [blame] | 1196 | if (device_pending != NULL) { |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 1197 | const gchar *tmp; |
| 1198 | |
| 1199 | /* update pending database */ |
Richard Hughes | 68982c6 | 2017-09-13 15:40:14 +0100 | [diff] [blame] | 1200 | fu_pending_set_state (pending, device, |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 1201 | FWUPD_UPDATE_STATE_SUCCESS, NULL); |
| 1202 | |
| 1203 | /* delete cab file */ |
Richard Hughes | cc3de2e | 2017-09-13 19:28:17 +0100 | [diff] [blame] | 1204 | tmp = fu_device_get_filename_pending (device_pending); |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 1205 | if (tmp != NULL && g_str_has_prefix (tmp, LIBEXECDIR)) { |
| 1206 | g_autoptr(GError) error_local = NULL; |
| 1207 | g_autoptr(GFile) file = NULL; |
| 1208 | file = g_file_new_for_path (tmp); |
| 1209 | if (!g_file_delete (file, NULL, &error_local)) { |
| 1210 | g_set_error (error, |
| 1211 | FWUPD_ERROR, |
| 1212 | FWUPD_ERROR_INVALID_FILE, |
| 1213 | "Failed to delete %s: %s", |
| 1214 | tmp, error_local->message); |
| 1215 | return FALSE; |
| 1216 | } |
| 1217 | } |
| 1218 | } |
Richard Hughes | d090514 | 2016-03-13 09:46:49 +0000 | [diff] [blame] | 1219 | return TRUE; |
| 1220 | } |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 1221 | |
| 1222 | gboolean |
| 1223 | fu_plugin_runner_clear_results (FuPlugin *plugin, FuDevice *device, GError **error) |
| 1224 | { |
| 1225 | FuPluginPrivate *priv = GET_PRIVATE (plugin); |
Richard Hughes | 7b8b202 | 2016-12-12 16:15:03 +0000 | [diff] [blame] | 1226 | FuPluginDeviceFunc func = NULL; |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 1227 | g_autoptr(GError) error_local = NULL; |
Richard Hughes | 68982c6 | 2017-09-13 15:40:14 +0100 | [diff] [blame] | 1228 | g_autoptr(FuDevice) device_pending = NULL; |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 1229 | g_autoptr(FuPending) pending = NULL; |
| 1230 | |
| 1231 | /* not enabled */ |
| 1232 | if (!priv->enabled) |
| 1233 | return TRUE; |
| 1234 | |
| 1235 | /* use the plugin if the vfunc is provided */ |
| 1236 | g_module_symbol (priv->module, "fu_plugin_clear_result", (gpointer *) &func); |
| 1237 | if (func != NULL) { |
| 1238 | g_debug ("performing clear_result() on %s", priv->name); |
| 1239 | if (!func (plugin, device, error)) { |
| 1240 | g_prefix_error (error, "failed to clear_result %s: ", priv->name); |
| 1241 | return FALSE; |
| 1242 | } |
| 1243 | return TRUE; |
| 1244 | } |
| 1245 | |
| 1246 | /* handled using the database */ |
| 1247 | pending = fu_pending_new (); |
Richard Hughes | 68982c6 | 2017-09-13 15:40:14 +0100 | [diff] [blame] | 1248 | device_pending = fu_pending_get_device (pending, |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 1249 | fu_device_get_id (device), |
| 1250 | &error_local); |
Richard Hughes | 68982c6 | 2017-09-13 15:40:14 +0100 | [diff] [blame] | 1251 | if (device_pending == NULL) { |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 1252 | g_set_error (error, |
| 1253 | FWUPD_ERROR, |
| 1254 | FWUPD_ERROR_INVALID_FILE, |
| 1255 | "Failed to find %s in pending database: %s", |
| 1256 | fu_device_get_id (device), |
| 1257 | error_local->message); |
| 1258 | return FALSE; |
| 1259 | } |
| 1260 | |
| 1261 | /* remove from pending database */ |
Richard Hughes | 68982c6 | 2017-09-13 15:40:14 +0100 | [diff] [blame] | 1262 | return fu_pending_remove_device (pending, device, error); |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 1263 | } |
| 1264 | |
| 1265 | gboolean |
| 1266 | fu_plugin_runner_get_results (FuPlugin *plugin, FuDevice *device, GError **error) |
| 1267 | { |
| 1268 | FuPluginPrivate *priv = GET_PRIVATE (plugin); |
Richard Hughes | 7b8b202 | 2016-12-12 16:15:03 +0000 | [diff] [blame] | 1269 | FuPluginDeviceFunc func = NULL; |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 1270 | FwupdUpdateState update_state; |
| 1271 | const gchar *tmp; |
| 1272 | g_autoptr(GError) error_local = NULL; |
Richard Hughes | 68982c6 | 2017-09-13 15:40:14 +0100 | [diff] [blame] | 1273 | g_autoptr(FuDevice) device_pending = NULL; |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 1274 | g_autoptr(FuPending) pending = NULL; |
| 1275 | |
| 1276 | /* not enabled */ |
| 1277 | if (!priv->enabled) |
| 1278 | return TRUE; |
| 1279 | |
| 1280 | /* use the plugin if the vfunc is provided */ |
| 1281 | g_module_symbol (priv->module, "fu_plugin_get_results", (gpointer *) &func); |
| 1282 | if (func != NULL) { |
| 1283 | g_debug ("performing get_results() on %s", priv->name); |
| 1284 | if (!func (plugin, device, error)) { |
| 1285 | g_prefix_error (error, "failed to get_results %s: ", priv->name); |
| 1286 | return FALSE; |
| 1287 | } |
| 1288 | return TRUE; |
| 1289 | } |
| 1290 | |
| 1291 | /* handled using the database */ |
| 1292 | pending = fu_pending_new (); |
Richard Hughes | 68982c6 | 2017-09-13 15:40:14 +0100 | [diff] [blame] | 1293 | device_pending = fu_pending_get_device (pending, |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 1294 | fu_device_get_id (device), |
| 1295 | &error_local); |
Richard Hughes | 68982c6 | 2017-09-13 15:40:14 +0100 | [diff] [blame] | 1296 | if (device_pending == NULL) { |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 1297 | g_set_error (error, |
| 1298 | FWUPD_ERROR, |
| 1299 | FWUPD_ERROR_NOTHING_TO_DO, |
| 1300 | "Failed to find %s in pending database: %s", |
| 1301 | fu_device_get_id (device), |
| 1302 | error_local->message); |
| 1303 | return FALSE; |
| 1304 | } |
| 1305 | |
| 1306 | /* copy the important parts from the pending device to the real one */ |
Richard Hughes | cc3de2e | 2017-09-13 19:28:17 +0100 | [diff] [blame] | 1307 | update_state = fu_device_get_update_state (device_pending); |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 1308 | if (update_state == FWUPD_UPDATE_STATE_UNKNOWN || |
| 1309 | update_state == FWUPD_UPDATE_STATE_PENDING) { |
| 1310 | g_set_error (error, |
| 1311 | FWUPD_ERROR, |
| 1312 | FWUPD_ERROR_NOTHING_TO_DO, |
| 1313 | "Device %s has not been updated offline yet", |
| 1314 | fu_device_get_id (device)); |
| 1315 | return FALSE; |
| 1316 | } |
| 1317 | |
| 1318 | /* copy */ |
| 1319 | fu_device_set_update_state (device, update_state); |
Richard Hughes | cc3de2e | 2017-09-13 19:28:17 +0100 | [diff] [blame] | 1320 | tmp = fu_device_get_update_error (device_pending); |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 1321 | if (tmp != NULL) |
| 1322 | fu_device_set_update_error (device, tmp); |
Richard Hughes | cc3de2e | 2017-09-13 19:28:17 +0100 | [diff] [blame] | 1323 | tmp = fu_device_get_version (device_pending); |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 1324 | if (tmp != NULL) |
| 1325 | fu_device_set_version (device, tmp); |
Richard Hughes | cc3de2e | 2017-09-13 19:28:17 +0100 | [diff] [blame] | 1326 | tmp = fu_device_get_version_new (device_pending); |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 1327 | if (tmp != NULL) |
Richard Hughes | cc3de2e | 2017-09-13 19:28:17 +0100 | [diff] [blame] | 1328 | fu_device_set_version_new (device, tmp); |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 1329 | return TRUE; |
| 1330 | } |
| 1331 | |
Richard Hughes | 08a3799 | 2017-09-12 12:57:43 +0100 | [diff] [blame] | 1332 | /** |
| 1333 | * fu_plugin_get_order: |
| 1334 | * @plugin: a #FuPlugin |
| 1335 | * |
| 1336 | * Gets the plugin order, where higher numbers are run after lower |
| 1337 | * numbers. |
| 1338 | * |
| 1339 | * Returns: the integer value |
| 1340 | **/ |
| 1341 | guint |
| 1342 | fu_plugin_get_order (FuPlugin *plugin) |
| 1343 | { |
| 1344 | FuPluginPrivate *priv = fu_plugin_get_instance_private (plugin); |
| 1345 | return priv->order; |
| 1346 | } |
| 1347 | |
| 1348 | /** |
| 1349 | * fu_plugin_set_order: |
| 1350 | * @plugin: a #FuPlugin |
| 1351 | * @order: a integer value |
| 1352 | * |
| 1353 | * Sets the plugin order, where higher numbers are run after lower |
| 1354 | * numbers. |
| 1355 | **/ |
| 1356 | void |
| 1357 | fu_plugin_set_order (FuPlugin *plugin, guint order) |
| 1358 | { |
| 1359 | FuPluginPrivate *priv = fu_plugin_get_instance_private (plugin); |
| 1360 | priv->order = order; |
| 1361 | } |
| 1362 | |
| 1363 | /** |
| 1364 | * fu_plugin_add_rule: |
| 1365 | * @plugin: a #FuPlugin |
| 1366 | * @rule: a #FuPluginRule, e.g. %FU_PLUGIN_RULE_CONFLICTS |
Richard Hughes | 4eada34 | 2017-10-03 21:20:32 +0100 | [diff] [blame] | 1367 | * @name: a plugin name, e.g. `upower` |
Richard Hughes | 08a3799 | 2017-09-12 12:57:43 +0100 | [diff] [blame] | 1368 | * |
| 1369 | * If the plugin name is found, the rule will be used to sort the plugin list, |
| 1370 | * for example the plugin specified by @name will be ordered after this plugin |
| 1371 | * when %FU_PLUGIN_RULE_RUN_AFTER is used. |
| 1372 | * |
| 1373 | * NOTE: The depsolver is iterative and may not solve overly-complicated rules; |
| 1374 | * If depsolving fails then fwupd will not start. |
| 1375 | **/ |
| 1376 | void |
| 1377 | fu_plugin_add_rule (FuPlugin *plugin, FuPluginRule rule, const gchar *name) |
| 1378 | { |
| 1379 | FuPluginPrivate *priv = fu_plugin_get_instance_private (plugin); |
| 1380 | g_ptr_array_add (priv->rules[rule], g_strdup (name)); |
| 1381 | } |
| 1382 | |
| 1383 | /** |
| 1384 | * fu_plugin_get_rules: |
| 1385 | * @plugin: a #FuPlugin |
| 1386 | * @rule: a #FuPluginRule, e.g. %FU_PLUGIN_RULE_CONFLICTS |
| 1387 | * |
| 1388 | * Gets the plugin IDs that should be run after this plugin. |
| 1389 | * |
| 1390 | * Returns: (element-type utf8) (transfer none): the list of plugin names, e.g. ['appstream'] |
| 1391 | **/ |
| 1392 | GPtrArray * |
| 1393 | fu_plugin_get_rules (FuPlugin *plugin, FuPluginRule rule) |
| 1394 | { |
| 1395 | FuPluginPrivate *priv = fu_plugin_get_instance_private (plugin); |
| 1396 | return priv->rules[rule]; |
| 1397 | } |
| 1398 | |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 1399 | static void |
| 1400 | fu_plugin_class_init (FuPluginClass *klass) |
| 1401 | { |
| 1402 | GObjectClass *object_class = G_OBJECT_CLASS (klass); |
| 1403 | object_class->finalize = fu_plugin_finalize; |
| 1404 | signals[SIGNAL_DEVICE_ADDED] = |
| 1405 | g_signal_new ("device-added", |
| 1406 | G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST, |
| 1407 | G_STRUCT_OFFSET (FuPluginClass, device_added), |
| 1408 | NULL, NULL, g_cclosure_marshal_VOID__OBJECT, |
| 1409 | G_TYPE_NONE, 1, FU_TYPE_DEVICE); |
| 1410 | signals[SIGNAL_DEVICE_REMOVED] = |
| 1411 | g_signal_new ("device-removed", |
| 1412 | G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST, |
| 1413 | G_STRUCT_OFFSET (FuPluginClass, device_removed), |
| 1414 | NULL, NULL, g_cclosure_marshal_VOID__OBJECT, |
| 1415 | G_TYPE_NONE, 1, FU_TYPE_DEVICE); |
Richard Hughes | e1fd34d | 2017-08-24 14:19:51 +0100 | [diff] [blame] | 1416 | signals[SIGNAL_DEVICE_REGISTER] = |
| 1417 | g_signal_new ("device-register", |
| 1418 | G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST, |
| 1419 | G_STRUCT_OFFSET (FuPluginClass, device_register), |
| 1420 | NULL, NULL, g_cclosure_marshal_VOID__OBJECT, |
| 1421 | G_TYPE_NONE, 1, FU_TYPE_DEVICE); |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 1422 | signals[SIGNAL_STATUS_CHANGED] = |
| 1423 | g_signal_new ("status-changed", |
| 1424 | G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST, |
| 1425 | G_STRUCT_OFFSET (FuPluginClass, status_changed), |
| 1426 | NULL, NULL, g_cclosure_marshal_VOID__UINT, |
| 1427 | G_TYPE_NONE, 1, G_TYPE_UINT); |
| 1428 | signals[SIGNAL_PERCENTAGE_CHANGED] = |
| 1429 | g_signal_new ("percentage-changed", |
| 1430 | G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST, |
| 1431 | G_STRUCT_OFFSET (FuPluginClass, percentage_changed), |
| 1432 | NULL, NULL, g_cclosure_marshal_VOID__UINT, |
| 1433 | G_TYPE_NONE, 1, G_TYPE_UINT); |
Richard Hughes | 362d6d7 | 2017-01-07 21:42:14 +0000 | [diff] [blame] | 1434 | signals[SIGNAL_RECOLDPLUG] = |
| 1435 | g_signal_new ("recoldplug", |
| 1436 | G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST, |
| 1437 | G_STRUCT_OFFSET (FuPluginClass, recoldplug), |
| 1438 | NULL, NULL, g_cclosure_marshal_VOID__VOID, |
| 1439 | G_TYPE_NONE, 0); |
Richard Hughes | b082903 | 2017-01-10 09:27:08 +0000 | [diff] [blame] | 1440 | signals[SIGNAL_SET_COLDPLUG_DELAY] = |
| 1441 | g_signal_new ("set-coldplug-delay", |
| 1442 | G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST, |
| 1443 | G_STRUCT_OFFSET (FuPluginClass, set_coldplug_delay), |
| 1444 | NULL, NULL, g_cclosure_marshal_VOID__UINT, |
| 1445 | G_TYPE_NONE, 1, G_TYPE_UINT); |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 1446 | } |
| 1447 | |
| 1448 | static void |
| 1449 | fu_plugin_init (FuPlugin *plugin) |
| 1450 | { |
| 1451 | FuPluginPrivate *priv = GET_PRIVATE (plugin); |
| 1452 | priv->enabled = TRUE; |
| 1453 | priv->devices = g_hash_table_new_full (g_str_hash, g_str_equal, |
| 1454 | g_free, (GDestroyNotify) g_object_unref); |
Richard Hughes | e8b5db6 | 2017-07-17 14:18:22 +0100 | [diff] [blame] | 1455 | priv->devices_delay = g_hash_table_new (g_direct_hash, g_direct_equal); |
Richard Hughes | 08a3799 | 2017-09-12 12:57:43 +0100 | [diff] [blame] | 1456 | for (guint i = 0; i < FU_PLUGIN_RULE_LAST; i++) |
| 1457 | priv->rules[i] = g_ptr_array_new_with_free_func (g_free); |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 1458 | } |
| 1459 | |
| 1460 | static void |
| 1461 | fu_plugin_finalize (GObject *object) |
| 1462 | { |
| 1463 | FuPlugin *plugin = FU_PLUGIN (object); |
| 1464 | FuPluginPrivate *priv = GET_PRIVATE (plugin); |
| 1465 | FuPluginInitFunc func = NULL; |
| 1466 | |
| 1467 | /* optional */ |
| 1468 | if (priv->module != NULL) { |
| 1469 | g_module_symbol (priv->module, "fu_plugin_destroy", (gpointer *) &func); |
| 1470 | if (func != NULL) { |
| 1471 | g_debug ("performing destroy() on %s", priv->name); |
| 1472 | func (plugin); |
| 1473 | } |
| 1474 | } |
| 1475 | |
Richard Hughes | 08a3799 | 2017-09-12 12:57:43 +0100 | [diff] [blame] | 1476 | for (guint i = 0; i < FU_PLUGIN_RULE_LAST; i++) |
| 1477 | g_ptr_array_unref (priv->rules[i]); |
| 1478 | |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 1479 | if (priv->usb_ctx != NULL) |
| 1480 | g_object_unref (priv->usb_ctx); |
Richard Hughes | b8f8db2 | 2017-04-25 15:56:00 +0100 | [diff] [blame] | 1481 | if (priv->hwids != NULL) |
Richard Hughes | d7704d4 | 2017-08-08 20:29:09 +0100 | [diff] [blame] | 1482 | g_object_unref (priv->hwids); |
Richard Hughes | 9c028f0 | 2017-10-28 21:14:28 +0100 | [diff] [blame] | 1483 | if (priv->quirks != NULL) |
| 1484 | g_object_unref (priv->quirks); |
Richard Hughes | 1354ea9 | 2017-09-19 15:58:31 +0100 | [diff] [blame] | 1485 | if (priv->supported_guids != NULL) |
| 1486 | g_ptr_array_unref (priv->supported_guids); |
Richard Hughes | 49e5e05 | 2017-09-03 12:15:41 +0100 | [diff] [blame] | 1487 | if (priv->smbios != NULL) |
| 1488 | g_object_unref (priv->smbios); |
Richard Hughes | 576c012 | 2017-02-24 09:47:00 +0000 | [diff] [blame] | 1489 | #ifndef RUNNING_ON_VALGRIND |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 1490 | if (priv->module != NULL) |
| 1491 | g_module_close (priv->module); |
Richard Hughes | 576c012 | 2017-02-24 09:47:00 +0000 | [diff] [blame] | 1492 | #endif |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 1493 | g_hash_table_unref (priv->devices); |
Richard Hughes | ae3d65f | 2016-12-16 09:38:01 +0000 | [diff] [blame] | 1494 | g_hash_table_unref (priv->devices_delay); |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 1495 | g_free (priv->name); |
| 1496 | g_free (priv->data); |
| 1497 | |
| 1498 | G_OBJECT_CLASS (fu_plugin_parent_class)->finalize (object); |
| 1499 | } |
| 1500 | |
| 1501 | FuPlugin * |
| 1502 | fu_plugin_new (void) |
| 1503 | { |
| 1504 | FuPlugin *plugin; |
| 1505 | plugin = g_object_new (FU_TYPE_PLUGIN, NULL); |
| 1506 | return plugin; |
| 1507 | } |