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
38static void tinymix_list_controls(struct mixer *mixer);
39static int tinymix_detail_control(struct mixer *mixer, const char *control,
40                                  int prefix, int print_all);
41static int tinymix_set_value(struct mixer *mixer, const char *control,
42                             char **values, unsigned int num_values);
43static void tinymix_print_enum(struct mixer_ctl *ctl, const char *space,
44                               int print_all);
45
46static const char *tinymix_short_options = "D:atvh";
47static 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
56static int g_tabs_only = 0;
57static int g_all_values = 0;
58static int g_value_only = 0;
59
60static 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
71int 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
128static void tinymix_list_controls(struct mixer *mixer)
129{
130    struct mixer_ctl *ctl;
131    const char *name, *type;
132    unsigned int num_ctls, num_values;
133    unsigned int i;
134
135    num_ctls = mixer_get_num_ctls(mixer);
136
137    printf("Number of controls: %d\n", num_ctls);
138
139    if (g_tabs_only)
140        printf("ctl\ttype\tnum\tname\tvalue");
141    else
142        printf("ctl\ttype\tnum\t%-40s value\n", "name");
143    if (g_all_values)
144        printf("\trange/values\n");
145    else
146        printf("\n");
147    for (i = 0; i < num_ctls; i++) {
148        ctl = mixer_get_ctl(mixer, i);
149
150        name = mixer_ctl_get_name(ctl);
151        type = mixer_ctl_get_type_string(ctl);
152        num_values = mixer_ctl_get_num_values(ctl);
153        if (g_tabs_only)
154            printf("%d\t%s\t%d\t%s\t", i, type, num_values, name);
155        else
156            printf("%d\t%s\t%d\t%-40s ", i, type, num_values, name);
157        tinymix_detail_control(mixer, name, 0, g_all_values);
158    }
159}
160
161static void tinymix_print_enum(struct mixer_ctl *ctl, const char *space,
162                               int print_all)
163{
164    unsigned int num_enums;
165    unsigned int i;
166    const char *string;
167    int control_value = mixer_ctl_get_value(ctl, 0);
168
169    if (print_all) {
170        num_enums = mixer_ctl_get_num_enums(ctl);
171        for (i = 0; i < num_enums; i++) {
172            string = mixer_ctl_get_enum_string(ctl, i);
173            printf("%s%s%s",
174                   control_value == (int)i ? ">" : "", string,
175                   (i < num_enums - 1) ? space : "");
176        }
177    }
178    else {
179        string = mixer_ctl_get_enum_string(ctl, control_value);
180        printf("%s", string);
181    }
182}
183
184static int tinymix_detail_control(struct mixer *mixer, const char *control,
185                                  int prefix, int print_all)
186{
187    struct mixer_ctl *ctl;
188    enum mixer_ctl_type type;
189    unsigned int num_values;
190    unsigned int i;
191    int min, max;
192    int ret;
193    char *buf = NULL;
194    size_t len;
195    unsigned int tlv_header_size = 0;
196    const char *space = g_tabs_only ? "\t" : " ";
197
198    if (isdigit(control[0]))
199        ctl = mixer_get_ctl(mixer, atoi(control));
200    else
201        ctl = mixer_get_ctl_by_name(mixer, control);
202
203    if (!ctl) {
204        fprintf(stderr, "Invalid mixer control: %s\n", control);
205        return ENOENT;
206    }
207
208    type = mixer_ctl_get_type(ctl);
209    num_values = mixer_ctl_get_num_values(ctl);
210
211    if (type == MIXER_CTL_TYPE_BYTE) {
212        if (mixer_ctl_is_access_tlv_rw(ctl)) {
213            tlv_header_size = TLV_HEADER_SIZE;
214        }
215        buf = calloc(1, num_values + tlv_header_size);
216        if (buf == NULL) {
217            fprintf(stderr, "Failed to alloc mem for bytes %d\n", num_values);
218            return ENOENT;
219        }
220
221        len = num_values;
222        ret = mixer_ctl_get_array(ctl, buf, len + tlv_header_size);
223        if (ret < 0) {
224            fprintf(stderr, "Failed to mixer_ctl_get_array\n");
225            free(buf);
226            return ENOENT;
227        }
228    }
229
230    if (prefix)
231        printf("%s:%s", mixer_ctl_get_name(ctl), space);
232
233    for (i = 0; i < num_values; i++) {
234        switch (type)
235        {
236        case MIXER_CTL_TYPE_INT:
237            printf("%d", mixer_ctl_get_value(ctl, i));
238            break;
239        case MIXER_CTL_TYPE_BOOL:
240            printf("%s", mixer_ctl_get_value(ctl, i) ? "On" : "Off");
241            break;
242        case MIXER_CTL_TYPE_ENUM:
243            tinymix_print_enum(ctl, space, print_all);
244            break;
245        case MIXER_CTL_TYPE_BYTE:
246            /* skip printing TLV header if exists */
247            printf(" %02x", buf[i + tlv_header_size]);
248            break;
249        default:
250            printf("unknown");
251            break;
252        }
253
254        if (i < num_values - 1)
255            printf("%s", space);
256    }
257
258    if (print_all) {
259        if (type == MIXER_CTL_TYPE_INT) {
260            min = mixer_ctl_get_range_min(ctl);
261            max = mixer_ctl_get_range_max(ctl);
262            printf("%s(dsrange %d->%d)", space, min, max);
263        }
264    }
265
266    free(buf);
267
268    printf("\n");
269    return 0;
270}
271
272static void tinymix_set_byte_ctl(struct mixer_ctl *ctl,
273    char **values, unsigned int num_values)
274{
275    int ret;
276    char *buf;
277    char *end;
278    unsigned int i;
279    long n;
280    unsigned int *tlv, tlv_size;
281    unsigned int tlv_header_size = 0;
282
283    if (mixer_ctl_is_access_tlv_rw(ctl)) {
284        tlv_header_size = TLV_HEADER_SIZE;
285    }
286
287    tlv_size = num_values + tlv_header_size;
288
289    buf = calloc(1, tlv_size);
290    if (buf == NULL) {
291        fprintf(stderr, "set_byte_ctl: Failed to alloc mem for bytes %d\n", num_values);
292        exit(EXIT_FAILURE);
293    }
294
295    tlv = (unsigned int *)buf;
296    tlv[0] = 0;
297    tlv[1] = num_values;
298
299    for (i = 0; i < num_values; i++) {
300        errno = 0;
301        n = strtol(values[i], &end, 0);
302        if (*end) {
303            fprintf(stderr, "%s not an integer\n", values[i]);
304            goto fail;
305        }
306        if (errno) {
307            fprintf(stderr, "strtol: %s: %s\n", values[i],
308                strerror(errno));
309            goto fail;
310        }
311        if (n < 0 || n > 0xff) {
312            fprintf(stderr, "%s should be between [0, 0xff]\n",
313                values[i]);
314            goto fail;
315        }
316        /* start filling after the TLV header */
317        buf[i + tlv_header_size] = n;
318    }
319
320    ret = mixer_ctl_set_array(ctl, buf, tlv_size);
321    if (ret < 0) {
322        fprintf(stderr, "Failed to set binary control\n");
323        goto fail;
324    }
325
326    free(buf);
327    return;
328
329fail:
330    free(buf);
331    exit(EXIT_FAILURE);
332}
333
334static int tinymix_set_value(struct mixer *mixer, const char *control,
335                             char **values, unsigned int num_values)
336{
337    struct mixer_ctl *ctl;
338    enum mixer_ctl_type type;
339    unsigned int num_ctl_values;
340    unsigned int i;
341
342    if (isdigit(control[0]))
343        ctl = mixer_get_ctl(mixer, atoi(control));
344    else
345        ctl = mixer_get_ctl_by_name(mixer, control);
346
347    if (!ctl) {
348        fprintf(stderr, "Invalid mixer control: %s\n", control);
349        return ENOENT;
350    }
351
352    type = mixer_ctl_get_type(ctl);
353    num_ctl_values = mixer_ctl_get_num_values(ctl);
354
355    if (type == MIXER_CTL_TYPE_BYTE) {
356        tinymix_set_byte_ctl(ctl, values, num_values);
357        return ENOENT;
358    }
359
360    if (isdigit(values[0][0])) {
361        if (num_values == 1) {
362            /* Set all values the same */
363            int value = atoi(values[0]);
364
365            for (i = 0; i < num_ctl_values; i++) {
366                if (mixer_ctl_set_value(ctl, i, value)) {
367                    fprintf(stderr, "Error: invalid value\n");
368                    return EINVAL;
369                }
370            }
371        } else {
372            /* Set multiple values */
373            if (num_values > num_ctl_values) {
374                fprintf(stderr,
375                        "Error: %d values given, but control only takes %d\n",
376                        num_values, num_ctl_values);
377                return EINVAL;
378            }
379            for (i = 0; i < num_values; i++) {
380                if (mixer_ctl_set_value(ctl, i, atoi(values[i]))) {
381                    fprintf(stderr, "Error: invalid value for index %d\n", i);
382                    return EINVAL;
383                }
384            }
385        }
386    } else {
387        if (type == MIXER_CTL_TYPE_ENUM) {
388            if (num_values != 1) {
389                fprintf(stderr, "Enclose strings in quotes and try again\n");
390                return EINVAL;
391            }
392            if (mixer_ctl_set_enum_by_string(ctl, values[0])) {
393                fprintf(stderr, "Error: invalid enum value\n");
394                return EINVAL;
395            }
396        } else {
397            fprintf(stderr, "Error: only enum types can be set with strings\n");
398            return EINVAL;
399        }
400    }
401
402    return 0;
403}
404