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[] = {
70 // &input_chronovu_la8,
73 /* This one has to be last, because it will take any input. */
79 * Returns a NULL-terminated list of all available input modules.
83 SR_API const struct sr_input_module **sr_input_list(void)
85 return input_module_list;
89 * Returns the specified input module's ID.
93 SR_API const char *sr_input_id_get(const struct sr_input_module *o)
96 sr_err("Invalid input module NULL!");
104 * Returns the specified input module's name.
108 SR_API const char *sr_input_name_get(const struct sr_input_module *o)
111 sr_err("Invalid input module NULL!");
119 * Returns the specified input module's description.
123 SR_API const char *sr_input_description_get(const struct sr_input_module *o)
126 sr_err("Invalid input module NULL!");
134 * Return the input module with the specified ID, or NULL if no module
135 * with that id is found.
139 SR_API const struct sr_input_module *sr_input_find(char *id)
143 for (i = 0; input_module_list[i]; i++) {
144 if (!strcmp(input_module_list[i]->id, id))
145 return input_module_list[i];
152 * Returns a NULL-terminated array of struct sr_option, or NULL if the
153 * module takes no options.
155 * Each call to this function must be followed by a call to
156 * sr_input_options_free().
160 SR_API const struct sr_option *sr_input_options_get(const struct sr_input_module *o)
163 if (!o || !o->options)
170 * After a call to sr_input_options_get(), this function cleans up all
171 * resources allocated by that call.
175 SR_API void sr_input_options_free(const struct sr_input_module *o)
177 struct sr_option *opt;
179 if (!o || !o->options)
182 for (opt = o->options(); opt->id; opt++) {
184 g_variant_unref(opt->def);
189 g_slist_free_full(opt->values, (GDestroyNotify)g_variant_unref);
196 * Create a new input instance using the specified input module.
198 * This function is used when a client wants to use a specific input
199 * module to parse a stream. No effort is made to identify the format.
201 * @param options GHashTable consisting of keys corresponding with
202 * the module options \c id field. The values should be GVariant
203 * pointers with sunk references, of the same GVariantType as the option's
208 SR_API struct sr_input *sr_input_new(const struct sr_input_module *imod,
212 struct sr_option *mod_opts;
213 const GVariantType *gvt;
214 GHashTable *new_opts;
219 in = g_malloc0(sizeof(struct sr_input));
223 new_opts = g_hash_table_new_full(g_str_hash, g_str_equal, g_free,
224 (GDestroyNotify)g_variant_unref);
225 mod_opts = imod->options();
226 for (i = 0; mod_opts[i].id; i++) {
227 if (g_hash_table_lookup_extended(options, mod_opts[i].id,
229 /* Option not given: insert the default value. */
230 gvt = g_variant_get_type(mod_opts[i].def);
231 if (!g_variant_is_of_type(value, gvt)) {
232 sr_err("Invalid type for '%s' option.", key);
236 g_hash_table_insert(new_opts, g_strdup(mod_opts[i].id),
237 g_variant_ref(value));
239 /* Pass option along. */
240 g_hash_table_insert(new_opts, g_strdup(mod_opts[i].id),
241 g_variant_ref(mod_opts[i].def));
245 /* Make sure no invalid options were given. */
246 g_hash_table_iter_init(&iter, options);
247 while (g_hash_table_iter_next(&iter, &key, &value)) {
248 if (!g_hash_table_lookup(new_opts, key)) {
249 sr_err("Input module '%s' has no option '%s'", imod->id, key);
250 g_hash_table_destroy(new_opts);
258 if (in->module->init && in->module->init(in, new_opts) != SR_OK) {
259 g_hash_table_destroy(new_opts);
264 g_hash_table_destroy(new_opts);
269 /* Returns TRUE is all required meta items are available. */
270 static gboolean check_required_metadata(const uint8_t *metadata, uint8_t *avail)
275 for (m = 0; metadata[m]; m++) {
276 if (!metadata[m] & SR_INPUT_META_REQUIRED)
278 reqd = metadata[m] & ~SR_INPUT_META_REQUIRED;
279 for (a = 0; avail[a]; a++) {
280 if (avail[a] == reqd)
284 /* Found a required meta item that isn't available. */
292 * Try to find an input module that can parse the given buffer.
294 * The buffer must contain enough of the beginning of the file for
295 * the input modules to find a match. This is format-dependent, but
296 * 128 bytes is normally enough.
298 * If an input module is found, an instance is created and returned.
299 * Otherwise, NULL is returned.
302 SR_API const struct sr_input *sr_input_scan_buffer(GString *buf)
305 const struct sr_input_module *imod;
308 uint8_t mitem, avail_metadata[8];
310 /* No more metadata to be had from a buffer. */
311 avail_metadata[0] = SR_INPUT_META_HEADER;
312 avail_metadata[1] = 0;
315 for (i = 0; input_module_list[i]; i++) {
316 imod = input_module_list[i];
317 if (!imod->metadata[0]) {
318 /* Module has no metadata for matching so will take
319 * any input. No point in letting it try to match. */
322 if (!check_required_metadata(imod->metadata, avail_metadata))
323 /* Cannot satisfy this module's requirements. */
326 meta = g_hash_table_new(NULL, NULL);
327 for (m = 0; m < sizeof(imod->metadata); m++) {
328 mitem = imod->metadata[m] & ~SR_INPUT_META_REQUIRED;
329 if (mitem == SR_INPUT_META_HEADER)
330 g_hash_table_insert(meta, GINT_TO_POINTER(mitem), buf);
332 if (g_hash_table_size(meta) == 0) {
333 /* No metadata for this module, so nothing to match. */
334 g_hash_table_destroy(meta);
337 if (!imod->format_match(meta)) {
338 /* Module didn't recognize this buffer. */
339 g_hash_table_destroy(meta);
342 g_hash_table_destroy(meta);
344 /* Found a matching module. */
345 in = sr_input_new(imod, NULL);
346 in->buf = g_string_new_len(buf->str, buf->len);
354 * Try to find an input module that can parse the given file.
356 * If an input module is found, an instance is created and returned.
357 * Otherwise, NULL is returned.
360 SR_API const struct sr_input *sr_input_scan_file(const char *filename)
363 const struct sr_input_module *imod;
367 unsigned int midx, m, i;
370 uint8_t mitem, avail_metadata[8];
372 if (!filename || !filename[0]) {
373 sr_err("Invalid filename.");
377 if (!g_file_test(filename, G_FILE_TEST_EXISTS)) {
378 sr_err("No such file.");
382 if (stat(filename, &st) < 0) {
383 sr_err("%s", strerror(errno));
388 avail_metadata[midx++] = SR_INPUT_META_FILENAME;
389 avail_metadata[midx++] = SR_INPUT_META_FILESIZE;
390 avail_metadata[midx++] = SR_INPUT_META_HEADER;
391 /* TODO: MIME type */
395 for (i = 0; input_module_list[i]; i++) {
396 imod = input_module_list[i];
397 if (!imod->metadata[0]) {
398 /* Module has no metadata for matching so will take
399 * any input. No point in letting it try to match. */
402 if (!check_required_metadata(imod->metadata, avail_metadata))
403 /* Cannot satisfy this module's requirements. */
406 meta = g_hash_table_new(NULL, NULL);
407 for (m = 0; m < sizeof(imod->metadata); m++) {
408 mitem = imod->metadata[m] & ~SR_INPUT_META_REQUIRED;
409 if (mitem == SR_INPUT_META_FILENAME)
410 g_hash_table_insert(meta, GINT_TO_POINTER(mitem),
412 else if (mitem == SR_INPUT_META_FILESIZE)
413 g_hash_table_insert(meta, GINT_TO_POINTER(mitem),
414 GINT_TO_POINTER(st.st_size));
415 else if (mitem == SR_INPUT_META_HEADER) {
416 buf = g_string_sized_new(128);
417 if ((fd = open(filename, O_RDONLY)) < 0) {
418 sr_err("%s", strerror(errno));
421 size = read(fd, buf->str, 128);
425 g_string_free(buf, TRUE);
426 sr_err("%s", strerror(errno));
429 g_hash_table_insert(meta, GINT_TO_POINTER(mitem), buf);
430 g_string_free(buf, TRUE);
433 if (g_hash_table_size(meta) == 0) {
434 /* No metadata for this module, so there's no way it
436 g_hash_table_destroy(meta);
439 if (!imod->format_match(meta))
440 /* Module didn't recognize this buffer. */
443 /* Found a matching module. */
444 in = sr_input_new(imod, NULL);
445 in->buf = g_string_new_len(buf->str, buf->len);
449 g_string_free(buf, TRUE);
455 * Return the input instance's (virtual) device instance. This can be
456 * used to find out the number of channels and other information.
460 SR_API struct sr_dev_inst *sr_input_dev_inst_get(const struct sr_input *in)
466 * Send data to the specified input instance.
468 * When an input module instance is created with sr_input_new(), this
469 * function is used to feed data to the instance.
473 SR_API int sr_input_send(const struct sr_input *in, GString *buf)
475 return in->module->receive(in, buf);
479 * Free the specified input instance and all associated resources.
483 SR_API int sr_input_free(const struct sr_input *in)
491 if (in->module->cleanup)
492 ret = in->module->cleanup((struct sr_input *)in);
494 g_string_free(in->buf, TRUE);
495 g_free((gpointer)in);