sync PRIVATE and RSA for tizen2.1
[platform/core/location/libslp-location.git] / location / module / module-internal.c
1 /*
2  * libslp-location
3  *
4  * Copyright (c) 2010-2013 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Youngae Kang <youngae.kang@samsung.com>, Minjune Kim <sena06.kim@samsung.com>
7  *          Genie Kim <daejins.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 <stdio.h>
28 #include <limits.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include "module-internal.h"
32 #include "location-log.h"
33
34 #define MAX_MODULE_INDEX 3
35 const char* MODULE_PATH_PREFIX = "/usr/lib/location/module";
36
37 static GMod*
38 gmod_new (const char* module_name, gboolean is_resident)
39 {
40         if(!module_name)
41                 return NULL;
42
43         GMod* gmod = g_new0(GMod, 1);
44         gmod->name = g_strdup(module_name);
45         if(!gmod->name) {
46                 g_free(gmod);
47                 return NULL;
48         }
49         gmod->path = g_module_build_path (MODULE_PATH_PREFIX, gmod->name);
50         if(!gmod->path){
51                 g_free(gmod->name);
52                 g_free(gmod);
53                 return NULL;
54         }
55         gmod->module = g_module_open(gmod->path, G_MODULE_BIND_LAZY);
56         if(!gmod->module){
57                 g_free(gmod->name);
58                 g_free(gmod->path);
59                 g_free(gmod);
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{
160                 LOCATION_LOGW("module name (%s) is wrong", module_name);
161                 ret_mod = NULL;
162         }
163         return ret_mod;
164 }
165
166
167 static void
168 mod_free (gpointer mod,
169                 const char* module_name)
170 {
171         if(!mod || !module_name)
172                 return;
173
174         if(g_str_has_prefix(module_name, "map-service")){
175                 LocationServiceMod* _mod = (LocationServiceMod*)mod;
176                 if(_mod->shutdown && _mod->handler){
177                         _mod->shutdown(_mod->handler);
178                 }
179                 _mod->handler = NULL;
180                 _mod->init = NULL;
181                 _mod->shutdown= NULL;
182                 gmod_free(_mod->gmod);
183                 _mod->gmod = NULL;
184         }else if(0 == g_strcmp0(module_name, "gps")){
185                 LocationGpsMod* _mod = (LocationGpsMod*)mod;
186                 if(_mod->shutdown && _mod->handler){
187                         _mod->shutdown(_mod->handler);
188                 }
189                 _mod->handler = NULL;
190                 _mod->init = NULL;
191                 _mod->shutdown= NULL;
192                 gmod_free(_mod->gmod);
193                 _mod->gmod = NULL;
194         }else if(0 == g_strcmp0(module_name, "wps")){
195                 LocationWpsMod* _mod = (LocationWpsMod*)mod;
196                 if(_mod->shutdown && _mod->handler){
197                         _mod->shutdown(_mod->handler);
198                 }
199                 _mod->handler = NULL;
200                 _mod->init = NULL;
201                 _mod->shutdown= NULL;
202                 gmod_free(_mod->gmod);
203                 _mod->gmod = NULL;
204         }else
205                 LOCATION_LOGW("module name (%s) is wrong", module_name);
206
207         g_free(mod);
208 }
209
210 static gboolean
211 mod_is_supported(const char *module_name)
212 {
213         GMod * gmod = NULL;
214         gmod = gmod_new(module_name, FALSE);
215         if(!gmod) {
216                 return FALSE;
217         }
218         gmod_free(gmod);
219
220         return TRUE;
221 }
222
223 gboolean module_init (void)
224 {
225         if (!g_module_supported()) {
226                 LOCATION_LOGW("module is not supported");
227                 return FALSE;
228         }
229         return TRUE;
230 }
231
232 void module_free (gpointer mod,
233                 const char* module_name)
234 {
235         if(!mod || !module_name)
236                 return;
237         mod_free(mod, module_name);
238 }
239
240 gpointer module_new (const char* module_name)
241 {
242         if(!module_name)
243                 return NULL;
244         int index = 0;
245         char name[256];
246
247         gpointer mod = NULL;
248         for(index = -1 ; index < MAX_MODULE_INDEX ; index++){
249                 if(index >= 0){
250                         if( 0 >= g_snprintf(name, 256, "%s%d", module_name, index)){
251                                 LOCATION_LOGW("module name(%s) is wrong", name);
252                                 continue;
253                         }
254                 }else{
255                         if( 0 >= g_snprintf(name, 256, "%s", module_name)){
256                                 LOCATION_LOGW("module name(%s) is wrong", name);
257                                 continue;
258                         }
259                 }
260                 mod = mod_new(name);
261                 if(mod){
262                         LOCATION_LOGW("module (%s) open success", name);
263                         break;
264                 }
265                 LOCATION_LOGW("module (%s) open failed", name);
266         }
267         return mod;
268 }
269
270 gboolean module_is_supported(const char *module_name)
271 {
272         if(!module_name)
273                 return FALSE;
274
275         int index = 0;
276         gboolean ret = FALSE;
277         gboolean found = FALSE;
278
279         char name[256] = {0, };
280
281         for(index = -1 ; index < MAX_MODULE_INDEX ; index++){
282                 if(index >= 0){
283                         g_snprintf(name, 256, "%s%d", module_name, index);
284                 }else{
285                         g_snprintf(name, 256, "%s", module_name);
286                 }
287
288                 ret = mod_is_supported(name);
289                 if(ret == TRUE) {
290                         found = TRUE;
291                         LOCATION_LOGW("module name(%s) is found", name);
292                         break;
293                 }
294         }
295
296         return found;
297 }
298
299 gchar * mod_get_realpath (const gchar *module_name)
300 {
301         gchar origin_path[PATH_MAX] = {0, };
302         gchar link_path[PATH_MAX] = {0, };
303         gchar *path = NULL;
304
305         snprintf (link_path, PATH_MAX, "%s/lib%s.so", MODULE_PATH_PREFIX, module_name);
306
307         realpath (link_path, origin_path);
308
309         if (strlen(origin_path) == 0) {
310                 LOCATION_LOGE ("Fail to get real path of [%s]", module_name);
311                 return NULL;
312         }
313
314         path = strrchr(origin_path, '/');
315         if (!path) return NULL;
316
317         return g_strdup (path);
318 }