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