blob: 1afd487a6e70ca2db72030f38c866ee8e425ade0 [file] [log] [blame]
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -08001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "APM::ConfigParsingUtils"
18//#define LOG_NDEBUG 0
19
20#include "AudioPolicyManager.h"
21
22namespace android {
23
24//static
25uint32_t ConfigParsingUtils::stringToEnum(const struct StringToEnum *table,
26 size_t size,
27 const char *name)
28{
29 for (size_t i = 0; i < size; i++) {
30 if (strcmp(table[i].name, name) == 0) {
31 ALOGV("stringToEnum() found %s", table[i].name);
32 return table[i].value;
33 }
34 }
35 return 0;
36}
37
38//static
39const char *ConfigParsingUtils::enumToString(const struct StringToEnum *table,
40 size_t size,
41 uint32_t value)
42{
43 for (size_t i = 0; i < size; i++) {
44 if (table[i].value == value) {
45 return table[i].name;
46 }
47 }
48 return "";
49}
50
51//static
52bool ConfigParsingUtils::stringToBool(const char *value)
53{
54 return ((strcasecmp("true", value) == 0) || (strcmp("1", value) == 0));
55}
56
57
58// --- audio_policy.conf file parsing
59//static
60uint32_t ConfigParsingUtils::parseOutputFlagNames(char *name)
61{
62 uint32_t flag = 0;
63
64 // it is OK to cast name to non const here as we are not going to use it after
65 // strtok() modifies it
66 char *flagName = strtok(name, "|");
67 while (flagName != NULL) {
68 if (strlen(flagName) != 0) {
69 flag |= ConfigParsingUtils::stringToEnum(sOutputFlagNameToEnumTable,
70 ARRAY_SIZE(sOutputFlagNameToEnumTable),
71 flagName);
72 }
73 flagName = strtok(NULL, "|");
74 }
75 //force direct flag if offload flag is set: offloading implies a direct output stream
76 // and all common behaviors are driven by checking only the direct flag
77 // this should normally be set appropriately in the policy configuration file
78 if ((flag & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
79 flag |= AUDIO_OUTPUT_FLAG_DIRECT;
80 }
81
82 return flag;
83}
84
85//static
86uint32_t ConfigParsingUtils::parseInputFlagNames(char *name)
87{
88 uint32_t flag = 0;
89
90 // it is OK to cast name to non const here as we are not going to use it after
91 // strtok() modifies it
92 char *flagName = strtok(name, "|");
93 while (flagName != NULL) {
94 if (strlen(flagName) != 0) {
95 flag |= stringToEnum(sInputFlagNameToEnumTable,
96 ARRAY_SIZE(sInputFlagNameToEnumTable),
97 flagName);
98 }
99 flagName = strtok(NULL, "|");
100 }
101 return flag;
102}
103
104//static
105audio_devices_t ConfigParsingUtils::parseDeviceNames(char *name)
106{
107 uint32_t device = 0;
108
109 char *devName = strtok(name, "|");
110 while (devName != NULL) {
111 if (strlen(devName) != 0) {
112 device |= stringToEnum(sDeviceNameToEnumTable,
113 ARRAY_SIZE(sDeviceNameToEnumTable),
114 devName);
115 }
116 devName = strtok(NULL, "|");
117 }
118 return device;
119}
120
121}; // namespace android