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