X-Git-Url: https://sigrok.org/gitweb/?p=libsigrokdecode.git;a=blobdiff_plain;f=decoder.c;h=60bd4868d3faba2ca926a8f22a48e55d84a79e23;hp=f6032f59b65830a64654a67c98a48d706b88a7bb;hb=ef36224880135a05d2fbde8f048ea3fe3f425df9;hpb=22c9bc2a4e958fccf498f4480965ec03884bc058 diff --git a/decoder.c b/decoder.c index f6032f5..60bd486 100644 --- a/decoder.c +++ b/decoder.c @@ -42,6 +42,10 @@ /* The list of protocol decoders. */ SRD_PRIV GSList *pd_list = NULL; +/* srd.c */ +extern GSList *searchpaths; + +/* session.c */ extern GSList *sessions; /* module_sigrokdecode.c */ @@ -250,7 +254,7 @@ SRD_API int srd_decoder_load(const char *module_name) *py_bin_classes, *py_bin_class; struct srd_decoder *d; int len, ret, i; - char **ann, *bin; + char **ann, **bin; struct srd_probe *p; GSList *l; @@ -260,6 +264,11 @@ SRD_API int srd_decoder_load(const char *module_name) if (!module_name) return SRD_ERR_ARG; + if (PyDict_GetItemString(PyImport_GetModuleDict(), module_name)) { + /* Module was already imported. */ + return SRD_OK; + } + srd_dbg("Loading protocol decoder '%s'.", module_name); py_basedec = py_method = py_attr = NULL; @@ -372,21 +381,20 @@ SRD_API int srd_decoder_load(const char *module_name) if (PyObject_HasAttrString(d->py_dec, "annotations")) { py_annlist = PyObject_GetAttrString(d->py_dec, "annotations"); if (!PyList_Check(py_annlist)) { - srd_err("Protocol decoder module %s annotations " - "should be a list.", module_name); + srd_err("Protocol decoder %s annotations should " + "be a list.", module_name); goto err_out; } len = PyList_Size(py_annlist); for (i = 0; i < len; i++) { py_ann = PyList_GetItem(py_annlist, i); 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.", module_name, i + 1); + srd_err("Protocol decoder %s annotation %d should " + "be a list with two elements.", module_name, i + 1); goto err_out; } - if (py_strlist_to_char(py_ann, &ann) != SRD_OK) { + if (py_strseq_to_char(py_ann, &ann) != SRD_OK) { goto err_out; } d->annotations = g_slist_append(d->annotations, ann); @@ -398,20 +406,27 @@ SRD_API int srd_decoder_load(const char *module_name) if (PyObject_HasAttrString(d->py_dec, "binary")) { py_bin_classes = PyObject_GetAttrString(d->py_dec, "binary"); if (!PyTuple_Check(py_bin_classes)) { - srd_err("Protocol decoder module %s binary classes " - "should be a tuple.", module_name); + srd_err("Protocol decoder %s binary classes should " + "be a tuple.", module_name); goto err_out; } len = PyTuple_Size(py_bin_classes); for (i = 0; i < len; i++) { py_bin_class = PyTuple_GetItem(py_bin_classes, i); - if (!PyUnicode_Check(py_bin_class)) { - srd_err("Protocol decoder module %s binary " - "class should be a string.", module_name); + if (!PyTuple_Check(py_bin_class)) { + srd_err("Protocol decoder %s binary classes " + "should consist of tuples.", module_name); + goto err_out; + } + if (PyTuple_Size(py_bin_class) != 2 + || !PyUnicode_Check(PyTuple_GetItem(py_bin_class, 0)) + || !PyUnicode_Check(PyTuple_GetItem(py_bin_class, 1))) { + srd_err("Protocol decoder %s binary classes should " + "contain tuples with two strings.", module_name); goto err_out; } - if (py_str_as_str(py_bin_class, &bin) != SRD_OK) { + if (py_strseq_to_char(py_bin_class, &bin) != SRD_OK) { goto err_out; } d->binary = g_slist_append(d->binary, bin); @@ -551,6 +566,26 @@ SRD_API int srd_decoder_unload(struct srd_decoder *dec) return SRD_OK; } +static void srd_decoder_load_all_path(char *path) +{ + GDir *dir; + const gchar *direntry; + + if (!(dir = g_dir_open(path, 0, NULL))) + /* Not really fatal */ + return; + + /* This ignores errors returned by srd_decoder_load(). That + * function will have logged the cause, but in any case we + * want to continue anyway. */ + while ((direntry = g_dir_read_name(dir)) != NULL) { + /* The directory name is the module name (e.g. "i2c"). */ + srd_decoder_load(direntry); + } + g_dir_close(dir); + +} + /** * Load all installed protocol decoders. * @@ -560,23 +595,13 @@ SRD_API int srd_decoder_unload(struct srd_decoder *dec) */ SRD_API int srd_decoder_load_all(void) { - GDir *dir; - GError *error; - const gchar *direntry; + GSList *l; if (!srd_check_init()) return SRD_ERR; - 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 directory name is the module name (e.g. "i2c"). */ - srd_decoder_load(direntry); - } - g_dir_close(dir); + for (l = searchpaths; l; l = l->next) + srd_decoder_load_all_path(l->data); return SRD_OK; } @@ -597,6 +622,8 @@ SRD_API int srd_decoder_unload_all(void) dec = l->data; srd_decoder_unload(dec); } + g_slist_free(pd_list); + pd_list = NULL; return SRD_OK; }