]> sigrok.org Git - sigrok-cli.git/blob - session.c
Bump version to 0.6.0 (the upcoming next major release).
[sigrok-cli.git] / session.c
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
20 #include <glib.h>
21 #include <glib/gstdio.h>
22 #include <string.h>
23 #include <stdlib.h>
24 #include "sigrok-cli.h"
25
26 static uint64_t limit_samples = 0;
27 static uint64_t limit_frames = 0;
28 static char *srzip_and_filename = NULL;
29
30 #ifdef HAVE_SRD
31 extern struct srd_session *srd_sess;
32 #endif
33
34 static int set_limit_time(const struct sr_dev_inst *sdi)
35 {
36         GVariant *gvar;
37         uint64_t time_msec;
38         uint64_t samplerate;
39         struct sr_dev_driver *driver;
40
41         driver = sr_dev_inst_driver_get(sdi);
42
43         if (!(time_msec = sr_parse_timestring(opt_time))) {
44                 g_critical("Invalid time '%s'", opt_time);
45                 return SR_ERR;
46         }
47
48         if (config_key_has_cap(driver, sdi, NULL, SR_CONF_LIMIT_MSEC, SR_CONF_SET)) {
49                 gvar = g_variant_new_uint64(time_msec);
50                 if (sr_config_set(sdi, NULL, SR_CONF_LIMIT_MSEC, gvar) != SR_OK) {
51                         g_critical("Failed to configure time limit.");
52                         return SR_ERR;
53                 }
54         } else if (config_key_has_cap(driver, sdi, NULL, SR_CONF_SAMPLERATE,
55                         SR_CONF_GET | SR_CONF_SET)) {
56                 /* Convert to samples based on the samplerate. */
57                 sr_config_get(driver, sdi, NULL, SR_CONF_SAMPLERATE, &gvar);
58                 samplerate = g_variant_get_uint64(gvar);
59                 g_variant_unref(gvar);
60                 limit_samples = (samplerate) * time_msec / (uint64_t)1000;
61                 if (limit_samples == 0) {
62                         g_critical("Not enough time at this samplerate.");
63                         return SR_ERR;
64                 }
65                 gvar = g_variant_new_uint64(limit_samples);
66                 if (sr_config_set(sdi, NULL, SR_CONF_LIMIT_SAMPLES, gvar) != SR_OK) {
67                         g_critical("Failed to configure time-based sample limit.");
68                         return SR_ERR;
69                 }
70         } else {
71                 g_critical("This device does not support time limits.");
72                 return SR_ERR;
73         }
74
75         return SR_OK;
76 }
77
78 const struct sr_output *setup_output_format(const struct sr_dev_inst *sdi)
79 {
80         const struct sr_output_module *omod;
81         const struct sr_option **options;
82         const struct sr_output *o;
83         GHashTable *fmtargs, *fmtopts;
84         int size;
85         char *fmtspec;
86
87         if (!opt_output_format) {
88                 if (opt_output_file) {
89                         size = strlen(opt_output_file) + 32;
90                         srzip_and_filename = g_malloc(size);
91                         snprintf(srzip_and_filename, size, "srzip:filename=%s", opt_output_file);
92                         opt_output_format = srzip_and_filename;
93                 } else {
94                         opt_output_format = DEFAULT_OUTPUT_FORMAT;
95                 }
96         }
97
98         fmtargs = parse_generic_arg(opt_output_format, TRUE);
99         fmtspec = g_hash_table_lookup(fmtargs, "sigrok_key");
100         if (!fmtspec)
101                 g_critical("Invalid output format.");
102         if (!(omod = sr_output_find(fmtspec)))
103                 g_critical("Unknown output module '%s'.", fmtspec);
104         g_hash_table_remove(fmtargs, "sigrok_key");
105         if ((options = sr_output_options_get(omod))) {
106                 fmtopts = generic_arg_to_opt(options, fmtargs);
107                 sr_output_options_free(options);
108         } else
109                 fmtopts = NULL;
110         o = sr_output_new(omod, fmtopts, sdi);
111         if (fmtopts)
112                 g_hash_table_destroy(fmtopts);
113         g_hash_table_destroy(fmtargs);
114
115         return o;
116 }
117
118 const struct sr_transform *setup_transform_module(const struct sr_dev_inst *sdi)
119 {
120         const struct sr_transform_module *tmod;
121         const struct sr_option **options;
122         const struct sr_transform *t;
123         GHashTable *fmtargs, *fmtopts;
124         char *fmtspec;
125
126         if (!opt_transform_module)
127                 opt_transform_module = "nop";
128
129         fmtargs = parse_generic_arg(opt_transform_module, TRUE);
130         fmtspec = g_hash_table_lookup(fmtargs, "sigrok_key");
131         if (!fmtspec)
132                 g_critical("Invalid transform module.");
133         if (!(tmod = sr_transform_find(fmtspec)))
134                 g_critical("Unknown transform module '%s'.", fmtspec);
135         g_hash_table_remove(fmtargs, "sigrok_key");
136         if ((options = sr_transform_options_get(tmod))) {
137                 fmtopts = generic_arg_to_opt(options, fmtargs);
138                 sr_transform_options_free(options);
139         } else
140                 fmtopts = NULL;
141         t = sr_transform_new(tmod, fmtopts, sdi);
142         if (fmtopts)
143                 g_hash_table_destroy(fmtopts);
144         g_hash_table_destroy(fmtargs);
145
146         return t;
147 }
148
149 void datafeed_in(const struct sr_dev_inst *sdi,
150                 const struct sr_datafeed_packet *packet, void *cb_data)
151 {
152         const struct sr_datafeed_meta *meta;
153         const struct sr_datafeed_logic *logic;
154         const struct sr_datafeed_analog *analog;
155         struct sr_session *session;
156         struct sr_config *src;
157         static const struct sr_output *o = NULL;
158         static const struct sr_output *oa = NULL;
159         static uint64_t rcvd_samples_logic = 0;
160         static uint64_t rcvd_samples_analog = 0;
161         static uint64_t samplerate = 0;
162         static int triggered = 0;
163         static FILE *outfile = NULL;
164         GSList *l;
165         GString *out;
166         GVariant *gvar;
167         uint64_t end_sample;
168         uint64_t input_len;
169         struct sr_dev_driver *driver;
170
171         driver = sr_dev_inst_driver_get(sdi);
172
173         /* If the first packet to come in isn't a header, don't even try. */
174         if (packet->type != SR_DF_HEADER && !o)
175                 return;
176
177         session = cb_data;
178         switch (packet->type) {
179         case SR_DF_HEADER:
180                 g_debug("cli: Received SR_DF_HEADER.");
181                 if (!(o = setup_output_format(sdi)))
182                         g_critical("Failed to initialize output module.");
183
184                 /* Set up backup analog output module. */
185                 oa = sr_output_new(sr_output_find("analog"), NULL, sdi);
186
187                 if (opt_output_file)
188                         outfile = g_fopen(opt_output_file, "wb");
189                 else
190                         outfile = stdout;
191
192                 rcvd_samples_logic = rcvd_samples_analog = 0;
193
194                 if (maybe_config_get(driver, sdi, NULL, SR_CONF_SAMPLERATE,
195                                 &gvar) == SR_OK) {
196                         samplerate = g_variant_get_uint64(gvar);
197                         g_variant_unref(gvar);
198                 }
199
200 #ifdef HAVE_SRD
201                 if (opt_pds) {
202                         if (samplerate) {
203                                 if (srd_session_metadata_set(srd_sess, SRD_CONF_SAMPLERATE,
204                                                 g_variant_new_uint64(samplerate)) != SRD_OK) {
205                                         g_critical("Failed to configure decode session.");
206                                         break;
207                                 }
208                         }
209                         if (srd_session_start(srd_sess) != SRD_OK) {
210                                 g_critical("Failed to start decode session.");
211                                 break;
212                         }
213                 }
214 #endif
215                 break;
216
217         case SR_DF_META:
218                 g_debug("cli: Received SR_DF_META.");
219                 meta = packet->payload;
220                 for (l = meta->config; l; l = l->next) {
221                         src = l->data;
222                         switch (src->key) {
223                         case SR_CONF_SAMPLERATE:
224                                 samplerate = g_variant_get_uint64(src->data);
225                                 g_debug("cli: Got samplerate %"PRIu64" Hz.", samplerate);
226 #ifdef HAVE_SRD
227                                 if (opt_pds) {
228                                         if (srd_session_metadata_set(srd_sess, SRD_CONF_SAMPLERATE,
229                                                         g_variant_new_uint64(samplerate)) != SRD_OK) {
230                                                 g_critical("Failed to pass samplerate to decoder.");
231                                         }
232                                 }
233 #endif
234                                 break;
235                         case SR_CONF_SAMPLE_INTERVAL:
236                                 samplerate = g_variant_get_uint64(src->data);
237                                 g_debug("cli: Got sample interval %"PRIu64" ms.", samplerate);
238                                 break;
239                         default:
240                                 /* Unknown metadata is not an error. */
241                                 break;
242                         }
243                 }
244                 break;
245
246         case SR_DF_TRIGGER:
247                 g_debug("cli: Received SR_DF_TRIGGER.");
248                 triggered = 1;
249                 break;
250
251         case SR_DF_LOGIC:
252                 logic = packet->payload;
253                 g_message("cli: Received SR_DF_LOGIC (%"PRIu64" bytes, unitsize = %d).",
254                                 logic->length, logic->unitsize);
255                 if (logic->length == 0)
256                         break;
257
258                 /* Don't store any samples until triggered. */
259                 if (opt_wait_trigger && !triggered)
260                         break;
261
262                 if (limit_samples && rcvd_samples_logic >= limit_samples)
263                         break;
264
265                 end_sample = rcvd_samples_logic + logic->length / logic->unitsize;
266                 /* Cut off last packet according to the sample limit. */
267                 if (limit_samples && end_sample > limit_samples)
268                         end_sample = limit_samples;
269                 input_len = (end_sample - rcvd_samples_logic) * logic->unitsize;
270
271                 if (opt_pds) {
272 #ifdef HAVE_SRD
273                         if (srd_session_send(srd_sess, rcvd_samples_logic, end_sample,
274                                         logic->data, input_len) != SRD_OK)
275                                 sr_session_stop(session);
276 #endif
277                 }
278
279                 rcvd_samples_logic = end_sample;
280                 break;
281
282         case SR_DF_ANALOG:
283                 analog = packet->payload;
284                 g_message("cli: Received SR_DF_ANALOG (%d samples).", analog->num_samples);
285                 if (analog->num_samples == 0)
286                         break;
287
288                 if (limit_samples && rcvd_samples_analog >= limit_samples)
289                         break;
290
291                 rcvd_samples_analog += analog->num_samples;
292                 break;
293
294         case SR_DF_FRAME_BEGIN:
295                 g_debug("cli: Received SR_DF_FRAME_BEGIN.");
296                 break;
297
298         case SR_DF_FRAME_END:
299                 g_debug("cli: Received SR_DF_FRAME_END.");
300                 break;
301
302         default:
303                 break;
304         }
305
306         if (o && outfile && !opt_pds) {
307                 if (sr_output_send(o, packet, &out) == SR_OK) {
308                         if (!out || (out->len == 0
309                                         && !opt_output_format
310                                         && packet->type == SR_DF_ANALOG)) {
311                                 /*
312                                  * The user didn't specify an output module,
313                                  * but needs to see this analog data.
314                                  */
315                                 sr_output_send(oa, packet, &out);
316                         }
317                         if (out && out->len > 0) {
318                                 fwrite(out->str, 1, out->len, outfile);
319                                 fflush(outfile);
320                         }
321                         if (out)
322                                 g_string_free(out, TRUE);
323                 }
324         }
325
326         /*
327          * SR_DF_END needs to be handled after the output module's receive()
328          * is called, so it can properly clean up that module.
329          */
330         if (packet->type == SR_DF_END) {
331                 g_debug("cli: Received SR_DF_END.");
332
333                 if (o) {
334                         sr_output_free(o);
335                         g_free(srzip_and_filename);
336                 }
337                 o = NULL;
338
339                 sr_output_free(oa);
340                 oa = NULL;
341
342                 if (outfile && outfile != stdout)
343                         fclose(outfile);
344
345                 if (limit_samples) {
346                         if (rcvd_samples_logic > 0 && rcvd_samples_logic < limit_samples)
347                                 g_warning("Device only sent %" PRIu64 " samples.",
348                                            rcvd_samples_logic);
349                         else if (rcvd_samples_analog > 0 && rcvd_samples_analog < limit_samples)
350                                 g_warning("Device only sent %" PRIu64 " samples.",
351                                            rcvd_samples_analog);
352                 }
353         }
354
355 }
356
357 int opt_to_gvar(char *key, char *value, struct sr_config *src)
358 {
359         const struct sr_config_info *srci;
360         double tmp_double, dlow, dhigh;
361         uint64_t tmp_u64, p, q, low, high;
362         GVariant *rational[2], *range[2];
363         GVariantBuilder *vbl;
364         gboolean tmp_bool;
365         gchar **keyval;
366         int ret;
367
368         if (!(srci = sr_config_info_name_get(key))) {
369                 g_critical("Unknown device option '%s'.", (char *) key);
370                 return -1;
371         }
372         src->key = srci->key;
373
374         if ((!value || strlen(value) == 0) &&
375                 (srci->datatype != SR_T_BOOL)) {
376                 g_critical("Option '%s' needs a value.", (char *)key);
377                 return -1;
378         }
379
380         ret = 0;
381         switch (srci->datatype) {
382         case SR_T_UINT64:
383                 ret = sr_parse_sizestring(value, &tmp_u64);
384                 if (ret != 0)
385                         break;
386                 src->data = g_variant_new_uint64(tmp_u64);
387                 break;
388         case SR_T_INT32:
389                 ret = sr_parse_sizestring(value, &tmp_u64);
390                 if (ret != 0)
391                         break;
392                 src->data = g_variant_new_int32(tmp_u64);
393                 break;
394         case SR_T_STRING:
395                 src->data = g_variant_new_string(value);
396                 break;
397         case SR_T_BOOL:
398                 if (!value)
399                         tmp_bool = TRUE;
400                 else
401                         tmp_bool = sr_parse_boolstring(value);
402                 src->data = g_variant_new_boolean(tmp_bool);
403                 break;
404         case SR_T_FLOAT:
405                 tmp_double = strtof(value, NULL);
406                 src->data = g_variant_new_double(tmp_double);
407                 break;
408         case SR_T_RATIONAL_PERIOD:
409                 if ((ret = sr_parse_period(value, &p, &q)) != SR_OK)
410                         break;
411                 rational[0] = g_variant_new_uint64(p);
412                 rational[1] = g_variant_new_uint64(q);
413                 src->data = g_variant_new_tuple(rational, 2);
414                 break;
415         case SR_T_RATIONAL_VOLT:
416                 if ((ret = sr_parse_voltage(value, &p, &q)) != SR_OK)
417                         break;
418                 rational[0] = g_variant_new_uint64(p);
419                 rational[1] = g_variant_new_uint64(q);
420                 src->data = g_variant_new_tuple(rational, 2);
421                 break;
422         case SR_T_UINT64_RANGE:
423                 if (sscanf(value, "%"PRIu64"-%"PRIu64, &low, &high) != 2) {
424                         ret = -1;
425                         break;
426                 } else {
427                         range[0] = g_variant_new_uint64(low);
428                         range[1] = g_variant_new_uint64(high);
429                         src->data = g_variant_new_tuple(range, 2);
430                 }
431                 break;
432         case SR_T_DOUBLE_RANGE:
433                 if (sscanf(value, "%lf-%lf", &dlow, &dhigh) != 2) {
434                         ret = -1;
435                         break;
436                 } else {
437                         range[0] = g_variant_new_double(dlow);
438                         range[1] = g_variant_new_double(dhigh);
439                         src->data = g_variant_new_tuple(range, 2);
440                 }
441                 break;
442         case SR_T_KEYVALUE:
443                 /* Expects the argument to be in the form of key=value. */
444                 keyval = g_strsplit(value, "=", 2);
445                 if (!keyval[0] || !keyval[1]) {
446                         g_strfreev(keyval);
447                         ret = -1;
448                         break;
449                 } else {
450                         vbl = g_variant_builder_new(G_VARIANT_TYPE_DICTIONARY);
451                         g_variant_builder_add(vbl, "{ss}",
452                                               keyval[0], keyval[1]);
453                         src->data = g_variant_builder_end(vbl);
454                         g_strfreev(keyval);
455                 }
456                 break;
457         default:
458                 g_critical("Unknown data type specified for option '%s' "
459                            "(driver implementation bug?).", key);
460                 ret = -1;
461         }
462
463         if (ret < 0)
464                 g_critical("Invalid value: '%s' for option '%s'", value, key);
465
466         return ret;
467 }
468
469 int set_dev_options(struct sr_dev_inst *sdi, GHashTable *args)
470 {
471         struct sr_config src;
472         struct sr_channel_group *cg;
473         GHashTableIter iter;
474         gpointer key, value;
475         int ret;
476
477         g_hash_table_iter_init(&iter, args);
478         while (g_hash_table_iter_next(&iter, &key, &value)) {
479                 if ((ret = opt_to_gvar(key, value, &src)) != 0)
480                         return ret;
481                 cg = select_channel_group(sdi);
482                 if ((ret = maybe_config_set(sr_dev_inst_driver_get(sdi), sdi, cg,
483                                 src.key, src.data)) != SR_OK) {
484                         g_critical("Failed to set device option '%s': %s.",
485                                    (char *)key, sr_strerror(ret));
486                         return ret;
487                 }
488         }
489
490         return SR_OK;
491 }
492
493 void run_session(void)
494 {
495         GSList *devices, *real_devices, *sd;
496         GHashTable *devargs;
497         GVariant *gvar;
498         struct sr_session *session;
499         struct sr_trigger *trigger;
500         struct sr_dev_inst *sdi;
501         uint64_t min_samples, max_samples;
502         gsize n_elements, i;
503         const uint32_t *dev_opts;
504         int is_demo_dev;
505         struct sr_dev_driver *driver;
506         const struct sr_transform *t;
507
508         devices = device_scan();
509         if (!devices) {
510                 g_critical("No devices found.");
511                 return;
512         }
513
514         real_devices = NULL;
515         for (sd = devices; sd; sd = sd->next) {
516                 sdi = sd->data;
517
518                 driver = sr_dev_inst_driver_get(sdi);
519
520                 if (sr_config_list(driver, sdi, NULL, SR_CONF_DEVICE_OPTIONS, &gvar) != SR_OK) {
521                         g_critical("Failed to query list device options.");
522                         return;
523                 }
524
525                 dev_opts = g_variant_get_fixed_array(gvar, &n_elements, sizeof(uint32_t));
526
527                 is_demo_dev = 0;
528                 for (i = 0; i < n_elements; i++) {
529                         if (dev_opts[i] == SR_CONF_DEMO_DEV)
530                                 is_demo_dev = 1;
531                 }
532
533                 g_variant_unref(gvar);
534
535                 if (!is_demo_dev)
536                         real_devices = g_slist_append(real_devices, sdi);
537         }
538
539         if (g_slist_length(devices) > 1) {
540                 if (g_slist_length(real_devices) != 1) {
541                         g_critical("sigrok-cli only supports one device for capturing.");
542                         return;
543                 } else {
544                         /* We only have one non-demo device. */
545                         g_slist_free(devices);
546                         devices = real_devices;
547                         real_devices = NULL;
548                 }
549         }
550
551         sdi = devices->data;
552         g_slist_free(devices);
553         g_slist_free(real_devices);
554
555         sr_session_new(sr_ctx, &session);
556         sr_session_datafeed_callback_add(session, datafeed_in, NULL);
557
558         if (sr_dev_open(sdi) != SR_OK) {
559                 g_critical("Failed to open device.");
560                 return;
561         }
562
563         if (sr_session_dev_add(session, sdi) != SR_OK) {
564                 g_critical("Failed to add device to session.");
565                 sr_session_destroy(session);
566                 return;
567         }
568
569         if (opt_config) {
570                 if ((devargs = parse_generic_arg(opt_config, FALSE))) {
571                         if (set_dev_options(sdi, devargs) != SR_OK)
572                                 return;
573                         g_hash_table_destroy(devargs);
574                 }
575         }
576
577         if (select_channels(sdi) != SR_OK) {
578                 g_critical("Failed to set channels.");
579                 sr_session_destroy(session);
580                 return;
581         }
582
583         if (opt_triggers) {
584                 if (!parse_triggerstring(sdi, opt_triggers, &trigger)) {
585                         sr_session_destroy(session);
586                         return;
587                 }
588                 if (sr_session_trigger_set(session, trigger) != SR_OK) {
589                         sr_session_destroy(session);
590                         return;
591                 }
592         }
593
594         if (opt_continuous) {
595                 if (!sr_dev_has_option(sdi, SR_CONF_CONTINUOUS)) {
596                         g_critical("This device does not support continuous sampling.");
597                         sr_session_destroy(session);
598                         return;
599                 }
600         }
601
602         if (opt_time) {
603                 if (set_limit_time(sdi) != SR_OK) {
604                         sr_session_destroy(session);
605                         return;
606                 }
607         }
608
609         if (opt_samples) {
610                 if ((sr_parse_sizestring(opt_samples, &limit_samples) != SR_OK)) {
611                         g_critical("Invalid sample limit '%s'.", opt_samples);
612                         sr_session_destroy(session);
613                         return;
614                 }
615                 if (maybe_config_list(driver, sdi, NULL, SR_CONF_LIMIT_SAMPLES,
616                                 &gvar) == SR_OK) {
617                         /*
618                          * The device has no compression, or compression is turned
619                          * off, and publishes its sample memory size.
620                          */
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);
626                         }
627                         if (limit_samples > max_samples) {
628                                 g_critical("The device can store only %"PRIu64
629                                                 " samples with the current settings.", max_samples);
630                         }
631                 }
632                 gvar = g_variant_new_uint64(limit_samples);
633                 if (maybe_config_set(sr_dev_inst_driver_get(sdi), sdi, NULL, SR_CONF_LIMIT_SAMPLES, gvar) != SR_OK) {
634                         g_critical("Failed to configure sample limit.");
635                         sr_session_destroy(session);
636                         return;
637                 }
638         }
639
640         if (opt_frames) {
641                 if ((sr_parse_sizestring(opt_frames, &limit_frames) != SR_OK)) {
642                         g_critical("Invalid sample limit '%s'.", opt_samples);
643                         sr_session_destroy(session);
644                         return;
645                 }
646                 gvar = g_variant_new_uint64(limit_frames);
647                 if (maybe_config_set(sr_dev_inst_driver_get(sdi), sdi, NULL, SR_CONF_LIMIT_FRAMES, gvar) != SR_OK) {
648                         g_critical("Failed to configure frame limit.");
649                         sr_session_destroy(session);
650                         return;
651                 }
652         }
653
654         if (!(t = setup_transform_module(sdi)))
655                 g_critical("Failed to initialize transform module.");
656
657         if (sr_session_start(session) != SR_OK) {
658                 g_critical("Failed to start session.");
659                 sr_session_destroy(session);
660                 return;
661         }
662
663         if (opt_continuous)
664                 add_anykey(session);
665
666         sr_session_run(session);
667
668         if (opt_continuous)
669                 clear_anykey();
670
671         sr_session_datafeed_callback_remove_all(session);
672         sr_session_destroy(session);
673
674 }