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 int default_output_format = FALSE;
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)
40 if (!(time_msec = sr_parse_timestring(opt_time))) {
41 g_critical("Invalid time '%s'", opt_time);
45 if (sr_dev_has_option(sdi, SR_CONF_LIMIT_MSEC)) {
46 gvar = g_variant_new_uint64(time_msec);
47 if (sr_config_set(sdi, NULL, SR_CONF_LIMIT_MSEC, gvar) != SR_OK) {
48 g_critical("Failed to configure time limit.");
51 } else if (sr_dev_has_option(sdi, SR_CONF_SAMPLERATE)) {
52 /* Convert to samples based on the samplerate. */
53 sr_config_get(sdi->driver, sdi, NULL, SR_CONF_SAMPLERATE, &gvar);
54 samplerate = g_variant_get_uint64(gvar);
55 g_variant_unref(gvar);
56 limit_samples = (samplerate) * time_msec / (uint64_t)1000;
57 if (limit_samples == 0) {
58 g_critical("Not enough time at this samplerate.");
61 gvar = g_variant_new_uint64(limit_samples);
62 if (sr_config_set(sdi, NULL, SR_CONF_LIMIT_SAMPLES, gvar) != SR_OK) {
63 g_critical("Failed to configure time-based sample limit.");
67 g_critical("This device does not support time limits.");
74 GHashTable *generic_arg_to_opt(const struct sr_option *opts, GHashTable *genargs)
78 const struct sr_option *opt;
81 hash = g_hash_table_new_full(g_str_hash, g_str_equal, g_free,
82 (GDestroyNotify)g_variant_unref);
83 for (opt = opts; opt->id && opt->def; opt++) {
84 if (!(s = g_hash_table_lookup(genargs, opt->id)))
86 if (g_variant_is_of_type(opt->def, G_VARIANT_TYPE_UINT32)) {
87 gvar = g_variant_new_uint32(strtoul(s, NULL, 10));
88 g_hash_table_insert(hash, g_strdup(opt->id),
89 g_variant_ref_sink(gvar));
90 printf("opt %s value %s\n", opt->id, s);
92 g_critical("Don't know how to convert option '%s' to %s!",
93 opt->id, g_variant_get_type_string(opt->def));
100 const struct sr_output *setup_output_format(const struct sr_dev_inst *sdi)
102 const struct sr_output_module *omod;
103 const struct sr_option *opts;
104 const struct sr_output *o;
105 GHashTable *fmtargs, *fmtopts;
108 if (opt_output_format && !strcmp(opt_output_format, "sigrok")) {
109 /* Doesn't really exist as an output module - this is
110 * the session save mode. */
111 g_free(opt_output_format);
112 opt_output_format = NULL;
115 if (!opt_output_format) {
116 opt_output_format = DEFAULT_OUTPUT_FORMAT;
117 /* we'll need to remember this so when saving to a file
118 * later, sigrok session format will be used.
120 default_output_format = TRUE;
123 fmtargs = parse_generic_arg(opt_output_format, TRUE);
124 fmtspec = g_hash_table_lookup(fmtargs, "sigrok_key");
126 g_critical("Invalid output format.");
127 if (!(omod = sr_output_find(fmtspec)))
128 g_critical("Unknown output format '%s'.", fmtspec);
129 g_hash_table_remove(fmtargs, "sigrok_key");
130 if ((opts = sr_output_options_get(omod))) {
131 fmtopts = generic_arg_to_opt(opts, fmtargs);
132 sr_output_options_free(omod);
135 o = sr_output_new(omod, fmtopts, sdi);
137 g_hash_table_destroy(fmtopts);
138 g_hash_table_destroy(fmtargs);
143 void datafeed_in(const struct sr_dev_inst *sdi,
144 const struct sr_datafeed_packet *packet, void *cb_data)
146 const struct sr_datafeed_meta *meta;
147 const struct sr_datafeed_logic *logic;
148 const struct sr_datafeed_analog *analog;
149 struct sr_session *session;
150 struct sr_config *src;
151 struct sr_channel *ch;
152 static const struct sr_output *o = NULL;
153 static const struct sr_output *oa = NULL;
154 static uint64_t rcvd_samples_logic = 0;
155 static uint64_t rcvd_samples_analog = 0;
156 static uint64_t samplerate = 0;
157 static int triggered = 0;
158 static FILE *outfile = NULL;
167 /* If the first packet to come in isn't a header, don't even try. */
168 if (packet->type != SR_DF_HEADER && o == NULL)
172 switch (packet->type) {
174 g_debug("cli: Received SR_DF_HEADER.");
175 o = setup_output_format(sdi);
177 /* Set up backup analog output module. */
178 oa = sr_output_new(sr_output_find("analog"), NULL, sdi);
180 /* Prepare non-stdout output. */
182 if (opt_output_file) {
183 if (default_output_format) {
186 /* saving to a file in whatever format was set
187 * with --format, so all we need is a filehandle */
188 outfile = g_fopen(opt_output_file, "wb");
191 rcvd_samples_logic = rcvd_samples_analog = 0;
193 if (sr_config_get(sdi->driver, sdi, NULL, SR_CONF_SAMPLERATE,
195 samplerate = g_variant_get_uint64(gvar);
196 g_variant_unref(gvar);
202 if (srd_session_metadata_set(srd_sess, SRD_CONF_SAMPLERATE,
203 g_variant_new_uint64(samplerate)) != SRD_OK) {
204 g_critical("Failed to configure decode session.");
208 if (srd_session_start(srd_sess) != SRD_OK) {
209 g_critical("Failed to start decode session.");
217 g_debug("cli: Received SR_DF_META.");
218 meta = packet->payload;
219 for (l = meta->config; l; l = l->next) {
222 case SR_CONF_SAMPLERATE:
223 samplerate = g_variant_get_uint64(src->data);
224 g_debug("cli: Got samplerate %"PRIu64" Hz.", samplerate);
227 if (srd_session_metadata_set(srd_sess, SRD_CONF_SAMPLERATE,
228 g_variant_new_uint64(samplerate)) != SRD_OK) {
229 g_critical("Failed to pass samplerate to decoder.");
234 case SR_CONF_SAMPLE_INTERVAL:
235 samplerate = g_variant_get_uint64(src->data);
236 g_debug("cli: Got sample interval %"PRIu64" ms.", samplerate);
239 /* Unknown metadata is not an error. */
246 g_debug("cli: Received SR_DF_TRIGGER.");
251 logic = packet->payload;
252 g_message("cli: Received SR_DF_LOGIC (%"PRIu64" bytes, unitsize = %d).",
253 logic->length, logic->unitsize);
254 if (logic->length == 0)
257 /* Don't store any samples until triggered. */
258 if (opt_wait_trigger && !triggered)
261 if (limit_samples && rcvd_samples_logic >= limit_samples)
264 end_sample = rcvd_samples_logic + logic->length / logic->unitsize;
265 /* Cut off last packet according to the sample limit. */
266 if (limit_samples && end_sample > limit_samples)
267 end_sample = limit_samples;
268 input_len = (end_sample - rcvd_samples_logic) * logic->unitsize;
270 if (opt_output_file && default_output_format) {
271 /* Saving to a session file. */
272 if (rcvd_samples_logic == 0) {
273 /* First packet with logic data, init session file. */
274 channels = g_malloc(sizeof(char *) * g_slist_length(sdi->channels));
275 for (i = 0, l = sdi->channels; l; l = l->next) {
277 if (ch->enabled && ch->type == SR_CHANNEL_LOGIC)
278 channels[i++] = ch->name;
281 sr_session_save_init(session, opt_output_file,
282 samplerate, channels);
285 save_chunk_logic(session, logic->data, input_len, logic->unitsize);
289 if (srd_session_send(srd_sess, rcvd_samples_logic, end_sample,
290 logic->data, input_len) != SRD_OK)
291 sr_session_stop(session);
296 rcvd_samples_logic = end_sample;
300 analog = packet->payload;
301 g_message("cli: Received SR_DF_ANALOG (%d samples).", analog->num_samples);
302 if (analog->num_samples == 0)
305 if (limit_samples && rcvd_samples_analog >= limit_samples)
308 rcvd_samples_analog += analog->num_samples;
311 case SR_DF_FRAME_BEGIN:
312 g_debug("cli: Received SR_DF_FRAME_BEGIN.");
315 case SR_DF_FRAME_END:
316 g_debug("cli: Received SR_DF_FRAME_END.");
323 if (o && outfile && !opt_pds) {
324 if (sr_output_send(o, packet, &out) == SR_OK) {
325 if (!out || (out->len == 0 && default_output_format
326 && packet->type == SR_DF_ANALOG)) {
327 /* The user didn't specify an output module,
328 * but needs to see this analog data. */
329 sr_output_send(oa, packet, &out);
331 if (out && out->len > 0) {
332 fwrite(out->str, 1, out->len, outfile);
336 g_string_free(out, TRUE);
340 /* SR_DF_END needs to be handled after the output module's receive()
341 * is called, so it can properly clean up that module. */
342 if (packet->type == SR_DF_END) {
343 g_debug("cli: Received SR_DF_END.");
352 if (outfile && outfile != stdout)
355 if (opt_output_file && default_output_format)
356 /* Flush whatever is left out to the session file. */
357 save_chunk_logic(session, NULL, 0, 0);
360 if (rcvd_samples_logic > 0 && rcvd_samples_logic < limit_samples)
361 g_warning("Device only sent %" PRIu64 " samples.",
363 else if (rcvd_samples_analog > 0 && rcvd_samples_analog < limit_samples)
364 g_warning("Device only sent %" PRIu64 " samples.",
365 rcvd_samples_analog);
371 int opt_to_gvar(char *key, char *value, struct sr_config *src)
373 const struct sr_config_info *srci;
374 double tmp_double, dlow, dhigh;
375 uint64_t tmp_u64, p, q, low, high;
376 GVariant *rational[2], *range[2];
380 if (!(srci = sr_config_info_name_get(key))) {
381 g_critical("Unknown device option '%s'.", (char *) key);
384 src->key = srci->key;
386 if ((value == NULL) &&
387 (srci->datatype != SR_T_BOOL)) {
388 g_critical("Option '%s' needs a value.", (char *)key);
393 switch (srci->datatype) {
395 ret = sr_parse_sizestring(value, &tmp_u64);
398 src->data = g_variant_new_uint64(tmp_u64);
401 ret = sr_parse_sizestring(value, &tmp_u64);
404 src->data = g_variant_new_int32(tmp_u64);
407 src->data = g_variant_new_string(value);
413 tmp_bool = sr_parse_boolstring(value);
414 src->data = g_variant_new_boolean(tmp_bool);
417 tmp_double = strtof(value, NULL);
418 src->data = g_variant_new_double(tmp_double);
420 case SR_T_RATIONAL_PERIOD:
421 if ((ret = sr_parse_period(value, &p, &q)) != SR_OK)
423 rational[0] = g_variant_new_uint64(p);
424 rational[1] = g_variant_new_uint64(q);
425 src->data = g_variant_new_tuple(rational, 2);
427 case SR_T_RATIONAL_VOLT:
428 if ((ret = sr_parse_voltage(value, &p, &q)) != SR_OK)
430 rational[0] = g_variant_new_uint64(p);
431 rational[1] = g_variant_new_uint64(q);
432 src->data = g_variant_new_tuple(rational, 2);
434 case SR_T_UINT64_RANGE:
435 if (sscanf(value, "%"PRIu64"-%"PRIu64, &low, &high) != 2) {
439 range[0] = g_variant_new_uint64(low);
440 range[1] = g_variant_new_uint64(high);
441 src->data = g_variant_new_tuple(range, 2);
444 case SR_T_DOUBLE_RANGE:
445 if (sscanf(value, "%lf-%lf", &dlow, &dhigh) != 2) {
449 range[0] = g_variant_new_double(dlow);
450 range[1] = g_variant_new_double(dhigh);
451 src->data = g_variant_new_tuple(range, 2);
461 int set_dev_options(struct sr_dev_inst *sdi, GHashTable *args)
463 struct sr_config src;
464 struct sr_channel_group *cg;
469 g_hash_table_iter_init(&iter, args);
470 while (g_hash_table_iter_next(&iter, &key, &value)) {
471 if ((ret = opt_to_gvar(key, value, &src)) != 0)
473 cg = select_channel_group(sdi);
474 ret = sr_config_set(sdi, cg, src.key, src.data);
476 g_critical("Failed to set device option '%s'.", (char *)key);
484 void run_session(void)
489 struct sr_session *session;
490 struct sr_trigger *trigger;
491 struct sr_dev_inst *sdi;
492 uint64_t min_samples, max_samples;
494 devices = device_scan();
496 g_critical("No devices found.");
499 if (g_slist_length(devices) > 1) {
500 g_critical("sigrok-cli only supports one device for capturing.");
501 g_slist_free(devices);
505 g_slist_free(devices);
507 sr_session_new(&session);
508 sr_session_datafeed_callback_add(session, datafeed_in, NULL);
510 if (sr_dev_open(sdi) != SR_OK) {
511 g_critical("Failed to open device.");
515 if (sr_session_dev_add(session, sdi) != SR_OK) {
516 g_critical("Failed to add device to session.");
517 sr_session_destroy(session);
522 if ((devargs = parse_generic_arg(opt_config, FALSE))) {
523 if (set_dev_options(sdi, devargs) != SR_OK)
525 g_hash_table_destroy(devargs);
529 if (select_channels(sdi) != SR_OK) {
530 g_critical("Failed to set channels.");
531 sr_session_destroy(session);
536 if (!parse_triggerstring(sdi, opt_triggers, &trigger)) {
537 sr_session_destroy(session);
540 if (sr_session_trigger_set(session, trigger) != SR_OK) {
541 sr_session_destroy(session);
546 if (opt_continuous) {
547 if (!sr_dev_has_option(sdi, SR_CONF_CONTINUOUS)) {
548 g_critical("This device does not support continuous sampling.");
549 sr_session_destroy(session);
555 if (set_limit_time(sdi) != SR_OK) {
556 sr_session_destroy(session);
562 if ((sr_parse_sizestring(opt_samples, &limit_samples) != SR_OK)) {
563 g_critical("Invalid sample limit '%s'.", opt_samples);
564 sr_session_destroy(session);
567 if (sr_config_list(sdi->driver, sdi, NULL,
568 SR_CONF_LIMIT_SAMPLES, &gvar) == SR_OK) {
569 /* The device has no compression, or compression is turned
570 * off, and publishes its sample memory size. */
571 g_variant_get(gvar, "(tt)", &min_samples, &max_samples);
572 g_variant_unref(gvar);
573 if (limit_samples < min_samples) {
574 g_critical("The device stores at least %"PRIu64
575 " samples with the current settings.", min_samples);
577 if (limit_samples > max_samples) {
578 g_critical("The device can store only %"PRIu64
579 " samples with the current settings.", max_samples);
582 gvar = g_variant_new_uint64(limit_samples);
583 if (sr_config_set(sdi, NULL, SR_CONF_LIMIT_SAMPLES, gvar) != SR_OK) {
584 g_critical("Failed to configure sample limit.");
585 sr_session_destroy(session);
591 if ((sr_parse_sizestring(opt_frames, &limit_frames) != SR_OK)) {
592 g_critical("Invalid sample limit '%s'.", opt_samples);
593 sr_session_destroy(session);
596 gvar = g_variant_new_uint64(limit_frames);
597 if (sr_config_set(sdi, NULL, SR_CONF_LIMIT_FRAMES, gvar) != SR_OK) {
598 g_critical("Failed to configure frame limit.");
599 sr_session_destroy(session);
604 if (sr_session_start(session) != SR_OK) {
605 g_critical("Failed to start session.");
606 sr_session_destroy(session);
613 sr_session_run(session);
618 sr_session_datafeed_callback_remove_all(session);
619 sr_session_destroy(session);
623 void save_chunk_logic(struct sr_session *session, uint8_t *data,
624 uint64_t data_len, int unitsize)
626 static uint8_t *buf = NULL;
627 static int buf_len = 0;
628 static int last_unitsize = 0;
632 buf = g_malloc(SAVE_CHUNK_SIZE);
634 while (data_len > SAVE_CHUNK_SIZE) {
635 save_chunk_logic(session, data, SAVE_CHUNK_SIZE, unitsize);
636 data += SAVE_CHUNK_SIZE;
637 data_len -= SAVE_CHUNK_SIZE;
640 if (buf_len + data_len > SAVE_CHUNK_SIZE) {
641 max = (SAVE_CHUNK_SIZE - buf_len) / unitsize * unitsize;
642 memcpy(buf + buf_len, data, max);
643 sr_session_append(session, opt_output_file, buf, unitsize,
644 (buf_len + max) / unitsize);
645 memcpy(buf, data + max, data_len - max);
646 buf_len = data_len - max;
647 } else if (data_len == 0 && last_unitsize != 0) {
648 /* End of data, flush the buffer out. */
649 sr_session_append(session, opt_output_file, buf, last_unitsize,
650 buf_len / last_unitsize);
653 memcpy(buf + buf_len, data, data_len);
656 last_unitsize = unitsize;