]> sigrok.org Git - libsigrok.git/blame - src/input/input.c
Bindings: Check for empty opts also in Configurable::config_keys()
[libsigrok.git] / src / input / input.c
CommitLineData
34e4813f 1/*
50985c20 2 * This file is part of the libsigrok project.
34e4813f 3 *
17bfaca6 4 * Copyright (C) 2014 Bert Vermeulen <bert@biot.com>
34e4813f
BV
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
6ec6c43b 20#include <config.h>
17bfaca6 21#include <string.h>
17bfaca6 22#include <errno.h>
5e364d4f
DE
23#include <glib.h>
24#include <glib/gstdio.h>
c1aae900 25#include <libsigrok/libsigrok.h>
45c59c8b 26#include "libsigrok-internal.h"
34e4813f 27
e00b3f58 28/** @cond PRIVATE */
17bfaca6 29#define LOG_PREFIX "input"
e00b3f58 30/** @endcond */
17bfaca6 31
393fb9cb
UH
32/**
33 * @file
34 *
0f3dbc95 35 * Input module handling.
393fb9cb
UH
36 */
37
7b870c38 38/**
d4c93774 39 * @defgroup grp_input Input modules
7b870c38 40 *
d4c93774 41 * Input file/data module handling.
7b870c38 42 *
83687343 43 * libsigrok can process acquisition data in several different ways.
0f3dbc95
BV
44 * Aside from acquiring data from a hardware device, it can also take it
45 * from a file in various formats (binary, CSV, VCD, and so on).
83687343 46 *
0f3dbc95
BV
47 * Like all libsigrok data handling, processing is done in a streaming
48 * manner: input should be supplied a chunk at a time. This way anything
49 * that processes data can do so in real time, without the user having
50 * to wait for the whole thing to be finished.
83687343
UH
51 *
52 * Every input module is "pluggable", meaning it's handled as being separate
53 * from the main libsigrok, but linked in to it statically. To keep things
54 * modular and separate like this, functions within an input module should be
d4c93774 55 * declared static, with only the respective 'struct sr_input_module' being
83687343
UH
56 * exported for use into the wider libsigrok namespace.
57 *
7b870c38
UH
58 * @{
59 */
60
b4bd7088 61/** @cond PRIVATE */
d4c93774
BV
62extern SR_PRIV struct sr_input_module input_chronovu_la8;
63extern SR_PRIV struct sr_input_module input_csv;
64extern SR_PRIV struct sr_input_module input_binary;
6d2897e3 65extern SR_PRIV struct sr_input_module input_trace32_ad;
d4c93774
BV
66extern SR_PRIV struct sr_input_module input_vcd;
67extern SR_PRIV struct sr_input_module input_wav;
e6b15cb5 68extern SR_PRIV struct sr_input_module input_raw_analog;
b4bd7088 69/* @endcond */
34e4813f 70
17bfaca6 71static const struct sr_input_module *input_module_list[] = {
b84cba4d 72 &input_binary,
02e24c0c 73 &input_chronovu_la8,
41d214f6 74 &input_csv,
6d2897e3 75 &input_trace32_ad,
b84cba4d
BV
76 &input_vcd,
77 &input_wav,
e6b15cb5 78 &input_raw_analog,
34e4813f
BV
79 NULL,
80};
81
0f3dbc95
BV
82/**
83 * Returns a NULL-terminated list of all available input modules.
84 *
85 * @since 0.4.0
86 */
17bfaca6 87SR_API const struct sr_input_module **sr_input_list(void)
34e4813f 88{
34e4813f
BV
89 return input_module_list;
90}
7b870c38 91
17bfaca6
BV
92/**
93 * Returns the specified input module's ID.
94 *
95 * @since 0.4.0
96 */
4fb0a5f8 97SR_API const char *sr_input_id_get(const struct sr_input_module *imod)
17bfaca6 98{
4fb0a5f8 99 if (!imod) {
17bfaca6
BV
100 sr_err("Invalid input module NULL!");
101 return NULL;
102 }
103
4fb0a5f8 104 return imod->id;
17bfaca6
BV
105}
106
107/**
108 * Returns the specified input module's name.
109 *
110 * @since 0.4.0
111 */
4fb0a5f8 112SR_API const char *sr_input_name_get(const struct sr_input_module *imod)
17bfaca6 113{
4fb0a5f8 114 if (!imod) {
17bfaca6
BV
115 sr_err("Invalid input module NULL!");
116 return NULL;
117 }
118
4fb0a5f8 119 return imod->name;
17bfaca6
BV
120}
121
122/**
123 * Returns the specified input module's description.
124 *
125 * @since 0.4.0
126 */
4fb0a5f8 127SR_API const char *sr_input_description_get(const struct sr_input_module *imod)
17bfaca6 128{
4fb0a5f8 129 if (!imod) {
17bfaca6
BV
130 sr_err("Invalid input module NULL!");
131 return NULL;
132 }
133
4fb0a5f8 134 return imod->desc;
17bfaca6
BV
135}
136
c7bc82ff
JH
137/**
138 * Returns the specified input module's file extensions typical for the file
139 * format, as a NULL terminated array, or returns a NULL pointer if there is
140 * no preferred extension.
141 * @note these are a suggestions only.
142 *
143 * @since 0.4.0
144 */
145SR_API const char *const *sr_input_extensions_get(
4fb0a5f8 146 const struct sr_input_module *imod)
c7bc82ff 147{
4fb0a5f8 148 if (!imod) {
c7bc82ff
JH
149 sr_err("Invalid input module NULL!");
150 return NULL;
151 }
152
4fb0a5f8 153 return imod->exts;
c7bc82ff
JH
154}
155
17bfaca6
BV
156/**
157 * Return the input module with the specified ID, or NULL if no module
158 * with that id is found.
159 *
160 * @since 0.4.0
161 */
162SR_API const struct sr_input_module *sr_input_find(char *id)
163{
164 int i;
165
166 for (i = 0; input_module_list[i]; i++) {
167 if (!strcmp(input_module_list[i]->id, id))
168 return input_module_list[i];
169 }
170
171 return NULL;
172}
173
174/**
175 * Returns a NULL-terminated array of struct sr_option, or NULL if the
176 * module takes no options.
177 *
178 * Each call to this function must be followed by a call to
179 * sr_input_options_free().
180 *
181 * @since 0.4.0
182 */
bd0bfaaf 183SR_API const struct sr_option **sr_input_options_get(const struct sr_input_module *imod)
17bfaca6 184{
bd0bfaaf
BV
185 const struct sr_option *mod_opts, **opts;
186 int size, i;
17bfaca6 187
bd0bfaaf 188 if (!imod || !imod->options)
17bfaca6
BV
189 return NULL;
190
bd0bfaaf
BV
191 mod_opts = imod->options();
192
fe4fe25b 193 for (size = 0; mod_opts[size].id; size++)
bd0bfaaf 194 ;
fe4fe25b 195 opts = g_malloc((size + 1) * sizeof(struct sr_option *));
bd0bfaaf
BV
196
197 for (i = 0; i < size; i++)
198 opts[i] = &mod_opts[i];
199 opts[i] = NULL;
200
201 return opts;
17bfaca6
BV
202}
203
204/**
205 * After a call to sr_input_options_get(), this function cleans up all
bd0bfaaf 206 * resources returned by that call.
17bfaca6
BV
207 *
208 * @since 0.4.0
209 */
bd0bfaaf 210SR_API void sr_input_options_free(const struct sr_option **options)
17bfaca6 211{
fe4fe25b 212 int i;
17bfaca6 213
bd0bfaaf 214 if (!options)
17bfaca6
BV
215 return;
216
fe4fe25b
BV
217 for (i = 0; options[i]; i++) {
218 if (options[i]->def) {
219 g_variant_unref(options[i]->def);
220 ((struct sr_option *)options[i])->def = NULL;
17bfaca6
BV
221 }
222
fe4fe25b
BV
223 if (options[i]->values) {
224 g_slist_free_full(options[i]->values, (GDestroyNotify)g_variant_unref);
225 ((struct sr_option *)options[i])->values = NULL;
17bfaca6
BV
226 }
227 }
bd0bfaaf 228 g_free(options);
17bfaca6
BV
229}
230
231/**
232 * Create a new input instance using the specified input module.
233 *
234 * This function is used when a client wants to use a specific input
235 * module to parse a stream. No effort is made to identify the format.
236 *
7a54232b 237 * @param imod The input module to use. Must not be NULL.
0f3dbc95 238 * @param options GHashTable consisting of keys corresponding with
dff0a894 239 * the module options @c id field. The values should be GVariant
0f3dbc95 240 * pointers with sunk references, of the same GVariantType as the option's
17bfaca6
BV
241 * default value.
242 *
243 * @since 0.4.0
244 */
245SR_API struct sr_input *sr_input_new(const struct sr_input_module *imod,
246 GHashTable *options)
247{
248 struct sr_input *in;
2c240774 249 const struct sr_option *mod_opts;
17bfaca6
BV
250 const GVariantType *gvt;
251 GHashTable *new_opts;
252 GHashTableIter iter;
253 gpointer key, value;
254 int i;
255
256 in = g_malloc0(sizeof(struct sr_input));
257 in->module = imod;
258
7db06394
BV
259 new_opts = g_hash_table_new_full(g_str_hash, g_str_equal, g_free,
260 (GDestroyNotify)g_variant_unref);
261 if (imod->options) {
17bfaca6
BV
262 mod_opts = imod->options();
263 for (i = 0; mod_opts[i].id; i++) {
7db06394
BV
264 if (options && g_hash_table_lookup_extended(options,
265 mod_opts[i].id, &key, &value)) {
17bfaca6
BV
266 /* Option not given: insert the default value. */
267 gvt = g_variant_get_type(mod_opts[i].def);
268 if (!g_variant_is_of_type(value, gvt)) {
6433156c
DE
269 sr_err("Invalid type for '%s' option.",
270 (char *)key);
17bfaca6
BV
271 g_free(in);
272 return NULL;
273 }
274 g_hash_table_insert(new_opts, g_strdup(mod_opts[i].id),
275 g_variant_ref(value));
276 } else {
277 /* Pass option along. */
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. */
7db06394
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)) {
6433156c
DE
288 sr_err("Input module '%s' has no option '%s'",
289 imod->id, (char *)key);
7db06394
BV
290 g_hash_table_destroy(new_opts);
291 g_free(in);
292 return NULL;
293 }
17bfaca6
BV
294 }
295 }
7db06394 296 }
17bfaca6
BV
297
298 if (in->module->init && in->module->init(in, new_opts) != SR_OK) {
17bfaca6
BV
299 g_free(in);
300 in = NULL;
64098211
BV
301 } else {
302 in->buf = g_string_sized_new(128);
17bfaca6 303 }
64098211 304
17bfaca6
BV
305 if (new_opts)
306 g_hash_table_destroy(new_opts);
307
308 return in;
309}
310
64098211 311/* Returns TRUE if all required meta items are available. */
17bfaca6
BV
312static gboolean check_required_metadata(const uint8_t *metadata, uint8_t *avail)
313{
314 int m, a;
315 uint8_t reqd;
316
317 for (m = 0; metadata[m]; m++) {
7db06394 318 if (!(metadata[m] & SR_INPUT_META_REQUIRED))
17bfaca6
BV
319 continue;
320 reqd = metadata[m] & ~SR_INPUT_META_REQUIRED;
321 for (a = 0; avail[a]; a++) {
322 if (avail[a] == reqd)
323 break;
324 }
325 if (!avail[a])
326 /* Found a required meta item that isn't available. */
327 return FALSE;
328 }
329
330 return TRUE;
331}
332
333/**
334 * Try to find an input module that can parse the given buffer.
335 *
336 * The buffer must contain enough of the beginning of the file for
337 * the input modules to find a match. This is format-dependent, but
338 * 128 bytes is normally enough.
339 *
4f979115
BV
340 * If an input module is found, an instance is created into *in.
341 * Otherwise, *in contains NULL.
17bfaca6 342 *
33e4303b
BV
343 * If an instance is created, it has the given buffer used for scanning
344 * already submitted to it, to be processed before more data is sent.
345 * This allows a frontend to submit an initial chunk of a non-seekable
346 * stream, such as stdin, without having to keep it around and submit
347 * it again later.
348 *
17bfaca6 349 */
4f979115 350SR_API int sr_input_scan_buffer(GString *buf, const struct sr_input **in)
17bfaca6 351{
17bfaca6
BV
352 const struct sr_input_module *imod;
353 GHashTable *meta;
354 unsigned int m, i;
4f979115 355 int ret;
17bfaca6
BV
356 uint8_t mitem, avail_metadata[8];
357
358 /* No more metadata to be had from a buffer. */
359 avail_metadata[0] = SR_INPUT_META_HEADER;
360 avail_metadata[1] = 0;
361
4f979115
BV
362 *in = NULL;
363 ret = SR_ERR;
17bfaca6
BV
364 for (i = 0; input_module_list[i]; i++) {
365 imod = input_module_list[i];
366 if (!imod->metadata[0]) {
367 /* Module has no metadata for matching so will take
368 * any input. No point in letting it try to match. */
369 continue;
370 }
371 if (!check_required_metadata(imod->metadata, avail_metadata))
372 /* Cannot satisfy this module's requirements. */
373 continue;
374
375 meta = g_hash_table_new(NULL, NULL);
376 for (m = 0; m < sizeof(imod->metadata); m++) {
377 mitem = imod->metadata[m] & ~SR_INPUT_META_REQUIRED;
378 if (mitem == SR_INPUT_META_HEADER)
379 g_hash_table_insert(meta, GINT_TO_POINTER(mitem), buf);
380 }
381 if (g_hash_table_size(meta) == 0) {
382 /* No metadata for this module, so nothing to match. */
383 g_hash_table_destroy(meta);
384 continue;
385 }
4f979115
BV
386 sr_spew("Trying module %s.", imod->id);
387 ret = imod->format_match(meta);
388 g_hash_table_destroy(meta);
389 if (ret == SR_ERR_DATA) {
390 /* Module recognized this buffer, but cannot handle it. */
391 break;
392 } else if (ret == SR_ERR) {
17bfaca6 393 /* Module didn't recognize this buffer. */
17bfaca6 394 continue;
4f979115 395 } else if (ret != SR_OK) {
60107497 396 /* Can be SR_ERR_NA. */
4f979115 397 return ret;
17bfaca6 398 }
17bfaca6
BV
399
400 /* Found a matching module. */
4f979115
BV
401 sr_spew("Module %s matched.", imod->id);
402 *in = sr_input_new(imod, NULL);
403 g_string_insert_len((*in)->buf, 0, buf->str, buf->len);
17bfaca6
BV
404 break;
405 }
406
4f979115 407 return ret;
17bfaca6
BV
408}
409
410/**
411 * Try to find an input module that can parse the given file.
412 *
4f979115
BV
413 * If an input module is found, an instance is created into *in.
414 * Otherwise, *in contains NULL.
17bfaca6
BV
415 *
416 */
4f979115 417SR_API int sr_input_scan_file(const char *filename, const struct sr_input **in)
17bfaca6 418{
4619fab4 419 int64_t filesize;
5e364d4f 420 FILE *stream;
17bfaca6
BV
421 const struct sr_input_module *imod;
422 GHashTable *meta;
5e364d4f 423 GString *header;
5e364d4f
DE
424 size_t count;
425 unsigned int midx, i;
426 int ret;
427 uint8_t avail_metadata[8];
428
429 *in = NULL;
17bfaca6
BV
430
431 if (!filename || !filename[0]) {
432 sr_err("Invalid filename.");
4f979115 433 return SR_ERR_ARG;
17bfaca6 434 }
5e364d4f
DE
435 stream = g_fopen(filename, "rb");
436 if (!stream) {
437 sr_err("Failed to open %s: %s", filename, g_strerror(errno));
438 return SR_ERR;
439 }
4619fab4
DE
440 filesize = sr_file_get_size(stream);
441 if (filesize < 0) {
442 sr_err("Failed to get size of %s: %s",
443 filename, g_strerror(errno));
444 fclose(stream);
445 return SR_ERR;
446 }
5e364d4f
DE
447 /* This actually allocates 256 bytes to allow for NUL termination. */
448 header = g_string_sized_new(255);
449 count = fread(header->str, 1, header->allocated_len - 1, stream);
450
451 if (count != header->allocated_len - 1 && ferror(stream)) {
452 sr_err("Failed to read %s: %s", filename, g_strerror(errno));
453 fclose(stream);
454 g_string_free(header, TRUE);
455 return SR_ERR;
456 }
457 fclose(stream);
458 g_string_set_size(header, count);
459
460 meta = g_hash_table_new(NULL, NULL);
461 g_hash_table_insert(meta, GINT_TO_POINTER(SR_INPUT_META_FILENAME),
462 (char *)filename);
463 g_hash_table_insert(meta, GINT_TO_POINTER(SR_INPUT_META_FILESIZE),
4619fab4 464 GSIZE_TO_POINTER(MIN(filesize, G_MAXSSIZE)));
5e364d4f
DE
465 g_hash_table_insert(meta, GINT_TO_POINTER(SR_INPUT_META_HEADER),
466 header);
17bfaca6
BV
467 midx = 0;
468 avail_metadata[midx++] = SR_INPUT_META_FILENAME;
469 avail_metadata[midx++] = SR_INPUT_META_FILESIZE;
470 avail_metadata[midx++] = SR_INPUT_META_HEADER;
5e364d4f 471 avail_metadata[midx] = 0;
17bfaca6
BV
472 /* TODO: MIME type */
473
4f979115 474 ret = SR_ERR;
115fbe94 475
5e364d4f 476 for (i = 0; input_module_list[i]; i++) {
17bfaca6
BV
477 imod = input_module_list[i];
478 if (!imod->metadata[0]) {
479 /* Module has no metadata for matching so will take
480 * any input. No point in letting it try to match. */
481 continue;
482 }
483 if (!check_required_metadata(imod->metadata, avail_metadata))
484 /* Cannot satisfy this module's requirements. */
485 continue;
486
5e364d4f
DE
487 sr_dbg("Trying module %s.", imod->id);
488
4f979115 489 ret = imod->format_match(meta);
5e364d4f 490 if (ret == SR_ERR) {
17bfaca6
BV
491 /* Module didn't recognize this buffer. */
492 continue;
4f979115 493 } else if (ret != SR_OK) {
5e364d4f
DE
494 /* Module recognized this buffer, but cannot handle it. */
495 break;
4f979115 496 }
17bfaca6 497 /* Found a matching module. */
5e364d4f
DE
498 sr_dbg("Module %s matched.", imod->id);
499
4f979115 500 *in = sr_input_new(imod, NULL);
17bfaca6
BV
501 break;
502 }
5e364d4f
DE
503 g_hash_table_destroy(meta);
504 g_string_free(header, TRUE);
17bfaca6 505
4f979115 506 return ret;
17bfaca6
BV
507}
508
0f3dbc95
BV
509/**
510 * Return the input instance's (virtual) device instance. This can be
511 * used to find out the number of channels and other information.
512 *
d0181813
BV
513 * If the device instance has not yet been fully populated by the input
514 * module, NULL is returned. This indicates the module needs more data
515 * to identify the number of channels and so on.
516 *
0f3dbc95
BV
517 * @since 0.4.0
518 */
17bfaca6
BV
519SR_API struct sr_dev_inst *sr_input_dev_inst_get(const struct sr_input *in)
520{
d0181813
BV
521 if (in->sdi_ready)
522 return in->sdi;
523 else
524 return NULL;
17bfaca6
BV
525}
526
527/**
528 * Send data to the specified input instance.
529 *
530 * When an input module instance is created with sr_input_new(), this
531 * function is used to feed data to the instance.
532 *
d0181813
BV
533 * As enough data gets fed into this function to completely populate
534 * the device instance associated with this input instance, this is
535 * guaranteed to return the moment it's ready. This gives the caller
536 * the chance to examine the device instance, attach session callbacks
60107497 537 * and so on.
d0181813 538 *
17bfaca6
BV
539 * @since 0.4.0
540 */
541SR_API int sr_input_send(const struct sr_input *in, GString *buf)
542{
6433156c
DE
543 sr_spew("Sending %" G_GSIZE_FORMAT " bytes to %s module.",
544 buf->len, in->module->id);
d0181813 545 return in->module->receive((struct sr_input *)in, buf);
17bfaca6
BV
546}
547
7066fd46
BV
548/**
549 * Signal the input module no more data will come.
550 *
551 * This will cause the module to process any data it may have buffered.
552 * The SR_DF_END packet will also typically be sent at this time.
553 *
554 * @since 0.4.0
555 */
556SR_API int sr_input_end(const struct sr_input *in)
557{
558 sr_spew("Calling end() on %s module.", in->module->id);
559 return in->module->end((struct sr_input *)in);
560}
561
17bfaca6
BV
562/**
563 * Free the specified input instance and all associated resources.
564 *
565 * @since 0.4.0
566 */
d5cc282f 567SR_API void sr_input_free(const struct sr_input *in)
17bfaca6 568{
17bfaca6 569 if (!in)
d5cc282f 570 return;
17bfaca6 571
17bfaca6 572 if (in->module->cleanup)
d5cc282f 573 in->module->cleanup((struct sr_input *)in);
7db06394
BV
574 if (in->sdi)
575 sr_dev_inst_free(in->sdi);
d0181813
BV
576 if (in->buf->len > 64) {
577 /* That seems more than just some sub-unitsize leftover... */
6433156c
DE
578 sr_warn("Found %" G_GSIZE_FORMAT
579 " unprocessed bytes at free time.", in->buf->len);
d0181813 580 }
d5cc282f 581 g_string_free(in->buf, TRUE);
89da5b3b 582 g_free(in->priv);
17bfaca6 583 g_free((gpointer)in);
17bfaca6
BV
584}
585
7b870c38 586/** @} */