]> sigrok.org Git - libsigrok.git/blame_incremental - src/input/input.c
sr_input_new(): Add missing @param comment.
[libsigrok.git] / src / input / input.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 <sys/types.h>
22#include <sys/stat.h>
23#include <unistd.h>
24#include <fcntl.h>
25#include <errno.h>
26#include "libsigrok.h"
27#include "libsigrok-internal.h"
28
29#define LOG_PREFIX "input"
30
31/**
32 * @file
33 *
34 * Input module handling.
35 */
36
37/**
38 * @defgroup grp_input Input modules
39 *
40 * Input file/data module handling.
41 *
42 * libsigrok can process acquisition data in several different ways.
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).
45 *
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.
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
54 * declared static, with only the respective 'struct sr_input_module' being
55 * exported for use into the wider libsigrok namespace.
56 *
57 * @{
58 */
59
60/** @cond PRIVATE */
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;
66/* @endcond */
67
68static const struct sr_input_module *input_module_list[] = {
69 &input_binary,
70 &input_chronovu_la8,
71 &input_csv,
72 &input_vcd,
73 &input_wav,
74 NULL,
75};
76
77/**
78 * Returns a NULL-terminated list of all available input modules.
79 *
80 * @since 0.4.0
81 */
82SR_API const struct sr_input_module **sr_input_list(void)
83{
84 return input_module_list;
85}
86
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 */
159SR_API const struct sr_option **sr_input_options_get(const struct sr_input_module *imod)
160{
161 const struct sr_option *mod_opts, **opts;
162 int size, i;
163
164 if (!imod || !imod->options)
165 return NULL;
166
167 mod_opts = imod->options();
168
169 for (size = 0; mod_opts[size].id; size++)
170 ;
171 opts = g_malloc((size + 1) * sizeof(struct sr_option *));
172
173 for (i = 0; i < size; i++)
174 opts[i] = &mod_opts[i];
175 opts[i] = NULL;
176
177 return opts;
178}
179
180/**
181 * After a call to sr_input_options_get(), this function cleans up all
182 * resources returned by that call.
183 *
184 * @since 0.4.0
185 */
186SR_API void sr_input_options_free(const struct sr_option **options)
187{
188 int i;
189
190 if (!options)
191 return;
192
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;
197 }
198
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;
202 }
203 }
204 g_free(options);
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 *
213 * @param imod The input module to use. Must not be NULL.
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
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
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) {
238 mod_opts = imod->options();
239 for (i = 0; mod_opts[i].id; i++) {
240 if (options && g_hash_table_lookup_extended(options,
241 mod_opts[i].id, &key, &value)) {
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. */
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 }
268 }
269 }
270 }
271
272 if (in->module->init && in->module->init(in, new_opts) != SR_OK) {
273 g_free(in);
274 in = NULL;
275 } else {
276 in->buf = g_string_sized_new(128);
277 }
278
279 if (new_opts)
280 g_hash_table_destroy(new_opts);
281
282 return in;
283}
284
285/* Returns TRUE if all required meta items are available. */
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++) {
292 if (!(metadata[m] & SR_INPUT_META_REQUIRED))
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 *
314 * If an input module is found, an instance is created into *in.
315 * Otherwise, *in contains NULL.
316 *
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 *
323 */
324SR_API int sr_input_scan_buffer(GString *buf, const struct sr_input **in)
325{
326 const struct sr_input_module *imod;
327 GHashTable *meta;
328 unsigned int m, i;
329 int ret;
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
336 *in = NULL;
337 ret = SR_ERR;
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 }
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) {
367 /* Module didn't recognize this buffer. */
368 continue;
369 } else if (ret != SR_OK) {
370 /* Can be SR_ERR_NA. */
371 return ret;
372 }
373
374 /* Found a matching module. */
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);
378 break;
379 }
380
381 return ret;
382}
383
384/**
385 * Try to find an input module that can parse the given file.
386 *
387 * If an input module is found, an instance is created into *in.
388 * Otherwise, *in contains NULL.
389 *
390 */
391SR_API int sr_input_scan_file(const char *filename, const struct sr_input **in)
392{
393 const struct sr_input_module *imod;
394 GHashTable *meta;
395 GString *header_buf;
396 struct stat st;
397 unsigned int midx, m, i;
398 int fd, ret;
399 ssize_t size;
400 uint8_t mitem, avail_metadata[8];
401
402 if (!filename || !filename[0]) {
403 sr_err("Invalid filename.");
404 return SR_ERR_ARG;
405 }
406
407 if (!g_file_test(filename, G_FILE_TEST_EXISTS)) {
408 sr_err("No such file.");
409 return SR_ERR_ARG;
410 }
411
412 if (stat(filename, &st) < 0) {
413 sr_err("%s", strerror(errno));
414 return SR_ERR_ARG;
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
423 *in = NULL;
424 ret = SR_ERR;
425 header_buf = g_string_sized_new(128);
426 for (i = 0; input_module_list[i]; i++) {
427 g_string_truncate(header_buf, 0);
428
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;
442 if (!mitem)
443 /* Metadata list is 0-terminated. */
444 break;
445 if (mitem == SR_INPUT_META_FILENAME) {
446 g_hash_table_insert(meta, GINT_TO_POINTER(mitem),
447 (gpointer)filename);
448 } else if (mitem == SR_INPUT_META_FILESIZE) {
449 g_hash_table_insert(meta, GINT_TO_POINTER(mitem),
450 GINT_TO_POINTER(st.st_size));
451 } else if (mitem == SR_INPUT_META_HEADER) {
452 if ((fd = open(filename, O_RDONLY)) < 0) {
453 sr_err("%s", strerror(errno));
454 return SR_ERR;
455 }
456 size = read(fd, header_buf->str, 128);
457 header_buf->len = size;
458 close(fd);
459 if (size <= 0) {
460 g_string_free(header_buf, TRUE);
461 sr_err("%s", strerror(errno));
462 return SR_ERR;
463 }
464 g_hash_table_insert(meta, GINT_TO_POINTER(mitem), header_buf);
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 }
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) {
480 /* Module didn't recognize this buffer. */
481 continue;
482 } else if (ret != SR_OK) {
483 /* Can be SR_ERR_NA. */
484 return ret;
485 }
486
487 /* Found a matching module. */
488 sr_spew("Module %s matched.", imod->id);
489 *in = sr_input_new(imod, NULL);
490 break;
491 }
492 g_string_free(header_buf, TRUE);
493
494 return ret;
495}
496
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 *
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 *
505 * @since 0.4.0
506 */
507SR_API struct sr_dev_inst *sr_input_dev_inst_get(const struct sr_input *in)
508{
509 if (in->sdi_ready)
510 return in->sdi;
511 else
512 return NULL;
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 *
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
525 * and so on.
526 *
527 * @since 0.4.0
528 */
529SR_API int sr_input_send(const struct sr_input *in, GString *buf)
530{
531 sr_spew("Sending %d bytes to %s module.", buf->len, in->module->id);
532 return in->module->receive((struct sr_input *)in, buf);
533}
534
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
549/**
550 * Free the specified input instance and all associated resources.
551 *
552 * @since 0.4.0
553 */
554SR_API void sr_input_free(const struct sr_input *in)
555{
556 if (!in)
557 return;
558
559 if (in->module->cleanup)
560 in->module->cleanup((struct sr_input *)in);
561 if (in->sdi)
562 sr_dev_inst_free(in->sdi);
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 }
567 g_string_free(in->buf, TRUE);
568 g_free(in->priv);
569 g_free((gpointer)in);
570}
571
572
573/** @} */