24 #include <glib/gstdio.h> 26 #include "libsigrok-internal.h" 29 #define LOG_PREFIX "input" 33 #define CHUNK_SIZE (4 * 1024 * 1024) 66 extern SR_PRIV struct sr_input_module input_binary;
67 extern SR_PRIV struct sr_input_module input_chronovu_la8;
68 extern SR_PRIV struct sr_input_module input_csv;
69 extern SR_PRIV struct sr_input_module input_logicport;
70 extern SR_PRIV struct sr_input_module input_null;
71 extern SR_PRIV struct sr_input_module input_protocoldata;
72 extern SR_PRIV struct sr_input_module input_raw_analog;
73 extern SR_PRIV struct sr_input_module input_saleae;
74 extern SR_PRIV struct sr_input_module input_stf;
75 extern SR_PRIV struct sr_input_module input_trace32_ad;
76 extern SR_PRIV struct sr_input_module input_vcd;
77 extern SR_PRIV struct sr_input_module input_wav;
80 static const struct sr_input_module *input_module_list[] = {
89 #if defined HAVE_INPUT_STF && HAVE_INPUT_STF 105 return input_module_list;
116 sr_err(
"Invalid input module NULL!");
131 sr_err(
"Invalid input module NULL!");
146 sr_err(
"Invalid input module NULL!");
162 const struct sr_input_module *imod)
165 sr_err(
"Invalid input module NULL!");
182 for (i = 0; input_module_list[i]; i++) {
183 if (!strcmp(input_module_list[i]->
id,
id))
184 return input_module_list[i];
201 const struct sr_option *mod_opts, **opts;
204 if (!imod || !imod->options)
207 mod_opts = imod->options();
209 for (size = 0; mod_opts[
size].
id; size++)
211 opts = g_malloc((size + 1) *
sizeof(
struct sr_option *));
213 for (i = 0; i <
size; i++)
214 opts[i] = &mod_opts[i];
233 for (i = 0; options[i]; i++) {
234 if (options[i]->
def) {
235 g_variant_unref(options[i]->
def);
236 ((
struct sr_option *)options[i])->def = NULL;
240 g_slist_free_full(options[i]->
values, (GDestroyNotify)g_variant_unref);
241 ((
struct sr_option *)options[i])->values = NULL;
266 const GVariantType *gvt;
267 GHashTable *new_opts;
272 in = g_malloc0(
sizeof(
struct sr_input));
275 new_opts = g_hash_table_new_full(g_str_hash, g_str_equal, g_free,
276 (GDestroyNotify)g_variant_unref);
278 mod_opts = imod->options();
279 for (i = 0; mod_opts[i].
id; i++) {
280 if (options && g_hash_table_lookup_extended(options,
281 mod_opts[i].
id, &key, &value)) {
283 gvt = g_variant_get_type(mod_opts[i].
def);
284 if (!g_variant_is_of_type(value, gvt)) {
285 sr_err(
"Invalid type for '%s' option.",
290 g_hash_table_insert(new_opts, g_strdup(mod_opts[i].
id),
291 g_variant_ref(value));
294 g_hash_table_insert(new_opts, g_strdup(mod_opts[i].
id),
295 g_variant_ref(mod_opts[i].
def));
301 g_hash_table_iter_init(&iter, options);
302 while (g_hash_table_iter_next(&iter, &key, &value)) {
303 if (!g_hash_table_lookup(new_opts, key)) {
304 sr_err(
"Input module '%s' has no option '%s'",
305 imod->id, (
char *)key);
306 g_hash_table_destroy(new_opts);
314 if (in->module->init && in->module->init(in, new_opts) !=
SR_OK) {
318 in->buf = g_string_sized_new(128);
322 g_hash_table_destroy(new_opts);
328 static gboolean check_required_metadata(
const uint8_t *metadata, uint8_t *avail)
333 for (m = 0; metadata[m]; m++) {
334 if (!(metadata[m] & SR_INPUT_META_REQUIRED))
336 reqd = metadata[m] & ~SR_INPUT_META_REQUIRED;
337 for (a = 0; avail[a]; a++) {
338 if (avail[a] == reqd)
372 const struct sr_input_module *imod, *best_imod;
375 unsigned int conf, best_conf;
377 uint8_t mitem, avail_metadata[8];
380 avail_metadata[0] = SR_INPUT_META_HEADER;
381 avail_metadata[1] = 0;
386 for (i = 0; input_module_list[i]; i++) {
387 imod = input_module_list[i];
388 if (!imod->metadata[0]) {
393 if (!check_required_metadata(imod->metadata, avail_metadata))
397 meta = g_hash_table_new(NULL, NULL);
398 for (m = 0; m <
sizeof(imod->metadata); m++) {
399 mitem = imod->metadata[m] & ~SR_INPUT_META_REQUIRED;
400 if (mitem == SR_INPUT_META_HEADER)
401 g_hash_table_insert(meta, GINT_TO_POINTER(mitem), buf);
403 if (g_hash_table_size(meta) == 0) {
405 g_hash_table_destroy(meta);
408 sr_spew(
"Trying module %s.", imod->id);
409 ret = imod->format_match(meta, &conf);
410 g_hash_table_destroy(meta);
414 }
else if (ret ==
SR_ERR) {
417 }
else if (ret !=
SR_OK) {
423 sr_spew(
"Module %s matched, confidence %u.", imod->id, conf);
424 if (conf >= best_conf)
432 g_string_insert_len((*in)->buf, 0, buf->str, buf->len);
452 const struct sr_input_module *imod, *best_imod;
456 unsigned int midx, i;
457 unsigned int conf, best_conf;
459 uint8_t avail_metadata[8];
463 if (!filename || !filename[0]) {
464 sr_err(
"Invalid filename.");
467 stream = g_fopen(filename,
"rb");
469 sr_err(
"Failed to open %s: %s", filename, g_strerror(errno));
472 filesize = sr_file_get_size(stream);
474 sr_err(
"Failed to get size of %s: %s",
475 filename, g_strerror(errno));
479 header = g_string_sized_new(CHUNK_SIZE);
480 count = fread(header->str, 1, header->allocated_len - 1, stream);
481 if (count < 1 || ferror(stream)) {
482 sr_err(
"Failed to read %s: %s", filename, g_strerror(errno));
484 g_string_free(header, TRUE);
488 g_string_set_size(header, count);
490 meta = g_hash_table_new(NULL, NULL);
491 g_hash_table_insert(meta, GINT_TO_POINTER(SR_INPUT_META_FILENAME),
493 g_hash_table_insert(meta, GINT_TO_POINTER(SR_INPUT_META_FILESIZE),
494 GSIZE_TO_POINTER(MIN(filesize, G_MAXSSIZE)));
495 g_hash_table_insert(meta, GINT_TO_POINTER(SR_INPUT_META_HEADER),
498 avail_metadata[midx++] = SR_INPUT_META_FILENAME;
499 avail_metadata[midx++] = SR_INPUT_META_FILESIZE;
500 avail_metadata[midx++] = SR_INPUT_META_HEADER;
501 avail_metadata[midx] = 0;
506 for (i = 0; input_module_list[i]; i++) {
507 imod = input_module_list[i];
508 if (!imod->metadata[0]) {
513 if (!check_required_metadata(imod->metadata, avail_metadata))
517 sr_dbg(
"Trying module %s.", imod->id);
519 ret = imod->format_match(meta, &conf);
523 }
else if (ret !=
SR_OK) {
528 sr_dbg(
"Module %s matched, confidence %u.", imod->id, conf);
529 if (conf >= best_conf)
534 g_hash_table_destroy(meta);
535 g_string_free(header, TRUE);
597 len = buf ? buf->len : 0;
598 sr_spew(
"Sending %zu bytes to %s module.", len, in->module->id);
599 return in->module->receive((
struct sr_input *)in, buf);
612 sr_spew(
"Calling end() on %s module.", in->module->id);
613 return in->module->end((
struct sr_input *)in);
630 in = (
struct sr_input *)in_ro;
631 if (!in || !in->module)
638 if (in->module->reset) {
639 sr_spew(
"Resetting %s module.", in->module->id);
640 rc = in->module->reset(in);
642 sr_spew(
"Tried to reset %s module but no reset handler found.",
664 g_string_truncate(in->buf, 0);
665 in->sdi_ready = FALSE;
684 if (in->module->cleanup)
685 in->module->cleanup((
struct sr_input *)in);
693 sr_dev_inst_free(in->sdi);
694 if (in->buf->len > 64) {
696 sr_warn(
"Found %" G_GSIZE_FORMAT
697 " unprocessed bytes at free time.", in->buf->len);
699 g_string_free(in->buf, TRUE);
701 g_free((gpointer)in);
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.
int sr_input_reset(const struct sr_input *in_ro)
Reset the input module's input handling structures.
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.
int sr_input_end(const struct sr_input *in)
Signal the input module no more data will come.
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...
Generic/unspecified error.
const struct sr_input_module ** sr_input_list(void)
Returns a NULL-terminated list of all available input modules.
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.
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...
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(const char *id)
Return the input module with the specified ID, or NULL if no module with that id is found...
The public libsigrok header file to be used by frontends.
struct sr_input * sr_input_new(const struct sr_input_module *imod, GHashTable *options)
Create a new input instance using the specified input module.
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.