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