2 * This file is part of the libsigrok project.
4 * Copyright (C) 2010-2012 Bert Vermeulen <bert@biot.com>
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include "libsigrok.h"
22 #include "libsigrok-internal.h"
24 #define LOG_PREFIX "output"
29 * Output module handling.
33 * @defgroup grp_output Output modules
35 * Output module handling.
37 * libsigrok supports several output modules for file formats such as binary,
38 * VCD, gnuplot, and so on. It provides an output API that frontends can use.
39 * New output modules can be added/implemented in libsigrok without having
40 * to change the frontends at all.
42 * All output modules are fed data in a stream. Devices that can stream data
43 * into libsigrok, instead of storing and then transferring the whole buffer,
44 * can thus generate output live.
46 * Output modules generate a newly allocated GString. The caller is then
47 * expected to free this with g_string_free() when finished with it.
53 extern SR_PRIV struct sr_output_module output_bits;
54 extern SR_PRIV struct sr_output_module output_hex;
55 extern SR_PRIV struct sr_output_module output_ascii;
56 extern SR_PRIV struct sr_output_module output_binary;
57 extern SR_PRIV struct sr_output_module output_vcd;
58 extern SR_PRIV struct sr_output_module output_ols;
59 extern SR_PRIV struct sr_output_module output_gnuplot;
60 extern SR_PRIV struct sr_output_module output_chronovu_la8;
61 extern SR_PRIV struct sr_output_module output_csv;
62 extern SR_PRIV struct sr_output_module output_analog;
63 extern SR_PRIV struct sr_output_module output_wav;
66 static const struct sr_output_module *output_module_list[] = {
82 * Returns a NULL-terminated list of all available output modules.
86 SR_API const struct sr_output_module **sr_output_list(void)
88 return output_module_list;
92 * Returns the specified output module's ID.
96 SR_API const char *sr_output_id_get(const struct sr_output_module *o)
99 sr_err("Invalid output module NULL!");
107 * Returns the specified output module's name.
111 SR_API const char *sr_output_name_get(const struct sr_output_module *o)
114 sr_err("Invalid output module NULL!");
122 * Returns the specified output module's description.
126 SR_API const char *sr_output_description_get(const struct sr_output_module *o)
129 sr_err("Invalid output module NULL!");
137 * Return the output module with the specified ID, or NULL if no module
138 * with that id is found.
142 SR_API const struct sr_output_module *sr_output_find(char *id)
146 for (i = 0; output_module_list[i]; i++) {
147 if (!strcmp(output_module_list[i]->id, id))
148 return output_module_list[i];
155 * Returns a NULL-terminated array of struct sr_option, or NULL if the
156 * module takes no options.
158 * Each call to this function must be followed by a call to
159 * sr_output_options_free().
163 SR_API const struct sr_option *sr_output_options_get(const struct sr_output_module *o)
166 if (!o || !o->options)
173 * After a call to sr_output_options_get(), this function cleans up all
174 * resources allocated by that call.
178 SR_API void sr_output_options_free(const struct sr_output_module *o)
180 struct sr_option *opt;
182 if (!o || !o->options)
185 for (opt = o->options(); opt->id; opt++) {
187 g_variant_unref(opt->def);
192 g_slist_free_full(opt->values, (GDestroyNotify)g_variant_unref);
199 * Create a new output instance using the specified output module.
201 * <code>options</code> is a *HashTable with the keys corresponding with
202 * the module options' <code>id</code> field. The values should be GVariant
203 * pointers with sunk * references, of the same GVariantType as the option's
206 * The sr_dev_inst passed in can be used by the instance to determine
207 * channel names, samplerate, and so on.
211 SR_API const struct sr_output *sr_output_new(const struct sr_output_module *o,
212 GHashTable *options, const struct sr_dev_inst *sdi)
214 struct sr_output *op;
215 struct sr_option *mod_opts;
216 const GVariantType *gvt;
217 GHashTable *new_opts;
222 op = g_malloc(sizeof(struct sr_output));
226 new_opts = g_hash_table_new_full(g_str_hash, g_str_equal, g_free,
227 (GDestroyNotify)g_variant_unref);
229 mod_opts = o->options();
230 for (i = 0; mod_opts[i].id; i++) {
231 if (options && g_hash_table_lookup_extended(options,
232 mod_opts[i].id, &key, &value)) {
233 /* Pass option along. */
234 gvt = g_variant_get_type(mod_opts[i].def);
235 if (!g_variant_is_of_type(value, gvt)) {
236 sr_err("Invalid type for '%s' option.", key);
240 g_hash_table_insert(new_opts, g_strdup(mod_opts[i].id),
241 g_variant_ref(value));
243 /* Option not given: insert the default value. */
244 g_hash_table_insert(new_opts, g_strdup(mod_opts[i].id),
245 g_variant_ref(mod_opts[i].def));
249 /* Make sure no invalid options were given. */
251 g_hash_table_iter_init(&iter, options);
252 while (g_hash_table_iter_next(&iter, &key, &value)) {
253 if (!g_hash_table_lookup(new_opts, key)) {
254 sr_err("Output module '%s' has no option '%s'", o->id, key);
255 g_hash_table_destroy(new_opts);
263 if (op->module->init && op->module->init(op, new_opts) != SR_OK) {
264 g_hash_table_destroy(new_opts);
269 g_hash_table_destroy(new_opts);
275 * Send a packet to the specified output instance.
277 * The instance's output is returned as a newly allocated GString,
278 * which must be freed by the caller.
282 SR_API int sr_output_send(const struct sr_output *o,
283 const struct sr_datafeed_packet *packet, GString **out)
285 return o->module->receive(o, packet, out);
289 * Free the specified output instance and all associated resources.
293 SR_API int sr_output_free(const struct sr_output *o)
301 if (o->module->cleanup)
302 ret = o->module->cleanup((struct sr_output *)o);