blob: c67a6d37a5e307b143c26b57872a38a921044d8d [file] [log] [blame]
Richard Hughesd0905142016-03-13 09:46:49 +00001/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
2 *
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 Hughesd0905142016-03-13 09:46:49 +000029FuPlugin *
30fu_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 Hughesbc93e4a2016-12-08 17:29:51 +000059GUsbContext *
60fu_plugin_get_usb_context (FuPlugin *plugin)
61{
62 return g_object_ref (plugin->usb_ctx);
63}
64
65void
66fu_plugin_set_usb_context (FuPlugin *plugin, GUsbContext *usb_ctx)
67{
68 g_set_object (&plugin->usb_ctx, usb_ctx);
69}
70
Richard Hughesd0905142016-03-13 09:46:49 +000071void
72fu_plugin_free (FuPlugin *plugin)
73{
74 FuPluginInitFunc func;
75
76 /* optional */
77 if (g_module_symbol (plugin->module,
78 "fu_plugin_destroy", (gpointer *) &func)) {
79 g_debug ("performing destroy() on %s", plugin->name);
80 func (plugin);
81 }
82
83 /* deallocate */
Richard Hughesbc93e4a2016-12-08 17:29:51 +000084 if (plugin->usb_ctx != NULL)
85 g_object_unref (plugin->usb_ctx);
Richard Hughesd0905142016-03-13 09:46:49 +000086 g_module_close (plugin->module);
87 g_free (plugin->name);
88 g_free (plugin->priv);
89 g_free (plugin);
90}
91
Richard Hughesd0905142016-03-13 09:46:49 +000092gboolean
93fu_plugin_run_startup (FuPlugin *plugin, GError **error)
94{
95 FuPluginStartupFunc func;
96
97 /* not enabled */
98 if (!plugin->enabled)
99 return TRUE;
100
101 /* optional */
102 if (!g_module_symbol (plugin->module,
103 "fu_plugin_startup", (gpointer *) &func))
104 return TRUE;
105 g_debug ("performing startup() on %s", plugin->name);
106 if (!func (plugin, error)) {
107 g_prefix_error (error, "failed to startup %s: ", plugin->name);
108 return FALSE;
109 }
110 return TRUE;
111}
112
Richard Hughesd0905142016-03-13 09:46:49 +0000113gboolean
114fu_plugin_run_device_probe (FuPlugin *plugin, FuDevice *device, GError **error)
115{
116 FuPluginDeviceProbeFunc func;
117
118 /* not enabled */
119 if (!plugin->enabled)
120 return TRUE;
121
122 /* optional */
123 if (!g_module_symbol (plugin->module,
124 "fu_plugin_device_probe", (gpointer *) &func))
125 return TRUE;
126 g_debug ("performing device_probe() on %s", plugin->name);
127 if (!func (plugin, device, error)) {
128 g_prefix_error (error, "failed to device_probe %s: ", plugin->name);
129 return FALSE;
130 }
131 return TRUE;
132}
133
Richard Hughesd0905142016-03-13 09:46:49 +0000134gboolean
135fu_plugin_run_device_update (FuPlugin *plugin, FuDevice *device,
136 GBytes *data, GError **error)
137{
138 FuPluginDeviceUpdateFunc func;
139
140 /* not enabled */
141 if (!plugin->enabled)
142 return TRUE;
143
144 /* optional */
145 if (!g_module_symbol (plugin->module,
146 "fu_plugin_device_update", (gpointer *) &func))
147 return TRUE;
148
149 /* does a vendor plugin exist */
150 g_debug ("performing device_update() on %s", plugin->name);
151 if (!func (plugin, device, data, error)) {
152 g_prefix_error (error, "failed to device_update %s: ", plugin->name);
153 return FALSE;
154 }
155 return TRUE;
156}