Add missing implementations
[platform/adaptation/emulator/audio-hal-emul.git] / tizen-audio-impl-ctrl.c
1 /*
2  * audio-hal
3  *
4  * Copyright (c) 2016 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #include <config.h>
22 #endif
23
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <pthread.h>
28
29 #include "tizen-audio-internal.h"
30
31 #ifdef __MIXER_PARAM_DUMP
32 static void __dump_mixer_param(char *dump, long *param, int size)
33 {
34     int i, len;
35
36     for (i = 0; i < size; i++) {
37         len = sprintf(dump, "%ld", *param);
38         if (len > 0)
39             dump += len;
40         if (i != size -1)
41             *dump++ = ',';
42
43         param++;
44     }
45     *dump = '\0';
46 }
47 #endif
48
49 audio_return_e _mixer_control_init(audio_hal_s *ah)
50 {
51     AUDIO_RETURN_VAL_IF_FAIL(ah, AUDIO_ERR_PARAMETER);
52
53     pthread_mutex_init(&(ah->mixer.mutex), NULL);
54     return AUDIO_RET_OK;
55 }
56
57 audio_return_e _mixer_control_deinit(audio_hal_s *ah)
58 {
59     AUDIO_RETURN_VAL_IF_FAIL(ah, AUDIO_ERR_PARAMETER);
60
61     pthread_mutex_destroy(&(ah->mixer.mutex));
62     return AUDIO_RET_OK;
63 }
64
65 audio_return_e _mixer_control_set_param(audio_hal_s *ah, const char* ctl_name, snd_ctl_elem_value_t* param, int size)
66 {
67     AUDIO_RETURN_VAL_IF_FAIL(ah, AUDIO_ERR_PARAMETER);
68
69     /* TODO. */
70     return AUDIO_RET_OK;
71 }
72
73 audio_return_e _mixer_control_get_value(audio_hal_s *ah, const char *card, const char *ctl_name, int *val)
74 {
75     snd_ctl_t *handle;
76     snd_ctl_elem_value_t *control;
77     snd_ctl_elem_id_t *id;
78     snd_ctl_elem_info_t *info;
79     snd_ctl_elem_type_t type;
80
81     int ret = 0, count = 0, i = 0;
82
83     AUDIO_RETURN_VAL_IF_FAIL(ah, AUDIO_ERR_PARAMETER);
84     AUDIO_RETURN_VAL_IF_FAIL(card, AUDIO_ERR_PARAMETER);
85     AUDIO_RETURN_VAL_IF_FAIL(ctl_name, AUDIO_ERR_PARAMETER);
86     AUDIO_RETURN_VAL_IF_FAIL(val, AUDIO_ERR_PARAMETER);
87
88     pthread_mutex_lock(&(ah->mixer.mutex));
89
90     ret = snd_ctl_open(&handle, card, 0);
91     if (ret < 0) {
92         AUDIO_LOG_ERROR("snd_ctl_open error, %s\n", snd_strerror(ret));
93         pthread_mutex_unlock(&(ah->mixer.mutex));
94         return AUDIO_ERR_IOCTL;
95     }
96
97     // Get Element Info
98
99     snd_ctl_elem_id_alloca(&id);
100     snd_ctl_elem_info_alloca(&info);
101     snd_ctl_elem_value_alloca(&control);
102
103     snd_ctl_elem_id_set_interface(id, SND_CTL_ELEM_IFACE_MIXER);
104     snd_ctl_elem_id_set_name(id, ctl_name);
105
106     snd_ctl_elem_info_set_id(info, id);
107     if (snd_ctl_elem_info(handle, info) < 0) {
108         AUDIO_LOG_ERROR("Cannot find control element: %s\n", ctl_name);
109         goto close;
110     }
111     snd_ctl_elem_info_get_id(info, id);
112
113     type = snd_ctl_elem_info_get_type(info);
114     count = snd_ctl_elem_info_get_count(info);
115
116     snd_ctl_elem_value_set_id(control, id);
117
118     if (snd_ctl_elem_read(handle, control) < 0) {
119         AUDIO_LOG_ERROR("snd_ctl_elem_read failed \n");
120         goto close;
121 }
122
123     switch (type) {
124     case SND_CTL_ELEM_TYPE_BOOLEAN:
125         *val = snd_ctl_elem_value_get_boolean(control, i);
126         break;
127     case SND_CTL_ELEM_TYPE_INTEGER:
128         for (i = 0; i < count; i++)
129         *val = snd_ctl_elem_value_get_integer(control, i);
130         break;
131     case SND_CTL_ELEM_TYPE_ENUMERATED:
132         for (i = 0; i < count; i++)
133         *val = snd_ctl_elem_value_get_enumerated(control, i);
134         break;
135     default:
136         AUDIO_LOG_WARN("unsupported control element type\n");
137         goto close;
138     }
139
140     snd_ctl_close(handle);
141
142 #ifdef AUDIO_DEBUG
143     AUDIO_LOG_INFO("get mixer(%s) = %d success", ctl_name, *val);
144 #endif
145
146     pthread_mutex_unlock(&(ah->mixer.mutex));
147     return AUDIO_RET_OK;
148
149 close:
150     AUDIO_LOG_ERROR("Error\n");
151     snd_ctl_close(handle);
152     pthread_mutex_unlock(&(ah->mixer.mutex));
153     return AUDIO_ERR_UNDEFINED;
154 }
155
156 audio_return_e _mixer_control_set_value(audio_hal_s *ah, const char *card, const char *ctl_name, int val)
157 {
158     snd_ctl_t *handle;
159     snd_ctl_elem_value_t *control;
160     snd_ctl_elem_id_t *id;
161     snd_ctl_elem_info_t *info;
162     snd_ctl_elem_type_t type;
163     int ret = 0, count = 0, i = 0;
164
165     AUDIO_RETURN_VAL_IF_FAIL(ah, AUDIO_ERR_PARAMETER);
166     AUDIO_RETURN_VAL_IF_FAIL(card, AUDIO_ERR_PARAMETER);
167     AUDIO_RETURN_VAL_IF_FAIL(ctl_name, AUDIO_ERR_PARAMETER);
168
169     pthread_mutex_lock(&(ah->mixer.mutex));
170
171     ret = snd_ctl_open(&handle, card, 0);
172     if (ret < 0) {
173         AUDIO_LOG_ERROR("snd_ctl_open error, card: %s: %s", card, snd_strerror(ret));
174         pthread_mutex_unlock(&(ah->mixer.mutex));
175         return AUDIO_ERR_IOCTL;
176     }
177
178     // Get Element Info
179
180     snd_ctl_elem_id_alloca(&id);
181     snd_ctl_elem_info_alloca(&info);
182     snd_ctl_elem_value_alloca(&control);
183
184     snd_ctl_elem_id_set_interface(id, SND_CTL_ELEM_IFACE_MIXER);
185     snd_ctl_elem_id_set_name(id, ctl_name);
186
187     snd_ctl_elem_info_set_id(info, id);
188     if (snd_ctl_elem_info(handle, info) < 0) {
189         AUDIO_LOG_ERROR("Cannot find control element: %s", ctl_name);
190         goto close;
191     }
192     snd_ctl_elem_info_get_id(info, id);
193
194     type = snd_ctl_elem_info_get_type(info);
195     count = snd_ctl_elem_info_get_count(info);
196
197     snd_ctl_elem_value_set_id(control, id);
198
199     snd_ctl_elem_read(handle, control);
200
201     switch (type) {
202     case SND_CTL_ELEM_TYPE_BOOLEAN:
203         for (i = 0; i < count; i++)
204             snd_ctl_elem_value_set_boolean(control, i, val);
205         break;
206     case SND_CTL_ELEM_TYPE_INTEGER:
207         for (i = 0; i < count; i++)
208             snd_ctl_elem_value_set_integer(control, i, val);
209         break;
210     case SND_CTL_ELEM_TYPE_ENUMERATED:
211         for (i = 0; i < count; i++)
212             snd_ctl_elem_value_set_enumerated(control, i, val);
213         break;
214
215     default:
216         AUDIO_LOG_WARN("unsupported control element type");
217         goto close;
218     }
219
220     snd_ctl_elem_write(handle, control);
221
222     snd_ctl_close(handle);
223
224     AUDIO_LOG_INFO("set mixer(%s) = %d success", ctl_name, val);
225
226     pthread_mutex_unlock(&(ah->mixer.mutex));
227     return AUDIO_RET_OK;
228
229 close:
230     AUDIO_LOG_ERROR("Error");
231     snd_ctl_close(handle);
232     pthread_mutex_unlock(&(ah->mixer.mutex));
233     return AUDIO_ERR_UNDEFINED;
234 }
235
236 audio_return_e _mixer_control_set_value_string(audio_hal_s *ah, const char* ctl_name, const char* value)
237 {
238     AUDIO_RETURN_VAL_IF_FAIL(ah, AUDIO_ERR_PARAMETER);
239     AUDIO_RETURN_VAL_IF_FAIL(ctl_name, AUDIO_ERR_PARAMETER);
240
241     /* TODO. */
242     return AUDIO_RET_OK;
243 }
244
245
246 audio_return_e _mixer_control_get_element(audio_hal_s *ah, const char *ctl_name, snd_hctl_elem_t **elem)
247 {
248     AUDIO_RETURN_VAL_IF_FAIL(ah, AUDIO_ERR_PARAMETER);
249     AUDIO_RETURN_VAL_IF_FAIL(ctl_name, AUDIO_ERR_PARAMETER);
250     AUDIO_RETURN_VAL_IF_FAIL(elem, AUDIO_ERR_PARAMETER);
251
252     /* TODO. */
253     return AUDIO_RET_OK;
254 }