4 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: Jonghyuk Choi <jhchoi.choi@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.
25 #include <sys/types.h>
28 #include "avsys-common.h"
29 #include "avsys-audio-sync.h"
30 #include "avsys-error.h"
31 #include "avsys-debug.h"
32 #include "avsys-audio-shm.h"
33 #include "avsys-audio-path.h"
35 static avsys_sync_param_t g_presettings[AVSYS_AUDIO_SYNC_IDEN_CNT] = {
36 {"audio_handle_lock", AVSYS_KEY_PREFIX_GEN(AVSYS_KEY_PREFIX_AUDIO, AVSYS_AUDIO_SYNC_IDEN_HANDLE)},
37 {"audio_path_lock", AVSYS_KEY_PREFIX_GEN(AVSYS_KEY_PREFIX_AUDIO, AVSYS_AUDIO_SYNC_IDEN_PATH)},
38 {"audio_route_policy_lock", AVSYS_KEY_PREFIX_GEN(AVSYS_KEY_PREFIX_AUDIO, AVSYS_AUDIO_SYNC_IDEN_SOUNDPATH)},
39 {"av_volume_sem", AVSYS_KEY_PREFIX_GEN(AVSYS_KEY_PREFIX_AUDIO, AVSYS_AUDIO_SYNC_IDEN_VOLUME)},
42 int avsys_audio_create_sync(const avsys_audio_sync_iden_t iden)
44 if (iden >= AVSYS_AUDIO_SYNC_IDEN_CNT || 0 > iden)
45 return AVSYS_STATE_ERR_INVALID_PARAMETER;
46 return avsys_create_sync(&g_presettings[iden]);
49 int avsys_audio_remove_sync(const avsys_audio_sync_iden_t iden)
51 if (iden >= AVSYS_AUDIO_SYNC_IDEN_CNT || 0 > iden)
52 return AVSYS_STATE_ERR_INVALID_PARAMETER;
53 return avsys_remove_sync(&g_presettings[iden]);
56 int avsys_audio_lock_sync(const avsys_audio_sync_iden_t iden)
60 int err = AVSYS_STATE_SUCCESS;
62 if (iden >= AVSYS_AUDIO_SYNC_IDEN_CNT || 0 > iden) {
63 return AVSYS_STATE_ERR_INVALID_PARAMETER;
65 //avsys_info(AVAUDIO,"lock[%d]\n",(int)iden);
67 err = avsys_lock_sync(&g_presettings[iden]);
70 if ((iden == AVSYS_AUDIO_SYNC_IDEN_PATH) && (err == AVSYS_STATE_SUCCESS)) {
71 avsys_audio_path_ex_info_t *control = NULL;
72 avsys_audio_path_ex_info_t **temp = NULL;
75 if (AVSYS_FAIL(avsys_audio_get_shm(AVSYS_AUDIO_SHM_IDEN_PATH, (void **)temp))) {
76 avsys_error_r(AVAUDIO, "avsys_audio_get_shm() for path failed in %s\n", __func__);
77 return AVSYS_STATE_ERR_INTERNAL;
82 if (control->pathlock_pid[index] < 0) { /* find empty slot */
83 control->pathlock_pid[index] = pid;
87 if (index == AVSYS_AUDIO_LOCK_SLOT_MAX) {
89 avsys_critical(AVAUDIO, "path lock pid slot is full. print stored pid before clear all slot\n");
90 /* print and cleanup all stored pid */
91 for (i = 0; i < AVSYS_AUDIO_LOCK_SLOT_MAX; i++) {
92 avsys_critical(AVAUDIO, "path lock pid : %d\n", control->pathlock_pid[i]);
93 control->pathlock_pid[i] = -1;
95 control->pathlock_pid[0] = pid; /* add current pid */
97 } while (index < AVSYS_AUDIO_LOCK_SLOT_MAX);
98 } else if ((iden == AVSYS_AUDIO_SYNC_IDEN_HANDLE) && (err == AVSYS_STATE_SUCCESS)) {
99 avsys_audio_handle_info_t *control = NULL;
100 avsys_audio_handle_info_t **temp = NULL;
103 if (AVSYS_FAIL(avsys_audio_get_shm(AVSYS_AUDIO_SHM_IDEN_HANDLE, (void **)temp))) {
104 avsys_error(AVAUDIO, "avsys_audio_get_shm() for handle failed in %s\n", __func__);
105 return AVSYS_STATE_ERR_INTERNAL;
110 if (control->handlelock_pid[index] < 0) { /* find empty slot */
111 control->handlelock_pid[index] = pid;
115 if (index == AVSYS_AUDIO_LOCK_SLOT_MAX) {
117 avsys_critical(AVAUDIO, "handle lock pid slot is full. print stored pid before clear all slot\n");
118 /* print and cleanup all stored pid */
119 for (i = 0; i < AVSYS_AUDIO_LOCK_SLOT_MAX; i++) {
120 avsys_critical(AVAUDIO, "handle lock pid : %d\n", control->handlelock_pid[i]);
121 control->handlelock_pid[i] = -1;
123 control->handlelock_pid[0] = pid; /*add current pid */
125 } while (index < AVSYS_AUDIO_LOCK_SLOT_MAX);
130 int avsys_audio_unlock_sync(const avsys_audio_sync_iden_t iden)
134 int err = AVSYS_STATE_SUCCESS;
136 if (iden >= AVSYS_AUDIO_SYNC_IDEN_CNT || 0 > iden) {
137 return AVSYS_STATE_ERR_INVALID_PARAMETER;
139 //avsys_info(AVAUDIO,"unlock[%d]\n",(int)iden);
141 err = avsys_unlock_sync(&g_presettings[iden]);
144 if ((iden == AVSYS_AUDIO_SYNC_IDEN_PATH) && (err == AVSYS_STATE_SUCCESS)) {
145 avsys_audio_path_ex_info_t *control = NULL;
146 avsys_audio_path_ex_info_t **temp = NULL;
149 if (AVSYS_FAIL(avsys_audio_get_shm(AVSYS_AUDIO_SHM_IDEN_PATH, (void **)temp))) {
150 avsys_error_r(AVAUDIO, "avsys_audio_get_shm() failed in %s\n", __func__);
151 return AVSYS_STATE_ERR_INTERNAL;
156 if (control->pathlock_pid[index] == pid) { /* find empty slot */
157 control->pathlock_pid[index] = -1;
161 if (index == AVSYS_AUDIO_LOCK_SLOT_MAX)
162 avsys_error(AVAUDIO, "Can not find pid (%d) in path_lock_pid slot\n", pid);
163 } while (index < AVSYS_AUDIO_LOCK_SLOT_MAX);
164 } else if ((iden == AVSYS_AUDIO_SYNC_IDEN_HANDLE) && (err == AVSYS_STATE_SUCCESS)) {
165 avsys_audio_handle_info_t *control = NULL;
166 avsys_audio_handle_info_t **temp = NULL;
169 if (AVSYS_FAIL(avsys_audio_get_shm(AVSYS_AUDIO_SHM_IDEN_HANDLE, (void **)temp))) {
170 avsys_error(AVAUDIO, "avsys_audio_get_shm() for handle failed in %s\n", __func__);
171 return AVSYS_STATE_ERR_INTERNAL;
176 if (control->handlelock_pid[index] == pid) { /* find empty slot */
177 control->handlelock_pid[index] = -1;
181 if (index == AVSYS_AUDIO_LOCK_SLOT_MAX)
182 avsys_error(AVAUDIO, "Can not find pid (%d) in handlelock_pid slot\n", pid);
183 } while (index < AVSYS_AUDIO_LOCK_SLOT_MAX);
189 /* find pid information in proc filesystem */
190 /* if pid exist, return AVSYS_STATE_SUCCESS */
191 /* if pid does not exist, return AVSYS_STATE_ERR_ALLOCATION */
192 int avsys_check_process(int check_pid)
195 char check_path[128] = "";
196 int exist = AVSYS_STATE_SUCCESS;
198 memset(check_path, '\0', sizeof(check_path));
199 snprintf(check_path, sizeof(check_path) - 1, "/proc/%d", check_pid);
201 dir = opendir(check_path);
205 avsys_error(AVAUDIO, "pid %d does not exist anymore\n", check_pid);
206 exist = AVSYS_STATE_ERR_ALLOCATION;
209 avsys_error(AVAUDIO, "Permission denied\n");
212 avsys_error(AVAUDIO, "Too many file descriptors in use by process\n");
215 avsys_error(AVAUDIO, "Too many files are currently open in the system\n");
218 avsys_error(AVAUDIO, "Other error : %d\n", errno);
222 avsys_warning(AVAUDIO, "pid : %d still alive\n", check_pid);
223 if (-1 == closedir(dir)) {
224 avsys_error(AVAUDIO, "[%s] closedir failed with errno : %d\n", __func__, errno);
230 int avsys_audio_dump_sync(void)
234 int err = AVSYS_STATE_SUCCESS;
238 fprintf(stdout, "Dump sync : Start\n");
239 for (i = 0; i < AVSYS_AUDIO_SYNC_IDEN_CNT; i++) {
240 err = avsys_dump_sync(&g_presettings[i]);
242 fprintf(stdout, "Dump sync : End\n");