summaryrefslogtreecommitdiff
path: root/topology/pre-process-object.c
blob: c0ae79f4d3a356f863d1b4ba3894c85ef6fc8869 (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
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
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
/*
  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 <stddef.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <alsa/asoundlib.h>
#include "gettext.h"
#include "topology.h"
#include "pre-processor.h"

/* Parse VendorToken object, create the "SectionVendorToken" and save it */
int tplg_build_vendor_token_object(struct tplg_pre_processor *tplg_pp,
				   snd_config_t *obj_cfg, snd_config_t *parent)
{
	snd_config_iterator_t i, next;
	snd_config_t *vtop, *n, *obj;
	const char *name;
	int ret;

	ret = tplg_build_object_from_template(tplg_pp, obj_cfg, &vtop, NULL, false);
	if (ret < 0)
		return ret;

	ret = snd_config_get_id(vtop, &name);
	if (ret < 0)
		return ret;

	/* add the tuples */
	obj = tplg_object_get_instance_config(tplg_pp, obj_cfg);
	snd_config_for_each(i, next, obj) {
		snd_config_t *dst;
		const char *id;

		n = snd_config_iterator_entry(i);

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

		if (!strcmp(id, "name"))
			continue;

		ret = snd_config_copy(&dst, n);
		if (ret < 0) {
			SNDERR("Error copying config node %s for '%s'\n", id, name);
			return ret;
		}

		ret = snd_config_add(vtop, dst);
		if (ret < 0) {
			snd_config_delete(dst);
			SNDERR("Error adding vendortoken %s for %s\n", id, name);
			return ret;
		}
	}

	return ret;
}

int tplg_parent_update(struct tplg_pre_processor *tplg_pp, snd_config_t *parent,
			  const char *section_name, const char *item_name)
{
	snd_config_iterator_t i, next;
	snd_config_t *child, *cfg, *top, *item_config, *n;
	const char *parent_name;
	char *item_id;
	int ret, id = 0;

	/* Nothing to do if parent is NULL */
	if (!parent)
		return 0;

	child = tplg_object_get_instance_config(tplg_pp, parent);
	ret = snd_config_search(child, "name", &cfg);
	if (ret < 0) {
		ret = snd_config_get_id(child, &parent_name);
		if (ret < 0) {
			SNDERR("No name config for parent\n");
			return ret;
		}
	} else {
		ret = snd_config_get_string(cfg, &parent_name);
		if (ret < 0) {
			SNDERR("Invalid name for parent\n");
			return ret;
		}
	}

	top = tplg_object_get_section(tplg_pp, parent);
	if (!top)
		return -EINVAL;

	/* get config with name */
	cfg = tplg_find_config(top, parent_name);
	if (!cfg)
		return ret;

	/* get section config */
	if (!strcmp(section_name, "tlv")) {
		/* set tlv name if config exists already */
		ret = snd_config_search(cfg, section_name, &item_config);
			if (ret < 0) {
			ret = tplg_config_make_add(&item_config, section_name,
						  SND_CONFIG_TYPE_STRING, cfg);
			if (ret < 0) {
				SNDERR("Error creating section config widget %s for %s\n",
				       section_name, parent_name);
				return ret;
			}
		}

		return snd_config_set_string(item_config, item_name);
	}

	ret = snd_config_search(cfg, section_name, &item_config);
	if (ret < 0) {
		ret = tplg_config_make_add(&item_config, section_name,
					  SND_CONFIG_TYPE_COMPOUND, cfg);
		if (ret < 0) {
			SNDERR("Error creating section config widget %s for %s\n",
			       section_name, parent_name);
			return ret;
		}
	}

	snd_config_for_each(i, next, item_config) {
		const char *name;

		n = snd_config_iterator_entry(i);
		if (snd_config_get_string(n, &name) < 0)
			continue;

		/* item already exists */
		if (!strcmp(name, item_name))
			return 0;
		id++;
	}

	/* add new item */
	item_id = tplg_snprintf("%d", id);
	if (!item_id)
		return -ENOMEM;

	ret = snd_config_make(&cfg, item_id, SND_CONFIG_TYPE_STRING);
	free(item_id);
	if (ret < 0)
		return ret;

	ret = snd_config_set_string(cfg, item_name);
	if (ret < 0)
		return ret;

	ret = snd_config_add(item_config, cfg);
	if (ret < 0)
		snd_config_delete(cfg);

	return ret;
}

/* Parse data object, create the "SectionData" and save it. Only "bytes" data supported for now */
int tplg_build_data_object(struct tplg_pre_processor *tplg_pp, snd_config_t *obj_cfg,
			    snd_config_t *parent)
{
	snd_config_t *dtop;
	const char *name;
	int ret;

	ret = tplg_build_object_from_template(tplg_pp, obj_cfg, &dtop, NULL, false);
	if (ret < 0)
		return ret;

	ret = snd_config_get_id(dtop, &name);
	if (ret < 0)
		return ret;

	return tplg_parent_update(tplg_pp, parent, "data", name);
}

static int tplg_create_config_template(struct tplg_pre_processor *tplg_pp,
				       snd_config_t **template,
				       const struct config_template_items *items)
{
	snd_config_t *top, *child;
	int ret, i;

	ret = snd_config_make(&top, "template", SND_CONFIG_TYPE_COMPOUND);
	if (ret < 0)
		return ret;

	/* add integer configs */
	for (i = 0; i < MAX_CONFIGS_IN_TEMPLATE; i++)
		if (items->int_config_ids[i]) {
			ret = tplg_config_make_add(&child, items->int_config_ids[i],
						   SND_CONFIG_TYPE_INTEGER, top);
			if (ret < 0)
				goto err;
		}

	/* add string configs */
	for (i = 0; i < MAX_CONFIGS_IN_TEMPLATE; i++)
		if (items->string_config_ids[i]) {
			ret = tplg_config_make_add(&child, items->string_config_ids[i],
						   SND_CONFIG_TYPE_STRING, top);
			if (ret < 0)
				goto err;
		}

	/* add compound configs */
	for (i = 0; i < MAX_CONFIGS_IN_TEMPLATE; i++) {
		if (items->compound_config_ids[i]) {
			ret = tplg_config_make_add(&child, items->compound_config_ids[i],
						   SND_CONFIG_TYPE_COMPOUND, top);
			if (ret < 0)
				goto err;
		}
	}

err:
	if (ret < 0) {
		snd_config_delete(top);
		return ret;
	}

	*template = top;
	return ret;
}

static void tplg_attribute_print_valid_values(snd_config_t *valid_values, const char *name)
{
	snd_config_iterator_t i, next;
	snd_config_t *n;

	SNDERR("valid values for attribute %s are:\n", name);

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

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

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

		SNDERR("%s", s);
	}
}

/* check is attribute value belongs in the set of valid values */
static bool tplg_is_attribute_valid_value(snd_config_t *valid_values, const char *value)
{
	snd_config_iterator_t i, next;
	snd_config_t *n;

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

		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(value, s))
			return true;
	}

	return false;
}

#if SND_LIB_VER(1, 2, 5) < SND_LIB_VERSION
static int pre_process_find_variable(snd_config_t **dst, const char *str, snd_config_t *config);

