58 if (!strcmp(dec->
id,
id))
65 static int get_probes(
const struct srd_decoder *d,
const char *attr,
68 PyObject *py_probelist, *py_entry;
70 int ret, num_probes, i;
72 if (!PyObject_HasAttrString(d->
py_dec, attr))
77 py_probelist = py_entry = NULL;
79 py_probelist = PyObject_GetAttrString(d->
py_dec, attr);
80 if (!PyList_Check(py_probelist)) {
81 srd_err(
"Protocol decoder %s %s attribute is not "
82 "a list.", d->
name, attr);
86 num_probes = PyList_Size(py_probelist);
91 for (i = 0; i < num_probes; i++) {
92 py_entry = PyList_GetItem(py_probelist, i);
93 if (!PyDict_Check(py_entry)) {
94 srd_err(
"Protocol decoder %s %s attribute is not "
95 "a list with dict elements.", d->
name, attr);
99 if (!(p = g_try_malloc(
sizeof(
struct srd_probe)))) {
100 srd_err(
"Failed to g_malloc() struct srd_probe.");
113 *pl = g_slist_append(*pl, p);
119 Py_DecRef(py_probelist);
133 PyObject *py_basedec, *py_method, *py_attr, *py_annlist, *py_ann;
138 srd_dbg(
"Loading protocol decoder '%s'.", module_name);
140 py_basedec = py_method = py_attr = NULL;
142 if (!(d = g_try_malloc0(
sizeof(
struct srd_decoder)))) {
143 srd_dbg(
"Failed to g_malloc() struct srd_decoder.");
151 if (!(d->
py_mod = PyImport_ImportModule(module_name))) {
157 if (!(d->
py_dec = PyObject_GetAttrString(d->
py_mod,
"Decoder"))) {
160 srd_err(
"Decoder class not found in protocol decoder %s.",
166 srd_dbg(
"sigrokdecode module not loaded.");
170 if (!PyObject_IsSubclass(d->
py_dec, py_basedec)) {
171 srd_err(
"Decoder class in protocol decoder module %s is not "
172 "a subclass of sigrokdecode.Decoder.", module_name);
175 Py_CLEAR(py_basedec);
178 if (!PyObject_HasAttrString(d->
py_dec,
"start")) {
179 srd_err(
"Protocol decoder %s has no start() method Decoder "
180 "class.", module_name);
183 py_method = PyObject_GetAttrString(d->
py_dec,
"start");
184 if (!PyFunction_Check(py_method)) {
185 srd_err(
"Protocol decoder %s Decoder class attribute 'start' "
186 "is not a method.", module_name);
192 if (!PyObject_HasAttrString(d->
py_dec,
"decode")) {
193 srd_err(
"Protocol decoder %s has no decode() method Decoder "
194 "class.", module_name);
197 py_method = PyObject_GetAttrString(d->
py_dec,
"decode");
198 if (!PyFunction_Check(py_method)) {
199 srd_err(
"Protocol decoder %s Decoder class attribute 'decode' "
200 "is not a method.", module_name);
206 if (PyObject_HasAttrString(d->
py_dec,
"options")) {
207 py_attr = PyObject_GetAttrString(d->
py_dec,
"options");
208 if (!PyDict_Check(py_attr)) {
209 srd_err(
"Protocol decoder %s options attribute is not "
210 "a dictionary.", d->
name);
243 if (PyObject_HasAttrString(d->
py_dec,
"annotations")) {
244 py_annlist = PyObject_GetAttrString(d->
py_dec,
"annotations");
245 if (!PyList_Check(py_annlist)) {
246 srd_err(
"Protocol decoder module %s annotations "
247 "should be a list.", module_name);
250 alen = PyList_Size(py_annlist);
251 for (i = 0; i < alen; i++) {
252 py_ann = PyList_GetItem(py_annlist, i);
253 if (!PyList_Check(py_ann) || PyList_Size(py_ann) != 2) {
254 srd_err(
"Protocol decoder module %s "
255 "annotation %d should be a list with "
256 "two elements.", module_name, i + 1);
274 Py_XDECREF(py_method);
275 Py_XDECREF(py_basedec);
297 if (!PyObject_HasAttrString(dec->
py_mod,
"__doc__"))
300 if (!(py_str = PyObject_GetAttrString(dec->
py_mod,
"__doc__"))) {
306 if (py_str != Py_None)
313 static void free_probes(GSList *probelist)
318 if (probelist == NULL)
321 for (l = probelist; l; l = l->next) {
328 g_slist_free(probelist);
340 srd_dbg(
"Unloading protocol decoder '%s'.", dec->
name);
377 const gchar *direntry;
379 if (!(dir = g_dir_open(DECODERS_DIR, 0, &error))) {
380 srd_err(
"Unable to open %s for reading.", DECODERS_DIR);
384 while ((direntry = g_dir_read_name(dir)) != NULL) {