]>
Commit | Line | Data |
---|---|---|
988357ca UH |
1 | /* |
2 | * This file is part of the libsigrok project. | |
3 | * | |
4 | * Copyright (C) 2014 Bert Vermeulen <bert@biot.com> | |
5 | * Copyright (C) 2015 Uwe Hermann <uwe@hermann-uwe.de> | |
6 | * | |
7 | * This program is free software: you can redistribute it and/or modify | |
8 | * it under the terms of the GNU General Public License as published by | |
9 | * the Free Software Foundation, either version 3 of the License, or | |
10 | * (at your option) any later version. | |
11 | * | |
12 | * This program is distributed in the hope that it will be useful, | |
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | * GNU General Public License for more details. | |
16 | * | |
17 | * You should have received a copy of the GNU General Public License | |
18 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
19 | */ | |
20 | ||
21 | #include <string.h> | |
22 | #include "libsigrok.h" | |
23 | #include "libsigrok-internal.h" | |
24 | ||
25 | #define LOG_PREFIX "transform" | |
26 | ||
27 | /** | |
28 | * @file | |
29 | * | |
30 | * Transform module handling. | |
31 | */ | |
32 | ||
33 | /** | |
34 | * @defgroup grp_transform Transform modules | |
35 | * | |
36 | * Transform module handling. | |
37 | * | |
38 | * @{ | |
39 | */ | |
40 | ||
41 | /** @cond PRIVATE */ | |
39f1752e | 42 | extern SR_PRIV struct sr_transform_module transform_nop; |
43caa466 | 43 | extern SR_PRIV struct sr_transform_module transform_scale; |
988357ca UH |
44 | /* @endcond */ |
45 | ||
46 | static const struct sr_transform_module *transform_module_list[] = { | |
39f1752e | 47 | &transform_nop, |
43caa466 | 48 | &transform_scale, |
988357ca UH |
49 | NULL, |
50 | }; | |
51 | ||
52 | /** | |
53 | * Returns a NULL-terminated list of all available transform modules. | |
54 | * | |
55 | * @since 0.4.0 | |
56 | */ | |
57 | SR_API const struct sr_transform_module **sr_transform_list(void) | |
58 | { | |
59 | return transform_module_list; | |
60 | } | |
61 | ||
62 | /** | |
63 | * Returns the specified transform module's ID. | |
64 | * | |
65 | * @since 0.4.0 | |
66 | */ | |
67 | SR_API const char *sr_transform_id_get(const struct sr_transform_module *tmod) | |
68 | { | |
69 | if (!tmod) { | |
70 | sr_err("Invalid transform module NULL!"); | |
71 | return NULL; | |
72 | } | |
73 | ||
74 | return tmod->id; | |
75 | } | |
76 | ||
77 | /** | |
78 | * Returns the specified transform module's name. | |
79 | * | |
80 | * @since 0.4.0 | |
81 | */ | |
82 | SR_API const char *sr_transform_name_get(const struct sr_transform_module *tmod) | |
83 | { | |
84 | if (!tmod) { | |
85 | sr_err("Invalid transform module NULL!"); | |
86 | return NULL; | |
87 | } | |
88 | ||
89 | return tmod->name; | |
90 | } | |
91 | ||
92 | /** | |
93 | * Returns the specified transform module's description. | |
94 | * | |
95 | * @since 0.4.0 | |
96 | */ | |
97 | SR_API const char *sr_transform_description_get(const struct sr_transform_module *tmod) | |
98 | { | |
99 | if (!tmod) { | |
100 | sr_err("Invalid transform module NULL!"); | |
101 | return NULL; | |
102 | } | |
103 | ||
104 | return tmod->desc; | |
105 | } | |
106 | ||
107 | /** | |
108 | * Return the transform module with the specified ID, or NULL if no module | |
109 | * with that ID is found. | |
110 | * | |
111 | * @since 0.4.0 | |
112 | */ | |
113 | SR_API const struct sr_transform_module *sr_transform_find(const char *id) | |
114 | { | |
115 | int i; | |
116 | ||
117 | for (i = 0; transform_module_list[i]; i++) { | |
118 | if (!strcmp(transform_module_list[i]->id, id)) | |
119 | return transform_module_list[i]; | |
120 | } | |
121 | ||
122 | return NULL; | |
123 | } | |
124 | ||
125 | /** | |
126 | * Returns a NULL-terminated array of struct sr_option, or NULL if the | |
127 | * module takes no options. | |
128 | * | |
129 | * Each call to this function must be followed by a call to | |
130 | * sr_transform_options_free(). | |
131 | * | |
132 | * @since 0.4.0 | |
133 | */ | |
134 | SR_API const struct sr_option **sr_transform_options_get(const struct sr_transform_module *tmod) | |
135 | { | |
136 | const struct sr_option *mod_opts, **opts; | |
137 | int size, i; | |
138 | ||
139 | if (!tmod || !tmod->options) | |
140 | return NULL; | |
141 | ||
142 | mod_opts = tmod->options(); | |
143 | ||
144 | for (size = 0; mod_opts[size].id; size++) | |
145 | ; | |
146 | opts = g_malloc((size + 1) * sizeof(struct sr_option *)); | |
147 | ||
148 | for (i = 0; i < size; i++) | |
149 | opts[i] = &mod_opts[i]; | |
150 | opts[i] = NULL; | |
151 | ||
152 | return opts; | |
153 | } | |
154 | ||
155 | /** | |
156 | * After a call to sr_transform_options_get(), this function cleans up all | |
157 | * resources returned by that call. | |
158 | * | |
159 | * @since 0.4.0 | |
160 | */ | |
161 | SR_API void sr_transform_options_free(const struct sr_option **options) | |
162 | { | |
163 | int i; | |
164 | ||
165 | if (!options) | |
166 | return; | |
167 | ||
168 | for (i = 0; options[i]; i++) { | |
169 | if (options[i]->def) { | |
170 | g_variant_unref(options[i]->def); | |
171 | ((struct sr_option *)options[i])->def = NULL; | |
172 | } | |
173 | ||
174 | if (options[i]->values) { | |
175 | g_slist_free_full(options[i]->values, (GDestroyNotify)g_variant_unref); | |
176 | ((struct sr_option *)options[i])->values = NULL; | |
177 | } | |
178 | } | |
179 | g_free(options); | |
180 | } | |
181 | ||
182 | /** | |
183 | * Create a new transform instance using the specified transform module. | |
184 | * | |
185 | * <code>options</code> is a *GHashTable with the keys corresponding with | |
186 | * the module options' <code>id</code> field. The values should be GVariant | |
187 | * pointers with sunk * references, of the same GVariantType as the option's | |
188 | * default value. | |
189 | * | |
190 | * The sr_dev_inst passed in can be used by the instance to determine | |
191 | * channel names, samplerate, and so on. | |
192 | * | |
193 | * @since 0.4.0 | |
194 | */ | |
195 | SR_API const struct sr_transform *sr_transform_new(const struct sr_transform_module *tmod, | |
196 | GHashTable *options, const struct sr_dev_inst *sdi) | |
197 | { | |
198 | struct sr_transform *t; | |
199 | const struct sr_option *mod_opts; | |
200 | const GVariantType *gvt; | |
201 | GHashTable *new_opts; | |
202 | GHashTableIter iter; | |
203 | gpointer key, value; | |
204 | int i; | |
205 | ||
206 | t = g_malloc(sizeof(struct sr_transform)); | |
207 | t->module = tmod; | |
208 | t->sdi = sdi; | |
209 | ||
210 | new_opts = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, | |
211 | (GDestroyNotify)g_variant_unref); | |
212 | if (tmod->options) { | |
213 | mod_opts = tmod->options(); | |
214 | for (i = 0; mod_opts[i].id; i++) { | |
215 | if (options && g_hash_table_lookup_extended(options, | |
216 | mod_opts[i].id, &key, &value)) { | |
217 | /* Pass option along. */ | |
218 | gvt = g_variant_get_type(mod_opts[i].def); | |
219 | if (!g_variant_is_of_type(value, gvt)) { | |
220 | sr_err("Invalid type for '%s' option.", key); | |
221 | g_free(t); | |
222 | return NULL; | |
223 | } | |
224 | g_hash_table_insert(new_opts, g_strdup(mod_opts[i].id), | |
225 | g_variant_ref(value)); | |
226 | } else { | |
227 | /* Option not given: insert the default value. */ | |
228 | g_hash_table_insert(new_opts, g_strdup(mod_opts[i].id), | |
229 | g_variant_ref(mod_opts[i].def)); | |
230 | } | |
231 | } | |
232 | ||
233 | /* Make sure no invalid options were given. */ | |
234 | if (options) { | |
235 | g_hash_table_iter_init(&iter, options); | |
236 | while (g_hash_table_iter_next(&iter, &key, &value)) { | |
237 | if (!g_hash_table_lookup(new_opts, key)) { | |
238 | sr_err("Transform module '%s' has no option '%s'.", tmod->id, key); | |
239 | g_hash_table_destroy(new_opts); | |
240 | g_free(t); | |
241 | return NULL; | |
242 | } | |
243 | } | |
244 | } | |
245 | } | |
246 | ||
247 | if (t->module->init && t->module->init(t, new_opts) != SR_OK) { | |
248 | g_free(t); | |
249 | t = NULL; | |
250 | } | |
251 | if (new_opts) | |
252 | g_hash_table_destroy(new_opts); | |
253 | ||
c0a1e532 UH |
254 | /* Add the transform to the session's list of transforms. */ |
255 | sdi->session->transforms = g_slist_append(sdi->session->transforms, t); | |
256 | ||
988357ca UH |
257 | return t; |
258 | } | |
259 | ||
260 | /** | |
261 | * Free the specified transform instance and all associated resources. | |
262 | * | |
263 | * @since 0.4.0 | |
264 | */ | |
265 | SR_API int sr_transform_free(const struct sr_transform *t) | |
266 | { | |
267 | int ret; | |
268 | ||
269 | if (!t) | |
270 | return SR_ERR_ARG; | |
271 | ||
272 | ret = SR_OK; | |
273 | if (t->module->cleanup) | |
274 | ret = t->module->cleanup((struct sr_transform *)t); | |
275 | g_free((gpointer)t); | |
276 | ||
277 | return ret; | |
278 | } | |
279 | ||
280 | /** @} */ |