]>
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 | |
32cfb920 BV |
45 | extern GSList *sessions; |
46 | ||
c9bfccc6 | 47 | /* module_sigrokdecode.c */ |
55c3c5f4 | 48 | extern SRD_PRIV PyObject *mod_sigrokdecode; |
451680f1 | 49 | |
57790bc8 UH |
50 | /** @endcond */ |
51 | ||
b2c19614 BV |
52 | /** |
53 | * Returns the list of supported/loaded protocol decoders. | |
54 | * | |
55 | * This is a GSList containing the names of the decoders as strings. | |
56 | * | |
57 | * @return List of decoders, NULL if none are supported or loaded. | |
8c664ca2 UH |
58 | * |
59 | * @since 0.1.0 (but the API changed in 0.2.0) | |
b2c19614 | 60 | */ |
38ff5046 | 61 | SRD_API const GSList *srd_decoder_list(void) |
b2c19614 | 62 | { |
e5080882 | 63 | return pd_list; |
b2c19614 BV |
64 | } |
65 | ||
b2c19614 BV |
66 | /** |
67 | * Get the decoder with the specified ID. | |
68 | * | |
69 | * @param id The ID string of the decoder to return. | |
582c8473 | 70 | * |
b2c19614 | 71 | * @return The decoder with the specified ID, or NULL if not found. |
8c664ca2 UH |
72 | * |
73 | * @since 0.1.0 | |
b2c19614 | 74 | */ |
9d122fd5 | 75 | SRD_API struct srd_decoder *srd_decoder_get_by_id(const char *id) |
b2c19614 BV |
76 | { |
77 | GSList *l; | |
78 | struct srd_decoder *dec; | |
79 | ||
38ff5046 | 80 | for (l = pd_list; l; l = l->next) { |
b2c19614 BV |
81 | dec = l->data; |
82 | if (!strcmp(dec->id, id)) | |
83 | return dec; | |
84 | } | |
85 | ||
86 | return NULL; | |
87 | } | |
88 | ||
5a2c4dc4 | 89 | static int get_probes(const struct srd_decoder *d, const char *attr, |
22c9bc2a | 90 | GSList **pl) |
f38ec285 BV |
91 | { |
92 | PyObject *py_probelist, *py_entry; | |
93 | struct srd_probe *p; | |
94 | int ret, num_probes, i; | |
95 | ||
96 | if (!PyObject_HasAttrString(d->py_dec, attr)) | |
97 | /* No probes of this type specified. */ | |
98 | return SRD_OK; | |
99 | ||
f38ec285 BV |
100 | py_probelist = PyObject_GetAttrString(d->py_dec, attr); |
101 | if (!PyList_Check(py_probelist)) { | |
22c9bc2a BV |
102 | srd_err("Protocol decoder %s %s attribute is not a list.", |
103 | d->name, attr); | |
104 | return SRD_ERR_PYTHON; | |
f38ec285 BV |
105 | } |
106 | ||
22c9bc2a | 107 | if ((num_probes = PyList_Size(py_probelist)) == 0) |
f38ec285 BV |
108 | /* Empty probelist. */ |
109 | return SRD_OK; | |
110 | ||
22c9bc2a | 111 | ret = SRD_OK; |
f38ec285 BV |
112 | for (i = 0; i < num_probes; i++) { |
113 | py_entry = PyList_GetItem(py_probelist, i); | |
114 | if (!PyDict_Check(py_entry)) { | |
115 | srd_err("Protocol decoder %s %s attribute is not " | |
c9bfccc6 | 116 | "a list with dict elements.", d->name, attr); |
22c9bc2a BV |
117 | ret = SRD_ERR_PYTHON; |
118 | break; | |
f38ec285 BV |
119 | } |
120 | ||
121 | if (!(p = g_try_malloc(sizeof(struct srd_probe)))) { | |
a61ece20 | 122 | srd_err("Failed to g_malloc() struct srd_probe."); |
f38ec285 | 123 | ret = SRD_ERR_MALLOC; |
22c9bc2a | 124 | break; |
f38ec285 BV |
125 | } |
126 | ||
22c9bc2a BV |
127 | if ((py_dictitem_as_str(py_entry, "id", &p->id)) != SRD_OK) { |
128 | ret = SRD_ERR_PYTHON; | |
129 | break; | |
130 | } | |
131 | if ((py_dictitem_as_str(py_entry, "name", &p->name)) != SRD_OK) { | |
132 | ret = SRD_ERR_PYTHON; | |
133 | break; | |
134 | } | |
135 | if ((py_dictitem_as_str(py_entry, "desc", &p->desc)) != SRD_OK) { | |
136 | ret = SRD_ERR_PYTHON; | |
137 | break; | |
138 | } | |
f38ec285 BV |
139 | p->order = i; |
140 | ||
141 | *pl = g_slist_append(*pl, p); | |
142 | } | |
f38ec285 | 143 | |
f38ec285 BV |
144 | Py_DecRef(py_probelist); |
145 | ||
146 | return ret; | |
147 | } | |
148 | ||
2f395bff BV |
149 | static int get_options(struct srd_decoder *d) |
150 | { | |
151 | PyObject *py_opts, *py_keys, *py_values, *py_val, *py_desc, *py_default; | |
152 | Py_ssize_t i; | |
153 | struct srd_decoder_option *o; | |
154 | gint64 def_long; | |
155 | int num_keys, overflow, ret; | |
156 | char *key, *def_str; | |
157 | ||
158 | ret = SRD_ERR_PYTHON; | |
159 | key = NULL; | |
160 | ||
161 | if (!PyObject_HasAttrString(d->py_dec, "options")) | |
162 | /* That's fine. */ | |
163 | return SRD_OK; | |
164 | ||
165 | /* If present, options must be a dictionary. */ | |
166 | py_opts = PyObject_GetAttrString(d->py_dec, "options"); | |
167 | if (!PyDict_Check(py_opts)) { | |
168 | srd_err("Protocol decoder %s options attribute is not " | |
169 | "a dictionary.", d->name); | |
170 | goto err_out; | |
171 | } | |
172 | ||
173 | py_keys = PyDict_Keys(py_opts); | |
174 | py_values = PyDict_Values(py_opts); | |
175 | num_keys = PyList_Size(py_keys); | |
176 | for (i = 0; i < num_keys; i++) { | |
177 | py_str_as_str(PyList_GetItem(py_keys, i), &key); | |
178 | srd_dbg("option '%s'", key); | |
179 | py_val = PyList_GetItem(py_values, i); | |
180 | if (!PyList_Check(py_val) || PyList_Size(py_val) != 2) { | |
181 | srd_err("Protocol decoder %s option '%s' value must be " | |
182 | "a list with two elements.", d->name, key); | |
183 | goto err_out; | |
184 | } | |
185 | py_desc = PyList_GetItem(py_val, 0); | |
186 | if (!PyUnicode_Check(py_desc)) { | |
187 | srd_err("Protocol decoder %s option '%s' has no " | |
188 | "description.", d->name, key); | |
189 | goto err_out; | |
190 | } | |
191 | py_default = PyList_GetItem(py_val, 1); | |
192 | if (!PyUnicode_Check(py_default) && !PyLong_Check(py_default)) { | |
193 | srd_err("Protocol decoder %s option '%s' has default " | |
194 | "of unsupported type '%s'.", d->name, key, | |
195 | Py_TYPE(py_default)->tp_name); | |
196 | goto err_out; | |
197 | } | |
198 | if (!(o = g_try_malloc(sizeof(struct srd_decoder_option)))) { | |
199 | srd_err("option malloc failed"); | |
200 | goto err_out; | |
201 | } | |
202 | o->id = g_strdup(key); | |
203 | py_str_as_str(py_desc, &o->desc); | |
204 | if (PyUnicode_Check(py_default)) { | |
205 | /* UTF-8 string */ | |
206 | py_str_as_str(py_default, &def_str); | |
207 | o->def = g_variant_new_string(def_str); | |
208 | g_free(def_str); | |
209 | } else { | |
210 | /* Long */ | |
211 | def_long = PyLong_AsLongAndOverflow(py_default, &overflow); | |
212 | if (overflow) { | |
213 | /* Value is < LONG_MIN or > LONG_MAX */ | |
214 | PyErr_Clear(); | |
215 | srd_err("Protocol decoder %s option '%s' has " | |
216 | "invalid default value.", d->name, key); | |
217 | goto err_out; | |
218 | } | |
219 | o->def = g_variant_new_int64(def_long); | |
220 | } | |
221 | g_variant_ref_sink(o->def); | |
222 | d->options = g_slist_append(d->options, o); | |
e592ac89 | 223 | g_free(key); |
ca3fc3c0 | 224 | key = NULL; |
2f395bff BV |
225 | } |
226 | Py_DecRef(py_keys); | |
227 | Py_DecRef(py_values); | |
228 | ||
229 | ret = SRD_OK; | |
230 | ||
231 | err_out: | |
232 | Py_XDECREF(py_opts); | |
233 | g_free(key); | |
234 | ||
235 | return ret; | |
236 | } | |
237 | ||
b2c19614 | 238 | /** |
64c29e28 | 239 | * Load a protocol decoder module into the embedded Python interpreter. |
b2c19614 | 240 | * |
38ff5046 | 241 | * @param module_name The module name to be loaded. |
b2c19614 BV |
242 | * |
243 | * @return SRD_OK upon success, a (negative) error code otherwise. | |
8c664ca2 UH |
244 | * |
245 | * @since 0.1.0 | |
b2c19614 | 246 | */ |
9d122fd5 | 247 | SRD_API int srd_decoder_load(const char *module_name) |
b2c19614 | 248 | { |
d75d8a7c BV |
249 | PyObject *py_basedec, *py_method, *py_attr, *py_annlist, *py_ann, \ |
250 | *py_bin_classes, *py_bin_class; | |
b2c19614 | 251 | struct srd_decoder *d; |
d75d8a7c BV |
252 | int len, ret, i; |
253 | char **ann, *bin; | |
38ff5046 UH |
254 | struct srd_probe *p; |
255 | GSList *l; | |
b2c19614 | 256 | |
e195c025 BV |
257 | if (!srd_check_init()) |
258 | return SRD_ERR; | |
259 | ||
8a014683 BV |
260 | if (!module_name) |
261 | return SRD_ERR_ARG; | |
262 | ||
aac68131 BV |
263 | if (PyDict_GetItemString(PyImport_GetModuleDict(), module_name)) { |
264 | /* Module was already imported. */ | |
265 | return SRD_OK; | |
266 | } | |
267 | ||
8ad6e500 | 268 | srd_dbg("Loading protocol decoder '%s'.", module_name); |
64c29e28 | 269 | |
d906d3f9 | 270 | py_basedec = py_method = py_attr = NULL; |
b2c19614 | 271 | |
451680f1 | 272 | if (!(d = g_try_malloc0(sizeof(struct srd_decoder)))) { |
a61ece20 | 273 | srd_dbg("Failed to g_malloc() struct srd_decoder."); |
451680f1 BV |
274 | ret = SRD_ERR_MALLOC; |
275 | goto err_out; | |
b2c19614 BV |
276 | } |
277 | ||
21481b66 BV |
278 | ret = SRD_ERR_PYTHON; |
279 | ||
451680f1 | 280 | /* Import the Python module. */ |
9b37109d | 281 | if (!(d->py_mod = PyImport_ImportModule(module_name))) { |
aafeeaea | 282 | srd_exception_catch("Import of '%s' failed.", module_name); |
451680f1 BV |
283 | goto err_out; |
284 | } | |
b2c19614 | 285 | |
451680f1 BV |
286 | /* Get the 'Decoder' class as Python object. */ |
287 | if (!(d->py_dec = PyObject_GetAttrString(d->py_mod, "Decoder"))) { | |
288 | /* This generated an AttributeError exception. */ | |
289 | PyErr_Clear(); | |
361fdcaa UH |
290 | srd_err("Decoder class not found in protocol decoder %s.", |
291 | module_name); | |
451680f1 BV |
292 | goto err_out; |
293 | } | |
b2c19614 | 294 | |
451680f1 | 295 | if (!(py_basedec = PyObject_GetAttrString(mod_sigrokdecode, "Decoder"))) { |
7a1712c4 | 296 | srd_dbg("sigrokdecode module not loaded."); |
451680f1 BV |
297 | goto err_out; |
298 | } | |
b2c19614 | 299 | |
451680f1 BV |
300 | if (!PyObject_IsSubclass(d->py_dec, py_basedec)) { |
301 | srd_err("Decoder class in protocol decoder module %s is not " | |
9b37109d | 302 | "a subclass of sigrokdecode.Decoder.", module_name); |
451680f1 BV |
303 | goto err_out; |
304 | } | |
ad022d94 | 305 | Py_CLEAR(py_basedec); |
b2c19614 | 306 | |
1d552cd3 BV |
307 | /* Check for a proper start() method. */ |
308 | if (!PyObject_HasAttrString(d->py_dec, "start")) { | |
309 | srd_err("Protocol decoder %s has no start() method Decoder " | |
9b37109d | 310 | "class.", module_name); |
1d552cd3 BV |
311 | goto err_out; |
312 | } | |
313 | py_method = PyObject_GetAttrString(d->py_dec, "start"); | |
314 | if (!PyFunction_Check(py_method)) { | |
d906d3f9 | 315 | srd_err("Protocol decoder %s Decoder class attribute 'start' " |
9b37109d | 316 | "is not a method.", module_name); |
1d552cd3 BV |
317 | goto err_out; |
318 | } | |
ad022d94 | 319 | Py_CLEAR(py_method); |
1d552cd3 BV |
320 | |
321 | /* Check for a proper decode() method. */ | |
322 | if (!PyObject_HasAttrString(d->py_dec, "decode")) { | |
323 | srd_err("Protocol decoder %s has no decode() method Decoder " | |
9b37109d | 324 | "class.", module_name); |
1d552cd3 BV |
325 | goto err_out; |
326 | } | |
327 | py_method = PyObject_GetAttrString(d->py_dec, "decode"); | |
328 | if (!PyFunction_Check(py_method)) { | |
329 | srd_err("Protocol decoder %s Decoder class attribute 'decode' " | |
9b37109d | 330 | "is not a method.", module_name); |
1d552cd3 BV |
331 | goto err_out; |
332 | } | |
ad022d94 | 333 | Py_CLEAR(py_method); |
1d552cd3 | 334 | |
2f395bff BV |
335 | if (get_options(d) != SRD_OK) |
336 | goto err_out; | |
11ea8ae1 | 337 | |
f38ec285 BV |
338 | /* Check and import required probes. */ |
339 | if (get_probes(d, "probes", &d->probes) != SRD_OK) | |
340 | goto err_out; | |
341 | ||
342 | /* Check and import optional probes. */ | |
dcdf4883 | 343 | if (get_probes(d, "optional_probes", &d->opt_probes) != SRD_OK) |
f38ec285 BV |
344 | goto err_out; |
345 | ||
38ff5046 UH |
346 | /* |
347 | * Fix order numbers for the optional probes. | |
348 | * | |
349 | * Example: | |
350 | * Required probes: r1, r2, r3. Optional: o1, o2, o3, o4. | |
351 | * 'order' fields in the d->probes list = 0, 1, 2. | |
352 | * 'order' fields in the d->opt_probes list = 3, 4, 5, 6. | |
353 | */ | |
354 | for (l = d->opt_probes; l; l = l->next) { | |
355 | p = l->data; | |
356 | p->order += g_slist_length(d->probes); | |
357 | } | |
358 | ||
f38ec285 | 359 | /* Store required fields in newly allocated strings. */ |
21481b66 | 360 | if (py_attr_as_str(d->py_dec, "id", &(d->id)) != SRD_OK) |
451680f1 | 361 | goto err_out; |
b2c19614 | 362 | |
21481b66 | 363 | if (py_attr_as_str(d->py_dec, "name", &(d->name)) != SRD_OK) |
451680f1 | 364 | goto err_out; |
b2c19614 | 365 | |
21481b66 | 366 | if (py_attr_as_str(d->py_dec, "longname", &(d->longname)) != SRD_OK) |
451680f1 | 367 | goto err_out; |
b2c19614 | 368 | |
21481b66 | 369 | if (py_attr_as_str(d->py_dec, "desc", &(d->desc)) != SRD_OK) |
451680f1 | 370 | goto err_out; |
b2c19614 | 371 | |
21481b66 | 372 | if (py_attr_as_str(d->py_dec, "license", &(d->license)) != SRD_OK) |
451680f1 | 373 | goto err_out; |
b2c19614 | 374 | |
d75d8a7c | 375 | /* Convert annotation class attribute to GSList of char **. */ |
e97b6ef5 | 376 | d->annotations = NULL; |
451680f1 BV |
377 | if (PyObject_HasAttrString(d->py_dec, "annotations")) { |
378 | py_annlist = PyObject_GetAttrString(d->py_dec, "annotations"); | |
15969949 | 379 | if (!PyList_Check(py_annlist)) { |
c9bfccc6 | 380 | srd_err("Protocol decoder module %s annotations " |
9b37109d | 381 | "should be a list.", module_name); |
451680f1 | 382 | goto err_out; |
15969949 | 383 | } |
d75d8a7c BV |
384 | len = PyList_Size(py_annlist); |
385 | for (i = 0; i < len; i++) { | |
15969949 BV |
386 | py_ann = PyList_GetItem(py_annlist, i); |
387 | if (!PyList_Check(py_ann) || PyList_Size(py_ann) != 2) { | |
c9bfccc6 UH |
388 | srd_err("Protocol decoder module %s " |
389 | "annotation %d should be a list with " | |
9b37109d | 390 | "two elements.", module_name, i + 1); |
451680f1 | 391 | goto err_out; |
15969949 BV |
392 | } |
393 | ||
451680f1 | 394 | if (py_strlist_to_char(py_ann, &ann) != SRD_OK) { |
451680f1 BV |
395 | goto err_out; |
396 | } | |
e97b6ef5 | 397 | d->annotations = g_slist_append(d->annotations, ann); |
15969949 BV |
398 | } |
399 | } | |
400 | ||
d75d8a7c BV |
401 | /* Convert binary class to GSList of char *. */ |
402 | d->binary = NULL; | |
403 | if (PyObject_HasAttrString(d->py_dec, "binary")) { | |
404 | py_bin_classes = PyObject_GetAttrString(d->py_dec, "binary"); | |
405 | if (!PyTuple_Check(py_bin_classes)) { | |
406 | srd_err("Protocol decoder module %s binary classes " | |
407 | "should be a tuple.", module_name); | |
408 | goto err_out; | |
409 | } | |
410 | len = PyTuple_Size(py_bin_classes); | |
411 | for (i = 0; i < len; i++) { | |
412 | py_bin_class = PyTuple_GetItem(py_bin_classes, i); | |
413 | if (!PyUnicode_Check(py_bin_class)) { | |
414 | srd_err("Protocol decoder module %s binary " | |
415 | "class should be a string.", module_name); | |
416 | goto err_out; | |
417 | } | |
418 | ||
419 | if (py_str_as_str(py_bin_class, &bin) != SRD_OK) { | |
420 | goto err_out; | |
421 | } | |
422 | d->binary = g_slist_append(d->binary, bin); | |
423 | } | |
424 | } | |
425 | ||
9b37109d BV |
426 | /* Append it to the list of supported/loaded decoders. */ |
427 | pd_list = g_slist_append(pd_list, d); | |
428 | ||
451680f1 BV |
429 | ret = SRD_OK; |
430 | ||
431 | err_out: | |
432 | if (ret != SRD_OK) { | |
1d552cd3 | 433 | Py_XDECREF(py_method); |
451680f1 BV |
434 | Py_XDECREF(py_basedec); |
435 | Py_XDECREF(d->py_dec); | |
436 | Py_XDECREF(d->py_mod); | |
437 | g_free(d); | |
438 | } | |
b2c19614 | 439 | |
451680f1 BV |
440 | return ret; |
441 | } | |
442 | ||
582c8473 BV |
443 | /** |
444 | * Return a protocol decoder's docstring. | |
445 | * | |
446 | * @param dec The loaded protocol decoder. | |
447 | * | |
361fdcaa | 448 | * @return A newly allocated buffer containing the protocol decoder's |
abeeed8b | 449 | * documentation. The caller is responsible for free'ing the buffer. |
8c664ca2 UH |
450 | * |
451 | * @since 0.1.0 | |
582c8473 | 452 | */ |
b33b8aa5 | 453 | SRD_API char *srd_decoder_doc_get(const struct srd_decoder *dec) |
451680f1 | 454 | { |
e7edca07 | 455 | PyObject *py_str; |
451680f1 BV |
456 | char *doc; |
457 | ||
e195c025 BV |
458 | if (!srd_check_init()) |
459 | return NULL; | |
460 | ||
461 | if (!dec) | |
462 | return NULL; | |
463 | ||
e7edca07 BV |
464 | if (!PyObject_HasAttrString(dec->py_mod, "__doc__")) |
465 | return NULL; | |
466 | ||
467 | if (!(py_str = PyObject_GetAttrString(dec->py_mod, "__doc__"))) { | |
aafeeaea | 468 | srd_exception_catch(""); |
e7edca07 BV |
469 | return NULL; |
470 | } | |
471 | ||
451680f1 | 472 | doc = NULL; |
e7edca07 BV |
473 | if (py_str != Py_None) |
474 | py_str_as_str(py_str, &doc); | |
475 | Py_DecRef(py_str); | |
451680f1 BV |
476 | |
477 | return doc; | |
b2c19614 BV |
478 | } |
479 | ||
fa12a21e BV |
480 | static void free_probes(GSList *probelist) |
481 | { | |
482 | GSList *l; | |
483 | struct srd_probe *p; | |
484 | ||
485 | if (probelist == NULL) | |
486 | return; | |
487 | ||
488 | for (l = probelist; l; l = l->next) { | |
489 | p = l->data; | |
490 | g_free(p->id); | |
491 | g_free(p->name); | |
492 | g_free(p->desc); | |
493 | g_free(p); | |
494 | } | |
495 | g_slist_free(probelist); | |
fa12a21e BV |
496 | } |
497 | ||
b2c19614 | 498 | /** |
4cc0d9fe | 499 | * Unload the specified protocol decoder. |
451680f1 | 500 | * |
fa12a21e | 501 | * @param dec The struct srd_decoder to be unloaded. |
451680f1 BV |
502 | * |
503 | * @return SRD_OK upon success, a (negative) error code otherwise. | |
8c664ca2 UH |
504 | * |
505 | * @since 0.1.0 | |
b2c19614 | 506 | */ |
9d122fd5 | 507 | SRD_API int srd_decoder_unload(struct srd_decoder *dec) |
b2c19614 | 508 | { |
2f395bff | 509 | struct srd_decoder_option *o; |
32cfb920 | 510 | struct srd_session *sess; |
2f395bff BV |
511 | GSList *l; |
512 | ||
e195c025 BV |
513 | if (!srd_check_init()) |
514 | return SRD_ERR; | |
515 | ||
516 | if (!dec) | |
517 | return SRD_ERR_ARG; | |
518 | ||
8ad6e500 | 519 | srd_dbg("Unloading protocol decoder '%s'.", dec->name); |
fa12a21e | 520 | |
361fdcaa UH |
521 | /* |
522 | * Since any instances of this decoder need to be released as well, | |
fa12a21e BV |
523 | * but they could be anywhere in the stack, just free the entire |
524 | * stack. A frontend reloading a decoder thus has to restart all | |
361fdcaa UH |
525 | * instances, and rebuild the stack. |
526 | */ | |
32cfb920 BV |
527 | for (l = sessions; l; l = l->next) { |
528 | sess = l->data; | |
529 | srd_inst_free_all(sess, NULL); | |
530 | } | |
fa12a21e | 531 | |
2f395bff BV |
532 | for (l = dec->options; l; l = l->next) { |
533 | o = l->data; | |
534 | g_free(o->id); | |
535 | g_free(o->desc); | |
536 | g_variant_unref(o->def); | |
537 | g_free(o); | |
538 | } | |
539 | g_slist_free(dec->options); | |
540 | ||
fa12a21e | 541 | free_probes(dec->probes); |
dcdf4883 | 542 | free_probes(dec->opt_probes); |
b2c19614 BV |
543 | g_free(dec->id); |
544 | g_free(dec->name); | |
b231546d | 545 | g_free(dec->longname); |
b2c19614 | 546 | g_free(dec->desc); |
b231546d | 547 | g_free(dec->license); |
b2c19614 | 548 | |
fa12a21e | 549 | /* The module's Decoder class. */ |
451680f1 | 550 | Py_XDECREF(dec->py_dec); |
fa12a21e | 551 | /* The module itself. */ |
b2c19614 BV |
552 | Py_XDECREF(dec->py_mod); |
553 | ||
e592ac89 | 554 | g_free(dec); |
b2c19614 BV |
555 | |
556 | return SRD_OK; | |
557 | } | |
558 | ||
582c8473 | 559 | /** |
9b37109d | 560 | * Load all installed protocol decoders. |
582c8473 BV |
561 | * |
562 | * @return SRD_OK upon success, a (negative) error code otherwise. | |
8c664ca2 UH |
563 | * |
564 | * @since 0.1.0 | |
582c8473 | 565 | */ |
8ad6e500 | 566 | SRD_API int srd_decoder_load_all(void) |
b2c19614 | 567 | { |
eb2bbd66 BV |
568 | GDir *dir; |
569 | GError *error; | |
eb2bbd66 | 570 | const gchar *direntry; |
b2c19614 | 571 | |
e195c025 BV |
572 | if (!srd_check_init()) |
573 | return SRD_ERR; | |
574 | ||
eb2bbd66 | 575 | if (!(dir = g_dir_open(DECODERS_DIR, 0, &error))) { |
9b37109d | 576 | srd_err("Unable to open %s for reading.", DECODERS_DIR); |
b2c19614 BV |
577 | return SRD_ERR_DECODERS_DIR; |
578 | } | |
579 | ||
eb2bbd66 | 580 | while ((direntry = g_dir_read_name(dir)) != NULL) { |
9b37109d | 581 | /* The directory name is the module name (e.g. "i2c"). */ |
9d122fd5 | 582 | srd_decoder_load(direntry); |
b2c19614 | 583 | } |
eb2bbd66 | 584 | g_dir_close(dir); |
b2c19614 BV |
585 | |
586 | return SRD_OK; | |
587 | } | |
588 | ||
b2c19614 | 589 | /** |
582c8473 BV |
590 | * Unload all loaded protocol decoders. |
591 | * | |
592 | * @return SRD_OK upon success, a (negative) error code otherwise. | |
8c664ca2 UH |
593 | * |
594 | * @since 0.1.0 | |
b2c19614 | 595 | */ |
8ad6e500 | 596 | SRD_API int srd_decoder_unload_all(void) |
b2c19614 BV |
597 | { |
598 | GSList *l; | |
599 | struct srd_decoder *dec; | |
600 | ||
38ff5046 | 601 | for (l = pd_list; l; l = l->next) { |
b2c19614 | 602 | dec = l->data; |
9d122fd5 | 603 | srd_decoder_unload(dec); |
b2c19614 BV |
604 | } |
605 | ||
606 | return SRD_OK; | |
607 | } | |
4895418c UH |
608 | |
609 | /** @} */ |