Remove special handling of pattern mode options.
[sigrok-cli.git] / show.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 "sigrok-cli.h"
21 #include "config.h"
22 #include <glib.h>
23
24 extern gint opt_loglevel;
25 extern gchar *opt_pds;
26
27 void show_version(void)
28 {
29         struct sr_dev_driver **drivers;
30         struct sr_input_format **inputs;
31         struct sr_output_format **outputs;
32         int i;
33 #ifdef HAVE_SRD
34         struct srd_decoder *dec;
35         const GSList *l;
36 #endif
37
38         printf("sigrok-cli %s\n\n", VERSION);
39
40         printf("Using libsigrok %s (lib version %s).\n",
41                sr_package_version_string_get(), sr_lib_version_string_get());
42 #ifdef HAVE_SRD
43         printf("Using libsigrokdecode %s (lib version %s).\n\n",
44                srd_package_version_string_get(), srd_lib_version_string_get());
45 #endif
46
47         printf("Supported hardware drivers:\n");
48         drivers = sr_driver_list();
49         for (i = 0; drivers[i]; i++) {
50                 printf("  %-20s %s\n", drivers[i]->name, drivers[i]->longname);
51         }
52         printf("\n");
53
54         printf("Supported input formats:\n");
55         inputs = sr_input_list();
56         for (i = 0; inputs[i]; i++)
57                 printf("  %-20s %s\n", inputs[i]->id, inputs[i]->description);
58         printf("\n");
59
60         printf("Supported output formats:\n");
61         outputs = sr_output_list();
62         for (i = 0; outputs[i]; i++)
63                 printf("  %-20s %s\n", outputs[i]->id, outputs[i]->description);
64         printf("  %-20s %s\n", "sigrok", "Default file output format");
65         printf("\n");
66
67 #ifdef HAVE_SRD
68         if (srd_init(NULL) == SRD_OK) {
69                 printf("Supported protocol decoders:\n");
70                 srd_decoder_load_all();
71                 for (l = srd_decoder_list(); l; l = l->next) {
72                         dec = l->data;
73                         printf("  %-20s %s\n", dec->id, dec->longname);
74                         /* Print protocol description upon "-l 3" or higher. */
75                         if (opt_loglevel >= SR_LOG_INFO)
76                                 printf("  %-20s %s\n", "", dec->desc);
77                 }
78                 srd_exit();
79         }
80         printf("\n");
81 #endif
82 }
83
84 static void print_dev_line(const struct sr_dev_inst *sdi)
85 {
86         struct sr_probe *probe;
87         GSList *l;
88         GString *s;
89         GVariant *gvar;
90
91         s = g_string_sized_new(128);
92         g_string_assign(s, sdi->driver->name);
93         if (sr_config_get(sdi->driver, sdi, NULL, SR_CONF_CONN, &gvar) == SR_OK) {
94                 g_string_append(s, ":conn=");
95                 g_string_append(s, g_variant_get_string(gvar, NULL));
96                 g_variant_unref(gvar);
97         }
98         g_string_append(s, " - ");
99         if (sdi->vendor && sdi->vendor[0])
100                 g_string_append_printf(s, "%s ", sdi->vendor);
101         if (sdi->model && sdi->model[0])
102                 g_string_append_printf(s, "%s ", sdi->model);
103         if (sdi->version && sdi->version[0])
104                 g_string_append_printf(s, "%s ", sdi->version);
105         if (sdi->probes) {
106                 if (g_slist_length(sdi->probes) == 1) {
107                         probe = sdi->probes->data;
108                         g_string_append_printf(s, "with 1 probe: %s", probe->name);
109                 } else {
110                         g_string_append_printf(s, "with %d probes:", g_slist_length(sdi->probes));
111                         for (l = sdi->probes; l; l = l->next) {
112                                 probe = l->data;
113                                 g_string_append_printf(s, " %s", probe->name);
114                         }
115                 }
116         }
117         g_string_append_printf(s, "\n");
118         printf("%s", s->str);
119         g_string_free(s, TRUE);
120
121 }
122
123 void show_dev_list(void)
124 {
125         struct sr_dev_inst *sdi;
126         GSList *devices, *l;
127
128         if (!(devices = device_scan()))
129                 return;
130
131         printf("The following devices were found:\n");
132         for (l = devices; l; l = l->next) {
133                 sdi = l->data;
134                 print_dev_line(sdi);
135         }
136         g_slist_free(devices);
137
138 }
139
140 void show_dev_detail(void)
141 {
142         struct sr_dev_inst *sdi;
143         const struct sr_config_info *srci;
144         struct sr_probe *probe;
145         struct sr_probe_group *probe_group, *pg;
146         GSList *devices, *pgl, *prl;
147         GVariant *gvar_opts, *gvar_dict, *gvar_list, *gvar;
148         gsize num_opts, num_elements;
149         const uint64_t *uint64, p, q, low, high;
150         uint64_t cur_low, cur_high;
151         const int32_t *opts;
152         unsigned int num_devices, o, i;
153         char *tmp_str;
154         char *s;
155         const char *charopts, **stropts;
156
157         if (!(devices = device_scan())) {
158                 g_critical("No devices found.");
159                 return;
160         }
161
162         num_devices = g_slist_length(devices);
163         if (num_devices > 1) {
164                 g_critical("%d devices found. Use --scan to show them, "
165                                 "and select one to show.", num_devices);
166                 return;
167         }
168
169         sdi = devices->data;
170         print_dev_line(sdi);
171
172         if (sr_dev_open(sdi) != SR_OK) {
173                 g_critical("Failed to open device.");
174                 return;
175         }
176
177         if ((sr_config_list(sdi->driver, NULL, NULL, SR_CONF_SCAN_OPTIONS,
178                         &gvar_opts) == SR_OK)) {
179                 opts = g_variant_get_fixed_array(gvar_opts, &num_elements,
180                                 sizeof(int32_t));
181                 printf("Supported driver options:\n");
182                 for (i = 0; i < num_elements; i++) {
183                         if (!(srci = sr_config_info_get(opts[i])))
184                                 continue;
185                         printf("    %s\n", srci->id);
186                 }
187                 g_variant_unref(gvar_opts);
188         }
189
190         probe_group = select_probe_group(sdi);
191         if ((sr_config_list(sdi->driver, sdi, probe_group, SR_CONF_DEVICE_OPTIONS,
192                         &gvar_opts)) != SR_OK)
193                 /* Driver supports no device instance options. */
194                 return;
195
196         if (sdi->probe_groups) {
197                 printf("Probe groups:\n");
198                 for (pgl = sdi->probe_groups; pgl; pgl = pgl->next) {
199                         pg = pgl->data;
200                         printf("    %s: channel%s", pg->name,
201                                         g_slist_length(pg->probes) > 1 ? "s" : "");
202                         for (prl = pg->probes; prl; prl = prl->next) {
203                                 probe = prl->data;
204                                 printf(" %s", probe->name);
205                         }
206                         printf("\n");
207                 }
208         }
209
210         printf("Supported configuration options");
211         if (sdi->probe_groups) {
212                 if (!probe_group)
213                         printf(" across all probe groups");
214                 else
215                         printf(" on probe group %s", probe_group->name);
216         }
217         printf(":\n");
218         opts = g_variant_get_fixed_array(gvar_opts, &num_opts, sizeof(int32_t));
219         for (o = 0; o < num_opts; o++) {
220                 if (!(srci = sr_config_info_get(opts[o])))
221                         continue;
222
223                 if (srci->key == SR_CONF_TRIGGER_TYPE) {
224                         if (sr_config_list(sdi->driver, sdi, probe_group, srci->key,
225                                         &gvar) != SR_OK) {
226                                 printf("\n");
227                                 continue;
228                         }
229                         charopts = g_variant_get_string(gvar, NULL);
230                         printf("    Supported triggers: ");
231                         while (*charopts) {
232                                 printf("%c ", *charopts);
233                                 charopts++;
234                         }
235                         printf("\n");
236                         g_variant_unref(gvar);
237
238                 } else if (srci->key == SR_CONF_SAMPLERATE) {
239                         /* Supported samplerates */
240                         printf("    %s", srci->id);
241                         if (sr_config_list(sdi->driver, sdi, probe_group, SR_CONF_SAMPLERATE,
242                                         &gvar_dict) != SR_OK) {
243                                 printf("\n");
244                                 continue;
245                         }
246                         if ((gvar_list = g_variant_lookup_value(gvar_dict,
247                                         "samplerates", G_VARIANT_TYPE("at")))) {
248                                 uint64 = g_variant_get_fixed_array(gvar_list,
249                                                 &num_elements, sizeof(uint64_t));
250                                 printf(" - supported samplerates:\n");
251                                 for (i = 0; i < num_elements; i++) {
252                                         if (!(s = sr_samplerate_string(uint64[i])))
253                                                 continue;
254                                         printf("      %s\n", s);
255                                         g_free(s);
256                                 }
257                                 g_variant_unref(gvar_list);
258                         } else if ((gvar_list = g_variant_lookup_value(gvar_dict,
259                                         "samplerate-steps", G_VARIANT_TYPE("at")))) {
260                                 uint64 = g_variant_get_fixed_array(gvar_list,
261                                                 &num_elements, sizeof(uint64_t));
262                                 /* low */
263                                 if (!(s = sr_samplerate_string(uint64[0])))
264                                         continue;
265                                 printf(" (%s", s);
266                                 g_free(s);
267                                 /* high */
268                                 if (!(s = sr_samplerate_string(uint64[1])))
269                                         continue;
270                                 printf(" - %s", s);
271                                 g_free(s);
272                                 /* step */
273                                 if (!(s = sr_samplerate_string(uint64[2])))
274                                         continue;
275                                 printf(" in steps of %s)\n", s);
276                                 g_free(s);
277                                 g_variant_unref(gvar_list);
278                         }
279                         g_variant_unref(gvar_dict);
280
281                 } else if (srci->key == SR_CONF_BUFFERSIZE) {
282                         /* Supported buffer sizes */
283                         printf("    %s", srci->id);
284                         if (sr_config_list(sdi->driver, sdi, probe_group,
285                                         SR_CONF_BUFFERSIZE, &gvar_list) != SR_OK) {
286                                 printf("\n");
287                                 continue;
288                         }
289                         uint64 = g_variant_get_fixed_array(gvar_list,
290                                         &num_elements, sizeof(uint64_t));
291                         printf(" - supported buffer sizes:\n");
292                         for (i = 0; i < num_elements; i++)
293                                 printf("      %"PRIu64"\n", uint64[i]);
294                         g_variant_unref(gvar_list);
295
296                 } else if (srci->key == SR_CONF_TIMEBASE) {
297                         /* Supported time bases */
298                         printf("    %s", srci->id);
299                         if (sr_config_list(sdi->driver, sdi, probe_group,
300                                         SR_CONF_TIMEBASE, &gvar_list) != SR_OK) {
301                                 printf("\n");
302                                 continue;
303                         }
304                         printf(" - supported time bases:\n");
305                         num_elements = g_variant_n_children(gvar_list);
306                         for (i = 0; i < num_elements; i++) {
307                                 gvar = g_variant_get_child_value(gvar_list, i);
308                                 g_variant_get(gvar, "(tt)", &p, &q);
309                                 s = sr_period_string(p * q);
310                                 printf("      %s\n", s);
311                                 g_free(s);
312                         }
313                         g_variant_unref(gvar_list);
314
315                 } else if (srci->key == SR_CONF_VDIV) {
316                         /* Supported volts/div values */
317                         printf("    %s", srci->id);
318                         if (sr_config_list(sdi->driver, sdi, probe_group,
319                                         SR_CONF_VDIV, &gvar_list) != SR_OK) {
320                                 printf("\n");
321                                 continue;
322                         }
323                         printf(" - supported volts/div:\n");
324                         num_elements = g_variant_n_children(gvar_list);
325                         for (i = 0; i < num_elements; i++) {
326                                 gvar = g_variant_get_child_value(gvar_list, i);
327                                 g_variant_get(gvar, "(tt)", &p, &q);
328                                 s = sr_voltage_string(p, q);
329                                 printf("      %s\n", s);
330                                 g_free(s);
331                         }
332                         g_variant_unref(gvar_list);
333
334                 } else if (srci->datatype == SR_T_CHAR) {
335                         printf("    %s: ", srci->id);
336                         if (sr_config_get(sdi->driver, sdi, probe_group, srci->key,
337                                         &gvar) == SR_OK) {
338                                 tmp_str = g_strdup(g_variant_get_string(gvar, NULL));
339                                 g_variant_unref(gvar);
340                         } else
341                                 tmp_str = NULL;
342
343                         if (sr_config_list(sdi->driver, sdi, probe_group, srci->key,
344                                         &gvar) != SR_OK) {
345                                 printf("\n");
346                                 continue;
347                         }
348
349                         stropts = g_variant_get_strv(gvar, &num_elements);
350                         for (i = 0; i < num_elements; i++) {
351                                 if (i)
352                                         printf(", ");
353                                 printf("%s", stropts[i]);
354                                 if (tmp_str && !strcmp(tmp_str, stropts[i]))
355                                         printf(" (current)");
356                         }
357                         printf("\n");
358                         g_free(stropts);
359                         g_free(tmp_str);
360                         g_variant_unref(gvar);
361
362                 } else if (srci->datatype == SR_T_UINT64_RANGE) {
363                         printf("    %s: ", srci->id);
364                         if (sr_config_list(sdi->driver, sdi, probe_group, srci->key,
365                                         &gvar_list) != SR_OK) {
366                                 printf("\n");
367                                 continue;
368                         }
369
370                         if (sr_config_get(sdi->driver, sdi, NULL, srci->key, &gvar) == SR_OK) {
371                                 g_variant_get(gvar, "(tt)", &cur_low, &cur_high);
372                                 g_variant_unref(gvar);
373                         } else {
374                                 cur_low = 0;
375                                 cur_high = 0;
376                         }
377
378                         num_elements = g_variant_n_children(gvar_list);
379                         for (i = 0; i < num_elements; i++) {
380                                 gvar = g_variant_get_child_value(gvar_list, i);
381                                 g_variant_get(gvar, "(tt)", &low, &high);
382                                 g_variant_unref(gvar);
383                                 if (i)
384                                         printf(", ");
385                                 printf("%"PRIu64"-%"PRIu64, low, high);
386                                 if (low == cur_low && high == cur_high)
387                                         printf(" (current)");
388                         }
389                         printf("\n");
390                         g_variant_unref(gvar_list);
391
392                 } else if (srci->datatype == SR_T_BOOL) {
393                         printf("    %s: ", srci->id);
394                         if (sr_config_get(sdi->driver, sdi, NULL, srci->key,
395                                         &gvar) == SR_OK) {
396                                 if (g_variant_get_boolean(gvar))
397                                         printf("on (current), off\n");
398                                 else
399                                         printf("on, off (current)\n");
400                                 g_variant_unref(gvar);
401                         } else
402                                 printf("on, off\n");
403
404                 } else {
405
406                         /* Everything else */
407                         printf("    %s\n", srci->id);
408                 }
409         }
410         g_variant_unref(gvar_opts);
411
412         sr_dev_close(sdi);
413         g_slist_free(devices);
414
415 }
416
417 #ifdef HAVE_SRD
418 void show_pd_detail(void)
419 {
420         GSList *l;
421         struct srd_decoder *dec;
422         struct srd_decoder_option *o;
423         char **pdtokens, **pdtok, *optsep, **ann, *val, *doc;
424         struct srd_probe *p;
425
426         pdtokens = g_strsplit(opt_pds, ",", -1);
427         for (pdtok = pdtokens; *pdtok; pdtok++) {
428                 /* Strip options. */
429                 if ((optsep = strchr(*pdtok, ':')))
430                         *optsep = '\0';
431                 if (!(dec = srd_decoder_get_by_id(*pdtok))) {
432                         g_critical("Protocol decoder %s not found.", *pdtok);
433                         return;
434                 }
435                 printf("ID: %s\nName: %s\nLong name: %s\nDescription: %s\n",
436                                 dec->id, dec->name, dec->longname, dec->desc);
437                 printf("License: %s\n", dec->license);
438                 printf("Annotations:\n");
439                 if (dec->annotations) {
440                         for (l = dec->annotations; l; l = l->next) {
441                                 ann = l->data;
442                                 printf("- %s\n  %s\n", ann[0], ann[1]);
443                         }
444                 } else {
445                         printf("None.\n");
446                 }
447                 printf("Required probes:\n");
448                 if (dec->probes) {
449                         for (l = dec->probes; l; l = l->next) {
450                                 p = l->data;
451                                 printf("- %s (%s): %s\n",
452                                        p->name, p->id, p->desc);
453                         }
454                 } else {
455                         printf("None.\n");
456                 }
457                 printf("Optional probes:\n");
458                 if (dec->opt_probes) {
459                         for (l = dec->opt_probes; l; l = l->next) {
460                                 p = l->data;
461                                 printf("- %s (%s): %s\n",
462                                        p->name, p->id, p->desc);
463                         }
464                 } else {
465                         printf("None.\n");
466                 }
467                 if (dec->options) {
468                         printf("Options:\n");
469                         for (l = dec->options; l; l = l->next) {
470                                 o = l->data;
471                                 val = g_variant_print(o->def, FALSE);
472                                 printf("- %s: %s (default %s)\n", o->id, o->desc, val);
473                                 g_free(val);
474                         }
475                 }
476                 if ((doc = srd_decoder_doc_get(dec))) {
477                         printf("Documentation:\n%s\n",
478                                doc[0] == '\n' ? doc + 1 : doc);
479                         g_free(doc);
480                 }
481         }
482
483         g_strfreev(pdtokens);
484 }
485 #endif
486