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>
46 #include <security-server.h>
49 #if defined(USE_VCONF)
53 #include <gconf/gconf.h>
54 #include <gconf/gconf-client.h>
55 #include <phonestatus.h>
58 #include "../include/audio-session-manager.h"
60 #define asmgettid() (long int)getpid()
61 #define ASM_HANDLE_MAX 256
63 #define NO_EINTR(stmt) while ((stmt) == -1 && errno == EINTR); /* sample code by THE LINUX PROGRAMMING INTERFACE */
65 int asm_register_instance_id;
71 ASM_msg_lib_to_asm_t asm_snd_msg;
72 ASM_msg_asm_to_lib_t asm_rcv_msg;
73 ASM_msg_asm_to_cb_t asm_cb_msg;
75 ASM_sound_cb_t asm_callback;
77 unsigned char str_pass[] = "< OK >";
78 unsigned char str_fail[] = "<FAIL>";
80 typedef gboolean (*gLoopPollHandler_t)(gpointer d);
82 GThread *g_asm_thread;
83 GMainLoop *g_asm_loop;
89 ASM_sound_events_t sound_event;
90 ASM_sound_states_t sound_state;
91 ASM_sound_cb_t asm_callback;
92 ASM_watch_cb_t watch_callback;
95 GSourceFuncs* g_src_funcs;
103 ASM_sound_ino_t ASM_sound_handle[ASM_HANDLE_MAX];
105 static const char* ASM_sound_events_str[] =
112 "EXCLUSIVE_MMPLAYER",
113 "EXCLUSIVE_MMCAMCORDER",
116 "EXCLUSIVE_AVSYSTEM",
128 "EXCLUSIVE_RESOURCE",
134 static const char* ASM_sound_cases_str[] =
138 "CASE_1PLAY_2ALTER_PLAY",
140 "CASE_1ALTER_PLAY_2PLAY",
144 "CASE_1VIRTUAL_2PLAY",
145 "CASE_1PLAY_2PLAY_MIX",
146 "CASE_RESOURCE_CHECK"
149 static const char* ASM_sound_state_str[] =
156 "STATE_PAUSE_BY_APP",
157 "STATE_ALTER_PLAYING"
161 * function prototypes
165 unsigned int ASM_all_sound_status;
167 int __ASM_find_index_by_handle(int handle);
169 gpointer thread_func(gpointer data)
171 debug_log(">>> thread func..ID of this thread(%u)\n", (unsigned int)pthread_self());
172 g_main_loop_run(g_asm_loop);
173 debug_log("<<< quit thread func..\n");
177 bool __ASM_get_sound_state(unsigned int *all_sound_status, int *error_code)
181 if(vconf_get_int(SOUND_STATUS_KEY, &value)) {
182 debug_error("failed to vconf_get_int(SOUND_STATUS_KEY)");
183 *error_code = ERR_ASM_VCONF_ERROR;
186 debug_log("All status(%#X)", value);
187 *all_sound_status = value;
188 ASM_all_sound_status = value;
193 gboolean __asm_fd_check(GSource * source)
199 debug_error("GSource is null");
202 fd_list = source->poll_fds;
204 debug_error("fd_list is null");
208 temp = (GPollFD*)fd_list->data;
210 debug_error("fd_list->data is null");
213 if (temp->revents & (POLLIN | POLLPRI)) {
216 fd_list = fd_list->next;
219 return FALSE; /* there is no change in any fd state */
222 gboolean __asm_fd_prepare(GSource *source, gint *timeout)
227 gboolean __asm_fd_dispatch(GSource *source, GSourceFunc callback, gpointer user_data)
233 gboolean asm_callback_handler( gpointer d)
235 GPollFD *data = (GPollFD*)d;
241 debug_log(">>> asm_callback_handler()..ID of this thread(%u)\n", (unsigned int)pthread_self());
244 debug_error("GPollFd is null");
247 if (data->revents & (POLLIN | POLLPRI)) {
251 unsigned int sound_status_value;
252 ASM_sound_commands_t rcv_command;
253 ASM_cb_result_t cb_res = ASM_CB_RES_NONE;
256 count = read(data->fd, &buf, sizeof(int));
258 handle = (int)( buf & 0x0000ffff);
259 rcv_command = (ASM_sound_commands_t)((buf >> 16) & 0xff);
260 event_src = (ASM_event_sources_t)((buf >> 24) & 0xff);
262 asm_index = __ASM_find_index_by_handle(handle);
263 if (asm_index == -1) {
264 debug_error("Can not find index");
268 if (ASM_sound_handle[asm_index].asm_lock) {
269 g_mutex_lock(ASM_sound_handle[asm_index].asm_lock);
272 tid = ASM_sound_handle[asm_index].asm_tid;
275 debug_msg("Got and start CB : TID(%d), handle(%d), command(%d,(PLAY(2)/STOP(3)/PAUSE(4)/RESUME(5)), event_src(%d)", tid, handle, rcv_command, event_src );
276 if (!__ASM_get_sound_state(&sound_status_value, &error_code)) {
277 debug_error("failed to __ASM_get_sound_state(), error(%d)", error_code);
279 switch (rcv_command) {
280 case ASM_COMMAND_PLAY:
281 case ASM_COMMAND_RESUME:
282 case ASM_COMMAND_PAUSE:
283 case ASM_COMMAND_STOP:
284 if (ASM_sound_handle[asm_index].asm_callback == NULL) {
285 debug_msg("callback is null..");
288 debug_msg("[CALLBACK(%p) START]",ASM_sound_handle[asm_index].asm_callback);
289 cb_res = (ASM_sound_handle[asm_index].asm_callback)(handle, event_src, rcv_command, sound_status_value, ASM_sound_handle[asm_index].user_data);
290 debug_msg("[CALLBACK END]");
295 #ifdef CONFIG_ENABLE_RETCB
297 /* If command is other than RESUME, send return */
298 if (rcv_command != ASM_COMMAND_RESUME) {
302 char *filename2 = g_strdup_printf("/tmp/ASM.%d.%dr", ASM_sound_handle[asm_index].asm_tid, handle);
303 tmpfd = open(filename2, O_WRONLY | O_NONBLOCK);
306 strerror_r(errno, str_error, sizeof(str_error));
307 debug_error("[RETCB][Failed(May Server Close First)]tid(%d) fd(%d) %s errno=%d(%s)\n", tid, tmpfd, filename2, errno, str_error);
309 if (ASM_sound_handle[asm_index].asm_lock) {
310 g_mutex_unlock(ASM_sound_handle[asm_index].asm_lock);
314 rett = write(tmpfd, &buf, sizeof(buf));
317 debug_msg("[RETCB] tid(%d) finishing CB (write=%d)\n", tid, rett);
319 debug_msg("[RETCB] No need to send return for RESUME command\n");
326 if (ASM_sound_handle[asm_index].asm_lock) {
327 g_mutex_unlock(ASM_sound_handle[asm_index].asm_lock);
333 gboolean watch_callback_handler( gpointer d)
335 GPollFD *data = (GPollFD*)d;
344 debug_error("GPollFd is null");
347 if (data->revents & (POLLIN | POLLPRI)) {
349 ASM_sound_events_t rcv_sound_event = ASM_EVENT_NONE;
350 ASM_sound_states_t rcv_sound_state = ASM_STATE_NONE;
353 unsigned int sound_status_value;
355 ASM_cb_result_t cb_res = ASM_CB_RES_NONE;
358 count = read(data->fd, &buf, sizeof(int));
360 handle = (int)( buf & 0x0000ffff);
361 rcv_sound_event = (ASM_sound_events_t)((buf >> 16) & 0xff);
362 rcv_sound_state = (ASM_sound_states_t)((buf >> 24) & 0xff);
364 asm_index = __ASM_find_index_by_handle(handle);
365 if (asm_index == -1) {
366 debug_error("Can not find index");
370 if (ASM_sound_handle[asm_index].asm_lock) {
371 g_mutex_lock(ASM_sound_handle[asm_index].asm_lock);
374 tid = ASM_sound_handle[asm_index].asm_tid;
376 debug_msg("Got and start CB : handle(%d) sound_event(%d) sound_state(%d)", handle, rcv_sound_event, rcv_sound_state );
378 if (!__ASM_get_sound_state(&sound_status_value, &error_code)) {
379 debug_error("failed to __ASM_get_sound_state(), error(%d)", error_code);
382 if (ASM_sound_handle[asm_index].watch_callback == NULL) {
383 debug_msg("callback is null..");
384 if (ASM_sound_handle[asm_index].asm_lock) {
385 g_mutex_unlock(ASM_sound_handle[asm_index].asm_lock);
389 debug_msg("[CALLBACK(%p) START]",ASM_sound_handle[asm_index].watch_callback);
390 cb_res = (ASM_sound_handle[asm_index].watch_callback)(handle, rcv_sound_event, rcv_sound_state, ASM_sound_handle[asm_index].user_data);
391 debug_msg("[CALLBACK END]");
393 #ifdef CONFIG_ENABLE_RETCB
398 char *filename2 = g_strdup_printf("/tmp/ASM.%d.%dr", ASM_sound_handle[asm_index].asm_tid, handle);
399 tmpfd = open(filename2, O_WRONLY | O_NONBLOCK);
402 strerror_r(errno, str_error, sizeof(str_error));
403 debug_error("[RETCB][Failed(May Server Close First)]tid(%d) fd(%d) %s errno=%d(%s)\n", tid, tmpfd, filename2, errno, str_error);
405 if (ASM_sound_handle[asm_index].asm_lock) {
406 g_mutex_unlock(ASM_sound_handle[asm_index].asm_lock);
410 rett = write(tmpfd, &buf, sizeof(buf));
413 debug_msg("[RETCB] tid(%d) finishing CB (write=%d)\n", tid, rett);
420 if (ASM_sound_handle[asm_index].asm_lock) {
421 g_mutex_unlock(ASM_sound_handle[asm_index].asm_lock);
428 bool __ASM_add_sound_callback(int index, int fd, gushort events, gLoopPollHandler_t p_gloop_poll_handler )
430 GSource* g_src = NULL;
431 GSourceFuncs *g_src_funcs = NULL; /* handler function */
432 guint gsource_handle;
433 GPollFD *g_poll_fd = NULL; /* file descriptor */
435 ASM_sound_handle[index].asm_lock = g_mutex_new();
436 if (!ASM_sound_handle[index].asm_lock) {
437 debug_error("failed to g_mutex_new() for index(%d)", index);
441 /* 1. make GSource Object */
442 g_src_funcs = (GSourceFuncs *)g_malloc(sizeof(GSourceFuncs));
444 debug_error("g_malloc failed on g_src_funcs");
447 g_src_funcs->prepare = __asm_fd_prepare;
448 g_src_funcs->check = __asm_fd_check;
449 g_src_funcs->dispatch = __asm_fd_dispatch;
450 g_src_funcs->finalize = NULL;
451 g_src = g_source_new(g_src_funcs, sizeof(GSource));
453 debug_error("g_malloc failed on m_readfd");
456 ASM_sound_handle[index].asm_src = g_src;
457 ASM_sound_handle[index].g_src_funcs = g_src_funcs;
459 /* 2. add file description which used in g_loop() */
460 g_poll_fd = (GPollFD *)g_malloc(sizeof(GPollFD));
462 debug_error("g_malloc failed on g_poll_fd");
466 g_poll_fd->events = events;
467 ASM_sound_handle[index].g_poll_fd = g_poll_fd;
469 /* 3. combine g_source object and file descriptor */
470 g_source_add_poll(g_src, g_poll_fd);
471 gsource_handle = g_source_attach(g_src, g_main_loop_get_context(g_asm_loop));
472 if (!gsource_handle) {
473 debug_error(" Failed to attach the source to context");
476 g_source_unref(g_src);
478 /* 4. set callback */
479 g_source_set_callback(g_src, p_gloop_poll_handler,(gpointer)g_poll_fd, NULL);
481 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)",
482 g_src_funcs, g_poll_fd, gsource_handle, errno);
487 bool __ASM_remove_sound_callback(int index, gushort events)
490 gboolean gret = TRUE;
492 if (ASM_sound_handle[index].asm_lock) {
493 g_mutex_free(ASM_sound_handle[index].asm_lock);
494 ASM_sound_handle[index].asm_lock = NULL;
497 GSourceFunc *g_src_funcs = ASM_sound_handle[index].g_src_funcs;
498 GPollFD *g_poll_fd = ASM_sound_handle[index].g_poll_fd; /* store file descriptor */
500 debug_error("g_poll_fd is null..");
504 g_poll_fd->fd = ASM_sound_handle[index].asm_fd;
505 g_poll_fd->events = events;
507 g_source_remove_poll(ASM_sound_handle[index].asm_src, g_poll_fd);
508 debug_log(" g_source_remove_poll : fd(%d), event(%x), errno(%d)", g_poll_fd->fd, g_poll_fd->events, errno);
512 g_source_destroy(ASM_sound_handle[index].asm_src);
513 if (!g_source_is_destroyed (ASM_sound_handle[index].asm_src)) {
514 debug_warning(" failed to g_source_destroy(), asm_src(0x%p)", ASM_sound_handle[index].asm_src);
524 debug_log(" g_free : g_src_funcs(%#X), g_poll_fd(%#X)", g_src_funcs, g_poll_fd);
526 ASM_sound_handle[index].g_src_funcs = NULL;
527 ASM_sound_handle[index].g_poll_fd = NULL;
528 ASM_sound_handle[index].asm_src = NULL;
529 ASM_sound_handle[index].asm_callback = NULL;
530 ASM_sound_handle[index].watch_callback = NULL;
536 bool __ASM_is_existed_request_for_watching(ASM_sound_events_t interest_event, ASM_sound_states_t interest_state, int *index)
539 for(i = 0; i< ASM_HANDLE_MAX; i++) {
540 if (ASM_sound_handle[i].is_for_watching && ASM_sound_handle[i].sound_event == interest_event) {
541 if (ASM_sound_handle[i].sound_state == interest_state) {
542 debug_warning("already requested interest-session(%s, %s)",
543 ASM_sound_events_str[interest_event], ASM_sound_state_str[interest_state]);
554 bool __ASM_is_supported_session_for_watching(ASM_sound_events_t interest_event, ASM_sound_states_t interest_state)
558 /* check sound_event */
559 switch (interest_event) {
560 case ASM_EVENT_SHARE_MMPLAYER:
561 case ASM_EVENT_SHARE_MMCAMCORDER:
562 case ASM_EVENT_SHARE_MMSOUND:
563 case ASM_EVENT_SHARE_OPENAL:
564 case ASM_EVENT_SHARE_AVSYSTEM:
565 case ASM_EVENT_EXCLUSIVE_MMPLAYER:
566 case ASM_EVENT_EXCLUSIVE_MMCAMCORDER:
567 case ASM_EVENT_EXCLUSIVE_MMSOUND:
568 case ASM_EVENT_EXCLUSIVE_OPENAL:
569 case ASM_EVENT_EXCLUSIVE_AVSYSTEM:
570 case ASM_EVENT_NOTIFY:
572 case ASM_EVENT_SHARE_FMRADIO:
573 case ASM_EVENT_EXCLUSIVE_FMRADIO:
574 case ASM_EVENT_EARJACK_UNPLUG:
575 case ASM_EVENT_ALARM:
576 case ASM_EVENT_VIDEOCALL:
578 case ASM_EVENT_MONITOR:
579 case ASM_EVENT_RICH_CALL:
580 case ASM_EVENT_EMERGENCY:
581 case ASM_EVENT_EXCLUSIVE_RESOURCE:
582 case ASM_EVENT_VOICE_RECOGNITION:
583 case ASM_EVENT_MMCAMCORDER_AUDIO:
584 case ASM_EVENT_MMCAMCORDER_VIDEO:
588 debug_error("not supported sound_event(%d)", interest_event);
593 /* check sound_state */
594 switch (interest_state) {
595 case ASM_STATE_PLAYING:
600 debug_error("not supported sound_state(%d)", interest_state);
609 int __ASM_find_index_by_handle(int handle)
612 for(i = 0; i< ASM_HANDLE_MAX; i++) {
613 if (handle == ASM_sound_handle[i].handle) {
614 //debug_msg("found index(%d) for handle(%d)", i, handle);
621 int __ASM_find_index_by_event(ASM_sound_events_t sound_event, int pid)
625 for(i = 0; i< ASM_HANDLE_MAX; i++) {
626 if (sound_event == ASM_sound_handle[i].sound_event && pid == ASM_sound_handle[i].asm_tid) {
627 debug_msg("found index(%d) for sound_event(%d)", i, sound_event);
635 void __ASM_add_callback(int index, bool is_for_watching)
637 if (!is_for_watching) {
638 if (!__ASM_add_sound_callback(index, ASM_sound_handle[index].asm_fd, (gushort)POLLIN | POLLPRI, asm_callback_handler)) {
639 debug_error("failed to __ASM_add_sound_callback(asm_callback_handler)");
643 if (!__ASM_add_sound_callback(index, ASM_sound_handle[index].asm_fd, (gushort)POLLIN | POLLPRI, watch_callback_handler)) {
644 debug_error("failed to __ASM_add_sound_callback(watch_callback_handler)");
651 void __ASM_remove_callback(int index)
653 if (!__ASM_remove_sound_callback(index, (gushort)POLLIN | POLLPRI)) {
654 debug_error("failed to __ASM_remove_sound_callback()");
660 void __ASM_open_callback(int index)
664 char *filename = g_strdup_printf("/tmp/ASM.%d.%d", ASM_sound_handle[index].asm_tid, ASM_sound_handle[index].handle);
666 if (mknod(filename, S_IFIFO|0666, 0)) {
667 debug_error("mknod() failure, errno(%d)", errno);
670 ASM_sound_handle[index].asm_fd = open( filename, O_RDWR|O_NONBLOCK);
671 if (ASM_sound_handle[index].asm_fd == -1) {
672 debug_error("%s : index(%d), file open error(%d)", str_fail, index, errno);
674 debug_log("%s : index(%d), filename(%s), fd(%d)", str_pass, index, filename, ASM_sound_handle[index].asm_fd);
678 #ifdef CONFIG_ENABLE_RETCB
679 char *filename2 = g_strdup_printf("/tmp/ASM.%d.%dr", ASM_sound_handle[index].asm_tid, ASM_sound_handle[index].handle);
681 if (mknod(filename2, S_IFIFO | 0666, 0)) {
682 debug_error("mknod() failure, errno(%d)", errno);
691 void __ASM_close_callback(int index)
693 if (ASM_sound_handle[index].asm_fd < 0) {
694 debug_error("%s : fd error.", str_fail);
696 char *filename = g_strdup_printf("/tmp/ASM.%d.%d", ASM_sound_handle[index].asm_tid, ASM_sound_handle[index].handle);
697 close(ASM_sound_handle[index].asm_fd);
698 if (remove(filename)) {
699 debug_error("remove() failure, filename(%s), errno(%d)", filename, errno);
701 debug_log("%s : index(%d), filename(%s), fd(%d)", str_pass, index, filename, ASM_sound_handle[index].asm_fd);
705 #ifdef CONFIG_ENABLE_RETCB
706 char *filename2 = g_strdup_printf("/tmp/ASM.%d.%dr", ASM_sound_handle[index].asm_tid, ASM_sound_handle[index].handle);
708 /* Defensive code - wait until callback timeout although callback is removed */
709 int buf = ASM_CB_RES_STOP;
712 tmpfd = open(filename2, O_WRONLY | O_NONBLOCK);
715 strerror_r(errno, str_error, sizeof(str_error));
716 debug_warning("could not open file(%s) (may server close it first), tid(%d) fd(%d) %s errno=%d(%s)",
717 filename2, ASM_sound_handle[index].asm_tid, tmpfd, filename2, errno, str_error);
719 debug_msg("write ASM_CB_RES_STOP(tid:%d) for waiting server", ASM_sound_handle[index].asm_tid);
720 write(tmpfd, &buf, sizeof(buf));
724 if (remove(filename2)) {
725 debug_error("remove() failure, filename(%s), errno(%d)", filename2, errno);
732 bool __asm_construct_snd_msg(int asm_pid, int handle, ASM_sound_events_t sound_event,
733 ASM_requests_t request_id, ASM_sound_states_t sound_state, ASM_resource_t resource, int *error_code)
735 asm_snd_msg.instance_id = asm_pid;
737 asm_snd_msg.data.handle = handle;
738 asm_snd_msg.data.request_id = request_id;
739 asm_snd_msg.data.sound_event = sound_event;
740 asm_snd_msg.data.sound_state = sound_state;
741 asm_snd_msg.data.system_resource = resource;
743 debug_msg("tid=%ld,handle=%d,req=%d,evt=%d,state=%d,resource=%d,instance_id=%ld", asm_snd_msg.instance_id, asm_snd_msg.data.handle,
744 asm_snd_msg.data.request_id, asm_snd_msg.data.sound_event, asm_snd_msg.data.sound_state, asm_snd_msg.data.system_resource, asm_snd_msg.instance_id);
750 bool __ASM_init_msg(int *error_code)
752 asm_snd_msgid = msgget((key_t)2014, 0666);
753 asm_rcv_msgid = msgget((key_t)4102, 0666);
754 asm_cb_msgid = msgget((key_t)4103, 0666);
756 debug_msg("snd_msqid(%#X), rcv_msqid(%#X), cb_msqid(%#X)\n", asm_snd_msgid, asm_rcv_msgid, asm_cb_msgid);
758 if (asm_snd_msgid == -1 || asm_rcv_msgid == -1 || asm_cb_msgid == -1 ) {
759 *error_code = ERR_ASM_MSG_QUEUE_MSGID_GET_FAILED;
760 debug_error("failed to msgget with error(%d)",*error_code);
767 void __ASM_init_callback(int index, bool is_for_watching)
770 __ASM_open_callback(index);
771 __ASM_add_callback(index, is_for_watching);
776 void __ASM_destroy_callback(int index)
779 __ASM_remove_callback(index);
780 __ASM_close_callback(index);
785 bool __ASM_set_cookie (unsigned char* cookie)
790 cookie_size = security_server_get_cookie_size();
791 if (cookie_size != COOKIE_SIZE) {
792 debug_error ("[Security] security_server_get_cookie_size(%d) != COOKIE_SIZE(%d)\n", cookie_size, COOKIE_SIZE);
796 retval = security_server_request_cookie (cookie, COOKIE_SIZE);
797 if (retval == SECURITY_SERVER_API_SUCCESS) {
798 debug_msg ("[Security] security_server_request_cookie() returns [%d]\n", retval);
801 debug_error ("[Security] security_server_request_cookie() returns [%d]\n", retval);
808 bool ASM_register_sound_ex (const int application_pid, int *asm_handle, ASM_sound_events_t sound_event, ASM_sound_states_t sound_state,
809 ASM_sound_cb_t callback, void *user_data, ASM_resource_t mm_resource, int *error_code, int (*func)(void*,void*))
811 unsigned int sound_status_value;
819 if (error_code==NULL) {
820 debug_error ("invalid parameter. error code is null");
823 *error_code = ERR_ASM_ERROR_NONE;
825 if (sound_event< ASM_EVENT_SHARE_MMPLAYER || sound_event >= ASM_EVENT_MAX) {
826 *error_code = ERR_ASM_EVENT_IS_INVALID;
827 debug_error ("invalid sound event(%d)",sound_event);
831 for (index = 0; index < ASM_HANDLE_MAX; index++) {
832 if (ASM_sound_handle[index].is_used == false) {
837 if (index == ASM_HANDLE_MAX) {
838 *error_code = ERR_ASM_LOCAL_HANDLE_IS_FULL;
839 debug_error ("local sound event is full(MAX)");
844 GMainContext* asm_context = g_main_context_new ();
845 g_asm_loop = g_main_loop_new (asm_context, FALSE);
846 g_main_context_unref(asm_context);
847 g_asm_thread = g_thread_create(thread_func, NULL, TRUE, NULL);
848 if (g_asm_thread == NULL) {
849 debug_error ("could not create thread..");
850 g_main_loop_unref(g_asm_loop);
855 if (application_pid == -1) {
856 asm_pid = asmgettid();
857 } else if (application_pid > 2) {
858 asm_pid = application_pid;
860 *error_code = ERR_ASM_INVALID_PARAMETER;
861 debug_error ("invalid pid %d", application_pid);
865 ASM_sound_handle[index].sound_event = sound_event;
866 ASM_sound_handle[index].asm_tid = asm_pid;
868 if (!__ASM_init_msg(error_code)) {
869 debug_error("failed to __ASM_init_msg(), error(%d)", *error_code);
873 if (!__ASM_get_sound_state(&sound_status_value, error_code)) {
874 debug_error("failed to __ASM_get_sound_state(), error(%d)", *error_code);
878 debug_msg(" <<<< Event(%s), Tid(%d), Index(%d), State(%s)", ASM_sound_events_str[sound_event], ASM_sound_handle[index].asm_tid, index, ASM_sound_state_str[sound_state]);
880 handle = -1; /* for register & get handle from server */
883 /* get cookie from security server */
884 if (__ASM_set_cookie (asm_snd_msg.data.cookie) == false) {
885 debug_error("failed to ASM_set_cookie()");
890 /* Construct msg to send -> send msg -> recv msg */
891 if (!__asm_construct_snd_msg(asm_pid, handle, sound_event, ASM_REQUEST_REGISTER, sound_state, mm_resource, error_code)) {
892 debug_error("failed to __asm_construct_snd_msg(), error(%d)", *error_code);
897 func ((void*)&asm_snd_msg, (void*)&asm_rcv_msg);
899 NO_EINTR(ret = msgsnd(asm_snd_msgid, (void *)&asm_snd_msg, sizeof(asm_snd_msg.data), 0));
901 *error_code = ERR_ASM_MSG_QUEUE_SND_ERROR;
902 debug_error("failed to msgsnd(%d,%s)", errno, strerror(errno));
906 NO_EINTR(ret = msgrcv(asm_rcv_msgid, (void *)&asm_rcv_msg, sizeof(asm_rcv_msg.data), ASM_sound_handle[index].asm_tid, 0));
908 *error_code = ERR_ASM_MSG_QUEUE_RCV_ERROR;
909 debug_error("failed to msgrcv(%d,%s)", errno, strerror(errno));
913 if (asm_rcv_msg.data.source_request_id != ASM_REQUEST_REGISTER) {
914 *error_code = ERR_ASM_MSG_QUEUE_RCV_ERROR;
915 debug_error("received msg is not valid, source_request_id(%d)", asm_rcv_msg.data.source_request_id);
919 /* Construct msg to send -> send msg -> recv msg : end */
922 /* Check privilege first */
923 if (asm_rcv_msg.data.check_privilege == 0) {
924 debug_error("[Security] Check privilege from server Failed!!!");
925 *error_code = ERR_ASM_CHECK_PRIVILEGE_FAILED;
928 debug_msg ("[Security] Check privilege from server Success");
932 handle = asm_rcv_msg.data.alloc_handle; /* get handle from server */
934 debug_error("failed to create handle from server");
935 *error_code = ERR_ASM_SERVER_HANDLE_IS_FULL;
939 ASM_sound_handle[index].handle = handle;
941 __ASM_init_callback(index, ASM_sound_handle[index].is_for_watching);
943 /********************************************************************************************************/
944 switch (asm_rcv_msg.data.result_sound_command) {
945 case ASM_COMMAND_PAUSE:
946 case ASM_COMMAND_STOP:
947 debug_msg( " <<<<<<<<<<<<<<<< Received command : %d (STOP(3)/PAUSE(4)) >>>>>>>>>>>>>>>>>>>>\n", asm_rcv_msg.data.result_sound_command);
948 if (handle == asm_rcv_msg.data.cmd_handle) {
950 __ASM_destroy_callback(index);
952 ASM_sound_handle[index].asm_fd = 0;
953 ASM_sound_handle[index].asm_tid = 0;
954 ASM_sound_handle[index].sound_event = ASM_EVENT_NONE;
955 ASM_sound_handle[index].is_used = false;
956 if (asm_rcv_msg.data.result_sound_command == ASM_COMMAND_PAUSE) {
957 ASM_sound_handle[index].sound_state = ASM_STATE_PAUSE;
958 } else if (asm_rcv_msg.data.result_sound_command == ASM_COMMAND_STOP) {
959 ASM_sound_handle[index].sound_state = ASM_STATE_STOP;
962 if (asm_rcv_msg.data.reason_of_blocking == ASM_SOUND_BLOCKED_BY_TABLE) {
963 /* check previous sound event */
964 switch (asm_rcv_msg.data.previous_sound_event) {
965 case ASM_EVENT_ALARM:
966 debug_warning("blocked by ALARM");
967 *error_code = ERR_ASM_POLICY_CANNOT_PLAY_BY_ALARM;
970 case ASM_EVENT_VIDEOCALL:
972 case ASM_EVENT_RICH_CALL:
973 debug_warning("blocked by CALL/VIDEOCALL/RICH_CALL");
974 *error_code = ERR_ASM_POLICY_CANNOT_PLAY_BY_CALL;
977 debug_warning("blocked by Other(sound_event num:%d)", asm_rcv_msg.data.previous_sound_event);
978 *error_code = ERR_ASM_POLICY_CANNOT_PLAY;
981 } else if (asm_rcv_msg.data.reason_of_blocking == ASM_SOUND_BLOCKED_BY_SOUND_PROFILE) {
982 debug_warning("blocked by sound profile, previous sound_event:%d", asm_rcv_msg.data.previous_sound_event);
983 *error_code = ERR_ASM_POLICY_CANNOT_PLAY_BY_PROFILE;
984 } else if (asm_rcv_msg.data.reason_of_blocking == ASM_SOUND_BLOCKED_BY_CUSTOM) {
985 debug_warning("blocked by custom reason, previous sound_event:%d", asm_rcv_msg.data.previous_sound_event);
986 *error_code = ERR_ASM_POLICY_CANNOT_PLAY_BY_CUSTOM;
988 debug_warning("could not find reason, previous sound_event:%d", asm_rcv_msg.data.previous_sound_event);
989 *error_code = ERR_ASM_POLICY_CANNOT_PLAY;
993 int action_index = 0;
994 unsigned int rcv_sound_status_value = 0;
996 if (!__ASM_get_sound_state(&rcv_sound_status_value, error_code)) {
997 debug_error("failed to __ASM_get_sound_state(), error(%d)", *error_code);
1000 debug_msg("Callback : TID(%ld), handle(%d), command(%d)", asm_rcv_msg.instance_id, asm_rcv_msg.data.cmd_handle, asm_rcv_msg.data.result_sound_command);
1001 action_index = __ASM_find_index_by_handle(asm_rcv_msg.data.cmd_handle);
1002 if (action_index == -1) {
1003 debug_error("Can not find index of instance %ld, handle %d", asm_rcv_msg.instance_id, asm_rcv_msg.data.cmd_handle);
1005 if (ASM_sound_handle[action_index].asm_callback!=NULL) {
1006 ASM_sound_handle[action_index].asm_callback(asm_rcv_msg.data.cmd_handle, ASM_sound_handle[action_index].sound_event, asm_rcv_msg.data.result_sound_command, rcv_sound_status_value, ASM_sound_handle[action_index].user_data);
1008 debug_msg("null callback");
1014 case ASM_COMMAND_PLAY:
1015 case ASM_COMMAND_NONE:
1016 case ASM_COMMAND_RESUME:
1017 ASM_sound_handle[index].sound_state = sound_state;
1022 /********************************************************************************************************/
1025 ASM_sound_handle[index].asm_callback = callback;
1026 ASM_sound_handle[index].user_data = user_data;
1027 ASM_sound_handle[index].is_used = true;
1029 debug_msg(" >>>> Event(%s), Handle(%d), CBFuncPtr(%p)", ASM_sound_events_str[sound_event], handle, callback);
1030 /* Add [out] param, asm_handle */
1031 *asm_handle = handle;
1040 bool ASM_register_sound (const int application_pid, int *asm_handle, ASM_sound_events_t sound_event, ASM_sound_states_t sound_state,
1041 ASM_sound_cb_t callback, void *user_data, ASM_resource_t mm_resource, int *error_code)
1043 return ASM_register_sound_ex (application_pid, asm_handle, sound_event, sound_state, callback, user_data, mm_resource, error_code, NULL);
1048 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)
1052 if (error_code==NULL) {
1053 debug_error ("invalid parameter. error code is null");
1056 *error_code = ERR_ASM_ERROR_NONE;
1058 if (sound_event < ASM_EVENT_SHARE_MMPLAYER || sound_event >= ASM_EVENT_MAX) {
1059 *error_code = ERR_ASM_EVENT_IS_INVALID;
1060 debug_error ("invalid sound event(%d)",sound_event);
1066 if (asm_handle < 0 || asm_handle >= ASM_SERVER_HANDLE_MAX) {
1067 *error_code = ERR_ASM_POLICY_INVALID_HANDLE;
1068 debug_error("invalid handle(%d). callback is not registered", asm_handle);
1072 handle = asm_handle;
1074 asm_index = __ASM_find_index_by_handle(handle);
1075 if (asm_index == -1) {
1076 *error_code = ERR_ASM_POLICY_INVALID_HANDLE;
1077 debug_error("Can not find index for handle %d", handle);
1081 debug_msg("callback function has changed to %p", callback);
1082 ASM_sound_handle[asm_index].asm_callback = callback;
1083 ASM_sound_handle[asm_index].user_data = user_data;
1089 bool ASM_unregister_sound_ex(const int asm_handle, ASM_sound_events_t sound_event, int *error_code, int (*func)(void*,void*))
1097 if (error_code==NULL) {
1098 debug_error ("invalid parameter. error code is null");
1101 *error_code = ERR_ASM_ERROR_NONE;
1103 if (sound_event<ASM_EVENT_SHARE_MMPLAYER || sound_event >= ASM_EVENT_MAX) {
1104 *error_code = ERR_ASM_EVENT_IS_INVALID;
1105 debug_error ("invalid sound event(%d)",sound_event);
1109 if (asm_handle < 0 || asm_handle >= ASM_SERVER_HANDLE_MAX) {
1110 *error_code = ERR_ASM_POLICY_INVALID_HANDLE;
1111 debug_error("invalid handle(%d). callback is not registered", asm_handle);
1115 handle = asm_handle;
1116 asm_index = __ASM_find_index_by_handle(handle);
1117 if (asm_index == -1) {
1118 *error_code = ERR_ASM_POLICY_INVALID_HANDLE;
1119 debug_error("Can not find index for handle(%d)", handle);
1122 debug_msg("<<<< Event(%s), Tid(%d), Handle(%d) Index(%d)",
1123 ASM_sound_events_str[sound_event], ASM_sound_handle[asm_index].asm_tid, ASM_sound_handle[asm_index].handle, asm_index);
1125 if (!__asm_construct_snd_msg(ASM_sound_handle[asm_index].asm_tid, handle, sound_event, ASM_REQUEST_UNREGISTER, ASM_STATE_NONE, ASM_RESOURCE_NONE, error_code)) {
1126 debug_error("failed to __asm_construct_snd_msg(), error(%d)", *error_code);
1130 if (ASM_sound_handle[asm_index].asm_lock) {
1131 if (!g_mutex_trylock(ASM_sound_handle[asm_index].asm_lock)) {
1132 debug_warning("maybe asm_callback is being called, try one more time..");
1133 usleep(2500000); // 2.5 sec
1134 if (g_mutex_trylock(ASM_sound_handle[asm_index].asm_lock)) {
1135 debug_msg("finally got asm_lock");
1141 func(&asm_snd_msg, &asm_rcv_msg);
1143 NO_EINTR(ret = msgsnd(asm_snd_msgid, (void *)&asm_snd_msg, sizeof(asm_snd_msg.data), 0));
1145 *error_code = ERR_ASM_MSG_QUEUE_SND_ERROR;
1146 debug_error("failed to msgsnd(%d,%s)", errno, strerror(errno));
1147 if (ASM_sound_handle[asm_index].asm_lock) {
1148 g_mutex_unlock(ASM_sound_handle[asm_index].asm_lock);
1154 if (ASM_sound_handle[asm_index].asm_lock) {
1155 g_mutex_unlock(ASM_sound_handle[asm_index].asm_lock);
1158 __ASM_destroy_callback(asm_index);
1160 ASM_sound_handle[asm_index].asm_fd = 0;
1161 ASM_sound_handle[asm_index].asm_tid = 0;
1162 ASM_sound_handle[asm_index].handle = 0;
1163 ASM_sound_handle[asm_index].sound_event = ASM_EVENT_NONE;
1164 ASM_sound_handle[asm_index].sound_state = ASM_STATE_NONE;
1165 ASM_sound_handle[asm_index].is_used = false;
1173 bool ASM_unregister_sound(const int asm_handle, ASM_sound_events_t sound_event, int *error_code)
1175 return ASM_unregister_sound_ex (asm_handle, sound_event, error_code, NULL);
1179 bool ASM_set_watch_session (const int application_pid, ASM_sound_events_t interest_sound_event,
1180 ASM_sound_states_t interest_sound_state, ASM_watch_cb_t callback, void *user_data, int *error_code)
1182 unsigned int sound_status_value;
1190 if (error_code==NULL) {
1191 debug_error ("invalid parameter. error code is null");
1194 *error_code = ERR_ASM_ERROR_NONE;
1196 if (interest_sound_event < ASM_EVENT_SHARE_MMPLAYER || interest_sound_event >= ASM_EVENT_MAX) {
1197 *error_code = ERR_ASM_EVENT_IS_INVALID;
1198 debug_error ("invalid sound event(%d)", interest_sound_event);
1202 if (!__ASM_is_supported_session_for_watching(interest_sound_event, interest_sound_state)) {
1203 debug_error("not supported sound_event(%d) or sound_state(%d)", interest_sound_event, interest_sound_state);
1204 *error_code = ERR_ASM_WATCH_NOT_SUPPORTED;
1208 if (__ASM_is_existed_request_for_watching(interest_sound_event, interest_sound_state, &index))
1210 debug_warning("already requested interest-session, do not send request message");
1211 *error_code = ERR_ASM_WATCH_ALREADY_REQUESTED;
1215 for (index = 0; index < ASM_HANDLE_MAX; index++) {
1216 if (ASM_sound_handle[index].is_used == false) {
1221 if (index == ASM_HANDLE_MAX) {
1222 *error_code = ERR_ASM_LOCAL_HANDLE_IS_FULL;
1223 debug_error ("local sound event is full(MAX)");
1227 if (application_pid == -1) {
1228 asm_pid = asmgettid();
1229 } else if (application_pid > 2) {
1230 asm_pid = application_pid;
1232 *error_code = ERR_ASM_INVALID_PARAMETER;
1233 debug_error ("invalid pid %d", application_pid);
1237 ASM_sound_handle[index].asm_tid = asm_pid;
1238 ASM_sound_handle[index].sound_event = interest_sound_event;
1239 ASM_sound_handle[index].sound_state = interest_sound_state;
1241 debug_msg(" <<<< Interest event(%s), state(%s), Tid(%d), Index(%d)",
1242 ASM_sound_events_str[interest_sound_event], ASM_sound_state_str[interest_sound_state], ASM_sound_handle[index].asm_tid, index);
1244 if (!__ASM_init_msg(error_code)) {
1245 debug_error("failed to __ASM_init_msg(), error(%d)", *error_code);
1249 if (!__ASM_get_sound_state(&sound_status_value, error_code)) {
1250 debug_error("failed to __ASM_get_sound_state(), error(%d)", *error_code);
1254 handle = -1; /* for register & get handle from server */
1257 /* get cookie from security server */
1258 if (__ASM_set_cookie (asm_snd_msg.data.cookie) == false) {
1259 debug_error("failed to ASM_set_cookie()");
1264 /* Construct msg to send -> send msg -> recv msg */
1265 if (!__asm_construct_snd_msg(asm_pid, handle, interest_sound_event, ASM_REQUEST_REGISTER_WATCHER, interest_sound_state, ASM_RESOURCE_NONE, error_code)) {
1266 debug_error("failed to __asm_construct_snd_msg(), error(%d)", *error_code);
1270 NO_EINTR(ret = msgsnd(asm_snd_msgid, (void *)&asm_snd_msg, sizeof(asm_snd_msg.data), 0));
1272 *error_code = ERR_ASM_MSG_QUEUE_SND_ERROR;
1273 debug_error("failed to msgsnd(%d,%s)", errno, strerror(errno));
1277 NO_EINTR(ret = msgrcv(asm_rcv_msgid, (void *)&asm_rcv_msg, sizeof(asm_rcv_msg.data), ASM_sound_handle[index].asm_tid, 0));
1279 *error_code = ERR_ASM_MSG_QUEUE_RCV_ERROR;
1280 debug_error("failed to msgrcv(%d,%s)", errno, strerror(errno));
1283 if (asm_rcv_msg.data.source_request_id != ASM_REQUEST_REGISTER_WATCHER) {
1284 *error_code = ERR_ASM_MSG_QUEUE_RCV_ERROR;
1285 debug_error("received msg is not valid, source_request_id(%d)", asm_rcv_msg.data.source_request_id);
1288 /* Construct msg to send -> send msg -> recv msg : end */
1291 /* Check privilege first */
1292 if (asm_rcv_msg.data.check_privilege == 0) {
1293 debug_error("[Security] Check privilege from server Failed!!!");
1294 *error_code = ERR_ASM_CHECK_PRIVILEGE_FAILED;
1297 debug_msg ("[Security] Check privilege from server Success");
1301 handle = asm_rcv_msg.data.alloc_handle; /* get handle from server */
1303 debug_error("failed to create handle from server");
1304 *error_code = ERR_ASM_SERVER_HANDLE_IS_FULL;
1308 ASM_sound_handle[index].handle = handle;
1309 ASM_sound_handle[index].watch_callback = callback;
1310 ASM_sound_handle[index].user_data = user_data;
1311 ASM_sound_handle[index].is_used = true;
1312 ASM_sound_handle[index].is_for_watching = true;
1314 __ASM_init_callback(index, ASM_sound_handle[index].is_for_watching);
1316 /********************************************************************************************************/
1317 switch (asm_rcv_msg.data.result_sound_command) {
1318 case ASM_COMMAND_PLAY:
1319 debug_msg(" >>>> added to watch list successfully");
1323 debug_error("received message is abnormal..result_sound_command(%d) from ASM server", asm_rcv_msg.data.result_sound_command);
1324 *error_code = ERR_ASM_MSG_QUEUE_RCV_ERROR;
1327 /********************************************************************************************************/
1335 bool ASM_unset_watch_session (ASM_sound_events_t interest_sound_event, ASM_sound_states_t interest_sound_state, int *error_code)
1337 unsigned int sound_status_value;
1345 if (error_code==NULL) {
1346 debug_error ("invalid parameter. error code is null");
1349 *error_code = ERR_ASM_ERROR_NONE;
1351 if (interest_sound_event < ASM_EVENT_SHARE_MMPLAYER || interest_sound_event >= ASM_EVENT_MAX) {
1352 *error_code = ERR_ASM_EVENT_IS_INVALID;
1353 debug_error ("invalid sound event(%d)",interest_sound_event);
1357 if (!__ASM_is_supported_session_for_watching(interest_sound_event, interest_sound_state)) {
1358 debug_error("not supported sound_event(%d) or sound_state(%d)", interest_sound_event, interest_sound_state);
1359 *error_code = ERR_ASM_WATCH_NOT_SUPPORTED;
1363 if (!__ASM_is_existed_request_for_watching(interest_sound_event, interest_sound_state, &index))
1365 debug_warning("already unrequested interest-session or have not been requested it before, do not send request message");
1366 *error_code = ERR_ASM_WATCH_ALREADY_UNREQUESTED;
1370 debug_msg(" <<<< Unregister interest event(%s), state(%s), Tid(%d), Index(%d)",
1371 ASM_sound_events_str[ASM_sound_handle[index].sound_event], ASM_sound_state_str[ASM_sound_handle[index].sound_state], ASM_sound_handle[index].asm_tid, index);
1373 if (!__ASM_init_msg(error_code)) {
1374 debug_error("failed to __ASM_init_msg(), error(%d)", *error_code);
1378 if (!__ASM_get_sound_state(&sound_status_value, error_code)) {
1379 debug_error("failed to __ASM_get_sound_state(), error(%d)", *error_code);
1383 handle = ASM_sound_handle[index].handle;
1384 asm_pid = ASM_sound_handle[index].asm_tid;
1387 /* get cookie from security server */
1388 if (__ASM_set_cookie (asm_snd_msg.data.cookie) == false) {
1389 debug_error("failed to ASM_set_cookie()");
1394 /* Construct msg to send -> send msg */
1395 if (!__asm_construct_snd_msg(asm_pid, handle, interest_sound_event, ASM_REQUEST_UNREGISTER_WATCHER, interest_sound_state, ASM_RESOURCE_NONE, error_code)) {
1396 debug_error("failed to __asm_construct_snd_msg(), error(%d)", *error_code);
1400 if (ASM_sound_handle[index].asm_lock) {
1401 g_mutex_lock(ASM_sound_handle[index].asm_lock);
1404 NO_EINTR(ret = msgsnd(asm_snd_msgid, (void *)&asm_snd_msg, sizeof(asm_snd_msg.data), 0));
1406 *error_code = ERR_ASM_MSG_QUEUE_SND_ERROR;
1407 debug_error("failed to msgsnd(%d,%s)", errno, strerror(errno));
1408 if (ASM_sound_handle[index].asm_lock) {
1409 g_mutex_unlock(ASM_sound_handle[index].asm_lock);
1413 /* Construct msg to send -> send msg : end */
1415 if (ASM_sound_handle[index].asm_lock) {
1416 g_mutex_unlock(ASM_sound_handle[index].asm_lock);
1419 __ASM_destroy_callback(index);
1421 ASM_sound_handle[index].asm_tid = 0;
1422 ASM_sound_handle[index].handle = 0;
1423 ASM_sound_handle[index].sound_event = ASM_EVENT_NONE;
1424 ASM_sound_handle[index].sound_state = ASM_STATE_NONE;
1425 ASM_sound_handle[index].is_used = false;
1426 ASM_sound_handle[index].is_for_watching = false;
1428 debug_msg(" >>>> send requesting message successfully");
1436 bool ASM_get_sound_status(unsigned int *all_sound_status, int *error_code)
1438 if (all_sound_status == NULL || error_code == NULL) {
1440 *error_code = ERR_ASM_INVALID_PARAMETER;
1441 debug_error("invalid parameter");
1445 debug_msg("Tid(%ld)", asmgettid());
1447 if (!__ASM_get_sound_state(all_sound_status, error_code)) {
1448 debug_error("failed to __ASM_get_sound_state(), error(%d)", *error_code);
1456 bool ASM_get_process_session_state(const int asm_handle, ASM_sound_states_t *sound_state, int *error_code)
1462 if (sound_state == NULL || error_code == NULL) {
1464 *error_code = ERR_ASM_INVALID_PARAMETER;
1465 debug_error("invalid parameter");
1469 handle = asm_handle;
1470 asm_index = __ASM_find_index_by_handle(handle);
1471 if (asm_index == -1) {
1472 debug_error("Can not find index of %d", handle);
1477 debug_msg("Pid(%d)", ASM_sound_handle[asm_index].asm_tid);
1479 if (!__asm_construct_snd_msg(ASM_sound_handle[asm_index].asm_tid, handle, ASM_EVENT_MONITOR, ASM_REQUEST_GETMYSTATE, ASM_STATE_NONE, ASM_RESOURCE_NONE, error_code)) {
1480 debug_error("failed to __asm_construct_snd_msg(), error(%d)", *error_code);
1485 NO_EINTR(ret = msgsnd(asm_snd_msgid, (void *)&asm_snd_msg, sizeof(asm_snd_msg.data), 0));
1487 *error_code = ERR_ASM_MSG_QUEUE_SND_ERROR;
1488 debug_error("failed to msgsnd(%d,%s)", errno, strerror(errno));
1492 NO_EINTR(ret = msgrcv(asm_rcv_msgid, (void *)&asm_rcv_msg, sizeof(asm_rcv_msg.data), ASM_sound_handle[asm_index].asm_tid, 0));
1494 *error_code = ERR_ASM_MSG_QUEUE_RCV_ERROR;
1495 debug_error("failed to msgrcv(%d,%s)", errno, strerror(errno));
1499 if (asm_rcv_msg.data.source_request_id != ASM_REQUEST_GETMYSTATE) {
1500 *error_code = ERR_ASM_MSG_QUEUE_RCV_ERROR;
1501 debug_error("received msg is not valid, source_request_id(%d)", asm_rcv_msg.data.source_request_id);
1505 *sound_state = asm_rcv_msg.data.result_sound_state;
1507 debug_msg(">>>> Pid(%d), State(%s)", ASM_sound_handle[asm_index].asm_tid, ASM_sound_state_str[*sound_state]);
1514 bool ASM_attach_callback(ASM_sound_events_t sound_event, ASM_sound_cb_t callback, void *user_data, int *error_code)
1518 if (callback == NULL || error_code == NULL) {
1520 *error_code = ERR_ASM_INVALID_PARAMETER;
1521 debug_error("invalid parameter");
1525 asm_index = __ASM_find_index_by_event(sound_event, asmgettid());
1526 if (asm_index == -1) {
1527 debug_error("Could not find index of the event(%d)", sound_event);
1531 if (!ASM_sound_handle[asm_index].asm_callback) {
1532 ASM_sound_handle[asm_index].asm_callback = callback;
1533 ASM_sound_handle[asm_index].user_data = user_data;
1536 *error_code = ERR_ASM_ALREADY_REGISTERED;
1537 debug_error("asm_callback was already registered(0x%x)", ASM_sound_handle[asm_index].asm_callback);
1541 debug_msg(">>>> Pid(%d), Handle(%d), Event(%s), Callback(0x%x)", ASM_sound_handle[asm_index].asm_tid, ASM_sound_handle[asm_index].handle,
1542 ASM_sound_events_str[ASM_sound_handle[asm_index].sound_event], ASM_sound_handle[asm_index].asm_callback);
1548 bool ASM_get_sound_state(const int asm_handle, ASM_sound_events_t sound_event, ASM_sound_states_t *sound_state, int *error_code)
1554 if (sound_state == NULL || error_code == NULL) {
1556 *error_code = ERR_ASM_UNKNOWN_ERROR;
1557 debug_error("invalid parameter");
1560 if (sound_event < ASM_EVENT_SHARE_MMPLAYER || sound_event >= ASM_EVENT_MAX) {
1561 *error_code = ERR_ASM_EVENT_IS_INVALID;
1562 debug_error("invalid sound event(%d)",sound_event);
1565 handle = asm_handle;
1567 asm_index = __ASM_find_index_by_handle(handle);
1568 if (asm_index == -1) {
1569 debug_error("Can not find index of %d", handle);
1572 debug_msg("<<<< Event(%s), Tid(%d), handle(%d)",
1573 ASM_sound_events_str[sound_event], ASM_sound_handle[asm_index].asm_tid, ASM_sound_handle[asm_index].handle);
1575 if (!__asm_construct_snd_msg(ASM_sound_handle[asm_index].asm_tid, handle, sound_event, ASM_REQUEST_GETSTATE, ASM_STATE_NONE, ASM_RESOURCE_NONE, error_code)) {
1576 debug_error("failed to __asm_construct_snd_msg(), error(%d)", *error_code);
1580 NO_EINTR(ret = msgsnd(asm_snd_msgid, (void *)&asm_snd_msg, sizeof(asm_snd_msg.data), 0));
1582 *error_code = ERR_ASM_MSG_QUEUE_SND_ERROR;
1583 debug_error("failed to msgsnd(%d,%s)", errno, strerror(errno));
1587 NO_EINTR(ret = msgrcv(asm_rcv_msgid, (void *)&asm_rcv_msg, sizeof(asm_rcv_msg.data), ASM_sound_handle[asm_index].asm_tid, 0));
1589 *error_code = ERR_ASM_MSG_QUEUE_RCV_ERROR;
1590 debug_error("failed to msgrcv(%d,%s)", errno, strerror(errno));
1594 if (asm_rcv_msg.data.source_request_id != ASM_REQUEST_GETSTATE) {
1595 *error_code = ERR_ASM_MSG_QUEUE_RCV_ERROR;
1596 debug_error("received msg is not valid, source_request_id(%d)", asm_rcv_msg.data.source_request_id);
1600 *sound_state = asm_rcv_msg.data.result_sound_state;
1602 debug_msg(">>>> Event(%s), State(%s)", ASM_sound_events_str[sound_event], ASM_sound_state_str[*sound_state]);
1609 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*))
1617 if (error_code == NULL) {
1618 debug_error("error_code is null");
1622 if (sound_event < 0 || sound_event > ASM_PRIORITY_MATRIX_MIN) {
1623 debug_error("invalid sound event(%d)",sound_event);
1624 *error_code = ERR_ASM_EVENT_IS_INVALID;
1628 if (asm_handle < 0 || asm_handle >= ASM_SERVER_HANDLE_MAX) {
1629 *error_code = ERR_ASM_POLICY_INVALID_HANDLE;
1630 debug_error("Invalid handle %d", asm_handle);
1634 handle = asm_handle;
1636 asm_index = __ASM_find_index_by_handle(handle);
1637 if (asm_index == -1) {
1638 debug_error("Can not find index of %d", handle);
1642 debug_msg("<<<< Event(%s), State(%s), Tid(%d), handle(%d)",
1643 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);
1645 if (!__asm_construct_snd_msg(ASM_sound_handle[asm_index].asm_tid, ASM_sound_handle[asm_index].handle, sound_event, ASM_REQUEST_SETSTATE, sound_state, mm_resource, error_code)) {
1646 debug_error("failed to __asm_construct_snd_msg(), error(%d)", *error_code);
1651 debug_msg( "[func(%p) START]", func);
1652 func (&asm_snd_msg, &asm_rcv_msg);
1653 debug_msg( "[func END]");
1655 NO_EINTR(ret = msgsnd(asm_snd_msgid, (void *)&asm_snd_msg, sizeof(asm_snd_msg.data), 0));
1657 *error_code = ERR_ASM_MSG_QUEUE_SND_ERROR;
1658 debug_error("failed to msgsnd(%d,%s)", errno, strerror(errno));
1663 if (sound_state == ASM_STATE_PLAYING ) {
1664 debug_log( "sound_state is PLAYING, func(0x%x)", func);
1666 NO_EINTR(ret = msgrcv(asm_rcv_msgid, (void *)&asm_rcv_msg, sizeof(asm_rcv_msg.data), ASM_sound_handle[asm_index].asm_tid, 0));
1668 *error_code = ERR_ASM_MSG_QUEUE_RCV_ERROR;
1669 debug_error("failed to msgrcv(%d,%s)", errno, strerror(errno));
1673 if (asm_rcv_msg.data.source_request_id != ASM_REQUEST_SETSTATE) {
1674 *error_code = ERR_ASM_MSG_QUEUE_RCV_ERROR;
1675 debug_error("received msg is not valid, source_request_id(%d)", asm_rcv_msg.data.source_request_id);
1681 switch (asm_rcv_msg.data.result_sound_command) {
1682 case ASM_COMMAND_PAUSE:
1683 case ASM_COMMAND_STOP:
1684 debug_msg( " <<<<<<<<<<<<<<<< Received command : %d (STOP(3)/PAUSE(4)) >>>>>>>>>>>>>>>>>>>>\n", asm_rcv_msg.data.result_sound_command);
1685 if (handle == asm_rcv_msg.data.cmd_handle) {
1687 debug_msg("handle(%d) is same as asm_rcv_msg.data.cmd_handle", handle);
1689 asm_index = __ASM_find_index_by_handle(asm_rcv_msg.data.cmd_handle);
1690 if (asm_index == -1) {
1691 *error_code = ERR_ASM_POLICY_INVALID_HANDLE;
1692 debug_error( "Can not find index from instance_id %ld, handle %d", asm_rcv_msg.instance_id, asm_rcv_msg.data.cmd_handle);
1696 if (asm_rcv_msg.data.result_sound_command == ASM_COMMAND_PAUSE) {
1697 ASM_sound_handle[asm_index].sound_state = ASM_STATE_PAUSE;
1698 } else if (asm_rcv_msg.data.result_sound_command == ASM_COMMAND_STOP) {
1699 ASM_sound_handle[asm_index].sound_state = ASM_STATE_STOP;
1702 if (asm_rcv_msg.data.reason_of_blocking == ASM_SOUND_BLOCKED_BY_TABLE) {
1703 /* check previous sound event */
1704 switch (asm_rcv_msg.data.previous_sound_event) {
1705 case ASM_EVENT_ALARM:
1706 debug_warning("blocked by ALARM");
1707 *error_code = ERR_ASM_POLICY_CANNOT_PLAY_BY_ALARM;
1709 case ASM_EVENT_CALL:
1710 case ASM_EVENT_VIDEOCALL:
1711 case ASM_EVENT_VOIP:
1712 case ASM_EVENT_RICH_CALL:
1713 debug_warning("blocked by CALL/VIDEOCALL/RICH_CALL");
1714 *error_code = ERR_ASM_POLICY_CANNOT_PLAY_BY_CALL;
1717 debug_warning("blocked by Other(sound_event num:%d)", asm_rcv_msg.data.previous_sound_event);
1718 *error_code = ERR_ASM_POLICY_CANNOT_PLAY;
1721 } else if (asm_rcv_msg.data.reason_of_blocking == ASM_SOUND_BLOCKED_BY_SOUND_PROFILE) {
1722 debug_warning("blocked by sound profile, previous sound_event:%d", asm_rcv_msg.data.previous_sound_event);
1723 *error_code = ERR_ASM_POLICY_CANNOT_PLAY_BY_PROFILE;
1724 } else if (asm_rcv_msg.data.reason_of_blocking == ASM_SOUND_BLOCKED_BY_CUSTOM) {
1725 debug_warning("blocked by custom reason, previous sound_event:%d", asm_rcv_msg.data.previous_sound_event);
1726 *error_code = ERR_ASM_POLICY_CANNOT_PLAY_BY_CUSTOM;
1728 debug_warning("could not find reason, previous sound_event:%d", asm_rcv_msg.data.previous_sound_event);
1729 *error_code = ERR_ASM_POLICY_CANNOT_PLAY;
1733 unsigned int rcv_sound_status_value = 0;
1734 if (!__ASM_get_sound_state(&rcv_sound_status_value, error_code)) {
1735 debug_error("failed to __ASM_get_sound_state(), error(%d)", *error_code);
1738 debug_msg("[ASM_CB] Callback : TID(%ld), handle(%d)", asm_rcv_msg.instance_id, asm_rcv_msg.data.cmd_handle);
1740 asm_index = __ASM_find_index_by_handle(asm_rcv_msg.data.cmd_handle);
1741 if (asm_index == -1) {
1742 *error_code = ERR_ASM_POLICY_INVALID_HANDLE;
1743 debug_error("Can not find index from instance_id %ld, handle %d", asm_rcv_msg.instance_id, asm_rcv_msg.data.cmd_handle);
1747 if (ASM_sound_handle[asm_index].asm_callback!=NULL) {
1748 debug_msg( "[ASM_CB(%p) START]", ASM_sound_handle[asm_index].asm_callback);
1749 ASM_sound_handle[asm_index].asm_callback(asm_rcv_msg.data.cmd_handle, ASM_sound_handle[asm_index].sound_event, asm_rcv_msg.data.result_sound_command, rcv_sound_status_value, ASM_sound_handle[asm_index].user_data);
1750 debug_msg( "[ASM_CB END]");
1752 debug_msg("asm callback is null");
1756 case ASM_COMMAND_PLAY:
1757 case ASM_COMMAND_NONE:
1758 case ASM_COMMAND_RESUME:
1759 ASM_sound_handle[asm_index].sound_state = sound_state;
1773 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)
1775 return ASM_set_sound_state_ex (asm_handle, sound_event, sound_state, mm_resource, error_code, NULL);
1779 bool ASM_set_subsession (const int asm_handle, ASM_sound_sub_sessions_t subsession, int resource, int *error_code)
1787 if (error_code == NULL) {
1788 debug_error("error_code is null");
1792 if (asm_handle < 0 || asm_handle >= ASM_SERVER_HANDLE_MAX) {
1793 *error_code = ERR_ASM_POLICY_INVALID_HANDLE;
1794 debug_error("Invalid handle(%d)", asm_handle);
1798 if (subsession < ASM_SUB_SESSION_TYPE_VOICE || subsession >= ASM_SUB_SESSION_TYPE_MAX) {
1799 *error_code = ERR_ASM_INVALID_PARAMETER;
1800 debug_error("Invalid sub session type(%d)", subsession);
1804 handle = asm_handle;
1806 asm_index = __ASM_find_index_by_handle(handle);
1807 if (asm_index == -1) {
1808 debug_error("Can not find index of %d", handle);
1812 if (!__asm_construct_snd_msg(ASM_sound_handle[asm_index].asm_tid, ASM_sound_handle[asm_index].handle, subsession, ASM_REQUEST_SET_SUBSESSION, ASM_sound_handle[asm_index].sound_state, resource, error_code)) {
1813 debug_error("failed to __asm_construct_snd_msg(), error(%d)", *error_code);
1818 NO_EINTR(ret = msgsnd(asm_snd_msgid, (void *)&asm_snd_msg, sizeof(asm_snd_msg.data), 0));
1820 *error_code = ERR_ASM_MSG_QUEUE_SND_ERROR;
1821 debug_error("failed to msgsnd(%d,%s)", errno, strerror(errno));
1825 NO_EINTR(ret = msgrcv(asm_rcv_msgid, (void *)&asm_rcv_msg, sizeof(asm_rcv_msg.data), ASM_sound_handle[asm_index].asm_tid, 0));
1827 *error_code = ERR_ASM_MSG_QUEUE_RCV_ERROR;
1828 debug_error("failed to msgrcv(%d,%s)", errno, strerror(errno));
1832 if (asm_rcv_msg.data.source_request_id != ASM_REQUEST_SET_SUBSESSION) {
1833 *error_code = ERR_ASM_MSG_QUEUE_RCV_ERROR;
1834 debug_error("received msg is not valid, source_request_id(%d)", asm_rcv_msg.data.source_request_id);
1838 /* TODO: Should check msg returned.....*/
1841 debug_msg( " <<<<<<<<<<<<<<<< [BEFORE] Callback : Main Context >>>>>>>>>>>>>>>>>>>> \n");
1842 /********************************************************************************************************/
1843 switch (asm_rcv_msg.data.result_sound_command) {
1844 case ASM_COMMAND_PAUSE:
1845 case ASM_COMMAND_STOP:
1846 case ASM_COMMAND_PLAY:
1847 case ASM_COMMAND_NONE:
1848 case ASM_COMMAND_RESUME:
1852 /********************************************************************************************************/
1853 debug_msg(" <<<<<<<<<<<<<<<< [AFTER] Callback : Main Context >>>>>>>>>>>>>>>>>>>> \n");
1864 bool ASM_get_subsession (const int asm_handle, ASM_sound_sub_sessions_t *subsession, int *error_code)
1872 if (error_code == NULL) {
1873 debug_error("error_code is null");
1877 if (subsession == NULL) {
1878 debug_error("subsession is null");
1879 *error_code = ERR_ASM_INVALID_PARAMETER;
1883 if (asm_handle < 0 || asm_handle >= ASM_SERVER_HANDLE_MAX) {
1884 *error_code = ERR_ASM_POLICY_INVALID_HANDLE;
1885 debug_error("Invalid handle %d \n", asm_handle);
1889 handle = asm_handle;
1891 asm_index = __ASM_find_index_by_handle(handle);
1892 if (asm_index == -1) {
1893 debug_error("Can not find index of %d", handle);
1897 if (!__asm_construct_snd_msg(ASM_sound_handle[asm_index].asm_tid, ASM_sound_handle[asm_index].handle, 0, ASM_REQUEST_GET_SUBSESSION, 0, 0, error_code)) {
1898 debug_error("failed to __asm_construct_snd_msg(), error(%d)", *error_code);
1903 NO_EINTR(ret = msgsnd(asm_snd_msgid, (void *)&asm_snd_msg, sizeof(asm_snd_msg.data), 0));
1905 *error_code = ERR_ASM_MSG_QUEUE_SND_ERROR;
1906 debug_error("failed to msgsnd(%d,%s)", errno, strerror(errno));
1910 NO_EINTR(ret = msgrcv(asm_rcv_msgid, (void *)&asm_rcv_msg, sizeof(asm_rcv_msg.data), ASM_sound_handle[asm_index].asm_tid, 0));
1912 *error_code = ERR_ASM_MSG_QUEUE_RCV_ERROR;
1913 debug_error("failed to msgrcv(%d,%s)", errno, strerror(errno));
1917 if (asm_rcv_msg.data.source_request_id != ASM_REQUEST_GET_SUBSESSION) {
1918 *error_code = ERR_ASM_MSG_QUEUE_RCV_ERROR;
1919 debug_error("received msg is not valid, source_request_id(%d)", asm_rcv_msg.data.source_request_id);
1923 *subsession = asm_rcv_msg.data.result_sound_command;
1925 debug_msg(">>>> ASM_get_subsession with subsession value [%d]\n", *subsession);
1932 bool ASM_set_subevent (const int asm_handle, ASM_sound_sub_events_t subevent, int *error_code)
1937 ASM_sound_states_t sound_state = ASM_STATE_NONE;
1941 if (error_code == NULL) {
1942 debug_error("error_code is null");
1946 if (asm_handle < 0 || asm_handle >= ASM_SERVER_HANDLE_MAX) {
1947 *error_code = ERR_ASM_POLICY_INVALID_HANDLE;
1948 debug_error("Invalid handle(%d)", asm_handle);
1952 if (subevent < ASM_SUB_EVENT_NONE || subevent >= ASM_SUB_EVENT_MAX) {
1953 *error_code = ERR_ASM_INVALID_PARAMETER;
1954 debug_error("Invalid sub event(%d)", subevent);
1958 handle = asm_handle;
1960 asm_index = __ASM_find_index_by_handle(handle);
1961 if (asm_index == -1) {
1962 debug_error("Can not find index of %d", handle);
1966 if (subevent == ASM_SUB_EVENT_NONE) {
1967 sound_state = ASM_STATE_STOP;
1968 if (!__asm_construct_snd_msg(ASM_sound_handle[asm_index].asm_tid, ASM_sound_handle[asm_index].handle, subevent, ASM_REQUEST_SET_SUBEVENT, sound_state, 0, error_code)) {
1969 debug_error("failed to __asm_construct_snd_msg() for ASM_SUB_EVENT_NONE, error(%d)", *error_code);
1972 } else if (subevent < ASM_SUB_EVENT_MAX) {
1973 sound_state = ASM_STATE_PLAYING;
1974 if (!__asm_construct_snd_msg(ASM_sound_handle[asm_index].asm_tid, ASM_sound_handle[asm_index].handle, subevent, ASM_REQUEST_SET_SUBEVENT, sound_state, 0, error_code)) {
1975 debug_error("failed to __asm_construct_snd_msg() for ASM_SUB_EVENT(%d), error(%d)", subevent, *error_code);
1981 NO_EINTR(ret = msgsnd(asm_snd_msgid, (void *)&asm_snd_msg, sizeof(asm_snd_msg.data), 0));
1983 *error_code = ERR_ASM_MSG_QUEUE_SND_ERROR;
1984 debug_error("failed to msgsnd(%d,%s)", errno, strerror(errno));
1988 if (subevent == ASM_SUB_EVENT_NONE) {
1989 ASM_sound_handle[asm_index].sound_state = sound_state;
1990 debug_msg("Sent SUB_EVENT_NONE, do not wait a returned message");
1995 NO_EINTR(ret = msgrcv(asm_rcv_msgid, (void *)&asm_rcv_msg, sizeof(asm_rcv_msg.data), ASM_sound_handle[asm_index].asm_tid, 0));
1997 *error_code = ERR_ASM_MSG_QUEUE_RCV_ERROR;
1998 debug_error("failed to msgrcv(%d,%s)", errno, strerror(errno));
2002 if (asm_rcv_msg.data.source_request_id != ASM_REQUEST_SET_SUBEVENT) {
2003 *error_code = ERR_ASM_MSG_QUEUE_RCV_ERROR;
2004 debug_error("received msg is not valid, source_request_id(%d)", asm_rcv_msg.data.source_request_id);
2008 switch (asm_rcv_msg.data.result_sound_command) {
2009 case ASM_COMMAND_PAUSE:
2010 case ASM_COMMAND_STOP:
2011 debug_msg( " <<<<<<<<<<<<<<<< Received command : %d (STOP(3)/PAUSE(4)) >>>>>>>>>>>>>>>>>>>>\n", asm_rcv_msg.data.result_sound_command);
2012 if (handle == asm_rcv_msg.data.cmd_handle) {
2014 debug_msg("handle(%d) is same as asm_rcv_msg.data.cmd_handle", handle);
2016 asm_index = __ASM_find_index_by_handle(asm_rcv_msg.data.cmd_handle);
2017 if (asm_index == -1) {
2018 *error_code = ERR_ASM_POLICY_INVALID_HANDLE;
2019 debug_error( "Can not find index from instance_id %ld, handle %d", asm_rcv_msg.instance_id, asm_rcv_msg.data.cmd_handle);
2023 if (asm_rcv_msg.data.result_sound_command == ASM_COMMAND_PAUSE) {
2024 ASM_sound_handle[asm_index].sound_state = ASM_STATE_PAUSE;
2025 } else if (asm_rcv_msg.data.result_sound_command == ASM_COMMAND_STOP) {
2026 ASM_sound_handle[asm_index].sound_state = ASM_STATE_STOP;
2029 if (asm_rcv_msg.data.reason_of_blocking == ASM_SOUND_BLOCKED_BY_TABLE) {
2030 /* check previous sound event */
2031 switch (asm_rcv_msg.data.previous_sound_event) {
2032 case ASM_EVENT_ALARM:
2033 debug_warning("blocked by ALARM");
2034 *error_code = ERR_ASM_POLICY_CANNOT_PLAY_BY_ALARM;
2036 case ASM_EVENT_CALL:
2037 case ASM_EVENT_VIDEOCALL:
2038 case ASM_EVENT_VOIP:
2039 case ASM_EVENT_RICH_CALL:
2040 debug_warning("blocked by CALL/VIDEOCALL/RICH_CALL");
2041 *error_code = ERR_ASM_POLICY_CANNOT_PLAY_BY_CALL;
2044 debug_warning("blocked by Other(sound_event num:%d)", asm_rcv_msg.data.previous_sound_event);
2045 *error_code = ERR_ASM_POLICY_CANNOT_PLAY;
2048 } else if (asm_rcv_msg.data.reason_of_blocking == ASM_SOUND_BLOCKED_BY_SOUND_PROFILE) {
2049 debug_warning("blocked by sound profile, previous sound_event:%d", asm_rcv_msg.data.previous_sound_event);
2050 *error_code = ERR_ASM_POLICY_CANNOT_PLAY_BY_PROFILE;
2051 } else if (asm_rcv_msg.data.reason_of_blocking == ASM_SOUND_BLOCKED_BY_CUSTOM) {
2052 debug_warning("blocked by custom reason, previous sound_event:%d", asm_rcv_msg.data.previous_sound_event);
2053 *error_code = ERR_ASM_POLICY_CANNOT_PLAY_BY_CUSTOM;
2055 debug_warning("could not find reason, previous sound_event:%d", asm_rcv_msg.data.previous_sound_event);
2056 *error_code = ERR_ASM_POLICY_CANNOT_PLAY;
2060 unsigned int rcv_sound_status_value = 0;
2061 if (!__ASM_get_sound_state(&rcv_sound_status_value, error_code)) {
2062 debug_error("failed to __ASM_get_sound_state(), error(%d)", *error_code);
2065 debug_msg("[ASM_CB] Callback : TID(%ld), handle(%d)", asm_rcv_msg.instance_id, asm_rcv_msg.data.cmd_handle);
2067 asm_index = __ASM_find_index_by_handle(asm_rcv_msg.data.cmd_handle);
2068 if (asm_index == -1) {
2069 *error_code = ERR_ASM_POLICY_INVALID_HANDLE;
2070 debug_error("Can not find index from instance_id %ld, handle %d", asm_rcv_msg.instance_id, asm_rcv_msg.data.cmd_handle);
2074 if (ASM_sound_handle[asm_index].asm_callback!=NULL) {
2075 debug_msg( "[ASM_CB(%p) START]", ASM_sound_handle[asm_index].asm_callback);
2076 ASM_sound_handle[asm_index].asm_callback(asm_rcv_msg.data.cmd_handle, ASM_sound_handle[asm_index].sound_event, asm_rcv_msg.data.result_sound_command, rcv_sound_status_value, ASM_sound_handle[asm_index].user_data);
2077 debug_msg( "[ASM_CB END]");
2079 debug_msg("asm callback is null");
2083 case ASM_COMMAND_PLAY:
2084 case ASM_COMMAND_NONE:
2085 case ASM_COMMAND_RESUME:
2086 ASM_sound_handle[asm_index].sound_state = sound_state;
2098 bool ASM_get_subevent (const int asm_handle, ASM_sound_sub_events_t *subevent, int *error_code)
2106 if (error_code == NULL) {
2107 debug_error("error_code is null");
2111 if (subevent == NULL) {
2112 debug_error("subevent is null");
2113 *error_code = ERR_ASM_INVALID_PARAMETER;
2117 if (asm_handle < 0 || asm_handle >= ASM_SERVER_HANDLE_MAX) {
2118 *error_code = ERR_ASM_POLICY_INVALID_HANDLE;
2119 debug_error("Invalid handle %d \n", asm_handle);
2123 handle = asm_handle;
2125 asm_index = __ASM_find_index_by_handle(handle);
2126 if (asm_index == -1) {
2127 debug_error("Can not find index of %d", handle);
2131 if (!__asm_construct_snd_msg(ASM_sound_handle[asm_index].asm_tid, ASM_sound_handle[asm_index].handle, 0, ASM_REQUEST_GET_SUBEVENT, 0, 0, error_code)) {
2132 debug_error("failed to __asm_construct_snd_msg(), error(%d)", *error_code);
2137 NO_EINTR(ret = msgsnd(asm_snd_msgid, (void *)&asm_snd_msg, sizeof(asm_snd_msg.data), 0));
2139 *error_code = ERR_ASM_MSG_QUEUE_SND_ERROR;
2140 debug_error("failed to msgsnd(%d,%s)", errno, strerror(errno));
2144 NO_EINTR(ret = msgrcv(asm_rcv_msgid, (void *)&asm_rcv_msg, sizeof(asm_rcv_msg.data), ASM_sound_handle[asm_index].asm_tid, 0));
2146 *error_code = ERR_ASM_MSG_QUEUE_RCV_ERROR;
2147 debug_error("failed to msgrcv(%d,%s)", errno, strerror(errno));
2151 if (asm_rcv_msg.data.source_request_id != ASM_REQUEST_GET_SUBEVENT) {
2152 *error_code = ERR_ASM_MSG_QUEUE_RCV_ERROR;
2153 debug_error("received msg is not valid, source_request_id(%d)", asm_rcv_msg.data.source_request_id);
2157 *subevent = asm_rcv_msg.data.result_sound_command;
2159 debug_msg(">>>> ASM_get_subevent with subevent value [%d]\n", *subevent);
2166 bool ASM_reset_resumption_info(const int asm_handle, int *error_code)
2174 if (error_code == NULL) {
2175 debug_error("error_code is null");
2179 if (asm_handle < 0 || asm_handle >= ASM_SERVER_HANDLE_MAX) {
2180 *error_code = ERR_ASM_POLICY_INVALID_HANDLE;
2181 debug_error("Invalid handle %d \n", asm_handle);
2185 handle = asm_handle;
2187 asm_index = __ASM_find_index_by_handle(handle);
2188 if (asm_index == -1) {
2189 debug_error("Can not find index of %d", handle);
2193 if (!__asm_construct_snd_msg(ASM_sound_handle[asm_index].asm_tid, ASM_sound_handle[asm_index].handle, 0, ASM_REQUEST_RESET_RESUME_TAG, 0, 0, error_code)) {
2194 debug_error("failed to __asm_construct_snd_msg(), error(%d)", *error_code);
2198 NO_EINTR(ret = msgsnd(asm_snd_msgid, (void *)&asm_snd_msg, sizeof(asm_snd_msg.data), 0));
2200 *error_code = ERR_ASM_MSG_QUEUE_SND_ERROR;
2201 debug_error("failed to msgsnd(%d,%s)", errno, strerror(errno));
2205 NO_EINTR(ret = msgrcv(asm_rcv_msgid, (void *)&asm_rcv_msg, sizeof(asm_rcv_msg.data), ASM_sound_handle[asm_index].asm_tid, 0));
2207 *error_code = ERR_ASM_MSG_QUEUE_RCV_ERROR;
2208 debug_error("failed to msgrcv(%d,%s)", errno, strerror(errno));
2212 if (asm_rcv_msg.data.source_request_id != ASM_REQUEST_RESET_RESUME_TAG) {
2213 *error_code = ERR_ASM_MSG_QUEUE_RCV_ERROR;
2214 debug_error("received msg is not valid, source_request_id(%d)", asm_rcv_msg.data.source_request_id);
2218 switch (asm_rcv_msg.data.result_sound_command) {
2219 case ASM_COMMAND_PLAY:
2220 debug_msg(" >>>> reset information of resumption successfully");
2223 debug_error("received message is abnormal..result_sound_command(%d) from ASM server", asm_rcv_msg.data.result_sound_command);
2224 *error_code = ERR_ASM_MSG_QUEUE_RCV_ERROR;
2234 void ASM_dump_sound_state()
2239 if (!__ASM_init_msg(&error) ) {
2240 debug_error("failed to __ASM_init_msg(), error(%d)", error);
2242 if (!__asm_construct_snd_msg(getpid(), 0, 0, ASM_REQUEST_DUMP, ASM_STATE_NONE, ASM_RESOURCE_NONE, &error)) {
2243 debug_error("failed to __asm_construct_snd_msg(), error(%d)", error);
2246 NO_EINTR(ret = msgsnd(asm_snd_msgid, (void *)&asm_snd_msg, sizeof(asm_snd_msg.data), 0));
2248 debug_error("failed to msgsnd(%d,%s)", errno, strerror(errno));
2254 #if defined(CONFIG_ENABLE_SIGNAL_HANDLER)
2255 struct sigaction ASM_int_old_action;
2256 struct sigaction ASM_abrt_old_action;
2257 struct sigaction ASM_segv_old_action;
2258 struct sigaction ASM_term_old_action;
2259 struct sigaction ASM_sys_old_action;
2260 struct sigaction ASM_xcpu_old_action;
2262 void __ASM_signal_handler(int signo)
2267 debug_warning("ENTER, sig.num(%d)",signo);
2269 /* signal block -------------- */
2270 sigset_t old_mask, all_mask;
2271 sigfillset(&all_mask);
2272 sigprocmask(SIG_BLOCK, &all_mask, &old_mask);
2274 for (asm_index=0 ;asm_index < ASM_HANDLE_MAX; asm_index++) {
2275 if (ASM_sound_handle[asm_index].is_used == true &&
2276 ASM_sound_handle[asm_index].is_for_watching == false) {
2277 exit_pid = ASM_sound_handle[asm_index].asm_tid;
2278 if (exit_pid == asmgettid()) {
2279 asm_snd_msg.instance_id = exit_pid;
2280 asm_snd_msg.data.handle = ASM_sound_handle[asm_index].handle;
2281 asm_snd_msg.data.request_id = ASM_REQUEST_EMERGENT_EXIT;
2282 asm_snd_msg.data.sound_event = ASM_sound_handle[asm_index].sound_event;
2283 asm_snd_msg.data.sound_state = ASM_sound_handle[asm_index].sound_state;
2285 if (msgsnd(asm_snd_msgid, (void *)&asm_snd_msg, sizeof(asm_snd_msg.data), 0) < 0 ) {
2286 debug_msg( "msgsnd() failed, tid=%ld, reqid=%d, handle=0x%x, state=0x%x event=%d size=%d",asm_snd_msg.instance_id,
2287 asm_snd_msg.data.request_id, asm_snd_msg.data.handle, asm_snd_msg.data.sound_state, asm_snd_msg.data.sound_event, sizeof(asm_snd_msg.data) );
2288 int tmpid = msgget((key_t)2014, 0666);
2289 if (msgsnd(tmpid, (void *)&asm_snd_msg, sizeof(asm_snd_msg.data), 0) > 0) {
2290 debug_warning("msgsnd() succeed");
2292 debug_error("msgsnd() retry also failed");
2299 sigprocmask(SIG_SETMASK, &old_mask, NULL);
2300 /* signal unblock ------------ */
2304 sigaction(SIGINT, &ASM_int_old_action, NULL);
2308 sigaction(SIGABRT, &ASM_abrt_old_action, NULL);
2312 sigaction(SIGSEGV, &ASM_segv_old_action, NULL);
2316 sigaction(SIGTERM, &ASM_term_old_action, NULL);
2320 sigaction(SIGSYS, &ASM_sys_old_action, NULL);
2324 sigaction(SIGXCPU, &ASM_xcpu_old_action, NULL);
2331 debug_warning("LEAVE");
2335 void __attribute__((constructor)) __ASM_init_module(void)
2337 #if defined(CONFIG_ENABLE_SIGNAL_HANDLER)
2338 struct sigaction ASM_action;
2339 ASM_action.sa_handler = __ASM_signal_handler;
2340 ASM_action.sa_flags = SA_NOCLDSTOP;
2344 sigemptyset(&ASM_action.sa_mask);
2346 sigaction(SIGINT, &ASM_action, &ASM_int_old_action);
2347 sigaction(SIGABRT, &ASM_action, &ASM_abrt_old_action);
2348 sigaction(SIGSEGV, &ASM_action, &ASM_segv_old_action);
2349 sigaction(SIGTERM, &ASM_action, &ASM_term_old_action);
2350 sigaction(SIGSYS, &ASM_action, &ASM_sys_old_action);
2351 sigaction(SIGXCPU, &ASM_action, &ASM_xcpu_old_action);
2358 void __attribute__((destructor)) __ASM_fini_module(void)
2362 #if defined(CONFIG_ENABLE_SIGNAL_HANDLER)
2367 for (asm_index = 0; asm_index < ASM_HANDLE_MAX; asm_index++) {
2368 if (ASM_sound_handle[asm_index].is_used == true &&
2369 ASM_sound_handle[asm_index].is_for_watching == false) {
2370 exit_pid = ASM_sound_handle[asm_index].asm_tid;
2371 if (exit_pid == asmgettid()) {
2372 asm_snd_msg.instance_id = exit_pid;
2373 asm_snd_msg.data.handle = ASM_sound_handle[asm_index].handle;
2374 asm_snd_msg.data.request_id = ASM_REQUEST_EMERGENT_EXIT;
2375 asm_snd_msg.data.sound_event = ASM_sound_handle[asm_index].sound_event;
2376 asm_snd_msg.data.sound_state = ASM_sound_handle[asm_index].sound_state;
2378 if (msgsnd(asm_snd_msgid, (void *)&asm_snd_msg, sizeof(asm_snd_msg.data), 0) < 0 ) {
2379 debug_msg( "msgsnd() failed, tid=%ld, reqid=%d, handle=0x%x, state=0x%x event=%d size=%d",asm_snd_msg.instance_id,
2380 asm_snd_msg.data.request_id, asm_snd_msg.data.handle, asm_snd_msg.data.sound_state, asm_snd_msg.data.sound_event, sizeof(asm_snd_msg.data) );
2381 int tmpid = msgget((key_t)2014, 0666);
2382 if (msgsnd(tmpid, (void *)&asm_snd_msg, sizeof(asm_snd_msg.data), 0) > 0) {
2383 debug_warning("msgsnd() succeed");
2385 debug_error("msgsnd() retry also failed");
2395 g_main_loop_quit(g_asm_loop);
2396 g_thread_join(g_asm_thread);
2397 debug_log("after thread join");
2398 g_main_loop_unref(g_asm_loop);
2399 g_asm_thread = NULL;
2401 sigaction(SIGINT, &ASM_int_old_action, NULL);
2402 sigaction(SIGABRT, &ASM_abrt_old_action, NULL);
2403 sigaction(SIGSEGV, &ASM_segv_old_action, NULL);
2404 sigaction(SIGTERM, &ASM_term_old_action, NULL);
2405 sigaction(SIGSYS, &ASM_sys_old_action, NULL);
2406 sigaction(SIGXCPU, &ASM_xcpu_old_action, NULL);