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