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 <log/log.h>
24 #include <audio_hw.h>
25 #include "platform_api.h"
26 #include <platform.h>
27 #include <math.h>
28 #include <pthread.h>
29 
30 /*
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 
38 typedef enum {
39     ROOT,
40     ACDB,
41     MODULE,
42     AEC,
43     NS,
44     PCM_ID,
45     BACKEND_NAME,
46     CONFIG_PARAMS,
47     OPERATOR_SPECIFIC,
48     GAIN_LEVEL_MAPPING,
49     APP_TYPE,
50     MICROPHONE_CHARACTERISTIC,
51     SND_DEVICES,
52     INPUT_SND_DEVICE,
53     INPUT_SND_DEVICE_TO_MIC_MAPPING,
54     SND_DEV,
55     MIC_INFO,
56     ACDB_METAINFO_KEY,
57     EXTERNAL_DEVICE_SPECIFIC,
58     AUDIO_SOURCE_DELAY,
59     AUDIO_OUTPUT_USECASE_DELAY,
60 } section_t;
61 
62 typedef void (* section_process_fn)(const XML_Char **attr);
63 
64 static void process_acdb_id(const XML_Char **attr);
65 static void process_audio_effect(const XML_Char **attr, effect_type_t effect_type);
66 static void process_effect_aec(const XML_Char **attr);
67 static void process_effect_ns(const XML_Char **attr);
68 static void process_pcm_id(const XML_Char **attr);
69 static void process_backend_name(const XML_Char **attr);
70 static void process_config_params(const XML_Char **attr);
71 static void process_root(const XML_Char **attr);
72 static void process_operator_specific(const XML_Char **attr);
73 static void process_gain_db_to_level_map(const XML_Char **attr);
74 static void process_app_type(const XML_Char **attr);
75 static void process_microphone_characteristic(const XML_Char **attr);
76 static void process_snd_dev(const XML_Char **attr);
77 static void process_mic_info(const XML_Char **attr);
78 static void process_acdb_metainfo_key(const XML_Char **attr);
79 static void process_external_dev(const XML_Char **attr);
80 static void process_audio_source_delay(const XML_Char **attr);
81 static void process_audio_usecase_delay(const XML_Char **attr);
82 
83 static section_process_fn section_table[] = {
84     [ROOT] = process_root,
85     [ACDB] = process_acdb_id,
86     [AEC] = process_effect_aec,
87     [NS] = process_effect_ns,
88     [PCM_ID] = process_pcm_id,
89     [BACKEND_NAME] = process_backend_name,
90     [CONFIG_PARAMS] = process_config_params,
91     [OPERATOR_SPECIFIC] = process_operator_specific,
92     [GAIN_LEVEL_MAPPING] = process_gain_db_to_level_map,
93     [APP_TYPE] = process_app_type,
94     [MICROPHONE_CHARACTERISTIC] = process_microphone_characteristic,
95     [SND_DEV] = process_snd_dev,
96     [MIC_INFO] = process_mic_info,
97     [ACDB_METAINFO_KEY] = process_acdb_metainfo_key,
98     [EXTERNAL_DEVICE_SPECIFIC] = process_external_dev,
99     [AUDIO_SOURCE_DELAY] = process_audio_source_delay,
100     [AUDIO_OUTPUT_USECASE_DELAY] = process_audio_usecase_delay,
101 };
102 
103 static section_t section;
104 
105 struct platform_info {
106     pthread_mutex_t   lock;
107     bool              do_full_parse;
108     void             *platform;
109     struct str_parms *kvpairs;
110     set_parameters_fn set_parameters;
111 };
112 
113 static struct platform_info my_data = {PTHREAD_MUTEX_INITIALIZER,
114                                        true, NULL, NULL,
115                                        &platform_set_parameters};
116 
117 struct audio_string_to_enum {
118     const char* name;
119     unsigned int value;
120 };
121 
122 static snd_device_t in_snd_device;
123 
124 static const struct audio_string_to_enum mic_locations[AUDIO_MICROPHONE_LOCATION_CNT] = {
125     AUDIO_MAKE_STRING_FROM_ENUM(AUDIO_MICROPHONE_LOCATION_UNKNOWN),
126     AUDIO_MAKE_STRING_FROM_ENUM(AUDIO_MICROPHONE_LOCATION_MAINBODY),
127     AUDIO_MAKE_STRING_FROM_ENUM(AUDIO_MICROPHONE_LOCATION_MAINBODY_MOVABLE),
128     AUDIO_MAKE_STRING_FROM_ENUM(AUDIO_MICROPHONE_LOCATION_PERIPHERAL),
129 };
130 
131 static const struct audio_string_to_enum mic_directionalities[AUDIO_MICROPHONE_DIRECTIONALITY_CNT] = {
132     AUDIO_MAKE_STRING_FROM_ENUM(AUDIO_MICROPHONE_DIRECTIONALITY_OMNI),
133     AUDIO_MAKE_STRING_FROM_ENUM(AUDIO_MICROPHONE_DIRECTIONALITY_BI_DIRECTIONAL),
134     AUDIO_MAKE_STRING_FROM_ENUM(AUDIO_MICROPHONE_DIRECTIONALITY_UNKNOWN),
135     AUDIO_MAKE_STRING_FROM_ENUM(AUDIO_MICROPHONE_DIRECTIONALITY_CARDIOID),
136     AUDIO_MAKE_STRING_FROM_ENUM(AUDIO_MICROPHONE_DIRECTIONALITY_HYPER_CARDIOID),
137     AUDIO_MAKE_STRING_FROM_ENUM(AUDIO_MICROPHONE_DIRECTIONALITY_SUPER_CARDIOID),
138 };
139 
140 static const struct audio_string_to_enum mic_channel_mapping[AUDIO_MICROPHONE_CHANNEL_MAPPING_CNT] = {
141     AUDIO_MAKE_STRING_FROM_ENUM(AUDIO_MICROPHONE_CHANNEL_MAPPING_UNUSED),
142     AUDIO_MAKE_STRING_FROM_ENUM(AUDIO_MICROPHONE_CHANNEL_MAPPING_DIRECT),
143     AUDIO_MAKE_STRING_FROM_ENUM(AUDIO_MICROPHONE_CHANNEL_MAPPING_PROCESSED),
144 };
145 
146 static const struct audio_string_to_enum device_in_types[] = {
147     AUDIO_MAKE_STRING_FROM_ENUM(AUDIO_DEVICE_IN_AMBIENT),
148     AUDIO_MAKE_STRING_FROM_ENUM(AUDIO_DEVICE_IN_COMMUNICATION),
149     AUDIO_MAKE_STRING_FROM_ENUM(AUDIO_DEVICE_IN_BUILTIN_MIC),
150     AUDIO_MAKE_STRING_FROM_ENUM(AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET),
151     AUDIO_MAKE_STRING_FROM_ENUM(AUDIO_DEVICE_IN_WIRED_HEADSET),
152     AUDIO_MAKE_STRING_FROM_ENUM(AUDIO_DEVICE_IN_AUX_DIGITAL),
153     AUDIO_MAKE_STRING_FROM_ENUM(AUDIO_DEVICE_IN_HDMI),
154     AUDIO_MAKE_STRING_FROM_ENUM(AUDIO_DEVICE_IN_VOICE_CALL),
155     AUDIO_MAKE_STRING_FROM_ENUM(AUDIO_DEVICE_IN_TELEPHONY_RX),
156     AUDIO_MAKE_STRING_FROM_ENUM(AUDIO_DEVICE_IN_BACK_MIC),
157     AUDIO_MAKE_STRING_FROM_ENUM(AUDIO_DEVICE_IN_REMOTE_SUBMIX),
158     AUDIO_MAKE_STRING_FROM_ENUM(AUDIO_DEVICE_IN_ANLG_DOCK_HEADSET),
159     AUDIO_MAKE_STRING_FROM_ENUM(AUDIO_DEVICE_IN_DGTL_DOCK_HEADSET),
160     AUDIO_MAKE_STRING_FROM_ENUM(AUDIO_DEVICE_IN_USB_ACCESSORY),
161     AUDIO_MAKE_STRING_FROM_ENUM(AUDIO_DEVICE_IN_USB_DEVICE),
162     AUDIO_MAKE_STRING_FROM_ENUM(AUDIO_DEVICE_IN_FM_TUNER),
163     AUDIO_MAKE_STRING_FROM_ENUM(AUDIO_DEVICE_IN_TV_TUNER),
164     AUDIO_MAKE_STRING_FROM_ENUM(AUDIO_DEVICE_IN_LINE),
165     AUDIO_MAKE_STRING_FROM_ENUM(AUDIO_DEVICE_IN_SPDIF),
166     AUDIO_MAKE_STRING_FROM_ENUM(AUDIO_DEVICE_IN_BLUETOOTH_A2DP),
167     AUDIO_MAKE_STRING_FROM_ENUM(AUDIO_DEVICE_IN_LOOPBACK),
168     AUDIO_MAKE_STRING_FROM_ENUM(AUDIO_DEVICE_IN_IP),
169     AUDIO_MAKE_STRING_FROM_ENUM(AUDIO_DEVICE_IN_BUS),
170     AUDIO_MAKE_STRING_FROM_ENUM(AUDIO_DEVICE_IN_PROXY),
171     AUDIO_MAKE_STRING_FROM_ENUM(AUDIO_DEVICE_IN_USB_HEADSET),
172     AUDIO_MAKE_STRING_FROM_ENUM(AUDIO_DEVICE_IN_BLUETOOTH_BLE),
173     AUDIO_MAKE_STRING_FROM_ENUM(AUDIO_DEVICE_IN_DEFAULT),
174 };
175 
find_enum_by_string(const struct audio_string_to_enum * table,const char * name,int32_t len,unsigned int * value)176 static bool find_enum_by_string(const struct audio_string_to_enum * table, const char * name,
177                                 int32_t len, unsigned int *value)
178 {
179     if (table == NULL) {
180         ALOGE("%s: table is NULL", __func__);
181         return false;
182     }
183 
184     if (name == NULL) {
185         ALOGE("null key");
186         return false;
187     }
188 
189     for (int i = 0; i < len; i++) {
190         if (!strcmp(table[i].name, name)) {
191             *value = table[i].value;
192             return true;
193         }
194     }
195     return false;
196 }
197 
198 /*
199  * <audio_platform_info>
200  * <acdb_ids>
201  * <device name="???" acdb_id="???"/>
202  * ...
203  * ...
204  * </acdb_ids>
205  * <module_ids>
206  * <device name="???" module_id="???"/>
207  * ...
208  * ...
209  * </module_ids>
210  * <backend_names>
211  * <device name="???" backend="???"/>
212  * ...
213  * ...
214  * </backend_names>
215  * <pcm_ids>
216  * <usecase name="???" type="in/out" id="???"/>
217  * ...
218  * ...
219  * </pcm_ids>
220  * <config_params>
221  *      <param key="snd_card_name" value="msm8994-tomtom-mtp-snd-card"/>
222  *      <param key="operator_info" value="tmus;aa;bb;cc"/>
223  *      <param key="operator_info" value="sprint;xx;yy;zz"/>
224  *      ...
225  *      ...
226  * </config_params>
227  *
228  * <operator_specific>
229  *      <device name="???" operator="???" mixer_path="???" acdb_id="???"/>
230  *      ...
231  *      ...
232  * </operator_specific>
233  *
234  * </audio_platform_info>
235  */
236 
process_root(const XML_Char ** attr __unused)237 static void process_root(const XML_Char **attr __unused)
238 {
239 }
240 
process_audio_effect(const XML_Char ** attr,effect_type_t effect_type)241 static void process_audio_effect(const XML_Char **attr, effect_type_t effect_type)
242 {
243     int index;
244     struct audio_effect_config effect_config;
245 
246     if (strncmp(attr[0], "name", strlen("name")) != 0) {
247         ALOGE("%s: 'name' not found, no MODULE ID set!", __func__);
248         goto done;
249     }
250 
251     index = platform_get_snd_device_index((char *)attr[1]);
252     if (index < 0) {
253         ALOGE("%s: Device %s in platform info xml not found, no MODULE ID set!",
254               __func__, attr[1]);
255         goto done;
256     }
257 
258     if (strncmp(attr[2], "module_id", strlen("module_id")) != 0) {
259         ALOGE("%s: Device %s in platform info xml has no module_id, no MODULE ID set!",
260               __func__, attr[2]);
261         goto done;
262     }
263 
264     if (strncmp(attr[4], "instance_id", strlen("instance_id")) != 0) {
265         ALOGE("%s: Device %s in platform info xml has no instance_id, no INSTANCE ID set!",
266               __func__, attr[4]);
267         goto done;
268     }
269 
270     if (strncmp(attr[6], "param_id", strlen("param_id")) != 0) {
271         ALOGE("%s: Device %s in platform info xml has no param_id, no PARAM ID set!",
272               __func__, attr[6]);
273         goto done;
274     }
275 
276     if (strncmp(attr[8], "param_value", strlen("param_value")) != 0) {
277         ALOGE("%s: Device %s in platform info xml has no param_value, no PARAM VALUE set!",
278               __func__, attr[8]);
279         goto done;
280     }
281 
282     effect_config = (struct audio_effect_config){strtol((char *)attr[3], NULL, 0),
283                                                  strtol((char *)attr[5], NULL, 0),
284                                                  strtol((char *)attr[7], NULL, 0),
285                                                  strtol((char *)attr[9], NULL, 0)};
286 
287 
288     if (platform_set_effect_config_data(index, effect_config, effect_type) < 0) {
289         ALOGE("%s: Effect = %d Device %s, MODULE/INSTANCE/PARAM ID %u %u %u %u was not set!",
290               __func__, effect_type, attr[1], effect_config.module_id,
291               effect_config.instance_id, effect_config.param_id,
292               effect_config.param_value);
293         goto done;
294     }
295 
296 done:
297     return;
298 }
299 
process_effect_aec(const XML_Char ** attr)300 static void process_effect_aec(const XML_Char **attr)
301 {
302     process_audio_effect(attr, EFFECT_AEC);
303     return;
304 }
305 
process_effect_ns(const XML_Char ** attr)306 static void process_effect_ns(const XML_Char **attr)
307 {
308     process_audio_effect(attr, EFFECT_NS);
309     return;
310 }
311 
312 /* mapping from usecase to pcm dev id */
process_pcm_id(const XML_Char ** attr)313 static void process_pcm_id(const XML_Char **attr)
314 {
315     int index;
316 
317     if (strcmp(attr[0], "name") != 0) {
318         ALOGE("%s: 'name' not found, no pcm_id set!", __func__);
319         goto done;
320     }
321 
322     index = platform_get_usecase_index((char *)attr[1]);
323     if (index < 0) {
324         ALOGE("%s: usecase %s in %s not found!",
325               __func__, attr[1], PLATFORM_INFO_XML_PATH);
326         goto done;
327     }
328 
329     if (strcmp(attr[2], "type") != 0) {
330         ALOGE("%s: usecase type not mentioned", __func__);
331         goto done;
332     }
333 
334     int type = -1;
335 
336     if (!strcasecmp((char *)attr[3], "in")) {
337         type = 1;
338     } else if (!strcasecmp((char *)attr[3], "out")) {
339         type = 0;
340     } else {
341         ALOGE("%s: type must be IN or OUT", __func__);
342         goto done;
343     }
344 
345     if (strcmp(attr[4], "id") != 0) {
346         ALOGE("%s: usecase id not mentioned", __func__);
347         goto done;
348     }
349 
350     int id = atoi((char *)attr[5]);
351 
352     if (platform_set_usecase_pcm_id(index, type, id) < 0) {
353         ALOGE("%s: usecase %s in %s, type %d id %d was not set!",
354               __func__, attr[1], PLATFORM_INFO_XML_PATH, type, id);
355         goto done;
356     }
357 
358 done:
359     return;
360 }
361 
362 /* backend to be used for a device */
process_backend_name(const XML_Char ** attr)363 static void process_backend_name(const XML_Char **attr)
364 {
365     int index;
366     char *hw_interface = NULL;
367 
368     if (strcmp(attr[0], "name") != 0) {
369         ALOGE("%s: 'name' not found, no ACDB ID set!", __func__);
370         goto done;
371     }
372 
373     index = platform_get_snd_device_index((char *)attr[1]);
374     if (index < 0) {
375         ALOGE("%s: Device %s in %s not found, no ACDB ID set!",
376               __func__, attr[1], PLATFORM_INFO_XML_PATH);
377         goto done;
378     }
379 
380     if (strcmp(attr[2], "backend") != 0) {
381         ALOGE("%s: Device %s in %s has no backed set!",
382               __func__, attr[1], PLATFORM_INFO_XML_PATH);
383         goto done;
384     }
385 
386     if (attr[4] != NULL) {
387         if (strcmp(attr[4], "interface") != 0) {
388             hw_interface = NULL;
389         } else {
390             hw_interface = (char *)attr[5];
391         }
392     }
393 
394     if (platform_set_snd_device_backend(index, attr[3], hw_interface) < 0) {
395         ALOGE("%s: Device %s in %s, backend %s was not set!",
396               __func__, attr[1], PLATFORM_INFO_XML_PATH, attr[3]);
397         goto done;
398     }
399 
400 done:
401     return;
402 }
403 
process_gain_db_to_level_map(const XML_Char ** attr)404 static void process_gain_db_to_level_map(const XML_Char **attr)
405 {
406     struct amp_db_and_gain_table tbl_entry;
407 
408     if ((strcmp(attr[0], "db") != 0) ||
409         (strcmp(attr[2], "level") != 0)) {
410         ALOGE("%s: invalid attribute passed  %s %sexpected amp db level",
411                __func__, attr[0], attr[2]);
412         goto done;
413     }
414 
415     tbl_entry.db = atof(attr[1]);
416     tbl_entry.amp = exp(tbl_entry.db * 0.115129f);
417     tbl_entry.level = atoi(attr[3]);
418 
419     //custome level should be > 0. Level 0 is fixed for default
420     CHECK(tbl_entry.level > 0);
421 
422     ALOGV("%s: amp [%f]  db [%f] level [%d]", __func__,
423            tbl_entry.amp, tbl_entry.db, tbl_entry.level);
424     platform_add_gain_level_mapping(&tbl_entry);
425 
426 done:
427     return;
428 }
429 
process_acdb_id(const XML_Char ** attr)430 static void process_acdb_id(const XML_Char **attr)
431 {
432     int index;
433 
434     if (strcmp(attr[0], "name") != 0) {
435         ALOGE("%s: 'name' not found, no ACDB ID set!", __func__);
436         goto done;
437     }
438 
439     index = platform_get_snd_device_index((char *)attr[1]);
440     if (index < 0) {
441         ALOGE("%s: Device %s in %s not found, no ACDB ID set!",
442               __func__, attr[1], PLATFORM_INFO_XML_PATH);
443         goto done;
444     }
445 
446     if (strcmp(attr[2], "acdb_id") != 0) {
447         ALOGE("%s: Device %s in %s has no acdb_id, no ACDB ID set!",
448               __func__, attr[1], PLATFORM_INFO_XML_PATH);
449         goto done;
450     }
451 
452     if (platform_set_snd_device_acdb_id(index, atoi((char *)attr[3])) < 0) {
453         ALOGE("%s: Device %s in %s, ACDB ID %d was not set!",
454               __func__, attr[1], PLATFORM_INFO_XML_PATH, atoi((char *)attr[3]));
455         goto done;
456     }
457 
458 done:
459     return;
460 }
461 
462 
process_operator_specific(const XML_Char ** attr)463 static void process_operator_specific(const XML_Char **attr)
464 {
465     snd_device_t snd_device = SND_DEVICE_NONE;
466 
467     if (strcmp(attr[0], "name") != 0) {
468         ALOGE("%s: 'name' not found", __func__);
469         goto done;
470     }
471 
472     snd_device = platform_get_snd_device_index((char *)attr[1]);
473     if (snd_device < 0) {
474         ALOGE("%s: Device %s in %s not found, no ACDB ID set!",
475               __func__, (char *)attr[3], PLATFORM_INFO_XML_PATH);
476         goto done;
477     }
478 
479     if (strcmp(attr[2], "operator") != 0) {
480         ALOGE("%s: 'operator' not found", __func__);
481         goto done;
482     }
483 
484     if (strcmp(attr[4], "mixer_path") != 0) {
485         ALOGE("%s: 'mixer_path' not found", __func__);
486         goto done;
487     }
488 
489     if (strcmp(attr[6], "acdb_id") != 0) {
490         ALOGE("%s: 'acdb_id' not found", __func__);
491         goto done;
492     }
493 
494     platform_add_operator_specific_device(snd_device, (char *)attr[3], (char *)attr[5], atoi((char *)attr[7]));
495 
496 done:
497     return;
498 }
499 
process_external_dev(const XML_Char ** attr)500 static void process_external_dev(const XML_Char **attr)
501 {
502     snd_device_t snd_device = SND_DEVICE_NONE;
503 
504     if (strcmp(attr[0], "name") != 0) {
505         ALOGE("%s: 'name' not found", __func__);
506         goto done;
507     }
508 
509     snd_device = platform_get_snd_device_index((char *)attr[1]);
510     if (snd_device < 0) {
511         ALOGE("%s: Device %s in %s not found, no ACDB ID set!",
512               __func__, (char *)attr[3], PLATFORM_INFO_XML_PATH);
513         goto done;
514     }
515 
516     if (strcmp(attr[2], "usbid") != 0) {
517         ALOGE("%s: 'usbid' not found", __func__);
518         goto done;
519     }
520 
521     if (strcmp(attr[4], "acdb_id") != 0) {
522         ALOGE("%s: 'acdb_id' not found", __func__);
523         goto done;
524     }
525 
526     platform_add_external_specific_device(snd_device, (char *)attr[3], atoi((char *)attr[5]));
527 
528 done:
529     return;
530 }
531 
process_audio_source_delay(const XML_Char ** attr)532 static void process_audio_source_delay(const XML_Char **attr)
533 {
534     audio_source_t audio_source = -1;
535 
536     if (strcmp(attr[0], "name") != 0) {
537         ALOGE("%s: 'name' not found", __func__);
538         goto done;
539     }
540 
541     audio_source = platform_get_audio_source_index((const char *)attr[1]);
542 
543     if (audio_source < 0) {
544         ALOGE("%s: audio_source %s is not defined",
545               __func__, (char *)attr[1]);
546         goto done;
547     }
548 
549     if (strcmp(attr[2], "delay") != 0) {
550         ALOGE("%s: 'delay' not found", __func__);
551         goto done;
552     }
553 
554     platform_set_audio_source_delay(audio_source, atoi((char *)attr[3]));
555 
556 done:
557     return;
558 }
559 
process_audio_usecase_delay(const XML_Char ** attr)560 static void process_audio_usecase_delay(const XML_Char **attr)
561 {
562     int index;
563 
564     if (strcmp(attr[0], "name") != 0) {
565         ALOGE("%s: 'name' not found", __func__);
566         goto done;
567     }
568 
569     index = platform_get_usecase_index((char *)attr[1]);
570     if (index < 0) {
571         ALOGE("%s: usecase %s in %s not found!",
572               __func__, attr[1], PLATFORM_INFO_XML_PATH);
573         goto done;
574     }
575 
576     if (strcmp(attr[2], "delay") != 0) {
577         ALOGE("%s: 'delay' not found", __func__);
578         goto done;
579     }
580 
581     platform_set_audio_usecase_delay(index, atoi((char *)attr[3]));
582 
583 done:
584     return;
585 }
586 
587 /* platform specific configuration key-value pairs */
process_config_params(const XML_Char ** attr)588 static void process_config_params(const XML_Char **attr)
589 {
590     if (strcmp(attr[0], "key") != 0) {
591         ALOGE("%s: 'key' not found", __func__);
592         goto done;
593     }
594 
595     if (strcmp(attr[2], "value") != 0) {
596         ALOGE("%s: 'value' not found", __func__);
597         goto done;
598     }
599 
600     str_parms_add_str(my_data.kvpairs, (char*)attr[1], (char*)attr[3]);
601     my_data.set_parameters(my_data.platform, my_data.kvpairs);
602 done:
603     return;
604 }
605 
process_app_type(const XML_Char ** attr)606 static void process_app_type(const XML_Char **attr)
607 {
608     if (strcmp(attr[0], "uc_type")) {
609         ALOGE("%s: uc_type not found", __func__);
610         goto done;
611     }
612 
613     if (strcmp(attr[2], "mode")) {
614         ALOGE("%s: mode not found", __func__);
615         goto done;
616     }
617 
618     if (strcmp(attr[4], "bit_width")) {
619         ALOGE("%s: bit_width not found", __func__);
620         goto done;
621     }
622 
623     if (strcmp(attr[6], "id")) {
624         ALOGE("%s: id not found", __func__);
625         goto done;
626     }
627 
628     if (strcmp(attr[8], "max_rate")) {
629         ALOGE("%s: max rate not found", __func__);
630         goto done;
631     }
632 
633     platform_add_app_type(attr[1], attr[3], atoi(attr[5]), atoi(attr[7]),
634                           atoi(attr[9]));
635 done:
636     return;
637 }
638 
process_microphone_characteristic(const XML_Char ** attr)639 static void process_microphone_characteristic(const XML_Char **attr) {
640     struct audio_microphone_characteristic_t microphone;
641     uint32_t index = 0;
642     uint32_t found_mandatory_characteristics = 0;
643     uint32_t num_frequencies = 0;
644     uint32_t num_responses = 0;
645     microphone.sensitivity = AUDIO_MICROPHONE_SENSITIVITY_UNKNOWN;
646     microphone.max_spl = AUDIO_MICROPHONE_SPL_UNKNOWN;
647     microphone.min_spl = AUDIO_MICROPHONE_SPL_UNKNOWN;
648     microphone.orientation.x = 0.0f;
649     microphone.orientation.y = 0.0f;
650     microphone.orientation.z = 0.0f;
651     microphone.geometric_location.x = AUDIO_MICROPHONE_COORDINATE_UNKNOWN;
652     microphone.geometric_location.y = AUDIO_MICROPHONE_COORDINATE_UNKNOWN;
653     microphone.geometric_location.z = AUDIO_MICROPHONE_COORDINATE_UNKNOWN;
654 
655     while (attr[index] != NULL) {
656         const char *attribute = attr[index++];
657         char value[strlen(attr[index]) + 1];
658         strcpy(value, attr[index++]);
659         if (strcmp(attribute, "device_id") == 0) {
660             if (strlen(value) > AUDIO_MICROPHONE_ID_MAX_LEN) {
661                 ALOGE("%s: device_id %s is too long", __func__, value);
662                 goto done;
663             }
664             strcpy(microphone.device_id, value);
665             found_mandatory_characteristics |= 1;
666         } else if (strcmp(attribute, "type") == 0) {
667             if (!find_enum_by_string(device_in_types, value,
668                     ARRAY_SIZE(device_in_types), &microphone.device)) {
669                 ALOGE("%s: type %s in %s not found!",
670                         __func__, value, PLATFORM_INFO_XML_PATH);
671                 goto done;
672             }
673             found_mandatory_characteristics |= (1 << 1);
674         } else if (strcmp(attribute, "address") == 0) {
675             if (strlen(value) > AUDIO_DEVICE_MAX_ADDRESS_LEN) {
676                 ALOGE("%s, address %s is too long", __func__, value);
677                 goto done;
678             }
679             strcpy(microphone.address, value);
680             if (strlen(microphone.address) == 0) {
681                 // If the address is empty, populate the address according to device type.
682                 if (microphone.device == AUDIO_DEVICE_IN_BUILTIN_MIC) {
683                     strcpy(microphone.address, AUDIO_BOTTOM_MICROPHONE_ADDRESS);
684                 } else if (microphone.device == AUDIO_DEVICE_IN_BACK_MIC) {
685                     strcpy(microphone.address, AUDIO_BACK_MICROPHONE_ADDRESS);
686                 }
687             }
688             found_mandatory_characteristics |= (1 << 2);
689         } else if (strcmp(attribute, "location") == 0) {
690             if (!find_enum_by_string(mic_locations, value,
691                     AUDIO_MICROPHONE_LOCATION_CNT, &microphone.location)) {
692                 ALOGE("%s: location %s in %s not found!",
693                         __func__, value, PLATFORM_INFO_XML_PATH);
694                 goto done;
695             }
696             found_mandatory_characteristics |= (1 << 3);
697         } else if (strcmp(attribute, "group") == 0) {
698             microphone.group = atoi(value);
699             found_mandatory_characteristics |= (1 << 4);
700         } else if (strcmp(attribute, "index_in_the_group") == 0) {
701             microphone.index_in_the_group = atoi(value);
702             found_mandatory_characteristics |= (1 << 5);
703         } else if (strcmp(attribute, "directionality") == 0) {
704             if (!find_enum_by_string(mic_directionalities, value,
705                     AUDIO_MICROPHONE_DIRECTIONALITY_CNT, &microphone.directionality)) {
706                 ALOGE("%s: directionality %s in %s not found!",
707                       __func__, attr[index], PLATFORM_INFO_XML_PATH);
708                 goto done;
709             }
710             found_mandatory_characteristics |= (1 << 6);
711         } else if (strcmp(attribute, "num_frequency_responses") == 0) {
712             microphone.num_frequency_responses = atoi(value);
713             if (microphone.num_frequency_responses > AUDIO_MICROPHONE_MAX_FREQUENCY_RESPONSES) {
714                 ALOGE("%s: num_frequency_responses is too large", __func__);
715                 goto done;
716             }
717             found_mandatory_characteristics |= (1 << 7);
718         } else if (strcmp(attribute, "frequencies") == 0) {
719             char *token = strtok(value, " ");
720             while (token) {
721                 microphone.frequency_responses[0][num_frequencies++] = atof(token);
722                 if (num_frequencies > AUDIO_MICROPHONE_MAX_FREQUENCY_RESPONSES) {
723                     ALOGE("%s: num %u of frequency is too large", __func__, num_frequencies);
724                     goto done;
725                 }
726                 token = strtok(NULL, " ");
727             }
728             found_mandatory_characteristics |= (1 << 8);
729         } else if (strcmp(attribute, "responses") == 0) {
730             char *token = strtok(value, " ");
731             while (token) {
732                 microphone.frequency_responses[1][num_responses++] = atof(token);
733                 if (num_responses > AUDIO_MICROPHONE_MAX_FREQUENCY_RESPONSES) {
734                     ALOGE("%s: num %u of response is too large", __func__, num_responses);
735                     goto done;
736                 }
737                 token = strtok(NULL, " ");
738             }
739             found_mandatory_characteristics |= (1 << 9);
740         } else if (strcmp(attribute, "sensitivity") == 0) {
741             microphone.sensitivity = atof(value);
742         } else if (strcmp(attribute, "max_spl") == 0) {
743             microphone.max_spl = atof(value);
744         } else if (strcmp(attribute, "min_spl") == 0) {
745             microphone.min_spl = atof(value);
746         } else if (strcmp(attribute, "orientation") == 0) {
747             char *token = strtok(value, " ");
748             float orientation[3];
749             uint32_t idx = 0;
750             while (token) {
751                 orientation[idx++] = atof(token);
752                 if (idx > 3) {
753                     ALOGE("%s: orientation invalid", __func__);
754                     goto done;
755                 }
756                 token = strtok(NULL, " ");
757             }
758             if (idx != 3) {
759                 ALOGE("%s: orientation invalid", __func__);
760                 goto done;
761             }
762             microphone.orientation.x = orientation[0];
763             microphone.orientation.y = orientation[1];
764             microphone.orientation.z = orientation[2];
765         } else if (strcmp(attribute, "geometric_location") == 0) {
766             char *token = strtok(value, " ");
767             float geometric_location[3];
768             uint32_t idx = 0;
769             while (token) {
770                 geometric_location[idx++] = atof(token);
771                 if (idx > 3) {
772                     ALOGE("%s: geometric_location invalid", __func__);
773                     goto done;
774                 }
775                 token = strtok(NULL, " ");
776             }
777             if (idx != 3) {
778                 ALOGE("%s: geometric_location invalid", __func__);
779                 goto done;
780             }
781             microphone.geometric_location.x = geometric_location[0];
782             microphone.geometric_location.y = geometric_location[1];
783             microphone.geometric_location.z = geometric_location[2];
784         } else {
785             ALOGW("%s: unknown attribute of microphone characteristics: %s",
786                     __func__, attribute);
787         }
788     }
789 
790     if (num_frequencies != num_responses
791             || num_frequencies != microphone.num_frequency_responses) {
792         ALOGE("%s: num of frequency and response not match: %u, %u, %u",
793               __func__, num_frequencies, num_responses, microphone.num_frequency_responses);
794         goto done;
795     }
796 
797     if (found_mandatory_characteristics != MANDATORY_MICROPHONE_CHARACTERISTICS) {
798         ALOGE("%s: some of mandatory microphone characteriscts are missed: %u",
799                 __func__, found_mandatory_characteristics);
800     }
801 
802     platform_set_microphone_characteristic(my_data.platform, microphone);
803 done:
804     return;
805 }
806 
process_snd_dev(const XML_Char ** attr)807 static void process_snd_dev(const XML_Char **attr)
808 {
809     uint32_t curIdx = 0;
810     in_snd_device = SND_DEVICE_NONE;
811 
812     if (strcmp(attr[curIdx++], "in_snd_device")) {
813         ALOGE("%s: snd_device not found", __func__);
814         return;
815     }
816     in_snd_device = platform_get_snd_device_index((char *)attr[curIdx++]);
817     if (in_snd_device < SND_DEVICE_IN_BEGIN ||
818             in_snd_device >= SND_DEVICE_IN_END) {
819         ALOGE("%s: Sound device not valid", __func__);
820         in_snd_device = SND_DEVICE_NONE;
821     }
822 
823     return;
824 }
825 
process_mic_info(const XML_Char ** attr)826 static void process_mic_info(const XML_Char **attr)
827 {
828     uint32_t curIdx = 0;
829     struct mic_info microphone;
830 
831     memset(&microphone.channel_mapping, AUDIO_MICROPHONE_CHANNEL_MAPPING_UNUSED,
832                sizeof(microphone.channel_mapping));
833 
834     if (strcmp(attr[curIdx++], "mic_device_id")) {
835         ALOGE("%s: mic_device_id not found", __func__);
836         goto on_error;
837     }
838     strlcpy(microphone.device_id,
839                 (char *)attr[curIdx++], AUDIO_MICROPHONE_ID_MAX_LEN);
840 
841     if (strcmp(attr[curIdx++], "channel_mapping")) {
842         ALOGE("%s: channel_mapping not found", __func__);
843         goto on_error;
844     }
845     const char *token = strtok((char *)attr[curIdx++], " ");
846     uint32_t idx = 0;
847     while (token) {
848         if (!find_enum_by_string(mic_channel_mapping, token,
849                 AUDIO_MICROPHONE_CHANNEL_MAPPING_CNT,
850                 &microphone.channel_mapping[idx++])) {
851             ALOGE("%s: channel_mapping %s in %s not found!",
852                       __func__, attr[--curIdx], PLATFORM_INFO_XML_PATH);
853             goto on_error;
854         }
855         token = strtok(NULL, " ");
856     }
857     microphone.channel_count = idx;
858 
859     platform_set_microphone_map(my_data.platform, in_snd_device,
860                                     &microphone);
861     return;
862 on_error:
863     in_snd_device = SND_DEVICE_NONE;
864     return;
865 }
866 
867 /* process acdb meta info key value */
process_acdb_metainfo_key(const XML_Char ** attr)868 static void process_acdb_metainfo_key(const XML_Char **attr)
869 {
870     if (strcmp(attr[0], "name") != 0) {
871         ALOGE("%s: 'name' not found", __func__);
872         goto done;
873     }
874     if (strcmp(attr[2], "value") != 0) {
875         ALOGE("%s: 'value' not found", __func__);
876         goto done;
877     }
878 
879     int key = atoi((char *)attr[3]);
880     if (platform_set_acdb_metainfo_key(my_data.platform,
881                                        (char*)attr[1], key) < 0) {
882         ALOGE("%s: key %d was not set!", __func__, key);
883     }
884 
885 done:
886     return;
887 }
888 
start_tag(void * userdata __unused,const XML_Char * tag_name,const XML_Char ** attr)889 static void start_tag(void *userdata __unused, const XML_Char *tag_name,
890                       const XML_Char **attr)
891 {
892     const XML_Char              *attr_name = NULL;
893     const XML_Char              *attr_value = NULL;
894     unsigned int                i;
895 
896 
897     if (my_data.do_full_parse) {
898         if (strcmp(tag_name, "acdb_ids") == 0) {
899             section = ACDB;
900         } else if (strncmp(tag_name, "module_ids", strlen("module_ids")) == 0) {
901             section = MODULE;
902         } else if (strcmp(tag_name, "pcm_ids") == 0) {
903             section = PCM_ID;
904         } else if (strcmp(tag_name, "backend_names") == 0) {
905             section = BACKEND_NAME;
906         } else if (strcmp(tag_name, "config_params") == 0) {
907             section = CONFIG_PARAMS;
908         } else if (strcmp(tag_name, "operator_specific") == 0) {
909             section = OPERATOR_SPECIFIC;
910         } else if (strcmp(tag_name, "gain_db_to_level_mapping") == 0) {
911             section = GAIN_LEVEL_MAPPING;
912         } else if (strcmp(tag_name, "app_types") == 0) {
913             section = APP_TYPE;
914         } else if (strcmp(tag_name, "microphone_characteristics") == 0) {
915             section = MICROPHONE_CHARACTERISTIC;
916         } else if (strcmp(tag_name, "snd_devices") == 0) {
917             section = SND_DEVICES;
918         } else if(strcmp(tag_name, "acdb_metainfo_key") == 0) {
919             section = ACDB_METAINFO_KEY;
920         } else if (strcmp(tag_name, "device") == 0) {
921             if ((section != ACDB) && (section != AEC) && (section != NS) &&
922                 (section != BACKEND_NAME) && (section != OPERATOR_SPECIFIC)) {
923                 ALOGE("device tag only supported for acdb/backend/aec/ns/operator_specific names");
924                 return;
925             }
926 
927             /* call into process function for the current section */
928             section_process_fn fn = section_table[section];
929             fn(attr);
930         } else if (strcmp(tag_name, "usecase") == 0) {
931             if (section != PCM_ID) {
932                 ALOGE("usecase tag only supported with PCM_ID section");
933                 return;
934             }
935 
936             section_process_fn fn = section_table[PCM_ID];
937             fn(attr);
938         } else if (strcmp(tag_name, "param") == 0) {
939             if ((section != CONFIG_PARAMS) && (section != ACDB_METAINFO_KEY)) {
940                 ALOGE("param tag only supported with CONFIG_PARAMS section");
941                 return;
942             }
943 
944             section_process_fn fn = section_table[section];
945             fn(attr);
946         } else if (strcmp(tag_name, "gain_level_map") == 0) {
947             if (section != GAIN_LEVEL_MAPPING) {
948                 ALOGE("gain_level_map tag only supported with GAIN_LEVEL_MAPPING section");
949                 return;
950             }
951 
952             section_process_fn fn = section_table[GAIN_LEVEL_MAPPING];
953             fn(attr);
954         } else if (!strcmp(tag_name, "app")) {
955             if (section != APP_TYPE) {
956                 ALOGE("app tag only valid in section APP_TYPE");
957                 return;
958             }
959 
960             section_process_fn fn = section_table[APP_TYPE];
961             fn(attr);
962         } else if (strcmp(tag_name, "microphone") == 0) {
963             if (section != MICROPHONE_CHARACTERISTIC) {
964                 ALOGE("microphone tag only supported with MICROPHONE_CHARACTERISTIC section");
965                 return;
966             }
967             section_process_fn fn = section_table[MICROPHONE_CHARACTERISTIC];
968             fn(attr);
969         } else if (strcmp(tag_name, "input_snd_device") == 0) {
970             if (section != SND_DEVICES) {
971                 ALOGE("input_snd_device tag only supported with SND_DEVICES section");
972                 return;
973             }
974             section = INPUT_SND_DEVICE;
975         } else if (strcmp(tag_name, "input_snd_device_mic_mapping") == 0) {
976             if (section != INPUT_SND_DEVICE) {
977                 ALOGE("input_snd_device_mic_mapping tag only supported with INPUT_SND_DEVICE section");
978                 return;
979             }
980             section = INPUT_SND_DEVICE_TO_MIC_MAPPING;
981         } else if (strcmp(tag_name, "snd_dev") == 0) {
982             if (section != INPUT_SND_DEVICE_TO_MIC_MAPPING) {
983                 ALOGE("snd_dev tag only supported with INPUT_SND_DEVICE_TO_MIC_MAPPING section");
984                 return;
985             }
986             section_process_fn fn = section_table[SND_DEV];
987             fn(attr);
988         } else if (strcmp(tag_name, "mic_info") == 0) {
989             if (section != INPUT_SND_DEVICE_TO_MIC_MAPPING) {
990                 ALOGE("mic_info tag only supported with INPUT_SND_DEVICE_TO_MIC_MAPPING section");
991                 return;
992             }
993             if (in_snd_device == SND_DEVICE_NONE) {
994                 ALOGE("%s: Error in previous tags, do not process mic info", __func__);
995                 return;
996             }
997             section_process_fn fn = section_table[MIC_INFO];
998             fn(attr);
999         } else if (strcmp(tag_name, "external_specific_dev") == 0) {
1000             section = EXTERNAL_DEVICE_SPECIFIC;
1001         } else if (strcmp(tag_name, "ext_device") == 0) {
1002             section_process_fn fn = section_table[section];
1003             fn(attr);
1004         }
1005         else if (strncmp(tag_name, "aec", strlen("aec")) == 0) {
1006             if (section != MODULE) {
1007                 ALOGE("aec tag only supported with MODULE section");
1008                 return;
1009             }
1010             section = AEC;
1011         }
1012         else if (strncmp(tag_name, "ns", strlen("ns")) == 0) {
1013             if (section != MODULE) {
1014                 ALOGE("ns tag only supported with MODULE section");
1015                 return;
1016             }
1017             section = NS;
1018         } else if (strcmp(tag_name, "audio_input_source_delay") == 0) {
1019             section = AUDIO_SOURCE_DELAY;
1020         } else if (strcmp(tag_name, "audio_source_delay") == 0) {
1021             section_process_fn fn = section_table[section];
1022             fn(attr);
1023         } else if (strcmp(tag_name, "audio_output_usecase_delay") == 0) {
1024             section = AUDIO_OUTPUT_USECASE_DELAY;
1025         } else if (strcmp(tag_name, "audio_usecase_delay") == 0) {
1026             section_process_fn fn = section_table[section];
1027             fn(attr);
1028         }
1029     } else {
1030         if(strcmp(tag_name, "config_params") == 0) {
1031             section = CONFIG_PARAMS;
1032         } else if (strcmp(tag_name, "param") == 0) {
1033             if (section != CONFIG_PARAMS) {
1034                 ALOGE("param tag only supported with CONFIG_PARAMS section");
1035                 return;
1036             }
1037 
1038             section_process_fn fn = section_table[section];
1039             fn(attr);
1040         }
1041     }
1042 
1043     return;
1044 }
1045 
end_tag(void * userdata __unused,const XML_Char * tag_name)1046 static void end_tag(void *userdata __unused, const XML_Char *tag_name)
1047 {
1048     if (strcmp(tag_name, "acdb_ids") == 0) {
1049         section = ROOT;
1050     } else if (strncmp(tag_name, "module_ids", strlen("module_ids")) == 0) {
1051         section = ROOT;
1052     } else if (strncmp(tag_name, "aec", strlen("aec")) == 0) {
1053         section = MODULE;
1054     } else if (strncmp(tag_name, "ns", strlen("ns")) == 0) {
1055         section = MODULE;
1056     } else if (strcmp(tag_name, "pcm_ids") == 0) {
1057         section = ROOT;
1058     } else if (strcmp(tag_name, "backend_names") == 0) {
1059         section = ROOT;
1060     } else if (strcmp(tag_name, "config_params") == 0) {
1061         section = ROOT;
1062     } else if (strcmp(tag_name, "operator_specific") == 0) {
1063         section = ROOT;
1064     } else if (strcmp(tag_name, "gain_db_to_level_mapping") == 0) {
1065         section = ROOT;
1066     } else if (strcmp(tag_name, "app_types") == 0) {
1067         section = ROOT;
1068     } else if (strcmp(tag_name, "microphone_characteristics") == 0) {
1069         section = ROOT;
1070     } else if (strcmp(tag_name, "snd_devices") == 0) {
1071         section = ROOT;
1072     } else if (strcmp(tag_name, "external_specific_dev") == 0) {
1073         section = ROOT;
1074     } else if (strcmp(tag_name, "input_snd_device") == 0) {
1075         section = SND_DEVICES;
1076     } else if (strcmp(tag_name, "input_snd_device_mic_mapping") == 0) {
1077         section = INPUT_SND_DEVICE;
1078     } else if (strcmp(tag_name, "acdb_metainfo_key") == 0) {
1079         section = ROOT;
1080     }
1081 }
1082 
platform_info_init(const char * filename,void * platform,bool do_full_parse,set_parameters_fn fn)1083 int platform_info_init(const char *filename, void *platform,
1084                        bool do_full_parse, set_parameters_fn fn)
1085 {
1086     XML_Parser      parser;
1087     FILE            *file;
1088     int             ret = 0;
1089     int             bytes_read;
1090     void            *buf;
1091     static const uint32_t kBufSize = 1024;
1092     char   platform_info_file_name[MIXER_PATH_MAX_LENGTH]= {0};
1093 
1094     if (filename == NULL) {
1095         strlcpy(platform_info_file_name, PLATFORM_INFO_XML_PATH, MIXER_PATH_MAX_LENGTH);
1096     } else {
1097         strlcpy(platform_info_file_name, filename, MIXER_PATH_MAX_LENGTH);
1098     }
1099 
1100     ALOGV("%s: platform info file name is %s", __func__, platform_info_file_name);
1101 
1102     file = fopen(platform_info_file_name, "r");
1103 
1104     if (!file) {
1105         ALOGD("%s: Failed to open %s, using defaults.",
1106             __func__, platform_info_file_name);
1107         ret = -ENODEV;
1108         goto done;
1109     }
1110 
1111     parser = XML_ParserCreate(NULL);
1112     if (!parser) {
1113         ALOGE("%s: Failed to create XML parser!", __func__);
1114         ret = -ENODEV;
1115         goto err_close_file;
1116     }
1117 
1118     pthread_mutex_lock(&my_data.lock);
1119     section = ROOT;
1120     my_data.do_full_parse = do_full_parse;
1121     my_data.platform = platform;
1122     my_data.kvpairs = str_parms_create();
1123     my_data.set_parameters = fn;
1124 
1125     XML_SetElementHandler(parser, start_tag, end_tag);
1126 
1127     while (1) {
1128         buf = XML_GetBuffer(parser, kBufSize);
1129         if (buf == NULL) {
1130             ALOGE("%s: XML_GetBuffer failed", __func__);
1131             ret = -ENOMEM;
1132             goto err_free_parser;
1133         }
1134 
1135         bytes_read = fread(buf, 1, kBufSize, file);
1136         if (bytes_read < 0) {
1137             ALOGE("%s: fread failed, bytes read = %d", __func__, bytes_read);
1138              ret = bytes_read;
1139             goto err_free_parser;
1140         }
1141 
1142         if (XML_ParseBuffer(parser, bytes_read,
1143                             bytes_read == 0) == XML_STATUS_ERROR) {
1144             ALOGE("%s: XML_ParseBuffer failed, for %s",
1145                 __func__, platform_info_file_name);
1146             ret = -EINVAL;
1147             goto err_free_parser;
1148         }
1149 
1150         if (bytes_read == 0)
1151             break;
1152     }
1153 
1154 err_free_parser:
1155     if (my_data.kvpairs != NULL) {
1156         str_parms_destroy(my_data.kvpairs);
1157         my_data.kvpairs = NULL;
1158     }
1159     pthread_mutex_unlock(&my_data.lock);
1160     XML_ParserFree(parser);
1161 err_close_file:
1162     fclose(file);
1163 done:
1164     return ret;
1165 }
1166