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