• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2   Copyright(c) 2021 Intel Corporation
3   All rights reserved.
4 
5   This program is free software; you can redistribute it and/or modify
6   it under the terms of version 2 of the GNU General Public License as
7   published by the Free Software Foundation.
8 
9   This program is distributed in the hope that it will be useful, but
10   WITHOUT ANY WARRANTY; without even the implied warranty of
11   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12   General Public License for more details.
13 
14   You should have received a copy of the GNU General Public License
15   along with this program; if not, write to the Free Software
16   Foundation, Inc., 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
17   The full GNU General Public License is included in this distribution
18   in the file called LICENSE.GPL.
19 */
20 #include <assert.h>
21 #include <errno.h>
22 #include <limits.h>
23 #include <stdio.h>
24 #include <alsa/asoundlib.h>
25 #include "topology.h"
26 #include "pre-processor.h"
27 
tplg_class_is_attribute_check(const char * attr,snd_config_t * class_cfg,char * category)28 bool tplg_class_is_attribute_check(const char *attr, snd_config_t *class_cfg, char *category)
29 {
30 	snd_config_iterator_t i, next;
31 	snd_config_t *cfg, *n;
32 	int ret;
33 
34 	ret = snd_config_search(class_cfg, category, &cfg);
35 	if (ret < 0)
36 		return false;
37 
38 	snd_config_for_each(i, next, cfg) {
39 		const char *id, *s;
40 
41 		n = snd_config_iterator_entry(i);
42 		if (snd_config_get_id(n, &id) < 0)
43 			continue;
44 
45 		if (snd_config_get_string(n, &s) < 0)
46 			continue;
47 
48 		if (!strcmp(attr, s))
49 			return true;
50 	}
51 
52 	return false;
53 }
54 
55 /* check if attribute is mandatory */
tplg_class_is_attribute_mandatory(const char * attr,snd_config_t * class_cfg)56 bool tplg_class_is_attribute_mandatory(const char *attr, snd_config_t *class_cfg)
57 {
58 	return tplg_class_is_attribute_check(attr, class_cfg, "attributes.mandatory");
59 }
60 
61 /* check if attribute is immutable */
tplg_class_is_attribute_immutable(const char * attr,snd_config_t * class_cfg)62 bool tplg_class_is_attribute_immutable(const char *attr, snd_config_t *class_cfg)
63 {
64 	return tplg_class_is_attribute_check(attr, class_cfg, "attributes.immutable");
65 }
66 
67 /* check if attribute is unique */
tplg_class_is_attribute_unique(const char * attr,snd_config_t * class_cfg)68 bool tplg_class_is_attribute_unique(const char *attr, snd_config_t *class_cfg)
69 {
70 	snd_config_t *unique;
71 	const char *s;
72 	int ret;
73 
74 	ret = snd_config_search(class_cfg, "attributes.unique", &unique);
75 	if (ret < 0)
76 		return false;
77 
78 	if (snd_config_get_string(unique, &s) < 0)
79 		return false;
80 
81 	if (!strcmp(attr, s))
82 		return true;
83 
84 	return false;
85 }
86 
87 /*
88  * Helper function to look up class definition from the Object config.
89  * ex: For an object declaration, Object.Widget.pga.0{}, return the config correspdonding to
90  * Class.Widget.pga{}. Note that input config , "cfg" does not include the "Object" node.
91  */
tplg_class_lookup(struct tplg_pre_processor * tplg_pp,snd_config_t * cfg)92 snd_config_t *tplg_class_lookup(struct tplg_pre_processor *tplg_pp, snd_config_t *cfg)
93 {
94 	snd_config_iterator_t first, end;
95 	snd_config_t *class, *class_cfg = NULL;
96 	const char *class_type, *class_name;
97 	char *class_config_id;
98 	int ret;
99 
100 	if (snd_config_get_id(cfg, &class_type) < 0)
101 		return NULL;
102 
103 	first = snd_config_iterator_first(cfg);
104 	end = snd_config_iterator_end(cfg);
105 
106 	if (first == end) {
107 		SNDERR("No class name provided for object type: %s\n", class_type);
108 		return NULL;
109 	}
110 
111 	class = snd_config_iterator_entry(first);
112 
113 	if (snd_config_get_id(class, &class_name) < 0)
114 		return NULL;
115 
116 	class_config_id = tplg_snprintf("Class.%s.%s", class_type, class_name);
117 	if (!class_config_id)
118 		return NULL;
119 
120 	ret = snd_config_search(tplg_pp->input_cfg, class_config_id, &class_cfg);
121 	if (ret < 0)
122 		SNDERR("No Class definition found for %s\n", class_config_id);
123 
124 	free(class_config_id);
125 	return class_cfg;
126 }
127 
128 /* find the attribute config by name in the class definition */
tplg_class_find_attribute_by_name(struct tplg_pre_processor * tplg_pp,snd_config_t * class,const char * name)129 snd_config_t *tplg_class_find_attribute_by_name(struct tplg_pre_processor *tplg_pp,
130 						snd_config_t *class, const char *name)
131 {
132 	snd_config_t *attr = NULL;
133 	const char *class_id;
134 	char *attr_str;
135 	int ret;
136 
137 	if (snd_config_get_id(class, &class_id) < 0)
138 		return NULL;
139 
140 	attr_str = tplg_snprintf("DefineAttribute.%s", name);
141 	if (!attr_str)
142 		return NULL;
143 
144 	ret = snd_config_search(class, attr_str, &attr);
145 	if (ret < 0)
146 		SNDERR("No definition for attribute '%s' in class '%s'\n",
147 			name, class_id);
148 
149 	free(attr_str);
150 	return attr;
151 }
152 
153 /* get the name of the attribute that must have a unique value in the object instance */
tplg_class_get_unique_attribute_name(struct tplg_pre_processor * tplg_pp,snd_config_t * class)154 const char *tplg_class_get_unique_attribute_name(struct tplg_pre_processor *tplg_pp,
155 						 snd_config_t *class)
156 {
157 	snd_config_t *unique;
158 	const char *unique_name, *class_id;
159 	int ret;
160 
161 	if (snd_config_get_id(class, &class_id) < 0)
162 		return NULL;
163 
164 	ret = snd_config_search(class, "attributes.unique", &unique);
165 	if (ret < 0) {
166 		SNDERR("No unique attribute in class '%s'\n", class_id);
167 		return NULL;
168 	}
169 
170 	if (snd_config_get_string(unique, &unique_name) < 0) {
171 		SNDERR("Invalid name for unique attribute in class '%s'\n", class_id);
172 		return NULL;
173 	}
174 
175 	return unique_name;
176 }
177 
178 /* get attribute type from the definition */
tplg_class_get_attribute_type(struct tplg_pre_processor * tplg_pp,snd_config_t * attr)179 snd_config_type_t tplg_class_get_attribute_type(struct tplg_pre_processor *tplg_pp,
180 						snd_config_t *attr)
181 {
182 	snd_config_t *type;
183 	const char *s;
184 	int ret;
185 
186 	/* default to integer if no type is given */
187 	ret = snd_config_search(attr, "type", &type);
188 	if (ret < 0)
189 		return SND_CONFIG_TYPE_INTEGER;
190 
191 	ret = snd_config_get_string(type, &s);
192 	assert(ret >= 0);
193 
194 	if (!strcmp(s, "string"))
195 		return SND_CONFIG_TYPE_STRING;
196 
197 	if (!strcmp(s, "compound"))
198 		return SND_CONFIG_TYPE_COMPOUND;
199 
200 	if (!strcmp(s, "real"))
201 		return SND_CONFIG_TYPE_REAL;
202 
203 	if (!strcmp(s, "integer64"))
204 		return SND_CONFIG_TYPE_INTEGER64;
205 
206 	return SND_CONFIG_TYPE_INTEGER;
207 }
208 
209 /* get token_ref for attribute with name attr_name in the class */
tplg_class_get_attribute_token_ref(struct tplg_pre_processor * tplg_pp,snd_config_t * class,const char * attr_name)210 const char *tplg_class_get_attribute_token_ref(struct tplg_pre_processor *tplg_pp,
211 					       snd_config_t *class, const char *attr_name)
212 {
213 	snd_config_t *attributes, *attr, *token_ref;
214 	const char *token;
215 	int ret;
216 
217 	ret = snd_config_search(class, "DefineAttribute", &attributes);
218 	if (ret < 0)
219 		return NULL;
220 
221 	ret = snd_config_search(attributes, attr_name, &attr);
222 	if (ret < 0)
223 		return NULL;
224 
225 	ret = snd_config_search(attr, "token_ref", &token_ref);
226 	if (ret < 0)
227 		return NULL;
228 
229 	ret = snd_config_get_string(token_ref, &token);
230 	if (ret < 0)
231 		return NULL;
232 
233 	return token;
234 }
235 
236 /* convert a valid attribute string value to the corresponding tuple value */
tplg_class_attribute_valid_tuple_value(struct tplg_pre_processor * tplg_pp,snd_config_t * class,snd_config_t * attr)237 long tplg_class_attribute_valid_tuple_value(struct tplg_pre_processor *tplg_pp,
238 					    snd_config_t *class, snd_config_t *attr)
239 {
240 
241 	snd_config_t *attributes, *cfg, *valid, *tuples, *n;
242 	snd_config_iterator_t i, next;
243 	const char *attr_name, *attr_value;
244 	int ret;
245 
246 	ret = snd_config_get_id(attr, &attr_name);
247 	if (ret < 0)
248 		return -EINVAL;
249 
250 	ret = snd_config_get_string(attr, &attr_value);
251 	if (ret < 0)
252 		return -EINVAL;
253 
254 	/* find attribute definition in class */
255 	ret = snd_config_search(class, "DefineAttribute", &attributes);
256 	if (ret < 0)
257 		return -EINVAL;
258 
259 
260 	ret = snd_config_search(attributes, attr_name, &cfg);
261 	if (ret < 0)
262 		return -EINVAL;
263 
264 	/* check if it has valid values */
265 	ret = snd_config_search(cfg, "constraints.valid_values", &valid);
266 	if (ret < 0)
267 		return -EINVAL;
268 
269 	ret = snd_config_search(cfg, "constraints.tuple_values", &tuples);
270 	if (ret < 0)
271 		return -EINVAL;
272 
273 	/* find and return the tuple value matching the attribute value id */
274 	snd_config_for_each(i, next, valid) {
275 		const char *s, *id;
276 
277 		n = snd_config_iterator_entry(i);
278 		if (snd_config_get_string(n, &s) < 0)
279 			continue;
280 		if (snd_config_get_id(n, &id) < 0)
281 			continue;
282 
283 		if (!strcmp(attr_value, s)) {
284 			snd_config_t *tuple;
285 			long tuple_value;
286 
287 			ret = snd_config_search(tuples, id, &tuple);
288 			if (ret < 0)
289 				return -EINVAL;
290 
291 			ret = snd_config_get_integer(tuple, &tuple_value);
292 			if (ret < 0)
293 				return ret;
294 
295 			return tuple_value;
296 		}
297 	}
298 
299 	return -EINVAL;
300 }
301