blob: ac0d73fc1be80a23cbbbe14c6183376fef687500 [file] [log] [blame]
Bert Vermeulenb2c19612011-12-04 10:33:02 +01001/*
2 * This file is part of the sigrok project.
3 *
4 * Copyright (C) 2010 Uwe Hermann <uwe@hermann-uwe.de>
Bert Vermeulenbc5f5a42012-01-05 03:31:36 +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
Uwe Hermann73191412012-01-03 19:56:01 +010021#include "sigrokdecode.h" /* First, so we avoid a _POSIX_C_SOURCE warning. */
Bert Vermeulen7ce77752012-01-10 00:25:16 +010022#include "sigrokdecode-internal.h"
23#include "config.h"
Bert Vermeulenb2c19612011-12-04 10:33:02 +010024#include <glib.h>
Bert Vermeulen1aef2f92011-12-15 03:31:31 +010025#include <inttypes.h>
Bert Vermeulen0bdadba2012-01-17 03:37:34 +010026#include <stdlib.h>
Bert Vermeulenb2c19612011-12-04 10:33:02 +010027
Bert Vermeulenb2c19612011-12-04 10:33:02 +010028
Bert Vermeulend906d3f2012-01-22 02:51:49 +010029/* List of decoder instances. */
Bert Vermeulen7ce77752012-01-10 00:25:16 +010030static GSList *di_list = NULL;
Bert Vermeulend906d3f2012-01-22 02:51:49 +010031
32/* List of frontend callbacks to receive PD output. */
Bert Vermeulen983cb0f2012-01-08 03:20:12 +010033static GSList *callbacks = NULL;
34
Bert Vermeulenb2c19612011-12-04 10:33:02 +010035/* lives in decoder.c */
Bert Vermeulene5080882011-12-07 09:56:49 +010036extern GSList *pd_list;
Bert Vermeulenb2c19612011-12-04 10:33:02 +010037
Bert Vermeulenbc5f5a42012-01-05 03:31:36 +010038/* lives in module_sigrokdecode.c */
39extern PyMODINIT_FUNC PyInit_sigrokdecode(void);
Bert Vermeulenb2c19612011-12-04 10:33:02 +010040
Bert Vermeulenbc5f5a42012-01-05 03:31:36 +010041/* lives in type_logic.c */
42extern PyTypeObject srd_logic_type;
Bert Vermeulenb2c19612011-12-04 10:33:02 +010043
44
Bert Vermeulenb2c19612011-12-04 10:33:02 +010045/**
46 * Initialize libsigrokdecode.
47 *
48 * This initializes the Python interpreter, and creates and initializes
49 * a "sigrok" Python module with a single put() method.
50 *
51 * Then, it searches for sigrok protocol decoder files (*.py) in the
52 * "decoders" subdirectory of the the sigrok installation directory.
53 * All decoders that are found are loaded into memory and added to an
54 * internal list of decoders, which can be queried via srd_list_decoders().
55 *
56 * The caller is responsible for calling the clean-up function srd_exit(),
57 * which will properly shut down libsigrokdecode and free its allocated memory.
58 *
Bert Vermeulen7ce77752012-01-10 00:25:16 +010059 * Multiple calls to srd_init(), without calling srd_exit() in between,
Bert Vermeulenb2c19612011-12-04 10:33:02 +010060 * are not allowed.
61 *
62 * @return SRD_OK upon success, a (negative) error code otherwise.
63 * Upon Python errors, return SRD_ERR_PYTHON. If the sigrok decoders
64 * directory cannot be accessed, return SRD_ERR_DECODERS_DIR.
65 * If not enough memory could be allocated, return SRD_ERR_MALLOC.
66 */
67int srd_init(void)
68{
69 int ret;
70
Uwe Hermann7a1712c2012-01-26 01:15:10 +010071 srd_dbg("Initializing libsigrokdecode.");
Bert Vermeulend906d3f2012-01-22 02:51:49 +010072
73 /* Add our own module to the list of built-in modules. */
Bert Vermeulenbc5f5a42012-01-05 03:31:36 +010074 PyImport_AppendInittab("sigrokdecode", PyInit_sigrokdecode);
Bert Vermeulen5f802ec2011-12-27 22:15:53 +010075
Bert Vermeulend906d3f2012-01-22 02:51:49 +010076 /* Initialize the python interpreter. */
Bert Vermeulenb2c19612011-12-04 10:33:02 +010077 Py_Initialize();
78
Bert Vermeulenb2c19612011-12-04 10:33:02 +010079 if ((ret = set_modulepath()) != SRD_OK) {
80 Py_Finalize();
81 return ret;
82 }
83
84 if ((ret = srd_load_all_decoders()) != SRD_OK) {
85 Py_Finalize();
86 return ret;
87 }
88
89 return SRD_OK;
90}
91
92
93/**
94 * Shutdown libsigrokdecode.
95 *
96 * This frees all the memory allocated for protocol decoders and shuts down
97 * the Python interpreter.
98 *
99 * This function should only be called if there was a (successful!) invocation
100 * of srd_init() before. Calling this function multiple times in a row, without
Bert Vermeulen7ce77752012-01-10 00:25:16 +0100101 * any successful srd_init() calls in between, is not allowed.
Bert Vermeulenb2c19612011-12-04 10:33:02 +0100102 *
103 * @return SRD_OK upon success, a (negative) error code otherwise.
104 */
105int srd_exit(void)
106{
Bert Vermeulend906d3f2012-01-22 02:51:49 +0100107
Uwe Hermann7a1712c2012-01-26 01:15:10 +0100108 srd_dbg("Exiting libsigrokdecode.");
Bert Vermeulend906d3f2012-01-22 02:51:49 +0100109
Bert Vermeulenb2c19612011-12-04 10:33:02 +0100110 srd_unload_all_decoders();
Bert Vermeulene5080882011-12-07 09:56:49 +0100111 g_slist_free(pd_list);
Bert Vermeulenb2c19612011-12-04 10:33:02 +0100112
113 /* Py_Finalize() returns void, any finalization errors are ignored. */
114 Py_Finalize();
115
116 return SRD_OK;
117}
118
119
120/**
Uwe Hermann9d9fcb32012-01-23 19:58:06 +0100121 * Add an additional search directory for the protocol decoders.
122 *
123 * The specified directory is prepended (not appended!) to Python's sys.path,
124 * in order to search for sigrok protocol decoders in the specified
125 * directories first, and in the generic Python module directories (and in
126 * the current working directory) last. This avoids conflicts if there are
127 * Python modules which have the same name as a sigrok protocol decoder in
128 * sys.path or in the current working directory.
Bert Vermeulenb2c19612011-12-04 10:33:02 +0100129 *
130 * TODO: add path from env var SIGROKDECODE_PATH, config etc
Uwe Hermann639c5682012-01-19 00:21:00 +0100131 * TODO: Should take directoryname/path as input.
Uwe Hermann9d9fcb32012-01-23 19:58:06 +0100132 *
133 * @return TODO.
Bert Vermeulenb2c19612011-12-04 10:33:02 +0100134 */
135int set_modulepath(void)
136{
137 int ret;
Uwe Hermann639c5682012-01-19 00:21:00 +0100138 gchar *path, *s;
Bert Vermeulenb2c19612011-12-04 10:33:02 +0100139
Uwe Hermann639c5682012-01-19 00:21:00 +0100140#ifdef _WIN32
141 gchar **splitted;
142
143 /*
144 * On Windows/MinGW, Python's sys.path needs entries of the form
145 * 'C:\\foo\\bar' instead of '/foo/bar'.
146 */
147
148 splitted = g_strsplit(DECODERS_DIR, "/", 0);
149 path = g_build_pathv("\\\\", splitted);
150 g_strfreev(splitted);
151#else
152 path = g_strdup(DECODERS_DIR);
153#endif
154
Uwe Hermann639c5682012-01-19 00:21:00 +0100155 /* TODO: Sanity check on 'path' (length, escape special chars, ...). */
Uwe Hermann9d9fcb32012-01-23 19:58:06 +0100156 s = g_strdup_printf("import sys; sys.path.insert(0, r'%s')", path);
Uwe Hermann639c5682012-01-19 00:21:00 +0100157
158 ret = PyRun_SimpleString(s);
159
160 g_free(path);
161 g_free(s);
Bert Vermeulenb2c19612011-12-04 10:33:02 +0100162
163 return ret;
164}
165
166
Bert Vermeulen7ce77752012-01-10 00:25:16 +0100167/**
Bert Vermeulen0bdadba2012-01-17 03:37:34 +0100168 * Set options in a decoder instance.
169 *
170 * @param di Decoder instance.
171 * @param options A GHashTable of options to set.
172 *
173 * Handled options are removed from the hash.
174 *
175 * @return SRD_OK upon success, a (negative) error code otherwise.
176 */
177int srd_instance_set_options(struct srd_decoder_instance *di,
178 GHashTable *options)
179{
180 PyObject *py_dec_options, *py_dec_optkeys, *py_di_options, *py_optval;
181 PyObject *py_optlist, *py_classval;
182 Py_UNICODE *py_ustr;
183 unsigned long long int val_ull;
184 int num_optkeys, ret, size, i;
185 char *key, *value;
186
Bert Vermeulene431d9c2012-01-18 22:59:14 +0100187 if(!PyObject_HasAttrString(di->decoder->py_dec, "options")) {
Bert Vermeulen0bdadba2012-01-17 03:37:34 +0100188 /* Decoder has no options. */
Bert Vermeulene431d9c2012-01-18 22:59:14 +0100189 if (g_hash_table_size(options) == 0) {
190 /* No options provided. */
191 return SRD_OK;
192 } else {
193 srd_err("Protocol decoder has no options.");
194 return SRD_ERR_ARG;
195 }
Bert Vermeulen0bdadba2012-01-17 03:37:34 +0100196 return SRD_OK;
Bert Vermeulene431d9c2012-01-18 22:59:14 +0100197 }
Bert Vermeulen0bdadba2012-01-17 03:37:34 +0100198
199 ret = SRD_ERR_PYTHON;
200 key = NULL;
201 py_dec_options = py_dec_optkeys = py_di_options = py_optval = NULL;
202 py_optlist = py_classval = NULL;
203 py_dec_options = PyObject_GetAttrString(di->decoder->py_dec, "options");
Bert Vermeulen0bdadba2012-01-17 03:37:34 +0100204
205 /* All of these are synthesized objects, so they're good. */
206 py_dec_optkeys = PyDict_Keys(py_dec_options);
207 num_optkeys = PyList_Size(py_dec_optkeys);
208 if (!(py_di_options = PyObject_GetAttrString(di->py_instance, "options")))
209 goto err_out;
210 for (i = 0; i < num_optkeys; i++) {
211 /* Get the default class value for this option. */
212 py_str_as_str(PyList_GetItem(py_dec_optkeys, i), &key);
213 if (!(py_optlist = PyDict_GetItemString(py_dec_options, key)))
214 goto err_out;
215 if (!(py_classval = PyList_GetItem(py_optlist, 1)))
216 goto err_out;
Bert Vermeulen130ef082012-01-19 00:00:02 +0100217 if (!PyUnicode_Check(py_classval) && !PyLong_Check(py_classval)) {
218 srd_err("Options of type %s are not yet supported.", Py_TYPE(py_classval)->tp_name);
219 goto err_out;
220 }
Bert Vermeulen0bdadba2012-01-17 03:37:34 +0100221
222 if ((value = g_hash_table_lookup(options, key))) {
223 /* An override for this option was provided. */
224 if (PyUnicode_Check(py_classval)) {
225 if (!(py_optval = PyUnicode_FromString(value))) {
226 /* Some UTF-8 encoding error. */
227 PyErr_Clear();
228 goto err_out;
229 }
230 } else if (PyLong_Check(py_classval)) {
231 if (!(py_optval = PyLong_FromString(value, NULL, 0))) {
232 /* ValueError Exception */
233 PyErr_Clear();
Bert Vermeulend906d3f2012-01-22 02:51:49 +0100234 srd_err("Option %s has invalid value %s: expected integer.",
Bert Vermeulen0bdadba2012-01-17 03:37:34 +0100235 key, value);
236 goto err_out;
237 }
238 }
239 g_hash_table_remove(options, key);
240 } else {
241 /* Use the class default for this option. */
242 if (PyUnicode_Check(py_classval)) {
243 /* Make a brand new copy of the string. */
244 py_ustr = PyUnicode_AS_UNICODE(py_classval);
245 size = PyUnicode_GET_SIZE(py_classval);
246 py_optval = PyUnicode_FromUnicode(py_ustr, size);
247 } else if (PyLong_Check(py_classval)) {
248 /* Make a brand new copy of the integer. */
249 val_ull = PyLong_AsUnsignedLongLong(py_classval);
250 if (val_ull == (unsigned long long)-1) {
251 /* OverFlowError exception */
252 PyErr_Clear();
Bert Vermeulend906d3f2012-01-22 02:51:49 +0100253 srd_err("Invalid integer value for %s: expected integer.", key);
Bert Vermeulen0bdadba2012-01-17 03:37:34 +0100254 goto err_out;
255 }
256 if (!(py_optval = PyLong_FromUnsignedLongLong(val_ull)))
257 goto err_out;
258 }
259 }
260
261 /* If we got here, py_optval holds a known good new reference
262 * to the instance option to set.
263 */
264 if (PyDict_SetItemString(py_di_options, key, py_optval) == -1)
265 goto err_out;
266 }
267
268 ret = SRD_OK;
269
270err_out:
271 Py_XDECREF(py_optlist);
272 Py_XDECREF(py_di_options);
273 Py_XDECREF(py_dec_optkeys);
274 Py_XDECREF(py_dec_options);
275 if (key)
276 g_free(key);
Bert Vermeulen2086c682012-01-23 04:51:33 +0100277 if (PyErr_Occurred())
Uwe Hermann7a1712c2012-01-26 01:15:10 +0100278 catch_exception("Stray exception in srd_instance_set_options().");
Bert Vermeulen0bdadba2012-01-17 03:37:34 +0100279
280 return ret;
281}
282
Bert Vermeulen4fadb122012-01-22 03:29:22 +0100283/* Helper GComparefunc for g_slist_find_custom() in srd_instance_set_probes() */
Bert Vermeulenf38ec282012-01-20 22:25:42 +0100284static gint compare_probe_id(struct srd_probe *a, char *probe_id)
285{
286
287 return strcmp(a->id, probe_id);
288}
289
Bert Vermeulen0bdadba2012-01-17 03:37:34 +0100290/**
291 * Set probes in a decoder instance.
292 *
293 * @param di Decoder instance.
294 * @param probes A GHashTable of probes to set. Key is probe name, value is
295 * the probe number. Samples passed to this instance will be arranged in this
296 * order.
297 *
Bert Vermeulen0bdadba2012-01-17 03:37:34 +0100298 * @return SRD_OK upon success, a (negative) error code otherwise.
299 */
300int srd_instance_set_probes(struct srd_decoder_instance *di,
Bert Vermeulenf38ec282012-01-20 22:25:42 +0100301 GHashTable *new_probes)
Bert Vermeulen0bdadba2012-01-17 03:37:34 +0100302{
Bert Vermeulenf38ec282012-01-20 22:25:42 +0100303 GList *l;
304 GSList *sl;
305 struct srd_probe *p;
306 int *new_probemap, new_probenum;
Bert Vermeulenbe873262012-01-23 19:34:23 +0100307 char *probe_id, *probenum_str;
Bert Vermeulen0bdadba2012-01-17 03:37:34 +0100308
Bert Vermeulence46bee2012-01-27 08:21:58 +0100309 srd_dbg("set probes called for instance %s with list of %d probes",
310 di->instance_id, g_hash_table_size(new_probes));
311
Bert Vermeulenf38ec282012-01-20 22:25:42 +0100312 if (g_hash_table_size(new_probes) == 0)
Bert Vermeulen0bdadba2012-01-17 03:37:34 +0100313 /* No probes provided. */
314 return SRD_OK;
315
Bert Vermeulen19a90ba2012-01-21 19:45:04 +0100316 if(di->dec_num_probes == 0) {
Bert Vermeulen0bdadba2012-01-17 03:37:34 +0100317 /* Decoder has no probes. */
Bert Vermeulenf38ec282012-01-20 22:25:42 +0100318 srd_err("Protocol decoder %s has no probes to define.",
319 di->decoder->name);
320 return SRD_ERR_ARG;
Bert Vermeulen0bdadba2012-01-17 03:37:34 +0100321 }
322
Bert Vermeulenf38ec282012-01-20 22:25:42 +0100323 new_probemap = NULL;
Bert Vermeulen0bdadba2012-01-17 03:37:34 +0100324
Bert Vermeulenf38ec282012-01-20 22:25:42 +0100325 if (!(new_probemap = g_try_malloc(sizeof(int) * di->dec_num_probes))) {
326 srd_err("Failed to malloc new probe map.");
327 return SRD_ERR_MALLOC;
328 }
329
330 for (l = g_hash_table_get_keys(new_probes); l; l = l->next) {
331 probe_id = l->data;
Bert Vermeulenbe873262012-01-23 19:34:23 +0100332 probenum_str = g_hash_table_lookup(new_probes, probe_id);
333 if (!probenum_str) {
334 /* Probe name was specified without a value. */
335 srd_err("No probe number was specified for %s.", probe_id);
336 g_free(new_probemap);
337 return SRD_ERR_ARG;
338 }
339 new_probenum = strtol(probenum_str, NULL, 10);
Bert Vermeulenf38ec282012-01-20 22:25:42 +0100340 if (!(sl = g_slist_find_custom(di->decoder->probes, probe_id,
341 (GCompareFunc)compare_probe_id))) {
342 /* Fall back on optional probes. */
343 if (!(sl = g_slist_find_custom(di->decoder->extra_probes,
344 probe_id, (GCompareFunc)compare_probe_id))) {
Bert Vermeulend906d3f2012-01-22 02:51:49 +0100345 srd_err("Protocol decoder %s has no probe '%s'.",
Bert Vermeulenf38ec282012-01-20 22:25:42 +0100346 di->decoder->name, probe_id);
347 g_free(new_probemap);
348 return SRD_ERR_ARG;
349 }
350 }
351 p = sl->data;
352 new_probemap[p->order] = new_probenum;
Bert Vermeulence46bee2012-01-27 08:21:58 +0100353 srd_dbg("setting probe mapping for %d = probe %d", p->order, new_probenum);
Bert Vermeulenf38ec282012-01-20 22:25:42 +0100354 }
355 g_free(di->dec_probemap);
356 di->dec_probemap = new_probemap;
357
358 return SRD_OK;
Bert Vermeulen0bdadba2012-01-17 03:37:34 +0100359}
360
361/**
Bert Vermeulen7ce77752012-01-10 00:25:16 +0100362 * Create a new protocol decoder instance.
363 *
Uwe Hermann42a85ed2012-01-10 20:00:25 +0100364 * @param id Decoder 'id' field.
Bert Vermeulen0bdadba2012-01-17 03:37:34 +0100365 * @param options GHashtable of options which override the defaults set in
366 * the decoder class.
Uwe Hermann42a85ed2012-01-10 20:00:25 +0100367 * @return Pointer to a newly allocated struct srd_decoder_instance, or
Bert Vermeulen0bdadba2012-01-17 03:37:34 +0100368 * NULL in case of failure.
Bert Vermeulen7ce77752012-01-10 00:25:16 +0100369 */
Bert Vermeulen0bdadba2012-01-17 03:37:34 +0100370struct srd_decoder_instance *srd_instance_new(const char *decoder_id,
371 GHashTable *options)
Bert Vermeulenb2c19612011-12-04 10:33:02 +0100372{
373 struct srd_decoder *dec;
374 struct srd_decoder_instance *di;
Bert Vermeulenf38ec282012-01-20 22:25:42 +0100375 int i;
Bert Vermeulen0bdadba2012-01-17 03:37:34 +0100376 char *instance_id;
Bert Vermeulenb2c19612011-12-04 10:33:02 +0100377
Uwe Hermann7a1712c2012-01-26 01:15:10 +0100378 srd_dbg("Creating new %s instance.", decoder_id);
Bert Vermeulen7ce77752012-01-10 00:25:16 +0100379
Bert Vermeulen0bdadba2012-01-17 03:37:34 +0100380 if (!(dec = srd_get_decoder_by_id(decoder_id))) {
381 srd_err("Protocol decoder %s not found.", decoder_id);
Bert Vermeulen7ce77752012-01-10 00:25:16 +0100382 return NULL;
383 }
Bert Vermeulen0bdadba2012-01-17 03:37:34 +0100384
385 if (!(di = g_try_malloc0(sizeof(*di)))) {
386 srd_err("Failed to malloc instance.");
387 return NULL;
388 }
389
390 instance_id = g_hash_table_lookup(options, "id");
Bert Vermeulenb2c19612011-12-04 10:33:02 +0100391 di->decoder = dec;
Bert Vermeulen0bdadba2012-01-17 03:37:34 +0100392 di->instance_id = g_strdup(instance_id ? instance_id : decoder_id);
393 g_hash_table_remove(options, "id");
Bert Vermeulenb2c19612011-12-04 10:33:02 +0100394
Bert Vermeulenf38ec282012-01-20 22:25:42 +0100395 /* Prepare a default probe map, where samples come in the
396 * order in which the decoder class defined them.
397 */
398 di->dec_num_probes = g_slist_length(di->decoder->probes) +
399 g_slist_length(di->decoder->extra_probes);
Bert Vermeulen19a90ba2012-01-21 19:45:04 +0100400 if (di->dec_num_probes) {
401 if (!(di->dec_probemap = g_try_malloc(sizeof(int) * di->dec_num_probes))) {
402 srd_err("Failed to malloc probe map.");
403 g_free(di);
404 return NULL;
405 }
406 for (i = 0; i < di->dec_num_probes; i++)
407 di->dec_probemap[i] = i;
Bert Vermeulenf38ec282012-01-20 22:25:42 +0100408 }
Bert Vermeulenf38ec282012-01-20 22:25:42 +0100409
Bert Vermeulen0bdadba2012-01-17 03:37:34 +0100410 /* Create a new instance of this decoder class. */
411 if (!(di->py_instance = PyObject_CallObject(dec->py_dec, NULL))) {
Bert Vermeulenb2c19612011-12-04 10:33:02 +0100412 if (PyErr_Occurred())
Bert Vermeulen2086c682012-01-23 04:51:33 +0100413 catch_exception("failed to create %s instance: ", decoder_id);
Bert Vermeulenf38ec282012-01-20 22:25:42 +0100414 g_free(di->dec_probemap);
415 g_free(di);
416 return NULL;
417 }
418
419 if (srd_instance_set_options(di, options) != SRD_OK) {
420 g_free(di->dec_probemap);
Bert Vermeulen0bdadba2012-01-17 03:37:34 +0100421 g_free(di);
Bert Vermeulen7ce77752012-01-10 00:25:16 +0100422 return NULL;
Bert Vermeulenb2c19612011-12-04 10:33:02 +0100423 }
Bert Vermeulen7ce77752012-01-10 00:25:16 +0100424
425 /* Instance takes input from a frontend by default. */
Bert Vermeulene5080882011-12-07 09:56:49 +0100426 di_list = g_slist_append(di_list, di);
Bert Vermeulenb2c19612011-12-04 10:33:02 +0100427
Bert Vermeulenb2c19612011-12-04 10:33:02 +0100428 return di;
429}
430
Bert Vermeulen7ce77752012-01-10 00:25:16 +0100431int srd_instance_stack(struct srd_decoder_instance *di_from,
432 struct srd_decoder_instance *di_to)
433{
434
435 if (!di_from || !di_to) {
Bert Vermeulend906d3f2012-01-22 02:51:49 +0100436 srd_err("Invalid from/to instance pair.");
Bert Vermeulen7ce77752012-01-10 00:25:16 +0100437 return SRD_ERR_ARG;
438 }
439
Bert Vermeulen2072ae02012-01-25 01:49:35 +0100440 if (g_slist_find(di_list, di_to)) {
441 /* Remove from the unstacked list. */
442 di_list = g_slist_remove(di_list, di_to);
Bert Vermeulen7ce77752012-01-10 00:25:16 +0100443 }
444
Bert Vermeulen7ce77752012-01-10 00:25:16 +0100445 /* Stack on top of source di. */
446 di_from->next_di = g_slist_append(di_from->next_di, di_to);
447
448 return SRD_OK;
449}
450
Bert Vermeulen2072ae02012-01-25 01:49:35 +0100451/**
452 * Finds a decoder instance by its instance id, but only in the bottom
453 * level of instances -- instances already stacked on top of another one
454 * will not be found.
455 *
456 * @param instance_id The instance id to be found.
457 *
458 * @return Pointer to struct srd_decoder_instance, or NULL if not found.
459 */
460struct srd_decoder_instance *srd_instance_find_by_id(char *instance_id)
Bert Vermeulenb2c19612011-12-04 10:33:02 +0100461{
Bert Vermeulen7ce77752012-01-10 00:25:16 +0100462 GSList *l;
463 struct srd_decoder_instance *tmp, *di;
Bert Vermeulenb2c19612011-12-04 10:33:02 +0100464
Bert Vermeulen7ce77752012-01-10 00:25:16 +0100465 di = NULL;
466 for (l = di_list; l; l = l->next) {
467 tmp = l->data;
468 if (!strcmp(tmp->instance_id, instance_id)) {
469 di = tmp;
470 break;
Bert Vermeulenb2c19612011-12-04 10:33:02 +0100471 }
Bert Vermeulenb2c19612011-12-04 10:33:02 +0100472 }
473
Bert Vermeulen7ce77752012-01-10 00:25:16 +0100474 return di;
475}
476
Bert Vermeulen2072ae02012-01-25 01:49:35 +0100477/**
478 * Finds a decoder instance by its python object, i.e. that instance's
479 * instantiation of the sigrokdecode.Decoder class. This will recurse
480 * to find the instance anywhere in the stack tree.
481 *
482 * @param stack Pointer to a GSList of struct srd_decoder_instance,
483 * indicating the stack to search. To start searching at the bottom
484 * level of decoder instances, pass NULL.
485 * @param obj The python class instantiation.
486 *
487 * @return Pointer to struct srd_decoder_instance, or NULL if not found.
488 */
489struct srd_decoder_instance *srd_instance_find_by_obj(GSList *stack,
490 PyObject *obj)
491{
492 GSList *l;
493 struct srd_decoder_instance *tmp, *di;
494
495 di = NULL;
496 for (l = stack ? stack : di_list; di == NULL && l != NULL; l = l->next) {
497 tmp = l->data;
498 if (tmp->py_instance == obj)
499 di = tmp;
500 else if (tmp->next_di)
501 di = srd_instance_find_by_obj(tmp->next_di, obj);
502 }
503
504 return di;
505}
506
Bert Vermeulen7ce77752012-01-10 00:25:16 +0100507int srd_instance_start(struct srd_decoder_instance *di, PyObject *args)
508{
509 PyObject *py_name, *py_res;
Bert Vermeulen2072ae02012-01-25 01:49:35 +0100510 GSList *l;
511 struct srd_decoder_instance *next_di;
Bert Vermeulen7ce77752012-01-10 00:25:16 +0100512
Uwe Hermann7a1712c2012-01-26 01:15:10 +0100513 srd_dbg("Calling start() method on protocol decoder instance %s.",
514 di->instance_id);
Bert Vermeulen7ce77752012-01-10 00:25:16 +0100515
516 if (!(py_name = PyUnicode_FromString("start"))) {
Bert Vermeulend906d3f2012-01-22 02:51:49 +0100517 srd_err("Unable to build python object for 'start'.");
Bert Vermeulen2086c682012-01-23 04:51:33 +0100518 catch_exception("Protocol decoder instance %s: ", di->instance_id);
Bert Vermeulen7ce77752012-01-10 00:25:16 +0100519 return SRD_ERR_PYTHON;
520 }
521
522 if (!(py_res = PyObject_CallMethodObjArgs(di->py_instance,
523 py_name, args, NULL))) {
Bert Vermeulen2086c682012-01-23 04:51:33 +0100524 catch_exception("Protocol decoder instance %s: ", di->instance_id);
Bert Vermeulen7ce77752012-01-10 00:25:16 +0100525 return SRD_ERR_PYTHON;
526 }
527
Bert Vermeulenf38ec282012-01-20 22:25:42 +0100528 Py_DecRef(py_res);
529 Py_DecRef(py_name);
Bert Vermeulen7ce77752012-01-10 00:25:16 +0100530
Bert Vermeulen2072ae02012-01-25 01:49:35 +0100531 /* Start all the PDs stacked on top of this one. Pass along the
532 * metadata all the way from the bottom PD, even though it's only
533 * applicable to logic data for now.
534 */
535 for (l = di->next_di; l; l = l->next) {
536 next_di = l->data;
537 srd_instance_start(next_di, args);
538 }
539
Bert Vermeulenb2c19612011-12-04 10:33:02 +0100540 return SRD_OK;
541}
542
Bert Vermeulenb2c19612011-12-04 10:33:02 +0100543/**
544 * Run the specified decoder function.
545 *
Bert Vermeulend906d3f2012-01-22 02:51:49 +0100546 * @param start_samplenum The starting sample number for the buffer's sample
Uwe Hermann7a1712c2012-01-26 01:15:10 +0100547 * set, relative to the start of capture.
548 * @param di The decoder instance to call. Must not be NULL.
549 * @param inbuf The buffer to decode. Must not be NULL.
550 * @param inbuflen Length of the buffer. Must be > 0.
Bert Vermeulenb2c19612011-12-04 10:33:02 +0100551 *
552 * @return SRD_OK upon success, a (negative) error code otherwise.
553 */
Bert Vermeulen86528292012-01-15 23:20:39 +0100554int srd_instance_decode(uint64_t start_samplenum,
Uwe Hermann7a1712c2012-01-26 01:15:10 +0100555 struct srd_decoder_instance *di, uint8_t *inbuf, uint64_t inbuflen)
Bert Vermeulenb2c19612011-12-04 10:33:02 +0100556{
Bert Vermeulend906d3f2012-01-22 02:51:49 +0100557 PyObject *py_res;
Bert Vermeulenbc5f5a42012-01-05 03:31:36 +0100558 srd_logic *logic;
Bert Vermeulen86528292012-01-15 23:20:39 +0100559 uint64_t end_samplenum;
Bert Vermeulenb2c19612011-12-04 10:33:02 +0100560
Uwe Hermann7a1712c2012-01-26 01:15:10 +0100561 srd_dbg("Calling decode() on instance %s with %d bytes starting "
562 "at sample %d.", di->instance_id, inbuflen, start_samplenum);
Bert Vermeulend906d3f2012-01-22 02:51:49 +0100563
Bert Vermeulenb2c19612011-12-04 10:33:02 +0100564 /* Return an error upon unusable input. */
Uwe Hermann7a1712c2012-01-26 01:15:10 +0100565 if (!di) {
566 srd_dbg("empty decoder instance");
Bert Vermeulend906d3f2012-01-22 02:51:49 +0100567 return SRD_ERR_ARG;
568 }
Uwe Hermann7a1712c2012-01-26 01:15:10 +0100569 if (!inbuf) {
570 srd_dbg("NULL buffer pointer");
Bert Vermeulend906d3f2012-01-22 02:51:49 +0100571 return SRD_ERR_ARG;
572 }
573 if (inbuflen == 0) {
Uwe Hermann7a1712c2012-01-26 01:15:10 +0100574 srd_dbg("empty buffer");
Bert Vermeulend906d3f2012-01-22 02:51:49 +0100575 return SRD_ERR_ARG;
576 }
Bert Vermeulenb2c19612011-12-04 10:33:02 +0100577
Bert Vermeulend906d3f2012-01-22 02:51:49 +0100578 /* Create new srd_logic object. Each iteration around the PD's loop
579 * will fill one sample into this object.
580 */
Bert Vermeulenbc5f5a42012-01-05 03:31:36 +0100581 logic = PyObject_New(srd_logic, &srd_logic_type);
582 Py_INCREF(logic);
583 logic->di = di;
Bert Vermeulen86528292012-01-15 23:20:39 +0100584 logic->start_samplenum = start_samplenum;
Bert Vermeulenbc5f5a42012-01-05 03:31:36 +0100585 logic->itercnt = 0;
586 logic->inbuf = inbuf;
587 logic->inbuflen = inbuflen;
588 logic->sample = PyList_New(2);
589 Py_INCREF(logic->sample);
590
Bert Vermeulend906d3f2012-01-22 02:51:49 +0100591 Py_IncRef(di->py_instance);
Bert Vermeulenf38ec282012-01-20 22:25:42 +0100592 end_samplenum = start_samplenum + inbuflen / di->data_unitsize;
Bert Vermeulend906d3f2012-01-22 02:51:49 +0100593 if (!(py_res = PyObject_CallMethod(di->py_instance, "decode",
Bert Vermeulen86528292012-01-15 23:20:39 +0100594 "KKO", logic->start_samplenum, end_samplenum, logic))) {
Bert Vermeulen2086c682012-01-23 04:51:33 +0100595 catch_exception("Protocol decoder instance %s: ", di->instance_id);
Bert Vermeulenb2c19612011-12-04 10:33:02 +0100596 return SRD_ERR_PYTHON; /* TODO: More specific error? */
597 }
Bert Vermeulend906d3f2012-01-22 02:51:49 +0100598 Py_DecRef(py_res);
Bert Vermeulenbc5f5a42012-01-05 03:31:36 +0100599
Bert Vermeulenb2c19612011-12-04 10:33:02 +0100600 return SRD_OK;
601}
602
603
Bert Vermeulen7ce77752012-01-10 00:25:16 +0100604int srd_session_start(int num_probes, int unitsize, uint64_t samplerate)
605{
606 PyObject *args;
Bert Vermeulen2072ae02012-01-25 01:49:35 +0100607 GSList *d;
Bert Vermeulen7ce77752012-01-10 00:25:16 +0100608 struct srd_decoder_instance *di;
609 int ret;
610
Uwe Hermann7a1712c2012-01-26 01:15:10 +0100611 srd_dbg("Calling start() on all instances with %d probes, "
612 "unitsize %d samplerate %d.", num_probes, unitsize, samplerate);
Bert Vermeulend906d3f2012-01-22 02:51:49 +0100613
614 /* Currently only one item of metadata is passed along to decoders,
615 * samplerate. This can be extended as needed.
616 */
Bert Vermeulen7ce77752012-01-10 00:25:16 +0100617 if (!(args = Py_BuildValue("{s:l}", "samplerate", (long)samplerate))) {
Bert Vermeulend906d3f2012-01-22 02:51:49 +0100618 srd_err("Unable to build python object for metadata.");
Bert Vermeulen7ce77752012-01-10 00:25:16 +0100619 return SRD_ERR_PYTHON;
620 }
621
622 /* Run the start() method on all decoders receiving frontend data. */
623 for (d = di_list; d; d = d->next) {
624 di = d->data;
Bert Vermeulenf38ec282012-01-20 22:25:42 +0100625 di->data_num_probes = num_probes;
626 di->data_unitsize = unitsize;
627 di->data_samplerate = samplerate;
Bert Vermeulen7ce77752012-01-10 00:25:16 +0100628 if ((ret = srd_instance_start(di, args) != SRD_OK))
Bert Vermeulen2072ae02012-01-25 01:49:35 +0100629 break;
Bert Vermeulen7ce77752012-01-10 00:25:16 +0100630 }
631
Bert Vermeulend906d3f2012-01-22 02:51:49 +0100632 Py_DecRef(args);
Bert Vermeulen7ce77752012-01-10 00:25:16 +0100633
Bert Vermeulen2072ae02012-01-25 01:49:35 +0100634 return ret;
Bert Vermeulen7ce77752012-01-10 00:25:16 +0100635}
636
Bert Vermeulenb2c19612011-12-04 10:33:02 +0100637/* Feed logic samples to decoder session. */
Bert Vermeulen86528292012-01-15 23:20:39 +0100638int srd_session_feed(uint64_t start_samplenum, uint8_t *inbuf, uint64_t inbuflen)
Bert Vermeulenb2c19612011-12-04 10:33:02 +0100639{
640 GSList *d;
641 int ret;
642
Uwe Hermann7a1712c2012-01-26 01:15:10 +0100643 srd_dbg("Calling decode() on all instances with starting sample "
644 "number %"PRIu64", %"PRIu64" bytes at 0x%p", start_samplenum,
645 inbuflen, inbuf);
Bert Vermeulend906d3f2012-01-22 02:51:49 +0100646
Bert Vermeulene5080882011-12-07 09:56:49 +0100647 for (d = di_list; d; d = d->next) {
Bert Vermeulen86528292012-01-15 23:20:39 +0100648 if ((ret = srd_instance_decode(start_samplenum, d->data, inbuf,
Bert Vermeulen1aef2f92011-12-15 03:31:31 +0100649 inbuflen)) != SRD_OK)
Bert Vermeulenb2c19612011-12-04 10:33:02 +0100650 return ret;
651 }
652
653 return SRD_OK;
654}
655
656
Bert Vermeulen4fadb122012-01-22 03:29:22 +0100657int srd_register_callback(int output_type, void *cb)
658{
659 struct srd_pd_callback *pd_cb;
660
Uwe Hermann7a1712c2012-01-26 01:15:10 +0100661 srd_dbg("Registering new callback for output type %d.", output_type);
Bert Vermeulen4fadb122012-01-22 03:29:22 +0100662
663 if (!(pd_cb = g_try_malloc(sizeof(struct srd_pd_callback))))
664 return SRD_ERR_MALLOC;
665
666 pd_cb->output_type = output_type;
667 pd_cb->callback = cb;
668 callbacks = g_slist_append(callbacks, pd_cb);
669
670 return SRD_OK;
671}
672
673void *srd_find_callback(int output_type)
674{
675 GSList *l;
676 struct srd_pd_callback *pd_cb;
677 void *(cb);
678
679 cb = NULL;
680 for (l = callbacks; l; l = l->next) {
681 pd_cb = l->data;
682 if (pd_cb->output_type == output_type) {
683 cb = pd_cb->callback;
684 break;
685 }
686 }
687
688 return cb;
689}
690
691
Bert Vermeulend906d3f2012-01-22 02:51:49 +0100692/* This is the backend function to python sigrokdecode.add() call. */
Uwe Hermann7a1712c2012-01-26 01:15:10 +0100693int pd_add(struct srd_decoder_instance *di, int output_type, char *proto_id)
Bert Vermeulene5080882011-12-07 09:56:49 +0100694{
Bert Vermeulene5080882011-12-07 09:56:49 +0100695 struct srd_pd_output *pdo;
Bert Vermeulene5080882011-12-07 09:56:49 +0100696
Uwe Hermann7a1712c2012-01-26 01:15:10 +0100697 srd_dbg("Instance %s creating new output type %d for %s.",
698 di->instance_id, output_type, proto_id);
Bert Vermeulend906d3f2012-01-22 02:51:49 +0100699
Bert Vermeulene5080882011-12-07 09:56:49 +0100700 if (!(pdo = g_try_malloc(sizeof(struct srd_pd_output))))
701 return -1;
702
Bert Vermeulenb2315462012-01-09 00:13:03 +0100703 /* pdo_id is just a simple index, nothing is deleted from this list anyway. */
Bert Vermeulen15969942012-01-07 02:50:14 +0100704 pdo->pdo_id = g_slist_length(di->pd_output);
Bert Vermeulene5080882011-12-07 09:56:49 +0100705 pdo->output_type = output_type;
Bert Vermeulenb3b2cae2012-01-24 02:02:03 +0100706 pdo->di = di;
Uwe Hermann94d43b32012-01-10 20:44:22 +0100707 pdo->proto_id = g_strdup(proto_id);
Bert Vermeulene5080882011-12-07 09:56:49 +0100708 di->pd_output = g_slist_append(di->pd_output, pdo);
709
Bert Vermeulen15969942012-01-07 02:50:14 +0100710 return pdo->pdo_id;
Bert Vermeulene5080882011-12-07 09:56:49 +0100711}
712