/* check is attribute value belongs in the set of valid values after expanding the valid values */
static bool tplg_is_attribute_valid_expanded_value(struct tplg_pre_processor *tplg_pp,
						   snd_config_t *valid_values, int value)
{
	snd_config_iterator_t i, next;
	snd_config_t *n, *var, *conf_defines;
	int ret;

	ret = snd_config_search(tplg_pp->input_cfg, "Define", &conf_defines);
	if (ret < 0)
		return false;

	snd_config_for_each(i, next, valid_values) {
		const char *s, *id;
		long v;

		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 (s[0] != '$')
			continue;

		/* find the variable definition */
		ret = pre_process_find_variable(&var, s + 1, conf_defines);
		if (ret < 0) {
			SNDERR("No definition for variable %s\n", s + 1);
			return false;
		}

		if (snd_config_get_integer(var, &v) < 0) {
			SNDERR("Invalid definition for %s\n", s);
			snd_config_delete(var);
			return false;
		}

		snd_config_delete(var);

		if (value == v)
			return true;
	}

	return false;
}
#endif

/* check if attribute value passes the min/max value constraints */
static bool tplg_object_is_attribute_min_max_valid(snd_config_t *attr, snd_config_t *obj_attr,
						   bool min_check)
{
	snd_config_type_t type = snd_config_get_type(obj_attr);
	snd_config_t *valid;
	const char *attr_name;
	int ret;

	if (snd_config_get_id(attr, &attr_name) < 0)
		return false;

	if (min_check) {
		ret = snd_config_search(attr, "constraints.min", &valid);
		if (ret < 0)
			return true;
	} else {
		ret = snd_config_search(attr, "constraints.max", &valid);
		if (ret < 0)
			return true;
	}

	switch(type) {
	case SND_CONFIG_TYPE_INTEGER:
	{
		long v, m;

		if (snd_config_get_integer(valid, &m) < 0)
			return true;

		if (snd_config_get_integer(obj_attr, &v) < 0)
			return false;

		if (min_check) {
			if (v < m) {
				SNDERR("attribute '%s' value: %ld is less than min value: %d\n",
				       attr_name, v, m);
				return false;
			}
		} else {
			if (v > m) {
				SNDERR("attribute '%s' value: %ld is greater than max value: %d\n",
				       attr_name, v, m);
				return false;
			}
		}

		return true;
	}
	case SND_CONFIG_TYPE_INTEGER64:
	{
		long long v;
		long m;

		if (snd_config_get_integer(valid, &m) < 0)
			return true;

		if (snd_config_get_integer64(obj_attr, &v) < 0)
			return false;

		if (min_check) {
			if (v < m) {
				SNDERR("attribute '%s' value: %ld is less than min value: %d\n",
				       attr_name, v, m);
				return false;
			}
		} else {
			if (v > m) {
				SNDERR("attribute '%s' value: %ld is greater than max value: %d\n",
				       attr_name, v, m);
				return false;
			}
		}

		return true;
	}
	default:
		break;
	}

	return false;
}

/* check for min/max and valid value constraints */
static bool tplg_object_is_attribute_valid(struct tplg_pre_processor *tplg_pp,
					   snd_config_t *attr, snd_config_t *obj_attr)
{
	snd_config_iterator_t i, next;
	snd_config_t *valid, *n;
	snd_config_type_t type;
	const char *attr_name, *obj_value;
	int ret;

	if (snd_config_get_id(attr, &attr_name) < 0)
		return false;

	type = snd_config_get_type(obj_attr);

	/* check if attribute has valid values */
	ret = snd_config_search(attr, "constraints.valid_values", &valid);
	if (ret < 0)
		goto min_max_check;

	switch(type) {
	case SND_CONFIG_TYPE_STRING:
		if (snd_config_get_string(obj_attr, &obj_value) < 0)
			return false;
		if (!tplg_is_attribute_valid_value(valid, obj_value)) {
			tplg_attribute_print_valid_values(valid, attr_name);
			return false;
		}
		return true;
	case SND_CONFIG_TYPE_COMPOUND:
		snd_config_for_each(i, next, obj_attr) {
			const char *s, *id;

			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 (!tplg_is_attribute_valid_value(valid, s)) {
				tplg_attribute_print_valid_values(valid, attr_name);
				return false;
			}
		}
		return true;
#if SND_LIB_VER(1, 2, 5) < SND_LIB_VERSION
	case SND_CONFIG_TYPE_INTEGER:
	{
		long v;

		if (snd_config_get_integer(obj_attr, &v) < 0)
			return false;

		if (!tplg_is_attribute_valid_expanded_value(tplg_pp, valid, v)) {
			tplg_attribute_print_valid_values(valid, attr_name);
			return false;
		}
		return true;
	}
#endif
	default:
		break;
	}

	return false;

min_max_check:
	if (!tplg_object_is_attribute_min_max_valid(attr, obj_attr, true))
		return false;

	return tplg_object_is_attribute_min_max_valid(attr, obj_attr, false);
}

/* get object's name attribute value */
const char *tplg_object_get_name(struct tplg_pre_processor *tplg_pp,
				 snd_config_t *object)
{
	snd_config_t *cfg;
	const char *name;
	int ret;

	ret = snd_config_search(object, "name", &cfg);
	if (ret < 0)
		return NULL;

	ret = snd_config_get_string(cfg, &name);
	if (ret < 0)
		return NULL;

	return name;
}

/* look up the instance of object in a config */
static snd_config_t *tplg_object_lookup_in_config(struct tplg_pre_processor *tplg_pp,
						  snd_config_t *class, const char *type,
						  const char *class_name, const char *id)
{
	snd_config_t *obj_cfg = NULL;
	char *config_id;

	config_id = tplg_snprintf("Object.%s.%s.%s", type, class_name, id);
	if (!config_id)
		return NULL;

	if (snd_config_search(class, config_id, &obj_cfg) < 0)
		return NULL;
	free(config_id);
	return obj_cfg;
}

