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 "list.h"
22 #include "tplg_local.h"
23
24 #define TEXT_SIZE_MAX \
25 (SND_SOC_TPLG_NUM_TEXTS * SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
26
parse_text_values(snd_config_t * cfg,struct tplg_elem * elem)27 static int parse_text_values(snd_config_t *cfg, struct tplg_elem *elem)
28 {
29 struct tplg_texts *texts = elem->texts;
30 snd_config_iterator_t i, next;
31 snd_config_t *n;
32 const char *value = NULL;
33 int j = 0;
34
35 tplg_dbg(" Text Values: %s", elem->id);
36
37 snd_config_for_each(i, next, cfg) {
38 n = snd_config_iterator_entry(i);
39
40 if (j == SND_SOC_TPLG_NUM_TEXTS) {
41 tplg_dbg("text string number exceeds %d", j);
42 return -ENOMEM;
43 }
44
45 /* get value */
46 if (snd_config_get_string(n, &value) < 0)
47 continue;
48
49 snd_strlcpy(&texts->items[j][0], value,
50 SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
51 tplg_dbg("\t%s", &texts->items[j][0]);
52
53 j++;
54 }
55
56 texts->num_items = j;
57 return 0;
58 }
59
60 /* Parse Text data */
tplg_parse_text(snd_tplg_t * tplg,snd_config_t * cfg,void * private ATTRIBUTE_UNUSED)61 int tplg_parse_text(snd_tplg_t *tplg, snd_config_t *cfg,
62 void *private ATTRIBUTE_UNUSED)
63 {
64 snd_config_iterator_t i, next;
65 snd_config_t *n;
66 const char *id;
67 int err = 0;
68 struct tplg_elem *elem;
69
70 elem = tplg_elem_new_common(tplg, cfg, NULL, SND_TPLG_TYPE_TEXT);
71 if (!elem)
72 return -ENOMEM;
73
74 snd_config_for_each(i, next, cfg) {
75
76 n = snd_config_iterator_entry(i);
77 if (snd_config_get_id(n, &id) < 0)
78 continue;
79
80 if (strcmp(id, "values") == 0) {
81 err = parse_text_values(n, elem);
82 if (err < 0) {
83 SNDERR("error: failed to parse text values");
84 return err;
85 }
86 continue;
87 }
88 }
89
90 return err;
91 }
92
93 /* save text data */
tplg_save_text(snd_tplg_t * tplg ATTRIBUTE_UNUSED,struct tplg_elem * elem,struct tplg_buf * dst,const char * pfx)94 int tplg_save_text(snd_tplg_t *tplg ATTRIBUTE_UNUSED,
95 struct tplg_elem *elem,
96 struct tplg_buf *dst, const char *pfx)
97 {
98 struct tplg_texts *texts = elem->texts;
99 unsigned int i;
100 int err;
101
102 if (!texts || texts->num_items == 0)
103 return 0;
104 err = tplg_save_printf(dst, pfx, "'%s'.values [\n", elem->id);
105 for (i = 0; err >= 0 && i < texts->num_items; i++)
106 err = tplg_save_printf(dst, pfx, "\t'%s'\n", texts->items[i]);
107 if (err >= 0)
108 err = tplg_save_printf(dst, pfx, "]\n");
109 return err;
110 }
111