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