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 (config_key_has_cap(driver, sdi, NULL, SR_CONF_LIMIT_MSEC, SR_CONF_SET)) {
49 gvar = g_variant_new_uint64(time_msec);
50 if (sr_config_set(sdi, NULL, SR_CONF_LIMIT_MSEC, gvar) != SR_OK) {
51 g_critical("Failed to configure time limit.");
54 } else if (config_key_has_cap(driver, sdi, NULL, SR_CONF_SAMPLERATE,
55 SR_CONF_GET | SR_CONF_SET)) {
56 /* Convert to samples based on the samplerate. */
57 sr_config_get(driver, sdi, NULL, SR_CONF_SAMPLERATE, &gvar);
58 samplerate = g_variant_get_uint64(gvar);
59 g_variant_unref(gvar);
60 limit_samples = (samplerate) * time_msec / (uint64_t)1000;
61 if (limit_samples == 0) {
62 g_critical("Not enough time at this samplerate.");
65 gvar = g_variant_new_uint64(limit_samples);
66 if (sr_config_set(sdi, NULL, SR_CONF_LIMIT_SAMPLES, gvar) != SR_OK) {
67 g_critical("Failed to configure time-based sample limit.");
71 g_critical("This device does not support time limits.");
78 const struct sr_output *setup_output_format(const struct sr_dev_inst *sdi, FILE **outfile)
80 const struct sr_output_module *omod;
81 const struct sr_option **options;
82 const struct sr_output *o;
83 GHashTable *fmtargs, *fmtopts;
86 if (!opt_output_format) {
87 if (opt_output_file) {
88 opt_output_format = DEFAULT_OUTPUT_FORMAT_FILE;
90 opt_output_format = DEFAULT_OUTPUT_FORMAT_NOFILE;
94 fmtargs = parse_generic_arg(opt_output_format, TRUE);
95 fmtspec = g_hash_table_lookup(fmtargs, "sigrok_key");
97 g_critical("Invalid output format.");
98 if (!(omod = sr_output_find(fmtspec)))
99 g_critical("Unknown output module '%s'.", fmtspec);
100 g_hash_table_remove(fmtargs, "sigrok_key");
101 if ((options = sr_output_options_get(omod))) {
102 fmtopts = generic_arg_to_opt(options, fmtargs);
103 sr_output_options_free(options);
106 o = sr_output_new(omod, fmtopts, sdi, opt_output_file);
108 if (opt_output_file) {
109 if (!sr_output_test_flag(omod, SR_OUTPUT_INTERNAL_IO_HANDLING))
110 *outfile = g_fopen(opt_output_file, "wb");
118 g_hash_table_destroy(fmtopts);
119 g_hash_table_destroy(fmtargs);
124 const struct sr_transform *setup_transform_module(const struct sr_dev_inst *sdi)
126 const struct sr_transform_module *tmod;
127 const struct sr_option **options;
128 const struct sr_transform *t;
129 GHashTable *fmtargs, *fmtopts;
132 if (!opt_transform_module)
133 opt_transform_module = "nop";
135 fmtargs = parse_generic_arg(opt_transform_module, TRUE);
136 fmtspec = g_hash_table_lookup(fmtargs, "sigrok_key");
138 g_critical("Invalid transform module.");
139 if (!(tmod = sr_transform_find(fmtspec)))
140 g_critical("Unknown transform module '%s'.", fmtspec);
141 g_hash_table_remove(fmtargs, "sigrok_key");
142 if ((options = sr_transform_options_get(tmod))) {
143 fmtopts = generic_arg_to_opt(options, fmtargs);
144 sr_transform_options_free(options);
147 t = sr_transform_new(tmod, fmtopts, sdi);
149 g_hash_table_destroy(fmtopts);
150 g_hash_table_destroy(fmtargs);
155 void datafeed_in(const struct sr_dev_inst *sdi,
156 const struct sr_datafeed_packet *packet, void *cb_data)
158 const struct sr_datafeed_meta *meta;
159 const struct sr_datafeed_logic *logic;
160 const struct sr_datafeed_analog *analog;
161 struct sr_session *session;
162 struct sr_config *src;
163 static const struct sr_output *o = NULL;
164 static const struct sr_output *oa = NULL;
165 static uint64_t rcvd_samples_logic = 0;
166 static uint64_t rcvd_samples_analog = 0;
167 static uint64_t samplerate = 0;
168 static int triggered = 0;
169 static FILE *outfile = NULL;
175 struct sr_dev_driver *driver;
177 driver = sr_dev_inst_driver_get(sdi);
179 /* If the first packet to come in isn't a header, don't even try. */
180 if (packet->type != SR_DF_HEADER && !o)
184 switch (packet->type) {
186 g_debug("cli: Received SR_DF_HEADER.");
187 if (!(o = setup_output_format(sdi, &outfile)))
188 g_critical("Failed to initialize output module.");
190 /* Set up backup analog output module. */
191 oa = sr_output_new(sr_output_find("analog"), NULL, sdi, NULL);
193 rcvd_samples_logic = rcvd_samples_analog = 0;
195 if (maybe_config_get(driver, sdi, NULL, SR_CONF_SAMPLERATE,
197 samplerate = g_variant_get_uint64(gvar);
198 g_variant_unref(gvar);
204 if (srd_session_metadata_set(srd_sess, SRD_CONF_SAMPLERATE,
205 g_variant_new_uint64(samplerate)) != SRD_OK) {
206 g_critical("Failed to configure decode session.");
210 if (srd_session_start(srd_sess) != SRD_OK) {
211 g_critical("Failed to start decode session.");
219 g_debug("cli: Received SR_DF_META.");
220 meta = packet->payload;
221 for (l = meta->config; l; l = l->next) {
224 case SR_CONF_SAMPLERATE:
225 samplerate = g_variant_get_uint64(src->data);
226 g_debug("cli: Got samplerate %"PRIu64" Hz.", samplerate);
229 if (srd_session_metadata_set(srd_sess, SRD_CONF_SAMPLERATE,
230 g_variant_new_uint64(samplerate)) != SRD_OK) {
231 g_critical("Failed to pass samplerate to decoder.");
236 case SR_CONF_SAMPLE_INTERVAL:
237 samplerate = g_variant_get_uint64(src->data);
238 g_debug("cli: Got sample interval %"PRIu64" ms.", samplerate);
241 /* Unknown metadata is not an error. */
248 g_debug("cli: Received SR_DF_TRIGGER.");
253 logic = packet->payload;
254 g_message("cli: Received SR_DF_LOGIC (%"PRIu64" bytes, unitsize = %d).",
255 logic->length, logic->unitsize);
256 if (logic->length == 0)
259 /* Don't store any samples until triggered. */
260 if (opt_wait_trigger && !triggered)
263 if (limit_samples && rcvd_samples_logic >= limit_samples)
266 end_sample = rcvd_samples_logic + logic->length / logic->unitsize;
267 /* Cut off last packet according to the sample limit. */
268 if (limit_samples && end_sample > limit_samples)
269 end_sample = limit_samples;
270 input_len = (end_sample - rcvd_samples_logic) * logic->unitsize;
274 if (srd_session_send(srd_sess, rcvd_samples_logic, end_sample,
275 logic->data, input_len, logic->unitsize) != SRD_OK)
276 sr_session_stop(session);
280 rcvd_samples_logic = end_sample;
284 analog = packet->payload;
285 g_message("cli: Received SR_DF_ANALOG (%d samples).", analog->num_samples);
286 if (analog->num_samples == 0)
289 if (limit_samples && rcvd_samples_analog >= limit_samples)
292 rcvd_samples_analog += analog->num_samples;
295 case SR_DF_FRAME_BEGIN:
296 g_debug("cli: Received SR_DF_FRAME_BEGIN.");
299 case SR_DF_FRAME_END:
300 g_debug("cli: Received SR_DF_FRAME_END.");
308 if (sr_output_send(o, packet, &out) == SR_OK) {
309 if (!out || (out->len == 0
310 && !opt_output_format
311 && packet->type == SR_DF_ANALOG)) {
313 * The user didn't specify an output module,
314 * but needs to see this analog data.
316 sr_output_send(oa, packet, &out);
318 if (outfile && out && out->len > 0) {
319 fwrite(out->str, 1, out->len, outfile);
323 g_string_free(out, TRUE);
328 * SR_DF_END needs to be handled after the output module's receive()
329 * is called, so it can properly clean up that module.
331 if (packet->type == SR_DF_END) {
332 g_debug("cli: Received SR_DF_END.");
341 if (outfile && outfile != stdout)
345 if (rcvd_samples_logic > 0 && rcvd_samples_logic < limit_samples)
346 g_warning("Device only sent %" PRIu64 " samples.",
348 else if (rcvd_samples_analog > 0 && rcvd_samples_analog < limit_samples)
349 g_warning("Device only sent %" PRIu64 " samples.",
350 rcvd_samples_analog);
356 int opt_to_gvar(char *key, char *value, struct sr_config *src)
358 const struct sr_key_info *srci, *srmqi;
359 double tmp_double, dlow, dhigh;
360 uint64_t tmp_u64, p, q, low, high, mqflags;
362 GVariant *rational[2], *range[2], *gtup[2];
363 GVariantBuilder *vbl;
368 if (!(srci = sr_key_info_name_get(SR_KEY_CONFIG, key))) {
369 g_critical("Unknown device option '%s'.", (char *) key);
372 src->key = srci->key;
374 if ((!value || strlen(value) == 0) &&
375 (srci->datatype != SR_T_BOOL)) {
376 g_critical("Option '%s' needs a value.", (char *)key);
381 switch (srci->datatype) {
383 ret = sr_parse_sizestring(value, &tmp_u64);
386 src->data = g_variant_new_uint64(tmp_u64);
389 ret = sr_parse_sizestring(value, &tmp_u64);
392 src->data = g_variant_new_int32(tmp_u64);
395 src->data = g_variant_new_string(value);
401 tmp_bool = sr_parse_boolstring(value);
402 src->data = g_variant_new_boolean(tmp_bool);
405 tmp_double = strtof(value, NULL);
406 src->data = g_variant_new_double(tmp_double);
408 case SR_T_RATIONAL_PERIOD:
409 if ((ret = sr_parse_period(value, &p, &q)) != SR_OK)
411 rational[0] = g_variant_new_uint64(p);
412 rational[1] = g_variant_new_uint64(q);
413 src->data = g_variant_new_tuple(rational, 2);
415 case SR_T_RATIONAL_VOLT:
416 if ((ret = sr_parse_voltage(value, &p, &q)) != SR_OK)
418 rational[0] = g_variant_new_uint64(p);
419 rational[1] = g_variant_new_uint64(q);
420 src->data = g_variant_new_tuple(rational, 2);
422 case SR_T_UINT64_RANGE:
423 if (sscanf(value, "%"PRIu64"-%"PRIu64, &low, &high) != 2) {
427 range[0] = g_variant_new_uint64(low);
428 range[1] = g_variant_new_uint64(high);
429 src->data = g_variant_new_tuple(range, 2);
432 case SR_T_DOUBLE_RANGE:
433 if (sscanf(value, "%lf-%lf", &dlow, &dhigh) != 2) {
437 range[0] = g_variant_new_double(dlow);
438 range[1] = g_variant_new_double(dhigh);
439 src->data = g_variant_new_tuple(range, 2);
443 /* Expects the argument to be in the form of key=value. */
444 keyval = g_strsplit(value, "=", 2);
445 if (!keyval[0] || !keyval[1]) {
450 vbl = g_variant_builder_new(G_VARIANT_TYPE_DICTIONARY);
451 g_variant_builder_add(vbl, "{ss}",
452 keyval[0], keyval[1]);
453 src->data = g_variant_builder_end(vbl);
459 Argument is MQ id e.g. ("voltage") optionally followed by one
460 or more /<mqflag> e.g. "/ac".
462 keyval = g_strsplit(value, "/", 0);
463 if (!keyval[0] || !(srmqi = sr_key_info_name_get(SR_KEY_MQ, keyval[0]))) {
470 for (i = 1; keyval[i]; i++) {
471 if (!(srmqi = sr_key_info_name_get(SR_KEY_MQFLAGS, keyval[i]))) {
475 mqflags |= srmqi->key;
479 gtup[0] = g_variant_new_uint32(mq);
480 gtup[1] = g_variant_new_uint64(mqflags);
481 src->data = g_variant_new_tuple(gtup, 2);
485 g_critical("Unknown data type specified for option '%s' "
486 "(driver implementation bug?).", key);
491 g_critical("Invalid value: '%s' for option '%s'", value, key);
496 int set_dev_options(struct sr_dev_inst *sdi, GHashTable *args)
498 struct sr_config src;
499 struct sr_channel_group *cg;
504 g_hash_table_iter_init(&iter, args);
505 while (g_hash_table_iter_next(&iter, &key, &value)) {
506 if ((ret = opt_to_gvar(key, value, &src)) != 0)
508 cg = select_channel_group(sdi);
509 if ((ret = maybe_config_set(sr_dev_inst_driver_get(sdi), sdi, cg,
510 src.key, src.data)) != SR_OK) {
511 g_critical("Failed to set device option '%s': %s.",
512 (char *)key, sr_strerror(ret));
520 void run_session(void)
522 GSList *devices, *real_devices, *sd;
525 struct sr_session *session;
526 struct sr_trigger *trigger;
527 struct sr_dev_inst *sdi;
528 uint64_t min_samples, max_samples;
530 const uint32_t *dev_opts;
532 struct sr_dev_driver *driver;
533 const struct sr_transform *t;
534 GMainLoop *main_loop;
536 devices = device_scan();
538 g_critical("No devices found.");
543 for (sd = devices; sd; sd = sd->next) {
546 driver = sr_dev_inst_driver_get(sdi);
548 if (sr_config_list(driver, sdi, NULL, SR_CONF_DEVICE_OPTIONS, &gvar) != SR_OK) {
549 g_critical("Failed to query list device options.");
553 dev_opts = g_variant_get_fixed_array(gvar, &n_elements, sizeof(uint32_t));
556 for (i = 0; i < n_elements; i++) {
557 if (dev_opts[i] == SR_CONF_DEMO_DEV)
561 g_variant_unref(gvar);
564 real_devices = g_slist_append(real_devices, sdi);
567 if (g_slist_length(devices) > 1) {
568 if (g_slist_length(real_devices) != 1) {
569 g_critical("sigrok-cli only supports one device for capturing.");
572 /* We only have one non-demo device. */
573 g_slist_free(devices);
574 devices = real_devices;
580 g_slist_free(devices);
581 g_slist_free(real_devices);
583 sr_session_new(sr_ctx, &session);
584 sr_session_datafeed_callback_add(session, datafeed_in, NULL);
586 if (sr_dev_open(sdi) != SR_OK) {
587 g_critical("Failed to open device.");
591 if (sr_session_dev_add(session, sdi) != SR_OK) {
592 g_critical("Failed to add device to session.");
593 sr_session_destroy(session);
598 if ((devargs = parse_generic_arg(opt_config, FALSE))) {
599 if (set_dev_options(sdi, devargs) != SR_OK)
601 g_hash_table_destroy(devargs);
605 if (select_channels(sdi) != SR_OK) {
606 g_critical("Failed to set channels.");
607 sr_session_destroy(session);
613 if (!parse_triggerstring(sdi, opt_triggers, &trigger)) {
614 sr_session_destroy(session);
617 if (sr_session_trigger_set(session, trigger) != SR_OK) {
618 sr_session_destroy(session);
623 if (opt_continuous) {
624 if (!sr_dev_has_option(sdi, SR_CONF_CONTINUOUS)) {
625 g_critical("This device does not support continuous sampling.");
626 sr_session_destroy(session);
632 if (set_limit_time(sdi) != SR_OK) {
633 sr_session_destroy(session);
639 if ((sr_parse_sizestring(opt_samples, &limit_samples) != SR_OK)) {
640 g_critical("Invalid sample limit '%s'.", opt_samples);
641 sr_session_destroy(session);
644 if (maybe_config_list(driver, sdi, NULL, SR_CONF_LIMIT_SAMPLES,
647 * The device has no compression, or compression is turned
648 * off, and publishes its sample memory size.
650 g_variant_get(gvar, "(tt)", &min_samples, &max_samples);
651 g_variant_unref(gvar);
652 if (limit_samples < min_samples) {
653 g_critical("The device stores at least %"PRIu64
654 " samples with the current settings.", min_samples);
656 if (limit_samples > max_samples) {
657 g_critical("The device can store only %"PRIu64
658 " samples with the current settings.", max_samples);
661 gvar = g_variant_new_uint64(limit_samples);
662 if (maybe_config_set(sr_dev_inst_driver_get(sdi), sdi, NULL, SR_CONF_LIMIT_SAMPLES, gvar) != SR_OK) {
663 g_critical("Failed to configure sample limit.");
664 sr_session_destroy(session);
670 if ((sr_parse_sizestring(opt_frames, &limit_frames) != SR_OK)) {
671 g_critical("Invalid sample limit '%s'.", opt_samples);
672 sr_session_destroy(session);
675 gvar = g_variant_new_uint64(limit_frames);
676 if (maybe_config_set(sr_dev_inst_driver_get(sdi), sdi, NULL, SR_CONF_LIMIT_FRAMES, gvar) != SR_OK) {
677 g_critical("Failed to configure frame limit.");
678 sr_session_destroy(session);
683 if (!(t = setup_transform_module(sdi)))
684 g_critical("Failed to initialize transform module.");
686 main_loop = g_main_loop_new(NULL, FALSE);
688 sr_session_stopped_callback_set(session,
689 (sr_session_stopped_callback)g_main_loop_quit, main_loop);
691 if (sr_session_start(session) != SR_OK) {
692 g_critical("Failed to start session.");
693 g_main_loop_unref(main_loop);
694 sr_session_destroy(session);
701 g_main_loop_run(main_loop);
707 sr_trigger_free(trigger);
709 sr_session_datafeed_callback_remove_all(session);
710 g_main_loop_unref(main_loop);
711 sr_session_destroy(session);