]>
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 | |
d906d3f9 | 28 | /* List of decoder instances. */ |
7ce7775c | 29 | static GSList *di_list = NULL; |
d906d3f9 BV |
30 | |
31 | /* List of frontend callbacks to receive PD output. */ | |
983cb0f5 BV |
32 | static GSList *callbacks = NULL; |
33 | ||
c9bfccc6 | 34 | /* decoder.c */ |
55c3c5f4 | 35 | extern SRD_PRIV GSList *pd_list; |
b2c19614 | 36 | |
c9bfccc6 | 37 | /* module_sigrokdecode.c */ |
55c3c5f4 | 38 | extern SRD_PRIV PyMODINIT_FUNC PyInit_sigrokdecode(void); |
b2c19614 | 39 | |
c9bfccc6 | 40 | /* type_logic.c */ |
55c3c5f4 | 41 | extern SRD_PRIV PyTypeObject srd_logic_type; |
b2c19614 BV |
42 | |
43 | /** | |
44 | * Initialize libsigrokdecode. | |
45 | * | |
46 | * This initializes the Python interpreter, and creates and initializes | |
47 | * a "sigrok" Python module with a single put() method. | |
48 | * | |
49 | * Then, it searches for sigrok protocol decoder files (*.py) in the | |
50 | * "decoders" subdirectory of the the sigrok installation directory. | |
51 | * All decoders that are found are loaded into memory and added to an | |
52 | * internal list of decoders, which can be queried via srd_list_decoders(). | |
53 | * | |
54 | * The caller is responsible for calling the clean-up function srd_exit(), | |
55 | * which will properly shut down libsigrokdecode and free its allocated memory. | |
56 | * | |
7ce7775c | 57 | * Multiple calls to srd_init(), without calling srd_exit() in between, |
b2c19614 BV |
58 | * are not allowed. |
59 | * | |
60 | * @return SRD_OK upon success, a (negative) error code otherwise. | |
61 | * Upon Python errors, return SRD_ERR_PYTHON. If the sigrok decoders | |
62 | * directory cannot be accessed, return SRD_ERR_DECODERS_DIR. | |
63 | * If not enough memory could be allocated, return SRD_ERR_MALLOC. | |
64 | */ | |
55c3c5f4 | 65 | SRD_API int srd_init(void) |
b2c19614 BV |
66 | { |
67 | int ret; | |
68 | ||
7a1712c4 | 69 | srd_dbg("Initializing libsigrokdecode."); |
d906d3f9 BV |
70 | |
71 | /* Add our own module to the list of built-in modules. */ | |
bc5f5a43 | 72 | PyImport_AppendInittab("sigrokdecode", PyInit_sigrokdecode); |
5f802ec6 | 73 | |
511e2123 | 74 | /* Initialize the Python interpreter. */ |
b2c19614 BV |
75 | Py_Initialize(); |
76 | ||
b2c19614 BV |
77 | if ((ret = set_modulepath()) != SRD_OK) { |
78 | Py_Finalize(); | |
79 | return ret; | |
80 | } | |
81 | ||
82 | if ((ret = srd_load_all_decoders()) != SRD_OK) { | |
83 | Py_Finalize(); | |
84 | return ret; | |
85 | } | |
86 | ||
87 | return SRD_OK; | |
88 | } | |
89 | ||
b2c19614 BV |
90 | /** |
91 | * Shutdown libsigrokdecode. | |
92 | * | |
93 | * This frees all the memory allocated for protocol decoders and shuts down | |
94 | * the Python interpreter. | |
95 | * | |
96 | * This function should only be called if there was a (successful!) invocation | |
97 | * of srd_init() before. Calling this function multiple times in a row, without | |
7ce7775c | 98 | * any successful srd_init() calls in between, is not allowed. |
b2c19614 BV |
99 | * |
100 | * @return SRD_OK upon success, a (negative) error code otherwise. | |
101 | */ | |
55c3c5f4 | 102 | SRD_API int srd_exit(void) |
b2c19614 | 103 | { |
7a1712c4 | 104 | srd_dbg("Exiting libsigrokdecode."); |
d906d3f9 | 105 | |
b2c19614 | 106 | srd_unload_all_decoders(); |
e5080882 | 107 | g_slist_free(pd_list); |
b2c19614 BV |
108 | |
109 | /* Py_Finalize() returns void, any finalization errors are ignored. */ | |
110 | Py_Finalize(); | |
111 | ||
112 | return SRD_OK; | |
113 | } | |
114 | ||
b2c19614 | 115 | /** |
9d9fcb37 UH |
116 | * Add an additional search directory for the protocol decoders. |
117 | * | |
118 | * The specified directory is prepended (not appended!) to Python's sys.path, | |
119 | * in order to search for sigrok protocol decoders in the specified | |
120 | * directories first, and in the generic Python module directories (and in | |
121 | * the current working directory) last. This avoids conflicts if there are | |
122 | * Python modules which have the same name as a sigrok protocol decoder in | |
123 | * sys.path or in the current working directory. | |
b2c19614 BV |
124 | * |
125 | * TODO: add path from env var SIGROKDECODE_PATH, config etc | |
639c5689 | 126 | * TODO: Should take directoryname/path as input. |
9d9fcb37 UH |
127 | * |
128 | * @return TODO. | |
b2c19614 | 129 | */ |
55c3c5f4 | 130 | SRD_API int set_modulepath(void) |
b2c19614 BV |
131 | { |
132 | int ret; | |
639c5689 | 133 | gchar *path, *s; |
b2c19614 | 134 | |
639c5689 UH |
135 | #ifdef _WIN32 |
136 | gchar **splitted; | |
137 | ||
138 | /* | |
139 | * On Windows/MinGW, Python's sys.path needs entries of the form | |
140 | * 'C:\\foo\\bar' instead of '/foo/bar'. | |
141 | */ | |
142 | ||
143 | splitted = g_strsplit(DECODERS_DIR, "/", 0); | |
144 | path = g_build_pathv("\\\\", splitted); | |
145 | g_strfreev(splitted); | |
146 | #else | |
147 | path = g_strdup(DECODERS_DIR); | |
148 | #endif | |
149 | ||
639c5689 | 150 | /* TODO: Sanity check on 'path' (length, escape special chars, ...). */ |
9d9fcb37 | 151 | s = g_strdup_printf("import sys; sys.path.insert(0, r'%s')", path); |
639c5689 UH |
152 | |
153 | ret = PyRun_SimpleString(s); | |
154 | ||
155 | g_free(path); | |
156 | g_free(s); | |
b2c19614 BV |
157 | |
158 | return ret; | |
159 | } | |
160 | ||
7ce7775c | 161 | /** |
0bdadba2 BV |
162 | * Set options in a decoder instance. |
163 | * | |
164 | * @param di Decoder instance. | |
165 | * @param options A GHashTable of options to set. | |
7ce7775c | 166 | * |
0bdadba2 BV |
167 | * Handled options are removed from the hash. |
168 | * | |
169 | * @return SRD_OK upon success, a (negative) error code otherwise. | |
170 | */ | |
a8b72b05 | 171 | SRD_API int srd_inst_set_options(struct srd_decoder_inst *di, |
55c3c5f4 | 172 | GHashTable *options) |
0bdadba2 BV |
173 | { |
174 | PyObject *py_dec_options, *py_dec_optkeys, *py_di_options, *py_optval; | |
175 | PyObject *py_optlist, *py_classval; | |
176 | Py_UNICODE *py_ustr; | |
177 | unsigned long long int val_ull; | |
178 | int num_optkeys, ret, size, i; | |
179 | char *key, *value; | |
180 | ||
c9bfccc6 | 181 | if (!PyObject_HasAttrString(di->decoder->py_dec, "options")) { |
0bdadba2 | 182 | /* Decoder has no options. */ |
e431d9cc BV |
183 | if (g_hash_table_size(options) == 0) { |
184 | /* No options provided. */ | |
185 | return SRD_OK; | |
186 | } else { | |
187 | srd_err("Protocol decoder has no options."); | |
188 | return SRD_ERR_ARG; | |
189 | } | |
0bdadba2 | 190 | return SRD_OK; |
e431d9cc | 191 | } |
0bdadba2 BV |
192 | |
193 | ret = SRD_ERR_PYTHON; | |
194 | key = NULL; | |
195 | py_dec_options = py_dec_optkeys = py_di_options = py_optval = NULL; | |
196 | py_optlist = py_classval = NULL; | |
197 | py_dec_options = PyObject_GetAttrString(di->decoder->py_dec, "options"); | |
0bdadba2 BV |
198 | |
199 | /* All of these are synthesized objects, so they're good. */ | |
200 | py_dec_optkeys = PyDict_Keys(py_dec_options); | |
201 | num_optkeys = PyList_Size(py_dec_optkeys); | |
a8b72b05 | 202 | if (!(py_di_options = PyObject_GetAttrString(di->py_inst, "options"))) |
0bdadba2 BV |
203 | goto err_out; |
204 | for (i = 0; i < num_optkeys; i++) { | |
205 | /* Get the default class value for this option. */ | |
206 | py_str_as_str(PyList_GetItem(py_dec_optkeys, i), &key); | |
207 | if (!(py_optlist = PyDict_GetItemString(py_dec_options, key))) | |
208 | goto err_out; | |
209 | if (!(py_classval = PyList_GetItem(py_optlist, 1))) | |
210 | goto err_out; | |
130ef08a | 211 | if (!PyUnicode_Check(py_classval) && !PyLong_Check(py_classval)) { |
c9bfccc6 UH |
212 | srd_err("Options of type %s are not yet supported.", |
213 | Py_TYPE(py_classval)->tp_name); | |
130ef08a BV |
214 | goto err_out; |
215 | } | |
0bdadba2 BV |
216 | |
217 | if ((value = g_hash_table_lookup(options, key))) { | |
218 | /* An override for this option was provided. */ | |
219 | if (PyUnicode_Check(py_classval)) { | |
220 | if (!(py_optval = PyUnicode_FromString(value))) { | |
221 | /* Some UTF-8 encoding error. */ | |
222 | PyErr_Clear(); | |
223 | goto err_out; | |
224 | } | |
225 | } else if (PyLong_Check(py_classval)) { | |
226 | if (!(py_optval = PyLong_FromString(value, NULL, 0))) { | |
227 | /* ValueError Exception */ | |
228 | PyErr_Clear(); | |
c9bfccc6 UH |
229 | srd_err("Option %s has invalid value " |
230 | "%s: expected integer.", | |
231 | key, value); | |
0bdadba2 BV |
232 | goto err_out; |
233 | } | |
234 | } | |
235 | g_hash_table_remove(options, key); | |
236 | } else { | |
237 | /* Use the class default for this option. */ | |
238 | if (PyUnicode_Check(py_classval)) { | |
239 | /* Make a brand new copy of the string. */ | |
240 | py_ustr = PyUnicode_AS_UNICODE(py_classval); | |
241 | size = PyUnicode_GET_SIZE(py_classval); | |
242 | py_optval = PyUnicode_FromUnicode(py_ustr, size); | |
243 | } else if (PyLong_Check(py_classval)) { | |
244 | /* Make a brand new copy of the integer. */ | |
245 | val_ull = PyLong_AsUnsignedLongLong(py_classval); | |
246 | if (val_ull == (unsigned long long)-1) { | |
247 | /* OverFlowError exception */ | |
248 | PyErr_Clear(); | |
c9bfccc6 UH |
249 | srd_err("Invalid integer value for %s: " |
250 | "expected integer.", key); | |
0bdadba2 BV |
251 | goto err_out; |
252 | } | |
253 | if (!(py_optval = PyLong_FromUnsignedLongLong(val_ull))) | |
254 | goto err_out; | |
255 | } | |
256 | } | |
257 | ||
258 | /* If we got here, py_optval holds a known good new reference | |
259 | * to the instance option to set. | |
260 | */ | |
261 | if (PyDict_SetItemString(py_di_options, key, py_optval) == -1) | |
262 | goto err_out; | |
263 | } | |
264 | ||
265 | ret = SRD_OK; | |
266 | ||
267 | err_out: | |
268 | Py_XDECREF(py_optlist); | |
269 | Py_XDECREF(py_di_options); | |
270 | Py_XDECREF(py_dec_optkeys); | |
271 | Py_XDECREF(py_dec_options); | |
6f858319 | 272 | g_free(key); |
2086c684 | 273 | if (PyErr_Occurred()) |
a8b72b05 | 274 | catch_exception("Stray exception in srd_inst_set_options()."); |
0bdadba2 BV |
275 | |
276 | return ret; | |
277 | } | |
278 | ||
a8b72b05 | 279 | /* Helper GComparefunc for g_slist_find_custom() in srd_inst_set_probes() */ |
f38ec285 BV |
280 | static gint compare_probe_id(struct srd_probe *a, char *probe_id) |
281 | { | |
f38ec285 BV |
282 | return strcmp(a->id, probe_id); |
283 | } | |
284 | ||
0bdadba2 BV |
285 | /** |
286 | * Set probes in a decoder instance. | |
287 | * | |
288 | * @param di Decoder instance. | |
289 | * @param probes A GHashTable of probes to set. Key is probe name, value is | |
c9bfccc6 UH |
290 | * the probe number. Samples passed to this instance will be |
291 | * arranged in this order. | |
0bdadba2 BV |
292 | * @return SRD_OK upon success, a (negative) error code otherwise. |
293 | */ | |
a8b72b05 | 294 | SRD_API int srd_inst_set_probes(struct srd_decoder_inst *di, |
55c3c5f4 | 295 | GHashTable *new_probes) |
0bdadba2 | 296 | { |
f38ec285 BV |
297 | GList *l; |
298 | GSList *sl; | |
299 | struct srd_probe *p; | |
300 | int *new_probemap, new_probenum; | |
be873260 | 301 | char *probe_id, *probenum_str; |
0bdadba2 | 302 | |
ce46beed | 303 | srd_dbg("set probes called for instance %s with list of %d probes", |
a8b72b05 | 304 | di->inst_id, g_hash_table_size(new_probes)); |
ce46beed | 305 | |
f38ec285 | 306 | if (g_hash_table_size(new_probes) == 0) |
0bdadba2 BV |
307 | /* No probes provided. */ |
308 | return SRD_OK; | |
309 | ||
c9bfccc6 | 310 | if (di->dec_num_probes == 0) { |
0bdadba2 | 311 | /* Decoder has no probes. */ |
f38ec285 | 312 | srd_err("Protocol decoder %s has no probes to define.", |
c9bfccc6 | 313 | di->decoder->name); |
f38ec285 BV |
314 | return SRD_ERR_ARG; |
315 | } | |
0bdadba2 | 316 | |
f38ec285 | 317 | new_probemap = NULL; |
0bdadba2 | 318 | |
f38ec285 | 319 | if (!(new_probemap = g_try_malloc(sizeof(int) * di->dec_num_probes))) { |
a61ece20 | 320 | srd_err("Failed to g_malloc() new probe map."); |
f38ec285 | 321 | return SRD_ERR_MALLOC; |
0bdadba2 BV |
322 | } |
323 | ||
f38ec285 BV |
324 | for (l = g_hash_table_get_keys(new_probes); l; l = l->next) { |
325 | probe_id = l->data; | |
be873260 BV |
326 | probenum_str = g_hash_table_lookup(new_probes, probe_id); |
327 | if (!probenum_str) { | |
328 | /* Probe name was specified without a value. */ | |
c9bfccc6 UH |
329 | srd_err("No probe number was specified for %s.", |
330 | probe_id); | |
be873260 BV |
331 | g_free(new_probemap); |
332 | return SRD_ERR_ARG; | |
333 | } | |
334 | new_probenum = strtol(probenum_str, NULL, 10); | |
f38ec285 BV |
335 | if (!(sl = g_slist_find_custom(di->decoder->probes, probe_id, |
336 | (GCompareFunc)compare_probe_id))) { | |
337 | /* Fall back on optional probes. */ | |
dcdf4883 | 338 | if (!(sl = g_slist_find_custom(di->decoder->opt_probes, |
c9bfccc6 UH |
339 | probe_id, (GCompareFunc) compare_probe_id))) { |
340 | srd_err("Protocol decoder %s has no probe " | |
341 | "'%s'.", di->decoder->name, probe_id); | |
f38ec285 BV |
342 | g_free(new_probemap); |
343 | return SRD_ERR_ARG; | |
344 | } | |
345 | } | |
346 | p = sl->data; | |
347 | new_probemap[p->order] = new_probenum; | |
c9bfccc6 UH |
348 | srd_dbg("setting probe mapping for %d = probe %d", p->order, |
349 | new_probenum); | |
f38ec285 BV |
350 | } |
351 | g_free(di->dec_probemap); | |
352 | di->dec_probemap = new_probemap; | |
0bdadba2 | 353 | |
f38ec285 | 354 | return SRD_OK; |
0bdadba2 BV |
355 | } |
356 | ||
357 | /** | |
358 | * Create a new protocol decoder instance. | |
7ce7775c | 359 | * |
42a85ed0 | 360 | * @param id Decoder 'id' field. |
0bdadba2 | 361 | * @param options GHashtable of options which override the defaults set in |
c9bfccc6 | 362 | * the decoder class. |
a8b72b05 | 363 | * @return Pointer to a newly allocated struct srd_decoder_inst, or |
c9bfccc6 | 364 | * NULL in case of failure. |
7ce7775c | 365 | */ |
a8b72b05 | 366 | SRD_API struct srd_decoder_inst *srd_inst_new(const char *decoder_id, |
55c3c5f4 | 367 | GHashTable *options) |
b2c19614 | 368 | { |
c9bfccc6 | 369 | int i; |
b2c19614 | 370 | struct srd_decoder *dec; |
a8b72b05 BV |
371 | struct srd_decoder_inst *di; |
372 | char *inst_id; | |
b2c19614 | 373 | |
7a1712c4 | 374 | srd_dbg("Creating new %s instance.", decoder_id); |
7ce7775c | 375 | |
0bdadba2 BV |
376 | if (!(dec = srd_get_decoder_by_id(decoder_id))) { |
377 | srd_err("Protocol decoder %s not found.", decoder_id); | |
b2c19614 | 378 | return NULL; |
0bdadba2 | 379 | } |
b2c19614 | 380 | |
a8b72b05 | 381 | if (!(di = g_try_malloc0(sizeof(struct srd_decoder_inst)))) { |
a61ece20 | 382 | srd_err("Failed to g_malloc() instance."); |
7ce7775c BV |
383 | return NULL; |
384 | } | |
0bdadba2 | 385 | |
a8b72b05 | 386 | inst_id = g_hash_table_lookup(options, "id"); |
b2c19614 | 387 | di->decoder = dec; |
a8b72b05 | 388 | di->inst_id = g_strdup(inst_id ? inst_id : decoder_id); |
0bdadba2 | 389 | g_hash_table_remove(options, "id"); |
b2c19614 | 390 | |
f38ec285 BV |
391 | /* Prepare a default probe map, where samples come in the |
392 | * order in which the decoder class defined them. | |
393 | */ | |
394 | di->dec_num_probes = g_slist_length(di->decoder->probes) + | |
c9bfccc6 | 395 | g_slist_length(di->decoder->opt_probes); |
19a90bab | 396 | if (di->dec_num_probes) { |
c9bfccc6 UH |
397 | if (!(di->dec_probemap = |
398 | g_try_malloc(sizeof(int) * di->dec_num_probes))) { | |
a61ece20 | 399 | srd_err("Failed to g_malloc() probe map."); |
19a90bab BV |
400 | g_free(di); |
401 | return NULL; | |
402 | } | |
403 | for (i = 0; i < di->dec_num_probes; i++) | |
404 | di->dec_probemap[i] = i; | |
f38ec285 | 405 | } |
f38ec285 | 406 | |
0bdadba2 | 407 | /* Create a new instance of this decoder class. */ |
a8b72b05 | 408 | if (!(di->py_inst = PyObject_CallObject(dec->py_dec, NULL))) { |
b2c19614 | 409 | if (PyErr_Occurred()) |
c9bfccc6 UH |
410 | catch_exception("failed to create %s instance: ", |
411 | decoder_id); | |
f38ec285 | 412 | g_free(di->dec_probemap); |
0bdadba2 | 413 | g_free(di); |
7ce7775c | 414 | return NULL; |
b2c19614 | 415 | } |
7ce7775c | 416 | |
a8b72b05 | 417 | if (srd_inst_set_options(di, options) != SRD_OK) { |
f38ec285 | 418 | g_free(di->dec_probemap); |
0bdadba2 BV |
419 | g_free(di); |
420 | return NULL; | |
421 | } | |
b2c19614 | 422 | |
f38ec285 BV |
423 | /* Instance takes input from a frontend by default. */ |
424 | di_list = g_slist_append(di_list, di); | |
425 | ||
b2c19614 BV |
426 | return di; |
427 | } | |
428 | ||
a8b72b05 BV |
429 | SRD_API int srd_inst_stack(struct srd_decoder_inst *di_from, |
430 | struct srd_decoder_inst *di_to) | |
7ce7775c | 431 | { |
7ce7775c | 432 | if (!di_from || !di_to) { |
d906d3f9 | 433 | srd_err("Invalid from/to instance pair."); |
7ce7775c BV |
434 | return SRD_ERR_ARG; |
435 | } | |
436 | ||
2072ae0c BV |
437 | if (g_slist_find(di_list, di_to)) { |
438 | /* Remove from the unstacked list. */ | |
439 | di_list = g_slist_remove(di_list, di_to); | |
7ce7775c BV |
440 | } |
441 | ||
7ce7775c BV |
442 | /* Stack on top of source di. */ |
443 | di_from->next_di = g_slist_append(di_from->next_di, di_to); | |
444 | ||
445 | return SRD_OK; | |
446 | } | |
447 | ||
2072ae0c BV |
448 | /** |
449 | * Finds a decoder instance by its instance id, but only in the bottom | |
450 | * level of instances -- instances already stacked on top of another one | |
451 | * will not be found. | |
452 | * | |
a8b72b05 | 453 | * @param inst_id The instance id to be found. |
2072ae0c | 454 | * |
a8b72b05 | 455 | * @return Pointer to struct srd_decoder_inst, or NULL if not found. |
2072ae0c | 456 | */ |
a8b72b05 | 457 | SRD_API struct srd_decoder_inst *srd_inst_find_by_id(char *inst_id) |
7ce7775c BV |
458 | { |
459 | GSList *l; | |
a8b72b05 | 460 | struct srd_decoder_inst *tmp, *di; |
b2c19614 | 461 | |
7ce7775c BV |
462 | di = NULL; |
463 | for (l = di_list; l; l = l->next) { | |
464 | tmp = l->data; | |
a8b72b05 | 465 | if (!strcmp(tmp->inst_id, inst_id)) { |
7ce7775c BV |
466 | di = tmp; |
467 | break; | |
468 | } | |
469 | } | |
470 | ||
471 | return di; | |
472 | } | |
473 | ||
2072ae0c | 474 | /** |
511e2123 | 475 | * Finds a decoder instance by its Python object, i.e. that instance's |
2072ae0c BV |
476 | * instantiation of the sigrokdecode.Decoder class. This will recurse |
477 | * to find the instance anywhere in the stack tree. | |
478 | * | |
a8b72b05 | 479 | * @param stack Pointer to a GSList of struct srd_decoder_inst, |
2072ae0c BV |
480 | * indicating the stack to search. To start searching at the bottom |
481 | * level of decoder instances, pass NULL. | |
511e2123 | 482 | * @param obj The Python class instantiation. |
2072ae0c | 483 | * |
a8b72b05 | 484 | * @return Pointer to struct srd_decoder_inst, or NULL if not found. |
2072ae0c | 485 | */ |
a8b72b05 | 486 | SRD_API struct srd_decoder_inst *srd_inst_find_by_obj(GSList *stack, |
55c3c5f4 | 487 | PyObject *obj) |
2072ae0c BV |
488 | { |
489 | GSList *l; | |
a8b72b05 | 490 | struct srd_decoder_inst *tmp, *di; |
2072ae0c BV |
491 | |
492 | di = NULL; | |
493 | for (l = stack ? stack : di_list; di == NULL && l != NULL; l = l->next) { | |
494 | tmp = l->data; | |
a8b72b05 | 495 | if (tmp->py_inst == obj) |
2072ae0c BV |
496 | di = tmp; |
497 | else if (tmp->next_di) | |
a8b72b05 | 498 | di = srd_inst_find_by_obj(tmp->next_di, obj); |
2072ae0c BV |
499 | } |
500 | ||
501 | return di; | |
502 | } | |
503 | ||
a8b72b05 | 504 | SRD_API int srd_inst_start(struct srd_decoder_inst *di, PyObject *args) |
b2c19614 | 505 | { |
7ce7775c | 506 | PyObject *py_name, *py_res; |
2072ae0c | 507 | GSList *l; |
a8b72b05 | 508 | struct srd_decoder_inst *next_di; |
b2c19614 | 509 | |
7a1712c4 | 510 | srd_dbg("Calling start() method on protocol decoder instance %s.", |
a8b72b05 | 511 | di->inst_id); |
b2c19614 | 512 | |
7ce7775c | 513 | if (!(py_name = PyUnicode_FromString("start"))) { |
511e2123 | 514 | srd_err("Unable to build Python object for 'start'."); |
c9bfccc6 | 515 | catch_exception("Protocol decoder instance %s: ", |
a8b72b05 | 516 | di->inst_id); |
7ce7775c BV |
517 | return SRD_ERR_PYTHON; |
518 | } | |
519 | ||
a8b72b05 | 520 | if (!(py_res = PyObject_CallMethodObjArgs(di->py_inst, |
c9bfccc6 UH |
521 | py_name, args, NULL))) { |
522 | catch_exception("Protocol decoder instance %s: ", | |
a8b72b05 | 523 | di->inst_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; | |
a8b72b05 | 536 | srd_inst_start(next_di, args); |
2072ae0c BV |
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 | */ | |
a8b72b05 BV |
553 | SRD_API int srd_inst_decode(uint64_t start_samplenum, |
554 | struct srd_decoder_inst *di, | |
55c3c5f4 | 555 | uint8_t *inbuf, uint64_t inbuflen) |
b2c19614 | 556 | { |
d906d3f9 | 557 | PyObject *py_res; |
bc5f5a43 | 558 | srd_logic *logic; |
86528298 | 559 | uint64_t end_samplenum; |
b2c19614 | 560 | |
7a1712c4 | 561 | srd_dbg("Calling decode() on instance %s with %d bytes starting " |
a8b72b05 | 562 | "at sample %d.", di->inst_id, inbuflen, start_samplenum); |
b2c19614 | 563 | |
d906d3f9 | 564 | /* Return an error upon unusable input. */ |
7a1712c4 UH |
565 | if (!di) { |
566 | srd_dbg("empty decoder instance"); | |
d906d3f9 BV |
567 | return SRD_ERR_ARG; |
568 | } | |
7a1712c4 UH |
569 | if (!inbuf) { |
570 | srd_dbg("NULL buffer pointer"); | |
d906d3f9 BV |
571 | return SRD_ERR_ARG; |
572 | } | |
573 | if (inbuflen == 0) { | |
7a1712c4 | 574 | srd_dbg("empty buffer"); |
d906d3f9 BV |
575 | return SRD_ERR_ARG; |
576 | } | |
b2c19614 | 577 | |
d906d3f9 BV |
578 | /* Create new srd_logic object. Each iteration around the PD's loop |
579 | * will fill one sample into this object. | |
580 | */ | |
bc5f5a43 BV |
581 | logic = PyObject_New(srd_logic, &srd_logic_type); |
582 | Py_INCREF(logic); | |
583 | logic->di = di; | |
86528298 | 584 | logic->start_samplenum = start_samplenum; |
bc5f5a43 BV |
585 | logic->itercnt = 0; |
586 | logic->inbuf = inbuf; | |
587 | logic->inbuflen = inbuflen; | |
588 | logic->sample = PyList_New(2); | |
589 | Py_INCREF(logic->sample); | |
590 | ||
a8b72b05 | 591 | Py_IncRef(di->py_inst); |
f38ec285 | 592 | end_samplenum = start_samplenum + inbuflen / di->data_unitsize; |
a8b72b05 | 593 | if (!(py_res = PyObject_CallMethod(di->py_inst, "decode", |
c9bfccc6 UH |
594 | "KKO", logic->start_samplenum, |
595 | end_samplenum, logic))) { | |
596 | catch_exception("Protocol decoder instance %s: ", | |
a8b72b05 | 597 | di->inst_id); |
b2c19614 BV |
598 | return SRD_ERR_PYTHON; /* TODO: More specific error? */ |
599 | } | |
d906d3f9 | 600 | Py_DecRef(py_res); |
bc5f5a43 | 601 | |
b2c19614 BV |
602 | return SRD_OK; |
603 | } | |
604 | ||
a8b72b05 | 605 | SRD_API void srd_inst_free(struct srd_decoder_inst *di) |
fa12a21e BV |
606 | { |
607 | GSList *l; | |
608 | struct srd_pd_output *pdo; | |
609 | ||
a8b72b05 | 610 | srd_dbg("Freeing instance %s", di->inst_id); |
fa12a21e | 611 | |
a8b72b05 BV |
612 | Py_DecRef(di->py_inst); |
613 | g_free(di->inst_id); | |
fa12a21e BV |
614 | g_free(di->dec_probemap); |
615 | g_slist_free(di->next_di); | |
616 | for (l = di->pd_output; l; l = l->next) { | |
617 | pdo = l->data; | |
618 | g_free(pdo->proto_id); | |
619 | g_free(pdo); | |
620 | } | |
621 | g_slist_free(di->pd_output); | |
fa12a21e BV |
622 | } |
623 | ||
a8b72b05 | 624 | SRD_API void srd_inst_free_all(GSList *stack) |
fa12a21e BV |
625 | { |
626 | GSList *l; | |
a8b72b05 | 627 | struct srd_decoder_inst *di; |
fa12a21e BV |
628 | |
629 | di = NULL; | |
630 | for (l = stack ? stack : di_list; di == NULL && l != NULL; l = l->next) { | |
631 | di = l->data; | |
632 | if (di->next_di) | |
a8b72b05 BV |
633 | srd_inst_free_all(di->next_di); |
634 | srd_inst_free(di); | |
fa12a21e BV |
635 | } |
636 | if (!stack) { | |
637 | g_slist_free(di_list); | |
638 | di_list = NULL; | |
639 | } | |
fa12a21e | 640 | } |
b2c19614 | 641 | |
55c3c5f4 | 642 | SRD_API int srd_session_start(int num_probes, int unitsize, uint64_t samplerate) |
7ce7775c BV |
643 | { |
644 | PyObject *args; | |
2072ae0c | 645 | GSList *d; |
a8b72b05 | 646 | struct srd_decoder_inst *di; |
7ce7775c BV |
647 | int ret; |
648 | ||
7a1712c4 UH |
649 | srd_dbg("Calling start() on all instances with %d probes, " |
650 | "unitsize %d samplerate %d.", num_probes, unitsize, samplerate); | |
d906d3f9 BV |
651 | |
652 | /* Currently only one item of metadata is passed along to decoders, | |
653 | * samplerate. This can be extended as needed. | |
654 | */ | |
7ce7775c | 655 | if (!(args = Py_BuildValue("{s:l}", "samplerate", (long)samplerate))) { |
511e2123 | 656 | srd_err("Unable to build Python object for metadata."); |
7ce7775c BV |
657 | return SRD_ERR_PYTHON; |
658 | } | |
659 | ||
660 | /* Run the start() method on all decoders receiving frontend data. */ | |
661 | for (d = di_list; d; d = d->next) { | |
662 | di = d->data; | |
f38ec285 BV |
663 | di->data_num_probes = num_probes; |
664 | di->data_unitsize = unitsize; | |
665 | di->data_samplerate = samplerate; | |
a8b72b05 | 666 | if ((ret = srd_inst_start(di, args) != SRD_OK)) |
2072ae0c | 667 | break; |
7ce7775c BV |
668 | } |
669 | ||
d906d3f9 | 670 | Py_DecRef(args); |
7ce7775c | 671 | |
2072ae0c | 672 | return ret; |
7ce7775c BV |
673 | } |
674 | ||
b2c19614 | 675 | /* Feed logic samples to decoder session. */ |
55c3c5f4 UH |
676 | SRD_API int srd_session_feed(uint64_t start_samplenum, uint8_t * inbuf, |
677 | uint64_t inbuflen) | |
b2c19614 BV |
678 | { |
679 | GSList *d; | |
680 | int ret; | |
681 | ||
7a1712c4 | 682 | srd_dbg("Calling decode() on all instances with starting sample " |
c9bfccc6 UH |
683 | "number %" PRIu64 ", %" PRIu64 " bytes at 0x%p", |
684 | start_samplenum, inbuflen, inbuf); | |
d906d3f9 | 685 | |
e5080882 | 686 | for (d = di_list; d; d = d->next) { |
a8b72b05 | 687 | if ((ret = srd_inst_decode(start_samplenum, d->data, inbuf, |
c9bfccc6 | 688 | inbuflen)) != SRD_OK) |
b2c19614 BV |
689 | return ret; |
690 | } | |
691 | ||
692 | return SRD_OK; | |
693 | } | |
694 | ||
55c3c5f4 | 695 | SRD_API int srd_register_callback(int output_type, srd_pd_output_callback_t cb) |
4fadb128 BV |
696 | { |
697 | struct srd_pd_callback *pd_cb; | |
698 | ||
7a1712c4 | 699 | srd_dbg("Registering new callback for output type %d.", output_type); |
4fadb128 | 700 | |
a61ece20 UH |
701 | if (!(pd_cb = g_try_malloc(sizeof(struct srd_pd_callback)))) { |
702 | srd_err("Failed to g_malloc() struct srd_pd_callback."); | |
4fadb128 | 703 | return SRD_ERR_MALLOC; |
a61ece20 | 704 | } |
4fadb128 BV |
705 | |
706 | pd_cb->output_type = output_type; | |
707 | pd_cb->callback = cb; | |
708 | callbacks = g_slist_append(callbacks, pd_cb); | |
709 | ||
710 | return SRD_OK; | |
711 | } | |
712 | ||
55c3c5f4 | 713 | SRD_API void *srd_find_callback(int output_type) |
4fadb128 BV |
714 | { |
715 | GSList *l; | |
716 | struct srd_pd_callback *pd_cb; | |
717 | void *(cb); | |
718 | ||
719 | cb = NULL; | |
720 | for (l = callbacks; l; l = l->next) { | |
721 | pd_cb = l->data; | |
722 | if (pd_cb->output_type == output_type) { | |
723 | cb = pd_cb->callback; | |
724 | break; | |
725 | } | |
726 | } | |
727 | ||
728 | return cb; | |
729 | } | |
730 | ||
511e2123 | 731 | /* This is the backend function to Python sigrokdecode.add() call. */ |
a8b72b05 | 732 | SRD_PRIV int pd_add(struct srd_decoder_inst *di, int output_type, |
55c3c5f4 | 733 | char *proto_id) |
e5080882 | 734 | { |
e5080882 | 735 | struct srd_pd_output *pdo; |
e5080882 | 736 | |
7a1712c4 | 737 | srd_dbg("Instance %s creating new output type %d for %s.", |
a8b72b05 | 738 | di->inst_id, output_type, proto_id); |
d906d3f9 | 739 | |
a61ece20 UH |
740 | if (!(pdo = g_try_malloc(sizeof(struct srd_pd_output)))) { |
741 | srd_err("Failed to g_malloc() struct srd_pd_output."); | |
e5080882 | 742 | return -1; |
a61ece20 | 743 | } |
e5080882 | 744 | |
b231546d | 745 | /* pdo_id is just a simple index, nothing is deleted from this list anyway. */ |
15969949 | 746 | pdo->pdo_id = g_slist_length(di->pd_output); |
e5080882 | 747 | pdo->output_type = output_type; |
b3b2cae8 | 748 | pdo->di = di; |
94d43b37 | 749 | pdo->proto_id = g_strdup(proto_id); |
e5080882 BV |
750 | di->pd_output = g_slist_append(di->pd_output, pdo); |
751 | ||
15969949 | 752 | return pdo->pdo_id; |
e5080882 | 753 | } |