+/**
+ * Get the pin values at the current sample number.
+ *
+ * @param di The decoder instance to use. Must not be NULL.
+ * The number of channels must be >= 1.
+ *
+ * @return A newly allocated PyTuple containing the pin values at the
+ * current sample number.
+ */
+static PyObject *get_current_pinvalues(const struct srd_decoder_inst *di)
+{
+ int i;
+ uint8_t sample;
+ const uint8_t *sample_pos;
+ int byte_offset, bit_offset;
+ PyObject *py_pinvalues;
+ PyGILState_STATE gstate;
+
+ if (!di) {
+ srd_err("Invalid decoder instance.");
+ return NULL;
+ }
+
+ gstate = PyGILState_Ensure();
+
+ py_pinvalues = PyTuple_New(di->dec_num_channels);
+
+ for (i = 0; i < di->dec_num_channels; i++) {
+ /* A channelmap value of -1 means "unused optional channel". */
+ if (di->dec_channelmap[i] == -1) {
+ /* Value of unused channel is 0xff, instead of 0 or 1. */
+ PyTuple_SetItem(py_pinvalues, i, PyLong_FromLong(0xff));
+ } else {
+ sample_pos = di->inbuf + ((di->abs_cur_samplenum - di->abs_start_samplenum) * di->data_unitsize);
+ byte_offset = di->dec_channelmap[i] / 8;
+ bit_offset = di->dec_channelmap[i] % 8;
+ sample = *(sample_pos + byte_offset) & (1 << bit_offset) ? 1 : 0;
+ PyTuple_SetItem(py_pinvalues, i, PyLong_FromLong(sample));
+ }
+ }
+
+ PyGILState_Release(gstate);
+
+ return py_pinvalues;
+}
+
+/**
+ * Create a list of terms in the specified condition.
+ *
+ * If there are no terms in the condition, 'term_list' will be NULL.
+ *
+ * @param py_dict A Python dict containing terms. Must not be NULL.
+ * @param term_list Pointer to a GSList which will be set to the newly
+ * created list of terms. Must not be NULL.
+ *
+ * @return SRD_OK upon success, a negative error code otherwise.
+ */
+static int create_term_list(PyObject *py_dict, GSList **term_list)
+{
+ Py_ssize_t pos = 0;
+ PyObject *py_key, *py_value;
+ struct srd_term *term;
+ uint64_t num_samples_to_skip;
+ char *term_str;
+ PyGILState_STATE gstate;
+
+ if (!py_dict || !term_list)
+ return SRD_ERR_ARG;
+
+ /* "Create" an empty GSList of terms. */
+ *term_list = NULL;
+
+ gstate = PyGILState_Ensure();
+
+ /* Iterate over all items in the current dict. */
+ while (PyDict_Next(py_dict, &pos, &py_key, &py_value)) {
+ /* Check whether the current key is a string or a number. */
+ if (PyLong_Check(py_key)) {
+ /* The key is a number. */
+ /* TODO: Check if the number is a valid channel. */
+ /* Get the value string. */
+ if ((py_pydictitem_as_str(py_dict, py_key, &term_str)) != SRD_OK) {
+ srd_err("Failed to get the value.");
+ goto err;
+ }
+ term = g_malloc(sizeof(struct srd_term));
+ term->type = get_term_type(term_str);
+ term->channel = PyLong_AsLong(py_key);
+ g_free(term_str);
+ } else if (PyUnicode_Check(py_key)) {
+ /* The key is a string. */
+ /* TODO: Check if it's "skip". */
+ if ((py_pydictitem_as_long(py_dict, py_key, &num_samples_to_skip)) != SRD_OK) {
+ srd_err("Failed to get number of samples to skip.");
+ goto err;
+ }
+ term = g_malloc(sizeof(struct srd_term));
+ term->type = SRD_TERM_SKIP;
+ term->num_samples_to_skip = num_samples_to_skip;
+ term->num_samples_already_skipped = 0;
+ } else {
+ srd_err("Term key is neither a string nor a number.");
+ goto err;
+ }
+
+ /* Add the term to the list of terms. */
+ *term_list = g_slist_append(*term_list, term);
+ }
+
+ PyGILState_Release(gstate);
+
+ return SRD_OK;
+
+err:
+ PyGILState_Release(gstate);
+
+ return SRD_ERR;
+}
+
+/**
+ * Replace the current condition list with the new one.
+ *
+ * @param self TODO. Must not be NULL.
+ * @param args TODO. Must not be NULL.
+ *
+ * @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.
+ * @retval 9999 TODO.
+ */
+static int set_new_condition_list(PyObject *self, PyObject *args)
+{
+ struct srd_decoder_inst *di;
+ GSList *term_list;
+ PyObject *py_conditionlist, *py_conds, *py_dict;
+ int i, num_conditions, ret;
+ PyGILState_STATE gstate;
+
+ if (!self || !args)
+ return SRD_ERR_ARG;
+
+ gstate = PyGILState_Ensure();
+
+ /* Get the decoder instance. */
+ if (!(di = srd_inst_find_by_obj(NULL, self))) {