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