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>
23 #include <libsigrok/libsigrok.h>
25 #include <libsigrokdecode/libsigrokdecode.h> /* First, so we avoid a _POSIX_C_SOURCE warning. */
27 #include "sigrok-cli.h"
29 static struct sr_output_format *output_format = NULL;
30 static int default_output_format = FALSE;
31 static char *output_format_param = NULL;
32 static GByteArray *savebuf;
33 static uint64_t limit_samples = 0;
34 static uint64_t limit_frames = 0;
36 extern gchar *opt_output_file;
37 extern gchar *opt_output_format;
38 extern gchar *opt_pds;
39 extern gboolean opt_wait_trigger;
40 extern gchar *opt_time;
41 extern gchar *opt_samples;
42 extern gchar *opt_frames;
43 extern gchar *opt_continuous;
44 extern gchar *opt_config;
45 extern gchar *opt_triggers;
47 extern struct srd_session *srd_sess;
51 static GArray *get_enabled_logic_probes(const struct sr_dev_inst *sdi)
53 struct sr_probe *probe;
57 probes = g_array_new(FALSE, FALSE, sizeof(int));
58 for (l = sdi->probes; l; l = l->next) {
60 if (probe->type != SR_PROBE_LOGIC)
62 if (probe->enabled != TRUE)
64 g_array_append_val(probes, probe->index);
70 static int set_limit_time(const struct sr_dev_inst *sdi)
76 if (!(time_msec = sr_parse_timestring(opt_time))) {
77 g_critical("Invalid time '%s'", opt_time);
81 if (sr_dev_has_option(sdi, SR_CONF_LIMIT_MSEC)) {
82 gvar = g_variant_new_uint64(time_msec);
83 if (sr_config_set(sdi, NULL, SR_CONF_LIMIT_MSEC, gvar) != SR_OK) {
84 g_critical("Failed to configure time limit.");
87 } else if (sr_dev_has_option(sdi, SR_CONF_SAMPLERATE)) {
88 /* Convert to samples based on the samplerate. */
89 sr_config_get(sdi->driver, sdi, NULL, SR_CONF_SAMPLERATE, &gvar);
90 samplerate = g_variant_get_uint64(gvar);
91 g_variant_unref(gvar);
92 limit_samples = (samplerate) * time_msec / (uint64_t)1000;
93 if (limit_samples == 0) {
94 g_critical("Not enough time at this samplerate.");
97 gvar = g_variant_new_uint64(limit_samples);
98 if (sr_config_set(sdi, NULL, SR_CONF_LIMIT_SAMPLES, gvar) != SR_OK) {
99 g_critical("Failed to configure time-based sample limit.");
103 g_critical("This device does not support time limits.");
110 int setup_output_format(void)
115 struct sr_output_format **outputs;
119 if (opt_output_format && !strcmp(opt_output_format, "sigrok")) {
120 /* Doesn't really exist as an output module - this is
121 * the session save mode. */
122 g_free(opt_output_format);
123 opt_output_format = NULL;
126 if (!opt_output_format) {
127 opt_output_format = DEFAULT_OUTPUT_FORMAT;
128 /* we'll need to remember this so when saving to a file
129 * later, sigrok session format will be used.
131 default_output_format = TRUE;
134 fmtargs = parse_generic_arg(opt_output_format, TRUE);
135 fmtspec = g_hash_table_lookup(fmtargs, "sigrok_key");
137 g_critical("Invalid output format.");
140 outputs = sr_output_list();
141 for (i = 0; outputs[i]; i++) {
142 if (strcmp(outputs[i]->id, fmtspec))
144 g_hash_table_remove(fmtargs, "sigrok_key");
145 output_format = outputs[i];
146 g_hash_table_iter_init(&iter, fmtargs);
147 while (g_hash_table_iter_next(&iter, &key, &value)) {
148 /* only supporting one parameter per output module
149 * for now, and only its value */
150 output_format_param = g_strdup(value);
155 if (!output_format) {
156 g_critical("Invalid output format %s.", opt_output_format);
159 g_hash_table_destroy(fmtargs);
164 void datafeed_in(const struct sr_dev_inst *sdi,
165 const struct sr_datafeed_packet *packet, void *cb_data)
167 const struct sr_datafeed_meta *meta;
168 const struct sr_datafeed_logic *logic;
169 const struct sr_datafeed_analog *analog;
170 struct sr_config *src;
171 static struct sr_output *o = NULL;
172 static GArray *logic_probelist = NULL;
173 static uint64_t received_samples = 0;
174 static int unitsize = 0;
175 static int triggered = 0;
176 static FILE *outfile = NULL;
179 int sample_size, ret;
180 uint64_t samplerate, output_len, filter_out_len, end_sample;
181 uint8_t *output_buf, *filter_out;
185 /* If the first packet to come in isn't a header, don't even try. */
186 if (packet->type != SR_DF_HEADER && o == NULL)
190 switch (packet->type) {
192 g_debug("cli: Received SR_DF_HEADER");
193 /* Initialize the output module. */
194 if (!(o = g_try_malloc(sizeof(struct sr_output)))) {
195 g_critical("Output module malloc failed.");
198 o->format = output_format;
199 o->sdi = (struct sr_dev_inst *)sdi;
200 o->param = output_format_param;
201 if (o->format->init) {
202 if (o->format->init(o) != SR_OK) {
203 g_critical("Output format initialization failed.");
208 /* Prepare non-stdout output. */
210 if (opt_output_file) {
211 if (default_output_format) {
212 /* output file is in session format, so we'll
213 * keep a copy of everything as it comes in
214 * and save from there after the session. */
216 savebuf = g_byte_array_new();
218 /* saving to a file in whatever format was set
219 * with --format, so all we need is a filehandle */
220 outfile = g_fopen(opt_output_file, "wb");
224 /* Prepare for logic data. */
225 logic_probelist = get_enabled_logic_probes(sdi);
226 /* How many bytes we need to store the packed samples. */
227 unitsize = (logic_probelist->len + 7) / 8;
231 if (opt_pds && logic_probelist->len) {
232 if (sr_config_get(sdi->driver, sdi, NULL, SR_CONF_SAMPLERATE,
234 samplerate = g_variant_get_uint64(gvar);
235 g_variant_unref(gvar);
236 if (srd_session_metadata_set(srd_sess, SRD_CONF_SAMPLERATE,
237 g_variant_new_uint64(samplerate)) != SRD_OK) {
238 g_critical("Failed to configure decode session.");
242 if (srd_session_start(srd_sess) != SRD_OK) {
243 g_critical("Failed to start decode session.");
251 g_debug("cli: received SR_DF_META");
252 meta = packet->payload;
253 for (l = meta->config; l; l = l->next) {
256 case SR_CONF_SAMPLERATE:
257 samplerate = g_variant_get_uint64(src->data);
258 g_debug("cli: got samplerate %"PRIu64" Hz", samplerate);
261 if (srd_session_metadata_set(srd_sess, SRD_CONF_SAMPLERATE,
262 g_variant_new_uint64(samplerate)) != SRD_OK) {
263 g_critical("Failed to pass samplerate to decoder.");
268 case SR_CONF_SAMPLE_INTERVAL:
269 samplerate = g_variant_get_uint64(src->data);
270 g_debug("cli: got sample interval %"PRIu64" ms", samplerate);
273 /* Unknown metadata is not an error. */
280 g_debug("cli: received SR_DF_TRIGGER");
281 if (o->format->event)
282 o->format->event(o, SR_DF_TRIGGER, &output_buf,
288 logic = packet->payload;
289 g_message("cli: received SR_DF_LOGIC, %"PRIu64" bytes", logic->length);
290 sample_size = logic->unitsize;
291 if (logic->length == 0)
294 /* Don't store any samples until triggered. */
295 if (opt_wait_trigger && !triggered)
298 if (limit_samples && received_samples >= limit_samples)
301 ret = sr_filter_probes(sample_size, unitsize, logic_probelist,
302 logic->data, logic->length,
303 &filter_out, &filter_out_len);
308 * What comes out of the filter is guaranteed to be packed into the
309 * minimum size needed to support the number of samples at this sample
310 * size. however, the driver may have submitted too much. Cut off
311 * the buffer of the last packet according to the sample limit.
313 if (limit_samples && (received_samples + logic->length / sample_size >
314 limit_samples * sample_size))
315 filter_out_len = limit_samples * sample_size - received_samples;
317 if (opt_output_file && default_output_format) {
318 /* Saving to a session file. */
319 g_byte_array_append(savebuf, filter_out, filter_out_len);
323 end_sample = received_samples + filter_out_len / unitsize;
324 if (srd_session_send(srd_sess, received_samples, end_sample,
325 (uint8_t*)filter_out, filter_out_len) != SRD_OK)
330 if (o->format->data && packet->type == o->format->df_type)
331 o->format->data(o, filter_out, filter_out_len,
332 &output_buf, &output_len);
334 fwrite(output_buf, 1, output_len, outfile);
342 received_samples += logic->length / sample_size;
346 analog = packet->payload;
347 g_message("cli: received SR_DF_ANALOG, %d samples", analog->num_samples);
348 if (analog->num_samples == 0)
351 if (limit_samples && received_samples >= limit_samples)
354 if (o->format->data && packet->type == o->format->df_type) {
355 o->format->data(o, (const uint8_t *)analog->data,
356 analog->num_samples * sizeof(float),
357 &output_buf, &output_len);
359 fwrite(output_buf, 1, output_len, outfile);
365 received_samples += analog->num_samples;
368 case SR_DF_FRAME_BEGIN:
369 g_debug("cli: received SR_DF_FRAME_BEGIN");
370 if (o->format->event) {
371 o->format->event(o, SR_DF_FRAME_BEGIN, &output_buf,
374 fwrite(output_buf, 1, output_len, outfile);
381 case SR_DF_FRAME_END:
382 g_debug("cli: received SR_DF_FRAME_END");
383 if (o->format->event) {
384 o->format->event(o, SR_DF_FRAME_END, &output_buf,
387 fwrite(output_buf, 1, output_len, outfile);
398 if (o && o->format->receive) {
399 if (o->format->receive(o, sdi, packet, &out) == SR_OK && out) {
400 fwrite(out->str, 1, out->len, outfile);
402 g_string_free(out, TRUE);
406 /* SR_DF_END needs to be handled after the output module's receive()
407 * is called, so it can properly clean up that module etc. */
408 if (packet->type == SR_DF_END) {
409 g_debug("cli: Received SR_DF_END");
411 if (o->format->event) {
412 o->format->event(o, SR_DF_END, &output_buf, &output_len);
415 fwrite(output_buf, 1, output_len, outfile);
421 if (limit_samples && received_samples < limit_samples)
422 g_warning("Device only sent %" PRIu64 " samples.",
426 g_warning("Device stopped after %" PRIu64 " samples.",
429 g_array_free(logic_probelist, TRUE);
431 if (o->format->cleanup)
432 o->format->cleanup(o);
436 if (outfile && outfile != stdout)
439 if (opt_output_file && default_output_format && savebuf->len) {
440 if (sr_session_save(opt_output_file, sdi, savebuf->data,
441 unitsize, savebuf->len / unitsize) != SR_OK)
442 g_critical("Failed to save session.");
443 g_byte_array_free(savebuf, TRUE);
449 int set_dev_options(struct sr_dev_inst *sdi, GHashTable *args)
451 const struct sr_config_info *srci;
452 struct sr_probe_group *pg;
457 uint64_t tmp_u64, p, q, low, high;
459 GVariant *val, *rational[2], *range[2];
461 g_hash_table_iter_init(&iter, args);
462 while (g_hash_table_iter_next(&iter, &key, &value)) {
463 if (!(srci = sr_config_info_name_get(key))) {
464 g_critical("Unknown device option '%s'.", (char *) key);
468 if ((value == NULL) &&
469 (srci->datatype != SR_T_BOOL)) {
470 g_critical("Option '%s' needs a value.", (char *)key);
474 switch (srci->datatype) {
476 ret = sr_parse_sizestring(value, &tmp_u64);
479 val = g_variant_new_uint64(tmp_u64);
482 val = g_variant_new_string(value);
488 tmp_bool = sr_parse_boolstring(value);
489 val = g_variant_new_boolean(tmp_bool);
492 tmp_double = strtof(value, NULL);
493 val = g_variant_new_double(tmp_double);
495 case SR_T_RATIONAL_PERIOD:
496 if ((ret = sr_parse_period(value, &p, &q)) != SR_OK)
498 rational[0] = g_variant_new_uint64(p);
499 rational[1] = g_variant_new_uint64(q);
500 val = g_variant_new_tuple(rational, 2);
502 case SR_T_RATIONAL_VOLT:
503 if ((ret = sr_parse_voltage(value, &p, &q)) != SR_OK)
505 rational[0] = g_variant_new_uint64(p);
506 rational[1] = g_variant_new_uint64(q);
507 val = g_variant_new_tuple(rational, 2);
509 case SR_T_UINT64_RANGE:
510 if (sscanf(value, "%"PRIu64"-%"PRIu64, &low, &high) != 2) {
514 range[0] = g_variant_new_uint64(low);
515 range[1] = g_variant_new_uint64(high);
516 val = g_variant_new_tuple(range, 2);
523 pg = select_probe_group(sdi);
524 ret = sr_config_set(sdi, pg, srci->key, val);
527 g_critical("Failed to set device option '%s'.", (char *)key);
535 void run_session(void)
540 struct sr_dev_inst *sdi;
544 devices = device_scan();
546 g_critical("No devices found.");
549 if (g_slist_length(devices) > 1) {
550 g_critical("sigrok-cli only supports one device for capturing.");
556 sr_session_datafeed_callback_add(datafeed_in, NULL);
558 if (sr_dev_open(sdi) != SR_OK) {
559 g_critical("Failed to open device.");
563 if (sr_session_dev_add(sdi) != SR_OK) {
564 g_critical("Failed to add device to session.");
565 sr_session_destroy();
570 if ((devargs = parse_generic_arg(opt_config, FALSE))) {
571 if (set_dev_options(sdi, devargs) != SR_OK)
573 g_hash_table_destroy(devargs);
577 if (select_probes(sdi) != SR_OK) {
578 g_critical("Failed to set probes.");
579 sr_session_destroy();
584 if (!(triggerlist = sr_parse_triggerstring(sdi, opt_triggers))) {
585 sr_session_destroy();
588 max_probes = g_slist_length(sdi->probes);
589 for (i = 0; i < max_probes; i++) {
590 if (triggerlist[i]) {
591 sr_dev_trigger_set(sdi, i, triggerlist[i]);
592 g_free(triggerlist[i]);
598 if (opt_continuous) {
599 if (!sr_dev_has_option(sdi, SR_CONF_CONTINUOUS)) {
600 g_critical("This device does not support continuous sampling.");
601 sr_session_destroy();
607 if (set_limit_time(sdi) != SR_OK) {
608 sr_session_destroy();
614 if ((sr_parse_sizestring(opt_samples, &limit_samples) != SR_OK)) {
615 g_critical("Invalid sample limit '%s'.", opt_samples);
616 sr_session_destroy();
619 gvar = g_variant_new_uint64(limit_samples);
620 if (sr_config_set(sdi, NULL, SR_CONF_LIMIT_SAMPLES, gvar) != SR_OK) {
621 g_critical("Failed to configure sample limit.");
622 sr_session_destroy();
628 if ((sr_parse_sizestring(opt_frames, &limit_frames) != SR_OK)) {
629 g_critical("Invalid sample limit '%s'.", opt_samples);
630 sr_session_destroy();
633 gvar = g_variant_new_uint64(limit_frames);
634 if (sr_config_set(sdi, NULL, SR_CONF_LIMIT_FRAMES, gvar) != SR_OK) {
635 g_critical("Failed to configure frame limit.");
636 sr_session_destroy();
641 if (sr_session_start() != SR_OK) {
642 g_critical("Failed to start session.");
643 sr_session_destroy();
655 sr_session_datafeed_callback_remove_all();
656 sr_session_destroy();
657 g_slist_free(devices);