2 * This file is part of the libsigrokdecode project.
4 * Copyright (C) 2012 Bert Vermeulen <bert@biot.com>
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include "libsigrokdecode-internal.h" /* First, so we avoid a _POSIX_C_SOURCE warning. */
22 #include "libsigrokdecode.h"
26 extern SRD_PRIV GSList *sessions;
33 /* This is only used for nicer srd_dbg() output. */
34 static const char *output_type_name(unsigned int idx)
36 static const char names[][16] = {
44 return names[MIN(idx, G_N_ELEMENTS(names) - 1)];
47 static void release_annotation(struct srd_proto_data_annotation *pda)
52 g_strfreev(pda->ann_text);
56 static int convert_annotation(struct srd_decoder_inst *di, PyObject *obj,
57 struct srd_proto_data *pdata)
60 struct srd_pd_output *pdo;
61 struct srd_proto_data_annotation *pda;
64 PyGILState_STATE gstate;
66 gstate = PyGILState_Ensure();
68 /* Should be a list of [annotation class, [string, ...]]. */
69 if (!PyList_Check(obj)) {
70 srd_err("Protocol decoder %s submitted an annotation that"
71 " is not a list", di->decoder->name);
75 /* Should have 2 elements. */
76 if (PyList_Size(obj) != 2) {
77 srd_err("Protocol decoder %s submitted annotation list with "
78 "%zd elements instead of 2", di->decoder->name,
84 * The first element should be an integer matching a previously
85 * registered annotation class.
87 py_tmp = PyList_GetItem(obj, 0);
88 if (!PyLong_Check(py_tmp)) {
89 srd_err("Protocol decoder %s submitted annotation list, but "
90 "first element was not an integer.", di->decoder->name);
93 ann_class = PyLong_AsLong(py_tmp);
94 if (!(pdo = g_slist_nth_data(di->decoder->annotations, ann_class))) {
95 srd_err("Protocol decoder %s submitted data to unregistered "
96 "annotation class %d.", di->decoder->name, ann_class);
100 /* Second element must be a list. */
101 py_tmp = PyList_GetItem(obj, 1);
102 if (!PyList_Check(py_tmp)) {
103 srd_err("Protocol decoder %s submitted annotation list, but "
104 "second element was not a list.", di->decoder->name);
107 if (py_strseq_to_char(py_tmp, &ann_text) != SRD_OK) {
108 srd_err("Protocol decoder %s submitted annotation list, but "
109 "second element was malformed.", di->decoder->name);
113 pda = g_malloc(sizeof(struct srd_proto_data_annotation));
114 pda->ann_class = ann_class;
115 pda->ann_text = ann_text;
118 PyGILState_Release(gstate);
123 PyGILState_Release(gstate);
125 return SRD_ERR_PYTHON;
128 static void release_binary(struct srd_proto_data_binary *pdb)
132 g_free((void *)pdb->data);
136 static int convert_binary(struct srd_decoder_inst *di, PyObject *obj,
137 struct srd_proto_data *pdata)
139 struct srd_proto_data_binary *pdb;
143 char *class_name, *buf;
144 PyGILState_STATE gstate;
146 gstate = PyGILState_Ensure();
148 /* Should be a list of [binary class, bytes]. */
149 if (!PyList_Check(obj)) {
150 srd_err("Protocol decoder %s submitted non-list for SRD_OUTPUT_BINARY.",
155 /* Should have 2 elements. */
156 if (PyList_Size(obj) != 2) {
157 srd_err("Protocol decoder %s submitted SRD_OUTPUT_BINARY list "
158 "with %zd elements instead of 2", di->decoder->name,
163 /* The first element should be an integer. */
164 py_tmp = PyList_GetItem(obj, 0);
165 if (!PyLong_Check(py_tmp)) {
166 srd_err("Protocol decoder %s submitted SRD_OUTPUT_BINARY list, "
167 "but first element was not an integer.", di->decoder->name);
170 bin_class = PyLong_AsLong(py_tmp);
171 if (!(class_name = g_slist_nth_data(di->decoder->binary, bin_class))) {
172 srd_err("Protocol decoder %s submitted SRD_OUTPUT_BINARY with "
173 "unregistered binary class %d.", di->decoder->name, bin_class);
177 /* Second element should be bytes. */
178 py_tmp = PyList_GetItem(obj, 1);
179 if (!PyBytes_Check(py_tmp)) {
180 srd_err("Protocol decoder %s submitted SRD_OUTPUT_BINARY list, "
181 "but second element was not bytes.", di->decoder->name);
185 /* Consider an empty set of bytes a bug. */
186 if (PyBytes_Size(py_tmp) == 0) {
187 srd_err("Protocol decoder %s submitted SRD_OUTPUT_BINARY "
188 "with empty data set.", di->decoder->name);
192 if (PyBytes_AsStringAndSize(py_tmp, &buf, &size) == -1)
195 PyGILState_Release(gstate);
197 pdb = g_malloc(sizeof(struct srd_proto_data_binary));
198 pdb->bin_class = bin_class;
200 if (!(pdb->data = g_try_malloc(pdb->size))) {
202 return SRD_ERR_MALLOC;
204 memcpy((void *)pdb->data, (const void *)buf, pdb->size);
210 PyGILState_Release(gstate);
212 return SRD_ERR_PYTHON;
215 static inline struct srd_decoder_inst *srd_sess_inst_find_by_obj(
216 struct srd_session *sess, const GSList *stack, const PyObject *obj)
219 struct srd_decoder_inst *tmp, *di;
225 for (l = stack ? stack : sess->di_list; di == NULL && l != NULL; l = l->next) {
227 if (tmp->py_inst == obj)
229 else if (tmp->next_di)
230 di = srd_sess_inst_find_by_obj(sess, tmp->next_di, obj);
237 * Find a decoder instance by its Python object.
239 * I.e. find that instance's instantiation of the sigrokdecode.Decoder class.
240 * This will recurse to find the instance anywhere in the stack tree of all
243 * @param stack Pointer to a GSList of struct srd_decoder_inst, indicating the
244 * stack to search. To start searching at the bottom level of
245 * decoder instances, pass NULL.
246 * @param obj The Python class instantiation.
248 * @return Pointer to struct srd_decoder_inst, or NULL if not found.
252 static inline struct srd_decoder_inst *srd_inst_find_by_obj(
253 const GSList *stack, const PyObject *obj)
255 struct srd_decoder_inst *di;
256 struct srd_session *sess;
260 for (l = sessions; di == NULL && l != NULL; l = l->next) {
262 di = srd_sess_inst_find_by_obj(sess, stack, obj);
268 static int convert_meta(struct srd_proto_data *pdata, PyObject *obj)
272 PyGILState_STATE gstate;
274 gstate = PyGILState_Ensure();
276 if (g_variant_type_equal(pdata->pdo->meta_type, G_VARIANT_TYPE_INT64)) {
277 if (!PyLong_Check(obj)) {
278 PyErr_Format(PyExc_TypeError, "This output was registered "
279 "as 'int', but something else was passed.");
282 intvalue = PyLong_AsLongLong(obj);
283 if (PyErr_Occurred())
285 pdata->data = g_variant_new_int64(intvalue);
286 } else if (g_variant_type_equal(pdata->pdo->meta_type, G_VARIANT_TYPE_DOUBLE)) {
287 if (!PyFloat_Check(obj)) {
288 PyErr_Format(PyExc_TypeError, "This output was registered "
289 "as 'float', but something else was passed.");
292 dvalue = PyFloat_AsDouble(obj);
293 if (PyErr_Occurred())
295 pdata->data = g_variant_new_double(dvalue);
298 PyGILState_Release(gstate);
303 PyGILState_Release(gstate);
305 return SRD_ERR_PYTHON;
308 static void release_meta(GVariant *gvar)
312 g_variant_unref(gvar);
315 static PyObject *Decoder_put(PyObject *self, PyObject *args)
318 PyObject *py_data, *py_res;
319 struct srd_decoder_inst *di, *next_di;
320 struct srd_pd_output *pdo;
321 struct srd_proto_data pdata;
322 uint64_t start_sample, end_sample;
324 struct srd_pd_callback *cb;
325 PyGILState_STATE gstate;
329 gstate = PyGILState_Ensure();
331 if (!(di = srd_inst_find_by_obj(NULL, self))) {
332 /* Shouldn't happen. */
333 srd_dbg("put(): self instance not found.");
337 if (!PyArg_ParseTuple(args, "KKiO", &start_sample, &end_sample,
338 &output_id, &py_data)) {
340 * This throws an exception, but by returning NULL here we let
341 * Python raise it. This results in a much better trace in
342 * controller.c on the decode() method call.
347 if (!(l = g_slist_nth(di->pd_output, output_id))) {
348 srd_err("Protocol decoder %s submitted invalid output ID %d.",
349 di->decoder->name, output_id);
354 srd_spew("Instance %s put %" PRIu64 "-%" PRIu64 " %s on oid %d.",
355 di->inst_id, start_sample, end_sample,
356 output_type_name(pdo->output_type), output_id);
358 pdata.start_sample = start_sample;
359 pdata.end_sample = end_sample;
363 switch (pdo->output_type) {
365 /* Annotations are only fed to callbacks. */
366 if ((cb = srd_pd_output_callback_find(di->sess, pdo->output_type))) {
367 /* Convert from PyDict to srd_proto_data_annotation. */
368 if (convert_annotation(di, py_data, &pdata) != SRD_OK) {
369 /* An error was already logged. */
372 Py_BEGIN_ALLOW_THREADS
373 cb->cb(&pdata, cb->cb_data);
375 release_annotation(pdata.data);
378 case SRD_OUTPUT_PYTHON:
379 for (l = di->next_di; l; l = l->next) {
381 srd_spew("Sending %" PRIu64 "-%" PRIu64 " to instance %s",
382 start_sample, end_sample, next_di->inst_id);
383 if (!(py_res = PyObject_CallMethod(
384 next_di->py_inst, "decode", "KKO", start_sample,
385 end_sample, py_data))) {
386 srd_exception_catch("Calling %s decode() failed",
391 if ((cb = srd_pd_output_callback_find(di->sess, pdo->output_type))) {
393 * Frontends aren't really supposed to get Python
394 * callbacks, but it's useful for testing.
396 pdata.data = py_data;
397 cb->cb(&pdata, cb->cb_data);
400 case SRD_OUTPUT_BINARY:
401 if ((cb = srd_pd_output_callback_find(di->sess, pdo->output_type))) {
402 /* Convert from PyDict to srd_proto_data_binary. */
403 if (convert_binary(di, py_data, &pdata) != SRD_OK) {
404 /* An error was already logged. */
407 Py_BEGIN_ALLOW_THREADS
408 cb->cb(&pdata, cb->cb_data);
410 release_binary(pdata.data);
413 case SRD_OUTPUT_META:
414 if ((cb = srd_pd_output_callback_find(di->sess, pdo->output_type))) {
415 /* Annotations need converting from PyObject. */
416 if (convert_meta(&pdata, py_data) != SRD_OK) {
417 /* An exception was already set up. */
420 Py_BEGIN_ALLOW_THREADS
421 cb->cb(&pdata, cb->cb_data);
423 release_meta(pdata.data);
427 srd_err("Protocol decoder %s submitted invalid output type %d.",
428 di->decoder->name, pdo->output_type);
432 PyGILState_Release(gstate);
437 PyGILState_Release(gstate);
442 static PyObject *Decoder_register(PyObject *self, PyObject *args,
445 struct srd_decoder_inst *di;
446 struct srd_pd_output *pdo;
447 PyObject *py_new_output_id;
448 PyTypeObject *meta_type_py;
449 const GVariantType *meta_type_gv;
451 char *proto_id, *meta_name, *meta_descr;
452 char *keywords[] = { "output_type", "proto_id", "meta", NULL };
453 PyGILState_STATE gstate;
456 struct srd_pd_output *cmp;
458 gstate = PyGILState_Ensure();
462 meta_name = meta_descr = NULL;
464 if (!(di = srd_inst_find_by_obj(NULL, self))) {
465 PyErr_SetString(PyExc_Exception, "decoder instance not found");
469 /* Default to instance ID, which defaults to class ID. */
470 proto_id = di->inst_id;
471 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "i|s(Oss)", keywords,
472 &output_type, &proto_id,
473 &meta_type_py, &meta_name, &meta_descr)) {
474 /* Let Python raise this exception. */
478 /* Check if the meta value's type is supported. */
479 is_meta = output_type == SRD_OUTPUT_META;
481 if (meta_type_py == &PyLong_Type)
482 meta_type_gv = G_VARIANT_TYPE_INT64;
483 else if (meta_type_py == &PyFloat_Type)
484 meta_type_gv = G_VARIANT_TYPE_DOUBLE;
486 PyErr_Format(PyExc_TypeError, "Unsupported type.");
492 for (l = di->pd_output; l; l = l->next) {
494 if (cmp->output_type != output_type)
496 if (strcmp(cmp->proto_id, proto_id) != 0)
498 if (is_meta && cmp->meta_type != meta_type_gv)
500 if (is_meta && strcmp(cmp->meta_name, meta_name) != 0)
502 if (is_meta && strcmp(cmp->meta_descr, meta_descr) != 0)
508 py_new_output_id = Py_BuildValue("i", pdo->pdo_id);
509 PyGILState_Release(gstate);
510 return py_new_output_id;
513 srd_dbg("Instance %s creating new output type %d for %s.",
514 di->inst_id, output_type, proto_id);
516 pdo = g_malloc(sizeof(struct srd_pd_output));
518 /* pdo_id is just a simple index, nothing is deleted from this list anyway. */
519 pdo->pdo_id = g_slist_length(di->pd_output);
520 pdo->output_type = output_type;
522 pdo->proto_id = g_strdup(proto_id);
524 if (output_type == SRD_OUTPUT_META) {
525 pdo->meta_type = meta_type_gv;
526 pdo->meta_name = g_strdup(meta_name);
527 pdo->meta_descr = g_strdup(meta_descr);
530 di->pd_output = g_slist_append(di->pd_output, pdo);
531 py_new_output_id = Py_BuildValue("i", pdo->pdo_id);
533 PyGILState_Release(gstate);
535 return py_new_output_id;
538 PyGILState_Release(gstate);
543 static int get_term_type(const char *v)
547 return SRD_TERM_HIGH;
551 return SRD_TERM_RISING_EDGE;
553 return SRD_TERM_FALLING_EDGE;
555 return SRD_TERM_EITHER_EDGE;
557 return SRD_TERM_NO_EDGE;
566 * Get the pin values at the current sample number.
568 * @param di The decoder instance to use. Must not be NULL.
569 * The number of channels must be >= 1.
571 * @return A newly allocated PyTuple containing the pin values at the
572 * current sample number.
574 static PyObject *get_current_pinvalues(const struct srd_decoder_inst *di)
578 const uint8_t *sample_pos;
579 int byte_offset, bit_offset;
580 PyObject *py_pinvalues;
581 PyGILState_STATE gstate;
584 srd_err("Invalid decoder instance.");
588 gstate = PyGILState_Ensure();
590 py_pinvalues = PyTuple_New(di->dec_num_channels);
592 for (i = 0; i < di->dec_num_channels; i++) {
593 /* A channelmap value of -1 means "unused optional channel". */
594 if (di->dec_channelmap[i] == -1) {
595 /* Value of unused channel is 0xff, instead of 0 or 1. */
596 PyTuple_SetItem(py_pinvalues, i, PyLong_FromLong(0xff));
598 sample_pos = di->inbuf + ((di->abs_cur_samplenum - di->abs_start_samplenum) * di->data_unitsize);
599 byte_offset = di->dec_channelmap[i] / 8;
600 bit_offset = di->dec_channelmap[i] % 8;
601 sample = *(sample_pos + byte_offset) & (1 << bit_offset) ? 1 : 0;
602 PyTuple_SetItem(py_pinvalues, i, PyLong_FromLong(sample));
606 PyGILState_Release(gstate);
612 * Create a list of terms in the specified condition.
614 * If there are no terms in the condition, 'term_list' will be NULL.
616 * @param py_dict A Python dict containing terms. Must not be NULL.
617 * @param term_list Pointer to a GSList which will be set to the newly
618 * created list of terms. Must not be NULL.
620 * @return SRD_OK upon success, a negative error code otherwise.
622 static int create_term_list(PyObject *py_dict, GSList **term_list)
625 PyObject *py_key, *py_value;
626 struct srd_term *term;
627 uint64_t num_samples_to_skip;
629 PyGILState_STATE gstate;
631 if (!py_dict || !term_list)
634 /* "Create" an empty GSList of terms. */
637 gstate = PyGILState_Ensure();
639 /* Iterate over all items in the current dict. */
640 while (PyDict_Next(py_dict, &pos, &py_key, &py_value)) {
641 /* Check whether the current key is a string or a number. */
642 if (PyLong_Check(py_key)) {
643 /* The key is a number. */
644 /* TODO: Check if the number is a valid channel. */
645 /* Get the value string. */
646 if ((py_pydictitem_as_str(py_dict, py_key, &term_str)) != SRD_OK) {
647 srd_err("Failed to get the value.");
650 term = g_malloc(sizeof(struct srd_term));
651 term->type = get_term_type(term_str);
652 term->channel = PyLong_AsLong(py_key);
654 } else if (PyUnicode_Check(py_key)) {
655 /* The key is a string. */
656 /* TODO: Check if it's "skip". */
657 if ((py_pydictitem_as_long(py_dict, py_key, &num_samples_to_skip)) != SRD_OK) {
658 srd_err("Failed to get number of samples to skip.");
661 term = g_malloc(sizeof(struct srd_term));
662 term->type = SRD_TERM_SKIP;
663 term->num_samples_to_skip = num_samples_to_skip;
664 term->num_samples_already_skipped = 0;
666 srd_err("Term key is neither a string nor a number.");
670 /* Add the term to the list of terms. */
671 *term_list = g_slist_append(*term_list, term);
674 PyGILState_Release(gstate);
679 PyGILState_Release(gstate);
685 * Replace the current condition list with the new one.
687 * @param self TODO. Must not be NULL.
688 * @param args TODO. Must not be NULL.
690 * @retval SRD_OK The new condition list was set successfully.
691 * @retval SRD_ERR There was an error setting the new condition list.
692 * The contents of di->condition_list are undefined.
695 static int set_new_condition_list(PyObject *self, PyObject *args)
697 struct srd_decoder_inst *di;
699 PyObject *py_conditionlist, *py_conds, *py_dict;
700 int i, num_conditions, ret;
701 PyGILState_STATE gstate;
706 gstate = PyGILState_Ensure();
708 /* Get the decoder instance. */
709 if (!(di = srd_inst_find_by_obj(NULL, self))) {
710 PyErr_SetString(PyExc_Exception, "decoder instance not found");
715 * Return an error condition from .wait() when termination is
716 * requested, such that decode() will terminate.
718 if (di->want_wait_terminate) {
719 srd_dbg("%s: %s: Skip (want_term).", di->inst_id, __func__);
724 * Parse the argument of self.wait() into 'py_conds', and check
725 * the data type. The argument is optional, None is assumed in
726 * its absence. None or an empty dict or an empty list mean that
727 * there is no condition, and the next available sample shall
728 * get returned to the caller.
731 if (!PyArg_ParseTuple(args, "|O", &py_conds)) {
732 /* Let Python raise this exception. */
735 if (py_conds == Py_None) {
736 /* 'py_conds' is None. */
738 } else if (PyList_Check(py_conds)) {
739 /* 'py_conds' is a list. */
740 py_conditionlist = py_conds;
741 num_conditions = PyList_Size(py_conditionlist);
742 if (num_conditions == 0)
743 goto ret_9999; /* The PD invoked self.wait([]). */
744 Py_IncRef(py_conditionlist);
745 } else if (PyDict_Check(py_conds)) {
746 /* 'py_conds' is a dict. */
747 if (PyDict_Size(py_conds) == 0)
748 goto ret_9999; /* The PD invoked self.wait({}). */
749 /* Make a list and put the dict in there for convenience. */
750 py_conditionlist = PyList_New(1);
752 PyList_SetItem(py_conditionlist, 0, py_conds);
755 srd_err("Condition list is neither a list nor a dict.");
759 /* Free the old condition list. */
760 condition_list_free(di);
764 /* Iterate over the conditions, set di->condition_list accordingly. */
765 for (i = 0; i < num_conditions; i++) {
766 /* Get a condition (dict) from the condition list. */
767 py_dict = PyList_GetItem(py_conditionlist, i);
768 if (!PyDict_Check(py_dict)) {
769 srd_err("Condition is not a dict.");
774 /* Create the list of terms in this condition. */
775 if ((ret = create_term_list(py_dict, &term_list)) < 0)
778 /* Add the new condition to the PD instance's condition list. */
779 di->condition_list = g_slist_append(di->condition_list, term_list);
782 Py_DecRef(py_conditionlist);
784 PyGILState_Release(gstate);
789 PyGILState_Release(gstate);
794 PyGILState_Release(gstate);
800 * Create a SKIP condition list for condition-less .wait() calls.
802 * @param di Decoder instance.
803 * @param count Number of samples to skip.
805 * @retval SRD_OK The new condition list was set successfully.
806 * @retval SRD_ERR There was an error setting the new condition list.
807 * The contents of di->condition_list are undefined.
809 * This routine is a reduced and specialized version of the @ref
810 * set_new_condition_list() and @ref create_term_list() routines which
811 * gets invoked when .wait() was called without specifications for
812 * conditions. This minor duplication of the SKIP term list creation
813 * simplifies the logic and avoids the creation of expensive Python
814 * objects with "constant" values which the caller did not pass in the
815 * first place. It results in maximum sharing of match handling code
818 static int set_skip_condition(struct srd_decoder_inst *di, uint64_t count)
820 struct srd_term *term;
823 condition_list_free(di);
824 term = g_malloc(sizeof(*term));
825 term->type = SRD_TERM_SKIP;
826 term->num_samples_to_skip = count;
827 term->num_samples_already_skipped = 0;
828 term_list = g_slist_append(NULL, term);
829 di->condition_list = g_slist_append(di->condition_list, term_list);
834 static PyObject *Decoder_wait(PyObject *self, PyObject *args)
839 gboolean found_match;
840 struct srd_decoder_inst *di;
841 PyObject *py_pinvalues, *py_matched;
842 PyGILState_STATE gstate;
847 gstate = PyGILState_Ensure();
849 if (!(di = srd_inst_find_by_obj(NULL, self))) {
850 PyErr_SetString(PyExc_Exception, "decoder instance not found");
851 PyGILState_Release(gstate);
855 ret = set_new_condition_list(self, args);
857 srd_dbg("%s: %s: Aborting wait().", di->inst_id, __func__);
862 * Empty condition list, automatic match. Arrange for the
863 * execution of regular match handling code paths such that
864 * the next available sample is returned to the caller.
865 * Make sure to skip one sample when "anywhere within the
866 * stream", yet make sure to not skip sample number 0.
868 if (di->abs_cur_samplenum)
870 else if (!di->condition_list)
874 ret = set_skip_condition(di, skip_count);
876 srd_dbg("%s: %s: Cannot setup condition-less wait().",
877 di->inst_id, __func__);
884 Py_BEGIN_ALLOW_THREADS
886 /* Wait for new samples to process, or termination request. */
887 g_mutex_lock(&di->data_mutex);
888 while (!di->got_new_samples && !di->want_wait_terminate)
889 g_cond_wait(&di->got_new_samples_cond, &di->data_mutex);
892 * Check whether any of the current condition(s) match.
893 * Arrange for termination requests to take a code path which
894 * won't find new samples to process, pretends to have processed
895 * previously stored samples, and returns to the main thread,
896 * while the termination request still gets signalled.
900 /* Ignore return value for now, should never be negative. */
901 (void)process_samples_until_condition_match(di, &found_match);
905 /* If there's a match, set self.samplenum etc. and return. */
907 /* Set self.samplenum to the (absolute) sample number that matched. */
908 PyObject_SetAttrString(di->py_inst, "samplenum",
909 PyLong_FromLong(di->abs_cur_samplenum));
911 if (di->match_array && di->match_array->len > 0) {
912 py_matched = PyTuple_New(di->match_array->len);
913 for (i = 0; i < di->match_array->len; i++)
914 PyTuple_SetItem(py_matched, i, PyBool_FromLong(di->match_array->data[i]));
915 PyObject_SetAttrString(di->py_inst, "matched", py_matched);
916 match_array_free(di);
918 PyObject_SetAttrString(di->py_inst, "matched", Py_None);
921 py_pinvalues = get_current_pinvalues(di);
923 g_mutex_unlock(&di->data_mutex);
925 PyGILState_Release(gstate);
930 /* No match, reset state for the next chunk. */
931 di->got_new_samples = FALSE;
932 di->handled_all_samples = TRUE;
933 di->abs_start_samplenum = 0;
934 di->abs_end_samplenum = 0;
938 /* Signal the main thread that we handled all samples. */
939 g_cond_signal(&di->handled_all_samples_cond);
942 * When termination of wait() and decode() was requested,
943 * then exit the loop after releasing the mutex.
945 if (di->want_wait_terminate) {
946 srd_dbg("%s: %s: Will return from wait().",
947 di->inst_id, __func__);
948 g_mutex_unlock(&di->data_mutex);
952 g_mutex_unlock(&di->data_mutex);
955 PyGILState_Release(gstate);
960 PyGILState_Release(gstate);
966 * Return whether the specified channel was supplied to the decoder.
968 * @param self TODO. Must not be NULL.
969 * @param args TODO. Must not be NULL.
971 * @retval Py_True The channel has been supplied by the frontend.
972 * @retval Py_False The channel has been supplied by the frontend.
973 * @retval NULL An error occurred.
975 static PyObject *Decoder_has_channel(PyObject *self, PyObject *args)
978 struct srd_decoder_inst *di;
979 PyGILState_STATE gstate;
984 gstate = PyGILState_Ensure();
986 if (!(di = srd_inst_find_by_obj(NULL, self))) {
987 PyErr_SetString(PyExc_Exception, "decoder instance not found");
992 * Get the integer argument of self.has_channel(). Check for
993 * the range of supported PD input channel numbers.
995 if (!PyArg_ParseTuple(args, "i", &idx)) {
996 /* Let Python raise this exception. */
1000 count = g_slist_length(di->decoder->channels) +
1001 g_slist_length(di->decoder->opt_channels);
1002 if (idx < 0 || idx >= count) {
1003 srd_err("Invalid index %d, PD channel count %d.", idx, count);
1004 PyErr_SetString(PyExc_IndexError, "invalid channel index");
1008 PyGILState_Release(gstate);
1010 return (di->dec_channelmap[idx] == -1) ? Py_False : Py_True;
1013 PyGILState_Release(gstate);
1018 static PyMethodDef Decoder_methods[] = {
1019 { "put", Decoder_put, METH_VARARGS,
1020 "Accepts a dictionary with the following keys: startsample, endsample, data" },
1021 { "register", (PyCFunction)Decoder_register, METH_VARARGS|METH_KEYWORDS,
1022 "Register a new output stream" },
1023 { "wait", Decoder_wait, METH_VARARGS,
1024 "Wait for one or more conditions to occur" },
1025 { "has_channel", Decoder_has_channel, METH_VARARGS,
1026 "Report whether a channel was supplied" },
1027 {NULL, NULL, 0, NULL}
1031 * Create the sigrokdecode.Decoder type.
1033 * @return The new type object.
1037 SRD_PRIV PyObject *srd_Decoder_type_new(void)
1040 PyType_Slot slots[] = {
1041 { Py_tp_doc, "sigrok Decoder base class" },
1042 { Py_tp_methods, Decoder_methods },
1043 { Py_tp_new, (void *)&PyType_GenericNew },
1047 PyGILState_STATE gstate;
1049 gstate = PyGILState_Ensure();
1051 spec.name = "sigrokdecode.Decoder";
1052 spec.basicsize = sizeof(srd_Decoder);
1054 spec.flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE;
1057 py_obj = PyType_FromSpec(&spec);
1059 PyGILState_Release(gstate);