blob: becd78516199ec88fe2581ff4a4eb99b2c2f027f [file] [log] [blame]
Bert Vermeulenb2c19612011-12-04 10:33:02 +01001/*
Uwe Hermann50bd5d22013-04-23 22:27:20 +02002 * This file is part of the libsigrokdecode project.
Bert Vermeulenb2c19612011-12-04 10:33:02 +01003 *
4 * Copyright (C) 2010 Uwe Hermann <uwe@hermann-uwe.de>
Bert Vermeulen4fadb122012-01-22 03:29:22 +01005 * Copyright (C) 2012 Bert Vermeulen <bert@biot.com>
Bert Vermeulenb2c19612011-12-04 10:33:02 +01006 *
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"
Uwe Hermannc1f86f02013-05-03 14:49:21 +020022#include "libsigrokdecode.h" /* First, so we avoid a _POSIX_C_SOURCE warning. */
23#include "libsigrokdecode-internal.h"
Bert Vermeuleneb2bbd62012-01-19 17:12:02 +010024#include <glib.h>
Bert Vermeulenb2c19612011-12-04 10:33:02 +010025
Uwe Hermann54fdeee2013-02-09 22:10:57 +010026/**
27 * @file
28 *
29 * Listing, loading, unloading, and handling protocol decoders.
30 */
31
Uwe Hermann48954182013-02-09 22:25:15 +010032/**
33 * @defgroup grp_decoder Protocol decoders
34 *
35 * Handling protocol decoders.
36 *
37 * @{
38 */
39
Uwe Hermann57790bc2013-02-09 21:44:11 +010040/** @cond PRIVATE */
41
Bert Vermeulenb2c19612011-12-04 10:33:02 +010042/* The list of protocol decoders. */
Uwe Hermann55c3c5f2012-02-09 19:11:53 +010043SRD_PRIV GSList *pd_list = NULL;
Bert Vermeulenb2c19612011-12-04 10:33:02 +010044
Bert Vermeulenea81b492013-12-11 18:31:56 +010045/* srd.c */
46extern GSList *searchpaths;
47
48/* session.c */
Bert Vermeulen32cfb922013-09-12 23:54:43 +020049extern GSList *sessions;
50
Uwe Hermannc9bfccc2012-02-08 22:39:30 +010051/* module_sigrokdecode.c */
Uwe Hermann55c3c5f2012-02-09 19:11:53 +010052extern SRD_PRIV PyObject *mod_sigrokdecode;
Bert Vermeulen451680f2012-01-15 03:58:27 +010053
Uwe Hermann57790bc2013-02-09 21:44:11 +010054/** @endcond */
55
Bert Vermeulenb2c19612011-12-04 10:33:02 +010056/**
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.
Uwe Hermann8c664ca2013-05-03 14:45:49 +020062 *
63 * @since 0.1.0 (but the API changed in 0.2.0)
Bert Vermeulenb2c19612011-12-04 10:33:02 +010064 */
Uwe Hermann38ff5042013-02-02 12:18:29 +010065SRD_API const GSList *srd_decoder_list(void)
Bert Vermeulenb2c19612011-12-04 10:33:02 +010066{
Bert Vermeulene5080882011-12-07 09:56:49 +010067 return pd_list;
Bert Vermeulenb2c19612011-12-04 10:33:02 +010068}
69
Bert Vermeulenb2c19612011-12-04 10:33:02 +010070/**
71 * Get the decoder with the specified ID.
72 *
73 * @param id The ID string of the decoder to return.
Bert Vermeulen582c8472012-02-12 14:55:20 +010074 *
Bert Vermeulenb2c19612011-12-04 10:33:02 +010075 * @return The decoder with the specified ID, or NULL if not found.
Uwe Hermann8c664ca2013-05-03 14:45:49 +020076 *
77 * @since 0.1.0
Bert Vermeulenb2c19612011-12-04 10:33:02 +010078 */
Bert Vermeulen9d122fd2012-02-14 03:43:28 +010079SRD_API struct srd_decoder *srd_decoder_get_by_id(const char *id)
Bert Vermeulenb2c19612011-12-04 10:33:02 +010080{
81 GSList *l;
82 struct srd_decoder *dec;
83
Uwe Hermann38ff5042013-02-02 12:18:29 +010084 for (l = pd_list; l; l = l->next) {
Bert Vermeulenb2c19612011-12-04 10:33:02 +010085 dec = l->data;
86 if (!strcmp(dec->id, id))
87 return dec;
88 }
89
90 return NULL;
91}
92
Uwe Hermann5a2c4dc2012-03-21 19:28:43 +010093static int get_probes(const struct srd_decoder *d, const char *attr,
Bert Vermeulen22c9bc22013-11-18 09:41:14 +010094 GSList **pl)
Bert Vermeulenf38ec282012-01-20 22:25:42 +010095{
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
Bert Vermeulenf38ec282012-01-20 22:25:42 +0100104 py_probelist = PyObject_GetAttrString(d->py_dec, attr);
105 if (!PyList_Check(py_probelist)) {
Bert Vermeulen22c9bc22013-11-18 09:41:14 +0100106 srd_err("Protocol decoder %s %s attribute is not a list.",
107 d->name, attr);
108 return SRD_ERR_PYTHON;
Bert Vermeulenf38ec282012-01-20 22:25:42 +0100109 }
110
Bert Vermeulen22c9bc22013-11-18 09:41:14 +0100111 if ((num_probes = PyList_Size(py_probelist)) == 0)
Bert Vermeulenf38ec282012-01-20 22:25:42 +0100112 /* Empty probelist. */
113 return SRD_OK;
114
Bert Vermeulen22c9bc22013-11-18 09:41:14 +0100115 ret = SRD_OK;
Bert Vermeulenf38ec282012-01-20 22:25:42 +0100116 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 "
Uwe Hermannc9bfccc2012-02-08 22:39:30 +0100120 "a list with dict elements.", d->name, attr);
Bert Vermeulen22c9bc22013-11-18 09:41:14 +0100121 ret = SRD_ERR_PYTHON;
122 break;
Bert Vermeulenf38ec282012-01-20 22:25:42 +0100123 }
124
125 if (!(p = g_try_malloc(sizeof(struct srd_probe)))) {
Uwe Hermanna61ece22012-02-10 00:06:58 +0100126 srd_err("Failed to g_malloc() struct srd_probe.");
Bert Vermeulenf38ec282012-01-20 22:25:42 +0100127 ret = SRD_ERR_MALLOC;
Bert Vermeulen22c9bc22013-11-18 09:41:14 +0100128 break;
Bert Vermeulenf38ec282012-01-20 22:25:42 +0100129 }
130
Bert Vermeulen22c9bc22013-11-18 09:41:14 +0100131 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 }
Bert Vermeulenf38ec282012-01-20 22:25:42 +0100143 p->order = i;
144
145 *pl = g_slist_append(*pl, p);
146 }
Bert Vermeulenf38ec282012-01-20 22:25:42 +0100147
Bert Vermeulenf38ec282012-01-20 22:25:42 +0100148 Py_DecRef(py_probelist);
149
150 return ret;
151}
152
Bert Vermeulen2f395bf2013-03-19 01:47:53 +0100153static 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);
Bert Vermeulene592ac82013-05-05 17:20:13 +0200227 g_free(key);
Bert Vermeulenca3fc3c2013-05-08 22:25:37 +0200228 key = NULL;
Bert Vermeulen2f395bf2013-03-19 01:47:53 +0100229 }
230 Py_DecRef(py_keys);
231 Py_DecRef(py_values);
232
233 ret = SRD_OK;
234
235err_out:
236 Py_XDECREF(py_opts);
237 g_free(key);
238
239 return ret;
240}
241
Bert Vermeulenb2c19612011-12-04 10:33:02 +0100242/**
Uwe Hermann64c29e22012-01-15 15:36:01 +0100243 * Load a protocol decoder module into the embedded Python interpreter.
Bert Vermeulenb2c19612011-12-04 10:33:02 +0100244 *
Uwe Hermann38ff5042013-02-02 12:18:29 +0100245 * @param module_name The module name to be loaded.
Bert Vermeulenb2c19612011-12-04 10:33:02 +0100246 *
247 * @return SRD_OK upon success, a (negative) error code otherwise.
Uwe Hermann8c664ca2013-05-03 14:45:49 +0200248 *
249 * @since 0.1.0
Bert Vermeulenb2c19612011-12-04 10:33:02 +0100250 */
Bert Vermeulen9d122fd2012-02-14 03:43:28 +0100251SRD_API int srd_decoder_load(const char *module_name)
Bert Vermeulenb2c19612011-12-04 10:33:02 +0100252{
Uwe Hermann1ce46812014-01-31 16:59:22 +0100253 PyObject *py_basedec, *py_method, *py_attr, *py_annlist, *py_ann;
254 PyObject *py_bin_classes, *py_bin_class, *py_ann_rows, *py_ann_row;
255 PyObject *py_ann_classes, *py_long;
Bert Vermeulenb2c19612011-12-04 10:33:02 +0100256 struct srd_decoder *d;
Uwe Hermann1ce46812014-01-31 16:59:22 +0100257 int ret, i, j;
258 char **ann, **bin, *ann_row_id, *ann_row_desc;
Uwe Hermann38ff5042013-02-02 12:18:29 +0100259 struct srd_probe *p;
Uwe Hermann1ce46812014-01-31 16:59:22 +0100260 GSList *l, *ann_classes;
261 struct srd_decoder_annotation_row *ann_row;
Bert Vermeulenb2c19612011-12-04 10:33:02 +0100262
Bert Vermeulene195c022013-11-18 01:21:16 +0100263 if (!srd_check_init())
264 return SRD_ERR;
265
Bert Vermeulen8a014682013-10-17 23:52:15 +0200266 if (!module_name)
267 return SRD_ERR_ARG;
268
Bert Vermeulenaac68132013-11-18 10:44:28 +0100269 if (PyDict_GetItemString(PyImport_GetModuleDict(), module_name)) {
270 /* Module was already imported. */
271 return SRD_OK;
272 }
273
Uwe Hermann8ad6e502012-03-16 15:25:53 +0100274 srd_dbg("Loading protocol decoder '%s'.", module_name);
Bert Vermeulend906d3f2012-01-22 02:51:49 +0100275
Bert Vermeulen11ea8ae2012-01-20 22:23:27 +0100276 py_basedec = py_method = py_attr = NULL;
Uwe Hermann64c29e22012-01-15 15:36:01 +0100277
Bert Vermeulen451680f2012-01-15 03:58:27 +0100278 if (!(d = g_try_malloc0(sizeof(struct srd_decoder)))) {
Uwe Hermanna61ece22012-02-10 00:06:58 +0100279 srd_dbg("Failed to g_malloc() struct srd_decoder.");
Bert Vermeulen451680f2012-01-15 03:58:27 +0100280 ret = SRD_ERR_MALLOC;
281 goto err_out;
282 }
283
Bert Vermeulen21481b62012-01-19 09:59:00 +0100284 ret = SRD_ERR_PYTHON;
285
Bert Vermeulen451680f2012-01-15 03:58:27 +0100286 /* Import the Python module. */
Bert Vermeulen9b371092012-02-14 03:28:53 +0100287 if (!(d->py_mod = PyImport_ImportModule(module_name))) {
Uwe Hermannaafeeae2012-03-25 15:08:16 +0200288 srd_exception_catch("Import of '%s' failed.", module_name);
Bert Vermeulen451680f2012-01-15 03:58:27 +0100289 goto err_out;
Bert Vermeulenb2c19612011-12-04 10:33:02 +0100290 }
291
292 /* Get the 'Decoder' class as Python object. */
Bert Vermeulen451680f2012-01-15 03:58:27 +0100293 if (!(d->py_dec = PyObject_GetAttrString(d->py_mod, "Decoder"))) {
294 /* This generated an AttributeError exception. */
295 PyErr_Clear();
Uwe Hermann361fdca2012-03-15 22:00:24 +0100296 srd_err("Decoder class not found in protocol decoder %s.",
297 module_name);
Bert Vermeulen451680f2012-01-15 03:58:27 +0100298 goto err_out;
Bert Vermeulenb2c19612011-12-04 10:33:02 +0100299 }
300
Bert Vermeulen451680f2012-01-15 03:58:27 +0100301 if (!(py_basedec = PyObject_GetAttrString(mod_sigrokdecode, "Decoder"))) {
Uwe Hermann7a1712c2012-01-26 01:15:10 +0100302 srd_dbg("sigrokdecode module not loaded.");
Bert Vermeulen451680f2012-01-15 03:58:27 +0100303 goto err_out;
304 }
Bert Vermeulenb2c19612011-12-04 10:33:02 +0100305
Bert Vermeulen451680f2012-01-15 03:58:27 +0100306 if (!PyObject_IsSubclass(d->py_dec, py_basedec)) {
307 srd_err("Decoder class in protocol decoder module %s is not "
Bert Vermeulen9b371092012-02-14 03:28:53 +0100308 "a subclass of sigrokdecode.Decoder.", module_name);
Bert Vermeulen451680f2012-01-15 03:58:27 +0100309 goto err_out;
310 }
Bert Vermeulenad022d92012-01-30 01:22:27 +0100311 Py_CLEAR(py_basedec);
Bert Vermeulenb2c19612011-12-04 10:33:02 +0100312
Bert Vermeulen1d552cd2012-01-19 15:05:38 +0100313 /* Check for a proper start() method. */
314 if (!PyObject_HasAttrString(d->py_dec, "start")) {
315 srd_err("Protocol decoder %s has no start() method Decoder "
Bert Vermeulen9b371092012-02-14 03:28:53 +0100316 "class.", module_name);
Bert Vermeulen1d552cd2012-01-19 15:05:38 +0100317 goto err_out;
318 }
319 py_method = PyObject_GetAttrString(d->py_dec, "start");
320 if (!PyFunction_Check(py_method)) {
Bert Vermeulend906d3f2012-01-22 02:51:49 +0100321 srd_err("Protocol decoder %s Decoder class attribute 'start' "
Bert Vermeulen9b371092012-02-14 03:28:53 +0100322 "is not a method.", module_name);
Bert Vermeulen1d552cd2012-01-19 15:05:38 +0100323 goto err_out;
324 }
Bert Vermeulenad022d92012-01-30 01:22:27 +0100325 Py_CLEAR(py_method);
Bert Vermeulen1d552cd2012-01-19 15:05:38 +0100326
327 /* Check for a proper decode() method. */
328 if (!PyObject_HasAttrString(d->py_dec, "decode")) {
329 srd_err("Protocol decoder %s has no decode() method Decoder "
Bert Vermeulen9b371092012-02-14 03:28:53 +0100330 "class.", module_name);
Bert Vermeulen1d552cd2012-01-19 15:05:38 +0100331 goto err_out;
332 }
333 py_method = PyObject_GetAttrString(d->py_dec, "decode");
334 if (!PyFunction_Check(py_method)) {
335 srd_err("Protocol decoder %s Decoder class attribute 'decode' "
Bert Vermeulen9b371092012-02-14 03:28:53 +0100336 "is not a method.", module_name);
Bert Vermeulen1d552cd2012-01-19 15:05:38 +0100337 goto err_out;
338 }
Bert Vermeulenad022d92012-01-30 01:22:27 +0100339 Py_CLEAR(py_method);
Bert Vermeulen1d552cd2012-01-19 15:05:38 +0100340
Bert Vermeulen2f395bf2013-03-19 01:47:53 +0100341 if (get_options(d) != SRD_OK)
342 goto err_out;
Bert Vermeulen11ea8ae2012-01-20 22:23:27 +0100343
Bert Vermeulenf38ec282012-01-20 22:25:42 +0100344 /* Check and import required probes. */
345 if (get_probes(d, "probes", &d->probes) != SRD_OK)
346 goto err_out;
347
348 /* Check and import optional probes. */
Bert Vermeulendcdf4882012-02-01 00:07:55 +0100349 if (get_probes(d, "optional_probes", &d->opt_probes) != SRD_OK)
Bert Vermeulenf38ec282012-01-20 22:25:42 +0100350 goto err_out;
351
Uwe Hermann38ff5042013-02-02 12:18:29 +0100352 /*
353 * Fix order numbers for the optional probes.
354 *
355 * Example:
356 * Required probes: r1, r2, r3. Optional: o1, o2, o3, o4.
357 * 'order' fields in the d->probes list = 0, 1, 2.
358 * 'order' fields in the d->opt_probes list = 3, 4, 5, 6.
359 */
360 for (l = d->opt_probes; l; l = l->next) {
361 p = l->data;
362 p->order += g_slist_length(d->probes);
363 }
364
Bert Vermeulenf38ec282012-01-20 22:25:42 +0100365 /* Store required fields in newly allocated strings. */
Bert Vermeulen21481b62012-01-19 09:59:00 +0100366 if (py_attr_as_str(d->py_dec, "id", &(d->id)) != SRD_OK)
Bert Vermeulen451680f2012-01-15 03:58:27 +0100367 goto err_out;
Bert Vermeulenb2c19612011-12-04 10:33:02 +0100368
Bert Vermeulen21481b62012-01-19 09:59:00 +0100369 if (py_attr_as_str(d->py_dec, "name", &(d->name)) != SRD_OK)
Bert Vermeulen451680f2012-01-15 03:58:27 +0100370 goto err_out;
Bert Vermeulenb2c19612011-12-04 10:33:02 +0100371
Bert Vermeulen21481b62012-01-19 09:59:00 +0100372 if (py_attr_as_str(d->py_dec, "longname", &(d->longname)) != SRD_OK)
Bert Vermeulen451680f2012-01-15 03:58:27 +0100373 goto err_out;
Bert Vermeulenb2c19612011-12-04 10:33:02 +0100374
Bert Vermeulen21481b62012-01-19 09:59:00 +0100375 if (py_attr_as_str(d->py_dec, "desc", &(d->desc)) != SRD_OK)
Bert Vermeulen451680f2012-01-15 03:58:27 +0100376 goto err_out;
Bert Vermeulenb2c19612011-12-04 10:33:02 +0100377
Bert Vermeulen21481b62012-01-19 09:59:00 +0100378 if (py_attr_as_str(d->py_dec, "license", &(d->license)) != SRD_OK)
Bert Vermeulen451680f2012-01-15 03:58:27 +0100379 goto err_out;
Bert Vermeulenb2c19612011-12-04 10:33:02 +0100380
Bert Vermeulend75d8a72013-11-10 12:42:11 +0100381 /* Convert annotation class attribute to GSList of char **. */
Uwe Hermanne97b6ef2012-01-10 21:05:09 +0100382 d->annotations = NULL;
Bert Vermeulen451680f2012-01-15 03:58:27 +0100383 if (PyObject_HasAttrString(d->py_dec, "annotations")) {
384 py_annlist = PyObject_GetAttrString(d->py_dec, "annotations");
Bert Vermeulen15969942012-01-07 02:50:14 +0100385 if (!PyList_Check(py_annlist)) {
Bert Vermeulen1f2275d2013-12-12 00:56:12 +0100386 srd_err("Protocol decoder %s annotations should "
387 "be a list.", module_name);
Bert Vermeulen451680f2012-01-15 03:58:27 +0100388 goto err_out;
Bert Vermeulen15969942012-01-07 02:50:14 +0100389 }
Uwe Hermann1ce46812014-01-31 16:59:22 +0100390 for (i = 0; i < PyList_Size(py_annlist); i++) {
Bert Vermeulen15969942012-01-07 02:50:14 +0100391 py_ann = PyList_GetItem(py_annlist, i);
392 if (!PyList_Check(py_ann) || PyList_Size(py_ann) != 2) {
Bert Vermeulen1f2275d2013-12-12 00:56:12 +0100393 srd_err("Protocol decoder %s annotation %d should "
394 "be a list with two elements.", module_name, i + 1);
Bert Vermeulen451680f2012-01-15 03:58:27 +0100395 goto err_out;
Bert Vermeulen15969942012-01-07 02:50:14 +0100396 }
397
Bert Vermeulen62a2b152013-12-11 23:41:02 +0100398 if (py_strseq_to_char(py_ann, &ann) != SRD_OK) {
Bert Vermeulen451680f2012-01-15 03:58:27 +0100399 goto err_out;
400 }
Uwe Hermanne97b6ef2012-01-10 21:05:09 +0100401 d->annotations = g_slist_append(d->annotations, ann);
Bert Vermeulen15969942012-01-07 02:50:14 +0100402 }
403 }
404
Uwe Hermann1ce46812014-01-31 16:59:22 +0100405 /* Convert annotation_rows to GSList of 'struct srd_decoder_annotation_row'. */
406 d->annotation_rows = NULL;
407 if (PyObject_HasAttrString(d->py_dec, "annotation_rows")) {
408 py_ann_rows = PyObject_GetAttrString(d->py_dec, "annotation_rows");
409 if (!PyTuple_Check(py_ann_rows)) {
410 srd_err("Protocol decoder %s annotation row list "
411 "must be a tuple.", module_name);
412 goto err_out;
413 }
414 for (i = 0; i < PyTuple_Size(py_ann_rows); i++) {
415 py_ann_row = PyTuple_GetItem(py_ann_rows, i);
416 if (!PyTuple_Check(py_ann_row)) {
417 srd_err("Protocol decoder %s annotation rows "
418 "must be tuples.", module_name);
419 goto err_out;
420 }
421 if (PyTuple_Size(py_ann_row) != 3
422 || !PyUnicode_Check(PyTuple_GetItem(py_ann_row, 0))
423 || !PyUnicode_Check(PyTuple_GetItem(py_ann_row, 1))
424 || !PyTuple_Check(PyTuple_GetItem(py_ann_row, 2))) {
425 srd_err("Protocol decoder %s annotation rows "
426 "must contain tuples containing two "
427 "strings and a tuple.", module_name);
428 goto err_out;
429 }
430
431 if (py_str_as_str(PyTuple_GetItem(py_ann_row, 0), &ann_row_id) != SRD_OK)
432 goto err_out;
433
434 if (py_str_as_str(PyTuple_GetItem(py_ann_row, 1), &ann_row_desc) != SRD_OK)
435 goto err_out;
436
437 py_ann_classes = PyTuple_GetItem(py_ann_row, 2);
438 ann_classes = NULL;
439 for (j = 0; j < PyTuple_Size(py_ann_classes); j++) {
440 py_long = PyTuple_GetItem(py_ann_classes, j);
441 if (!PyLong_Check(py_long)) {
442 srd_err("Protocol decoder %s annotation row class "
443 "list must only contain numbers.", module_name);
444 goto err_out;
445 }
446 ann_classes = g_slist_append(ann_classes,
447 GINT_TO_POINTER(PyLong_AsLong(py_long)));
448 }
449
450 ann_row = g_malloc0(sizeof(struct srd_decoder_annotation_row));
451 ann_row->id = ann_row_id;
452 ann_row->desc = ann_row_desc;
453 ann_row->ann_classes = ann_classes;
454 d->annotation_rows = g_slist_append(d->annotation_rows, ann_row);
455 }
456 }
457
Bert Vermeulend75d8a72013-11-10 12:42:11 +0100458 /* Convert binary class to GSList of char *. */
459 d->binary = NULL;
460 if (PyObject_HasAttrString(d->py_dec, "binary")) {
461 py_bin_classes = PyObject_GetAttrString(d->py_dec, "binary");
462 if (!PyTuple_Check(py_bin_classes)) {
Bert Vermeulen1f2275d2013-12-12 00:56:12 +0100463 srd_err("Protocol decoder %s binary classes should "
464 "be a tuple.", module_name);
Bert Vermeulend75d8a72013-11-10 12:42:11 +0100465 goto err_out;
466 }
Uwe Hermann1ce46812014-01-31 16:59:22 +0100467 for (i = 0; i < PyTuple_Size(py_bin_classes); i++) {
Bert Vermeulend75d8a72013-11-10 12:42:11 +0100468 py_bin_class = PyTuple_GetItem(py_bin_classes, i);
Bert Vermeulen1f2275d2013-12-12 00:56:12 +0100469 if (!PyTuple_Check(py_bin_class)) {
470 srd_err("Protocol decoder %s binary classes "
471 "should consist of tuples.", module_name);
472 goto err_out;
473 }
474 if (PyTuple_Size(py_bin_class) != 2
475 || !PyUnicode_Check(PyTuple_GetItem(py_bin_class, 0))
476 || !PyUnicode_Check(PyTuple_GetItem(py_bin_class, 1))) {
477 srd_err("Protocol decoder %s binary classes should "
478 "contain tuples with two strings.", module_name);
Bert Vermeulend75d8a72013-11-10 12:42:11 +0100479 goto err_out;
480 }
481
Bert Vermeulen1f2275d2013-12-12 00:56:12 +0100482 if (py_strseq_to_char(py_bin_class, &bin) != SRD_OK) {
Bert Vermeulend75d8a72013-11-10 12:42:11 +0100483 goto err_out;
484 }
485 d->binary = g_slist_append(d->binary, bin);
486 }
487 }
488
Bert Vermeulen9b371092012-02-14 03:28:53 +0100489 /* Append it to the list of supported/loaded decoders. */
490 pd_list = g_slist_append(pd_list, d);
491
Bert Vermeulen451680f2012-01-15 03:58:27 +0100492 ret = SRD_OK;
Bert Vermeulenb2c19612011-12-04 10:33:02 +0100493
Bert Vermeulen451680f2012-01-15 03:58:27 +0100494err_out:
495 if (ret != SRD_OK) {
Bert Vermeulen1d552cd2012-01-19 15:05:38 +0100496 Py_XDECREF(py_method);
Bert Vermeulen451680f2012-01-15 03:58:27 +0100497 Py_XDECREF(py_basedec);
498 Py_XDECREF(d->py_dec);
499 Py_XDECREF(d->py_mod);
500 g_free(d);
501 }
502
503 return ret;
504}
505
Bert Vermeulen582c8472012-02-12 14:55:20 +0100506/**
507 * Return a protocol decoder's docstring.
508 *
509 * @param dec The loaded protocol decoder.
510 *
Uwe Hermann361fdca2012-03-15 22:00:24 +0100511 * @return A newly allocated buffer containing the protocol decoder's
Uwe Hermannabeeed82012-03-16 15:12:54 +0100512 * documentation. The caller is responsible for free'ing the buffer.
Uwe Hermann8c664ca2013-05-03 14:45:49 +0200513 *
514 * @since 0.1.0
Bert Vermeulen582c8472012-02-12 14:55:20 +0100515 */
Uwe Hermannb33b8aa2012-03-25 14:49:11 +0200516SRD_API char *srd_decoder_doc_get(const struct srd_decoder *dec)
Bert Vermeulen451680f2012-01-15 03:58:27 +0100517{
Bert Vermeulene7edca02012-01-15 04:24:15 +0100518 PyObject *py_str;
Bert Vermeulen451680f2012-01-15 03:58:27 +0100519 char *doc;
520
Bert Vermeulene195c022013-11-18 01:21:16 +0100521 if (!srd_check_init())
522 return NULL;
523
524 if (!dec)
525 return NULL;
526
Bert Vermeulene7edca02012-01-15 04:24:15 +0100527 if (!PyObject_HasAttrString(dec->py_mod, "__doc__"))
528 return NULL;
529
530 if (!(py_str = PyObject_GetAttrString(dec->py_mod, "__doc__"))) {
Uwe Hermannaafeeae2012-03-25 15:08:16 +0200531 srd_exception_catch("");
Bert Vermeulene7edca02012-01-15 04:24:15 +0100532 return NULL;
533 }
534
Bert Vermeulen451680f2012-01-15 03:58:27 +0100535 doc = NULL;
Bert Vermeulene7edca02012-01-15 04:24:15 +0100536 if (py_str != Py_None)
537 py_str_as_str(py_str, &doc);
538 Py_DecRef(py_str);
Bert Vermeulen451680f2012-01-15 03:58:27 +0100539
540 return doc;
Bert Vermeulenb2c19612011-12-04 10:33:02 +0100541}
542
Bert Vermeulenfa12a212012-01-31 23:48:10 +0100543static void free_probes(GSList *probelist)
544{
545 GSList *l;
546 struct srd_probe *p;
547
548 if (probelist == NULL)
549 return;
550
551 for (l = probelist; l; l = l->next) {
552 p = l->data;
553 g_free(p->id);
554 g_free(p->name);
555 g_free(p->desc);
556 g_free(p);
557 }
558 g_slist_free(probelist);
Bert Vermeulenfa12a212012-01-31 23:48:10 +0100559}
560
Bert Vermeulenb2c19612011-12-04 10:33:02 +0100561/**
Uwe Hermann4cc0d9f2013-05-03 21:21:30 +0200562 * Unload the specified protocol decoder.
Bert Vermeulen451680f2012-01-15 03:58:27 +0100563 *
Bert Vermeulenfa12a212012-01-31 23:48:10 +0100564 * @param dec The struct srd_decoder to be unloaded.
Bert Vermeulen451680f2012-01-15 03:58:27 +0100565 *
566 * @return SRD_OK upon success, a (negative) error code otherwise.
Uwe Hermann8c664ca2013-05-03 14:45:49 +0200567 *
568 * @since 0.1.0
Bert Vermeulenb2c19612011-12-04 10:33:02 +0100569 */
Bert Vermeulen9d122fd2012-02-14 03:43:28 +0100570SRD_API int srd_decoder_unload(struct srd_decoder *dec)
Bert Vermeulenb2c19612011-12-04 10:33:02 +0100571{
Bert Vermeulen2f395bf2013-03-19 01:47:53 +0100572 struct srd_decoder_option *o;
Bert Vermeulen32cfb922013-09-12 23:54:43 +0200573 struct srd_session *sess;
Bert Vermeulen2f395bf2013-03-19 01:47:53 +0100574 GSList *l;
575
Bert Vermeulene195c022013-11-18 01:21:16 +0100576 if (!srd_check_init())
577 return SRD_ERR;
578
579 if (!dec)
580 return SRD_ERR_ARG;
581
Uwe Hermann8ad6e502012-03-16 15:25:53 +0100582 srd_dbg("Unloading protocol decoder '%s'.", dec->name);
Bert Vermeulenfa12a212012-01-31 23:48:10 +0100583
Uwe Hermann361fdca2012-03-15 22:00:24 +0100584 /*
585 * Since any instances of this decoder need to be released as well,
Bert Vermeulenfa12a212012-01-31 23:48:10 +0100586 * but they could be anywhere in the stack, just free the entire
587 * stack. A frontend reloading a decoder thus has to restart all
Uwe Hermann361fdca2012-03-15 22:00:24 +0100588 * instances, and rebuild the stack.
589 */
Bert Vermeulen32cfb922013-09-12 23:54:43 +0200590 for (l = sessions; l; l = l->next) {
591 sess = l->data;
592 srd_inst_free_all(sess, NULL);
593 }
Bert Vermeulenfa12a212012-01-31 23:48:10 +0100594
Bert Vermeulen2f395bf2013-03-19 01:47:53 +0100595 for (l = dec->options; l; l = l->next) {
596 o = l->data;
597 g_free(o->id);
598 g_free(o->desc);
599 g_variant_unref(o->def);
600 g_free(o);
601 }
602 g_slist_free(dec->options);
603
Bert Vermeulenfa12a212012-01-31 23:48:10 +0100604 free_probes(dec->probes);
Bert Vermeulendcdf4882012-02-01 00:07:55 +0100605 free_probes(dec->opt_probes);
Bert Vermeulenb2c19612011-12-04 10:33:02 +0100606 g_free(dec->id);
607 g_free(dec->name);
Bert Vermeulenb2315462012-01-09 00:13:03 +0100608 g_free(dec->longname);
Bert Vermeulenb2c19612011-12-04 10:33:02 +0100609 g_free(dec->desc);
Bert Vermeulenb2315462012-01-09 00:13:03 +0100610 g_free(dec->license);
Bert Vermeulenb2c19612011-12-04 10:33:02 +0100611
Bert Vermeulenfa12a212012-01-31 23:48:10 +0100612 /* The module's Decoder class. */
Bert Vermeulen451680f2012-01-15 03:58:27 +0100613 Py_XDECREF(dec->py_dec);
Bert Vermeulenfa12a212012-01-31 23:48:10 +0100614 /* The module itself. */
Bert Vermeulenb2c19612011-12-04 10:33:02 +0100615 Py_XDECREF(dec->py_mod);
616
Bert Vermeulene592ac82013-05-05 17:20:13 +0200617 g_free(dec);
Bert Vermeulenb2c19612011-12-04 10:33:02 +0100618
619 return SRD_OK;
620}
621
Bert Vermeulenea81b492013-12-11 18:31:56 +0100622static void srd_decoder_load_all_path(char *path)
623{
624 GDir *dir;
625 const gchar *direntry;
626
627 if (!(dir = g_dir_open(path, 0, NULL)))
628 /* Not really fatal */
629 return;
630
631 /* This ignores errors returned by srd_decoder_load(). That
632 * function will have logged the cause, but in any case we
633 * want to continue anyway. */
634 while ((direntry = g_dir_read_name(dir)) != NULL) {
635 /* The directory name is the module name (e.g. "i2c"). */
636 srd_decoder_load(direntry);
637 }
638 g_dir_close(dir);
639
640}
641
Bert Vermeulen582c8472012-02-12 14:55:20 +0100642/**
Bert Vermeulen9b371092012-02-14 03:28:53 +0100643 * Load all installed protocol decoders.
Bert Vermeulen582c8472012-02-12 14:55:20 +0100644 *
645 * @return SRD_OK upon success, a (negative) error code otherwise.
Uwe Hermann8c664ca2013-05-03 14:45:49 +0200646 *
647 * @since 0.1.0
Bert Vermeulen582c8472012-02-12 14:55:20 +0100648 */
Uwe Hermann8ad6e502012-03-16 15:25:53 +0100649SRD_API int srd_decoder_load_all(void)
Bert Vermeulenb2c19612011-12-04 10:33:02 +0100650{
Bert Vermeulenea81b492013-12-11 18:31:56 +0100651 GSList *l;
Bert Vermeulenb2c19612011-12-04 10:33:02 +0100652
Bert Vermeulene195c022013-11-18 01:21:16 +0100653 if (!srd_check_init())
654 return SRD_ERR;
655
Bert Vermeulenea81b492013-12-11 18:31:56 +0100656 for (l = searchpaths; l; l = l->next)
657 srd_decoder_load_all_path(l->data);
Bert Vermeulenb2c19612011-12-04 10:33:02 +0100658
659 return SRD_OK;
660}
661
Bert Vermeulenb2c19612011-12-04 10:33:02 +0100662/**
Bert Vermeulen582c8472012-02-12 14:55:20 +0100663 * Unload all loaded protocol decoders.
664 *
665 * @return SRD_OK upon success, a (negative) error code otherwise.
Uwe Hermann8c664ca2013-05-03 14:45:49 +0200666 *
667 * @since 0.1.0
Bert Vermeulenb2c19612011-12-04 10:33:02 +0100668 */
Uwe Hermann8ad6e502012-03-16 15:25:53 +0100669SRD_API int srd_decoder_unload_all(void)
Bert Vermeulenb2c19612011-12-04 10:33:02 +0100670{
671 GSList *l;
672 struct srd_decoder *dec;
673
Uwe Hermann38ff5042013-02-02 12:18:29 +0100674 for (l = pd_list; l; l = l->next) {
Bert Vermeulenb2c19612011-12-04 10:33:02 +0100675 dec = l->data;
Bert Vermeulen9d122fd2012-02-14 03:43:28 +0100676 srd_decoder_unload(dec);
Bert Vermeulenb2c19612011-12-04 10:33:02 +0100677 }
Bert Vermeulen820bf442013-12-11 16:30:25 +0100678 g_slist_free(pd_list);
679 pd_list = NULL;
Bert Vermeulenb2c19612011-12-04 10:33:02 +0100680
681 return SRD_OK;
682}
Uwe Hermann48954182013-02-09 22:25:15 +0100683
684/** @} */