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