From 7bf31094e299b3ef4e897c18e891e6546ee2e0c6 Mon Sep 17 00:00:00 2001 From: Ranjani Sridharan Date: Fri, 23 Apr 2021 13:13:01 -0700 Subject: [PATCH] topology: pre-process-class: Add functions to check attribute constraints Add helper functions to check if an attribute is mandatory, immutable or unique in the class definition. ex: for a host widget component, these are defined as follows: attributes { # # host objects instantiated within the same alsaconf node must have unique # direction attribute # unique "direction" mandatory [ "type" "stream_name" ] immutable [ "uuid" ] } Signed-off-by: Ranjani Sridharan Signed-off-by: Jaroslav Kysela --- topology/pre-process-class.c | 59 ++++++++++++++++++++++++++++++++++++ topology/pre-processor.h | 5 +++ 2 files changed, 64 insertions(+) diff --git a/topology/pre-process-class.c b/topology/pre-process-class.c index cd4c7c3..9033343 100644 --- a/topology/pre-process-class.c +++ b/topology/pre-process-class.c @@ -26,6 +26,65 @@ #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 diff --git a/topology/pre-processor.h b/topology/pre-processor.h index d7d1b11..a5cfa0a 100644 --- a/topology/pre-processor.h +++ b/topology/pre-processor.h @@ -18,6 +18,8 @@ #define __PRE_PROCESSOR_H #include +#include +#include #include "topology.h" #define DEBUG_MAX_LENGTH 256 @@ -30,6 +32,9 @@ void tplg_pp_config_debug(struct tplg_pre_processor *tplg_pp, snd_config_t *cfg) snd_config_t *tplg_class_lookup(struct tplg_pre_processor *tplg_pp, snd_config_t *cfg); snd_config_t *tplg_class_find_attribute_by_name(struct tplg_pre_processor *tplg_pp, snd_config_t *class, const char *name); +bool tplg_class_is_attribute_mandatory(const char *attr, snd_config_t *class_cfg); +bool tplg_class_is_attribute_immutable(const char *attr, snd_config_t *class_cfg); +bool tplg_class_is_attribute_unique(const char *attr, snd_config_t *class_cfg); /* config helpers */ snd_config_t *tplg_find_config(snd_config_t *config, const char *name); -- 2.47.1