blob: c89020d8cc92dd774e66b7cabb979a16a183bc67 [file] [log] [blame]
Haynes Mathew George5bc18842014-06-16 16:36:20 -07001/*
Haynes Mathew George569b7482017-05-08 14:44:27 -07002 * Copyright (C) 2014 The Android Open Source Project
Haynes Mathew George5bc18842014-06-16 16:36:20 -07003 *
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>
Haynes Mathew Georgee6e2d442018-02-22 18:51:56 -080023#include <log/log.h>
Haynes Mathew George5bc18842014-06-16 16:36:20 -070024#include <audio_hw.h>
25#include "platform_api.h"
26#include <platform.h>
vivek mehtaa8d7c922016-05-25 14:40:44 -070027#include <math.h>
Haynes Mathew George5bc18842014-06-16 16:36:20 -070028
jiabin749942e2018-05-25 12:37:29 -070029/*
30 * Mandatory microphone characteristics include: device_id, type, address, location, group,
31 * index_in_the_group, directionality, num_frequency_responses, frequencies and responses.
32 * MANDATORY_MICROPHONE_CHARACTERISTICS should be updated when mandatory microphone
33 * characteristics are changed.
34 */
35#define MANDATORY_MICROPHONE_CHARACTERISTICS (1 << 10) - 1
36
Haynes Mathew George98c95622014-06-20 19:14:25 -070037typedef enum {
38 ROOT,
39 ACDB,
40 PCM_ID,
41 BACKEND_NAME,
Ravi Kumar Alamandac4f57312015-06-26 17:41:02 -070042 CONFIG_PARAMS,
keunhui.park2f7306a2015-07-16 16:48:06 +090043 OPERATOR_SPECIFIC,
vivek mehtaa8d7c922016-05-25 14:40:44 -070044 GAIN_LEVEL_MAPPING,
Haynes Mathew Georgee5ff0fc2017-02-16 20:33:38 -080045 APP_TYPE,
jiabin8962a4d2018-03-19 18:21:24 -070046 MICROPHONE_CHARACTERISTIC,
Aniket Kumar Latadebab2b2018-04-30 20:20:25 -070047 SND_DEVICES,
48 INPUT_SND_DEVICE,
49 INPUT_SND_DEVICE_TO_MIC_MAPPING,
50 SND_DEV,
51 MIC_INFO,
Ashish Jain1c849702018-05-11 20:11:55 +053052 ACDB_METAINFO_KEY,
Haynes Mathew George98c95622014-06-20 19:14:25 -070053} section_t;
54
55typedef void (* section_process_fn)(const XML_Char **attr);
56
57static void process_acdb_id(const XML_Char **attr);
58static void process_pcm_id(const XML_Char **attr);
59static void process_backend_name(const XML_Char **attr);
Ravi Kumar Alamandac4f57312015-06-26 17:41:02 -070060static void process_config_params(const XML_Char **attr);
Haynes Mathew George98c95622014-06-20 19:14:25 -070061static void process_root(const XML_Char **attr);
keunhui.park2f7306a2015-07-16 16:48:06 +090062static void process_operator_specific(const XML_Char **attr);
vivek mehtaa8d7c922016-05-25 14:40:44 -070063static void process_gain_db_to_level_map(const XML_Char **attr);
Haynes Mathew Georgee5ff0fc2017-02-16 20:33:38 -080064static void process_app_type(const XML_Char **attr);
jiabin8962a4d2018-03-19 18:21:24 -070065static void process_microphone_characteristic(const XML_Char **attr);
Aniket Kumar Latadebab2b2018-04-30 20:20:25 -070066static void process_snd_dev(const XML_Char **attr);
67static void process_mic_info(const XML_Char **attr);
Ashish Jain1c849702018-05-11 20:11:55 +053068static void process_acdb_metainfo_key(const XML_Char **attr);
Haynes Mathew George98c95622014-06-20 19:14:25 -070069
70static section_process_fn section_table[] = {
71 [ROOT] = process_root,
72 [ACDB] = process_acdb_id,
73 [PCM_ID] = process_pcm_id,
74 [BACKEND_NAME] = process_backend_name,
Ravi Kumar Alamandac4f57312015-06-26 17:41:02 -070075 [CONFIG_PARAMS] = process_config_params,
keunhui.park2f7306a2015-07-16 16:48:06 +090076 [OPERATOR_SPECIFIC] = process_operator_specific,
vivek mehtaa8d7c922016-05-25 14:40:44 -070077 [GAIN_LEVEL_MAPPING] = process_gain_db_to_level_map,
Haynes Mathew Georgee5ff0fc2017-02-16 20:33:38 -080078 [APP_TYPE] = process_app_type,
jiabin8962a4d2018-03-19 18:21:24 -070079 [MICROPHONE_CHARACTERISTIC] = process_microphone_characteristic,
Aniket Kumar Latadebab2b2018-04-30 20:20:25 -070080 [SND_DEV] = process_snd_dev,
81 [MIC_INFO] = process_mic_info,
Ashish Jain1c849702018-05-11 20:11:55 +053082 [ACDB_METAINFO_KEY] = process_acdb_metainfo_key,
Haynes Mathew George98c95622014-06-20 19:14:25 -070083};
84
vivek mehta0fb11312017-05-15 19:35:32 -070085static set_parameters_fn set_parameters = &platform_set_parameters;
86
Haynes Mathew George98c95622014-06-20 19:14:25 -070087static section_t section;
88
Ravi Kumar Alamandac4f57312015-06-26 17:41:02 -070089struct platform_info {
vivek mehta0fb11312017-05-15 19:35:32 -070090 bool do_full_parse;
Ravi Kumar Alamandac4f57312015-06-26 17:41:02 -070091 void *platform;
92 struct str_parms *kvpairs;
93};
94
vivek mehta0fb11312017-05-15 19:35:32 -070095static struct platform_info my_data = {true, NULL, NULL};
Ravi Kumar Alamandac4f57312015-06-26 17:41:02 -070096
jiabin8962a4d2018-03-19 18:21:24 -070097struct audio_string_to_enum {
98 const char* name;
99 unsigned int value;
100};
101
Aniket Kumar Latadebab2b2018-04-30 20:20:25 -0700102static snd_device_t in_snd_device;
103
jiabin8962a4d2018-03-19 18:21:24 -0700104static const struct audio_string_to_enum mic_locations[AUDIO_MICROPHONE_LOCATION_CNT] = {
105 AUDIO_MAKE_STRING_FROM_ENUM(AUDIO_MICROPHONE_LOCATION_UNKNOWN),
106 AUDIO_MAKE_STRING_FROM_ENUM(AUDIO_MICROPHONE_LOCATION_MAINBODY),
107 AUDIO_MAKE_STRING_FROM_ENUM(AUDIO_MICROPHONE_LOCATION_MAINBODY_MOVABLE),
108 AUDIO_MAKE_STRING_FROM_ENUM(AUDIO_MICROPHONE_LOCATION_PERIPHERAL),
109};
110
111static const struct audio_string_to_enum mic_directionalities[AUDIO_MICROPHONE_DIRECTIONALITY_CNT] = {
112 AUDIO_MAKE_STRING_FROM_ENUM(AUDIO_MICROPHONE_DIRECTIONALITY_OMNI),
113 AUDIO_MAKE_STRING_FROM_ENUM(AUDIO_MICROPHONE_DIRECTIONALITY_BI_DIRECTIONAL),
114 AUDIO_MAKE_STRING_FROM_ENUM(AUDIO_MICROPHONE_DIRECTIONALITY_UNKNOWN),
115 AUDIO_MAKE_STRING_FROM_ENUM(AUDIO_MICROPHONE_DIRECTIONALITY_CARDIOID),
116 AUDIO_MAKE_STRING_FROM_ENUM(AUDIO_MICROPHONE_DIRECTIONALITY_HYPER_CARDIOID),
117 AUDIO_MAKE_STRING_FROM_ENUM(AUDIO_MICROPHONE_DIRECTIONALITY_SUPER_CARDIOID),
118};
119
Aniket Kumar Latadebab2b2018-04-30 20:20:25 -0700120static const struct audio_string_to_enum mic_channel_mapping[AUDIO_MICROPHONE_CHANNEL_MAPPING_CNT] = {
121 AUDIO_MAKE_STRING_FROM_ENUM(AUDIO_MICROPHONE_CHANNEL_MAPPING_UNUSED),
122 AUDIO_MAKE_STRING_FROM_ENUM(AUDIO_MICROPHONE_CHANNEL_MAPPING_DIRECT),
123 AUDIO_MAKE_STRING_FROM_ENUM(AUDIO_MICROPHONE_CHANNEL_MAPPING_PROCESSED),
124};
125
jiabin8962a4d2018-03-19 18:21:24 -0700126static const struct audio_string_to_enum device_in_types[] = {
127 AUDIO_MAKE_STRING_FROM_ENUM(AUDIO_DEVICE_IN_AMBIENT),
128 AUDIO_MAKE_STRING_FROM_ENUM(AUDIO_DEVICE_IN_COMMUNICATION),
129 AUDIO_MAKE_STRING_FROM_ENUM(AUDIO_DEVICE_IN_BUILTIN_MIC),
130 AUDIO_MAKE_STRING_FROM_ENUM(AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET),
131 AUDIO_MAKE_STRING_FROM_ENUM(AUDIO_DEVICE_IN_WIRED_HEADSET),
132 AUDIO_MAKE_STRING_FROM_ENUM(AUDIO_DEVICE_IN_AUX_DIGITAL),
133 AUDIO_MAKE_STRING_FROM_ENUM(AUDIO_DEVICE_IN_HDMI),
134 AUDIO_MAKE_STRING_FROM_ENUM(AUDIO_DEVICE_IN_VOICE_CALL),
135 AUDIO_MAKE_STRING_FROM_ENUM(AUDIO_DEVICE_IN_TELEPHONY_RX),
136 AUDIO_MAKE_STRING_FROM_ENUM(AUDIO_DEVICE_IN_BACK_MIC),
137 AUDIO_MAKE_STRING_FROM_ENUM(AUDIO_DEVICE_IN_REMOTE_SUBMIX),
138 AUDIO_MAKE_STRING_FROM_ENUM(AUDIO_DEVICE_IN_ANLG_DOCK_HEADSET),
139 AUDIO_MAKE_STRING_FROM_ENUM(AUDIO_DEVICE_IN_DGTL_DOCK_HEADSET),
140 AUDIO_MAKE_STRING_FROM_ENUM(AUDIO_DEVICE_IN_USB_ACCESSORY),
141 AUDIO_MAKE_STRING_FROM_ENUM(AUDIO_DEVICE_IN_USB_DEVICE),
142 AUDIO_MAKE_STRING_FROM_ENUM(AUDIO_DEVICE_IN_FM_TUNER),
143 AUDIO_MAKE_STRING_FROM_ENUM(AUDIO_DEVICE_IN_TV_TUNER),
144 AUDIO_MAKE_STRING_FROM_ENUM(AUDIO_DEVICE_IN_LINE),
145 AUDIO_MAKE_STRING_FROM_ENUM(AUDIO_DEVICE_IN_SPDIF),
146 AUDIO_MAKE_STRING_FROM_ENUM(AUDIO_DEVICE_IN_BLUETOOTH_A2DP),
147 AUDIO_MAKE_STRING_FROM_ENUM(AUDIO_DEVICE_IN_LOOPBACK),
148 AUDIO_MAKE_STRING_FROM_ENUM(AUDIO_DEVICE_IN_IP),
149 AUDIO_MAKE_STRING_FROM_ENUM(AUDIO_DEVICE_IN_BUS),
150 AUDIO_MAKE_STRING_FROM_ENUM(AUDIO_DEVICE_IN_PROXY),
151 AUDIO_MAKE_STRING_FROM_ENUM(AUDIO_DEVICE_IN_USB_HEADSET),
152 AUDIO_MAKE_STRING_FROM_ENUM(AUDIO_DEVICE_IN_BLUETOOTH_BLE),
153 AUDIO_MAKE_STRING_FROM_ENUM(AUDIO_DEVICE_IN_DEFAULT),
154};
155
156static bool find_enum_by_string(const struct audio_string_to_enum * table, const char * name,
157 int32_t len, unsigned int *value)
158{
159 if (table == NULL) {
160 ALOGE("%s: table is NULL", __func__);
161 return false;
162 }
163
164 if (name == NULL) {
165 ALOGE("null key");
166 return false;
167 }
168
169 for (int i = 0; i < len; i++) {
170 if (!strcmp(table[i].name, name)) {
171 *value = table[i].value;
172 return true;
173 }
174 }
175 return false;
176}
177
Haynes Mathew George98c95622014-06-20 19:14:25 -0700178/*
179 * <audio_platform_info>
180 * <acdb_ids>
181 * <device name="???" acdb_id="???"/>
182 * ...
183 * ...
184 * </acdb_ids>
185 * <backend_names>
186 * <device name="???" backend="???"/>
187 * ...
188 * ...
189 * </backend_names>
190 * <pcm_ids>
191 * <usecase name="???" type="in/out" id="???"/>
192 * ...
193 * ...
194 * </pcm_ids>
Ravi Kumar Alamandac4f57312015-06-26 17:41:02 -0700195 * <config_params>
196 * <param key="snd_card_name" value="msm8994-tomtom-mtp-snd-card"/>
keunhui.park2f7306a2015-07-16 16:48:06 +0900197 * <param key="operator_info" value="tmus;aa;bb;cc"/>
198 * <param key="operator_info" value="sprint;xx;yy;zz"/>
Ravi Kumar Alamandac4f57312015-06-26 17:41:02 -0700199 * ...
200 * ...
201 * </config_params>
202 *
keunhui.park2f7306a2015-07-16 16:48:06 +0900203 * <operator_specific>
204 * <device name="???" operator="???" mixer_path="???" acdb_id="???"/>
205 * ...
206 * ...
207 * </operator_specific>
208 *
Haynes Mathew George98c95622014-06-20 19:14:25 -0700209 * </audio_platform_info>
210 */
211
212static void process_root(const XML_Char **attr __unused)
213{
214}
215
216/* mapping from usecase to pcm dev id */
217static void process_pcm_id(const XML_Char **attr)
218{
219 int index;
220
221 if (strcmp(attr[0], "name") != 0) {
Ravi Kumar Alamandac4f57312015-06-26 17:41:02 -0700222 ALOGE("%s: 'name' not found, no pcm_id set!", __func__);
Haynes Mathew George98c95622014-06-20 19:14:25 -0700223 goto done;
224 }
225
226 index = platform_get_usecase_index((char *)attr[1]);
227 if (index < 0) {
228 ALOGE("%s: usecase %s in %s not found!",
229 __func__, attr[1], PLATFORM_INFO_XML_PATH);
230 goto done;
231 }
232
233 if (strcmp(attr[2], "type") != 0) {
234 ALOGE("%s: usecase type not mentioned", __func__);
235 goto done;
236 }
237
238 int type = -1;
239
240 if (!strcasecmp((char *)attr[3], "in")) {
241 type = 1;
242 } else if (!strcasecmp((char *)attr[3], "out")) {
243 type = 0;
244 } else {
245 ALOGE("%s: type must be IN or OUT", __func__);
246 goto done;
247 }
248
249 if (strcmp(attr[4], "id") != 0) {
250 ALOGE("%s: usecase id not mentioned", __func__);
251 goto done;
252 }
253
254 int id = atoi((char *)attr[5]);
255
256 if (platform_set_usecase_pcm_id(index, type, id) < 0) {
257 ALOGE("%s: usecase %s in %s, type %d id %d was not set!",
258 __func__, attr[1], PLATFORM_INFO_XML_PATH, type, id);
259 goto done;
260 }
261
262done:
263 return;
264}
265
266/* backend to be used for a device */
267static void process_backend_name(const XML_Char **attr)
268{
269 int index;
Ravi Kumar Alamandab7ea4f52015-06-08 16:44:05 -0700270 char *hw_interface = NULL;
Haynes Mathew George98c95622014-06-20 19:14:25 -0700271
272 if (strcmp(attr[0], "name") != 0) {
273 ALOGE("%s: 'name' not found, no ACDB ID set!", __func__);
274 goto done;
275 }
276
277 index = platform_get_snd_device_index((char *)attr[1]);
278 if (index < 0) {
279 ALOGE("%s: Device %s in %s not found, no ACDB ID set!",
280 __func__, attr[1], PLATFORM_INFO_XML_PATH);
281 goto done;
282 }
283
284 if (strcmp(attr[2], "backend") != 0) {
285 ALOGE("%s: Device %s in %s has no backed set!",
286 __func__, attr[1], PLATFORM_INFO_XML_PATH);
287 goto done;
288 }
289
Ravi Kumar Alamandab7ea4f52015-06-08 16:44:05 -0700290 if (attr[4] != NULL) {
291 if (strcmp(attr[4], "interface") != 0) {
292 hw_interface = NULL;
293 } else {
294 hw_interface = (char *)attr[5];
295 }
296 }
297
298 if (platform_set_snd_device_backend(index, attr[3], hw_interface) < 0) {
Haynes Mathew George98c95622014-06-20 19:14:25 -0700299 ALOGE("%s: Device %s in %s, backend %s was not set!",
300 __func__, attr[1], PLATFORM_INFO_XML_PATH, attr[3]);
301 goto done;
302 }
303
304done:
305 return;
306}
307
vivek mehtaa8d7c922016-05-25 14:40:44 -0700308static void process_gain_db_to_level_map(const XML_Char **attr)
309{
310 struct amp_db_and_gain_table tbl_entry;
311
312 if ((strcmp(attr[0], "db") != 0) ||
313 (strcmp(attr[2], "level") != 0)) {
314 ALOGE("%s: invalid attribute passed %s %sexpected amp db level",
315 __func__, attr[0], attr[2]);
316 goto done;
317 }
318
319 tbl_entry.db = atof(attr[1]);
320 tbl_entry.amp = exp(tbl_entry.db * 0.115129f);
321 tbl_entry.level = atoi(attr[3]);
322
vivek mehta40125092017-08-21 18:48:51 -0700323 //custome level should be > 0. Level 0 is fixed for default
324 CHECK(tbl_entry.level > 0);
325
vivek mehtaa8d7c922016-05-25 14:40:44 -0700326 ALOGV("%s: amp [%f] db [%f] level [%d]", __func__,
327 tbl_entry.amp, tbl_entry.db, tbl_entry.level);
328 platform_add_gain_level_mapping(&tbl_entry);
329
330done:
331 return;
332}
333
Haynes Mathew George98c95622014-06-20 19:14:25 -0700334static void process_acdb_id(const XML_Char **attr)
Haynes Mathew George5bc18842014-06-16 16:36:20 -0700335{
336 int index;
337
338 if (strcmp(attr[0], "name") != 0) {
339 ALOGE("%s: 'name' not found, no ACDB ID set!", __func__);
340 goto done;
341 }
342
343 index = platform_get_snd_device_index((char *)attr[1]);
344 if (index < 0) {
345 ALOGE("%s: Device %s in %s not found, no ACDB ID set!",
346 __func__, attr[1], PLATFORM_INFO_XML_PATH);
347 goto done;
348 }
349
350 if (strcmp(attr[2], "acdb_id") != 0) {
351 ALOGE("%s: Device %s in %s has no acdb_id, no ACDB ID set!",
352 __func__, attr[1], PLATFORM_INFO_XML_PATH);
353 goto done;
354 }
355
Haynes Mathew George98c95622014-06-20 19:14:25 -0700356 if (platform_set_snd_device_acdb_id(index, atoi((char *)attr[3])) < 0) {
Haynes Mathew George5bc18842014-06-16 16:36:20 -0700357 ALOGE("%s: Device %s in %s, ACDB ID %d was not set!",
358 __func__, attr[1], PLATFORM_INFO_XML_PATH, atoi((char *)attr[3]));
359 goto done;
360 }
361
362done:
363 return;
364}
365
keunhui.park2f7306a2015-07-16 16:48:06 +0900366
367static void process_operator_specific(const XML_Char **attr)
368{
369 snd_device_t snd_device = SND_DEVICE_NONE;
370
371 if (strcmp(attr[0], "name") != 0) {
372 ALOGE("%s: 'name' not found", __func__);
373 goto done;
374 }
375
376 snd_device = platform_get_snd_device_index((char *)attr[1]);
377 if (snd_device < 0) {
378 ALOGE("%s: Device %s in %s not found, no ACDB ID set!",
379 __func__, (char *)attr[3], PLATFORM_INFO_XML_PATH);
380 goto done;
381 }
382
383 if (strcmp(attr[2], "operator") != 0) {
384 ALOGE("%s: 'operator' not found", __func__);
385 goto done;
386 }
387
388 if (strcmp(attr[4], "mixer_path") != 0) {
389 ALOGE("%s: 'mixer_path' not found", __func__);
390 goto done;
391 }
392
393 if (strcmp(attr[6], "acdb_id") != 0) {
394 ALOGE("%s: 'acdb_id' not found", __func__);
395 goto done;
396 }
397
398 platform_add_operator_specific_device(snd_device, (char *)attr[3], (char *)attr[5], atoi((char *)attr[7]));
399
400done:
401 return;
402}
403
Ravi Kumar Alamandac4f57312015-06-26 17:41:02 -0700404/* platform specific configuration key-value pairs */
405static void process_config_params(const XML_Char **attr)
406{
407 if (strcmp(attr[0], "key") != 0) {
408 ALOGE("%s: 'key' not found", __func__);
409 goto done;
410 }
411
412 if (strcmp(attr[2], "value") != 0) {
413 ALOGE("%s: 'value' not found", __func__);
414 goto done;
415 }
416
417 str_parms_add_str(my_data.kvpairs, (char*)attr[1], (char*)attr[3]);
vivek mehta0fb11312017-05-15 19:35:32 -0700418 set_parameters(my_data.platform, my_data.kvpairs);
Ravi Kumar Alamandac4f57312015-06-26 17:41:02 -0700419done:
420 return;
421}
422
Haynes Mathew Georgee5ff0fc2017-02-16 20:33:38 -0800423static void process_app_type(const XML_Char **attr)
424{
425 if (strcmp(attr[0], "uc_type")) {
426 ALOGE("%s: uc_type not found", __func__);
427 goto done;
428 }
429
vivek mehtaa68fea62017-06-08 19:04:02 -0700430 if (strcmp(attr[2], "mode")) {
431 ALOGE("%s: mode not found", __func__);
432 goto done;
433 }
434
435 if (strcmp(attr[4], "bit_width")) {
Haynes Mathew Georgee5ff0fc2017-02-16 20:33:38 -0800436 ALOGE("%s: bit_width not found", __func__);
437 goto done;
438 }
439
vivek mehtaa68fea62017-06-08 19:04:02 -0700440 if (strcmp(attr[6], "id")) {
Haynes Mathew Georgee5ff0fc2017-02-16 20:33:38 -0800441 ALOGE("%s: id not found", __func__);
442 goto done;
443 }
444
vivek mehtaa68fea62017-06-08 19:04:02 -0700445 if (strcmp(attr[8], "max_rate")) {
Haynes Mathew Georgee5ff0fc2017-02-16 20:33:38 -0800446 ALOGE("%s: max rate not found", __func__);
447 goto done;
448 }
449
vivek mehtaa68fea62017-06-08 19:04:02 -0700450 platform_add_app_type(attr[1], attr[3], atoi(attr[5]), atoi(attr[7]),
451 atoi(attr[9]));
Haynes Mathew Georgee5ff0fc2017-02-16 20:33:38 -0800452done:
453 return;
454}
455
jiabin8962a4d2018-03-19 18:21:24 -0700456static void process_microphone_characteristic(const XML_Char **attr) {
457 struct audio_microphone_characteristic_t microphone;
jiabin749942e2018-05-25 12:37:29 -0700458 uint32_t index = 0;
459 uint32_t found_mandatory_characteristics = 0;
460 uint32_t num_frequencies = 0;
461 uint32_t num_responses = 0;
462 microphone.sensitivity = AUDIO_MICROPHONE_SENSITIVITY_UNKNOWN;
463 microphone.max_spl = AUDIO_MICROPHONE_SPL_UNKNOWN;
464 microphone.min_spl = AUDIO_MICROPHONE_SPL_UNKNOWN;
465 microphone.orientation.x = 0.0f;
466 microphone.orientation.y = 0.0f;
467 microphone.orientation.z = 0.0f;
468 microphone.geometric_location.x = AUDIO_MICROPHONE_COORDINATE_UNKNOWN;
469 microphone.geometric_location.y = AUDIO_MICROPHONE_COORDINATE_UNKNOWN;
470 microphone.geometric_location.z = AUDIO_MICROPHONE_COORDINATE_UNKNOWN;
jiabin8962a4d2018-03-19 18:21:24 -0700471
jiabin749942e2018-05-25 12:37:29 -0700472 while (attr[index] != NULL) {
473 const char *attribute = attr[index++];
474 char value[strlen(attr[index]) + 1];
475 strcpy(value, attr[index++]);
476 if (strcmp(attribute, "device_id") == 0) {
477 if (strlen(value) > AUDIO_MICROPHONE_ID_MAX_LEN) {
478 ALOGE("%s: device_id %s is too long", __func__, value);
jiabin8962a4d2018-03-19 18:21:24 -0700479 goto done;
480 }
jiabin749942e2018-05-25 12:37:29 -0700481 strcpy(microphone.device_id, value);
482 found_mandatory_characteristics |= 1;
483 } else if (strcmp(attribute, "type") == 0) {
484 if (!find_enum_by_string(device_in_types, value,
485 ARRAY_SIZE(device_in_types), &microphone.device)) {
486 ALOGE("%s: type %s in %s not found!",
487 __func__, value, PLATFORM_INFO_XML_PATH);
jiabin8962a4d2018-03-19 18:21:24 -0700488 goto done;
489 }
jiabin749942e2018-05-25 12:37:29 -0700490 found_mandatory_characteristics |= (1 << 1);
491 } else if (strcmp(attribute, "address") == 0) {
492 if (strlen(value) > AUDIO_DEVICE_MAX_ADDRESS_LEN) {
493 ALOGE("%s, address %s is too long", __func__, value);
494 goto done;
495 }
496 strcpy(microphone.address, value);
497 if (strlen(microphone.address) == 0) {
498 // If the address is empty, populate the address according to device type.
499 if (microphone.device == AUDIO_DEVICE_IN_BUILTIN_MIC) {
500 strcpy(microphone.address, AUDIO_BOTTOM_MICROPHONE_ADDRESS);
501 } else if (microphone.device == AUDIO_DEVICE_IN_BACK_MIC) {
502 strcpy(microphone.address, AUDIO_BACK_MICROPHONE_ADDRESS);
503 }
504 }
505 found_mandatory_characteristics |= (1 << 2);
506 } else if (strcmp(attribute, "location") == 0) {
507 if (!find_enum_by_string(mic_locations, value,
508 AUDIO_MICROPHONE_LOCATION_CNT, &microphone.location)) {
509 ALOGE("%s: location %s in %s not found!",
510 __func__, value, PLATFORM_INFO_XML_PATH);
511 goto done;
512 }
513 found_mandatory_characteristics |= (1 << 3);
514 } else if (strcmp(attribute, "group") == 0) {
515 microphone.group = atoi(value);
516 found_mandatory_characteristics |= (1 << 4);
517 } else if (strcmp(attribute, "index_in_the_group") == 0) {
518 microphone.index_in_the_group = atoi(value);
519 found_mandatory_characteristics |= (1 << 5);
520 } else if (strcmp(attribute, "directionality") == 0) {
521 if (!find_enum_by_string(mic_directionalities, value,
522 AUDIO_MICROPHONE_DIRECTIONALITY_CNT, &microphone.directionality)) {
523 ALOGE("%s: directionality %s in %s not found!",
524 __func__, attr[index], PLATFORM_INFO_XML_PATH);
525 goto done;
526 }
527 found_mandatory_characteristics |= (1 << 6);
528 } else if (strcmp(attribute, "num_frequency_responses") == 0) {
529 microphone.num_frequency_responses = atoi(value);
530 if (microphone.num_frequency_responses > AUDIO_MICROPHONE_MAX_FREQUENCY_RESPONSES) {
531 ALOGE("%s: num_frequency_responses is too large", __func__);
532 goto done;
533 }
534 found_mandatory_characteristics |= (1 << 7);
535 } else if (strcmp(attribute, "frequencies") == 0) {
536 char *token = strtok(value, " ");
537 while (token) {
538 microphone.frequency_responses[0][num_frequencies++] = atof(token);
539 if (num_frequencies > AUDIO_MICROPHONE_MAX_FREQUENCY_RESPONSES) {
540 ALOGE("%s: num %u of frequency is too large", __func__, num_frequencies);
541 goto done;
542 }
543 token = strtok(NULL, " ");
544 }
545 found_mandatory_characteristics |= (1 << 8);
546 } else if (strcmp(attribute, "responses") == 0) {
547 char *token = strtok(value, " ");
548 while (token) {
549 microphone.frequency_responses[1][num_responses++] = atof(token);
550 if (num_responses > AUDIO_MICROPHONE_MAX_FREQUENCY_RESPONSES) {
551 ALOGE("%s: num %u of response is too large", __func__, num_responses);
552 goto done;
553 }
554 token = strtok(NULL, " ");
555 }
556 found_mandatory_characteristics |= (1 << 9);
557 } else if (strcmp(attribute, "sensitivity") == 0) {
558 microphone.sensitivity = atof(value);
559 } else if (strcmp(attribute, "max_spl") == 0) {
560 microphone.max_spl = atof(value);
561 } else if (strcmp(attribute, "min_spl") == 0) {
562 microphone.min_spl = atof(value);
563 } else if (strcmp(attribute, "orientation") == 0) {
564 char *token = strtok(value, " ");
565 float orientation[3];
566 uint32_t idx = 0;
567 while (token) {
568 orientation[idx++] = atof(token);
569 if (idx > 3) {
570 ALOGE("%s: orientation invalid", __func__);
571 goto done;
572 }
573 token = strtok(NULL, " ");
574 }
575 if (idx != 3) {
jiabin8962a4d2018-03-19 18:21:24 -0700576 ALOGE("%s: orientation invalid", __func__);
577 goto done;
578 }
jiabin749942e2018-05-25 12:37:29 -0700579 microphone.orientation.x = orientation[0];
580 microphone.orientation.y = orientation[1];
581 microphone.orientation.z = orientation[2];
582 } else if (strcmp(attribute, "geometric_location") == 0) {
583 char *token = strtok(value, " ");
584 float geometric_location[3];
585 uint32_t idx = 0;
586 while (token) {
587 geometric_location[idx++] = atof(token);
588 if (idx > 3) {
589 ALOGE("%s: geometric_location invalid", __func__);
590 goto done;
591 }
592 token = strtok(NULL, " ");
593 }
594 if (idx != 3) {
jiabin8962a4d2018-03-19 18:21:24 -0700595 ALOGE("%s: geometric_location invalid", __func__);
596 goto done;
597 }
jiabin749942e2018-05-25 12:37:29 -0700598 microphone.geometric_location.x = geometric_location[0];
599 microphone.geometric_location.y = geometric_location[1];
600 microphone.geometric_location.z = geometric_location[2];
601 } else {
602 ALOGW("%s: unknown attribute of microphone characteristics: %s",
603 __func__, attribute);
jiabin8962a4d2018-03-19 18:21:24 -0700604 }
jiabin749942e2018-05-25 12:37:29 -0700605 }
606
607 if (num_frequencies != num_responses
608 || num_frequencies != microphone.num_frequency_responses) {
609 ALOGE("%s: num of frequency and response not match: %u, %u, %u",
610 __func__, num_frequencies, num_responses, microphone.num_frequency_responses);
611 goto done;
612 }
613
614 if (found_mandatory_characteristics != MANDATORY_MICROPHONE_CHARACTERISTICS) {
615 ALOGE("%s: some of mandatory microphone characteriscts are missed: %u",
616 __func__, found_mandatory_characteristics);
jiabin8962a4d2018-03-19 18:21:24 -0700617 }
618
619 platform_set_microphone_characteristic(my_data.platform, microphone);
620done:
621 return;
622}
623
Aniket Kumar Latadebab2b2018-04-30 20:20:25 -0700624static void process_snd_dev(const XML_Char **attr)
625{
626 uint32_t curIdx = 0;
627 in_snd_device = SND_DEVICE_NONE;
628
629 if (strcmp(attr[curIdx++], "in_snd_device")) {
630 ALOGE("%s: snd_device not found", __func__);
631 return;
632 }
633 in_snd_device = platform_get_snd_device_index((char *)attr[curIdx++]);
634 if (in_snd_device < SND_DEVICE_IN_BEGIN ||
635 in_snd_device >= SND_DEVICE_IN_END) {
636 ALOGE("%s: Sound device not valid", __func__);
637 in_snd_device = SND_DEVICE_NONE;
638 }
639
640 return;
641}
642
643static void process_mic_info(const XML_Char **attr)
644{
645 uint32_t curIdx = 0;
646 struct mic_info microphone;
647
648 memset(&microphone.channel_mapping, AUDIO_MICROPHONE_CHANNEL_MAPPING_UNUSED,
649 sizeof(microphone.channel_mapping));
650
651 if (strcmp(attr[curIdx++], "mic_device_id")) {
652 ALOGE("%s: mic_device_id not found", __func__);
653 goto on_error;
654 }
655 strlcpy(microphone.device_id,
656 (char *)attr[curIdx++], AUDIO_MICROPHONE_ID_MAX_LEN);
657
658 if (strcmp(attr[curIdx++], "channel_mapping")) {
659 ALOGE("%s: channel_mapping not found", __func__);
660 goto on_error;
661 }
662 const char *token = strtok((char *)attr[curIdx++], " ");
663 uint32_t idx = 0;
664 while (token) {
665 if (!find_enum_by_string(mic_channel_mapping, token,
666 AUDIO_MICROPHONE_CHANNEL_MAPPING_CNT,
667 &microphone.channel_mapping[idx++])) {
668 ALOGE("%s: channel_mapping %s in %s not found!",
669 __func__, attr[--curIdx], PLATFORM_INFO_XML_PATH);
670 goto on_error;
671 }
672 token = strtok(NULL, " ");
673 }
674 microphone.channel_count = idx;
675
676 platform_set_microphone_map(my_data.platform, in_snd_device,
677 &microphone);
678 return;
679on_error:
680 in_snd_device = SND_DEVICE_NONE;
681 return;
682}
683
Ashish Jain1c849702018-05-11 20:11:55 +0530684/* process acdb meta info key value */
685static void process_acdb_metainfo_key(const XML_Char **attr)
686{
687 if (strcmp(attr[0], "name") != 0) {
688 ALOGE("%s: 'name' not found", __func__);
689 goto done;
690 }
691 if (strcmp(attr[2], "value") != 0) {
692 ALOGE("%s: 'value' not found", __func__);
693 goto done;
694 }
695
696 int key = atoi((char *)attr[3]);
697 if (platform_set_acdb_metainfo_key(my_data.platform,
698 (char*)attr[1], key) < 0) {
699 ALOGE("%s: key %d was not set!", __func__, key);
700 }
701
702done:
703 return;
704}
705
Haynes Mathew George5bc18842014-06-16 16:36:20 -0700706static void start_tag(void *userdata __unused, const XML_Char *tag_name,
707 const XML_Char **attr)
708{
709 const XML_Char *attr_name = NULL;
710 const XML_Char *attr_value = NULL;
711 unsigned int i;
712
Haynes Mathew George98c95622014-06-20 19:14:25 -0700713
vivek mehta0fb11312017-05-15 19:35:32 -0700714 if (my_data.do_full_parse) {
715 if (strcmp(tag_name, "acdb_ids") == 0) {
716 section = ACDB;
717 } else if (strcmp(tag_name, "pcm_ids") == 0) {
718 section = PCM_ID;
719 } else if (strcmp(tag_name, "backend_names") == 0) {
720 section = BACKEND_NAME;
721 } else if (strcmp(tag_name, "config_params") == 0) {
722 section = CONFIG_PARAMS;
723 } else if (strcmp(tag_name, "operator_specific") == 0) {
724 section = OPERATOR_SPECIFIC;
725 } else if (strcmp(tag_name, "gain_db_to_level_mapping") == 0) {
726 section = GAIN_LEVEL_MAPPING;
727 } else if (strcmp(tag_name, "app_types") == 0) {
728 section = APP_TYPE;
jiabin8962a4d2018-03-19 18:21:24 -0700729 } else if (strcmp(tag_name, "microphone_characteristics") == 0) {
730 section = MICROPHONE_CHARACTERISTIC;
Aniket Kumar Latadebab2b2018-04-30 20:20:25 -0700731 } else if (strcmp(tag_name, "snd_devices") == 0) {
732 section = SND_DEVICES;
Ashish Jain1c849702018-05-11 20:11:55 +0530733 } else if(strcmp(tag_name, "acdb_metainfo_key") == 0) {
734 section = ACDB_METAINFO_KEY;
vivek mehta0fb11312017-05-15 19:35:32 -0700735 } else if (strcmp(tag_name, "device") == 0) {
736 if ((section != ACDB) && (section != BACKEND_NAME) && (section != OPERATOR_SPECIFIC)) {
737 ALOGE("device tag only supported for acdb/backend names");
738 return;
739 }
Haynes Mathew George98c95622014-06-20 19:14:25 -0700740
vivek mehta0fb11312017-05-15 19:35:32 -0700741 /* call into process function for the current section */
742 section_process_fn fn = section_table[section];
743 fn(attr);
744 } else if (strcmp(tag_name, "usecase") == 0) {
745 if (section != PCM_ID) {
746 ALOGE("usecase tag only supported with PCM_ID section");
747 return;
748 }
Ravi Kumar Alamandac4f57312015-06-26 17:41:02 -0700749
vivek mehta0fb11312017-05-15 19:35:32 -0700750 section_process_fn fn = section_table[PCM_ID];
751 fn(attr);
752 } else if (strcmp(tag_name, "param") == 0) {
Ashish Jain1c849702018-05-11 20:11:55 +0530753 if ((section != CONFIG_PARAMS) && (section != ACDB_METAINFO_KEY)) {
vivek mehta0fb11312017-05-15 19:35:32 -0700754 ALOGE("param tag only supported with CONFIG_PARAMS section");
755 return;
756 }
vivek mehtaa8d7c922016-05-25 14:40:44 -0700757
vivek mehta0fb11312017-05-15 19:35:32 -0700758 section_process_fn fn = section_table[section];
759 fn(attr);
760 } else if (strcmp(tag_name, "gain_level_map") == 0) {
761 if (section != GAIN_LEVEL_MAPPING) {
jiabin8962a4d2018-03-19 18:21:24 -0700762 ALOGE("gain_level_map tag only supported with GAIN_LEVEL_MAPPING section");
vivek mehta0fb11312017-05-15 19:35:32 -0700763 return;
764 }
Haynes Mathew Georgee5ff0fc2017-02-16 20:33:38 -0800765
vivek mehta0fb11312017-05-15 19:35:32 -0700766 section_process_fn fn = section_table[GAIN_LEVEL_MAPPING];
767 fn(attr);
768 } else if (!strcmp(tag_name, "app")) {
769 if (section != APP_TYPE) {
770 ALOGE("app tag only valid in section APP_TYPE");
771 return;
772 }
773
774 section_process_fn fn = section_table[APP_TYPE];
775 fn(attr);
jiabin8962a4d2018-03-19 18:21:24 -0700776 } else if (strcmp(tag_name, "microphone") == 0) {
777 if (section != MICROPHONE_CHARACTERISTIC) {
778 ALOGE("microphone tag only supported with MICROPHONE_CHARACTERISTIC section");
779 return;
780 }
781 section_process_fn fn = section_table[MICROPHONE_CHARACTERISTIC];
782 fn(attr);
Aniket Kumar Latadebab2b2018-04-30 20:20:25 -0700783 } else if (strcmp(tag_name, "input_snd_device") == 0) {
784 if (section != SND_DEVICES) {
785 ALOGE("input_snd_device tag only supported with SND_DEVICES section");
786 return;
787 }
788 section = INPUT_SND_DEVICE;
789 } else if (strcmp(tag_name, "input_snd_device_mic_mapping") == 0) {
790 if (section != INPUT_SND_DEVICE) {
791 ALOGE("input_snd_device_mic_mapping tag only supported with INPUT_SND_DEVICE section");
792 return;
793 }
794 section = INPUT_SND_DEVICE_TO_MIC_MAPPING;
795 } else if (strcmp(tag_name, "snd_dev") == 0) {
796 if (section != INPUT_SND_DEVICE_TO_MIC_MAPPING) {
797 ALOGE("snd_dev tag only supported with INPUT_SND_DEVICE_TO_MIC_MAPPING section");
798 return;
799 }
800 section_process_fn fn = section_table[SND_DEV];
801 fn(attr);
802 } else if (strcmp(tag_name, "mic_info") == 0) {
803 if (section != INPUT_SND_DEVICE_TO_MIC_MAPPING) {
804 ALOGE("mic_info tag only supported with INPUT_SND_DEVICE_TO_MIC_MAPPING section");
805 return;
806 }
807 if (in_snd_device == SND_DEVICE_NONE) {
808 ALOGE("%s: Error in previous tags, do not process mic info", __func__);
809 return;
810 }
811 section_process_fn fn = section_table[MIC_INFO];
812 fn(attr);
vivek mehta0fb11312017-05-15 19:35:32 -0700813 }
814 } else {
815 if(strcmp(tag_name, "config_params") == 0) {
816 section = CONFIG_PARAMS;
817 } else if (strcmp(tag_name, "param") == 0) {
818 if (section != CONFIG_PARAMS) {
819 ALOGE("param tag only supported with CONFIG_PARAMS section");
820 return;
821 }
822
823 section_process_fn fn = section_table[section];
824 fn(attr);
825 }
Haynes Mathew George98c95622014-06-20 19:14:25 -0700826 }
Haynes Mathew George5bc18842014-06-16 16:36:20 -0700827
828 return;
829}
830
Haynes Mathew George98c95622014-06-20 19:14:25 -0700831static void end_tag(void *userdata __unused, const XML_Char *tag_name)
Haynes Mathew George5bc18842014-06-16 16:36:20 -0700832{
Haynes Mathew George98c95622014-06-20 19:14:25 -0700833 if (strcmp(tag_name, "acdb_ids") == 0) {
834 section = ROOT;
835 } else if (strcmp(tag_name, "pcm_ids") == 0) {
836 section = ROOT;
837 } else if (strcmp(tag_name, "backend_names") == 0) {
838 section = ROOT;
Ravi Kumar Alamandac4f57312015-06-26 17:41:02 -0700839 } else if (strcmp(tag_name, "config_params") == 0) {
840 section = ROOT;
keunhui.park2f7306a2015-07-16 16:48:06 +0900841 } else if (strcmp(tag_name, "operator_specific") == 0) {
842 section = ROOT;
vivek mehtaa8d7c922016-05-25 14:40:44 -0700843 } else if (strcmp(tag_name, "gain_db_to_level_mapping") == 0) {
844 section = ROOT;
Haynes Mathew Georgee5ff0fc2017-02-16 20:33:38 -0800845 } else if (strcmp(tag_name, "app_types") == 0) {
846 section = ROOT;
jiabin8962a4d2018-03-19 18:21:24 -0700847 } else if (strcmp(tag_name, "microphone_characteristics") == 0) {
848 section = ROOT;
Aniket Kumar Latadebab2b2018-04-30 20:20:25 -0700849 } else if (strcmp(tag_name, "snd_devices") == 0) {
850 section = ROOT;
851 } else if (strcmp(tag_name, "input_snd_device") == 0) {
852 section = SND_DEVICES;
853 } else if (strcmp(tag_name, "input_snd_device_mic_mapping") == 0) {
854 section = INPUT_SND_DEVICE;
Ashish Jain1c849702018-05-11 20:11:55 +0530855 } else if (strcmp(tag_name, "acdb_metainfo_key") == 0) {
856 section = ROOT;
Haynes Mathew George98c95622014-06-20 19:14:25 -0700857 }
Haynes Mathew George5bc18842014-06-16 16:36:20 -0700858}
859
vivek mehta0fb11312017-05-15 19:35:32 -0700860int snd_card_info_init(const char *filename, void *platform, set_parameters_fn fn)
861{
862 set_parameters = fn;
863 my_data.do_full_parse = false;
864 return platform_info_init(filename, platform);
865}
866
vivek mehtade4849c2016-03-03 17:23:38 -0800867int platform_info_init(const char *filename, void *platform)
Haynes Mathew George5bc18842014-06-16 16:36:20 -0700868{
869 XML_Parser parser;
870 FILE *file;
871 int ret = 0;
872 int bytes_read;
873 void *buf;
874 static const uint32_t kBufSize = 1024;
vivek mehtade4849c2016-03-03 17:23:38 -0800875 char platform_info_file_name[MIXER_PATH_MAX_LENGTH]= {0};
Haynes Mathew George98c95622014-06-20 19:14:25 -0700876 section = ROOT;
877
vivek mehtade4849c2016-03-03 17:23:38 -0800878 if (filename == NULL) {
879 strlcpy(platform_info_file_name, PLATFORM_INFO_XML_PATH, MIXER_PATH_MAX_LENGTH);
880 } else {
881 strlcpy(platform_info_file_name, filename, MIXER_PATH_MAX_LENGTH);
882 }
883
884 ALOGV("%s: platform info file name is %s", __func__, platform_info_file_name);
885
886 file = fopen(platform_info_file_name, "r");
887
Haynes Mathew George5bc18842014-06-16 16:36:20 -0700888 if (!file) {
889 ALOGD("%s: Failed to open %s, using defaults.",
vivek mehtade4849c2016-03-03 17:23:38 -0800890 __func__, platform_info_file_name);
Haynes Mathew George5bc18842014-06-16 16:36:20 -0700891 ret = -ENODEV;
892 goto done;
893 }
894
895 parser = XML_ParserCreate(NULL);
896 if (!parser) {
897 ALOGE("%s: Failed to create XML parser!", __func__);
898 ret = -ENODEV;
899 goto err_close_file;
900 }
901
Ravi Kumar Alamandac4f57312015-06-26 17:41:02 -0700902 my_data.platform = platform;
903 my_data.kvpairs = str_parms_create();
904
Haynes Mathew George5bc18842014-06-16 16:36:20 -0700905 XML_SetElementHandler(parser, start_tag, end_tag);
906
907 while (1) {
908 buf = XML_GetBuffer(parser, kBufSize);
909 if (buf == NULL) {
910 ALOGE("%s: XML_GetBuffer failed", __func__);
911 ret = -ENOMEM;
912 goto err_free_parser;
913 }
914
915 bytes_read = fread(buf, 1, kBufSize, file);
916 if (bytes_read < 0) {
917 ALOGE("%s: fread failed, bytes read = %d", __func__, bytes_read);
918 ret = bytes_read;
919 goto err_free_parser;
920 }
921
922 if (XML_ParseBuffer(parser, bytes_read,
923 bytes_read == 0) == XML_STATUS_ERROR) {
924 ALOGE("%s: XML_ParseBuffer failed, for %s",
vivek mehtade4849c2016-03-03 17:23:38 -0800925 __func__, platform_info_file_name);
Haynes Mathew George5bc18842014-06-16 16:36:20 -0700926 ret = -EINVAL;
927 goto err_free_parser;
928 }
929
930 if (bytes_read == 0)
931 break;
932 }
933
vivek mehta0fb11312017-05-15 19:35:32 -0700934 set_parameters = &platform_set_parameters;
935 my_data.do_full_parse = true;
936
Haynes Mathew George5bc18842014-06-16 16:36:20 -0700937err_free_parser:
938 XML_ParserFree(parser);
939err_close_file:
940 fclose(file);
941done:
942 return ret;
943}