X-Git-Url: https://sigrok.org/gitweb/?a=blobdiff_plain;ds=sidebyside;f=controller.c;h=0cad3f544fd3002fb03c0b3c0825fc7fb8d61cbd;hb=66e4c27c13b2e7ba857c9c9bd9a33a8b3a5cb0fa;hp=d79c607d550f0c3356ee23147c74cfbf56c77746;hpb=2086c68463fef416fd9a48dac5e1aa7fa593308d;p=libsigrokdecode.git diff --git a/controller.c b/controller.c index d79c607..0cad3f5 100644 --- a/controller.c +++ b/controller.c @@ -118,10 +118,19 @@ int srd_exit(void) /** - * Add search directories for the protocol decoders. + * Add an additional search directory for the protocol decoders. + * + * The specified directory is prepended (not appended!) to Python's sys.path, + * in order to search for sigrok protocol decoders in the specified + * directories first, and in the generic Python module directories (and in + * the current working directory) last. This avoids conflicts if there are + * Python modules which have the same name as a sigrok protocol decoder in + * sys.path or in the current working directory. * * TODO: add path from env var SIGROKDECODE_PATH, config etc * TODO: Should take directoryname/path as input. + * + * @return TODO. */ int set_modulepath(void) { @@ -143,9 +152,8 @@ int set_modulepath(void) path = g_strdup(DECODERS_DIR); #endif - /* TODO: Prepend instead of appending. */ /* TODO: Sanity check on 'path' (length, escape special chars, ...). */ - s = g_strdup_printf("import sys; sys.path.append(r'%s')", path); + s = g_strdup_printf("import sys; sys.path.insert(0, r'%s')", path); ret = PyRun_SimpleString(s); @@ -296,7 +304,7 @@ int srd_instance_set_probes(struct srd_decoder_instance *di, GSList *sl; struct srd_probe *p; int *new_probemap, new_probenum; - char *probe_id; + char *probe_id, *probenum_str; if (g_hash_table_size(new_probes) == 0) /* No probes provided. */ @@ -318,7 +326,14 @@ int srd_instance_set_probes(struct srd_decoder_instance *di, for (l = g_hash_table_get_keys(new_probes); l; l = l->next) { probe_id = l->data; - new_probenum = strtol(g_hash_table_lookup(new_probes, probe_id), NULL, 10); + probenum_str = g_hash_table_lookup(new_probes, probe_id); + if (!probenum_str) { + /* Probe name was specified without a value. */ + srd_err("No probe number was specified for %s.", probe_id); + g_free(new_probemap); + return SRD_ERR_ARG; + } + new_probenum = strtol(probenum_str, NULL, 10); if (!(sl = g_slist_find_custom(di->decoder->probes, probe_id, (GCompareFunc)compare_probe_id))) { /* Fall back on optional probes. */ @@ -418,23 +433,27 @@ int srd_instance_stack(struct srd_decoder_instance *di_from, return SRD_ERR_ARG; } - if (!g_slist_find(di_list, di_from)) { - srd_err("Unstacked instance not found."); - return SRD_ERR_ARG; + if (g_slist_find(di_list, di_to)) { + /* Remove from the unstacked list. */ + di_list = g_slist_remove(di_list, di_to); } - /* Remove from the unstacked list. */ - di_list = g_slist_remove(di_list, di_to); - /* Stack on top of source di. */ di_from->next_di = g_slist_append(di_from->next_di, di_to); return SRD_OK; } - -/* TODO: this should go into the PD stack */ -struct srd_decoder_instance *srd_instance_find(char *instance_id) +/** + * Finds a decoder instance by its instance id, but only in the bottom + * level of instances -- instances already stacked on top of another one + * will not be found. + * + * @param instance_id The instance id to be found. + * + * @return Pointer to struct srd_decoder_instance, or NULL if not found. + */ +struct srd_decoder_instance *srd_instance_find_by_id(char *instance_id) { GSList *l; struct srd_decoder_instance *tmp, *di; @@ -451,9 +470,41 @@ struct srd_decoder_instance *srd_instance_find(char *instance_id) return di; } +/** + * Finds a decoder instance by its python object, i.e. that instance's + * instantiation of the sigrokdecode.Decoder class. This will recurse + * to find the instance anywhere in the stack tree. + * + * @param stack Pointer to a GSList of struct srd_decoder_instance, + * indicating the stack to search. To start searching at the bottom + * level of decoder instances, pass NULL. + * @param obj The python class instantiation. + * + * @return Pointer to struct srd_decoder_instance, or NULL if not found. + */ +struct srd_decoder_instance *srd_instance_find_by_obj(GSList *stack, + PyObject *obj) +{ + GSList *l; + struct srd_decoder_instance *tmp, *di; + + di = NULL; + for (l = stack ? stack : di_list; di == NULL && l != NULL; l = l->next) { + tmp = l->data; + if (tmp->py_instance == obj) + di = tmp; + else if (tmp->next_di) + di = srd_instance_find_by_obj(tmp->next_di, obj); + } + + return di; +} + int srd_instance_start(struct srd_decoder_instance *di, PyObject *args) { PyObject *py_name, *py_res; + GSList *l; + struct srd_decoder_instance *next_di; srd_dbg("srd: calling start() method on protocol decoder instance %s", di->instance_id); @@ -473,6 +524,15 @@ int srd_instance_start(struct srd_decoder_instance *di, PyObject *args) Py_DecRef(py_res); Py_DecRef(py_name); + /* Start all the PDs stacked on top of this one. Pass along the + * metadata all the way from the bottom PD, even though it's only + * applicable to logic data for now. + */ + for (l = di->next_di; l; l = l->next) { + next_di = l->data; + srd_instance_start(next_di, args); + } + return SRD_OK; } @@ -540,7 +600,7 @@ int srd_instance_decode(uint64_t start_samplenum, int srd_session_start(int num_probes, int unitsize, uint64_t samplerate) { PyObject *args; - GSList *d, *s; + GSList *d; struct srd_decoder_instance *di; int ret; @@ -562,20 +622,12 @@ int srd_session_start(int num_probes, int unitsize, uint64_t samplerate) di->data_unitsize = unitsize; di->data_samplerate = samplerate; if ((ret = srd_instance_start(di, args) != SRD_OK)) - return ret; - - /* Run the start() method on all decoders up the stack from this one. */ - for (s = di->next_di; s; s = s->next) { - /* These don't need probes, unitsize and samplerate. */ - di = s->data; - if ((ret = srd_instance_start(di, args) != SRD_OK)) - return ret; - } + break; } Py_DecRef(args); - return SRD_OK; + return ret; } /* Feed logic samples to decoder session. */ @@ -648,30 +700,10 @@ int pd_add(struct srd_decoder_instance *di, int output_type, /* pdo_id is just a simple index, nothing is deleted from this list anyway. */ pdo->pdo_id = g_slist_length(di->pd_output); pdo->output_type = output_type; - pdo->decoder = di->decoder; + pdo->di = di; pdo->proto_id = g_strdup(proto_id); di->pd_output = g_slist_append(di->pd_output, pdo); return pdo->pdo_id; } -struct srd_decoder_instance *get_di_by_decobject(void *decobject) -{ - GSList *l, *s; - struct srd_decoder_instance *di; - - for (l = di_list; l; l = l->next) { - di = l->data; - if (decobject == di->py_instance) - return di; - /* Check decoders stacked on top of this one. */ - for (s = di->next_di; s; s = s->next) { - di = s->data; - if (decobject == di->py_instance) - return di; - } - } - - return NULL; -} -