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