]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * This file is part of the sigrok-cli project. | |
3 | * | |
4 | * Copyright (C) 2013 Bert Vermeulen <bert@biot.com> | |
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 | ||
20 | #include <config.h> | |
21 | #include <glib.h> | |
22 | #include <glib/gstdio.h> | |
23 | #include <string.h> | |
24 | #include <stdlib.h> | |
25 | #include "sigrok-cli.h" | |
26 | ||
27 | static uint64_t limit_samples = 0; | |
28 | static uint64_t limit_frames = 0; | |
29 | ||
30 | #ifdef HAVE_SRD | |
31 | extern struct srd_session *srd_sess; | |
32 | #endif | |
33 | ||
34 | static int set_limit_time(const struct sr_dev_inst *sdi) | |
35 | { | |
36 | GVariant *gvar; | |
37 | uint64_t time_msec; | |
38 | uint64_t samplerate; | |
39 | struct sr_dev_driver *driver; | |
40 | ||
41 | driver = sr_dev_inst_driver_get(sdi); | |
42 | ||
43 | if (!(time_msec = sr_parse_timestring(opt_time))) { | |
44 | g_critical("Invalid time '%s'", opt_time); | |
45 | return SR_ERR; | |
46 | } | |
47 | ||
48 | if (sr_dev_config_capabilities_list(sdi, NULL, SR_CONF_LIMIT_MSEC) | |
49 | & SR_CONF_SET) { | |
50 | gvar = g_variant_new_uint64(time_msec); | |
51 | if (sr_config_set(sdi, NULL, SR_CONF_LIMIT_MSEC, gvar) != SR_OK) { | |
52 | g_critical("Failed to configure time limit."); | |
53 | return SR_ERR; | |
54 | } | |
55 | } else if (sr_dev_config_capabilities_list(sdi, NULL, SR_CONF_SAMPLERATE) | |
56 | & (SR_CONF_GET | SR_CONF_SET)) { | |
57 | /* Convert to samples based on the samplerate. */ | |
58 | sr_config_get(driver, sdi, NULL, SR_CONF_SAMPLERATE, &gvar); | |
59 | samplerate = g_variant_get_uint64(gvar); | |
60 | g_variant_unref(gvar); | |
61 | limit_samples = (samplerate) * time_msec / (uint64_t)1000; | |
62 | if (limit_samples == 0) { | |
63 | g_critical("Not enough time at this samplerate."); | |
64 | return SR_ERR; | |
65 | } | |
66 | gvar = g_variant_new_uint64(limit_samples); | |
67 | if (sr_config_set(sdi, NULL, SR_CONF_LIMIT_SAMPLES, gvar) != SR_OK) { | |
68 | g_critical("Failed to configure time-based sample limit."); | |
69 | return SR_ERR; | |
70 | } | |
71 | } else { | |
72 | g_critical("This device does not support time limits."); | |
73 | return SR_ERR; | |
74 | } | |
75 | ||
76 | return SR_OK; | |
77 | } | |
78 | ||
79 | const struct sr_output *setup_output_format(const struct sr_dev_inst *sdi, FILE **outfile) | |
80 | { | |
81 | const struct sr_output_module *omod; | |
82 | const struct sr_option **options; | |
83 | const struct sr_output *o; | |
84 | GHashTable *fmtargs, *fmtopts; | |
85 | char *fmtspec; | |
86 | ||
87 | if (!opt_output_format) { | |
88 | if (opt_output_file) { | |
89 | opt_output_format = DEFAULT_OUTPUT_FORMAT_FILE; | |
90 | } else { | |
91 | opt_output_format = DEFAULT_OUTPUT_FORMAT_NOFILE; | |
92 | } | |
93 | } | |
94 | ||
95 | fmtargs = parse_generic_arg(opt_output_format, TRUE); | |
96 | fmtspec = g_hash_table_lookup(fmtargs, "sigrok_key"); | |
97 | if (!fmtspec) | |
98 | g_critical("Invalid output format."); | |
99 | if (!(omod = sr_output_find(fmtspec))) | |
100 | g_critical("Unknown output module '%s'.", fmtspec); | |
101 | g_hash_table_remove(fmtargs, "sigrok_key"); | |
102 | if ((options = sr_output_options_get(omod))) { | |
103 | fmtopts = generic_arg_to_opt(options, fmtargs); | |
104 | sr_output_options_free(options); | |
105 | } else | |
106 | fmtopts = NULL; | |
107 | o = sr_output_new(omod, fmtopts, sdi, opt_output_file); | |
108 | ||
109 | if (opt_output_file) { | |
110 | if (!sr_output_test_flag(omod, SR_OUTPUT_INTERNAL_IO_HANDLING)) | |
111 | *outfile = g_fopen(opt_output_file, "wb"); | |
112 | else | |
113 | *outfile = NULL; | |
114 | } else { | |
115 | *outfile = stdout; | |
116 | } | |
117 | ||
118 | if (fmtopts) | |
119 | g_hash_table_destroy(fmtopts); | |
120 | g_hash_table_destroy(fmtargs); | |
121 | ||
122 | return o; | |
123 | } | |
124 | ||
125 | const struct sr_transform *setup_transform_module(const struct sr_dev_inst *sdi) | |
126 | { | |
127 | const struct sr_transform_module *tmod; | |
128 | const struct sr_option **options; | |
129 | const struct sr_transform *t; | |
130 | GHashTable *fmtargs, *fmtopts; | |
131 | char *fmtspec; | |
132 | ||
133 | fmtargs = parse_generic_arg(opt_transform_module, TRUE); | |
134 | fmtspec = g_hash_table_lookup(fmtargs, "sigrok_key"); | |
135 | if (!fmtspec) | |
136 | g_critical("Invalid transform module."); | |
137 | if (!(tmod = sr_transform_find(fmtspec))) | |
138 | g_critical("Unknown transform module '%s'.", fmtspec); | |
139 | g_hash_table_remove(fmtargs, "sigrok_key"); | |
140 | if ((options = sr_transform_options_get(tmod))) { | |
141 | fmtopts = generic_arg_to_opt(options, fmtargs); | |
142 | sr_transform_options_free(options); | |
143 | } else | |
144 | fmtopts = NULL; | |
145 | t = sr_transform_new(tmod, fmtopts, sdi); | |
146 | if (fmtopts) | |
147 | g_hash_table_destroy(fmtopts); | |
148 | g_hash_table_destroy(fmtargs); | |
149 | ||
150 | return t; | |
151 | } | |
152 | ||
153 | void datafeed_in(const struct sr_dev_inst *sdi, | |
154 | const struct sr_datafeed_packet *packet, void *cb_data) | |
155 | { | |
156 | static const struct sr_output *o = NULL; | |
157 | static const struct sr_output *oa = NULL; | |
158 | static uint64_t rcvd_samples_logic = 0; | |
159 | static uint64_t rcvd_samples_analog = 0; | |
160 | static uint64_t samplerate = 0; | |
161 | static int triggered = 0; | |
162 | static FILE *outfile = NULL; | |
163 | ||
164 | const struct sr_datafeed_meta *meta; | |
165 | const struct sr_datafeed_logic *logic; | |
166 | const struct sr_datafeed_analog *analog; | |
167 | struct sr_session *session; | |
168 | struct sr_config *src; | |
169 | GSList *l; | |
170 | GString *out; | |
171 | GVariant *gvar; | |
172 | uint64_t end_sample; | |
173 | uint64_t input_len; | |
174 | struct sr_dev_driver *driver; | |
175 | ||
176 | /* Avoid warnings when building without decoder support. */ | |
177 | (void)session; | |
178 | (void)input_len; | |
179 | ||
180 | driver = sr_dev_inst_driver_get(sdi); | |
181 | ||
182 | /* Skip all packets before the first header. */ | |
183 | if (packet->type != SR_DF_HEADER && !o) | |
184 | return; | |
185 | ||
186 | session = cb_data; | |
187 | switch (packet->type) { | |
188 | case SR_DF_HEADER: | |
189 | g_debug("cli: Received SR_DF_HEADER."); | |
190 | if (!(o = setup_output_format(sdi, &outfile))) | |
191 | g_critical("Failed to initialize output module."); | |
192 | ||
193 | /* Set up backup analog output module. */ | |
194 | if (outfile) | |
195 | oa = sr_output_new(sr_output_find("analog"), NULL, | |
196 | sdi, NULL); | |
197 | ||
198 | rcvd_samples_logic = rcvd_samples_analog = 0; | |
199 | ||
200 | if (maybe_config_get(driver, sdi, NULL, SR_CONF_SAMPLERATE, | |
201 | &gvar) == SR_OK) { | |
202 | samplerate = g_variant_get_uint64(gvar); | |
203 | g_variant_unref(gvar); | |
204 | } | |
205 | ||
206 | #ifdef HAVE_SRD | |
207 | if (opt_pds) { | |
208 | if (samplerate) { | |
209 | if (srd_session_metadata_set(srd_sess, SRD_CONF_SAMPLERATE, | |
210 | g_variant_new_uint64(samplerate)) != SRD_OK) { | |
211 | g_critical("Failed to configure decode session."); | |
212 | break; | |
213 | } | |
214 | } | |
215 | if (srd_session_start(srd_sess) != SRD_OK) { | |
216 | g_critical("Failed to start decode session."); | |
217 | break; | |
218 | } | |
219 | } | |
220 | #endif | |
221 | break; | |
222 | ||
223 | case SR_DF_META: | |
224 | g_debug("cli: Received SR_DF_META."); | |
225 | meta = packet->payload; | |
226 | for (l = meta->config; l; l = l->next) { | |
227 | src = l->data; | |
228 | switch (src->key) { | |
229 | case SR_CONF_SAMPLERATE: | |
230 | samplerate = g_variant_get_uint64(src->data); | |
231 | g_debug("cli: Got samplerate %"PRIu64" Hz.", samplerate); | |
232 | #ifdef HAVE_SRD | |
233 | if (opt_pds) { | |
234 | if (srd_session_metadata_set(srd_sess, SRD_CONF_SAMPLERATE, | |
235 | g_variant_new_uint64(samplerate)) != SRD_OK) { | |
236 | g_critical("Failed to pass samplerate to decoder."); | |
237 | } | |
238 | } | |
239 | #endif | |
240 | break; | |
241 | case SR_CONF_SAMPLE_INTERVAL: | |
242 | samplerate = g_variant_get_uint64(src->data); | |
243 | g_debug("cli: Got sample interval %"PRIu64" ms.", samplerate); | |
244 | break; | |
245 | default: | |
246 | /* Unknown metadata is not an error. */ | |
247 | break; | |
248 | } | |
249 | } | |
250 | break; | |
251 | ||
252 | case SR_DF_TRIGGER: | |
253 | g_debug("cli: Received SR_DF_TRIGGER."); | |
254 | triggered = 1; | |
255 | break; | |
256 | ||
257 | case SR_DF_LOGIC: | |
258 | logic = packet->payload; | |
259 | g_message("cli: Received SR_DF_LOGIC (%"PRIu64" bytes, unitsize = %d).", | |
260 | logic->length, logic->unitsize); | |
261 | if (logic->length == 0) | |
262 | break; | |
263 | ||
264 | /* Don't store any samples until triggered. */ | |
265 | if (opt_wait_trigger && !triggered) | |
266 | break; | |
267 | ||
268 | if (limit_samples && rcvd_samples_logic >= limit_samples) | |
269 | break; | |
270 | ||
271 | end_sample = rcvd_samples_logic + logic->length / logic->unitsize; | |
272 | /* Cut off last packet according to the sample limit. */ | |
273 | if (limit_samples && end_sample > limit_samples) | |
274 | end_sample = limit_samples; | |
275 | input_len = (end_sample - rcvd_samples_logic) * logic->unitsize; | |
276 | ||
277 | if (opt_pds) { | |
278 | #ifdef HAVE_SRD | |
279 | if (srd_session_send(srd_sess, rcvd_samples_logic, end_sample, | |
280 | logic->data, input_len, logic->unitsize) != SRD_OK) | |
281 | sr_session_stop(session); | |
282 | #endif | |
283 | } | |
284 | ||
285 | rcvd_samples_logic = end_sample; | |
286 | break; | |
287 | ||
288 | case SR_DF_ANALOG: | |
289 | analog = packet->payload; | |
290 | g_message("cli: Received SR_DF_ANALOG (%d samples).", analog->num_samples); | |
291 | if (analog->num_samples == 0) | |
292 | break; | |
293 | ||
294 | if (limit_samples && rcvd_samples_analog >= limit_samples) | |
295 | break; | |
296 | ||
297 | rcvd_samples_analog += analog->num_samples; | |
298 | break; | |
299 | ||
300 | case SR_DF_FRAME_BEGIN: | |
301 | g_debug("cli: Received SR_DF_FRAME_BEGIN."); | |
302 | break; | |
303 | ||
304 | case SR_DF_FRAME_END: | |
305 | g_debug("cli: Received SR_DF_FRAME_END."); | |
306 | break; | |
307 | ||
308 | default: | |
309 | break; | |
310 | } | |
311 | ||
312 | if (o && !opt_pds) { | |
313 | if (sr_output_send(o, packet, &out) == SR_OK) { | |
314 | if (oa && !out) { | |
315 | /* | |
316 | * The user didn't specify an output module, | |
317 | * but needs to see this analog data. | |
318 | */ | |
319 | sr_output_send(oa, packet, &out); | |
320 | } | |
321 | if (outfile && out && out->len > 0) { | |
322 | fwrite(out->str, 1, out->len, outfile); | |
323 | fflush(outfile); | |
324 | } | |
325 | if (out) | |
326 | g_string_free(out, TRUE); | |
327 | } | |
328 | } | |
329 | ||
330 | /* | |
331 | * SR_DF_END needs to be handled after the output module's receive() | |
332 | * is called, so it can properly clean up that module. | |
333 | */ | |
334 | if (packet->type == SR_DF_END) { | |
335 | g_debug("cli: Received SR_DF_END."); | |
336 | ||
337 | if (o) | |
338 | sr_output_free(o); | |
339 | o = NULL; | |
340 | ||
341 | if (oa) | |
342 | sr_output_free(oa); | |
343 | oa = NULL; | |
344 | ||
345 | if (outfile && outfile != stdout) | |
346 | fclose(outfile); | |
347 | ||
348 | if (limit_samples) { | |
349 | if (rcvd_samples_logic > 0 && rcvd_samples_logic < limit_samples) | |
350 | g_warning("Device only sent %" PRIu64 " samples.", | |
351 | rcvd_samples_logic); | |
352 | else if (rcvd_samples_analog > 0 && rcvd_samples_analog < limit_samples) | |
353 | g_warning("Device only sent %" PRIu64 " samples.", | |
354 | rcvd_samples_analog); | |
355 | } | |
356 | } | |
357 | ||
358 | } | |
359 | ||
360 | int opt_to_gvar(char *key, char *value, struct sr_config *src) | |
361 | { | |
362 | const struct sr_key_info *srci, *srmqi; | |
363 | double tmp_double, dlow, dhigh; | |
364 | uint64_t tmp_u64, p, q, low, high, mqflags; | |
365 | uint32_t mq; | |
366 | GVariant *rational[2], *range[2], *gtup[2]; | |
367 | GVariantBuilder *vbl; | |
368 | gboolean tmp_bool; | |
369 | gchar **keyval; | |
370 | int ret, i; | |
371 | ||
372 | if (!(srci = sr_key_info_name_get(SR_KEY_CONFIG, key))) { | |
373 | g_critical("Unknown device option '%s'.", (char *) key); | |
374 | return -1; | |
375 | } | |
376 | src->key = srci->key; | |
377 | ||
378 | if ((!value || strlen(value) == 0) && | |
379 | (srci->datatype != SR_T_BOOL)) { | |
380 | g_critical("Option '%s' needs a value.", (char *)key); | |
381 | return -1; | |
382 | } | |
383 | ||
384 | ret = 0; | |
385 | switch (srci->datatype) { | |
386 | case SR_T_UINT64: | |
387 | ret = sr_parse_sizestring(value, &tmp_u64); | |
388 | if (ret != 0) | |
389 | break; | |
390 | src->data = g_variant_new_uint64(tmp_u64); | |
391 | break; | |
392 | case SR_T_INT32: | |
393 | ret = sr_parse_sizestring(value, &tmp_u64); | |
394 | if (ret != 0) | |
395 | break; | |
396 | src->data = g_variant_new_int32(tmp_u64); | |
397 | break; | |
398 | case SR_T_STRING: | |
399 | src->data = g_variant_new_string(value); | |
400 | break; | |
401 | case SR_T_BOOL: | |
402 | if (!value) | |
403 | tmp_bool = TRUE; | |
404 | else | |
405 | tmp_bool = sr_parse_boolstring(value); | |
406 | src->data = g_variant_new_boolean(tmp_bool); | |
407 | break; | |
408 | case SR_T_FLOAT: | |
409 | tmp_double = strtof(value, NULL); | |
410 | src->data = g_variant_new_double(tmp_double); | |
411 | break; | |
412 | case SR_T_RATIONAL_PERIOD: | |
413 | if ((ret = sr_parse_period(value, &p, &q)) != SR_OK) | |
414 | break; | |
415 | rational[0] = g_variant_new_uint64(p); | |
416 | rational[1] = g_variant_new_uint64(q); | |
417 | src->data = g_variant_new_tuple(rational, 2); | |
418 | break; | |
419 | case SR_T_RATIONAL_VOLT: | |
420 | if ((ret = sr_parse_voltage(value, &p, &q)) != SR_OK) | |
421 | break; | |
422 | rational[0] = g_variant_new_uint64(p); | |
423 | rational[1] = g_variant_new_uint64(q); | |
424 | src->data = g_variant_new_tuple(rational, 2); | |
425 | break; | |
426 | case SR_T_UINT64_RANGE: | |
427 | if (sscanf(value, "%"PRIu64"-%"PRIu64, &low, &high) != 2) { | |
428 | ret = -1; | |
429 | break; | |
430 | } else { | |
431 | range[0] = g_variant_new_uint64(low); | |
432 | range[1] = g_variant_new_uint64(high); | |
433 | src->data = g_variant_new_tuple(range, 2); | |
434 | } | |
435 | break; | |
436 | case SR_T_DOUBLE_RANGE: | |
437 | if (sscanf(value, "%lf-%lf", &dlow, &dhigh) != 2) { | |
438 | ret = -1; | |
439 | break; | |
440 | } else { | |
441 | range[0] = g_variant_new_double(dlow); | |
442 | range[1] = g_variant_new_double(dhigh); | |
443 | src->data = g_variant_new_tuple(range, 2); | |
444 | } | |
445 | break; | |
446 | case SR_T_KEYVALUE: | |
447 | /* Expects the argument to be in the form of key=value. */ | |
448 | keyval = g_strsplit(value, "=", 2); | |
449 | if (!keyval[0] || !keyval[1]) { | |
450 | g_strfreev(keyval); | |
451 | ret = -1; | |
452 | break; | |
453 | } else { | |
454 | vbl = g_variant_builder_new(G_VARIANT_TYPE_DICTIONARY); | |
455 | g_variant_builder_add(vbl, "{ss}", | |
456 | keyval[0], keyval[1]); | |
457 | src->data = g_variant_builder_end(vbl); | |
458 | g_strfreev(keyval); | |
459 | } | |
460 | break; | |
461 | case SR_T_MQ: | |
462 | /* | |
463 | Argument is MQ id e.g. ("voltage") optionally followed by one | |
464 | or more /<mqflag> e.g. "/ac". | |
465 | */ | |
466 | keyval = g_strsplit(value, "/", 0); | |
467 | if (!keyval[0] || !(srmqi = sr_key_info_name_get(SR_KEY_MQ, keyval[0]))) { | |
468 | g_strfreev(keyval); | |
469 | ret = -1; | |
470 | break; | |
471 | } | |
472 | mq = srmqi->key; | |
473 | mqflags = 0; | |
474 | for (i = 1; keyval[i]; i++) { | |
475 | if (!(srmqi = sr_key_info_name_get(SR_KEY_MQFLAGS, keyval[i]))) { | |
476 | ret = -1; | |
477 | break; | |
478 | } | |
479 | mqflags |= srmqi->key; | |
480 | } | |
481 | g_strfreev(keyval); | |
482 | if (ret != -1) { | |
483 | gtup[0] = g_variant_new_uint32(mq); | |
484 | gtup[1] = g_variant_new_uint64(mqflags); | |
485 | src->data = g_variant_new_tuple(gtup, 2); | |
486 | } | |
487 | break; | |
488 | default: | |
489 | g_critical("Unknown data type specified for option '%s' " | |
490 | "(driver implementation bug?).", key); | |
491 | ret = -1; | |
492 | } | |
493 | ||
494 | if (ret < 0) | |
495 | g_critical("Invalid value: '%s' for option '%s'", value, key); | |
496 | ||
497 | return ret; | |
498 | } | |
499 | ||
500 | int set_dev_options(struct sr_dev_inst *sdi, GHashTable *args) | |
501 | { | |
502 | struct sr_config src; | |
503 | struct sr_channel_group *cg; | |
504 | GHashTableIter iter; | |
505 | gpointer key, value; | |
506 | int ret; | |
507 | ||
508 | g_hash_table_iter_init(&iter, args); | |
509 | while (g_hash_table_iter_next(&iter, &key, &value)) { | |
510 | if ((ret = opt_to_gvar(key, value, &src)) != 0) | |
511 | return ret; | |
512 | cg = select_channel_group(sdi); | |
513 | if ((ret = maybe_config_set(sr_dev_inst_driver_get(sdi), sdi, cg, | |
514 | src.key, src.data)) != SR_OK) { | |
515 | g_critical("Failed to set device option '%s': %s.", | |
516 | (char *)key, sr_strerror(ret)); | |
517 | return ret; | |
518 | } | |
519 | } | |
520 | ||
521 | return SR_OK; | |
522 | } | |
523 | ||
524 | void run_session(void) | |
525 | { | |
526 | GSList *devices, *real_devices, *sd; | |
527 | GHashTable *devargs; | |
528 | GVariant *gvar; | |
529 | struct sr_session *session; | |
530 | struct sr_trigger *trigger; | |
531 | struct sr_dev_inst *sdi; | |
532 | uint64_t min_samples, max_samples; | |
533 | GArray *drv_opts; | |
534 | guint i; | |
535 | int is_demo_dev; | |
536 | struct sr_dev_driver *driver; | |
537 | const struct sr_transform *t; | |
538 | GMainLoop *main_loop; | |
539 | ||
540 | devices = device_scan(); | |
541 | if (!devices) { | |
542 | g_critical("No devices found."); | |
543 | return; | |
544 | } | |
545 | ||
546 | real_devices = NULL; | |
547 | for (sd = devices; sd; sd = sd->next) { | |
548 | sdi = sd->data; | |
549 | ||
550 | driver = sr_dev_inst_driver_get(sdi); | |
551 | ||
552 | if (!(drv_opts = sr_dev_options(driver, NULL, NULL))) { | |
553 | g_critical("Failed to query list of driver options."); | |
554 | return; | |
555 | } | |
556 | ||
557 | is_demo_dev = 0; | |
558 | for (i = 0; i < drv_opts->len; i++) { | |
559 | if (g_array_index(drv_opts, uint32_t, i) == SR_CONF_DEMO_DEV) | |
560 | is_demo_dev = 1; | |
561 | } | |
562 | ||
563 | g_array_free(drv_opts, TRUE); | |
564 | ||
565 | if (!is_demo_dev) | |
566 | real_devices = g_slist_append(real_devices, sdi); | |
567 | } | |
568 | ||
569 | if (g_slist_length(devices) > 1) { | |
570 | if (g_slist_length(real_devices) != 1) { | |
571 | g_critical("sigrok-cli only supports one device for capturing."); | |
572 | return; | |
573 | } else { | |
574 | /* We only have one non-demo device. */ | |
575 | g_slist_free(devices); | |
576 | devices = real_devices; | |
577 | real_devices = NULL; | |
578 | } | |
579 | } | |
580 | ||
581 | /* This is unlikely to happen but it makes static analyzers stop complaining. */ | |
582 | if (!devices) { | |
583 | g_critical("No real devices found."); | |
584 | return; | |
585 | } | |
586 | ||
587 | sdi = devices->data; | |
588 | g_slist_free(devices); | |
589 | g_slist_free(real_devices); | |
590 | ||
591 | sr_session_new(sr_ctx, &session); | |
592 | sr_session_datafeed_callback_add(session, datafeed_in, session); | |
593 | ||
594 | if (sr_dev_open(sdi) != SR_OK) { | |
595 | g_critical("Failed to open device."); | |
596 | return; | |
597 | } | |
598 | ||
599 | if (sr_session_dev_add(session, sdi) != SR_OK) { | |
600 | g_critical("Failed to add device to session."); | |
601 | sr_session_destroy(session); | |
602 | return; | |
603 | } | |
604 | ||
605 | if (opt_config) { | |
606 | if ((devargs = parse_generic_arg(opt_config, FALSE))) { | |
607 | if (set_dev_options(sdi, devargs) != SR_OK) | |
608 | return; | |
609 | g_hash_table_destroy(devargs); | |
610 | } | |
611 | } | |
612 | ||
613 | if (select_channels(sdi) != SR_OK) { | |
614 | g_critical("Failed to set channels."); | |
615 | sr_session_destroy(session); | |
616 | return; | |
617 | } | |
618 | ||
619 | trigger = NULL; | |
620 | if (opt_triggers) { | |
621 | if (!parse_triggerstring(sdi, opt_triggers, &trigger)) { | |
622 | sr_session_destroy(session); | |
623 | return; | |
624 | } | |
625 | if (sr_session_trigger_set(session, trigger) != SR_OK) { | |
626 | sr_session_destroy(session); | |
627 | return; | |
628 | } | |
629 | } | |
630 | ||
631 | if (opt_continuous) { | |
632 | if (!sr_dev_has_option(sdi, SR_CONF_CONTINUOUS)) { | |
633 | g_critical("This device does not support continuous sampling."); | |
634 | sr_session_destroy(session); | |
635 | return; | |
636 | } | |
637 | } | |
638 | ||
639 | if (opt_time) { | |
640 | if (set_limit_time(sdi) != SR_OK) { | |
641 | sr_session_destroy(session); | |
642 | return; | |
643 | } | |
644 | } | |
645 | ||
646 | if (opt_samples) { | |
647 | if ((sr_parse_sizestring(opt_samples, &limit_samples) != SR_OK)) { | |
648 | g_critical("Invalid sample limit '%s'.", opt_samples); | |
649 | sr_session_destroy(session); | |
650 | return; | |
651 | } | |
652 | if (maybe_config_list(driver, sdi, NULL, SR_CONF_LIMIT_SAMPLES, | |
653 | &gvar) == SR_OK) { | |
654 | /* | |
655 | * The device has no compression, or compression is turned | |
656 | * off, and publishes its sample memory size. | |
657 | */ | |
658 | g_variant_get(gvar, "(tt)", &min_samples, &max_samples); | |
659 | g_variant_unref(gvar); | |
660 | if (limit_samples < min_samples) { | |
661 | g_critical("The device stores at least %"PRIu64 | |
662 | " samples with the current settings.", min_samples); | |
663 | } | |
664 | if (limit_samples > max_samples) { | |
665 | g_critical("The device can store only %"PRIu64 | |
666 | " samples with the current settings.", max_samples); | |
667 | } | |
668 | } | |
669 | gvar = g_variant_new_uint64(limit_samples); | |
670 | if (maybe_config_set(sr_dev_inst_driver_get(sdi), sdi, NULL, SR_CONF_LIMIT_SAMPLES, gvar) != SR_OK) { | |
671 | g_critical("Failed to configure sample limit."); | |
672 | sr_session_destroy(session); | |
673 | return; | |
674 | } | |
675 | } | |
676 | ||
677 | if (opt_frames) { | |
678 | if ((sr_parse_sizestring(opt_frames, &limit_frames) != SR_OK)) { | |
679 | g_critical("Invalid frame limit '%s'.", opt_frames); | |
680 | sr_session_destroy(session); | |
681 | return; | |
682 | } | |
683 | gvar = g_variant_new_uint64(limit_frames); | |
684 | if (maybe_config_set(sr_dev_inst_driver_get(sdi), sdi, NULL, SR_CONF_LIMIT_FRAMES, gvar) != SR_OK) { | |
685 | g_critical("Failed to configure frame limit."); | |
686 | sr_session_destroy(session); | |
687 | return; | |
688 | } | |
689 | } | |
690 | ||
691 | if (opt_transform_module) { | |
692 | if (!(t = setup_transform_module(sdi))) | |
693 | g_critical("Failed to initialize transform module."); | |
694 | } | |
695 | ||
696 | main_loop = g_main_loop_new(NULL, FALSE); | |
697 | ||
698 | sr_session_stopped_callback_set(session, | |
699 | (sr_session_stopped_callback)g_main_loop_quit, main_loop); | |
700 | ||
701 | if (sr_session_start(session) != SR_OK) { | |
702 | g_critical("Failed to start session."); | |
703 | g_main_loop_unref(main_loop); | |
704 | sr_session_destroy(session); | |
705 | return; | |
706 | } | |
707 | ||
708 | if (opt_continuous) | |
709 | add_anykey(session); | |
710 | ||
711 | g_main_loop_run(main_loop); | |
712 | ||
713 | if (opt_continuous) | |
714 | clear_anykey(); | |
715 | ||
716 | if (trigger) | |
717 | sr_trigger_free(trigger); | |
718 | ||
719 | sr_session_datafeed_callback_remove_all(session); | |
720 | g_main_loop_unref(main_loop); | |
721 | sr_session_destroy(session); | |
722 | } |