3 * neard - Near Field Communication manager
5 * Copyright (C) 2011 Intel Corporation. All rights reserved.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
30 #ifdef NEAR_PLUGIN_BUILTIN
31 #undef NEAR_PLUGIN_BUILTIN
36 static GSList *plugins = NULL;
41 struct near_plugin_desc *desc;
44 static gint compare_priority(gconstpointer a, gconstpointer b)
46 const struct near_plugin *plugin1 = a;
47 const struct near_plugin *plugin2 = b;
49 return plugin2->desc->priority - plugin1->desc->priority;
52 static bool add_plugin(void *handle, struct near_plugin_desc *desc)
54 struct near_plugin *plugin;
59 if (!g_str_equal(desc->version, NEAR_VERSION)) {
60 near_error("Version mismatch for %s", desc->description);
64 plugin = g_try_new0(struct near_plugin, 1);
68 plugin->handle = handle;
69 plugin->active = false;
72 plugins = g_slist_insert_sorted(plugins, plugin, compare_priority);
77 static bool check_plugin(struct near_plugin_desc *desc,
78 char **patterns, char **excludes)
81 for (; *excludes; excludes++)
82 if (g_pattern_match_simple(*excludes, desc->name))
85 near_info("Excluding %s", desc->description);
91 for (; *patterns; patterns++)
92 if (g_pattern_match_simple(*patterns, desc->name))
95 near_info("Ignoring %s", desc->description);
105 int __near_plugin_init(const char *pattern, const char *exclude)
107 gchar **patterns = NULL;
108 gchar **excludes = NULL;
118 patterns = g_strsplit_set(pattern, ":, ", -1);
121 excludes = g_strsplit_set(exclude, ":, ", -1);
123 for (i = 0; __near_builtin[i]; i++) {
124 if (!check_plugin(__near_builtin[i], patterns, excludes))
127 add_plugin(NULL, __near_builtin[i]);
130 dir = g_dir_open(PLUGINDIR, 0, NULL);
132 while ((file = g_dir_read_name(dir))) {
134 struct near_plugin_desc *desc;
136 if (g_str_has_prefix(file, "lib") ||
137 !g_str_has_suffix(file, ".so"))
140 filename = g_build_filename(PLUGINDIR, file, NULL);
142 handle = dlopen(filename, RTLD_NOW);
144 near_error("Can't load %s: %s",
145 filename, dlerror());
152 desc = dlsym(handle, "near_plugin_desc");
154 near_error("Can't load symbol: %s",
160 if (!check_plugin(desc, patterns, excludes)) {
165 if (!add_plugin(handle, desc))
172 for (list = plugins; list; list = list->next) {
173 struct near_plugin *plugin = list->data;
175 if (plugin->desc->init() < 0)
178 plugin->active = true;
181 g_strfreev(patterns);
182 g_strfreev(excludes);
187 void __near_plugin_cleanup(void)
193 for (list = plugins; list; list = list->next) {
194 struct near_plugin *plugin = list->data;
196 if (plugin->active && plugin->desc->exit)
197 plugin->desc->exit();
200 dlclose(plugin->handle);
205 g_slist_free(plugins);