2 * This file is part of the sigrok project.
4 * Copyright (C) 2010 Uwe Hermann <uwe@hermann-uwe.de>
5 * Copyright (C) 2011 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"
26 /* The list of protocol decoders. */
27 GSList *pd_list = NULL;
28 GSList *di_list = NULL;
32 * Returns the list of supported/loaded protocol decoders.
34 * This is a GSList containing the names of the decoders as strings.
36 * @return List of decoders, NULL if none are supported or loaded.
38 GSList *srd_list_decoders(void)
46 * Get the decoder with the specified ID.
48 * @param id The ID string of the decoder to return.
49 * @return The decoder with the specified ID, or NULL if not found.
51 struct srd_decoder *srd_get_decoder_by_id(const char *id)
54 struct srd_decoder *dec;
56 for (l = srd_list_decoders(); l; l = l->next) {
58 if (!strcmp(dec->id, id))
67 * Load a protocol decoder module into the embedded python interpreter.
69 * @param name The module name to be loaded.
70 * @param dec Pointer to the struct srd_decoder filled with the loaded module.
72 * @return SRD_OK upon success, a (negative) error code otherwise.
74 int srd_load_decoder(const char *name, struct srd_decoder **dec)
76 PyObject *py_mod, *py_res, *py_annlist, *py_ann;
77 struct srd_decoder *d;
81 /* "Import" the Python module. */
82 if (!(py_mod = PyImport_ImportModule(name))) { /* NEWREF */
83 PyErr_Print(); /* Returns void. */
84 return SRD_ERR_PYTHON; /* TODO: More specific error? */
87 /* Get the 'Decoder' class as Python object. */
88 py_res = PyObject_GetAttrString(py_mod, "Decoder"); /* NEWREF */
91 PyErr_Print(); /* Returns void. */
93 srd_err("Decoder class not found in PD module %s", name);
94 return SRD_ERR_PYTHON; /* TODO: More specific error? */
97 if (!(d = malloc(sizeof(struct srd_decoder))))
98 return SRD_ERR_MALLOC;
100 if ((r = h_str(py_res, "id", &(d->id))) < 0)
103 if ((r = h_str(py_res, "name", &(d->name))) < 0)
106 if ((r = h_str(py_res, "longname", &(d->longname))) < 0)
109 if ((r = h_str(py_res, "desc", &(d->desc))) < 0)
112 if ((r = h_str(py_res, "longdesc", &(d->longdesc))) < 0)
115 if ((r = h_str(py_res, "author", &(d->author))) < 0)
118 if ((r = h_str(py_res, "license", &(d->license))) < 0)
122 d->py_decobj = py_res;
124 /* TODO: Handle func, inputformats, outputformats. */
125 /* Note: They must at least be set to NULL, will segfault otherwise. */
127 d->inputformats = NULL;
128 d->outputformats = NULL;
130 /* Convert class annotation attribute to GSList of **char */
131 d->annotation = NULL;
132 if (PyObject_HasAttrString(py_res, "annotation")) {
133 py_annlist = PyObject_GetAttrString(py_res, "annotation");
134 if (!PyList_Check(py_annlist)) {
135 srd_err("Protocol decoder module %s annotation should be a list", name);
136 return SRD_ERR_PYTHON;
138 alen = PyList_Size(py_annlist);
139 for (i = 0; i < alen; i++) {
140 py_ann = PyList_GetItem(py_annlist, i);
141 if (!PyList_Check(py_ann) || PyList_Size(py_ann) != 2) {
142 srd_err("Protocol decoder module %s annotation %d should be a list with two elements",
144 return SRD_ERR_PYTHON;
147 if (py_strlist_to_char(py_ann, &ann) != SRD_OK)
148 return SRD_ERR_PYTHON;
149 d->annotation = g_slist_append(d->annotation, ann);
162 int srd_unload_decoder(struct srd_decoder *dec)
166 g_free(dec->longname);
168 g_free(dec->longdesc);
170 g_free(dec->license);
173 /* TODO: Free everything in inputformats and outputformats. */
175 if (dec->inputformats != NULL)
176 g_slist_free(dec->inputformats);
177 if (dec->outputformats != NULL)
178 g_slist_free(dec->outputformats);
180 Py_XDECREF(dec->py_decobj);
181 Py_XDECREF(dec->py_mod);
183 /* TODO: (g_)free dec itself? */
189 int srd_load_all_decoders(void)
195 struct srd_decoder *dec;
197 if (!(dir = opendir(DECODERS_DIR))) {
198 Py_Finalize(); /* Returns void. */
199 return SRD_ERR_DECODERS_DIR;
202 while ((dp = readdir(dir)) != NULL) {
203 /* Ignore filenames which don't end with ".py". */
204 if (!g_str_has_suffix(dp->d_name, ".py"))
207 /* Decoder name == filename (without .py suffix). */
208 decodername = g_strndup(dp->d_name, strlen(dp->d_name) - 3);
210 /* TODO: Error handling. Use g_try_malloc(). */
211 if (!(dec = malloc(sizeof(struct srd_decoder)))) {
212 Py_Finalize(); /* Returns void. */
213 return SRD_ERR_MALLOC;
216 /* Load the decoder. */
217 /* TODO: Warning if loading fails for a decoder. */
218 if ((ret = srd_load_decoder(decodername, &dec)) == SRD_OK) {
219 /* Append it to the list of supported/loaded decoders. */
220 pd_list = g_slist_append(pd_list, dec);
232 int srd_unload_all_decoders(void)
235 struct srd_decoder *dec;
237 for (l = srd_list_decoders(); l; l = l->next) {
239 /* TODO: Error handling. */
240 srd_unload_decoder(dec);