2 * This file is part of the sigrok project.
4 * Copyright (C) 2010 Uwe Hermann <uwe@hermann-uwe.de>
5 * Copyright (C) 2012 Bert Vermeulen <bert@biot.com>
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.
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.
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/>.
22 #include "sigrokdecode.h" /* First, so we avoid a _POSIX_C_SOURCE warning. */
23 #include "sigrokdecode-internal.h"
28 /* The list of protocol decoders. */
29 SRD_PRIV GSList *pd_list = NULL;
31 /* module_sigrokdecode.c */
32 extern SRD_PRIV PyObject *mod_sigrokdecode;
37 * Returns the list of supported/loaded protocol decoders.
39 * This is a GSList containing the names of the decoders as strings.
41 * @return List of decoders, NULL if none are supported or loaded.
43 SRD_API const GSList *srd_decoder_list(void)
49 * Get the decoder with the specified ID.
51 * @param id The ID string of the decoder to return.
53 * @return The decoder with the specified ID, or NULL if not found.
55 SRD_API struct srd_decoder *srd_decoder_get_by_id(const char *id)
58 struct srd_decoder *dec;
60 for (l = pd_list; l; l = l->next) {
62 if (!strcmp(dec->id, id))
69 static int get_probes(const struct srd_decoder *d, const char *attr,
72 PyObject *py_probelist, *py_entry;
74 int ret, num_probes, i;
76 if (!PyObject_HasAttrString(d->py_dec, attr))
77 /* No probes of this type specified. */
81 py_probelist = py_entry = NULL;
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 "
86 "a list.", d->name, attr);
90 num_probes = PyList_Size(py_probelist);
92 /* Empty probelist. */
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 "
99 "a list with dict elements.", d->name, attr);
103 if (!(p = g_try_malloc(sizeof(struct srd_probe)))) {
104 srd_err("Failed to g_malloc() struct srd_probe.");
105 ret = SRD_ERR_MALLOC;
109 if ((py_dictitem_as_str(py_entry, "id", &p->id)) != SRD_OK)
111 if ((py_dictitem_as_str(py_entry, "name", &p->name)) != SRD_OK)
113 if ((py_dictitem_as_str(py_entry, "desc", &p->desc)) != SRD_OK)
117 *pl = g_slist_append(*pl, p);
123 Py_DecRef(py_probelist);
129 * Load a protocol decoder module into the embedded Python interpreter.
131 * @param module_name The module name to be loaded.
133 * @return SRD_OK upon success, a (negative) error code otherwise.
135 SRD_API int srd_decoder_load(const char *module_name)
137 PyObject *py_basedec, *py_method, *py_attr, *py_annlist, *py_ann;
138 struct srd_decoder *d;
144 srd_dbg("Loading protocol decoder '%s'.", module_name);
146 py_basedec = py_method = py_attr = NULL;
148 if (!(d = g_try_malloc0(sizeof(struct srd_decoder)))) {
149 srd_dbg("Failed to g_malloc() struct srd_decoder.");
150 ret = SRD_ERR_MALLOC;
154 ret = SRD_ERR_PYTHON;
156 /* Import the Python module. */
157 if (!(d->py_mod = PyImport_ImportModule(module_name))) {
158 srd_exception_catch("Import of '%s' failed.", module_name);
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. */
166 srd_err("Decoder class not found in protocol decoder %s.",
171 if (!(py_basedec = PyObject_GetAttrString(mod_sigrokdecode, "Decoder"))) {
172 srd_dbg("sigrokdecode module not loaded.");
176 if (!PyObject_IsSubclass(d->py_dec, py_basedec)) {
177 srd_err("Decoder class in protocol decoder module %s is not "
178 "a subclass of sigrokdecode.Decoder.", module_name);
181 Py_CLEAR(py_basedec);
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 "
186 "class.", module_name);
189 py_method = PyObject_GetAttrString(d->py_dec, "start");
190 if (!PyFunction_Check(py_method)) {
191 srd_err("Protocol decoder %s Decoder class attribute 'start' "
192 "is not a method.", module_name);
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 "
200 "class.", module_name);
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' "
206 "is not a method.", module_name);
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 "
216 "a dictionary.", d->name);
223 /* Check and import required probes. */
224 if (get_probes(d, "probes", &d->probes) != SRD_OK)
227 /* Check and import optional probes. */
228 if (get_probes(d, "optional_probes", &d->opt_probes) != SRD_OK)
232 * Fix order numbers for the optional probes.
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.
239 for (l = d->opt_probes; l; l = l->next) {
241 p->order += g_slist_length(d->probes);
244 /* Store required fields in newly allocated strings. */
245 if (py_attr_as_str(d->py_dec, "id", &(d->id)) != SRD_OK)
248 if (py_attr_as_str(d->py_dec, "name", &(d->name)) != SRD_OK)
251 if (py_attr_as_str(d->py_dec, "longname", &(d->longname)) != SRD_OK)
254 if (py_attr_as_str(d->py_dec, "desc", &(d->desc)) != SRD_OK)
257 if (py_attr_as_str(d->py_dec, "license", &(d->license)) != SRD_OK)
260 /* Convert class annotation attribute to GSList of **char. */
261 d->annotations = NULL;
262 if (PyObject_HasAttrString(d->py_dec, "annotations")) {
263 py_annlist = PyObject_GetAttrString(d->py_dec, "annotations");
264 if (!PyList_Check(py_annlist)) {
265 srd_err("Protocol decoder module %s annotations "
266 "should be a list.", module_name);
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) {
273 srd_err("Protocol decoder module %s "
274 "annotation %d should be a list with "
275 "two elements.", module_name, i + 1);
279 if (py_strlist_to_char(py_ann, &ann) != SRD_OK) {
282 d->annotations = g_slist_append(d->annotations, ann);
286 /* Append it to the list of supported/loaded decoders. */
287 pd_list = g_slist_append(pd_list, d);
293 Py_XDECREF(py_method);
294 Py_XDECREF(py_basedec);
295 Py_XDECREF(d->py_dec);
296 Py_XDECREF(d->py_mod);
304 * Return a protocol decoder's docstring.
306 * @param dec The loaded protocol decoder.
308 * @return A newly allocated buffer containing the protocol decoder's
309 * documentation. The caller is responsible for free'ing the buffer.
311 SRD_API char *srd_decoder_doc_get(const struct srd_decoder *dec)
316 if (!PyObject_HasAttrString(dec->py_mod, "__doc__"))
319 if (!(py_str = PyObject_GetAttrString(dec->py_mod, "__doc__"))) {
320 srd_exception_catch("");
325 if (py_str != Py_None)
326 py_str_as_str(py_str, &doc);
332 static void free_probes(GSList *probelist)
337 if (probelist == NULL)
340 for (l = probelist; l; l = l->next) {
347 g_slist_free(probelist);
351 * Unload decoder module.
353 * @param dec The struct srd_decoder to be unloaded.
355 * @return SRD_OK upon success, a (negative) error code otherwise.
357 SRD_API int srd_decoder_unload(struct srd_decoder *dec)
359 srd_dbg("Unloading protocol decoder '%s'.", dec->name);
362 * Since any instances of this decoder need to be released as well,
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
365 * instances, and rebuild the stack.
367 srd_inst_free_all(NULL);
369 free_probes(dec->probes);
370 free_probes(dec->opt_probes);
373 g_free(dec->longname);
375 g_free(dec->license);
377 /* The module's Decoder class. */
378 Py_XDECREF(dec->py_dec);
379 /* The module itself. */
380 Py_XDECREF(dec->py_mod);
382 /* TODO: (g_)free dec itself? */
388 * Load all installed protocol decoders.
390 * @return SRD_OK upon success, a (negative) error code otherwise.
392 SRD_API int srd_decoder_load_all(void)
396 const gchar *direntry;
398 if (!(dir = g_dir_open(DECODERS_DIR, 0, &error))) {
399 srd_err("Unable to open %s for reading.", DECODERS_DIR);
400 return SRD_ERR_DECODERS_DIR;
403 while ((direntry = g_dir_read_name(dir)) != NULL) {
404 /* The directory name is the module name (e.g. "i2c"). */
405 srd_decoder_load(direntry);
413 * Unload all loaded protocol decoders.
415 * @return SRD_OK upon success, a (negative) error code otherwise.
417 SRD_API int srd_decoder_unload_all(void)
420 struct srd_decoder *dec;
422 for (l = pd_list; l; l = l->next) {
424 srd_decoder_unload(dec);