static int tplg_pp_add_object_tuple_section(struct tplg_pre_processor *tplg_pp,
					    snd_config_t *class_cfg,
					    snd_config_t *attr, char *data_name,
					    const char *token_ref, const char *array_name)
{
	snd_config_t *top, *tuple_cfg, *child, *cfg, *new;
	const char *id;
	char *token, *type, *str;
	long tuple_value;
	int ret;

	tplg_pp_debug("Building vendor tuples section: '%s' ...", data_name);

	ret = snd_config_search(tplg_pp->output_cfg, "SectionVendorTuples", &top);
	if (ret < 0) {
		ret = tplg_config_make_add(&top, "SectionVendorTuples",
					  SND_CONFIG_TYPE_COMPOUND, tplg_pp->output_cfg);
		if (ret < 0) {
			SNDERR("Error creating SectionVendorTuples config\n");
			return ret;
		}
	}

	type = strchr(token_ref, '.');
	if(!type) {
		SNDERR("Error getting type for %s\n", token_ref);
		return -EINVAL;
	}

	token = calloc(1, strlen(token_ref) - strlen(type) + 1);
	if (!token)
		return -ENOMEM;
	snprintf(token, strlen(token_ref) - strlen(type) + 1, "%s", token_ref);

	if (!array_name)
		str = strdup(type + 1);
	else
		str = tplg_snprintf("%s.%s", type + 1, array_name);
	if (!str) {
		ret = -ENOMEM;
		goto free;
	}

	tuple_cfg = tplg_find_config(top, data_name);
	if (!tuple_cfg) {
		/* add new SectionVendorTuples */
		ret = tplg_config_make_add(&tuple_cfg, data_name, SND_CONFIG_TYPE_COMPOUND, top);
		if (ret < 0) {
			SNDERR("Error creating new vendor tuples config %s\n", data_name);
			goto err;
		}

		ret = tplg_config_make_add(&child, "tokens", SND_CONFIG_TYPE_STRING,
					  tuple_cfg);
		if (ret < 0) {
			SNDERR("Error creating tokens config for '%s'\n", data_name);
			goto err;
		}

		ret = snd_config_set_string(child, token);
		if (ret < 0) {
			SNDERR("Error setting tokens config for '%s'\n", data_name);
			goto err;
		}

		ret = tplg_config_make_add(&child, "tuples", SND_CONFIG_TYPE_COMPOUND,
					  tuple_cfg);
		if (ret < 0) {
			SNDERR("Error creating tuples config for '%s'\n", data_name);
			goto err;
		}

		ret = tplg_config_make_add(&cfg, str, SND_CONFIG_TYPE_COMPOUND,
					  child);
		if (ret < 0) {
			SNDERR("Error creating tuples type config for '%s'\n", data_name);
			goto err;
		}
	} else {
		snd_config_t *tuples_cfg;

		ret = snd_config_search(tuple_cfg, "tuples" , &tuples_cfg);
		if (ret < 0) {
			SNDERR("can't find tuples config in %s\n", data_name);
			goto err;
		}

		cfg = tplg_find_config(tuples_cfg, str);
		if (!cfg) {
			ret = tplg_config_make_add(&cfg, str, SND_CONFIG_TYPE_COMPOUND,
						  tuples_cfg);
			if (ret < 0) {
				SNDERR("Error creating tuples config for '%s' and type name %s\n", data_name, str);
				goto err;
			}
		}
	}

	ret = snd_config_get_id(attr, &id);
	if (ret < 0)
		goto err;

	/* tuple exists already? */
	ret = snd_config_search(cfg, id, &child);
	if (ret >=0)
		goto err;

	/* add attribute to tuples */
	tuple_value = tplg_class_attribute_valid_tuple_value(tplg_pp, class_cfg, attr);
	if (tuple_value < 0) {
		/* just copy attribute cfg as is */
		ret = snd_config_copy(&new, attr);
		if (ret < 0) {
			SNDERR("can't copy attribute for %s\n", data_name);
			goto err;
		}
	} else {
		ret = snd_config_make(&new, id, SND_CONFIG_TYPE_INTEGER);
		if (ret < 0)
			goto err;

		ret = snd_config_set_integer(new, tuple_value);
		if (ret < 0)
			goto err;
	}

	ret = snd_config_add(cfg, new);
err:
	free(str);
free:
	free(token);
	return ret;
}

static int tplg_pp_add_object_data_section(struct tplg_pre_processor *tplg_pp,
					   snd_config_t *obj_data, char *data_name)
{
	snd_config_iterator_t i, next;
	snd_config_t *top, *data_cfg, *child;
	char *data_id;
	int ret, id = 0;

	ret = snd_config_search(tplg_pp->output_cfg, "SectionData", &top);
	if (ret < 0) {
		ret = tplg_config_make_add(&top, "SectionData", SND_CONFIG_TYPE_COMPOUND,
					  tplg_pp->output_cfg);
		if (ret < 0) {
			SNDERR("Failed to add SectionData\n");
			return ret;
		}
	}

	/* nothing to do if data section already exists */
	data_cfg = tplg_find_config(top, data_name);
	if (data_cfg)
		return 0;

	tplg_pp_debug("Building data section %s ...", data_name);

	/* add new SectionData */
	ret = tplg_config_make_add(&data_cfg, data_name, SND_CONFIG_TYPE_COMPOUND, top);
	if (ret < 0)
		return ret;

	ret = tplg_config_make_add(&child, "tuples", SND_CONFIG_TYPE_STRING, data_cfg);
	if (ret < 0) {
		SNDERR("error adding data ref for %s\n", data_name);
		return ret;
	}

	ret = snd_config_set_string(child, data_name);
	if (ret < 0) {
		SNDERR("error setting tuples ref for %s\n", data_name);
		return ret;
	}

	/* add data item to object */
	snd_config_for_each(i, next, obj_data)
		id++;

	data_id = tplg_snprintf("%d", id);
	if (!data_id)
		return -ENOMEM;

	ret = tplg_config_make_add(&child, data_id, SND_CONFIG_TYPE_STRING, obj_data);
	free(data_id);
	if (ret < 0) {
		SNDERR("error adding data ref %s\n", data_name);
		return ret;
	}

	return snd_config_set_string(child, data_name);
}

int tplg_add_object_data(struct tplg_pre_processor *tplg_pp, snd_config_t *obj_cfg,
			 snd_config_t *top, const char *array_name)
{
	snd_config_iterator_t i, next;
	snd_config_t *data_cfg, *class_cfg, *n, *obj;
	const char *object_id;
	int ret;

	if (snd_config_get_id(top, &object_id) < 0)
		return 0;

	obj = tplg_object_get_instance_config(tplg_pp, obj_cfg);

	class_cfg = tplg_class_lookup(tplg_pp, obj_cfg);
	if (!class_cfg)
		return -EINVAL;

	/* add data config to top */
	ret = snd_config_search(top, "data", &data_cfg);
	if (ret < 0) {
		ret = tplg_config_make_add(&data_cfg, "data", SND_CONFIG_TYPE_COMPOUND, top);
		if (ret < 0) {
			SNDERR("error creating data config for %s\n", object_id);
			return ret;
		}
	}

	/* add data items to object's data section */
	snd_config_for_each(i, next, obj) {
		const char *id, *token;
		char *data_cfg_name;

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

		token = tplg_class_get_attribute_token_ref(tplg_pp, class_cfg, id);
		if (!token)
			continue;

		data_cfg_name = tplg_snprintf("%s.%s", object_id, token);
		if (!data_cfg_name)
			return -ENOMEM;

		ret = tplg_pp_add_object_data_section(tplg_pp, data_cfg, data_cfg_name);
		if (ret < 0) {
			SNDERR("Failed to add data section %s\n", data_cfg_name);
			free(data_cfg_name);
			return ret;
		}

		ret = tplg_pp_add_object_tuple_section(tplg_pp, class_cfg, n, data_cfg_name,
						       token, array_name);
		if (ret < 0) {
			SNDERR("Failed to add data section %s\n", data_cfg_name);
			free(data_cfg_name);
			return ret;
		}
		free(data_cfg_name);
	}

	return 0;
}

/* search for all template configs in the source config and copy them to the destination */
static int tplg_object_add_attributes(snd_config_t *dst, snd_config_t *template,
				      snd_config_t *src)
{
	snd_config_iterator_t i, next;
	snd_config_t *n;
	int ret;

	snd_config_for_each(i, next, template) {
		snd_config_t *attr, *new;
		const char *id;

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

		ret = snd_config_search(src, id, &attr);
		if (ret < 0)
			continue;

		/* skip if attribute is already set */
		ret = snd_config_search(dst, id, &new);
		if (ret >= 0)
			continue;

		ret = snd_config_copy(&new, attr);
		if (ret < 0) {
			SNDERR("failed to copy attribute %s\n", id);
			return ret;
		}

		ret = snd_config_add(dst, new);
		if (ret < 0) {
			snd_config_delete(new);
			SNDERR("failed to add attribute %s\n", id);
			return ret;
		}
	}

	return 0;
}

