Tizen 2.4 Sync
[platform/core/location/lbs-server.git] / lbs-server / src / gps_plugin_module.c
1 /*
2  * lbs_server
3  *
4  * Copyright (c) 2011-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 #include <stdio.h>
23 #include <string.h>
24 #include <dlfcn.h>
25 #include <unistd.h>
26 #include <sys/types.h>
27 #include <sys/stat.h>
28 #include "gps_plugin_module.h"
29 #include "setting.h"
30 #include "debug_util.h"
31
32 #define SPECIFIC_PLUGIN_PATH_PREFIX     LIB_DIR"/liblbs-server-plugin-"
33 #define SPECIFIC_PLUGIN_PATH_POSTFIX    ".so"
34
35 static const gps_plugin_interface *g_plugin = NULL;
36
37 int load_plugin_module(char *specific_name, void **plugin_handle)
38 {
39         char plugin_path[256];
40
41         if (specific_name[0] == '\0') {
42                 strncpy(plugin_path, GPS_PLUGIN_PATH, sizeof(plugin_path));
43         } else {
44                 snprintf(plugin_path, sizeof(plugin_path),
45                                 SPECIFIC_PLUGIN_PATH_PREFIX"%s"
46                                 SPECIFIC_PLUGIN_PATH_POSTFIX,
47                                 specific_name);
48
49                 struct stat st = {0};
50
51                 if (stat(plugin_path, &st) != 0) {
52                         strncpy(plugin_path, GPS_PLUGIN_PATH, sizeof(plugin_path));
53                         /* To support real GPS when additional plugin is added*/
54                         /* setting_set_int(VCONFKEY_LOCATION_REPLAY_ENABLED, 1); */
55                 }
56         }
57
58         *plugin_handle = dlopen(plugin_path, RTLD_NOW);
59         if (!*plugin_handle) {
60                 LOG_GPS(DBG_ERR, "Failed to load plugin module: %s", plugin_path);
61                 /* LOG_GPS(DBG_ERR, "%s", dlerror()); */
62                 return FALSE;
63         }
64
65         const gps_plugin_interface *(*get_gps_plugin_interface)();
66         get_gps_plugin_interface = dlsym(*plugin_handle, "get_gps_plugin_interface");
67         if (!get_gps_plugin_interface) {
68                 LOG_GPS(DBG_ERR, "Failed to find entry symbol in plugin module.");
69                 dlclose(*plugin_handle);
70                 return FALSE;
71         }
72
73         g_plugin = get_gps_plugin_interface();
74
75         if (!g_plugin) {
76                 LOG_GPS(DBG_ERR, "Failed to find load symbol in plugin module.");
77                 dlclose(*plugin_handle);
78                 return FALSE;
79         }
80         LOG_GPS(DBG_LOW, "Success to load plugin module");
81
82         return TRUE;
83 }
84
85 int unload_plugin_module(void *plugin_handle)
86 {
87         if (plugin_handle == NULL) {
88                 LOG_GPS(DBG_ERR, "plugin_handle is already NULL.");
89                 return FALSE;
90         }
91
92         dlclose(plugin_handle);
93
94         return TRUE;
95 }
96
97 const gps_plugin_interface *get_plugin_module(void)
98 {
99         return g_plugin;
100 }