removed wrong contact points & added two authors
[platform/core/location/lbs-server.git] / lbs-server / src / nps_plugin_module.c
1 /*
2  * lbs-server
3  *
4  * Copyright (c) 2010-2013 Samsung Electronics Co., Ltd All Rights Reserved
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18
19 #include <stdio.h>
20 #include <string.h>
21 #include <dlfcn.h>
22 #include <unistd.h>
23 #include "nps_plugin_module.h"
24 #include "setting.h"
25 #include "debug_util.h"
26
27 #define PLUGIN_PATH_POSTFIX     ".so"
28
29
30 static const nps_plugin_interface  *g_plugin = NULL;
31 static int g_is_nps_dummy_module = FALSE;
32
33 int dummy_load(void);
34 int dummy_unload(void);
35 int dummy_start(unsigned long period, LocationCallback cb, void *arg, void **handle);
36 int dummy_stop(void *handle, CancelCallback cb, void *arg);
37 void dummy_get_offline_token(const unsigned char *key, unsigned int keyLengh, OfflineTokenCallback cb, void *arg);
38 int dummy_offline_location(const unsigned char *key, unsigned int keyLength, const unsigned char *token, unsigned int tokenSize, LocationCallback cb, void *arg);
39 void dummy_cell_location(LocationCallback callback, void *arg);
40
41 static nps_plugin_interface g_dummy = {
42         .load = dummy_load,
43         .unload = dummy_unload,
44         .start = dummy_start,
45         .stop = dummy_stop,
46         .getOfflineToken = dummy_get_offline_token,
47         .offlineLocation = dummy_offline_location,
48         .cellLocation = dummy_cell_location
49 };
50
51 int nps_is_dummy_plugin_module()
52 {
53         return g_is_nps_dummy_module;
54 }
55
56 int nps_load_plugin_module(void **plugin_handle)
57 {
58         LOG_NPS(DBG_LOW, "Begin to load NPS plugin module");
59
60         char plugin_path[256] = {0};
61
62         strncpy(plugin_path, NPS_PLUGIN_PATH, sizeof(plugin_path));
63
64         if (access(plugin_path, R_OK) != 0) {
65                 LOG_NPS(DBG_ERR, "Failed to access NPS plugin[%s]. Dummy loading", plugin_path);
66                 g_plugin = &g_dummy;
67                 g_is_nps_dummy_module = TRUE;
68                 return TRUE;
69         }
70
71         *plugin_handle = dlopen(plugin_path, RTLD_NOW);
72         if (!*plugin_handle) {
73                 LOG_NPS(DBG_ERR, "Failed to load plugin module. [%s]", plugin_path);
74                 LOG_NPS(DBG_ERR, "%s", dlerror());
75                 return FALSE;
76         }
77
78         const nps_plugin_interface *(*get_nps_plugin_interface)();
79         get_nps_plugin_interface = dlsym(*plugin_handle, "get_nps_plugin_interface");
80         if (!get_nps_plugin_interface) {
81                 LOG_NPS(DBG_ERR, "Failed to find entry symbol in plugin module.");
82                 dlclose(*plugin_handle);
83                 return FALSE;
84         }
85
86         g_plugin = get_nps_plugin_interface();
87
88         if (!g_plugin) {
89                 LOG_NPS(DBG_ERR, "Failed to find load symbol in plugin module.");
90                 dlclose(*plugin_handle);
91                 return FALSE;
92         }
93         LOG_NPS(DBG_LOW, "Success to load plugin module (%s).", plugin_path);
94
95         return TRUE;
96 }
97
98 int nps_unload_plugin_module(void *plugin_handle)
99 {
100         if (plugin_handle == NULL) {
101                 LOG_NPS(DBG_ERR, "plugin_handle is already NULL.");
102                 return FALSE;
103         }
104
105         dlclose(plugin_handle);
106
107         if (g_plugin)
108                 g_plugin = NULL;
109
110         return TRUE;
111 }
112
113 const nps_plugin_interface *get_nps_plugin_module(void)
114 {
115         return g_plugin;
116 }
117
118 int dummy_load(void)
119 {
120         LOG_NPS(DBG_ERR, "Dummy func.");
121         return FALSE;
122 }
123
124 int dummy_unload(void)
125 {
126         LOG_NPS(DBG_ERR, "Dummy func.");
127         return FALSE;
128 }
129
130 int dummy_start(unsigned long period, LocationCallback cb, void *arg, void **handle)
131 {
132         LOG_NPS(DBG_ERR, "Dummy func.");
133         return FALSE;
134 }
135
136 int dummy_stop(void *handle, CancelCallback cb, void *arg)
137 {
138         LOG_NPS(DBG_ERR, "Dummy func.");
139         return FALSE;
140 }
141
142 void dummy_get_offline_token(const unsigned char *key, unsigned int keyLengh, OfflineTokenCallback cb, void *arg)
143 {
144         LOG_NPS(DBG_ERR, "Dummy func.");
145 }
146
147 int dummy_offline_location(const unsigned char *key, unsigned int keyLength, const unsigned char *token, unsigned int tokenSize, LocationCallback cb, void *arg)
148 {
149         LOG_NPS(DBG_ERR, "Dummy func.");
150         return FALSE;
151 }
152
153 void dummy_cell_location(LocationCallback callback, void *arg)
154 {
155         LOG_NPS(DBG_ERR, "Dummy func.");
156 }