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