static const struct build_function_map *tplg_object_get_map(struct tplg_pre_processor *tplg_pp,
							    snd_config_t *obj);

/* Add object attributes to the private data of the parent object config */
static int tplg_build_parent_data(struct tplg_pre_processor *tplg_pp, snd_config_t *obj_cfg,
				  snd_config_t *parent)
{
	snd_config_t *obj, *parent_obj, *section_cfg, *top;
	const struct build_function_map *map;
	const char *id, *parent_id;
	int ret;

	/* nothing to do if parent is NULL */
	if (!parent)
		return 0;

	obj = tplg_object_get_instance_config(tplg_pp, obj_cfg);
	parent_obj = tplg_object_get_instance_config(tplg_pp, parent);

	/* get object ID */
	if (snd_config_get_id(obj, &id) < 0) {
		SNDERR("Invalid ID for object\n");
		return -EINVAL;
	}

	/* get parent object name or ID */
	parent_id = tplg_object_get_name(tplg_pp, parent_obj);
	if (!parent_id) {
		ret = snd_config_get_id(parent_obj, &parent_id);
		if (ret < 0) {
			SNDERR("Invalid ID for parent of object %s\n", id);
			return ret;
		}
	}

	map = tplg_object_get_map(tplg_pp, parent);
	if (!map) {
		SNDERR("Parent object %s not supported\n", parent_id);
		return -EINVAL;
	}

	/* find parent config with ID */
	ret = snd_config_search(tplg_pp->output_cfg, map->section_name, &section_cfg);
	if (ret < 0) {
		SNDERR("No SectionBE found\n");
		return ret;
	}

	top = tplg_find_config(section_cfg, parent_id);
	if (!top) {
		SNDERR("SectionBE %s not found\n", parent_id);
		return -EINVAL;
	}

	return tplg_add_object_data(tplg_pp, obj_cfg, top, id);
}

/*
 * Function to create a new "section" config based on the template. The new config will be
 * added to the output_cfg or the top_config input parameter.
 */
int tplg_build_object_from_template(struct tplg_pre_processor *tplg_pp, snd_config_t *obj_cfg,
				    snd_config_t **wtop, snd_config_t *top_config,
				    bool skip_name)
{
	snd_config_t *top, *template, *obj;
	const struct build_function_map *map;
	const char *object_name;
	int ret;

	/* look up object map */
	map = tplg_object_get_map(tplg_pp, obj_cfg);
	if (!map) {
		SNDERR("unknown object type or class name\n");
		return -EINVAL;
	}

	obj = tplg_object_get_instance_config(tplg_pp, obj_cfg);

	/* look up or create the corresponding section config for object */
	if (!top_config)
		top_config = tplg_pp->output_cfg;

	ret = snd_config_search(top_config, map->section_name, &top);
	if (ret < 0) {
		ret = tplg_config_make_add(&top, map->section_name, SND_CONFIG_TYPE_COMPOUND,
					   top_config);
		if (ret < 0) {
			SNDERR("Error creating %s config\n", map->section_name);
			return ret;
		}
	}

	/* get object name */
	object_name = tplg_object_get_name(tplg_pp, obj);
	if (!object_name) {
		ret = snd_config_get_id(obj, &object_name);
		if (ret < 0) {
			SNDERR("Invalid ID for %s\n", map->section_name);
			return ret;
		}
	}

	tplg_pp_debug("Building object: '%s' ...", object_name);

	/* create and add new object config with name, if needed */
	if (skip_name) {
		*wtop = top;
	} else {
		*wtop = tplg_find_config(top, object_name);
		if (*wtop)
			goto template;

		ret = tplg_config_make_add(wtop, object_name, SND_CONFIG_TYPE_COMPOUND,
					   top);
		if (ret < 0) {
			SNDERR("Error creating config for %s\n", object_name);
			return ret;
		}
	}

template:
	/* create template config */
	if (!map->template_items)
		return 0;

	ret = tplg_create_config_template(tplg_pp, &template, map->template_items);
	if (ret < 0) {
		SNDERR("Error creating template config for %s\n", object_name);
		return ret;
	}

	/* update section config based on template and the attribute values in the object */
	ret = tplg_object_add_attributes(*wtop, template, obj);
	snd_config_delete(template);
	if (ret < 0)
		SNDERR("Error adding attributes for object '%s'\n", object_name);

	return ret;
}

static int tplg_build_generic_object(struct tplg_pre_processor *tplg_pp, snd_config_t *obj_cfg,
				     snd_config_t *parent)
{
	snd_config_t *wtop;
	const char *name;
	int ret;

	ret = tplg_build_object_from_template(tplg_pp, obj_cfg, &wtop, NULL, false);
	if (ret < 0)
		return ret;

	ret = snd_config_get_id(wtop, &name);
	if (ret < 0)
		return ret;

	ret = tplg_add_object_data(tplg_pp, obj_cfg, wtop, NULL);
	if (ret < 0)
		SNDERR("Failed to add data section for %s\n", name);

	return ret;
}

const struct config_template_items pcm_caps_config = {
	.int_config_ids = {"rate_min", "rate_max", "channels_min", "channels_max", "periods_min",
			   "periods_max", "period_size_min", "period_size_max", "buffer_size_min",
			   "buffer_size_max", "sig_bits"},
	.string_config_ids = {"formats", "rates"},
};

const struct config_template_items fe_dai_config = {
	.int_config_ids = {"id"},
};

const struct config_template_items hwcfg_config = {
	.int_config_ids = {"id", "bclk_freq", "bclk_invert", "fsync_invert", "fsync_freq",
			   "mclk_freq", "pm_gate_clocks", "tdm_slots", "tdm_slot_width",
			   "tx_slots", "rx_slots", "tx_channels", "rx_channels"},
	.string_config_ids = {"format", "bclk", "fsync", "mclk"},
};

const struct config_template_items be_dai_config = {
	.int_config_ids = {"id", "default_hw_conf_id", "symmertic_rates", "symmetric_channels",
			   "symmetric_sample_bits"},
	.string_config_ids = {"stream_name"},
};

const struct config_template_items pcm_config = {
	.int_config_ids = {"id", "compress", "symmertic_rates", "symmetric_channels",
			   "symmetric_sample_bits"},
};

const struct config_template_items mixer_control_config = {
	.int_config_ids = {"index", "max", "invert"},
	.compound_config_ids = {"access"}
};

const struct config_template_items bytes_control_config = {
	.int_config_ids = {"index", "base", "num_regs", "max", "mask"},
	.compound_config_ids = {"access"}
};

const struct config_template_items scale_config = {
	.int_config_ids = {"min", "step", "mute"},
};

const struct config_template_items ops_config = {
	.int_config_ids = {"get", "put"},
	.string_config_ids = {"info"},
};

const struct config_template_items channel_config = {
	.int_config_ids = {"reg", "shift"},
};

const struct config_template_items widget_config = {
	.int_config_ids = {"index", "no_pm", "shift", "invert", "subseq", "event_type",
			    "event_flags"},
	.string_config_ids = {"type", "stream_name"},
};

const struct config_template_items data_config = {
	.string_config_ids = {"bytes"}
};

/*
 * Items without class name should be placed lower than those with one,
 * because they are much more generic.
 */
