]>
Commit | Line | Data |
---|---|---|
b2c19614 | 1 | /* |
50bd5d25 | 2 | * This file is part of the libsigrokdecode project. |
b2c19614 BV |
3 | * |
4 | * Copyright (C) 2010 Uwe Hermann <uwe@hermann-uwe.de> | |
4fadb128 | 5 | * Copyright (C) 2012 Bert Vermeulen <bert@biot.com> |
b2c19614 BV |
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" | |
c1f86f02 UH |
22 | #include "libsigrokdecode.h" /* First, so we avoid a _POSIX_C_SOURCE warning. */ |
23 | #include "libsigrokdecode-internal.h" | |
eb2bbd66 | 24 | #include <glib.h> |
b2c19614 | 25 | |
54fdeeef UH |
26 | /** |
27 | * @file | |
28 | * | |
29 | * Listing, loading, unloading, and handling protocol decoders. | |
30 | */ | |
31 | ||
4895418c UH |
32 | /** |
33 | * @defgroup grp_decoder Protocol decoders | |
34 | * | |
35 | * Handling protocol decoders. | |
36 | * | |
37 | * @{ | |
38 | */ | |
39 | ||
57790bc8 UH |
40 | /** @cond PRIVATE */ |
41 | ||
b2c19614 | 42 | /* The list of protocol decoders. */ |
55c3c5f4 | 43 | SRD_PRIV GSList *pd_list = NULL; |
b2c19614 | 44 | |
ea81b49a BV |
45 | /* srd.c */ |
46 | extern GSList *searchpaths; | |
47 | ||
48 | /* session.c */ | |
32cfb920 BV |
49 | extern GSList *sessions; |
50 | ||
c9bfccc6 | 51 | /* module_sigrokdecode.c */ |
55c3c5f4 | 52 | extern SRD_PRIV PyObject *mod_sigrokdecode; |
451680f1 | 53 | |
57790bc8 UH |
54 | /** @endcond */ |
55 | ||
b2c19614 BV |
56 | /** |
57 | * Returns the list of supported/loaded protocol decoders. | |
58 | * | |
59 | * This is a GSList containing the names of the decoders as strings. | |
60 | * | |
61 | * @return List of decoders, NULL if none are supported or loaded. | |
8c664ca2 UH |
62 | * |
63 | * @since 0.1.0 (but the API changed in 0.2.0) | |
b2c19614 | 64 | */ |
38ff5046 | 65 | SRD_API const GSList *srd_decoder_list(void) |
b2c19614 | 66 | { |
e5080882 | 67 | return pd_list; |
b2c19614 BV |
68 | } |
69 | ||
b2c19614 BV |
70 | /** |
71 | * Get the decoder with the specified ID. | |
72 | * | |
73 | * @param id The ID string of the decoder to return. | |
582c8473 | 74 | * |
b2c19614 | 75 | * @return The decoder with the specified ID, or NULL if not found. |
8c664ca2 UH |
76 | * |
77 | * @since 0.1.0 | |
b2c19614 | 78 | */ |
9d122fd5 | 79 | SRD_API struct srd_decoder *srd_decoder_get_by_id(const char *id) |
b2c19614 BV |
80 | { |
81 | GSList *l; | |
82 | struct srd_decoder *dec; | |
83 | ||
38ff5046 | 84 | for (l = pd_list; l; l = l->next) { |
b2c19614 BV |
85 | dec = l->data; |
86 | if (!strcmp(dec->id, id)) | |
87 | return dec; | |
88 | } | |
89 | ||
90 | return NULL; | |
91 | } | |
92 | ||
5a2c4dc4 | 93 | static int get_probes(const struct srd_decoder *d, const char *attr, |
22c9bc2a | 94 | GSList **pl) |
f38ec285 BV |
95 | { |
96 | PyObject *py_probelist, *py_entry; | |
97 | struct srd_probe *p; | |
98 | int ret, num_probes, i; | |
99 | ||
100 | if (!PyObject_HasAttrString(d->py_dec, attr)) | |
101 | /* No probes of this type specified. */ | |
102 | return SRD_OK; | |
103 | ||
f38ec285 | 104 | py_probelist = PyObject_GetAttrString(d->py_dec, attr); |
da9bcbd9 BV |
105 | if (!PyTuple_Check(py_probelist)) { |
106 | srd_err("Protocol decoder %s %s attribute is not a tuple.", | |
22c9bc2a BV |
107 | d->name, attr); |
108 | return SRD_ERR_PYTHON; | |
f38ec285 BV |
109 | } |
110 | ||
da9bcbd9 | 111 | if ((num_probes = PyTuple_Size(py_probelist)) == 0) |
f38ec285 BV |
112 | /* Empty probelist. */ |
113 | return SRD_OK; | |
114 | ||
22c9bc2a | 115 | ret = SRD_OK; |
f38ec285 | 116 | for (i = 0; i < num_probes; i++) { |
da9bcbd9 | 117 | py_entry = PyTuple_GetItem(py_probelist, i); |
f38ec285 BV |
118 | if (!PyDict_Check(py_entry)) { |
119 | srd_err("Protocol decoder %s %s attribute is not " | |
c9bfccc6 | 120 | "a list with dict elements.", d->name, attr); |
22c9bc2a BV |
121 | ret = SRD_ERR_PYTHON; |
122 | break; | |
f38ec285 BV |
123 | } |
124 | ||
125 | if (!(p = g_try_malloc(sizeof(struct srd_probe)))) { | |
a61ece20 | 126 | srd_err("Failed to g_malloc() struct srd_probe."); |
f38ec285 | 127 | ret = SRD_ERR_MALLOC; |
22c9bc2a | 128 | break; |
f38ec285 BV |
129 | } |
130 | ||
22c9bc2a BV |
131 | if ((py_dictitem_as_str(py_entry, "id", &p->id)) != SRD_OK) { |
132 | ret = SRD_ERR_PYTHON; | |
133 | break; | |
134 | } | |
135 | if ((py_dictitem_as_str(py_entry, "name", &p->name)) != SRD_OK) { | |
136 | ret = SRD_ERR_PYTHON; | |
137 | break; | |
138 | } | |
139 | if ((py_dictitem_as_str(py_entry, "desc", &p->desc)) != SRD_OK) { | |
140 | ret = SRD_ERR_PYTHON; | |
141 | break; | |
142 | } | |
f38ec285 BV |
143 | p->order = i; |
144 | ||
145 | *pl = g_slist_append(*pl, p); | |
146 | } | |
f38ec285 | 147 | |
f38ec285 BV |
148 | Py_DecRef(py_probelist); |
149 | ||
150 | return ret; | |
151 | } | |
152 | ||
2f395bff BV |
153 | static int get_options(struct srd_decoder *d) |
154 | { | |
84c1c0b5 BV |
155 | PyObject *py_opts, *py_opt, *py_val, *py_default, *py_item; |
156 | Py_ssize_t opt, i; | |
2f395bff | 157 | struct srd_decoder_option *o; |
84c1c0b5 BV |
158 | GVariant *gvar; |
159 | gint64 lval; | |
160 | double dval; | |
161 | int overflow; | |
162 | char *sval; | |
2f395bff BV |
163 | |
164 | if (!PyObject_HasAttrString(d->py_dec, "options")) | |
84c1c0b5 | 165 | /* No options, that's fine. */ |
2f395bff BV |
166 | return SRD_OK; |
167 | ||
84c1c0b5 | 168 | /* If present, options must be a tuple. */ |
2f395bff | 169 | py_opts = PyObject_GetAttrString(d->py_dec, "options"); |
84c1c0b5 BV |
170 | if (!PyTuple_Check(py_opts)) { |
171 | srd_err("Protocol decoder %s: options attribute is not " | |
172 | "a tuple.", d->id); | |
173 | return SRD_ERR_PYTHON; | |
2f395bff BV |
174 | } |
175 | ||
84c1c0b5 BV |
176 | for (opt = 0; opt < PyTuple_Size(py_opts); opt++) { |
177 | py_opt = PyTuple_GetItem(py_opts, opt); | |
178 | if (!PyDict_Check(py_opt)) { | |
179 | srd_err("Protocol decoder %s options: each option " | |
180 | "must consist of a dictionary.", d->name); | |
181 | return SRD_ERR_PYTHON; | |
2f395bff | 182 | } |
84c1c0b5 BV |
183 | if (!(py_val = PyDict_GetItemString(py_opt, "id"))) { |
184 | srd_err("Protocol decoder %s option %d has no " | |
185 | "id.", d->name); | |
186 | return SRD_ERR_PYTHON; | |
2f395bff | 187 | } |
84c1c0b5 BV |
188 | o = g_malloc0(sizeof(struct srd_decoder_option)); |
189 | py_str_as_str(py_val, &o->id); | |
190 | ||
191 | if ((py_val = PyDict_GetItemString(py_opt, "desc"))) | |
192 | py_str_as_str(py_val, &o->desc); | |
193 | ||
194 | if ((py_default = PyDict_GetItemString(py_opt, "default"))) { | |
195 | if (PyUnicode_Check(py_default)) { | |
196 | /* UTF-8 string */ | |
197 | py_str_as_str(py_default, &sval); | |
198 | o->def = g_variant_new_string(sval); | |
199 | g_free(sval); | |
200 | } else if (PyLong_Check(py_default)) { | |
201 | /* Long */ | |
202 | lval = PyLong_AsLongAndOverflow(py_default, &overflow); | |
203 | if (overflow) { | |
204 | /* Value is < LONG_MIN or > LONG_MAX */ | |
205 | PyErr_Clear(); | |
206 | srd_err("Protocol decoder %s option 'default' has " | |
207 | "invalid default value.", d->name); | |
208 | return SRD_ERR_PYTHON; | |
209 | } | |
210 | o->def = g_variant_new_int64(lval); | |
211 | } else if (PyFloat_Check(py_default)) { | |
212 | /* Float */ | |
213 | if ((dval = PyFloat_AsDouble(py_default)) == -1.0) { | |
214 | PyErr_Clear(); | |
215 | srd_err("Protocol decoder %s option 'default' has " | |
216 | "invalid default value.", d->name); | |
217 | return SRD_ERR_PYTHON; | |
218 | } | |
219 | o->def = g_variant_new_double(dval); | |
220 | } else { | |
221 | srd_err("Protocol decoder %s option 'default' has " | |
222 | "value of unsupported type '%s'.", d->name, | |
223 | Py_TYPE(py_default)->tp_name); | |
224 | return SRD_ERR_PYTHON; | |
225 | } | |
226 | g_variant_ref_sink(o->def); | |
2f395bff | 227 | } |
84c1c0b5 BV |
228 | |
229 | if ((py_val = PyDict_GetItemString(py_opt, "values"))) { | |
230 | /* A default is required if a list of values is | |
231 | * given, since it's used to verify their type. */ | |
232 | if (!o->def) { | |
233 | srd_err("No default for option '%s'", o->id); | |
234 | return SRD_ERR_PYTHON; | |
235 | } | |
236 | if (!PyTuple_Check(py_val)) { | |
237 | srd_err("Option '%s' values should be a tuple.", o->id); | |
238 | return SRD_ERR_PYTHON; | |
239 | } | |
240 | for (i = 0; i < PyTuple_Size(py_val); i++) { | |
241 | py_item = PyTuple_GetItem(py_val, i); | |
242 | if (Py_TYPE(py_default) != Py_TYPE(py_item)) { | |
243 | srd_err("All values for option '%s' must be " | |
244 | "of the same type as the default.", | |
245 | o->id); | |
246 | return SRD_ERR_PYTHON; | |
247 | } | |
248 | if (PyUnicode_Check(py_item)) { | |
249 | /* UTF-8 string */ | |
250 | py_str_as_str(py_item, &sval); | |
251 | gvar = g_variant_new_string(sval); | |
252 | g_variant_ref_sink(gvar); | |
253 | g_free(sval); | |
254 | o->values = g_slist_append(o->values, gvar); | |
255 | } else if (PyLong_Check(py_item)) { | |
256 | /* Long */ | |
65f6eb77 | 257 | lval = PyLong_AsLongAndOverflow(py_item, &overflow); |
84c1c0b5 BV |
258 | if (overflow) { |
259 | /* Value is < LONG_MIN or > LONG_MAX */ | |
260 | PyErr_Clear(); | |
261 | srd_err("Protocol decoder %s option 'values' " | |
262 | "has invalid value.", d->name); | |
263 | return SRD_ERR_PYTHON; | |
264 | } | |
265 | gvar = g_variant_new_int64(lval); | |
266 | g_variant_ref_sink(gvar); | |
267 | o->values = g_slist_append(o->values, gvar); | |
65f6eb77 | 268 | } else if (PyFloat_Check(py_item)) { |
84c1c0b5 | 269 | /* Float */ |
65f6eb77 | 270 | if ((dval = PyFloat_AsDouble(py_item)) == -1.0) { |
84c1c0b5 BV |
271 | PyErr_Clear(); |
272 | srd_err("Protocol decoder %s option 'default' has " | |
273 | "invalid default value.", d->name); | |
274 | return SRD_ERR_PYTHON; | |
275 | } | |
276 | gvar = g_variant_new_double(dval); | |
277 | g_variant_ref_sink(gvar); | |
278 | o->values = g_slist_append(o->values, gvar); | |
279 | } | |
2f395bff | 280 | } |
2f395bff | 281 | } |
2f395bff BV |
282 | d->options = g_slist_append(d->options, o); |
283 | } | |
2f395bff | 284 | |
84c1c0b5 | 285 | return SRD_OK; |
2f395bff BV |
286 | } |
287 | ||
b2c19614 | 288 | /** |
64c29e28 | 289 | * Load a protocol decoder module into the embedded Python interpreter. |
b2c19614 | 290 | * |
38ff5046 | 291 | * @param module_name The module name to be loaded. |
b2c19614 BV |
292 | * |
293 | * @return SRD_OK upon success, a (negative) error code otherwise. | |
8c664ca2 UH |
294 | * |
295 | * @since 0.1.0 | |
b2c19614 | 296 | */ |
9d122fd5 | 297 | SRD_API int srd_decoder_load(const char *module_name) |
b2c19614 | 298 | { |
1ce46817 UH |
299 | PyObject *py_basedec, *py_method, *py_attr, *py_annlist, *py_ann; |
300 | PyObject *py_bin_classes, *py_bin_class, *py_ann_rows, *py_ann_row; | |
301 | PyObject *py_ann_classes, *py_long; | |
b2c19614 | 302 | struct srd_decoder *d; |
1ce46817 UH |
303 | int ret, i, j; |
304 | char **ann, **bin, *ann_row_id, *ann_row_desc; | |
38ff5046 | 305 | struct srd_probe *p; |
1ce46817 UH |
306 | GSList *l, *ann_classes; |
307 | struct srd_decoder_annotation_row *ann_row; | |
b2c19614 | 308 | |
e195c025 BV |
309 | if (!srd_check_init()) |
310 | return SRD_ERR; | |
311 | ||
8a014683 BV |
312 | if (!module_name) |
313 | return SRD_ERR_ARG; | |
314 | ||
aac68131 BV |
315 | if (PyDict_GetItemString(PyImport_GetModuleDict(), module_name)) { |
316 | /* Module was already imported. */ | |
317 | return SRD_OK; | |
318 | } | |
319 | ||
8ad6e500 | 320 | srd_dbg("Loading protocol decoder '%s'.", module_name); |
64c29e28 | 321 | |
d906d3f9 | 322 | py_basedec = py_method = py_attr = NULL; |
b2c19614 | 323 | |
451680f1 | 324 | if (!(d = g_try_malloc0(sizeof(struct srd_decoder)))) { |
a61ece20 | 325 | srd_dbg("Failed to g_malloc() struct srd_decoder."); |
451680f1 BV |
326 | ret = SRD_ERR_MALLOC; |
327 | goto err_out; | |
b2c19614 BV |
328 | } |
329 | ||
21481b66 BV |
330 | ret = SRD_ERR_PYTHON; |
331 | ||
451680f1 | 332 | /* Import the Python module. */ |
9b37109d | 333 | if (!(d->py_mod = PyImport_ImportModule(module_name))) { |
aafeeaea | 334 | srd_exception_catch("Import of '%s' failed.", module_name); |
451680f1 BV |
335 | goto err_out; |
336 | } | |
b2c19614 | 337 | |
451680f1 BV |
338 | /* Get the 'Decoder' class as Python object. */ |
339 | if (!(d->py_dec = PyObject_GetAttrString(d->py_mod, "Decoder"))) { | |
340 | /* This generated an AttributeError exception. */ | |
341 | PyErr_Clear(); | |
361fdcaa UH |
342 | srd_err("Decoder class not found in protocol decoder %s.", |
343 | module_name); | |
451680f1 BV |
344 | goto err_out; |
345 | } | |
b2c19614 | 346 | |
451680f1 | 347 | if (!(py_basedec = PyObject_GetAttrString(mod_sigrokdecode, "Decoder"))) { |
7a1712c4 | 348 | srd_dbg("sigrokdecode module not loaded."); |
451680f1 BV |
349 | goto err_out; |
350 | } | |
b2c19614 | 351 | |
451680f1 BV |
352 | if (!PyObject_IsSubclass(d->py_dec, py_basedec)) { |
353 | srd_err("Decoder class in protocol decoder module %s is not " | |
9b37109d | 354 | "a subclass of sigrokdecode.Decoder.", module_name); |
451680f1 BV |
355 | goto err_out; |
356 | } | |
ad022d94 | 357 | Py_CLEAR(py_basedec); |
b2c19614 | 358 | |
1d552cd3 BV |
359 | /* Check for a proper start() method. */ |
360 | if (!PyObject_HasAttrString(d->py_dec, "start")) { | |
361 | srd_err("Protocol decoder %s has no start() method Decoder " | |
9b37109d | 362 | "class.", module_name); |
1d552cd3 BV |
363 | goto err_out; |
364 | } | |
365 | py_method = PyObject_GetAttrString(d->py_dec, "start"); | |
366 | if (!PyFunction_Check(py_method)) { | |
d906d3f9 | 367 | srd_err("Protocol decoder %s Decoder class attribute 'start' " |
9b37109d | 368 | "is not a method.", module_name); |
1d552cd3 BV |
369 | goto err_out; |
370 | } | |
ad022d94 | 371 | Py_CLEAR(py_method); |
1d552cd3 BV |
372 | |
373 | /* Check for a proper decode() method. */ | |
374 | if (!PyObject_HasAttrString(d->py_dec, "decode")) { | |
375 | srd_err("Protocol decoder %s has no decode() method Decoder " | |
9b37109d | 376 | "class.", module_name); |
1d552cd3 BV |
377 | goto err_out; |
378 | } | |
379 | py_method = PyObject_GetAttrString(d->py_dec, "decode"); | |
380 | if (!PyFunction_Check(py_method)) { | |
381 | srd_err("Protocol decoder %s Decoder class attribute 'decode' " | |
9b37109d | 382 | "is not a method.", module_name); |
1d552cd3 BV |
383 | goto err_out; |
384 | } | |
ad022d94 | 385 | Py_CLEAR(py_method); |
1d552cd3 | 386 | |
84c1c0b5 BV |
387 | /* Store required fields in newly allocated strings. */ |
388 | if (py_attr_as_str(d->py_dec, "id", &(d->id)) != SRD_OK) | |
389 | goto err_out; | |
390 | ||
391 | if (py_attr_as_str(d->py_dec, "name", &(d->name)) != SRD_OK) | |
392 | goto err_out; | |
393 | ||
394 | if (py_attr_as_str(d->py_dec, "longname", &(d->longname)) != SRD_OK) | |
395 | goto err_out; | |
396 | ||
397 | if (py_attr_as_str(d->py_dec, "desc", &(d->desc)) != SRD_OK) | |
398 | goto err_out; | |
399 | ||
400 | if (py_attr_as_str(d->py_dec, "license", &(d->license)) != SRD_OK) | |
401 | goto err_out; | |
402 | ||
403 | /* All options and their default values. */ | |
2f395bff BV |
404 | if (get_options(d) != SRD_OK) |
405 | goto err_out; | |
11ea8ae1 | 406 | |
f38ec285 BV |
407 | /* Check and import required probes. */ |
408 | if (get_probes(d, "probes", &d->probes) != SRD_OK) | |
409 | goto err_out; | |
410 | ||
411 | /* Check and import optional probes. */ | |
dcdf4883 | 412 | if (get_probes(d, "optional_probes", &d->opt_probes) != SRD_OK) |
f38ec285 BV |
413 | goto err_out; |
414 | ||
38ff5046 UH |
415 | /* |
416 | * Fix order numbers for the optional probes. | |
417 | * | |
418 | * Example: | |
419 | * Required probes: r1, r2, r3. Optional: o1, o2, o3, o4. | |
420 | * 'order' fields in the d->probes list = 0, 1, 2. | |
421 | * 'order' fields in the d->opt_probes list = 3, 4, 5, 6. | |
422 | */ | |
423 | for (l = d->opt_probes; l; l = l->next) { | |
424 | p = l->data; | |
425 | p->order += g_slist_length(d->probes); | |
426 | } | |
427 | ||
d75d8a7c | 428 | /* Convert annotation class attribute to GSList of char **. */ |
e97b6ef5 | 429 | d->annotations = NULL; |
451680f1 BV |
430 | if (PyObject_HasAttrString(d->py_dec, "annotations")) { |
431 | py_annlist = PyObject_GetAttrString(d->py_dec, "annotations"); | |
da9bcbd9 | 432 | if (!PyTuple_Check(py_annlist)) { |
1f2275dd | 433 | srd_err("Protocol decoder %s annotations should " |
da9bcbd9 | 434 | "be a tuple.", module_name); |
451680f1 | 435 | goto err_out; |
15969949 | 436 | } |
da9bcbd9 BV |
437 | for (i = 0; i < PyTuple_Size(py_annlist); i++) { |
438 | py_ann = PyTuple_GetItem(py_annlist, i); | |
439 | if (!PyTuple_Check(py_ann) || PyTuple_Size(py_ann) != 2) { | |
1f2275dd | 440 | srd_err("Protocol decoder %s annotation %d should " |
da9bcbd9 | 441 | "be a tuple with two elements.", module_name, i + 1); |
451680f1 | 442 | goto err_out; |
15969949 BV |
443 | } |
444 | ||
62a2b15c | 445 | if (py_strseq_to_char(py_ann, &ann) != SRD_OK) { |
451680f1 BV |
446 | goto err_out; |
447 | } | |
e97b6ef5 | 448 | d->annotations = g_slist_append(d->annotations, ann); |
15969949 BV |
449 | } |
450 | } | |
451 | ||
1ce46817 UH |
452 | /* Convert annotation_rows to GSList of 'struct srd_decoder_annotation_row'. */ |
453 | d->annotation_rows = NULL; | |
454 | if (PyObject_HasAttrString(d->py_dec, "annotation_rows")) { | |
455 | py_ann_rows = PyObject_GetAttrString(d->py_dec, "annotation_rows"); | |
456 | if (!PyTuple_Check(py_ann_rows)) { | |
457 | srd_err("Protocol decoder %s annotation row list " | |
458 | "must be a tuple.", module_name); | |
459 | goto err_out; | |
460 | } | |
461 | for (i = 0; i < PyTuple_Size(py_ann_rows); i++) { | |
462 | py_ann_row = PyTuple_GetItem(py_ann_rows, i); | |
463 | if (!PyTuple_Check(py_ann_row)) { | |
464 | srd_err("Protocol decoder %s annotation rows " | |
465 | "must be tuples.", module_name); | |
466 | goto err_out; | |
467 | } | |
468 | if (PyTuple_Size(py_ann_row) != 3 | |
469 | || !PyUnicode_Check(PyTuple_GetItem(py_ann_row, 0)) | |
470 | || !PyUnicode_Check(PyTuple_GetItem(py_ann_row, 1)) | |
471 | || !PyTuple_Check(PyTuple_GetItem(py_ann_row, 2))) { | |
472 | srd_err("Protocol decoder %s annotation rows " | |
473 | "must contain tuples containing two " | |
474 | "strings and a tuple.", module_name); | |
475 | goto err_out; | |
476 | } | |
477 | ||
478 | if (py_str_as_str(PyTuple_GetItem(py_ann_row, 0), &ann_row_id) != SRD_OK) | |
479 | goto err_out; | |
480 | ||
481 | if (py_str_as_str(PyTuple_GetItem(py_ann_row, 1), &ann_row_desc) != SRD_OK) | |
482 | goto err_out; | |
483 | ||
484 | py_ann_classes = PyTuple_GetItem(py_ann_row, 2); | |
485 | ann_classes = NULL; | |
486 | for (j = 0; j < PyTuple_Size(py_ann_classes); j++) { | |
487 | py_long = PyTuple_GetItem(py_ann_classes, j); | |
488 | if (!PyLong_Check(py_long)) { | |
489 | srd_err("Protocol decoder %s annotation row class " | |
490 | "list must only contain numbers.", module_name); | |
491 | goto err_out; | |
492 | } | |
493 | ann_classes = g_slist_append(ann_classes, | |
494 | GINT_TO_POINTER(PyLong_AsLong(py_long))); | |
495 | } | |
496 | ||
497 | ann_row = g_malloc0(sizeof(struct srd_decoder_annotation_row)); | |
498 | ann_row->id = ann_row_id; | |
499 | ann_row->desc = ann_row_desc; | |
500 | ann_row->ann_classes = ann_classes; | |
501 | d->annotation_rows = g_slist_append(d->annotation_rows, ann_row); | |
502 | } | |
503 | } | |
504 | ||
d75d8a7c BV |
505 | /* Convert binary class to GSList of char *. */ |
506 | d->binary = NULL; | |
507 | if (PyObject_HasAttrString(d->py_dec, "binary")) { | |
508 | py_bin_classes = PyObject_GetAttrString(d->py_dec, "binary"); | |
509 | if (!PyTuple_Check(py_bin_classes)) { | |
1f2275dd BV |
510 | srd_err("Protocol decoder %s binary classes should " |
511 | "be a tuple.", module_name); | |
d75d8a7c BV |
512 | goto err_out; |
513 | } | |
1ce46817 | 514 | for (i = 0; i < PyTuple_Size(py_bin_classes); i++) { |
d75d8a7c | 515 | py_bin_class = PyTuple_GetItem(py_bin_classes, i); |
1f2275dd BV |
516 | if (!PyTuple_Check(py_bin_class)) { |
517 | srd_err("Protocol decoder %s binary classes " | |
518 | "should consist of tuples.", module_name); | |
519 | goto err_out; | |
520 | } | |
521 | if (PyTuple_Size(py_bin_class) != 2 | |
522 | || !PyUnicode_Check(PyTuple_GetItem(py_bin_class, 0)) | |
523 | || !PyUnicode_Check(PyTuple_GetItem(py_bin_class, 1))) { | |
524 | srd_err("Protocol decoder %s binary classes should " | |
525 | "contain tuples with two strings.", module_name); | |
d75d8a7c BV |
526 | goto err_out; |
527 | } | |
528 | ||
1f2275dd | 529 | if (py_strseq_to_char(py_bin_class, &bin) != SRD_OK) { |
d75d8a7c BV |
530 | goto err_out; |
531 | } | |
532 | d->binary = g_slist_append(d->binary, bin); | |
533 | } | |
534 | } | |
535 | ||
9b37109d BV |
536 | /* Append it to the list of supported/loaded decoders. */ |
537 | pd_list = g_slist_append(pd_list, d); | |
538 | ||
451680f1 BV |
539 | ret = SRD_OK; |
540 | ||
541 | err_out: | |
542 | if (ret != SRD_OK) { | |
1d552cd3 | 543 | Py_XDECREF(py_method); |
451680f1 BV |
544 | Py_XDECREF(py_basedec); |
545 | Py_XDECREF(d->py_dec); | |
546 | Py_XDECREF(d->py_mod); | |
547 | g_free(d); | |
548 | } | |
b2c19614 | 549 | |
451680f1 BV |
550 | return ret; |
551 | } | |
552 | ||
582c8473 BV |
553 | /** |
554 | * Return a protocol decoder's docstring. | |
555 | * | |
556 | * @param dec The loaded protocol decoder. | |
557 | * | |
361fdcaa | 558 | * @return A newly allocated buffer containing the protocol decoder's |
abeeed8b | 559 | * documentation. The caller is responsible for free'ing the buffer. |
8c664ca2 UH |
560 | * |
561 | * @since 0.1.0 | |
582c8473 | 562 | */ |
b33b8aa5 | 563 | SRD_API char *srd_decoder_doc_get(const struct srd_decoder *dec) |
451680f1 | 564 | { |
e7edca07 | 565 | PyObject *py_str; |
451680f1 BV |
566 | char *doc; |
567 | ||
e195c025 BV |
568 | if (!srd_check_init()) |
569 | return NULL; | |
570 | ||
571 | if (!dec) | |
572 | return NULL; | |
573 | ||
e7edca07 BV |
574 | if (!PyObject_HasAttrString(dec->py_mod, "__doc__")) |
575 | return NULL; | |
576 | ||
577 | if (!(py_str = PyObject_GetAttrString(dec->py_mod, "__doc__"))) { | |
aafeeaea | 578 | srd_exception_catch(""); |
e7edca07 BV |
579 | return NULL; |
580 | } | |
581 | ||
451680f1 | 582 | doc = NULL; |
e7edca07 BV |
583 | if (py_str != Py_None) |
584 | py_str_as_str(py_str, &doc); | |
585 | Py_DecRef(py_str); | |
451680f1 BV |
586 | |
587 | return doc; | |
b2c19614 BV |
588 | } |
589 | ||
fa12a21e BV |
590 | static void free_probes(GSList *probelist) |
591 | { | |
592 | GSList *l; | |
593 | struct srd_probe *p; | |
594 | ||
595 | if (probelist == NULL) | |
596 | return; | |
597 | ||
598 | for (l = probelist; l; l = l->next) { | |
599 | p = l->data; | |
600 | g_free(p->id); | |
601 | g_free(p->name); | |
602 | g_free(p->desc); | |
603 | g_free(p); | |
604 | } | |
605 | g_slist_free(probelist); | |
fa12a21e BV |
606 | } |
607 | ||
b2c19614 | 608 | /** |
4cc0d9fe | 609 | * Unload the specified protocol decoder. |
451680f1 | 610 | * |
fa12a21e | 611 | * @param dec The struct srd_decoder to be unloaded. |
451680f1 BV |
612 | * |
613 | * @return SRD_OK upon success, a (negative) error code otherwise. | |
8c664ca2 UH |
614 | * |
615 | * @since 0.1.0 | |
b2c19614 | 616 | */ |
9d122fd5 | 617 | SRD_API int srd_decoder_unload(struct srd_decoder *dec) |
b2c19614 | 618 | { |
2f395bff | 619 | struct srd_decoder_option *o; |
32cfb920 | 620 | struct srd_session *sess; |
2f395bff BV |
621 | GSList *l; |
622 | ||
e195c025 BV |
623 | if (!srd_check_init()) |
624 | return SRD_ERR; | |
625 | ||
626 | if (!dec) | |
627 | return SRD_ERR_ARG; | |
628 | ||
8ad6e500 | 629 | srd_dbg("Unloading protocol decoder '%s'.", dec->name); |
fa12a21e | 630 | |
361fdcaa UH |
631 | /* |
632 | * Since any instances of this decoder need to be released as well, | |
fa12a21e BV |
633 | * but they could be anywhere in the stack, just free the entire |
634 | * stack. A frontend reloading a decoder thus has to restart all | |
361fdcaa UH |
635 | * instances, and rebuild the stack. |
636 | */ | |
32cfb920 BV |
637 | for (l = sessions; l; l = l->next) { |
638 | sess = l->data; | |
639 | srd_inst_free_all(sess, NULL); | |
640 | } | |
fa12a21e | 641 | |
2f395bff BV |
642 | for (l = dec->options; l; l = l->next) { |
643 | o = l->data; | |
644 | g_free(o->id); | |
645 | g_free(o->desc); | |
646 | g_variant_unref(o->def); | |
647 | g_free(o); | |
648 | } | |
649 | g_slist_free(dec->options); | |
650 | ||
fa12a21e | 651 | free_probes(dec->probes); |
dcdf4883 | 652 | free_probes(dec->opt_probes); |
b2c19614 BV |
653 | g_free(dec->id); |
654 | g_free(dec->name); | |
b231546d | 655 | g_free(dec->longname); |
b2c19614 | 656 | g_free(dec->desc); |
b231546d | 657 | g_free(dec->license); |
b2c19614 | 658 | |
fa12a21e | 659 | /* The module's Decoder class. */ |
451680f1 | 660 | Py_XDECREF(dec->py_dec); |
fa12a21e | 661 | /* The module itself. */ |
b2c19614 BV |
662 | Py_XDECREF(dec->py_mod); |
663 | ||
e592ac89 | 664 | g_free(dec); |
b2c19614 BV |
665 | |
666 | return SRD_OK; | |
667 | } | |
668 | ||
ea81b49a BV |
669 | static void srd_decoder_load_all_path(char *path) |
670 | { | |
671 | GDir *dir; | |
672 | const gchar *direntry; | |
673 | ||
674 | if (!(dir = g_dir_open(path, 0, NULL))) | |
675 | /* Not really fatal */ | |
676 | return; | |
677 | ||
678 | /* This ignores errors returned by srd_decoder_load(). That | |
679 | * function will have logged the cause, but in any case we | |
680 | * want to continue anyway. */ | |
681 | while ((direntry = g_dir_read_name(dir)) != NULL) { | |
682 | /* The directory name is the module name (e.g. "i2c"). */ | |
683 | srd_decoder_load(direntry); | |
684 | } | |
685 | g_dir_close(dir); | |
686 | ||
687 | } | |
688 | ||
582c8473 | 689 | /** |
9b37109d | 690 | * Load all installed protocol decoders. |
582c8473 BV |
691 | * |
692 | * @return SRD_OK upon success, a (negative) error code otherwise. | |
8c664ca2 UH |
693 | * |
694 | * @since 0.1.0 | |
582c8473 | 695 | */ |
8ad6e500 | 696 | SRD_API int srd_decoder_load_all(void) |
b2c19614 | 697 | { |
ea81b49a | 698 | GSList *l; |
b2c19614 | 699 | |
e195c025 BV |
700 | if (!srd_check_init()) |
701 | return SRD_ERR; | |
702 | ||
ea81b49a BV |
703 | for (l = searchpaths; l; l = l->next) |
704 | srd_decoder_load_all_path(l->data); | |
b2c19614 BV |
705 | |
706 | return SRD_OK; | |
707 | } | |
708 | ||
b2c19614 | 709 | /** |
582c8473 BV |
710 | * Unload all loaded protocol decoders. |
711 | * | |
712 | * @return SRD_OK upon success, a (negative) error code otherwise. | |
8c664ca2 UH |
713 | * |
714 | * @since 0.1.0 | |
b2c19614 | 715 | */ |
8ad6e500 | 716 | SRD_API int srd_decoder_unload_all(void) |
b2c19614 BV |
717 | { |
718 | GSList *l; | |
719 | struct srd_decoder *dec; | |
720 | ||
38ff5046 | 721 | for (l = pd_list; l; l = l->next) { |
b2c19614 | 722 | dec = l->data; |
9d122fd5 | 723 | srd_decoder_unload(dec); |
b2c19614 | 724 | } |
820bf448 BV |
725 | g_slist_free(pd_list); |
726 | pd_list = NULL; | |
b2c19614 BV |
727 | |
728 | return SRD_OK; | |
729 | } | |
4895418c UH |
730 | |
731 | /** @} */ |