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