4 * Copyright (c) 2014 - 2015 Samsung Electronics Co., Ltd. All rights reserved.
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
23 #include "storage_adaptor.h"
24 #include "service_adaptor_internal.h"
26 //******************************************************************************
27 //* Global variables and defines
28 //******************************************************************************
30 //******************************************************************************
32 //******************************************************************************
34 //******************************************************************************
35 //* Private interface definition
36 //******************************************************************************
38 static void _storage_adaptor_free_plugin(storage_plugin_h plugin)
42 //******************************************************************************
43 //* Public interface definition
44 //******************************************************************************
46 API storage_adaptor_h storage_adaptor_create()
50 storage_adaptor_h storage = (storage_adaptor_h) g_malloc0(sizeof(storage_adaptor_s));
52 g_mutex_init(&storage->mutex);
57 API service_adaptor_error_e storage_adaptor_destroy(storage_adaptor_h storage)
61 RETV_IF(NULL == storage, SERVICE_ADAPTOR_ERROR_INVALID_PARAMETER);
63 g_mutex_lock(&storage->mutex);
65 if (0 != storage->start)
67 storage_adaptor_stop(storage);
72 g_mutex_unlock(&storage->mutex);
74 return SERVICE_ADAPTOR_ERROR_NONE;
77 API service_adaptor_error_e storage_adaptor_start(storage_adaptor_h storage)
81 RETV_IF(NULL == storage, SERVICE_ADAPTOR_ERROR_INVALID_PARAMETER);
83 g_mutex_lock(&storage->mutex);
87 g_mutex_unlock(&storage->mutex);
89 return SERVICE_ADAPTOR_ERROR_NONE;
92 API service_adaptor_error_e storage_adaptor_stop(storage_adaptor_h storage)
96 RETV_IF(NULL == storage, SERVICE_ADAPTOR_ERROR_INVALID_PARAMETER);
98 // TODO: notify storage adaptor stop to each plugin
100 g_mutex_lock(&storage->mutex);
104 g_mutex_unlock(&storage->mutex);
106 return SERVICE_ADAPTOR_ERROR_NONE;
109 API service_adaptor_error_e storage_adaptor_register_listener(storage_adaptor_h storage, storage_adaptor_listener_h listener)
113 RETV_IF(NULL == storage, SERVICE_ADAPTOR_ERROR_INVALID_PARAMETER);
114 RETV_IF(NULL == listener, SERVICE_ADAPTOR_ERROR_INVALID_PARAMETER);
116 // TODO: register insert/ update/ delete callback for service-adaptor
118 return SERVICE_ADAPTOR_ERROR_NONE;
121 API service_adaptor_error_e storage_adaptor_unregister_listener(storage_adaptor_h storage, storage_adaptor_listener_h listener)
125 RETV_IF(NULL == storage, SERVICE_ADAPTOR_ERROR_INVALID_PARAMETER);
126 RETV_IF(NULL == listener, SERVICE_ADAPTOR_ERROR_INVALID_PARAMETER);
128 // TODO: unregister insert/ update/ delete callback for service-adaptor
130 return SERVICE_ADAPTOR_ERROR_NONE;
133 API service_adaptor_error_e storage_adaptor_create_plugin(const char *uri, const char *name, const char *package, storage_plugin_h *plugin)
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);
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);
146 g_mutex_init(&storage_plugin->mutex);
147 g_cond_init(&storage_plugin->cond);
149 *plugin = storage_plugin;
151 return SERVICE_ADAPTOR_ERROR_NONE;
154 API service_adaptor_error_e storage_adaptor_destroy_plugin(storage_plugin_h plugin)
158 RETV_IF(NULL == plugin, SERVICE_ADAPTOR_ERROR_INVALID_PARAMETER);
160 SAL_FREE(plugin->uri);
161 SAL_FREE(plugin->name);
164 return SERVICE_ADAPTOR_ERROR_NONE;
167 API service_adaptor_error_e storage_adaptor_register_plugin_service(storage_plugin_h plugin, GHashTable *service)
171 RETV_IF(NULL == plugin, SERVICE_ADAPTOR_ERROR_INVALID_PARAMETER);
172 RETV_IF(NULL == service, SERVICE_ADAPTOR_ERROR_INVALID_PARAMETER);
174 int ret = SERVICE_ADAPTOR_ERROR_NONE;
176 plugin->cloud = (cloud_service_h) g_malloc0(sizeof(cloud_service_s));
177 ret = cloud_register_service(plugin->cloud, service);
179 if (SERVICE_ADAPTOR_ERROR_NONE != ret)
181 SAL_INFO("could not find the function for cloud");
182 SAL_FREE(plugin->cloud);
185 plugin->posix = (posix_service_h) g_malloc0(sizeof(posix_service_s));
186 ret = posix_register_service(plugin->posix, service);
188 if (SERVICE_ADAPTOR_ERROR_NONE != ret)
190 SAL_INFO("could not find the function for posix");
191 SAL_FREE(plugin->posix);
194 return SERVICE_ADAPTOR_ERROR_NONE;
197 API service_adaptor_error_e storage_adaptor_unregister_plugin_service(storage_plugin_h plugin)
201 RETV_IF(NULL == plugin, SERVICE_ADAPTOR_ERROR_INVALID_PARAMETER);
203 if (NULL != plugin->cloud)
205 cloud_unregister_service(plugin->cloud);
206 SAL_FREE(plugin->cloud);
209 if (NULL != plugin->posix)
211 posix_unregister_service(plugin->posix);
212 SAL_FREE(plugin->posix);
215 return SERVICE_ADAPTOR_ERROR_NONE;
218 API service_adaptor_error_e storage_adaptor_add_plugin(storage_adaptor_h storage, storage_plugin_h plugin)
222 RETV_IF(NULL == storage, SERVICE_ADAPTOR_ERROR_INVALID_PARAMETER);
223 RETV_IF(NULL == plugin, SERVICE_ADAPTOR_ERROR_INVALID_PARAMETER);
225 g_mutex_lock(&storage->mutex);
227 storage->plugins = g_list_append(storage->plugins, plugin);
229 g_mutex_unlock(&storage->mutex);
231 return SERVICE_ADAPTOR_ERROR_NONE;
234 API service_adaptor_error_e storage_adaptor_remove_plugin(storage_adaptor_h storage, storage_plugin_h plugin)
238 RETV_IF(NULL == storage, SERVICE_ADAPTOR_ERROR_INVALID_PARAMETER);
239 RETV_IF(NULL == plugin, SERVICE_ADAPTOR_ERROR_INVALID_PARAMETER);
241 g_mutex_lock(&storage->mutex);
243 RETV_IF(NULL == g_list_find(storage->plugins, plugin), SERVICE_ADAPTOR_ERROR_INVALID_PARAMETER);
245 storage->plugins = g_list_remove(storage->plugins, plugin);
247 _storage_adaptor_free_plugin(plugin);
249 g_mutex_unlock(&storage->mutex);
251 return SERVICE_ADAPTOR_ERROR_NONE;
254 API storage_plugin_h storage_adaptor_get_plugin(storage_adaptor_h storage, const char *uri)
258 RETV_IF(NULL == storage, NULL);
259 RETV_IF(NULL == uri, NULL);
261 g_mutex_lock(&storage->mutex);
263 storage_plugin_h plugin = NULL;
265 for (GList *list = g_list_first(storage->plugins); list != NULL; list = list->next)
267 storage_plugin_h this = (storage_plugin_h) list->data;
269 if (0 == strcmp(this->uri, uri))
276 g_mutex_unlock(&storage->mutex);
281 API char *storage_adaptor_get_uri(storage_adaptor_h storage, const char *package)
285 RETV_IF(NULL == storage, NULL);
286 RETV_IF(NULL == package, NULL);
288 g_mutex_lock(&storage->mutex);
292 for (GList *list = g_list_first(storage->plugins); list != NULL; list = list->next)
294 storage_plugin_h this = (storage_plugin_h) list->data;
296 if (0 == strcmp(this->package, package))
303 g_mutex_unlock(&storage->mutex);
308 API service_adaptor_error_e storage_adaptor_ref_plugin(storage_adaptor_h storage, const char *uri)
312 RETV_IF(NULL == storage, SERVICE_ADAPTOR_ERROR_INVALID_PARAMETER);
313 RETV_IF(NULL == uri, SERVICE_ADAPTOR_ERROR_INVALID_PARAMETER);
315 g_mutex_lock(&storage->mutex);
317 int ret = SERVICE_ADAPTOR_ERROR_NO_DATA;
319 for (GList *list = g_list_first(storage->plugins); list != NULL; list = list->next)
321 storage_plugin_h this = (storage_plugin_h) list->data;
323 if (0 == strcmp(this->uri, uri))
325 ret = SERVICE_ADAPTOR_ERROR_NONE;
326 // TODO: increase ref count
331 g_mutex_unlock(&storage->mutex);
336 API service_adaptor_error_e storage_adaptor_unref_plugin(storage_adaptor_h storage, const char *uri)
340 RETV_IF(NULL == storage, SERVICE_ADAPTOR_ERROR_INVALID_PARAMETER);
341 RETV_IF(NULL == uri, SERVICE_ADAPTOR_ERROR_INVALID_PARAMETER);
343 g_mutex_lock(&storage->mutex);
345 int ret = SERVICE_ADAPTOR_ERROR_NO_DATA;
347 for (GList *list = g_list_first(storage->plugins); list != NULL; list = list->next)
349 storage_plugin_h this = (storage_plugin_h) list->data;
351 if (0 == strcmp(this->uri, uri))
353 ret = SERVICE_ADAPTOR_ERROR_NONE;
354 // TODO: decrease ref count
359 g_mutex_unlock(&storage->mutex);