]>
Commit | Line | Data |
---|---|---|
2be182e6 BV |
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 | ||
d486cbdd | 20 | #include <config.h> |
f0f54487 UH |
21 | #include <stdlib.h> |
22 | #include <string.h> | |
2be182e6 | 23 | #include <glib.h> |
662a1e27 | 24 | #include "sigrok-cli.h" |
2be182e6 BV |
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; | |
3dfbfbc8 | 30 | static GHashTable *pd_channel_maps = NULL; |
2be182e6 | 31 | |
69110b5c GS |
32 | uint64_t pd_samplerate = 0; |
33 | ||
2be182e6 | 34 | extern struct srd_session *srd_sess; |
2be182e6 BV |
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; | |
4c1a29f4 | 43 | double val_dbl; |
2be182e6 BV |
44 | int ret; |
45 | char *val_str, *conv; | |
46 | ||
47 | ret = TRUE; | |
48 | *options = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, | |
49 | (GDestroyNotify)g_variant_unref); | |
50 | ||
51 | for (optl = dec->options; optl; optl = optl->next) { | |
52 | o = optl->data; | |
53 | if (!(val_str = g_hash_table_lookup(hash, o->id))) | |
54 | /* Not specified. */ | |
55 | continue; | |
56 | if (g_variant_is_of_type(o->def, G_VARIANT_TYPE_STRING)) { | |
57 | gvar = g_variant_new_string(val_str); | |
58 | } else if (g_variant_is_of_type(o->def, G_VARIANT_TYPE_INT64)) { | |
b5c8f3a1 | 59 | conv = NULL; |
2be182e6 | 60 | val_int = strtoll(val_str, &conv, 0); |
b5c8f3a1 | 61 | if (!conv || conv == val_str || *conv) { |
2be182e6 BV |
62 | g_critical("Protocol decoder '%s' option '%s' " |
63 | "requires a number.", dec->name, o->id); | |
64 | ret = FALSE; | |
65 | break; | |
66 | } | |
67 | gvar = g_variant_new_int64(val_int); | |
4c1a29f4 GS |
68 | } else if (g_variant_is_of_type(o->def, G_VARIANT_TYPE_DOUBLE)) { |
69 | conv = NULL; | |
70 | val_dbl = strtod(val_str, &conv); | |
71 | if (!conv || conv == val_str || *conv) { | |
72 | g_critical("Protocol decoder '%s' option '%s' requires a float number.", | |
73 | dec->name, o->id); | |
74 | ret = FALSE; | |
75 | break; | |
76 | } | |
77 | gvar = g_variant_new_double(val_dbl); | |
2be182e6 BV |
78 | } else { |
79 | g_critical("Unsupported type for option '%s' (%s)", | |
80 | o->id, g_variant_get_type_string(o->def)); | |
81 | ret = FALSE; | |
82 | break; | |
83 | } | |
84 | g_variant_ref_sink(gvar); | |
85 | g_hash_table_insert(*options, g_strdup(o->id), gvar); | |
86 | g_hash_table_remove(hash, o->id); | |
87 | } | |
88 | ||
89 | return ret; | |
90 | } | |
91 | ||
39aedc34 | 92 | static int move_hash_element(GHashTable *src, GHashTable *dest, void *key) |
2be182e6 | 93 | { |
39aedc34 DE |
94 | void *orig_key, *value; |
95 | ||
96 | if (!g_hash_table_lookup_extended(src, key, &orig_key, &value)) | |
97 | /* Not specified. */ | |
98 | return FALSE; | |
99 | g_hash_table_steal(src, orig_key); | |
100 | g_hash_table_insert(dest, orig_key, value); | |
101 | ||
102 | return TRUE; | |
103 | } | |
104 | ||
3dfbfbc8 | 105 | static GHashTable *extract_channel_map(struct srd_decoder *dec, GHashTable *hash) |
39aedc34 | 106 | { |
3dfbfbc8 UH |
107 | GHashTable *channel_map; |
108 | struct srd_channel *pdch; | |
39aedc34 | 109 | GSList *l; |
2be182e6 | 110 | |
3dfbfbc8 | 111 | channel_map = g_hash_table_new_full(g_str_hash, g_str_equal, |
39aedc34 | 112 | g_free, g_free); |
2be182e6 | 113 | |
3dfbfbc8 UH |
114 | for (l = dec->channels; l; l = l->next) { |
115 | pdch = l->data; | |
116 | move_hash_element(hash, channel_map, pdch->id); | |
39aedc34 | 117 | } |
3dfbfbc8 UH |
118 | for (l = dec->opt_channels; l; l = l->next) { |
119 | pdch = l->data; | |
120 | move_hash_element(hash, channel_map, pdch->id); | |
2be182e6 | 121 | } |
2be182e6 | 122 | |
3dfbfbc8 | 123 | return channel_map; |
2be182e6 BV |
124 | } |
125 | ||
551f570c | 126 | static int register_pd(char *opt_pds, char *opt_pd_annotations) |
2be182e6 | 127 | { |
551f570c | 128 | int ret; |
2be182e6 | 129 | struct srd_decoder *dec; |
551f570c KP |
130 | struct srd_decoder_inst *di, *di_prior; |
131 | char **pdtokens, **pdtok, *pd_name; | |
3dfbfbc8 | 132 | GHashTable *pd_opthash, *options, *channels; |
2be182e6 | 133 | GList *leftover, *l; |
2be182e6 | 134 | |
2be182e6 BV |
135 | ret = 0; |
136 | pd_name = NULL; | |
551f570c KP |
137 | di_prior = NULL; |
138 | pd_opthash = options = channels = NULL; | |
139 | ||
26c63758 | 140 | pdtokens = g_strsplit(opt_pds, ",", 0); |
2be182e6 | 141 | for (pdtok = pdtokens; *pdtok; pdtok++) { |
cad0cba6 | 142 | if (!(pd_opthash = parse_generic_arg(*pdtok, TRUE, NULL))) { |
2be182e6 BV |
143 | g_critical("Invalid protocol decoder option '%s'.", *pdtok); |
144 | break; | |
145 | } | |
146 | ||
147 | pd_name = g_strdup(g_hash_table_lookup(pd_opthash, "sigrok_key")); | |
148 | g_hash_table_remove(pd_opthash, "sigrok_key"); | |
149 | if (srd_decoder_load(pd_name) != SRD_OK) { | |
150 | g_critical("Failed to load protocol decoder %s.", pd_name); | |
151 | ret = 1; | |
152 | break; | |
153 | } | |
3a8ceb6a BV |
154 | if (!(dec = srd_decoder_get_by_id(pd_name))) { |
155 | g_critical("Failed to get decoder %s by id.", pd_name); | |
156 | ret = 1; | |
157 | break; | |
158 | } | |
2be182e6 | 159 | |
3dfbfbc8 | 160 | /* Convert decoder option and channel values to GVariant. */ |
2be182e6 BV |
161 | if (!opts_to_gvar(dec, pd_opthash, &options)) { |
162 | ret = 1; | |
163 | break; | |
164 | } | |
3dfbfbc8 | 165 | channels = extract_channel_map(dec, pd_opthash); |
39aedc34 | 166 | |
2be182e6 BV |
167 | if (g_hash_table_size(pd_opthash) > 0) { |
168 | leftover = g_hash_table_get_keys(pd_opthash); | |
169 | for (l = leftover; l; l = l->next) | |
3dfbfbc8 | 170 | g_critical("Unknown option or channel '%s'", (char *)l->data); |
2be182e6 BV |
171 | g_list_free(leftover); |
172 | break; | |
173 | } | |
174 | ||
175 | if (!(di = srd_inst_new(srd_sess, pd_name, options))) { | |
176 | g_critical("Failed to instantiate protocol decoder %s.", pd_name); | |
177 | ret = 1; | |
178 | break; | |
179 | } | |
180 | ||
09fea207 | 181 | if (pdtok == pdtokens) { |
3dfbfbc8 UH |
182 | /* |
183 | * Save the channel setup for later, but only on the | |
184 | * first decoder (stacked decoders don't get channels). | |
185 | */ | |
3dfbfbc8 UH |
186 | g_hash_table_insert(pd_channel_maps, g_strdup(di->inst_id), channels); |
187 | channels = NULL; | |
09fea207 | 188 | } |
39aedc34 | 189 | |
f0f54487 UH |
190 | /* |
191 | * If no annotation list was specified, add them all in now. | |
2be182e6 BV |
192 | * This will be pared down later to leave only the last PD |
193 | * in the stack. | |
194 | */ | |
551f570c | 195 | if (!opt_pd_annotations) { |
10d4fc25 | 196 | g_hash_table_insert(pd_ann_visible, g_strdup(di->decoder->id), |
790b0261 | 197 | g_slist_append(NULL, GINT_TO_POINTER(-1))); |
551f570c KP |
198 | } |
199 | if (di_prior) { | |
200 | if (srd_inst_stack(srd_sess, di_prior, di) != SRD_OK) { | |
201 | g_critical("Failed to stack %s -> %s.", | |
202 | di_prior->inst_id, di->inst_id); | |
203 | ret = 1; | |
204 | break; | |
205 | } | |
206 | /* Remove annotations from prior levels. */ | |
207 | if (!opt_pd_annotations) | |
208 | g_hash_table_remove(pd_ann_visible, di_prior->inst_id); | |
209 | } | |
210 | di_prior = di; | |
c5e0e72e KP |
211 | g_free(pd_name); |
212 | g_hash_table_destroy(pd_opthash); | |
213 | g_hash_table_destroy(options); | |
214 | pd_opthash = options = NULL; | |
2be182e6 BV |
215 | } |
216 | ||
2be182e6 BV |
217 | if (pd_opthash) |
218 | g_hash_table_destroy(pd_opthash); | |
219 | if (options) | |
220 | g_hash_table_destroy(options); | |
3dfbfbc8 UH |
221 | if (channels) |
222 | g_hash_table_destroy(channels); | |
551f570c KP |
223 | |
224 | g_strfreev(pdtokens); | |
2be182e6 BV |
225 | |
226 | return ret; | |
227 | } | |
228 | ||
551f570c KP |
229 | /* |
230 | * Register all the PDs from all stacks. | |
231 | * | |
232 | * Each PD string is a single stack such as "uart:baudrate=19200,modbus". | |
233 | */ | |
234 | int register_pds(gchar **all_pds, char *opt_pd_annotations) | |
235 | { | |
236 | int ret; | |
237 | ||
238 | ret = 0; | |
239 | pd_ann_visible = g_hash_table_new_full(g_str_hash, g_str_equal, | |
240 | g_free, NULL); | |
241 | pd_channel_maps = g_hash_table_new_full(g_str_hash, | |
242 | g_str_equal, g_free, (GDestroyNotify)g_hash_table_destroy); | |
243 | ||
244 | for (int i = 0; all_pds[i]; i++) | |
245 | ret += register_pd(all_pds[i], opt_pd_annotations); | |
246 | ||
247 | return ret; | |
248 | } | |
249 | ||
3dfbfbc8 | 250 | static void map_pd_inst_channels(void *key, void *value, void *user_data) |
39aedc34 | 251 | { |
3dfbfbc8 UH |
252 | GHashTable *channel_map; |
253 | GHashTable *channel_indices; | |
254 | GSList *channel_list; | |
39aedc34 DE |
255 | struct srd_decoder_inst *di; |
256 | GVariant *var; | |
3dfbfbc8 | 257 | void *channel_id; |
029d73fe UH |
258 | void *channel_target; |
259 | struct sr_channel *ch; | |
39aedc34 | 260 | GHashTableIter iter; |
39aedc34 | 261 | |
3dfbfbc8 UH |
262 | channel_map = value; |
263 | channel_list = user_data; | |
39aedc34 DE |
264 | |
265 | di = srd_inst_find_by_id(srd_sess, key); | |
266 | if (!di) { | |
267 | g_critical("Protocol decoder instance \"%s\" not found.", | |
268 | (char *)key); | |
269 | return; | |
270 | } | |
3dfbfbc8 | 271 | channel_indices = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, |
39aedc34 DE |
272 | (GDestroyNotify)g_variant_unref); |
273 | ||
3dfbfbc8 UH |
274 | g_hash_table_iter_init(&iter, channel_map); |
275 | while (g_hash_table_iter_next(&iter, &channel_id, &channel_target)) { | |
172d2b30 WS |
276 | if (!channel_target) { |
277 | g_printerr("cli: Channel name for \"%s\" missing.\n", | |
278 | (char *)channel_id); | |
279 | continue; | |
280 | } | |
3dfbfbc8 | 281 | ch = find_channel(channel_list, channel_target); |
029d73fe UH |
282 | if (!ch) { |
283 | g_printerr("cli: No channel with name \"%s\" found.\n", | |
284 | (char *)channel_target); | |
39aedc34 DE |
285 | continue; |
286 | } | |
029d73fe UH |
287 | if (!ch->enabled) |
288 | g_printerr("cli: Target channel \"%s\" not enabled.\n", | |
289 | (char *)channel_target); | |
39aedc34 | 290 | |
029d73fe | 291 | var = g_variant_new_int32(ch->index); |
39aedc34 | 292 | g_variant_ref_sink(var); |
3dfbfbc8 | 293 | g_hash_table_insert(channel_indices, g_strdup(channel_id), var); |
39aedc34 DE |
294 | } |
295 | ||
ee639fb4 | 296 | srd_inst_channel_set_all(di, channel_indices); |
c5e0e72e | 297 | g_hash_table_destroy(channel_indices); |
39aedc34 DE |
298 | } |
299 | ||
3dfbfbc8 | 300 | void map_pd_channels(struct sr_dev_inst *sdi) |
39aedc34 | 301 | { |
c6fa2b2e UH |
302 | GSList *channels; |
303 | ||
304 | channels = sr_dev_inst_channels_get(sdi); | |
305 | ||
3dfbfbc8 UH |
306 | if (pd_channel_maps) { |
307 | g_hash_table_foreach(pd_channel_maps, &map_pd_inst_channels, | |
c6fa2b2e | 308 | channels); |
3dfbfbc8 UH |
309 | g_hash_table_destroy(pd_channel_maps); |
310 | pd_channel_maps = NULL; | |
39aedc34 DE |
311 | } |
312 | } | |
313 | ||
26c63758 | 314 | int setup_pd_annotations(char *opt_pd_annotations) |
2be182e6 | 315 | { |
790b0261 | 316 | GSList *l, *l_ann; |
2be182e6 BV |
317 | struct srd_decoder *dec; |
318 | int ann_class; | |
790b0261 | 319 | char **pds, **pdtok, **keyval, **annlist, **ann, **ann_descr; |
d6ebfaa8 GS |
320 | const char *dec_id; |
321 | const char *ann_txt; | |
322 | const char *ann_id; | |
65cd80ad GS |
323 | const struct srd_decoder_annotation_row *row_desc; |
324 | char **ann_diag; | |
2be182e6 BV |
325 | |
326 | /* Set up custom list of PDs and annotations to show. */ | |
327 | pds = g_strsplit(opt_pd_annotations, ",", 0); | |
328 | for (pdtok = pds; *pdtok && **pdtok; pdtok++) { | |
329 | keyval = g_strsplit(*pdtok, "=", 0); | |
d6ebfaa8 GS |
330 | dec_id = keyval[0]; |
331 | if (!(dec = srd_decoder_get_by_id(dec_id))) { | |
332 | g_critical("Protocol decoder '%s' not found.", dec_id); | |
8f72edc3 GS |
333 | g_strfreev(keyval); |
334 | g_strfreev(pds); | |
2be182e6 BV |
335 | return 1; |
336 | } | |
337 | if (!dec->annotations) { | |
d6ebfaa8 | 338 | g_critical("Protocol decoder '%s' has no annotations.", dec_id); |
8f72edc3 GS |
339 | g_strfreev(keyval); |
340 | g_strfreev(pds); | |
2be182e6 BV |
341 | return 1; |
342 | } | |
d6ebfaa8 GS |
343 | ann_txt = (g_strv_length(keyval) == 2) ? keyval[1] : NULL; |
344 | if (ann_txt && *ann_txt) { | |
345 | annlist = g_strsplit(ann_txt, ":", 0); | |
790b0261 | 346 | for (ann = annlist; *ann && **ann; ann++) { |
d6ebfaa8 | 347 | ann_id = *ann; |
65cd80ad | 348 | g_debug("cli: Lookup decoder %s annotation %s.", dec_id, ann_id); |
4171a0a0 | 349 | /* Lookup annotation class. */ |
790b0261 BV |
350 | ann_class = 0; |
351 | for (l = dec->annotations; l; l = l->next, ann_class++) { | |
352 | ann_descr = l->data; | |
d6ebfaa8 | 353 | if (!canon_cmp(ann_descr[0], ann_id)) |
790b0261 BV |
354 | /* Found it. */ |
355 | break; | |
356 | } | |
4171a0a0 GS |
357 | if (l) { |
358 | l_ann = g_hash_table_lookup(pd_ann_visible, dec_id); | |
359 | l_ann = g_slist_append(l_ann, GINT_TO_POINTER(ann_class)); | |
360 | g_hash_table_replace(pd_ann_visible, g_strdup(dec_id), l_ann); | |
361 | g_debug("cli: Showing protocol decoder %s annotation " | |
362 | "class %d (%s).", dec_id, ann_class, ann_descr[0]); | |
363 | continue; | |
790b0261 | 364 | } |
65cd80ad GS |
365 | /* Lookup annotation row. */ |
366 | for (l = dec->annotation_rows; l; l = l->next) { | |
367 | row_desc = l->data; | |
368 | if (!canon_cmp(row_desc->id, ann_id)) | |
369 | break; | |
370 | } | |
371 | if (l) { | |
372 | g_debug("cli: Showing decoder %s annotation row %s (%s).", | |
373 | dec_id, row_desc->id, row_desc->desc); | |
374 | l_ann = g_hash_table_lookup(pd_ann_visible, dec_id); | |
375 | for (l = row_desc->ann_classes; l; l = l->next) { | |
376 | /* | |
377 | * This could just be: | |
378 | * l_ann = g_slist_append(l_ann, l->data); | |
379 | * But we are explicit for readability | |
380 | * and to access details for diagnostics. | |
381 | */ | |
382 | ann_class = GPOINTER_TO_INT(l->data); | |
383 | l_ann = g_slist_append(l_ann, GINT_TO_POINTER(ann_class)); | |
384 | ann_diag = g_slist_nth_data(dec->annotations, ann_class); | |
385 | g_debug("cli: Adding class %d/%s from row %s.", | |
386 | ann_class, ann_diag[0], row_desc->id); | |
387 | } | |
388 | g_hash_table_replace(pd_ann_visible, g_strdup(dec_id), l_ann); | |
389 | continue; | |
390 | } | |
4171a0a0 GS |
391 | /* No match found. */ |
392 | g_critical("Annotation '%s' not found " | |
393 | "for protocol decoder '%s'.", ann_id, dec_id); | |
394 | g_strfreev(keyval); | |
395 | g_strfreev(pds); | |
396 | return 1; | |
2be182e6 | 397 | } |
2be182e6 BV |
398 | } else { |
399 | /* No class specified: show all of them. */ | |
11e554c2 GS |
400 | ann_class = -1; |
401 | l_ann = g_slist_append(NULL, GINT_TO_POINTER(ann_class)); | |
402 | g_hash_table_insert(pd_ann_visible, g_strdup(dec_id), l_ann); | |
2be182e6 | 403 | g_debug("cli: Showing all annotation classes for protocol " |
d6ebfaa8 | 404 | "decoder %s.", dec_id); |
2be182e6 | 405 | } |
2be182e6 BV |
406 | g_strfreev(keyval); |
407 | } | |
408 | g_strfreev(pds); | |
409 | ||
410 | return 0; | |
411 | } | |
412 | ||
26c63758 | 413 | int setup_pd_meta(char *opt_pd_meta) |
2be182e6 BV |
414 | { |
415 | struct srd_decoder *dec; | |
416 | char **pds, **pdtok; | |
417 | ||
418 | pd_meta_visible = g_hash_table_new_full(g_str_hash, g_int_equal, | |
419 | g_free, NULL); | |
420 | pds = g_strsplit(opt_pd_meta, ",", 0); | |
421 | for (pdtok = pds; *pdtok && **pdtok; pdtok++) { | |
422 | if (!(dec = srd_decoder_get_by_id(*pdtok))) { | |
423 | g_critical("Protocol decoder '%s' not found.", *pdtok); | |
424 | return 1; | |
425 | } | |
426 | g_debug("cli: Showing protocol decoder meta output from '%s'.", *pdtok); | |
427 | g_hash_table_insert(pd_meta_visible, g_strdup(*pdtok), NULL); | |
428 | } | |
429 | g_strfreev(pds); | |
430 | ||
431 | return 0; | |
432 | } | |
433 | ||
26c63758 | 434 | int setup_pd_binary(char *opt_pd_binary) |
2be182e6 BV |
435 | { |
436 | GSList *l; | |
437 | struct srd_decoder *dec; | |
438 | int bin_class; | |
5ff52594 | 439 | char **pds, **pdtok, **keyval, **bin_name; |
2be182e6 BV |
440 | |
441 | pd_binary_visible = g_hash_table_new_full(g_str_hash, g_int_equal, | |
442 | g_free, NULL); | |
443 | pds = g_strsplit(opt_pd_binary, ",", 0); | |
444 | for (pdtok = pds; *pdtok && **pdtok; pdtok++) { | |
445 | keyval = g_strsplit(*pdtok, "=", 0); | |
446 | if (!(dec = srd_decoder_get_by_id(keyval[0]))) { | |
447 | g_critical("Protocol decoder '%s' not found.", keyval[0]); | |
448 | return 1; | |
449 | } | |
450 | if (!dec->binary) { | |
451 | g_critical("Protocol decoder '%s' has no binary output.", keyval[0]); | |
452 | return 1; | |
453 | } | |
454 | bin_class = 0; | |
455 | if (g_strv_length(keyval) == 2) { | |
456 | for (l = dec->binary; l; l = l->next, bin_class++) { | |
457 | bin_name = l->data; | |
5ff52594 | 458 | if (!strcmp(bin_name[0], keyval[1])) |
2be182e6 BV |
459 | /* Found it. */ |
460 | break; | |
461 | } | |
462 | if (!l) { | |
463 | g_critical("binary output '%s' not found " | |
464 | "for protocol decoder '%s'.", keyval[1], keyval[0]); | |
465 | return 1; | |
466 | } | |
467 | g_debug("cli: Showing protocol decoder %s binary class " | |
5ff52594 | 468 | "%d (%s).", keyval[0], bin_class, bin_name[0]); |
2be182e6 BV |
469 | } else { |
470 | /* No class specified: output all of them. */ | |
471 | bin_class = -1; | |
472 | g_debug("cli: Showing all binary classes for protocol " | |
473 | "decoder %s.", keyval[0]); | |
474 | } | |
475 | g_hash_table_insert(pd_binary_visible, g_strdup(keyval[0]), GINT_TO_POINTER(bin_class)); | |
476 | g_strfreev(keyval); | |
477 | } | |
478 | g_strfreev(pds); | |
479 | ||
480 | return 0; | |
481 | } | |
482 | ||
54614916 GS |
483 | /* |
484 | * Balance JSON object and array parentheses, and separate array items. | |
485 | * Somewhat convoluted API to re-use the routine for individual items as | |
486 | * well as the surrounding array and object, including deferred start of | |
487 | * the output and late flush (and to keep the state strictly local to the | |
488 | * routine). Some additional complexity due to JSON's inability to handle | |
489 | * a trailing comma at the last item. Code phrased such that text literals | |
118228e1 | 490 | * are kept in their order of appearance in the output (where possible). |
54614916 | 491 | */ |
118228e1 GS |
492 | static void jsontrace_open_close(gboolean is_close_req, |
493 | gboolean open_item, gboolean close_item) | |
54614916 | 494 | { |
118228e1 GS |
495 | static gboolean is_file_open; |
496 | static gboolean is_item_open; | |
54614916 | 497 | |
118228e1 GS |
498 | if (is_close_req && is_item_open) |
499 | close_item = TRUE; | |
500 | ||
501 | /* Automatic file header, and array item separation. */ | |
502 | if (open_item) { | |
503 | if (!is_file_open) | |
54614916 | 504 | printf("{\"traceEvents\": [\n"); |
118228e1 GS |
505 | if (is_item_open) { |
506 | printf("}"); | |
507 | is_item_open = FALSE; | |
54614916 | 508 | } |
118228e1 GS |
509 | if (is_file_open) { |
510 | printf(",\n"); | |
54614916 | 511 | } |
118228e1 GS |
512 | is_file_open = TRUE; |
513 | } | |
514 | ||
515 | /* Array item open/append/close. */ | |
516 | if (open_item) { | |
517 | printf("{"); | |
518 | is_item_open = TRUE; | |
54614916 | 519 | } |
118228e1 GS |
520 | if (!open_item && !close_item && !is_close_req) { |
521 | printf(", "); | |
522 | is_item_open = TRUE; | |
523 | } | |
524 | if (close_item) { | |
525 | printf("}"); | |
526 | is_item_open = FALSE; | |
527 | } | |
528 | ||
529 | /* Automatic file footer on shutdown. */ | |
530 | if (is_close_req && is_file_open) { | |
531 | printf("\n"); | |
532 | printf("]}\n"); | |
533 | } | |
534 | if (is_close_req) | |
535 | is_file_open = FALSE; | |
536 | ||
537 | /* Flush at end of lines, or end of file. */ | |
538 | if (close_item || is_close_req) | |
539 | fflush(stdout); | |
54614916 GS |
540 | } |
541 | ||
542 | /* Convert uint64 sample number to double timestamp in microseconds. */ | |
543 | static double jsontrace_ts_usec(uint64_t snum) | |
544 | { | |
545 | double ts_usec; | |
546 | ||
547 | ts_usec = snum; | |
548 | ts_usec *= 1e6; | |
549 | ts_usec /= pd_samplerate; | |
550 | return ts_usec; | |
551 | } | |
552 | ||
553 | /* Emit two Google Trace Events (JSON) for one PD annotation (ss, es). */ | |
554 | static void jsontrace_annotation(struct srd_decoder *dec, | |
555 | struct srd_proto_data_annotation *pda, struct srd_proto_data *pdata) | |
556 | { | |
557 | char *row_text; | |
558 | GSList *lrow, *lcls; | |
559 | struct srd_decoder_annotation_row *row; | |
560 | int cls; | |
561 | char **ann_descr; | |
562 | ||
563 | /* | |
564 | * Search for an annotation row for this index, or use the | |
565 | * annotation's descriptor. | |
566 | */ | |
567 | row_text = NULL; | |
568 | if (dec->annotation_rows) { | |
569 | for (lrow = dec->annotation_rows; lrow; lrow = lrow->next) { | |
570 | row = lrow->data; | |
571 | for (lcls = row->ann_classes; lcls; lcls = lcls->next) { | |
572 | cls = GPOINTER_TO_INT(lcls->data); | |
573 | if (cls == pda->ann_class) { | |
574 | row_text = row->desc; | |
575 | break; | |
576 | } | |
577 | } | |
578 | if (row_text) | |
579 | break; | |
580 | } | |
581 | } | |
582 | if (!row_text) { | |
583 | ann_descr = g_slist_nth_data(dec->annotations, pda->ann_class); | |
584 | row_text = ann_descr[0]; | |
585 | } | |
586 | ||
587 | /* | |
588 | * Emit two Google Trace Events for the start and end times. | |
589 | * Set the 'pid' (process ID) to the decoder name to group a | |
590 | * decoder's annotations. Set the 'tid' (thread ID) to the | |
591 | * annotation row's description. The 'ts' (timestamp) is in | |
592 | * microseconds. Set 'name' to the longest annotation text. | |
593 | * | |
a92fe14d GS |
594 | * BEWARE of the unfortunate JSON format limitation, which |
595 | * clutters data output calls with format helper calls. | |
118228e1 GS |
596 | * TODO Want to introduce a cJSON dependency to delegate the |
597 | * construction of output text? | |
54614916 | 598 | */ |
118228e1 | 599 | jsontrace_open_close(FALSE, TRUE, FALSE); |
118228e1 GS |
600 | printf("\"%s\": \"%s\"", "ph", "B"); |
601 | jsontrace_open_close(FALSE, FALSE, FALSE); | |
a92fe14d GS |
602 | printf("\"%s\": %lf", "ts", jsontrace_ts_usec(pdata->start_sample)); |
603 | jsontrace_open_close(FALSE, FALSE, FALSE); | |
118228e1 GS |
604 | printf("\"%s\": \"%s\"", "pid", pdata->pdo->proto_id); |
605 | jsontrace_open_close(FALSE, FALSE, FALSE); | |
606 | printf("\"%s\": \"%s\"", "tid", row_text); | |
607 | jsontrace_open_close(FALSE, FALSE, FALSE); | |
a92fe14d | 608 | printf("\"%s\": \"%s\"", "name", pda->ann_text[0]); |
118228e1 GS |
609 | |
610 | jsontrace_open_close(FALSE, TRUE, FALSE); | |
118228e1 GS |
611 | printf("\"%s\": \"%s\"", "ph", "E"); |
612 | jsontrace_open_close(FALSE, FALSE, FALSE); | |
a92fe14d GS |
613 | printf("\"%s\": %lf", "ts", jsontrace_ts_usec(pdata->end_sample)); |
614 | jsontrace_open_close(FALSE, FALSE, FALSE); | |
118228e1 GS |
615 | printf("\"%s\": \"%s\"", "pid", pdata->pdo->proto_id); |
616 | jsontrace_open_close(FALSE, FALSE, FALSE); | |
617 | printf("\"%s\": \"%s\"", "tid", row_text); | |
618 | jsontrace_open_close(FALSE, FALSE, FALSE); | |
a92fe14d | 619 | printf("\"%s\": \"%s\"", "name", pda->ann_text[0]); |
118228e1 GS |
620 | |
621 | jsontrace_open_close(FALSE, FALSE, TRUE); | |
54614916 GS |
622 | } |
623 | ||
2be182e6 BV |
624 | void show_pd_annotations(struct srd_proto_data *pdata, void *cb_data) |
625 | { | |
6fdcc67d | 626 | struct srd_decoder *dec; |
2be182e6 | 627 | struct srd_proto_data_annotation *pda; |
790b0261 BV |
628 | GSList *ann_list, *l; |
629 | int i; | |
6fdcc67d | 630 | char **ann_descr; |
241f9b13 | 631 | gboolean show_ann, show_snum, show_class, show_quotes, show_abbrev; |
e1a352d1 | 632 | const char *quote; |
2be182e6 | 633 | |
2be182e6 BV |
634 | (void)cb_data; |
635 | ||
636 | if (!pd_ann_visible) | |
637 | return; | |
638 | ||
10d4fc25 KP |
639 | if (!g_hash_table_lookup_extended(pd_ann_visible, pdata->pdo->di->decoder->id, |
640 | NULL, (void **)&ann_list)) { | |
2be182e6 BV |
641 | /* Not in the list of PDs whose annotations we're showing. */ |
642 | return; | |
10d4fc25 | 643 | } |
2be182e6 | 644 | |
6fdcc67d | 645 | dec = pdata->pdo->di->decoder; |
2be182e6 | 646 | pda = pdata->data; |
241f9b13 | 647 | show_ann = FALSE; |
790b0261 BV |
648 | for (l = ann_list; l; l = l->next) { |
649 | if (GPOINTER_TO_INT(l->data) == -1 | |
a23105b1 | 650 | || GPOINTER_TO_INT(l->data) == pda->ann_class) { |
241f9b13 | 651 | show_ann = TRUE; |
790b0261 BV |
652 | break; |
653 | } | |
654 | } | |
241f9b13 | 655 | if (!show_ann) |
2be182e6 BV |
656 | return; |
657 | ||
54614916 GS |
658 | /* Google Trace Events are rather special. Use a separate code path. */ |
659 | if (opt_pd_jsontrace) { | |
660 | jsontrace_annotation(dec, pda, pdata); | |
661 | return; | |
662 | } | |
663 | ||
241f9b13 | 664 | /* |
08e9378b GS |
665 | * Determine which fields of the annotation to display. Inspect |
666 | * user specified options as well as the verbosity of the log level: | |
241f9b13 GS |
667 | * - Optionally show the sample numbers for the annotation's span. |
668 | * - Always show the protocol decoder ID. | |
669 | * - Optionally show the annotation's class description. | |
670 | * - Always show the longest annotation text. | |
671 | * - Optionally show alternative annotation text (abbreviations | |
672 | * for different zoom levels). | |
673 | * - Optionally put quote marks around annotation text, when | |
674 | * recipients might have to deal with a set of text variants. | |
675 | */ | |
676 | show_snum = show_class = show_quotes = show_abbrev = FALSE; | |
08e9378b | 677 | if (opt_pd_samplenum || opt_loglevel > SR_LOG_WARN) { |
241f9b13 GS |
678 | show_snum = TRUE; |
679 | } | |
680 | if (opt_loglevel > SR_LOG_WARN) { | |
681 | show_quotes = TRUE; | |
682 | } | |
683 | if (opt_loglevel > SR_LOG_INFO) { | |
684 | show_class = TRUE; | |
685 | show_abbrev = TRUE; | |
686 | } | |
241f9b13 GS |
687 | |
688 | /* | |
689 | * Display the annotation's fields after the layout was | |
690 | * determined above. | |
691 | */ | |
e1a352d1 GS |
692 | if (show_snum) { |
693 | printf("%" PRIu64 "-%" PRIu64 " ", | |
694 | pdata->start_sample, pdata->end_sample); | |
695 | } | |
aa001713 | 696 | printf("%s: ", pdata->pdo->proto_id); |
e1a352d1 GS |
697 | if (show_class) { |
698 | ann_descr = g_slist_nth_data(dec->annotations, pda->ann_class); | |
699 | printf("%s: ", ann_descr[0]); | |
700 | } | |
701 | quote = show_quotes ? "\"" : ""; | |
702 | printf("%s%s%s", quote, pda->ann_text[0], quote); | |
703 | if (show_abbrev) { | |
704 | for (i = 1; pda->ann_text[i]; i++) | |
705 | printf(" %s%s%s", quote, pda->ann_text[i], quote); | |
6fdcc67d | 706 | } |
2be182e6 BV |
707 | printf("\n"); |
708 | fflush(stdout); | |
709 | } | |
710 | ||
711 | void show_pd_meta(struct srd_proto_data *pdata, void *cb_data) | |
712 | { | |
2be182e6 BV |
713 | (void)cb_data; |
714 | ||
715 | if (!g_hash_table_lookup_extended(pd_meta_visible, | |
716 | pdata->pdo->di->decoder->id, NULL, NULL)) | |
717 | /* Not in the list of PDs whose meta output we're showing. */ | |
718 | return; | |
719 | ||
08e9378b | 720 | if (opt_pd_samplenum || opt_loglevel > SR_LOG_WARN) |
2be182e6 BV |
721 | printf("%"PRIu64"-%"PRIu64" ", pdata->start_sample, pdata->end_sample); |
722 | printf("%s: ", pdata->pdo->proto_id); | |
723 | printf("%s: %s", pdata->pdo->meta_name, g_variant_print(pdata->data, FALSE)); | |
724 | printf("\n"); | |
725 | fflush(stdout); | |
726 | } | |
727 | ||
728 | void show_pd_binary(struct srd_proto_data *pdata, void *cb_data) | |
729 | { | |
730 | struct srd_proto_data_binary *pdb; | |
731 | gpointer classp; | |
4ae9434a | 732 | int classi; |
2be182e6 | 733 | |
2be182e6 BV |
734 | (void)cb_data; |
735 | ||
736 | if (!g_hash_table_lookup_extended(pd_binary_visible, | |
737 | pdata->pdo->di->decoder->id, NULL, (void **)&classp)) | |
738 | /* Not in the list of PDs whose meta output we're showing. */ | |
739 | return; | |
740 | ||
4ae9434a | 741 | classi = GPOINTER_TO_INT(classp); |
2be182e6 | 742 | pdb = pdata->data; |
4ae9434a | 743 | if (classi != -1 && classi != pdb->bin_class) |
2be182e6 BV |
744 | /* Not showing this binary class. */ |
745 | return; | |
746 | ||
747 | /* Just send the binary output to stdout, no embellishments. */ | |
748 | fwrite(pdb->data, pdb->size, 1, stdout); | |
749 | fflush(stdout); | |
750 | } | |
54614916 GS |
751 | |
752 | void show_pd_prepare(void) | |
753 | { | |
754 | if (opt_pd_jsontrace) | |
118228e1 | 755 | jsontrace_open_close(TRUE, FALSE, FALSE); |
54614916 GS |
756 | } |
757 | ||
758 | void show_pd_close(void) | |
759 | { | |
760 | if (opt_pd_jsontrace) | |
118228e1 | 761 | jsontrace_open_close(TRUE, FALSE, FALSE); |
54614916 | 762 | } |
2be182e6 | 763 | #endif |