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