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