const struct build_function_map object_build_map[] = {
	{"Base", "manifest", "SectionManifest", &tplg_build_generic_object, NULL, NULL},
	{"Base", "data", "SectionData", &tplg_build_data_object, NULL, &data_config},
	{"Base", "tlv", "SectionTLV", &tplg_build_tlv_object, NULL, NULL},
	{"Base", "scale", "scale", &tplg_build_scale_object, NULL, &scale_config},
	{"Base", "ops", "ops" ,&tplg_build_ops_object, NULL, &ops_config},
	{"Base", "extops", "extops" ,&tplg_build_ops_object, NULL, &ops_config},
	{"Base", "channel", "channel", &tplg_build_channel_object, NULL, &channel_config},
	{"Base", "VendorToken", "SectionVendorTokens", &tplg_build_vendor_token_object,
	 NULL, NULL},
	{"Base", "hw_config", "SectionHWConfig", &tplg_build_hw_cfg_object, NULL,
	 &hwcfg_config},
	{"Base", "fe_dai", "dai", &tplg_build_fe_dai_object, NULL, &fe_dai_config},
	{"Base", "route", "SectionGraph", &tplg_build_dapm_route_object, NULL, NULL},
	{"Widget", "buffer", "SectionWidget", &tplg_build_generic_object, NULL, &widget_config},
	{"Widget", "", "SectionWidget", &tplg_build_generic_object, NULL, &widget_config},
	{"Control", "mixer", "SectionControlMixer", &tplg_build_mixer_control, NULL,
	 &mixer_control_config},
	{"Control", "bytes", "SectionControlBytes", &tplg_build_bytes_control, NULL,
	 &bytes_control_config},
	{"Dai", "", "SectionBE", &tplg_build_generic_object, NULL, &be_dai_config},
	{"PCM", "pcm", "SectionPCM", &tplg_build_generic_object, NULL, &pcm_config},
	{"PCM", "pcm_caps", "SectionPCMCapabilities", &tplg_build_pcm_caps_object,
	 NULL, &pcm_caps_config},
};

static const struct build_function_map *tplg_object_get_map(struct tplg_pre_processor *tplg_pp,
							    snd_config_t *obj)
{
	snd_config_iterator_t first;
	snd_config_t *class;
	const char *class_type, *class_name;
	unsigned int i;

	first = snd_config_iterator_first(obj);
	class = snd_config_iterator_entry(first);

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

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

	for (i = 0; i < ARRAY_SIZE(object_build_map); i++) {
		if (!strcmp(class_type, "Widget") &&
		    !strcmp(object_build_map[i].class_type, "Widget") &&
			!strcmp(object_build_map[i].class_name, ""))
			return &object_build_map[i];

		if (!strcmp(class_type, "Dai") &&
		    !strcmp(object_build_map[i].class_type, "Dai"))
			return &object_build_map[i];

		/* for other type objects, also match the object class_name */
		if (!strcmp(class_type, object_build_map[i].class_type) &&
		    !strcmp(object_build_map[i].class_name, class_name))
			return &object_build_map[i];
	}

	return NULL;
}

/* search for section name based on class type and name and return the config in output_cfg */
snd_config_t *tplg_object_get_section(struct tplg_pre_processor *tplg_pp, snd_config_t *class)
{
	const struct build_function_map *map;
	snd_config_t *cfg = NULL;
	int ret;

	map = tplg_object_get_map(tplg_pp, class);
	if (!map)
		return NULL;

	ret = snd_config_search(tplg_pp->output_cfg, map->section_name, &cfg);
	if (ret < 0)
		SNDERR("Section config for %s not found\n", map->section_name);

	return cfg;
}

/* return 1 if attribute not found in search_config, 0 on success and negative value on error */
static int tplg_object_copy_and_add_param(struct tplg_pre_processor *tplg_pp,
					  snd_config_t *obj,
					  snd_config_t *attr_cfg,
					  snd_config_t *search_config)
{
	snd_config_iterator_t first = snd_config_iterator_first(obj);
	snd_config_t *attr, *new, *first_cfg;
	const char *id, *search_id;
	int ret;

	first_cfg = snd_config_iterator_entry(first);

	if (snd_config_get_id(attr_cfg, &id) < 0)
		return 0;

	if (snd_config_get_id(search_config, &search_id) < 0)
		return 0;

	/* copy object value */
	ret = snd_config_search(search_config, id, &attr);
	if (ret < 0)
		return 1;

	ret = snd_config_copy(&new, attr);
	if (ret < 0) {
		SNDERR("error copying attribute '%s' value from %s\n", id, search_id);
		return ret;
	}

	if (first_cfg) {
		/* prepend the new config */
		ret = snd_config_add_before(first_cfg, new);
		if (ret < 0) {
			snd_config_delete(new);
			SNDERR("error prepending attribute '%s' value to %s\n", id, search_id);
		}
	} else {
		ret = snd_config_add(obj, new);
		if (ret < 0) {
			snd_config_delete(new);
			SNDERR("error adding attribute '%s' value to %s\n", id, search_id);
		}
	}

	return ret;
}

/*
 * Attribute values for an object can be set in one of the following in order of
 * precedence:
 * 1. Value set in object instance
 * 2. Default value set in the object's class definition
 * 3. Inherited value from the parent object
 * 4. Value set in the object instance embedded in the parent object
 * 5. Value set in the object instance embedded in the parent class definition
 */
static int tplg_object_update(struct tplg_pre_processor *tplg_pp, snd_config_t *obj,
			      snd_config_t *parent)
{
	snd_config_iterator_t i, next;
	snd_config_t *n, *cfg, *args;
	snd_config_t *obj_cfg, *class_cfg, *parent_obj;
	const char *obj_id, *class_name, *class_type;
	int ret;

	class_cfg = tplg_class_lookup(tplg_pp, obj);
	if (!class_cfg)
		return -EINVAL;

	/* find config for class attributes */
	ret = snd_config_search(class_cfg, "DefineAttribute", &args);
	if (ret < 0)
		return 0;

	if (snd_config_get_id(obj, &class_type) < 0)
		return 0;

	if (snd_config_get_id(class_cfg, &class_name) < 0)
		return 0;

	/* get obj cfg */
	obj_cfg = tplg_object_get_instance_config(tplg_pp, obj);
	if (snd_config_get_id(obj_cfg, &obj_id) < 0)
		return 0;

	/* copy and add attributes */
	snd_config_for_each(i, next, args) {
		snd_config_t *attr;
		const char *id;

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

		if (tplg_class_is_attribute_unique(id, class_cfg))
			continue;

		if (tplg_class_is_attribute_immutable(id, class_cfg))
			goto class;

		/* check if attribute value is set in the object */
		ret = snd_config_search(obj_cfg, id, &attr);
		if (ret < 0)
			goto class;
		continue;
class:
		/* search for attributes value in class */
		ret = tplg_object_copy_and_add_param(tplg_pp, obj_cfg, n, class_cfg);
		if (ret == 1) {
			if (tplg_class_is_attribute_immutable(id, class_cfg)) {
				SNDERR("Immutable attribute %s not set in class %s\n",
				       id, class_name);
				return -EINVAL;
			}
			goto parent;
		}
		else if (ret < 0)
			return ret;
		continue;
parent:
		/* search for attribute value in parent */
		if (!parent)
			goto parent_object;

		/* get parent obj cfg */
		parent_obj = tplg_object_get_instance_config(tplg_pp, parent);
		if (!parent_obj)
			goto parent_object;

		ret = tplg_object_copy_and_add_param(tplg_pp, obj_cfg, n, parent_obj);
		if (ret == 1)
			goto parent_object;
		else if (ret < 0)
			return ret;
		continue;
parent_object:
		if (!parent)
			goto parent_class;

		cfg = tplg_object_lookup_in_config(tplg_pp, parent_obj, class_type,
						   class_name, obj_id);
		if (!cfg)
			goto parent_class;

		ret = tplg_object_copy_and_add_param(tplg_pp, obj_cfg, n, cfg);
		if (ret == 1)
			goto parent_class;
		else if (ret < 0)
			return ret;
		continue;
parent_class:
		if (!parent)
			goto check;

		cfg = tplg_class_lookup(tplg_pp, parent);
		if (!cfg)
			return -EINVAL;

		cfg = tplg_object_lookup_in_config(tplg_pp, cfg, class_type,
						   class_name, obj_id);
		if (!cfg)
			goto check;

		ret = tplg_object_copy_and_add_param(tplg_pp, obj_cfg, n, cfg);
		if (ret == 1)
			goto check;
		else if (ret < 0)
			return ret;
		continue;
check:
		if (tplg_class_is_attribute_mandatory(id, class_cfg)) {
			SNDERR("Mandatory attribute %s not set for class %s\n", id, class_name);
			return -EINVAL;
		}
	}

	return 0;
}

