Ben Romberger | 5588688 | 2014-01-10 13:49:02 -0800 | [diff] [blame] | 1 | /* |
| 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 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, |
| 46 | PCM_ID, |
| 47 | BACKEND_NAME, |
| 48 | } section_t; |
| 49 | |
| 50 | typedef void (* section_process_fn)(const XML_Char **attr); |
| 51 | |
| 52 | static void process_acdb_id(const XML_Char **attr); |
| 53 | static void process_pcm_id(const XML_Char **attr); |
| 54 | static void process_backend_name(const XML_Char **attr); |
| 55 | static void process_root(const XML_Char **attr); |
| 56 | |
| 57 | static 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 | |
| 64 | static 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 | |
| 86 | static void process_root(const XML_Char **attr __unused) |
| 87 | { |
| 88 | } |
| 89 | |
| 90 | /* mapping from usecase to pcm dev id */ |
| 91 | static 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 | |
| 136 | done: |
| 137 | return; |
| 138 | } |
| 139 | |
| 140 | /* backend to be used for a device */ |
| 141 | static 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 | |
| 169 | done: |
| 170 | return; |
| 171 | } |
| 172 | |
| 173 | static void process_acdb_id(const XML_Char **attr) |
Ben Romberger | 5588688 | 2014-01-10 13:49:02 -0800 | [diff] [blame] | 174 | { |
Ben Romberger | 61764e3 | 2014-01-10 13:49:02 -0800 | [diff] [blame] | 175 | int index; |
Ben Romberger | 5588688 | 2014-01-10 13:49:02 -0800 | [diff] [blame] | 176 | |
Ben Romberger | 61764e3 | 2014-01-10 13:49:02 -0800 | [diff] [blame] | 177 | if (strcmp(attr[0], "name") != 0) { |
| 178 | ALOGE("%s: 'name' not found, no ACDB ID set!", __func__); |
Ben Romberger | 5588688 | 2014-01-10 13:49:02 -0800 | [diff] [blame] | 179 | goto done; |
Ben Romberger | 61764e3 | 2014-01-10 13:49:02 -0800 | [diff] [blame] | 180 | } |
Ben Romberger | 5588688 | 2014-01-10 13:49:02 -0800 | [diff] [blame] | 181 | |
Ben Romberger | 61764e3 | 2014-01-10 13:49:02 -0800 | [diff] [blame] | 182 | index = platform_get_snd_device_index((char *)attr[1]); |
| 183 | if (index < 0) { |
Helen Zeng | 6a16ad7 | 2014-02-23 22:04:44 -0800 | [diff] [blame] | 184 | ALOGE("%s: Device %s in platform info xml not found, no ACDB ID set!", |
| 185 | __func__, attr[1]); |
Ben Romberger | 5588688 | 2014-01-10 13:49:02 -0800 | [diff] [blame] | 186 | goto done; |
| 187 | } |
| 188 | |
| 189 | if (strcmp(attr[2], "acdb_id") != 0) { |
Helen Zeng | 6a16ad7 | 2014-02-23 22:04:44 -0800 | [diff] [blame] | 190 | ALOGE("%s: Device %s in platform info xml has no acdb_id, no ACDB ID set!", |
| 191 | __func__, attr[1]); |
Ben Romberger | 5588688 | 2014-01-10 13:49:02 -0800 | [diff] [blame] | 192 | goto done; |
| 193 | } |
| 194 | |
Haynes Mathew George | f4da6fe | 2014-06-20 19:14:25 -0700 | [diff] [blame] | 195 | 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 Zeng | 6a16ad7 | 2014-02-23 22:04:44 -0800 | [diff] [blame] | 197 | __func__, attr[1], atoi((char *)attr[3])); |
Ben Romberger | 5588688 | 2014-01-10 13:49:02 -0800 | [diff] [blame] | 198 | goto done; |
Ben Romberger | 61764e3 | 2014-01-10 13:49:02 -0800 | [diff] [blame] | 199 | } |
Ben Romberger | 5588688 | 2014-01-10 13:49:02 -0800 | [diff] [blame] | 200 | |
Ben Romberger | 5588688 | 2014-01-10 13:49:02 -0800 | [diff] [blame] | 201 | done: |
| 202 | return; |
| 203 | } |
| 204 | |
Haynes Mathew George | f4da6fe | 2014-06-20 19:14:25 -0700 | [diff] [blame] | 205 | static void start_tag(void *userdata __unused, const XML_Char *tag_name, |
Ben Romberger | 5588688 | 2014-01-10 13:49:02 -0800 | [diff] [blame] | 206 | 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 George | f4da6fe | 2014-06-20 19:14:25 -0700 | [diff] [blame] | 212 | 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 Romberger | 5588688 | 2014-01-10 13:49:02 -0800 | [diff] [blame] | 236 | |
| 237 | return; |
| 238 | } |
| 239 | |
Haynes Mathew George | f4da6fe | 2014-06-20 19:14:25 -0700 | [diff] [blame] | 240 | static void end_tag(void *userdata __unused, const XML_Char *tag_name) |
Ben Romberger | 5588688 | 2014-01-10 13:49:02 -0800 | [diff] [blame] | 241 | { |
Haynes Mathew George | f4da6fe | 2014-06-20 19:14:25 -0700 | [diff] [blame] | 242 | 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 Romberger | 5588688 | 2014-01-10 13:49:02 -0800 | [diff] [blame] | 249 | } |
| 250 | |
Helen Zeng | 6a16ad7 | 2014-02-23 22:04:44 -0800 | [diff] [blame] | 251 | int platform_info_init(const char *filename) |
Ben Romberger | 5588688 | 2014-01-10 13:49:02 -0800 | [diff] [blame] | 252 | { |
| 253 | XML_Parser parser; |
| 254 | FILE *file; |
| 255 | int ret = 0; |
| 256 | int bytes_read; |
Ben Romberger | 5588688 | 2014-01-10 13:49:02 -0800 | [diff] [blame] | 257 | void *buf; |
| 258 | |
Helen Zeng | 6a16ad7 | 2014-02-23 22:04:44 -0800 | [diff] [blame] | 259 | file = fopen(filename, "r"); |
Haynes Mathew George | f4da6fe | 2014-06-20 19:14:25 -0700 | [diff] [blame] | 260 | section = ROOT; |
| 261 | |
Ben Romberger | 5588688 | 2014-01-10 13:49:02 -0800 | [diff] [blame] | 262 | if (!file) { |
| 263 | ALOGD("%s: Failed to open %s, using defaults.", |
Helen Zeng | 6a16ad7 | 2014-02-23 22:04:44 -0800 | [diff] [blame] | 264 | __func__, filename); |
Ben Romberger | 5588688 | 2014-01-10 13:49:02 -0800 | [diff] [blame] | 265 | 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 Romberger | 5588688 | 2014-01-10 13:49:02 -0800 | [diff] [blame] | 276 | 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 Zeng | 6a16ad7 | 2014-02-23 22:04:44 -0800 | [diff] [blame] | 296 | __func__, filename); |
Ben Romberger | 5588688 | 2014-01-10 13:49:02 -0800 | [diff] [blame] | 297 | ret = -EINVAL; |
| 298 | goto err_free_parser; |
| 299 | } |
| 300 | |
| 301 | if (bytes_read == 0) |
| 302 | break; |
| 303 | } |
| 304 | |
Ben Romberger | 5588688 | 2014-01-10 13:49:02 -0800 | [diff] [blame] | 305 | err_free_parser: |
| 306 | XML_ParserFree(parser); |
| 307 | err_close_file: |
| 308 | fclose(file); |
| 309 | done: |
| 310 | return ret; |
| 311 | } |