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 uint64_t limit_samples = 0;
31 static uint64_t limit_frames = 0;
33 extern gchar *opt_output_file;
34 extern gchar *opt_output_format;
35 extern gchar *opt_pds;
36 extern gboolean opt_wait_trigger;
37 extern gchar *opt_time;
38 extern gchar *opt_samples;
39 extern gchar *opt_frames;
40 extern gchar *opt_continuous;
41 extern gchar *opt_config;
42 extern gchar *opt_triggers;
44 extern struct srd_session *srd_sess;
47 static int set_limit_time(const struct sr_dev_inst *sdi)
53 if (!(time_msec = sr_parse_timestring(opt_time))) {
54 g_critical("Invalid time '%s'", opt_time);
58 if (sr_dev_has_option(sdi, SR_CONF_LIMIT_MSEC)) {
59 gvar = g_variant_new_uint64(time_msec);
60 if (sr_config_set(sdi, NULL, SR_CONF_LIMIT_MSEC, gvar) != SR_OK) {
61 g_critical("Failed to configure time limit.");
64 } else if (sr_dev_has_option(sdi, SR_CONF_SAMPLERATE)) {
65 /* Convert to samples based on the samplerate. */
66 sr_config_get(sdi->driver, sdi, NULL, SR_CONF_SAMPLERATE, &gvar);
67 samplerate = g_variant_get_uint64(gvar);
68 g_variant_unref(gvar);
69 limit_samples = (samplerate) * time_msec / (uint64_t)1000;
70 if (limit_samples == 0) {
71 g_critical("Not enough time at this samplerate.");
74 gvar = g_variant_new_uint64(limit_samples);
75 if (sr_config_set(sdi, NULL, SR_CONF_LIMIT_SAMPLES, gvar) != SR_OK) {
76 g_critical("Failed to configure time-based sample limit.");
80 g_critical("This device does not support time limits.");
87 int setup_output_format(void)
92 struct sr_output_format **outputs;
96 if (opt_output_format && !strcmp(opt_output_format, "sigrok")) {
97 /* Doesn't really exist as an output module - this is
98 * the session save mode. */
99 g_free(opt_output_format);
100 opt_output_format = NULL;
103 if (!opt_output_format) {
104 opt_output_format = DEFAULT_OUTPUT_FORMAT;
105 /* we'll need to remember this so when saving to a file
106 * later, sigrok session format will be used.
108 default_output_format = TRUE;
111 fmtargs = parse_generic_arg(opt_output_format, TRUE);
112 fmtspec = g_hash_table_lookup(fmtargs, "sigrok_key");
114 g_critical("Invalid output format.");
117 outputs = sr_output_list();
118 for (i = 0; outputs[i]; i++) {
119 if (strcmp(outputs[i]->id, fmtspec))
121 g_hash_table_remove(fmtargs, "sigrok_key");
122 output_format = outputs[i];
123 g_hash_table_iter_init(&iter, fmtargs);
124 while (g_hash_table_iter_next(&iter, &key, &value)) {
125 /* only supporting one parameter per output module
126 * for now, and only its value */
127 output_format_param = g_strdup(value);
132 if (!output_format) {
133 g_critical("Invalid output format %s.", opt_output_format);
136 g_hash_table_destroy(fmtargs);
141 void datafeed_in(const struct sr_dev_inst *sdi,
142 const struct sr_datafeed_packet *packet, void *cb_data)
144 const struct sr_datafeed_meta *meta;
145 const struct sr_datafeed_logic *logic;
146 const struct sr_datafeed_analog *analog;
147 struct sr_config *src;
148 struct sr_channel *ch;
149 static struct sr_output *o = NULL;
150 static uint64_t rcvd_samples_logic = 0;
151 static uint64_t rcvd_samples_analog = 0;
152 static uint64_t samplerate = 0;
153 static int triggered = 0;
154 static FILE *outfile = NULL;
159 uint64_t output_len, input_len;
166 /* If the first packet to come in isn't a header, don't even try. */
167 if (packet->type != SR_DF_HEADER && o == NULL)
170 switch (packet->type) {
172 g_debug("cli: Received SR_DF_HEADER");
173 /* Initialize the output module. */
174 if (!(o = g_try_malloc(sizeof(struct sr_output)))) {
175 g_critical("Output module malloc failed.");
178 o->format = output_format;
179 o->sdi = (struct sr_dev_inst *)sdi;
180 o->param = output_format_param;
181 if (o->format->init) {
182 if (o->format->init(o) != SR_OK) {
183 g_critical("Output format initialization failed.");
188 /* Prepare non-stdout output. */
190 if (opt_output_file) {
191 if (default_output_format) {
194 /* saving to a file in whatever format was set
195 * with --format, so all we need is a filehandle */
196 outfile = g_fopen(opt_output_file, "wb");
199 rcvd_samples_logic = rcvd_samples_analog = 0;
201 if (sr_config_get(sdi->driver, sdi, NULL, SR_CONF_SAMPLERATE,
203 samplerate = g_variant_get_uint64(gvar);
204 g_variant_unref(gvar);
210 if (srd_session_metadata_set(srd_sess, SRD_CONF_SAMPLERATE,
211 g_variant_new_uint64(samplerate)) != SRD_OK) {
212 g_critical("Failed to configure decode session.");
216 if (srd_session_start(srd_sess) != SRD_OK) {
217 g_critical("Failed to start decode session.");
225 g_debug("cli: received SR_DF_META");
226 meta = packet->payload;
227 for (l = meta->config; l; l = l->next) {
230 case SR_CONF_SAMPLERATE:
231 samplerate = g_variant_get_uint64(src->data);
232 g_debug("cli: got samplerate %"PRIu64" Hz", samplerate);
235 if (srd_session_metadata_set(srd_sess, SRD_CONF_SAMPLERATE,
236 g_variant_new_uint64(samplerate)) != SRD_OK) {
237 g_critical("Failed to pass samplerate to decoder.");
242 case SR_CONF_SAMPLE_INTERVAL:
243 samplerate = g_variant_get_uint64(src->data);
244 g_debug("cli: got sample interval %"PRIu64" ms", samplerate);
247 /* Unknown metadata is not an error. */
254 g_debug("cli: received SR_DF_TRIGGER");
255 if (o->format->event)
256 o->format->event(o, SR_DF_TRIGGER, &output_buf,
262 logic = packet->payload;
263 g_message("cli: received SR_DF_LOGIC, %"PRIu64" bytes", logic->length);
264 if (logic->length == 0)
267 /* Don't store any samples until triggered. */
268 if (opt_wait_trigger && !triggered)
271 if (limit_samples && rcvd_samples_logic >= limit_samples)
274 end_sample = rcvd_samples_logic + logic->length / logic->unitsize;
275 /* Cut off last packet according to the sample limit. */
276 if (limit_samples && end_sample > limit_samples)
277 end_sample = limit_samples;
278 input_len = (end_sample - rcvd_samples_logic) * logic->unitsize;
280 if (opt_output_file && default_output_format) {
281 /* Saving to a session file. */
282 if (rcvd_samples_logic == 0) {
283 /* First packet with logic data, init session file. */
284 channels = g_malloc(sizeof(char *) * g_slist_length(sdi->channels));
285 for (i = 0, l = sdi->channels; l; l = l->next) {
287 if (ch->enabled && ch->type == SR_CHANNEL_LOGIC)
288 channels[i++] = ch->name;
291 sr_session_save_init(opt_output_file, samplerate,
295 save_chunk_logic(logic->data, input_len, logic->unitsize);
299 if (srd_session_send(srd_sess, rcvd_samples_logic, end_sample,
300 logic->data, input_len) != SRD_OK)
305 if (o->format->data && packet->type == o->format->df_type)
306 o->format->data(o, logic->data, input_len,
307 &output_buf, &output_len);
309 fwrite(output_buf, 1, output_len, outfile);
316 rcvd_samples_logic = end_sample;
320 analog = packet->payload;
321 g_message("cli: received SR_DF_ANALOG, %d samples", analog->num_samples);
322 if (analog->num_samples == 0)
325 if (limit_samples && rcvd_samples_analog >= limit_samples)
328 if (o->format->data && packet->type == o->format->df_type) {
329 o->format->data(o, (const uint8_t *)analog->data,
330 analog->num_samples * sizeof(float),
331 &output_buf, &output_len);
333 fwrite(output_buf, 1, output_len, outfile);
339 rcvd_samples_analog += analog->num_samples;
342 case SR_DF_FRAME_BEGIN:
343 g_debug("cli: received SR_DF_FRAME_BEGIN");
344 if (o->format->event) {
345 o->format->event(o, SR_DF_FRAME_BEGIN, &output_buf,
348 fwrite(output_buf, 1, output_len, outfile);
355 case SR_DF_FRAME_END:
356 g_debug("cli: received SR_DF_FRAME_END");
357 if (o->format->event) {
358 o->format->event(o, SR_DF_FRAME_END, &output_buf,
361 fwrite(output_buf, 1, output_len, outfile);
372 if (o && o->format->receive) {
373 if (o->format->receive(o, sdi, packet, &out) == SR_OK && out) {
374 fwrite(out->str, 1, out->len, outfile);
376 g_string_free(out, TRUE);
380 /* SR_DF_END needs to be handled after the output module's receive()
381 * is called, so it can properly clean up that module. */
382 if (packet->type == SR_DF_END) {
383 g_debug("cli: Received SR_DF_END");
385 if (o->format->event) {
386 o->format->event(o, SR_DF_END, &output_buf, &output_len);
389 fwrite(output_buf, 1, output_len, outfile);
395 if (o->format->cleanup)
396 o->format->cleanup(o);
400 if (outfile && outfile != stdout)
403 if (opt_output_file && default_output_format)
404 /* Flush whatever is left out to the session file. */
405 save_chunk_logic(NULL, 0, 0);
408 if (rcvd_samples_logic > 0 && rcvd_samples_logic < limit_samples)
409 g_warning("Device only sent %" PRIu64 " samples.",
411 else if (rcvd_samples_analog > 0 && rcvd_samples_analog < limit_samples)
412 g_warning("Device only sent %" PRIu64 " samples.",
413 rcvd_samples_analog);
419 int opt_to_gvar(char *key, char *value, struct sr_config *src)
421 const struct sr_config_info *srci;
422 double tmp_double, dlow, dhigh;
423 uint64_t tmp_u64, p, q, low, high;
424 GVariant *rational[2], *range[2];
428 if (!(srci = sr_config_info_name_get(key))) {
429 g_critical("Unknown device option '%s'.", (char *) key);
432 src->key = srci->key;
434 if ((value == NULL) &&
435 (srci->datatype != SR_T_BOOL)) {
436 g_critical("Option '%s' needs a value.", (char *)key);
441 switch (srci->datatype) {
443 ret = sr_parse_sizestring(value, &tmp_u64);
446 src->data = g_variant_new_uint64(tmp_u64);
449 ret = sr_parse_sizestring(value, &tmp_u64);
452 src->data = g_variant_new_int32(tmp_u64);
455 src->data = g_variant_new_string(value);
461 tmp_bool = sr_parse_boolstring(value);
462 src->data = g_variant_new_boolean(tmp_bool);
465 tmp_double = strtof(value, NULL);
466 src->data = g_variant_new_double(tmp_double);
468 case SR_T_RATIONAL_PERIOD:
469 if ((ret = sr_parse_period(value, &p, &q)) != SR_OK)
471 rational[0] = g_variant_new_uint64(p);
472 rational[1] = g_variant_new_uint64(q);
473 src->data = g_variant_new_tuple(rational, 2);
475 case SR_T_RATIONAL_VOLT:
476 if ((ret = sr_parse_voltage(value, &p, &q)) != SR_OK)
478 rational[0] = g_variant_new_uint64(p);
479 rational[1] = g_variant_new_uint64(q);
480 src->data = g_variant_new_tuple(rational, 2);
482 case SR_T_UINT64_RANGE:
483 if (sscanf(value, "%"PRIu64"-%"PRIu64, &low, &high) != 2) {
487 range[0] = g_variant_new_uint64(low);
488 range[1] = g_variant_new_uint64(high);
489 src->data = g_variant_new_tuple(range, 2);
492 case SR_T_DOUBLE_RANGE:
493 if (sscanf(value, "%lf-%lf", &dlow, &dhigh) != 2) {
497 range[0] = g_variant_new_double(dlow);
498 range[1] = g_variant_new_double(dhigh);
499 src->data = g_variant_new_tuple(range, 2);
509 int set_dev_options(struct sr_dev_inst *sdi, GHashTable *args)
511 struct sr_config src;
512 struct sr_channel_group *cg;
517 g_hash_table_iter_init(&iter, args);
518 while (g_hash_table_iter_next(&iter, &key, &value)) {
519 if ((ret = opt_to_gvar(key, value, &src)) != 0)
521 cg = select_channel_group(sdi);
522 ret = sr_config_set(sdi, cg, src.key, src.data);
524 g_critical("Failed to set device option '%s'.", (char *)key);
532 void run_session(void)
537 struct sr_dev_inst *sdi;
538 uint64_t min_samples, max_samples;
542 devices = device_scan();
544 g_critical("No devices found.");
547 if (g_slist_length(devices) > 1) {
548 g_critical("sigrok-cli only supports one device for capturing.");
554 sr_session_datafeed_callback_add(datafeed_in, NULL);
556 if (sr_dev_open(sdi) != SR_OK) {
557 g_critical("Failed to open device.");
561 if (sr_session_dev_add(sdi) != SR_OK) {
562 g_critical("Failed to add device to session.");
563 sr_session_destroy();
568 if ((devargs = parse_generic_arg(opt_config, FALSE))) {
569 if (set_dev_options(sdi, devargs) != SR_OK)
571 g_hash_table_destroy(devargs);
575 if (select_channels(sdi) != SR_OK) {
576 g_critical("Failed to set channels.");
577 sr_session_destroy();
582 if (!(triggerlist = sr_parse_triggerstring(sdi, opt_triggers))) {
583 sr_session_destroy();
586 max_channels = g_slist_length(sdi->channels);
587 for (i = 0; i < max_channels; i++) {
588 if (triggerlist[i]) {
589 sr_dev_trigger_set(sdi, i, triggerlist[i]);
590 g_free(triggerlist[i]);
596 if (opt_continuous) {
597 if (!sr_dev_has_option(sdi, SR_CONF_CONTINUOUS)) {
598 g_critical("This device does not support continuous sampling.");
599 sr_session_destroy();
605 if (set_limit_time(sdi) != SR_OK) {
606 sr_session_destroy();
612 if ((sr_parse_sizestring(opt_samples, &limit_samples) != SR_OK)) {
613 g_critical("Invalid sample limit '%s'.", opt_samples);
614 sr_session_destroy();
617 if (sr_config_list(sdi->driver, sdi, NULL,
618 SR_CONF_LIMIT_SAMPLES, &gvar) == SR_OK) {
619 /* The device has no compression, or compression is turned
620 * off, and publishes its sample memory size. */
621 g_variant_get(gvar, "(tt)", &min_samples, &max_samples);
622 g_variant_unref(gvar);
623 if (limit_samples < min_samples) {
624 g_critical("The device stores at least %"PRIu64
625 " samples with the current settings.", min_samples);
627 if (limit_samples > max_samples) {
628 g_critical("The device can store only %"PRIu64
629 " samples with the current settings.", max_samples);
632 gvar = g_variant_new_uint64(limit_samples);
633 if (sr_config_set(sdi, NULL, SR_CONF_LIMIT_SAMPLES, gvar) != SR_OK) {
634 g_critical("Failed to configure sample limit.");
635 sr_session_destroy();
641 if ((sr_parse_sizestring(opt_frames, &limit_frames) != SR_OK)) {
642 g_critical("Invalid sample limit '%s'.", opt_samples);
643 sr_session_destroy();
646 gvar = g_variant_new_uint64(limit_frames);
647 if (sr_config_set(sdi, NULL, SR_CONF_LIMIT_FRAMES, gvar) != SR_OK) {
648 g_critical("Failed to configure frame limit.");
649 sr_session_destroy();
654 if (sr_session_start() != SR_OK) {
655 g_critical("Failed to start session.");
656 sr_session_destroy();
668 sr_session_datafeed_callback_remove_all();
669 sr_session_destroy();
670 g_slist_free(devices);
674 void save_chunk_logic(uint8_t *data, uint64_t data_len, int unitsize)
676 static uint8_t *buf = NULL;
677 static int buf_len = 0;
678 static int last_unitsize = 0;
682 buf = g_malloc(SAVE_CHUNK_SIZE);
684 if (buf_len + data_len > SAVE_CHUNK_SIZE) {
685 max = (SAVE_CHUNK_SIZE - buf_len) / unitsize * unitsize;
686 memcpy(buf + buf_len, data, max);
687 sr_session_append(opt_output_file, buf, unitsize,
688 (buf_len + max) / unitsize);
689 memcpy(buf, data + max, data_len - max);
690 buf_len = data_len - max;
691 } else if (data_len == 0 && last_unitsize != 0) {
692 /* End of data, flush the buffer out. */
693 sr_session_append(opt_output_file, buf, last_unitsize,
694 buf_len / last_unitsize);
697 memcpy(buf + buf_len, data, data_len);
700 last_unitsize = unitsize;