static int tplg_object_pre_process_children(struct tplg_pre_processor *tplg_pp,
					    snd_config_t *parent, snd_config_t *cfg)
{
	snd_config_iterator_t i, next;
	snd_config_t *children, *n;
	int ret;

	ret = snd_config_search(cfg, "Object", &children);
	if (ret < 0)
		return 0;

	/* create all embedded objects */
	snd_config_for_each(i, next, children) {
		const char *id;

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

		ret = tplg_pre_process_objects(tplg_pp, n, parent);
		if (ret < 0)
			return ret;
	}

	return 0;
}

static int tplg_construct_object_name(struct tplg_pre_processor *tplg_pp, snd_config_t *obj,
				      snd_config_t *class_cfg)
{
	snd_config_iterator_t i, next;
	snd_config_t *args, *n;
	const char *id, *class_id, *obj_id, *s;
	char *new_name;
	int ret;

	/* find config for class constructor attributes. Nothing to do if not defined */
	ret = snd_config_search(class_cfg, "attributes.constructor", &args);
	if (ret < 0)
		return 0;

	/* set class name as the name prefix for the object */
	if (snd_config_get_id(obj, &obj_id) < 0)
		return -EINVAL;
	if (snd_config_get_id(class_cfg, &class_id) < 0)
		return -EINVAL;
	new_name = strdup(class_id);
	if (!new_name)
		return -ENOMEM;

	/* iterate through all class arguments and set object name */
	snd_config_for_each(i, next, args) {
		snd_config_t *arg;
		char *arg_value, *temp;

		n = snd_config_iterator_entry(i);

		if (snd_config_get_id(n, &id) < 0) {
			SNDERR("Invalid ID for constructor argument\n");
			ret = -EINVAL;
			goto err;
		}

		if (snd_config_get_string(n, &s) < 0) {
			SNDERR("Invalid value for constructor argument\n");
			ret = -EINVAL;
			goto err;
		}

		/* find and replace with value set in object */
		ret = snd_config_search(obj, s, &arg);
		if (ret < 0) {
			SNDERR("Argument %s not set for object '%s.%s'\n", s, class_id, obj_id);
			ret = -ENOENT;
			goto err;
		}

		/* concat arg value to object name. arg types must be either integer or string */
		switch (snd_config_get_type(arg)) {
		case SND_CONFIG_TYPE_INTEGER:
		{
			long v;
			ret = snd_config_get_integer(arg, &v);
			assert(ret >= 0);

			arg_value = tplg_snprintf("%ld", v);
			if (!arg_value) {
				ret = -ENOMEM;
				goto err;
			}
			break;
		}
		case SND_CONFIG_TYPE_STRING:
		{
			const char *s;

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

			arg_value = strdup(s);
			if (!arg_value) {
				ret = -ENOMEM;
				goto err;
			}
			break;
		}
		default:
			SNDERR("Argument '%s' in object '%s.%s' is not an integer or a string\n",
			       s, class_id, obj_id);
			ret = -EINVAL;
			goto err;
		}

		/* alloc and concat arg value to the name */
		temp = tplg_snprintf("%s.%s", new_name, arg_value);
		free(arg_value);
		if (!temp) {
			ret = -ENOMEM;
			goto err;
		}
		free(new_name);
		new_name = temp;
	}

	ret = snd_config_set_id(obj, new_name);
err:
	free(new_name);
	return ret;
}

/* set the attribute value by type */
static int tplg_set_attribute_value(snd_config_t *attr, const char *value)
{
	int err;
	snd_config_type_t type = snd_config_get_type(attr);

	switch (type) {
	case SND_CONFIG_TYPE_INTEGER:
	{
		long v;

		v = strtol(value, NULL, 10);
		err = snd_config_set_integer(attr, v);
		assert(err >= 0);
		break;
	}
	case SND_CONFIG_TYPE_INTEGER64:
	{
		long long v;

		v = strtoll(value, NULL, 10);
		err = snd_config_set_integer64(attr, v);
		assert(err >= 0);
		break;
	}
	case SND_CONFIG_TYPE_STRING:
	{
		err = snd_config_set_string(attr, value);
		assert(err >= 0);
		break;
	}
	default:
		return -EINVAL;
	}

	return 0;
}


/*
 * Find the unique attribute in the class definition and set its value and type.
 * Only string or integer types are allowed for unique values.
 */
static int tplg_object_set_unique_attribute(struct tplg_pre_processor *tplg_pp,
					    snd_config_t *obj, snd_config_t *class_cfg,
					    const char *id)
{
	snd_config_t *unique_attr, *new;
	const char *unique_name, *class_id;
	int ret;

	if (snd_config_get_id(class_cfg, &class_id) < 0)
		return 0;

	/* find config for class unique attribute */
	unique_name = tplg_class_get_unique_attribute_name(tplg_pp, class_cfg);
	if (!unique_name)
		return -ENOENT;

	/* find the unique attribute definition in the class */
	unique_attr = tplg_class_find_attribute_by_name(tplg_pp, class_cfg, unique_name);
	if (!unique_attr)
		return -ENOENT;

	/* override value if unique attribute is set in the object instance */
	ret = snd_config_search(obj, unique_name, &new);
	if (ret < 0) {
		ret = snd_config_make(&new, unique_name,
				      tplg_class_get_attribute_type(tplg_pp, unique_attr));
		if (ret < 0) {
			SNDERR("error creating new attribute cfg for object %s\n", id);
			return ret;
		}
		ret = snd_config_add(obj, new);
		if (ret < 0) {
			SNDERR("error adding new attribute cfg for object %s\n", id);
			return ret;
		}
	}

	ret = tplg_set_attribute_value(new, id);
	if (ret < 0) {
		SNDERR("error setting unique attribute cfg for object %s\n", id);
		return ret;
	}

	return ret;
}

/*
 * Helper function to get object instance config which is 2 nodes down from class_type config.
 * ex: Get the pointer to the config node with ID "0" from the input config Widget.pga.0 {}
 */
