]> sigrok.org Git - libsigrokdecode.git/blame_incremental - decoder.c
Doxyfile: Set version to "unreleased development snapshot".
[libsigrokdecode.git] / decoder.c
... / ...
CommitLineData
1/*
2 * This file is part of the libsigrokdecode project.
3 *
4 * Copyright (C) 2010 Uwe Hermann <uwe@hermann-uwe.de>
5 * Copyright (C) 2012 Bert Vermeulen <bert@biot.com>
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"
22#include "libsigrokdecode.h" /* First, so we avoid a _POSIX_C_SOURCE warning. */
23#include "libsigrokdecode-internal.h"
24#include <glib.h>
25
26/**
27 * @file
28 *
29 * Listing, loading, unloading, and handling protocol decoders.
30 */
31
32/**
33 * @defgroup grp_decoder Protocol decoders
34 *
35 * Handling protocol decoders.
36 *
37 * @{
38 */
39
40/** @cond PRIVATE */
41
42/* The list of protocol decoders. */
43SRD_PRIV GSList *pd_list = NULL;
44
45/* module_sigrokdecode.c */
46extern SRD_PRIV PyObject *mod_sigrokdecode;
47
48/** @endcond */
49
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 *
57 * @since 0.1.0 (but the API changed in 0.2.0)
58 */
59SRD_API const GSList *srd_decoder_list(void)
60{
61 return pd_list;
62}
63
64/**
65 * Get the decoder with the specified ID.
66 *
67 * @param id The ID string of the decoder to return.
68 *
69 * @return The decoder with the specified ID, or NULL if not found.
70 *
71 * @since 0.1.0
72 */
73SRD_API struct srd_decoder *srd_decoder_get_by_id(const char *id)
74{
75 GSList *l;
76 struct srd_decoder *dec;
77
78 for (l = pd_list; l; l = l->next) {
79 dec = l->data;
80 if (!strcmp(dec->id, id))
81 return dec;
82 }
83
84 return NULL;
85}
86
87static int get_probes(const struct srd_decoder *d, const char *attr,
88 GSList **pl)
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 "
104 "a list.", d->name, attr);
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 "
117 "a list with dict elements.", d->name, attr);
118 goto err_out;
119 }
120
121 if (!(p = g_try_malloc(sizeof(struct srd_probe)))) {
122 srd_err("Failed to g_malloc() struct srd_probe.");
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
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 g_free(key);
221 }
222 Py_DecRef(py_keys);
223 Py_DecRef(py_values);
224
225 ret = SRD_OK;
226
227err_out:
228 Py_XDECREF(py_opts);
229 g_free(key);
230
231 return ret;
232}
233
234/**
235 * Load a protocol decoder module into the embedded Python interpreter.
236 *
237 * @param module_name The module name to be loaded.
238 *
239 * @return SRD_OK upon success, a (negative) error code otherwise.
240 *
241 * @since 0.1.0
242 */
243SRD_API int srd_decoder_load(const char *module_name)
244{
245 PyObject *py_basedec, *py_method, *py_attr, *py_annlist, *py_ann;
246 struct srd_decoder *d;
247 int alen, ret, i;
248 char **ann;
249 struct srd_probe *p;
250 GSList *l;
251
252 srd_dbg("Loading protocol decoder '%s'.", module_name);
253
254 py_basedec = py_method = py_attr = NULL;
255
256 if (!(d = g_try_malloc0(sizeof(struct srd_decoder)))) {
257 srd_dbg("Failed to g_malloc() struct srd_decoder.");
258 ret = SRD_ERR_MALLOC;
259 goto err_out;
260 }
261
262 ret = SRD_ERR_PYTHON;
263
264 /* Import the Python module. */
265 if (!(d->py_mod = PyImport_ImportModule(module_name))) {
266 srd_exception_catch("Import of '%s' failed.", module_name);
267 goto err_out;
268 }
269
270 /* Get the 'Decoder' class as Python object. */
271 if (!(d->py_dec = PyObject_GetAttrString(d->py_mod, "Decoder"))) {
272 /* This generated an AttributeError exception. */
273 PyErr_Clear();
274 srd_err("Decoder class not found in protocol decoder %s.",
275 module_name);
276 goto err_out;
277 }
278
279 if (!(py_basedec = PyObject_GetAttrString(mod_sigrokdecode, "Decoder"))) {
280 srd_dbg("sigrokdecode module not loaded.");
281 goto err_out;
282 }
283
284 if (!PyObject_IsSubclass(d->py_dec, py_basedec)) {
285 srd_err("Decoder class in protocol decoder module %s is not "
286 "a subclass of sigrokdecode.Decoder.", module_name);
287 goto err_out;
288 }
289 Py_CLEAR(py_basedec);
290
291 /* Check for a proper start() method. */
292 if (!PyObject_HasAttrString(d->py_dec, "start")) {
293 srd_err("Protocol decoder %s has no start() method Decoder "
294 "class.", module_name);
295 goto err_out;
296 }
297 py_method = PyObject_GetAttrString(d->py_dec, "start");
298 if (!PyFunction_Check(py_method)) {
299 srd_err("Protocol decoder %s Decoder class attribute 'start' "
300 "is not a method.", module_name);
301 goto err_out;
302 }
303 Py_CLEAR(py_method);
304
305 /* Check for a proper decode() method. */
306 if (!PyObject_HasAttrString(d->py_dec, "decode")) {
307 srd_err("Protocol decoder %s has no decode() method Decoder "
308 "class.", module_name);
309 goto err_out;
310 }
311 py_method = PyObject_GetAttrString(d->py_dec, "decode");
312 if (!PyFunction_Check(py_method)) {
313 srd_err("Protocol decoder %s Decoder class attribute 'decode' "
314 "is not a method.", module_name);
315 goto err_out;
316 }
317 Py_CLEAR(py_method);
318
319 if (get_options(d) != SRD_OK)
320 goto err_out;
321
322 /* Check and import required probes. */
323 if (get_probes(d, "probes", &d->probes) != SRD_OK)
324 goto err_out;
325
326 /* Check and import optional probes. */
327 if (get_probes(d, "optional_probes", &d->opt_probes) != SRD_OK)
328 goto err_out;
329
330 /*
331 * Fix order numbers for the optional probes.
332 *
333 * Example:
334 * Required probes: r1, r2, r3. Optional: o1, o2, o3, o4.
335 * 'order' fields in the d->probes list = 0, 1, 2.
336 * 'order' fields in the d->opt_probes list = 3, 4, 5, 6.
337 */
338 for (l = d->opt_probes; l; l = l->next) {
339 p = l->data;
340 p->order += g_slist_length(d->probes);
341 }
342
343 /* Store required fields in newly allocated strings. */
344 if (py_attr_as_str(d->py_dec, "id", &(d->id)) != SRD_OK)
345 goto err_out;
346
347 if (py_attr_as_str(d->py_dec, "name", &(d->name)) != SRD_OK)
348 goto err_out;
349
350 if (py_attr_as_str(d->py_dec, "longname", &(d->longname)) != SRD_OK)
351 goto err_out;
352
353 if (py_attr_as_str(d->py_dec, "desc", &(d->desc)) != SRD_OK)
354 goto err_out;
355
356 if (py_attr_as_str(d->py_dec, "license", &(d->license)) != SRD_OK)
357 goto err_out;
358
359 /* Convert class annotation attribute to GSList of **char. */
360 d->annotations = NULL;
361 if (PyObject_HasAttrString(d->py_dec, "annotations")) {
362 py_annlist = PyObject_GetAttrString(d->py_dec, "annotations");
363 if (!PyList_Check(py_annlist)) {
364 srd_err("Protocol decoder module %s annotations "
365 "should be a list.", module_name);
366 goto err_out;
367 }
368 alen = PyList_Size(py_annlist);
369 for (i = 0; i < alen; i++) {
370 py_ann = PyList_GetItem(py_annlist, i);
371 if (!PyList_Check(py_ann) || PyList_Size(py_ann) != 2) {
372 srd_err("Protocol decoder module %s "
373 "annotation %d should be a list with "
374 "two elements.", module_name, i + 1);
375 goto err_out;
376 }
377
378 if (py_strlist_to_char(py_ann, &ann) != SRD_OK) {
379 goto err_out;
380 }
381 d->annotations = g_slist_append(d->annotations, ann);
382 }
383 }
384
385 /* Append it to the list of supported/loaded decoders. */
386 pd_list = g_slist_append(pd_list, d);
387
388 ret = SRD_OK;
389
390err_out:
391 if (ret != SRD_OK) {
392 Py_XDECREF(py_method);
393 Py_XDECREF(py_basedec);
394 Py_XDECREF(d->py_dec);
395 Py_XDECREF(d->py_mod);
396 g_free(d);
397 }
398
399 return ret;
400}
401
402/**
403 * Return a protocol decoder's docstring.
404 *
405 * @param dec The loaded protocol decoder.
406 *
407 * @return A newly allocated buffer containing the protocol decoder's
408 * documentation. The caller is responsible for free'ing the buffer.
409 *
410 * @since 0.1.0
411 */
412SRD_API char *srd_decoder_doc_get(const struct srd_decoder *dec)
413{
414 PyObject *py_str;
415 char *doc;
416
417 if (!PyObject_HasAttrString(dec->py_mod, "__doc__"))
418 return NULL;
419
420 if (!(py_str = PyObject_GetAttrString(dec->py_mod, "__doc__"))) {
421 srd_exception_catch("");
422 return NULL;
423 }
424
425 doc = NULL;
426 if (py_str != Py_None)
427 py_str_as_str(py_str, &doc);
428 Py_DecRef(py_str);
429
430 return doc;
431}
432
433static void free_probes(GSList *probelist)
434{
435 GSList *l;
436 struct srd_probe *p;
437
438 if (probelist == NULL)
439 return;
440
441 for (l = probelist; l; l = l->next) {
442 p = l->data;
443 g_free(p->id);
444 g_free(p->name);
445 g_free(p->desc);
446 g_free(p);
447 }
448 g_slist_free(probelist);
449}
450
451/**
452 * Unload the specified protocol decoder.
453 *
454 * @param dec The struct srd_decoder to be unloaded.
455 *
456 * @return SRD_OK upon success, a (negative) error code otherwise.
457 *
458 * @since 0.1.0
459 */
460SRD_API int srd_decoder_unload(struct srd_decoder *dec)
461{
462 struct srd_decoder_option *o;
463 GSList *l;
464
465 srd_dbg("Unloading protocol decoder '%s'.", dec->name);
466
467 /*
468 * Since any instances of this decoder need to be released as well,
469 * but they could be anywhere in the stack, just free the entire
470 * stack. A frontend reloading a decoder thus has to restart all
471 * instances, and rebuild the stack.
472 */
473 srd_inst_free_all(NULL);
474
475 for (l = dec->options; l; l = l->next) {
476 o = l->data;
477 g_free(o->id);
478 g_free(o->desc);
479 g_variant_unref(o->def);
480 g_free(o);
481 }
482 g_slist_free(dec->options);
483
484 free_probes(dec->probes);
485 free_probes(dec->opt_probes);
486 g_free(dec->id);
487 g_free(dec->name);
488 g_free(dec->longname);
489 g_free(dec->desc);
490 g_free(dec->license);
491
492 /* The module's Decoder class. */
493 Py_XDECREF(dec->py_dec);
494 /* The module itself. */
495 Py_XDECREF(dec->py_mod);
496
497 g_free(dec);
498
499 return SRD_OK;
500}
501
502/**
503 * Load all installed protocol decoders.
504 *
505 * @return SRD_OK upon success, a (negative) error code otherwise.
506 *
507 * @since 0.1.0
508 */
509SRD_API int srd_decoder_load_all(void)
510{
511 GDir *dir;
512 GError *error;
513 const gchar *direntry;
514
515 if (!(dir = g_dir_open(DECODERS_DIR, 0, &error))) {
516 srd_err("Unable to open %s for reading.", DECODERS_DIR);
517 return SRD_ERR_DECODERS_DIR;
518 }
519
520 while ((direntry = g_dir_read_name(dir)) != NULL) {
521 /* The directory name is the module name (e.g. "i2c"). */
522 srd_decoder_load(direntry);
523 }
524 g_dir_close(dir);
525
526 return SRD_OK;
527}
528
529/**
530 * Unload all loaded protocol decoders.
531 *
532 * @return SRD_OK upon success, a (negative) error code otherwise.
533 *
534 * @since 0.1.0
535 */
536SRD_API int srd_decoder_unload_all(void)
537{
538 GSList *l;
539 struct srd_decoder *dec;
540
541 for (l = pd_list; l; l = l->next) {
542 dec = l->data;
543 srd_decoder_unload(dec);
544 }
545
546 return SRD_OK;
547}
548
549/** @} */