1c9a19b67879ef50e342216bf2bce5ef9e2c0de2
[framework/location/libslp-location.git] / location / location-module-internal.c
1 /*
2  * libslp-location
3  *
4  * Copyright (c) 2010-2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
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>
8  *
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
12  *
13  * http://www.apache.org/licenses/LICENSE-2.0
14  *
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.
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include "config.h"
24 #endif
25
26 #include <glib.h> 
27 #include "location/location-module-internal.h"
28 #include "location/location-log.h"
29
30 #define MAX_MODULE_INDEX 10
31 const char* MODULE_PATH_PREFIX = "/usr/lib/location/module";
32
33 static GMod*
34 gmod_new (const char* module_name, gboolean is_resident)
35 {
36         if(!module_name)
37                 return NULL;
38
39         GMod* gmod = g_new0(GMod, 1);
40         gmod->name = g_strdup(module_name);
41         if(!gmod->name) {
42                 g_free(gmod);
43                 return NULL;
44         }
45         gmod->path = g_module_build_path (MODULE_PATH_PREFIX, gmod->name);
46         if(!gmod->path){
47                 g_free(gmod->name);
48                 g_free(gmod);
49                 gmod->name = NULL;
50                 return NULL;
51         }
52         gmod->module = g_module_open(gmod->path, G_MODULE_BIND_LAZY);
53         if(!gmod->module){
54                 g_free(gmod->name);
55                 g_free(gmod->path);
56                 g_free(gmod);
57                 gmod->name = NULL;
58                 gmod->path = NULL;
59
60                 return NULL;
61         }
62         if(is_resident)
63                 g_module_make_resident(gmod->module);
64
65         return gmod;
66 }
67
68 static void
69 gmod_free (GMod* gmod)
70 {       
71         if(gmod->name)
72                 g_free(gmod->name);
73         if(gmod->path)
74                 g_free(gmod->path);     
75         if(gmod->module)
76                 g_module_close(gmod->module);
77         g_free(gmod);
78 }
79
80 static gboolean
81 gmod_find_sym (GMod* gmod,
82         gpointer* init_func, gpointer* shutdown_func)
83 {
84         char sym[256];
85         g_stpcpy(sym, "init");
86         if ( !g_module_symbol (gmod->module, sym, init_func) ){
87                 LOCATION_LOGW("symbol not found: %s", sym);
88                 return FALSE;   
89         }
90         g_stpcpy(sym, "shutdown");
91         if ( !g_module_symbol (gmod->module, sym, shutdown_func) ){
92                 LOCATION_LOGW("symbol not found: %s", sym);
93                 return FALSE;
94         }
95         return TRUE;
96 }
97
98 static gpointer
99 mod_new (const char* module_name)
100 {
101         gpointer ret_mod = NULL;        
102         if(!module_name)
103                 return NULL;
104
105         GMod* gmod = NULL;
106         gpointer init = NULL;
107         gpointer shutdown = NULL;
108         gmod = gmod_new(module_name, TRUE);
109         if(!gmod){
110                 LOCATION_LOGW("module(%s) new failed", module_name);
111                 return NULL;
112         }       
113         if( !gmod_find_sym(gmod, &init, &shutdown) ){
114                 LOCATION_LOGW("symbol (init, shutdown) finding failed");
115                 gmod_free(gmod);
116                 return NULL;
117         }
118         if(!init || !shutdown){
119                 LOCATION_LOGW("init, shutdown symbol is NULL"); 
120                 gmod_free(gmod);
121                 return NULL;
122         }       
123         if(g_str_has_prefix(module_name, "geocode")){
124                 LocationGeoMod* _mod = g_new0(LocationGeoMod, 1);
125                 _mod->gmod = gmod;
126                 _mod->init = init;
127                 _mod->shutdown= shutdown;       
128                 _mod->handler= _mod->init(&(_mod->ops));        
129                 if(!_mod->handler){     
130                         LOCATION_LOGW("module init failed");
131                         gmod_free(_mod->gmod);
132                         ret_mod = NULL;
133                 }else
134                         ret_mod = (gpointer)_mod;
135         }else if(g_str_has_prefix(module_name, "gps")){
136                 LocationGpsMod* _mod = g_new0(LocationGpsMod, 1);
137                 _mod->gmod = gmod;
138                 _mod->init = init;
139                 _mod->shutdown= shutdown;       
140                 _mod->handler= _mod->init(&(_mod->ops));        
141                 if(!_mod->handler){     
142                         LOCATION_LOGW("module init failed");
143                         gmod_free(_mod->gmod);
144                         ret_mod = NULL;
145                 }else
146                         ret_mod = (gpointer)_mod;
147         }else if(g_str_has_prefix(module_name, "wps")){
148                 LocationWpsMod* _mod = g_new0(LocationWpsMod, 1);
149                 _mod->gmod = gmod;
150                 _mod->init = init;
151                 _mod->shutdown= shutdown;       
152                 _mod->handler= _mod->init(&(_mod->ops));        
153                 if(!_mod->handler){     
154                         LOCATION_LOGW("module init failed");
155                         gmod_free(_mod->gmod);
156                         ret_mod = NULL;
157                 }else
158                         ret_mod = (gpointer)_mod;
159         }else if(g_str_has_prefix(module_name, "cps")){
160                 LocationCpsMod* _mod = g_new0(LocationCpsMod, 1);
161                 _mod->gmod = gmod;
162                 _mod->init = init;
163                 _mod->shutdown= shutdown;
164                 _mod->handler= _mod->init(&(_mod->ops));
165                 if(!_mod->handler){
166                         LOCATION_LOGW("module init failed");
167                         gmod_free(_mod->gmod);
168                         ret_mod = NULL;
169                 }else
170                         ret_mod = (gpointer)_mod;
171         }else if(g_str_has_prefix(module_name, "ips")){
172                 LocationIpsMod* _mod = g_new0(LocationIpsMod, 1);
173                 _mod->gmod = gmod;
174                 _mod->init = init;
175                 _mod->shutdown= shutdown;       
176                 _mod->handler= _mod->init(&(_mod->ops));        
177                 if(!_mod->handler){     
178                         LOCATION_LOGW("module init failed");
179                         gmod_free(_mod->gmod);
180                         ret_mod = NULL;
181                 }else
182                         ret_mod = (gpointer)_mod;
183         }else if(g_str_has_prefix(module_name, "sps")){
184                 LocationSpsMod* _mod = g_new0(LocationSpsMod, 1);
185                 _mod->gmod = gmod;
186                 _mod->init = init;
187                 _mod->shutdown= shutdown;       
188                 _mod->handler= _mod->init(&(_mod->ops));        
189                 if(!_mod->handler){     
190                         LOCATION_LOGW("module init failed");
191                         gmod_free(_mod->gmod);
192                         ret_mod = NULL;
193                 }else
194                         ret_mod = (gpointer)_mod;
195         }else if(g_str_has_prefix(module_name, "poi")){
196                 LocationPoiMod* _mod = g_new0(LocationPoiMod, 1);
197                 _mod->gmod = gmod;
198                 _mod->init = init;
199                 _mod->shutdown= shutdown;       
200                 _mod->handler= _mod->init(&(_mod->ops));        
201                 if(!_mod->handler){     
202                         LOCATION_LOGW("module init failed");
203                         gmod_free(_mod->gmod);
204                         ret_mod = NULL;
205                 }else
206                         ret_mod = (gpointer)_mod;
207         }else{
208                 LOCATION_LOGW("module name (%s) is wrong", module_name); 
209                 ret_mod = NULL;
210         }       
211         return ret_mod;
212 }
213
214                 
215 static void
216 mod_free (gpointer mod,
217         const char* module_name)        
218 {
219         if(!mod || !module_name)
220                 return;
221         
222         if(0 == g_strcmp0(module_name, "geocode")){
223                 LocationGeoMod* _mod = (LocationGeoMod*)mod;
224                 if(_mod->shutdown && _mod->handler){
225                         _mod->shutdown(_mod->handler);                  
226                 }
227                 _mod->handler = NULL;
228                 _mod->init = NULL;
229                 _mod->shutdown= NULL;
230                 gmod_free(_mod->gmod);
231                 _mod->gmod = NULL;
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);                  
236                 }
237                 _mod->handler = NULL;
238                 _mod->init = NULL;
239                 _mod->shutdown= NULL;
240                 gmod_free(_mod->gmod);
241                 _mod->gmod = NULL;
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);                  
246                 }               
247                 _mod->handler = NULL;
248                 _mod->init = NULL;
249                 _mod->shutdown= NULL;
250                 gmod_free(_mod->gmod);
251                 _mod->gmod = NULL;
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);                  
256                 }               
257                 _mod->handler = NULL;
258                 _mod->init = NULL;
259                 _mod->shutdown= NULL;
260                 gmod_free(_mod->gmod);
261                 _mod->gmod = NULL;
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);                  
266                 }               
267                 _mod->handler = NULL;
268                 _mod->init = NULL;
269                 _mod->shutdown= NULL;
270                 gmod_free(_mod->gmod);
271                 _mod->gmod = NULL;
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);                  
276                 }               
277                 _mod->handler = NULL;
278                 _mod->init = NULL;
279                 _mod->shutdown= NULL;
280                 gmod_free(_mod->gmod);
281                 _mod->gmod = NULL;
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);                  
286                 }
287                 _mod->handler = NULL;
288                 _mod->init = NULL;
289                 _mod->shutdown= NULL;
290                 gmod_free(_mod->gmod);
291                 _mod->gmod = NULL;
292         }else
293                 LOCATION_LOGW("module name (%s) is wrong", module_name); 
294
295         g_free(mod);
296 }
297
298 static gboolean
299 mod_is_supported(const char *module_name)
300 {
301         GMod * gmod = NULL;
302         gmod = gmod_new(module_name, FALSE);
303         if(!gmod) {
304                 return FALSE;
305         }
306         gmod_free(gmod);
307
308         return TRUE;
309 }
310
311 gboolean module_init (void)
312 {
313         if (!g_module_supported()) { 
314                 LOCATION_LOGW("module is not supported"); 
315                 return FALSE;
316         }
317         return TRUE;
318 }
319
320 void
321 module_free (gpointer mod,
322         const char* module_name)
323 {
324         if(!mod || !module_name)
325                 return;
326         mod_free(mod, module_name);
327 }
328
329 gpointer
330 module_new (const char* module_name)
331 {
332         if(!module_name)
333                 return NULL;
334         int index = 0;
335         char name[256];
336
337         gpointer mod = NULL;
338         for(index = -1 ; index < MAX_MODULE_INDEX ; index++){
339                 if(index >= 0){
340                         if( 0 >= g_snprintf(name, 256, "%s%d", module_name, index)){
341                                 LOCATION_LOGW("module name(%s) is wrong", name);
342                                 continue;
343                         }
344                 }else{
345                         if( 0 >= g_snprintf(name, 256, "%s", module_name)){                             
346                                 LOCATION_LOGW("module name(%s) is wrong", name);
347                                 continue;
348                         }               
349                 }
350                 mod = mod_new(name);
351                 if(mod){
352                         LOCATION_LOGW("module (%s) open success", name);
353                         break;
354                 }
355                 LOCATION_LOGW("module (%s) open failed", name);
356         }
357         return mod;
358 }
359
360 gboolean
361 module_is_supported(const char *module_name)
362 {
363         if(!module_name)
364                 return FALSE;
365
366         int index = 0;
367         gboolean ret = FALSE;
368         gboolean found = FALSE;
369
370         char name[256] = {0, };
371
372         for(index = -1 ; index < MAX_MODULE_INDEX ; index++){
373                 if(index >= 0){
374                         g_snprintf(name, 256, "%s%d", module_name, index);
375                 }else{
376                         g_snprintf(name, 256, "%s", module_name);
377                 }
378
379                 ret = mod_is_supported(name);
380                 if(ret == TRUE) {
381                         found = TRUE;
382                         LOCATION_LOGW("module name(%s) is found", name);
383                         break;
384                 }
385         }
386
387         return found;
388 }