4 * Copyright (c) 2010-2013 Samsung Electronics Co., Ltd. All rights reserved.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
28 #include "module-internal.h"
29 #include "location-log.h"
31 #define MAX_MODULE_INDEX 4
33 #define MODULE_PATH_PREFIX LIBDIR"/location/module"
35 static gchar* _g_module_build_path (const gchar *directory, const gchar *module_name)
37 if (directory && *directory) {
38 if (strncmp(module_name, "lib", 3) == 0)
39 return g_strconcat(directory, "/", module_name, NULL);
41 return g_strconcat(directory, "/lib", module_name, "." G_MODULE_SUFFIX, NULL);
42 } else if (strncmp (module_name, "lib", 3) == 0)
43 return g_strdup(module_name);
45 return g_strconcat("lib", module_name, "." G_MODULE_SUFFIX, NULL);
48 static GMod *gmod_new(const char *module_name, gboolean is_resident)
53 GMod *gmod = g_new0(GMod, 1);
54 gmod->name = g_strdup(module_name);
60 char *module_path = g_strndup(MODULE_PATH_PREFIX, strlen(MODULE_PATH_PREFIX)+1);
61 /* gmod->path = g_module_build_path(MODULE_PATH_PREFIX, gmod->name); */
62 gmod->path = _g_module_build_path(module_path, gmod->name);
70 gmod->module = g_module_open(gmod->path, G_MODULE_BIND_LAZY);
78 g_module_make_resident(gmod->module);
83 static void gmod_free(GMod *gmod)
90 g_module_close(gmod->module);
95 gmod_find_sym(GMod *gmod, gpointer *init_func, gpointer *shutdown_func)
98 g_stpcpy(sym, "init");
99 if (!g_module_symbol(gmod->module, sym, init_func)) {
100 LOCATION_LOGW("symbol not found: %s", sym);
103 g_stpcpy(sym, "shutdown");
104 if (!g_module_symbol(gmod->module, sym, shutdown_func)) {
105 LOCATION_LOGW("symbol not found: %s", sym);
111 static gpointer mod_new(const char *module_name)
113 gpointer ret_mod = NULL;
118 gpointer init = NULL;
119 gpointer shutdown = NULL;
120 gmod = gmod_new(module_name, TRUE);
122 LOCATION_LOGW("module(%s) new failed", module_name);
125 if (!gmod_find_sym(gmod, &init, &shutdown)) {
126 LOCATION_LOGW("symbol (init, shutdown) finding failed");
130 if (!init || !shutdown) {
131 LOCATION_LOGW("init, shutdown symbol is NULL");
135 if (g_str_has_prefix(module_name, "gps")) {
136 LocationGpsMod *_mod = g_new0(LocationGpsMod, 1);
139 _mod->shutdown = shutdown;
140 _mod->handler = _mod->init(&(_mod->ops));
141 if (!_mod->handler) {
142 LOCATION_LOGW("module init failed : gps");
143 gmod_free(_mod->gmod);
146 ret_mod = (gpointer) _mod;
147 } else if (g_str_has_prefix(module_name, "wps")) {
148 LocationWpsMod *_mod = g_new0(LocationWpsMod, 1);
151 _mod->shutdown = shutdown;
152 _mod->handler = _mod->init(&(_mod->ops));
153 if (!_mod->handler) {
154 LOCATION_LOGW("module init failed : wps");
155 gmod_free(_mod->gmod);
158 ret_mod = (gpointer) _mod;
159 } else if (g_str_has_prefix(module_name, "passive")) {
160 LocationPassiveMod *_mod = g_new0(LocationPassiveMod, 1);
163 _mod->shutdown = shutdown;
164 _mod->handler = _mod->init(&(_mod->ops));
165 if (!_mod->handler) {
166 LOCATION_LOGW("module init failed : passive");
167 gmod_free(_mod->gmod);
170 ret_mod = (gpointer) _mod;
171 } else if (g_str_has_prefix(module_name, "fused")) {
172 LocationFusedMod *_mod = g_new0(LocationFusedMod, 1);
175 _mod->shutdown = shutdown;
176 _mod->handler = _mod->init(&(_mod->ops));
177 if (!_mod->handler) {
178 LOCATION_LOGW("module init failed : fused");
179 gmod_free(_mod->gmod);
182 ret_mod = (gpointer) _mod;
184 LOCATION_LOGW("module name (%s) is wrong", module_name);
190 static void mod_free(gpointer mod, const char *module_name)
192 if (!mod || !module_name)
195 if (0 == g_strcmp0(module_name, "gps")) {
196 LocationGpsMod *_mod = (LocationGpsMod *) mod;
197 if (_mod->shutdown && _mod->handler)
198 _mod->shutdown(_mod->handler);
200 _mod->handler = NULL;
202 _mod->shutdown = NULL;
203 gmod_free(_mod->gmod);
205 } else if (0 == g_strcmp0(module_name, "wps")) {
206 LocationWpsMod *_mod = (LocationWpsMod *) mod;
207 if (_mod->shutdown && _mod->handler)
208 _mod->shutdown(_mod->handler);
210 _mod->handler = NULL;
212 _mod->shutdown = NULL;
213 gmod_free(_mod->gmod);
215 } else if (0 == g_strcmp0(module_name, "passive")) {
216 LocationPassiveMod *_mod = (LocationPassiveMod *)mod;
217 if (_mod->shutdown && _mod->handler)
218 _mod->shutdown(_mod->handler);
220 _mod->handler = NULL;
222 _mod->shutdown = NULL;
223 gmod_free(_mod->gmod);
225 } else if (0 == g_strcmp0(module_name, "fused")) {
226 LocationFusedMod *_mod = (LocationFusedMod *) mod;
227 if (_mod->shutdown && _mod->handler)
228 _mod->shutdown(_mod->handler);
230 _mod->handler = NULL;
232 _mod->shutdown = NULL;
233 gmod_free(_mod->gmod);
236 LOCATION_LOGW("module name (%s) is wrong", module_name);
242 static gboolean mod_is_supported(const char *module_name)
245 gmod = gmod_new(module_name, FALSE);
254 gboolean module_init(void)
256 if (!g_module_supported()) {
257 LOCATION_LOGW("module is not supported");
263 void module_free(gpointer mod, const char *module_name)
265 if (!mod || !module_name)
267 mod_free(mod, module_name);
270 gpointer module_new(const char *module_name)
280 for (index = -1; index < MAX_MODULE_INDEX; index++) {
282 if (0 >= g_snprintf(name, 256, "%s%d", module_name, index)) {
283 LOCATION_LOGW("module name(%s) is wrong", name);
287 if (0 >= g_snprintf(name, 256, "%s", module_name)) {
288 LOCATION_LOGW("module name(%s) is wrong", name);
294 LOCATION_LOGW("module (%s) open success", name);
297 LOCATION_LOGW("module (%s) open failed", name);
300 if (0 >= g_snprintf(name, 256, "%s", module_name)) {
301 LOCATION_LOGW("module name(%s) is wrong", name);
305 LOCATION_LOGW("module (%s) open success", name);
307 LOCATION_LOGW("module (%s) open failed", name);
312 gboolean module_is_supported(const char *module_name)
318 gboolean ret = FALSE;
319 gboolean found = FALSE;
321 char name[256] = { 0, };
323 for (index = -1; index < MAX_MODULE_INDEX; index++) {
325 g_snprintf(name, 256, "%s%d", module_name, index);
327 g_snprintf(name, 256, "%s", module_name);
329 ret = mod_is_supported(name);
332 LOCATION_LOGW("module name(%s) is found", name);