Add 3.0 APIs and sync APIs same as 2.4
[platform/core/convergence/service-adaptor.git] / adaptor / storage-adaptor / storage_adaptor.c
1 /*
2  * Storage Adaptor
3  *
4  * Copyright (c) 2014 - 2015 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
20 #include <stdio.h>
21 #include <glib.h>
22
23 #include "storage_adaptor.h"
24 #include "service_adaptor_internal.h"
25
26 //******************************************************************************
27 //* Global variables and defines
28 //******************************************************************************
29
30 //******************************************************************************
31 //* Private interface
32 //******************************************************************************
33
34 //******************************************************************************
35 //* Private interface definition
36 //******************************************************************************
37
38 static void _storage_adaptor_free_plugin(storage_plugin_h plugin)
39 {
40 }
41
42 //******************************************************************************
43 //* Public interface definition
44 //******************************************************************************
45
46 API storage_adaptor_h storage_adaptor_create()
47 {
48         SAL_FN_CALL;
49
50         storage_adaptor_h storage = (storage_adaptor_h) g_malloc0(sizeof(storage_adaptor_s));
51
52         g_mutex_init(&storage->mutex);
53
54         return storage;
55 }
56
57 API service_adaptor_error_e storage_adaptor_destroy(storage_adaptor_h storage)
58 {
59         SAL_FN_CALL;
60
61         RETV_IF(NULL == storage, SERVICE_ADAPTOR_ERROR_INVALID_PARAMETER);
62
63         g_mutex_lock(&storage->mutex);
64
65         if (0 != storage->start)
66         {
67                 storage_adaptor_stop(storage);
68         }
69
70         SAL_FREE(storage);
71
72         g_mutex_unlock(&storage->mutex);
73
74         return SERVICE_ADAPTOR_ERROR_NONE;
75 }
76
77 API service_adaptor_error_e storage_adaptor_start(storage_adaptor_h storage)
78 {
79         SAL_FN_CALL;
80
81         RETV_IF(NULL == storage, SERVICE_ADAPTOR_ERROR_INVALID_PARAMETER);
82
83         g_mutex_lock(&storage->mutex);
84
85         storage->start = 1;
86
87         g_mutex_unlock(&storage->mutex);
88
89         return SERVICE_ADAPTOR_ERROR_NONE;
90 }
91
92 API service_adaptor_error_e storage_adaptor_stop(storage_adaptor_h storage)
93 {
94         SAL_FN_CALL;
95
96         RETV_IF(NULL == storage, SERVICE_ADAPTOR_ERROR_INVALID_PARAMETER);
97
98         // TODO: notify storage adaptor stop to each plugin
99
100         g_mutex_lock(&storage->mutex);
101
102         storage->start = 0;
103
104         g_mutex_unlock(&storage->mutex);
105
106         return SERVICE_ADAPTOR_ERROR_NONE;
107 }
108
109 API service_adaptor_error_e storage_adaptor_register_listener(storage_adaptor_h storage, storage_adaptor_listener_h listener)
110 {
111         SAL_FN_CALL;
112
113         RETV_IF(NULL == storage, SERVICE_ADAPTOR_ERROR_INVALID_PARAMETER);
114         RETV_IF(NULL == listener, SERVICE_ADAPTOR_ERROR_INVALID_PARAMETER);
115
116         // TODO: register insert/ update/ delete callback for service-adaptor
117
118         return SERVICE_ADAPTOR_ERROR_NONE;
119 }
120
121 API service_adaptor_error_e storage_adaptor_unregister_listener(storage_adaptor_h storage, storage_adaptor_listener_h listener)
122 {
123         SAL_FN_CALL;
124
125         RETV_IF(NULL == storage, SERVICE_ADAPTOR_ERROR_INVALID_PARAMETER);
126         RETV_IF(NULL == listener, SERVICE_ADAPTOR_ERROR_INVALID_PARAMETER);
127
128         // TODO: unregister insert/ update/ delete callback for service-adaptor
129
130         return SERVICE_ADAPTOR_ERROR_NONE;
131 }
132
133 API service_adaptor_error_e storage_adaptor_create_plugin(const char *uri, const char *name, const char *package, storage_plugin_h *plugin)
134 {
135         SAL_FN_CALL;
136
137         RETV_IF(NULL == uri, SERVICE_ADAPTOR_ERROR_INVALID_PARAMETER);
138         RETV_IF(NULL == name, SERVICE_ADAPTOR_ERROR_INVALID_PARAMETER);
139         RETV_IF(NULL == package, SERVICE_ADAPTOR_ERROR_INVALID_PARAMETER);
140
141         storage_plugin_h storage_plugin = (storage_plugin_h) g_malloc0(sizeof(storage_plugin_s));
142         storage_plugin->uri = strdup(uri);
143         storage_plugin->name = strdup(name);
144         storage_plugin->package = strdup(package);
145
146         g_mutex_init(&storage_plugin->mutex);
147         g_cond_init(&storage_plugin->cond);
148
149         *plugin = storage_plugin;
150
151         return SERVICE_ADAPTOR_ERROR_NONE;
152 }
153
154 API service_adaptor_error_e storage_adaptor_destroy_plugin(storage_plugin_h plugin)
155 {
156         SAL_FN_CALL;
157
158         RETV_IF(NULL == plugin, SERVICE_ADAPTOR_ERROR_INVALID_PARAMETER);
159
160         SAL_FREE(plugin->uri);
161         SAL_FREE(plugin->name);
162         SAL_FREE(plugin);
163
164         return SERVICE_ADAPTOR_ERROR_NONE;
165 }
166
167 API service_adaptor_error_e storage_adaptor_register_plugin_service(storage_plugin_h plugin, GHashTable *service)
168 {
169         SAL_FN_CALL;
170
171         RETV_IF(NULL == plugin, SERVICE_ADAPTOR_ERROR_INVALID_PARAMETER);
172         RETV_IF(NULL == service, SERVICE_ADAPTOR_ERROR_INVALID_PARAMETER);
173
174         int ret = SERVICE_ADAPTOR_ERROR_NONE;
175
176         plugin->cloud = (cloud_service_h) g_malloc0(sizeof(cloud_service_s));
177         ret = cloud_register_service(plugin->cloud, service);
178
179         if (SERVICE_ADAPTOR_ERROR_NONE != ret)
180         {
181                 SAL_INFO("could not find the function for cloud");
182                 SAL_FREE(plugin->cloud);
183         }
184 /*
185         plugin->posix = (posix_service_h) g_malloc0(sizeof(posix_service_s));
186         ret = posix_register_service(plugin->posix, service);
187
188         if (SERVICE_ADAPTOR_ERROR_NONE != ret)
189         {
190                 SAL_INFO("could not find the function for posix");
191                 SAL_FREE(plugin->posix);
192         }
193 */
194         return SERVICE_ADAPTOR_ERROR_NONE;
195 }
196
197 API service_adaptor_error_e storage_adaptor_unregister_plugin_service(storage_plugin_h plugin)
198 {
199         SAL_FN_CALL;
200
201         RETV_IF(NULL == plugin, SERVICE_ADAPTOR_ERROR_INVALID_PARAMETER);
202
203         if (NULL != plugin->cloud)
204         {
205                 cloud_unregister_service(plugin->cloud);
206                 SAL_FREE(plugin->cloud);
207         }
208 /*
209         if (NULL != plugin->posix)
210         {
211                 posix_unregister_service(plugin->posix);
212                 SAL_FREE(plugin->posix);
213         }
214 */
215         return SERVICE_ADAPTOR_ERROR_NONE;
216 }
217
218 API service_adaptor_error_e storage_adaptor_add_plugin(storage_adaptor_h storage, storage_plugin_h plugin)
219 {
220         SAL_FN_CALL;
221
222         RETV_IF(NULL == storage, SERVICE_ADAPTOR_ERROR_INVALID_PARAMETER);
223         RETV_IF(NULL == plugin, SERVICE_ADAPTOR_ERROR_INVALID_PARAMETER);
224
225         g_mutex_lock(&storage->mutex);
226
227         storage->plugins = g_list_append(storage->plugins, plugin);
228
229         g_mutex_unlock(&storage->mutex);
230
231         return SERVICE_ADAPTOR_ERROR_NONE;
232 }
233
234 API service_adaptor_error_e storage_adaptor_remove_plugin(storage_adaptor_h storage, storage_plugin_h plugin)
235 {
236         SAL_FN_CALL;
237
238         RETV_IF(NULL == storage, SERVICE_ADAPTOR_ERROR_INVALID_PARAMETER);
239         RETV_IF(NULL == plugin, SERVICE_ADAPTOR_ERROR_INVALID_PARAMETER);
240
241         g_mutex_lock(&storage->mutex);
242
243         RETV_IF(NULL == g_list_find(storage->plugins, plugin), SERVICE_ADAPTOR_ERROR_INVALID_PARAMETER);
244
245         storage->plugins = g_list_remove(storage->plugins, plugin);
246
247         _storage_adaptor_free_plugin(plugin);
248
249         g_mutex_unlock(&storage->mutex);
250
251         return SERVICE_ADAPTOR_ERROR_NONE;
252 }
253
254 API storage_plugin_h storage_adaptor_get_plugin(storage_adaptor_h storage, const char *uri)
255 {
256         SAL_FN_CALL;
257
258         RETV_IF(NULL == storage, NULL);
259         RETV_IF(NULL == uri, NULL);
260
261         g_mutex_lock(&storage->mutex);
262
263         storage_plugin_h plugin = NULL;
264
265         for (GList *list = g_list_first(storage->plugins); list != NULL; list = list->next)
266         {
267                 storage_plugin_h this = (storage_plugin_h) list->data;
268
269                 if (0 == strcmp(this->uri, uri))
270                 {
271                         plugin = this;
272                         break;
273                 }
274         }
275
276         g_mutex_unlock(&storage->mutex);
277
278         return plugin;
279 }
280
281 API char *storage_adaptor_get_uri(storage_adaptor_h storage, const char *package)
282 {
283         SAL_FN_CALL;
284
285         RETV_IF(NULL == storage, NULL);
286         RETV_IF(NULL == package, NULL);
287
288         g_mutex_lock(&storage->mutex);
289
290         char *uri = NULL;
291
292         for (GList *list = g_list_first(storage->plugins); list != NULL; list = list->next)
293         {
294                 storage_plugin_h this = (storage_plugin_h) list->data;
295
296                 if (0 == strcmp(this->package, package))
297                 {
298                         uri = this->uri;
299                         break;
300                 }
301         }
302
303         g_mutex_unlock(&storage->mutex);
304
305         return uri;
306 }
307
308 API service_adaptor_error_e storage_adaptor_ref_plugin(storage_adaptor_h storage, const char *uri)
309 {
310         SAL_FN_CALL;
311
312         RETV_IF(NULL == storage, SERVICE_ADAPTOR_ERROR_INVALID_PARAMETER);
313         RETV_IF(NULL == uri, SERVICE_ADAPTOR_ERROR_INVALID_PARAMETER);
314
315         g_mutex_lock(&storage->mutex);
316
317         int ret = SERVICE_ADAPTOR_ERROR_NO_DATA;
318
319         for (GList *list = g_list_first(storage->plugins); list != NULL; list = list->next)
320         {
321                 storage_plugin_h this = (storage_plugin_h) list->data;
322
323                 if (0 == strcmp(this->uri, uri))
324                 {
325                         ret = SERVICE_ADAPTOR_ERROR_NONE;
326                         // TODO: increase ref count
327                         break;
328                 }
329         }
330
331         g_mutex_unlock(&storage->mutex);
332
333         return ret;
334 }
335
336 API service_adaptor_error_e storage_adaptor_unref_plugin(storage_adaptor_h storage, const char *uri)
337 {
338         SAL_FN_CALL;
339
340         RETV_IF(NULL == storage, SERVICE_ADAPTOR_ERROR_INVALID_PARAMETER);
341         RETV_IF(NULL == uri, SERVICE_ADAPTOR_ERROR_INVALID_PARAMETER);
342
343         g_mutex_lock(&storage->mutex);
344
345         int ret = SERVICE_ADAPTOR_ERROR_NO_DATA;
346
347         for (GList *list = g_list_first(storage->plugins); list != NULL; list = list->next)
348         {
349                 storage_plugin_h this = (storage_plugin_h) list->data;
350
351                 if (0 == strcmp(this->uri, uri))
352                 {
353                         ret = SERVICE_ADAPTOR_ERROR_NONE;
354                         // TODO: decrease ref count
355                         break;
356                 }
357         }
358
359         g_mutex_unlock(&storage->mutex);
360
361         return ret;
362 }