24 #include <glib/gstdio.h>
26 #include "libsigrok-internal.h"
29 #define LOG_PREFIX "input"
62 extern SR_PRIV struct sr_input_module input_chronovu_la8;
63 extern SR_PRIV struct sr_input_module input_csv;
64 extern SR_PRIV struct sr_input_module input_binary;
65 extern SR_PRIV struct sr_input_module input_trace32_ad;
66 extern SR_PRIV struct sr_input_module input_vcd;
67 extern SR_PRIV struct sr_input_module input_wav;
68 extern SR_PRIV struct sr_input_module input_raw_analog;
71 static const struct sr_input_module *input_module_list[] = {
89 return input_module_list;
100 sr_err(
"Invalid input module NULL!");
115 sr_err(
"Invalid input module NULL!");
130 sr_err(
"Invalid input module NULL!");
146 const struct sr_input_module *imod)
149 sr_err(
"Invalid input module NULL!");
166 for (i = 0; input_module_list[i]; i++) {
167 if (!strcmp(input_module_list[i]->
id,
id))
168 return input_module_list[i];
185 const struct sr_option *mod_opts, **opts;
188 if (!imod || !imod->options)
191 mod_opts = imod->options();
193 for (size = 0; mod_opts[size].
id; size++)
195 opts = g_malloc((size + 1) *
sizeof(
struct sr_option *));
197 for (i = 0; i < size; i++)
198 opts[i] = &mod_opts[i];
217 for (i = 0; options[i]; i++) {
218 if (options[i]->
def) {
219 g_variant_unref(options[i]->
def);
220 ((
struct sr_option *)options[i])->def = NULL;
224 g_slist_free_full(options[i]->
values, (GDestroyNotify)g_variant_unref);
225 ((
struct sr_option *)options[i])->values = NULL;
250 const GVariantType *gvt;
251 GHashTable *new_opts;
256 in = g_malloc0(
sizeof(
struct sr_input));
259 new_opts = g_hash_table_new_full(g_str_hash, g_str_equal, g_free,
260 (GDestroyNotify)g_variant_unref);
262 mod_opts = imod->options();
263 for (i = 0; mod_opts[i].
id; i++) {
264 if (options && g_hash_table_lookup_extended(options,
265 mod_opts[i].
id, &key, &value)) {
267 gvt = g_variant_get_type(mod_opts[i].
def);
268 if (!g_variant_is_of_type(value, gvt)) {
269 sr_err(
"Invalid type for '%s' option.",
274 g_hash_table_insert(new_opts, g_strdup(mod_opts[i].
id),
275 g_variant_ref(value));
278 g_hash_table_insert(new_opts, g_strdup(mod_opts[i].
id),
279 g_variant_ref(mod_opts[i].
def));
285 g_hash_table_iter_init(&iter, options);
286 while (g_hash_table_iter_next(&iter, &key, &value)) {
287 if (!g_hash_table_lookup(new_opts, key)) {
288 sr_err(
"Input module '%s' has no option '%s'",
289 imod->id, (
char *)key);
290 g_hash_table_destroy(new_opts);
298 if (in->module->init && in->module->init(in, new_opts) !=
SR_OK) {
302 in->buf = g_string_sized_new(128);
306 g_hash_table_destroy(new_opts);
312 static gboolean check_required_metadata(
const uint8_t *metadata, uint8_t *avail)
317 for (m = 0; metadata[m]; m++) {
318 if (!(metadata[m] & SR_INPUT_META_REQUIRED))
320 reqd = metadata[m] & ~SR_INPUT_META_REQUIRED;
321 for (a = 0; avail[a]; a++) {
322 if (avail[a] == reqd)
352 const struct sr_input_module *imod;
356 uint8_t mitem, avail_metadata[8];
359 avail_metadata[0] = SR_INPUT_META_HEADER;
360 avail_metadata[1] = 0;
364 for (i = 0; input_module_list[i]; i++) {
365 imod = input_module_list[i];
366 if (!imod->metadata[0]) {
371 if (!check_required_metadata(imod->metadata, avail_metadata))
375 meta = g_hash_table_new(NULL, NULL);
376 for (m = 0; m <
sizeof(imod->metadata); m++) {
377 mitem = imod->metadata[m] & ~SR_INPUT_META_REQUIRED;
378 if (mitem == SR_INPUT_META_HEADER)
379 g_hash_table_insert(meta, GINT_TO_POINTER(mitem), buf);
381 if (g_hash_table_size(meta) == 0) {
383 g_hash_table_destroy(meta);
386 sr_spew(
"Trying module %s.", imod->id);
387 ret = imod->format_match(meta);
388 g_hash_table_destroy(meta);
392 }
else if (ret ==
SR_ERR) {
395 }
else if (ret !=
SR_OK) {
401 sr_spew(
"Module %s matched.", imod->id);
403 g_string_insert_len((*in)->buf, 0, buf->str, buf->len);
421 const struct sr_input_module *imod;
425 unsigned int midx, i;
427 uint8_t avail_metadata[8];
431 if (!filename || !filename[0]) {
432 sr_err(
"Invalid filename.");
435 stream = g_fopen(filename,
"rb");
437 sr_err(
"Failed to open %s: %s", filename, g_strerror(errno));
440 filesize = sr_file_get_size(stream);
442 sr_err(
"Failed to get size of %s: %s",
443 filename, g_strerror(errno));
448 header = g_string_sized_new(255);
449 count = fread(header->str, 1, header->allocated_len - 1, stream);
451 if (count != header->allocated_len - 1 && ferror(stream)) {
452 sr_err(
"Failed to read %s: %s", filename, g_strerror(errno));
454 g_string_free(header, TRUE);
458 g_string_set_size(header, count);
460 meta = g_hash_table_new(NULL, NULL);
461 g_hash_table_insert(meta, GINT_TO_POINTER(SR_INPUT_META_FILENAME),
463 g_hash_table_insert(meta, GINT_TO_POINTER(SR_INPUT_META_FILESIZE),
464 GSIZE_TO_POINTER(MIN(filesize, G_MAXSSIZE)));
465 g_hash_table_insert(meta, GINT_TO_POINTER(SR_INPUT_META_HEADER),
468 avail_metadata[midx++] = SR_INPUT_META_FILENAME;
469 avail_metadata[midx++] = SR_INPUT_META_FILESIZE;
470 avail_metadata[midx++] = SR_INPUT_META_HEADER;
471 avail_metadata[midx] = 0;
476 for (i = 0; input_module_list[i]; i++) {
477 imod = input_module_list[i];
478 if (!imod->metadata[0]) {
483 if (!check_required_metadata(imod->metadata, avail_metadata))
487 sr_dbg(
"Trying module %s.", imod->id);
489 ret = imod->format_match(meta);
493 }
else if (ret !=
SR_OK) {
498 sr_dbg(
"Module %s matched.", imod->id);
503 g_hash_table_destroy(meta);
504 g_string_free(header, TRUE);
543 sr_spew(
"Sending %" G_GSIZE_FORMAT
" bytes to %s module.",
544 buf->len, in->module->id);
545 return in->module->receive((
struct sr_input *)in, buf);
558 sr_spew(
"Calling end() on %s module.", in->module->id);
559 return in->module->end((
struct sr_input *)in);
572 if (in->module->cleanup)
573 in->module->cleanup((
struct sr_input *)in);
575 sr_dev_inst_free(in->sdi);
576 if (in->buf->len > 64) {
578 sr_warn(
"Found %" G_GSIZE_FORMAT
579 " unprocessed bytes at free time.", in->buf->len);
581 g_string_free(in->buf, TRUE);
583 g_free((gpointer)in);
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...
The public libsigrok header file to be used by frontends.
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...
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...
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.
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...
int sr_input_scan_buffer(GString *buf, const struct sr_input **in)
Try to find an input module that can parse the given buffer.
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.
Generic option struct used by various subsystems.
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_send(const struct sr_input *in, GString *buf)
Send data to the specified input instance.
const struct sr_input_module ** sr_input_list(void)
Returns a NULL-terminated list of all available input modules.
const char * sr_input_name_get(const struct sr_input_module *imod)
Returns the specified input module's name.