X-Git-Url: https://sigrok.org/gitweb/?p=libsigrokdecode.git;a=blobdiff_plain;f=decoder.c;h=9c57b1c337652840417de01829fc0c5d75654636;hp=6ad4037eb5828a5489ecc4a61e3386e644a9681c;hb=fb83ab342944ce8bddfeb87220f311954be8d0a9;hpb=e9dd2fea0514df9316a12bae191b810811fc9f01 diff --git a/decoder.c b/decoder.c index 6ad4037..9c57b1c 100644 --- a/decoder.c +++ b/decoder.c @@ -39,7 +39,7 @@ /** @cond PRIVATE */ -/* The list of protocol decoders. */ +/* The list of loaded protocol decoders. */ static GSList *pd_list = NULL; /* srd.c */ @@ -64,7 +64,7 @@ static gboolean srd_check_init(void) } /** - * Returns the list of supported/loaded protocol decoders. + * Returns the list of loaded protocol decoders. * * This is a GSList of pointers to struct srd_decoder items. * @@ -165,6 +165,8 @@ static void decoder_free(struct srd_decoder *dec) g_slist_free_full(dec->opt_channels, &channel_free); g_slist_free_full(dec->channels, &channel_free); + g_slist_free_full(dec->outputs, g_free); + g_slist_free_full(dec->inputs, g_free); g_free(dec->license); g_free(dec->desc); g_free(dec->longname); @@ -586,6 +588,29 @@ static int check_method(PyObject *py_dec, const char *mod_name, return SRD_OK; } +/** + * Get the API version of the specified decoder. + * + * @param d The decoder to use. Must not be NULL. + * + * @return The API version of the decoder, or 0 upon errors. + */ +SRD_PRIV long srd_decoder_apiver(const struct srd_decoder *d) +{ + PyObject *py_apiver; + long apiver; + + if (!d) + return 0; + + py_apiver = PyObject_GetAttrString(d->py_dec, "api_version"); + apiver = (py_apiver && PyLong_Check(py_apiver)) + ? PyLong_AsLong(py_apiver) : 0; + Py_XDECREF(py_apiver); + + return apiver; +} + /** * Load a protocol decoder module into the embedded Python interpreter. * @@ -597,10 +622,11 @@ static int check_method(PyObject *py_dec, const char *mod_name, */ SRD_API int srd_decoder_load(const char *module_name) { - PyObject *py_basedec, *py_apiver; + PyObject *py_basedec; struct srd_decoder *d; long apiver; int is_subclass; + const char *fail_txt; if (!srd_check_init()) return SRD_ERR; @@ -616,24 +642,32 @@ SRD_API int srd_decoder_load(const char *module_name) srd_dbg("Loading protocol decoder '%s'.", module_name); d = g_malloc0(sizeof(struct srd_decoder)); + fail_txt = NULL; d->py_mod = py_import_by_name(module_name); - if (!d->py_mod) + if (!d->py_mod) { + fail_txt = "import by name failed"; goto except_out; + } if (!mod_sigrokdecode) { srd_err("sigrokdecode module not loaded."); + fail_txt = "sigrokdecode(3) not loaded"; goto err_out; } /* Get the 'Decoder' class as Python object. */ d->py_dec = PyObject_GetAttrString(d->py_mod, "Decoder"); - if (!d->py_dec) + if (!d->py_dec) { + fail_txt = "no 'Decoder' attribute in imported module"; goto except_out; + } py_basedec = PyObject_GetAttrString(mod_sigrokdecode, "Decoder"); - if (!py_basedec) + if (!py_basedec) { + fail_txt = "no 'Decoder' attribute in sigrokdecode(3)"; goto except_out; + } is_subclass = PyObject_IsSubclass(d->py_dec, py_basedec); Py_DECREF(py_basedec); @@ -641,6 +675,7 @@ SRD_API int srd_decoder_load(const char *module_name) if (!is_subclass) { srd_err("Decoder class in protocol decoder module %s is not " "a subclass of sigrokdecode.Decoder.", module_name); + fail_txt = "not a subclass of sigrokdecode.Decoder"; goto err_out; } @@ -648,70 +683,111 @@ SRD_API int srd_decoder_load(const char *module_name) * Check that this decoder has the correct PD API version. * PDs of different API versions are incompatible and cannot work. */ - py_apiver = PyObject_GetAttrString(d->py_dec, "api_version"); - apiver = (py_apiver && PyLong_Check(py_apiver)) - ? PyLong_AsLong(py_apiver) : 0; - Py_XDECREF(py_apiver); - - if (apiver != 2) { - srd_exception_catch("Only PDs of API version 2 are supported"); + apiver = srd_decoder_apiver(d); + if (apiver != 2 && apiver != 3) { + srd_exception_catch("Only PD API version 2/3 is supported, " + "decoder %s has version %ld", module_name, apiver); + fail_txt = "API version mismatch"; goto err_out; } /* Check Decoder class for required methods. */ - if (check_method(d->py_dec, module_name, "start") != SRD_OK) + if (check_method(d->py_dec, module_name, "start") != SRD_OK) { + fail_txt = "no 'start()' method"; goto err_out; + } - if (check_method(d->py_dec, module_name, "decode") != SRD_OK) + if (check_method(d->py_dec, module_name, "decode") != SRD_OK) { + fail_txt = "no 'decode()' method"; goto err_out; + } /* Store required fields in newly allocated strings. */ - if (py_attr_as_str(d->py_dec, "id", &(d->id)) != SRD_OK) + if (py_attr_as_str(d->py_dec, "id", &(d->id)) != SRD_OK) { + fail_txt = "no 'id' attribute"; goto err_out; + } - if (py_attr_as_str(d->py_dec, "name", &(d->name)) != SRD_OK) + if (py_attr_as_str(d->py_dec, "name", &(d->name)) != SRD_OK) { + fail_txt = "no 'name' attribute"; goto err_out; + } - if (py_attr_as_str(d->py_dec, "longname", &(d->longname)) != SRD_OK) + if (py_attr_as_str(d->py_dec, "longname", &(d->longname)) != SRD_OK) { + fail_txt = "no 'longname' attribute"; goto err_out; + } - if (py_attr_as_str(d->py_dec, "desc", &(d->desc)) != SRD_OK) + if (py_attr_as_str(d->py_dec, "desc", &(d->desc)) != SRD_OK) { + fail_txt = "no 'desc' attribute"; goto err_out; + } - if (py_attr_as_str(d->py_dec, "license", &(d->license)) != SRD_OK) + if (py_attr_as_str(d->py_dec, "license", &(d->license)) != SRD_OK) { + fail_txt = "no 'license' attribute"; goto err_out; + } + + if (py_attr_as_strlist(d->py_dec, "inputs", &(d->inputs)) != SRD_OK) { + fail_txt = "missing or malformed 'inputs' attribute"; + goto err_out; + } + + if (py_attr_as_strlist(d->py_dec, "outputs", &(d->outputs)) != SRD_OK) { + fail_txt = "missing or malformed 'outputs' attribute"; + goto err_out; + } /* All options and their default values. */ - if (get_options(d) != SRD_OK) + if (get_options(d) != SRD_OK) { + fail_txt = "cannot get options"; goto err_out; + } /* Check and import required channels. */ - if (get_channels(d, "channels", &d->channels, 0) != SRD_OK) + if (get_channels(d, "channels", &d->channels, 0) != SRD_OK) { + fail_txt = "cannot get channels"; goto err_out; + } /* Check and import optional channels. */ if (get_channels(d, "optional_channels", &d->opt_channels, - g_slist_length(d->channels)) != SRD_OK) + g_slist_length(d->channels)) != SRD_OK) { + fail_txt = "cannot get optional channels"; goto err_out; + } - if (get_annotations(d) != SRD_OK) + if (get_annotations(d) != SRD_OK) { + fail_txt = "cannot get annotations"; goto err_out; + } - if (get_annotation_rows(d) != SRD_OK) + if (get_annotation_rows(d) != SRD_OK) { + fail_txt = "cannot get annotation rows"; goto err_out; + } - if (get_binary_classes(d) != SRD_OK) + if (get_binary_classes(d) != SRD_OK) { + fail_txt = "cannot get binary classes"; goto err_out; + } - /* Append it to the list of supported/loaded decoders. */ + /* Append it to the list of loaded decoders. */ pd_list = g_slist_append(pd_list, d); return SRD_OK; except_out: - srd_exception_catch("Failed to load decoder %s", module_name); + /* Don't show a message for the "common" directory, it's not a PD. */ + if (strcmp(module_name, "common")) { + srd_exception_catch("Failed to load decoder %s: %s", + module_name, fail_txt); + } + fail_txt = NULL; err_out: + if (fail_txt) + srd_err("Failed to load decoder %s: %s", module_name, fail_txt); decoder_free(d); return SRD_ERR_PYTHON; @@ -784,9 +860,12 @@ SRD_API int srd_decoder_unload(struct srd_decoder *dec) */ for (l = sessions; l; l = l->next) { sess = l->data; - srd_inst_free_all(sess, NULL); + srd_inst_free_all(sess); } + /* Remove the PD from the list of loaded decoders. */ + pd_list = g_slist_remove(pd_list, dec); + decoder_free(dec); return SRD_OK; @@ -924,13 +1003,7 @@ SRD_API int srd_decoder_load_all(void) */ SRD_API int srd_decoder_unload_all(void) { - GSList *l; - struct srd_decoder *dec; - - for (l = pd_list; l; l = l->next) { - dec = l->data; - srd_decoder_unload(dec); - } + g_slist_foreach(pd_list, (GFunc)srd_decoder_unload, NULL); g_slist_free(pd_list); pd_list = NULL;