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