ConfigParsingUtils.cpp revision 98cc191247388132b6fd8a4ecd07abd6e4c5a0ed
1/*
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 "ConfigParsingUtils.h"
21#include <utils/Log.h>
22
23namespace android {
24
25//static
26uint32_t ConfigParsingUtils::stringToEnum(const struct StringToEnum *table,
27                                              size_t size,
28                                              const char *name)
29{
30    for (size_t i = 0; i < size; i++) {
31        if (strcmp(table[i].name, name) == 0) {
32            ALOGV("stringToEnum() found %s", table[i].name);
33            return table[i].value;
34        }
35    }
36    return 0;
37}
38
39//static
40const char *ConfigParsingUtils::enumToString(const struct StringToEnum *table,
41                                              size_t size,
42                                              uint32_t value)
43{
44    for (size_t i = 0; i < size; i++) {
45        if (table[i].value == value) {
46            return table[i].name;
47        }
48    }
49    return "";
50}
51
52//static
53bool ConfigParsingUtils::stringToBool(const char *value)
54{
55    return ((strcasecmp("true", value) == 0) || (strcmp("1", value) == 0));
56}
57
58
59// --- audio_policy.conf file parsing
60//static
61uint32_t ConfigParsingUtils::parseOutputFlagNames(char *name)
62{
63    uint32_t flag = 0;
64
65    // it is OK to cast name to non const here as we are not going to use it after
66    // strtok() modifies it
67    char *flagName = strtok(name, "|");
68    while (flagName != NULL) {
69        if (strlen(flagName) != 0) {
70            flag |= ConfigParsingUtils::stringToEnum(sOutputFlagNameToEnumTable,
71                               ARRAY_SIZE(sOutputFlagNameToEnumTable),
72                               flagName);
73        }
74        flagName = strtok(NULL, "|");
75    }
76    //force direct flag if offload flag is set: offloading implies a direct output stream
77    // and all common behaviors are driven by checking only the direct flag
78    // this should normally be set appropriately in the policy configuration file
79    if ((flag & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
80        flag |= AUDIO_OUTPUT_FLAG_DIRECT;
81    }
82
83    return flag;
84}
85
86//static
87uint32_t ConfigParsingUtils::parseInputFlagNames(char *name)
88{
89    uint32_t flag = 0;
90
91    // it is OK to cast name to non const here as we are not going to use it after
92    // strtok() modifies it
93    char *flagName = strtok(name, "|");
94    while (flagName != NULL) {
95        if (strlen(flagName) != 0) {
96            flag |= stringToEnum(sInputFlagNameToEnumTable,
97                               ARRAY_SIZE(sInputFlagNameToEnumTable),
98                               flagName);
99        }
100        flagName = strtok(NULL, "|");
101    }
102    return flag;
103}
104
105//static
106audio_devices_t ConfigParsingUtils::parseDeviceNames(char *name)
107{
108    uint32_t device = 0;
109
110    char *devName = strtok(name, "|");
111    while (devName != NULL) {
112        if (strlen(devName) != 0) {
113            device |= stringToEnum(sDeviceNameToEnumTable,
114                                 ARRAY_SIZE(sDeviceNameToEnumTable),
115                                 devName);
116         }
117        devName = strtok(NULL, "|");
118     }
119    return device;
120}
121
122}; // namespace android
123