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 | 2de8f13 | 2018-01-17 09:12:02 +0000 | [diff] [blame] | 3 | * Copyright (C) 2016-2018 Richard Hughes <richard@hughsie.com> |
Richard Hughes | d090514 | 2016-03-13 09:46:49 +0000 | [diff] [blame] | 4 | * |
Mario Limonciello | 51308e6 | 2018-05-28 20:05:46 -0500 | [diff] [blame] | 5 | * SPDX-License-Identifier: LGPL-2.1+ |
Richard Hughes | d090514 | 2016-03-13 09:46:49 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include "config.h" |
| 9 | |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 10 | #include <fwupd.h> |
| 11 | #include <gmodule.h> |
| 12 | #include <appstream-glib.h> |
| 13 | #include <errno.h> |
| 14 | #include <string.h> |
| 15 | #include <gio/gunixinputstream.h> |
Mario Limonciello | 6d0aa3d | 2017-02-28 08:22:27 -0600 | [diff] [blame] | 16 | #ifdef HAVE_VALGRIND |
Richard Hughes | 576c012 | 2017-02-24 09:47:00 +0000 | [diff] [blame] | 17 | #include <valgrind.h> |
Mario Limonciello | 6d0aa3d | 2017-02-28 08:22:27 -0600 | [diff] [blame] | 18 | #endif /* HAVE_VALGRIND */ |
Richard Hughes | d090514 | 2016-03-13 09:46:49 +0000 | [diff] [blame] | 19 | |
Richard Hughes | 9dde04f | 2017-09-13 12:07:15 +0100 | [diff] [blame] | 20 | #include "fu-device-private.h" |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 21 | #include "fu-plugin-private.h" |
Richard Hughes | bc3a4e1 | 2018-01-06 22:41:47 +0000 | [diff] [blame] | 22 | #include "fu-history.h" |
Richard Hughes | d090514 | 2016-03-13 09:46:49 +0000 | [diff] [blame] | 23 | |
Richard Hughes | 4eada34 | 2017-10-03 21:20:32 +0100 | [diff] [blame] | 24 | /** |
| 25 | * SECTION:fu-plugin |
| 26 | * @short_description: a daemon plugin |
| 27 | * |
| 28 | * An object that represents a plugin run by the daemon. |
| 29 | * |
| 30 | * See also: #FuDevice |
| 31 | */ |
| 32 | |
Richard Hughes | b082903 | 2017-01-10 09:27:08 +0000 | [diff] [blame] | 33 | #define FU_PLUGIN_COLDPLUG_DELAY_MAXIMUM 3000u /* ms */ |
| 34 | |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 35 | static void fu_plugin_finalize (GObject *object); |
| 36 | |
| 37 | typedef struct { |
| 38 | GModule *module; |
| 39 | GUsbContext *usb_ctx; |
| 40 | gboolean enabled; |
Richard Hughes | 08a3799 | 2017-09-12 12:57:43 +0100 | [diff] [blame] | 41 | guint order; |
| 42 | GPtrArray *rules[FU_PLUGIN_RULE_LAST]; |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 43 | gchar *name; |
Richard Hughes | d7704d4 | 2017-08-08 20:29:09 +0100 | [diff] [blame] | 44 | FuHwids *hwids; |
Richard Hughes | 9c028f0 | 2017-10-28 21:14:28 +0100 | [diff] [blame] | 45 | FuQuirks *quirks; |
Richard Hughes | 0eb123b | 2018-04-19 12:00:04 +0100 | [diff] [blame] | 46 | GHashTable *runtime_versions; |
Richard Hughes | 34e0dab | 2018-04-20 16:43:00 +0100 | [diff] [blame] | 47 | GHashTable *compile_versions; |
Richard Hughes | 1354ea9 | 2017-09-19 15:58:31 +0100 | [diff] [blame] | 48 | GPtrArray *supported_guids; |
| 49 | FuSmbios *smbios; |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 50 | GHashTable *devices; /* platform_id:GObject */ |
Richard Hughes | ae3d65f | 2016-12-16 09:38:01 +0000 | [diff] [blame] | 51 | GHashTable *devices_delay; /* FuDevice:FuPluginHelper */ |
Richard Hughes | 80b79bb | 2018-01-11 21:11:06 +0000 | [diff] [blame] | 52 | GHashTable *report_metadata; /* key:value */ |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 53 | FuPluginData *data; |
| 54 | } FuPluginPrivate; |
| 55 | |
| 56 | enum { |
| 57 | SIGNAL_DEVICE_ADDED, |
| 58 | SIGNAL_DEVICE_REMOVED, |
Richard Hughes | e1fd34d | 2017-08-24 14:19:51 +0100 | [diff] [blame] | 59 | SIGNAL_DEVICE_REGISTER, |
Richard Hughes | 362d6d7 | 2017-01-07 21:42:14 +0000 | [diff] [blame] | 60 | SIGNAL_RECOLDPLUG, |
Richard Hughes | b082903 | 2017-01-10 09:27:08 +0000 | [diff] [blame] | 61 | SIGNAL_SET_COLDPLUG_DELAY, |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 62 | SIGNAL_LAST |
| 63 | }; |
| 64 | |
| 65 | static guint signals[SIGNAL_LAST] = { 0 }; |
| 66 | |
| 67 | G_DEFINE_TYPE_WITH_PRIVATE (FuPlugin, fu_plugin, G_TYPE_OBJECT) |
| 68 | #define GET_PRIVATE(o) (fu_plugin_get_instance_private (o)) |
| 69 | |
| 70 | typedef const gchar *(*FuPluginGetNameFunc) (void); |
| 71 | typedef void (*FuPluginInitFunc) (FuPlugin *plugin); |
| 72 | typedef gboolean (*FuPluginStartupFunc) (FuPlugin *plugin, |
| 73 | GError **error); |
Richard Hughes | e1fd34d | 2017-08-24 14:19:51 +0100 | [diff] [blame] | 74 | typedef void (*FuPluginDeviceRegisterFunc) (FuPlugin *plugin, |
| 75 | FuDevice *device); |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 76 | typedef gboolean (*FuPluginDeviceFunc) (FuPlugin *plugin, |
| 77 | FuDevice *device, |
| 78 | GError **error); |
| 79 | typedef gboolean (*FuPluginVerifyFunc) (FuPlugin *plugin, |
| 80 | FuDevice *device, |
| 81 | FuPluginVerifyFlags flags, |
| 82 | GError **error); |
| 83 | typedef gboolean (*FuPluginUpdateFunc) (FuPlugin *plugin, |
| 84 | FuDevice *device, |
| 85 | GBytes *blob_fw, |
| 86 | FwupdInstallFlags flags, |
| 87 | GError **error); |
Richard Hughes | 104f651 | 2017-11-24 11:44:57 +0000 | [diff] [blame] | 88 | typedef gboolean (*FuPluginUsbDeviceAddedFunc) (FuPlugin *plugin, |
| 89 | GUsbDevice *usb_device, |
| 90 | GError **error); |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 91 | |
Richard Hughes | 57d1822 | 2017-01-10 16:02:59 +0000 | [diff] [blame] | 92 | /** |
| 93 | * fu_plugin_get_name: |
| 94 | * @plugin: A #FuPlugin |
| 95 | * |
| 96 | * Gets the plugin name. |
| 97 | * |
| 98 | * Returns: a plugin name, or %NULL for unknown. |
| 99 | * |
| 100 | * Since: 0.8.0 |
| 101 | **/ |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 102 | const gchar * |
| 103 | fu_plugin_get_name (FuPlugin *plugin) |
Richard Hughes | d090514 | 2016-03-13 09:46:49 +0000 | [diff] [blame] | 104 | { |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 105 | FuPluginPrivate *priv = GET_PRIVATE (plugin); |
Richard Hughes | ccd78a9 | 2017-01-11 16:57:41 +0000 | [diff] [blame] | 106 | g_return_val_if_fail (FU_IS_PLUGIN (plugin), NULL); |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 107 | return priv->name; |
| 108 | } |
Richard Hughes | d090514 | 2016-03-13 09:46:49 +0000 | [diff] [blame] | 109 | |
Richard Hughes | 3483410 | 2017-11-21 21:55:00 +0000 | [diff] [blame] | 110 | void |
| 111 | fu_plugin_set_name (FuPlugin *plugin, const gchar *name) |
| 112 | { |
| 113 | FuPluginPrivate *priv = GET_PRIVATE (plugin); |
| 114 | g_return_if_fail (FU_IS_PLUGIN (plugin)); |
| 115 | g_return_if_fail (name != NULL); |
| 116 | g_free (priv->name); |
| 117 | priv->name = g_strdup (name); |
| 118 | } |
| 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 | |
Richard Hughes | 1e456bc | 2018-05-10 20:16:16 +0100 | [diff] [blame] | 281 | gchar * |
| 282 | fu_plugin_guess_name_from_fn (const gchar *filename) |
| 283 | { |
| 284 | const gchar *prefix = "libfu_plugin_"; |
| 285 | gchar *name; |
| 286 | gchar *str = g_strstr_len (filename, -1, prefix); |
| 287 | if (str == NULL) |
| 288 | return NULL; |
| 289 | name = g_strdup (str + strlen (prefix)); |
| 290 | g_strdelimit (name, ".", '\0'); |
| 291 | return name; |
| 292 | } |
| 293 | |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 294 | gboolean |
| 295 | fu_plugin_open (FuPlugin *plugin, const gchar *filename, GError **error) |
| 296 | { |
| 297 | FuPluginPrivate *priv = GET_PRIVATE (plugin); |
| 298 | FuPluginInitFunc func = NULL; |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 299 | |
| 300 | priv->module = g_module_open (filename, 0); |
| 301 | if (priv->module == NULL) { |
| 302 | g_set_error (error, |
| 303 | G_IO_ERROR, |
| 304 | G_IO_ERROR_FAILED, |
| 305 | "failed to open plugin: %s", |
| 306 | g_module_error ()); |
| 307 | return FALSE; |
| 308 | } |
| 309 | |
| 310 | /* set automatically */ |
Richard Hughes | 1e456bc | 2018-05-10 20:16:16 +0100 | [diff] [blame] | 311 | if (priv->name == NULL) |
| 312 | priv->name = fu_plugin_guess_name_from_fn (filename); |
Richard Hughes | d090514 | 2016-03-13 09:46:49 +0000 | [diff] [blame] | 313 | |
| 314 | /* optional */ |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 315 | g_module_symbol (priv->module, "fu_plugin_init", (gpointer *) &func); |
| 316 | if (func != NULL) { |
| 317 | g_debug ("performing init() on %s", filename); |
Richard Hughes | d090514 | 2016-03-13 09:46:49 +0000 | [diff] [blame] | 318 | func (plugin); |
| 319 | } |
| 320 | |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 321 | return TRUE; |
| 322 | } |
| 323 | |
Richard Hughes | 57d1822 | 2017-01-10 16:02:59 +0000 | [diff] [blame] | 324 | /** |
| 325 | * fu_plugin_device_add: |
| 326 | * @plugin: A #FuPlugin |
| 327 | * @device: A #FuDevice |
| 328 | * |
| 329 | * Asks the daemon to add a device to the exported list. If this device ID |
| 330 | * has already been added by a different plugin then this request will be |
| 331 | * ignored. |
| 332 | * |
| 333 | * Plugins should use fu_plugin_device_add_delay() if they are not capable of |
| 334 | * actually flashing an image to the hardware so that higher-priority plugins |
| 335 | * can add the device themselves. |
| 336 | * |
| 337 | * Since: 0.8.0 |
| 338 | **/ |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 339 | void |
| 340 | fu_plugin_device_add (FuPlugin *plugin, FuDevice *device) |
| 341 | { |
Richard Hughes | 5e44729 | 2018-04-27 14:25:54 +0100 | [diff] [blame] | 342 | GPtrArray *children; |
| 343 | |
Richard Hughes | ccd78a9 | 2017-01-11 16:57:41 +0000 | [diff] [blame] | 344 | g_return_if_fail (FU_IS_PLUGIN (plugin)); |
| 345 | g_return_if_fail (FU_IS_DEVICE (device)); |
| 346 | |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 347 | g_debug ("emit added from %s: %s", |
| 348 | fu_plugin_get_name (plugin), |
| 349 | fu_device_get_id (device)); |
| 350 | fu_device_set_created (device, (guint64) g_get_real_time () / G_USEC_PER_SEC); |
| 351 | fu_device_set_plugin (device, fu_plugin_get_name (plugin)); |
| 352 | g_signal_emit (plugin, signals[SIGNAL_DEVICE_ADDED], 0, device); |
Richard Hughes | 5e44729 | 2018-04-27 14:25:54 +0100 | [diff] [blame] | 353 | |
| 354 | /* add children */ |
| 355 | children = fu_device_get_children (device); |
| 356 | for (guint i = 0; i < children->len; i++) { |
| 357 | FuDevice *child = g_ptr_array_index (children, i); |
| 358 | fu_plugin_device_add (plugin, child); |
| 359 | } |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 360 | } |
| 361 | |
Richard Hughes | e1fd34d | 2017-08-24 14:19:51 +0100 | [diff] [blame] | 362 | /** |
| 363 | * fu_plugin_device_register: |
| 364 | * @plugin: A #FuPlugin |
| 365 | * @device: A #FuDevice |
| 366 | * |
| 367 | * Registers the device with other plugins so they can set metadata. |
| 368 | * |
| 369 | * Plugins do not have to call this manually as this is done automatically |
| 370 | * when using fu_plugin_device_add(). They may wish to use this manually |
| 371 | * if for intance the coldplug should be ignored based on the metadata |
| 372 | * set from other plugins. |
| 373 | * |
| 374 | * Since: 0.9.7 |
| 375 | **/ |
| 376 | void |
| 377 | fu_plugin_device_register (FuPlugin *plugin, FuDevice *device) |
| 378 | { |
| 379 | g_return_if_fail (FU_IS_PLUGIN (plugin)); |
| 380 | g_return_if_fail (FU_IS_DEVICE (device)); |
| 381 | |
| 382 | g_debug ("emit device-register from %s: %s", |
| 383 | fu_plugin_get_name (plugin), |
| 384 | fu_device_get_id (device)); |
| 385 | g_signal_emit (plugin, signals[SIGNAL_DEVICE_REGISTER], 0, device); |
| 386 | } |
| 387 | |
Richard Hughes | ae3d65f | 2016-12-16 09:38:01 +0000 | [diff] [blame] | 388 | typedef struct { |
| 389 | FuPlugin *plugin; |
| 390 | FuDevice *device; |
| 391 | guint timeout_id; |
Richard Hughes | d4184cf | 2016-12-21 16:05:17 +0000 | [diff] [blame] | 392 | GHashTable *devices; |
Richard Hughes | ae3d65f | 2016-12-16 09:38:01 +0000 | [diff] [blame] | 393 | } FuPluginHelper; |
| 394 | |
| 395 | static void |
| 396 | fu_plugin_helper_free (FuPluginHelper *helper) |
| 397 | { |
| 398 | g_object_unref (helper->plugin); |
| 399 | g_object_unref (helper->device); |
Richard Hughes | d4184cf | 2016-12-21 16:05:17 +0000 | [diff] [blame] | 400 | g_hash_table_unref (helper->devices); |
Richard Hughes | ae3d65f | 2016-12-16 09:38:01 +0000 | [diff] [blame] | 401 | g_free (helper); |
| 402 | } |
| 403 | |
| 404 | static gboolean |
| 405 | fu_plugin_device_add_delay_cb (gpointer user_data) |
| 406 | { |
| 407 | FuPluginHelper *helper = (FuPluginHelper *) user_data; |
| 408 | fu_plugin_device_add (helper->plugin, helper->device); |
Richard Hughes | 0dec274 | 2017-09-18 11:07:39 +0100 | [diff] [blame] | 409 | g_hash_table_remove (helper->devices, helper->device); |
Richard Hughes | ae3d65f | 2016-12-16 09:38:01 +0000 | [diff] [blame] | 410 | fu_plugin_helper_free (helper); |
| 411 | return FALSE; |
| 412 | } |
| 413 | |
Richard Hughes | 57d1822 | 2017-01-10 16:02:59 +0000 | [diff] [blame] | 414 | /** |
Richard Hughes | f0a799e | 2017-01-17 20:13:30 +0000 | [diff] [blame] | 415 | * fu_plugin_has_device_delay: |
| 416 | * @plugin: A #FuPlugin |
| 417 | * |
| 418 | * Returns if the device has a pending device that is waiting to be added. |
| 419 | * |
| 420 | * Returns: %TRUE if a device is waiting to be added |
| 421 | * |
| 422 | * Since: 0.8.0 |
| 423 | **/ |
| 424 | gboolean |
| 425 | fu_plugin_has_device_delay (FuPlugin *plugin) |
| 426 | { |
| 427 | FuPluginPrivate *priv = GET_PRIVATE (plugin); |
| 428 | return g_hash_table_size (priv->devices_delay) > 0; |
| 429 | } |
| 430 | |
| 431 | /** |
Richard Hughes | 57d1822 | 2017-01-10 16:02:59 +0000 | [diff] [blame] | 432 | * fu_plugin_device_add_delay: |
| 433 | * @plugin: A #FuPlugin |
| 434 | * @device: A #FuDevice |
| 435 | * |
| 436 | * Asks the daemon to add a device to the exported list after a small delay. |
| 437 | * |
| 438 | * Since: 0.8.0 |
| 439 | **/ |
Richard Hughes | ae3d65f | 2016-12-16 09:38:01 +0000 | [diff] [blame] | 440 | void |
| 441 | fu_plugin_device_add_delay (FuPlugin *plugin, FuDevice *device) |
| 442 | { |
| 443 | FuPluginPrivate *priv = GET_PRIVATE (plugin); |
| 444 | FuPluginHelper *helper; |
Richard Hughes | ccd78a9 | 2017-01-11 16:57:41 +0000 | [diff] [blame] | 445 | |
| 446 | g_return_if_fail (FU_IS_PLUGIN (plugin)); |
| 447 | g_return_if_fail (FU_IS_DEVICE (device)); |
| 448 | |
Richard Hughes | 6ad951d | 2017-02-03 10:12:12 +0000 | [diff] [blame] | 449 | /* already waiting for add */ |
| 450 | helper = g_hash_table_lookup (priv->devices_delay, device); |
| 451 | if (helper != NULL) { |
Richard Hughes | 3cca1c6 | 2017-07-21 13:38:20 +0100 | [diff] [blame] | 452 | g_debug ("ignoring add-delay as device %s already pending", |
| 453 | fu_device_get_id (device)); |
Richard Hughes | 6ad951d | 2017-02-03 10:12:12 +0000 | [diff] [blame] | 454 | return; |
| 455 | } |
| 456 | |
| 457 | /* add after a small delay */ |
Richard Hughes | ae3d65f | 2016-12-16 09:38:01 +0000 | [diff] [blame] | 458 | g_debug ("waiting a small time for other plugins"); |
| 459 | helper = g_new0 (FuPluginHelper, 1); |
| 460 | helper->plugin = g_object_ref (plugin); |
| 461 | helper->device = g_object_ref (device); |
| 462 | 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] | 463 | helper->devices = g_hash_table_ref (priv->devices_delay); |
| 464 | g_hash_table_insert (helper->devices, device, helper); |
Richard Hughes | ae3d65f | 2016-12-16 09:38:01 +0000 | [diff] [blame] | 465 | } |
| 466 | |
Richard Hughes | 57d1822 | 2017-01-10 16:02:59 +0000 | [diff] [blame] | 467 | /** |
Richard Hughes | 4eada34 | 2017-10-03 21:20:32 +0100 | [diff] [blame] | 468 | * fu_plugin_device_remove: |
Richard Hughes | 57d1822 | 2017-01-10 16:02:59 +0000 | [diff] [blame] | 469 | * @plugin: A #FuPlugin |
| 470 | * @device: A #FuDevice |
| 471 | * |
| 472 | * Asks the daemon to remove a device from the exported list. |
| 473 | * |
| 474 | * Since: 0.8.0 |
| 475 | **/ |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 476 | void |
| 477 | fu_plugin_device_remove (FuPlugin *plugin, FuDevice *device) |
| 478 | { |
Richard Hughes | ae3d65f | 2016-12-16 09:38:01 +0000 | [diff] [blame] | 479 | FuPluginPrivate *priv = GET_PRIVATE (plugin); |
| 480 | FuPluginHelper *helper; |
| 481 | |
Richard Hughes | ccd78a9 | 2017-01-11 16:57:41 +0000 | [diff] [blame] | 482 | g_return_if_fail (FU_IS_PLUGIN (plugin)); |
| 483 | g_return_if_fail (FU_IS_DEVICE (device)); |
| 484 | |
Richard Hughes | ae3d65f | 2016-12-16 09:38:01 +0000 | [diff] [blame] | 485 | /* waiting for add */ |
| 486 | helper = g_hash_table_lookup (priv->devices_delay, device); |
| 487 | if (helper != NULL) { |
| 488 | g_debug ("ignoring remove from delayed addition"); |
| 489 | g_source_remove (helper->timeout_id); |
Richard Hughes | d4184cf | 2016-12-21 16:05:17 +0000 | [diff] [blame] | 490 | g_hash_table_remove (priv->devices_delay, helper->device); |
Richard Hughes | ae3d65f | 2016-12-16 09:38:01 +0000 | [diff] [blame] | 491 | fu_plugin_helper_free (helper); |
| 492 | return; |
| 493 | } |
| 494 | |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 495 | g_debug ("emit removed from %s: %s", |
| 496 | fu_plugin_get_name (plugin), |
| 497 | fu_device_get_id (device)); |
| 498 | g_signal_emit (plugin, signals[SIGNAL_DEVICE_REMOVED], 0, device); |
| 499 | } |
| 500 | |
Richard Hughes | 57d1822 | 2017-01-10 16:02:59 +0000 | [diff] [blame] | 501 | /** |
Richard Hughes | 2de8f13 | 2018-01-17 09:12:02 +0000 | [diff] [blame] | 502 | * fu_plugin_request_recoldplug: |
Richard Hughes | 362d6d7 | 2017-01-07 21:42:14 +0000 | [diff] [blame] | 503 | * @plugin: A #FuPlugin |
| 504 | * |
| 505 | * Ask all the plugins to coldplug all devices, which will include the prepare() |
| 506 | * and cleanup() phases. Duplicate devices added will be ignored. |
| 507 | * |
| 508 | * Since: 0.8.0 |
| 509 | **/ |
| 510 | void |
Richard Hughes | 2de8f13 | 2018-01-17 09:12:02 +0000 | [diff] [blame] | 511 | fu_plugin_request_recoldplug (FuPlugin *plugin) |
Richard Hughes | 362d6d7 | 2017-01-07 21:42:14 +0000 | [diff] [blame] | 512 | { |
Richard Hughes | ccd78a9 | 2017-01-11 16:57:41 +0000 | [diff] [blame] | 513 | g_return_if_fail (FU_IS_PLUGIN (plugin)); |
Richard Hughes | 362d6d7 | 2017-01-07 21:42:14 +0000 | [diff] [blame] | 514 | g_signal_emit (plugin, signals[SIGNAL_RECOLDPLUG], 0); |
| 515 | } |
| 516 | |
Richard Hughes | b082903 | 2017-01-10 09:27:08 +0000 | [diff] [blame] | 517 | /** |
Richard Hughes | b8f8db2 | 2017-04-25 15:56:00 +0100 | [diff] [blame] | 518 | * fu_plugin_check_hwid: |
| 519 | * @plugin: A #FuPlugin |
Richard Hughes | 4eada34 | 2017-10-03 21:20:32 +0100 | [diff] [blame] | 520 | * @hwid: A Hardware ID GUID, e.g. `6de5d951-d755-576b-bd09-c5cf66b27234` |
Richard Hughes | b8f8db2 | 2017-04-25 15:56:00 +0100 | [diff] [blame] | 521 | * |
Richard Hughes | d7704d4 | 2017-08-08 20:29:09 +0100 | [diff] [blame] | 522 | * 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] | 523 | * specific system can be shown using the `fwupdmgr hwids` command. |
| 524 | * |
Richard Hughes | 4eada34 | 2017-10-03 21:20:32 +0100 | [diff] [blame] | 525 | * Returns: %TRUE if the HwId is found on the system. |
| 526 | * |
Richard Hughes | b8f8db2 | 2017-04-25 15:56:00 +0100 | [diff] [blame] | 527 | * Since: 0.9.1 |
| 528 | **/ |
| 529 | gboolean |
| 530 | fu_plugin_check_hwid (FuPlugin *plugin, const gchar *hwid) |
| 531 | { |
| 532 | FuPluginPrivate *priv = GET_PRIVATE (plugin); |
| 533 | if (priv->hwids == NULL) |
| 534 | return FALSE; |
Richard Hughes | d7704d4 | 2017-08-08 20:29:09 +0100 | [diff] [blame] | 535 | return fu_hwids_has_guid (priv->hwids, hwid); |
| 536 | } |
| 537 | |
| 538 | /** |
Richard Hughes | 1354ea9 | 2017-09-19 15:58:31 +0100 | [diff] [blame] | 539 | * fu_plugin_check_supported: |
| 540 | * @plugin: A #FuPlugin |
Richard Hughes | 4eada34 | 2017-10-03 21:20:32 +0100 | [diff] [blame] | 541 | * @guid: A Hardware ID GUID, e.g. `6de5d951-d755-576b-bd09-c5cf66b27234` |
Richard Hughes | 1354ea9 | 2017-09-19 15:58:31 +0100 | [diff] [blame] | 542 | * |
| 543 | * Checks to see if a specific device GUID is supported, i.e. available in the |
| 544 | * AppStream metadata. |
| 545 | * |
Richard Hughes | 4eada34 | 2017-10-03 21:20:32 +0100 | [diff] [blame] | 546 | * Returns: %TRUE if the device is supported. |
| 547 | * |
Richard Hughes | 1354ea9 | 2017-09-19 15:58:31 +0100 | [diff] [blame] | 548 | * Since: 1.0.0 |
| 549 | **/ |
| 550 | gboolean |
| 551 | fu_plugin_check_supported (FuPlugin *plugin, const gchar *guid) |
| 552 | { |
| 553 | FuPluginPrivate *priv = GET_PRIVATE (plugin); |
| 554 | if (priv->supported_guids == NULL) |
| 555 | return FALSE; |
| 556 | for (guint i = 0; i < priv->supported_guids->len; i++) { |
| 557 | const gchar *guid_tmp = g_ptr_array_index (priv->supported_guids, i); |
| 558 | if (g_strcmp0 (guid, guid_tmp) == 0) |
| 559 | return TRUE; |
| 560 | } |
| 561 | return FALSE; |
| 562 | } |
| 563 | |
| 564 | /** |
Richard Hughes | d7704d4 | 2017-08-08 20:29:09 +0100 | [diff] [blame] | 565 | * fu_plugin_get_dmi_value: |
| 566 | * @plugin: A #FuPlugin |
Richard Hughes | 4eada34 | 2017-10-03 21:20:32 +0100 | [diff] [blame] | 567 | * @dmi_id: A DMI ID, e.g. `BiosVersion` |
Richard Hughes | d7704d4 | 2017-08-08 20:29:09 +0100 | [diff] [blame] | 568 | * |
| 569 | * Gets a hardware DMI value. |
| 570 | * |
Richard Hughes | 4eada34 | 2017-10-03 21:20:32 +0100 | [diff] [blame] | 571 | * Returns: The string, or %NULL |
| 572 | * |
Richard Hughes | d7704d4 | 2017-08-08 20:29:09 +0100 | [diff] [blame] | 573 | * Since: 0.9.7 |
| 574 | **/ |
| 575 | const gchar * |
| 576 | fu_plugin_get_dmi_value (FuPlugin *plugin, const gchar *dmi_id) |
| 577 | { |
| 578 | FuPluginPrivate *priv = GET_PRIVATE (plugin); |
| 579 | if (priv->hwids == NULL) |
Richard Hughes | 7ef96b8 | 2017-08-23 18:28:24 +0100 | [diff] [blame] | 580 | return NULL; |
Richard Hughes | d7704d4 | 2017-08-08 20:29:09 +0100 | [diff] [blame] | 581 | return fu_hwids_get_value (priv->hwids, dmi_id); |
Richard Hughes | b8f8db2 | 2017-04-25 15:56:00 +0100 | [diff] [blame] | 582 | } |
| 583 | |
Richard Hughes | 49e5e05 | 2017-09-03 12:15:41 +0100 | [diff] [blame] | 584 | /** |
| 585 | * fu_plugin_get_smbios_string: |
| 586 | * @plugin: A #FuPlugin |
| 587 | * @structure_type: A SMBIOS structure type, e.g. %FU_SMBIOS_STRUCTURE_TYPE_BIOS |
| 588 | * @offset: A SMBIOS offset |
| 589 | * |
| 590 | * Gets a hardware SMBIOS string. |
| 591 | * |
| 592 | * The @type and @offset can be referenced from the DMTF SMBIOS specification: |
| 593 | * https://www.dmtf.org/sites/default/files/standards/documents/DSP0134_3.1.1.pdf |
| 594 | * |
Richard Hughes | 4eada34 | 2017-10-03 21:20:32 +0100 | [diff] [blame] | 595 | * Returns: A string, or %NULL |
| 596 | * |
Richard Hughes | 49e5e05 | 2017-09-03 12:15:41 +0100 | [diff] [blame] | 597 | * Since: 0.9.8 |
| 598 | **/ |
| 599 | const gchar * |
| 600 | fu_plugin_get_smbios_string (FuPlugin *plugin, guint8 structure_type, guint8 offset) |
| 601 | { |
| 602 | FuPluginPrivate *priv = GET_PRIVATE (plugin); |
| 603 | if (priv->smbios == NULL) |
| 604 | return NULL; |
| 605 | return fu_smbios_get_string (priv->smbios, structure_type, offset, NULL); |
| 606 | } |
| 607 | |
| 608 | /** |
| 609 | * fu_plugin_get_smbios_data: |
| 610 | * @plugin: A #FuPlugin |
| 611 | * @structure_type: A SMBIOS structure type, e.g. %FU_SMBIOS_STRUCTURE_TYPE_BIOS |
| 612 | * |
| 613 | * Gets a hardware SMBIOS data. |
| 614 | * |
Richard Hughes | 4eada34 | 2017-10-03 21:20:32 +0100 | [diff] [blame] | 615 | * Returns: (transfer none): A #GBytes, or %NULL |
| 616 | * |
Richard Hughes | 49e5e05 | 2017-09-03 12:15:41 +0100 | [diff] [blame] | 617 | * Since: 0.9.8 |
| 618 | **/ |
| 619 | GBytes * |
| 620 | fu_plugin_get_smbios_data (FuPlugin *plugin, guint8 structure_type) |
| 621 | { |
| 622 | FuPluginPrivate *priv = GET_PRIVATE (plugin); |
| 623 | if (priv->smbios == NULL) |
| 624 | return NULL; |
| 625 | return fu_smbios_get_data (priv->smbios, structure_type, NULL); |
| 626 | } |
| 627 | |
Richard Hughes | b8f8db2 | 2017-04-25 15:56:00 +0100 | [diff] [blame] | 628 | void |
Richard Hughes | d7704d4 | 2017-08-08 20:29:09 +0100 | [diff] [blame] | 629 | fu_plugin_set_hwids (FuPlugin *plugin, FuHwids *hwids) |
Richard Hughes | b8f8db2 | 2017-04-25 15:56:00 +0100 | [diff] [blame] | 630 | { |
| 631 | FuPluginPrivate *priv = GET_PRIVATE (plugin); |
Richard Hughes | d7704d4 | 2017-08-08 20:29:09 +0100 | [diff] [blame] | 632 | g_set_object (&priv->hwids, hwids); |
Richard Hughes | b8f8db2 | 2017-04-25 15:56:00 +0100 | [diff] [blame] | 633 | } |
| 634 | |
Richard Hughes | 49e5e05 | 2017-09-03 12:15:41 +0100 | [diff] [blame] | 635 | void |
Richard Hughes | 1354ea9 | 2017-09-19 15:58:31 +0100 | [diff] [blame] | 636 | fu_plugin_set_supported (FuPlugin *plugin, GPtrArray *supported_guids) |
| 637 | { |
| 638 | FuPluginPrivate *priv = GET_PRIVATE (plugin); |
| 639 | if (priv->supported_guids != NULL) |
| 640 | g_ptr_array_unref (priv->supported_guids); |
| 641 | priv->supported_guids = g_ptr_array_ref (supported_guids); |
| 642 | } |
| 643 | |
Richard Hughes | 9c028f0 | 2017-10-28 21:14:28 +0100 | [diff] [blame] | 644 | void |
| 645 | fu_plugin_set_quirks (FuPlugin *plugin, FuQuirks *quirks) |
| 646 | { |
| 647 | FuPluginPrivate *priv = GET_PRIVATE (plugin); |
| 648 | g_set_object (&priv->quirks, quirks); |
| 649 | } |
| 650 | |
| 651 | /** |
| 652 | * fu_plugin_get_quirks: |
| 653 | * @plugin: A #FuPlugin |
| 654 | * |
| 655 | * Returns the hardware database object. This can be used to discover device |
| 656 | * quirks or other device-specific settings. |
| 657 | * |
| 658 | * Returns: (transfer none): a #FuQuirks, or %NULL if not set |
| 659 | * |
| 660 | * Since: 1.0.1 |
| 661 | **/ |
| 662 | FuQuirks * |
| 663 | fu_plugin_get_quirks (FuPlugin *plugin) |
| 664 | { |
| 665 | FuPluginPrivate *priv = GET_PRIVATE (plugin); |
| 666 | return priv->quirks; |
| 667 | } |
| 668 | |
Richard Hughes | 0eb123b | 2018-04-19 12:00:04 +0100 | [diff] [blame] | 669 | void |
| 670 | fu_plugin_set_runtime_versions (FuPlugin *plugin, GHashTable *runtime_versions) |
| 671 | { |
| 672 | FuPluginPrivate *priv = GET_PRIVATE (plugin); |
| 673 | priv->runtime_versions = g_hash_table_ref (runtime_versions); |
| 674 | } |
| 675 | |
| 676 | /** |
| 677 | * fu_plugin_add_runtime_version: |
| 678 | * @plugin: A #FuPlugin |
| 679 | * @component_id: An AppStream component id, e.g. "org.gnome.Software" |
| 680 | * @version: A version string, e.g. "1.2.3" |
| 681 | * |
| 682 | * Sets a runtime version of a specific dependancy. |
| 683 | * |
| 684 | * Since: 1.0.7 |
| 685 | **/ |
| 686 | void |
| 687 | fu_plugin_add_runtime_version (FuPlugin *plugin, |
| 688 | const gchar *component_id, |
| 689 | const gchar *version) |
| 690 | { |
| 691 | FuPluginPrivate *priv = GET_PRIVATE (plugin); |
Richard Hughes | b01b486 | 2018-04-20 16:39:48 +0100 | [diff] [blame] | 692 | if (priv->runtime_versions == NULL) |
| 693 | return; |
Richard Hughes | 0eb123b | 2018-04-19 12:00:04 +0100 | [diff] [blame] | 694 | g_hash_table_insert (priv->runtime_versions, |
| 695 | g_strdup (component_id), |
| 696 | g_strdup (version)); |
| 697 | } |
| 698 | |
Richard Hughes | 34e0dab | 2018-04-20 16:43:00 +0100 | [diff] [blame] | 699 | void |
| 700 | fu_plugin_set_compile_versions (FuPlugin *plugin, GHashTable *compile_versions) |
| 701 | { |
| 702 | FuPluginPrivate *priv = GET_PRIVATE (plugin); |
| 703 | priv->compile_versions = g_hash_table_ref (compile_versions); |
| 704 | } |
| 705 | |
| 706 | /** |
| 707 | * fu_plugin_add_compile_version: |
| 708 | * @plugin: A #FuPlugin |
| 709 | * @component_id: An AppStream component id, e.g. "org.gnome.Software" |
| 710 | * @version: A version string, e.g. "1.2.3" |
| 711 | * |
| 712 | * Sets a compile-time version of a specific dependancy. |
| 713 | * |
| 714 | * Since: 1.0.7 |
| 715 | **/ |
| 716 | void |
| 717 | fu_plugin_add_compile_version (FuPlugin *plugin, |
| 718 | const gchar *component_id, |
| 719 | const gchar *version) |
| 720 | { |
| 721 | FuPluginPrivate *priv = GET_PRIVATE (plugin); |
| 722 | if (priv->compile_versions == NULL) |
| 723 | return; |
| 724 | g_hash_table_insert (priv->compile_versions, |
| 725 | g_strdup (component_id), |
| 726 | g_strdup (version)); |
| 727 | } |
| 728 | |
Richard Hughes | 9c028f0 | 2017-10-28 21:14:28 +0100 | [diff] [blame] | 729 | /** |
| 730 | * fu_plugin_lookup_quirk_by_id: |
| 731 | * @plugin: A #FuPlugin |
| 732 | * @prefix: A string prefix that matches the quirks file basename, e.g. "dfu-quirks" |
| 733 | * @id: An ID to match the entry, e.g. "012345" |
| 734 | * |
| 735 | * Looks up an entry in the hardware database using a string value. |
| 736 | * |
| 737 | * Returns: (transfer none): values from the database, or %NULL if not found |
| 738 | * |
| 739 | * Since: 1.0.1 |
| 740 | **/ |
| 741 | const gchar * |
| 742 | fu_plugin_lookup_quirk_by_id (FuPlugin *plugin, const gchar *prefix, const gchar *id) |
| 743 | { |
| 744 | FuPluginPrivate *priv = GET_PRIVATE (plugin); |
| 745 | g_return_val_if_fail (FU_IS_PLUGIN (plugin), NULL); |
| 746 | |
| 747 | /* wildcard */ |
| 748 | if (g_strstr_len (id, -1, "*") != NULL) |
| 749 | return fu_quirks_lookup_by_glob (priv->quirks, prefix, id); |
| 750 | |
| 751 | /* exact ID */ |
| 752 | return fu_quirks_lookup_by_id (priv->quirks, prefix, id); |
| 753 | } |
| 754 | |
| 755 | /** |
| 756 | * fu_plugin_lookup_quirk_by_usb_device: |
| 757 | * @plugin: A #FuPlugin |
| 758 | * @prefix: A string prefix that matches the quirks file basename, e.g. "dfu-quirks" |
| 759 | * @dev: A #GUsbDevice |
| 760 | * |
| 761 | * Looks up an entry in the hardware database using various keys generated |
| 762 | * from @dev. |
| 763 | * |
| 764 | * Returns: (transfer none): values from the database, or %NULL if not found |
| 765 | * |
| 766 | * Since: 1.0.1 |
| 767 | **/ |
| 768 | const gchar * |
| 769 | fu_plugin_lookup_quirk_by_usb_device (FuPlugin *plugin, const gchar *prefix, GUsbDevice *dev) |
| 770 | { |
| 771 | FuPluginPrivate *priv = GET_PRIVATE (plugin); |
| 772 | g_return_val_if_fail (FU_IS_PLUGIN (plugin), NULL); |
| 773 | return fu_quirks_lookup_by_usb_device (priv->quirks, prefix, dev); |
| 774 | } |
| 775 | |
Richard Hughes | 1354ea9 | 2017-09-19 15:58:31 +0100 | [diff] [blame] | 776 | /** |
| 777 | * fu_plugin_get_supported: |
| 778 | * @plugin: A #FuPlugin |
| 779 | * |
| 780 | * Gets all the device GUIDs supported by the daemon. |
| 781 | * |
Richard Hughes | 4eada34 | 2017-10-03 21:20:32 +0100 | [diff] [blame] | 782 | * Returns: (element-type utf8) (transfer none): GUIDs |
| 783 | * |
Richard Hughes | 1354ea9 | 2017-09-19 15:58:31 +0100 | [diff] [blame] | 784 | * Since: 1.0.0 |
| 785 | **/ |
| 786 | GPtrArray * |
| 787 | fu_plugin_get_supported (FuPlugin *plugin) |
| 788 | { |
| 789 | FuPluginPrivate *priv = GET_PRIVATE (plugin); |
| 790 | return priv->supported_guids; |
| 791 | } |
| 792 | |
| 793 | void |
Richard Hughes | 49e5e05 | 2017-09-03 12:15:41 +0100 | [diff] [blame] | 794 | fu_plugin_set_smbios (FuPlugin *plugin, FuSmbios *smbios) |
| 795 | { |
| 796 | FuPluginPrivate *priv = GET_PRIVATE (plugin); |
| 797 | g_set_object (&priv->smbios, smbios); |
| 798 | } |
| 799 | |
Richard Hughes | b8f8db2 | 2017-04-25 15:56:00 +0100 | [diff] [blame] | 800 | /** |
Richard Hughes | b082903 | 2017-01-10 09:27:08 +0000 | [diff] [blame] | 801 | * fu_plugin_set_coldplug_delay: |
| 802 | * @plugin: A #FuPlugin |
| 803 | * @duration: A delay in milliseconds |
| 804 | * |
| 805 | * Set the minimum time that should be waited inbetween the call to |
| 806 | * fu_plugin_coldplug_prepare() and fu_plugin_coldplug(). This is usually going |
| 807 | * to be the minimum hardware initialisation time from a datasheet. |
| 808 | * |
| 809 | * It is better to use this function rather than using a sleep() in the plugin |
| 810 | * itself as then only one delay is done in the daemon rather than waiting for |
| 811 | * each coldplug prepare in a serial way. |
| 812 | * |
| 813 | * Additionally, very long delays should be avoided as the daemon will be |
| 814 | * blocked from processing requests whilst the coldplug delay is being |
| 815 | * performed. |
| 816 | * |
| 817 | * Since: 0.8.0 |
| 818 | **/ |
| 819 | void |
| 820 | fu_plugin_set_coldplug_delay (FuPlugin *plugin, guint duration) |
| 821 | { |
| 822 | g_return_if_fail (FU_IS_PLUGIN (plugin)); |
| 823 | g_return_if_fail (duration > 0); |
| 824 | |
| 825 | /* check sanity */ |
| 826 | if (duration > FU_PLUGIN_COLDPLUG_DELAY_MAXIMUM) { |
| 827 | g_warning ("duration of %ums is crazy, truncating to %ums", |
| 828 | duration, |
| 829 | FU_PLUGIN_COLDPLUG_DELAY_MAXIMUM); |
| 830 | duration = FU_PLUGIN_COLDPLUG_DELAY_MAXIMUM; |
| 831 | } |
| 832 | |
| 833 | /* emit */ |
| 834 | g_signal_emit (plugin, signals[SIGNAL_SET_COLDPLUG_DELAY], 0, duration); |
| 835 | } |
| 836 | |
Richard Hughes | d090514 | 2016-03-13 09:46:49 +0000 | [diff] [blame] | 837 | gboolean |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 838 | fu_plugin_runner_startup (FuPlugin *plugin, GError **error) |
Richard Hughes | d090514 | 2016-03-13 09:46:49 +0000 | [diff] [blame] | 839 | { |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 840 | FuPluginPrivate *priv = GET_PRIVATE (plugin); |
Richard Hughes | 7b8b202 | 2016-12-12 16:15:03 +0000 | [diff] [blame] | 841 | FuPluginStartupFunc func = NULL; |
Richard Hughes | d090514 | 2016-03-13 09:46:49 +0000 | [diff] [blame] | 842 | |
| 843 | /* not enabled */ |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 844 | if (!priv->enabled) |
Richard Hughes | d090514 | 2016-03-13 09:46:49 +0000 | [diff] [blame] | 845 | return TRUE; |
| 846 | |
Richard Hughes | 639da47 | 2018-01-06 22:35:04 +0000 | [diff] [blame] | 847 | /* no object loaded */ |
| 848 | if (priv->module == NULL) |
| 849 | return TRUE; |
| 850 | |
Richard Hughes | d090514 | 2016-03-13 09:46:49 +0000 | [diff] [blame] | 851 | /* optional */ |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 852 | g_module_symbol (priv->module, "fu_plugin_startup", (gpointer *) &func); |
| 853 | if (func == NULL) |
Richard Hughes | d090514 | 2016-03-13 09:46:49 +0000 | [diff] [blame] | 854 | return TRUE; |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 855 | g_debug ("performing startup() on %s", priv->name); |
Richard Hughes | d090514 | 2016-03-13 09:46:49 +0000 | [diff] [blame] | 856 | if (!func (plugin, error)) { |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 857 | g_prefix_error (error, "failed to startup %s: ", priv->name); |
| 858 | return FALSE; |
| 859 | } |
| 860 | return TRUE; |
| 861 | } |
| 862 | |
| 863 | static gboolean |
| 864 | fu_plugin_runner_offline_invalidate (GError **error) |
| 865 | { |
| 866 | g_autoptr(GError) error_local = NULL; |
| 867 | g_autoptr(GFile) file1 = NULL; |
| 868 | |
| 869 | g_return_val_if_fail (error == NULL || *error == NULL, FALSE); |
| 870 | |
| 871 | file1 = g_file_new_for_path (FU_OFFLINE_TRIGGER_FILENAME); |
| 872 | if (!g_file_query_exists (file1, NULL)) |
| 873 | return TRUE; |
| 874 | if (!g_file_delete (file1, NULL, &error_local)) { |
| 875 | g_set_error (error, |
| 876 | FWUPD_ERROR, |
| 877 | FWUPD_ERROR_INTERNAL, |
| 878 | "Cannot delete %s: %s", |
| 879 | FU_OFFLINE_TRIGGER_FILENAME, |
| 880 | error_local->message); |
| 881 | return FALSE; |
| 882 | } |
| 883 | return TRUE; |
| 884 | } |
| 885 | |
| 886 | static gboolean |
| 887 | fu_plugin_runner_offline_setup (GError **error) |
| 888 | { |
| 889 | gint rc; |
| 890 | |
| 891 | g_return_val_if_fail (error == NULL || *error == NULL, FALSE); |
| 892 | |
| 893 | /* create symlink for the systemd-system-update-generator */ |
| 894 | rc = symlink ("/var/lib/fwupd", FU_OFFLINE_TRIGGER_FILENAME); |
| 895 | if (rc < 0) { |
| 896 | g_set_error (error, |
| 897 | FWUPD_ERROR, |
| 898 | FWUPD_ERROR_INTERNAL, |
| 899 | "Failed to create symlink %s to %s: %s", |
| 900 | FU_OFFLINE_TRIGGER_FILENAME, |
| 901 | "/var/lib", strerror (errno)); |
Richard Hughes | d090514 | 2016-03-13 09:46:49 +0000 | [diff] [blame] | 902 | return FALSE; |
| 903 | } |
| 904 | return TRUE; |
| 905 | } |
| 906 | |
Richard Hughes | 0d7fdb3 | 2017-11-11 20:23:14 +0000 | [diff] [blame] | 907 | static gboolean |
| 908 | fu_plugin_runner_device_generic (FuPlugin *plugin, FuDevice *device, |
| 909 | const gchar *symbol_name, GError **error) |
| 910 | { |
| 911 | FuPluginPrivate *priv = GET_PRIVATE (plugin); |
| 912 | FuPluginDeviceFunc func = NULL; |
| 913 | |
| 914 | /* not enabled */ |
| 915 | if (!priv->enabled) |
| 916 | return TRUE; |
| 917 | |
Richard Hughes | d3d96cc | 2017-11-14 11:34:33 +0000 | [diff] [blame] | 918 | /* no object loaded */ |
| 919 | if (priv->module == NULL) |
| 920 | return TRUE; |
| 921 | |
Richard Hughes | 0d7fdb3 | 2017-11-11 20:23:14 +0000 | [diff] [blame] | 922 | /* optional */ |
| 923 | g_module_symbol (priv->module, symbol_name, (gpointer *) &func); |
| 924 | if (func == NULL) |
| 925 | return TRUE; |
| 926 | g_debug ("performing %s() on %s", symbol_name + 10, priv->name); |
| 927 | if (!func (plugin, device, error)) { |
Richard Hughes | 83e54e4 | 2018-01-31 23:27:30 +0000 | [diff] [blame] | 928 | g_prefix_error (error, "failed to run %s() on %s: ", |
Richard Hughes | 0d7fdb3 | 2017-11-11 20:23:14 +0000 | [diff] [blame] | 929 | symbol_name + 10, |
Richard Hughes | 0d7fdb3 | 2017-11-11 20:23:14 +0000 | [diff] [blame] | 930 | priv->name); |
| 931 | return FALSE; |
| 932 | } |
| 933 | return TRUE; |
| 934 | } |
| 935 | |
Richard Hughes | d090514 | 2016-03-13 09:46:49 +0000 | [diff] [blame] | 936 | gboolean |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 937 | fu_plugin_runner_coldplug (FuPlugin *plugin, GError **error) |
Richard Hughes | d090514 | 2016-03-13 09:46:49 +0000 | [diff] [blame] | 938 | { |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 939 | FuPluginPrivate *priv = GET_PRIVATE (plugin); |
Richard Hughes | 7b8b202 | 2016-12-12 16:15:03 +0000 | [diff] [blame] | 940 | FuPluginStartupFunc func = NULL; |
Richard Hughes | d090514 | 2016-03-13 09:46:49 +0000 | [diff] [blame] | 941 | |
| 942 | /* not enabled */ |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 943 | if (!priv->enabled) |
Richard Hughes | d090514 | 2016-03-13 09:46:49 +0000 | [diff] [blame] | 944 | return TRUE; |
| 945 | |
Richard Hughes | 639da47 | 2018-01-06 22:35:04 +0000 | [diff] [blame] | 946 | /* no object loaded */ |
| 947 | if (priv->module == NULL) |
| 948 | return TRUE; |
| 949 | |
Richard Hughes | d090514 | 2016-03-13 09:46:49 +0000 | [diff] [blame] | 950 | /* optional */ |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 951 | g_module_symbol (priv->module, "fu_plugin_coldplug", (gpointer *) &func); |
| 952 | if (func == NULL) |
Richard Hughes | d090514 | 2016-03-13 09:46:49 +0000 | [diff] [blame] | 953 | return TRUE; |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 954 | g_debug ("performing coldplug() on %s", priv->name); |
| 955 | if (!func (plugin, error)) { |
| 956 | g_prefix_error (error, "failed to coldplug %s: ", priv->name); |
| 957 | return FALSE; |
| 958 | } |
| 959 | return TRUE; |
| 960 | } |
| 961 | |
Richard Hughes | 7b8b202 | 2016-12-12 16:15:03 +0000 | [diff] [blame] | 962 | gboolean |
Richard Hughes | 2de8f13 | 2018-01-17 09:12:02 +0000 | [diff] [blame] | 963 | fu_plugin_runner_recoldplug (FuPlugin *plugin, GError **error) |
| 964 | { |
| 965 | FuPluginPrivate *priv = GET_PRIVATE (plugin); |
| 966 | FuPluginStartupFunc func = NULL; |
| 967 | |
| 968 | /* not enabled */ |
| 969 | if (!priv->enabled) |
| 970 | return TRUE; |
| 971 | |
| 972 | /* no object loaded */ |
| 973 | if (priv->module == NULL) |
| 974 | return TRUE; |
| 975 | |
| 976 | /* optional */ |
| 977 | g_module_symbol (priv->module, "fu_plugin_recoldplug", (gpointer *) &func); |
| 978 | if (func == NULL) |
| 979 | return TRUE; |
| 980 | g_debug ("performing recoldplug() on %s", priv->name); |
| 981 | if (!func (plugin, error)) { |
| 982 | g_prefix_error (error, "failed to recoldplug %s: ", priv->name); |
| 983 | return FALSE; |
| 984 | } |
| 985 | return TRUE; |
| 986 | } |
| 987 | |
| 988 | gboolean |
Richard Hughes | 46487c9 | 2017-01-07 21:26:34 +0000 | [diff] [blame] | 989 | fu_plugin_runner_coldplug_prepare (FuPlugin *plugin, GError **error) |
| 990 | { |
| 991 | FuPluginPrivate *priv = GET_PRIVATE (plugin); |
| 992 | FuPluginStartupFunc func = NULL; |
| 993 | |
| 994 | /* not enabled */ |
| 995 | if (!priv->enabled) |
| 996 | return TRUE; |
| 997 | |
Richard Hughes | 639da47 | 2018-01-06 22:35:04 +0000 | [diff] [blame] | 998 | /* no object loaded */ |
| 999 | if (priv->module == NULL) |
| 1000 | return TRUE; |
| 1001 | |
Richard Hughes | 46487c9 | 2017-01-07 21:26:34 +0000 | [diff] [blame] | 1002 | /* optional */ |
| 1003 | g_module_symbol (priv->module, "fu_plugin_coldplug_prepare", (gpointer *) &func); |
| 1004 | if (func == NULL) |
| 1005 | return TRUE; |
| 1006 | g_debug ("performing coldplug_prepare() on %s", priv->name); |
| 1007 | if (!func (plugin, error)) { |
| 1008 | g_prefix_error (error, "failed to prepare for coldplug %s: ", priv->name); |
| 1009 | return FALSE; |
| 1010 | } |
| 1011 | return TRUE; |
| 1012 | } |
| 1013 | |
| 1014 | gboolean |
| 1015 | fu_plugin_runner_coldplug_cleanup (FuPlugin *plugin, GError **error) |
| 1016 | { |
| 1017 | FuPluginPrivate *priv = GET_PRIVATE (plugin); |
| 1018 | FuPluginStartupFunc func = NULL; |
| 1019 | |
| 1020 | /* not enabled */ |
| 1021 | if (!priv->enabled) |
| 1022 | return TRUE; |
| 1023 | |
Richard Hughes | 639da47 | 2018-01-06 22:35:04 +0000 | [diff] [blame] | 1024 | /* no object loaded */ |
| 1025 | if (priv->module == NULL) |
| 1026 | return TRUE; |
| 1027 | |
Richard Hughes | 46487c9 | 2017-01-07 21:26:34 +0000 | [diff] [blame] | 1028 | /* optional */ |
| 1029 | g_module_symbol (priv->module, "fu_plugin_coldplug_cleanup", (gpointer *) &func); |
| 1030 | if (func == NULL) |
| 1031 | return TRUE; |
| 1032 | g_debug ("performing coldplug_cleanup() on %s", priv->name); |
| 1033 | if (!func (plugin, error)) { |
| 1034 | g_prefix_error (error, "failed to cleanup coldplug %s: ", priv->name); |
| 1035 | return FALSE; |
| 1036 | } |
| 1037 | return TRUE; |
| 1038 | } |
| 1039 | |
| 1040 | gboolean |
Richard Hughes | 7b8b202 | 2016-12-12 16:15:03 +0000 | [diff] [blame] | 1041 | fu_plugin_runner_update_prepare (FuPlugin *plugin, FuDevice *device, GError **error) |
| 1042 | { |
Richard Hughes | 0d7fdb3 | 2017-11-11 20:23:14 +0000 | [diff] [blame] | 1043 | return fu_plugin_runner_device_generic (plugin, device, |
| 1044 | "fu_plugin_update_prepare", error); |
Richard Hughes | 7b8b202 | 2016-12-12 16:15:03 +0000 | [diff] [blame] | 1045 | } |
| 1046 | |
| 1047 | gboolean |
| 1048 | fu_plugin_runner_update_cleanup (FuPlugin *plugin, FuDevice *device, GError **error) |
| 1049 | { |
Richard Hughes | 0d7fdb3 | 2017-11-11 20:23:14 +0000 | [diff] [blame] | 1050 | return fu_plugin_runner_device_generic (plugin, device, |
| 1051 | "fu_plugin_update_cleanup", error); |
| 1052 | } |
Richard Hughes | 7b8b202 | 2016-12-12 16:15:03 +0000 | [diff] [blame] | 1053 | |
Richard Hughes | 0d7fdb3 | 2017-11-11 20:23:14 +0000 | [diff] [blame] | 1054 | gboolean |
| 1055 | fu_plugin_runner_update_attach (FuPlugin *plugin, FuDevice *device, GError **error) |
| 1056 | { |
| 1057 | return fu_plugin_runner_device_generic (plugin, device, |
| 1058 | "fu_plugin_update_attach", error); |
| 1059 | } |
Richard Hughes | 7b8b202 | 2016-12-12 16:15:03 +0000 | [diff] [blame] | 1060 | |
Richard Hughes | 0d7fdb3 | 2017-11-11 20:23:14 +0000 | [diff] [blame] | 1061 | gboolean |
| 1062 | fu_plugin_runner_update_detach (FuPlugin *plugin, FuDevice *device, GError **error) |
| 1063 | { |
| 1064 | return fu_plugin_runner_device_generic (plugin, device, |
| 1065 | "fu_plugin_update_detach", error); |
| 1066 | } |
| 1067 | |
| 1068 | gboolean |
| 1069 | fu_plugin_runner_update_reload (FuPlugin *plugin, FuDevice *device, GError **error) |
| 1070 | { |
| 1071 | return fu_plugin_runner_device_generic (plugin, device, |
| 1072 | "fu_plugin_update_reload", error); |
Richard Hughes | 7b8b202 | 2016-12-12 16:15:03 +0000 | [diff] [blame] | 1073 | } |
| 1074 | |
Richard Hughes | 104f651 | 2017-11-24 11:44:57 +0000 | [diff] [blame] | 1075 | gboolean |
| 1076 | fu_plugin_runner_usb_device_added (FuPlugin *plugin, GUsbDevice *usb_device, GError **error) |
| 1077 | { |
| 1078 | FuPluginPrivate *priv = GET_PRIVATE (plugin); |
| 1079 | FuPluginUsbDeviceAddedFunc func = NULL; |
| 1080 | |
| 1081 | /* not enabled */ |
| 1082 | if (!priv->enabled) |
| 1083 | return TRUE; |
Richard Hughes | 639da47 | 2018-01-06 22:35:04 +0000 | [diff] [blame] | 1084 | |
| 1085 | /* no object loaded */ |
Richard Hughes | 104f651 | 2017-11-24 11:44:57 +0000 | [diff] [blame] | 1086 | if (priv->module == NULL) |
| 1087 | return TRUE; |
| 1088 | |
| 1089 | /* optional */ |
| 1090 | g_module_symbol (priv->module, "fu_plugin_usb_device_added", (gpointer *) &func); |
| 1091 | if (func != NULL) { |
| 1092 | g_debug ("performing usb_device_added() on %s", priv->name); |
| 1093 | return func (plugin, usb_device, error); |
| 1094 | } |
| 1095 | return TRUE; |
| 1096 | } |
| 1097 | |
Richard Hughes | e1fd34d | 2017-08-24 14:19:51 +0100 | [diff] [blame] | 1098 | void |
| 1099 | fu_plugin_runner_device_register (FuPlugin *plugin, FuDevice *device) |
| 1100 | { |
| 1101 | FuPluginPrivate *priv = GET_PRIVATE (plugin); |
| 1102 | FuPluginDeviceRegisterFunc func = NULL; |
| 1103 | |
| 1104 | /* not enabled */ |
| 1105 | if (!priv->enabled) |
| 1106 | return; |
Richard Hughes | 3483410 | 2017-11-21 21:55:00 +0000 | [diff] [blame] | 1107 | if (priv->module == NULL) |
| 1108 | return; |
Richard Hughes | e1fd34d | 2017-08-24 14:19:51 +0100 | [diff] [blame] | 1109 | |
| 1110 | /* optional */ |
| 1111 | g_module_symbol (priv->module, "fu_plugin_device_registered", (gpointer *) &func); |
| 1112 | if (func != NULL) { |
| 1113 | g_debug ("performing device_added() on %s", priv->name); |
| 1114 | func (plugin, device); |
| 1115 | } |
| 1116 | } |
| 1117 | |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 1118 | static gboolean |
| 1119 | fu_plugin_runner_schedule_update (FuPlugin *plugin, |
| 1120 | FuDevice *device, |
| 1121 | GBytes *blob_cab, |
| 1122 | GError **error) |
| 1123 | { |
Richard Hughes | 38fb56c | 2018-02-01 22:07:52 +0000 | [diff] [blame] | 1124 | FwupdRelease *release; |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 1125 | gchar tmpname[] = {"XXXXXX.cap"}; |
| 1126 | g_autofree gchar *dirname = NULL; |
| 1127 | g_autofree gchar *filename = NULL; |
Richard Hughes | 68982c6 | 2017-09-13 15:40:14 +0100 | [diff] [blame] | 1128 | g_autoptr(FuDevice) res_tmp = NULL; |
Richard Hughes | 780ef3f | 2018-01-12 16:20:31 +0000 | [diff] [blame] | 1129 | g_autoptr(FuHistory) history = NULL; |
Richard Hughes | 38fb56c | 2018-02-01 22:07:52 +0000 | [diff] [blame] | 1130 | g_autoptr(FwupdRelease) release_tmp = fwupd_release_new (); |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 1131 | g_autoptr(GFile) file = NULL; |
| 1132 | |
| 1133 | /* id already exists */ |
Richard Hughes | 780ef3f | 2018-01-12 16:20:31 +0000 | [diff] [blame] | 1134 | history = fu_history_new (); |
Richard Hughes | 0b9d996 | 2018-01-12 16:31:28 +0000 | [diff] [blame] | 1135 | res_tmp = fu_history_get_device_by_id (history, fu_device_get_id (device), NULL); |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 1136 | if (res_tmp != NULL) { |
| 1137 | g_set_error (error, |
| 1138 | FWUPD_ERROR, |
| 1139 | FWUPD_ERROR_ALREADY_PENDING, |
| 1140 | "%s is already scheduled to be updated", |
| 1141 | fu_device_get_id (device)); |
| 1142 | return FALSE; |
| 1143 | } |
| 1144 | |
| 1145 | /* create directory */ |
Richard Hughes | 4be17d1 | 2018-05-30 20:36:29 +0100 | [diff] [blame] | 1146 | dirname = fu_common_get_path (FU_PATH_KIND_LOCALSTATEDIR_PKG); |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 1147 | file = g_file_new_for_path (dirname); |
| 1148 | if (!g_file_query_exists (file, NULL)) { |
| 1149 | if (!g_file_make_directory_with_parents (file, NULL, error)) |
| 1150 | return FALSE; |
| 1151 | } |
| 1152 | |
| 1153 | /* get a random filename */ |
| 1154 | for (guint i = 0; i < 6; i++) |
| 1155 | tmpname[i] = (gchar) g_random_int_range ('A', 'Z'); |
| 1156 | filename = g_build_filename (dirname, tmpname, NULL); |
| 1157 | |
| 1158 | /* just copy to the temp file */ |
Richard Hughes | 23135eb | 2017-11-30 21:01:25 +0000 | [diff] [blame] | 1159 | fu_device_set_status (device, FWUPD_STATUS_SCHEDULING); |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 1160 | if (!g_file_set_contents (filename, |
| 1161 | g_bytes_get_data (blob_cab, NULL), |
| 1162 | (gssize) g_bytes_get_size (blob_cab), |
| 1163 | error)) |
| 1164 | return FALSE; |
| 1165 | |
| 1166 | /* schedule for next boot */ |
| 1167 | g_debug ("schedule %s to be installed to %s on next boot", |
| 1168 | filename, fu_device_get_id (device)); |
Richard Hughes | 38fb56c | 2018-02-01 22:07:52 +0000 | [diff] [blame] | 1169 | release = fu_device_get_release_default (device); |
| 1170 | fwupd_release_set_version (release_tmp, fwupd_release_get_version (release)); |
| 1171 | fwupd_release_set_filename (release_tmp, filename); |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 1172 | |
| 1173 | /* add to database */ |
Richard Hughes | 3e90a58 | 2018-01-06 22:38:09 +0000 | [diff] [blame] | 1174 | fu_device_set_update_state (device, FWUPD_UPDATE_STATE_PENDING); |
Richard Hughes | 38fb56c | 2018-02-01 22:07:52 +0000 | [diff] [blame] | 1175 | if (!fu_history_add_device (history, device, release_tmp, error)) |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 1176 | return FALSE; |
| 1177 | |
| 1178 | /* next boot we run offline */ |
| 1179 | return fu_plugin_runner_offline_setup (error); |
| 1180 | } |
| 1181 | |
| 1182 | gboolean |
| 1183 | fu_plugin_runner_verify (FuPlugin *plugin, |
| 1184 | FuDevice *device, |
| 1185 | FuPluginVerifyFlags flags, |
| 1186 | GError **error) |
| 1187 | { |
| 1188 | FuPluginPrivate *priv = GET_PRIVATE (plugin); |
Richard Hughes | 7b8b202 | 2016-12-12 16:15:03 +0000 | [diff] [blame] | 1189 | FuPluginVerifyFunc func = NULL; |
Richard Hughes | ababbb7 | 2017-06-15 20:18:36 +0100 | [diff] [blame] | 1190 | GPtrArray *checksums; |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 1191 | |
| 1192 | /* not enabled */ |
| 1193 | if (!priv->enabled) |
| 1194 | return TRUE; |
| 1195 | |
Richard Hughes | 639da47 | 2018-01-06 22:35:04 +0000 | [diff] [blame] | 1196 | /* no object loaded */ |
| 1197 | if (priv->module == NULL) |
| 1198 | return TRUE; |
| 1199 | |
Richard Hughes | ababbb7 | 2017-06-15 20:18:36 +0100 | [diff] [blame] | 1200 | /* clear any existing verification checksums */ |
| 1201 | checksums = fu_device_get_checksums (device); |
| 1202 | g_ptr_array_set_size (checksums, 0); |
| 1203 | |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 1204 | /* optional */ |
| 1205 | g_module_symbol (priv->module, "fu_plugin_verify", (gpointer *) &func); |
| 1206 | if (func == NULL) |
| 1207 | return TRUE; |
| 1208 | g_debug ("performing verify() on %s", priv->name); |
| 1209 | if (!func (plugin, device, flags, error)) { |
| 1210 | g_prefix_error (error, "failed to verify %s: ", priv->name); |
Richard Hughes | d090514 | 2016-03-13 09:46:49 +0000 | [diff] [blame] | 1211 | return FALSE; |
| 1212 | } |
| 1213 | return TRUE; |
| 1214 | } |
| 1215 | |
Richard Hughes | d090514 | 2016-03-13 09:46:49 +0000 | [diff] [blame] | 1216 | gboolean |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 1217 | fu_plugin_runner_unlock (FuPlugin *plugin, FuDevice *device, GError **error) |
Richard Hughes | d090514 | 2016-03-13 09:46:49 +0000 | [diff] [blame] | 1218 | { |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 1219 | guint64 flags; |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 1220 | |
| 1221 | /* final check */ |
| 1222 | flags = fu_device_get_flags (device); |
| 1223 | if ((flags & FWUPD_DEVICE_FLAG_LOCKED) == 0) { |
| 1224 | g_set_error (error, |
| 1225 | FWUPD_ERROR, |
| 1226 | FWUPD_ERROR_NOT_SUPPORTED, |
| 1227 | "Device %s is not locked", |
| 1228 | fu_device_get_id (device)); |
| 1229 | return FALSE; |
| 1230 | } |
| 1231 | |
Richard Hughes | 9c4b531 | 2017-11-14 11:34:53 +0000 | [diff] [blame] | 1232 | /* run vfunc */ |
| 1233 | if (!fu_plugin_runner_device_generic (plugin, device, |
| 1234 | "fu_plugin_unlock", error)) |
| 1235 | return FALSE; |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 1236 | |
| 1237 | /* update with correct flags */ |
| 1238 | flags = fu_device_get_flags (device); |
| 1239 | fu_device_set_flags (device, flags &= ~FWUPD_DEVICE_FLAG_LOCKED); |
| 1240 | fu_device_set_modified (device, (guint64) g_get_real_time () / G_USEC_PER_SEC); |
| 1241 | return TRUE; |
| 1242 | } |
| 1243 | |
| 1244 | gboolean |
| 1245 | fu_plugin_runner_update (FuPlugin *plugin, |
Richard Hughes | a785a1c | 2017-08-25 16:00:58 +0100 | [diff] [blame] | 1246 | FuDevice *device, |
| 1247 | GBytes *blob_cab, |
| 1248 | GBytes *blob_fw, |
| 1249 | FwupdInstallFlags flags, |
| 1250 | GError **error) |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 1251 | { |
| 1252 | FuPluginPrivate *priv = GET_PRIVATE (plugin); |
Richard Hughes | a785a1c | 2017-08-25 16:00:58 +0100 | [diff] [blame] | 1253 | FuPluginUpdateFunc update_func; |
Richard Hughes | 780ef3f | 2018-01-12 16:20:31 +0000 | [diff] [blame] | 1254 | g_autoptr(FuHistory) history = NULL; |
Richard Hughes | 68982c6 | 2017-09-13 15:40:14 +0100 | [diff] [blame] | 1255 | g_autoptr(FuDevice) device_pending = NULL; |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 1256 | GError *error_update = NULL; |
Richard Hughes | f556d37 | 2017-06-15 19:49:18 +0100 | [diff] [blame] | 1257 | GPtrArray *checksums; |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 1258 | |
| 1259 | /* not enabled */ |
Richard Hughes | 41c1548 | 2018-02-01 22:07:21 +0000 | [diff] [blame] | 1260 | if (!priv->enabled) { |
| 1261 | g_debug ("plugin not enabled, skipping"); |
Richard Hughes | d090514 | 2016-03-13 09:46:49 +0000 | [diff] [blame] | 1262 | return TRUE; |
Richard Hughes | 41c1548 | 2018-02-01 22:07:21 +0000 | [diff] [blame] | 1263 | } |
Richard Hughes | d090514 | 2016-03-13 09:46:49 +0000 | [diff] [blame] | 1264 | |
Richard Hughes | 639da47 | 2018-01-06 22:35:04 +0000 | [diff] [blame] | 1265 | /* no object loaded */ |
Richard Hughes | 41c1548 | 2018-02-01 22:07:21 +0000 | [diff] [blame] | 1266 | if (priv->module == NULL) { |
| 1267 | g_debug ("module not enabled, skipping"); |
Richard Hughes | 639da47 | 2018-01-06 22:35:04 +0000 | [diff] [blame] | 1268 | return TRUE; |
Richard Hughes | 41c1548 | 2018-02-01 22:07:21 +0000 | [diff] [blame] | 1269 | } |
Richard Hughes | 639da47 | 2018-01-06 22:35:04 +0000 | [diff] [blame] | 1270 | |
Richard Hughes | d090514 | 2016-03-13 09:46:49 +0000 | [diff] [blame] | 1271 | /* optional */ |
Richard Hughes | a785a1c | 2017-08-25 16:00:58 +0100 | [diff] [blame] | 1272 | g_module_symbol (priv->module, "fu_plugin_update", (gpointer *) &update_func); |
| 1273 | if (update_func == NULL) { |
| 1274 | g_set_error_literal (error, |
| 1275 | FWUPD_ERROR, |
| 1276 | FWUPD_ERROR_NOT_SUPPORTED, |
| 1277 | "No update possible"); |
| 1278 | return FALSE; |
| 1279 | } |
Richard Hughes | d090514 | 2016-03-13 09:46:49 +0000 | [diff] [blame] | 1280 | |
Richard Hughes | a785a1c | 2017-08-25 16:00:58 +0100 | [diff] [blame] | 1281 | /* just schedule this for the next reboot */ |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 1282 | if (flags & FWUPD_INSTALL_FLAG_OFFLINE) { |
Richard Hughes | 026cdd8 | 2018-05-18 10:23:19 +0100 | [diff] [blame] | 1283 | if (blob_cab == NULL) { |
| 1284 | g_set_error_literal (error, |
| 1285 | FWUPD_ERROR, |
| 1286 | FWUPD_ERROR_NOT_SUPPORTED, |
| 1287 | "No cabinet archive to schedule"); |
| 1288 | return FALSE; |
| 1289 | } |
Richard Hughes | a785a1c | 2017-08-25 16:00:58 +0100 | [diff] [blame] | 1290 | return fu_plugin_runner_schedule_update (plugin, |
| 1291 | device, |
| 1292 | blob_cab, |
| 1293 | error); |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 1294 | } |
| 1295 | |
| 1296 | /* cancel the pending action */ |
| 1297 | if (!fu_plugin_runner_offline_invalidate (error)) |
| 1298 | return FALSE; |
| 1299 | |
| 1300 | /* online */ |
Richard Hughes | 780ef3f | 2018-01-12 16:20:31 +0000 | [diff] [blame] | 1301 | history = fu_history_new (); |
Richard Hughes | 0b9d996 | 2018-01-12 16:31:28 +0000 | [diff] [blame] | 1302 | device_pending = fu_history_get_device_by_id (history, fu_device_get_id (device), NULL); |
Richard Hughes | a785a1c | 2017-08-25 16:00:58 +0100 | [diff] [blame] | 1303 | if (!update_func (plugin, device, blob_fw, flags, &error_update)) { |
Richard Hughes | c0cd023 | 2018-01-31 15:02:00 +0000 | [diff] [blame] | 1304 | fu_device_set_update_error (device, error_update->message); |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 1305 | g_propagate_error (error, error_update); |
| 1306 | return FALSE; |
| 1307 | } |
| 1308 | |
Richard Hughes | f556d37 | 2017-06-15 19:49:18 +0100 | [diff] [blame] | 1309 | /* no longer valid */ |
| 1310 | checksums = fu_device_get_checksums (device); |
| 1311 | g_ptr_array_set_size (checksums, 0); |
| 1312 | |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 1313 | /* cleanup */ |
Richard Hughes | 68982c6 | 2017-09-13 15:40:14 +0100 | [diff] [blame] | 1314 | if (device_pending != NULL) { |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 1315 | const gchar *tmp; |
Richard Hughes | bc3a4e1 | 2018-01-06 22:41:47 +0000 | [diff] [blame] | 1316 | FwupdRelease *release; |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 1317 | |
Richard Hughes | 780ef3f | 2018-01-12 16:20:31 +0000 | [diff] [blame] | 1318 | /* update history database */ |
Richard Hughes | c0cd023 | 2018-01-31 15:02:00 +0000 | [diff] [blame] | 1319 | fu_device_set_update_state (device, FWUPD_UPDATE_STATE_SUCCESS); |
| 1320 | if (!fu_history_modify_device (history, device, |
| 1321 | FU_HISTORY_FLAGS_MATCH_NEW_VERSION, |
| 1322 | error)) |
Richard Hughes | 0b9d996 | 2018-01-12 16:31:28 +0000 | [diff] [blame] | 1323 | return FALSE; |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 1324 | |
| 1325 | /* delete cab file */ |
Richard Hughes | bc3a4e1 | 2018-01-06 22:41:47 +0000 | [diff] [blame] | 1326 | release = fu_device_get_release_default (device_pending); |
| 1327 | tmp = fwupd_release_get_filename (release); |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 1328 | if (tmp != NULL && g_str_has_prefix (tmp, LIBEXECDIR)) { |
| 1329 | g_autoptr(GError) error_local = NULL; |
| 1330 | g_autoptr(GFile) file = NULL; |
| 1331 | file = g_file_new_for_path (tmp); |
| 1332 | if (!g_file_delete (file, NULL, &error_local)) { |
| 1333 | g_set_error (error, |
| 1334 | FWUPD_ERROR, |
| 1335 | FWUPD_ERROR_INVALID_FILE, |
| 1336 | "Failed to delete %s: %s", |
| 1337 | tmp, error_local->message); |
| 1338 | return FALSE; |
| 1339 | } |
| 1340 | } |
| 1341 | } |
Richard Hughes | d090514 | 2016-03-13 09:46:49 +0000 | [diff] [blame] | 1342 | return TRUE; |
| 1343 | } |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 1344 | |
| 1345 | gboolean |
| 1346 | fu_plugin_runner_clear_results (FuPlugin *plugin, FuDevice *device, GError **error) |
| 1347 | { |
| 1348 | FuPluginPrivate *priv = GET_PRIVATE (plugin); |
Richard Hughes | 7b8b202 | 2016-12-12 16:15:03 +0000 | [diff] [blame] | 1349 | FuPluginDeviceFunc func = NULL; |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 1350 | |
| 1351 | /* not enabled */ |
| 1352 | if (!priv->enabled) |
| 1353 | return TRUE; |
| 1354 | |
Richard Hughes | 639da47 | 2018-01-06 22:35:04 +0000 | [diff] [blame] | 1355 | /* no object loaded */ |
| 1356 | if (priv->module == NULL) |
| 1357 | return TRUE; |
| 1358 | |
Richard Hughes | 65e44ca | 2018-01-30 17:26:30 +0000 | [diff] [blame] | 1359 | /* optional */ |
| 1360 | g_module_symbol (priv->module, "fu_plugin_get_results", (gpointer *) &func); |
| 1361 | if (func == NULL) |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 1362 | return TRUE; |
Richard Hughes | 65e44ca | 2018-01-30 17:26:30 +0000 | [diff] [blame] | 1363 | g_debug ("performing clear_result() on %s", priv->name); |
| 1364 | if (!func (plugin, device, error)) { |
| 1365 | g_prefix_error (error, "failed to clear_result %s: ", priv->name); |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 1366 | return FALSE; |
| 1367 | } |
Richard Hughes | 65e44ca | 2018-01-30 17:26:30 +0000 | [diff] [blame] | 1368 | return TRUE; |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 1369 | } |
| 1370 | |
| 1371 | gboolean |
| 1372 | fu_plugin_runner_get_results (FuPlugin *plugin, FuDevice *device, GError **error) |
| 1373 | { |
| 1374 | FuPluginPrivate *priv = GET_PRIVATE (plugin); |
Richard Hughes | 7b8b202 | 2016-12-12 16:15:03 +0000 | [diff] [blame] | 1375 | FuPluginDeviceFunc func = NULL; |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 1376 | |
| 1377 | /* not enabled */ |
| 1378 | if (!priv->enabled) |
| 1379 | return TRUE; |
| 1380 | |
Richard Hughes | 639da47 | 2018-01-06 22:35:04 +0000 | [diff] [blame] | 1381 | /* no object loaded */ |
| 1382 | if (priv->module == NULL) |
| 1383 | return TRUE; |
| 1384 | |
Richard Hughes | 65e44ca | 2018-01-30 17:26:30 +0000 | [diff] [blame] | 1385 | /* optional */ |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 1386 | g_module_symbol (priv->module, "fu_plugin_get_results", (gpointer *) &func); |
Richard Hughes | 65e44ca | 2018-01-30 17:26:30 +0000 | [diff] [blame] | 1387 | if (func == NULL) |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 1388 | return TRUE; |
Richard Hughes | 65e44ca | 2018-01-30 17:26:30 +0000 | [diff] [blame] | 1389 | g_debug ("performing get_results() on %s", priv->name); |
| 1390 | if (!func (plugin, device, error)) { |
| 1391 | g_prefix_error (error, "failed to get_results %s: ", priv->name); |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 1392 | return FALSE; |
| 1393 | } |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 1394 | return TRUE; |
| 1395 | } |
| 1396 | |
Richard Hughes | 08a3799 | 2017-09-12 12:57:43 +0100 | [diff] [blame] | 1397 | /** |
| 1398 | * fu_plugin_get_order: |
| 1399 | * @plugin: a #FuPlugin |
| 1400 | * |
| 1401 | * Gets the plugin order, where higher numbers are run after lower |
| 1402 | * numbers. |
| 1403 | * |
| 1404 | * Returns: the integer value |
| 1405 | **/ |
| 1406 | guint |
| 1407 | fu_plugin_get_order (FuPlugin *plugin) |
| 1408 | { |
| 1409 | FuPluginPrivate *priv = fu_plugin_get_instance_private (plugin); |
| 1410 | return priv->order; |
| 1411 | } |
| 1412 | |
| 1413 | /** |
| 1414 | * fu_plugin_set_order: |
| 1415 | * @plugin: a #FuPlugin |
| 1416 | * @order: a integer value |
| 1417 | * |
| 1418 | * Sets the plugin order, where higher numbers are run after lower |
| 1419 | * numbers. |
| 1420 | **/ |
| 1421 | void |
| 1422 | fu_plugin_set_order (FuPlugin *plugin, guint order) |
| 1423 | { |
| 1424 | FuPluginPrivate *priv = fu_plugin_get_instance_private (plugin); |
| 1425 | priv->order = order; |
| 1426 | } |
| 1427 | |
| 1428 | /** |
| 1429 | * fu_plugin_add_rule: |
| 1430 | * @plugin: a #FuPlugin |
| 1431 | * @rule: a #FuPluginRule, e.g. %FU_PLUGIN_RULE_CONFLICTS |
Richard Hughes | 4eada34 | 2017-10-03 21:20:32 +0100 | [diff] [blame] | 1432 | * @name: a plugin name, e.g. `upower` |
Richard Hughes | 08a3799 | 2017-09-12 12:57:43 +0100 | [diff] [blame] | 1433 | * |
| 1434 | * If the plugin name is found, the rule will be used to sort the plugin list, |
| 1435 | * for example the plugin specified by @name will be ordered after this plugin |
| 1436 | * when %FU_PLUGIN_RULE_RUN_AFTER is used. |
| 1437 | * |
| 1438 | * NOTE: The depsolver is iterative and may not solve overly-complicated rules; |
| 1439 | * If depsolving fails then fwupd will not start. |
| 1440 | **/ |
| 1441 | void |
| 1442 | fu_plugin_add_rule (FuPlugin *plugin, FuPluginRule rule, const gchar *name) |
| 1443 | { |
| 1444 | FuPluginPrivate *priv = fu_plugin_get_instance_private (plugin); |
| 1445 | g_ptr_array_add (priv->rules[rule], g_strdup (name)); |
| 1446 | } |
| 1447 | |
| 1448 | /** |
| 1449 | * fu_plugin_get_rules: |
| 1450 | * @plugin: a #FuPlugin |
| 1451 | * @rule: a #FuPluginRule, e.g. %FU_PLUGIN_RULE_CONFLICTS |
| 1452 | * |
| 1453 | * Gets the plugin IDs that should be run after this plugin. |
| 1454 | * |
| 1455 | * Returns: (element-type utf8) (transfer none): the list of plugin names, e.g. ['appstream'] |
| 1456 | **/ |
| 1457 | GPtrArray * |
| 1458 | fu_plugin_get_rules (FuPlugin *plugin, FuPluginRule rule) |
| 1459 | { |
| 1460 | FuPluginPrivate *priv = fu_plugin_get_instance_private (plugin); |
| 1461 | return priv->rules[rule]; |
| 1462 | } |
| 1463 | |
Richard Hughes | 80b79bb | 2018-01-11 21:11:06 +0000 | [diff] [blame] | 1464 | /** |
| 1465 | * fu_plugin_add_report_metadata: |
| 1466 | * @plugin: a #FuPlugin |
| 1467 | * @key: a string, e.g. `FwupdateVersion` |
| 1468 | * @value: a string, e.g. `10` |
| 1469 | * |
| 1470 | * Sets any additional metadata to be included in the firmware report to aid |
| 1471 | * debugging problems. |
| 1472 | * |
| 1473 | * Any data included here will be sent to the metadata server after user |
| 1474 | * confirmation. |
| 1475 | **/ |
| 1476 | void |
| 1477 | fu_plugin_add_report_metadata (FuPlugin *plugin, const gchar *key, const gchar *value) |
| 1478 | { |
| 1479 | FuPluginPrivate *priv = fu_plugin_get_instance_private (plugin); |
| 1480 | g_hash_table_insert (priv->report_metadata, g_strdup (key), g_strdup (value)); |
| 1481 | } |
| 1482 | |
| 1483 | /** |
| 1484 | * fu_plugin_get_report_metadata: |
| 1485 | * @plugin: a #FuPlugin |
| 1486 | * |
| 1487 | * Returns the list of additional metadata to be added when filing a report. |
| 1488 | * |
| 1489 | * Returns: (transfer none): the map of report metadata |
| 1490 | **/ |
| 1491 | GHashTable * |
| 1492 | fu_plugin_get_report_metadata (FuPlugin *plugin) |
| 1493 | { |
| 1494 | FuPluginPrivate *priv = fu_plugin_get_instance_private (plugin); |
| 1495 | return priv->report_metadata; |
| 1496 | } |
| 1497 | |
Mario Limonciello | 963dc42 | 2018-02-27 14:26:58 -0600 | [diff] [blame] | 1498 | /** |
| 1499 | * fu_plugin_get_config_value: |
| 1500 | * @plugin: a #FuPlugin |
| 1501 | * @key: A settings key |
| 1502 | * |
| 1503 | * Return the value of a key if it's been configured |
| 1504 | * |
| 1505 | * Since: 1.0.6 |
| 1506 | **/ |
| 1507 | gchar * |
| 1508 | fu_plugin_get_config_value (FuPlugin *plugin, const gchar *key) |
| 1509 | { |
Richard Hughes | 4be17d1 | 2018-05-30 20:36:29 +0100 | [diff] [blame] | 1510 | g_autofree gchar *conf_dir = NULL; |
Mario Limonciello | 963dc42 | 2018-02-27 14:26:58 -0600 | [diff] [blame] | 1511 | g_autofree gchar *conf_file = NULL; |
| 1512 | g_autofree gchar *conf_path = NULL; |
| 1513 | g_autoptr(GKeyFile) keyfile = NULL; |
| 1514 | const gchar *plugin_name; |
| 1515 | |
Richard Hughes | 4be17d1 | 2018-05-30 20:36:29 +0100 | [diff] [blame] | 1516 | conf_dir = fu_common_get_path (FU_PATH_KIND_SYSCONFDIR_PKG); |
Mario Limonciello | 963dc42 | 2018-02-27 14:26:58 -0600 | [diff] [blame] | 1517 | plugin_name = fu_plugin_get_name (plugin); |
| 1518 | conf_file = g_strdup_printf ("%s.conf", plugin_name); |
Richard Hughes | 4be17d1 | 2018-05-30 20:36:29 +0100 | [diff] [blame] | 1519 | conf_path = g_build_filename (conf_dir, conf_file, NULL); |
Mario Limonciello | 963dc42 | 2018-02-27 14:26:58 -0600 | [diff] [blame] | 1520 | if (!g_file_test (conf_path, G_FILE_TEST_IS_REGULAR)) |
| 1521 | return NULL; |
| 1522 | keyfile = g_key_file_new (); |
| 1523 | if (!g_key_file_load_from_file (keyfile, conf_path, |
| 1524 | G_KEY_FILE_NONE, NULL)) |
| 1525 | return NULL; |
| 1526 | return g_key_file_get_string (keyfile, plugin_name, key, NULL); |
| 1527 | } |
| 1528 | |
Richard Hughes | 8c71a3f | 2018-05-22 19:19:52 +0100 | [diff] [blame] | 1529 | /** |
| 1530 | * fu_plugin_name_compare: |
| 1531 | * @plugin1: first #FuPlugin to compare. |
| 1532 | * @plugin2: second #FuPlugin to compare. |
| 1533 | * |
| 1534 | * Compares two plugins by their names. |
| 1535 | * |
| 1536 | * Returns: 1, 0 or -1 if @plugin1 is greater, equal, or less than @plugin2. |
| 1537 | **/ |
| 1538 | gint |
| 1539 | fu_plugin_name_compare (FuPlugin *plugin1, FuPlugin *plugin2) |
| 1540 | { |
| 1541 | FuPluginPrivate *priv1 = fu_plugin_get_instance_private (plugin1); |
| 1542 | FuPluginPrivate *priv2 = fu_plugin_get_instance_private (plugin2); |
| 1543 | return g_strcmp0 (priv1->name, priv2->name); |
| 1544 | } |
| 1545 | |
| 1546 | /** |
| 1547 | * fu_plugin_order_compare: |
| 1548 | * @plugin1: first #FuPlugin to compare. |
| 1549 | * @plugin2: second #FuPlugin to compare. |
| 1550 | * |
| 1551 | * Compares two plugins by their depsolved order. |
| 1552 | * |
| 1553 | * Returns: 1, 0 or -1 if @plugin1 is greater, equal, or less than @plugin2. |
| 1554 | **/ |
| 1555 | gint |
| 1556 | fu_plugin_order_compare (FuPlugin *plugin1, FuPlugin *plugin2) |
| 1557 | { |
| 1558 | FuPluginPrivate *priv1 = fu_plugin_get_instance_private (plugin1); |
| 1559 | FuPluginPrivate *priv2 = fu_plugin_get_instance_private (plugin2); |
| 1560 | if (priv1->order < priv2->order) |
| 1561 | return -1; |
| 1562 | if (priv1->order > priv2->order) |
| 1563 | return 1; |
| 1564 | return 0; |
| 1565 | } |
| 1566 | |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 1567 | static void |
| 1568 | fu_plugin_class_init (FuPluginClass *klass) |
| 1569 | { |
| 1570 | GObjectClass *object_class = G_OBJECT_CLASS (klass); |
| 1571 | object_class->finalize = fu_plugin_finalize; |
| 1572 | signals[SIGNAL_DEVICE_ADDED] = |
| 1573 | g_signal_new ("device-added", |
| 1574 | G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST, |
| 1575 | G_STRUCT_OFFSET (FuPluginClass, device_added), |
| 1576 | NULL, NULL, g_cclosure_marshal_VOID__OBJECT, |
| 1577 | G_TYPE_NONE, 1, FU_TYPE_DEVICE); |
| 1578 | signals[SIGNAL_DEVICE_REMOVED] = |
| 1579 | g_signal_new ("device-removed", |
| 1580 | G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST, |
| 1581 | G_STRUCT_OFFSET (FuPluginClass, device_removed), |
| 1582 | NULL, NULL, g_cclosure_marshal_VOID__OBJECT, |
| 1583 | G_TYPE_NONE, 1, FU_TYPE_DEVICE); |
Richard Hughes | e1fd34d | 2017-08-24 14:19:51 +0100 | [diff] [blame] | 1584 | signals[SIGNAL_DEVICE_REGISTER] = |
| 1585 | g_signal_new ("device-register", |
| 1586 | G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST, |
| 1587 | G_STRUCT_OFFSET (FuPluginClass, device_register), |
| 1588 | NULL, NULL, g_cclosure_marshal_VOID__OBJECT, |
| 1589 | G_TYPE_NONE, 1, FU_TYPE_DEVICE); |
Richard Hughes | 362d6d7 | 2017-01-07 21:42:14 +0000 | [diff] [blame] | 1590 | signals[SIGNAL_RECOLDPLUG] = |
| 1591 | g_signal_new ("recoldplug", |
| 1592 | G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST, |
| 1593 | G_STRUCT_OFFSET (FuPluginClass, recoldplug), |
| 1594 | NULL, NULL, g_cclosure_marshal_VOID__VOID, |
| 1595 | G_TYPE_NONE, 0); |
Richard Hughes | b082903 | 2017-01-10 09:27:08 +0000 | [diff] [blame] | 1596 | signals[SIGNAL_SET_COLDPLUG_DELAY] = |
| 1597 | g_signal_new ("set-coldplug-delay", |
| 1598 | G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST, |
| 1599 | G_STRUCT_OFFSET (FuPluginClass, set_coldplug_delay), |
| 1600 | NULL, NULL, g_cclosure_marshal_VOID__UINT, |
| 1601 | G_TYPE_NONE, 1, G_TYPE_UINT); |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 1602 | } |
| 1603 | |
| 1604 | static void |
| 1605 | fu_plugin_init (FuPlugin *plugin) |
| 1606 | { |
| 1607 | FuPluginPrivate *priv = GET_PRIVATE (plugin); |
| 1608 | priv->enabled = TRUE; |
| 1609 | priv->devices = g_hash_table_new_full (g_str_hash, g_str_equal, |
| 1610 | g_free, (GDestroyNotify) g_object_unref); |
Richard Hughes | e8b5db6 | 2017-07-17 14:18:22 +0100 | [diff] [blame] | 1611 | priv->devices_delay = g_hash_table_new (g_direct_hash, g_direct_equal); |
Richard Hughes | 80b79bb | 2018-01-11 21:11:06 +0000 | [diff] [blame] | 1612 | priv->report_metadata = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free); |
Richard Hughes | 08a3799 | 2017-09-12 12:57:43 +0100 | [diff] [blame] | 1613 | for (guint i = 0; i < FU_PLUGIN_RULE_LAST; i++) |
| 1614 | priv->rules[i] = g_ptr_array_new_with_free_func (g_free); |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 1615 | } |
| 1616 | |
| 1617 | static void |
| 1618 | fu_plugin_finalize (GObject *object) |
| 1619 | { |
| 1620 | FuPlugin *plugin = FU_PLUGIN (object); |
| 1621 | FuPluginPrivate *priv = GET_PRIVATE (plugin); |
| 1622 | FuPluginInitFunc func = NULL; |
| 1623 | |
| 1624 | /* optional */ |
| 1625 | if (priv->module != NULL) { |
| 1626 | g_module_symbol (priv->module, "fu_plugin_destroy", (gpointer *) &func); |
| 1627 | if (func != NULL) { |
| 1628 | g_debug ("performing destroy() on %s", priv->name); |
| 1629 | func (plugin); |
| 1630 | } |
| 1631 | } |
| 1632 | |
Richard Hughes | 08a3799 | 2017-09-12 12:57:43 +0100 | [diff] [blame] | 1633 | for (guint i = 0; i < FU_PLUGIN_RULE_LAST; i++) |
| 1634 | g_ptr_array_unref (priv->rules[i]); |
| 1635 | |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 1636 | if (priv->usb_ctx != NULL) |
| 1637 | g_object_unref (priv->usb_ctx); |
Richard Hughes | b8f8db2 | 2017-04-25 15:56:00 +0100 | [diff] [blame] | 1638 | if (priv->hwids != NULL) |
Richard Hughes | d7704d4 | 2017-08-08 20:29:09 +0100 | [diff] [blame] | 1639 | g_object_unref (priv->hwids); |
Richard Hughes | 9c028f0 | 2017-10-28 21:14:28 +0100 | [diff] [blame] | 1640 | if (priv->quirks != NULL) |
| 1641 | g_object_unref (priv->quirks); |
Richard Hughes | 1354ea9 | 2017-09-19 15:58:31 +0100 | [diff] [blame] | 1642 | if (priv->supported_guids != NULL) |
| 1643 | g_ptr_array_unref (priv->supported_guids); |
Richard Hughes | 49e5e05 | 2017-09-03 12:15:41 +0100 | [diff] [blame] | 1644 | if (priv->smbios != NULL) |
| 1645 | g_object_unref (priv->smbios); |
Richard Hughes | 275d3b4 | 2018-04-20 16:40:37 +0100 | [diff] [blame] | 1646 | if (priv->runtime_versions != NULL) |
| 1647 | g_hash_table_unref (priv->runtime_versions); |
Richard Hughes | 34e0dab | 2018-04-20 16:43:00 +0100 | [diff] [blame] | 1648 | if (priv->compile_versions != NULL) |
| 1649 | g_hash_table_unref (priv->compile_versions); |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 1650 | g_hash_table_unref (priv->devices); |
Richard Hughes | ae3d65f | 2016-12-16 09:38:01 +0000 | [diff] [blame] | 1651 | g_hash_table_unref (priv->devices_delay); |
Richard Hughes | 80b79bb | 2018-01-11 21:11:06 +0000 | [diff] [blame] | 1652 | g_hash_table_unref (priv->report_metadata); |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 1653 | g_free (priv->name); |
| 1654 | g_free (priv->data); |
Mario Limonciello | 3f9a1c1 | 2018-06-06 14:06:40 -0500 | [diff] [blame] | 1655 | /* Must happen as the last step to avoid prematurely |
| 1656 | * freeing memory held by the plugin */ |
| 1657 | #ifndef RUNNING_ON_VALGRIND |
| 1658 | if (priv->module != NULL) |
| 1659 | g_module_close (priv->module); |
| 1660 | #endif |
Richard Hughes | cff38bc | 2016-12-12 12:03:37 +0000 | [diff] [blame] | 1661 | |
| 1662 | G_OBJECT_CLASS (fu_plugin_parent_class)->finalize (object); |
| 1663 | } |
| 1664 | |
| 1665 | FuPlugin * |
| 1666 | fu_plugin_new (void) |
| 1667 | { |
| 1668 | FuPlugin *plugin; |
| 1669 | plugin = g_object_new (FU_TYPE_PLUGIN, NULL); |
| 1670 | return plugin; |
| 1671 | } |