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