1 /* tinymix.c
2 **
3 ** Copyright 2011, The Android Open Source Project
4 **
5 ** Redistribution and use in source and binary forms, with or without
6 ** modification, are permitted provided that the following conditions are met:
7 ** * Redistributions of source code must retain the above copyright
8 ** notice, this list of conditions and the following disclaimer.
9 ** * Redistributions in binary form must reproduce the above copyright
10 ** notice, this list of conditions and the following disclaimer in the
11 ** documentation and/or other materials provided with the distribution.
12 ** * Neither the name of The Android Open Source Project nor the names of
13 ** its contributors may be used to endorse or promote products derived
14 ** from this software without specific prior written permission.
15 **
16 ** THIS SOFTWARE IS PROVIDED BY The Android Open Source Project ``AS IS'' AND
17 ** ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 ** IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 ** ARE DISCLAIMED. IN NO EVENT SHALL The Android Open Source Project BE LIABLE
20 ** FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 ** DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22 ** SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
23 ** CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 ** LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 ** OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
26 ** DAMAGE.
27 */
28
29 #include <tinyalsa/asoundlib.h>
30 #include <errno.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <ctype.h>
34 #include <string.h>
35 #include <getopt.h>
36 #include <errno.h>
37
38 static void tinymix_list_controls(struct mixer *mixer);
39 static int tinymix_detail_control(struct mixer *mixer, const char *control,
40 int prefix, int print_all);
41 static int tinymix_set_value(struct mixer *mixer, const char *control,
42 char **values, unsigned int num_values);
43 static void tinymix_print_enum(struct mixer_ctl *ctl, const char *space,
44 int print_all);
45
46 static const char *tinymix_short_options = "D:atvh";
47 static struct option tinymix_long_options[] = {
48 {"device", required_argument, 0, 'D'},
49 {"all-values", no_argument, 0, 'a'},
50 {"tabs-only", no_argument, 0, 't'},
51 {"value-only", no_argument, 0, 'v'},
52 {"help", no_argument, 0, 'h'},
53 {0, 0, 0, 0}
54 };
55
56 static int g_tabs_only = 0;
57 static int g_all_values = 0;
58 static int g_value_only = 0;
59
usage(void)60 static void usage (void) {
61 fprintf(stderr,
62 "tinymix [options] [control name/#] [value to set]\n"
63 " options:\n"
64 " --device|-D <card#> - use the given card # instead of 0.\n"
65 " --all-values|-a - show all possible values/ranges for control.\n"
66 " --tabs-only|-t - separate all output columns/values with tabs.\n"
67 " --value-only|-v - show only the value for the selected control.\n"
68 );
69 }
70
main(int argc,char ** argv)71 int main(int argc, char **argv)
72 {
73 struct mixer *mixer;
74 int card = 0;
75 int ret = 0;
76
77 while (1) {
78 int option_index = 0;
79 int option_char = 0;
80
81 option_char = getopt_long(argc, argv, tinymix_short_options,
82 tinymix_long_options, &option_index);
83 if (option_char == -1)
84 break;
85
86 switch (option_char) {
87 case 'D':
88 card = atoi(optarg);
89 break;
90 case 'a':
91 g_all_values = 1;
92 break;
93 case 't':
94 g_tabs_only = 1;
95 break;
96 case 'v':
97 g_value_only = 1;
98 break;
99 case 'h':
100 usage();
101 return 0;
102 default:
103 usage();
104 return EINVAL;
105 }
106 }
107
108 mixer = mixer_open(card);
109 if (!mixer) {
110 fprintf(stderr, "Failed to open mixer\n");
111 return ENODEV;
112 }
113
114 if (argc == optind) {
115 printf("Mixer name: '%s'\n", mixer_get_name(mixer));
116 tinymix_list_controls(mixer);
117 } else if (argc == optind + 1) {
118 ret = tinymix_detail_control(mixer, argv[optind], !g_value_only, !g_value_only);
119 } else if (argc >= optind + 2) {
120 ret = tinymix_set_value(mixer, argv[optind], &argv[optind + 1], argc - optind - 1);
121 }
122
123 mixer_close(mixer);
124
125 return ret;
126 }
127
isnumber(const char * str)128 static int isnumber(const char *str) {
129 char *end;
130
131 if (str == NULL || strlen(str) == 0)
132 return 0;
133
134 strtol(str, &end, 0);
135 return strlen(end) == 0;
136 }
137
tinymix_list_controls(struct mixer * mixer)138 static void tinymix_list_controls(struct mixer *mixer)
139 {
140 struct mixer_ctl *ctl;
141 const char *name, *type;
142 unsigned int num_ctls, num_values;
143 unsigned int i;
144
145 num_ctls = mixer_get_num_ctls(mixer);
146
147 printf("Number of controls: %u\n", num_ctls);
148
149 if (g_tabs_only)
150 printf("ctl\ttype\tnum\tname\tvalue");
151 else
152 printf("ctl\ttype\tnum\t%-40s value\n", "name");
153 if (g_all_values)
154 printf("\trange/values\n");
155 else
156 printf("\n");
157 for (i = 0; i < num_ctls; i++) {
158 ctl = mixer_get_ctl(mixer, i);
159
160 name = mixer_ctl_get_name(ctl);
161 type = mixer_ctl_get_type_string(ctl);
162 num_values = mixer_ctl_get_num_values(ctl);
163 if (g_tabs_only)
164 printf("%d\t%s\t%d\t%s\t", i, type, num_values, name);
165 else
166 printf("%d\t%s\t%d\t%-40s ", i, type, num_values, name);
167 tinymix_detail_control(mixer, name, 0, g_all_values);
168 }
169 }
170
tinymix_print_enum(struct mixer_ctl * ctl,const char * space,int print_all)171 static void tinymix_print_enum(struct mixer_ctl *ctl, const char *space,
172 int print_all)
173 {
174 unsigned int num_enums;
175 unsigned int i;
176 const char *string;
177 int control_value = mixer_ctl_get_value(ctl, 0);
178
179 if (print_all) {
180 num_enums = mixer_ctl_get_num_enums(ctl);
181 for (i = 0; i < num_enums; i++) {
182 string = mixer_ctl_get_enum_string(ctl, i);
183 printf("%s%s%s",
184 control_value == (int)i ? ">" : "", string,
185 (i < num_enums - 1) ? space : "");
186 }
187 }
188 else {
189 string = mixer_ctl_get_enum_string(ctl, control_value);
190 printf("%s", string);
191 }
192 }
193
tinymix_detail_control(struct mixer * mixer,const char * control,int prefix,int print_all)194 static int tinymix_detail_control(struct mixer *mixer, const char *control,
195 int prefix, int print_all)
196 {
197 struct mixer_ctl *ctl;
198 enum mixer_ctl_type type;
199 unsigned int num_values;
200 unsigned int i;
201 int min, max;
202 int ret;
203 char *buf = NULL;
204 size_t len;
205 unsigned int tlv_header_size = 0;
206 const char *space = g_tabs_only ? "\t" : " ";
207
208 if (isnumber(control))
209 ctl = mixer_get_ctl(mixer, atoi(control));
210 else
211 ctl = mixer_get_ctl_by_name(mixer, control);
212
213 if (!ctl) {
214 fprintf(stderr, "Invalid mixer control: %s\n", control);
215 return ENOENT;
216 }
217
218 type = mixer_ctl_get_type(ctl);
219 num_values = mixer_ctl_get_num_values(ctl);
220
221 if (type == MIXER_CTL_TYPE_BYTE) {
222 if (mixer_ctl_is_access_tlv_rw(ctl)) {
223 tlv_header_size = TLV_HEADER_SIZE;
224 }
225 buf = calloc(1, num_values + tlv_header_size);
226 if (buf == NULL) {
227 fprintf(stderr, "Failed to alloc mem for bytes %d\n", num_values);
228 return ENOENT;
229 }
230
231 len = num_values;
232 ret = mixer_ctl_get_array(ctl, buf, len + tlv_header_size);
233 if (ret < 0) {
234 fprintf(stderr, "Failed to mixer_ctl_get_array\n");
235 free(buf);
236 return ENOENT;
237 }
238 }
239
240 if (prefix)
241 printf("%s:%s", mixer_ctl_get_name(ctl), space);
242
243 for (i = 0; i < num_values; i++) {
244 switch (type)
245 {
246 case MIXER_CTL_TYPE_INT:
247 printf("%d", mixer_ctl_get_value(ctl, i));
248 break;
249 case MIXER_CTL_TYPE_BOOL:
250 printf("%s", mixer_ctl_get_value(ctl, i) ? "On" : "Off");
251 break;
252 case MIXER_CTL_TYPE_ENUM:
253 tinymix_print_enum(ctl, space, print_all);
254 break;
255 case MIXER_CTL_TYPE_BYTE:
256 /* skip printing TLV header if exists */
257 printf(" %02x", buf[i + tlv_header_size]);
258 break;
259 default:
260 printf("unknown");
261 break;
262 }
263
264 if (i < num_values - 1)
265 printf("%s", space);
266 }
267
268 if (print_all) {
269 if (type == MIXER_CTL_TYPE_INT) {
270 min = mixer_ctl_get_range_min(ctl);
271 max = mixer_ctl_get_range_max(ctl);
272 printf("%s(dsrange %d->%d)", space, min, max);
273 }
274 }
275
276 free(buf);
277
278 printf("\n");
279 return 0;
280 }
281
tinymix_set_byte_ctl(struct mixer_ctl * ctl,char ** values,unsigned int num_values)282 static void tinymix_set_byte_ctl(struct mixer_ctl *ctl,
283 char **values, unsigned int num_values)
284 {
285 int ret;
286 char *buf;
287 char *end;
288 unsigned int i;
289 long n;
290 unsigned int *tlv, tlv_size;
291 unsigned int tlv_header_size = 0;
292
293 if (mixer_ctl_is_access_tlv_rw(ctl)) {
294 tlv_header_size = TLV_HEADER_SIZE;
295 }
296
297 tlv_size = num_values + tlv_header_size;
298
299 buf = calloc(1, tlv_size);
300 if (buf == NULL) {
301 fprintf(stderr, "set_byte_ctl: Failed to alloc mem for bytes %d\n", num_values);
302 exit(EXIT_FAILURE);
303 }
304
305 tlv = (unsigned int *)buf;
306 tlv[0] = 0;
307 tlv[1] = num_values;
308
309 for (i = 0; i < num_values; i++) {
310 errno = 0;
311 n = strtol(values[i], &end, 0);
312 if (*end) {
313 fprintf(stderr, "%s not an integer\n", values[i]);
314 goto fail;
315 }
316 if (errno) {
317 fprintf(stderr, "strtol: %s: %s\n", values[i],
318 strerror(errno));
319 goto fail;
320 }
321 if (n < 0 || n > 0xff) {
322 fprintf(stderr, "%s should be between [0, 0xff]\n",
323 values[i]);
324 goto fail;
325 }
326 /* start filling after the TLV header */
327 buf[i + tlv_header_size] = n;
328 }
329
330 ret = mixer_ctl_set_array(ctl, buf, tlv_size);
331 if (ret < 0) {
332 fprintf(stderr, "Failed to set binary control\n");
333 goto fail;
334 }
335
336 free(buf);
337 return;
338
339 fail:
340 free(buf);
341 exit(EXIT_FAILURE);
342 }
343
tinymix_set_value(struct mixer * mixer,const char * control,char ** values,unsigned int num_values)344 static int tinymix_set_value(struct mixer *mixer, const char *control,
345 char **values, unsigned int num_values)
346 {
347 struct mixer_ctl *ctl;
348 enum mixer_ctl_type type;
349 unsigned int num_ctl_values;
350 unsigned int i;
351
352 if (isnumber(control))
353 ctl = mixer_get_ctl(mixer, atoi(control));
354 else
355 ctl = mixer_get_ctl_by_name(mixer, control);
356
357 if (!ctl) {
358 fprintf(stderr, "Invalid mixer control: %s\n", control);
359 return ENOENT;
360 }
361
362 type = mixer_ctl_get_type(ctl);
363 num_ctl_values = mixer_ctl_get_num_values(ctl);
364
365 if (type == MIXER_CTL_TYPE_BYTE) {
366 tinymix_set_byte_ctl(ctl, values, num_values);
367 return ENOENT;
368 }
369
370 if (isnumber(values[0])) {
371 if (num_values == 1) {
372 /* Set all values the same */
373 int value = atoi(values[0]);
374
375 for (i = 0; i < num_ctl_values; i++) {
376 if (mixer_ctl_set_value(ctl, i, value)) {
377 fprintf(stderr, "Error: invalid value\n");
378 return EINVAL;
379 }
380 }
381 } else {
382 /* Set multiple values */
383 if (num_values > num_ctl_values) {
384 fprintf(stderr,
385 "Error: %u values given, but control only takes %u\n",
386 num_values, num_ctl_values);
387 return EINVAL;
388 }
389 for (i = 0; i < num_values; i++) {
390 if (mixer_ctl_set_value(ctl, i, atoi(values[i]))) {
391 fprintf(stderr, "Error: invalid value for index %d\n", i);
392 return EINVAL;
393 }
394 }
395 }
396 } else {
397 if (type == MIXER_CTL_TYPE_ENUM) {
398 if (num_values != 1) {
399 fprintf(stderr, "Enclose strings in quotes and try again\n");
400 return EINVAL;
401 }
402 if (mixer_ctl_set_enum_by_string(ctl, values[0])) {
403 fprintf(stderr, "Error: invalid enum value\n");
404 return EINVAL;
405 }
406 } else {
407 fprintf(stderr, "Error: only enum types can be set with strings\n");
408 return EINVAL;
409 }
410 }
411
412 return 0;
413 }
414