3 * oFono - Open Source Telephony
5 * Copyright (C) 2008-2010 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
32 static GSList *plugins = NULL;
37 struct ofono_plugin_desc *desc;
40 static gint compare_priority(gconstpointer a, gconstpointer b)
42 const struct ofono_plugin *plugin1 = a;
43 const struct ofono_plugin *plugin2 = b;
45 return plugin2->desc->priority - plugin1->desc->priority;
48 static gboolean add_plugin(void *handle, struct ofono_plugin_desc *desc)
50 struct ofono_plugin *plugin;
52 if (desc->init == NULL)
55 if (g_str_equal(desc->version, OFONO_VERSION) == FALSE) {
56 ofono_error("Version mismatch for %s: found %s, expected %s",
57 desc->description, desc->version, OFONO_VERSION);
61 plugin = g_try_new0(struct ofono_plugin, 1);
65 plugin->handle = handle;
66 plugin->active = FALSE;
69 __ofono_log_enable(desc->debug_start, desc->debug_stop);
71 plugins = g_slist_insert_sorted(plugins, plugin, compare_priority);
76 static gboolean check_plugin(struct ofono_plugin_desc *desc,
77 char **patterns, char **excludes)
80 for (; *excludes; excludes++)
81 if (g_pattern_match_simple(*excludes, desc->name))
84 ofono_info("Excluding %s", desc->description);
90 for (; *patterns; patterns++)
91 if (g_pattern_match_simple(*patterns, desc->name))
93 if (*patterns == NULL) {
94 ofono_info("Ignoring %s", desc->description);
104 int __ofono_plugin_init(const char *pattern, const char *exclude)
106 gchar **patterns = NULL;
107 gchar **excludes = NULL;
117 patterns = g_strsplit_set(pattern, ":, ", -1);
120 excludes = g_strsplit_set(exclude, ":, ", -1);
122 for (i = 0; __ofono_builtin[i]; i++) {
123 if (check_plugin(__ofono_builtin[i],
124 patterns, excludes) == FALSE)
127 add_plugin(NULL, __ofono_builtin[i]);
130 dir = g_dir_open(PLUGINDIR, 0, NULL);
132 while ((file = g_dir_read_name(dir)) != NULL) {
134 struct ofono_plugin_desc *desc;
136 if (g_str_has_prefix(file, "lib") == TRUE ||
137 g_str_has_suffix(file, ".so") == FALSE)
140 filename = g_build_filename(PLUGINDIR, file, NULL);
142 handle = dlopen(filename, RTLD_NOW);
143 if (handle == NULL) {
144 ofono_error("Can't load %s: %s",
145 filename, dlerror());
152 desc = dlsym(handle, "ofono_plugin_desc");
154 ofono_error("Can't load symbol: %s",
160 if (check_plugin(desc, patterns, excludes) == FALSE) {
165 if (add_plugin(handle, desc) == FALSE)
172 for (list = plugins; list; list = list->next) {
173 struct ofono_plugin *plugin = list->data;
175 if (plugin->desc->init() < 0)
178 plugin->active = TRUE;
181 g_strfreev(patterns);
182 g_strfreev(excludes);
187 void __ofono_plugin_cleanup(void)
193 for (list = plugins; list; list = list->next) {
194 struct ofono_plugin *plugin = list->data;
196 if (plugin->active == TRUE && plugin->desc->exit)
197 plugin->desc->exit();
200 dlclose(plugin->handle);
205 g_slist_free(plugins);