]> sigrok.org Git - libsigrokdecode.git/blame - decoder.c
README: Refer to INSTALL and new Building wiki page.
[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
b2c19614 142/**
64c29e28 143 * Load a protocol decoder module into the embedded Python interpreter.
b2c19614 144 *
38ff5046 145 * @param module_name The module name to be loaded.
b2c19614
BV
146 *
147 * @return SRD_OK upon success, a (negative) error code otherwise.
148 */
9d122fd5 149SRD_API int srd_decoder_load(const char *module_name)
b2c19614 150{
11ea8ae1 151 PyObject *py_basedec, *py_method, *py_attr, *py_annlist, *py_ann;
b2c19614 152 struct srd_decoder *d;
451680f1 153 int alen, ret, i;
15969949 154 char **ann;
38ff5046
UH
155 struct srd_probe *p;
156 GSList *l;
b2c19614 157
8ad6e500 158 srd_dbg("Loading protocol decoder '%s'.", module_name);
64c29e28 159
d906d3f9 160 py_basedec = py_method = py_attr = NULL;
b2c19614 161
451680f1 162 if (!(d = g_try_malloc0(sizeof(struct srd_decoder)))) {
a61ece20 163 srd_dbg("Failed to g_malloc() struct srd_decoder.");
451680f1
BV
164 ret = SRD_ERR_MALLOC;
165 goto err_out;
b2c19614
BV
166 }
167
21481b66
BV
168 ret = SRD_ERR_PYTHON;
169
451680f1 170 /* Import the Python module. */
9b37109d 171 if (!(d->py_mod = PyImport_ImportModule(module_name))) {
aafeeaea 172 srd_exception_catch("Import of '%s' failed.", module_name);
451680f1
BV
173 goto err_out;
174 }
b2c19614 175
451680f1
BV
176 /* Get the 'Decoder' class as Python object. */
177 if (!(d->py_dec = PyObject_GetAttrString(d->py_mod, "Decoder"))) {
178 /* This generated an AttributeError exception. */
179 PyErr_Clear();
361fdcaa
UH
180 srd_err("Decoder class not found in protocol decoder %s.",
181 module_name);
451680f1
BV
182 goto err_out;
183 }
b2c19614 184
451680f1 185 if (!(py_basedec = PyObject_GetAttrString(mod_sigrokdecode, "Decoder"))) {
7a1712c4 186 srd_dbg("sigrokdecode module not loaded.");
451680f1
BV
187 goto err_out;
188 }
b2c19614 189
451680f1
BV
190 if (!PyObject_IsSubclass(d->py_dec, py_basedec)) {
191 srd_err("Decoder class in protocol decoder module %s is not "
9b37109d 192 "a subclass of sigrokdecode.Decoder.", module_name);
451680f1
BV
193 goto err_out;
194 }
ad022d94 195 Py_CLEAR(py_basedec);
b2c19614 196
1d552cd3
BV
197 /* Check for a proper start() method. */
198 if (!PyObject_HasAttrString(d->py_dec, "start")) {
199 srd_err("Protocol decoder %s has no start() method Decoder "
9b37109d 200 "class.", module_name);
1d552cd3
BV
201 goto err_out;
202 }
203 py_method = PyObject_GetAttrString(d->py_dec, "start");
204 if (!PyFunction_Check(py_method)) {
d906d3f9 205 srd_err("Protocol decoder %s Decoder class attribute 'start' "
9b37109d 206 "is not a method.", module_name);
1d552cd3
BV
207 goto err_out;
208 }
ad022d94 209 Py_CLEAR(py_method);
1d552cd3
BV
210
211 /* Check for a proper decode() method. */
212 if (!PyObject_HasAttrString(d->py_dec, "decode")) {
213 srd_err("Protocol decoder %s has no decode() method Decoder "
9b37109d 214 "class.", module_name);
1d552cd3
BV
215 goto err_out;
216 }
217 py_method = PyObject_GetAttrString(d->py_dec, "decode");
218 if (!PyFunction_Check(py_method)) {
219 srd_err("Protocol decoder %s Decoder class attribute 'decode' "
9b37109d 220 "is not a method.", module_name);
1d552cd3
BV
221 goto err_out;
222 }
ad022d94 223 Py_CLEAR(py_method);
1d552cd3 224
11ea8ae1
BV
225 /* If present, options must be a dictionary. */
226 if (PyObject_HasAttrString(d->py_dec, "options")) {
227 py_attr = PyObject_GetAttrString(d->py_dec, "options");
228 if (!PyDict_Check(py_attr)) {
229 srd_err("Protocol decoder %s options attribute is not "
c9bfccc6 230 "a dictionary.", d->name);
ad022d94 231 Py_DecRef(py_attr);
11ea8ae1
BV
232 goto err_out;
233 }
234 Py_DecRef(py_attr);
235 }
236
f38ec285
BV
237 /* Check and import required probes. */
238 if (get_probes(d, "probes", &d->probes) != SRD_OK)
239 goto err_out;
240
241 /* Check and import optional probes. */
dcdf4883 242 if (get_probes(d, "optional_probes", &d->opt_probes) != SRD_OK)
f38ec285
BV
243 goto err_out;
244
38ff5046
UH
245 /*
246 * Fix order numbers for the optional probes.
247 *
248 * Example:
249 * Required probes: r1, r2, r3. Optional: o1, o2, o3, o4.
250 * 'order' fields in the d->probes list = 0, 1, 2.
251 * 'order' fields in the d->opt_probes list = 3, 4, 5, 6.
252 */
253 for (l = d->opt_probes; l; l = l->next) {
254 p = l->data;
255 p->order += g_slist_length(d->probes);
256 }
257
f38ec285 258 /* Store required fields in newly allocated strings. */
21481b66 259 if (py_attr_as_str(d->py_dec, "id", &(d->id)) != SRD_OK)
451680f1 260 goto err_out;
b2c19614 261
21481b66 262 if (py_attr_as_str(d->py_dec, "name", &(d->name)) != SRD_OK)
451680f1 263 goto err_out;
b2c19614 264
21481b66 265 if (py_attr_as_str(d->py_dec, "longname", &(d->longname)) != SRD_OK)
451680f1 266 goto err_out;
b2c19614 267
21481b66 268 if (py_attr_as_str(d->py_dec, "desc", &(d->desc)) != SRD_OK)
451680f1 269 goto err_out;
b2c19614 270
21481b66 271 if (py_attr_as_str(d->py_dec, "license", &(d->license)) != SRD_OK)
451680f1 272 goto err_out;
b2c19614 273
361fdcaa 274 /* Convert class annotation attribute to GSList of **char. */
e97b6ef5 275 d->annotations = NULL;
451680f1
BV
276 if (PyObject_HasAttrString(d->py_dec, "annotations")) {
277 py_annlist = PyObject_GetAttrString(d->py_dec, "annotations");
15969949 278 if (!PyList_Check(py_annlist)) {
c9bfccc6 279 srd_err("Protocol decoder module %s annotations "
9b37109d 280 "should be a list.", module_name);
451680f1 281 goto err_out;
15969949
BV
282 }
283 alen = PyList_Size(py_annlist);
284 for (i = 0; i < alen; i++) {
285 py_ann = PyList_GetItem(py_annlist, i);
286 if (!PyList_Check(py_ann) || PyList_Size(py_ann) != 2) {
c9bfccc6
UH
287 srd_err("Protocol decoder module %s "
288 "annotation %d should be a list with "
9b37109d 289 "two elements.", module_name, i + 1);
451680f1 290 goto err_out;
15969949
BV
291 }
292
451680f1 293 if (py_strlist_to_char(py_ann, &ann) != SRD_OK) {
451680f1
BV
294 goto err_out;
295 }
e97b6ef5 296 d->annotations = g_slist_append(d->annotations, ann);
15969949
BV
297 }
298 }
299
9b37109d
BV
300 /* Append it to the list of supported/loaded decoders. */
301 pd_list = g_slist_append(pd_list, d);
302
451680f1
BV
303 ret = SRD_OK;
304
305err_out:
306 if (ret != SRD_OK) {
1d552cd3 307 Py_XDECREF(py_method);
451680f1
BV
308 Py_XDECREF(py_basedec);
309 Py_XDECREF(d->py_dec);
310 Py_XDECREF(d->py_mod);
311 g_free(d);
312 }
b2c19614 313
451680f1
BV
314 return ret;
315}
316
582c8473
BV
317/**
318 * Return a protocol decoder's docstring.
319 *
320 * @param dec The loaded protocol decoder.
321 *
361fdcaa 322 * @return A newly allocated buffer containing the protocol decoder's
abeeed8b 323 * documentation. The caller is responsible for free'ing the buffer.
582c8473 324 */
b33b8aa5 325SRD_API char *srd_decoder_doc_get(const struct srd_decoder *dec)
451680f1 326{
e7edca07 327 PyObject *py_str;
451680f1
BV
328 char *doc;
329
e7edca07
BV
330 if (!PyObject_HasAttrString(dec->py_mod, "__doc__"))
331 return NULL;
332
333 if (!(py_str = PyObject_GetAttrString(dec->py_mod, "__doc__"))) {
aafeeaea 334 srd_exception_catch("");
e7edca07
BV
335 return NULL;
336 }
337
451680f1 338 doc = NULL;
e7edca07
BV
339 if (py_str != Py_None)
340 py_str_as_str(py_str, &doc);
341 Py_DecRef(py_str);
451680f1
BV
342
343 return doc;
b2c19614
BV
344}
345
fa12a21e
BV
346static void free_probes(GSList *probelist)
347{
348 GSList *l;
349 struct srd_probe *p;
350
351 if (probelist == NULL)
352 return;
353
354 for (l = probelist; l; l = l->next) {
355 p = l->data;
356 g_free(p->id);
357 g_free(p->name);
358 g_free(p->desc);
359 g_free(p);
360 }
361 g_slist_free(probelist);
fa12a21e
BV
362}
363
b2c19614 364/**
451680f1
BV
365 * Unload decoder module.
366 *
fa12a21e 367 * @param dec The struct srd_decoder to be unloaded.
451680f1
BV
368 *
369 * @return SRD_OK upon success, a (negative) error code otherwise.
b2c19614 370 */
9d122fd5 371SRD_API int srd_decoder_unload(struct srd_decoder *dec)
b2c19614 372{
8ad6e500 373 srd_dbg("Unloading protocol decoder '%s'.", dec->name);
fa12a21e 374
361fdcaa
UH
375 /*
376 * Since any instances of this decoder need to be released as well,
fa12a21e
BV
377 * but they could be anywhere in the stack, just free the entire
378 * stack. A frontend reloading a decoder thus has to restart all
361fdcaa
UH
379 * instances, and rebuild the stack.
380 */
a8b72b05 381 srd_inst_free_all(NULL);
fa12a21e
BV
382
383 free_probes(dec->probes);
dcdf4883 384 free_probes(dec->opt_probes);
b2c19614
BV
385 g_free(dec->id);
386 g_free(dec->name);
b231546d 387 g_free(dec->longname);
b2c19614 388 g_free(dec->desc);
b231546d 389 g_free(dec->license);
b2c19614 390
fa12a21e 391 /* The module's Decoder class. */
451680f1 392 Py_XDECREF(dec->py_dec);
fa12a21e 393 /* The module itself. */
b2c19614
BV
394 Py_XDECREF(dec->py_mod);
395
396 /* TODO: (g_)free dec itself? */
397
398 return SRD_OK;
399}
400
582c8473 401/**
9b37109d 402 * Load all installed protocol decoders.
582c8473
BV
403 *
404 * @return SRD_OK upon success, a (negative) error code otherwise.
405 */
8ad6e500 406SRD_API int srd_decoder_load_all(void)
b2c19614 407{
eb2bbd66
BV
408 GDir *dir;
409 GError *error;
eb2bbd66 410 const gchar *direntry;
b2c19614 411
eb2bbd66 412 if (!(dir = g_dir_open(DECODERS_DIR, 0, &error))) {
9b37109d 413 srd_err("Unable to open %s for reading.", DECODERS_DIR);
b2c19614
BV
414 return SRD_ERR_DECODERS_DIR;
415 }
416
eb2bbd66 417 while ((direntry = g_dir_read_name(dir)) != NULL) {
9b37109d 418 /* The directory name is the module name (e.g. "i2c"). */
9d122fd5 419 srd_decoder_load(direntry);
b2c19614 420 }
eb2bbd66 421 g_dir_close(dir);
b2c19614
BV
422
423 return SRD_OK;
424}
425
b2c19614 426/**
582c8473
BV
427 * Unload all loaded protocol decoders.
428 *
429 * @return SRD_OK upon success, a (negative) error code otherwise.
b2c19614 430 */
8ad6e500 431SRD_API int srd_decoder_unload_all(void)
b2c19614
BV
432{
433 GSList *l;
434 struct srd_decoder *dec;
435
38ff5046 436 for (l = pd_list; l; l = l->next) {
b2c19614 437 dec = l->data;
9d122fd5 438 srd_decoder_unload(dec);
b2c19614
BV
439 }
440
441 return SRD_OK;
442}
4895418c
UH
443
444/** @} */