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)},
40 int avsys_audio_create_sync(const avsys_audio_sync_iden_t iden)
42 if (iden >= AVSYS_AUDIO_SYNC_IDEN_CNT || 0 > iden)
43 return AVSYS_STATE_ERR_INVALID_PARAMETER;
44 return avsys_create_sync(&g_presettings[iden]);
47 int avsys_audio_remove_sync(const avsys_audio_sync_iden_t iden)
49 if (iden >= AVSYS_AUDIO_SYNC_IDEN_CNT || 0 > iden)
50 return AVSYS_STATE_ERR_INVALID_PARAMETER;
51 return avsys_remove_sync(&g_presettings[iden]);
54 int avsys_audio_lock_sync(const avsys_audio_sync_iden_t iden)
58 int err = AVSYS_STATE_SUCCESS;
60 if (iden >= AVSYS_AUDIO_SYNC_IDEN_CNT || 0 > iden) {
61 return AVSYS_STATE_ERR_INVALID_PARAMETER;
63 //avsys_info(AVAUDIO,"lock[%d]\n",(int)iden);
65 err = avsys_lock_sync(&g_presettings[iden]);
68 if ((iden == AVSYS_AUDIO_SYNC_IDEN_PATH) && (err == AVSYS_STATE_SUCCESS)) {
69 avsys_audio_path_ex_info_t *control = NULL;
70 avsys_audio_path_ex_info_t **temp = NULL;
73 if (AVSYS_FAIL(avsys_audio_get_shm(AVSYS_AUDIO_SHM_IDEN_PATH, (void **)temp))) {
74 avsys_error_r(AVAUDIO, "avsys_audio_get_shm() for path failed in %s\n", __func__);
75 return AVSYS_STATE_ERR_INTERNAL;
80 if (control->pathlock_pid[index] < 0) { /* find empty slot */
81 control->pathlock_pid[index] = pid;
85 if (index == AVSYS_AUDIO_LOCK_SLOT_MAX) {
87 avsys_critical(AVAUDIO, "path lock pid slot is full. print stored pid before clear all slot\n");
88 /* print and cleanup all stored pid */
89 for (i = 0; i < AVSYS_AUDIO_LOCK_SLOT_MAX; i++) {
90 avsys_critical(AVAUDIO, "path lock pid : %d\n", control->pathlock_pid[i]);
91 control->pathlock_pid[i] = -1;
93 control->pathlock_pid[0] = pid; /* add current pid */
95 } while (index < AVSYS_AUDIO_LOCK_SLOT_MAX);
96 } else if ((iden == AVSYS_AUDIO_SYNC_IDEN_HANDLE) && (err == AVSYS_STATE_SUCCESS)) {
97 avsys_audio_handle_info_t *control = NULL;
98 avsys_audio_handle_info_t **temp = NULL;
101 if (AVSYS_FAIL(avsys_audio_get_shm(AVSYS_AUDIO_SHM_IDEN_HANDLE, (void **)temp))) {
102 avsys_error(AVAUDIO, "avsys_audio_get_shm() for handle failed in %s\n", __func__);
103 return AVSYS_STATE_ERR_INTERNAL;
108 if (control->handlelock_pid[index] < 0) { /* find empty slot */
109 control->handlelock_pid[index] = pid;
113 if (index == AVSYS_AUDIO_LOCK_SLOT_MAX) {
115 avsys_critical(AVAUDIO, "handle lock pid slot is full. print stored pid before clear all slot\n");
116 /* print and cleanup all stored pid */
117 for (i = 0; i < AVSYS_AUDIO_LOCK_SLOT_MAX; i++) {
118 avsys_critical(AVAUDIO, "handle lock pid : %d\n", control->handlelock_pid[i]);
119 control->handlelock_pid[i] = -1;
121 control->handlelock_pid[0] = pid; /*add current pid */
123 } while (index < AVSYS_AUDIO_LOCK_SLOT_MAX);
128 int avsys_audio_unlock_sync(const avsys_audio_sync_iden_t iden)
132 int err = AVSYS_STATE_SUCCESS;
134 if (iden >= AVSYS_AUDIO_SYNC_IDEN_CNT || 0 > iden) {
135 return AVSYS_STATE_ERR_INVALID_PARAMETER;
137 //avsys_info(AVAUDIO,"unlock[%d]\n",(int)iden);
139 err = avsys_unlock_sync(&g_presettings[iden]);
142 if ((iden == AVSYS_AUDIO_SYNC_IDEN_PATH) && (err == AVSYS_STATE_SUCCESS)) {
143 avsys_audio_path_ex_info_t *control = NULL;
144 avsys_audio_path_ex_info_t **temp = NULL;
147 if (AVSYS_FAIL(avsys_audio_get_shm(AVSYS_AUDIO_SHM_IDEN_PATH, (void **)temp))) {
148 avsys_error_r(AVAUDIO, "avsys_audio_get_shm() failed in %s\n", __func__);
149 return AVSYS_STATE_ERR_INTERNAL;
154 if (control->pathlock_pid[index] == pid) { /* find empty slot */
155 control->pathlock_pid[index] = -1;
159 if (index == AVSYS_AUDIO_LOCK_SLOT_MAX)
160 avsys_error(AVAUDIO, "Can not find pid (%d) in path_lock_pid slot\n", pid);
161 } while (index < AVSYS_AUDIO_LOCK_SLOT_MAX);
162 } else if ((iden == AVSYS_AUDIO_SYNC_IDEN_HANDLE) && (err == AVSYS_STATE_SUCCESS)) {
163 avsys_audio_handle_info_t *control = NULL;
164 avsys_audio_handle_info_t **temp = NULL;
167 if (AVSYS_FAIL(avsys_audio_get_shm(AVSYS_AUDIO_SHM_IDEN_HANDLE, (void **)temp))) {
168 avsys_error(AVAUDIO, "avsys_audio_get_shm() for handle failed in %s\n", __func__);
169 return AVSYS_STATE_ERR_INTERNAL;
174 if (control->handlelock_pid[index] == pid) { /* find empty slot */
175 control->handlelock_pid[index] = -1;
179 if (index == AVSYS_AUDIO_LOCK_SLOT_MAX)
180 avsys_error(AVAUDIO, "Can not find pid (%d) in handlelock_pid slot\n", pid);
181 } while (index < AVSYS_AUDIO_LOCK_SLOT_MAX);
187 /* find pid information in proc filesystem */
188 /* if pid exist, return AVSYS_STATE_SUCCESS */
189 /* if pid does not exist, return AVSYS_STATE_ERR_ALLOCATION */
190 int avsys_check_process(int check_pid)
193 char check_path[128] = "";
194 int exist = AVSYS_STATE_SUCCESS;
196 memset(check_path, '\0', sizeof(check_path));
197 snprintf(check_path, sizeof(check_path) - 1, "/proc/%d", check_pid);
199 dir = opendir(check_path);
203 avsys_error(AVAUDIO, "pid %d does not exist anymore\n", check_pid);
204 exist = AVSYS_STATE_ERR_ALLOCATION;
207 avsys_error(AVAUDIO, "Permission denied\n");
210 avsys_error(AVAUDIO, "Too many file descriptors in use by process\n");
213 avsys_error(AVAUDIO, "Too many files are currently open in the system\n");
216 avsys_error(AVAUDIO, "Other error : %d\n", errno);
220 avsys_warning(AVAUDIO, "pid : %d still alive\n", check_pid);
221 if (-1 == closedir(dir)) {
222 avsys_error(AVAUDIO, "[%s] closedir failed with errno : %d\n", __func__, errno);
228 int avsys_audio_dump_sync(void)
232 int err = AVSYS_STATE_SUCCESS;
236 fprintf(stdout, "Dump sync : Start\n");
237 for (i = 0; i < AVSYS_AUDIO_SYNC_IDEN_CNT; i++) {
238 err = avsys_dump_sync(&g_presettings[i]);
240 fprintf(stdout, "Dump sync : End\n");