]> sigrok.org Git - libsigrokdecode.git/blame - decoder.c
code cleanup
[libsigrokdecode.git] / decoder.c
CommitLineData
b2c19614
BV
1/*
2 * This file is part of the sigrok project.
3 *
4 * Copyright (C) 2010 Uwe Hermann <uwe@hermann-uwe.de>
5 * Copyright (C) 2011 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"
73191416 22#include "sigrokdecode.h" /* First, so we avoid a _POSIX_C_SOURCE warning. */
15969949 23#include "sigrokdecode-internal.h"
b2c19614
BV
24#include <dirent.h>
25
26/* The list of protocol decoders. */
e5080882
BV
27GSList *pd_list = NULL;
28GSList *di_list = NULL;
b2c19614
BV
29
30
31/**
32 * Returns the list of supported/loaded protocol decoders.
33 *
34 * This is a GSList containing the names of the decoders as strings.
35 *
36 * @return List of decoders, NULL if none are supported or loaded.
37 */
38GSList *srd_list_decoders(void)
39{
40
e5080882 41 return pd_list;
b2c19614
BV
42}
43
44
45/**
46 * Get the decoder with the specified ID.
47 *
48 * @param id The ID string of the decoder to return.
49 * @return The decoder with the specified ID, or NULL if not found.
50 */
51struct 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
66/**
15969949 67 * Load a protocol decoder module into the embedded python interpreter.
b2c19614 68 *
15969949
BV
69 * @param name The module name to be loaded.
70 * @param dec Pointer to the struct srd_decoder filled with the loaded module.
b2c19614
BV
71 *
72 * @return SRD_OK upon success, a (negative) error code otherwise.
73 */
74int srd_load_decoder(const char *name, struct srd_decoder **dec)
75{
15969949 76 PyObject *py_mod, *py_res, *py_annlist, *py_ann;
b2c19614 77 struct srd_decoder *d;
15969949
BV
78 int alen, r, i;
79 char **ann;
b2c19614 80
b2c19614
BV
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? */
85 }
86
87 /* Get the 'Decoder' class as Python object. */
88 py_res = PyObject_GetAttrString(py_mod, "Decoder"); /* NEWREF */
89 if (!py_res) {
90 if (PyErr_Occurred())
91 PyErr_Print(); /* Returns void. */
92 Py_XDECREF(py_mod);
b231546d 93 srd_err("Decoder class not found in PD module %s", name);
b2c19614
BV
94 return SRD_ERR_PYTHON; /* TODO: More specific error? */
95 }
96
97 if (!(d = malloc(sizeof(struct srd_decoder))))
98 return SRD_ERR_MALLOC;
99
8b4bbd2a 100 if ((r = h_str(py_res, "id", &(d->id))) < 0)
09b0acbb 101 return r;
b2c19614 102
8b4bbd2a 103 if ((r = h_str(py_res, "name", &(d->name))) < 0)
b2c19614
BV
104 return r;
105
15969949 106 if ((r = h_str(py_res, "longname", &(d->longname))) < 0)
b2c19614
BV
107 return r;
108
8b4bbd2a 109 if ((r = h_str(py_res, "desc", &(d->desc))) < 0)
b2c19614
BV
110 return r;
111
15969949 112 if ((r = h_str(py_res, "longdesc", &(d->longdesc))) < 0)
b2c19614
BV
113 return r;
114
8b4bbd2a 115 if ((r = h_str(py_res, "author", &(d->author))) < 0)
b2c19614
BV
116 return r;
117
8b4bbd2a 118 if ((r = h_str(py_res, "license", &(d->license))) < 0)
b2c19614
BV
119 return r;
120
121 d->py_mod = py_mod;
122 d->py_decobj = py_res;
123
124 /* TODO: Handle func, inputformats, outputformats. */
125 /* Note: They must at least be set to NULL, will segfault otherwise. */
126 d->func = NULL;
127 d->inputformats = NULL;
128 d->outputformats = NULL;
129
15969949
BV
130 /* Convert class annotation attribute to GSList of **char */
131 d->annotation = NULL;
f9afa084
BV
132 if (PyObject_HasAttrString(py_res, "annotation")) {
133 py_annlist = PyObject_GetAttrString(py_res, "annotation");
15969949
BV
134 if (!PyList_Check(py_annlist)) {
135 srd_err("Protocol decoder module %s annotation should be a list", name);
136 return SRD_ERR_PYTHON;
137 }
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",
143 name, i+1);
144 return SRD_ERR_PYTHON;
145 }
146
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);
150 }
151 }
152
b2c19614
BV
153 *dec = d;
154
155 return SRD_OK;
156}
157
158
159/**
160 * TODO
161 */
162int srd_unload_decoder(struct srd_decoder *dec)
163{
164 g_free(dec->id);
165 g_free(dec->name);
b231546d 166 g_free(dec->longname);
b2c19614 167 g_free(dec->desc);
b231546d
BV
168 g_free(dec->longdesc);
169 g_free(dec->author);
170 g_free(dec->license);
b2c19614
BV
171 g_free(dec->func);
172
173 /* TODO: Free everything in inputformats and outputformats. */
174
175 if (dec->inputformats != NULL)
176 g_slist_free(dec->inputformats);
177 if (dec->outputformats != NULL)
178 g_slist_free(dec->outputformats);
179
180 Py_XDECREF(dec->py_decobj);
181 Py_XDECREF(dec->py_mod);
182
183 /* TODO: (g_)free dec itself? */
184
185 return SRD_OK;
186}
187
188
189int srd_load_all_decoders(void)
190{
191 DIR *dir;
192 struct dirent *dp;
193 int ret;
194 char *decodername;
195 struct srd_decoder *dec;
196
197 if (!(dir = opendir(DECODERS_DIR))) {
198 Py_Finalize(); /* Returns void. */
199 return SRD_ERR_DECODERS_DIR;
200 }
201
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"))
205 continue;
206
207 /* Decoder name == filename (without .py suffix). */
208 decodername = g_strndup(dp->d_name, strlen(dp->d_name) - 3);
209
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;
214 }
215
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. */
e5080882 220 pd_list = g_slist_append(pd_list, dec);
b2c19614
BV
221 }
222 }
223 closedir(dir);
224
225 return SRD_OK;
226}
227
228
229/**
230 * TODO
231 */
232int srd_unload_all_decoders(void)
233{
234 GSList *l;
235 struct srd_decoder *dec;
236
237 for (l = srd_list_decoders(); l; l = l->next) {
238 dec = l->data;
239 /* TODO: Error handling. */
240 srd_unload_decoder(dec);
241 }
242
243 return SRD_OK;
244}
245
246
247