update for beta universally
[framework/location/libslp-location.git] / location / module / 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 "module-internal.h"
28 #include "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, "map-service")){
124                 LocationServiceMod* _mod = g_new0(LocationServiceMod, 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{
196                 LOCATION_LOGW("module name (%s) is wrong", module_name);
197                 ret_mod = NULL;
198         }
199         return ret_mod;
200 }
201
202
203 static void
204 mod_free (gpointer mod,
205         const char* module_name)
206 {
207         if(!mod || !module_name)
208                 return;
209
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);
214                 }
215                 _mod->handler = NULL;
216                 _mod->init = NULL;
217                 _mod->shutdown= NULL;
218                 gmod_free(_mod->gmod);
219                 _mod->gmod = NULL;
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);
224                 }
225                 _mod->handler = NULL;
226                 _mod->init = NULL;
227                 _mod->shutdown= NULL;
228                 gmod_free(_mod->gmod);
229                 _mod->gmod = NULL;
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);
234                 }
235                 _mod->handler = NULL;
236                 _mod->init = NULL;
237                 _mod->shutdown= NULL;
238                 gmod_free(_mod->gmod);
239                 _mod->gmod = NULL;
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);
244                 }
245                 _mod->handler = NULL;
246                 _mod->init = NULL;
247                 _mod->shutdown= NULL;
248                 gmod_free(_mod->gmod);
249                 _mod->gmod = NULL;
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);
254                 }
255                 _mod->handler = NULL;
256                 _mod->init = NULL;
257                 _mod->shutdown= NULL;
258                 gmod_free(_mod->gmod);
259                 _mod->gmod = NULL;
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);
264                 }
265                 _mod->handler = NULL;
266                 _mod->init = NULL;
267                 _mod->shutdown= NULL;
268                 gmod_free(_mod->gmod);
269                 _mod->gmod = NULL;
270         }else
271                 LOCATION_LOGW("module name (%s) is wrong", module_name);
272
273         g_free(mod);
274 }
275
276 static gboolean
277 mod_is_supported(const char *module_name)
278 {
279         GMod * gmod = NULL;
280         gmod = gmod_new(module_name, FALSE);
281         if(!gmod) {
282                 return FALSE;
283         }
284         gmod_free(gmod);
285
286         return TRUE;
287 }
288
289 gboolean module_init (void)
290 {
291         if (!g_module_supported()) {
292                 LOCATION_LOGW("module is not supported");
293                 return FALSE;
294         }
295         return TRUE;
296 }
297
298 void
299 module_free (gpointer mod,
300         const char* module_name)
301 {
302         if(!mod || !module_name)
303                 return;
304         mod_free(mod, module_name);
305 }
306
307 gpointer
308 module_new (const char* module_name)
309 {
310         if(!module_name)
311                 return NULL;
312         int index = 0;
313         char name[256];
314
315         gpointer mod = NULL;
316         for(index = -1 ; index < MAX_MODULE_INDEX ; index++){
317                 if(index >= 0){
318                         if( 0 >= g_snprintf(name, 256, "%s%d", module_name, index)){
319                                 LOCATION_LOGW("module name(%s) is wrong", name);
320                                 continue;
321                         }
322                 }else{
323                         if( 0 >= g_snprintf(name, 256, "%s", module_name)){
324                                 LOCATION_LOGW("module name(%s) is wrong", name);
325                                 continue;
326                         }
327                 }
328                 mod = mod_new(name);
329                 if(mod){
330                         LOCATION_LOGW("module (%s) open success", name);
331                         break;
332                 }
333                 LOCATION_LOGW("module (%s) open failed", name);
334         }
335         return mod;
336 }
337
338 gboolean
339 module_is_supported(const char *module_name)
340 {
341         if(!module_name)
342                 return FALSE;
343
344         int index = 0;
345         gboolean ret = FALSE;
346         gboolean found = FALSE;
347
348         char name[256] = {0, };
349
350         for(index = -1 ; index < MAX_MODULE_INDEX ; index++){
351                 if(index >= 0){
352                         g_snprintf(name, 256, "%s%d", module_name, index);
353                 }else{
354                         g_snprintf(name, 256, "%s", module_name);
355                 }
356
357                 ret = mod_is_supported(name);
358                 if(ret == TRUE) {
359                         found = TRUE;
360                         LOCATION_LOGW("module name(%s) is found", name);
361                         break;
362                 }
363         }
364
365         return found;
366 }