X-Git-Url: https://sigrok.org/gitweb/?p=libsigrokdecode.git;a=blobdiff_plain;f=decoder.c;h=6dfeecae7fa1fa5a5ca6e1c55eb7e27641ffcc49;hp=33521c4045e3e297b27103a19b3e333033bd55fc;hb=de2f532821bab1bef235ee65b38b0f4427bb9d5c;hpb=9b37109da365335c1b6da14f073513c7855872d8 diff --git a/decoder.c b/decoder.c index 33521c4..6dfeeca 100644 --- a/decoder.c +++ b/decoder.c @@ -36,7 +36,7 @@ extern SRD_PRIV PyObject *mod_sigrokdecode; * * @return List of decoders, NULL if none are supported or loaded. */ -SRD_API GSList *srd_list_decoders(void) +SRD_API GSList *srd_decoder_list(void) { return pd_list; } @@ -48,12 +48,12 @@ SRD_API GSList *srd_list_decoders(void) * * @return The decoder with the specified ID, or NULL if not found. */ -SRD_API struct srd_decoder *srd_get_decoder_by_id(const char *id) +SRD_API struct srd_decoder *srd_decoder_get_by_id(const char *id) { GSList *l; struct srd_decoder *dec; - for (l = srd_list_decoders(); l; l = l->next) { + for (l = srd_decoder_list(); l; l = l->next) { dec = l->data; if (!strcmp(dec->id, id)) return dec; @@ -62,7 +62,8 @@ SRD_API struct srd_decoder *srd_get_decoder_by_id(const char *id) return NULL; } -static int get_probes(struct srd_decoder *d, char *attr, GSList **pl) +static int get_probes(const struct srd_decoder *d, const char *attr, + GSList **pl) { PyObject *py_probelist, *py_entry; struct srd_probe *p; @@ -123,18 +124,20 @@ err_out: /** * Load a protocol decoder module into the embedded Python interpreter. * - * @param name The module name to be loaded. + * @param module_name The module name to be loaded. * * @return SRD_OK upon success, a (negative) error code otherwise. */ -SRD_API int srd_load_decoder(const char *module_name) +SRD_API int srd_decoder_load(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; + struct srd_probe *p; + GSList *l; - srd_dbg("Loading module '%s'.", module_name); + srd_dbg("Loading protocol decoder '%s'.", module_name); py_basedec = py_method = py_attr = NULL; @@ -148,7 +151,7 @@ SRD_API int srd_load_decoder(const char *module_name) /* Import the Python module. */ if (!(d->py_mod = PyImport_ImportModule(module_name))) { - catch_exception("import of '%s' failed.", module_name); + srd_exception_catch("Import of '%s' failed.", module_name); goto err_out; } @@ -156,7 +159,8 @@ SRD_API int srd_load_decoder(const char *module_name) 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.", module_name); + srd_err("Decoder class not found in protocol decoder %s.", + module_name); goto err_out; } @@ -220,6 +224,19 @@ SRD_API int srd_load_decoder(const char *module_name) if (get_probes(d, "optional_probes", &d->opt_probes) != SRD_OK) goto err_out; + /* + * Fix order numbers for the optional probes. + * + * Example: + * Required probes: r1, r2, r3. Optional: o1, o2, o3, o4. + * 'order' fields in the d->probes list = 0, 1, 2. + * 'order' fields in the d->opt_probes list = 3, 4, 5, 6. + */ + for (l = d->opt_probes; l; l = l->next) { + p = l->data; + p->order += g_slist_length(d->probes); + } + /* Store required fields in newly allocated strings. */ if (py_attr_as_str(d->py_dec, "id", &(d->id)) != SRD_OK) goto err_out; @@ -236,11 +253,7 @@ SRD_API int srd_load_decoder(const char *module_name) if (py_attr_as_str(d->py_dec, "license", &(d->license)) != SRD_OK) goto err_out; - /* TODO: Handle inputformats, outputformats. */ - d->inputformats = NULL; - d->outputformats = NULL; - - /* Convert class annotation attribute to GSList of **char */ + /* Convert class annotation attribute to GSList of **char. */ d->annotations = NULL; if (PyObject_HasAttrString(d->py_dec, "annotations")) { py_annlist = PyObject_GetAttrString(d->py_dec, "annotations"); @@ -288,10 +301,10 @@ err_out: * * @param dec The loaded protocol decoder. * - * @return A newly allocated buffer containing the docstring. The caller should - * free this after use. + * @return A newly allocated buffer containing the protocol decoder's + * documentation. The caller is responsible for free'ing the buffer. */ -SRD_API char *srd_decoder_doc(struct srd_decoder *dec) +SRD_API char *srd_decoder_doc_get(const struct srd_decoder *dec) { PyObject *py_str; char *doc; @@ -300,7 +313,7 @@ SRD_API char *srd_decoder_doc(struct srd_decoder *dec) return NULL; if (!(py_str = PyObject_GetAttrString(dec->py_mod, "__doc__"))) { - catch_exception(""); + srd_exception_catch(""); return NULL; } @@ -328,7 +341,6 @@ static void free_probes(GSList *probelist) g_free(p); } g_slist_free(probelist); - } /** @@ -338,14 +350,16 @@ static void free_probes(GSList *probelist) * * @return SRD_OK upon success, a (negative) error code otherwise. */ -SRD_API int srd_unload_decoder(struct srd_decoder *dec) +SRD_API int srd_decoder_unload(struct srd_decoder *dec) { - srd_dbg("unloading decoder %s", dec->name); + srd_dbg("Unloading protocol decoder '%s'.", dec->name); - /* Since any instances of this decoder need to be released as well, + /* + * Since any instances of this decoder need to be released as well, * but they could be anywhere in the stack, just free the entire * stack. A frontend reloading a decoder thus has to restart all - * instances, and rebuild the stack. */ + * instances, and rebuild the stack. + */ srd_inst_free_all(NULL); free_probes(dec->probes); @@ -356,12 +370,6 @@ SRD_API int srd_unload_decoder(struct srd_decoder *dec) g_free(dec->desc); g_free(dec->license); - /* TODO: Free everything in inputformats and outputformats. */ - if (dec->inputformats != NULL) - g_slist_free(dec->inputformats); - if (dec->outputformats != NULL) - g_slist_free(dec->outputformats); - /* The module's Decoder class. */ Py_XDECREF(dec->py_dec); /* The module itself. */ @@ -377,7 +385,7 @@ SRD_API int srd_unload_decoder(struct srd_decoder *dec) * * @return SRD_OK upon success, a (negative) error code otherwise. */ -SRD_API int srd_load_all_decoders(void) +SRD_API int srd_decoder_load_all(void) { GDir *dir; GError *error; @@ -390,7 +398,7 @@ SRD_API int srd_load_all_decoders(void) while ((direntry = g_dir_read_name(dir)) != NULL) { /* The directory name is the module name (e.g. "i2c"). */ - srd_load_decoder(direntry); + srd_decoder_load(direntry); } g_dir_close(dir); @@ -402,14 +410,14 @@ SRD_API int srd_load_all_decoders(void) * * @return SRD_OK upon success, a (negative) error code otherwise. */ -SRD_API int srd_unload_all_decoders(void) +SRD_API int srd_decoder_unload_all(void) { GSList *l; struct srd_decoder *dec; - for (l = srd_list_decoders(); l; l = l->next) { + for (l = srd_decoder_list(); l; l = l->next) { dec = l->data; - srd_unload_decoder(dec); + srd_decoder_unload(dec); } return SRD_OK;