blob: ac23f866b5c794bf5716b0905a54442a48e27690 [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>
Ben Romberger61764e32014-01-10 13:49:02 -080039#include "platform_api.h"
40#include <platform.h>
Aniket Kumar Lataf56b6402016-10-27 12:03:18 -070041#include <math.h>
Ben Romberger55886882014-01-10 13:49:02 -080042
Revathi Uddaraju1eac8b02017-05-18 17:13:33 +053043#ifdef DYNAMIC_LOG_ENABLED
44#include <log_xml_parser.h>
45#define LOG_MASK HAL_MOD_FILE_PLATFORM_INFO
46#include <log_utils.h>
47#endif
48
Ben Romberger55886882014-01-10 13:49:02 -080049#define BUF_SIZE 1024
50
Haynes Mathew Georgef4da6fe2014-06-20 19:14:25 -070051typedef enum {
52 ROOT,
53 ACDB,
Amit Shekhar5a39c912014-10-14 15:39:30 -070054 BITWIDTH,
Haynes Mathew Georgef4da6fe2014-06-20 19:14:25 -070055 PCM_ID,
56 BACKEND_NAME,
Narsinga Rao Chellaf928a982015-03-06 14:57:35 -080057 INTERFACE_NAME,
Ravi Kumar Alamanda14b0f2d2015-06-28 21:04:09 -070058 CONFIG_PARAMS,
Aniket Kumar Lataf56b6402016-10-27 12:03:18 -070059 GAIN_LEVEL_MAPPING,
Dhanalakshmi Siddani21be3ac2016-12-29 14:31:08 +053060 ACDB_METAINFO_KEY,
Haynes Mathew Georgef4da6fe2014-06-20 19:14:25 -070061} section_t;
62
63typedef void (* section_process_fn)(const XML_Char **attr);
64
65static void process_acdb_id(const XML_Char **attr);
Amit Shekhar5a39c912014-10-14 15:39:30 -070066static void process_bit_width(const XML_Char **attr);
Haynes Mathew Georgef4da6fe2014-06-20 19:14:25 -070067static void process_pcm_id(const XML_Char **attr);
68static void process_backend_name(const XML_Char **attr);
Narsinga Rao Chellaf928a982015-03-06 14:57:35 -080069static void process_interface_name(const XML_Char **attr);
Ravi Kumar Alamanda14b0f2d2015-06-28 21:04:09 -070070static void process_config_params(const XML_Char **attr);
Haynes Mathew Georgef4da6fe2014-06-20 19:14:25 -070071static void process_root(const XML_Char **attr);
Aniket Kumar Lataf56b6402016-10-27 12:03:18 -070072static void process_gain_db_to_level_map(const XML_Char **attr);
Dhanalakshmi Siddani21be3ac2016-12-29 14:31:08 +053073static void process_acdb_metainfo_key(const XML_Char **attr);
Haynes Mathew Georgef4da6fe2014-06-20 19:14:25 -070074
75static section_process_fn section_table[] = {
76 [ROOT] = process_root,
77 [ACDB] = process_acdb_id,
Amit Shekhar5a39c912014-10-14 15:39:30 -070078 [BITWIDTH] = process_bit_width,
Haynes Mathew Georgef4da6fe2014-06-20 19:14:25 -070079 [PCM_ID] = process_pcm_id,
80 [BACKEND_NAME] = process_backend_name,
Narsinga Rao Chellaf928a982015-03-06 14:57:35 -080081 [INTERFACE_NAME] = process_interface_name,
Ravi Kumar Alamanda14b0f2d2015-06-28 21:04:09 -070082 [CONFIG_PARAMS] = process_config_params,
Aniket Kumar Lataf56b6402016-10-27 12:03:18 -070083 [GAIN_LEVEL_MAPPING] = process_gain_db_to_level_map,
Dhanalakshmi Siddani21be3ac2016-12-29 14:31:08 +053084 [ACDB_METAINFO_KEY] = process_acdb_metainfo_key,
Haynes Mathew Georgef4da6fe2014-06-20 19:14:25 -070085};
86
87static section_t section;
88
Ravi Kumar Alamanda14b0f2d2015-06-28 21:04:09 -070089struct platform_info {
90 void *platform;
91 struct str_parms *kvpairs;
92};
93
94static struct platform_info my_data;
95
Haynes Mathew Georgef4da6fe2014-06-20 19:14:25 -070096/*
97 * <audio_platform_info>
98 * <acdb_ids>
99 * <device name="???" acdb_id="???"/>
100 * ...
101 * ...
102 * </acdb_ids>
103 * <backend_names>
104 * <device name="???" backend="???"/>
105 * ...
106 * ...
107 * </backend_names>
108 * <pcm_ids>
109 * <usecase name="???" type="in/out" id="???"/>
110 * ...
111 * ...
112 * </pcm_ids>
Narsinga Rao Chellaf928a982015-03-06 14:57:35 -0800113 * <interface_names>
Karthik Reddy Katta508eca42015-05-11 13:43:18 +0530114 * <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 -0800115 * ...
116 * ...
117 * </interface_names>
Ravi Kumar Alamanda14b0f2d2015-06-28 21:04:09 -0700118 * <config_params>
119 * <param key="snd_card_name" value="msm8994-tomtom-mtp-snd-card"/>
120 * ...
121 * ...
122 * </config_params>
Haynes Mathew Georgef4da6fe2014-06-20 19:14:25 -0700123 * </audio_platform_info>
124 */
125
126static void process_root(const XML_Char **attr __unused)
127{
128}
129
130/* mapping from usecase to pcm dev id */
131static void process_pcm_id(const XML_Char **attr)
132{
133 int index;
134
135 if (strcmp(attr[0], "name") != 0) {
Ravi Kumar Alamanda14b0f2d2015-06-28 21:04:09 -0700136 ALOGE("%s: 'name' not found, no pcm_id set!", __func__);
Haynes Mathew Georgef4da6fe2014-06-20 19:14:25 -0700137 goto done;
138 }
139
140 index = platform_get_usecase_index((char *)attr[1]);
141 if (index < 0) {
142 ALOGE("%s: usecase %s not found!",
143 __func__, attr[1]);
144 goto done;
145 }
146
147 if (strcmp(attr[2], "type") != 0) {
148 ALOGE("%s: usecase type not mentioned", __func__);
149 goto done;
150 }
151
152 int type = -1;
153
154 if (!strcasecmp((char *)attr[3], "in")) {
155 type = 1;
156 } else if (!strcasecmp((char *)attr[3], "out")) {
157 type = 0;
158 } else {
159 ALOGE("%s: type must be IN or OUT", __func__);
160 goto done;
161 }
162
163 if (strcmp(attr[4], "id") != 0) {
164 ALOGE("%s: usecase id not mentioned", __func__);
165 goto done;
166 }
167
168 int id = atoi((char *)attr[5]);
169
170 if (platform_set_usecase_pcm_id(index, type, id) < 0) {
171 ALOGE("%s: usecase %s type %d id %d was not set!",
172 __func__, attr[1], type, id);
173 goto done;
174 }
175
176done:
177 return;
178}
179
180/* backend to be used for a device */
181static void process_backend_name(const XML_Char **attr)
182{
183 int index;
Sidipotu Ashok9f0b16e2016-04-28 13:48:28 +0530184 char *hw_interface = NULL;
Ashish Jaind150d4c2017-02-03 18:44:34 +0530185 char *backend = NULL;
Haynes Mathew Georgef4da6fe2014-06-20 19:14:25 -0700186
187 if (strcmp(attr[0], "name") != 0) {
188 ALOGE("%s: 'name' not found, no ACDB ID set!", __func__);
189 goto done;
190 }
191
192 index = platform_get_snd_device_index((char *)attr[1]);
193 if (index < 0) {
194 ALOGE("%s: Device %s not found, no ACDB ID set!",
195 __func__, attr[1]);
196 goto done;
197 }
198
199 if (strcmp(attr[2], "backend") != 0) {
Ashish Jaind150d4c2017-02-03 18:44:34 +0530200 if (strcmp(attr[2], "interface") == 0)
201 hw_interface = (char *)attr[3];
202 } else {
203 backend = (char *)attr[3];
Haynes Mathew Georgef4da6fe2014-06-20 19:14:25 -0700204 }
205
Sidipotu Ashok9f0b16e2016-04-28 13:48:28 +0530206 if (attr[4] != NULL) {
207 if (strcmp(attr[4], "interface") != 0) {
208 hw_interface = NULL;
209 } else {
210 hw_interface = (char *)attr[5];
211 }
212 }
213
Ashish Jaind150d4c2017-02-03 18:44:34 +0530214 if (platform_set_snd_device_backend(index, backend, hw_interface) < 0) {
Haynes Mathew Georgef4da6fe2014-06-20 19:14:25 -0700215 ALOGE("%s: Device %s backend %s was not set!",
216 __func__, attr[1], attr[3]);
217 goto done;
218 }
219
220done:
221 return;
222}
223
Aniket Kumar Lataf56b6402016-10-27 12:03:18 -0700224static void process_gain_db_to_level_map(const XML_Char **attr)
225{
226 struct amp_db_and_gain_table tbl_entry;
227
228 if ((strcmp(attr[0], "db") != 0) ||
229 (strcmp(attr[2], "level") != 0)) {
230 ALOGE("%s: invalid attribute passed %s %sexpected amp db level",
231 __func__, attr[0], attr[2]);
232 goto done;
233 }
234
235 tbl_entry.db = atof(attr[1]);
236 tbl_entry.amp = exp(tbl_entry.db * 0.115129f);
237 tbl_entry.level = atoi(attr[3]);
238
239 ALOGV("%s: amp [%f] db [%f] level [%d]", __func__,
240 tbl_entry.amp, tbl_entry.db, tbl_entry.level);
241 platform_add_gain_level_mapping(&tbl_entry);
242
243done:
244 return;
245}
246
247
Haynes Mathew Georgef4da6fe2014-06-20 19:14:25 -0700248static void process_acdb_id(const XML_Char **attr)
Ben Romberger55886882014-01-10 13:49:02 -0800249{
Ben Romberger61764e32014-01-10 13:49:02 -0800250 int index;
Ben Romberger55886882014-01-10 13:49:02 -0800251
Ben Romberger61764e32014-01-10 13:49:02 -0800252 if (strcmp(attr[0], "name") != 0) {
253 ALOGE("%s: 'name' not found, no ACDB ID set!", __func__);
Ben Romberger55886882014-01-10 13:49:02 -0800254 goto done;
Ben Romberger61764e32014-01-10 13:49:02 -0800255 }
Ben Romberger55886882014-01-10 13:49:02 -0800256
Ben Romberger61764e32014-01-10 13:49:02 -0800257 index = platform_get_snd_device_index((char *)attr[1]);
258 if (index < 0) {
Helen Zeng6a16ad72014-02-23 22:04:44 -0800259 ALOGE("%s: Device %s in platform info xml not found, no ACDB ID set!",
260 __func__, attr[1]);
Ben Romberger55886882014-01-10 13:49:02 -0800261 goto done;
262 }
263
264 if (strcmp(attr[2], "acdb_id") != 0) {
Helen Zeng6a16ad72014-02-23 22:04:44 -0800265 ALOGE("%s: Device %s in platform info xml has no acdb_id, no ACDB ID set!",
266 __func__, attr[1]);
Ben Romberger55886882014-01-10 13:49:02 -0800267 goto done;
268 }
269
Haynes Mathew Georgef4da6fe2014-06-20 19:14:25 -0700270 if (platform_set_snd_device_acdb_id(index, atoi((char *)attr[3])) < 0) {
271 ALOGE("%s: Device %s, ACDB ID %d was not set!",
Helen Zeng6a16ad72014-02-23 22:04:44 -0800272 __func__, attr[1], atoi((char *)attr[3]));
Ben Romberger55886882014-01-10 13:49:02 -0800273 goto done;
Ben Romberger61764e32014-01-10 13:49:02 -0800274 }
Ben Romberger55886882014-01-10 13:49:02 -0800275
Ben Romberger55886882014-01-10 13:49:02 -0800276done:
277 return;
278}
279
Amit Shekhar5a39c912014-10-14 15:39:30 -0700280static void process_bit_width(const XML_Char **attr)
281{
282 int index;
283
284 if (strcmp(attr[0], "name") != 0) {
285 ALOGE("%s: 'name' not found, no ACDB ID set!", __func__);
286 goto done;
287 }
288
289 index = platform_get_snd_device_index((char *)attr[1]);
290 if (index < 0) {
291 ALOGE("%s: Device %s in platform info xml not found, no ACDB ID set!",
292 __func__, attr[1]);
293 goto done;
294 }
295
296 if (strcmp(attr[2], "bit_width") != 0) {
297 ALOGE("%s: Device %s in platform info xml has no bit_width, no ACDB ID set!",
298 __func__, attr[1]);
299 goto done;
300 }
301
302 if (platform_set_snd_device_bit_width(index, atoi((char *)attr[3])) < 0) {
303 ALOGE("%s: Device %s, ACDB ID %d was not set!",
304 __func__, attr[1], atoi((char *)attr[3]));
305 goto done;
306 }
307
308done:
309 return;
310}
311
Narsinga Rao Chellaf928a982015-03-06 14:57:35 -0800312static void process_interface_name(const XML_Char **attr)
313{
314 int ret;
315
316 if (strcmp(attr[0], "name") != 0) {
317 ALOGE("%s: 'name' not found, no Audio Interface set!", __func__);
318
319 goto done;
320 }
321
322 if (strcmp(attr[2], "interface") != 0) {
323 ALOGE("%s: Device %s has no Audio Interface set!",
324 __func__, attr[1]);
325
326 goto done;
327 }
328
Karthik Reddy Katta508eca42015-05-11 13:43:18 +0530329 if (strcmp(attr[4], "codec_type") != 0) {
330 ALOGE("%s: Device %s has no codec type set!",
331 __func__, attr[1]);
332
333 goto done;
334 }
335
336 ret = platform_set_audio_device_interface((char *)attr[1], (char *)attr[3],
337 (char *)attr[5]);
Narsinga Rao Chellaf928a982015-03-06 14:57:35 -0800338 if (ret < 0) {
339 ALOGE("%s: Audio Interface not set!", __func__);
Apoorv Raghuvanshi21492162015-02-19 18:19:36 -0800340 goto done;
341 }
Narsinga Rao Chellaf928a982015-03-06 14:57:35 -0800342
Apoorv Raghuvanshi21492162015-02-19 18:19:36 -0800343done:
344 return;
345}
Laxminath Kasam44f49402015-05-29 18:37:11 +0530346
Ravi Kumar Alamanda14b0f2d2015-06-28 21:04:09 -0700347static void process_config_params(const XML_Char **attr)
348{
349 if (strcmp(attr[0], "key") != 0) {
350 ALOGE("%s: 'key' not found", __func__);
351 goto done;
352 }
353
354 if (strcmp(attr[2], "value") != 0) {
355 ALOGE("%s: 'value' not found", __func__);
356 goto done;
357 }
358
359 str_parms_add_str(my_data.kvpairs, (char*)attr[1], (char*)attr[3]);
360done:
361 return;
362}
363
Dhanalakshmi Siddani21be3ac2016-12-29 14:31:08 +0530364/* process acdb meta info key value */
365static void process_acdb_metainfo_key(const XML_Char **attr)
366{
367 if (strcmp(attr[0], "name") != 0) {
368 ALOGE("%s: 'name' not found", __func__);
369 goto done;
370 }
371
372 if (strcmp(attr[2], "value") != 0) {
373 ALOGE("%s: 'value' not found", __func__);
374 goto done;
375 }
376
377 int key = atoi((char *)attr[3]);
378 if (platform_set_acdb_metainfo_key(my_data.platform, (char*)attr[1], key) < 0) {
379 ALOGE("%s: key %d was not set!", __func__, key);
380 goto done;
381 }
382
383done:
384 return;
385}
386
Haynes Mathew Georgef4da6fe2014-06-20 19:14:25 -0700387static void start_tag(void *userdata __unused, const XML_Char *tag_name,
Ben Romberger55886882014-01-10 13:49:02 -0800388 const XML_Char **attr)
389{
Amit Shekhar5a39c912014-10-14 15:39:30 -0700390 if (strcmp(tag_name, "bit_width_configs") == 0) {
391 section = BITWIDTH;
392 } else if (strcmp(tag_name, "acdb_ids") == 0) {
Haynes Mathew Georgef4da6fe2014-06-20 19:14:25 -0700393 section = ACDB;
394 } else if (strcmp(tag_name, "pcm_ids") == 0) {
395 section = PCM_ID;
396 } else if (strcmp(tag_name, "backend_names") == 0) {
397 section = BACKEND_NAME;
Ravi Kumar Alamanda14b0f2d2015-06-28 21:04:09 -0700398 } else if (strcmp(tag_name, "config_params") == 0) {
399 section = CONFIG_PARAMS;
Narsinga Rao Chellaf928a982015-03-06 14:57:35 -0800400 } else if (strcmp(tag_name, "interface_names") == 0) {
401 section = INTERFACE_NAME;
Aniket Kumar Lataf56b6402016-10-27 12:03:18 -0700402 } else if (strcmp(tag_name, "gain_db_to_level_mapping") == 0) {
403 section = GAIN_LEVEL_MAPPING;
Dhanalakshmi Siddani21be3ac2016-12-29 14:31:08 +0530404 } else if(strcmp(tag_name, "acdb_metainfo_key") == 0) {
405 section = ACDB_METAINFO_KEY;
Haynes Mathew Georgef4da6fe2014-06-20 19:14:25 -0700406 } else if (strcmp(tag_name, "device") == 0) {
Narsinga Rao Chellaf928a982015-03-06 14:57:35 -0800407 if ((section != ACDB) && (section != BACKEND_NAME) && (section != BITWIDTH) &&
Laxminath Kasam167ade52016-05-23 17:46:51 +0530408 (section != INTERFACE_NAME)) {
409 ALOGE("device tag only supported for acdb/backend names/bitwitdh/interface names");
Haynes Mathew Georgef4da6fe2014-06-20 19:14:25 -0700410 return;
411 }
412
413 /* call into process function for the current section */
414 section_process_fn fn = section_table[section];
415 fn(attr);
Aniket Kumar Lataf56b6402016-10-27 12:03:18 -0700416 } else if (strcmp(tag_name, "gain_level_map") == 0) {
417 if (section != GAIN_LEVEL_MAPPING) {
418 ALOGE("usecase tag only supported with GAIN_LEVEL_MAPPING section");
419 return;
420 }
421
422 section_process_fn fn = section_table[GAIN_LEVEL_MAPPING];
423 fn(attr);
Haynes Mathew Georgef4da6fe2014-06-20 19:14:25 -0700424 } else if (strcmp(tag_name, "usecase") == 0) {
425 if (section != PCM_ID) {
426 ALOGE("usecase tag only supported with PCM_ID section");
427 return;
428 }
429
430 section_process_fn fn = section_table[PCM_ID];
431 fn(attr);
Ravi Kumar Alamanda14b0f2d2015-06-28 21:04:09 -0700432 } else if (strcmp(tag_name, "param") == 0) {
Dhanalakshmi Siddani21be3ac2016-12-29 14:31:08 +0530433 if ((section != CONFIG_PARAMS) && (section != ACDB_METAINFO_KEY)) {
Ravi Kumar Alamanda14b0f2d2015-06-28 21:04:09 -0700434 ALOGE("param tag only supported with CONFIG_PARAMS section");
435 return;
436 }
437
438 section_process_fn fn = section_table[section];
439 fn(attr);
Haynes Mathew Georgef4da6fe2014-06-20 19:14:25 -0700440 }
Ben Romberger55886882014-01-10 13:49:02 -0800441
442 return;
443}
444
Haynes Mathew Georgef4da6fe2014-06-20 19:14:25 -0700445static void end_tag(void *userdata __unused, const XML_Char *tag_name)
Ben Romberger55886882014-01-10 13:49:02 -0800446{
Amit Shekhar5a39c912014-10-14 15:39:30 -0700447 if (strcmp(tag_name, "bit_width_configs") == 0) {
448 section = ROOT;
449 } else if (strcmp(tag_name, "acdb_ids") == 0) {
Haynes Mathew Georgef4da6fe2014-06-20 19:14:25 -0700450 section = ROOT;
451 } else if (strcmp(tag_name, "pcm_ids") == 0) {
452 section = ROOT;
453 } else if (strcmp(tag_name, "backend_names") == 0) {
454 section = ROOT;
Ravi Kumar Alamanda14b0f2d2015-06-28 21:04:09 -0700455 } else if (strcmp(tag_name, "config_params") == 0) {
456 section = ROOT;
457 platform_set_parameters(my_data.platform, my_data.kvpairs);
Narsinga Rao Chellaf928a982015-03-06 14:57:35 -0800458 } else if (strcmp(tag_name, "interface_names") == 0) {
459 section = ROOT;
Aniket Kumar Lataf56b6402016-10-27 12:03:18 -0700460 } else if (strcmp(tag_name, "gain_db_to_level_mapping") == 0) {
461 section = ROOT;
Dhanalakshmi Siddani21be3ac2016-12-29 14:31:08 +0530462 } else if (strcmp(tag_name, "acdb_metainfo_key") == 0) {
463 section = ROOT;
Haynes Mathew Georgef4da6fe2014-06-20 19:14:25 -0700464 }
Ben Romberger55886882014-01-10 13:49:02 -0800465}
466
Ravi Kumar Alamanda14b0f2d2015-06-28 21:04:09 -0700467int platform_info_init(const char *filename, void *platform)
Ben Romberger55886882014-01-10 13:49:02 -0800468{
469 XML_Parser parser;
470 FILE *file;
471 int ret = 0;
472 int bytes_read;
Ben Romberger55886882014-01-10 13:49:02 -0800473 void *buf;
474
Helen Zeng6a16ad72014-02-23 22:04:44 -0800475 file = fopen(filename, "r");
Haynes Mathew Georgef4da6fe2014-06-20 19:14:25 -0700476 section = ROOT;
477
Ben Romberger55886882014-01-10 13:49:02 -0800478 if (!file) {
479 ALOGD("%s: Failed to open %s, using defaults.",
Helen Zeng6a16ad72014-02-23 22:04:44 -0800480 __func__, filename);
Ben Romberger55886882014-01-10 13:49:02 -0800481 ret = -ENODEV;
482 goto done;
483 }
484
485 parser = XML_ParserCreate(NULL);
486 if (!parser) {
487 ALOGE("%s: Failed to create XML parser!", __func__);
488 ret = -ENODEV;
489 goto err_close_file;
490 }
491
Ravi Kumar Alamanda14b0f2d2015-06-28 21:04:09 -0700492 my_data.platform = platform;
493 my_data.kvpairs = str_parms_create();
494
Ben Romberger55886882014-01-10 13:49:02 -0800495 XML_SetElementHandler(parser, start_tag, end_tag);
496
497 while (1) {
498 buf = XML_GetBuffer(parser, BUF_SIZE);
499 if (buf == NULL) {
500 ALOGE("%s: XML_GetBuffer failed", __func__);
501 ret = -ENOMEM;
502 goto err_free_parser;
503 }
504
505 bytes_read = fread(buf, 1, BUF_SIZE, file);
506 if (bytes_read < 0) {
507 ALOGE("%s: fread failed, bytes read = %d", __func__, bytes_read);
508 ret = bytes_read;
509 goto err_free_parser;
510 }
511
512 if (XML_ParseBuffer(parser, bytes_read,
513 bytes_read == 0) == XML_STATUS_ERROR) {
514 ALOGE("%s: XML_ParseBuffer failed, for %s",
Helen Zeng6a16ad72014-02-23 22:04:44 -0800515 __func__, filename);
Ben Romberger55886882014-01-10 13:49:02 -0800516 ret = -EINVAL;
517 goto err_free_parser;
518 }
519
520 if (bytes_read == 0)
521 break;
522 }
523
Ben Romberger55886882014-01-10 13:49:02 -0800524err_free_parser:
525 XML_ParserFree(parser);
526err_close_file:
527 fclose(file);
528done:
529 return ret;
530}