blob: e1ff36192d47757908fca86f3c83440942c62608 [file] [log] [blame]
Haynes Mathew George5bc18842014-06-16 16:36:20 -07001/*
2 * Copyright (C) 2014 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 "platform_info"
18#define LOG_NDDEBUG 0
19
20#include <errno.h>
21#include <stdio.h>
22#include <expat.h>
23#include <cutils/log.h>
24#include <audio_hw.h>
25#include "platform_api.h"
26#include <platform.h>
27
28#define PLATFORM_INFO_XML_PATH "/system/etc/audio_platform_info.xml"
29
Haynes Mathew George98c95622014-06-20 19:14:25 -070030typedef enum {
31 ROOT,
32 ACDB,
33 PCM_ID,
34 BACKEND_NAME,
35} section_t;
36
37typedef void (* section_process_fn)(const XML_Char **attr);
38
39static void process_acdb_id(const XML_Char **attr);
40static void process_pcm_id(const XML_Char **attr);
41static void process_backend_name(const XML_Char **attr);
42static void process_root(const XML_Char **attr);
43
44static section_process_fn section_table[] = {
45 [ROOT] = process_root,
46 [ACDB] = process_acdb_id,
47 [PCM_ID] = process_pcm_id,
48 [BACKEND_NAME] = process_backend_name,
49};
50
51static section_t section;
52
53/*
54 * <audio_platform_info>
55 * <acdb_ids>
56 * <device name="???" acdb_id="???"/>
57 * ...
58 * ...
59 * </acdb_ids>
60 * <backend_names>
61 * <device name="???" backend="???"/>
62 * ...
63 * ...
64 * </backend_names>
65 * <pcm_ids>
66 * <usecase name="???" type="in/out" id="???"/>
67 * ...
68 * ...
69 * </pcm_ids>
70 * </audio_platform_info>
71 */
72
73static void process_root(const XML_Char **attr __unused)
74{
75}
76
77/* mapping from usecase to pcm dev id */
78static void process_pcm_id(const XML_Char **attr)
79{
80 int index;
81
82 if (strcmp(attr[0], "name") != 0) {
83 ALOGE("%s: 'name' not found, no ACDB ID set!", __func__);
84 goto done;
85 }
86
87 index = platform_get_usecase_index((char *)attr[1]);
88 if (index < 0) {
89 ALOGE("%s: usecase %s in %s not found!",
90 __func__, attr[1], PLATFORM_INFO_XML_PATH);
91 goto done;
92 }
93
94 if (strcmp(attr[2], "type") != 0) {
95 ALOGE("%s: usecase type not mentioned", __func__);
96 goto done;
97 }
98
99 int type = -1;
100
101 if (!strcasecmp((char *)attr[3], "in")) {
102 type = 1;
103 } else if (!strcasecmp((char *)attr[3], "out")) {
104 type = 0;
105 } else {
106 ALOGE("%s: type must be IN or OUT", __func__);
107 goto done;
108 }
109
110 if (strcmp(attr[4], "id") != 0) {
111 ALOGE("%s: usecase id not mentioned", __func__);
112 goto done;
113 }
114
115 int id = atoi((char *)attr[5]);
116
117 if (platform_set_usecase_pcm_id(index, type, id) < 0) {
118 ALOGE("%s: usecase %s in %s, type %d id %d was not set!",
119 __func__, attr[1], PLATFORM_INFO_XML_PATH, type, id);
120 goto done;
121 }
122
123done:
124 return;
125}
126
127/* backend to be used for a device */
128static void process_backend_name(const XML_Char **attr)
129{
130 int index;
Ravi Kumar Alamandab7ea4f52015-06-08 16:44:05 -0700131 char *hw_interface = NULL;
Haynes Mathew George98c95622014-06-20 19:14:25 -0700132
133 if (strcmp(attr[0], "name") != 0) {
134 ALOGE("%s: 'name' not found, no ACDB ID set!", __func__);
135 goto done;
136 }
137
138 index = platform_get_snd_device_index((char *)attr[1]);
139 if (index < 0) {
140 ALOGE("%s: Device %s in %s not found, no ACDB ID set!",
141 __func__, attr[1], PLATFORM_INFO_XML_PATH);
142 goto done;
143 }
144
145 if (strcmp(attr[2], "backend") != 0) {
146 ALOGE("%s: Device %s in %s has no backed set!",
147 __func__, attr[1], PLATFORM_INFO_XML_PATH);
148 goto done;
149 }
150
Ravi Kumar Alamandab7ea4f52015-06-08 16:44:05 -0700151 if (attr[4] != NULL) {
152 if (strcmp(attr[4], "interface") != 0) {
153 hw_interface = NULL;
154 } else {
155 hw_interface = (char *)attr[5];
156 }
157 }
158
159 if (platform_set_snd_device_backend(index, attr[3], hw_interface) < 0) {
Haynes Mathew George98c95622014-06-20 19:14:25 -0700160 ALOGE("%s: Device %s in %s, backend %s was not set!",
161 __func__, attr[1], PLATFORM_INFO_XML_PATH, attr[3]);
162 goto done;
163 }
164
165done:
166 return;
167}
168
169static void process_acdb_id(const XML_Char **attr)
Haynes Mathew George5bc18842014-06-16 16:36:20 -0700170{
171 int index;
172
173 if (strcmp(attr[0], "name") != 0) {
174 ALOGE("%s: 'name' not found, no ACDB ID set!", __func__);
175 goto done;
176 }
177
178 index = platform_get_snd_device_index((char *)attr[1]);
179 if (index < 0) {
180 ALOGE("%s: Device %s in %s not found, no ACDB ID set!",
181 __func__, attr[1], PLATFORM_INFO_XML_PATH);
182 goto done;
183 }
184
185 if (strcmp(attr[2], "acdb_id") != 0) {
186 ALOGE("%s: Device %s in %s has no acdb_id, no ACDB ID set!",
187 __func__, attr[1], PLATFORM_INFO_XML_PATH);
188 goto done;
189 }
190
Haynes Mathew George98c95622014-06-20 19:14:25 -0700191 if (platform_set_snd_device_acdb_id(index, atoi((char *)attr[3])) < 0) {
Haynes Mathew George5bc18842014-06-16 16:36:20 -0700192 ALOGE("%s: Device %s in %s, ACDB ID %d was not set!",
193 __func__, attr[1], PLATFORM_INFO_XML_PATH, atoi((char *)attr[3]));
194 goto done;
195 }
196
197done:
198 return;
199}
200
201static void start_tag(void *userdata __unused, const XML_Char *tag_name,
202 const XML_Char **attr)
203{
204 const XML_Char *attr_name = NULL;
205 const XML_Char *attr_value = NULL;
206 unsigned int i;
207
Haynes Mathew George98c95622014-06-20 19:14:25 -0700208 if (strcmp(tag_name, "acdb_ids") == 0) {
209 section = ACDB;
210 } else if (strcmp(tag_name, "pcm_ids") == 0) {
211 section = PCM_ID;
212 } else if (strcmp(tag_name, "backend_names") == 0) {
213 section = BACKEND_NAME;
214 } else if (strcmp(tag_name, "device") == 0) {
215 if ((section != ACDB) && (section != BACKEND_NAME)) {
216 ALOGE("device tag only supported for acdb/backend names");
217 return;
218 }
219
220 /* call into process function for the current section */
221 section_process_fn fn = section_table[section];
222 fn(attr);
223 } else if (strcmp(tag_name, "usecase") == 0) {
224 if (section != PCM_ID) {
225 ALOGE("usecase tag only supported with PCM_ID section");
226 return;
227 }
228
229 section_process_fn fn = section_table[PCM_ID];
230 fn(attr);
231 }
Haynes Mathew George5bc18842014-06-16 16:36:20 -0700232
233 return;
234}
235
Haynes Mathew George98c95622014-06-20 19:14:25 -0700236static void end_tag(void *userdata __unused, const XML_Char *tag_name)
Haynes Mathew George5bc18842014-06-16 16:36:20 -0700237{
Haynes Mathew George98c95622014-06-20 19:14:25 -0700238 if (strcmp(tag_name, "acdb_ids") == 0) {
239 section = ROOT;
240 } else if (strcmp(tag_name, "pcm_ids") == 0) {
241 section = ROOT;
242 } else if (strcmp(tag_name, "backend_names") == 0) {
243 section = ROOT;
244 }
Haynes Mathew George5bc18842014-06-16 16:36:20 -0700245}
246
247int platform_info_init(void)
248{
249 XML_Parser parser;
250 FILE *file;
251 int ret = 0;
252 int bytes_read;
253 void *buf;
254 static const uint32_t kBufSize = 1024;
255
Haynes Mathew George98c95622014-06-20 19:14:25 -0700256 section = ROOT;
257
Haynes Mathew George5bc18842014-06-16 16:36:20 -0700258 file = fopen(PLATFORM_INFO_XML_PATH, "r");
259 if (!file) {
260 ALOGD("%s: Failed to open %s, using defaults.",
261 __func__, PLATFORM_INFO_XML_PATH);
262 ret = -ENODEV;
263 goto done;
264 }
265
266 parser = XML_ParserCreate(NULL);
267 if (!parser) {
268 ALOGE("%s: Failed to create XML parser!", __func__);
269 ret = -ENODEV;
270 goto err_close_file;
271 }
272
273 XML_SetElementHandler(parser, start_tag, end_tag);
274
275 while (1) {
276 buf = XML_GetBuffer(parser, kBufSize);
277 if (buf == NULL) {
278 ALOGE("%s: XML_GetBuffer failed", __func__);
279 ret = -ENOMEM;
280 goto err_free_parser;
281 }
282
283 bytes_read = fread(buf, 1, kBufSize, file);
284 if (bytes_read < 0) {
285 ALOGE("%s: fread failed, bytes read = %d", __func__, bytes_read);
286 ret = bytes_read;
287 goto err_free_parser;
288 }
289
290 if (XML_ParseBuffer(parser, bytes_read,
291 bytes_read == 0) == XML_STATUS_ERROR) {
292 ALOGE("%s: XML_ParseBuffer failed, for %s",
293 __func__, PLATFORM_INFO_XML_PATH);
294 ret = -EINVAL;
295 goto err_free_parser;
296 }
297
298 if (bytes_read == 0)
299 break;
300 }
301
302err_free_parser:
303 XML_ParserFree(parser);
304err_close_file:
305 fclose(file);
306done:
307 return ret;
308}