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