]>
Commit | Line | Data |
---|---|---|
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 ((py_annlist = PyObject_GetAttrString(py_res, "annotation"))) { | |
138 | if (!PyList_Check(py_annlist)) { | |
139 | srd_err("Protocol decoder module %s annotation should be a list", name); | |
140 | return SRD_ERR_PYTHON; | |
141 | } | |
142 | alen = PyList_Size(py_annlist); | |
143 | for (i = 0; i < alen; i++) { | |
144 | py_ann = PyList_GetItem(py_annlist, i); | |
145 | if (!PyList_Check(py_ann) || PyList_Size(py_ann) != 2) { | |
146 | srd_err("Protocol decoder module %s annotation %d should be a list with two elements", | |
147 | name, i+1); | |
148 | return SRD_ERR_PYTHON; | |
149 | } | |
150 | ||
151 | if (py_strlist_to_char(py_ann, &ann) != SRD_OK) | |
152 | return SRD_ERR_PYTHON; | |
153 | d->annotation = g_slist_append(d->annotation, ann); | |
154 | } | |
155 | } | |
156 | ||
157 | *dec = d; | |
158 | ||
159 | return SRD_OK; | |
160 | } | |
161 | ||
162 | ||
163 | /** | |
164 | * TODO | |
165 | */ | |
166 | int srd_unload_decoder(struct srd_decoder *dec) | |
167 | { | |
168 | g_free(dec->id); | |
169 | g_free(dec->name); | |
170 | g_free(dec->desc); | |
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 | ||
189 | int 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. */ | |
220 | pd_list = g_slist_append(pd_list, dec); | |
221 | } | |
222 | } | |
223 | closedir(dir); | |
224 | ||
225 | return SRD_OK; | |
226 | } | |
227 | ||
228 | ||
229 | /** | |
230 | * TODO | |
231 | */ | |
232 | int 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 |