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"
23 #include <glib/gstdio.h>
27 static struct sr_output_format *output_format = NULL;
28 static int default_output_format = FALSE;
29 static char *output_format_param = NULL;
30 static GByteArray *savebuf;
31 static uint64_t limit_samples = 0;
32 static uint64_t limit_frames = 0;
34 extern gchar *opt_output_file;
35 extern gchar *opt_output_format;
36 extern gchar *opt_pds;
37 extern gboolean opt_wait_trigger;
38 extern gchar *opt_time;
39 extern gchar *opt_samples;
40 extern gchar *opt_frames;
41 extern gchar *opt_continuous;
42 extern gchar *opt_config;
43 extern gchar *opt_triggers;
45 extern struct srd_session *srd_sess;
49 static GArray *get_enabled_logic_probes(const struct sr_dev_inst *sdi)
51 struct sr_probe *probe;
55 probes = g_array_new(FALSE, FALSE, sizeof(int));
56 for (l = sdi->probes; l; l = l->next) {
58 if (probe->type != SR_PROBE_LOGIC)
60 if (probe->enabled != TRUE)
62 g_array_append_val(probes, probe->index);
68 static int set_limit_time(const struct sr_dev_inst *sdi)
74 if (!(time_msec = sr_parse_timestring(opt_time))) {
75 g_critical("Invalid time '%s'", opt_time);
79 if (sr_dev_has_option(sdi, SR_CONF_LIMIT_MSEC)) {
80 gvar = g_variant_new_uint64(time_msec);
81 if (sr_config_set(sdi, NULL, SR_CONF_LIMIT_MSEC, gvar) != SR_OK) {
82 g_critical("Failed to configure time limit.");
85 } else if (sr_dev_has_option(sdi, SR_CONF_SAMPLERATE)) {
86 /* Convert to samples based on the samplerate. */
87 sr_config_get(sdi->driver, sdi, NULL, SR_CONF_SAMPLERATE, &gvar);
88 samplerate = g_variant_get_uint64(gvar);
89 g_variant_unref(gvar);
90 limit_samples = (samplerate) * time_msec / (uint64_t)1000;
91 if (limit_samples == 0) {
92 g_critical("Not enough time at this samplerate.");
95 gvar = g_variant_new_uint64(limit_samples);
96 if (sr_config_set(sdi, NULL, SR_CONF_LIMIT_SAMPLES, gvar) != SR_OK) {
97 g_critical("Failed to configure time-based sample limit.");
101 g_critical("This device does not support time limits.");
108 int setup_output_format(void)
113 struct sr_output_format **outputs;
117 if (opt_output_format && !strcmp(opt_output_format, "sigrok")) {
118 /* Doesn't really exist as an output module - this is
119 * the session save mode. */
120 g_free(opt_output_format);
121 opt_output_format = NULL;
124 if (!opt_output_format) {
125 opt_output_format = DEFAULT_OUTPUT_FORMAT;
126 /* we'll need to remember this so when saving to a file
127 * later, sigrok session format will be used.
129 default_output_format = TRUE;
132 fmtargs = parse_generic_arg(opt_output_format, TRUE);
133 fmtspec = g_hash_table_lookup(fmtargs, "sigrok_key");
135 g_critical("Invalid output format.");
138 outputs = sr_output_list();
139 for (i = 0; outputs[i]; i++) {
140 if (strcmp(outputs[i]->id, fmtspec))
142 g_hash_table_remove(fmtargs, "sigrok_key");
143 output_format = outputs[i];
144 g_hash_table_iter_init(&iter, fmtargs);
145 while (g_hash_table_iter_next(&iter, &key, &value)) {
146 /* only supporting one parameter per output module
147 * for now, and only its value */
148 output_format_param = g_strdup(value);
153 if (!output_format) {
154 g_critical("Invalid output format %s.", opt_output_format);
157 g_hash_table_destroy(fmtargs);
162 void datafeed_in(const struct sr_dev_inst *sdi,
163 const struct sr_datafeed_packet *packet, void *cb_data)
165 const struct sr_datafeed_meta *meta;
166 const struct sr_datafeed_logic *logic;
167 const struct sr_datafeed_analog *analog;
168 struct sr_config *src;
169 static struct sr_output *o = NULL;
170 static GArray *logic_probelist = NULL;
171 static uint64_t received_samples = 0;
172 static int unitsize = 0;
173 static int triggered = 0;
174 static FILE *outfile = NULL;
179 uint64_t output_len, input_len;
184 /* If the first packet to come in isn't a header, don't even try. */
185 if (packet->type != SR_DF_HEADER && o == NULL)
188 switch (packet->type) {
190 g_debug("cli: Received SR_DF_HEADER");
191 /* Initialize the output module. */
192 if (!(o = g_try_malloc(sizeof(struct sr_output)))) {
193 g_critical("Output module malloc failed.");
196 o->format = output_format;
197 o->sdi = (struct sr_dev_inst *)sdi;
198 o->param = output_format_param;
199 if (o->format->init) {
200 if (o->format->init(o) != SR_OK) {
201 g_critical("Output format initialization failed.");
206 /* Prepare non-stdout output. */
208 if (opt_output_file) {
209 if (default_output_format) {
210 /* output file is in session format, so we'll
211 * keep a copy of everything as it comes in
212 * and save from there after the session. */
214 savebuf = g_byte_array_new();
216 /* saving to a file in whatever format was set
217 * with --format, so all we need is a filehandle */
218 outfile = g_fopen(opt_output_file, "wb");
222 /* Prepare for logic data. */
223 logic_probelist = get_enabled_logic_probes(sdi);
224 /* How many bytes we need to store the packed samples. */
225 unitsize = (logic_probelist->len + 7) / 8;
229 if (opt_pds && logic_probelist->len) {
230 if (sr_config_get(sdi->driver, sdi, NULL, SR_CONF_SAMPLERATE,
232 samplerate = g_variant_get_uint64(gvar);
233 g_variant_unref(gvar);
234 if (srd_session_metadata_set(srd_sess, SRD_CONF_SAMPLERATE,
235 g_variant_new_uint64(samplerate)) != SRD_OK) {
236 g_critical("Failed to configure decode session.");
240 if (srd_session_start(srd_sess) != SRD_OK) {
241 g_critical("Failed to start decode session.");
249 g_debug("cli: received SR_DF_META");
250 meta = packet->payload;
251 for (l = meta->config; l; l = l->next) {
254 case SR_CONF_SAMPLERATE:
255 samplerate = g_variant_get_uint64(src->data);
256 g_debug("cli: got samplerate %"PRIu64" Hz", samplerate);
259 if (srd_session_metadata_set(srd_sess, SRD_CONF_SAMPLERATE,
260 g_variant_new_uint64(samplerate)) != SRD_OK) {
261 g_critical("Failed to pass samplerate to decoder.");
266 case SR_CONF_SAMPLE_INTERVAL:
267 samplerate = g_variant_get_uint64(src->data);
268 g_debug("cli: got sample interval %"PRIu64" ms", samplerate);
271 /* Unknown metadata is not an error. */
278 g_debug("cli: received SR_DF_TRIGGER");
279 if (o->format->event)
280 o->format->event(o, SR_DF_TRIGGER, &output_buf,
286 logic = packet->payload;
287 g_message("cli: received SR_DF_LOGIC, %"PRIu64" bytes", logic->length);
288 if (logic->length == 0)
291 /* Don't store any samples until triggered. */
292 if (opt_wait_trigger && !triggered)
295 if (limit_samples && received_samples >= limit_samples)
298 end_sample = received_samples + logic->length / logic->unitsize;
299 /* Cut off last packet according to the sample limit. */
300 if (limit_samples && end_sample > limit_samples)
301 end_sample = limit_samples;
302 input_len = (end_sample - received_samples) * logic->unitsize;
304 if (opt_output_file && default_output_format) {
305 /* Saving to a session file. */
306 g_byte_array_append(savebuf, logic->data, input_len);
310 if (srd_session_send(srd_sess, received_samples, end_sample,
311 logic->data, input_len) != SRD_OK)
316 if (o->format->data && packet->type == o->format->df_type)
317 o->format->data(o, logic->data, input_len,
318 &output_buf, &output_len);
320 fwrite(output_buf, 1, output_len, outfile);
327 received_samples += logic->length / logic->unitsize;
331 analog = packet->payload;
332 g_message("cli: received SR_DF_ANALOG, %d samples", analog->num_samples);
333 if (analog->num_samples == 0)
336 if (limit_samples && received_samples >= limit_samples)
339 if (o->format->data && packet->type == o->format->df_type) {
340 o->format->data(o, (const uint8_t *)analog->data,
341 analog->num_samples * sizeof(float),
342 &output_buf, &output_len);
344 fwrite(output_buf, 1, output_len, outfile);
350 received_samples += analog->num_samples;
353 case SR_DF_FRAME_BEGIN:
354 g_debug("cli: received SR_DF_FRAME_BEGIN");
355 if (o->format->event) {
356 o->format->event(o, SR_DF_FRAME_BEGIN, &output_buf,
359 fwrite(output_buf, 1, output_len, outfile);
366 case SR_DF_FRAME_END:
367 g_debug("cli: received SR_DF_FRAME_END");
368 if (o->format->event) {
369 o->format->event(o, SR_DF_FRAME_END, &output_buf,
372 fwrite(output_buf, 1, output_len, outfile);
383 if (o && o->format->receive) {
384 if (o->format->receive(o, sdi, packet, &out) == SR_OK && out) {
385 fwrite(out->str, 1, out->len, outfile);
387 g_string_free(out, TRUE);
391 /* SR_DF_END needs to be handled after the output module's receive()
392 * is called, so it can properly clean up that module etc. */
393 if (packet->type == SR_DF_END) {
394 g_debug("cli: Received SR_DF_END");
396 if (o->format->event) {
397 o->format->event(o, SR_DF_END, &output_buf, &output_len);
400 fwrite(output_buf, 1, output_len, outfile);
406 if (limit_samples && received_samples < limit_samples)
407 g_warning("Device only sent %" PRIu64 " samples.",
411 g_warning("Device stopped after %" PRIu64 " samples.",
414 g_array_free(logic_probelist, TRUE);
416 if (o->format->cleanup)
417 o->format->cleanup(o);
421 if (outfile && outfile != stdout)
424 if (opt_output_file && default_output_format && savebuf->len) {
425 if (sr_session_save(opt_output_file, sdi, savebuf->data,
426 unitsize, savebuf->len / unitsize) != SR_OK)
427 g_critical("Failed to save session.");
428 g_byte_array_free(savebuf, TRUE);
434 int opt_to_gvar(char *key, char *value, struct sr_config *src)
436 const struct sr_config_info *srci;
438 uint64_t tmp_u64, p, q, low, high;
439 GVariant *rational[2], *range[2];
443 if (!(srci = sr_config_info_name_get(key))) {
444 g_critical("Unknown device option '%s'.", (char *) key);
447 src->key = srci->key;
449 if ((value == NULL) &&
450 (srci->datatype != SR_T_BOOL)) {
451 g_critical("Option '%s' needs a value.", (char *)key);
456 switch (srci->datatype) {
458 ret = sr_parse_sizestring(value, &tmp_u64);
461 src->data = g_variant_new_uint64(tmp_u64);
464 ret = sr_parse_sizestring(value, &tmp_u64);
467 src->data = g_variant_new_int32(tmp_u64);
470 src->data = g_variant_new_string(value);
476 tmp_bool = sr_parse_boolstring(value);
477 src->data = g_variant_new_boolean(tmp_bool);
480 tmp_double = strtof(value, NULL);
481 src->data = g_variant_new_double(tmp_double);
483 case SR_T_RATIONAL_PERIOD:
484 if ((ret = sr_parse_period(value, &p, &q)) != SR_OK)
486 rational[0] = g_variant_new_uint64(p);
487 rational[1] = g_variant_new_uint64(q);
488 src->data = g_variant_new_tuple(rational, 2);
490 case SR_T_RATIONAL_VOLT:
491 if ((ret = sr_parse_voltage(value, &p, &q)) != SR_OK)
493 rational[0] = g_variant_new_uint64(p);
494 rational[1] = g_variant_new_uint64(q);
495 src->data = g_variant_new_tuple(rational, 2);
497 case SR_T_UINT64_RANGE:
498 if (sscanf(value, "%"PRIu64"-%"PRIu64, &low, &high) != 2) {
502 range[0] = g_variant_new_uint64(low);
503 range[1] = g_variant_new_uint64(high);
504 src->data = g_variant_new_tuple(range, 2);
514 int set_dev_options(struct sr_dev_inst *sdi, GHashTable *args)
516 struct sr_config src;
517 struct sr_probe_group *pg;
522 g_hash_table_iter_init(&iter, args);
523 while (g_hash_table_iter_next(&iter, &key, &value)) {
524 if ((ret = opt_to_gvar(key, value, &src)) != 0)
526 pg = select_probe_group(sdi);
527 ret = sr_config_set(sdi, pg, src.key, src.data);
529 g_critical("Failed to set device option '%s'.", (char *)key);
537 void run_session(void)
542 struct sr_dev_inst *sdi;
543 uint64_t min_samples, max_samples;
547 devices = device_scan();
549 g_critical("No devices found.");
552 if (g_slist_length(devices) > 1) {
553 g_critical("sigrok-cli only supports one device for capturing.");
559 sr_session_datafeed_callback_add(datafeed_in, NULL);
561 if (sr_dev_open(sdi) != SR_OK) {
562 g_critical("Failed to open device.");
566 if (sr_session_dev_add(sdi) != SR_OK) {
567 g_critical("Failed to add device to session.");
568 sr_session_destroy();
573 if ((devargs = parse_generic_arg(opt_config, FALSE))) {
574 if (set_dev_options(sdi, devargs) != SR_OK)
576 g_hash_table_destroy(devargs);
580 if (select_probes(sdi) != SR_OK) {
581 g_critical("Failed to set probes.");
582 sr_session_destroy();
587 if (!(triggerlist = sr_parse_triggerstring(sdi, opt_triggers))) {
588 sr_session_destroy();
591 max_probes = g_slist_length(sdi->probes);
592 for (i = 0; i < max_probes; i++) {
593 if (triggerlist[i]) {
594 sr_dev_trigger_set(sdi, i, triggerlist[i]);
595 g_free(triggerlist[i]);
601 if (opt_continuous) {
602 if (!sr_dev_has_option(sdi, SR_CONF_CONTINUOUS)) {
603 g_critical("This device does not support continuous sampling.");
604 sr_session_destroy();
610 if (set_limit_time(sdi) != SR_OK) {
611 sr_session_destroy();
617 if ((sr_parse_sizestring(opt_samples, &limit_samples) != SR_OK)) {
618 g_critical("Invalid sample limit '%s'.", opt_samples);
619 sr_session_destroy();
622 if (sr_config_list(sdi->driver, sdi, NULL,
623 SR_CONF_LIMIT_SAMPLES, &gvar) == SR_OK) {
624 /* The device has no compression, or compression is turned
625 * off, and publishes its sample memory size. */
626 g_variant_get(gvar, "(tt)", &min_samples, &max_samples);
627 g_variant_unref(gvar);
628 if (limit_samples < min_samples) {
629 g_critical("The device stores at least %"PRIu64
630 " samples with the current settings.", min_samples);
632 if (limit_samples > max_samples) {
633 g_critical("The device can store only %"PRIu64
634 " samples with the current settings.", max_samples);
637 gvar = g_variant_new_uint64(limit_samples);
638 if (sr_config_set(sdi, NULL, SR_CONF_LIMIT_SAMPLES, gvar) != SR_OK) {
639 g_critical("Failed to configure sample limit.");
640 sr_session_destroy();
646 if ((sr_parse_sizestring(opt_frames, &limit_frames) != SR_OK)) {
647 g_critical("Invalid sample limit '%s'.", opt_samples);
648 sr_session_destroy();
651 gvar = g_variant_new_uint64(limit_frames);
652 if (sr_config_set(sdi, NULL, SR_CONF_LIMIT_FRAMES, gvar) != SR_OK) {
653 g_critical("Failed to configure frame limit.");
654 sr_session_destroy();
659 if (sr_session_start() != SR_OK) {
660 g_critical("Failed to start session.");
661 sr_session_destroy();
673 sr_session_datafeed_callback_remove_all();
674 sr_session_destroy();
675 g_slist_free(devices);