]> sigrok.org Git - sigrok-cli.git/blame - session.c
session: Remove call to sr_filter_probes()
[sigrok-cli.git] / session.c
CommitLineData
2be182e6
BV
1/*
2 * This file is part of the sigrok-cli project.
3 *
4 * Copyright (C) 2013 Bert Vermeulen <bert@biot.com>
5 *
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.
10 *
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.
15 *
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/>.
18 */
19
20fb52e0 20#include "sigrok-cli.h"
2be182e6
BV
21#include "config.h"
22#include <glib.h>
23#include <glib/gstdio.h>
55de58a2
UH
24#include <string.h>
25#include <stdlib.h>
2be182e6
BV
26
27static struct sr_output_format *output_format = NULL;
28static int default_output_format = FALSE;
29static char *output_format_param = NULL;
30static GByteArray *savebuf;
31static uint64_t limit_samples = 0;
32static uint64_t limit_frames = 0;
33
34extern gchar *opt_output_file;
35extern gchar *opt_output_format;
36extern gchar *opt_pds;
37extern gboolean opt_wait_trigger;
38extern gchar *opt_time;
39extern gchar *opt_samples;
40extern gchar *opt_frames;
41extern gchar *opt_continuous;
42extern gchar *opt_config;
43extern gchar *opt_triggers;
44#ifdef HAVE_SRD
45extern struct srd_session *srd_sess;
46#endif
47
48
49static GArray *get_enabled_logic_probes(const struct sr_dev_inst *sdi)
50{
51 struct sr_probe *probe;
52 GArray *probes;
53 GSList *l;
54
55 probes = g_array_new(FALSE, FALSE, sizeof(int));
56 for (l = sdi->probes; l; l = l->next) {
57 probe = l->data;
58 if (probe->type != SR_PROBE_LOGIC)
59 continue;
60 if (probe->enabled != TRUE)
61 continue;
62 g_array_append_val(probes, probe->index);
63 }
64
65 return probes;
66}
67
68static int set_limit_time(const struct sr_dev_inst *sdi)
69{
70 GVariant *gvar;
71 uint64_t time_msec;
72 uint64_t samplerate;
73
74 if (!(time_msec = sr_parse_timestring(opt_time))) {
75 g_critical("Invalid time '%s'", opt_time);
76 return SR_ERR;
77 }
78
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.");
83 return SR_ERR;
84 }
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.");
93 return SR_ERR;
94 }
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.");
98 return SR_ERR;
99 }
100 } else {
101 g_critical("This device does not support time limits.");
102 return SR_ERR;
103 }
104
105 return SR_OK;
106}
107
108int setup_output_format(void)
109{
110 GHashTable *fmtargs;
111 GHashTableIter iter;
112 gpointer key, value;
113 struct sr_output_format **outputs;
114 int i;
115 char *fmtspec;
116
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;
122 }
123
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.
128 */
129 default_output_format = TRUE;
130 }
131
132 fmtargs = parse_generic_arg(opt_output_format, TRUE);
133 fmtspec = g_hash_table_lookup(fmtargs, "sigrok_key");
134 if (!fmtspec) {
135 g_critical("Invalid output format.");
136 return 1;
137 }
138 outputs = sr_output_list();
139 for (i = 0; outputs[i]; i++) {
140 if (strcmp(outputs[i]->id, fmtspec))
141 continue;
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);
149 break;
150 }
151 break;
152 }
153 if (!output_format) {
154 g_critical("Invalid output format %s.", opt_output_format);
155 return 1;
156 }
157 g_hash_table_destroy(fmtargs);
158
159 return 0;
160}
161
162void datafeed_in(const struct sr_dev_inst *sdi,
163 const struct sr_datafeed_packet *packet, void *cb_data)
164{
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;
175 GSList *l;
176 GString *out;
8667d3c2 177 uint64_t samplerate;
55de58a2 178 uint64_t end_sample;
8667d3c2
DE
179 uint64_t output_len, input_len;
180 uint8_t *output_buf;
2be182e6
BV
181
182 (void) cb_data;
183
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)
186 return;
187
2be182e6
BV
188 switch (packet->type) {
189 case SR_DF_HEADER:
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.");
194 exit(1);
195 }
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.");
202 exit(1);
203 }
204 }
205
206 /* Prepare non-stdout output. */
207 outfile = stdout;
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. */
213 outfile = NULL;
214 savebuf = g_byte_array_new();
215 } else {
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");
219 }
220 }
221
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;
226
227#ifdef HAVE_SRD
228 GVariant *gvar;
229 if (opt_pds && logic_probelist->len) {
230 if (sr_config_get(sdi->driver, sdi, NULL, SR_CONF_SAMPLERATE,
231 &gvar) == SR_OK) {
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.");
237 break;
238 }
239 }
240 if (srd_session_start(srd_sess) != SRD_OK) {
241 g_critical("Failed to start decode session.");
242 break;
243 }
244 }
245#endif
246 break;
247
248 case SR_DF_META:
249 g_debug("cli: received SR_DF_META");
250 meta = packet->payload;
251 for (l = meta->config; l; l = l->next) {
252 src = l->data;
253 switch (src->key) {
254 case SR_CONF_SAMPLERATE:
255 samplerate = g_variant_get_uint64(src->data);
256 g_debug("cli: got samplerate %"PRIu64" Hz", samplerate);
257#ifdef HAVE_SRD
258 if (opt_pds) {
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.");
262 }
263 }
264#endif
265 break;
266 case SR_CONF_SAMPLE_INTERVAL:
267 samplerate = g_variant_get_uint64(src->data);
268 g_debug("cli: got sample interval %"PRIu64" ms", samplerate);
269 break;
270 default:
271 /* Unknown metadata is not an error. */
272 break;
273 }
274 }
275 break;
276
277 case SR_DF_TRIGGER:
278 g_debug("cli: received SR_DF_TRIGGER");
279 if (o->format->event)
280 o->format->event(o, SR_DF_TRIGGER, &output_buf,
281 &output_len);
282 triggered = 1;
283 break;
284
285 case SR_DF_LOGIC:
286 logic = packet->payload;
287 g_message("cli: received SR_DF_LOGIC, %"PRIu64" bytes", logic->length);
2be182e6
BV
288 if (logic->length == 0)
289 break;
290
291 /* Don't store any samples until triggered. */
292 if (opt_wait_trigger && !triggered)
293 break;
294
295 if (limit_samples && received_samples >= limit_samples)
296 break;
297
8667d3c2
DE
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;
2be182e6
BV
303
304 if (opt_output_file && default_output_format) {
305 /* Saving to a session file. */
8667d3c2 306 g_byte_array_append(savebuf, logic->data, input_len);
2be182e6
BV
307 } else {
308 if (opt_pds) {
309#ifdef HAVE_SRD
2be182e6 310 if (srd_session_send(srd_sess, received_samples, end_sample,
8667d3c2 311 logic->data, input_len) != SRD_OK)
2be182e6
BV
312 sr_session_stop();
313#endif
314 } else {
315 output_len = 0;
316 if (o->format->data && packet->type == o->format->df_type)
8667d3c2 317 o->format->data(o, logic->data, input_len,
2be182e6
BV
318 &output_buf, &output_len);
319 if (output_len) {
320 fwrite(output_buf, 1, output_len, outfile);
321 fflush(outfile);
322 g_free(output_buf);
323 }
324 }
325 }
2be182e6 326
8667d3c2 327 received_samples += logic->length / logic->unitsize;
2be182e6
BV
328 break;
329
330 case SR_DF_ANALOG:
331 analog = packet->payload;
332 g_message("cli: received SR_DF_ANALOG, %d samples", analog->num_samples);
333 if (analog->num_samples == 0)
334 break;
335
336 if (limit_samples && received_samples >= limit_samples)
337 break;
338
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);
343 if (output_buf) {
344 fwrite(output_buf, 1, output_len, outfile);
345 fflush(outfile);
346 g_free(output_buf);
347 }
348 }
349
350 received_samples += analog->num_samples;
351 break;
352
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,
357 &output_len);
358 if (output_buf) {
359 fwrite(output_buf, 1, output_len, outfile);
360 fflush(outfile);
361 g_free(output_buf);
362 }
363 }
364 break;
365
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,
370 &output_len);
371 if (output_buf) {
372 fwrite(output_buf, 1, output_len, outfile);
373 fflush(outfile);
374 g_free(output_buf);
375 }
376 }
377 break;
378
379 default:
380 break;
381 }
382
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);
386 fflush(outfile);
387 g_string_free(out, TRUE);
388 }
389 }
390
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");
395
396 if (o->format->event) {
397 o->format->event(o, SR_DF_END, &output_buf, &output_len);
398 if (output_buf) {
399 if (outfile)
400 fwrite(output_buf, 1, output_len, outfile);
401 g_free(output_buf);
402 output_len = 0;
403 }
404 }
405
406 if (limit_samples && received_samples < limit_samples)
407 g_warning("Device only sent %" PRIu64 " samples.",
408 received_samples);
409
410 if (opt_continuous)
411 g_warning("Device stopped after %" PRIu64 " samples.",
412 received_samples);
413
414 g_array_free(logic_probelist, TRUE);
415
416 if (o->format->cleanup)
417 o->format->cleanup(o);
418 g_free(o);
419 o = NULL;
420
421 if (outfile && outfile != stdout)
422 fclose(outfile);
423
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.");
30364883 428 g_byte_array_free(savebuf, TRUE);
2be182e6
BV
429 }
430 }
431
432}
433
ad54a1a6 434int opt_to_gvar(char *key, char *value, struct sr_config *src)
2be182e6
BV
435{
436 const struct sr_config_info *srci;
2be182e6
BV
437 double tmp_double;
438 uint64_t tmp_u64, p, q, low, high;
ad54a1a6 439 GVariant *rational[2], *range[2];
2be182e6 440 gboolean tmp_bool;
ad54a1a6 441 int ret;
2be182e6 442
ad54a1a6
BV
443 if (!(srci = sr_config_info_name_get(key))) {
444 g_critical("Unknown device option '%s'.", (char *) key);
445 return -1;
446 }
447 src->key = srci->key;
2be182e6 448
ad54a1a6
BV
449 if ((value == NULL) &&
450 (srci->datatype != SR_T_BOOL)) {
451 g_critical("Option '%s' needs a value.", (char *)key);
452 return -1;
453 }
454
455 ret = 0;
456 switch (srci->datatype) {
457 case SR_T_UINT64:
458 ret = sr_parse_sizestring(value, &tmp_u64);
459 if (ret != 0)
2be182e6 460 break;
ad54a1a6
BV
461 src->data = g_variant_new_uint64(tmp_u64);
462 break;
463 case SR_T_INT32:
464 ret = sr_parse_sizestring(value, &tmp_u64);
465 if (ret != 0)
2be182e6 466 break;
ad54a1a6
BV
467 src->data = g_variant_new_int32(tmp_u64);
468 break;
469 case SR_T_CHAR:
470 src->data = g_variant_new_string(value);
471 break;
472 case SR_T_BOOL:
473 if (!value)
474 tmp_bool = TRUE;
475 else
476 tmp_bool = sr_parse_boolstring(value);
477 src->data = g_variant_new_boolean(tmp_bool);
478 break;
479 case SR_T_FLOAT:
480 tmp_double = strtof(value, NULL);
481 src->data = g_variant_new_double(tmp_double);
482 break;
483 case SR_T_RATIONAL_PERIOD:
484 if ((ret = sr_parse_period(value, &p, &q)) != SR_OK)
2be182e6 485 break;
ad54a1a6
BV
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);
489 break;
490 case SR_T_RATIONAL_VOLT:
491 if ((ret = sr_parse_voltage(value, &p, &q)) != SR_OK)
2be182e6 492 break;
ad54a1a6
BV
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);
496 break;
497 case SR_T_UINT64_RANGE:
498 if (sscanf(value, "%"PRIu64"-%"PRIu64, &low, &high) != 2) {
499 ret = -1;
2be182e6 500 break;
ad54a1a6
BV
501 } else {
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);
2be182e6 505 }
ad54a1a6
BV
506 break;
507 default:
508 ret = -1;
509 }
510
511 return ret;
512}
513
514int set_dev_options(struct sr_dev_inst *sdi, GHashTable *args)
515{
516 struct sr_config src;
517 struct sr_probe_group *pg;
518 GHashTableIter iter;
519 gpointer key, value;
520 int ret;
521
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)
525 return ret;
526 pg = select_probe_group(sdi);
527 ret = sr_config_set(sdi, pg, src.key, src.data);
2be182e6
BV
528 if (ret != SR_OK) {
529 g_critical("Failed to set device option '%s'.", (char *)key);
530 return ret;
531 }
532 }
533
534 return SR_OK;
535}
536
537void run_session(void)
538{
539 GSList *devices;
540 GHashTable *devargs;
541 GVariant *gvar;
542 struct sr_dev_inst *sdi;
02c65935 543 uint64_t min_samples, max_samples;
2be182e6
BV
544 int max_probes, i;
545 char **triggerlist;
546
547 devices = device_scan();
548 if (!devices) {
549 g_critical("No devices found.");
550 return;
551 }
552 if (g_slist_length(devices) > 1) {
553 g_critical("sigrok-cli only supports one device for capturing.");
554 return;
555 }
556 sdi = devices->data;
557
558 sr_session_new();
559 sr_session_datafeed_callback_add(datafeed_in, NULL);
560
561 if (sr_dev_open(sdi) != SR_OK) {
562 g_critical("Failed to open device.");
563 return;
564 }
565
566 if (sr_session_dev_add(sdi) != SR_OK) {
567 g_critical("Failed to add device to session.");
568 sr_session_destroy();
569 return;
570 }
571
572 if (opt_config) {
573 if ((devargs = parse_generic_arg(opt_config, FALSE))) {
574 if (set_dev_options(sdi, devargs) != SR_OK)
575 return;
576 g_hash_table_destroy(devargs);
577 }
578 }
579
580 if (select_probes(sdi) != SR_OK) {
581 g_critical("Failed to set probes.");
582 sr_session_destroy();
583 return;
584 }
585
586 if (opt_triggers) {
587 if (!(triggerlist = sr_parse_triggerstring(sdi, opt_triggers))) {
588 sr_session_destroy();
589 return;
590 }
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]);
596 }
597 }
598 g_free(triggerlist);
599 }
600
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();
605 return;
606 }
607 }
608
609 if (opt_time) {
610 if (set_limit_time(sdi) != SR_OK) {
611 sr_session_destroy();
612 return;
613 }
614 }
615
616 if (opt_samples) {
617 if ((sr_parse_sizestring(opt_samples, &limit_samples) != SR_OK)) {
618 g_critical("Invalid sample limit '%s'.", opt_samples);
619 sr_session_destroy();
620 return;
621 }
02c65935
BV
622 if (sr_config_list(sdi->driver, sdi, NULL,
623 SR_CONF_LIMIT_SAMPLES, &gvar) == SR_OK) {
c7a5cb12
BV
624 /* The device has no compression, or compression is turned
625 * off, and publishes its sample memory size. */
02c65935 626 g_variant_get(gvar, "(tt)", &min_samples, &max_samples);
be781321 627 g_variant_unref(gvar);
02c65935
BV
628 if (limit_samples < min_samples) {
629 g_critical("The device stores at least %"PRIu64
630 " samples with the current settings.", min_samples);
631 }
c7a5cb12
BV
632 if (limit_samples > max_samples) {
633 g_critical("The device can store only %"PRIu64
634 " samples with the current settings.", max_samples);
635 }
636 }
2be182e6
BV
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();
641 return;
642 }
643 }
644
645 if (opt_frames) {
646 if ((sr_parse_sizestring(opt_frames, &limit_frames) != SR_OK)) {
647 g_critical("Invalid sample limit '%s'.", opt_samples);
648 sr_session_destroy();
649 return;
650 }
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();
655 return;
656 }
657 }
658
659 if (sr_session_start() != SR_OK) {
660 g_critical("Failed to start session.");
661 sr_session_destroy();
662 return;
663 }
664
665 if (opt_continuous)
666 add_anykey();
667
668 sr_session_run();
669
670 if (opt_continuous)
671 clear_anykey();
672
673 sr_session_datafeed_callback_remove_all();
674 sr_session_destroy();
675 g_slist_free(devices);
676
677}
678