24 #include <glib/gstdio.h>
26 #include "libsigrok-internal.h"
29 #define LOG_PREFIX "input"
32 #define CHUNK_SIZE (4 * 1024 * 1024)
64 extern SR_PRIV struct sr_input_module input_chronovu_la8;
65 extern SR_PRIV struct sr_input_module input_csv;
66 extern SR_PRIV struct sr_input_module input_binary;
67 extern SR_PRIV struct sr_input_module input_trace32_ad;
68 extern SR_PRIV struct sr_input_module input_vcd;
69 extern SR_PRIV struct sr_input_module input_wav;
70 extern SR_PRIV struct sr_input_module input_raw_analog;
71 extern SR_PRIV struct sr_input_module input_logicport;
72 extern SR_PRIV struct sr_input_module input_null;
75 static const struct sr_input_module *input_module_list[] = {
95 return input_module_list;
106 sr_err(
"Invalid input module NULL!");
121 sr_err(
"Invalid input module NULL!");
136 sr_err(
"Invalid input module NULL!");
152 const struct sr_input_module *imod)
155 sr_err(
"Invalid input module NULL!");
172 for (i = 0; input_module_list[i]; i++) {
173 if (!strcmp(input_module_list[i]->
id,
id))
174 return input_module_list[i];
191 const struct sr_option *mod_opts, **opts;
194 if (!imod || !imod->options)
197 mod_opts = imod->options();
199 for (size = 0; mod_opts[size].
id; size++)
201 opts = g_malloc((size + 1) *
sizeof(
struct sr_option *));
203 for (i = 0; i < size; i++)
204 opts[i] = &mod_opts[i];
223 for (i = 0; options[i]; i++) {
224 if (options[i]->
def) {
225 g_variant_unref(options[i]->
def);
226 ((
struct sr_option *)options[i])->def = NULL;
230 g_slist_free_full(options[i]->
values, (GDestroyNotify)g_variant_unref);
231 ((
struct sr_option *)options[i])->values = NULL;
256 const GVariantType *gvt;
257 GHashTable *new_opts;
262 in = g_malloc0(
sizeof(
struct sr_input));
265 new_opts = g_hash_table_new_full(g_str_hash, g_str_equal, g_free,
266 (GDestroyNotify)g_variant_unref);
268 mod_opts = imod->options();
269 for (i = 0; mod_opts[i].
id; i++) {
270 if (options && g_hash_table_lookup_extended(options,
271 mod_opts[i].
id, &key, &value)) {
273 gvt = g_variant_get_type(mod_opts[i].
def);
274 if (!g_variant_is_of_type(value, gvt)) {
275 sr_err(
"Invalid type for '%s' option.",
280 g_hash_table_insert(new_opts, g_strdup(mod_opts[i].
id),
281 g_variant_ref(value));
284 g_hash_table_insert(new_opts, g_strdup(mod_opts[i].
id),
285 g_variant_ref(mod_opts[i].
def));
291 g_hash_table_iter_init(&iter, options);
292 while (g_hash_table_iter_next(&iter, &key, &value)) {
293 if (!g_hash_table_lookup(new_opts, key)) {
294 sr_err(
"Input module '%s' has no option '%s'",
295 imod->id, (
char *)key);
296 g_hash_table_destroy(new_opts);
304 if (in->module->init && in->module->init(in, new_opts) !=
SR_OK) {
308 in->buf = g_string_sized_new(128);
312 g_hash_table_destroy(new_opts);
318 static gboolean check_required_metadata(
const uint8_t *metadata, uint8_t *avail)
323 for (m = 0; metadata[m]; m++) {
324 if (!(metadata[m] & SR_INPUT_META_REQUIRED))
326 reqd = metadata[m] & ~SR_INPUT_META_REQUIRED;
327 for (a = 0; avail[a]; a++) {
328 if (avail[a] == reqd)
362 const struct sr_input_module *imod, *best_imod;
365 unsigned int conf, best_conf;
367 uint8_t mitem, avail_metadata[8];
370 avail_metadata[0] = SR_INPUT_META_HEADER;
371 avail_metadata[1] = 0;
376 for (i = 0; input_module_list[i]; i++) {
377 imod = input_module_list[i];
378 if (!imod->metadata[0]) {
383 if (!check_required_metadata(imod->metadata, avail_metadata))
387 meta = g_hash_table_new(NULL, NULL);
388 for (m = 0; m <
sizeof(imod->metadata); m++) {
389 mitem = imod->metadata[m] & ~SR_INPUT_META_REQUIRED;
390 if (mitem == SR_INPUT_META_HEADER)
391 g_hash_table_insert(meta, GINT_TO_POINTER(mitem), buf);
393 if (g_hash_table_size(meta) == 0) {
395 g_hash_table_destroy(meta);
398 sr_spew(
"Trying module %s.", imod->id);
399 ret = imod->format_match(meta, &conf);
400 g_hash_table_destroy(meta);
404 }
else if (ret ==
SR_ERR) {
407 }
else if (ret !=
SR_OK) {
413 sr_spew(
"Module %s matched, confidence %u.", imod->id, conf);
414 if (conf >= best_conf)
422 g_string_insert_len((*in)->buf, 0, buf->str, buf->len);
442 const struct sr_input_module *imod, *best_imod;
446 unsigned int midx, i;
447 unsigned int conf, best_conf;
449 uint8_t avail_metadata[8];
453 if (!filename || !filename[0]) {
454 sr_err(
"Invalid filename.");
457 stream = g_fopen(filename,
"rb");
459 sr_err(
"Failed to open %s: %s", filename, g_strerror(errno));
462 filesize = sr_file_get_size(stream);
464 sr_err(
"Failed to get size of %s: %s",
465 filename, g_strerror(errno));
470 count = fread(header->str, 1, header->allocated_len - 1, stream);
471 if (count < 1 || ferror(stream)) {
472 sr_err(
"Failed to read %s: %s", filename, g_strerror(errno));
474 g_string_free(header, TRUE);
478 g_string_set_size(header, count);
480 meta = g_hash_table_new(NULL, NULL);
481 g_hash_table_insert(meta, GINT_TO_POINTER(SR_INPUT_META_FILENAME),
483 g_hash_table_insert(meta, GINT_TO_POINTER(SR_INPUT_META_FILESIZE),
484 GSIZE_TO_POINTER(MIN(filesize, G_MAXSSIZE)));
485 g_hash_table_insert(meta, GINT_TO_POINTER(SR_INPUT_META_HEADER),
488 avail_metadata[midx++] = SR_INPUT_META_FILENAME;
489 avail_metadata[midx++] = SR_INPUT_META_FILESIZE;
490 avail_metadata[midx++] = SR_INPUT_META_HEADER;
491 avail_metadata[midx] = 0;
496 for (i = 0; input_module_list[i]; i++) {
497 imod = input_module_list[i];
498 if (!imod->metadata[0]) {
503 if (!check_required_metadata(imod->metadata, avail_metadata))
507 sr_dbg(
"Trying module %s.", imod->id);
509 ret = imod->format_match(meta, &conf);
513 }
else if (ret !=
SR_OK) {
518 sr_dbg(
"Module %s matched, confidence %u.", imod->id, conf);
519 if (conf >= best_conf)
524 g_hash_table_destroy(meta);
525 g_string_free(header, TRUE);
587 len = buf ? buf->len : 0;
588 sr_spew(
"Sending %zu bytes to %s module.", len, in->module->id);
589 return in->module->receive((
struct sr_input *)in, buf);
602 sr_spew(
"Calling end() on %s module.", in->module->id);
603 return in->module->end((
struct sr_input *)in);
620 in = (
struct sr_input *)in_ro;
621 if (!in || !in->module)
628 if (in->module->reset) {
629 sr_spew(
"Resetting %s module.", in->module->id);
630 rc = in->module->reset(in);
632 sr_spew(
"Tried to reset %s module but no reset handler found.",
654 g_string_truncate(in->buf, 0);
655 in->sdi_ready = FALSE;
674 if (in->module->cleanup)
675 in->module->cleanup((
struct sr_input *)in);
683 sr_dev_inst_free(in->sdi);
684 if (in->buf->len > 64) {
686 sr_warn(
"Found %" G_GSIZE_FORMAT
687 " unprocessed bytes at free time.", in->buf->len);
689 g_string_free(in->buf, TRUE);
691 g_free((gpointer)in);
Generic/unspecified error.
struct sr_input * sr_input_new(const struct sr_input_module *imod, GHashTable *options)
Create a new input instance using the specified input module.
Generic option struct used by various subsystems.
int sr_input_send(const struct sr_input *in, GString *buf)
Send data to the specified input instance.
The public libsigrok header file to be used by frontends.
const struct sr_option ** sr_input_options_get(const struct sr_input_module *imod)
Returns a NULL-terminated array of struct sr_option, or NULL if the module takes no options...
const struct sr_input_module ** sr_input_list(void)
Returns a NULL-terminated list of all available input modules.
int sr_input_reset(const struct sr_input *in_ro)
Reset the input module's input handling structures.
void sr_input_options_free(const struct sr_option **options)
After a call to sr_input_options_get(), this function cleans up all resources returned by that call...
const struct sr_input_module * sr_input_find(char *id)
Return the input module with the specified ID, or NULL if no module with that id is found...
int sr_input_end(const struct sr_input *in)
Signal the input module no more data will come.
int sr_input_scan_file(const char *filename, const struct sr_input **in)
Try to find an input module that can parse the given file.
const struct sr_input_module * sr_input_module_get(const struct sr_input *in)
Return the input instance's module "class".
const char * sr_input_name_get(const struct sr_input_module *imod)
Returns the specified input module's name.
int sr_input_scan_buffer(GString *buf, const struct sr_input **in)
Try to find an input module that can parse the given buffer.
const char * sr_input_id_get(const struct sr_input_module *imod)
Returns the specified input module's ID.
void sr_input_free(const struct sr_input *in)
Free the specified input instance and all associated resources.
struct sr_dev_inst * sr_input_dev_inst_get(const struct sr_input *in)
Return the input instance's (virtual) device instance.
const char * sr_input_description_get(const struct sr_input_module *imod)
Returns the specified input module's description.
const char *const * sr_input_extensions_get(const struct sr_input_module *imod)
Returns the specified input module's file extensions typical for the file format, as a NULL terminate...