OSDN Git Service

Merge "Remove old Brillo code."
[android-x86/system-media.git] / audio_route / audio_route.c
1 /*
2  * Copyright (C) 2013 The Android Open Source Project
3  * Inspired by TinyHW, written by Mark Brown at Wolfson Micro
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 #define LOG_TAG "audio_route"
19 /*#define LOG_NDEBUG 0*/
20
21 #include <errno.h>
22 #include <expat.h>
23 #include <stdbool.h>
24 #include <stdio.h>
25 #include <string.h>
26
27 #include <log/log.h>
28
29 #include <tinyalsa/asoundlib.h>
30
31 #define BUF_SIZE 1024
32 #define MIXER_XML_PATH "/system/etc/mixer_paths.xml"
33 #define INITIAL_MIXER_PATH_SIZE 8
34
35 union ctl_values {
36     int *enumerated;
37     long *integer;
38     void *ptr;
39     unsigned char *bytes;
40 };
41
42 struct mixer_state {
43     struct mixer_ctl *ctl;
44     unsigned int num_values;
45     union ctl_values old_value;
46     union ctl_values new_value;
47     union ctl_values reset_value;
48 };
49
50 struct mixer_setting {
51     unsigned int ctl_index;
52     unsigned int num_values;
53     unsigned int type;
54     union ctl_values value;
55 };
56
57 struct mixer_value {
58     unsigned int ctl_index;
59     int index;
60     long value;
61 };
62
63 struct mixer_path {
64     char *name;
65     unsigned int size;
66     unsigned int length;
67     struct mixer_setting *setting;
68 };
69
70 struct audio_route {
71     struct mixer *mixer;
72     unsigned int num_mixer_ctls;
73     struct mixer_state *mixer_state;
74
75     unsigned int mixer_path_size;
76     unsigned int num_mixer_paths;
77     struct mixer_path *mixer_path;
78 };
79
80 struct config_parse_state {
81     struct audio_route *ar;
82     struct mixer_path *path;
83     int level;
84 };
85
86 /* path functions */
87
88 static bool is_supported_ctl_type(enum mixer_ctl_type type)
89 {
90     switch (type) {
91     case MIXER_CTL_TYPE_BOOL:
92     case MIXER_CTL_TYPE_INT:
93     case MIXER_CTL_TYPE_ENUM:
94     case MIXER_CTL_TYPE_BYTE:
95         return true;
96     default:
97         return false;
98     }
99 }
100
101 /* as they match in alsa */
102 static size_t sizeof_ctl_type(enum mixer_ctl_type type) {
103     switch (type) {
104     case MIXER_CTL_TYPE_BOOL:
105     case MIXER_CTL_TYPE_INT:
106         return sizeof(long);
107     case MIXER_CTL_TYPE_ENUM:
108         return sizeof(int);
109     case MIXER_CTL_TYPE_BYTE:
110         return sizeof(unsigned char);
111     case MIXER_CTL_TYPE_INT64:
112     case MIXER_CTL_TYPE_IEC958:
113     case MIXER_CTL_TYPE_UNKNOWN:
114     default:
115         LOG_ALWAYS_FATAL("Unsupported mixer ctl type: %d, check type before calling", (int)type);
116         return 0;
117     }
118 }
119
120 static inline struct mixer_ctl *index_to_ctl(struct audio_route *ar,
121                                              unsigned int ctl_index)
122 {
123     return ar->mixer_state[ctl_index].ctl;
124 }
125
126 #if 0
127 static void path_print(struct audio_route *ar, struct mixer_path *path)
128 {
129     unsigned int i;
130     unsigned int j;
131
132     ALOGE("Path: %s, length: %d", path->name, path->length);
133     for (i = 0; i < path->length; i++) {
134         struct mixer_ctl *ctl = index_to_ctl(ar, path->setting[i].ctl_index);
135
136         ALOGE("  id=%d: ctl=%s", i, mixer_ctl_get_name(ctl));
137         if (mixer_ctl_get_type(ctl) == MIXER_CTL_TYPE_BYTE) {
138             for (j = 0; j < path->setting[i].num_values; j++)
139                 ALOGE("    id=%d value=0x%02x", j, path->setting[i].value.bytes[j]);
140         } else if (mixer_ctl_get_type(ctl) == MIXER_CTL_TYPE_ENUM) {
141             for (j = 0; j < path->setting[i].num_values; j++)
142                 ALOGE("    id=%d value=%d", j, path->setting[i].value.enumerated[j]);
143         } else {
144             for (j = 0; j < path->setting[i].num_values; j++)
145                 ALOGE("    id=%d value=%ld", j, path->setting[i].value.integer[j]);
146         }
147     }
148 }
149 #endif
150
151 static void path_free(struct audio_route *ar)
152 {
153     unsigned int i;
154
155     for (i = 0; i < ar->num_mixer_paths; i++) {
156         free(ar->mixer_path[i].name);
157         if (ar->mixer_path[i].setting) {
158             size_t j;
159             for (j = 0; j < ar->mixer_path[i].length; j++) {
160                 free(ar->mixer_path[i].setting[j].value.ptr);
161             }
162             free(ar->mixer_path[i].setting);
163             ar->mixer_path[i].size = 0;
164             ar->mixer_path[i].length = 0;
165             ar->mixer_path[i].setting = NULL;
166         }
167     }
168     free(ar->mixer_path);
169     ar->mixer_path = NULL;
170     ar->mixer_path_size = 0;
171     ar->num_mixer_paths = 0;
172 }
173
174 static struct mixer_path *path_get_by_name(struct audio_route *ar,
175                                            const char *name)
176 {
177     unsigned int i;
178
179     for (i = 0; i < ar->num_mixer_paths; i++)
180         if (strcmp(ar->mixer_path[i].name, name) == 0)
181             return &ar->mixer_path[i];
182
183     return NULL;
184 }
185
186 static struct mixer_path *path_create(struct audio_route *ar, const char *name)
187 {
188     struct mixer_path *new_mixer_path = NULL;
189
190     if (path_get_by_name(ar, name)) {
191         ALOGE("Path name '%s' already exists", name);
192         return NULL;
193     }
194
195     /* check if we need to allocate more space for mixer paths */
196     if (ar->mixer_path_size <= ar->num_mixer_paths) {
197         if (ar->mixer_path_size == 0)
198             ar->mixer_path_size = INITIAL_MIXER_PATH_SIZE;
199         else
200             ar->mixer_path_size *= 2;
201
202         new_mixer_path = realloc(ar->mixer_path, ar->mixer_path_size *
203                                  sizeof(struct mixer_path));
204         if (new_mixer_path == NULL) {
205             ALOGE("Unable to allocate more paths");
206             return NULL;
207         } else {
208             ar->mixer_path = new_mixer_path;
209         }
210     }
211
212     /* initialise the new mixer path */
213     ar->mixer_path[ar->num_mixer_paths].name = strdup(name);
214     ar->mixer_path[ar->num_mixer_paths].size = 0;
215     ar->mixer_path[ar->num_mixer_paths].length = 0;
216     ar->mixer_path[ar->num_mixer_paths].setting = NULL;
217
218     /* return the mixer path just added, then increment number of them */
219     return &ar->mixer_path[ar->num_mixer_paths++];
220 }
221
222 static int find_ctl_index_in_path(struct mixer_path *path,
223                                   unsigned int ctl_index)
224 {
225     unsigned int i;
226
227     for (i = 0; i < path->length; i++)
228         if (path->setting[i].ctl_index == ctl_index)
229             return i;
230
231     return -1;
232 }
233
234 static int alloc_path_setting(struct mixer_path *path)
235 {
236     struct mixer_setting *new_path_setting;
237     int path_index;
238
239     /* check if we need to allocate more space for path settings */
240     if (path->size <= path->length) {
241         if (path->size == 0)
242             path->size = INITIAL_MIXER_PATH_SIZE;
243         else
244             path->size *= 2;
245
246         new_path_setting = realloc(path->setting,
247                                    path->size * sizeof(struct mixer_setting));
248         if (new_path_setting == NULL) {
249             ALOGE("Unable to allocate more path settings");
250             return -1;
251         } else {
252             path->setting = new_path_setting;
253         }
254     }
255
256     path_index = path->length;
257     path->length++;
258
259     return path_index;
260 }
261
262 static int path_add_setting(struct audio_route *ar, struct mixer_path *path,
263                             struct mixer_setting *setting)
264 {
265     int path_index;
266
267     if (find_ctl_index_in_path(path, setting->ctl_index) != -1) {
268         struct mixer_ctl *ctl = index_to_ctl(ar, setting->ctl_index);
269
270         ALOGE("Control '%s' already exists in path '%s'",
271               mixer_ctl_get_name(ctl), path->name);
272         return -1;
273     }
274
275     if (!is_supported_ctl_type(setting->type)) {
276         ALOGE("unsupported type %d", (int)setting->type);
277         return -1;
278     }
279
280     path_index = alloc_path_setting(path);
281     if (path_index < 0)
282         return -1;
283
284     path->setting[path_index].ctl_index = setting->ctl_index;
285     path->setting[path_index].type = setting->type;
286     path->setting[path_index].num_values = setting->num_values;
287
288     size_t value_sz = sizeof_ctl_type(setting->type);
289
290     path->setting[path_index].value.ptr = calloc(setting->num_values, value_sz);
291     /* copy all values */
292     memcpy(path->setting[path_index].value.ptr, setting->value.ptr,
293            setting->num_values * value_sz);
294
295     return 0;
296 }
297
298 static int path_add_value(struct audio_route *ar, struct mixer_path *path,
299                           struct mixer_value *mixer_value)
300 {
301     unsigned int i;
302     int path_index;
303     unsigned int num_values;
304     struct mixer_ctl *ctl;
305
306     /* Check that mixer value index is within range */
307     ctl = index_to_ctl(ar, mixer_value->ctl_index);
308     num_values = mixer_ctl_get_num_values(ctl);
309     if (mixer_value->index >= (int)num_values) {
310         ALOGE("mixer index %d is out of range for '%s'", mixer_value->index,
311               mixer_ctl_get_name(ctl));
312         return -1;
313     }
314
315     path_index = find_ctl_index_in_path(path, mixer_value->ctl_index);
316     if (path_index < 0) {
317         /* New path */
318
319         enum mixer_ctl_type type = mixer_ctl_get_type(ctl);
320         if (!is_supported_ctl_type(type)) {
321             ALOGE("unsupported type %d", (int)type);
322             return -1;
323         }
324         path_index = alloc_path_setting(path);
325         if (path_index < 0)
326             return -1;
327
328         /* initialise the new path setting */
329         path->setting[path_index].ctl_index = mixer_value->ctl_index;
330         path->setting[path_index].num_values = num_values;
331         path->setting[path_index].type = type;
332
333         size_t value_sz = sizeof_ctl_type(type);
334         path->setting[path_index].value.ptr = calloc(num_values, value_sz);
335         if (path->setting[path_index].type == MIXER_CTL_TYPE_BYTE)
336             path->setting[path_index].value.bytes[0] = mixer_value->value;
337         else if (path->setting[path_index].type == MIXER_CTL_TYPE_ENUM)
338             path->setting[path_index].value.enumerated[0] = mixer_value->value;
339         else
340             path->setting[path_index].value.integer[0] = mixer_value->value;
341     }
342
343     if (mixer_value->index == -1) {
344         /* set all values the same */
345         if (path->setting[path_index].type == MIXER_CTL_TYPE_BYTE) {
346             for (i = 0; i < num_values; i++)
347                 path->setting[path_index].value.bytes[i] = mixer_value->value;
348         } else if (path->setting[path_index].type == MIXER_CTL_TYPE_ENUM) {
349             for (i = 0; i < num_values; i++)
350                 path->setting[path_index].value.enumerated[i] = mixer_value->value;
351         } else {
352             for (i = 0; i < num_values; i++)
353                 path->setting[path_index].value.integer[i] = mixer_value->value;
354         }
355     } else {
356         /* set only one value */
357         if (path->setting[path_index].type == MIXER_CTL_TYPE_BYTE)
358             path->setting[path_index].value.bytes[mixer_value->index] = mixer_value->value;
359         else if (path->setting[path_index].type == MIXER_CTL_TYPE_ENUM)
360             path->setting[path_index].value.enumerated[mixer_value->index] = mixer_value->value;
361         else
362             path->setting[path_index].value.integer[mixer_value->index] = mixer_value->value;
363     }
364
365     return 0;
366 }
367
368 static int path_add_path(struct audio_route *ar, struct mixer_path *path,
369                          struct mixer_path *sub_path)
370 {
371     unsigned int i;
372
373     for (i = 0; i < sub_path->length; i++)
374         if (path_add_setting(ar, path, &sub_path->setting[i]) < 0)
375             return -1;
376
377     return 0;
378 }
379
380 static int path_apply(struct audio_route *ar, struct mixer_path *path)
381 {
382     unsigned int i;
383     unsigned int ctl_index;
384     struct mixer_ctl *ctl;
385     enum mixer_ctl_type type;
386
387     for (i = 0; i < path->length; i++) {
388         ctl_index = path->setting[i].ctl_index;
389         ctl = index_to_ctl(ar, ctl_index);
390         type = mixer_ctl_get_type(ctl);
391         if (!is_supported_ctl_type(type))
392             continue;
393         size_t value_sz = sizeof_ctl_type(type);
394         memcpy(ar->mixer_state[ctl_index].new_value.ptr, path->setting[i].value.ptr,
395                    path->setting[i].num_values * value_sz);
396     }
397
398     return 0;
399 }
400
401 static int path_reset(struct audio_route *ar, struct mixer_path *path)
402 {
403     unsigned int i;
404     unsigned int ctl_index;
405     struct mixer_ctl *ctl;
406     enum mixer_ctl_type type;
407
408     for (i = 0; i < path->length; i++) {
409         ctl_index = path->setting[i].ctl_index;
410         ctl = index_to_ctl(ar, ctl_index);
411         type = mixer_ctl_get_type(ctl);
412         if (!is_supported_ctl_type(type))
413             continue;
414         size_t value_sz = sizeof_ctl_type(type);
415         /* reset the value(s) */
416         memcpy(ar->mixer_state[ctl_index].new_value.ptr,
417                ar->mixer_state[ctl_index].reset_value.ptr,
418                ar->mixer_state[ctl_index].num_values * value_sz);
419     }
420
421     return 0;
422 }
423
424 /* mixer helper function */
425 static int mixer_enum_string_to_value(struct mixer_ctl *ctl, const char *string)
426 {
427     unsigned int i;
428     unsigned int num_values = mixer_ctl_get_num_enums(ctl);
429
430     if (string == NULL) {
431         ALOGE("NULL enum value string passed to mixer_enum_string_to_value() for ctl %s",
432               mixer_ctl_get_name(ctl));
433         return 0;
434     }
435
436     /* Search the enum strings for a particular one */
437     for (i = 0; i < num_values; i++) {
438         if (strcmp(mixer_ctl_get_enum_string(ctl, i), string) == 0)
439             break;
440     }
441     if (i == num_values) {
442         ALOGE("unknown enum value string %s for ctl %s",
443               string, mixer_ctl_get_name(ctl));
444         return 0;
445     }
446     return i;
447 }
448
449 static void start_tag(void *data, const XML_Char *tag_name,
450                       const XML_Char **attr)
451 {
452     const XML_Char *attr_name = NULL;
453     const XML_Char *attr_id = NULL;
454     const XML_Char *attr_value = NULL;
455     struct config_parse_state *state = data;
456     struct audio_route *ar = state->ar;
457     unsigned int i;
458     unsigned int ctl_index;
459     struct mixer_ctl *ctl;
460     long value;
461     unsigned int id;
462     struct mixer_value mixer_value;
463     enum mixer_ctl_type type;
464
465     /* Get name, id and value attributes (these may be empty) */
466     for (i = 0; attr[i]; i += 2) {
467         if (strcmp(attr[i], "name") == 0)
468             attr_name = attr[i + 1];
469         if (strcmp(attr[i], "id") == 0)
470             attr_id = attr[i + 1];
471         else if (strcmp(attr[i], "value") == 0)
472             attr_value = attr[i + 1];
473     }
474
475     /* Look at tags */
476     if (strcmp(tag_name, "path") == 0) {
477         if (attr_name == NULL) {
478             ALOGE("Unnamed path!");
479         } else {
480             if (state->level == 1) {
481                 /* top level path: create and stash the path */
482                 state->path = path_create(ar, (char *)attr_name);
483                 if (state->path == NULL)
484                     ALOGE("path created failed, please check the path if existed");
485             } else {
486                 /* nested path */
487                 struct mixer_path *sub_path = path_get_by_name(ar, attr_name);
488                 if (!sub_path) {
489                     ALOGE("unable to find sub path '%s'", attr_name);
490                 } else if (state->path != NULL) {
491                     path_add_path(ar, state->path, sub_path);
492                 }
493             }
494         }
495     }
496
497     else if (strcmp(tag_name, "ctl") == 0) {
498         /* Obtain the mixer ctl and value */
499         ctl = mixer_get_ctl_by_name(ar->mixer, attr_name);
500         if (ctl == NULL) {
501             ALOGE("Control '%s' doesn't exist - skipping", attr_name);
502             goto done;
503         }
504
505         switch (mixer_ctl_get_type(ctl)) {
506         case MIXER_CTL_TYPE_BOOL:
507         case MIXER_CTL_TYPE_INT:
508             value = strtol((char *)attr_value, NULL, 0);
509             break;
510         case MIXER_CTL_TYPE_BYTE:
511             value = (unsigned char) strtol((char *)attr_value, NULL, 16);
512             break;
513         case MIXER_CTL_TYPE_ENUM:
514             value = mixer_enum_string_to_value(ctl, (char *)attr_value);
515             break;
516         default:
517             value = 0;
518             break;
519         }
520
521         /* locate the mixer ctl in the list */
522         for (ctl_index = 0; ctl_index < ar->num_mixer_ctls; ctl_index++) {
523             if (ar->mixer_state[ctl_index].ctl == ctl)
524                 break;
525         }
526
527         if (state->level == 1) {
528             /* top level ctl (initial setting) */
529
530             type = mixer_ctl_get_type(ctl);
531             if (is_supported_ctl_type(type)) {
532                 /* apply the new value */
533                 if (attr_id) {
534                     /* set only one value */
535                     id = atoi((char *)attr_id);
536                     if (id < ar->mixer_state[ctl_index].num_values)
537                         if (type == MIXER_CTL_TYPE_BYTE)
538                             ar->mixer_state[ctl_index].new_value.bytes[id] = value;
539                         else if (type == MIXER_CTL_TYPE_ENUM)
540                             ar->mixer_state[ctl_index].new_value.enumerated[id] = value;
541                         else
542                             ar->mixer_state[ctl_index].new_value.integer[id] = value;
543                     else
544                         ALOGE("value id out of range for mixer ctl '%s'",
545                               mixer_ctl_get_name(ctl));
546                 } else {
547                     /* set all values the same */
548                     for (i = 0; i < ar->mixer_state[ctl_index].num_values; i++)
549                         if (type == MIXER_CTL_TYPE_BYTE)
550                             ar->mixer_state[ctl_index].new_value.bytes[i] = value;
551                         else if (type == MIXER_CTL_TYPE_ENUM)
552                             ar->mixer_state[ctl_index].new_value.enumerated[i] = value;
553                         else
554                             ar->mixer_state[ctl_index].new_value.integer[i] = value;
555                 }
556             }
557         } else {
558             /* nested ctl (within a path) */
559             mixer_value.ctl_index = ctl_index;
560             mixer_value.value = value;
561             if (attr_id)
562                 mixer_value.index = atoi((char *)attr_id);
563             else
564                 mixer_value.index = -1;
565             if (state->path != NULL)
566                 path_add_value(ar, state->path, &mixer_value);
567         }
568     }
569
570 done:
571     state->level++;
572 }
573
574 static void end_tag(void *data, const XML_Char *tag_name)
575 {
576     struct config_parse_state *state = data;
577     (void)tag_name;
578
579     state->level--;
580 }
581
582 static int alloc_mixer_state(struct audio_route *ar)
583 {
584     unsigned int i;
585     unsigned int num_values;
586     struct mixer_ctl *ctl;
587     enum mixer_ctl_type type;
588
589     ar->num_mixer_ctls = mixer_get_num_ctls(ar->mixer);
590     ar->mixer_state = calloc(ar->num_mixer_ctls, sizeof(struct mixer_state));
591     if (!ar->mixer_state)
592         return -1;
593
594     for (i = 0; i < ar->num_mixer_ctls; i++) {
595         ctl = mixer_get_ctl(ar->mixer, i);
596         num_values = mixer_ctl_get_num_values(ctl);
597
598         ar->mixer_state[i].ctl = ctl;
599         ar->mixer_state[i].num_values = num_values;
600
601         /* Skip unsupported types that are not supported yet in XML */
602         type = mixer_ctl_get_type(ctl);
603
604         if (!is_supported_ctl_type(type))
605             continue;
606
607         size_t value_sz = sizeof_ctl_type(type);
608         ar->mixer_state[i].old_value.ptr = calloc(num_values, value_sz);
609         ar->mixer_state[i].new_value.ptr = calloc(num_values, value_sz);
610         ar->mixer_state[i].reset_value.ptr = calloc(num_values, value_sz);
611
612         if (type == MIXER_CTL_TYPE_ENUM)
613             ar->mixer_state[i].old_value.enumerated[0] = mixer_ctl_get_value(ctl, 0);
614         else
615             mixer_ctl_get_array(ctl, ar->mixer_state[i].old_value.ptr, num_values);
616
617         memcpy(ar->mixer_state[i].new_value.ptr, ar->mixer_state[i].old_value.ptr,
618                num_values * value_sz);
619     }
620
621     return 0;
622 }
623
624 static void free_mixer_state(struct audio_route *ar)
625 {
626     unsigned int i;
627     enum mixer_ctl_type type;
628
629     for (i = 0; i < ar->num_mixer_ctls; i++) {
630         type = mixer_ctl_get_type(ar->mixer_state[i].ctl);
631         if (!is_supported_ctl_type(type))
632             continue;
633
634         free(ar->mixer_state[i].old_value.ptr);
635         free(ar->mixer_state[i].new_value.ptr);
636         free(ar->mixer_state[i].reset_value.ptr);
637     }
638
639     free(ar->mixer_state);
640     ar->mixer_state = NULL;
641 }
642
643 /* Update the mixer with any changed values */
644 int audio_route_update_mixer(struct audio_route *ar)
645 {
646     unsigned int i;
647     unsigned int j;
648     struct mixer_ctl *ctl;
649
650     for (i = 0; i < ar->num_mixer_ctls; i++) {
651         unsigned int num_values = ar->mixer_state[i].num_values;
652         enum mixer_ctl_type type;
653
654         ctl = ar->mixer_state[i].ctl;
655
656         /* Skip unsupported types */
657         type = mixer_ctl_get_type(ctl);
658         if (!is_supported_ctl_type(type))
659             continue;
660
661         /* if the value has changed, update the mixer */
662         bool changed = false;
663         if (type == MIXER_CTL_TYPE_BYTE) {
664             for (j = 0; j < num_values; j++) {
665                 if (ar->mixer_state[i].old_value.bytes[j] != ar->mixer_state[i].new_value.bytes[j]) {
666                     changed = true;
667                     break;
668                 }
669             }
670          } else if (type == MIXER_CTL_TYPE_ENUM) {
671              for (j = 0; j < num_values; j++) {
672                  if (ar->mixer_state[i].old_value.enumerated[j]
673                          != ar->mixer_state[i].new_value.enumerated[j]) {
674                      changed = true;
675                      break;
676                  }
677              }
678          } else {
679             for (j = 0; j < num_values; j++) {
680                 if (ar->mixer_state[i].old_value.integer[j] != ar->mixer_state[i].new_value.integer[j]) {
681                     changed = true;
682                     break;
683                 }
684             }
685         }
686         if (changed) {
687             if (type == MIXER_CTL_TYPE_ENUM)
688                 mixer_ctl_set_value(ctl, 0, ar->mixer_state[i].new_value.enumerated[0]);
689             else
690                 mixer_ctl_set_array(ctl, ar->mixer_state[i].new_value.ptr, num_values);
691
692             size_t value_sz = sizeof_ctl_type(type);
693             memcpy(ar->mixer_state[i].old_value.ptr, ar->mixer_state[i].new_value.ptr,
694                    num_values * value_sz);
695         }
696     }
697
698     return 0;
699 }
700
701 /* saves the current state of the mixer, for resetting all controls */
702 static void save_mixer_state(struct audio_route *ar)
703 {
704     unsigned int i;
705     enum mixer_ctl_type type;
706
707     for (i = 0; i < ar->num_mixer_ctls; i++) {
708         type = mixer_ctl_get_type(ar->mixer_state[i].ctl);
709         if (!is_supported_ctl_type(type))
710             continue;
711
712         size_t value_sz = sizeof_ctl_type(type);
713         memcpy(ar->mixer_state[i].reset_value.ptr, ar->mixer_state[i].new_value.ptr,
714                ar->mixer_state[i].num_values * value_sz);
715     }
716 }
717
718 /* Reset the audio routes back to the initial state */
719 void audio_route_reset(struct audio_route *ar)
720 {
721     unsigned int i;
722     enum mixer_ctl_type type;
723
724     /* load all of the saved values */
725     for (i = 0; i < ar->num_mixer_ctls; i++) {
726         type = mixer_ctl_get_type(ar->mixer_state[i].ctl);
727         if (!is_supported_ctl_type(type))
728             continue;
729
730         size_t value_sz = sizeof_ctl_type(type);
731         memcpy(ar->mixer_state[i].new_value.ptr, ar->mixer_state[i].reset_value.ptr,
732             ar->mixer_state[i].num_values * value_sz);
733     }
734 }
735
736 /* Apply an audio route path by name */
737 int audio_route_apply_path(struct audio_route *ar, const char *name)
738 {
739     struct mixer_path *path;
740
741     if (!ar) {
742         ALOGE("invalid audio_route");
743         return -1;
744     }
745
746     path = path_get_by_name(ar, name);
747     if (!path) {
748         ALOGE("unable to find path '%s'", name);
749         return -1;
750     }
751
752     path_apply(ar, path);
753
754     return 0;
755 }
756
757 /* Reset an audio route path by name */
758 int audio_route_reset_path(struct audio_route *ar, const char *name)
759 {
760     struct mixer_path *path;
761
762     if (!ar) {
763         ALOGE("invalid audio_route");
764         return -1;
765     }
766
767     path = path_get_by_name(ar, name);
768     if (!path) {
769         ALOGE("unable to find path '%s'", name);
770         return -1;
771     }
772
773     path_reset(ar, path);
774
775     return 0;
776 }
777
778 /*
779  * Operates on the specified path .. controls will be updated in the
780  * order listed in the XML file
781  */
782 static int audio_route_update_path(struct audio_route *ar, const char *name, bool reverse)
783 {
784     struct mixer_path *path;
785     int32_t i, end;
786     unsigned int j;
787
788     if (!ar) {
789         ALOGE("invalid audio_route");
790         return -1;
791     }
792
793     path = path_get_by_name(ar, name);
794     if (!path) {
795         ALOGE("unable to find path '%s'", name);
796         return -1;
797     }
798
799     i = reverse ? (path->length - 1) : 0;
800     end = reverse ? -1 : (int32_t)path->length;
801
802     while (i != end) {
803         unsigned int ctl_index;
804         enum mixer_ctl_type type;
805
806         ctl_index = path->setting[i].ctl_index;
807
808         struct mixer_state * ms = &ar->mixer_state[ctl_index];
809
810         type = mixer_ctl_get_type(ms->ctl);
811         if (!is_supported_ctl_type(type)) {
812             continue;
813         }
814
815        size_t value_sz = sizeof_ctl_type(type);
816         /* if any value has changed, update the mixer */
817         for (j = 0; j < ms->num_values; j++) {
818             if (type == MIXER_CTL_TYPE_BYTE) {
819                 if (ms->old_value.bytes[j] != ms->new_value.bytes[j]) {
820                     mixer_ctl_set_array(ms->ctl, ms->new_value.bytes, ms->num_values);
821                     memcpy(ms->old_value.bytes, ms->new_value.bytes, ms->num_values * value_sz);
822                     break;
823                 }
824             } else if (type == MIXER_CTL_TYPE_ENUM) {
825                 if (ms->old_value.enumerated[j] != ms->new_value.enumerated[j]) {
826                     mixer_ctl_set_value(ms->ctl, 0, ms->new_value.enumerated[0]);
827                     memcpy(ms->old_value.enumerated, ms->new_value.enumerated,
828                             ms->num_values * value_sz);
829                     break;
830                 }
831             } else if (ms->old_value.integer[j] != ms->new_value.integer[j]) {
832                 mixer_ctl_set_array(ms->ctl, ms->new_value.integer, ms->num_values);
833                 memcpy(ms->old_value.integer, ms->new_value.integer, ms->num_values * value_sz);
834                 break;
835             }
836         }
837
838         i = reverse ? (i - 1) : (i + 1);
839     }
840     return 0;
841 }
842
843 int audio_route_apply_and_update_path(struct audio_route *ar, const char *name)
844 {
845     if (audio_route_apply_path(ar, name) < 0) {
846         return -1;
847     }
848     return audio_route_update_path(ar, name, false /*reverse*/);
849 }
850
851 int audio_route_reset_and_update_path(struct audio_route *ar, const char *name)
852 {
853     if (audio_route_reset_path(ar, name) < 0) {
854         return -1;
855     }
856     return audio_route_update_path(ar, name, true /*reverse*/);
857 }
858
859 struct audio_route *audio_route_init(unsigned int card, const char *xml_path)
860 {
861     struct config_parse_state state;
862     XML_Parser parser;
863     FILE *file;
864     int bytes_read;
865     void *buf;
866     struct audio_route *ar;
867
868     ar = calloc(1, sizeof(struct audio_route));
869     if (!ar)
870         goto err_calloc;
871
872     ar->mixer = mixer_open(card);
873     if (!ar->mixer) {
874         ALOGE("Unable to open the mixer, aborting.");
875         goto err_mixer_open;
876     }
877
878     ar->mixer_path = NULL;
879     ar->mixer_path_size = 0;
880     ar->num_mixer_paths = 0;
881
882     /* allocate space for and read current mixer settings */
883     if (alloc_mixer_state(ar) < 0)
884         goto err_mixer_state;
885
886     /* use the default XML path if none is provided */
887     if (xml_path == NULL)
888         xml_path = MIXER_XML_PATH;
889
890     file = fopen(xml_path, "r");
891
892     if (!file) {
893         ALOGE("Failed to open %s: %s", xml_path, strerror(errno));
894         goto err_fopen;
895     }
896
897     parser = XML_ParserCreate(NULL);
898     if (!parser) {
899         ALOGE("Failed to create XML parser");
900         goto err_parser_create;
901     }
902
903     memset(&state, 0, sizeof(state));
904     state.ar = ar;
905     XML_SetUserData(parser, &state);
906     XML_SetElementHandler(parser, start_tag, end_tag);
907
908     for (;;) {
909         buf = XML_GetBuffer(parser, BUF_SIZE);
910         if (buf == NULL)
911             goto err_parse;
912
913         bytes_read = fread(buf, 1, BUF_SIZE, file);
914         if (bytes_read < 0)
915             goto err_parse;
916
917         if (XML_ParseBuffer(parser, bytes_read,
918                             bytes_read == 0) == XML_STATUS_ERROR) {
919             ALOGE("Error in mixer xml (%s)", MIXER_XML_PATH);
920             goto err_parse;
921         }
922
923         if (bytes_read == 0)
924             break;
925     }
926
927     /* apply the initial mixer values, and save them so we can reset the
928        mixer to the original values */
929     audio_route_update_mixer(ar);
930     save_mixer_state(ar);
931
932     XML_ParserFree(parser);
933     fclose(file);
934     return ar;
935
936 err_parse:
937     path_free(ar);
938     XML_ParserFree(parser);
939 err_parser_create:
940     fclose(file);
941 err_fopen:
942     free_mixer_state(ar);
943 err_mixer_state:
944     mixer_close(ar->mixer);
945 err_mixer_open:
946     free(ar);
947     ar = NULL;
948 err_calloc:
949     return NULL;
950 }
951
952 void audio_route_free(struct audio_route *ar)
953 {
954     free_mixer_state(ar);
955     mixer_close(ar->mixer);
956     path_free(ar);
957     free(ar);
958 }