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 | |
| 20 | #include "config.h" |
| 21 | #include <glib.h> |
| 22 | #include <libsigrok/libsigrok.h> |
| 23 | #ifdef HAVE_SRD |
| 24 | #include <libsigrokdecode/libsigrokdecode.h> /* First, so we avoid a _POSIX_C_SOURCE warning. */ |
| 25 | #endif |
| 26 | #include "sigrok-cli.h" |
| 27 | |
| 28 | #ifdef HAVE_SRD |
| 29 | static GHashTable *pd_ann_visible = NULL; |
| 30 | static GHashTable *pd_meta_visible = NULL; |
| 31 | static GHashTable *pd_binary_visible = NULL; |
| 32 | |
| 33 | extern struct srd_session *srd_sess; |
| 34 | extern gint opt_loglevel; |
Bert Vermeulen | 2be182e | 2013-11-17 13:15:38 +0100 | [diff] [blame] | 35 | |
| 36 | |
| 37 | static int opts_to_gvar(struct srd_decoder *dec, GHashTable *hash, |
| 38 | GHashTable **options) |
| 39 | { |
| 40 | struct srd_decoder_option *o; |
| 41 | GSList *optl; |
| 42 | GVariant *gvar; |
| 43 | gint64 val_int; |
| 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)) { |
| 59 | val_int = strtoll(val_str, &conv, 0); |
| 60 | if (!conv || conv == val_str) { |
| 61 | g_critical("Protocol decoder '%s' option '%s' " |
| 62 | "requires a number.", dec->name, o->id); |
| 63 | ret = FALSE; |
| 64 | break; |
| 65 | } |
| 66 | gvar = g_variant_new_int64(val_int); |
| 67 | } else { |
| 68 | g_critical("Unsupported type for option '%s' (%s)", |
| 69 | o->id, g_variant_get_type_string(o->def)); |
| 70 | ret = FALSE; |
| 71 | break; |
| 72 | } |
| 73 | g_variant_ref_sink(gvar); |
| 74 | g_hash_table_insert(*options, g_strdup(o->id), gvar); |
| 75 | g_hash_table_remove(hash, o->id); |
| 76 | } |
| 77 | |
| 78 | return ret; |
| 79 | } |
| 80 | |
| 81 | static int probes_to_gvar(struct srd_decoder *dec, GHashTable *hash, |
| 82 | GHashTable **probes) |
| 83 | { |
| 84 | struct srd_probe *p; |
| 85 | GSList *all_probes, *l; |
| 86 | GVariant *gvar; |
| 87 | gint32 val_int; |
| 88 | int ret; |
| 89 | char *val_str, *conv; |
| 90 | |
| 91 | ret = TRUE; |
| 92 | *probes = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, |
| 93 | (GDestroyNotify)g_variant_unref); |
| 94 | |
| 95 | all_probes = g_slist_copy(dec->probes); |
| 96 | all_probes = g_slist_concat(all_probes, g_slist_copy(dec->opt_probes)); |
| 97 | for (l = all_probes; l; l = l->next) { |
| 98 | p = l->data; |
| 99 | if (!(val_str = g_hash_table_lookup(hash, p->id))) |
| 100 | /* Not specified. */ |
| 101 | continue; |
| 102 | val_int = strtoll(val_str, &conv, 10); |
| 103 | if (!conv || conv == val_str) { |
| 104 | g_critical("Protocol decoder '%s' probes '%s' " |
| 105 | "is not a number.", dec->name, p->id); |
| 106 | ret = FALSE; |
| 107 | break; |
| 108 | } |
| 109 | gvar = g_variant_new_int32(val_int); |
| 110 | g_variant_ref_sink(gvar); |
| 111 | g_hash_table_insert(*probes, g_strdup(p->id), gvar); |
| 112 | g_hash_table_remove(hash, p->id); |
| 113 | } |
| 114 | g_slist_free(all_probes); |
| 115 | |
| 116 | return ret; |
| 117 | } |
| 118 | |
| 119 | /* Register the given PDs for this session. |
| 120 | * Accepts a string of the form: "spi:sck=3:sdata=4,spi:sck=3:sdata=5" |
| 121 | * That will instantiate two SPI decoders on the clock but different data |
| 122 | * lines. |
| 123 | */ |
Bert Vermeulen | 26c6375 | 2013-11-17 18:51:15 +0100 | [diff] [blame^] | 124 | int register_pds(const char *opt_pds, char *opt_pd_annotations) |
Bert Vermeulen | 2be182e | 2013-11-17 13:15:38 +0100 | [diff] [blame] | 125 | { |
| 126 | struct srd_decoder *dec; |
| 127 | GHashTable *pd_opthash, *options, *probes; |
| 128 | GList *leftover, *l; |
| 129 | struct srd_decoder_inst *di; |
| 130 | int ret; |
| 131 | char **pdtokens, **pdtok, *pd_name; |
| 132 | |
| 133 | pd_ann_visible = g_hash_table_new_full(g_str_hash, g_int_equal, |
| 134 | g_free, NULL); |
| 135 | ret = 0; |
| 136 | pd_name = NULL; |
| 137 | pd_opthash = options = probes = NULL; |
Bert Vermeulen | 26c6375 | 2013-11-17 18:51:15 +0100 | [diff] [blame^] | 138 | pdtokens = g_strsplit(opt_pds, ",", 0); |
Bert Vermeulen | 2be182e | 2013-11-17 13:15:38 +0100 | [diff] [blame] | 139 | for (pdtok = pdtokens; *pdtok; pdtok++) { |
| 140 | if (!(pd_opthash = parse_generic_arg(*pdtok, TRUE))) { |
| 141 | g_critical("Invalid protocol decoder option '%s'.", *pdtok); |
| 142 | break; |
| 143 | } |
| 144 | |
| 145 | pd_name = g_strdup(g_hash_table_lookup(pd_opthash, "sigrok_key")); |
| 146 | g_hash_table_remove(pd_opthash, "sigrok_key"); |
| 147 | if (srd_decoder_load(pd_name) != SRD_OK) { |
| 148 | g_critical("Failed to load protocol decoder %s.", pd_name); |
| 149 | ret = 1; |
| 150 | break; |
| 151 | } |
| 152 | dec = srd_decoder_get_by_id(pd_name); |
| 153 | |
| 154 | /* Convert decoder option and probe values to GVariant. */ |
| 155 | if (!opts_to_gvar(dec, pd_opthash, &options)) { |
| 156 | ret = 1; |
| 157 | break; |
| 158 | } |
| 159 | if (!probes_to_gvar(dec, pd_opthash, &probes)) { |
| 160 | ret = 1; |
| 161 | break; |
| 162 | } |
| 163 | if (g_hash_table_size(pd_opthash) > 0) { |
| 164 | leftover = g_hash_table_get_keys(pd_opthash); |
| 165 | for (l = leftover; l; l = l->next) |
| 166 | g_critical("Unknown option or probe '%s'", (char *)l->data); |
| 167 | g_list_free(leftover); |
| 168 | break; |
| 169 | } |
| 170 | |
| 171 | if (!(di = srd_inst_new(srd_sess, pd_name, options))) { |
| 172 | g_critical("Failed to instantiate protocol decoder %s.", pd_name); |
| 173 | ret = 1; |
| 174 | break; |
| 175 | } |
| 176 | |
| 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) |
| 182 | g_hash_table_insert(pd_ann_visible, |
| 183 | g_strdup(di->inst_id), GINT_TO_POINTER(-1)); |
| 184 | |
| 185 | /* Remap the probes if needed. */ |
| 186 | if (srd_inst_probe_set_all(di, probes) != SRD_OK) { |
| 187 | ret = 1; |
| 188 | break; |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | g_strfreev(pdtokens); |
| 193 | if (pd_opthash) |
| 194 | g_hash_table_destroy(pd_opthash); |
| 195 | if (options) |
| 196 | g_hash_table_destroy(options); |
| 197 | if (probes) |
| 198 | g_hash_table_destroy(probes); |
| 199 | if (pd_name) |
| 200 | g_free(pd_name); |
| 201 | |
| 202 | return ret; |
| 203 | } |
| 204 | |
Bert Vermeulen | 26c6375 | 2013-11-17 18:51:15 +0100 | [diff] [blame^] | 205 | 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] | 206 | { |
| 207 | struct srd_decoder_inst *di_from, *di_to; |
| 208 | int ret, i; |
| 209 | char **pds, **ids; |
| 210 | |
| 211 | /* Set up the protocol decoder stack. */ |
| 212 | pds = g_strsplit(opt_pds, ",", 0); |
| 213 | if (g_strv_length(pds) > 1) { |
| 214 | if (opt_pd_stack) { |
| 215 | /* A stack setup was specified, use that. */ |
| 216 | g_strfreev(pds); |
| 217 | pds = g_strsplit(opt_pd_stack, ",", 0); |
| 218 | if (g_strv_length(pds) < 2) { |
| 219 | g_strfreev(pds); |
| 220 | g_critical("Specify at least two protocol decoders to stack."); |
| 221 | return 1; |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | /* First PD goes at the bottom of the stack. */ |
| 226 | ids = g_strsplit(pds[0], ":", 0); |
| 227 | if (!(di_from = srd_inst_find_by_id(srd_sess, ids[0]))) { |
| 228 | g_strfreev(ids); |
| 229 | g_critical("Cannot stack protocol decoder '%s': " |
| 230 | "instance not found.", pds[0]); |
| 231 | return 1; |
| 232 | } |
| 233 | g_strfreev(ids); |
| 234 | |
| 235 | /* Every subsequent PD goes on top. */ |
| 236 | for (i = 1; pds[i]; i++) { |
| 237 | ids = g_strsplit(pds[i], ":", 0); |
| 238 | if (!(di_to = srd_inst_find_by_id(srd_sess, ids[0]))) { |
| 239 | g_strfreev(ids); |
| 240 | g_critical("Cannot stack protocol decoder '%s': " |
| 241 | "instance not found.", pds[i]); |
| 242 | return 1; |
| 243 | } |
| 244 | g_strfreev(ids); |
| 245 | if ((ret = srd_inst_stack(srd_sess, di_from, di_to)) != SRD_OK) |
| 246 | return 1; |
| 247 | |
| 248 | /* Don't show annotation from this PD. Only the last PD in |
| 249 | * the stack will be left on the annotation list (unless |
| 250 | * the annotation list was specifically provided). |
| 251 | */ |
| 252 | if (!opt_pd_annotations) |
| 253 | g_hash_table_remove(pd_ann_visible, |
| 254 | di_from->inst_id); |
| 255 | |
| 256 | di_from = di_to; |
| 257 | } |
| 258 | } |
| 259 | g_strfreev(pds); |
| 260 | |
| 261 | return 0; |
| 262 | } |
| 263 | |
Bert Vermeulen | 26c6375 | 2013-11-17 18:51:15 +0100 | [diff] [blame^] | 264 | int setup_pd_annotations(char *opt_pd_annotations) |
Bert Vermeulen | 2be182e | 2013-11-17 13:15:38 +0100 | [diff] [blame] | 265 | { |
| 266 | GSList *l; |
| 267 | struct srd_decoder *dec; |
| 268 | int ann_class; |
| 269 | char **pds, **pdtok, **keyval, **ann_descr; |
| 270 | |
| 271 | /* Set up custom list of PDs and annotations to show. */ |
| 272 | pds = g_strsplit(opt_pd_annotations, ",", 0); |
| 273 | for (pdtok = pds; *pdtok && **pdtok; pdtok++) { |
| 274 | keyval = g_strsplit(*pdtok, "=", 0); |
| 275 | if (!(dec = srd_decoder_get_by_id(keyval[0]))) { |
| 276 | g_critical("Protocol decoder '%s' not found.", keyval[0]); |
| 277 | return 1; |
| 278 | } |
| 279 | if (!dec->annotations) { |
| 280 | g_critical("Protocol decoder '%s' has no annotations.", keyval[0]); |
| 281 | return 1; |
| 282 | } |
| 283 | ann_class = 0; |
| 284 | if (g_strv_length(keyval) == 2) { |
| 285 | for (l = dec->annotations; l; l = l->next, ann_class++) { |
| 286 | ann_descr = l->data; |
| 287 | if (!canon_cmp(ann_descr[0], keyval[1])) |
| 288 | /* Found it. */ |
| 289 | break; |
| 290 | } |
| 291 | if (!l) { |
| 292 | g_critical("Annotation '%s' not found " |
| 293 | "for protocol decoder '%s'.", keyval[1], keyval[0]); |
| 294 | return 1; |
| 295 | } |
| 296 | g_debug("cli: Showing protocol decoder %s annotation " |
| 297 | "class %d (%s).", keyval[0], ann_class, ann_descr[0]); |
| 298 | } else { |
| 299 | /* No class specified: show all of them. */ |
| 300 | ann_class = -1; |
| 301 | g_debug("cli: Showing all annotation classes for protocol " |
| 302 | "decoder %s.", keyval[0]); |
| 303 | } |
| 304 | g_hash_table_insert(pd_ann_visible, g_strdup(keyval[0]), GINT_TO_POINTER(ann_class)); |
| 305 | g_strfreev(keyval); |
| 306 | } |
| 307 | g_strfreev(pds); |
| 308 | |
| 309 | return 0; |
| 310 | } |
| 311 | |
Bert Vermeulen | 26c6375 | 2013-11-17 18:51:15 +0100 | [diff] [blame^] | 312 | int setup_pd_meta(char *opt_pd_meta) |
Bert Vermeulen | 2be182e | 2013-11-17 13:15:38 +0100 | [diff] [blame] | 313 | { |
| 314 | struct srd_decoder *dec; |
| 315 | char **pds, **pdtok; |
| 316 | |
| 317 | pd_meta_visible = g_hash_table_new_full(g_str_hash, g_int_equal, |
| 318 | g_free, NULL); |
| 319 | pds = g_strsplit(opt_pd_meta, ",", 0); |
| 320 | for (pdtok = pds; *pdtok && **pdtok; pdtok++) { |
| 321 | if (!(dec = srd_decoder_get_by_id(*pdtok))) { |
| 322 | g_critical("Protocol decoder '%s' not found.", *pdtok); |
| 323 | return 1; |
| 324 | } |
| 325 | g_debug("cli: Showing protocol decoder meta output from '%s'.", *pdtok); |
| 326 | g_hash_table_insert(pd_meta_visible, g_strdup(*pdtok), NULL); |
| 327 | } |
| 328 | g_strfreev(pds); |
| 329 | |
| 330 | return 0; |
| 331 | } |
| 332 | |
Bert Vermeulen | 26c6375 | 2013-11-17 18:51:15 +0100 | [diff] [blame^] | 333 | int setup_pd_binary(char *opt_pd_binary) |
Bert Vermeulen | 2be182e | 2013-11-17 13:15:38 +0100 | [diff] [blame] | 334 | { |
| 335 | GSList *l; |
| 336 | struct srd_decoder *dec; |
| 337 | int bin_class; |
| 338 | char **pds, **pdtok, **keyval, *bin_name; |
| 339 | |
| 340 | pd_binary_visible = g_hash_table_new_full(g_str_hash, g_int_equal, |
| 341 | g_free, NULL); |
| 342 | pds = g_strsplit(opt_pd_binary, ",", 0); |
| 343 | for (pdtok = pds; *pdtok && **pdtok; pdtok++) { |
| 344 | keyval = g_strsplit(*pdtok, "=", 0); |
| 345 | if (!(dec = srd_decoder_get_by_id(keyval[0]))) { |
| 346 | g_critical("Protocol decoder '%s' not found.", keyval[0]); |
| 347 | return 1; |
| 348 | } |
| 349 | if (!dec->binary) { |
| 350 | g_critical("Protocol decoder '%s' has no binary output.", keyval[0]); |
| 351 | return 1; |
| 352 | } |
| 353 | bin_class = 0; |
| 354 | if (g_strv_length(keyval) == 2) { |
| 355 | for (l = dec->binary; l; l = l->next, bin_class++) { |
| 356 | bin_name = l->data; |
| 357 | if (!canon_cmp(bin_name, keyval[1])) |
| 358 | /* Found it. */ |
| 359 | break; |
| 360 | } |
| 361 | if (!l) { |
| 362 | g_critical("binary output '%s' not found " |
| 363 | "for protocol decoder '%s'.", keyval[1], keyval[0]); |
| 364 | return 1; |
| 365 | } |
| 366 | g_debug("cli: Showing protocol decoder %s binary class " |
| 367 | "%d (%s).", keyval[0], bin_class, bin_name); |
| 368 | } else { |
| 369 | /* No class specified: output all of them. */ |
| 370 | bin_class = -1; |
| 371 | g_debug("cli: Showing all binary classes for protocol " |
| 372 | "decoder %s.", keyval[0]); |
| 373 | } |
| 374 | g_hash_table_insert(pd_binary_visible, g_strdup(keyval[0]), GINT_TO_POINTER(bin_class)); |
| 375 | g_strfreev(keyval); |
| 376 | } |
| 377 | g_strfreev(pds); |
| 378 | |
| 379 | return 0; |
| 380 | } |
| 381 | |
| 382 | void show_pd_annotations(struct srd_proto_data *pdata, void *cb_data) |
| 383 | { |
| 384 | struct srd_proto_data_annotation *pda; |
| 385 | gpointer ann_format; |
| 386 | int format; |
| 387 | |
| 388 | /* 'cb_data' is not used in this specific callback. */ |
| 389 | (void)cb_data; |
| 390 | |
| 391 | if (!pd_ann_visible) |
| 392 | return; |
| 393 | |
| 394 | if (!g_hash_table_lookup_extended(pd_ann_visible, pdata->pdo->di->inst_id, |
| 395 | NULL, &ann_format)) |
| 396 | /* Not in the list of PDs whose annotations we're showing. */ |
| 397 | return; |
| 398 | |
| 399 | format = GPOINTER_TO_INT(ann_format); |
| 400 | pda = pdata->data; |
| 401 | if (format != -1 && pda->ann_format != format) |
| 402 | /* We don't want this particular format from the PD. */ |
| 403 | return; |
| 404 | |
| 405 | if (opt_loglevel > SR_LOG_WARN) |
| 406 | printf("%"PRIu64"-%"PRIu64" ", pdata->start_sample, pdata->end_sample); |
| 407 | printf("%s: ", pdata->pdo->proto_id); |
| 408 | /* Show only the longest annotation. */ |
| 409 | printf("\"%s\" ", pda->ann_text[0]); |
| 410 | printf("\n"); |
| 411 | fflush(stdout); |
| 412 | } |
| 413 | |
| 414 | void show_pd_meta(struct srd_proto_data *pdata, void *cb_data) |
| 415 | { |
| 416 | |
| 417 | /* 'cb_data' is not used in this specific callback. */ |
| 418 | (void)cb_data; |
| 419 | |
| 420 | if (!g_hash_table_lookup_extended(pd_meta_visible, |
| 421 | pdata->pdo->di->decoder->id, NULL, NULL)) |
| 422 | /* Not in the list of PDs whose meta output we're showing. */ |
| 423 | return; |
| 424 | |
| 425 | if (opt_loglevel > SR_LOG_WARN) |
| 426 | printf("%"PRIu64"-%"PRIu64" ", pdata->start_sample, pdata->end_sample); |
| 427 | printf("%s: ", pdata->pdo->proto_id); |
| 428 | printf("%s: %s", pdata->pdo->meta_name, g_variant_print(pdata->data, FALSE)); |
| 429 | printf("\n"); |
| 430 | fflush(stdout); |
| 431 | } |
| 432 | |
| 433 | void show_pd_binary(struct srd_proto_data *pdata, void *cb_data) |
| 434 | { |
| 435 | struct srd_proto_data_binary *pdb; |
| 436 | gpointer classp; |
| 437 | int class; |
| 438 | |
| 439 | /* 'cb_data' is not used in this specific callback. */ |
| 440 | (void)cb_data; |
| 441 | |
| 442 | if (!g_hash_table_lookup_extended(pd_binary_visible, |
| 443 | pdata->pdo->di->decoder->id, NULL, (void **)&classp)) |
| 444 | /* Not in the list of PDs whose meta output we're showing. */ |
| 445 | return; |
| 446 | |
| 447 | class = GPOINTER_TO_INT(classp); |
| 448 | pdb = pdata->data; |
| 449 | if (class != -1 && class != pdb->bin_class) |
| 450 | /* Not showing this binary class. */ |
| 451 | return; |
| 452 | |
| 453 | /* Just send the binary output to stdout, no embellishments. */ |
| 454 | fwrite(pdb->data, pdb->size, 1, stdout); |
| 455 | fflush(stdout); |
| 456 | } |
| 457 | #endif |
| 458 | |