X-Git-Url: https://sigrok.org/gitweb/?a=blobdiff_plain;f=src%2Finput%2Finput.c;h=b5de532bc58a1c25a4d3430d223a8b5b8ee7299b;hb=e4c9ea56d76cbcc0dbce1eb5386873f25759699f;hp=48660d379b9b6e69fde4546cbe8f91a7a67669b9;hpb=54ee427df0d923c8e17f3dc8ee57552b6c5fd57b;p=libsigrok.git diff --git a/src/input/input.c b/src/input/input.c index 48660d37..b5de532b 100644 --- a/src/input/input.c +++ b/src/input/input.c @@ -29,6 +29,10 @@ #define LOG_PREFIX "input" /** @endcond */ +/** @cond PRIVATE */ +#define CHUNK_SIZE (4 * 1024 * 1024) +/** @endcond */ + /** * @file * @@ -62,23 +66,29 @@ extern SR_PRIV struct sr_input_module input_chronovu_la8; extern SR_PRIV struct sr_input_module input_csv; extern SR_PRIV struct sr_input_module input_binary; +extern SR_PRIV struct sr_input_module input_stf; extern SR_PRIV struct sr_input_module input_trace32_ad; extern SR_PRIV struct sr_input_module input_vcd; extern SR_PRIV struct sr_input_module input_wav; extern SR_PRIV struct sr_input_module input_raw_analog; extern SR_PRIV struct sr_input_module input_logicport; +extern SR_PRIV struct sr_input_module input_saleae; extern SR_PRIV struct sr_input_module input_null; -/* @endcond */ +/** @endcond */ static const struct sr_input_module *input_module_list[] = { &input_binary, &input_chronovu_la8, &input_csv, +#if defined HAVE_INPUT_STF && HAVE_INPUT_STF + &input_stf, +#endif &input_trace32_ad, &input_vcd, &input_wav, &input_raw_analog, &input_logicport, + &input_saleae, &input_null, NULL, }; @@ -338,8 +348,10 @@ static gboolean check_required_metadata(const uint8_t *metadata, uint8_t *avail) * Try to find an input module that can parse the given buffer. * * The buffer must contain enough of the beginning of the file for - * the input modules to find a match. This is format-dependent, but - * 128 bytes is normally enough. + * the input modules to find a match. This is format-dependent. When + * magic strings get checked, 128 bytes normally could be enough. Note + * that some formats try to parse larger header sections, and benefit + * from seeing a larger scope. * * If an input module is found, an instance is created into *in. * Otherwise, *in contains NULL. When multiple input moduless claim @@ -462,11 +474,9 @@ SR_API int sr_input_scan_file(const char *filename, const struct sr_input **in) fclose(stream); return SR_ERR; } - /* This actually allocates 256 bytes to allow for NUL termination. */ - header = g_string_sized_new(255); + header = g_string_sized_new(CHUNK_SIZE); count = fread(header->str, 1, header->allocated_len - 1, stream); - - if (count != header->allocated_len - 1 && ferror(stream)) { + if (count < 1 || ferror(stream)) { sr_err("Failed to read %s: %s", filename, g_strerror(errno)); fclose(stream); g_string_free(header, TRUE); @@ -530,6 +540,22 @@ SR_API int sr_input_scan_file(const char *filename, const struct sr_input **in) return SR_ERR; } +/** + * Return the input instance's module "class". This can be used to find out + * which input module handles a specific input file. This is especially + * useful when an application did not create the input stream by specifying + * an input module, but instead some shortcut or convenience wrapper did. + * + * @since 0.6.0 + */ +SR_API const struct sr_input_module *sr_input_module_get(const struct sr_input *in) +{ + if (!in) + return NULL; + + return in->module; +} + /** * Return the input instance's (virtual) device instance. This can be * used to find out the number of channels and other information. @@ -564,8 +590,10 @@ SR_API struct sr_dev_inst *sr_input_dev_inst_get(const struct sr_input *in) */ SR_API int sr_input_send(const struct sr_input *in, GString *buf) { - sr_spew("Sending %" G_GSIZE_FORMAT " bytes to %s module.", - buf->len, in->module->id); + size_t len; + + len = buf ? buf->len : 0; + sr_spew("Sending %zu bytes to %s module.", len, in->module->id); return in->module->receive((struct sr_input *)in, buf); } @@ -592,16 +620,49 @@ SR_API int sr_input_end(const struct sr_input *in) * * @since 0.5.0 */ -SR_API int sr_input_reset(const struct sr_input *in) +SR_API int sr_input_reset(const struct sr_input *in_ro) { - if (!in->module->reset) { + struct sr_input *in; + int rc; + + in = (struct sr_input *)in_ro; /* "un-const" */ + if (!in || !in->module) + return SR_ERR_ARG; + + /* + * Run the optional input module's .reset() method. This shall + * take care of the context (kept in the 'inc' variable). + */ + if (in->module->reset) { + sr_spew("Resetting %s module.", in->module->id); + rc = in->module->reset(in); + } else { sr_spew("Tried to reset %s module but no reset handler found.", in->module->id); - return SR_OK; + rc = SR_OK; } - sr_spew("Resetting %s module.", in->module->id); - return in->module->reset((struct sr_input *)in); + /* + * Handle input module status (kept in the 'in' variable) here + * in common logic. This agrees with how input module's receive() + * and end() routines "amend but never seed" the 'in' information. + * + * Void potentially accumulated receive() buffer content, and + * clear the sdi_ready flag. This makes sure that subsequent + * processing will scan the header again before sample data gets + * interpreted, and stale content from previous calls won't affect + * the result. + * + * This common logic does not harm when the input module implements + * .reset() and contains identical assignments. In the absence of + * an individual .reset() method, simple input modules can completely + * rely on common code and keep working across resets. + */ + if (in->buf) + g_string_truncate(in->buf, 0); + in->sdi_ready = FALSE; + + return rc; } /** @@ -614,8 +675,19 @@ SR_API void sr_input_free(const struct sr_input *in) if (!in) return; + /* + * Run the input module's optional .cleanup() routine. This + * takes care of the context (kept in the 'inc' variable). + */ if (in->module->cleanup) in->module->cleanup((struct sr_input *)in); + + /* + * Common code releases the input module's state (kept in the + * 'in' variable). Release the device instance, the receive() + * buffer, the shallow 'in->priv' block which is 'inc' (after + * .cleanup() released potentially nested resources under 'inc'). + */ sr_dev_inst_free(in->sdi); if (in->buf->len > 64) { /* That seems more than just some sub-unitsize leftover... */