4 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: Seungbae Shin <seungbae.shin@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.
35 #include "include/mm_sound_mgr_common.h"
36 #include "include/mm_sound_thread_pool.h"
37 #include "../include/mm_sound_common.h"
42 //#include <audio-session-manager.h>
43 #include <avsys-audio.h>
45 #include "include/mm_sound_mgr_headset.h"
46 //#include "include/mm_sound_mgr_asm.h"
47 #include "include/mm_sound_mgr_session.h"
49 /******************************* Headset Code **********************************/
52 #include <sys/types.h>
61 #include <avsys-audio.h>
64 #include "mm_sound_common.h"
69 #define EARJACK_EJECTED 0
70 #define NO_FORCE_RESET 0
72 void __headset_main_run (void* param)
77 int err = AVSYS_STATE_SUCCESS;
78 int eject_event_count = 0;
82 /* re-check current path */
83 avsys_audio_path_ex_reset(NO_FORCE_RESET);
85 if (AVSYS_FAIL(avsys_audio_earjack_manager_init(¤t_type, &waitfd)))
89 //waiting earjack event
90 err = avsys_audio_earjack_manager_wait(waitfd, ¤t_type, &new_type, &need_mute);
91 debug_log ("wait result = %x, current_type= %d, new_type = %d, need_mute = %d\n", err, current_type, new_type, need_mute);
92 if (err & AVSYS_STATE_ERROR) {
93 #if !defined(_MMFW_I386_ALL_SIMULATOR)
94 if (err != AVSYS_STATE_ERR_NULL_POINTER) {
95 if (AVSYS_FAIL(avsys_audio_earjack_manager_unlock())) {
96 debug_error("avsys_audio_earjack_manager_unlock() failed in %s\n",__func__);
97 err = MM_ERROR_SOUND_INTERNAL;
103 } else if ((err & AVSYS_STATE_WARING)) {
104 if (err != AVSYS_STATE_WAR_INVALID_VALUE) {
105 if (AVSYS_FAIL(avsys_audio_earjack_manager_unlock())) {
106 debug_error("avsys_audio_earjack_manager_unlock() failed in %s\n",__func__);
107 err = MM_ERROR_SOUND_INTERNAL;
111 continue; /* Ignore current changes and do wait again */
113 debug_warning("Current type is %d, New type is %d\n", current_type, new_type);
115 if (current_type == new_type) {
116 if (AVSYS_FAIL(avsys_audio_earjack_manager_unlock())) {
117 debug_error("avsys_audio_earjack_manager_unlock() failed in %s\n",__func__);
118 err = MM_ERROR_SOUND_INTERNAL;
121 continue; /* Ignore current changes and do wait again */
123 current_type = new_type;
125 debug_warning("Current type is %d\n", current_type);
127 /* mute if needed, unmute will be done end of this loop */
129 if (AVSYS_FAIL(avsys_audio_set_global_mute(AVSYS_AUDIO_MUTE_NOLOCK)))
130 debug_error("Set mute failed\n");
133 //current_type '0' means earjack ejected
134 if (current_type == EARJACK_EJECTED) {
136 if (eject_event_count == 1) {
137 debug_msg ("earjack [EJECTED]\n");
139 /* ToDo: Device Update */
140 MMSoundMgrSessionSetDeviceAvailable (DEVICE_WIRED, NOT_AVAILABLE, 0, NULL);
143 } else if (current_type != EARJACK_EJECTED) { /* INSERT */
144 debug_msg ("earjack is [INSERTED]\n");
145 eject_event_count = 0;
147 /* ToDo: Device Update */
148 ret = MMSoundMgrSessionSetDeviceAvailable (DEVICE_WIRED, AVAILABLE, current_type, NULL);
149 if (ret != MM_ERROR_NONE) {
150 /* TODO : Error Handling */
151 debug_error ("MMSoundMgrSessionSetDeviceAvailable failed....ret = [%x]\n", ret);
156 err = avsys_audio_earjack_manager_process(current_type);
157 if (err & AVSYS_STATE_ERROR) {
158 debug_error("Earjack Managing Fatal Error 0x%x\n", err);
160 if (AVSYS_FAIL(avsys_audio_set_global_mute(AVSYS_AUDIO_UNMUTE_NOLOCK))) {
161 debug_error("Set unmute failed\n");
164 #if !defined(_MMFW_I386_ALL_SIMULATOR)
165 if (AVSYS_FAIL(avsys_audio_earjack_manager_unlock())) {
166 debug_error("avsys_audio_earjack_manager_unlock() failed in %s\n",__func__);
167 err = MM_ERROR_SOUND_INTERNAL;
172 } else if (err & AVSYS_STATE_WARING) {
173 debug_error("Earjack Managing Warning 0x%x\n", err);
175 if (AVSYS_FAIL(avsys_audio_set_global_mute(AVSYS_AUDIO_UNMUTE_NOLOCK))) {
176 debug_error("Set unmute failed\n");
180 if (AVSYS_FAIL(avsys_audio_earjack_manager_unlock())) {
181 debug_error("avsys_audio_earjack_manager_unlock() failed in %s\n",__func__);
182 err = MM_ERROR_SOUND_INTERNAL;
188 /* Unmute if muted */
190 if (AVSYS_FAIL(avsys_audio_set_global_mute(AVSYS_AUDIO_UNMUTE_NOLOCK))) {
191 debug_error("Set unmute failed\n");
195 if (AVSYS_FAIL(avsys_audio_earjack_manager_unlock())) {
196 debug_error("avsys_audio_earjack_manager_unlock() failed in %s\n",__func__);
197 err = MM_ERROR_SOUND_INTERNAL;
202 if (AVSYS_FAIL(avsys_audio_earjack_manager_deinit(waitfd))) {
203 err = MM_ERROR_SOUND_INTERNAL;
210 debug_error("earjack manager exit with 0x%x\n", err);
213 int MMSoundMgrHeadsetGetType (int *type)
216 *type = avsys_audio_earjack_manager_get_type();
218 return MM_ERROR_NONE;
222 int MMSoundMgrHeadsetInit(void)
226 MMSoundThreadPoolRun(NULL, __headset_main_run);
229 return MM_ERROR_NONE;
232 int MMSoundMgrHeadsetFini(void)
237 return MM_ERROR_NONE;