topology: pre-process-class: add function to convert valid attribute values to integer tuple values

Some attributes have valid values that need to be converted
to integer tuple values before it is appended to the
object's private data:

For ex, the buffer widget object's "caps" attribute has the
following definition:
	DefineAttribute."caps" {
		type	"string"
		# Token reference and type
		token_ref	"sof_tkn_buffer.word"
		constraints {
			value_ref	"sof_tkn_mem"
			valid_values [
				"dai"
				"host"
				"pass"
				"comp"
			]
			tuple_values [
				113
				113
				113
				65
			]
		}
	}

Depending on the user input, the value string values for "caps"
will be converted to the appropriate tuple values.

Signed-off-by: Ranjani Sridharan <ranjani.sridharan@linux.intel.com>
Signed-off-by: Jaroslav Kysela <perex@perex.cz>
This commit is contained in:
Ranjani Sridharan 2021-04-23 13:46:24 -07:00 committed by Jaroslav Kysela
parent 963578af1e
commit e3ad68b185
2 changed files with 69 additions and 0 deletions

View file

@ -18,6 +18,7 @@
in the file called LICENSE.GPL. in the file called LICENSE.GPL.
*/ */
#include <assert.h> #include <assert.h>
#include <errno.h>
#include <limits.h> #include <limits.h>
#include <stdio.h> #include <stdio.h>
#include <alsa/input.h> #include <alsa/input.h>
@ -234,3 +235,69 @@ const char *tplg_class_get_attribute_token_ref(struct tplg_pre_processor *tplg_p
return token; 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;
}

View file

@ -41,6 +41,8 @@ snd_config_type_t tplg_class_get_attribute_type(struct tplg_pre_processor *tplg_
snd_config_t *attr); snd_config_t *attr);
const char *tplg_class_get_attribute_token_ref(struct tplg_pre_processor *tplg_pp, 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 *class, const char *attr_name);
long tplg_class_attribute_valid_tuple_value(struct tplg_pre_processor *tplg_pp,
snd_config_t *class, snd_config_t *attr);
/* config helpers */ /* config helpers */
snd_config_t *tplg_find_config(snd_config_t *config, const char *name); snd_config_t *tplg_find_config(snd_config_t *config, const char *name);