summaryrefslogtreecommitdiff
path: root/topology/pre-process-class.c
blob: 49d93f6441c590b59156fb6c1f7fa8d79d83a1fc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
/*
  Copyright(c) 2021 Intel Corporation
  All rights reserved.

  This program is free software; you can redistribute it and/or modify
  it under the terms of version 2 of the GNU General Public License as
  published by the Free Software Foundation.

  This program is distributed in the hope that it will be useful, but
  WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
  The full GNU General Public License is included in this distribution
  in the file called LICENSE.GPL.
*/
#include <assert.h>
#include <errno.h>
#include <limits.h>
#include <stdio.h>
#include <alsa/asoundlib.h>
#include "topology.h"
#include "pre-processor.h"

bool tplg_class_is_attribute_check(const char *attr, snd_config_t *class_cfg, char *category)
{
	snd_config_iterator_t i, next;
	snd_config_t *cfg, *n;
	int ret;

	ret = snd_config_search(class_cfg, category, &cfg);
	if (ret < 0)
		return false;

	snd_config_for_each(i, next, cfg) {
		const char *id, *s;

		n = snd_config_iterator_entry(i);
		if (snd_config_get_id(n, &id) < 0)
			continue;

		if (snd_config_get_string(n, &s) < 0)
			continue;

		if (!strcmp(attr, s))
			return true;
	}

	return false;
}

/* check if attribute is mandatory */
bool tplg_class_is_attribute_mandatory(const char *attr, snd_config_t *class_cfg)
{
	return tplg_class_is_attribute_check(attr, class_cfg, "attributes.mandatory");
}

/* check if attribute is immutable */
bool tplg_class_is_attribute_immutable(const char *attr, snd_config_t *class_cfg)
{
	return tplg_class_is_attribute_check(attr, class_cfg, "attributes.immutable");
}

/* check if attribute is unique */
bool tplg_class_is_attribute_unique(const char *attr, snd_config_t *class_cfg)
{
	snd_config_t *unique;
	const char *s;
	int ret;

	ret = snd_config_search(class_cfg, "attributes.unique", &unique);
	if (ret < 0)
		return false;

	if (snd_config_get_string(unique, &s) < 0)
		return false;

	if (!strcmp(attr, s))
		return true;

	return false;
}

/*
 * Helper function to look up class definition from the Object config.
 * ex: For an object declaration, Object.Widget.pga.0{}, return the config correspdonding to
 * Class.Widget.pga{}. Note that input config , "cfg" does not include the "Object" node.
 */
snd_config_t *tplg_class_lookup(struct tplg_pre_processor *tplg_pp, snd_config_t *cfg)
{
	snd_config_iterator_t first, end;
	snd_config_t *class, *class_cfg = NULL;
	const char *class_type, *class_name;
	char *class_config_id;
	int ret;

	if (snd_config_get_id(cfg, &class_type) < 0)
		return NULL;

	first = snd_config_iterator_first(cfg);
	end = snd_config_iterator_end(cfg);

	if (first == end) {
		SNDERR("No class name provided for object type: %s\n", class_type);
		return NULL;
	}

	class = snd_config_iterator_entry(first);

	if (snd_config_get_id(class, &class_name) < 0)
		return NULL;

	class_config_id = tplg_snprintf("Class.%s.%s", class_type, class_name);
	if (!class_config_id)
		return NULL;

	ret = snd_config_search(tplg_pp->input_cfg, class_config_id, &class_cfg);
	if (ret < 0)
		SNDERR("No Class definition found for %s\n", class_config_id);

	free(class_config_id);
	return class_cfg;
}

/* find the attribute config by name in the class definition */
snd_config_t *tplg_class_find_attribute_by_name(struct tplg_pre_processor *tplg_pp,
						snd_config_t *class, const char *name)
{
	snd_config_t *attr = NULL;
	const char *class_id;
	char *attr_str;
	int ret;

	if (snd_config_get_id(class, &class_id) < 0)
		return NULL;

	attr_str = tplg_snprintf("DefineAttribute.%s", name);
	if (!attr_str)
		return NULL;

	ret = snd_config_search(class, attr_str, &attr);
	if (ret < 0)
		SNDERR("No definition for attribute '%s' in class '%s'\n",
			name, class_id);

	free(attr_str);
	return attr;
}

