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"
29 /* This is only used for nicer srd_dbg() output.
31 static const char *output_type_name(unsigned int idx)
33 static const char names[][16] = {
40 return names[MIN(idx, G_N_ELEMENTS(names) - 1)];
43 static int convert_annotation(struct srd_decoder_inst *di, PyObject *obj,
44 struct srd_proto_data *pdata)
47 struct srd_pd_output *pdo;
48 struct srd_proto_data_annotation *pda;
51 PyGILState_STATE gstate;
53 gstate = PyGILState_Ensure();
55 /* Should be a list of [annotation class, [string, ...]]. */
56 if (!PyList_Check(obj)) {
57 srd_err("Protocol decoder %s submitted an annotation that"
58 " is not a list", di->decoder->name);
62 /* Should have 2 elements. */
63 if (PyList_Size(obj) != 2) {
64 srd_err("Protocol decoder %s submitted annotation list with "
65 "%zd elements instead of 2", di->decoder->name,
71 * The first element should be an integer matching a previously
72 * registered annotation class.
74 py_tmp = PyList_GetItem(obj, 0);
75 if (!PyLong_Check(py_tmp)) {
76 srd_err("Protocol decoder %s submitted annotation list, but "
77 "first element was not an integer.", di->decoder->name);
80 ann_class = PyLong_AsLong(py_tmp);
81 if (!(pdo = g_slist_nth_data(di->decoder->annotations, ann_class))) {
82 srd_err("Protocol decoder %s submitted data to unregistered "
83 "annotation class %d.", di->decoder->name, ann_class);
87 /* Second element must be a list. */
88 py_tmp = PyList_GetItem(obj, 1);
89 if (!PyList_Check(py_tmp)) {
90 srd_err("Protocol decoder %s submitted annotation list, but "
91 "second element was not a list.", di->decoder->name);
94 if (py_strseq_to_char(py_tmp, &ann_text) != SRD_OK) {
95 srd_err("Protocol decoder %s submitted annotation list, but "
96 "second element was malformed.", di->decoder->name);
100 pda = g_malloc(sizeof(struct srd_proto_data_annotation));
101 pda->ann_class = ann_class;
102 pda->ann_text = ann_text;
105 PyGILState_Release(gstate);
110 PyGILState_Release(gstate);
112 return SRD_ERR_PYTHON;
115 static int convert_binary(struct srd_decoder_inst *di, PyObject *obj,
116 struct srd_proto_data *pdata)
118 struct srd_proto_data_binary *pdb;
122 char *class_name, *buf;
123 PyGILState_STATE gstate;
125 gstate = PyGILState_Ensure();
127 /* Should be a list of [binary class, bytes]. */
128 if (!PyList_Check(obj)) {
129 srd_err("Protocol decoder %s submitted non-list for SRD_OUTPUT_BINARY.",
134 /* Should have 2 elements. */
135 if (PyList_Size(obj) != 2) {
136 srd_err("Protocol decoder %s submitted SRD_OUTPUT_BINARY list "
137 "with %zd elements instead of 2", di->decoder->name,
142 /* The first element should be an integer. */
143 py_tmp = PyList_GetItem(obj, 0);
144 if (!PyLong_Check(py_tmp)) {
145 srd_err("Protocol decoder %s submitted SRD_OUTPUT_BINARY list, "
146 "but first element was not an integer.", di->decoder->name);
149 bin_class = PyLong_AsLong(py_tmp);
150 if (!(class_name = g_slist_nth_data(di->decoder->binary, bin_class))) {
151 srd_err("Protocol decoder %s submitted SRD_OUTPUT_BINARY with "
152 "unregistered binary class %d.", di->decoder->name, bin_class);
156 /* Second element should be bytes. */
157 py_tmp = PyList_GetItem(obj, 1);
158 if (!PyBytes_Check(py_tmp)) {
159 srd_err("Protocol decoder %s submitted SRD_OUTPUT_BINARY list, "
160 "but second element was not bytes.", di->decoder->name);
164 /* Consider an empty set of bytes a bug. */
165 if (PyBytes_Size(py_tmp) == 0) {
166 srd_err("Protocol decoder %s submitted SRD_OUTPUT_BINARY "
167 "with empty data set.", di->decoder->name);
171 pdb = g_malloc(sizeof(struct srd_proto_data_binary));
172 if (PyBytes_AsStringAndSize(py_tmp, &buf, &size) == -1)
175 PyGILState_Release(gstate);
177 pdb->bin_class = bin_class;
179 if (!(pdb->data = g_try_malloc(pdb->size)))
180 return SRD_ERR_MALLOC;
181 memcpy((void *)pdb->data, (const void *)buf, pdb->size);
187 PyGILState_Release(gstate);
189 return SRD_ERR_PYTHON;
192 static int convert_meta(struct srd_proto_data *pdata, PyObject *obj)
196 PyGILState_STATE gstate;
198 gstate = PyGILState_Ensure();
200 if (pdata->pdo->meta_type == G_VARIANT_TYPE_INT64) {
201 if (!PyLong_Check(obj)) {
202 PyErr_Format(PyExc_TypeError, "This output was registered "
203 "as 'int', but something else was passed.");
206 intvalue = PyLong_AsLongLong(obj);
207 if (PyErr_Occurred())
209 pdata->data = g_variant_new_int64(intvalue);
210 } else if (pdata->pdo->meta_type == G_VARIANT_TYPE_DOUBLE) {
211 if (!PyFloat_Check(obj)) {
212 PyErr_Format(PyExc_TypeError, "This output was registered "
213 "as 'float', but something else was passed.");
216 dvalue = PyFloat_AsDouble(obj);
217 if (PyErr_Occurred())
219 pdata->data = g_variant_new_double(dvalue);
222 PyGILState_Release(gstate);
227 PyGILState_Release(gstate);
229 return SRD_ERR_PYTHON;
232 static PyObject *Decoder_put(PyObject *self, PyObject *args)
235 PyObject *py_data, *py_res;
236 struct srd_decoder_inst *di, *next_di;
237 struct srd_pd_output *pdo;
238 struct srd_proto_data pdata;
239 uint64_t start_sample, end_sample;
241 struct srd_pd_callback *cb;
242 PyGILState_STATE gstate;
244 gstate = PyGILState_Ensure();
246 if (!(di = srd_inst_find_by_obj(NULL, self))) {
247 /* Shouldn't happen. */
248 srd_dbg("put(): self instance not found.");
252 if (!PyArg_ParseTuple(args, "KKiO", &start_sample, &end_sample,
253 &output_id, &py_data)) {
255 * This throws an exception, but by returning NULL here we let
256 * Python raise it. This results in a much better trace in
257 * controller.c on the decode() method call.
262 if (!(l = g_slist_nth(di->pd_output, output_id))) {
263 srd_err("Protocol decoder %s submitted invalid output ID %d.",
264 di->decoder->name, output_id);
269 srd_spew("Instance %s put %" PRIu64 "-%" PRIu64 " %s on oid %d.",
270 di->inst_id, start_sample, end_sample,
271 output_type_name(pdo->output_type), output_id);
273 pdata.start_sample = start_sample;
274 pdata.end_sample = end_sample;
278 switch (pdo->output_type) {
280 /* Annotations are only fed to callbacks. */
281 if ((cb = srd_pd_output_callback_find(di->sess, pdo->output_type))) {
282 /* Convert from PyDict to srd_proto_data_annotation. */
283 if (convert_annotation(di, py_data, &pdata) != SRD_OK) {
284 /* An error was already logged. */
287 Py_BEGIN_ALLOW_THREADS
288 cb->cb(&pdata, cb->cb_data);
292 case SRD_OUTPUT_PYTHON:
293 for (l = di->next_di; l; l = l->next) {
295 srd_spew("Sending %" PRIu64 "-%" PRIu64 " to instance %s",
296 start_sample, end_sample, next_di->inst_id);
297 if (!(py_res = PyObject_CallMethod(
298 next_di->py_inst, "decode", "KKO", start_sample,
299 end_sample, py_data))) {
300 srd_exception_catch("Calling %s decode() failed",
305 if ((cb = srd_pd_output_callback_find(di->sess, pdo->output_type))) {
306 /* Frontends aren't really supposed to get Python
307 * callbacks, but it's useful for testing. */
308 pdata.data = py_data;
309 cb->cb(&pdata, cb->cb_data);
312 case SRD_OUTPUT_BINARY:
313 if ((cb = srd_pd_output_callback_find(di->sess, pdo->output_type))) {
314 /* Convert from PyDict to srd_proto_data_binary. */
315 if (convert_binary(di, py_data, &pdata) != SRD_OK) {
316 /* An error was already logged. */
319 Py_BEGIN_ALLOW_THREADS
320 cb->cb(&pdata, cb->cb_data);
324 case SRD_OUTPUT_META:
325 if ((cb = srd_pd_output_callback_find(di->sess, pdo->output_type))) {
326 /* Annotations need converting from PyObject. */
327 if (convert_meta(&pdata, py_data) != SRD_OK) {
328 /* An exception was already set up. */
331 Py_BEGIN_ALLOW_THREADS
332 cb->cb(&pdata, cb->cb_data);
337 srd_err("Protocol decoder %s submitted invalid output type %d.",
338 di->decoder->name, pdo->output_type);
342 PyGILState_Release(gstate);
347 PyGILState_Release(gstate);
352 static PyObject *Decoder_register(PyObject *self, PyObject *args,
355 struct srd_decoder_inst *di;
356 struct srd_pd_output *pdo;
357 PyObject *py_new_output_id;
358 PyTypeObject *meta_type_py;
359 const GVariantType *meta_type_gv;
361 char *proto_id, *meta_name, *meta_descr;
362 char *keywords[] = {"output_type", "proto_id", "meta", NULL};
363 PyGILState_STATE gstate;
366 struct srd_pd_output *cmp;
368 gstate = PyGILState_Ensure();
372 meta_name = meta_descr = NULL;
374 if (!(di = srd_inst_find_by_obj(NULL, self))) {
375 PyErr_SetString(PyExc_Exception, "decoder instance not found");
379 /* Default to instance id, which defaults to class id. */
380 proto_id = di->inst_id;
381 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "i|s(Oss)", keywords,
382 &output_type, &proto_id,
383 &meta_type_py, &meta_name, &meta_descr)) {
384 /* Let Python raise this exception. */
388 /* Check if the meta value's type is supported. */
389 is_meta = output_type == SRD_OUTPUT_META;
391 if (meta_type_py == &PyLong_Type)
392 meta_type_gv = G_VARIANT_TYPE_INT64;
393 else if (meta_type_py == &PyFloat_Type)
394 meta_type_gv = G_VARIANT_TYPE_DOUBLE;
396 PyErr_Format(PyExc_TypeError, "Unsupported type.");
402 for (l = di->pd_output; l; l = l->next) {
404 if (cmp->output_type != output_type)
406 if (strcmp(cmp->proto_id, proto_id) != 0)
408 if (is_meta && cmp->meta_type != meta_type_gv)
410 if (is_meta && strcmp(cmp->meta_name, meta_name) != 0)
412 if (is_meta && strcmp(cmp->meta_descr, meta_descr) != 0)
418 py_new_output_id = Py_BuildValue("i", pdo->pdo_id);
419 PyGILState_Release(gstate);
420 return py_new_output_id;
423 srd_dbg("Instance %s creating new output type %d for %s.",
424 di->inst_id, output_type, proto_id);
426 pdo = g_malloc(sizeof(struct srd_pd_output));
428 /* pdo_id is just a simple index, nothing is deleted from this list anyway. */
429 pdo->pdo_id = g_slist_length(di->pd_output);
430 pdo->output_type = output_type;
432 pdo->proto_id = g_strdup(proto_id);
434 if (output_type == SRD_OUTPUT_META) {
435 pdo->meta_type = meta_type_gv;
436 pdo->meta_name = g_strdup(meta_name);
437 pdo->meta_descr = g_strdup(meta_descr);
440 di->pd_output = g_slist_append(di->pd_output, pdo);
441 py_new_output_id = Py_BuildValue("i", pdo->pdo_id);
443 PyGILState_Release(gstate);
445 return py_new_output_id;
448 PyGILState_Release(gstate);
453 static int get_term_type(const char *v)
457 return SRD_TERM_HIGH;
461 return SRD_TERM_RISING_EDGE;
463 return SRD_TERM_FALLING_EDGE;
465 return SRD_TERM_EITHER_EDGE;
467 return SRD_TERM_NO_EDGE;
474 * Get the pin values at the current sample number.
476 * @param di The decoder instance to use. Must not be NULL.
477 * The number of channels must be >= 1.
479 * @return A newly allocated PyTuple containing the pin values at the
480 * current sample number.
482 static PyObject *get_current_pinvalues(const struct srd_decoder_inst *di)
486 const uint8_t *sample_pos;
487 int byte_offset, bit_offset;
488 PyObject *py_pinvalues;
489 PyGILState_STATE gstate;
491 gstate = PyGILState_Ensure();
494 srd_err("Invalid decoder instance.");
495 PyGILState_Release(gstate);
499 py_pinvalues = PyTuple_New(di->dec_num_channels);
501 for (i = 0; i < di->dec_num_channels; i++) {
502 /* A channelmap value of -1 means "unused optional channel". */
503 if (di->dec_channelmap[i] == -1) {
504 /* Value of unused channel is 0xff, instead of 0 or 1. */
505 PyTuple_SetItem(py_pinvalues, i, PyLong_FromLong(0xff));
507 sample_pos = di->inbuf + ((di->abs_cur_samplenum - di->abs_start_samplenum) * di->data_unitsize);
508 byte_offset = di->dec_channelmap[i] / 8;
509 bit_offset = di->dec_channelmap[i] % 8;
510 sample = *(sample_pos + byte_offset) & (1 << bit_offset) ? 1 : 0;
511 PyTuple_SetItem(py_pinvalues, i, PyLong_FromLong(sample));
515 PyGILState_Release(gstate);
521 * Create a list of terms in the specified condition.
523 * If there are no terms in the condition, 'term_list' will be NULL.
525 * @param py_dict A Python dict containing terms. Must not be NULL.
526 * @param term_list Pointer to a GSList which will be set to the newly
527 * created list of terms. Must not be NULL.
529 * @return SRD_OK upon success, a negative error code otherwise.
531 static int create_term_list(PyObject *py_dict, GSList **term_list)
534 PyObject *py_key, *py_value;
535 struct srd_term *term;
536 uint64_t num_samples_to_skip;
538 PyGILState_STATE gstate;
540 if (!py_dict || !term_list)
543 /* "Create" an empty GSList of terms. */
546 gstate = PyGILState_Ensure();
548 /* Iterate over all items in the current dict. */
549 while (PyDict_Next(py_dict, &pos, &py_key, &py_value)) {
550 /* Check whether the current key is a string or a number. */
551 if (PyLong_Check(py_key)) {
552 /* The key is a number. */
553 /* TODO: Check if the number is a valid channel. */
554 /* Get the value string. */
555 if ((py_pydictitem_as_str(py_dict, py_key, &term_str)) != SRD_OK) {
556 srd_err("Failed to get the value.");
559 term = g_malloc0(sizeof(struct srd_term));
560 term->type = get_term_type(term_str);
561 term->channel = PyLong_AsLong(py_key);
563 } else if (PyUnicode_Check(py_key)) {
564 /* The key is a string. */
565 /* TODO: Check if it's "skip". */
566 if ((py_pydictitem_as_long(py_dict, py_key, &num_samples_to_skip)) != SRD_OK) {
567 srd_err("Failed to get number of samples to skip.");
570 term = g_malloc0(sizeof(struct srd_term));
571 term->type = SRD_TERM_SKIP;
572 term->num_samples_to_skip = num_samples_to_skip;
573 term->num_samples_already_skipped = 0;
575 srd_err("Term key is neither a string nor a number.");
579 /* Add the term to the list of terms. */
580 *term_list = g_slist_append(*term_list, term);
583 PyGILState_Release(gstate);
588 PyGILState_Release(gstate);
594 * Replace the current condition list with the new one.
596 * @param self TODO. Must not be NULL.
597 * @param args TODO. Must not be NULL.
599 * @retval SRD_OK The new condition list was set successfully.
600 * @retval SRD_ERR There was an error setting the new condition list.
601 * The contents of di->condition_list are undefined.
604 static int set_new_condition_list(PyObject *self, PyObject *args)
606 struct srd_decoder_inst *di;
608 PyObject *py_conditionlist, *py_conds, *py_dict;
609 int i, num_conditions, ret;
610 PyGILState_STATE gstate;
615 gstate = PyGILState_Ensure();
617 /* Get the decoder instance. */
618 if (!(di = srd_inst_find_by_obj(NULL, self))) {
619 PyErr_SetString(PyExc_Exception, "decoder instance not found");
624 * Return an error condition from .wait() when termination is
625 * requested, such that decode() will terminate.
627 if (di->want_wait_terminate) {
628 srd_dbg("%s: %s: Skip (want_term).", di->inst_id, __func__);
633 * Parse the argument of self.wait() into 'py_conds', and check
634 * the data type. The argument is optional, None is assumed in
635 * its absence. None or an empty dict or an empty list mean that
636 * there is no condition, and the next available sample shall
637 * get returned to the caller.
640 if (!PyArg_ParseTuple(args, "|O", &py_conds)) {
641 /* Let Python raise this exception. */
644 if (py_conds == Py_None) {
645 /* 'py_conds' is None. */
647 } else if (PyList_Check(py_conds)) {
648 /* 'py_conds' is a list. */
649 py_conditionlist = py_conds;
650 num_conditions = PyList_Size(py_conditionlist);
651 if (num_conditions == 0)
652 goto ret_9999; /* The PD invoked self.wait([]). */
653 Py_IncRef(py_conditionlist);
654 } else if (PyDict_Check(py_conds)) {
655 /* 'py_conds' is a dict. */
656 if (PyDict_Size(py_conds) == 0)
657 goto ret_9999; /* The PD invoked self.wait({}). */
658 /* Make a list and put the dict in there for convenience. */
659 py_conditionlist = PyList_New(1);
661 PyList_SetItem(py_conditionlist, 0, py_conds);
664 srd_err("Condition list is neither a list nor a dict.");
668 /* Free the old condition list. */
669 condition_list_free(di);
673 /* Iterate over the conditions, set di->condition_list accordingly. */
674 for (i = 0; i < num_conditions; i++) {
675 /* Get a condition (dict) from the condition list. */
676 py_dict = PyList_GetItem(py_conditionlist, i);
677 if (!PyDict_Check(py_dict)) {
678 srd_err("Condition is not a dict.");
683 /* Create the list of terms in this condition. */
684 if ((ret = create_term_list(py_dict, &term_list)) < 0)
687 /* Add the new condition to the PD instance's condition list. */
688 di->condition_list = g_slist_append(di->condition_list, term_list);
691 Py_DecRef(py_conditionlist);
693 PyGILState_Release(gstate);
698 PyGILState_Release(gstate);
703 PyGILState_Release(gstate);
709 * Create a SKIP condition list for condition-less .wait() calls.
711 * @param di Decoder instance.
712 * @param count Number of samples to skip.
714 * @retval SRD_OK The new condition list was set successfully.
715 * @retval SRD_ERR There was an error setting the new condition list.
716 * The contents of di->condition_list are undefined.
718 * This routine is a reduced and specialized version of the @ref
719 * set_new_condition_list() and @ref create_term_list() routines which
720 * gets invoked when .wait() was called without specifications for
721 * conditions. This minor duplication of the SKIP term list creation
722 * simplifies the logic and avoids the creation of expensive Python
723 * objects with "constant" values which the caller did not pass in the
724 * first place. It results in maximum sharing of match handling code
727 static int set_skip_condition(struct srd_decoder_inst *di, uint64_t count)
729 struct srd_term *term;
732 condition_list_free(di);
733 term = g_malloc0(sizeof(*term));
734 term->type = SRD_TERM_SKIP;
735 term->num_samples_to_skip = count;
736 term->num_samples_already_skipped = 0;
737 term_list = g_slist_append(NULL, term);
738 di->condition_list = g_slist_append(di->condition_list, term_list);
743 static PyObject *Decoder_wait(PyObject *self, PyObject *args)
748 gboolean found_match;
749 struct srd_decoder_inst *di;
750 PyObject *py_pinvalues, *py_matched;
751 PyGILState_STATE gstate;
756 gstate = PyGILState_Ensure();
758 if (!(di = srd_inst_find_by_obj(NULL, self))) {
759 PyErr_SetString(PyExc_Exception, "decoder instance not found");
760 PyGILState_Release(gstate);
764 ret = set_new_condition_list(self, args);
766 srd_dbg("%s: %s: Aborting wait().", di->inst_id, __func__);
771 * Empty condition list, automatic match. Arrange for the
772 * execution of regular match handling code paths such that
773 * the next available sample is returned to the caller.
774 * Make sure to skip one sample when "anywhere within the
775 * stream", yet make sure to not skip sample number 0.
777 if (di->abs_cur_samplenum)
779 else if (!di->condition_list)
783 ret = set_skip_condition(di, skip_count);
785 srd_dbg("%s: %s: Cannot setup condition-less wait().",
786 di->inst_id, __func__);
793 Py_BEGIN_ALLOW_THREADS
795 /* Wait for new samples to process, or termination request. */
796 g_mutex_lock(&di->data_mutex);
797 while (!di->got_new_samples && !di->want_wait_terminate)
798 g_cond_wait(&di->got_new_samples_cond, &di->data_mutex);
801 * Check whether any of the current condition(s) match.
802 * Arrange for termination requests to take a code path which
803 * won't find new samples to process, pretends to have processed
804 * previously stored samples, and returns to the main thread,
805 * while the termination request still gets signalled.
808 ret = process_samples_until_condition_match(di, &found_match);
812 /* If there's a match, set self.samplenum etc. and return. */
814 /* Set self.samplenum to the (absolute) sample number that matched. */
815 PyObject_SetAttrString(di->py_inst, "samplenum",
816 PyLong_FromLong(di->abs_cur_samplenum));
818 if (di->match_array && di->match_array->len > 0) {
819 py_matched = PyTuple_New(di->match_array->len);
820 for (i = 0; i < di->match_array->len; i++)
821 PyTuple_SetItem(py_matched, i, PyBool_FromLong(di->match_array->data[i]));
822 PyObject_SetAttrString(di->py_inst, "matched", py_matched);
823 match_array_free(di);
825 PyObject_SetAttrString(di->py_inst, "matched", Py_None);
828 py_pinvalues = get_current_pinvalues(di);
830 g_mutex_unlock(&di->data_mutex);
832 PyGILState_Release(gstate);
837 /* No match, reset state for the next chunk. */
838 di->got_new_samples = FALSE;
839 di->handled_all_samples = TRUE;
840 di->abs_start_samplenum = 0;
841 di->abs_end_samplenum = 0;
845 /* Signal the main thread that we handled all samples. */
846 g_cond_signal(&di->handled_all_samples_cond);
849 * When termination of wait() and decode() was requested,
850 * then exit the loop after releasing the mutex.
852 if (di->want_wait_terminate) {
853 srd_dbg("%s: %s: Will return from wait().",
854 di->inst_id, __func__);
855 g_mutex_unlock(&di->data_mutex);
859 g_mutex_unlock(&di->data_mutex);
862 PyGILState_Release(gstate);
867 PyGILState_Release(gstate);
873 * Return whether the specified channel was supplied to the decoder.
875 * @param self TODO. Must not be NULL.
876 * @param args TODO. Must not be NULL.
878 * @retval Py_True The channel has been supplied by the frontend.
879 * @retval Py_False The channel has been supplied by the frontend.
880 * @retval NULL An error occurred.
882 static PyObject *Decoder_has_channel(PyObject *self, PyObject *args)
885 struct srd_decoder_inst *di;
886 PyGILState_STATE gstate;
891 gstate = PyGILState_Ensure();
893 if (!(di = srd_inst_find_by_obj(NULL, self))) {
894 PyErr_SetString(PyExc_Exception, "decoder instance not found");
899 * Get the integer argument of self.has_channel(). Check for
900 * the range of supported PD input channel numbers.
902 if (!PyArg_ParseTuple(args, "i", &idx)) {
903 /* Let Python raise this exception. */
907 count = g_slist_length(di->decoder->channels) +
908 g_slist_length(di->decoder->opt_channels);
909 if (idx < 0 || idx >= count) {
910 srd_err("Invalid index %d, PD channel count %d.", idx, count);
911 PyErr_SetString(PyExc_IndexError, "invalid channel index");
915 PyGILState_Release(gstate);
917 return (di->dec_channelmap[idx] == -1) ? Py_False : Py_True;
920 PyGILState_Release(gstate);
925 static PyMethodDef Decoder_methods[] = {
926 {"put", Decoder_put, METH_VARARGS,
927 "Accepts a dictionary with the following keys: startsample, endsample, data"},
928 {"register", (PyCFunction)Decoder_register, METH_VARARGS|METH_KEYWORDS,
929 "Register a new output stream"},
930 {"wait", Decoder_wait, METH_VARARGS,
931 "Wait for one or more conditions to occur"},
932 {"has_channel", Decoder_has_channel, METH_VARARGS,
933 "Report whether a channel was supplied"},
934 {NULL, NULL, 0, NULL}
938 * Create the sigrokdecode.Decoder type.
940 * @return The new type object.
944 SRD_PRIV PyObject *srd_Decoder_type_new(void)
947 PyType_Slot slots[] = {
948 { Py_tp_doc, "sigrok Decoder base class" },
949 { Py_tp_methods, Decoder_methods },
950 { Py_tp_new, (void *)&PyType_GenericNew },
954 PyGILState_STATE gstate;
956 gstate = PyGILState_Ensure();
958 spec.name = "sigrokdecode.Decoder";
959 spec.basicsize = sizeof(srd_Decoder);
961 spec.flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE;
964 py_obj = PyType_FromSpec(&spec);
966 PyGILState_Release(gstate);