]> sigrok.org Git - libsigrok.git/blame - src/input/input.c
output/csv: use intermediate time_t var, silence compiler warning
[libsigrok.git] / src / input / input.c
CommitLineData
34e4813f 1/*
50985c20 2 * This file is part of the libsigrok project.
34e4813f 3 *
17bfaca6 4 * Copyright (C) 2014 Bert Vermeulen <bert@biot.com>
34e4813f
BV
5 *
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.
10 *
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.
15 *
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/>.
18 */
19
6ec6c43b 20#include <config.h>
17bfaca6 21#include <string.h>
17bfaca6 22#include <errno.h>
5e364d4f
DE
23#include <glib.h>
24#include <glib/gstdio.h>
c1aae900 25#include <libsigrok/libsigrok.h>
45c59c8b 26#include "libsigrok-internal.h"
34e4813f 27
e00b3f58 28/** @cond PRIVATE */
17bfaca6 29#define LOG_PREFIX "input"
e00b3f58 30/** @endcond */
17bfaca6 31
82b9f3d1 32/** @cond PRIVATE */
0dabb880 33#define CHUNK_SIZE (4 * 1024 * 1024)
82b9f3d1 34/** @endcond */
0dabb880 35
393fb9cb
UH
36/**
37 * @file
38 *
0f3dbc95 39 * Input module handling.
393fb9cb
UH
40 */
41
7b870c38 42/**
d4c93774 43 * @defgroup grp_input Input modules
7b870c38 44 *
d4c93774 45 * Input file/data module handling.
7b870c38 46 *
83687343 47 * libsigrok can process acquisition data in several different ways.
0f3dbc95
BV
48 * Aside from acquiring data from a hardware device, it can also take it
49 * from a file in various formats (binary, CSV, VCD, and so on).
83687343 50 *
0f3dbc95
BV
51 * Like all libsigrok data handling, processing is done in a streaming
52 * manner: input should be supplied a chunk at a time. This way anything
53 * that processes data can do so in real time, without the user having
54 * to wait for the whole thing to be finished.
83687343
UH
55 *
56 * Every input module is "pluggable", meaning it's handled as being separate
57 * from the main libsigrok, but linked in to it statically. To keep things
58 * modular and separate like this, functions within an input module should be
d4c93774 59 * declared static, with only the respective 'struct sr_input_module' being
83687343
UH
60 * exported for use into the wider libsigrok namespace.
61 *
7b870c38
UH
62 * @{
63 */
64
b4bd7088 65/** @cond PRIVATE */
b6c6daa1 66extern SR_PRIV struct sr_input_module input_binary;
d4c93774
BV
67extern SR_PRIV struct sr_input_module input_chronovu_la8;
68extern SR_PRIV struct sr_input_module input_csv;
b6c6daa1
GS
69extern SR_PRIV struct sr_input_module input_logicport;
70extern SR_PRIV struct sr_input_module input_null;
71extern SR_PRIV struct sr_input_module input_protocoldata;
72extern SR_PRIV struct sr_input_module input_raw_analog;
73extern SR_PRIV struct sr_input_module input_saleae;
e972674d 74extern SR_PRIV struct sr_input_module input_stf;
6d2897e3 75extern SR_PRIV struct sr_input_module input_trace32_ad;
d4c93774
BV
76extern SR_PRIV struct sr_input_module input_vcd;
77extern SR_PRIV struct sr_input_module input_wav;
82b9f3d1 78/** @endcond */
34e4813f 79
17bfaca6 80static const struct sr_input_module *input_module_list[] = {
b84cba4d 81 &input_binary,
02e24c0c 82 &input_chronovu_la8,
41d214f6 83 &input_csv,
b6c6daa1
GS
84 &input_logicport,
85 &input_null,
86 &input_protocoldata,
87 &input_raw_analog,
88 &input_saleae,
e4c9ea56 89#if defined HAVE_INPUT_STF && HAVE_INPUT_STF
e972674d 90 &input_stf,
e4c9ea56 91#endif
6d2897e3 92 &input_trace32_ad,
b84cba4d
BV
93 &input_vcd,
94 &input_wav,
34e4813f
BV
95 NULL,
96};
97
0f3dbc95
BV
98/**
99 * Returns a NULL-terminated list of all available input modules.
100 *
101 * @since 0.4.0
102 */
17bfaca6 103SR_API const struct sr_input_module **sr_input_list(void)
34e4813f 104{
34e4813f
BV
105 return input_module_list;
106}
7b870c38 107
17bfaca6
BV
108/**
109 * Returns the specified input module's ID.
110 *
111 * @since 0.4.0
112 */
4fb0a5f8 113SR_API const char *sr_input_id_get(const struct sr_input_module *imod)
17bfaca6 114{
4fb0a5f8 115 if (!imod) {
17bfaca6
BV
116 sr_err("Invalid input module NULL!");
117 return NULL;
118 }
119
4fb0a5f8 120 return imod->id;
17bfaca6
BV
121}
122
123/**
124 * Returns the specified input module's name.
125 *
126 * @since 0.4.0
127 */
4fb0a5f8 128SR_API const char *sr_input_name_get(const struct sr_input_module *imod)
17bfaca6 129{
4fb0a5f8 130 if (!imod) {
17bfaca6
BV
131 sr_err("Invalid input module NULL!");
132 return NULL;
133 }
134
4fb0a5f8 135 return imod->name;
17bfaca6
BV
136}
137
138/**
139 * Returns the specified input module's description.
140 *
141 * @since 0.4.0
142 */
4fb0a5f8 143SR_API const char *sr_input_description_get(const struct sr_input_module *imod)
17bfaca6 144{
4fb0a5f8 145 if (!imod) {
17bfaca6
BV
146 sr_err("Invalid input module NULL!");
147 return NULL;
148 }
149
4fb0a5f8 150 return imod->desc;
17bfaca6
BV
151}
152
c7bc82ff
JH
153/**
154 * Returns the specified input module's file extensions typical for the file
155 * format, as a NULL terminated array, or returns a NULL pointer if there is
156 * no preferred extension.
157 * @note these are a suggestions only.
158 *
159 * @since 0.4.0
160 */
161SR_API const char *const *sr_input_extensions_get(
4fb0a5f8 162 const struct sr_input_module *imod)
c7bc82ff 163{
4fb0a5f8 164 if (!imod) {
c7bc82ff
JH
165 sr_err("Invalid input module NULL!");
166 return NULL;
167 }
168
4fb0a5f8 169 return imod->exts;
c7bc82ff
JH
170}
171
17bfaca6
BV
172/**
173 * Return the input module with the specified ID, or NULL if no module
174 * with that id is found.
175 *
176 * @since 0.4.0
177 */
e3847923 178SR_API const struct sr_input_module *sr_input_find(const char *id)
17bfaca6
BV
179{
180 int i;
181
182 for (i = 0; input_module_list[i]; i++) {
183 if (!strcmp(input_module_list[i]->id, id))
184 return input_module_list[i];
185 }
186
187 return NULL;
188}
189
190/**
191 * Returns a NULL-terminated array of struct sr_option, or NULL if the
192 * module takes no options.
193 *
194 * Each call to this function must be followed by a call to
195 * sr_input_options_free().
196 *
197 * @since 0.4.0
198 */
bd0bfaaf 199SR_API const struct sr_option **sr_input_options_get(const struct sr_input_module *imod)
17bfaca6 200{
bd0bfaaf
BV
201 const struct sr_option *mod_opts, **opts;
202 int size, i;
17bfaca6 203
bd0bfaaf 204 if (!imod || !imod->options)
17bfaca6
BV
205 return NULL;
206
bd0bfaaf
BV
207 mod_opts = imod->options();
208
fe4fe25b 209 for (size = 0; mod_opts[size].id; size++)
bd0bfaaf 210 ;
fe4fe25b 211 opts = g_malloc((size + 1) * sizeof(struct sr_option *));
bd0bfaaf
BV
212
213 for (i = 0; i < size; i++)
214 opts[i] = &mod_opts[i];
215 opts[i] = NULL;
216
217 return opts;
17bfaca6
BV
218}
219
220/**
221 * After a call to sr_input_options_get(), this function cleans up all
bd0bfaaf 222 * resources returned by that call.
17bfaca6
BV
223 *
224 * @since 0.4.0
225 */
bd0bfaaf 226SR_API void sr_input_options_free(const struct sr_option **options)
17bfaca6 227{
fe4fe25b 228 int i;
17bfaca6 229
bd0bfaaf 230 if (!options)
17bfaca6
BV
231 return;
232
fe4fe25b
BV
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;
17bfaca6
BV
237 }
238
fe4fe25b
BV
239 if (options[i]->values) {
240 g_slist_free_full(options[i]->values, (GDestroyNotify)g_variant_unref);
241 ((struct sr_option *)options[i])->values = NULL;
17bfaca6
BV
242 }
243 }
bd0bfaaf 244 g_free(options);
17bfaca6
BV
245}
246
247/**
248 * Create a new input instance using the specified input module.
249 *
250 * This function is used when a client wants to use a specific input
251 * module to parse a stream. No effort is made to identify the format.
252 *
7a54232b 253 * @param imod The input module to use. Must not be NULL.
0f3dbc95 254 * @param options GHashTable consisting of keys corresponding with
dff0a894 255 * the module options @c id field. The values should be GVariant
0f3dbc95 256 * pointers with sunk references, of the same GVariantType as the option's
17bfaca6
BV
257 * default value.
258 *
259 * @since 0.4.0
260 */
261SR_API struct sr_input *sr_input_new(const struct sr_input_module *imod,
262 GHashTable *options)
263{
264 struct sr_input *in;
2c240774 265 const struct sr_option *mod_opts;
17bfaca6
BV
266 const GVariantType *gvt;
267 GHashTable *new_opts;
268 GHashTableIter iter;
269 gpointer key, value;
270 int i;
271
272 in = g_malloc0(sizeof(struct sr_input));
273 in->module = imod;
274
7db06394
BV
275 new_opts = g_hash_table_new_full(g_str_hash, g_str_equal, g_free,
276 (GDestroyNotify)g_variant_unref);
277 if (imod->options) {
17bfaca6
BV
278 mod_opts = imod->options();
279 for (i = 0; mod_opts[i].id; i++) {
7db06394
BV
280 if (options && g_hash_table_lookup_extended(options,
281 mod_opts[i].id, &key, &value)) {
17bfaca6
BV
282 /* Option not given: insert the default value. */
283 gvt = g_variant_get_type(mod_opts[i].def);
284 if (!g_variant_is_of_type(value, gvt)) {
6433156c
DE
285 sr_err("Invalid type for '%s' option.",
286 (char *)key);
17bfaca6
BV
287 g_free(in);
288 return NULL;
289 }
290 g_hash_table_insert(new_opts, g_strdup(mod_opts[i].id),
291 g_variant_ref(value));
292 } else {
293 /* Pass option along. */
294 g_hash_table_insert(new_opts, g_strdup(mod_opts[i].id),
295 g_variant_ref(mod_opts[i].def));
296 }
297 }
298
299 /* Make sure no invalid options were given. */
7db06394
BV
300 if (options) {
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)) {
6433156c
DE
304 sr_err("Input module '%s' has no option '%s'",
305 imod->id, (char *)key);
7db06394
BV
306 g_hash_table_destroy(new_opts);
307 g_free(in);
308 return NULL;
309 }
17bfaca6
BV
310 }
311 }
7db06394 312 }
17bfaca6
BV
313
314 if (in->module->init && in->module->init(in, new_opts) != SR_OK) {
17bfaca6
BV
315 g_free(in);
316 in = NULL;
64098211
BV
317 } else {
318 in->buf = g_string_sized_new(128);
17bfaca6 319 }
64098211 320
17bfaca6
BV
321 if (new_opts)
322 g_hash_table_destroy(new_opts);
323
324 return in;
325}
326
64098211 327/* Returns TRUE if all required meta items are available. */
17bfaca6
BV
328static gboolean check_required_metadata(const uint8_t *metadata, uint8_t *avail)
329{
330 int m, a;
331 uint8_t reqd;
332
333 for (m = 0; metadata[m]; m++) {
7db06394 334 if (!(metadata[m] & SR_INPUT_META_REQUIRED))
17bfaca6
BV
335 continue;
336 reqd = metadata[m] & ~SR_INPUT_META_REQUIRED;
337 for (a = 0; avail[a]; a++) {
338 if (avail[a] == reqd)
339 break;
340 }
341 if (!avail[a])
342 /* Found a required meta item that isn't available. */
343 return FALSE;
344 }
345
346 return TRUE;
347}
348
349/**
350 * Try to find an input module that can parse the given buffer.
351 *
352 * The buffer must contain enough of the beginning of the file for
0dabb880
GS
353 * the input modules to find a match. This is format-dependent. When
354 * magic strings get checked, 128 bytes normally could be enough. Note
355 * that some formats try to parse larger header sections, and benefit
356 * from seeing a larger scope.
17bfaca6 357 *
4f979115 358 * If an input module is found, an instance is created into *in.
54ee427d
GS
359 * Otherwise, *in contains NULL. When multiple input moduless claim
360 * support for the format, the one with highest confidence takes
361 * precedence. Applications will see at most one input module spec.
17bfaca6 362 *
33e4303b
BV
363 * If an instance is created, it has the given buffer used for scanning
364 * already submitted to it, to be processed before more data is sent.
365 * This allows a frontend to submit an initial chunk of a non-seekable
366 * stream, such as stdin, without having to keep it around and submit
367 * it again later.
368 *
17bfaca6 369 */
4f979115 370SR_API int sr_input_scan_buffer(GString *buf, const struct sr_input **in)
17bfaca6 371{
54ee427d 372 const struct sr_input_module *imod, *best_imod;
17bfaca6
BV
373 GHashTable *meta;
374 unsigned int m, i;
54ee427d 375 unsigned int conf, best_conf;
4f979115 376 int ret;
17bfaca6
BV
377 uint8_t mitem, avail_metadata[8];
378
379 /* No more metadata to be had from a buffer. */
380 avail_metadata[0] = SR_INPUT_META_HEADER;
381 avail_metadata[1] = 0;
382
4f979115 383 *in = NULL;
54ee427d
GS
384 best_imod = NULL;
385 best_conf = ~0;
17bfaca6
BV
386 for (i = 0; input_module_list[i]; i++) {
387 imod = input_module_list[i];
388 if (!imod->metadata[0]) {
389 /* Module has no metadata for matching so will take
390 * any input. No point in letting it try to match. */
391 continue;
392 }
393 if (!check_required_metadata(imod->metadata, avail_metadata))
394 /* Cannot satisfy this module's requirements. */
395 continue;
396
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);
402 }
403 if (g_hash_table_size(meta) == 0) {
404 /* No metadata for this module, so nothing to match. */
405 g_hash_table_destroy(meta);
406 continue;
407 }
4f979115 408 sr_spew("Trying module %s.", imod->id);
54ee427d 409 ret = imod->format_match(meta, &conf);
4f979115
BV
410 g_hash_table_destroy(meta);
411 if (ret == SR_ERR_DATA) {
412 /* Module recognized this buffer, but cannot handle it. */
54ee427d 413 continue;
4f979115 414 } else if (ret == SR_ERR) {
17bfaca6 415 /* Module didn't recognize this buffer. */
17bfaca6 416 continue;
4f979115 417 } else if (ret != SR_OK) {
60107497 418 /* Can be SR_ERR_NA. */
54ee427d 419 continue;
17bfaca6 420 }
17bfaca6
BV
421
422 /* Found a matching module. */
54ee427d
GS
423 sr_spew("Module %s matched, confidence %u.", imod->id, conf);
424 if (conf >= best_conf)
425 continue;
426 best_imod = imod;
427 best_conf = conf;
428 }
429
430 if (best_imod) {
431 *in = sr_input_new(best_imod, NULL);
4f979115 432 g_string_insert_len((*in)->buf, 0, buf->str, buf->len);
54ee427d 433 return SR_OK;
17bfaca6
BV
434 }
435
54ee427d 436 return SR_ERR;
17bfaca6
BV
437}
438
439/**
440 * Try to find an input module that can parse the given file.
441 *
4f979115 442 * If an input module is found, an instance is created into *in.
54ee427d
GS
443 * Otherwise, *in contains NULL. When multiple input moduless claim
444 * support for the format, the one with highest confidence takes
445 * precedence. Applications will see at most one input module spec.
17bfaca6
BV
446 *
447 */
4f979115 448SR_API int sr_input_scan_file(const char *filename, const struct sr_input **in)
17bfaca6 449{
4619fab4 450 int64_t filesize;
5e364d4f 451 FILE *stream;
54ee427d 452 const struct sr_input_module *imod, *best_imod;
17bfaca6 453 GHashTable *meta;
5e364d4f 454 GString *header;
5e364d4f
DE
455 size_t count;
456 unsigned int midx, i;
54ee427d 457 unsigned int conf, best_conf;
5e364d4f
DE
458 int ret;
459 uint8_t avail_metadata[8];
460
461 *in = NULL;
17bfaca6
BV
462
463 if (!filename || !filename[0]) {
464 sr_err("Invalid filename.");
4f979115 465 return SR_ERR_ARG;
17bfaca6 466 }
5e364d4f
DE
467 stream = g_fopen(filename, "rb");
468 if (!stream) {
469 sr_err("Failed to open %s: %s", filename, g_strerror(errno));
470 return SR_ERR;
471 }
4619fab4
DE
472 filesize = sr_file_get_size(stream);
473 if (filesize < 0) {
474 sr_err("Failed to get size of %s: %s",
475 filename, g_strerror(errno));
476 fclose(stream);
477 return SR_ERR;
478 }
0dabb880 479 header = g_string_sized_new(CHUNK_SIZE);
5e364d4f 480 count = fread(header->str, 1, header->allocated_len - 1, stream);
0dabb880 481 if (count < 1 || ferror(stream)) {
5e364d4f
DE
482 sr_err("Failed to read %s: %s", filename, g_strerror(errno));
483 fclose(stream);
484 g_string_free(header, TRUE);
485 return SR_ERR;
486 }
487 fclose(stream);
488 g_string_set_size(header, count);
489
490 meta = g_hash_table_new(NULL, NULL);
491 g_hash_table_insert(meta, GINT_TO_POINTER(SR_INPUT_META_FILENAME),
492 (char *)filename);
493 g_hash_table_insert(meta, GINT_TO_POINTER(SR_INPUT_META_FILESIZE),
4619fab4 494 GSIZE_TO_POINTER(MIN(filesize, G_MAXSSIZE)));
5e364d4f
DE
495 g_hash_table_insert(meta, GINT_TO_POINTER(SR_INPUT_META_HEADER),
496 header);
17bfaca6
BV
497 midx = 0;
498 avail_metadata[midx++] = SR_INPUT_META_FILENAME;
499 avail_metadata[midx++] = SR_INPUT_META_FILESIZE;
500 avail_metadata[midx++] = SR_INPUT_META_HEADER;
5e364d4f 501 avail_metadata[midx] = 0;
17bfaca6
BV
502 /* TODO: MIME type */
503
54ee427d
GS
504 best_imod = NULL;
505 best_conf = ~0;
5e364d4f 506 for (i = 0; input_module_list[i]; i++) {
17bfaca6
BV
507 imod = input_module_list[i];
508 if (!imod->metadata[0]) {
509 /* Module has no metadata for matching so will take
510 * any input. No point in letting it try to match. */
511 continue;
512 }
513 if (!check_required_metadata(imod->metadata, avail_metadata))
514 /* Cannot satisfy this module's requirements. */
515 continue;
516
5e364d4f
DE
517 sr_dbg("Trying module %s.", imod->id);
518
54ee427d 519 ret = imod->format_match(meta, &conf);
5e364d4f 520 if (ret == SR_ERR) {
17bfaca6
BV
521 /* Module didn't recognize this buffer. */
522 continue;
4f979115 523 } else if (ret != SR_OK) {
5e364d4f 524 /* Module recognized this buffer, but cannot handle it. */
54ee427d 525 continue;
4f979115 526 }
17bfaca6 527 /* Found a matching module. */
54ee427d
GS
528 sr_dbg("Module %s matched, confidence %u.", imod->id, conf);
529 if (conf >= best_conf)
530 continue;
531 best_imod = imod;
532 best_conf = conf;
17bfaca6 533 }
5e364d4f
DE
534 g_hash_table_destroy(meta);
535 g_string_free(header, TRUE);
17bfaca6 536
54ee427d
GS
537 if (best_imod) {
538 *in = sr_input_new(best_imod, NULL);
539 return SR_OK;
540 }
541
542 return SR_ERR;
17bfaca6
BV
543}
544
c83bdde9
GS
545/**
546 * Return the input instance's module "class". This can be used to find out
547 * which input module handles a specific input file. This is especially
548 * useful when an application did not create the input stream by specifying
549 * an input module, but instead some shortcut or convenience wrapper did.
550 *
551 * @since 0.6.0
552 */
553SR_API const struct sr_input_module *sr_input_module_get(const struct sr_input *in)
554{
555 if (!in)
556 return NULL;
557
558 return in->module;
559}
560
0f3dbc95
BV
561/**
562 * Return the input instance's (virtual) device instance. This can be
563 * used to find out the number of channels and other information.
564 *
d0181813
BV
565 * If the device instance has not yet been fully populated by the input
566 * module, NULL is returned. This indicates the module needs more data
567 * to identify the number of channels and so on.
568 *
0f3dbc95
BV
569 * @since 0.4.0
570 */
17bfaca6
BV
571SR_API struct sr_dev_inst *sr_input_dev_inst_get(const struct sr_input *in)
572{
d0181813
BV
573 if (in->sdi_ready)
574 return in->sdi;
575 else
576 return NULL;
17bfaca6
BV
577}
578
579/**
580 * Send data to the specified input instance.
581 *
582 * When an input module instance is created with sr_input_new(), this
583 * function is used to feed data to the instance.
584 *
d0181813
BV
585 * As enough data gets fed into this function to completely populate
586 * the device instance associated with this input instance, this is
587 * guaranteed to return the moment it's ready. This gives the caller
588 * the chance to examine the device instance, attach session callbacks
60107497 589 * and so on.
d0181813 590 *
17bfaca6
BV
591 * @since 0.4.0
592 */
593SR_API int sr_input_send(const struct sr_input *in, GString *buf)
594{
0da8e0bd
GS
595 size_t len;
596
597 len = buf ? buf->len : 0;
598 sr_spew("Sending %zu bytes to %s module.", len, in->module->id);
d0181813 599 return in->module->receive((struct sr_input *)in, buf);
17bfaca6
BV
600}
601
7066fd46
BV
602/**
603 * Signal the input module no more data will come.
604 *
605 * This will cause the module to process any data it may have buffered.
606 * The SR_DF_END packet will also typically be sent at this time.
607 *
608 * @since 0.4.0
609 */
610SR_API int sr_input_end(const struct sr_input *in)
611{
612 sr_spew("Calling end() on %s module.", in->module->id);
613 return in->module->end((struct sr_input *)in);
614}
615
b6b4f03e
SA
616/**
617 * Reset the input module's input handling structures.
618 *
619 * Causes the input module to reset its internal state so that we can re-send
620 * the input data from the beginning without having to re-create the entire
621 * input module.
622 *
623 * @since 0.5.0
624 */
a38c2bfb 625SR_API int sr_input_reset(const struct sr_input *in_ro)
b6b4f03e 626{
a38c2bfb
GS
627 struct sr_input *in;
628 int rc;
629
630 in = (struct sr_input *)in_ro; /* "un-const" */
631 if (!in || !in->module)
632 return SR_ERR_ARG;
633
634 /*
635 * Run the optional input module's .reset() method. This shall
636 * take care of the context (kept in the 'inc' variable).
637 */
638 if (in->module->reset) {
639 sr_spew("Resetting %s module.", in->module->id);
640 rc = in->module->reset(in);
641 } else {
b6b4f03e
SA
642 sr_spew("Tried to reset %s module but no reset handler found.",
643 in->module->id);
a38c2bfb 644 rc = SR_OK;
b6b4f03e
SA
645 }
646
a38c2bfb
GS
647 /*
648 * Handle input module status (kept in the 'in' variable) here
649 * in common logic. This agrees with how input module's receive()
650 * and end() routines "amend but never seed" the 'in' information.
651 *
652 * Void potentially accumulated receive() buffer content, and
653 * clear the sdi_ready flag. This makes sure that subsequent
654 * processing will scan the header again before sample data gets
655 * interpreted, and stale content from previous calls won't affect
656 * the result.
657 *
658 * This common logic does not harm when the input module implements
659 * .reset() and contains identical assignments. In the absence of
660 * an individual .reset() method, simple input modules can completely
661 * rely on common code and keep working across resets.
662 */
663 if (in->buf)
664 g_string_truncate(in->buf, 0);
665 in->sdi_ready = FALSE;
666
667 return rc;
b6b4f03e
SA
668}
669
17bfaca6
BV
670/**
671 * Free the specified input instance and all associated resources.
672 *
673 * @since 0.4.0
674 */
d5cc282f 675SR_API void sr_input_free(const struct sr_input *in)
17bfaca6 676{
17bfaca6 677 if (!in)
d5cc282f 678 return;
17bfaca6 679
a38c2bfb
GS
680 /*
681 * Run the input module's optional .cleanup() routine. This
682 * takes care of the context (kept in the 'inc' variable).
683 */
17bfaca6 684 if (in->module->cleanup)
d5cc282f 685 in->module->cleanup((struct sr_input *)in);
a38c2bfb
GS
686
687 /*
688 * Common code releases the input module's state (kept in the
689 * 'in' variable). Release the device instance, the receive()
690 * buffer, the shallow 'in->priv' block which is 'inc' (after
691 * .cleanup() released potentially nested resources under 'inc').
692 */
4bf93988 693 sr_dev_inst_free(in->sdi);
d0181813
BV
694 if (in->buf->len > 64) {
695 /* That seems more than just some sub-unitsize leftover... */
6433156c
DE
696 sr_warn("Found %" G_GSIZE_FORMAT
697 " unprocessed bytes at free time.", in->buf->len);
d0181813 698 }
d5cc282f 699 g_string_free(in->buf, TRUE);
89da5b3b 700 g_free(in->priv);
17bfaca6 701 g_free((gpointer)in);
17bfaca6
BV
702}
703
7b870c38 704/** @} */