]> sigrok.org Git - libsigrokdecode.git/blame - decoder.c
Python: Restrict code to stable ABI subset
[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
b2c19614 42/* The list of 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
BV
66/**
67 * Returns the list of supported/loaded protocol decoders.
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
6a15597a
UH
103static int get_channels(const struct srd_decoder *d, const char *attr,
104 GSList **pdchl)
f38ec285 105{
6a15597a
UH
106 PyObject *py_channellist, *py_entry;
107 struct srd_channel *pdch;
108 int ret, num_channels, i;
f38ec285
BV
109
110 if (!PyObject_HasAttrString(d->py_dec, attr))
6a15597a 111 /* No channels of this type specified. */
f38ec285
BV
112 return SRD_OK;
113
6a15597a
UH
114 py_channellist = PyObject_GetAttrString(d->py_dec, attr);
115 if (!PyTuple_Check(py_channellist)) {
da9bcbd9 116 srd_err("Protocol decoder %s %s attribute is not a tuple.",
22c9bc2a
BV
117 d->name, attr);
118 return SRD_ERR_PYTHON;
f38ec285
BV
119 }
120
6a15597a
UH
121 if ((num_channels = PyTuple_Size(py_channellist)) == 0)
122 /* Empty channellist. */
f38ec285
BV
123 return SRD_OK;
124
22c9bc2a 125 ret = SRD_OK;
6a15597a
UH
126 for (i = 0; i < num_channels; i++) {
127 py_entry = PyTuple_GetItem(py_channellist, i);
f38ec285
BV
128 if (!PyDict_Check(py_entry)) {
129 srd_err("Protocol decoder %s %s attribute is not "
c9bfccc6 130 "a list with dict elements.", d->name, attr);
22c9bc2a
BV
131 ret = SRD_ERR_PYTHON;
132 break;
f38ec285
BV
133 }
134
077fa8ac 135 pdch = g_malloc(sizeof(struct srd_channel));
f38ec285 136
6a15597a 137 if ((py_dictitem_as_str(py_entry, "id", &pdch->id)) != SRD_OK) {
22c9bc2a
BV
138 ret = SRD_ERR_PYTHON;
139 break;
140 }
6a15597a 141 if ((py_dictitem_as_str(py_entry, "name", &pdch->name)) != SRD_OK) {
22c9bc2a
BV
142 ret = SRD_ERR_PYTHON;
143 break;
144 }
6a15597a 145 if ((py_dictitem_as_str(py_entry, "desc", &pdch->desc)) != SRD_OK) {
22c9bc2a
BV
146 ret = SRD_ERR_PYTHON;
147 break;
148 }
6a15597a 149 pdch->order = i;
f38ec285 150
6a15597a 151 *pdchl = g_slist_append(*pdchl, pdch);
f38ec285 152 }
f38ec285 153
6a15597a 154 Py_DecRef(py_channellist);
f38ec285
BV
155
156 return ret;
157}
158
2f395bff
BV
159static int get_options(struct srd_decoder *d)
160{
84c1c0b5
BV
161 PyObject *py_opts, *py_opt, *py_val, *py_default, *py_item;
162 Py_ssize_t opt, i;
2f395bff 163 struct srd_decoder_option *o;
84c1c0b5
BV
164 GVariant *gvar;
165 gint64 lval;
166 double dval;
167 int overflow;
168 char *sval;
2f395bff
BV
169
170 if (!PyObject_HasAttrString(d->py_dec, "options"))
84c1c0b5 171 /* No options, that's fine. */
2f395bff
BV
172 return SRD_OK;
173
84c1c0b5 174 /* If present, options must be a tuple. */
2f395bff 175 py_opts = PyObject_GetAttrString(d->py_dec, "options");
84c1c0b5
BV
176 if (!PyTuple_Check(py_opts)) {
177 srd_err("Protocol decoder %s: options attribute is not "
178 "a tuple.", d->id);
179 return SRD_ERR_PYTHON;
2f395bff
BV
180 }
181
84c1c0b5
BV
182 for (opt = 0; opt < PyTuple_Size(py_opts); opt++) {
183 py_opt = PyTuple_GetItem(py_opts, opt);
184 if (!PyDict_Check(py_opt)) {
185 srd_err("Protocol decoder %s options: each option "
186 "must consist of a dictionary.", d->name);
187 return SRD_ERR_PYTHON;
2f395bff 188 }
84c1c0b5 189 if (!(py_val = PyDict_GetItemString(py_opt, "id"))) {
4916b173
DE
190 srd_err("Protocol decoder %s option %zd has no "
191 "id.", d->name, opt);
84c1c0b5 192 return SRD_ERR_PYTHON;
2f395bff 193 }
84c1c0b5
BV
194 o = g_malloc0(sizeof(struct srd_decoder_option));
195 py_str_as_str(py_val, &o->id);
196
197 if ((py_val = PyDict_GetItemString(py_opt, "desc")))
198 py_str_as_str(py_val, &o->desc);
199
200 if ((py_default = PyDict_GetItemString(py_opt, "default"))) {
201 if (PyUnicode_Check(py_default)) {
202 /* UTF-8 string */
203 py_str_as_str(py_default, &sval);
204 o->def = g_variant_new_string(sval);
205 g_free(sval);
206 } else if (PyLong_Check(py_default)) {
207 /* Long */
208 lval = PyLong_AsLongAndOverflow(py_default, &overflow);
209 if (overflow) {
210 /* Value is < LONG_MIN or > LONG_MAX */
211 PyErr_Clear();
212 srd_err("Protocol decoder %s option 'default' has "
213 "invalid default value.", d->name);
214 return SRD_ERR_PYTHON;
215 }
216 o->def = g_variant_new_int64(lval);
217 } else if (PyFloat_Check(py_default)) {
218 /* Float */
219 if ((dval = PyFloat_AsDouble(py_default)) == -1.0) {
220 PyErr_Clear();
221 srd_err("Protocol decoder %s option 'default' has "
222 "invalid default value.", d->name);
223 return SRD_ERR_PYTHON;
224 }
225 o->def = g_variant_new_double(dval);
226 } else {
227 srd_err("Protocol decoder %s option 'default' has "
201a85a8 228 "value of unsupported type.", d->name);
84c1c0b5
BV
229 return SRD_ERR_PYTHON;
230 }
231 g_variant_ref_sink(o->def);
2f395bff 232 }
84c1c0b5
BV
233
234 if ((py_val = PyDict_GetItemString(py_opt, "values"))) {
235 /* A default is required if a list of values is
236 * given, since it's used to verify their type. */
237 if (!o->def) {
238 srd_err("No default for option '%s'", o->id);
239 return SRD_ERR_PYTHON;
240 }
241 if (!PyTuple_Check(py_val)) {
242 srd_err("Option '%s' values should be a tuple.", o->id);
243 return SRD_ERR_PYTHON;
244 }
245 for (i = 0; i < PyTuple_Size(py_val); i++) {
246 py_item = PyTuple_GetItem(py_val, i);
247 if (Py_TYPE(py_default) != Py_TYPE(py_item)) {
248 srd_err("All values for option '%s' must be "
249 "of the same type as the default.",
250 o->id);
251 return SRD_ERR_PYTHON;
252 }
253 if (PyUnicode_Check(py_item)) {
254 /* UTF-8 string */
255 py_str_as_str(py_item, &sval);
256 gvar = g_variant_new_string(sval);
257 g_variant_ref_sink(gvar);
258 g_free(sval);
259 o->values = g_slist_append(o->values, gvar);
260 } else if (PyLong_Check(py_item)) {
261 /* Long */
65f6eb77 262 lval = PyLong_AsLongAndOverflow(py_item, &overflow);
84c1c0b5
BV
263 if (overflow) {
264 /* Value is < LONG_MIN or > LONG_MAX */
265 PyErr_Clear();
266 srd_err("Protocol decoder %s option 'values' "
267 "has invalid value.", d->name);
268 return SRD_ERR_PYTHON;
269 }
270 gvar = g_variant_new_int64(lval);
271 g_variant_ref_sink(gvar);
272 o->values = g_slist_append(o->values, gvar);
65f6eb77 273 } else if (PyFloat_Check(py_item)) {
84c1c0b5 274 /* Float */
65f6eb77 275 if ((dval = PyFloat_AsDouble(py_item)) == -1.0) {
84c1c0b5
BV
276 PyErr_Clear();
277 srd_err("Protocol decoder %s option 'default' has "
278 "invalid default value.", d->name);
279 return SRD_ERR_PYTHON;
280 }
281 gvar = g_variant_new_double(dval);
282 g_variant_ref_sink(gvar);
283 o->values = g_slist_append(o->values, gvar);
284 }
2f395bff 285 }
2f395bff 286 }
2f395bff
BV
287 d->options = g_slist_append(d->options, o);
288 }
2f395bff 289
84c1c0b5 290 return SRD_OK;
2f395bff
BV
291}
292
b2c19614 293/**
64c29e28 294 * Load a protocol decoder module into the embedded Python interpreter.
b2c19614 295 *
38ff5046 296 * @param module_name The module name to be loaded.
b2c19614
BV
297 *
298 * @return SRD_OK upon success, a (negative) error code otherwise.
8c664ca2
UH
299 *
300 * @since 0.1.0
b2c19614 301 */
9d122fd5 302SRD_API int srd_decoder_load(const char *module_name)
b2c19614 303{
1ce46817
UH
304 PyObject *py_basedec, *py_method, *py_attr, *py_annlist, *py_ann;
305 PyObject *py_bin_classes, *py_bin_class, *py_ann_rows, *py_ann_row;
306 PyObject *py_ann_classes, *py_long;
b2c19614 307 struct srd_decoder *d;
1ce46817
UH
308 int ret, i, j;
309 char **ann, **bin, *ann_row_id, *ann_row_desc;
6a15597a 310 struct srd_channel *pdch;
1ce46817
UH
311 GSList *l, *ann_classes;
312 struct srd_decoder_annotation_row *ann_row;
b2c19614 313
e195c025
BV
314 if (!srd_check_init())
315 return SRD_ERR;
316
8a014683
BV
317 if (!module_name)
318 return SRD_ERR_ARG;
319
aac68131
BV
320 if (PyDict_GetItemString(PyImport_GetModuleDict(), module_name)) {
321 /* Module was already imported. */
322 return SRD_OK;
323 }
324
8ad6e500 325 srd_dbg("Loading protocol decoder '%s'.", module_name);
64c29e28 326
d906d3f9 327 py_basedec = py_method = py_attr = NULL;
b2c19614 328
077fa8ac 329 d = g_malloc0(sizeof(struct srd_decoder));
b2c19614 330
21481b66
BV
331 ret = SRD_ERR_PYTHON;
332
451680f1 333 /* Import the Python module. */
9b37109d 334 if (!(d->py_mod = PyImport_ImportModule(module_name))) {
201a85a8 335 srd_exception_catch("Import of '%s' failed", module_name);
451680f1
BV
336 goto err_out;
337 }
b2c19614 338
451680f1
BV
339 /* Get the 'Decoder' class as Python object. */
340 if (!(d->py_dec = PyObject_GetAttrString(d->py_mod, "Decoder"))) {
341 /* This generated an AttributeError exception. */
342 PyErr_Clear();
361fdcaa
UH
343 srd_err("Decoder class not found in protocol decoder %s.",
344 module_name);
451680f1
BV
345 goto err_out;
346 }
b2c19614 347
451680f1 348 if (!(py_basedec = PyObject_GetAttrString(mod_sigrokdecode, "Decoder"))) {
7a1712c4 349 srd_dbg("sigrokdecode module not loaded.");
451680f1
BV
350 goto err_out;
351 }
b2c19614 352
451680f1
BV
353 if (!PyObject_IsSubclass(d->py_dec, py_basedec)) {
354 srd_err("Decoder class in protocol decoder module %s is not "
9b37109d 355 "a subclass of sigrokdecode.Decoder.", module_name);
451680f1
BV
356 goto err_out;
357 }
ad022d94 358 Py_CLEAR(py_basedec);
b2c19614 359
5c0c9cb3 360 /*
077fa8ac 361 * Check that this decoder has the correct PD API version.
5c0c9cb3
UH
362 * PDs of different API versions are incompatible and cannot work.
363 */
364 py_long = PyObject_GetAttrString(d->py_dec, "api_version");
365 if (PyLong_AsLong(py_long) != 2) {
366 srd_err("Only PDs of API version 2 are supported.");
367 goto err_out;
368 }
369 Py_CLEAR(py_long);
370
1d552cd3
BV
371 /* Check for a proper start() method. */
372 if (!PyObject_HasAttrString(d->py_dec, "start")) {
373 srd_err("Protocol decoder %s has no start() method Decoder "
9b37109d 374 "class.", module_name);
1d552cd3
BV
375 goto err_out;
376 }
377 py_method = PyObject_GetAttrString(d->py_dec, "start");
201a85a8 378 if (!PyCallable_Check(py_method)) {
d906d3f9 379 srd_err("Protocol decoder %s Decoder class attribute 'start' "
9b37109d 380 "is not a method.", module_name);
1d552cd3
BV
381 goto err_out;
382 }
ad022d94 383 Py_CLEAR(py_method);
1d552cd3
BV
384
385 /* Check for a proper decode() method. */
386 if (!PyObject_HasAttrString(d->py_dec, "decode")) {
387 srd_err("Protocol decoder %s has no decode() method Decoder "
9b37109d 388 "class.", module_name);
1d552cd3
BV
389 goto err_out;
390 }
391 py_method = PyObject_GetAttrString(d->py_dec, "decode");
201a85a8 392 if (!PyCallable_Check(py_method)) {
1d552cd3 393 srd_err("Protocol decoder %s Decoder class attribute 'decode' "
9b37109d 394 "is not a method.", module_name);
1d552cd3
BV
395 goto err_out;
396 }
ad022d94 397 Py_CLEAR(py_method);
1d552cd3 398
84c1c0b5
BV
399 /* Store required fields in newly allocated strings. */
400 if (py_attr_as_str(d->py_dec, "id", &(d->id)) != SRD_OK)
401 goto err_out;
402
403 if (py_attr_as_str(d->py_dec, "name", &(d->name)) != SRD_OK)
404 goto err_out;
405
406 if (py_attr_as_str(d->py_dec, "longname", &(d->longname)) != SRD_OK)
407 goto err_out;
408
409 if (py_attr_as_str(d->py_dec, "desc", &(d->desc)) != SRD_OK)
410 goto err_out;
411
412 if (py_attr_as_str(d->py_dec, "license", &(d->license)) != SRD_OK)
413 goto err_out;
414
415 /* All options and their default values. */
2f395bff
BV
416 if (get_options(d) != SRD_OK)
417 goto err_out;
11ea8ae1 418
6a15597a
UH
419 /* Check and import required channels. */
420 if (get_channels(d, "channels", &d->channels) != SRD_OK)
f38ec285
BV
421 goto err_out;
422
6a15597a
UH
423 /* Check and import optional channels. */
424 if (get_channels(d, "optional_channels", &d->opt_channels) != SRD_OK)
f38ec285
BV
425 goto err_out;
426
38ff5046 427 /*
6a15597a 428 * Fix order numbers for the optional channels.
38ff5046
UH
429 *
430 * Example:
6a15597a
UH
431 * Required channels: r1, r2, r3. Optional: o1, o2, o3, o4.
432 * 'order' fields in the d->channels list = 0, 1, 2.
433 * 'order' fields in the d->opt_channels list = 3, 4, 5, 6.
38ff5046 434 */
6a15597a
UH
435 for (l = d->opt_channels; l; l = l->next) {
436 pdch = l->data;
437 pdch->order += g_slist_length(d->channels);
38ff5046
UH
438 }
439
d75d8a7c 440 /* Convert annotation class attribute to GSList of char **. */
e97b6ef5 441 d->annotations = NULL;
451680f1
BV
442 if (PyObject_HasAttrString(d->py_dec, "annotations")) {
443 py_annlist = PyObject_GetAttrString(d->py_dec, "annotations");
da9bcbd9 444 if (!PyTuple_Check(py_annlist)) {
1f2275dd 445 srd_err("Protocol decoder %s annotations should "
da9bcbd9 446 "be a tuple.", module_name);
451680f1 447 goto err_out;
15969949 448 }
da9bcbd9
BV
449 for (i = 0; i < PyTuple_Size(py_annlist); i++) {
450 py_ann = PyTuple_GetItem(py_annlist, i);
451 if (!PyTuple_Check(py_ann) || PyTuple_Size(py_ann) != 2) {
1f2275dd 452 srd_err("Protocol decoder %s annotation %d should "
da9bcbd9 453 "be a tuple with two elements.", module_name, i + 1);
451680f1 454 goto err_out;
15969949
BV
455 }
456
62a2b15c 457 if (py_strseq_to_char(py_ann, &ann) != SRD_OK) {
451680f1
BV
458 goto err_out;
459 }
e97b6ef5 460 d->annotations = g_slist_append(d->annotations, ann);
15969949
BV
461 }
462 }
463
1ce46817
UH
464 /* Convert annotation_rows to GSList of 'struct srd_decoder_annotation_row'. */
465 d->annotation_rows = NULL;
466 if (PyObject_HasAttrString(d->py_dec, "annotation_rows")) {
467 py_ann_rows = PyObject_GetAttrString(d->py_dec, "annotation_rows");
468 if (!PyTuple_Check(py_ann_rows)) {
469 srd_err("Protocol decoder %s annotation row list "
470 "must be a tuple.", module_name);
471 goto err_out;
472 }
473 for (i = 0; i < PyTuple_Size(py_ann_rows); i++) {
474 py_ann_row = PyTuple_GetItem(py_ann_rows, i);
475 if (!PyTuple_Check(py_ann_row)) {
476 srd_err("Protocol decoder %s annotation rows "
477 "must be tuples.", module_name);
478 goto err_out;
479 }
480 if (PyTuple_Size(py_ann_row) != 3
481 || !PyUnicode_Check(PyTuple_GetItem(py_ann_row, 0))
482 || !PyUnicode_Check(PyTuple_GetItem(py_ann_row, 1))
483 || !PyTuple_Check(PyTuple_GetItem(py_ann_row, 2))) {
484 srd_err("Protocol decoder %s annotation rows "
485 "must contain tuples containing two "
486 "strings and a tuple.", module_name);
487 goto err_out;
488 }
489
490 if (py_str_as_str(PyTuple_GetItem(py_ann_row, 0), &ann_row_id) != SRD_OK)
491 goto err_out;
492
493 if (py_str_as_str(PyTuple_GetItem(py_ann_row, 1), &ann_row_desc) != SRD_OK)
494 goto err_out;
495
496 py_ann_classes = PyTuple_GetItem(py_ann_row, 2);
497 ann_classes = NULL;
498 for (j = 0; j < PyTuple_Size(py_ann_classes); j++) {
499 py_long = PyTuple_GetItem(py_ann_classes, j);
500 if (!PyLong_Check(py_long)) {
501 srd_err("Protocol decoder %s annotation row class "
502 "list must only contain numbers.", module_name);
503 goto err_out;
504 }
505 ann_classes = g_slist_append(ann_classes,
506 GINT_TO_POINTER(PyLong_AsLong(py_long)));
507 }
508
509 ann_row = g_malloc0(sizeof(struct srd_decoder_annotation_row));
510 ann_row->id = ann_row_id;
511 ann_row->desc = ann_row_desc;
512 ann_row->ann_classes = ann_classes;
513 d->annotation_rows = g_slist_append(d->annotation_rows, ann_row);
514 }
515 }
516
d75d8a7c
BV
517 /* Convert binary class to GSList of char *. */
518 d->binary = NULL;
519 if (PyObject_HasAttrString(d->py_dec, "binary")) {
520 py_bin_classes = PyObject_GetAttrString(d->py_dec, "binary");
521 if (!PyTuple_Check(py_bin_classes)) {
1f2275dd
BV
522 srd_err("Protocol decoder %s binary classes should "
523 "be a tuple.", module_name);
d75d8a7c
BV
524 goto err_out;
525 }
1ce46817 526 for (i = 0; i < PyTuple_Size(py_bin_classes); i++) {
d75d8a7c 527 py_bin_class = PyTuple_GetItem(py_bin_classes, i);
1f2275dd
BV
528 if (!PyTuple_Check(py_bin_class)) {
529 srd_err("Protocol decoder %s binary classes "
530 "should consist of tuples.", module_name);
531 goto err_out;
532 }
533 if (PyTuple_Size(py_bin_class) != 2
534 || !PyUnicode_Check(PyTuple_GetItem(py_bin_class, 0))
535 || !PyUnicode_Check(PyTuple_GetItem(py_bin_class, 1))) {
536 srd_err("Protocol decoder %s binary classes should "
537 "contain tuples with two strings.", module_name);
d75d8a7c
BV
538 goto err_out;
539 }
540
1f2275dd 541 if (py_strseq_to_char(py_bin_class, &bin) != SRD_OK) {
d75d8a7c
BV
542 goto err_out;
543 }
544 d->binary = g_slist_append(d->binary, bin);
545 }
546 }
547
9b37109d
BV
548 /* Append it to the list of supported/loaded decoders. */
549 pd_list = g_slist_append(pd_list, d);
550
451680f1
BV
551 ret = SRD_OK;
552
553err_out:
554 if (ret != SRD_OK) {
1d552cd3 555 Py_XDECREF(py_method);
451680f1
BV
556 Py_XDECREF(py_basedec);
557 Py_XDECREF(d->py_dec);
558 Py_XDECREF(d->py_mod);
559 g_free(d);
560 }
b2c19614 561
451680f1
BV
562 return ret;
563}
564
582c8473
BV
565/**
566 * Return a protocol decoder's docstring.
567 *
568 * @param dec The loaded protocol decoder.
569 *
361fdcaa 570 * @return A newly allocated buffer containing the protocol decoder's
abeeed8b 571 * documentation. The caller is responsible for free'ing the buffer.
8c664ca2
UH
572 *
573 * @since 0.1.0
582c8473 574 */
b33b8aa5 575SRD_API char *srd_decoder_doc_get(const struct srd_decoder *dec)
451680f1 576{
e7edca07 577 PyObject *py_str;
451680f1
BV
578 char *doc;
579
e195c025
BV
580 if (!srd_check_init())
581 return NULL;
582
583 if (!dec)
584 return NULL;
585
e7edca07
BV
586 if (!PyObject_HasAttrString(dec->py_mod, "__doc__"))
587 return NULL;
588
589 if (!(py_str = PyObject_GetAttrString(dec->py_mod, "__doc__"))) {
201a85a8 590 srd_exception_catch("Failed to get docstring");
e7edca07
BV
591 return NULL;
592 }
593
451680f1 594 doc = NULL;
e7edca07
BV
595 if (py_str != Py_None)
596 py_str_as_str(py_str, &doc);
597 Py_DecRef(py_str);
451680f1
BV
598
599 return doc;
b2c19614
BV
600}
601
6a15597a 602static void free_channels(GSList *channellist)
fa12a21e
BV
603{
604 GSList *l;
6a15597a 605 struct srd_channel *pdch;
fa12a21e 606
6a15597a 607 if (channellist == NULL)
fa12a21e
BV
608 return;
609
6a15597a
UH
610 for (l = channellist; l; l = l->next) {
611 pdch = l->data;
612 g_free(pdch->id);
613 g_free(pdch->name);
614 g_free(pdch->desc);
615 g_free(pdch);
fa12a21e 616 }
6a15597a 617 g_slist_free(channellist);
fa12a21e
BV
618}
619
b2c19614 620/**
4cc0d9fe 621 * Unload the specified protocol decoder.
451680f1 622 *
fa12a21e 623 * @param dec The struct srd_decoder to be unloaded.
451680f1
BV
624 *
625 * @return SRD_OK upon success, a (negative) error code otherwise.
8c664ca2
UH
626 *
627 * @since 0.1.0
b2c19614 628 */
9d122fd5 629SRD_API int srd_decoder_unload(struct srd_decoder *dec)
b2c19614 630{
2f395bff 631 struct srd_decoder_option *o;
32cfb920 632 struct srd_session *sess;
2f395bff
BV
633 GSList *l;
634
e195c025
BV
635 if (!srd_check_init())
636 return SRD_ERR;
637
638 if (!dec)
639 return SRD_ERR_ARG;
640
8ad6e500 641 srd_dbg("Unloading protocol decoder '%s'.", dec->name);
fa12a21e 642
361fdcaa
UH
643 /*
644 * Since any instances of this decoder need to be released as well,
fa12a21e
BV
645 * but they could be anywhere in the stack, just free the entire
646 * stack. A frontend reloading a decoder thus has to restart all
361fdcaa
UH
647 * instances, and rebuild the stack.
648 */
32cfb920
BV
649 for (l = sessions; l; l = l->next) {
650 sess = l->data;
651 srd_inst_free_all(sess, NULL);
652 }
fa12a21e 653
2f395bff
BV
654 for (l = dec->options; l; l = l->next) {
655 o = l->data;
656 g_free(o->id);
657 g_free(o->desc);
658 g_variant_unref(o->def);
659 g_free(o);
660 }
661 g_slist_free(dec->options);
662
6a15597a
UH
663 free_channels(dec->channels);
664 free_channels(dec->opt_channels);
b2c19614
BV
665 g_free(dec->id);
666 g_free(dec->name);
b231546d 667 g_free(dec->longname);
b2c19614 668 g_free(dec->desc);
b231546d 669 g_free(dec->license);
b2c19614 670
fa12a21e 671 /* The module's Decoder class. */
451680f1 672 Py_XDECREF(dec->py_dec);
fa12a21e 673 /* The module itself. */
b2c19614
BV
674 Py_XDECREF(dec->py_mod);
675
e592ac89 676 g_free(dec);
b2c19614
BV
677
678 return SRD_OK;
679}
680
6a034159
MC
681static void srd_decoder_load_all_zip_path(char *path)
682{
683 PyObject *zipimport_mod, *zipimporter_class, *zipimporter;
684 PyObject *prefix_obj, *files, *key, *value, *set, *modname;
685 Py_ssize_t pos = 0;
686 char *prefix;
687 size_t prefix_len;
688
689 set = files = prefix_obj = zipimporter = zipimporter_class = NULL;
690
691 zipimport_mod = PyImport_ImportModule("zipimport");
692 if (zipimport_mod == NULL)
693 goto err_out;
694
695 zipimporter_class = PyObject_GetAttrString(zipimport_mod, "zipimporter");
696 if (zipimporter_class == NULL)
697 goto err_out;
698
699 zipimporter = PyObject_CallFunction(zipimporter_class, "s", path);
700 if (zipimporter == NULL)
701 goto err_out;
702
703 prefix_obj = PyObject_GetAttrString(zipimporter, "prefix");
704 if (prefix_obj == NULL)
705 goto err_out;
706
707 files = PyObject_GetAttrString(zipimporter, "_files");
708 if (files == NULL)
709 goto err_out;
710
711 set = PySet_New(NULL);
712 if (set == NULL)
713 goto err_out;
714
715 if (py_str_as_str(prefix_obj, &prefix) != SRD_OK)
716 goto err_out;
717
718 prefix_len = strlen(prefix);
719
720 while (PyDict_Next(files, &pos, &key, &value)) {
721 char *path, *slash;
722 if (py_str_as_str(key, &path) == SRD_OK) {
723 if (strlen(path) > prefix_len &&
724 !memcmp(path, prefix, prefix_len) &&
725 (slash = strchr(path+prefix_len, '/'))) {
726 modname =
727 PyUnicode_FromStringAndSize(path+prefix_len,
728 slash-(path+prefix_len));
729 if (modname == NULL) {
730 PyErr_Clear();
731 } else {
732 PySet_Add(set, modname);
733 Py_XDECREF(modname);
734 }
735 }
736 free(path);
737 }
738 }
739
740 free(prefix);
741
742 while ((modname = PySet_Pop(set))) {
743 char *modname_str;
744 if (py_str_as_str(modname, &modname_str) == SRD_OK) {
745 /* The directory name is the module name (e.g. "i2c"). */
746 srd_decoder_load(modname_str);
747 free(modname_str);
748 }
749 Py_XDECREF(modname);
750 }
751
752err_out:
753 Py_XDECREF(set);
754 Py_XDECREF(files);
755 Py_XDECREF(prefix_obj);
756 Py_XDECREF(zipimporter);
757 Py_XDECREF(zipimporter_class);
758 Py_XDECREF(zipimport_mod);
759 PyErr_Clear();
760}
761
ea81b49a
BV
762static void srd_decoder_load_all_path(char *path)
763{
764 GDir *dir;
765 const gchar *direntry;
766
6a034159 767 if (!(dir = g_dir_open(path, 0, NULL))) {
ea81b49a 768 /* Not really fatal */
6a034159
MC
769 /* Try zipimport method too */
770 srd_decoder_load_all_zip_path(path);
ea81b49a 771 return;
6a034159 772 }
ea81b49a
BV
773
774 /* This ignores errors returned by srd_decoder_load(). That
775 * function will have logged the cause, but in any case we
776 * want to continue anyway. */
777 while ((direntry = g_dir_read_name(dir)) != NULL) {
778 /* The directory name is the module name (e.g. "i2c"). */
779 srd_decoder_load(direntry);
780 }
781 g_dir_close(dir);
782
783}
784
582c8473 785/**
9b37109d 786 * Load all installed protocol decoders.
582c8473
BV
787 *
788 * @return SRD_OK upon success, a (negative) error code otherwise.
8c664ca2
UH
789 *
790 * @since 0.1.0
582c8473 791 */
8ad6e500 792SRD_API int srd_decoder_load_all(void)
b2c19614 793{
ea81b49a 794 GSList *l;
b2c19614 795
e195c025
BV
796 if (!srd_check_init())
797 return SRD_ERR;
798
ea81b49a
BV
799 for (l = searchpaths; l; l = l->next)
800 srd_decoder_load_all_path(l->data);
b2c19614
BV
801
802 return SRD_OK;
803}
804
b2c19614 805/**
582c8473
BV
806 * Unload all loaded protocol decoders.
807 *
808 * @return SRD_OK upon success, a (negative) error code otherwise.
8c664ca2
UH
809 *
810 * @since 0.1.0
b2c19614 811 */
8ad6e500 812SRD_API int srd_decoder_unload_all(void)
b2c19614
BV
813{
814 GSList *l;
815 struct srd_decoder *dec;
816
38ff5046 817 for (l = pd_list; l; l = l->next) {
b2c19614 818 dec = l->data;
9d122fd5 819 srd_decoder_unload(dec);
b2c19614 820 }
820bf448
BV
821 g_slist_free(pd_list);
822 pd_list = NULL;
b2c19614
BV
823
824 return SRD_OK;
825}
4895418c
UH
826
827/** @} */