Enabled On-demand mode
[platform/core/convergence/service-adaptor.git] / server / src / service-adaptor.c
1 /*
2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3 *
4 * Licensed under the Apache License, Version 2.0 (the License);
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an AS IS BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <string.h>
20 #include <unistd.h>
21 #include <fcntl.h>
22 #include <errno.h>
23 #include <sys/types.h>
24 #include <sys/socket.h>
25 #include <sys/time.h>
26 #include <netinet/in.h>
27 #include <arpa/inet.h>
28 #include <glib-object.h>
29 #include <glib-unix.h>
30
31 #include "service-adaptor.h"
32 #include "service-adaptor-auth.h"
33 #include "service-adaptor-contact.h"
34 #include "service-adaptor-message.h"
35 #include "service-adaptor-shop.h"
36 #include "service-adaptor-storage.h"
37 #include "service-adaptor-push.h"
38 #include "service-adaptor-plugin.h"
39 #include "service-adaptor-type.h"
40 #include "service-adaptor-log.h"
41 #include "dbus-ipc.h"
42 #include "dbus-server.h"
43 #include "dbus-service-adaptor.h"
44 #include "util/client_checker.h"
45 #include "util/ping_manager.h"
46
47 #include <bundle.h>
48
49 service_adaptor_h g_service_adaptor = (void *) NULL;
50
51 static char *safe_strdup(const char *str)
52 {
53         if (NULL == str) {
54                 return NULL;
55         } else {
56                 return strdup(str);
57         }
58 }
59
60 static void safe_free(void **ptrptr)
61 {
62         if (ptrptr != NULL && *ptrptr != NULL) {
63                 free(*ptrptr);
64                 *ptrptr = NULL;
65         }
66 }
67
68 /******************************************************************************
69  * Global variables and defines
70  ******************************************************************************/
71
72 static GMainLoop *g_default_loop = NULL;
73
74 /******************************************************************************
75  * Private interface
76  ******************************************************************************/
77
78 #define __init_context_info_s(x)        do { \
79                                                 (x).user_id = NULL; \
80                                                 (x).app_id = NULL; \
81                                                 (x).service_id = 0U; \
82                                                 (x).imsi = NULL; \
83                                                 (x).duid = NULL; \
84                                                 (x).msisdn = NULL; \
85                                                 (x).access_token = NULL; \
86                                                 (x).refresh_token = NULL; \
87                                                 (x).property = NULL; \
88                                         } while (0)
89
90
91 /******************************************************************************
92  * Private interface definition
93  ******************************************************************************/
94
95 /******************************************************************************
96  * Public interface definition
97  ******************************************************************************/
98
99 service_adaptor_h service_adaptor_get_handle()
100 {
101         service_adaptor_debug("Get adaptor handle");
102
103         return g_service_adaptor;
104 }
105
106 void debug_service_context(GList *service_list)
107 {
108 #ifdef SERVICE_ADAPTOR_DEBUG_CONTEXT
109         if (NULL == service_list) {
110                 return;
111         }
112
113         int service_count = g_list_length(service_list);
114
115         for (int i = 0; i < service_count; i++) {
116                 service_adaptor_service_context_h service = g_list_nth_data(service_list, i);
117
118                 service_adaptor_debug_func("[CONTEXT_DEBUG]  ============ index %d =============", i);
119                 if (service) {
120                         service_adaptor_debug_func("[CONTEXT_DEBUG] service_name(%s)", service->service_name);
121                         service_adaptor_debug_func("[CONTEXT_DEBUG] plugin_uri (%s)", service->plugin_uri);
122                         service_adaptor_debug_func("[CONTEXT_DEBUG] ctx [auth(%p) contact(%p) storage(%p) message(%p) push(%p) shop(%p)]",
123                                         service->auth_context, service->contact_context, service->storage_context,
124                                         service->message_context, service->push_context, service->shop_context);
125                         service_adaptor_debug_func("[CONTEXT_DEBUG] server_info (%p)", service->server_info);
126                         service_adaptor_debug_func("[CONTEXT_DEBUG] connected (%d)", (int)service->connected);
127                         service_adaptor_debug_func("[CONTEXT_DEBUG]==========================================");
128                 }
129         }
130 #endif
131 }
132
133 static void __glog_handler_cb(const gchar *log_domain,
134                         GLogLevelFlags log_level,
135                         const gchar *message,
136                         gpointer user_data)
137 {
138     service_adaptor_error("============================================================");
139     service_adaptor_error("============================================================");
140     service_adaptor_error("================== Critical GLib Error =====================");
141     service_adaptor_error("============================================================");
142     service_adaptor_error("============================================================");
143     service_adaptor_error("=== Log Domain : %s", log_domain);
144     service_adaptor_error("=== Level : %d", (int)log_level);
145     service_adaptor_error("=== Message : %s", message);
146     service_adaptor_error("============================================================");
147     service_adaptor_error("============================================================");
148 }
149
150 static void glog_handler_init()
151 {
152         service_adaptor_info("glib log handler init : %d",
153                         (int)g_log_set_handler("GLib", G_LOG_LEVEL_CRITICAL, __glog_handler_cb, NULL));
154 }
155
156 service_adaptor_service_context_h service_adaptor_get_service_context(service_adaptor_h service_adaptor,
157                                                 const char *service_name)
158 {
159 FUNC_START();
160         service_adaptor_debug("Get service context");
161
162         if ((NULL == service_adaptor) || (NULL == service_name)) {
163                 service_adaptor_error("Invalid argument");
164                 return NULL;
165         }
166
167         int service_count = g_list_length(service_adaptor->service_list);
168         /* debug_service_context(service_adaptor->service_list); */
169
170         for (int i = 0; i < service_count; i++) {
171                 service_adaptor_service_context_h service = g_list_nth_data(service_adaptor->service_list, i);
172
173                 if ((NULL != service) && (0 == strncmp(service->service_name, service_name, strlen(service_name)))) {
174                         return service;
175                 }
176         }
177
178         service_adaptor_warning("First time connected : service_name(%s)", service_name);
179
180 FUNC_END();
181         return NULL;
182 }
183
184 GList *service_adaptor_get_services_by_plugin_uri(service_adaptor_h service_adaptor,
185                                                 const char *plugin_uri)
186 {
187 FUNC_START();
188         service_adaptor_debug("Get service context");
189
190         if (NULL == service_adaptor) {
191                 service_adaptor_error("Invalid argument");
192                 return NULL;
193         }
194
195         GList *service_list = NULL;
196         int service_count = g_list_length(service_adaptor->service_list);
197         service_adaptor_debug("service count : %d", service_count);
198
199         for (int i = 0; i < service_count; i++) {
200                 service_adaptor_service_context_h service = g_list_nth_data(service_adaptor->service_list, i);
201                 if (NULL != service) {
202                         service_adaptor_debug("service name : %s", service->service_name);
203                 }
204
205                 if ((NULL != service) && (0 == strncmp(service->plugin_uri, plugin_uri, strlen(plugin_uri)))) {
206                         service_list = g_list_append(service_list, service);
207                 }
208         }
209
210         if (NULL == service_list) {
211                 service_adaptor_info("Could not get service context with plugin_uri(%s)", plugin_uri);
212         }
213
214 FUNC_END();
215         return service_list;
216 }
217
218 service_adaptor_internal_error_code_e service_adaptor_bind_storage_context(service_adaptor_h service_adaptor,
219                                                 service_adaptor_service_context_h service_src,
220                                                 service_adaptor_service_context_h service_dst)
221 {
222 FUNC_START();
223         service_adaptor_debug("START");
224
225         if ((NULL == service_adaptor) || (NULL == service_src) || (NULL == service_dst)) {
226                 service_adaptor_error("Invalid parameter");
227                 return SERVICE_ADAPTOR_INTERNAL_ERROR_INVALID_ARGUMENT;
228         }
229
230         storage_adaptor_plugin_h plugin = storage_adaptor_get_plugin_by_name(service_adaptor->storage_handle, service_dst->plugin_uri);
231         if (NULL != service_dst->storage_context) {
232                 storage_adaptor_destroy_plugin_context(plugin, service_dst->storage_context);
233         }
234
235         service_dst->storage_context = service_src->storage_context;
236
237         service_adaptor_debug("END");
238
239 FUNC_END();
240         return SERVICE_ADAPTOR_INTERNAL_ERROR_NONE;
241 }
242
243 service_adaptor_internal_error_code_e service_adaptor_bind_push_context(service_adaptor_h service_adaptor,
244                                                 service_adaptor_service_context_h service_src,
245                                                 service_adaptor_service_context_h service_dst)
246 {
247 FUNC_START();
248         service_adaptor_debug("START");
249
250         if ((NULL == service_adaptor) || (NULL == service_src) || (NULL == service_dst)) {
251                 service_adaptor_error("Invalid parameter");
252                 return SERVICE_ADAPTOR_INTERNAL_ERROR_INVALID_ARGUMENT;
253         }
254
255         /*push_adaptor_plugin_h plugin = push_adaptor_get_plugin_by_name(service_adaptor->push_handle, service_dst->plugin_uri);*/
256         if (NULL != service_dst->push_context) {
257                 service_dst->push_context = NULL;
258         }
259
260         service_dst->push_context = service_src->push_context;
261
262         service_adaptor_debug("END");
263
264 FUNC_END();
265         return SERVICE_ADAPTOR_INTERNAL_ERROR_NONE;
266 }
267
268 service_adaptor_internal_error_code_e service_adaptor_bind_context(service_adaptor_h service_adaptor,
269                                                 service_adaptor_service_context_h service_src,
270                                                 service_adaptor_service_context_h service_dst)
271 {
272 FUNC_START();
273         service_adaptor_debug("START");
274
275         if ((NULL == service_adaptor) || (NULL == service_src) || (NULL == service_dst)) {
276                 service_adaptor_error("Invalid parameter");
277                 return SERVICE_ADAPTOR_INTERNAL_ERROR_INVALID_ARGUMENT;
278         }
279
280         if (NULL == service_dst->auth_context) {
281                 service_dst->auth_context = service_src->auth_context;
282         }
283
284         if (NULL == service_dst->contact_context) {
285                 service_dst->contact_context = service_src->contact_context;
286         }
287
288         if (NULL == service_dst->message_context) {
289                 service_dst->message_context = service_src->message_context;
290         }
291
292         if (NULL == service_dst->shop_context) {
293                 service_dst->shop_context = service_src->shop_context;
294         }
295
296         if (NULL == service_dst->storage_context) {
297                 service_dst->storage_context = service_src->storage_context;
298         }
299
300         if (NULL == service_dst->push_context) {
301                 service_dst->push_context = service_src->push_context;
302         }
303
304         service_adaptor_debug("END");
305
306 FUNC_END();
307         return SERVICE_ADAPTOR_INTERNAL_ERROR_NONE;
308 }
309
310 /**
311  * @brief Check service_context is binded
312  * @param[in] service_adaptor   specifies Service-adaptor handle
313  * @param[in] app_id            specifies app_id
314  * @param[in] service_id        specifies service_id
315  * @return count of context, otherwise a negative error value
316  **/
317 int service_adaptor_is_service_binded(service_adaptor_h service_adaptor,
318                                                 const char *service_package_id)
319 {
320 FUNC_START();
321         if ((NULL == service_adaptor) || (NULL == service_package_id)) {
322                 return 0;
323         }
324
325         if ((NULL == service_adaptor->service_list) || (0 >= g_list_length(service_adaptor->service_list))) {
326                 return 0;
327         }
328
329         GList *list = NULL;
330         char *temp_service_name;
331         for (list = g_list_first(service_adaptor->service_list); NULL != list; list = g_list_next(list)) {
332                 if (NULL != list->data) {
333                         temp_service_name = ((service_adaptor_service_context_h) list->data)->service_name;
334                         if ((NULL != temp_service_name)) {
335                                 if (0 == strcmp(temp_service_name, service_package_id)) {
336                                         return 1;
337                                 }
338                         }
339                 }
340         }
341 FUNC_END();
342         return 0;
343 }
344
345 service_adaptor_internal_error_code_e service_adaptor_connect(service_adaptor_h service_adaptor,
346                                                 service_adaptor_context_info_s *context_info,
347                                                 const char *service_name,
348                                                 const char *plugin_uri,
349                                                 const char *user_password,
350                                                 const char *app_secret,
351                                                 bool auth_enable,
352                                                 bool storage_enable,
353                                                 bool contact_enable,
354                                                 bool message_enable,
355                                                 bool push_enable,
356                                                 bool shop_enable,
357                                                 char *ret_msg)
358 {
359 FUNC_START();
360         service_adaptor_debug("Connect to adaptors: %s", plugin_uri);
361
362         if ((NULL == service_adaptor) || (NULL == context_info) || (NULL == service_name)
363                         || (NULL == plugin_uri)) {
364                 service_adaptor_error("Invalid parameter");
365                 return SERVICE_ADAPTOR_INTERNAL_ERROR_INVALID_ARGUMENT;
366         }
367
368         service_adaptor_service_context_h service =
369                         service_adaptor_get_service_context(service_adaptor, service_name);
370
371         if (NULL != service) {
372                 service_adaptor_info("Already connected to adaptors: %s", service_name);
373                 return SERVICE_ADAPTOR_INTERNAL_ERROR_NONE;
374         } else {
375                 service_adaptor_debug("Create service_context_h");
376                 service = (service_adaptor_service_context_h) calloc(1, sizeof(service_adaptor_service_context_s));
377                 service_adaptor_context_info_s *_context_info =
378                         (service_adaptor_context_info_s *) calloc(1, sizeof(service_adaptor_context_info_s));
379
380                 if ((NULL == service) || (NULL == _context_info)) {
381                         service_adaptor_error("Critical : Memory allocation failed");
382                         free(service);
383                         free(_context_info);
384                         snprintf(ret_msg, 2048, "Critical : There is no memory remained");
385                         return SERVICE_ADAPTOR_INTERNAL_ERROR_ADAPTOR_INTERNAL;
386                 }
387
388                 g_mutex_init(&service->service_context_mutex);
389                 g_cond_init(&service->service_context_cond);
390
391                 service->auth_context = NULL;
392                 service->contact_context = NULL;
393                 service->message_context = NULL;
394                 service->shop_context = NULL;
395                 service->push_context = NULL;
396                 service->storage_context = NULL;
397
398                 service->service_name = safe_strdup(service_name);
399                 service->plugin_uri = safe_strdup(plugin_uri);
400
401                 service->context_info = _context_info;
402
403                 service->context_info->user_id = safe_strdup(context_info->user_id);
404                 service->context_info->app_id = safe_strdup(context_info->app_id);
405                 service->context_info->imsi = safe_strdup(context_info->imsi);
406                 service->context_info->service_id = context_info->service_id;
407                 service->context_info->property = context_info->property; /* Not dup */
408                 context_info->property = NULL;
409
410                 service->connected = 0x0000000;
411         }
412
413 FUNC_STEP();
414         /* 1) Connect to AUTH PLUGIN */
415         int ret = SERVICE_ADAPTOR_INTERNAL_ERROR_NONE;
416         int last_res = SERVICE_ADAPTOR_INTERNAL_ERROR_NONE;
417
418         if (auth_enable) {
419                 FUNC_STEP();
420                 ret += (last_res = service_adaptor_connect_auth_plugin(service_adaptor, service->context_info, service_name,
421                                 plugin_uri, user_password, app_secret, &service, ret_msg));
422
423                 if ((SERVICE_ADAPTOR_INTERNAL_ERROR_NONE != ret) || (NULL == service)) {
424                         service_adaptor_error("Could not connect to auth plugin: %d", ret);
425                         goto CONNECT_API_CHECK_RESULT;
426                 }
427         }
428
429         /* 2) Connect to CONTACT PLUGIN */
430         if (contact_enable) {
431                 FUNC_STEP();
432                 ret += (last_res = service_adaptor_connect_contact_plugin(service_adaptor, service, ret_msg));
433
434                 if (SERVICE_ADAPTOR_INTERNAL_ERROR_NONE != ret) {
435                         service_adaptor_warning("Could not connect to contact plugin: %d", ret);
436                         goto CONNECT_API_CHECK_RESULT;
437                 }
438         }
439
440         /* 3) Connect to MESSAGE PLUGIN */
441         if (message_enable) {
442                 FUNC_STEP();
443                 ret += (last_res = service_adaptor_connect_message_plugin(service_adaptor, service, ret_msg));
444
445                 if (SERVICE_ADAPTOR_INTERNAL_ERROR_NONE != ret) {
446                         service_adaptor_warning("Could not connect to message plugin: %d", ret);
447                         goto CONNECT_API_CHECK_RESULT;
448                 }
449         }
450
451         /* 4) Connect to SHOP PLUGIN */
452         if (shop_enable) {
453                 FUNC_STEP();
454                 ret += (last_res = service_adaptor_connect_shop_plugin(service_adaptor, service, ret_msg));
455
456                 if (SERVICE_ADAPTOR_INTERNAL_ERROR_NONE != ret) {
457                         service_adaptor_warning("Could not connect to shop plugin: %d", ret);
458                         goto CONNECT_API_CHECK_RESULT;
459                 }
460         }
461
462         /* 5) Connect to STORAGE PLUGIN */
463         if (storage_enable) {
464                 FUNC_STEP();
465                 ret += (last_res = service_adaptor_connect_storage_plugin(service_adaptor, service, app_secret, ret_msg));
466
467                 if (SERVICE_ADAPTOR_INTERNAL_ERROR_NONE != ret) {
468                         service_adaptor_warning("Could not connect to storage plugin: %d", ret);
469                         goto CONNECT_API_CHECK_RESULT;
470                 }
471         }
472
473         /* 6) Connect to PUSH PLUGIN */
474         if (push_enable) {
475                 FUNC_STEP();
476                 ret += (last_res = service_adaptor_connect_push_plugin(service_adaptor, service, ret_msg));
477
478                 if (SERVICE_ADAPTOR_INTERNAL_ERROR_NONE != ret) {
479                         service_adaptor_warning("Could not connect to push plugin: %d", ret);
480                         goto CONNECT_API_CHECK_RESULT;
481                 }
482         }
483
484 CONNECT_API_CHECK_RESULT:
485         if (ret || (0 == service->connected)) {
486                 service_adaptor_warning("Plugin Connect failed : ret(%d) connected(%d)", ret, service->connected);
487                 service_adaptor_warning("<service_name : %s>", service_name);
488
489                 g_mutex_clear(&service->service_context_mutex);
490                 g_cond_clear(&service->service_context_cond);
491
492                 free(service->context_info->user_id);
493                 free(service->context_info->app_id);
494                 free(service->context_info->imsi);
495
496                 free(service->context_info);
497
498                 free(service->service_name);
499                 free(service->plugin_uri);
500
501                 free(service);
502
503                 return last_res;
504         }
505
506
507         service_adaptor->service_list = g_list_append(service_adaptor->service_list, service);
508
509         service_adaptor_debug_func("Connect success <service_name : %s>", service_name);
510         debug_service_context(service_adaptor->service_list);
511 FUNC_END();
512         return SERVICE_ADAPTOR_INTERNAL_ERROR_NONE;
513 }
514
515 service_adaptor_internal_error_code_e service_adaptor_disconnect(service_adaptor_h service_adaptor,
516                                                 const char *service_name)
517 {
518 FUNC_START();
519         service_adaptor_debug("Disconnects adaptor contexts");
520
521         service_adaptor_service_context_h service = NULL;
522         while (NULL != (service = service_adaptor_get_service_context(service_adaptor, service_name))) {
523
524                 if (NULL == service) {
525                         service_adaptor_debug("service context already released");
526                         return SERVICE_ADAPTOR_INTERNAL_ERROR_NONE;
527                 }
528
529                 service_adaptor_internal_error_code_e ret = SERVICE_ADAPTOR_INTERNAL_ERROR_NONE;
530                 ret = service_adaptor_disconnect_message_plugin(service_adaptor, service);
531                 service_adaptor_debug("Disconnected message (%d)", ret);
532
533                 ret = service_adaptor_disconnect_contact_plugin(service_adaptor, service);
534                 service_adaptor_debug("Disconnected contact (%d)", ret);
535
536                 ret = service_adaptor_disconnect_shop_plugin(service_adaptor, service);
537                 service_adaptor_debug("Disconnected shop (%d)", ret);
538
539                 ret = service_adaptor_disconnect_storage_plugin(service_adaptor, service);
540                 service_adaptor_debug("Disconnected storage (%d)", ret);
541
542                 ret = service_adaptor_disconnect_push_plugin(service_adaptor, service);
543                 service_adaptor_debug("Disconnected push (%d)", ret);
544
545                 ret = service_adaptor_disconnect_auth_plugin(service_adaptor, service);
546                 service_adaptor_debug("Disconnected auth (%d)", ret);
547
548                 if ((NULL != service) && (NULL != service->server_info)) {
549                         g_hash_table_destroy(service->server_info);
550                 }
551
552                 service_adaptor_debug_func("Clears mutex & cond");
553                 g_mutex_clear(&service->service_context_mutex);
554                 g_cond_clear(&service->service_context_cond);
555
556                 free(service->service_name);
557                 free(service->plugin_uri);
558
559                 service_adaptor_debug_func("Clears context info");
560                 if (NULL != (service->context_info)) {
561                         free(service->context_info->user_id);
562                         free(service->context_info->app_id);
563                         free(service->context_info->imsi);
564                         free(service->context_info->duid);
565                         free(service->context_info->msisdn);
566                         free(service->context_info->access_token);
567                         free(service->context_info->refresh_token);
568                         if (service->context_info->property) {
569                                 bundle_free((bundle *)(service->context_info->property));
570                         }
571                 }
572                 free(service->context_info);
573
574                 service_adaptor_debug_func("Removes from service list");
575                 service_adaptor->service_list = g_list_remove(service_adaptor->service_list, service);
576                 free(service);
577                 service = NULL;
578         }
579         service_adaptor_debug("Disconnected from adaptors");
580
581 FUNC_END();
582         return SERVICE_ADAPTOR_INTERNAL_ERROR_NONE;
583 }
584
585
586 service_adaptor_internal_error_code_e service_adaptor_start(service_adaptor_h service_adaptor)
587 {
588 FUNC_START();
589         service_adaptor_debug("Service Adaptor: Start");
590
591         if (NULL == service_adaptor) {
592                 service_adaptor_error("Invalid argument");
593                 return SERVICE_ADAPTOR_INTERNAL_ERROR_INVALID_ARGUMENT;
594         }
595
596         int ret = auth_adaptor_start(service_adaptor->auth_handle);
597
598         if (AUTH_ADAPTOR_ERROR_NONE != ret) {
599                 service_adaptor_error("Could not start auth adaptor");
600                 service_adaptor_info("Auth-adaptor is mandatory");
601 /*              return SERVICE_ADAPTOR_INTERNAL_ERROR_LAUNCH; */
602         }
603
604         service_adaptor_debug("Auth Adaptor: Started");
605
606         ret = contact_adaptor_start(service_adaptor->contact_handle);
607
608         if (CONTACT_ADAPTOR_ERROR_NONE != ret) {
609                 service_adaptor_warning("Could not start contact adaptor");
610 /*              return SERVICE_ADAPTOR_INTERNAL_ERROR_LAUNCH; */
611         }
612
613         service_adaptor_debug("Contact Adaptor: Started");
614
615         ret = message_adaptor_start(service_adaptor->message_handle);
616
617         if (MESSAGE_ADAPTOR_ERROR_NONE != ret) {
618                 service_adaptor_warning("Could not start message adaptor");
619 /*              return SERVICE_ADAPTOR_INTERNAL_ERROR_LAUNCH; */
620         }
621
622         service_adaptor_debug("Message Adaptor: Started");
623
624         ret = shop_adaptor_start(service_adaptor->shop_handle);
625
626         if (SHOP_ADAPTOR_ERROR_NONE != ret) {
627                 service_adaptor_warning("Could not start shop adaptor");
628 /*              return SERVICE_ADAPTOR_INTERNAL_ERROR_LAUNCH; */
629         }
630
631         service_adaptor_debug("Shop Adaptor: Started");
632
633         ret = storage_adaptor_start(service_adaptor->storage_handle);
634
635         if (STORAGE_ADAPTOR_ERROR_NONE != ret) {
636                 service_adaptor_warning("Could not start storage adaptor");
637 /*              return SERVICE_ADAPTOR_INTERNAL_ERROR_LAUNCH; */
638         }
639
640         service_adaptor_debug("Storage Adaptor: Started");
641
642         ret = push_adaptor_start(service_adaptor->push_handle);
643
644         if (PUSH_ADAPTOR_ERROR_NONE != ret) {
645                 service_adaptor_warning("Could not start push adaptor");
646 /*              return SERVICE_ADAPTOR_INTERNAL_ERROR_LAUNCH; */
647         }
648
649         service_adaptor_debug("Push Adaptor: Started");
650 /*
651         ret = service_adaptor_scan_all_packages_async(service_adaptor);
652         service_adaptor_debug("Scan all packages ret(%d)", ret);
653
654         ret = service_adaptor_set_package_installed_callback(service_adaptor);
655         service_adaptor_debug("Sets package installed callback ret(%d)", ret);
656 */
657         g_mutex_lock(&service_adaptor->service_adaptor_mutex);
658         service_adaptor->started = 1;
659         g_cond_signal(&service_adaptor->service_adaptor_cond);
660         g_mutex_unlock(&service_adaptor->service_adaptor_mutex);
661
662 FUNC_END();
663         return SERVICE_ADAPTOR_INTERNAL_ERROR_NONE;
664 }
665
666 service_adaptor_internal_error_code_e service_adaptor_stop(service_adaptor_h service_adaptor)
667 {
668 FUNC_START();
669         service_adaptor_debug("Service Adaptor: Stop");
670
671         if (NULL == service_adaptor) {
672                 service_adaptor_error("Invalid argument");
673                 return SERVICE_ADAPTOR_INTERNAL_ERROR_INVALID_ARGUMENT;
674         }
675
676         if (0 <= service_adaptor->started) {
677                 service_adaptor_error("Service Adaptor is not running");
678                 return SERVICE_ADAPTOR_INTERNAL_ERROR_START;
679         }
680
681 FUNC_STEP();
682         int ret = auth_adaptor_stop(service_adaptor->auth_handle);
683         ret += contact_adaptor_stop(service_adaptor->contact_handle);
684         ret += message_adaptor_stop(service_adaptor->message_handle);
685         ret += shop_adaptor_stop(service_adaptor->shop_handle);
686         ret += storage_adaptor_stop(service_adaptor->storage_handle);
687         ret += push_adaptor_stop(service_adaptor->push_handle);
688
689         service_adaptor->started = 0;
690
691         if (0 != ret) {
692                 service_adaptor_error("Error while stopping adaptors");
693                 return SERVICE_ADAPTOR_INTERNAL_ERROR_CORRUPTED;
694         }
695
696         service_adaptor_debug("Service Adaptor: Stopped");
697
698 FUNC_END();
699         return SERVICE_ADAPTOR_INTERNAL_ERROR_NONE;
700 }
701
702 /**************************************************************************
703  * Create / Destroy Adaptors
704  **************************************************************************/
705 service_adaptor_h service_adaptor_create()
706 {
707 FUNC_START();
708         service_adaptor_debug("Service Adaptor: Create");
709
710         /* create handle of adaptor */
711         auth_adaptor_h auth_handle = service_adaptor_create_auth();
712
713         if (NULL == auth_handle) {
714                 service_adaptor_error("Could not create auth adaptor");
715                 return NULL;
716         }
717
718         contact_adaptor_h contact_handle = service_adaptor_create_contact();
719
720         if (NULL == contact_handle) {
721                 service_adaptor_error("Could not create contact adaptor");
722                 auth_adaptor_destroy(auth_handle);
723                 return NULL;
724         }
725
726         message_adaptor_h message_handle = service_adaptor_create_message();
727
728         if (NULL == message_handle) {
729                 service_adaptor_error("Could not create message adaptor");
730                 auth_adaptor_destroy(auth_handle);
731                 contact_adaptor_destroy(contact_handle);
732                 return NULL;
733         }
734
735         shop_adaptor_h shop_handle = service_adaptor_create_shop();
736
737         if (NULL == shop_handle) {
738                 service_adaptor_error("Could not create shop adaptor");
739                 auth_adaptor_destroy(auth_handle);
740                 contact_adaptor_destroy(contact_handle);
741                 message_adaptor_destroy(message_handle);
742                 return NULL;
743         }
744
745         storage_adaptor_h storage_handle = service_adaptor_create_storage();
746
747         if (NULL == storage_handle) {
748                 service_adaptor_error("Could not create storage adaptor");
749                 auth_adaptor_destroy(auth_handle);
750                 contact_adaptor_destroy(contact_handle);
751                 message_adaptor_destroy(message_handle);
752                 shop_adaptor_destroy(shop_handle);
753                 return NULL;
754         }
755
756         push_adaptor_h push_handle = service_adaptor_create_push();
757
758         if (NULL == push_handle) {
759                 service_adaptor_error("Could not create push adaptor");
760                 auth_adaptor_destroy(auth_handle);
761                 contact_adaptor_destroy(contact_handle);
762                 message_adaptor_destroy(message_handle);
763                 shop_adaptor_destroy(shop_handle);
764                 storage_adaptor_destroy(storage_handle);
765                 return NULL;
766         }
767
768         /* register listener of adaptor */
769         auth_adaptor_listener_h auth_listener = service_adaptor_register_auth_listener(auth_handle);
770
771         if (NULL == auth_listener) {
772                 service_adaptor_error("Could not create auth listener");
773                 auth_adaptor_destroy(auth_handle);
774                 contact_adaptor_destroy(contact_handle);
775                 message_adaptor_destroy(message_handle);
776                 shop_adaptor_destroy(shop_handle);
777                 storage_adaptor_destroy(storage_handle);
778                 push_adaptor_destroy(push_handle);
779                 return NULL;
780         }
781
782         contact_adaptor_listener_h contact_listener = service_adaptor_register_contact_listener(contact_handle);
783
784         if (NULL == contact_listener) {
785                 service_adaptor_error("Could not create contact listener");
786                 auth_adaptor_unregister_listener(auth_handle, auth_listener);
787
788                 auth_adaptor_destroy(auth_handle);
789                 contact_adaptor_destroy(contact_handle);
790                 message_adaptor_destroy(message_handle);
791                 shop_adaptor_destroy(shop_handle);
792                 storage_adaptor_destroy(storage_handle);
793                 push_adaptor_destroy(push_handle);
794                 return NULL;
795         }
796
797         message_adaptor_listener_h message_listener = service_adaptor_register_message_listener(message_handle);
798
799         if (NULL == message_listener) {
800                 service_adaptor_error("Could not create message listener");
801                 auth_adaptor_unregister_listener(auth_handle, auth_listener);
802                 contact_adaptor_unregister_listener(contact_handle, contact_listener);
803
804                 auth_adaptor_destroy(auth_handle);
805                 contact_adaptor_destroy(contact_handle);
806                 message_adaptor_destroy(message_handle);
807                 shop_adaptor_destroy(shop_handle);
808                 storage_adaptor_destroy(storage_handle);
809                 push_adaptor_destroy(push_handle);
810                 return NULL;
811         }
812
813         shop_adaptor_listener_h shop_listener = service_adaptor_register_shop_listener(shop_handle);
814
815         if (NULL == shop_listener) {
816                 service_adaptor_error("Could not create shop listener");
817                 auth_adaptor_unregister_listener(auth_handle, auth_listener);
818                 contact_adaptor_unregister_listener(contact_handle, contact_listener);
819                 message_adaptor_unregister_listener(message_handle, message_listener);
820
821                 auth_adaptor_destroy(auth_handle);
822                 contact_adaptor_destroy(contact_handle);
823                 message_adaptor_destroy(message_handle);
824                 shop_adaptor_destroy(shop_handle);
825                 storage_adaptor_destroy(storage_handle);
826                 push_adaptor_destroy(push_handle);
827                 return NULL;
828         }
829
830         storage_adaptor_listener_h storage_listener = service_adaptor_register_storage_listener(storage_handle);
831
832         if (NULL == storage_listener) {
833                 service_adaptor_error("Could not create storage listener");
834                 auth_adaptor_unregister_listener(auth_handle, auth_listener);
835                 contact_adaptor_unregister_listener(contact_handle, contact_listener);
836                 message_adaptor_unregister_listener(message_handle, message_listener);
837                 shop_adaptor_unregister_listener(shop_handle, shop_listener);
838
839                 auth_adaptor_destroy(auth_handle);
840                 contact_adaptor_destroy(contact_handle);
841                 message_adaptor_destroy(message_handle);
842                 shop_adaptor_destroy(shop_handle);
843                 storage_adaptor_destroy(storage_handle);
844                 push_adaptor_destroy(push_handle);
845                 return NULL;
846         }
847
848         push_adaptor_listener_h push_listener = service_adaptor_register_push_listener(push_handle);
849
850         if (NULL == push_listener) {
851                 service_adaptor_error("Could not create push listener");
852                 auth_adaptor_unregister_listener(auth_handle, auth_listener);
853                 contact_adaptor_unregister_listener(contact_handle, contact_listener);
854                 message_adaptor_unregister_listener(message_handle, message_listener);
855                 shop_adaptor_unregister_listener(shop_handle, shop_listener);
856                 storage_adaptor_unregister_listener(storage_handle, storage_listener);
857
858                 auth_adaptor_destroy(auth_handle);
859                 contact_adaptor_destroy(contact_handle);
860                 message_adaptor_destroy(message_handle);
861                 shop_adaptor_destroy(shop_handle);
862                 storage_adaptor_destroy(storage_handle);
863                 push_adaptor_destroy(push_handle);
864                 return NULL;
865         }
866
867         /* create Service Adaptor */
868         service_adaptor_h service_adaptor = (service_adaptor_h) g_malloc0(sizeof(service_adaptor_s));
869
870         if (NULL == service_adaptor) {
871                 service_adaptor_error("Could not create service adaptor");
872                 auth_adaptor_unregister_listener(auth_handle, auth_listener);
873                 contact_adaptor_unregister_listener(contact_handle, contact_listener);
874                 message_adaptor_unregister_listener(message_handle, message_listener);
875                 shop_adaptor_unregister_listener(shop_handle, shop_listener);
876                 storage_adaptor_unregister_listener(storage_handle, storage_listener);
877                 push_adaptor_unregister_listener(push_handle, push_listener);
878
879                 auth_adaptor_destroy(auth_handle);
880                 contact_adaptor_destroy(contact_handle);
881                 message_adaptor_destroy(message_handle);
882                 shop_adaptor_destroy(shop_handle);
883                 storage_adaptor_destroy(storage_handle);
884                 push_adaptor_destroy(push_handle);
885                 return NULL;
886         }
887
888 FUNC_STEP();
889         service_adaptor->auth_handle            = auth_handle;
890         service_adaptor->contact_handle         = contact_handle;
891         service_adaptor->message_handle         = message_handle;
892         service_adaptor->shop_handle            = shop_handle;
893         service_adaptor->storage_handle         = storage_handle;
894         service_adaptor->push_handle            = push_handle;
895
896         service_adaptor->auth_listener          = auth_listener;
897         service_adaptor->push_listener          = push_listener;
898         service_adaptor->shop_listener          = shop_listener;
899         service_adaptor->contact_listener       = contact_listener;
900         service_adaptor->storage_listener       = storage_listener;
901         service_adaptor->message_listener       = message_listener;
902
903         service_adaptor->service_list = NULL;
904         service_adaptor->started = 0;
905
906         g_mutex_init(&service_adaptor->service_adaptor_mutex);
907         g_cond_init(&service_adaptor->service_adaptor_cond);
908
909         service_adaptor_debug("Service Adaptor: Created");
910
911         service_adaptor_info("Init client checker (%d)", client_checker_init());
912
913 FUNC_END();
914         return service_adaptor;
915 }
916
917 void service_adaptor_destroy(service_adaptor_h service_adaptor)
918 {
919 FUNC_START();
920         service_adaptor_debug("Service Adaptor: Destroy");
921         client_checker_deinit();
922         service_adaptor_info("Deinit client checker");
923
924         if ((void *) NULL == service_adaptor) {
925                 service_adaptor_debug("Invalid argument");
926                 return;
927         }
928
929         /* 1) deinit D-Bus */
930         dbus_ipc_server_layer_deinit();
931
932         service_adaptor_debug("Service adaptor destroyed: D-Bus");
933
934         /* 2) destroy service list */
935         if (NULL != service_adaptor->service_list) {
936                 g_list_free(service_adaptor->service_list);
937                 service_adaptor->service_list = NULL;
938         }
939
940         service_adaptor_debug("Service adaptor destroyed: service list");
941
942         /* 3) stop service adaptor */
943         if (0 < service_adaptor->started) {
944                 service_adaptor_error("Service Adaptor is running. Force stopping before destroy");
945                 service_adaptor_stop(service_adaptor);
946         }
947
948         service_adaptor_safe_free(service_adaptor);
949
950         service_adaptor_debug("Service Adaptor: Destroyed");
951 FUNC_END();
952 }
953
954 void *servive_adaptor_preload_service_files(void *data)
955 {
956 FUNC_START();
957         service_adaptor_debug("5 sec sleep for load service files (TODO change)");
958         sleep(5);
959         service_adaptor_internal_error_code_e ret;
960         push_activate_h *services = NULL;
961         int svc_len = 0;
962         ret = service_adaptor_ref_enabled_push_services(&services, &svc_len);
963
964         service_adaptor_debug_func("### Preload service len : %d", svc_len);
965         if (SERVICE_ADAPTOR_INTERNAL_ERROR_NONE == ret) {
966                 for (int i = 0; i < svc_len; i++) {
967                         service_adaptor_context_info_s new_context_info;
968                         __init_context_info_s(new_context_info);
969                         new_context_info.app_id = services[i]->app_id;
970                         char service_name[1024] = {0, };
971                         snprintf(service_name, 1024, "preloaded_service/plugin='%s'&app_id='%s'",
972                                         services[i]->plugin_uri, services[i]->app_id);
973                         char ret_msg[2048] = {0, };
974                         ret = service_adaptor_connect(g_service_adaptor, &new_context_info, service_name,
975                                         services[i]->plugin_uri, "", "",
976                                         false, false, false, false, true, false, ret_msg);
977                         service_adaptor_debug_func("### Preload service : ret(%d) service_name(%s)", ret, service_name);
978                 }
979                 free(services);
980         }
981
982 FUNC_END();
983         return NULL;
984 }
985
986 /**
987  * @brief init service adaptor
988  *
989  * @return      void.
990  */
991 service_adaptor_internal_error_code_e service_adaptor_init()
992 {
993 FUNC_START();
994         service_adaptor_debug("Service Adaptor: Initialize");
995
996         int ret = SERVICE_ADAPTOR_INTERNAL_ERROR_NONE;
997
998         glog_handler_init();
999
1000         /* 1) create adaptor (memory allocation) */
1001         service_adaptor_h service_adaptor = service_adaptor_create();
1002
1003         if (NULL == service_adaptor) {
1004                 service_adaptor_error("Could not create Service Adaptor");
1005                 return SERVICE_ADAPTOR_INTERNAL_ERROR_CREATE;
1006         }
1007
1008         service_adaptor_debug("Service Adaptor: created");
1009
1010         /* 2) start adaptor (plugin load, get IMIS list) */
1011         ret = service_adaptor_start(service_adaptor);
1012
1013         if (SERVICE_ADAPTOR_INTERNAL_ERROR_NONE != ret) {
1014                 service_adaptor_error("Could not load Adaptors: %d", ret);
1015                 return SERVICE_ADAPTOR_INTERNAL_ERROR_START;
1016         }
1017
1018         service_adaptor_debug("Service Adaptor: started");
1019 /*      TODO it will be activated for on-demand */
1020         ping_manager_init(5, g_default_loop);
1021
1022         /* 3) assign to global service adaptor handle */
1023         g_service_adaptor = service_adaptor;
1024
1025         /* 4) init dbus */
1026         ret = dbus_ipc_server_layer_init();
1027
1028         if (SERVICE_ADAPTOR_INTERNAL_ERROR_NONE != ret) {
1029                 service_adaptor_error("Could not init D-Bus IPC server: %d", ret);
1030                 return SERVICE_ADAPTOR_INTERNAL_ERROR_DBUS;
1031         }
1032
1033         service_adaptor_debug("Service Adaptor: init D-Bus");
1034         service_adaptor_debug("Service Adaptor: Initialized (%d)", service_adaptor->started);
1035
1036         service_adaptor_debug("Service Adaptor: init preload service");
1037         pthread_t job;
1038         pthread_create(&job, NULL, servive_adaptor_preload_service_files, NULL);
1039 FUNC_END();
1040         return SERVICE_ADAPTOR_INTERNAL_ERROR_NONE;
1041 }
1042
1043 /**
1044  * @brief deinit service adaptor
1045  *
1046  * @param[in]   service_adaptor         specifies handle of service adaptor
1047  * @return      void.
1048  */
1049 void service_adaptor_deinit()
1050 {
1051         service_adaptor_debug("Service Adaptor: Deinitialize");
1052
1053         if (NULL == g_service_adaptor) {
1054                 service_adaptor_error("Invalid argument");
1055                 return;
1056         }
1057
1058         service_adaptor_h service_adaptor = g_service_adaptor;
1059         g_service_adaptor = NULL;
1060         service_adaptor_destroy(service_adaptor);
1061         service_adaptor_debug("Service Adaptor: Deinitialized");
1062 }
1063
1064 /**
1065  * @brief main signal function
1066  *
1067  * @param[in]   data            specifies user data passed by main function
1068  * @return      void.
1069  */
1070 static gint sigterm_callback(void *data)
1071 {
1072         service_adaptor_info("Service Adaptor Shutdown");
1073
1074         g_main_loop_quit((GMainLoop *)data);
1075
1076         return FALSE;
1077 }
1078
1079 /******************************************************************************
1080   Public interface definition
1081  *****************************************************************************/
1082
1083 service_adaptor_internal_error_code_e service_adaptor_auth_refresh(service_adaptor_h service_adaptor,
1084                                                 const char *service_name,
1085                                                 const char *plugin_uri)
1086 {
1087 FUNC_START();
1088         service_adaptor_debug("Auth refresh Start");
1089
1090         service_adaptor_service_context_h service_context =
1091                         service_adaptor_get_service_context(service_adaptor, service_name);
1092
1093 FUNC_END();
1094         return service_adaptor_auth_refresh_with_service_context(service_adaptor, service_context, plugin_uri);
1095 }
1096
1097 service_adaptor_internal_error_code_e service_adaptor_auth_refresh_with_service_context(service_adaptor_h service_adaptor,
1098                                                 service_adaptor_service_context_h service_context,
1099                                                 const char *plugin_uri)
1100 {
1101 FUNC_START();
1102         service_adaptor_debug("Auth refresh with service context Start");
1103
1104         if (NULL == service_context) {
1105                 service_adaptor_warning("Parameter is NULL (service_adaptor_h service_adaptor)");
1106                 return SERVICE_ADAPTOR_INTERNAL_ERROR_INVALID_HANDLE;
1107         }
1108
1109         if (NULL == service_context->auth_context) {
1110                 service_adaptor_warning("Parameter is NULL (service_context->auth_context)");
1111                 return SERVICE_ADAPTOR_INTERNAL_ERROR_INVALID_HANDLE;
1112         }
1113
1114         int ret = SERVICE_ADAPTOR_INTERNAL_ERROR_NONE;
1115         char *old_access_token = NULL;
1116         char *new_access_token = NULL;
1117         char *new_uid = NULL;
1118
1119         service_adaptor_debug("Get contexts and plugins from service_adaptor_h");
1120         if (NULL != service_context->context_info) {
1121                 old_access_token = service_context->context_info->access_token;
1122         } else {
1123                 service_adaptor_warning("Element is NULL (service_context->context->info)");
1124                 return SERVICE_ADAPTOR_INTERNAL_ERROR_INVALID_HANDLE;
1125         }
1126         auth_adaptor_plugin_h auth_plugin =
1127                         auth_adaptor_get_plugin_by_name(service_adaptor->auth_handle, plugin_uri);
1128         auth_adaptor_error_code_h       auth_error      = NULL;
1129
1130         int is_auth = -1;
1131         service_adaptor_debug("Call is_auth");
1132         ret = auth_adaptor_is_auth(auth_plugin, service_context->auth_context, NULL, &is_auth, &auth_error, NULL);
1133
1134         if (0 == is_auth) {
1135                 service_adaptor_error("Auth was withdrew (Can not use all service)");
1136                 /*TODO change context_info's auth flag */
1137                 return SERVICE_ADAPTOR_INTERNAL_ERROR_NOT_AUTHORIZED;
1138         }
1139         auth_adaptor_destroy_error_code(&auth_error);
1140
1141         service_adaptor_debug("Call auth_adaptor_login");
1142         ret = auth_adaptor_login(auth_plugin, service_context->auth_context, is_auth, NULL, &auth_error, NULL);
1143
1144         if ((NULL != old_access_token) && (AUTH_ADAPTOR_ERROR_NONE == ret)) {
1145                 if (NULL == service_context->auth_context->access_token) {
1146                         service_adaptor_error("auth_plugin returns login success but auth_access token is empty");
1147                         service_context->auth_context->access_token = strdup(old_access_token);
1148                         ret = SERVICE_ADAPTOR_INTERNAL_ERROR_NOT_AUTHORIZED;
1149                 } else {
1150                         if (0 == strcmp(old_access_token, service_context->auth_context->access_token)) {
1151                                 service_adaptor_debug("Call auth_adaptor_login_refresh");
1152                                 ret = auth_adaptor_login_refresh(auth_plugin, service_context->auth_context, NULL, &auth_error, NULL);
1153                         }
1154
1155                         if ((AUTH_ADAPTOR_ERROR_NONE == ret) &&
1156                                         (NULL != service_context->auth_context->access_token) &&
1157                                         (0 == strcmp(old_access_token, service_context->auth_context->access_token))) {
1158                                 service_adaptor_debug("Access token was not changed");
1159                         } else if (AUTH_ADAPTOR_ERROR_NONE == ret) {
1160                                 service_adaptor_debug("Changing access token start");
1161
1162                                 new_access_token = auth_adaptor_get_access_token_dup(service_context->auth_context);
1163                                 if (NULL != new_access_token) {
1164                                         service_adaptor_debug_secure("New access token : %s", new_access_token);
1165                                         free(service_context->context_info->access_token);
1166                                         service_context->context_info->access_token = new_access_token;
1167                                         service_adaptor_debug("service_context->context_info->access_token was changed");
1168
1169                                         ret = contact_adaptor_refresh_access_token(service_context->contact_context, new_access_token);
1170                                         if (!ret) {
1171                                                 service_adaptor_debug("service_context->contact_context was changed");
1172                                         }
1173
1174                                         ret = storage_adaptor_refresh_access_token(service_context->storage_context, new_access_token);
1175                                         if (!ret) {
1176                                                 service_adaptor_debug("service_context->storage_context was changed");
1177                                         }
1178
1179                                         ret = message_adaptor_refresh_access_token(service_context->message_context, new_access_token);
1180                                         if (!ret) {
1181                                                 service_adaptor_debug("service_context->message_context was changed");
1182                                         }
1183
1184                                         ret = shop_adaptor_refresh_access_token(service_context->shop_context, new_access_token);
1185                                         if (!ret) {
1186                                                 service_adaptor_debug("service_context->shop_context was changed");
1187                                         }
1188                                 }
1189
1190                                 new_uid = auth_adaptor_get_uid_dup(service_context->auth_context);
1191                                 if (NULL != new_uid) {
1192                                         service_adaptor_debug_secure("New unique id : %s", new_uid);
1193                                         free(service_context->context_info->duid);
1194                                         service_context->context_info->duid = new_uid;
1195                                         service_adaptor_debug("service_context->context_info->uid was changed");
1196
1197                                         ret = contact_adaptor_refresh_uid(service_context->contact_context, new_uid);
1198                                         if (!ret) {
1199                                                 service_adaptor_debug("service_context->contact_context was changed");
1200                                         }
1201
1202                                         ret = storage_adaptor_refresh_uid(service_context->storage_context, new_uid);
1203                                         if (!ret) {
1204                                                 service_adaptor_debug("service_context->storage_context was changed");
1205                                         }
1206
1207                                         ret = message_adaptor_refresh_uid(service_context->message_context, new_uid);
1208                                         if (!ret) {
1209                                                 service_adaptor_debug("service_context->message_context was changed");
1210                                         }
1211
1212                                         ret = shop_adaptor_refresh_uid(service_context->shop_context, new_uid);
1213                                         if (!ret) {
1214                                                 service_adaptor_debug("service_context->shop_context was changed");
1215                                         }
1216                                 }
1217                         } else {
1218                                 service_adaptor_error("Login refresh failed");
1219                                 if (NULL != auth_error) {
1220                                         service_adaptor_error("Auth error code(%lld) message(%s)", auth_error->code, auth_error->msg);
1221                                 }
1222                         }
1223                 }
1224         } else {
1225                 service_adaptor_error("Login refresh failed");
1226                 if (NULL != auth_error) {
1227                         service_adaptor_error("Auth error code(%lld) message(%s)", auth_error->code, auth_error->msg);
1228                 }
1229         }
1230
1231         service_adaptor_debug("Auth refresh End");
1232 FUNC_END();
1233         return ret;
1234 }
1235
1236
1237 /**
1238  * @brief main function
1239  *
1240  * @param[in]   argc            specifies count of arguments
1241  * @param[in]   argv            specifies value list of arguments
1242  * @return      void.
1243  */
1244 int main(int argc, char *argv[])
1245 {
1246         int ret = SERVICE_ADAPTOR_INTERNAL_ERROR_NONE;
1247         GMainLoop *loop;
1248
1249 #if !GLIB_CHECK_VERSION(2, 32, 0)
1250         g_thread_init(NULL);
1251 #endif
1252 #if !GLIB_CHECK_VERSION(2, 35, 0)
1253         g_type_init();
1254 #endif
1255
1256         /* mainloop of main thread */
1257         loop = g_main_loop_new(NULL, FALSE);
1258         g_default_loop = loop;
1259
1260         ret = service_adaptor_init();
1261
1262         if (SERVICE_ADAPTOR_INTERNAL_ERROR_NONE != ret) {
1263                 service_adaptor_error("Service Adaptor initialize error: %d\n", ret);
1264                 return -1;
1265         }
1266
1267         /* installing signal handlers */
1268         g_unix_signal_add_full(G_PRIORITY_HIGH, SIGINT,
1269                         sigterm_callback, loop, NULL);
1270         g_unix_signal_add_full(G_PRIORITY_HIGH, SIGTERM,
1271                         sigterm_callback, loop, NULL);
1272
1273         /* start application's main loop */
1274         g_main_loop_run(loop);
1275
1276         /* cleanup after mainloop */
1277         g_main_loop_unref(loop);
1278
1279         service_adaptor_h service_adaptor = service_adaptor_get_handle();
1280         service_adaptor_deinit(service_adaptor);
1281
1282         return ret;
1283 }
1284
1285 #ifdef SERVICE_ADAPTOR_DEBUG_TIME_CHECK
1286
1287 #include <sys/time.h>
1288 /******************* for debug func *************/
1289
1290 /* sa time */
1291 static __thread long start_time = 0;
1292 static __thread long now_time = 0;
1293 static __thread long delayed_time = 0;
1294
1295 /* auth */
1296 static __thread long a_start_time = 0;
1297 static __thread long a_now_time = 0;
1298 static __thread long a_delayed_time = 0;
1299
1300 /* storage */
1301 static __thread long s_start_time = 0;
1302 static __thread long s_now_time = 0;
1303 static __thread long s_delayed_time = 0;
1304
1305 /* contact */
1306 static __thread long c_start_time = 0;
1307 static __thread long c_now_time = 0;
1308 static __thread long c_delayed_time = 0;
1309
1310 /* message */
1311 static __thread long m_start_time = 0;
1312 static __thread long m_now_time = 0;
1313 static __thread long m_delayed_time = 0;
1314
1315 /* push */
1316 static __thread long p_start_time = 0;
1317 static __thread long p_now_time = 0;
1318 static __thread long p_delayed_time = 0;
1319
1320 /* shop */
1321 static __thread long h_start_time = 0;
1322 static __thread long h_now_time = 0;
1323 static __thread long h_delayed_time = 0;
1324
1325 static __thread int a_count = 0;
1326 static __thread int s_count = 0;
1327 static __thread int c_count = 0;
1328 static __thread int m_count = 0;
1329 static __thread int p_count = 0;
1330 static __thread int h_count = 0;
1331
1332 static __thread struct timeval tv;
1333
1334 #endif
1335 void SERVICE_ADAPTOR_API_TIME_CHECK_START()
1336 {
1337 #ifdef SERVICE_ADAPTOR_DEBUG_TIME_CHECK
1338         gettimeofday(&tv, NULL);
1339         start_time = tv.tv_sec;
1340 #endif
1341 }
1342 void SERVICE_ADAPTOR_API_TIME_CHECK_PAUSE()
1343 {
1344 #ifdef SERVICE_ADAPTOR_DEBUG_TIME_CHECK
1345         gettimeofday(&tv, NULL);
1346         now_time = tv.tv_sec;
1347
1348         delayed_time += (now_time - start_time);
1349 #endif
1350 }
1351
1352 void SERVICE_ADAPTOR_PLUGIN_API_TIME_CHECK_START(sa_time_check_flag_e flag)
1353 {
1354 #ifdef SERVICE_ADAPTOR_DEBUG_TIME_CHECK
1355         if (SA_TIME_CHECK_FLAG_AUTH == flag) {
1356                 gettimeofday(&tv, NULL);
1357                 a_start_time = tv.tv_sec;
1358         }
1359         if (SA_TIME_CHECK_FLAG_STORAGE == flag) {
1360                 gettimeofday(&tv, NULL);
1361                 s_start_time = tv.tv_sec;
1362         }
1363         if (SA_TIME_CHECK_FLAG_CONTACT == flag) {
1364                 gettimeofday(&tv, NULL);
1365                 c_start_time = tv.tv_sec;
1366         }
1367         if (SA_TIME_CHECK_FLAG_MESSAGE == flag) {
1368                 gettimeofday(&tv, NULL);
1369                 m_start_time = tv.tv_sec;
1370         }
1371         if (SA_TIME_CHECK_FLAG_PUSH == flag) {
1372                 gettimeofday(&tv, NULL);
1373                 p_start_time = tv.tv_sec;
1374         }
1375         if (SA_TIME_CHECK_FLAG_SHOP == flag) {
1376                 gettimeofday(&tv, NULL);
1377                 h_start_time = tv.tv_sec;
1378         }
1379 #endif
1380 }
1381 void SERVICE_ADAPTOR_PLUGIN_API_TIME_CHECK_PAUSE(sa_time_check_flag_e flag)
1382 {
1383 #ifdef SERVICE_ADAPTOR_DEBUG_TIME_CHECK
1384         if (SA_TIME_CHECK_FLAG_AUTH == flag) {
1385                 gettimeofday(&tv, NULL);
1386                 a_now_time = tv.tv_sec;
1387
1388                 a_delayed_time += (a_now_time - a_start_time);
1389                 a_count++;
1390         }
1391         if (SA_TIME_CHECK_FLAG_STORAGE == flag) {
1392                 gettimeofday(&tv, NULL);
1393                 s_now_time = tv.tv_sec;
1394
1395                 s_delayed_time += (s_now_time - s_start_time);
1396                 s_count++;
1397         }
1398         if (SA_TIME_CHECK_FLAG_CONTACT == flag) {
1399                 gettimeofday(&tv, NULL);
1400                 c_now_time = tv.tv_sec;
1401
1402                 c_delayed_time += (c_now_time - c_start_time);
1403                 c_count++;
1404         }
1405         if (SA_TIME_CHECK_FLAG_MESSAGE == flag) {
1406                 gettimeofday(&tv, NULL);
1407                 m_now_time = tv.tv_sec;
1408
1409                 m_delayed_time += (m_now_time - m_start_time);
1410                 m_count++;
1411         }
1412         if (SA_TIME_CHECK_FLAG_PUSH == flag) {
1413                 gettimeofday(&tv, NULL);
1414                 p_now_time = tv.tv_sec;
1415
1416                 p_delayed_time += (p_now_time - p_start_time);
1417                 p_count++;
1418         }
1419         if (SA_TIME_CHECK_FLAG_SHOP == flag) {
1420                 gettimeofday(&tv, NULL);
1421                 h_now_time = tv.tv_sec;
1422
1423                 h_delayed_time += (h_now_time - h_start_time);
1424                 h_count++;
1425         }
1426 #endif
1427 }
1428 void SERVICE_ADAPTOR_API_TIME_CHECK_TOTAL_REPORT(const char *service_name)
1429 {
1430 #ifdef SERVICE_ADAPTOR_DEBUG_TIME_CHECK
1431         service_adaptor_debug_func("[TIMECHECK]================================================");
1432         service_adaptor_debug_func("[TIMECHECK]================================================");
1433         service_adaptor_debug_func("[TIMECHECK]     Total set_auth time report (TID : %lld)", (long long int)syscall(__NR_gettid));
1434         service_adaptor_debug_func("[TIMECHECK]     Service name : %s", service_name);
1435         service_adaptor_debug_func("[TIMECHECK] Total delay time : %ld sec",
1436                         (delayed_time + a_delayed_time + s_delayed_time + c_delayed_time + m_delayed_time + p_delayed_time + h_delayed_time));
1437         service_adaptor_debug_func("[TIMECHECK] Adaptor : %ld sec", delayed_time);
1438         service_adaptor_debug_func("[TIMECHECK] Auth plugin : %ld sec, called : %d", a_delayed_time, a_count);
1439         service_adaptor_debug_func("[TIMECHECK] Storage plugin : %ld sec, called : %d", s_delayed_time, s_count);
1440         service_adaptor_debug_func("[TIMECHECK] Contact plugin : %ld sec, called : %d", c_delayed_time, c_count);
1441         service_adaptor_debug_func("[TIMECHECK] Message plugin : %ld sec, called : %d", m_delayed_time, m_count);
1442         service_adaptor_debug_func("[TIMECHECK] Push plugin : %ld sec, called : %d", p_delayed_time, p_count);
1443         service_adaptor_debug_func("[TIMECHECK] Shop plugin : %ld sec, called : %d", h_delayed_time, h_count);
1444         service_adaptor_debug_func("[TIMECHECK]================================================");
1445         service_adaptor_debug_func("[TIMECHECK]================================================");
1446 #endif
1447 }
1448