snd_config_t *tplg_object_get_instance_config(struct tplg_pre_processor *tplg_pp,
					snd_config_t *class_type)
{
	snd_config_iterator_t first;
	snd_config_t *cfg;

	first = snd_config_iterator_first(class_type);
	cfg = snd_config_iterator_entry(first);
	first = snd_config_iterator_first(cfg);
	return snd_config_iterator_entry(first);
}

#if SND_LIB_VER(1, 2, 5) < SND_LIB_VERSION
static int pre_process_find_variable(snd_config_t **dst, const char *str, snd_config_t *config)
{
	snd_config_iterator_t i, next;

	snd_config_for_each(i, next, config) {
		snd_config_t *n;
		const char *id;

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

		if (strcmp(id, str))
			continue;

		/* found definition, copy config */
		return snd_config_copy(dst, n);
	}

	return -EINVAL;
}

static int
pre_process_object_variables_expand_fcn(snd_config_t **dst, const char *str, void *private_data)
{

	struct tplg_pre_processor *tplg_pp = private_data;
	snd_config_t *object_cfg = tplg_pp->current_obj_cfg;
	snd_config_t *conf_defines;
	const char *object_id;
	int ret;

	ret = snd_config_search(tplg_pp->input_cfg, "Define", &conf_defines);
	if (ret < 0)
		return 0;

	/* find variable from global definitions first */
	ret = pre_process_find_variable(dst, str, conf_defines);
	if (ret >= 0)
		return ret;

	if (snd_config_get_id(object_cfg, &object_id) < 0)
		return -EINVAL;

	/* find variable from object attribute values if not found in global definitions */
	ret = pre_process_find_variable(dst, str, object_cfg);
	if (ret < 0)
		SNDERR("Failed to find definition for attribute %s in '%s' object\n",
		       str, object_id);

	return ret;
}

/*
 * Searches for the first '$VAR_NAME' or '$[<contents>]' occurrence in
 * *stringp. Allocates memory for it and copies it there. The
 * allocated string is returned in '*varname'. If there was a prefix
 * before $VAR_NAME, it is returned in '*prefix'. The *stringp is
 * moved forward to the char after the $VAR_NAME.
 *
 * The end of $VAR_NAME is the first char that is not alpha numeric, '_',
 * or '\0'.
 *
 * In '$[<contents>]' case all letters but '[' and ']' are allow in
 * any sequence. Nested '[]' is also allowed if the number if '[' and
 * ']' match.
 *
 * The function modifies *stringp, and *prefix - if not NULL - points
 * to the original *stringp, *varname - if not NULL - is malloced and
 * should be freed by the caller.
 *
 * Returns 0		if the *stringp was an empty string.
 *         1		if *prefix or *varname was set
 *         -ENOMEM	if malloc failed
 */
static int tplg_get_varname(char **stringp, char **prefix, char **varname)
{
	size_t prefix_len, varname_len = 0;

	*prefix = NULL;
	*varname = NULL;

	prefix_len = strcspn(*stringp, "$");
	*prefix = *stringp;
	(*stringp) += prefix_len;
	if (**stringp == '$') {
		if ((*stringp)[1] == '[') {
			int brackets = 1;
			varname_len = 1;

			do {
				varname_len += strcspn((*stringp) + varname_len + 1, "[]") + 1;
				if ((*stringp)[varname_len] == '[')
					brackets++;
				else if ((*stringp)[varname_len] == ']')
					brackets--;
				else
					break;
			}  while (brackets > 0);
			if (brackets != 0)
				return -EINVAL;
			varname_len++;
		} else {
			varname_len = 1;
			while (isalnum((*stringp)[varname_len]) || (*stringp)[varname_len] == '_')
				varname_len++;
		}
	}

	if (varname_len == 0 && prefix_len == 0)
		return 0;

	if (varname_len) {
		*varname = malloc(varname_len + 1);
		if (*varname == NULL)
			return -ENOMEM;
		strncpy(*varname, *stringp, varname_len);
		(*varname)[varname_len] = '\0';
		(*stringp) += varname_len;
	}

	if (prefix_len)
		(*prefix)[prefix_len] = '\0';
	else
		*prefix = NULL;

	return 1;
}

static int tplg_evaluate_config_string(struct tplg_pre_processor *tplg_pp,
				    snd_config_t **dst, const char *s, const char *id)
{
	char *str = strdup(s);
	char *varname, *prefix, *freep = str;
	int ret;

	if (!str)
		return -ENOMEM;

	*dst = NULL;

	/* split the string and expand global definitions or object attribute values */
	while (tplg_get_varname(&str, &prefix, &varname) == 1) {
		const char *current_str;
		char *temp;

		if (prefix) {
			if (*dst == NULL) {
				ret = snd_config_make(dst, id, SND_CONFIG_TYPE_STRING);
				if (ret < 0)
					goto out;
				ret = snd_config_set_string(*dst, prefix);
				if (ret < 0)
					goto out;
			} else {
				/* concat the prefix */
				snd_config_get_string(*dst, &current_str);
				temp = tplg_snprintf("%s%s", current_str, prefix);
				if (!temp) {
					ret = -ENOMEM;
					goto out;
				}

				ret = snd_config_set_string(*dst, temp);
				free(temp);
				if (ret < 0)
					goto out;
			}
		}

		if (varname) {
			snd_config_t *tmp_config;

			ret = snd_config_evaluate_string(&tmp_config, varname,
							 pre_process_object_variables_expand_fcn,
							 tplg_pp);
			if (ret < 0)
				goto out;

			if (*dst == NULL) {
				*dst = tmp_config;
			} else {
				char *ascii;

				snd_config_get_string(*dst, &current_str);

				ret = snd_config_get_ascii(tmp_config, &ascii);
				if (ret)
					goto out;

				temp = tplg_snprintf("%s%s", current_str, ascii);
				free(ascii);

				if (!temp) {
					ret = -ENOMEM;
					goto out;
				}

				ret = snd_config_set_string(*dst, temp);
				free(temp);
				snd_config_delete(tmp_config);
				if (ret < 0)
					goto out;
			}
			free(varname);
		}
	}

	free(freep);
	snd_config_set_id(*dst, id);

	return 0;
out:
	if (*dst)
		snd_config_delete(*dst);
	free(varname);
	free(freep);
	return ret;
}

#endif

