+
+err:
+ PyGILState_Release(gstate);
+
+ return SRD_ERR;
+
+ret_9999:
+ PyGILState_Release(gstate);
+
+ return 9999;
+}
+
+/**
+ * Create a SKIP condition list for condition-less .wait() calls.
+ *
+ * @param di Decoder instance.
+ * @param count Number of samples to skip.
+ *
+ * @retval SRD_OK The new condition list was set successfully.
+ * @retval SRD_ERR There was an error setting the new condition list.
+ * The contents of di->condition_list are undefined.
+ *
+ * This routine is a reduced and specialized version of the @ref
+ * set_new_condition_list() and @ref create_term_list() routines which
+ * gets invoked when .wait() was called without specifications for
+ * conditions. This minor duplication of the SKIP term list creation
+ * simplifies the logic and avoids the creation of expensive Python
+ * objects with "constant" values which the caller did not pass in the
+ * first place. It results in maximum sharing of match handling code
+ * paths.
+ */
+static int set_skip_condition(struct srd_decoder_inst *di, uint64_t count)
+{
+ struct srd_term *term;
+ GSList *term_list;
+
+ condition_list_free(di);
+ term = g_malloc0(sizeof(*term));
+ term->type = SRD_TERM_SKIP;
+ term->num_samples_to_skip = count;
+ term->num_samples_already_skipped = 0;
+ term_list = g_slist_append(NULL, term);
+ di->condition_list = g_slist_append(di->condition_list, term_list);
+
+ return SRD_OK;
+}
+
+static PyObject *Decoder_wait(PyObject *self, PyObject *args)
+{
+ int ret;
+ uint64_t skip_count;
+ unsigned int i;
+ gboolean found_match;
+ struct srd_decoder_inst *di;
+ PyObject *py_pinvalues, *py_matched;
+ PyGILState_STATE gstate;
+
+ if (!self || !args)
+ return NULL;
+
+ gstate = PyGILState_Ensure();
+
+ if (!(di = srd_inst_find_by_obj(NULL, self))) {
+ PyErr_SetString(PyExc_Exception, "decoder instance not found");
+ PyGILState_Release(gstate);
+ Py_RETURN_NONE;
+ }
+
+ ret = set_new_condition_list(self, args);
+ if (ret < 0) {
+ srd_dbg("%s: %s: Aborting wait().", di->inst_id, __func__);
+ goto err;
+ }
+ if (ret == 9999) {
+ /*
+ * Empty condition list, automatic match. Arrange for the
+ * execution of regular match handling code paths such that
+ * the next available sample is returned to the caller.
+ * Make sure to skip one sample when "anywhere within the
+ * stream", yet make sure to not skip sample number 0.
+ */
+ if (di->abs_cur_samplenum)
+ skip_count = 1;
+ else if (!di->condition_list)
+ skip_count = 0;
+ else
+ skip_count = 1;
+ ret = set_skip_condition(di, skip_count);
+ if (ret < 0) {
+ srd_dbg("%s: %s: Cannot setup condition-less wait().",
+ di->inst_id, __func__);
+ goto err;
+ }
+ }
+
+ while (1) {
+
+ Py_BEGIN_ALLOW_THREADS
+
+ /* Wait for new samples to process, or termination request. */
+ g_mutex_lock(&di->data_mutex);
+ while (!di->got_new_samples && !di->want_wait_terminate)
+ g_cond_wait(&di->got_new_samples_cond, &di->data_mutex);
+
+ /*
+ * Check whether any of the current condition(s) match.
+ * Arrange for termination requests to take a code path which
+ * won't find new samples to process, pretends to have processed
+ * previously stored samples, and returns to the main thread,
+ * while the termination request still gets signalled.
+ */
+ found_match = FALSE;
+ ret = process_samples_until_condition_match(di, &found_match);
+
+ Py_END_ALLOW_THREADS
+
+ /* If there's a match, set self.samplenum etc. and return. */
+ if (found_match) {
+ /* Set self.samplenum to the (absolute) sample number that matched. */
+ PyObject_SetAttrString(di->py_inst, "samplenum",
+ PyLong_FromLong(di->abs_cur_samplenum));
+
+ if (di->match_array && di->match_array->len > 0) {
+ py_matched = PyTuple_New(di->match_array->len);
+ for (i = 0; i < di->match_array->len; i++)
+ PyTuple_SetItem(py_matched, i, PyBool_FromLong(di->match_array->data[i]));
+ PyObject_SetAttrString(di->py_inst, "matched", py_matched);
+ match_array_free(di);
+ } else {
+ PyObject_SetAttrString(di->py_inst, "matched", Py_None);
+ }
+
+ py_pinvalues = get_current_pinvalues(di);
+
+ g_mutex_unlock(&di->data_mutex);
+
+ PyGILState_Release(gstate);
+
+ return py_pinvalues;
+ }
+
+ /* No match, reset state for the next chunk. */
+ di->got_new_samples = FALSE;
+ di->handled_all_samples = TRUE;
+ di->abs_start_samplenum = 0;
+ di->abs_end_samplenum = 0;
+ di->inbuf = NULL;
+ di->inbuflen = 0;
+
+ /* Signal the main thread that we handled all samples. */
+ g_cond_signal(&di->handled_all_samples_cond);
+
+ /*
+ * When termination of wait() and decode() was requested,
+ * then exit the loop after releasing the mutex.
+ */
+ if (di->want_wait_terminate) {
+ srd_dbg("%s: %s: Will return from wait().",
+ di->inst_id, __func__);
+ g_mutex_unlock(&di->data_mutex);
+ goto err;
+ }
+
+ g_mutex_unlock(&di->data_mutex);
+ }
+
+ PyGILState_Release(gstate);
+
+ Py_RETURN_NONE;
+
+err:
+ PyGILState_Release(gstate);
+
+ return NULL;
+}
+
+/**
+ * Return whether the specified channel was supplied to the decoder.
+ *
+ * @param self TODO. Must not be NULL.
+ * @param args TODO. Must not be NULL.
+ *
+ * @retval Py_True The channel has been supplied by the frontend.
+ * @retval Py_False The channel has been supplied by the frontend.
+ * @retval NULL An error occurred.
+ */
+static PyObject *Decoder_has_channel(PyObject *self, PyObject *args)
+{
+ int idx, max_idx;
+ struct srd_decoder_inst *di;
+ PyObject *py_channel;
+ PyGILState_STATE gstate;
+
+ if (!self || !args)
+ return NULL;
+
+ gstate = PyGILState_Ensure();
+
+ if (!(di = srd_inst_find_by_obj(NULL, self))) {
+ PyErr_SetString(PyExc_Exception, "decoder instance not found");
+ goto err;
+ }
+
+ /* Parse the argument of self.has_channel() into 'py_channel'. */
+ if (!PyArg_ParseTuple(args, "O", &py_channel)) {
+ /* Let Python raise this exception. */
+ goto err;
+ }
+
+ if (!PyLong_Check(py_channel)) {
+ PyErr_SetString(PyExc_Exception, "channel index not a number");
+ goto err;
+ }
+
+ idx = PyLong_AsLong(py_channel);
+ max_idx = g_slist_length(di->decoder->channels)
+ + g_slist_length(di->decoder->opt_channels) - 1;
+
+ if (idx < 0 || idx > max_idx) {
+ srd_err("Invalid channel index %d/%d.", idx, max_idx);
+ PyErr_SetString(PyExc_Exception, "invalid channel");
+ goto err;
+ }
+
+ PyGILState_Release(gstate);
+
+ return (di->dec_channelmap[idx] == -1) ? Py_False : Py_True;
+
+err:
+ PyGILState_Release(gstate);
+
+ return NULL;