]>
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 | ||
ddcde186 UH |
139 | static void logic_output_channel_free(void *data) |
140 | { | |
141 | struct srd_decoder_logic_output_channel *logic_out_ch = data; | |
142 | ||
143 | if (!logic_out_ch) | |
144 | return; | |
145 | ||
146 | g_free(logic_out_ch->desc); | |
147 | g_free(logic_out_ch->id); | |
148 | g_free(logic_out_ch); | |
149 | } | |
150 | ||
6d67d057 DE |
151 | static void decoder_option_free(void *data) |
152 | { | |
153 | struct srd_decoder_option *opt = data; | |
154 | ||
155 | if (!opt) | |
156 | return; | |
157 | ||
158 | g_slist_free_full(opt->values, &variant_free); | |
159 | variant_free(opt->def); | |
160 | g_free(opt->desc); | |
161 | g_free(opt->id); | |
162 | g_free(opt); | |
163 | } | |
164 | ||
165 | static void decoder_free(struct srd_decoder *dec) | |
166 | { | |
514b2edc UH |
167 | PyGILState_STATE gstate; |
168 | ||
6d67d057 DE |
169 | if (!dec) |
170 | return; | |
171 | ||
514b2edc | 172 | gstate = PyGILState_Ensure(); |
6d67d057 DE |
173 | Py_XDECREF(dec->py_dec); |
174 | Py_XDECREF(dec->py_mod); | |
514b2edc | 175 | PyGILState_Release(gstate); |
6d67d057 DE |
176 | |
177 | g_slist_free_full(dec->options, &decoder_option_free); | |
178 | g_slist_free_full(dec->binary, (GDestroyNotify)&g_strfreev); | |
179 | g_slist_free_full(dec->annotation_rows, &annotation_row_free); | |
180 | g_slist_free_full(dec->annotations, (GDestroyNotify)&g_strfreev); | |
181 | g_slist_free_full(dec->opt_channels, &channel_free); | |
182 | g_slist_free_full(dec->channels, &channel_free); | |
183 | ||
d480174d UH |
184 | g_slist_free_full(dec->outputs, g_free); |
185 | g_slist_free_full(dec->inputs, g_free); | |
4c180223 | 186 | g_slist_free_full(dec->tags, g_free); |
6d67d057 DE |
187 | g_free(dec->license); |
188 | g_free(dec->desc); | |
189 | g_free(dec->longname); | |
190 | g_free(dec->name); | |
191 | g_free(dec->id); | |
192 | ||
193 | g_free(dec); | |
194 | } | |
195 | ||
6a15597a | 196 | static int get_channels(const struct srd_decoder *d, const char *attr, |
6d67d057 | 197 | GSList **out_pdchl, int offset) |
f38ec285 | 198 | { |
6a15597a UH |
199 | PyObject *py_channellist, *py_entry; |
200 | struct srd_channel *pdch; | |
6d67d057 DE |
201 | GSList *pdchl; |
202 | ssize_t i; | |
514b2edc UH |
203 | PyGILState_STATE gstate; |
204 | ||
205 | gstate = PyGILState_Ensure(); | |
f38ec285 | 206 | |
514b2edc | 207 | if (!PyObject_HasAttrString(d->py_dec, attr)) { |
6a15597a | 208 | /* No channels of this type specified. */ |
514b2edc | 209 | PyGILState_Release(gstate); |
f38ec285 | 210 | return SRD_OK; |
514b2edc | 211 | } |
f38ec285 | 212 | |
6d67d057 DE |
213 | pdchl = NULL; |
214 | ||
6a15597a | 215 | py_channellist = PyObject_GetAttrString(d->py_dec, attr); |
6d67d057 DE |
216 | if (!py_channellist) |
217 | goto except_out; | |
218 | ||
6a15597a | 219 | if (!PyTuple_Check(py_channellist)) { |
da9bcbd9 | 220 | srd_err("Protocol decoder %s %s attribute is not a tuple.", |
6d67d057 DE |
221 | d->name, attr); |
222 | goto err_out; | |
f38ec285 BV |
223 | } |
224 | ||
6d67d057 | 225 | for (i = PyTuple_Size(py_channellist) - 1; i >= 0; i--) { |
6a15597a | 226 | py_entry = PyTuple_GetItem(py_channellist, i); |
6d67d057 DE |
227 | if (!py_entry) |
228 | goto except_out; | |
229 | ||
f38ec285 BV |
230 | if (!PyDict_Check(py_entry)) { |
231 | srd_err("Protocol decoder %s %s attribute is not " | |
6d67d057 DE |
232 | "a list of dict elements.", d->name, attr); |
233 | goto err_out; | |
f38ec285 | 234 | } |
35c10c0e | 235 | pdch = g_malloc(sizeof(struct srd_channel)); |
6d67d057 DE |
236 | /* Add to list right away so it doesn't get lost. */ |
237 | pdchl = g_slist_prepend(pdchl, pdch); | |
f38ec285 | 238 | |
6d67d057 DE |
239 | if (py_dictitem_as_str(py_entry, "id", &pdch->id) != SRD_OK) |
240 | goto err_out; | |
241 | if (py_dictitem_as_str(py_entry, "name", &pdch->name) != SRD_OK) | |
242 | goto err_out; | |
243 | if (py_dictitem_as_str(py_entry, "desc", &pdch->desc) != SRD_OK) | |
244 | goto err_out; | |
f38ec285 | 245 | |
6d67d057 | 246 | pdch->order = offset + i; |
f38ec285 | 247 | } |
f38ec285 | 248 | |
6d67d057 DE |
249 | Py_DECREF(py_channellist); |
250 | *out_pdchl = pdchl; | |
251 | ||
514b2edc UH |
252 | PyGILState_Release(gstate); |
253 | ||
6d67d057 | 254 | return SRD_OK; |
f38ec285 | 255 | |
6d67d057 DE |
256 | except_out: |
257 | srd_exception_catch("Failed to get %s list of %s decoder", | |
258 | attr, d->name); | |
7969d803 | 259 | |
6d67d057 DE |
260 | err_out: |
261 | g_slist_free_full(pdchl, &channel_free); | |
262 | Py_XDECREF(py_channellist); | |
514b2edc | 263 | PyGILState_Release(gstate); |
6d67d057 DE |
264 | |
265 | return SRD_ERR_PYTHON; | |
f38ec285 BV |
266 | } |
267 | ||
2f395bff BV |
268 | static int get_options(struct srd_decoder *d) |
269 | { | |
6d67d057 DE |
270 | PyObject *py_opts, *py_opt, *py_str, *py_values, *py_default, *py_item; |
271 | GSList *options; | |
2f395bff | 272 | struct srd_decoder_option *o; |
84c1c0b5 | 273 | GVariant *gvar; |
6d67d057 | 274 | ssize_t opt, i; |
514b2edc | 275 | PyGILState_STATE gstate; |
2f395bff | 276 | |
514b2edc UH |
277 | gstate = PyGILState_Ensure(); |
278 | ||
279 | if (!PyObject_HasAttrString(d->py_dec, "options")) { | |
84c1c0b5 | 280 | /* No options, that's fine. */ |
514b2edc | 281 | PyGILState_Release(gstate); |
2f395bff | 282 | return SRD_OK; |
514b2edc | 283 | } |
2f395bff | 284 | |
6d67d057 DE |
285 | options = NULL; |
286 | ||
84c1c0b5 | 287 | /* If present, options must be a tuple. */ |
2f395bff | 288 | py_opts = PyObject_GetAttrString(d->py_dec, "options"); |
6d67d057 DE |
289 | if (!py_opts) |
290 | goto except_out; | |
291 | ||
84c1c0b5 BV |
292 | if (!PyTuple_Check(py_opts)) { |
293 | srd_err("Protocol decoder %s: options attribute is not " | |
294 | "a tuple.", d->id); | |
6d67d057 | 295 | goto err_out; |
2f395bff BV |
296 | } |
297 | ||
6d67d057 | 298 | for (opt = PyTuple_Size(py_opts) - 1; opt >= 0; opt--) { |
84c1c0b5 | 299 | py_opt = PyTuple_GetItem(py_opts, opt); |
6d67d057 DE |
300 | if (!py_opt) |
301 | goto except_out; | |
302 | ||
84c1c0b5 BV |
303 | if (!PyDict_Check(py_opt)) { |
304 | srd_err("Protocol decoder %s options: each option " | |
305 | "must consist of a dictionary.", d->name); | |
6d67d057 | 306 | goto err_out; |
2f395bff | 307 | } |
6d67d057 | 308 | |
84c1c0b5 | 309 | o = g_malloc0(sizeof(struct srd_decoder_option)); |
6d67d057 DE |
310 | /* Add to list right away so it doesn't get lost. */ |
311 | options = g_slist_prepend(options, o); | |
312 | ||
313 | py_str = PyDict_GetItemString(py_opt, "id"); | |
314 | if (!py_str) { | |
fa168be6 | 315 | srd_err("Protocol decoder %s option %zd has no ID.", |
6d67d057 DE |
316 | d->name, opt); |
317 | goto err_out; | |
318 | } | |
319 | if (py_str_as_str(py_str, &o->id) != SRD_OK) | |
320 | goto err_out; | |
321 | ||
322 | py_str = PyDict_GetItemString(py_opt, "desc"); | |
323 | if (py_str) { | |
324 | if (py_str_as_str(py_str, &o->desc) != SRD_OK) | |
325 | goto err_out; | |
326 | } | |
327 | ||
328 | py_default = PyDict_GetItemString(py_opt, "default"); | |
329 | if (py_default) { | |
330 | gvar = py_obj_to_variant(py_default); | |
331 | if (!gvar) { | |
84c1c0b5 | 332 | srd_err("Protocol decoder %s option 'default' has " |
6d67d057 DE |
333 | "invalid default value.", d->name); |
334 | goto err_out; | |
84c1c0b5 | 335 | } |
6d67d057 | 336 | o->def = g_variant_ref_sink(gvar); |
2f395bff | 337 | } |
84c1c0b5 | 338 | |
6d67d057 DE |
339 | py_values = PyDict_GetItemString(py_opt, "values"); |
340 | if (py_values) { | |
7969d803 UH |
341 | /* |
342 | * A default is required if a list of values is | |
343 | * given, since it's used to verify their type. | |
344 | */ | |
84c1c0b5 | 345 | if (!o->def) { |
6d67d057 DE |
346 | srd_err("No default for option '%s'.", o->id); |
347 | goto err_out; | |
84c1c0b5 | 348 | } |
6d67d057 | 349 | if (!PyTuple_Check(py_values)) { |
84c1c0b5 | 350 | srd_err("Option '%s' values should be a tuple.", o->id); |
6d67d057 | 351 | goto err_out; |
84c1c0b5 | 352 | } |
6d67d057 DE |
353 | |
354 | for (i = PyTuple_Size(py_values) - 1; i >= 0; i--) { | |
355 | py_item = PyTuple_GetItem(py_values, i); | |
356 | if (!py_item) | |
357 | goto except_out; | |
358 | ||
2d661bcb | 359 | if (py_default && (Py_TYPE(py_default) != Py_TYPE(py_item))) { |
84c1c0b5 | 360 | srd_err("All values for option '%s' must be " |
6d67d057 DE |
361 | "of the same type as the default.", |
362 | o->id); | |
363 | goto err_out; | |
84c1c0b5 | 364 | } |
6d67d057 DE |
365 | gvar = py_obj_to_variant(py_item); |
366 | if (!gvar) { | |
367 | srd_err("Protocol decoder %s option 'values' " | |
368 | "contains invalid value.", d->name); | |
369 | goto err_out; | |
84c1c0b5 | 370 | } |
6d67d057 DE |
371 | o->values = g_slist_prepend(o->values, |
372 | g_variant_ref_sink(gvar)); | |
2f395bff | 373 | } |
2f395bff | 374 | } |
6d67d057 DE |
375 | } |
376 | d->options = options; | |
377 | Py_DECREF(py_opts); | |
514b2edc | 378 | PyGILState_Release(gstate); |
6d67d057 DE |
379 | |
380 | return SRD_OK; | |
381 | ||
382 | except_out: | |
383 | srd_exception_catch("Failed to get %s decoder options", d->name); | |
7969d803 | 384 | |
6d67d057 DE |
385 | err_out: |
386 | g_slist_free_full(options, &decoder_option_free); | |
387 | Py_XDECREF(py_opts); | |
514b2edc | 388 | PyGILState_Release(gstate); |
6d67d057 DE |
389 | |
390 | return SRD_ERR_PYTHON; | |
391 | } | |
392 | ||
7969d803 | 393 | /* Convert annotation class attribute to GSList of char **. */ |
6d67d057 DE |
394 | static int get_annotations(struct srd_decoder *dec) |
395 | { | |
396 | PyObject *py_annlist, *py_ann; | |
397 | GSList *annotations; | |
398 | char **annpair; | |
399 | ssize_t i; | |
514b2edc UH |
400 | PyGILState_STATE gstate; |
401 | ||
402 | gstate = PyGILState_Ensure(); | |
6d67d057 | 403 | |
514b2edc UH |
404 | if (!PyObject_HasAttrString(dec->py_dec, "annotations")) { |
405 | PyGILState_Release(gstate); | |
6d67d057 | 406 | return SRD_OK; |
514b2edc | 407 | } |
6d67d057 DE |
408 | |
409 | annotations = NULL; | |
410 | ||
411 | py_annlist = PyObject_GetAttrString(dec->py_dec, "annotations"); | |
412 | if (!py_annlist) | |
413 | goto except_out; | |
414 | ||
415 | if (!PyTuple_Check(py_annlist)) { | |
416 | srd_err("Protocol decoder %s annotations should " | |
417 | "be a tuple.", dec->name); | |
418 | goto err_out; | |
419 | } | |
420 | ||
421 | for (i = PyTuple_Size(py_annlist) - 1; i >= 0; i--) { | |
422 | py_ann = PyTuple_GetItem(py_annlist, i); | |
423 | if (!py_ann) | |
424 | goto except_out; | |
425 | ||
426 | if (!PyTuple_Check(py_ann) || PyTuple_Size(py_ann) != 2) { | |
427 | srd_err("Protocol decoder %s annotation %zd should " | |
428 | "be a tuple with two elements.", | |
429 | dec->name, i + 1); | |
430 | goto err_out; | |
431 | } | |
432 | if (py_strseq_to_char(py_ann, &annpair) != SRD_OK) | |
433 | goto err_out; | |
434 | ||
435 | annotations = g_slist_prepend(annotations, annpair); | |
436 | } | |
437 | dec->annotations = annotations; | |
438 | Py_DECREF(py_annlist); | |
514b2edc | 439 | PyGILState_Release(gstate); |
6d67d057 DE |
440 | |
441 | return SRD_OK; | |
442 | ||
443 | except_out: | |
444 | srd_exception_catch("Failed to get %s decoder annotations", dec->name); | |
7969d803 | 445 | |
6d67d057 DE |
446 | err_out: |
447 | g_slist_free_full(annotations, (GDestroyNotify)&g_strfreev); | |
448 | Py_XDECREF(py_annlist); | |
514b2edc | 449 | PyGILState_Release(gstate); |
6d67d057 DE |
450 | |
451 | return SRD_ERR_PYTHON; | |
452 | } | |
453 | ||
7969d803 | 454 | /* Convert annotation_rows to GSList of 'struct srd_decoder_annotation_row'. */ |
6d67d057 DE |
455 | static int get_annotation_rows(struct srd_decoder *dec) |
456 | { | |
457 | PyObject *py_ann_rows, *py_ann_row, *py_ann_classes, *py_item; | |
458 | GSList *annotation_rows; | |
459 | struct srd_decoder_annotation_row *ann_row; | |
460 | ssize_t i, k; | |
461 | size_t class_idx; | |
514b2edc UH |
462 | PyGILState_STATE gstate; |
463 | ||
464 | gstate = PyGILState_Ensure(); | |
6d67d057 | 465 | |
514b2edc UH |
466 | if (!PyObject_HasAttrString(dec->py_dec, "annotation_rows")) { |
467 | PyGILState_Release(gstate); | |
6d67d057 | 468 | return SRD_OK; |
514b2edc | 469 | } |
6d67d057 DE |
470 | |
471 | annotation_rows = NULL; | |
472 | ||
473 | py_ann_rows = PyObject_GetAttrString(dec->py_dec, "annotation_rows"); | |
474 | if (!py_ann_rows) | |
475 | goto except_out; | |
476 | ||
477 | if (!PyTuple_Check(py_ann_rows)) { | |
478 | srd_err("Protocol decoder %s annotation_rows " | |
479 | "must be a tuple.", dec->name); | |
480 | goto err_out; | |
481 | } | |
482 | ||
483 | for (i = PyTuple_Size(py_ann_rows) - 1; i >= 0; i--) { | |
484 | py_ann_row = PyTuple_GetItem(py_ann_rows, i); | |
485 | if (!py_ann_row) | |
486 | goto except_out; | |
487 | ||
488 | if (!PyTuple_Check(py_ann_row) || PyTuple_Size(py_ann_row) != 3) { | |
489 | srd_err("Protocol decoder %s annotation_rows " | |
490 | "must contain only tuples of 3 elements.", | |
491 | dec->name); | |
492 | goto err_out; | |
493 | } | |
494 | ann_row = g_malloc0(sizeof(struct srd_decoder_annotation_row)); | |
495 | /* Add to list right away so it doesn't get lost. */ | |
496 | annotation_rows = g_slist_prepend(annotation_rows, ann_row); | |
497 | ||
498 | py_item = PyTuple_GetItem(py_ann_row, 0); | |
499 | if (!py_item) | |
500 | goto except_out; | |
501 | if (py_str_as_str(py_item, &ann_row->id) != SRD_OK) | |
502 | goto err_out; | |
503 | ||
504 | py_item = PyTuple_GetItem(py_ann_row, 1); | |
505 | if (!py_item) | |
506 | goto except_out; | |
507 | if (py_str_as_str(py_item, &ann_row->desc) != SRD_OK) | |
508 | goto err_out; | |
509 | ||
510 | py_ann_classes = PyTuple_GetItem(py_ann_row, 2); | |
511 | if (!py_ann_classes) | |
512 | goto except_out; | |
513 | ||
514 | if (!PyTuple_Check(py_ann_classes)) { | |
515 | srd_err("Protocol decoder %s annotation_rows tuples " | |
516 | "must have a tuple of numbers as 3rd element.", | |
517 | dec->name); | |
518 | goto err_out; | |
519 | } | |
520 | ||
521 | for (k = PyTuple_Size(py_ann_classes) - 1; k >= 0; k--) { | |
522 | py_item = PyTuple_GetItem(py_ann_classes, k); | |
523 | if (!py_item) | |
524 | goto except_out; | |
525 | ||
526 | if (!PyLong_Check(py_item)) { | |
527 | srd_err("Protocol decoder %s annotation row " | |
528 | "class tuple must only contain numbers.", | |
529 | dec->name); | |
530 | goto err_out; | |
531 | } | |
532 | class_idx = PyLong_AsSize_t(py_item); | |
533 | if (PyErr_Occurred()) | |
534 | goto except_out; | |
535 | ||
536 | ann_row->ann_classes = g_slist_prepend(ann_row->ann_classes, | |
537 | GSIZE_TO_POINTER(class_idx)); | |
538 | } | |
539 | } | |
540 | dec->annotation_rows = annotation_rows; | |
541 | Py_DECREF(py_ann_rows); | |
514b2edc | 542 | PyGILState_Release(gstate); |
6d67d057 DE |
543 | |
544 | return SRD_OK; | |
545 | ||
546 | except_out: | |
547 | srd_exception_catch("Failed to get %s decoder annotation rows", | |
548 | dec->name); | |
7969d803 | 549 | |
6d67d057 DE |
550 | err_out: |
551 | g_slist_free_full(annotation_rows, &annotation_row_free); | |
552 | Py_XDECREF(py_ann_rows); | |
514b2edc | 553 | PyGILState_Release(gstate); |
6d67d057 DE |
554 | |
555 | return SRD_ERR_PYTHON; | |
556 | } | |
557 | ||
7969d803 | 558 | /* Convert binary classes to GSList of char **. */ |
6d67d057 DE |
559 | static int get_binary_classes(struct srd_decoder *dec) |
560 | { | |
561 | PyObject *py_bin_classes, *py_bin_class; | |
562 | GSList *bin_classes; | |
563 | char **bin; | |
564 | ssize_t i; | |
514b2edc | 565 | PyGILState_STATE gstate; |
6d67d057 | 566 | |
514b2edc UH |
567 | gstate = PyGILState_Ensure(); |
568 | ||
569 | if (!PyObject_HasAttrString(dec->py_dec, "binary")) { | |
570 | PyGILState_Release(gstate); | |
6d67d057 | 571 | return SRD_OK; |
514b2edc | 572 | } |
6d67d057 DE |
573 | |
574 | bin_classes = NULL; | |
575 | ||
576 | py_bin_classes = PyObject_GetAttrString(dec->py_dec, "binary"); | |
577 | if (!py_bin_classes) | |
578 | goto except_out; | |
579 | ||
580 | if (!PyTuple_Check(py_bin_classes)) { | |
581 | srd_err("Protocol decoder %s binary classes should " | |
582 | "be a tuple.", dec->name); | |
583 | goto err_out; | |
584 | } | |
585 | ||
586 | for (i = PyTuple_Size(py_bin_classes) - 1; i >= 0; i--) { | |
587 | py_bin_class = PyTuple_GetItem(py_bin_classes, i); | |
588 | if (!py_bin_class) | |
589 | goto except_out; | |
590 | ||
591 | if (!PyTuple_Check(py_bin_class) | |
592 | || PyTuple_Size(py_bin_class) != 2) { | |
593 | srd_err("Protocol decoder %s binary classes should " | |
594 | "consist only of tuples of 2 elements.", | |
595 | dec->name); | |
596 | goto err_out; | |
597 | } | |
598 | if (py_strseq_to_char(py_bin_class, &bin) != SRD_OK) | |
599 | goto err_out; | |
600 | ||
601 | bin_classes = g_slist_prepend(bin_classes, bin); | |
602 | } | |
603 | dec->binary = bin_classes; | |
604 | Py_DECREF(py_bin_classes); | |
514b2edc | 605 | PyGILState_Release(gstate); |
6d67d057 DE |
606 | |
607 | return SRD_OK; | |
608 | ||
609 | except_out: | |
610 | srd_exception_catch("Failed to get %s decoder binary classes", | |
611 | dec->name); | |
7969d803 | 612 | |
6d67d057 DE |
613 | err_out: |
614 | g_slist_free_full(bin_classes, (GDestroyNotify)&g_strfreev); | |
615 | Py_XDECREF(py_bin_classes); | |
514b2edc | 616 | PyGILState_Release(gstate); |
6d67d057 DE |
617 | |
618 | return SRD_ERR_PYTHON; | |
619 | } | |
620 | ||
ddcde186 UH |
621 | /* Convert logic_output_channels to GSList of 'struct srd_decoder_logic_output_channel'. */ |
622 | static int get_logic_output_channels(struct srd_decoder *dec) | |
623 | { | |
624 | PyObject *py_logic_out_chs, *py_logic_out_ch, *py_samplerate, *py_item; | |
625 | GSList *logic_out_chs; | |
626 | struct srd_decoder_logic_output_channel *logic_out_ch; | |
627 | ssize_t i; | |
628 | PyGILState_STATE gstate; | |
629 | ||
630 | gstate = PyGILState_Ensure(); | |
631 | ||
632 | if (!PyObject_HasAttrString(dec->py_dec, "logic_output_channels")) { | |
633 | PyGILState_Release(gstate); | |
634 | return SRD_OK; | |
635 | } | |
636 | ||
637 | logic_out_chs = NULL; | |
638 | ||
639 | py_logic_out_chs = PyObject_GetAttrString(dec->py_dec, "logic_output_channels"); | |
640 | if (!py_logic_out_chs) | |
641 | goto except_out; | |
642 | ||
643 | if (!PyTuple_Check(py_logic_out_chs)) { | |
644 | srd_err("Protocol decoder %s logic_output_channels " | |
645 | "must be a tuple.", dec->name); | |
646 | goto err_out; | |
647 | } | |
648 | ||
649 | for (i = PyTuple_Size(py_logic_out_chs) - 1; i >= 0; i--) { | |
650 | py_logic_out_ch = PyTuple_GetItem(py_logic_out_chs, i); | |
651 | if (!py_logic_out_ch) | |
652 | goto except_out; | |
653 | ||
654 | if (!PyTuple_Check(py_logic_out_ch) || PyTuple_Size(py_logic_out_ch) != 3) { | |
655 | srd_err("Protocol decoder %s logic_output_channels " | |
656 | "must contain only tuples of 3 elements.", | |
657 | dec->name); | |
658 | goto err_out; | |
659 | } | |
660 | logic_out_ch = g_malloc0(sizeof(*logic_out_ch)); | |
661 | /* Add to list right away so it doesn't get lost. */ | |
662 | logic_out_chs = g_slist_prepend(logic_out_chs, logic_out_ch); | |
663 | ||
664 | py_item = PyTuple_GetItem(py_logic_out_ch, 0); | |
665 | if (!py_item) | |
666 | goto except_out; | |
667 | if (py_str_as_str(py_item, &logic_out_ch->id) != SRD_OK) | |
668 | goto err_out; | |
669 | ||
670 | py_item = PyTuple_GetItem(py_logic_out_ch, 1); | |
671 | if (!py_item) | |
672 | goto except_out; | |
673 | if (py_str_as_str(py_item, &logic_out_ch->desc) != SRD_OK) | |
674 | goto err_out; | |
675 | ||
676 | py_samplerate = PyTuple_GetItem(py_logic_out_ch, 2); | |
677 | if (!py_samplerate) | |
678 | goto except_out; | |
679 | ||
680 | if (!PyLong_Check(py_samplerate)) { | |
681 | srd_err("Protocol decoder %s logic_output_channels tuples " | |
682 | "must have a number as 3rd element.", | |
683 | dec->name); | |
684 | goto err_out; | |
685 | } | |
686 | ||
687 | logic_out_ch->samplerate = PyLong_AsUnsignedLongLong(py_samplerate); | |
688 | } | |
689 | dec->logic_output_channels = logic_out_chs; | |
690 | Py_DECREF(py_logic_out_chs); | |
691 | PyGILState_Release(gstate); | |
692 | ||
693 | return SRD_OK; | |
694 | ||
695 | except_out: | |
696 | srd_exception_catch("Failed to get %s decoder logic output channels", | |
697 | dec->name); | |
698 | ||
699 | err_out: | |
700 | g_slist_free_full(logic_out_chs, &logic_output_channel_free); | |
701 | Py_XDECREF(py_logic_out_chs); | |
702 | PyGILState_Release(gstate); | |
703 | ||
704 | return SRD_ERR_PYTHON; | |
705 | } | |
706 | ||
7969d803 | 707 | /* Check whether the Decoder class defines the named method. */ |
6d67d057 DE |
708 | static int check_method(PyObject *py_dec, const char *mod_name, |
709 | const char *method_name) | |
710 | { | |
711 | PyObject *py_method; | |
712 | int is_callable; | |
514b2edc UH |
713 | PyGILState_STATE gstate; |
714 | ||
715 | gstate = PyGILState_Ensure(); | |
6d67d057 DE |
716 | |
717 | py_method = PyObject_GetAttrString(py_dec, method_name); | |
718 | if (!py_method) { | |
719 | srd_exception_catch("Protocol decoder %s Decoder class " | |
720 | "has no %s() method", mod_name, method_name); | |
514b2edc | 721 | PyGILState_Release(gstate); |
6d67d057 DE |
722 | return SRD_ERR_PYTHON; |
723 | } | |
724 | ||
725 | is_callable = PyCallable_Check(py_method); | |
726 | Py_DECREF(py_method); | |
727 | ||
514b2edc UH |
728 | PyGILState_Release(gstate); |
729 | ||
6d67d057 DE |
730 | if (!is_callable) { |
731 | srd_err("Protocol decoder %s Decoder class attribute '%s' " | |
732 | "is not a method.", mod_name, method_name); | |
733 | return SRD_ERR_PYTHON; | |
2f395bff | 734 | } |
2f395bff | 735 | |
84c1c0b5 | 736 | return SRD_OK; |
2f395bff BV |
737 | } |
738 | ||
40589f25 UH |
739 | /** |
740 | * Get the API version of the specified decoder. | |
741 | * | |
742 | * @param d The decoder to use. Must not be NULL. | |
743 | * | |
744 | * @return The API version of the decoder, or 0 upon errors. | |
c947ab01 UH |
745 | * |
746 | * @private | |
40589f25 UH |
747 | */ |
748 | SRD_PRIV long srd_decoder_apiver(const struct srd_decoder *d) | |
749 | { | |
750 | PyObject *py_apiver; | |
751 | long apiver; | |
514b2edc | 752 | PyGILState_STATE gstate; |
40589f25 UH |
753 | |
754 | if (!d) | |
755 | return 0; | |
756 | ||
514b2edc UH |
757 | gstate = PyGILState_Ensure(); |
758 | ||
40589f25 UH |
759 | py_apiver = PyObject_GetAttrString(d->py_dec, "api_version"); |
760 | apiver = (py_apiver && PyLong_Check(py_apiver)) | |
761 | ? PyLong_AsLong(py_apiver) : 0; | |
762 | Py_XDECREF(py_apiver); | |
763 | ||
514b2edc UH |
764 | PyGILState_Release(gstate); |
765 | ||
40589f25 UH |
766 | return apiver; |
767 | } | |
768 | ||
1734f4c2 UH |
769 | static gboolean contains_duplicates(GSList *list) |
770 | { | |
771 | for (GSList *l1 = list; l1; l1 = l1->next) { | |
772 | for (GSList *l2 = l1->next; l2; l2 = l2->next) | |
773 | if (!strcmp(l1->data, l2->data)) | |
774 | return TRUE; | |
775 | } | |
776 | ||
777 | return FALSE; | |
778 | } | |
779 | ||
780 | static gboolean contains_duplicate_ids(GSList *list1, GSList *list2) | |
781 | { | |
782 | for (GSList *l1 = list1; l1; l1 = l1->next) { | |
783 | unsigned int cnt = 0; | |
784 | const char **s1 = l1->data; | |
785 | for (GSList *l2 = list2; l2; l2 = l2->next) { | |
786 | const char **s2 = l2->data; | |
787 | if (!strcmp(s1[0], s2[0])) | |
788 | cnt++; | |
789 | if ((list1 == list2) && cnt > 1) | |
790 | return TRUE; | |
791 | if ((list1 != list2) && cnt > 0) | |
792 | return TRUE; | |
793 | } | |
794 | } | |
795 | ||
796 | return FALSE; | |
797 | } | |
798 | ||
799 | static gboolean contains_duplicate_row_ids(GSList *list1, GSList *list2) | |
800 | { | |
801 | for (GSList *l1 = list1; l1; l1 = l1->next) { | |
802 | unsigned int cnt = 0; | |
803 | const struct srd_decoder_annotation_row *r1 = l1->data; | |
804 | for (GSList *l2 = list2; l2; l2 = l2->next) { | |
805 | const struct srd_decoder_annotation_row *r2 = l2->data; | |
806 | if (!strcmp(r1->id, r2->id)) | |
807 | cnt++; | |
808 | if (cnt > 1) | |
809 | return TRUE; | |
810 | } | |
811 | } | |
812 | ||
813 | return FALSE; | |
814 | } | |
815 | ||
b2c19614 | 816 | /** |
64c29e28 | 817 | * Load a protocol decoder module into the embedded Python interpreter. |
b2c19614 | 818 | * |
38ff5046 | 819 | * @param module_name The module name to be loaded. |
b2c19614 BV |
820 | * |
821 | * @return SRD_OK upon success, a (negative) error code otherwise. | |
8c664ca2 UH |
822 | * |
823 | * @since 0.1.0 | |
b2c19614 | 824 | */ |
9d122fd5 | 825 | SRD_API int srd_decoder_load(const char *module_name) |
b2c19614 | 826 | { |
40589f25 | 827 | PyObject *py_basedec; |
b2c19614 | 828 | struct srd_decoder *d; |
6d67d057 DE |
829 | long apiver; |
830 | int is_subclass; | |
60697682 | 831 | const char *fail_txt; |
514b2edc | 832 | PyGILState_STATE gstate; |
b2c19614 | 833 | |
e195c025 BV |
834 | if (!srd_check_init()) |
835 | return SRD_ERR; | |
836 | ||
8a014683 BV |
837 | if (!module_name) |
838 | return SRD_ERR_ARG; | |
839 | ||
514b2edc UH |
840 | gstate = PyGILState_Ensure(); |
841 | ||
aac68131 BV |
842 | if (PyDict_GetItemString(PyImport_GetModuleDict(), module_name)) { |
843 | /* Module was already imported. */ | |
514b2edc | 844 | PyGILState_Release(gstate); |
aac68131 BV |
845 | return SRD_OK; |
846 | } | |
847 | ||
077fa8ac | 848 | d = g_malloc0(sizeof(struct srd_decoder)); |
60697682 | 849 | fail_txt = NULL; |
b2c19614 | 850 | |
e9dd2fea | 851 | d->py_mod = py_import_by_name(module_name); |
60697682 GS |
852 | if (!d->py_mod) { |
853 | fail_txt = "import by name failed"; | |
6d67d057 | 854 | goto except_out; |
60697682 | 855 | } |
6d67d057 DE |
856 | |
857 | if (!mod_sigrokdecode) { | |
858 | srd_err("sigrokdecode module not loaded."); | |
60697682 | 859 | fail_txt = "sigrokdecode(3) not loaded"; |
451680f1 BV |
860 | goto err_out; |
861 | } | |
b2c19614 | 862 | |
451680f1 | 863 | /* Get the 'Decoder' class as Python object. */ |
6d67d057 | 864 | d->py_dec = PyObject_GetAttrString(d->py_mod, "Decoder"); |
60697682 GS |
865 | if (!d->py_dec) { |
866 | fail_txt = "no 'Decoder' attribute in imported module"; | |
6d67d057 | 867 | goto except_out; |
60697682 | 868 | } |
b2c19614 | 869 | |
6d67d057 | 870 | py_basedec = PyObject_GetAttrString(mod_sigrokdecode, "Decoder"); |
60697682 GS |
871 | if (!py_basedec) { |
872 | fail_txt = "no 'Decoder' attribute in sigrokdecode(3)"; | |
6d67d057 | 873 | goto except_out; |
60697682 | 874 | } |
6d67d057 DE |
875 | |
876 | is_subclass = PyObject_IsSubclass(d->py_dec, py_basedec); | |
877 | Py_DECREF(py_basedec); | |
b2c19614 | 878 | |
6d67d057 | 879 | if (!is_subclass) { |
451680f1 | 880 | srd_err("Decoder class in protocol decoder module %s is not " |
9b37109d | 881 | "a subclass of sigrokdecode.Decoder.", module_name); |
60697682 | 882 | fail_txt = "not a subclass of sigrokdecode.Decoder"; |
451680f1 BV |
883 | goto err_out; |
884 | } | |
b2c19614 | 885 | |
5c0c9cb3 | 886 | /* |
077fa8ac | 887 | * Check that this decoder has the correct PD API version. |
5c0c9cb3 UH |
888 | * PDs of different API versions are incompatible and cannot work. |
889 | */ | |
40589f25 | 890 | apiver = srd_decoder_apiver(d); |
eb883723 UH |
891 | if (apiver != 3) { |
892 | srd_exception_catch("Only PD API version 3 is supported, " | |
60697682 GS |
893 | "decoder %s has version %ld", module_name, apiver); |
894 | fail_txt = "API version mismatch"; | |
1d552cd3 BV |
895 | goto err_out; |
896 | } | |
1d552cd3 | 897 | |
7969d803 UH |
898 | /* Check Decoder class for required methods. */ |
899 | ||
be760e59 GS |
900 | if (check_method(d->py_dec, module_name, "reset") != SRD_OK) { |
901 | fail_txt = "no 'reset()' method"; | |
902 | goto err_out; | |
903 | } | |
904 | ||
60697682 GS |
905 | if (check_method(d->py_dec, module_name, "start") != SRD_OK) { |
906 | fail_txt = "no 'start()' method"; | |
1d552cd3 | 907 | goto err_out; |
60697682 | 908 | } |
6d67d057 | 909 | |
60697682 GS |
910 | if (check_method(d->py_dec, module_name, "decode") != SRD_OK) { |
911 | fail_txt = "no 'decode()' method"; | |
1d552cd3 | 912 | goto err_out; |
60697682 | 913 | } |
1d552cd3 | 914 | |
84c1c0b5 | 915 | /* Store required fields in newly allocated strings. */ |
60697682 GS |
916 | if (py_attr_as_str(d->py_dec, "id", &(d->id)) != SRD_OK) { |
917 | fail_txt = "no 'id' attribute"; | |
84c1c0b5 | 918 | goto err_out; |
60697682 | 919 | } |
84c1c0b5 | 920 | |
60697682 GS |
921 | if (py_attr_as_str(d->py_dec, "name", &(d->name)) != SRD_OK) { |
922 | fail_txt = "no 'name' attribute"; | |
84c1c0b5 | 923 | goto err_out; |
60697682 | 924 | } |
84c1c0b5 | 925 | |
60697682 GS |
926 | if (py_attr_as_str(d->py_dec, "longname", &(d->longname)) != SRD_OK) { |
927 | fail_txt = "no 'longname' attribute"; | |
84c1c0b5 | 928 | goto err_out; |
60697682 | 929 | } |
84c1c0b5 | 930 | |
60697682 GS |
931 | if (py_attr_as_str(d->py_dec, "desc", &(d->desc)) != SRD_OK) { |
932 | fail_txt = "no 'desc' attribute"; | |
84c1c0b5 | 933 | goto err_out; |
60697682 | 934 | } |
84c1c0b5 | 935 | |
60697682 GS |
936 | if (py_attr_as_str(d->py_dec, "license", &(d->license)) != SRD_OK) { |
937 | fail_txt = "no 'license' attribute"; | |
84c1c0b5 | 938 | goto err_out; |
60697682 | 939 | } |
84c1c0b5 | 940 | |
d480174d UH |
941 | if (py_attr_as_strlist(d->py_dec, "inputs", &(d->inputs)) != SRD_OK) { |
942 | fail_txt = "missing or malformed 'inputs' attribute"; | |
943 | goto err_out; | |
944 | } | |
945 | ||
946 | if (py_attr_as_strlist(d->py_dec, "outputs", &(d->outputs)) != SRD_OK) { | |
947 | fail_txt = "missing or malformed 'outputs' attribute"; | |
948 | goto err_out; | |
949 | } | |
950 | ||
4c180223 SA |
951 | if (py_attr_as_strlist(d->py_dec, "tags", &(d->tags)) != SRD_OK) { |
952 | fail_txt = "missing or malformed 'tags' attribute"; | |
953 | goto err_out; | |
954 | } | |
955 | ||
84c1c0b5 | 956 | /* All options and their default values. */ |
60697682 GS |
957 | if (get_options(d) != SRD_OK) { |
958 | fail_txt = "cannot get options"; | |
2f395bff | 959 | goto err_out; |
60697682 | 960 | } |
11ea8ae1 | 961 | |
6a15597a | 962 | /* Check and import required channels. */ |
60697682 GS |
963 | if (get_channels(d, "channels", &d->channels, 0) != SRD_OK) { |
964 | fail_txt = "cannot get channels"; | |
f38ec285 | 965 | goto err_out; |
60697682 | 966 | } |
f38ec285 | 967 | |
6a15597a | 968 | /* Check and import optional channels. */ |
6d67d057 | 969 | if (get_channels(d, "optional_channels", &d->opt_channels, |
60697682 GS |
970 | g_slist_length(d->channels)) != SRD_OK) { |
971 | fail_txt = "cannot get optional channels"; | |
f38ec285 | 972 | goto err_out; |
60697682 | 973 | } |
f38ec285 | 974 | |
60697682 GS |
975 | if (get_annotations(d) != SRD_OK) { |
976 | fail_txt = "cannot get annotations"; | |
6d67d057 | 977 | goto err_out; |
60697682 | 978 | } |
1ce46817 | 979 | |
60697682 GS |
980 | if (get_annotation_rows(d) != SRD_OK) { |
981 | fail_txt = "cannot get annotation rows"; | |
6d67d057 | 982 | goto err_out; |
60697682 | 983 | } |
d75d8a7c | 984 | |
60697682 GS |
985 | if (get_binary_classes(d) != SRD_OK) { |
986 | fail_txt = "cannot get binary classes"; | |
6d67d057 | 987 | goto err_out; |
60697682 | 988 | } |
d75d8a7c | 989 | |
1734f4c2 UH |
990 | if (contains_duplicates(d->inputs)) { |
991 | fail_txt = "duplicate input IDs"; | |
992 | goto err_out; | |
993 | } | |
994 | ||
995 | if (contains_duplicates(d->outputs)) { | |
996 | fail_txt = "duplicate output IDs"; | |
997 | goto err_out; | |
998 | } | |
999 | ||
1000 | if (contains_duplicates(d->tags)) { | |
1001 | fail_txt = "duplicate tags"; | |
1002 | goto err_out; | |
1003 | } | |
1004 | ||
1005 | if (contains_duplicate_ids(d->channels, d->channels)) { | |
1006 | fail_txt = "duplicate channel IDs"; | |
1007 | goto err_out; | |
1008 | } | |
1009 | ||
1010 | if (contains_duplicate_ids(d->opt_channels, d->opt_channels)) { | |
1011 | fail_txt = "duplicate optional channel IDs"; | |
1012 | goto err_out; | |
1013 | } | |
1014 | ||
1015 | if (contains_duplicate_ids(d->channels, d->opt_channels)) { | |
1016 | fail_txt = "channel and optional channel IDs contain duplicates"; | |
1017 | goto err_out; | |
1018 | } | |
1019 | ||
1020 | if (contains_duplicate_ids(d->options, d->options)) { | |
1021 | fail_txt = "duplicate option IDs"; | |
1022 | goto err_out; | |
1023 | } | |
1024 | ||
1025 | if (contains_duplicate_ids(d->annotations, d->annotations)) { | |
1026 | fail_txt = "duplicate annotation class IDs"; | |
1027 | goto err_out; | |
1028 | } | |
1029 | ||
1030 | if (contains_duplicate_row_ids(d->annotation_rows, d->annotation_rows)) { | |
1031 | fail_txt = "duplicate annotation row IDs"; | |
1032 | goto err_out; | |
1033 | } | |
1034 | ||
1035 | if (contains_duplicate_ids(d->annotations, d->annotation_rows)) { | |
1036 | fail_txt = "annotation class/row IDs contain duplicates"; | |
1037 | goto err_out; | |
1038 | } | |
1039 | ||
1040 | if (contains_duplicate_ids(d->binary, d->binary)) { | |
1041 | fail_txt = "duplicate binary class IDs"; | |
1042 | goto err_out; | |
1043 | } | |
1044 | ||
ddcde186 UH |
1045 | if (get_logic_output_channels(d) != SRD_OK) { |
1046 | fail_txt = "cannot get logic output channels"; | |
1047 | goto err_out; | |
1048 | } | |
1049 | ||
514b2edc UH |
1050 | PyGILState_Release(gstate); |
1051 | ||
c63f5a87 | 1052 | /* Append it to the list of loaded decoders. */ |
9b37109d BV |
1053 | pd_list = g_slist_append(pd_list, d); |
1054 | ||
6d67d057 | 1055 | return SRD_OK; |
451680f1 | 1056 | |
6d67d057 | 1057 | except_out: |
e51475ad UH |
1058 | /* Don't show a message for the "common" directory, it's not a PD. */ |
1059 | if (strcmp(module_name, "common")) { | |
1060 | srd_exception_catch("Failed to load decoder %s: %s", | |
1061 | module_name, fail_txt); | |
1062 | } | |
01b60707 | 1063 | fail_txt = NULL; |
7969d803 | 1064 | |
451680f1 | 1065 | err_out: |
60697682 GS |
1066 | if (fail_txt) |
1067 | srd_err("Failed to load decoder %s: %s", module_name, fail_txt); | |
6d67d057 | 1068 | decoder_free(d); |
514b2edc | 1069 | PyGILState_Release(gstate); |
b2c19614 | 1070 | |
6d67d057 | 1071 | return SRD_ERR_PYTHON; |
451680f1 BV |
1072 | } |
1073 | ||
582c8473 BV |
1074 | /** |
1075 | * Return a protocol decoder's docstring. | |
1076 | * | |
f8c24e2e | 1077 | * @param dec The loaded protocol decoder. Must not be NULL. |
582c8473 | 1078 | * |
361fdcaa | 1079 | * @return A newly allocated buffer containing the protocol decoder's |
abeeed8b | 1080 | * documentation. The caller is responsible for free'ing the buffer. |
8c664ca2 UH |
1081 | * |
1082 | * @since 0.1.0 | |
582c8473 | 1083 | */ |
b33b8aa5 | 1084 | SRD_API char *srd_decoder_doc_get(const struct srd_decoder *dec) |
451680f1 | 1085 | { |
e7edca07 | 1086 | PyObject *py_str; |
451680f1 | 1087 | char *doc; |
514b2edc | 1088 | PyGILState_STATE gstate; |
451680f1 | 1089 | |
e195c025 BV |
1090 | if (!srd_check_init()) |
1091 | return NULL; | |
1092 | ||
f8c24e2e | 1093 | if (!dec || !dec->py_mod) |
e195c025 BV |
1094 | return NULL; |
1095 | ||
514b2edc UH |
1096 | gstate = PyGILState_Ensure(); |
1097 | ||
e7edca07 | 1098 | if (!PyObject_HasAttrString(dec->py_mod, "__doc__")) |
514b2edc | 1099 | goto err; |
e7edca07 BV |
1100 | |
1101 | if (!(py_str = PyObject_GetAttrString(dec->py_mod, "__doc__"))) { | |
201a85a8 | 1102 | srd_exception_catch("Failed to get docstring"); |
514b2edc | 1103 | goto err; |
e7edca07 BV |
1104 | } |
1105 | ||
451680f1 | 1106 | doc = NULL; |
e7edca07 BV |
1107 | if (py_str != Py_None) |
1108 | py_str_as_str(py_str, &doc); | |
6d67d057 | 1109 | Py_DECREF(py_str); |
451680f1 | 1110 | |
514b2edc UH |
1111 | PyGILState_Release(gstate); |
1112 | ||
451680f1 | 1113 | return doc; |
514b2edc UH |
1114 | |
1115 | err: | |
1116 | PyGILState_Release(gstate); | |
1117 | ||
1118 | return NULL; | |
b2c19614 BV |
1119 | } |
1120 | ||
b2c19614 | 1121 | /** |
4cc0d9fe | 1122 | * Unload the specified protocol decoder. |
451680f1 | 1123 | * |
fa12a21e | 1124 | * @param dec The struct srd_decoder to be unloaded. |
451680f1 BV |
1125 | * |
1126 | * @return SRD_OK upon success, a (negative) error code otherwise. | |
8c664ca2 UH |
1127 | * |
1128 | * @since 0.1.0 | |
b2c19614 | 1129 | */ |
9d122fd5 | 1130 | SRD_API int srd_decoder_unload(struct srd_decoder *dec) |
b2c19614 | 1131 | { |
32cfb920 | 1132 | struct srd_session *sess; |
2f395bff BV |
1133 | GSList *l; |
1134 | ||
e195c025 BV |
1135 | if (!srd_check_init()) |
1136 | return SRD_ERR; | |
1137 | ||
1138 | if (!dec) | |
1139 | return SRD_ERR_ARG; | |
1140 | ||
361fdcaa UH |
1141 | /* |
1142 | * Since any instances of this decoder need to be released as well, | |
fa12a21e BV |
1143 | * but they could be anywhere in the stack, just free the entire |
1144 | * stack. A frontend reloading a decoder thus has to restart all | |
361fdcaa UH |
1145 | * instances, and rebuild the stack. |
1146 | */ | |
32cfb920 BV |
1147 | for (l = sessions; l; l = l->next) { |
1148 | sess = l->data; | |
3262ef02 | 1149 | srd_inst_free_all(sess); |
32cfb920 | 1150 | } |
fa12a21e | 1151 | |
c63f5a87 UH |
1152 | /* Remove the PD from the list of loaded decoders. */ |
1153 | pd_list = g_slist_remove(pd_list, dec); | |
1154 | ||
6d67d057 | 1155 | decoder_free(dec); |
b2c19614 BV |
1156 | |
1157 | return SRD_OK; | |
1158 | } | |
1159 | ||
1d757906 | 1160 | static void srd_decoder_load_all_zip_path(char *zip_path) |
6a034159 MC |
1161 | { |
1162 | PyObject *zipimport_mod, *zipimporter_class, *zipimporter; | |
1163 | PyObject *prefix_obj, *files, *key, *value, *set, *modname; | |
1164 | Py_ssize_t pos = 0; | |
1165 | char *prefix; | |
1166 | size_t prefix_len; | |
514b2edc | 1167 | PyGILState_STATE gstate; |
6a034159 MC |
1168 | |
1169 | set = files = prefix_obj = zipimporter = zipimporter_class = NULL; | |
1170 | ||
514b2edc UH |
1171 | gstate = PyGILState_Ensure(); |
1172 | ||
e9dd2fea | 1173 | zipimport_mod = py_import_by_name("zipimport"); |
6a034159 MC |
1174 | if (zipimport_mod == NULL) |
1175 | goto err_out; | |
1176 | ||
1177 | zipimporter_class = PyObject_GetAttrString(zipimport_mod, "zipimporter"); | |
1178 | if (zipimporter_class == NULL) | |
1179 | goto err_out; | |
1180 | ||
1d757906 | 1181 | zipimporter = PyObject_CallFunction(zipimporter_class, "s", zip_path); |
6a034159 MC |
1182 | if (zipimporter == NULL) |
1183 | goto err_out; | |
1184 | ||
1185 | prefix_obj = PyObject_GetAttrString(zipimporter, "prefix"); | |
1186 | if (prefix_obj == NULL) | |
1187 | goto err_out; | |
1188 | ||
1189 | files = PyObject_GetAttrString(zipimporter, "_files"); | |
6d67d057 | 1190 | if (files == NULL || !PyDict_Check(files)) |
6a034159 MC |
1191 | goto err_out; |
1192 | ||
1193 | set = PySet_New(NULL); | |
1194 | if (set == NULL) | |
1195 | goto err_out; | |
1196 | ||
1197 | if (py_str_as_str(prefix_obj, &prefix) != SRD_OK) | |
1198 | goto err_out; | |
1199 | ||
1200 | prefix_len = strlen(prefix); | |
1201 | ||
1202 | while (PyDict_Next(files, &pos, &key, &value)) { | |
1203 | char *path, *slash; | |
1204 | if (py_str_as_str(key, &path) == SRD_OK) { | |
6d67d057 DE |
1205 | if (strlen(path) > prefix_len |
1206 | && memcmp(path, prefix, prefix_len) == 0 | |
1207 | && (slash = strchr(path + prefix_len, '/'))) { | |
1208 | ||
1209 | modname = PyUnicode_FromStringAndSize(path + prefix_len, | |
1210 | slash - (path + prefix_len)); | |
6a034159 MC |
1211 | if (modname == NULL) { |
1212 | PyErr_Clear(); | |
1213 | } else { | |
1214 | PySet_Add(set, modname); | |
6d67d057 | 1215 | Py_DECREF(modname); |
6a034159 MC |
1216 | } |
1217 | } | |
6d67d057 | 1218 | g_free(path); |
6a034159 MC |
1219 | } |
1220 | } | |
6d67d057 | 1221 | g_free(prefix); |
6a034159 MC |
1222 | |
1223 | while ((modname = PySet_Pop(set))) { | |
1224 | char *modname_str; | |
1225 | if (py_str_as_str(modname, &modname_str) == SRD_OK) { | |
1226 | /* The directory name is the module name (e.g. "i2c"). */ | |
1227 | srd_decoder_load(modname_str); | |
6d67d057 | 1228 | g_free(modname_str); |
6a034159 | 1229 | } |
6d67d057 | 1230 | Py_DECREF(modname); |
6a034159 MC |
1231 | } |
1232 | ||
1233 | err_out: | |
1234 | Py_XDECREF(set); | |
1235 | Py_XDECREF(files); | |
1236 | Py_XDECREF(prefix_obj); | |
1237 | Py_XDECREF(zipimporter); | |
1238 | Py_XDECREF(zipimporter_class); | |
1239 | Py_XDECREF(zipimport_mod); | |
1240 | PyErr_Clear(); | |
514b2edc | 1241 | PyGILState_Release(gstate); |
6a034159 MC |
1242 | } |
1243 | ||
ea81b49a BV |
1244 | static void srd_decoder_load_all_path(char *path) |
1245 | { | |
1246 | GDir *dir; | |
1247 | const gchar *direntry; | |
1248 | ||
6a034159 | 1249 | if (!(dir = g_dir_open(path, 0, NULL))) { |
7969d803 | 1250 | /* Not really fatal. Try zipimport method too. */ |
6a034159 | 1251 | srd_decoder_load_all_zip_path(path); |
ea81b49a | 1252 | return; |
6a034159 | 1253 | } |
ea81b49a | 1254 | |
7969d803 UH |
1255 | /* |
1256 | * This ignores errors returned by srd_decoder_load(). That | |
ea81b49a | 1257 | * function will have logged the cause, but in any case we |
7969d803 UH |
1258 | * want to continue anyway. |
1259 | */ | |
ea81b49a BV |
1260 | while ((direntry = g_dir_read_name(dir)) != NULL) { |
1261 | /* The directory name is the module name (e.g. "i2c"). */ | |
1262 | srd_decoder_load(direntry); | |
1263 | } | |
1264 | g_dir_close(dir); | |
ea81b49a BV |
1265 | } |
1266 | ||
582c8473 | 1267 | /** |
9b37109d | 1268 | * Load all installed protocol decoders. |
582c8473 BV |
1269 | * |
1270 | * @return SRD_OK upon success, a (negative) error code otherwise. | |
8c664ca2 UH |
1271 | * |
1272 | * @since 0.1.0 | |
582c8473 | 1273 | */ |
8ad6e500 | 1274 | SRD_API int srd_decoder_load_all(void) |
b2c19614 | 1275 | { |
ea81b49a | 1276 | GSList *l; |
b2c19614 | 1277 | |
e195c025 BV |
1278 | if (!srd_check_init()) |
1279 | return SRD_ERR; | |
1280 | ||
ea81b49a BV |
1281 | for (l = searchpaths; l; l = l->next) |
1282 | srd_decoder_load_all_path(l->data); | |
b2c19614 BV |
1283 | |
1284 | return SRD_OK; | |
1285 | } | |
1286 | ||
611a7340 JB |
1287 | static void srd_decoder_unload_cb(void *arg, void *ignored) |
1288 | { | |
1289 | (void)ignored; | |
1290 | ||
1291 | srd_decoder_unload((struct srd_decoder *)arg); | |
1292 | } | |
1293 | ||
b2c19614 | 1294 | /** |
582c8473 BV |
1295 | * Unload all loaded protocol decoders. |
1296 | * | |
1297 | * @return SRD_OK upon success, a (negative) error code otherwise. | |
8c664ca2 UH |
1298 | * |
1299 | * @since 0.1.0 | |
b2c19614 | 1300 | */ |
8ad6e500 | 1301 | SRD_API int srd_decoder_unload_all(void) |
b2c19614 | 1302 | { |
611a7340 | 1303 | g_slist_foreach(pd_list, srd_decoder_unload_cb, NULL); |
820bf448 BV |
1304 | g_slist_free(pd_list); |
1305 | pd_list = NULL; | |
b2c19614 BV |
1306 | |
1307 | return SRD_OK; | |
1308 | } | |
4895418c UH |
1309 | |
1310 | /** @} */ |