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 fprintf(stdout, "%s: %s\n", __func__, name);
83 /* "Import" the Python module. */
84 if (!(py_mod = PyImport_ImportModule(name))) { /* NEWREF */
85 PyErr_Print(); /* Returns void. */
86 return SRD_ERR_PYTHON; /* TODO: More specific error? */
89 /* Get the 'Decoder' class as Python object. */
90 py_res = PyObject_GetAttrString(py_mod, "Decoder"); /* NEWREF */
93 PyErr_Print(); /* Returns void. */
95 fprintf(stderr, "Decoder class not found in PD module %s\n", name);
96 return SRD_ERR_PYTHON; /* TODO: More specific error? */
99 if (!(d = malloc(sizeof(struct srd_decoder))))
100 return SRD_ERR_MALLOC;
102 if ((r = h_str(py_res, "id", &(d->id))) < 0)
105 if ((r = h_str(py_res, "name", &(d->name))) < 0)
108 if ((r = h_str(py_res, "longname", &(d->longname))) < 0)
111 if ((r = h_str(py_res, "desc", &(d->desc))) < 0)
114 if ((r = h_str(py_res, "longdesc", &(d->longdesc))) < 0)
117 if ((r = h_str(py_res, "author", &(d->author))) < 0)
120 if ((r = h_str(py_res, "email", &(d->email))) < 0)
123 if ((r = h_str(py_res, "license", &(d->license))) < 0)
127 d->py_decobj = py_res;
129 /* TODO: Handle func, inputformats, outputformats. */
130 /* Note: They must at least be set to NULL, will segfault otherwise. */
132 d->inputformats = NULL;
133 d->outputformats = NULL;
135 /* Convert class annotation attribute to GSList of **char */
136 d->annotation = NULL;
137 if (PyObject_HasAttrString(py_res, "annotation")) {
138 py_annlist = PyObject_GetAttrString(py_res, "annotation");
139 if (!PyList_Check(py_annlist)) {
140 srd_err("Protocol decoder module %s annotation should be a list", name);
141 return SRD_ERR_PYTHON;
143 alen = PyList_Size(py_annlist);
144 for (i = 0; i < alen; i++) {
145 py_ann = PyList_GetItem(py_annlist, i);
146 if (!PyList_Check(py_ann) || PyList_Size(py_ann) != 2) {
147 srd_err("Protocol decoder module %s annotation %d should be a list with two elements",
149 return SRD_ERR_PYTHON;
152 if (py_strlist_to_char(py_ann, &ann) != SRD_OK)
153 return SRD_ERR_PYTHON;
154 d->annotation = g_slist_append(d->annotation, ann);
167 int srd_unload_decoder(struct srd_decoder *dec)
174 /* TODO: Free everything in inputformats and outputformats. */
176 if (dec->inputformats != NULL)
177 g_slist_free(dec->inputformats);
178 if (dec->outputformats != NULL)
179 g_slist_free(dec->outputformats);
181 Py_XDECREF(dec->py_decobj);
182 Py_XDECREF(dec->py_mod);
184 /* TODO: (g_)free dec itself? */
190 int srd_load_all_decoders(void)
196 struct srd_decoder *dec;
198 if (!(dir = opendir(DECODERS_DIR))) {
199 Py_Finalize(); /* Returns void. */
200 return SRD_ERR_DECODERS_DIR;
203 while ((dp = readdir(dir)) != NULL) {
204 /* Ignore filenames which don't end with ".py". */
205 if (!g_str_has_suffix(dp->d_name, ".py"))
208 /* Decoder name == filename (without .py suffix). */
209 decodername = g_strndup(dp->d_name, strlen(dp->d_name) - 3);
211 /* TODO: Error handling. Use g_try_malloc(). */
212 if (!(dec = malloc(sizeof(struct srd_decoder)))) {
213 Py_Finalize(); /* Returns void. */
214 return SRD_ERR_MALLOC;
217 /* Load the decoder. */
218 /* TODO: Warning if loading fails for a decoder. */
219 if ((ret = srd_load_decoder(decodername, &dec)) == SRD_OK) {
220 /* Append it to the list of supported/loaded decoders. */
221 pd_list = g_slist_append(pd_list, dec);
233 int srd_unload_all_decoders(void)
236 struct srd_decoder *dec;
238 for (l = srd_list_decoders(); l; l = l->next) {
240 /* TODO: Error handling. */
241 srd_unload_decoder(dec);