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