Fix build warnings
[platform/core/multimedia/audio-session-manager.git] / src / audio-session-mgr.c
1 /*
2  * audio-session-manager
3  *
4  * Copyright (c) 2000 - 2013 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Seungbae Shin <seungbae.shin at samsung.com>, Sangchul Lee <sc11.lee at samsung.com>
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  */
21
22 #define CONFIG_ENABLE_MULTI_INSTANCE
23 #define CONFIG_ENABLE_ASM_SERVER_USING_GLIB
24 #define CONFIG_ENABLE_SIGNAL_HANDLER
25 #define CONFIG_ENABLE_RETCB
26 #define MAKE_HANDLE_FROM_SERVER
27
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <glib.h>
31 #include <sys/poll.h>
32 #include <sys/syscall.h>
33 #include <sys/time.h>
34 #include <sys/types.h>
35 #include <sys/ipc.h>
36 #include <sys/msg.h>
37 #include <sys/stat.h>
38 #include <unistd.h>
39 #include <fcntl.h>
40 #include <signal.h>
41 #include <poll.h>
42 #include <string.h>
43 #include <mm_debug.h>
44
45 #include <gio/gio.h>
46 #include <unistd.h>
47
48 #ifdef USE_SECURITY
49 #include <security-server.h>
50 #define COOKIE_SIZE 20
51 #endif
52
53 #if defined(USE_VCONF)
54 #include <vconf.h>
55 #include <errno.h>
56 #else
57 #include <gconf/gconf.h>
58 #include <gconf/gconf-client.h>
59 #include <phonestatus.h>
60
61 #endif
62 #include "../include/audio-session-manager.h"
63
64 #define asmgettid() (long int)getpid()
65 #define ASM_HANDLE_MAX 256
66
67 #define NO_EINTR(stmt) while ((stmt) == -1 && errno == EINTR);  /* sample code by THE LINUX PROGRAMMING INTERFACE */
68
69 static ASM_msg_lib_to_asm_t asm_snd_msg;
70 static ASM_msg_asm_to_lib_t asm_rcv_msg;
71
72 static unsigned char            str_pass[] = "< OK >";
73 static unsigned char            str_fail[] = "<FAIL>";
74
75 typedef gboolean (*gLoopPollHandler_t)(gpointer d);
76
77 static GThread *g_asm_thread;
78 static GMainLoop *g_asm_loop;
79
80 typedef struct
81 {
82         int                asm_tid;
83         int                handle;
84         ASM_sound_events_t sound_event;
85         ASM_sound_states_t sound_state;
86         ASM_sound_cb_t     asm_callback;
87         ASM_watch_cb_t     watch_callback;
88         void               *user_data;
89         int                option_flags;
90         int                asm_fd;
91         GSourceFuncs*      g_src_funcs;
92         GPollFD*           g_poll_fd;
93         GSource*           asm_src;
94         bool               is_used;
95         bool               is_for_watching;
96         GMutex*            asm_lock;
97 } ASM_sound_info_t;
98
99 static ASM_sound_info_t ASM_sound_handle[ASM_HANDLE_MAX];
100
101 static const char* ASM_sound_events_str[] =
102 {
103         "MEDIA_MMPLAYER",
104         "MEDIA_MMCAMCORDER",
105         "MEDIA_MMSOUND",
106         "MEDIA_OPENAL",
107         "MEDIA_FMRADIO",
108         "MEDIA_WEBKIT",
109         "NOTIFY",
110         "ALARM",
111         "EARJACK_UNPLUG",
112         "CALL",
113         "VIDEOCALL",
114         "VOIP",
115         "MONITOR",
116         "EMERGENCY",
117         "EXCLUSIVE_RESOURCE",
118         "VOICE_RECOGNITION",
119         "MMCAMCORDER_AUDIO",
120         "MMCAMCORDER_VIDEO"
121 };
122
123 static const char* ASM_sound_state_str[] =
124 {
125         "STATE_NONE",
126         "STATE_PLAYING",
127         "STATE_WAITING",
128         "STATE_STOP",
129         "STATE_PAUSE"
130 };
131
132 static unsigned int ASM_all_sound_status;
133
134 static int __ASM_find_index_by_handle(int handle);
135
136 static gpointer thread_func(gpointer data)
137 {
138         debug_log(">>> thread func..ID of this thread(%u)\n", (unsigned int)pthread_self());
139         g_main_loop_run(g_asm_loop);
140         debug_log("<<< quit thread func..\n");
141         return NULL;
142 }
143
144 static bool __ASM_get_sound_state(unsigned int *all_sound_status, int *error_code)
145 {
146         int value = 0;
147
148         if(vconf_get_int(SOUND_STATUS_KEY, &value)) {
149                 debug_error("failed to vconf_get_int(SOUND_STATUS_KEY)");
150                 *error_code = ERR_ASM_VCONF_ERROR;
151                 return false;
152         }
153         debug_log("All status(%#X)", value);
154         *all_sound_status = value;
155         ASM_all_sound_status = value;
156
157         return true;
158 }
159
160 static gboolean __asm_fd_check(GSource * source)
161 {
162         GSList *fd_list;
163         GPollFD *temp;
164
165         if (!source) {
166                 debug_error("GSource is null");
167                 return FALSE;
168         }
169         fd_list = source->poll_fds;
170         if (!fd_list) {
171                 debug_error("fd_list is null");
172                 return FALSE;
173         }
174         do {
175                 temp = (GPollFD*)fd_list->data;
176                 if (!temp) {
177                         debug_error("fd_list->data is null");
178                         return FALSE;
179                 }
180                 if (temp->revents & (POLLIN | POLLPRI)) {
181                         return TRUE;
182                 }
183                 fd_list = fd_list->next;
184         } while (fd_list);
185
186         return FALSE; /* there is no change in any fd state */
187 }
188
189 static gboolean __asm_fd_prepare(GSource *source, gint *timeout)
190 {
191         return FALSE;
192 }
193
194 static gboolean __asm_fd_dispatch(GSource *source,      GSourceFunc callback, gpointer user_data)
195 {
196         callback(user_data);
197         return TRUE;
198 }
199
200 static gboolean asm_callback_handler( gpointer d)
201 {
202         GPollFD *data = (GPollFD*)d;
203         unsigned int buf;
204         int count;
205         int tid = 0;
206         int asm_index = 0;
207         //debug_fenter();
208         debug_log(">>> asm_callback_handler()..ID of this thread(%u)\n", (unsigned int)pthread_self());
209
210         if (!data) {
211                 debug_error("GPollFd is null");
212                 return FALSE;
213         }
214         if (data->revents & (POLLIN | POLLPRI)) {
215                 int handle;
216                 int error_code = 0;
217                 int event_src;
218                 unsigned int sound_status_value;
219                 ASM_sound_commands_t rcv_command;
220                 ASM_cb_result_t cb_res = ASM_CB_RES_NONE;
221
222
223                 count = read(data->fd, &buf, sizeof(int));
224                 if (count != sizeof(int)) {
225                         debug_log("read %d/%d", count, sizeof(int));
226                 }
227
228                 handle = (int)( buf & 0x0000ffff);
229                 rcv_command = (ASM_sound_commands_t)((buf >> 16) & 0xff);
230                 event_src = (ASM_event_sources_t)((buf >> 24) & 0xff);
231
232                 asm_index = __ASM_find_index_by_handle(handle);
233                 if (asm_index == -1) {
234                         debug_error("Can not find index");
235                         return FALSE;
236                 }
237
238                 if (ASM_sound_handle[asm_index].asm_lock) {
239                         g_mutex_lock(ASM_sound_handle[asm_index].asm_lock);
240                 }
241
242                 tid = ASM_sound_handle[asm_index].asm_tid;
243
244                 if (rcv_command) {
245                         debug_msg("Got and start CB : TID(%d), handle(%d), command(%d,(PLAY(2)/STOP(3)/PAUSE(4)/RESUME(5)), event_src(%d)",
246                                         tid, handle, rcv_command, event_src );
247                         if (!__ASM_get_sound_state(&sound_status_value, &error_code)) {
248                                 debug_error("failed to __ASM_get_sound_state(), error(%d)", error_code);
249                         }
250                         switch (rcv_command) {
251                         case ASM_COMMAND_PLAY:
252                         case ASM_COMMAND_RESUME:
253                         case ASM_COMMAND_PAUSE:
254                         case ASM_COMMAND_STOP:
255                                 if (ASM_sound_handle[asm_index].asm_callback == NULL) {
256                                         debug_msg("callback is null..");
257                                         break;
258                                 }
259                                 debug_msg("[CALLBACK(%p) START]",ASM_sound_handle[asm_index].asm_callback);
260                                 cb_res = (ASM_sound_handle[asm_index].asm_callback)(handle, event_src, rcv_command, sound_status_value, ASM_sound_handle[asm_index].user_data);
261                                 debug_msg("[CALLBACK END]");
262                                 break;
263                         default:
264                                 break;
265                         }
266 #ifdef CONFIG_ENABLE_RETCB
267
268                         /* If the command is not RESUME, send return */
269                         if (rcv_command != ASM_COMMAND_RESUME) {
270                                 int rett = 0;
271                                 int buf = cb_res;
272                                 int tmpfd = -1;
273                                 char *filename2 = g_strdup_printf("/tmp/ASM.%d.%dr", ASM_sound_handle[asm_index].asm_tid, handle);
274                                 tmpfd = open(filename2, O_WRONLY | O_NONBLOCK);
275                                 if (tmpfd < 0) {
276                                         char str_error[256];
277                                         strerror_r(errno, str_error, sizeof(str_error));
278                                         debug_error("[RETCB][Failed(May Server Close First)]tid(%d) fd(%d) %s errno=%d(%s)\n", tid, tmpfd, filename2, errno, str_error);
279                                         g_free(filename2);
280                                         if (ASM_sound_handle[asm_index].asm_lock) {
281                                                 g_mutex_unlock(ASM_sound_handle[asm_index].asm_lock);
282                                         }
283                                         return FALSE;
284                                 }
285                                 rett = write(tmpfd, &buf, sizeof(buf));
286                                 close(tmpfd);
287                                 g_free(filename2);
288                                 debug_msg("[RETCB] tid(%d) finishing CB (write=%d)\n", tid, rett);
289                         } else {
290                                 debug_msg("[RETCB] No need to send return for RESUME command\n");
291                         }
292 #endif
293                 }
294         }
295         //debug_fleave();
296
297         if (ASM_sound_handle[asm_index].asm_lock) {
298                 g_mutex_unlock(ASM_sound_handle[asm_index].asm_lock);
299         }
300
301         return TRUE;
302 }
303
304 static gboolean watch_callback_handler( gpointer d)
305 {
306         GPollFD *data = (GPollFD*)d;
307         unsigned int buf;
308         int count;
309         int tid = 0;
310         int asm_index = 0;
311
312         debug_fenter();
313
314         if (!data) {
315                 debug_error("GPollFd is null");
316                 return FALSE;
317         }
318         if (data->revents & (POLLIN | POLLPRI)) {
319                 int handle;
320                 ASM_sound_events_t rcv_sound_event = ASM_EVENT_NONE;
321                 ASM_sound_states_t rcv_sound_state = ASM_STATE_NONE;
322                 int error_code = 0;
323
324                 unsigned int sound_status_value;
325
326                 ASM_cb_result_t cb_res = ASM_CB_RES_NONE;
327
328
329                 count = read(data->fd, &buf, sizeof(int));
330                 if (count != sizeof(int)) {
331                         debug_log("read %d/%d", count, sizeof(int));
332                 }
333
334                 handle = (int)( buf & 0x0000ffff);
335                 rcv_sound_event = (ASM_sound_events_t)((buf >> 16) & 0xff);
336                 rcv_sound_state = (ASM_sound_states_t)((buf >> 24) & 0xff);
337
338                 asm_index = __ASM_find_index_by_handle(handle);
339                 if (asm_index == -1) {
340                         debug_error("Can not find index");
341                         return FALSE;
342                 }
343
344                 if (ASM_sound_handle[asm_index].asm_lock) {
345                         g_mutex_lock(ASM_sound_handle[asm_index].asm_lock);
346                 }
347
348                 tid = ASM_sound_handle[asm_index].asm_tid;
349
350                 debug_msg("Got and start CB : handle(%d) sound_event(%d) sound_state(%d)", handle, rcv_sound_event, rcv_sound_state );
351
352                 if (!__ASM_get_sound_state(&sound_status_value, &error_code)) {
353                         debug_error("failed to __ASM_get_sound_state(), error(%d)", error_code);
354                 }
355
356                 if (ASM_sound_handle[asm_index].watch_callback == NULL) {
357                         debug_msg("callback is null..");
358                         if (ASM_sound_handle[asm_index].asm_lock) {
359                                 g_mutex_unlock(ASM_sound_handle[asm_index].asm_lock);
360                         }
361                         return FALSE;
362                 }
363                 debug_msg("[CALLBACK(%p) START]",ASM_sound_handle[asm_index].watch_callback);
364                 cb_res = (ASM_sound_handle[asm_index].watch_callback)(handle, rcv_sound_event, rcv_sound_state, ASM_sound_handle[asm_index].user_data);
365                 debug_msg("[CALLBACK END]");
366
367 #ifdef CONFIG_ENABLE_RETCB
368                 {
369                         int rett = 0;
370                         int buf = cb_res;
371                         int tmpfd = -1;
372                         char *filename2 = g_strdup_printf("/tmp/ASM.%d.%dr", ASM_sound_handle[asm_index].asm_tid, handle);
373                         tmpfd = open(filename2, O_WRONLY | O_NONBLOCK);
374                         if (tmpfd < 0) {
375                                 char str_error[256];
376                                 strerror_r(errno, str_error, sizeof(str_error));
377                                 debug_error("[RETCB][Failed(May Server Close First)]tid(%d) fd(%d) %s errno=%d(%s)\n", tid, tmpfd, filename2, errno, str_error);
378                                 g_free(filename2);
379                                 if (ASM_sound_handle[asm_index].asm_lock) {
380                                         g_mutex_unlock(ASM_sound_handle[asm_index].asm_lock);
381                                 }
382                                 return FALSE;
383                         }
384                         rett = write(tmpfd, &buf, sizeof(buf));
385                         close(tmpfd);
386                         g_free(filename2);
387                         debug_msg("[RETCB] tid(%d) finishing CB (write=%d)\n", tid, rett);
388                 }
389 #endif
390
391         }
392         debug_fleave();
393
394         if (ASM_sound_handle[asm_index].asm_lock) {
395                 g_mutex_unlock(ASM_sound_handle[asm_index].asm_lock);
396         }
397
398         return TRUE;
399 }
400
401
402 static bool __ASM_add_sound_callback(int index, int fd, gushort events, gLoopPollHandler_t p_gloop_poll_handler )
403 {
404         GSource* g_src = NULL;
405         GSourceFuncs *g_src_funcs = NULL;               /* handler function */
406         guint gsource_handle;
407         GPollFD *g_poll_fd = NULL;                      /* file descriptor */
408
409         ASM_sound_handle[index].asm_lock = g_new(GMutex, 1);
410         if (!ASM_sound_handle[index].asm_lock) {
411                 debug_error("failed to alloc GMutex for index(%d)", index);
412                 return false;
413         }
414         g_mutex_init(ASM_sound_handle[index].asm_lock);
415
416         /* 1. make GSource Object */
417         g_src_funcs = (GSourceFuncs *)g_malloc(sizeof(GSourceFuncs));
418         if (!g_src_funcs) {
419                 debug_error("g_malloc failed on g_src_funcs");
420                 return false;
421         }
422         g_src_funcs->prepare = __asm_fd_prepare;
423         g_src_funcs->check = __asm_fd_check;
424         g_src_funcs->dispatch = __asm_fd_dispatch;
425         g_src_funcs->finalize = NULL;
426         g_src = g_source_new(g_src_funcs, sizeof(GSource));
427         if (!g_src) {
428                 debug_error("g_malloc failed on m_readfd");
429                 return false;
430         }
431         ASM_sound_handle[index].asm_src = g_src;
432         ASM_sound_handle[index].g_src_funcs = g_src_funcs;
433
434         /* 2. add file description which used in g_loop() */
435         g_poll_fd = (GPollFD *)g_malloc(sizeof(GPollFD));
436         if (!g_poll_fd) {
437                 debug_error("g_malloc failed on g_poll_fd");
438                 return false;
439         }
440         g_poll_fd->fd = fd;
441         g_poll_fd->events = events;
442         ASM_sound_handle[index].g_poll_fd = g_poll_fd;
443
444         /* 3. combine g_source object and file descriptor */
445         g_source_add_poll(g_src, g_poll_fd);
446         gsource_handle = g_source_attach(g_src, g_main_loop_get_context(g_asm_loop));
447         if (!gsource_handle) {
448                 debug_error(" Failed to attach the source to context");
449                 return false;
450         }
451         g_source_unref(g_src);
452
453         /* 4. set callback */
454         g_source_set_callback(g_src, p_gloop_poll_handler,(gpointer)g_poll_fd, NULL);
455
456         debug_log(" g_malloc:g_src_funcs(%#X),g_poll_fd(%#X)  g_source_add_poll:g_src_id(%d)  g_source_set_callback:errno(%d)",
457                                 g_src_funcs, g_poll_fd, gsource_handle, errno);
458         return true;
459 }
460
461
462 static bool __ASM_remove_sound_callback(int index, gushort events)
463 {
464         bool ret = true;
465
466         if (ASM_sound_handle[index].asm_lock) {
467                 g_mutex_clear(ASM_sound_handle[index].asm_lock);
468                 g_free(ASM_sound_handle[index].asm_lock);
469                 ASM_sound_handle[index].asm_lock = NULL;
470         }
471
472         GSourceFuncs *g_src_funcs = ASM_sound_handle[index].g_src_funcs;
473         GPollFD *g_poll_fd = ASM_sound_handle[index].g_poll_fd; /* store file descriptor */
474         if (!g_poll_fd) {
475                 debug_error("g_poll_fd is null..");
476                 ret = false;
477                 goto init_handle;
478         }
479         g_poll_fd->fd = ASM_sound_handle[index].asm_fd;
480         g_poll_fd->events = events;
481
482         if (!ASM_sound_handle[index].asm_src) {
483                 debug_error("ASM_sound_handle[%d].asm_src is null..", index);
484                 goto init_handle;
485         }
486         debug_log(" g_source_remove_poll : fd(%d), event(%x), errno(%d)", g_poll_fd->fd, g_poll_fd->events, errno);
487         g_source_remove_poll(ASM_sound_handle[index].asm_src, g_poll_fd);
488
489 init_handle:
490
491         if (ASM_sound_handle[index].asm_src) {
492                 g_source_destroy(ASM_sound_handle[index].asm_src);
493                 if (!g_source_is_destroyed (ASM_sound_handle[index].asm_src)) {
494                         debug_warning(" failed to g_source_destroy(), asm_src(0x%p)", ASM_sound_handle[index].asm_src);
495                 }
496         }
497         debug_log(" g_free : g_src_funcs(%#X), g_poll_fd(%#X)", g_src_funcs, g_poll_fd);
498
499         if (g_src_funcs) {
500                 g_free(g_src_funcs);
501                 g_src_funcs = NULL;
502         }
503         if (g_poll_fd) {
504                 g_free(g_poll_fd);
505                 g_poll_fd = NULL;
506         }
507
508         ASM_sound_handle[index].g_src_funcs = NULL;
509         ASM_sound_handle[index].g_poll_fd = NULL;
510         ASM_sound_handle[index].asm_src = NULL;
511         ASM_sound_handle[index].asm_callback = NULL;
512         ASM_sound_handle[index].watch_callback = NULL;
513
514         return ret;
515 }
516
517
518 static bool __ASM_is_existed_request_for_watching(ASM_sound_events_t interest_event, ASM_sound_states_t interest_state, int *index)
519 {
520         int i = 0;
521         for(i = 0; i< ASM_HANDLE_MAX; i++) {
522                 if (ASM_sound_handle[i].is_for_watching && ASM_sound_handle[i].sound_event == interest_event) {
523                         if (ASM_sound_handle[i].sound_state == interest_state) {
524                                 debug_warning("already requested interest-session(%s, %s)",
525                                                 ASM_sound_events_str[interest_event], ASM_sound_state_str[interest_state]);
526                                 *index = i;
527                                 return true;
528                         }
529                 }
530         }
531         *index = 0;
532         return false;
533 }
534
535
536 static bool __ASM_is_supported_session_for_watching(ASM_sound_events_t interest_event, ASM_sound_states_t interest_state)
537 {
538         bool ret = false;
539
540         /* check sound_event */
541         switch (interest_event) {
542         case ASM_EVENT_MEDIA_MMPLAYER:
543         case ASM_EVENT_MEDIA_MMCAMCORDER:
544         case ASM_EVENT_MEDIA_MMSOUND:
545         case ASM_EVENT_MEDIA_OPENAL:
546         case ASM_EVENT_MEDIA_FMRADIO:
547         case ASM_EVENT_MEDIA_WEBKIT:
548         case ASM_EVENT_NOTIFY:
549         case ASM_EVENT_ALARM:
550         case ASM_EVENT_EARJACK_UNPLUG:
551         case ASM_EVENT_CALL:
552         case ASM_EVENT_VIDEOCALL:
553         case ASM_EVENT_VOIP:
554         case ASM_EVENT_MONITOR:
555         case ASM_EVENT_EMERGENCY:
556         case ASM_EVENT_EXCLUSIVE_RESOURCE:
557         case ASM_EVENT_VOICE_RECOGNITION:
558         case ASM_EVENT_MMCAMCORDER_AUDIO:
559         case ASM_EVENT_MMCAMCORDER_VIDEO:
560                 ret = true;
561                 break;
562         default:
563                 debug_error("not supported sound_event(%d)", interest_event);
564                 ret = false;
565                 return ret;
566         }
567
568         /* check sound_state */
569         switch (interest_state) {
570         case ASM_STATE_PLAYING:
571         case ASM_STATE_STOP:
572                 ret = true;
573                 break;
574         default:
575                 debug_error("not supported sound_state(%d)", interest_state);
576                 ret = false;
577                 return ret;
578         }
579
580         return ret;
581 }
582
583
584 static int __ASM_find_index_by_handle(int handle)
585 {
586         int i = 0;
587         for(i = 0; i< ASM_HANDLE_MAX; i++) {
588                 if (handle == ASM_sound_handle[i].handle) {
589                         //debug_msg("found index(%d) for handle(%d)", i, handle);
590                         if (handle == ASM_HANDLE_INIT_VAL) {
591                                 return -1;
592                         }
593                         return i;
594                 }
595         }
596         return -1;
597 }
598
599 static int __ASM_find_index_by_event(ASM_sound_events_t sound_event, int pid)
600 {
601         int i = 0;
602
603         for(i = 0; i< ASM_HANDLE_MAX; i++) {
604                 if (sound_event == ASM_sound_handle[i].sound_event && pid == ASM_sound_handle[i].asm_tid) {
605                         debug_msg("found index(%d) for sound_event(%d)", i, sound_event);
606                         return i;
607                 }
608         }
609         return -1;
610 }
611
612
613 static void __ASM_add_callback(int index, bool is_for_watching)
614 {
615         if (!is_for_watching) {
616                 if (!__ASM_add_sound_callback(index, ASM_sound_handle[index].asm_fd, (gushort)POLLIN | POLLPRI, asm_callback_handler)) {
617                         debug_error("failed to __ASM_add_sound_callback(asm_callback_handler)");
618                         //return false;
619                 }
620         } else {
621                 if (!__ASM_add_sound_callback(index, ASM_sound_handle[index].asm_fd, (gushort)POLLIN | POLLPRI, watch_callback_handler)) {
622                         debug_error("failed to __ASM_add_sound_callback(watch_callback_handler)");
623                         //return false;
624                 }
625         }
626 }
627
628
629 static void __ASM_remove_callback(int index)
630 {
631         if (!__ASM_remove_sound_callback(index, (gushort)POLLIN | POLLPRI)) {
632                 debug_error("failed to __ASM_remove_sound_callback()");
633                 //return false;
634         }
635 }
636
637
638 static void __ASM_open_callback(int index)
639 {
640         mode_t pre_mask;
641
642         char *filename = g_strdup_printf("/tmp/ASM.%d.%d", ASM_sound_handle[index].asm_tid, ASM_sound_handle[index].handle);
643         pre_mask = umask(0);
644         if (mknod(filename, S_IFIFO|0666, 0)) {
645                 debug_error("mknod() failure, errno(%d)", errno);
646         }
647         umask(pre_mask);
648         ASM_sound_handle[index].asm_fd = open( filename, O_RDWR|O_NONBLOCK);
649         if (ASM_sound_handle[index].asm_fd == -1) {
650                 debug_error("%s : index(%d), file open error(%d)", str_fail, index, errno);
651         } else {
652                 debug_log("%s : index(%d), filename(%s), fd(%d)", str_pass, index, filename, ASM_sound_handle[index].asm_fd);
653         }
654         g_free(filename);
655         filename = NULL;
656
657 #ifdef CONFIG_ENABLE_RETCB
658         char *filename2 = g_strdup_printf("/tmp/ASM.%d.%dr", ASM_sound_handle[index].asm_tid,  ASM_sound_handle[index].handle);
659         pre_mask = umask(0);
660         if (mknod(filename2, S_IFIFO | 0666, 0)) {
661                 debug_error("mknod() failure, errno(%d)", errno);
662         }
663         umask(pre_mask);
664         g_free(filename2);
665         filename2 = NULL;
666 #endif
667
668 }
669
670
671 void __ASM_close_callback(int index)
672 {
673         if (ASM_sound_handle[index].asm_fd < 0) {
674                 debug_error("%s : fd error.", str_fail);
675         } else {
676                 char *filename = g_strdup_printf("/tmp/ASM.%d.%d", ASM_sound_handle[index].asm_tid, ASM_sound_handle[index].handle);
677                 close(ASM_sound_handle[index].asm_fd);
678                 if (remove(filename)) {
679                         debug_error("remove() failure, filename(%s), errno(%d)", filename, errno);
680                 }
681                 debug_log("%s : index(%d), filename(%s), fd(%d)", str_pass, index, filename, ASM_sound_handle[index].asm_fd);
682                 g_free(filename);
683                 filename = NULL;
684         }
685
686 #ifdef CONFIG_ENABLE_RETCB
687         char *filename2 = g_strdup_printf("/tmp/ASM.%d.%dr", ASM_sound_handle[index].asm_tid, ASM_sound_handle[index].handle);
688
689         /* Defensive code - wait until callback timeout although callback is removed */
690         int buf = ASM_CB_RES_STOP;
691         int tmpfd = -1;
692         int ret;
693         tmpfd = open(filename2, O_WRONLY | O_NONBLOCK);
694         if (tmpfd < 0) {
695                 char str_error[256];
696                 strerror_r(errno, str_error, sizeof(str_error));
697                 debug_warning("could not open file(%s) (may server close it first), tid(%d) fd(%d) %s errno=%d(%s)",
698                         filename2, ASM_sound_handle[index].asm_tid, tmpfd, filename2, errno, str_error);
699         } else {
700                 ret = write(tmpfd, &buf, sizeof(buf));
701                 close(tmpfd);
702                 debug_msg("write ASM_CB_RES_STOP(tid:%d) for waiting server , error code(%d)", ASM_sound_handle[index].asm_tid, ret);
703         }
704
705         if (remove(filename2)) {
706                 debug_error("remove() failure, filename(%s), errno(%d)", filename2, errno);
707         }
708         g_free(filename2);
709         filename2 = NULL;
710 #endif
711
712 }
713
714 static bool __asm_construct_snd_msg(int asm_pid, int handle, ASM_sound_events_t sound_event,
715                                         ASM_requests_t request_id, ASM_sound_states_t sound_state, ASM_resource_t resource, int *error_code)
716 {
717         asm_snd_msg.instance_id = asm_pid;
718
719         asm_snd_msg.data.handle = handle;
720         asm_snd_msg.data.request_id = request_id;
721         asm_snd_msg.data.sound_event = sound_event;
722         asm_snd_msg.data.sound_state = sound_state;
723         asm_snd_msg.data.system_resource = resource;
724
725         debug_msg("tid=%ld,handle=%d,req=%d,evt=%d,state=%d,resource=%d,instance_id=%ld", asm_snd_msg.instance_id,
726                                 asm_snd_msg.data.handle, asm_snd_msg.data.request_id, asm_snd_msg.data.sound_event,
727                                 asm_snd_msg.data.sound_state, asm_snd_msg.data.system_resource, asm_snd_msg.instance_id);
728
729         return true;
730 }
731
732 static void __ASM_init_callback(int index, bool is_for_watching)
733 {
734         debug_fenter();
735         __ASM_open_callback(index);
736         __ASM_add_callback(index, is_for_watching);
737         debug_fleave();
738 }
739
740
741 static void __ASM_destroy_callback(int index)
742 {
743         debug_fenter();
744         __ASM_remove_callback(index);
745         __ASM_close_callback(index);
746         debug_fleave();
747 }
748
749 #ifdef SUPPORT_CONTAINER
750 #ifdef USE_SECURITY
751 char* _get_cookie(int cookie_size)
752 {
753         int retval = -1;
754         char* cookie = NULL;
755
756         if (security_server_get_cookie_size() != cookie_size) {
757                 debug_error ("[Security] security_server_get_cookie_size() != COOKIE_SIZE(%d)\n", cookie_size);
758                 return false;
759         }
760
761         cookie = (char*)malloc (cookie_size);
762
763         retval = security_server_request_cookie (cookie, cookie_size);
764         if (retval == SECURITY_SERVER_API_SUCCESS) {
765                 debug_msg ("[Security] security_server_request_cookie() returns [%d]\n", retval);
766         } else {
767                 debug_error ("[Security] security_server_request_cookie() returns [%d]\n", retval);
768         }
769
770         return cookie;
771 }
772
773 static GVariant* _get_cookie_variant ()
774 {
775         int i;
776         GVariantBuilder builder;
777         char* cookie = NULL;
778
779         cookie = _get_cookie(COOKIE_SIZE);
780
781         if (cookie == NULL)
782                 return NULL;
783
784         g_variant_builder_init(&builder, G_VARIANT_TYPE_ARRAY);
785         for (i = 0; i < COOKIE_SIZE; i++)
786                 g_variant_builder_add(&builder, "y", cookie[i]);
787
788         free (cookie);
789         return g_variant_builder_end(&builder);
790 }
791
792 #endif /* USE_SECURITY */
793 #endif /* SUPPORT_CONTAINER */
794
795 EXPORT_API
796 bool ASM_register_sound_ex (const int application_pid, int *asm_handle, ASM_sound_events_t sound_event, ASM_sound_states_t sound_state,
797                                                 ASM_sound_cb_t callback, void *user_data, ASM_resource_t mm_resource, int *error_code, int (*func)(void*,void*))
798 {
799         unsigned int sound_status_value;
800         int handle = 0;
801         int asm_pid = 0;
802         int index = 0;
803         GError *err = NULL;
804         GDBusConnection *conn = NULL;
805         GVariant *res_variant = NULL;
806         GVariant *client_variant = NULL;
807 #ifdef SUPPORT_CONTAINER
808         char container[128];
809 #endif
810
811         debug_fenter();
812
813         if (error_code==NULL) {
814                 debug_error ("invalid parameter. error code is null");
815                 return false;
816         }
817         *error_code = ERR_ASM_ERROR_NONE;
818
819         if (sound_event < ASM_EVENT_MEDIA_MMPLAYER || sound_event >= ASM_EVENT_MAX) {
820                 *error_code = ERR_ASM_EVENT_IS_INVALID;
821                 debug_error ("invalid sound event(%d)",sound_event);
822                 return false;
823         }
824
825         for (index = 0; index < ASM_HANDLE_MAX; index++) {
826                 if (ASM_sound_handle[index].is_used == false) {
827                         break;
828                 }
829         }
830
831         if (index == ASM_HANDLE_MAX) {
832                 *error_code = ERR_ASM_LOCAL_HANDLE_IS_FULL;
833                 debug_error ("local sound event is full(MAX)");
834                 return false;
835         }
836
837         if (!g_asm_thread) {
838                 GMainContext* asm_context = g_main_context_new ();
839                 g_asm_loop = g_main_loop_new (asm_context, FALSE);
840                 g_main_context_unref(asm_context);
841                 g_asm_thread = g_thread_new("ASM Thread", thread_func, NULL);
842                 if (g_asm_thread == NULL) {
843                         debug_error ("could not create thread..");
844                         g_main_loop_unref(g_asm_loop);
845                         return false;
846                 }
847         }
848
849         if (application_pid == -1) {
850                 asm_pid = asmgettid();
851         } else if (application_pid > 2) {
852                 asm_pid = application_pid;
853         } else {
854                 *error_code = ERR_ASM_INVALID_PARAMETER;
855                 debug_error ("invalid pid %d", application_pid);
856                 return false;
857         }
858
859         ASM_sound_handle[index].sound_event = sound_event;
860         ASM_sound_handle[index].asm_tid = asm_pid;
861
862         if (!__ASM_get_sound_state(&sound_status_value, error_code)) {
863                 debug_error("failed to __ASM_get_sound_state(), error(%d)", *error_code);
864                 return false;
865         }
866
867         debug_error(" <<<< Event(%s), Tid(%d), Index(%d), State(%s)",
868                                 ASM_sound_events_str[sound_event], ASM_sound_handle[index].asm_tid, index, ASM_sound_state_str[sound_state]);
869
870         handle = -1; /* for register & get handle from server */
871
872         if (func) {
873                 /* Construct msg to send -> send msg -> recv msg */
874                 if (!__asm_construct_snd_msg(asm_pid, handle, sound_event, ASM_REQUEST_REGISTER, sound_state, mm_resource, error_code)) {
875                         debug_error("failed to __asm_construct_snd_msg(), error(%d)", *error_code);
876                         return false;
877                 }
878                 func ((void*)&asm_snd_msg, (void*)&asm_rcv_msg);
879         } else  {
880                 conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &err);
881                 if (!conn && err) {
882                         debug_error ("g_bus_get_sync() error (%s) ", err->message);
883                         g_error_free (err);
884                         return false;
885                 }
886                 debug_error ("conn = %p", conn);
887
888 #ifdef SUPPORT_CONTAINER
889 #ifdef USE_SECURITY
890                 client_variant = g_variant_new("(@ayiiiiii)", _get_cookie_variant(), asm_pid, handle, sound_event,
891                                                                                 ASM_REQUEST_REGISTER, sound_state, mm_resource);
892 #else /* USE_SECURITY */
893                 gethostname(container, sizeof(container));
894                 debug_error ("container = %s", container);
895                 client_variant = g_variant_new("(siiiiii)", container, asm_pid, handle, sound_event,
896                                                                                 ASM_REQUEST_REGISTER, sound_state, mm_resource);
897 #endif /* USE_SECURITY */
898
899 #else /* SUPPORT_CONTAINER */
900                 client_variant = g_variant_new("(iiiiii)", asm_pid, handle, sound_event,
901                                                                                 ASM_REQUEST_REGISTER, sound_state, mm_resource);
902 #endif /* SUPPORT_CONTAINER */
903
904                 res_variant = g_dbus_connection_call_sync(conn, ASM_BUS_NAME_SOUND_SERVER, ASM_OBJECT_SOUND_SERVER, ASM_INTERFACE_SOUND_SERVER,
905                                                                                 "ASMRegisterSound", client_variant, NULL, G_DBUS_CALL_FLAGS_NONE, -1, NULL,  &err);
906                 if (!res_variant && err) {
907                         debug_error("g_dbus_connection_call_sync fail(%s)", err->message);
908                         *error_code = ERR_ASM_GDBUS_CONNECTION_ERROR;
909                         g_error_free (err);
910                         return false;
911                 } else {
912                         g_variant_get(res_variant ,"(iiiiii)",
913                                         &(asm_rcv_msg.instance_id), &(asm_rcv_msg.data.alloc_handle), &(asm_rcv_msg.data.cmd_handle),
914                                         &(asm_rcv_msg.data.source_request_id), &(asm_rcv_msg.data.result_sound_command),
915                                         &(asm_rcv_msg.data.result_sound_state));
916                 }
917         }
918
919         handle = asm_rcv_msg.data.alloc_handle; /* get handle from server */
920         if (handle == -1) {
921                 debug_error("failed to create handle from server");
922                 *error_code = ERR_ASM_SERVER_HANDLE_IS_FULL;
923                 return false;
924         }
925
926         ASM_sound_handle[index].handle = handle;
927
928         __ASM_init_callback(index, ASM_sound_handle[index].is_for_watching);
929
930 /********************************************************************************************************/
931         switch (asm_rcv_msg.data.result_sound_command) {
932         case ASM_COMMAND_PAUSE:
933         case ASM_COMMAND_STOP:
934                 debug_msg( " <<<<<<<<<<<<<<<< Received command : %d (STOP(3)/PAUSE(4)) >>>>>>>>>>>>>>>>>>>>\n", asm_rcv_msg.data.result_sound_command);
935                 if (handle == asm_rcv_msg.data.cmd_handle) {
936
937                         __ASM_destroy_callback(index);
938
939                         ASM_sound_handle[index].asm_fd = 0;
940                         ASM_sound_handle[index].asm_tid = 0;
941                         ASM_sound_handle[index].sound_event = ASM_EVENT_NONE;
942                         ASM_sound_handle[index].is_used = false;
943                         if (asm_rcv_msg.data.result_sound_command == ASM_COMMAND_PAUSE) {
944                                 ASM_sound_handle[index].sound_state = ASM_STATE_PAUSE;
945                         } else if (asm_rcv_msg.data.result_sound_command == ASM_COMMAND_STOP) {
946                                 ASM_sound_handle[index].sound_state = ASM_STATE_STOP;
947                         }
948
949                         if (asm_rcv_msg.data.error_code){
950                                 *error_code = asm_rcv_msg.data.error_code;
951                                 debug_warning("error code: %x",*error_code);
952                         }
953                         return false;
954
955                 } else {
956                         int action_index = 0;
957                         unsigned int rcv_sound_status_value = 0;
958
959                         if (!__ASM_get_sound_state(&rcv_sound_status_value, error_code)) {
960                                 debug_error("failed to __ASM_get_sound_state(), error(%d)", *error_code);
961                         }
962
963                         debug_msg("Callback : TID(%ld), handle(%d), command(%d)",
964                                         asm_rcv_msg.instance_id, asm_rcv_msg.data.cmd_handle, asm_rcv_msg.data.result_sound_command);
965                         action_index = __ASM_find_index_by_handle(asm_rcv_msg.data.cmd_handle);
966                         if (action_index == -1) {
967                                 debug_error("Can not find index of instance %ld, handle %d", asm_rcv_msg.instance_id, asm_rcv_msg.data.cmd_handle);
968                         } else {
969                                 if (ASM_sound_handle[action_index].asm_callback != NULL) {
970                                         ASM_sound_handle[action_index].asm_callback(asm_rcv_msg.data.cmd_handle, ASM_sound_handle[action_index].sound_event,
971                                                                                                                                 asm_rcv_msg.data.result_sound_command, rcv_sound_status_value,
972                                                                                                                                 ASM_sound_handle[action_index].user_data);
973                                 } else {
974                                         debug_msg("null callback");
975                                 }
976                         }
977                 }
978                 break;
979
980         case ASM_COMMAND_PLAY:
981         case ASM_COMMAND_NONE:
982         case ASM_COMMAND_RESUME:
983                 ASM_sound_handle[index].sound_state = sound_state;
984                 break;
985         default:
986                 break;
987         }
988 /********************************************************************************************************/
989
990
991         ASM_sound_handle[index].asm_callback = callback;
992         ASM_sound_handle[index].user_data = user_data;
993         ASM_sound_handle[index].is_used = true;
994
995         debug_error(" >>>> Event(%s), Handle(%d), CBFuncPtr(%p)", ASM_sound_events_str[sound_event], handle, callback);
996         /* Add [out] param, asm_handle */
997         *asm_handle = handle;
998
999         debug_fleave();
1000
1001         return true;
1002
1003 }
1004
1005 EXPORT_API
1006 bool ASM_register_sound (const int application_pid, int *asm_handle, ASM_sound_events_t sound_event, ASM_sound_states_t sound_state,
1007                                                 ASM_sound_cb_t callback, void *user_data, ASM_resource_t mm_resource, int *error_code)
1008 {
1009         return ASM_register_sound_ex (application_pid, asm_handle, sound_event, sound_state, callback, user_data, mm_resource, error_code, NULL);
1010 }
1011
1012
1013 EXPORT_API
1014 bool ASM_change_callback(const int asm_handle, ASM_sound_events_t sound_event, ASM_sound_cb_t callback, void *user_data, int *error_code)
1015 {
1016         int handle=0;
1017
1018         if (error_code==NULL) {
1019                 debug_error ("invalid parameter. error code is null");
1020                 return false;
1021         }
1022         *error_code = ERR_ASM_ERROR_NONE;
1023
1024         if (sound_event < ASM_EVENT_MEDIA_MMPLAYER || sound_event >= ASM_EVENT_MAX) {
1025                 *error_code = ERR_ASM_EVENT_IS_INVALID;
1026                 debug_error ("invalid sound event(%d)",sound_event);
1027                 return false;
1028         }
1029
1030         int asm_index = -1;
1031
1032         if (asm_handle < 0 || asm_handle >= ASM_SERVER_HANDLE_MAX) {
1033                 *error_code = ERR_ASM_LOCAL_HANDLE_IS_INVALID;
1034                 debug_error("invalid handle(%d). callback is not registered", asm_handle);
1035                 return false;
1036         }
1037
1038         handle = asm_handle;
1039
1040         asm_index = __ASM_find_index_by_handle(handle);
1041         if (asm_index == -1) {
1042                 *error_code = ERR_ASM_LOCAL_HANDLE_IS_INVALID;
1043                 debug_error("Can not find index for handle %d", handle);
1044                 return false;
1045         }
1046
1047         debug_msg("callback function has changed to %p", callback);
1048         ASM_sound_handle[asm_index].asm_callback = callback;
1049         ASM_sound_handle[asm_index].user_data = user_data;
1050
1051         return true;
1052 }
1053
1054 EXPORT_API
1055 bool ASM_unregister_sound_ex(const int asm_handle, ASM_sound_events_t sound_event, int *error_code, int (*func)(void*,void*))
1056 {
1057         int handle=0;
1058         int asm_index = -1;
1059         GError *err = NULL;
1060         GDBusConnection *conn = NULL;
1061         GVariant *client_variant = NULL;
1062
1063         debug_fenter();
1064
1065         if (error_code == NULL) {
1066                 debug_error ("invalid parameter. error code is null");
1067                 return false;
1068         }
1069         *error_code = ERR_ASM_ERROR_NONE;
1070
1071         if (sound_event < ASM_EVENT_MEDIA_MMPLAYER || sound_event >= ASM_EVENT_MAX) {
1072                 *error_code = ERR_ASM_EVENT_IS_INVALID;
1073                 debug_error ("invalid sound event(%d)",sound_event);
1074                 return false;
1075         }
1076
1077         if (asm_handle < 0 || asm_handle >= ASM_SERVER_HANDLE_MAX) {
1078                 *error_code = ERR_ASM_LOCAL_HANDLE_IS_INVALID;
1079                 debug_error("invalid handle(%d). callback is not registered", asm_handle);
1080                 return false;
1081         }
1082
1083         handle = asm_handle;
1084         asm_index = __ASM_find_index_by_handle(handle);
1085         if (asm_index == -1) {
1086                 *error_code = ERR_ASM_LOCAL_HANDLE_IS_INVALID;
1087                 debug_error("Can not find index for handle(%d)", handle);
1088                 return false;
1089         }
1090         debug_msg("<<<< Event(%s), Tid(%d), Handle(%d) Index(%d)",
1091                                 ASM_sound_events_str[sound_event],
1092                                 ASM_sound_handle[asm_index].asm_tid,
1093                                 ASM_sound_handle[asm_index].handle,
1094                                 asm_index);
1095
1096         if (ASM_sound_handle[asm_index].asm_lock) {
1097                 if (!g_mutex_trylock(ASM_sound_handle[asm_index].asm_lock)) {
1098                         debug_warning("maybe asm_callback is being called, try one more time..");
1099                         usleep(2500000); // 2.5 sec
1100                         if (g_mutex_trylock(ASM_sound_handle[asm_index].asm_lock)) {
1101                                 debug_msg("finally got asm_lock");
1102                         }
1103                 }
1104         }
1105
1106         if (func) {
1107                 /* Construct msg to send -> send msg -> recv msg */
1108                 if (!__asm_construct_snd_msg(ASM_sound_handle[asm_index].asm_tid, handle, sound_event,
1109                                                                         ASM_REQUEST_UNREGISTER, ASM_STATE_NONE, ASM_RESOURCE_NONE, error_code)) {
1110                         debug_error("failed to __asm_construct_snd_msg(), error(%d)", *error_code);
1111                         return false;
1112                 }
1113                 func(&asm_snd_msg, &asm_rcv_msg);
1114         } else  {
1115                 conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &err);
1116                 if (!conn && err) {
1117                         debug_error ("g_bus_get_sync() error (%s) ", err->message);
1118                         g_error_free (err);
1119                         return false;
1120                 }
1121
1122                 client_variant = g_variant_new("(iiiiii)",ASM_sound_handle[asm_index].asm_tid, handle, sound_event,
1123                                                                 ASM_REQUEST_UNREGISTER, ASM_STATE_NONE, ASM_RESOURCE_NONE);
1124                 g_dbus_connection_call_sync(conn, ASM_BUS_NAME_SOUND_SERVER, ASM_OBJECT_SOUND_SERVER, ASM_INTERFACE_SOUND_SERVER,
1125                                                                 "ASMUnregisterSound", client_variant, NULL, G_DBUS_CALL_FLAGS_NONE, -1, NULL,  &err);
1126
1127                 if(err) {
1128                         debug_error("g_dbus_connection_call_sync fail(%s)", err->message);
1129                         *error_code = ERR_ASM_GDBUS_CONNECTION_ERROR;
1130                         g_error_free (err);
1131                         return false;
1132                 }
1133         }
1134
1135         if (ASM_sound_handle[asm_index].asm_lock) {
1136                 g_mutex_unlock(ASM_sound_handle[asm_index].asm_lock);
1137         }
1138
1139         __ASM_destroy_callback(asm_index);
1140
1141         ASM_sound_handle[asm_index].asm_fd = 0;
1142         ASM_sound_handle[asm_index].asm_tid = 0;
1143         ASM_sound_handle[asm_index].handle = 0;
1144         ASM_sound_handle[asm_index].sound_event = ASM_EVENT_NONE;
1145         ASM_sound_handle[asm_index].sound_state = ASM_STATE_NONE;
1146         ASM_sound_handle[asm_index].is_used = false;
1147
1148         debug_fleave();
1149
1150         return true;
1151 }
1152
1153 EXPORT_API
1154 bool ASM_unregister_sound(const int asm_handle, ASM_sound_events_t sound_event, int *error_code)
1155 {
1156         return ASM_unregister_sound_ex (asm_handle, sound_event, error_code, NULL);
1157 }
1158
1159 EXPORT_API
1160 bool ASM_set_watch_session (const int application_pid,  ASM_sound_events_t interest_sound_event,
1161                             ASM_sound_states_t interest_sound_state, ASM_watch_cb_t callback, void *user_data, int *error_code)
1162 {
1163         unsigned int sound_status_value;
1164         int handle = 0;
1165         int asm_pid = 0;
1166         int index = 0;
1167         GError *err = NULL;
1168         GDBusConnection *conn = NULL;
1169         GVariant *res_variant = NULL;
1170         GVariant *client_variant = NULL;
1171 #ifdef SUPPORT_CONTAINER
1172         char container[128];
1173 #endif
1174
1175         debug_fenter();
1176
1177         if (error_code==NULL) {
1178                 debug_error ("invalid parameter. error code is null");
1179                 return false;
1180         }
1181         *error_code = ERR_ASM_ERROR_NONE;
1182
1183         if (interest_sound_event < ASM_EVENT_MEDIA_MMPLAYER || interest_sound_event >= ASM_EVENT_MAX) {
1184                 *error_code = ERR_ASM_EVENT_IS_INVALID;
1185                 debug_error ("invalid sound event(%d)", interest_sound_event);
1186                 return false;
1187         }
1188
1189         if (!__ASM_is_supported_session_for_watching(interest_sound_event, interest_sound_state)) {
1190                 debug_error("not supported sound_event(%d) or sound_state(%d)", interest_sound_event, interest_sound_state);
1191                 *error_code = ERR_ASM_WATCH_NOT_SUPPORTED;
1192                 return false;
1193         }
1194
1195         if (__ASM_is_existed_request_for_watching(interest_sound_event, interest_sound_state, &index))
1196         {
1197                 debug_warning("already requested interest-session, do not send request message");
1198                 *error_code = ERR_ASM_WATCH_ALREADY_REQUESTED;
1199                 return false;
1200         }
1201
1202         for (index = 0; index < ASM_HANDLE_MAX; index++) {
1203                 if (ASM_sound_handle[index].is_used == false) {
1204                         break;
1205                 }
1206         }
1207
1208         if (index == ASM_HANDLE_MAX) {
1209                 *error_code = ERR_ASM_LOCAL_HANDLE_IS_FULL;
1210                 debug_error ("local sound event is full(MAX)");
1211                 return false;
1212         }
1213
1214         if (application_pid == -1) {
1215                 asm_pid = asmgettid();
1216         } else if (application_pid > 2) {
1217                 asm_pid = application_pid;
1218         } else {
1219                 *error_code = ERR_ASM_INVALID_PARAMETER;
1220                 debug_error ("invalid pid %d", application_pid);
1221                 return false;
1222         }
1223
1224         ASM_sound_handle[index].asm_tid = asm_pid;
1225         ASM_sound_handle[index].sound_event = interest_sound_event;
1226         ASM_sound_handle[index].sound_state = interest_sound_state;
1227
1228         debug_msg(" <<<< Interest event(%s), state(%s), Tid(%d), Index(%d)",
1229                                 ASM_sound_events_str[interest_sound_event],
1230                                 ASM_sound_state_str[interest_sound_state],
1231                                 ASM_sound_handle[index].asm_tid, index);
1232
1233         if (!__ASM_get_sound_state(&sound_status_value, error_code)) {
1234                 debug_error("failed to __ASM_get_sound_state(), error(%d)", *error_code);
1235                 return false;
1236         }
1237
1238         handle = -1; /* for register & get handle from server */
1239         conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &err);
1240         if (!conn && err) {
1241                 debug_error ("g_bus_get_sync() error (%s) ", err->message);
1242                 g_error_free (err);
1243                 return false;
1244         }
1245
1246 #ifdef SUPPORT_CONTAINER
1247 #ifdef USE_SECURITY
1248         client_variant = g_variant_new("(@ayiiiiii)", _get_cookie_variant(), asm_pid, handle, interest_sound_event,
1249                                                                         ASM_REQUEST_REGISTER_WATCHER, interest_sound_state, ASM_RESOURCE_NONE);
1250 #else /* USE_SECURITY */
1251         gethostname(container, sizeof(container));
1252         debug_error ("container = %s", container);
1253         client_variant = g_variant_new("(siiiiii)", container, asm_pid, handle, interest_sound_event,
1254                                                                         ASM_REQUEST_REGISTER_WATCHER, interest_sound_state, ASM_RESOURCE_NONE);
1255 #endif /* USE_SECURITY */
1256
1257 #else /* SUPPORT_CONTAINER */
1258         client_variant = g_variant_new("(iiiiii)", asm_pid, handle, interest_sound_event,
1259                                                                         ASM_REQUEST_REGISTER_WATCHER, interest_sound_state, ASM_RESOURCE_NONE);
1260 #endif /* SUPPORT_CONTAINER */
1261
1262         res_variant = g_dbus_connection_call_sync(conn, ASM_BUS_NAME_SOUND_SERVER, ASM_OBJECT_SOUND_SERVER, ASM_INTERFACE_SOUND_SERVER,
1263                                                                         "ASMRegisterWatcher", client_variant, NULL, G_DBUS_CALL_FLAGS_NONE, -1, NULL,  &err);
1264
1265         if(!res_variant && err) {
1266                 debug_error("g_dbus_connection_call_sync fail(%s)", err->message);
1267                 *error_code = ERR_ASM_GDBUS_CONNECTION_ERROR;
1268                 g_error_free (err);
1269                 return false;
1270         } else {        
1271                 g_variant_get(res_variant ,"(iiiiii)", &(asm_rcv_msg.instance_id), &(asm_rcv_msg.data.alloc_handle), &(asm_rcv_msg.data.cmd_handle),
1272                                         &(asm_rcv_msg.data.source_request_id), &(asm_rcv_msg.data.result_sound_command), &(asm_rcv_msg.data.result_sound_state));
1273         }
1274
1275         handle = asm_rcv_msg.data.alloc_handle; /* get handle from server */
1276         if (handle == -1) {
1277                 debug_error("failed to create handle from server");
1278                 *error_code = ERR_ASM_SERVER_HANDLE_IS_FULL;
1279                 return false;
1280         }
1281
1282         ASM_sound_handle[index].handle = handle;
1283         ASM_sound_handle[index].watch_callback = callback;
1284         ASM_sound_handle[index].user_data = user_data;
1285         ASM_sound_handle[index].is_used = true;
1286         ASM_sound_handle[index].is_for_watching = true;
1287
1288         __ASM_init_callback(index, ASM_sound_handle[index].is_for_watching);
1289
1290         /********************************************************************************************************/
1291         switch (asm_rcv_msg.data.result_sound_command) {
1292         case ASM_COMMAND_PLAY:
1293                 debug_msg(" >>>> added to watch list successfully");
1294                 break;
1295
1296         default:
1297                 debug_error("received message is abnormal..result_sound_command(%d) from ASM server", asm_rcv_msg.data.result_sound_command);
1298                 *error_code = ERR_ASM_GDBUS_CONNECTION_ERROR;
1299                 return false;
1300         }
1301         /********************************************************************************************************/
1302
1303         debug_fleave();
1304
1305         return true;
1306 }
1307
1308 EXPORT_API
1309 bool ASM_unset_watch_session (ASM_sound_events_t interest_sound_event, ASM_sound_states_t interest_sound_state, int *error_code)
1310 {
1311         unsigned int sound_status_value;
1312         int handle = 0;
1313         int asm_pid = 0;
1314         int index = 0;
1315         GError *err = NULL;
1316         GDBusConnection *conn = NULL;
1317         GVariant *client_variant = NULL;
1318
1319         debug_fenter();
1320
1321         if (error_code==NULL) {
1322                 debug_error ("invalid parameter. error code is null");
1323                 return false;
1324         }
1325         *error_code = ERR_ASM_ERROR_NONE;
1326
1327         if (interest_sound_event < ASM_EVENT_MEDIA_MMPLAYER || interest_sound_event >= ASM_EVENT_MAX) {
1328                 *error_code = ERR_ASM_EVENT_IS_INVALID;
1329                 debug_error ("invalid sound event(%d)",interest_sound_event);
1330                 return false;
1331         }
1332
1333         if (!__ASM_is_supported_session_for_watching(interest_sound_event, interest_sound_state)) {
1334                 debug_error("not supported sound_event(%d) or sound_state(%d)", interest_sound_event, interest_sound_state);
1335                 *error_code = ERR_ASM_WATCH_NOT_SUPPORTED;
1336                 return false;
1337         }
1338
1339         if (!__ASM_is_existed_request_for_watching(interest_sound_event, interest_sound_state, &index))
1340         {
1341                 debug_warning("already unrequested interest-session or have not been requested it before, do not send request message");
1342                 *error_code = ERR_ASM_WATCH_ALREADY_UNREQUESTED;
1343                 return false;
1344         }
1345
1346         debug_msg(" <<<< Unregister interest event(%s), state(%s), Tid(%d), Index(%d)",
1347                                 ASM_sound_events_str[ASM_sound_handle[index].sound_event],
1348                                 ASM_sound_state_str[ASM_sound_handle[index].sound_state],
1349                                 ASM_sound_handle[index].asm_tid, index);
1350
1351         if (!__ASM_get_sound_state(&sound_status_value, error_code)) {
1352                 debug_error("failed to __ASM_get_sound_state(), error(%d)", *error_code);
1353                 return false;
1354         }
1355
1356         handle = ASM_sound_handle[index].handle;
1357         asm_pid = ASM_sound_handle[index].asm_tid;
1358
1359         if (ASM_sound_handle[index].asm_lock) {
1360                 g_mutex_lock(ASM_sound_handle[index].asm_lock);
1361         }
1362
1363         conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &err);
1364         if (!conn && err) {
1365                 debug_error ("g_bus_get_sync() error (%s) ", err->message);
1366                 g_error_free (err);
1367                 return false;
1368         }
1369
1370         client_variant = g_variant_new("(iiiiii)", asm_pid, handle, interest_sound_event,
1371                                                                 ASM_REQUEST_UNREGISTER_WATCHER, interest_sound_state, ASM_RESOURCE_NONE);
1372         g_dbus_connection_call_sync(conn, ASM_BUS_NAME_SOUND_SERVER, ASM_OBJECT_SOUND_SERVER, ASM_INTERFACE_SOUND_SERVER,
1373                                                                 "ASMUnregisterWatcher", client_variant, NULL, G_DBUS_CALL_FLAGS_NONE, -1, NULL,  &err);
1374
1375         if(err) {
1376                 debug_error("g_dbus_connection_call_sync fail(%s)", err->message);
1377                 *error_code = ERR_ASM_GDBUS_CONNECTION_ERROR;
1378                 g_error_free (err);
1379                 return false;
1380         }
1381
1382         if (ASM_sound_handle[index].asm_lock) {
1383                 g_mutex_unlock(ASM_sound_handle[index].asm_lock);
1384         }
1385
1386         __ASM_destroy_callback(index);
1387
1388         ASM_sound_handle[index].asm_tid = 0;
1389         ASM_sound_handle[index].handle = 0;
1390         ASM_sound_handle[index].sound_event = ASM_EVENT_NONE;
1391         ASM_sound_handle[index].sound_state = ASM_STATE_NONE;
1392         ASM_sound_handle[index].is_used = false;
1393         ASM_sound_handle[index].is_for_watching = false;
1394
1395         debug_msg(" >>>> send requesting message successfully");
1396
1397         debug_fleave();
1398
1399         return true;
1400 }
1401
1402 EXPORT_API
1403 bool ASM_get_sound_status(unsigned int *all_sound_status, int *error_code)
1404 {
1405         if (all_sound_status == NULL || error_code == NULL) {
1406                 if (error_code)
1407                         *error_code = ERR_ASM_INVALID_PARAMETER;
1408                 debug_error("invalid parameter");
1409                 return false;
1410         }
1411
1412         debug_msg("Tid(%ld)", asmgettid());
1413
1414         if (!__ASM_get_sound_state(all_sound_status, error_code)) {
1415                 debug_error("failed to __ASM_get_sound_state(), error(%d)", *error_code);
1416                 return false;
1417         }
1418
1419         return true;
1420 }
1421
1422 EXPORT_API
1423 bool ASM_get_process_session_state(const int asm_handle, ASM_sound_states_t *sound_state, int *error_code)
1424 {
1425         int handle = 0;
1426         int asm_index = 0;
1427         GError *err = NULL;
1428         GDBusConnection *conn = NULL;
1429         GVariant *res_variant = NULL;
1430         GVariant *client_variant = NULL;
1431
1432         if (sound_state == NULL || error_code == NULL) {
1433                 if (error_code)
1434                         *error_code = ERR_ASM_INVALID_PARAMETER;
1435                 debug_error("invalid parameter");
1436                 return false;
1437         }
1438
1439         handle = asm_handle;
1440         asm_index = __ASM_find_index_by_handle(handle);
1441         if (asm_index == -1) {
1442                 debug_error("Can not find index of %d", handle);
1443                 return false;
1444         }
1445
1446
1447         debug_msg("Pid(%d)", ASM_sound_handle[asm_index].asm_tid);
1448
1449         conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &err);
1450         if (!conn && err) {
1451                 debug_error ("g_bus_get_sync() error (%s) ", err->message);
1452                 g_error_free (err);
1453                 return false;
1454         }
1455
1456         client_variant = g_variant_new("(iiiiii)", ASM_sound_handle[asm_index].asm_tid, handle, ASM_EVENT_MONITOR,
1457                                                                         ASM_REQUEST_GETMYSTATE, ASM_STATE_NONE, ASM_RESOURCE_NONE);
1458         res_variant = g_dbus_connection_call_sync(conn, ASM_BUS_NAME_SOUND_SERVER, ASM_OBJECT_SOUND_SERVER, ASM_INTERFACE_SOUND_SERVER,
1459                                                                         "ASMGetMyState", client_variant, NULL, G_DBUS_CALL_FLAGS_NONE, -1, NULL, &err);
1460
1461         if(!res_variant && err) {
1462                 debug_error("g_dbus_connection_call_sync fail(%s)", err->message);
1463                 *error_code = ERR_ASM_GDBUS_CONNECTION_ERROR;
1464                 g_error_free (err);
1465                 return false;
1466         } else {
1467                 g_variant_get(res_variant ,"(iiiii)", &(asm_rcv_msg.instance_id), &(asm_rcv_msg.data.alloc_handle),
1468                                 &(asm_rcv_msg.data.cmd_handle), &(asm_rcv_msg.data.source_request_id), &(asm_rcv_msg.data.result_sound_state));
1469         }
1470
1471         *sound_state = asm_rcv_msg.data.result_sound_state;
1472
1473         debug_msg(">>>> Pid(%d), State(%s)", ASM_sound_handle[asm_index].asm_tid, ASM_sound_state_str[*sound_state]);
1474
1475         return true;
1476 }
1477
1478 EXPORT_API
1479 bool ASM_attach_callback(ASM_sound_events_t sound_event, ASM_sound_cb_t callback, void *user_data, int *error_code)
1480 {
1481         int asm_index = 0;
1482
1483         if (callback == NULL || error_code == NULL) {
1484                 if (error_code)
1485                         *error_code = ERR_ASM_INVALID_PARAMETER;
1486                 debug_error("invalid parameter");
1487                 return false;
1488         }
1489
1490         asm_index = __ASM_find_index_by_event(sound_event, asmgettid());
1491         if (asm_index == -1) {
1492                 debug_error("Could not find index of the event(%d)", sound_event);
1493                 return false;
1494         }
1495
1496         if (!ASM_sound_handle[asm_index].asm_callback) {
1497                 ASM_sound_handle[asm_index].asm_callback = callback;
1498                 ASM_sound_handle[asm_index].user_data = user_data;
1499         } else {
1500                 if (error_code)
1501                         *error_code = ERR_ASM_ALREADY_REGISTERED;
1502                 debug_error("asm_callback was already registered(0x%x)", ASM_sound_handle[asm_index].asm_callback);
1503                 return false;
1504         }
1505
1506         debug_msg(">>>> Pid(%d), Handle(%d), Event(%s), Callback(0x%x)", ASM_sound_handle[asm_index].asm_tid, ASM_sound_handle[asm_index].handle,
1507                 ASM_sound_events_str[ASM_sound_handle[asm_index].sound_event], ASM_sound_handle[asm_index].asm_callback);
1508
1509         return true;
1510 }
1511
1512 EXPORT_API
1513 bool ASM_get_sound_state(const int asm_handle, ASM_sound_events_t sound_event, ASM_sound_states_t *sound_state, int *error_code)
1514 {
1515         int handle = 0;
1516         int asm_index = 0;
1517         GError *err = NULL;
1518         GDBusConnection *conn = NULL;
1519         GVariant *res_variant = NULL;
1520         GVariant *client_variant = NULL;
1521
1522         if (sound_state == NULL || error_code == NULL) {
1523                 if (error_code)
1524                         *error_code = ERR_ASM_UNKNOWN_ERROR;
1525                 debug_error("invalid parameter");
1526                 return false;
1527         }
1528         if (sound_event < ASM_EVENT_MEDIA_MMPLAYER || sound_event >= ASM_EVENT_MAX) {
1529                 *error_code = ERR_ASM_EVENT_IS_INVALID;
1530                 debug_error("invalid sound event(%d)",sound_event);
1531                 return false;
1532         }
1533         handle = asm_handle;
1534
1535         asm_index = __ASM_find_index_by_handle(handle);
1536         if (asm_index == -1) {
1537                 debug_error("Can not find index of %d", handle);
1538                 return false;
1539         }
1540         debug_msg("<<<< Event(%s), Tid(%d), handle(%d)",
1541                         ASM_sound_events_str[sound_event], ASM_sound_handle[asm_index].asm_tid, ASM_sound_handle[asm_index].handle);
1542
1543         conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &err);
1544         if (!conn && err) {
1545                 debug_error ("g_bus_get_sync() error (%s) ", err->message);
1546                 g_error_free (err);
1547                 return false;
1548         }
1549
1550         client_variant = g_variant_new("(iiiiii)", ASM_sound_handle[asm_index].asm_tid, handle, sound_event,
1551                                                                 ASM_REQUEST_GETSTATE, ASM_STATE_NONE, ASM_RESOURCE_NONE);
1552         res_variant = g_dbus_connection_call_sync(conn, ASM_BUS_NAME_SOUND_SERVER, ASM_OBJECT_SOUND_SERVER, ASM_INTERFACE_SOUND_SERVER,
1553                                                                 "ASMGetState", client_variant, NULL, G_DBUS_CALL_FLAGS_NONE, -1, NULL, &err);
1554
1555         if(!res_variant && err) {
1556                 debug_error("g_dbus_connection_call_sync fail(%s)", err->message);
1557                 *error_code = ERR_ASM_GDBUS_CONNECTION_ERROR;
1558                 g_error_free (err);
1559                 return false;
1560         } else {        
1561                 g_variant_get(res_variant ,"(iiiii)", &(asm_rcv_msg.instance_id), &(asm_rcv_msg.data.alloc_handle), &(asm_rcv_msg.data.cmd_handle), &(asm_rcv_msg.data.source_request_id), &(asm_rcv_msg.data.result_sound_state));
1562         }
1563
1564         *sound_state = asm_rcv_msg.data.result_sound_state;
1565
1566         debug_msg(">>>> Event(%s), State(%s)", ASM_sound_events_str[sound_event], ASM_sound_state_str[*sound_state]);
1567
1568         return true;
1569 }
1570
1571 EXPORT_API
1572 bool ASM_set_sound_state_ex (const int asm_handle, ASM_sound_events_t sound_event, ASM_sound_states_t sound_state, ASM_resource_t mm_resource, int *error_code, int (*func)(void*,void*))
1573 {
1574         int handle = 0;
1575         int asm_index = 0;
1576         GError *err = NULL;
1577         GDBusConnection *conn = NULL;
1578         GVariant *res_variant = NULL;
1579         GVariant *client_variant = NULL;
1580
1581         debug_fenter();
1582
1583         if (error_code == NULL) {
1584                 debug_error("error_code is null");
1585                 return false;
1586         }
1587
1588         if (sound_event < 0 || sound_event > ASM_PRIORITY_MATRIX_MIN) {
1589                 debug_error("invalid sound event(%d)",sound_event);
1590                 *error_code = ERR_ASM_EVENT_IS_INVALID;
1591                 return false;
1592         }
1593
1594         if (asm_handle < 0 || asm_handle >= ASM_SERVER_HANDLE_MAX) {
1595                 *error_code = ERR_ASM_LOCAL_HANDLE_IS_INVALID;
1596                 debug_error("Invalid handle %d", asm_handle);
1597                 return false;
1598         }
1599
1600         handle = asm_handle;
1601
1602         asm_index = __ASM_find_index_by_handle(handle);
1603         if (asm_index == -1) {
1604                 debug_error("Can not find index of %d", handle);
1605                 return false;
1606         }
1607
1608         debug_msg("<<<< Event(%s), State(%s), Tid(%d), handle(%d)",
1609                                 ASM_sound_events_str[sound_event], ASM_sound_state_str[sound_state], ASM_sound_handle[asm_index].asm_tid, ASM_sound_handle[asm_index].handle);
1610
1611         if (func) {
1612                 /* Construct msg to send -> send msg -> recv msg */
1613                 if (!__asm_construct_snd_msg(ASM_sound_handle[asm_index].asm_tid, handle, sound_event, ASM_REQUEST_SETSTATE, sound_state, mm_resource, error_code)) {
1614                         debug_error("failed to __asm_construct_snd_msg(), error(%d)", *error_code);
1615                         return false;
1616                 }
1617                 debug_msg( "[func(%p) START]", func);
1618                 func (&asm_snd_msg, &asm_rcv_msg);
1619                 debug_msg( "[func END]");
1620         } else {
1621                 conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &err);
1622                 if (!conn && err) {
1623                         debug_error ("g_bus_get_sync() error (%s) ", err->message);
1624                         g_error_free (err);
1625                         return false;
1626                 }
1627
1628                 client_variant = g_variant_new("(iiiiii)", ASM_sound_handle[asm_index].asm_tid, handle, sound_event,
1629                                                                                 ASM_REQUEST_SETSTATE, sound_state, mm_resource);
1630                 res_variant = g_dbus_connection_call_sync(conn, ASM_BUS_NAME_SOUND_SERVER, ASM_OBJECT_SOUND_SERVER, ASM_INTERFACE_SOUND_SERVER,
1631                                                                                 "ASMSetState", client_variant, NULL, G_DBUS_CALL_FLAGS_NONE, -1, NULL, &err);
1632
1633                 if(!res_variant && err) {
1634                         debug_error("g_dbus_connection_call_sync fail(%s)", err->message);
1635                         *error_code = ERR_ASM_GDBUS_CONNECTION_ERROR;
1636                         g_error_free (err);
1637                         return false;
1638                 } else {
1639                         g_variant_get(res_variant ,"(iiiiiii)", &(asm_rcv_msg.instance_id), &(asm_rcv_msg.data.alloc_handle),
1640                                                                                                 &(asm_rcv_msg.data.cmd_handle), &(asm_rcv_msg.data.source_request_id),
1641                                                                                                 &(asm_rcv_msg.data.result_sound_command), &(asm_rcv_msg.data.result_sound_state),
1642                                                                                                 &(asm_rcv_msg.data.error_code));
1643                 }
1644         }
1645
1646         if (sound_state == ASM_STATE_PLAYING ) {
1647                 debug_log( "sound_state is PLAYING, func(0x%x)", func);
1648
1649                 switch (asm_rcv_msg.data.result_sound_command) {
1650                 case ASM_COMMAND_PAUSE:
1651                 case ASM_COMMAND_STOP:
1652                         debug_msg( " <<<<<<<<<<<<<<<< Received command : %d (STOP(3)/PAUSE(4)) >>>>>>>>>>>>>>>>>>>>\n", asm_rcv_msg.data.result_sound_command);
1653                         if (handle == asm_rcv_msg.data.cmd_handle) {
1654
1655                                 debug_msg("handle(%d) is same as asm_rcv_msg.data.cmd_handle", handle);
1656
1657                                 asm_index = __ASM_find_index_by_handle(asm_rcv_msg.data.cmd_handle);
1658                                 if (asm_index == -1) {
1659                                         *error_code = ERR_ASM_LOCAL_HANDLE_IS_INVALID;
1660                                         debug_error( "Can not find index from instance_id %ld, handle %d", asm_rcv_msg.instance_id, asm_rcv_msg.data.cmd_handle);
1661                                         return false;
1662                                 }
1663
1664                                 if (asm_rcv_msg.data.result_sound_command == ASM_COMMAND_PAUSE) {
1665                                         ASM_sound_handle[asm_index].sound_state = ASM_STATE_PAUSE;
1666                                 } else if (asm_rcv_msg.data.result_sound_command == ASM_COMMAND_STOP) {
1667                                         ASM_sound_handle[asm_index].sound_state = ASM_STATE_STOP;
1668                                 }
1669
1670                                 if (asm_rcv_msg.data.error_code){
1671                                         *error_code = asm_rcv_msg.data.error_code;
1672                                         debug_warning("error code: %x",*error_code);
1673                                 }
1674                                 return false;
1675
1676                         } else {
1677                                 unsigned int rcv_sound_status_value = 0;
1678                                 if (!__ASM_get_sound_state(&rcv_sound_status_value, error_code)) {
1679                                         debug_error("failed to __ASM_get_sound_state(), error(%d)", *error_code);
1680                                 }
1681
1682                                 debug_msg("[ASM_CB] Callback : TID(%ld), handle(%d)", asm_rcv_msg.instance_id, asm_rcv_msg.data.cmd_handle);
1683
1684                                 asm_index = __ASM_find_index_by_handle(asm_rcv_msg.data.cmd_handle);
1685                                 if (asm_index == -1) {
1686                                         *error_code = ERR_ASM_LOCAL_HANDLE_IS_INVALID;
1687                                         debug_error("Can not find index from instance_id %ld, handle %d", asm_rcv_msg.instance_id, asm_rcv_msg.data.cmd_handle);
1688                                         return false;
1689                                 }
1690
1691                                 if (ASM_sound_handle[asm_index].asm_callback!=NULL) {
1692                                         debug_msg( "[ASM_CB(%p) START]", ASM_sound_handle[asm_index].asm_callback);
1693                                         ASM_sound_handle[asm_index].asm_callback(asm_rcv_msg.data.cmd_handle, ASM_sound_handle[asm_index].sound_event,
1694                                                         asm_rcv_msg.data.result_sound_command, rcv_sound_status_value, ASM_sound_handle[asm_index].user_data);
1695                                         debug_msg( "[ASM_CB END]");
1696                                 } else {
1697                                         debug_msg("asm callback is null");
1698                                 }
1699                         }
1700                         break;
1701                 case ASM_COMMAND_PLAY:
1702                 case ASM_COMMAND_NONE:
1703                 case ASM_COMMAND_RESUME:
1704                         ASM_sound_handle[asm_index].sound_state = sound_state;
1705                         break;
1706                 default:
1707                         break;
1708                 }
1709
1710         }
1711
1712         debug_fleave();
1713
1714         return true;
1715 }
1716
1717 EXPORT_API
1718 bool ASM_set_sound_state (const int asm_handle, ASM_sound_events_t sound_event, ASM_sound_states_t sound_state, ASM_resource_t mm_resource, int *error_code)
1719 {
1720         return ASM_set_sound_state_ex (asm_handle, sound_event, sound_state, mm_resource, error_code, NULL);
1721 }
1722
1723 EXPORT_API
1724 bool ASM_set_subsession (const int asm_handle, ASM_sound_sub_sessions_t subsession, int resource, int *error_code)
1725 {
1726         int handle = 0;
1727         int asm_index = 0;
1728         GError *err = NULL;
1729         GDBusConnection *conn = NULL;
1730         GVariant *res_variant = NULL;
1731         GVariant *client_variant = NULL;
1732
1733         debug_fenter();
1734
1735         if (error_code == NULL) {
1736                 debug_error("error_code is null");
1737                 return false;
1738         }
1739
1740         if (asm_handle < 0 || asm_handle >= ASM_SERVER_HANDLE_MAX) {
1741                 *error_code = ERR_ASM_LOCAL_HANDLE_IS_INVALID;
1742                 debug_error("Invalid handle(%d)", asm_handle);
1743                 return false;
1744         }
1745
1746         if (subsession < ASM_SUB_SESSION_TYPE_VOICE || subsession >= ASM_SUB_SESSION_TYPE_MAX) {
1747                 *error_code = ERR_ASM_INVALID_PARAMETER;
1748                 debug_error("Invalid sub session type(%d)", subsession);
1749                 return false;
1750         }
1751
1752         handle = asm_handle;
1753
1754         asm_index = __ASM_find_index_by_handle(handle);
1755         if (asm_index == -1) {
1756                 debug_error("Can not find index of %d", handle);
1757                 return false;
1758         }
1759
1760         conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &err);
1761         if (!conn && err) {
1762                 debug_error ("g_bus_get_sync() error (%s) ", err->message);
1763                 g_error_free (err);
1764                 return false;
1765         }
1766
1767         client_variant = g_variant_new("(iiiiii)", ASM_sound_handle[asm_index].asm_tid, ASM_sound_handle[asm_index].handle, subsession,
1768                                                                 ASM_REQUEST_SET_SUBSESSION,  ASM_sound_handle[asm_index].sound_state, resource);
1769         res_variant = g_dbus_connection_call_sync(conn, ASM_BUS_NAME_SOUND_SERVER, ASM_OBJECT_SOUND_SERVER, ASM_INTERFACE_SOUND_SERVER,
1770                                                                 "ASMSetSubsession", client_variant, NULL, G_DBUS_CALL_FLAGS_NONE, -1, NULL, &err);
1771
1772         if(!res_variant && err) {
1773                 debug_error("g_dbus_connection_call_sync fail(%s)", err->message);
1774                 *error_code = ERR_ASM_GDBUS_CONNECTION_ERROR;
1775                 g_error_free (err);
1776                 return false;
1777         } else {
1778                 g_variant_get(res_variant ,"(iiii)", &(asm_rcv_msg.instance_id), &(asm_rcv_msg.data.alloc_handle), &(asm_rcv_msg.data.cmd_handle), &(asm_rcv_msg.data.source_request_id));
1779         }
1780
1781
1782
1783         /* TODO: Should check msg returned.....*/
1784 #if 0
1785         {
1786                 debug_msg( " <<<<<<<<<<<<<<<< [BEFORE] Callback : Main Context >>>>>>>>>>>>>>>>>>>> \n");
1787                 /********************************************************************************************************/
1788                 switch (asm_rcv_msg.data.result_sound_command) {
1789                 case ASM_COMMAND_PAUSE:
1790                 case ASM_COMMAND_STOP:
1791                 case ASM_COMMAND_PLAY:
1792                 case ASM_COMMAND_NONE:
1793                 case ASM_COMMAND_RESUME:
1794                 default:
1795                         break;
1796                 }
1797                 /********************************************************************************************************/
1798                 debug_msg(" <<<<<<<<<<<<<<<< [AFTER]  Callback : Main Context >>>>>>>>>>>>>>>>>>>> \n");
1799
1800         }
1801 #endif
1802
1803         debug_fleave();
1804
1805         return true;
1806 }
1807
1808 EXPORT_API
1809 bool ASM_get_subsession (const int asm_handle, ASM_sound_sub_sessions_t *subsession, int *error_code)
1810 {
1811         int handle = 0;
1812         int asm_index = 0;
1813         GError *err = NULL;
1814         GDBusConnection *conn = NULL;
1815         GVariant *res_variant = NULL;
1816         GVariant *client_variant = NULL;
1817
1818         debug_fenter();
1819
1820         if (error_code == NULL) {
1821                 debug_error("error_code is null");
1822                 return false;
1823         }
1824
1825         if (subsession == NULL) {
1826                 debug_error("subsession is null");
1827                 *error_code = ERR_ASM_INVALID_PARAMETER;
1828                 return false;
1829         }
1830
1831         if (asm_handle < 0 || asm_handle >= ASM_SERVER_HANDLE_MAX) {
1832                 *error_code = ERR_ASM_LOCAL_HANDLE_IS_INVALID;
1833                 debug_error("Invalid handle %d \n", asm_handle);
1834                 return false;
1835         }
1836
1837         handle = asm_handle;
1838
1839         asm_index = __ASM_find_index_by_handle(handle);
1840         if (asm_index == -1) {
1841                 debug_error("Can not find index of %d", handle);
1842                 return false;
1843         }
1844
1845         conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &err);
1846         if (!conn && err) {
1847                 debug_error ("g_bus_get_sync() error (%s) ", err->message);
1848                 g_error_free (err);
1849                 return false;
1850         }
1851
1852         client_variant = g_variant_new("(iiiiii)", ASM_sound_handle[asm_index].asm_tid, ASM_sound_handle[asm_index].handle, 0,
1853                                                                 ASM_REQUEST_GET_SUBSESSION, 0, 0);
1854         res_variant = g_dbus_connection_call_sync(conn, ASM_BUS_NAME_SOUND_SERVER, ASM_OBJECT_SOUND_SERVER, ASM_INTERFACE_SOUND_SERVER,
1855                                                         "ASMGetSubsession", client_variant, NULL, G_DBUS_CALL_FLAGS_NONE, -1, NULL, &err);
1856
1857         if(!res_variant && err) {
1858                 debug_error("g_dbus_connection_call_sync fail(%s)", err->message);
1859                 *error_code = ERR_ASM_GDBUS_CONNECTION_ERROR;
1860                 g_error_free (err);
1861                 return false;
1862         } else {
1863                 g_variant_get(res_variant ,"(iiiii)", &(asm_rcv_msg.instance_id), &(asm_rcv_msg.data.alloc_handle), &(asm_rcv_msg.data.cmd_handle), &(asm_rcv_msg.data.source_request_id), &(asm_rcv_msg.data.result_sound_command));
1864         }
1865
1866         *subsession = asm_rcv_msg.data.result_sound_command;
1867
1868         debug_msg(">>>> ASM_get_subsession with subsession value [%d]\n", *subsession);
1869         debug_fleave();
1870
1871         return true;
1872 }
1873
1874 EXPORT_API
1875 bool ASM_set_subevent (const int asm_handle, ASM_sound_sub_events_t subevent, int *error_code)
1876 {
1877         int handle = 0;
1878         int asm_index = 0;
1879         ASM_sound_states_t sound_state = ASM_STATE_NONE;
1880         GError *err = NULL;
1881         GDBusConnection *conn = NULL;
1882         GVariant *res_variant = NULL;
1883         GVariant *client_variant = NULL;
1884
1885         debug_fenter();
1886
1887         if (error_code == NULL) {
1888                 debug_error("error_code is null");
1889                 return false;
1890         }
1891
1892         if (asm_handle < 0 || asm_handle >= ASM_SERVER_HANDLE_MAX) {
1893                 *error_code = ERR_ASM_LOCAL_HANDLE_IS_INVALID;
1894                 debug_error("Invalid handle(%d)", asm_handle);
1895                 return false;
1896         }
1897
1898         if (subevent < ASM_SUB_EVENT_NONE || subevent >= ASM_SUB_EVENT_MAX) {
1899                 *error_code = ERR_ASM_INVALID_PARAMETER;
1900                 debug_error("Invalid sub event(%d)", subevent);
1901                 return false;
1902         }
1903
1904         handle = asm_handle;
1905
1906         asm_index = __ASM_find_index_by_handle(handle);
1907         if (asm_index == -1) {
1908                 debug_error("Can not find index of %d", handle);
1909                 return false;
1910         }
1911
1912         if (subevent == ASM_SUB_EVENT_NONE) {
1913                 sound_state = ASM_STATE_STOP;
1914         } else if (subevent < ASM_SUB_EVENT_MAX) {
1915                 sound_state = ASM_STATE_PLAYING;
1916         }
1917
1918
1919         conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &err);
1920         if (!conn && err) {
1921                 debug_error ("g_bus_get_sync() error (%s) ", err->message);
1922                 g_error_free (err);
1923                 return false;
1924         }
1925
1926         client_variant = g_variant_new("(iiiiii)", ASM_sound_handle[asm_index].asm_tid, ASM_sound_handle[asm_index].handle, subevent,
1927                                                                 ASM_REQUEST_SET_SUBEVENT, sound_state, 0);
1928         res_variant = g_dbus_connection_call_sync(conn, ASM_BUS_NAME_SOUND_SERVER, ASM_OBJECT_SOUND_SERVER, ASM_INTERFACE_SOUND_SERVER,
1929                                                                 "ASMSetSubevent", client_variant, NULL, G_DBUS_CALL_FLAGS_NONE, -1, NULL, &err);
1930
1931         if (!res_variant && err) {
1932                 debug_error("g_dbus_connection_call_sync fail(%s)", err->message);
1933                 *error_code = ERR_ASM_GDBUS_CONNECTION_ERROR;
1934                 g_error_free (err);
1935                 return false;
1936         } else {
1937                 g_variant_get(res_variant, "(iiiiii)", &(asm_rcv_msg.instance_id), &(asm_rcv_msg.data.alloc_handle), &(asm_rcv_msg.data.cmd_handle),
1938                                         &(asm_rcv_msg.data.source_request_id), &(asm_rcv_msg.data.result_sound_command), &(asm_rcv_msg.data.result_sound_state));
1939         }
1940
1941         switch (asm_rcv_msg.data.result_sound_command) {
1942         case ASM_COMMAND_PAUSE:
1943         case ASM_COMMAND_STOP:
1944                 debug_msg( " <<<<<<<<<<<<<<<< Received command : %d (STOP(3)/PAUSE(4)) >>>>>>>>>>>>>>>>>>>>\n", asm_rcv_msg.data.result_sound_command);
1945                 if (handle == asm_rcv_msg.data.cmd_handle) {
1946
1947                         debug_msg("handle(%d) is same as asm_rcv_msg.data.cmd_handle", handle);
1948
1949                         asm_index = __ASM_find_index_by_handle(asm_rcv_msg.data.cmd_handle);
1950                         if (asm_index == -1) {
1951                                 *error_code = ERR_ASM_LOCAL_HANDLE_IS_INVALID;
1952                                 debug_error( "Can not find index from instance_id %ld, handle %d", asm_rcv_msg.instance_id, asm_rcv_msg.data.cmd_handle);
1953                                 return false;
1954                         }
1955
1956                         if (asm_rcv_msg.data.result_sound_command == ASM_COMMAND_PAUSE) {
1957                                 ASM_sound_handle[asm_index].sound_state = ASM_STATE_PAUSE;
1958                         } else if (asm_rcv_msg.data.result_sound_command == ASM_COMMAND_STOP) {
1959                                 ASM_sound_handle[asm_index].sound_state = ASM_STATE_STOP;
1960                         }
1961
1962                         if (asm_rcv_msg.data.error_code){
1963                                 *error_code = asm_rcv_msg.data.error_code;
1964                                 debug_warning("error code: %x",*error_code);
1965                         }
1966                         return false;
1967
1968                 } else {
1969                         unsigned int rcv_sound_status_value = 0;
1970                         if (!__ASM_get_sound_state(&rcv_sound_status_value, error_code)) {
1971                                 debug_error("failed to __ASM_get_sound_state(), error(%d)", *error_code);
1972                         }
1973                         asm_index = __ASM_find_index_by_handle(asm_rcv_msg.data.cmd_handle);
1974                         if (asm_index == -1) {
1975                                 *error_code = ERR_ASM_LOCAL_HANDLE_IS_INVALID;
1976                                 debug_error("Can not find index from instance_id %ld, handle %d", asm_rcv_msg.instance_id, asm_rcv_msg.data.cmd_handle);
1977                                 return false;
1978                         }
1979
1980                         if (ASM_sound_handle[asm_index].asm_callback!=NULL) {
1981                                 debug_msg( "[ASM_CB(%p) START]", ASM_sound_handle[asm_index].asm_callback);
1982                                 ASM_sound_handle[asm_index].asm_callback(asm_rcv_msg.data.cmd_handle, ASM_sound_handle[asm_index].sound_event,
1983                                                         asm_rcv_msg.data.result_sound_command, rcv_sound_status_value, ASM_sound_handle[asm_index].user_data);
1984                                 debug_msg( "[ASM_CB END]");
1985                         } else {
1986                                 debug_msg("asm callback is null");
1987                         }
1988                 }
1989                 break;
1990         case ASM_COMMAND_PLAY:
1991         case ASM_COMMAND_NONE:
1992         case ASM_COMMAND_RESUME:
1993                 ASM_sound_handle[asm_index].sound_state = sound_state;
1994                 break;
1995         default:
1996                 break;
1997         }
1998
1999         debug_fleave();
2000
2001         return true;
2002 }
2003
2004 EXPORT_API
2005 bool ASM_get_subevent (const int asm_handle, ASM_sound_sub_events_t *subevent, int *error_code)
2006 {
2007         int handle = 0;
2008         int asm_index = 0;
2009         GError *err = NULL;
2010         GDBusConnection *conn = NULL;
2011         GVariant *res_variant = NULL;
2012         GVariant *client_variant = NULL;
2013
2014         debug_fenter();
2015
2016         if (error_code == NULL) {
2017                 debug_error("error_code is null");
2018                 return false;
2019         }
2020
2021         if (subevent == NULL) {
2022                 debug_error("subevent is null");
2023                 *error_code = ERR_ASM_INVALID_PARAMETER;
2024                 return false;
2025         }
2026
2027         if (asm_handle < 0 || asm_handle >= ASM_SERVER_HANDLE_MAX) {
2028                 *error_code = ERR_ASM_LOCAL_HANDLE_IS_INVALID;
2029                 debug_error("Invalid handle %d \n", asm_handle);
2030                 return false;
2031         }
2032
2033         handle = asm_handle;
2034
2035         asm_index = __ASM_find_index_by_handle(handle);
2036         if (asm_index == -1) {
2037                 debug_error("Can not find index of %d", handle);
2038                 return false;
2039         }
2040
2041         conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &err);
2042         if (!conn && err) {
2043                 debug_error ("g_bus_get_sync() error (%s) ", err->message);
2044                 g_error_free (err);
2045                 return false;
2046         }
2047
2048         client_variant = g_variant_new("(iiiiii)", ASM_sound_handle[asm_index].asm_tid, ASM_sound_handle[asm_index].handle, 0,
2049                                                                 ASM_REQUEST_GET_SUBEVENT, 0, 0);
2050         res_variant = g_dbus_connection_call_sync(conn, ASM_BUS_NAME_SOUND_SERVER, ASM_OBJECT_SOUND_SERVER, ASM_INTERFACE_SOUND_SERVER,
2051                                                                 "ASMGetSubevent", client_variant, NULL, G_DBUS_CALL_FLAGS_NONE, -1, NULL,  &err);
2052
2053         if(!res_variant && err) {
2054                 debug_error("g_dbus_connection_call_sync fail(%s)", err->message);
2055                 *error_code = ERR_ASM_GDBUS_CONNECTION_ERROR;
2056                 g_error_free (err);
2057                 return false;
2058         } else {
2059                 g_variant_get(res_variant, "(iiiii)", &(asm_rcv_msg.instance_id), &(asm_rcv_msg.data.alloc_handle), &(asm_rcv_msg.data.cmd_handle),
2060                                                 &(asm_rcv_msg.data.source_request_id), &(asm_rcv_msg.data.result_sound_command));
2061         }
2062
2063         *subevent = asm_rcv_msg.data.result_sound_command;
2064
2065         debug_msg(">>>> ASM_get_subevent with subevent value [%d]\n", *subevent);
2066         debug_fleave();
2067
2068         return true;
2069 }
2070
2071
2072 EXPORT_API
2073 bool ASM_set_session_option (const int asm_handle, int option_flags, int *error_code)
2074 {
2075         int handle = 0;
2076         int asm_index = 0;
2077         ASM_sound_states_t sound_state = ASM_STATE_NONE;
2078         GError *err = NULL;
2079         GDBusConnection *conn = NULL;
2080         GVariant *res_variant = NULL;
2081         GVariant *client_variant = NULL;
2082
2083         debug_fenter();
2084
2085         if (error_code == NULL) {
2086                 debug_error("error_code is null");
2087                 return false;
2088         }
2089
2090         if (asm_handle < 0 || asm_handle >= ASM_SERVER_HANDLE_MAX) {
2091                 *error_code = ERR_ASM_LOCAL_HANDLE_IS_INVALID;
2092                 debug_error("Invalid handle(%d)", asm_handle);
2093                 return false;
2094         }
2095
2096         if (option_flags < 0) {
2097                 *error_code = ERR_ASM_INVALID_PARAMETER;
2098                 debug_error("Invalid option_flags(%x)", option_flags);
2099                 return false;
2100         }
2101
2102         handle = asm_handle;
2103
2104         asm_index = __ASM_find_index_by_handle(handle);
2105         if (asm_index == -1) {
2106                 debug_error("Can not find index of %d", handle);
2107                 return false;
2108         }
2109
2110         conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &err);
2111         if (!conn && err) {
2112                 debug_error ("g_bus_get_sync() error (%s) ", err->message);
2113                 g_error_free (err);
2114                 return false;
2115         }
2116
2117     client_variant = g_variant_new("(iiiiii)",ASM_sound_handle[asm_index].asm_tid, ASM_sound_handle[asm_index].handle, option_flags,
2118                                                                         ASM_REQUEST_SET_SESSION_OPTIONS, sound_state, 0);
2119         res_variant = g_dbus_connection_call_sync(conn, ASM_BUS_NAME_SOUND_SERVER, ASM_OBJECT_SOUND_SERVER, ASM_INTERFACE_SOUND_SERVER,
2120                                                                 "ASMSetSessionOption", client_variant, NULL, G_DBUS_CALL_FLAGS_NONE, -1, NULL,  &err);
2121         if(!res_variant && err) {
2122                 debug_error("g_dbus_connection_call_sync fail(%s)", err->message);
2123                 *error_code = ERR_ASM_GDBUS_CONNECTION_ERROR;
2124                 g_error_free (err);
2125                 return false;
2126         } else {
2127                 g_variant_get(res_variant ,"(iiiiii)", &(asm_rcv_msg.instance_id), &(asm_rcv_msg.data.alloc_handle), &(asm_rcv_msg.data.cmd_handle),
2128                                                 &(asm_rcv_msg.data.source_request_id), &(asm_rcv_msg.data.result_sound_command), &(asm_rcv_msg.data.error_code));
2129         }
2130
2131
2132         switch (asm_rcv_msg.data.result_sound_command) {
2133         case ASM_COMMAND_PAUSE:
2134         case ASM_COMMAND_STOP:
2135                 debug_msg( " <<<<<<<<<<<<<<<< Received command : %d (STOP(3)/PAUSE(4)) >>>>>>>>>>>>>>>>>>>>\n", asm_rcv_msg.data.result_sound_command);
2136                 if (handle == asm_rcv_msg.data.cmd_handle) {
2137
2138                         debug_msg("handle(%d) is same as asm_rcv_msg.data.cmd_handle", handle);
2139
2140                         asm_index = __ASM_find_index_by_handle(asm_rcv_msg.data.cmd_handle);
2141                         if (asm_index == -1) {
2142                                 *error_code = ERR_ASM_LOCAL_HANDLE_IS_INVALID;
2143                                 debug_error( "Can not find index from instance_id %ld, handle %d", asm_rcv_msg.instance_id, asm_rcv_msg.data.cmd_handle);
2144                                 return false;
2145                         }
2146
2147                         if (asm_rcv_msg.data.result_sound_command == ASM_COMMAND_PAUSE) {
2148                                 ASM_sound_handle[asm_index].sound_state = ASM_STATE_PAUSE;
2149                         } else if (asm_rcv_msg.data.result_sound_command == ASM_COMMAND_STOP) {
2150                                 ASM_sound_handle[asm_index].sound_state = ASM_STATE_STOP;
2151                         }
2152
2153                         if (asm_rcv_msg.data.error_code){
2154                                 *error_code = asm_rcv_msg.data.error_code;
2155                                 debug_warning("error code: %x",*error_code);
2156                         }
2157                         return false;
2158
2159                 }
2160                 break;
2161         case ASM_COMMAND_PLAY:
2162                 ASM_sound_handle[asm_index].option_flags = option_flags;
2163                 break;
2164         default:
2165                 break;
2166         }
2167
2168         debug_fleave();
2169
2170         return true;
2171 }
2172
2173 EXPORT_API
2174 bool ASM_get_session_option (const int asm_handle, int *option_flags, int *error_code)
2175 {
2176         int handle = 0;
2177         int asm_index = 0;
2178         GError *err = NULL;
2179         GDBusConnection *conn = NULL;
2180         GVariant *res_variant = NULL;
2181         GVariant *client_variant = NULL;
2182
2183         debug_fenter();
2184
2185         if (error_code == NULL) {
2186                 debug_error("error_code is null");
2187                 return false;
2188         }
2189
2190         if (option_flags == NULL) {
2191                 debug_error("option_flags is null");
2192                 *error_code = ERR_ASM_INVALID_PARAMETER;
2193                 return false;
2194         }
2195
2196         if (asm_handle < 0 || asm_handle >= ASM_SERVER_HANDLE_MAX) {
2197                 *error_code = ERR_ASM_LOCAL_HANDLE_IS_INVALID;
2198                 debug_error("Invalid handle %d \n", asm_handle);
2199                 return false;
2200         }
2201
2202         handle = asm_handle;
2203
2204         asm_index = __ASM_find_index_by_handle(handle);
2205         if (asm_index == -1) {
2206                 debug_error("Can not find index of %d", handle);
2207                 return false;
2208         }
2209
2210         conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &err);
2211         if (!conn && err) {
2212                 debug_error ("g_bus_get_sync() error (%s) ", err->message);
2213                 g_error_free (err);
2214                 return false;
2215         }
2216
2217         client_variant = g_variant_new("(iiiiii)",ASM_sound_handle[asm_index].asm_tid, ASM_sound_handle[asm_index].handle, 0,
2218                                                                         ASM_REQUEST_GET_SESSION_OPTIONS, 0, 0);
2219         res_variant = g_dbus_connection_call_sync(conn, ASM_BUS_NAME_SOUND_SERVER, ASM_OBJECT_SOUND_SERVER, ASM_INTERFACE_SOUND_SERVER,
2220                                                                                 "ASMGetSessionOption", client_variant, NULL, G_DBUS_CALL_FLAGS_NONE, -1, NULL,  &err);
2221
2222         if(!res_variant && err) {
2223                 debug_error("g_dbus_connection_call_sync fail(%s)", err->message);
2224                 *error_code = ERR_ASM_GDBUS_CONNECTION_ERROR;
2225                 g_error_free (err);
2226                 return false;
2227         } else {
2228                 g_variant_get(res_variant ,"(iiiiii)", &(asm_rcv_msg.instance_id), &(asm_rcv_msg.data.alloc_handle), &(asm_rcv_msg.data.cmd_handle),
2229                                                 &(asm_rcv_msg.data.source_request_id), &(asm_rcv_msg.data.result_sound_command), &(asm_rcv_msg.data.option_flags));
2230         }
2231
2232         if (asm_rcv_msg.data.result_sound_command == ASM_COMMAND_STOP) {
2233                 *error_code = asm_rcv_msg.data.error_code;
2234                 debug_error("received handle is not valid");
2235                 return false;
2236         }
2237
2238         *option_flags = asm_rcv_msg.data.option_flags;
2239         if (ASM_sound_handle[asm_index].option_flags != *option_flags) {
2240                 debug_error("received flag(%x) from server is not same as local's(%x)", *option_flags, ASM_sound_handle[asm_index].option_flags);
2241                 return false;
2242         }
2243
2244         debug_msg(">>>> option flags [%x]\n", *option_flags);
2245
2246         debug_fleave();
2247
2248         return true;
2249 }
2250
2251 EXPORT_API
2252 bool ASM_reset_resumption_info(const int asm_handle, int *error_code)
2253 {
2254         int handle = 0;
2255         int asm_index = 0;
2256         GError *err = NULL;
2257         GDBusConnection *conn = NULL;
2258         GVariant *res_variant = NULL;
2259         GVariant *client_variant = NULL;
2260
2261         debug_fenter();
2262
2263         if (error_code == NULL) {
2264                 debug_error("error_code is null");
2265                 return false;
2266         }
2267
2268         if (asm_handle < 0 || asm_handle >= ASM_SERVER_HANDLE_MAX) {
2269                 *error_code = ERR_ASM_LOCAL_HANDLE_IS_INVALID;
2270                 debug_error("Invalid handle %d \n", asm_handle);
2271                 return false;
2272         }
2273
2274         handle = asm_handle;
2275
2276         asm_index = __ASM_find_index_by_handle(handle);
2277         if (asm_index == -1) {
2278                 debug_error("Can not find index of %d", handle);
2279                 return false;
2280         }
2281
2282         conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &err);
2283         if (!conn && err) {
2284                 debug_error ("g_bus_get_sync() error (%s) ", err->message);
2285                 g_error_free (err);
2286                 return false;
2287         }
2288
2289         client_variant = g_variant_new("(iiiiii)",ASM_sound_handle[asm_index].asm_tid, ASM_sound_handle[asm_index].handle, 0,
2290                                                                         ASM_REQUEST_RESET_RESUME_TAG, 0, 0);
2291         res_variant = g_dbus_connection_call_sync(conn, ASM_BUS_NAME_SOUND_SERVER, ASM_OBJECT_SOUND_SERVER, ASM_INTERFACE_SOUND_SERVER,
2292                                                                                         "ASMResetResumeTag", client_variant, NULL, G_DBUS_CALL_FLAGS_NONE, -1, NULL,  &err);
2293
2294         if(!res_variant && err) {
2295                 debug_error("g_dbus_connection_call_sync fail(%s)", err->message);
2296                 *error_code = ERR_ASM_GDBUS_CONNECTION_ERROR;
2297                 g_error_free (err);
2298                 return false;
2299         } else {
2300                 g_variant_get(res_variant ,"(iiiiii)", &(asm_rcv_msg.instance_id), &(asm_rcv_msg.data.alloc_handle), &(asm_rcv_msg.data.cmd_handle),
2301                                         &(asm_rcv_msg.data.source_request_id), &(asm_rcv_msg.data.result_sound_command), &(asm_rcv_msg.data.result_sound_state));
2302         }
2303
2304         switch (asm_rcv_msg.data.result_sound_command) {
2305         case ASM_COMMAND_PLAY:
2306                 debug_msg(" >>>> reset information of resumption successfully");
2307                 break;
2308         default:
2309                 debug_error("received message is abnormal..result_sound_command(%d) from ASM server", asm_rcv_msg.data.result_sound_command);
2310                 *error_code = ERR_ASM_GDBUS_CONNECTION_ERROR;
2311                 return false;
2312         }
2313
2314         debug_leave();
2315
2316         return true;
2317 }
2318
2319 EXPORT_API
2320 void ASM_dump_sound_state()
2321 {
2322         GError *err = NULL;
2323         GDBusConnection *conn = NULL;
2324         GVariant *client_variant = NULL;
2325
2326         conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &err);
2327         if (!conn && err) {
2328                 debug_error ("g_bus_get_sync() error (%s) ", err->message);
2329                 g_error_free (err);
2330                 return;
2331         }
2332
2333         client_variant = g_variant_new("(iiiiii)", getpid(), 0, 0, ASM_REQUEST_DUMP, ASM_STATE_NONE, ASM_RESOURCE_NONE);
2334         g_dbus_connection_call_sync(conn, ASM_BUS_NAME_SOUND_SERVER, ASM_OBJECT_SOUND_SERVER, ASM_INTERFACE_SOUND_SERVER,
2335                                                                 "ASMDump", client_variant, NULL, G_DBUS_CALL_FLAGS_NONE, -1, NULL, &err);
2336
2337         if(err) {
2338                 debug_error("g_dbus_connection_call_sync fail(%s)", err->message);
2339                 g_error_free (err);
2340                 return;
2341         }
2342
2343 }
2344
2345
2346 #if defined(CONFIG_ENABLE_SIGNAL_HANDLER)
2347 struct sigaction ASM_int_old_action;
2348 struct sigaction ASM_abrt_old_action;
2349 struct sigaction ASM_segv_old_action;
2350 struct sigaction ASM_term_old_action;
2351 struct sigaction ASM_sys_old_action;
2352 struct sigaction ASM_xcpu_old_action;
2353
2354 static void __asm_notify_emergent_exit(GDBusConnection *conn, int exit_pid, int handle, int sound_event, int request_id, int sound_state)
2355 {
2356         GVariant *client_variant = NULL;
2357         GError *err = NULL;
2358
2359         debug_log("Send Signal EmergentExit");
2360
2361         client_variant = g_variant_new("(iiiii)", exit_pid, handle, sound_event, ASM_REQUEST_EMERGENT_EXIT, sound_state);
2362         g_dbus_connection_emit_signal(conn, NULL, OBJECT_ASM, INTERFACE_ASM, "EmergentExit", client_variant, &err);
2363         if(err) {
2364                 debug_error("g_dbus_connection_emit_signal fail(%s)", err->message);
2365                 g_error_free (err);
2366         } else {
2367                 g_dbus_connection_flush_sync(conn, NULL, &err);
2368                 if(err) {
2369                         debug_error("g_dbus_connection_flush_sync fail(%s)", err->message);
2370                         g_error_free (err);
2371                 }
2372         }
2373 }
2374
2375 void __ASM_signal_handler(int signo)
2376 {
2377         int exit_pid = 0;
2378         int asm_index = 0;
2379         GError *err = NULL;
2380         GDBusConnection *conn = NULL;
2381
2382         debug_warning("ENTER, sig.num(%d)",signo);
2383
2384         /* signal block -------------- */
2385         sigset_t old_mask, all_mask;
2386         sigfillset(&all_mask);
2387         sigprocmask(SIG_BLOCK, &all_mask, &old_mask);
2388
2389         conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &err);
2390         if (!conn && err) {
2391                 debug_error ("g_bus_get_sync() error (%s) ", err->message);
2392                 g_error_free (err);
2393                 return;
2394         }
2395         for (asm_index=0 ;asm_index < ASM_HANDLE_MAX; asm_index++) {
2396                 if (ASM_sound_handle[asm_index].is_used == true &&
2397                                 ASM_sound_handle[asm_index].is_for_watching == false) {
2398                         exit_pid = ASM_sound_handle[asm_index].asm_tid;
2399                         if (exit_pid == asmgettid()) {
2400                                 __asm_notify_emergent_exit(conn, exit_pid, ASM_sound_handle[asm_index].handle, ASM_sound_handle[asm_index].sound_event,
2401                                                            ASM_REQUEST_EMERGENT_EXIT, ASM_sound_handle[asm_index].sound_state);
2402                         }
2403                 }
2404         }
2405
2406         sigprocmask(SIG_SETMASK, &old_mask, NULL);
2407         /* signal unblock ------------ */
2408
2409         switch (signo) {
2410         case SIGINT:
2411                 sigaction(SIGINT, &ASM_int_old_action, NULL);
2412                 raise( signo);
2413                 break;
2414         case SIGABRT:
2415                 sigaction(SIGABRT, &ASM_abrt_old_action, NULL);
2416                 raise( signo);
2417                 break;
2418         case SIGSEGV:
2419                 sigaction(SIGSEGV, &ASM_segv_old_action, NULL);
2420                 raise( signo);
2421                 break;
2422         case SIGTERM:
2423                 sigaction(SIGTERM, &ASM_term_old_action, NULL);
2424                 raise( signo);
2425                 break;
2426         case SIGSYS:
2427                 sigaction(SIGSYS, &ASM_sys_old_action, NULL);
2428                 raise( signo);
2429                 break;
2430         case SIGXCPU:
2431                 sigaction(SIGXCPU, &ASM_xcpu_old_action, NULL);
2432                 raise( signo);
2433                 break;
2434         default:
2435                 break;
2436         }
2437
2438         debug_warning("LEAVE");
2439 }
2440
2441 #endif
2442 static void __attribute__((constructor)) __ASM_init_module(void)
2443 {
2444 #if defined(CONFIG_ENABLE_SIGNAL_HANDLER)
2445         struct sigaction ASM_action;
2446         ASM_action.sa_handler = __ASM_signal_handler;
2447         ASM_action.sa_flags = SA_NOCLDSTOP;
2448         int asm_index = 0;
2449
2450         debug_fenter();
2451
2452         for (asm_index = 0; asm_index < ASM_HANDLE_MAX; asm_index++) {
2453                 ASM_sound_handle[asm_index].handle = ASM_HANDLE_INIT_VAL;
2454         }
2455
2456         sigemptyset(&ASM_action.sa_mask);
2457
2458         sigaction(SIGINT, &ASM_action, &ASM_int_old_action);
2459         sigaction(SIGABRT, &ASM_action, &ASM_abrt_old_action);
2460         sigaction(SIGSEGV, &ASM_action, &ASM_segv_old_action);
2461         sigaction(SIGTERM, &ASM_action, &ASM_term_old_action);
2462         sigaction(SIGSYS, &ASM_action, &ASM_sys_old_action);
2463         sigaction(SIGXCPU, &ASM_action, &ASM_xcpu_old_action);
2464
2465         debug_fleave();
2466 #endif
2467 }
2468
2469
2470 static void __attribute__((destructor)) __ASM_fini_module(void)
2471 {
2472         debug_fenter();
2473
2474 #if defined(CONFIG_ENABLE_SIGNAL_HANDLER)
2475
2476         int exit_pid = 0;
2477         int asm_index = 0;
2478         GError *err = NULL;
2479         GDBusConnection *conn = NULL;
2480
2481         conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &err);
2482         if (!conn && err) {
2483                 debug_error ("g_bus_get_sync() error (%s) ", err->message);
2484                 g_error_free (err);
2485                 return;
2486         }
2487         for (asm_index=0 ;asm_index < ASM_HANDLE_MAX; asm_index++) {
2488                 if (ASM_sound_handle[asm_index].is_used == true &&
2489                                 ASM_sound_handle[asm_index].is_for_watching == false) {
2490                         exit_pid = ASM_sound_handle[asm_index].asm_tid;
2491                         if (exit_pid == asmgettid()) {
2492                                 __asm_notify_emergent_exit(conn, exit_pid, ASM_sound_handle[asm_index].handle, ASM_sound_handle[asm_index].sound_event,
2493                                                        ASM_REQUEST_EMERGENT_EXIT, ASM_sound_handle[asm_index].sound_state);
2494                         }
2495                 }
2496         }
2497
2498 #endif
2499
2500         if (g_asm_thread) {
2501                 g_main_loop_quit(g_asm_loop);
2502                 g_thread_join(g_asm_thread);
2503                 debug_log("after thread join");
2504                 g_main_loop_unref(g_asm_loop);
2505                 g_asm_thread = NULL;
2506         }
2507         sigaction(SIGINT, &ASM_int_old_action, NULL);
2508         sigaction(SIGABRT, &ASM_abrt_old_action, NULL);
2509         sigaction(SIGSEGV, &ASM_segv_old_action, NULL);
2510         sigaction(SIGTERM, &ASM_term_old_action, NULL);
2511         sigaction(SIGSYS, &ASM_sys_old_action, NULL);
2512         sigaction(SIGXCPU, &ASM_xcpu_old_action, NULL);
2513
2514         debug_fleave();
2515 }
2516