]>
Commit | Line | Data |
---|---|---|
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 | |
393fb9cb UH |
32 | /** |
33 | * @file | |
34 | * | |
0f3dbc95 | 35 | * Input module handling. |
393fb9cb UH |
36 | */ |
37 | ||
7b870c38 | 38 | /** |
d4c93774 | 39 | * @defgroup grp_input Input modules |
7b870c38 | 40 | * |
d4c93774 | 41 | * Input file/data module handling. |
7b870c38 | 42 | * |
83687343 | 43 | * libsigrok can process acquisition data in several different ways. |
0f3dbc95 BV |
44 | * Aside from acquiring data from a hardware device, it can also take it |
45 | * from a file in various formats (binary, CSV, VCD, and so on). | |
83687343 | 46 | * |
0f3dbc95 BV |
47 | * Like all libsigrok data handling, processing is done in a streaming |
48 | * manner: input should be supplied a chunk at a time. This way anything | |
49 | * that processes data can do so in real time, without the user having | |
50 | * to wait for the whole thing to be finished. | |
83687343 UH |
51 | * |
52 | * Every input module is "pluggable", meaning it's handled as being separate | |
53 | * from the main libsigrok, but linked in to it statically. To keep things | |
54 | * modular and separate like this, functions within an input module should be | |
d4c93774 | 55 | * declared static, with only the respective 'struct sr_input_module' being |
83687343 UH |
56 | * exported for use into the wider libsigrok namespace. |
57 | * | |
7b870c38 UH |
58 | * @{ |
59 | */ | |
60 | ||
b4bd7088 | 61 | /** @cond PRIVATE */ |
d4c93774 BV |
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; | |
6d2897e3 | 65 | extern SR_PRIV struct sr_input_module input_trace32_ad; |
d4c93774 BV |
66 | extern SR_PRIV struct sr_input_module input_vcd; |
67 | extern SR_PRIV struct sr_input_module input_wav; | |
e6b15cb5 | 68 | extern SR_PRIV struct sr_input_module input_raw_analog; |
e1b115bd | 69 | extern SR_PRIV struct sr_input_module input_logicport; |
2003be8c | 70 | extern SR_PRIV struct sr_input_module input_null; |
b4bd7088 | 71 | /* @endcond */ |
34e4813f | 72 | |
17bfaca6 | 73 | static const struct sr_input_module *input_module_list[] = { |
b84cba4d | 74 | &input_binary, |
02e24c0c | 75 | &input_chronovu_la8, |
41d214f6 | 76 | &input_csv, |
6d2897e3 | 77 | &input_trace32_ad, |
b84cba4d BV |
78 | &input_vcd, |
79 | &input_wav, | |
e6b15cb5 | 80 | &input_raw_analog, |
e1b115bd | 81 | &input_logicport, |
2003be8c | 82 | &input_null, |
34e4813f BV |
83 | NULL, |
84 | }; | |
85 | ||
0f3dbc95 BV |
86 | /** |
87 | * Returns a NULL-terminated list of all available input modules. | |
88 | * | |
89 | * @since 0.4.0 | |
90 | */ | |
17bfaca6 | 91 | SR_API const struct sr_input_module **sr_input_list(void) |
34e4813f | 92 | { |
34e4813f BV |
93 | return input_module_list; |
94 | } | |
7b870c38 | 95 | |
17bfaca6 BV |
96 | /** |
97 | * Returns the specified input module's ID. | |
98 | * | |
99 | * @since 0.4.0 | |
100 | */ | |
4fb0a5f8 | 101 | SR_API const char *sr_input_id_get(const struct sr_input_module *imod) |
17bfaca6 | 102 | { |
4fb0a5f8 | 103 | if (!imod) { |
17bfaca6 BV |
104 | sr_err("Invalid input module NULL!"); |
105 | return NULL; | |
106 | } | |
107 | ||
4fb0a5f8 | 108 | return imod->id; |
17bfaca6 BV |
109 | } |
110 | ||
111 | /** | |
112 | * Returns the specified input module's name. | |
113 | * | |
114 | * @since 0.4.0 | |
115 | */ | |
4fb0a5f8 | 116 | SR_API const char *sr_input_name_get(const struct sr_input_module *imod) |
17bfaca6 | 117 | { |
4fb0a5f8 | 118 | if (!imod) { |
17bfaca6 BV |
119 | sr_err("Invalid input module NULL!"); |
120 | return NULL; | |
121 | } | |
122 | ||
4fb0a5f8 | 123 | return imod->name; |
17bfaca6 BV |
124 | } |
125 | ||
126 | /** | |
127 | * Returns the specified input module's description. | |
128 | * | |
129 | * @since 0.4.0 | |
130 | */ | |
4fb0a5f8 | 131 | SR_API const char *sr_input_description_get(const struct sr_input_module *imod) |
17bfaca6 | 132 | { |
4fb0a5f8 | 133 | if (!imod) { |
17bfaca6 BV |
134 | sr_err("Invalid input module NULL!"); |
135 | return NULL; | |
136 | } | |
137 | ||
4fb0a5f8 | 138 | return imod->desc; |
17bfaca6 BV |
139 | } |
140 | ||
c7bc82ff JH |
141 | /** |
142 | * Returns the specified input module's file extensions typical for the file | |
143 | * format, as a NULL terminated array, or returns a NULL pointer if there is | |
144 | * no preferred extension. | |
145 | * @note these are a suggestions only. | |
146 | * | |
147 | * @since 0.4.0 | |
148 | */ | |
149 | SR_API const char *const *sr_input_extensions_get( | |
4fb0a5f8 | 150 | const struct sr_input_module *imod) |
c7bc82ff | 151 | { |
4fb0a5f8 | 152 | if (!imod) { |
c7bc82ff JH |
153 | sr_err("Invalid input module NULL!"); |
154 | return NULL; | |
155 | } | |
156 | ||
4fb0a5f8 | 157 | return imod->exts; |
c7bc82ff JH |
158 | } |
159 | ||
17bfaca6 BV |
160 | /** |
161 | * Return the input module with the specified ID, or NULL if no module | |
162 | * with that id is found. | |
163 | * | |
164 | * @since 0.4.0 | |
165 | */ | |
166 | SR_API const struct sr_input_module *sr_input_find(char *id) | |
167 | { | |
168 | int i; | |
169 | ||
170 | for (i = 0; input_module_list[i]; i++) { | |
171 | if (!strcmp(input_module_list[i]->id, id)) | |
172 | return input_module_list[i]; | |
173 | } | |
174 | ||
175 | return NULL; | |
176 | } | |
177 | ||
178 | /** | |
179 | * Returns a NULL-terminated array of struct sr_option, or NULL if the | |
180 | * module takes no options. | |
181 | * | |
182 | * Each call to this function must be followed by a call to | |
183 | * sr_input_options_free(). | |
184 | * | |
185 | * @since 0.4.0 | |
186 | */ | |
bd0bfaaf | 187 | SR_API const struct sr_option **sr_input_options_get(const struct sr_input_module *imod) |
17bfaca6 | 188 | { |
bd0bfaaf BV |
189 | const struct sr_option *mod_opts, **opts; |
190 | int size, i; | |
17bfaca6 | 191 | |
bd0bfaaf | 192 | if (!imod || !imod->options) |
17bfaca6 BV |
193 | return NULL; |
194 | ||
bd0bfaaf BV |
195 | mod_opts = imod->options(); |
196 | ||
fe4fe25b | 197 | for (size = 0; mod_opts[size].id; size++) |
bd0bfaaf | 198 | ; |
fe4fe25b | 199 | opts = g_malloc((size + 1) * sizeof(struct sr_option *)); |
bd0bfaaf BV |
200 | |
201 | for (i = 0; i < size; i++) | |
202 | opts[i] = &mod_opts[i]; | |
203 | opts[i] = NULL; | |
204 | ||
205 | return opts; | |
17bfaca6 BV |
206 | } |
207 | ||
208 | /** | |
209 | * After a call to sr_input_options_get(), this function cleans up all | |
bd0bfaaf | 210 | * resources returned by that call. |
17bfaca6 BV |
211 | * |
212 | * @since 0.4.0 | |
213 | */ | |
bd0bfaaf | 214 | SR_API void sr_input_options_free(const struct sr_option **options) |
17bfaca6 | 215 | { |
fe4fe25b | 216 | int i; |
17bfaca6 | 217 | |
bd0bfaaf | 218 | if (!options) |
17bfaca6 BV |
219 | return; |
220 | ||
fe4fe25b BV |
221 | for (i = 0; options[i]; i++) { |
222 | if (options[i]->def) { | |
223 | g_variant_unref(options[i]->def); | |
224 | ((struct sr_option *)options[i])->def = NULL; | |
17bfaca6 BV |
225 | } |
226 | ||
fe4fe25b BV |
227 | if (options[i]->values) { |
228 | g_slist_free_full(options[i]->values, (GDestroyNotify)g_variant_unref); | |
229 | ((struct sr_option *)options[i])->values = NULL; | |
17bfaca6 BV |
230 | } |
231 | } | |
bd0bfaaf | 232 | g_free(options); |
17bfaca6 BV |
233 | } |
234 | ||
235 | /** | |
236 | * Create a new input instance using the specified input module. | |
237 | * | |
238 | * This function is used when a client wants to use a specific input | |
239 | * module to parse a stream. No effort is made to identify the format. | |
240 | * | |
7a54232b | 241 | * @param imod The input module to use. Must not be NULL. |
0f3dbc95 | 242 | * @param options GHashTable consisting of keys corresponding with |
dff0a894 | 243 | * the module options @c id field. The values should be GVariant |
0f3dbc95 | 244 | * pointers with sunk references, of the same GVariantType as the option's |
17bfaca6 BV |
245 | * default value. |
246 | * | |
247 | * @since 0.4.0 | |
248 | */ | |
249 | SR_API struct sr_input *sr_input_new(const struct sr_input_module *imod, | |
250 | GHashTable *options) | |
251 | { | |
252 | struct sr_input *in; | |
2c240774 | 253 | const struct sr_option *mod_opts; |
17bfaca6 BV |
254 | const GVariantType *gvt; |
255 | GHashTable *new_opts; | |
256 | GHashTableIter iter; | |
257 | gpointer key, value; | |
258 | int i; | |
259 | ||
260 | in = g_malloc0(sizeof(struct sr_input)); | |
261 | in->module = imod; | |
262 | ||
7db06394 BV |
263 | new_opts = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, |
264 | (GDestroyNotify)g_variant_unref); | |
265 | if (imod->options) { | |
17bfaca6 BV |
266 | mod_opts = imod->options(); |
267 | for (i = 0; mod_opts[i].id; i++) { | |
7db06394 BV |
268 | if (options && g_hash_table_lookup_extended(options, |
269 | mod_opts[i].id, &key, &value)) { | |
17bfaca6 BV |
270 | /* Option not given: insert the default value. */ |
271 | gvt = g_variant_get_type(mod_opts[i].def); | |
272 | if (!g_variant_is_of_type(value, gvt)) { | |
6433156c DE |
273 | sr_err("Invalid type for '%s' option.", |
274 | (char *)key); | |
17bfaca6 BV |
275 | g_free(in); |
276 | return NULL; | |
277 | } | |
278 | g_hash_table_insert(new_opts, g_strdup(mod_opts[i].id), | |
279 | g_variant_ref(value)); | |
280 | } else { | |
281 | /* Pass option along. */ | |
282 | g_hash_table_insert(new_opts, g_strdup(mod_opts[i].id), | |
283 | g_variant_ref(mod_opts[i].def)); | |
284 | } | |
285 | } | |
286 | ||
287 | /* Make sure no invalid options were given. */ | |
7db06394 BV |
288 | if (options) { |
289 | g_hash_table_iter_init(&iter, options); | |
290 | while (g_hash_table_iter_next(&iter, &key, &value)) { | |
291 | if (!g_hash_table_lookup(new_opts, key)) { | |
6433156c DE |
292 | sr_err("Input module '%s' has no option '%s'", |
293 | imod->id, (char *)key); | |
7db06394 BV |
294 | g_hash_table_destroy(new_opts); |
295 | g_free(in); | |
296 | return NULL; | |
297 | } | |
17bfaca6 BV |
298 | } |
299 | } | |
7db06394 | 300 | } |
17bfaca6 BV |
301 | |
302 | if (in->module->init && in->module->init(in, new_opts) != SR_OK) { | |
17bfaca6 BV |
303 | g_free(in); |
304 | in = NULL; | |
64098211 BV |
305 | } else { |
306 | in->buf = g_string_sized_new(128); | |
17bfaca6 | 307 | } |
64098211 | 308 | |
17bfaca6 BV |
309 | if (new_opts) |
310 | g_hash_table_destroy(new_opts); | |
311 | ||
312 | return in; | |
313 | } | |
314 | ||
64098211 | 315 | /* Returns TRUE if all required meta items are available. */ |
17bfaca6 BV |
316 | static gboolean check_required_metadata(const uint8_t *metadata, uint8_t *avail) |
317 | { | |
318 | int m, a; | |
319 | uint8_t reqd; | |
320 | ||
321 | for (m = 0; metadata[m]; m++) { | |
7db06394 | 322 | if (!(metadata[m] & SR_INPUT_META_REQUIRED)) |
17bfaca6 BV |
323 | continue; |
324 | reqd = metadata[m] & ~SR_INPUT_META_REQUIRED; | |
325 | for (a = 0; avail[a]; a++) { | |
326 | if (avail[a] == reqd) | |
327 | break; | |
328 | } | |
329 | if (!avail[a]) | |
330 | /* Found a required meta item that isn't available. */ | |
331 | return FALSE; | |
332 | } | |
333 | ||
334 | return TRUE; | |
335 | } | |
336 | ||
337 | /** | |
338 | * Try to find an input module that can parse the given buffer. | |
339 | * | |
340 | * The buffer must contain enough of the beginning of the file for | |
341 | * the input modules to find a match. This is format-dependent, but | |
342 | * 128 bytes is normally enough. | |
343 | * | |
4f979115 BV |
344 | * If an input module is found, an instance is created into *in. |
345 | * Otherwise, *in contains NULL. | |
17bfaca6 | 346 | * |
33e4303b BV |
347 | * If an instance is created, it has the given buffer used for scanning |
348 | * already submitted to it, to be processed before more data is sent. | |
349 | * This allows a frontend to submit an initial chunk of a non-seekable | |
350 | * stream, such as stdin, without having to keep it around and submit | |
351 | * it again later. | |
352 | * | |
17bfaca6 | 353 | */ |
4f979115 | 354 | SR_API int sr_input_scan_buffer(GString *buf, const struct sr_input **in) |
17bfaca6 | 355 | { |
17bfaca6 BV |
356 | const struct sr_input_module *imod; |
357 | GHashTable *meta; | |
358 | unsigned int m, i; | |
4f979115 | 359 | int ret; |
17bfaca6 BV |
360 | uint8_t mitem, avail_metadata[8]; |
361 | ||
362 | /* No more metadata to be had from a buffer. */ | |
363 | avail_metadata[0] = SR_INPUT_META_HEADER; | |
364 | avail_metadata[1] = 0; | |
365 | ||
4f979115 BV |
366 | *in = NULL; |
367 | ret = SR_ERR; | |
17bfaca6 BV |
368 | for (i = 0; input_module_list[i]; i++) { |
369 | imod = input_module_list[i]; | |
370 | if (!imod->metadata[0]) { | |
371 | /* Module has no metadata for matching so will take | |
372 | * any input. No point in letting it try to match. */ | |
373 | continue; | |
374 | } | |
375 | if (!check_required_metadata(imod->metadata, avail_metadata)) | |
376 | /* Cannot satisfy this module's requirements. */ | |
377 | continue; | |
378 | ||
379 | meta = g_hash_table_new(NULL, NULL); | |
380 | for (m = 0; m < sizeof(imod->metadata); m++) { | |
381 | mitem = imod->metadata[m] & ~SR_INPUT_META_REQUIRED; | |
382 | if (mitem == SR_INPUT_META_HEADER) | |
383 | g_hash_table_insert(meta, GINT_TO_POINTER(mitem), buf); | |
384 | } | |
385 | if (g_hash_table_size(meta) == 0) { | |
386 | /* No metadata for this module, so nothing to match. */ | |
387 | g_hash_table_destroy(meta); | |
388 | continue; | |
389 | } | |
4f979115 BV |
390 | sr_spew("Trying module %s.", imod->id); |
391 | ret = imod->format_match(meta); | |
392 | g_hash_table_destroy(meta); | |
393 | if (ret == SR_ERR_DATA) { | |
394 | /* Module recognized this buffer, but cannot handle it. */ | |
395 | break; | |
396 | } else if (ret == SR_ERR) { | |
17bfaca6 | 397 | /* Module didn't recognize this buffer. */ |
17bfaca6 | 398 | continue; |
4f979115 | 399 | } else if (ret != SR_OK) { |
60107497 | 400 | /* Can be SR_ERR_NA. */ |
4f979115 | 401 | return ret; |
17bfaca6 | 402 | } |
17bfaca6 BV |
403 | |
404 | /* Found a matching module. */ | |
4f979115 BV |
405 | sr_spew("Module %s matched.", imod->id); |
406 | *in = sr_input_new(imod, NULL); | |
407 | g_string_insert_len((*in)->buf, 0, buf->str, buf->len); | |
17bfaca6 BV |
408 | break; |
409 | } | |
410 | ||
4f979115 | 411 | return ret; |
17bfaca6 BV |
412 | } |
413 | ||
414 | /** | |
415 | * Try to find an input module that can parse the given file. | |
416 | * | |
4f979115 BV |
417 | * If an input module is found, an instance is created into *in. |
418 | * Otherwise, *in contains NULL. | |
17bfaca6 BV |
419 | * |
420 | */ | |
4f979115 | 421 | SR_API int sr_input_scan_file(const char *filename, const struct sr_input **in) |
17bfaca6 | 422 | { |
4619fab4 | 423 | int64_t filesize; |
5e364d4f | 424 | FILE *stream; |
17bfaca6 BV |
425 | const struct sr_input_module *imod; |
426 | GHashTable *meta; | |
5e364d4f | 427 | GString *header; |
5e364d4f DE |
428 | size_t count; |
429 | unsigned int midx, i; | |
430 | int ret; | |
431 | uint8_t avail_metadata[8]; | |
432 | ||
433 | *in = NULL; | |
17bfaca6 BV |
434 | |
435 | if (!filename || !filename[0]) { | |
436 | sr_err("Invalid filename."); | |
4f979115 | 437 | return SR_ERR_ARG; |
17bfaca6 | 438 | } |
5e364d4f DE |
439 | stream = g_fopen(filename, "rb"); |
440 | if (!stream) { | |
441 | sr_err("Failed to open %s: %s", filename, g_strerror(errno)); | |
442 | return SR_ERR; | |
443 | } | |
4619fab4 DE |
444 | filesize = sr_file_get_size(stream); |
445 | if (filesize < 0) { | |
446 | sr_err("Failed to get size of %s: %s", | |
447 | filename, g_strerror(errno)); | |
448 | fclose(stream); | |
449 | return SR_ERR; | |
450 | } | |
5e364d4f DE |
451 | /* This actually allocates 256 bytes to allow for NUL termination. */ |
452 | header = g_string_sized_new(255); | |
453 | count = fread(header->str, 1, header->allocated_len - 1, stream); | |
454 | ||
455 | if (count != header->allocated_len - 1 && ferror(stream)) { | |
456 | sr_err("Failed to read %s: %s", filename, g_strerror(errno)); | |
457 | fclose(stream); | |
458 | g_string_free(header, TRUE); | |
459 | return SR_ERR; | |
460 | } | |
461 | fclose(stream); | |
462 | g_string_set_size(header, count); | |
463 | ||
464 | meta = g_hash_table_new(NULL, NULL); | |
465 | g_hash_table_insert(meta, GINT_TO_POINTER(SR_INPUT_META_FILENAME), | |
466 | (char *)filename); | |
467 | g_hash_table_insert(meta, GINT_TO_POINTER(SR_INPUT_META_FILESIZE), | |
4619fab4 | 468 | GSIZE_TO_POINTER(MIN(filesize, G_MAXSSIZE))); |
5e364d4f DE |
469 | g_hash_table_insert(meta, GINT_TO_POINTER(SR_INPUT_META_HEADER), |
470 | header); | |
17bfaca6 BV |
471 | midx = 0; |
472 | avail_metadata[midx++] = SR_INPUT_META_FILENAME; | |
473 | avail_metadata[midx++] = SR_INPUT_META_FILESIZE; | |
474 | avail_metadata[midx++] = SR_INPUT_META_HEADER; | |
5e364d4f | 475 | avail_metadata[midx] = 0; |
17bfaca6 BV |
476 | /* TODO: MIME type */ |
477 | ||
4f979115 | 478 | ret = SR_ERR; |
115fbe94 | 479 | |
5e364d4f | 480 | for (i = 0; input_module_list[i]; i++) { |
17bfaca6 BV |
481 | imod = input_module_list[i]; |
482 | if (!imod->metadata[0]) { | |
483 | /* Module has no metadata for matching so will take | |
484 | * any input. No point in letting it try to match. */ | |
485 | continue; | |
486 | } | |
487 | if (!check_required_metadata(imod->metadata, avail_metadata)) | |
488 | /* Cannot satisfy this module's requirements. */ | |
489 | continue; | |
490 | ||
5e364d4f DE |
491 | sr_dbg("Trying module %s.", imod->id); |
492 | ||
4f979115 | 493 | ret = imod->format_match(meta); |
5e364d4f | 494 | if (ret == SR_ERR) { |
17bfaca6 BV |
495 | /* Module didn't recognize this buffer. */ |
496 | continue; | |
4f979115 | 497 | } else if (ret != SR_OK) { |
5e364d4f DE |
498 | /* Module recognized this buffer, but cannot handle it. */ |
499 | break; | |
4f979115 | 500 | } |
17bfaca6 | 501 | /* Found a matching module. */ |
5e364d4f DE |
502 | sr_dbg("Module %s matched.", imod->id); |
503 | ||
4f979115 | 504 | *in = sr_input_new(imod, NULL); |
17bfaca6 BV |
505 | break; |
506 | } | |
5e364d4f DE |
507 | g_hash_table_destroy(meta); |
508 | g_string_free(header, TRUE); | |
17bfaca6 | 509 | |
4f979115 | 510 | return ret; |
17bfaca6 BV |
511 | } |
512 | ||
0f3dbc95 BV |
513 | /** |
514 | * Return the input instance's (virtual) device instance. This can be | |
515 | * used to find out the number of channels and other information. | |
516 | * | |
d0181813 BV |
517 | * If the device instance has not yet been fully populated by the input |
518 | * module, NULL is returned. This indicates the module needs more data | |
519 | * to identify the number of channels and so on. | |
520 | * | |
0f3dbc95 BV |
521 | * @since 0.4.0 |
522 | */ | |
17bfaca6 BV |
523 | SR_API struct sr_dev_inst *sr_input_dev_inst_get(const struct sr_input *in) |
524 | { | |
d0181813 BV |
525 | if (in->sdi_ready) |
526 | return in->sdi; | |
527 | else | |
528 | return NULL; | |
17bfaca6 BV |
529 | } |
530 | ||
531 | /** | |
532 | * Send data to the specified input instance. | |
533 | * | |
534 | * When an input module instance is created with sr_input_new(), this | |
535 | * function is used to feed data to the instance. | |
536 | * | |
d0181813 BV |
537 | * As enough data gets fed into this function to completely populate |
538 | * the device instance associated with this input instance, this is | |
539 | * guaranteed to return the moment it's ready. This gives the caller | |
540 | * the chance to examine the device instance, attach session callbacks | |
60107497 | 541 | * and so on. |
d0181813 | 542 | * |
17bfaca6 BV |
543 | * @since 0.4.0 |
544 | */ | |
545 | SR_API int sr_input_send(const struct sr_input *in, GString *buf) | |
546 | { | |
6433156c DE |
547 | sr_spew("Sending %" G_GSIZE_FORMAT " bytes to %s module.", |
548 | buf->len, in->module->id); | |
d0181813 | 549 | return in->module->receive((struct sr_input *)in, buf); |
17bfaca6 BV |
550 | } |
551 | ||
7066fd46 BV |
552 | /** |
553 | * Signal the input module no more data will come. | |
554 | * | |
555 | * This will cause the module to process any data it may have buffered. | |
556 | * The SR_DF_END packet will also typically be sent at this time. | |
557 | * | |
558 | * @since 0.4.0 | |
559 | */ | |
560 | SR_API int sr_input_end(const struct sr_input *in) | |
561 | { | |
562 | sr_spew("Calling end() on %s module.", in->module->id); | |
563 | return in->module->end((struct sr_input *)in); | |
564 | } | |
565 | ||
b6b4f03e SA |
566 | /** |
567 | * Reset the input module's input handling structures. | |
568 | * | |
569 | * Causes the input module to reset its internal state so that we can re-send | |
570 | * the input data from the beginning without having to re-create the entire | |
571 | * input module. | |
572 | * | |
573 | * @since 0.5.0 | |
574 | */ | |
575 | SR_API int sr_input_reset(const struct sr_input *in) | |
576 | { | |
577 | if (!in->module->reset) { | |
578 | sr_spew("Tried to reset %s module but no reset handler found.", | |
579 | in->module->id); | |
580 | return SR_OK; | |
581 | } | |
582 | ||
583 | sr_spew("Resetting %s module.", in->module->id); | |
584 | return in->module->reset((struct sr_input *)in); | |
585 | } | |
586 | ||
17bfaca6 BV |
587 | /** |
588 | * Free the specified input instance and all associated resources. | |
589 | * | |
590 | * @since 0.4.0 | |
591 | */ | |
d5cc282f | 592 | SR_API void sr_input_free(const struct sr_input *in) |
17bfaca6 | 593 | { |
17bfaca6 | 594 | if (!in) |
d5cc282f | 595 | return; |
17bfaca6 | 596 | |
17bfaca6 | 597 | if (in->module->cleanup) |
d5cc282f | 598 | in->module->cleanup((struct sr_input *)in); |
4bf93988 | 599 | sr_dev_inst_free(in->sdi); |
d0181813 BV |
600 | if (in->buf->len > 64) { |
601 | /* That seems more than just some sub-unitsize leftover... */ | |
6433156c DE |
602 | sr_warn("Found %" G_GSIZE_FORMAT |
603 | " unprocessed bytes at free time.", in->buf->len); | |
d0181813 | 604 | } |
d5cc282f | 605 | g_string_free(in->buf, TRUE); |
89da5b3b | 606 | g_free(in->priv); |
17bfaca6 | 607 | g_free((gpointer)in); |
17bfaca6 BV |
608 | } |
609 | ||
7b870c38 | 610 | /** @} */ |