]>
Commit | Line | Data |
---|---|---|
b2c19614 BV |
1 | /* |
2 | * This file is part of the sigrok project. | |
3 | * | |
4 | * Copyright (C) 2010 Uwe Hermann <uwe@hermann-uwe.de> | |
bc5f5a43 | 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 | ||
73191416 | 21 | #include "sigrokdecode.h" /* First, so we avoid a _POSIX_C_SOURCE warning. */ |
7ce7775c BV |
22 | #include "sigrokdecode-internal.h" |
23 | #include "config.h" | |
b2c19614 | 24 | #include <glib.h> |
1aef2f93 | 25 | #include <inttypes.h> |
0bdadba2 | 26 | #include <stdlib.h> |
b2c19614 | 27 | |
b2c19614 | 28 | |
d906d3f9 | 29 | /* List of decoder instances. */ |
7ce7775c | 30 | static GSList *di_list = NULL; |
d906d3f9 BV |
31 | |
32 | /* List of frontend callbacks to receive PD output. */ | |
983cb0f5 BV |
33 | static GSList *callbacks = NULL; |
34 | ||
bc5f5a43 BV |
35 | /* lives in decoder.c */ |
36 | extern GSList *pd_list; | |
b2c19614 | 37 | |
bc5f5a43 BV |
38 | /* lives in module_sigrokdecode.c */ |
39 | extern PyMODINIT_FUNC PyInit_sigrokdecode(void); | |
b2c19614 | 40 | |
bc5f5a43 BV |
41 | /* lives in type_logic.c */ |
42 | extern PyTypeObject srd_logic_type; | |
b2c19614 | 43 | |
e5080882 | 44 | |
b2c19614 BV |
45 | /** |
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 | * | |
7ce7775c | 59 | * Multiple calls to srd_init(), without calling srd_exit() in between, |
b2c19614 BV |
60 | * 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 | */ | |
67 | int srd_init(void) | |
68 | { | |
69 | int ret; | |
70 | ||
7a1712c4 | 71 | srd_dbg("Initializing libsigrokdecode."); |
d906d3f9 BV |
72 | |
73 | /* Add our own module to the list of built-in modules. */ | |
bc5f5a43 | 74 | PyImport_AppendInittab("sigrokdecode", PyInit_sigrokdecode); |
5f802ec6 | 75 | |
d906d3f9 | 76 | /* Initialize the python interpreter. */ |
b2c19614 BV |
77 | Py_Initialize(); |
78 | ||
b2c19614 BV |
79 | 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 | |
7ce7775c | 101 | * any successful srd_init() calls in between, is not allowed. |
b2c19614 BV |
102 | * |
103 | * @return SRD_OK upon success, a (negative) error code otherwise. | |
104 | */ | |
105 | int srd_exit(void) | |
106 | { | |
d906d3f9 | 107 | |
7a1712c4 | 108 | srd_dbg("Exiting libsigrokdecode."); |
d906d3f9 | 109 | |
b2c19614 | 110 | srd_unload_all_decoders(); |
e5080882 | 111 | g_slist_free(pd_list); |
b2c19614 BV |
112 | |
113 | /* Py_Finalize() returns void, any finalization errors are ignored. */ | |
114 | Py_Finalize(); | |
115 | ||
116 | return SRD_OK; | |
117 | } | |
118 | ||
119 | ||
120 | /** | |
9d9fcb37 UH |
121 | * 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. | |
b2c19614 BV |
129 | * |
130 | * TODO: add path from env var SIGROKDECODE_PATH, config etc | |
639c5689 | 131 | * TODO: Should take directoryname/path as input. |
9d9fcb37 UH |
132 | * |
133 | * @return TODO. | |
b2c19614 BV |
134 | */ |
135 | int set_modulepath(void) | |
136 | { | |
137 | int ret; | |
639c5689 | 138 | gchar *path, *s; |
b2c19614 | 139 | |
639c5689 UH |
140 | #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 | ||
639c5689 | 155 | /* TODO: Sanity check on 'path' (length, escape special chars, ...). */ |
9d9fcb37 | 156 | s = g_strdup_printf("import sys; sys.path.insert(0, r'%s')", path); |
639c5689 UH |
157 | |
158 | ret = PyRun_SimpleString(s); | |
159 | ||
160 | g_free(path); | |
161 | g_free(s); | |
b2c19614 BV |
162 | |
163 | return ret; | |
164 | } | |
165 | ||
166 | ||
7ce7775c | 167 | /** |
0bdadba2 BV |
168 | * Set options in a decoder instance. |
169 | * | |
170 | * @param di Decoder instance. | |
171 | * @param options A GHashTable of options to set. | |
7ce7775c | 172 | * |
0bdadba2 BV |
173 | * Handled options are removed from the hash. |
174 | * | |
175 | * @return SRD_OK upon success, a (negative) error code otherwise. | |
176 | */ | |
177 | int 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 | ||
e431d9cc | 187 | if(!PyObject_HasAttrString(di->decoder->py_dec, "options")) { |
0bdadba2 | 188 | /* Decoder has no options. */ |
e431d9cc BV |
189 | 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 | } | |
0bdadba2 | 196 | return SRD_OK; |
e431d9cc | 197 | } |
0bdadba2 BV |
198 | |
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"); | |
0bdadba2 BV |
204 | |
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; | |
130ef08a BV |
217 | 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 | } | |
0bdadba2 BV |
221 | |
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(); | |
d906d3f9 | 234 | srd_err("Option %s has invalid value %s: expected integer.", |
0bdadba2 BV |
235 | 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(); | |
d906d3f9 | 253 | srd_err("Invalid integer value for %s: expected integer.", key); |
0bdadba2 BV |
254 | 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 | ||
270 | err_out: | |
271 | Py_XDECREF(py_optlist); | |
272 | Py_XDECREF(py_di_options); | |
273 | Py_XDECREF(py_dec_optkeys); | |
274 | Py_XDECREF(py_dec_options); | |
6f858319 | 275 | g_free(key); |
2086c684 | 276 | if (PyErr_Occurred()) |
7a1712c4 | 277 | catch_exception("Stray exception in srd_instance_set_options()."); |
0bdadba2 BV |
278 | |
279 | return ret; | |
280 | } | |
281 | ||
4fadb128 | 282 | /* Helper GComparefunc for g_slist_find_custom() in srd_instance_set_probes() */ |
f38ec285 BV |
283 | static gint compare_probe_id(struct srd_probe *a, char *probe_id) |
284 | { | |
285 | ||
286 | return strcmp(a->id, probe_id); | |
287 | } | |
288 | ||
0bdadba2 BV |
289 | /** |
290 | * Set probes in a decoder instance. | |
291 | * | |
292 | * @param di Decoder instance. | |
293 | * @param probes A GHashTable of probes to set. Key is probe name, value is | |
294 | * the probe number. Samples passed to this instance will be arranged in this | |
295 | * order. | |
296 | * | |
0bdadba2 BV |
297 | * @return SRD_OK upon success, a (negative) error code otherwise. |
298 | */ | |
299 | int srd_instance_set_probes(struct srd_decoder_instance *di, | |
f38ec285 | 300 | GHashTable *new_probes) |
0bdadba2 | 301 | { |
f38ec285 BV |
302 | GList *l; |
303 | GSList *sl; | |
304 | struct srd_probe *p; | |
305 | int *new_probemap, new_probenum; | |
be873260 | 306 | char *probe_id, *probenum_str; |
0bdadba2 | 307 | |
ce46beed BV |
308 | srd_dbg("set probes called for instance %s with list of %d probes", |
309 | di->instance_id, g_hash_table_size(new_probes)); | |
310 | ||
f38ec285 | 311 | if (g_hash_table_size(new_probes) == 0) |
0bdadba2 BV |
312 | /* No probes provided. */ |
313 | return SRD_OK; | |
314 | ||
19a90bab | 315 | if(di->dec_num_probes == 0) { |
0bdadba2 | 316 | /* Decoder has no probes. */ |
f38ec285 BV |
317 | srd_err("Protocol decoder %s has no probes to define.", |
318 | di->decoder->name); | |
319 | return SRD_ERR_ARG; | |
320 | } | |
0bdadba2 | 321 | |
f38ec285 | 322 | new_probemap = NULL; |
0bdadba2 | 323 | |
f38ec285 BV |
324 | if (!(new_probemap = g_try_malloc(sizeof(int) * di->dec_num_probes))) { |
325 | srd_err("Failed to malloc new probe map."); | |
326 | return SRD_ERR_MALLOC; | |
0bdadba2 BV |
327 | } |
328 | ||
f38ec285 BV |
329 | for (l = g_hash_table_get_keys(new_probes); l; l = l->next) { |
330 | probe_id = l->data; | |
be873260 BV |
331 | probenum_str = g_hash_table_lookup(new_probes, probe_id); |
332 | if (!probenum_str) { | |
333 | /* Probe name was specified without a value. */ | |
334 | srd_err("No probe number was specified for %s.", probe_id); | |
335 | g_free(new_probemap); | |
336 | return SRD_ERR_ARG; | |
337 | } | |
338 | new_probenum = strtol(probenum_str, NULL, 10); | |
f38ec285 BV |
339 | if (!(sl = g_slist_find_custom(di->decoder->probes, probe_id, |
340 | (GCompareFunc)compare_probe_id))) { | |
341 | /* Fall back on optional probes. */ | |
dcdf4883 | 342 | if (!(sl = g_slist_find_custom(di->decoder->opt_probes, |
f38ec285 | 343 | probe_id, (GCompareFunc)compare_probe_id))) { |
d906d3f9 | 344 | srd_err("Protocol decoder %s has no probe '%s'.", |
f38ec285 BV |
345 | di->decoder->name, probe_id); |
346 | g_free(new_probemap); | |
347 | return SRD_ERR_ARG; | |
348 | } | |
349 | } | |
350 | p = sl->data; | |
351 | new_probemap[p->order] = new_probenum; | |
ce46beed | 352 | srd_dbg("setting probe mapping for %d = probe %d", p->order, new_probenum); |
f38ec285 BV |
353 | } |
354 | g_free(di->dec_probemap); | |
355 | di->dec_probemap = new_probemap; | |
0bdadba2 | 356 | |
f38ec285 | 357 | return SRD_OK; |
0bdadba2 BV |
358 | } |
359 | ||
360 | /** | |
361 | * Create a new protocol decoder instance. | |
7ce7775c | 362 | * |
42a85ed0 | 363 | * @param id Decoder 'id' field. |
0bdadba2 BV |
364 | * @param options GHashtable of options which override the defaults set in |
365 | * the decoder class. | |
42a85ed0 | 366 | * @return Pointer to a newly allocated struct srd_decoder_instance, or |
0bdadba2 | 367 | * NULL in case of failure. |
7ce7775c | 368 | */ |
0bdadba2 BV |
369 | struct srd_decoder_instance *srd_instance_new(const char *decoder_id, |
370 | GHashTable *options) | |
b2c19614 BV |
371 | { |
372 | struct srd_decoder *dec; | |
373 | struct srd_decoder_instance *di; | |
f38ec285 | 374 | int i; |
0bdadba2 | 375 | char *instance_id; |
b2c19614 | 376 | |
7a1712c4 | 377 | srd_dbg("Creating new %s instance.", decoder_id); |
7ce7775c | 378 | |
0bdadba2 BV |
379 | if (!(dec = srd_get_decoder_by_id(decoder_id))) { |
380 | srd_err("Protocol decoder %s not found.", decoder_id); | |
b2c19614 | 381 | return NULL; |
0bdadba2 | 382 | } |
b2c19614 | 383 | |
0bdadba2 BV |
384 | if (!(di = g_try_malloc0(sizeof(*di)))) { |
385 | srd_err("Failed to malloc instance."); | |
7ce7775c BV |
386 | return NULL; |
387 | } | |
0bdadba2 BV |
388 | |
389 | instance_id = g_hash_table_lookup(options, "id"); | |
b2c19614 | 390 | di->decoder = dec; |
0bdadba2 BV |
391 | di->instance_id = g_strdup(instance_id ? instance_id : decoder_id); |
392 | g_hash_table_remove(options, "id"); | |
b2c19614 | 393 | |
f38ec285 BV |
394 | /* Prepare a default probe map, where samples come in the |
395 | * order in which the decoder class defined them. | |
396 | */ | |
397 | di->dec_num_probes = g_slist_length(di->decoder->probes) + | |
dcdf4883 | 398 | g_slist_length(di->decoder->opt_probes); |
19a90bab BV |
399 | if (di->dec_num_probes) { |
400 | if (!(di->dec_probemap = g_try_malloc(sizeof(int) * di->dec_num_probes))) { | |
401 | srd_err("Failed to malloc probe map."); | |
402 | g_free(di); | |
403 | return NULL; | |
404 | } | |
405 | for (i = 0; i < di->dec_num_probes; i++) | |
406 | di->dec_probemap[i] = i; | |
f38ec285 | 407 | } |
f38ec285 | 408 | |
0bdadba2 BV |
409 | /* Create a new instance of this decoder class. */ |
410 | if (!(di->py_instance = PyObject_CallObject(dec->py_dec, NULL))) { | |
b2c19614 | 411 | if (PyErr_Occurred()) |
2086c684 | 412 | catch_exception("failed to create %s instance: ", decoder_id); |
f38ec285 | 413 | g_free(di->dec_probemap); |
0bdadba2 | 414 | g_free(di); |
7ce7775c | 415 | return NULL; |
b2c19614 | 416 | } |
7ce7775c | 417 | |
0bdadba2 | 418 | if (srd_instance_set_options(di, options) != SRD_OK) { |
f38ec285 | 419 | g_free(di->dec_probemap); |
0bdadba2 BV |
420 | g_free(di); |
421 | return NULL; | |
422 | } | |
b2c19614 | 423 | |
f38ec285 BV |
424 | /* Instance takes input from a frontend by default. */ |
425 | di_list = g_slist_append(di_list, di); | |
426 | ||
b2c19614 BV |
427 | return di; |
428 | } | |
429 | ||
7ce7775c BV |
430 | int srd_instance_stack(struct srd_decoder_instance *di_from, |
431 | struct srd_decoder_instance *di_to) | |
432 | { | |
433 | ||
434 | if (!di_from || !di_to) { | |
d906d3f9 | 435 | srd_err("Invalid from/to instance pair."); |
7ce7775c BV |
436 | return SRD_ERR_ARG; |
437 | } | |
438 | ||
2072ae0c BV |
439 | if (g_slist_find(di_list, di_to)) { |
440 | /* Remove from the unstacked list. */ | |
441 | di_list = g_slist_remove(di_list, di_to); | |
7ce7775c BV |
442 | } |
443 | ||
7ce7775c BV |
444 | /* Stack on top of source di. */ |
445 | di_from->next_di = g_slist_append(di_from->next_di, di_to); | |
446 | ||
447 | return SRD_OK; | |
448 | } | |
449 | ||
2072ae0c BV |
450 | /** |
451 | * Finds a decoder instance by its instance id, but only in the bottom | |
452 | * level of instances -- instances already stacked on top of another one | |
453 | * will not be found. | |
454 | * | |
455 | * @param instance_id The instance id to be found. | |
456 | * | |
457 | * @return Pointer to struct srd_decoder_instance, or NULL if not found. | |
458 | */ | |
459 | struct srd_decoder_instance *srd_instance_find_by_id(char *instance_id) | |
7ce7775c BV |
460 | { |
461 | GSList *l; | |
462 | struct srd_decoder_instance *tmp, *di; | |
b2c19614 | 463 | |
7ce7775c BV |
464 | di = NULL; |
465 | for (l = di_list; l; l = l->next) { | |
466 | tmp = l->data; | |
467 | if (!strcmp(tmp->instance_id, instance_id)) { | |
468 | di = tmp; | |
469 | break; | |
470 | } | |
471 | } | |
472 | ||
473 | return di; | |
474 | } | |
475 | ||
2072ae0c BV |
476 | /** |
477 | * Finds a decoder instance by its python object, i.e. that instance's | |
478 | * instantiation of the sigrokdecode.Decoder class. This will recurse | |
479 | * to find the instance anywhere in the stack tree. | |
480 | * | |
481 | * @param stack Pointer to a GSList of struct srd_decoder_instance, | |
482 | * indicating the stack to search. To start searching at the bottom | |
483 | * level of decoder instances, pass NULL. | |
484 | * @param obj The python class instantiation. | |
485 | * | |
486 | * @return Pointer to struct srd_decoder_instance, or NULL if not found. | |
487 | */ | |
488 | struct srd_decoder_instance *srd_instance_find_by_obj(GSList *stack, | |
489 | PyObject *obj) | |
490 | { | |
491 | GSList *l; | |
492 | struct srd_decoder_instance *tmp, *di; | |
493 | ||
494 | di = NULL; | |
495 | for (l = stack ? stack : di_list; di == NULL && l != NULL; l = l->next) { | |
496 | tmp = l->data; | |
497 | if (tmp->py_instance == obj) | |
498 | di = tmp; | |
499 | else if (tmp->next_di) | |
500 | di = srd_instance_find_by_obj(tmp->next_di, obj); | |
501 | } | |
502 | ||
503 | return di; | |
504 | } | |
505 | ||
7ce7775c | 506 | int srd_instance_start(struct srd_decoder_instance *di, PyObject *args) |
b2c19614 | 507 | { |
7ce7775c | 508 | PyObject *py_name, *py_res; |
2072ae0c BV |
509 | GSList *l; |
510 | struct srd_decoder_instance *next_di; | |
b2c19614 | 511 | |
7a1712c4 UH |
512 | srd_dbg("Calling start() method on protocol decoder instance %s.", |
513 | di->instance_id); | |
b2c19614 | 514 | |
7ce7775c | 515 | if (!(py_name = PyUnicode_FromString("start"))) { |
d906d3f9 | 516 | srd_err("Unable to build python object for 'start'."); |
2086c684 | 517 | catch_exception("Protocol decoder instance %s: ", di->instance_id); |
7ce7775c BV |
518 | return SRD_ERR_PYTHON; |
519 | } | |
520 | ||
521 | if (!(py_res = PyObject_CallMethodObjArgs(di->py_instance, | |
522 | py_name, args, NULL))) { | |
2086c684 | 523 | catch_exception("Protocol decoder instance %s: ", di->instance_id); |
7ce7775c | 524 | return SRD_ERR_PYTHON; |
b2c19614 BV |
525 | } |
526 | ||
f38ec285 BV |
527 | Py_DecRef(py_res); |
528 | Py_DecRef(py_name); | |
7ce7775c | 529 | |
2072ae0c BV |
530 | /* Start all the PDs stacked on top of this one. Pass along the |
531 | * metadata all the way from the bottom PD, even though it's only | |
532 | * applicable to logic data for now. | |
533 | */ | |
534 | for (l = di->next_di; l; l = l->next) { | |
535 | next_di = l->data; | |
536 | srd_instance_start(next_di, args); | |
537 | } | |
538 | ||
b2c19614 BV |
539 | return SRD_OK; |
540 | } | |
541 | ||
b2c19614 BV |
542 | /** |
543 | * Run the specified decoder function. | |
544 | * | |
d906d3f9 | 545 | * @param start_samplenum The starting sample number for the buffer's sample |
7a1712c4 UH |
546 | * set, relative to the start of capture. |
547 | * @param di The decoder instance to call. Must not be NULL. | |
548 | * @param inbuf The buffer to decode. Must not be NULL. | |
549 | * @param inbuflen Length of the buffer. Must be > 0. | |
b2c19614 BV |
550 | * |
551 | * @return SRD_OK upon success, a (negative) error code otherwise. | |
552 | */ | |
86528298 | 553 | int srd_instance_decode(uint64_t start_samplenum, |
7a1712c4 | 554 | struct srd_decoder_instance *di, uint8_t *inbuf, uint64_t inbuflen) |
b2c19614 | 555 | { |
d906d3f9 | 556 | PyObject *py_res; |
bc5f5a43 | 557 | srd_logic *logic; |
86528298 | 558 | uint64_t end_samplenum; |
b2c19614 | 559 | |
7a1712c4 UH |
560 | srd_dbg("Calling decode() on instance %s with %d bytes starting " |
561 | "at sample %d.", di->instance_id, inbuflen, start_samplenum); | |
b2c19614 | 562 | |
d906d3f9 | 563 | /* Return an error upon unusable input. */ |
7a1712c4 UH |
564 | if (!di) { |
565 | srd_dbg("empty decoder instance"); | |
d906d3f9 BV |
566 | return SRD_ERR_ARG; |
567 | } | |
7a1712c4 UH |
568 | if (!inbuf) { |
569 | srd_dbg("NULL buffer pointer"); | |
d906d3f9 BV |
570 | return SRD_ERR_ARG; |
571 | } | |
572 | if (inbuflen == 0) { | |
7a1712c4 | 573 | srd_dbg("empty buffer"); |
d906d3f9 BV |
574 | return SRD_ERR_ARG; |
575 | } | |
b2c19614 | 576 | |
d906d3f9 BV |
577 | /* Create new srd_logic object. Each iteration around the PD's loop |
578 | * will fill one sample into this object. | |
579 | */ | |
bc5f5a43 BV |
580 | logic = PyObject_New(srd_logic, &srd_logic_type); |
581 | Py_INCREF(logic); | |
582 | logic->di = di; | |
86528298 | 583 | logic->start_samplenum = start_samplenum; |
bc5f5a43 BV |
584 | logic->itercnt = 0; |
585 | logic->inbuf = inbuf; | |
586 | logic->inbuflen = inbuflen; | |
587 | logic->sample = PyList_New(2); | |
588 | Py_INCREF(logic->sample); | |
589 | ||
d906d3f9 | 590 | Py_IncRef(di->py_instance); |
f38ec285 | 591 | end_samplenum = start_samplenum + inbuflen / di->data_unitsize; |
d906d3f9 | 592 | if (!(py_res = PyObject_CallMethod(di->py_instance, "decode", |
86528298 | 593 | "KKO", logic->start_samplenum, end_samplenum, logic))) { |
2086c684 | 594 | catch_exception("Protocol decoder instance %s: ", di->instance_id); |
b2c19614 BV |
595 | return SRD_ERR_PYTHON; /* TODO: More specific error? */ |
596 | } | |
d906d3f9 | 597 | Py_DecRef(py_res); |
bc5f5a43 | 598 | |
b2c19614 BV |
599 | return SRD_OK; |
600 | } | |
601 | ||
fa12a21e BV |
602 | void srd_instance_free(struct srd_decoder_instance *di) |
603 | { | |
604 | GSList *l; | |
605 | struct srd_pd_output *pdo; | |
606 | ||
607 | srd_dbg("Freeing instance %s", di->instance_id); | |
608 | ||
609 | Py_DecRef(di->py_instance); | |
610 | g_free(di->instance_id); | |
611 | g_free(di->dec_probemap); | |
612 | g_slist_free(di->next_di); | |
613 | for (l = di->pd_output; l; l = l->next) { | |
614 | pdo = l->data; | |
615 | g_free(pdo->proto_id); | |
616 | g_free(pdo); | |
617 | } | |
618 | g_slist_free(di->pd_output); | |
619 | ||
620 | } | |
621 | ||
622 | void srd_instance_free_all(GSList *stack) | |
623 | { | |
624 | GSList *l; | |
625 | struct srd_decoder_instance *di; | |
626 | ||
627 | di = NULL; | |
628 | for (l = stack ? stack : di_list; di == NULL && l != NULL; l = l->next) { | |
629 | di = l->data; | |
630 | if (di->next_di) | |
631 | srd_instance_free_all(di->next_di); | |
632 | srd_instance_free(di); | |
633 | } | |
634 | if (!stack) { | |
635 | g_slist_free(di_list); | |
636 | di_list = NULL; | |
637 | } | |
638 | ||
639 | } | |
b2c19614 | 640 | |
7ce7775c BV |
641 | int srd_session_start(int num_probes, int unitsize, uint64_t samplerate) |
642 | { | |
643 | PyObject *args; | |
2072ae0c | 644 | GSList *d; |
7ce7775c BV |
645 | struct srd_decoder_instance *di; |
646 | int ret; | |
647 | ||
7a1712c4 UH |
648 | srd_dbg("Calling start() on all instances with %d probes, " |
649 | "unitsize %d samplerate %d.", num_probes, unitsize, samplerate); | |
d906d3f9 BV |
650 | |
651 | /* Currently only one item of metadata is passed along to decoders, | |
652 | * samplerate. This can be extended as needed. | |
653 | */ | |
7ce7775c | 654 | if (!(args = Py_BuildValue("{s:l}", "samplerate", (long)samplerate))) { |
d906d3f9 | 655 | srd_err("Unable to build python object for metadata."); |
7ce7775c BV |
656 | return SRD_ERR_PYTHON; |
657 | } | |
658 | ||
659 | /* Run the start() method on all decoders receiving frontend data. */ | |
660 | for (d = di_list; d; d = d->next) { | |
661 | di = d->data; | |
f38ec285 BV |
662 | di->data_num_probes = num_probes; |
663 | di->data_unitsize = unitsize; | |
664 | di->data_samplerate = samplerate; | |
7ce7775c | 665 | if ((ret = srd_instance_start(di, args) != SRD_OK)) |
2072ae0c | 666 | break; |
7ce7775c BV |
667 | } |
668 | ||
d906d3f9 | 669 | Py_DecRef(args); |
7ce7775c | 670 | |
2072ae0c | 671 | return ret; |
7ce7775c BV |
672 | } |
673 | ||
b2c19614 | 674 | /* Feed logic samples to decoder session. */ |
86528298 | 675 | int srd_session_feed(uint64_t start_samplenum, uint8_t *inbuf, uint64_t inbuflen) |
b2c19614 BV |
676 | { |
677 | GSList *d; | |
678 | int ret; | |
679 | ||
7a1712c4 UH |
680 | srd_dbg("Calling decode() on all instances with starting sample " |
681 | "number %"PRIu64", %"PRIu64" bytes at 0x%p", start_samplenum, | |
682 | inbuflen, inbuf); | |
d906d3f9 | 683 | |
e5080882 | 684 | for (d = di_list; d; d = d->next) { |
86528298 | 685 | if ((ret = srd_instance_decode(start_samplenum, d->data, inbuf, |
1aef2f93 | 686 | inbuflen)) != SRD_OK) |
b2c19614 BV |
687 | return ret; |
688 | } | |
689 | ||
690 | return SRD_OK; | |
691 | } | |
692 | ||
693 | ||
4fadb128 BV |
694 | int srd_register_callback(int output_type, void *cb) |
695 | { | |
696 | struct srd_pd_callback *pd_cb; | |
697 | ||
7a1712c4 | 698 | srd_dbg("Registering new callback for output type %d.", output_type); |
4fadb128 BV |
699 | |
700 | if (!(pd_cb = g_try_malloc(sizeof(struct srd_pd_callback)))) | |
701 | return SRD_ERR_MALLOC; | |
702 | ||
703 | pd_cb->output_type = output_type; | |
704 | pd_cb->callback = cb; | |
705 | callbacks = g_slist_append(callbacks, pd_cb); | |
706 | ||
707 | return SRD_OK; | |
708 | } | |
709 | ||
710 | void *srd_find_callback(int output_type) | |
711 | { | |
712 | GSList *l; | |
713 | struct srd_pd_callback *pd_cb; | |
714 | void *(cb); | |
715 | ||
716 | cb = NULL; | |
717 | for (l = callbacks; l; l = l->next) { | |
718 | pd_cb = l->data; | |
719 | if (pd_cb->output_type == output_type) { | |
720 | cb = pd_cb->callback; | |
721 | break; | |
722 | } | |
723 | } | |
724 | ||
725 | return cb; | |
726 | } | |
727 | ||
728 | ||
d906d3f9 | 729 | /* This is the backend function to python sigrokdecode.add() call. */ |
7a1712c4 | 730 | int pd_add(struct srd_decoder_instance *di, int output_type, char *proto_id) |
e5080882 | 731 | { |
e5080882 | 732 | struct srd_pd_output *pdo; |
e5080882 | 733 | |
7a1712c4 UH |
734 | srd_dbg("Instance %s creating new output type %d for %s.", |
735 | di->instance_id, output_type, proto_id); | |
d906d3f9 | 736 | |
e5080882 BV |
737 | if (!(pdo = g_try_malloc(sizeof(struct srd_pd_output)))) |
738 | return -1; | |
739 | ||
b231546d | 740 | /* pdo_id is just a simple index, nothing is deleted from this list anyway. */ |
15969949 | 741 | pdo->pdo_id = g_slist_length(di->pd_output); |
e5080882 | 742 | pdo->output_type = output_type; |
b3b2cae8 | 743 | pdo->di = di; |
94d43b37 | 744 | pdo->proto_id = g_strdup(proto_id); |
e5080882 BV |
745 | di->pd_output = g_slist_append(di->pd_output, pdo); |
746 | ||
15969949 | 747 | return pdo->pdo_id; |
e5080882 BV |
748 | } |
749 |