]> sigrok.org Git - libsigrokdecode.git/blame - decoder.c
usb_signalling: Refactor/simplify bitrate/bitwidth handling.
[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. */
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);
e592ac89 220 g_free(key);
ca3fc3c0 221 key = NULL;
2f395bff
BV
222 }
223 Py_DecRef(py_keys);
224 Py_DecRef(py_values);
225
226 ret = SRD_OK;
227
228err_out:
229 Py_XDECREF(py_opts);
230 g_free(key);
231
232 return ret;
233}
234
b2c19614 235/**
64c29e28 236 * Load a protocol decoder module into the embedded Python interpreter.
b2c19614 237 *
38ff5046 238 * @param module_name The module name to be loaded.
b2c19614
BV
239 *
240 * @return SRD_OK upon success, a (negative) error code otherwise.
8c664ca2
UH
241 *
242 * @since 0.1.0
b2c19614 243 */
9d122fd5 244SRD_API int srd_decoder_load(const char *module_name)
b2c19614 245{
11ea8ae1 246 PyObject *py_basedec, *py_method, *py_attr, *py_annlist, *py_ann;
b2c19614 247 struct srd_decoder *d;
451680f1 248 int alen, ret, i;
15969949 249 char **ann;
38ff5046
UH
250 struct srd_probe *p;
251 GSList *l;
b2c19614 252
8ad6e500 253 srd_dbg("Loading protocol decoder '%s'.", module_name);
64c29e28 254
d906d3f9 255 py_basedec = py_method = py_attr = NULL;
b2c19614 256
451680f1 257 if (!(d = g_try_malloc0(sizeof(struct srd_decoder)))) {
a61ece20 258 srd_dbg("Failed to g_malloc() struct srd_decoder.");
451680f1
BV
259 ret = SRD_ERR_MALLOC;
260 goto err_out;
b2c19614
BV
261 }
262
21481b66
BV
263 ret = SRD_ERR_PYTHON;
264
451680f1 265 /* Import the Python module. */
9b37109d 266 if (!(d->py_mod = PyImport_ImportModule(module_name))) {
aafeeaea 267 srd_exception_catch("Import of '%s' failed.", module_name);
451680f1
BV
268 goto err_out;
269 }
b2c19614 270
451680f1
BV
271 /* Get the 'Decoder' class as Python object. */
272 if (!(d->py_dec = PyObject_GetAttrString(d->py_mod, "Decoder"))) {
273 /* This generated an AttributeError exception. */
274 PyErr_Clear();
361fdcaa
UH
275 srd_err("Decoder class not found in protocol decoder %s.",
276 module_name);
451680f1
BV
277 goto err_out;
278 }
b2c19614 279
451680f1 280 if (!(py_basedec = PyObject_GetAttrString(mod_sigrokdecode, "Decoder"))) {
7a1712c4 281 srd_dbg("sigrokdecode module not loaded.");
451680f1
BV
282 goto err_out;
283 }
b2c19614 284
451680f1
BV
285 if (!PyObject_IsSubclass(d->py_dec, py_basedec)) {
286 srd_err("Decoder class in protocol decoder module %s is not "
9b37109d 287 "a subclass of sigrokdecode.Decoder.", module_name);
451680f1
BV
288 goto err_out;
289 }
ad022d94 290 Py_CLEAR(py_basedec);
b2c19614 291
1d552cd3
BV
292 /* Check for a proper start() method. */
293 if (!PyObject_HasAttrString(d->py_dec, "start")) {
294 srd_err("Protocol decoder %s has no start() method Decoder "
9b37109d 295 "class.", module_name);
1d552cd3
BV
296 goto err_out;
297 }
298 py_method = PyObject_GetAttrString(d->py_dec, "start");
299 if (!PyFunction_Check(py_method)) {
d906d3f9 300 srd_err("Protocol decoder %s Decoder class attribute 'start' "
9b37109d 301 "is not a method.", module_name);
1d552cd3
BV
302 goto err_out;
303 }
ad022d94 304 Py_CLEAR(py_method);
1d552cd3
BV
305
306 /* Check for a proper decode() method. */
307 if (!PyObject_HasAttrString(d->py_dec, "decode")) {
308 srd_err("Protocol decoder %s has no decode() method Decoder "
9b37109d 309 "class.", module_name);
1d552cd3
BV
310 goto err_out;
311 }
312 py_method = PyObject_GetAttrString(d->py_dec, "decode");
313 if (!PyFunction_Check(py_method)) {
314 srd_err("Protocol decoder %s Decoder class attribute 'decode' "
9b37109d 315 "is not a method.", module_name);
1d552cd3
BV
316 goto err_out;
317 }
ad022d94 318 Py_CLEAR(py_method);
1d552cd3 319
2f395bff
BV
320 if (get_options(d) != SRD_OK)
321 goto err_out;
11ea8ae1 322
f38ec285
BV
323 /* Check and import required probes. */
324 if (get_probes(d, "probes", &d->probes) != SRD_OK)
325 goto err_out;
326
327 /* Check and import optional probes. */
dcdf4883 328 if (get_probes(d, "optional_probes", &d->opt_probes) != SRD_OK)
f38ec285
BV
329 goto err_out;
330
38ff5046
UH
331 /*
332 * Fix order numbers for the optional probes.
333 *
334 * Example:
335 * Required probes: r1, r2, r3. Optional: o1, o2, o3, o4.
336 * 'order' fields in the d->probes list = 0, 1, 2.
337 * 'order' fields in the d->opt_probes list = 3, 4, 5, 6.
338 */
339 for (l = d->opt_probes; l; l = l->next) {
340 p = l->data;
341 p->order += g_slist_length(d->probes);
342 }
343
f38ec285 344 /* Store required fields in newly allocated strings. */
21481b66 345 if (py_attr_as_str(d->py_dec, "id", &(d->id)) != SRD_OK)
451680f1 346 goto err_out;
b2c19614 347
21481b66 348 if (py_attr_as_str(d->py_dec, "name", &(d->name)) != SRD_OK)
451680f1 349 goto err_out;
b2c19614 350
21481b66 351 if (py_attr_as_str(d->py_dec, "longname", &(d->longname)) != SRD_OK)
451680f1 352 goto err_out;
b2c19614 353
21481b66 354 if (py_attr_as_str(d->py_dec, "desc", &(d->desc)) != SRD_OK)
451680f1 355 goto err_out;
b2c19614 356
21481b66 357 if (py_attr_as_str(d->py_dec, "license", &(d->license)) != SRD_OK)
451680f1 358 goto err_out;
b2c19614 359
361fdcaa 360 /* Convert class annotation attribute to GSList of **char. */
e97b6ef5 361 d->annotations = NULL;
451680f1
BV
362 if (PyObject_HasAttrString(d->py_dec, "annotations")) {
363 py_annlist = PyObject_GetAttrString(d->py_dec, "annotations");
15969949 364 if (!PyList_Check(py_annlist)) {
c9bfccc6 365 srd_err("Protocol decoder module %s annotations "
9b37109d 366 "should be a list.", module_name);
451680f1 367 goto err_out;
15969949
BV
368 }
369 alen = PyList_Size(py_annlist);
370 for (i = 0; i < alen; i++) {
371 py_ann = PyList_GetItem(py_annlist, i);
372 if (!PyList_Check(py_ann) || PyList_Size(py_ann) != 2) {
c9bfccc6
UH
373 srd_err("Protocol decoder module %s "
374 "annotation %d should be a list with "
9b37109d 375 "two elements.", module_name, i + 1);
451680f1 376 goto err_out;
15969949
BV
377 }
378
451680f1 379 if (py_strlist_to_char(py_ann, &ann) != SRD_OK) {
451680f1
BV
380 goto err_out;
381 }
e97b6ef5 382 d->annotations = g_slist_append(d->annotations, ann);
15969949
BV
383 }
384 }
385
9b37109d
BV
386 /* Append it to the list of supported/loaded decoders. */
387 pd_list = g_slist_append(pd_list, d);
388
451680f1
BV
389 ret = SRD_OK;
390
391err_out:
392 if (ret != SRD_OK) {
1d552cd3 393 Py_XDECREF(py_method);
451680f1
BV
394 Py_XDECREF(py_basedec);
395 Py_XDECREF(d->py_dec);
396 Py_XDECREF(d->py_mod);
397 g_free(d);
398 }
b2c19614 399
451680f1
BV
400 return ret;
401}
402
582c8473
BV
403/**
404 * Return a protocol decoder's docstring.
405 *
406 * @param dec The loaded protocol decoder.
407 *
361fdcaa 408 * @return A newly allocated buffer containing the protocol decoder's
abeeed8b 409 * documentation. The caller is responsible for free'ing the buffer.
8c664ca2
UH
410 *
411 * @since 0.1.0
582c8473 412 */
b33b8aa5 413SRD_API char *srd_decoder_doc_get(const struct srd_decoder *dec)
451680f1 414{
e7edca07 415 PyObject *py_str;
451680f1
BV
416 char *doc;
417
e7edca07
BV
418 if (!PyObject_HasAttrString(dec->py_mod, "__doc__"))
419 return NULL;
420
421 if (!(py_str = PyObject_GetAttrString(dec->py_mod, "__doc__"))) {
aafeeaea 422 srd_exception_catch("");
e7edca07
BV
423 return NULL;
424 }
425
451680f1 426 doc = NULL;
e7edca07
BV
427 if (py_str != Py_None)
428 py_str_as_str(py_str, &doc);
429 Py_DecRef(py_str);
451680f1
BV
430
431 return doc;
b2c19614
BV
432}
433
fa12a21e
BV
434static void free_probes(GSList *probelist)
435{
436 GSList *l;
437 struct srd_probe *p;
438
439 if (probelist == NULL)
440 return;
441
442 for (l = probelist; l; l = l->next) {
443 p = l->data;
444 g_free(p->id);
445 g_free(p->name);
446 g_free(p->desc);
447 g_free(p);
448 }
449 g_slist_free(probelist);
fa12a21e
BV
450}
451
b2c19614 452/**
4cc0d9fe 453 * Unload the specified protocol decoder.
451680f1 454 *
fa12a21e 455 * @param dec The struct srd_decoder to be unloaded.
451680f1
BV
456 *
457 * @return SRD_OK upon success, a (negative) error code otherwise.
8c664ca2
UH
458 *
459 * @since 0.1.0
b2c19614 460 */
9d122fd5 461SRD_API int srd_decoder_unload(struct srd_decoder *dec)
b2c19614 462{
2f395bff
BV
463 struct srd_decoder_option *o;
464 GSList *l;
465
8ad6e500 466 srd_dbg("Unloading protocol decoder '%s'.", dec->name);
fa12a21e 467
361fdcaa
UH
468 /*
469 * Since any instances of this decoder need to be released as well,
fa12a21e
BV
470 * but they could be anywhere in the stack, just free the entire
471 * stack. A frontend reloading a decoder thus has to restart all
361fdcaa
UH
472 * instances, and rebuild the stack.
473 */
a8b72b05 474 srd_inst_free_all(NULL);
fa12a21e 475
2f395bff
BV
476 for (l = dec->options; l; l = l->next) {
477 o = l->data;
478 g_free(o->id);
479 g_free(o->desc);
480 g_variant_unref(o->def);
481 g_free(o);
482 }
483 g_slist_free(dec->options);
484
fa12a21e 485 free_probes(dec->probes);
dcdf4883 486 free_probes(dec->opt_probes);
b2c19614
BV
487 g_free(dec->id);
488 g_free(dec->name);
b231546d 489 g_free(dec->longname);
b2c19614 490 g_free(dec->desc);
b231546d 491 g_free(dec->license);
b2c19614 492
fa12a21e 493 /* The module's Decoder class. */
451680f1 494 Py_XDECREF(dec->py_dec);
fa12a21e 495 /* The module itself. */
b2c19614
BV
496 Py_XDECREF(dec->py_mod);
497
e592ac89 498 g_free(dec);
b2c19614
BV
499
500 return SRD_OK;
501}
502
582c8473 503/**
9b37109d 504 * Load all installed protocol decoders.
582c8473
BV
505 *
506 * @return SRD_OK upon success, a (negative) error code otherwise.
8c664ca2
UH
507 *
508 * @since 0.1.0
582c8473 509 */
8ad6e500 510SRD_API int srd_decoder_load_all(void)
b2c19614 511{
eb2bbd66
BV
512 GDir *dir;
513 GError *error;
eb2bbd66 514 const gchar *direntry;
b2c19614 515
eb2bbd66 516 if (!(dir = g_dir_open(DECODERS_DIR, 0, &error))) {
9b37109d 517 srd_err("Unable to open %s for reading.", DECODERS_DIR);
b2c19614
BV
518 return SRD_ERR_DECODERS_DIR;
519 }
520
eb2bbd66 521 while ((direntry = g_dir_read_name(dir)) != NULL) {
9b37109d 522 /* The directory name is the module name (e.g. "i2c"). */
9d122fd5 523 srd_decoder_load(direntry);
b2c19614 524 }
eb2bbd66 525 g_dir_close(dir);
b2c19614
BV
526
527 return SRD_OK;
528}
529
b2c19614 530/**
582c8473
BV
531 * Unload all loaded protocol decoders.
532 *
533 * @return SRD_OK upon success, a (negative) error code otherwise.
8c664ca2
UH
534 *
535 * @since 0.1.0
b2c19614 536 */
8ad6e500 537SRD_API int srd_decoder_unload_all(void)
b2c19614
BV
538{
539 GSList *l;
540 struct srd_decoder *dec;
541
38ff5046 542 for (l = pd_list; l; l = l->next) {
b2c19614 543 dec = l->data;
9d122fd5 544 srd_decoder_unload(dec);
b2c19614
BV
545 }
546
547 return SRD_OK;
548}
4895418c
UH
549
550/** @} */