]> sigrok.org Git - sigrok-cli.git/blob - show.c
Add support for config keys of type G_VARIANT_TYPE_INT32.
[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_PATTERN_MODE) {
239                         /* Pattern generator modes */
240                         printf("    %s", srci->id);
241                         if (sr_config_list(sdi->driver, sdi, probe_group, srci->key,
242                                         &gvar) == SR_OK) {
243                                 printf(" - supported patterns:\n");
244                                 stropts = g_variant_get_strv(gvar, &num_elements);
245                                 for (i = 0; i < num_elements; i++)
246                                         printf("      %s\n", stropts[i]);
247                                 g_variant_unref(gvar);
248                         } else {
249                                 printf("\n");
250                         }
251
252                 } else if (srci->key == SR_CONF_SAMPLERATE) {
253                         /* Supported samplerates */
254                         printf("    %s", srci->id);
255                         if (sr_config_list(sdi->driver, sdi, probe_group, SR_CONF_SAMPLERATE,
256                                         &gvar_dict) != SR_OK) {
257                                 printf("\n");
258                                 continue;
259                         }
260                         if ((gvar_list = g_variant_lookup_value(gvar_dict,
261                                         "samplerates", G_VARIANT_TYPE("at")))) {
262                                 uint64 = g_variant_get_fixed_array(gvar_list,
263                                                 &num_elements, sizeof(uint64_t));
264                                 printf(" - supported samplerates:\n");
265                                 for (i = 0; i < num_elements; i++) {
266                                         if (!(s = sr_samplerate_string(uint64[i])))
267                                                 continue;
268                                         printf("      %s\n", s);
269                                         g_free(s);
270                                 }
271                                 g_variant_unref(gvar_list);
272                         } else if ((gvar_list = g_variant_lookup_value(gvar_dict,
273                                         "samplerate-steps", G_VARIANT_TYPE("at")))) {
274                                 uint64 = g_variant_get_fixed_array(gvar_list,
275                                                 &num_elements, sizeof(uint64_t));
276                                 /* low */
277                                 if (!(s = sr_samplerate_string(uint64[0])))
278                                         continue;
279                                 printf(" (%s", s);
280                                 g_free(s);
281                                 /* high */
282                                 if (!(s = sr_samplerate_string(uint64[1])))
283                                         continue;
284                                 printf(" - %s", s);
285                                 g_free(s);
286                                 /* step */
287                                 if (!(s = sr_samplerate_string(uint64[2])))
288                                         continue;
289                                 printf(" in steps of %s)\n", s);
290                                 g_free(s);
291                                 g_variant_unref(gvar_list);
292                         }
293                         g_variant_unref(gvar_dict);
294
295                 } else if (srci->key == SR_CONF_BUFFERSIZE) {
296                         /* Supported buffer sizes */
297                         printf("    %s", srci->id);
298                         if (sr_config_list(sdi->driver, sdi, probe_group,
299                                         SR_CONF_BUFFERSIZE, &gvar_list) != SR_OK) {
300                                 printf("\n");
301                                 continue;
302                         }
303                         uint64 = g_variant_get_fixed_array(gvar_list,
304                                         &num_elements, sizeof(uint64_t));
305                         printf(" - supported buffer sizes:\n");
306                         for (i = 0; i < num_elements; i++)
307                                 printf("      %"PRIu64"\n", uint64[i]);
308                         g_variant_unref(gvar_list);
309
310                 } else if (srci->key == SR_CONF_TIMEBASE) {
311                         /* Supported time bases */
312                         printf("    %s", srci->id);
313                         if (sr_config_list(sdi->driver, sdi, probe_group,
314                                         SR_CONF_TIMEBASE, &gvar_list) != SR_OK) {
315                                 printf("\n");
316                                 continue;
317                         }
318                         printf(" - supported time bases:\n");
319                         num_elements = g_variant_n_children(gvar_list);
320                         for (i = 0; i < num_elements; i++) {
321                                 gvar = g_variant_get_child_value(gvar_list, i);
322                                 g_variant_get(gvar, "(tt)", &p, &q);
323                                 s = sr_period_string(p * q);
324                                 printf("      %s\n", s);
325                                 g_free(s);
326                         }
327                         g_variant_unref(gvar_list);
328
329                 } else if (srci->key == SR_CONF_VDIV) {
330                         /* Supported volts/div values */
331                         printf("    %s", srci->id);
332                         if (sr_config_list(sdi->driver, sdi, probe_group,
333                                         SR_CONF_VDIV, &gvar_list) != SR_OK) {
334                                 printf("\n");
335                                 continue;
336                         }
337                         printf(" - supported volts/div:\n");
338                         num_elements = g_variant_n_children(gvar_list);
339                         for (i = 0; i < num_elements; i++) {
340                                 gvar = g_variant_get_child_value(gvar_list, i);
341                                 g_variant_get(gvar, "(tt)", &p, &q);
342                                 s = sr_voltage_string(p, q);
343                                 printf("      %s\n", s);
344                                 g_free(s);
345                         }
346                         g_variant_unref(gvar_list);
347
348                 } else if (srci->datatype == SR_T_CHAR) {
349                         printf("    %s: ", srci->id);
350                         if (sr_config_get(sdi->driver, sdi, probe_group, srci->key,
351                                         &gvar) == SR_OK) {
352                                 tmp_str = g_strdup(g_variant_get_string(gvar, NULL));
353                                 g_variant_unref(gvar);
354                         } else
355                                 tmp_str = NULL;
356
357                         if (sr_config_list(sdi->driver, sdi, probe_group, srci->key,
358                                         &gvar) != SR_OK) {
359                                 printf("\n");
360                                 continue;
361                         }
362
363                         stropts = g_variant_get_strv(gvar, &num_elements);
364                         for (i = 0; i < num_elements; i++) {
365                                 if (i)
366                                         printf(", ");
367                                 printf("%s", stropts[i]);
368                                 if (tmp_str && !strcmp(tmp_str, stropts[i]))
369                                         printf(" (current)");
370                         }
371                         printf("\n");
372                         g_free(stropts);
373                         g_free(tmp_str);
374                         g_variant_unref(gvar);
375
376                 } else if (srci->datatype == SR_T_UINT64_RANGE) {
377                         printf("    %s: ", srci->id);
378                         if (sr_config_list(sdi->driver, sdi, probe_group, srci->key,
379                                         &gvar_list) != SR_OK) {
380                                 printf("\n");
381                                 continue;
382                         }
383
384                         if (sr_config_get(sdi->driver, sdi, NULL, srci->key, &gvar) == SR_OK) {
385                                 g_variant_get(gvar, "(tt)", &cur_low, &cur_high);
386                                 g_variant_unref(gvar);
387                         } else {
388                                 cur_low = 0;
389                                 cur_high = 0;
390                         }
391
392                         num_elements = g_variant_n_children(gvar_list);
393                         for (i = 0; i < num_elements; i++) {
394                                 gvar = g_variant_get_child_value(gvar_list, i);
395                                 g_variant_get(gvar, "(tt)", &low, &high);
396                                 g_variant_unref(gvar);
397                                 if (i)
398                                         printf(", ");
399                                 printf("%"PRIu64"-%"PRIu64, low, high);
400                                 if (low == cur_low && high == cur_high)
401                                         printf(" (current)");
402                         }
403                         printf("\n");
404                         g_variant_unref(gvar_list);
405
406                 } else if (srci->datatype == SR_T_BOOL) {
407                         printf("    %s: ", srci->id);
408                         if (sr_config_get(sdi->driver, sdi, NULL, srci->key,
409                                         &gvar) == SR_OK) {
410                                 if (g_variant_get_boolean(gvar))
411                                         printf("on (current), off\n");
412                                 else
413                                         printf("on, off (current)\n");
414                                 g_variant_unref(gvar);
415                         } else
416                                 printf("on, off\n");
417
418                 } else {
419
420                         /* Everything else */
421                         printf("    %s\n", srci->id);
422                 }
423         }
424         g_variant_unref(gvar_opts);
425
426         sr_dev_close(sdi);
427         g_slist_free(devices);
428
429 }
430
431 #ifdef HAVE_SRD
432 void show_pd_detail(void)
433 {
434         GSList *l;
435         struct srd_decoder *dec;
436         struct srd_decoder_option *o;
437         char **pdtokens, **pdtok, *optsep, **ann, *val, *doc;
438         struct srd_probe *p;
439
440         pdtokens = g_strsplit(opt_pds, ",", -1);
441         for (pdtok = pdtokens; *pdtok; pdtok++) {
442                 /* Strip options. */
443                 if ((optsep = strchr(*pdtok, ':')))
444                         *optsep = '\0';
445                 if (!(dec = srd_decoder_get_by_id(*pdtok))) {
446                         g_critical("Protocol decoder %s not found.", *pdtok);
447                         return;
448                 }
449                 printf("ID: %s\nName: %s\nLong name: %s\nDescription: %s\n",
450                                 dec->id, dec->name, dec->longname, dec->desc);
451                 printf("License: %s\n", dec->license);
452                 printf("Annotations:\n");
453                 if (dec->annotations) {
454                         for (l = dec->annotations; l; l = l->next) {
455                                 ann = l->data;
456                                 printf("- %s\n  %s\n", ann[0], ann[1]);
457                         }
458                 } else {
459                         printf("None.\n");
460                 }
461                 printf("Required probes:\n");
462                 if (dec->probes) {
463                         for (l = dec->probes; l; l = l->next) {
464                                 p = l->data;
465                                 printf("- %s (%s): %s\n",
466                                        p->name, p->id, p->desc);
467                         }
468                 } else {
469                         printf("None.\n");
470                 }
471                 printf("Optional probes:\n");
472                 if (dec->opt_probes) {
473                         for (l = dec->opt_probes; l; l = l->next) {
474                                 p = l->data;
475                                 printf("- %s (%s): %s\n",
476                                        p->name, p->id, p->desc);
477                         }
478                 } else {
479                         printf("None.\n");
480                 }
481                 if (dec->options) {
482                         printf("Options:\n");
483                         for (l = dec->options; l; l = l->next) {
484                                 o = l->data;
485                                 val = g_variant_print(o->def, FALSE);
486                                 printf("- %s: %s (default %s)\n", o->id, o->desc, val);
487                                 g_free(val);
488                         }
489                 }
490                 if ((doc = srd_decoder_doc_get(dec))) {
491                         printf("Documentation:\n%s\n",
492                                doc[0] == '\n' ? doc + 1 : doc);
493                         g_free(doc);
494                 }
495         }
496
497         g_strfreev(pdtokens);
498 }
499 #endif
500