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