]> sigrok.org Git - libsigrokdecode.git/blame - decoder.c
configure.ac: Look for python-config-3.x besides python3.x-config.
[libsigrokdecode.git] / decoder.c
CommitLineData
b2c19614
BV
1/*
2 * This file is part of the sigrok project.
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"
73191416 22#include "sigrokdecode.h" /* First, so we avoid a _POSIX_C_SOURCE warning. */
15969949 23#include "sigrokdecode-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. */
55c3c5f4 43SRD_PRIV GSList *pd_list = NULL;
b2c19614 44
c9bfccc6 45/* module_sigrokdecode.c */
55c3c5f4 46extern SRD_PRIV PyObject *mod_sigrokdecode;
451680f1 47
57790bc8
UH
48/** @endcond */
49
b2c19614
BV
50/**
51 * Returns the list of supported/loaded protocol decoders.
52 *
53 * This is a GSList containing the names of the decoders as strings.
54 *
55 * @return List of decoders, NULL if none are supported or loaded.
56 */
38ff5046 57SRD_API const GSList *srd_decoder_list(void)
b2c19614 58{
e5080882 59 return pd_list;
b2c19614
BV
60}
61
b2c19614
BV
62/**
63 * Get the decoder with the specified ID.
64 *
65 * @param id The ID string of the decoder to return.
582c8473 66 *
b2c19614
BV
67 * @return The decoder with the specified ID, or NULL if not found.
68 */
9d122fd5 69SRD_API struct srd_decoder *srd_decoder_get_by_id(const char *id)
b2c19614
BV
70{
71 GSList *l;
72 struct srd_decoder *dec;
73
38ff5046 74 for (l = pd_list; l; l = l->next) {
b2c19614
BV
75 dec = l->data;
76 if (!strcmp(dec->id, id))
77 return dec;
78 }
79
80 return NULL;
81}
82
5a2c4dc4
UH
83static int get_probes(const struct srd_decoder *d, const char *attr,
84 GSList **pl)
f38ec285
BV
85{
86 PyObject *py_probelist, *py_entry;
87 struct srd_probe *p;
88 int ret, num_probes, i;
89
90 if (!PyObject_HasAttrString(d->py_dec, attr))
91 /* No probes of this type specified. */
92 return SRD_OK;
93
94 ret = SRD_ERR_PYTHON;
95 py_probelist = py_entry = NULL;
96
97 py_probelist = PyObject_GetAttrString(d->py_dec, attr);
98 if (!PyList_Check(py_probelist)) {
99 srd_err("Protocol decoder %s %s attribute is not "
c9bfccc6 100 "a list.", d->name, attr);
f38ec285
BV
101 goto err_out;
102 }
103
104 num_probes = PyList_Size(py_probelist);
105 if (num_probes == 0)
106 /* Empty probelist. */
107 return SRD_OK;
108
109 for (i = 0; i < num_probes; i++) {
110 py_entry = PyList_GetItem(py_probelist, i);
111 if (!PyDict_Check(py_entry)) {
112 srd_err("Protocol decoder %s %s attribute is not "
c9bfccc6 113 "a list with dict elements.", d->name, attr);
f38ec285
BV
114 goto err_out;
115 }
116
117 if (!(p = g_try_malloc(sizeof(struct srd_probe)))) {
a61ece20 118 srd_err("Failed to g_malloc() struct srd_probe.");
f38ec285
BV
119 ret = SRD_ERR_MALLOC;
120 goto err_out;
121 }
122
123 if ((py_dictitem_as_str(py_entry, "id", &p->id)) != SRD_OK)
124 goto err_out;
125 if ((py_dictitem_as_str(py_entry, "name", &p->name)) != SRD_OK)
126 goto err_out;
127 if ((py_dictitem_as_str(py_entry, "desc", &p->desc)) != SRD_OK)
128 goto err_out;
129 p->order = i;
130
131 *pl = g_slist_append(*pl, p);
132 }
133 ret = SRD_OK;
134
135err_out:
136 Py_DecRef(py_entry);
137 Py_DecRef(py_probelist);
138
139 return ret;
140}
141
2f395bff
BV
142static int get_options(struct srd_decoder *d)
143{
144 PyObject *py_opts, *py_keys, *py_values, *py_val, *py_desc, *py_default;
145 Py_ssize_t i;
146 struct srd_decoder_option *o;
147 gint64 def_long;
148 int num_keys, overflow, ret;
149 char *key, *def_str;
150
151 ret = SRD_ERR_PYTHON;
152 key = NULL;
153
154 if (!PyObject_HasAttrString(d->py_dec, "options"))
155 /* That's fine. */
156 return SRD_OK;
157
158 /* If present, options must be a dictionary. */
159 py_opts = PyObject_GetAttrString(d->py_dec, "options");
160 if (!PyDict_Check(py_opts)) {
161 srd_err("Protocol decoder %s options attribute is not "
162 "a dictionary.", d->name);
163 goto err_out;
164 }
165
166 py_keys = PyDict_Keys(py_opts);
167 py_values = PyDict_Values(py_opts);
168 num_keys = PyList_Size(py_keys);
169 for (i = 0; i < num_keys; i++) {
170 py_str_as_str(PyList_GetItem(py_keys, i), &key);
171 srd_dbg("option '%s'", key);
172 py_val = PyList_GetItem(py_values, i);
173 if (!PyList_Check(py_val) || PyList_Size(py_val) != 2) {
174 srd_err("Protocol decoder %s option '%s' value must be "
175 "a list with two elements.", d->name, key);
176 goto err_out;
177 }
178 py_desc = PyList_GetItem(py_val, 0);
179 if (!PyUnicode_Check(py_desc)) {
180 srd_err("Protocol decoder %s option '%s' has no "
181 "description.", d->name, key);
182 goto err_out;
183 }
184 py_default = PyList_GetItem(py_val, 1);
185 if (!PyUnicode_Check(py_default) && !PyLong_Check(py_default)) {
186 srd_err("Protocol decoder %s option '%s' has default "
187 "of unsupported type '%s'.", d->name, key,
188 Py_TYPE(py_default)->tp_name);
189 goto err_out;
190 }
191 if (!(o = g_try_malloc(sizeof(struct srd_decoder_option)))) {
192 srd_err("option malloc failed");
193 goto err_out;
194 }
195 o->id = g_strdup(key);
196 py_str_as_str(py_desc, &o->desc);
197 if (PyUnicode_Check(py_default)) {
198 /* UTF-8 string */
199 py_str_as_str(py_default, &def_str);
200 o->def = g_variant_new_string(def_str);
201 g_free(def_str);
202 } else {
203 /* Long */
204 def_long = PyLong_AsLongAndOverflow(py_default, &overflow);
205 if (overflow) {
206 /* Value is < LONG_MIN or > LONG_MAX */
207 PyErr_Clear();
208 srd_err("Protocol decoder %s option '%s' has "
209 "invalid default value.", d->name, key);
210 goto err_out;
211 }
212 o->def = g_variant_new_int64(def_long);
213 }
214 g_variant_ref_sink(o->def);
215 d->options = g_slist_append(d->options, o);
216 }
217 Py_DecRef(py_keys);
218 Py_DecRef(py_values);
219
220 ret = SRD_OK;
221
222err_out:
223 Py_XDECREF(py_opts);
224 g_free(key);
225
226 return ret;
227}
228
b2c19614 229/**
64c29e28 230 * Load a protocol decoder module into the embedded Python interpreter.
b2c19614 231 *
38ff5046 232 * @param module_name The module name to be loaded.
b2c19614
BV
233 *
234 * @return SRD_OK upon success, a (negative) error code otherwise.
235 */
9d122fd5 236SRD_API int srd_decoder_load(const char *module_name)
b2c19614 237{
11ea8ae1 238 PyObject *py_basedec, *py_method, *py_attr, *py_annlist, *py_ann;
b2c19614 239 struct srd_decoder *d;
451680f1 240 int alen, ret, i;
15969949 241 char **ann;
38ff5046
UH
242 struct srd_probe *p;
243 GSList *l;
b2c19614 244
8ad6e500 245 srd_dbg("Loading protocol decoder '%s'.", module_name);
64c29e28 246
d906d3f9 247 py_basedec = py_method = py_attr = NULL;
b2c19614 248
451680f1 249 if (!(d = g_try_malloc0(sizeof(struct srd_decoder)))) {
a61ece20 250 srd_dbg("Failed to g_malloc() struct srd_decoder.");
451680f1
BV
251 ret = SRD_ERR_MALLOC;
252 goto err_out;
b2c19614
BV
253 }
254
21481b66
BV
255 ret = SRD_ERR_PYTHON;
256
451680f1 257 /* Import the Python module. */
9b37109d 258 if (!(d->py_mod = PyImport_ImportModule(module_name))) {
aafeeaea 259 srd_exception_catch("Import of '%s' failed.", module_name);
451680f1
BV
260 goto err_out;
261 }
b2c19614 262
451680f1
BV
263 /* Get the 'Decoder' class as Python object. */
264 if (!(d->py_dec = PyObject_GetAttrString(d->py_mod, "Decoder"))) {
265 /* This generated an AttributeError exception. */
266 PyErr_Clear();
361fdcaa
UH
267 srd_err("Decoder class not found in protocol decoder %s.",
268 module_name);
451680f1
BV
269 goto err_out;
270 }
b2c19614 271
451680f1 272 if (!(py_basedec = PyObject_GetAttrString(mod_sigrokdecode, "Decoder"))) {
7a1712c4 273 srd_dbg("sigrokdecode module not loaded.");
451680f1
BV
274 goto err_out;
275 }
b2c19614 276
451680f1
BV
277 if (!PyObject_IsSubclass(d->py_dec, py_basedec)) {
278 srd_err("Decoder class in protocol decoder module %s is not "
9b37109d 279 "a subclass of sigrokdecode.Decoder.", module_name);
451680f1
BV
280 goto err_out;
281 }
ad022d94 282 Py_CLEAR(py_basedec);
b2c19614 283
1d552cd3
BV
284 /* Check for a proper start() method. */
285 if (!PyObject_HasAttrString(d->py_dec, "start")) {
286 srd_err("Protocol decoder %s has no start() method Decoder "
9b37109d 287 "class.", module_name);
1d552cd3
BV
288 goto err_out;
289 }
290 py_method = PyObject_GetAttrString(d->py_dec, "start");
291 if (!PyFunction_Check(py_method)) {
d906d3f9 292 srd_err("Protocol decoder %s Decoder class attribute 'start' "
9b37109d 293 "is not a method.", module_name);
1d552cd3
BV
294 goto err_out;
295 }
ad022d94 296 Py_CLEAR(py_method);
1d552cd3
BV
297
298 /* Check for a proper decode() method. */
299 if (!PyObject_HasAttrString(d->py_dec, "decode")) {
300 srd_err("Protocol decoder %s has no decode() method Decoder "
9b37109d 301 "class.", module_name);
1d552cd3
BV
302 goto err_out;
303 }
304 py_method = PyObject_GetAttrString(d->py_dec, "decode");
305 if (!PyFunction_Check(py_method)) {
306 srd_err("Protocol decoder %s Decoder class attribute 'decode' "
9b37109d 307 "is not a method.", module_name);
1d552cd3
BV
308 goto err_out;
309 }
ad022d94 310 Py_CLEAR(py_method);
1d552cd3 311
2f395bff
BV
312 if (get_options(d) != SRD_OK)
313 goto err_out;
11ea8ae1 314
f38ec285
BV
315 /* Check and import required probes. */
316 if (get_probes(d, "probes", &d->probes) != SRD_OK)
317 goto err_out;
318
319 /* Check and import optional probes. */
dcdf4883 320 if (get_probes(d, "optional_probes", &d->opt_probes) != SRD_OK)
f38ec285
BV
321 goto err_out;
322
38ff5046
UH
323 /*
324 * Fix order numbers for the optional probes.
325 *
326 * Example:
327 * Required probes: r1, r2, r3. Optional: o1, o2, o3, o4.
328 * 'order' fields in the d->probes list = 0, 1, 2.
329 * 'order' fields in the d->opt_probes list = 3, 4, 5, 6.
330 */
331 for (l = d->opt_probes; l; l = l->next) {
332 p = l->data;
333 p->order += g_slist_length(d->probes);
334 }
335
f38ec285 336 /* Store required fields in newly allocated strings. */
21481b66 337 if (py_attr_as_str(d->py_dec, "id", &(d->id)) != SRD_OK)
451680f1 338 goto err_out;
b2c19614 339
21481b66 340 if (py_attr_as_str(d->py_dec, "name", &(d->name)) != SRD_OK)
451680f1 341 goto err_out;
b2c19614 342
21481b66 343 if (py_attr_as_str(d->py_dec, "longname", &(d->longname)) != SRD_OK)
451680f1 344 goto err_out;
b2c19614 345
21481b66 346 if (py_attr_as_str(d->py_dec, "desc", &(d->desc)) != SRD_OK)
451680f1 347 goto err_out;
b2c19614 348
21481b66 349 if (py_attr_as_str(d->py_dec, "license", &(d->license)) != SRD_OK)
451680f1 350 goto err_out;
b2c19614 351
361fdcaa 352 /* Convert class annotation attribute to GSList of **char. */
e97b6ef5 353 d->annotations = NULL;
451680f1
BV
354 if (PyObject_HasAttrString(d->py_dec, "annotations")) {
355 py_annlist = PyObject_GetAttrString(d->py_dec, "annotations");
15969949 356 if (!PyList_Check(py_annlist)) {
c9bfccc6 357 srd_err("Protocol decoder module %s annotations "
9b37109d 358 "should be a list.", module_name);
451680f1 359 goto err_out;
15969949
BV
360 }
361 alen = PyList_Size(py_annlist);
362 for (i = 0; i < alen; i++) {
363 py_ann = PyList_GetItem(py_annlist, i);
364 if (!PyList_Check(py_ann) || PyList_Size(py_ann) != 2) {
c9bfccc6
UH
365 srd_err("Protocol decoder module %s "
366 "annotation %d should be a list with "
9b37109d 367 "two elements.", module_name, i + 1);
451680f1 368 goto err_out;
15969949
BV
369 }
370
451680f1 371 if (py_strlist_to_char(py_ann, &ann) != SRD_OK) {
451680f1
BV
372 goto err_out;
373 }
e97b6ef5 374 d->annotations = g_slist_append(d->annotations, ann);
15969949
BV
375 }
376 }
377
9b37109d
BV
378 /* Append it to the list of supported/loaded decoders. */
379 pd_list = g_slist_append(pd_list, d);
380
451680f1
BV
381 ret = SRD_OK;
382
383err_out:
384 if (ret != SRD_OK) {
1d552cd3 385 Py_XDECREF(py_method);
451680f1
BV
386 Py_XDECREF(py_basedec);
387 Py_XDECREF(d->py_dec);
388 Py_XDECREF(d->py_mod);
389 g_free(d);
390 }
b2c19614 391
451680f1
BV
392 return ret;
393}
394
582c8473
BV
395/**
396 * Return a protocol decoder's docstring.
397 *
398 * @param dec The loaded protocol decoder.
399 *
361fdcaa 400 * @return A newly allocated buffer containing the protocol decoder's
abeeed8b 401 * documentation. The caller is responsible for free'ing the buffer.
582c8473 402 */
b33b8aa5 403SRD_API char *srd_decoder_doc_get(const struct srd_decoder *dec)
451680f1 404{
e7edca07 405 PyObject *py_str;
451680f1
BV
406 char *doc;
407
e7edca07
BV
408 if (!PyObject_HasAttrString(dec->py_mod, "__doc__"))
409 return NULL;
410
411 if (!(py_str = PyObject_GetAttrString(dec->py_mod, "__doc__"))) {
aafeeaea 412 srd_exception_catch("");
e7edca07
BV
413 return NULL;
414 }
415
451680f1 416 doc = NULL;
e7edca07
BV
417 if (py_str != Py_None)
418 py_str_as_str(py_str, &doc);
419 Py_DecRef(py_str);
451680f1
BV
420
421 return doc;
b2c19614
BV
422}
423
fa12a21e
BV
424static void free_probes(GSList *probelist)
425{
426 GSList *l;
427 struct srd_probe *p;
428
429 if (probelist == NULL)
430 return;
431
432 for (l = probelist; l; l = l->next) {
433 p = l->data;
434 g_free(p->id);
435 g_free(p->name);
436 g_free(p->desc);
437 g_free(p);
438 }
439 g_slist_free(probelist);
fa12a21e
BV
440}
441
b2c19614 442/**
451680f1
BV
443 * Unload decoder module.
444 *
fa12a21e 445 * @param dec The struct srd_decoder to be unloaded.
451680f1
BV
446 *
447 * @return SRD_OK upon success, a (negative) error code otherwise.
b2c19614 448 */
9d122fd5 449SRD_API int srd_decoder_unload(struct srd_decoder *dec)
b2c19614 450{
2f395bff
BV
451 struct srd_decoder_option *o;
452 GSList *l;
453
8ad6e500 454 srd_dbg("Unloading protocol decoder '%s'.", dec->name);
fa12a21e 455
361fdcaa
UH
456 /*
457 * Since any instances of this decoder need to be released as well,
fa12a21e
BV
458 * but they could be anywhere in the stack, just free the entire
459 * stack. A frontend reloading a decoder thus has to restart all
361fdcaa
UH
460 * instances, and rebuild the stack.
461 */
a8b72b05 462 srd_inst_free_all(NULL);
fa12a21e 463
2f395bff
BV
464 for (l = dec->options; l; l = l->next) {
465 o = l->data;
466 g_free(o->id);
467 g_free(o->desc);
468 g_variant_unref(o->def);
469 g_free(o);
470 }
471 g_slist_free(dec->options);
472
fa12a21e 473 free_probes(dec->probes);
dcdf4883 474 free_probes(dec->opt_probes);
b2c19614
BV
475 g_free(dec->id);
476 g_free(dec->name);
b231546d 477 g_free(dec->longname);
b2c19614 478 g_free(dec->desc);
b231546d 479 g_free(dec->license);
b2c19614 480
fa12a21e 481 /* The module's Decoder class. */
451680f1 482 Py_XDECREF(dec->py_dec);
fa12a21e 483 /* The module itself. */
b2c19614
BV
484 Py_XDECREF(dec->py_mod);
485
486 /* TODO: (g_)free dec itself? */
487
488 return SRD_OK;
489}
490
582c8473 491/**
9b37109d 492 * Load all installed protocol decoders.
582c8473
BV
493 *
494 * @return SRD_OK upon success, a (negative) error code otherwise.
495 */
8ad6e500 496SRD_API int srd_decoder_load_all(void)
b2c19614 497{
eb2bbd66
BV
498 GDir *dir;
499 GError *error;
eb2bbd66 500 const gchar *direntry;
b2c19614 501
eb2bbd66 502 if (!(dir = g_dir_open(DECODERS_DIR, 0, &error))) {
9b37109d 503 srd_err("Unable to open %s for reading.", DECODERS_DIR);
b2c19614
BV
504 return SRD_ERR_DECODERS_DIR;
505 }
506
eb2bbd66 507 while ((direntry = g_dir_read_name(dir)) != NULL) {
9b37109d 508 /* The directory name is the module name (e.g. "i2c"). */
9d122fd5 509 srd_decoder_load(direntry);
b2c19614 510 }
eb2bbd66 511 g_dir_close(dir);
b2c19614
BV
512
513 return SRD_OK;
514}
515
b2c19614 516/**
582c8473
BV
517 * Unload all loaded protocol decoders.
518 *
519 * @return SRD_OK upon success, a (negative) error code otherwise.
b2c19614 520 */
8ad6e500 521SRD_API int srd_decoder_unload_all(void)
b2c19614
BV
522{
523 GSList *l;
524 struct srd_decoder *dec;
525
38ff5046 526 for (l = pd_list; l; l = l->next) {
b2c19614 527 dec = l->data;
9d122fd5 528 srd_decoder_unload(dec);
b2c19614
BV
529 }
530
531 return SRD_OK;
532}
4895418c
UH
533
534/** @} */