]> sigrok.org Git - sigrok-cli.git/blame_incremental - session.c
session: print error message when output file creation fails
[sigrok-cli.git] / session.c
... / ...
CommitLineData
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
27static uint64_t limit_samples = 0;
28static uint64_t limit_frames = 0;
29
30#ifdef HAVE_SRD
31extern struct srd_session *srd_sess;
32#endif
33
34static 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
79const 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 if (!*outfile) {
113 g_critical("Cannot write to output file '%s'.",
114 opt_output_file);
115 }
116 } else {
117 *outfile = NULL;
118 }
119 } else {
120 *outfile = stdout;
121 }
122
123 if (fmtopts)
124 g_hash_table_destroy(fmtopts);
125 g_hash_table_destroy(fmtargs);
126
127 return o;
128}
129
130const struct sr_transform *setup_transform_module(const struct sr_dev_inst *sdi)
131{
132 const struct sr_transform_module *tmod;
133 const struct sr_option **options;
134 const struct sr_transform *t;
135 GHashTable *fmtargs, *fmtopts;
136 char *fmtspec;
137
138 fmtargs = parse_generic_arg(opt_transform_module, TRUE);
139 fmtspec = g_hash_table_lookup(fmtargs, "sigrok_key");
140 if (!fmtspec)
141 g_critical("Invalid transform module.");
142 if (!(tmod = sr_transform_find(fmtspec)))
143 g_critical("Unknown transform module '%s'.", fmtspec);
144 g_hash_table_remove(fmtargs, "sigrok_key");
145 if ((options = sr_transform_options_get(tmod))) {
146 fmtopts = generic_arg_to_opt(options, fmtargs);
147 sr_transform_options_free(options);
148 } else
149 fmtopts = NULL;
150 t = sr_transform_new(tmod, fmtopts, sdi);
151 if (fmtopts)
152 g_hash_table_destroy(fmtopts);
153 g_hash_table_destroy(fmtargs);
154
155 return t;
156}
157
158/* Get the input stream's list of channels and their types, once. */
159static void props_get_channels(struct df_arg_desc *args,
160 const struct sr_dev_inst *sdi)
161{
162 struct input_stream_props *props;
163 GSList *l;
164 const struct sr_channel *ch;
165
166 if (!args)
167 return;
168 props = &args->props;
169 if (props->channels)
170 return;
171
172 props->channels = g_slist_copy(sr_dev_inst_channels_get(sdi));
173 if (!props->channels)
174 return;
175 for (l = props->channels; l; l = l->next) {
176 ch = l->data;
177 if (!ch->enabled)
178 continue;
179 if (ch->type != SR_CHANNEL_ANALOG)
180 continue;
181 props->first_analog_channel = ch;
182 break;
183 }
184}
185
186static gboolean props_chk_1st_channel(struct df_arg_desc *args,
187 const struct sr_datafeed_analog *analog)
188{
189 struct sr_channel *ch;
190
191 if (!args || !analog || !analog->meaning)
192 return FALSE;
193 ch = g_slist_nth_data(analog->meaning->channels, 0);
194 if (!ch)
195 return FALSE;
196 return ch == args->props.first_analog_channel;
197}
198
199static void props_dump_details(struct df_arg_desc *args)
200{
201 struct input_stream_props *props;
202 size_t ch_count;
203 GSList *l;
204 const struct sr_channel *ch;
205 const char *type;
206
207 if (!args)
208 return;
209 props = &args->props;
210 if (props->samplerate)
211 printf("Samplerate: %" PRIu64 "\n", props->samplerate);
212 if (props->channels) {
213 ch_count = g_slist_length(props->channels);
214 printf("Channels: %zu\n", ch_count);
215 for (l = props->channels; l; l = l->next) {
216 ch = l->data;
217 if (ch->type == SR_CHANNEL_ANALOG)
218 type = "analog";
219 else
220 type = "logic";
221 printf("- %s: %s\n", ch->name, type);
222 }
223 }
224 if (props->unitsize)
225 printf("Logic unitsize: %zu\n", props->unitsize);
226 if (props->sample_count_logic)
227 printf("Logic sample count: %" PRIu64 "\n", props->sample_count_logic);
228 if (props->sample_count_analog)
229 printf("Analog sample count: %" PRIu64 "\n", props->sample_count_analog);
230 if (props->frame_count)
231 printf("Frame count: %" PRIu64 "\n", props->frame_count);
232 if (props->triggered)
233 printf("Trigger count: %" PRIu64 "\n", props->triggered);
234}
235
236static void props_cleanup(struct df_arg_desc *args)
237{
238 struct input_stream_props *props;
239
240 if (!args)
241 return;
242 props = &args->props;
243 g_slist_free(props->channels);
244 props->channels = NULL;
245 props->first_analog_channel = NULL;
246}
247
248void datafeed_in(const struct sr_dev_inst *sdi,
249 const struct sr_datafeed_packet *packet, void *cb_data)
250{
251 static const struct sr_output *o = NULL;
252 static const struct sr_output *oa = NULL;
253 static uint64_t rcvd_samples_logic = 0;
254 static uint64_t rcvd_samples_analog = 0;
255 static uint64_t samplerate = 0;
256 static int triggered = 0;
257 static FILE *outfile = NULL;
258
259 const struct sr_datafeed_meta *meta;
260 const struct sr_datafeed_logic *logic;
261 const struct sr_datafeed_analog *analog;
262 struct df_arg_desc *df_arg;
263 int do_props;
264 struct input_stream_props *props;
265 struct sr_session *session;
266 struct sr_config *src;
267 GSList *l;
268 GString *out;
269 GVariant *gvar;
270 uint64_t end_sample;
271 uint64_t input_len;
272 struct sr_dev_driver *driver;
273
274 /* Avoid warnings when building without decoder support. */
275 (void)session;
276 (void)input_len;
277
278 driver = sr_dev_inst_driver_get(sdi);
279
280 /* Skip all packets before the first header. */
281 if (packet->type != SR_DF_HEADER && !o)
282 return;
283
284 /* Prepare to either process data, or "just" gather properties. */
285 df_arg = cb_data;
286 session = df_arg->session;
287 do_props = df_arg->do_props;
288 props = &df_arg->props;
289
290 switch (packet->type) {
291 case SR_DF_HEADER:
292 g_debug("cli: Received SR_DF_HEADER.");
293 if (maybe_config_get(driver, sdi, NULL, SR_CONF_SAMPLERATE,
294 &gvar) == SR_OK) {
295 samplerate = g_variant_get_uint64(gvar);
296 g_variant_unref(gvar);
297 }
298 if (do_props) {
299 /* Setup variables for maximum code path re-use. */
300 o = (void *)-1;
301 limit_samples = 0;
302 /* Start collecting input stream properties. */
303 memset(props, 0, sizeof(*props));
304 props->samplerate = samplerate;
305 props_get_channels(df_arg, sdi);
306 break;
307 }
308 if (!(o = setup_output_format(sdi, &outfile)))
309 g_critical("Failed to initialize output module.");
310
311 /* Set up backup analog output module. */
312 if (outfile)
313 oa = sr_output_new(sr_output_find("analog"), NULL,
314 sdi, NULL);
315
316 rcvd_samples_logic = rcvd_samples_analog = 0;
317
318#ifdef HAVE_SRD
319 if (opt_pds) {
320 if (samplerate) {
321 if (srd_session_metadata_set(srd_sess, SRD_CONF_SAMPLERATE,
322 g_variant_new_uint64(samplerate)) != SRD_OK) {
323 g_critical("Failed to configure decode session.");
324 break;
325 }
326 }
327 if (srd_session_start(srd_sess) != SRD_OK) {
328 g_critical("Failed to start decode session.");
329 break;
330 }
331 }
332#endif
333 break;
334
335 case SR_DF_META:
336 g_debug("cli: Received SR_DF_META.");
337 meta = packet->payload;
338 for (l = meta->config; l; l = l->next) {
339 src = l->data;
340 switch (src->key) {
341 case SR_CONF_SAMPLERATE:
342 samplerate = g_variant_get_uint64(src->data);
343 g_debug("cli: Got samplerate %"PRIu64" Hz.", samplerate);
344 if (do_props) {
345 props->samplerate = samplerate;
346 break;
347 }
348#ifdef HAVE_SRD
349 if (opt_pds) {
350 if (srd_session_metadata_set(srd_sess, SRD_CONF_SAMPLERATE,
351 g_variant_new_uint64(samplerate)) != SRD_OK) {
352 g_critical("Failed to pass samplerate to decoder.");
353 }
354 }
355#endif
356 break;
357 case SR_CONF_SAMPLE_INTERVAL:
358 samplerate = g_variant_get_uint64(src->data);
359 g_debug("cli: Got sample interval %"PRIu64" ms.", samplerate);
360 if (do_props) {
361 props->samplerate = samplerate;
362 break;
363 }
364 break;
365 default:
366 /* Unknown metadata is not an error. */
367 break;
368 }
369 }
370 break;
371
372 case SR_DF_TRIGGER:
373 g_debug("cli: Received SR_DF_TRIGGER.");
374 if (do_props) {
375 props->triggered++;
376 break;
377 }
378 triggered = 1;
379 break;
380
381 case SR_DF_LOGIC:
382 logic = packet->payload;
383 g_message("cli: Received SR_DF_LOGIC (%"PRIu64" bytes, unitsize = %d).",
384 logic->length, logic->unitsize);
385 if (logic->length == 0)
386 break;
387
388 if (do_props) {
389 props_get_channels(df_arg, sdi);
390 props->unitsize = logic->unitsize;
391 props->sample_count_logic += logic->length / logic->unitsize;
392 break;
393 }
394
395 /* Don't store any samples until triggered. */
396 if (opt_wait_trigger && !triggered)
397 break;
398
399 if (limit_samples && rcvd_samples_logic >= limit_samples)
400 break;
401
402 end_sample = rcvd_samples_logic + logic->length / logic->unitsize;
403 /* Cut off last packet according to the sample limit. */
404 if (limit_samples && end_sample > limit_samples)
405 end_sample = limit_samples;
406 input_len = (end_sample - rcvd_samples_logic) * logic->unitsize;
407
408 if (opt_pds) {
409#ifdef HAVE_SRD
410 if (srd_session_send(srd_sess, rcvd_samples_logic, end_sample,
411 logic->data, input_len, logic->unitsize) != SRD_OK)
412 sr_session_stop(session);
413#endif
414 }
415
416 rcvd_samples_logic = end_sample;
417 break;
418
419 case SR_DF_ANALOG:
420 analog = packet->payload;
421 g_message("cli: Received SR_DF_ANALOG (%d samples).", analog->num_samples);
422 if (analog->num_samples == 0)
423 break;
424
425 if (do_props) {
426 /* Only count the first analog channel. */
427 props_get_channels(df_arg, sdi);
428 if (!props_chk_1st_channel(df_arg, analog))
429 break;
430 props->sample_count_analog += analog->num_samples;
431 break;
432 }
433
434 if (limit_samples && rcvd_samples_analog >= limit_samples)
435 break;
436
437 rcvd_samples_analog += analog->num_samples;
438 break;
439
440 case SR_DF_FRAME_BEGIN:
441 g_debug("cli: Received SR_DF_FRAME_BEGIN.");
442 break;
443
444 case SR_DF_FRAME_END:
445 g_debug("cli: Received SR_DF_FRAME_END.");
446 if (do_props) {
447 props->frame_count++;
448 break;
449 }
450 break;
451
452 default:
453 break;
454 }
455
456 if (!do_props && o && !opt_pds) {
457 if (sr_output_send(o, packet, &out) == SR_OK) {
458 if (oa && !out) {
459 /*
460 * The user didn't specify an output module,
461 * but needs to see this analog data.
462 */
463 sr_output_send(oa, packet, &out);
464 }
465 if (outfile && out && out->len > 0) {
466 fwrite(out->str, 1, out->len, outfile);
467 fflush(outfile);
468 }
469 if (out)
470 g_string_free(out, TRUE);
471 }
472 }
473
474 /*
475 * SR_DF_END needs to be handled after the output module's receive()
476 * is called, so it can properly clean up that module.
477 */
478 if (packet->type == SR_DF_END) {
479 g_debug("cli: Received SR_DF_END.");
480
481 if (do_props) {
482 props_dump_details(df_arg);
483 props_cleanup(df_arg);
484 o = NULL;
485 }
486
487 if (o)
488 sr_output_free(o);
489 o = NULL;
490
491 if (oa)
492 sr_output_free(oa);
493 oa = NULL;
494
495 if (outfile && outfile != stdout)
496 fclose(outfile);
497
498 if (limit_samples) {
499 if (rcvd_samples_logic > 0 && rcvd_samples_logic < limit_samples)
500 g_warning("Device only sent %" PRIu64 " samples.",
501 rcvd_samples_logic);
502 else if (rcvd_samples_analog > 0 && rcvd_samples_analog < limit_samples)
503 g_warning("Device only sent %" PRIu64 " samples.",
504 rcvd_samples_analog);
505 }
506 }
507
508}
509
510int opt_to_gvar(char *key, char *value, struct sr_config *src)
511{
512 const struct sr_key_info *srci, *srmqi;
513 double tmp_double, dlow, dhigh;
514 uint64_t tmp_u64, p, q, low, high, mqflags;
515 uint32_t mq;
516 GVariant *rational[2], *range[2], *gtup[2];
517 GVariantBuilder *vbl;
518 gboolean tmp_bool;
519 gchar **keyval;
520 int ret, i;
521
522 if (!(srci = sr_key_info_name_get(SR_KEY_CONFIG, key))) {
523 g_critical("Unknown device option '%s'.", (char *) key);
524 return -1;
525 }
526 src->key = srci->key;
527
528 if ((!value || strlen(value) == 0) &&
529 (srci->datatype != SR_T_BOOL)) {
530 g_critical("Option '%s' needs a value.", (char *)key);
531 return -1;
532 }
533
534 ret = 0;
535 switch (srci->datatype) {
536 case SR_T_UINT64:
537 ret = sr_parse_sizestring(value, &tmp_u64);
538 if (ret != 0)
539 break;
540 src->data = g_variant_new_uint64(tmp_u64);
541 break;
542 case SR_T_INT32:
543 ret = sr_parse_sizestring(value, &tmp_u64);
544 if (ret != 0)
545 break;
546 src->data = g_variant_new_int32(tmp_u64);
547 break;
548 case SR_T_STRING:
549 src->data = g_variant_new_string(value);
550 break;
551 case SR_T_BOOL:
552 if (!value)
553 tmp_bool = TRUE;
554 else
555 tmp_bool = sr_parse_boolstring(value);
556 src->data = g_variant_new_boolean(tmp_bool);
557 break;
558 case SR_T_FLOAT:
559 tmp_double = strtof(value, NULL);
560 src->data = g_variant_new_double(tmp_double);
561 break;
562 case SR_T_RATIONAL_PERIOD:
563 if ((ret = sr_parse_period(value, &p, &q)) != SR_OK)
564 break;
565 rational[0] = g_variant_new_uint64(p);
566 rational[1] = g_variant_new_uint64(q);
567 src->data = g_variant_new_tuple(rational, 2);
568 break;
569 case SR_T_RATIONAL_VOLT:
570 if ((ret = sr_parse_voltage(value, &p, &q)) != SR_OK)
571 break;
572 rational[0] = g_variant_new_uint64(p);
573 rational[1] = g_variant_new_uint64(q);
574 src->data = g_variant_new_tuple(rational, 2);
575 break;
576 case SR_T_UINT64_RANGE:
577 if (sscanf(value, "%"PRIu64"-%"PRIu64, &low, &high) != 2) {
578 ret = -1;
579 break;
580 } else {
581 range[0] = g_variant_new_uint64(low);
582 range[1] = g_variant_new_uint64(high);
583 src->data = g_variant_new_tuple(range, 2);
584 }
585 break;
586 case SR_T_DOUBLE_RANGE:
587 if (sscanf(value, "%lf-%lf", &dlow, &dhigh) != 2) {
588 ret = -1;
589 break;
590 } else {
591 range[0] = g_variant_new_double(dlow);
592 range[1] = g_variant_new_double(dhigh);
593 src->data = g_variant_new_tuple(range, 2);
594 }
595 break;
596 case SR_T_KEYVALUE:
597 /* Expects the argument to be in the form of key=value. */
598 keyval = g_strsplit(value, "=", 2);
599 if (!keyval[0] || !keyval[1]) {
600 g_strfreev(keyval);
601 ret = -1;
602 break;
603 } else {
604 vbl = g_variant_builder_new(G_VARIANT_TYPE_DICTIONARY);
605 g_variant_builder_add(vbl, "{ss}",
606 keyval[0], keyval[1]);
607 src->data = g_variant_builder_end(vbl);
608 g_strfreev(keyval);
609 }
610 break;
611 case SR_T_MQ:
612 /*
613 Argument is MQ id e.g. ("voltage") optionally followed by one
614 or more /<mqflag> e.g. "/ac".
615 */
616 keyval = g_strsplit(value, "/", 0);
617 if (!keyval[0] || !(srmqi = sr_key_info_name_get(SR_KEY_MQ, keyval[0]))) {
618 g_strfreev(keyval);
619 ret = -1;
620 break;
621 }
622 mq = srmqi->key;
623 mqflags = 0;
624 for (i = 1; keyval[i]; i++) {
625 if (!(srmqi = sr_key_info_name_get(SR_KEY_MQFLAGS, keyval[i]))) {
626 ret = -1;
627 break;
628 }
629 mqflags |= srmqi->key;
630 }
631 g_strfreev(keyval);
632 if (ret != -1) {
633 gtup[0] = g_variant_new_uint32(mq);
634 gtup[1] = g_variant_new_uint64(mqflags);
635 src->data = g_variant_new_tuple(gtup, 2);
636 }
637 break;
638 default:
639 g_critical("Unknown data type specified for option '%s' "
640 "(driver implementation bug?).", key);
641 ret = -1;
642 }
643
644 if (ret < 0)
645 g_critical("Invalid value: '%s' for option '%s'", value, key);
646
647 return ret;
648}
649
650int set_dev_options(struct sr_dev_inst *sdi, GHashTable *args)
651{
652 struct sr_config src;
653 struct sr_channel_group *cg;
654 GHashTableIter iter;
655 gpointer key, value;
656 int ret;
657
658 g_hash_table_iter_init(&iter, args);
659 while (g_hash_table_iter_next(&iter, &key, &value)) {
660 if ((ret = opt_to_gvar(key, value, &src)) != 0)
661 return ret;
662 cg = select_channel_group(sdi);
663 if ((ret = maybe_config_set(sr_dev_inst_driver_get(sdi), sdi, cg,
664 src.key, src.data)) != SR_OK) {
665 g_critical("Failed to set device option '%s': %s.",
666 (char *)key, sr_strerror(ret));
667 return ret;
668 }
669 }
670
671 return SR_OK;
672}
673
674void run_session(void)
675{
676 struct df_arg_desc df_arg;
677 GSList *devices, *real_devices, *sd;
678 GHashTable *devargs;
679 GVariant *gvar;
680 struct sr_session *session;
681 struct sr_trigger *trigger;
682 struct sr_dev_inst *sdi;
683 uint64_t min_samples, max_samples;
684 GArray *drv_opts;
685 guint i;
686 int is_demo_dev;
687 struct sr_dev_driver *driver;
688 const struct sr_transform *t;
689 GMainLoop *main_loop;
690
691 memset(&df_arg, 0, sizeof(df_arg));
692 df_arg.do_props = FALSE;
693
694 devices = device_scan();
695 if (!devices) {
696 g_critical("No devices found.");
697 return;
698 }
699
700 real_devices = NULL;
701 for (sd = devices; sd; sd = sd->next) {
702 sdi = sd->data;
703
704 driver = sr_dev_inst_driver_get(sdi);
705
706 if (!(drv_opts = sr_dev_options(driver, NULL, NULL))) {
707 g_critical("Failed to query list of driver options.");
708 return;
709 }
710
711 is_demo_dev = 0;
712 for (i = 0; i < drv_opts->len; i++) {
713 if (g_array_index(drv_opts, uint32_t, i) == SR_CONF_DEMO_DEV)
714 is_demo_dev = 1;
715 }
716
717 g_array_free(drv_opts, TRUE);
718
719 if (!is_demo_dev)
720 real_devices = g_slist_append(real_devices, sdi);
721 }
722
723 if (g_slist_length(devices) > 1) {
724 if (g_slist_length(real_devices) != 1) {
725 g_critical("sigrok-cli only supports one device for capturing.");
726 return;
727 } else {
728 /* We only have one non-demo device. */
729 g_slist_free(devices);
730 devices = real_devices;
731 real_devices = NULL;
732 }
733 }
734
735 /* This is unlikely to happen but it makes static analyzers stop complaining. */
736 if (!devices) {
737 g_critical("No real devices found.");
738 return;
739 }
740
741 sdi = devices->data;
742 g_slist_free(devices);
743 g_slist_free(real_devices);
744
745 sr_session_new(sr_ctx, &session);
746 df_arg.session = session;
747 sr_session_datafeed_callback_add(session, datafeed_in, &df_arg);
748 df_arg.session = NULL;
749
750 if (sr_dev_open(sdi) != SR_OK) {
751 g_critical("Failed to open device.");
752 return;
753 }
754
755 if (sr_session_dev_add(session, sdi) != SR_OK) {
756 g_critical("Failed to add device to session.");
757 sr_session_destroy(session);
758 return;
759 }
760
761 if (opt_config) {
762 if ((devargs = parse_generic_arg(opt_config, FALSE))) {
763 if (set_dev_options(sdi, devargs) != SR_OK)
764 return;
765 g_hash_table_destroy(devargs);
766 }
767 }
768
769 if (select_channels(sdi) != SR_OK) {
770 g_critical("Failed to set channels.");
771 sr_session_destroy(session);
772 return;
773 }
774
775 trigger = NULL;
776 if (opt_triggers) {
777 if (!parse_triggerstring(sdi, opt_triggers, &trigger)) {
778 sr_session_destroy(session);
779 return;
780 }
781 if (sr_session_trigger_set(session, trigger) != SR_OK) {
782 sr_session_destroy(session);
783 return;
784 }
785 }
786
787 if (opt_continuous) {
788 if (!sr_dev_has_option(sdi, SR_CONF_CONTINUOUS)) {
789 g_critical("This device does not support continuous sampling.");
790 sr_session_destroy(session);
791 return;
792 }
793 }
794
795 if (opt_time) {
796 if (set_limit_time(sdi) != SR_OK) {
797 sr_session_destroy(session);
798 return;
799 }
800 }
801
802 if (opt_samples) {
803 if ((sr_parse_sizestring(opt_samples, &limit_samples) != SR_OK)) {
804 g_critical("Invalid sample limit '%s'.", opt_samples);
805 sr_session_destroy(session);
806 return;
807 }
808 if (maybe_config_list(driver, sdi, NULL, SR_CONF_LIMIT_SAMPLES,
809 &gvar) == SR_OK) {
810 /*
811 * The device has no compression, or compression is turned
812 * off, and publishes its sample memory size.
813 */
814 g_variant_get(gvar, "(tt)", &min_samples, &max_samples);
815 g_variant_unref(gvar);
816 if (limit_samples < min_samples) {
817 g_critical("The device stores at least %"PRIu64
818 " samples with the current settings.", min_samples);
819 }
820 if (limit_samples > max_samples) {
821 g_critical("The device can store only %"PRIu64
822 " samples with the current settings.", max_samples);
823 }
824 }
825 gvar = g_variant_new_uint64(limit_samples);
826 if (maybe_config_set(sr_dev_inst_driver_get(sdi), sdi, NULL, SR_CONF_LIMIT_SAMPLES, gvar) != SR_OK) {
827 g_critical("Failed to configure sample limit.");
828 sr_session_destroy(session);
829 return;
830 }
831 }
832
833 if (opt_frames) {
834 if ((sr_parse_sizestring(opt_frames, &limit_frames) != SR_OK)) {
835 g_critical("Invalid frame limit '%s'.", opt_frames);
836 sr_session_destroy(session);
837 return;
838 }
839 gvar = g_variant_new_uint64(limit_frames);
840 if (maybe_config_set(sr_dev_inst_driver_get(sdi), sdi, NULL, SR_CONF_LIMIT_FRAMES, gvar) != SR_OK) {
841 g_critical("Failed to configure frame limit.");
842 sr_session_destroy(session);
843 return;
844 }
845 }
846
847 if (opt_transform_module) {
848 if (!(t = setup_transform_module(sdi)))
849 g_critical("Failed to initialize transform module.");
850 }
851
852 main_loop = g_main_loop_new(NULL, FALSE);
853
854 sr_session_stopped_callback_set(session,
855 (sr_session_stopped_callback)g_main_loop_quit, main_loop);
856
857 if (sr_session_start(session) != SR_OK) {
858 g_critical("Failed to start session.");
859 g_main_loop_unref(main_loop);
860 sr_session_destroy(session);
861 return;
862 }
863
864 if (opt_continuous)
865 add_anykey(session);
866
867 g_main_loop_run(main_loop);
868
869 if (opt_continuous)
870 clear_anykey();
871
872 if (trigger)
873 sr_trigger_free(trigger);
874
875 sr_session_datafeed_callback_remove_all(session);
876 g_main_loop_unref(main_loop);
877 sr_session_destroy(session);
878}