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