2 * This file is part of the libsigrok project.
4 * Copyright (C) 2014 Bert Vermeulen <bert@biot.com>
5 * Copyright (C) 2015 Uwe Hermann <uwe@hermann-uwe.de>
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.
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.
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/>.
22 #include "libsigrok.h"
23 #include "libsigrok-internal.h"
25 #define LOG_PREFIX "transform"
30 * Transform module handling.
34 * @defgroup grp_transform Transform modules
36 * Transform module handling.
42 extern SR_PRIV struct sr_transform_module transform_nop;
45 static const struct sr_transform_module *transform_module_list[] = {
51 * Returns a NULL-terminated list of all available transform modules.
55 SR_API const struct sr_transform_module **sr_transform_list(void)
57 return transform_module_list;
61 * Returns the specified transform module's ID.
65 SR_API const char *sr_transform_id_get(const struct sr_transform_module *tmod)
68 sr_err("Invalid transform module NULL!");
76 * Returns the specified transform module's name.
80 SR_API const char *sr_transform_name_get(const struct sr_transform_module *tmod)
83 sr_err("Invalid transform module NULL!");
91 * Returns the specified transform module's description.
95 SR_API const char *sr_transform_description_get(const struct sr_transform_module *tmod)
98 sr_err("Invalid transform module NULL!");
106 * Return the transform module with the specified ID, or NULL if no module
107 * with that ID is found.
111 SR_API const struct sr_transform_module *sr_transform_find(const char *id)
115 for (i = 0; transform_module_list[i]; i++) {
116 if (!strcmp(transform_module_list[i]->id, id))
117 return transform_module_list[i];
124 * Returns a NULL-terminated array of struct sr_option, or NULL if the
125 * module takes no options.
127 * Each call to this function must be followed by a call to
128 * sr_transform_options_free().
132 SR_API const struct sr_option **sr_transform_options_get(const struct sr_transform_module *tmod)
134 const struct sr_option *mod_opts, **opts;
137 if (!tmod || !tmod->options)
140 mod_opts = tmod->options();
142 for (size = 0; mod_opts[size].id; size++)
144 opts = g_malloc((size + 1) * sizeof(struct sr_option *));
146 for (i = 0; i < size; i++)
147 opts[i] = &mod_opts[i];
154 * After a call to sr_transform_options_get(), this function cleans up all
155 * resources returned by that call.
159 SR_API void sr_transform_options_free(const struct sr_option **options)
166 for (i = 0; options[i]; i++) {
167 if (options[i]->def) {
168 g_variant_unref(options[i]->def);
169 ((struct sr_option *)options[i])->def = NULL;
172 if (options[i]->values) {
173 g_slist_free_full(options[i]->values, (GDestroyNotify)g_variant_unref);
174 ((struct sr_option *)options[i])->values = NULL;
181 * Create a new transform instance using the specified transform module.
183 * <code>options</code> is a *GHashTable with the keys corresponding with
184 * the module options' <code>id</code> field. The values should be GVariant
185 * pointers with sunk * references, of the same GVariantType as the option's
188 * The sr_dev_inst passed in can be used by the instance to determine
189 * channel names, samplerate, and so on.
193 SR_API const struct sr_transform *sr_transform_new(const struct sr_transform_module *tmod,
194 GHashTable *options, const struct sr_dev_inst *sdi)
196 struct sr_transform *t;
197 const struct sr_option *mod_opts;
198 const GVariantType *gvt;
199 GHashTable *new_opts;
204 t = g_malloc(sizeof(struct sr_transform));
208 new_opts = g_hash_table_new_full(g_str_hash, g_str_equal, g_free,
209 (GDestroyNotify)g_variant_unref);
211 mod_opts = tmod->options();
212 for (i = 0; mod_opts[i].id; i++) {
213 if (options && g_hash_table_lookup_extended(options,
214 mod_opts[i].id, &key, &value)) {
215 /* Pass option along. */
216 gvt = g_variant_get_type(mod_opts[i].def);
217 if (!g_variant_is_of_type(value, gvt)) {
218 sr_err("Invalid type for '%s' option.", key);
222 g_hash_table_insert(new_opts, g_strdup(mod_opts[i].id),
223 g_variant_ref(value));
225 /* Option not given: insert the default value. */
226 g_hash_table_insert(new_opts, g_strdup(mod_opts[i].id),
227 g_variant_ref(mod_opts[i].def));
231 /* Make sure no invalid options were given. */
233 g_hash_table_iter_init(&iter, options);
234 while (g_hash_table_iter_next(&iter, &key, &value)) {
235 if (!g_hash_table_lookup(new_opts, key)) {
236 sr_err("Transform module '%s' has no option '%s'.", tmod->id, key);
237 g_hash_table_destroy(new_opts);
245 if (t->module->init && t->module->init(t, new_opts) != SR_OK) {
250 g_hash_table_destroy(new_opts);
252 /* Add the transform to the session's list of transforms. */
253 sdi->session->transforms = g_slist_append(sdi->session->transforms, t);
259 * Free the specified transform instance and all associated resources.
263 SR_API int sr_transform_free(const struct sr_transform *t)
271 if (t->module->cleanup)
272 ret = t->module->cleanup((struct sr_transform *)t);