]> sigrok.org Git - libsigrok.git/blame_incremental - src/output/output.c
input: Added preferred file extension field
[libsigrok.git] / src / output / output.c
... / ...
CommitLineData
1/*
2 * This file is part of the libsigrok project.
3 *
4 * Copyright (C) 2014 Bert Vermeulen <bert@biot.com>
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
20#include <string.h>
21#include "libsigrok.h"
22#include "libsigrok-internal.h"
23
24#define LOG_PREFIX "output"
25
26/**
27 * @file
28 *
29 * Output module handling.
30 */
31
32/**
33 * @defgroup grp_output Output modules
34 *
35 * Output module handling.
36 *
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.
41 *
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.
45 *
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.
48 *
49 * @{
50 */
51
52/** @cond PRIVATE */
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;
63extern SR_PRIV struct sr_output_module output_srzip;
64extern SR_PRIV struct sr_output_module output_wav;
65/* @endcond */
66
67static const struct sr_output_module *output_module_list[] = {
68 &output_ascii,
69 &output_binary,
70 &output_bits,
71 &output_csv,
72 &output_gnuplot,
73 &output_hex,
74 &output_ols,
75 &output_vcd,
76 &output_chronovu_la8,
77 &output_analog,
78 &output_srzip,
79 &output_wav,
80 NULL,
81};
82
83/**
84 * Returns a NULL-terminated list of all available output modules.
85 *
86 * @since 0.4.0
87 */
88SR_API const struct sr_output_module **sr_output_list(void)
89{
90 return output_module_list;
91}
92
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
138/**
139 * Return the output module with the specified ID, or NULL if no module
140 * with that id is found.
141 *
142 * @since 0.4.0
143 */
144SR_API const struct sr_output_module *sr_output_find(char *id)
145{
146 int i;
147
148 for (i = 0; output_module_list[i]; i++) {
149 if (!strcmp(output_module_list[i]->id, id))
150 return output_module_list[i];
151 }
152
153 return NULL;
154}
155
156/**
157 * Returns a NULL-terminated array of struct sr_option, or NULL if the
158 * module takes no options.
159 *
160 * Each call to this function must be followed by a call to
161 * sr_output_options_free().
162 *
163 * @since 0.4.0
164 */
165SR_API const struct sr_option **sr_output_options_get(const struct sr_output_module *o)
166{
167 const struct sr_option *mod_opts, **opts;
168 int size, i;
169
170 if (!o || !o->options)
171 return NULL;
172
173 mod_opts = o->options();
174
175 for (size = 0; mod_opts[size].id; size++)
176 ;
177 opts = g_malloc((size + 1) * sizeof(struct sr_option *));
178
179 for (i = 0; i < size; i++)
180 opts[i] = &mod_opts[i];
181 opts[i] = NULL;
182
183 return opts;
184}
185
186/**
187 * After a call to sr_output_options_get(), this function cleans up all
188 * resources returned by that call.
189 *
190 * @since 0.4.0
191 */
192SR_API void sr_output_options_free(const struct sr_option **options)
193{
194 int i;
195
196 if (!options)
197 return;
198
199 for (i = 0; options[i]; i++) {
200 if (options[i]->def) {
201 g_variant_unref(options[i]->def);
202 ((struct sr_option *)options[i])->def = NULL;
203 }
204
205 if (options[i]->values) {
206 g_slist_free_full(options[i]->values, (GDestroyNotify)g_variant_unref);
207 ((struct sr_option *)options[i])->values = NULL;
208 }
209 }
210 g_free(options);
211}
212
213/**
214 * Create a new output instance using the specified output module.
215 *
216 * <code>options</code> is a *HashTable with the keys corresponding with
217 * the module options' <code>id</code> field. The values should be GVariant
218 * pointers with sunk * references, of the same GVariantType as the option's
219 * default value.
220 *
221 * The sr_dev_inst passed in can be used by the instance to determine
222 * channel names, samplerate, and so on.
223 *
224 * @since 0.4.0
225 */
226SR_API const struct sr_output *sr_output_new(const struct sr_output_module *o,
227 GHashTable *options, const struct sr_dev_inst *sdi)
228{
229 struct sr_output *op;
230 const struct sr_option *mod_opts;
231 const GVariantType *gvt;
232 GHashTable *new_opts;
233 GHashTableIter iter;
234 gpointer key, value;
235 int i;
236
237 op = g_malloc(sizeof(struct sr_output));
238 op->module = o;
239 op->sdi = sdi;
240
241 new_opts = g_hash_table_new_full(g_str_hash, g_str_equal, g_free,
242 (GDestroyNotify)g_variant_unref);
243 if (o->options) {
244 mod_opts = o->options();
245 for (i = 0; mod_opts[i].id; i++) {
246 if (options && g_hash_table_lookup_extended(options,
247 mod_opts[i].id, &key, &value)) {
248 /* Pass option along. */
249 gvt = g_variant_get_type(mod_opts[i].def);
250 if (!g_variant_is_of_type(value, gvt)) {
251 sr_err("Invalid type for '%s' option.", key);
252 g_free(op);
253 return NULL;
254 }
255 g_hash_table_insert(new_opts, g_strdup(mod_opts[i].id),
256 g_variant_ref(value));
257 } else {
258 /* Option not given: insert the default value. */
259 g_hash_table_insert(new_opts, g_strdup(mod_opts[i].id),
260 g_variant_ref(mod_opts[i].def));
261 }
262 }
263
264 /* Make sure no invalid options were given. */
265 if (options) {
266 g_hash_table_iter_init(&iter, options);
267 while (g_hash_table_iter_next(&iter, &key, &value)) {
268 if (!g_hash_table_lookup(new_opts, key)) {
269 sr_err("Output module '%s' has no option '%s'", o->id, key);
270 g_hash_table_destroy(new_opts);
271 g_free(op);
272 return NULL;
273 }
274 }
275 }
276 }
277
278 if (op->module->init && op->module->init(op, new_opts) != SR_OK) {
279 g_free(op);
280 op = NULL;
281 }
282 if (new_opts)
283 g_hash_table_destroy(new_opts);
284
285 return op;
286}
287
288/**
289 * Send a packet to the specified output instance.
290 *
291 * The instance's output is returned as a newly allocated GString,
292 * which must be freed by the caller.
293 *
294 * @since 0.4.0
295 */
296SR_API int sr_output_send(const struct sr_output *o,
297 const struct sr_datafeed_packet *packet, GString **out)
298{
299 return o->module->receive(o, packet, out);
300}
301
302/**
303 * Free the specified output instance and all associated resources.
304 *
305 * @since 0.4.0
306 */
307SR_API int sr_output_free(const struct sr_output *o)
308{
309 int ret;
310
311 if (!o)
312 return SR_ERR_ARG;
313
314 ret = SR_OK;
315 if (o->module->cleanup)
316 ret = o->module->cleanup((struct sr_output *)o);
317 g_free((gpointer)o);
318
319 return ret;
320}
321
322/** @} */