]> sigrok.org Git - libsigrokdecode.git/blob - decoder.c
srd: Add SRD_ERR_BUG.
[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) 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"
22 #include "sigrokdecode.h" /* First, so we avoid a _POSIX_C_SOURCE warning. */
23 #include "sigrokdecode-internal.h"
24 #include <dirent.h>
25
26 /* The list of protocol decoders. */
27 GSList *pd_list = NULL;
28 GSList *di_list = NULL;
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  */
38 GSList *srd_list_decoders(void)
39 {
40
41         return pd_list;
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  */
51 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
66 /**
67  * Load a protocol decoder module into the embedded python interpreter.
68  *
69  * @param name The module name to be loaded.
70  * @param dec Pointer to the struct srd_decoder filled with the loaded module.
71  *
72  * @return SRD_OK upon success, a (negative) error code otherwise.
73  */
74 int srd_load_decoder(const char *name, struct srd_decoder **dec)
75 {
76         PyObject *py_mod, *py_res, *py_annlist, *py_ann;
77         struct srd_decoder *d;
78         int alen, r, i;
79         char **ann;
80
81         fprintf(stdout, "%s: %s\n", __func__, name);
82
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? */
87         }
88
89         /* Get the 'Decoder' class as Python object. */
90         py_res = PyObject_GetAttrString(py_mod, "Decoder"); /* NEWREF */
91         if (!py_res) {
92                 if (PyErr_Occurred())
93                         PyErr_Print(); /* Returns void. */
94                 Py_XDECREF(py_mod);
95                 fprintf(stderr, "Decoder class not found in PD module %s\n", name);
96                 return SRD_ERR_PYTHON; /* TODO: More specific error? */
97         }
98
99         if (!(d = malloc(sizeof(struct srd_decoder))))
100                 return SRD_ERR_MALLOC;
101
102         if ((r = h_str(py_res, "id", &(d->id))) < 0)
103                 return r;
104
105         if ((r = h_str(py_res, "name", &(d->name))) < 0)
106                 return r;
107
108         if ((r = h_str(py_res, "longname", &(d->longname))) < 0)
109                 return r;
110
111         if ((r = h_str(py_res, "desc", &(d->desc))) < 0)
112                 return r;
113
114         if ((r = h_str(py_res, "longdesc", &(d->longdesc))) < 0)
115                 return r;
116
117         if ((r = h_str(py_res, "author", &(d->author))) < 0)
118                 return r;
119
120         if ((r = h_str(py_res, "email", &(d->email))) < 0)
121                 return r;
122
123         if ((r = h_str(py_res, "license", &(d->license))) < 0)
124                 return r;
125
126         d->py_mod = py_mod;
127         d->py_decobj = py_res;
128
129         /* TODO: Handle func, inputformats, outputformats. */
130         /* Note: They must at least be set to NULL, will segfault otherwise. */
131         d->func = NULL;
132         d->inputformats = NULL;
133         d->outputformats = NULL;
134
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;
142                 }
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",
148                                                 name, i+1);
149                                 return SRD_ERR_PYTHON;
150                         }
151
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);
155                 }
156         }
157
158         *dec = d;
159
160         return SRD_OK;
161 }
162
163
164 /**
165  * TODO
166  */
167 int srd_unload_decoder(struct srd_decoder *dec)
168 {
169         g_free(dec->id);
170         g_free(dec->name);
171         g_free(dec->desc);
172         g_free(dec->func);
173
174         /* TODO: Free everything in inputformats and outputformats. */
175
176         if (dec->inputformats != NULL)
177                 g_slist_free(dec->inputformats);
178         if (dec->outputformats != NULL)
179                 g_slist_free(dec->outputformats);
180
181         Py_XDECREF(dec->py_decobj);
182         Py_XDECREF(dec->py_mod);
183
184         /* TODO: (g_)free dec itself? */
185
186         return SRD_OK;
187 }
188
189
190 int srd_load_all_decoders(void)
191 {
192         DIR *dir;
193         struct dirent *dp;
194         int ret;
195         char *decodername;
196         struct srd_decoder *dec;
197
198         if (!(dir = opendir(DECODERS_DIR))) {
199                 Py_Finalize(); /* Returns void. */
200                 return SRD_ERR_DECODERS_DIR;
201         }
202
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"))
206                         continue;
207
208                 /* Decoder name == filename (without .py suffix). */
209                 decodername = g_strndup(dp->d_name, strlen(dp->d_name) - 3);
210
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;
215                 }
216
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);
222                 }
223         }
224         closedir(dir);
225
226         return SRD_OK;
227 }
228
229
230 /**
231  * TODO
232  */
233 int srd_unload_all_decoders(void)
234 {
235         GSList *l;
236         struct srd_decoder *dec;
237
238         for (l = srd_list_decoders(); l; l = l->next) {
239                 dec = l->data;
240                 /* TODO: Error handling. */
241                 srd_unload_decoder(dec);
242         }
243
244         return SRD_OK;
245 }
246
247
248