40c3b0bad82f70d0597db3296fbca3282bbd22da
[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
1023         /* 3) assign to global service adaptor handle */
1024         g_service_adaptor = service_adaptor;
1025
1026         /* 4) init dbus */
1027         ret = dbus_ipc_server_layer_init();
1028
1029         if (SERVICE_ADAPTOR_INTERNAL_ERROR_NONE != ret) {
1030                 service_adaptor_error("Could not init D-Bus IPC server: %d", ret);
1031                 return SERVICE_ADAPTOR_INTERNAL_ERROR_DBUS;
1032         }
1033
1034         service_adaptor_debug("Service Adaptor: init D-Bus");
1035         service_adaptor_debug("Service Adaptor: Initialized (%d)", service_adaptor->started);
1036
1037         service_adaptor_debug("Service Adaptor: init preload service");
1038         pthread_t job;
1039         pthread_create(&job, NULL, servive_adaptor_preload_service_files, NULL);
1040 FUNC_END();
1041         return SERVICE_ADAPTOR_INTERNAL_ERROR_NONE;
1042 }
1043
1044 /**
1045  * @brief deinit service adaptor
1046  *
1047  * @param[in]   service_adaptor         specifies handle of service adaptor
1048  * @return      void.
1049  */
1050 void service_adaptor_deinit()
1051 {
1052         service_adaptor_debug("Service Adaptor: Deinitialize");
1053
1054         if (NULL == g_service_adaptor) {
1055                 service_adaptor_error("Invalid argument");
1056                 return;
1057         }
1058
1059         service_adaptor_h service_adaptor = g_service_adaptor;
1060         g_service_adaptor = NULL;
1061         service_adaptor_destroy(service_adaptor);
1062         service_adaptor_debug("Service Adaptor: Deinitialized");
1063 }
1064
1065 /**
1066  * @brief main signal function
1067  *
1068  * @param[in]   data            specifies user data passed by main function
1069  * @return      void.
1070  */
1071 static gint sigterm_callback(void *data)
1072 {
1073         service_adaptor_info("Service Adaptor Shutdown");
1074
1075         g_main_loop_quit((GMainLoop *)data);
1076
1077         return FALSE;
1078 }
1079
1080 /******************************************************************************
1081   Public interface definition
1082  *****************************************************************************/
1083
1084 service_adaptor_internal_error_code_e service_adaptor_auth_refresh(service_adaptor_h service_adaptor,
1085                                                 const char *service_name,
1086                                                 const char *plugin_uri)
1087 {
1088 FUNC_START();
1089         service_adaptor_debug("Auth refresh Start");
1090
1091         service_adaptor_service_context_h service_context =
1092                         service_adaptor_get_service_context(service_adaptor, service_name);
1093
1094 FUNC_END();
1095         return service_adaptor_auth_refresh_with_service_context(service_adaptor, service_context, plugin_uri);
1096 }
1097
1098 service_adaptor_internal_error_code_e service_adaptor_auth_refresh_with_service_context(service_adaptor_h service_adaptor,
1099                                                 service_adaptor_service_context_h service_context,
1100                                                 const char *plugin_uri)
1101 {
1102 FUNC_START();
1103         service_adaptor_debug("Auth refresh with service context Start");
1104
1105         if (NULL == service_context) {
1106                 service_adaptor_warning("Parameter is NULL (service_adaptor_h service_adaptor)");
1107                 return SERVICE_ADAPTOR_INTERNAL_ERROR_INVALID_HANDLE;
1108         }
1109
1110         if (NULL == service_context->auth_context) {
1111                 service_adaptor_warning("Parameter is NULL (service_context->auth_context)");
1112                 return SERVICE_ADAPTOR_INTERNAL_ERROR_INVALID_HANDLE;
1113         }
1114
1115         int ret = SERVICE_ADAPTOR_INTERNAL_ERROR_NONE;
1116         char *old_access_token = NULL;
1117         char *new_access_token = NULL;
1118         char *new_uid = NULL;
1119
1120         service_adaptor_debug("Get contexts and plugins from service_adaptor_h");
1121         if (NULL != service_context->context_info) {
1122                 old_access_token = service_context->context_info->access_token;
1123         } else {
1124                 service_adaptor_warning("Element is NULL (service_context->context->info)");
1125                 return SERVICE_ADAPTOR_INTERNAL_ERROR_INVALID_HANDLE;
1126         }
1127         auth_adaptor_plugin_h auth_plugin =
1128                         auth_adaptor_get_plugin_by_name(service_adaptor->auth_handle, plugin_uri);
1129         auth_adaptor_error_code_h       auth_error      = NULL;
1130
1131         int is_auth = -1;
1132         service_adaptor_debug("Call is_auth");
1133         ret = auth_adaptor_is_auth(auth_plugin, service_context->auth_context, NULL, &is_auth, &auth_error, NULL);
1134
1135         if (0 == is_auth) {
1136                 service_adaptor_error("Auth was withdrew (Can not use all service)");
1137                 /*TODO change context_info's auth flag */
1138                 return SERVICE_ADAPTOR_INTERNAL_ERROR_NOT_AUTHORIZED;
1139         }
1140         auth_adaptor_destroy_error_code(&auth_error);
1141
1142         service_adaptor_debug("Call auth_adaptor_login");
1143         ret = auth_adaptor_login(auth_plugin, service_context->auth_context, is_auth, NULL, &auth_error, NULL);
1144
1145         if ((NULL != old_access_token) && (AUTH_ADAPTOR_ERROR_NONE == ret)) {
1146                 if (NULL == service_context->auth_context->access_token) {
1147                         service_adaptor_error("auth_plugin returns login success but auth_access token is empty");
1148                         service_context->auth_context->access_token = strdup(old_access_token);
1149                         ret = SERVICE_ADAPTOR_INTERNAL_ERROR_NOT_AUTHORIZED;
1150                 } else {
1151                         if (0 == strcmp(old_access_token, service_context->auth_context->access_token)) {
1152                                 service_adaptor_debug("Call auth_adaptor_login_refresh");
1153                                 ret = auth_adaptor_login_refresh(auth_plugin, service_context->auth_context, NULL, &auth_error, NULL);
1154                         }
1155
1156                         if ((AUTH_ADAPTOR_ERROR_NONE == ret) &&
1157                                         (NULL != service_context->auth_context->access_token) &&
1158                                         (0 == strcmp(old_access_token, service_context->auth_context->access_token))) {
1159                                 service_adaptor_debug("Access token was not changed");
1160                         } else if (AUTH_ADAPTOR_ERROR_NONE == ret) {
1161                                 service_adaptor_debug("Changing access token start");
1162
1163                                 new_access_token = auth_adaptor_get_access_token_dup(service_context->auth_context);
1164                                 if (NULL != new_access_token) {
1165                                         service_adaptor_debug_secure("New access token : %s", new_access_token);
1166                                         free(service_context->context_info->access_token);
1167                                         service_context->context_info->access_token = new_access_token;
1168                                         service_adaptor_debug("service_context->context_info->access_token was changed");
1169
1170                                         ret = contact_adaptor_refresh_access_token(service_context->contact_context, new_access_token);
1171                                         if (!ret) {
1172                                                 service_adaptor_debug("service_context->contact_context was changed");
1173                                         }
1174
1175                                         ret = storage_adaptor_refresh_access_token(service_context->storage_context, new_access_token);
1176                                         if (!ret) {
1177                                                 service_adaptor_debug("service_context->storage_context was changed");
1178                                         }
1179
1180                                         ret = message_adaptor_refresh_access_token(service_context->message_context, new_access_token);
1181                                         if (!ret) {
1182                                                 service_adaptor_debug("service_context->message_context was changed");
1183                                         }
1184
1185                                         ret = shop_adaptor_refresh_access_token(service_context->shop_context, new_access_token);
1186                                         if (!ret) {
1187                                                 service_adaptor_debug("service_context->shop_context was changed");
1188                                         }
1189                                 }
1190
1191                                 new_uid = auth_adaptor_get_uid_dup(service_context->auth_context);
1192                                 if (NULL != new_uid) {
1193                                         service_adaptor_debug_secure("New unique id : %s", new_uid);
1194                                         free(service_context->context_info->duid);
1195                                         service_context->context_info->duid = new_uid;
1196                                         service_adaptor_debug("service_context->context_info->uid was changed");
1197
1198                                         ret = contact_adaptor_refresh_uid(service_context->contact_context, new_uid);
1199                                         if (!ret) {
1200                                                 service_adaptor_debug("service_context->contact_context was changed");
1201                                         }
1202
1203                                         ret = storage_adaptor_refresh_uid(service_context->storage_context, new_uid);
1204                                         if (!ret) {
1205                                                 service_adaptor_debug("service_context->storage_context was changed");
1206                                         }
1207
1208                                         ret = message_adaptor_refresh_uid(service_context->message_context, new_uid);
1209                                         if (!ret) {
1210                                                 service_adaptor_debug("service_context->message_context was changed");
1211                                         }
1212
1213                                         ret = shop_adaptor_refresh_uid(service_context->shop_context, new_uid);
1214                                         if (!ret) {
1215                                                 service_adaptor_debug("service_context->shop_context was changed");
1216                                         }
1217                                 }
1218                         } else {
1219                                 service_adaptor_error("Login refresh failed");
1220                                 if (NULL != auth_error) {
1221                                         service_adaptor_error("Auth error code(%lld) message(%s)", auth_error->code, auth_error->msg);
1222                                 }
1223                         }
1224                 }
1225         } else {
1226                 service_adaptor_error("Login refresh failed");
1227                 if (NULL != auth_error) {
1228                         service_adaptor_error("Auth error code(%lld) message(%s)", auth_error->code, auth_error->msg);
1229                 }
1230         }
1231
1232         service_adaptor_debug("Auth refresh End");
1233 FUNC_END();
1234         return ret;
1235 }
1236
1237
1238 /**
1239  * @brief main function
1240  *
1241  * @param[in]   argc            specifies count of arguments
1242  * @param[in]   argv            specifies value list of arguments
1243  * @return      void.
1244  */
1245 int main(int argc, char *argv[])
1246 {
1247         int ret = SERVICE_ADAPTOR_INTERNAL_ERROR_NONE;
1248         GMainLoop *loop;
1249
1250 #if !GLIB_CHECK_VERSION(2, 32, 0)
1251         g_thread_init(NULL);
1252 #endif
1253 #if !GLIB_CHECK_VERSION(2, 35, 0)
1254         g_type_init();
1255 #endif
1256
1257         /* mainloop of main thread */
1258         loop = g_main_loop_new(NULL, FALSE);
1259         g_default_loop = loop;
1260
1261         ret = service_adaptor_init();
1262
1263         if (SERVICE_ADAPTOR_INTERNAL_ERROR_NONE != ret) {
1264                 service_adaptor_error("Service Adaptor initialize error: %d\n", ret);
1265                 return -1;
1266         }
1267
1268         /* installing signal handlers */
1269         g_unix_signal_add_full(G_PRIORITY_HIGH, SIGINT,
1270                         sigterm_callback, loop, NULL);
1271         g_unix_signal_add_full(G_PRIORITY_HIGH, SIGTERM,
1272                         sigterm_callback, loop, NULL);
1273
1274         /* start application's main loop */
1275         g_main_loop_run(loop);
1276
1277         /* cleanup after mainloop */
1278         g_main_loop_unref(loop);
1279
1280         service_adaptor_h service_adaptor = service_adaptor_get_handle();
1281         service_adaptor_deinit(service_adaptor);
1282
1283         return ret;
1284 }
1285
1286 #ifdef SERVICE_ADAPTOR_DEBUG_TIME_CHECK
1287
1288 #include <sys/time.h>
1289 /******************* for debug func *************/
1290
1291 /* sa time */
1292 static __thread long start_time = 0;
1293 static __thread long now_time = 0;
1294 static __thread long delayed_time = 0;
1295
1296 /* auth */
1297 static __thread long a_start_time = 0;
1298 static __thread long a_now_time = 0;
1299 static __thread long a_delayed_time = 0;
1300
1301 /* storage */
1302 static __thread long s_start_time = 0;
1303 static __thread long s_now_time = 0;
1304 static __thread long s_delayed_time = 0;
1305
1306 /* contact */
1307 static __thread long c_start_time = 0;
1308 static __thread long c_now_time = 0;
1309 static __thread long c_delayed_time = 0;
1310
1311 /* message */
1312 static __thread long m_start_time = 0;
1313 static __thread long m_now_time = 0;
1314 static __thread long m_delayed_time = 0;
1315
1316 /* push */
1317 static __thread long p_start_time = 0;
1318 static __thread long p_now_time = 0;
1319 static __thread long p_delayed_time = 0;
1320
1321 /* shop */
1322 static __thread long h_start_time = 0;
1323 static __thread long h_now_time = 0;
1324 static __thread long h_delayed_time = 0;
1325
1326 static __thread int a_count = 0;
1327 static __thread int s_count = 0;
1328 static __thread int c_count = 0;
1329 static __thread int m_count = 0;
1330 static __thread int p_count = 0;
1331 static __thread int h_count = 0;
1332
1333 static __thread struct timeval tv;
1334
1335 #endif
1336 void SERVICE_ADAPTOR_API_TIME_CHECK_START()
1337 {
1338 #ifdef SERVICE_ADAPTOR_DEBUG_TIME_CHECK
1339         gettimeofday(&tv, NULL);
1340         start_time = tv.tv_sec;
1341 #endif
1342 }
1343 void SERVICE_ADAPTOR_API_TIME_CHECK_PAUSE()
1344 {
1345 #ifdef SERVICE_ADAPTOR_DEBUG_TIME_CHECK
1346         gettimeofday(&tv, NULL);
1347         now_time = tv.tv_sec;
1348
1349         delayed_time += (now_time - start_time);
1350 #endif
1351 }
1352
1353 void SERVICE_ADAPTOR_PLUGIN_API_TIME_CHECK_START(sa_time_check_flag_e flag)
1354 {
1355 #ifdef SERVICE_ADAPTOR_DEBUG_TIME_CHECK
1356         if (SA_TIME_CHECK_FLAG_AUTH == flag) {
1357                 gettimeofday(&tv, NULL);
1358                 a_start_time = tv.tv_sec;
1359         }
1360         if (SA_TIME_CHECK_FLAG_STORAGE == flag) {
1361                 gettimeofday(&tv, NULL);
1362                 s_start_time = tv.tv_sec;
1363         }
1364         if (SA_TIME_CHECK_FLAG_CONTACT == flag) {
1365                 gettimeofday(&tv, NULL);
1366                 c_start_time = tv.tv_sec;
1367         }
1368         if (SA_TIME_CHECK_FLAG_MESSAGE == flag) {
1369                 gettimeofday(&tv, NULL);
1370                 m_start_time = tv.tv_sec;
1371         }
1372         if (SA_TIME_CHECK_FLAG_PUSH == flag) {
1373                 gettimeofday(&tv, NULL);
1374                 p_start_time = tv.tv_sec;
1375         }
1376         if (SA_TIME_CHECK_FLAG_SHOP == flag) {
1377                 gettimeofday(&tv, NULL);
1378                 h_start_time = tv.tv_sec;
1379         }
1380 #endif
1381 }
1382 void SERVICE_ADAPTOR_PLUGIN_API_TIME_CHECK_PAUSE(sa_time_check_flag_e flag)
1383 {
1384 #ifdef SERVICE_ADAPTOR_DEBUG_TIME_CHECK
1385         if (SA_TIME_CHECK_FLAG_AUTH == flag) {
1386                 gettimeofday(&tv, NULL);
1387                 a_now_time = tv.tv_sec;
1388
1389                 a_delayed_time += (a_now_time - a_start_time);
1390                 a_count++;
1391         }
1392         if (SA_TIME_CHECK_FLAG_STORAGE == flag) {
1393                 gettimeofday(&tv, NULL);
1394                 s_now_time = tv.tv_sec;
1395
1396                 s_delayed_time += (s_now_time - s_start_time);
1397                 s_count++;
1398         }
1399         if (SA_TIME_CHECK_FLAG_CONTACT == flag) {
1400                 gettimeofday(&tv, NULL);
1401                 c_now_time = tv.tv_sec;
1402
1403                 c_delayed_time += (c_now_time - c_start_time);
1404                 c_count++;
1405         }
1406         if (SA_TIME_CHECK_FLAG_MESSAGE == flag) {
1407                 gettimeofday(&tv, NULL);
1408                 m_now_time = tv.tv_sec;
1409
1410                 m_delayed_time += (m_now_time - m_start_time);
1411                 m_count++;
1412         }
1413         if (SA_TIME_CHECK_FLAG_PUSH == flag) {
1414                 gettimeofday(&tv, NULL);
1415                 p_now_time = tv.tv_sec;
1416
1417                 p_delayed_time += (p_now_time - p_start_time);
1418                 p_count++;
1419         }
1420         if (SA_TIME_CHECK_FLAG_SHOP == flag) {
1421                 gettimeofday(&tv, NULL);
1422                 h_now_time = tv.tv_sec;
1423
1424                 h_delayed_time += (h_now_time - h_start_time);
1425                 h_count++;
1426         }
1427 #endif
1428 }
1429 void SERVICE_ADAPTOR_API_TIME_CHECK_TOTAL_REPORT(const char *service_name)
1430 {
1431 #ifdef SERVICE_ADAPTOR_DEBUG_TIME_CHECK
1432         service_adaptor_debug_func("[TIMECHECK]================================================");
1433         service_adaptor_debug_func("[TIMECHECK]================================================");
1434         service_adaptor_debug_func("[TIMECHECK]     Total set_auth time report (TID : %lld)", (long long int)syscall(__NR_gettid));
1435         service_adaptor_debug_func("[TIMECHECK]     Service name : %s", service_name);
1436         service_adaptor_debug_func("[TIMECHECK] Total delay time : %ld sec",
1437                         (delayed_time + a_delayed_time + s_delayed_time + c_delayed_time + m_delayed_time + p_delayed_time + h_delayed_time));
1438         service_adaptor_debug_func("[TIMECHECK] Adaptor : %ld sec", delayed_time);
1439         service_adaptor_debug_func("[TIMECHECK] Auth plugin : %ld sec, called : %d", a_delayed_time, a_count);
1440         service_adaptor_debug_func("[TIMECHECK] Storage plugin : %ld sec, called : %d", s_delayed_time, s_count);
1441         service_adaptor_debug_func("[TIMECHECK] Contact plugin : %ld sec, called : %d", c_delayed_time, c_count);
1442         service_adaptor_debug_func("[TIMECHECK] Message plugin : %ld sec, called : %d", m_delayed_time, m_count);
1443         service_adaptor_debug_func("[TIMECHECK] Push plugin : %ld sec, called : %d", p_delayed_time, p_count);
1444         service_adaptor_debug_func("[TIMECHECK] Shop plugin : %ld sec, called : %d", h_delayed_time, h_count);
1445         service_adaptor_debug_func("[TIMECHECK]================================================");
1446         service_adaptor_debug_func("[TIMECHECK]================================================");
1447 #endif
1448 }
1449