Haynes Mathew George | 5bc1884 | 2014-06-16 16:36:20 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2014 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #define LOG_TAG "platform_info" |
| 18 | #define LOG_NDDEBUG 0 |
| 19 | |
| 20 | #include <errno.h> |
| 21 | #include <stdio.h> |
| 22 | #include <expat.h> |
| 23 | #include <cutils/log.h> |
| 24 | #include <audio_hw.h> |
| 25 | #include "platform_api.h" |
| 26 | #include <platform.h> |
| 27 | |
| 28 | #define PLATFORM_INFO_XML_PATH "/system/etc/audio_platform_info.xml" |
| 29 | |
| 30 | static void process_device(const XML_Char **attr) |
| 31 | { |
| 32 | int index; |
| 33 | |
| 34 | if (strcmp(attr[0], "name") != 0) { |
| 35 | ALOGE("%s: 'name' not found, no ACDB ID set!", __func__); |
| 36 | goto done; |
| 37 | } |
| 38 | |
| 39 | index = platform_get_snd_device_index((char *)attr[1]); |
| 40 | if (index < 0) { |
| 41 | ALOGE("%s: Device %s in %s not found, no ACDB ID set!", |
| 42 | __func__, attr[1], PLATFORM_INFO_XML_PATH); |
| 43 | goto done; |
| 44 | } |
| 45 | |
| 46 | if (strcmp(attr[2], "acdb_id") != 0) { |
| 47 | ALOGE("%s: Device %s in %s has no acdb_id, no ACDB ID set!", |
| 48 | __func__, attr[1], PLATFORM_INFO_XML_PATH); |
| 49 | goto done; |
| 50 | } |
| 51 | |
| 52 | if(platform_set_snd_device_acdb_id(index, atoi((char *)attr[3])) < 0) { |
| 53 | ALOGE("%s: Device %s in %s, ACDB ID %d was not set!", |
| 54 | __func__, attr[1], PLATFORM_INFO_XML_PATH, atoi((char *)attr[3])); |
| 55 | goto done; |
| 56 | } |
| 57 | |
| 58 | done: |
| 59 | return; |
| 60 | } |
| 61 | |
| 62 | static void start_tag(void *userdata __unused, const XML_Char *tag_name, |
| 63 | const XML_Char **attr) |
| 64 | { |
| 65 | const XML_Char *attr_name = NULL; |
| 66 | const XML_Char *attr_value = NULL; |
| 67 | unsigned int i; |
| 68 | |
| 69 | if (strcmp(tag_name, "device") == 0) |
| 70 | process_device(attr); |
| 71 | |
| 72 | return; |
| 73 | } |
| 74 | |
| 75 | static void end_tag(void *userdata __unused, const XML_Char *tag_name __unused) |
| 76 | { |
| 77 | |
| 78 | } |
| 79 | |
| 80 | int platform_info_init(void) |
| 81 | { |
| 82 | XML_Parser parser; |
| 83 | FILE *file; |
| 84 | int ret = 0; |
| 85 | int bytes_read; |
| 86 | void *buf; |
| 87 | static const uint32_t kBufSize = 1024; |
| 88 | |
| 89 | file = fopen(PLATFORM_INFO_XML_PATH, "r"); |
| 90 | if (!file) { |
| 91 | ALOGD("%s: Failed to open %s, using defaults.", |
| 92 | __func__, PLATFORM_INFO_XML_PATH); |
| 93 | ret = -ENODEV; |
| 94 | goto done; |
| 95 | } |
| 96 | |
| 97 | parser = XML_ParserCreate(NULL); |
| 98 | if (!parser) { |
| 99 | ALOGE("%s: Failed to create XML parser!", __func__); |
| 100 | ret = -ENODEV; |
| 101 | goto err_close_file; |
| 102 | } |
| 103 | |
| 104 | XML_SetElementHandler(parser, start_tag, end_tag); |
| 105 | |
| 106 | while (1) { |
| 107 | buf = XML_GetBuffer(parser, kBufSize); |
| 108 | if (buf == NULL) { |
| 109 | ALOGE("%s: XML_GetBuffer failed", __func__); |
| 110 | ret = -ENOMEM; |
| 111 | goto err_free_parser; |
| 112 | } |
| 113 | |
| 114 | bytes_read = fread(buf, 1, kBufSize, file); |
| 115 | if (bytes_read < 0) { |
| 116 | ALOGE("%s: fread failed, bytes read = %d", __func__, bytes_read); |
| 117 | ret = bytes_read; |
| 118 | goto err_free_parser; |
| 119 | } |
| 120 | |
| 121 | if (XML_ParseBuffer(parser, bytes_read, |
| 122 | bytes_read == 0) == XML_STATUS_ERROR) { |
| 123 | ALOGE("%s: XML_ParseBuffer failed, for %s", |
| 124 | __func__, PLATFORM_INFO_XML_PATH); |
| 125 | ret = -EINVAL; |
| 126 | goto err_free_parser; |
| 127 | } |
| 128 | |
| 129 | if (bytes_read == 0) |
| 130 | break; |
| 131 | } |
| 132 | |
| 133 | err_free_parser: |
| 134 | XML_ParserFree(parser); |
| 135 | err_close_file: |
| 136 | fclose(file); |
| 137 | done: |
| 138 | return ret; |
| 139 | } |