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