]>
Commit | Line | Data |
---|---|---|
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 <stdlib.h> | |
21 | #include <string.h> | |
22 | #include "sigrok-cli.h" | |
23 | #include "config.h" | |
24 | #include <glib.h> | |
25 | ||
26 | #ifdef HAVE_SRD | |
27 | static GHashTable *pd_ann_visible = NULL; | |
28 | static GHashTable *pd_meta_visible = NULL; | |
29 | static GHashTable *pd_binary_visible = NULL; | |
30 | static GHashTable *pd_channel_maps = NULL; | |
31 | ||
32 | extern struct srd_session *srd_sess; | |
33 | extern gint opt_loglevel; | |
34 | ||
35 | ||
36 | static int opts_to_gvar(struct srd_decoder *dec, GHashTable *hash, | |
37 | GHashTable **options) | |
38 | { | |
39 | struct srd_decoder_option *o; | |
40 | GSList *optl; | |
41 | GVariant *gvar; | |
42 | gint64 val_int; | |
43 | int ret; | |
44 | char *val_str, *conv; | |
45 | ||
46 | ret = TRUE; | |
47 | *options = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, | |
48 | (GDestroyNotify)g_variant_unref); | |
49 | ||
50 | for (optl = dec->options; optl; optl = optl->next) { | |
51 | o = optl->data; | |
52 | if (!(val_str = g_hash_table_lookup(hash, o->id))) | |
53 | /* Not specified. */ | |
54 | continue; | |
55 | if (g_variant_is_of_type(o->def, G_VARIANT_TYPE_STRING)) { | |
56 | gvar = g_variant_new_string(val_str); | |
57 | } else if (g_variant_is_of_type(o->def, G_VARIANT_TYPE_INT64)) { | |
58 | val_int = strtoll(val_str, &conv, 0); | |
59 | if (!conv || conv == val_str) { | |
60 | g_critical("Protocol decoder '%s' option '%s' " | |
61 | "requires a number.", dec->name, o->id); | |
62 | ret = FALSE; | |
63 | break; | |
64 | } | |
65 | gvar = g_variant_new_int64(val_int); | |
66 | } else { | |
67 | g_critical("Unsupported type for option '%s' (%s)", | |
68 | o->id, g_variant_get_type_string(o->def)); | |
69 | ret = FALSE; | |
70 | break; | |
71 | } | |
72 | g_variant_ref_sink(gvar); | |
73 | g_hash_table_insert(*options, g_strdup(o->id), gvar); | |
74 | g_hash_table_remove(hash, o->id); | |
75 | } | |
76 | ||
77 | return ret; | |
78 | } | |
79 | ||
80 | static int move_hash_element(GHashTable *src, GHashTable *dest, void *key) | |
81 | { | |
82 | void *orig_key, *value; | |
83 | ||
84 | if (!g_hash_table_lookup_extended(src, key, &orig_key, &value)) | |
85 | /* Not specified. */ | |
86 | return FALSE; | |
87 | g_hash_table_steal(src, orig_key); | |
88 | g_hash_table_insert(dest, orig_key, value); | |
89 | ||
90 | return TRUE; | |
91 | } | |
92 | ||
93 | static GHashTable *extract_channel_map(struct srd_decoder *dec, GHashTable *hash) | |
94 | { | |
95 | GHashTable *channel_map; | |
96 | struct srd_channel *pdch; | |
97 | GSList *l; | |
98 | ||
99 | channel_map = g_hash_table_new_full(g_str_hash, g_str_equal, | |
100 | g_free, g_free); | |
101 | ||
102 | for (l = dec->channels; l; l = l->next) { | |
103 | pdch = l->data; | |
104 | move_hash_element(hash, channel_map, pdch->id); | |
105 | } | |
106 | for (l = dec->opt_channels; l; l = l->next) { | |
107 | pdch = l->data; | |
108 | move_hash_element(hash, channel_map, pdch->id); | |
109 | } | |
110 | ||
111 | return channel_map; | |
112 | } | |
113 | ||
114 | /* Register the given PDs for this session. | |
115 | * Accepts a string of the form: "spi:sck=3:sdata=4,spi:sck=3:sdata=5" | |
116 | * That will instantiate two SPI decoders on the clock but different data | |
117 | * lines. | |
118 | */ | |
119 | int register_pds(const char *opt_pds, char *opt_pd_annotations) | |
120 | { | |
121 | struct srd_decoder *dec; | |
122 | GHashTable *pd_opthash, *options, *channels; | |
123 | GList *leftover, *l; | |
124 | struct srd_decoder_inst *di; | |
125 | int ret; | |
126 | char **pdtokens, **pdtok, *pd_name; | |
127 | ||
128 | pd_ann_visible = g_hash_table_new_full(g_str_hash, g_str_equal, | |
129 | g_free, NULL); | |
130 | ret = 0; | |
131 | pd_name = NULL; | |
132 | pd_opthash = options = channels = pd_channel_maps = NULL; | |
133 | pdtokens = g_strsplit(opt_pds, ",", 0); | |
134 | for (pdtok = pdtokens; *pdtok; pdtok++) { | |
135 | if (!(pd_opthash = parse_generic_arg(*pdtok, TRUE))) { | |
136 | g_critical("Invalid protocol decoder option '%s'.", *pdtok); | |
137 | break; | |
138 | } | |
139 | ||
140 | pd_name = g_strdup(g_hash_table_lookup(pd_opthash, "sigrok_key")); | |
141 | g_hash_table_remove(pd_opthash, "sigrok_key"); | |
142 | if (srd_decoder_load(pd_name) != SRD_OK) { | |
143 | g_critical("Failed to load protocol decoder %s.", pd_name); | |
144 | ret = 1; | |
145 | break; | |
146 | } | |
147 | if (!(dec = srd_decoder_get_by_id(pd_name))) { | |
148 | g_critical("Failed to get decoder %s by id.", pd_name); | |
149 | ret = 1; | |
150 | break; | |
151 | } | |
152 | ||
153 | /* Convert decoder option and channel values to GVariant. */ | |
154 | if (!opts_to_gvar(dec, pd_opthash, &options)) { | |
155 | ret = 1; | |
156 | break; | |
157 | } | |
158 | channels = extract_channel_map(dec, pd_opthash); | |
159 | ||
160 | if (g_hash_table_size(pd_opthash) > 0) { | |
161 | leftover = g_hash_table_get_keys(pd_opthash); | |
162 | for (l = leftover; l; l = l->next) | |
163 | g_critical("Unknown option or channel '%s'", (char *)l->data); | |
164 | g_list_free(leftover); | |
165 | break; | |
166 | } | |
167 | ||
168 | if (!(di = srd_inst_new(srd_sess, pd_name, options))) { | |
169 | g_critical("Failed to instantiate protocol decoder %s.", pd_name); | |
170 | ret = 1; | |
171 | break; | |
172 | } | |
173 | ||
174 | if (pdtok == pdtokens) { | |
175 | /* | |
176 | * Save the channel setup for later, but only on the | |
177 | * first decoder (stacked decoders don't get channels). | |
178 | */ | |
179 | pd_channel_maps = g_hash_table_new_full(g_str_hash, | |
180 | g_str_equal, g_free, (GDestroyNotify)g_hash_table_destroy); | |
181 | g_hash_table_insert(pd_channel_maps, g_strdup(di->inst_id), channels); | |
182 | channels = NULL; | |
183 | } | |
184 | ||
185 | /* If no annotation list was specified, add them all in now. | |
186 | * This will be pared down later to leave only the last PD | |
187 | * in the stack. | |
188 | */ | |
189 | if (!opt_pd_annotations) | |
190 | g_hash_table_insert(pd_ann_visible, g_strdup(di->inst_id), | |
191 | g_slist_append(NULL, GINT_TO_POINTER(-1))); | |
192 | } | |
193 | ||
194 | g_strfreev(pdtokens); | |
195 | if (pd_opthash) | |
196 | g_hash_table_destroy(pd_opthash); | |
197 | if (options) | |
198 | g_hash_table_destroy(options); | |
199 | if (channels) | |
200 | g_hash_table_destroy(channels); | |
201 | if (pd_name) | |
202 | g_free(pd_name); | |
203 | ||
204 | return ret; | |
205 | } | |
206 | ||
207 | static void map_pd_inst_channels(void *key, void *value, void *user_data) | |
208 | { | |
209 | GHashTable *channel_map; | |
210 | GHashTable *channel_indices; | |
211 | GSList *channel_list; | |
212 | struct srd_decoder_inst *di; | |
213 | GVariant *var; | |
214 | void *channel_id; | |
215 | void *channel_target; | |
216 | struct sr_channel *ch; | |
217 | GHashTableIter iter; | |
218 | int num_channels; | |
219 | ||
220 | channel_map = value; | |
221 | channel_list = user_data; | |
222 | ||
223 | di = srd_inst_find_by_id(srd_sess, key); | |
224 | if (!di) { | |
225 | g_critical("Protocol decoder instance \"%s\" not found.", | |
226 | (char *)key); | |
227 | return; | |
228 | } | |
229 | channel_indices = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, | |
230 | (GDestroyNotify)g_variant_unref); | |
231 | ||
232 | g_hash_table_iter_init(&iter, channel_map); | |
233 | while (g_hash_table_iter_next(&iter, &channel_id, &channel_target)) { | |
234 | ch = find_channel(channel_list, channel_target); | |
235 | if (!ch) { | |
236 | g_printerr("cli: No channel with name \"%s\" found.\n", | |
237 | (char *)channel_target); | |
238 | continue; | |
239 | } | |
240 | if (!ch->enabled) | |
241 | g_printerr("cli: Target channel \"%s\" not enabled.\n", | |
242 | (char *)channel_target); | |
243 | ||
244 | var = g_variant_new_int32(ch->index); | |
245 | g_variant_ref_sink(var); | |
246 | g_hash_table_insert(channel_indices, g_strdup(channel_id), var); | |
247 | } | |
248 | ||
249 | num_channels = g_slist_length(channel_list); | |
250 | srd_inst_channel_set_all(di, channel_indices, (num_channels + 7) / 8); | |
251 | } | |
252 | ||
253 | void map_pd_channels(struct sr_dev_inst *sdi) | |
254 | { | |
255 | if (pd_channel_maps) { | |
256 | g_hash_table_foreach(pd_channel_maps, &map_pd_inst_channels, | |
257 | sdi->channels); | |
258 | g_hash_table_destroy(pd_channel_maps); | |
259 | pd_channel_maps = NULL; | |
260 | } | |
261 | } | |
262 | ||
263 | int setup_pd_stack(char *opt_pds, char *opt_pd_stack, char *opt_pd_annotations) | |
264 | { | |
265 | struct srd_decoder_inst *di_from, *di_to; | |
266 | int ret, i; | |
267 | char **pds, **ids; | |
268 | ||
269 | /* Set up the protocol decoder stack. */ | |
270 | pds = g_strsplit(opt_pds, ",", 0); | |
271 | if (g_strv_length(pds) > 1) { | |
272 | if (opt_pd_stack) { | |
273 | /* A stack setup was specified, use that. */ | |
274 | g_strfreev(pds); | |
275 | pds = g_strsplit(opt_pd_stack, ",", 0); | |
276 | if (g_strv_length(pds) < 2) { | |
277 | g_strfreev(pds); | |
278 | g_critical("Specify at least two protocol decoders to stack."); | |
279 | return 1; | |
280 | } | |
281 | } | |
282 | ||
283 | /* First PD goes at the bottom of the stack. */ | |
284 | ids = g_strsplit(pds[0], ":", 0); | |
285 | if (!(di_from = srd_inst_find_by_id(srd_sess, ids[0]))) { | |
286 | g_strfreev(ids); | |
287 | g_critical("Cannot stack protocol decoder '%s': " | |
288 | "instance not found.", pds[0]); | |
289 | return 1; | |
290 | } | |
291 | g_strfreev(ids); | |
292 | ||
293 | /* Every subsequent PD goes on top. */ | |
294 | for (i = 1; pds[i]; i++) { | |
295 | ids = g_strsplit(pds[i], ":", 0); | |
296 | if (!(di_to = srd_inst_find_by_id(srd_sess, ids[0]))) { | |
297 | g_strfreev(ids); | |
298 | g_critical("Cannot stack protocol decoder '%s': " | |
299 | "instance not found.", pds[i]); | |
300 | return 1; | |
301 | } | |
302 | g_strfreev(ids); | |
303 | if ((ret = srd_inst_stack(srd_sess, di_from, di_to)) != SRD_OK) | |
304 | return 1; | |
305 | ||
306 | /* Don't show annotation from this PD. Only the last PD in | |
307 | * the stack will be left on the annotation list (unless | |
308 | * the annotation list was specifically provided). | |
309 | */ | |
310 | if (!opt_pd_annotations) | |
311 | g_hash_table_remove(pd_ann_visible, di_from->inst_id); | |
312 | ||
313 | di_from = di_to; | |
314 | } | |
315 | } | |
316 | g_strfreev(pds); | |
317 | ||
318 | return 0; | |
319 | } | |
320 | ||
321 | int setup_pd_annotations(char *opt_pd_annotations) | |
322 | { | |
323 | GSList *l, *l_ann; | |
324 | struct srd_decoder *dec; | |
325 | int ann_class; | |
326 | char **pds, **pdtok, **keyval, **annlist, **ann, **ann_descr; | |
327 | ||
328 | /* Set up custom list of PDs and annotations to show. */ | |
329 | pds = g_strsplit(opt_pd_annotations, ",", 0); | |
330 | for (pdtok = pds; *pdtok && **pdtok; pdtok++) { | |
331 | keyval = g_strsplit(*pdtok, "=", 0); | |
332 | if (!(dec = srd_decoder_get_by_id(keyval[0]))) { | |
333 | g_critical("Protocol decoder '%s' not found.", keyval[0]); | |
334 | return 1; | |
335 | } | |
336 | if (!dec->annotations) { | |
337 | g_critical("Protocol decoder '%s' has no annotations.", keyval[0]); | |
338 | return 1; | |
339 | } | |
340 | if (g_strv_length(keyval) == 2 && keyval[1][0] != '\0') { | |
341 | annlist = g_strsplit(keyval[1], ":", 0); | |
342 | for (ann = annlist; *ann && **ann; ann++) { | |
343 | ann_class = 0; | |
344 | for (l = dec->annotations; l; l = l->next, ann_class++) { | |
345 | ann_descr = l->data; | |
346 | if (!canon_cmp(ann_descr[0], *ann)) | |
347 | /* Found it. */ | |
348 | break; | |
349 | } | |
350 | if (!l) { | |
351 | g_critical("Annotation '%s' not found " | |
352 | "for protocol decoder '%s'.", *ann, keyval[0]); | |
353 | return 1; | |
354 | } | |
355 | l_ann = g_hash_table_lookup(pd_ann_visible, keyval[0]); | |
356 | l_ann = g_slist_append(l_ann, GINT_TO_POINTER(ann_class)); | |
357 | g_hash_table_replace(pd_ann_visible, g_strdup(keyval[0]), l_ann); | |
358 | g_debug("cli: Showing protocol decoder %s annotation " | |
359 | "class %d (%s).", keyval[0], ann_class, ann_descr[0]); | |
360 | } | |
361 | } else { | |
362 | /* No class specified: show all of them. */ | |
363 | g_hash_table_insert(pd_ann_visible, g_strdup(keyval[0]), | |
364 | g_slist_append(NULL, GINT_TO_POINTER(-1))); | |
365 | g_debug("cli: Showing all annotation classes for protocol " | |
366 | "decoder %s.", keyval[0]); | |
367 | } | |
368 | g_strfreev(keyval); | |
369 | } | |
370 | g_strfreev(pds); | |
371 | ||
372 | return 0; | |
373 | } | |
374 | ||
375 | int setup_pd_meta(char *opt_pd_meta) | |
376 | { | |
377 | struct srd_decoder *dec; | |
378 | char **pds, **pdtok; | |
379 | ||
380 | pd_meta_visible = g_hash_table_new_full(g_str_hash, g_int_equal, | |
381 | g_free, NULL); | |
382 | pds = g_strsplit(opt_pd_meta, ",", 0); | |
383 | for (pdtok = pds; *pdtok && **pdtok; pdtok++) { | |
384 | if (!(dec = srd_decoder_get_by_id(*pdtok))) { | |
385 | g_critical("Protocol decoder '%s' not found.", *pdtok); | |
386 | return 1; | |
387 | } | |
388 | g_debug("cli: Showing protocol decoder meta output from '%s'.", *pdtok); | |
389 | g_hash_table_insert(pd_meta_visible, g_strdup(*pdtok), NULL); | |
390 | } | |
391 | g_strfreev(pds); | |
392 | ||
393 | return 0; | |
394 | } | |
395 | ||
396 | int setup_pd_binary(char *opt_pd_binary) | |
397 | { | |
398 | GSList *l; | |
399 | struct srd_decoder *dec; | |
400 | int bin_class; | |
401 | char **pds, **pdtok, **keyval, **bin_name; | |
402 | ||
403 | pd_binary_visible = g_hash_table_new_full(g_str_hash, g_int_equal, | |
404 | g_free, NULL); | |
405 | pds = g_strsplit(opt_pd_binary, ",", 0); | |
406 | for (pdtok = pds; *pdtok && **pdtok; pdtok++) { | |
407 | keyval = g_strsplit(*pdtok, "=", 0); | |
408 | if (!(dec = srd_decoder_get_by_id(keyval[0]))) { | |
409 | g_critical("Protocol decoder '%s' not found.", keyval[0]); | |
410 | return 1; | |
411 | } | |
412 | if (!dec->binary) { | |
413 | g_critical("Protocol decoder '%s' has no binary output.", keyval[0]); | |
414 | return 1; | |
415 | } | |
416 | bin_class = 0; | |
417 | if (g_strv_length(keyval) == 2) { | |
418 | for (l = dec->binary; l; l = l->next, bin_class++) { | |
419 | bin_name = l->data; | |
420 | if (!strcmp(bin_name[0], keyval[1])) | |
421 | /* Found it. */ | |
422 | break; | |
423 | } | |
424 | if (!l) { | |
425 | g_critical("binary output '%s' not found " | |
426 | "for protocol decoder '%s'.", keyval[1], keyval[0]); | |
427 | return 1; | |
428 | } | |
429 | g_debug("cli: Showing protocol decoder %s binary class " | |
430 | "%d (%s).", keyval[0], bin_class, bin_name[0]); | |
431 | } else { | |
432 | /* No class specified: output all of them. */ | |
433 | bin_class = -1; | |
434 | g_debug("cli: Showing all binary classes for protocol " | |
435 | "decoder %s.", keyval[0]); | |
436 | } | |
437 | g_hash_table_insert(pd_binary_visible, g_strdup(keyval[0]), GINT_TO_POINTER(bin_class)); | |
438 | g_strfreev(keyval); | |
439 | } | |
440 | g_strfreev(pds); | |
441 | ||
442 | return 0; | |
443 | } | |
444 | ||
445 | void show_pd_annotations(struct srd_proto_data *pdata, void *cb_data) | |
446 | { | |
447 | struct srd_decoder *dec; | |
448 | struct srd_proto_data_annotation *pda; | |
449 | GSList *ann_list, *l; | |
450 | int i; | |
451 | char **ann_descr; | |
452 | gboolean show; | |
453 | ||
454 | /* 'cb_data' is not used in this specific callback. */ | |
455 | (void)cb_data; | |
456 | ||
457 | if (!pd_ann_visible) | |
458 | return; | |
459 | ||
460 | if (!g_hash_table_lookup_extended(pd_ann_visible, pdata->pdo->di->inst_id, | |
461 | NULL, (void **)&ann_list)) | |
462 | /* Not in the list of PDs whose annotations we're showing. */ | |
463 | return; | |
464 | ||
465 | dec = pdata->pdo->di->decoder; | |
466 | pda = pdata->data; | |
467 | show = FALSE; | |
468 | for (l = ann_list; l; l = l->next) { | |
469 | if (GPOINTER_TO_INT(l->data) == -1 | |
470 | || GPOINTER_TO_INT(l->data) == pda->ann_format) { | |
471 | show = TRUE; | |
472 | break; | |
473 | } | |
474 | } | |
475 | if (!show) | |
476 | return; | |
477 | ||
478 | if (opt_loglevel <= SR_LOG_WARN) { | |
479 | /* Show only the longest annotation. */ | |
480 | printf("%s", pda->ann_text[0]); | |
481 | } else if (opt_loglevel >= SR_LOG_INFO) { | |
482 | /* Sample numbers and quotes around the longest annotation. */ | |
483 | printf("%"PRIu64"-%"PRIu64"", pdata->start_sample, pdata->end_sample); | |
484 | if (opt_loglevel == SR_LOG_INFO) { | |
485 | printf(" \"%s\"", pda->ann_text[0]); | |
486 | } else { | |
487 | /* Protocol decoder id, annotation class, | |
488 | * all annotation strings. */ | |
489 | ann_descr = g_slist_nth_data(dec->annotations, pda->ann_format); | |
490 | printf(" %s: %s:", pdata->pdo->proto_id, ann_descr[0]); | |
491 | for (i = 0; pda->ann_text[i]; i++) | |
492 | printf(" \"%s\"", pda->ann_text[i]); | |
493 | } | |
494 | } | |
495 | printf("\n"); | |
496 | fflush(stdout); | |
497 | } | |
498 | ||
499 | void show_pd_meta(struct srd_proto_data *pdata, void *cb_data) | |
500 | { | |
501 | ||
502 | /* 'cb_data' is not used in this specific callback. */ | |
503 | (void)cb_data; | |
504 | ||
505 | if (!g_hash_table_lookup_extended(pd_meta_visible, | |
506 | pdata->pdo->di->decoder->id, NULL, NULL)) | |
507 | /* Not in the list of PDs whose meta output we're showing. */ | |
508 | return; | |
509 | ||
510 | if (opt_loglevel > SR_LOG_WARN) | |
511 | printf("%"PRIu64"-%"PRIu64" ", pdata->start_sample, pdata->end_sample); | |
512 | printf("%s: ", pdata->pdo->proto_id); | |
513 | printf("%s: %s", pdata->pdo->meta_name, g_variant_print(pdata->data, FALSE)); | |
514 | printf("\n"); | |
515 | fflush(stdout); | |
516 | } | |
517 | ||
518 | void show_pd_binary(struct srd_proto_data *pdata, void *cb_data) | |
519 | { | |
520 | struct srd_proto_data_binary *pdb; | |
521 | gpointer classp; | |
522 | int class; | |
523 | ||
524 | /* 'cb_data' is not used in this specific callback. */ | |
525 | (void)cb_data; | |
526 | ||
527 | if (!g_hash_table_lookup_extended(pd_binary_visible, | |
528 | pdata->pdo->di->decoder->id, NULL, (void **)&classp)) | |
529 | /* Not in the list of PDs whose meta output we're showing. */ | |
530 | return; | |
531 | ||
532 | class = GPOINTER_TO_INT(classp); | |
533 | pdb = pdata->data; | |
534 | if (class != -1 && class != pdb->bin_class) | |
535 | /* Not showing this binary class. */ | |
536 | return; | |
537 | ||
538 | /* Just send the binary output to stdout, no embellishments. */ | |
539 | fwrite(pdb->data, pdb->size, 1, stdout); | |
540 | fflush(stdout); | |
541 | } | |
542 | #endif | |
543 |