]>
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 | ||
36784362 | 21 | #include <config.h> |
f6c7eade MC |
22 | #include "libsigrokdecode-internal.h" /* First, so we avoid a _POSIX_C_SOURCE warning. */ |
23 | #include "libsigrokdecode.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 | ||
c63f5a87 | 42 | /* The list of loaded protocol decoders. */ |
2060510a | 43 | static GSList *pd_list = NULL; |
b2c19614 | 44 | |
ea81b49a | 45 | /* srd.c */ |
23a29d24 | 46 | extern SRD_PRIV GSList *searchpaths; |
ea81b49a BV |
47 | |
48 | /* session.c */ | |
23a29d24 UH |
49 | extern SRD_PRIV GSList *sessions; |
50 | extern SRD_PRIV int max_session_id; | |
32cfb920 | 51 | |
c9bfccc6 | 52 | /* module_sigrokdecode.c */ |
55c3c5f4 | 53 | extern SRD_PRIV PyObject *mod_sigrokdecode; |
451680f1 | 54 | |
57790bc8 UH |
55 | /** @endcond */ |
56 | ||
f6527cc4 UH |
57 | static gboolean srd_check_init(void) |
58 | { | |
59 | if (max_session_id < 0) { | |
60 | srd_err("Library is not initialized."); | |
61 | return FALSE; | |
62 | } else | |
63 | return TRUE; | |
64 | } | |
65 | ||
b2c19614 | 66 | /** |
c63f5a87 | 67 | * Returns the list of loaded protocol decoders. |
b2c19614 | 68 | * |
3a1501e9 | 69 | * This is a GSList of pointers to struct srd_decoder items. |
b2c19614 BV |
70 | * |
71 | * @return List of decoders, NULL if none are supported or loaded. | |
8c664ca2 | 72 | * |
c57d1013 | 73 | * @since 0.2.0 |
b2c19614 | 74 | */ |
38ff5046 | 75 | SRD_API const GSList *srd_decoder_list(void) |
b2c19614 | 76 | { |
e5080882 | 77 | return pd_list; |
b2c19614 BV |
78 | } |
79 | ||
b2c19614 BV |
80 | /** |
81 | * Get the decoder with the specified ID. | |
82 | * | |
83 | * @param id The ID string of the decoder to return. | |
582c8473 | 84 | * |
b2c19614 | 85 | * @return The decoder with the specified ID, or NULL if not found. |
8c664ca2 UH |
86 | * |
87 | * @since 0.1.0 | |
b2c19614 | 88 | */ |
9d122fd5 | 89 | SRD_API struct srd_decoder *srd_decoder_get_by_id(const char *id) |
b2c19614 BV |
90 | { |
91 | GSList *l; | |
92 | struct srd_decoder *dec; | |
93 | ||
38ff5046 | 94 | for (l = pd_list; l; l = l->next) { |
b2c19614 BV |
95 | dec = l->data; |
96 | if (!strcmp(dec->id, id)) | |
97 | return dec; | |
98 | } | |
99 | ||
100 | return NULL; | |
101 | } | |
102 | ||
6d67d057 DE |
103 | static void channel_free(void *data) |
104 | { | |
105 | struct srd_channel *ch = data; | |
106 | ||
107 | if (!ch) | |
108 | return; | |
109 | ||
110 | g_free(ch->desc); | |
111 | g_free(ch->name); | |
112 | g_free(ch->id); | |
113 | g_free(ch); | |
114 | } | |
115 | ||
116 | static void variant_free(void *data) | |
117 | { | |
118 | GVariant *var = data; | |
119 | ||
120 | if (!var) | |
121 | return; | |
122 | ||
123 | g_variant_unref(var); | |
124 | } | |
125 | ||
126 | static void annotation_row_free(void *data) | |
127 | { | |
128 | struct srd_decoder_annotation_row *row = data; | |
129 | ||
130 | if (!row) | |
131 | return; | |
132 | ||
133 | g_slist_free(row->ann_classes); | |
134 | g_free(row->desc); | |
135 | g_free(row->id); | |
136 | g_free(row); | |
137 | } | |
138 | ||
139 | static void decoder_option_free(void *data) | |
140 | { | |
141 | struct srd_decoder_option *opt = data; | |
142 | ||
143 | if (!opt) | |
144 | return; | |
145 | ||
146 | g_slist_free_full(opt->values, &variant_free); | |
147 | variant_free(opt->def); | |
148 | g_free(opt->desc); | |
149 | g_free(opt->id); | |
150 | g_free(opt); | |
151 | } | |
152 | ||
153 | static void decoder_free(struct srd_decoder *dec) | |
154 | { | |
155 | if (!dec) | |
156 | return; | |
157 | ||
158 | Py_XDECREF(dec->py_dec); | |
159 | Py_XDECREF(dec->py_mod); | |
160 | ||
161 | g_slist_free_full(dec->options, &decoder_option_free); | |
162 | g_slist_free_full(dec->binary, (GDestroyNotify)&g_strfreev); | |
163 | g_slist_free_full(dec->annotation_rows, &annotation_row_free); | |
164 | g_slist_free_full(dec->annotations, (GDestroyNotify)&g_strfreev); | |
165 | g_slist_free_full(dec->opt_channels, &channel_free); | |
166 | g_slist_free_full(dec->channels, &channel_free); | |
167 | ||
168 | g_free(dec->license); | |
169 | g_free(dec->desc); | |
170 | g_free(dec->longname); | |
171 | g_free(dec->name); | |
172 | g_free(dec->id); | |
173 | ||
174 | g_free(dec); | |
175 | } | |
176 | ||
6a15597a | 177 | static int get_channels(const struct srd_decoder *d, const char *attr, |
6d67d057 | 178 | GSList **out_pdchl, int offset) |
f38ec285 | 179 | { |
6a15597a UH |
180 | PyObject *py_channellist, *py_entry; |
181 | struct srd_channel *pdch; | |
6d67d057 DE |
182 | GSList *pdchl; |
183 | ssize_t i; | |
f38ec285 BV |
184 | |
185 | if (!PyObject_HasAttrString(d->py_dec, attr)) | |
6a15597a | 186 | /* No channels of this type specified. */ |
f38ec285 BV |
187 | return SRD_OK; |
188 | ||
6d67d057 DE |
189 | pdchl = NULL; |
190 | ||
6a15597a | 191 | py_channellist = PyObject_GetAttrString(d->py_dec, attr); |
6d67d057 DE |
192 | if (!py_channellist) |
193 | goto except_out; | |
194 | ||
6a15597a | 195 | if (!PyTuple_Check(py_channellist)) { |
da9bcbd9 | 196 | srd_err("Protocol decoder %s %s attribute is not a tuple.", |
6d67d057 DE |
197 | d->name, attr); |
198 | goto err_out; | |
f38ec285 BV |
199 | } |
200 | ||
6d67d057 | 201 | for (i = PyTuple_Size(py_channellist) - 1; i >= 0; i--) { |
6a15597a | 202 | py_entry = PyTuple_GetItem(py_channellist, i); |
6d67d057 DE |
203 | if (!py_entry) |
204 | goto except_out; | |
205 | ||
f38ec285 BV |
206 | if (!PyDict_Check(py_entry)) { |
207 | srd_err("Protocol decoder %s %s attribute is not " | |
6d67d057 DE |
208 | "a list of dict elements.", d->name, attr); |
209 | goto err_out; | |
f38ec285 | 210 | } |
6d67d057 DE |
211 | pdch = g_malloc0(sizeof(struct srd_channel)); |
212 | /* Add to list right away so it doesn't get lost. */ | |
213 | pdchl = g_slist_prepend(pdchl, pdch); | |
f38ec285 | 214 | |
6d67d057 DE |
215 | if (py_dictitem_as_str(py_entry, "id", &pdch->id) != SRD_OK) |
216 | goto err_out; | |
217 | if (py_dictitem_as_str(py_entry, "name", &pdch->name) != SRD_OK) | |
218 | goto err_out; | |
219 | if (py_dictitem_as_str(py_entry, "desc", &pdch->desc) != SRD_OK) | |
220 | goto err_out; | |
f38ec285 | 221 | |
6d67d057 | 222 | pdch->order = offset + i; |
f38ec285 | 223 | } |
f38ec285 | 224 | |
6d67d057 DE |
225 | Py_DECREF(py_channellist); |
226 | *out_pdchl = pdchl; | |
227 | ||
228 | return SRD_OK; | |
f38ec285 | 229 | |
6d67d057 DE |
230 | except_out: |
231 | srd_exception_catch("Failed to get %s list of %s decoder", | |
232 | attr, d->name); | |
233 | err_out: | |
234 | g_slist_free_full(pdchl, &channel_free); | |
235 | Py_XDECREF(py_channellist); | |
236 | ||
237 | return SRD_ERR_PYTHON; | |
f38ec285 BV |
238 | } |
239 | ||
2f395bff BV |
240 | static int get_options(struct srd_decoder *d) |
241 | { | |
6d67d057 DE |
242 | PyObject *py_opts, *py_opt, *py_str, *py_values, *py_default, *py_item; |
243 | GSList *options; | |
2f395bff | 244 | struct srd_decoder_option *o; |
84c1c0b5 | 245 | GVariant *gvar; |
6d67d057 | 246 | ssize_t opt, i; |
2f395bff BV |
247 | |
248 | if (!PyObject_HasAttrString(d->py_dec, "options")) | |
84c1c0b5 | 249 | /* No options, that's fine. */ |
2f395bff BV |
250 | return SRD_OK; |
251 | ||
6d67d057 DE |
252 | options = NULL; |
253 | ||
84c1c0b5 | 254 | /* If present, options must be a tuple. */ |
2f395bff | 255 | py_opts = PyObject_GetAttrString(d->py_dec, "options"); |
6d67d057 DE |
256 | if (!py_opts) |
257 | goto except_out; | |
258 | ||
84c1c0b5 BV |
259 | if (!PyTuple_Check(py_opts)) { |
260 | srd_err("Protocol decoder %s: options attribute is not " | |
261 | "a tuple.", d->id); | |
6d67d057 | 262 | goto err_out; |
2f395bff BV |
263 | } |
264 | ||
6d67d057 | 265 | for (opt = PyTuple_Size(py_opts) - 1; opt >= 0; opt--) { |
84c1c0b5 | 266 | py_opt = PyTuple_GetItem(py_opts, opt); |
6d67d057 DE |
267 | if (!py_opt) |
268 | goto except_out; | |
269 | ||
84c1c0b5 BV |
270 | if (!PyDict_Check(py_opt)) { |
271 | srd_err("Protocol decoder %s options: each option " | |
272 | "must consist of a dictionary.", d->name); | |
6d67d057 | 273 | goto err_out; |
2f395bff | 274 | } |
6d67d057 | 275 | |
84c1c0b5 | 276 | o = g_malloc0(sizeof(struct srd_decoder_option)); |
6d67d057 DE |
277 | /* Add to list right away so it doesn't get lost. */ |
278 | options = g_slist_prepend(options, o); | |
279 | ||
280 | py_str = PyDict_GetItemString(py_opt, "id"); | |
281 | if (!py_str) { | |
282 | srd_err("Protocol decoder %s option %zd has no id.", | |
283 | d->name, opt); | |
284 | goto err_out; | |
285 | } | |
286 | if (py_str_as_str(py_str, &o->id) != SRD_OK) | |
287 | goto err_out; | |
288 | ||
289 | py_str = PyDict_GetItemString(py_opt, "desc"); | |
290 | if (py_str) { | |
291 | if (py_str_as_str(py_str, &o->desc) != SRD_OK) | |
292 | goto err_out; | |
293 | } | |
294 | ||
295 | py_default = PyDict_GetItemString(py_opt, "default"); | |
296 | if (py_default) { | |
297 | gvar = py_obj_to_variant(py_default); | |
298 | if (!gvar) { | |
84c1c0b5 | 299 | srd_err("Protocol decoder %s option 'default' has " |
6d67d057 DE |
300 | "invalid default value.", d->name); |
301 | goto err_out; | |
84c1c0b5 | 302 | } |
6d67d057 | 303 | o->def = g_variant_ref_sink(gvar); |
2f395bff | 304 | } |
84c1c0b5 | 305 | |
6d67d057 DE |
306 | py_values = PyDict_GetItemString(py_opt, "values"); |
307 | if (py_values) { | |
84c1c0b5 BV |
308 | /* A default is required if a list of values is |
309 | * given, since it's used to verify their type. */ | |
310 | if (!o->def) { | |
6d67d057 DE |
311 | srd_err("No default for option '%s'.", o->id); |
312 | goto err_out; | |
84c1c0b5 | 313 | } |
6d67d057 | 314 | if (!PyTuple_Check(py_values)) { |
84c1c0b5 | 315 | srd_err("Option '%s' values should be a tuple.", o->id); |
6d67d057 | 316 | goto err_out; |
84c1c0b5 | 317 | } |
6d67d057 DE |
318 | |
319 | for (i = PyTuple_Size(py_values) - 1; i >= 0; i--) { | |
320 | py_item = PyTuple_GetItem(py_values, i); | |
321 | if (!py_item) | |
322 | goto except_out; | |
323 | ||
84c1c0b5 BV |
324 | if (Py_TYPE(py_default) != Py_TYPE(py_item)) { |
325 | srd_err("All values for option '%s' must be " | |
6d67d057 DE |
326 | "of the same type as the default.", |
327 | o->id); | |
328 | goto err_out; | |
84c1c0b5 | 329 | } |
6d67d057 DE |
330 | gvar = py_obj_to_variant(py_item); |
331 | if (!gvar) { | |
332 | srd_err("Protocol decoder %s option 'values' " | |
333 | "contains invalid value.", d->name); | |
334 | goto err_out; | |
84c1c0b5 | 335 | } |
6d67d057 DE |
336 | o->values = g_slist_prepend(o->values, |
337 | g_variant_ref_sink(gvar)); | |
2f395bff | 338 | } |
2f395bff | 339 | } |
6d67d057 DE |
340 | } |
341 | d->options = options; | |
342 | Py_DECREF(py_opts); | |
343 | ||
344 | return SRD_OK; | |
345 | ||
346 | except_out: | |
347 | srd_exception_catch("Failed to get %s decoder options", d->name); | |
348 | err_out: | |
349 | g_slist_free_full(options, &decoder_option_free); | |
350 | Py_XDECREF(py_opts); | |
351 | ||
352 | return SRD_ERR_PYTHON; | |
353 | } | |
354 | ||
355 | /* Convert annotation class attribute to GSList of char **. | |
356 | */ | |
357 | static int get_annotations(struct srd_decoder *dec) | |
358 | { | |
359 | PyObject *py_annlist, *py_ann; | |
360 | GSList *annotations; | |
361 | char **annpair; | |
362 | ssize_t i; | |
363 | ||
364 | if (!PyObject_HasAttrString(dec->py_dec, "annotations")) | |
365 | return SRD_OK; | |
366 | ||
367 | annotations = NULL; | |
368 | ||
369 | py_annlist = PyObject_GetAttrString(dec->py_dec, "annotations"); | |
370 | if (!py_annlist) | |
371 | goto except_out; | |
372 | ||
373 | if (!PyTuple_Check(py_annlist)) { | |
374 | srd_err("Protocol decoder %s annotations should " | |
375 | "be a tuple.", dec->name); | |
376 | goto err_out; | |
377 | } | |
378 | ||
379 | for (i = PyTuple_Size(py_annlist) - 1; i >= 0; i--) { | |
380 | py_ann = PyTuple_GetItem(py_annlist, i); | |
381 | if (!py_ann) | |
382 | goto except_out; | |
383 | ||
384 | if (!PyTuple_Check(py_ann) || PyTuple_Size(py_ann) != 2) { | |
385 | srd_err("Protocol decoder %s annotation %zd should " | |
386 | "be a tuple with two elements.", | |
387 | dec->name, i + 1); | |
388 | goto err_out; | |
389 | } | |
390 | if (py_strseq_to_char(py_ann, &annpair) != SRD_OK) | |
391 | goto err_out; | |
392 | ||
393 | annotations = g_slist_prepend(annotations, annpair); | |
394 | } | |
395 | dec->annotations = annotations; | |
396 | Py_DECREF(py_annlist); | |
397 | ||
398 | return SRD_OK; | |
399 | ||
400 | except_out: | |
401 | srd_exception_catch("Failed to get %s decoder annotations", dec->name); | |
402 | err_out: | |
403 | g_slist_free_full(annotations, (GDestroyNotify)&g_strfreev); | |
404 | Py_XDECREF(py_annlist); | |
405 | ||
406 | return SRD_ERR_PYTHON; | |
407 | } | |
408 | ||
409 | /* Convert annotation_rows to GSList of 'struct srd_decoder_annotation_row'. | |
410 | */ | |
411 | static int get_annotation_rows(struct srd_decoder *dec) | |
412 | { | |
413 | PyObject *py_ann_rows, *py_ann_row, *py_ann_classes, *py_item; | |
414 | GSList *annotation_rows; | |
415 | struct srd_decoder_annotation_row *ann_row; | |
416 | ssize_t i, k; | |
417 | size_t class_idx; | |
418 | ||
419 | if (!PyObject_HasAttrString(dec->py_dec, "annotation_rows")) | |
420 | return SRD_OK; | |
421 | ||
422 | annotation_rows = NULL; | |
423 | ||
424 | py_ann_rows = PyObject_GetAttrString(dec->py_dec, "annotation_rows"); | |
425 | if (!py_ann_rows) | |
426 | goto except_out; | |
427 | ||
428 | if (!PyTuple_Check(py_ann_rows)) { | |
429 | srd_err("Protocol decoder %s annotation_rows " | |
430 | "must be a tuple.", dec->name); | |
431 | goto err_out; | |
432 | } | |
433 | ||
434 | for (i = PyTuple_Size(py_ann_rows) - 1; i >= 0; i--) { | |
435 | py_ann_row = PyTuple_GetItem(py_ann_rows, i); | |
436 | if (!py_ann_row) | |
437 | goto except_out; | |
438 | ||
439 | if (!PyTuple_Check(py_ann_row) || PyTuple_Size(py_ann_row) != 3) { | |
440 | srd_err("Protocol decoder %s annotation_rows " | |
441 | "must contain only tuples of 3 elements.", | |
442 | dec->name); | |
443 | goto err_out; | |
444 | } | |
445 | ann_row = g_malloc0(sizeof(struct srd_decoder_annotation_row)); | |
446 | /* Add to list right away so it doesn't get lost. */ | |
447 | annotation_rows = g_slist_prepend(annotation_rows, ann_row); | |
448 | ||
449 | py_item = PyTuple_GetItem(py_ann_row, 0); | |
450 | if (!py_item) | |
451 | goto except_out; | |
452 | if (py_str_as_str(py_item, &ann_row->id) != SRD_OK) | |
453 | goto err_out; | |
454 | ||
455 | py_item = PyTuple_GetItem(py_ann_row, 1); | |
456 | if (!py_item) | |
457 | goto except_out; | |
458 | if (py_str_as_str(py_item, &ann_row->desc) != SRD_OK) | |
459 | goto err_out; | |
460 | ||
461 | py_ann_classes = PyTuple_GetItem(py_ann_row, 2); | |
462 | if (!py_ann_classes) | |
463 | goto except_out; | |
464 | ||
465 | if (!PyTuple_Check(py_ann_classes)) { | |
466 | srd_err("Protocol decoder %s annotation_rows tuples " | |
467 | "must have a tuple of numbers as 3rd element.", | |
468 | dec->name); | |
469 | goto err_out; | |
470 | } | |
471 | ||
472 | for (k = PyTuple_Size(py_ann_classes) - 1; k >= 0; k--) { | |
473 | py_item = PyTuple_GetItem(py_ann_classes, k); | |
474 | if (!py_item) | |
475 | goto except_out; | |
476 | ||
477 | if (!PyLong_Check(py_item)) { | |
478 | srd_err("Protocol decoder %s annotation row " | |
479 | "class tuple must only contain numbers.", | |
480 | dec->name); | |
481 | goto err_out; | |
482 | } | |
483 | class_idx = PyLong_AsSize_t(py_item); | |
484 | if (PyErr_Occurred()) | |
485 | goto except_out; | |
486 | ||
487 | ann_row->ann_classes = g_slist_prepend(ann_row->ann_classes, | |
488 | GSIZE_TO_POINTER(class_idx)); | |
489 | } | |
490 | } | |
491 | dec->annotation_rows = annotation_rows; | |
492 | Py_DECREF(py_ann_rows); | |
493 | ||
494 | return SRD_OK; | |
495 | ||
496 | except_out: | |
497 | srd_exception_catch("Failed to get %s decoder annotation rows", | |
498 | dec->name); | |
499 | err_out: | |
500 | g_slist_free_full(annotation_rows, &annotation_row_free); | |
501 | Py_XDECREF(py_ann_rows); | |
502 | ||
503 | return SRD_ERR_PYTHON; | |
504 | } | |
505 | ||
506 | /* Convert binary classes to GSList of char **. | |
507 | */ | |
508 | static int get_binary_classes(struct srd_decoder *dec) | |
509 | { | |
510 | PyObject *py_bin_classes, *py_bin_class; | |
511 | GSList *bin_classes; | |
512 | char **bin; | |
513 | ssize_t i; | |
514 | ||
515 | if (!PyObject_HasAttrString(dec->py_dec, "binary")) | |
516 | return SRD_OK; | |
517 | ||
518 | bin_classes = NULL; | |
519 | ||
520 | py_bin_classes = PyObject_GetAttrString(dec->py_dec, "binary"); | |
521 | if (!py_bin_classes) | |
522 | goto except_out; | |
523 | ||
524 | if (!PyTuple_Check(py_bin_classes)) { | |
525 | srd_err("Protocol decoder %s binary classes should " | |
526 | "be a tuple.", dec->name); | |
527 | goto err_out; | |
528 | } | |
529 | ||
530 | for (i = PyTuple_Size(py_bin_classes) - 1; i >= 0; i--) { | |
531 | py_bin_class = PyTuple_GetItem(py_bin_classes, i); | |
532 | if (!py_bin_class) | |
533 | goto except_out; | |
534 | ||
535 | if (!PyTuple_Check(py_bin_class) | |
536 | || PyTuple_Size(py_bin_class) != 2) { | |
537 | srd_err("Protocol decoder %s binary classes should " | |
538 | "consist only of tuples of 2 elements.", | |
539 | dec->name); | |
540 | goto err_out; | |
541 | } | |
542 | if (py_strseq_to_char(py_bin_class, &bin) != SRD_OK) | |
543 | goto err_out; | |
544 | ||
545 | bin_classes = g_slist_prepend(bin_classes, bin); | |
546 | } | |
547 | dec->binary = bin_classes; | |
548 | Py_DECREF(py_bin_classes); | |
549 | ||
550 | return SRD_OK; | |
551 | ||
552 | except_out: | |
553 | srd_exception_catch("Failed to get %s decoder binary classes", | |
554 | dec->name); | |
555 | err_out: | |
556 | g_slist_free_full(bin_classes, (GDestroyNotify)&g_strfreev); | |
557 | Py_XDECREF(py_bin_classes); | |
558 | ||
559 | return SRD_ERR_PYTHON; | |
560 | } | |
561 | ||
562 | /* Check whether the Decoder class defines the named method. | |
563 | */ | |
564 | static int check_method(PyObject *py_dec, const char *mod_name, | |
565 | const char *method_name) | |
566 | { | |
567 | PyObject *py_method; | |
568 | int is_callable; | |
569 | ||
570 | py_method = PyObject_GetAttrString(py_dec, method_name); | |
571 | if (!py_method) { | |
572 | srd_exception_catch("Protocol decoder %s Decoder class " | |
573 | "has no %s() method", mod_name, method_name); | |
574 | return SRD_ERR_PYTHON; | |
575 | } | |
576 | ||
577 | is_callable = PyCallable_Check(py_method); | |
578 | Py_DECREF(py_method); | |
579 | ||
580 | if (!is_callable) { | |
581 | srd_err("Protocol decoder %s Decoder class attribute '%s' " | |
582 | "is not a method.", mod_name, method_name); | |
583 | return SRD_ERR_PYTHON; | |
2f395bff | 584 | } |
2f395bff | 585 | |
84c1c0b5 | 586 | return SRD_OK; |
2f395bff BV |
587 | } |
588 | ||
40589f25 UH |
589 | /** |
590 | * Get the API version of the specified decoder. | |
591 | * | |
592 | * @param d The decoder to use. Must not be NULL. | |
593 | * | |
594 | * @return The API version of the decoder, or 0 upon errors. | |
595 | */ | |
596 | SRD_PRIV long srd_decoder_apiver(const struct srd_decoder *d) | |
597 | { | |
598 | PyObject *py_apiver; | |
599 | long apiver; | |
600 | ||
601 | if (!d) | |
602 | return 0; | |
603 | ||
604 | py_apiver = PyObject_GetAttrString(d->py_dec, "api_version"); | |
605 | apiver = (py_apiver && PyLong_Check(py_apiver)) | |
606 | ? PyLong_AsLong(py_apiver) : 0; | |
607 | Py_XDECREF(py_apiver); | |
608 | ||
609 | return apiver; | |
610 | } | |
611 | ||
b2c19614 | 612 | /** |
64c29e28 | 613 | * Load a protocol decoder module into the embedded Python interpreter. |
b2c19614 | 614 | * |
38ff5046 | 615 | * @param module_name The module name to be loaded. |
b2c19614 BV |
616 | * |
617 | * @return SRD_OK upon success, a (negative) error code otherwise. | |
8c664ca2 UH |
618 | * |
619 | * @since 0.1.0 | |
b2c19614 | 620 | */ |
9d122fd5 | 621 | SRD_API int srd_decoder_load(const char *module_name) |
b2c19614 | 622 | { |
40589f25 | 623 | PyObject *py_basedec; |
b2c19614 | 624 | struct srd_decoder *d; |
6d67d057 DE |
625 | long apiver; |
626 | int is_subclass; | |
60697682 | 627 | const char *fail_txt; |
b2c19614 | 628 | |
e195c025 BV |
629 | if (!srd_check_init()) |
630 | return SRD_ERR; | |
631 | ||
8a014683 BV |
632 | if (!module_name) |
633 | return SRD_ERR_ARG; | |
634 | ||
aac68131 BV |
635 | if (PyDict_GetItemString(PyImport_GetModuleDict(), module_name)) { |
636 | /* Module was already imported. */ | |
637 | return SRD_OK; | |
638 | } | |
639 | ||
8ad6e500 | 640 | srd_dbg("Loading protocol decoder '%s'.", module_name); |
64c29e28 | 641 | |
077fa8ac | 642 | d = g_malloc0(sizeof(struct srd_decoder)); |
60697682 | 643 | fail_txt = NULL; |
b2c19614 | 644 | |
e9dd2fea | 645 | d->py_mod = py_import_by_name(module_name); |
60697682 GS |
646 | if (!d->py_mod) { |
647 | fail_txt = "import by name failed"; | |
6d67d057 | 648 | goto except_out; |
60697682 | 649 | } |
6d67d057 DE |
650 | |
651 | if (!mod_sigrokdecode) { | |
652 | srd_err("sigrokdecode module not loaded."); | |
60697682 | 653 | fail_txt = "sigrokdecode(3) not loaded"; |
451680f1 BV |
654 | goto err_out; |
655 | } | |
b2c19614 | 656 | |
451680f1 | 657 | /* Get the 'Decoder' class as Python object. */ |
6d67d057 | 658 | d->py_dec = PyObject_GetAttrString(d->py_mod, "Decoder"); |
60697682 GS |
659 | if (!d->py_dec) { |
660 | fail_txt = "no 'Decoder' attribute in imported module"; | |
6d67d057 | 661 | goto except_out; |
60697682 | 662 | } |
b2c19614 | 663 | |
6d67d057 | 664 | py_basedec = PyObject_GetAttrString(mod_sigrokdecode, "Decoder"); |
60697682 GS |
665 | if (!py_basedec) { |
666 | fail_txt = "no 'Decoder' attribute in sigrokdecode(3)"; | |
6d67d057 | 667 | goto except_out; |
60697682 | 668 | } |
6d67d057 DE |
669 | |
670 | is_subclass = PyObject_IsSubclass(d->py_dec, py_basedec); | |
671 | Py_DECREF(py_basedec); | |
b2c19614 | 672 | |
6d67d057 | 673 | if (!is_subclass) { |
451680f1 | 674 | srd_err("Decoder class in protocol decoder module %s is not " |
9b37109d | 675 | "a subclass of sigrokdecode.Decoder.", module_name); |
60697682 | 676 | fail_txt = "not a subclass of sigrokdecode.Decoder"; |
451680f1 BV |
677 | goto err_out; |
678 | } | |
b2c19614 | 679 | |
5c0c9cb3 | 680 | /* |
077fa8ac | 681 | * Check that this decoder has the correct PD API version. |
5c0c9cb3 UH |
682 | * PDs of different API versions are incompatible and cannot work. |
683 | */ | |
40589f25 | 684 | apiver = srd_decoder_apiver(d); |
21dfd91d UH |
685 | if (apiver != 2 && apiver != 3) { |
686 | srd_exception_catch("Only PD API version 2/3 is supported, " | |
60697682 GS |
687 | "decoder %s has version %ld", module_name, apiver); |
688 | fail_txt = "API version mismatch"; | |
1d552cd3 BV |
689 | goto err_out; |
690 | } | |
1d552cd3 | 691 | |
6d67d057 DE |
692 | /* Check Decoder class for required methods. |
693 | */ | |
60697682 GS |
694 | if (check_method(d->py_dec, module_name, "start") != SRD_OK) { |
695 | fail_txt = "no 'start()' method"; | |
1d552cd3 | 696 | goto err_out; |
60697682 | 697 | } |
6d67d057 | 698 | |
60697682 GS |
699 | if (check_method(d->py_dec, module_name, "decode") != SRD_OK) { |
700 | fail_txt = "no 'decode()' method"; | |
1d552cd3 | 701 | goto err_out; |
60697682 | 702 | } |
1d552cd3 | 703 | |
84c1c0b5 | 704 | /* Store required fields in newly allocated strings. */ |
60697682 GS |
705 | if (py_attr_as_str(d->py_dec, "id", &(d->id)) != SRD_OK) { |
706 | fail_txt = "no 'id' attribute"; | |
84c1c0b5 | 707 | goto err_out; |
60697682 | 708 | } |
84c1c0b5 | 709 | |
60697682 GS |
710 | if (py_attr_as_str(d->py_dec, "name", &(d->name)) != SRD_OK) { |
711 | fail_txt = "no 'name' attribute"; | |
84c1c0b5 | 712 | goto err_out; |
60697682 | 713 | } |
84c1c0b5 | 714 | |
60697682 GS |
715 | if (py_attr_as_str(d->py_dec, "longname", &(d->longname)) != SRD_OK) { |
716 | fail_txt = "no 'longname' attribute"; | |
84c1c0b5 | 717 | goto err_out; |
60697682 | 718 | } |
84c1c0b5 | 719 | |
60697682 GS |
720 | if (py_attr_as_str(d->py_dec, "desc", &(d->desc)) != SRD_OK) { |
721 | fail_txt = "no 'desc' attribute"; | |
84c1c0b5 | 722 | goto err_out; |
60697682 | 723 | } |
84c1c0b5 | 724 | |
60697682 GS |
725 | if (py_attr_as_str(d->py_dec, "license", &(d->license)) != SRD_OK) { |
726 | fail_txt = "no 'license' attribute"; | |
84c1c0b5 | 727 | goto err_out; |
60697682 | 728 | } |
84c1c0b5 BV |
729 | |
730 | /* All options and their default values. */ | |
60697682 GS |
731 | if (get_options(d) != SRD_OK) { |
732 | fail_txt = "cannot get options"; | |
2f395bff | 733 | goto err_out; |
60697682 | 734 | } |
11ea8ae1 | 735 | |
6a15597a | 736 | /* Check and import required channels. */ |
60697682 GS |
737 | if (get_channels(d, "channels", &d->channels, 0) != SRD_OK) { |
738 | fail_txt = "cannot get channels"; | |
f38ec285 | 739 | goto err_out; |
60697682 | 740 | } |
f38ec285 | 741 | |
6a15597a | 742 | /* Check and import optional channels. */ |
6d67d057 | 743 | if (get_channels(d, "optional_channels", &d->opt_channels, |
60697682 GS |
744 | g_slist_length(d->channels)) != SRD_OK) { |
745 | fail_txt = "cannot get optional channels"; | |
f38ec285 | 746 | goto err_out; |
60697682 | 747 | } |
f38ec285 | 748 | |
60697682 GS |
749 | if (get_annotations(d) != SRD_OK) { |
750 | fail_txt = "cannot get annotations"; | |
6d67d057 | 751 | goto err_out; |
60697682 | 752 | } |
1ce46817 | 753 | |
60697682 GS |
754 | if (get_annotation_rows(d) != SRD_OK) { |
755 | fail_txt = "cannot get annotation rows"; | |
6d67d057 | 756 | goto err_out; |
60697682 | 757 | } |
d75d8a7c | 758 | |
60697682 GS |
759 | if (get_binary_classes(d) != SRD_OK) { |
760 | fail_txt = "cannot get binary classes"; | |
6d67d057 | 761 | goto err_out; |
60697682 | 762 | } |
d75d8a7c | 763 | |
c63f5a87 | 764 | /* Append it to the list of loaded decoders. */ |
9b37109d BV |
765 | pd_list = g_slist_append(pd_list, d); |
766 | ||
6d67d057 | 767 | return SRD_OK; |
451680f1 | 768 | |
6d67d057 | 769 | except_out: |
60697682 GS |
770 | if (fail_txt) { |
771 | srd_exception_catch("Failed to load decoder %s: %s", | |
772 | module_name, fail_txt); | |
773 | fail_txt = NULL; | |
774 | } else { | |
775 | srd_exception_catch("Failed to load decoder %s", module_name); | |
776 | } | |
451680f1 | 777 | err_out: |
60697682 GS |
778 | if (fail_txt) |
779 | srd_err("Failed to load decoder %s: %s", module_name, fail_txt); | |
6d67d057 | 780 | decoder_free(d); |
b2c19614 | 781 | |
6d67d057 | 782 | return SRD_ERR_PYTHON; |
451680f1 BV |
783 | } |
784 | ||
582c8473 BV |
785 | /** |
786 | * Return a protocol decoder's docstring. | |
787 | * | |
788 | * @param dec The loaded protocol decoder. | |
789 | * | |
361fdcaa | 790 | * @return A newly allocated buffer containing the protocol decoder's |
abeeed8b | 791 | * documentation. The caller is responsible for free'ing the buffer. |
8c664ca2 UH |
792 | * |
793 | * @since 0.1.0 | |
582c8473 | 794 | */ |
b33b8aa5 | 795 | SRD_API char *srd_decoder_doc_get(const struct srd_decoder *dec) |
451680f1 | 796 | { |
e7edca07 | 797 | PyObject *py_str; |
451680f1 BV |
798 | char *doc; |
799 | ||
e195c025 BV |
800 | if (!srd_check_init()) |
801 | return NULL; | |
802 | ||
803 | if (!dec) | |
804 | return NULL; | |
805 | ||
e7edca07 BV |
806 | if (!PyObject_HasAttrString(dec->py_mod, "__doc__")) |
807 | return NULL; | |
808 | ||
809 | if (!(py_str = PyObject_GetAttrString(dec->py_mod, "__doc__"))) { | |
201a85a8 | 810 | srd_exception_catch("Failed to get docstring"); |
e7edca07 BV |
811 | return NULL; |
812 | } | |
813 | ||
451680f1 | 814 | doc = NULL; |
e7edca07 BV |
815 | if (py_str != Py_None) |
816 | py_str_as_str(py_str, &doc); | |
6d67d057 | 817 | Py_DECREF(py_str); |
451680f1 BV |
818 | |
819 | return doc; | |
b2c19614 BV |
820 | } |
821 | ||
b2c19614 | 822 | /** |
4cc0d9fe | 823 | * Unload the specified protocol decoder. |
451680f1 | 824 | * |
fa12a21e | 825 | * @param dec The struct srd_decoder to be unloaded. |
451680f1 BV |
826 | * |
827 | * @return SRD_OK upon success, a (negative) error code otherwise. | |
8c664ca2 UH |
828 | * |
829 | * @since 0.1.0 | |
b2c19614 | 830 | */ |
9d122fd5 | 831 | SRD_API int srd_decoder_unload(struct srd_decoder *dec) |
b2c19614 | 832 | { |
32cfb920 | 833 | struct srd_session *sess; |
2f395bff BV |
834 | GSList *l; |
835 | ||
e195c025 BV |
836 | if (!srd_check_init()) |
837 | return SRD_ERR; | |
838 | ||
839 | if (!dec) | |
840 | return SRD_ERR_ARG; | |
841 | ||
8ad6e500 | 842 | srd_dbg("Unloading protocol decoder '%s'.", dec->name); |
fa12a21e | 843 | |
361fdcaa UH |
844 | /* |
845 | * Since any instances of this decoder need to be released as well, | |
fa12a21e BV |
846 | * but they could be anywhere in the stack, just free the entire |
847 | * stack. A frontend reloading a decoder thus has to restart all | |
361fdcaa UH |
848 | * instances, and rebuild the stack. |
849 | */ | |
32cfb920 BV |
850 | for (l = sessions; l; l = l->next) { |
851 | sess = l->data; | |
852 | srd_inst_free_all(sess, NULL); | |
853 | } | |
fa12a21e | 854 | |
c63f5a87 UH |
855 | /* Remove the PD from the list of loaded decoders. */ |
856 | pd_list = g_slist_remove(pd_list, dec); | |
857 | ||
6d67d057 | 858 | decoder_free(dec); |
b2c19614 BV |
859 | |
860 | return SRD_OK; | |
861 | } | |
862 | ||
6a034159 MC |
863 | static void srd_decoder_load_all_zip_path(char *path) |
864 | { | |
865 | PyObject *zipimport_mod, *zipimporter_class, *zipimporter; | |
866 | PyObject *prefix_obj, *files, *key, *value, *set, *modname; | |
867 | Py_ssize_t pos = 0; | |
868 | char *prefix; | |
869 | size_t prefix_len; | |
870 | ||
871 | set = files = prefix_obj = zipimporter = zipimporter_class = NULL; | |
872 | ||
e9dd2fea | 873 | zipimport_mod = py_import_by_name("zipimport"); |
6a034159 MC |
874 | if (zipimport_mod == NULL) |
875 | goto err_out; | |
876 | ||
877 | zipimporter_class = PyObject_GetAttrString(zipimport_mod, "zipimporter"); | |
878 | if (zipimporter_class == NULL) | |
879 | goto err_out; | |
880 | ||
881 | zipimporter = PyObject_CallFunction(zipimporter_class, "s", path); | |
882 | if (zipimporter == NULL) | |
883 | goto err_out; | |
884 | ||
885 | prefix_obj = PyObject_GetAttrString(zipimporter, "prefix"); | |
886 | if (prefix_obj == NULL) | |
887 | goto err_out; | |
888 | ||
889 | files = PyObject_GetAttrString(zipimporter, "_files"); | |
6d67d057 | 890 | if (files == NULL || !PyDict_Check(files)) |
6a034159 MC |
891 | goto err_out; |
892 | ||
893 | set = PySet_New(NULL); | |
894 | if (set == NULL) | |
895 | goto err_out; | |
896 | ||
897 | if (py_str_as_str(prefix_obj, &prefix) != SRD_OK) | |
898 | goto err_out; | |
899 | ||
900 | prefix_len = strlen(prefix); | |
901 | ||
902 | while (PyDict_Next(files, &pos, &key, &value)) { | |
903 | char *path, *slash; | |
904 | if (py_str_as_str(key, &path) == SRD_OK) { | |
6d67d057 DE |
905 | if (strlen(path) > prefix_len |
906 | && memcmp(path, prefix, prefix_len) == 0 | |
907 | && (slash = strchr(path + prefix_len, '/'))) { | |
908 | ||
909 | modname = PyUnicode_FromStringAndSize(path + prefix_len, | |
910 | slash - (path + prefix_len)); | |
6a034159 MC |
911 | if (modname == NULL) { |
912 | PyErr_Clear(); | |
913 | } else { | |
914 | PySet_Add(set, modname); | |
6d67d057 | 915 | Py_DECREF(modname); |
6a034159 MC |
916 | } |
917 | } | |
6d67d057 | 918 | g_free(path); |
6a034159 MC |
919 | } |
920 | } | |
6d67d057 | 921 | g_free(prefix); |
6a034159 MC |
922 | |
923 | while ((modname = PySet_Pop(set))) { | |
924 | char *modname_str; | |
925 | if (py_str_as_str(modname, &modname_str) == SRD_OK) { | |
926 | /* The directory name is the module name (e.g. "i2c"). */ | |
927 | srd_decoder_load(modname_str); | |
6d67d057 | 928 | g_free(modname_str); |
6a034159 | 929 | } |
6d67d057 | 930 | Py_DECREF(modname); |
6a034159 MC |
931 | } |
932 | ||
933 | err_out: | |
934 | Py_XDECREF(set); | |
935 | Py_XDECREF(files); | |
936 | Py_XDECREF(prefix_obj); | |
937 | Py_XDECREF(zipimporter); | |
938 | Py_XDECREF(zipimporter_class); | |
939 | Py_XDECREF(zipimport_mod); | |
940 | PyErr_Clear(); | |
941 | } | |
942 | ||
ea81b49a BV |
943 | static void srd_decoder_load_all_path(char *path) |
944 | { | |
945 | GDir *dir; | |
946 | const gchar *direntry; | |
947 | ||
6a034159 | 948 | if (!(dir = g_dir_open(path, 0, NULL))) { |
ea81b49a | 949 | /* Not really fatal */ |
6a034159 MC |
950 | /* Try zipimport method too */ |
951 | srd_decoder_load_all_zip_path(path); | |
ea81b49a | 952 | return; |
6a034159 | 953 | } |
ea81b49a BV |
954 | |
955 | /* This ignores errors returned by srd_decoder_load(). That | |
956 | * function will have logged the cause, but in any case we | |
957 | * want to continue anyway. */ | |
958 | while ((direntry = g_dir_read_name(dir)) != NULL) { | |
959 | /* The directory name is the module name (e.g. "i2c"). */ | |
960 | srd_decoder_load(direntry); | |
961 | } | |
962 | g_dir_close(dir); | |
963 | ||
964 | } | |
965 | ||
582c8473 | 966 | /** |
9b37109d | 967 | * Load all installed protocol decoders. |
582c8473 BV |
968 | * |
969 | * @return SRD_OK upon success, a (negative) error code otherwise. | |
8c664ca2 UH |
970 | * |
971 | * @since 0.1.0 | |
582c8473 | 972 | */ |
8ad6e500 | 973 | SRD_API int srd_decoder_load_all(void) |
b2c19614 | 974 | { |
ea81b49a | 975 | GSList *l; |
b2c19614 | 976 | |
e195c025 BV |
977 | if (!srd_check_init()) |
978 | return SRD_ERR; | |
979 | ||
ea81b49a BV |
980 | for (l = searchpaths; l; l = l->next) |
981 | srd_decoder_load_all_path(l->data); | |
b2c19614 BV |
982 | |
983 | return SRD_OK; | |
984 | } | |
985 | ||
b2c19614 | 986 | /** |
582c8473 BV |
987 | * Unload all loaded protocol decoders. |
988 | * | |
989 | * @return SRD_OK upon success, a (negative) error code otherwise. | |
8c664ca2 UH |
990 | * |
991 | * @since 0.1.0 | |
b2c19614 | 992 | */ |
8ad6e500 | 993 | SRD_API int srd_decoder_unload_all(void) |
b2c19614 | 994 | { |
95e40a0c | 995 | g_slist_foreach(pd_list, (GFunc)srd_decoder_unload, NULL); |
820bf448 BV |
996 | g_slist_free(pd_list); |
997 | pd_list = NULL; | |
b2c19614 BV |
998 | |
999 | return SRD_OK; | |
1000 | } | |
4895418c UH |
1001 | |
1002 | /** @} */ |