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