Ben Romberger | 5588688 | 2014-01-10 13:49:02 -0800 | [diff] [blame] | 1 | /* |
Narsinga Rao Chella | f928a98 | 2015-03-06 14:57:35 -0800 | [diff] [blame] | 2 | * Copyright (c) 2014-2015, The Linux Foundation. All rights reserved. |
Ben Romberger | 5588688 | 2014-01-10 13:49:02 -0800 | [diff] [blame] | 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 Romberger | 61764e3 | 2014-01-10 13:49:02 -0800 | [diff] [blame] | 30 | #define LOG_TAG "platform_info" |
Ben Romberger | 5588688 | 2014-01-10 13:49:02 -0800 | [diff] [blame] | 31 | #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 Romberger | 61764e3 | 2014-01-10 13:49:02 -0800 | [diff] [blame] | 38 | #include "platform_api.h" |
| 39 | #include <platform.h> |
Ben Romberger | 5588688 | 2014-01-10 13:49:02 -0800 | [diff] [blame] | 40 | |
Ben Romberger | 5588688 | 2014-01-10 13:49:02 -0800 | [diff] [blame] | 41 | #define BUF_SIZE 1024 |
| 42 | |
Haynes Mathew George | f4da6fe | 2014-06-20 19:14:25 -0700 | [diff] [blame] | 43 | typedef enum { |
| 44 | ROOT, |
| 45 | ACDB, |
Amit Shekhar | 5a39c91 | 2014-10-14 15:39:30 -0700 | [diff] [blame] | 46 | BITWIDTH, |
Apoorv Raghuvanshi | 2149216 | 2015-02-19 18:19:36 -0800 | [diff] [blame^] | 47 | NATIVESUPPORT, |
Haynes Mathew George | f4da6fe | 2014-06-20 19:14:25 -0700 | [diff] [blame] | 48 | PCM_ID, |
| 49 | BACKEND_NAME, |
Narsinga Rao Chella | f928a98 | 2015-03-06 14:57:35 -0800 | [diff] [blame] | 50 | INTERFACE_NAME, |
Haynes Mathew George | f4da6fe | 2014-06-20 19:14:25 -0700 | [diff] [blame] | 51 | } section_t; |
| 52 | |
| 53 | typedef void (* section_process_fn)(const XML_Char **attr); |
| 54 | |
| 55 | static void process_acdb_id(const XML_Char **attr); |
Amit Shekhar | 5a39c91 | 2014-10-14 15:39:30 -0700 | [diff] [blame] | 56 | static void process_bit_width(const XML_Char **attr); |
Apoorv Raghuvanshi | 2149216 | 2015-02-19 18:19:36 -0800 | [diff] [blame^] | 57 | static void process_native_support(const XML_Char **attr); |
Haynes Mathew George | f4da6fe | 2014-06-20 19:14:25 -0700 | [diff] [blame] | 58 | static void process_pcm_id(const XML_Char **attr); |
| 59 | static void process_backend_name(const XML_Char **attr); |
Narsinga Rao Chella | f928a98 | 2015-03-06 14:57:35 -0800 | [diff] [blame] | 60 | static void process_interface_name(const XML_Char **attr); |
Haynes Mathew George | f4da6fe | 2014-06-20 19:14:25 -0700 | [diff] [blame] | 61 | static void process_root(const XML_Char **attr); |
| 62 | |
| 63 | static section_process_fn section_table[] = { |
| 64 | [ROOT] = process_root, |
| 65 | [ACDB] = process_acdb_id, |
Amit Shekhar | 5a39c91 | 2014-10-14 15:39:30 -0700 | [diff] [blame] | 66 | [BITWIDTH] = process_bit_width, |
Apoorv Raghuvanshi | 2149216 | 2015-02-19 18:19:36 -0800 | [diff] [blame^] | 67 | [NATIVESUPPORT] = process_native_support, |
Haynes Mathew George | f4da6fe | 2014-06-20 19:14:25 -0700 | [diff] [blame] | 68 | [PCM_ID] = process_pcm_id, |
| 69 | [BACKEND_NAME] = process_backend_name, |
Narsinga Rao Chella | f928a98 | 2015-03-06 14:57:35 -0800 | [diff] [blame] | 70 | [INTERFACE_NAME] = process_interface_name, |
Haynes Mathew George | f4da6fe | 2014-06-20 19:14:25 -0700 | [diff] [blame] | 71 | }; |
| 72 | |
| 73 | static section_t section; |
| 74 | |
| 75 | /* |
| 76 | * <audio_platform_info> |
| 77 | * <acdb_ids> |
| 78 | * <device name="???" acdb_id="???"/> |
| 79 | * ... |
| 80 | * ... |
| 81 | * </acdb_ids> |
| 82 | * <backend_names> |
| 83 | * <device name="???" backend="???"/> |
| 84 | * ... |
| 85 | * ... |
| 86 | * </backend_names> |
| 87 | * <pcm_ids> |
| 88 | * <usecase name="???" type="in/out" id="???"/> |
| 89 | * ... |
| 90 | * ... |
| 91 | * </pcm_ids> |
Narsinga Rao Chella | f928a98 | 2015-03-06 14:57:35 -0800 | [diff] [blame] | 92 | * <interface_names> |
Karthik Reddy Katta | 508eca4 | 2015-05-11 13:43:18 +0530 | [diff] [blame] | 93 | * <device name="Use audio device name here, not sound device name" interface="PRIMARY_I2S" codec_type="external/internal"/> |
Narsinga Rao Chella | f928a98 | 2015-03-06 14:57:35 -0800 | [diff] [blame] | 94 | * ... |
| 95 | * ... |
| 96 | * </interface_names> |
Haynes Mathew George | f4da6fe | 2014-06-20 19:14:25 -0700 | [diff] [blame] | 97 | * </audio_platform_info> |
| 98 | */ |
| 99 | |
| 100 | static void process_root(const XML_Char **attr __unused) |
| 101 | { |
| 102 | } |
| 103 | |
| 104 | /* mapping from usecase to pcm dev id */ |
| 105 | static void process_pcm_id(const XML_Char **attr) |
| 106 | { |
| 107 | int index; |
| 108 | |
| 109 | if (strcmp(attr[0], "name") != 0) { |
| 110 | ALOGE("%s: 'name' not found, no ACDB ID set!", __func__); |
| 111 | goto done; |
| 112 | } |
| 113 | |
| 114 | index = platform_get_usecase_index((char *)attr[1]); |
| 115 | if (index < 0) { |
| 116 | ALOGE("%s: usecase %s not found!", |
| 117 | __func__, attr[1]); |
| 118 | goto done; |
| 119 | } |
| 120 | |
| 121 | if (strcmp(attr[2], "type") != 0) { |
| 122 | ALOGE("%s: usecase type not mentioned", __func__); |
| 123 | goto done; |
| 124 | } |
| 125 | |
| 126 | int type = -1; |
| 127 | |
| 128 | if (!strcasecmp((char *)attr[3], "in")) { |
| 129 | type = 1; |
| 130 | } else if (!strcasecmp((char *)attr[3], "out")) { |
| 131 | type = 0; |
| 132 | } else { |
| 133 | ALOGE("%s: type must be IN or OUT", __func__); |
| 134 | goto done; |
| 135 | } |
| 136 | |
| 137 | if (strcmp(attr[4], "id") != 0) { |
| 138 | ALOGE("%s: usecase id not mentioned", __func__); |
| 139 | goto done; |
| 140 | } |
| 141 | |
| 142 | int id = atoi((char *)attr[5]); |
| 143 | |
| 144 | if (platform_set_usecase_pcm_id(index, type, id) < 0) { |
| 145 | ALOGE("%s: usecase %s type %d id %d was not set!", |
| 146 | __func__, attr[1], type, id); |
| 147 | goto done; |
| 148 | } |
| 149 | |
| 150 | done: |
| 151 | return; |
| 152 | } |
| 153 | |
| 154 | /* backend to be used for a device */ |
| 155 | static void process_backend_name(const XML_Char **attr) |
| 156 | { |
| 157 | int index; |
| 158 | |
| 159 | if (strcmp(attr[0], "name") != 0) { |
| 160 | ALOGE("%s: 'name' not found, no ACDB ID set!", __func__); |
| 161 | goto done; |
| 162 | } |
| 163 | |
| 164 | index = platform_get_snd_device_index((char *)attr[1]); |
| 165 | if (index < 0) { |
| 166 | ALOGE("%s: Device %s not found, no ACDB ID set!", |
| 167 | __func__, attr[1]); |
| 168 | goto done; |
| 169 | } |
| 170 | |
| 171 | if (strcmp(attr[2], "backend") != 0) { |
| 172 | ALOGE("%s: Device %s has no backend set!", |
| 173 | __func__, attr[1]); |
| 174 | goto done; |
| 175 | } |
| 176 | |
| 177 | if (platform_set_snd_device_backend(index, attr[3]) < 0) { |
| 178 | ALOGE("%s: Device %s backend %s was not set!", |
| 179 | __func__, attr[1], attr[3]); |
| 180 | goto done; |
| 181 | } |
| 182 | |
| 183 | done: |
| 184 | return; |
| 185 | } |
| 186 | |
| 187 | static void process_acdb_id(const XML_Char **attr) |
Ben Romberger | 5588688 | 2014-01-10 13:49:02 -0800 | [diff] [blame] | 188 | { |
Ben Romberger | 61764e3 | 2014-01-10 13:49:02 -0800 | [diff] [blame] | 189 | int index; |
Ben Romberger | 5588688 | 2014-01-10 13:49:02 -0800 | [diff] [blame] | 190 | |
Ben Romberger | 61764e3 | 2014-01-10 13:49:02 -0800 | [diff] [blame] | 191 | if (strcmp(attr[0], "name") != 0) { |
| 192 | ALOGE("%s: 'name' not found, no ACDB ID set!", __func__); |
Ben Romberger | 5588688 | 2014-01-10 13:49:02 -0800 | [diff] [blame] | 193 | goto done; |
Ben Romberger | 61764e3 | 2014-01-10 13:49:02 -0800 | [diff] [blame] | 194 | } |
Ben Romberger | 5588688 | 2014-01-10 13:49:02 -0800 | [diff] [blame] | 195 | |
Ben Romberger | 61764e3 | 2014-01-10 13:49:02 -0800 | [diff] [blame] | 196 | index = platform_get_snd_device_index((char *)attr[1]); |
| 197 | if (index < 0) { |
Helen Zeng | 6a16ad7 | 2014-02-23 22:04:44 -0800 | [diff] [blame] | 198 | ALOGE("%s: Device %s in platform info xml not found, no ACDB ID set!", |
| 199 | __func__, attr[1]); |
Ben Romberger | 5588688 | 2014-01-10 13:49:02 -0800 | [diff] [blame] | 200 | goto done; |
| 201 | } |
| 202 | |
| 203 | if (strcmp(attr[2], "acdb_id") != 0) { |
Helen Zeng | 6a16ad7 | 2014-02-23 22:04:44 -0800 | [diff] [blame] | 204 | ALOGE("%s: Device %s in platform info xml has no acdb_id, no ACDB ID set!", |
| 205 | __func__, attr[1]); |
Ben Romberger | 5588688 | 2014-01-10 13:49:02 -0800 | [diff] [blame] | 206 | goto done; |
| 207 | } |
| 208 | |
Haynes Mathew George | f4da6fe | 2014-06-20 19:14:25 -0700 | [diff] [blame] | 209 | if (platform_set_snd_device_acdb_id(index, atoi((char *)attr[3])) < 0) { |
| 210 | ALOGE("%s: Device %s, ACDB ID %d was not set!", |
Helen Zeng | 6a16ad7 | 2014-02-23 22:04:44 -0800 | [diff] [blame] | 211 | __func__, attr[1], atoi((char *)attr[3])); |
Ben Romberger | 5588688 | 2014-01-10 13:49:02 -0800 | [diff] [blame] | 212 | goto done; |
Ben Romberger | 61764e3 | 2014-01-10 13:49:02 -0800 | [diff] [blame] | 213 | } |
Ben Romberger | 5588688 | 2014-01-10 13:49:02 -0800 | [diff] [blame] | 214 | |
Ben Romberger | 5588688 | 2014-01-10 13:49:02 -0800 | [diff] [blame] | 215 | done: |
| 216 | return; |
| 217 | } |
| 218 | |
Amit Shekhar | 5a39c91 | 2014-10-14 15:39:30 -0700 | [diff] [blame] | 219 | static void process_bit_width(const XML_Char **attr) |
| 220 | { |
| 221 | int index; |
| 222 | |
| 223 | if (strcmp(attr[0], "name") != 0) { |
| 224 | ALOGE("%s: 'name' not found, no ACDB ID set!", __func__); |
| 225 | goto done; |
| 226 | } |
| 227 | |
| 228 | index = platform_get_snd_device_index((char *)attr[1]); |
| 229 | if (index < 0) { |
| 230 | ALOGE("%s: Device %s in platform info xml not found, no ACDB ID set!", |
| 231 | __func__, attr[1]); |
| 232 | goto done; |
| 233 | } |
| 234 | |
| 235 | if (strcmp(attr[2], "bit_width") != 0) { |
| 236 | ALOGE("%s: Device %s in platform info xml has no bit_width, no ACDB ID set!", |
| 237 | __func__, attr[1]); |
| 238 | goto done; |
| 239 | } |
| 240 | |
| 241 | if (platform_set_snd_device_bit_width(index, atoi((char *)attr[3])) < 0) { |
| 242 | ALOGE("%s: Device %s, ACDB ID %d was not set!", |
| 243 | __func__, attr[1], atoi((char *)attr[3])); |
| 244 | goto done; |
| 245 | } |
| 246 | |
| 247 | done: |
| 248 | return; |
| 249 | } |
| 250 | |
Narsinga Rao Chella | f928a98 | 2015-03-06 14:57:35 -0800 | [diff] [blame] | 251 | static void process_interface_name(const XML_Char **attr) |
| 252 | { |
| 253 | int ret; |
| 254 | |
| 255 | if (strcmp(attr[0], "name") != 0) { |
| 256 | ALOGE("%s: 'name' not found, no Audio Interface set!", __func__); |
| 257 | |
| 258 | goto done; |
| 259 | } |
| 260 | |
| 261 | if (strcmp(attr[2], "interface") != 0) { |
| 262 | ALOGE("%s: Device %s has no Audio Interface set!", |
| 263 | __func__, attr[1]); |
| 264 | |
| 265 | goto done; |
| 266 | } |
| 267 | |
Karthik Reddy Katta | 508eca4 | 2015-05-11 13:43:18 +0530 | [diff] [blame] | 268 | if (strcmp(attr[4], "codec_type") != 0) { |
| 269 | ALOGE("%s: Device %s has no codec type set!", |
| 270 | __func__, attr[1]); |
| 271 | |
| 272 | goto done; |
| 273 | } |
| 274 | |
| 275 | ret = platform_set_audio_device_interface((char *)attr[1], (char *)attr[3], |
| 276 | (char *)attr[5]); |
Narsinga Rao Chella | f928a98 | 2015-03-06 14:57:35 -0800 | [diff] [blame] | 277 | if (ret < 0) { |
| 278 | ALOGE("%s: Audio Interface not set!", __func__); |
Apoorv Raghuvanshi | 2149216 | 2015-02-19 18:19:36 -0800 | [diff] [blame^] | 279 | goto done; |
| 280 | } |
Narsinga Rao Chella | f928a98 | 2015-03-06 14:57:35 -0800 | [diff] [blame] | 281 | |
Apoorv Raghuvanshi | 2149216 | 2015-02-19 18:19:36 -0800 | [diff] [blame^] | 282 | done: |
| 283 | return; |
| 284 | } |
| 285 | |
| 286 | static void process_native_support(const XML_Char **attr) |
| 287 | { |
| 288 | int index; |
| 289 | |
| 290 | if (strcmp(attr[0], "name") != 0) { |
| 291 | ALOGE("%s: 'name' not found, no NATIVE_AUDIO_44.1 set!", __func__); |
| 292 | goto done; |
| 293 | } |
| 294 | |
| 295 | if (strcmp(attr[2], "codec_support") != 0) { |
| 296 | ALOGE("%s: NATIVE_AUDIO_44.1 in platform info xml has no codec_support set!", |
| 297 | __func__); |
| 298 | goto done; |
| 299 | } |
| 300 | |
| 301 | if (platform_set_native_support(atoi((char *)attr[3])) < 0) { |
| 302 | ALOGE("%s: Device %s, ACDB ID %d was not set!", |
| 303 | __func__, attr[1], atoi((char *)attr[3])); |
Narsinga Rao Chella | f928a98 | 2015-03-06 14:57:35 -0800 | [diff] [blame] | 304 | goto done; |
| 305 | } |
| 306 | |
| 307 | done: |
| 308 | return; |
| 309 | } |
| 310 | |
Haynes Mathew George | f4da6fe | 2014-06-20 19:14:25 -0700 | [diff] [blame] | 311 | static void start_tag(void *userdata __unused, const XML_Char *tag_name, |
Ben Romberger | 5588688 | 2014-01-10 13:49:02 -0800 | [diff] [blame] | 312 | const XML_Char **attr) |
| 313 | { |
| 314 | const XML_Char *attr_name = NULL; |
| 315 | const XML_Char *attr_value = NULL; |
| 316 | unsigned int i; |
| 317 | |
Amit Shekhar | 5a39c91 | 2014-10-14 15:39:30 -0700 | [diff] [blame] | 318 | if (strcmp(tag_name, "bit_width_configs") == 0) { |
| 319 | section = BITWIDTH; |
| 320 | } else if (strcmp(tag_name, "acdb_ids") == 0) { |
Haynes Mathew George | f4da6fe | 2014-06-20 19:14:25 -0700 | [diff] [blame] | 321 | section = ACDB; |
| 322 | } else if (strcmp(tag_name, "pcm_ids") == 0) { |
| 323 | section = PCM_ID; |
| 324 | } else if (strcmp(tag_name, "backend_names") == 0) { |
| 325 | section = BACKEND_NAME; |
Narsinga Rao Chella | f928a98 | 2015-03-06 14:57:35 -0800 | [diff] [blame] | 326 | } else if (strcmp(tag_name, "interface_names") == 0) { |
| 327 | section = INTERFACE_NAME; |
Apoorv Raghuvanshi | 2149216 | 2015-02-19 18:19:36 -0800 | [diff] [blame^] | 328 | } else if (strcmp(tag_name, "native_configs") == 0) { |
| 329 | section = NATIVESUPPORT; |
Haynes Mathew George | f4da6fe | 2014-06-20 19:14:25 -0700 | [diff] [blame] | 330 | } else if (strcmp(tag_name, "device") == 0) { |
Narsinga Rao Chella | f928a98 | 2015-03-06 14:57:35 -0800 | [diff] [blame] | 331 | if ((section != ACDB) && (section != BACKEND_NAME) && (section != BITWIDTH) && |
| 332 | (section != INTERFACE_NAME)) { |
| 333 | ALOGE("device tag only supported for acdb/backend names/bitwitdh/interface names"); |
Haynes Mathew George | f4da6fe | 2014-06-20 19:14:25 -0700 | [diff] [blame] | 334 | return; |
| 335 | } |
| 336 | |
| 337 | /* call into process function for the current section */ |
| 338 | section_process_fn fn = section_table[section]; |
| 339 | fn(attr); |
| 340 | } else if (strcmp(tag_name, "usecase") == 0) { |
| 341 | if (section != PCM_ID) { |
| 342 | ALOGE("usecase tag only supported with PCM_ID section"); |
| 343 | return; |
| 344 | } |
| 345 | |
| 346 | section_process_fn fn = section_table[PCM_ID]; |
| 347 | fn(attr); |
Apoorv Raghuvanshi | 2149216 | 2015-02-19 18:19:36 -0800 | [diff] [blame^] | 348 | } else if (strcmp(tag_name, "feature") == 0) { |
| 349 | if (section != NATIVESUPPORT) { |
| 350 | ALOGE("usecase tag only supported with NATIVESUPPORT section"); |
| 351 | return; |
| 352 | } |
| 353 | |
| 354 | section_process_fn fn = section_table[NATIVESUPPORT]; |
| 355 | fn(attr); |
Haynes Mathew George | f4da6fe | 2014-06-20 19:14:25 -0700 | [diff] [blame] | 356 | } |
Ben Romberger | 5588688 | 2014-01-10 13:49:02 -0800 | [diff] [blame] | 357 | |
| 358 | return; |
| 359 | } |
| 360 | |
Haynes Mathew George | f4da6fe | 2014-06-20 19:14:25 -0700 | [diff] [blame] | 361 | static void end_tag(void *userdata __unused, const XML_Char *tag_name) |
Ben Romberger | 5588688 | 2014-01-10 13:49:02 -0800 | [diff] [blame] | 362 | { |
Amit Shekhar | 5a39c91 | 2014-10-14 15:39:30 -0700 | [diff] [blame] | 363 | if (strcmp(tag_name, "bit_width_configs") == 0) { |
| 364 | section = ROOT; |
| 365 | } else if (strcmp(tag_name, "acdb_ids") == 0) { |
Haynes Mathew George | f4da6fe | 2014-06-20 19:14:25 -0700 | [diff] [blame] | 366 | section = ROOT; |
| 367 | } else if (strcmp(tag_name, "pcm_ids") == 0) { |
| 368 | section = ROOT; |
| 369 | } else if (strcmp(tag_name, "backend_names") == 0) { |
| 370 | section = ROOT; |
Narsinga Rao Chella | f928a98 | 2015-03-06 14:57:35 -0800 | [diff] [blame] | 371 | } else if (strcmp(tag_name, "interface_names") == 0) { |
| 372 | section = ROOT; |
Apoorv Raghuvanshi | 2149216 | 2015-02-19 18:19:36 -0800 | [diff] [blame^] | 373 | } else if (strcmp(tag_name, "native_configs") == 0) { |
| 374 | section = ROOT; |
Haynes Mathew George | f4da6fe | 2014-06-20 19:14:25 -0700 | [diff] [blame] | 375 | } |
Ben Romberger | 5588688 | 2014-01-10 13:49:02 -0800 | [diff] [blame] | 376 | } |
| 377 | |
Helen Zeng | 6a16ad7 | 2014-02-23 22:04:44 -0800 | [diff] [blame] | 378 | int platform_info_init(const char *filename) |
Ben Romberger | 5588688 | 2014-01-10 13:49:02 -0800 | [diff] [blame] | 379 | { |
| 380 | XML_Parser parser; |
| 381 | FILE *file; |
| 382 | int ret = 0; |
| 383 | int bytes_read; |
Ben Romberger | 5588688 | 2014-01-10 13:49:02 -0800 | [diff] [blame] | 384 | void *buf; |
| 385 | |
Helen Zeng | 6a16ad7 | 2014-02-23 22:04:44 -0800 | [diff] [blame] | 386 | file = fopen(filename, "r"); |
Haynes Mathew George | f4da6fe | 2014-06-20 19:14:25 -0700 | [diff] [blame] | 387 | section = ROOT; |
| 388 | |
Ben Romberger | 5588688 | 2014-01-10 13:49:02 -0800 | [diff] [blame] | 389 | if (!file) { |
| 390 | ALOGD("%s: Failed to open %s, using defaults.", |
Helen Zeng | 6a16ad7 | 2014-02-23 22:04:44 -0800 | [diff] [blame] | 391 | __func__, filename); |
Ben Romberger | 5588688 | 2014-01-10 13:49:02 -0800 | [diff] [blame] | 392 | ret = -ENODEV; |
| 393 | goto done; |
| 394 | } |
| 395 | |
| 396 | parser = XML_ParserCreate(NULL); |
| 397 | if (!parser) { |
| 398 | ALOGE("%s: Failed to create XML parser!", __func__); |
| 399 | ret = -ENODEV; |
| 400 | goto err_close_file; |
| 401 | } |
| 402 | |
Ben Romberger | 5588688 | 2014-01-10 13:49:02 -0800 | [diff] [blame] | 403 | XML_SetElementHandler(parser, start_tag, end_tag); |
| 404 | |
| 405 | while (1) { |
| 406 | buf = XML_GetBuffer(parser, BUF_SIZE); |
| 407 | if (buf == NULL) { |
| 408 | ALOGE("%s: XML_GetBuffer failed", __func__); |
| 409 | ret = -ENOMEM; |
| 410 | goto err_free_parser; |
| 411 | } |
| 412 | |
| 413 | bytes_read = fread(buf, 1, BUF_SIZE, file); |
| 414 | if (bytes_read < 0) { |
| 415 | ALOGE("%s: fread failed, bytes read = %d", __func__, bytes_read); |
| 416 | ret = bytes_read; |
| 417 | goto err_free_parser; |
| 418 | } |
| 419 | |
| 420 | if (XML_ParseBuffer(parser, bytes_read, |
| 421 | bytes_read == 0) == XML_STATUS_ERROR) { |
| 422 | ALOGE("%s: XML_ParseBuffer failed, for %s", |
Helen Zeng | 6a16ad7 | 2014-02-23 22:04:44 -0800 | [diff] [blame] | 423 | __func__, filename); |
Ben Romberger | 5588688 | 2014-01-10 13:49:02 -0800 | [diff] [blame] | 424 | ret = -EINVAL; |
| 425 | goto err_free_parser; |
| 426 | } |
| 427 | |
| 428 | if (bytes_read == 0) |
| 429 | break; |
| 430 | } |
| 431 | |
Ben Romberger | 5588688 | 2014-01-10 13:49:02 -0800 | [diff] [blame] | 432 | err_free_parser: |
| 433 | XML_ParserFree(parser); |
| 434 | err_close_file: |
| 435 | fclose(file); |
| 436 | done: |
| 437 | return ret; |
| 438 | } |