2 * audio-session-manager
4 * Copyright (c) 2000 - 2013 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: Seungbae Shin <seungbae.shin at samsung.com>, Sangchul Lee <sc11.lee at samsung.com>
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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.
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
32 #include <sys/syscall.h>
34 #include <sys/types.h>
49 #include <security-server.h>
50 #define COOKIE_SIZE 20
53 #if defined(USE_VCONF)
57 #include <gconf/gconf.h>
58 #include <gconf/gconf-client.h>
59 #include <phonestatus.h>
62 #include "../include/audio-session-manager.h"
64 #define asmgettid() (long int)getpid()
65 #define ASM_HANDLE_MAX 256
67 #define NO_EINTR(stmt) while ((stmt) == -1 && errno == EINTR); /* sample code by THE LINUX PROGRAMMING INTERFACE */
69 static ASM_msg_lib_to_asm_t asm_snd_msg;
70 static ASM_msg_asm_to_lib_t asm_rcv_msg;
72 static unsigned char str_pass[] = "< OK >";
73 static unsigned char str_fail[] = "<FAIL>";
75 typedef gboolean (*gLoopPollHandler_t)(gpointer d);
77 static GThread *g_asm_thread;
78 static GMainLoop *g_asm_loop;
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;
91 GSourceFuncs* g_src_funcs;
99 static ASM_sound_info_t ASM_sound_handle[ASM_HANDLE_MAX];
101 static const char* ASM_sound_events_str[] =
117 "EXCLUSIVE_RESOURCE",
123 static const char* ASM_sound_state_str[] =
132 static unsigned int ASM_all_sound_status;
134 static int __ASM_find_index_by_handle(int handle);
136 static gpointer thread_func(gpointer data)
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");
144 static bool __ASM_get_sound_state(unsigned int *all_sound_status, int *error_code)
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;
153 debug_log("All status(%#X)", value);
154 *all_sound_status = value;
155 ASM_all_sound_status = value;
160 static gboolean __asm_fd_check(GSource * source)
166 debug_error("GSource is null");
169 fd_list = source->poll_fds;
171 debug_error("fd_list is null");
175 temp = (GPollFD*)fd_list->data;
177 debug_error("fd_list->data is null");
180 if (temp->revents & (POLLIN | POLLPRI)) {
183 fd_list = fd_list->next;
186 return FALSE; /* there is no change in any fd state */
189 static gboolean __asm_fd_prepare(GSource *source, gint *timeout)
194 static gboolean __asm_fd_dispatch(GSource *source, GSourceFunc callback, gpointer user_data)
200 static gboolean asm_callback_handler( gpointer d)
202 GPollFD *data = (GPollFD*)d;
208 debug_log(">>> asm_callback_handler()..ID of this thread(%u)\n", (unsigned int)pthread_self());
211 debug_error("GPollFd is null");
214 if (data->revents & (POLLIN | POLLPRI)) {
218 unsigned int sound_status_value;
219 ASM_sound_commands_t rcv_command;
220 ASM_cb_result_t cb_res = ASM_CB_RES_NONE;
223 count = read(data->fd, &buf, sizeof(int));
224 if (count != sizeof(int)) {
225 debug_log("read %d/%zu", count, sizeof(int));
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);
232 asm_index = __ASM_find_index_by_handle(handle);
233 if (asm_index == -1) {
234 debug_error("Can not find index");
238 if (ASM_sound_handle[asm_index].asm_lock) {
239 g_mutex_lock(ASM_sound_handle[asm_index].asm_lock);
242 tid = ASM_sound_handle[asm_index].asm_tid;
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);
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..");
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]");
266 #ifdef CONFIG_ENABLE_RETCB
268 /* If the command is not RESUME, send return */
269 if (rcv_command != ASM_COMMAND_RESUME) {
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);
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);
280 if (ASM_sound_handle[asm_index].asm_lock) {
281 g_mutex_unlock(ASM_sound_handle[asm_index].asm_lock);
285 rett = write(tmpfd, &buf, sizeof(buf));
288 debug_msg("[RETCB] tid(%d) finishing CB (write=%d)\n", tid, rett);
290 debug_msg("[RETCB] No need to send return for RESUME command\n");
297 if (ASM_sound_handle[asm_index].asm_lock) {
298 g_mutex_unlock(ASM_sound_handle[asm_index].asm_lock);
304 static gboolean watch_callback_handler( gpointer d)
306 GPollFD *data = (GPollFD*)d;
315 debug_error("GPollFd is null");
318 if (data->revents & (POLLIN | POLLPRI)) {
320 ASM_sound_events_t rcv_sound_event = ASM_EVENT_NONE;
321 ASM_sound_states_t rcv_sound_state = ASM_STATE_NONE;
324 unsigned int sound_status_value;
326 ASM_cb_result_t cb_res = ASM_CB_RES_NONE;
329 count = read(data->fd, &buf, sizeof(int));
330 if (count != sizeof(int)) {
331 debug_log("read %d/%zu", count, sizeof(int));
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);
338 asm_index = __ASM_find_index_by_handle(handle);
339 if (asm_index == -1) {
340 debug_error("Can not find index");
344 if (ASM_sound_handle[asm_index].asm_lock) {
345 g_mutex_lock(ASM_sound_handle[asm_index].asm_lock);
348 tid = ASM_sound_handle[asm_index].asm_tid;
350 debug_msg("Got and start CB : handle(%d) sound_event(%d) sound_state(%d)", handle, rcv_sound_event, rcv_sound_state );
352 if (!__ASM_get_sound_state(&sound_status_value, &error_code)) {
353 debug_error("failed to __ASM_get_sound_state(), error(%d)", error_code);
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);
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]");
367 #ifdef CONFIG_ENABLE_RETCB
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);
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);
379 if (ASM_sound_handle[asm_index].asm_lock) {
380 g_mutex_unlock(ASM_sound_handle[asm_index].asm_lock);
384 rett = write(tmpfd, &buf, sizeof(buf));
387 debug_msg("[RETCB] tid(%d) finishing CB (write=%d)\n", tid, rett);
394 if (ASM_sound_handle[asm_index].asm_lock) {
395 g_mutex_unlock(ASM_sound_handle[asm_index].asm_lock);
402 static bool __ASM_add_sound_callback(int index, int fd, gushort events, gLoopPollHandler_t p_gloop_poll_handler )
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 */
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);
414 g_mutex_init(ASM_sound_handle[index].asm_lock);
416 /* 1. make GSource Object */
417 g_src_funcs = (GSourceFuncs *)g_malloc(sizeof(GSourceFuncs));
419 debug_error("g_malloc failed on g_src_funcs");
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));
428 debug_error("g_malloc failed on m_readfd");
431 ASM_sound_handle[index].asm_src = g_src;
432 ASM_sound_handle[index].g_src_funcs = g_src_funcs;
434 /* 2. add file description which used in g_loop() */
435 g_poll_fd = (GPollFD *)g_malloc(sizeof(GPollFD));
437 debug_error("g_malloc failed on g_poll_fd");
441 g_poll_fd->events = events;
442 ASM_sound_handle[index].g_poll_fd = g_poll_fd;
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");
451 g_source_unref(g_src);
453 /* 4. set callback */
454 g_source_set_callback(g_src, p_gloop_poll_handler,(gpointer)g_poll_fd, NULL);
456 debug_log(" g_malloc:g_src_funcs(%p),g_poll_fd(%p) g_source_add_poll:g_src_id(%u) g_source_set_callback:errno(%d)",
457 g_src_funcs, g_poll_fd, gsource_handle, errno);
462 static bool __ASM_remove_sound_callback(int index, gushort events)
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;
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 */
475 debug_error("g_poll_fd is null..");
479 g_poll_fd->fd = ASM_sound_handle[index].asm_fd;
480 g_poll_fd->events = events;
482 if (!ASM_sound_handle[index].asm_src) {
483 debug_error("ASM_sound_handle[%d].asm_src is null..", index);
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);
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);
497 debug_log(" g_free : g_src_funcs(%p), g_poll_fd(%p)", g_src_funcs, g_poll_fd);
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;
518 static bool __ASM_is_existed_request_for_watching(ASM_sound_events_t interest_event, ASM_sound_states_t interest_state, int *index)
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]);
536 static bool __ASM_is_supported_session_for_watching(ASM_sound_events_t interest_event, ASM_sound_states_t interest_state)
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:
552 case ASM_EVENT_VIDEOCALL:
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:
563 debug_error("not supported sound_event(%d)", interest_event);
568 /* check sound_state */
569 switch (interest_state) {
570 case ASM_STATE_PLAYING:
575 debug_error("not supported sound_state(%d)", interest_state);
584 static int __ASM_find_index_by_handle(int handle)
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) {
599 static int __ASM_find_index_by_event(ASM_sound_events_t sound_event, int pid)
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);
613 static void __ASM_add_callback(int index, bool is_for_watching)
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)");
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)");
629 static void __ASM_remove_callback(int index)
631 if (!__ASM_remove_sound_callback(index, (gushort)POLLIN | POLLPRI)) {
632 debug_error("failed to __ASM_remove_sound_callback()");
638 static void __ASM_open_callback(int index)
642 char *filename = g_strdup_printf("/tmp/ASM.%d.%d", ASM_sound_handle[index].asm_tid, ASM_sound_handle[index].handle);
644 if (mknod(filename, S_IFIFO|0666, 0)) {
645 debug_error("mknod() failure, errno(%d)", errno);
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);
652 debug_log("%s : index(%d), filename(%s), fd(%d)", str_pass, index, filename, ASM_sound_handle[index].asm_fd);
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);
660 if (mknod(filename2, S_IFIFO | 0666, 0)) {
661 debug_error("mknod() failure, errno(%d)", errno);
671 void __ASM_close_callback(int index)
673 if (ASM_sound_handle[index].asm_fd < 0) {
674 debug_error("%s : fd error.", str_fail);
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);
681 debug_log("%s : index(%d), filename(%s), fd(%d)", str_pass, index, filename, ASM_sound_handle[index].asm_fd);
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);
689 /* Defensive code - wait until callback timeout although callback is removed */
690 int buf = ASM_CB_RES_STOP;
693 tmpfd = open(filename2, O_WRONLY | O_NONBLOCK);
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);
700 ret = write(tmpfd, &buf, sizeof(buf));
702 debug_msg("write ASM_CB_RES_STOP(tid:%d) for waiting server , error code(%d)", ASM_sound_handle[index].asm_tid, ret);
705 if (remove(filename2)) {
706 debug_error("remove() failure, filename(%s), errno(%d)", filename2, errno);
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)
717 asm_snd_msg.instance_id = asm_pid;
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;
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);
732 static void __ASM_init_callback(int index, bool is_for_watching)
735 __ASM_open_callback(index);
736 __ASM_add_callback(index, is_for_watching);
741 static void __ASM_destroy_callback(int index)
744 __ASM_remove_callback(index);
745 __ASM_close_callback(index);
749 #ifdef SUPPORT_CONTAINER
751 char* _get_cookie(int cookie_size)
756 if (security_server_get_cookie_size() != cookie_size) {
757 debug_error ("[Security] security_server_get_cookie_size() != COOKIE_SIZE(%d)\n", cookie_size);
761 cookie = (char*)malloc (cookie_size);
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);
767 debug_error ("[Security] security_server_request_cookie() returns [%d]\n", retval);
773 static GVariant* _get_cookie_variant ()
776 GVariantBuilder builder;
779 cookie = _get_cookie(COOKIE_SIZE);
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]);
789 return g_variant_builder_end(&builder);
792 #endif /* USE_SECURITY */
793 #endif /* SUPPORT_CONTAINER */
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*))
799 unsigned int sound_status_value;
804 GDBusConnection *conn = NULL;
805 GVariant *res_variant = NULL;
806 GVariant *client_variant = NULL;
807 #ifdef SUPPORT_CONTAINER
813 if (error_code==NULL) {
814 debug_error ("invalid parameter. error code is null");
817 *error_code = ERR_ASM_ERROR_NONE;
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);
825 for (index = 0; index < ASM_HANDLE_MAX; index++) {
826 if (ASM_sound_handle[index].is_used == false) {
831 if (index == ASM_HANDLE_MAX) {
832 *error_code = ERR_ASM_LOCAL_HANDLE_IS_FULL;
833 debug_error ("local sound event is full(MAX)");
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);
849 if (application_pid == -1) {
850 asm_pid = asmgettid();
851 } else if (application_pid > 2) {
852 asm_pid = application_pid;
854 *error_code = ERR_ASM_INVALID_PARAMETER;
855 debug_error ("invalid pid %d", application_pid);
859 ASM_sound_handle[index].sound_event = sound_event;
860 ASM_sound_handle[index].asm_tid = asm_pid;
862 if (!__ASM_get_sound_state(&sound_status_value, error_code)) {
863 debug_error("failed to __ASM_get_sound_state(), error(%d)", *error_code);
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]);
870 handle = -1; /* for register & get handle from server */
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);
878 func ((void*)&asm_snd_msg, (void*)&asm_rcv_msg);
880 conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &err);
882 debug_error ("g_bus_get_sync() error (%s) ", err->message);
886 debug_error ("conn = %p", conn);
888 #ifdef SUPPORT_CONTAINER
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 */
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 */
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;
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));
919 handle = asm_rcv_msg.data.alloc_handle; /* get handle from server */
921 debug_error("failed to create handle from server");
922 *error_code = ERR_ASM_SERVER_HANDLE_IS_FULL;
926 ASM_sound_handle[index].handle = handle;
928 __ASM_init_callback(index, ASM_sound_handle[index].is_for_watching);
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) {
937 __ASM_destroy_callback(index);
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;
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);
956 int action_index = 0;
957 unsigned int rcv_sound_status_value = 0;
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);
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);
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);
974 debug_msg("null callback");
980 case ASM_COMMAND_PLAY:
981 case ASM_COMMAND_NONE:
982 case ASM_COMMAND_RESUME:
983 ASM_sound_handle[index].sound_state = sound_state;
988 /********************************************************************************************************/
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;
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;
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)
1009 return ASM_register_sound_ex (application_pid, asm_handle, sound_event, sound_state, callback, user_data, mm_resource, error_code, NULL);
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)
1018 if (error_code==NULL) {
1019 debug_error ("invalid parameter. error code is null");
1022 *error_code = ERR_ASM_ERROR_NONE;
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);
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);
1038 handle = asm_handle;
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);
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;
1055 bool ASM_unregister_sound_ex(const int asm_handle, ASM_sound_events_t sound_event, int *error_code, int (*func)(void*,void*))
1060 GDBusConnection *conn = NULL;
1061 GVariant *client_variant = NULL;
1065 if (error_code == NULL) {
1066 debug_error ("invalid parameter. error code is null");
1069 *error_code = ERR_ASM_ERROR_NONE;
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);
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);
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);
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,
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");
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);
1113 func(&asm_snd_msg, &asm_rcv_msg);
1115 conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &err);
1117 debug_error ("g_bus_get_sync() error (%s) ", err->message);
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);
1128 debug_error("g_dbus_connection_call_sync fail(%s)", err->message);
1129 *error_code = ERR_ASM_GDBUS_CONNECTION_ERROR;
1135 if (ASM_sound_handle[asm_index].asm_lock) {
1136 g_mutex_unlock(ASM_sound_handle[asm_index].asm_lock);
1139 __ASM_destroy_callback(asm_index);
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;
1154 bool ASM_unregister_sound(const int asm_handle, ASM_sound_events_t sound_event, int *error_code)
1156 return ASM_unregister_sound_ex (asm_handle, sound_event, error_code, NULL);
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)
1163 unsigned int sound_status_value;
1168 GDBusConnection *conn = NULL;
1169 GVariant *res_variant = NULL;
1170 GVariant *client_variant = NULL;
1171 #ifdef SUPPORT_CONTAINER
1172 char container[128];
1177 if (error_code==NULL) {
1178 debug_error ("invalid parameter. error code is null");
1181 *error_code = ERR_ASM_ERROR_NONE;
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);
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;
1195 if (__ASM_is_existed_request_for_watching(interest_sound_event, interest_sound_state, &index))
1197 debug_warning("already requested interest-session, do not send request message");
1198 *error_code = ERR_ASM_WATCH_ALREADY_REQUESTED;
1202 for (index = 0; index < ASM_HANDLE_MAX; index++) {
1203 if (ASM_sound_handle[index].is_used == false) {
1208 if (index == ASM_HANDLE_MAX) {
1209 *error_code = ERR_ASM_LOCAL_HANDLE_IS_FULL;
1210 debug_error ("local sound event is full(MAX)");
1214 if (application_pid == -1) {
1215 asm_pid = asmgettid();
1216 } else if (application_pid > 2) {
1217 asm_pid = application_pid;
1219 *error_code = ERR_ASM_INVALID_PARAMETER;
1220 debug_error ("invalid pid %d", application_pid);
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;
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);
1233 if (!__ASM_get_sound_state(&sound_status_value, error_code)) {
1234 debug_error("failed to __ASM_get_sound_state(), error(%d)", *error_code);
1238 handle = -1; /* for register & get handle from server */
1239 conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &err);
1241 debug_error ("g_bus_get_sync() error (%s) ", err->message);
1246 #ifdef SUPPORT_CONTAINER
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 */
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 */
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);
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;
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));
1275 handle = asm_rcv_msg.data.alloc_handle; /* get handle from server */
1277 debug_error("failed to create handle from server");
1278 *error_code = ERR_ASM_SERVER_HANDLE_IS_FULL;
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;
1288 __ASM_init_callback(index, ASM_sound_handle[index].is_for_watching);
1290 /********************************************************************************************************/
1291 switch (asm_rcv_msg.data.result_sound_command) {
1292 case ASM_COMMAND_PLAY:
1293 debug_msg(" >>>> added to watch list successfully");
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;
1301 /********************************************************************************************************/
1309 bool ASM_unset_watch_session (ASM_sound_events_t interest_sound_event, ASM_sound_states_t interest_sound_state, int *error_code)
1311 unsigned int sound_status_value;
1316 GDBusConnection *conn = NULL;
1317 GVariant *client_variant = NULL;
1321 if (error_code==NULL) {
1322 debug_error ("invalid parameter. error code is null");
1325 *error_code = ERR_ASM_ERROR_NONE;
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);
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;
1339 if (!__ASM_is_existed_request_for_watching(interest_sound_event, interest_sound_state, &index))
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;
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);
1351 if (!__ASM_get_sound_state(&sound_status_value, error_code)) {
1352 debug_error("failed to __ASM_get_sound_state(), error(%d)", *error_code);
1356 handle = ASM_sound_handle[index].handle;
1357 asm_pid = ASM_sound_handle[index].asm_tid;
1359 if (ASM_sound_handle[index].asm_lock) {
1360 g_mutex_lock(ASM_sound_handle[index].asm_lock);
1363 conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &err);
1365 debug_error ("g_bus_get_sync() error (%s) ", err->message);
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);
1376 debug_error("g_dbus_connection_call_sync fail(%s)", err->message);
1377 *error_code = ERR_ASM_GDBUS_CONNECTION_ERROR;
1382 if (ASM_sound_handle[index].asm_lock) {
1383 g_mutex_unlock(ASM_sound_handle[index].asm_lock);
1386 __ASM_destroy_callback(index);
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;
1395 debug_msg(" >>>> send requesting message successfully");
1403 bool ASM_get_sound_status(unsigned int *all_sound_status, int *error_code)
1405 if (all_sound_status == NULL || error_code == NULL) {
1407 *error_code = ERR_ASM_INVALID_PARAMETER;
1408 debug_error("invalid parameter");
1412 debug_msg("Tid(%ld)", asmgettid());
1414 if (!__ASM_get_sound_state(all_sound_status, error_code)) {
1415 debug_error("failed to __ASM_get_sound_state(), error(%d)", *error_code);
1423 bool ASM_get_process_session_state(const int asm_handle, ASM_sound_states_t *sound_state, int *error_code)
1428 GDBusConnection *conn = NULL;
1429 GVariant *res_variant = NULL;
1430 GVariant *client_variant = NULL;
1432 if (sound_state == NULL || error_code == NULL) {
1434 *error_code = ERR_ASM_INVALID_PARAMETER;
1435 debug_error("invalid parameter");
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);
1447 debug_msg("Pid(%d)", ASM_sound_handle[asm_index].asm_tid);
1449 conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &err);
1451 debug_error ("g_bus_get_sync() error (%s) ", err->message);
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);
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;
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));
1471 *sound_state = asm_rcv_msg.data.result_sound_state;
1473 debug_msg(">>>> Pid(%d), State(%s)", ASM_sound_handle[asm_index].asm_tid, ASM_sound_state_str[*sound_state]);
1479 bool ASM_attach_callback(ASM_sound_events_t sound_event, ASM_sound_cb_t callback, void *user_data, int *error_code)
1483 if (callback == NULL || error_code == NULL) {
1485 *error_code = ERR_ASM_INVALID_PARAMETER;
1486 debug_error("invalid parameter");
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);
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;
1501 *error_code = ERR_ASM_ALREADY_REGISTERED;
1502 debug_error("asm_callback was already registered(%p)", ASM_sound_handle[asm_index].asm_callback);
1506 debug_msg(">>>> Pid(%d), Handle(%d), Event(%s), Callback(%p)", 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);
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)
1518 GDBusConnection *conn = NULL;
1519 GVariant *res_variant = NULL;
1520 GVariant *client_variant = NULL;
1522 if (sound_state == NULL || error_code == NULL) {
1524 *error_code = ERR_ASM_UNKNOWN_ERROR;
1525 debug_error("invalid parameter");
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);
1533 handle = asm_handle;
1535 asm_index = __ASM_find_index_by_handle(handle);
1536 if (asm_index == -1) {
1537 debug_error("Can not find index of %d", handle);
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);
1543 conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &err);
1545 debug_error ("g_bus_get_sync() error (%s) ", err->message);
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);
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;
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));
1564 *sound_state = asm_rcv_msg.data.result_sound_state;
1566 debug_msg(">>>> Event(%s), State(%s)", ASM_sound_events_str[sound_event], ASM_sound_state_str[*sound_state]);
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*))
1577 GDBusConnection *conn = NULL;
1578 GVariant *res_variant = NULL;
1579 GVariant *client_variant = NULL;
1583 if (error_code == NULL) {
1584 debug_error("error_code is null");
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;
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);
1600 handle = asm_handle;
1602 asm_index = __ASM_find_index_by_handle(handle);
1603 if (asm_index == -1) {
1604 debug_error("Can not find index of %d", handle);
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);
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);
1617 debug_msg( "[func(%p) START]", func);
1618 func (&asm_snd_msg, &asm_rcv_msg);
1619 debug_msg( "[func END]");
1621 conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &err);
1623 debug_error ("g_bus_get_sync() error (%s) ", err->message);
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);
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;
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));
1646 if (sound_state == ASM_STATE_PLAYING ) {
1647 debug_log( "sound_state is PLAYING, func(%p)", func);
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) {
1655 debug_msg("handle(%d) is same as asm_rcv_msg.data.cmd_handle", handle);
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);
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;
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);
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);
1682 debug_msg("[ASM_CB] Callback : TID(%ld), handle(%d)", asm_rcv_msg.instance_id, asm_rcv_msg.data.cmd_handle);
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);
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]");
1697 debug_msg("asm callback is null");
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;
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)
1720 return ASM_set_sound_state_ex (asm_handle, sound_event, sound_state, mm_resource, error_code, NULL);
1724 bool ASM_set_subsession (const int asm_handle, ASM_sound_sub_sessions_t subsession, int resource, int *error_code)
1729 GDBusConnection *conn = NULL;
1730 GVariant *res_variant = NULL;
1731 GVariant *client_variant = NULL;
1735 if (error_code == NULL) {
1736 debug_error("error_code is null");
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);
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);
1752 handle = asm_handle;
1754 asm_index = __ASM_find_index_by_handle(handle);
1755 if (asm_index == -1) {
1756 debug_error("Can not find index of %d", handle);
1760 conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &err);
1762 debug_error ("g_bus_get_sync() error (%s) ", err->message);
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);
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;
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));
1783 /* TODO: Should check msg returned.....*/
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:
1797 /********************************************************************************************************/
1798 debug_msg(" <<<<<<<<<<<<<<<< [AFTER] Callback : Main Context >>>>>>>>>>>>>>>>>>>> \n");
1809 bool ASM_get_subsession (const int asm_handle, ASM_sound_sub_sessions_t *subsession, int *error_code)
1814 GDBusConnection *conn = NULL;
1815 GVariant *res_variant = NULL;
1816 GVariant *client_variant = NULL;
1820 if (error_code == NULL) {
1821 debug_error("error_code is null");
1825 if (subsession == NULL) {
1826 debug_error("subsession is null");
1827 *error_code = ERR_ASM_INVALID_PARAMETER;
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);
1837 handle = asm_handle;
1839 asm_index = __ASM_find_index_by_handle(handle);
1840 if (asm_index == -1) {
1841 debug_error("Can not find index of %d", handle);
1845 conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &err);
1847 debug_error ("g_bus_get_sync() error (%s) ", err->message);
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);
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;
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));
1866 *subsession = asm_rcv_msg.data.result_sound_command;
1868 debug_msg(">>>> ASM_get_subsession with subsession value [%d]\n", *subsession);
1875 bool ASM_set_subevent (const int asm_handle, ASM_sound_sub_events_t subevent, int *error_code)
1879 ASM_sound_states_t sound_state = ASM_STATE_NONE;
1881 GDBusConnection *conn = NULL;
1882 GVariant *res_variant = NULL;
1883 GVariant *client_variant = NULL;
1887 if (error_code == NULL) {
1888 debug_error("error_code is null");
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);
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);
1904 handle = asm_handle;
1906 asm_index = __ASM_find_index_by_handle(handle);
1907 if (asm_index == -1) {
1908 debug_error("Can not find index of %d", handle);
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;
1919 conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &err);
1921 debug_error ("g_bus_get_sync() error (%s) ", err->message);
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);
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;
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));
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) {
1947 debug_msg("handle(%d) is same as asm_rcv_msg.data.cmd_handle", handle);
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);
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;
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);
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);
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);
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]");
1986 debug_msg("asm callback is null");
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;
2005 bool ASM_get_subevent (const int asm_handle, ASM_sound_sub_events_t *subevent, int *error_code)
2010 GDBusConnection *conn = NULL;
2011 GVariant *res_variant = NULL;
2012 GVariant *client_variant = NULL;
2016 if (error_code == NULL) {
2017 debug_error("error_code is null");
2021 if (subevent == NULL) {
2022 debug_error("subevent is null");
2023 *error_code = ERR_ASM_INVALID_PARAMETER;
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);
2033 handle = asm_handle;
2035 asm_index = __ASM_find_index_by_handle(handle);
2036 if (asm_index == -1) {
2037 debug_error("Can not find index of %d", handle);
2041 conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &err);
2043 debug_error ("g_bus_get_sync() error (%s) ", err->message);
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);
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;
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));
2063 *subevent = asm_rcv_msg.data.result_sound_command;
2065 debug_msg(">>>> ASM_get_subevent with subevent value [%d]\n", *subevent);
2073 bool ASM_set_session_option (const int asm_handle, int option_flags, int *error_code)
2077 ASM_sound_states_t sound_state = ASM_STATE_NONE;
2079 GDBusConnection *conn = NULL;
2080 GVariant *res_variant = NULL;
2081 GVariant *client_variant = NULL;
2085 if (error_code == NULL) {
2086 debug_error("error_code is null");
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);
2096 if (option_flags < 0) {
2097 *error_code = ERR_ASM_INVALID_PARAMETER;
2098 debug_error("Invalid option_flags(%x)", option_flags);
2102 handle = asm_handle;
2104 asm_index = __ASM_find_index_by_handle(handle);
2105 if (asm_index == -1) {
2106 debug_error("Can not find index of %d", handle);
2110 conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &err);
2112 debug_error ("g_bus_get_sync() error (%s) ", err->message);
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;
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));
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) {
2138 debug_msg("handle(%d) is same as asm_rcv_msg.data.cmd_handle", handle);
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);
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;
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);
2161 case ASM_COMMAND_PLAY:
2162 ASM_sound_handle[asm_index].option_flags = option_flags;
2174 bool ASM_get_session_option (const int asm_handle, int *option_flags, int *error_code)
2179 GDBusConnection *conn = NULL;
2180 GVariant *res_variant = NULL;
2181 GVariant *client_variant = NULL;
2185 if (error_code == NULL) {
2186 debug_error("error_code is null");
2190 if (option_flags == NULL) {
2191 debug_error("option_flags is null");
2192 *error_code = ERR_ASM_INVALID_PARAMETER;
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);
2202 handle = asm_handle;
2204 asm_index = __ASM_find_index_by_handle(handle);
2205 if (asm_index == -1) {
2206 debug_error("Can not find index of %d", handle);
2210 conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &err);
2212 debug_error ("g_bus_get_sync() error (%s) ", err->message);
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);
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;
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));
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");
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);
2244 debug_msg(">>>> option flags [%x]\n", *option_flags);
2252 bool ASM_reset_resumption_info(const int asm_handle, int *error_code)
2257 GDBusConnection *conn = NULL;
2258 GVariant *res_variant = NULL;
2259 GVariant *client_variant = NULL;
2263 if (error_code == NULL) {
2264 debug_error("error_code is null");
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);
2274 handle = asm_handle;
2276 asm_index = __ASM_find_index_by_handle(handle);
2277 if (asm_index == -1) {
2278 debug_error("Can not find index of %d", handle);
2282 conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &err);
2284 debug_error ("g_bus_get_sync() error (%s) ", err->message);
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);
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;
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));
2304 switch (asm_rcv_msg.data.result_sound_command) {
2305 case ASM_COMMAND_PLAY:
2306 debug_msg(" >>>> reset information of resumption successfully");
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;
2320 void ASM_dump_sound_state()
2323 GDBusConnection *conn = NULL;
2324 GVariant *client_variant = NULL;
2326 conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &err);
2328 debug_error ("g_bus_get_sync() error (%s) ", err->message);
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);
2338 debug_error("g_dbus_connection_call_sync fail(%s)", err->message);
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;
2354 static void __asm_notify_emergent_exit(GDBusConnection *conn, int exit_pid, int handle, int sound_event, int request_id, int sound_state)
2356 GVariant *client_variant = NULL;
2359 debug_log("Send Signal EmergentExit");
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);
2364 debug_error("g_dbus_connection_emit_signal fail(%s)", err->message);
2367 g_dbus_connection_flush_sync(conn, NULL, &err);
2369 debug_error("g_dbus_connection_flush_sync fail(%s)", err->message);
2375 void __ASM_signal_handler(int signo)
2380 GDBusConnection *conn = NULL;
2382 debug_warning("ENTER, sig.num(%d)",signo);
2384 /* signal block -------------- */
2385 sigset_t old_mask, all_mask;
2386 sigfillset(&all_mask);
2387 sigprocmask(SIG_BLOCK, &all_mask, &old_mask);
2389 conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &err);
2391 debug_error ("g_bus_get_sync() error (%s) ", err->message);
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);
2406 sigprocmask(SIG_SETMASK, &old_mask, NULL);
2407 /* signal unblock ------------ */
2411 sigaction(SIGINT, &ASM_int_old_action, NULL);
2415 sigaction(SIGABRT, &ASM_abrt_old_action, NULL);
2419 sigaction(SIGSEGV, &ASM_segv_old_action, NULL);
2423 sigaction(SIGTERM, &ASM_term_old_action, NULL);
2427 sigaction(SIGSYS, &ASM_sys_old_action, NULL);
2431 sigaction(SIGXCPU, &ASM_xcpu_old_action, NULL);
2438 debug_warning("LEAVE");
2442 static void __attribute__((constructor)) __ASM_init_module(void)
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;
2452 for (asm_index = 0; asm_index < ASM_HANDLE_MAX; asm_index++) {
2453 ASM_sound_handle[asm_index].handle = ASM_HANDLE_INIT_VAL;
2456 sigemptyset(&ASM_action.sa_mask);
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);
2470 static void __attribute__((destructor)) __ASM_fini_module(void)
2474 #if defined(CONFIG_ENABLE_SIGNAL_HANDLER)
2479 GDBusConnection *conn = NULL;
2481 conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &err);
2483 debug_error ("g_bus_get_sync() error (%s) ", err->message);
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);
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;
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);