]> sigrok.org Git - libsigrokdecode.git/blame - decoder.c
Clarify and improve channel map debug output.
[libsigrokdecode.git] / decoder.c
CommitLineData
b2c19614 1/*
50bd5d25 2 * This file is part of the libsigrokdecode project.
b2c19614
BV
3 *
4 * Copyright (C) 2010 Uwe Hermann <uwe@hermann-uwe.de>
4fadb128 5 * Copyright (C) 2012 Bert Vermeulen <bert@biot.com>
b2c19614
BV
6 *
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
36784362 21#include <config.h>
f6c7eade
MC
22#include "libsigrokdecode-internal.h" /* First, so we avoid a _POSIX_C_SOURCE warning. */
23#include "libsigrokdecode.h"
eb2bbd66 24#include <glib.h>
b2c19614 25
54fdeeef
UH
26/**
27 * @file
28 *
29 * Listing, loading, unloading, and handling protocol decoders.
30 */
31
4895418c
UH
32/**
33 * @defgroup grp_decoder Protocol decoders
34 *
35 * Handling protocol decoders.
36 *
37 * @{
38 */
39
57790bc8
UH
40/** @cond PRIVATE */
41
c63f5a87 42/* The list of loaded protocol decoders. */
2060510a 43static GSList *pd_list = NULL;
b2c19614 44
ea81b49a 45/* srd.c */
23a29d24 46extern SRD_PRIV GSList *searchpaths;
ea81b49a
BV
47
48/* session.c */
23a29d24
UH
49extern SRD_PRIV GSList *sessions;
50extern SRD_PRIV int max_session_id;
32cfb920 51
c9bfccc6 52/* module_sigrokdecode.c */
55c3c5f4 53extern SRD_PRIV PyObject *mod_sigrokdecode;
451680f1 54
57790bc8
UH
55/** @endcond */
56
f6527cc4
UH
57static gboolean srd_check_init(void)
58{
59 if (max_session_id < 0) {
60 srd_err("Library is not initialized.");
61 return FALSE;
62 } else
63 return TRUE;
64}
65
b2c19614 66/**
c63f5a87 67 * Returns the list of loaded protocol decoders.
b2c19614 68 *
3a1501e9 69 * This is a GSList of pointers to struct srd_decoder items.
b2c19614
BV
70 *
71 * @return List of decoders, NULL if none are supported or loaded.
8c664ca2 72 *
c57d1013 73 * @since 0.2.0
b2c19614 74 */
38ff5046 75SRD_API const GSList *srd_decoder_list(void)
b2c19614 76{
e5080882 77 return pd_list;
b2c19614
BV
78}
79
b2c19614
BV
80/**
81 * Get the decoder with the specified ID.
82 *
83 * @param id The ID string of the decoder to return.
582c8473 84 *
b2c19614 85 * @return The decoder with the specified ID, or NULL if not found.
8c664ca2
UH
86 *
87 * @since 0.1.0
b2c19614 88 */
9d122fd5 89SRD_API struct srd_decoder *srd_decoder_get_by_id(const char *id)
b2c19614
BV
90{
91 GSList *l;
92 struct srd_decoder *dec;
93
38ff5046 94 for (l = pd_list; l; l = l->next) {
b2c19614
BV
95 dec = l->data;
96 if (!strcmp(dec->id, id))
97 return dec;
98 }
99
100 return NULL;
101}
102
6d67d057
DE
103static void channel_free(void *data)
104{
105 struct srd_channel *ch = data;
106
107 if (!ch)
108 return;
109
110 g_free(ch->desc);
111 g_free(ch->name);
112 g_free(ch->id);
113 g_free(ch);
114}
115
116static void variant_free(void *data)
117{
118 GVariant *var = data;
119
120 if (!var)
121 return;
122
123 g_variant_unref(var);
124}
125
126static void annotation_row_free(void *data)
127{
128 struct srd_decoder_annotation_row *row = data;
129
130 if (!row)
131 return;
132
133 g_slist_free(row->ann_classes);
134 g_free(row->desc);
135 g_free(row->id);
136 g_free(row);
137}
138
139static void decoder_option_free(void *data)
140{
141 struct srd_decoder_option *opt = data;
142
143 if (!opt)
144 return;
145
146 g_slist_free_full(opt->values, &variant_free);
147 variant_free(opt->def);
148 g_free(opt->desc);
149 g_free(opt->id);
150 g_free(opt);
151}
152
153static void decoder_free(struct srd_decoder *dec)
154{
514b2edc
UH
155 PyGILState_STATE gstate;
156
6d67d057
DE
157 if (!dec)
158 return;
159
514b2edc 160 gstate = PyGILState_Ensure();
6d67d057
DE
161 Py_XDECREF(dec->py_dec);
162 Py_XDECREF(dec->py_mod);
514b2edc 163 PyGILState_Release(gstate);
6d67d057
DE
164
165 g_slist_free_full(dec->options, &decoder_option_free);
166 g_slist_free_full(dec->binary, (GDestroyNotify)&g_strfreev);
167 g_slist_free_full(dec->annotation_rows, &annotation_row_free);
168 g_slist_free_full(dec->annotations, (GDestroyNotify)&g_strfreev);
169 g_slist_free_full(dec->opt_channels, &channel_free);
170 g_slist_free_full(dec->channels, &channel_free);
171
d480174d
UH
172 g_slist_free_full(dec->outputs, g_free);
173 g_slist_free_full(dec->inputs, g_free);
6d67d057
DE
174 g_free(dec->license);
175 g_free(dec->desc);
176 g_free(dec->longname);
177 g_free(dec->name);
178 g_free(dec->id);
179
180 g_free(dec);
181}
182
6a15597a 183static int get_channels(const struct srd_decoder *d, const char *attr,
6d67d057 184 GSList **out_pdchl, int offset)
f38ec285 185{
6a15597a
UH
186 PyObject *py_channellist, *py_entry;
187 struct srd_channel *pdch;
6d67d057
DE
188 GSList *pdchl;
189 ssize_t i;
514b2edc
UH
190 PyGILState_STATE gstate;
191
192 gstate = PyGILState_Ensure();
f38ec285 193
514b2edc 194 if (!PyObject_HasAttrString(d->py_dec, attr)) {
6a15597a 195 /* No channels of this type specified. */
514b2edc 196 PyGILState_Release(gstate);
f38ec285 197 return SRD_OK;
514b2edc 198 }
f38ec285 199
6d67d057
DE
200 pdchl = NULL;
201
6a15597a 202 py_channellist = PyObject_GetAttrString(d->py_dec, attr);
6d67d057
DE
203 if (!py_channellist)
204 goto except_out;
205
6a15597a 206 if (!PyTuple_Check(py_channellist)) {
da9bcbd9 207 srd_err("Protocol decoder %s %s attribute is not a tuple.",
6d67d057
DE
208 d->name, attr);
209 goto err_out;
f38ec285
BV
210 }
211
6d67d057 212 for (i = PyTuple_Size(py_channellist) - 1; i >= 0; i--) {
6a15597a 213 py_entry = PyTuple_GetItem(py_channellist, i);
6d67d057
DE
214 if (!py_entry)
215 goto except_out;
216
f38ec285
BV
217 if (!PyDict_Check(py_entry)) {
218 srd_err("Protocol decoder %s %s attribute is not "
6d67d057
DE
219 "a list of dict elements.", d->name, attr);
220 goto err_out;
f38ec285 221 }
6d67d057
DE
222 pdch = g_malloc0(sizeof(struct srd_channel));
223 /* Add to list right away so it doesn't get lost. */
224 pdchl = g_slist_prepend(pdchl, pdch);
f38ec285 225
6d67d057
DE
226 if (py_dictitem_as_str(py_entry, "id", &pdch->id) != SRD_OK)
227 goto err_out;
228 if (py_dictitem_as_str(py_entry, "name", &pdch->name) != SRD_OK)
229 goto err_out;
230 if (py_dictitem_as_str(py_entry, "desc", &pdch->desc) != SRD_OK)
231 goto err_out;
f38ec285 232
6d67d057 233 pdch->order = offset + i;
f38ec285 234 }
f38ec285 235
6d67d057
DE
236 Py_DECREF(py_channellist);
237 *out_pdchl = pdchl;
238
514b2edc
UH
239 PyGILState_Release(gstate);
240
6d67d057 241 return SRD_OK;
f38ec285 242
6d67d057
DE
243except_out:
244 srd_exception_catch("Failed to get %s list of %s decoder",
245 attr, d->name);
246err_out:
247 g_slist_free_full(pdchl, &channel_free);
248 Py_XDECREF(py_channellist);
514b2edc 249 PyGILState_Release(gstate);
6d67d057
DE
250
251 return SRD_ERR_PYTHON;
f38ec285
BV
252}
253
2f395bff
BV
254static int get_options(struct srd_decoder *d)
255{
6d67d057
DE
256 PyObject *py_opts, *py_opt, *py_str, *py_values, *py_default, *py_item;
257 GSList *options;
2f395bff 258 struct srd_decoder_option *o;
84c1c0b5 259 GVariant *gvar;
6d67d057 260 ssize_t opt, i;
514b2edc 261 PyGILState_STATE gstate;
2f395bff 262
514b2edc
UH
263 gstate = PyGILState_Ensure();
264
265 if (!PyObject_HasAttrString(d->py_dec, "options")) {
84c1c0b5 266 /* No options, that's fine. */
514b2edc 267 PyGILState_Release(gstate);
2f395bff 268 return SRD_OK;
514b2edc 269 }
2f395bff 270
6d67d057
DE
271 options = NULL;
272
84c1c0b5 273 /* If present, options must be a tuple. */
2f395bff 274 py_opts = PyObject_GetAttrString(d->py_dec, "options");
6d67d057
DE
275 if (!py_opts)
276 goto except_out;
277
84c1c0b5
BV
278 if (!PyTuple_Check(py_opts)) {
279 srd_err("Protocol decoder %s: options attribute is not "
280 "a tuple.", d->id);
6d67d057 281 goto err_out;
2f395bff
BV
282 }
283
6d67d057 284 for (opt = PyTuple_Size(py_opts) - 1; opt >= 0; opt--) {
84c1c0b5 285 py_opt = PyTuple_GetItem(py_opts, opt);
6d67d057
DE
286 if (!py_opt)
287 goto except_out;
288
84c1c0b5
BV
289 if (!PyDict_Check(py_opt)) {
290 srd_err("Protocol decoder %s options: each option "
291 "must consist of a dictionary.", d->name);
6d67d057 292 goto err_out;
2f395bff 293 }
6d67d057 294
84c1c0b5 295 o = g_malloc0(sizeof(struct srd_decoder_option));
6d67d057
DE
296 /* Add to list right away so it doesn't get lost. */
297 options = g_slist_prepend(options, o);
298
299 py_str = PyDict_GetItemString(py_opt, "id");
300 if (!py_str) {
301 srd_err("Protocol decoder %s option %zd has no id.",
302 d->name, opt);
303 goto err_out;
304 }
305 if (py_str_as_str(py_str, &o->id) != SRD_OK)
306 goto err_out;
307
308 py_str = PyDict_GetItemString(py_opt, "desc");
309 if (py_str) {
310 if (py_str_as_str(py_str, &o->desc) != SRD_OK)
311 goto err_out;
312 }
313
314 py_default = PyDict_GetItemString(py_opt, "default");
315 if (py_default) {
316 gvar = py_obj_to_variant(py_default);
317 if (!gvar) {
84c1c0b5 318 srd_err("Protocol decoder %s option 'default' has "
6d67d057
DE
319 "invalid default value.", d->name);
320 goto err_out;
84c1c0b5 321 }
6d67d057 322 o->def = g_variant_ref_sink(gvar);
2f395bff 323 }
84c1c0b5 324
6d67d057
DE
325 py_values = PyDict_GetItemString(py_opt, "values");
326 if (py_values) {
84c1c0b5
BV
327 /* A default is required if a list of values is
328 * given, since it's used to verify their type. */
329 if (!o->def) {
6d67d057
DE
330 srd_err("No default for option '%s'.", o->id);
331 goto err_out;
84c1c0b5 332 }
6d67d057 333 if (!PyTuple_Check(py_values)) {
84c1c0b5 334 srd_err("Option '%s' values should be a tuple.", o->id);
6d67d057 335 goto err_out;
84c1c0b5 336 }
6d67d057
DE
337
338 for (i = PyTuple_Size(py_values) - 1; i >= 0; i--) {
339 py_item = PyTuple_GetItem(py_values, i);
340 if (!py_item)
341 goto except_out;
342
84c1c0b5
BV
343 if (Py_TYPE(py_default) != Py_TYPE(py_item)) {
344 srd_err("All values for option '%s' must be "
6d67d057
DE
345 "of the same type as the default.",
346 o->id);
347 goto err_out;
84c1c0b5 348 }
6d67d057
DE
349 gvar = py_obj_to_variant(py_item);
350 if (!gvar) {
351 srd_err("Protocol decoder %s option 'values' "
352 "contains invalid value.", d->name);
353 goto err_out;
84c1c0b5 354 }
6d67d057
DE
355 o->values = g_slist_prepend(o->values,
356 g_variant_ref_sink(gvar));
2f395bff 357 }
2f395bff 358 }
6d67d057
DE
359 }
360 d->options = options;
361 Py_DECREF(py_opts);
514b2edc 362 PyGILState_Release(gstate);
6d67d057
DE
363
364 return SRD_OK;
365
366except_out:
367 srd_exception_catch("Failed to get %s decoder options", d->name);
368err_out:
369 g_slist_free_full(options, &decoder_option_free);
370 Py_XDECREF(py_opts);
514b2edc 371 PyGILState_Release(gstate);
6d67d057
DE
372
373 return SRD_ERR_PYTHON;
374}
375
376/* Convert annotation class attribute to GSList of char **.
377 */
378static int get_annotations(struct srd_decoder *dec)
379{
380 PyObject *py_annlist, *py_ann;
381 GSList *annotations;
382 char **annpair;
383 ssize_t i;
514b2edc
UH
384 PyGILState_STATE gstate;
385
386 gstate = PyGILState_Ensure();
6d67d057 387
514b2edc
UH
388 if (!PyObject_HasAttrString(dec->py_dec, "annotations")) {
389 PyGILState_Release(gstate);
6d67d057 390 return SRD_OK;
514b2edc 391 }
6d67d057
DE
392
393 annotations = NULL;
394
395 py_annlist = PyObject_GetAttrString(dec->py_dec, "annotations");
396 if (!py_annlist)
397 goto except_out;
398
399 if (!PyTuple_Check(py_annlist)) {
400 srd_err("Protocol decoder %s annotations should "
401 "be a tuple.", dec->name);
402 goto err_out;
403 }
404
405 for (i = PyTuple_Size(py_annlist) - 1; i >= 0; i--) {
406 py_ann = PyTuple_GetItem(py_annlist, i);
407 if (!py_ann)
408 goto except_out;
409
410 if (!PyTuple_Check(py_ann) || PyTuple_Size(py_ann) != 2) {
411 srd_err("Protocol decoder %s annotation %zd should "
412 "be a tuple with two elements.",
413 dec->name, i + 1);
414 goto err_out;
415 }
416 if (py_strseq_to_char(py_ann, &annpair) != SRD_OK)
417 goto err_out;
418
419 annotations = g_slist_prepend(annotations, annpair);
420 }
421 dec->annotations = annotations;
422 Py_DECREF(py_annlist);
514b2edc 423 PyGILState_Release(gstate);
6d67d057
DE
424
425 return SRD_OK;
426
427except_out:
428 srd_exception_catch("Failed to get %s decoder annotations", dec->name);
429err_out:
430 g_slist_free_full(annotations, (GDestroyNotify)&g_strfreev);
431 Py_XDECREF(py_annlist);
514b2edc 432 PyGILState_Release(gstate);
6d67d057
DE
433
434 return SRD_ERR_PYTHON;
435}
436
437/* Convert annotation_rows to GSList of 'struct srd_decoder_annotation_row'.
438 */
439static int get_annotation_rows(struct srd_decoder *dec)
440{
441 PyObject *py_ann_rows, *py_ann_row, *py_ann_classes, *py_item;
442 GSList *annotation_rows;
443 struct srd_decoder_annotation_row *ann_row;
444 ssize_t i, k;
445 size_t class_idx;
514b2edc
UH
446 PyGILState_STATE gstate;
447
448 gstate = PyGILState_Ensure();
6d67d057 449
514b2edc
UH
450 if (!PyObject_HasAttrString(dec->py_dec, "annotation_rows")) {
451 PyGILState_Release(gstate);
6d67d057 452 return SRD_OK;
514b2edc 453 }
6d67d057
DE
454
455 annotation_rows = NULL;
456
457 py_ann_rows = PyObject_GetAttrString(dec->py_dec, "annotation_rows");
458 if (!py_ann_rows)
459 goto except_out;
460
461 if (!PyTuple_Check(py_ann_rows)) {
462 srd_err("Protocol decoder %s annotation_rows "
463 "must be a tuple.", dec->name);
464 goto err_out;
465 }
466
467 for (i = PyTuple_Size(py_ann_rows) - 1; i >= 0; i--) {
468 py_ann_row = PyTuple_GetItem(py_ann_rows, i);
469 if (!py_ann_row)
470 goto except_out;
471
472 if (!PyTuple_Check(py_ann_row) || PyTuple_Size(py_ann_row) != 3) {
473 srd_err("Protocol decoder %s annotation_rows "
474 "must contain only tuples of 3 elements.",
475 dec->name);
476 goto err_out;
477 }
478 ann_row = g_malloc0(sizeof(struct srd_decoder_annotation_row));
479 /* Add to list right away so it doesn't get lost. */
480 annotation_rows = g_slist_prepend(annotation_rows, ann_row);
481
482 py_item = PyTuple_GetItem(py_ann_row, 0);
483 if (!py_item)
484 goto except_out;
485 if (py_str_as_str(py_item, &ann_row->id) != SRD_OK)
486 goto err_out;
487
488 py_item = PyTuple_GetItem(py_ann_row, 1);
489 if (!py_item)
490 goto except_out;
491 if (py_str_as_str(py_item, &ann_row->desc) != SRD_OK)
492 goto err_out;
493
494 py_ann_classes = PyTuple_GetItem(py_ann_row, 2);
495 if (!py_ann_classes)
496 goto except_out;
497
498 if (!PyTuple_Check(py_ann_classes)) {
499 srd_err("Protocol decoder %s annotation_rows tuples "
500 "must have a tuple of numbers as 3rd element.",
501 dec->name);
502 goto err_out;
503 }
504
505 for (k = PyTuple_Size(py_ann_classes) - 1; k >= 0; k--) {
506 py_item = PyTuple_GetItem(py_ann_classes, k);
507 if (!py_item)
508 goto except_out;
509
510 if (!PyLong_Check(py_item)) {
511 srd_err("Protocol decoder %s annotation row "
512 "class tuple must only contain numbers.",
513 dec->name);
514 goto err_out;
515 }
516 class_idx = PyLong_AsSize_t(py_item);
517 if (PyErr_Occurred())
518 goto except_out;
519
520 ann_row->ann_classes = g_slist_prepend(ann_row->ann_classes,
521 GSIZE_TO_POINTER(class_idx));
522 }
523 }
524 dec->annotation_rows = annotation_rows;
525 Py_DECREF(py_ann_rows);
514b2edc 526 PyGILState_Release(gstate);
6d67d057
DE
527
528 return SRD_OK;
529
530except_out:
531 srd_exception_catch("Failed to get %s decoder annotation rows",
532 dec->name);
533err_out:
534 g_slist_free_full(annotation_rows, &annotation_row_free);
535 Py_XDECREF(py_ann_rows);
514b2edc 536 PyGILState_Release(gstate);
6d67d057
DE
537
538 return SRD_ERR_PYTHON;
539}
540
541/* Convert binary classes to GSList of char **.
542 */
543static int get_binary_classes(struct srd_decoder *dec)
544{
545 PyObject *py_bin_classes, *py_bin_class;
546 GSList *bin_classes;
547 char **bin;
548 ssize_t i;
514b2edc 549 PyGILState_STATE gstate;
6d67d057 550
514b2edc
UH
551 gstate = PyGILState_Ensure();
552
553 if (!PyObject_HasAttrString(dec->py_dec, "binary")) {
554 PyGILState_Release(gstate);
6d67d057 555 return SRD_OK;
514b2edc 556 }
6d67d057
DE
557
558 bin_classes = NULL;
559
560 py_bin_classes = PyObject_GetAttrString(dec->py_dec, "binary");
561 if (!py_bin_classes)
562 goto except_out;
563
564 if (!PyTuple_Check(py_bin_classes)) {
565 srd_err("Protocol decoder %s binary classes should "
566 "be a tuple.", dec->name);
567 goto err_out;
568 }
569
570 for (i = PyTuple_Size(py_bin_classes) - 1; i >= 0; i--) {
571 py_bin_class = PyTuple_GetItem(py_bin_classes, i);
572 if (!py_bin_class)
573 goto except_out;
574
575 if (!PyTuple_Check(py_bin_class)
576 || PyTuple_Size(py_bin_class) != 2) {
577 srd_err("Protocol decoder %s binary classes should "
578 "consist only of tuples of 2 elements.",
579 dec->name);
580 goto err_out;
581 }
582 if (py_strseq_to_char(py_bin_class, &bin) != SRD_OK)
583 goto err_out;
584
585 bin_classes = g_slist_prepend(bin_classes, bin);
586 }
587 dec->binary = bin_classes;
588 Py_DECREF(py_bin_classes);
514b2edc 589 PyGILState_Release(gstate);
6d67d057
DE
590
591 return SRD_OK;
592
593except_out:
594 srd_exception_catch("Failed to get %s decoder binary classes",
595 dec->name);
596err_out:
597 g_slist_free_full(bin_classes, (GDestroyNotify)&g_strfreev);
598 Py_XDECREF(py_bin_classes);
514b2edc 599 PyGILState_Release(gstate);
6d67d057
DE
600
601 return SRD_ERR_PYTHON;
602}
603
604/* Check whether the Decoder class defines the named method.
605 */
606static int check_method(PyObject *py_dec, const char *mod_name,
607 const char *method_name)
608{
609 PyObject *py_method;
610 int is_callable;
514b2edc
UH
611 PyGILState_STATE gstate;
612
613 gstate = PyGILState_Ensure();
6d67d057
DE
614
615 py_method = PyObject_GetAttrString(py_dec, method_name);
616 if (!py_method) {
617 srd_exception_catch("Protocol decoder %s Decoder class "
618 "has no %s() method", mod_name, method_name);
514b2edc 619 PyGILState_Release(gstate);
6d67d057
DE
620 return SRD_ERR_PYTHON;
621 }
622
623 is_callable = PyCallable_Check(py_method);
624 Py_DECREF(py_method);
625
514b2edc
UH
626 PyGILState_Release(gstate);
627
6d67d057
DE
628 if (!is_callable) {
629 srd_err("Protocol decoder %s Decoder class attribute '%s' "
630 "is not a method.", mod_name, method_name);
631 return SRD_ERR_PYTHON;
2f395bff 632 }
2f395bff 633
84c1c0b5 634 return SRD_OK;
2f395bff
BV
635}
636
40589f25
UH
637/**
638 * Get the API version of the specified decoder.
639 *
640 * @param d The decoder to use. Must not be NULL.
641 *
642 * @return The API version of the decoder, or 0 upon errors.
c947ab01
UH
643 *
644 * @private
40589f25
UH
645 */
646SRD_PRIV long srd_decoder_apiver(const struct srd_decoder *d)
647{
648 PyObject *py_apiver;
649 long apiver;
514b2edc 650 PyGILState_STATE gstate;
40589f25
UH
651
652 if (!d)
653 return 0;
654
514b2edc
UH
655 gstate = PyGILState_Ensure();
656
40589f25
UH
657 py_apiver = PyObject_GetAttrString(d->py_dec, "api_version");
658 apiver = (py_apiver && PyLong_Check(py_apiver))
659 ? PyLong_AsLong(py_apiver) : 0;
660 Py_XDECREF(py_apiver);
661
514b2edc
UH
662 PyGILState_Release(gstate);
663
40589f25
UH
664 return apiver;
665}
666
b2c19614 667/**
64c29e28 668 * Load a protocol decoder module into the embedded Python interpreter.
b2c19614 669 *
38ff5046 670 * @param module_name The module name to be loaded.
b2c19614
BV
671 *
672 * @return SRD_OK upon success, a (negative) error code otherwise.
8c664ca2
UH
673 *
674 * @since 0.1.0
b2c19614 675 */
9d122fd5 676SRD_API int srd_decoder_load(const char *module_name)
b2c19614 677{
40589f25 678 PyObject *py_basedec;
b2c19614 679 struct srd_decoder *d;
6d67d057
DE
680 long apiver;
681 int is_subclass;
60697682 682 const char *fail_txt;
514b2edc 683 PyGILState_STATE gstate;
b2c19614 684
e195c025
BV
685 if (!srd_check_init())
686 return SRD_ERR;
687
8a014683
BV
688 if (!module_name)
689 return SRD_ERR_ARG;
690
514b2edc
UH
691 gstate = PyGILState_Ensure();
692
aac68131
BV
693 if (PyDict_GetItemString(PyImport_GetModuleDict(), module_name)) {
694 /* Module was already imported. */
514b2edc 695 PyGILState_Release(gstate);
aac68131
BV
696 return SRD_OK;
697 }
698
8ad6e500 699 srd_dbg("Loading protocol decoder '%s'.", module_name);
64c29e28 700
077fa8ac 701 d = g_malloc0(sizeof(struct srd_decoder));
60697682 702 fail_txt = NULL;
b2c19614 703
e9dd2fea 704 d->py_mod = py_import_by_name(module_name);
60697682
GS
705 if (!d->py_mod) {
706 fail_txt = "import by name failed";
6d67d057 707 goto except_out;
60697682 708 }
6d67d057
DE
709
710 if (!mod_sigrokdecode) {
711 srd_err("sigrokdecode module not loaded.");
60697682 712 fail_txt = "sigrokdecode(3) not loaded";
451680f1
BV
713 goto err_out;
714 }
b2c19614 715
451680f1 716 /* Get the 'Decoder' class as Python object. */
6d67d057 717 d->py_dec = PyObject_GetAttrString(d->py_mod, "Decoder");
60697682
GS
718 if (!d->py_dec) {
719 fail_txt = "no 'Decoder' attribute in imported module";
6d67d057 720 goto except_out;
60697682 721 }
b2c19614 722
6d67d057 723 py_basedec = PyObject_GetAttrString(mod_sigrokdecode, "Decoder");
60697682
GS
724 if (!py_basedec) {
725 fail_txt = "no 'Decoder' attribute in sigrokdecode(3)";
6d67d057 726 goto except_out;
60697682 727 }
6d67d057
DE
728
729 is_subclass = PyObject_IsSubclass(d->py_dec, py_basedec);
730 Py_DECREF(py_basedec);
b2c19614 731
6d67d057 732 if (!is_subclass) {
451680f1 733 srd_err("Decoder class in protocol decoder module %s is not "
9b37109d 734 "a subclass of sigrokdecode.Decoder.", module_name);
60697682 735 fail_txt = "not a subclass of sigrokdecode.Decoder";
451680f1
BV
736 goto err_out;
737 }
b2c19614 738
5c0c9cb3 739 /*
077fa8ac 740 * Check that this decoder has the correct PD API version.
5c0c9cb3
UH
741 * PDs of different API versions are incompatible and cannot work.
742 */
40589f25 743 apiver = srd_decoder_apiver(d);
eb883723
UH
744 if (apiver != 3) {
745 srd_exception_catch("Only PD API version 3 is supported, "
60697682
GS
746 "decoder %s has version %ld", module_name, apiver);
747 fail_txt = "API version mismatch";
1d552cd3
BV
748 goto err_out;
749 }
1d552cd3 750
6d67d057
DE
751 /* Check Decoder class for required methods.
752 */
60697682
GS
753 if (check_method(d->py_dec, module_name, "start") != SRD_OK) {
754 fail_txt = "no 'start()' method";
1d552cd3 755 goto err_out;
60697682 756 }
6d67d057 757
60697682
GS
758 if (check_method(d->py_dec, module_name, "decode") != SRD_OK) {
759 fail_txt = "no 'decode()' method";
1d552cd3 760 goto err_out;
60697682 761 }
1d552cd3 762
84c1c0b5 763 /* Store required fields in newly allocated strings. */
60697682
GS
764 if (py_attr_as_str(d->py_dec, "id", &(d->id)) != SRD_OK) {
765 fail_txt = "no 'id' attribute";
84c1c0b5 766 goto err_out;
60697682 767 }
84c1c0b5 768
60697682
GS
769 if (py_attr_as_str(d->py_dec, "name", &(d->name)) != SRD_OK) {
770 fail_txt = "no 'name' attribute";
84c1c0b5 771 goto err_out;
60697682 772 }
84c1c0b5 773
60697682
GS
774 if (py_attr_as_str(d->py_dec, "longname", &(d->longname)) != SRD_OK) {
775 fail_txt = "no 'longname' attribute";
84c1c0b5 776 goto err_out;
60697682 777 }
84c1c0b5 778
60697682
GS
779 if (py_attr_as_str(d->py_dec, "desc", &(d->desc)) != SRD_OK) {
780 fail_txt = "no 'desc' attribute";
84c1c0b5 781 goto err_out;
60697682 782 }
84c1c0b5 783
60697682
GS
784 if (py_attr_as_str(d->py_dec, "license", &(d->license)) != SRD_OK) {
785 fail_txt = "no 'license' attribute";
84c1c0b5 786 goto err_out;
60697682 787 }
84c1c0b5 788
d480174d
UH
789 if (py_attr_as_strlist(d->py_dec, "inputs", &(d->inputs)) != SRD_OK) {
790 fail_txt = "missing or malformed 'inputs' attribute";
791 goto err_out;
792 }
793
794 if (py_attr_as_strlist(d->py_dec, "outputs", &(d->outputs)) != SRD_OK) {
795 fail_txt = "missing or malformed 'outputs' attribute";
796 goto err_out;
797 }
798
84c1c0b5 799 /* All options and their default values. */
60697682
GS
800 if (get_options(d) != SRD_OK) {
801 fail_txt = "cannot get options";
2f395bff 802 goto err_out;
60697682 803 }
11ea8ae1 804
6a15597a 805 /* Check and import required channels. */
60697682
GS
806 if (get_channels(d, "channels", &d->channels, 0) != SRD_OK) {
807 fail_txt = "cannot get channels";
f38ec285 808 goto err_out;
60697682 809 }
f38ec285 810
6a15597a 811 /* Check and import optional channels. */
6d67d057 812 if (get_channels(d, "optional_channels", &d->opt_channels,
60697682
GS
813 g_slist_length(d->channels)) != SRD_OK) {
814 fail_txt = "cannot get optional channels";
f38ec285 815 goto err_out;
60697682 816 }
f38ec285 817
60697682
GS
818 if (get_annotations(d) != SRD_OK) {
819 fail_txt = "cannot get annotations";
6d67d057 820 goto err_out;
60697682 821 }
1ce46817 822
60697682
GS
823 if (get_annotation_rows(d) != SRD_OK) {
824 fail_txt = "cannot get annotation rows";
6d67d057 825 goto err_out;
60697682 826 }
d75d8a7c 827
60697682
GS
828 if (get_binary_classes(d) != SRD_OK) {
829 fail_txt = "cannot get binary classes";
6d67d057 830 goto err_out;
60697682 831 }
d75d8a7c 832
514b2edc
UH
833 PyGILState_Release(gstate);
834
c63f5a87 835 /* Append it to the list of loaded decoders. */
9b37109d
BV
836 pd_list = g_slist_append(pd_list, d);
837
6d67d057 838 return SRD_OK;
451680f1 839
6d67d057 840except_out:
e51475ad
UH
841 /* Don't show a message for the "common" directory, it's not a PD. */
842 if (strcmp(module_name, "common")) {
843 srd_exception_catch("Failed to load decoder %s: %s",
844 module_name, fail_txt);
845 }
01b60707 846 fail_txt = NULL;
451680f1 847err_out:
60697682
GS
848 if (fail_txt)
849 srd_err("Failed to load decoder %s: %s", module_name, fail_txt);
6d67d057 850 decoder_free(d);
514b2edc 851 PyGILState_Release(gstate);
b2c19614 852
6d67d057 853 return SRD_ERR_PYTHON;
451680f1
BV
854}
855
582c8473
BV
856/**
857 * Return a protocol decoder's docstring.
858 *
859 * @param dec The loaded protocol decoder.
860 *
361fdcaa 861 * @return A newly allocated buffer containing the protocol decoder's
abeeed8b 862 * documentation. The caller is responsible for free'ing the buffer.
8c664ca2
UH
863 *
864 * @since 0.1.0
582c8473 865 */
b33b8aa5 866SRD_API char *srd_decoder_doc_get(const struct srd_decoder *dec)
451680f1 867{
e7edca07 868 PyObject *py_str;
451680f1 869 char *doc;
514b2edc 870 PyGILState_STATE gstate;
451680f1 871
e195c025
BV
872 if (!srd_check_init())
873 return NULL;
874
875 if (!dec)
876 return NULL;
877
514b2edc
UH
878 gstate = PyGILState_Ensure();
879
e7edca07 880 if (!PyObject_HasAttrString(dec->py_mod, "__doc__"))
514b2edc 881 goto err;
e7edca07
BV
882
883 if (!(py_str = PyObject_GetAttrString(dec->py_mod, "__doc__"))) {
201a85a8 884 srd_exception_catch("Failed to get docstring");
514b2edc 885 goto err;
e7edca07
BV
886 }
887
451680f1 888 doc = NULL;
e7edca07
BV
889 if (py_str != Py_None)
890 py_str_as_str(py_str, &doc);
6d67d057 891 Py_DECREF(py_str);
451680f1 892
514b2edc
UH
893 PyGILState_Release(gstate);
894
451680f1 895 return doc;
514b2edc
UH
896
897err:
898 PyGILState_Release(gstate);
899
900 return NULL;
b2c19614
BV
901}
902
b2c19614 903/**
4cc0d9fe 904 * Unload the specified protocol decoder.
451680f1 905 *
fa12a21e 906 * @param dec The struct srd_decoder to be unloaded.
451680f1
BV
907 *
908 * @return SRD_OK upon success, a (negative) error code otherwise.
8c664ca2
UH
909 *
910 * @since 0.1.0
b2c19614 911 */
9d122fd5 912SRD_API int srd_decoder_unload(struct srd_decoder *dec)
b2c19614 913{
32cfb920 914 struct srd_session *sess;
2f395bff
BV
915 GSList *l;
916
e195c025
BV
917 if (!srd_check_init())
918 return SRD_ERR;
919
920 if (!dec)
921 return SRD_ERR_ARG;
922
8ad6e500 923 srd_dbg("Unloading protocol decoder '%s'.", dec->name);
fa12a21e 924
361fdcaa
UH
925 /*
926 * Since any instances of this decoder need to be released as well,
fa12a21e
BV
927 * but they could be anywhere in the stack, just free the entire
928 * stack. A frontend reloading a decoder thus has to restart all
361fdcaa
UH
929 * instances, and rebuild the stack.
930 */
32cfb920
BV
931 for (l = sessions; l; l = l->next) {
932 sess = l->data;
3262ef02 933 srd_inst_free_all(sess);
32cfb920 934 }
fa12a21e 935
c63f5a87
UH
936 /* Remove the PD from the list of loaded decoders. */
937 pd_list = g_slist_remove(pd_list, dec);
938
6d67d057 939 decoder_free(dec);
b2c19614
BV
940
941 return SRD_OK;
942}
943
6a034159
MC
944static void srd_decoder_load_all_zip_path(char *path)
945{
946 PyObject *zipimport_mod, *zipimporter_class, *zipimporter;
947 PyObject *prefix_obj, *files, *key, *value, *set, *modname;
948 Py_ssize_t pos = 0;
949 char *prefix;
950 size_t prefix_len;
514b2edc 951 PyGILState_STATE gstate;
6a034159
MC
952
953 set = files = prefix_obj = zipimporter = zipimporter_class = NULL;
954
514b2edc
UH
955 gstate = PyGILState_Ensure();
956
e9dd2fea 957 zipimport_mod = py_import_by_name("zipimport");
6a034159
MC
958 if (zipimport_mod == NULL)
959 goto err_out;
960
961 zipimporter_class = PyObject_GetAttrString(zipimport_mod, "zipimporter");
962 if (zipimporter_class == NULL)
963 goto err_out;
964
965 zipimporter = PyObject_CallFunction(zipimporter_class, "s", path);
966 if (zipimporter == NULL)
967 goto err_out;
968
969 prefix_obj = PyObject_GetAttrString(zipimporter, "prefix");
970 if (prefix_obj == NULL)
971 goto err_out;
972
973 files = PyObject_GetAttrString(zipimporter, "_files");
6d67d057 974 if (files == NULL || !PyDict_Check(files))
6a034159
MC
975 goto err_out;
976
977 set = PySet_New(NULL);
978 if (set == NULL)
979 goto err_out;
980
981 if (py_str_as_str(prefix_obj, &prefix) != SRD_OK)
982 goto err_out;
983
984 prefix_len = strlen(prefix);
985
986 while (PyDict_Next(files, &pos, &key, &value)) {
987 char *path, *slash;
988 if (py_str_as_str(key, &path) == SRD_OK) {
6d67d057
DE
989 if (strlen(path) > prefix_len
990 && memcmp(path, prefix, prefix_len) == 0
991 && (slash = strchr(path + prefix_len, '/'))) {
992
993 modname = PyUnicode_FromStringAndSize(path + prefix_len,
994 slash - (path + prefix_len));
6a034159
MC
995 if (modname == NULL) {
996 PyErr_Clear();
997 } else {
998 PySet_Add(set, modname);
6d67d057 999 Py_DECREF(modname);
6a034159
MC
1000 }
1001 }
6d67d057 1002 g_free(path);
6a034159
MC
1003 }
1004 }
6d67d057 1005 g_free(prefix);
6a034159
MC
1006
1007 while ((modname = PySet_Pop(set))) {
1008 char *modname_str;
1009 if (py_str_as_str(modname, &modname_str) == SRD_OK) {
1010 /* The directory name is the module name (e.g. "i2c"). */
1011 srd_decoder_load(modname_str);
6d67d057 1012 g_free(modname_str);
6a034159 1013 }
6d67d057 1014 Py_DECREF(modname);
6a034159
MC
1015 }
1016
1017err_out:
1018 Py_XDECREF(set);
1019 Py_XDECREF(files);
1020 Py_XDECREF(prefix_obj);
1021 Py_XDECREF(zipimporter);
1022 Py_XDECREF(zipimporter_class);
1023 Py_XDECREF(zipimport_mod);
1024 PyErr_Clear();
514b2edc 1025 PyGILState_Release(gstate);
6a034159
MC
1026}
1027
ea81b49a
BV
1028static void srd_decoder_load_all_path(char *path)
1029{
1030 GDir *dir;
1031 const gchar *direntry;
1032
6a034159 1033 if (!(dir = g_dir_open(path, 0, NULL))) {
ea81b49a 1034 /* Not really fatal */
6a034159
MC
1035 /* Try zipimport method too */
1036 srd_decoder_load_all_zip_path(path);
ea81b49a 1037 return;
6a034159 1038 }
ea81b49a
BV
1039
1040 /* This ignores errors returned by srd_decoder_load(). That
1041 * function will have logged the cause, but in any case we
1042 * want to continue anyway. */
1043 while ((direntry = g_dir_read_name(dir)) != NULL) {
1044 /* The directory name is the module name (e.g. "i2c"). */
1045 srd_decoder_load(direntry);
1046 }
1047 g_dir_close(dir);
1048
1049}
1050
582c8473 1051/**
9b37109d 1052 * Load all installed protocol decoders.
582c8473
BV
1053 *
1054 * @return SRD_OK upon success, a (negative) error code otherwise.
8c664ca2
UH
1055 *
1056 * @since 0.1.0
582c8473 1057 */
8ad6e500 1058SRD_API int srd_decoder_load_all(void)
b2c19614 1059{
ea81b49a 1060 GSList *l;
b2c19614 1061
e195c025
BV
1062 if (!srd_check_init())
1063 return SRD_ERR;
1064
ea81b49a
BV
1065 for (l = searchpaths; l; l = l->next)
1066 srd_decoder_load_all_path(l->data);
b2c19614
BV
1067
1068 return SRD_OK;
1069}
1070
b2c19614 1071/**
582c8473
BV
1072 * Unload all loaded protocol decoders.
1073 *
1074 * @return SRD_OK upon success, a (negative) error code otherwise.
8c664ca2
UH
1075 *
1076 * @since 0.1.0
b2c19614 1077 */
8ad6e500 1078SRD_API int srd_decoder_unload_all(void)
b2c19614 1079{
95e40a0c 1080 g_slist_foreach(pd_list, (GFunc)srd_decoder_unload, NULL);
820bf448
BV
1081 g_slist_free(pd_list);
1082 pd_list = NULL;
b2c19614
BV
1083
1084 return SRD_OK;
1085}
4895418c
UH
1086
1087/** @} */