29 static GSList *di_list = NULL;
32 static GSList *callbacks = NULL;
74 srd_dbg(
"Initializing libsigrokdecode.");
97 if ((env_path = getenv(
"SIGROKDECODE_DIR"))) {
121 srd_dbg(
"Exiting libsigrokdecode.");
150 PyObject *py_cur_path, *py_item;
153 wchar_t *wc_new_path;
156 srd_dbg(
"Adding '%s' to module path.", path);
158 new_path = g_string_sized_new(256);
159 g_string_assign(new_path, g_strdup(path));
160 py_cur_path = PySys_GetObject(
"path");
161 for (i = 0; i < PyList_Size(py_cur_path); i++) {
162 g_string_append(new_path, g_strdup(G_SEARCHPATH_SEPARATOR_S));
163 py_item = PyList_GetItem(py_cur_path, i);
164 if (!PyUnicode_Check(py_item))
169 g_string_append(new_path, item);
173 wc_len =
sizeof(wchar_t) * (new_path->len + 1);
174 if (!(wc_new_path = g_try_malloc(wc_len))) {
178 mbstowcs(wc_new_path, new_path->str, wc_len);
179 PySys_SetPath(wc_new_path);
180 g_string_free(new_path, TRUE);
214 PyObject *py_dec_options, *py_dec_optkeys, *py_di_options, *py_optval;
215 PyObject *py_optlist, *py_classval;
217 unsigned long long int val_ull;
218 int num_optkeys, ret, size, i;
221 if (!PyObject_HasAttrString(di->
decoder->
py_dec,
"options")) {
223 if (g_hash_table_size(options) == 0) {
227 srd_err(
"Protocol decoder has no options.");
235 py_dec_options = py_dec_optkeys = py_di_options = py_optval = NULL;
236 py_optlist = py_classval = NULL;
237 py_dec_options = PyObject_GetAttrString(di->
decoder->
py_dec,
"options");
240 py_dec_optkeys = PyDict_Keys(py_dec_options);
241 num_optkeys = PyList_Size(py_dec_optkeys);
242 if (!(py_di_options = PyObject_GetAttrString(di->
py_inst,
"options")))
244 for (i = 0; i < num_optkeys; i++) {
247 if (!(py_optlist = PyDict_GetItemString(py_dec_options, key)))
249 if (!(py_classval = PyList_GetItem(py_optlist, 1)))
251 if (!PyUnicode_Check(py_classval) && !PyLong_Check(py_classval)) {
252 srd_err(
"Options of type %s are not yet supported.",
253 Py_TYPE(py_classval)->tp_name);
257 if ((value = g_hash_table_lookup(options, key))) {
259 if (PyUnicode_Check(py_classval)) {
260 if (!(py_optval = PyUnicode_FromString(value))) {
265 }
else if (PyLong_Check(py_classval)) {
266 if (!(py_optval = PyLong_FromString(value, NULL, 0))) {
269 srd_err(
"Option %s has invalid value "
270 "%s: expected integer.",
275 g_hash_table_remove(options, key);
278 if (PyUnicode_Check(py_classval)) {
280 py_ustr = PyUnicode_AS_UNICODE(py_classval);
281 size = PyUnicode_GET_SIZE(py_classval);
282 py_optval = PyUnicode_FromUnicode(py_ustr, size);
283 }
else if (PyLong_Check(py_classval)) {
285 val_ull = PyLong_AsUnsignedLongLong(py_classval);
286 if (val_ull == (
unsigned long long)-1) {
289 srd_err(
"Invalid integer value for %s: "
290 "expected integer.", key);
293 if (!(py_optval = PyLong_FromUnsignedLongLong(val_ull)))
302 if (PyDict_SetItemString(py_di_options, key, py_optval) == -1)
309 Py_XDECREF(py_optlist);
310 Py_XDECREF(py_di_options);
311 Py_XDECREF(py_dec_optkeys);
312 Py_XDECREF(py_dec_options);
314 if (PyErr_Occurred())
321 static gint compare_probe_id(
const struct srd_probe *a,
const char *probe_id)
323 return strcmp(a->
id, probe_id);
340 GHashTable *new_probes)
345 int *new_probemap, new_probenum;
346 char *probe_id, *probenum_str;
348 srd_dbg(
"set probes called for instance %s with list of %d probes",
349 di->
inst_id, g_hash_table_size(new_probes));
351 if (g_hash_table_size(new_probes) == 0)
357 srd_err(
"Protocol decoder %s has no probes to define.",
364 if (!(new_probemap = g_try_malloc(
sizeof(
int) * di->
dec_num_probes))) {
365 srd_err(
"Failed to g_malloc() new probe map.");
369 for (l = g_hash_table_get_keys(new_probes); l; l = l->next) {
371 probenum_str = g_hash_table_lookup(new_probes, probe_id);
374 srd_err(
"No probe number was specified for %s.",
376 g_free(new_probemap);
379 new_probenum = strtol(probenum_str, NULL, 10);
380 if (!(sl = g_slist_find_custom(di->
decoder->
probes, probe_id,
381 (GCompareFunc)compare_probe_id))) {
384 probe_id, (GCompareFunc) compare_probe_id))) {
385 srd_err(
"Protocol decoder %s has no probe "
387 g_free(new_probemap);
392 new_probemap[p->
order] = new_probenum;
393 srd_dbg(
"setting probe mapping for %d = probe %d", p->
order,
420 srd_dbg(
"Creating new %s instance.", decoder_id);
423 srd_err(
"Protocol decoder %s not found.", decoder_id);
428 srd_err(
"Failed to g_malloc() instance.");
432 inst_id = g_hash_table_lookup(options,
"id");
434 di->
inst_id = g_strdup(inst_id ? inst_id : decoder_id);
435 g_hash_table_remove(options,
"id");
446 srd_err(
"Failed to g_malloc() probe map.");
455 if (!(di->
py_inst = PyObject_CallObject(dec->
py_dec, NULL))) {
456 if (PyErr_Occurred())
471 di_list = g_slist_append(di_list, di);
487 if (!di_from || !di_to) {
488 srd_err(
"Invalid from/to instance pair.");
492 if (g_slist_find(di_list, di_to)) {
494 di_list = g_slist_remove(di_list, di_to);
519 for (l = di_list; l; l = l->next) {
521 if (!strcmp(tmp->
inst_id, inst_id)) {
551 for (l = stack ? stack : di_list; di == NULL && l != NULL; l = l->next) {
564 PyObject *py_name, *py_res;
568 srd_dbg(
"Calling start() method on protocol decoder instance %s.",
571 if (!(py_name = PyUnicode_FromString(
"start"))) {
572 srd_err(
"Unable to build Python object for 'start'.");
578 if (!(py_res = PyObject_CallMethodObjArgs(di->
py_inst,
579 py_name, args, NULL))) {
593 for (l = di->
next_di; l; l = l->next) {
614 const uint8_t *inbuf, uint64_t inbuflen)
618 uint64_t end_samplenum;
620 srd_dbg(
"Calling decode() on instance %s with %d bytes starting "
621 "at sample %d.", di->
inst_id, inbuflen, start_samplenum);
625 srd_dbg(
"empty decoder instance");
629 srd_dbg(
"NULL buffer pointer");
646 logic->
inbuf = (uint8_t *)inbuf;
648 logic->
sample = PyList_New(2);
652 end_samplenum = start_samplenum + inbuflen / di->
data_unitsize;
653 if (!(py_res = PyObject_CallMethod(di->
py_inst,
"decode",
655 end_samplenum, logic))) {
676 for (l = di->
pd_output; l; l = l->next) {
690 for (l = stack ? stack : di_list; di == NULL && l != NULL; l = l->next) {
697 g_slist_free(di_list);
720 srd_dbg(
"Calling start() on all instances with %d probes, "
721 "unitsize %d samplerate %d.", num_probes, unitsize, samplerate);
727 if (!(args = Py_BuildValue(
"{s:l}",
"samplerate", (
long)samplerate))) {
728 srd_err(
"Unable to build Python object for metadata.");
733 for (d = di_list; d; d = d->next) {
762 srd_dbg(
"Calling decode() on all instances with starting sample "
763 "number %" PRIu64
", %" PRIu64
" bytes at 0x%p",
764 start_samplenum, inbuflen, inbuf);
766 for (d = di_list; d; d = d->next) {
792 srd_dbg(
"Registering new callback for output type %d.", output_type);
795 srd_err(
"Failed to g_malloc() struct srd_pd_callback.");
802 callbacks = g_slist_append(callbacks, pd_cb);
814 for (l = callbacks; l; l = l->next) {
831 srd_dbg(
"Instance %s creating new output type %d for %s.",
832 di->
inst_id, output_type, proto_id);
835 srd_err(
"Failed to g_malloc() struct srd_pd_output.");