1 /* Copyright 2018 The Chromium OS Authors. All rights reserved.
2  * Use of this source code is governed by a BSD-style license that can be
3  * found in the LICENSE file.
4  */
5 
6 #include <syslog.h>
7 
8 #include "apm_config.h"
9 #include "iniparser_wrapper.h"
10 
11 static const unsigned int MAX_INI_NAME_LEN = 63;
12 
13 #define APM_CONFIG_NAME "apm.ini"
14 
15 #define APM_GET_INT(ini, key)	\
16 	iniparser_getint(	\
17 		ini, key,	\
18 		key ## _VALUE)
19 #define APM_GET_FLOAT(ini, key)	\
20 	iniparser_getdouble(	\
21 		ini, key,	\
22 		key ## _VALUE)
23 
apm_config_get(const char * device_config_dir)24 struct apm_config *apm_config_get(const char *device_config_dir)
25 {
26 	struct apm_config *config;
27 	char ini_name[MAX_INI_NAME_LEN + 1];
28 	dictionary *ini;
29 
30 	snprintf(ini_name, MAX_INI_NAME_LEN, "%s/%s",
31 		 device_config_dir, APM_CONFIG_NAME);
32 	ini_name[MAX_INI_NAME_LEN] = '\0';
33 
34 	ini = iniparser_load_wrapper(ini_name);
35 	if (ini == NULL) {
36 		syslog(LOG_DEBUG, "No ini file %s", ini_name);
37 		return NULL;
38 	}
39 
40 	config = (struct apm_config *)calloc(1, sizeof(*config));
41 	config->residual_echo_detector_enabled =
42 			APM_GET_INT(ini, APM_RESIDUAL_ECHO_DETECTOR_ENABLED);
43 	config->high_pass_filter_enabled =
44 			APM_GET_INT(ini, APM_HIGH_PASS_FILTER_ENABLED);
45 	config->pre_amplifier_enabled =
46 			APM_GET_INT(ini, APM_PRE_AMPLIFIER_ENABLED);
47 	config->pre_amplifier_fixed_gain_factor =
48 			APM_GET_FLOAT(ini, APM_PRE_AMPLIFIER_FIXED_GAIN_FACTOR);
49 	config->gain_controller2_enabled =
50 			APM_GET_INT(ini, APM_GAIN_CONTROLLER2_ENABLED);
51 	config->gain_controller2_fixed_gain_db =
52 			APM_GET_FLOAT(ini, APM_GAIN_CONTROLLER2_FIXED_GAIN_DB);
53 	config->gain_controller2_adaptive_digital_mode =
54 			APM_GET_INT(ini, APM_GAIN_CONTROLLER2_ADAPTIVE_DIGITAL_MODE);
55 	config->gain_control_compression_gain_db =
56 			APM_GET_INT(ini, APM_GAIN_CONTROL_COMPRESSION_GAIN_DB);
57 	config->agc_mode = (enum gain_control_mode)
58 			APM_GET_INT(ini, APM_GAIN_CONTROL_MODE);
59 	config->gain_control_enabled =
60 			APM_GET_INT(ini, APM_GAIN_CONTROL_ENABLED);
61 	config->ns_level = (enum noise_suppression_level)
62 			APM_GET_INT(ini, APM_NOISE_SUPPRESSION_LEVEL);
63 	config->noise_suppression_enabled =
64 			APM_GET_INT(ini, APM_NOISE_SUPPRESSION_ENABLED);
65 
66 	return config;
67 }
68 
apm_config_dump(struct apm_config * config)69 void apm_config_dump(struct apm_config *config)
70 {
71 	syslog(LOG_ERR, "---- apm config dump ----");
72 	syslog(LOG_ERR, "residual_echo_detector_enabled %u",
73 		config->residual_echo_detector_enabled);
74 	syslog(LOG_ERR, "high_pass_filter_enabled %u",
75 		config->high_pass_filter_enabled);
76 	syslog(LOG_ERR, "pre_amplifier_enabled %u",
77 		config->pre_amplifier_enabled);
78 	syslog(LOG_ERR, "pre_amplifier_fixed_gain_factor %f",
79 		config->pre_amplifier_fixed_gain_factor);
80 	syslog(LOG_ERR, "gain_controller2_enabled %u",
81 		config->gain_controller2_enabled);
82 	syslog(LOG_ERR, "gain_controller2_fixed_gain_db %f",
83 		config->gain_controller2_fixed_gain_db);
84 	syslog(LOG_ERR, "gain_controller2_adaptive_digital_mode %d",
85 		config->gain_controller2_adaptive_digital_mode);
86 	syslog(LOG_ERR, "gain_control_compression_gain_db %u",
87 		config->gain_control_compression_gain_db);
88 	syslog(LOG_ERR, "gain_control_mode %u", config->agc_mode);
89 	syslog(LOG_ERR, "gain_control_enabled %u",
90 		config->gain_control_enabled);
91 	syslog(LOG_ERR, "noise_suppression_level %u", config->ns_level);
92 	syslog(LOG_ERR, "noise_suppression_enabled %u",
93 		config->noise_suppression_enabled);
94 }
95