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