]> sigrok.org Git - sigrok-cli.git/blame_incremental - session.c
Pass unitsize to srd_session_send() directly.
[sigrok-cli.git] / session.c
... / ...
CommitLineData
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
20#include <glib.h>
21#include <glib/gstdio.h>
22#include <string.h>
23#include <stdlib.h>
24#include "sigrok-cli.h"
25
26static uint64_t limit_samples = 0;
27static uint64_t limit_frames = 0;
28
29#ifdef HAVE_SRD
30extern struct srd_session *srd_sess;
31#endif
32
33static int set_limit_time(const struct sr_dev_inst *sdi)
34{
35 GVariant *gvar;
36 uint64_t time_msec;
37 uint64_t samplerate;
38 struct sr_dev_driver *driver;
39
40 driver = sr_dev_inst_driver_get(sdi);
41
42 if (!(time_msec = sr_parse_timestring(opt_time))) {
43 g_critical("Invalid time '%s'", opt_time);
44 return SR_ERR;
45 }
46
47 if (config_key_has_cap(driver, sdi, NULL, SR_CONF_LIMIT_MSEC, SR_CONF_SET)) {
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 }
53 } else if (config_key_has_cap(driver, sdi, NULL, SR_CONF_SAMPLERATE,
54 SR_CONF_GET | SR_CONF_SET)) {
55 /* Convert to samples based on the samplerate. */
56 sr_config_get(driver, sdi, NULL, SR_CONF_SAMPLERATE, &gvar);
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
77const struct sr_output *setup_output_format(const struct sr_dev_inst *sdi)
78{
79 const struct sr_output_module *omod;
80 const struct sr_option **options;
81 const struct sr_output *o;
82 GHashTable *fmtargs, *fmtopts;
83 char *fmtspec;
84
85 if (!opt_output_format) {
86 if (opt_output_file) {
87 opt_output_format = DEFAULT_OUTPUT_FORMAT_FILE;
88 } else {
89 opt_output_format = DEFAULT_OUTPUT_FORMAT_NOFILE;
90 }
91 }
92
93 fmtargs = parse_generic_arg(opt_output_format, TRUE);
94 fmtspec = g_hash_table_lookup(fmtargs, "sigrok_key");
95 if (!fmtspec)
96 g_critical("Invalid output format.");
97 if (!(omod = sr_output_find(fmtspec)))
98 g_critical("Unknown output module '%s'.", fmtspec);
99 g_hash_table_remove(fmtargs, "sigrok_key");
100 if ((options = sr_output_options_get(omod))) {
101 fmtopts = generic_arg_to_opt(options, fmtargs);
102 sr_output_options_free(options);
103 } else
104 fmtopts = NULL;
105 o = sr_output_new(omod, fmtopts, sdi, opt_output_file);
106
107 if (fmtopts)
108 g_hash_table_destroy(fmtopts);
109 g_hash_table_destroy(fmtargs);
110
111 return o;
112}
113
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;
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
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;
151 struct sr_session *session;
152 struct sr_config *src;
153 static const struct sr_output *o = NULL;
154 static const struct sr_output *oa = NULL;
155 static uint64_t rcvd_samples_logic = 0;
156 static uint64_t rcvd_samples_analog = 0;
157 static uint64_t samplerate = 0;
158 static int triggered = 0;
159 static FILE *outfile = NULL;
160 GSList *l;
161 GString *out;
162 GVariant *gvar;
163 uint64_t end_sample;
164 uint64_t input_len;
165 struct sr_dev_driver *driver;
166
167 driver = sr_dev_inst_driver_get(sdi);
168
169 /* If the first packet to come in isn't a header, don't even try. */
170 if (packet->type != SR_DF_HEADER && !o)
171 return;
172
173 session = cb_data;
174 switch (packet->type) {
175 case SR_DF_HEADER:
176 g_debug("cli: Received SR_DF_HEADER.");
177 if (!(o = setup_output_format(sdi)))
178 g_critical("Failed to initialize output module.");
179
180 /* Set up backup analog output module. */
181 oa = sr_output_new(sr_output_find("analog"), NULL, sdi, NULL);
182
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 {
199 outfile = stdout;
200 }
201
202 rcvd_samples_logic = rcvd_samples_analog = 0;
203
204 if (maybe_config_get(driver, sdi, NULL, SR_CONF_SAMPLERATE,
205 &gvar) == SR_OK) {
206 samplerate = g_variant_get_uint64(gvar);
207 g_variant_unref(gvar);
208 }
209
210#ifdef HAVE_SRD
211 if (opt_pds) {
212 if (samplerate) {
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:
228 g_debug("cli: Received SR_DF_META.");
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);
235 g_debug("cli: Got samplerate %"PRIu64" Hz.", samplerate);
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);
247 g_debug("cli: Got sample interval %"PRIu64" ms.", samplerate);
248 break;
249 default:
250 /* Unknown metadata is not an error. */
251 break;
252 }
253 }
254 break;
255
256 case SR_DF_TRIGGER:
257 g_debug("cli: Received SR_DF_TRIGGER.");
258 triggered = 1;
259 break;
260
261 case SR_DF_LOGIC:
262 logic = packet->payload;
263 g_message("cli: Received SR_DF_LOGIC (%"PRIu64" bytes, unitsize = %d).",
264 logic->length, logic->unitsize);
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
272 if (limit_samples && rcvd_samples_logic >= limit_samples)
273 break;
274
275 end_sample = rcvd_samples_logic + logic->length / logic->unitsize;
276 /* Cut off last packet according to the sample limit. */
277 if (limit_samples && end_sample > limit_samples)
278 end_sample = limit_samples;
279 input_len = (end_sample - rcvd_samples_logic) * logic->unitsize;
280
281 if (opt_pds) {
282#ifdef HAVE_SRD
283 if (srd_session_send(srd_sess, rcvd_samples_logic, end_sample,
284 logic->data, input_len, logic->unitsize) != SRD_OK)
285 sr_session_stop(session);
286#endif
287 }
288
289 rcvd_samples_logic = end_sample;
290 break;
291
292 case SR_DF_ANALOG:
293 analog = packet->payload;
294 g_message("cli: Received SR_DF_ANALOG (%d samples).", analog->num_samples);
295 if (analog->num_samples == 0)
296 break;
297
298 if (limit_samples && rcvd_samples_analog >= limit_samples)
299 break;
300
301 rcvd_samples_analog += analog->num_samples;
302 break;
303
304 case SR_DF_FRAME_BEGIN:
305 g_debug("cli: Received SR_DF_FRAME_BEGIN.");
306 break;
307
308 case SR_DF_FRAME_END:
309 g_debug("cli: Received SR_DF_FRAME_END.");
310 break;
311
312 default:
313 break;
314 }
315
316 if (o && (outfile || g_str_has_prefix(opt_output_format, "srzip")) && !opt_pds) {
317 if (sr_output_send(o, packet, &out) == SR_OK) {
318 if (!out || (out->len == 0
319 && !opt_output_format
320 && packet->type == SR_DF_ANALOG)) {
321 /*
322 * The user didn't specify an output module,
323 * but needs to see this analog data.
324 */
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);
333 }
334 }
335
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 */
340 if (packet->type == SR_DF_END) {
341 g_debug("cli: Received SR_DF_END.");
342
343 if (o)
344 sr_output_free(o);
345 o = NULL;
346
347 sr_output_free(oa);
348 oa = NULL;
349
350 if (outfile && outfile != stdout)
351 fclose(outfile);
352
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);
360 }
361 }
362
363}
364
365int opt_to_gvar(char *key, char *value, struct sr_config *src)
366{
367 const struct sr_config_info *srci;
368 double tmp_double, dlow, dhigh;
369 uint64_t tmp_u64, p, q, low, high;
370 GVariant *rational[2], *range[2];
371 GVariantBuilder *vbl;
372 gboolean tmp_bool;
373 gchar **keyval;
374 int ret;
375
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;
381
382 if ((!value || strlen(value) == 0) &&
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)
393 break;
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)
399 break;
400 src->data = g_variant_new_int32(tmp_u64);
401 break;
402 case SR_T_STRING:
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)
418 break;
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)
425 break;
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;
433 break;
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);
438 }
439 break;
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;
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;
465 default:
466 g_critical("Unknown data type specified for option '%s' "
467 "(driver implementation bug?).", key);
468 ret = -1;
469 }
470
471 if (ret < 0)
472 g_critical("Invalid value: '%s' for option '%s'", value, key);
473
474 return ret;
475}
476
477int set_dev_options(struct sr_dev_inst *sdi, GHashTable *args)
478{
479 struct sr_config src;
480 struct sr_channel_group *cg;
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;
489 cg = select_channel_group(sdi);
490 if ((ret = maybe_config_set(sr_dev_inst_driver_get(sdi), sdi, cg,
491 src.key, src.data)) != SR_OK) {
492 g_critical("Failed to set device option '%s': %s.",
493 (char *)key, sr_strerror(ret));
494 return ret;
495 }
496 }
497
498 return SR_OK;
499}
500
501void run_session(void)
502{
503 GSList *devices, *real_devices, *sd;
504 GHashTable *devargs;
505 GVariant *gvar;
506 struct sr_session *session;
507 struct sr_trigger *trigger;
508 struct sr_dev_inst *sdi;
509 uint64_t min_samples, max_samples;
510 gsize n_elements, i;
511 const uint32_t *dev_opts;
512 int is_demo_dev;
513 struct sr_dev_driver *driver;
514 const struct sr_transform *t;
515
516 devices = device_scan();
517 if (!devices) {
518 g_critical("No devices found.");
519 return;
520 }
521
522 real_devices = NULL;
523 for (sd = devices; sd; sd = sd->next) {
524 sdi = sd->data;
525
526 driver = sr_dev_inst_driver_get(sdi);
527
528 if (sr_config_list(driver, sdi, NULL, SR_CONF_DEVICE_OPTIONS, &gvar) != SR_OK) {
529 g_critical("Failed to query list device options.");
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
547 if (g_slist_length(devices) > 1) {
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 }
557 }
558
559 sdi = devices->data;
560 g_slist_free(devices);
561 g_slist_free(real_devices);
562
563 sr_session_new(sr_ctx, &session);
564 sr_session_datafeed_callback_add(session, datafeed_in, NULL);
565
566 if (sr_dev_open(sdi) != SR_OK) {
567 g_critical("Failed to open device.");
568 return;
569 }
570
571 if (sr_session_dev_add(session, sdi) != SR_OK) {
572 g_critical("Failed to add device to session.");
573 sr_session_destroy(session);
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
585 if (select_channels(sdi) != SR_OK) {
586 g_critical("Failed to set channels.");
587 sr_session_destroy(session);
588 return;
589 }
590
591 if (opt_triggers) {
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);
598 return;
599 }
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.");
605 sr_session_destroy(session);
606 return;
607 }
608 }
609
610 if (opt_time) {
611 if (set_limit_time(sdi) != SR_OK) {
612 sr_session_destroy(session);
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);
620 sr_session_destroy(session);
621 return;
622 }
623 if (maybe_config_list(driver, sdi, NULL, SR_CONF_LIMIT_SAMPLES,
624 &gvar) == SR_OK) {
625 /*
626 * The device has no compression, or compression is turned
627 * off, and publishes its sample memory size.
628 */
629 g_variant_get(gvar, "(tt)", &min_samples, &max_samples);
630 g_variant_unref(gvar);
631 if (limit_samples < min_samples) {
632 g_critical("The device stores at least %"PRIu64
633 " samples with the current settings.", min_samples);
634 }
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 }
640 gvar = g_variant_new_uint64(limit_samples);
641 if (maybe_config_set(sr_dev_inst_driver_get(sdi), sdi, NULL, SR_CONF_LIMIT_SAMPLES, gvar) != SR_OK) {
642 g_critical("Failed to configure sample limit.");
643 sr_session_destroy(session);
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);
651 sr_session_destroy(session);
652 return;
653 }
654 gvar = g_variant_new_uint64(limit_frames);
655 if (maybe_config_set(sr_dev_inst_driver_get(sdi), sdi, NULL, SR_CONF_LIMIT_FRAMES, gvar) != SR_OK) {
656 g_critical("Failed to configure frame limit.");
657 sr_session_destroy(session);
658 return;
659 }
660 }
661
662 if (!(t = setup_transform_module(sdi)))
663 g_critical("Failed to initialize transform module.");
664
665 if (sr_session_start(session) != SR_OK) {
666 g_critical("Failed to start session.");
667 sr_session_destroy(session);
668 return;
669 }
670
671 if (opt_continuous)
672 add_anykey(session);
673
674 sr_session_run(session);
675
676 if (opt_continuous)
677 clear_anykey();
678
679 sr_session_datafeed_callback_remove_all(session);
680 sr_session_destroy(session);
681
682}