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"
34 /* const char *MODULE_PATH_PREFIX = "/usr/lib/location/module"; */
36 static GMod *gmod_new(const char *module_name, gboolean is_resident)
41 GMod *gmod = g_new0(GMod, 1);
42 gmod->name = g_strdup(module_name);
48 char *module_path = g_strndup(MODULE_PATH_PREFIX, strlen(MODULE_PATH_PREFIX)+1);
49 /* gmod->path = g_module_build_path(MODULE_PATH_PREFIX, gmod->name); */
50 gmod->path = g_module_build_path(module_path, gmod->name);
58 gmod->module = g_module_open(gmod->path, G_MODULE_BIND_LAZY);
66 g_module_make_resident(gmod->module);
71 static void gmod_free(GMod *gmod)
78 g_module_close(gmod->module);
83 gmod_find_sym(GMod *gmod, gpointer *init_func, gpointer *shutdown_func)
86 g_stpcpy(sym, "init");
87 if (!g_module_symbol(gmod->module, sym, init_func)) {
88 LOCATION_LOGW("symbol not found: %s", sym);
91 g_stpcpy(sym, "shutdown");
92 if (!g_module_symbol(gmod->module, sym, shutdown_func)) {
93 LOCATION_LOGW("symbol not found: %s", sym);
99 static gpointer mod_new(const char *module_name)
101 gpointer ret_mod = NULL;
106 gpointer init = NULL;
107 gpointer shutdown = NULL;
108 gmod = gmod_new(module_name, TRUE);
110 LOCATION_LOGW("module(%s) new failed", module_name);
113 if (!gmod_find_sym(gmod, &init, &shutdown)) {
114 LOCATION_LOGW("symbol (init, shutdown) finding failed");
118 if (!init || !shutdown) {
119 LOCATION_LOGW("init, shutdown symbol is NULL");
123 if (g_str_has_prefix(module_name, "gps")) {
124 LocationGpsMod *_mod = g_new0(LocationGpsMod, 1);
127 _mod->shutdown = shutdown;
128 _mod->handler = _mod->init(&(_mod->ops));
129 if (!_mod->handler) {
130 LOCATION_LOGW("module init failed");
131 gmod_free(_mod->gmod);
134 ret_mod = (gpointer) _mod;
135 } else if (g_str_has_prefix(module_name, "wps")) {
136 LocationWpsMod *_mod = g_new0(LocationWpsMod, 1);
139 _mod->shutdown = shutdown;
140 _mod->handler = _mod->init(&(_mod->ops));
141 if (!_mod->handler) {
142 LOCATION_LOGW("module init failed");
143 gmod_free(_mod->gmod);
146 ret_mod = (gpointer) _mod;
147 } else if (g_str_has_prefix(module_name, "passive")) {
148 LocationPassiveMod *_mod = g_new0(LocationPassiveMod, 1);
151 _mod->shutdown = shutdown;
152 _mod->handler = _mod->init(&(_mod->ops));
153 if (!_mod->handler) {
154 LOCATION_LOGW("module init failed");
155 gmod_free(_mod->gmod);
158 ret_mod = (gpointer) _mod;
160 LOCATION_LOGW("module name (%s) is wrong", module_name);
166 static void mod_free(gpointer mod, const char *module_name)
168 if (!mod || !module_name)
171 if (0 == g_strcmp0(module_name, "gps")) {
172 LocationGpsMod *_mod = (LocationGpsMod *) mod;
173 if (_mod->shutdown && _mod->handler)
174 _mod->shutdown(_mod->handler);
176 _mod->handler = NULL;
178 _mod->shutdown = NULL;
179 gmod_free(_mod->gmod);
181 } else if (0 == g_strcmp0(module_name, "wps")) {
182 LocationWpsMod *_mod = (LocationWpsMod *) mod;
183 if (_mod->shutdown && _mod->handler)
184 _mod->shutdown(_mod->handler);
186 _mod->handler = NULL;
188 _mod->shutdown = NULL;
189 gmod_free(_mod->gmod);
191 } else if (0 == g_strcmp0(module_name, "passive")) {
192 LocationPassiveMod *_mod = (LocationPassiveMod *)mod;
193 if (_mod->shutdown && _mod->handler)
194 _mod->shutdown(_mod->handler);
196 _mod->handler = NULL;
198 _mod->shutdown = NULL;
199 gmod_free(_mod->gmod);
202 LOCATION_LOGW("module name (%s) is wrong", module_name);
208 static gboolean mod_is_supported(const char *module_name)
211 gmod = gmod_new(module_name, FALSE);
220 gboolean module_init(void)
222 if (!g_module_supported()) {
223 LOCATION_LOGW("module is not supported");
229 void module_free(gpointer mod, const char *module_name)
231 if (!mod || !module_name)
233 mod_free(mod, module_name);
236 gpointer module_new(const char *module_name)
246 for (index = -1; index < MAX_MODULE_INDEX; index++) {
248 if (0 >= g_snprintf(name, 256, "%s%d", module_name, index)) {
249 LOCATION_LOGW("module name(%s) is wrong", name);
253 if (0 >= g_snprintf(name, 256, "%s", module_name)) {
254 LOCATION_LOGW("module name(%s) is wrong", name);
260 LOCATION_LOGW("module (%s) open success", name);
263 LOCATION_LOGW("module (%s) open failed", name);
266 if (0 >= g_snprintf(name, 256, "%s", module_name)) {
267 LOCATION_LOGW("module name(%s) is wrong", name);
271 LOCATION_LOGW("module (%s) open success", name);
273 LOCATION_LOGW("module (%s) open failed", name);
278 gboolean module_is_supported(const char *module_name)
284 gboolean ret = FALSE;
285 gboolean found = FALSE;
287 char name[256] = { 0, };
289 for (index = -1; index < MAX_MODULE_INDEX; index++) {
291 g_snprintf(name, 256, "%s%d", module_name, index);
293 g_snprintf(name, 256, "%s", module_name);
295 ret = mod_is_supported(name);
298 LOCATION_LOGW("module name(%s) is found", name);
306 gchar *mod_get_realpath(const gchar *module_name)
308 gchar origin_path[PATH_MAX] = { 0, };
309 gchar link_path[PATH_MAX] = { 0, };
312 snprintf(link_path, PATH_MAX, "%s/lib%s.so", MODULE_PATH_PREFIX, module_name);
314 if (realpath(link_path, origin_path) == NULL || strlen(origin_path) == 0) {
315 LOCATION_LOGE("Fail to get real path of [%s]", module_name);
319 path = strrchr(origin_path, '/');
323 return g_strdup(path);