Tizen 2.1 base
[platform/core/system/sync-agent.git] / src / framework / plugin / data_converter_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 <stdio.h>
20
21 #include "utility/sync_util.h"
22 #include "plugin/data_converter_plugin.h"
23
24 #ifndef SYNC_AGENT_LOG
25 #undef LOG_TAG
26 #define LOG_TAG "AF_PlugIn"
27 #endif
28
29 static int current_plugin_count_dataconverter = 0;
30
31 static plugin_data_converter_s plugIn_repository[PLUGIN_MAX_DATA_CONVERTER];
32
33 static int _find_data_converter_plugin(int plugin_id);
34
35 plugin_data_converter_func_set_s plugin_get_data_converter_func_set(void *plugin_handle, plugin_error_e * error_code)
36 {
37         _EXTERN_FUNC_ENTER;
38
39         *error_code = PLUGIN_SUCCESS;
40
41         plugin_data_converter_func_set_s func_set;
42
43         func_set.func_converter = dlsym(plugin_handle, "sync_agent_plugin_converter");
44         func_set.func_replace_converter = dlsym(plugin_handle, "sync_agent_plugin_replace_converter");
45         func_set.func_reverse_converter = dlsym(plugin_handle, "sync_agent_plugin_reverse_converter");
46         func_set.func_alloc_object = dlsym(plugin_handle, "sync_agent_plugin_alloc_object");
47         func_set.func_free_object = dlsym(plugin_handle, "sync_agent_plugin_free_object");
48         func_set.func_set_value = dlsym(plugin_handle, "sync_agent_plugin_set_value");
49         func_set.func_get_value = dlsym(plugin_handle, "sync_agent_plugin_get_value");
50         func_set.func_get_obj_info = dlsym(plugin_handle, "sync_agent_plugin_get_obj_info");
51         func_set.func_free_obj_info = dlsym(plugin_handle, "sync_agent_plugin_free_obj_info");
52
53         _EXTERN_FUNC_EXIT;
54
55         return func_set;
56 }
57
58 plugin_error_e plugin_register_plugin_data_converter(plugin_data_converter_s plugin)
59 {
60         _EXTERN_FUNC_ENTER;
61
62         if (current_plugin_count_dataconverter >= PLUGIN_MAX_DATA_CONVERTER) {
63                 return PLUGIN_FULL_CAPACITY;
64         }
65
66         plugIn_repository[current_plugin_count_dataconverter] = plugin;
67
68         current_plugin_count_dataconverter++;
69
70         _EXTERN_FUNC_EXIT;
71
72         return PLUGIN_SUCCESS;
73 }
74
75 void plugin_clear_plugin_data_converter()
76 {
77         _EXTERN_FUNC_ENTER;
78
79         int i;
80         for (i = 0; i < current_plugin_count_dataconverter; i++) {
81                 memset(&(plugIn_repository[i].plugin_info), 0x00, sizeof(plugin_info_s));
82                 memset(&(plugIn_repository[i].func_set), 0x00, sizeof(plugin_data_converter_func_set_s));
83                 memset(&(plugIn_repository[i]), 0x00, sizeof(plugin_data_converter_s));
84         }
85
86         _EXTERN_FUNC_EXIT;
87 }
88
89 const plugin_data_converter_s *plugin_get_data_converter_plugin_repository(int *count)
90 {
91         _EXTERN_FUNC_ENTER;
92
93         *count = current_plugin_count_dataconverter;
94
95         _EXTERN_FUNC_EXIT;
96
97         return plugIn_repository;
98 }
99
100 plugin_converter_cb plugin_get_function_converter(int plugin_id)
101 {
102         _EXTERN_FUNC_ENTER;
103
104         int index = _find_data_converter_plugin(plugin_id);
105         if (index == -1) {
106                 return NULL;
107         }
108
109         _EXTERN_FUNC_EXIT;
110
111         return plugIn_repository[index].func_set.func_converter;
112 }
113
114 plugin_replace_converter_cb plugin_get_function_replace_converter(int plugin_id)
115 {
116         _EXTERN_FUNC_ENTER;
117
118         int index = _find_data_converter_plugin(plugin_id);
119         if (index == -1) {
120                 return NULL;
121         }
122
123         _EXTERN_FUNC_EXIT;
124
125         return plugIn_repository[index].func_set.func_replace_converter;
126 }
127
128 plugin_reverse_converter_cb plugin_get_function_reverse_converter(int plugin_id)
129 {
130         _EXTERN_FUNC_ENTER;
131
132         int index = _find_data_converter_plugin(plugin_id);
133         if (index == -1) {
134                 return NULL;
135         }
136
137         _EXTERN_FUNC_EXIT;
138
139         return plugIn_repository[index].func_set.func_reverse_converter;
140 }
141
142 plugin_alloc_object_cb plugin_get_function_alloc_object(int plugIn_id)
143 {
144         _EXTERN_FUNC_ENTER;
145
146         int index = _find_data_converter_plugin(plugIn_id);
147         if (index == -1) {
148                 return NULL;
149         }
150
151         _EXTERN_FUNC_EXIT;
152
153         return plugIn_repository[index].func_set.func_alloc_object;
154 }
155
156 plugin_free_object_cb plugin_get_function_free_object(int plugin_id)
157 {
158         _EXTERN_FUNC_ENTER;
159
160         int index = _find_data_converter_plugin(plugin_id);
161         if (index == -1) {
162                 return NULL;
163         }
164
165         _EXTERN_FUNC_EXIT;
166
167         return plugIn_repository[index].func_set.func_free_object;
168 }
169
170 plugin_set_value_cb plugin_get_function_set_value_to_object(int plugin_id)
171 {
172         _EXTERN_FUNC_ENTER;
173
174         int index = _find_data_converter_plugin(plugin_id);
175         if (index == -1) {
176                 return NULL;
177         }
178
179         _EXTERN_FUNC_EXIT;
180
181         return plugIn_repository[index].func_set.func_set_value;
182 }
183
184 plugin_get_value_cb plugin_get_function_get_value_to_object(int plugin_id)
185 {
186         _EXTERN_FUNC_ENTER;
187
188         int index = _find_data_converter_plugin(plugin_id);
189         if (index == -1) {
190                 return NULL;
191         }
192
193         _EXTERN_FUNC_EXIT;
194
195         return plugIn_repository[index].func_set.func_get_value;
196 }
197
198 plugin_get_obj_info_cb plugin_get_function_get_obj_info(int plugin_id)
199 {
200         _EXTERN_FUNC_ENTER;
201
202         int index = _find_data_converter_plugin(plugin_id);
203         if (index == -1) {
204                 return NULL;
205         }
206
207         _EXTERN_FUNC_EXIT;
208
209         return plugIn_repository[index].func_set.func_get_obj_info;
210 }
211
212 plugin_free_obj_info_cb plugin_get_function_free_obj_info(int plugin_id)
213 {
214         _EXTERN_FUNC_ENTER;
215
216         int index = _find_data_converter_plugin(plugin_id);
217         if (index == -1) {
218                 return NULL;
219         }
220
221         _EXTERN_FUNC_EXIT;
222
223         return plugIn_repository[index].func_set.func_free_obj_info;
224 }
225
226 static int _find_data_converter_plugin(int plugin_id)
227 {
228         _INNER_FUNC_ENTER;
229
230         int i = 0;
231         for (; i < current_plugin_count_dataconverter; i++) {
232                 if (plugIn_repository[i].plugin_info.plugin_id == plugin_id) {
233                         _INNER_FUNC_EXIT;
234                         return i;
235                 }
236         }
237
238         return -1;
239 }