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