Bert Vermeulen | 2be182e | 2013-11-17 13:15:38 +0100 | [diff] [blame] | 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 | |
Uwe Hermann | 20fb52e | 2013-11-19 11:48:06 +0100 | [diff] [blame] | 20 | #include "sigrok-cli.h" |
Bert Vermeulen | 2be182e | 2013-11-17 13:15:38 +0100 | [diff] [blame] | 21 | #include "config.h" |
| 22 | #include <glib.h> |
Bert Vermeulen | 2be182e | 2013-11-17 13:15:38 +0100 | [diff] [blame] | 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 | |
Bert Vermeulen | 02c6593 | 2014-01-19 17:22:19 +0100 | [diff] [blame^] | 190 | /* Selected probes and probe group may affect which options are |
| 191 | * returned, or which values for them. */ |
| 192 | select_probes(sdi); |
Bert Vermeulen | 2be182e | 2013-11-17 13:15:38 +0100 | [diff] [blame] | 193 | probe_group = select_probe_group(sdi); |
Bert Vermeulen | 02c6593 | 2014-01-19 17:22:19 +0100 | [diff] [blame^] | 194 | |
Bert Vermeulen | 2be182e | 2013-11-17 13:15:38 +0100 | [diff] [blame] | 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 | |
Bert Vermeulen | 02c6593 | 2014-01-19 17:22:19 +0100 | [diff] [blame^] | 242 | } else if (srci->key == SR_CONF_LIMIT_SAMPLES) { |
| 243 | /* If implemented in config_list(), this denotes the |
| 244 | * maximum number of samples a device can send. This |
| 245 | * really applies only to logic analyzers, and then |
| 246 | * only to those that don't support compression, or |
| 247 | * have it turned off by default. The values returned |
| 248 | * are the low/high limits. */ |
| 249 | if (sr_config_list(sdi->driver, sdi, probe_group, srci->key, |
| 250 | &gvar) != SR_OK) { |
| 251 | continue; |
| 252 | } |
| 253 | g_variant_get(gvar, "(tt)", &low, &high); |
| 254 | g_variant_unref(gvar); |
| 255 | printf(" Maximum number of samples: %"PRIu64"\n", high); |
| 256 | |
Bert Vermeulen | 2be182e | 2013-11-17 13:15:38 +0100 | [diff] [blame] | 257 | } else if (srci->key == SR_CONF_SAMPLERATE) { |
| 258 | /* Supported samplerates */ |
| 259 | printf(" %s", srci->id); |
| 260 | if (sr_config_list(sdi->driver, sdi, probe_group, SR_CONF_SAMPLERATE, |
| 261 | &gvar_dict) != SR_OK) { |
| 262 | printf("\n"); |
| 263 | continue; |
| 264 | } |
| 265 | if ((gvar_list = g_variant_lookup_value(gvar_dict, |
| 266 | "samplerates", G_VARIANT_TYPE("at")))) { |
| 267 | uint64 = g_variant_get_fixed_array(gvar_list, |
| 268 | &num_elements, sizeof(uint64_t)); |
| 269 | printf(" - supported samplerates:\n"); |
| 270 | for (i = 0; i < num_elements; i++) { |
| 271 | if (!(s = sr_samplerate_string(uint64[i]))) |
| 272 | continue; |
| 273 | printf(" %s\n", s); |
| 274 | g_free(s); |
| 275 | } |
| 276 | g_variant_unref(gvar_list); |
| 277 | } else if ((gvar_list = g_variant_lookup_value(gvar_dict, |
| 278 | "samplerate-steps", G_VARIANT_TYPE("at")))) { |
| 279 | uint64 = g_variant_get_fixed_array(gvar_list, |
| 280 | &num_elements, sizeof(uint64_t)); |
| 281 | /* low */ |
| 282 | if (!(s = sr_samplerate_string(uint64[0]))) |
| 283 | continue; |
| 284 | printf(" (%s", s); |
| 285 | g_free(s); |
| 286 | /* high */ |
| 287 | if (!(s = sr_samplerate_string(uint64[1]))) |
| 288 | continue; |
| 289 | printf(" - %s", s); |
| 290 | g_free(s); |
| 291 | /* step */ |
| 292 | if (!(s = sr_samplerate_string(uint64[2]))) |
| 293 | continue; |
| 294 | printf(" in steps of %s)\n", s); |
| 295 | g_free(s); |
| 296 | g_variant_unref(gvar_list); |
| 297 | } |
| 298 | g_variant_unref(gvar_dict); |
| 299 | |
| 300 | } else if (srci->key == SR_CONF_BUFFERSIZE) { |
| 301 | /* Supported buffer sizes */ |
| 302 | printf(" %s", srci->id); |
| 303 | if (sr_config_list(sdi->driver, sdi, probe_group, |
| 304 | SR_CONF_BUFFERSIZE, &gvar_list) != SR_OK) { |
| 305 | printf("\n"); |
| 306 | continue; |
| 307 | } |
| 308 | uint64 = g_variant_get_fixed_array(gvar_list, |
| 309 | &num_elements, sizeof(uint64_t)); |
| 310 | printf(" - supported buffer sizes:\n"); |
| 311 | for (i = 0; i < num_elements; i++) |
| 312 | printf(" %"PRIu64"\n", uint64[i]); |
| 313 | g_variant_unref(gvar_list); |
| 314 | |
| 315 | } else if (srci->key == SR_CONF_TIMEBASE) { |
| 316 | /* Supported time bases */ |
| 317 | printf(" %s", srci->id); |
| 318 | if (sr_config_list(sdi->driver, sdi, probe_group, |
| 319 | SR_CONF_TIMEBASE, &gvar_list) != SR_OK) { |
| 320 | printf("\n"); |
| 321 | continue; |
| 322 | } |
| 323 | printf(" - supported time bases:\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_period_string(p * q); |
| 329 | printf(" %s\n", s); |
| 330 | g_free(s); |
| 331 | } |
| 332 | g_variant_unref(gvar_list); |
| 333 | |
| 334 | } else if (srci->key == SR_CONF_VDIV) { |
| 335 | /* Supported volts/div values */ |
| 336 | printf(" %s", srci->id); |
| 337 | if (sr_config_list(sdi->driver, sdi, probe_group, |
| 338 | SR_CONF_VDIV, &gvar_list) != SR_OK) { |
| 339 | printf("\n"); |
| 340 | continue; |
| 341 | } |
| 342 | printf(" - supported volts/div:\n"); |
| 343 | num_elements = g_variant_n_children(gvar_list); |
| 344 | for (i = 0; i < num_elements; i++) { |
| 345 | gvar = g_variant_get_child_value(gvar_list, i); |
| 346 | g_variant_get(gvar, "(tt)", &p, &q); |
| 347 | s = sr_voltage_string(p, q); |
| 348 | printf(" %s\n", s); |
| 349 | g_free(s); |
| 350 | } |
| 351 | g_variant_unref(gvar_list); |
| 352 | |
| 353 | } else if (srci->datatype == SR_T_CHAR) { |
| 354 | printf(" %s: ", srci->id); |
| 355 | if (sr_config_get(sdi->driver, sdi, probe_group, srci->key, |
| 356 | &gvar) == SR_OK) { |
| 357 | tmp_str = g_strdup(g_variant_get_string(gvar, NULL)); |
| 358 | g_variant_unref(gvar); |
| 359 | } else |
| 360 | tmp_str = NULL; |
| 361 | |
| 362 | if (sr_config_list(sdi->driver, sdi, probe_group, srci->key, |
| 363 | &gvar) != SR_OK) { |
| 364 | printf("\n"); |
| 365 | continue; |
| 366 | } |
| 367 | |
| 368 | stropts = g_variant_get_strv(gvar, &num_elements); |
| 369 | for (i = 0; i < num_elements; i++) { |
| 370 | if (i) |
| 371 | printf(", "); |
| 372 | printf("%s", stropts[i]); |
| 373 | if (tmp_str && !strcmp(tmp_str, stropts[i])) |
| 374 | printf(" (current)"); |
| 375 | } |
| 376 | printf("\n"); |
| 377 | g_free(stropts); |
| 378 | g_free(tmp_str); |
| 379 | g_variant_unref(gvar); |
| 380 | |
| 381 | } else if (srci->datatype == SR_T_UINT64_RANGE) { |
| 382 | printf(" %s: ", srci->id); |
| 383 | if (sr_config_list(sdi->driver, sdi, probe_group, srci->key, |
| 384 | &gvar_list) != SR_OK) { |
| 385 | printf("\n"); |
| 386 | continue; |
| 387 | } |
| 388 | |
| 389 | if (sr_config_get(sdi->driver, sdi, NULL, srci->key, &gvar) == SR_OK) { |
| 390 | g_variant_get(gvar, "(tt)", &cur_low, &cur_high); |
| 391 | g_variant_unref(gvar); |
| 392 | } else { |
| 393 | cur_low = 0; |
| 394 | cur_high = 0; |
| 395 | } |
| 396 | |
| 397 | num_elements = g_variant_n_children(gvar_list); |
| 398 | for (i = 0; i < num_elements; i++) { |
| 399 | gvar = g_variant_get_child_value(gvar_list, i); |
| 400 | g_variant_get(gvar, "(tt)", &low, &high); |
| 401 | g_variant_unref(gvar); |
| 402 | if (i) |
| 403 | printf(", "); |
| 404 | printf("%"PRIu64"-%"PRIu64, low, high); |
| 405 | if (low == cur_low && high == cur_high) |
| 406 | printf(" (current)"); |
| 407 | } |
| 408 | printf("\n"); |
| 409 | g_variant_unref(gvar_list); |
| 410 | |
| 411 | } else if (srci->datatype == SR_T_BOOL) { |
| 412 | printf(" %s: ", srci->id); |
| 413 | if (sr_config_get(sdi->driver, sdi, NULL, srci->key, |
| 414 | &gvar) == SR_OK) { |
| 415 | if (g_variant_get_boolean(gvar)) |
| 416 | printf("on (current), off\n"); |
| 417 | else |
| 418 | printf("on, off (current)\n"); |
| 419 | g_variant_unref(gvar); |
| 420 | } else |
| 421 | printf("on, off\n"); |
| 422 | |
| 423 | } else { |
| 424 | |
| 425 | /* Everything else */ |
| 426 | printf(" %s\n", srci->id); |
| 427 | } |
| 428 | } |
| 429 | g_variant_unref(gvar_opts); |
| 430 | |
| 431 | sr_dev_close(sdi); |
| 432 | g_slist_free(devices); |
| 433 | |
| 434 | } |
| 435 | |
| 436 | #ifdef HAVE_SRD |
| 437 | void show_pd_detail(void) |
| 438 | { |
| 439 | GSList *l; |
| 440 | struct srd_decoder *dec; |
| 441 | struct srd_decoder_option *o; |
| 442 | char **pdtokens, **pdtok, *optsep, **ann, *val, *doc; |
| 443 | struct srd_probe *p; |
| 444 | |
| 445 | pdtokens = g_strsplit(opt_pds, ",", -1); |
| 446 | for (pdtok = pdtokens; *pdtok; pdtok++) { |
| 447 | /* Strip options. */ |
| 448 | if ((optsep = strchr(*pdtok, ':'))) |
| 449 | *optsep = '\0'; |
| 450 | if (!(dec = srd_decoder_get_by_id(*pdtok))) { |
| 451 | g_critical("Protocol decoder %s not found.", *pdtok); |
| 452 | return; |
| 453 | } |
| 454 | printf("ID: %s\nName: %s\nLong name: %s\nDescription: %s\n", |
| 455 | dec->id, dec->name, dec->longname, dec->desc); |
| 456 | printf("License: %s\n", dec->license); |
| 457 | printf("Annotations:\n"); |
| 458 | if (dec->annotations) { |
| 459 | for (l = dec->annotations; l; l = l->next) { |
| 460 | ann = l->data; |
| 461 | printf("- %s\n %s\n", ann[0], ann[1]); |
| 462 | } |
| 463 | } else { |
| 464 | printf("None.\n"); |
| 465 | } |
| 466 | printf("Required probes:\n"); |
| 467 | if (dec->probes) { |
| 468 | for (l = dec->probes; l; l = l->next) { |
| 469 | p = l->data; |
| 470 | printf("- %s (%s): %s\n", |
| 471 | p->name, p->id, p->desc); |
| 472 | } |
| 473 | } else { |
| 474 | printf("None.\n"); |
| 475 | } |
| 476 | printf("Optional probes:\n"); |
| 477 | if (dec->opt_probes) { |
| 478 | for (l = dec->opt_probes; l; l = l->next) { |
| 479 | p = l->data; |
| 480 | printf("- %s (%s): %s\n", |
| 481 | p->name, p->id, p->desc); |
| 482 | } |
| 483 | } else { |
| 484 | printf("None.\n"); |
| 485 | } |
| 486 | if (dec->options) { |
| 487 | printf("Options:\n"); |
| 488 | for (l = dec->options; l; l = l->next) { |
| 489 | o = l->data; |
| 490 | val = g_variant_print(o->def, FALSE); |
| 491 | printf("- %s: %s (default %s)\n", o->id, o->desc, val); |
| 492 | g_free(val); |
| 493 | } |
| 494 | } |
| 495 | if ((doc = srd_decoder_doc_get(dec))) { |
| 496 | printf("Documentation:\n%s\n", |
| 497 | doc[0] == '\n' ? doc + 1 : doc); |
| 498 | g_free(doc); |
| 499 | } |
| 500 | } |
| 501 | |
| 502 | g_strfreev(pdtokens); |
| 503 | } |
| 504 | #endif |
| 505 | |