blob: 0695592b941b769d0fdaa17627808974174c2891 [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>
juyuchenbf6571f2018-11-30 18:50:47 +080028#include <pthread.h>
Haynes Mathew George5bc18842014-06-16 16:36:20 -070029
jiabin749942e2018-05-25 12:37:29 -070030/*
31 * Mandatory microphone characteristics include: device_id, type, address, location, group,
32 * index_in_the_group, directionality, num_frequency_responses, frequencies and responses.
33 * MANDATORY_MICROPHONE_CHARACTERISTICS should be updated when mandatory microphone
34 * characteristics are changed.
35 */
36#define MANDATORY_MICROPHONE_CHARACTERISTICS (1 << 10) - 1
37
Haynes Mathew George98c95622014-06-20 19:14:25 -070038typedef enum {
39 ROOT,
40 ACDB,
41 PCM_ID,
42 BACKEND_NAME,
Ravi Kumar Alamandac4f57312015-06-26 17:41:02 -070043 CONFIG_PARAMS,
keunhui.park2f7306a2015-07-16 16:48:06 +090044 OPERATOR_SPECIFIC,
vivek mehtaa8d7c922016-05-25 14:40:44 -070045 GAIN_LEVEL_MAPPING,
Haynes Mathew Georgee5ff0fc2017-02-16 20:33:38 -080046 APP_TYPE,
jiabin8962a4d2018-03-19 18:21:24 -070047 MICROPHONE_CHARACTERISTIC,
Aniket Kumar Latadebab2b2018-04-30 20:20:25 -070048 SND_DEVICES,
49 INPUT_SND_DEVICE,
50 INPUT_SND_DEVICE_TO_MIC_MAPPING,
51 SND_DEV,
52 MIC_INFO,
Ashish Jain1c849702018-05-11 20:11:55 +053053 ACDB_METAINFO_KEY,
Haynes Mathew George98c95622014-06-20 19:14:25 -070054} section_t;
55
56typedef void (* section_process_fn)(const XML_Char **attr);
57
58static void process_acdb_id(const XML_Char **attr);
59static void process_pcm_id(const XML_Char **attr);
60static void process_backend_name(const XML_Char **attr);
Ravi Kumar Alamandac4f57312015-06-26 17:41:02 -070061static void process_config_params(const XML_Char **attr);
Haynes Mathew George98c95622014-06-20 19:14:25 -070062static void process_root(const XML_Char **attr);
keunhui.park2f7306a2015-07-16 16:48:06 +090063static void process_operator_specific(const XML_Char **attr);
vivek mehtaa8d7c922016-05-25 14:40:44 -070064static void process_gain_db_to_level_map(const XML_Char **attr);
Haynes Mathew Georgee5ff0fc2017-02-16 20:33:38 -080065static void process_app_type(const XML_Char **attr);
jiabin8962a4d2018-03-19 18:21:24 -070066static void process_microphone_characteristic(const XML_Char **attr);
Aniket Kumar Latadebab2b2018-04-30 20:20:25 -070067static void process_snd_dev(const XML_Char **attr);
68static void process_mic_info(const XML_Char **attr);
Ashish Jain1c849702018-05-11 20:11:55 +053069static void process_acdb_metainfo_key(const XML_Char **attr);
Haynes Mathew George98c95622014-06-20 19:14:25 -070070
71static section_process_fn section_table[] = {
72 [ROOT] = process_root,
73 [ACDB] = process_acdb_id,
74 [PCM_ID] = process_pcm_id,
75 [BACKEND_NAME] = process_backend_name,
Ravi Kumar Alamandac4f57312015-06-26 17:41:02 -070076 [CONFIG_PARAMS] = process_config_params,
keunhui.park2f7306a2015-07-16 16:48:06 +090077 [OPERATOR_SPECIFIC] = process_operator_specific,
vivek mehtaa8d7c922016-05-25 14:40:44 -070078 [GAIN_LEVEL_MAPPING] = process_gain_db_to_level_map,
Haynes Mathew Georgee5ff0fc2017-02-16 20:33:38 -080079 [APP_TYPE] = process_app_type,
jiabin8962a4d2018-03-19 18:21:24 -070080 [MICROPHONE_CHARACTERISTIC] = process_microphone_characteristic,
Aniket Kumar Latadebab2b2018-04-30 20:20:25 -070081 [SND_DEV] = process_snd_dev,
82 [MIC_INFO] = process_mic_info,
Ashish Jain1c849702018-05-11 20:11:55 +053083 [ACDB_METAINFO_KEY] = process_acdb_metainfo_key,
Haynes Mathew George98c95622014-06-20 19:14:25 -070084};
85
86static section_t section;
87
Ravi Kumar Alamandac4f57312015-06-26 17:41:02 -070088struct platform_info {
juyuchenbf6571f2018-11-30 18:50:47 +080089 pthread_mutex_t lock;
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;
juyuchenbf6571f2018-11-30 18:50:47 +080093 set_parameters_fn set_parameters;
Ravi Kumar Alamandac4f57312015-06-26 17:41:02 -070094};
95
juyuchenbf6571f2018-11-30 18:50:47 +080096static struct platform_info my_data = {PTHREAD_MUTEX_INITIALIZER,
97 true, NULL, NULL,
98 &platform_set_parameters};
Ravi Kumar Alamandac4f57312015-06-26 17:41:02 -070099
jiabin8962a4d2018-03-19 18:21:24 -0700100struct audio_string_to_enum {
101 const char* name;
102 unsigned int value;
103};
104
Aniket Kumar Latadebab2b2018-04-30 20:20:25 -0700105static snd_device_t in_snd_device;
106
jiabin8962a4d2018-03-19 18:21:24 -0700107static const struct audio_string_to_enum mic_locations[AUDIO_MICROPHONE_LOCATION_CNT] = {
108 AUDIO_MAKE_STRING_FROM_ENUM(AUDIO_MICROPHONE_LOCATION_UNKNOWN),
109 AUDIO_MAKE_STRING_FROM_ENUM(AUDIO_MICROPHONE_LOCATION_MAINBODY),
110 AUDIO_MAKE_STRING_FROM_ENUM(AUDIO_MICROPHONE_LOCATION_MAINBODY_MOVABLE),
111 AUDIO_MAKE_STRING_FROM_ENUM(AUDIO_MICROPHONE_LOCATION_PERIPHERAL),
112};
113
114static const struct audio_string_to_enum mic_directionalities[AUDIO_MICROPHONE_DIRECTIONALITY_CNT] = {
115 AUDIO_MAKE_STRING_FROM_ENUM(AUDIO_MICROPHONE_DIRECTIONALITY_OMNI),
116 AUDIO_MAKE_STRING_FROM_ENUM(AUDIO_MICROPHONE_DIRECTIONALITY_BI_DIRECTIONAL),
117 AUDIO_MAKE_STRING_FROM_ENUM(AUDIO_MICROPHONE_DIRECTIONALITY_UNKNOWN),
118 AUDIO_MAKE_STRING_FROM_ENUM(AUDIO_MICROPHONE_DIRECTIONALITY_CARDIOID),
119 AUDIO_MAKE_STRING_FROM_ENUM(AUDIO_MICROPHONE_DIRECTIONALITY_HYPER_CARDIOID),
120 AUDIO_MAKE_STRING_FROM_ENUM(AUDIO_MICROPHONE_DIRECTIONALITY_SUPER_CARDIOID),
121};
122
Aniket Kumar Latadebab2b2018-04-30 20:20:25 -0700123static const struct audio_string_to_enum mic_channel_mapping[AUDIO_MICROPHONE_CHANNEL_MAPPING_CNT] = {
124 AUDIO_MAKE_STRING_FROM_ENUM(AUDIO_MICROPHONE_CHANNEL_MAPPING_UNUSED),
125 AUDIO_MAKE_STRING_FROM_ENUM(AUDIO_MICROPHONE_CHANNEL_MAPPING_DIRECT),
126 AUDIO_MAKE_STRING_FROM_ENUM(AUDIO_MICROPHONE_CHANNEL_MAPPING_PROCESSED),
127};
128
jiabin8962a4d2018-03-19 18:21:24 -0700129static const struct audio_string_to_enum device_in_types[] = {
130 AUDIO_MAKE_STRING_FROM_ENUM(AUDIO_DEVICE_IN_AMBIENT),
131 AUDIO_MAKE_STRING_FROM_ENUM(AUDIO_DEVICE_IN_COMMUNICATION),
132 AUDIO_MAKE_STRING_FROM_ENUM(AUDIO_DEVICE_IN_BUILTIN_MIC),
133 AUDIO_MAKE_STRING_FROM_ENUM(AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET),
134 AUDIO_MAKE_STRING_FROM_ENUM(AUDIO_DEVICE_IN_WIRED_HEADSET),
135 AUDIO_MAKE_STRING_FROM_ENUM(AUDIO_DEVICE_IN_AUX_DIGITAL),
136 AUDIO_MAKE_STRING_FROM_ENUM(AUDIO_DEVICE_IN_HDMI),
137 AUDIO_MAKE_STRING_FROM_ENUM(AUDIO_DEVICE_IN_VOICE_CALL),
138 AUDIO_MAKE_STRING_FROM_ENUM(AUDIO_DEVICE_IN_TELEPHONY_RX),
139 AUDIO_MAKE_STRING_FROM_ENUM(AUDIO_DEVICE_IN_BACK_MIC),
140 AUDIO_MAKE_STRING_FROM_ENUM(AUDIO_DEVICE_IN_REMOTE_SUBMIX),
141 AUDIO_MAKE_STRING_FROM_ENUM(AUDIO_DEVICE_IN_ANLG_DOCK_HEADSET),
142 AUDIO_MAKE_STRING_FROM_ENUM(AUDIO_DEVICE_IN_DGTL_DOCK_HEADSET),
143 AUDIO_MAKE_STRING_FROM_ENUM(AUDIO_DEVICE_IN_USB_ACCESSORY),
144 AUDIO_MAKE_STRING_FROM_ENUM(AUDIO_DEVICE_IN_USB_DEVICE),
145 AUDIO_MAKE_STRING_FROM_ENUM(AUDIO_DEVICE_IN_FM_TUNER),
146 AUDIO_MAKE_STRING_FROM_ENUM(AUDIO_DEVICE_IN_TV_TUNER),
147 AUDIO_MAKE_STRING_FROM_ENUM(AUDIO_DEVICE_IN_LINE),
148 AUDIO_MAKE_STRING_FROM_ENUM(AUDIO_DEVICE_IN_SPDIF),
149 AUDIO_MAKE_STRING_FROM_ENUM(AUDIO_DEVICE_IN_BLUETOOTH_A2DP),
150 AUDIO_MAKE_STRING_FROM_ENUM(AUDIO_DEVICE_IN_LOOPBACK),
151 AUDIO_MAKE_STRING_FROM_ENUM(AUDIO_DEVICE_IN_IP),
152 AUDIO_MAKE_STRING_FROM_ENUM(AUDIO_DEVICE_IN_BUS),
153 AUDIO_MAKE_STRING_FROM_ENUM(AUDIO_DEVICE_IN_PROXY),
154 AUDIO_MAKE_STRING_FROM_ENUM(AUDIO_DEVICE_IN_USB_HEADSET),
155 AUDIO_MAKE_STRING_FROM_ENUM(AUDIO_DEVICE_IN_BLUETOOTH_BLE),
156 AUDIO_MAKE_STRING_FROM_ENUM(AUDIO_DEVICE_IN_DEFAULT),
157};
158
159static bool find_enum_by_string(const struct audio_string_to_enum * table, const char * name,
160 int32_t len, unsigned int *value)
161{
162 if (table == NULL) {
163 ALOGE("%s: table is NULL", __func__);
164 return false;
165 }
166
167 if (name == NULL) {
168 ALOGE("null key");
169 return false;
170 }
171
172 for (int i = 0; i < len; i++) {
173 if (!strcmp(table[i].name, name)) {
174 *value = table[i].value;
175 return true;
176 }
177 }
178 return false;
179}
180
Haynes Mathew George98c95622014-06-20 19:14:25 -0700181/*
182 * <audio_platform_info>
183 * <acdb_ids>
184 * <device name="???" acdb_id="???"/>
185 * ...
186 * ...
187 * </acdb_ids>
188 * <backend_names>
189 * <device name="???" backend="???"/>
190 * ...
191 * ...
192 * </backend_names>
193 * <pcm_ids>
194 * <usecase name="???" type="in/out" id="???"/>
195 * ...
196 * ...
197 * </pcm_ids>
Ravi Kumar Alamandac4f57312015-06-26 17:41:02 -0700198 * <config_params>
199 * <param key="snd_card_name" value="msm8994-tomtom-mtp-snd-card"/>
keunhui.park2f7306a2015-07-16 16:48:06 +0900200 * <param key="operator_info" value="tmus;aa;bb;cc"/>
201 * <param key="operator_info" value="sprint;xx;yy;zz"/>
Ravi Kumar Alamandac4f57312015-06-26 17:41:02 -0700202 * ...
203 * ...
204 * </config_params>
205 *
keunhui.park2f7306a2015-07-16 16:48:06 +0900206 * <operator_specific>
207 * <device name="???" operator="???" mixer_path="???" acdb_id="???"/>
208 * ...
209 * ...
210 * </operator_specific>
211 *
Haynes Mathew George98c95622014-06-20 19:14:25 -0700212 * </audio_platform_info>
213 */
214
215static void process_root(const XML_Char **attr __unused)
216{
217}
218
219/* mapping from usecase to pcm dev id */
220static void process_pcm_id(const XML_Char **attr)
221{
222 int index;
223
224 if (strcmp(attr[0], "name") != 0) {
Ravi Kumar Alamandac4f57312015-06-26 17:41:02 -0700225 ALOGE("%s: 'name' not found, no pcm_id set!", __func__);
Haynes Mathew George98c95622014-06-20 19:14:25 -0700226 goto done;
227 }
228
229 index = platform_get_usecase_index((char *)attr[1]);
230 if (index < 0) {
231 ALOGE("%s: usecase %s in %s not found!",
232 __func__, attr[1], PLATFORM_INFO_XML_PATH);
233 goto done;
234 }
235
236 if (strcmp(attr[2], "type") != 0) {
237 ALOGE("%s: usecase type not mentioned", __func__);
238 goto done;
239 }
240
241 int type = -1;
242
243 if (!strcasecmp((char *)attr[3], "in")) {
244 type = 1;
245 } else if (!strcasecmp((char *)attr[3], "out")) {
246 type = 0;
247 } else {
248 ALOGE("%s: type must be IN or OUT", __func__);
249 goto done;
250 }
251
252 if (strcmp(attr[4], "id") != 0) {
253 ALOGE("%s: usecase id not mentioned", __func__);
254 goto done;
255 }
256
257 int id = atoi((char *)attr[5]);
258
259 if (platform_set_usecase_pcm_id(index, type, id) < 0) {
260 ALOGE("%s: usecase %s in %s, type %d id %d was not set!",
261 __func__, attr[1], PLATFORM_INFO_XML_PATH, type, id);
262 goto done;
263 }
264
265done:
266 return;
267}
268
269/* backend to be used for a device */
270static void process_backend_name(const XML_Char **attr)
271{
272 int index;
Ravi Kumar Alamandab7ea4f52015-06-08 16:44:05 -0700273 char *hw_interface = NULL;
Haynes Mathew George98c95622014-06-20 19:14:25 -0700274
275 if (strcmp(attr[0], "name") != 0) {
276 ALOGE("%s: 'name' not found, no ACDB ID set!", __func__);
277 goto done;
278 }
279
280 index = platform_get_snd_device_index((char *)attr[1]);
281 if (index < 0) {
282 ALOGE("%s: Device %s in %s not found, no ACDB ID set!",
283 __func__, attr[1], PLATFORM_INFO_XML_PATH);
284 goto done;
285 }
286
287 if (strcmp(attr[2], "backend") != 0) {
288 ALOGE("%s: Device %s in %s has no backed set!",
289 __func__, attr[1], PLATFORM_INFO_XML_PATH);
290 goto done;
291 }
292
Ravi Kumar Alamandab7ea4f52015-06-08 16:44:05 -0700293 if (attr[4] != NULL) {
294 if (strcmp(attr[4], "interface") != 0) {
295 hw_interface = NULL;
296 } else {
297 hw_interface = (char *)attr[5];
298 }
299 }
300
301 if (platform_set_snd_device_backend(index, attr[3], hw_interface) < 0) {
Haynes Mathew George98c95622014-06-20 19:14:25 -0700302 ALOGE("%s: Device %s in %s, backend %s was not set!",
303 __func__, attr[1], PLATFORM_INFO_XML_PATH, attr[3]);
304 goto done;
305 }
306
307done:
308 return;
309}
310
vivek mehtaa8d7c922016-05-25 14:40:44 -0700311static void process_gain_db_to_level_map(const XML_Char **attr)
312{
313 struct amp_db_and_gain_table tbl_entry;
314
315 if ((strcmp(attr[0], "db") != 0) ||
316 (strcmp(attr[2], "level") != 0)) {
317 ALOGE("%s: invalid attribute passed %s %sexpected amp db level",
318 __func__, attr[0], attr[2]);
319 goto done;
320 }
321
322 tbl_entry.db = atof(attr[1]);
323 tbl_entry.amp = exp(tbl_entry.db * 0.115129f);
324 tbl_entry.level = atoi(attr[3]);
325
vivek mehta40125092017-08-21 18:48:51 -0700326 //custome level should be > 0. Level 0 is fixed for default
327 CHECK(tbl_entry.level > 0);
328
vivek mehtaa8d7c922016-05-25 14:40:44 -0700329 ALOGV("%s: amp [%f] db [%f] level [%d]", __func__,
330 tbl_entry.amp, tbl_entry.db, tbl_entry.level);
331 platform_add_gain_level_mapping(&tbl_entry);
332
333done:
334 return;
335}
336
Haynes Mathew George98c95622014-06-20 19:14:25 -0700337static void process_acdb_id(const XML_Char **attr)
Haynes Mathew George5bc18842014-06-16 16:36:20 -0700338{
339 int index;
340
341 if (strcmp(attr[0], "name") != 0) {
342 ALOGE("%s: 'name' not found, no ACDB ID set!", __func__);
343 goto done;
344 }
345
346 index = platform_get_snd_device_index((char *)attr[1]);
347 if (index < 0) {
348 ALOGE("%s: Device %s in %s not found, no ACDB ID set!",
349 __func__, attr[1], PLATFORM_INFO_XML_PATH);
350 goto done;
351 }
352
353 if (strcmp(attr[2], "acdb_id") != 0) {
354 ALOGE("%s: Device %s in %s has no acdb_id, no ACDB ID set!",
355 __func__, attr[1], PLATFORM_INFO_XML_PATH);
356 goto done;
357 }
358
Haynes Mathew George98c95622014-06-20 19:14:25 -0700359 if (platform_set_snd_device_acdb_id(index, atoi((char *)attr[3])) < 0) {
Haynes Mathew George5bc18842014-06-16 16:36:20 -0700360 ALOGE("%s: Device %s in %s, ACDB ID %d was not set!",
361 __func__, attr[1], PLATFORM_INFO_XML_PATH, atoi((char *)attr[3]));
362 goto done;
363 }
364
365done:
366 return;
367}
368
keunhui.park2f7306a2015-07-16 16:48:06 +0900369
370static void process_operator_specific(const XML_Char **attr)
371{
372 snd_device_t snd_device = SND_DEVICE_NONE;
373
374 if (strcmp(attr[0], "name") != 0) {
375 ALOGE("%s: 'name' not found", __func__);
376 goto done;
377 }
378
379 snd_device = platform_get_snd_device_index((char *)attr[1]);
380 if (snd_device < 0) {
381 ALOGE("%s: Device %s in %s not found, no ACDB ID set!",
382 __func__, (char *)attr[3], PLATFORM_INFO_XML_PATH);
383 goto done;
384 }
385
386 if (strcmp(attr[2], "operator") != 0) {
387 ALOGE("%s: 'operator' not found", __func__);
388 goto done;
389 }
390
391 if (strcmp(attr[4], "mixer_path") != 0) {
392 ALOGE("%s: 'mixer_path' not found", __func__);
393 goto done;
394 }
395
396 if (strcmp(attr[6], "acdb_id") != 0) {
397 ALOGE("%s: 'acdb_id' not found", __func__);
398 goto done;
399 }
400
401 platform_add_operator_specific_device(snd_device, (char *)attr[3], (char *)attr[5], atoi((char *)attr[7]));
402
403done:
404 return;
405}
406
Ravi Kumar Alamandac4f57312015-06-26 17:41:02 -0700407/* platform specific configuration key-value pairs */
408static void process_config_params(const XML_Char **attr)
409{
410 if (strcmp(attr[0], "key") != 0) {
411 ALOGE("%s: 'key' not found", __func__);
412 goto done;
413 }
414
415 if (strcmp(attr[2], "value") != 0) {
416 ALOGE("%s: 'value' not found", __func__);
417 goto done;
418 }
419
420 str_parms_add_str(my_data.kvpairs, (char*)attr[1], (char*)attr[3]);
juyuchenbf6571f2018-11-30 18:50:47 +0800421 my_data.set_parameters(my_data.platform, my_data.kvpairs);
Ravi Kumar Alamandac4f57312015-06-26 17:41:02 -0700422done:
423 return;
424}
425
Haynes Mathew Georgee5ff0fc2017-02-16 20:33:38 -0800426static void process_app_type(const XML_Char **attr)
427{
428 if (strcmp(attr[0], "uc_type")) {
429 ALOGE("%s: uc_type not found", __func__);
430 goto done;
431 }
432
vivek mehtaa68fea62017-06-08 19:04:02 -0700433 if (strcmp(attr[2], "mode")) {
434 ALOGE("%s: mode not found", __func__);
435 goto done;
436 }
437
438 if (strcmp(attr[4], "bit_width")) {
Haynes Mathew Georgee5ff0fc2017-02-16 20:33:38 -0800439 ALOGE("%s: bit_width not found", __func__);
440 goto done;
441 }
442
vivek mehtaa68fea62017-06-08 19:04:02 -0700443 if (strcmp(attr[6], "id")) {
Haynes Mathew Georgee5ff0fc2017-02-16 20:33:38 -0800444 ALOGE("%s: id not found", __func__);
445 goto done;
446 }
447
vivek mehtaa68fea62017-06-08 19:04:02 -0700448 if (strcmp(attr[8], "max_rate")) {
Haynes Mathew Georgee5ff0fc2017-02-16 20:33:38 -0800449 ALOGE("%s: max rate not found", __func__);
450 goto done;
451 }
452
vivek mehtaa68fea62017-06-08 19:04:02 -0700453 platform_add_app_type(attr[1], attr[3], atoi(attr[5]), atoi(attr[7]),
454 atoi(attr[9]));
Haynes Mathew Georgee5ff0fc2017-02-16 20:33:38 -0800455done:
456 return;
457}
458
jiabin8962a4d2018-03-19 18:21:24 -0700459static void process_microphone_characteristic(const XML_Char **attr) {
460 struct audio_microphone_characteristic_t microphone;
jiabin749942e2018-05-25 12:37:29 -0700461 uint32_t index = 0;
462 uint32_t found_mandatory_characteristics = 0;
463 uint32_t num_frequencies = 0;
464 uint32_t num_responses = 0;
465 microphone.sensitivity = AUDIO_MICROPHONE_SENSITIVITY_UNKNOWN;
466 microphone.max_spl = AUDIO_MICROPHONE_SPL_UNKNOWN;
467 microphone.min_spl = AUDIO_MICROPHONE_SPL_UNKNOWN;
468 microphone.orientation.x = 0.0f;
469 microphone.orientation.y = 0.0f;
470 microphone.orientation.z = 0.0f;
471 microphone.geometric_location.x = AUDIO_MICROPHONE_COORDINATE_UNKNOWN;
472 microphone.geometric_location.y = AUDIO_MICROPHONE_COORDINATE_UNKNOWN;
473 microphone.geometric_location.z = AUDIO_MICROPHONE_COORDINATE_UNKNOWN;
jiabin8962a4d2018-03-19 18:21:24 -0700474
jiabin749942e2018-05-25 12:37:29 -0700475 while (attr[index] != NULL) {
476 const char *attribute = attr[index++];
477 char value[strlen(attr[index]) + 1];
478 strcpy(value, attr[index++]);
479 if (strcmp(attribute, "device_id") == 0) {
480 if (strlen(value) > AUDIO_MICROPHONE_ID_MAX_LEN) {
481 ALOGE("%s: device_id %s is too long", __func__, value);
jiabin8962a4d2018-03-19 18:21:24 -0700482 goto done;
483 }
jiabin749942e2018-05-25 12:37:29 -0700484 strcpy(microphone.device_id, value);
485 found_mandatory_characteristics |= 1;
486 } else if (strcmp(attribute, "type") == 0) {
487 if (!find_enum_by_string(device_in_types, value,
488 ARRAY_SIZE(device_in_types), &microphone.device)) {
489 ALOGE("%s: type %s in %s not found!",
490 __func__, value, PLATFORM_INFO_XML_PATH);
jiabin8962a4d2018-03-19 18:21:24 -0700491 goto done;
492 }
jiabin749942e2018-05-25 12:37:29 -0700493 found_mandatory_characteristics |= (1 << 1);
494 } else if (strcmp(attribute, "address") == 0) {
495 if (strlen(value) > AUDIO_DEVICE_MAX_ADDRESS_LEN) {
496 ALOGE("%s, address %s is too long", __func__, value);
497 goto done;
498 }
499 strcpy(microphone.address, value);
500 if (strlen(microphone.address) == 0) {
501 // If the address is empty, populate the address according to device type.
502 if (microphone.device == AUDIO_DEVICE_IN_BUILTIN_MIC) {
503 strcpy(microphone.address, AUDIO_BOTTOM_MICROPHONE_ADDRESS);
504 } else if (microphone.device == AUDIO_DEVICE_IN_BACK_MIC) {
505 strcpy(microphone.address, AUDIO_BACK_MICROPHONE_ADDRESS);
506 }
507 }
508 found_mandatory_characteristics |= (1 << 2);
509 } else if (strcmp(attribute, "location") == 0) {
510 if (!find_enum_by_string(mic_locations, value,
511 AUDIO_MICROPHONE_LOCATION_CNT, &microphone.location)) {
512 ALOGE("%s: location %s in %s not found!",
513 __func__, value, PLATFORM_INFO_XML_PATH);
514 goto done;
515 }
516 found_mandatory_characteristics |= (1 << 3);
517 } else if (strcmp(attribute, "group") == 0) {
518 microphone.group = atoi(value);
519 found_mandatory_characteristics |= (1 << 4);
520 } else if (strcmp(attribute, "index_in_the_group") == 0) {
521 microphone.index_in_the_group = atoi(value);
522 found_mandatory_characteristics |= (1 << 5);
523 } else if (strcmp(attribute, "directionality") == 0) {
524 if (!find_enum_by_string(mic_directionalities, value,
525 AUDIO_MICROPHONE_DIRECTIONALITY_CNT, &microphone.directionality)) {
526 ALOGE("%s: directionality %s in %s not found!",
527 __func__, attr[index], PLATFORM_INFO_XML_PATH);
528 goto done;
529 }
530 found_mandatory_characteristics |= (1 << 6);
531 } else if (strcmp(attribute, "num_frequency_responses") == 0) {
532 microphone.num_frequency_responses = atoi(value);
533 if (microphone.num_frequency_responses > AUDIO_MICROPHONE_MAX_FREQUENCY_RESPONSES) {
534 ALOGE("%s: num_frequency_responses is too large", __func__);
535 goto done;
536 }
537 found_mandatory_characteristics |= (1 << 7);
538 } else if (strcmp(attribute, "frequencies") == 0) {
539 char *token = strtok(value, " ");
540 while (token) {
541 microphone.frequency_responses[0][num_frequencies++] = atof(token);
542 if (num_frequencies > AUDIO_MICROPHONE_MAX_FREQUENCY_RESPONSES) {
543 ALOGE("%s: num %u of frequency is too large", __func__, num_frequencies);
544 goto done;
545 }
546 token = strtok(NULL, " ");
547 }
548 found_mandatory_characteristics |= (1 << 8);
549 } else if (strcmp(attribute, "responses") == 0) {
550 char *token = strtok(value, " ");
551 while (token) {
552 microphone.frequency_responses[1][num_responses++] = atof(token);
553 if (num_responses > AUDIO_MICROPHONE_MAX_FREQUENCY_RESPONSES) {
554 ALOGE("%s: num %u of response is too large", __func__, num_responses);
555 goto done;
556 }
557 token = strtok(NULL, " ");
558 }
559 found_mandatory_characteristics |= (1 << 9);
560 } else if (strcmp(attribute, "sensitivity") == 0) {
561 microphone.sensitivity = atof(value);
562 } else if (strcmp(attribute, "max_spl") == 0) {
563 microphone.max_spl = atof(value);
564 } else if (strcmp(attribute, "min_spl") == 0) {
565 microphone.min_spl = atof(value);
566 } else if (strcmp(attribute, "orientation") == 0) {
567 char *token = strtok(value, " ");
568 float orientation[3];
569 uint32_t idx = 0;
570 while (token) {
571 orientation[idx++] = atof(token);
572 if (idx > 3) {
573 ALOGE("%s: orientation invalid", __func__);
574 goto done;
575 }
576 token = strtok(NULL, " ");
577 }
578 if (idx != 3) {
jiabin8962a4d2018-03-19 18:21:24 -0700579 ALOGE("%s: orientation invalid", __func__);
580 goto done;
581 }
jiabin749942e2018-05-25 12:37:29 -0700582 microphone.orientation.x = orientation[0];
583 microphone.orientation.y = orientation[1];
584 microphone.orientation.z = orientation[2];
585 } else if (strcmp(attribute, "geometric_location") == 0) {
586 char *token = strtok(value, " ");
587 float geometric_location[3];
588 uint32_t idx = 0;
589 while (token) {
590 geometric_location[idx++] = atof(token);
591 if (idx > 3) {
592 ALOGE("%s: geometric_location invalid", __func__);
593 goto done;
594 }
595 token = strtok(NULL, " ");
596 }
597 if (idx != 3) {
jiabin8962a4d2018-03-19 18:21:24 -0700598 ALOGE("%s: geometric_location invalid", __func__);
599 goto done;
600 }
jiabin749942e2018-05-25 12:37:29 -0700601 microphone.geometric_location.x = geometric_location[0];
602 microphone.geometric_location.y = geometric_location[1];
603 microphone.geometric_location.z = geometric_location[2];
604 } else {
605 ALOGW("%s: unknown attribute of microphone characteristics: %s",
606 __func__, attribute);
jiabin8962a4d2018-03-19 18:21:24 -0700607 }
jiabin749942e2018-05-25 12:37:29 -0700608 }
609
610 if (num_frequencies != num_responses
611 || num_frequencies != microphone.num_frequency_responses) {
612 ALOGE("%s: num of frequency and response not match: %u, %u, %u",
613 __func__, num_frequencies, num_responses, microphone.num_frequency_responses);
614 goto done;
615 }
616
617 if (found_mandatory_characteristics != MANDATORY_MICROPHONE_CHARACTERISTICS) {
618 ALOGE("%s: some of mandatory microphone characteriscts are missed: %u",
619 __func__, found_mandatory_characteristics);
jiabin8962a4d2018-03-19 18:21:24 -0700620 }
621
622 platform_set_microphone_characteristic(my_data.platform, microphone);
623done:
624 return;
625}
626
Aniket Kumar Latadebab2b2018-04-30 20:20:25 -0700627static void process_snd_dev(const XML_Char **attr)
628{
629 uint32_t curIdx = 0;
630 in_snd_device = SND_DEVICE_NONE;
631
632 if (strcmp(attr[curIdx++], "in_snd_device")) {
633 ALOGE("%s: snd_device not found", __func__);
634 return;
635 }
636 in_snd_device = platform_get_snd_device_index((char *)attr[curIdx++]);
637 if (in_snd_device < SND_DEVICE_IN_BEGIN ||
638 in_snd_device >= SND_DEVICE_IN_END) {
639 ALOGE("%s: Sound device not valid", __func__);
640 in_snd_device = SND_DEVICE_NONE;
641 }
642
643 return;
644}
645
646static void process_mic_info(const XML_Char **attr)
647{
648 uint32_t curIdx = 0;
649 struct mic_info microphone;
650
651 memset(&microphone.channel_mapping, AUDIO_MICROPHONE_CHANNEL_MAPPING_UNUSED,
652 sizeof(microphone.channel_mapping));
653
654 if (strcmp(attr[curIdx++], "mic_device_id")) {
655 ALOGE("%s: mic_device_id not found", __func__);
656 goto on_error;
657 }
658 strlcpy(microphone.device_id,
659 (char *)attr[curIdx++], AUDIO_MICROPHONE_ID_MAX_LEN);
660
661 if (strcmp(attr[curIdx++], "channel_mapping")) {
662 ALOGE("%s: channel_mapping not found", __func__);
663 goto on_error;
664 }
665 const char *token = strtok((char *)attr[curIdx++], " ");
666 uint32_t idx = 0;
667 while (token) {
668 if (!find_enum_by_string(mic_channel_mapping, token,
669 AUDIO_MICROPHONE_CHANNEL_MAPPING_CNT,
670 &microphone.channel_mapping[idx++])) {
671 ALOGE("%s: channel_mapping %s in %s not found!",
672 __func__, attr[--curIdx], PLATFORM_INFO_XML_PATH);
673 goto on_error;
674 }
675 token = strtok(NULL, " ");
676 }
677 microphone.channel_count = idx;
678
679 platform_set_microphone_map(my_data.platform, in_snd_device,
680 &microphone);
681 return;
682on_error:
683 in_snd_device = SND_DEVICE_NONE;
684 return;
685}
686
Ashish Jain1c849702018-05-11 20:11:55 +0530687/* process acdb meta info key value */
688static void process_acdb_metainfo_key(const XML_Char **attr)
689{
690 if (strcmp(attr[0], "name") != 0) {
691 ALOGE("%s: 'name' not found", __func__);
692 goto done;
693 }
694 if (strcmp(attr[2], "value") != 0) {
695 ALOGE("%s: 'value' not found", __func__);
696 goto done;
697 }
698
699 int key = atoi((char *)attr[3]);
700 if (platform_set_acdb_metainfo_key(my_data.platform,
701 (char*)attr[1], key) < 0) {
702 ALOGE("%s: key %d was not set!", __func__, key);
703 }
704
705done:
706 return;
707}
708
Haynes Mathew George5bc18842014-06-16 16:36:20 -0700709static void start_tag(void *userdata __unused, const XML_Char *tag_name,
710 const XML_Char **attr)
711{
712 const XML_Char *attr_name = NULL;
713 const XML_Char *attr_value = NULL;
714 unsigned int i;
715
Haynes Mathew George98c95622014-06-20 19:14:25 -0700716
vivek mehta0fb11312017-05-15 19:35:32 -0700717 if (my_data.do_full_parse) {
718 if (strcmp(tag_name, "acdb_ids") == 0) {
719 section = ACDB;
720 } else if (strcmp(tag_name, "pcm_ids") == 0) {
721 section = PCM_ID;
722 } else if (strcmp(tag_name, "backend_names") == 0) {
723 section = BACKEND_NAME;
724 } else if (strcmp(tag_name, "config_params") == 0) {
725 section = CONFIG_PARAMS;
726 } else if (strcmp(tag_name, "operator_specific") == 0) {
727 section = OPERATOR_SPECIFIC;
728 } else if (strcmp(tag_name, "gain_db_to_level_mapping") == 0) {
729 section = GAIN_LEVEL_MAPPING;
730 } else if (strcmp(tag_name, "app_types") == 0) {
731 section = APP_TYPE;
jiabin8962a4d2018-03-19 18:21:24 -0700732 } else if (strcmp(tag_name, "microphone_characteristics") == 0) {
733 section = MICROPHONE_CHARACTERISTIC;
Aniket Kumar Latadebab2b2018-04-30 20:20:25 -0700734 } else if (strcmp(tag_name, "snd_devices") == 0) {
735 section = SND_DEVICES;
Ashish Jain1c849702018-05-11 20:11:55 +0530736 } else if(strcmp(tag_name, "acdb_metainfo_key") == 0) {
737 section = ACDB_METAINFO_KEY;
vivek mehta0fb11312017-05-15 19:35:32 -0700738 } else if (strcmp(tag_name, "device") == 0) {
739 if ((section != ACDB) && (section != BACKEND_NAME) && (section != OPERATOR_SPECIFIC)) {
740 ALOGE("device tag only supported for acdb/backend names");
741 return;
742 }
Haynes Mathew George98c95622014-06-20 19:14:25 -0700743
vivek mehta0fb11312017-05-15 19:35:32 -0700744 /* call into process function for the current section */
745 section_process_fn fn = section_table[section];
746 fn(attr);
747 } else if (strcmp(tag_name, "usecase") == 0) {
748 if (section != PCM_ID) {
749 ALOGE("usecase tag only supported with PCM_ID section");
750 return;
751 }
Ravi Kumar Alamandac4f57312015-06-26 17:41:02 -0700752
vivek mehta0fb11312017-05-15 19:35:32 -0700753 section_process_fn fn = section_table[PCM_ID];
754 fn(attr);
755 } else if (strcmp(tag_name, "param") == 0) {
Ashish Jain1c849702018-05-11 20:11:55 +0530756 if ((section != CONFIG_PARAMS) && (section != ACDB_METAINFO_KEY)) {
vivek mehta0fb11312017-05-15 19:35:32 -0700757 ALOGE("param tag only supported with CONFIG_PARAMS section");
758 return;
759 }
vivek mehtaa8d7c922016-05-25 14:40:44 -0700760
vivek mehta0fb11312017-05-15 19:35:32 -0700761 section_process_fn fn = section_table[section];
762 fn(attr);
763 } else if (strcmp(tag_name, "gain_level_map") == 0) {
764 if (section != GAIN_LEVEL_MAPPING) {
jiabin8962a4d2018-03-19 18:21:24 -0700765 ALOGE("gain_level_map tag only supported with GAIN_LEVEL_MAPPING section");
vivek mehta0fb11312017-05-15 19:35:32 -0700766 return;
767 }
Haynes Mathew Georgee5ff0fc2017-02-16 20:33:38 -0800768
vivek mehta0fb11312017-05-15 19:35:32 -0700769 section_process_fn fn = section_table[GAIN_LEVEL_MAPPING];
770 fn(attr);
771 } else if (!strcmp(tag_name, "app")) {
772 if (section != APP_TYPE) {
773 ALOGE("app tag only valid in section APP_TYPE");
774 return;
775 }
776
777 section_process_fn fn = section_table[APP_TYPE];
778 fn(attr);
jiabin8962a4d2018-03-19 18:21:24 -0700779 } else if (strcmp(tag_name, "microphone") == 0) {
780 if (section != MICROPHONE_CHARACTERISTIC) {
781 ALOGE("microphone tag only supported with MICROPHONE_CHARACTERISTIC section");
782 return;
783 }
784 section_process_fn fn = section_table[MICROPHONE_CHARACTERISTIC];
785 fn(attr);
Aniket Kumar Latadebab2b2018-04-30 20:20:25 -0700786 } else if (strcmp(tag_name, "input_snd_device") == 0) {
787 if (section != SND_DEVICES) {
788 ALOGE("input_snd_device tag only supported with SND_DEVICES section");
789 return;
790 }
791 section = INPUT_SND_DEVICE;
792 } else if (strcmp(tag_name, "input_snd_device_mic_mapping") == 0) {
793 if (section != INPUT_SND_DEVICE) {
794 ALOGE("input_snd_device_mic_mapping tag only supported with INPUT_SND_DEVICE section");
795 return;
796 }
797 section = INPUT_SND_DEVICE_TO_MIC_MAPPING;
798 } else if (strcmp(tag_name, "snd_dev") == 0) {
799 if (section != INPUT_SND_DEVICE_TO_MIC_MAPPING) {
800 ALOGE("snd_dev tag only supported with INPUT_SND_DEVICE_TO_MIC_MAPPING section");
801 return;
802 }
803 section_process_fn fn = section_table[SND_DEV];
804 fn(attr);
805 } else if (strcmp(tag_name, "mic_info") == 0) {
806 if (section != INPUT_SND_DEVICE_TO_MIC_MAPPING) {
807 ALOGE("mic_info tag only supported with INPUT_SND_DEVICE_TO_MIC_MAPPING section");
808 return;
809 }
810 if (in_snd_device == SND_DEVICE_NONE) {
811 ALOGE("%s: Error in previous tags, do not process mic info", __func__);
812 return;
813 }
814 section_process_fn fn = section_table[MIC_INFO];
815 fn(attr);
vivek mehta0fb11312017-05-15 19:35:32 -0700816 }
817 } else {
818 if(strcmp(tag_name, "config_params") == 0) {
819 section = CONFIG_PARAMS;
820 } else if (strcmp(tag_name, "param") == 0) {
821 if (section != CONFIG_PARAMS) {
822 ALOGE("param tag only supported with CONFIG_PARAMS section");
823 return;
824 }
825
826 section_process_fn fn = section_table[section];
827 fn(attr);
828 }
Haynes Mathew George98c95622014-06-20 19:14:25 -0700829 }
Haynes Mathew George5bc18842014-06-16 16:36:20 -0700830
831 return;
832}
833
Haynes Mathew George98c95622014-06-20 19:14:25 -0700834static void end_tag(void *userdata __unused, const XML_Char *tag_name)
Haynes Mathew George5bc18842014-06-16 16:36:20 -0700835{
Haynes Mathew George98c95622014-06-20 19:14:25 -0700836 if (strcmp(tag_name, "acdb_ids") == 0) {
837 section = ROOT;
838 } else if (strcmp(tag_name, "pcm_ids") == 0) {
839 section = ROOT;
840 } else if (strcmp(tag_name, "backend_names") == 0) {
841 section = ROOT;
Ravi Kumar Alamandac4f57312015-06-26 17:41:02 -0700842 } else if (strcmp(tag_name, "config_params") == 0) {
843 section = ROOT;
keunhui.park2f7306a2015-07-16 16:48:06 +0900844 } else if (strcmp(tag_name, "operator_specific") == 0) {
845 section = ROOT;
vivek mehtaa8d7c922016-05-25 14:40:44 -0700846 } else if (strcmp(tag_name, "gain_db_to_level_mapping") == 0) {
847 section = ROOT;
Haynes Mathew Georgee5ff0fc2017-02-16 20:33:38 -0800848 } else if (strcmp(tag_name, "app_types") == 0) {
849 section = ROOT;
jiabin8962a4d2018-03-19 18:21:24 -0700850 } else if (strcmp(tag_name, "microphone_characteristics") == 0) {
851 section = ROOT;
Aniket Kumar Latadebab2b2018-04-30 20:20:25 -0700852 } else if (strcmp(tag_name, "snd_devices") == 0) {
853 section = ROOT;
854 } else if (strcmp(tag_name, "input_snd_device") == 0) {
855 section = SND_DEVICES;
856 } else if (strcmp(tag_name, "input_snd_device_mic_mapping") == 0) {
857 section = INPUT_SND_DEVICE;
Ashish Jain1c849702018-05-11 20:11:55 +0530858 } else if (strcmp(tag_name, "acdb_metainfo_key") == 0) {
859 section = ROOT;
Haynes Mathew George98c95622014-06-20 19:14:25 -0700860 }
Haynes Mathew George5bc18842014-06-16 16:36:20 -0700861}
862
juyuchenbf6571f2018-11-30 18:50:47 +0800863int platform_info_init(const char *filename, void *platform,
864 bool do_full_parse, set_parameters_fn fn)
Haynes Mathew George5bc18842014-06-16 16:36:20 -0700865{
866 XML_Parser parser;
867 FILE *file;
868 int ret = 0;
869 int bytes_read;
870 void *buf;
871 static const uint32_t kBufSize = 1024;
vivek mehtade4849c2016-03-03 17:23:38 -0800872 char platform_info_file_name[MIXER_PATH_MAX_LENGTH]= {0};
Haynes Mathew George98c95622014-06-20 19:14:25 -0700873
vivek mehtade4849c2016-03-03 17:23:38 -0800874 if (filename == NULL) {
875 strlcpy(platform_info_file_name, PLATFORM_INFO_XML_PATH, MIXER_PATH_MAX_LENGTH);
876 } else {
877 strlcpy(platform_info_file_name, filename, MIXER_PATH_MAX_LENGTH);
878 }
879
880 ALOGV("%s: platform info file name is %s", __func__, platform_info_file_name);
881
882 file = fopen(platform_info_file_name, "r");
883
Haynes Mathew George5bc18842014-06-16 16:36:20 -0700884 if (!file) {
885 ALOGD("%s: Failed to open %s, using defaults.",
vivek mehtade4849c2016-03-03 17:23:38 -0800886 __func__, platform_info_file_name);
Haynes Mathew George5bc18842014-06-16 16:36:20 -0700887 ret = -ENODEV;
888 goto done;
889 }
890
891 parser = XML_ParserCreate(NULL);
892 if (!parser) {
893 ALOGE("%s: Failed to create XML parser!", __func__);
894 ret = -ENODEV;
895 goto err_close_file;
896 }
897
juyuchenbf6571f2018-11-30 18:50:47 +0800898 pthread_mutex_lock(&my_data.lock);
899 section = ROOT;
900 my_data.do_full_parse = do_full_parse;
Ravi Kumar Alamandac4f57312015-06-26 17:41:02 -0700901 my_data.platform = platform;
902 my_data.kvpairs = str_parms_create();
juyuchenbf6571f2018-11-30 18:50:47 +0800903 my_data.set_parameters = fn;
Ravi Kumar Alamandac4f57312015-06-26 17:41:02 -0700904
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
Haynes Mathew George5bc18842014-06-16 16:36:20 -0700934err_free_parser:
juyuchenbf6571f2018-11-30 18:50:47 +0800935 if (my_data.kvpairs != NULL) {
936 str_parms_destroy(my_data.kvpairs);
937 my_data.kvpairs = NULL;
938 }
939 pthread_mutex_unlock(&my_data.lock);
Haynes Mathew George5bc18842014-06-16 16:36:20 -0700940 XML_ParserFree(parser);
941err_close_file:
942 fclose(file);
943done:
944 return ret;
945}