]> git.alsa-project.org Git - alsa-utils.git/commitdiff
topology: pre-process-class: add function to convert valid attribute values to intege...
authorRanjani Sridharan <ranjani.sridharan@linux.intel.com>
Fri, 23 Apr 2021 20:46:24 +0000 (13:46 -0700)
committerJaroslav Kysela <perex@perex.cz>
Tue, 25 May 2021 16:26:51 +0000 (18:26 +0200)
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>
topology/pre-process-class.c
topology/pre-processor.h

index 909865ad02d6869079f17145ca921ce531925d44..f4f3b01fd76bbb7fa6038399dc1a9fb7090275f7 100644 (file)
@@ -18,6 +18,7 @@
   in the file called LICENSE.GPL.
 */
 #include <assert.h>
+#include <errno.h>
 #include <limits.h>
 #include <stdio.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;
 }
+
+/* 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;
+}
index 81063b7702779759a04da5ea7be2ff76ae9ef423..ce72e096b5926638dd899698c434cc11912b3fd9 100644 (file)
@@ -41,6 +41,8 @@ snd_config_type_t tplg_class_get_attribute_type(struct tplg_pre_processor *tplg_
                                                snd_config_t *attr);
 const char *tplg_class_get_attribute_token_ref(struct tplg_pre_processor *tplg_pp,
                                                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 */
 snd_config_t *tplg_find_config(snd_config_t *config, const char *name);