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/>.
20 #include "sigrok-cli.h"
22 #include <glib/gstdio.h>
26 static uint64_t limit_samples = 0;
27 static uint64_t limit_frames = 0;
28 static char *srzip_and_filename = NULL;
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)
80 const struct sr_output_module *omod;
81 const struct sr_option **options;
82 const struct sr_output *o;
83 GHashTable *fmtargs, *fmtopts;
87 if (!opt_output_format) {
88 if (opt_output_file) {
89 size = strlen(opt_output_file) + 32;
90 srzip_and_filename = g_malloc(size);
91 snprintf(srzip_and_filename, size, "srzip:filename=%s", opt_output_file);
92 opt_output_format = srzip_and_filename;
94 opt_output_format = DEFAULT_OUTPUT_FORMAT;
98 fmtargs = parse_generic_arg(opt_output_format, TRUE);
99 fmtspec = g_hash_table_lookup(fmtargs, "sigrok_key");
101 g_critical("Invalid output format.");
102 if (!(omod = sr_output_find(fmtspec)))
103 g_critical("Unknown output module '%s'.", fmtspec);
104 g_hash_table_remove(fmtargs, "sigrok_key");
105 if ((options = sr_output_options_get(omod))) {
106 fmtopts = generic_arg_to_opt(options, fmtargs);
107 sr_output_options_free(options);
110 o = sr_output_new(omod, fmtopts, sdi);
112 g_hash_table_destroy(fmtopts);
113 g_hash_table_destroy(fmtargs);
118 const struct sr_transform *setup_transform_module(const struct sr_dev_inst *sdi)
120 const struct sr_transform_module *tmod;
121 const struct sr_option **options;
122 const struct sr_transform *t;
123 GHashTable *fmtargs, *fmtopts;
126 if (!opt_transform_module)
127 opt_transform_module = "nop";
129 fmtargs = parse_generic_arg(opt_transform_module, TRUE);
130 fmtspec = g_hash_table_lookup(fmtargs, "sigrok_key");
132 g_critical("Invalid transform module.");
133 if (!(tmod = sr_transform_find(fmtspec)))
134 g_critical("Unknown transform module '%s'.", fmtspec);
135 g_hash_table_remove(fmtargs, "sigrok_key");
136 if ((options = sr_transform_options_get(tmod))) {
137 fmtopts = generic_arg_to_opt(options, fmtargs);
138 sr_transform_options_free(options);
141 t = sr_transform_new(tmod, fmtopts, sdi);
143 g_hash_table_destroy(fmtopts);
144 g_hash_table_destroy(fmtargs);
149 void datafeed_in(const struct sr_dev_inst *sdi,
150 const struct sr_datafeed_packet *packet, void *cb_data)
152 const struct sr_datafeed_meta *meta;
153 const struct sr_datafeed_logic *logic;
154 const struct sr_datafeed_analog *analog;
155 struct sr_session *session;
156 struct sr_config *src;
157 static const struct sr_output *o = NULL;
158 static const struct sr_output *oa = NULL;
159 static uint64_t rcvd_samples_logic = 0;
160 static uint64_t rcvd_samples_analog = 0;
161 static uint64_t samplerate = 0;
162 static int triggered = 0;
163 static FILE *outfile = NULL;
169 struct sr_dev_driver *driver;
171 driver = sr_dev_inst_driver_get(sdi);
173 /* If the first packet to come in isn't a header, don't even try. */
174 if (packet->type != SR_DF_HEADER && o == NULL)
178 switch (packet->type) {
180 g_debug("cli: Received SR_DF_HEADER.");
181 if (!(o = setup_output_format(sdi)))
182 g_critical("Failed to initialize output module.");
184 /* Set up backup analog output module. */
185 oa = sr_output_new(sr_output_find("analog"), NULL, sdi);
188 outfile = g_fopen(opt_output_file, "wb");
192 rcvd_samples_logic = rcvd_samples_analog = 0;
194 if (maybe_config_get(driver, sdi, NULL, SR_CONF_SAMPLERATE,
196 samplerate = g_variant_get_uint64(gvar);
197 g_variant_unref(gvar);
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.");
209 if (srd_session_start(srd_sess) != SRD_OK) {
210 g_critical("Failed to start decode session.");
218 g_debug("cli: Received SR_DF_META.");
219 meta = packet->payload;
220 for (l = meta->config; l; l = l->next) {
223 case SR_CONF_SAMPLERATE:
224 samplerate = g_variant_get_uint64(src->data);
225 g_debug("cli: Got samplerate %"PRIu64" Hz.", samplerate);
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.");
235 case SR_CONF_SAMPLE_INTERVAL:
236 samplerate = g_variant_get_uint64(src->data);
237 g_debug("cli: Got sample interval %"PRIu64" ms.", samplerate);
240 /* Unknown metadata is not an error. */
247 g_debug("cli: Received SR_DF_TRIGGER.");
252 logic = packet->payload;
253 g_message("cli: Received SR_DF_LOGIC (%"PRIu64" bytes, unitsize = %d).",
254 logic->length, logic->unitsize);
255 if (logic->length == 0)
258 /* Don't store any samples until triggered. */
259 if (opt_wait_trigger && !triggered)
262 if (limit_samples && rcvd_samples_logic >= limit_samples)
265 end_sample = rcvd_samples_logic + logic->length / logic->unitsize;
266 /* Cut off last packet according to the sample limit. */
267 if (limit_samples && end_sample > limit_samples)
268 end_sample = limit_samples;
269 input_len = (end_sample - rcvd_samples_logic) * logic->unitsize;
273 if (srd_session_send(srd_sess, rcvd_samples_logic, end_sample,
274 logic->data, input_len) != SRD_OK)
275 sr_session_stop(session);
279 rcvd_samples_logic = end_sample;
283 analog = packet->payload;
284 g_message("cli: Received SR_DF_ANALOG (%d samples).", analog->num_samples);
285 if (analog->num_samples == 0)
288 if (limit_samples && rcvd_samples_analog >= limit_samples)
291 rcvd_samples_analog += analog->num_samples;
294 case SR_DF_FRAME_BEGIN:
295 g_debug("cli: Received SR_DF_FRAME_BEGIN.");
298 case SR_DF_FRAME_END:
299 g_debug("cli: Received SR_DF_FRAME_END.");
306 if (o && outfile && !opt_pds) {
307 if (sr_output_send(o, packet, &out) == SR_OK) {
308 if (!out || (out->len == 0
309 && !opt_output_format
310 && packet->type == SR_DF_ANALOG)) {
311 /* The user didn't specify an output module,
312 * but needs to see this analog data. */
313 sr_output_send(oa, packet, &out);
315 if (out && out->len > 0) {
316 fwrite(out->str, 1, out->len, outfile);
320 g_string_free(out, TRUE);
324 /* SR_DF_END needs to be handled after the output module's receive()
325 * is called, so it can properly clean up that module. */
326 if (packet->type == SR_DF_END) {
327 g_debug("cli: Received SR_DF_END.");
331 if (srzip_and_filename)
332 g_free(srzip_and_filename);
339 if (outfile && outfile != stdout)
343 if (rcvd_samples_logic > 0 && rcvd_samples_logic < limit_samples)
344 g_warning("Device only sent %" PRIu64 " samples.",
346 else if (rcvd_samples_analog > 0 && rcvd_samples_analog < limit_samples)
347 g_warning("Device only sent %" PRIu64 " samples.",
348 rcvd_samples_analog);
354 int opt_to_gvar(char *key, char *value, struct sr_config *src)
356 const struct sr_config_info *srci;
357 double tmp_double, dlow, dhigh;
358 uint64_t tmp_u64, p, q, low, high;
359 GVariant *rational[2], *range[2];
360 GVariantBuilder *vbl;
365 if (!(srci = sr_config_info_name_get(key))) {
366 g_critical("Unknown device option '%s'.", (char *) key);
369 src->key = srci->key;
371 if ((value == NULL || strlen(value) == 0) &&
372 (srci->datatype != SR_T_BOOL)) {
373 g_critical("Option '%s' needs a value.", (char *)key);
378 switch (srci->datatype) {
380 ret = sr_parse_sizestring(value, &tmp_u64);
383 src->data = g_variant_new_uint64(tmp_u64);
386 ret = sr_parse_sizestring(value, &tmp_u64);
389 src->data = g_variant_new_int32(tmp_u64);
392 src->data = g_variant_new_string(value);
398 tmp_bool = sr_parse_boolstring(value);
399 src->data = g_variant_new_boolean(tmp_bool);
402 tmp_double = strtof(value, NULL);
403 src->data = g_variant_new_double(tmp_double);
405 case SR_T_RATIONAL_PERIOD:
406 if ((ret = sr_parse_period(value, &p, &q)) != SR_OK)
408 rational[0] = g_variant_new_uint64(p);
409 rational[1] = g_variant_new_uint64(q);
410 src->data = g_variant_new_tuple(rational, 2);
412 case SR_T_RATIONAL_VOLT:
413 if ((ret = sr_parse_voltage(value, &p, &q)) != SR_OK)
415 rational[0] = g_variant_new_uint64(p);
416 rational[1] = g_variant_new_uint64(q);
417 src->data = g_variant_new_tuple(rational, 2);
419 case SR_T_UINT64_RANGE:
420 if (sscanf(value, "%"PRIu64"-%"PRIu64, &low, &high) != 2) {
424 range[0] = g_variant_new_uint64(low);
425 range[1] = g_variant_new_uint64(high);
426 src->data = g_variant_new_tuple(range, 2);
429 case SR_T_DOUBLE_RANGE:
430 if (sscanf(value, "%lf-%lf", &dlow, &dhigh) != 2) {
434 range[0] = g_variant_new_double(dlow);
435 range[1] = g_variant_new_double(dhigh);
436 src->data = g_variant_new_tuple(range, 2);
440 /* Expects the argument to be in the form of key=value. */
441 keyval = g_strsplit(value, "=", 2);
442 if (!keyval[0] || !keyval[1]) {
447 vbl = g_variant_builder_new(G_VARIANT_TYPE_DICTIONARY);
448 g_variant_builder_add(vbl, "{ss}",
449 keyval[0], keyval[1]);
450 src->data = g_variant_builder_end(vbl);
455 g_critical("Unknown data type specified for option '%s' "
456 "(driver implementation bug?).", key);
461 g_critical("Invalid value: '%s' for option '%s'", value, key);
466 int set_dev_options(struct sr_dev_inst *sdi, GHashTable *args)
468 struct sr_config src;
469 struct sr_channel_group *cg;
474 g_hash_table_iter_init(&iter, args);
475 while (g_hash_table_iter_next(&iter, &key, &value)) {
476 if ((ret = opt_to_gvar(key, value, &src)) != 0)
478 cg = select_channel_group(sdi);
479 if ((ret = maybe_config_set(sr_dev_inst_driver_get(sdi), sdi, cg,
480 src.key, src.data)) != SR_OK) {
481 g_critical("Failed to set device option '%s'.", (char *)key);
489 void run_session(void)
491 GSList *devices, *real_devices, *sd;
494 struct sr_session *session;
495 struct sr_trigger *trigger;
496 struct sr_dev_inst *sdi;
497 uint64_t min_samples, max_samples;
499 const uint32_t *dev_opts;
501 struct sr_dev_driver *driver;
502 const struct sr_transform *t;
504 devices = device_scan();
506 g_critical("No devices found.");
511 for (sd = devices; sd; sd = sd->next) {
514 driver = sr_dev_inst_driver_get(sdi);
516 if (sr_config_list(driver, sdi, NULL, SR_CONF_DEVICE_OPTIONS, &gvar) != SR_OK) {
517 g_critical("Failed to query list device options.");
521 dev_opts = g_variant_get_fixed_array(gvar, &n_elements, sizeof(uint32_t));
524 for (i = 0; i < n_elements; i++) {
525 if (dev_opts[i] == SR_CONF_DEMO_DEV)
529 g_variant_unref(gvar);
532 real_devices = g_slist_append(real_devices, sdi);
535 if (g_slist_length(devices) > 1) {
536 if (g_slist_length(real_devices) != 1) {
537 g_critical("sigrok-cli only supports one device for capturing.");
540 /* We only have one non-demo device. */
541 g_slist_free(devices);
542 devices = real_devices;
548 g_slist_free(devices);
549 g_slist_free(real_devices);
551 sr_session_new(&session);
552 sr_session_datafeed_callback_add(session, datafeed_in, NULL);
554 if (sr_dev_open(sdi) != SR_OK) {
555 g_critical("Failed to open device.");
559 if (sr_session_dev_add(session, sdi) != SR_OK) {
560 g_critical("Failed to add device to session.");
561 sr_session_destroy(session);
566 if ((devargs = parse_generic_arg(opt_config, FALSE))) {
567 if (set_dev_options(sdi, devargs) != SR_OK)
569 g_hash_table_destroy(devargs);
573 if (select_channels(sdi) != SR_OK) {
574 g_critical("Failed to set channels.");
575 sr_session_destroy(session);
580 if (!parse_triggerstring(sdi, opt_triggers, &trigger)) {
581 sr_session_destroy(session);
584 if (sr_session_trigger_set(session, trigger) != SR_OK) {
585 sr_session_destroy(session);
590 if (opt_continuous) {
591 if (!sr_dev_has_option(sdi, SR_CONF_CONTINUOUS)) {
592 g_critical("This device does not support continuous sampling.");
593 sr_session_destroy(session);
599 if (set_limit_time(sdi) != SR_OK) {
600 sr_session_destroy(session);
606 if ((sr_parse_sizestring(opt_samples, &limit_samples) != SR_OK)) {
607 g_critical("Invalid sample limit '%s'.", opt_samples);
608 sr_session_destroy(session);
611 if (maybe_config_list(driver, sdi, NULL, SR_CONF_LIMIT_SAMPLES,
613 /* The device has no compression, or compression is turned
614 * off, and publishes its sample memory size. */
615 g_variant_get(gvar, "(tt)", &min_samples, &max_samples);
616 g_variant_unref(gvar);
617 if (limit_samples < min_samples) {
618 g_critical("The device stores at least %"PRIu64
619 " samples with the current settings.", min_samples);
621 if (limit_samples > max_samples) {
622 g_critical("The device can store only %"PRIu64
623 " samples with the current settings.", max_samples);
626 gvar = g_variant_new_uint64(limit_samples);
627 if (maybe_config_set(sr_dev_inst_driver_get(sdi), sdi, NULL, SR_CONF_LIMIT_SAMPLES, gvar) != SR_OK) {
628 g_critical("Failed to configure sample limit.");
629 sr_session_destroy(session);
635 if ((sr_parse_sizestring(opt_frames, &limit_frames) != SR_OK)) {
636 g_critical("Invalid sample limit '%s'.", opt_samples);
637 sr_session_destroy(session);
640 gvar = g_variant_new_uint64(limit_frames);
641 if (maybe_config_set(sr_dev_inst_driver_get(sdi), sdi, NULL, SR_CONF_LIMIT_FRAMES, gvar) != SR_OK) {
642 g_critical("Failed to configure frame limit.");
643 sr_session_destroy(session);
648 if (!(t = setup_transform_module(sdi)))
649 g_critical("Failed to initialize transform module.");
651 if (sr_session_start(session) != SR_OK) {
652 g_critical("Failed to start session.");
653 sr_session_destroy(session);
660 sr_session_run(session);
665 sr_session_datafeed_callback_remove_all(session);
666 sr_session_destroy(session);