]> sigrok.org Git - libsigrok.git/blame - src/output/output.c
output/wav: Improved description
[libsigrok.git] / src / output / output.c
CommitLineData
a1bb33af 1/*
50985c20 2 * This file is part of the libsigrok project.
a1bb33af 3 *
17bfaca6 4 * Copyright (C) 2014 Bert Vermeulen <bert@biot.com>
a1bb33af
UH
5 *
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.
10 *
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.
15 *
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/>.
18 */
19
a755b0e1 20#include <string.h>
45c59c8b
BV
21#include "libsigrok.h"
22#include "libsigrok-internal.h"
a1bb33af 23
a755b0e1
BV
24#define LOG_PREFIX "output"
25
393fb9cb
UH
26/**
27 * @file
28 *
a755b0e1 29 * Output module handling.
393fb9cb
UH
30 */
31
7b870c38 32/**
a755b0e1 33 * @defgroup grp_output Output modules
7b870c38 34 *
a755b0e1 35 * Output module handling.
7b870c38 36 *
a755b0e1
BV
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
07e1aad5
UH
40 * to change the frontends at all.
41 *
42 * All output modules are fed data in a stream. Devices that can stream data
dba3e682
BV
43 * into libsigrok, instead of storing and then transferring the whole buffer,
44 * can thus generate output live.
07e1aad5 45 *
dba3e682
BV
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.
07e1aad5 48 *
7b870c38
UH
49 * @{
50 */
51
b4bd7088 52/** @cond PRIVATE */
a755b0e1
BV
53extern SR_PRIV struct sr_output_module output_bits;
54extern SR_PRIV struct sr_output_module output_hex;
55extern SR_PRIV struct sr_output_module output_ascii;
56extern SR_PRIV struct sr_output_module output_binary;
57extern SR_PRIV struct sr_output_module output_vcd;
58extern SR_PRIV struct sr_output_module output_ols;
59extern SR_PRIV struct sr_output_module output_gnuplot;
60extern SR_PRIV struct sr_output_module output_chronovu_la8;
61extern SR_PRIV struct sr_output_module output_csv;
62extern SR_PRIV struct sr_output_module output_analog;
3250d8c7 63extern SR_PRIV struct sr_output_module output_srzip;
afaa75b9 64extern SR_PRIV struct sr_output_module output_wav;
b4bd7088 65/* @endcond */
a1bb33af 66
a755b0e1 67static const struct sr_output_module *output_module_list[] = {
6f64ebb2 68 &output_ascii,
1c5b9d30 69 &output_binary,
dba3e682
BV
70 &output_bits,
71 &output_csv,
e2ad47b5 72 &output_gnuplot,
dba3e682
BV
73 &output_hex,
74 &output_ols,
75 &output_vcd,
8c48f179 76 &output_chronovu_la8,
161a8a27 77 &output_analog,
3250d8c7 78 &output_srzip,
afaa75b9 79 &output_wav,
4c9ffa83 80 NULL,
a1bb33af
UH
81};
82
a755b0e1 83/**
63f6df68 84 * Returns a NULL-terminated list of all available output modules.
a755b0e1
BV
85 *
86 * @since 0.4.0
87 */
88SR_API const struct sr_output_module **sr_output_list(void)
a1bb33af 89{
a1bb33af
UH
90 return output_module_list;
91}
7b870c38 92
a755b0e1
BV
93/**
94 * Returns the specified output module's ID.
95 *
96 * @since 0.4.0
97 */
98SR_API const char *sr_output_id_get(const struct sr_output_module *o)
99{
100 if (!o) {
101 sr_err("Invalid output module NULL!");
102 return NULL;
103 }
104
105 return o->id;
106}
107
108/**
109 * Returns the specified output module's name.
110 *
111 * @since 0.4.0
112 */
113SR_API const char *sr_output_name_get(const struct sr_output_module *o)
114{
115 if (!o) {
116 sr_err("Invalid output module NULL!");
117 return NULL;
118 }
119
120 return o->name;
121}
122
123/**
124 * Returns the specified output module's description.
125 *
126 * @since 0.4.0
127 */
128SR_API const char *sr_output_description_get(const struct sr_output_module *o)
129{
130 if (!o) {
131 sr_err("Invalid output module NULL!");
132 return NULL;
133 }
134
135 return o->desc;
136}
137
8a174d23
JH
138/**
139 * Returns the specified output module's file extensions typical for the file
140 * format, as a NULL terminated array, or returns a NULL pointer if there is
141 * no preferred extension.
142 * @note these are a suggestions only.
143 *
144 * @since 0.4.0
145 */
146SR_API const char *const *sr_output_extensions_get(
147 const struct sr_output_module *o)
148{
149 if (!o) {
150 sr_err("Invalid output module NULL!");
151 return NULL;
152 }
153
154 return o->exts;
155}
156
a755b0e1
BV
157/**
158 * Return the output module with the specified ID, or NULL if no module
159 * with that id is found.
160 *
161 * @since 0.4.0
162 */
163SR_API const struct sr_output_module *sr_output_find(char *id)
164{
165 int i;
166
167 for (i = 0; output_module_list[i]; i++) {
168 if (!strcmp(output_module_list[i]->id, id))
169 return output_module_list[i];
170 }
171
172 return NULL;
173}
174
175/**
176 * Returns a NULL-terminated array of struct sr_option, or NULL if the
177 * module takes no options.
178 *
179 * Each call to this function must be followed by a call to
180 * sr_output_options_free().
181 *
182 * @since 0.4.0
183 */
fc746430 184SR_API const struct sr_option **sr_output_options_get(const struct sr_output_module *o)
a755b0e1 185{
fc746430
BV
186 const struct sr_option *mod_opts, **opts;
187 int size, i;
a755b0e1
BV
188
189 if (!o || !o->options)
190 return NULL;
191
fc746430
BV
192 mod_opts = o->options();
193
fdefc40a 194 for (size = 0; mod_opts[size].id; size++)
fc746430 195 ;
879dd50f 196 opts = g_malloc((size + 1) * sizeof(struct sr_option *));
fc746430
BV
197
198 for (i = 0; i < size; i++)
199 opts[i] = &mod_opts[i];
200 opts[i] = NULL;
201
202 return opts;
a755b0e1
BV
203}
204
205/**
206 * After a call to sr_output_options_get(), this function cleans up all
fc746430 207 * resources returned by that call.
a755b0e1
BV
208 *
209 * @since 0.4.0
210 */
499c85dc 211SR_API void sr_output_options_free(const struct sr_option **options)
dba3e682 212{
499c85dc 213 int i;
a755b0e1 214
499c85dc 215 if (!options)
a755b0e1
BV
216 return;
217
499c85dc
BV
218 for (i = 0; options[i]; i++) {
219 if (options[i]->def) {
220 g_variant_unref(options[i]->def);
221 ((struct sr_option *)options[i])->def = NULL;
a755b0e1
BV
222 }
223
499c85dc
BV
224 if (options[i]->values) {
225 g_slist_free_full(options[i]->values, (GDestroyNotify)g_variant_unref);
226 ((struct sr_option *)options[i])->values = NULL;
a755b0e1 227 }
dba3e682 228 }
499c85dc 229 g_free(options);
a755b0e1 230}
dba3e682 231
a755b0e1
BV
232/**
233 * Create a new output instance using the specified output module.
234 *
235 * <code>options</code> is a *HashTable with the keys corresponding with
236 * the module options' <code>id</code> field. The values should be GVariant
237 * pointers with sunk * references, of the same GVariantType as the option's
238 * default value.
239 *
240 * The sr_dev_inst passed in can be used by the instance to determine
241 * channel names, samplerate, and so on.
242 *
243 * @since 0.4.0
244 */
245SR_API const struct sr_output *sr_output_new(const struct sr_output_module *o,
246 GHashTable *options, const struct sr_dev_inst *sdi)
247{
248 struct sr_output *op;
af7d656d 249 const struct sr_option *mod_opts;
dcc55fe9
BV
250 const GVariantType *gvt;
251 GHashTable *new_opts;
252 GHashTableIter iter;
253 gpointer key, value;
254 int i;
a755b0e1
BV
255
256 op = g_malloc(sizeof(struct sr_output));
257 op->module = o;
258 op->sdi = sdi;
950043c3 259
63f6df68
BV
260 new_opts = g_hash_table_new_full(g_str_hash, g_str_equal, g_free,
261 (GDestroyNotify)g_variant_unref);
262 if (o->options) {
dcc55fe9
BV
263 mod_opts = o->options();
264 for (i = 0; mod_opts[i].id; i++) {
63f6df68
BV
265 if (options && g_hash_table_lookup_extended(options,
266 mod_opts[i].id, &key, &value)) {
267 /* Pass option along. */
dcc55fe9
BV
268 gvt = g_variant_get_type(mod_opts[i].def);
269 if (!g_variant_is_of_type(value, gvt)) {
270 sr_err("Invalid type for '%s' option.", key);
271 g_free(op);
272 return NULL;
273 }
274 g_hash_table_insert(new_opts, g_strdup(mod_opts[i].id),
275 g_variant_ref(value));
276 } else {
63f6df68 277 /* Option not given: insert the default value. */
dcc55fe9
BV
278 g_hash_table_insert(new_opts, g_strdup(mod_opts[i].id),
279 g_variant_ref(mod_opts[i].def));
280 }
281 }
282
283 /* Make sure no invalid options were given. */
63f6df68
BV
284 if (options) {
285 g_hash_table_iter_init(&iter, options);
286 while (g_hash_table_iter_next(&iter, &key, &value)) {
287 if (!g_hash_table_lookup(new_opts, key)) {
288 sr_err("Output module '%s' has no option '%s'", o->id, key);
289 g_hash_table_destroy(new_opts);
290 g_free(op);
291 return NULL;
292 }
dcc55fe9
BV
293 }
294 }
63f6df68 295 }
dcc55fe9
BV
296
297 if (op->module->init && op->module->init(op, new_opts) != SR_OK) {
a755b0e1
BV
298 g_free(op);
299 op = NULL;
300 }
706f482a
BV
301 if (new_opts)
302 g_hash_table_destroy(new_opts);
a755b0e1
BV
303
304 return op;
dba3e682
BV
305}
306
a755b0e1
BV
307/**
308 * Send a packet to the specified output instance.
309 *
310 * The instance's output is returned as a newly allocated GString,
311 * which must be freed by the caller.
312 *
313 * @since 0.4.0
314 */
315SR_API int sr_output_send(const struct sr_output *o,
dba3e682
BV
316 const struct sr_datafeed_packet *packet, GString **out)
317{
a755b0e1 318 return o->module->receive(o, packet, out);
dba3e682
BV
319}
320
a755b0e1
BV
321/**
322 * Free the specified output instance and all associated resources.
323 *
324 * @since 0.4.0
325 */
326SR_API int sr_output_free(const struct sr_output *o)
dba3e682
BV
327{
328 int ret;
329
a755b0e1
BV
330 if (!o)
331 return SR_ERR_ARG;
332
dba3e682 333 ret = SR_OK;
a755b0e1
BV
334 if (o->module->cleanup)
335 ret = o->module->cleanup((struct sr_output *)o);
336 g_free((gpointer)o);
dba3e682
BV
337
338 return ret;
339}
340
7b870c38 341/** @} */