4 * Copyright (c) 2010-2013 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: Youngae Kang <youngae.kang@samsung.com>, Minjune Kim <sena06.kim@samsung.com>
7 * Genie Kim <daejins.kim@samsung.com>
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
31 #include "module-internal.h"
32 #include "location-log.h"
34 #define MAX_MODULE_INDEX 4
36 #define MODULE_PATH_PREFIX LIBDIR"/location/module"
37 /* const char *MODULE_PATH_PREFIX = "/usr/lib/location/module"; */
39 static GMod *gmod_new(const char *module_name, gboolean is_resident)
44 GMod *gmod = g_new0(GMod, 1);
45 gmod->name = g_strdup(module_name);
51 char *module_path = g_strndup(MODULE_PATH_PREFIX, strlen(MODULE_PATH_PREFIX)+1);
52 /* gmod->path = g_module_build_path(MODULE_PATH_PREFIX, gmod->name); */
53 gmod->path = g_module_build_path(module_path, gmod->name);
61 gmod->module = g_module_open(gmod->path, G_MODULE_BIND_LAZY);
69 g_module_make_resident(gmod->module);
74 static void gmod_free(GMod *gmod)
81 g_module_close(gmod->module);
86 gmod_find_sym(GMod *gmod, gpointer *init_func, gpointer *shutdown_func)
89 g_stpcpy(sym, "init");
90 if (!g_module_symbol(gmod->module, sym, init_func)) {
91 LOCATION_LOGW("symbol not found: %s", sym);
94 g_stpcpy(sym, "shutdown");
95 if (!g_module_symbol(gmod->module, sym, shutdown_func)) {
96 LOCATION_LOGW("symbol not found: %s", sym);
102 static gpointer mod_new(const char *module_name)
104 gpointer ret_mod = NULL;
109 gpointer init = NULL;
110 gpointer shutdown = NULL;
111 gmod = gmod_new(module_name, TRUE);
113 LOCATION_LOGW("module(%s) new failed", module_name);
116 if (!gmod_find_sym(gmod, &init, &shutdown)) {
117 LOCATION_LOGW("symbol (init, shutdown) finding failed");
121 if (!init || !shutdown) {
122 LOCATION_LOGW("init, shutdown symbol is NULL");
126 if (g_str_has_prefix(module_name, "gps")) {
127 LocationGpsMod *_mod = g_new0(LocationGpsMod, 1);
130 _mod->shutdown = shutdown;
131 _mod->handler = _mod->init(&(_mod->ops));
132 if (!_mod->handler) {
133 LOCATION_LOGW("module init failed");
134 gmod_free(_mod->gmod);
137 ret_mod = (gpointer) _mod;
138 } else if (g_str_has_prefix(module_name, "wps")) {
139 LocationWpsMod *_mod = g_new0(LocationWpsMod, 1);
142 _mod->shutdown = shutdown;
143 _mod->handler = _mod->init(&(_mod->ops));
144 if (!_mod->handler) {
145 LOCATION_LOGW("module init failed");
146 gmod_free(_mod->gmod);
149 ret_mod = (gpointer) _mod;
150 } else if (g_str_has_prefix(module_name, "passive")) {
151 LocationPassiveMod *_mod = g_new0(LocationPassiveMod, 1);
154 _mod->shutdown = shutdown;
155 _mod->handler = _mod->init(&(_mod->ops));
156 if (!_mod->handler) {
157 LOCATION_LOGW("module init failed");
158 gmod_free(_mod->gmod);
161 ret_mod = (gpointer) _mod;
163 LOCATION_LOGW("module name (%s) is wrong", module_name);
169 static void mod_free(gpointer mod, const char *module_name)
171 if (!mod || !module_name)
174 if (0 == g_strcmp0(module_name, "gps")) {
175 LocationGpsMod *_mod = (LocationGpsMod *) mod;
176 if (_mod->shutdown && _mod->handler)
177 _mod->shutdown(_mod->handler);
179 _mod->handler = NULL;
181 _mod->shutdown = NULL;
182 gmod_free(_mod->gmod);
184 } else if (0 == g_strcmp0(module_name, "wps")) {
185 LocationWpsMod *_mod = (LocationWpsMod *) mod;
186 if (_mod->shutdown && _mod->handler)
187 _mod->shutdown(_mod->handler);
189 _mod->handler = NULL;
191 _mod->shutdown = NULL;
192 gmod_free(_mod->gmod);
194 } else if (0 == g_strcmp0(module_name, "passive")) {
195 LocationPassiveMod *_mod = (LocationPassiveMod *)mod;
196 if (_mod->shutdown && _mod->handler)
197 _mod->shutdown(_mod->handler);
199 _mod->handler = NULL;
201 _mod->shutdown = NULL;
202 gmod_free(_mod->gmod);
205 LOCATION_LOGW("module name (%s) is wrong", module_name);
211 static gboolean mod_is_supported(const char *module_name)
214 gmod = gmod_new(module_name, FALSE);
223 gboolean module_init(void)
225 if (!g_module_supported()) {
226 LOCATION_LOGW("module is not supported");
232 void module_free(gpointer mod, const char *module_name)
234 if (!mod || !module_name)
236 mod_free(mod, module_name);
239 gpointer module_new(const char *module_name)
249 for (index = -1; index < MAX_MODULE_INDEX; index++) {
251 if (0 >= g_snprintf(name, 256, "%s%d", module_name, index)) {
252 LOCATION_LOGW("module name(%s) is wrong", name);
256 if (0 >= g_snprintf(name, 256, "%s", module_name)) {
257 LOCATION_LOGW("module name(%s) is wrong", name);
263 LOCATION_LOGW("module (%s) open success", name);
266 LOCATION_LOGW("module (%s) open failed", name);
269 if (0 >= g_snprintf(name, 256, "%s", module_name)) {
270 LOCATION_LOGW("module name(%s) is wrong", name);
274 LOCATION_LOGW("module (%s) open success", name);
276 LOCATION_LOGW("module (%s) open failed", name);
281 gboolean module_is_supported(const char *module_name)
287 gboolean ret = FALSE;
288 gboolean found = FALSE;
290 char name[256] = { 0, };
292 for (index = -1; index < MAX_MODULE_INDEX; index++) {
294 g_snprintf(name, 256, "%s%d", module_name, index);
296 g_snprintf(name, 256, "%s", module_name);
298 ret = mod_is_supported(name);
301 LOCATION_LOGW("module name(%s) is found", name);
309 gchar *mod_get_realpath(const gchar *module_name)
311 gchar origin_path[PATH_MAX] = { 0, };
312 gchar link_path[PATH_MAX] = { 0, };
315 snprintf(link_path, PATH_MAX, "%s/lib%s.so", MODULE_PATH_PREFIX, module_name);
317 if (realpath(link_path, origin_path) == NULL || strlen(origin_path) == 0) {
318 LOCATION_LOGE("Fail to get real path of [%s]", module_name);
322 path = strrchr(origin_path, '/');
326 return g_strdup(path);