2 * This file is part of the sigrok-cli project.
4 * Copyright (C) 2013 Bert Vermeulen <bert@biot.com>
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.
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.
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/>.
22 #include <glib/gstdio.h>
25 #include "sigrok-cli.h"
27 static uint64_t limit_samples = 0;
28 static uint64_t limit_frames = 0;
31 extern struct srd_session *srd_sess;
34 static int set_limit_time(const struct sr_dev_inst *sdi)
39 struct sr_dev_driver *driver;
41 driver = sr_dev_inst_driver_get(sdi);
43 if (!(time_msec = sr_parse_timestring(opt_time))) {
44 g_critical("Invalid time '%s'", opt_time);
48 if (sr_dev_config_capabilities_list(sdi, NULL, SR_CONF_LIMIT_MSEC)
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.");
55 } else if (sr_dev_config_capabilities_list(sdi, NULL, SR_CONF_SAMPLERATE)
56 & (SR_CONF_GET | SR_CONF_SET)) {
57 /* Convert to samples based on the samplerate. */
58 sr_config_get(driver, sdi, NULL, SR_CONF_SAMPLERATE, &gvar);
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.");
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.");
72 g_critical("This device does not support time limits.");
79 const struct sr_output *setup_output_format(const struct sr_dev_inst *sdi, FILE **outfile)
81 const struct sr_output_module *omod;
82 const struct sr_option **options;
83 const struct sr_output *o;
84 GHashTable *fmtargs, *fmtopts;
87 if (!opt_output_format) {
88 if (opt_output_file) {
89 opt_output_format = DEFAULT_OUTPUT_FORMAT_FILE;
91 opt_output_format = DEFAULT_OUTPUT_FORMAT_NOFILE;
95 fmtargs = parse_generic_arg(opt_output_format, TRUE);
96 fmtspec = g_hash_table_lookup(fmtargs, "sigrok_key");
98 g_critical("Invalid output format.");
99 if (!(omod = sr_output_find(fmtspec)))
100 g_critical("Unknown output module '%s'.", fmtspec);
101 g_hash_table_remove(fmtargs, "sigrok_key");
102 if ((options = sr_output_options_get(omod))) {
103 fmtopts = generic_arg_to_opt(options, fmtargs);
104 sr_output_options_free(options);
107 o = sr_output_new(omod, fmtopts, sdi, opt_output_file);
109 if (opt_output_file) {
110 if (!sr_output_test_flag(omod, SR_OUTPUT_INTERNAL_IO_HANDLING))
111 *outfile = g_fopen(opt_output_file, "wb");
119 g_hash_table_destroy(fmtopts);
120 g_hash_table_destroy(fmtargs);
125 const struct sr_transform *setup_transform_module(const struct sr_dev_inst *sdi)
127 const struct sr_transform_module *tmod;
128 const struct sr_option **options;
129 const struct sr_transform *t;
130 GHashTable *fmtargs, *fmtopts;
133 fmtargs = parse_generic_arg(opt_transform_module, TRUE);
134 fmtspec = g_hash_table_lookup(fmtargs, "sigrok_key");
136 g_critical("Invalid transform module.");
137 if (!(tmod = sr_transform_find(fmtspec)))
138 g_critical("Unknown transform module '%s'.", fmtspec);
139 g_hash_table_remove(fmtargs, "sigrok_key");
140 if ((options = sr_transform_options_get(tmod))) {
141 fmtopts = generic_arg_to_opt(options, fmtargs);
142 sr_transform_options_free(options);
145 t = sr_transform_new(tmod, fmtopts, sdi);
147 g_hash_table_destroy(fmtopts);
148 g_hash_table_destroy(fmtargs);
153 void datafeed_in(const struct sr_dev_inst *sdi,
154 const struct sr_datafeed_packet *packet, void *cb_data)
156 const struct sr_datafeed_meta *meta;
157 const struct sr_datafeed_logic *logic;
158 const struct sr_datafeed_analog *analog;
159 struct sr_session *session;
160 struct sr_config *src;
161 static const struct sr_output *o = NULL;
162 static const struct sr_output *oa = NULL;
163 static uint64_t rcvd_samples_logic = 0;
164 static uint64_t rcvd_samples_analog = 0;
165 static uint64_t samplerate = 0;
166 static int triggered = 0;
167 static FILE *outfile = NULL;
173 struct sr_dev_driver *driver;
175 /* Avoid warnings when building without decoder support. */
179 driver = sr_dev_inst_driver_get(sdi);
181 /* If the first packet to come in isn't a header, don't even try. */
182 if (packet->type != SR_DF_HEADER && !o)
186 switch (packet->type) {
188 g_debug("cli: Received SR_DF_HEADER.");
189 if (!(o = setup_output_format(sdi, &outfile)))
190 g_critical("Failed to initialize output module.");
192 /* Set up backup analog output module. */
194 oa = sr_output_new(sr_output_find("analog"), NULL,
197 rcvd_samples_logic = rcvd_samples_analog = 0;
199 if (maybe_config_get(driver, sdi, NULL, SR_CONF_SAMPLERATE,
201 samplerate = g_variant_get_uint64(gvar);
202 g_variant_unref(gvar);
208 if (srd_session_metadata_set(srd_sess, SRD_CONF_SAMPLERATE,
209 g_variant_new_uint64(samplerate)) != SRD_OK) {
210 g_critical("Failed to configure decode session.");
214 if (srd_session_start(srd_sess) != SRD_OK) {
215 g_critical("Failed to start decode session.");
223 g_debug("cli: Received SR_DF_META.");
224 meta = packet->payload;
225 for (l = meta->config; l; l = l->next) {
228 case SR_CONF_SAMPLERATE:
229 samplerate = g_variant_get_uint64(src->data);
230 g_debug("cli: Got samplerate %"PRIu64" Hz.", samplerate);
233 if (srd_session_metadata_set(srd_sess, SRD_CONF_SAMPLERATE,
234 g_variant_new_uint64(samplerate)) != SRD_OK) {
235 g_critical("Failed to pass samplerate to decoder.");
240 case SR_CONF_SAMPLE_INTERVAL:
241 samplerate = g_variant_get_uint64(src->data);
242 g_debug("cli: Got sample interval %"PRIu64" ms.", samplerate);
245 /* Unknown metadata is not an error. */
252 g_debug("cli: Received SR_DF_TRIGGER.");
257 logic = packet->payload;
258 g_message("cli: Received SR_DF_LOGIC (%"PRIu64" bytes, unitsize = %d).",
259 logic->length, logic->unitsize);
260 if (logic->length == 0)
263 /* Don't store any samples until triggered. */
264 if (opt_wait_trigger && !triggered)
267 if (limit_samples && rcvd_samples_logic >= limit_samples)
270 end_sample = rcvd_samples_logic + logic->length / logic->unitsize;
271 /* Cut off last packet according to the sample limit. */
272 if (limit_samples && end_sample > limit_samples)
273 end_sample = limit_samples;
274 input_len = (end_sample - rcvd_samples_logic) * logic->unitsize;
278 if (srd_session_send(srd_sess, rcvd_samples_logic, end_sample,
279 logic->data, input_len, logic->unitsize) != SRD_OK)
280 sr_session_stop(session);
284 rcvd_samples_logic = end_sample;
288 analog = packet->payload;
289 g_message("cli: Received SR_DF_ANALOG (%d samples).", analog->num_samples);
290 if (analog->num_samples == 0)
293 if (limit_samples && rcvd_samples_analog >= limit_samples)
296 rcvd_samples_analog += analog->num_samples;
299 case SR_DF_FRAME_BEGIN:
300 g_debug("cli: Received SR_DF_FRAME_BEGIN.");
303 case SR_DF_FRAME_END:
304 g_debug("cli: Received SR_DF_FRAME_END.");
312 if (sr_output_send(o, packet, &out) == SR_OK) {
315 * The user didn't specify an output module,
316 * but needs to see this analog data.
318 sr_output_send(oa, packet, &out);
320 if (outfile && out && out->len > 0) {
321 fwrite(out->str, 1, out->len, outfile);
325 g_string_free(out, TRUE);
330 * SR_DF_END needs to be handled after the output module's receive()
331 * is called, so it can properly clean up that module.
333 if (packet->type == SR_DF_END) {
334 g_debug("cli: Received SR_DF_END.");
344 if (outfile && outfile != stdout)
348 if (rcvd_samples_logic > 0 && rcvd_samples_logic < limit_samples)
349 g_warning("Device only sent %" PRIu64 " samples.",
351 else if (rcvd_samples_analog > 0 && rcvd_samples_analog < limit_samples)
352 g_warning("Device only sent %" PRIu64 " samples.",
353 rcvd_samples_analog);
359 int opt_to_gvar(char *key, char *value, struct sr_config *src)
361 const struct sr_key_info *srci, *srmqi;
362 double tmp_double, dlow, dhigh;
363 uint64_t tmp_u64, p, q, low, high, mqflags;
365 GVariant *rational[2], *range[2], *gtup[2];
366 GVariantBuilder *vbl;
371 if (!(srci = sr_key_info_name_get(SR_KEY_CONFIG, key))) {
372 g_critical("Unknown device option '%s'.", (char *) key);
375 src->key = srci->key;
377 if ((!value || strlen(value) == 0) &&
378 (srci->datatype != SR_T_BOOL)) {
379 g_critical("Option '%s' needs a value.", (char *)key);
384 switch (srci->datatype) {
386 ret = sr_parse_sizestring(value, &tmp_u64);
389 src->data = g_variant_new_uint64(tmp_u64);
392 ret = sr_parse_sizestring(value, &tmp_u64);
395 src->data = g_variant_new_int32(tmp_u64);
398 src->data = g_variant_new_string(value);
404 tmp_bool = sr_parse_boolstring(value);
405 src->data = g_variant_new_boolean(tmp_bool);
408 tmp_double = strtof(value, NULL);
409 src->data = g_variant_new_double(tmp_double);
411 case SR_T_RATIONAL_PERIOD:
412 if ((ret = sr_parse_period(value, &p, &q)) != SR_OK)
414 rational[0] = g_variant_new_uint64(p);
415 rational[1] = g_variant_new_uint64(q);
416 src->data = g_variant_new_tuple(rational, 2);
418 case SR_T_RATIONAL_VOLT:
419 if ((ret = sr_parse_voltage(value, &p, &q)) != SR_OK)
421 rational[0] = g_variant_new_uint64(p);
422 rational[1] = g_variant_new_uint64(q);
423 src->data = g_variant_new_tuple(rational, 2);
425 case SR_T_UINT64_RANGE:
426 if (sscanf(value, "%"PRIu64"-%"PRIu64, &low, &high) != 2) {
430 range[0] = g_variant_new_uint64(low);
431 range[1] = g_variant_new_uint64(high);
432 src->data = g_variant_new_tuple(range, 2);
435 case SR_T_DOUBLE_RANGE:
436 if (sscanf(value, "%lf-%lf", &dlow, &dhigh) != 2) {
440 range[0] = g_variant_new_double(dlow);
441 range[1] = g_variant_new_double(dhigh);
442 src->data = g_variant_new_tuple(range, 2);
446 /* Expects the argument to be in the form of key=value. */
447 keyval = g_strsplit(value, "=", 2);
448 if (!keyval[0] || !keyval[1]) {
453 vbl = g_variant_builder_new(G_VARIANT_TYPE_DICTIONARY);
454 g_variant_builder_add(vbl, "{ss}",
455 keyval[0], keyval[1]);
456 src->data = g_variant_builder_end(vbl);
462 Argument is MQ id e.g. ("voltage") optionally followed by one
463 or more /<mqflag> e.g. "/ac".
465 keyval = g_strsplit(value, "/", 0);
466 if (!keyval[0] || !(srmqi = sr_key_info_name_get(SR_KEY_MQ, keyval[0]))) {
473 for (i = 1; keyval[i]; i++) {
474 if (!(srmqi = sr_key_info_name_get(SR_KEY_MQFLAGS, keyval[i]))) {
478 mqflags |= srmqi->key;
482 gtup[0] = g_variant_new_uint32(mq);
483 gtup[1] = g_variant_new_uint64(mqflags);
484 src->data = g_variant_new_tuple(gtup, 2);
488 g_critical("Unknown data type specified for option '%s' "
489 "(driver implementation bug?).", key);
494 g_critical("Invalid value: '%s' for option '%s'", value, key);
499 int set_dev_options(struct sr_dev_inst *sdi, GHashTable *args)
501 struct sr_config src;
502 struct sr_channel_group *cg;
507 g_hash_table_iter_init(&iter, args);
508 while (g_hash_table_iter_next(&iter, &key, &value)) {
509 if ((ret = opt_to_gvar(key, value, &src)) != 0)
511 cg = select_channel_group(sdi);
512 if ((ret = maybe_config_set(sr_dev_inst_driver_get(sdi), sdi, cg,
513 src.key, src.data)) != SR_OK) {
514 g_critical("Failed to set device option '%s': %s.",
515 (char *)key, sr_strerror(ret));
523 void run_session(void)
525 GSList *devices, *real_devices, *sd;
528 struct sr_session *session;
529 struct sr_trigger *trigger;
530 struct sr_dev_inst *sdi;
531 uint64_t min_samples, max_samples;
535 struct sr_dev_driver *driver;
536 const struct sr_transform *t;
537 GMainLoop *main_loop;
539 devices = device_scan();
541 g_critical("No devices found.");
546 for (sd = devices; sd; sd = sd->next) {
549 driver = sr_dev_inst_driver_get(sdi);
551 if (!(drv_opts = sr_dev_options(driver, NULL, NULL))) {
552 g_critical("Failed to query list of driver options.");
557 for (i = 0; i < drv_opts->len; i++) {
558 if (g_array_index(drv_opts, uint32_t, i) == SR_CONF_DEMO_DEV)
562 g_array_free(drv_opts, TRUE);
565 real_devices = g_slist_append(real_devices, sdi);
568 if (g_slist_length(devices) > 1) {
569 if (g_slist_length(real_devices) != 1) {
570 g_critical("sigrok-cli only supports one device for capturing.");
573 /* We only have one non-demo device. */
574 g_slist_free(devices);
575 devices = real_devices;
581 g_slist_free(devices);
582 g_slist_free(real_devices);
584 sr_session_new(sr_ctx, &session);
585 sr_session_datafeed_callback_add(session, datafeed_in, session);
587 if (sr_dev_open(sdi) != SR_OK) {
588 g_critical("Failed to open device.");
592 if (sr_session_dev_add(session, sdi) != SR_OK) {
593 g_critical("Failed to add device to session.");
594 sr_session_destroy(session);
599 if ((devargs = parse_generic_arg(opt_config, FALSE))) {
600 if (set_dev_options(sdi, devargs) != SR_OK)
602 g_hash_table_destroy(devargs);
606 if (select_channels(sdi) != SR_OK) {
607 g_critical("Failed to set channels.");
608 sr_session_destroy(session);
614 if (!parse_triggerstring(sdi, opt_triggers, &trigger)) {
615 sr_session_destroy(session);
618 if (sr_session_trigger_set(session, trigger) != SR_OK) {
619 sr_session_destroy(session);
624 if (opt_continuous) {
625 if (!sr_dev_has_option(sdi, SR_CONF_CONTINUOUS)) {
626 g_critical("This device does not support continuous sampling.");
627 sr_session_destroy(session);
633 if (set_limit_time(sdi) != SR_OK) {
634 sr_session_destroy(session);
640 if ((sr_parse_sizestring(opt_samples, &limit_samples) != SR_OK)) {
641 g_critical("Invalid sample limit '%s'.", opt_samples);
642 sr_session_destroy(session);
645 if (maybe_config_list(driver, sdi, NULL, SR_CONF_LIMIT_SAMPLES,
648 * The device has no compression, or compression is turned
649 * off, and publishes its sample memory size.
651 g_variant_get(gvar, "(tt)", &min_samples, &max_samples);
652 g_variant_unref(gvar);
653 if (limit_samples < min_samples) {
654 g_critical("The device stores at least %"PRIu64
655 " samples with the current settings.", min_samples);
657 if (limit_samples > max_samples) {
658 g_critical("The device can store only %"PRIu64
659 " samples with the current settings.", max_samples);
662 gvar = g_variant_new_uint64(limit_samples);
663 if (maybe_config_set(sr_dev_inst_driver_get(sdi), sdi, NULL, SR_CONF_LIMIT_SAMPLES, gvar) != SR_OK) {
664 g_critical("Failed to configure sample limit.");
665 sr_session_destroy(session);
671 if ((sr_parse_sizestring(opt_frames, &limit_frames) != SR_OK)) {
672 g_critical("Invalid sample limit '%s'.", opt_samples);
673 sr_session_destroy(session);
676 gvar = g_variant_new_uint64(limit_frames);
677 if (maybe_config_set(sr_dev_inst_driver_get(sdi), sdi, NULL, SR_CONF_LIMIT_FRAMES, gvar) != SR_OK) {
678 g_critical("Failed to configure frame limit.");
679 sr_session_destroy(session);
684 if (opt_transform_module) {
685 if (!(t = setup_transform_module(sdi)))
686 g_critical("Failed to initialize transform module.");
689 main_loop = g_main_loop_new(NULL, FALSE);
691 sr_session_stopped_callback_set(session,
692 (sr_session_stopped_callback)g_main_loop_quit, main_loop);
694 if (sr_session_start(session) != SR_OK) {
695 g_critical("Failed to start session.");
696 g_main_loop_unref(main_loop);
697 sr_session_destroy(session);
704 g_main_loop_run(main_loop);
710 sr_trigger_free(trigger);
712 sr_session_datafeed_callback_remove_all(session);
713 g_main_loop_unref(main_loop);
714 sr_session_destroy(session);