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.
21 #include <glib-object.h>
22 #include <glib-unix.h>
24 #include "service_adaptor.h"
25 #include "service_discovery.h"
26 #include "service_federation.h"
30 #include "sal_contact.h"
31 #include "sal_storage.h"
32 #include "sal_observer.h"
34 //******************************************************************************
35 //* Global variables and defines
36 //******************************************************************************
37 #define SAL_PLUGIN_METADATA_KEY_ADAPTOR "service-adaptor"
39 static service_adaptor_h g_service_adaptor = NULL;
41 //******************************************************************************
43 //******************************************************************************
45 //******************************************************************************
46 //* Private interface definition
47 //******************************************************************************
50 * @brief create spec file
54 static service_adaptor_error_e _sal_create_spec_file(service_adaptor_h sal)
59 RETV_IF(NULL == sal, SERVICE_ADAPTOR_ERROR_INVALID_PARAMETER);
65 * @brief destroy spec file
69 static service_adaptor_error_e _sal_destroy_spec_file(service_adaptor_h sal)
74 RETV_IF(NULL == sal, SERVICE_ADAPTOR_ERROR_INVALID_PARAMETER);
80 * @brief start service adaptor
84 static service_adaptor_error_e _sal_start(service_adaptor_h sal)
89 RETV_IF(NULL == sal, SERVICE_ADAPTOR_ERROR_INVALID_PARAMETER);
91 ret = auth_adaptor_start(sal->auth);
92 RETVM_IF(SERVICE_ADAPTOR_ERROR_NONE != ret, SERVICE_ADAPTOR_ERROR_INTERNAL, "auth_adaptor_start() Fail(%d)", ret);
94 ret = contact_adaptor_start(sal->contact);
95 RETVM_IF(SERVICE_ADAPTOR_ERROR_NONE != ret, SERVICE_ADAPTOR_ERROR_INTERNAL, "contact_adaptor_start() Fail(%d)", ret);
97 ret = storage_adaptor_start(sal->storage);
98 RETVM_IF(SERVICE_ADAPTOR_ERROR_NONE != ret, SERVICE_ADAPTOR_ERROR_INTERNAL, "storage_adaptor_start() Fail(%d)", ret);
100 g_mutex_lock(&sal->mutex);
101 sal->start = sal->start + 1;
102 g_cond_signal(&sal->cond);
103 g_mutex_unlock(&sal->mutex);
109 * @brief stop service adaptor
113 static service_adaptor_error_e _sal_stop(service_adaptor_h sal)
118 RETV_IF(NULL == sal, SERVICE_ADAPTOR_ERROR_INVALID_PARAMETER);
119 RETVM_IF(0 == sal->start, SERVICE_ADAPTOR_ERROR_INTERNAL, "could not start service adaptor");
121 ret += auth_adaptor_stop(sal->auth);
122 ret += contact_adaptor_stop(sal->contact);
123 ret += storage_adaptor_stop(sal->storage);
124 RETVM_IF(SERVICE_ADAPTOR_ERROR_NONE != ret, SERVICE_ADAPTOR_ERROR_INTERNAL, "could not stop service adaptor(%d)", ret);
132 * @brief create service adaptor
136 static service_adaptor_h _sal_create()
140 service_adaptor_h sal = NULL;
142 // 1) create auth adaptor handle
143 auth_adaptor_h auth = sal_auth_create_handle();
144 TRYM_IF(NULL == auth, "sal_auth_create_handle() Fail");
146 // 2) create contact adaptor handle
147 contact_adaptor_h contact = sal_contact_create_handle();
148 TRYM_IF(NULL == contact, "sal_contact_create_handle() Fail");
150 // 3) create storage adaptor handle
151 storage_adaptor_h storage = sal_storage_create_handle();
152 TRYM_IF(NULL == storage, "sal_storage_create_handle() Fail");
154 // 4) register auth adaptor listener
155 auth_adaptor_listener_h auth_listener = sal_auth_register_listener(auth);
156 TRYM_IF(NULL == auth_listener, "sal_auth_register_listener() Fail");
158 // 5) register contact adaptor listener
159 contact_adaptor_listener_h contact_listener = sal_contact_register_listener(contact);
160 TRYM_IF(NULL == contact_listener, "sal_contact_register_listener() Fail");
162 // 6) register storage adaptor listener
163 storage_adaptor_listener_h storage_listener = sal_storage_register_listener(storage);
164 TRYM_IF(NULL == storage_listener, "sal_storage_register_listener() Fail");
166 // 7) create service adaptor
167 sal = (service_adaptor_h) g_malloc0(sizeof(service_adaptor_s));
168 TRYM_IF(NULL == sal, "could not create service adaptor");
171 sal->contact = contact;
172 sal->storage = storage;
174 g_mutex_init(&sal->mutex);
175 g_cond_init(&sal->cond);
184 * @brief destroy service adaptor
188 static void _sal_destroy(service_adaptor_h sal)
194 // 1) destroy service list
195 if (NULL != sal->svc_list)
197 g_list_free(sal->svc_list);
198 sal->svc_list = NULL;
201 // 2) free service adaptor handle
208 * @brief init service adaptor
212 static service_adaptor_error_e _sal_init()
215 int ret = SERVICE_ADAPTOR_ERROR_NONE;
217 // 1) create adaptor (memory allocation)
218 service_adaptor_h sal = _sal_create();
219 RETVM_IF(NULL == sal, SERVICE_ADAPTOR_ERROR_INTERNAL, "_sal_create() Fail");
221 // 2) start adaptor (plugin loading)
222 ret = _sal_start(sal);
223 RETVM_IF(SERVICE_ADAPTOR_ERROR_NONE != ret, SERVICE_ADAPTOR_ERROR_INTERNAL, "_sal_start() Fail(%d)", ret);
225 // 3) start adaptor (spec file creation)
226 ret = _sal_create_spec_file(sal);
227 RETVM_IF(SERVICE_ADAPTOR_ERROR_NONE != ret, SERVICE_ADAPTOR_ERROR_INTERNAL, "_sal_create_spec_file() Fail(%d)", ret);
230 ret = sal_ipc_init_server();
231 RETVM_IF(SERVICE_ADAPTOR_ERROR_NONE != ret, SERVICE_ADAPTOR_ERROR_IPC_UNSTABLE, "sal_ipc_init_server() Fail(%d)", ret);
233 // 5) assign to global service adaptor handle
234 g_service_adaptor = sal;
236 // 6) register callback for package event
237 ret = sal_observer_start();
238 RETVM_IF(SERVICE_ADAPTOR_ERROR_NONE != ret, ret, "sal_observer_start() Fail(%d)", ret);
240 // 7) create service discovery
241 ret = service_discovery_create();
242 RETVM_IF(SERVICE_ADAPTOR_ERROR_NONE != ret, ret, "service_discovery_create() Fail(%d)", ret);
244 // 8) create service federation
245 ret = service_federation_create();
246 RETVM_IF(SERVICE_ADAPTOR_ERROR_NONE != ret, ret, "service_federation_create() Fail(%d)", ret);
252 * @brief deinit service adaptor
254 * @param[in] service_adaptor specifies handle of service adaptor
257 static void _sal_deinit(service_adaptor_h sal)
263 sal_ipc_deinit_server();
265 _sal_destroy_spec_file(sal);
278 * @brief main signal function
280 * @param[in] data specifies user data passed by main function
283 static gint _sigterm_callback(void *data)
285 g_main_loop_quit((GMainLoop*)data);
291 * @brief main function
293 * @param[in] argc specifies count of arguments
294 * @param[in] argv specifies value list of arguments
297 int main(int argc, char *argv[])
299 int ret = SERVICE_ADAPTOR_ERROR_NONE;
300 GMainLoop *loop = NULL;
302 #if !GLIB_CHECK_VERSION(2,32,0)
305 #if !GLIB_CHECK_VERSION(2,35,0)
310 RETVM_IF(SERVICE_ADAPTOR_ERROR_NONE != ret, 0, "_sal_init() Fail(%d)", ret);
312 // mainloop of main thread
313 loop = g_main_loop_new(NULL, FALSE);
315 // installing signal handlers
316 g_unix_signal_add_full(G_PRIORITY_HIGH, SIGINT,
317 _sigterm_callback, loop, NULL );
318 g_unix_signal_add_full(G_PRIORITY_HIGH, SIGTERM,
319 _sigterm_callback, loop, NULL );
321 // start application's main loop
322 g_main_loop_run(loop);
324 // cleanup after mainloop
325 g_main_loop_unref(loop);
327 service_adaptor_h sal = sal_get_handle();
333 //******************************************************************************
334 //* Public interface definition
335 //******************************************************************************
337 service_adaptor_h sal_get_handle()
341 return g_service_adaptor;
344 char *sal_get_root_path()
348 char *root_path = NULL;
349 char tnfs_root_path[FILE_PATH_LEN] = {0,};
351 // TODO: tmp -> need to use get_path APIs of TNFS
352 snprintf(tnfs_root_path, FILE_PATH_LEN, "%s", "/opt/storage/tnfs");
353 root_path = strdup(tnfs_root_path);
358 service_adaptor_error_e sal_connect(service_adaptor_h sal)
362 RETV_IF(NULL == sal, SERVICE_ADAPTOR_ERROR_INVALID_PARAMETER);
364 return SERVICE_ADAPTOR_ERROR_NONE;
367 service_adaptor_error_e sal_disconnect(service_adaptor_h sal)
371 RETV_IF(NULL == sal, SERVICE_ADAPTOR_ERROR_INVALID_PARAMETER);
373 return SERVICE_ADAPTOR_ERROR_NONE;