blob: 615b9f33cee27bcb99a8e3309f2fd5836b6941a2 [file] [log] [blame]
Ben Romberger55886882014-01-10 13:49:02 -08001/*
2 * Copyright (c) 2014, The Linux Foundation. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * 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
10 * copyright notice, this list of conditions and the following
11 * disclaimer in the documentation and/or other materials provided
12 * with the distribution.
13 * * Neither the name of The Linux Foundation nor the names of its
14 * contributors may be used to endorse or promote products derived
15 * from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
18 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
21 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
24 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
26 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
Ben Romberger61764e32014-01-10 13:49:02 -080030#define LOG_TAG "platform_info"
Ben Romberger55886882014-01-10 13:49:02 -080031#define LOG_NDDEBUG 0
32
33#include <errno.h>
34#include <stdio.h>
35#include <expat.h>
36#include <cutils/log.h>
37#include <audio_hw.h>
Ben Romberger61764e32014-01-10 13:49:02 -080038#include "platform_api.h"
39#include <platform.h>
Ben Romberger55886882014-01-10 13:49:02 -080040
Ben Romberger55886882014-01-10 13:49:02 -080041#define BUF_SIZE 1024
42
Haynes Mathew Georgef4da6fe2014-06-20 19:14:25 -070043typedef enum {
44 ROOT,
45 ACDB,
46 PCM_ID,
47 BACKEND_NAME,
48} section_t;
49
50typedef void (* section_process_fn)(const XML_Char **attr);
51
52static void process_acdb_id(const XML_Char **attr);
53static void process_pcm_id(const XML_Char **attr);
54static void process_backend_name(const XML_Char **attr);
55static void process_root(const XML_Char **attr);
56
57static section_process_fn section_table[] = {
58 [ROOT] = process_root,
59 [ACDB] = process_acdb_id,
60 [PCM_ID] = process_pcm_id,
61 [BACKEND_NAME] = process_backend_name,
62};
63
64static section_t section;
65
66/*
67 * <audio_platform_info>
68 * <acdb_ids>
69 * <device name="???" acdb_id="???"/>
70 * ...
71 * ...
72 * </acdb_ids>
73 * <backend_names>
74 * <device name="???" backend="???"/>
75 * ...
76 * ...
77 * </backend_names>
78 * <pcm_ids>
79 * <usecase name="???" type="in/out" id="???"/>
80 * ...
81 * ...
82 * </pcm_ids>
83 * </audio_platform_info>
84 */
85
86static void process_root(const XML_Char **attr __unused)
87{
88}
89
90/* mapping from usecase to pcm dev id */
91static void process_pcm_id(const XML_Char **attr)
92{
93 int index;
94
95 if (strcmp(attr[0], "name") != 0) {
96 ALOGE("%s: 'name' not found, no ACDB ID set!", __func__);
97 goto done;
98 }
99
100 index = platform_get_usecase_index((char *)attr[1]);
101 if (index < 0) {
102 ALOGE("%s: usecase %s not found!",
103 __func__, attr[1]);
104 goto done;
105 }
106
107 if (strcmp(attr[2], "type") != 0) {
108 ALOGE("%s: usecase type not mentioned", __func__);
109 goto done;
110 }
111
112 int type = -1;
113
114 if (!strcasecmp((char *)attr[3], "in")) {
115 type = 1;
116 } else if (!strcasecmp((char *)attr[3], "out")) {
117 type = 0;
118 } else {
119 ALOGE("%s: type must be IN or OUT", __func__);
120 goto done;
121 }
122
123 if (strcmp(attr[4], "id") != 0) {
124 ALOGE("%s: usecase id not mentioned", __func__);
125 goto done;
126 }
127
128 int id = atoi((char *)attr[5]);
129
130 if (platform_set_usecase_pcm_id(index, type, id) < 0) {
131 ALOGE("%s: usecase %s type %d id %d was not set!",
132 __func__, attr[1], type, id);
133 goto done;
134 }
135
136done:
137 return;
138}
139
140/* backend to be used for a device */
141static void process_backend_name(const XML_Char **attr)
142{
143 int index;
144
145 if (strcmp(attr[0], "name") != 0) {
146 ALOGE("%s: 'name' not found, no ACDB ID set!", __func__);
147 goto done;
148 }
149
150 index = platform_get_snd_device_index((char *)attr[1]);
151 if (index < 0) {
152 ALOGE("%s: Device %s not found, no ACDB ID set!",
153 __func__, attr[1]);
154 goto done;
155 }
156
157 if (strcmp(attr[2], "backend") != 0) {
158 ALOGE("%s: Device %s has no backend set!",
159 __func__, attr[1]);
160 goto done;
161 }
162
163 if (platform_set_snd_device_backend(index, attr[3]) < 0) {
164 ALOGE("%s: Device %s backend %s was not set!",
165 __func__, attr[1], attr[3]);
166 goto done;
167 }
168
169done:
170 return;
171}
172
173static void process_acdb_id(const XML_Char **attr)
Ben Romberger55886882014-01-10 13:49:02 -0800174{
Ben Romberger61764e32014-01-10 13:49:02 -0800175 int index;
Ben Romberger55886882014-01-10 13:49:02 -0800176
Ben Romberger61764e32014-01-10 13:49:02 -0800177 if (strcmp(attr[0], "name") != 0) {
178 ALOGE("%s: 'name' not found, no ACDB ID set!", __func__);
Ben Romberger55886882014-01-10 13:49:02 -0800179 goto done;
Ben Romberger61764e32014-01-10 13:49:02 -0800180 }
Ben Romberger55886882014-01-10 13:49:02 -0800181
Ben Romberger61764e32014-01-10 13:49:02 -0800182 index = platform_get_snd_device_index((char *)attr[1]);
183 if (index < 0) {
Helen Zeng6a16ad72014-02-23 22:04:44 -0800184 ALOGE("%s: Device %s in platform info xml not found, no ACDB ID set!",
185 __func__, attr[1]);
Ben Romberger55886882014-01-10 13:49:02 -0800186 goto done;
187 }
188
189 if (strcmp(attr[2], "acdb_id") != 0) {
Helen Zeng6a16ad72014-02-23 22:04:44 -0800190 ALOGE("%s: Device %s in platform info xml has no acdb_id, no ACDB ID set!",
191 __func__, attr[1]);
Ben Romberger55886882014-01-10 13:49:02 -0800192 goto done;
193 }
194
Haynes Mathew Georgef4da6fe2014-06-20 19:14:25 -0700195 if (platform_set_snd_device_acdb_id(index, atoi((char *)attr[3])) < 0) {
196 ALOGE("%s: Device %s, ACDB ID %d was not set!",
Helen Zeng6a16ad72014-02-23 22:04:44 -0800197 __func__, attr[1], atoi((char *)attr[3]));
Ben Romberger55886882014-01-10 13:49:02 -0800198 goto done;
Ben Romberger61764e32014-01-10 13:49:02 -0800199 }
Ben Romberger55886882014-01-10 13:49:02 -0800200
Ben Romberger55886882014-01-10 13:49:02 -0800201done:
202 return;
203}
204
Haynes Mathew Georgef4da6fe2014-06-20 19:14:25 -0700205static void start_tag(void *userdata __unused, const XML_Char *tag_name,
Ben Romberger55886882014-01-10 13:49:02 -0800206 const XML_Char **attr)
207{
208 const XML_Char *attr_name = NULL;
209 const XML_Char *attr_value = NULL;
210 unsigned int i;
211
Haynes Mathew Georgef4da6fe2014-06-20 19:14:25 -0700212 if (strcmp(tag_name, "acdb_ids") == 0) {
213 section = ACDB;
214 } else if (strcmp(tag_name, "pcm_ids") == 0) {
215 section = PCM_ID;
216 } else if (strcmp(tag_name, "backend_names") == 0) {
217 section = BACKEND_NAME;
218 } else if (strcmp(tag_name, "device") == 0) {
219 if ((section != ACDB) && (section != BACKEND_NAME)) {
220 ALOGE("device tag only supported for acdb/backend names");
221 return;
222 }
223
224 /* call into process function for the current section */
225 section_process_fn fn = section_table[section];
226 fn(attr);
227 } else if (strcmp(tag_name, "usecase") == 0) {
228 if (section != PCM_ID) {
229 ALOGE("usecase tag only supported with PCM_ID section");
230 return;
231 }
232
233 section_process_fn fn = section_table[PCM_ID];
234 fn(attr);
235 }
Ben Romberger55886882014-01-10 13:49:02 -0800236
237 return;
238}
239
Haynes Mathew Georgef4da6fe2014-06-20 19:14:25 -0700240static void end_tag(void *userdata __unused, const XML_Char *tag_name)
Ben Romberger55886882014-01-10 13:49:02 -0800241{
Haynes Mathew Georgef4da6fe2014-06-20 19:14:25 -0700242 if (strcmp(tag_name, "acdb_ids") == 0) {
243 section = ROOT;
244 } else if (strcmp(tag_name, "pcm_ids") == 0) {
245 section = ROOT;
246 } else if (strcmp(tag_name, "backend_names") == 0) {
247 section = ROOT;
248 }
Ben Romberger55886882014-01-10 13:49:02 -0800249}
250
Helen Zeng6a16ad72014-02-23 22:04:44 -0800251int platform_info_init(const char *filename)
Ben Romberger55886882014-01-10 13:49:02 -0800252{
253 XML_Parser parser;
254 FILE *file;
255 int ret = 0;
256 int bytes_read;
Ben Romberger55886882014-01-10 13:49:02 -0800257 void *buf;
258
Helen Zeng6a16ad72014-02-23 22:04:44 -0800259 file = fopen(filename, "r");
Haynes Mathew Georgef4da6fe2014-06-20 19:14:25 -0700260 section = ROOT;
261
Ben Romberger55886882014-01-10 13:49:02 -0800262 if (!file) {
263 ALOGD("%s: Failed to open %s, using defaults.",
Helen Zeng6a16ad72014-02-23 22:04:44 -0800264 __func__, filename);
Ben Romberger55886882014-01-10 13:49:02 -0800265 ret = -ENODEV;
266 goto done;
267 }
268
269 parser = XML_ParserCreate(NULL);
270 if (!parser) {
271 ALOGE("%s: Failed to create XML parser!", __func__);
272 ret = -ENODEV;
273 goto err_close_file;
274 }
275
Ben Romberger55886882014-01-10 13:49:02 -0800276 XML_SetElementHandler(parser, start_tag, end_tag);
277
278 while (1) {
279 buf = XML_GetBuffer(parser, BUF_SIZE);
280 if (buf == NULL) {
281 ALOGE("%s: XML_GetBuffer failed", __func__);
282 ret = -ENOMEM;
283 goto err_free_parser;
284 }
285
286 bytes_read = fread(buf, 1, BUF_SIZE, file);
287 if (bytes_read < 0) {
288 ALOGE("%s: fread failed, bytes read = %d", __func__, bytes_read);
289 ret = bytes_read;
290 goto err_free_parser;
291 }
292
293 if (XML_ParseBuffer(parser, bytes_read,
294 bytes_read == 0) == XML_STATUS_ERROR) {
295 ALOGE("%s: XML_ParseBuffer failed, for %s",
Helen Zeng6a16ad72014-02-23 22:04:44 -0800296 __func__, filename);
Ben Romberger55886882014-01-10 13:49:02 -0800297 ret = -EINVAL;
298 goto err_free_parser;
299 }
300
301 if (bytes_read == 0)
302 break;
303 }
304
Ben Romberger55886882014-01-10 13:49:02 -0800305err_free_parser:
306 XML_ParserFree(parser);
307err_close_file:
308 fclose(file);
309done:
310 return ret;
311}