]> sigrok.org Git - libsigrokdecode.git/blame_incremental - decoder.c
Doxygen: Initial groups and topic short descriptions.
[libsigrokdecode.git] / decoder.c
... / ...
CommitLineData
1/*
2 * This file is part of the sigrok 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 "sigrokdecode.h" /* First, so we avoid a _POSIX_C_SOURCE warning. */
23#include "sigrokdecode-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 */
57SRD_API const GSList *srd_decoder_list(void)
58{
59 return pd_list;
60}
61
62/**
63 * Get the decoder with the specified ID.
64 *
65 * @param id The ID string of the decoder to return.
66 *
67 * @return The decoder with the specified ID, or NULL if not found.
68 */
69SRD_API struct srd_decoder *srd_decoder_get_by_id(const char *id)
70{
71 GSList *l;
72 struct srd_decoder *dec;
73
74 for (l = pd_list; l; l = l->next) {
75 dec = l->data;
76 if (!strcmp(dec->id, id))
77 return dec;
78 }
79
80 return NULL;
81}
82
83static int get_probes(const struct srd_decoder *d, const char *attr,
84 GSList **pl)
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 "
100 "a list.", d->name, attr);
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 "
113 "a list with dict elements.", d->name, attr);
114 goto err_out;
115 }
116
117 if (!(p = g_try_malloc(sizeof(struct srd_probe)))) {
118 srd_err("Failed to g_malloc() struct srd_probe.");
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
142/**
143 * Load a protocol decoder module into the embedded Python interpreter.
144 *
145 * @param module_name The module name to be loaded.
146 *
147 * @return SRD_OK upon success, a (negative) error code otherwise.
148 */
149SRD_API int srd_decoder_load(const char *module_name)
150{
151 PyObject *py_basedec, *py_method, *py_attr, *py_annlist, *py_ann;
152 struct srd_decoder *d;
153 int alen, ret, i;
154 char **ann;
155 struct srd_probe *p;
156 GSList *l;
157
158 srd_dbg("Loading protocol decoder '%s'.", module_name);
159
160 py_basedec = py_method = py_attr = NULL;
161
162 if (!(d = g_try_malloc0(sizeof(struct srd_decoder)))) {
163 srd_dbg("Failed to g_malloc() struct srd_decoder.");
164 ret = SRD_ERR_MALLOC;
165 goto err_out;
166 }
167
168 ret = SRD_ERR_PYTHON;
169
170 /* Import the Python module. */
171 if (!(d->py_mod = PyImport_ImportModule(module_name))) {
172 srd_exception_catch("Import of '%s' failed.", module_name);
173 goto err_out;
174 }
175
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();
180 srd_err("Decoder class not found in protocol decoder %s.",
181 module_name);
182 goto err_out;
183 }
184
185 if (!(py_basedec = PyObject_GetAttrString(mod_sigrokdecode, "Decoder"))) {
186 srd_dbg("sigrokdecode module not loaded.");
187 goto err_out;
188 }
189
190 if (!PyObject_IsSubclass(d->py_dec, py_basedec)) {
191 srd_err("Decoder class in protocol decoder module %s is not "
192 "a subclass of sigrokdecode.Decoder.", module_name);
193 goto err_out;
194 }
195 Py_CLEAR(py_basedec);
196
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 "
200 "class.", module_name);
201 goto err_out;
202 }
203 py_method = PyObject_GetAttrString(d->py_dec, "start");
204 if (!PyFunction_Check(py_method)) {
205 srd_err("Protocol decoder %s Decoder class attribute 'start' "
206 "is not a method.", module_name);
207 goto err_out;
208 }
209 Py_CLEAR(py_method);
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 "
214 "class.", module_name);
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' "
220 "is not a method.", module_name);
221 goto err_out;
222 }
223 Py_CLEAR(py_method);
224
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 "
230 "a dictionary.", d->name);
231 Py_DecRef(py_attr);
232 goto err_out;
233 }
234 Py_DecRef(py_attr);
235 }
236
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. */
242 if (get_probes(d, "optional_probes", &d->opt_probes) != SRD_OK)
243 goto err_out;
244
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
258 /* Store required fields in newly allocated strings. */
259 if (py_attr_as_str(d->py_dec, "id", &(d->id)) != SRD_OK)
260 goto err_out;
261
262 if (py_attr_as_str(d->py_dec, "name", &(d->name)) != SRD_OK)
263 goto err_out;
264
265 if (py_attr_as_str(d->py_dec, "longname", &(d->longname)) != SRD_OK)
266 goto err_out;
267
268 if (py_attr_as_str(d->py_dec, "desc", &(d->desc)) != SRD_OK)
269 goto err_out;
270
271 if (py_attr_as_str(d->py_dec, "license", &(d->license)) != SRD_OK)
272 goto err_out;
273
274 /* Convert class annotation attribute to GSList of **char. */
275 d->annotations = NULL;
276 if (PyObject_HasAttrString(d->py_dec, "annotations")) {
277 py_annlist = PyObject_GetAttrString(d->py_dec, "annotations");
278 if (!PyList_Check(py_annlist)) {
279 srd_err("Protocol decoder module %s annotations "
280 "should be a list.", module_name);
281 goto err_out;
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) {
287 srd_err("Protocol decoder module %s "
288 "annotation %d should be a list with "
289 "two elements.", module_name, i + 1);
290 goto err_out;
291 }
292
293 if (py_strlist_to_char(py_ann, &ann) != SRD_OK) {
294 goto err_out;
295 }
296 d->annotations = g_slist_append(d->annotations, ann);
297 }
298 }
299
300 /* Append it to the list of supported/loaded decoders. */
301 pd_list = g_slist_append(pd_list, d);
302
303 ret = SRD_OK;
304
305err_out:
306 if (ret != SRD_OK) {
307 Py_XDECREF(py_method);
308 Py_XDECREF(py_basedec);
309 Py_XDECREF(d->py_dec);
310 Py_XDECREF(d->py_mod);
311 g_free(d);
312 }
313
314 return ret;
315}
316
317/**
318 * Return a protocol decoder's docstring.
319 *
320 * @param dec The loaded protocol decoder.
321 *
322 * @return A newly allocated buffer containing the protocol decoder's
323 * documentation. The caller is responsible for free'ing the buffer.
324 */
325SRD_API char *srd_decoder_doc_get(const struct srd_decoder *dec)
326{
327 PyObject *py_str;
328 char *doc;
329
330 if (!PyObject_HasAttrString(dec->py_mod, "__doc__"))
331 return NULL;
332
333 if (!(py_str = PyObject_GetAttrString(dec->py_mod, "__doc__"))) {
334 srd_exception_catch("");
335 return NULL;
336 }
337
338 doc = NULL;
339 if (py_str != Py_None)
340 py_str_as_str(py_str, &doc);
341 Py_DecRef(py_str);
342
343 return doc;
344}
345
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);
362}
363
364/**
365 * Unload decoder module.
366 *
367 * @param dec The struct srd_decoder to be unloaded.
368 *
369 * @return SRD_OK upon success, a (negative) error code otherwise.
370 */
371SRD_API int srd_decoder_unload(struct srd_decoder *dec)
372{
373 srd_dbg("Unloading protocol decoder '%s'.", dec->name);
374
375 /*
376 * Since any instances of this decoder need to be released as well,
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
379 * instances, and rebuild the stack.
380 */
381 srd_inst_free_all(NULL);
382
383 free_probes(dec->probes);
384 free_probes(dec->opt_probes);
385 g_free(dec->id);
386 g_free(dec->name);
387 g_free(dec->longname);
388 g_free(dec->desc);
389 g_free(dec->license);
390
391 /* The module's Decoder class. */
392 Py_XDECREF(dec->py_dec);
393 /* The module itself. */
394 Py_XDECREF(dec->py_mod);
395
396 /* TODO: (g_)free dec itself? */
397
398 return SRD_OK;
399}
400
401/**
402 * Load all installed protocol decoders.
403 *
404 * @return SRD_OK upon success, a (negative) error code otherwise.
405 */
406SRD_API int srd_decoder_load_all(void)
407{
408 GDir *dir;
409 GError *error;
410 const gchar *direntry;
411
412 if (!(dir = g_dir_open(DECODERS_DIR, 0, &error))) {
413 srd_err("Unable to open %s for reading.", DECODERS_DIR);
414 return SRD_ERR_DECODERS_DIR;
415 }
416
417 while ((direntry = g_dir_read_name(dir)) != NULL) {
418 /* The directory name is the module name (e.g. "i2c"). */
419 srd_decoder_load(direntry);
420 }
421 g_dir_close(dir);
422
423 return SRD_OK;
424}
425
426/**
427 * Unload all loaded protocol decoders.
428 *
429 * @return SRD_OK upon success, a (negative) error code otherwise.
430 */
431SRD_API int srd_decoder_unload_all(void)
432{
433 GSList *l;
434 struct srd_decoder *dec;
435
436 for (l = pd_list; l; l = l->next) {
437 dec = l->data;
438 srd_decoder_unload(dec);
439 }
440
441 return SRD_OK;
442}
443
444/** @} */