* Load a protocol decoder module into the embedded Python interpreter.
*
* @param name The module name to be loaded.
- * @param dec Pointer to the struct srd_decoder filled with the loaded module.
*
* @return SRD_OK upon success, a (negative) error code otherwise.
*/
-SRD_API int srd_load_decoder(const char *name, struct srd_decoder **dec)
+SRD_API int srd_load_decoder(const char *module_name)
{
PyObject *py_basedec, *py_method, *py_attr, *py_annlist, *py_ann;
struct srd_decoder *d;
int alen, ret, i;
char **ann;
- srd_dbg("Loading module '%s'.", name);
+ srd_dbg("Loading module '%s'.", module_name);
py_basedec = py_method = py_attr = NULL;
ret = SRD_ERR_PYTHON;
/* Import the Python module. */
- if (!(d->py_mod = PyImport_ImportModule(name))) {
- catch_exception("import of '%s' failed.", name);
+ if (!(d->py_mod = PyImport_ImportModule(module_name))) {
+ catch_exception("import of '%s' failed.", module_name);
goto err_out;
}
if (!(d->py_dec = PyObject_GetAttrString(d->py_mod, "Decoder"))) {
/* This generated an AttributeError exception. */
PyErr_Clear();
- srd_err("Decoder class not found in protocol decoder %s.", name);
+ srd_err("Decoder class not found in protocol decoder %s.", module_name);
goto err_out;
}
if (!PyObject_IsSubclass(d->py_dec, py_basedec)) {
srd_err("Decoder class in protocol decoder module %s is not "
- "a subclass of sigrokdecode.Decoder.", name);
+ "a subclass of sigrokdecode.Decoder.", module_name);
goto err_out;
}
Py_CLEAR(py_basedec);
/* Check for a proper start() method. */
if (!PyObject_HasAttrString(d->py_dec, "start")) {
srd_err("Protocol decoder %s has no start() method Decoder "
- "class.", name);
+ "class.", module_name);
goto err_out;
}
py_method = PyObject_GetAttrString(d->py_dec, "start");
if (!PyFunction_Check(py_method)) {
srd_err("Protocol decoder %s Decoder class attribute 'start' "
- "is not a method.", name);
+ "is not a method.", module_name);
goto err_out;
}
Py_CLEAR(py_method);
/* Check for a proper decode() method. */
if (!PyObject_HasAttrString(d->py_dec, "decode")) {
srd_err("Protocol decoder %s has no decode() method Decoder "
- "class.", name);
+ "class.", module_name);
goto err_out;
}
py_method = PyObject_GetAttrString(d->py_dec, "decode");
if (!PyFunction_Check(py_method)) {
srd_err("Protocol decoder %s Decoder class attribute 'decode' "
- "is not a method.", name);
+ "is not a method.", module_name);
goto err_out;
}
Py_CLEAR(py_method);
py_annlist = PyObject_GetAttrString(d->py_dec, "annotations");
if (!PyList_Check(py_annlist)) {
srd_err("Protocol decoder module %s annotations "
- "should be a list.", name);
+ "should be a list.", module_name);
goto err_out;
}
alen = PyList_Size(py_annlist);
if (!PyList_Check(py_ann) || PyList_Size(py_ann) != 2) {
srd_err("Protocol decoder module %s "
"annotation %d should be a list with "
- "two elements.", name, i + 1);
+ "two elements.", module_name, i + 1);
goto err_out;
}
}
}
- *dec = d;
+ /* Append it to the list of supported/loaded decoders. */
+ pd_list = g_slist_append(pd_list, d);
+
ret = SRD_OK;
err_out:
}
/**
- * Load all protocol decoders libsigrokdecode knows about.
+ * Load all installed protocol decoders.
*
* @return SRD_OK upon success, a (negative) error code otherwise.
*/
{
GDir *dir;
GError *error;
- struct srd_decoder *dec;
- int ret;
const gchar *direntry;
- char *decodername;
if (!(dir = g_dir_open(DECODERS_DIR, 0, &error))) {
+ srd_err("Unable to open %s for reading.", DECODERS_DIR);
return SRD_ERR_DECODERS_DIR;
}
while ((direntry = g_dir_read_name(dir)) != NULL) {
- /* The decoder name is the PD directory name (e.g. "i2c"). */
- decodername = g_strdup(direntry);
-
- if ((ret = srd_load_decoder(decodername, &dec)) == SRD_OK) {
- /* Append it to the list of supported/loaded decoders. */
- pd_list = g_slist_append(pd_list, dec);
- }
+ /* The directory name is the module name (e.g. "i2c"). */
+ srd_load_decoder(direntry);
}
g_dir_close(dir);