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