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