]> sigrok.org Git - sigrok-cli.git/blame - session.c
Use new sr_key_info instead of sr_config_info.
[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
2be182e6
BV
20#include <glib.h>
21#include <glib/gstdio.h>
55de58a2
UH
22#include <string.h>
23#include <stdlib.h>
662a1e27 24#include "sigrok-cli.h"
2be182e6 25
2be182e6
BV
26static uint64_t limit_samples = 0;
27static uint64_t limit_frames = 0;
28
2be182e6
BV
29#ifdef HAVE_SRD
30extern struct srd_session *srd_sess;
31#endif
32
2be182e6
BV
33static int set_limit_time(const struct sr_dev_inst *sdi)
34{
35 GVariant *gvar;
36 uint64_t time_msec;
37 uint64_t samplerate;
c6fa2b2e
UH
38 struct sr_dev_driver *driver;
39
40 driver = sr_dev_inst_driver_get(sdi);
2be182e6
BV
41
42 if (!(time_msec = sr_parse_timestring(opt_time))) {
43 g_critical("Invalid time '%s'", opt_time);
44 return SR_ERR;
45 }
46
24bd9719 47 if (config_key_has_cap(driver, sdi, NULL, SR_CONF_LIMIT_MSEC, SR_CONF_SET)) {
2be182e6
BV
48 gvar = g_variant_new_uint64(time_msec);
49 if (sr_config_set(sdi, NULL, SR_CONF_LIMIT_MSEC, gvar) != SR_OK) {
50 g_critical("Failed to configure time limit.");
51 return SR_ERR;
52 }
24bd9719
BV
53 } else if (config_key_has_cap(driver, sdi, NULL, SR_CONF_SAMPLERATE,
54 SR_CONF_GET | SR_CONF_SET)) {
23c40b60 55 /* Convert to samples based on the samplerate. */
c6fa2b2e 56 sr_config_get(driver, sdi, NULL, SR_CONF_SAMPLERATE, &gvar);
2be182e6
BV
57 samplerate = g_variant_get_uint64(gvar);
58 g_variant_unref(gvar);
59 limit_samples = (samplerate) * time_msec / (uint64_t)1000;
60 if (limit_samples == 0) {
61 g_critical("Not enough time at this samplerate.");
62 return SR_ERR;
63 }
64 gvar = g_variant_new_uint64(limit_samples);
65 if (sr_config_set(sdi, NULL, SR_CONF_LIMIT_SAMPLES, gvar) != SR_OK) {
66 g_critical("Failed to configure time-based sample limit.");
67 return SR_ERR;
68 }
69 } else {
70 g_critical("This device does not support time limits.");
71 return SR_ERR;
72 }
73
74 return SR_OK;
75}
76
cbbea087 77const struct sr_output *setup_output_format(const struct sr_dev_inst *sdi, FILE **outfile)
2be182e6 78{
ad6520c4 79 const struct sr_output_module *omod;
7c6a0420 80 const struct sr_option **options;
ad6520c4
BV
81 const struct sr_output *o;
82 GHashTable *fmtargs, *fmtopts;
2be182e6
BV
83 char *fmtspec;
84
2be182e6 85 if (!opt_output_format) {
fea5acab 86 if (opt_output_file) {
5d7604d1 87 opt_output_format = DEFAULT_OUTPUT_FORMAT_FILE;
fea5acab 88 } else {
5d7604d1 89 opt_output_format = DEFAULT_OUTPUT_FORMAT_NOFILE;
fea5acab 90 }
2be182e6
BV
91 }
92
93 fmtargs = parse_generic_arg(opt_output_format, TRUE);
94 fmtspec = g_hash_table_lookup(fmtargs, "sigrok_key");
6709a694 95 if (!fmtspec)
2be182e6 96 g_critical("Invalid output format.");
ad6520c4 97 if (!(omod = sr_output_find(fmtspec)))
7c6a0420 98 g_critical("Unknown output module '%s'.", fmtspec);
9216694f 99 g_hash_table_remove(fmtargs, "sigrok_key");
7c6a0420
BV
100 if ((options = sr_output_options_get(omod))) {
101 fmtopts = generic_arg_to_opt(options, fmtargs);
102 sr_output_options_free(options);
ad6520c4
BV
103 } else
104 fmtopts = NULL;
5d7604d1
SA
105 o = sr_output_new(omod, fmtopts, sdi, opt_output_file);
106
cbbea087
SA
107 if (opt_output_file) {
108 if (!sr_output_test_flag(omod, SR_OUTPUT_INTERNAL_IO_HANDLING))
109 *outfile = g_fopen(opt_output_file, "wb");
110 else
111 *outfile = NULL;
112 } else {
113 *outfile = stdout;
114 }
115
ad6520c4
BV
116 if (fmtopts)
117 g_hash_table_destroy(fmtopts);
2be182e6
BV
118 g_hash_table_destroy(fmtargs);
119
6709a694 120 return o;
2be182e6
BV
121}
122
fb995521
UH
123const struct sr_transform *setup_transform_module(const struct sr_dev_inst *sdi)
124{
125 const struct sr_transform_module *tmod;
126 const struct sr_option **options;
127 const struct sr_transform *t;
128 GHashTable *fmtargs, *fmtopts;
fb995521
UH
129 char *fmtspec;
130
131 if (!opt_transform_module)
132 opt_transform_module = "nop";
133
134 fmtargs = parse_generic_arg(opt_transform_module, TRUE);
135 fmtspec = g_hash_table_lookup(fmtargs, "sigrok_key");
136 if (!fmtspec)
137 g_critical("Invalid transform module.");
138 if (!(tmod = sr_transform_find(fmtspec)))
139 g_critical("Unknown transform module '%s'.", fmtspec);
140 g_hash_table_remove(fmtargs, "sigrok_key");
141 if ((options = sr_transform_options_get(tmod))) {
142 fmtopts = generic_arg_to_opt(options, fmtargs);
143 sr_transform_options_free(options);
144 } else
145 fmtopts = NULL;
146 t = sr_transform_new(tmod, fmtopts, sdi);
147 if (fmtopts)
148 g_hash_table_destroy(fmtopts);
149 g_hash_table_destroy(fmtargs);
150
151 return t;
152}
153
2be182e6
BV
154void datafeed_in(const struct sr_dev_inst *sdi,
155 const struct sr_datafeed_packet *packet, void *cb_data)
156{
157 const struct sr_datafeed_meta *meta;
158 const struct sr_datafeed_logic *logic;
159 const struct sr_datafeed_analog *analog;
4bf77ec6 160 struct sr_session *session;
2be182e6 161 struct sr_config *src;
ad6520c4
BV
162 static const struct sr_output *o = NULL;
163 static const struct sr_output *oa = NULL;
6ea663a7
BV
164 static uint64_t rcvd_samples_logic = 0;
165 static uint64_t rcvd_samples_analog = 0;
166 static uint64_t samplerate = 0;
2be182e6
BV
167 static int triggered = 0;
168 static FILE *outfile = NULL;
fea5acab 169 GSList *l;
2be182e6 170 GString *out;
8b65cdcc 171 GVariant *gvar;
55de58a2 172 uint64_t end_sample;
667d4a18 173 uint64_t input_len;
c6fa2b2e
UH
174 struct sr_dev_driver *driver;
175
176 driver = sr_dev_inst_driver_get(sdi);
2be182e6 177
2be182e6 178 /* If the first packet to come in isn't a header, don't even try. */
23c40b60 179 if (packet->type != SR_DF_HEADER && !o)
2be182e6
BV
180 return;
181
4bf77ec6 182 session = cb_data;
2be182e6
BV
183 switch (packet->type) {
184 case SR_DF_HEADER:
4ed1cdea 185 g_debug("cli: Received SR_DF_HEADER.");
cbbea087 186 if (!(o = setup_output_format(sdi, &outfile)))
e786e625 187 g_critical("Failed to initialize output module.");
2be182e6 188
9216694f 189 /* Set up backup analog output module. */
5d7604d1 190 oa = sr_output_new(sr_output_find("analog"), NULL, sdi, NULL);
9216694f 191
6ea663a7 192 rcvd_samples_logic = rcvd_samples_analog = 0;
2be182e6 193
24bd9719 194 if (maybe_config_get(driver, sdi, NULL, SR_CONF_SAMPLERATE,
8b65cdcc
BV
195 &gvar) == SR_OK) {
196 samplerate = g_variant_get_uint64(gvar);
197 g_variant_unref(gvar);
198 }
199
2be182e6 200#ifdef HAVE_SRD
ea6d6dec 201 if (opt_pds) {
8b65cdcc 202 if (samplerate) {
2be182e6
BV
203 if (srd_session_metadata_set(srd_sess, SRD_CONF_SAMPLERATE,
204 g_variant_new_uint64(samplerate)) != SRD_OK) {
205 g_critical("Failed to configure decode session.");
206 break;
207 }
208 }
209 if (srd_session_start(srd_sess) != SRD_OK) {
210 g_critical("Failed to start decode session.");
211 break;
212 }
213 }
214#endif
215 break;
216
217 case SR_DF_META:
4ed1cdea 218 g_debug("cli: Received SR_DF_META.");
2be182e6
BV
219 meta = packet->payload;
220 for (l = meta->config; l; l = l->next) {
221 src = l->data;
222 switch (src->key) {
223 case SR_CONF_SAMPLERATE:
224 samplerate = g_variant_get_uint64(src->data);
4ed1cdea 225 g_debug("cli: Got samplerate %"PRIu64" Hz.", samplerate);
2be182e6
BV
226#ifdef HAVE_SRD
227 if (opt_pds) {
228 if (srd_session_metadata_set(srd_sess, SRD_CONF_SAMPLERATE,
229 g_variant_new_uint64(samplerate)) != SRD_OK) {
230 g_critical("Failed to pass samplerate to decoder.");
231 }
232 }
233#endif
234 break;
235 case SR_CONF_SAMPLE_INTERVAL:
236 samplerate = g_variant_get_uint64(src->data);
4ed1cdea 237 g_debug("cli: Got sample interval %"PRIu64" ms.", samplerate);
2be182e6
BV
238 break;
239 default:
240 /* Unknown metadata is not an error. */
241 break;
242 }
243 }
244 break;
245
246 case SR_DF_TRIGGER:
4ed1cdea 247 g_debug("cli: Received SR_DF_TRIGGER.");
2be182e6
BV
248 triggered = 1;
249 break;
250
251 case SR_DF_LOGIC:
252 logic = packet->payload;
4ed1cdea
UH
253 g_message("cli: Received SR_DF_LOGIC (%"PRIu64" bytes, unitsize = %d).",
254 logic->length, logic->unitsize);
2be182e6
BV
255 if (logic->length == 0)
256 break;
257
258 /* Don't store any samples until triggered. */
259 if (opt_wait_trigger && !triggered)
260 break;
261
6ea663a7 262 if (limit_samples && rcvd_samples_logic >= limit_samples)
2be182e6
BV
263 break;
264
6ea663a7 265 end_sample = rcvd_samples_logic + logic->length / logic->unitsize;
8667d3c2
DE
266 /* Cut off last packet according to the sample limit. */
267 if (limit_samples && end_sample > limit_samples)
268 end_sample = limit_samples;
6ea663a7 269 input_len = (end_sample - rcvd_samples_logic) * logic->unitsize;
2be182e6 270
fea5acab 271 if (opt_pds) {
2be182e6 272#ifdef HAVE_SRD
fea5acab 273 if (srd_session_send(srd_sess, rcvd_samples_logic, end_sample,
ee639fb4 274 logic->data, input_len, logic->unitsize) != SRD_OK)
fea5acab 275 sr_session_stop(session);
2be182e6 276#endif
2be182e6 277 }
2be182e6 278
6ea663a7 279 rcvd_samples_logic = end_sample;
2be182e6
BV
280 break;
281
282 case SR_DF_ANALOG:
283 analog = packet->payload;
4ed1cdea 284 g_message("cli: Received SR_DF_ANALOG (%d samples).", analog->num_samples);
2be182e6
BV
285 if (analog->num_samples == 0)
286 break;
287
6ea663a7 288 if (limit_samples && rcvd_samples_analog >= limit_samples)
2be182e6
BV
289 break;
290
6ea663a7 291 rcvd_samples_analog += analog->num_samples;
2be182e6
BV
292 break;
293
294 case SR_DF_FRAME_BEGIN:
4ed1cdea 295 g_debug("cli: Received SR_DF_FRAME_BEGIN.");
2be182e6
BV
296 break;
297
298 case SR_DF_FRAME_END:
4ed1cdea 299 g_debug("cli: Received SR_DF_FRAME_END.");
2be182e6
BV
300 break;
301
302 default:
303 break;
304 }
305
cbbea087 306 if (o && !opt_pds) {
9216694f 307 if (sr_output_send(o, packet, &out) == SR_OK) {
fea5acab
BV
308 if (!out || (out->len == 0
309 && !opt_output_format
9216694f 310 && packet->type == SR_DF_ANALOG)) {
f0f54487
UH
311 /*
312 * The user didn't specify an output module,
313 * but needs to see this analog data.
314 */
9216694f
BV
315 sr_output_send(oa, packet, &out);
316 }
cbbea087 317 if (outfile && out && out->len > 0) {
9216694f
BV
318 fwrite(out->str, 1, out->len, outfile);
319 fflush(outfile);
320 }
321 if (out)
322 g_string_free(out, TRUE);
2be182e6
BV
323 }
324 }
325
f0f54487
UH
326 /*
327 * SR_DF_END needs to be handled after the output module's receive()
328 * is called, so it can properly clean up that module.
329 */
2be182e6 330 if (packet->type == SR_DF_END) {
4ed1cdea 331 g_debug("cli: Received SR_DF_END.");
2be182e6 332
5d7604d1 333 if (o)
6709a694 334 sr_output_free(o);
2be182e6
BV
335 o = NULL;
336
9216694f
BV
337 sr_output_free(oa);
338 oa = NULL;
339
2be182e6
BV
340 if (outfile && outfile != stdout)
341 fclose(outfile);
342
6ea663a7
BV
343 if (limit_samples) {
344 if (rcvd_samples_logic > 0 && rcvd_samples_logic < limit_samples)
345 g_warning("Device only sent %" PRIu64 " samples.",
346 rcvd_samples_logic);
347 else if (rcvd_samples_analog > 0 && rcvd_samples_analog < limit_samples)
348 g_warning("Device only sent %" PRIu64 " samples.",
349 rcvd_samples_analog);
2be182e6
BV
350 }
351 }
352
353}
354
ad54a1a6 355int opt_to_gvar(char *key, char *value, struct sr_config *src)
2be182e6 356{
e4e4b472 357 const struct sr_key_info *srci;
426d0cda 358 double tmp_double, dlow, dhigh;
2be182e6 359 uint64_t tmp_u64, p, q, low, high;
ad54a1a6 360 GVariant *rational[2], *range[2];
e4588937 361 GVariantBuilder *vbl;
2be182e6 362 gboolean tmp_bool;
e4588937 363 gchar **keyval;
ad54a1a6 364 int ret;
2be182e6 365
e4e4b472 366 if (!(srci = sr_key_info_name_get(SR_KEY_CONFIG, key))) {
ad54a1a6
BV
367 g_critical("Unknown device option '%s'.", (char *) key);
368 return -1;
369 }
370 src->key = srci->key;
2be182e6 371
23c40b60 372 if ((!value || strlen(value) == 0) &&
ad54a1a6
BV
373 (srci->datatype != SR_T_BOOL)) {
374 g_critical("Option '%s' needs a value.", (char *)key);
375 return -1;
376 }
377
378 ret = 0;
379 switch (srci->datatype) {
380 case SR_T_UINT64:
381 ret = sr_parse_sizestring(value, &tmp_u64);
382 if (ret != 0)
2be182e6 383 break;
ad54a1a6
BV
384 src->data = g_variant_new_uint64(tmp_u64);
385 break;
386 case SR_T_INT32:
387 ret = sr_parse_sizestring(value, &tmp_u64);
388 if (ret != 0)
2be182e6 389 break;
ad54a1a6
BV
390 src->data = g_variant_new_int32(tmp_u64);
391 break;
9db40e9f 392 case SR_T_STRING:
ad54a1a6
BV
393 src->data = g_variant_new_string(value);
394 break;
395 case SR_T_BOOL:
396 if (!value)
397 tmp_bool = TRUE;
398 else
399 tmp_bool = sr_parse_boolstring(value);
400 src->data = g_variant_new_boolean(tmp_bool);
401 break;
402 case SR_T_FLOAT:
403 tmp_double = strtof(value, NULL);
404 src->data = g_variant_new_double(tmp_double);
405 break;
406 case SR_T_RATIONAL_PERIOD:
407 if ((ret = sr_parse_period(value, &p, &q)) != SR_OK)
2be182e6 408 break;
ad54a1a6
BV
409 rational[0] = g_variant_new_uint64(p);
410 rational[1] = g_variant_new_uint64(q);
411 src->data = g_variant_new_tuple(rational, 2);
412 break;
413 case SR_T_RATIONAL_VOLT:
414 if ((ret = sr_parse_voltage(value, &p, &q)) != SR_OK)
2be182e6 415 break;
ad54a1a6
BV
416 rational[0] = g_variant_new_uint64(p);
417 rational[1] = g_variant_new_uint64(q);
418 src->data = g_variant_new_tuple(rational, 2);
419 break;
420 case SR_T_UINT64_RANGE:
421 if (sscanf(value, "%"PRIu64"-%"PRIu64, &low, &high) != 2) {
422 ret = -1;
2be182e6 423 break;
ad54a1a6
BV
424 } else {
425 range[0] = g_variant_new_uint64(low);
426 range[1] = g_variant_new_uint64(high);
427 src->data = g_variant_new_tuple(range, 2);
2be182e6 428 }
ad54a1a6 429 break;
426d0cda
BV
430 case SR_T_DOUBLE_RANGE:
431 if (sscanf(value, "%lf-%lf", &dlow, &dhigh) != 2) {
432 ret = -1;
433 break;
434 } else {
435 range[0] = g_variant_new_double(dlow);
436 range[1] = g_variant_new_double(dhigh);
437 src->data = g_variant_new_tuple(range, 2);
438 }
439 break;
e4588937
BG
440 case SR_T_KEYVALUE:
441 /* Expects the argument to be in the form of key=value. */
442 keyval = g_strsplit(value, "=", 2);
443 if (!keyval[0] || !keyval[1]) {
444 g_strfreev(keyval);
445 ret = -1;
446 break;
447 } else {
448 vbl = g_variant_builder_new(G_VARIANT_TYPE_DICTIONARY);
449 g_variant_builder_add(vbl, "{ss}",
450 keyval[0], keyval[1]);
451 src->data = g_variant_builder_end(vbl);
452 g_strfreev(keyval);
453 }
454 break;
ad54a1a6 455 default:
b262d8f2
BG
456 g_critical("Unknown data type specified for option '%s' "
457 "(driver implementation bug?).", key);
ad54a1a6
BV
458 ret = -1;
459 }
460
27d310f0
BG
461 if (ret < 0)
462 g_critical("Invalid value: '%s' for option '%s'", value, key);
463
ad54a1a6
BV
464 return ret;
465}
466
467int set_dev_options(struct sr_dev_inst *sdi, GHashTable *args)
468{
469 struct sr_config src;
ca50f4b3 470 struct sr_channel_group *cg;
ad54a1a6
BV
471 GHashTableIter iter;
472 gpointer key, value;
473 int ret;
474
475 g_hash_table_iter_init(&iter, args);
476 while (g_hash_table_iter_next(&iter, &key, &value)) {
477 if ((ret = opt_to_gvar(key, value, &src)) != 0)
478 return ret;
ca50f4b3 479 cg = select_channel_group(sdi);
24bd9719
BV
480 if ((ret = maybe_config_set(sr_dev_inst_driver_get(sdi), sdi, cg,
481 src.key, src.data)) != SR_OK) {
28b0b84e
BG
482 g_critical("Failed to set device option '%s': %s.",
483 (char *)key, sr_strerror(ret));
2be182e6
BV
484 return ret;
485 }
486 }
487
488 return SR_OK;
489}
490
491void run_session(void)
492{
ac3dcd3e 493 GSList *devices, *real_devices, *sd;
2be182e6
BV
494 GHashTable *devargs;
495 GVariant *gvar;
4bf77ec6
BV
496 struct sr_session *session;
497 struct sr_trigger *trigger;
2be182e6 498 struct sr_dev_inst *sdi;
02c65935 499 uint64_t min_samples, max_samples;
ac3dcd3e
PZ
500 gsize n_elements, i;
501 const uint32_t *dev_opts;
502 int is_demo_dev;
c6fa2b2e 503 struct sr_dev_driver *driver;
fb995521 504 const struct sr_transform *t;
2be182e6
BV
505
506 devices = device_scan();
507 if (!devices) {
508 g_critical("No devices found.");
509 return;
510 }
ac3dcd3e
PZ
511
512 real_devices = NULL;
513 for (sd = devices; sd; sd = sd->next) {
514 sdi = sd->data;
515
c6fa2b2e
UH
516 driver = sr_dev_inst_driver_get(sdi);
517
518 if (sr_config_list(driver, sdi, NULL, SR_CONF_DEVICE_OPTIONS, &gvar) != SR_OK) {
24bd9719 519 g_critical("Failed to query list device options.");
ac3dcd3e
PZ
520 return;
521 }
522
523 dev_opts = g_variant_get_fixed_array(gvar, &n_elements, sizeof(uint32_t));
524
525 is_demo_dev = 0;
526 for (i = 0; i < n_elements; i++) {
527 if (dev_opts[i] == SR_CONF_DEMO_DEV)
528 is_demo_dev = 1;
529 }
530
531 g_variant_unref(gvar);
532
533 if (!is_demo_dev)
534 real_devices = g_slist_append(real_devices, sdi);
535 }
536
2be182e6 537 if (g_slist_length(devices) > 1) {
ac3dcd3e
PZ
538 if (g_slist_length(real_devices) != 1) {
539 g_critical("sigrok-cli only supports one device for capturing.");
540 return;
541 } else {
542 /* We only have one non-demo device. */
543 g_slist_free(devices);
544 devices = real_devices;
545 real_devices = NULL;
546 }
2be182e6 547 }
ac3dcd3e 548
2be182e6 549 sdi = devices->data;
b4eece7c 550 g_slist_free(devices);
ac3dcd3e 551 g_slist_free(real_devices);
2be182e6 552
3d24ca2d 553 sr_session_new(sr_ctx, &session);
4bf77ec6 554 sr_session_datafeed_callback_add(session, datafeed_in, NULL);
2be182e6
BV
555
556 if (sr_dev_open(sdi) != SR_OK) {
557 g_critical("Failed to open device.");
558 return;
559 }
560
4bf77ec6 561 if (sr_session_dev_add(session, sdi) != SR_OK) {
2be182e6 562 g_critical("Failed to add device to session.");
4bf77ec6 563 sr_session_destroy(session);
2be182e6
BV
564 return;
565 }
566
567 if (opt_config) {
568 if ((devargs = parse_generic_arg(opt_config, FALSE))) {
569 if (set_dev_options(sdi, devargs) != SR_OK)
570 return;
571 g_hash_table_destroy(devargs);
572 }
573 }
574
029d73fe
UH
575 if (select_channels(sdi) != SR_OK) {
576 g_critical("Failed to set channels.");
4bf77ec6 577 sr_session_destroy(session);
2be182e6
BV
578 return;
579 }
580
581 if (opt_triggers) {
4bf77ec6
BV
582 if (!parse_triggerstring(sdi, opt_triggers, &trigger)) {
583 sr_session_destroy(session);
584 return;
585 }
586 if (sr_session_trigger_set(session, trigger) != SR_OK) {
587 sr_session_destroy(session);
2be182e6
BV
588 return;
589 }
2be182e6
BV
590 }
591
592 if (opt_continuous) {
593 if (!sr_dev_has_option(sdi, SR_CONF_CONTINUOUS)) {
594 g_critical("This device does not support continuous sampling.");
4bf77ec6 595 sr_session_destroy(session);
2be182e6
BV
596 return;
597 }
598 }
599
600 if (opt_time) {
601 if (set_limit_time(sdi) != SR_OK) {
4bf77ec6 602 sr_session_destroy(session);
2be182e6
BV
603 return;
604 }
605 }
606
607 if (opt_samples) {
608 if ((sr_parse_sizestring(opt_samples, &limit_samples) != SR_OK)) {
609 g_critical("Invalid sample limit '%s'.", opt_samples);
4bf77ec6 610 sr_session_destroy(session);
2be182e6
BV
611 return;
612 }
24bd9719
BV
613 if (maybe_config_list(driver, sdi, NULL, SR_CONF_LIMIT_SAMPLES,
614 &gvar) == SR_OK) {
f0f54487
UH
615 /*
616 * The device has no compression, or compression is turned
617 * off, and publishes its sample memory size.
618 */
02c65935 619 g_variant_get(gvar, "(tt)", &min_samples, &max_samples);
be781321 620 g_variant_unref(gvar);
02c65935
BV
621 if (limit_samples < min_samples) {
622 g_critical("The device stores at least %"PRIu64
623 " samples with the current settings.", min_samples);
624 }
c7a5cb12
BV
625 if (limit_samples > max_samples) {
626 g_critical("The device can store only %"PRIu64
627 " samples with the current settings.", max_samples);
628 }
629 }
2be182e6 630 gvar = g_variant_new_uint64(limit_samples);
24bd9719 631 if (maybe_config_set(sr_dev_inst_driver_get(sdi), sdi, NULL, SR_CONF_LIMIT_SAMPLES, gvar) != SR_OK) {
2be182e6 632 g_critical("Failed to configure sample limit.");
4bf77ec6 633 sr_session_destroy(session);
2be182e6
BV
634 return;
635 }
636 }
637
638 if (opt_frames) {
639 if ((sr_parse_sizestring(opt_frames, &limit_frames) != SR_OK)) {
640 g_critical("Invalid sample limit '%s'.", opt_samples);
4bf77ec6 641 sr_session_destroy(session);
2be182e6
BV
642 return;
643 }
644 gvar = g_variant_new_uint64(limit_frames);
24bd9719 645 if (maybe_config_set(sr_dev_inst_driver_get(sdi), sdi, NULL, SR_CONF_LIMIT_FRAMES, gvar) != SR_OK) {
2be182e6 646 g_critical("Failed to configure frame limit.");
4bf77ec6 647 sr_session_destroy(session);
2be182e6
BV
648 return;
649 }
650 }
651
fb995521
UH
652 if (!(t = setup_transform_module(sdi)))
653 g_critical("Failed to initialize transform module.");
654
4bf77ec6 655 if (sr_session_start(session) != SR_OK) {
2be182e6 656 g_critical("Failed to start session.");
4bf77ec6 657 sr_session_destroy(session);
2be182e6
BV
658 return;
659 }
660
661 if (opt_continuous)
4bf77ec6 662 add_anykey(session);
2be182e6 663
4bf77ec6 664 sr_session_run(session);
2be182e6
BV
665
666 if (opt_continuous)
667 clear_anykey();
668
4bf77ec6
BV
669 sr_session_datafeed_callback_remove_all(session);
670 sr_session_destroy(session);
2be182e6
BV
671
672}