]> sigrok.org Git - libsigrokdecode.git/blame - decoder.c
Doxygen: Mark private functions/variables properly.
[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
57790bc8
UH
26/** @cond PRIVATE */
27
b2c19614 28/* The list of protocol decoders. */
55c3c5f4 29SRD_PRIV GSList *pd_list = NULL;
b2c19614 30
c9bfccc6 31/* module_sigrokdecode.c */
55c3c5f4 32extern SRD_PRIV PyObject *mod_sigrokdecode;
451680f1 33
57790bc8
UH
34/** @endcond */
35
b2c19614
BV
36/**
37 * Returns the list of supported/loaded protocol decoders.
38 *
39 * This is a GSList containing the names of the decoders as strings.
40 *
41 * @return List of decoders, NULL if none are supported or loaded.
42 */
38ff5046 43SRD_API const GSList *srd_decoder_list(void)
b2c19614 44{
e5080882 45 return pd_list;
b2c19614
BV
46}
47
b2c19614
BV
48/**
49 * Get the decoder with the specified ID.
50 *
51 * @param id The ID string of the decoder to return.
582c8473 52 *
b2c19614
BV
53 * @return The decoder with the specified ID, or NULL if not found.
54 */
9d122fd5 55SRD_API struct srd_decoder *srd_decoder_get_by_id(const char *id)
b2c19614
BV
56{
57 GSList *l;
58 struct srd_decoder *dec;
59
38ff5046 60 for (l = pd_list; l; l = l->next) {
b2c19614
BV
61 dec = l->data;
62 if (!strcmp(dec->id, id))
63 return dec;
64 }
65
66 return NULL;
67}
68
5a2c4dc4
UH
69static int get_probes(const struct srd_decoder *d, const char *attr,
70 GSList **pl)
f38ec285
BV
71{
72 PyObject *py_probelist, *py_entry;
73 struct srd_probe *p;
74 int ret, num_probes, i;
75
76 if (!PyObject_HasAttrString(d->py_dec, attr))
77 /* No probes of this type specified. */
78 return SRD_OK;
79
80 ret = SRD_ERR_PYTHON;
81 py_probelist = py_entry = NULL;
82
83 py_probelist = PyObject_GetAttrString(d->py_dec, attr);
84 if (!PyList_Check(py_probelist)) {
85 srd_err("Protocol decoder %s %s attribute is not "
c9bfccc6 86 "a list.", d->name, attr);
f38ec285
BV
87 goto err_out;
88 }
89
90 num_probes = PyList_Size(py_probelist);
91 if (num_probes == 0)
92 /* Empty probelist. */
93 return SRD_OK;
94
95 for (i = 0; i < num_probes; i++) {
96 py_entry = PyList_GetItem(py_probelist, i);
97 if (!PyDict_Check(py_entry)) {
98 srd_err("Protocol decoder %s %s attribute is not "
c9bfccc6 99 "a list with dict elements.", d->name, attr);
f38ec285
BV
100 goto err_out;
101 }
102
103 if (!(p = g_try_malloc(sizeof(struct srd_probe)))) {
a61ece20 104 srd_err("Failed to g_malloc() struct srd_probe.");
f38ec285
BV
105 ret = SRD_ERR_MALLOC;
106 goto err_out;
107 }
108
109 if ((py_dictitem_as_str(py_entry, "id", &p->id)) != SRD_OK)
110 goto err_out;
111 if ((py_dictitem_as_str(py_entry, "name", &p->name)) != SRD_OK)
112 goto err_out;
113 if ((py_dictitem_as_str(py_entry, "desc", &p->desc)) != SRD_OK)
114 goto err_out;
115 p->order = i;
116
117 *pl = g_slist_append(*pl, p);
118 }
119 ret = SRD_OK;
120
121err_out:
122 Py_DecRef(py_entry);
123 Py_DecRef(py_probelist);
124
125 return ret;
126}
127
b2c19614 128/**
64c29e28 129 * Load a protocol decoder module into the embedded Python interpreter.
b2c19614 130 *
38ff5046 131 * @param module_name The module name to be loaded.
b2c19614
BV
132 *
133 * @return SRD_OK upon success, a (negative) error code otherwise.
134 */
9d122fd5 135SRD_API int srd_decoder_load(const char *module_name)
b2c19614 136{
11ea8ae1 137 PyObject *py_basedec, *py_method, *py_attr, *py_annlist, *py_ann;
b2c19614 138 struct srd_decoder *d;
451680f1 139 int alen, ret, i;
15969949 140 char **ann;
38ff5046
UH
141 struct srd_probe *p;
142 GSList *l;
b2c19614 143
8ad6e500 144 srd_dbg("Loading protocol decoder '%s'.", module_name);
64c29e28 145
d906d3f9 146 py_basedec = py_method = py_attr = NULL;
b2c19614 147
451680f1 148 if (!(d = g_try_malloc0(sizeof(struct srd_decoder)))) {
a61ece20 149 srd_dbg("Failed to g_malloc() struct srd_decoder.");
451680f1
BV
150 ret = SRD_ERR_MALLOC;
151 goto err_out;
b2c19614
BV
152 }
153
21481b66
BV
154 ret = SRD_ERR_PYTHON;
155
451680f1 156 /* Import the Python module. */
9b37109d 157 if (!(d->py_mod = PyImport_ImportModule(module_name))) {
aafeeaea 158 srd_exception_catch("Import of '%s' failed.", module_name);
451680f1
BV
159 goto err_out;
160 }
b2c19614 161
451680f1
BV
162 /* Get the 'Decoder' class as Python object. */
163 if (!(d->py_dec = PyObject_GetAttrString(d->py_mod, "Decoder"))) {
164 /* This generated an AttributeError exception. */
165 PyErr_Clear();
361fdcaa
UH
166 srd_err("Decoder class not found in protocol decoder %s.",
167 module_name);
451680f1
BV
168 goto err_out;
169 }
b2c19614 170
451680f1 171 if (!(py_basedec = PyObject_GetAttrString(mod_sigrokdecode, "Decoder"))) {
7a1712c4 172 srd_dbg("sigrokdecode module not loaded.");
451680f1
BV
173 goto err_out;
174 }
b2c19614 175
451680f1
BV
176 if (!PyObject_IsSubclass(d->py_dec, py_basedec)) {
177 srd_err("Decoder class in protocol decoder module %s is not "
9b37109d 178 "a subclass of sigrokdecode.Decoder.", module_name);
451680f1
BV
179 goto err_out;
180 }
ad022d94 181 Py_CLEAR(py_basedec);
b2c19614 182
1d552cd3
BV
183 /* Check for a proper start() method. */
184 if (!PyObject_HasAttrString(d->py_dec, "start")) {
185 srd_err("Protocol decoder %s has no start() method Decoder "
9b37109d 186 "class.", module_name);
1d552cd3
BV
187 goto err_out;
188 }
189 py_method = PyObject_GetAttrString(d->py_dec, "start");
190 if (!PyFunction_Check(py_method)) {
d906d3f9 191 srd_err("Protocol decoder %s Decoder class attribute 'start' "
9b37109d 192 "is not a method.", module_name);
1d552cd3
BV
193 goto err_out;
194 }
ad022d94 195 Py_CLEAR(py_method);
1d552cd3
BV
196
197 /* Check for a proper decode() method. */
198 if (!PyObject_HasAttrString(d->py_dec, "decode")) {
199 srd_err("Protocol decoder %s has no decode() method Decoder "
9b37109d 200 "class.", module_name);
1d552cd3
BV
201 goto err_out;
202 }
203 py_method = PyObject_GetAttrString(d->py_dec, "decode");
204 if (!PyFunction_Check(py_method)) {
205 srd_err("Protocol decoder %s Decoder class attribute 'decode' "
9b37109d 206 "is not a method.", module_name);
1d552cd3
BV
207 goto err_out;
208 }
ad022d94 209 Py_CLEAR(py_method);
1d552cd3 210
11ea8ae1
BV
211 /* If present, options must be a dictionary. */
212 if (PyObject_HasAttrString(d->py_dec, "options")) {
213 py_attr = PyObject_GetAttrString(d->py_dec, "options");
214 if (!PyDict_Check(py_attr)) {
215 srd_err("Protocol decoder %s options attribute is not "
c9bfccc6 216 "a dictionary.", d->name);
ad022d94 217 Py_DecRef(py_attr);
11ea8ae1
BV
218 goto err_out;
219 }
220 Py_DecRef(py_attr);
221 }
222
f38ec285
BV
223 /* Check and import required probes. */
224 if (get_probes(d, "probes", &d->probes) != SRD_OK)
225 goto err_out;
226
227 /* Check and import optional probes. */
dcdf4883 228 if (get_probes(d, "optional_probes", &d->opt_probes) != SRD_OK)
f38ec285
BV
229 goto err_out;
230
38ff5046
UH
231 /*
232 * Fix order numbers for the optional probes.
233 *
234 * Example:
235 * Required probes: r1, r2, r3. Optional: o1, o2, o3, o4.
236 * 'order' fields in the d->probes list = 0, 1, 2.
237 * 'order' fields in the d->opt_probes list = 3, 4, 5, 6.
238 */
239 for (l = d->opt_probes; l; l = l->next) {
240 p = l->data;
241 p->order += g_slist_length(d->probes);
242 }
243
f38ec285 244 /* Store required fields in newly allocated strings. */
21481b66 245 if (py_attr_as_str(d->py_dec, "id", &(d->id)) != SRD_OK)
451680f1 246 goto err_out;
b2c19614 247
21481b66 248 if (py_attr_as_str(d->py_dec, "name", &(d->name)) != SRD_OK)
451680f1 249 goto err_out;
b2c19614 250
21481b66 251 if (py_attr_as_str(d->py_dec, "longname", &(d->longname)) != SRD_OK)
451680f1 252 goto err_out;
b2c19614 253
21481b66 254 if (py_attr_as_str(d->py_dec, "desc", &(d->desc)) != SRD_OK)
451680f1 255 goto err_out;
b2c19614 256
21481b66 257 if (py_attr_as_str(d->py_dec, "license", &(d->license)) != SRD_OK)
451680f1 258 goto err_out;
b2c19614 259
361fdcaa 260 /* Convert class annotation attribute to GSList of **char. */
e97b6ef5 261 d->annotations = NULL;
451680f1
BV
262 if (PyObject_HasAttrString(d->py_dec, "annotations")) {
263 py_annlist = PyObject_GetAttrString(d->py_dec, "annotations");
15969949 264 if (!PyList_Check(py_annlist)) {
c9bfccc6 265 srd_err("Protocol decoder module %s annotations "
9b37109d 266 "should be a list.", module_name);
451680f1 267 goto err_out;
15969949
BV
268 }
269 alen = PyList_Size(py_annlist);
270 for (i = 0; i < alen; i++) {
271 py_ann = PyList_GetItem(py_annlist, i);
272 if (!PyList_Check(py_ann) || PyList_Size(py_ann) != 2) {
c9bfccc6
UH
273 srd_err("Protocol decoder module %s "
274 "annotation %d should be a list with "
9b37109d 275 "two elements.", module_name, i + 1);
451680f1 276 goto err_out;
15969949
BV
277 }
278
451680f1 279 if (py_strlist_to_char(py_ann, &ann) != SRD_OK) {
451680f1
BV
280 goto err_out;
281 }
e97b6ef5 282 d->annotations = g_slist_append(d->annotations, ann);
15969949
BV
283 }
284 }
285
9b37109d
BV
286 /* Append it to the list of supported/loaded decoders. */
287 pd_list = g_slist_append(pd_list, d);
288
451680f1
BV
289 ret = SRD_OK;
290
291err_out:
292 if (ret != SRD_OK) {
1d552cd3 293 Py_XDECREF(py_method);
451680f1
BV
294 Py_XDECREF(py_basedec);
295 Py_XDECREF(d->py_dec);
296 Py_XDECREF(d->py_mod);
297 g_free(d);
298 }
b2c19614 299
451680f1
BV
300 return ret;
301}
302
582c8473
BV
303/**
304 * Return a protocol decoder's docstring.
305 *
306 * @param dec The loaded protocol decoder.
307 *
361fdcaa 308 * @return A newly allocated buffer containing the protocol decoder's
abeeed8b 309 * documentation. The caller is responsible for free'ing the buffer.
582c8473 310 */
b33b8aa5 311SRD_API char *srd_decoder_doc_get(const struct srd_decoder *dec)
451680f1 312{
e7edca07 313 PyObject *py_str;
451680f1
BV
314 char *doc;
315
e7edca07
BV
316 if (!PyObject_HasAttrString(dec->py_mod, "__doc__"))
317 return NULL;
318
319 if (!(py_str = PyObject_GetAttrString(dec->py_mod, "__doc__"))) {
aafeeaea 320 srd_exception_catch("");
e7edca07
BV
321 return NULL;
322 }
323
451680f1 324 doc = NULL;
e7edca07
BV
325 if (py_str != Py_None)
326 py_str_as_str(py_str, &doc);
327 Py_DecRef(py_str);
451680f1
BV
328
329 return doc;
b2c19614
BV
330}
331
fa12a21e
BV
332static void free_probes(GSList *probelist)
333{
334 GSList *l;
335 struct srd_probe *p;
336
337 if (probelist == NULL)
338 return;
339
340 for (l = probelist; l; l = l->next) {
341 p = l->data;
342 g_free(p->id);
343 g_free(p->name);
344 g_free(p->desc);
345 g_free(p);
346 }
347 g_slist_free(probelist);
fa12a21e
BV
348}
349
b2c19614 350/**
451680f1
BV
351 * Unload decoder module.
352 *
fa12a21e 353 * @param dec The struct srd_decoder to be unloaded.
451680f1
BV
354 *
355 * @return SRD_OK upon success, a (negative) error code otherwise.
b2c19614 356 */
9d122fd5 357SRD_API int srd_decoder_unload(struct srd_decoder *dec)
b2c19614 358{
8ad6e500 359 srd_dbg("Unloading protocol decoder '%s'.", dec->name);
fa12a21e 360
361fdcaa
UH
361 /*
362 * Since any instances of this decoder need to be released as well,
fa12a21e
BV
363 * but they could be anywhere in the stack, just free the entire
364 * stack. A frontend reloading a decoder thus has to restart all
361fdcaa
UH
365 * instances, and rebuild the stack.
366 */
a8b72b05 367 srd_inst_free_all(NULL);
fa12a21e
BV
368
369 free_probes(dec->probes);
dcdf4883 370 free_probes(dec->opt_probes);
b2c19614
BV
371 g_free(dec->id);
372 g_free(dec->name);
b231546d 373 g_free(dec->longname);
b2c19614 374 g_free(dec->desc);
b231546d 375 g_free(dec->license);
b2c19614 376
fa12a21e 377 /* The module's Decoder class. */
451680f1 378 Py_XDECREF(dec->py_dec);
fa12a21e 379 /* The module itself. */
b2c19614
BV
380 Py_XDECREF(dec->py_mod);
381
382 /* TODO: (g_)free dec itself? */
383
384 return SRD_OK;
385}
386
582c8473 387/**
9b37109d 388 * Load all installed protocol decoders.
582c8473
BV
389 *
390 * @return SRD_OK upon success, a (negative) error code otherwise.
391 */
8ad6e500 392SRD_API int srd_decoder_load_all(void)
b2c19614 393{
eb2bbd66
BV
394 GDir *dir;
395 GError *error;
eb2bbd66 396 const gchar *direntry;
b2c19614 397
eb2bbd66 398 if (!(dir = g_dir_open(DECODERS_DIR, 0, &error))) {
9b37109d 399 srd_err("Unable to open %s for reading.", DECODERS_DIR);
b2c19614
BV
400 return SRD_ERR_DECODERS_DIR;
401 }
402
eb2bbd66 403 while ((direntry = g_dir_read_name(dir)) != NULL) {
9b37109d 404 /* The directory name is the module name (e.g. "i2c"). */
9d122fd5 405 srd_decoder_load(direntry);
b2c19614 406 }
eb2bbd66 407 g_dir_close(dir);
b2c19614
BV
408
409 return SRD_OK;
410}
411
b2c19614 412/**
582c8473
BV
413 * Unload all loaded protocol decoders.
414 *
415 * @return SRD_OK upon success, a (negative) error code otherwise.
b2c19614 416 */
8ad6e500 417SRD_API int srd_decoder_unload_all(void)
b2c19614
BV
418{
419 GSList *l;
420 struct srd_decoder *dec;
421
38ff5046 422 for (l = pd_list; l; l = l->next) {
b2c19614 423 dec = l->data;
9d122fd5 424 srd_decoder_unload(dec);
b2c19614
BV
425 }
426
427 return SRD_OK;
428}