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 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179
|
/*
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 "aconfig.h"
#include <stdarg.h>
#include <stdio.h>
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <regex.h>
#include <dlfcn.h>
#include <alsa/asoundlib.h>
#include "gettext.h"
#include "topology.h"
#include "pre-processor.h"
#include "pre-process-external.h"
#define SND_TOPOLOGY_MAX_PLUGINS 32
static int get_plugin_string(struct tplg_pre_processor *tplg_pp, char **plugin_string)
{
const char *lib_names_t = NULL;
snd_config_t *defines;
int ret;
ret = snd_config_search(tplg_pp->input_cfg, "Define.PREPROCESS_PLUGINS", &defines);
if (ret < 0)
return ret;
ret = snd_config_get_string(defines, &lib_names_t);
if (ret < 0)
return ret;
*plugin_string = strdup(lib_names_t);
if (!*plugin_string)
return -ENOMEM;
return 0;
}
static int run_plugin(struct tplg_pre_processor *tplg_pp, char *plugin)
{
plugin_pre_process process;
char *xlib, *xfunc, *path;
void *h = NULL;
int ret = 0;
/* compose the plugin path, if not from environment, then from default plugins dir */
path = getenv("ALSA_TOPOLOGY_PLUGIN_DIR");
if (!path)
path = ALSA_TOPOLOGY_PLUGIN_DIR;
xlib = tplg_snprintf("%s/%s%s%s", path, PROCESS_LIB_PREFIX, plugin,
PROCESS_LIB_POSTFIX);
xfunc = tplg_snprintf("%s%s%s", PROCESS_FUNC_PREFIX, plugin,
PROCESS_FUNC_POSTFIX);
if (!xlib || !xfunc) {
fprintf(stderr, "can't reserve memory for plugin paths and func names\n");
ret = -ENOMEM;
goto err;
}
/* open plugin */
h = dlopen(xlib, RTLD_NOW);
if (!h) {
fprintf(stderr, "unable to open library '%s'\n", xlib);
ret = -EINVAL;
goto err;
}
/* find function */
process = dlsym(h, xfunc);
if (!process) {
fprintf(stderr, "symbol 'topology_process' was not found in %s\n", xlib);
ret = -EINVAL;
goto err;
}
/* process plugin */
ret = process(tplg_pp->input_cfg, tplg_pp->output_cfg);
err:
if (h)
dlclose(h);
if (xlib)
free(xlib);
if (xfunc)
free(xfunc);
return ret;
}
static int pre_process_plugins(struct tplg_pre_processor *tplg_pp)
{
char *plugins[SND_TOPOLOGY_MAX_PLUGINS];
char *plugin_string;
int count;
int ret;
int i;
/* parse plugin names */
ret = get_plugin_string(tplg_pp, &plugin_string);
/* no plugins defined, so just return */
if (ret < 0)
return 0;
count = 0;
plugins[count] = strtok(plugin_string, ":");
while ((count < SND_TOPOLOGY_MAX_PLUGINS - 1) && plugins[count]) {
count++;
plugins[count] = strtok(NULL, ":");
}
/* run all plugins */
for (i = 0; i < count; i++) {
ret = run_plugin(tplg_pp, plugins[i]);
if (ret < 0)
return ret;
}
free(plugin_string);
return 0;
}
/*
* Helper function to find config by id.
* Topology2.0 object names are constructed with attribute values separated by '.'.
* So snd_config_search() cannot be used as it interprets the '.' as the node separator.
*/
snd_config_t *tplg_find_config(snd_config_t *config, const char *name)
{
snd_config_iterator_t i, next;
snd_config_t *n;
const char *id;
snd_config_for_each(i, next, config) {
n = snd_config_iterator_entry(i);
if (snd_config_get_id(n, &id) < 0)
continue;
if (!strcmp(id, name))
return n;
}
return NULL;
}
/* make a new config and add it to parent */
int tplg_config_make_add(snd_config_t **config, const char *id, snd_config_type_t type,
snd_config_t *parent)
{
int ret;
ret = snd_config_make(config, id, type);
if (ret < 0)
return ret;
ret = snd_config_add(parent, *config);
if (ret < 0)
snd_config_delete(*config);
return ret;
}
/*
* The pre-processor will need to concat multiple strings separate by '.' to construct the object
* name and search for configs with ID's separated by '.'.
* This function helps concat input strings in the specified input format
*/
char *tplg_snprintf(char *fmt, ...)
{
char *string;
int len = 1;
va_list va;
va_start(va, fmt);
len += vsnprintf(NULL, 0, fmt, va);
va_end(va);
string = calloc(1, len);
if (!string)
return NULL;
va_start(va, fmt);
vsnprintf(string, len, fmt, va);
va_end(va);
return string;
}
#ifdef TPLG_DEBUG
void tplg_pp_debug(char *fmt, ...)
{
char msg[DEBUG_MAX_LENGTH];
va_list va;
va_start(va, fmt);
vsnprintf(msg, DEBUG_MAX_LENGTH, fmt, va);
va_end(va);
fprintf(stdout, "%s\n", msg);
}
void tplg_pp_config_debug(struct tplg_pre_processor *tplg_pp, snd_config_t *cfg)
{
snd_config_save(cfg, tplg_pp->dbg_output);
}
#else
void tplg_pp_debug(char *fmt ATTRIBUTE_UNUSED, ...) {}
void tplg_pp_config_debug(struct tplg_pre_processor *tplg_pp ATTRIBUTE_UNUSED,
snd_config_t *cfg ATTRIBUTE_UNUSED) {}
#endif
static int pre_process_config(struct tplg_pre_processor *tplg_pp, snd_config_t *cfg)
{
snd_config_iterator_t i, next, i2, next2;
snd_config_t *n, *n2;
const char *id;
int err;
if (snd_config_get_type(cfg) != SND_CONFIG_TYPE_COMPOUND) {
fprintf(stderr, "compound type expected at top level");
return -EINVAL;
}
/* parse topology objects */
snd_config_for_each(i, next, cfg) {
n = snd_config_iterator_entry(i);
if (snd_config_get_id(n, &id) < 0)
continue;
if (strcmp(id, "Object"))
continue;
if (snd_config_get_type(n) != SND_CONFIG_TYPE_COMPOUND) {
fprintf(stderr, "compound type expected for %s", id);
return -EINVAL;
}
snd_config_for_each(i2, next2, n) {
n2 = snd_config_iterator_entry(i2);
if (snd_config_get_id(n2, &id) < 0)
continue;
if (snd_config_get_type(n2) != SND_CONFIG_TYPE_COMPOUND) {
fprintf(stderr, "compound type expected for %s", id);
return -EINVAL;
}
/* pre-process Object instance. Top-level object have no parent */
err = tplg_pre_process_objects(tplg_pp, n2, NULL);
if (err < 0)
return err;
}
}
return 0;
}
void free_pre_processor(struct tplg_pre_processor *tplg_pp)
{
snd_output_close(tplg_pp->output);
snd_output_close(tplg_pp->dbg_output);
snd_config_delete(tplg_pp->output_cfg);
if (tplg_pp->define_cfg)
snd_config_delete(tplg_pp->define_cfg);
free(tplg_pp->inc_path);
free(tplg_pp);
}
int init_pre_processor(struct tplg_pre_processor **tplg_pp, snd_output_type_t type,
const char *output_file)
{
struct tplg_pre_processor *_tplg_pp;
int ret;
_tplg_pp = calloc(1, sizeof(struct tplg_pre_processor));
if (!_tplg_pp)
return -ENOMEM;
*tplg_pp = _tplg_pp;
/* create output top-level config node */
ret = snd_config_top(&_tplg_pp->output_cfg);
if (ret < 0)
goto err;
/* open output based on type */
if (type == SND_OUTPUT_STDIO) {
ret = snd_output_stdio_open(&_tplg_pp->output, output_file, "w");
if (ret < 0) {
fprintf(stderr, "failed to open file output\n");
goto open_err;
}
} else {
ret = snd_output_buffer_open(&_tplg_pp->output);
if (ret < 0) {
fprintf(stderr, "failed to open buffer output\n");
goto open_err;
}
}
/* debug output */
ret = snd_output_stdio_attach(&_tplg_pp->dbg_output, stdout, 0);
if (ret < 0) {
fprintf(stderr, "failed to open stdout output\n");
goto out_close;
}
return 0;
out_close:
snd_output_close(_tplg_pp->output);
open_err:
snd_config_delete(_tplg_pp->output_cfg);
err:
free(_tplg_pp);
return ret;
}
#if SND_LIB_VER(1, 2, 5) < SND_LIB_VERSION
static int pre_process_set_defines(struct tplg_pre_processor *tplg_pp, const char *pre_processor_defs)
{
int ret;
/*
* load the command line defines to the configuration tree
*/
if (pre_processor_defs != NULL) {
ret = snd_config_load_string(&tplg_pp->define_cfg, pre_processor_defs, 0);
if (ret < 0) {
fprintf(stderr, "Failed to load pre-processor command line definitions\n");
return ret;
}
} else {
tplg_pp->define_cfg = NULL;
}
return 0;
}
static int pre_process_add_defines(struct tplg_pre_processor *tplg_pp, snd_config_t *from)
{
snd_config_t *conf_defines, *conf_tmp;
int ret;
ret = snd_config_search(from, "Define", &conf_defines);
if (ret == -ENOENT) {
if (tplg_pp->input_cfg == from && tplg_pp->define_cfg) {
conf_defines = NULL;
goto create;
}
}
if (ret < 0)
return ret;
if (snd_config_get_type(conf_defines) != SND_CONFIG_TYPE_COMPOUND) {
fprintf(stderr, "Define must be a compound!\n");
return -EINVAL;
}
if (tplg_pp->input_cfg == from)
tplg_pp->define_cfg_merged = conf_defines;
if (tplg_pp->define_cfg_merged == NULL) {
create:
ret = snd_config_make_compound(&tplg_pp->define_cfg_merged, "Define", 0);
if (ret < 0)
return ret;
ret = snd_config_add(tplg_pp->input_cfg, tplg_pp->define_cfg_merged);
if (ret < 0)
return ret;
}
if (tplg_pp->define_cfg_merged != conf_defines) {
/*
* merge back to the main configuration tree (Define subtree)
*/
ret = snd_config_merge(tplg_pp->define_cfg_merged, conf_defines, true);
if (ret < 0) {
fprintf(stderr, "Failed to override main variable definitions\n");
return ret;
}
}
/*
* merge the command line defines with the variables in the conf file to override
* default values; use a copy (merge deletes the source tree)
*/
if (tplg_pp->define_cfg) {
ret = snd_config_copy(&conf_tmp, tplg_pp->define_cfg);
if (ret < 0) {
fprintf(stderr, "Failed to copy variable definitions\n");
return ret;
}
ret = snd_config_merge(tplg_pp->define_cfg_merged, conf_tmp, true);
if (ret < 0) {
fprintf(stderr, "Failed to override variable definitions\n");
snd_config_delete(conf_tmp);
return ret;
}
}
return 0;
}
static int pre_process_includes(struct tplg_pre_processor *tplg_pp, snd_config_t *top);
static int pre_process_include_conf(struct tplg_pre_processor *tplg_pp, snd_config_t *config,
snd_config_t **new, snd_config_t *variable)
{
snd_config_iterator_t i, next;
const char *variable_name;
char *value;
int ret;
if (snd_config_get_id(variable, &variable_name) < 0)
return 0;
switch(snd_config_get_type(variable)) {
case SND_CONFIG_TYPE_STRING:
{
const char *s;
if (snd_config_get_string(variable, &s) < 0) {
SNDERR("Invalid value for variable %s\n", variable_name);
return -EINVAL;
}
value = strdup(s);
if (!value)
return -ENOMEM;
break;
}
case SND_CONFIG_TYPE_INTEGER:
{
long v;
ret = snd_config_get_integer(variable, &v);
if (ret < 0) {
SNDERR("Invalid value for variable %s\n", variable_name);
return ret;
}
value = tplg_snprintf("%ld", v);
if (!value)
return -ENOMEM;
break;
}
default:
SNDERR("Invalid type for variable definition %s\n", variable_name);
return -EINVAL;
}
/* create top-level config node */
ret = snd_config_top(new);
if (ret < 0) {
SNDERR("failed to create top-level node for include conf %s\n", variable_name);
goto err;
}
snd_config_for_each(i, next, config) {
snd_input_t *in;
snd_config_t *n;
regex_t regex;
const char *filename;
const char *id;
char *full_path;
n = snd_config_iterator_entry(i);
if (snd_config_get_id(n, &id) < 0)
continue;
ret = regcomp(®ex, id, REG_EXTENDED | REG_ICASE);
if (ret) {
fprintf(stderr, "Could not compile regex\n");
goto err;
}
/* Execute regular expression */
ret = regexec(®ex, value, 0, NULL, 0);
if (ret)
continue;
/* regex matched. now include or use the configuration */
if (snd_config_get_type(n) == SND_CONFIG_TYPE_COMPOUND) {
/* configuration block */
ret = snd_config_merge(*new, n, 0);
if (ret < 0) {
fprintf(stderr, "Unable to merge key '%s'\n", value);
goto err;
}
} else {
ret = snd_config_get_string(n, &filename);
if (ret < 0)
goto err;
if (filename && filename[0] != '/')
full_path = tplg_snprintf("%s/%s", tplg_pp->inc_path, filename);
else
full_path = tplg_snprintf("%s", filename);
ret = snd_input_stdio_open(&in, full_path, "r");
if (ret < 0) {
fprintf(stderr, "Unable to open included conf file %s\n", full_path);
free(full_path);
goto err;
}
free(full_path);
/* load config */
ret = snd_config_load(*new, in);
snd_input_close(in);
if (ret < 0) {
fprintf(stderr, "Unable to load included configuration\n");
goto err;
}
}
/* forcefully overwrite with defines from the command line */
ret = pre_process_add_defines(tplg_pp, *new);
if (ret < 0 && ret != -ENOENT) {
fprintf(stderr, "Failed to parse arguments in input config\n");
goto err;
}
/* recursively process any nested includes */
ret = pre_process_includes(tplg_pp, *new);
if (ret < 0)
goto err;
}
err:
free(value);
return ret;
}
static int pre_process_includes(struct tplg_pre_processor *tplg_pp, snd_config_t *top)
{
snd_config_iterator_t i, next;
snd_config_t *includes;
const char *top_id;
int ret;
if (tplg_pp->define_cfg_merged == NULL)
return 0;
ret = snd_config_search(top, "IncludeByKey", &includes);
if (ret < 0)
return 0;
snd_config_get_id(top, &top_id);
snd_config_for_each(i, next, includes) {
snd_config_t *n, *new, *define;
const char *id;
n = snd_config_iterator_entry(i);
if (snd_config_get_id(n, &id) < 0)
continue;
/* find id from variable definitions */
ret = snd_config_search(tplg_pp->define_cfg_merged, id, &define);
if (ret < 0) {
fprintf(stderr, "No variable defined for %s\n", id);
return ret;
}
/* create conf node from included file */
ret = pre_process_include_conf(tplg_pp, n, &new, define);
if (ret < 0) {
fprintf(stderr, "Unable to process include file \n");
return ret;
}
/* merge the included conf file with the top-level conf */
ret = snd_config_merge(top, new, 0);
if (ret < 0) {
fprintf(stderr, "Failed to add included conf\n");
return ret;
}
}
/* delete all includes from current top */
snd_config_delete(includes);
return 0;
}
static int pre_process_includes_all(struct tplg_pre_processor *tplg_pp, snd_config_t *top)
{
snd_config_iterator_t i, next;
int ret;
if (snd_config_get_type(top) != SND_CONFIG_TYPE_COMPOUND)
return 0;
/* process includes at this node */
ret = pre_process_includes(tplg_pp, top);
if (ret < 0) {
fprintf(stderr, "Failed to process includes\n");
return ret;
}
/* process includes at all child nodes */
snd_config_for_each(i, next, top) {
snd_config_t *n;
n = snd_config_iterator_entry(i);
ret = pre_process_includes_all(tplg_pp, n);
if (ret < 0)
return ret;
}
return 0;
}
/* duplicate the existing objects in src into dest and update with new attribute */
static int pre_process_add_objects(struct tplg_pre_processor *tplg_pp ATTRIBUTE_UNUSED,
int *object_count, snd_config_t *src,
snd_config_t *dest, snd_config_t *attr_cfg)
{
snd_config_iterator_t i, next;
int ret;
snd_config_for_each(i, next, src) {
snd_config_t *n, *new, *new_attr;
char* new_id = tplg_snprintf("%d", (*object_count)++);
n = snd_config_iterator_entry(i);
/* duplicate the existing object */
ret = snd_config_copy(&new, n);
if (ret < 0) {
free(new_id);
return ret;
}
ret = snd_config_set_id(new, new_id);
free(new_id);
if (ret < 0) {
snd_config_delete(new);
return ret;
}
ret = snd_config_add(dest, new);
if (ret < 0) {
snd_config_delete(new);
return ret;
}
/* and update the new attribute */
ret = snd_config_copy(&new_attr, attr_cfg);
if (ret < 0)
return ret;
ret = snd_config_add(new, new_attr);
if (ret < 0) {
snd_config_delete(new_attr);
return ret;
}
}
return 0;
}
/* Create config based on the number of items in the array */
static int pre_process_create_items(struct tplg_pre_processor *tplg_pp,
snd_config_t *cfg, snd_config_t *top,
int *object_count_offset)
{
snd_config_iterator_t i, next;
snd_config_type_t type;
const char *class_id;
int attr_count = 0;
int object_count = *object_count_offset;
int ret;
snd_config_get_id(top, &class_id);
snd_config_for_each(i, next, cfg) {
snd_config_iterator_t i2, next2;
snd_config_t *n, *local_top;
const char *attribute;
int attr_val_count = 0;
object_count = *object_count_offset;
n = snd_config_iterator_entry(i);
type = snd_config_get_type(n);
if (type != SND_CONFIG_TYPE_COMPOUND)
continue;
if (snd_config_get_id(n, &attribute) < 0)
continue;
ret = snd_config_make(&local_top, class_id, SND_CONFIG_TYPE_COMPOUND);
snd_config_for_each(i2, next2, n) {
snd_config_t *n2, *new, *new_obj;
snd_config_type_t attr_type;
char *new_id;
n2 = snd_config_iterator_entry(i2);
attr_type = snd_config_get_type(n2);
/* create new config based on type */
if (attr_type == SND_CONFIG_TYPE_INTEGER) {
long val;
ret = snd_config_get_integer(n2, &val);
if (ret < 0)
return ret;
ret = snd_config_make(&new, attribute, SND_CONFIG_TYPE_INTEGER);
if (ret < 0)
return ret;
ret = snd_config_set_integer(new, val);
} else {
const char *s;
ret = snd_config_get_string(n2, &s);
if (ret < 0)
return ret;
ret = snd_config_make(&new, attribute, SND_CONFIG_TYPE_STRING);
if (ret < 0)
return ret;
ret = snd_config_set_string(new, s);
}
if (ret < 0)
goto err;
/* for the first array simply create new conf nodes */
if (!attr_count) {
new_id = tplg_snprintf("%d", object_count++);
ret = snd_config_make(&new_obj, new_id, SND_CONFIG_TYPE_COMPOUND);
free(new_id);
ret = snd_config_add(new_obj, new);
if (ret < 0) {
snd_config_delete(new_obj);
goto err;
}
ret = snd_config_add(local_top, new_obj);
if (ret < 0) {
snd_config_delete(new_obj);
goto err;
}
continue;
}
/*
* for the subsequent arrays, duplicate the existing objects
* and update them with the new ones
*/
ret = pre_process_add_objects(tplg_pp, &object_count, top,
local_top, new);
if (ret < 0) {
SNDERR("failed to add objects of type %s\n", class_id);
goto err;
}
attr_val_count++;
err:
snd_config_delete(new);
if (ret < 0) {
snd_config_delete(local_top);
return ret;
}
}
/* substitute current list of configs with the updated list */
ret = snd_config_substitute(top, local_top);
if (ret < 0) {
snd_config_delete(local_top);
return ret;
}
attr_count++;
}
*object_count_offset = object_count;
return 0;
}
static int pre_process_array_item(struct tplg_pre_processor *tplg_pp, snd_config_t *top,
snd_config_t *array)
{
snd_config_iterator_t i, next;
int object_count;
int ret;
snd_config_for_each(i, next, array) {
snd_config_iterator_t i3, next3;
snd_config_t *n, *new;
const char *id;
n = snd_config_iterator_entry(i);
if (snd_config_get_id(n, &id) < 0)
continue;
/* Create a new node if it doesn't exist already */
if (snd_config_search(top, id, &new) < 0) {
ret = snd_config_make(&new, id, SND_CONFIG_TYPE_COMPOUND);
if (ret < 0)
return ret;
/* add the list of objects to the current top */
ret = snd_config_add(top, new);
if (ret < 0) {
snd_config_delete(new);
return ret;
}
}
/* if the conf node is not an array, move on to parse child nodes */
if (snd_config_is_array(n) <= 0)
return pre_process_array_item(tplg_pp, new, n);
object_count = 0;
snd_config_for_each(i3, next3, n) {
snd_config_t *n3, *local_top;
n3 = snd_config_iterator_entry(i3);
ret = snd_config_make(&local_top, id, SND_CONFIG_TYPE_COMPOUND);
if (ret < 0)
return ret;
ret = pre_process_create_items(tplg_pp, n3, local_top,
&object_count);
if (ret < 0) {
SNDERR("failed to create objects of type %s\n", id);
return ret;
}
ret = snd_config_merge(new, local_top, 0);
if (ret < 0) {
snd_config_delete(local_top);
return ret;
}
}
}
return 0;
}
static int pre_process_array(struct tplg_pre_processor *tplg_pp, snd_config_t *top)
{
snd_config_t *arrays;
int ret;
ret = snd_config_search(top, "CombineArrays", &arrays);
if (ret < 0)
return 0;
ret = pre_process_array_item(tplg_pp, top, arrays);
if (ret < 0)
return ret;
snd_config_delete(arrays);
return 0;
}
static int pre_process_arrays(struct tplg_pre_processor *tplg_pp, snd_config_t *top)
{
snd_config_iterator_t i, next;
int ret;
if (snd_config_get_type(top) != SND_CONFIG_TYPE_COMPOUND)
return 0;
/* process object arrays at this node */
ret = pre_process_array(tplg_pp, top);
if (ret < 0) {
fprintf(stderr, "Failed to process object arrays\n");
return ret;
}
/* process object arrays at all child nodes */
snd_config_for_each(i, next, top) {
snd_config_t *n;
n = snd_config_iterator_entry(i);
ret = pre_process_arrays(tplg_pp, n);
if (ret < 0)
return ret;
}
return 0;
}
static int pre_process_subtree_copy(struct tplg_pre_processor *tplg_pp, snd_config_t *curr,
snd_config_t *top)
{
snd_config_iterator_t i, next;
snd_config_t *subtrees;
int ret;
ret = snd_config_search(curr, "SubTreeCopy", &subtrees);
if (ret < 0)
return 0;
snd_config_for_each(i, next, subtrees) {
snd_config_t *n, *source_cfg, *target_cfg, *type_cfg;
snd_config_t *source_tree, *target_tree, *tmp, *subtree_cfg;
const char *id, *source_id;
const char *type = NULL;
const char *target_id = NULL;
bool override = false;
n = snd_config_iterator_entry(i);
ret = snd_config_get_id(n, &id);
if (ret < 0) {
SNDERR("Failed to get ID for subtree copy\n");
return ret;
}
/* get the type of copy ex: override/extend if set, by default override is false */
ret = snd_config_search(n, "type", &type_cfg);
if (ret >= 0) {
ret = snd_config_get_string(type_cfg, &type);
if (ret >= 0) {
if (strcmp(type, "override") && strcmp(type, "extend")) {
SNDERR("Invalid value for sub tree copy type %s\n", type);
return ret;
}
if (!strcmp(type, "override"))
override = true;
}
}
ret = snd_config_search(n, "source", &source_cfg);
if (ret < 0) {
SNDERR("failed to get source config for subtree %s\n", id);
return ret;
}
/* if the target node is not set, the subtree will be copied to current node */
ret = snd_config_search(n, "target", &target_cfg);
if (ret >= 0) {
snd_config_t *temp_target;
char *s;
ret = snd_config_get_string(target_cfg, &target_id);
if (ret < 0) {
SNDERR("Invalid target id for subtree %s\n", id);
return ret;
}
/*
* create a temporary node with target_id and merge with top to avoid
* failure in case the target node ID doesn't exist already
*/
s = tplg_snprintf("%s {}", target_id);
if (!s)
return -ENOMEM;
ret = snd_config_load_string(&temp_target, s, 0);
free(s);
if (ret < 0) {
SNDERR("Cannot create temp node with target id %s\n", target_id);
return ret;
}
ret = snd_config_merge(top, temp_target, false);
if (ret < 0) {
SNDERR("Cannot merge temp node with target id %s\n", target_id);
return ret;
}
ret = snd_config_search(top, target_id, &target_tree);
if (ret < 0) {
SNDERR("failed to get target tree %s\n", target_id);
return ret;
}
} else {
target_tree = curr;
}
/* get the source tree node */
ret = snd_config_get_string(source_cfg, &source_id);
if (ret < 0) {
SNDERR("Invalid source node id for subtree %s\n", id);
return ret;
}
ret = snd_config_search(top, source_id, &source_tree);
if (ret < 0) {
SNDERR("failed to get source tree %s\n", source_id);
return ret;
}
/* get the subtree to be merged */
ret = snd_config_search(n, "tree", &subtree_cfg);
if (ret < 0)
subtree_cfg = NULL;
/* make a temp copy of the source tree */
ret = snd_config_copy(&tmp, source_tree);
if (ret < 0) {
SNDERR("failed to copy source tree for subtreecopy %s\n", id);
return ret;
}
/* merge the current block with the source tree */
ret = snd_config_merge(tmp, subtree_cfg, override);
if (ret < 0) {
snd_config_delete(tmp);
SNDERR("Failed to merge source tree w/ subtree %s\n", id);
return ret;
}
/* merge the merged block to the target tree */
ret = snd_config_merge(target_tree, tmp, override);
if (ret < 0) {
snd_config_delete(tmp);
SNDERR("Failed to merge subtree %s w/ target\n", id);
return ret;
}
}
/* all subtree copies have been processed, remove the node */
snd_config_delete(subtrees);
return 0;
}
static int pre_process_subtree_copies(struct tplg_pre_processor *tplg_pp, snd_config_t *top,
snd_config_t *curr)
{
snd_config_iterator_t i, next;
int ret;
if (snd_config_get_type(curr) != SND_CONFIG_TYPE_COMPOUND)
return 0;
/* process subtreecopies at this node */
ret = pre_process_subtree_copy(tplg_pp, curr, top);
if (ret < 0)
return ret;
/* process subtreecopies at all child nodes */
snd_config_for_each(i, next, curr) {
snd_config_t *n;
n = snd_config_iterator_entry(i);
/* process subtreecopies at this node */
ret = pre_process_subtree_copies(tplg_pp, top, n);
if (ret < 0)
return ret;
}
return 0;
}
#endif /* version < 1.2.6 */
int pre_process(struct tplg_pre_processor *tplg_pp, char *config, size_t config_size,
const char *pre_processor_defs, const char *inc_path)
{
snd_input_t *in;
snd_config_t *top;
int err;
/* create input buffer */
err = snd_input_buffer_open(&in, config, config_size);
if (err < 0) {
fprintf(stderr, "Unable to open input buffer\n");
return err;
}
/* create top-level config node */
err = snd_config_top(&top);
if (err < 0)
goto input_close;
/* load config */
err = snd_config_load(top, in);
if (err < 0) {
fprintf(stderr, "Unable not load configuration\n");
goto err;
}
tplg_pp->input_cfg = top;
tplg_pp->inc_path = inc_path ? strdup(inc_path) : NULL;
#if SND_LIB_VER(1, 2, 5) < SND_LIB_VERSION
/* parse command line definitions */
err = pre_process_set_defines(tplg_pp, pre_processor_defs);
if (err < 0) {
fprintf(stderr, "Failed to parse arguments in input config\n");
goto err;
}
/* parse command line definitions */
err = pre_process_add_defines(tplg_pp, top);
if (err < 0) {
fprintf(stderr, "Failed to parse arguments in input config\n");
goto err;
}
/* include conditional conf files */
err = pre_process_includes_all(tplg_pp, tplg_pp->input_cfg);
if (err < 0) {
fprintf(stderr, "Failed to process conditional includes in input config\n");
goto err;
}
/* expand object arrays */
err = pre_process_arrays(tplg_pp, tplg_pp->input_cfg);
if (err < 0) {
fprintf(stderr, "Failed to process object arrays in input config\n");
goto err;
}
err = pre_process_subtree_copies(tplg_pp, tplg_pp->input_cfg, tplg_pp->input_cfg);
if (err < 0) {
SNDERR("Failed to process subtree copies in input config\n");
goto err;
}
#endif
err = pre_process_config(tplg_pp, tplg_pp->input_cfg);
if (err < 0) {
fprintf(stderr, "Unable to pre-process configuration\n");
goto err;
}
/* process topology plugins */
err = pre_process_plugins(tplg_pp);
if (err < 0) {
fprintf(stderr, "Unable to run pre-process plugins or plugins return error\n");
goto err;
}
/* save config to output */
err = snd_config_save(tplg_pp->output_cfg, tplg_pp->output);
if (err < 0)
fprintf(stderr, "failed to save pre-processed output file\n");
err:
snd_config_delete(top);
input_close:
snd_input_close(in);
return err;
}
|