2 * This file is part of the libsigrok project.
4 * Copyright (C) 2014 Bert Vermeulen <bert@biot.com>
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.
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.
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/>.
21 #include <sys/types.h>
26 #include "libsigrok.h"
27 #include "libsigrok-internal.h"
29 #define LOG_PREFIX "input"
34 * Input module handling.
38 * @defgroup grp_input Input modules
40 * Input file/data module handling.
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).
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.
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.
61 extern SR_PRIV struct sr_input_module input_chronovu_la8;
62 extern SR_PRIV struct sr_input_module input_csv;
63 extern SR_PRIV struct sr_input_module input_binary;
64 extern SR_PRIV struct sr_input_module input_vcd;
65 extern SR_PRIV struct sr_input_module input_wav;
68 static const struct sr_input_module *input_module_list[] = {
78 * Returns a NULL-terminated list of all available input modules.
82 SR_API const struct sr_input_module **sr_input_list(void)
84 return input_module_list;
88 * Returns the specified input module's ID.
92 SR_API const char *sr_input_id_get(const struct sr_input_module *o)
95 sr_err("Invalid input module NULL!");
103 * Returns the specified input module's name.
107 SR_API const char *sr_input_name_get(const struct sr_input_module *o)
110 sr_err("Invalid input module NULL!");
118 * Returns the specified input module's description.
122 SR_API const char *sr_input_description_get(const struct sr_input_module *o)
125 sr_err("Invalid input module NULL!");
133 * Return the input module with the specified ID, or NULL if no module
134 * with that id is found.
138 SR_API const struct sr_input_module *sr_input_find(char *id)
142 for (i = 0; input_module_list[i]; i++) {
143 if (!strcmp(input_module_list[i]->id, id))
144 return input_module_list[i];
151 * Returns a NULL-terminated array of struct sr_option, or NULL if the
152 * module takes no options.
154 * Each call to this function must be followed by a call to
155 * sr_input_options_free().
159 SR_API const struct sr_option **sr_input_options_get(const struct sr_input_module *imod)
161 const struct sr_option *mod_opts, **opts;
164 if (!imod || !imod->options)
167 mod_opts = imod->options();
169 for (size = 0; mod_opts[size].id; size++)
171 opts = g_malloc((size + 1) * sizeof(struct sr_option *));
173 for (i = 0; i < size; i++)
174 opts[i] = &mod_opts[i];
181 * After a call to sr_input_options_get(), this function cleans up all
182 * resources returned by that call.
186 SR_API void sr_input_options_free(const struct sr_option **options)
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;
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;
208 * Create a new input instance using the specified input module.
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.
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
220 SR_API struct sr_input *sr_input_new(const struct sr_input_module *imod,
224 struct sr_option *mod_opts;
225 const GVariantType *gvt;
226 GHashTable *new_opts;
231 in = g_malloc0(sizeof(struct sr_input));
234 new_opts = g_hash_table_new_full(g_str_hash, g_str_equal, g_free,
235 (GDestroyNotify)g_variant_unref);
237 mod_opts = imod->options();
238 for (i = 0; mod_opts[i].id; i++) {
239 if (options && g_hash_table_lookup_extended(options,
240 mod_opts[i].id, &key, &value)) {
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);
248 g_hash_table_insert(new_opts, g_strdup(mod_opts[i].id),
249 g_variant_ref(value));
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));
257 /* Make sure no invalid options were given. */
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);
271 if (in->module->init && in->module->init(in, new_opts) != SR_OK) {
276 g_hash_table_destroy(new_opts);
277 in->buf = g_string_sized_new(128);
282 /* Returns TRUE is all required meta items are available. */
283 static gboolean check_required_metadata(const uint8_t *metadata, uint8_t *avail)
288 for (m = 0; metadata[m]; m++) {
289 if (!(metadata[m] & SR_INPUT_META_REQUIRED))
291 reqd = metadata[m] & ~SR_INPUT_META_REQUIRED;
292 for (a = 0; avail[a]; a++) {
293 if (avail[a] == reqd)
297 /* Found a required meta item that isn't available. */
305 * Try to find an input module that can parse the given buffer.
307 * The buffer must contain enough of the beginning of the file for
308 * the input modules to find a match. This is format-dependent, but
309 * 128 bytes is normally enough.
311 * If an input module is found, an instance is created into *in.
312 * Otherwise, *in contains NULL.
314 * If an instance is created, it has the given buffer used for scanning
315 * already submitted to it, to be processed before more data is sent.
316 * This allows a frontend to submit an initial chunk of a non-seekable
317 * stream, such as stdin, without having to keep it around and submit
321 SR_API int sr_input_scan_buffer(GString *buf, const struct sr_input **in)
323 const struct sr_input_module *imod;
327 uint8_t mitem, avail_metadata[8];
329 /* No more metadata to be had from a buffer. */
330 avail_metadata[0] = SR_INPUT_META_HEADER;
331 avail_metadata[1] = 0;
335 for (i = 0; input_module_list[i]; i++) {
336 imod = input_module_list[i];
337 if (!imod->metadata[0]) {
338 /* Module has no metadata for matching so will take
339 * any input. No point in letting it try to match. */
342 if (!check_required_metadata(imod->metadata, avail_metadata))
343 /* Cannot satisfy this module's requirements. */
346 meta = g_hash_table_new(NULL, NULL);
347 for (m = 0; m < sizeof(imod->metadata); m++) {
348 mitem = imod->metadata[m] & ~SR_INPUT_META_REQUIRED;
349 if (mitem == SR_INPUT_META_HEADER)
350 g_hash_table_insert(meta, GINT_TO_POINTER(mitem), buf);
352 if (g_hash_table_size(meta) == 0) {
353 /* No metadata for this module, so nothing to match. */
354 g_hash_table_destroy(meta);
357 sr_spew("Trying module %s.", imod->id);
358 ret = imod->format_match(meta);
359 g_hash_table_destroy(meta);
360 if (ret == SR_ERR_DATA) {
361 /* Module recognized this buffer, but cannot handle it. */
363 } else if (ret == SR_ERR) {
364 /* Module didn't recognize this buffer. */
366 } else if (ret != SR_OK) {
367 /* Can be SR_OK_CONTINUE. */
371 /* Found a matching module. */
372 sr_spew("Module %s matched.", imod->id);
373 *in = sr_input_new(imod, NULL);
374 g_string_insert_len((*in)->buf, 0, buf->str, buf->len);
382 * Try to find an input module that can parse the given file.
384 * If an input module is found, an instance is created into *in.
385 * Otherwise, *in contains NULL.
388 SR_API int sr_input_scan_file(const char *filename, const struct sr_input **in)
390 const struct sr_input_module *imod;
394 unsigned int midx, m, i;
397 uint8_t mitem, avail_metadata[8];
399 if (!filename || !filename[0]) {
400 sr_err("Invalid filename.");
404 if (!g_file_test(filename, G_FILE_TEST_EXISTS)) {
405 sr_err("No such file.");
409 if (stat(filename, &st) < 0) {
410 sr_err("%s", strerror(errno));
415 avail_metadata[midx++] = SR_INPUT_META_FILENAME;
416 avail_metadata[midx++] = SR_INPUT_META_FILESIZE;
417 avail_metadata[midx++] = SR_INPUT_META_HEADER;
418 /* TODO: MIME type */
422 header_buf = g_string_sized_new(128);
423 for (i = 0; input_module_list[i]; i++) {
424 g_string_truncate(header_buf, 0);
426 imod = input_module_list[i];
427 if (!imod->metadata[0]) {
428 /* Module has no metadata for matching so will take
429 * any input. No point in letting it try to match. */
432 if (!check_required_metadata(imod->metadata, avail_metadata))
433 /* Cannot satisfy this module's requirements. */
436 meta = g_hash_table_new(NULL, NULL);
437 for (m = 0; m < sizeof(imod->metadata); m++) {
438 mitem = imod->metadata[m] & ~SR_INPUT_META_REQUIRED;
440 /* Metadata list is 0-terminated. */
442 if (mitem == SR_INPUT_META_FILENAME) {
443 g_hash_table_insert(meta, GINT_TO_POINTER(mitem),
445 } else if (mitem == SR_INPUT_META_FILESIZE) {
446 g_hash_table_insert(meta, GINT_TO_POINTER(mitem),
447 GINT_TO_POINTER(st.st_size));
448 } else if (mitem == SR_INPUT_META_HEADER) {
449 if ((fd = open(filename, O_RDONLY)) < 0) {
450 sr_err("%s", strerror(errno));
453 size = read(fd, header_buf->str, 128);
454 header_buf->len = size;
457 g_string_free(header_buf, TRUE);
458 sr_err("%s", strerror(errno));
461 g_hash_table_insert(meta, GINT_TO_POINTER(mitem), header_buf);
464 if (g_hash_table_size(meta) == 0) {
465 /* No metadata for this module, so there's no way it
467 g_hash_table_destroy(meta);
470 sr_spew("Trying module %s.", imod->id);
471 ret = imod->format_match(meta);
472 g_hash_table_destroy(meta);
473 if (ret == SR_ERR_DATA) {
474 /* Module recognized this buffer, but cannot handle it. */
476 } else if (ret == SR_ERR) {
477 /* Module didn't recognize this buffer. */
479 } else if (ret != SR_OK) {
480 /* Can be SR_OK_CONTINUE. */
484 /* Found a matching module. */
485 sr_spew("Module %s matched.", imod->id);
486 *in = sr_input_new(imod, NULL);
489 g_string_free(header_buf, TRUE);
495 * Return the input instance's (virtual) device instance. This can be
496 * used to find out the number of channels and other information.
500 SR_API struct sr_dev_inst *sr_input_dev_inst_get(const struct sr_input *in)
506 * Send data to the specified input instance.
508 * When an input module instance is created with sr_input_new(), this
509 * function is used to feed data to the instance.
513 SR_API int sr_input_send(const struct sr_input *in, GString *buf)
515 return in->module->receive(in, buf);
519 * Free the specified input instance and all associated resources.
523 SR_API int sr_input_free(const struct sr_input *in)
531 if (in->module->cleanup)
532 ret = in->module->cleanup((struct sr_input *)in);
534 sr_dev_inst_free(in->sdi);
536 g_string_free(in->buf, TRUE);
537 g_free((gpointer)in);