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 "location/location-module-internal.h"
28 #include "location/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, "geocode")){
124 LocationGeoMod* _mod = g_new0(LocationGeoMod, 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;
195 }else if(g_str_has_prefix(module_name, "poi")){
196 LocationPoiMod* _mod = g_new0(LocationPoiMod, 1);
199 _mod->shutdown= shutdown;
200 _mod->handler= _mod->init(&(_mod->ops));
202 LOCATION_LOGW("module init failed");
203 gmod_free(_mod->gmod);
206 ret_mod = (gpointer)_mod;
208 LOCATION_LOGW("module name (%s) is wrong", module_name);
216 mod_free (gpointer mod,
217 const char* module_name)
219 if(!mod || !module_name)
222 if(0 == g_strcmp0(module_name, "geocode")){
223 LocationGeoMod* _mod = (LocationGeoMod*)mod;
224 if(_mod->shutdown && _mod->handler){
225 _mod->shutdown(_mod->handler);
227 _mod->handler = NULL;
229 _mod->shutdown= NULL;
230 gmod_free(_mod->gmod);
232 }else if(0 == g_strcmp0(module_name, "gps")){
233 LocationGpsMod* _mod = (LocationGpsMod*)mod;
234 if(_mod->shutdown && _mod->handler){
235 _mod->shutdown(_mod->handler);
237 _mod->handler = NULL;
239 _mod->shutdown= NULL;
240 gmod_free(_mod->gmod);
242 }else if(0 == g_strcmp0(module_name, "wps")){
243 LocationWpsMod* _mod = (LocationWpsMod*)mod;
244 if(_mod->shutdown && _mod->handler){
245 _mod->shutdown(_mod->handler);
247 _mod->handler = NULL;
249 _mod->shutdown= NULL;
250 gmod_free(_mod->gmod);
252 }else if(0 == g_strcmp0(module_name, "cps")){
253 LocationCpsMod* _mod = (LocationCpsMod*)mod;
254 if(_mod->shutdown && _mod->handler){
255 _mod->shutdown(_mod->handler);
257 _mod->handler = NULL;
259 _mod->shutdown= NULL;
260 gmod_free(_mod->gmod);
262 }else if(0 == g_strcmp0(module_name, "ips")){
263 LocationIpsMod* _mod = (LocationIpsMod*)mod;
264 if(_mod->shutdown && _mod->handler){
265 _mod->shutdown(_mod->handler);
267 _mod->handler = NULL;
269 _mod->shutdown= NULL;
270 gmod_free(_mod->gmod);
272 }else if(0 == g_strcmp0(module_name, "sps")){
273 LocationSpsMod* _mod = (LocationSpsMod*)mod;
274 if(_mod->shutdown && _mod->handler){
275 _mod->shutdown(_mod->handler);
277 _mod->handler = NULL;
279 _mod->shutdown= NULL;
280 gmod_free(_mod->gmod);
282 }else if(0 == g_strcmp0(module_name, "poi")){
283 LocationPoiMod* _mod = (LocationPoiMod*)mod;
284 if(_mod->shutdown && _mod->handler){
285 _mod->shutdown(_mod->handler);
287 _mod->handler = NULL;
289 _mod->shutdown= NULL;
290 gmod_free(_mod->gmod);
293 LOCATION_LOGW("module name (%s) is wrong", module_name);
299 mod_is_supported(const char *module_name)
302 gmod = gmod_new(module_name, FALSE);
311 gboolean module_init (void)
313 if (!g_module_supported()) {
314 LOCATION_LOGW("module is not supported");
321 module_free (gpointer mod,
322 const char* module_name)
324 if(!mod || !module_name)
326 mod_free(mod, module_name);
330 module_new (const char* module_name)
338 for(index = -1 ; index < MAX_MODULE_INDEX ; index++){
340 if( 0 >= g_snprintf(name, 256, "%s%d", module_name, index)){
341 LOCATION_LOGW("module name(%s) is wrong", name);
345 if( 0 >= g_snprintf(name, 256, "%s", module_name)){
346 LOCATION_LOGW("module name(%s) is wrong", name);
352 LOCATION_LOGW("module (%s) open success", name);
355 LOCATION_LOGW("module (%s) open failed", name);
361 module_is_supported(const char *module_name)
367 gboolean ret = FALSE;
368 gboolean found = FALSE;
370 char name[256] = {0, };
372 for(index = -1 ; index < MAX_MODULE_INDEX ; index++){
374 g_snprintf(name, 256, "%s%d", module_name, index);
376 g_snprintf(name, 256, "%s", module_name);
379 ret = mod_is_supported(name);
382 LOCATION_LOGW("module name(%s) is found", name);