/* get the name of the attribute that must have a unique value in the object instance */
const char *tplg_class_get_unique_attribute_name(struct tplg_pre_processor *tplg_pp,
						 snd_config_t *class)
{
	snd_config_t *unique;
	const char *unique_name, *class_id;
	int ret;

	if (snd_config_get_id(class, &class_id) < 0)
		return NULL;

	ret = snd_config_search(class, "attributes.unique", &unique);
	if (ret < 0) {
		SNDERR("No unique attribute in class '%s'\n", class_id);
		return NULL;
	}

	if (snd_config_get_string(unique, &unique_name) < 0) {
		SNDERR("Invalid name for unique attribute in class '%s'\n", class_id);
		return NULL;
	}

	return unique_name;
}

/* get attribute type from the definition */
snd_config_type_t tplg_class_get_attribute_type(struct tplg_pre_processor *tplg_pp,
						snd_config_t *attr)
{
	snd_config_t *type;
	const char *s;
	int ret;

	/* default to integer if no type is given */
	ret = snd_config_search(attr, "type", &type);
	if (ret < 0)
		return SND_CONFIG_TYPE_INTEGER;

	ret = snd_config_get_string(type, &s);
	assert(ret >= 0);

	if (!strcmp(s, "string"))
		return SND_CONFIG_TYPE_STRING;

	if (!strcmp(s, "compound"))
		return SND_CONFIG_TYPE_COMPOUND;

	if (!strcmp(s, "real"))
		return SND_CONFIG_TYPE_REAL;

	if (!strcmp(s, "integer64"))
		return SND_CONFIG_TYPE_INTEGER64;

	return SND_CONFIG_TYPE_INTEGER;
}

/* get token_ref for attribute with name attr_name in the class */
const char *tplg_class_get_attribute_token_ref(struct tplg_pre_processor *tplg_pp,
					       snd_config_t *class, const char *attr_name)
{
	snd_config_t *attributes, *attr, *token_ref;
	const char *token;
	int ret;

	ret = snd_config_search(class, "DefineAttribute", &attributes);
	if (ret < 0)
		return NULL;

	ret = snd_config_search(attributes, attr_name, &attr);
	if (ret < 0)
		return NULL;

	ret = snd_config_search(attr, "token_ref", &token_ref);
	if (ret < 0)
		return NULL;

	ret = snd_config_get_string(token_ref, &token);
	if (ret < 0)
		return NULL;

	return token;
}

/* convert a valid attribute string value to the corresponding tuple value */
long tplg_class_attribute_valid_tuple_value(struct tplg_pre_processor *tplg_pp,
					    snd_config_t *class, snd_config_t *attr)
{

	snd_config_t *attributes, *cfg, *valid, *tuples, *n;
	snd_config_iterator_t i, next;
	const char *attr_name, *attr_value;
	int ret;

	ret = snd_config_get_id(attr, &attr_name);
	if (ret < 0)
		return -EINVAL;

	ret = snd_config_get_string(attr, &attr_value);
	if (ret < 0)
		return -EINVAL;

	/* find attribute definition in class */
	ret = snd_config_search(class, "DefineAttribute", &attributes);
	if (ret < 0)
		return -EINVAL;


	ret = snd_config_search(attributes, attr_name, &cfg);
	if (ret < 0)
		return -EINVAL;

	/* check if it has valid values */
	ret = snd_config_search(cfg, "constraints.valid_values", &valid);
	if (ret < 0)
		return -EINVAL;

	ret = snd_config_search(cfg, "constraints.tuple_values", &tuples);
	if (ret < 0)
		return -EINVAL;

	/* find and return the tuple value matching the attribute value id */
	snd_config_for_each(i, next, valid) {
		const char *s, *id;

		n = snd_config_iterator_entry(i);
		if (snd_config_get_string(n, &s) < 0)
			continue;
		if (snd_config_get_id(n, &id) < 0)
			continue;

		if (!strcmp(attr_value, s)) {
			snd_config_t *tuple;
			long tuple_value;

			ret = snd_config_search(tuples, id, &tuple);
			if (ret < 0)
				return -EINVAL;

			ret = snd_config_get_integer(tuple, &tuple_value);
			if (ret < 0)
				return ret;

			return tuple_value;
		}
	}

	return -EINVAL;
}