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
34 #ifdef NEAR_PLUGIN_BUILTIN
35 #undef NEAR_PLUGIN_BUILTIN
40 static GSList *plugins = NULL;
45 struct near_plugin_desc *desc;
48 static gint compare_priority(gconstpointer a, gconstpointer b)
50 const struct near_plugin *plugin1 = a;
51 const struct near_plugin *plugin2 = b;
53 return plugin2->desc->priority - plugin1->desc->priority;
56 static gboolean add_plugin(void *handle, struct near_plugin_desc *desc)
58 struct near_plugin *plugin;
60 if (desc->init == NULL)
63 if (g_str_equal(desc->version, NEAR_VERSION) == FALSE) {
64 near_error("Version mismatch for %s", desc->description);
68 plugin = g_try_new0(struct near_plugin, 1);
72 plugin->handle = handle;
73 plugin->active = FALSE;
76 plugins = g_slist_insert_sorted(plugins, plugin, compare_priority);
81 static gboolean check_plugin(struct near_plugin_desc *desc,
82 char **patterns, char **excludes)
85 for (; *excludes; excludes++)
86 if (g_pattern_match_simple(*excludes, desc->name))
89 near_info("Excluding %s", desc->description);
95 for (; *patterns; patterns++)
96 if (g_pattern_match_simple(*patterns, desc->name))
99 near_info("Ignoring %s", desc->description);
109 int __near_plugin_init(const char *pattern, const char *exclude)
111 gchar **patterns = NULL;
112 gchar **excludes = NULL;
122 patterns = g_strsplit_set(pattern, ":, ", -1);
125 excludes = g_strsplit_set(exclude, ":, ", -1);
127 for (i = 0; __near_builtin[i]; i++) {
128 if (check_plugin(__near_builtin[i],
129 patterns, excludes) == FALSE)
132 add_plugin(NULL, __near_builtin[i]);
135 dir = g_dir_open(PLUGINDIR, 0, NULL);
137 while ((file = g_dir_read_name(dir)) != NULL) {
139 struct near_plugin_desc *desc;
141 if (g_str_has_prefix(file, "lib") == TRUE ||
142 g_str_has_suffix(file, ".so") == FALSE)
145 filename = g_build_filename(PLUGINDIR, file, NULL);
147 handle = dlopen(filename, RTLD_NOW);
148 if (handle == NULL) {
149 near_error("Can't load %s: %s",
150 filename, dlerror());
157 desc = dlsym(handle, "near_plugin_desc");
159 near_error("Can't load symbol: %s",
165 if (check_plugin(desc, patterns, excludes) == FALSE) {
170 if (add_plugin(handle, desc) == FALSE)
177 for (list = plugins; list; list = list->next) {
178 struct near_plugin *plugin = list->data;
180 if (plugin->desc->init() < 0)
183 plugin->active = TRUE;
186 g_strfreev(patterns);
187 g_strfreev(excludes);
192 void __near_plugin_cleanup(void)
198 for (list = plugins; list; list = list->next) {
199 struct near_plugin *plugin = list->data;
201 if (plugin->active == TRUE && plugin->desc->exit)
202 plugin->desc->exit();
204 if (plugin->handle != NULL)
205 dlclose(plugin->handle);
210 g_slist_free(plugins);