upload tizen1.0 source
[framework/location/gps-manager.git] / gps-manager / plugin_module.c
1 /*
2  * gps-manager
3  *
4  * Copyright (c) 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 #include <stdio.h>
23 #include <string.h>
24 #include <dlfcn.h>
25 #include <unistd.h>
26 #include "plugin_module.h"
27 #include "setting.h"
28 #include "debug_util.h"
29
30 #define SPECIFIC_PLUGIN_PATH_PREFIX     "/usr/lib/libSLP-lbs-plugin-"
31 #define SPECIFIC_PLUGIN_PATH_POSTFIX ".so"
32
33 static const gps_plugin_interface *g_plugin;
34
35 int load_plugin_module(char *specific_name, void **plugin_handle)
36 {
37         char plugin_path[256];
38
39         if (specific_name[0] == '\0') {
40                 strncpy(plugin_path, GPS_MANAGER_PLUGIN_PATH, sizeof(plugin_path));
41         } else {
42                 snprintf(plugin_path, sizeof(plugin_path),
43                          SPECIFIC_PLUGIN_PATH_PREFIX
44                          "%s"
45                          SPECIFIC_PLUGIN_PATH_POSTFIX,
46                          specific_name);
47         }
48
49         if (access (plugin_path, R_OK) != 0) {
50                 strncpy(plugin_path, GPS_MANAGER_PLUGIN_PATH, sizeof(plugin_path));
51                 setting_set_int(REPLAY_ENABLED, 1);
52         }
53
54         *plugin_handle = dlopen(plugin_path, RTLD_NOW);
55         if (!*plugin_handle) {
56                 LOG_GPS(DBG_ERR, "Failed to load plugin module.");
57                 LOG_GPS(DBG_ERR, "%s", dlerror());
58                 return FALSE;
59         }
60
61         const gps_plugin_interface *(*get_gps_plugin_interface) ();
62         get_gps_plugin_interface = dlsym(*plugin_handle, "get_gps_plugin_interface");
63         if (!get_gps_plugin_interface) {
64                 LOG_GPS(DBG_ERR, "Failed to find entry symbol in plugin module.");
65                 dlclose(*plugin_handle);
66                 return FALSE;
67         }
68
69         g_plugin = get_gps_plugin_interface();
70
71         if (!g_plugin) {
72                 LOG_GPS(DBG_ERR, "Failed to find load symbol in plugin module.");
73                 dlclose(*plugin_handle);
74                 return FALSE;
75         }
76         LOG_GPS(DBG_LOW, "Success to load plugin module (%s).", plugin_path);
77
78         return TRUE;
79 }
80
81 int unload_plugin_module(void *plugin_handle)
82 {
83         if (plugin_handle == NULL) {
84                 LOG_GPS(DBG_ERR, "plugin_handle is already NULL.");
85                 return FALSE;
86         }
87
88         dlclose(plugin_handle);
89
90         return TRUE;
91 }
92
93 const gps_plugin_interface *get_plugin_module(void)
94 {
95         return g_plugin;
96 }