4 * Copyright (c) 2010-2011 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: Youngae Kang <youngae.kang@samsung.com>, Yunhan Kim <yhan.kim@samsung.com>,
7 * Genie Kim <daejins.kim@samsung.com>, Minjune Kim <sena06.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);
56 gmod->module = g_module_open(gmod->path, G_MODULE_BIND_LAZY);
67 g_module_make_resident(gmod->module);
73 gmod_free (GMod* gmod)
80 g_module_close(gmod->module);
85 gmod_find_sym (GMod* gmod,
86 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);
103 mod_new (const char* module_name)
105 gpointer ret_mod = NULL;
110 gpointer init = NULL;
111 gpointer shutdown = NULL;
112 gmod = gmod_new(module_name, TRUE);
114 LOCATION_LOGW("module(%s) new failed", module_name);
117 if( !gmod_find_sym(gmod, &init, &shutdown) ){
118 LOCATION_LOGW("symbol (init, shutdown) finding failed");
122 if(!init || !shutdown){
123 LOCATION_LOGW("init, shutdown symbol is NULL");
127 if(g_str_has_prefix(module_name, "map-service")){
128 LocationServiceMod* _mod = g_new0(LocationServiceMod, 1);
131 _mod->shutdown= shutdown;
132 _mod->handler= _mod->init(&(_mod->ops));
134 LOCATION_LOGW("module init failed");
135 gmod_free(_mod->gmod);
138 ret_mod = (gpointer)_mod;
139 }else if(g_str_has_prefix(module_name, "gps")){
140 LocationGpsMod* _mod = g_new0(LocationGpsMod, 1);
143 _mod->shutdown= shutdown;
144 _mod->handler= _mod->init(&(_mod->ops));
146 LOCATION_LOGW("module init failed");
147 gmod_free(_mod->gmod);
150 ret_mod = (gpointer)_mod;
151 }else if(g_str_has_prefix(module_name, "wps")){
152 LocationWpsMod* _mod = g_new0(LocationWpsMod, 1);
155 _mod->shutdown= shutdown;
156 _mod->handler= _mod->init(&(_mod->ops));
158 LOCATION_LOGW("module init failed");
159 gmod_free(_mod->gmod);
162 ret_mod = (gpointer)_mod;
163 }else if(g_str_has_prefix(module_name, "cps")){
164 LocationCpsMod* _mod = g_new0(LocationCpsMod, 1);
167 _mod->shutdown= shutdown;
168 _mod->handler= _mod->init(&(_mod->ops));
170 LOCATION_LOGW("module init failed");
171 gmod_free(_mod->gmod);
174 ret_mod = (gpointer)_mod;
176 LOCATION_LOGW("module name (%s) is wrong", module_name);
184 mod_free (gpointer mod,
185 const char* module_name)
187 if(!mod || !module_name)
190 if(g_str_has_prefix(module_name, "map-service")){
191 LocationServiceMod* _mod = (LocationServiceMod*)mod;
192 if(_mod->shutdown && _mod->handler){
193 _mod->shutdown(_mod->handler);
195 _mod->handler = NULL;
197 _mod->shutdown= NULL;
198 gmod_free(_mod->gmod);
200 }else if(0 == g_strcmp0(module_name, "gps")){
201 LocationGpsMod* _mod = (LocationGpsMod*)mod;
202 if(_mod->shutdown && _mod->handler){
203 _mod->shutdown(_mod->handler);
205 _mod->handler = NULL;
207 _mod->shutdown= NULL;
208 gmod_free(_mod->gmod);
210 }else if(0 == g_strcmp0(module_name, "wps")){
211 LocationWpsMod* _mod = (LocationWpsMod*)mod;
212 if(_mod->shutdown && _mod->handler){
213 _mod->shutdown(_mod->handler);
215 _mod->handler = NULL;
217 _mod->shutdown= NULL;
218 gmod_free(_mod->gmod);
220 }else if(0 == g_strcmp0(module_name, "cps")){
221 LocationCpsMod* _mod = (LocationCpsMod*)mod;
222 if(_mod->shutdown && _mod->handler){
223 _mod->shutdown(_mod->handler);
225 _mod->handler = NULL;
227 _mod->shutdown= NULL;
228 gmod_free(_mod->gmod);
231 LOCATION_LOGW("module name (%s) is wrong", module_name);
237 mod_is_supported(const char *module_name)
240 gmod = gmod_new(module_name, FALSE);
249 gboolean module_init (void)
251 if (!g_module_supported()) {
252 LOCATION_LOGW("module is not supported");
259 module_free (gpointer mod,
260 const char* module_name)
262 if(!mod || !module_name)
264 mod_free(mod, module_name);
268 module_new (const char* module_name)
276 for(index = -1 ; index < MAX_MODULE_INDEX ; index++){
278 if( 0 >= g_snprintf(name, 256, "%s%d", module_name, index)){
279 LOCATION_LOGW("module name(%s) is wrong", name);
283 if( 0 >= g_snprintf(name, 256, "%s", module_name)){
284 LOCATION_LOGW("module name(%s) is wrong", name);
290 LOCATION_LOGW("module (%s) open success", name);
293 LOCATION_LOGW("module (%s) open failed", name);
299 module_is_supported(const char *module_name)
305 gboolean ret = FALSE;
306 gboolean found = FALSE;
308 char name[256] = {0, };
310 for(index = -1 ; index < MAX_MODULE_INDEX ; index++){
312 g_snprintf(name, 256, "%s%d", module_name, index);
314 g_snprintf(name, 256, "%s", module_name);
317 ret = mod_is_supported(name);
320 LOCATION_LOGW("module name(%s) is found", name);
329 mod_get_realpath (const gchar *module_name)
331 gchar origin_path[PATH_MAX] = {0, };
332 gchar link_path[PATH_MAX] = {0, };
335 snprintf (link_path, PATH_MAX, "%s/lib%s.so", MODULE_PATH_PREFIX, module_name);
337 realpath (link_path, origin_path);
340 LOCATION_LOGE ("Fail to get real path of [%s]", module_name);
344 path = strrchr(origin_path, '/');
345 if (!path) return NULL;
347 return g_strdup (path);