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 | * |
| 3 | * Copyright (C) 2016 Richard Hughes <richard@hughsie.com> |
| 4 | * |
| 5 | * Licensed under the GNU General Public License Version 2 |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this program; if not, write to the Free Software |
| 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 | */ |
| 21 | |
| 22 | #include "config.h" |
| 23 | |
| 24 | #include <gio/gio.h> |
| 25 | |
| 26 | #include "fu-device.h" |
| 27 | #include "fu-plugin.h" |
| 28 | |
Richard Hughes | d090514 | 2016-03-13 09:46:49 +0000 | [diff] [blame] | 29 | FuPlugin * |
| 30 | fu_plugin_new (GModule *module) |
| 31 | { |
| 32 | FuPlugin *plugin; |
| 33 | FuPluginInitFunc func; |
| 34 | FuPluginGetNameFunc plugin_name = NULL; |
| 35 | gboolean ret; |
| 36 | |
| 37 | /* get description */ |
| 38 | ret = g_module_symbol (module, |
| 39 | "fu_plugin_get_name", |
| 40 | (gpointer *) &plugin_name); |
| 41 | if (!ret) |
| 42 | return NULL; |
| 43 | |
| 44 | /* add to registered plugins */ |
| 45 | plugin = g_new0 (FuPlugin, 1); |
| 46 | plugin->enabled = TRUE; |
| 47 | plugin->module = module; |
| 48 | plugin->name = g_strdup (plugin_name ()); |
| 49 | |
| 50 | /* optional */ |
| 51 | if (g_module_symbol (plugin->module, |
| 52 | "fu_plugin_init", (gpointer *) &func)) { |
| 53 | g_debug ("performing init() on %s", plugin->name); |
| 54 | func (plugin); |
| 55 | } |
| 56 | return plugin; |
| 57 | } |
| 58 | |
Richard Hughes | d090514 | 2016-03-13 09:46:49 +0000 | [diff] [blame] | 59 | void |
| 60 | fu_plugin_free (FuPlugin *plugin) |
| 61 | { |
| 62 | FuPluginInitFunc func; |
| 63 | |
| 64 | /* optional */ |
| 65 | if (g_module_symbol (plugin->module, |
| 66 | "fu_plugin_destroy", (gpointer *) &func)) { |
| 67 | g_debug ("performing destroy() on %s", plugin->name); |
| 68 | func (plugin); |
| 69 | } |
| 70 | |
| 71 | /* deallocate */ |
| 72 | g_module_close (plugin->module); |
| 73 | g_free (plugin->name); |
| 74 | g_free (plugin->priv); |
| 75 | g_free (plugin); |
| 76 | } |
| 77 | |
Richard Hughes | d090514 | 2016-03-13 09:46:49 +0000 | [diff] [blame] | 78 | gboolean |
| 79 | fu_plugin_run_startup (FuPlugin *plugin, GError **error) |
| 80 | { |
| 81 | FuPluginStartupFunc func; |
| 82 | |
| 83 | /* not enabled */ |
| 84 | if (!plugin->enabled) |
| 85 | return TRUE; |
| 86 | |
| 87 | /* optional */ |
| 88 | if (!g_module_symbol (plugin->module, |
| 89 | "fu_plugin_startup", (gpointer *) &func)) |
| 90 | return TRUE; |
| 91 | g_debug ("performing startup() on %s", plugin->name); |
| 92 | if (!func (plugin, error)) { |
| 93 | g_prefix_error (error, "failed to startup %s: ", plugin->name); |
| 94 | return FALSE; |
| 95 | } |
| 96 | return TRUE; |
| 97 | } |
| 98 | |
Richard Hughes | d090514 | 2016-03-13 09:46:49 +0000 | [diff] [blame] | 99 | gboolean |
| 100 | fu_plugin_run_device_probe (FuPlugin *plugin, FuDevice *device, GError **error) |
| 101 | { |
| 102 | FuPluginDeviceProbeFunc func; |
| 103 | |
| 104 | /* not enabled */ |
| 105 | if (!plugin->enabled) |
| 106 | return TRUE; |
| 107 | |
| 108 | /* optional */ |
| 109 | if (!g_module_symbol (plugin->module, |
| 110 | "fu_plugin_device_probe", (gpointer *) &func)) |
| 111 | return TRUE; |
| 112 | g_debug ("performing device_probe() on %s", plugin->name); |
| 113 | if (!func (plugin, device, error)) { |
| 114 | g_prefix_error (error, "failed to device_probe %s: ", plugin->name); |
| 115 | return FALSE; |
| 116 | } |
| 117 | return TRUE; |
| 118 | } |
| 119 | |
Richard Hughes | d090514 | 2016-03-13 09:46:49 +0000 | [diff] [blame] | 120 | gboolean |
| 121 | fu_plugin_run_device_update (FuPlugin *plugin, FuDevice *device, |
| 122 | GBytes *data, GError **error) |
| 123 | { |
| 124 | FuPluginDeviceUpdateFunc func; |
| 125 | |
| 126 | /* not enabled */ |
| 127 | if (!plugin->enabled) |
| 128 | return TRUE; |
| 129 | |
| 130 | /* optional */ |
| 131 | if (!g_module_symbol (plugin->module, |
| 132 | "fu_plugin_device_update", (gpointer *) &func)) |
| 133 | return TRUE; |
| 134 | |
| 135 | /* does a vendor plugin exist */ |
| 136 | g_debug ("performing device_update() on %s", plugin->name); |
| 137 | if (!func (plugin, device, data, error)) { |
| 138 | g_prefix_error (error, "failed to device_update %s: ", plugin->name); |
| 139 | return FALSE; |
| 140 | } |
| 141 | return TRUE; |
| 142 | } |