23 #include "libsigrokdecode-internal.h"
46 extern SRD_PRIV PyObject *mod_sigrokdecode;
78 for (l = pd_list; l; l = l->next) {
80 if (!strcmp(dec->
id,
id))
87 static int get_probes(
const struct srd_decoder *d,
const char *attr,
90 PyObject *py_probelist, *py_entry;
92 int ret, num_probes, i;
94 if (!PyObject_HasAttrString(d->
py_dec, attr))
99 py_probelist = py_entry = NULL;
101 py_probelist = PyObject_GetAttrString(d->
py_dec, attr);
102 if (!PyList_Check(py_probelist)) {
103 srd_err(
"Protocol decoder %s %s attribute is not "
104 "a list.", d->
name, attr);
108 num_probes = PyList_Size(py_probelist);
113 for (i = 0; i < num_probes; i++) {
114 py_entry = PyList_GetItem(py_probelist, i);
115 if (!PyDict_Check(py_entry)) {
116 srd_err(
"Protocol decoder %s %s attribute is not "
117 "a list with dict elements.", d->
name, attr);
121 if (!(p = g_try_malloc(
sizeof(
struct srd_probe)))) {
122 srd_err(
"Failed to g_malloc() struct srd_probe.");
127 if ((py_dictitem_as_str(py_entry,
"id", &p->
id)) !=
SRD_OK)
129 if ((py_dictitem_as_str(py_entry,
"name", &p->
name)) !=
SRD_OK)
131 if ((py_dictitem_as_str(py_entry,
"desc", &p->
desc)) !=
SRD_OK)
135 *pl = g_slist_append(*pl, p);
141 Py_DecRef(py_probelist);
148 PyObject *py_opts, *py_keys, *py_values, *py_val, *py_desc, *py_default;
152 int num_keys, overflow, ret;
158 if (!PyObject_HasAttrString(d->
py_dec,
"options"))
163 py_opts = PyObject_GetAttrString(d->
py_dec,
"options");
164 if (!PyDict_Check(py_opts)) {
165 srd_err(
"Protocol decoder %s options attribute is not "
166 "a dictionary.", d->
name);
170 py_keys = PyDict_Keys(py_opts);
171 py_values = PyDict_Values(py_opts);
172 num_keys = PyList_Size(py_keys);
173 for (i = 0; i < num_keys; i++) {
174 py_str_as_str(PyList_GetItem(py_keys, i), &key);
175 srd_dbg(
"option '%s'", key);
176 py_val = PyList_GetItem(py_values, i);
177 if (!PyList_Check(py_val) || PyList_Size(py_val) != 2) {
178 srd_err(
"Protocol decoder %s option '%s' value must be "
179 "a list with two elements.", d->
name, key);
182 py_desc = PyList_GetItem(py_val, 0);
183 if (!PyUnicode_Check(py_desc)) {
184 srd_err(
"Protocol decoder %s option '%s' has no "
185 "description.", d->
name, key);
188 py_default = PyList_GetItem(py_val, 1);
189 if (!PyUnicode_Check(py_default) && !PyLong_Check(py_default)) {
190 srd_err(
"Protocol decoder %s option '%s' has default "
191 "of unsupported type '%s'.", d->
name, key,
192 Py_TYPE(py_default)->tp_name);
196 srd_err(
"option malloc failed");
199 o->
id = g_strdup(key);
200 py_str_as_str(py_desc, &o->
desc);
201 if (PyUnicode_Check(py_default)) {
203 py_str_as_str(py_default, &def_str);
204 o->
def = g_variant_new_string(def_str);
208 def_long = PyLong_AsLongAndOverflow(py_default, &overflow);
212 srd_err(
"Protocol decoder %s option '%s' has "
213 "invalid default value.", d->
name, key);
216 o->
def = g_variant_new_int64(def_long);
218 g_variant_ref_sink(o->
def);
222 Py_DecRef(py_values);
244 PyObject *py_basedec, *py_method, *py_attr, *py_annlist, *py_ann;
251 srd_dbg(
"Loading protocol decoder '%s'.", module_name);
253 py_basedec = py_method = py_attr = NULL;
255 if (!(d = g_try_malloc0(
sizeof(
struct srd_decoder)))) {
256 srd_dbg(
"Failed to g_malloc() struct srd_decoder.");
264 if (!(d->
py_mod = PyImport_ImportModule(module_name))) {
265 srd_exception_catch(
"Import of '%s' failed.", module_name);
270 if (!(d->
py_dec = PyObject_GetAttrString(d->
py_mod,
"Decoder"))) {
273 srd_err(
"Decoder class not found in protocol decoder %s.",
278 if (!(py_basedec = PyObject_GetAttrString(mod_sigrokdecode,
"Decoder"))) {
279 srd_dbg(
"sigrokdecode module not loaded.");
283 if (!PyObject_IsSubclass(d->
py_dec, py_basedec)) {
284 srd_err(
"Decoder class in protocol decoder module %s is not "
285 "a subclass of sigrokdecode.Decoder.", module_name);
288 Py_CLEAR(py_basedec);
291 if (!PyObject_HasAttrString(d->
py_dec,
"start")) {
292 srd_err(
"Protocol decoder %s has no start() method Decoder "
293 "class.", module_name);
296 py_method = PyObject_GetAttrString(d->
py_dec,
"start");
297 if (!PyFunction_Check(py_method)) {
298 srd_err(
"Protocol decoder %s Decoder class attribute 'start' "
299 "is not a method.", module_name);
305 if (!PyObject_HasAttrString(d->
py_dec,
"decode")) {
306 srd_err(
"Protocol decoder %s has no decode() method Decoder "
307 "class.", module_name);
310 py_method = PyObject_GetAttrString(d->
py_dec,
"decode");
311 if (!PyFunction_Check(py_method)) {
312 srd_err(
"Protocol decoder %s Decoder class attribute 'decode' "
313 "is not a method.", module_name);
318 if (get_options(d) !=
SRD_OK)
360 if (PyObject_HasAttrString(d->
py_dec,
"annotations")) {
361 py_annlist = PyObject_GetAttrString(d->
py_dec,
"annotations");
362 if (!PyList_Check(py_annlist)) {
363 srd_err(
"Protocol decoder module %s annotations "
364 "should be a list.", module_name);
367 alen = PyList_Size(py_annlist);
368 for (i = 0; i < alen; i++) {
369 py_ann = PyList_GetItem(py_annlist, i);
370 if (!PyList_Check(py_ann) || PyList_Size(py_ann) != 2) {
371 srd_err(
"Protocol decoder module %s "
372 "annotation %d should be a list with "
373 "two elements.", module_name, i + 1);
377 if (py_strlist_to_char(py_ann, &ann) !=
SRD_OK) {
385 pd_list = g_slist_append(pd_list, d);
391 Py_XDECREF(py_method);
392 Py_XDECREF(py_basedec);
416 if (!PyObject_HasAttrString(dec->
py_mod,
"__doc__"))
419 if (!(py_str = PyObject_GetAttrString(dec->
py_mod,
"__doc__"))) {
420 srd_exception_catch(
"");
425 if (py_str != Py_None)
426 py_str_as_str(py_str, &doc);
432 static void free_probes(GSList *probelist)
437 if (probelist == NULL)
440 for (l = probelist; l; l = l->next) {
447 g_slist_free(probelist);
464 srd_dbg(
"Unloading protocol decoder '%s'.", dec->
name);
472 srd_inst_free_all(NULL);
474 for (l = dec->
options; l; l = l->next) {
478 g_variant_unref(o->
def);
512 const gchar *direntry;
514 if (!(dir = g_dir_open(DECODERS_DIR, 0, &error))) {
515 srd_err(
"Unable to open %s for reading.", DECODERS_DIR);
519 while ((direntry = g_dir_read_name(dir)) != NULL) {
540 for (l = pd_list; l; l = l->next) {