Merge "Removed unused result variable from sound_codec_asm_callback" into tizen
[platform/core/multimedia/libmm-sound.git] / server / mm_sound_mgr_headset.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 <stdlib.h>
23 #include <string.h>
24
25 #include <pthread.h>
26 #include <sys/shm.h>
27 #include <sys/msg.h>
28 #include <sys/mman.h>
29 #include <sys/stat.h>
30 #include <fcntl.h>
31 #include <stdbool.h>
32
33 #include <errno.h>
34
35 #include "include/mm_sound_mgr_common.h"
36 #include "include/mm_sound_thread_pool.h"
37 #include "../include/mm_sound_common.h"
38
39 #include <mm_error.h>
40 #include <mm_debug.h>
41
42 //#include <audio-session-manager.h>
43 #include <avsys-audio.h>
44
45 #include "include/mm_sound_mgr_headset.h"
46 //#include "include/mm_sound_mgr_asm.h"
47 #include "include/mm_sound_mgr_session.h"
48
49 /******************************* Headset Code **********************************/
50
51 #include <stdio.h>
52 #include <sys/types.h>
53 #include <sys/stat.h>
54 #include <stdbool.h>
55 #include <errno.h>
56 #include <assert.h>
57
58 #include <mm_error.h>
59 #include <mm_debug.h>
60 #include <pthread.h>
61 #include <avsys-audio.h>
62
63 #include "mm_ipc.h"
64 #include "mm_sound_common.h"
65 #include "mm_sound.h"
66
67 #include <string.h>
68
69 #define EARJACK_EJECTED 0
70 #define NO_FORCE_RESET  0
71
72 void __headset_main_run (void* param)
73 {
74         int current_type = 0;
75         int new_type = 0;
76         int waitfd = 0;
77         int err = AVSYS_STATE_SUCCESS;
78         int eject_event_count = 0;
79         int need_mute = 0;
80         int ret = 0;
81
82         /* re-check current path */
83         avsys_audio_path_ex_reset(NO_FORCE_RESET);
84
85         if (AVSYS_FAIL(avsys_audio_earjack_manager_init(&current_type, &waitfd)))
86                 return;
87
88         while(1) {
89                 //waiting earjack event
90                 err = avsys_audio_earjack_manager_wait(waitfd, &current_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;
98                                         goto fail;
99                                 }
100                         }
101 #endif
102                         break;
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;
108                                         goto fail;
109                                 }
110                         }
111                         continue; /* Ignore current changes and do wait again */
112                 }
113                 debug_warning("Current type is %d, New type is %d\n", current_type, new_type);
114
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;
119                                 goto fail;
120                         }
121                         continue; /* Ignore current changes and do wait again */
122                 } else {
123                         current_type = new_type;
124                 }
125                 debug_warning("Current type is %d\n", current_type);
126
127                 /* mute if needed, unmute will be done end of this loop */
128                 if (need_mute) {
129                         if (AVSYS_FAIL(avsys_audio_set_global_mute(AVSYS_AUDIO_MUTE_NOLOCK)))
130                                 debug_error("Set mute failed\n");
131                 }
132
133                 //current_type '0' means earjack ejected
134                 if (current_type == EARJACK_EJECTED) {
135                         eject_event_count++;
136                         if (eject_event_count == 1) {
137                                 debug_msg ("earjack [EJECTED]\n");
138
139                                 /* ToDo: Device Update */
140                                 MMSoundMgrSessionSetDeviceAvailable (DEVICE_WIRED, NOT_AVAILABLE, 0, NULL);
141
142                         }
143                 } else if (current_type != EARJACK_EJECTED) { /* INSERT */
144                         debug_msg ("earjack is [INSERTED]\n");
145                         eject_event_count = 0;
146
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);
152                         }
153                 }
154
155                 //process change
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);
159                         if (need_mute) {
160                                 if (AVSYS_FAIL(avsys_audio_set_global_mute(AVSYS_AUDIO_UNMUTE_NOLOCK))) {
161                                         debug_error("Set unmute failed\n");
162                                 }
163                         }
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;
168                                 goto fail;
169                         }
170 #endif
171                         break;
172                 } else if (err & AVSYS_STATE_WARING) {
173                         debug_error("Earjack Managing Warning 0x%x\n", err);
174                         if (need_mute) {
175                                 if (AVSYS_FAIL(avsys_audio_set_global_mute(AVSYS_AUDIO_UNMUTE_NOLOCK))) {
176                                         debug_error("Set unmute failed\n");
177                                 }
178                         }
179
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;
183                                 goto fail;
184                         }
185                         continue;
186                 }
187
188                 /* Unmute if muted */
189                 if (need_mute) {
190                         if (AVSYS_FAIL(avsys_audio_set_global_mute(AVSYS_AUDIO_UNMUTE_NOLOCK))) {
191                                 debug_error("Set unmute failed\n");
192                         }
193                 }
194
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;
198                         goto fail;
199                 }
200         } /* while (1) */
201
202         if (AVSYS_FAIL(avsys_audio_earjack_manager_deinit(waitfd))) {
203                 err = MM_ERROR_SOUND_INTERNAL;
204                 goto fail;
205         }
206
207         return;
208
209 fail:
210         debug_error("earjack manager exit with 0x%x\n", err);
211 }
212
213 int MMSoundMgrHeadsetGetType (int *type)
214 {
215         if (type) {
216                 *type = avsys_audio_earjack_manager_get_type();
217         }
218         return MM_ERROR_NONE;
219 }
220
221
222 int MMSoundMgrHeadsetInit(void)
223 {
224         debug_enter("\n");
225
226         MMSoundThreadPoolRun(NULL, __headset_main_run);
227
228         debug_leave("\n");
229         return MM_ERROR_NONE;
230 }
231
232 int MMSoundMgrHeadsetFini(void)
233 {
234         debug_enter("\n");
235
236         debug_leave("\n");
237         return MM_ERROR_NONE;
238 }
239