Tizen 2.1 base
[platform/core/system/sync-agent.git] / src / framework / plugin / network_access_plugin.c
1 /*
2  * sync-agent
3  * Copyright (c) 2012 Samsung Electronics Co., Ltd.
4  *
5  * Licensed under the Apache License, Version 2.0 (the License);
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 #include <dlfcn.h>
19 #include <stdlib.h>
20
21 #include "plugin/network_access_plugin.h"
22
23 #include "utility/sync_util.h"
24
25 #ifndef SYNC_AGENT_LOG
26 #undef LOG_TAG
27 #define LOG_TAG "AF_PlugIn"
28 #endif
29
30 static int current_plugin_count_network_access = 0;
31
32 static plugin_network_access_s plugin_repository[PLUGIN_MAX_NETWORK_ACCESS];
33
34 static int _find_network_access_plugin(int plugin_id);
35
36 plugin_network_access_func_set_s plugin_get_network_access_func_set(void *plugin_handle, plugin_error_e * error_code)
37 {
38         _EXTERN_FUNC_ENTER;
39
40         *error_code = PLUGIN_SUCCESS;
41
42         plugin_network_access_func_set_s func_set;
43
44         func_set.func_open_connection = dlsym(plugin_handle, "sync_agent_plugin_open_connection");
45         func_set.func_header_binding = dlsym(plugin_handle, "sync_agent_plugin_header_binding");
46         func_set.func_send_message = dlsym(plugin_handle, "sync_agent_plugin_send_message");
47         func_set.func_header_unbinding = dlsym(plugin_handle, "sync_agent_plugin_header_unbinding");
48         func_set.func_close_connection = dlsym(plugin_handle, "sync_agent_plugin_close_connection");
49         func_set.func_cancel_message = dlsym(plugin_handle, "sync_agent_plugin_cancel_message");
50         func_set.func_just_send_message = dlsym(plugin_handle, "sync_agent_plugin_just_send_message");
51         func_set.func_get_header_info = dlsym(plugin_handle, "sync_agent_plugin_get_header_info");
52         func_set.func_set_data_down_info = dlsym(plugin_handle, "sync_agent_plugin_set_data_download_info");
53         func_set.func_get_data_down_info = dlsym(plugin_handle, "sync_agent_plugin_get_data_download_info");
54         func_set.func_add_authentication_info = dlsym(plugin_handle, "sync_agent_plugin_add_authentication_info");
55         func_set.func_register_fw_mainloop_na = dlsym(plugin_handle, "sync_agent_plugin_register_fw_main_loop_network_access");
56         func_set.func_set_property = dlsym(plugin_handle, "sync_agent_plugin_set_property");
57         func_set.func_get_status = dlsym(plugin_handle, "sync_agent_plugin_get_status");
58
59         _EXTERN_FUNC_EXIT;
60
61         return func_set;
62 }
63
64 plugin_error_e plugin_register_plugin_network_access(plugin_network_access_s plugin)
65 {
66         _EXTERN_FUNC_ENTER;
67
68         if (current_plugin_count_network_access >= PLUGIN_MAX_NETWORK_ACCESS) {
69                 return PLUGIN_FULL_CAPACITY;
70         }
71
72         plugin_repository[current_plugin_count_network_access] = plugin;
73
74         current_plugin_count_network_access++;
75
76         _EXTERN_FUNC_EXIT;
77
78         return PLUGIN_SUCCESS;
79 }
80
81 void plugin_clear_plugin_network_access()
82 {
83         _EXTERN_FUNC_ENTER;
84
85         int i;
86         for (i = 0; i < current_plugin_count_network_access; i++) {
87                 memset(&(plugin_repository[i].plugin_info), 0x00, sizeof(plugin_info_s));
88                 memset(&(plugin_repository[i].func_set), 0x00, sizeof(plugin_network_access_func_set_s));
89                 memset(&(plugin_repository[i]), 0x00, sizeof(plugin_network_access_s));
90         }
91
92         _EXTERN_FUNC_EXIT;
93 }
94
95 const plugin_network_access_s *plugin_get_network_access_plugin_repository(int *count)
96 {
97         _EXTERN_FUNC_ENTER;
98
99         *count = current_plugin_count_network_access;
100
101         _EXTERN_FUNC_EXIT;
102
103         return plugin_repository;
104 }
105
106 int plugin_use_network(int plugin_id)
107 {
108         _EXTERN_FUNC_ENTER;
109
110         int index = _find_network_access_plugin(plugin_id);
111         if (index == -1) {
112                 return -1;
113         }
114
115         _EXTERN_FUNC_EXIT;
116
117         return plugin_repository[index].use_network;
118 }
119
120 int plugin_get_network_access_plugin_id_list(int **plugin_id_list)
121 {
122         _EXTERN_FUNC_ENTER;
123
124         if (current_plugin_count_network_access <= 0) {
125                 return 0;
126         }
127
128         *plugin_id_list = (int *)calloc(current_plugin_count_network_access, sizeof(int));
129         if (*plugin_id_list == NULL) {
130                 _DEBUG_ERROR("CALLOC failed !!!");
131                 return 0;
132         }
133
134         int i = 0;
135         for (; i < current_plugin_count_network_access; i++) {
136                 (*plugin_id_list)[i] = plugin_repository[i].plugin_info.plugin_id;
137         }
138
139         _EXTERN_FUNC_EXIT;
140
141         return current_plugin_count_network_access;
142 }
143
144 plugin_open_connection_cb plugin_get_function_connection(int plugin_id)
145 {
146         _EXTERN_FUNC_ENTER;
147
148         int index = _find_network_access_plugin(plugin_id);
149         if (index == -1) {
150                 return NULL;
151         }
152
153         _EXTERN_FUNC_EXIT;
154
155         return plugin_repository[index].func_set.func_open_connection;
156 }
157
158 plugin_header_binding_cb plugin_get_function_header_binding(int plugin_id)
159 {
160         _EXTERN_FUNC_ENTER;
161
162         int index = _find_network_access_plugin(plugin_id);
163         _DEBUG_INFO("index : %d", index);
164         if (index == -1) {
165                 return NULL;
166         }
167
168         _EXTERN_FUNC_EXIT;
169
170         return plugin_repository[index].func_set.func_header_binding;
171 }
172
173 plugin_send_message_cb plugin_get_function_send_message(int plugin_id)
174 {
175         _EXTERN_FUNC_ENTER;
176
177         int index = _find_network_access_plugin(plugin_id);
178         _DEBUG_INFO("index : %d", index);
179         if (index == -1) {
180                 return NULL;
181         }
182
183         _EXTERN_FUNC_EXIT;
184
185         return plugin_repository[index].func_set.func_send_message;
186 }
187
188 plugin_header_unbinding_cb plugin_get_function_header_unbinding(int plugin_id)
189 {
190         _EXTERN_FUNC_ENTER;
191
192         int index = _find_network_access_plugin(plugin_id);
193         _DEBUG_INFO("index : %d", index);
194         if (index == -1) {
195                 return NULL;
196         }
197
198         _EXTERN_FUNC_EXIT;
199
200         return plugin_repository[index].func_set.func_header_unbinding;
201 }
202
203 plugin_close_connection_cb plugin_get_function_close_connection(int plugin_id)
204 {
205         _EXTERN_FUNC_ENTER;
206
207         int index = _find_network_access_plugin(plugin_id);
208         _DEBUG_INFO("index : %d", index);
209         if (index == -1) {
210                 return NULL;
211         }
212
213         _EXTERN_FUNC_EXIT;
214
215         return plugin_repository[index].func_set.func_close_connection;
216 }
217
218 plugin_cancel_message_cb plugin_get_function_cancel_message(int plugin_id)
219 {
220         _EXTERN_FUNC_ENTER;
221
222         int index = _find_network_access_plugin(plugin_id);
223         _DEBUG_INFO("index : %d", index);
224         if (index == -1) {
225                 return NULL;
226         }
227
228         _EXTERN_FUNC_EXIT;
229
230         return plugin_repository[index].func_set.func_cancel_message;
231 }
232
233 plugin_just_send_message_cb plugin_get_function_just_send_message(int plugin_id)
234 {
235         _EXTERN_FUNC_ENTER;
236
237         int index = _find_network_access_plugin(plugin_id);
238         _DEBUG_INFO("index : %d", index);
239         if (index == -1) {
240                 return NULL;
241         }
242
243         _EXTERN_FUNC_EXIT;
244
245         return plugin_repository[index].func_set.func_just_send_message;
246 }
247
248 plugin_get_header_info_cb plugin_get_function_get_header_info(int plugin_id)
249 {
250         _EXTERN_FUNC_ENTER;
251
252         int index = _find_network_access_plugin(plugin_id);
253         _DEBUG_INFO("index : %d", index);
254         if (index == -1) {
255                 return NULL;
256         }
257
258         _EXTERN_FUNC_EXIT;
259
260         return plugin_repository[index].func_set.func_get_header_info;
261 }
262
263 plugin_set_data_download_info_cb plugin_get_function_set_data_download_info(int plugin_id)
264 {
265         _EXTERN_FUNC_ENTER;
266
267         int index = _find_network_access_plugin(plugin_id);
268         _DEBUG_INFO("index : %d", index);
269         if (index == -1) {
270                 return NULL;
271         }
272
273         _EXTERN_FUNC_EXIT;
274
275         return plugin_repository[index].func_set.func_set_data_down_info;
276 }
277
278 plugin_get_data_download_info_cb plugin_get_function_get_data_download_info(int plugin_id)
279 {
280         _EXTERN_FUNC_ENTER;
281
282         int index = _find_network_access_plugin(plugin_id);
283         _DEBUG_INFO("index : %d", index);
284         if (index == -1) {
285                 return NULL;
286         }
287
288         _EXTERN_FUNC_EXIT;
289
290         return plugin_repository[index].func_set.func_get_data_down_info;
291 }
292
293 plugin_add_authentication_info_cb plugin_get_function_add_authentication_info(int plugin_id)
294 {
295         _EXTERN_FUNC_ENTER;
296
297         int index = _find_network_access_plugin(plugin_id);
298         _DEBUG_INFO("index : %d", index);
299         if (index == -1) {
300                 return NULL;
301         }
302
303         _EXTERN_FUNC_EXIT;
304
305         return plugin_repository[index].func_set.func_add_authentication_info;
306 }
307
308 plugin_register_fw_mainloop_na_cb plugin_get_function_register_fw_mainloop_na(int plugin_id)
309 {
310         _EXTERN_FUNC_ENTER;
311
312         int index = _find_network_access_plugin(plugin_id);
313         _DEBUG_INFO("index : %d", index);
314         if (index == -1) {
315                 return NULL;
316         }
317
318         _EXTERN_FUNC_EXIT;
319
320         return plugin_repository[index].func_set.func_register_fw_mainloop_na;
321 }
322
323 plugin_set_property_cb plugin_get_function_set_property(int plugin_id)
324 {
325         _EXTERN_FUNC_ENTER;
326
327         int index = _find_network_access_plugin(plugin_id);
328         _DEBUG_INFO("index : %d", index);
329         if (index == -1) {
330                 return NULL;
331         }
332
333         _EXTERN_FUNC_EXIT;
334
335         return plugin_repository[index].func_set.func_set_property;
336 }
337
338 plugin_get_status_cb plugin_get_function_get_status(int plugin_id)
339 {
340         _EXTERN_FUNC_ENTER;
341
342         int index = _find_network_access_plugin(plugin_id);
343         _DEBUG_INFO("index : %d", index);
344         if (index == -1) {
345                 return NULL;
346         }
347
348         _EXTERN_FUNC_EXIT;
349
350         return plugin_repository[index].func_set.func_get_status;
351 }
352
353 static int _find_network_access_plugin(int plugin_id)
354 {
355         _INNER_FUNC_ENTER;
356
357         int i = 0;
358         for (; i < current_plugin_count_network_access; i++) {
359                 if (plugin_repository[i].plugin_info.plugin_id == plugin_id) {
360                         _INNER_FUNC_EXIT;
361                         return i;
362                 }
363         }
364
365         return -1;
366 }