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 file/data format 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 from
44 * a file in various formats (binary, CSV, VCD, and so on).
46 * Like everything in libsigrok that handles data, processing is done in a
47 * streaming manner -- input should be supplied to libsigrok a chunk at a time.
48 * This way anything that processes data can do so in real time, without the
49 * user having 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[] = {
70 // &input_chronovu_la8,
73 /* This one has to be last, because it will take any input. */
79 SR_API const struct sr_input_module **sr_input_list(void)
81 return input_module_list;
85 * Returns the specified input module's ID.
89 SR_API const char *sr_input_id_get(const struct sr_input_module *o)
92 sr_err("Invalid input module NULL!");
100 * Returns the specified input module's name.
104 SR_API const char *sr_input_name_get(const struct sr_input_module *o)
107 sr_err("Invalid input module NULL!");
115 * Returns the specified input module's description.
119 SR_API const char *sr_input_description_get(const struct sr_input_module *o)
122 sr_err("Invalid input module NULL!");
130 * Return the input module with the specified ID, or NULL if no module
131 * with that id is found.
135 SR_API const struct sr_input_module *sr_input_find(char *id)
139 for (i = 0; input_module_list[i]; i++) {
140 if (!strcmp(input_module_list[i]->id, id))
141 return input_module_list[i];
148 * Returns a NULL-terminated array of struct sr_option, or NULL if the
149 * module takes no options.
151 * Each call to this function must be followed by a call to
152 * sr_input_options_free().
156 SR_API const struct sr_option *sr_input_options_get(const struct sr_input_module *o)
159 if (!o || !o->options)
166 * After a call to sr_input_options_get(), this function cleans up all
167 * the resources allocated by that call.
171 SR_API void sr_input_options_free(const struct sr_input_module *o)
173 struct sr_option *opt;
175 if (!o || !o->options)
178 for (opt = o->options(); opt->id; opt++) {
180 g_variant_unref(opt->def);
185 g_slist_free_full(opt->values, (GDestroyNotify)g_variant_unref);
192 * Create a new input instance using the specified input module.
194 * This function is used when a client wants to use a specific input
195 * module to parse a stream. No effort is made to identify the format.
197 * <code>options</code> is a *HashTable with the keys corresponding with
198 * the module options' <code>id</code> field. The values should be GVariant
199 * pointers with sunk * references, of the same GVariantType as the option's
204 SR_API struct sr_input *sr_input_new(const struct sr_input_module *imod,
208 struct sr_option *mod_opts;
209 const GVariantType *gvt;
210 GHashTable *new_opts;
215 in = g_malloc0(sizeof(struct sr_input));
219 new_opts = g_hash_table_new_full(g_str_hash, g_str_equal, g_free,
220 (GDestroyNotify)g_variant_unref);
221 mod_opts = imod->options();
222 for (i = 0; mod_opts[i].id; i++) {
223 if (g_hash_table_lookup_extended(options, mod_opts[i].id,
225 /* Option not given: insert the default value. */
226 gvt = g_variant_get_type(mod_opts[i].def);
227 if (!g_variant_is_of_type(value, gvt)) {
228 sr_err("Invalid type for '%s' option.", key);
232 g_hash_table_insert(new_opts, g_strdup(mod_opts[i].id),
233 g_variant_ref(value));
235 /* Pass option along. */
236 g_hash_table_insert(new_opts, g_strdup(mod_opts[i].id),
237 g_variant_ref(mod_opts[i].def));
241 /* Make sure no invalid options were given. */
242 g_hash_table_iter_init(&iter, options);
243 while (g_hash_table_iter_next(&iter, &key, &value)) {
244 if (!g_hash_table_lookup(new_opts, key)) {
245 sr_err("Input module '%s' has no option '%s'", imod->id, key);
246 g_hash_table_destroy(new_opts);
254 if (in->module->init && in->module->init(in, new_opts) != SR_OK) {
255 g_hash_table_destroy(new_opts);
260 g_hash_table_destroy(new_opts);
265 /* Returns TRUE is all required meta items are available. */
266 static gboolean check_required_metadata(const uint8_t *metadata, uint8_t *avail)
271 for (m = 0; metadata[m]; m++) {
272 if (!metadata[m] & SR_INPUT_META_REQUIRED)
274 reqd = metadata[m] & ~SR_INPUT_META_REQUIRED;
275 for (a = 0; avail[a]; a++) {
276 if (avail[a] == reqd)
280 /* Found a required meta item that isn't available. */
288 * Try to find an input module that can parse the given buffer.
290 * The buffer must contain enough of the beginning of the file for
291 * the input modules to find a match. This is format-dependent, but
292 * 128 bytes is normally enough.
294 * If an input module is found, an instance is created and returned.
295 * Otherwise, NULL is returned.
298 SR_API const struct sr_input *sr_input_scan_buffer(GString *buf)
301 const struct sr_input_module *imod;
304 uint8_t mitem, avail_metadata[8];
306 /* No more metadata to be had from a buffer. */
307 avail_metadata[0] = SR_INPUT_META_HEADER;
308 avail_metadata[1] = 0;
311 for (i = 0; input_module_list[i]; i++) {
312 imod = input_module_list[i];
313 if (!imod->metadata[0]) {
314 /* Module has no metadata for matching so will take
315 * any input. No point in letting it try to match. */
318 if (!check_required_metadata(imod->metadata, avail_metadata))
319 /* Cannot satisfy this module's requirements. */
322 meta = g_hash_table_new(NULL, NULL);
323 for (m = 0; m < sizeof(imod->metadata); m++) {
324 mitem = imod->metadata[m] & ~SR_INPUT_META_REQUIRED;
325 if (mitem == SR_INPUT_META_HEADER)
326 g_hash_table_insert(meta, GINT_TO_POINTER(mitem), buf);
328 if (g_hash_table_size(meta) == 0) {
329 /* No metadata for this module, so nothing to match. */
330 g_hash_table_destroy(meta);
333 if (!imod->format_match(meta)) {
334 /* Module didn't recognize this buffer. */
335 g_hash_table_destroy(meta);
338 g_hash_table_destroy(meta);
340 /* Found a matching module. */
341 in = sr_input_new(imod, NULL);
342 in->buf = g_string_new_len(buf->str, buf->len);
350 * Try to find an input module that can parse the given file.
352 * If an input module is found, an instance is created and returned.
353 * Otherwise, NULL is returned.
356 SR_API const struct sr_input *sr_input_scan_file(const char *filename)
359 const struct sr_input_module *imod;
363 unsigned int midx, m, i;
366 uint8_t mitem, avail_metadata[8];
368 if (!filename || !filename[0]) {
369 sr_err("Invalid filename.");
373 if (!g_file_test(filename, G_FILE_TEST_EXISTS)) {
374 sr_err("No such file.");
378 if (stat(filename, &st) < 0) {
379 sr_err("%s", strerror(errno));
384 avail_metadata[midx++] = SR_INPUT_META_FILENAME;
385 avail_metadata[midx++] = SR_INPUT_META_FILESIZE;
386 avail_metadata[midx++] = SR_INPUT_META_HEADER;
387 /* TODO: MIME type */
391 for (i = 0; input_module_list[i]; i++) {
392 imod = input_module_list[i];
393 if (!imod->metadata[0]) {
394 /* Module has no metadata for matching so will take
395 * any input. No point in letting it try to match. */
398 if (!check_required_metadata(imod->metadata, avail_metadata))
399 /* Cannot satisfy this module's requirements. */
402 meta = g_hash_table_new(NULL, NULL);
403 for (m = 0; m < sizeof(imod->metadata); m++) {
404 mitem = imod->metadata[m] & ~SR_INPUT_META_REQUIRED;
405 if (mitem == SR_INPUT_META_FILENAME)
406 g_hash_table_insert(meta, GINT_TO_POINTER(mitem),
408 else if (mitem == SR_INPUT_META_FILESIZE)
409 g_hash_table_insert(meta, GINT_TO_POINTER(mitem),
410 GINT_TO_POINTER(st.st_size));
411 else if (mitem == SR_INPUT_META_HEADER) {
412 buf = g_string_sized_new(128);
413 if ((fd = open(filename, O_RDONLY)) < 0) {
414 sr_err("%s", strerror(errno));
417 size = read(fd, buf->str, 128);
421 g_string_free(buf, TRUE);
422 sr_err("%s", strerror(errno));
425 g_hash_table_insert(meta, GINT_TO_POINTER(mitem), buf);
426 g_string_free(buf, TRUE);
429 if (g_hash_table_size(meta) == 0) {
430 /* No metadata for this module, so there's no way it
432 g_hash_table_destroy(meta);
435 if (!imod->format_match(meta))
436 /* Module didn't recognize this buffer. */
439 /* Found a matching module. */
440 in = sr_input_new(imod, NULL);
441 in->buf = g_string_new_len(buf->str, buf->len);
445 g_string_free(buf, TRUE);
450 SR_API struct sr_dev_inst *sr_input_dev_inst_get(const struct sr_input *in)
456 * Send data to the specified input instance.
458 * When an input module instance is created with sr_input_new(), this
459 * function is used to feed data to the instance.
463 SR_API int sr_input_send(const struct sr_input *in, GString *buf)
465 return in->module->receive(in, buf);
469 * Free the specified input instance and all associated resources.
473 SR_API int sr_input_free(const struct sr_input *in)
481 if (in->module->cleanup)
482 ret = in->module->cleanup((struct sr_input *)in);
484 g_string_free(in->buf, TRUE);
485 g_free((gpointer)in);