]> sigrok.org Git - sigrok-cli.git/blame - session.c
Set stdout to binary mode on Windows as needed
[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 109 if (opt_output_file) {
9ed80f1d 110 if (!sr_output_test_flag(omod, SR_OUTPUT_INTERNAL_IO_HANDLING)) {
cbbea087 111 *outfile = g_fopen(opt_output_file, "wb");
9ed80f1d
GS
112 if (!*outfile) {
113 g_critical("Cannot write to output file '%s'.",
114 opt_output_file);
115 }
116 } else {
cbbea087 117 *outfile = NULL;
9ed80f1d 118 }
cbbea087 119 } else {
e8a9eb8d 120 setup_binary_stdout();
cbbea087
SA
121 *outfile = stdout;
122 }
123
ad6520c4
BV
124 if (fmtopts)
125 g_hash_table_destroy(fmtopts);
2be182e6
BV
126 g_hash_table_destroy(fmtargs);
127
6709a694 128 return o;
2be182e6
BV
129}
130
fb995521
UH
131const 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;
fb995521
UH
137 char *fmtspec;
138
fb995521
UH
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
1c950633
GS
159/* Get the input stream's list of channels and their types, once. */
160static 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
187static 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
200static 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
237static 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
2be182e6
BV
249void datafeed_in(const struct sr_dev_inst *sdi,
250 const struct sr_datafeed_packet *packet, void *cb_data)
251{
ad6520c4
BV
252 static const struct sr_output *o = NULL;
253 static const struct sr_output *oa = NULL;
6ea663a7
BV
254 static uint64_t rcvd_samples_logic = 0;
255 static uint64_t rcvd_samples_analog = 0;
256 static uint64_t samplerate = 0;
2be182e6
BV
257 static int triggered = 0;
258 static FILE *outfile = NULL;
196e1a9c
GS
259
260 const struct sr_datafeed_meta *meta;
261 const struct sr_datafeed_logic *logic;
262 const struct sr_datafeed_analog *analog;
1c950633
GS
263 struct df_arg_desc *df_arg;
264 int do_props;
265 struct input_stream_props *props;
196e1a9c
GS
266 struct sr_session *session;
267 struct sr_config *src;
fea5acab 268 GSList *l;
2be182e6 269 GString *out;
8b65cdcc 270 GVariant *gvar;
55de58a2 271 uint64_t end_sample;
667d4a18 272 uint64_t input_len;
c6fa2b2e
UH
273 struct sr_dev_driver *driver;
274
3f85a618
UH
275 /* Avoid warnings when building without decoder support. */
276 (void)session;
277 (void)input_len;
278
c6fa2b2e 279 driver = sr_dev_inst_driver_get(sdi);
2be182e6 280
196e1a9c 281 /* Skip all packets before the first header. */
23c40b60 282 if (packet->type != SR_DF_HEADER && !o)
2be182e6
BV
283 return;
284
1c950633
GS
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
2be182e6
BV
291 switch (packet->type) {
292 case SR_DF_HEADER:
4ed1cdea 293 g_debug("cli: Received SR_DF_HEADER.");
1c950633
GS
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 }
cbbea087 309 if (!(o = setup_output_format(sdi, &outfile)))
e786e625 310 g_critical("Failed to initialize output module.");
2be182e6 311
9216694f 312 /* Set up backup analog output module. */
f0de24ff
VP
313 if (outfile)
314 oa = sr_output_new(sr_output_find("analog"), NULL,
315 sdi, NULL);
9216694f 316
6ea663a7 317 rcvd_samples_logic = rcvd_samples_analog = 0;
2be182e6
BV
318
319#ifdef HAVE_SRD
ea6d6dec 320 if (opt_pds) {
8b65cdcc 321 if (samplerate) {
2be182e6
BV
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 }
328 if (srd_session_start(srd_sess) != SRD_OK) {
329 g_critical("Failed to start decode session.");
330 break;
331 }
332 }
333#endif
334 break;
335
336 case SR_DF_META:
4ed1cdea 337 g_debug("cli: Received SR_DF_META.");
2be182e6
BV
338 meta = packet->payload;
339 for (l = meta->config; l; l = l->next) {
340 src = l->data;
341 switch (src->key) {
342 case SR_CONF_SAMPLERATE:
343 samplerate = g_variant_get_uint64(src->data);
4ed1cdea 344 g_debug("cli: Got samplerate %"PRIu64" Hz.", samplerate);
1c950633
GS
345 if (do_props) {
346 props->samplerate = samplerate;
347 break;
348 }
2be182e6
BV
349#ifdef HAVE_SRD
350 if (opt_pds) {
351 if (srd_session_metadata_set(srd_sess, SRD_CONF_SAMPLERATE,
352 g_variant_new_uint64(samplerate)) != SRD_OK) {
353 g_critical("Failed to pass samplerate to decoder.");
354 }
355 }
356#endif
357 break;
358 case SR_CONF_SAMPLE_INTERVAL:
359 samplerate = g_variant_get_uint64(src->data);
4ed1cdea 360 g_debug("cli: Got sample interval %"PRIu64" ms.", samplerate);
1c950633
GS
361 if (do_props) {
362 props->samplerate = samplerate;
363 break;
364 }
2be182e6
BV
365 break;
366 default:
367 /* Unknown metadata is not an error. */
368 break;
369 }
370 }
371 break;
372
373 case SR_DF_TRIGGER:
4ed1cdea 374 g_debug("cli: Received SR_DF_TRIGGER.");
1c950633
GS
375 if (do_props) {
376 props->triggered++;
377 break;
378 }
2be182e6
BV
379 triggered = 1;
380 break;
381
382 case SR_DF_LOGIC:
383 logic = packet->payload;
4ed1cdea
UH
384 g_message("cli: Received SR_DF_LOGIC (%"PRIu64" bytes, unitsize = %d).",
385 logic->length, logic->unitsize);
2be182e6
BV
386 if (logic->length == 0)
387 break;
388
1c950633
GS
389 if (do_props) {
390 props_get_channels(df_arg, sdi);
391 props->unitsize = logic->unitsize;
392 props->sample_count_logic += logic->length / logic->unitsize;
393 break;
394 }
395
2be182e6
BV
396 /* Don't store any samples until triggered. */
397 if (opt_wait_trigger && !triggered)
398 break;
399
6ea663a7 400 if (limit_samples && rcvd_samples_logic >= limit_samples)
2be182e6
BV
401 break;
402
6ea663a7 403 end_sample = rcvd_samples_logic + logic->length / logic->unitsize;
8667d3c2
DE
404 /* Cut off last packet according to the sample limit. */
405 if (limit_samples && end_sample > limit_samples)
406 end_sample = limit_samples;
6ea663a7 407 input_len = (end_sample - rcvd_samples_logic) * logic->unitsize;
2be182e6 408
fea5acab 409 if (opt_pds) {
2be182e6 410#ifdef HAVE_SRD
fea5acab 411 if (srd_session_send(srd_sess, rcvd_samples_logic, end_sample,
ee639fb4 412 logic->data, input_len, logic->unitsize) != SRD_OK)
fea5acab 413 sr_session_stop(session);
2be182e6 414#endif
2be182e6 415 }
2be182e6 416
6ea663a7 417 rcvd_samples_logic = end_sample;
2be182e6
BV
418 break;
419
420 case SR_DF_ANALOG:
421 analog = packet->payload;
4ed1cdea 422 g_message("cli: Received SR_DF_ANALOG (%d samples).", analog->num_samples);
2be182e6
BV
423 if (analog->num_samples == 0)
424 break;
425
1c950633
GS
426 if (do_props) {
427 /* Only count the first analog channel. */
428 props_get_channels(df_arg, sdi);
429 if (!props_chk_1st_channel(df_arg, analog))
430 break;
431 props->sample_count_analog += analog->num_samples;
432 break;
433 }
434
6ea663a7 435 if (limit_samples && rcvd_samples_analog >= limit_samples)
2be182e6
BV
436 break;
437
6ea663a7 438 rcvd_samples_analog += analog->num_samples;
2be182e6
BV
439 break;
440
441 case SR_DF_FRAME_BEGIN:
4ed1cdea 442 g_debug("cli: Received SR_DF_FRAME_BEGIN.");
2be182e6
BV
443 break;
444
445 case SR_DF_FRAME_END:
4ed1cdea 446 g_debug("cli: Received SR_DF_FRAME_END.");
1c950633
GS
447 if (do_props) {
448 props->frame_count++;
449 break;
450 }
2be182e6
BV
451 break;
452
453 default:
454 break;
455 }
456
1c950633 457 if (!do_props && o && !opt_pds) {
9216694f 458 if (sr_output_send(o, packet, &out) == SR_OK) {
f0de24ff 459 if (oa && !out) {
f0f54487
UH
460 /*
461 * The user didn't specify an output module,
462 * but needs to see this analog data.
463 */
9216694f
BV
464 sr_output_send(oa, packet, &out);
465 }
cbbea087 466 if (outfile && out && out->len > 0) {
9216694f
BV
467 fwrite(out->str, 1, out->len, outfile);
468 fflush(outfile);
469 }
470 if (out)
471 g_string_free(out, TRUE);
2be182e6
BV
472 }
473 }
474
f0f54487
UH
475 /*
476 * SR_DF_END needs to be handled after the output module's receive()
477 * is called, so it can properly clean up that module.
478 */
2be182e6 479 if (packet->type == SR_DF_END) {
4ed1cdea 480 g_debug("cli: Received SR_DF_END.");
2be182e6 481
1c950633
GS
482 if (do_props) {
483 props_dump_details(df_arg);
484 props_cleanup(df_arg);
485 o = NULL;
486 }
487
5d7604d1 488 if (o)
6709a694 489 sr_output_free(o);
2be182e6
BV
490 o = NULL;
491
f0de24ff
VP
492 if (oa)
493 sr_output_free(oa);
9216694f
BV
494 oa = NULL;
495
2be182e6
BV
496 if (outfile && outfile != stdout)
497 fclose(outfile);
498
6ea663a7
BV
499 if (limit_samples) {
500 if (rcvd_samples_logic > 0 && rcvd_samples_logic < limit_samples)
501 g_warning("Device only sent %" PRIu64 " samples.",
502 rcvd_samples_logic);
503 else if (rcvd_samples_analog > 0 && rcvd_samples_analog < limit_samples)
504 g_warning("Device only sent %" PRIu64 " samples.",
505 rcvd_samples_analog);
2be182e6
BV
506 }
507 }
508
509}
510
ad54a1a6 511int opt_to_gvar(char *key, char *value, struct sr_config *src)
2be182e6 512{
e9505599 513 const struct sr_key_info *srci, *srmqi;
426d0cda 514 double tmp_double, dlow, dhigh;
e9505599
BV
515 uint64_t tmp_u64, p, q, low, high, mqflags;
516 uint32_t mq;
84b39e3e 517 GVariant *rational[2], *range[2], *gtup[2];
e4588937 518 GVariantBuilder *vbl;
2be182e6 519 gboolean tmp_bool;
e4588937 520 gchar **keyval;
e9505599 521 int ret, i;
2be182e6 522
e4e4b472 523 if (!(srci = sr_key_info_name_get(SR_KEY_CONFIG, key))) {
ad54a1a6
BV
524 g_critical("Unknown device option '%s'.", (char *) key);
525 return -1;
526 }
527 src->key = srci->key;
2be182e6 528
23c40b60 529 if ((!value || strlen(value) == 0) &&
ad54a1a6
BV
530 (srci->datatype != SR_T_BOOL)) {
531 g_critical("Option '%s' needs a value.", (char *)key);
532 return -1;
533 }
534
535 ret = 0;
536 switch (srci->datatype) {
537 case SR_T_UINT64:
538 ret = sr_parse_sizestring(value, &tmp_u64);
539 if (ret != 0)
2be182e6 540 break;
ad54a1a6
BV
541 src->data = g_variant_new_uint64(tmp_u64);
542 break;
543 case SR_T_INT32:
544 ret = sr_parse_sizestring(value, &tmp_u64);
545 if (ret != 0)
2be182e6 546 break;
ad54a1a6
BV
547 src->data = g_variant_new_int32(tmp_u64);
548 break;
9db40e9f 549 case SR_T_STRING:
ad54a1a6
BV
550 src->data = g_variant_new_string(value);
551 break;
552 case SR_T_BOOL:
553 if (!value)
554 tmp_bool = TRUE;
555 else
556 tmp_bool = sr_parse_boolstring(value);
557 src->data = g_variant_new_boolean(tmp_bool);
558 break;
559 case SR_T_FLOAT:
560 tmp_double = strtof(value, NULL);
561 src->data = g_variant_new_double(tmp_double);
562 break;
563 case SR_T_RATIONAL_PERIOD:
564 if ((ret = sr_parse_period(value, &p, &q)) != SR_OK)
2be182e6 565 break;
ad54a1a6
BV
566 rational[0] = g_variant_new_uint64(p);
567 rational[1] = g_variant_new_uint64(q);
568 src->data = g_variant_new_tuple(rational, 2);
569 break;
570 case SR_T_RATIONAL_VOLT:
571 if ((ret = sr_parse_voltage(value, &p, &q)) != SR_OK)
2be182e6 572 break;
ad54a1a6
BV
573 rational[0] = g_variant_new_uint64(p);
574 rational[1] = g_variant_new_uint64(q);
575 src->data = g_variant_new_tuple(rational, 2);
576 break;
577 case SR_T_UINT64_RANGE:
578 if (sscanf(value, "%"PRIu64"-%"PRIu64, &low, &high) != 2) {
579 ret = -1;
2be182e6 580 break;
ad54a1a6
BV
581 } else {
582 range[0] = g_variant_new_uint64(low);
583 range[1] = g_variant_new_uint64(high);
584 src->data = g_variant_new_tuple(range, 2);
2be182e6 585 }
ad54a1a6 586 break;
426d0cda
BV
587 case SR_T_DOUBLE_RANGE:
588 if (sscanf(value, "%lf-%lf", &dlow, &dhigh) != 2) {
589 ret = -1;
590 break;
591 } else {
592 range[0] = g_variant_new_double(dlow);
593 range[1] = g_variant_new_double(dhigh);
594 src->data = g_variant_new_tuple(range, 2);
595 }
596 break;
e4588937
BG
597 case SR_T_KEYVALUE:
598 /* Expects the argument to be in the form of key=value. */
599 keyval = g_strsplit(value, "=", 2);
600 if (!keyval[0] || !keyval[1]) {
601 g_strfreev(keyval);
602 ret = -1;
603 break;
604 } else {
605 vbl = g_variant_builder_new(G_VARIANT_TYPE_DICTIONARY);
606 g_variant_builder_add(vbl, "{ss}",
607 keyval[0], keyval[1]);
608 src->data = g_variant_builder_end(vbl);
609 g_strfreev(keyval);
610 }
611 break;
84b39e3e 612 case SR_T_MQ:
e9505599
BV
613 /*
614 Argument is MQ id e.g. ("voltage") optionally followed by one
615 or more /<mqflag> e.g. "/ac".
616 */
617 keyval = g_strsplit(value, "/", 0);
618 if (!keyval[0] || !(srmqi = sr_key_info_name_get(SR_KEY_MQ, keyval[0]))) {
619 g_strfreev(keyval);
620 ret = -1;
621 break;
622 }
623 mq = srmqi->key;
624 mqflags = 0;
625 for (i = 1; keyval[i]; i++) {
626 if (!(srmqi = sr_key_info_name_get(SR_KEY_MQFLAGS, keyval[i]))) {
627 ret = -1;
628 break;
629 }
630 mqflags |= srmqi->key;
631 }
632 g_strfreev(keyval);
633 if (ret != -1) {
e9505599
BV
634 gtup[0] = g_variant_new_uint32(mq);
635 gtup[1] = g_variant_new_uint64(mqflags);
84b39e3e 636 src->data = g_variant_new_tuple(gtup, 2);
e9505599
BV
637 }
638 break;
ad54a1a6 639 default:
b262d8f2
BG
640 g_critical("Unknown data type specified for option '%s' "
641 "(driver implementation bug?).", key);
ad54a1a6
BV
642 ret = -1;
643 }
644
27d310f0
BG
645 if (ret < 0)
646 g_critical("Invalid value: '%s' for option '%s'", value, key);
647
ad54a1a6
BV
648 return ret;
649}
650
651int set_dev_options(struct sr_dev_inst *sdi, GHashTable *args)
652{
653 struct sr_config src;
ca50f4b3 654 struct sr_channel_group *cg;
ad54a1a6
BV
655 GHashTableIter iter;
656 gpointer key, value;
657 int ret;
658
659 g_hash_table_iter_init(&iter, args);
660 while (g_hash_table_iter_next(&iter, &key, &value)) {
661 if ((ret = opt_to_gvar(key, value, &src)) != 0)
662 return ret;
ca50f4b3 663 cg = select_channel_group(sdi);
24bd9719
BV
664 if ((ret = maybe_config_set(sr_dev_inst_driver_get(sdi), sdi, cg,
665 src.key, src.data)) != SR_OK) {
28b0b84e
BG
666 g_critical("Failed to set device option '%s': %s.",
667 (char *)key, sr_strerror(ret));
2be182e6
BV
668 return ret;
669 }
670 }
671
672 return SR_OK;
673}
674
675void run_session(void)
676{
1c950633 677 struct df_arg_desc df_arg;
ac3dcd3e 678 GSList *devices, *real_devices, *sd;
2be182e6
BV
679 GHashTable *devargs;
680 GVariant *gvar;
4bf77ec6
BV
681 struct sr_session *session;
682 struct sr_trigger *trigger;
2be182e6 683 struct sr_dev_inst *sdi;
02c65935 684 uint64_t min_samples, max_samples;
35e8578e 685 GArray *drv_opts;
c7639c1d 686 guint i;
ac3dcd3e 687 int is_demo_dev;
c6fa2b2e 688 struct sr_dev_driver *driver;
fb995521 689 const struct sr_transform *t;
fd65ec4c 690 GMainLoop *main_loop;
2be182e6 691
1c950633
GS
692 memset(&df_arg, 0, sizeof(df_arg));
693 df_arg.do_props = FALSE;
694
2be182e6
BV
695 devices = device_scan();
696 if (!devices) {
697 g_critical("No devices found.");
698 return;
699 }
ac3dcd3e
PZ
700
701 real_devices = NULL;
702 for (sd = devices; sd; sd = sd->next) {
703 sdi = sd->data;
704
c6fa2b2e
UH
705 driver = sr_dev_inst_driver_get(sdi);
706
35e8578e
GS
707 if (!(drv_opts = sr_dev_options(driver, NULL, NULL))) {
708 g_critical("Failed to query list of driver options.");
ac3dcd3e
PZ
709 return;
710 }
711
ac3dcd3e 712 is_demo_dev = 0;
35e8578e
GS
713 for (i = 0; i < drv_opts->len; i++) {
714 if (g_array_index(drv_opts, uint32_t, i) == SR_CONF_DEMO_DEV)
ac3dcd3e
PZ
715 is_demo_dev = 1;
716 }
717
35e8578e 718 g_array_free(drv_opts, TRUE);
ac3dcd3e
PZ
719
720 if (!is_demo_dev)
721 real_devices = g_slist_append(real_devices, sdi);
722 }
723
2be182e6 724 if (g_slist_length(devices) > 1) {
ac3dcd3e
PZ
725 if (g_slist_length(real_devices) != 1) {
726 g_critical("sigrok-cli only supports one device for capturing.");
727 return;
728 } else {
729 /* We only have one non-demo device. */
730 g_slist_free(devices);
731 devices = real_devices;
732 real_devices = NULL;
733 }
2be182e6 734 }
ac3dcd3e 735
690617b8
AA
736 /* This is unlikely to happen but it makes static analyzers stop complaining. */
737 if (!devices) {
738 g_critical("No real devices found.");
739 return;
740 }
741
2be182e6 742 sdi = devices->data;
b4eece7c 743 g_slist_free(devices);
ac3dcd3e 744 g_slist_free(real_devices);
2be182e6 745
3d24ca2d 746 sr_session_new(sr_ctx, &session);
1c950633
GS
747 df_arg.session = session;
748 sr_session_datafeed_callback_add(session, datafeed_in, &df_arg);
749 df_arg.session = NULL;
2be182e6
BV
750
751 if (sr_dev_open(sdi) != SR_OK) {
752 g_critical("Failed to open device.");
753 return;
754 }
755
4bf77ec6 756 if (sr_session_dev_add(session, sdi) != SR_OK) {
2be182e6 757 g_critical("Failed to add device to session.");
4bf77ec6 758 sr_session_destroy(session);
2be182e6
BV
759 return;
760 }
761
762 if (opt_config) {
763 if ((devargs = parse_generic_arg(opt_config, FALSE))) {
764 if (set_dev_options(sdi, devargs) != SR_OK)
765 return;
766 g_hash_table_destroy(devargs);
767 }
768 }
769
029d73fe
UH
770 if (select_channels(sdi) != SR_OK) {
771 g_critical("Failed to set channels.");
4bf77ec6 772 sr_session_destroy(session);
2be182e6
BV
773 return;
774 }
775
15a14bff 776 trigger = NULL;
2be182e6 777 if (opt_triggers) {
4bf77ec6
BV
778 if (!parse_triggerstring(sdi, opt_triggers, &trigger)) {
779 sr_session_destroy(session);
780 return;
781 }
782 if (sr_session_trigger_set(session, trigger) != SR_OK) {
783 sr_session_destroy(session);
2be182e6
BV
784 return;
785 }
2be182e6
BV
786 }
787
788 if (opt_continuous) {
789 if (!sr_dev_has_option(sdi, SR_CONF_CONTINUOUS)) {
790 g_critical("This device does not support continuous sampling.");
4bf77ec6 791 sr_session_destroy(session);
2be182e6
BV
792 return;
793 }
794 }
795
796 if (opt_time) {
797 if (set_limit_time(sdi) != SR_OK) {
4bf77ec6 798 sr_session_destroy(session);
2be182e6
BV
799 return;
800 }
801 }
802
803 if (opt_samples) {
804 if ((sr_parse_sizestring(opt_samples, &limit_samples) != SR_OK)) {
805 g_critical("Invalid sample limit '%s'.", opt_samples);
4bf77ec6 806 sr_session_destroy(session);
2be182e6
BV
807 return;
808 }
24bd9719
BV
809 if (maybe_config_list(driver, sdi, NULL, SR_CONF_LIMIT_SAMPLES,
810 &gvar) == SR_OK) {
f0f54487
UH
811 /*
812 * The device has no compression, or compression is turned
813 * off, and publishes its sample memory size.
814 */
02c65935 815 g_variant_get(gvar, "(tt)", &min_samples, &max_samples);
be781321 816 g_variant_unref(gvar);
02c65935
BV
817 if (limit_samples < min_samples) {
818 g_critical("The device stores at least %"PRIu64
819 " samples with the current settings.", min_samples);
820 }
c7a5cb12
BV
821 if (limit_samples > max_samples) {
822 g_critical("The device can store only %"PRIu64
823 " samples with the current settings.", max_samples);
824 }
825 }
2be182e6 826 gvar = g_variant_new_uint64(limit_samples);
24bd9719 827 if (maybe_config_set(sr_dev_inst_driver_get(sdi), sdi, NULL, SR_CONF_LIMIT_SAMPLES, gvar) != SR_OK) {
2be182e6 828 g_critical("Failed to configure sample limit.");
4bf77ec6 829 sr_session_destroy(session);
2be182e6
BV
830 return;
831 }
832 }
833
834 if (opt_frames) {
835 if ((sr_parse_sizestring(opt_frames, &limit_frames) != SR_OK)) {
230e607b 836 g_critical("Invalid frame limit '%s'.", opt_frames);
4bf77ec6 837 sr_session_destroy(session);
2be182e6
BV
838 return;
839 }
840 gvar = g_variant_new_uint64(limit_frames);
24bd9719 841 if (maybe_config_set(sr_dev_inst_driver_get(sdi), sdi, NULL, SR_CONF_LIMIT_FRAMES, gvar) != SR_OK) {
2be182e6 842 g_critical("Failed to configure frame limit.");
4bf77ec6 843 sr_session_destroy(session);
2be182e6
BV
844 return;
845 }
846 }
847
6af207e6
UH
848 if (opt_transform_module) {
849 if (!(t = setup_transform_module(sdi)))
850 g_critical("Failed to initialize transform module.");
851 }
fb995521 852
fd65ec4c
DE
853 main_loop = g_main_loop_new(NULL, FALSE);
854
855 sr_session_stopped_callback_set(session,
856 (sr_session_stopped_callback)g_main_loop_quit, main_loop);
857
4bf77ec6 858 if (sr_session_start(session) != SR_OK) {
2be182e6 859 g_critical("Failed to start session.");
fd65ec4c 860 g_main_loop_unref(main_loop);
4bf77ec6 861 sr_session_destroy(session);
2be182e6
BV
862 return;
863 }
864
865 if (opt_continuous)
4bf77ec6 866 add_anykey(session);
2be182e6 867
fd65ec4c 868 g_main_loop_run(main_loop);
2be182e6
BV
869
870 if (opt_continuous)
871 clear_anykey();
872
15a14bff
BV
873 if (trigger)
874 sr_trigger_free(trigger);
875
4bf77ec6 876 sr_session_datafeed_callback_remove_all(session);
fd65ec4c 877 g_main_loop_unref(main_loop);
4bf77ec6 878 sr_session_destroy(session);
2be182e6 879}