]> sigrok.org Git - libsigrok.git/blame - src/input/input.c
cxx: Fix a linking issue.
[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 *
7a54232b 213 * @param imod The input module to use. Must not be NULL.
0f3dbc95
BV
214 * @param options GHashTable consisting of keys corresponding with
215 * the module options \c id field. The values should be GVariant
216 * pointers with sunk references, of the same GVariantType as the option's
17bfaca6
BV
217 * default value.
218 *
219 * @since 0.4.0
220 */
221SR_API struct sr_input *sr_input_new(const struct sr_input_module *imod,
222 GHashTable *options)
223{
224 struct sr_input *in;
225 struct sr_option *mod_opts;
226 const GVariantType *gvt;
227 GHashTable *new_opts;
228 GHashTableIter iter;
229 gpointer key, value;
230 int i;
231
232 in = g_malloc0(sizeof(struct sr_input));
233 in->module = imod;
234
7db06394
BV
235 new_opts = g_hash_table_new_full(g_str_hash, g_str_equal, g_free,
236 (GDestroyNotify)g_variant_unref);
237 if (imod->options) {
17bfaca6
BV
238 mod_opts = imod->options();
239 for (i = 0; mod_opts[i].id; i++) {
7db06394
BV
240 if (options && g_hash_table_lookup_extended(options,
241 mod_opts[i].id, &key, &value)) {
17bfaca6
BV
242 /* Option not given: insert the default value. */
243 gvt = g_variant_get_type(mod_opts[i].def);
244 if (!g_variant_is_of_type(value, gvt)) {
245 sr_err("Invalid type for '%s' option.", key);
246 g_free(in);
247 return NULL;
248 }
249 g_hash_table_insert(new_opts, g_strdup(mod_opts[i].id),
250 g_variant_ref(value));
251 } else {
252 /* Pass option along. */
253 g_hash_table_insert(new_opts, g_strdup(mod_opts[i].id),
254 g_variant_ref(mod_opts[i].def));
255 }
256 }
257
258 /* Make sure no invalid options were given. */
7db06394
BV
259 if (options) {
260 g_hash_table_iter_init(&iter, options);
261 while (g_hash_table_iter_next(&iter, &key, &value)) {
262 if (!g_hash_table_lookup(new_opts, key)) {
263 sr_err("Input module '%s' has no option '%s'", imod->id, key);
264 g_hash_table_destroy(new_opts);
265 g_free(in);
266 return NULL;
267 }
17bfaca6
BV
268 }
269 }
7db06394 270 }
17bfaca6
BV
271
272 if (in->module->init && in->module->init(in, new_opts) != SR_OK) {
17bfaca6
BV
273 g_free(in);
274 in = NULL;
64098211
BV
275 } else {
276 in->buf = g_string_sized_new(128);
17bfaca6 277 }
64098211 278
17bfaca6
BV
279 if (new_opts)
280 g_hash_table_destroy(new_opts);
281
282 return in;
283}
284
64098211 285/* Returns TRUE if all required meta items are available. */
17bfaca6
BV
286static gboolean check_required_metadata(const uint8_t *metadata, uint8_t *avail)
287{
288 int m, a;
289 uint8_t reqd;
290
291 for (m = 0; metadata[m]; m++) {
7db06394 292 if (!(metadata[m] & SR_INPUT_META_REQUIRED))
17bfaca6
BV
293 continue;
294 reqd = metadata[m] & ~SR_INPUT_META_REQUIRED;
295 for (a = 0; avail[a]; a++) {
296 if (avail[a] == reqd)
297 break;
298 }
299 if (!avail[a])
300 /* Found a required meta item that isn't available. */
301 return FALSE;
302 }
303
304 return TRUE;
305}
306
307/**
308 * Try to find an input module that can parse the given buffer.
309 *
310 * The buffer must contain enough of the beginning of the file for
311 * the input modules to find a match. This is format-dependent, but
312 * 128 bytes is normally enough.
313 *
4f979115
BV
314 * If an input module is found, an instance is created into *in.
315 * Otherwise, *in contains NULL.
17bfaca6 316 *
33e4303b
BV
317 * If an instance is created, it has the given buffer used for scanning
318 * already submitted to it, to be processed before more data is sent.
319 * This allows a frontend to submit an initial chunk of a non-seekable
320 * stream, such as stdin, without having to keep it around and submit
321 * it again later.
322 *
17bfaca6 323 */
4f979115 324SR_API int sr_input_scan_buffer(GString *buf, const struct sr_input **in)
17bfaca6 325{
17bfaca6
BV
326 const struct sr_input_module *imod;
327 GHashTable *meta;
328 unsigned int m, i;
4f979115 329 int ret;
17bfaca6
BV
330 uint8_t mitem, avail_metadata[8];
331
332 /* No more metadata to be had from a buffer. */
333 avail_metadata[0] = SR_INPUT_META_HEADER;
334 avail_metadata[1] = 0;
335
4f979115
BV
336 *in = NULL;
337 ret = SR_ERR;
17bfaca6
BV
338 for (i = 0; input_module_list[i]; i++) {
339 imod = input_module_list[i];
340 if (!imod->metadata[0]) {
341 /* Module has no metadata for matching so will take
342 * any input. No point in letting it try to match. */
343 continue;
344 }
345 if (!check_required_metadata(imod->metadata, avail_metadata))
346 /* Cannot satisfy this module's requirements. */
347 continue;
348
349 meta = g_hash_table_new(NULL, NULL);
350 for (m = 0; m < sizeof(imod->metadata); m++) {
351 mitem = imod->metadata[m] & ~SR_INPUT_META_REQUIRED;
352 if (mitem == SR_INPUT_META_HEADER)
353 g_hash_table_insert(meta, GINT_TO_POINTER(mitem), buf);
354 }
355 if (g_hash_table_size(meta) == 0) {
356 /* No metadata for this module, so nothing to match. */
357 g_hash_table_destroy(meta);
358 continue;
359 }
4f979115
BV
360 sr_spew("Trying module %s.", imod->id);
361 ret = imod->format_match(meta);
362 g_hash_table_destroy(meta);
363 if (ret == SR_ERR_DATA) {
364 /* Module recognized this buffer, but cannot handle it. */
365 break;
366 } else if (ret == SR_ERR) {
17bfaca6 367 /* Module didn't recognize this buffer. */
17bfaca6 368 continue;
4f979115 369 } else if (ret != SR_OK) {
60107497 370 /* Can be SR_ERR_NA. */
4f979115 371 return ret;
17bfaca6 372 }
17bfaca6
BV
373
374 /* Found a matching module. */
4f979115
BV
375 sr_spew("Module %s matched.", imod->id);
376 *in = sr_input_new(imod, NULL);
377 g_string_insert_len((*in)->buf, 0, buf->str, buf->len);
17bfaca6
BV
378 break;
379 }
380
4f979115 381 return ret;
17bfaca6
BV
382}
383
384/**
385 * Try to find an input module that can parse the given file.
386 *
4f979115
BV
387 * If an input module is found, an instance is created into *in.
388 * Otherwise, *in contains NULL.
17bfaca6
BV
389 *
390 */
4f979115 391SR_API int sr_input_scan_file(const char *filename, const struct sr_input **in)
17bfaca6 392{
17bfaca6
BV
393 const struct sr_input_module *imod;
394 GHashTable *meta;
115fbe94 395 GString *header_buf;
17bfaca6
BV
396 struct stat st;
397 unsigned int midx, m, i;
4f979115 398 int fd, ret;
17bfaca6
BV
399 ssize_t size;
400 uint8_t mitem, avail_metadata[8];
401
402 if (!filename || !filename[0]) {
403 sr_err("Invalid filename.");
4f979115 404 return SR_ERR_ARG;
17bfaca6
BV
405 }
406
407 if (!g_file_test(filename, G_FILE_TEST_EXISTS)) {
408 sr_err("No such file.");
4f979115 409 return SR_ERR_ARG;
17bfaca6
BV
410 }
411
412 if (stat(filename, &st) < 0) {
413 sr_err("%s", strerror(errno));
4f979115 414 return SR_ERR_ARG;
17bfaca6
BV
415 }
416
417 midx = 0;
418 avail_metadata[midx++] = SR_INPUT_META_FILENAME;
419 avail_metadata[midx++] = SR_INPUT_META_FILESIZE;
420 avail_metadata[midx++] = SR_INPUT_META_HEADER;
421 /* TODO: MIME type */
422
4f979115
BV
423 *in = NULL;
424 ret = SR_ERR;
115fbe94 425 header_buf = g_string_sized_new(128);
17bfaca6 426 for (i = 0; input_module_list[i]; i++) {
115fbe94
BV
427 g_string_truncate(header_buf, 0);
428
17bfaca6
BV
429 imod = input_module_list[i];
430 if (!imod->metadata[0]) {
431 /* Module has no metadata for matching so will take
432 * any input. No point in letting it try to match. */
433 continue;
434 }
435 if (!check_required_metadata(imod->metadata, avail_metadata))
436 /* Cannot satisfy this module's requirements. */
437 continue;
438
439 meta = g_hash_table_new(NULL, NULL);
440 for (m = 0; m < sizeof(imod->metadata); m++) {
441 mitem = imod->metadata[m] & ~SR_INPUT_META_REQUIRED;
115fbe94
BV
442 if (!mitem)
443 /* Metadata list is 0-terminated. */
444 break;
4f979115 445 if (mitem == SR_INPUT_META_FILENAME) {
17bfaca6
BV
446 g_hash_table_insert(meta, GINT_TO_POINTER(mitem),
447 (gpointer)filename);
4f979115 448 } else if (mitem == SR_INPUT_META_FILESIZE) {
17bfaca6
BV
449 g_hash_table_insert(meta, GINT_TO_POINTER(mitem),
450 GINT_TO_POINTER(st.st_size));
115fbe94 451 } else if (mitem == SR_INPUT_META_HEADER) {
17bfaca6
BV
452 if ((fd = open(filename, O_RDONLY)) < 0) {
453 sr_err("%s", strerror(errno));
4f979115 454 return SR_ERR;
17bfaca6 455 }
115fbe94
BV
456 size = read(fd, header_buf->str, 128);
457 header_buf->len = size;
17bfaca6
BV
458 close(fd);
459 if (size <= 0) {
115fbe94 460 g_string_free(header_buf, TRUE);
17bfaca6 461 sr_err("%s", strerror(errno));
4f979115 462 return SR_ERR;
17bfaca6 463 }
115fbe94 464 g_hash_table_insert(meta, GINT_TO_POINTER(mitem), header_buf);
17bfaca6
BV
465 }
466 }
467 if (g_hash_table_size(meta) == 0) {
468 /* No metadata for this module, so there's no way it
469 * can match. */
470 g_hash_table_destroy(meta);
471 continue;
472 }
4f979115
BV
473 sr_spew("Trying module %s.", imod->id);
474 ret = imod->format_match(meta);
475 g_hash_table_destroy(meta);
476 if (ret == SR_ERR_DATA) {
477 /* Module recognized this buffer, but cannot handle it. */
478 break;
479 } else if (ret == SR_ERR) {
17bfaca6
BV
480 /* Module didn't recognize this buffer. */
481 continue;
4f979115 482 } else if (ret != SR_OK) {
60107497 483 /* Can be SR_ERR_NA. */
4f979115
BV
484 return ret;
485 }
17bfaca6
BV
486
487 /* Found a matching module. */
4f979115
BV
488 sr_spew("Module %s matched.", imod->id);
489 *in = sr_input_new(imod, NULL);
17bfaca6
BV
490 break;
491 }
115fbe94 492 g_string_free(header_buf, TRUE);
17bfaca6 493
4f979115 494 return ret;
17bfaca6
BV
495}
496
0f3dbc95
BV
497/**
498 * Return the input instance's (virtual) device instance. This can be
499 * used to find out the number of channels and other information.
500 *
d0181813
BV
501 * If the device instance has not yet been fully populated by the input
502 * module, NULL is returned. This indicates the module needs more data
503 * to identify the number of channels and so on.
504 *
0f3dbc95
BV
505 * @since 0.4.0
506 */
17bfaca6
BV
507SR_API struct sr_dev_inst *sr_input_dev_inst_get(const struct sr_input *in)
508{
d0181813
BV
509 if (in->sdi_ready)
510 return in->sdi;
511 else
512 return NULL;
17bfaca6
BV
513}
514
515/**
516 * Send data to the specified input instance.
517 *
518 * When an input module instance is created with sr_input_new(), this
519 * function is used to feed data to the instance.
520 *
d0181813
BV
521 * As enough data gets fed into this function to completely populate
522 * the device instance associated with this input instance, this is
523 * guaranteed to return the moment it's ready. This gives the caller
524 * the chance to examine the device instance, attach session callbacks
60107497 525 * and so on.
d0181813 526 *
17bfaca6
BV
527 * @since 0.4.0
528 */
529SR_API int sr_input_send(const struct sr_input *in, GString *buf)
530{
25f20faf 531 sr_spew("Sending %d bytes to %s module.", buf->len, in->module->id);
d0181813 532 return in->module->receive((struct sr_input *)in, buf);
17bfaca6
BV
533}
534
7066fd46
BV
535/**
536 * Signal the input module no more data will come.
537 *
538 * This will cause the module to process any data it may have buffered.
539 * The SR_DF_END packet will also typically be sent at this time.
540 *
541 * @since 0.4.0
542 */
543SR_API int sr_input_end(const struct sr_input *in)
544{
545 sr_spew("Calling end() on %s module.", in->module->id);
546 return in->module->end((struct sr_input *)in);
547}
548
17bfaca6
BV
549/**
550 * Free the specified input instance and all associated resources.
551 *
552 * @since 0.4.0
553 */
d5cc282f 554SR_API void sr_input_free(const struct sr_input *in)
17bfaca6 555{
17bfaca6 556 if (!in)
d5cc282f 557 return;
17bfaca6 558
17bfaca6 559 if (in->module->cleanup)
d5cc282f 560 in->module->cleanup((struct sr_input *)in);
7db06394
BV
561 if (in->sdi)
562 sr_dev_inst_free(in->sdi);
d0181813
BV
563 if (in->buf->len > 64) {
564 /* That seems more than just some sub-unitsize leftover... */
565 sr_warn("Found %d unprocessed bytes at free time.", in->buf->len);
566 }
d5cc282f 567 g_string_free(in->buf, TRUE);
89da5b3b 568 g_free(in->priv);
17bfaca6 569 g_free((gpointer)in);
17bfaca6
BV
570}
571
572
7b870c38 573/** @} */