Dbus logic/layer/interface enhancements
[platform/core/multimedia/libmm-sound.git] / common / mm_ipc.c
1 /*
2  * libmm-sound
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Seungbae Shin <seungbae.shin@samsung.com>
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  */
21
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <errno.h>
26
27 #include <sys/time.h>
28 #include <sys/types.h>
29 #include <sys/stat.h>
30 #include <fcntl.h>
31 #include <semaphore.h>
32 #include <fcntl.h>
33 #include <unistd.h>
34
35 #include <aio.h>
36
37 #include <mm_ipc.h>
38 #include <mm_error.h>
39 #include <mm_debug.h>
40
41 #define FIX_PROCESS 1
42
43 #include <assert.h>
44 #include <sys/ipc.h>
45 #include <sys/msg.h>
46
47 #include <vconf.h>
48
49 #define AUDIO_ROUTE_POLICY_LOCK "audio_route_policy_lock"
50 #define LOCK_TIMEOUT_SEC 6
51
52 EXPORT_API
53 int __mm_sound_lock()
54 {
55     sem_t *sem = NULL;
56     int ret;
57     int err = MM_ERROR_NONE;
58     struct timespec wait_time;
59
60     sem = sem_open(AUDIO_ROUTE_POLICY_LOCK, O_CREAT, 0666, 1);
61     if (sem == SEM_FAILED)
62     {
63         debug_error("Semaphore open Fail! (name:%s, %s)\n", AUDIO_ROUTE_POLICY_LOCK, strerror(errno));
64         return MM_ERROR_SOUND_INTERNAL;
65     }
66 retry_lock:
67         wait_time.tv_sec = (long int)(time(NULL)) + LOCK_TIMEOUT_SEC;
68         wait_time.tv_nsec = 0;
69         ret = sem_timedwait(sem, &wait_time);
70     if(ret == -1)
71     {
72         switch(errno)
73         {
74         case EINTR:
75                 debug_error("Lock RETRY LOCK\n");
76             goto retry_lock;
77             break;
78         case EINVAL:
79                 debug_error("Invalid semaphore\n");
80             err = MM_ERROR_SOUND_INTERNAL;
81             break;
82         case EAGAIN:
83             debug_error("EAGAIN\n");
84             err = MM_ERROR_SOUND_INTERNAL;
85             break;
86         case ETIMEDOUT:
87             debug_error("sem_wait leached %d seconds timeout.\n", LOCK_TIMEOUT_SEC);
88             {
89                 //Recovery of sem_wait lock....in abnormal condition
90                 int sem_value = -1;
91                 if(0 == sem_getvalue(sem, &sem_value))
92                 {
93                         debug_error("%s sem value is %d\n",AUDIO_ROUTE_POLICY_LOCK, sem_value);
94                         if(sem_value == 0)
95                         {
96                                 ret = sem_post(sem);
97                                 if(ret == -1)
98                                 {
99                                         debug_error("sem_post error %s : %d\n", AUDIO_ROUTE_POLICY_LOCK, sem_value);
100                                 }
101                                 else
102                                 {
103                                         debug_error("lock recovery success...try lock again\n");
104                                         goto retry_lock;
105                                 }
106                         }
107                         else
108                         {
109                                 debug_error("sem value is not 0. but failed sem_timedwait so retry.. : %s\n",AUDIO_ROUTE_POLICY_LOCK);
110                                 usleep(5);
111                                 goto retry_lock;
112                         }
113                 }
114                 else
115                 {
116                         debug_error("sem_getvalue failed : %s\n",AUDIO_ROUTE_POLICY_LOCK);
117                 }
118             }
119             err = MM_ERROR_SOUND_INTERNAL;
120             break;
121         }
122     }
123     sem_close(sem);
124     return err;
125 }
126
127 EXPORT_API
128 int __mm_sound_unlock()
129 {
130     sem_t *sem = NULL;
131     int ret;
132     int err = MM_ERROR_NONE;
133
134     sem = sem_open(AUDIO_ROUTE_POLICY_LOCK, O_CREAT, 0666, 1);
135     if (sem == SEM_FAILED)
136     {
137         debug_error("Semaphore open Fail! (name:%s, errno %d)\n", AUDIO_ROUTE_POLICY_LOCK, errno);
138         return MM_ERROR_SOUND_INTERNAL;
139     }
140
141     ret = sem_post(sem);
142     if (ret == -1)
143     {
144         debug_error("UNLOCK FAIL\n");
145         err = MM_ERROR_SOUND_INTERNAL;
146     }
147
148     sem_close(sem);
149     return err;
150 }
151