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 3
35 const char* MODULE_PATH_PREFIX = "/usr/lib/location/module";
38 gmod_new (const char* module_name, gboolean is_resident)
43 GMod* gmod = g_new0(GMod, 1);
44 gmod->name = g_strdup(module_name);
49 gmod->path = g_module_build_path (MODULE_PATH_PREFIX, gmod->name);
55 gmod->module = g_module_open(gmod->path, G_MODULE_BIND_LAZY);
63 g_module_make_resident(gmod->module);
69 gmod_free (GMod* gmod)
76 g_module_close(gmod->module);
81 gmod_find_sym (GMod* gmod,
82 gpointer* init_func, gpointer* shutdown_func)
85 g_stpcpy(sym, "init");
86 if ( !g_module_symbol (gmod->module, sym, init_func) ){
87 LOCATION_LOGW("symbol not found: %s", sym);
90 g_stpcpy(sym, "shutdown");
91 if ( !g_module_symbol (gmod->module, sym, shutdown_func) ){
92 LOCATION_LOGW("symbol not found: %s", sym);
99 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, "map-service")){
124 LocationServiceMod* _mod = g_new0(LocationServiceMod, 1);
127 _mod->shutdown= shutdown;
128 _mod->handler= _mod->init(&(_mod->ops));
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, "gps")){
136 LocationGpsMod* _mod = g_new0(LocationGpsMod, 1);
139 _mod->shutdown= shutdown;
140 _mod->handler= _mod->init(&(_mod->ops));
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, "wps")){
148 LocationWpsMod* _mod = g_new0(LocationWpsMod, 1);
151 _mod->shutdown= shutdown;
152 _mod->handler= _mod->init(&(_mod->ops));
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);
168 mod_free (gpointer mod,
169 const char* module_name)
171 if(!mod || !module_name)
174 if(g_str_has_prefix(module_name, "map-service")){
175 LocationServiceMod* _mod = (LocationServiceMod*)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, "gps")){
185 LocationGpsMod* _mod = (LocationGpsMod*)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, "wps")){
195 LocationWpsMod* _mod = (LocationWpsMod*)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 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,
233 const char* module_name)
235 if(!mod || !module_name)
237 mod_free(mod, module_name);
240 gpointer module_new (const char* module_name)
248 for(index = -1 ; index < MAX_MODULE_INDEX ; index++){
250 if( 0 >= g_snprintf(name, 256, "%s%d", module_name, index)){
251 LOCATION_LOGW("module name(%s) is wrong", name);
255 if( 0 >= g_snprintf(name, 256, "%s", module_name)){
256 LOCATION_LOGW("module name(%s) is wrong", name);
262 LOCATION_LOGW("module (%s) open success", name);
265 LOCATION_LOGW("module (%s) open failed", name);
270 gboolean module_is_supported(const char *module_name)
276 gboolean ret = FALSE;
277 gboolean found = FALSE;
279 char name[256] = {0, };
281 for(index = -1 ; index < MAX_MODULE_INDEX ; index++){
283 g_snprintf(name, 256, "%s%d", module_name, index);
285 g_snprintf(name, 256, "%s", module_name);
288 ret = mod_is_supported(name);
291 LOCATION_LOGW("module name(%s) is found", name);
299 gchar * mod_get_realpath (const gchar *module_name)
301 gchar origin_path[PATH_MAX] = {0, };
302 gchar link_path[PATH_MAX] = {0, };
305 snprintf (link_path, PATH_MAX, "%s/lib%s.so", MODULE_PATH_PREFIX, module_name);
307 realpath (link_path, origin_path);
309 if (strlen(origin_path) == 0) {
310 LOCATION_LOGE ("Fail to get real path of [%s]", module_name);
314 path = strrchr(origin_path, '/');
315 if (!path) return NULL;
317 return g_strdup (path);