/* build object config and its child objects recursively */
static int tplg_build_object(struct tplg_pre_processor *tplg_pp, snd_config_t *new_obj,
			      snd_config_t *parent)
{
	snd_config_t *obj_local, *class_cfg;
	const struct build_function_map *map;
	snd_config_iterator_t i, next;
	build_func builder;
	update_auto_attr_func auto_attr_updater;
	const char *id, *class_id;
	int ret;

	obj_local = tplg_object_get_instance_config(tplg_pp, new_obj);
	if (!obj_local)
		return -EINVAL;

	class_cfg = tplg_class_lookup(tplg_pp, new_obj);
	if (!class_cfg)
		return -EINVAL;

	if (snd_config_get_id(obj_local, &id) < 0)
		return 0;

	if (snd_config_get_id(class_cfg, &class_id) < 0)
		return 0;

	/* set unique attribute value */
	ret = tplg_object_set_unique_attribute(tplg_pp, obj_local, class_cfg, id);
	if (ret < 0) {
		SNDERR("error setting unique attribute value for '%s.%s'\n", class_id, id);
		return ret;
	}

	/* update object attributes and validate them */
	ret = tplg_object_update(tplg_pp, new_obj, parent);
	if (ret < 0) {
		SNDERR("Failed to update attributes for object '%s.%s'\n", class_id, id);
		return ret;
	}

#if SND_LIB_VER(1, 2, 5) < SND_LIB_VERSION
	tplg_pp_config_debug(tplg_pp, obj_local);

	/* expand all non-compound type child configs in object */
	snd_config_for_each(i, next, obj_local) {
		snd_config_t *n, *new, *class_attr;
		const char *id, *s;
		char *attr_config_name;

		n = snd_config_iterator_entry(i);

		if (snd_config_get_type(n) == SND_CONFIG_TYPE_COMPOUND)
			continue;

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

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

		if (!strstr(s, "$"))
			goto validate;

		tplg_pp->current_obj_cfg = obj_local;

		/* Expand definitions and object attribute references. */
		ret = tplg_evaluate_config_string(tplg_pp, &new, s, id);
		if (ret < 0) {
			SNDERR("Failed to evaluate attributes %s in %s, from '%s'\n",
			       id, class_id, s);
			return ret;
		}

		ret = snd_config_merge(n, new, true);
		if (ret < 0)
			return ret;
validate:
		/* validate attribute value */
		snd_config_get_id(n, &id);
		attr_config_name = tplg_snprintf("DefineAttribute.%s", id);
		if (!attr_config_name)
			return -ENOMEM;

		ret = snd_config_search(class_cfg, attr_config_name, &class_attr);
		free(attr_config_name);
		if (ret < 0)
			continue;

		if (!tplg_object_is_attribute_valid(tplg_pp, class_attr, n)) {
			SNDERR("Failed to validate attribute %s in %s\n", id, class_id);
			return -EINVAL;
		}
	}
#endif

	/* construct object name using class constructor */
	ret = tplg_construct_object_name(tplg_pp, obj_local, class_cfg);
	if (ret < 0) {
		SNDERR("Failed to construct object name for %s\n", id);
		return ret;
	}

	/*
	 * Build objects if object type is supported.
	 * If not, process object attributes and add to parent's data section
	 */
	map = tplg_object_get_map(tplg_pp, new_obj);
	if (map) {
		builder = map->builder;

		/* update automatic attribute for current object */
		auto_attr_updater = map->auto_attr_updater;
		if(auto_attr_updater) {
			ret = auto_attr_updater(tplg_pp, obj_local, parent);
			if (ret < 0) {
				SNDERR("Failed to update automatic attributes for %s\n", id);
				return ret;
			}
		}
	} else {
		builder = &tplg_build_parent_data;
	}

	ret = builder(tplg_pp, new_obj, parent);
	if (ret < 0)
		return ret;

	/* create child objects in the object instance */
	ret = tplg_object_pre_process_children(tplg_pp, new_obj, obj_local);
	if (ret < 0) {
		SNDERR("error processing child objects in object %s\n", id);
		return ret;
	}

	/* create child objects in the object's class definition */
	ret = tplg_object_pre_process_children(tplg_pp, new_obj, class_cfg);
	if (ret < 0)
		SNDERR("error processing child objects in class %s\n", class_id);

	return ret;
}

/* create top-level topology objects */
int tplg_pre_process_objects(struct tplg_pre_processor *tplg_pp, snd_config_t *cfg,
			     snd_config_t *parent)
{
	snd_config_iterator_t i, next, i2, next2;
	snd_config_t *n, *n2, *_obj_type, *_obj_class, *_obj;
	const char *id, *class_type, *class_name;
	int ret;

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

	/* create all objects of the same type and class */
	snd_config_for_each(i, next, cfg) {
		n = snd_config_iterator_entry(i);
		if (snd_config_get_id(n, &class_name) < 0)
			continue;
		snd_config_for_each(i2, next2, n) {
			snd_config_t *temp_n2;

			n2 = snd_config_iterator_entry(i2);
			if (snd_config_get_id(n2, &id) < 0) {
				SNDERR("Invalid id for object\n");
				return -EINVAL;
			}

			ret = snd_config_copy(&temp_n2, n2);
			if (ret < 0)
				return ret;

			/*
			 * An object declared within a class definition as follows:
			 * Class.Pipeline.volume-playback {
			 * 	Object.Widget.pga.0 {
			 * 		ramp_step_ms 250
            		 * 	}
			 * }
			 *
			 * While instantiating the volume-pipeline class, the pga object
			 * could be modified as follows:
			 * Object.Pipeline.volume-playback.0 {
			 * 	Object.Widget.pga.0 {
			 * 		format "s24le"
			 * 	}
			 * }
			 * When building the pga.0 object in the class definition, merge
			 * the attributes declared in the volume-playback.0 object to create
			 * a new config as follows to make sure that all attributes are
			 * set for the pga object.
			 * Object.Widget.pga.0 {
			 * 	ramp_step_ms 250
			 * 	format "s24le"
			 * }
			 */

			if (parent) {
				snd_config_t *parent_instance, *parent_obj, *temp;
				char *obj_cfg_name;

				obj_cfg_name = tplg_snprintf("%s%s.%s.%s", "Object.",
							     class_type, class_name, id);

				/* search for object instance in the parent */
				parent_instance = tplg_object_get_instance_config(tplg_pp, parent);
				if (!parent_instance)
					goto temp_cfg;

				ret = snd_config_search(parent_instance, obj_cfg_name, &parent_obj);
				free(obj_cfg_name);
				if (ret < 0)
					goto temp_cfg;

				/* don't merge if the object configs are the same */
				if (parent_obj == n2)
					goto temp_cfg;

				/* create a temp config copying the parent object config */
				ret = snd_config_copy(&temp, parent_obj);
				if (ret < 0) {
					snd_config_delete(temp_n2);
					return ret;
				}

				/*
				 * Merge parent object with the current object instance.
				 * temp will be deleted by merge
				 */
				ret = snd_config_merge(temp_n2, temp, false);
				if (ret < 0) {
					SNDERR("error merging parent object config for %s.%s.%s\n",
					       class_type, class_name, id);
					snd_config_delete(temp_n2);
					return ret;
				}
			}
temp_cfg:
			/* create a temp config for object with class type as the root node */
			ret = snd_config_make(&_obj_type, class_type, SND_CONFIG_TYPE_COMPOUND);
			if (ret < 0) {
				snd_config_delete(temp_n2);
				return ret;
			}

			ret = snd_config_make(&_obj_class, class_name, SND_CONFIG_TYPE_COMPOUND);
			if (ret < 0)
				goto err;

			ret = snd_config_add(_obj_type, _obj_class);
			if (ret < 0) {
				snd_config_delete(_obj_class);
				goto err;
			}

			ret = snd_config_copy(&_obj, temp_n2);
			if (ret < 0)
				goto err;

			ret = snd_config_add(_obj_class, _obj);
			if (ret < 0) {
				snd_config_delete(_obj);
				goto err;
			}

			/* Build the object now */
			ret = tplg_build_object(tplg_pp, _obj_type, parent);
			if (ret < 0)
				SNDERR("Error building object %s.%s.%s\n",
				       class_type, class_name, id);
err:
			snd_config_delete(temp_n2);
			snd_config_delete(_obj_type);
			if (ret < 0)
				return ret;
		}
	}

	return 0;
}