2 * This file is part of the libsigrok project.
4 * Copyright (C) 2014 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/libsigrok.h>
22 #include "libsigrok-internal.h"
25 #define LOG_PREFIX "output"
31 * Output module handling.
35 * @defgroup grp_output Output modules
37 * Output module handling.
39 * libsigrok supports several output modules for file formats such as binary,
40 * VCD, gnuplot, and so on. It provides an output API that frontends can use.
41 * New output modules can be added/implemented in libsigrok without having
42 * to change the frontends at all.
44 * All output modules are fed data in a stream. Devices that can stream data
45 * into libsigrok, instead of storing and then transferring the whole buffer,
46 * can thus generate output live.
48 * Output modules generate a newly allocated GString. The caller is then
49 * expected to free this with g_string_free() when finished with it.
55 extern SR_PRIV struct sr_output_module output_bits;
56 extern SR_PRIV struct sr_output_module output_hex;
57 extern SR_PRIV struct sr_output_module output_ascii;
58 extern SR_PRIV struct sr_output_module output_binary;
59 extern SR_PRIV struct sr_output_module output_vcd;
60 extern SR_PRIV struct sr_output_module output_ols;
61 extern SR_PRIV struct sr_output_module output_gnuplot;
62 extern SR_PRIV struct sr_output_module output_chronovu_la8;
63 extern SR_PRIV struct sr_output_module output_csv;
64 extern SR_PRIV struct sr_output_module output_analog;
65 extern SR_PRIV struct sr_output_module output_srzip;
66 extern SR_PRIV struct sr_output_module output_wav;
69 static const struct sr_output_module *output_module_list[] = {
86 * Returns a NULL-terminated list of all available output modules.
90 SR_API const struct sr_output_module **sr_output_list(void)
92 return output_module_list;
96 * Returns the specified output module's ID.
100 SR_API const char *sr_output_id_get(const struct sr_output_module *omod)
103 sr_err("Invalid output module NULL!");
111 * Returns the specified output module's name.
115 SR_API const char *sr_output_name_get(const struct sr_output_module *omod)
118 sr_err("Invalid output module NULL!");
126 * Returns the specified output module's description.
130 SR_API const char *sr_output_description_get(const struct sr_output_module *omod)
133 sr_err("Invalid output module NULL!");
141 * Returns the specified output module's file extensions typical for the file
142 * format, as a NULL terminated array, or returns a NULL pointer if there is
143 * no preferred extension.
144 * @note these are a suggestions only.
148 SR_API const char *const *sr_output_extensions_get(
149 const struct sr_output_module *omod)
152 sr_err("Invalid output module NULL!");
160 * Checks whether a given flag is set.
162 * @see sr_output_flag
165 SR_API gboolean sr_output_test_flag(const struct sr_output_module *omod,
168 return (flag & omod->flags);
172 * Return the output module with the specified ID, or NULL if no module
173 * with that id is found.
177 SR_API const struct sr_output_module *sr_output_find(char *id)
181 for (i = 0; output_module_list[i]; i++) {
182 if (!strcmp(output_module_list[i]->id, id))
183 return output_module_list[i];
190 * Returns a NULL-terminated array of struct sr_option, or NULL if the
191 * module takes no options.
193 * Each call to this function must be followed by a call to
194 * sr_output_options_free().
198 SR_API const struct sr_option **sr_output_options_get(const struct sr_output_module *omod)
200 const struct sr_option *mod_opts, **opts;
203 if (!omod || !omod->options)
206 mod_opts = omod->options();
208 for (size = 0; mod_opts[size].id; size++)
210 opts = g_malloc((size + 1) * sizeof(struct sr_option *));
212 for (i = 0; i < size; i++)
213 opts[i] = &mod_opts[i];
220 * After a call to sr_output_options_get(), this function cleans up all
221 * resources returned by that call.
225 SR_API void sr_output_options_free(const struct sr_option **options)
232 for (i = 0; options[i]; i++) {
233 if (options[i]->def) {
234 g_variant_unref(options[i]->def);
235 ((struct sr_option *)options[i])->def = NULL;
238 if (options[i]->values) {
239 g_slist_free_full(options[i]->values, (GDestroyNotify)g_variant_unref);
240 ((struct sr_option *)options[i])->values = NULL;
247 * Create a new output instance using the specified output module.
249 * <code>options</code> is a *HashTable with the keys corresponding with
250 * the module options' <code>id</code> field. The values should be GVariant
251 * pointers with sunk * references, of the same GVariantType as the option's
254 * The sr_dev_inst passed in can be used by the instance to determine
255 * channel names, samplerate, and so on.
259 SR_API const struct sr_output *sr_output_new(const struct sr_output_module *omod,
260 GHashTable *options, const struct sr_dev_inst *sdi,
261 const char *filename)
263 struct sr_output *op;
264 const struct sr_option *mod_opts;
265 const GVariantType *gvt;
266 GHashTable *new_opts;
271 op = g_malloc(sizeof(struct sr_output));
274 op->filename = g_strdup(filename);
276 new_opts = g_hash_table_new_full(g_str_hash, g_str_equal, g_free,
277 (GDestroyNotify)g_variant_unref);
279 mod_opts = omod->options();
280 for (i = 0; mod_opts[i].id; i++) {
281 if (options && g_hash_table_lookup_extended(options,
282 mod_opts[i].id, &key, &value)) {
283 /* Pass option along. */
284 gvt = g_variant_get_type(mod_opts[i].def);
285 if (!g_variant_is_of_type(value, gvt)) {
286 sr_err("Invalid type for '%s' option.",
291 g_hash_table_insert(new_opts, g_strdup(mod_opts[i].id),
292 g_variant_ref(value));
294 /* Option not given: insert the default value. */
295 g_hash_table_insert(new_opts, g_strdup(mod_opts[i].id),
296 g_variant_ref(mod_opts[i].def));
300 /* Make sure no invalid options were given. */
302 g_hash_table_iter_init(&iter, options);
303 while (g_hash_table_iter_next(&iter, &key, &value)) {
304 if (!g_hash_table_lookup(new_opts, key)) {
305 sr_err("Output module '%s' has no option '%s'",
306 omod->id, (char *)key);
307 g_hash_table_destroy(new_opts);
315 if (op->module->init && op->module->init(op, new_opts) != SR_OK) {
320 g_hash_table_destroy(new_opts);
326 * Send a packet to the specified output instance.
328 * The instance's output is returned as a newly allocated GString,
329 * which must be freed by the caller.
333 SR_API int sr_output_send(const struct sr_output *o,
334 const struct sr_datafeed_packet *packet, GString **out)
336 return o->module->receive(o, packet, out);
340 * Free the specified output instance and all associated resources.
344 SR_API int sr_output_free(const struct sr_output *o)
352 if (o->module->cleanup)
353 ret = o->module->cleanup((struct sr_output *)o);
354 g_free((char *)o->filename);