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.
27 #include "module-internal.h"
28 #include "location-log.h"
30 #define MAX_MODULE_INDEX 10
31 const char* MODULE_PATH_PREFIX = "/usr/lib/location/module";
34 gmod_new (const char* module_name, gboolean is_resident)
39 GMod* gmod = g_new0(GMod, 1);
40 gmod->name = g_strdup(module_name);
45 gmod->path = g_module_build_path (MODULE_PATH_PREFIX, gmod->name);
52 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;
159 }else if(g_str_has_prefix(module_name, "cps")){
160 LocationCpsMod* _mod = g_new0(LocationCpsMod, 1);
163 _mod->shutdown= shutdown;
164 _mod->handler= _mod->init(&(_mod->ops));
166 LOCATION_LOGW("module init failed");
167 gmod_free(_mod->gmod);
170 ret_mod = (gpointer)_mod;
171 }else if(g_str_has_prefix(module_name, "ips")){
172 LocationIpsMod* _mod = g_new0(LocationIpsMod, 1);
175 _mod->shutdown= shutdown;
176 _mod->handler= _mod->init(&(_mod->ops));
178 LOCATION_LOGW("module init failed");
179 gmod_free(_mod->gmod);
182 ret_mod = (gpointer)_mod;
183 }else if(g_str_has_prefix(module_name, "sps")){
184 LocationSpsMod* _mod = g_new0(LocationSpsMod, 1);
187 _mod->shutdown= shutdown;
188 _mod->handler= _mod->init(&(_mod->ops));
190 LOCATION_LOGW("module init failed");
191 gmod_free(_mod->gmod);
194 ret_mod = (gpointer)_mod;
196 LOCATION_LOGW("module name (%s) is wrong", module_name);
204 mod_free (gpointer mod,
205 const char* module_name)
207 if(!mod || !module_name)
210 if(0 == g_strcmp0(module_name, "map-service")){
211 LocationServiceMod* _mod = (LocationServiceMod*)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, "gps")){
221 LocationGpsMod* _mod = (LocationGpsMod*)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);
230 }else if(0 == g_strcmp0(module_name, "wps")){
231 LocationWpsMod* _mod = (LocationWpsMod*)mod;
232 if(_mod->shutdown && _mod->handler){
233 _mod->shutdown(_mod->handler);
235 _mod->handler = NULL;
237 _mod->shutdown= NULL;
238 gmod_free(_mod->gmod);
240 }else if(0 == g_strcmp0(module_name, "cps")){
241 LocationCpsMod* _mod = (LocationCpsMod*)mod;
242 if(_mod->shutdown && _mod->handler){
243 _mod->shutdown(_mod->handler);
245 _mod->handler = NULL;
247 _mod->shutdown= NULL;
248 gmod_free(_mod->gmod);
250 }else if(0 == g_strcmp0(module_name, "ips")){
251 LocationIpsMod* _mod = (LocationIpsMod*)mod;
252 if(_mod->shutdown && _mod->handler){
253 _mod->shutdown(_mod->handler);
255 _mod->handler = NULL;
257 _mod->shutdown= NULL;
258 gmod_free(_mod->gmod);
260 }else if(0 == g_strcmp0(module_name, "sps")){
261 LocationSpsMod* _mod = (LocationSpsMod*)mod;
262 if(_mod->shutdown && _mod->handler){
263 _mod->shutdown(_mod->handler);
265 _mod->handler = NULL;
267 _mod->shutdown= NULL;
268 gmod_free(_mod->gmod);
271 LOCATION_LOGW("module name (%s) is wrong", module_name);
277 mod_is_supported(const char *module_name)
280 gmod = gmod_new(module_name, FALSE);
289 gboolean module_init (void)
291 if (!g_module_supported()) {
292 LOCATION_LOGW("module is not supported");
299 module_free (gpointer mod,
300 const char* module_name)
302 if(!mod || !module_name)
304 mod_free(mod, module_name);
308 module_new (const char* module_name)
316 for(index = -1 ; index < MAX_MODULE_INDEX ; index++){
318 if( 0 >= g_snprintf(name, 256, "%s%d", module_name, index)){
319 LOCATION_LOGW("module name(%s) is wrong", name);
323 if( 0 >= g_snprintf(name, 256, "%s", module_name)){
324 LOCATION_LOGW("module name(%s) is wrong", name);
330 LOCATION_LOGW("module (%s) open success", name);
333 LOCATION_LOGW("module (%s) open failed", name);
339 module_is_supported(const char *module_name)
345 gboolean ret = FALSE;
346 gboolean found = FALSE;
348 char name[256] = {0, };
350 for(index = -1 ; index < MAX_MODULE_INDEX ; index++){
352 g_snprintf(name, 256, "%s%d", module_name, index);
354 g_snprintf(name, 256, "%s", module_name);
357 ret = mod_is_supported(name);
360 LOCATION_LOGW("module name(%s) is found", name);