1 /*
2   Copyright(c) 2014-2015 Intel Corporation
3   All rights reserved.
4 
5   This library is free software; you can redistribute it and/or modify
6   it under the terms of the GNU Lesser General Public License as
7   published by the Free Software Foundation; either version 2.1 of
8   the License, or (at your option) any later version.
9 
10   This program is distributed in the hope that it will be useful,
11   but WITHOUT ANY WARRANTY; without even the implied warranty of
12   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   GNU Lesser General Public License for more details.
14 
15   Authors: Mengdong Lin <mengdong.lin@intel.com>
16            Yao Jin <yao.jin@intel.com>
17            Liam Girdwood <liam.r.girdwood@linux.intel.com>
18 
19 */
20 
21 #include "tplg_local.h"
22 
23 #define TEXT_SIZE_MAX \
24 	(SND_SOC_TPLG_NUM_TEXTS * SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
25 
parse_text_values(snd_config_t *cfg, struct tplg_elem *elem)26 static int parse_text_values(snd_config_t *cfg, struct tplg_elem *elem)
27 {
28 	struct tplg_texts *texts = elem->texts;
29 	snd_config_iterator_t i, next;
30 	snd_config_t *n;
31 	const char *value = NULL;
32 	int j = 0;
33 
34 	tplg_dbg(" Text Values: %s", elem->id);
35 
36 	snd_config_for_each(i, next, cfg) {
37 		n = snd_config_iterator_entry(i);
38 
39 		if (j == SND_SOC_TPLG_NUM_TEXTS) {
40 			tplg_dbg("text string number exceeds %d", j);
41 			return -ENOMEM;
42 		}
43 
44 		/* get value */
45 		if (snd_config_get_string(n, &value) < 0)
46 			continue;
47 
48 		snd_strlcpy(&texts->items[j][0], value,
49 			SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
50 		tplg_dbg("\t%s", &texts->items[j][0]);
51 
52 		j++;
53 	}
54 
55 	texts->num_items = j;
56 	return 0;
57 }
58 
59 /* Parse Text data */
tplg_parse_text(snd_tplg_t *tplg, snd_config_t *cfg, void *private ATTRIBUTE_UNUSED)60 int tplg_parse_text(snd_tplg_t *tplg, snd_config_t *cfg,
61 		    void *private ATTRIBUTE_UNUSED)
62 {
63 	snd_config_iterator_t i, next;
64 	snd_config_t *n;
65 	const char *id;
66 	int err = 0;
67 	struct tplg_elem *elem;
68 
69 	elem = tplg_elem_new_common(tplg, cfg, NULL, SND_TPLG_TYPE_TEXT);
70 	if (!elem)
71 		return -ENOMEM;
72 
73 	snd_config_for_each(i, next, cfg) {
74 
75 		n = snd_config_iterator_entry(i);
76 		if (snd_config_get_id(n, &id) < 0)
77 			continue;
78 
79 		if (strcmp(id, "values") == 0) {
80 			err = parse_text_values(n, elem);
81 			if (err < 0) {
82 				SNDERR("error: failed to parse text values");
83 				return err;
84 			}
85 			continue;
86 		}
87 	}
88 
89 	return err;
90 }
91 
92 /* save text data */
tplg_save_text(snd_tplg_t *tplg ATTRIBUTE_UNUSED, struct tplg_elem *elem, struct tplg_buf *dst, const char *pfx)93 int tplg_save_text(snd_tplg_t *tplg ATTRIBUTE_UNUSED,
94 		   struct tplg_elem *elem,
95 		   struct tplg_buf *dst, const char *pfx)
96 {
97 	struct tplg_texts *texts = elem->texts;
98 	unsigned int i;
99 	int err;
100 
101 	if (!texts || texts->num_items == 0)
102 		return 0;
103 	err = tplg_save_printf(dst, pfx, "'%s'.values [\n", elem->id);
104 	for (i = 0; err >= 0 && i < texts->num_items; i++)
105 		err = tplg_save_printf(dst, pfx, "\t'%s'\n", texts->items[i]);
106 	if (err >= 0)
107 		err = tplg_save_printf(dst, pfx, "]\n");
108 	return err;
109 }
110