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 void release_annotation(struct srd_proto_data_annotation *pda)
48 g_strfreev(pda->ann_text);
52 static int convert_annotation(struct srd_decoder_inst *di, PyObject *obj,
53 struct srd_proto_data *pdata)
56 struct srd_pd_output *pdo;
57 struct srd_proto_data_annotation *pda;
60 PyGILState_STATE gstate;
62 gstate = PyGILState_Ensure();
64 /* Should be a list of [annotation class, [string, ...]]. */
65 if (!PyList_Check(obj)) {
66 srd_err("Protocol decoder %s submitted an annotation that"
67 " is not a list", di->decoder->name);
71 /* Should have 2 elements. */
72 if (PyList_Size(obj) != 2) {
73 srd_err("Protocol decoder %s submitted annotation list with "
74 "%zd elements instead of 2", di->decoder->name,
80 * The first element should be an integer matching a previously
81 * registered annotation class.
83 py_tmp = PyList_GetItem(obj, 0);
84 if (!PyLong_Check(py_tmp)) {
85 srd_err("Protocol decoder %s submitted annotation list, but "
86 "first element was not an integer.", di->decoder->name);
89 ann_class = PyLong_AsLong(py_tmp);
90 if (!(pdo = g_slist_nth_data(di->decoder->annotations, ann_class))) {
91 srd_err("Protocol decoder %s submitted data to unregistered "
92 "annotation class %d.", di->decoder->name, ann_class);
96 /* Second element must be a list. */
97 py_tmp = PyList_GetItem(obj, 1);
98 if (!PyList_Check(py_tmp)) {
99 srd_err("Protocol decoder %s submitted annotation list, but "
100 "second element was not a list.", di->decoder->name);
103 if (py_strseq_to_char(py_tmp, &ann_text) != SRD_OK) {
104 srd_err("Protocol decoder %s submitted annotation list, but "
105 "second element was malformed.", di->decoder->name);
109 pda = g_malloc(sizeof(struct srd_proto_data_annotation));
110 pda->ann_class = ann_class;
111 pda->ann_text = ann_text;
114 PyGILState_Release(gstate);
119 PyGILState_Release(gstate);
121 return SRD_ERR_PYTHON;
124 static void release_binary(struct srd_proto_data_binary *pdb)
129 g_free((void *)pdb->data);
133 static int convert_binary(struct srd_decoder_inst *di, PyObject *obj,
134 struct srd_proto_data *pdata)
136 struct srd_proto_data_binary *pdb;
140 char *class_name, *buf;
141 PyGILState_STATE gstate;
143 gstate = PyGILState_Ensure();
145 /* Should be a list of [binary class, bytes]. */
146 if (!PyList_Check(obj)) {
147 srd_err("Protocol decoder %s submitted non-list for SRD_OUTPUT_BINARY.",
152 /* Should have 2 elements. */
153 if (PyList_Size(obj) != 2) {
154 srd_err("Protocol decoder %s submitted SRD_OUTPUT_BINARY list "
155 "with %zd elements instead of 2", di->decoder->name,
160 /* The first element should be an integer. */
161 py_tmp = PyList_GetItem(obj, 0);
162 if (!PyLong_Check(py_tmp)) {
163 srd_err("Protocol decoder %s submitted SRD_OUTPUT_BINARY list, "
164 "but first element was not an integer.", di->decoder->name);
167 bin_class = PyLong_AsLong(py_tmp);
168 if (!(class_name = g_slist_nth_data(di->decoder->binary, bin_class))) {
169 srd_err("Protocol decoder %s submitted SRD_OUTPUT_BINARY with "
170 "unregistered binary class %d.", di->decoder->name, bin_class);
174 /* Second element should be bytes. */
175 py_tmp = PyList_GetItem(obj, 1);
176 if (!PyBytes_Check(py_tmp)) {
177 srd_err("Protocol decoder %s submitted SRD_OUTPUT_BINARY list, "
178 "but second element was not bytes.", di->decoder->name);
182 /* Consider an empty set of bytes a bug. */
183 if (PyBytes_Size(py_tmp) == 0) {
184 srd_err("Protocol decoder %s submitted SRD_OUTPUT_BINARY "
185 "with empty data set.", di->decoder->name);
189 pdb = g_malloc(sizeof(struct srd_proto_data_binary));
190 if (PyBytes_AsStringAndSize(py_tmp, &buf, &size) == -1)
193 PyGILState_Release(gstate);
195 pdb->bin_class = bin_class;
197 if (!(pdb->data = g_try_malloc(pdb->size)))
198 return SRD_ERR_MALLOC;
199 memcpy((void *)pdb->data, (const void *)buf, pdb->size);
205 PyGILState_Release(gstate);
207 return SRD_ERR_PYTHON;
210 static int convert_meta(struct srd_proto_data *pdata, PyObject *obj)
214 PyGILState_STATE gstate;
216 gstate = PyGILState_Ensure();
218 if (pdata->pdo->meta_type == G_VARIANT_TYPE_INT64) {
219 if (!PyLong_Check(obj)) {
220 PyErr_Format(PyExc_TypeError, "This output was registered "
221 "as 'int', but something else was passed.");
224 intvalue = PyLong_AsLongLong(obj);
225 if (PyErr_Occurred())
227 pdata->data = g_variant_new_int64(intvalue);
228 } else if (pdata->pdo->meta_type == G_VARIANT_TYPE_DOUBLE) {
229 if (!PyFloat_Check(obj)) {
230 PyErr_Format(PyExc_TypeError, "This output was registered "
231 "as 'float', but something else was passed.");
234 dvalue = PyFloat_AsDouble(obj);
235 if (PyErr_Occurred())
237 pdata->data = g_variant_new_double(dvalue);
240 PyGILState_Release(gstate);
245 PyGILState_Release(gstate);
247 return SRD_ERR_PYTHON;
250 static void release_meta(GVariant *gvar)
254 g_variant_unref(gvar);
257 static PyObject *Decoder_put(PyObject *self, PyObject *args)
260 PyObject *py_data, *py_res;
261 struct srd_decoder_inst *di, *next_di;
262 struct srd_pd_output *pdo;
263 struct srd_proto_data pdata;
264 uint64_t start_sample, end_sample;
266 struct srd_pd_callback *cb;
267 PyGILState_STATE gstate;
271 gstate = PyGILState_Ensure();
273 if (!(di = srd_inst_find_by_obj(NULL, self))) {
274 /* Shouldn't happen. */
275 srd_dbg("put(): self instance not found.");
279 if (!PyArg_ParseTuple(args, "KKiO", &start_sample, &end_sample,
280 &output_id, &py_data)) {
282 * This throws an exception, but by returning NULL here we let
283 * Python raise it. This results in a much better trace in
284 * controller.c on the decode() method call.
289 if (!(l = g_slist_nth(di->pd_output, output_id))) {
290 srd_err("Protocol decoder %s submitted invalid output ID %d.",
291 di->decoder->name, output_id);
296 srd_spew("Instance %s put %" PRIu64 "-%" PRIu64 " %s on oid %d.",
297 di->inst_id, start_sample, end_sample,
298 output_type_name(pdo->output_type), output_id);
300 pdata.start_sample = start_sample;
301 pdata.end_sample = end_sample;
305 switch (pdo->output_type) {
307 /* Annotations are only fed to callbacks. */
308 if ((cb = srd_pd_output_callback_find(di->sess, pdo->output_type))) {
309 /* Convert from PyDict to srd_proto_data_annotation. */
310 if (convert_annotation(di, py_data, &pdata) != SRD_OK) {
311 /* An error was already logged. */
314 Py_BEGIN_ALLOW_THREADS
315 cb->cb(&pdata, cb->cb_data);
317 release_annotation(pdata.data);
320 case SRD_OUTPUT_PYTHON:
321 for (l = di->next_di; l; l = l->next) {
323 srd_spew("Sending %" PRIu64 "-%" PRIu64 " to instance %s",
324 start_sample, end_sample, next_di->inst_id);
325 if (!(py_res = PyObject_CallMethod(
326 next_di->py_inst, "decode", "KKO", start_sample,
327 end_sample, py_data))) {
328 srd_exception_catch("Calling %s decode() failed",
333 if ((cb = srd_pd_output_callback_find(di->sess, pdo->output_type))) {
334 /* Frontends aren't really supposed to get Python
335 * callbacks, but it's useful for testing. */
336 pdata.data = py_data;
337 cb->cb(&pdata, cb->cb_data);
340 case SRD_OUTPUT_BINARY:
341 if ((cb = srd_pd_output_callback_find(di->sess, pdo->output_type))) {
342 /* Convert from PyDict to srd_proto_data_binary. */
343 if (convert_binary(di, py_data, &pdata) != SRD_OK) {
344 /* An error was already logged. */
347 Py_BEGIN_ALLOW_THREADS
348 cb->cb(&pdata, cb->cb_data);
350 release_binary(pdata.data);
353 case SRD_OUTPUT_META:
354 if ((cb = srd_pd_output_callback_find(di->sess, pdo->output_type))) {
355 /* Annotations need converting from PyObject. */
356 if (convert_meta(&pdata, py_data) != SRD_OK) {
357 /* An exception was already set up. */
360 Py_BEGIN_ALLOW_THREADS
361 cb->cb(&pdata, cb->cb_data);
363 release_meta(pdata.data);
367 srd_err("Protocol decoder %s submitted invalid output type %d.",
368 di->decoder->name, pdo->output_type);
372 PyGILState_Release(gstate);
377 PyGILState_Release(gstate);
382 static PyObject *Decoder_register(PyObject *self, PyObject *args,
385 struct srd_decoder_inst *di;
386 struct srd_pd_output *pdo;
387 PyObject *py_new_output_id;
388 PyTypeObject *meta_type_py;
389 const GVariantType *meta_type_gv;
391 char *proto_id, *meta_name, *meta_descr;
392 char *keywords[] = {"output_type", "proto_id", "meta", NULL};
393 PyGILState_STATE gstate;
396 struct srd_pd_output *cmp;
398 gstate = PyGILState_Ensure();
402 meta_name = meta_descr = NULL;
404 if (!(di = srd_inst_find_by_obj(NULL, self))) {
405 PyErr_SetString(PyExc_Exception, "decoder instance not found");
409 /* Default to instance id, which defaults to class id. */
410 proto_id = di->inst_id;
411 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "i|s(Oss)", keywords,
412 &output_type, &proto_id,
413 &meta_type_py, &meta_name, &meta_descr)) {
414 /* Let Python raise this exception. */
418 /* Check if the meta value's type is supported. */
419 is_meta = output_type == SRD_OUTPUT_META;
421 if (meta_type_py == &PyLong_Type)
422 meta_type_gv = G_VARIANT_TYPE_INT64;
423 else if (meta_type_py == &PyFloat_Type)
424 meta_type_gv = G_VARIANT_TYPE_DOUBLE;
426 PyErr_Format(PyExc_TypeError, "Unsupported type.");
432 for (l = di->pd_output; l; l = l->next) {
434 if (cmp->output_type != output_type)
436 if (strcmp(cmp->proto_id, proto_id) != 0)
438 if (is_meta && cmp->meta_type != meta_type_gv)
440 if (is_meta && strcmp(cmp->meta_name, meta_name) != 0)
442 if (is_meta && strcmp(cmp->meta_descr, meta_descr) != 0)
448 py_new_output_id = Py_BuildValue("i", pdo->pdo_id);
449 PyGILState_Release(gstate);
450 return py_new_output_id;
453 srd_dbg("Instance %s creating new output type %d for %s.",
454 di->inst_id, output_type, proto_id);
456 pdo = g_malloc(sizeof(struct srd_pd_output));
458 /* pdo_id is just a simple index, nothing is deleted from this list anyway. */
459 pdo->pdo_id = g_slist_length(di->pd_output);
460 pdo->output_type = output_type;
462 pdo->proto_id = g_strdup(proto_id);
464 if (output_type == SRD_OUTPUT_META) {
465 pdo->meta_type = meta_type_gv;
466 pdo->meta_name = g_strdup(meta_name);
467 pdo->meta_descr = g_strdup(meta_descr);
470 di->pd_output = g_slist_append(di->pd_output, pdo);
471 py_new_output_id = Py_BuildValue("i", pdo->pdo_id);
473 PyGILState_Release(gstate);
475 return py_new_output_id;
478 PyGILState_Release(gstate);
483 static int get_term_type(const char *v)
487 return SRD_TERM_HIGH;
491 return SRD_TERM_RISING_EDGE;
493 return SRD_TERM_FALLING_EDGE;
495 return SRD_TERM_EITHER_EDGE;
497 return SRD_TERM_NO_EDGE;
504 * Get the pin values at the current sample number.
506 * @param di The decoder instance to use. Must not be NULL.
507 * The number of channels must be >= 1.
509 * @return A newly allocated PyTuple containing the pin values at the
510 * current sample number.
512 static PyObject *get_current_pinvalues(const struct srd_decoder_inst *di)
516 const uint8_t *sample_pos;
517 int byte_offset, bit_offset;
518 PyObject *py_pinvalues;
519 PyGILState_STATE gstate;
521 gstate = PyGILState_Ensure();
524 srd_err("Invalid decoder instance.");
525 PyGILState_Release(gstate);
529 py_pinvalues = PyTuple_New(di->dec_num_channels);
531 for (i = 0; i < di->dec_num_channels; i++) {
532 /* A channelmap value of -1 means "unused optional channel". */
533 if (di->dec_channelmap[i] == -1) {
534 /* Value of unused channel is 0xff, instead of 0 or 1. */
535 PyTuple_SetItem(py_pinvalues, i, PyLong_FromLong(0xff));
537 sample_pos = di->inbuf + ((di->abs_cur_samplenum - di->abs_start_samplenum) * di->data_unitsize);
538 byte_offset = di->dec_channelmap[i] / 8;
539 bit_offset = di->dec_channelmap[i] % 8;
540 sample = *(sample_pos + byte_offset) & (1 << bit_offset) ? 1 : 0;
541 PyTuple_SetItem(py_pinvalues, i, PyLong_FromLong(sample));
545 PyGILState_Release(gstate);
551 * Create a list of terms in the specified condition.
553 * If there are no terms in the condition, 'term_list' will be NULL.
555 * @param py_dict A Python dict containing terms. Must not be NULL.
556 * @param term_list Pointer to a GSList which will be set to the newly
557 * created list of terms. Must not be NULL.
559 * @return SRD_OK upon success, a negative error code otherwise.
561 static int create_term_list(PyObject *py_dict, GSList **term_list)
564 PyObject *py_key, *py_value;
565 struct srd_term *term;
566 uint64_t num_samples_to_skip;
568 PyGILState_STATE gstate;
570 if (!py_dict || !term_list)
573 /* "Create" an empty GSList of terms. */
576 gstate = PyGILState_Ensure();
578 /* Iterate over all items in the current dict. */
579 while (PyDict_Next(py_dict, &pos, &py_key, &py_value)) {
580 /* Check whether the current key is a string or a number. */
581 if (PyLong_Check(py_key)) {
582 /* The key is a number. */
583 /* TODO: Check if the number is a valid channel. */
584 /* Get the value string. */
585 if ((py_pydictitem_as_str(py_dict, py_key, &term_str)) != SRD_OK) {
586 srd_err("Failed to get the value.");
589 term = g_malloc0(sizeof(struct srd_term));
590 term->type = get_term_type(term_str);
591 term->channel = PyLong_AsLong(py_key);
593 } else if (PyUnicode_Check(py_key)) {
594 /* The key is a string. */
595 /* TODO: Check if it's "skip". */
596 if ((py_pydictitem_as_long(py_dict, py_key, &num_samples_to_skip)) != SRD_OK) {
597 srd_err("Failed to get number of samples to skip.");
600 term = g_malloc0(sizeof(struct srd_term));
601 term->type = SRD_TERM_SKIP;
602 term->num_samples_to_skip = num_samples_to_skip;
603 term->num_samples_already_skipped = 0;
605 srd_err("Term key is neither a string nor a number.");
609 /* Add the term to the list of terms. */
610 *term_list = g_slist_append(*term_list, term);
613 PyGILState_Release(gstate);
618 PyGILState_Release(gstate);
624 * Replace the current condition list with the new one.
626 * @param self TODO. Must not be NULL.
627 * @param args TODO. Must not be NULL.
629 * @retval SRD_OK The new condition list was set successfully.
630 * @retval SRD_ERR There was an error setting the new condition list.
631 * The contents of di->condition_list are undefined.
634 static int set_new_condition_list(PyObject *self, PyObject *args)
636 struct srd_decoder_inst *di;
638 PyObject *py_conditionlist, *py_conds, *py_dict;
639 int i, num_conditions, ret;
640 PyGILState_STATE gstate;
645 gstate = PyGILState_Ensure();
647 /* Get the decoder instance. */
648 if (!(di = srd_inst_find_by_obj(NULL, self))) {
649 PyErr_SetString(PyExc_Exception, "decoder instance not found");
654 * Return an error condition from .wait() when termination is
655 * requested, such that decode() will terminate.
657 if (di->want_wait_terminate) {
658 srd_dbg("%s: %s: Skip (want_term).", di->inst_id, __func__);
663 * Parse the argument of self.wait() into 'py_conds', and check
664 * the data type. The argument is optional, None is assumed in
665 * its absence. None or an empty dict or an empty list mean that
666 * there is no condition, and the next available sample shall
667 * get returned to the caller.
670 if (!PyArg_ParseTuple(args, "|O", &py_conds)) {
671 /* Let Python raise this exception. */
674 if (py_conds == Py_None) {
675 /* 'py_conds' is None. */
677 } else if (PyList_Check(py_conds)) {
678 /* 'py_conds' is a list. */
679 py_conditionlist = py_conds;
680 num_conditions = PyList_Size(py_conditionlist);
681 if (num_conditions == 0)
682 goto ret_9999; /* The PD invoked self.wait([]). */
683 Py_IncRef(py_conditionlist);
684 } else if (PyDict_Check(py_conds)) {
685 /* 'py_conds' is a dict. */
686 if (PyDict_Size(py_conds) == 0)
687 goto ret_9999; /* The PD invoked self.wait({}). */
688 /* Make a list and put the dict in there for convenience. */
689 py_conditionlist = PyList_New(1);
691 PyList_SetItem(py_conditionlist, 0, py_conds);
694 srd_err("Condition list is neither a list nor a dict.");
698 /* Free the old condition list. */
699 condition_list_free(di);
703 /* Iterate over the conditions, set di->condition_list accordingly. */
704 for (i = 0; i < num_conditions; i++) {
705 /* Get a condition (dict) from the condition list. */
706 py_dict = PyList_GetItem(py_conditionlist, i);
707 if (!PyDict_Check(py_dict)) {
708 srd_err("Condition is not a dict.");
713 /* Create the list of terms in this condition. */
714 if ((ret = create_term_list(py_dict, &term_list)) < 0)
717 /* Add the new condition to the PD instance's condition list. */
718 di->condition_list = g_slist_append(di->condition_list, term_list);
721 Py_DecRef(py_conditionlist);
723 PyGILState_Release(gstate);
728 PyGILState_Release(gstate);
733 PyGILState_Release(gstate);
739 * Create a SKIP condition list for condition-less .wait() calls.
741 * @param di Decoder instance.
742 * @param count Number of samples to skip.
744 * @retval SRD_OK The new condition list was set successfully.
745 * @retval SRD_ERR There was an error setting the new condition list.
746 * The contents of di->condition_list are undefined.
748 * This routine is a reduced and specialized version of the @ref
749 * set_new_condition_list() and @ref create_term_list() routines which
750 * gets invoked when .wait() was called without specifications for
751 * conditions. This minor duplication of the SKIP term list creation
752 * simplifies the logic and avoids the creation of expensive Python
753 * objects with "constant" values which the caller did not pass in the
754 * first place. It results in maximum sharing of match handling code
757 static int set_skip_condition(struct srd_decoder_inst *di, uint64_t count)
759 struct srd_term *term;
762 condition_list_free(di);
763 term = g_malloc0(sizeof(*term));
764 term->type = SRD_TERM_SKIP;
765 term->num_samples_to_skip = count;
766 term->num_samples_already_skipped = 0;
767 term_list = g_slist_append(NULL, term);
768 di->condition_list = g_slist_append(di->condition_list, term_list);
773 static PyObject *Decoder_wait(PyObject *self, PyObject *args)
778 gboolean found_match;
779 struct srd_decoder_inst *di;
780 PyObject *py_pinvalues, *py_matched;
781 PyGILState_STATE gstate;
786 gstate = PyGILState_Ensure();
788 if (!(di = srd_inst_find_by_obj(NULL, self))) {
789 PyErr_SetString(PyExc_Exception, "decoder instance not found");
790 PyGILState_Release(gstate);
794 ret = set_new_condition_list(self, args);
796 srd_dbg("%s: %s: Aborting wait().", di->inst_id, __func__);
801 * Empty condition list, automatic match. Arrange for the
802 * execution of regular match handling code paths such that
803 * the next available sample is returned to the caller.
804 * Make sure to skip one sample when "anywhere within the
805 * stream", yet make sure to not skip sample number 0.
807 if (di->abs_cur_samplenum)
809 else if (!di->condition_list)
813 ret = set_skip_condition(di, skip_count);
815 srd_dbg("%s: %s: Cannot setup condition-less wait().",
816 di->inst_id, __func__);
823 Py_BEGIN_ALLOW_THREADS
825 /* Wait for new samples to process, or termination request. */
826 g_mutex_lock(&di->data_mutex);
827 while (!di->got_new_samples && !di->want_wait_terminate)
828 g_cond_wait(&di->got_new_samples_cond, &di->data_mutex);
831 * Check whether any of the current condition(s) match.
832 * Arrange for termination requests to take a code path which
833 * won't find new samples to process, pretends to have processed
834 * previously stored samples, and returns to the main thread,
835 * while the termination request still gets signalled.
838 ret = process_samples_until_condition_match(di, &found_match);
842 /* If there's a match, set self.samplenum etc. and return. */
844 /* Set self.samplenum to the (absolute) sample number that matched. */
845 PyObject_SetAttrString(di->py_inst, "samplenum",
846 PyLong_FromLong(di->abs_cur_samplenum));
848 if (di->match_array && di->match_array->len > 0) {
849 py_matched = PyTuple_New(di->match_array->len);
850 for (i = 0; i < di->match_array->len; i++)
851 PyTuple_SetItem(py_matched, i, PyBool_FromLong(di->match_array->data[i]));
852 PyObject_SetAttrString(di->py_inst, "matched", py_matched);
853 match_array_free(di);
855 PyObject_SetAttrString(di->py_inst, "matched", Py_None);
858 py_pinvalues = get_current_pinvalues(di);
860 g_mutex_unlock(&di->data_mutex);
862 PyGILState_Release(gstate);
867 /* No match, reset state for the next chunk. */
868 di->got_new_samples = FALSE;
869 di->handled_all_samples = TRUE;
870 di->abs_start_samplenum = 0;
871 di->abs_end_samplenum = 0;
875 /* Signal the main thread that we handled all samples. */
876 g_cond_signal(&di->handled_all_samples_cond);
879 * When termination of wait() and decode() was requested,
880 * then exit the loop after releasing the mutex.
882 if (di->want_wait_terminate) {
883 srd_dbg("%s: %s: Will return from wait().",
884 di->inst_id, __func__);
885 g_mutex_unlock(&di->data_mutex);
889 g_mutex_unlock(&di->data_mutex);
892 PyGILState_Release(gstate);
897 PyGILState_Release(gstate);
903 * Return whether the specified channel was supplied to the decoder.
905 * @param self TODO. Must not be NULL.
906 * @param args TODO. Must not be NULL.
908 * @retval Py_True The channel has been supplied by the frontend.
909 * @retval Py_False The channel has been supplied by the frontend.
910 * @retval NULL An error occurred.
912 static PyObject *Decoder_has_channel(PyObject *self, PyObject *args)
915 struct srd_decoder_inst *di;
916 PyGILState_STATE gstate;
921 gstate = PyGILState_Ensure();
923 if (!(di = srd_inst_find_by_obj(NULL, self))) {
924 PyErr_SetString(PyExc_Exception, "decoder instance not found");
929 * Get the integer argument of self.has_channel(). Check for
930 * the range of supported PD input channel numbers.
932 if (!PyArg_ParseTuple(args, "i", &idx)) {
933 /* Let Python raise this exception. */
937 count = g_slist_length(di->decoder->channels) +
938 g_slist_length(di->decoder->opt_channels);
939 if (idx < 0 || idx >= count) {
940 srd_err("Invalid index %d, PD channel count %d.", idx, count);
941 PyErr_SetString(PyExc_IndexError, "invalid channel index");
945 PyGILState_Release(gstate);
947 return (di->dec_channelmap[idx] == -1) ? Py_False : Py_True;
950 PyGILState_Release(gstate);
955 static PyMethodDef Decoder_methods[] = {
956 {"put", Decoder_put, METH_VARARGS,
957 "Accepts a dictionary with the following keys: startsample, endsample, data"},
958 {"register", (PyCFunction)Decoder_register, METH_VARARGS|METH_KEYWORDS,
959 "Register a new output stream"},
960 {"wait", Decoder_wait, METH_VARARGS,
961 "Wait for one or more conditions to occur"},
962 {"has_channel", Decoder_has_channel, METH_VARARGS,
963 "Report whether a channel was supplied"},
964 {NULL, NULL, 0, NULL}
968 * Create the sigrokdecode.Decoder type.
970 * @return The new type object.
974 SRD_PRIV PyObject *srd_Decoder_type_new(void)
977 PyType_Slot slots[] = {
978 { Py_tp_doc, "sigrok Decoder base class" },
979 { Py_tp_methods, Decoder_methods },
980 { Py_tp_new, (void *)&PyType_GenericNew },
984 PyGILState_STATE gstate;
986 gstate = PyGILState_Ensure();
988 spec.name = "sigrokdecode.Decoder";
989 spec.basicsize = sizeof(srd_Decoder);
991 spec.flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE;
994 py_obj = PyType_FromSpec(&spec);
996 PyGILState_Release(gstate);