]>
Commit | Line | Data |
---|---|---|
43e5747a | 1 | /* |
630293b4 | 2 | * This file is part of the sigrok-cli project. |
43e5747a | 3 | * |
110aa72a | 4 | * Copyright (C) 2012 Bert Vermeulen <bert@biot.com> |
43e5747a UH |
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 | ||
de0a066e BV |
20 | #include "config.h" |
21 | #ifdef HAVE_SRD | |
efdb6a22 | 22 | #include <libsigrokdecode/libsigrokdecode.h> /* First, so we avoid a _POSIX_C_SOURCE warning. */ |
de0a066e | 23 | #endif |
43e5747a UH |
24 | #include <stdio.h> |
25 | #include <stdlib.h> | |
26 | #include <unistd.h> | |
27 | #include <string.h> | |
28 | #include <time.h> | |
29 | #include <sys/time.h> | |
30 | #include <inttypes.h> | |
31 | #include <sys/types.h> | |
32 | #include <sys/stat.h> | |
33 | #include <errno.h> | |
34 | #include <glib.h> | |
35 | #include <glib/gstdio.h> | |
60ea8937 | 36 | #include <libsigrok/libsigrok.h> |
43e5747a | 37 | #include "sigrok-cli.h" |
43e5747a UH |
38 | |
39 | #define DEFAULT_OUTPUT_FORMAT "bits:width=64" | |
40 | ||
e2fe62cc PS |
41 | static struct sr_context *sr_ctx = NULL; |
42 | ||
1999ae06 | 43 | static uint64_t limit_samples = 0; |
ce48d892 | 44 | static uint64_t limit_frames = 0; |
1999ae06 UH |
45 | static struct sr_output_format *output_format = NULL; |
46 | static int default_output_format = FALSE; | |
47 | static char *output_format_param = NULL; | |
de0a066e | 48 | #ifdef HAVE_SRD |
120f9ee7 | 49 | static GHashTable *pd_ann_visible = NULL; |
de0a066e | 50 | #endif |
c27450ea | 51 | static GByteArray *savebuf; |
43e5747a UH |
52 | |
53 | static gboolean opt_version = FALSE; | |
54 | static gint opt_loglevel = SR_LOG_WARN; /* Show errors+warnings per default. */ | |
ea7741ee | 55 | static gboolean opt_scan_devs = FALSE; |
43e5747a UH |
56 | static gboolean opt_wait_trigger = FALSE; |
57 | static gchar *opt_input_file = NULL; | |
58 | static gchar *opt_output_file = NULL; | |
9e2e414f | 59 | static gchar *opt_drv = NULL; |
ea7741ee | 60 | static gchar *opt_config = NULL; |
43e5747a UH |
61 | static gchar *opt_probes = NULL; |
62 | static gchar *opt_triggers = NULL; | |
63 | static gchar *opt_pds = NULL; | |
de0a066e | 64 | #ifdef HAVE_SRD |
9f4a898e | 65 | static gchar *opt_pd_stack = NULL; |
b6bd032d | 66 | static gchar *opt_pd_annotations = NULL; |
de0a066e | 67 | #endif |
43e5747a | 68 | static gchar *opt_input_format = NULL; |
76ae913d | 69 | static gchar *opt_output_format = NULL; |
22981b2c | 70 | static gchar *opt_show = NULL; |
43e5747a UH |
71 | static gchar *opt_time = NULL; |
72 | static gchar *opt_samples = NULL; | |
ce48d892 | 73 | static gchar *opt_frames = NULL; |
43e5747a | 74 | static gchar *opt_continuous = NULL; |
2d73284e | 75 | static gchar *opt_set = NULL; |
43e5747a UH |
76 | |
77 | static GOptionEntry optargs[] = { | |
03996962 BV |
78 | {"version", 'V', 0, G_OPTION_ARG_NONE, &opt_version, |
79 | "Show version and support list", NULL}, | |
80 | {"loglevel", 'l', 0, G_OPTION_ARG_INT, &opt_loglevel, | |
ea7741ee BV |
81 | "Set loglevel (5 is most verbose)", NULL}, |
82 | {"driver", 'd', 0, G_OPTION_ARG_STRING, &opt_drv, | |
83 | "The driver to use", NULL}, | |
84 | {"config", 'c', 0, G_OPTION_ARG_STRING, &opt_config, | |
85 | "Specify device configuration options", NULL}, | |
03996962 BV |
86 | {"input-file", 'i', 0, G_OPTION_ARG_FILENAME, &opt_input_file, |
87 | "Load input from file", NULL}, | |
88 | {"input-format", 'I', 0, G_OPTION_ARG_STRING, &opt_input_format, | |
89 | "Input format", NULL}, | |
90 | {"output-file", 'o', 0, G_OPTION_ARG_FILENAME, &opt_output_file, | |
91 | "Save output to file", NULL}, | |
92 | {"output-format", 'O', 0, G_OPTION_ARG_STRING, &opt_output_format, | |
93 | "Output format", NULL}, | |
94 | {"probes", 'p', 0, G_OPTION_ARG_STRING, &opt_probes, | |
95 | "Probes to use", NULL}, | |
96 | {"triggers", 't', 0, G_OPTION_ARG_STRING, &opt_triggers, | |
97 | "Trigger configuration", NULL}, | |
98 | {"wait-trigger", 'w', 0, G_OPTION_ARG_NONE, &opt_wait_trigger, | |
99 | "Wait for trigger", NULL}, | |
de0a066e | 100 | #ifdef HAVE_SRD |
ea7741ee | 101 | {"protocol-decoders", 'P', 0, G_OPTION_ARG_STRING, &opt_pds, |
03996962 | 102 | "Protocol decoders to run", NULL}, |
ea7741ee | 103 | {"protocol-decoder-stack", 'S', 0, G_OPTION_ARG_STRING, &opt_pd_stack, |
03996962 | 104 | "Protocol decoder stack", NULL}, |
b6bd032d UH |
105 | {"protocol-decoder-annotations", 'A', 0, G_OPTION_ARG_STRING, &opt_pd_annotations, |
106 | "Protocol decoder annotation(s) to show", NULL}, | |
de0a066e | 107 | #endif |
ea7741ee BV |
108 | {"scan", 0, 0, G_OPTION_ARG_NONE, &opt_scan_devs, |
109 | "Scan for devices", NULL}, | |
22981b2c BV |
110 | {"show", 0, 0, G_OPTION_ARG_NONE, &opt_show, |
111 | "Show device detail", NULL}, | |
03996962 BV |
112 | {"time", 0, 0, G_OPTION_ARG_STRING, &opt_time, |
113 | "How long to sample (ms)", NULL}, | |
114 | {"samples", 0, 0, G_OPTION_ARG_STRING, &opt_samples, | |
115 | "Number of samples to acquire", NULL}, | |
ce48d892 BV |
116 | {"frames", 0, 0, G_OPTION_ARG_STRING, &opt_frames, |
117 | "Number of frames to acquire", NULL}, | |
03996962 BV |
118 | {"continuous", 0, 0, G_OPTION_ARG_NONE, &opt_continuous, |
119 | "Sample continuously", NULL}, | |
2d73284e | 120 | {"set", 0, 0, G_OPTION_ARG_NONE, &opt_set, "Set device options only", NULL}, |
43e5747a UH |
121 | {NULL, 0, 0, 0, NULL, NULL, NULL} |
122 | }; | |
123 | ||
9e2e414f | 124 | |
995713f4 | 125 | /* Convert driver options hash to GSList of struct sr_config. */ |
9e2e414f BV |
126 | static GSList *hash_to_hwopt(GHashTable *hash) |
127 | { | |
995713f4 BV |
128 | const struct sr_config_info *srci; |
129 | struct sr_config *src; | |
9e2e414f BV |
130 | GList *gl, *keys; |
131 | GSList *opts; | |
132 | char *key, *value; | |
133 | ||
134 | keys = g_hash_table_get_keys(hash); | |
135 | opts = NULL; | |
136 | for (gl = keys; gl; gl = gl->next) { | |
137 | key = gl->data; | |
74b9bf0c | 138 | if (!(srci = sr_config_info_name_get(key))) { |
9e2e414f BV |
139 | g_critical("Unknown option %s", key); |
140 | return NULL; | |
141 | } | |
995713f4 BV |
142 | src = g_try_malloc(sizeof(struct sr_config)); |
143 | src->key = srci->key; | |
cfd3ec6e | 144 | value = g_hash_table_lookup(hash, key); |
1616f663 | 145 | src->data = g_variant_new_string(value); |
995713f4 | 146 | opts = g_slist_append(opts, src); |
9e2e414f BV |
147 | } |
148 | g_list_free(keys); | |
149 | ||
150 | return opts; | |
151 | } | |
152 | ||
995713f4 | 153 | static void free_drvopts(struct sr_config *src) |
a2853311 | 154 | { |
1616f663 | 155 | g_variant_unref(src->data); |
995713f4 | 156 | g_free(src); |
a2853311 AG |
157 | } |
158 | ||
9e2e414f BV |
159 | static GSList *device_scan(void) |
160 | { | |
161 | struct sr_dev_driver **drivers, *driver; | |
162 | GHashTable *drvargs; | |
163 | GSList *drvopts, *devices, *tmpdevs, *l; | |
164 | int i; | |
165 | char *drvname; | |
166 | ||
167 | if (opt_drv) { | |
168 | drvargs = parse_generic_arg(opt_drv, TRUE); | |
169 | drvname = g_strdup(g_hash_table_lookup(drvargs, "sigrok_key")); | |
170 | g_hash_table_remove(drvargs, "sigrok_key"); | |
171 | driver = NULL; | |
172 | drivers = sr_driver_list(); | |
173 | for (i = 0; drivers[i]; i++) { | |
174 | if (strcmp(drivers[i]->name, drvname)) | |
175 | continue; | |
176 | driver = drivers[i]; | |
177 | } | |
178 | if (!driver) { | |
179 | g_critical("Driver %s not found.", drvname); | |
180 | return NULL; | |
181 | } | |
182 | g_free(drvname); | |
e2fe62cc | 183 | if (sr_driver_init(sr_ctx, driver) != SR_OK) { |
9e2e414f BV |
184 | g_critical("Failed to initialize driver."); |
185 | return NULL; | |
186 | } | |
187 | drvopts = NULL; | |
17982bc9 BV |
188 | if (g_hash_table_size(drvargs) > 0) { |
189 | if (!(drvopts = hash_to_hwopt(drvargs))) { | |
9e2e414f | 190 | /* Unknown options, already logged. */ |
17982bc9 | 191 | g_hash_table_destroy(drvargs); |
9e2e414f | 192 | return NULL; |
17982bc9 BV |
193 | } |
194 | } | |
195 | g_hash_table_destroy(drvargs); | |
9e2e414f | 196 | devices = sr_driver_scan(driver, drvopts); |
995713f4 | 197 | g_slist_free_full(drvopts, (GDestroyNotify)free_drvopts); |
9e2e414f BV |
198 | } else { |
199 | /* No driver specified, let them all scan on their own. */ | |
200 | devices = NULL; | |
201 | drivers = sr_driver_list(); | |
202 | for (i = 0; drivers[i]; i++) { | |
203 | driver = drivers[i]; | |
e2fe62cc | 204 | if (sr_driver_init(sr_ctx, driver) != SR_OK) { |
9e2e414f BV |
205 | g_critical("Failed to initialize driver."); |
206 | return NULL; | |
207 | } | |
208 | tmpdevs = sr_driver_scan(driver, NULL); | |
209 | for (l = tmpdevs; l; l = l->next) | |
210 | devices = g_slist_append(devices, l->data); | |
211 | g_slist_free(tmpdevs); | |
212 | } | |
213 | } | |
214 | ||
215 | return devices; | |
216 | } | |
217 | ||
43e5747a UH |
218 | static void show_version(void) |
219 | { | |
a210c4cc | 220 | struct sr_dev_driver **drivers; |
43e5747a UH |
221 | struct sr_input_format **inputs; |
222 | struct sr_output_format **outputs; | |
43e5747a | 223 | int i; |
de0a066e BV |
224 | #ifdef HAVE_SRD |
225 | struct srd_decoder *dec; | |
226 | const GSList *l; | |
227 | #endif | |
43e5747a UH |
228 | |
229 | printf("sigrok-cli %s\n\n", VERSION); | |
15d920a9 | 230 | |
4d167240 UH |
231 | printf("Using libsigrok %s (lib version %s).\n", |
232 | sr_package_version_string_get(), sr_lib_version_string_get()); | |
de0a066e | 233 | #ifdef HAVE_SRD |
4d167240 UH |
234 | printf("Using libsigrokdecode %s (lib version %s).\n\n", |
235 | srd_package_version_string_get(), srd_lib_version_string_get()); | |
de0a066e | 236 | #endif |
4d167240 | 237 | |
43e5747a | 238 | printf("Supported hardware drivers:\n"); |
a214a2eb | 239 | drivers = sr_driver_list(); |
a210c4cc UH |
240 | for (i = 0; drivers[i]; i++) { |
241 | printf(" %-20s %s\n", drivers[i]->name, drivers[i]->longname); | |
43e5747a UH |
242 | } |
243 | printf("\n"); | |
244 | ||
245 | printf("Supported input formats:\n"); | |
246 | inputs = sr_input_list(); | |
247 | for (i = 0; inputs[i]; i++) | |
248 | printf(" %-20s %s\n", inputs[i]->id, inputs[i]->description); | |
249 | printf("\n"); | |
250 | ||
251 | printf("Supported output formats:\n"); | |
252 | outputs = sr_output_list(); | |
253 | for (i = 0; outputs[i]; i++) | |
254 | printf(" %-20s %s\n", outputs[i]->id, outputs[i]->description); | |
255 | printf("\n"); | |
256 | ||
de0a066e | 257 | #ifdef HAVE_SRD |
15d920a9 BV |
258 | if (srd_init(NULL) == SRD_OK) { |
259 | printf("Supported protocol decoders:\n"); | |
1b34803d UH |
260 | srd_decoder_load_all(); |
261 | for (l = srd_decoder_list(); l; l = l->next) { | |
15d920a9 BV |
262 | dec = l->data; |
263 | printf(" %-20s %s\n", dec->id, dec->longname); | |
20622036 UH |
264 | /* Print protocol description upon "-l 3" or higher. */ |
265 | if (opt_loglevel >= SR_LOG_INFO) | |
266 | printf(" %-20s %s\n", "", dec->desc); | |
15d920a9 BV |
267 | } |
268 | srd_exit(); | |
43e5747a UH |
269 | } |
270 | printf("\n"); | |
de0a066e | 271 | #endif |
43e5747a UH |
272 | } |
273 | ||
22981b2c | 274 | static void print_dev_line(const struct sr_dev_inst *sdi) |
43e5747a | 275 | { |
11b62b6f BV |
276 | struct sr_probe *probe; |
277 | GSList *l; | |
fe6970ec BV |
278 | GString *s; |
279 | GVariant *gvar; | |
43e5747a | 280 | |
fe6970ec BV |
281 | s = g_string_sized_new(128); |
282 | g_string_assign(s, sdi->driver->name); | |
283 | if (sr_config_get(sdi->driver, SR_CONF_CONN, &gvar, sdi) == SR_OK) { | |
284 | g_string_append(s, ":conn="); | |
285 | g_string_append(s, g_variant_get_string(gvar, NULL)); | |
286 | g_variant_unref(gvar); | |
287 | } | |
288 | g_string_append(s, " - "); | |
43e5747a | 289 | if (sdi->vendor && sdi->vendor[0]) |
fe6970ec | 290 | g_string_append_printf(s, "%s ", sdi->vendor); |
43e5747a | 291 | if (sdi->model && sdi->model[0]) |
fe6970ec | 292 | g_string_append_printf(s, "%s ", sdi->model); |
43e5747a | 293 | if (sdi->version && sdi->version[0]) |
fe6970ec | 294 | g_string_append_printf(s, "%s ", sdi->version); |
11b62b6f BV |
295 | if (sdi->probes) { |
296 | if (g_slist_length(sdi->probes) == 1) { | |
297 | probe = sdi->probes->data; | |
fe6970ec | 298 | g_string_append_printf(s, "with 1 probe: %s", probe->name); |
11b62b6f | 299 | } else { |
fe6970ec | 300 | g_string_append_printf(s, "with %d probes:", g_slist_length(sdi->probes)); |
11b62b6f BV |
301 | for (l = sdi->probes; l; l = l->next) { |
302 | probe = l->data; | |
fe6970ec | 303 | g_string_append_printf(s, " %s", probe->name); |
11b62b6f BV |
304 | } |
305 | } | |
4d326ca1 | 306 | } |
fe6970ec BV |
307 | g_string_append_printf(s, "\n"); |
308 | printf("%s", s->str); | |
309 | g_string_free(s, TRUE); | |
310 | ||
43e5747a UH |
311 | } |
312 | ||
1e0f9ed9 | 313 | static void show_dev_list(void) |
43e5747a | 314 | { |
22981b2c BV |
315 | struct sr_dev_inst *sdi; |
316 | GSList *devices, *l; | |
43e5747a | 317 | |
22981b2c | 318 | if (!(devices = device_scan())) |
43e5747a UH |
319 | return; |
320 | ||
22981b2c BV |
321 | printf("The following devices were found:\n"); |
322 | for (l = devices; l; l = l->next) { | |
323 | sdi = l->data; | |
324 | print_dev_line(sdi); | |
43e5747a | 325 | } |
22981b2c BV |
326 | g_slist_free(devices); |
327 | ||
43e5747a UH |
328 | } |
329 | ||
1e0f9ed9 | 330 | static void show_dev_detail(void) |
43e5747a | 331 | { |
22981b2c | 332 | struct sr_dev_inst *sdi; |
995713f4 | 333 | const struct sr_config_info *srci; |
22981b2c | 334 | GSList *devices; |
1616f663 BV |
335 | GVariant *gvar_opts, *gvar_dict, *gvar_list, *gvar; |
336 | gsize num_opts, num_elements; | |
43d3d80b BV |
337 | const uint64_t *uint64, p, q, low, high; |
338 | uint64_t cur_low, cur_high; | |
e07340ad | 339 | const int32_t *opts; |
43d3d80b BV |
340 | unsigned int num_devices, o, i; |
341 | char *tmp_str; | |
1616f663 | 342 | char *s; |
9c9c1080 | 343 | const char *charopts, **stropts; |
43e5747a | 344 | |
22981b2c BV |
345 | if (!(devices = device_scan())) { |
346 | g_critical("No devices found."); | |
43e5747a UH |
347 | return; |
348 | } | |
349 | ||
22981b2c BV |
350 | num_devices = g_slist_length(devices); |
351 | if (num_devices > 1) { | |
7d559a46 | 352 | g_critical("%d devices found. Use --scan to show them, " |
d79d4806 | 353 | "and select one to show.", num_devices); |
41d7fb23 BV |
354 | return; |
355 | } | |
22981b2c | 356 | |
41d7fb23 | 357 | sdi = devices->data; |
22981b2c | 358 | print_dev_line(sdi); |
43e5747a | 359 | |
0687f23d BV |
360 | if (sr_dev_open(sdi) != SR_OK) { |
361 | g_critical("Failed to open device."); | |
8e8827f4 BV |
362 | return; |
363 | } | |
364 | ||
1616f663 BV |
365 | if ((sr_config_list(sdi->driver, SR_CONF_SCAN_OPTIONS, &gvar_opts, |
366 | NULL) == SR_OK)) { | |
367 | opts = g_variant_get_fixed_array(gvar_opts, &num_elements, | |
368 | sizeof(int32_t)); | |
22981b2c | 369 | printf("Supported driver options:\n"); |
1616f663 BV |
370 | for (i = 0; i < num_elements; i++) { |
371 | if (!(srci = sr_config_info_get(opts[i]))) | |
22981b2c | 372 | continue; |
995713f4 | 373 | printf(" %s\n", srci->id); |
22981b2c | 374 | } |
1616f663 | 375 | g_variant_unref(gvar_opts); |
22981b2c BV |
376 | } |
377 | ||
1616f663 BV |
378 | if ((sr_config_list(sdi->driver, SR_CONF_DEVICE_OPTIONS, &gvar_opts, |
379 | sdi) != SR_OK)) | |
22981b2c BV |
380 | /* Driver supports no device instance options. */ |
381 | return; | |
382 | ||
43d3d80b | 383 | printf("Supported configuration options:\n"); |
1616f663 BV |
384 | opts = g_variant_get_fixed_array(gvar_opts, &num_opts, sizeof(int32_t)); |
385 | for (o = 0; o < num_opts; o++) { | |
386 | if (!(srci = sr_config_info_get(opts[o]))) | |
43e5747a UH |
387 | continue; |
388 | ||
1616f663 BV |
389 | if (srci->key == SR_CONF_TRIGGER_TYPE) { |
390 | if (sr_config_list(sdi->driver, srci->key, &gvar, | |
391 | sdi) != SR_OK) { | |
392 | printf("\n"); | |
393 | continue; | |
394 | } | |
395 | charopts = g_variant_get_string(gvar, NULL); | |
396 | printf(" Supported triggers: "); | |
397 | while (*charopts) { | |
398 | printf("%c ", *charopts); | |
399 | charopts++; | |
400 | } | |
401 | printf("\n"); | |
402 | g_variant_unref(gvar); | |
43e5747a | 403 | |
1616f663 | 404 | } else if (srci->key == SR_CONF_PATTERN_MODE) { |
c8db7172 | 405 | /* Pattern generator modes */ |
995713f4 | 406 | printf(" %s", srci->id); |
1616f663 BV |
407 | if (sr_config_list(sdi->driver, srci->key, &gvar, |
408 | sdi) == SR_OK) { | |
0a56f4ec | 409 | printf(" - supported patterns:\n"); |
1e1316b3 BV |
410 | stropts = g_variant_get_strv(gvar, &num_elements); |
411 | for (i = 0; i < num_elements; i++) | |
9c9c1080 | 412 | printf(" %s\n", stropts[i]); |
1616f663 | 413 | g_variant_unref(gvar); |
9c9c1080 | 414 | } else { |
43e5747a | 415 | printf("\n"); |
43e5747a | 416 | } |
c8db7172 | 417 | |
b5c63de9 | 418 | } else if (srci->key == SR_CONF_SAMPLERATE) { |
43e5747a | 419 | /* Supported samplerates */ |
995713f4 | 420 | printf(" %s", srci->id); |
d2f671dc | 421 | if (sr_config_list(sdi->driver, SR_CONF_SAMPLERATE, |
1616f663 | 422 | &gvar_dict, sdi) != SR_OK) { |
43e5747a UH |
423 | printf("\n"); |
424 | continue; | |
425 | } | |
1616f663 BV |
426 | if ((gvar_list = g_variant_lookup_value(gvar_dict, |
427 | "samplerates", G_VARIANT_TYPE("at")))) { | |
4e0db44d | 428 | uint64 = g_variant_get_fixed_array(gvar_list, |
1616f663 BV |
429 | &num_elements, sizeof(uint64_t)); |
430 | printf(" - supported samplerates:\n"); | |
17982bc9 BV |
431 | for (i = 0; i < num_elements; i++) { |
432 | if (!(s = sr_samplerate_string(uint64[i]))) | |
433 | continue; | |
434 | printf(" %s\n", s); | |
435 | g_free(s); | |
436 | } | |
437 | g_variant_unref(gvar_list); | |
438 | } else if ((gvar_list = g_variant_lookup_value(gvar_dict, | |
1616f663 | 439 | "samplerate-steps", G_VARIANT_TYPE("at")))) { |
4e0db44d | 440 | uint64 = g_variant_get_fixed_array(gvar_list, |
1616f663 | 441 | &num_elements, sizeof(uint64_t)); |
43e5747a | 442 | /* low */ |
4e0db44d | 443 | if (!(s = sr_samplerate_string(uint64[0]))) |
43e5747a UH |
444 | continue; |
445 | printf(" (%s", s); | |
c2c4a0de | 446 | g_free(s); |
43e5747a | 447 | /* high */ |
4e0db44d | 448 | if (!(s = sr_samplerate_string(uint64[1]))) |
43e5747a UH |
449 | continue; |
450 | printf(" - %s", s); | |
c2c4a0de | 451 | g_free(s); |
43e5747a | 452 | /* step */ |
4e0db44d | 453 | if (!(s = sr_samplerate_string(uint64[2]))) |
43e5747a UH |
454 | continue; |
455 | printf(" in steps of %s)\n", s); | |
c2c4a0de | 456 | g_free(s); |
1e1316b3 | 457 | g_variant_unref(gvar_list); |
43e5747a | 458 | } |
1616f663 | 459 | g_variant_unref(gvar_dict); |
c8db7172 | 460 | |
b5c63de9 | 461 | } else if (srci->key == SR_CONF_BUFFERSIZE) { |
c8db7172 | 462 | /* Supported buffer sizes */ |
995713f4 | 463 | printf(" %s", srci->id); |
2777cbd2 | 464 | if (sr_config_list(sdi->driver, SR_CONF_BUFFERSIZE, |
1e1316b3 | 465 | &gvar_list, sdi) != SR_OK) { |
c8db7172 BV |
466 | printf("\n"); |
467 | continue; | |
468 | } | |
4e0db44d | 469 | uint64 = g_variant_get_fixed_array(gvar_list, |
1e1316b3 | 470 | &num_elements, sizeof(uint64_t)); |
c8db7172 | 471 | printf(" - supported buffer sizes:\n"); |
1e1316b3 | 472 | for (i = 0; i < num_elements; i++) |
4e0db44d | 473 | printf(" %"PRIu64"\n", uint64[i]); |
1e1316b3 | 474 | g_variant_unref(gvar_list); |
c8db7172 | 475 | |
b5c63de9 | 476 | } else if (srci->key == SR_CONF_TIMEBASE) { |
c8db7172 | 477 | /* Supported time bases */ |
995713f4 | 478 | printf(" %s", srci->id); |
2bedb9bf | 479 | if (sr_config_list(sdi->driver, SR_CONF_TIMEBASE, |
1e1316b3 | 480 | &gvar_list, sdi) != SR_OK) { |
c8db7172 BV |
481 | printf("\n"); |
482 | continue; | |
483 | } | |
484 | printf(" - supported time bases:\n"); | |
e07340ad BV |
485 | num_elements = g_variant_n_children(gvar_list); |
486 | for (i = 0; i < num_elements; i++) { | |
487 | gvar = g_variant_get_child_value(gvar_list, i); | |
488 | g_variant_get(gvar, "(tt)", &p, &q); | |
489 | s = sr_period_string(p * q); | |
490 | printf(" %s\n", s); | |
491 | g_free(s); | |
492 | } | |
1e1316b3 | 493 | g_variant_unref(gvar_list); |
c8db7172 | 494 | |
b5c63de9 | 495 | } else if (srci->key == SR_CONF_VDIV) { |
8f3b8464 | 496 | /* Supported volts/div values */ |
995713f4 | 497 | printf(" %s", srci->id); |
9b0b39f6 | 498 | if (sr_config_list(sdi->driver, SR_CONF_VDIV, |
1e1316b3 | 499 | &gvar_list, sdi) != SR_OK) { |
8f3b8464 BV |
500 | printf("\n"); |
501 | continue; | |
502 | } | |
503 | printf(" - supported volts/div:\n"); | |
e07340ad BV |
504 | num_elements = g_variant_n_children(gvar_list); |
505 | for (i = 0; i < num_elements; i++) { | |
506 | gvar = g_variant_get_child_value(gvar_list, i); | |
507 | g_variant_get(gvar, "(tt)", &p, &q); | |
508 | s = sr_voltage_string(p, q); | |
509 | printf(" %s\n", s); | |
510 | g_free(s); | |
511 | } | |
1e1316b3 | 512 | g_variant_unref(gvar_list); |
8f3b8464 | 513 | |
43d3d80b BV |
514 | } else if (srci->datatype == SR_T_CHAR) { |
515 | printf(" %s: ", srci->id); | |
516 | if (sr_config_get(sdi->driver, srci->key, | |
517 | &gvar, sdi) == SR_OK) { | |
518 | tmp_str = g_strdup(g_variant_get_string(gvar, NULL)); | |
519 | g_variant_unref(gvar); | |
520 | } else | |
521 | tmp_str = NULL; | |
522 | ||
523 | if (sr_config_list(sdi->driver, srci->key, | |
1e1316b3 | 524 | &gvar, sdi) != SR_OK) { |
498f9167 BV |
525 | printf("\n"); |
526 | continue; | |
527 | } | |
43d3d80b | 528 | |
1e1316b3 | 529 | stropts = g_variant_get_strv(gvar, &num_elements); |
43d3d80b BV |
530 | for (i = 0; i < num_elements; i++) { |
531 | if (i) | |
532 | printf(", "); | |
533 | printf("%s", stropts[i]); | |
534 | if (tmp_str && !strcmp(tmp_str, stropts[i])) | |
535 | printf(" (current)"); | |
536 | } | |
537 | printf("\n"); | |
538 | g_free(stropts); | |
539 | g_free(tmp_str); | |
1e1316b3 | 540 | g_variant_unref(gvar); |
498f9167 | 541 | |
43d3d80b BV |
542 | } else if (srci->datatype == SR_T_UINT64_RANGE) { |
543 | printf(" %s: ", srci->id); | |
544 | if (sr_config_list(sdi->driver, srci->key, | |
545 | &gvar_list, sdi) != SR_OK) { | |
546 | printf("\n"); | |
547 | continue; | |
548 | } | |
549 | ||
550 | if (sr_config_get(sdi->driver, srci->key, &gvar, sdi) == SR_OK) { | |
551 | g_variant_get(gvar, "(tt)", &cur_low, &cur_high); | |
1e1316b3 | 552 | g_variant_unref(gvar); |
43d3d80b BV |
553 | } else { |
554 | cur_low = 0; | |
555 | cur_high = 0; | |
8e8827f4 | 556 | } |
43d3d80b BV |
557 | |
558 | num_elements = g_variant_n_children(gvar_list); | |
559 | for (i = 0; i < num_elements; i++) { | |
560 | gvar = g_variant_get_child_value(gvar_list, i); | |
561 | g_variant_get(gvar, "(tt)", &low, &high); | |
562 | g_variant_unref(gvar); | |
563 | if (i) | |
564 | printf(", "); | |
565 | printf("%"PRIu64"-%"PRIu64, low, high); | |
566 | if (low == cur_low && high == cur_high) | |
567 | printf(" (current)"); | |
568 | } | |
569 | printf("\n"); | |
570 | g_variant_unref(gvar_list); | |
571 | ||
572 | } else if (srci->datatype == SR_T_BOOL) { | |
573 | printf(" %s: ", srci->id); | |
574 | if (sr_config_get(sdi->driver, srci->key, | |
575 | &gvar, sdi) == SR_OK) { | |
576 | if (g_variant_get_boolean(gvar)) | |
577 | printf("on (current), off\n"); | |
578 | else | |
579 | printf("on, off (current)\n"); | |
580 | g_variant_unref(gvar); | |
581 | } else | |
582 | printf("on, off\n"); | |
583 | ||
43e5747a | 584 | } else { |
43d3d80b | 585 | |
c8db7172 | 586 | /* Everything else */ |
995713f4 | 587 | printf(" %s\n", srci->id); |
43e5747a UH |
588 | } |
589 | } | |
1616f663 | 590 | g_variant_unref(gvar_opts); |
22981b2c | 591 | |
0687f23d | 592 | sr_dev_close(sdi); |
17982bc9 | 593 | g_slist_free(devices); |
8e8827f4 | 594 | |
43e5747a UH |
595 | } |
596 | ||
de0a066e | 597 | #ifdef HAVE_SRD |
71b1ea4e BV |
598 | static void show_pd_detail(void) |
599 | { | |
600 | GSList *l; | |
601 | struct srd_decoder *dec; | |
d142a3f7 BV |
602 | struct srd_decoder_option *o; |
603 | char **pdtokens, **pdtok, *optsep, **ann, *val, *doc; | |
e9ff6974 | 604 | struct srd_probe *p; |
71b1ea4e BV |
605 | |
606 | pdtokens = g_strsplit(opt_pds, ",", -1); | |
607 | for (pdtok = pdtokens; *pdtok; pdtok++) { | |
d142a3f7 BV |
608 | /* Strip options. */ |
609 | if ((optsep = strchr(*pdtok, ':'))) | |
610 | *optsep = '\0'; | |
2358775c | 611 | if (!(dec = srd_decoder_get_by_id(*pdtok))) { |
f8ccd825 | 612 | g_critical("Protocol decoder %s not found.", *pdtok); |
71b1ea4e BV |
613 | return; |
614 | } | |
615 | printf("ID: %s\nName: %s\nLong name: %s\nDescription: %s\n", | |
616 | dec->id, dec->name, dec->longname, dec->desc); | |
617 | printf("License: %s\n", dec->license); | |
e9ff6974 | 618 | printf("Annotations:\n"); |
71b1ea4e | 619 | if (dec->annotations) { |
71b1ea4e BV |
620 | for (l = dec->annotations; l; l = l->next) { |
621 | ann = l->data; | |
622 | printf("- %s\n %s\n", ann[0], ann[1]); | |
623 | } | |
e9ff6974 UH |
624 | } else { |
625 | printf("None.\n"); | |
626 | } | |
e9ff6974 UH |
627 | printf("Required probes:\n"); |
628 | if (dec->probes) { | |
629 | for (l = dec->probes; l; l = l->next) { | |
630 | p = l->data; | |
631 | printf("- %s (%s): %s\n", | |
632 | p->name, p->id, p->desc); | |
633 | } | |
634 | } else { | |
635 | printf("None.\n"); | |
636 | } | |
637 | printf("Optional probes:\n"); | |
638 | if (dec->opt_probes) { | |
639 | for (l = dec->opt_probes; l; l = l->next) { | |
640 | p = l->data; | |
641 | printf("- %s (%s): %s\n", | |
642 | p->name, p->id, p->desc); | |
643 | } | |
644 | } else { | |
645 | printf("None.\n"); | |
71b1ea4e | 646 | } |
d142a3f7 BV |
647 | if (dec->options) { |
648 | printf("Options:\n"); | |
649 | for (l = dec->options; l; l = l->next) { | |
650 | o = l->data; | |
651 | val = g_variant_print(o->def, FALSE); | |
652 | printf("- %s: %s (default %s)\n", o->id, o->desc, val); | |
653 | g_free(val); | |
654 | } | |
655 | } | |
4359a4da | 656 | if ((doc = srd_decoder_doc_get(dec))) { |
e9ff6974 UH |
657 | printf("Documentation:\n%s\n", |
658 | doc[0] == '\n' ? doc + 1 : doc); | |
71b1ea4e BV |
659 | g_free(doc); |
660 | } | |
661 | } | |
662 | ||
663 | g_strfreev(pdtokens); | |
71b1ea4e | 664 | } |
de0a066e | 665 | #endif |
71b1ea4e | 666 | |
c27450ea BV |
667 | static GArray *get_enabled_logic_probes(const struct sr_dev_inst *sdi) |
668 | { | |
669 | struct sr_probe *probe; | |
670 | GArray *probes; | |
671 | GSList *l; | |
672 | ||
673 | probes = g_array_new(FALSE, FALSE, sizeof(int)); | |
674 | for (l = sdi->probes; l; l = l->next) { | |
675 | probe = l->data; | |
676 | if (probe->type != SR_PROBE_LOGIC) | |
677 | continue; | |
678 | if (probe->enabled != TRUE) | |
679 | continue; | |
680 | g_array_append_val(probes, probe->index); | |
681 | } | |
682 | ||
683 | return probes; | |
684 | } | |
685 | ||
37d5ccc1 | 686 | static void datafeed_in(const struct sr_dev_inst *sdi, |
74f6195b | 687 | const struct sr_datafeed_packet *packet, void *cb_data) |
43e5747a | 688 | { |
7e97afa0 | 689 | const struct sr_datafeed_meta *meta; |
c27450ea BV |
690 | const struct sr_datafeed_logic *logic; |
691 | const struct sr_datafeed_analog *analog; | |
7e97afa0 | 692 | struct sr_config *src; |
43e5747a | 693 | static struct sr_output *o = NULL; |
c27450ea | 694 | static GArray *logic_probelist = NULL; |
43e5747a UH |
695 | static uint64_t received_samples = 0; |
696 | static int unitsize = 0; | |
697 | static int triggered = 0; | |
698 | static FILE *outfile = NULL; | |
1616f663 BV |
699 | GSList *l; |
700 | GString *out; | |
c27450ea | 701 | int sample_size, ret; |
1616f663 | 702 | uint64_t samplerate, output_len, filter_out_len; |
8de01efe | 703 | uint8_t *output_buf, *filter_out; |
43e5747a | 704 | |
74f6195b ML |
705 | (void) cb_data; |
706 | ||
43e5747a UH |
707 | /* If the first packet to come in isn't a header, don't even try. */ |
708 | if (packet->type != SR_DF_HEADER && o == NULL) | |
709 | return; | |
710 | ||
711 | sample_size = -1; | |
712 | switch (packet->type) { | |
713 | case SR_DF_HEADER: | |
8170b8ea | 714 | g_debug("cli: Received SR_DF_HEADER"); |
43e5747a | 715 | /* Initialize the output module. */ |
c2c4a0de | 716 | if (!(o = g_try_malloc(sizeof(struct sr_output)))) { |
8170b8ea | 717 | g_critical("Output module malloc failed."); |
43e5747a UH |
718 | exit(1); |
719 | } | |
720 | o->format = output_format; | |
37d5ccc1 | 721 | o->sdi = (struct sr_dev_inst *)sdi; |
43e5747a UH |
722 | o->param = output_format_param; |
723 | if (o->format->init) { | |
724 | if (o->format->init(o) != SR_OK) { | |
8170b8ea | 725 | g_critical("Output format initialization failed."); |
43e5747a UH |
726 | exit(1); |
727 | } | |
728 | } | |
c27450ea BV |
729 | |
730 | /* Prepare non-stdout output. */ | |
731 | outfile = stdout; | |
732 | if (opt_output_file) { | |
733 | if (default_output_format) { | |
734 | /* output file is in session format, so we'll | |
735 | * keep a copy of everything as it comes in | |
736 | * and save from there after the session. */ | |
737 | outfile = NULL; | |
738 | savebuf = g_byte_array_new(); | |
739 | } else { | |
740 | /* saving to a file in whatever format was set | |
741 | * with --format, so all we need is a filehandle */ | |
742 | outfile = g_fopen(opt_output_file, "wb"); | |
743 | } | |
744 | } | |
745 | ||
746 | /* Prepare for logic data. */ | |
747 | logic_probelist = get_enabled_logic_probes(sdi); | |
748 | /* How many bytes we need to store the packed samples. */ | |
749 | unitsize = (logic_probelist->len + 7) / 8; | |
750 | ||
de0a066e | 751 | #ifdef HAVE_SRD |
7d559a46 | 752 | GVariant *gvar; |
c27450ea | 753 | if (opt_pds && logic_probelist->len) { |
9e0e651f | 754 | if (sr_config_get(sdi->driver, SR_CONF_SAMPLERATE, |
1616f663 | 755 | &gvar, sdi) != SR_OK) { |
c27450ea BV |
756 | g_critical("Unable to initialize protocol " |
757 | "decoders: no samplerate found."); | |
758 | break; | |
759 | } | |
1616f663 BV |
760 | samplerate = g_variant_get_uint64(gvar); |
761 | g_variant_unref(gvar); | |
762 | srd_session_start(logic_probelist->len, unitsize, samplerate); | |
c27450ea | 763 | } |
de0a066e | 764 | #endif |
43e5747a | 765 | break; |
53993299 | 766 | |
7e97afa0 BV |
767 | case SR_DF_META: |
768 | g_debug("cli: received SR_DF_META"); | |
769 | meta = packet->payload; | |
770 | for (l = meta->config; l; l = l->next) { | |
771 | src = l->data; | |
772 | switch (src->key) { | |
b5c63de9 | 773 | case SR_CONF_SAMPLERATE: |
1616f663 BV |
774 | samplerate = g_variant_get_uint64(src->data); |
775 | g_debug("cli: got samplerate %"PRIu64" Hz", samplerate); | |
5570e0f2 BV |
776 | break; |
777 | case SR_CONF_SAMPLE_INTERVAL: | |
1616f663 BV |
778 | samplerate = g_variant_get_uint64(src->data); |
779 | g_debug("cli: got sample interval %"PRIu64" ms", samplerate); | |
7e97afa0 BV |
780 | break; |
781 | default: | |
782 | /* Unknown metadata is not an error. */ | |
783 | break; | |
784 | } | |
785 | } | |
786 | break; | |
787 | ||
43e5747a | 788 | case SR_DF_TRIGGER: |
8170b8ea | 789 | g_debug("cli: received SR_DF_TRIGGER"); |
43e5747a UH |
790 | if (o->format->event) |
791 | o->format->event(o, SR_DF_TRIGGER, &output_buf, | |
792 | &output_len); | |
793 | triggered = 1; | |
794 | break; | |
53993299 | 795 | |
43e5747a UH |
796 | case SR_DF_LOGIC: |
797 | logic = packet->payload; | |
b46c4414 | 798 | g_message("cli: received SR_DF_LOGIC, %"PRIu64" bytes", logic->length); |
53993299 BV |
799 | sample_size = logic->unitsize; |
800 | if (logic->length == 0) | |
801 | break; | |
802 | ||
803 | /* Don't store any samples until triggered. */ | |
804 | if (opt_wait_trigger && !triggered) | |
805 | break; | |
43e5747a | 806 | |
53993299 BV |
807 | if (limit_samples && received_samples >= limit_samples) |
808 | break; | |
43e5747a | 809 | |
53993299 | 810 | ret = sr_filter_probes(sample_size, unitsize, logic_probelist, |
f8ccd825 BV |
811 | logic->data, logic->length, |
812 | &filter_out, &filter_out_len); | |
53993299 BV |
813 | if (ret != SR_OK) |
814 | break; | |
43e5747a | 815 | |
c27450ea | 816 | /* What comes out of the filter is guaranteed to be packed into the |
53993299 BV |
817 | * minimum size needed to support the number of samples at this sample |
818 | * size. however, the driver may have submitted too much -- cut off | |
819 | * the buffer of the last packet according to the sample limit. | |
820 | */ | |
821 | if (limit_samples && (received_samples + logic->length / sample_size > | |
822 | limit_samples * sample_size)) | |
823 | filter_out_len = limit_samples * sample_size - received_samples; | |
43e5747a | 824 | |
c27450ea BV |
825 | if (opt_output_file && default_output_format) { |
826 | /* Saving to a session file. */ | |
827 | g_byte_array_append(savebuf, filter_out, filter_out_len); | |
53993299 | 828 | } else { |
c27450ea | 829 | if (opt_pds) { |
de0a066e | 830 | #ifdef HAVE_SRD |
c27450ea BV |
831 | if (srd_session_send(received_samples, (uint8_t*)filter_out, |
832 | filter_out_len) != SRD_OK) | |
833 | sr_session_stop(); | |
de0a066e | 834 | #endif |
c27450ea BV |
835 | } else { |
836 | output_len = 0; | |
837 | if (o->format->data && packet->type == o->format->df_type) | |
838 | o->format->data(o, filter_out, filter_out_len, | |
839 | &output_buf, &output_len); | |
a172e2a0 | 840 | if (output_len) { |
c27450ea BV |
841 | fwrite(output_buf, 1, output_len, outfile); |
842 | fflush(outfile); | |
843 | g_free(output_buf); | |
844 | } | |
53993299 BV |
845 | } |
846 | } | |
53993299 | 847 | g_free(filter_out); |
43e5747a | 848 | |
c27450ea | 849 | received_samples += logic->length / sample_size; |
53993299 BV |
850 | break; |
851 | ||
852 | case SR_DF_ANALOG: | |
853 | analog = packet->payload; | |
854 | g_message("cli: received SR_DF_ANALOG, %d samples", analog->num_samples); | |
855 | if (analog->num_samples == 0) | |
856 | break; | |
857 | ||
858 | if (limit_samples && received_samples >= limit_samples) | |
859 | break; | |
860 | ||
48f71481 BV |
861 | if (o->format->data && packet->type == o->format->df_type) { |
862 | o->format->data(o, (const uint8_t *)analog->data, | |
863 | analog->num_samples * sizeof(float), | |
864 | &output_buf, &output_len); | |
865 | if (output_buf) { | |
866 | fwrite(output_buf, 1, output_len, outfile); | |
e09810e9 | 867 | fflush(outfile); |
48f71481 | 868 | g_free(output_buf); |
c8db7172 | 869 | } |
53993299 BV |
870 | } |
871 | ||
53993299 | 872 | received_samples += analog->num_samples; |
ce48d892 BV |
873 | break; |
874 | ||
875 | case SR_DF_FRAME_BEGIN: | |
48f71481 BV |
876 | g_debug("cli: received SR_DF_FRAME_BEGIN"); |
877 | if (o->format->event) { | |
878 | o->format->event(o, SR_DF_FRAME_BEGIN, &output_buf, | |
879 | &output_len); | |
880 | if (output_buf) { | |
881 | fwrite(output_buf, 1, output_len, outfile); | |
e09810e9 | 882 | fflush(outfile); |
48f71481 BV |
883 | g_free(output_buf); |
884 | } | |
885 | } | |
ce48d892 BV |
886 | break; |
887 | ||
888 | case SR_DF_FRAME_END: | |
48f71481 BV |
889 | g_debug("cli: received SR_DF_FRAME_END"); |
890 | if (o->format->event) { | |
891 | o->format->event(o, SR_DF_FRAME_END, &output_buf, | |
892 | &output_len); | |
893 | if (output_buf) { | |
894 | fwrite(output_buf, 1, output_len, outfile); | |
e09810e9 | 895 | fflush(outfile); |
48f71481 BV |
896 | g_free(output_buf); |
897 | } | |
898 | } | |
ce48d892 | 899 | break; |
53993299 BV |
900 | |
901 | default: | |
c27450ea | 902 | break; |
43e5747a UH |
903 | } |
904 | ||
644bb1e8 BV |
905 | if (o && o->format->receive) { |
906 | if (o->format->receive(o, sdi, packet, &out) == SR_OK && out) { | |
05e7fa2c BV |
907 | fwrite(out->str, 1, out->len, outfile); |
908 | fflush(outfile); | |
644bb1e8 | 909 | g_string_free(out, TRUE); |
05e7fa2c BV |
910 | } |
911 | } | |
912 | ||
644bb1e8 | 913 | /* SR_DF_END needs to be handled after the output module's receive() |
bea29a44 BV |
914 | * is called, so it can properly clean up that module etc. */ |
915 | if (packet->type == SR_DF_END) { | |
916 | g_debug("cli: Received SR_DF_END"); | |
917 | ||
918 | if (o->format->event) { | |
919 | o->format->event(o, SR_DF_END, &output_buf, &output_len); | |
920 | if (output_buf) { | |
921 | if (outfile) | |
922 | fwrite(output_buf, 1, output_len, outfile); | |
923 | g_free(output_buf); | |
924 | output_len = 0; | |
925 | } | |
926 | } | |
927 | ||
928 | if (limit_samples && received_samples < limit_samples) | |
929 | g_warning("Device only sent %" PRIu64 " samples.", | |
930 | received_samples); | |
931 | ||
932 | if (opt_continuous) | |
933 | g_warning("Device stopped after %" PRIu64 " samples.", | |
934 | received_samples); | |
935 | ||
936 | g_array_free(logic_probelist, TRUE); | |
937 | ||
938 | if (o->format->cleanup) | |
939 | o->format->cleanup(o); | |
940 | g_free(o); | |
941 | o = NULL; | |
942 | ||
943 | if (outfile && outfile != stdout) | |
944 | fclose(outfile); | |
945 | ||
946 | if (opt_output_file && default_output_format) { | |
947 | if (sr_session_save(opt_output_file, sdi, savebuf->data, | |
948 | unitsize, savebuf->len / unitsize) != SR_OK) | |
949 | g_critical("Failed to save session."); | |
950 | g_byte_array_free(savebuf, FALSE); | |
951 | } | |
952 | } | |
953 | ||
43e5747a UH |
954 | } |
955 | ||
de0a066e | 956 | #ifdef HAVE_SRD |
d142a3f7 BV |
957 | static int opts_to_gvar(struct srd_decoder *dec, GHashTable *hash, |
958 | GHashTable **options) | |
959 | { | |
960 | struct srd_decoder_option *o; | |
961 | GSList *optl; | |
962 | GVariant *gvar; | |
963 | gint64 val_int; | |
964 | int ret; | |
965 | char *val_str, *conv; | |
966 | ||
967 | ret = TRUE; | |
968 | *options = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, | |
969 | (GDestroyNotify)g_variant_unref); | |
970 | ||
971 | for (optl = dec->options; optl; optl = optl->next) { | |
972 | o = optl->data; | |
973 | if (!(val_str = g_hash_table_lookup(hash, o->id))) | |
974 | /* Not specified. */ | |
975 | continue; | |
976 | if (g_variant_is_of_type(o->def, G_VARIANT_TYPE_STRING)) { | |
977 | gvar = g_variant_new_string(val_str); | |
978 | } else if (g_variant_is_of_type(o->def, G_VARIANT_TYPE_INT64)) { | |
979 | val_int = strtoll(val_str, &conv, 10); | |
980 | if (!conv || conv == val_str) { | |
981 | g_critical("Protocol decoder '%s' option '%s' " | |
982 | "requires a number.", dec->name, o->id); | |
983 | ret = FALSE; | |
984 | break; | |
985 | } | |
986 | gvar = g_variant_new_int64(val_int); | |
987 | } else { | |
988 | g_critical("Unsupported type for option '%s' (%s)", | |
989 | o->id, g_variant_get_type_string(o->def)); | |
990 | ret = FALSE; | |
991 | break; | |
992 | } | |
993 | g_variant_ref_sink(gvar); | |
994 | g_hash_table_insert(*options, g_strdup(o->id), gvar); | |
995 | g_hash_table_remove(hash, o->id); | |
996 | } | |
997 | ||
998 | return ret; | |
999 | } | |
1000 | ||
1001 | static int probes_to_gvar(struct srd_decoder *dec, GHashTable *hash, | |
1002 | GHashTable **probes) | |
1003 | { | |
1004 | struct srd_probe *p; | |
1005 | GSList *all_probes, *l; | |
1006 | GVariant *gvar; | |
1007 | gint32 val_int; | |
1008 | int ret; | |
1009 | char *val_str, *conv; | |
1010 | ||
1011 | ret = TRUE; | |
1012 | *probes = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, | |
1013 | (GDestroyNotify)g_variant_unref); | |
1014 | ||
1015 | all_probes = g_slist_copy(dec->probes); | |
1016 | all_probes = g_slist_concat(all_probes, dec->opt_probes); | |
1017 | for (l = all_probes; l; l = l->next) { | |
1018 | p = l->data; | |
1019 | if (!(val_str = g_hash_table_lookup(hash, p->id))) | |
1020 | /* Not specified. */ | |
1021 | continue; | |
1022 | val_int = strtoll(val_str, &conv, 10); | |
1023 | if (!conv || conv == val_str) { | |
1024 | g_critical("Protocol decoder '%s' probes '%s' " | |
1025 | "is not a number.", dec->name, p->id); | |
1026 | ret = FALSE; | |
1027 | break; | |
1028 | } | |
1029 | gvar = g_variant_new_int32(val_int); | |
1030 | g_variant_ref_sink(gvar); | |
1031 | g_hash_table_insert(*probes, g_strdup(p->id), gvar); | |
1032 | g_hash_table_remove(hash, p->id); | |
1033 | } | |
1034 | g_slist_free(all_probes); | |
1035 | ||
1036 | return ret; | |
1037 | } | |
1038 | ||
9f4a898e BV |
1039 | /* Register the given PDs for this session. |
1040 | * Accepts a string of the form: "spi:sck=3:sdata=4,spi:sck=3:sdata=5" | |
43e5747a UH |
1041 | * That will instantiate two SPI decoders on the clock but different data |
1042 | * lines. | |
1043 | */ | |
1e0f9ed9 | 1044 | static int register_pds(struct sr_dev *dev, const char *pdstring) |
43e5747a | 1045 | { |
d142a3f7 BV |
1046 | struct srd_decoder *dec; |
1047 | GHashTable *pd_opthash, *options, *probes; | |
1048 | GList *leftover, *l; | |
878e90d9 | 1049 | struct srd_decoder_inst *di; |
445950d3 | 1050 | int ret; |
a1418b73 | 1051 | char **pdtokens, **pdtok, *pd_name; |
43e5747a | 1052 | |
1e0f9ed9 | 1053 | (void)dev; |
43e5747a | 1054 | |
445950d3 | 1055 | ret = 0; |
120f9ee7 BV |
1056 | pd_ann_visible = g_hash_table_new_full(g_str_hash, g_int_equal, |
1057 | g_free, NULL); | |
a1418b73 | 1058 | pd_name = NULL; |
d142a3f7 | 1059 | pd_opthash = options = probes = NULL; |
120f9ee7 | 1060 | pdtokens = g_strsplit(pdstring, ",", 0); |
43e5747a | 1061 | for (pdtok = pdtokens; *pdtok; pdtok++) { |
63bb454c | 1062 | if (!(pd_opthash = parse_generic_arg(*pdtok, TRUE))) { |
8170b8ea | 1063 | g_critical("Invalid protocol decoder option '%s'.", *pdtok); |
d142a3f7 | 1064 | break; |
43e5747a | 1065 | } |
a1418b73 | 1066 | |
9720f23a BV |
1067 | pd_name = g_strdup(g_hash_table_lookup(pd_opthash, "sigrok_key")); |
1068 | g_hash_table_remove(pd_opthash, "sigrok_key"); | |
2358775c | 1069 | if (srd_decoder_load(pd_name) != SRD_OK) { |
8170b8ea | 1070 | g_critical("Failed to load protocol decoder %s.", pd_name); |
445950d3 | 1071 | ret = 1; |
d142a3f7 BV |
1072 | break; |
1073 | } | |
1074 | dec = srd_decoder_get_by_id(pd_name); | |
1075 | ||
1076 | /* Convert decoder option and probe values to GVariant. */ | |
1077 | if (!opts_to_gvar(dec, pd_opthash, &options)) { | |
1078 | ret = 1; | |
1079 | break; | |
1080 | } | |
1081 | if (!probes_to_gvar(dec, pd_opthash, &probes)) { | |
1082 | ret = 1; | |
1083 | break; | |
15d920a9 | 1084 | } |
d142a3f7 BV |
1085 | if (g_hash_table_size(pd_opthash) > 0) { |
1086 | leftover = g_hash_table_get_keys(pd_opthash); | |
1087 | for (l = leftover; l; l = l->next) | |
1088 | g_critical("Unknown option or probe '%s'", (char *)l->data); | |
1089 | g_list_free(leftover); | |
1090 | break; | |
1091 | } | |
1092 | ||
1093 | if (!(di = srd_inst_new(pd_name, options))) { | |
8170b8ea | 1094 | g_critical("Failed to instantiate protocol decoder %s.", pd_name); |
445950d3 | 1095 | ret = 1; |
d142a3f7 | 1096 | break; |
43e5747a | 1097 | } |
120f9ee7 BV |
1098 | |
1099 | /* If no annotation list was specified, add them all in now. | |
1100 | * This will be pared down later to leave only the last PD | |
1101 | * in the stack. | |
1102 | */ | |
b6bd032d UH |
1103 | if (!opt_pd_annotations) |
1104 | g_hash_table_insert(pd_ann_visible, | |
1105 | g_strdup(di->inst_id), NULL); | |
a1418b73 | 1106 | |
d142a3f7 BV |
1107 | /* Remap the probes if needed. */ |
1108 | if (srd_inst_probe_set_all(di, probes) != SRD_OK) { | |
445950d3 | 1109 | ret = 1; |
d142a3f7 | 1110 | break; |
445950d3 | 1111 | } |
67ce0d27 | 1112 | } |
43e5747a | 1113 | |
43e5747a | 1114 | g_strfreev(pdtokens); |
9720f23a BV |
1115 | if (pd_opthash) |
1116 | g_hash_table_destroy(pd_opthash); | |
d142a3f7 BV |
1117 | if (options) |
1118 | g_hash_table_destroy(options); | |
1119 | if (probes) | |
1120 | g_hash_table_destroy(probes); | |
a1418b73 BV |
1121 | if (pd_name) |
1122 | g_free(pd_name); | |
43e5747a | 1123 | |
445950d3 | 1124 | return ret; |
43e5747a UH |
1125 | } |
1126 | ||
120f9ee7 BV |
1127 | int setup_pd_stack(void) |
1128 | { | |
1129 | struct srd_decoder_inst *di_from, *di_to; | |
1130 | int ret, i; | |
41e915a6 | 1131 | char **pds, **ids; |
120f9ee7 BV |
1132 | |
1133 | /* Set up the protocol decoder stack. */ | |
1134 | pds = g_strsplit(opt_pds, ",", 0); | |
1135 | if (g_strv_length(pds) > 1) { | |
1136 | if (opt_pd_stack) { | |
1137 | /* A stack setup was specified, use that. */ | |
1138 | g_strfreev(pds); | |
1139 | pds = g_strsplit(opt_pd_stack, ",", 0); | |
1140 | if (g_strv_length(pds) < 2) { | |
1141 | g_strfreev(pds); | |
1142 | g_critical("Specify at least two protocol decoders to stack."); | |
1143 | return 1; | |
1144 | } | |
1145 | } | |
1146 | ||
41e915a6 BV |
1147 | /* First PD goes at the bottom of the stack. */ |
1148 | ids = g_strsplit(pds[0], ":", 0); | |
1149 | if (!(di_from = srd_inst_find_by_id(ids[0]))) { | |
1150 | g_strfreev(ids); | |
120f9ee7 BV |
1151 | g_critical("Cannot stack protocol decoder '%s': " |
1152 | "instance not found.", pds[0]); | |
1153 | return 1; | |
1154 | } | |
41e915a6 BV |
1155 | g_strfreev(ids); |
1156 | ||
1157 | /* Every subsequent PD goes on top. */ | |
120f9ee7 | 1158 | for (i = 1; pds[i]; i++) { |
41e915a6 BV |
1159 | ids = g_strsplit(pds[i], ":", 0); |
1160 | if (!(di_to = srd_inst_find_by_id(ids[0]))) { | |
1161 | g_strfreev(ids); | |
120f9ee7 BV |
1162 | g_critical("Cannot stack protocol decoder '%s': " |
1163 | "instance not found.", pds[i]); | |
1164 | return 1; | |
1165 | } | |
41e915a6 | 1166 | g_strfreev(ids); |
120f9ee7 BV |
1167 | if ((ret = srd_inst_stack(di_from, di_to)) != SRD_OK) |
1168 | return 1; | |
1169 | ||
1170 | /* Don't show annotation from this PD. Only the last PD in | |
1171 | * the stack will be left on the annotation list (unless | |
1172 | * the annotation list was specifically provided). | |
1173 | */ | |
478a782d | 1174 | if (!opt_pd_annotations) |
b6bd032d UH |
1175 | g_hash_table_remove(pd_ann_visible, |
1176 | di_from->inst_id); | |
120f9ee7 BV |
1177 | |
1178 | di_from = di_to; | |
1179 | } | |
1180 | } | |
1181 | g_strfreev(pds); | |
1182 | ||
1183 | return 0; | |
1184 | } | |
1185 | ||
1186 | int setup_pd_annotations(void) | |
1187 | { | |
1188 | GSList *l; | |
1189 | struct srd_decoder *dec; | |
1190 | int ann; | |
1191 | char **pds, **pdtok, **keyval, **ann_descr; | |
1192 | ||
1193 | /* Set up custom list of PDs and annotations to show. */ | |
b6bd032d UH |
1194 | if (opt_pd_annotations) { |
1195 | pds = g_strsplit(opt_pd_annotations, ",", 0); | |
120f9ee7 BV |
1196 | for (pdtok = pds; *pdtok && **pdtok; pdtok++) { |
1197 | ann = 0; | |
1198 | keyval = g_strsplit(*pdtok, "=", 0); | |
1199 | if (!(dec = srd_decoder_get_by_id(keyval[0]))) { | |
1200 | g_critical("Protocol decoder '%s' not found.", keyval[0]); | |
1201 | return 1; | |
1202 | } | |
1203 | if (!dec->annotations) { | |
1204 | g_critical("Protocol decoder '%s' has no annotations.", keyval[0]); | |
1205 | return 1; | |
1206 | } | |
1207 | if (g_strv_length(keyval) == 2) { | |
1208 | for (l = dec->annotations; l; l = l->next, ann++) { | |
1209 | ann_descr = l->data; | |
1210 | if (!canon_cmp(ann_descr[0], keyval[1])) | |
1211 | /* Found it. */ | |
1212 | break; | |
1213 | } | |
1214 | if (!l) { | |
1215 | g_critical("Annotation '%s' not found " | |
1216 | "for protocol decoder '%s'.", keyval[1], keyval[0]); | |
1217 | return 1; | |
1218 | } | |
1219 | } | |
1220 | g_debug("cli: showing protocol decoder annotation %d from '%s'", ann, keyval[0]); | |
1221 | g_hash_table_insert(pd_ann_visible, g_strdup(keyval[0]), GINT_TO_POINTER(ann)); | |
1222 | g_strfreev(keyval); | |
1223 | } | |
1224 | g_strfreev(pds); | |
1225 | } | |
1226 | ||
1227 | return 0; | |
1228 | } | |
1229 | ||
de0a066e BV |
1230 | void show_pd_annotations(struct srd_proto_data *pdata, void *cb_data) |
1231 | { | |
1232 | int i; | |
1233 | char **annotations; | |
1234 | gpointer ann_format; | |
1235 | ||
1236 | /* 'cb_data' is not used in this specific callback. */ | |
1237 | (void)cb_data; | |
1238 | ||
1239 | if (!pd_ann_visible) | |
1240 | return; | |
1241 | ||
1242 | if (!g_hash_table_lookup_extended(pd_ann_visible, pdata->pdo->di->inst_id, | |
1243 | NULL, &ann_format)) | |
1244 | /* Not in the list of PDs whose annotations we're showing. */ | |
1245 | return; | |
1246 | ||
1247 | if (pdata->ann_format != GPOINTER_TO_INT(ann_format)) | |
1248 | /* We don't want this particular format from the PD. */ | |
1249 | return; | |
1250 | ||
1251 | annotations = pdata->data; | |
1252 | if (opt_loglevel > SR_LOG_WARN) | |
1253 | printf("%"PRIu64"-%"PRIu64" ", pdata->start_sample, pdata->end_sample); | |
1254 | printf("%s: ", pdata->pdo->proto_id); | |
1255 | for (i = 0; annotations[i]; i++) | |
1256 | printf("\"%s\" ", annotations[i]); | |
1257 | printf("\n"); | |
1258 | fflush(stdout); | |
1259 | } | |
1260 | #endif | |
1261 | ||
ad2bc491 BV |
1262 | int setup_output_format(void) |
1263 | { | |
1264 | GHashTable *fmtargs; | |
1265 | GHashTableIter iter; | |
1266 | gpointer key, value; | |
1267 | struct sr_output_format **outputs; | |
1268 | int i; | |
1269 | char *fmtspec; | |
1270 | ||
1271 | if (!opt_output_format) { | |
1272 | opt_output_format = DEFAULT_OUTPUT_FORMAT; | |
1273 | /* we'll need to remember this so when saving to a file | |
1274 | * later, sigrok session format will be used. | |
1275 | */ | |
1276 | default_output_format = TRUE; | |
1277 | } | |
48f71481 | 1278 | |
63bb454c | 1279 | fmtargs = parse_generic_arg(opt_output_format, TRUE); |
ad2bc491 BV |
1280 | fmtspec = g_hash_table_lookup(fmtargs, "sigrok_key"); |
1281 | if (!fmtspec) { | |
1282 | g_critical("Invalid output format."); | |
1283 | return 1; | |
1284 | } | |
1285 | outputs = sr_output_list(); | |
1286 | for (i = 0; outputs[i]; i++) { | |
1287 | if (strcmp(outputs[i]->id, fmtspec)) | |
1288 | continue; | |
1289 | g_hash_table_remove(fmtargs, "sigrok_key"); | |
1290 | output_format = outputs[i]; | |
1291 | g_hash_table_iter_init(&iter, fmtargs); | |
1292 | while (g_hash_table_iter_next(&iter, &key, &value)) { | |
1293 | /* only supporting one parameter per output module | |
1294 | * for now, and only its value */ | |
1295 | output_format_param = g_strdup(value); | |
1296 | break; | |
1297 | } | |
1298 | break; | |
1299 | } | |
1300 | if (!output_format) { | |
1301 | g_critical("Invalid output format %s.", opt_output_format); | |
1302 | return 1; | |
1303 | } | |
1304 | g_hash_table_destroy(fmtargs); | |
1305 | ||
1306 | return 0; | |
1307 | } | |
1308 | ||
37d5ccc1 | 1309 | static int select_probes(struct sr_dev_inst *sdi) |
43e5747a | 1310 | { |
497f5362 BV |
1311 | struct sr_probe *probe; |
1312 | GSList *selected_probes, *l; | |
43e5747a UH |
1313 | |
1314 | if (!opt_probes) | |
1315 | return SR_OK; | |
1316 | ||
497f5362 | 1317 | if (!(selected_probes = parse_probestring(sdi, opt_probes))) |
43e5747a | 1318 | return SR_ERR; |
43e5747a | 1319 | |
497f5362 BV |
1320 | for (l = sdi->probes; l; l = l->next) { |
1321 | probe = l->data; | |
1322 | if (g_slist_find(selected_probes, probe)) | |
1323 | probe->enabled = TRUE; | |
1324 | else | |
1325 | probe->enabled = FALSE; | |
43e5747a | 1326 | } |
497f5362 | 1327 | g_slist_free(selected_probes); |
43e5747a UH |
1328 | |
1329 | return SR_OK; | |
1330 | } | |
1331 | ||
1332 | /** | |
1333 | * Return the input file format which the CLI tool should use. | |
1334 | * | |
1335 | * If the user specified -I / --input-format, use that one. Otherwise, try to | |
1336 | * autodetect the format as good as possible. Failing that, return NULL. | |
1337 | * | |
1338 | * @param filename The filename of the input file. Must not be NULL. | |
1339 | * @param opt The -I / --input-file option the user specified (or NULL). | |
1340 | * | |
1341 | * @return A pointer to the 'struct sr_input_format' that should be used, | |
1342 | * or NULL if no input format was selected or auto-detected. | |
1343 | */ | |
1344 | static struct sr_input_format *determine_input_file_format( | |
1345 | const char *filename, const char *opt) | |
1346 | { | |
1347 | int i; | |
1348 | struct sr_input_format **inputs; | |
1349 | ||
1350 | /* If there are no input formats, return NULL right away. */ | |
1351 | inputs = sr_input_list(); | |
1352 | if (!inputs) { | |
8170b8ea | 1353 | g_critical("No supported input formats available."); |
43e5747a UH |
1354 | return NULL; |
1355 | } | |
1356 | ||
1357 | /* If the user specified -I / --input-format, use that one. */ | |
1358 | if (opt) { | |
1359 | for (i = 0; inputs[i]; i++) { | |
943d0c08 | 1360 | if (strcasecmp(inputs[i]->id, opt)) |
43e5747a | 1361 | continue; |
8170b8ea BV |
1362 | g_debug("Using user-specified input file format '%s'.", |
1363 | inputs[i]->id); | |
43e5747a UH |
1364 | return inputs[i]; |
1365 | } | |
1366 | ||
1367 | /* The user specified an unknown input format, return NULL. */ | |
8170b8ea | 1368 | g_critical("Error: specified input file format '%s' is " |
943d0c08 | 1369 | "unknown.", opt); |
43e5747a UH |
1370 | return NULL; |
1371 | } | |
1372 | ||
1373 | /* Otherwise, try to find an input module that can handle this file. */ | |
1374 | for (i = 0; inputs[i]; i++) { | |
1375 | if (inputs[i]->format_match(filename)) | |
1376 | break; | |
1377 | } | |
1378 | ||
1379 | /* Return NULL if no input module wanted to touch this. */ | |
1380 | if (!inputs[i]) { | |
8170b8ea | 1381 | g_critical("Error: no matching input module found."); |
43e5747a UH |
1382 | return NULL; |
1383 | } | |
78912cc1 UH |
1384 | |
1385 | g_debug("cli: Autodetected '%s' input format for file '%s'.", | |
1386 | inputs[i]->id, filename); | |
43e5747a | 1387 | |
43e5747a UH |
1388 | return inputs[i]; |
1389 | } | |
1390 | ||
1391 | static void load_input_file_format(void) | |
1392 | { | |
943d0c08 | 1393 | GHashTable *fmtargs = NULL; |
43e5747a UH |
1394 | struct stat st; |
1395 | struct sr_input *in; | |
1396 | struct sr_input_format *input_format; | |
943d0c08 TÅ |
1397 | char *fmtspec = NULL; |
1398 | ||
1399 | if (opt_input_format) { | |
3e8e0c2d | 1400 | fmtargs = parse_generic_arg(opt_input_format, TRUE); |
943d0c08 TÅ |
1401 | fmtspec = g_hash_table_lookup(fmtargs, "sigrok_key"); |
1402 | } | |
43e5747a | 1403 | |
8170b8ea | 1404 | if (!(input_format = determine_input_file_format(opt_input_file, |
943d0c08 | 1405 | fmtspec))) { |
8170b8ea | 1406 | /* The exact cause was already logged. */ |
43e5747a | 1407 | return; |
943d0c08 | 1408 | } |
3e8e0c2d | 1409 | |
943d0c08 TÅ |
1410 | if (fmtargs) |
1411 | g_hash_table_remove(fmtargs, "sigrok_key"); | |
43e5747a UH |
1412 | |
1413 | if (stat(opt_input_file, &st) == -1) { | |
8170b8ea | 1414 | g_critical("Failed to load %s: %s", opt_input_file, |
43e5747a UH |
1415 | strerror(errno)); |
1416 | exit(1); | |
1417 | } | |
1418 | ||
1419 | /* Initialize the input module. */ | |
c2c4a0de | 1420 | if (!(in = g_try_malloc(sizeof(struct sr_input)))) { |
8170b8ea | 1421 | g_critical("Failed to allocate input module."); |
43e5747a UH |
1422 | exit(1); |
1423 | } | |
1424 | in->format = input_format; | |
943d0c08 | 1425 | in->param = fmtargs; |
43e5747a | 1426 | if (in->format->init) { |
41602d35 | 1427 | if (in->format->init(in, opt_input_file) != SR_OK) { |
8170b8ea | 1428 | g_critical("Input format init failed."); |
43e5747a UH |
1429 | exit(1); |
1430 | } | |
1431 | } | |
1432 | ||
37d5ccc1 | 1433 | if (select_probes(in->sdi) > 0) |
c27450ea | 1434 | return; |
43e5747a UH |
1435 | |
1436 | sr_session_new(); | |
74f6195b | 1437 | sr_session_datafeed_callback_add(datafeed_in, NULL); |
37d5ccc1 | 1438 | if (sr_session_dev_add(in->sdi) != SR_OK) { |
8170b8ea | 1439 | g_critical("Failed to use device."); |
43e5747a UH |
1440 | sr_session_destroy(); |
1441 | return; | |
1442 | } | |
1443 | ||
1444 | input_format->loadfile(in, opt_input_file); | |
c27450ea | 1445 | |
43e5747a | 1446 | sr_session_destroy(); |
943d0c08 TÅ |
1447 | |
1448 | if (fmtargs) | |
1449 | g_hash_table_destroy(fmtargs); | |
43e5747a UH |
1450 | } |
1451 | ||
1452 | static void load_input_file(void) | |
1453 | { | |
1454 | ||
1455 | if (sr_session_load(opt_input_file) == SR_OK) { | |
1456 | /* sigrok session file */ | |
74f6195b | 1457 | sr_session_datafeed_callback_add(datafeed_in, NULL); |
43e5747a UH |
1458 | sr_session_start(); |
1459 | sr_session_run(); | |
1460 | sr_session_stop(); | |
1461 | } | |
1462 | else { | |
1463 | /* fall back on input modules */ | |
1464 | load_input_file_format(); | |
1465 | } | |
43e5747a UH |
1466 | } |
1467 | ||
37d5ccc1 | 1468 | static int set_dev_options(struct sr_dev_inst *sdi, GHashTable *args) |
43e5747a | 1469 | { |
995713f4 | 1470 | const struct sr_config_info *srci; |
43e5747a UH |
1471 | GHashTableIter iter; |
1472 | gpointer key, value; | |
cfd3ec6e | 1473 | int ret; |
2f50086c | 1474 | double tmp_double; |
43d3d80b | 1475 | uint64_t tmp_u64, p, q, low, high; |
43e5747a | 1476 | gboolean tmp_bool; |
43d3d80b | 1477 | GVariant *val, *rational[2], *range[2]; |
43e5747a UH |
1478 | |
1479 | g_hash_table_iter_init(&iter, args); | |
1480 | while (g_hash_table_iter_next(&iter, &key, &value)) { | |
74b9bf0c | 1481 | if (!(srci = sr_config_info_name_get(key))) { |
cfd3ec6e BV |
1482 | g_critical("Unknown device option '%s'.", (char *) key); |
1483 | return SR_ERR; | |
1484 | } | |
1485 | ||
1486 | if ((value == NULL) && | |
995713f4 | 1487 | (srci->datatype != SR_T_BOOL)) { |
cfd3ec6e BV |
1488 | g_critical("Option '%s' needs a value.", (char *)key); |
1489 | return SR_ERR; | |
1490 | } | |
1491 | val = NULL; | |
995713f4 | 1492 | switch (srci->datatype) { |
cfd3ec6e BV |
1493 | case SR_T_UINT64: |
1494 | ret = sr_parse_sizestring(value, &tmp_u64); | |
1495 | if (ret != SR_OK) | |
8f3b8464 | 1496 | break; |
2f50086c | 1497 | val = g_variant_new_uint64(tmp_u64); |
cfd3ec6e BV |
1498 | break; |
1499 | case SR_T_CHAR: | |
2f50086c | 1500 | val = g_variant_new_string(value); |
cfd3ec6e BV |
1501 | break; |
1502 | case SR_T_BOOL: | |
1503 | if (!value) | |
1504 | tmp_bool = TRUE; | |
43e5747a | 1505 | else |
cfd3ec6e | 1506 | tmp_bool = sr_parse_boolstring(value); |
2f50086c | 1507 | val = g_variant_new_boolean(tmp_bool); |
cfd3ec6e BV |
1508 | break; |
1509 | case SR_T_FLOAT: | |
2f50086c BV |
1510 | tmp_double = strtof(value, NULL); |
1511 | val = g_variant_new_double(tmp_double); | |
cfd3ec6e BV |
1512 | break; |
1513 | case SR_T_RATIONAL_PERIOD: | |
bd31fc3f | 1514 | if ((ret = sr_parse_period(value, &p, &q)) != SR_OK) |
cfd3ec6e | 1515 | break; |
bd31fc3f BV |
1516 | rational[0] = g_variant_new_uint64(p); |
1517 | rational[1] = g_variant_new_uint64(q); | |
1518 | val = g_variant_new_tuple(rational, 2); | |
cfd3ec6e BV |
1519 | break; |
1520 | case SR_T_RATIONAL_VOLT: | |
bd31fc3f | 1521 | if ((ret = sr_parse_voltage(value, &p, &q)) != SR_OK) |
43e5747a | 1522 | break; |
bd31fc3f BV |
1523 | rational[0] = g_variant_new_uint64(p); |
1524 | rational[1] = g_variant_new_uint64(q); | |
1525 | val = g_variant_new_tuple(rational, 2); | |
cfd3ec6e | 1526 | break; |
43d3d80b BV |
1527 | case SR_T_UINT64_RANGE: |
1528 | if (sscanf(value, "%"PRIu64"-%"PRIu64, &low, &high) != 2) { | |
1529 | ret = SR_ERR; | |
1530 | break; | |
1531 | } else { | |
1532 | range[0] = g_variant_new_uint64(low); | |
1533 | range[1] = g_variant_new_uint64(high); | |
1534 | val = g_variant_new_tuple(range, 2); | |
1535 | } | |
1536 | break; | |
cfd3ec6e BV |
1537 | default: |
1538 | ret = SR_ERR; | |
43e5747a | 1539 | } |
cfd3ec6e | 1540 | if (val) |
c0b9d56b | 1541 | ret = sr_config_set(sdi, srci->key, val); |
cfd3ec6e BV |
1542 | if (ret != SR_OK) { |
1543 | g_critical("Failed to set device option '%s'.", (char *)key); | |
1544 | return ret; | |
43e5747a UH |
1545 | } |
1546 | } | |
1547 | ||
1548 | return SR_OK; | |
1549 | } | |
1550 | ||
2d73284e BV |
1551 | static void set_options(void) |
1552 | { | |
1553 | struct sr_dev_inst *sdi; | |
1554 | GSList *devices; | |
1555 | GHashTable *devargs; | |
1556 | ||
ea7741ee | 1557 | if (!opt_config) { |
2d73284e BV |
1558 | g_critical("No setting specified."); |
1559 | return; | |
1560 | } | |
1561 | ||
ea7741ee | 1562 | if (!(devargs = parse_generic_arg(opt_config, FALSE))) |
2d73284e BV |
1563 | return; |
1564 | ||
1565 | if (!(devices = device_scan())) { | |
1566 | g_critical("No devices found."); | |
1567 | return; | |
1568 | } | |
1569 | sdi = devices->data; | |
1570 | ||
0687f23d BV |
1571 | if (sr_dev_open(sdi) != SR_OK) { |
1572 | g_critical("Failed to open device."); | |
2d73284e BV |
1573 | return; |
1574 | } | |
1575 | ||
1576 | set_dev_options(sdi, devargs); | |
1577 | ||
0687f23d | 1578 | sr_dev_close(sdi); |
2d73284e BV |
1579 | g_slist_free(devices); |
1580 | g_hash_table_destroy(devargs); | |
1581 | ||
1582 | } | |
1583 | ||
37d5ccc1 | 1584 | static int set_limit_time(const struct sr_dev_inst *sdi) |
43e5747a | 1585 | { |
1616f663 | 1586 | GVariant *gvar; |
9c9c1080 | 1587 | uint64_t time_msec; |
1616f663 | 1588 | uint64_t samplerate; |
43e5747a | 1589 | |
43c062e6 | 1590 | if (!(time_msec = sr_parse_timestring(opt_time))) { |
37d5ccc1 | 1591 | g_critical("Invalid time '%s'", opt_time); |
37d5ccc1 | 1592 | return SR_ERR; |
43e5747a UH |
1593 | } |
1594 | ||
e4ffb9d6 | 1595 | if (sr_dev_has_option(sdi, SR_CONF_LIMIT_MSEC)) { |
1616f663 BV |
1596 | gvar = g_variant_new_uint64(time_msec); |
1597 | if (sr_config_set(sdi, SR_CONF_LIMIT_MSEC, gvar) != SR_OK) { | |
37d5ccc1 | 1598 | g_critical("Failed to configure time limit."); |
37d5ccc1 | 1599 | return SR_ERR; |
43e5747a | 1600 | } |
43c062e6 BV |
1601 | } else if (sr_dev_has_option(sdi, SR_CONF_SAMPLERATE)) { |
1602 | /* Convert to samples based on the samplerate. */ | |
1616f663 BV |
1603 | sr_config_get(sdi->driver, SR_CONF_SAMPLERATE, &gvar, sdi); |
1604 | samplerate = g_variant_get_uint64(gvar); | |
1605 | g_variant_unref(gvar); | |
1606 | limit_samples = (samplerate) * time_msec / (uint64_t)1000; | |
37d5ccc1 BV |
1607 | if (limit_samples == 0) { |
1608 | g_critical("Not enough time at this samplerate."); | |
37d5ccc1 | 1609 | return SR_ERR; |
43e5747a | 1610 | } |
1616f663 BV |
1611 | gvar = g_variant_new_uint64(limit_samples); |
1612 | if (sr_config_set(sdi, SR_CONF_LIMIT_SAMPLES, gvar) != SR_OK) { | |
37d5ccc1 | 1613 | g_critical("Failed to configure time-based sample limit."); |
37d5ccc1 | 1614 | return SR_ERR; |
43e5747a | 1615 | } |
43c062e6 BV |
1616 | } else { |
1617 | g_critical("This device does not support time limits."); | |
1618 | return SR_ERR; | |
37d5ccc1 | 1619 | } |
43e5747a | 1620 | |
37d5ccc1 BV |
1621 | return SR_OK; |
1622 | } | |
1623 | ||
1624 | static void run_session(void) | |
1625 | { | |
06a3fb10 | 1626 | GSList *devices; |
37d5ccc1 | 1627 | GHashTable *devargs; |
1616f663 | 1628 | GVariant *gvar; |
37d5ccc1 BV |
1629 | struct sr_dev_inst *sdi; |
1630 | int max_probes, i; | |
06a3fb10 BV |
1631 | char **triggerlist; |
1632 | ||
1633 | devices = device_scan(); | |
1634 | if (!devices) { | |
1635 | g_critical("No devices found."); | |
1636 | return; | |
1637 | } | |
1638 | if (g_slist_length(devices) > 1) { | |
1639 | g_critical("sigrok-cli only supports one device for capturing."); | |
1640 | return; | |
1641 | } | |
1642 | sdi = devices->data; | |
37d5ccc1 BV |
1643 | |
1644 | sr_session_new(); | |
74f6195b | 1645 | sr_session_datafeed_callback_add(datafeed_in, NULL); |
37d5ccc1 | 1646 | |
0687f23d BV |
1647 | if (sr_dev_open(sdi) != SR_OK) { |
1648 | g_critical("Failed to open device."); | |
1649 | return; | |
1650 | } | |
1651 | ||
8ab6aafc | 1652 | if (sr_session_dev_add(sdi) != SR_OK) { |
0687f23d | 1653 | g_critical("Failed to add device to session."); |
8ab6aafc BV |
1654 | sr_session_destroy(); |
1655 | return; | |
1656 | } | |
1657 | ||
ea7741ee BV |
1658 | if (opt_config) { |
1659 | if ((devargs = parse_generic_arg(opt_config, FALSE))) { | |
06a3fb10 BV |
1660 | if (set_dev_options(sdi, devargs) != SR_OK) |
1661 | return; | |
1662 | g_hash_table_destroy(devargs); | |
43e5747a | 1663 | } |
06a3fb10 | 1664 | } |
43e5747a | 1665 | |
06a3fb10 BV |
1666 | if (select_probes(sdi) != SR_OK) { |
1667 | g_critical("Failed to set probes."); | |
1668 | sr_session_destroy(); | |
1669 | return; | |
1670 | } | |
1671 | ||
1672 | if (opt_triggers) { | |
1673 | if (!(triggerlist = sr_parse_triggerstring(sdi, opt_triggers))) { | |
43e5747a UH |
1674 | sr_session_destroy(); |
1675 | return; | |
1676 | } | |
06a3fb10 BV |
1677 | max_probes = g_slist_length(sdi->probes); |
1678 | for (i = 0; i < max_probes; i++) { | |
1679 | if (triggerlist[i]) { | |
1680 | sr_dev_trigger_set(sdi, i, triggerlist[i]); | |
1681 | g_free(triggerlist[i]); | |
43e5747a UH |
1682 | } |
1683 | } | |
06a3fb10 BV |
1684 | g_free(triggerlist); |
1685 | } | |
9c9c1080 | 1686 | |
06a3fb10 | 1687 | if (opt_continuous) { |
e4ffb9d6 | 1688 | if (!sr_dev_has_option(sdi, SR_CONF_CONTINUOUS)) { |
06a3fb10 | 1689 | g_critical("This device does not support continuous sampling."); |
37d5ccc1 BV |
1690 | sr_session_destroy(); |
1691 | return; | |
1692 | } | |
37d5ccc1 BV |
1693 | } |
1694 | ||
1695 | if (opt_time) { | |
1696 | if (set_limit_time(sdi) != SR_OK) { | |
1697 | sr_session_destroy(); | |
1698 | return; | |
43e5747a UH |
1699 | } |
1700 | } | |
1701 | ||
1702 | if (opt_samples) { | |
1616f663 BV |
1703 | if ((sr_parse_sizestring(opt_samples, &limit_samples) != SR_OK)) { |
1704 | g_critical("Invalid sample limit '%s'.", opt_samples); | |
1705 | sr_session_destroy(); | |
1706 | return; | |
1707 | } | |
1708 | gvar = g_variant_new_uint64(limit_samples); | |
1709 | if (sr_config_set(sdi, SR_CONF_LIMIT_SAMPLES, gvar) != SR_OK) { | |
8170b8ea | 1710 | g_critical("Failed to configure sample limit."); |
43e5747a UH |
1711 | sr_session_destroy(); |
1712 | return; | |
1713 | } | |
1714 | } | |
1715 | ||
ce48d892 | 1716 | if (opt_frames) { |
1616f663 BV |
1717 | if ((sr_parse_sizestring(opt_frames, &limit_frames) != SR_OK)) { |
1718 | g_critical("Invalid sample limit '%s'.", opt_samples); | |
1719 | sr_session_destroy(); | |
1720 | return; | |
1721 | } | |
2f50086c | 1722 | gvar = g_variant_new_uint64(limit_frames); |
1616f663 | 1723 | if (sr_config_set(sdi, SR_CONF_LIMIT_FRAMES, gvar) != SR_OK) { |
f8ccd825 | 1724 | g_critical("Failed to configure frame limit."); |
ce48d892 BV |
1725 | sr_session_destroy(); |
1726 | return; | |
1727 | } | |
1728 | } | |
1729 | ||
43e5747a | 1730 | if (sr_session_start() != SR_OK) { |
8170b8ea | 1731 | g_critical("Failed to start session."); |
43e5747a UH |
1732 | sr_session_destroy(); |
1733 | return; | |
1734 | } | |
1735 | ||
1736 | if (opt_continuous) | |
1737 | add_anykey(); | |
1738 | ||
1739 | sr_session_run(); | |
1740 | ||
1741 | if (opt_continuous) | |
1742 | clear_anykey(); | |
1743 | ||
43e5747a | 1744 | sr_session_destroy(); |
37d5ccc1 BV |
1745 | g_slist_free(devices); |
1746 | ||
43e5747a UH |
1747 | } |
1748 | ||
1749 | static void logger(const gchar *log_domain, GLogLevelFlags log_level, | |
68cdf174 | 1750 | const gchar *message, gpointer cb_data) |
43e5747a | 1751 | { |
43e5747a | 1752 | (void)log_domain; |
68cdf174 | 1753 | (void)cb_data; |
43e5747a UH |
1754 | |
1755 | /* | |
1756 | * All messages, warnings, errors etc. go to stderr (not stdout) in | |
1757 | * order to not mess up the CLI tool data output, e.g. VCD output. | |
1758 | */ | |
2d6ff326 | 1759 | if (log_level & (G_LOG_LEVEL_ERROR | G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_WARNING) |
d740e6ac | 1760 | || opt_loglevel > SR_LOG_WARN) { |
43e5747a UH |
1761 | fprintf(stderr, "%s\n", message); |
1762 | fflush(stderr); | |
43e5747a UH |
1763 | } |
1764 | } | |
1765 | ||
1766 | int main(int argc, char **argv) | |
1767 | { | |
43e5747a UH |
1768 | GOptionContext *context; |
1769 | GError *error; | |
17982bc9 BV |
1770 | int ret; |
1771 | char *help; | |
43e5747a UH |
1772 | |
1773 | g_log_set_default_handler(logger, NULL); | |
43e5747a | 1774 | |
43e5747a UH |
1775 | context = g_option_context_new(NULL); |
1776 | g_option_context_add_main_entries(context, optargs, NULL); | |
1777 | ||
17982bc9 BV |
1778 | ret = 1; |
1779 | error = NULL; | |
43e5747a | 1780 | if (!g_option_context_parse(context, &argc, &argv, &error)) { |
8170b8ea | 1781 | g_critical("%s", error->message); |
5acb7682 | 1782 | goto done; |
43e5747a UH |
1783 | } |
1784 | ||
1785 | /* Set the loglevel (amount of messages to output) for libsigrok. */ | |
120f9ee7 | 1786 | if (sr_log_loglevel_set(opt_loglevel) != SR_OK) |
5acb7682 | 1787 | goto done; |
43e5747a | 1788 | |
de0a066e | 1789 | if (sr_init(&sr_ctx) != SR_OK) |
5acb7682 | 1790 | goto done; |
6de7ec06 | 1791 | |
de0a066e BV |
1792 | #ifdef HAVE_SRD |
1793 | /* Set the loglevel (amount of messages to output) for libsigrokdecode. */ | |
1794 | if (srd_log_loglevel_set(opt_loglevel) != SRD_OK) | |
5acb7682 | 1795 | goto done; |
43e5747a UH |
1796 | |
1797 | if (opt_pds) { | |
120f9ee7 | 1798 | if (srd_init(NULL) != SRD_OK) |
198f4c6c | 1799 | goto done; |
120f9ee7 | 1800 | if (register_pds(NULL, opt_pds) != 0) |
198f4c6c | 1801 | goto done; |
4359a4da | 1802 | if (srd_pd_output_callback_add(SRD_OUTPUT_ANN, |
b6bd032d | 1803 | show_pd_annotations, NULL) != SRD_OK) |
198f4c6c | 1804 | goto done; |
120f9ee7 | 1805 | if (setup_pd_stack() != 0) |
198f4c6c | 1806 | goto done; |
120f9ee7 | 1807 | if (setup_pd_annotations() != 0) |
198f4c6c | 1808 | goto done; |
9f4a898e | 1809 | } |
de0a066e | 1810 | #endif |
9f4a898e | 1811 | |
ad2bc491 | 1812 | if (setup_output_format() != 0) |
198f4c6c | 1813 | goto done; |
43e5747a UH |
1814 | |
1815 | if (opt_version) | |
1816 | show_version(); | |
ea7741ee | 1817 | else if (opt_scan_devs) |
1e0f9ed9 | 1818 | show_dev_list(); |
de0a066e | 1819 | #ifdef HAVE_SRD |
f83fbc57 BV |
1820 | else if (opt_pds && opt_show) |
1821 | show_pd_detail(); | |
de0a066e | 1822 | #endif |
22981b2c BV |
1823 | else if (opt_show) |
1824 | show_dev_detail(); | |
43e5747a UH |
1825 | else if (opt_input_file) |
1826 | load_input_file(); | |
2d73284e BV |
1827 | else if (opt_set) |
1828 | set_options(); | |
ce48d892 | 1829 | else if (opt_samples || opt_time || opt_frames || opt_continuous) |
43e5747a | 1830 | run_session(); |
17982bc9 BV |
1831 | else { |
1832 | help = g_option_context_get_help(context, TRUE, NULL); | |
1833 | printf("%s", help); | |
1834 | g_free(help); | |
1835 | } | |
43e5747a | 1836 | |
de0a066e | 1837 | #ifdef HAVE_SRD |
43e5747a UH |
1838 | if (opt_pds) |
1839 | srd_exit(); | |
de0a066e | 1840 | #endif |
43e5747a | 1841 | |
198f4c6c PS |
1842 | ret = 0; |
1843 | ||
1844 | done: | |
5acb7682 PS |
1845 | if (sr_ctx) |
1846 | sr_exit(sr_ctx); | |
198f4c6c | 1847 | |
8630d4a8 | 1848 | g_option_context_free(context); |
43e5747a | 1849 | |
198f4c6c | 1850 | return ret; |
43e5747a | 1851 | } |