blob: 61dfb5de6484e753f2e32e5c0a8442e3d3bc3155 [file] [log] [blame]
Ben Romberger55886882014-01-10 13:49:02 -08001/*
Dhanalakshmi Siddani21be3ac2016-12-29 14:31:08 +05302 * Copyright (c) 2014-2017, The Linux Foundation. All rights reserved.
Ben Romberger55886882014-01-10 13:49:02 -08003 *
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>
Ravi Kumar Alamanda14b0f2d2015-06-28 21:04:09 -070037#include <cutils/str_parms.h>
Ben Romberger55886882014-01-10 13:49:02 -080038#include <audio_hw.h>
Vignesh Kulothungan55396882017-04-20 14:37:02 -070039#include "acdb.h"
Ben Romberger61764e32014-01-10 13:49:02 -080040#include "platform_api.h"
41#include <platform.h>
Aniket Kumar Lataf56b6402016-10-27 12:03:18 -070042#include <math.h>
Ben Romberger55886882014-01-10 13:49:02 -080043
Ben Romberger55886882014-01-10 13:49:02 -080044#define BUF_SIZE 1024
45
Haynes Mathew Georgef4da6fe2014-06-20 19:14:25 -070046typedef enum {
47 ROOT,
48 ACDB,
Amit Shekhar5a39c912014-10-14 15:39:30 -070049 BITWIDTH,
Haynes Mathew Georgef4da6fe2014-06-20 19:14:25 -070050 PCM_ID,
51 BACKEND_NAME,
Narsinga Rao Chellaf928a982015-03-06 14:57:35 -080052 INTERFACE_NAME,
Ravi Kumar Alamanda14b0f2d2015-06-28 21:04:09 -070053 CONFIG_PARAMS,
Aniket Kumar Lataf56b6402016-10-27 12:03:18 -070054 GAIN_LEVEL_MAPPING,
Dhanalakshmi Siddani21be3ac2016-12-29 14:31:08 +053055 ACDB_METAINFO_KEY,
Haynes Mathew Georgef4da6fe2014-06-20 19:14:25 -070056} section_t;
57
58typedef void (* section_process_fn)(const XML_Char **attr);
59
60static void process_acdb_id(const XML_Char **attr);
Amit Shekhar5a39c912014-10-14 15:39:30 -070061static void process_bit_width(const XML_Char **attr);
Haynes Mathew Georgef4da6fe2014-06-20 19:14:25 -070062static void process_pcm_id(const XML_Char **attr);
63static void process_backend_name(const XML_Char **attr);
Narsinga Rao Chellaf928a982015-03-06 14:57:35 -080064static void process_interface_name(const XML_Char **attr);
Ravi Kumar Alamanda14b0f2d2015-06-28 21:04:09 -070065static void process_config_params(const XML_Char **attr);
Haynes Mathew Georgef4da6fe2014-06-20 19:14:25 -070066static void process_root(const XML_Char **attr);
Aniket Kumar Lataf56b6402016-10-27 12:03:18 -070067static void process_gain_db_to_level_map(const XML_Char **attr);
Dhanalakshmi Siddani21be3ac2016-12-29 14:31:08 +053068static void process_acdb_metainfo_key(const XML_Char **attr);
Haynes Mathew Georgef4da6fe2014-06-20 19:14:25 -070069
70static section_process_fn section_table[] = {
71 [ROOT] = process_root,
72 [ACDB] = process_acdb_id,
Amit Shekhar5a39c912014-10-14 15:39:30 -070073 [BITWIDTH] = process_bit_width,
Haynes Mathew Georgef4da6fe2014-06-20 19:14:25 -070074 [PCM_ID] = process_pcm_id,
75 [BACKEND_NAME] = process_backend_name,
Narsinga Rao Chellaf928a982015-03-06 14:57:35 -080076 [INTERFACE_NAME] = process_interface_name,
Ravi Kumar Alamanda14b0f2d2015-06-28 21:04:09 -070077 [CONFIG_PARAMS] = process_config_params,
Aniket Kumar Lataf56b6402016-10-27 12:03:18 -070078 [GAIN_LEVEL_MAPPING] = process_gain_db_to_level_map,
Dhanalakshmi Siddani21be3ac2016-12-29 14:31:08 +053079 [ACDB_METAINFO_KEY] = process_acdb_metainfo_key,
Haynes Mathew Georgef4da6fe2014-06-20 19:14:25 -070080};
81
82static section_t section;
83
Ravi Kumar Alamanda14b0f2d2015-06-28 21:04:09 -070084struct platform_info {
Vignesh Kulothungan55396882017-04-20 14:37:02 -070085 caller_t caller;
Ravi Kumar Alamanda14b0f2d2015-06-28 21:04:09 -070086 void *platform;
87 struct str_parms *kvpairs;
88};
89
90static struct platform_info my_data;
91
Haynes Mathew Georgef4da6fe2014-06-20 19:14:25 -070092/*
93 * <audio_platform_info>
94 * <acdb_ids>
95 * <device name="???" acdb_id="???"/>
96 * ...
97 * ...
98 * </acdb_ids>
99 * <backend_names>
100 * <device name="???" backend="???"/>
101 * ...
102 * ...
103 * </backend_names>
104 * <pcm_ids>
105 * <usecase name="???" type="in/out" id="???"/>
106 * ...
107 * ...
108 * </pcm_ids>
Narsinga Rao Chellaf928a982015-03-06 14:57:35 -0800109 * <interface_names>
Karthik Reddy Katta508eca42015-05-11 13:43:18 +0530110 * <device name="Use audio device name here, not sound device name" interface="PRIMARY_I2S" codec_type="external/internal"/>
Narsinga Rao Chellaf928a982015-03-06 14:57:35 -0800111 * ...
112 * ...
113 * </interface_names>
Ravi Kumar Alamanda14b0f2d2015-06-28 21:04:09 -0700114 * <config_params>
115 * <param key="snd_card_name" value="msm8994-tomtom-mtp-snd-card"/>
116 * ...
117 * ...
118 * </config_params>
Haynes Mathew Georgef4da6fe2014-06-20 19:14:25 -0700119 * </audio_platform_info>
120 */
121
122static void process_root(const XML_Char **attr __unused)
123{
124}
125
126/* mapping from usecase to pcm dev id */
127static void process_pcm_id(const XML_Char **attr)
128{
129 int index;
130
131 if (strcmp(attr[0], "name") != 0) {
Ravi Kumar Alamanda14b0f2d2015-06-28 21:04:09 -0700132 ALOGE("%s: 'name' not found, no pcm_id set!", __func__);
Haynes Mathew Georgef4da6fe2014-06-20 19:14:25 -0700133 goto done;
134 }
135
136 index = platform_get_usecase_index((char *)attr[1]);
137 if (index < 0) {
138 ALOGE("%s: usecase %s not found!",
139 __func__, attr[1]);
140 goto done;
141 }
142
143 if (strcmp(attr[2], "type") != 0) {
144 ALOGE("%s: usecase type not mentioned", __func__);
145 goto done;
146 }
147
148 int type = -1;
149
150 if (!strcasecmp((char *)attr[3], "in")) {
151 type = 1;
152 } else if (!strcasecmp((char *)attr[3], "out")) {
153 type = 0;
154 } else {
155 ALOGE("%s: type must be IN or OUT", __func__);
156 goto done;
157 }
158
159 if (strcmp(attr[4], "id") != 0) {
160 ALOGE("%s: usecase id not mentioned", __func__);
161 goto done;
162 }
163
164 int id = atoi((char *)attr[5]);
165
166 if (platform_set_usecase_pcm_id(index, type, id) < 0) {
167 ALOGE("%s: usecase %s type %d id %d was not set!",
168 __func__, attr[1], type, id);
169 goto done;
170 }
171
172done:
173 return;
174}
175
176/* backend to be used for a device */
177static void process_backend_name(const XML_Char **attr)
178{
179 int index;
Sidipotu Ashok9f0b16e2016-04-28 13:48:28 +0530180 char *hw_interface = NULL;
Ashish Jaind150d4c2017-02-03 18:44:34 +0530181 char *backend = NULL;
Haynes Mathew Georgef4da6fe2014-06-20 19:14:25 -0700182
183 if (strcmp(attr[0], "name") != 0) {
184 ALOGE("%s: 'name' not found, no ACDB ID set!", __func__);
185 goto done;
186 }
187
188 index = platform_get_snd_device_index((char *)attr[1]);
189 if (index < 0) {
190 ALOGE("%s: Device %s not found, no ACDB ID set!",
191 __func__, attr[1]);
192 goto done;
193 }
194
195 if (strcmp(attr[2], "backend") != 0) {
Ashish Jaind150d4c2017-02-03 18:44:34 +0530196 if (strcmp(attr[2], "interface") == 0)
197 hw_interface = (char *)attr[3];
198 } else {
199 backend = (char *)attr[3];
Haynes Mathew Georgef4da6fe2014-06-20 19:14:25 -0700200 }
201
Sidipotu Ashok9f0b16e2016-04-28 13:48:28 +0530202 if (attr[4] != NULL) {
203 if (strcmp(attr[4], "interface") != 0) {
204 hw_interface = NULL;
205 } else {
206 hw_interface = (char *)attr[5];
207 }
208 }
209
Ashish Jaind150d4c2017-02-03 18:44:34 +0530210 if (platform_set_snd_device_backend(index, backend, hw_interface) < 0) {
Haynes Mathew Georgef4da6fe2014-06-20 19:14:25 -0700211 ALOGE("%s: Device %s backend %s was not set!",
212 __func__, attr[1], attr[3]);
213 goto done;
214 }
215
216done:
217 return;
218}
219
Aniket Kumar Lataf56b6402016-10-27 12:03:18 -0700220static void process_gain_db_to_level_map(const XML_Char **attr)
221{
222 struct amp_db_and_gain_table tbl_entry;
223
224 if ((strcmp(attr[0], "db") != 0) ||
225 (strcmp(attr[2], "level") != 0)) {
226 ALOGE("%s: invalid attribute passed %s %sexpected amp db level",
227 __func__, attr[0], attr[2]);
228 goto done;
229 }
230
231 tbl_entry.db = atof(attr[1]);
232 tbl_entry.amp = exp(tbl_entry.db * 0.115129f);
233 tbl_entry.level = atoi(attr[3]);
234
235 ALOGV("%s: amp [%f] db [%f] level [%d]", __func__,
236 tbl_entry.amp, tbl_entry.db, tbl_entry.level);
237 platform_add_gain_level_mapping(&tbl_entry);
238
239done:
240 return;
241}
242
243
Haynes Mathew Georgef4da6fe2014-06-20 19:14:25 -0700244static void process_acdb_id(const XML_Char **attr)
Ben Romberger55886882014-01-10 13:49:02 -0800245{
Ben Romberger61764e32014-01-10 13:49:02 -0800246 int index;
Ben Romberger55886882014-01-10 13:49:02 -0800247
Ben Romberger61764e32014-01-10 13:49:02 -0800248 if (strcmp(attr[0], "name") != 0) {
249 ALOGE("%s: 'name' not found, no ACDB ID set!", __func__);
Ben Romberger55886882014-01-10 13:49:02 -0800250 goto done;
Ben Romberger61764e32014-01-10 13:49:02 -0800251 }
Ben Romberger55886882014-01-10 13:49:02 -0800252
Ben Romberger61764e32014-01-10 13:49:02 -0800253 index = platform_get_snd_device_index((char *)attr[1]);
254 if (index < 0) {
Helen Zeng6a16ad72014-02-23 22:04:44 -0800255 ALOGE("%s: Device %s in platform info xml not found, no ACDB ID set!",
256 __func__, attr[1]);
Ben Romberger55886882014-01-10 13:49:02 -0800257 goto done;
258 }
259
260 if (strcmp(attr[2], "acdb_id") != 0) {
Helen Zeng6a16ad72014-02-23 22:04:44 -0800261 ALOGE("%s: Device %s in platform info xml has no acdb_id, no ACDB ID set!",
262 __func__, attr[1]);
Ben Romberger55886882014-01-10 13:49:02 -0800263 goto done;
264 }
265
Haynes Mathew Georgef4da6fe2014-06-20 19:14:25 -0700266 if (platform_set_snd_device_acdb_id(index, atoi((char *)attr[3])) < 0) {
267 ALOGE("%s: Device %s, ACDB ID %d was not set!",
Helen Zeng6a16ad72014-02-23 22:04:44 -0800268 __func__, attr[1], atoi((char *)attr[3]));
Ben Romberger55886882014-01-10 13:49:02 -0800269 goto done;
Ben Romberger61764e32014-01-10 13:49:02 -0800270 }
Ben Romberger55886882014-01-10 13:49:02 -0800271
Ben Romberger55886882014-01-10 13:49:02 -0800272done:
273 return;
274}
275
Amit Shekhar5a39c912014-10-14 15:39:30 -0700276static void process_bit_width(const XML_Char **attr)
277{
278 int index;
279
280 if (strcmp(attr[0], "name") != 0) {
281 ALOGE("%s: 'name' not found, no ACDB ID set!", __func__);
282 goto done;
283 }
284
285 index = platform_get_snd_device_index((char *)attr[1]);
286 if (index < 0) {
287 ALOGE("%s: Device %s in platform info xml not found, no ACDB ID set!",
288 __func__, attr[1]);
289 goto done;
290 }
291
292 if (strcmp(attr[2], "bit_width") != 0) {
293 ALOGE("%s: Device %s in platform info xml has no bit_width, no ACDB ID set!",
294 __func__, attr[1]);
295 goto done;
296 }
297
298 if (platform_set_snd_device_bit_width(index, atoi((char *)attr[3])) < 0) {
299 ALOGE("%s: Device %s, ACDB ID %d was not set!",
300 __func__, attr[1], atoi((char *)attr[3]));
301 goto done;
302 }
303
304done:
305 return;
306}
307
Narsinga Rao Chellaf928a982015-03-06 14:57:35 -0800308static void process_interface_name(const XML_Char **attr)
309{
310 int ret;
311
312 if (strcmp(attr[0], "name") != 0) {
313 ALOGE("%s: 'name' not found, no Audio Interface set!", __func__);
314
315 goto done;
316 }
317
318 if (strcmp(attr[2], "interface") != 0) {
319 ALOGE("%s: Device %s has no Audio Interface set!",
320 __func__, attr[1]);
321
322 goto done;
323 }
324
Karthik Reddy Katta508eca42015-05-11 13:43:18 +0530325 if (strcmp(attr[4], "codec_type") != 0) {
326 ALOGE("%s: Device %s has no codec type set!",
327 __func__, attr[1]);
328
329 goto done;
330 }
331
332 ret = platform_set_audio_device_interface((char *)attr[1], (char *)attr[3],
333 (char *)attr[5]);
Narsinga Rao Chellaf928a982015-03-06 14:57:35 -0800334 if (ret < 0) {
335 ALOGE("%s: Audio Interface not set!", __func__);
Apoorv Raghuvanshi21492162015-02-19 18:19:36 -0800336 goto done;
337 }
Narsinga Rao Chellaf928a982015-03-06 14:57:35 -0800338
Apoorv Raghuvanshi21492162015-02-19 18:19:36 -0800339done:
340 return;
341}
Laxminath Kasam44f49402015-05-29 18:37:11 +0530342
Ravi Kumar Alamanda14b0f2d2015-06-28 21:04:09 -0700343static void process_config_params(const XML_Char **attr)
344{
345 if (strcmp(attr[0], "key") != 0) {
346 ALOGE("%s: 'key' not found", __func__);
347 goto done;
348 }
349
350 if (strcmp(attr[2], "value") != 0) {
351 ALOGE("%s: 'value' not found", __func__);
352 goto done;
353 }
354
355 str_parms_add_str(my_data.kvpairs, (char*)attr[1], (char*)attr[3]);
356done:
357 return;
358}
359
Dhanalakshmi Siddani21be3ac2016-12-29 14:31:08 +0530360/* process acdb meta info key value */
361static void process_acdb_metainfo_key(const XML_Char **attr)
362{
363 if (strcmp(attr[0], "name") != 0) {
364 ALOGE("%s: 'name' not found", __func__);
365 goto done;
366 }
367
368 if (strcmp(attr[2], "value") != 0) {
369 ALOGE("%s: 'value' not found", __func__);
370 goto done;
371 }
372
373 int key = atoi((char *)attr[3]);
Vignesh Kulothungan55396882017-04-20 14:37:02 -0700374 switch(my_data.caller) {
375 case ACDB_EXTN:
376 if(acdb_set_metainfo_key(my_data.platform, (char*)attr[1], key) < 0) {
377 ALOGE("%s: key %d was not set!", __func__, key);
378 goto done;
379 }
380 break;
381 case PLATFORM:
382 if(platform_set_acdb_metainfo_key(my_data.platform, (char*)attr[1], key) < 0) {
383 ALOGE("%s: key %d was not set!", __func__, key);
384 goto done;
385 }
386 break;
387 default:
388 ALOGE("%s: unknown caller!", __func__);
Dhanalakshmi Siddani21be3ac2016-12-29 14:31:08 +0530389 }
390
391done:
392 return;
393}
394
Haynes Mathew Georgef4da6fe2014-06-20 19:14:25 -0700395static void start_tag(void *userdata __unused, const XML_Char *tag_name,
Ben Romberger55886882014-01-10 13:49:02 -0800396 const XML_Char **attr)
397{
Vignesh Kulothungan55396882017-04-20 14:37:02 -0700398 if (my_data.caller == ACDB_EXTN) {
399 if(strcmp(tag_name, "acdb_metainfo_key") == 0) {
400 section = ACDB_METAINFO_KEY;
401 } else if (strcmp(tag_name, "param") == 0) {
402 if ((section != CONFIG_PARAMS) && (section != ACDB_METAINFO_KEY)) {
403 ALOGE("param tag only supported with CONFIG_PARAMS section");
404 return;
405 }
Haynes Mathew Georgef4da6fe2014-06-20 19:14:25 -0700406
Vignesh Kulothungan55396882017-04-20 14:37:02 -0700407 section_process_fn fn = section_table[section];
408 fn(attr);
Aniket Kumar Lataf56b6402016-10-27 12:03:18 -0700409 }
Vignesh Kulothungan55396882017-04-20 14:37:02 -0700410 } else if(my_data.caller == PLATFORM) {
411 if (strcmp(tag_name, "bit_width_configs") == 0) {
412 section = BITWIDTH;
413 } else if (strcmp(tag_name, "acdb_ids") == 0) {
414 section = ACDB;
415 } else if (strcmp(tag_name, "pcm_ids") == 0) {
416 section = PCM_ID;
417 } else if (strcmp(tag_name, "backend_names") == 0) {
418 section = BACKEND_NAME;
419 } else if (strcmp(tag_name, "config_params") == 0) {
420 section = CONFIG_PARAMS;
421 } else if (strcmp(tag_name, "interface_names") == 0) {
422 section = INTERFACE_NAME;
423 } else if (strcmp(tag_name, "gain_db_to_level_mapping") == 0) {
424 section = GAIN_LEVEL_MAPPING;
425 } else if(strcmp(tag_name, "acdb_metainfo_key") == 0) {
426 section = ACDB_METAINFO_KEY;
427 } else if (strcmp(tag_name, "device") == 0) {
428 if ((section != ACDB) && (section != BACKEND_NAME) && (section != BITWIDTH) &&
429 (section != INTERFACE_NAME)) {
430 ALOGE("device tag only supported for acdb/backend names/bitwitdh/interface names");
431 return;
432 }
Aniket Kumar Lataf56b6402016-10-27 12:03:18 -0700433
Vignesh Kulothungan55396882017-04-20 14:37:02 -0700434 /* call into process function for the current section */
435 section_process_fn fn = section_table[section];
436 fn(attr);
437 } else if (strcmp(tag_name, "gain_level_map") == 0) {
438 if (section != GAIN_LEVEL_MAPPING) {
439 ALOGE("usecase tag only supported with GAIN_LEVEL_MAPPING section");
440 return;
441 }
442
443 section_process_fn fn = section_table[GAIN_LEVEL_MAPPING];
444 fn(attr);
445 } else if (strcmp(tag_name, "usecase") == 0) {
446 if (section != PCM_ID) {
447 ALOGE("usecase tag only supported with PCM_ID section");
448 return;
449 }
450
451 section_process_fn fn = section_table[PCM_ID];
452 fn(attr);
453 } else if (strcmp(tag_name, "param") == 0) {
454 if ((section != CONFIG_PARAMS) && (section != ACDB_METAINFO_KEY)) {
455 ALOGE("param tag only supported with CONFIG_PARAMS section");
456 return;
457 }
458
459 section_process_fn fn = section_table[section];
460 fn(attr);
Haynes Mathew Georgef4da6fe2014-06-20 19:14:25 -0700461 }
Vignesh Kulothungan55396882017-04-20 14:37:02 -0700462 } else {
463 ALOGE("%s: unknown caller!", __func__);
Haynes Mathew Georgef4da6fe2014-06-20 19:14:25 -0700464 }
Ben Romberger55886882014-01-10 13:49:02 -0800465 return;
466}
467
Haynes Mathew Georgef4da6fe2014-06-20 19:14:25 -0700468static void end_tag(void *userdata __unused, const XML_Char *tag_name)
Ben Romberger55886882014-01-10 13:49:02 -0800469{
Amit Shekhar5a39c912014-10-14 15:39:30 -0700470 if (strcmp(tag_name, "bit_width_configs") == 0) {
471 section = ROOT;
472 } else if (strcmp(tag_name, "acdb_ids") == 0) {
Haynes Mathew Georgef4da6fe2014-06-20 19:14:25 -0700473 section = ROOT;
474 } else if (strcmp(tag_name, "pcm_ids") == 0) {
475 section = ROOT;
476 } else if (strcmp(tag_name, "backend_names") == 0) {
477 section = ROOT;
Ravi Kumar Alamanda14b0f2d2015-06-28 21:04:09 -0700478 } else if (strcmp(tag_name, "config_params") == 0) {
479 section = ROOT;
Vignesh Kulothungan55396882017-04-20 14:37:02 -0700480 if (my_data.caller == PLATFORM) {
481 platform_set_parameters(my_data.platform, my_data.kvpairs);
482 }
Narsinga Rao Chellaf928a982015-03-06 14:57:35 -0800483 } else if (strcmp(tag_name, "interface_names") == 0) {
484 section = ROOT;
Aniket Kumar Lataf56b6402016-10-27 12:03:18 -0700485 } else if (strcmp(tag_name, "gain_db_to_level_mapping") == 0) {
486 section = ROOT;
Dhanalakshmi Siddani21be3ac2016-12-29 14:31:08 +0530487 } else if (strcmp(tag_name, "acdb_metainfo_key") == 0) {
488 section = ROOT;
Haynes Mathew Georgef4da6fe2014-06-20 19:14:25 -0700489 }
Ben Romberger55886882014-01-10 13:49:02 -0800490}
491
Vignesh Kulothungan55396882017-04-20 14:37:02 -0700492int platform_info_init(const char *filename, void *platform, caller_t caller_type)
Ben Romberger55886882014-01-10 13:49:02 -0800493{
494 XML_Parser parser;
495 FILE *file;
496 int ret = 0;
497 int bytes_read;
Ben Romberger55886882014-01-10 13:49:02 -0800498 void *buf;
499
Helen Zeng6a16ad72014-02-23 22:04:44 -0800500 file = fopen(filename, "r");
Haynes Mathew Georgef4da6fe2014-06-20 19:14:25 -0700501 section = ROOT;
502
Ben Romberger55886882014-01-10 13:49:02 -0800503 if (!file) {
504 ALOGD("%s: Failed to open %s, using defaults.",
Helen Zeng6a16ad72014-02-23 22:04:44 -0800505 __func__, filename);
Ben Romberger55886882014-01-10 13:49:02 -0800506 ret = -ENODEV;
507 goto done;
508 }
509
510 parser = XML_ParserCreate(NULL);
511 if (!parser) {
512 ALOGE("%s: Failed to create XML parser!", __func__);
513 ret = -ENODEV;
514 goto err_close_file;
515 }
516
Vignesh Kulothungan55396882017-04-20 14:37:02 -0700517 my_data.caller = caller_type;
Ravi Kumar Alamanda14b0f2d2015-06-28 21:04:09 -0700518 my_data.platform = platform;
519 my_data.kvpairs = str_parms_create();
520
Ben Romberger55886882014-01-10 13:49:02 -0800521 XML_SetElementHandler(parser, start_tag, end_tag);
522
523 while (1) {
524 buf = XML_GetBuffer(parser, BUF_SIZE);
525 if (buf == NULL) {
526 ALOGE("%s: XML_GetBuffer failed", __func__);
527 ret = -ENOMEM;
528 goto err_free_parser;
529 }
530
531 bytes_read = fread(buf, 1, BUF_SIZE, file);
532 if (bytes_read < 0) {
533 ALOGE("%s: fread failed, bytes read = %d", __func__, bytes_read);
534 ret = bytes_read;
535 goto err_free_parser;
536 }
537
538 if (XML_ParseBuffer(parser, bytes_read,
539 bytes_read == 0) == XML_STATUS_ERROR) {
540 ALOGE("%s: XML_ParseBuffer failed, for %s",
Helen Zeng6a16ad72014-02-23 22:04:44 -0800541 __func__, filename);
Ben Romberger55886882014-01-10 13:49:02 -0800542 ret = -EINVAL;
543 goto err_free_parser;
544 }
545
546 if (bytes_read == 0)
547 break;
548 }
549
Ben Romberger55886882014-01-10 13:49:02 -0800550err_free_parser:
551 XML_ParserFree(parser);
552err_close_file:
553 fclose(file);
554done:
555 return ret;
556}